evs.c 15 KB

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