cpg.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2009 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Christine Caulfield (ccaulfi@redhat.com)
  9. * Author: Jan Friesse (jfriesse@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Provides a closed process group API using the coroipcc executive
  39. */
  40. #include <config.h>
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <sys/mman.h>
  48. #include <errno.h>
  49. #include <limits.h>
  50. #include <qb/qbdefs.h>
  51. #include <qb/qbipcc.h>
  52. #include <corosync/hdb.h>
  53. #include <corosync/list.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/corodefs.h>
  56. #include <corosync/cpg.h>
  57. #include <corosync/ipc_cpg.h>
  58. #include "util.h"
  59. struct cpg_inst {
  60. qb_ipcc_connection_t *c;
  61. int finalize;
  62. void *context;
  63. union {
  64. cpg_model_data_t model_data;
  65. cpg_model_v1_data_t model_v1_data;
  66. };
  67. struct list_head iteration_list_head;
  68. };
  69. DECLARE_HDB_DATABASE(cpg_handle_t_db,NULL);
  70. struct cpg_iteration_instance_t {
  71. cpg_iteration_handle_t cpg_iteration_handle;
  72. qb_ipcc_connection_t *conn;
  73. hdb_handle_t executive_iteration_handle;
  74. struct list_head list;
  75. };
  76. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  77. /*
  78. * Internal (not visible by API) functions
  79. */
  80. static cs_error_t
  81. coroipcc_msg_send_reply_receive (
  82. qb_ipcc_connection_t *c,
  83. const struct iovec *iov,
  84. unsigned int iov_len,
  85. void *res_msg,
  86. size_t res_len)
  87. {
  88. return qb_to_cs_error(qb_ipcc_sendv_recv(c, iov, iov_len, res_msg, res_len, -1));
  89. }
  90. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  91. {
  92. list_del (&cpg_iteration_instance->list);
  93. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  94. }
  95. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  96. {
  97. struct list_head *iter, *iter_next;
  98. struct cpg_iteration_instance_t *cpg_iteration_instance;
  99. /*
  100. * Traverse thru iteration instances and delete them
  101. */
  102. for (iter = cpg_inst->iteration_list_head.next; iter != &cpg_inst->iteration_list_head;iter = iter_next) {
  103. iter_next = iter->next;
  104. cpg_iteration_instance = list_entry (iter, struct cpg_iteration_instance_t, list);
  105. cpg_iteration_instance_finalize (cpg_iteration_instance);
  106. }
  107. hdb_handle_destroy (&cpg_handle_t_db, handle);
  108. }
  109. /**
  110. * @defgroup cpg_coroipcc The closed process group API
  111. * @ingroup coroipcc
  112. *
  113. * @{
  114. */
  115. cs_error_t cpg_initialize (
  116. cpg_handle_t *handle,
  117. cpg_callbacks_t *callbacks)
  118. {
  119. cpg_model_v1_data_t model_v1_data;
  120. memset (&model_v1_data, 0, sizeof (cpg_model_v1_data_t));
  121. if (callbacks) {
  122. model_v1_data.cpg_deliver_fn = callbacks->cpg_deliver_fn;
  123. model_v1_data.cpg_confchg_fn = callbacks->cpg_confchg_fn;
  124. }
  125. return (cpg_model_initialize (handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_v1_data, NULL));
  126. }
  127. cs_error_t cpg_model_initialize (
  128. cpg_handle_t *handle,
  129. cpg_model_t model,
  130. cpg_model_data_t *model_data,
  131. void *context)
  132. {
  133. cs_error_t error;
  134. struct cpg_inst *cpg_inst;
  135. if (model != CPG_MODEL_V1) {
  136. error = CPG_ERR_INVALID_PARAM;
  137. goto error_no_destroy;
  138. }
  139. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  140. if (error != CS_OK) {
  141. goto error_no_destroy;
  142. }
  143. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  144. if (error != CS_OK) {
  145. goto error_destroy;
  146. }
  147. cpg_inst->c = qb_ipcc_connect ("cpg", IPC_REQUEST_SIZE);
  148. if (cpg_inst->c == NULL) {
  149. error = qb_to_cs_error(-errno);
  150. goto error_put_destroy;
  151. }
  152. if (model_data != NULL) {
  153. switch (model) {
  154. case CPG_MODEL_V1:
  155. memcpy (&cpg_inst->model_v1_data, model_data, sizeof (cpg_model_v1_data_t));
  156. if ((cpg_inst->model_v1_data.flags & ~(CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF)) != 0) {
  157. error = CS_ERR_INVALID_PARAM;
  158. goto error_destroy;
  159. }
  160. break;
  161. }
  162. }
  163. cpg_inst->model_data.model = model;
  164. cpg_inst->context = context;
  165. list_init(&cpg_inst->iteration_list_head);
  166. hdb_handle_put (&cpg_handle_t_db, *handle);
  167. return (CS_OK);
  168. error_put_destroy:
  169. hdb_handle_put (&cpg_handle_t_db, *handle);
  170. error_destroy:
  171. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  172. error_no_destroy:
  173. return (error);
  174. }
  175. cs_error_t cpg_finalize (
  176. cpg_handle_t handle)
  177. {
  178. struct cpg_inst *cpg_inst;
  179. struct iovec iov;
  180. struct req_lib_cpg_finalize req_lib_cpg_finalize;
  181. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  182. cs_error_t error;
  183. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  184. if (error != CS_OK) {
  185. return (error);
  186. }
  187. /*
  188. * Another thread has already started finalizing
  189. */
  190. if (cpg_inst->finalize) {
  191. hdb_handle_put (&cpg_handle_t_db, handle);
  192. return (CPG_ERR_BAD_HANDLE);
  193. }
  194. cpg_inst->finalize = 1;
  195. /*
  196. * Send service request
  197. */
  198. req_lib_cpg_finalize.header.size = sizeof (struct req_lib_cpg_finalize);
  199. req_lib_cpg_finalize.header.id = MESSAGE_REQ_CPG_FINALIZE;
  200. iov.iov_base = (void *)&req_lib_cpg_finalize;
  201. iov.iov_len = sizeof (struct req_lib_cpg_finalize);
  202. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  203. &iov,
  204. 1,
  205. &res_lib_cpg_finalize,
  206. sizeof (struct res_lib_cpg_finalize));
  207. qb_ipcc_disconnect(cpg_inst->c);
  208. cpg_inst_finalize (cpg_inst, handle);
  209. hdb_handle_put (&cpg_handle_t_db, handle);
  210. return (error);
  211. }
  212. cs_error_t cpg_fd_get (
  213. cpg_handle_t handle,
  214. int *fd)
  215. {
  216. cs_error_t error;
  217. struct cpg_inst *cpg_inst;
  218. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  219. if (error != CS_OK) {
  220. return (error);
  221. }
  222. error = qb_to_cs_error (qb_ipcc_fd_get (cpg_inst->c, fd));
  223. hdb_handle_put (&cpg_handle_t_db, handle);
  224. return (error);
  225. }
  226. cs_error_t cpg_context_get (
  227. cpg_handle_t handle,
  228. void **context)
  229. {
  230. cs_error_t error;
  231. struct cpg_inst *cpg_inst;
  232. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  233. if (error != CS_OK) {
  234. return (error);
  235. }
  236. *context = cpg_inst->context;
  237. hdb_handle_put (&cpg_handle_t_db, handle);
  238. return (CS_OK);
  239. }
  240. cs_error_t cpg_context_set (
  241. cpg_handle_t handle,
  242. void *context)
  243. {
  244. cs_error_t error;
  245. struct cpg_inst *cpg_inst;
  246. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  247. if (error != CS_OK) {
  248. return (error);
  249. }
  250. cpg_inst->context = context;
  251. hdb_handle_put (&cpg_handle_t_db, handle);
  252. return (CS_OK);
  253. }
  254. cs_error_t cpg_dispatch (
  255. cpg_handle_t handle,
  256. cs_dispatch_flags_t dispatch_types)
  257. {
  258. int timeout = -1;
  259. cs_error_t error;
  260. int cont = 1; /* always continue do loop except when set to 0 */
  261. struct cpg_inst *cpg_inst;
  262. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  263. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  264. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  265. struct cpg_inst cpg_inst_copy;
  266. struct qb_ipc_response_header *dispatch_data;
  267. struct cpg_address member_list[CPG_MEMBERS_MAX];
  268. struct cpg_address left_list[CPG_MEMBERS_MAX];
  269. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  270. struct cpg_name group_name;
  271. mar_cpg_address_t *left_list_start;
  272. mar_cpg_address_t *joined_list_start;
  273. unsigned int i;
  274. struct cpg_ring_id ring_id;
  275. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  276. int32_t errno_res;
  277. char dispatch_buf[IPC_DISPATCH_SIZE];
  278. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  279. if (error != CS_OK) {
  280. return (error);
  281. }
  282. /*
  283. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  284. * wait indefinately for CS_DISPATCH_BLOCKING
  285. */
  286. if (dispatch_types == CPG_DISPATCH_ALL) {
  287. timeout = 0;
  288. }
  289. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  290. do {
  291. errno_res = qb_ipcc_event_recv (
  292. cpg_inst->c,
  293. dispatch_buf,
  294. IPC_DISPATCH_SIZE,
  295. timeout);
  296. error = qb_to_cs_error (errno_res);
  297. if (error == CS_ERR_BAD_HANDLE) {
  298. error = CS_OK;
  299. goto error_put;
  300. }
  301. if (error == CS_ERR_TRY_AGAIN) {
  302. error = CS_OK;
  303. if (dispatch_types == CPG_DISPATCH_ALL) {
  304. break; /* exit do while cont is 1 loop */
  305. } else {
  306. continue; /* next poll */
  307. }
  308. }
  309. if (error != CS_OK) {
  310. goto error_put;
  311. }
  312. /*
  313. * Make copy of callbacks, message data, unlock instance, and call callback
  314. * A risk of this dispatch method is that the callback routines may
  315. * operate at the same time that cpgFinalize has been called.
  316. */
  317. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  318. switch (cpg_inst_copy.model_data.model) {
  319. case CPG_MODEL_V1:
  320. /*
  321. * Dispatch incoming message
  322. */
  323. switch (dispatch_data->id) {
  324. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  325. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  326. break;
  327. }
  328. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  329. marshall_from_mar_cpg_name_t (
  330. &group_name,
  331. &res_cpg_deliver_callback->group_name);
  332. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  333. &group_name,
  334. res_cpg_deliver_callback->nodeid,
  335. res_cpg_deliver_callback->pid,
  336. &res_cpg_deliver_callback->message,
  337. res_cpg_deliver_callback->msglen);
  338. break;
  339. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  340. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  341. break;
  342. }
  343. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  344. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  345. marshall_from_mar_cpg_address_t (&member_list[i],
  346. &res_cpg_confchg_callback->member_list[i]);
  347. }
  348. left_list_start = res_cpg_confchg_callback->member_list +
  349. res_cpg_confchg_callback->member_list_entries;
  350. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  351. marshall_from_mar_cpg_address_t (&left_list[i],
  352. &left_list_start[i]);
  353. }
  354. joined_list_start = res_cpg_confchg_callback->member_list +
  355. res_cpg_confchg_callback->member_list_entries +
  356. res_cpg_confchg_callback->left_list_entries;
  357. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  358. marshall_from_mar_cpg_address_t (&joined_list[i],
  359. &joined_list_start[i]);
  360. }
  361. marshall_from_mar_cpg_name_t (
  362. &group_name,
  363. &res_cpg_confchg_callback->group_name);
  364. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  365. &group_name,
  366. member_list,
  367. res_cpg_confchg_callback->member_list_entries,
  368. left_list,
  369. res_cpg_confchg_callback->left_list_entries,
  370. joined_list,
  371. res_cpg_confchg_callback->joined_list_entries);
  372. break;
  373. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  374. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  375. break;
  376. }
  377. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  378. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  379. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  380. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  381. }
  382. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  383. ring_id,
  384. res_cpg_totem_confchg_callback->member_list_entries,
  385. totem_member_list);
  386. break;
  387. default:
  388. error = CS_ERR_LIBRARY;
  389. goto error_put;
  390. break;
  391. } /* - switch (dispatch_data->id) */
  392. break; /* case CPG_MODEL_V1 */
  393. } /* - switch (cpg_inst_copy.model_data.model) */
  394. /*
  395. * Determine if more messages should be processed
  396. */
  397. if (dispatch_types == CS_DISPATCH_ONE) {
  398. cont = 0;
  399. }
  400. } while (cont);
  401. error_put:
  402. hdb_handle_put (&cpg_handle_t_db, handle);
  403. return (error);
  404. }
  405. cs_error_t cpg_join (
  406. cpg_handle_t handle,
  407. const struct cpg_name *group)
  408. {
  409. cs_error_t error;
  410. struct cpg_inst *cpg_inst;
  411. struct iovec iov[2];
  412. struct req_lib_cpg_join req_lib_cpg_join;
  413. struct res_lib_cpg_join response;
  414. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  415. if (error != CS_OK) {
  416. return (error);
  417. }
  418. /* Now join */
  419. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  420. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  421. req_lib_cpg_join.pid = getpid();
  422. req_lib_cpg_join.flags = 0;
  423. switch (cpg_inst->model_data.model) {
  424. case CPG_MODEL_V1:
  425. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  426. break;
  427. }
  428. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  429. group);
  430. iov[0].iov_base = (void *)&req_lib_cpg_join;
  431. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  432. do {
  433. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  434. &response, sizeof (struct res_lib_cpg_join));
  435. if (error != CS_OK) {
  436. goto error_exit;
  437. }
  438. } while (response.header.error == CPG_ERR_BUSY);
  439. error = response.header.error;
  440. error_exit:
  441. hdb_handle_put (&cpg_handle_t_db, handle);
  442. return (error);
  443. }
  444. cs_error_t cpg_leave (
  445. cpg_handle_t handle,
  446. const struct cpg_name *group)
  447. {
  448. cs_error_t error;
  449. struct cpg_inst *cpg_inst;
  450. struct iovec iov[2];
  451. struct req_lib_cpg_leave req_lib_cpg_leave;
  452. struct res_lib_cpg_leave res_lib_cpg_leave;
  453. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  454. if (error != CS_OK) {
  455. return (error);
  456. }
  457. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  458. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  459. req_lib_cpg_leave.pid = getpid();
  460. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  461. group);
  462. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  463. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  464. do {
  465. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  466. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  467. if (error != CS_OK) {
  468. goto error_exit;
  469. }
  470. } while (res_lib_cpg_leave.header.error == CPG_ERR_BUSY);
  471. error = res_lib_cpg_leave.header.error;
  472. error_exit:
  473. hdb_handle_put (&cpg_handle_t_db, handle);
  474. return (error);
  475. }
  476. cs_error_t cpg_membership_get (
  477. cpg_handle_t handle,
  478. struct cpg_name *group_name,
  479. struct cpg_address *member_list,
  480. int *member_list_entries)
  481. {
  482. cs_error_t error;
  483. struct cpg_inst *cpg_inst;
  484. struct iovec iov;
  485. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  486. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  487. unsigned int i;
  488. if (member_list == NULL) {
  489. return (CS_ERR_INVALID_PARAM);
  490. }
  491. if (member_list_entries == NULL) {
  492. return (CS_ERR_INVALID_PARAM);
  493. }
  494. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  495. if (error != CS_OK) {
  496. return (error);
  497. }
  498. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  499. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  500. memcpy (&req_lib_cpg_membership_get.group_name, group_name,
  501. sizeof (struct cpg_name));
  502. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  503. iov.iov_len = sizeof (struct qb_ipc_request_header);
  504. do {
  505. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  506. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  507. if (error != CS_OK) {
  508. goto error_exit;
  509. }
  510. } while (res_lib_cpg_membership_get.header.error == CPG_ERR_BUSY);
  511. error = res_lib_cpg_membership_get.header.error;
  512. /*
  513. * Copy results to caller
  514. */
  515. *member_list_entries = res_lib_cpg_membership_get.member_count;
  516. if (member_list) {
  517. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  518. marshall_from_mar_cpg_address_t (&member_list[i],
  519. &res_lib_cpg_membership_get.member_list[i]);
  520. }
  521. }
  522. error_exit:
  523. hdb_handle_put (&cpg_handle_t_db, handle);
  524. return (error);
  525. }
  526. cs_error_t cpg_local_get (
  527. cpg_handle_t handle,
  528. unsigned int *local_nodeid)
  529. {
  530. cs_error_t error;
  531. struct cpg_inst *cpg_inst;
  532. struct iovec iov;
  533. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  534. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  535. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  536. if (error != CS_OK) {
  537. return (error);
  538. }
  539. req_lib_cpg_local_get.header.size = sizeof (struct qb_ipc_request_header);
  540. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  541. iov.iov_base = (void *)&req_lib_cpg_local_get;
  542. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  543. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  544. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  545. if (error != CS_OK) {
  546. goto error_exit;
  547. }
  548. error = res_lib_cpg_local_get.header.error;
  549. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  550. error_exit:
  551. hdb_handle_put (&cpg_handle_t_db, handle);
  552. return (error);
  553. }
  554. cs_error_t cpg_flow_control_state_get (
  555. cpg_handle_t handle,
  556. cpg_flow_control_state_t *flow_control_state)
  557. {
  558. cs_error_t error;
  559. struct cpg_inst *cpg_inst;
  560. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  561. if (error != CS_OK) {
  562. return (error);
  563. }
  564. *flow_control_state = CPG_FLOW_CONTROL_DISABLED;
  565. error = CS_OK;
  566. hdb_handle_put (&cpg_handle_t_db, handle);
  567. return (error);
  568. }
  569. static int
  570. memory_map (char *path, const char *file, void **buf, size_t bytes)
  571. {
  572. int32_t fd;
  573. void *addr_orig;
  574. void *addr;
  575. int32_t res;
  576. char *buffer;
  577. int32_t i;
  578. size_t written;
  579. size_t page_size;
  580. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  581. fd = mkstemp (path);
  582. if (fd == -1) {
  583. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  584. fd = mkstemp (path);
  585. if (fd == -1) {
  586. return (-1);
  587. }
  588. }
  589. res = ftruncate (fd, bytes);
  590. if (res == -1) {
  591. goto error_close_unlink;
  592. }
  593. page_size = (size_t)sysconf(_SC_PAGESIZE);
  594. buffer = malloc (page_size);
  595. if (buffer == NULL) {
  596. goto error_close_unlink;
  597. }
  598. memset (buffer, 0, page_size);
  599. for (i = 0; i < (bytes / page_size); i++) {
  600. retry_write:
  601. written = write (fd, buffer, page_size);
  602. if (written == -1 && errno == EINTR) {
  603. goto retry_write;
  604. }
  605. if (written != page_size) {
  606. free (buffer);
  607. goto error_close_unlink;
  608. }
  609. }
  610. free (buffer);
  611. addr_orig = mmap (NULL, bytes, PROT_NONE,
  612. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  613. if (addr_orig == MAP_FAILED) {
  614. goto error_close_unlink;
  615. }
  616. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  617. MAP_FIXED | MAP_SHARED, fd, 0);
  618. if (addr != addr_orig) {
  619. goto error_close_unlink;
  620. }
  621. #ifdef COROSYNC_BSD
  622. madvise(addr_orig, bytes, MADV_NOSYNC);
  623. #endif
  624. res = close (fd);
  625. if (res) {
  626. return (-1);
  627. }
  628. *buf = addr_orig;
  629. return 0;
  630. error_close_unlink:
  631. close (fd);
  632. unlink(path);
  633. return -1;
  634. }
  635. cs_error_t cpg_zcb_alloc (
  636. cpg_handle_t handle,
  637. size_t size,
  638. void **buffer)
  639. {
  640. void *buf = NULL;
  641. char path[PATH_MAX];
  642. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  643. struct qb_ipc_response_header res_coroipcs_zc_alloc;
  644. size_t map_size;
  645. struct iovec iovec;
  646. struct coroipcs_zc_header *hdr;
  647. cs_error_t error;
  648. struct cpg_inst *cpg_inst;
  649. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  650. if (error != CS_OK) {
  651. return (error);
  652. }
  653. map_size = size + sizeof (struct req_lib_cpg_mcast) + sizeof (struct coroipcs_zc_header);
  654. assert(memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size) != -1);
  655. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  656. req_coroipcc_zc_alloc.header.id = MESSAGE_REQ_CPG_ZC_ALLOC;
  657. req_coroipcc_zc_alloc.map_size = map_size;
  658. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  659. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  660. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  661. error = coroipcc_msg_send_reply_receive (
  662. cpg_inst->c,
  663. &iovec,
  664. 1,
  665. &res_coroipcs_zc_alloc,
  666. sizeof (struct qb_ipc_response_header));
  667. hdr = (struct coroipcs_zc_header *)buf;
  668. hdr->map_size = map_size;
  669. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  670. hdb_handle_put (&cpg_handle_t_db, handle);
  671. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  672. return (error);
  673. }
  674. cs_error_t cpg_zcb_free (
  675. cpg_handle_t handle,
  676. void *buffer)
  677. {
  678. cs_error_t error;
  679. struct cpg_inst *cpg_inst;
  680. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  681. struct qb_ipc_response_header res_coroipcs_zc_free;
  682. struct iovec iovec;
  683. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  684. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  685. if (error != CS_OK) {
  686. return (error);
  687. }
  688. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  689. req_coroipcc_zc_free.header.id = MESSAGE_REQ_CPG_ZC_FREE;
  690. req_coroipcc_zc_free.map_size = header->map_size;
  691. req_coroipcc_zc_free.server_address = header->server_address;
  692. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  693. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  694. error = coroipcc_msg_send_reply_receive (
  695. cpg_inst->c,
  696. &iovec,
  697. 1,
  698. &res_coroipcs_zc_free,
  699. sizeof (struct qb_ipc_response_header));
  700. munmap ((void *)header, header->map_size);
  701. hdb_handle_put (&cpg_handle_t_db, handle);
  702. return (error);
  703. }
  704. cs_error_t cpg_zcb_mcast_joined (
  705. cpg_handle_t handle,
  706. cpg_guarantee_t guarantee,
  707. void *msg,
  708. size_t msg_len)
  709. {
  710. cs_error_t error;
  711. struct cpg_inst *cpg_inst;
  712. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  713. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  714. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  715. struct coroipcs_zc_header *hdr;
  716. struct iovec iovec;
  717. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  718. if (error != CS_OK) {
  719. return (error);
  720. }
  721. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  722. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  723. msg_len;
  724. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  725. req_lib_cpg_mcast->guarantee = guarantee;
  726. req_lib_cpg_mcast->msglen = msg_len;
  727. hdr = (struct coroipcs_zc_header *)(((char *)req_lib_cpg_mcast) - sizeof (struct coroipcs_zc_header));
  728. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  729. req_coroipcc_zc_execute.header.id = MESSAGE_REQ_CPG_ZC_EXECUTE;
  730. req_coroipcc_zc_execute.server_address = hdr->server_address;
  731. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  732. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  733. error = coroipcc_msg_send_reply_receive (
  734. cpg_inst->c,
  735. &iovec,
  736. 1,
  737. &res_lib_cpg_mcast,
  738. sizeof(res_lib_cpg_mcast));
  739. if (error != CS_OK) {
  740. goto error_exit;
  741. }
  742. error = res_lib_cpg_mcast.header.error;
  743. error_exit:
  744. hdb_handle_put (&cpg_handle_t_db, handle);
  745. return (error);
  746. }
  747. cs_error_t cpg_mcast_joined (
  748. cpg_handle_t handle,
  749. cpg_guarantee_t guarantee,
  750. const struct iovec *iovec,
  751. unsigned int iov_len)
  752. {
  753. int i;
  754. cs_error_t error;
  755. struct cpg_inst *cpg_inst;
  756. struct iovec iov[64];
  757. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  758. size_t msg_len = 0;
  759. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  760. if (error != CS_OK) {
  761. return (error);
  762. }
  763. for (i = 0; i < iov_len; i++ ) {
  764. msg_len += iovec[i].iov_len;
  765. }
  766. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  767. msg_len;
  768. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  769. req_lib_cpg_mcast.guarantee = guarantee;
  770. req_lib_cpg_mcast.msglen = msg_len;
  771. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  772. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  773. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  774. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  775. error = qb_to_cs_error(qb_ipcc_sendv(cpg_inst->c, iov, iov_len + 1));
  776. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  777. hdb_handle_put (&cpg_handle_t_db, handle);
  778. return (error);
  779. }
  780. cs_error_t cpg_iteration_initialize(
  781. cpg_handle_t handle,
  782. cpg_iteration_type_t iteration_type,
  783. const struct cpg_name *group,
  784. cpg_iteration_handle_t *cpg_iteration_handle)
  785. {
  786. cs_error_t error;
  787. struct iovec iov;
  788. struct cpg_inst *cpg_inst;
  789. struct cpg_iteration_instance_t *cpg_iteration_instance;
  790. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  791. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  792. if (cpg_iteration_handle == NULL) {
  793. return (CS_ERR_INVALID_PARAM);
  794. }
  795. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  796. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  797. return (CS_ERR_INVALID_PARAM);
  798. }
  799. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  800. iteration_type != CPG_ITERATION_ALL) {
  801. return (CS_ERR_INVALID_PARAM);
  802. }
  803. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  804. if (error != CS_OK) {
  805. return (error);
  806. }
  807. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  808. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  809. if (error != CS_OK) {
  810. goto error_put_cpg_db;
  811. }
  812. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  813. (void *)&cpg_iteration_instance));
  814. if (error != CS_OK) {
  815. goto error_destroy;
  816. }
  817. cpg_iteration_instance->conn = cpg_inst->c;
  818. list_init (&cpg_iteration_instance->list);
  819. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  820. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  821. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  822. if (group) {
  823. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  824. }
  825. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  826. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  827. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  828. &iov,
  829. 1,
  830. &res_lib_cpg_iterationinitialize,
  831. sizeof (struct res_lib_cpg_iterationinitialize));
  832. if (error != CS_OK) {
  833. goto error_put_destroy;
  834. }
  835. cpg_iteration_instance->executive_iteration_handle =
  836. res_lib_cpg_iterationinitialize.iteration_handle;
  837. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  838. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  839. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  840. hdb_handle_put (&cpg_handle_t_db, handle);
  841. return (res_lib_cpg_iterationinitialize.header.error);
  842. error_put_destroy:
  843. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  844. error_destroy:
  845. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  846. error_put_cpg_db:
  847. hdb_handle_put (&cpg_handle_t_db, handle);
  848. return (error);
  849. }
  850. cs_error_t cpg_iteration_next(
  851. cpg_iteration_handle_t handle,
  852. struct cpg_iteration_description_t *description)
  853. {
  854. cs_error_t error;
  855. struct cpg_iteration_instance_t *cpg_iteration_instance;
  856. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  857. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  858. if (description == NULL) {
  859. return CS_ERR_INVALID_PARAM;
  860. }
  861. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  862. (void *)&cpg_iteration_instance));
  863. if (error != CS_OK) {
  864. goto error_exit;
  865. }
  866. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  867. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  868. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  869. error = qb_to_cs_error (qb_ipcc_send (cpg_iteration_instance->conn,
  870. &req_lib_cpg_iterationnext,
  871. req_lib_cpg_iterationnext.header.size));
  872. if (error != CS_OK) {
  873. goto error_put;
  874. }
  875. error = qb_to_cs_error (qb_ipcc_recv (cpg_iteration_instance->conn,
  876. &res_lib_cpg_iterationnext,
  877. sizeof(struct res_lib_cpg_iterationnext), -1));
  878. if (error != CS_OK) {
  879. goto error_put;
  880. }
  881. marshall_from_mar_cpg_iteration_description_t(
  882. description,
  883. &res_lib_cpg_iterationnext.description);
  884. error = res_lib_cpg_iterationnext.header.error;
  885. error_put:
  886. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  887. error_exit:
  888. return (error);
  889. }
  890. cs_error_t cpg_iteration_finalize (
  891. cpg_iteration_handle_t handle)
  892. {
  893. cs_error_t error;
  894. struct iovec iov;
  895. struct cpg_iteration_instance_t *cpg_iteration_instance;
  896. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  897. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  898. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  899. (void *)&cpg_iteration_instance));
  900. if (error != CS_OK) {
  901. goto error_exit;
  902. }
  903. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  904. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  905. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  906. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  907. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  908. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn,
  909. &iov,
  910. 1,
  911. &res_lib_cpg_iterationfinalize,
  912. sizeof (struct req_lib_cpg_iterationfinalize));
  913. if (error != CS_OK) {
  914. goto error_put;
  915. }
  916. cpg_iteration_instance_finalize (cpg_iteration_instance);
  917. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  918. return (res_lib_cpg_iterationfinalize.header.error);
  919. error_put:
  920. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  921. error_exit:
  922. return (error);
  923. }
  924. /** @} */