evs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Copyright (c) 2004-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <sys/un.h>
  39. #include <sys/ioctl.h>
  40. #include <netinet/in.h>
  41. #include <sys/uio.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <time.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <corosync/swab.h>
  51. #include <corosync/corotypes.h>
  52. #include <corosync/coroipc_types.h>
  53. #include <corosync/corodefs.h>
  54. #include <corosync/mar_gen.h>
  55. #include <corosync/lcr/lcr_comp.h>
  56. #include <corosync/engine/coroapi.h>
  57. #include <corosync/engine/logsys.h>
  58. #include <corosync/list.h>
  59. #include <corosync/evs.h>
  60. #include <corosync/ipc_evs.h>
  61. LOGSYS_DECLARE_SUBSYS ("EVS");
  62. enum evs_exec_message_req_types {
  63. MESSAGE_REQ_EXEC_EVS_MCAST = 0
  64. };
  65. /*
  66. * Service Interfaces required by service_message_handler struct
  67. */
  68. static int evs_exec_init_fn (
  69. struct corosync_api_v1 *corosync_api);
  70. static void evs_confchg_fn (
  71. enum totem_configuration_type configuration_type,
  72. const unsigned int *member_list, size_t member_list_entries,
  73. const unsigned int *left_list, size_t left_list_entries,
  74. const unsigned int *joined_list, size_t joined_list_entries,
  75. const struct memb_ring_id *ring_id);
  76. static void message_handler_req_exec_mcast (const void *msg, unsigned int nodeid);
  77. static void req_exec_mcast_endian_convert (void *msg);
  78. static void message_handler_req_evs_join (void *conn, const void *msg);
  79. static void message_handler_req_evs_leave (void *conn, const void *msg);
  80. static void message_handler_req_evs_mcast_joined (void *conn, const void *msg);
  81. static void message_handler_req_evs_mcast_groups (void *conn, const void *msg);
  82. static void message_handler_req_evs_membership_get (void *conn, const void *msg);
  83. static int evs_lib_init_fn (void *conn);
  84. static int evs_lib_exit_fn (void *conn);
  85. struct evs_pd {
  86. struct evs_group *groups;
  87. int group_entries;
  88. struct list_head list;
  89. void *conn;
  90. };
  91. static struct corosync_api_v1 *api;
  92. static struct corosync_lib_handler evs_lib_engine[] =
  93. {
  94. { /* 0 */
  95. .lib_handler_fn = message_handler_req_evs_join,
  96. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  97. },
  98. { /* 1 */
  99. .lib_handler_fn = message_handler_req_evs_leave,
  100. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  101. },
  102. { /* 2 */
  103. .lib_handler_fn = message_handler_req_evs_mcast_joined,
  104. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  105. },
  106. { /* 3 */
  107. .lib_handler_fn = message_handler_req_evs_mcast_groups,
  108. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  109. },
  110. { /* 4 */
  111. .lib_handler_fn = message_handler_req_evs_membership_get,
  112. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  113. }
  114. };
  115. static struct corosync_exec_handler evs_exec_engine[] =
  116. {
  117. {
  118. .exec_handler_fn = message_handler_req_exec_mcast,
  119. .exec_endian_convert_fn = req_exec_mcast_endian_convert
  120. }
  121. };
  122. struct corosync_service_engine evs_service_engine = {
  123. .name = "corosync extended virtual synchrony service",
  124. .id = EVS_SERVICE,
  125. .private_data_size = sizeof (struct evs_pd),
  126. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  127. .lib_init_fn = evs_lib_init_fn,
  128. .lib_exit_fn = evs_lib_exit_fn,
  129. .lib_engine = evs_lib_engine,
  130. .lib_engine_count = sizeof (evs_lib_engine) / sizeof (struct corosync_lib_handler),
  131. .exec_engine = evs_exec_engine,
  132. .exec_engine_count = sizeof (evs_exec_engine) / sizeof (struct corosync_exec_handler),
  133. .confchg_fn = evs_confchg_fn,
  134. .exec_init_fn = evs_exec_init_fn,
  135. .exec_dump_fn = NULL
  136. };
  137. static DECLARE_LIST_INIT (confchg_notify);
  138. /*
  139. * Dynamic loading descriptor
  140. */
  141. static struct corosync_service_engine *evs_get_service_engine_ver0 (void);
  142. static struct corosync_service_engine_iface_ver0 evs_service_engine_iface = {
  143. .corosync_get_service_engine_ver0 = evs_get_service_engine_ver0
  144. };
  145. static struct lcr_iface corosync_evs_ver0[1] = {
  146. {
  147. .name = "corosync_evs",
  148. .version = 0,
  149. .versions_replace = 0,
  150. .versions_replace_count = 0,
  151. .dependencies = 0,
  152. .dependency_count = 0,
  153. .constructor = NULL,
  154. .destructor = NULL,
  155. .interfaces = NULL,
  156. }
  157. };
  158. static struct lcr_comp evs_comp_ver0 = {
  159. .iface_count = 1,
  160. .ifaces = corosync_evs_ver0
  161. };
  162. static struct corosync_service_engine *evs_get_service_engine_ver0 (void)
  163. {
  164. return (&evs_service_engine);
  165. }
  166. #ifdef COROSYNC_SOLARIS
  167. void corosync_lcr_component_register (void);
  168. void corosync_lcr_component_register (void) {
  169. #else
  170. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  171. #endif
  172. lcr_interfaces_set (&corosync_evs_ver0[0], &evs_service_engine_iface);
  173. lcr_component_register (&evs_comp_ver0);
  174. }
  175. static int evs_exec_init_fn (
  176. struct corosync_api_v1 *corosync_api)
  177. {
  178. api = corosync_api;
  179. return 0;
  180. }
  181. struct res_evs_confchg_callback res_evs_confchg_callback;
  182. static void evs_confchg_fn (
  183. enum totem_configuration_type configuration_type,
  184. const unsigned int *member_list, size_t member_list_entries,
  185. const unsigned int *left_list, size_t left_list_entries,
  186. const unsigned int *joined_list, size_t joined_list_entries,
  187. const struct memb_ring_id *ring_id)
  188. {
  189. struct list_head *list;
  190. struct evs_pd *evs_pd;
  191. /*
  192. * Build configuration change message
  193. */
  194. res_evs_confchg_callback.header.size = sizeof (struct res_evs_confchg_callback);
  195. res_evs_confchg_callback.header.id = MESSAGE_RES_EVS_CONFCHG_CALLBACK;
  196. res_evs_confchg_callback.header.error = CS_OK;
  197. memcpy (res_evs_confchg_callback.member_list,
  198. member_list, member_list_entries * sizeof(*member_list));
  199. res_evs_confchg_callback.member_list_entries = member_list_entries;
  200. memcpy (res_evs_confchg_callback.left_list,
  201. left_list, left_list_entries * sizeof(*left_list));
  202. res_evs_confchg_callback.left_list_entries = left_list_entries;
  203. memcpy (res_evs_confchg_callback.joined_list,
  204. joined_list, joined_list_entries * sizeof(*joined_list));
  205. res_evs_confchg_callback.joined_list_entries = joined_list_entries;
  206. /*
  207. * Send configuration change message to every EVS library user
  208. */
  209. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  210. evs_pd = list_entry (list, struct evs_pd, list);
  211. api->ipc_response_send (evs_pd->conn,
  212. &res_evs_confchg_callback,
  213. sizeof (res_evs_confchg_callback));
  214. }
  215. }
  216. static int evs_lib_init_fn (void *conn)
  217. {
  218. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  219. log_printf (LOGSYS_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  220. evs_pd->groups = NULL;
  221. evs_pd->group_entries = 0;
  222. evs_pd->conn = conn;
  223. list_init (&evs_pd->list);
  224. list_add (&evs_pd->list, &confchg_notify);
  225. api->ipc_dispatch_send (conn, &res_evs_confchg_callback,
  226. sizeof (res_evs_confchg_callback));
  227. return (0);
  228. }
  229. static int evs_lib_exit_fn (void *conn)
  230. {
  231. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  232. list_del (&evs_pd->list);
  233. return (0);
  234. }
  235. static void message_handler_req_evs_join (void *conn, const void *msg)
  236. {
  237. cs_error_t error = CS_OK;
  238. const struct req_lib_evs_join *req_lib_evs_join = msg;
  239. struct res_lib_evs_join res_lib_evs_join;
  240. void *addr;
  241. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  242. if (req_lib_evs_join->group_entries > 50) {
  243. error = CS_ERR_TOO_MANY_GROUPS;
  244. goto exit_error;
  245. }
  246. addr = realloc (evs_pd->groups, sizeof (struct evs_group) *
  247. (evs_pd->group_entries + req_lib_evs_join->group_entries));
  248. if (addr == NULL) {
  249. error = CS_ERR_NO_MEMORY;
  250. goto exit_error;
  251. }
  252. evs_pd->groups = addr;
  253. memcpy (&evs_pd->groups[evs_pd->group_entries],
  254. req_lib_evs_join->groups,
  255. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  256. evs_pd->group_entries += req_lib_evs_join->group_entries;
  257. exit_error:
  258. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  259. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  260. res_lib_evs_join.header.error = error;
  261. api->ipc_response_send (conn, &res_lib_evs_join,
  262. sizeof (struct res_lib_evs_join));
  263. }
  264. static void message_handler_req_evs_leave (void *conn, const void *msg)
  265. {
  266. const struct req_lib_evs_leave *req_lib_evs_leave = msg;
  267. struct res_lib_evs_leave res_lib_evs_leave;
  268. cs_error_t error = CS_OK;
  269. int error_index;
  270. int i, j;
  271. int found;
  272. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  273. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  274. found = 0;
  275. for (j = 0; j < evs_pd->group_entries;) {
  276. if (memcmp (&req_lib_evs_leave->groups[i],
  277. &evs_pd->groups[j], sizeof (struct evs_group)) == 0) {
  278. /*
  279. * Delete entry
  280. */
  281. memmove (&evs_pd->groups[j], &evs_pd->groups[j + 1],
  282. (evs_pd->group_entries - j - 1) * sizeof (struct evs_group));
  283. evs_pd->group_entries -= 1;
  284. found = 1;
  285. break;
  286. } else {
  287. j++;
  288. }
  289. }
  290. if (found == 0) {
  291. error = CS_ERR_NOT_EXIST;
  292. error_index = i;
  293. break;
  294. }
  295. }
  296. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  297. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  298. res_lib_evs_leave.header.error = error;
  299. api->ipc_response_send (conn, &res_lib_evs_leave,
  300. sizeof (struct res_lib_evs_leave));
  301. }
  302. static void message_handler_req_evs_mcast_joined (void *conn, const void *msg)
  303. {
  304. cs_error_t error = CS_ERR_TRY_AGAIN;
  305. const struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = msg;
  306. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  307. struct iovec req_exec_evs_mcast_iovec[3];
  308. struct req_exec_evs_mcast req_exec_evs_mcast;
  309. int res;
  310. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  311. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  312. evs_pd->group_entries * sizeof (struct evs_group) +
  313. req_lib_evs_mcast_joined->msg_len;
  314. req_exec_evs_mcast.header.id =
  315. SERVICE_ID_MAKE (EVS_SERVICE, MESSAGE_REQ_EXEC_EVS_MCAST);
  316. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  317. req_exec_evs_mcast.group_entries = evs_pd->group_entries;
  318. req_exec_evs_mcast_iovec[0].iov_base = (char *)&req_exec_evs_mcast;
  319. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  320. req_exec_evs_mcast_iovec[1].iov_base = (char *)evs_pd->groups;
  321. req_exec_evs_mcast_iovec[1].iov_len = evs_pd->group_entries * sizeof (struct evs_group);
  322. req_exec_evs_mcast_iovec[2].iov_base = (char *)&req_lib_evs_mcast_joined->msg;
  323. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  324. res = api->totem_mcast (req_exec_evs_mcast_iovec, 3, TOTEM_AGREED);
  325. // TODO
  326. if (res == 0) {
  327. error = CS_OK;
  328. }
  329. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  330. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  331. res_lib_evs_mcast_joined.header.error = error;
  332. api->ipc_response_send (conn, &res_lib_evs_mcast_joined,
  333. sizeof (struct res_lib_evs_mcast_joined));
  334. }
  335. static void message_handler_req_evs_mcast_groups (void *conn, const void *msg)
  336. {
  337. cs_error_t error = CS_ERR_TRY_AGAIN;
  338. const struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = msg;
  339. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  340. struct iovec req_exec_evs_mcast_iovec[3];
  341. struct req_exec_evs_mcast req_exec_evs_mcast;
  342. const char *msg_addr;
  343. int res;
  344. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  345. sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries +
  346. req_lib_evs_mcast_groups->msg_len;
  347. req_exec_evs_mcast.header.id =
  348. SERVICE_ID_MAKE (EVS_SERVICE, MESSAGE_REQ_EXEC_EVS_MCAST);
  349. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  350. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  351. msg_addr = (const char *)req_lib_evs_mcast_groups +
  352. sizeof (struct req_lib_evs_mcast_groups) +
  353. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  354. req_exec_evs_mcast_iovec[0].iov_base = (char *)&req_exec_evs_mcast;
  355. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  356. req_exec_evs_mcast_iovec[1].iov_base = (char *)&req_lib_evs_mcast_groups->groups;
  357. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  358. req_exec_evs_mcast_iovec[2].iov_base = (void *) msg_addr; /* discard const */
  359. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  360. res = api->totem_mcast (req_exec_evs_mcast_iovec, 3, TOTEM_AGREED);
  361. if (res == 0) {
  362. error = CS_OK;
  363. }
  364. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  365. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  366. res_lib_evs_mcast_groups.header.error = error;
  367. api->ipc_response_send (conn, &res_lib_evs_mcast_groups,
  368. sizeof (struct res_lib_evs_mcast_groups));
  369. }
  370. static void message_handler_req_evs_membership_get (void *conn, const void *msg)
  371. {
  372. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  373. res_lib_evs_membership_get.header.size = sizeof (struct res_lib_evs_membership_get);
  374. res_lib_evs_membership_get.header.id = MESSAGE_RES_EVS_MEMBERSHIP_GET;
  375. res_lib_evs_membership_get.header.error = CS_OK;
  376. res_lib_evs_membership_get.local_nodeid = api->totem_nodeid_get ();
  377. memcpy (&res_lib_evs_membership_get.member_list,
  378. &res_evs_confchg_callback.member_list,
  379. sizeof (res_lib_evs_membership_get.member_list));
  380. res_lib_evs_membership_get.member_list_entries =
  381. res_evs_confchg_callback.member_list_entries;
  382. api->ipc_response_send (conn, &res_lib_evs_membership_get,
  383. sizeof (struct res_lib_evs_membership_get));
  384. }
  385. static void req_exec_mcast_endian_convert (void *msg)
  386. {
  387. struct req_exec_evs_mcast *req_exec_evs_mcast =
  388. (struct req_exec_evs_mcast *)msg;
  389. req_exec_evs_mcast->group_entries =
  390. swab32 (req_exec_evs_mcast->group_entries);
  391. req_exec_evs_mcast->msg_len = swab32 (req_exec_evs_mcast->msg_len);
  392. }
  393. static void message_handler_req_exec_mcast (
  394. const void *msg,
  395. unsigned int nodeid)
  396. {
  397. const struct req_exec_evs_mcast *req_exec_evs_mcast = msg;
  398. struct res_evs_deliver_callback res_evs_deliver_callback;
  399. const char *msg_addr;
  400. struct list_head *list;
  401. int found = 0;
  402. int i, j;
  403. struct evs_pd *evs_pd;
  404. struct iovec iov[2];
  405. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  406. req_exec_evs_mcast->msg_len;
  407. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  408. res_evs_deliver_callback.header.error = CS_OK;
  409. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  410. msg_addr = (const char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  411. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  412. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  413. found = 0;
  414. evs_pd = list_entry (list, struct evs_pd, list);
  415. for (i = 0; i < evs_pd->group_entries; i++) {
  416. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  417. if (memcmp (&evs_pd->groups[i], &req_exec_evs_mcast->groups[j],
  418. sizeof (struct evs_group)) == 0) {
  419. found = 1;
  420. break;
  421. }
  422. }
  423. if (found) {
  424. break;
  425. }
  426. }
  427. if (found) {
  428. res_evs_deliver_callback.local_nodeid = nodeid;
  429. iov[0].iov_base = &res_evs_deliver_callback;
  430. iov[0].iov_len = sizeof (struct res_evs_deliver_callback);
  431. iov[1].iov_base = (void *) msg_addr; /* discard const */
  432. iov[1].iov_len = req_exec_evs_mcast->msg_len;
  433. api->ipc_dispatch_iov_send (
  434. evs_pd->conn,
  435. iov,
  436. 2);
  437. }
  438. }
  439. }