evs.c 16 KB

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