cpg.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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;
  606. int32_t res;
  607. char *buffer;
  608. int32_t i;
  609. size_t written;
  610. size_t page_size;
  611. snprintf (path, PATH_MAX, "/dev/shm/%s", file);
  612. fd = mkstemp (path);
  613. if (fd == -1) {
  614. snprintf (path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
  615. fd = mkstemp (path);
  616. if (fd == -1) {
  617. return (-1);
  618. }
  619. }
  620. res = ftruncate (fd, bytes);
  621. if (res == -1) {
  622. goto error_close_unlink;
  623. }
  624. page_size = (size_t)sysconf(_SC_PAGESIZE);
  625. buffer = malloc (page_size);
  626. if (buffer == NULL) {
  627. goto error_close_unlink;
  628. }
  629. memset (buffer, 0, page_size);
  630. for (i = 0; i < (bytes / page_size); i++) {
  631. retry_write:
  632. written = write (fd, buffer, page_size);
  633. if (written == -1 && errno == EINTR) {
  634. goto retry_write;
  635. }
  636. if (written != page_size) {
  637. free (buffer);
  638. goto error_close_unlink;
  639. }
  640. }
  641. free (buffer);
  642. addr = mmap (NULL, bytes, PROT_READ | PROT_WRITE,
  643. MAP_SHARED, fd, 0);
  644. if (addr == MAP_FAILED) {
  645. goto error_close_unlink;
  646. }
  647. #ifdef MADV_NOSYNC
  648. madvise(addr, bytes, MADV_NOSYNC);
  649. #endif
  650. res = close (fd);
  651. if (res) {
  652. return (-1);
  653. }
  654. *buf = addr;
  655. return 0;
  656. error_close_unlink:
  657. close (fd);
  658. unlink(path);
  659. return -1;
  660. }
  661. cs_error_t cpg_zcb_alloc (
  662. cpg_handle_t handle,
  663. size_t size,
  664. void **buffer)
  665. {
  666. void *buf = NULL;
  667. char path[PATH_MAX];
  668. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  669. struct qb_ipc_response_header res_coroipcs_zc_alloc;
  670. size_t map_size;
  671. struct iovec iovec;
  672. struct coroipcs_zc_header *hdr;
  673. cs_error_t error;
  674. struct cpg_inst *cpg_inst;
  675. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  676. if (error != CS_OK) {
  677. return (error);
  678. }
  679. map_size = size + sizeof (struct req_lib_cpg_mcast) + sizeof (struct coroipcs_zc_header);
  680. assert(memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size) != -1);
  681. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  682. req_coroipcc_zc_alloc.header.id = MESSAGE_REQ_CPG_ZC_ALLOC;
  683. req_coroipcc_zc_alloc.map_size = map_size;
  684. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  685. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  686. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  687. error = coroipcc_msg_send_reply_receive (
  688. cpg_inst->c,
  689. &iovec,
  690. 1,
  691. &res_coroipcs_zc_alloc,
  692. sizeof (struct qb_ipc_response_header));
  693. hdr = (struct coroipcs_zc_header *)buf;
  694. hdr->map_size = map_size;
  695. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  696. hdb_handle_put (&cpg_handle_t_db, handle);
  697. *buffer = ((char *)*buffer) + sizeof (struct req_lib_cpg_mcast);
  698. return (error);
  699. }
  700. cs_error_t cpg_zcb_free (
  701. cpg_handle_t handle,
  702. void *buffer)
  703. {
  704. cs_error_t error;
  705. struct cpg_inst *cpg_inst;
  706. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  707. struct qb_ipc_response_header res_coroipcs_zc_free;
  708. struct iovec iovec;
  709. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  710. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  711. if (error != CS_OK) {
  712. return (error);
  713. }
  714. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  715. req_coroipcc_zc_free.header.id = MESSAGE_REQ_CPG_ZC_FREE;
  716. req_coroipcc_zc_free.map_size = header->map_size;
  717. req_coroipcc_zc_free.server_address = header->server_address;
  718. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  719. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  720. error = coroipcc_msg_send_reply_receive (
  721. cpg_inst->c,
  722. &iovec,
  723. 1,
  724. &res_coroipcs_zc_free,
  725. sizeof (struct qb_ipc_response_header));
  726. munmap ((void *)header, header->map_size);
  727. hdb_handle_put (&cpg_handle_t_db, handle);
  728. return (error);
  729. }
  730. cs_error_t cpg_zcb_mcast_joined (
  731. cpg_handle_t handle,
  732. cpg_guarantee_t guarantee,
  733. void *msg,
  734. size_t msg_len)
  735. {
  736. cs_error_t error;
  737. struct cpg_inst *cpg_inst;
  738. struct req_lib_cpg_mcast *req_lib_cpg_mcast;
  739. struct res_lib_cpg_mcast res_lib_cpg_mcast;
  740. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  741. struct coroipcs_zc_header *hdr;
  742. struct iovec iovec;
  743. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  744. if (error != CS_OK) {
  745. return (error);
  746. }
  747. req_lib_cpg_mcast = (struct req_lib_cpg_mcast *)(((char *)msg) - sizeof (struct req_lib_cpg_mcast));
  748. req_lib_cpg_mcast->header.size = sizeof (struct req_lib_cpg_mcast) +
  749. msg_len;
  750. req_lib_cpg_mcast->header.id = MESSAGE_REQ_CPG_MCAST;
  751. req_lib_cpg_mcast->guarantee = guarantee;
  752. req_lib_cpg_mcast->msglen = msg_len;
  753. hdr = (struct coroipcs_zc_header *)(((char *)req_lib_cpg_mcast) - sizeof (struct coroipcs_zc_header));
  754. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  755. req_coroipcc_zc_execute.header.id = MESSAGE_REQ_CPG_ZC_EXECUTE;
  756. req_coroipcc_zc_execute.server_address = hdr->server_address;
  757. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  758. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  759. error = coroipcc_msg_send_reply_receive (
  760. cpg_inst->c,
  761. &iovec,
  762. 1,
  763. &res_lib_cpg_mcast,
  764. sizeof(res_lib_cpg_mcast));
  765. if (error != CS_OK) {
  766. goto error_exit;
  767. }
  768. error = res_lib_cpg_mcast.header.error;
  769. error_exit:
  770. hdb_handle_put (&cpg_handle_t_db, handle);
  771. return (error);
  772. }
  773. cs_error_t cpg_mcast_joined (
  774. cpg_handle_t handle,
  775. cpg_guarantee_t guarantee,
  776. const struct iovec *iovec,
  777. unsigned int iov_len)
  778. {
  779. int i;
  780. cs_error_t error;
  781. struct cpg_inst *cpg_inst;
  782. struct iovec iov[64];
  783. struct req_lib_cpg_mcast req_lib_cpg_mcast;
  784. size_t msg_len = 0;
  785. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  786. if (error != CS_OK) {
  787. return (error);
  788. }
  789. for (i = 0; i < iov_len; i++ ) {
  790. msg_len += iovec[i].iov_len;
  791. }
  792. req_lib_cpg_mcast.header.size = sizeof (struct req_lib_cpg_mcast) +
  793. msg_len;
  794. req_lib_cpg_mcast.header.id = MESSAGE_REQ_CPG_MCAST;
  795. req_lib_cpg_mcast.guarantee = guarantee;
  796. req_lib_cpg_mcast.msglen = msg_len;
  797. iov[0].iov_base = (void *)&req_lib_cpg_mcast;
  798. iov[0].iov_len = sizeof (struct req_lib_cpg_mcast);
  799. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  800. qb_ipcc_fc_enable_max_set(cpg_inst->c, 2);
  801. error = qb_to_cs_error(qb_ipcc_sendv(cpg_inst->c, iov, iov_len + 1));
  802. qb_ipcc_fc_enable_max_set(cpg_inst->c, 1);
  803. hdb_handle_put (&cpg_handle_t_db, handle);
  804. return (error);
  805. }
  806. cs_error_t cpg_iteration_initialize(
  807. cpg_handle_t handle,
  808. cpg_iteration_type_t iteration_type,
  809. const struct cpg_name *group,
  810. cpg_iteration_handle_t *cpg_iteration_handle)
  811. {
  812. cs_error_t error;
  813. struct iovec iov;
  814. struct cpg_inst *cpg_inst;
  815. struct cpg_iteration_instance_t *cpg_iteration_instance;
  816. struct req_lib_cpg_iterationinitialize req_lib_cpg_iterationinitialize;
  817. struct res_lib_cpg_iterationinitialize res_lib_cpg_iterationinitialize;
  818. if (group && group->length > CPG_MAX_NAME_LENGTH) {
  819. return (CS_ERR_NAME_TOO_LONG);
  820. }
  821. if (cpg_iteration_handle == NULL) {
  822. return (CS_ERR_INVALID_PARAM);
  823. }
  824. if ((iteration_type == CPG_ITERATION_ONE_GROUP && group == NULL) ||
  825. (iteration_type != CPG_ITERATION_ONE_GROUP && group != NULL)) {
  826. return (CS_ERR_INVALID_PARAM);
  827. }
  828. if (iteration_type != CPG_ITERATION_NAME_ONLY && iteration_type != CPG_ITERATION_ONE_GROUP &&
  829. iteration_type != CPG_ITERATION_ALL) {
  830. return (CS_ERR_INVALID_PARAM);
  831. }
  832. error = hdb_error_to_cs (hdb_handle_get (&cpg_handle_t_db, handle, (void *)&cpg_inst));
  833. if (error != CS_OK) {
  834. return (error);
  835. }
  836. error = hdb_error_to_cs (hdb_handle_create (&cpg_iteration_handle_t_db,
  837. sizeof (struct cpg_iteration_instance_t), cpg_iteration_handle));
  838. if (error != CS_OK) {
  839. goto error_put_cpg_db;
  840. }
  841. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, *cpg_iteration_handle,
  842. (void *)&cpg_iteration_instance));
  843. if (error != CS_OK) {
  844. goto error_destroy;
  845. }
  846. cpg_iteration_instance->conn = cpg_inst->c;
  847. list_init (&cpg_iteration_instance->list);
  848. req_lib_cpg_iterationinitialize.header.size = sizeof (struct req_lib_cpg_iterationinitialize);
  849. req_lib_cpg_iterationinitialize.header.id = MESSAGE_REQ_CPG_ITERATIONINITIALIZE;
  850. req_lib_cpg_iterationinitialize.iteration_type = iteration_type;
  851. if (group) {
  852. marshall_to_mar_cpg_name_t (&req_lib_cpg_iterationinitialize.group_name, group);
  853. }
  854. iov.iov_base = (void *)&req_lib_cpg_iterationinitialize;
  855. iov.iov_len = sizeof (struct req_lib_cpg_iterationinitialize);
  856. error = coroipcc_msg_send_reply_receive (cpg_inst->c,
  857. &iov,
  858. 1,
  859. &res_lib_cpg_iterationinitialize,
  860. sizeof (struct res_lib_cpg_iterationinitialize));
  861. if (error != CS_OK) {
  862. goto error_put_destroy;
  863. }
  864. cpg_iteration_instance->executive_iteration_handle =
  865. res_lib_cpg_iterationinitialize.iteration_handle;
  866. cpg_iteration_instance->cpg_iteration_handle = *cpg_iteration_handle;
  867. list_add (&cpg_iteration_instance->list, &cpg_inst->iteration_list_head);
  868. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  869. hdb_handle_put (&cpg_handle_t_db, handle);
  870. return (res_lib_cpg_iterationinitialize.header.error);
  871. error_put_destroy:
  872. hdb_handle_put (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  873. error_destroy:
  874. hdb_handle_destroy (&cpg_iteration_handle_t_db, *cpg_iteration_handle);
  875. error_put_cpg_db:
  876. hdb_handle_put (&cpg_handle_t_db, handle);
  877. return (error);
  878. }
  879. cs_error_t cpg_iteration_next(
  880. cpg_iteration_handle_t handle,
  881. struct cpg_iteration_description_t *description)
  882. {
  883. cs_error_t error;
  884. struct cpg_iteration_instance_t *cpg_iteration_instance;
  885. struct req_lib_cpg_iterationnext req_lib_cpg_iterationnext;
  886. struct res_lib_cpg_iterationnext res_lib_cpg_iterationnext;
  887. if (description == NULL) {
  888. return CS_ERR_INVALID_PARAM;
  889. }
  890. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  891. (void *)&cpg_iteration_instance));
  892. if (error != CS_OK) {
  893. goto error_exit;
  894. }
  895. req_lib_cpg_iterationnext.header.size = sizeof (struct req_lib_cpg_iterationnext);
  896. req_lib_cpg_iterationnext.header.id = MESSAGE_REQ_CPG_ITERATIONNEXT;
  897. req_lib_cpg_iterationnext.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  898. error = qb_to_cs_error (qb_ipcc_send (cpg_iteration_instance->conn,
  899. &req_lib_cpg_iterationnext,
  900. req_lib_cpg_iterationnext.header.size));
  901. if (error != CS_OK) {
  902. goto error_put;
  903. }
  904. error = qb_to_cs_error (qb_ipcc_recv (cpg_iteration_instance->conn,
  905. &res_lib_cpg_iterationnext,
  906. sizeof(struct res_lib_cpg_iterationnext), -1));
  907. if (error != CS_OK) {
  908. goto error_put;
  909. }
  910. marshall_from_mar_cpg_iteration_description_t(
  911. description,
  912. &res_lib_cpg_iterationnext.description);
  913. error = res_lib_cpg_iterationnext.header.error;
  914. error_put:
  915. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  916. error_exit:
  917. return (error);
  918. }
  919. cs_error_t cpg_iteration_finalize (
  920. cpg_iteration_handle_t handle)
  921. {
  922. cs_error_t error;
  923. struct iovec iov;
  924. struct cpg_iteration_instance_t *cpg_iteration_instance;
  925. struct req_lib_cpg_iterationfinalize req_lib_cpg_iterationfinalize;
  926. struct res_lib_cpg_iterationfinalize res_lib_cpg_iterationfinalize;
  927. error = hdb_error_to_cs (hdb_handle_get (&cpg_iteration_handle_t_db, handle,
  928. (void *)&cpg_iteration_instance));
  929. if (error != CS_OK) {
  930. goto error_exit;
  931. }
  932. req_lib_cpg_iterationfinalize.header.size = sizeof (struct req_lib_cpg_iterationfinalize);
  933. req_lib_cpg_iterationfinalize.header.id = MESSAGE_REQ_CPG_ITERATIONFINALIZE;
  934. req_lib_cpg_iterationfinalize.iteration_handle = cpg_iteration_instance->executive_iteration_handle;
  935. iov.iov_base = (void *)&req_lib_cpg_iterationfinalize;
  936. iov.iov_len = sizeof (struct req_lib_cpg_iterationfinalize);
  937. error = coroipcc_msg_send_reply_receive (cpg_iteration_instance->conn,
  938. &iov,
  939. 1,
  940. &res_lib_cpg_iterationfinalize,
  941. sizeof (struct req_lib_cpg_iterationfinalize));
  942. if (error != CS_OK) {
  943. goto error_put;
  944. }
  945. cpg_iteration_instance_finalize (cpg_iteration_instance);
  946. hdb_handle_put (&cpg_iteration_handle_t_db, cpg_iteration_instance->cpg_iteration_handle);
  947. return (res_lib_cpg_iterationfinalize.header.error);
  948. error_put:
  949. hdb_handle_put (&cpg_iteration_handle_t_db, handle);
  950. error_exit:
  951. return (error);
  952. }
  953. /** @} */