cpg.c 36 KB

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