cpg.c 38 KB

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