clm.c 15 KB

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