evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 <pthread.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <errno.h>
  47. #include <corosync/totem/totem.h>
  48. #include <corosync/corotypes.h>
  49. #include <corosync/evs.h>
  50. #include <corosync/ipc_evs.h>
  51. #include <corosync/coroipcc.h>
  52. #undef MIN
  53. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  54. struct evs_inst {
  55. void *ipc_ctx;
  56. int finalize;
  57. evs_callbacks_t callbacks;
  58. pthread_mutex_t response_mutex;
  59. pthread_mutex_t dispatch_mutex;
  60. };
  61. static void evs_instance_destructor (void *instance);
  62. static struct saHandleDatabase evs_handle_t_db = {
  63. .handleCount = 0,
  64. .handles = 0,
  65. .mutex = PTHREAD_MUTEX_INITIALIZER,
  66. .handleInstanceDestructor = evs_instance_destructor
  67. };
  68. /*
  69. * Clean up function for an evt instance (saEvtInitialize) handle
  70. */
  71. static void evs_instance_destructor (void *instance)
  72. {
  73. struct evs_inst *evs_inst = instance;
  74. pthread_mutex_destroy (&evs_inst->response_mutex);
  75. pthread_mutex_destroy (&evs_inst->dispatch_mutex);
  76. }
  77. /**
  78. * @defgroup evs_coroipcc The extended virtual synchrony passthrough API
  79. * @ingroup coroipcc
  80. *
  81. * @{
  82. */
  83. /**
  84. * test
  85. * @param handle The handle of evs initialize
  86. * @param callbacks The callbacks for evs_initialize
  87. * @returns EVS_OK
  88. */
  89. evs_error_t evs_initialize (
  90. evs_handle_t *handle,
  91. evs_callbacks_t *callbacks)
  92. {
  93. cs_error_t error;
  94. struct evs_inst *evs_inst;
  95. error = saHandleCreate (&evs_handle_t_db, sizeof (struct evs_inst), handle);
  96. if (error != CS_OK) {
  97. goto error_no_destroy;
  98. }
  99. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  100. if (error != CS_OK) {
  101. goto error_destroy;
  102. }
  103. error = coroipcc_service_connect (IPC_SOCKET_NAME, EVS_SERVICE, &evs_inst->ipc_ctx);
  104. if (error != EVS_OK) {
  105. goto error_put_destroy;
  106. }
  107. memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
  108. pthread_mutex_init (&evs_inst->response_mutex, NULL);
  109. pthread_mutex_init (&evs_inst->dispatch_mutex, NULL);
  110. saHandleInstancePut (&evs_handle_t_db, *handle);
  111. return (CS_OK);
  112. error_put_destroy:
  113. saHandleInstancePut (&evs_handle_t_db, *handle);
  114. error_destroy:
  115. saHandleDestroy (&evs_handle_t_db, *handle);
  116. error_no_destroy:
  117. return (error);
  118. }
  119. evs_error_t evs_finalize (
  120. evs_handle_t handle)
  121. {
  122. struct evs_inst *evs_inst;
  123. cs_error_t error;
  124. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  125. if (error != CS_OK) {
  126. return (error);
  127. }
  128. pthread_mutex_lock (&evs_inst->response_mutex);
  129. /*
  130. * Another thread has already started finalizing
  131. */
  132. if (evs_inst->finalize) {
  133. pthread_mutex_unlock (&evs_inst->response_mutex);
  134. saHandleInstancePut (&evs_handle_t_db, handle);
  135. return (EVS_ERR_BAD_HANDLE);
  136. }
  137. evs_inst->finalize = 1;
  138. coroipcc_service_disconnect (evs_inst->ipc_ctx);
  139. pthread_mutex_unlock (&evs_inst->response_mutex);
  140. saHandleDestroy (&evs_handle_t_db, handle);
  141. saHandleInstancePut (&evs_handle_t_db, handle);
  142. return (EVS_OK);
  143. }
  144. evs_error_t evs_fd_get (
  145. evs_handle_t handle,
  146. int *fd)
  147. {
  148. cs_error_t error;
  149. struct evs_inst *evs_inst;
  150. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  151. if (error != CS_OK) {
  152. return (error);
  153. }
  154. *fd = coroipcc_fd_get (evs_inst->ipc_ctx);
  155. saHandleInstancePut (&evs_handle_t_db, handle);
  156. return (CS_OK);
  157. }
  158. evs_error_t evs_dispatch (
  159. evs_handle_t handle,
  160. cs_dispatch_flags_t dispatch_types)
  161. {
  162. int timeout = -1;
  163. cs_error_t error;
  164. int cont = 1; /* always continue do loop except when set to 0 */
  165. int dispatch_avail;
  166. struct evs_inst *evs_inst;
  167. struct res_evs_confchg_callback *res_evs_confchg_callback;
  168. struct res_evs_deliver_callback *res_evs_deliver_callback;
  169. evs_callbacks_t callbacks;
  170. mar_res_header_t *dispatch_data;
  171. int ignore_dispatch = 0;
  172. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  173. if (error != CS_OK) {
  174. return (error);
  175. }
  176. /*
  177. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  178. * wait indefinately for SA_DISPATCH_BLOCKING
  179. */
  180. if (dispatch_types == EVS_DISPATCH_ALL) {
  181. timeout = 0;
  182. }
  183. do {
  184. pthread_mutex_lock (&evs_inst->dispatch_mutex);
  185. dispatch_avail = coroipcc_dispatch_get (
  186. evs_inst->ipc_ctx,
  187. (void **)&dispatch_data,
  188. timeout);
  189. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  190. if (dispatch_avail == 0 && dispatch_types == EVS_DISPATCH_ALL) {
  191. break; /* exit do while cont is 1 loop */
  192. } else
  193. if (dispatch_avail == 0) {
  194. continue; /* next dispatch event */
  195. }
  196. if (dispatch_avail == -1) {
  197. if (evs_inst->finalize == 1) {
  198. error = CS_OK;
  199. } else {
  200. error = CS_ERR_LIBRARY;
  201. }
  202. goto error_put;
  203. }
  204. /*
  205. * Make copy of callbacks, message data, unlock instance, and call callback
  206. * A risk of this dispatch method is that the callback routines may
  207. * operate at the same time that evsFinalize has been called.
  208. */
  209. memcpy (&callbacks, &evs_inst->callbacks, sizeof (evs_callbacks_t));
  210. /*
  211. * Dispatch incoming message
  212. */
  213. switch (dispatch_data->id) {
  214. case MESSAGE_RES_EVS_DELIVER_CALLBACK:
  215. res_evs_deliver_callback = (struct res_evs_deliver_callback *)dispatch_data;
  216. callbacks.evs_deliver_fn (
  217. res_evs_deliver_callback->local_nodeid,
  218. &res_evs_deliver_callback->msg,
  219. res_evs_deliver_callback->msglen);
  220. break;
  221. case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
  222. res_evs_confchg_callback = (struct res_evs_confchg_callback *)dispatch_data;
  223. callbacks.evs_confchg_fn (
  224. res_evs_confchg_callback->member_list,
  225. res_evs_confchg_callback->member_list_entries,
  226. res_evs_confchg_callback->left_list,
  227. res_evs_confchg_callback->left_list_entries,
  228. res_evs_confchg_callback->joined_list,
  229. res_evs_confchg_callback->joined_list_entries);
  230. break;
  231. default:
  232. coroipcc_dispatch_put (evs_inst->ipc_ctx);
  233. error = CS_ERR_LIBRARY;
  234. goto error_put;
  235. break;
  236. }
  237. coroipcc_dispatch_put (evs_inst->ipc_ctx);
  238. /*
  239. * Determine if more messages should be processed
  240. * */
  241. switch (dispatch_types) {
  242. case EVS_DISPATCH_ONE:
  243. if (ignore_dispatch) {
  244. ignore_dispatch = 0;
  245. } else {
  246. cont = 0;
  247. }
  248. break;
  249. case EVS_DISPATCH_ALL:
  250. if (ignore_dispatch) {
  251. ignore_dispatch = 0;
  252. }
  253. break;
  254. case EVS_DISPATCH_BLOCKING:
  255. break;
  256. }
  257. } while (cont);
  258. error_put:
  259. saHandleInstancePut (&evs_handle_t_db, handle);
  260. return (error);
  261. }
  262. evs_error_t evs_join (
  263. evs_handle_t handle,
  264. const struct evs_group *groups,
  265. size_t group_entries)
  266. {
  267. evs_error_t error;
  268. struct evs_inst *evs_inst;
  269. struct iovec iov[2];
  270. struct req_lib_evs_join req_lib_evs_join;
  271. struct res_lib_evs_join res_lib_evs_join;
  272. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  273. if (error != EVS_OK) {
  274. return (error);
  275. }
  276. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  277. (group_entries * sizeof (struct evs_group));
  278. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  279. req_lib_evs_join.group_entries = group_entries;
  280. iov[0].iov_base = &req_lib_evs_join;
  281. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  282. iov[1].iov_base = (void*) groups; /* cast away const */
  283. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  284. pthread_mutex_lock (&evs_inst->response_mutex);
  285. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov, 2,
  286. &res_lib_evs_join, sizeof (struct res_lib_evs_join));
  287. pthread_mutex_unlock (&evs_inst->response_mutex);
  288. if (error != CS_OK) {
  289. goto error_exit;
  290. }
  291. error = res_lib_evs_join.header.error;
  292. error_exit:
  293. saHandleInstancePut (&evs_handle_t_db, handle);
  294. return (error);
  295. }
  296. evs_error_t evs_leave (
  297. evs_handle_t handle,
  298. const struct evs_group *groups,
  299. size_t group_entries)
  300. {
  301. evs_error_t error;
  302. struct evs_inst *evs_inst;
  303. struct iovec iov[2];
  304. struct req_lib_evs_leave req_lib_evs_leave;
  305. struct res_lib_evs_leave res_lib_evs_leave;
  306. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  307. if (error != CS_OK) {
  308. return (error);
  309. }
  310. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  311. (group_entries * sizeof (struct evs_group));
  312. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  313. req_lib_evs_leave.group_entries = group_entries;
  314. iov[0].iov_base = &req_lib_evs_leave;
  315. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  316. iov[1].iov_base = (void *) groups; /* cast away const */
  317. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  318. pthread_mutex_lock (&evs_inst->response_mutex);
  319. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov, 2,
  320. &res_lib_evs_leave, sizeof (struct res_lib_evs_leave));
  321. pthread_mutex_unlock (&evs_inst->response_mutex);
  322. if (error != CS_OK) {
  323. goto error_exit;
  324. }
  325. error = res_lib_evs_leave.header.error;
  326. error_exit:
  327. saHandleInstancePut (&evs_handle_t_db, handle);
  328. return (error);
  329. }
  330. evs_error_t evs_mcast_joined (
  331. evs_handle_t handle,
  332. evs_guarantee_t guarantee,
  333. const struct iovec *iovec,
  334. unsigned int iov_len)
  335. {
  336. int i;
  337. evs_error_t error;
  338. struct evs_inst *evs_inst;
  339. struct iovec iov[64];
  340. struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
  341. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  342. size_t msg_len = 0;
  343. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  344. if (error != CS_OK) {
  345. return (error);
  346. }
  347. for (i = 0; i < iov_len; i++ ) {
  348. msg_len += iovec[i].iov_len;
  349. }
  350. req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
  351. msg_len;
  352. req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
  353. req_lib_evs_mcast_joined.guarantee = guarantee;
  354. req_lib_evs_mcast_joined.msg_len = msg_len;
  355. iov[0].iov_base = &req_lib_evs_mcast_joined;
  356. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
  357. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  358. pthread_mutex_lock (&evs_inst->response_mutex);
  359. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov,
  360. iov_len + 1,
  361. &res_lib_evs_mcast_joined,
  362. sizeof (struct res_lib_evs_mcast_joined));
  363. pthread_mutex_unlock (&evs_inst->response_mutex);
  364. if (error != CS_OK) {
  365. goto error_exit;
  366. }
  367. error = res_lib_evs_mcast_joined.header.error;
  368. error_exit:
  369. saHandleInstancePut (&evs_handle_t_db, handle);
  370. return (error);
  371. }
  372. evs_error_t evs_mcast_groups (
  373. evs_handle_t handle,
  374. evs_guarantee_t guarantee,
  375. const struct evs_group *groups,
  376. size_t group_entries,
  377. const struct iovec *iovec,
  378. unsigned int iov_len)
  379. {
  380. int i;
  381. evs_error_t error;
  382. struct evs_inst *evs_inst;
  383. struct iovec iov[64]; /* FIXME: what if iov_len > 62 ? use malloc */
  384. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  385. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  386. size_t msg_len = 0;
  387. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  388. if (error != CS_OK) {
  389. return (error);
  390. }
  391. for (i = 0; i < iov_len; i++) {
  392. msg_len += iovec[i].iov_len;
  393. }
  394. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  395. (group_entries * sizeof (struct evs_group)) + msg_len;
  396. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  397. req_lib_evs_mcast_groups.guarantee = guarantee;
  398. req_lib_evs_mcast_groups.msg_len = msg_len;
  399. req_lib_evs_mcast_groups.group_entries = group_entries;
  400. iov[0].iov_base = &req_lib_evs_mcast_groups;
  401. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  402. iov[1].iov_base = (void *) groups; /* cast away const */
  403. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  404. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  405. pthread_mutex_lock (&evs_inst->response_mutex);
  406. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov,
  407. iov_len + 2,
  408. &res_lib_evs_mcast_groups,
  409. sizeof (struct res_lib_evs_mcast_groups));
  410. pthread_mutex_unlock (&evs_inst->response_mutex);
  411. if (error != CS_OK) {
  412. goto error_exit;
  413. }
  414. error = res_lib_evs_mcast_groups.header.error;
  415. error_exit:
  416. saHandleInstancePut (&evs_handle_t_db, handle);
  417. return (error);
  418. }
  419. evs_error_t evs_membership_get (
  420. evs_handle_t handle,
  421. unsigned int *local_nodeid,
  422. unsigned int *member_list,
  423. size_t *member_list_entries)
  424. {
  425. evs_error_t error;
  426. struct evs_inst *evs_inst;
  427. struct iovec iov;
  428. struct req_lib_evs_membership_get req_lib_evs_membership_get;
  429. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  430. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  431. if (error != CS_OK) {
  432. return (error);
  433. }
  434. req_lib_evs_membership_get.header.size = sizeof (struct req_lib_evs_membership_get);
  435. req_lib_evs_membership_get.header.id = MESSAGE_REQ_EVS_MEMBERSHIP_GET;
  436. iov.iov_base = &req_lib_evs_membership_get;
  437. iov.iov_len = sizeof (struct req_lib_evs_membership_get);
  438. pthread_mutex_lock (&evs_inst->response_mutex);
  439. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx,
  440. &iov,
  441. 1,
  442. &res_lib_evs_membership_get,
  443. sizeof (struct res_lib_evs_membership_get));
  444. pthread_mutex_unlock (&evs_inst->response_mutex);
  445. if (error != CS_OK) {
  446. goto error_exit;
  447. }
  448. error = res_lib_evs_membership_get.header.error;
  449. /*
  450. * Copy results to caller
  451. */
  452. if (local_nodeid) {
  453. *local_nodeid = res_lib_evs_membership_get.local_nodeid;
  454. }
  455. *member_list_entries = MIN (*member_list_entries,
  456. res_lib_evs_membership_get.member_list_entries);
  457. if (member_list) {
  458. memcpy (member_list, &res_lib_evs_membership_get.member_list,
  459. *member_list_entries * sizeof (struct in_addr));
  460. }
  461. error_exit:
  462. saHandleInstancePut (&evs_handle_t_db, handle);
  463. return (error);
  464. }
  465. /** @} */