evs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 <qb/qbipcc.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/corodefs.h>
  49. #include <corosync/hdb.h>
  50. #include <corosync/evs.h>
  51. #include <corosync/ipc_evs.h>
  52. #include "util.h"
  53. struct evs_inst {
  54. qb_ipcc_connection_t *c;
  55. int finalize;
  56. evs_callbacks_t callbacks;
  57. void *context;
  58. };
  59. DECLARE_HDB_DATABASE (evs_handle_t_db,NULL);
  60. /*
  61. * Clean up function for an evt instance (saEvtInitialize) handle
  62. */
  63. /**
  64. * @defgroup evs_coroipcc The extended virtual synchrony passthrough API
  65. * @ingroup coroipcc
  66. *
  67. * @{
  68. */
  69. /**
  70. * test
  71. * @param handle The handle of evs initialize
  72. * @param callbacks The callbacks for evs_initialize
  73. * @returns EVS_OK
  74. */
  75. evs_error_t evs_initialize (
  76. evs_handle_t *handle,
  77. evs_callbacks_t *callbacks)
  78. {
  79. cs_error_t error;
  80. struct evs_inst *evs_inst;
  81. error = hdb_error_to_cs(hdb_handle_create (&evs_handle_t_db, sizeof (struct evs_inst), handle));
  82. if (error != CS_OK) {
  83. goto error_no_destroy;
  84. }
  85. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, *handle, (void *)&evs_inst));
  86. if (error != CS_OK) {
  87. goto error_destroy;
  88. }
  89. evs_inst->c = qb_ipcc_connect ("evs", IPC_REQUEST_SIZE);
  90. if (evs_inst->c == NULL) {
  91. error = qb_to_cs_error(-errno);
  92. goto error_put_destroy;
  93. }
  94. if (error != EVS_OK) {
  95. goto error_put_destroy;
  96. }
  97. if (callbacks) {
  98. memcpy (&evs_inst->callbacks, callbacks, sizeof (evs_callbacks_t));
  99. }
  100. hdb_handle_put (&evs_handle_t_db, *handle);
  101. return (CS_OK);
  102. error_put_destroy:
  103. hdb_handle_put (&evs_handle_t_db, *handle);
  104. error_destroy:
  105. hdb_handle_destroy (&evs_handle_t_db, *handle);
  106. error_no_destroy:
  107. return (error);
  108. }
  109. evs_error_t evs_finalize (
  110. evs_handle_t handle)
  111. {
  112. struct evs_inst *evs_inst;
  113. cs_error_t error;
  114. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  115. if (error != CS_OK) {
  116. return (error);
  117. }
  118. /*
  119. * Another thread has already started finalizing
  120. */
  121. if (evs_inst->finalize) {
  122. hdb_handle_put (&evs_handle_t_db, handle);
  123. return (EVS_ERR_BAD_HANDLE);
  124. }
  125. evs_inst->finalize = 1;
  126. qb_ipcc_disconnect (evs_inst->c);
  127. hdb_handle_destroy (&evs_handle_t_db, handle);
  128. hdb_handle_put (&evs_handle_t_db, handle);
  129. return (EVS_OK);
  130. }
  131. evs_error_t evs_fd_get (
  132. evs_handle_t handle,
  133. int *fd)
  134. {
  135. cs_error_t error;
  136. struct evs_inst *evs_inst;
  137. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  138. if (error != CS_OK) {
  139. return (error);
  140. }
  141. qb_ipcc_fd_get (evs_inst->c, fd);
  142. hdb_handle_put (&evs_handle_t_db, handle);
  143. return (CS_OK);
  144. }
  145. evs_error_t evs_context_get (
  146. evs_handle_t handle,
  147. void **context)
  148. {
  149. cs_error_t error;
  150. struct evs_inst *evs_inst;
  151. error = hdb_error_to_cs (hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  152. if (error != CS_OK) {
  153. return (error);
  154. }
  155. *context = evs_inst->context;
  156. hdb_handle_put (&evs_handle_t_db, handle);
  157. return (CS_OK);
  158. }
  159. cs_error_t evs_context_set (
  160. evs_handle_t handle,
  161. void *context)
  162. {
  163. cs_error_t error;
  164. struct evs_inst *evs_inst;
  165. error = hdb_error_to_cs (hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  166. if (error != CS_OK) {
  167. return (error);
  168. }
  169. evs_inst->context = context;
  170. hdb_handle_put (&evs_handle_t_db, handle);
  171. return (CS_OK);
  172. }
  173. evs_error_t evs_dispatch (
  174. evs_handle_t handle,
  175. cs_dispatch_flags_t dispatch_types)
  176. {
  177. int timeout = -1;
  178. cs_error_t error;
  179. int cont = 1; /* always continue do loop except when set to 0 */
  180. struct evs_inst *evs_inst;
  181. struct res_evs_confchg_callback *res_evs_confchg_callback;
  182. struct res_evs_deliver_callback *res_evs_deliver_callback;
  183. evs_callbacks_t callbacks;
  184. struct qb_ipc_response_header *dispatch_data;
  185. char dispatch_buf[IPC_DISPATCH_SIZE];
  186. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  187. if (error != CS_OK) {
  188. return (error);
  189. }
  190. /*
  191. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  192. * wait indefinately for CS_DISPATCH_BLOCKING
  193. */
  194. if (dispatch_types == EVS_DISPATCH_ALL) {
  195. timeout = 0;
  196. }
  197. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  198. do {
  199. error = qb_to_cs_error(qb_ipcc_event_recv (
  200. evs_inst->c,
  201. dispatch_buf,
  202. IPC_DISPATCH_SIZE,
  203. timeout));
  204. if (error == CS_ERR_BAD_HANDLE) {
  205. error = CS_OK;
  206. goto error_put;
  207. }
  208. if (error == CS_ERR_TRY_AGAIN) {
  209. error = CS_OK;
  210. if (dispatch_types == CPG_DISPATCH_ALL) {
  211. break; /* exit do while cont is 1 loop */
  212. } else {
  213. continue; /* next poll */
  214. }
  215. }
  216. if (error != CS_OK) {
  217. goto error_put;
  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. if (callbacks.evs_deliver_fn == NULL) {
  231. break;
  232. }
  233. res_evs_deliver_callback = (struct res_evs_deliver_callback *)dispatch_data;
  234. callbacks.evs_deliver_fn (
  235. handle,
  236. res_evs_deliver_callback->local_nodeid,
  237. &res_evs_deliver_callback->msg,
  238. res_evs_deliver_callback->msglen);
  239. break;
  240. case MESSAGE_RES_EVS_CONFCHG_CALLBACK:
  241. if (callbacks.evs_confchg_fn == NULL) {
  242. break;
  243. }
  244. res_evs_confchg_callback = (struct res_evs_confchg_callback *)dispatch_data;
  245. callbacks.evs_confchg_fn (
  246. handle,
  247. res_evs_confchg_callback->member_list,
  248. res_evs_confchg_callback->member_list_entries,
  249. res_evs_confchg_callback->left_list,
  250. res_evs_confchg_callback->left_list_entries,
  251. res_evs_confchg_callback->joined_list,
  252. res_evs_confchg_callback->joined_list_entries,
  253. NULL);
  254. break;
  255. default:
  256. error = CS_ERR_LIBRARY;
  257. goto error_put;
  258. break;
  259. }
  260. /*
  261. * Determine if more messages should be processed
  262. */
  263. if (dispatch_types == CS_DISPATCH_ONE) {
  264. cont = 0;
  265. }
  266. } while (cont);
  267. error_put:
  268. hdb_handle_put (&evs_handle_t_db, handle);
  269. return (error);
  270. }
  271. evs_error_t evs_join (
  272. evs_handle_t handle,
  273. const struct evs_group *groups,
  274. size_t group_entries)
  275. {
  276. evs_error_t error;
  277. struct evs_inst *evs_inst;
  278. struct iovec iov[2];
  279. struct req_lib_evs_join req_lib_evs_join;
  280. struct res_lib_evs_join res_lib_evs_join;
  281. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  282. if (error != EVS_OK) {
  283. return (error);
  284. }
  285. req_lib_evs_join.header.size = sizeof (struct req_lib_evs_join) +
  286. (group_entries * sizeof (struct evs_group));
  287. req_lib_evs_join.header.id = MESSAGE_REQ_EVS_JOIN;
  288. req_lib_evs_join.group_entries = group_entries;
  289. iov[0].iov_base = (void *)&req_lib_evs_join;
  290. iov[0].iov_len = sizeof (struct req_lib_evs_join);
  291. iov[1].iov_base = (void*) groups; /* cast away const */
  292. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  293. error = qb_to_cs_error(qb_ipcc_sendv_recv (evs_inst->c, iov, 2,
  294. &res_lib_evs_join, sizeof (struct res_lib_evs_join), -1));
  295. if (error != CS_OK) {
  296. goto error_exit;
  297. }
  298. error = res_lib_evs_join.header.error;
  299. error_exit:
  300. hdb_handle_put (&evs_handle_t_db, handle);
  301. return (error);
  302. }
  303. evs_error_t evs_leave (
  304. evs_handle_t handle,
  305. const struct evs_group *groups,
  306. size_t group_entries)
  307. {
  308. evs_error_t error;
  309. struct evs_inst *evs_inst;
  310. struct iovec iov[2];
  311. struct req_lib_evs_leave req_lib_evs_leave;
  312. struct res_lib_evs_leave res_lib_evs_leave;
  313. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  314. if (error != CS_OK) {
  315. return (error);
  316. }
  317. req_lib_evs_leave.header.size = sizeof (struct req_lib_evs_leave) +
  318. (group_entries * sizeof (struct evs_group));
  319. req_lib_evs_leave.header.id = MESSAGE_REQ_EVS_LEAVE;
  320. req_lib_evs_leave.group_entries = group_entries;
  321. iov[0].iov_base = (void *)&req_lib_evs_leave;
  322. iov[0].iov_len = sizeof (struct req_lib_evs_leave);
  323. iov[1].iov_base = (void *) groups; /* cast away const */
  324. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  325. error = qb_to_cs_error(qb_ipcc_sendv_recv (evs_inst->c, iov, 2,
  326. &res_lib_evs_leave, sizeof (struct res_lib_evs_leave), -1));
  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 = (void *)&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. error = qb_to_cs_error(qb_ipcc_sendv_recv (evs_inst->c, iov,
  364. iov_len + 1,
  365. &res_lib_evs_mcast_joined,
  366. sizeof (struct res_lib_evs_mcast_joined), -1));
  367. if (error != CS_OK) {
  368. goto error_exit;
  369. }
  370. error = res_lib_evs_mcast_joined.header.error;
  371. error_exit:
  372. hdb_handle_put (&evs_handle_t_db, handle);
  373. return (error);
  374. }
  375. evs_error_t evs_mcast_groups (
  376. evs_handle_t handle,
  377. evs_guarantee_t guarantee,
  378. const struct evs_group *groups,
  379. size_t group_entries,
  380. const struct iovec *iovec,
  381. unsigned int iov_len)
  382. {
  383. int i;
  384. evs_error_t error;
  385. struct evs_inst *evs_inst;
  386. struct iovec iov[64]; /* FIXME: what if iov_len > 62 ? use malloc */
  387. struct req_lib_evs_mcast_groups req_lib_evs_mcast_groups;
  388. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  389. size_t msg_len = 0;
  390. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  391. if (error != CS_OK) {
  392. return (error);
  393. }
  394. for (i = 0; i < iov_len; i++) {
  395. msg_len += iovec[i].iov_len;
  396. }
  397. req_lib_evs_mcast_groups.header.size = sizeof (struct req_lib_evs_mcast_groups) +
  398. (group_entries * sizeof (struct evs_group)) + msg_len;
  399. req_lib_evs_mcast_groups.header.id = MESSAGE_REQ_EVS_MCAST_GROUPS;
  400. req_lib_evs_mcast_groups.guarantee = guarantee;
  401. req_lib_evs_mcast_groups.msg_len = msg_len;
  402. req_lib_evs_mcast_groups.group_entries = group_entries;
  403. iov[0].iov_base = (void *)&req_lib_evs_mcast_groups;
  404. iov[0].iov_len = sizeof (struct req_lib_evs_mcast_groups);
  405. iov[1].iov_base = (void *) groups; /* cast away const */
  406. iov[1].iov_len = (group_entries * sizeof (struct evs_group));
  407. memcpy (&iov[2], iovec, iov_len * sizeof (struct iovec));
  408. error = qb_to_cs_error(qb_ipcc_sendv_recv (evs_inst->c, iov,
  409. iov_len + 2,
  410. &res_lib_evs_mcast_groups,
  411. sizeof (struct res_lib_evs_mcast_groups), -1));
  412. if (error != CS_OK) {
  413. goto error_exit;
  414. }
  415. error = res_lib_evs_mcast_groups.header.error;
  416. error_exit:
  417. hdb_handle_put (&evs_handle_t_db, handle);
  418. return (error);
  419. }
  420. evs_error_t evs_membership_get (
  421. evs_handle_t handle,
  422. unsigned int *local_nodeid,
  423. unsigned int *member_list,
  424. size_t *member_list_entries)
  425. {
  426. evs_error_t error;
  427. struct evs_inst *evs_inst;
  428. struct iovec iov;
  429. struct req_lib_evs_membership_get req_lib_evs_membership_get;
  430. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  431. error = hdb_error_to_cs(hdb_handle_get (&evs_handle_t_db, handle, (void *)&evs_inst));
  432. if (error != CS_OK) {
  433. return (error);
  434. }
  435. req_lib_evs_membership_get.header.size = sizeof (struct req_lib_evs_membership_get);
  436. req_lib_evs_membership_get.header.id = MESSAGE_REQ_EVS_MEMBERSHIP_GET;
  437. iov.iov_base = (void *)&req_lib_evs_membership_get;
  438. iov.iov_len = sizeof (struct req_lib_evs_membership_get);
  439. error = qb_to_cs_error(qb_ipcc_sendv_recv (evs_inst->c,
  440. &iov,
  441. 1,
  442. &res_lib_evs_membership_get,
  443. sizeof (struct res_lib_evs_membership_get), -1));
  444. if (error != CS_OK) {
  445. goto error_exit;
  446. }
  447. error = res_lib_evs_membership_get.header.error;
  448. /*
  449. * Copy results to caller
  450. */
  451. if (local_nodeid) {
  452. *local_nodeid = res_lib_evs_membership_get.local_nodeid;
  453. }
  454. *member_list_entries = QB_MIN (*member_list_entries,
  455. res_lib_evs_membership_get.member_list_entries);
  456. if (member_list) {
  457. memcpy (member_list, &res_lib_evs_membership_get.member_list,
  458. *member_list_entries * sizeof (struct in_addr));
  459. }
  460. error_exit:
  461. hdb_handle_put (&evs_handle_t_db, handle);
  462. return (error);
  463. }
  464. /** @} */