cpg.c 31 KB

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