evs.c 16 KB

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