evs.c 16 KB

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