evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. * Copyright (c) 2004-2005 MontaVista Software, Inc.
  4. * Copyright (c) 2006-2007, 2009 Red Hat, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@redhat.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 corosync executive
  38. */
  39. #include <config.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <pthread.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <errno.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/coroipc_types.h>
  49. #include <corosync/coroipcc.h>
  50. #include <corosync/corodefs.h>
  51. #include <corosync/hdb.h>
  52. #include <corosync/evs.h>
  53. #include <corosync/mar_gen.h>
  54. #include <corosync/ipc_evs.h>
  55. #include "util.h"
  56. #undef MIN
  57. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  58. struct evs_inst {
  59. void *ipc_ctx;
  60. int finalize;
  61. evs_callbacks_t callbacks;
  62. pthread_mutex_t response_mutex;
  63. pthread_mutex_t dispatch_mutex;
  64. };
  65. static void evs_instance_destructor (void *instance);
  66. DECLARE_HDB_DATABASE (evs_handle_t_db, evs_instance_destructor);
  67. /*
  68. * Clean up function for an evt instance (saEvtInitialize) handle
  69. */
  70. static void evs_instance_destructor (void *instance)
  71. {
  72. struct evs_inst *evs_inst = instance;
  73. pthread_mutex_destroy (&evs_inst->response_mutex);
  74. pthread_mutex_destroy (&evs_inst->dispatch_mutex);
  75. }
  76. /**
  77. * @defgroup evs_coroipcc The extended virtual synchrony passthrough API
  78. * @ingroup coroipcc
  79. *
  80. * @{
  81. */
  82. /**
  83. * test
  84. * @param handle The handle of evs initialize
  85. * @param callbacks The callbacks for evs_initialize
  86. * @returns EVS_OK
  87. */
  88. evs_error_t evs_initialize (
  89. evs_handle_t *handle,
  90. evs_callbacks_t *callbacks)
  91. {
  92. cs_error_t error;
  93. struct evs_inst *evs_inst;
  94. error = hdb_error_to_cs(hdb_handle_create (&evs_handle_t_db, sizeof (struct evs_inst), handle));
  95. if (error != CS_OK) {
  96. goto error_no_destroy;
  97. }
  98. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, *handle, (void *)&evs_inst));
  99. if (error != CS_OK) {
  100. goto error_destroy;
  101. }
  102. error = coroipcc_service_connect (
  103. COROSYNC_SOCKET_NAME,
  104. EVS_SERVICE,
  105. IPC_REQUEST_SIZE,
  106. IPC_RESPONSE_SIZE,
  107. IPC_DISPATCH_SIZE,
  108. &evs_inst->ipc_ctx);
  109. if (error != EVS_OK) {
  110. goto error_put_destroy;
  111. }
  112. memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
  113. pthread_mutex_init (&evs_inst->response_mutex, NULL);
  114. pthread_mutex_init (&evs_inst->dispatch_mutex, NULL);
  115. hdb_handle_put (&evs_handle_t_db, *handle);
  116. return (CS_OK);
  117. error_put_destroy:
  118. hdb_handle_put (&evs_handle_t_db, *handle);
  119. error_destroy:
  120. hdb_handle_destroy (&evs_handle_t_db, *handle);
  121. error_no_destroy:
  122. return (error);
  123. }
  124. evs_error_t evs_finalize (
  125. evs_handle_t handle)
  126. {
  127. struct evs_inst *evs_inst;
  128. cs_error_t error;
  129. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  130. if (error != CS_OK) {
  131. return (error);
  132. }
  133. pthread_mutex_lock (&evs_inst->response_mutex);
  134. /*
  135. * Another thread has already started finalizing
  136. */
  137. if (evs_inst->finalize) {
  138. pthread_mutex_unlock (&evs_inst->response_mutex);
  139. hdb_handle_put (&evs_handle_t_db, handle);
  140. return (EVS_ERR_BAD_HANDLE);
  141. }
  142. evs_inst->finalize = 1;
  143. coroipcc_service_disconnect (evs_inst->ipc_ctx);
  144. pthread_mutex_unlock (&evs_inst->response_mutex);
  145. hdb_handle_destroy (&evs_handle_t_db, handle);
  146. hdb_handle_put (&evs_handle_t_db, handle);
  147. return (EVS_OK);
  148. }
  149. evs_error_t evs_fd_get (
  150. evs_handle_t handle,
  151. int *fd)
  152. {
  153. cs_error_t error;
  154. struct evs_inst *evs_inst;
  155. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  156. if (error != CS_OK) {
  157. return (error);
  158. }
  159. *fd = coroipcc_fd_get (evs_inst->ipc_ctx);
  160. hdb_handle_put (&evs_handle_t_db, handle);
  161. return (CS_OK);
  162. }
  163. evs_error_t evs_dispatch (
  164. evs_handle_t handle,
  165. cs_dispatch_flags_t dispatch_types)
  166. {
  167. int timeout = -1;
  168. cs_error_t error;
  169. int cont = 1; /* always continue do loop except when set to 0 */
  170. int dispatch_avail;
  171. struct evs_inst *evs_inst;
  172. struct res_evs_confchg_callback *res_evs_confchg_callback;
  173. struct res_evs_deliver_callback *res_evs_deliver_callback;
  174. evs_callbacks_t callbacks;
  175. coroipc_response_header_t *dispatch_data;
  176. int ignore_dispatch = 0;
  177. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  178. if (error != CS_OK) {
  179. return (error);
  180. }
  181. /*
  182. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  183. * wait indefinately for SA_DISPATCH_BLOCKING
  184. */
  185. if (dispatch_types == EVS_DISPATCH_ALL) {
  186. timeout = 0;
  187. }
  188. do {
  189. pthread_mutex_lock (&evs_inst->dispatch_mutex);
  190. dispatch_avail = coroipcc_dispatch_get (
  191. evs_inst->ipc_ctx,
  192. (void **)&dispatch_data,
  193. timeout);
  194. pthread_mutex_unlock (&evs_inst->dispatch_mutex);
  195. if (dispatch_avail == 0 && dispatch_types == EVS_DISPATCH_ALL) {
  196. break; /* exit do while cont is 1 loop */
  197. } else
  198. if (dispatch_avail == 0) {
  199. continue; /* next dispatch event */
  200. }
  201. if (dispatch_avail == -1) {
  202. if (evs_inst->finalize == 1) {
  203. error = CS_OK;
  204. } else {
  205. error = CS_ERR_LIBRARY;
  206. }
  207. goto error_put;
  208. }
  209. /*
  210. * Make copy of callbacks, message data, unlock instance, and call callback
  211. * A risk of this dispatch method is that the callback routines may
  212. * operate at the same time that evsFinalize has been called.
  213. */
  214. memcpy (&callbacks, &evs_inst->callbacks, sizeof (evs_callbacks_t));
  215. /*
  216. * Dispatch incoming message
  217. */
  218. switch (dispatch_data->id) {
  219. case MESSAGE_RES_EVS_DELIVER_CALLBACK:
  220. res_evs_deliver_callback = (struct res_evs_deliver_callback *)dispatch_data;
  221. callbacks.evs_deliver_fn (
  222. res_evs_deliver_callback->local_nodeid,
  223. &res_evs_deliver_callback->msg,
  224. res_evs_deliver_callback->msglen);
  225. break;
  226. case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
  227. res_evs_confchg_callback = (struct res_evs_confchg_callback *)dispatch_data;
  228. callbacks.evs_confchg_fn (
  229. res_evs_confchg_callback->member_list,
  230. res_evs_confchg_callback->member_list_entries,
  231. res_evs_confchg_callback->left_list,
  232. res_evs_confchg_callback->left_list_entries,
  233. res_evs_confchg_callback->joined_list,
  234. res_evs_confchg_callback->joined_list_entries);
  235. break;
  236. default:
  237. coroipcc_dispatch_put (evs_inst->ipc_ctx);
  238. error = CS_ERR_LIBRARY;
  239. goto error_put;
  240. break;
  241. }
  242. coroipcc_dispatch_put (evs_inst->ipc_ctx);
  243. /*
  244. * Determine if more messages should be processed
  245. * */
  246. switch (dispatch_types) {
  247. case EVS_DISPATCH_ONE:
  248. if (ignore_dispatch) {
  249. ignore_dispatch = 0;
  250. } else {
  251. cont = 0;
  252. }
  253. break;
  254. case EVS_DISPATCH_ALL:
  255. if (ignore_dispatch) {
  256. ignore_dispatch = 0;
  257. }
  258. break;
  259. case EVS_DISPATCH_BLOCKING:
  260. break;
  261. }
  262. } while (cont);
  263. error_put:
  264. hdb_handle_put (&evs_handle_t_db, handle);
  265. return (error);
  266. }
  267. evs_error_t evs_join (
  268. evs_handle_t handle,
  269. const struct evs_group *groups,
  270. size_t group_entries)
  271. {
  272. evs_error_t error;
  273. struct evs_inst *evs_inst;
  274. struct iovec iov[2];
  275. struct req_lib_evs_join req_lib_evs_join;
  276. struct res_lib_evs_join res_lib_evs_join;
  277. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  278. if (error != EVS_OK) {
  279. return (error);
  280. }
  281. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  282. (group_entries * sizeof (struct evs_group));
  283. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  284. req_lib_evs_join.group_entries = group_entries;
  285. iov[0].iov_base = &req_lib_evs_join;
  286. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  287. iov[1].iov_base = (void*) groups; /* cast away const */
  288. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  289. pthread_mutex_lock (&evs_inst->response_mutex);
  290. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov, 2,
  291. &res_lib_evs_join, sizeof (struct res_lib_evs_join));
  292. pthread_mutex_unlock (&evs_inst->response_mutex);
  293. if (error != CS_OK) {
  294. goto error_exit;
  295. }
  296. error = res_lib_evs_join.header.error;
  297. error_exit:
  298. hdb_handle_put (&evs_handle_t_db, handle);
  299. return (error);
  300. }
  301. evs_error_t evs_leave (
  302. evs_handle_t handle,
  303. const struct evs_group *groups,
  304. size_t group_entries)
  305. {
  306. evs_error_t error;
  307. struct evs_inst *evs_inst;
  308. struct iovec iov[2];
  309. struct req_lib_evs_leave req_lib_evs_leave;
  310. struct res_lib_evs_leave res_lib_evs_leave;
  311. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  312. if (error != CS_OK) {
  313. return (error);
  314. }
  315. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  316. (group_entries * sizeof (struct evs_group));
  317. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  318. req_lib_evs_leave.group_entries = group_entries;
  319. iov[0].iov_base = &req_lib_evs_leave;
  320. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  321. iov[1].iov_base = (void *) groups; /* cast away const */
  322. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  323. pthread_mutex_lock (&evs_inst->response_mutex);
  324. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov, 2,
  325. &res_lib_evs_leave, sizeof (struct res_lib_evs_leave));
  326. pthread_mutex_unlock (&evs_inst->response_mutex);
  327. if (error != CS_OK) {
  328. goto error_exit;
  329. }
  330. error = res_lib_evs_leave.header.error;
  331. error_exit:
  332. hdb_handle_put (&evs_handle_t_db, handle);
  333. return (error);
  334. }
  335. evs_error_t evs_mcast_joined (
  336. evs_handle_t handle,
  337. evs_guarantee_t guarantee,
  338. const struct iovec *iovec,
  339. unsigned int iov_len)
  340. {
  341. int i;
  342. evs_error_t error;
  343. struct evs_inst *evs_inst;
  344. struct iovec iov[64];
  345. struct req_lib_evs_mcast_joined req_lib_evs_mcast_joined;
  346. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  347. size_t msg_len = 0;
  348. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  349. if (error != CS_OK) {
  350. return (error);
  351. }
  352. for (i = 0; i < iov_len; i++ ) {
  353. msg_len += iovec[i].iov_len;
  354. }
  355. req_lib_evs_mcast_joined.header.size = sizeof (struct req_lib_evs_mcast_joined) +
  356. msg_len;
  357. req_lib_evs_mcast_joined.header.id = MESSAGE_REQ_EVS_MCAST_JOINED;
  358. req_lib_evs_mcast_joined.guarantee = guarantee;
  359. req_lib_evs_mcast_joined.msg_len = msg_len;
  360. iov[0].iov_base = &req_lib_evs_mcast_joined;
  361. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_joined);
  362. memcpy (&iov[1], iovec, iov_len * sizeof (struct iovec));
  363. pthread_mutex_lock (&evs_inst->response_mutex);
  364. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov,
  365. iov_len + 1,
  366. &res_lib_evs_mcast_joined,
  367. sizeof (struct res_lib_evs_mcast_joined));
  368. pthread_mutex_unlock (&evs_inst->response_mutex);
  369. if (error != CS_OK) {
  370. goto error_exit;
  371. }
  372. error = res_lib_evs_mcast_joined.header.error;
  373. error_exit:
  374. hdb_handle_put (&evs_handle_t_db, handle);
  375. return (error);
  376. }
  377. evs_error_t evs_mcast_groups (
  378. evs_handle_t handle,
  379. evs_guarantee_t guarantee,
  380. const struct evs_group *groups,
  381. size_t group_entries,
  382. const struct iovec *iovec,
  383. unsigned int iov_len)
  384. {
  385. int i;
  386. evs_error_t error;
  387. struct evs_inst *evs_inst;
  388. struct iovec iov[64]; /* FIXME: what if iov_len > 62 ? use malloc */
  389. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  390. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  391. size_t msg_len = 0;
  392. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  393. if (error != CS_OK) {
  394. return (error);
  395. }
  396. for (i = 0; i < iov_len; i++) {
  397. msg_len += iovec[i].iov_len;
  398. }
  399. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  400. (group_entries * sizeof (struct evs_group)) + msg_len;
  401. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  402. req_lib_evs_mcast_groups.guarantee = guarantee;
  403. req_lib_evs_mcast_groups.msg_len = msg_len;
  404. req_lib_evs_mcast_groups.group_entries = group_entries;
  405. iov[0].iov_base = &req_lib_evs_mcast_groups;
  406. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  407. iov[1].iov_base = (void *) groups; /* cast away const */
  408. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  409. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  410. pthread_mutex_lock (&evs_inst->response_mutex);
  411. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx, iov,
  412. iov_len + 2,
  413. &res_lib_evs_mcast_groups,
  414. sizeof (struct res_lib_evs_mcast_groups));
  415. pthread_mutex_unlock (&evs_inst->response_mutex);
  416. if (error != CS_OK) {
  417. goto error_exit;
  418. }
  419. error = res_lib_evs_mcast_groups.header.error;
  420. error_exit:
  421. hdb_handle_put (&evs_handle_t_db, handle);
  422. return (error);
  423. }
  424. evs_error_t evs_membership_get (
  425. evs_handle_t handle,
  426. unsigned int *local_nodeid,
  427. unsigned int *member_list,
  428. size_t *member_list_entries)
  429. {
  430. evs_error_t error;
  431. struct evs_inst *evs_inst;
  432. struct iovec iov;
  433. struct req_lib_evs_membership_get req_lib_evs_membership_get;
  434. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  435. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  436. if (error != CS_OK) {
  437. return (error);
  438. }
  439. req_lib_evs_membership_get.header.size = sizeof (struct req_lib_evs_membership_get);
  440. req_lib_evs_membership_get.header.id = MESSAGE_REQ_EVS_MEMBERSHIP_GET;
  441. iov.iov_base = &req_lib_evs_membership_get;
  442. iov.iov_len = sizeof (struct req_lib_evs_membership_get);
  443. pthread_mutex_lock (&evs_inst->response_mutex);
  444. error = coroipcc_msg_send_reply_receive (evs_inst->ipc_ctx,
  445. &iov,
  446. 1,
  447. &res_lib_evs_membership_get,
  448. sizeof (struct res_lib_evs_membership_get));
  449. pthread_mutex_unlock (&evs_inst->response_mutex);
  450. if (error != CS_OK) {
  451. goto error_exit;
  452. }
  453. error = res_lib_evs_membership_get.header.error;
  454. /*
  455. * Copy results to caller
  456. */
  457. if (local_nodeid) {
  458. *local_nodeid = res_lib_evs_membership_get.local_nodeid;
  459. }
  460. *member_list_entries = MIN (*member_list_entries,
  461. res_lib_evs_membership_get.member_list_entries);
  462. if (member_list) {
  463. memcpy (member_list, &res_lib_evs_membership_get.member_list,
  464. *member_list_entries * sizeof (struct in_addr));
  465. }
  466. error_exit:
  467. hdb_handle_put (&evs_handle_t_db, handle);
  468. return (error);
  469. }
  470. /** @} */