clm.c 14 KB

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