clm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 <linux/if.h>
  41. #include <linux/sockios.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <signal.h>
  48. #include <time.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include "../include/ais_types.h"
  52. #include "../include/ais_msg.h"
  53. #include "../include/list.h"
  54. #include "../include/queue.h"
  55. #include "aispoll.h"
  56. #include "gmi.h"
  57. #include "parse.h"
  58. #include "main.h"
  59. #include "print.h"
  60. #include "mempool.h"
  61. #include "handlers.h"
  62. SaClmClusterChangesT thisClusterNodeLastChange = SA_CLM_NODE_JOINED;
  63. SaClmClusterNodeT thisClusterNode;
  64. #define NODE_MAX 16
  65. SaClmClusterNodeT clusterNodes[NODE_MAX];
  66. int clusterNodeEntries = 0;
  67. static DECLARE_LIST_INIT (library_notification_send_listhead);
  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 (void *message);
  77. static int message_handler_req_clm_init (struct conn_info *conn_info,
  78. void *message);
  79. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  80. void *message);
  81. static int message_handler_req_clm_trackstart (struct conn_info *conn_info,
  82. void *message);
  83. static int message_handler_req_clm_trackstop (struct conn_info *conn_info,
  84. void *message);
  85. static int message_handler_req_clm_nodeget (struct conn_info *conn_info,
  86. void *message);
  87. static int clm_exit_fn (struct conn_info *conn_info);
  88. static int (*clm_libais_handler_fns[]) (struct conn_info *conn_info, void *) = {
  89. message_handler_req_lib_activatepoll,
  90. message_handler_req_clm_trackstart,
  91. message_handler_req_clm_trackstop,
  92. message_handler_req_clm_nodeget
  93. };
  94. static int (*clm_aisexec_handler_fns[]) (void *) = {
  95. message_handler_req_exec_clm_nodejoin
  96. };
  97. struct service_handler clm_service_handler = {
  98. libais_handler_fns: clm_libais_handler_fns,
  99. libais_handler_fns_count: sizeof (clm_libais_handler_fns) / sizeof (int (*)),
  100. aisexec_handler_fns: clm_aisexec_handler_fns ,
  101. aisexec_handler_fns_count: sizeof (clm_aisexec_handler_fns) / sizeof (int (*)),
  102. confchg_fn: clmConfChg,
  103. libais_init_fn: message_handler_req_clm_init,
  104. libais_exit_fn: clm_exit_fn,
  105. aisexec_init_fn: clmExecutiveInitialize
  106. };
  107. static int clmExecutiveInitialize (void)
  108. {
  109. memset (clusterNodes, 0, sizeof (SaClmClusterNodeT) * NODE_MAX);
  110. /*
  111. * Build local cluster node data structure
  112. */
  113. thisClusterNode.nodeId = this_ip.sin_addr.s_addr;
  114. memcpy (&thisClusterNode.nodeAddress.value, &this_ip.sin_addr, sizeof (struct in_addr));
  115. thisClusterNode.nodeAddress.length = sizeof (struct in_addr);
  116. strcpy (thisClusterNode.nodeName.value, (char *)inet_ntoa (this_ip.sin_addr));
  117. thisClusterNode.nodeName.length = strlen (thisClusterNode.nodeName.value);
  118. strcpy (thisClusterNode.clusterName.value, "mvlcge");
  119. thisClusterNode.clusterName.length = strlen ("mvlcge");
  120. thisClusterNode.member = 1;
  121. {
  122. struct sysinfo s_info;
  123. time_t current_time;
  124. sysinfo (&s_info);
  125. current_time = time (NULL);
  126. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  127. thisClusterNode.bootTimestamp = ((SaTimeT)(current_time - s_info.uptime)) * 1000000000;
  128. }
  129. #ifdef DEBUG
  130. printSaClmClusterNodeT ("this cluster node", &thisClusterNode);
  131. #endif
  132. memcpy (&clusterNodes[0], &thisClusterNode, sizeof (SaClmClusterNodeT));
  133. clusterNodeEntries = 1;
  134. return (0);
  135. }
  136. static int clm_exit_fn (struct conn_info *conn_info)
  137. {
  138. /*
  139. * Delete track entry if there is one
  140. */
  141. list_del (&conn_info->conn_list);
  142. return (0);
  143. }
  144. static void libraryNotificationCurrentState (struct conn_info *conn_info)
  145. {
  146. struct res_clm_trackcallback res_clm_trackcallback;
  147. SaClmClusterNotificationT clusterNotification[NODE_MAX];
  148. int i;
  149. if ((conn_info->ais_ci.u.libclm_ci.trackFlags & SA_TRACK_CURRENT) == 0) {
  150. return;
  151. }
  152. /*
  153. * Turn off track current
  154. */
  155. conn_info->ais_ci.u.libclm_ci.trackFlags &= ~SA_TRACK_CURRENT;
  156. /*
  157. * Build notification list
  158. */
  159. for (i = 0; i < clusterNodeEntries; i++) {
  160. clusterNotification[i].clusterChanges = SA_CLM_NODE_NO_CHANGE;
  161. memcpy (&clusterNotification[i].clusterNode, &clusterNodes[i],
  162. sizeof (SaClmClusterNodeT));
  163. }
  164. /*
  165. * Send track response
  166. */
  167. res_clm_trackcallback.header.magic = MESSAGE_MAGIC;
  168. res_clm_trackcallback.header.size = sizeof (struct res_clm_trackcallback) +
  169. sizeof (SaClmClusterNotificationT) * i;
  170. res_clm_trackcallback.header.id = MESSAGE_RES_CLM_TRACKCALLBACK;
  171. res_clm_trackcallback.viewNumber = 0;
  172. res_clm_trackcallback.numberOfItems = i;
  173. res_clm_trackcallback.numberOfMembers = i;
  174. res_clm_trackcallback.notificationBufferAddress =
  175. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress;
  176. libais_send_response (conn_info, &res_clm_trackcallback, sizeof (struct res_clm_trackcallback));
  177. libais_send_response (conn_info, clusterNotification, sizeof (SaClmClusterNotificationT) * i);
  178. }
  179. void library_notification_send (SaClmClusterNotificationT *cluster_notification_entries,
  180. int notify_entries)
  181. {
  182. struct res_clm_trackcallback res_clm_trackcallback;
  183. struct conn_info *conn_info;
  184. struct list_head *list;
  185. for (list = library_notification_send_listhead.next;
  186. list != &library_notification_send_listhead;
  187. list = list->next) {
  188. conn_info = list_entry (list, struct conn_info, conn_list);
  189. /*
  190. * Send notifications to all CLM listeners
  191. */
  192. if (notify_entries) {
  193. res_clm_trackcallback.header.magic = MESSAGE_MAGIC;
  194. res_clm_trackcallback.header.size = sizeof (struct res_clm_trackcallback) +
  195. (notify_entries * sizeof (SaClmClusterNotificationT));
  196. res_clm_trackcallback.header.id = MESSAGE_RES_CLM_TRACKCALLBACK;
  197. res_clm_trackcallback.viewNumber = 0;
  198. res_clm_trackcallback.numberOfItems = notify_entries;
  199. res_clm_trackcallback.numberOfMembers = notify_entries;
  200. res_clm_trackcallback.notificationBufferAddress =
  201. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress;
  202. libais_send_response (conn_info, &res_clm_trackcallback,
  203. sizeof (struct res_clm_trackcallback));
  204. libais_send_response (conn_info, cluster_notification_entries,
  205. sizeof (SaClmClusterNotificationT) * notify_entries);
  206. }
  207. }
  208. }
  209. static void libraryNotificationJoin (SaClmNodeIdT node)
  210. {
  211. SaClmClusterNotificationT clusterNotification;
  212. int i;
  213. /*
  214. * Generate notification element
  215. */
  216. clusterNotification.clusterChanges = SA_CLM_NODE_JOINED;
  217. for (i = 0; i < clusterNodeEntries; i++) {
  218. if (node == clusterNodes[i].nodeId) {
  219. memcpy (&clusterNotification.clusterNode, &clusterNodes[i],
  220. sizeof (SaClmClusterNodeT));
  221. }
  222. }
  223. library_notification_send (&clusterNotification, 1);
  224. }
  225. static void libraryNotificationLeave (SaClmNodeIdT *nodes, int nodes_entries)
  226. {
  227. SaClmClusterNotificationT clusterNotification[NODE_MAX];
  228. int i, j;
  229. int notifyEntries;
  230. /*
  231. * Determine notification list
  232. */
  233. for (notifyEntries = 0, i = 0; i < clusterNodeEntries; i++) {
  234. for (j = 0; j < nodes_entries; j++) {
  235. if (clusterNodes[i].nodeId == nodes[j]) {
  236. memcpy (&clusterNotification[notifyEntries].clusterNode,
  237. &clusterNodes[i],
  238. sizeof (SaClmClusterNodeT));
  239. clusterNotification[notifyEntries].clusterChanges = SA_CLM_NODE_LEFT;
  240. notifyEntries += 1;
  241. break;
  242. }
  243. }
  244. }
  245. library_notification_send (clusterNotification, notifyEntries);
  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. memmove (&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 (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 (struct conn_info *conn_info, void *message)
  339. {
  340. SaErrorT error = SA_ERR_SECURITY;
  341. struct res_lib_init res_lib_init;
  342. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize cluster membership service.\n");
  343. if (conn_info->authenticated) {
  344. conn_info->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 (conn_info, &res_lib_init, sizeof (res_lib_init));
  352. list_init (&conn_info->conn_list);
  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. list_add (&conn_info->conn_list, &library_notification_send_listhead);
  374. libraryNotificationCurrentState (conn_info);
  375. return (0);
  376. }
  377. static int message_handler_req_clm_trackstop (struct conn_info *conn_info, void *message)
  378. {
  379. conn_info->ais_ci.u.libclm_ci.trackFlags = 0;
  380. conn_info->ais_ci.u.libclm_ci.notificationBufferAddress = 0;
  381. list_del (&conn_info->conn_list);
  382. return (0);
  383. }
  384. static int message_handler_req_clm_nodeget (struct conn_info *conn_info, void *message)
  385. {
  386. struct req_clm_nodeget *req_clm_nodeget = (struct req_clm_nodeget *)message;
  387. struct res_clm_nodeget res_clm_nodeget;
  388. SaClmClusterNodeT *clusterNode = 0;
  389. int valid = 0;
  390. int i;
  391. log_printf (LOG_LEVEL_DEBUG, "nodeget: trying to find node %x\n", (int)req_clm_nodeget->nodeId);
  392. if (req_clm_nodeget->nodeId == SA_CLM_LOCAL_NODE_ID) {
  393. clusterNode = &clusterNodes[0];
  394. valid = 1;
  395. } else
  396. for (i = 0; i < clusterNodeEntries; i++) {
  397. if (clusterNodes[i].nodeId == req_clm_nodeget->nodeId) {
  398. log_printf (LOG_LEVEL_DEBUG, "found host that matches one desired in nodeget.\n");
  399. clusterNode = &clusterNodes[i];
  400. valid = 1;
  401. break;
  402. }
  403. }
  404. res_clm_nodeget.header.magic = MESSAGE_MAGIC;
  405. res_clm_nodeget.header.size = sizeof (struct res_clm_nodeget);
  406. res_clm_nodeget.header.id = MESSAGE_RES_CLM_NODEGET;
  407. res_clm_nodeget.invocation = req_clm_nodeget->invocation;
  408. res_clm_nodeget.clusterNodeAddress = req_clm_nodeget->clusterNodeAddress;
  409. res_clm_nodeget.valid = valid;
  410. if (valid) {
  411. memcpy (&res_clm_nodeget.clusterNode, clusterNode, sizeof (SaClmClusterNodeT));
  412. }
  413. libais_send_response (conn_info, &res_clm_nodeget, sizeof (struct res_clm_nodeget));
  414. return (0);
  415. }