evs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. * Copyright (c) 2004-2005 MontaVista Software, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Provides an extended virtual synchrony API using the openais executive
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <pthread.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <errno.h>
  45. #include "../include/saAis.h"
  46. #include "../include/evs.h"
  47. #include "../include/ipc_evs.h"
  48. #include "util.h"
  49. struct evs_inst {
  50. int response_fd;
  51. int dispatch_fd;
  52. int finalize;
  53. evs_callbacks_t callbacks;
  54. pthread_mutex_t response_mutex;
  55. pthread_mutex_t dispatch_mutex;
  56. };
  57. struct res_overlay {
  58. struct res_header header;
  59. char data[512000];
  60. };
  61. static void evs_instance_destructor (void *instance);
  62. static struct saHandleDatabase evs_handle_t_db = {
  63. .handleCount = 0,
  64. .handles = 0,
  65. .mutex = PTHREAD_MUTEX_INITIALIZER,
  66. .handleInstanceDestructor = evs_instance_destructor
  67. };
  68. /*
  69. * Clean up function for an evt instance (saEvtInitialize) handle
  70. */
  71. static void evs_instance_destructor (void *instance)
  72. {
  73. }
  74. /**
  75. * @defgroup evs_openais The extended virtual synchrony passthrough API
  76. * @ingroup openais
  77. *
  78. * @{
  79. */
  80. /**
  81. * test
  82. * @param handle The handle of evs initialize
  83. * @param callbacks The callbacks for evs_initialize
  84. * @returns EVS_OK
  85. */
  86. evs_error_t evs_initialize (
  87. evs_handle_t *handle,
  88. evs_callbacks_t *callbacks)
  89. {
  90. SaAisErrorT error;
  91. struct evs_inst *evs_inst;
  92. error = saHandleCreate (&evs_handle_t_db, sizeof (struct evs_inst), handle);
  93. if (error != SA_AIS_OK) {
  94. goto error_no_destroy;
  95. }
  96. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  97. if (error != SA_AIS_OK) {
  98. goto error_destroy;
  99. }
  100. error = saServiceConnectTwo (&evs_inst->response_fd,
  101. &evs_inst->dispatch_fd,
  102. EVS_SERVICE);
  103. if (error != SA_AIS_OK) {
  104. goto error_put_destroy;
  105. }
  106. memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
  107. pthread_mutex_init (&evs_inst->response_mutex, NULL);
  108. pthread_mutex_init (&evs_inst->dispatch_mutex, NULL);
  109. saHandleInstancePut (&evs_handle_t_db, *handle);
  110. return (SA_AIS_OK);
  111. error_put_destroy:
  112. saHandleInstancePut (&evs_handle_t_db, *handle);
  113. error_destroy:
  114. saHandleDestroy (&evs_handle_t_db, *handle);
  115. error_no_destroy:
  116. return (error);
  117. }
  118. evs_error_t evs_finalize (
  119. evs_handle_t handle)
  120. {
  121. struct evs_inst *evs_inst;
  122. SaAisErrorT error;
  123. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  124. if (error != SA_AIS_OK) {
  125. return (error);
  126. }
  127. // TODO is the locking right here
  128. pthread_mutex_lock (&evs_inst->response_mutex);
  129. /*
  130. * Another thread has already started finalizing
  131. */
  132. if (evs_inst->finalize) {
  133. pthread_mutex_unlock (&evs_inst->response_mutex);
  134. saHandleInstancePut (&evs_handle_t_db, handle);
  135. return (EVS_ERR_BAD_HANDLE);
  136. }
  137. evs_inst->finalize = 1;
  138. pthread_mutex_unlock (&evs_inst->response_mutex);
  139. saHandleDestroy (&evs_handle_t_db, handle);
  140. /*
  141. * Disconnect from the server
  142. */
  143. if (evs_inst->response_fd != -1) {
  144. shutdown(evs_inst->response_fd, 0);
  145. close(evs_inst->response_fd);
  146. }
  147. if (evs_inst->dispatch_fd != -1) {
  148. shutdown(evs_inst->dispatch_fd, 0);
  149. close(evs_inst->dispatch_fd);
  150. }
  151. saHandleInstancePut (&evs_handle_t_db, handle);
  152. return (EVS_OK);
  153. }
  154. evs_error_t evs_fd_get (
  155. evs_handle_t handle,
  156. int *fd)
  157. {
  158. SaAisErrorT error;
  159. struct evs_inst *evs_inst;
  160. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  161. if (error != SA_AIS_OK) {
  162. return (error);
  163. }
  164. *fd = evs_inst->dispatch_fd;
  165. saHandleInstancePut (&evs_handle_t_db, handle);
  166. return (SA_AIS_OK);
  167. }
  168. evs_error_t evs_dispatch (
  169. evs_handle_t handle,
  170. evs_dispatch_t dispatch_types)
  171. {
  172. struct pollfd ufds;
  173. int timeout = -1;
  174. SaAisErrorT error;
  175. int cont = 1; /* always continue do loop except when set to 0 */
  176. int dispatch_avail;
  177. struct evs_inst *evs_inst;
  178. struct res_evs_confchg_callback *res_evs_confchg_callback;
  179. struct res_evs_deliver_callback *res_evs_deliver_callback;
  180. evs_callbacks_t callbacks;
  181. struct res_overlay dispatch_data;
  182. int ignore_dispatch = 0;
  183. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  184. if (error != SA_AIS_OK) {
  185. return (error);
  186. }
  187. /*
  188. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  189. * wait indefinately for SA_DISPATCH_BLOCKING
  190. */
  191. if (dispatch_types == EVS_DISPATCH_ALL) {
  192. timeout = 0;
  193. }
  194. do {
  195. ufds.fd = evs_inst->dispatch_fd;
  196. ufds.events = POLLIN;
  197. ufds.revents = 0;
  198. error = saPollRetry (&ufds, 1, timeout);
  199. if (error != SA_AIS_OK) {
  200. goto error_nounlock;
  201. }
  202. pthread_mutex_lock (&evs_inst->dispatch_mutex);
  203. /*
  204. * Regather poll data in case ufds has changed since taking lock
  205. */
  206. error = saPollRetry (&ufds, 1, 0);
  207. if (error != SA_AIS_OK) {
  208. goto error_nounlock;
  209. }
  210. /*
  211. * Handle has been finalized in another thread
  212. */
  213. if (evs_inst->finalize == 1) {
  214. error = EVS_OK;
  215. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  216. goto error_unlock;
  217. }
  218. dispatch_avail = ufds.revents & POLLIN;
  219. if (dispatch_avail == 0 && dispatch_types == EVS_DISPATCH_ALL) {
  220. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  221. break; /* exit do while cont is 1 loop */
  222. } else
  223. if (dispatch_avail == 0) {
  224. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  225. continue; /* next poll */
  226. }
  227. if (ufds.revents & POLLIN) {
  228. /*
  229. * Queue empty, read response from socket
  230. */
  231. error = saRecvRetry (evs_inst->dispatch_fd, &dispatch_data.header,
  232. sizeof (struct res_header));
  233. if (error != SA_AIS_OK) {
  234. goto error_unlock;
  235. }
  236. if (dispatch_data.header.size > sizeof (struct res_header)) {
  237. error = saRecvRetry (evs_inst->dispatch_fd, &dispatch_data.data,
  238. dispatch_data.header.size - sizeof (struct res_header));
  239. if (error != SA_AIS_OK) {
  240. goto error_unlock;
  241. }
  242. }
  243. } else {
  244. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  245. continue;
  246. }
  247. /*
  248. * Make copy of callbacks, message data, unlock instance, and call callback
  249. * A risk of this dispatch method is that the callback routines may
  250. * operate at the same time that evsFinalize has been called.
  251. */
  252. memcpy (&callbacks, &evs_inst->callbacks, sizeof (evs_callbacks_t));
  253. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  254. /*
  255. * Dispatch incoming message
  256. */
  257. switch (dispatch_data.header.id) {
  258. case MESSAGE_RES_EVS_DELIVER_CALLBACK:
  259. res_evs_deliver_callback = (struct res_evs_deliver_callback *)&dispatch_data;
  260. callbacks.evs_deliver_fn (
  261. &res_evs_deliver_callback->evs_address,
  262. &res_evs_deliver_callback->msg,
  263. res_evs_deliver_callback->msglen);
  264. break;
  265. case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
  266. res_evs_confchg_callback = (struct res_evs_confchg_callback *)&dispatch_data;
  267. callbacks.evs_confchg_fn (
  268. res_evs_confchg_callback->member_list,
  269. res_evs_confchg_callback->member_list_entries,
  270. res_evs_confchg_callback->left_list,
  271. res_evs_confchg_callback->left_list_entries,
  272. res_evs_confchg_callback->joined_list,
  273. res_evs_confchg_callback->joined_list_entries);
  274. break;
  275. default:
  276. error = SA_AIS_ERR_LIBRARY;
  277. goto error_nounlock;
  278. break;
  279. }
  280. /*
  281. * Determine if more messages should be processed
  282. * */
  283. switch (dispatch_types) {
  284. case EVS_DISPATCH_ONE:
  285. if (ignore_dispatch) {
  286. ignore_dispatch = 0;
  287. } else {
  288. cont = 0;
  289. }
  290. break;
  291. case EVS_DISPATCH_ALL:
  292. if (ignore_dispatch) {
  293. ignore_dispatch = 0;
  294. }
  295. break;
  296. case EVS_DISPATCH_BLOCKING:
  297. break;
  298. }
  299. } while (cont);
  300. error_unlock:
  301. saHandleInstancePut (&evs_handle_t_db, handle);
  302. error_nounlock:
  303. return (error);
  304. }
  305. evs_error_t evs_join (
  306. evs_handle_t handle,
  307. struct evs_group *groups,
  308. int group_entries)
  309. {
  310. evs_error_t error;
  311. struct evs_inst *evs_inst;
  312. struct iovec iov[2];
  313. struct req_lib_evs_join req_lib_evs_join;
  314. struct res_lib_evs_join res_lib_evs_join;
  315. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  316. if (error != SA_AIS_OK) {
  317. return (error);
  318. }
  319. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  320. (group_entries * sizeof (struct evs_group));
  321. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  322. req_lib_evs_join.group_entries = group_entries;
  323. iov[0].iov_base = &req_lib_evs_join;
  324. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  325. iov[1].iov_base = groups;
  326. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  327. pthread_mutex_lock (&evs_inst->response_mutex);
  328. error = saSendMsgReceiveReply (evs_inst->response_fd, iov, 2,
  329. &res_lib_evs_join, sizeof (struct res_lib_evs_join));
  330. pthread_mutex_unlock (&evs_inst->response_mutex);
  331. if (error != SA_AIS_OK) {
  332. goto error_exit;
  333. }
  334. error = res_lib_evs_join.header.error;
  335. error_exit:
  336. saHandleInstancePut (&evs_handle_t_db, handle);
  337. return (error);
  338. }
  339. evs_error_t evs_leave (
  340. evs_handle_t handle,
  341. struct evs_group *groups,
  342. int group_entries)
  343. {
  344. evs_error_t error;
  345. struct evs_inst *evs_inst;
  346. struct iovec iov[2];
  347. struct req_lib_evs_leave req_lib_evs_leave;
  348. struct res_lib_evs_leave res_lib_evs_leave;
  349. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  350. if (error != SA_AIS_OK) {
  351. return (error);
  352. }
  353. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  354. (group_entries * sizeof (struct evs_group));
  355. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  356. req_lib_evs_leave.group_entries = group_entries;
  357. iov[0].iov_base = &req_lib_evs_leave;
  358. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  359. iov[1].iov_base = groups;
  360. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  361. pthread_mutex_lock (&evs_inst->response_mutex);
  362. error = saSendMsgReceiveReply (evs_inst->response_fd, iov, 2,
  363. &res_lib_evs_leave, sizeof (struct res_lib_evs_leave));
  364. pthread_mutex_unlock (&evs_inst->response_mutex);
  365. if (error != SA_AIS_OK) {
  366. goto error_exit;
  367. }
  368. error = res_lib_evs_leave.header.error;
  369. error_exit:
  370. saHandleInstancePut (&evs_handle_t_db, handle);
  371. return (error);
  372. }
  373. evs_error_t evs_mcast_joined (
  374. evs_handle_t handle,
  375. evs_guarantee_t guarantee,
  376. struct iovec *iovec,
  377. int iov_len)
  378. {
  379. int i;
  380. evs_error_t error;
  381. struct evs_inst *evs_inst;
  382. struct iovec iov[64];
  383. struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
  384. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  385. int msg_len = 0;
  386. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  387. if (error != SA_AIS_OK) {
  388. return (error);
  389. }
  390. for (i = 0; i < iov_len; i++ ) {
  391. msg_len += iovec[i].iov_len;
  392. }
  393. req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
  394. msg_len;
  395. req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
  396. req_lib_evs_mcast_joined.guarantee = guarantee;
  397. req_lib_evs_mcast_joined.msg_len = msg_len;
  398. iov[0].iov_base = &req_lib_evs_mcast_joined;
  399. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
  400. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  401. pthread_mutex_lock (&evs_inst->response_mutex);
  402. error = saSendMsgReceiveReply (evs_inst->response_fd, iov, iov_len + 1,
  403. &res_lib_evs_mcast_joined, sizeof (struct res_lib_evs_mcast_joined));
  404. pthread_mutex_unlock (&evs_inst->response_mutex);
  405. if (error != SA_AIS_OK) {
  406. goto error_exit;
  407. }
  408. error = res_lib_evs_mcast_joined.header.error;
  409. error_exit:
  410. saHandleInstancePut (&evs_handle_t_db, handle);
  411. return (error);
  412. }
  413. evs_error_t evs_mcast_groups (
  414. evs_handle_t handle,
  415. evs_guarantee_t guarantee,
  416. struct evs_group *groups,
  417. int group_entries,
  418. struct iovec *iovec,
  419. int iov_len)
  420. {
  421. int i;
  422. evs_error_t error;
  423. struct evs_inst *evs_inst;
  424. struct iovec iov[64];
  425. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  426. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  427. int msg_len = 0;
  428. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  429. if (error != SA_AIS_OK) {
  430. return (error);
  431. }
  432. for (i = 0; i < iov_len; i++) {
  433. msg_len += iovec[i].iov_len;
  434. }
  435. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  436. (group_entries * sizeof (struct evs_group)) + msg_len;
  437. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  438. req_lib_evs_mcast_groups.guarantee = guarantee;
  439. req_lib_evs_mcast_groups.msg_len = msg_len;
  440. req_lib_evs_mcast_groups.group_entries = group_entries;
  441. iov[0].iov_base = &req_lib_evs_mcast_groups;
  442. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  443. iov[1].iov_base = groups;
  444. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  445. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  446. pthread_mutex_lock (&evs_inst->response_mutex);
  447. error = saSendMsgReceiveReply (evs_inst->response_fd, iov, iov_len + 2,
  448. &res_lib_evs_mcast_groups, sizeof (struct res_lib_evs_mcast_groups));
  449. pthread_mutex_unlock (&evs_inst->response_mutex);
  450. if (error != SA_AIS_OK) {
  451. goto error_exit;
  452. }
  453. error = res_lib_evs_mcast_groups.header.error;
  454. error_exit:
  455. saHandleInstancePut (&evs_handle_t_db, handle);
  456. return (error);
  457. }
  458. evs_error_t evs_membership_get (
  459. evs_handle_t handle,
  460. struct evs_address *local_addr,
  461. struct evs_address *member_list,
  462. int *member_list_entries)
  463. {
  464. evs_error_t error;
  465. struct evs_inst *evs_inst;
  466. struct iovec iov;
  467. struct req_lib_evs_membership_get req_lib_evs_membership_get;
  468. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  469. error = saHandleInstanceGet (&evs_handle_t_db, handle, (void *)&evs_inst);
  470. if (error != SA_AIS_OK) {
  471. return (error);
  472. }
  473. req_lib_evs_membership_get.header.size = sizeof (struct req_lib_evs_membership_get);
  474. req_lib_evs_membership_get.header.id = MESSAGE_REQ_EVS_MEMBERSHIP_GET;
  475. iov.iov_base = &req_lib_evs_membership_get;
  476. iov.iov_len = sizeof (struct req_lib_evs_membership_get);
  477. pthread_mutex_lock (&evs_inst->response_mutex);
  478. error = saSendMsgReceiveReply (evs_inst->response_fd, &iov, 1,
  479. &res_lib_evs_membership_get, sizeof (struct res_lib_evs_membership_get));
  480. pthread_mutex_unlock (&evs_inst->response_mutex);
  481. if (error != SA_AIS_OK) {
  482. goto error_exit;
  483. }
  484. error = res_lib_evs_membership_get.header.error;
  485. /*
  486. * Copy results to caller
  487. */
  488. if (local_addr) {
  489. memcpy (local_addr, &res_lib_evs_membership_get.local_addr, sizeof (struct in_addr));
  490. }
  491. *member_list_entries = *member_list_entries < res_lib_evs_membership_get.member_list_entries ?
  492. *member_list_entries : res_lib_evs_membership_get.member_list_entries;
  493. if (member_list) {
  494. memcpy (member_list, &res_lib_evs_membership_get.member_list,
  495. *member_list_entries * sizeof (struct in_addr));
  496. }
  497. error_exit:
  498. saHandleInstancePut (&evs_handle_t_db, handle);
  499. return (error);
  500. }
  501. /** @} */