evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. saHandleInstancePut (&evs_handle_t_db, *handle);
  324. error_nounlock:
  325. return (error);
  326. }
  327. evs_error_t evs_join (
  328. evs_handle_t *handle,
  329. struct evs_group *groups,
  330. int group_entries)
  331. {
  332. evs_error_t error;
  333. struct evs_inst *evs_inst;
  334. struct iovec iov[2];
  335. struct req_lib_evs_join req_lib_evs_join;
  336. struct res_lib_evs_join res_lib_evs_join;
  337. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  338. if (error != SA_OK) {
  339. return (error);
  340. }
  341. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  342. (group_entries * sizeof (struct evs_group));
  343. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  344. req_lib_evs_join.group_entries = group_entries;
  345. iov[0].iov_base = &req_lib_evs_join;
  346. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  347. iov[1].iov_base = groups;
  348. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  349. error = saSendMsgRetry (evs_inst->fd, iov, 2);
  350. if (error != SA_OK) {
  351. goto error_exit;
  352. }
  353. error = saRecvRetry (evs_inst->fd, &res_lib_evs_join,
  354. sizeof (struct res_lib_evs_join), MSG_WAITALL | MSG_NOSIGNAL);
  355. if (error != SA_OK) {
  356. goto error_exit;
  357. }
  358. error = res_lib_evs_join.header.error;
  359. error_exit:
  360. saHandleInstancePut (&evs_handle_t_db, *handle);
  361. return (error);
  362. }
  363. evs_error_t evs_leave (
  364. evs_handle_t *handle,
  365. struct evs_group *groups,
  366. int group_entries)
  367. {
  368. evs_error_t error;
  369. struct evs_inst *evs_inst;
  370. struct iovec iov[2];
  371. struct req_lib_evs_leave req_lib_evs_leave;
  372. struct res_lib_evs_leave res_lib_evs_leave;
  373. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  374. if (error != SA_OK) {
  375. return (error);
  376. }
  377. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  378. (group_entries * sizeof (struct evs_group));
  379. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  380. req_lib_evs_leave.group_entries = group_entries;
  381. iov[0].iov_base = &req_lib_evs_leave;
  382. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  383. iov[1].iov_base = groups;
  384. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  385. error = saSendMsgRetry (evs_inst->fd, iov, 2);
  386. if (error != SA_OK) {
  387. goto error_exit;
  388. }
  389. error = saRecvRetry (evs_inst->fd, &res_lib_evs_leave,
  390. sizeof (struct res_lib_evs_leave), MSG_WAITALL | MSG_NOSIGNAL);
  391. if (error != SA_OK) {
  392. goto error_exit;
  393. }
  394. error = res_lib_evs_leave.header.error;
  395. error_exit:
  396. saHandleInstancePut (&evs_handle_t_db, *handle);
  397. return (error);
  398. }
  399. evs_error_t evs_mcast_joined (
  400. evs_handle_t *handle,
  401. evs_guarantee_t guarantee,
  402. evs_priority_t priority,
  403. struct iovec *iovec,
  404. int iov_len)
  405. {
  406. int i;
  407. evs_error_t error;
  408. struct evs_inst *evs_inst;
  409. struct iovec iov[64];
  410. struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
  411. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  412. int msg_len = 0;
  413. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  414. if (error != SA_OK) {
  415. return (error);
  416. }
  417. for (i = 0; i < iov_len; i++ ) {
  418. msg_len += iovec[i].iov_len;
  419. }
  420. req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
  421. msg_len;
  422. req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
  423. req_lib_evs_mcast_joined.priority = priority;
  424. req_lib_evs_mcast_joined.guarantee = guarantee;
  425. req_lib_evs_mcast_joined.msg_len = msg_len;
  426. iov[0].iov_base = &req_lib_evs_mcast_joined;
  427. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
  428. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  429. error = saSendMsgRetry (evs_inst->fd, iov, 1 + iov_len);
  430. if (error != SA_OK) {
  431. goto error_exit;
  432. }
  433. error = saRecvQueue (evs_inst->fd, &res_lib_evs_mcast_joined, &evs_inst->inq,
  434. MESSAGE_RES_EVS_MCAST_JOINED);
  435. if (error != SA_OK) {
  436. goto error_exit;
  437. }
  438. error = res_lib_evs_mcast_joined.header.error;
  439. error_exit:
  440. saHandleInstancePut (&evs_handle_t_db, *handle);
  441. return (error);
  442. }
  443. evs_error_t evs_mcast_groups (
  444. evs_handle_t *handle,
  445. evs_guarantee_t guarantee,
  446. evs_priority_t priority,
  447. struct evs_group *groups,
  448. int group_entries,
  449. struct iovec *iovec,
  450. int iov_len)
  451. {
  452. int i;
  453. evs_error_t error;
  454. struct evs_inst *evs_inst;
  455. struct iovec iov[64];
  456. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  457. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  458. int msg_len = 0;
  459. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  460. if (error != SA_OK) {
  461. return (error);
  462. }
  463. for (i = 0; i < iov_len; i++) {
  464. msg_len += iovec[i].iov_len;
  465. }
  466. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  467. (group_entries * sizeof (struct evs_group)) + msg_len;
  468. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  469. req_lib_evs_mcast_groups.priority = priority;
  470. req_lib_evs_mcast_groups.guarantee = guarantee;
  471. req_lib_evs_mcast_groups.msg_len = msg_len;
  472. req_lib_evs_mcast_groups.group_entries = group_entries;
  473. iov[0].iov_base = &req_lib_evs_mcast_groups;
  474. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  475. iov[1].iov_base = groups;
  476. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  477. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  478. error = saSendMsgRetry (evs_inst->fd, iov, 2 + iov_len);
  479. if (error != SA_OK) {
  480. goto error_exit;
  481. }
  482. error = saRecvQueue (evs_inst->fd, &res_lib_evs_mcast_groups, &evs_inst->inq,
  483. MESSAGE_RES_EVS_MCAST_GROUPS);
  484. if (error != SA_OK) {
  485. goto error_exit;
  486. }
  487. error = res_lib_evs_mcast_groups.header.error;
  488. error_exit:
  489. saHandleInstancePut (&evs_handle_t_db, *handle);
  490. return (error);
  491. }