cpg.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 <stdio.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <sys/types.h>
  46. #include <sys/socket.h>
  47. #include <sys/mman.h>
  48. #include <errno.h>
  49. #include <limits.h>
  50. #include <qb/qbdefs.h>
  51. #include <qb/qbipcc.h>
  52. #include <corosync/hdb.h>
  53. #include <corosync/list.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/corodefs.h>
  56. #include <corosync/cpg.h>
  57. #include <corosync/ipc_cpg.h>
  58. #include "util.h"
  59. struct cpg_inst {
  60. qb_ipcc_connection_t *c;
  61. int finalize;
  62. void *context;
  63. union {
  64. cpg_model_data_t model_data;
  65. cpg_model_v1_data_t model_v1_data;
  66. };
  67. struct list_head iteration_list_head;
  68. };
  69. DECLARE_HDB_DATABASE(cpg_handle_t_db,NULL);
  70. struct cpg_iteration_instance_t {
  71. cpg_iteration_handle_t cpg_iteration_handle;
  72. qb_ipcc_connection_t *conn;
  73. hdb_handle_t executive_iteration_handle;
  74. struct list_head list;
  75. };
  76. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  77. /*
  78. * Internal (not visible by API) functions
  79. */
  80. static cs_error_t
  81. coroipcc_msg_send_reply_receive (
  82. qb_ipcc_connection_t *c,
  83. const struct iovec *iov,
  84. unsigned int iov_len,
  85. void *res_msg,
  86. size_t res_len)
  87. {
  88. return qb_to_cs_error(qb_ipcc_sendv_recv(c, iov, iov_len, res_msg, res_len,
  89. CS_IPC_TIMEOUT_MS));
  90. }
  91. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  92. {
  93. list_del (&cpg_iteration_instance->list);
  94. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  95. }
  96. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  97. {
  98. struct list_head *iter, *iter_next;
  99. struct cpg_iteration_instance_t *cpg_iteration_instance;
  100. /*
  101. * Traverse thru iteration instances and delete them
  102. */
  103. for (iter = cpg_inst->iteration_list_head.next; iter != &cpg_inst->iteration_list_head;iter = iter_next) {
  104. iter_next = iter->next;
  105. cpg_iteration_instance = list_entry (iter, struct cpg_iteration_instance_t, list);
  106. cpg_iteration_instance_finalize (cpg_iteration_instance);
  107. }
  108. hdb_handle_destroy (&cpg_handle_t_db, handle);
  109. }
  110. /**
  111. * @defgroup cpg_coroipcc The closed process group API
  112. * @ingroup coroipcc
  113. *
  114. * @{
  115. */
  116. cs_error_t cpg_initialize (
  117. cpg_handle_t *handle,
  118. cpg_callbacks_t *callbacks)
  119. {
  120. cpg_model_v1_data_t model_v1_data;
  121. memset (&model_v1_data, 0, sizeof (cpg_model_v1_data_t));
  122. if (callbacks) {
  123. model_v1_data.cpg_deliver_fn = callbacks->cpg_deliver_fn;
  124. model_v1_data.cpg_confchg_fn = callbacks->cpg_confchg_fn;
  125. }
  126. return (cpg_model_initialize (handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_v1_data, NULL));
  127. }
  128. cs_error_t cpg_model_initialize (
  129. cpg_handle_t *handle,
  130. cpg_model_t model,
  131. cpg_model_data_t *model_data,
  132. void *context)
  133. {
  134. cs_error_t error;
  135. struct cpg_inst *cpg_inst;
  136. if (model != CPG_MODEL_V1) {
  137. error = CS_ERR_INVALID_PARAM;
  138. goto error_no_destroy;
  139. }
  140. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  141. if (error != CS_OK) {
  142. goto error_no_destroy;
  143. }
  144. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  145. if (error != CS_OK) {
  146. goto error_destroy;
  147. }
  148. cpg_inst->c = qb_ipcc_connect ("cpg", IPC_REQUEST_SIZE);
  149. if (cpg_inst->c == NULL) {
  150. error = qb_to_cs_error(-errno);
  151. goto error_put_destroy;
  152. }
  153. if (model_data != NULL) {
  154. switch (model) {
  155. case CPG_MODEL_V1:
  156. memcpy (&cpg_inst->model_v1_data, model_data, sizeof (cpg_model_v1_data_t));
  157. if ((cpg_inst->model_v1_data.flags & ~(CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF)) != 0) {
  158. error = CS_ERR_INVALID_PARAM;
  159. goto error_destroy;
  160. }
  161. break;
  162. }
  163. }
  164. cpg_inst->model_data.model = model;
  165. cpg_inst->context = context;
  166. list_init(&cpg_inst->iteration_list_head);
  167. hdb_handle_put (&cpg_handle_t_db, *handle);
  168. return (CS_OK);
  169. error_put_destroy:
  170. hdb_handle_put (&cpg_handle_t_db, *handle);
  171. error_destroy:
  172. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  173. error_no_destroy:
  174. return (error);
  175. }
  176. cs_error_t cpg_finalize (
  177. cpg_handle_t handle)
  178. {
  179. struct cpg_inst *cpg_inst;
  180. struct iovec iov;
  181. struct req_lib_cpg_finalize req_lib_cpg_finalize;
  182. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  183. cs_error_t error;
  184. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  185. if (error != CS_OK) {
  186. return (error);
  187. }
  188. /*
  189. * Another thread has already started finalizing
  190. */
  191. if (cpg_inst->finalize) {
  192. hdb_handle_put (&cpg_handle_t_db, handle);
  193. return (CS_ERR_BAD_HANDLE);
  194. }
  195. cpg_inst->finalize = 1;
  196. /*
  197. * Send service request
  198. */
  199. req_lib_cpg_finalize.header.size = sizeof (struct req_lib_cpg_finalize);
  200. req_lib_cpg_finalize.header.id = MESSAGE_REQ_CPG_FINALIZE;
  201. iov.iov_base = (void *)&req_lib_cpg_finalize;
  202. iov.iov_len = sizeof (struct req_lib_cpg_finalize);
  203. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  204. &iov,
  205. 1,
  206. &res_lib_cpg_finalize,
  207. sizeof (struct res_lib_cpg_finalize));
  208. qb_ipcc_disconnect(cpg_inst->c);
  209. cpg_inst_finalize (cpg_inst, handle);
  210. hdb_handle_put (&cpg_handle_t_db, handle);
  211. return (error);
  212. }
  213. cs_error_t cpg_fd_get (
  214. cpg_handle_t handle,
  215. int *fd)
  216. {
  217. cs_error_t error;
  218. struct cpg_inst *cpg_inst;
  219. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  220. if (error != CS_OK) {
  221. return (error);
  222. }
  223. error = qb_to_cs_error (qb_ipcc_fd_get (cpg_inst->c, fd));
  224. hdb_handle_put (&cpg_handle_t_db, handle);
  225. return (error);
  226. }
  227. cs_error_t cpg_context_get (
  228. cpg_handle_t handle,
  229. void **context)
  230. {
  231. cs_error_t error;
  232. struct cpg_inst *cpg_inst;
  233. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  234. if (error != CS_OK) {
  235. return (error);
  236. }
  237. *context = cpg_inst->context;
  238. hdb_handle_put (&cpg_handle_t_db, handle);
  239. return (CS_OK);
  240. }
  241. cs_error_t cpg_context_set (
  242. cpg_handle_t handle,
  243. void *context)
  244. {
  245. cs_error_t error;
  246. struct cpg_inst *cpg_inst;
  247. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  248. if (error != CS_OK) {
  249. return (error);
  250. }
  251. cpg_inst->context = context;
  252. hdb_handle_put (&cpg_handle_t_db, handle);
  253. return (CS_OK);
  254. }
  255. cs_error_t cpg_dispatch (
  256. cpg_handle_t handle,
  257. cs_dispatch_flags_t dispatch_types)
  258. {
  259. int timeout = -1;
  260. cs_error_t error;
  261. int cont = 1; /* always continue do loop except when set to 0 */
  262. struct cpg_inst *cpg_inst;
  263. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  264. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  265. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  266. struct cpg_inst cpg_inst_copy;
  267. struct qb_ipc_response_header *dispatch_data;
  268. struct cpg_address member_list[CPG_MEMBERS_MAX];
  269. struct cpg_address left_list[CPG_MEMBERS_MAX];
  270. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  271. struct cpg_name group_name;
  272. mar_cpg_address_t *left_list_start;
  273. mar_cpg_address_t *joined_list_start;
  274. unsigned int i;
  275. struct cpg_ring_id ring_id;
  276. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  277. int32_t errno_res;
  278. char dispatch_buf[IPC_DISPATCH_SIZE];
  279. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  280. if (error != CS_OK) {
  281. return (error);
  282. }
  283. /*
  284. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  285. * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  286. */
  287. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  288. timeout = 0;
  289. }
  290. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  291. do {
  292. errno_res = qb_ipcc_event_recv (
  293. cpg_inst->c,
  294. dispatch_buf,
  295. IPC_DISPATCH_SIZE,
  296. timeout);
  297. error = qb_to_cs_error (errno_res);
  298. if (error == CS_ERR_BAD_HANDLE) {
  299. error = CS_OK;
  300. goto error_put;
  301. }
  302. if (error == CS_ERR_TRY_AGAIN) {
  303. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  304. /*
  305. * Don't mask error
  306. */
  307. goto error_put;
  308. }
  309. error = CS_OK;
  310. if (dispatch_types == CS_DISPATCH_ALL) {
  311. break; /* exit do while cont is 1 loop */
  312. } else {
  313. continue; /* next poll */
  314. }
  315. }
  316. if (error != CS_OK) {
  317. goto error_put;
  318. }
  319. /*
  320. * Make copy of callbacks, message data, unlock instance, and call callback
  321. * A risk of this dispatch method is that the callback routines may
  322. * operate at the same time that cpgFinalize has been called.
  323. */
  324. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  325. switch (cpg_inst_copy.model_data.model) {
  326. case CPG_MODEL_V1:
  327. /*
  328. * Dispatch incoming message
  329. */
  330. switch (dispatch_data->id) {
  331. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  332. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  333. break;
  334. }
  335. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  336. marshall_from_mar_cpg_name_t (
  337. &group_name,
  338. &res_cpg_deliver_callback->group_name);
  339. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  340. &group_name,
  341. res_cpg_deliver_callback->nodeid,
  342. res_cpg_deliver_callback->pid,
  343. &res_cpg_deliver_callback->message,
  344. res_cpg_deliver_callback->msglen);
  345. break;
  346. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  347. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  348. break;
  349. }
  350. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  351. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  352. marshall_from_mar_cpg_address_t (&member_list[i],
  353. &res_cpg_confchg_callback->member_list[i]);
  354. }
  355. left_list_start = res_cpg_confchg_callback->member_list +
  356. res_cpg_confchg_callback->member_list_entries;
  357. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  358. marshall_from_mar_cpg_address_t (&left_list[i],
  359. &left_list_start[i]);
  360. }
  361. joined_list_start = res_cpg_confchg_callback->member_list +
  362. res_cpg_confchg_callback->member_list_entries +
  363. res_cpg_confchg_callback->left_list_entries;
  364. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  365. marshall_from_mar_cpg_address_t (&joined_list[i],
  366. &joined_list_start[i]);
  367. }
  368. marshall_from_mar_cpg_name_t (
  369. &group_name,
  370. &res_cpg_confchg_callback->group_name);
  371. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  372. &group_name,
  373. member_list,
  374. res_cpg_confchg_callback->member_list_entries,
  375. left_list,
  376. res_cpg_confchg_callback->left_list_entries,
  377. joined_list,
  378. res_cpg_confchg_callback->joined_list_entries);
  379. break;
  380. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  381. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  382. break;
  383. }
  384. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  385. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  386. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  387. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  388. }
  389. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  390. ring_id,
  391. res_cpg_totem_confchg_callback->member_list_entries,
  392. totem_member_list);
  393. break;
  394. default:
  395. error = CS_ERR_LIBRARY;
  396. goto error_put;
  397. break;
  398. } /* - switch (dispatch_data->id) */
  399. break; /* case CPG_MODEL_V1 */
  400. } /* - switch (cpg_inst_copy.model_data.model) */
  401. /*
  402. * Determine if more messages should be processed
  403. */
  404. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  405. cont = 0;
  406. }
  407. } while (cont);
  408. error_put:
  409. hdb_handle_put (&cpg_handle_t_db, handle);
  410. return (error);
  411. }
  412. cs_error_t cpg_join (
  413. cpg_handle_t handle,
  414. const struct cpg_name *group)
  415. {
  416. cs_error_t error;
  417. struct cpg_inst *cpg_inst;
  418. struct iovec iov[2];
  419. struct req_lib_cpg_join req_lib_cpg_join;
  420. struct res_lib_cpg_join response;
  421. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  422. if (error != CS_OK) {
  423. return (error);
  424. }
  425. /* Now join */
  426. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  427. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  428. req_lib_cpg_join.pid = getpid();
  429. req_lib_cpg_join.flags = 0;
  430. switch (cpg_inst->model_data.model) {
  431. case CPG_MODEL_V1:
  432. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  433. break;
  434. }
  435. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  436. group);
  437. iov[0].iov_base = (void *)&req_lib_cpg_join;
  438. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  439. do {
  440. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  441. &response, sizeof (struct res_lib_cpg_join));
  442. if (error != CS_OK) {
  443. goto error_exit;
  444. }
  445. } while (response.header.error == CS_ERR_BUSY);
  446. error = response.header.error;
  447. error_exit:
  448. hdb_handle_put (&cpg_handle_t_db, handle);
  449. return (error);
  450. }
  451. cs_error_t cpg_leave (
  452. cpg_handle_t handle,
  453. const struct cpg_name *group)
  454. {
  455. cs_error_t error;
  456. struct cpg_inst *cpg_inst;
  457. struct iovec iov[2];
  458. struct req_lib_cpg_leave req_lib_cpg_leave;
  459. struct res_lib_cpg_leave res_lib_cpg_leave;
  460. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  461. if (error != CS_OK) {
  462. return (error);
  463. }
  464. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  465. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  466. req_lib_cpg_leave.pid = getpid();
  467. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  468. group);
  469. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  470. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  471. do {
  472. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  473. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  474. if (error != CS_OK) {
  475. goto error_exit;
  476. }
  477. } while (res_lib_cpg_leave.header.error == CS_ERR_BUSY);
  478. error = res_lib_cpg_leave.header.error;
  479. error_exit:
  480. hdb_handle_put (&cpg_handle_t_db, handle);
  481. return (error);
  482. }
  483. cs_error_t cpg_membership_get (
  484. cpg_handle_t handle,
  485. struct cpg_name *group_name,
  486. struct cpg_address *member_list,
  487. int *member_list_entries)
  488. {
  489. cs_error_t error;
  490. struct cpg_inst *cpg_inst;
  491. struct iovec iov;
  492. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  493. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  494. unsigned int i;
  495. if (member_list == NULL) {
  496. return (CS_ERR_INVALID_PARAM);
  497. }
  498. if (member_list_entries == NULL) {
  499. return (CS_ERR_INVALID_PARAM);
  500. }
  501. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  502. if (error != CS_OK) {
  503. return (error);
  504. }
  505. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  506. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  507. memcpy (&req_lib_cpg_membership_get.group_name, group_name,
  508. sizeof (struct cpg_name));
  509. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  510. iov.iov_len = sizeof (struct qb_ipc_request_header);
  511. do {
  512. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  513. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  514. if (error != CS_OK) {
  515. goto error_exit;
  516. }
  517. } while (res_lib_cpg_membership_get.header.error == CS_ERR_BUSY);
  518. error = res_lib_cpg_membership_get.header.error;
  519. /*
  520. * Copy results to caller
  521. */
  522. *member_list_entries = res_lib_cpg_membership_get.member_count;
  523. if (member_list) {
  524. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  525. marshall_from_mar_cpg_address_t (&member_list[i],
  526. &res_lib_cpg_membership_get.member_list[i]);
  527. }
  528. }
  529. error_exit:
  530. hdb_handle_put (&cpg_handle_t_db, handle);
  531. return (error);
  532. }
  533. cs_error_t cpg_local_get (
  534. cpg_handle_t handle,
  535. unsigned int *local_nodeid)
  536. {
  537. cs_error_t error;
  538. struct cpg_inst *cpg_inst;
  539. struct iovec iov;
  540. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  541. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  542. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  543. if (error != CS_OK) {
  544. return (error);
  545. }
  546. req_lib_cpg_local_get.header.size = sizeof (struct qb_ipc_request_header);
  547. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  548. iov.iov_base = (void *)&req_lib_cpg_local_get;
  549. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  550. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  551. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  552. if (error != CS_OK) {
  553. goto error_exit;
  554. }
  555. error = res_lib_cpg_local_get.header.error;
  556. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  557. error_exit:
  558. hdb_handle_put (&cpg_handle_t_db, handle);
  559. return (error);
  560. }
  561. cs_error_t cpg_flow_control_state_get (
  562. cpg_handle_t handle,
  563. cpg_flow_control_state_t *flow_control_state)
  564. {
  565. cs_error_t error;
  566. struct cpg_inst *cpg_inst;
  567. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  568. if (error != CS_OK) {
  569. return (error);
  570. }
  571. *flow_control_state = CPG_FLOW_CONTROL_DISABLED;
  572. error = CS_OK;
  573. hdb_handle_put (&cpg_handle_t_db, handle);
  574. return (error);
  575. }
  576. static int
  577. memory_map (char *path, const char *file, void **buf, size_t bytes)
  578. {
  579. int32_t fd;
  580. void *addr_orig;
  581. void *addr;
  582. int32_t res;
  583. char *buffer;
  584. int32_t i;
  585. size_t written;
  586. size_t page_size;
  587. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  588. fd = mkstemp (path);
  589. if (fd == -1) {
  590. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  591. fd = mkstemp (path);
  592. if (fd == -1) {
  593. return (-1);
  594. }
  595. }
  596. res = ftruncate (fd, bytes);
  597. if (res == -1) {
  598. goto error_close_unlink;
  599. }
  600. page_size = (size_t)sysconf(_SC_PAGESIZE);
  601. buffer = malloc (page_size);
  602. if (buffer == NULL) {
  603. goto error_close_unlink;
  604. }
  605. memset (buffer, 0, page_size);
  606. for (i = 0; i < (bytes / page_size); i++) {
  607. retry_write:
  608. written = write (fd, buffer, page_size);
  609. if (written == -1 && errno == EINTR) {
  610. goto retry_write;
  611. }
  612. if (written != page_size) {
  613. free (buffer);
  614. goto error_close_unlink;
  615. }
  616. }
  617. free (buffer);
  618. addr_orig = mmap (NULL, bytes, PROT_NONE,
  619. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  620. if (addr_orig == MAP_FAILED) {
  621. goto error_close_unlink;
  622. }
  623. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  624. MAP_FIXED | MAP_SHARED, fd, 0);
  625. if (addr != addr_orig) {
  626. goto error_close_unlink;
  627. }
  628. #ifdef COROSYNC_BSD
  629. madvise(addr_orig, bytes, MADV_NOSYNC);
  630. #endif
  631. res = close (fd);
  632. if (res) {
  633. return (-1);
  634. }
  635. *buf = addr_orig;
  636. return 0;
  637. error_close_unlink:
  638. close (fd);
  639. unlink(path);
  640. return -1;
  641. }
  642. cs_error_t cpg_zcb_alloc (
  643. cpg_handle_t handle,
  644. size_t size,
  645. void **buffer)
  646. {
  647. void *buf = NULL;
  648. char path[PATH_MAX];
  649. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  650. struct qb_ipc_response_header res_coroipcs_zc_alloc;
  651. size_t map_size;
  652. struct iovec iovec;
  653. struct coroipcs_zc_header *hdr;
  654. cs_error_t error;
  655. struct cpg_inst *cpg_inst;
  656. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  657. if (error != CS_OK) {
  658. return (error);
  659. }
  660. map_size = size + sizeof (struct req_lib_cpg_mcast) + sizeof (struct coroipcs_zc_header);
  661. assert(memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size) != -1);
  662. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  663. req_coroipcc_zc_alloc.header.id = MESSAGE_REQ_CPG_ZC_ALLOC;
  664. req_coroipcc_zc_alloc.map_size = map_size;
  665. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  666. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  667. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  668. error = coroipcc_msg_send_reply_receive (
  669. cpg_inst->c,
  670. &iovec,
  671. 1,
  672. &res_coroipcs_zc_alloc,
  673. sizeof (struct qb_ipc_response_header));
  674. hdr = (struct coroipcs_zc_header *)buf;
  675. hdr->map_size = map_size;
  676. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  677. hdb_handle_put (&cpg_handle_t_db, handle);
  678. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  679. return (error);
  680. }
  681. cs_error_t cpg_zcb_free (
  682. cpg_handle_t handle,
  683. void *buffer)
  684. {
  685. cs_error_t error;
  686. struct cpg_inst *cpg_inst;
  687. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  688. struct qb_ipc_response_header res_coroipcs_zc_free;
  689. struct iovec iovec;
  690. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  691. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  692. if (error != CS_OK) {
  693. return (error);
  694. }
  695. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  696. req_coroipcc_zc_free.header.id = MESSAGE_REQ_CPG_ZC_FREE;
  697. req_coroipcc_zc_free.map_size = header->map_size;
  698. req_coroipcc_zc_free.server_address = header->server_address;
  699. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  700. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  701. error = coroipcc_msg_send_reply_receive (
  702. cpg_inst->c,
  703. &iovec,
  704. 1,
  705. &res_coroipcs_zc_free,
  706. sizeof (struct qb_ipc_response_header));
  707. munmap ((void *)header, header->map_size);
  708. hdb_handle_put (&cpg_handle_t_db, handle);
  709. return (error);
  710. }
  711. cs_error_t cpg_zcb_mcast_joined (
  712. cpg_handle_t handle,
  713. cpg_guarantee_t guarantee,
  714. void *msg,
  715. size_t msg_len)
  716. {
  717. cs_error_t error;
  718. struct cpg_inst *cpg_inst;
  719. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  720. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  721. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  722. struct coroipcs_zc_header *hdr;
  723. struct iovec iovec;
  724. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  725. if (error != CS_OK) {
  726. return (error);
  727. }
  728. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  729. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  730. msg_len;
  731. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  732. req_lib_cpg_mcast->guarantee = guarantee;
  733. req_lib_cpg_mcast->msglen = msg_len;
  734. hdr = (struct coroipcs_zc_header *)(((char *)req_lib_cpg_mcast) - sizeof (struct coroipcs_zc_header));
  735. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  736. req_coroipcc_zc_execute.header.id = MESSAGE_REQ_CPG_ZC_EXECUTE;
  737. req_coroipcc_zc_execute.server_address = hdr->server_address;
  738. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  739. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  740. error = coroipcc_msg_send_reply_receive (
  741. cpg_inst->c,
  742. &iovec,
  743. 1,
  744. &res_lib_cpg_mcast,
  745. sizeof(res_lib_cpg_mcast));
  746. if (error != CS_OK) {
  747. goto error_exit;
  748. }
  749. error = res_lib_cpg_mcast.header.error;
  750. error_exit:
  751. hdb_handle_put (&cpg_handle_t_db, handle);
  752. return (error);
  753. }
  754. cs_error_t cpg_mcast_joined (
  755. cpg_handle_t handle,
  756. cpg_guarantee_t guarantee,
  757. const struct iovec *iovec,
  758. unsigned int iov_len)
  759. {
  760. int i;
  761. cs_error_t error;
  762. struct cpg_inst *cpg_inst;
  763. struct iovec iov[64];
  764. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  765. size_t msg_len = 0;
  766. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  767. if (error != CS_OK) {
  768. return (error);
  769. }
  770. for (i = 0; i < iov_len; i++ ) {
  771. msg_len += iovec[i].iov_len;
  772. }
  773. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  774. msg_len;
  775. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  776. req_lib_cpg_mcast.guarantee = guarantee;
  777. req_lib_cpg_mcast.msglen = msg_len;
  778. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  779. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  780. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  781. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  782. error = qb_to_cs_error(qb_ipcc_sendv(cpg_inst->c, iov, iov_len + 1));
  783. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  784. hdb_handle_put (&cpg_handle_t_db, handle);
  785. return (error);
  786. }
  787. cs_error_t cpg_iteration_initialize(
  788. cpg_handle_t handle,
  789. cpg_iteration_type_t iteration_type,
  790. const struct cpg_name *group,
  791. cpg_iteration_handle_t *cpg_iteration_handle)
  792. {
  793. cs_error_t error;
  794. struct iovec iov;
  795. struct cpg_inst *cpg_inst;
  796. struct cpg_iteration_instance_t *cpg_iteration_instance;
  797. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  798. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  799. if (cpg_iteration_handle == NULL) {
  800. return (CS_ERR_INVALID_PARAM);
  801. }
  802. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  803. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  804. return (CS_ERR_INVALID_PARAM);
  805. }
  806. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  807. iteration_type != CPG_ITERATION_ALL) {
  808. return (CS_ERR_INVALID_PARAM);
  809. }
  810. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  811. if (error != CS_OK) {
  812. return (error);
  813. }
  814. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  815. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  816. if (error != CS_OK) {
  817. goto error_put_cpg_db;
  818. }
  819. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  820. (void *)&cpg_iteration_instance));
  821. if (error != CS_OK) {
  822. goto error_destroy;
  823. }
  824. cpg_iteration_instance->conn = cpg_inst->c;
  825. list_init (&cpg_iteration_instance->list);
  826. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  827. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  828. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  829. if (group) {
  830. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  831. }
  832. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  833. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  834. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  835. &iov,
  836. 1,
  837. &res_lib_cpg_iterationinitialize,
  838. sizeof (struct res_lib_cpg_iterationinitialize));
  839. if (error != CS_OK) {
  840. goto error_put_destroy;
  841. }
  842. cpg_iteration_instance->executive_iteration_handle =
  843. res_lib_cpg_iterationinitialize.iteration_handle;
  844. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  845. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  846. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  847. hdb_handle_put (&cpg_handle_t_db, handle);
  848. return (res_lib_cpg_iterationinitialize.header.error);
  849. error_put_destroy:
  850. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  851. error_destroy:
  852. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  853. error_put_cpg_db:
  854. hdb_handle_put (&cpg_handle_t_db, handle);
  855. return (error);
  856. }
  857. cs_error_t cpg_iteration_next(
  858. cpg_iteration_handle_t handle,
  859. struct cpg_iteration_description_t *description)
  860. {
  861. cs_error_t error;
  862. struct cpg_iteration_instance_t *cpg_iteration_instance;
  863. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  864. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  865. if (description == NULL) {
  866. return CS_ERR_INVALID_PARAM;
  867. }
  868. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  869. (void *)&cpg_iteration_instance));
  870. if (error != CS_OK) {
  871. goto error_exit;
  872. }
  873. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  874. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  875. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  876. error = qb_to_cs_error (qb_ipcc_send (cpg_iteration_instance->conn,
  877. &req_lib_cpg_iterationnext,
  878. req_lib_cpg_iterationnext.header.size));
  879. if (error != CS_OK) {
  880. goto error_put;
  881. }
  882. error = qb_to_cs_error (qb_ipcc_recv (cpg_iteration_instance->conn,
  883. &res_lib_cpg_iterationnext,
  884. sizeof(struct res_lib_cpg_iterationnext), -1));
  885. if (error != CS_OK) {
  886. goto error_put;
  887. }
  888. marshall_from_mar_cpg_iteration_description_t(
  889. description,
  890. &res_lib_cpg_iterationnext.description);
  891. error = res_lib_cpg_iterationnext.header.error;
  892. error_put:
  893. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  894. error_exit:
  895. return (error);
  896. }
  897. cs_error_t cpg_iteration_finalize (
  898. cpg_iteration_handle_t handle)
  899. {
  900. cs_error_t error;
  901. struct iovec iov;
  902. struct cpg_iteration_instance_t *cpg_iteration_instance;
  903. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  904. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  905. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  906. (void *)&cpg_iteration_instance));
  907. if (error != CS_OK) {
  908. goto error_exit;
  909. }
  910. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  911. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  912. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  913. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  914. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  915. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn,
  916. &iov,
  917. 1,
  918. &res_lib_cpg_iterationfinalize,
  919. sizeof (struct req_lib_cpg_iterationfinalize));
  920. if (error != CS_OK) {
  921. goto error_put;
  922. }
  923. cpg_iteration_instance_finalize (cpg_iteration_instance);
  924. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  925. return (res_lib_cpg_iterationfinalize.header.error);
  926. error_put:
  927. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  928. error_exit:
  929. return (error);
  930. }
  931. /** @} */