cpg.c 36 KB

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