cpg.c 30 KB

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