cpg.c 30 KB

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