evs.c 15 KB

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