cpg.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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 <string.h>
  43. #include <unistd.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <errno.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/coroipc_types.h>
  49. #include <corosync/coroipcc.h>
  50. #include <corosync/corodefs.h>
  51. #include <corosync/hdb.h>
  52. #include <corosync/list.h>
  53. #include <corosync/cpg.h>
  54. #include <corosync/ipc_cpg.h>
  55. #include "util.h"
  56. struct cpg_inst {
  57. hdb_handle_t handle;
  58. int finalize;
  59. void *context;
  60. union {
  61. cpg_model_data_t model_data;
  62. cpg_model_v1_data_t model_v1_data;
  63. };
  64. struct list_head iteration_list_head;
  65. };
  66. DECLARE_HDB_DATABASE(cpg_handle_t_db,NULL);
  67. struct cpg_iteration_instance_t {
  68. cpg_iteration_handle_t cpg_iteration_handle;
  69. hdb_handle_t conn_handle;
  70. hdb_handle_t executive_iteration_handle;
  71. struct list_head list;
  72. };
  73. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  74. /*
  75. * Internal (not visible by API) functions
  76. */
  77. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  78. {
  79. list_del (&cpg_iteration_instance->list);
  80. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  81. }
  82. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  83. {
  84. struct list_head *iter, *iter_next;
  85. struct cpg_iteration_instance_t *cpg_iteration_instance;
  86. /*
  87. * Traverse thru iteration instances and delete them
  88. */
  89. for (iter = cpg_inst->iteration_list_head.next; iter != &cpg_inst->iteration_list_head;iter = iter_next) {
  90. iter_next = iter->next;
  91. cpg_iteration_instance = list_entry (iter, struct cpg_iteration_instance_t, list);
  92. cpg_iteration_instance_finalize (cpg_iteration_instance);
  93. }
  94. hdb_handle_destroy (&cpg_handle_t_db, handle);
  95. }
  96. /**
  97. * @defgroup cpg_coroipcc The closed process group API
  98. * @ingroup coroipcc
  99. *
  100. * @{
  101. */
  102. cs_error_t cpg_initialize (
  103. cpg_handle_t *handle,
  104. cpg_callbacks_t *callbacks)
  105. {
  106. cpg_model_v1_data_t model_v1_data;
  107. memset (&model_v1_data, 0, sizeof (cpg_model_v1_data_t));
  108. if (callbacks) {
  109. model_v1_data.cpg_deliver_fn = callbacks->cpg_deliver_fn;
  110. model_v1_data.cpg_confchg_fn = callbacks->cpg_confchg_fn;
  111. }
  112. return (cpg_model_initialize (handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_v1_data, NULL));
  113. }
  114. cs_error_t cpg_model_initialize (
  115. cpg_handle_t *handle,
  116. cpg_model_t model,
  117. cpg_model_data_t *model_data,
  118. void *context)
  119. {
  120. cs_error_t error;
  121. struct cpg_inst *cpg_inst;
  122. if (model != CPG_MODEL_V1) {
  123. error = CPG_ERR_INVALID_PARAM;
  124. goto error_no_destroy;
  125. }
  126. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  127. if (error != CS_OK) {
  128. goto error_no_destroy;
  129. }
  130. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  131. if (error != CS_OK) {
  132. goto error_destroy;
  133. }
  134. error = coroipcc_service_connect (
  135. COROSYNC_SOCKET_NAME,
  136. CPG_SERVICE,
  137. IPC_REQUEST_SIZE,
  138. IPC_RESPONSE_SIZE,
  139. IPC_DISPATCH_SIZE,
  140. &cpg_inst->handle);
  141. if (error != CS_OK) {
  142. goto error_put_destroy;
  143. }
  144. if (model_data != NULL) {
  145. switch (model) {
  146. case CPG_MODEL_V1:
  147. memcpy (&cpg_inst->model_v1_data, model_data, sizeof (cpg_model_v1_data_t));
  148. if ((cpg_inst->model_v1_data.flags & ~(CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF)) != 0) {
  149. error = CS_ERR_INVALID_PARAM;
  150. goto error_destroy;
  151. }
  152. break;
  153. default:
  154. error = CS_ERR_LIBRARY;
  155. goto error_destroy;
  156. break;
  157. }
  158. }
  159. cpg_inst->model_data.model = model;
  160. cpg_inst->context = context;
  161. list_init(&cpg_inst->iteration_list_head);
  162. hdb_handle_put (&cpg_handle_t_db, *handle);
  163. return (CS_OK);
  164. error_put_destroy:
  165. hdb_handle_put (&cpg_handle_t_db, *handle);
  166. error_destroy:
  167. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  168. error_no_destroy:
  169. return (error);
  170. }
  171. cs_error_t cpg_finalize (
  172. cpg_handle_t handle)
  173. {
  174. struct cpg_inst *cpg_inst;
  175. struct iovec iov;
  176. struct req_lib_cpg_finalize req_lib_cpg_finalize;
  177. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  178. cs_error_t error;
  179. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  180. if (error != CS_OK) {
  181. return (error);
  182. }
  183. /*
  184. * Another thread has already started finalizing
  185. */
  186. if (cpg_inst->finalize) {
  187. hdb_handle_put (&cpg_handle_t_db, handle);
  188. return (CPG_ERR_BAD_HANDLE);
  189. }
  190. cpg_inst->finalize = 1;
  191. /*
  192. * Send service request
  193. */
  194. req_lib_cpg_finalize.header.size = sizeof (struct req_lib_cpg_finalize);
  195. req_lib_cpg_finalize.header.id = MESSAGE_REQ_CPG_FINALIZE;
  196. iov.iov_base = (void *)&req_lib_cpg_finalize;
  197. iov.iov_len = sizeof (struct req_lib_cpg_finalize);
  198. error = coroipcc_msg_send_reply_receive (cpg_inst->handle,
  199. &iov,
  200. 1,
  201. &res_lib_cpg_finalize,
  202. sizeof (struct req_lib_cpg_finalize));
  203. if (error != CS_OK) {
  204. goto error_put;
  205. }
  206. coroipcc_service_disconnect (cpg_inst->handle);
  207. cpg_inst_finalize (cpg_inst, handle);
  208. hdb_handle_put (&cpg_handle_t_db, handle);
  209. return (CPG_OK);
  210. error_put:
  211. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  212. cpg_inst->finalize = 0;
  213. return (error);
  214. }
  215. cs_error_t cpg_fd_get (
  216. cpg_handle_t handle,
  217. int *fd)
  218. {
  219. cs_error_t error;
  220. struct cpg_inst *cpg_inst;
  221. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  222. if (error != CS_OK) {
  223. return (error);
  224. }
  225. error = coroipcc_fd_get (cpg_inst->handle, fd);
  226. hdb_handle_put (&cpg_handle_t_db, handle);
  227. return (error);
  228. }
  229. cs_error_t cpg_context_get (
  230. cpg_handle_t handle,
  231. void **context)
  232. {
  233. cs_error_t error;
  234. struct cpg_inst *cpg_inst;
  235. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  236. if (error != CS_OK) {
  237. return (error);
  238. }
  239. *context = cpg_inst->context;
  240. hdb_handle_put (&cpg_handle_t_db, handle);
  241. return (CS_OK);
  242. }
  243. cs_error_t cpg_context_set (
  244. cpg_handle_t handle,
  245. void *context)
  246. {
  247. cs_error_t error;
  248. struct cpg_inst *cpg_inst;
  249. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  250. if (error != CS_OK) {
  251. return (error);
  252. }
  253. cpg_inst->context = context;
  254. hdb_handle_put (&cpg_handle_t_db, handle);
  255. return (CS_OK);
  256. }
  257. cs_error_t cpg_dispatch (
  258. cpg_handle_t handle,
  259. cs_dispatch_flags_t dispatch_types)
  260. {
  261. int timeout = -1;
  262. cs_error_t error;
  263. int cont = 1; /* always continue do loop except when set to 0 */
  264. struct cpg_inst *cpg_inst;
  265. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  266. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  267. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  268. struct cpg_inst cpg_inst_copy;
  269. coroipc_response_header_t *dispatch_data;
  270. struct cpg_address member_list[CPG_MEMBERS_MAX];
  271. struct cpg_address left_list[CPG_MEMBERS_MAX];
  272. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  273. struct cpg_name group_name;
  274. mar_cpg_address_t *left_list_start;
  275. mar_cpg_address_t *joined_list_start;
  276. unsigned int i;
  277. struct cpg_ring_id ring_id;
  278. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  279. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  280. if (error != CS_OK) {
  281. return (error);
  282. }
  283. /*
  284. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  285. * wait indefinately for CS_DISPATCH_BLOCKING
  286. */
  287. if (dispatch_types == CPG_DISPATCH_ALL) {
  288. timeout = 0;
  289. }
  290. do {
  291. error = coroipcc_dispatch_get (
  292. cpg_inst->handle,
  293. (void **)&dispatch_data,
  294. timeout);
  295. if (error == CS_ERR_BAD_HANDLE) {
  296. error = CS_OK;
  297. goto error_put;
  298. }
  299. if (error == CS_ERR_TRY_AGAIN) {
  300. error = CS_OK;
  301. if (dispatch_types == CPG_DISPATCH_ALL) {
  302. break; /* exit do while cont is 1 loop */
  303. } else {
  304. continue; /* next poll */
  305. }
  306. }
  307. if (error != CS_OK) {
  308. goto error_put;
  309. }
  310. /*
  311. * Make copy of callbacks, message data, unlock instance, and call callback
  312. * A risk of this dispatch method is that the callback routines may
  313. * operate at the same time that cpgFinalize has been called.
  314. */
  315. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  316. switch (cpg_inst_copy.model_data.model) {
  317. case CPG_MODEL_V1:
  318. /*
  319. * Dispatch incoming message
  320. */
  321. switch (dispatch_data->id) {
  322. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  323. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  324. break;
  325. }
  326. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  327. marshall_from_mar_cpg_name_t (
  328. &group_name,
  329. &res_cpg_deliver_callback->group_name);
  330. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  331. &group_name,
  332. res_cpg_deliver_callback->nodeid,
  333. res_cpg_deliver_callback->pid,
  334. &res_cpg_deliver_callback->message,
  335. res_cpg_deliver_callback->msglen);
  336. break;
  337. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  338. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  339. break;
  340. }
  341. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  342. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  343. marshall_from_mar_cpg_address_t (&member_list[i],
  344. &res_cpg_confchg_callback->member_list[i]);
  345. }
  346. left_list_start = res_cpg_confchg_callback->member_list +
  347. res_cpg_confchg_callback->member_list_entries;
  348. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  349. marshall_from_mar_cpg_address_t (&left_list[i],
  350. &left_list_start[i]);
  351. }
  352. joined_list_start = res_cpg_confchg_callback->member_list +
  353. res_cpg_confchg_callback->member_list_entries +
  354. res_cpg_confchg_callback->left_list_entries;
  355. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  356. marshall_from_mar_cpg_address_t (&joined_list[i],
  357. &joined_list_start[i]);
  358. }
  359. marshall_from_mar_cpg_name_t (
  360. &group_name,
  361. &res_cpg_confchg_callback->group_name);
  362. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  363. &group_name,
  364. member_list,
  365. res_cpg_confchg_callback->member_list_entries,
  366. left_list,
  367. res_cpg_confchg_callback->left_list_entries,
  368. joined_list,
  369. res_cpg_confchg_callback->joined_list_entries);
  370. break;
  371. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  372. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  373. break;
  374. }
  375. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  376. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  377. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  378. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  379. }
  380. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  381. ring_id,
  382. res_cpg_totem_confchg_callback->member_list_entries,
  383. totem_member_list);
  384. break;
  385. default:
  386. coroipcc_dispatch_put (cpg_inst->handle);
  387. error = CS_ERR_LIBRARY;
  388. goto error_put;
  389. break;
  390. } /* - switch (dispatch_data->id) */
  391. break; /* case CPG_MODEL_V1 */
  392. } /* - switch (cpg_inst_copy.model_data.model) */
  393. coroipcc_dispatch_put (cpg_inst->handle);
  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 res_lib_cpg_join;
  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->handle, iov, 1,
  434. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  435. if (error != CS_OK) {
  436. goto error_exit;
  437. }
  438. } while (res_lib_cpg_join.header.error == CPG_ERR_BUSY);
  439. error = res_lib_cpg_join.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->handle, 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 req_lib_cpg_membership_get;
  486. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  487. unsigned int i;
  488. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  489. if (error != CS_OK) {
  490. return (error);
  491. }
  492. req_lib_cpg_membership_get.header.size = sizeof (coroipc_request_header_t);
  493. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  494. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  495. iov.iov_len = sizeof (coroipc_request_header_t);
  496. do {
  497. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  498. &res_lib_cpg_membership_get, sizeof (coroipc_response_header_t));
  499. if (error != CS_OK) {
  500. goto error_exit;
  501. }
  502. } while (res_lib_cpg_membership_get.header.error == CPG_ERR_BUSY);
  503. error = res_lib_cpg_membership_get.header.error;
  504. /*
  505. * Copy results to caller
  506. */
  507. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  508. if (member_list) {
  509. for (i = 0; i < res_lib_cpg_membership_get.member_list_entries; i++) {
  510. marshall_from_mar_cpg_address_t (&member_list[i],
  511. &res_lib_cpg_membership_get.member_list[i]);
  512. }
  513. }
  514. error_exit:
  515. hdb_handle_put (&cpg_handle_t_db, handle);
  516. return (error);
  517. }
  518. cs_error_t cpg_local_get (
  519. cpg_handle_t handle,
  520. unsigned int *local_nodeid)
  521. {
  522. cs_error_t error;
  523. struct cpg_inst *cpg_inst;
  524. struct iovec iov;
  525. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  526. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  527. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  528. if (error != CS_OK) {
  529. return (error);
  530. }
  531. req_lib_cpg_local_get.header.size = sizeof (coroipc_request_header_t);
  532. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  533. iov.iov_base = (void *)&req_lib_cpg_local_get;
  534. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  535. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  536. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  537. if (error != CS_OK) {
  538. goto error_exit;
  539. }
  540. error = res_lib_cpg_local_get.header.error;
  541. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  542. error_exit:
  543. hdb_handle_put (&cpg_handle_t_db, handle);
  544. return (error);
  545. }
  546. cs_error_t cpg_flow_control_state_get (
  547. cpg_handle_t handle,
  548. cpg_flow_control_state_t *flow_control_state)
  549. {
  550. cs_error_t error;
  551. struct cpg_inst *cpg_inst;
  552. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  553. if (error != CS_OK) {
  554. return (error);
  555. }
  556. error = coroipcc_dispatch_flow_control_get (cpg_inst->handle, (unsigned int *)flow_control_state);
  557. hdb_handle_put (&cpg_handle_t_db, handle);
  558. return (error);
  559. }
  560. cs_error_t cpg_zcb_alloc (
  561. cpg_handle_t handle,
  562. size_t size,
  563. void **buffer)
  564. {
  565. cs_error_t error;
  566. struct cpg_inst *cpg_inst;
  567. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  568. if (error != CS_OK) {
  569. return (error);
  570. }
  571. error = coroipcc_zcb_alloc (cpg_inst->handle,
  572. buffer,
  573. size,
  574. sizeof (struct req_lib_cpg_mcast));
  575. hdb_handle_put (&cpg_handle_t_db, handle);
  576. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  577. return (error);
  578. }
  579. cs_error_t cpg_zcb_free (
  580. cpg_handle_t handle,
  581. void *buffer)
  582. {
  583. cs_error_t error;
  584. struct cpg_inst *cpg_inst;
  585. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  586. if (error != CS_OK) {
  587. return (error);
  588. }
  589. coroipcc_zcb_free (cpg_inst->handle, ((char *)buffer) - sizeof (struct req_lib_cpg_mcast));
  590. hdb_handle_put (&cpg_handle_t_db, handle);
  591. return (error);
  592. }
  593. cs_error_t cpg_zcb_mcast_joined (
  594. cpg_handle_t handle,
  595. cpg_guarantee_t guarantee,
  596. void *msg,
  597. size_t msg_len)
  598. {
  599. cs_error_t error;
  600. struct cpg_inst *cpg_inst;
  601. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  602. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  603. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  604. if (error != CS_OK) {
  605. return (error);
  606. }
  607. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  608. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  609. msg_len;
  610. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  611. req_lib_cpg_mcast->guarantee = guarantee;
  612. req_lib_cpg_mcast->msglen = msg_len;
  613. error = coroipcc_zcb_msg_send_reply_receive (
  614. cpg_inst->handle,
  615. req_lib_cpg_mcast,
  616. &res_lib_cpg_mcast,
  617. sizeof (res_lib_cpg_mcast));
  618. if (error != CS_OK) {
  619. goto error_exit;
  620. }
  621. error = res_lib_cpg_mcast.header.error;
  622. error_exit:
  623. hdb_handle_put (&cpg_handle_t_db, handle);
  624. return (error);
  625. }
  626. cs_error_t cpg_mcast_joined (
  627. cpg_handle_t handle,
  628. cpg_guarantee_t guarantee,
  629. const struct iovec *iovec,
  630. unsigned int iov_len)
  631. {
  632. int i;
  633. cs_error_t error;
  634. struct cpg_inst *cpg_inst;
  635. struct iovec iov[64];
  636. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  637. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  638. size_t msg_len = 0;
  639. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  640. if (error != CS_OK) {
  641. return (error);
  642. }
  643. for (i = 0; i < iov_len; i++ ) {
  644. msg_len += iovec[i].iov_len;
  645. }
  646. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  647. msg_len;
  648. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  649. req_lib_cpg_mcast.guarantee = guarantee;
  650. req_lib_cpg_mcast.msglen = msg_len;
  651. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  652. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  653. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  654. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov,
  655. iov_len + 1, &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  656. if (error != CS_OK) {
  657. goto error_exit;
  658. }
  659. error = res_lib_cpg_mcast.header.error;
  660. error_exit:
  661. hdb_handle_put (&cpg_handle_t_db, handle);
  662. return (error);
  663. }
  664. cs_error_t cpg_iteration_initialize(
  665. cpg_handle_t handle,
  666. cpg_iteration_type_t iteration_type,
  667. const struct cpg_name *group,
  668. cpg_iteration_handle_t *cpg_iteration_handle)
  669. {
  670. cs_error_t error;
  671. struct iovec iov;
  672. struct cpg_inst *cpg_inst;
  673. struct cpg_iteration_instance_t *cpg_iteration_instance;
  674. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  675. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  676. if (cpg_iteration_handle == NULL) {
  677. return (CS_ERR_INVALID_PARAM);
  678. }
  679. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  680. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  681. return (CS_ERR_INVALID_PARAM);
  682. }
  683. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  684. iteration_type != CPG_ITERATION_ALL) {
  685. return (CS_ERR_INVALID_PARAM);
  686. }
  687. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  688. if (error != CS_OK) {
  689. return (error);
  690. }
  691. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  692. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  693. if (error != CS_OK) {
  694. goto error_put_cpg_db;
  695. }
  696. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  697. (void *)&cpg_iteration_instance));
  698. if (error != CS_OK) {
  699. goto error_destroy;
  700. }
  701. cpg_iteration_instance->conn_handle = cpg_inst->handle;
  702. list_init (&cpg_iteration_instance->list);
  703. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  704. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  705. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  706. if (group) {
  707. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  708. }
  709. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  710. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  711. error = coroipcc_msg_send_reply_receive (cpg_inst->handle,
  712. &iov,
  713. 1,
  714. &res_lib_cpg_iterationinitialize,
  715. sizeof (struct res_lib_cpg_iterationinitialize));
  716. if (error != CS_OK) {
  717. goto error_put_destroy;
  718. }
  719. cpg_iteration_instance->executive_iteration_handle =
  720. res_lib_cpg_iterationinitialize.iteration_handle;
  721. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  722. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  723. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  724. hdb_handle_put (&cpg_handle_t_db, handle);
  725. return (res_lib_cpg_iterationinitialize.header.error);
  726. error_put_destroy:
  727. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  728. error_destroy:
  729. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  730. error_put_cpg_db:
  731. hdb_handle_put (&cpg_handle_t_db, handle);
  732. return (error);
  733. }
  734. cs_error_t cpg_iteration_next(
  735. cpg_iteration_handle_t handle,
  736. struct cpg_iteration_description_t *description)
  737. {
  738. cs_error_t error;
  739. struct cpg_iteration_instance_t *cpg_iteration_instance;
  740. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  741. struct res_lib_cpg_iterationnext *res_lib_cpg_iterationnext;
  742. struct iovec iov;
  743. void *return_address;
  744. if (description == NULL) {
  745. return CS_ERR_INVALID_PARAM;
  746. }
  747. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  748. (void *)&cpg_iteration_instance));
  749. if (error != CS_OK) {
  750. goto error_exit;
  751. }
  752. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  753. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  754. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  755. iov.iov_base = (void *)&req_lib_cpg_iterationnext;
  756. iov.iov_len = sizeof (struct req_lib_cpg_iterationnext);
  757. error = coroipcc_msg_send_reply_receive_in_buf_get (cpg_iteration_instance->conn_handle,
  758. &iov,
  759. 1,
  760. &return_address);
  761. res_lib_cpg_iterationnext = return_address;
  762. if (error != CS_OK) {
  763. goto error_put;
  764. }
  765. marshall_from_mar_cpg_iteration_description_t(
  766. description,
  767. &res_lib_cpg_iterationnext->description);
  768. error = res_lib_cpg_iterationnext->header.error;
  769. coroipcc_msg_send_reply_receive_in_buf_put(
  770. cpg_iteration_instance->conn_handle);
  771. error_put:
  772. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  773. error_exit:
  774. return (error);
  775. }
  776. cs_error_t cpg_iteration_finalize (
  777. cpg_iteration_handle_t handle)
  778. {
  779. cs_error_t error;
  780. struct iovec iov;
  781. struct cpg_iteration_instance_t *cpg_iteration_instance;
  782. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  783. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  784. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  785. (void *)&cpg_iteration_instance));
  786. if (error != CS_OK) {
  787. goto error_exit;
  788. }
  789. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  790. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  791. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  792. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  793. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  794. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn_handle,
  795. &iov,
  796. 1,
  797. &res_lib_cpg_iterationfinalize,
  798. sizeof (struct req_lib_cpg_iterationfinalize));
  799. if (error != CS_OK) {
  800. goto error_put;
  801. }
  802. cpg_iteration_instance_finalize (cpg_iteration_instance);
  803. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  804. return (res_lib_cpg_iterationfinalize.header.error);
  805. error_put:
  806. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  807. error_exit:
  808. return (error);
  809. }
  810. /** @} */