evs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2004-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 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 <assert.h>
  48. #include <time.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <corosync/swab.h>
  52. #include <corosync/corotypes.h>
  53. #include <qb/qbipc_common.h>
  54. #include <corosync/corodefs.h>
  55. #include <corosync/mar_gen.h>
  56. #include <corosync/coroapi.h>
  57. #include <corosync/logsys.h>
  58. #include <corosync/list.h>
  59. #include <corosync/evs.h>
  60. #include <corosync/ipc_evs.h>
  61. #include "service.h"
  62. LOGSYS_DECLARE_SUBSYS ("EVS");
  63. enum evs_exec_message_req_types {
  64. MESSAGE_REQ_EXEC_EVS_MCAST = 0
  65. };
  66. /*
  67. * Service Interfaces required by service_message_handler struct
  68. */
  69. static char *evs_exec_init_fn (
  70. struct corosync_api_v1 *corosync_api);
  71. static void evs_confchg_fn (
  72. enum totem_configuration_type configuration_type,
  73. const unsigned int *member_list, size_t member_list_entries,
  74. const unsigned int *left_list, size_t left_list_entries,
  75. const unsigned int *joined_list, size_t joined_list_entries,
  76. const struct memb_ring_id *ring_id);
  77. static void message_handler_req_exec_mcast (const void *msg, unsigned int nodeid);
  78. static void req_exec_mcast_endian_convert (void *msg);
  79. static void message_handler_req_evs_join (void *conn, const void *msg);
  80. static void message_handler_req_evs_leave (void *conn, const void *msg);
  81. static void message_handler_req_evs_mcast_joined (void *conn, const void *msg);
  82. static void message_handler_req_evs_mcast_groups (void *conn, const void *msg);
  83. static void message_handler_req_evs_membership_get (void *conn, const void *msg);
  84. static int evs_lib_init_fn (void *conn);
  85. static int evs_lib_exit_fn (void *conn);
  86. struct evs_pd {
  87. struct evs_group *groups;
  88. int group_entries;
  89. struct list_head list;
  90. void *conn;
  91. };
  92. static struct corosync_api_v1 *api;
  93. static struct corosync_lib_handler evs_lib_engine[] =
  94. {
  95. { /* 0 */
  96. .lib_handler_fn = message_handler_req_evs_join,
  97. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  98. },
  99. { /* 1 */
  100. .lib_handler_fn = message_handler_req_evs_leave,
  101. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  102. },
  103. { /* 2 */
  104. .lib_handler_fn = message_handler_req_evs_mcast_joined,
  105. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  106. },
  107. { /* 3 */
  108. .lib_handler_fn = message_handler_req_evs_mcast_groups,
  109. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  110. },
  111. { /* 4 */
  112. .lib_handler_fn = message_handler_req_evs_membership_get,
  113. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  114. }
  115. };
  116. static struct corosync_exec_handler evs_exec_engine[] =
  117. {
  118. {
  119. .exec_handler_fn = message_handler_req_exec_mcast,
  120. .exec_endian_convert_fn = req_exec_mcast_endian_convert
  121. }
  122. };
  123. struct corosync_service_engine evs_service_engine = {
  124. .name = "corosync extended virtual synchrony service",
  125. .id = EVS_SERVICE,
  126. .priority = 1,
  127. .private_data_size = sizeof (struct evs_pd),
  128. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  129. .lib_init_fn = evs_lib_init_fn,
  130. .lib_exit_fn = evs_lib_exit_fn,
  131. .lib_engine = evs_lib_engine,
  132. .lib_engine_count = sizeof (evs_lib_engine) / sizeof (struct corosync_lib_handler),
  133. .exec_engine = evs_exec_engine,
  134. .exec_engine_count = sizeof (evs_exec_engine) / sizeof (struct corosync_exec_handler),
  135. .confchg_fn = evs_confchg_fn,
  136. .exec_init_fn = evs_exec_init_fn,
  137. .exec_dump_fn = NULL
  138. };
  139. static DECLARE_LIST_INIT (confchg_notify);
  140. struct corosync_service_engine *evs_get_service_engine_ver0 (void)
  141. {
  142. return (&evs_service_engine);
  143. }
  144. static char *evs_exec_init_fn (
  145. struct corosync_api_v1 *corosync_api)
  146. {
  147. #ifdef COROSYNC_SOLARIS
  148. logsys_subsys_init();
  149. #endif
  150. api = corosync_api;
  151. return NULL;
  152. }
  153. struct res_evs_confchg_callback res_evs_confchg_callback;
  154. static void evs_confchg_fn (
  155. enum totem_configuration_type configuration_type,
  156. const unsigned int *member_list, size_t member_list_entries,
  157. const unsigned int *left_list, size_t left_list_entries,
  158. const unsigned int *joined_list, size_t joined_list_entries,
  159. const struct memb_ring_id *ring_id)
  160. {
  161. struct list_head *list;
  162. struct evs_pd *evs_pd;
  163. /*
  164. * Build configuration change message
  165. */
  166. res_evs_confchg_callback.header.size = sizeof (struct res_evs_confchg_callback);
  167. res_evs_confchg_callback.header.id = MESSAGE_RES_EVS_CONFCHG_CALLBACK;
  168. res_evs_confchg_callback.header.error = CS_OK;
  169. memcpy (res_evs_confchg_callback.member_list,
  170. member_list, member_list_entries * sizeof(*member_list));
  171. res_evs_confchg_callback.member_list_entries = member_list_entries;
  172. memcpy (res_evs_confchg_callback.left_list,
  173. left_list, left_list_entries * sizeof(*left_list));
  174. res_evs_confchg_callback.left_list_entries = left_list_entries;
  175. memcpy (res_evs_confchg_callback.joined_list,
  176. joined_list, joined_list_entries * sizeof(*joined_list));
  177. res_evs_confchg_callback.joined_list_entries = joined_list_entries;
  178. /*
  179. * Send configuration change message to every EVS library user
  180. */
  181. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  182. evs_pd = list_entry (list, struct evs_pd, list);
  183. api->ipc_dispatch_send (evs_pd->conn,
  184. &res_evs_confchg_callback,
  185. sizeof (res_evs_confchg_callback));
  186. }
  187. }
  188. static int evs_lib_init_fn (void *conn)
  189. {
  190. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  191. log_printf (LOGSYS_LEVEL_DEBUG, "Got request to initalize evs service.");
  192. evs_pd->groups = NULL;
  193. evs_pd->group_entries = 0;
  194. evs_pd->conn = conn;
  195. list_init (&evs_pd->list);
  196. list_add (&evs_pd->list, &confchg_notify);
  197. api->ipc_dispatch_send (conn, &res_evs_confchg_callback,
  198. sizeof (res_evs_confchg_callback));
  199. return (0);
  200. }
  201. static int evs_lib_exit_fn (void *conn)
  202. {
  203. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  204. list_del (&evs_pd->list);
  205. return (0);
  206. }
  207. static void message_handler_req_evs_join (void *conn, const void *msg)
  208. {
  209. cs_error_t error = CS_OK;
  210. const struct req_lib_evs_join *req_lib_evs_join = msg;
  211. struct res_lib_evs_join res_lib_evs_join;
  212. void *addr;
  213. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  214. if (req_lib_evs_join->group_entries > 50) {
  215. error = CS_ERR_TOO_MANY_GROUPS;
  216. goto exit_error;
  217. }
  218. addr = realloc (evs_pd->groups, sizeof (struct evs_group) *
  219. (evs_pd->group_entries + req_lib_evs_join->group_entries));
  220. if (addr == NULL) {
  221. error = CS_ERR_NO_MEMORY;
  222. goto exit_error;
  223. }
  224. evs_pd->groups = addr;
  225. memcpy (&evs_pd->groups[evs_pd->group_entries],
  226. req_lib_evs_join->groups,
  227. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  228. evs_pd->group_entries += req_lib_evs_join->group_entries;
  229. exit_error:
  230. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  231. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  232. res_lib_evs_join.header.error = error;
  233. api->ipc_response_send (conn, &res_lib_evs_join,
  234. sizeof (struct res_lib_evs_join));
  235. }
  236. static void message_handler_req_evs_leave (void *conn, const void *msg)
  237. {
  238. const struct req_lib_evs_leave *req_lib_evs_leave = msg;
  239. struct res_lib_evs_leave res_lib_evs_leave;
  240. cs_error_t error = CS_OK;
  241. int i, j;
  242. int found;
  243. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  244. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  245. found = 0;
  246. for (j = 0; j < evs_pd->group_entries;) {
  247. if (memcmp (&req_lib_evs_leave->groups[i],
  248. &evs_pd->groups[j], sizeof (struct evs_group)) == 0) {
  249. /*
  250. * Delete entry
  251. */
  252. memmove (&evs_pd->groups[j], &evs_pd->groups[j + 1],
  253. (evs_pd->group_entries - j - 1) * sizeof (struct evs_group));
  254. evs_pd->group_entries -= 1;
  255. found = 1;
  256. break;
  257. } else {
  258. j++;
  259. }
  260. }
  261. if (found == 0) {
  262. error = CS_ERR_NOT_EXIST;
  263. break;
  264. }
  265. }
  266. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  267. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  268. res_lib_evs_leave.header.error = error;
  269. api->ipc_response_send (conn, &res_lib_evs_leave,
  270. sizeof (struct res_lib_evs_leave));
  271. }
  272. static void message_handler_req_evs_mcast_joined (void *conn, const void *msg)
  273. {
  274. cs_error_t error = CS_ERR_TRY_AGAIN;
  275. const struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = msg;
  276. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  277. struct iovec req_exec_evs_mcast_iovec[3];
  278. struct req_exec_evs_mcast req_exec_evs_mcast;
  279. int res;
  280. struct evs_pd *evs_pd = (struct evs_pd *)api->ipc_private_data_get (conn);
  281. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  282. evs_pd->group_entries * sizeof (struct evs_group) +
  283. req_lib_evs_mcast_joined->msg_len;
  284. req_exec_evs_mcast.header.id =
  285. SERVICE_ID_MAKE (EVS_SERVICE, MESSAGE_REQ_EXEC_EVS_MCAST);
  286. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  287. req_exec_evs_mcast.group_entries = evs_pd->group_entries;
  288. req_exec_evs_mcast_iovec[0].iov_base = (char *)&req_exec_evs_mcast;
  289. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  290. req_exec_evs_mcast_iovec[1].iov_base = (char *)evs_pd->groups;
  291. req_exec_evs_mcast_iovec[1].iov_len = evs_pd->group_entries * sizeof (struct evs_group);
  292. req_exec_evs_mcast_iovec[2].iov_base = (char *)&req_lib_evs_mcast_joined->msg;
  293. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  294. res = api->totem_mcast (req_exec_evs_mcast_iovec, 3, TOTEM_AGREED);
  295. // TODO
  296. if (res == 0) {
  297. error = CS_OK;
  298. }
  299. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  300. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  301. res_lib_evs_mcast_joined.header.error = error;
  302. api->ipc_response_send (conn, &res_lib_evs_mcast_joined,
  303. sizeof (struct res_lib_evs_mcast_joined));
  304. }
  305. static void message_handler_req_evs_mcast_groups (void *conn, const void *msg)
  306. {
  307. cs_error_t error = CS_ERR_TRY_AGAIN;
  308. const struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = msg;
  309. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  310. struct iovec req_exec_evs_mcast_iovec[3];
  311. struct req_exec_evs_mcast req_exec_evs_mcast;
  312. const char *msg_addr;
  313. int res;
  314. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  315. sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries +
  316. req_lib_evs_mcast_groups->msg_len;
  317. req_exec_evs_mcast.header.id =
  318. SERVICE_ID_MAKE (EVS_SERVICE, MESSAGE_REQ_EXEC_EVS_MCAST);
  319. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  320. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  321. msg_addr = (const char *)req_lib_evs_mcast_groups +
  322. sizeof (struct req_lib_evs_mcast_groups) +
  323. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  324. req_exec_evs_mcast_iovec[0].iov_base = (char *)&req_exec_evs_mcast;
  325. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  326. req_exec_evs_mcast_iovec[1].iov_base = (char *)&req_lib_evs_mcast_groups->groups;
  327. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  328. req_exec_evs_mcast_iovec[2].iov_base = (void *) msg_addr; /* discard const */
  329. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  330. res = api->totem_mcast (req_exec_evs_mcast_iovec, 3, TOTEM_AGREED);
  331. if (res == 0) {
  332. error = CS_OK;
  333. }
  334. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  335. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  336. res_lib_evs_mcast_groups.header.error = error;
  337. api->ipc_response_send (conn, &res_lib_evs_mcast_groups,
  338. sizeof (struct res_lib_evs_mcast_groups));
  339. }
  340. static void message_handler_req_evs_membership_get (void *conn, const void *msg)
  341. {
  342. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  343. res_lib_evs_membership_get.header.size = sizeof (struct res_lib_evs_membership_get);
  344. res_lib_evs_membership_get.header.id = MESSAGE_RES_EVS_MEMBERSHIP_GET;
  345. res_lib_evs_membership_get.header.error = CS_OK;
  346. res_lib_evs_membership_get.local_nodeid = api->totem_nodeid_get ();
  347. memcpy (&res_lib_evs_membership_get.member_list,
  348. &res_evs_confchg_callback.member_list,
  349. sizeof (res_lib_evs_membership_get.member_list));
  350. res_lib_evs_membership_get.member_list_entries =
  351. res_evs_confchg_callback.member_list_entries;
  352. api->ipc_response_send (conn, &res_lib_evs_membership_get,
  353. sizeof (struct res_lib_evs_membership_get));
  354. }
  355. static void req_exec_mcast_endian_convert (void *msg)
  356. {
  357. struct req_exec_evs_mcast *req_exec_evs_mcast =
  358. (struct req_exec_evs_mcast *)msg;
  359. req_exec_evs_mcast->group_entries =
  360. swab32 (req_exec_evs_mcast->group_entries);
  361. req_exec_evs_mcast->msg_len = swab32 (req_exec_evs_mcast->msg_len);
  362. }
  363. static void message_handler_req_exec_mcast (
  364. const void *msg,
  365. unsigned int nodeid)
  366. {
  367. const struct req_exec_evs_mcast *req_exec_evs_mcast = msg;
  368. struct res_evs_deliver_callback res_evs_deliver_callback;
  369. const char *msg_addr;
  370. struct list_head *list;
  371. int found = 0;
  372. int i, j;
  373. struct evs_pd *evs_pd;
  374. struct iovec iov[2];
  375. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  376. req_exec_evs_mcast->msg_len;
  377. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  378. res_evs_deliver_callback.header.error = CS_OK;
  379. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  380. msg_addr = (const char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  381. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  382. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  383. found = 0;
  384. evs_pd = list_entry (list, struct evs_pd, list);
  385. for (i = 0; i < evs_pd->group_entries; i++) {
  386. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  387. if (memcmp (&evs_pd->groups[i], &req_exec_evs_mcast->groups[j],
  388. sizeof (struct evs_group)) == 0) {
  389. found = 1;
  390. break;
  391. }
  392. }
  393. if (found) {
  394. break;
  395. }
  396. }
  397. if (found) {
  398. res_evs_deliver_callback.local_nodeid = nodeid;
  399. iov[0].iov_base = (void *)&res_evs_deliver_callback;
  400. iov[0].iov_len = sizeof (struct res_evs_deliver_callback);
  401. iov[1].iov_base = (void *) msg_addr; /* discard const */
  402. iov[1].iov_len = req_exec_evs_mcast->msg_len;
  403. api->ipc_dispatch_iov_send (
  404. evs_pd->conn,
  405. iov,
  406. 2);
  407. }
  408. }
  409. }