cpg.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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 res_lib_cpg_finalize));
  203. coroipcc_service_disconnect (cpg_inst->handle);
  204. cpg_inst_finalize (cpg_inst, handle);
  205. hdb_handle_put (&cpg_handle_t_db, handle);
  206. return (error);
  207. }
  208. cs_error_t cpg_fd_get (
  209. cpg_handle_t handle,
  210. int *fd)
  211. {
  212. cs_error_t error;
  213. struct cpg_inst *cpg_inst;
  214. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  215. if (error != CS_OK) {
  216. return (error);
  217. }
  218. error = coroipcc_fd_get (cpg_inst->handle, fd);
  219. hdb_handle_put (&cpg_handle_t_db, handle);
  220. return (error);
  221. }
  222. cs_error_t cpg_context_get (
  223. cpg_handle_t handle,
  224. void **context)
  225. {
  226. cs_error_t error;
  227. struct cpg_inst *cpg_inst;
  228. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  229. if (error != CS_OK) {
  230. return (error);
  231. }
  232. *context = cpg_inst->context;
  233. hdb_handle_put (&cpg_handle_t_db, handle);
  234. return (CS_OK);
  235. }
  236. cs_error_t cpg_context_set (
  237. cpg_handle_t handle,
  238. void *context)
  239. {
  240. cs_error_t error;
  241. struct cpg_inst *cpg_inst;
  242. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  243. if (error != CS_OK) {
  244. return (error);
  245. }
  246. cpg_inst->context = context;
  247. hdb_handle_put (&cpg_handle_t_db, handle);
  248. return (CS_OK);
  249. }
  250. cs_error_t cpg_dispatch (
  251. cpg_handle_t handle,
  252. cs_dispatch_flags_t dispatch_types)
  253. {
  254. int timeout = -1;
  255. cs_error_t error;
  256. int cont = 1; /* always continue do loop except when set to 0 */
  257. struct cpg_inst *cpg_inst;
  258. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  259. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  260. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  261. struct cpg_inst cpg_inst_copy;
  262. coroipc_response_header_t *dispatch_data;
  263. struct cpg_address member_list[CPG_MEMBERS_MAX];
  264. struct cpg_address left_list[CPG_MEMBERS_MAX];
  265. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  266. struct cpg_name group_name;
  267. mar_cpg_address_t *left_list_start;
  268. mar_cpg_address_t *joined_list_start;
  269. unsigned int i;
  270. struct cpg_ring_id ring_id;
  271. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  272. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  273. if (error != CS_OK) {
  274. return (error);
  275. }
  276. /*
  277. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  278. * wait indefinately for CS_DISPATCH_BLOCKING
  279. */
  280. if (dispatch_types == CPG_DISPATCH_ALL) {
  281. timeout = 0;
  282. }
  283. do {
  284. error = coroipcc_dispatch_get (
  285. cpg_inst->handle,
  286. (void **)&dispatch_data,
  287. timeout);
  288. if (error == CS_ERR_BAD_HANDLE) {
  289. error = CS_OK;
  290. goto error_put;
  291. }
  292. if (error == CS_ERR_TRY_AGAIN) {
  293. error = CS_OK;
  294. if (dispatch_types == CPG_DISPATCH_ALL) {
  295. break; /* exit do while cont is 1 loop */
  296. } else {
  297. continue; /* next poll */
  298. }
  299. }
  300. if (error != CS_OK) {
  301. goto error_put;
  302. }
  303. /*
  304. * Make copy of callbacks, message data, unlock instance, and call callback
  305. * A risk of this dispatch method is that the callback routines may
  306. * operate at the same time that cpgFinalize has been called.
  307. */
  308. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  309. switch (cpg_inst_copy.model_data.model) {
  310. case CPG_MODEL_V1:
  311. /*
  312. * Dispatch incoming message
  313. */
  314. switch (dispatch_data->id) {
  315. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  316. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  317. break;
  318. }
  319. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  320. marshall_from_mar_cpg_name_t (
  321. &group_name,
  322. &res_cpg_deliver_callback->group_name);
  323. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  324. &group_name,
  325. res_cpg_deliver_callback->nodeid,
  326. res_cpg_deliver_callback->pid,
  327. &res_cpg_deliver_callback->message,
  328. res_cpg_deliver_callback->msglen);
  329. break;
  330. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  331. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  332. break;
  333. }
  334. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  335. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  336. marshall_from_mar_cpg_address_t (&member_list[i],
  337. &res_cpg_confchg_callback->member_list[i]);
  338. }
  339. left_list_start = res_cpg_confchg_callback->member_list +
  340. res_cpg_confchg_callback->member_list_entries;
  341. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  342. marshall_from_mar_cpg_address_t (&left_list[i],
  343. &left_list_start[i]);
  344. }
  345. joined_list_start = res_cpg_confchg_callback->member_list +
  346. res_cpg_confchg_callback->member_list_entries +
  347. res_cpg_confchg_callback->left_list_entries;
  348. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  349. marshall_from_mar_cpg_address_t (&joined_list[i],
  350. &joined_list_start[i]);
  351. }
  352. marshall_from_mar_cpg_name_t (
  353. &group_name,
  354. &res_cpg_confchg_callback->group_name);
  355. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  356. &group_name,
  357. member_list,
  358. res_cpg_confchg_callback->member_list_entries,
  359. left_list,
  360. res_cpg_confchg_callback->left_list_entries,
  361. joined_list,
  362. res_cpg_confchg_callback->joined_list_entries);
  363. break;
  364. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  365. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  366. break;
  367. }
  368. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  369. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  370. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  371. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  372. }
  373. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  374. ring_id,
  375. res_cpg_totem_confchg_callback->member_list_entries,
  376. totem_member_list);
  377. break;
  378. default:
  379. coroipcc_dispatch_put (cpg_inst->handle);
  380. error = CS_ERR_LIBRARY;
  381. goto error_put;
  382. break;
  383. } /* - switch (dispatch_data->id) */
  384. break; /* case CPG_MODEL_V1 */
  385. } /* - switch (cpg_inst_copy.model_data.model) */
  386. coroipcc_dispatch_put (cpg_inst->handle);
  387. /*
  388. * Determine if more messages should be processed
  389. */
  390. if (dispatch_types == CS_DISPATCH_ONE) {
  391. cont = 0;
  392. }
  393. } while (cont);
  394. error_put:
  395. hdb_handle_put (&cpg_handle_t_db, handle);
  396. return (error);
  397. }
  398. cs_error_t cpg_join (
  399. cpg_handle_t handle,
  400. const struct cpg_name *group)
  401. {
  402. cs_error_t error;
  403. struct cpg_inst *cpg_inst;
  404. struct iovec iov[2];
  405. struct req_lib_cpg_join req_lib_cpg_join;
  406. struct res_lib_cpg_join res_lib_cpg_join;
  407. if (group->length > CPG_MAX_NAME_LENGTH) {
  408. return (CS_ERR_NAME_TOO_LONG);
  409. }
  410. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  411. if (error != CS_OK) {
  412. return (error);
  413. }
  414. /* Now join */
  415. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  416. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  417. req_lib_cpg_join.pid = getpid();
  418. req_lib_cpg_join.flags = 0;
  419. switch (cpg_inst->model_data.model) {
  420. case CPG_MODEL_V1:
  421. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  422. break;
  423. }
  424. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  425. group);
  426. iov[0].iov_base = (void *)&req_lib_cpg_join;
  427. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  428. do {
  429. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov, 1,
  430. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  431. if (error != CS_OK) {
  432. goto error_exit;
  433. }
  434. } while (res_lib_cpg_join.header.error == CPG_ERR_BUSY);
  435. error = res_lib_cpg_join.header.error;
  436. error_exit:
  437. hdb_handle_put (&cpg_handle_t_db, handle);
  438. return (error);
  439. }
  440. cs_error_t cpg_leave (
  441. cpg_handle_t handle,
  442. const struct cpg_name *group)
  443. {
  444. cs_error_t error;
  445. struct cpg_inst *cpg_inst;
  446. struct iovec iov[2];
  447. struct req_lib_cpg_leave req_lib_cpg_leave;
  448. struct res_lib_cpg_leave res_lib_cpg_leave;
  449. if (group->length > CPG_MAX_NAME_LENGTH) {
  450. return (CS_ERR_NAME_TOO_LONG);
  451. }
  452. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  453. if (error != CS_OK) {
  454. return (error);
  455. }
  456. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  457. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  458. req_lib_cpg_leave.pid = getpid();
  459. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  460. group);
  461. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  462. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  463. do {
  464. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov, 1,
  465. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  466. if (error != CS_OK) {
  467. goto error_exit;
  468. }
  469. } while (res_lib_cpg_leave.header.error == CPG_ERR_BUSY);
  470. error = res_lib_cpg_leave.header.error;
  471. error_exit:
  472. hdb_handle_put (&cpg_handle_t_db, handle);
  473. return (error);
  474. }
  475. cs_error_t cpg_membership_get (
  476. cpg_handle_t handle,
  477. struct cpg_name *group_name,
  478. struct cpg_address *member_list,
  479. int *member_list_entries)
  480. {
  481. cs_error_t error;
  482. struct cpg_inst *cpg_inst;
  483. struct iovec iov;
  484. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  485. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  486. unsigned int i;
  487. if (group_name->length > CPG_MAX_NAME_LENGTH) {
  488. return (CS_ERR_NAME_TOO_LONG);
  489. }
  490. if (member_list == NULL) {
  491. return (CS_ERR_INVALID_PARAM);
  492. }
  493. if (member_list_entries == NULL) {
  494. return (CS_ERR_INVALID_PARAM);
  495. }
  496. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  497. if (error != CS_OK) {
  498. return (error);
  499. }
  500. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  501. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  502. memcpy (&req_lib_cpg_membership_get.group_name, group_name,
  503. sizeof (struct cpg_name));
  504. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  505. iov.iov_len = sizeof (coroipc_request_header_t);
  506. do {
  507. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  508. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  509. if (error != CS_OK) {
  510. goto error_exit;
  511. }
  512. } while (res_lib_cpg_membership_get.header.error == CPG_ERR_BUSY);
  513. error = res_lib_cpg_membership_get.header.error;
  514. /*
  515. * Copy results to caller
  516. */
  517. *member_list_entries = res_lib_cpg_membership_get.member_count;
  518. if (member_list) {
  519. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  520. marshall_from_mar_cpg_address_t (&member_list[i],
  521. &res_lib_cpg_membership_get.member_list[i]);
  522. }
  523. }
  524. error_exit:
  525. hdb_handle_put (&cpg_handle_t_db, handle);
  526. return (error);
  527. }
  528. cs_error_t cpg_local_get (
  529. cpg_handle_t handle,
  530. unsigned int *local_nodeid)
  531. {
  532. cs_error_t error;
  533. struct cpg_inst *cpg_inst;
  534. struct iovec iov;
  535. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  536. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  537. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  538. if (error != CS_OK) {
  539. return (error);
  540. }
  541. req_lib_cpg_local_get.header.size = sizeof (coroipc_request_header_t);
  542. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  543. iov.iov_base = (void *)&req_lib_cpg_local_get;
  544. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  545. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  546. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  547. if (error != CS_OK) {
  548. goto error_exit;
  549. }
  550. error = res_lib_cpg_local_get.header.error;
  551. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  552. error_exit:
  553. hdb_handle_put (&cpg_handle_t_db, handle);
  554. return (error);
  555. }
  556. cs_error_t cpg_flow_control_state_get (
  557. cpg_handle_t handle,
  558. cpg_flow_control_state_t *flow_control_state)
  559. {
  560. cs_error_t error;
  561. struct cpg_inst *cpg_inst;
  562. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  563. if (error != CS_OK) {
  564. return (error);
  565. }
  566. error = coroipcc_dispatch_flow_control_get (cpg_inst->handle, (unsigned int *)flow_control_state);
  567. hdb_handle_put (&cpg_handle_t_db, handle);
  568. return (error);
  569. }
  570. cs_error_t cpg_zcb_alloc (
  571. cpg_handle_t handle,
  572. size_t size,
  573. void **buffer)
  574. {
  575. cs_error_t error;
  576. struct cpg_inst *cpg_inst;
  577. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  578. if (error != CS_OK) {
  579. return (error);
  580. }
  581. error = coroipcc_zcb_alloc (cpg_inst->handle,
  582. buffer,
  583. size,
  584. sizeof (struct req_lib_cpg_mcast));
  585. hdb_handle_put (&cpg_handle_t_db, handle);
  586. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  587. return (error);
  588. }
  589. cs_error_t cpg_zcb_free (
  590. cpg_handle_t handle,
  591. void *buffer)
  592. {
  593. cs_error_t error;
  594. struct cpg_inst *cpg_inst;
  595. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  596. if (error != CS_OK) {
  597. return (error);
  598. }
  599. coroipcc_zcb_free (cpg_inst->handle, ((char *)buffer) - sizeof (struct req_lib_cpg_mcast));
  600. hdb_handle_put (&cpg_handle_t_db, handle);
  601. return (error);
  602. }
  603. cs_error_t cpg_zcb_mcast_joined (
  604. cpg_handle_t handle,
  605. cpg_guarantee_t guarantee,
  606. void *msg,
  607. size_t msg_len)
  608. {
  609. cs_error_t error;
  610. struct cpg_inst *cpg_inst;
  611. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  612. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  613. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  614. if (error != CS_OK) {
  615. return (error);
  616. }
  617. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  618. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  619. msg_len;
  620. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  621. req_lib_cpg_mcast->guarantee = guarantee;
  622. req_lib_cpg_mcast->msglen = msg_len;
  623. error = coroipcc_zcb_msg_send_reply_receive (
  624. cpg_inst->handle,
  625. req_lib_cpg_mcast,
  626. &res_lib_cpg_mcast,
  627. sizeof (res_lib_cpg_mcast));
  628. if (error != CS_OK) {
  629. goto error_exit;
  630. }
  631. error = res_lib_cpg_mcast.header.error;
  632. error_exit:
  633. hdb_handle_put (&cpg_handle_t_db, handle);
  634. return (error);
  635. }
  636. cs_error_t cpg_mcast_joined (
  637. cpg_handle_t handle,
  638. cpg_guarantee_t guarantee,
  639. const struct iovec *iovec,
  640. unsigned int iov_len)
  641. {
  642. int i;
  643. cs_error_t error;
  644. struct cpg_inst *cpg_inst;
  645. struct iovec iov[64];
  646. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  647. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  648. size_t msg_len = 0;
  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. for (i = 0; i < iov_len; i++ ) {
  654. msg_len += iovec[i].iov_len;
  655. }
  656. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  657. msg_len;
  658. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  659. req_lib_cpg_mcast.guarantee = guarantee;
  660. req_lib_cpg_mcast.msglen = msg_len;
  661. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  662. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  663. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  664. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov,
  665. iov_len + 1, &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  666. if (error != CS_OK) {
  667. goto error_exit;
  668. }
  669. error = res_lib_cpg_mcast.header.error;
  670. error_exit:
  671. hdb_handle_put (&cpg_handle_t_db, handle);
  672. return (error);
  673. }
  674. cs_error_t cpg_iteration_initialize(
  675. cpg_handle_t handle,
  676. cpg_iteration_type_t iteration_type,
  677. const struct cpg_name *group,
  678. cpg_iteration_handle_t *cpg_iteration_handle)
  679. {
  680. cs_error_t error;
  681. struct iovec iov;
  682. struct cpg_inst *cpg_inst;
  683. struct cpg_iteration_instance_t *cpg_iteration_instance;
  684. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  685. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  686. if (group && group->length > CPG_MAX_NAME_LENGTH) {
  687. return (CS_ERR_NAME_TOO_LONG);
  688. }
  689. if (cpg_iteration_handle == NULL) {
  690. return (CS_ERR_INVALID_PARAM);
  691. }
  692. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  693. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  694. return (CS_ERR_INVALID_PARAM);
  695. }
  696. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  697. iteration_type != CPG_ITERATION_ALL) {
  698. return (CS_ERR_INVALID_PARAM);
  699. }
  700. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  701. if (error != CS_OK) {
  702. return (error);
  703. }
  704. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  705. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  706. if (error != CS_OK) {
  707. goto error_put_cpg_db;
  708. }
  709. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  710. (void *)&cpg_iteration_instance));
  711. if (error != CS_OK) {
  712. goto error_destroy;
  713. }
  714. cpg_iteration_instance->conn_handle = cpg_inst->handle;
  715. list_init (&cpg_iteration_instance->list);
  716. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  717. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  718. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  719. if (group) {
  720. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  721. }
  722. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  723. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  724. error = coroipcc_msg_send_reply_receive (cpg_inst->handle,
  725. &iov,
  726. 1,
  727. &res_lib_cpg_iterationinitialize,
  728. sizeof (struct res_lib_cpg_iterationinitialize));
  729. if (error != CS_OK) {
  730. goto error_put_destroy;
  731. }
  732. cpg_iteration_instance->executive_iteration_handle =
  733. res_lib_cpg_iterationinitialize.iteration_handle;
  734. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  735. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  736. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  737. hdb_handle_put (&cpg_handle_t_db, handle);
  738. return (res_lib_cpg_iterationinitialize.header.error);
  739. error_put_destroy:
  740. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  741. error_destroy:
  742. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  743. error_put_cpg_db:
  744. hdb_handle_put (&cpg_handle_t_db, handle);
  745. return (error);
  746. }
  747. cs_error_t cpg_iteration_next(
  748. cpg_iteration_handle_t handle,
  749. struct cpg_iteration_description_t *description)
  750. {
  751. cs_error_t error;
  752. struct cpg_iteration_instance_t *cpg_iteration_instance;
  753. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  754. struct res_lib_cpg_iterationnext *res_lib_cpg_iterationnext;
  755. struct iovec iov;
  756. void *return_address;
  757. if (description == NULL) {
  758. return CS_ERR_INVALID_PARAM;
  759. }
  760. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  761. (void *)&cpg_iteration_instance));
  762. if (error != CS_OK) {
  763. goto error_exit;
  764. }
  765. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  766. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  767. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  768. iov.iov_base = (void *)&req_lib_cpg_iterationnext;
  769. iov.iov_len = sizeof (struct req_lib_cpg_iterationnext);
  770. error = coroipcc_msg_send_reply_receive_in_buf_get (cpg_iteration_instance->conn_handle,
  771. &iov,
  772. 1,
  773. &return_address);
  774. res_lib_cpg_iterationnext = return_address;
  775. if (error != CS_OK) {
  776. goto error_put;
  777. }
  778. marshall_from_mar_cpg_iteration_description_t(
  779. description,
  780. &res_lib_cpg_iterationnext->description);
  781. error = res_lib_cpg_iterationnext->header.error;
  782. coroipcc_msg_send_reply_receive_in_buf_put(
  783. cpg_iteration_instance->conn_handle);
  784. error_put:
  785. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  786. error_exit:
  787. return (error);
  788. }
  789. cs_error_t cpg_iteration_finalize (
  790. cpg_iteration_handle_t handle)
  791. {
  792. cs_error_t error;
  793. struct iovec iov;
  794. struct cpg_iteration_instance_t *cpg_iteration_instance;
  795. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  796. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  797. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  798. (void *)&cpg_iteration_instance));
  799. if (error != CS_OK) {
  800. goto error_exit;
  801. }
  802. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  803. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  804. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  805. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  806. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  807. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn_handle,
  808. &iov,
  809. 1,
  810. &res_lib_cpg_iterationfinalize,
  811. sizeof (struct req_lib_cpg_iterationfinalize));
  812. if (error != CS_OK) {
  813. goto error_put;
  814. }
  815. cpg_iteration_instance_finalize (cpg_iteration_instance);
  816. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  817. return (res_lib_cpg_iterationfinalize.header.error);
  818. error_put:
  819. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  820. error_exit:
  821. return (error);
  822. }
  823. /** @} */