evs.c 14 KB

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