cpg.c 27 KB

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