evs.c 15 KB

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