clm.c 17 KB

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