evs.c 15 KB

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