evs.c 17 KB

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