evs.c 16 KB

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