evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. struct res_evs_confchg_callback res_evs_confchg_callback;
  133. static int evs_confchg_fn (
  134. enum totem_configuration_type configuration_type,
  135. struct in_addr *member_list, int member_list_entries,
  136. struct in_addr *left_list, int left_list_entries,
  137. struct in_addr *joined_list, int joined_list_entries,
  138. struct memb_ring_id *ring_id)
  139. {
  140. int i;
  141. struct list_head *list;
  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. libais_send_response (conn_info, &res_evs_confchg_callback,
  177. sizeof (res_evs_confchg_callback));
  178. return (0);
  179. }
  180. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  181. {
  182. evs_error_t error = EVS_OK;
  183. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  184. struct res_lib_evs_join res_lib_evs_join;
  185. void *addr;
  186. if (req_lib_evs_join->group_entries > 50) {
  187. error = EVS_ERR_TOO_MANY_GROUPS;
  188. goto exit_error;
  189. }
  190. #ifdef DEBUG
  191. { int i;
  192. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  193. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  194. }
  195. }
  196. #endif
  197. addr = realloc (conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups,
  198. sizeof (struct evs_group) *
  199. (conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries +
  200. req_lib_evs_join->group_entries));
  201. if (addr == 0) {
  202. error = SA_ERR_NO_MEMORY;
  203. goto exit_error;
  204. }
  205. conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups = addr;
  206. memcpy (&conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries],
  207. req_lib_evs_join->groups,
  208. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  209. conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  210. exit_error:
  211. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  212. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  213. res_lib_evs_join.header.error = error;
  214. libais_send_response (conn_info, &res_lib_evs_join,
  215. sizeof (struct res_lib_evs_join));
  216. return (0);
  217. }
  218. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  219. {
  220. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  221. struct res_lib_evs_leave res_lib_evs_leave;
  222. evs_error_t error = EVS_OK;
  223. int error_index;
  224. int i, j;
  225. int found;
  226. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  227. found = 0;
  228. for (j = 0; j < conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries;) {
  229. if (memcmp (&req_lib_evs_leave->groups[i],
  230. &conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[j],
  231. sizeof (struct evs_group)) == 0) {
  232. /*
  233. * Delete entry
  234. */
  235. memmove (&conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[j],
  236. &conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[j + 1],
  237. (conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries - j - 1) *
  238. sizeof (struct evs_group));
  239. conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries -= 1;
  240. found = 1;
  241. break;
  242. } else {
  243. j++;
  244. }
  245. }
  246. if (found == 0) {
  247. error = EVS_ERR_NOT_EXIST;
  248. error_index = i;
  249. break;
  250. }
  251. }
  252. #ifdef DEBUG
  253. for (i = 0; i < conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries; i++) {
  254. printf ("Groups Left %s\n",
  255. &conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups[i].key);
  256. }
  257. #endif
  258. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  259. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  260. res_lib_evs_leave.header.error = error;
  261. libais_send_response (conn_info, &res_lib_evs_leave,
  262. sizeof (struct res_lib_evs_leave));
  263. return (0);
  264. }
  265. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  266. {
  267. evs_error_t error = EVS_ERR_TRY_AGAIN;
  268. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  269. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  270. struct iovec req_exec_evs_mcast_iovec[3];
  271. struct req_exec_evs_mcast req_exec_evs_mcast;
  272. int send_ok = 0;
  273. int res;
  274. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast) +
  275. conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries *
  276. sizeof (struct evs_group) +
  277. req_lib_evs_mcast_joined->msg_len;
  278. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  279. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  280. req_exec_evs_mcast.group_entries = conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries;
  281. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  282. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  283. req_exec_evs_mcast_iovec[1].iov_base = conn_info->conn_info_partner->ais_ci.u.libevs_ci.groups;
  284. req_exec_evs_mcast_iovec[1].iov_len = conn_info->conn_info_partner->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  285. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  286. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  287. // TODO this doesn't seem to work for some reason
  288. send_ok = totempg_send_ok (req_lib_evs_mcast_joined->msg_len);
  289. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  290. // TODO
  291. if (res == 0) {
  292. error = EVS_OK;
  293. } else {
  294. printf ("res is wrong\n");
  295. }
  296. error = EVS_OK;
  297. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  298. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  299. res_lib_evs_mcast_joined.header.error = error;
  300. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  301. sizeof (struct res_lib_evs_mcast_joined));
  302. return (0);
  303. }
  304. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  305. {
  306. evs_error_t error = EVS_ERR_TRY_AGAIN;
  307. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  308. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  309. struct iovec req_exec_evs_mcast_iovec[3];
  310. struct req_exec_evs_mcast req_exec_evs_mcast;
  311. char *msg_addr;
  312. int send_ok = 0;
  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 = MESSAGE_REQ_EXEC_EVS_MCAST;
  318. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  319. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  320. msg_addr = (char *)req_lib_evs_mcast_groups +
  321. sizeof (struct req_lib_evs_mcast_groups) +
  322. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  323. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  324. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  325. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  326. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  327. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  328. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  329. // TODO this is wacky
  330. send_ok = totempg_send_ok (req_lib_evs_mcast_groups->msg_len);
  331. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  332. if (res == 0) {
  333. error = EVS_OK;
  334. }
  335. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  336. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  337. res_lib_evs_mcast_groups.header.error = error;
  338. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  339. sizeof (struct res_lib_evs_mcast_groups));
  340. return (0);
  341. }
  342. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr, int endian_conversion_required)
  343. {
  344. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  345. struct res_evs_deliver_callback res_evs_deliver_callback;
  346. char *msg_addr;
  347. struct conn_info *conn_info;
  348. struct list_head *list;
  349. int found = 0;
  350. int i, j;
  351. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  352. req_exec_evs_mcast->msg_len;
  353. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  354. res_evs_deliver_callback.header.error = SA_OK;
  355. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  356. msg_addr = (char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  357. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  358. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  359. found = 0;
  360. conn_info = list_entry (list, struct conn_info, conn_list);
  361. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  362. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  363. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  364. &req_exec_evs_mcast->groups[j],
  365. sizeof (struct evs_group)) == 0) {
  366. found = 1;
  367. break;
  368. }
  369. }
  370. if (found) {
  371. break;
  372. }
  373. }
  374. if (found) {
  375. res_evs_deliver_callback.source_addr.s_addr = source_addr.s_addr;
  376. libais_send_response (conn_info, &res_evs_deliver_callback,
  377. sizeof (struct res_evs_deliver_callback));
  378. libais_send_response (conn_info, msg_addr,
  379. req_exec_evs_mcast->msg_len);
  380. }
  381. }
  382. //TODO printf ("Got evs message %s\n", msg_addr);
  383. return (0);
  384. }