evs.c 17 KB

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