evs.c 16 KB

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