evs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2004 MontaVista Software, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.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. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <sys/un.h>
  39. #include <sys/sysinfo.h>
  40. #include <sys/ioctl.h>
  41. #include <netinet/in.h>
  42. #include <sys/uio.h>
  43. #include <unistd.h>
  44. #include <fcntl.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <errno.h>
  48. #include <signal.h>
  49. #include <time.h>
  50. #include <netinet/in.h>
  51. #include <arpa/inet.h>
  52. #include "../include/ais_types.h"
  53. #include "../include/ipc_gen.h"
  54. #include "../include/ipc_evs.h"
  55. #include "../include/list.h"
  56. #include "../include/queue.h"
  57. #include "aispoll.h"
  58. #include "totempg.h"
  59. #include "parse.h"
  60. #include "main.h"
  61. #include "mempool.h"
  62. #include "handlers.h"
  63. #define LOG_SERVICE LOG_SERVICE_EVS
  64. #include "print.h"
  65. static DECLARE_LIST_INIT (confchg_notify);
  66. /*
  67. * Service Interfaces required by service_message_handler struct
  68. */
  69. static int evs_executive_initialize (struct openais_config *);
  70. static int evs_confchg_fn (
  71. enum totem_configuration_type configuration_type,
  72. struct in_addr *member_list, int member_list_entries,
  73. struct in_addr *left_list, int left_list_entries,
  74. struct in_addr *joined_list, int joined_list_entries,
  75. struct memb_ring_id *ring_id);
  76. static int evs_init_two_fn (struct conn_info *conn_info);
  77. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr, int endian_conversion_required);
  78. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message);
  79. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message);
  80. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message);
  81. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message);
  82. static int message_handler_req_evs_membership_get (struct conn_info *conn_info, void *message);
  83. static int evs_exit_fn (struct conn_info *conn_info);
  84. struct libais_handler evs_libais_handlers[] =
  85. {
  86. { /* 0 */
  87. .libais_handler_fn = message_handler_req_evs_join,
  88. .response_size = sizeof (struct res_lib_evs_join),
  89. .response_id = MESSAGE_RES_EVS_JOIN,
  90. .flow_control = FLOW_CONTROL_NOT_REQUIRED
  91. },
  92. { /* 1 */
  93. .libais_handler_fn = message_handler_req_evs_leave,
  94. .response_size = sizeof (struct res_lib_evs_leave),
  95. .response_id = MESSAGE_RES_EVS_LEAVE,
  96. .flow_control = FLOW_CONTROL_NOT_REQUIRED
  97. },
  98. { /* 2 */
  99. .libais_handler_fn = message_handler_req_evs_mcast_joined,
  100. .response_size = sizeof (struct res_lib_evs_mcast_joined),
  101. .response_id = MESSAGE_RES_EVS_MCAST_JOINED,
  102. .flow_control = FLOW_CONTROL_REQUIRED
  103. },
  104. { /* 3 */
  105. .libais_handler_fn = message_handler_req_evs_mcast_groups,
  106. .response_size = sizeof (struct res_lib_evs_mcast_groups),
  107. .response_id = MESSAGE_RES_EVS_MCAST_GROUPS,
  108. .flow_control = FLOW_CONTROL_REQUIRED
  109. },
  110. { /* 4 */
  111. .libais_handler_fn = message_handler_req_evs_membership_get,
  112. .response_size = sizeof (struct res_lib_evs_membership_get),
  113. .response_id = MESSAGE_RES_EVS_MEMBERSHIP_GET,
  114. .flow_control = FLOW_CONTROL_NOT_REQUIRED
  115. }
  116. };
  117. static int (*evs_aisexec_handler_fns[]) (void *, struct in_addr source_addr, int endian_conversion_required) = {
  118. message_handler_req_exec_mcast
  119. };
  120. struct service_handler evs_service_handler = {
  121. .libais_handlers = evs_libais_handlers,
  122. .libais_handlers_count = sizeof (evs_libais_handlers) / sizeof (struct libais_handler),
  123. .aisexec_handler_fns = evs_aisexec_handler_fns,
  124. .aisexec_handler_fns_count = sizeof (evs_aisexec_handler_fns) / sizeof (int (*)),
  125. .confchg_fn = evs_confchg_fn,
  126. .libais_init_two_fn = evs_init_two_fn,
  127. .libais_exit_fn = evs_exit_fn,
  128. .exec_init_fn = evs_executive_initialize,
  129. .exec_dump_fn = 0
  130. };
  131. static int evs_executive_initialize (struct openais_config *openais_config)
  132. {
  133. return (0);
  134. }
  135. static int evs_exit_fn (struct conn_info *conn_info)
  136. {
  137. list_del (&conn_info->conn_list);
  138. return (0);
  139. }
  140. struct res_evs_confchg_callback res_evs_confchg_callback;
  141. static int evs_confchg_fn (
  142. enum totem_configuration_type configuration_type,
  143. struct in_addr *member_list, int member_list_entries,
  144. struct in_addr *left_list, int left_list_entries,
  145. struct in_addr *joined_list, int joined_list_entries,
  146. struct memb_ring_id *ring_id)
  147. {
  148. int i;
  149. struct list_head *list;
  150. struct conn_info *conn_info;
  151. /*
  152. * Build configuration change message
  153. */
  154. res_evs_confchg_callback.header.size = sizeof (struct res_evs_confchg_callback);
  155. res_evs_confchg_callback.header.id = MESSAGE_RES_EVS_CONFCHG_CALLBACK;
  156. res_evs_confchg_callback.header.error = SA_OK;
  157. for (i = 0; i < member_list_entries; i++) {
  158. res_evs_confchg_callback.member_list[i].s_addr = member_list[i].s_addr;
  159. }
  160. res_evs_confchg_callback.member_list_entries = member_list_entries;
  161. for (i = 0; i < left_list_entries; i++) {
  162. res_evs_confchg_callback.left_list[i].s_addr = left_list[i].s_addr;
  163. }
  164. res_evs_confchg_callback.left_list_entries = left_list_entries;
  165. for (i = 0; i < joined_list_entries; i++) {
  166. res_evs_confchg_callback.joined_list[i].s_addr = joined_list[i].s_addr;
  167. }
  168. res_evs_confchg_callback.joined_list_entries = joined_list_entries;
  169. /*
  170. * Send configuration change message to every EVS library user
  171. */
  172. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  173. conn_info = list_entry (list, struct conn_info, conn_list);
  174. libais_send_response (conn_info, &res_evs_confchg_callback,
  175. sizeof (res_evs_confchg_callback));
  176. }
  177. return (0);
  178. }
  179. static int evs_init_two_fn (struct conn_info *conn_info)
  180. {
  181. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  182. list_init (&conn_info->conn_list);
  183. list_add (&conn_info->conn_list, &confchg_notify);
  184. libais_send_response (conn_info, &res_evs_confchg_callback,
  185. sizeof (res_evs_confchg_callback));
  186. return (0);
  187. }
  188. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  189. {
  190. evs_error_t error = EVS_OK;
  191. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  192. struct res_lib_evs_join res_lib_evs_join;
  193. void *addr;
  194. if (req_lib_evs_join->group_entries > 50) {
  195. error = EVS_ERR_TOO_MANY_GROUPS;
  196. goto exit_error;
  197. }
  198. #ifdef DEBUG
  199. { int i;
  200. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  201. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  202. }
  203. }
  204. #endif
  205. addr = realloc (conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups,
  206. sizeof (struct evs_group) *
  207. (conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries +
  208. req_lib_evs_join->group_entries));
  209. if (addr == 0) {
  210. error = SA_ERR_NO_MEMORY;
  211. goto exit_error;
  212. }
  213. conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups = addr;
  214. memcpy (&conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries],
  215. req_lib_evs_join->groups,
  216. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  217. conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  218. exit_error:
  219. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  220. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  221. res_lib_evs_join.header.error = error;
  222. libais_send_response (conn_info, &res_lib_evs_join,
  223. sizeof (struct res_lib_evs_join));
  224. return (0);
  225. }
  226. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  227. {
  228. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  229. struct res_lib_evs_leave res_lib_evs_leave;
  230. evs_error_t error = EVS_OK;
  231. int error_index;
  232. int i, j;
  233. int found;
  234. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  235. found = 0;
  236. for (j = 0; j < conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries;) {
  237. if (memcmp (&req_lib_evs_leave->groups[i],
  238. &conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[j],
  239. sizeof (struct evs_group)) == 0) {
  240. /*
  241. * Delete entry
  242. */
  243. memmove (&conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[j],
  244. &conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[j + 1],
  245. (conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries - j - 1) *
  246. sizeof (struct evs_group));
  247. conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries -= 1;
  248. found = 1;
  249. break;
  250. } else {
  251. j++;
  252. }
  253. }
  254. if (found == 0) {
  255. error = EVS_ERR_NOT_EXIST;
  256. error_index = i;
  257. break;
  258. }
  259. }
  260. #ifdef DEBUG
  261. for (i = 0; i < conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries; i++) {
  262. printf ("Groups Left %s\n",
  263. &conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[i].key);
  264. }
  265. #endif
  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. libais_send_response (conn_info, &res_lib_evs_leave,
  270. sizeof (struct res_lib_evs_leave));
  271. return (0);
  272. }
  273. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  274. {
  275. evs_error_t error = EVS_ERR_TRY_AGAIN;
  276. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  277. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  278. struct iovec req_exec_evs_mcast_iovec[3];
  279. struct req_exec_evs_mcast req_exec_evs_mcast;
  280. int send_ok = 0;
  281. int res;
  282. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  283. conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries *
  284. sizeof (struct evs_group) +
  285. req_lib_evs_mcast_joined->msg_len;
  286. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  287. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  288. req_exec_evs_mcast.group_entries = conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries;
  289. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  290. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  291. req_exec_evs_mcast_iovec[1].iov_base = conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups;
  292. req_exec_evs_mcast_iovec[1].iov_len = conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  293. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  294. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  295. // TODO this doesn't seem to work for some reason
  296. send_ok = totempg_send_ok (req_lib_evs_mcast_joined->msg_len);
  297. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  298. // TODO
  299. if (res == 0) {
  300. error = EVS_OK;
  301. } else {
  302. printf ("res is wrong\n");
  303. }
  304. error = EVS_OK;
  305. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  306. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  307. res_lib_evs_mcast_joined.header.error = error;
  308. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  309. sizeof (struct res_lib_evs_mcast_joined));
  310. return (0);
  311. }
  312. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  313. {
  314. evs_error_t error = EVS_ERR_TRY_AGAIN;
  315. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  316. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  317. struct iovec req_exec_evs_mcast_iovec[3];
  318. struct req_exec_evs_mcast req_exec_evs_mcast;
  319. char *msg_addr;
  320. int send_ok = 0;
  321. int res;
  322. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  323. sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries +
  324. req_lib_evs_mcast_groups->msg_len;
  325. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  326. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  327. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  328. msg_addr = (char *)req_lib_evs_mcast_groups +
  329. sizeof (struct req_lib_evs_mcast_groups) +
  330. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  331. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  332. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  333. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  334. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  335. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  336. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  337. // TODO this is wacky
  338. send_ok = totempg_send_ok (req_lib_evs_mcast_groups->msg_len);
  339. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  340. if (res == 0) {
  341. error = EVS_OK;
  342. }
  343. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  344. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  345. res_lib_evs_mcast_groups.header.error = error;
  346. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  347. sizeof (struct res_lib_evs_mcast_groups));
  348. return (0);
  349. }
  350. static int message_handler_req_evs_membership_get (struct conn_info *conn_info, void *message)
  351. {
  352. struct res_lib_evs_membership_get res_lib_evs_membership_get;
  353. res_lib_evs_membership_get.header.size = sizeof (struct res_lib_evs_membership_get);
  354. res_lib_evs_membership_get.header.id = MESSAGE_RES_EVS_MEMBERSHIP_GET;
  355. res_lib_evs_membership_get.header.error = EVS_OK;
  356. res_lib_evs_membership_get.local_addr.s_addr = this_ip->sin_addr.s_addr;
  357. memcpy (&res_lib_evs_membership_get.member_list,
  358. &res_evs_confchg_callback.member_list,
  359. sizeof (res_lib_evs_membership_get.member_list));
  360. res_lib_evs_membership_get.member_list_entries =
  361. res_evs_confchg_callback.member_list_entries;
  362. libais_send_response (conn_info, &res_lib_evs_membership_get,
  363. sizeof (struct res_lib_evs_membership_get));
  364. return (0);
  365. }
  366. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr, int endian_conversion_required)
  367. {
  368. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  369. struct res_evs_deliver_callback res_evs_deliver_callback;
  370. char *msg_addr;
  371. struct conn_info *conn_info;
  372. struct list_head *list;
  373. int found = 0;
  374. int i, j;
  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 = SA_OK;
  379. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  380. msg_addr = (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. conn_info = list_entry (list, struct conn_info, conn_list);
  385. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  386. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  387. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  388. &req_exec_evs_mcast->groups[j],
  389. sizeof (struct evs_group)) == 0) {
  390. found = 1;
  391. break;
  392. }
  393. }
  394. if (found) {
  395. break;
  396. }
  397. }
  398. if (found) {
  399. res_evs_deliver_callback.source_addr.s_addr = source_addr.s_addr;
  400. libais_send_response (conn_info, &res_evs_deliver_callback,
  401. sizeof (struct res_evs_deliver_callback));
  402. libais_send_response (conn_info, msg_addr,
  403. req_exec_evs_mcast->msg_len);
  404. }
  405. }
  406. //TODO printf ("Got evs message %s\n", msg_addr);
  407. return (0);
  408. }