cpg.c 31 KB

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