clm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright (c) 2002-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 "gmi.h"
  56. #include "parse.h"
  57. #include "main.h"
  58. #include "print.h"
  59. #include "mempool.h"
  60. #include "handlers.h"
  61. SaClmClusterChangesT thisClusterNodeLastChange = SA_CLM_NODE_JOINED;
  62. SaClmClusterNodeT thisClusterNode;
  63. #define NODE_MAX 16
  64. SaClmClusterNodeT clusterNodes[NODE_MAX];
  65. int clusterNodeEntries = 0;
  66. static DECLARE_LIST_INIT (library_notification_send_listhead);
  67. /*
  68. * Service Interfaces required by service_message_handler struct
  69. */
  70. static int clmExecutiveInitialize (void);
  71. static int clmConfChg (
  72. struct sockaddr_in *member_list, int member_list_entries,
  73. struct sockaddr_in *left_list, int left_list_entries,
  74. struct sockaddr_in *joined_list, int joined_list_entries);
  75. static int message_handler_req_exec_clm_nodejoin (void *message, struct in_addr source_addr);
  76. static int message_handler_req_clm_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_clm_trackstart (struct conn_info *conn_info,
  81. void *message);
  82. static int message_handler_req_clm_trackstop (struct conn_info *conn_info,
  83. void *message);
  84. static int message_handler_req_clm_nodeget (struct conn_info *conn_info,
  85. void *message);
  86. static int clm_exit_fn (struct conn_info *conn_info);
  87. static int (*clm_libais_handler_fns[]) (struct conn_info *conn_info, void *) = {
  88. message_handler_req_lib_activatepoll,
  89. message_handler_req_clm_trackstart,
  90. message_handler_req_clm_trackstop,
  91. message_handler_req_clm_nodeget
  92. };
  93. static int (*clm_aisexec_handler_fns[]) (void *, struct in_addr source_addr) = {
  94. message_handler_req_exec_clm_nodejoin
  95. };
  96. struct service_handler clm_service_handler = {
  97. .libais_handler_fns = clm_libais_handler_fns,
  98. .libais_handler_fns_count = sizeof (clm_libais_handler_fns) / sizeof (int (*)),
  99. .aisexec_handler_fns = clm_aisexec_handler_fns,
  100. .aisexec_handler_fns_count = sizeof (clm_aisexec_handler_fns) / sizeof (int (*)),
  101. .confchg_fn = clmConfChg,
  102. .libais_init_fn = message_handler_req_clm_init,
  103. .libais_exit_fn = clm_exit_fn,
  104. .aisexec_init_fn = clmExecutiveInitialize
  105. };
  106. static int clmExecutiveInitialize (void)
  107. {
  108. memset (clusterNodes, 0, sizeof (SaClmClusterNodeT) * NODE_MAX);
  109. /*
  110. * Build local cluster node data structure
  111. */
  112. thisClusterNode.nodeId = this_ip.sin_addr.s_addr;
  113. memcpy (&thisClusterNode.nodeAddress.value, &this_ip.sin_addr, sizeof (struct in_addr));
  114. thisClusterNode.nodeAddress.length = sizeof (struct in_addr);
  115. strcpy (thisClusterNode.nodeName.value, (char *)inet_ntoa (this_ip.sin_addr));
  116. thisClusterNode.nodeName.length = strlen (thisClusterNode.nodeName.value);
  117. strcpy (thisClusterNode.clusterName.value, "mvlcge");
  118. thisClusterNode.clusterName.length = strlen ("mvlcge");
  119. thisClusterNode.member = 1;
  120. {
  121. struct sysinfo s_info;
  122. time_t current_time;
  123. sysinfo (&s_info);
  124. current_time = time (NULL);
  125. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  126. thisClusterNode.bootTimestamp = ((SaTimeT)(current_time - s_info.uptime)) * 1000000000;
  127. }
  128. #ifdef DEBUG
  129. printSaClmClusterNodeT ("this cluster node", &thisClusterNode);
  130. #endif
  131. memcpy (&clusterNodes[0], &thisClusterNode, sizeof (SaClmClusterNodeT));
  132. clusterNodeEntries = 1;
  133. return (0);
  134. }
  135. static int clm_exit_fn (struct conn_info *conn_info)
  136. {
  137. /*
  138. * Delete track entry if there is one
  139. */
  140. list_del (&conn_info->conn_list);
  141. return (0);
  142. }
  143. static void libraryNotificationCurrentState (struct conn_info *conn_info)
  144. {
  145. struct res_clm_trackcallback res_clm_trackcallback;
  146. SaClmClusterNotificationT clusterNotification[NODE_MAX];
  147. int i;
  148. if ((conn_info->ais_ci.u.libclm_ci.trackFlags & SA_TRACK_CURRENT) == 0) {
  149. return;
  150. }
  151. /*
  152. * Turn off track current
  153. */
  154. conn_info->ais_ci.u.libclm_ci.trackFlags &= ~SA_TRACK_CURRENT;
  155. /*
  156. * Build notification list
  157. */
  158. for (i = 0; i < clusterNodeEntries; i++) {
  159. clusterNotification[i].clusterChanges = SA_CLM_NODE_NO_CHANGE;
  160. memcpy (&clusterNotification[i].clusterNode, &clusterNodes[i],
  161. sizeof (SaClmClusterNodeT));
  162. }
  163. /*
  164. * Send track response
  165. */
  166. res_clm_trackcallback.header.magic = MESSAGE_MAGIC;
  167. res_clm_trackcallback.header.size = sizeof (struct res_clm_trackcallback) +
  168. sizeof (SaClmClusterNotificationT) * i;
  169. res_clm_trackcallback.header.id = MESSAGE_RES_CLM_TRACKCALLBACK;
  170. res_clm_trackcallback.viewNumber = 0;
  171. res_clm_trackcallback.numberOfItems = i;
  172. res_clm_trackcallback.numberOfMembers = i;
  173. res_clm_trackcallback.notificationBufferAddress =
  174. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress;
  175. libais_send_response (conn_info, &res_clm_trackcallback, sizeof (struct res_clm_trackcallback));
  176. libais_send_response (conn_info, clusterNotification, sizeof (SaClmClusterNotificationT) * i);
  177. }
  178. void library_notification_send (SaClmClusterNotificationT *cluster_notification_entries,
  179. int notify_entries)
  180. {
  181. struct res_clm_trackcallback res_clm_trackcallback;
  182. struct conn_info *conn_info;
  183. struct list_head *list;
  184. for (list = library_notification_send_listhead.next;
  185. list != &library_notification_send_listhead;
  186. list = list->next) {
  187. conn_info = list_entry (list, struct conn_info, conn_list);
  188. /*
  189. * Send notifications to all CLM listeners
  190. */
  191. if (notify_entries) {
  192. res_clm_trackcallback.header.magic = MESSAGE_MAGIC;
  193. res_clm_trackcallback.header.size = sizeof (struct res_clm_trackcallback) +
  194. (notify_entries * sizeof (SaClmClusterNotificationT));
  195. res_clm_trackcallback.header.id = MESSAGE_RES_CLM_TRACKCALLBACK;
  196. res_clm_trackcallback.viewNumber = 0;
  197. res_clm_trackcallback.numberOfItems = notify_entries;
  198. res_clm_trackcallback.numberOfMembers = notify_entries;
  199. res_clm_trackcallback.notificationBufferAddress =
  200. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress;
  201. libais_send_response (conn_info, &res_clm_trackcallback,
  202. sizeof (struct res_clm_trackcallback));
  203. libais_send_response (conn_info, cluster_notification_entries,
  204. sizeof (SaClmClusterNotificationT) * notify_entries);
  205. }
  206. }
  207. }
  208. static void libraryNotificationJoin (SaClmNodeIdT node)
  209. {
  210. SaClmClusterNotificationT clusterNotification;
  211. int i;
  212. /*
  213. * Generate notification element
  214. */
  215. clusterNotification.clusterChanges = SA_CLM_NODE_JOINED;
  216. for (i = 0; i < clusterNodeEntries; i++) {
  217. if (node == clusterNodes[i].nodeId) {
  218. memcpy (&clusterNotification.clusterNode, &clusterNodes[i],
  219. sizeof (SaClmClusterNodeT));
  220. }
  221. }
  222. library_notification_send (&clusterNotification, 1);
  223. }
  224. static void libraryNotificationLeave (SaClmNodeIdT *nodes, int nodes_entries)
  225. {
  226. SaClmClusterNotificationT clusterNotification[NODE_MAX];
  227. int i, j;
  228. int notifyEntries;
  229. /*
  230. * Determine notification list
  231. */
  232. for (notifyEntries = 0, i = 0; i < clusterNodeEntries; i++) {
  233. for (j = 0; j < nodes_entries; j++) {
  234. if (clusterNodes[i].nodeId == nodes[j]) {
  235. memcpy (&clusterNotification[notifyEntries].clusterNode,
  236. &clusterNodes[i],
  237. sizeof (SaClmClusterNodeT));
  238. clusterNotification[notifyEntries].clusterChanges = SA_CLM_NODE_LEFT;
  239. notifyEntries += 1;
  240. break;
  241. }
  242. }
  243. }
  244. library_notification_send (clusterNotification, notifyEntries);
  245. /*
  246. * Remove entries from clusterNodes array
  247. */
  248. for (i = 0; i < nodes_entries; i++) {
  249. for (j = 0; j < clusterNodeEntries;) {
  250. if (nodes[i] == clusterNodes[j].nodeId) {
  251. clusterNodeEntries -= 1;
  252. memmove (&clusterNodes[j], &clusterNodes[j + 1],
  253. (clusterNodeEntries - j) * sizeof (SaClmClusterNodeT));
  254. } else {
  255. /*
  256. * next clusterNode entry
  257. */
  258. j++;
  259. }
  260. }
  261. }
  262. }
  263. static int clmNodeJoinSend (void)
  264. {
  265. struct req_exec_clm_nodejoin req_exec_clm_nodejoin;
  266. struct iovec req_exec_clm_iovec;
  267. int result;
  268. req_exec_clm_nodejoin.header.magic = MESSAGE_MAGIC;
  269. req_exec_clm_nodejoin.header.size = sizeof (struct req_exec_clm_nodejoin);
  270. req_exec_clm_nodejoin.header.id = MESSAGE_REQ_EXEC_CLM_NODEJOIN;
  271. // TODO dont use memcpy, use iovecs !!
  272. memcpy (&req_exec_clm_nodejoin.clusterNode, &thisClusterNode,
  273. sizeof (SaClmClusterNodeT));
  274. req_exec_clm_iovec.iov_base = (char *)&req_exec_clm_nodejoin;
  275. req_exec_clm_iovec.iov_len = sizeof (req_exec_clm_nodejoin);
  276. result = gmi_mcast (&aisexec_groupname, &req_exec_clm_iovec, 1, GMI_PRIO_HIGH);
  277. return (result);
  278. }
  279. static int clmConfChg (
  280. struct sockaddr_in *member_list, int member_list_entries,
  281. struct sockaddr_in *left_list, int left_list_entries,
  282. struct sockaddr_in *joined_list, int joined_list_entries) {
  283. int i;
  284. SaClmNodeIdT nodes[NODE_MAX];
  285. log_printf (LOG_LEVEL_NOTICE, "CLM CONFIGURATION CHANGE\n");
  286. log_printf (LOG_LEVEL_NOTICE, "New Configuration:\n");
  287. for (i = 0; i < member_list_entries; i++) {
  288. log_printf (LOG_LEVEL_NOTICE, "\t%s\n", inet_ntoa (member_list[i].sin_addr));
  289. }
  290. log_printf (LOG_LEVEL_NOTICE, "Members Left:\n");
  291. for (i = 0; i < left_list_entries; i++) {
  292. log_printf (LOG_LEVEL_NOTICE, "\t%s\n", inet_ntoa (left_list[i].sin_addr));
  293. }
  294. log_printf (LOG_LEVEL_NOTICE, "Members Joined:\n");
  295. for (i = 0; i < joined_list_entries; i++) {
  296. log_printf (LOG_LEVEL_NOTICE, "\t%s\n", inet_ntoa (joined_list[i].sin_addr));
  297. }
  298. /*
  299. * Send node information to other nodes
  300. */
  301. if (joined_list_entries) {
  302. clmNodeJoinSend ();
  303. }
  304. for (i = 0; i < left_list_entries; i++) {
  305. nodes[i] = left_list[i].sin_addr.s_addr;
  306. }
  307. libraryNotificationLeave (nodes, i);
  308. return (0);
  309. }
  310. static int message_handler_req_exec_clm_nodejoin (void *message, struct in_addr source_addr)
  311. {
  312. struct req_exec_clm_nodejoin *req_exec_clm_nodejoin = (struct req_exec_clm_nodejoin *)message;
  313. int found;
  314. int i;
  315. log_printf (LOG_LEVEL_NOTICE, "got nodejoin message %s\n", req_exec_clm_nodejoin->clusterNode.nodeName.value);
  316. /*
  317. * Determine if nodejoin already received
  318. */
  319. for (found = 0, i = 0; i < clusterNodeEntries; i++) {
  320. if (memcmp (&clusterNodes[i], &req_exec_clm_nodejoin->clusterNode,
  321. sizeof (SaClmClusterNodeT)) == 0) {
  322. found = 1;
  323. }
  324. }
  325. /*
  326. * If not received, add to internal list
  327. */
  328. if (found == 0) {
  329. memcpy (&clusterNodes[clusterNodeEntries],
  330. &req_exec_clm_nodejoin->clusterNode,
  331. sizeof (SaClmClusterNodeT));
  332. clusterNodeEntries += 1;
  333. libraryNotificationJoin (req_exec_clm_nodejoin->clusterNode.nodeId);
  334. }
  335. return (0);
  336. }
  337. static int message_handler_req_clm_init (struct conn_info *conn_info, void *message)
  338. {
  339. SaErrorT error = SA_ERR_SECURITY;
  340. struct res_lib_init res_lib_init;
  341. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize cluster membership service.\n");
  342. if (conn_info->authenticated) {
  343. conn_info->service = SOCKET_SERVICE_CLM;
  344. error = SA_OK;
  345. }
  346. res_lib_init.header.magic = MESSAGE_MAGIC;
  347. res_lib_init.header.size = sizeof (struct res_lib_init);
  348. res_lib_init.header.id = MESSAGE_RES_INIT;
  349. res_lib_init.error = error;
  350. libais_send_response (conn_info, &res_lib_init, sizeof (res_lib_init));
  351. list_init (&conn_info->conn_list);
  352. if (conn_info->authenticated) {
  353. return (0);
  354. }
  355. return (-1);
  356. }
  357. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info, void *message)
  358. {
  359. struct res_lib_activatepoll res_lib_activatepoll;
  360. res_lib_activatepoll.header.magic = MESSAGE_MAGIC;
  361. res_lib_activatepoll.header.size = sizeof (struct res_lib_activatepoll);
  362. res_lib_activatepoll.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  363. libais_send_response (conn_info, &res_lib_activatepoll,
  364. sizeof (struct res_lib_activatepoll));
  365. return (0);
  366. }
  367. int message_handler_req_clm_trackstart (struct conn_info *conn_info, void *message)
  368. {
  369. struct req_clm_trackstart *req_clm_trackstart = (struct req_clm_trackstart *)message;
  370. conn_info->ais_ci.u.libclm_ci.trackFlags = req_clm_trackstart->trackFlags;
  371. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress = req_clm_trackstart->notificationBufferAddress;
  372. list_add (&conn_info->conn_list, &library_notification_send_listhead);
  373. libraryNotificationCurrentState (conn_info);
  374. return (0);
  375. }
  376. static int message_handler_req_clm_trackstop (struct conn_info *conn_info, void *message)
  377. {
  378. conn_info->ais_ci.u.libclm_ci.trackFlags = 0;
  379. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress = 0;
  380. list_del (&conn_info->conn_list);
  381. return (0);
  382. }
  383. static int message_handler_req_clm_nodeget (struct conn_info *conn_info, void *message)
  384. {
  385. struct req_clm_nodeget *req_clm_nodeget = (struct req_clm_nodeget *)message;
  386. struct res_clm_nodeget res_clm_nodeget;
  387. SaClmClusterNodeT *clusterNode = 0;
  388. int valid = 0;
  389. int i;
  390. log_printf (LOG_LEVEL_DEBUG, "nodeget: trying to find node %x\n", (int)req_clm_nodeget->nodeId);
  391. if (req_clm_nodeget->nodeId == SA_CLM_LOCAL_NODE_ID) {
  392. clusterNode = &clusterNodes[0];
  393. valid = 1;
  394. } else
  395. for (i = 0; i < clusterNodeEntries; i++) {
  396. if (clusterNodes[i].nodeId == req_clm_nodeget->nodeId) {
  397. log_printf (LOG_LEVEL_DEBUG, "found host that matches one desired in nodeget.\n");
  398. clusterNode = &clusterNodes[i];
  399. valid = 1;
  400. break;
  401. }
  402. }
  403. res_clm_nodeget.header.magic = MESSAGE_MAGIC;
  404. res_clm_nodeget.header.size = sizeof (struct res_clm_nodeget);
  405. res_clm_nodeget.header.id = MESSAGE_RES_CLM_NODEGET;
  406. res_clm_nodeget.invocation = req_clm_nodeget->invocation;
  407. res_clm_nodeget.clusterNodeAddress = req_clm_nodeget->clusterNodeAddress;
  408. res_clm_nodeget.valid = valid;
  409. if (valid) {
  410. memcpy (&res_clm_nodeget.clusterNode, clusterNode, sizeof (SaClmClusterNodeT));
  411. }
  412. libais_send_response (conn_info, &res_clm_nodeget, sizeof (struct res_clm_nodeget));
  413. return (0);
  414. }