evs.c 14 KB

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