cpg.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2006-2015 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 <sys/uio.h>
  49. #include <sys/stat.h>
  50. #include <errno.h>
  51. #include <limits.h>
  52. #include <qb/qblist.h>
  53. #include <qb/qbdefs.h>
  54. #include <qb/qbipcc.h>
  55. #include <qb/qblog.h>
  56. #include <corosync/hdb.h>
  57. #include <corosync/corotypes.h>
  58. #include <corosync/corodefs.h>
  59. #include <corosync/cpg.h>
  60. #include <corosync/ipc_cpg.h>
  61. #include "util.h"
  62. #ifndef MAP_ANONYMOUS
  63. #define MAP_ANONYMOUS MAP_ANON
  64. #endif
  65. /*
  66. * Maximum number of times to retry a send when transmitting
  67. * a large message fragment
  68. */
  69. #define MAX_RETRIES 100
  70. /*
  71. * ZCB files have following umask (umask is same as used in libqb)
  72. */
  73. #define CPG_MEMORY_MAP_UMASK 077
  74. struct cpg_assembly_data
  75. {
  76. struct qb_list_head list;
  77. uint32_t nodeid;
  78. uint32_t pid;
  79. char *assembly_buf;
  80. uint32_t assembly_buf_ptr;
  81. };
  82. struct cpg_inst {
  83. qb_ipcc_connection_t *c;
  84. int finalize;
  85. void *context;
  86. union {
  87. cpg_model_data_t model_data;
  88. cpg_model_v1_data_t model_v1_data;
  89. };
  90. struct qb_list_head iteration_list_head;
  91. uint32_t max_msg_size;
  92. struct qb_list_head assembly_list_head;
  93. };
  94. static void cpg_inst_free (void *inst);
  95. DECLARE_HDB_DATABASE(cpg_handle_t_db, cpg_inst_free);
  96. struct cpg_iteration_instance_t {
  97. cpg_iteration_handle_t cpg_iteration_handle;
  98. qb_ipcc_connection_t *conn;
  99. hdb_handle_t executive_iteration_handle;
  100. struct qb_list_head list;
  101. };
  102. DECLARE_HDB_DATABASE(cpg_iteration_handle_t_db,NULL);
  103. /*
  104. * Internal (not visible by API) functions
  105. */
  106. static cs_error_t
  107. coroipcc_msg_send_reply_receive (
  108. qb_ipcc_connection_t *c,
  109. const struct iovec *iov,
  110. unsigned int iov_len,
  111. void *res_msg,
  112. size_t res_len)
  113. {
  114. return qb_to_cs_error(qb_ipcc_sendv_recv(c, iov, iov_len, res_msg, res_len,
  115. CS_IPC_TIMEOUT_MS));
  116. }
  117. static void cpg_iteration_instance_finalize (struct cpg_iteration_instance_t *cpg_iteration_instance)
  118. {
  119. qb_list_del (&cpg_iteration_instance->list);
  120. hdb_handle_destroy (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  121. }
  122. static void cpg_inst_free (void *inst)
  123. {
  124. struct cpg_inst *cpg_inst = (struct cpg_inst *)inst;
  125. qb_ipcc_disconnect(cpg_inst->c);
  126. }
  127. static void cpg_inst_finalize (struct cpg_inst *cpg_inst, hdb_handle_t handle)
  128. {
  129. struct qb_list_head *iter, *tmp_iter;
  130. struct cpg_iteration_instance_t *cpg_iteration_instance;
  131. /*
  132. * Traverse thru iteration instances and delete them
  133. */
  134. qb_list_for_each_safe(iter, tmp_iter, &(cpg_inst->iteration_list_head)) {
  135. cpg_iteration_instance = qb_list_entry (iter, struct cpg_iteration_instance_t, list);
  136. cpg_iteration_instance_finalize (cpg_iteration_instance);
  137. }
  138. hdb_handle_destroy (&cpg_handle_t_db, handle);
  139. }
  140. /**
  141. * @defgroup cpg_coroipcc The closed process group API
  142. * @ingroup coroipcc
  143. *
  144. * @{
  145. */
  146. cs_error_t cpg_initialize (
  147. cpg_handle_t *handle,
  148. cpg_callbacks_t *callbacks)
  149. {
  150. cpg_model_v1_data_t model_v1_data;
  151. memset (&model_v1_data, 0, sizeof (cpg_model_v1_data_t));
  152. if (callbacks) {
  153. model_v1_data.cpg_deliver_fn = callbacks->cpg_deliver_fn;
  154. model_v1_data.cpg_confchg_fn = callbacks->cpg_confchg_fn;
  155. }
  156. return (cpg_model_initialize (handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_v1_data, NULL));
  157. }
  158. cs_error_t cpg_model_initialize (
  159. cpg_handle_t *handle,
  160. cpg_model_t model,
  161. cpg_model_data_t *model_data,
  162. void *context)
  163. {
  164. cs_error_t error;
  165. struct cpg_inst *cpg_inst;
  166. if (model != CPG_MODEL_V1) {
  167. error = CS_ERR_INVALID_PARAM;
  168. goto error_no_destroy;
  169. }
  170. error = hdb_error_to_cs (hdb_handle_create (&cpg_handle_t_db, sizeof (struct cpg_inst), handle));
  171. if (error != CS_OK) {
  172. goto error_no_destroy;
  173. }
  174. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, *handle, (void *)&cpg_inst));
  175. if (error != CS_OK) {
  176. goto error_destroy;
  177. }
  178. cpg_inst->c = qb_ipcc_connect ("cpg", IPC_REQUEST_SIZE);
  179. if (cpg_inst->c == NULL) {
  180. error = qb_to_cs_error(-errno);
  181. goto error_put_destroy;
  182. }
  183. if (model_data != NULL) {
  184. switch (model) {
  185. case CPG_MODEL_V1:
  186. memcpy (&cpg_inst->model_v1_data, model_data, sizeof (cpg_model_v1_data_t));
  187. if ((cpg_inst->model_v1_data.flags & ~(CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF)) != 0) {
  188. error = CS_ERR_INVALID_PARAM;
  189. goto error_destroy;
  190. }
  191. break;
  192. }
  193. }
  194. /* Allow space for corosync internal headers */
  195. cpg_inst->max_msg_size = IPC_REQUEST_SIZE - 1024;
  196. cpg_inst->model_data.model = model;
  197. cpg_inst->context = context;
  198. qb_list_init(&cpg_inst->iteration_list_head);
  199. qb_list_init(&cpg_inst->assembly_list_head);
  200. hdb_handle_put (&cpg_handle_t_db, *handle);
  201. return (CS_OK);
  202. error_put_destroy:
  203. hdb_handle_put (&cpg_handle_t_db, *handle);
  204. error_destroy:
  205. hdb_handle_destroy (&cpg_handle_t_db, *handle);
  206. error_no_destroy:
  207. return (error);
  208. }
  209. cs_error_t cpg_finalize (
  210. cpg_handle_t handle)
  211. {
  212. struct cpg_inst *cpg_inst;
  213. struct iovec iov;
  214. struct req_lib_cpg_finalize req_lib_cpg_finalize;
  215. struct res_lib_cpg_finalize res_lib_cpg_finalize;
  216. cs_error_t error;
  217. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  218. if (error != CS_OK) {
  219. return (error);
  220. }
  221. /*
  222. * Another thread has already started finalizing
  223. */
  224. if (cpg_inst->finalize) {
  225. hdb_handle_put (&cpg_handle_t_db, handle);
  226. return (CS_ERR_BAD_HANDLE);
  227. }
  228. cpg_inst->finalize = 1;
  229. /*
  230. * Send service request
  231. */
  232. req_lib_cpg_finalize.header.size = sizeof (struct req_lib_cpg_finalize);
  233. req_lib_cpg_finalize.header.id = MESSAGE_REQ_CPG_FINALIZE;
  234. iov.iov_base = (void *)&req_lib_cpg_finalize;
  235. iov.iov_len = sizeof (struct req_lib_cpg_finalize);
  236. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  237. &iov,
  238. 1,
  239. &res_lib_cpg_finalize,
  240. sizeof (struct res_lib_cpg_finalize));
  241. cpg_inst_finalize (cpg_inst, handle);
  242. hdb_handle_put (&cpg_handle_t_db, handle);
  243. return (error);
  244. }
  245. cs_error_t cpg_fd_get (
  246. cpg_handle_t handle,
  247. int *fd)
  248. {
  249. cs_error_t error;
  250. struct cpg_inst *cpg_inst;
  251. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  252. if (error != CS_OK) {
  253. return (error);
  254. }
  255. error = qb_to_cs_error (qb_ipcc_fd_get (cpg_inst->c, fd));
  256. hdb_handle_put (&cpg_handle_t_db, handle);
  257. return (error);
  258. }
  259. cs_error_t cpg_max_atomic_msgsize_get (
  260. cpg_handle_t handle,
  261. uint32_t *size)
  262. {
  263. cs_error_t error;
  264. struct cpg_inst *cpg_inst;
  265. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  266. if (error != CS_OK) {
  267. return (error);
  268. }
  269. *size = cpg_inst->max_msg_size;
  270. hdb_handle_put (&cpg_handle_t_db, handle);
  271. return (error);
  272. }
  273. cs_error_t cpg_context_get (
  274. cpg_handle_t handle,
  275. void **context)
  276. {
  277. cs_error_t error;
  278. struct cpg_inst *cpg_inst;
  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. *context = cpg_inst->context;
  284. hdb_handle_put (&cpg_handle_t_db, handle);
  285. return (CS_OK);
  286. }
  287. cs_error_t cpg_context_set (
  288. cpg_handle_t handle,
  289. void *context)
  290. {
  291. cs_error_t error;
  292. struct cpg_inst *cpg_inst;
  293. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  294. if (error != CS_OK) {
  295. return (error);
  296. }
  297. cpg_inst->context = context;
  298. hdb_handle_put (&cpg_handle_t_db, handle);
  299. return (CS_OK);
  300. }
  301. cs_error_t cpg_dispatch (
  302. cpg_handle_t handle,
  303. cs_dispatch_flags_t dispatch_types)
  304. {
  305. int timeout = -1;
  306. cs_error_t error;
  307. int cont = 1; /* always continue do loop except when set to 0 */
  308. struct cpg_inst *cpg_inst;
  309. struct res_lib_cpg_confchg_callback *res_cpg_confchg_callback;
  310. struct res_lib_cpg_deliver_callback *res_cpg_deliver_callback;
  311. struct res_lib_cpg_partial_deliver_callback *res_cpg_partial_deliver_callback;
  312. struct res_lib_cpg_totem_confchg_callback *res_cpg_totem_confchg_callback;
  313. struct cpg_inst cpg_inst_copy;
  314. struct qb_ipc_response_header *dispatch_data;
  315. struct cpg_address member_list[CPG_MEMBERS_MAX];
  316. struct cpg_address left_list[CPG_MEMBERS_MAX];
  317. struct cpg_address joined_list[CPG_MEMBERS_MAX];
  318. struct cpg_name group_name;
  319. struct cpg_assembly_data *assembly_data;
  320. struct qb_list_head *iter, *tmp_iter;
  321. mar_cpg_address_t *left_list_start;
  322. mar_cpg_address_t *joined_list_start;
  323. unsigned int i;
  324. struct cpg_ring_id ring_id;
  325. uint32_t totem_member_list[CPG_MEMBERS_MAX];
  326. int32_t errno_res;
  327. char dispatch_buf[IPC_DISPATCH_SIZE];
  328. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  329. if (error != CS_OK) {
  330. return (error);
  331. }
  332. /*
  333. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  334. * wait indefinitely for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  335. */
  336. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  337. timeout = 0;
  338. }
  339. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  340. do {
  341. errno_res = qb_ipcc_event_recv (
  342. cpg_inst->c,
  343. dispatch_buf,
  344. IPC_DISPATCH_SIZE,
  345. timeout);
  346. error = qb_to_cs_error (errno_res);
  347. if (error == CS_ERR_BAD_HANDLE) {
  348. error = CS_OK;
  349. goto error_put;
  350. }
  351. if (error == CS_ERR_TRY_AGAIN) {
  352. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  353. /*
  354. * Don't mask error
  355. */
  356. goto error_put;
  357. }
  358. error = CS_OK;
  359. if (dispatch_types == CS_DISPATCH_ALL) {
  360. break; /* exit do while cont is 1 loop */
  361. } else {
  362. continue; /* next poll */
  363. }
  364. }
  365. if (error != CS_OK) {
  366. goto error_put;
  367. }
  368. /*
  369. * Make copy of callbacks, message data, unlock instance, and call callback
  370. * A risk of this dispatch method is that the callback routines may
  371. * operate at the same time that cpgFinalize has been called.
  372. */
  373. memcpy (&cpg_inst_copy, cpg_inst, sizeof (struct cpg_inst));
  374. switch (cpg_inst_copy.model_data.model) {
  375. case CPG_MODEL_V1:
  376. /*
  377. * Dispatch incoming message
  378. */
  379. switch (dispatch_data->id) {
  380. case MESSAGE_RES_CPG_DELIVER_CALLBACK:
  381. if (cpg_inst_copy.model_v1_data.cpg_deliver_fn == NULL) {
  382. break;
  383. }
  384. res_cpg_deliver_callback = (struct res_lib_cpg_deliver_callback *)dispatch_data;
  385. marshall_from_mar_cpg_name_t (
  386. &group_name,
  387. &res_cpg_deliver_callback->group_name);
  388. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  389. &group_name,
  390. res_cpg_deliver_callback->nodeid,
  391. res_cpg_deliver_callback->pid,
  392. &res_cpg_deliver_callback->message,
  393. res_cpg_deliver_callback->msglen);
  394. break;
  395. case MESSAGE_RES_CPG_PARTIAL_DELIVER_CALLBACK:
  396. res_cpg_partial_deliver_callback = (struct res_lib_cpg_partial_deliver_callback *)dispatch_data;
  397. marshall_from_mar_cpg_name_t (
  398. &group_name,
  399. &res_cpg_partial_deliver_callback->group_name);
  400. /*
  401. * Search for assembly data for current messages (nodeid, pid) pair in list of assemblies
  402. */
  403. assembly_data = NULL;
  404. qb_list_for_each(iter, &(cpg_inst->assembly_list_head)) {
  405. struct cpg_assembly_data *current_assembly_data = qb_list_entry (iter, struct cpg_assembly_data, list);
  406. if (current_assembly_data->nodeid == res_cpg_partial_deliver_callback->nodeid && current_assembly_data->pid == res_cpg_partial_deliver_callback->pid) {
  407. assembly_data = current_assembly_data;
  408. break;
  409. }
  410. }
  411. if (res_cpg_partial_deliver_callback->type == LIBCPG_PARTIAL_FIRST) {
  412. /*
  413. * As this is LIBCPG_PARTIAL_FIRST packet, check that there is no ongoing assembly
  414. */
  415. if (assembly_data) {
  416. error = CS_ERR_MESSAGE_ERROR;
  417. goto error_put;
  418. }
  419. assembly_data = malloc(sizeof(struct cpg_assembly_data));
  420. if (!assembly_data) {
  421. error = CS_ERR_NO_MEMORY;
  422. goto error_put;
  423. }
  424. assembly_data->nodeid = res_cpg_partial_deliver_callback->nodeid;
  425. assembly_data->pid = res_cpg_partial_deliver_callback->pid;
  426. assembly_data->assembly_buf = malloc(res_cpg_partial_deliver_callback->msglen);
  427. if (!assembly_data->assembly_buf) {
  428. free(assembly_data);
  429. error = CS_ERR_NO_MEMORY;
  430. goto error_put;
  431. }
  432. assembly_data->assembly_buf_ptr = 0;
  433. qb_list_init (&assembly_data->list);
  434. qb_list_add (&assembly_data->list, &cpg_inst->assembly_list_head);
  435. }
  436. if (assembly_data) {
  437. memcpy(assembly_data->assembly_buf + assembly_data->assembly_buf_ptr,
  438. res_cpg_partial_deliver_callback->message, res_cpg_partial_deliver_callback->fraglen);
  439. assembly_data->assembly_buf_ptr += res_cpg_partial_deliver_callback->fraglen;
  440. if (res_cpg_partial_deliver_callback->type == LIBCPG_PARTIAL_LAST) {
  441. cpg_inst_copy.model_v1_data.cpg_deliver_fn (handle,
  442. &group_name,
  443. res_cpg_partial_deliver_callback->nodeid,
  444. res_cpg_partial_deliver_callback->pid,
  445. assembly_data->assembly_buf,
  446. res_cpg_partial_deliver_callback->msglen);
  447. qb_list_del (&assembly_data->list);
  448. free(assembly_data->assembly_buf);
  449. free(assembly_data);
  450. }
  451. }
  452. break;
  453. case MESSAGE_RES_CPG_CONFCHG_CALLBACK:
  454. if (cpg_inst_copy.model_v1_data.cpg_confchg_fn == NULL) {
  455. break;
  456. }
  457. res_cpg_confchg_callback = (struct res_lib_cpg_confchg_callback *)dispatch_data;
  458. for (i = 0; i < res_cpg_confchg_callback->member_list_entries; i++) {
  459. marshall_from_mar_cpg_address_t (&member_list[i],
  460. &res_cpg_confchg_callback->member_list[i]);
  461. }
  462. left_list_start = res_cpg_confchg_callback->member_list +
  463. res_cpg_confchg_callback->member_list_entries;
  464. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  465. marshall_from_mar_cpg_address_t (&left_list[i],
  466. &left_list_start[i]);
  467. }
  468. joined_list_start = res_cpg_confchg_callback->member_list +
  469. res_cpg_confchg_callback->member_list_entries +
  470. res_cpg_confchg_callback->left_list_entries;
  471. for (i = 0; i < res_cpg_confchg_callback->joined_list_entries; i++) {
  472. marshall_from_mar_cpg_address_t (&joined_list[i],
  473. &joined_list_start[i]);
  474. }
  475. marshall_from_mar_cpg_name_t (
  476. &group_name,
  477. &res_cpg_confchg_callback->group_name);
  478. cpg_inst_copy.model_v1_data.cpg_confchg_fn (handle,
  479. &group_name,
  480. member_list,
  481. res_cpg_confchg_callback->member_list_entries,
  482. left_list,
  483. res_cpg_confchg_callback->left_list_entries,
  484. joined_list,
  485. res_cpg_confchg_callback->joined_list_entries);
  486. /*
  487. * If member left while his partial packet was being assembled, assembly data must be removed from list
  488. */
  489. for (i = 0; i < res_cpg_confchg_callback->left_list_entries; i++) {
  490. qb_list_for_each_safe(iter, tmp_iter, &(cpg_inst->assembly_list_head)) {
  491. struct cpg_assembly_data *current_assembly_data = qb_list_entry (iter, struct cpg_assembly_data, list);
  492. if (current_assembly_data->nodeid != left_list[i].nodeid || current_assembly_data->pid != left_list[i].pid)
  493. continue;
  494. qb_list_del (&current_assembly_data->list);
  495. free(current_assembly_data->assembly_buf);
  496. free(current_assembly_data);
  497. }
  498. }
  499. break;
  500. case MESSAGE_RES_CPG_TOTEM_CONFCHG_CALLBACK:
  501. if (cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn == NULL) {
  502. break;
  503. }
  504. res_cpg_totem_confchg_callback = (struct res_lib_cpg_totem_confchg_callback *)dispatch_data;
  505. marshall_from_mar_cpg_ring_id_t (&ring_id, &res_cpg_totem_confchg_callback->ring_id);
  506. for (i = 0; i < res_cpg_totem_confchg_callback->member_list_entries; i++) {
  507. totem_member_list[i] = res_cpg_totem_confchg_callback->member_list[i];
  508. }
  509. cpg_inst_copy.model_v1_data.cpg_totem_confchg_fn (handle,
  510. ring_id,
  511. res_cpg_totem_confchg_callback->member_list_entries,
  512. totem_member_list);
  513. break;
  514. default:
  515. error = CS_ERR_LIBRARY;
  516. goto error_put;
  517. break;
  518. } /* - switch (dispatch_data->id) */
  519. break; /* case CPG_MODEL_V1 */
  520. } /* - switch (cpg_inst_copy.model_data.model) */
  521. if (cpg_inst_copy.finalize || cpg_inst->finalize) {
  522. /*
  523. * If the finalize has been called then get out of the dispatch.
  524. */
  525. cpg_inst->finalize = 1;
  526. error = CS_ERR_BAD_HANDLE;
  527. goto error_put;
  528. }
  529. /*
  530. * Determine if more messages should be processed
  531. */
  532. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  533. cont = 0;
  534. }
  535. } while (cont);
  536. error_put:
  537. hdb_handle_put (&cpg_handle_t_db, handle);
  538. return (error);
  539. }
  540. cs_error_t cpg_join (
  541. cpg_handle_t handle,
  542. const struct cpg_name *group)
  543. {
  544. cs_error_t error;
  545. struct cpg_inst *cpg_inst;
  546. struct iovec iov[2];
  547. struct req_lib_cpg_join req_lib_cpg_join;
  548. struct res_lib_cpg_join response;
  549. if (group->length > CPG_MAX_NAME_LENGTH) {
  550. return (CS_ERR_NAME_TOO_LONG);
  551. }
  552. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  553. if (error != CS_OK) {
  554. return (error);
  555. }
  556. /* Now join */
  557. req_lib_cpg_join.header.size = sizeof (struct req_lib_cpg_join);
  558. req_lib_cpg_join.header.id = MESSAGE_REQ_CPG_JOIN;
  559. req_lib_cpg_join.pid = getpid();
  560. req_lib_cpg_join.flags = 0;
  561. switch (cpg_inst->model_data.model) {
  562. case CPG_MODEL_V1:
  563. req_lib_cpg_join.flags = cpg_inst->model_v1_data.flags;
  564. break;
  565. }
  566. marshall_to_mar_cpg_name_t (&req_lib_cpg_join.group_name,
  567. group);
  568. iov[0].iov_base = (void *)&req_lib_cpg_join;
  569. iov[0].iov_len = sizeof (struct req_lib_cpg_join);
  570. do {
  571. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  572. &response, sizeof (struct res_lib_cpg_join));
  573. if (error != CS_OK) {
  574. goto error_exit;
  575. }
  576. } while (response.header.error == CS_ERR_BUSY);
  577. error = response.header.error;
  578. error_exit:
  579. hdb_handle_put (&cpg_handle_t_db, handle);
  580. return (error);
  581. }
  582. cs_error_t cpg_leave (
  583. cpg_handle_t handle,
  584. const struct cpg_name *group)
  585. {
  586. cs_error_t error;
  587. struct cpg_inst *cpg_inst;
  588. struct iovec iov[2];
  589. struct req_lib_cpg_leave req_lib_cpg_leave;
  590. struct res_lib_cpg_leave res_lib_cpg_leave;
  591. if (group->length > CPG_MAX_NAME_LENGTH) {
  592. return (CS_ERR_NAME_TOO_LONG);
  593. }
  594. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  595. if (error != CS_OK) {
  596. return (error);
  597. }
  598. req_lib_cpg_leave.header.size = sizeof (struct req_lib_cpg_leave);
  599. req_lib_cpg_leave.header.id = MESSAGE_REQ_CPG_LEAVE;
  600. req_lib_cpg_leave.pid = getpid();
  601. marshall_to_mar_cpg_name_t (&req_lib_cpg_leave.group_name,
  602. group);
  603. iov[0].iov_base = (void *)&req_lib_cpg_leave;
  604. iov[0].iov_len = sizeof (struct req_lib_cpg_leave);
  605. do {
  606. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 1,
  607. &res_lib_cpg_leave, sizeof (struct res_lib_cpg_leave));
  608. if (error != CS_OK) {
  609. goto error_exit;
  610. }
  611. } while (res_lib_cpg_leave.header.error == CS_ERR_BUSY);
  612. error = res_lib_cpg_leave.header.error;
  613. error_exit:
  614. hdb_handle_put (&cpg_handle_t_db, handle);
  615. return (error);
  616. }
  617. cs_error_t cpg_membership_get (
  618. cpg_handle_t handle,
  619. struct cpg_name *group_name,
  620. struct cpg_address *member_list,
  621. int *member_list_entries)
  622. {
  623. cs_error_t error;
  624. struct cpg_inst *cpg_inst;
  625. struct iovec iov;
  626. struct req_lib_cpg_membership_get req_lib_cpg_membership_get;
  627. struct res_lib_cpg_membership_get res_lib_cpg_membership_get;
  628. unsigned int i;
  629. if (group_name->length > CPG_MAX_NAME_LENGTH) {
  630. return (CS_ERR_NAME_TOO_LONG);
  631. }
  632. if (member_list == NULL) {
  633. return (CS_ERR_INVALID_PARAM);
  634. }
  635. if (member_list_entries == NULL) {
  636. return (CS_ERR_INVALID_PARAM);
  637. }
  638. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  639. if (error != CS_OK) {
  640. return (error);
  641. }
  642. req_lib_cpg_membership_get.header.size = sizeof (struct req_lib_cpg_membership_get);
  643. req_lib_cpg_membership_get.header.id = MESSAGE_REQ_CPG_MEMBERSHIP;
  644. marshall_to_mar_cpg_name_t (&req_lib_cpg_membership_get.group_name,
  645. group_name);
  646. iov.iov_base = (void *)&req_lib_cpg_membership_get;
  647. iov.iov_len = sizeof (struct req_lib_cpg_membership_get);
  648. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  649. &res_lib_cpg_membership_get, sizeof (res_lib_cpg_membership_get));
  650. if (error != CS_OK) {
  651. goto error_exit;
  652. }
  653. error = res_lib_cpg_membership_get.header.error;
  654. /*
  655. * Copy results to caller
  656. */
  657. *member_list_entries = res_lib_cpg_membership_get.member_count;
  658. if (member_list) {
  659. for (i = 0; i < res_lib_cpg_membership_get.member_count; i++) {
  660. marshall_from_mar_cpg_address_t (&member_list[i],
  661. &res_lib_cpg_membership_get.member_list[i]);
  662. }
  663. }
  664. error_exit:
  665. hdb_handle_put (&cpg_handle_t_db, handle);
  666. return (error);
  667. }
  668. cs_error_t cpg_local_get (
  669. cpg_handle_t handle,
  670. unsigned int *local_nodeid)
  671. {
  672. cs_error_t error;
  673. struct cpg_inst *cpg_inst;
  674. struct iovec iov;
  675. struct req_lib_cpg_local_get req_lib_cpg_local_get;
  676. struct res_lib_cpg_local_get res_lib_cpg_local_get;
  677. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  678. if (error != CS_OK) {
  679. return (error);
  680. }
  681. req_lib_cpg_local_get.header.size = sizeof (struct qb_ipc_request_header);
  682. req_lib_cpg_local_get.header.id = MESSAGE_REQ_CPG_LOCAL_GET;
  683. iov.iov_base = (void *)&req_lib_cpg_local_get;
  684. iov.iov_len = sizeof (struct req_lib_cpg_local_get);
  685. error = coroipcc_msg_send_reply_receive (cpg_inst->c, &iov, 1,
  686. &res_lib_cpg_local_get, sizeof (res_lib_cpg_local_get));
  687. if (error != CS_OK) {
  688. goto error_exit;
  689. }
  690. error = res_lib_cpg_local_get.header.error;
  691. *local_nodeid = res_lib_cpg_local_get.local_nodeid;
  692. error_exit:
  693. hdb_handle_put (&cpg_handle_t_db, handle);
  694. return (error);
  695. }
  696. cs_error_t cpg_flow_control_state_get (
  697. cpg_handle_t handle,
  698. cpg_flow_control_state_t *flow_control_state)
  699. {
  700. cs_error_t error;
  701. struct cpg_inst *cpg_inst;
  702. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  703. if (error != CS_OK) {
  704. return (error);
  705. }
  706. *flow_control_state = CPG_FLOW_CONTROL_DISABLED;
  707. error = CS_OK;
  708. hdb_handle_put (&cpg_handle_t_db, handle);
  709. return (error);
  710. }
  711. static int
  712. memory_map (char *path, const char *file, void **buf, size_t bytes)
  713. {
  714. int32_t fd;
  715. void *addr;
  716. int32_t res;
  717. char *buffer;
  718. int32_t i;
  719. size_t written;
  720. size_t page_size;
  721. long int sysconf_page_size;
  722. mode_t old_umask;
  723. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  724. old_umask = umask(CPG_MEMORY_MAP_UMASK);
  725. fd = mkstemp (path);
  726. (void)umask(old_umask);
  727. if (fd == -1) {
  728. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  729. old_umask = umask(CPG_MEMORY_MAP_UMASK);
  730. fd = mkstemp (path);
  731. (void)umask(old_umask);
  732. if (fd == -1) {
  733. return (-1);
  734. }
  735. }
  736. res = ftruncate (fd, bytes);
  737. if (res == -1) {
  738. goto error_close_unlink;
  739. }
  740. sysconf_page_size = sysconf(_SC_PAGESIZE);
  741. if (sysconf_page_size <= 0) {
  742. goto error_close_unlink;
  743. }
  744. page_size = sysconf_page_size;
  745. buffer = malloc (page_size);
  746. if (buffer == NULL) {
  747. goto error_close_unlink;
  748. }
  749. memset (buffer, 0, page_size);
  750. for (i = 0; i < (bytes / page_size); i++) {
  751. retry_write:
  752. written = write (fd, buffer, page_size);
  753. if (written == -1 && errno == EINTR) {
  754. goto retry_write;
  755. }
  756. if (written != page_size) {
  757. free (buffer);
  758. goto error_close_unlink;
  759. }
  760. }
  761. free (buffer);
  762. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  763. MAP_SHARED, fd, 0);
  764. if (addr == MAP_FAILED) {
  765. goto error_close_unlink;
  766. }
  767. #ifdef MADV_NOSYNC
  768. madvise(addr, bytes, MADV_NOSYNC);
  769. #endif
  770. res = close (fd);
  771. if (res) {
  772. munmap(addr, bytes);
  773. return (-1);
  774. }
  775. *buf = addr;
  776. return 0;
  777. error_close_unlink:
  778. close (fd);
  779. unlink(path);
  780. return -1;
  781. }
  782. cs_error_t cpg_zcb_alloc (
  783. cpg_handle_t handle,
  784. size_t size,
  785. void **buffer)
  786. {
  787. void *buf = NULL;
  788. char path[PATH_MAX];
  789. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  790. struct qb_ipc_response_header res_coroipcs_zc_alloc;
  791. size_t map_size;
  792. struct iovec iovec;
  793. struct coroipcs_zc_header *hdr;
  794. cs_error_t error;
  795. struct cpg_inst *cpg_inst;
  796. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  797. if (error != CS_OK) {
  798. return (error);
  799. }
  800. map_size = size + sizeof (struct req_lib_cpg_mcast) + sizeof (struct coroipcs_zc_header);
  801. assert(memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size) != -1);
  802. if (strlen(path) >= CPG_ZC_PATH_LEN) {
  803. unlink(path);
  804. munmap (buf, map_size);
  805. return (CS_ERR_NAME_TOO_LONG);
  806. }
  807. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  808. req_coroipcc_zc_alloc.header.id = MESSAGE_REQ_CPG_ZC_ALLOC;
  809. req_coroipcc_zc_alloc.map_size = map_size;
  810. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  811. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  812. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  813. error = coroipcc_msg_send_reply_receive (
  814. cpg_inst->c,
  815. &iovec,
  816. 1,
  817. &res_coroipcs_zc_alloc,
  818. sizeof (struct qb_ipc_response_header));
  819. if (error != CS_OK) {
  820. goto error_exit;
  821. }
  822. hdr = (struct coroipcs_zc_header *)buf;
  823. hdr->map_size = map_size;
  824. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header) + sizeof (struct req_lib_cpg_mcast);
  825. error_exit:
  826. hdb_handle_put (&cpg_handle_t_db, handle);
  827. return (error);
  828. }
  829. cs_error_t cpg_zcb_free (
  830. cpg_handle_t handle,
  831. void *buffer)
  832. {
  833. cs_error_t error;
  834. unsigned int res;
  835. struct cpg_inst *cpg_inst;
  836. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  837. struct qb_ipc_response_header res_coroipcs_zc_free;
  838. struct iovec iovec;
  839. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header) - sizeof (struct req_lib_cpg_mcast));
  840. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  841. if (error != CS_OK) {
  842. return (error);
  843. }
  844. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  845. req_coroipcc_zc_free.header.id = MESSAGE_REQ_CPG_ZC_FREE;
  846. req_coroipcc_zc_free.map_size = header->map_size;
  847. req_coroipcc_zc_free.server_address = header->server_address;
  848. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  849. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  850. error = coroipcc_msg_send_reply_receive (
  851. cpg_inst->c,
  852. &iovec,
  853. 1,
  854. &res_coroipcs_zc_free,
  855. sizeof (struct qb_ipc_response_header));
  856. if (error != CS_OK) {
  857. goto error_exit;
  858. }
  859. res = munmap ((void *)header, header->map_size);
  860. if (res == -1) {
  861. error = qb_to_cs_error(-errno);
  862. goto error_exit;
  863. }
  864. error_exit:
  865. hdb_handle_put (&cpg_handle_t_db, handle);
  866. return (error);
  867. }
  868. cs_error_t cpg_zcb_mcast_joined (
  869. cpg_handle_t handle,
  870. cpg_guarantee_t guarantee,
  871. void *msg,
  872. size_t msg_len)
  873. {
  874. cs_error_t error;
  875. struct cpg_inst *cpg_inst;
  876. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  877. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  878. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  879. struct coroipcs_zc_header *hdr;
  880. struct iovec iovec;
  881. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  882. if (error != CS_OK) {
  883. return (error);
  884. }
  885. if (msg_len > IPC_REQUEST_SIZE) {
  886. error = CS_ERR_TOO_BIG;
  887. goto error_exit;
  888. }
  889. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  890. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  891. msg_len;
  892. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  893. req_lib_cpg_mcast->guarantee = guarantee;
  894. req_lib_cpg_mcast->msglen = msg_len;
  895. hdr = (struct coroipcs_zc_header *)(((char *)req_lib_cpg_mcast) - sizeof (struct coroipcs_zc_header));
  896. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  897. req_coroipcc_zc_execute.header.id = MESSAGE_REQ_CPG_ZC_EXECUTE;
  898. req_coroipcc_zc_execute.server_address = hdr->server_address;
  899. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  900. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  901. error = coroipcc_msg_send_reply_receive (
  902. cpg_inst->c,
  903. &iovec,
  904. 1,
  905. &res_lib_cpg_mcast,
  906. sizeof(res_lib_cpg_mcast));
  907. if (error != CS_OK) {
  908. goto error_exit;
  909. }
  910. error = res_lib_cpg_mcast.header.error;
  911. error_exit:
  912. hdb_handle_put (&cpg_handle_t_db, handle);
  913. return (error);
  914. }
  915. static cs_error_t send_fragments (
  916. struct cpg_inst *cpg_inst,
  917. cpg_guarantee_t guarantee,
  918. size_t msg_len,
  919. const struct iovec *iovec,
  920. unsigned int iov_len)
  921. {
  922. int i;
  923. cs_error_t error = CS_OK;
  924. struct iovec iov[2];
  925. struct req_lib_cpg_partial_mcast req_lib_cpg_mcast;
  926. struct res_lib_cpg_partial_send res_lib_cpg_partial_send;
  927. size_t sent = 0;
  928. size_t iov_sent = 0;
  929. int retry_count;
  930. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_PARTIAL_MCAST;
  931. req_lib_cpg_mcast.guarantee = guarantee;
  932. req_lib_cpg_mcast.msglen = msg_len;
  933. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  934. iov[0].iov_len = sizeof (struct req_lib_cpg_partial_mcast);
  935. i=0;
  936. iov_sent = 0 ;
  937. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  938. while (error == CS_OK && sent < msg_len) {
  939. retry_count = 0;
  940. if ( (iovec[i].iov_len - iov_sent) > cpg_inst->max_msg_size) {
  941. iov[1].iov_len = cpg_inst->max_msg_size;
  942. }
  943. else {
  944. iov[1].iov_len = iovec[i].iov_len - iov_sent;
  945. }
  946. if (sent == 0) {
  947. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_FIRST;
  948. }
  949. else if ((sent + iov[1].iov_len) == msg_len) {
  950. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_LAST;
  951. }
  952. else {
  953. req_lib_cpg_mcast.type = LIBCPG_PARTIAL_CONTINUED;
  954. }
  955. req_lib_cpg_mcast.fraglen = iov[1].iov_len;
  956. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_partial_mcast) + iov[1].iov_len;
  957. iov[1].iov_base = (char *)iovec[i].iov_base + iov_sent;
  958. resend:
  959. error = coroipcc_msg_send_reply_receive (cpg_inst->c, iov, 2,
  960. &res_lib_cpg_partial_send,
  961. sizeof (res_lib_cpg_partial_send));
  962. if (error == CS_ERR_TRY_AGAIN) {
  963. fprintf(stderr, "sleep. counter=%d\n", retry_count);
  964. if (++retry_count > MAX_RETRIES) {
  965. goto error_exit;
  966. }
  967. usleep(10000);
  968. goto resend;
  969. }
  970. iov_sent += iov[1].iov_len;
  971. sent += iov[1].iov_len;
  972. /* Next iovec */
  973. if (iov_sent >= iovec[i].iov_len) {
  974. i++;
  975. iov_sent = 0;
  976. }
  977. error = res_lib_cpg_partial_send.header.error;
  978. }
  979. error_exit:
  980. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  981. return error;
  982. }
  983. cs_error_t cpg_mcast_joined (
  984. cpg_handle_t handle,
  985. cpg_guarantee_t guarantee,
  986. const struct iovec *iovec,
  987. unsigned int iov_len)
  988. {
  989. int i;
  990. cs_error_t error;
  991. struct cpg_inst *cpg_inst;
  992. struct iovec iov[64];
  993. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  994. size_t msg_len = 0;
  995. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  996. if (error != CS_OK) {
  997. return (error);
  998. }
  999. for (i = 0; i < iov_len; i++ ) {
  1000. msg_len += iovec[i].iov_len;
  1001. }
  1002. if (msg_len > cpg_inst->max_msg_size) {
  1003. error = send_fragments(cpg_inst, guarantee, msg_len, iovec, iov_len);
  1004. goto error_exit;
  1005. }
  1006. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  1007. msg_len;
  1008. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  1009. req_lib_cpg_mcast.guarantee = guarantee;
  1010. req_lib_cpg_mcast.msglen = msg_len;
  1011. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  1012. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  1013. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  1014. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  1015. error = qb_to_cs_error(qb_ipcc_sendv(cpg_inst->c, iov, iov_len + 1));
  1016. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  1017. error_exit:
  1018. hdb_handle_put (&cpg_handle_t_db, handle);
  1019. return (error);
  1020. }
  1021. cs_error_t cpg_iteration_initialize(
  1022. cpg_handle_t handle,
  1023. cpg_iteration_type_t iteration_type,
  1024. const struct cpg_name *group,
  1025. cpg_iteration_handle_t *cpg_iteration_handle)
  1026. {
  1027. cs_error_t error;
  1028. struct iovec iov;
  1029. struct cpg_inst *cpg_inst;
  1030. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1031. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  1032. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  1033. if (group && group->length > CPG_MAX_NAME_LENGTH) {
  1034. return (CS_ERR_NAME_TOO_LONG);
  1035. }
  1036. if (cpg_iteration_handle == NULL) {
  1037. return (CS_ERR_INVALID_PARAM);
  1038. }
  1039. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  1040. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  1041. return (CS_ERR_INVALID_PARAM);
  1042. }
  1043. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  1044. iteration_type != CPG_ITERATION_ALL) {
  1045. return (CS_ERR_INVALID_PARAM);
  1046. }
  1047. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  1048. if (error != CS_OK) {
  1049. return (error);
  1050. }
  1051. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  1052. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  1053. if (error != CS_OK) {
  1054. goto error_put_cpg_db;
  1055. }
  1056. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  1057. (void *)&cpg_iteration_instance));
  1058. if (error != CS_OK) {
  1059. goto error_destroy;
  1060. }
  1061. cpg_iteration_instance->conn = cpg_inst->c;
  1062. qb_list_init (&cpg_iteration_instance->list);
  1063. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  1064. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  1065. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  1066. if (group) {
  1067. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  1068. }
  1069. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  1070. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  1071. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  1072. &iov,
  1073. 1,
  1074. &res_lib_cpg_iterationinitialize,
  1075. sizeof (struct res_lib_cpg_iterationinitialize));
  1076. if (error != CS_OK) {
  1077. goto error_put_destroy;
  1078. }
  1079. cpg_iteration_instance->executive_iteration_handle =
  1080. res_lib_cpg_iterationinitialize.iteration_handle;
  1081. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  1082. qb_list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  1083. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1084. hdb_handle_put (&cpg_handle_t_db, handle);
  1085. return (res_lib_cpg_iterationinitialize.header.error);
  1086. error_put_destroy:
  1087. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1088. error_destroy:
  1089. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  1090. error_put_cpg_db:
  1091. hdb_handle_put (&cpg_handle_t_db, handle);
  1092. return (error);
  1093. }
  1094. cs_error_t cpg_iteration_next(
  1095. cpg_iteration_handle_t handle,
  1096. struct cpg_iteration_description_t *description)
  1097. {
  1098. cs_error_t error;
  1099. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1100. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  1101. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  1102. if (description == NULL) {
  1103. return CS_ERR_INVALID_PARAM;
  1104. }
  1105. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  1106. (void *)&cpg_iteration_instance));
  1107. if (error != CS_OK) {
  1108. goto error_exit;
  1109. }
  1110. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  1111. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  1112. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  1113. error = qb_to_cs_error (qb_ipcc_send (cpg_iteration_instance->conn,
  1114. &req_lib_cpg_iterationnext,
  1115. req_lib_cpg_iterationnext.header.size));
  1116. if (error != CS_OK) {
  1117. goto error_put;
  1118. }
  1119. error = qb_to_cs_error (qb_ipcc_recv (cpg_iteration_instance->conn,
  1120. &res_lib_cpg_iterationnext,
  1121. sizeof(struct res_lib_cpg_iterationnext), -1));
  1122. if (error != CS_OK) {
  1123. goto error_put;
  1124. }
  1125. marshall_from_mar_cpg_iteration_description_t(
  1126. description,
  1127. &res_lib_cpg_iterationnext.description);
  1128. error = res_lib_cpg_iterationnext.header.error;
  1129. error_put:
  1130. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  1131. error_exit:
  1132. return (error);
  1133. }
  1134. cs_error_t cpg_iteration_finalize (
  1135. cpg_iteration_handle_t handle)
  1136. {
  1137. cs_error_t error;
  1138. struct iovec iov;
  1139. struct cpg_iteration_instance_t *cpg_iteration_instance;
  1140. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  1141. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  1142. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  1143. (void *)&cpg_iteration_instance));
  1144. if (error != CS_OK) {
  1145. goto error_exit;
  1146. }
  1147. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  1148. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  1149. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  1150. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  1151. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  1152. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn,
  1153. &iov,
  1154. 1,
  1155. &res_lib_cpg_iterationfinalize,
  1156. sizeof (struct req_lib_cpg_iterationfinalize));
  1157. if (error != CS_OK) {
  1158. goto error_put;
  1159. }
  1160. cpg_iteration_instance_finalize (cpg_iteration_instance);
  1161. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  1162. return (res_lib_cpg_iterationfinalize.header.error);
  1163. error_put:
  1164. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  1165. error_exit:
  1166. return (error);
  1167. }
  1168. /** @} */