evs.c 15 KB

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