evs.c 16 KB

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