evs.c 16 KB

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