cpg.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. error = coroipcc_dispatch_put (cpg_inst->handle);
  380. if (error == CS_OK) {
  381. error = CS_ERR_LIBRARY;
  382. }
  383. goto error_put;
  384. break;
  385. } /* - switch (dispatch_data->id) */
  386. break; /* case CPG_MODEL_V1 */
  387. } /* - switch (cpg_inst_copy.model_data.model) */
  388. error = coroipcc_dispatch_put (cpg_inst->handle);
  389. if (error != CS_OK) {
  390. goto error_put;
  391. }
  392. /*
  393. * Determine if more messages should be processed
  394. */
  395. if (dispatch_types == CS_DISPATCH_ONE) {
  396. cont = 0;
  397. }
  398. } while (cont);
  399. error_put:
  400. hdb_handle_put (&cpg_handle_t_db, handle);
  401. return (error);
  402. }
  403. cs_error_t cpg_join (
  404. cpg_handle_t handle,
  405. const struct cpg_name *group)
  406. {
  407. cs_error_t error;
  408. struct cpg_inst *cpg_inst;
  409. struct iovec iov[2];
  410. struct req_lib_cpg_join req_lib_cpg_join;
  411. struct res_lib_cpg_join res_lib_cpg_join;
  412. if (group->length > CPG_MAX_NAME_LENGTH) {
  413. return (CS_ERR_NAME_TOO_LONG);
  414. }
  415. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  416. if (error != CS_OK) {
  417. return (error);
  418. }
  419. /* Now join */
  420. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  421. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  422. req_lib_cpg_join.pid = getpid();
  423. req_lib_cpg_join.flags = 0;
  424. switch (cpg_inst->model_data.model) {
  425. case CPG_MODEL_V1:
  426. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  427. break;
  428. }
  429. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  430. group);
  431. iov[0].iov_base = (void *)&req_lib_cpg_join;
  432. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  433. do {
  434. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov, 1,
  435. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  436. if (error != CS_OK) {
  437. goto error_exit;
  438. }
  439. } while (res_lib_cpg_join.header.error == CPG_ERR_BUSY);
  440. error = res_lib_cpg_join.header.error;
  441. error_exit:
  442. hdb_handle_put (&cpg_handle_t_db, handle);
  443. return (error);
  444. }
  445. cs_error_t cpg_leave (
  446. cpg_handle_t handle,
  447. const struct cpg_name *group)
  448. {
  449. cs_error_t error;
  450. struct cpg_inst *cpg_inst;
  451. struct iovec iov[2];
  452. struct req_lib_cpg_leave req_lib_cpg_leave;
  453. struct res_lib_cpg_leave res_lib_cpg_leave;
  454. if (group->length > CPG_MAX_NAME_LENGTH) {
  455. return (CS_ERR_NAME_TOO_LONG);
  456. }
  457. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  458. if (error != CS_OK) {
  459. return (error);
  460. }
  461. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  462. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  463. req_lib_cpg_leave.pid = getpid();
  464. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  465. group);
  466. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  467. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  468. do {
  469. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov, 1,
  470. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  471. if (error != CS_OK) {
  472. goto error_exit;
  473. }
  474. } while (res_lib_cpg_leave.header.error == CPG_ERR_BUSY);
  475. error = res_lib_cpg_leave.header.error;
  476. error_exit:
  477. hdb_handle_put (&cpg_handle_t_db, handle);
  478. return (error);
  479. }
  480. cs_error_t cpg_membership_get (
  481. cpg_handle_t handle,
  482. struct cpg_name *group_name,
  483. struct cpg_address *member_list,
  484. int *member_list_entries)
  485. {
  486. cs_error_t error;
  487. struct cpg_inst *cpg_inst;
  488. struct iovec iov;
  489. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  490. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  491. unsigned int i;
  492. if (group_name->length > CPG_MAX_NAME_LENGTH) {
  493. return (CS_ERR_NAME_TOO_LONG);
  494. }
  495. if (member_list == NULL) {
  496. return (CS_ERR_INVALID_PARAM);
  497. }
  498. if (member_list_entries == NULL) {
  499. return (CS_ERR_INVALID_PARAM);
  500. }
  501. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  502. if (error != CS_OK) {
  503. return (error);
  504. }
  505. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  506. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  507. memcpy (&req_lib_cpg_membership_get.group_name, group_name,
  508. sizeof (struct cpg_name));
  509. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  510. iov.iov_len = sizeof (coroipc_request_header_t);
  511. do {
  512. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  513. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  514. if (error != CS_OK) {
  515. goto error_exit;
  516. }
  517. } while (res_lib_cpg_membership_get.header.error == CPG_ERR_BUSY);
  518. error = res_lib_cpg_membership_get.header.error;
  519. /*
  520. * Copy results to caller
  521. */
  522. *member_list_entries = res_lib_cpg_membership_get.member_count;
  523. if (member_list) {
  524. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  525. marshall_from_mar_cpg_address_t (&member_list[i],
  526. &res_lib_cpg_membership_get.member_list[i]);
  527. }
  528. }
  529. error_exit:
  530. hdb_handle_put (&cpg_handle_t_db, handle);
  531. return (error);
  532. }
  533. cs_error_t cpg_local_get (
  534. cpg_handle_t handle,
  535. unsigned int *local_nodeid)
  536. {
  537. cs_error_t error;
  538. struct cpg_inst *cpg_inst;
  539. struct iovec iov;
  540. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  541. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  542. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  543. if (error != CS_OK) {
  544. return (error);
  545. }
  546. req_lib_cpg_local_get.header.size = sizeof (coroipc_request_header_t);
  547. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  548. iov.iov_base = (void *)&req_lib_cpg_local_get;
  549. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  550. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, &iov, 1,
  551. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  552. if (error != CS_OK) {
  553. goto error_exit;
  554. }
  555. error = res_lib_cpg_local_get.header.error;
  556. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  557. error_exit:
  558. hdb_handle_put (&cpg_handle_t_db, handle);
  559. return (error);
  560. }
  561. cs_error_t cpg_flow_control_state_get (
  562. cpg_handle_t handle,
  563. cpg_flow_control_state_t *flow_control_state)
  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_dispatch_flow_control_get (cpg_inst->handle, (unsigned int *)flow_control_state);
  572. hdb_handle_put (&cpg_handle_t_db, handle);
  573. return (error);
  574. }
  575. cs_error_t cpg_zcb_alloc (
  576. cpg_handle_t handle,
  577. size_t size,
  578. void **buffer)
  579. {
  580. cs_error_t error;
  581. struct cpg_inst *cpg_inst;
  582. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  583. if (error != CS_OK) {
  584. return (error);
  585. }
  586. error = coroipcc_zcb_alloc (cpg_inst->handle,
  587. buffer,
  588. size,
  589. sizeof (struct req_lib_cpg_mcast));
  590. hdb_handle_put (&cpg_handle_t_db, handle);
  591. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  592. return (error);
  593. }
  594. cs_error_t cpg_zcb_free (
  595. cpg_handle_t handle,
  596. void *buffer)
  597. {
  598. cs_error_t error;
  599. struct cpg_inst *cpg_inst;
  600. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  601. if (error != CS_OK) {
  602. return (error);
  603. }
  604. coroipcc_zcb_free (cpg_inst->handle, ((char *)buffer) - sizeof (struct req_lib_cpg_mcast));
  605. hdb_handle_put (&cpg_handle_t_db, handle);
  606. return (error);
  607. }
  608. cs_error_t cpg_zcb_mcast_joined (
  609. cpg_handle_t handle,
  610. cpg_guarantee_t guarantee,
  611. void *msg,
  612. size_t msg_len)
  613. {
  614. cs_error_t error;
  615. struct cpg_inst *cpg_inst;
  616. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  617. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  618. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  619. if (error != CS_OK) {
  620. return (error);
  621. }
  622. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  623. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  624. msg_len;
  625. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  626. req_lib_cpg_mcast->guarantee = guarantee;
  627. req_lib_cpg_mcast->msglen = msg_len;
  628. error = coroipcc_zcb_msg_send_reply_receive (
  629. cpg_inst->handle,
  630. req_lib_cpg_mcast,
  631. &res_lib_cpg_mcast,
  632. sizeof (res_lib_cpg_mcast));
  633. if (error != CS_OK) {
  634. goto error_exit;
  635. }
  636. error = res_lib_cpg_mcast.header.error;
  637. error_exit:
  638. hdb_handle_put (&cpg_handle_t_db, handle);
  639. return (error);
  640. }
  641. cs_error_t cpg_mcast_joined (
  642. cpg_handle_t handle,
  643. cpg_guarantee_t guarantee,
  644. const struct iovec *iovec,
  645. unsigned int iov_len)
  646. {
  647. int i;
  648. cs_error_t error;
  649. struct cpg_inst *cpg_inst;
  650. struct iovec iov[64];
  651. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  652. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  653. size_t msg_len = 0;
  654. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  655. if (error != CS_OK) {
  656. return (error);
  657. }
  658. for (i = 0; i < iov_len; i++ ) {
  659. msg_len += iovec[i].iov_len;
  660. }
  661. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  662. msg_len;
  663. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  664. req_lib_cpg_mcast.guarantee = guarantee;
  665. req_lib_cpg_mcast.msglen = msg_len;
  666. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  667. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  668. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  669. error = coroipcc_msg_send_reply_receive (cpg_inst->handle, iov,
  670. iov_len + 1, &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  671. if (error != CS_OK) {
  672. goto error_exit;
  673. }
  674. error = res_lib_cpg_mcast.header.error;
  675. error_exit:
  676. hdb_handle_put (&cpg_handle_t_db, handle);
  677. return (error);
  678. }
  679. cs_error_t cpg_iteration_initialize(
  680. cpg_handle_t handle,
  681. cpg_iteration_type_t iteration_type,
  682. const struct cpg_name *group,
  683. cpg_iteration_handle_t *cpg_iteration_handle)
  684. {
  685. cs_error_t error;
  686. struct iovec iov;
  687. struct cpg_inst *cpg_inst;
  688. struct cpg_iteration_instance_t *cpg_iteration_instance;
  689. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  690. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  691. if (group && group->length > CPG_MAX_NAME_LENGTH) {
  692. return (CS_ERR_NAME_TOO_LONG);
  693. }
  694. if (cpg_iteration_handle == NULL) {
  695. return (CS_ERR_INVALID_PARAM);
  696. }
  697. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  698. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  699. return (CS_ERR_INVALID_PARAM);
  700. }
  701. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  702. iteration_type != CPG_ITERATION_ALL) {
  703. return (CS_ERR_INVALID_PARAM);
  704. }
  705. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  706. if (error != CS_OK) {
  707. return (error);
  708. }
  709. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  710. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  711. if (error != CS_OK) {
  712. goto error_put_cpg_db;
  713. }
  714. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  715. (void *)&cpg_iteration_instance));
  716. if (error != CS_OK) {
  717. goto error_destroy;
  718. }
  719. cpg_iteration_instance->conn_handle = cpg_inst->handle;
  720. list_init (&cpg_iteration_instance->list);
  721. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  722. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  723. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  724. if (group) {
  725. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  726. }
  727. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  728. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  729. error = coroipcc_msg_send_reply_receive (cpg_inst->handle,
  730. &iov,
  731. 1,
  732. &res_lib_cpg_iterationinitialize,
  733. sizeof (struct res_lib_cpg_iterationinitialize));
  734. if (error != CS_OK) {
  735. goto error_put_destroy;
  736. }
  737. cpg_iteration_instance->executive_iteration_handle =
  738. res_lib_cpg_iterationinitialize.iteration_handle;
  739. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  740. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  741. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  742. hdb_handle_put (&cpg_handle_t_db, handle);
  743. return (res_lib_cpg_iterationinitialize.header.error);
  744. error_put_destroy:
  745. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  746. error_destroy:
  747. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  748. error_put_cpg_db:
  749. hdb_handle_put (&cpg_handle_t_db, handle);
  750. return (error);
  751. }
  752. cs_error_t cpg_iteration_next(
  753. cpg_iteration_handle_t handle,
  754. struct cpg_iteration_description_t *description)
  755. {
  756. cs_error_t error;
  757. struct cpg_iteration_instance_t *cpg_iteration_instance;
  758. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  759. struct res_lib_cpg_iterationnext *res_lib_cpg_iterationnext;
  760. struct iovec iov;
  761. void *return_address;
  762. if (description == NULL) {
  763. return CS_ERR_INVALID_PARAM;
  764. }
  765. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  766. (void *)&cpg_iteration_instance));
  767. if (error != CS_OK) {
  768. goto error_exit;
  769. }
  770. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  771. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  772. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  773. iov.iov_base = (void *)&req_lib_cpg_iterationnext;
  774. iov.iov_len = sizeof (struct req_lib_cpg_iterationnext);
  775. error = coroipcc_msg_send_reply_receive_in_buf_get (cpg_iteration_instance->conn_handle,
  776. &iov,
  777. 1,
  778. &return_address);
  779. res_lib_cpg_iterationnext = return_address;
  780. if (error != CS_OK) {
  781. goto error_put;
  782. }
  783. marshall_from_mar_cpg_iteration_description_t(
  784. description,
  785. &res_lib_cpg_iterationnext->description);
  786. error = res_lib_cpg_iterationnext->header.error;
  787. coroipcc_msg_send_reply_receive_in_buf_put(
  788. cpg_iteration_instance->conn_handle);
  789. error_put:
  790. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  791. error_exit:
  792. return (error);
  793. }
  794. cs_error_t cpg_iteration_finalize (
  795. cpg_iteration_handle_t handle)
  796. {
  797. cs_error_t error;
  798. struct iovec iov;
  799. struct cpg_iteration_instance_t *cpg_iteration_instance;
  800. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  801. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  802. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  803. (void *)&cpg_iteration_instance));
  804. if (error != CS_OK) {
  805. goto error_exit;
  806. }
  807. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  808. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  809. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  810. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  811. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  812. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn_handle,
  813. &iov,
  814. 1,
  815. &res_lib_cpg_iterationfinalize,
  816. sizeof (struct req_lib_cpg_iterationfinalize));
  817. if (error != CS_OK) {
  818. goto error_put;
  819. }
  820. cpg_iteration_instance_finalize (cpg_iteration_instance);
  821. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  822. return (res_lib_cpg_iterationfinalize.header.error);
  823. error_put:
  824. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  825. error_exit:
  826. return (error);
  827. }
  828. /** @} */