clm.c 16 KB

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