cpg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2008 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Christine Caulfield (ccaulfie@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * Provides a closed process group API using the corosync executive
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <pthread.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46. #include <corosync/saAis.h>
  47. #include <corosync/cpg.h>
  48. #include <corosync/ipc_cpg.h>
  49. #include <corosync/mar_cpg.h>
  50. #include <corosync/ais_util.h>
  51. struct cpg_inst {
  52. int response_fd;
  53. int dispatch_fd;
  54. int finalize;
  55. cpg_flow_control_state_t flow_control_state;
  56. cpg_callbacks_t callbacks;
  57. void *context;
  58. pthread_mutex_t response_mutex;
  59. pthread_mutex_t dispatch_mutex;
  60. };
  61. static void cpg_instance_destructor (void *instance);
  62. static struct saHandleDatabase cpg_handle_t_db = {
  63. .handleCount = 0,
  64. .handles = 0,
  65. .mutex = PTHREAD_MUTEX_INITIALIZER,
  66. .handleInstanceDestructor = cpg_instance_destructor
  67. };
  68. /*
  69. * Clean up function for a cpg instance (cpg_initialize) handle
  70. */
  71. static void cpg_instance_destructor (void *instance)
  72. {
  73. struct cpg_inst *cpg_inst = instance;
  74. pthread_mutex_destroy (&cpg_inst->response_mutex);
  75. pthread_mutex_destroy (&cpg_inst->dispatch_mutex);
  76. }
  77. /**
  78. * @defgroup cpg_corosync The closed process group API
  79. * @ingroup corosync
  80. *
  81. * @{
  82. */
  83. cpg_error_t cpg_initialize (
  84. cpg_handle_t *handle,
  85. cpg_callbacks_t *callbacks)
  86. {
  87. SaAisErrorT error;
  88. struct cpg_inst *cpg_inst;
  89. error = saHandleCreate (&cpg_handle_t_db, sizeof (struct cpg_inst), handle);
  90. if (error != SA_AIS_OK) {
  91. goto error_no_destroy;
  92. }
  93. error = saHandleInstanceGet (&cpg_handle_t_db, *handle, (void *)&cpg_inst);
  94. if (error != SA_AIS_OK) {
  95. goto error_destroy;
  96. }
  97. error = saServiceConnect (&cpg_inst->dispatch_fd,
  98. &cpg_inst->response_fd,
  99. CPG_SERVICE);
  100. if (error != SA_AIS_OK) {
  101. goto error_put_destroy;
  102. }
  103. memcpy (&cpg_inst->callbacks, callbacks, sizeof (cpg_callbacks_t));
  104. pthread_mutex_init (&cpg_inst->response_mutex, NULL);
  105. pthread_mutex_init (&cpg_inst->dispatch_mutex, NULL);
  106. saHandleInstancePut (&cpg_handle_t_db, *handle);
  107. return (SA_AIS_OK);
  108. error_put_destroy:
  109. saHandleInstancePut (&cpg_handle_t_db, *handle);
  110. error_destroy:
  111. saHandleDestroy (&cpg_handle_t_db, *handle);
  112. error_no_destroy:
  113. return (error);
  114. }
  115. cpg_error_t cpg_finalize (
  116. cpg_handle_t handle)
  117. {
  118. struct cpg_inst *cpg_inst;
  119. SaAisErrorT error;
  120. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  121. if (error != SA_AIS_OK) {
  122. return (error);
  123. }
  124. pthread_mutex_lock (&cpg_inst->response_mutex);
  125. /*
  126. * Another thread has already started finalizing
  127. */
  128. if (cpg_inst->finalize) {
  129. pthread_mutex_unlock (&cpg_inst->response_mutex);
  130. saHandleInstancePut (&cpg_handle_t_db, handle);
  131. return (CPG_ERR_BAD_HANDLE);
  132. }
  133. cpg_inst->finalize = 1;
  134. pthread_mutex_unlock (&cpg_inst->response_mutex);
  135. saHandleDestroy (&cpg_handle_t_db, handle);
  136. /*
  137. * Disconnect from the server
  138. */
  139. if (cpg_inst->response_fd != -1) {
  140. shutdown(cpg_inst->response_fd, 0);
  141. close(cpg_inst->response_fd);
  142. }
  143. if (cpg_inst->dispatch_fd != -1) {
  144. shutdown(cpg_inst->dispatch_fd, 0);
  145. close(cpg_inst->dispatch_fd);
  146. }
  147. saHandleInstancePut (&cpg_handle_t_db, handle);
  148. return (CPG_OK);
  149. }
  150. cpg_error_t cpg_fd_get (
  151. cpg_handle_t handle,
  152. int *fd)
  153. {
  154. SaAisErrorT error;
  155. struct cpg_inst *cpg_inst;
  156. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  157. if (error != SA_AIS_OK) {
  158. return (error);
  159. }
  160. *fd = cpg_inst->dispatch_fd;
  161. saHandleInstancePut (&cpg_handle_t_db, handle);
  162. return (SA_AIS_OK);
  163. }
  164. cpg_error_t cpg_context_get (
  165. cpg_handle_t handle,
  166. void **context)
  167. {
  168. SaAisErrorT error;
  169. struct cpg_inst *cpg_inst;
  170. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  171. if (error != SA_AIS_OK) {
  172. return (error);
  173. }
  174. *context = cpg_inst->context;
  175. saHandleInstancePut (&cpg_handle_t_db, handle);
  176. return (SA_AIS_OK);
  177. }
  178. cpg_error_t cpg_context_set (
  179. cpg_handle_t handle,
  180. void *context)
  181. {
  182. SaAisErrorT error;
  183. struct cpg_inst *cpg_inst;
  184. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  185. if (error != SA_AIS_OK) {
  186. return (error);
  187. }
  188. cpg_inst->context = context;
  189. saHandleInstancePut (&cpg_handle_t_db, handle);
  190. return (SA_AIS_OK);
  191. }
  192. struct res_overlay {
  193. mar_res_header_t header __attribute__((aligned(8)));
  194. char data[512000];
  195. };
  196. cpg_error_t cpg_dispatch (
  197. cpg_handle_t handle,
  198. cpg_dispatch_t dispatch_types)
  199. {
  200. struct pollfd ufds;
  201. int timeout = -1;
  202. SaAisErrorT error;
  203. int cont = 1; /* always continue do loop except when set to 0 */
  204. int dispatch_avail;
  205. struct cpg_inst *cpg_inst;
  206. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  207. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  208. struct res_lib_cpg_groups_get_callback *res_lib_cpg_groups_get_callback;
  209. cpg_callbacks_t callbacks;
  210. struct res_overlay dispatch_data;
  211. int ignore_dispatch = 0;
  212. struct cpg_address member_list[CPG_MEMBERS_MAX];
  213. struct cpg_address left_list[CPG_MEMBERS_MAX];
  214. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  215. struct cpg_name group_name;
  216. mar_cpg_address_t *left_list_start;
  217. mar_cpg_address_t *joined_list_start;
  218. unsigned int i;
  219. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  220. if (error != SA_AIS_OK) {
  221. return (error);
  222. }
  223. /*
  224. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  225. * wait indefinately for SA_DISPATCH_BLOCKING
  226. */
  227. if (dispatch_types == CPG_DISPATCH_ALL) {
  228. timeout = 0;
  229. }
  230. do {
  231. ufds.fd = cpg_inst->dispatch_fd;
  232. ufds.events = POLLIN;
  233. ufds.revents = 0;
  234. error = saPollRetry (&ufds, 1, timeout);
  235. if (error != SA_AIS_OK) {
  236. goto error_nounlock;
  237. }
  238. pthread_mutex_lock (&cpg_inst->dispatch_mutex);
  239. /*
  240. * Regather poll data in case ufds has changed since taking lock
  241. */
  242. error = saPollRetry (&ufds, 1, timeout);
  243. if (error != SA_AIS_OK) {
  244. goto error_nounlock;
  245. }
  246. /*
  247. * Handle has been finalized in another thread
  248. */
  249. if (cpg_inst->finalize == 1) {
  250. error = CPG_OK;
  251. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  252. goto error_unlock;
  253. }
  254. dispatch_avail = ufds.revents & POLLIN;
  255. if (dispatch_avail == 0 && dispatch_types == CPG_DISPATCH_ALL) {
  256. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  257. break; /* exit do while cont is 1 loop */
  258. } else
  259. if (dispatch_avail == 0) {
  260. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  261. continue; /* next poll */
  262. }
  263. if (ufds.revents & POLLIN) {
  264. /*
  265. * Queue empty, read response from socket
  266. */
  267. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.header,
  268. sizeof (mar_res_header_t));
  269. if (error != SA_AIS_OK) {
  270. goto error_unlock;
  271. }
  272. if (dispatch_data.header.size > sizeof (mar_res_header_t)) {
  273. error = saRecvRetry (cpg_inst->dispatch_fd, &dispatch_data.data,
  274. dispatch_data.header.size - sizeof (mar_res_header_t));
  275. if (error != SA_AIS_OK) {
  276. goto error_unlock;
  277. }
  278. }
  279. } else {
  280. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  281. continue;
  282. }
  283. /*
  284. * Make copy of callbacks, message data, unlock instance, and call callback
  285. * A risk of this dispatch method is that the callback routines may
  286. * operate at the same time that cpgFinalize has been called.
  287. */
  288. memcpy (&callbacks, &cpg_inst->callbacks, sizeof (cpg_callbacks_t));
  289. pthread_mutex_unlock (&cpg_inst->dispatch_mutex);
  290. /*
  291. * Dispatch incoming message
  292. */
  293. switch (dispatch_data.header.id) {
  294. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  295. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)&dispatch_data;
  296. cpg_inst->flow_control_state = res_cpg_deliver_callback->flow_control_state;
  297. marshall_from_mar_cpg_name_t (
  298. &group_name,
  299. &res_cpg_deliver_callback->group_name);
  300. callbacks.cpg_deliver_fn (handle,
  301. &group_name,
  302. res_cpg_deliver_callback->nodeid,
  303. res_cpg_deliver_callback->pid,
  304. &res_cpg_deliver_callback->message,
  305. res_cpg_deliver_callback->msglen);
  306. break;
  307. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  308. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)&dispatch_data;
  309. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  310. marshall_from_mar_cpg_address_t (&member_list[i],
  311. &res_cpg_confchg_callback->member_list[i]);
  312. }
  313. left_list_start = res_cpg_confchg_callback->member_list +
  314. res_cpg_confchg_callback->member_list_entries;
  315. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  316. marshall_from_mar_cpg_address_t (&left_list[i],
  317. &left_list_start[i]);
  318. }
  319. joined_list_start = res_cpg_confchg_callback->member_list +
  320. res_cpg_confchg_callback->member_list_entries +
  321. res_cpg_confchg_callback->left_list_entries;
  322. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  323. marshall_from_mar_cpg_address_t (&joined_list[i],
  324. &joined_list_start[i]);
  325. }
  326. marshall_from_mar_cpg_name_t (
  327. &group_name,
  328. &res_cpg_confchg_callback->group_name);
  329. callbacks.cpg_confchg_fn (handle,
  330. &group_name,
  331. member_list,
  332. res_cpg_confchg_callback->member_list_entries,
  333. left_list,
  334. res_cpg_confchg_callback->left_list_entries,
  335. joined_list,
  336. res_cpg_confchg_callback->joined_list_entries);
  337. break;
  338. case MESSAGE_RES_CPG_GROUPS_CALLBACK:
  339. res_lib_cpg_groups_get_callback = (struct res_lib_cpg_groups_get_callback *)&dispatch_data;
  340. marshall_from_mar_cpg_name_t (
  341. &group_name,
  342. &res_lib_cpg_groups_get_callback->group_name);
  343. for (i = 0; i < res_lib_cpg_groups_get_callback->num_members; i++) {
  344. marshall_from_mar_cpg_address_t (&member_list[i],
  345. &res_lib_cpg_groups_get_callback->member_list[i]);
  346. }
  347. callbacks.cpg_groups_get_fn(handle,
  348. res_lib_cpg_groups_get_callback->group_num,
  349. res_lib_cpg_groups_get_callback->total_groups,
  350. &group_name,
  351. member_list,
  352. res_lib_cpg_groups_get_callback->num_members);
  353. break;
  354. default:
  355. error = SA_AIS_ERR_LIBRARY;
  356. goto error_nounlock;
  357. break;
  358. }
  359. /*
  360. * Determine if more messages should be processed
  361. * */
  362. switch (dispatch_types) {
  363. case CPG_DISPATCH_ONE:
  364. if (ignore_dispatch) {
  365. ignore_dispatch = 0;
  366. } else {
  367. cont = 0;
  368. }
  369. break;
  370. case CPG_DISPATCH_ALL:
  371. if (ignore_dispatch) {
  372. ignore_dispatch = 0;
  373. }
  374. break;
  375. case CPG_DISPATCH_BLOCKING:
  376. break;
  377. }
  378. } while (cont);
  379. error_unlock:
  380. saHandleInstancePut (&cpg_handle_t_db, handle);
  381. error_nounlock:
  382. return (error);
  383. }
  384. cpg_error_t cpg_join (
  385. cpg_handle_t handle,
  386. struct cpg_name *group)
  387. {
  388. cpg_error_t error;
  389. struct cpg_inst *cpg_inst;
  390. struct iovec iov[2];
  391. struct req_lib_cpg_join req_lib_cpg_join;
  392. struct res_lib_cpg_join res_lib_cpg_join;
  393. struct req_lib_cpg_trackstart req_lib_cpg_trackstart;
  394. struct res_lib_cpg_trackstart res_lib_cpg_trackstart;
  395. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  396. if (error != SA_AIS_OK) {
  397. return (error);
  398. }
  399. pthread_mutex_lock (&cpg_inst->response_mutex);
  400. /* Automatically add a tracker */
  401. req_lib_cpg_trackstart.header.size = sizeof (struct req_lib_cpg_trackstart);
  402. req_lib_cpg_trackstart.header.id = MESSAGE_REQ_CPG_TRACKSTART;
  403. marshall_to_mar_cpg_name_t (&req_lib_cpg_trackstart.group_name,
  404. group);
  405. iov[0].iov_base = (char *)&req_lib_cpg_trackstart;
  406. iov[0].iov_len = sizeof (struct req_lib_cpg_trackstart);
  407. error = saSendMsgReceiveReply (cpg_inst->dispatch_fd, iov, 1,
  408. &res_lib_cpg_trackstart, sizeof (struct res_lib_cpg_trackstart));
  409. if (error != SA_AIS_OK) {
  410. pthread_mutex_unlock (&cpg_inst->response_mutex);
  411. goto error_exit;
  412. }
  413. /* Now join */
  414. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  415. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  416. req_lib_cpg_join.pid = getpid();
  417. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  418. group);
  419. iov[0].iov_base = (char *)&req_lib_cpg_join;
  420. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  421. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  422. &res_lib_cpg_join, sizeof (struct res_lib_cpg_join));
  423. pthread_mutex_unlock (&cpg_inst->response_mutex);
  424. if (error != SA_AIS_OK) {
  425. goto error_exit;
  426. }
  427. error = res_lib_cpg_join.header.error;
  428. error_exit:
  429. saHandleInstancePut (&cpg_handle_t_db, handle);
  430. return (error);
  431. }
  432. cpg_error_t cpg_leave (
  433. cpg_handle_t handle,
  434. struct cpg_name *group)
  435. {
  436. cpg_error_t error;
  437. struct cpg_inst *cpg_inst;
  438. struct iovec iov[2];
  439. struct req_lib_cpg_leave req_lib_cpg_leave;
  440. struct res_lib_cpg_leave res_lib_cpg_leave;
  441. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  442. if (error != SA_AIS_OK) {
  443. return (error);
  444. }
  445. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  446. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  447. req_lib_cpg_leave.pid = getpid();
  448. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  449. group);
  450. iov[0].iov_base = (char *)&req_lib_cpg_leave;
  451. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  452. pthread_mutex_lock (&cpg_inst->response_mutex);
  453. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, 1,
  454. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  455. pthread_mutex_unlock (&cpg_inst->response_mutex);
  456. if (error != SA_AIS_OK) {
  457. goto error_exit;
  458. }
  459. error = res_lib_cpg_leave.header.error;
  460. error_exit:
  461. saHandleInstancePut (&cpg_handle_t_db, handle);
  462. return (error);
  463. }
  464. cpg_error_t cpg_mcast_joined (
  465. cpg_handle_t handle,
  466. cpg_guarantee_t guarantee,
  467. struct iovec *iovec,
  468. int iov_len)
  469. {
  470. int i;
  471. cpg_error_t error;
  472. struct cpg_inst *cpg_inst;
  473. struct iovec iov[64];
  474. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  475. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  476. int msg_len = 0;
  477. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  478. if (error != SA_AIS_OK) {
  479. return (error);
  480. }
  481. for (i = 0; i < iov_len; i++ ) {
  482. msg_len += iovec[i].iov_len;
  483. }
  484. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  485. msg_len;
  486. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  487. req_lib_cpg_mcast.guarantee = guarantee;
  488. req_lib_cpg_mcast.msglen = msg_len;
  489. iov[0].iov_base = (char *)&req_lib_cpg_mcast;
  490. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  491. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  492. pthread_mutex_lock (&cpg_inst->response_mutex);
  493. error = saSendMsgReceiveReply (cpg_inst->response_fd, iov, iov_len + 1,
  494. &res_lib_cpg_mcast, sizeof (res_lib_cpg_mcast));
  495. pthread_mutex_unlock (&cpg_inst->response_mutex);
  496. if (error != SA_AIS_OK) {
  497. goto error_exit;
  498. }
  499. cpg_inst->flow_control_state = res_lib_cpg_mcast.flow_control_state;
  500. if (res_lib_cpg_mcast.header.error == CPG_ERR_TRY_AGAIN) {
  501. cpg_inst->flow_control_state = CPG_FLOW_CONTROL_ENABLED;
  502. }
  503. error = res_lib_cpg_mcast.header.error;
  504. error_exit:
  505. saHandleInstancePut (&cpg_handle_t_db, handle);
  506. return (error);
  507. }
  508. cpg_error_t cpg_membership_get (
  509. cpg_handle_t handle,
  510. struct cpg_name *group_name,
  511. struct cpg_address *member_list,
  512. int *member_list_entries)
  513. {
  514. cpg_error_t error;
  515. struct cpg_inst *cpg_inst;
  516. struct iovec iov;
  517. struct req_lib_cpg_membership req_lib_cpg_membership_get;
  518. struct res_lib_cpg_confchg_callback res_lib_cpg_membership_get;
  519. unsigned int i;
  520. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  521. if (error != SA_AIS_OK) {
  522. return (error);
  523. }
  524. req_lib_cpg_membership_get.header.size = sizeof (mar_req_header_t);
  525. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  526. marshall_to_mar_cpg_name_t (&req_lib_cpg_membership_get.group_name,
  527. group_name);
  528. iov.iov_base = (char *)&req_lib_cpg_membership_get;
  529. iov.iov_len = sizeof (mar_req_header_t);
  530. pthread_mutex_lock (&cpg_inst->response_mutex);
  531. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  532. &res_lib_cpg_membership_get, sizeof (mar_res_header_t));
  533. pthread_mutex_unlock (&cpg_inst->response_mutex);
  534. if (error != SA_AIS_OK) {
  535. goto error_exit;
  536. }
  537. error = res_lib_cpg_membership_get.header.error;
  538. /*
  539. * Copy results to caller
  540. */
  541. *member_list_entries = res_lib_cpg_membership_get.member_list_entries;
  542. if (member_list) {
  543. for (i = 0; i < res_lib_cpg_membership_get.member_list_entries; i++) {
  544. marshall_from_mar_cpg_address_t (&member_list[i],
  545. &res_lib_cpg_membership_get.member_list[i]);
  546. }
  547. }
  548. error_exit:
  549. saHandleInstancePut (&cpg_handle_t_db, handle);
  550. return (error);
  551. }
  552. cpg_error_t cpg_local_get (
  553. cpg_handle_t handle,
  554. unsigned int *local_nodeid)
  555. {
  556. cpg_error_t error;
  557. struct cpg_inst *cpg_inst;
  558. struct iovec iov;
  559. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  560. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  561. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  562. if (error != SA_AIS_OK) {
  563. return (error);
  564. }
  565. req_lib_cpg_local_get.header.size = sizeof (mar_req_header_t);
  566. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  567. iov.iov_base = &req_lib_cpg_local_get;
  568. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  569. pthread_mutex_lock (&cpg_inst->response_mutex);
  570. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  571. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  572. pthread_mutex_unlock (&cpg_inst->response_mutex);
  573. if (error != SA_AIS_OK) {
  574. goto error_exit;
  575. }
  576. error = res_lib_cpg_local_get.header.error;
  577. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  578. error_exit:
  579. saHandleInstancePut (&cpg_handle_t_db, handle);
  580. return (error);
  581. }
  582. cpg_error_t cpg_groups_get (
  583. cpg_handle_t handle,
  584. unsigned int *num_groups)
  585. {
  586. cpg_error_t error;
  587. struct cpg_inst *cpg_inst;
  588. struct iovec iov;
  589. struct req_lib_cpg_groups_get req_lib_cpg_groups_get;
  590. struct res_lib_cpg_groups_get res_lib_cpg_groups_get;
  591. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  592. if (error != SA_AIS_OK) {
  593. return (error);
  594. }
  595. req_lib_cpg_groups_get.header.size = sizeof (mar_req_header_t);
  596. req_lib_cpg_groups_get.header.id = MESSAGE_REQ_CPG_GROUPS_GET;
  597. iov.iov_base = &req_lib_cpg_groups_get;
  598. iov.iov_len = sizeof (struct req_lib_cpg_groups_get);
  599. pthread_mutex_lock (&cpg_inst->response_mutex);
  600. error = saSendMsgReceiveReply (cpg_inst->response_fd, &iov, 1,
  601. &res_lib_cpg_groups_get, sizeof (res_lib_cpg_groups_get));
  602. pthread_mutex_unlock (&cpg_inst->response_mutex);
  603. if (error != SA_AIS_OK) {
  604. goto error_exit;
  605. }
  606. *num_groups = res_lib_cpg_groups_get.num_groups;
  607. error = res_lib_cpg_groups_get.header.error;
  608. /* Real output is delivered via a callback */
  609. error_exit:
  610. saHandleInstancePut (&cpg_handle_t_db, handle);
  611. return (error);
  612. }
  613. cpg_error_t cpg_flow_control_state_get (
  614. cpg_handle_t handle,
  615. cpg_flow_control_state_t *flow_control_state)
  616. {
  617. cpg_error_t error;
  618. struct cpg_inst *cpg_inst;
  619. error = saHandleInstanceGet (&cpg_handle_t_db, handle, (void *)&cpg_inst);
  620. if (error != SA_AIS_OK) {
  621. return (error);
  622. }
  623. *flow_control_state = cpg_inst->flow_control_state;
  624. saHandleInstancePut (&cpg_handle_t_db, handle);
  625. return (error);
  626. }
  627. /** @} */