evs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 totem_configuration_type configuration_type,
  69. struct in_addr *member_list, int member_list_entries,
  70. struct in_addr *left_list, int left_list_entries,
  71. struct in_addr *joined_list, int joined_list_entries,
  72. struct memb_ring_id *ring_id);
  73. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr, int endian_conversion_required);
  74. static int message_handler_req_evs_init (struct conn_info *conn_info,
  75. void *message);
  76. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  77. void *message);
  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 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_lib_activatepoll,
  87. .response_size = sizeof (struct res_lib_activatepoll),
  88. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL, // TODO RESPONSE
  89. .flow_control = FLOW_CONTROL_NOT_REQUIRED
  90. },
  91. { /* 1 */
  92. .libais_handler_fn = message_handler_req_evs_join,
  93. .response_size = sizeof (struct res_lib_evs_join),
  94. .response_id = MESSAGE_RES_EVS_JOIN,
  95. .flow_control = FLOW_CONTROL_NOT_REQUIRED
  96. },
  97. { /* 2 */
  98. .libais_handler_fn = message_handler_req_evs_leave,
  99. .response_size = sizeof (struct res_lib_evs_leave),
  100. .response_id = MESSAGE_RES_EVS_LEAVE,
  101. .flow_control = FLOW_CONTROL_NOT_REQUIRED
  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. .flow_control = FLOW_CONTROL_REQUIRED
  108. },
  109. { /* 4 */
  110. .libais_handler_fn = message_handler_req_evs_mcast_groups,
  111. .response_size = sizeof (struct res_lib_evs_mcast_groups),
  112. .response_id = MESSAGE_RES_EVS_MCAST_GROUPS,
  113. .flow_control = FLOW_CONTROL_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_fn = message_handler_req_evs_init,
  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 (void)
  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. static int evs_confchg_fn (
  140. enum totem_configuration_type configuration_type,
  141. struct in_addr *member_list, int member_list_entries,
  142. struct in_addr *left_list, int left_list_entries,
  143. struct in_addr *joined_list, int joined_list_entries,
  144. struct memb_ring_id *ring_id)
  145. {
  146. int i;
  147. struct list_head *list;
  148. struct res_evs_confchg_callback res_evs_confchg_callback;
  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 message_handler_req_evs_init (struct conn_info *conn_info, void *message)
  179. {
  180. SaErrorT error = SA_ERR_SECURITY;
  181. struct res_lib_init res_lib_init;
  182. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  183. if (conn_info->authenticated) {
  184. conn_info->service = SOCKET_SERVICE_EVS;
  185. error = SA_OK;
  186. }
  187. res_lib_init.header.size = sizeof (struct res_lib_init);
  188. res_lib_init.header.id = MESSAGE_RES_INIT;
  189. res_lib_init.header.error = error;
  190. libais_send_response (conn_info, &res_lib_init, sizeof (res_lib_init));
  191. list_add (&conn_info->conn_list, &confchg_notify);
  192. if (conn_info->authenticated) {
  193. return (0);
  194. }
  195. return (-1);
  196. }
  197. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info, void *message)
  198. {
  199. struct res_lib_activatepoll res_lib_activatepoll;
  200. res_lib_activatepoll.header.size = sizeof (struct res_lib_activatepoll);
  201. res_lib_activatepoll.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  202. res_lib_activatepoll.header.error = SA_OK;
  203. libais_send_response (conn_info, &res_lib_activatepoll,
  204. sizeof (struct res_lib_activatepoll));
  205. return (0);
  206. }
  207. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  208. {
  209. evs_error_t error = EVS_OK;
  210. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  211. struct res_lib_evs_join res_lib_evs_join;
  212. void *addr;
  213. if (req_lib_evs_join->group_entries > 50) {
  214. error = EVS_ERR_TOO_MANY_GROUPS;
  215. goto exit_error;
  216. }
  217. #ifdef DEBUG
  218. { int i;
  219. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  220. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  221. }
  222. }
  223. #endif
  224. addr = realloc (conn_info->ais_ci.u.libevs_ci.groups,
  225. sizeof (struct evs_group) *
  226. (conn_info->ais_ci.u.libevs_ci.group_entries + req_lib_evs_join->group_entries));
  227. if (addr == 0) {
  228. error = SA_ERR_NO_MEMORY;
  229. goto exit_error;
  230. }
  231. conn_info->ais_ci.u.libevs_ci.groups = addr;
  232. memcpy (&conn_info->ais_ci.u.libevs_ci.groups[conn_info->ais_ci.u.libevs_ci.group_entries],
  233. req_lib_evs_join->groups,
  234. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  235. conn_info->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  236. exit_error:
  237. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  238. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  239. res_lib_evs_join.header.error = error;
  240. libais_send_response (conn_info, &res_lib_evs_join,
  241. sizeof (struct res_lib_evs_join));
  242. return (0);
  243. }
  244. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  245. {
  246. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  247. struct res_lib_evs_leave res_lib_evs_leave;
  248. evs_error_t error = EVS_OK;
  249. int error_index;
  250. int i, j;
  251. int found;
  252. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  253. found = 0;
  254. for (j = 0; j < conn_info->ais_ci.u.libevs_ci.group_entries;) {
  255. if (memcmp (&req_lib_evs_leave->groups[i],
  256. &conn_info->ais_ci.u.libevs_ci.groups[j],
  257. sizeof (struct evs_group)) == 0) {
  258. /*
  259. * Delete entry
  260. */
  261. memmove (&conn_info->ais_ci.u.libevs_ci.groups[j],
  262. &conn_info->ais_ci.u.libevs_ci.groups[j + 1],
  263. (conn_info->ais_ci.u.libevs_ci.group_entries - j - 1) *
  264. sizeof (struct evs_group));
  265. conn_info->ais_ci.u.libevs_ci.group_entries -= 1;
  266. found = 1;
  267. break;
  268. } else {
  269. j++;
  270. }
  271. }
  272. if (found == 0) {
  273. error = EVS_ERR_NOT_EXIST;
  274. error_index = i;
  275. break;
  276. }
  277. }
  278. #ifdef DEBUG
  279. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  280. printf ("Groups Left %s\n",
  281. &conn_info->ais_ci.u.libevs_ci.groups[i].key);
  282. }
  283. #endif
  284. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  285. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  286. res_lib_evs_leave.header.error = error;
  287. libais_send_response (conn_info, &res_lib_evs_leave,
  288. sizeof (struct res_lib_evs_leave));
  289. return (0);
  290. }
  291. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  292. {
  293. evs_error_t error = EVS_ERR_TRY_AGAIN;
  294. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  295. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  296. struct iovec req_exec_evs_mcast_iovec[3];
  297. struct req_exec_evs_mcast req_exec_evs_mcast;
  298. int send_ok = 0;
  299. int res;
  300. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  301. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  302. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  303. req_exec_evs_mcast.group_entries = conn_info->ais_ci.u.libevs_ci.group_entries;
  304. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  305. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  306. req_exec_evs_mcast_iovec[1].iov_base = conn_info->ais_ci.u.libevs_ci.groups;
  307. req_exec_evs_mcast_iovec[1].iov_len = conn_info->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  308. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  309. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  310. // TODO this doesn't seem to work for some reason
  311. send_ok = totempg_send_ok (req_lib_evs_mcast_joined->msg_len);
  312. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  313. // TODO
  314. if (res == 0) {
  315. error = EVS_OK;
  316. } else {
  317. printf ("res is wrong\n");
  318. }
  319. error = EVS_OK;
  320. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  321. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  322. res_lib_evs_mcast_joined.header.error = error;
  323. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  324. sizeof (struct res_lib_evs_mcast_joined));
  325. return (0);
  326. }
  327. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  328. {
  329. evs_error_t error = EVS_ERR_TRY_AGAIN;
  330. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  331. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  332. struct iovec req_exec_evs_mcast_iovec[3];
  333. struct req_exec_evs_mcast req_exec_evs_mcast;
  334. char *msg_addr;
  335. int send_ok = 0;
  336. int res;
  337. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  338. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  339. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  340. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  341. msg_addr = (char *)req_lib_evs_mcast_groups +
  342. sizeof (struct req_lib_evs_mcast_groups) +
  343. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  344. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  345. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  346. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  347. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  348. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  349. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  350. // TODO this is wacky
  351. send_ok = totempg_send_ok (req_lib_evs_mcast_groups->msg_len);
  352. res = totempg_mcast (req_exec_evs_mcast_iovec, 3, TOTEMPG_AGREED);
  353. if (res == 0) {
  354. error = EVS_OK;
  355. }
  356. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  357. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  358. res_lib_evs_mcast_groups.header.error = error;
  359. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  360. sizeof (struct res_lib_evs_mcast_groups));
  361. return (0);
  362. }
  363. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr, int endian_conversion_required)
  364. {
  365. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  366. struct res_evs_deliver_callback res_evs_deliver_callback;
  367. char *msg_addr;
  368. struct conn_info *conn_info;
  369. struct list_head *list;
  370. int found = 0;
  371. int i, j;
  372. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  373. req_exec_evs_mcast->msg_len;
  374. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  375. res_evs_deliver_callback.header.error = SA_OK;
  376. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  377. msg_addr = (char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  378. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  379. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  380. found = 0;
  381. conn_info = list_entry (list, struct conn_info, conn_list);
  382. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  383. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  384. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  385. &req_exec_evs_mcast->groups[j],
  386. sizeof (struct evs_group)) == 0) {
  387. found = 1;
  388. break;
  389. }
  390. }
  391. if (found) {
  392. break;
  393. }
  394. }
  395. if (found) {
  396. res_evs_deliver_callback.source_addr.s_addr = source_addr.s_addr;
  397. libais_send_response (conn_info, &res_evs_deliver_callback,
  398. sizeof (struct res_evs_deliver_callback));
  399. libais_send_response (conn_info, msg_addr,
  400. req_exec_evs_mcast->msg_len);
  401. }
  402. }
  403. //TODO printf ("Got evs message %s\n", msg_addr);
  404. return (0);
  405. }