evs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 <pthread.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include "util.h"
  43. #include "../include/ais_msg.h"
  44. #include "../include/ais_types.h"
  45. #include "../include/evs.h"
  46. struct evs_inst {
  47. int fd;
  48. int finalize;
  49. evs_callbacks_t callbacks;
  50. struct queue inq;
  51. char dispatch_buffer[512000];
  52. pthread_mutex_t mutex;
  53. };
  54. static struct saHandleDatabase evs_handle_t_db = {
  55. .handleCount = 0,
  56. .handles = 0,
  57. .mutex = PTHREAD_MUTEX_INITIALIZER,
  58. .handleInstanceDestructor = 0
  59. };
  60. evs_error_t evs_initialize (
  61. evs_handle_t *handle,
  62. evs_callbacks_t *callbacks)
  63. {
  64. SaErrorT error;
  65. struct evs_inst *evs_inst;
  66. error = saHandleCreate (&evs_handle_t_db, sizeof (struct evs_inst), handle);
  67. if (error != SA_OK) {
  68. goto error_no_destroy;
  69. }
  70. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  71. if (error != SA_OK) {
  72. goto error_destroy;
  73. }
  74. /*
  75. * An inq is needed to store async messages while waiting for a
  76. * sync response
  77. */
  78. error = saQueueInit (&evs_inst->inq, 128, sizeof (void *));
  79. if (error != SA_OK) {
  80. goto error_put_destroy;
  81. }
  82. error = saServiceConnect (&evs_inst->fd, MESSAGE_REQ_EVS_INIT);
  83. if (error != SA_OK) {
  84. goto error_put_destroy_free;
  85. }
  86. memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
  87. pthread_mutex_init (&evs_inst->mutex, NULL);
  88. saHandleInstancePut (&evs_handle_t_db, *handle);
  89. return (SA_OK);
  90. error_put_destroy_free:
  91. free (evs_inst->inq.items);
  92. error_put_destroy:
  93. saHandleInstancePut (&evs_handle_t_db, *handle);
  94. error_destroy:
  95. saHandleDestroy (&evs_handle_t_db, *handle);
  96. error_no_destroy:
  97. return (error);
  98. }
  99. evs_error_t evs_finalize (
  100. evs_handle_t *handle)
  101. {
  102. struct evs_inst *evs_inst;
  103. SaErrorT error;
  104. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  105. if (error != SA_OK) {
  106. return (error);
  107. }
  108. /*
  109. * Another thread has already started finalizing
  110. */
  111. if (evs_inst->finalize) {
  112. pthread_mutex_unlock (&evs_inst->mutex);
  113. saHandleInstancePut (&evs_handle_t_db, *handle);
  114. return (EVS_ERR_BAD_HANDLE);
  115. }
  116. evs_inst->finalize = 1;
  117. saActivatePoll (evs_inst->fd);
  118. pthread_mutex_unlock (&evs_inst->mutex);
  119. saHandleInstancePut (&evs_handle_t_db, *handle);
  120. saHandleDestroy (&evs_handle_t_db, *handle);
  121. return (EVS_OK);
  122. }
  123. evs_error_t evs_fd_get (
  124. evs_handle_t *handle,
  125. int *fd)
  126. {
  127. SaErrorT error;
  128. struct evs_inst *evs_inst;
  129. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  130. if (error != SA_OK) {
  131. return (error);
  132. }
  133. *fd = evs_inst->fd;
  134. saHandleInstancePut (&evs_handle_t_db, *handle);
  135. return (SA_OK);
  136. }
  137. struct message_overlay {
  138. struct res_header header;
  139. char data[4096];
  140. };
  141. evs_error_t evs_dispatch (
  142. evs_handle_t *handle,
  143. evs_dispatch_t dispatch_types)
  144. {
  145. struct pollfd ufds;
  146. int timeout = -1;
  147. SaErrorT error;
  148. int cont = 1; /* always continue do loop except when set to 0 */
  149. int dispatch_avail;
  150. int poll_fd;
  151. struct evs_inst *evs_inst;
  152. struct res_evs_confchg_callback *res_evs_confchg_callback;
  153. struct res_evs_deliver_callback *res_evs_deliver_callback;
  154. evs_callbacks_t callbacks;
  155. struct message_overlay *dispatch_data;
  156. int empty;
  157. struct res_header **queue_msg;
  158. struct res_header *msg = NULL;
  159. int ignore_dispatch = 0;
  160. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  161. if (error != SA_OK) {
  162. return (error);
  163. }
  164. /*
  165. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  166. * wait indefinately for SA_DISPATCH_BLOCKING
  167. */
  168. if (dispatch_types == EVS_DISPATCH_ALL) {
  169. timeout = 0;
  170. }
  171. do {
  172. poll_fd = evs_inst->fd;
  173. ufds.fd = poll_fd;
  174. ufds.events = POLLIN;
  175. ufds.revents = 0;
  176. pthread_mutex_lock (&evs_inst->mutex);
  177. saQueueIsEmpty (&evs_inst->inq, &empty);
  178. if (empty == 1) {
  179. pthread_mutex_unlock (&evs_inst->mutex);
  180. error = saPollRetry (&ufds, 1, timeout);
  181. if (error != SA_OK) {
  182. goto error_nounlock;
  183. }
  184. pthread_mutex_lock (&evs_inst->mutex);
  185. }
  186. /*
  187. * Handle has been finalized in another thread
  188. */
  189. if (evs_inst->finalize == 1) {
  190. error = SA_OK;
  191. pthread_mutex_unlock (&evs_inst->mutex);
  192. goto error_unlock;
  193. }
  194. dispatch_avail = (ufds.revents & POLLIN) | (empty == 0);
  195. if (dispatch_avail == 0 && dispatch_types == EVS_DISPATCH_ALL) {
  196. pthread_mutex_unlock (&evs_inst->mutex);
  197. break; /* exit do while cont is 1 loop */
  198. } else
  199. if (dispatch_avail == 0) {
  200. pthread_mutex_unlock (&evs_inst->mutex);
  201. continue; /* next poll */
  202. }
  203. saQueueIsEmpty (&evs_inst->inq, &empty);
  204. if (empty == 0) {
  205. /*
  206. * Queue is not empty, read data from queue
  207. */
  208. saQueueItemGet (&evs_inst->inq, (void *)&queue_msg);
  209. msg = *queue_msg;
  210. dispatch_data = (struct message_overlay *)msg;
  211. res_evs_deliver_callback = (struct res_evs_deliver_callback *)msg;
  212. res_evs_confchg_callback = (struct res_evs_confchg_callback *)msg;
  213. saQueueItemRemove (&evs_inst->inq);
  214. } else {
  215. dispatch_data = (struct message_overlay *)evs_inst->dispatch_buffer;
  216. res_evs_deliver_callback = (struct res_evs_deliver_callback *)dispatch_data;
  217. res_evs_confchg_callback = (struct res_evs_confchg_callback *)dispatch_data;
  218. /*
  219. * Queue empty, read response from socket
  220. */
  221. error = saRecvRetry (evs_inst->fd, &dispatch_data->header,
  222. sizeof (struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  223. if (error != SA_OK) {
  224. goto error_unlock;
  225. }
  226. if (dispatch_data->header.size > sizeof (struct res_header)) {
  227. error = saRecvRetry (evs_inst->fd, &dispatch_data->data,
  228. dispatch_data->header.size - sizeof (struct res_header),
  229. MSG_WAITALL | MSG_NOSIGNAL);
  230. if (error != SA_OK) {
  231. goto error_unlock;
  232. }
  233. }
  234. }
  235. /*
  236. * Make copy of callbacks, message data, unlock instance, and call callback
  237. * A risk of this dispatch method is that the callback routines may
  238. * operate at the same time that evsFinalize has been called.
  239. */
  240. memcpy (&callbacks, &evs_inst->callbacks, sizeof (evs_callbacks_t));
  241. pthread_mutex_unlock (&evs_inst->mutex);
  242. /*
  243. * Dispatch incoming message
  244. */
  245. switch (dispatch_data->header.id) {
  246. case MESSAGE_RES_LIB_ACTIVATEPOLL:
  247. ignore_dispatch = 1;
  248. break;
  249. case MESSAGE_RES_EVS_DELIVER_CALLBACK:
  250. callbacks.evs_deliver_fn (
  251. res_evs_deliver_callback->source_addr,
  252. &res_evs_deliver_callback->msg,
  253. res_evs_deliver_callback->msglen);
  254. break;
  255. case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
  256. callbacks.evs_confchg_fn (
  257. res_evs_confchg_callback->member_list,
  258. res_evs_confchg_callback->member_list_entries,
  259. res_evs_confchg_callback->left_list,
  260. res_evs_confchg_callback->left_list_entries,
  261. res_evs_confchg_callback->joined_list,
  262. res_evs_confchg_callback->joined_list_entries);
  263. break;
  264. default:
  265. error = SA_ERR_LIBRARY;
  266. goto error_nounlock;
  267. break;
  268. }
  269. if (empty == 0) {
  270. free (msg);
  271. }
  272. /*
  273. * Determine if more messages should be processed
  274. * */
  275. switch (dispatch_types) {
  276. case EVS_DISPATCH_ONE:
  277. if (ignore_dispatch) {
  278. ignore_dispatch = 0;
  279. } else {
  280. cont = 0;
  281. }
  282. break;
  283. case EVS_DISPATCH_ALL:
  284. if (ignore_dispatch) {
  285. ignore_dispatch = 0;
  286. }
  287. break;
  288. case EVS_DISPATCH_BLOCKING:
  289. break;
  290. }
  291. } while (cont);
  292. error_unlock:
  293. saHandleInstancePut (&evs_handle_t_db, *handle);
  294. error_nounlock:
  295. return (error);
  296. }
  297. evs_error_t evs_join (
  298. evs_handle_t *handle,
  299. struct evs_group *groups,
  300. int group_entries)
  301. {
  302. evs_error_t error;
  303. struct evs_inst *evs_inst;
  304. struct iovec iov[2];
  305. struct req_lib_evs_join req_lib_evs_join;
  306. struct res_lib_evs_join res_lib_evs_join;
  307. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  308. if (error != SA_OK) {
  309. return (error);
  310. }
  311. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  312. (group_entries * sizeof (struct evs_group));
  313. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  314. req_lib_evs_join.group_entries = group_entries;
  315. iov[0].iov_base = &req_lib_evs_join;
  316. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  317. iov[1].iov_base = groups;
  318. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  319. error = saSendMsgRetry (evs_inst->fd, iov, 2);
  320. if (error != SA_OK) {
  321. goto error_exit;
  322. }
  323. error = saRecvRetry (evs_inst->fd, &res_lib_evs_join,
  324. sizeof (struct res_lib_evs_join), MSG_WAITALL | MSG_NOSIGNAL);
  325. if (error != SA_OK) {
  326. goto error_exit;
  327. }
  328. error = res_lib_evs_join.header.error;
  329. error_exit:
  330. saHandleInstancePut (&evs_handle_t_db, *handle);
  331. return (error);
  332. }
  333. evs_error_t evs_leave (
  334. evs_handle_t *handle,
  335. struct evs_group *groups,
  336. int group_entries)
  337. {
  338. evs_error_t error;
  339. struct evs_inst *evs_inst;
  340. struct iovec iov[2];
  341. struct req_lib_evs_leave req_lib_evs_leave;
  342. struct res_lib_evs_leave res_lib_evs_leave;
  343. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  344. if (error != SA_OK) {
  345. return (error);
  346. }
  347. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  348. (group_entries * sizeof (struct evs_group));
  349. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  350. req_lib_evs_leave.group_entries = group_entries;
  351. iov[0].iov_base = &req_lib_evs_leave;
  352. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  353. iov[1].iov_base = groups;
  354. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  355. error = saSendMsgRetry (evs_inst->fd, iov, 2);
  356. if (error != SA_OK) {
  357. goto error_exit;
  358. }
  359. error = saRecvRetry (evs_inst->fd, &res_lib_evs_leave,
  360. sizeof (struct res_lib_evs_leave), MSG_WAITALL | MSG_NOSIGNAL);
  361. if (error != SA_OK) {
  362. goto error_exit;
  363. }
  364. error = res_lib_evs_leave.header.error;
  365. error_exit:
  366. saHandleInstancePut (&evs_handle_t_db, *handle);
  367. return (error);
  368. }
  369. evs_error_t evs_mcast_joined (
  370. evs_handle_t *handle,
  371. evs_guarantee_t guarantee,
  372. evs_priority_t priority,
  373. struct iovec *iovec,
  374. int iov_len)
  375. {
  376. int i;
  377. evs_error_t error;
  378. struct evs_inst *evs_inst;
  379. struct iovec iov[64];
  380. struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
  381. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  382. int msg_len = 0;
  383. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  384. if (error != SA_OK) {
  385. return (error);
  386. }
  387. for (i = 0; i < iov_len; i++ ) {
  388. msg_len += iovec[i].iov_len;
  389. }
  390. req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
  391. msg_len;
  392. req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
  393. req_lib_evs_mcast_joined.priority = priority;
  394. req_lib_evs_mcast_joined.guarantee = guarantee;
  395. req_lib_evs_mcast_joined.msg_len = msg_len;
  396. iov[0].iov_base = &req_lib_evs_mcast_joined;
  397. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
  398. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  399. error = saSendMsgRetry (evs_inst->fd, iov, 1 + iov_len);
  400. if (error != SA_OK) {
  401. goto error_exit;
  402. }
  403. error = saRecvQueue (evs_inst->fd, &res_lib_evs_mcast_joined, &evs_inst->inq,
  404. MESSAGE_RES_EVS_MCAST_JOINED);
  405. if (error != SA_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. evs_priority_t priority,
  417. struct evs_group *groups,
  418. int group_entries,
  419. struct iovec *iovec,
  420. int iov_len)
  421. {
  422. int i;
  423. evs_error_t error;
  424. struct evs_inst *evs_inst;
  425. struct iovec iov[64];
  426. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  427. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  428. int msg_len = 0;
  429. error = saHandleInstanceGet (&evs_handle_t_db, *handle, (void *)&evs_inst);
  430. if (error != SA_OK) {
  431. return (error);
  432. }
  433. for (i = 0; i < iov_len; i++) {
  434. msg_len += iovec[i].iov_len;
  435. }
  436. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  437. (group_entries * sizeof (struct evs_group)) + msg_len;
  438. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  439. req_lib_evs_mcast_groups.priority = priority;
  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 = &req_lib_evs_mcast_groups;
  444. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  445. iov[1].iov_base = groups;
  446. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  447. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  448. error = saSendMsgRetry (evs_inst->fd, iov, 2 + iov_len);
  449. if (error != SA_OK) {
  450. goto error_exit;
  451. }
  452. error = saRecvQueue (evs_inst->fd, &res_lib_evs_mcast_groups, &evs_inst->inq,
  453. MESSAGE_RES_EVS_MCAST_GROUPS);
  454. if (error != SA_OK) {
  455. goto error_exit;
  456. }
  457. error = res_lib_evs_mcast_groups.header.error;
  458. error_exit:
  459. saHandleInstancePut (&evs_handle_t_db, *handle);
  460. return (error);
  461. }