cpg.c 30 KB

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