evs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <sys/sysinfo.h>
  38. #include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. #include <sys/uio.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <signal.h>
  47. #include <time.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include "../include/ais_types.h"
  51. #include "../include/ais_msg.h"
  52. #include "../include/list.h"
  53. #include "../include/queue.h"
  54. #include "aispoll.h"
  55. #include "totempg.h"
  56. #include "parse.h"
  57. #include "main.h"
  58. #include "mempool.h"
  59. #include "handlers.h"
  60. #define LOG_SERVICE LOG_SERVICE_EVS
  61. #include "print.h"
  62. static DECLARE_LIST_INIT (confchg_notify);
  63. /*
  64. * Service Interfaces required by service_message_handler struct
  65. */
  66. static int evs_executive_initialize (void);
  67. static int evs_confchg_fn (
  68. enum totempg_configuration_type configuration_type,
  69. struct in_addr *member_list, void *member_list_private,
  70. int member_list_entries,
  71. struct in_addr *left_list, void *left_list_private,
  72. int left_list_entries,
  73. struct in_addr *joined_list, void *joined_list_private,
  74. int joined_list_entries,
  75. struct memb_ring_id *ring_id);
  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_init (struct conn_info *conn_info,
  78. void *message);
  79. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  80. void *message);
  81. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message);
  82. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message);
  83. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message);
  84. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message);
  85. static int evs_exit_fn (struct conn_info *conn_info);
  86. struct libais_handler evs_libais_handlers[] =
  87. {
  88. { /* 0 */
  89. .libais_handler_fn = message_handler_req_lib_activatepoll,
  90. .response_size = sizeof (struct res_lib_activatepoll),
  91. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL, // TODO RESPONSE
  92. },
  93. { /* 1 */
  94. .libais_handler_fn = message_handler_req_evs_join,
  95. .response_size = sizeof (struct res_lib_evs_join),
  96. .response_id = MESSAGE_RES_EVS_JOIN,
  97. },
  98. { /* 2 */
  99. .libais_handler_fn = message_handler_req_evs_leave,
  100. .response_size = sizeof (struct res_lib_evs_leave),
  101. .response_id = MESSAGE_RES_EVS_LEAVE,
  102. },
  103. { /* 3 */
  104. .libais_handler_fn = message_handler_req_evs_mcast_joined,
  105. .response_size = sizeof (struct res_lib_evs_mcast_joined),
  106. .response_id = MESSAGE_RES_EVS_MCAST_JOINED,
  107. },
  108. { /* 4 */
  109. .libais_handler_fn = message_handler_req_evs_mcast_groups,
  110. .response_size = sizeof (struct res_lib_evs_mcast_groups),
  111. .response_id = MESSAGE_RES_EVS_MCAST_GROUPS,
  112. }
  113. };
  114. static int (*evs_aisexec_handler_fns[]) (void *, struct in_addr source_addr, int endian_conversion_required) = {
  115. message_handler_req_exec_mcast
  116. };
  117. struct service_handler evs_service_handler = {
  118. .libais_handlers = evs_libais_handlers,
  119. .libais_handlers_count = sizeof (evs_libais_handlers) / sizeof (struct libais_handler),
  120. .aisexec_handler_fns = evs_aisexec_handler_fns,
  121. .aisexec_handler_fns_count = sizeof (evs_aisexec_handler_fns) / sizeof (int (*)),
  122. .confchg_fn = evs_confchg_fn,
  123. .libais_init_fn = message_handler_req_evs_init,
  124. .libais_exit_fn = evs_exit_fn,
  125. .exec_init_fn = evs_executive_initialize,
  126. .exec_dump_fn = 0
  127. };
  128. static int evs_executive_initialize (void)
  129. {
  130. return (0);
  131. }
  132. static int evs_exit_fn (struct conn_info *conn_info)
  133. {
  134. list_del (&conn_info->conn_list);
  135. return (0);
  136. }
  137. static int evs_confchg_fn (
  138. enum totempg_configuration_type configuration_type,
  139. struct in_addr *member_list, void *member_list_private,
  140. int member_list_entries,
  141. struct in_addr *left_list, void *left_list_private,
  142. int left_list_entries,
  143. struct in_addr *joined_list, void *joined_list_private,
  144. int joined_list_entries,
  145. struct memb_ring_id *ring_id)
  146. {
  147. int i;
  148. struct list_head *list;
  149. struct res_evs_confchg_callback res_evs_confchg_callback;
  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 message_handler_req_evs_init (struct conn_info *conn_info, void *message)
  180. {
  181. SaErrorT error = SA_ERR_SECURITY;
  182. struct res_lib_init res_lib_init;
  183. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  184. if (conn_info->authenticated) {
  185. conn_info->service = SOCKET_SERVICE_EVS;
  186. error = SA_OK;
  187. }
  188. res_lib_init.header.size = sizeof (struct res_lib_init);
  189. res_lib_init.header.id = MESSAGE_RES_INIT;
  190. res_lib_init.header.error = error;
  191. libais_send_response (conn_info, &res_lib_init, sizeof (res_lib_init));
  192. list_add (&conn_info->conn_list, &confchg_notify);
  193. if (conn_info->authenticated) {
  194. return (0);
  195. }
  196. return (-1);
  197. }
  198. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info, void *message)
  199. {
  200. struct res_lib_activatepoll res_lib_activatepoll;
  201. res_lib_activatepoll.header.size = sizeof (struct res_lib_activatepoll);
  202. res_lib_activatepoll.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  203. res_lib_activatepoll.header.error = SA_OK;
  204. libais_send_response (conn_info, &res_lib_activatepoll,
  205. sizeof (struct res_lib_activatepoll));
  206. return (0);
  207. }
  208. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  209. {
  210. evs_error_t error = EVS_OK;
  211. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  212. struct res_lib_evs_join res_lib_evs_join;
  213. void *addr;
  214. if (req_lib_evs_join->group_entries > 50) {
  215. error = EVS_ERR_TOO_MANY_GROUPS;
  216. goto exit_error;
  217. }
  218. #ifdef DEBUG
  219. { int i;
  220. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  221. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  222. }
  223. }
  224. #endif
  225. addr = realloc (conn_info->ais_ci.u.libevs_ci.groups,
  226. sizeof (struct evs_group) *
  227. (conn_info->ais_ci.u.libevs_ci.group_entries + req_lib_evs_join->group_entries));
  228. if (addr == 0) {
  229. error = SA_ERR_NO_MEMORY;
  230. goto exit_error;
  231. }
  232. conn_info->ais_ci.u.libevs_ci.groups = addr;
  233. memcpy (&conn_info->ais_ci.u.libevs_ci.groups[conn_info->ais_ci.u.libevs_ci.group_entries],
  234. req_lib_evs_join->groups,
  235. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  236. conn_info->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  237. exit_error:
  238. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  239. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  240. res_lib_evs_join.header.error = error;
  241. libais_send_response (conn_info, &res_lib_evs_join,
  242. sizeof (struct res_lib_evs_join));
  243. return (0);
  244. }
  245. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  246. {
  247. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  248. struct res_lib_evs_leave res_lib_evs_leave;
  249. evs_error_t error = EVS_OK;
  250. int error_index;
  251. int i, j;
  252. int found;
  253. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  254. found = 0;
  255. for (j = 0; j < conn_info->ais_ci.u.libevs_ci.group_entries;) {
  256. if (memcmp (&req_lib_evs_leave->groups[i],
  257. &conn_info->ais_ci.u.libevs_ci.groups[j],
  258. sizeof (struct evs_group)) == 0) {
  259. /*
  260. * Delete entry
  261. */
  262. memmove (&conn_info->ais_ci.u.libevs_ci.groups[j],
  263. &conn_info->ais_ci.u.libevs_ci.groups[j + 1],
  264. (conn_info->ais_ci.u.libevs_ci.group_entries - j - 1) *
  265. sizeof (struct evs_group));
  266. conn_info->ais_ci.u.libevs_ci.group_entries -= 1;
  267. found = 1;
  268. break;
  269. } else {
  270. j++;
  271. }
  272. }
  273. if (found == 0) {
  274. error = EVS_ERR_NOT_EXIST;
  275. error_index = i;
  276. break;
  277. }
  278. }
  279. #ifdef DEBUG
  280. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  281. printf ("Groups Left %s\n",
  282. &conn_info->ais_ci.u.libevs_ci.groups[i].key);
  283. }
  284. #endif
  285. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  286. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  287. res_lib_evs_leave.header.error = error;
  288. libais_send_response (conn_info, &res_lib_evs_leave,
  289. sizeof (struct res_lib_evs_leave));
  290. return (0);
  291. }
  292. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  293. {
  294. evs_error_t error = EVS_ERR_TRY_AGAIN;
  295. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  296. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  297. struct iovec req_exec_evs_mcast_iovec[3];
  298. struct req_exec_evs_mcast req_exec_evs_mcast;
  299. int send_ok = 0;
  300. int res;
  301. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  302. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  303. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  304. req_exec_evs_mcast.group_entries = conn_info->ais_ci.u.libevs_ci.group_entries;
  305. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  306. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  307. req_exec_evs_mcast_iovec[1].iov_base = conn_info->ais_ci.u.libevs_ci.groups;
  308. req_exec_evs_mcast_iovec[1].iov_len = conn_info->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  309. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  310. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  311. // TODO this doesn't seem to work for some reason
  312. send_ok = totempg_send_ok (req_lib_evs_mcast_joined->msg_len);
  313. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  314. // TODO
  315. if (res == 0) {
  316. error = EVS_OK;
  317. } else {
  318. printf ("res is wrong\n");
  319. }
  320. error = EVS_OK;
  321. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  322. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  323. res_lib_evs_mcast_joined.header.error = error;
  324. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  325. sizeof (struct res_lib_evs_mcast_joined));
  326. return (0);
  327. }
  328. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  329. {
  330. evs_error_t error = EVS_ERR_TRY_AGAIN;
  331. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  332. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  333. struct iovec req_exec_evs_mcast_iovec[3];
  334. struct req_exec_evs_mcast req_exec_evs_mcast;
  335. char *msg_addr;
  336. int send_ok = 0;
  337. int res;
  338. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  339. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  340. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  341. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  342. msg_addr = (char *)req_lib_evs_mcast_groups +
  343. sizeof (struct req_lib_evs_mcast_groups) +
  344. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  345. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  346. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  347. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  348. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  349. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  350. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  351. // TODO this is wacky
  352. send_ok = totempg_send_ok (req_lib_evs_mcast_groups->msg_len);
  353. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  354. if (res == 0) {
  355. error = EVS_OK;
  356. }
  357. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  358. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  359. res_lib_evs_mcast_groups.header.error = error;
  360. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  361. sizeof (struct res_lib_evs_mcast_groups));
  362. return (0);
  363. }
  364. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr, int endian_conversion_required)
  365. {
  366. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  367. struct res_evs_deliver_callback res_evs_deliver_callback;
  368. char *msg_addr;
  369. struct conn_info *conn_info;
  370. struct list_head *list;
  371. int found = 0;
  372. int i, j;
  373. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  374. req_exec_evs_mcast->msg_len;
  375. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  376. res_evs_deliver_callback.header.error = SA_OK;
  377. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  378. msg_addr = (char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  379. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  380. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  381. found = 0;
  382. conn_info = list_entry (list, struct conn_info, conn_list);
  383. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  384. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  385. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  386. &req_exec_evs_mcast->groups[j],
  387. sizeof (struct evs_group)) == 0) {
  388. found = 1;
  389. break;
  390. }
  391. }
  392. if (found) {
  393. break;
  394. }
  395. }
  396. if (found) {
  397. res_evs_deliver_callback.source_addr.s_addr = source_addr.s_addr;
  398. libais_send_response (conn_info, &res_evs_deliver_callback,
  399. sizeof (struct res_evs_deliver_callback));
  400. libais_send_response (conn_info, msg_addr,
  401. req_exec_evs_mcast->msg_len);
  402. }
  403. }
  404. //TODO printf ("Got evs message %s\n", msg_addr);
  405. return (0);
  406. }