cpg.c 30 KB

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