4
0

cpg.c 31 KB

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