evs.c 15 KB

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