evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. * Copyright (c) 2004-2005 MontaVista Software, Inc.
  4. * Copyright (c) 2006-2007, 2009 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * Provides an extended virtual synchrony API using the corosync executive
  38. */
  39. #include <config.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/coroipc_types.h>
  48. #include <corosync/coroipcc.h>
  49. #include <corosync/corodefs.h>
  50. #include <corosync/hdb.h>
  51. #include <corosync/evs.h>
  52. #include <corosync/ipc_evs.h>
  53. #include "util.h"
  54. #undef MIN
  55. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  56. struct evs_inst {
  57. hdb_handle_t handle;
  58. int finalize;
  59. evs_callbacks_t callbacks;
  60. void *context;
  61. };
  62. DECLARE_HDB_DATABASE (evs_handle_t_db,NULL);
  63. /*
  64. * Clean up function for an evt instance (saEvtInitialize) handle
  65. */
  66. /**
  67. * @defgroup evs_coroipcc The extended virtual synchrony passthrough API
  68. * @ingroup coroipcc
  69. *
  70. * @{
  71. */
  72. /**
  73. * test
  74. * @param handle The handle of evs initialize
  75. * @param callbacks The callbacks for evs_initialize
  76. * @returns EVS_OK
  77. */
  78. evs_error_t evs_initialize (
  79. evs_handle_t *handle,
  80. evs_callbacks_t *callbacks)
  81. {
  82. cs_error_t error;
  83. struct evs_inst *evs_inst;
  84. error = hdb_error_to_cs(hdb_handle_create (&evs_handle_t_db, sizeof (struct evs_inst), handle));
  85. if (error != CS_OK) {
  86. goto error_no_destroy;
  87. }
  88. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, *handle, (void *)&evs_inst));
  89. if (error != CS_OK) {
  90. goto error_destroy;
  91. }
  92. error = coroipcc_service_connect (
  93. COROSYNC_SOCKET_NAME,
  94. EVS_SERVICE,
  95. IPC_REQUEST_SIZE,
  96. IPC_RESPONSE_SIZE,
  97. IPC_DISPATCH_SIZE,
  98. &evs_inst->handle);
  99. if (error != EVS_OK) {
  100. goto error_put_destroy;
  101. }
  102. if (callbacks) {
  103. memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
  104. }
  105. hdb_handle_put (&evs_handle_t_db, *handle);
  106. return (CS_OK);
  107. error_put_destroy:
  108. hdb_handle_put (&evs_handle_t_db, *handle);
  109. error_destroy:
  110. hdb_handle_destroy (&evs_handle_t_db, *handle);
  111. error_no_destroy:
  112. return (error);
  113. }
  114. evs_error_t evs_finalize (
  115. evs_handle_t handle)
  116. {
  117. struct evs_inst *evs_inst;
  118. cs_error_t error;
  119. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  120. if (error != CS_OK) {
  121. return (error);
  122. }
  123. /*
  124. * Another thread has already started finalizing
  125. */
  126. if (evs_inst->finalize) {
  127. hdb_handle_put (&evs_handle_t_db, handle);
  128. return (EVS_ERR_BAD_HANDLE);
  129. }
  130. evs_inst->finalize = 1;
  131. coroipcc_service_disconnect (evs_inst->handle);
  132. hdb_handle_destroy (&evs_handle_t_db, handle);
  133. hdb_handle_put (&evs_handle_t_db, handle);
  134. return (EVS_OK);
  135. }
  136. evs_error_t evs_fd_get (
  137. evs_handle_t handle,
  138. int *fd)
  139. {
  140. cs_error_t error;
  141. struct evs_inst *evs_inst;
  142. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  143. if (error != CS_OK) {
  144. return (error);
  145. }
  146. coroipcc_fd_get (evs_inst->handle, fd);
  147. hdb_handle_put (&evs_handle_t_db, handle);
  148. return (CS_OK);
  149. }
  150. evs_error_t evs_context_get (
  151. evs_handle_t handle,
  152. void **context)
  153. {
  154. cs_error_t error;
  155. struct evs_inst *evs_inst;
  156. error = hdb_error_to_cs (hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  157. if (error != CS_OK) {
  158. return (error);
  159. }
  160. *context = evs_inst->context;
  161. hdb_handle_put (&evs_handle_t_db, handle);
  162. return (CS_OK);
  163. }
  164. cs_error_t evs_context_set (
  165. evs_handle_t handle,
  166. void *context)
  167. {
  168. cs_error_t error;
  169. struct evs_inst *evs_inst;
  170. error = hdb_error_to_cs (hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  171. if (error != CS_OK) {
  172. return (error);
  173. }
  174. evs_inst->context = context;
  175. hdb_handle_put (&evs_handle_t_db, handle);
  176. return (CS_OK);
  177. }
  178. evs_error_t evs_dispatch (
  179. evs_handle_t handle,
  180. cs_dispatch_flags_t dispatch_types)
  181. {
  182. int timeout = -1;
  183. cs_error_t error;
  184. int cont = 1; /* always continue do loop except when set to 0 */
  185. struct evs_inst *evs_inst;
  186. struct res_evs_confchg_callback *res_evs_confchg_callback;
  187. struct res_evs_deliver_callback *res_evs_deliver_callback;
  188. evs_callbacks_t callbacks;
  189. coroipc_response_header_t *dispatch_data;
  190. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  191. if (error != CS_OK) {
  192. return (error);
  193. }
  194. /*
  195. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  196. * wait indefinately for CS_DISPATCH_BLOCKING
  197. */
  198. if (dispatch_types == EVS_DISPATCH_ALL) {
  199. timeout = 0;
  200. }
  201. do {
  202. error = coroipcc_dispatch_get (
  203. evs_inst->handle,
  204. (void **)&dispatch_data,
  205. timeout);
  206. if (error == CS_ERR_BAD_HANDLE) {
  207. error = CS_OK;
  208. goto error_put;
  209. }
  210. if (error == CS_ERR_TRY_AGAIN) {
  211. error = CS_OK;
  212. if (dispatch_types == CPG_DISPATCH_ALL) {
  213. break; /* exit do while cont is 1 loop */
  214. } else {
  215. continue; /* next poll */
  216. }
  217. }
  218. if (error != CS_OK) {
  219. goto error_put;
  220. }
  221. /*
  222. * Make copy of callbacks, message data, unlock instance, and call callback
  223. * A risk of this dispatch method is that the callback routines may
  224. * operate at the same time that evsFinalize has been called.
  225. */
  226. memcpy (&callbacks, &evs_inst->callbacks, sizeof (evs_callbacks_t));
  227. /*
  228. * Dispatch incoming message
  229. */
  230. switch (dispatch_data->id) {
  231. case MESSAGE_RES_EVS_DELIVER_CALLBACK:
  232. if (callbacks.evs_deliver_fn == NULL) {
  233. break;
  234. }
  235. res_evs_deliver_callback = (struct res_evs_deliver_callback *)dispatch_data;
  236. callbacks.evs_deliver_fn (
  237. handle,
  238. res_evs_deliver_callback->local_nodeid,
  239. &res_evs_deliver_callback->msg,
  240. res_evs_deliver_callback->msglen);
  241. break;
  242. case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
  243. if (callbacks.evs_confchg_fn == NULL) {
  244. break;
  245. }
  246. res_evs_confchg_callback = (struct res_evs_confchg_callback *)dispatch_data;
  247. callbacks.evs_confchg_fn (
  248. handle,
  249. res_evs_confchg_callback->member_list,
  250. res_evs_confchg_callback->member_list_entries,
  251. res_evs_confchg_callback->left_list,
  252. res_evs_confchg_callback->left_list_entries,
  253. res_evs_confchg_callback->joined_list,
  254. res_evs_confchg_callback->joined_list_entries,
  255. NULL);
  256. break;
  257. default:
  258. error = coroipcc_dispatch_put (evs_inst->handle);
  259. if (error == CS_OK) {
  260. error = CS_ERR_LIBRARY;
  261. }
  262. goto error_put;
  263. break;
  264. }
  265. error = coroipcc_dispatch_put (evs_inst->handle);
  266. if (error == CS_ERR_IN_SHUTDOWN) {
  267. /*
  268. * Mask error.
  269. */
  270. error = CS_OK;
  271. goto error_put;
  272. }
  273. if (error != CS_OK) {
  274. goto error_put;
  275. }
  276. /*
  277. * Determine if more messages should be processed
  278. */
  279. if (dispatch_types == CS_DISPATCH_ONE) {
  280. cont = 0;
  281. }
  282. } while (cont);
  283. error_put:
  284. hdb_handle_put (&evs_handle_t_db, handle);
  285. return (error);
  286. }
  287. evs_error_t evs_join (
  288. evs_handle_t handle,
  289. const struct evs_group *groups,
  290. size_t group_entries)
  291. {
  292. evs_error_t error;
  293. struct evs_inst *evs_inst;
  294. struct iovec iov[2];
  295. struct req_lib_evs_join req_lib_evs_join;
  296. struct res_lib_evs_join res_lib_evs_join;
  297. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  298. if (error != EVS_OK) {
  299. return (error);
  300. }
  301. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  302. (group_entries * sizeof (struct evs_group));
  303. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  304. req_lib_evs_join.group_entries = group_entries;
  305. iov[0].iov_base = (void *)&req_lib_evs_join;
  306. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  307. iov[1].iov_base = (void*) groups; /* cast away const */
  308. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  309. error = coroipcc_msg_send_reply_receive (evs_inst->handle, iov, 2,
  310. &res_lib_evs_join, sizeof (struct res_lib_evs_join));
  311. if (error != CS_OK) {
  312. goto error_exit;
  313. }
  314. error = res_lib_evs_join.header.error;
  315. error_exit:
  316. hdb_handle_put (&evs_handle_t_db, handle);
  317. return (error);
  318. }
  319. evs_error_t evs_leave (
  320. evs_handle_t handle,
  321. const struct evs_group *groups,
  322. size_t group_entries)
  323. {
  324. evs_error_t error;
  325. struct evs_inst *evs_inst;
  326. struct iovec iov[2];
  327. struct req_lib_evs_leave req_lib_evs_leave;
  328. struct res_lib_evs_leave res_lib_evs_leave;
  329. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  330. if (error != CS_OK) {
  331. return (error);
  332. }
  333. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  334. (group_entries * sizeof (struct evs_group));
  335. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  336. req_lib_evs_leave.group_entries = group_entries;
  337. iov[0].iov_base = (void *)&req_lib_evs_leave;
  338. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  339. iov[1].iov_base = (void *) groups; /* cast away const */
  340. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  341. error = coroipcc_msg_send_reply_receive (evs_inst->handle, iov, 2,
  342. &res_lib_evs_leave, sizeof (struct res_lib_evs_leave));
  343. if (error != CS_OK) {
  344. goto error_exit;
  345. }
  346. error = res_lib_evs_leave.header.error;
  347. error_exit:
  348. hdb_handle_put (&evs_handle_t_db, handle);
  349. return (error);
  350. }
  351. evs_error_t evs_mcast_joined (
  352. evs_handle_t handle,
  353. evs_guarantee_t guarantee,
  354. const struct iovec *iovec,
  355. unsigned int iov_len)
  356. {
  357. int i;
  358. evs_error_t error;
  359. struct evs_inst *evs_inst;
  360. struct iovec iov[64];
  361. struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
  362. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  363. size_t msg_len = 0;
  364. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  365. if (error != CS_OK) {
  366. return (error);
  367. }
  368. for (i = 0; i < iov_len; i++ ) {
  369. msg_len += iovec[i].iov_len;
  370. }
  371. req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
  372. msg_len;
  373. req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
  374. req_lib_evs_mcast_joined.guarantee = guarantee;
  375. req_lib_evs_mcast_joined.msg_len = msg_len;
  376. iov[0].iov_base = (void *)&req_lib_evs_mcast_joined;
  377. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
  378. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  379. error = coroipcc_msg_send_reply_receive (evs_inst->handle, iov,
  380. iov_len + 1,
  381. &res_lib_evs_mcast_joined,
  382. sizeof (struct res_lib_evs_mcast_joined));
  383. if (error != CS_OK) {
  384. goto error_exit;
  385. }
  386. error = res_lib_evs_mcast_joined.header.error;
  387. error_exit:
  388. hdb_handle_put (&evs_handle_t_db, handle);
  389. return (error);
  390. }
  391. evs_error_t evs_mcast_groups (
  392. evs_handle_t handle,
  393. evs_guarantee_t guarantee,
  394. const struct evs_group *groups,
  395. size_t group_entries,
  396. const struct iovec *iovec,
  397. unsigned int iov_len)
  398. {
  399. int i;
  400. evs_error_t error;
  401. struct evs_inst *evs_inst;
  402. struct iovec iov[64]; /* FIXME: what if iov_len > 62 ? use malloc */
  403. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  404. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  405. size_t msg_len = 0;
  406. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  407. if (error != CS_OK) {
  408. return (error);
  409. }
  410. for (i = 0; i < iov_len; i++) {
  411. msg_len += iovec[i].iov_len;
  412. }
  413. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  414. (group_entries * sizeof (struct evs_group)) + msg_len;
  415. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  416. req_lib_evs_mcast_groups.guarantee = guarantee;
  417. req_lib_evs_mcast_groups.msg_len = msg_len;
  418. req_lib_evs_mcast_groups.group_entries = group_entries;
  419. iov[0].iov_base = (void *)&req_lib_evs_mcast_groups;
  420. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  421. iov[1].iov_base = (void *) groups; /* cast away const */
  422. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  423. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  424. error = coroipcc_msg_send_reply_receive (evs_inst->handle, iov,
  425. iov_len + 2,
  426. &res_lib_evs_mcast_groups,
  427. sizeof (struct res_lib_evs_mcast_groups));
  428. if (error != CS_OK) {
  429. goto error_exit;
  430. }
  431. error = res_lib_evs_mcast_groups.header.error;
  432. error_exit:
  433. hdb_handle_put (&evs_handle_t_db, handle);
  434. return (error);
  435. }
  436. evs_error_t evs_membership_get (
  437. evs_handle_t handle,
  438. unsigned int *local_nodeid,
  439. unsigned int *member_list,
  440. size_t *member_list_entries)
  441. {
  442. evs_error_t error;
  443. struct evs_inst *evs_inst;
  444. struct iovec iov;
  445. struct req_lib_evs_membership_get req_lib_evs_membership_get;
  446. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  447. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  448. if (error != CS_OK) {
  449. return (error);
  450. }
  451. req_lib_evs_membership_get.header.size = sizeof (struct req_lib_evs_membership_get);
  452. req_lib_evs_membership_get.header.id = MESSAGE_REQ_EVS_MEMBERSHIP_GET;
  453. iov.iov_base = (void *)&req_lib_evs_membership_get;
  454. iov.iov_len = sizeof (struct req_lib_evs_membership_get);
  455. error = coroipcc_msg_send_reply_receive (evs_inst->handle,
  456. &iov,
  457. 1,
  458. &res_lib_evs_membership_get,
  459. sizeof (struct res_lib_evs_membership_get));
  460. if (error != CS_OK) {
  461. goto error_exit;
  462. }
  463. error = res_lib_evs_membership_get.header.error;
  464. /*
  465. * Copy results to caller
  466. */
  467. if (local_nodeid) {
  468. *local_nodeid = res_lib_evs_membership_get.local_nodeid;
  469. }
  470. *member_list_entries = MIN (*member_list_entries,
  471. res_lib_evs_membership_get.member_list_entries);
  472. if (member_list) {
  473. memcpy (member_list, &res_lib_evs_membership_get.member_list,
  474. *member_list_entries * sizeof (struct in_addr));
  475. }
  476. error_exit:
  477. hdb_handle_put (&evs_handle_t_db, handle);
  478. return (error);
  479. }
  480. /** @} */