evs.c 15 KB

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