clm.c 15 KB

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