cpg.c 27 KB

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