4 Copyright (C) Ronnie Sahlberg 2007
5 Copyright (C) Andrew Tridgell 2007
6 Copyright (C) Martin Schwenke 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/dlinklist.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/wait.h"
27 #include "../include/ctdb_private.h"
28 #include "../common/rb_tree.h"
31 #define TAKEOVER_TIMEOUT() timeval_current_ofs(ctdb->tunable.takeover_timeout,0)
33 #define CTDB_ARP_INTERVAL 1
34 #define CTDB_ARP_REPEAT 3
36 /* Flags used in IP allocation algorithms. */
40 enum ctdb_runstate runstate;
44 struct ctdb_iface *prev, *next;
50 static const char *ctdb_vnn_iface_string(const struct ctdb_vnn *vnn)
53 return vnn->iface->name;
59 static int ctdb_add_local_iface(struct ctdb_context *ctdb, const char *iface)
63 /* Verify that we dont have an entry for this ip yet */
64 for (i=ctdb->ifaces;i;i=i->next) {
65 if (strcmp(i->name, iface) == 0) {
70 /* create a new structure for this interface */
71 i = talloc_zero(ctdb, struct ctdb_iface);
72 CTDB_NO_MEMORY_FATAL(ctdb, i);
73 i->name = talloc_strdup(i, iface);
74 CTDB_NO_MEMORY(ctdb, i->name);
76 * If link_up defaults to true then IPs can be allocated to a
77 * node during the first recovery. However, then an interface
78 * could have its link marked down during the startup event,
79 * causing the IP to move almost immediately. If link_up
80 * defaults to false then, during normal operation, IPs added
81 * to a new interface can't be assigned until a monitor cycle
82 * has occurred and marked the new interfaces up. This makes
83 * IP allocation unpredictable. The following is a neat
84 * compromise: early in startup link_up defaults to false, so
85 * IPs can't be assigned, and after startup IPs can be
86 * assigned immediately.
88 i->link_up = (ctdb->runstate == CTDB_RUNSTATE_RUNNING);
90 DLIST_ADD(ctdb->ifaces, i);
95 static bool vnn_has_interface_with_name(struct ctdb_vnn *vnn,
100 for (n = 0; vnn->ifaces[n] != NULL; n++) {
101 if (strcmp(name, vnn->ifaces[n]) == 0) {
109 /* If any interfaces now have no possible IPs then delete them. This
110 * implementation is naive (i.e. simple) rather than clever
111 * (i.e. complex). Given that this is run on delip and that operation
112 * is rare, this doesn't need to be efficient - it needs to be
113 * foolproof. One alternative is reference counting, where the logic
114 * is distributed and can, therefore, be broken in multiple places.
115 * Another alternative is to build a red-black tree of interfaces that
116 * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
117 * once) and then walking ctdb->ifaces once and deleting those not in
118 * the tree. Let's go to one of those if the naive implementation
119 * causes problems... :-)
121 static void ctdb_remove_orphaned_ifaces(struct ctdb_context *ctdb,
122 struct ctdb_vnn *vnn)
124 struct ctdb_iface *i, *next;
126 /* For each interface, check if there's an IP using it. */
127 for (i = ctdb->ifaces; i != NULL; i = next) {
132 /* Only consider interfaces named in the given VNN. */
133 if (!vnn_has_interface_with_name(vnn, i->name)) {
137 /* Is the "single IP" on this interface? */
138 if ((ctdb->single_ip_vnn != NULL) &&
139 (ctdb->single_ip_vnn->ifaces[0] != NULL) &&
140 (strcmp(i->name, ctdb->single_ip_vnn->ifaces[0]) == 0)) {
141 /* Found, next interface please... */
144 /* Search for a vnn with this interface. */
146 for (tv=ctdb->vnn; tv; tv=tv->next) {
147 if (vnn_has_interface_with_name(tv, i->name)) {
154 /* None of the VNNs are using this interface. */
155 DLIST_REMOVE(ctdb->ifaces, i);
162 static struct ctdb_iface *ctdb_find_iface(struct ctdb_context *ctdb,
165 struct ctdb_iface *i;
167 for (i=ctdb->ifaces;i;i=i->next) {
168 if (strcmp(i->name, iface) == 0) {
176 static struct ctdb_iface *ctdb_vnn_best_iface(struct ctdb_context *ctdb,
177 struct ctdb_vnn *vnn)
180 struct ctdb_iface *cur = NULL;
181 struct ctdb_iface *best = NULL;
183 for (i=0; vnn->ifaces[i]; i++) {
185 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
199 if (cur->references < best->references) {
208 static int32_t ctdb_vnn_assign_iface(struct ctdb_context *ctdb,
209 struct ctdb_vnn *vnn)
211 struct ctdb_iface *best = NULL;
214 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
215 "still assigned to iface '%s'\n",
216 ctdb_addr_to_str(&vnn->public_address),
217 ctdb_vnn_iface_string(vnn)));
221 best = ctdb_vnn_best_iface(ctdb, vnn);
223 DEBUG(DEBUG_ERR, (__location__ " public address '%s' "
224 "cannot assign to iface any iface\n",
225 ctdb_addr_to_str(&vnn->public_address)));
231 vnn->pnn = ctdb->pnn;
233 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
234 "now assigned to iface '%s' refs[%d]\n",
235 ctdb_addr_to_str(&vnn->public_address),
236 ctdb_vnn_iface_string(vnn),
241 static void ctdb_vnn_unassign_iface(struct ctdb_context *ctdb,
242 struct ctdb_vnn *vnn)
244 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
245 "now unassigned (old iface '%s' refs[%d])\n",
246 ctdb_addr_to_str(&vnn->public_address),
247 ctdb_vnn_iface_string(vnn),
248 vnn->iface?vnn->iface->references:0));
250 vnn->iface->references--;
253 if (vnn->pnn == ctdb->pnn) {
258 static bool ctdb_vnn_available(struct ctdb_context *ctdb,
259 struct ctdb_vnn *vnn)
263 if (vnn->delete_pending) {
267 if (vnn->iface && vnn->iface->link_up) {
271 for (i=0; vnn->ifaces[i]; i++) {
272 struct ctdb_iface *cur;
274 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
287 struct ctdb_takeover_arp {
288 struct ctdb_context *ctdb;
291 struct ctdb_tcp_array *tcparray;
292 struct ctdb_vnn *vnn;
297 lists of tcp endpoints
299 struct ctdb_tcp_list {
300 struct ctdb_tcp_list *prev, *next;
301 struct ctdb_tcp_connection connection;
305 list of clients to kill on IP release
307 struct ctdb_client_ip {
308 struct ctdb_client_ip *prev, *next;
309 struct ctdb_context *ctdb;
316 send a gratuitous arp
318 static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te,
319 struct timeval t, void *private_data)
321 struct ctdb_takeover_arp *arp = talloc_get_type(private_data,
322 struct ctdb_takeover_arp);
324 struct ctdb_tcp_array *tcparray;
325 const char *iface = ctdb_vnn_iface_string(arp->vnn);
327 ret = ctdb_sys_send_arp(&arp->addr, iface);
329 DEBUG(DEBUG_CRIT,(__location__ " sending of arp failed on iface '%s' (%s)\n",
330 iface, strerror(errno)));
333 tcparray = arp->tcparray;
335 for (i=0;i<tcparray->num;i++) {
336 struct ctdb_tcp_connection *tcon;
338 tcon = &tcparray->connections[i];
339 DEBUG(DEBUG_INFO,("sending tcp tickle ack for %u->%s:%u\n",
340 (unsigned)ntohs(tcon->dst_addr.ip.sin_port),
341 ctdb_addr_to_str(&tcon->src_addr),
342 (unsigned)ntohs(tcon->src_addr.ip.sin_port)));
343 ret = ctdb_sys_send_tcp(
348 DEBUG(DEBUG_CRIT,(__location__ " Failed to send tcp tickle ack for %s\n",
349 ctdb_addr_to_str(&tcon->src_addr)));
356 if (arp->count == CTDB_ARP_REPEAT) {
361 event_add_timed(arp->ctdb->ev, arp->vnn->takeover_ctx,
362 timeval_current_ofs(CTDB_ARP_INTERVAL, 100000),
363 ctdb_control_send_arp, arp);
366 static int32_t ctdb_announce_vnn_iface(struct ctdb_context *ctdb,
367 struct ctdb_vnn *vnn)
369 struct ctdb_takeover_arp *arp;
370 struct ctdb_tcp_array *tcparray;
372 if (!vnn->takeover_ctx) {
373 vnn->takeover_ctx = talloc_new(vnn);
374 if (!vnn->takeover_ctx) {
379 arp = talloc_zero(vnn->takeover_ctx, struct ctdb_takeover_arp);
385 arp->addr = vnn->public_address;
388 tcparray = vnn->tcp_array;
390 /* add all of the known tcp connections for this IP to the
391 list of tcp connections to send tickle acks for */
392 arp->tcparray = talloc_steal(arp, tcparray);
394 vnn->tcp_array = NULL;
395 vnn->tcp_update_needed = true;
398 event_add_timed(arp->ctdb->ev, vnn->takeover_ctx,
399 timeval_zero(), ctdb_control_send_arp, arp);
404 struct takeover_callback_state {
405 struct ctdb_req_control *c;
406 ctdb_sock_addr *addr;
407 struct ctdb_vnn *vnn;
410 struct ctdb_do_takeip_state {
411 struct ctdb_req_control *c;
412 struct ctdb_vnn *vnn;
416 called when takeip event finishes
418 static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
421 struct ctdb_do_takeip_state *state =
422 talloc_get_type(private_data, struct ctdb_do_takeip_state);
427 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
429 if (status == -ETIME) {
432 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
433 ctdb_addr_to_str(&state->vnn->public_address),
434 ctdb_vnn_iface_string(state->vnn)));
435 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
437 node->flags |= NODE_FLAGS_UNHEALTHY;
442 if (ctdb->do_checkpublicip) {
444 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
446 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
453 data.dptr = (uint8_t *)ctdb_addr_to_str(&state->vnn->public_address);
454 data.dsize = strlen((char *)data.dptr) + 1;
455 DEBUG(DEBUG_INFO,(__location__ " sending TAKE_IP for '%s'\n", data.dptr));
457 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_TAKE_IP, data);
460 /* the control succeeded */
461 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
466 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state *state)
468 state->vnn->update_in_flight = false;
473 take over an ip address
475 static int32_t ctdb_do_takeip(struct ctdb_context *ctdb,
476 struct ctdb_req_control *c,
477 struct ctdb_vnn *vnn)
480 struct ctdb_do_takeip_state *state;
482 if (vnn->update_in_flight) {
483 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u rejected "
484 "update for this IP already in flight\n",
485 ctdb_addr_to_str(&vnn->public_address),
486 vnn->public_netmask_bits));
490 ret = ctdb_vnn_assign_iface(ctdb, vnn);
492 DEBUG(DEBUG_ERR,("Takeover of IP %s/%u failed to "
493 "assign a usable interface\n",
494 ctdb_addr_to_str(&vnn->public_address),
495 vnn->public_netmask_bits));
499 state = talloc(vnn, struct ctdb_do_takeip_state);
500 CTDB_NO_MEMORY(ctdb, state);
502 state->c = talloc_steal(ctdb, c);
505 vnn->update_in_flight = true;
506 talloc_set_destructor(state, ctdb_takeip_destructor);
508 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u on interface %s\n",
509 ctdb_addr_to_str(&vnn->public_address),
510 vnn->public_netmask_bits,
511 ctdb_vnn_iface_string(vnn)));
513 ret = ctdb_event_script_callback(ctdb,
515 ctdb_do_takeip_callback,
519 ctdb_vnn_iface_string(vnn),
520 ctdb_addr_to_str(&vnn->public_address),
521 vnn->public_netmask_bits);
524 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
525 ctdb_addr_to_str(&vnn->public_address),
526 ctdb_vnn_iface_string(vnn)));
534 struct ctdb_do_updateip_state {
535 struct ctdb_req_control *c;
536 struct ctdb_iface *old;
537 struct ctdb_vnn *vnn;
541 called when updateip event finishes
543 static void ctdb_do_updateip_callback(struct ctdb_context *ctdb, int status,
546 struct ctdb_do_updateip_state *state =
547 talloc_get_type(private_data, struct ctdb_do_updateip_state);
551 if (status == -ETIME) {
554 DEBUG(DEBUG_ERR,(__location__ " Failed to move IP %s from interface %s to %s\n",
555 ctdb_addr_to_str(&state->vnn->public_address),
557 ctdb_vnn_iface_string(state->vnn)));
560 * All we can do is reset the old interface
561 * and let the next run fix it
563 ctdb_vnn_unassign_iface(ctdb, state->vnn);
564 state->vnn->iface = state->old;
565 state->vnn->iface->references++;
567 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
572 if (ctdb->do_checkpublicip) {
574 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
576 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
583 /* the control succeeded */
584 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
589 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state *state)
591 state->vnn->update_in_flight = false;
596 update (move) an ip address
598 static int32_t ctdb_do_updateip(struct ctdb_context *ctdb,
599 struct ctdb_req_control *c,
600 struct ctdb_vnn *vnn)
603 struct ctdb_do_updateip_state *state;
604 struct ctdb_iface *old = vnn->iface;
605 const char *new_name;
607 if (vnn->update_in_flight) {
608 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u rejected "
609 "update for this IP already in flight\n",
610 ctdb_addr_to_str(&vnn->public_address),
611 vnn->public_netmask_bits));
615 ctdb_vnn_unassign_iface(ctdb, vnn);
616 ret = ctdb_vnn_assign_iface(ctdb, vnn);
618 DEBUG(DEBUG_ERR,("update of IP %s/%u failed to "
619 "assin a usable interface (old iface '%s')\n",
620 ctdb_addr_to_str(&vnn->public_address),
621 vnn->public_netmask_bits,
626 new_name = ctdb_vnn_iface_string(vnn);
627 if (old->name != NULL && new_name != NULL && !strcmp(old->name, new_name)) {
628 /* A benign update from one interface onto itself.
629 * no need to run the eventscripts in this case, just return
632 ctdb_request_control_reply(ctdb, c, NULL, 0, NULL);
636 state = talloc(vnn, struct ctdb_do_updateip_state);
637 CTDB_NO_MEMORY(ctdb, state);
639 state->c = talloc_steal(ctdb, c);
643 vnn->update_in_flight = true;
644 talloc_set_destructor(state, ctdb_updateip_destructor);
646 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u from "
647 "interface %s to %s\n",
648 ctdb_addr_to_str(&vnn->public_address),
649 vnn->public_netmask_bits,
653 ret = ctdb_event_script_callback(ctdb,
655 ctdb_do_updateip_callback,
657 CTDB_EVENT_UPDATE_IP,
661 ctdb_addr_to_str(&vnn->public_address),
662 vnn->public_netmask_bits);
664 DEBUG(DEBUG_ERR,(__location__ " Failed update IP %s from interface %s to %s\n",
665 ctdb_addr_to_str(&vnn->public_address),
666 old->name, new_name));
675 Find the vnn of the node that has a public ip address
676 returns -1 if the address is not known as a public address
678 static struct ctdb_vnn *find_public_ip_vnn(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
680 struct ctdb_vnn *vnn;
682 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
683 if (ctdb_same_ip(&vnn->public_address, addr)) {
692 take over an ip address
694 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
695 struct ctdb_req_control *c,
700 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
701 struct ctdb_vnn *vnn;
702 bool have_ip = false;
703 bool do_updateip = false;
704 bool do_takeip = false;
705 struct ctdb_iface *best_iface = NULL;
707 if (pip->pnn != ctdb->pnn) {
708 DEBUG(DEBUG_ERR,(__location__" takeoverip called for an ip '%s' "
709 "with pnn %d, but we're node %d\n",
710 ctdb_addr_to_str(&pip->addr),
711 pip->pnn, ctdb->pnn));
715 /* update out vnn list */
716 vnn = find_public_ip_vnn(ctdb, &pip->addr);
718 DEBUG(DEBUG_INFO,("takeoverip called for an ip '%s' that is not a public address\n",
719 ctdb_addr_to_str(&pip->addr)));
723 if (ctdb->do_checkpublicip) {
724 have_ip = ctdb_sys_have_ip(&pip->addr);
726 best_iface = ctdb_vnn_best_iface(ctdb, vnn);
727 if (best_iface == NULL) {
728 DEBUG(DEBUG_ERR,("takeoverip of IP %s/%u failed to find"
729 "a usable interface (old %s, have_ip %d)\n",
730 ctdb_addr_to_str(&vnn->public_address),
731 vnn->public_netmask_bits,
732 ctdb_vnn_iface_string(vnn),
737 if (vnn->iface == NULL && vnn->pnn == -1 && have_ip && best_iface != NULL) {
738 DEBUG(DEBUG_ERR,("Taking over newly created ip\n"));
743 if (vnn->iface == NULL && have_ip) {
744 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
745 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
746 ctdb_addr_to_str(&vnn->public_address)));
750 if (vnn->pnn != ctdb->pnn && have_ip && vnn->pnn != -1) {
751 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
752 "and we have it on iface[%s], but it was assigned to node %d"
753 "and we are node %d, banning ourself\n",
754 ctdb_addr_to_str(&vnn->public_address),
755 ctdb_vnn_iface_string(vnn), vnn->pnn, ctdb->pnn));
760 if (vnn->pnn == -1 && have_ip) {
761 vnn->pnn = ctdb->pnn;
762 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
763 "and we already have it on iface[%s], update local daemon\n",
764 ctdb_addr_to_str(&vnn->public_address),
765 ctdb_vnn_iface_string(vnn)));
770 if (vnn->iface != best_iface) {
771 if (!vnn->iface->link_up) {
773 } else if (vnn->iface->references > (best_iface->references + 1)) {
774 /* only move when the rebalance gains something */
782 ctdb_vnn_unassign_iface(ctdb, vnn);
789 ret = ctdb_do_takeip(ctdb, c, vnn);
793 } else if (do_updateip) {
794 ret = ctdb_do_updateip(ctdb, c, vnn);
800 * The interface is up and the kernel known the ip
803 DEBUG(DEBUG_INFO,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
804 ctdb_addr_to_str(&pip->addr),
805 vnn->public_netmask_bits,
806 ctdb_vnn_iface_string(vnn)));
810 /* tell ctdb_control.c that we will be replying asynchronously */
817 takeover an ip address old v4 style
819 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb,
820 struct ctdb_req_control *c,
826 data.dsize = sizeof(struct ctdb_public_ip);
827 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
828 CTDB_NO_MEMORY(ctdb, data.dptr);
830 memcpy(data.dptr, indata.dptr, indata.dsize);
831 return ctdb_control_takeover_ip(ctdb, c, data, async_reply);
835 kill any clients that are registered with a IP that is being released
837 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
839 struct ctdb_client_ip *ip;
841 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
842 ctdb_addr_to_str(addr)));
844 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
845 ctdb_sock_addr tmp_addr;
848 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
850 ctdb_addr_to_str(&ip->addr)));
852 if (ctdb_same_ip(&tmp_addr, addr)) {
853 struct ctdb_client *client = ctdb_reqid_find(ctdb,
856 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
858 ctdb_addr_to_str(&ip->addr),
861 if (client->pid != 0) {
862 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
863 (unsigned)client->pid,
864 ctdb_addr_to_str(addr),
866 kill(client->pid, SIGKILL);
872 static void do_delete_ip(struct ctdb_context *ctdb, struct ctdb_vnn *vnn)
874 DLIST_REMOVE(ctdb->vnn, vnn);
875 ctdb_vnn_unassign_iface(ctdb, vnn);
876 ctdb_remove_orphaned_ifaces(ctdb, vnn);
881 called when releaseip event finishes
883 static void release_ip_callback(struct ctdb_context *ctdb, int status,
886 struct takeover_callback_state *state =
887 talloc_get_type(private_data, struct takeover_callback_state);
890 if (status == -ETIME) {
894 if (ctdb->do_checkpublicip && ctdb_sys_have_ip(state->addr)) {
895 DEBUG(DEBUG_ERR, ("IP %s still hosted during release IP callback, failing\n",
896 ctdb_addr_to_str(state->addr)));
897 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
902 /* send a message to all clients of this node telling them
903 that the cluster has been reconfigured and they should
904 release any sockets on this IP */
905 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
906 CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
907 data.dsize = strlen((char *)data.dptr)+1;
909 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
911 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
913 /* kill clients that have registered with this IP */
914 release_kill_clients(ctdb, state->addr);
916 ctdb_vnn_unassign_iface(ctdb, state->vnn);
918 /* Process the IP if it has been marked for deletion */
919 if (state->vnn->delete_pending) {
920 do_delete_ip(ctdb, state->vnn);
924 /* the control succeeded */
925 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
929 static int ctdb_releaseip_destructor(struct takeover_callback_state *state)
931 if (state->vnn != NULL) {
932 state->vnn->update_in_flight = false;
938 release an ip address
940 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
941 struct ctdb_req_control *c,
946 struct takeover_callback_state *state;
947 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
948 struct ctdb_vnn *vnn;
951 /* update our vnn list */
952 vnn = find_public_ip_vnn(ctdb, &pip->addr);
954 DEBUG(DEBUG_INFO,("releaseip called for an ip '%s' that is not a public address\n",
955 ctdb_addr_to_str(&pip->addr)));
960 /* stop any previous arps */
961 talloc_free(vnn->takeover_ctx);
962 vnn->takeover_ctx = NULL;
964 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
965 * lazy multicast to drop an IP from any node that isn't the
966 * intended new node. The following causes makes ctdbd ignore
967 * a release for any address it doesn't host.
969 if (ctdb->do_checkpublicip) {
970 if (!ctdb_sys_have_ip(&pip->addr)) {
971 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
972 ctdb_addr_to_str(&pip->addr),
973 vnn->public_netmask_bits,
974 ctdb_vnn_iface_string(vnn)));
975 ctdb_vnn_unassign_iface(ctdb, vnn);
979 if (vnn->iface == NULL) {
980 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u (ip not held)\n",
981 ctdb_addr_to_str(&pip->addr),
982 vnn->public_netmask_bits));
987 /* There is a potential race between take_ip and us because we
988 * update the VNN via a callback that run when the
989 * eventscripts have been run. Avoid the race by allowing one
990 * update to be in flight at a time.
992 if (vnn->update_in_flight) {
993 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u rejected "
994 "update for this IP already in flight\n",
995 ctdb_addr_to_str(&vnn->public_address),
996 vnn->public_netmask_bits));
1000 iface = strdup(ctdb_vnn_iface_string(vnn));
1002 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s node:%d\n",
1003 ctdb_addr_to_str(&pip->addr),
1004 vnn->public_netmask_bits,
1008 state = talloc(ctdb, struct takeover_callback_state);
1009 if (state == NULL) {
1010 ctdb_set_error(ctdb, "Out of memory at %s:%d",
1011 __FILE__, __LINE__);
1016 state->c = talloc_steal(state, c);
1017 state->addr = talloc(state, ctdb_sock_addr);
1018 if (state->addr == NULL) {
1019 ctdb_set_error(ctdb, "Out of memory at %s:%d",
1020 __FILE__, __LINE__);
1025 *state->addr = pip->addr;
1028 vnn->update_in_flight = true;
1029 talloc_set_destructor(state, ctdb_releaseip_destructor);
1031 ret = ctdb_event_script_callback(ctdb,
1032 state, release_ip_callback, state,
1033 CTDB_EVENT_RELEASE_IP,
1036 ctdb_addr_to_str(&pip->addr),
1037 vnn->public_netmask_bits);
1040 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
1041 ctdb_addr_to_str(&pip->addr),
1042 ctdb_vnn_iface_string(vnn)));
1047 /* tell the control that we will be reply asynchronously */
1048 *async_reply = true;
1053 release an ip address old v4 style
1055 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb,
1056 struct ctdb_req_control *c,
1062 data.dsize = sizeof(struct ctdb_public_ip);
1063 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
1064 CTDB_NO_MEMORY(ctdb, data.dptr);
1066 memcpy(data.dptr, indata.dptr, indata.dsize);
1067 return ctdb_control_release_ip(ctdb, c, data, async_reply);
1071 static int ctdb_add_public_address(struct ctdb_context *ctdb,
1072 ctdb_sock_addr *addr,
1073 unsigned mask, const char *ifaces,
1076 struct ctdb_vnn *vnn;
1083 tmp = strdup(ifaces);
1084 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1085 if (!ctdb_sys_check_iface_exists(iface)) {
1086 DEBUG(DEBUG_CRIT,("Interface %s does not exist. Can not add public-address : %s\n", iface, ctdb_addr_to_str(addr)));
1093 /* Verify that we dont have an entry for this ip yet */
1094 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1095 if (ctdb_same_sockaddr(addr, &vnn->public_address)) {
1096 DEBUG(DEBUG_CRIT,("Same ip '%s' specified multiple times in the public address list \n",
1097 ctdb_addr_to_str(addr)));
1102 /* create a new vnn structure for this ip address */
1103 vnn = talloc_zero(ctdb, struct ctdb_vnn);
1104 CTDB_NO_MEMORY_FATAL(ctdb, vnn);
1105 vnn->ifaces = talloc_array(vnn, const char *, num + 2);
1106 tmp = talloc_strdup(vnn, ifaces);
1107 CTDB_NO_MEMORY_FATAL(ctdb, tmp);
1108 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1109 vnn->ifaces = talloc_realloc(vnn, vnn->ifaces, const char *, num + 2);
1110 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces);
1111 vnn->ifaces[num] = talloc_strdup(vnn, iface);
1112 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces[num]);
1116 vnn->ifaces[num] = NULL;
1117 vnn->public_address = *addr;
1118 vnn->public_netmask_bits = mask;
1120 if (check_address) {
1121 if (ctdb_sys_have_ip(addr)) {
1122 DEBUG(DEBUG_ERR,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr), ctdb->pnn));
1123 vnn->pnn = ctdb->pnn;
1127 for (i=0; vnn->ifaces[i]; i++) {
1128 ret = ctdb_add_local_iface(ctdb, vnn->ifaces[i]);
1130 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1131 "for public_address[%s]\n",
1132 vnn->ifaces[i], ctdb_addr_to_str(addr)));
1138 DLIST_ADD(ctdb->vnn, vnn);
1144 setup the public address lists from a file
1146 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
1152 lines = file_lines_load(ctdb->public_addresses_file, &nlines, 0, ctdb);
1153 if (lines == NULL) {
1154 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", ctdb->public_addresses_file);
1157 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1161 for (i=0;i<nlines;i++) {
1163 ctdb_sock_addr addr;
1164 const char *addrstr;
1169 while ((*line == ' ') || (*line == '\t')) {
1175 if (strcmp(line, "") == 0) {
1178 tok = strtok(line, " \t");
1180 tok = strtok(NULL, " \t");
1182 if (NULL == ctdb->default_public_interface) {
1183 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
1188 ifaces = ctdb->default_public_interface;
1193 if (!addrstr || !parse_ip_mask(addrstr, ifaces, &addr, &mask)) {
1194 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
1198 if (ctdb_add_public_address(ctdb, &addr, mask, ifaces, check_addresses)) {
1199 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
1210 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1214 struct ctdb_vnn *svnn;
1215 struct ctdb_iface *cur = NULL;
1219 svnn = talloc_zero(ctdb, struct ctdb_vnn);
1220 CTDB_NO_MEMORY(ctdb, svnn);
1222 svnn->ifaces = talloc_array(svnn, const char *, 2);
1223 CTDB_NO_MEMORY(ctdb, svnn->ifaces);
1224 svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
1225 CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
1226 svnn->ifaces[1] = NULL;
1228 ok = parse_ip(ip, iface, 0, &svnn->public_address);
1234 ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
1236 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1237 "for single_ip[%s]\n",
1239 ctdb_addr_to_str(&svnn->public_address)));
1244 /* assume the single public ip interface is initially "good" */
1245 cur = ctdb_find_iface(ctdb, iface);
1247 DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
1250 cur->link_up = true;
1252 ret = ctdb_vnn_assign_iface(ctdb, svnn);
1258 ctdb->single_ip_vnn = svnn;
1262 struct ctdb_public_ip_list {
1263 struct ctdb_public_ip_list *next;
1265 ctdb_sock_addr addr;
1268 /* Given a physical node, return the number of
1269 public addresses that is currently assigned to this node.
1271 static int node_ip_coverage(struct ctdb_context *ctdb,
1273 struct ctdb_public_ip_list *ips)
1277 for (;ips;ips=ips->next) {
1278 if (ips->pnn == pnn) {
1286 /* Can the given node host the given IP: is the public IP known to the
1287 * node and is NOIPHOST unset?
1289 static bool can_node_host_ip(struct ctdb_context *ctdb, int32_t pnn,
1290 struct ctdb_ipflags ipflags,
1291 struct ctdb_public_ip_list *ip)
1293 struct ctdb_all_public_ips *public_ips;
1296 if (ipflags.noiphost) {
1300 public_ips = ctdb->nodes[pnn]->available_public_ips;
1302 if (public_ips == NULL) {
1306 for (i=0; i<public_ips->num; i++) {
1307 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
1308 /* yes, this node can serve this public ip */
1316 static bool can_node_takeover_ip(struct ctdb_context *ctdb, int32_t pnn,
1317 struct ctdb_ipflags ipflags,
1318 struct ctdb_public_ip_list *ip)
1320 if (ipflags.noiptakeover) {
1324 return can_node_host_ip(ctdb, pnn, ipflags, ip);
1327 /* search the node lists list for a node to takeover this ip.
1328 pick the node that currently are serving the least number of ips
1329 so that the ips get spread out evenly.
1331 static int find_takeover_node(struct ctdb_context *ctdb,
1332 struct ctdb_ipflags *ipflags,
1333 struct ctdb_public_ip_list *ip,
1334 struct ctdb_public_ip_list *all_ips)
1336 int pnn, min=0, num;
1339 numnodes = talloc_array_length(ipflags);
1341 for (i=0; i<numnodes; i++) {
1342 /* verify that this node can serve this ip */
1343 if (!can_node_takeover_ip(ctdb, i, ipflags[i], ip)) {
1344 /* no it couldnt so skip to the next node */
1348 num = node_ip_coverage(ctdb, i, all_ips);
1349 /* was this the first node we checked ? */
1361 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
1362 ctdb_addr_to_str(&ip->addr)));
1372 static uint32_t *ip_key(ctdb_sock_addr *ip)
1374 static uint32_t key[IP_KEYLEN];
1376 bzero(key, sizeof(key));
1378 switch (ip->sa.sa_family) {
1380 key[3] = htonl(ip->ip.sin_addr.s_addr);
1383 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1384 key[0] = htonl(s6_a32[0]);
1385 key[1] = htonl(s6_a32[1]);
1386 key[2] = htonl(s6_a32[2]);
1387 key[3] = htonl(s6_a32[3]);
1391 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1398 static void *add_ip_callback(void *parm, void *data)
1400 struct ctdb_public_ip_list *this_ip = parm;
1401 struct ctdb_public_ip_list *prev_ip = data;
1403 if (prev_ip == NULL) {
1406 if (this_ip->pnn == -1) {
1407 this_ip->pnn = prev_ip->pnn;
1413 static int getips_count_callback(void *param, void *data)
1415 struct ctdb_public_ip_list **ip_list = (struct ctdb_public_ip_list **)param;
1416 struct ctdb_public_ip_list *new_ip = (struct ctdb_public_ip_list *)data;
1418 new_ip->next = *ip_list;
1423 static struct ctdb_public_ip_list *
1424 create_merged_ip_list(struct ctdb_context *ctdb)
1427 struct ctdb_public_ip_list *ip_list;
1428 struct ctdb_all_public_ips *public_ips;
1430 if (ctdb->ip_tree != NULL) {
1431 talloc_free(ctdb->ip_tree);
1432 ctdb->ip_tree = NULL;
1434 ctdb->ip_tree = trbt_create(ctdb, 0);
1436 for (i=0;i<ctdb->num_nodes;i++) {
1437 public_ips = ctdb->nodes[i]->known_public_ips;
1439 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
1443 /* there were no public ips for this node */
1444 if (public_ips == NULL) {
1448 for (j=0;j<public_ips->num;j++) {
1449 struct ctdb_public_ip_list *tmp_ip;
1451 tmp_ip = talloc_zero(ctdb->ip_tree, struct ctdb_public_ip_list);
1452 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
1453 /* Do not use information about IP addresses hosted
1454 * on other nodes, it may not be accurate */
1455 if (public_ips->ips[j].pnn == ctdb->nodes[i]->pnn) {
1456 tmp_ip->pnn = public_ips->ips[j].pnn;
1460 tmp_ip->addr = public_ips->ips[j].addr;
1461 tmp_ip->next = NULL;
1463 trbt_insertarray32_callback(ctdb->ip_tree,
1464 IP_KEYLEN, ip_key(&public_ips->ips[j].addr),
1471 trbt_traversearray32(ctdb->ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
1477 * This is the length of the longtest common prefix between the IPs.
1478 * It is calculated by XOR-ing the 2 IPs together and counting the
1479 * number of leading zeroes. The implementation means that all
1480 * addresses end up being 128 bits long.
1482 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1483 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1484 * lots of nodes and IP addresses?
1486 static uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
1488 uint32_t ip1_k[IP_KEYLEN];
1493 uint32_t distance = 0;
1495 memcpy(ip1_k, ip_key(ip1), sizeof(ip1_k));
1497 for (i=0; i<IP_KEYLEN; i++) {
1498 x = ip1_k[i] ^ t[i];
1502 /* Count number of leading zeroes.
1503 * FIXME? This could be optimised...
1505 while ((x & (1 << 31)) == 0) {
1515 /* Calculate the IP distance for the given IP relative to IPs on the
1516 given node. The ips argument is generally the all_ips variable
1517 used in the main part of the algorithm.
1519 static uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1520 struct ctdb_public_ip_list *ips,
1523 struct ctdb_public_ip_list *t;
1528 for (t=ips; t != NULL; t=t->next) {
1529 if (t->pnn != pnn) {
1533 /* Optimisation: We never calculate the distance
1534 * between an address and itself. This allows us to
1535 * calculate the effect of removing an address from a
1536 * node by simply calculating the distance between
1537 * that address and all of the exitsing addresses.
1538 * Moreover, we assume that we're only ever dealing
1539 * with addresses from all_ips so we can identify an
1540 * address via a pointer rather than doing a more
1541 * expensive address comparison. */
1542 if (&(t->addr) == ip) {
1546 d = ip_distance(ip, &(t->addr));
1547 sum += d * d; /* Cheaper than pulling in math.h :-) */
1553 /* Return the LCP2 imbalance metric for addresses currently assigned
1556 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn)
1558 struct ctdb_public_ip_list *t;
1560 uint32_t imbalance = 0;
1562 for (t=all_ips; t!=NULL; t=t->next) {
1563 if (t->pnn != pnn) {
1566 /* Pass the rest of the IPs rather than the whole
1569 imbalance += ip_distance_2_sum(&(t->addr), t->next, pnn);
1575 /* Allocate any unassigned IPs just by looping through the IPs and
1576 * finding the best node for each.
1578 static void basic_allocate_unassigned(struct ctdb_context *ctdb,
1579 struct ctdb_ipflags *ipflags,
1580 struct ctdb_public_ip_list *all_ips)
1582 struct ctdb_public_ip_list *tmp_ip;
1584 /* loop over all ip's and find a physical node to cover for
1587 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1588 if (tmp_ip->pnn == -1) {
1589 if (find_takeover_node(ctdb, ipflags, tmp_ip, all_ips)) {
1590 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1591 ctdb_addr_to_str(&tmp_ip->addr)));
1597 /* Basic non-deterministic rebalancing algorithm.
1599 static void basic_failback(struct ctdb_context *ctdb,
1600 struct ctdb_ipflags *ipflags,
1601 struct ctdb_public_ip_list *all_ips,
1605 int maxnode, maxnum, minnode, minnum, num, retries;
1606 struct ctdb_public_ip_list *tmp_ip;
1608 numnodes = talloc_array_length(ipflags);
1615 /* for each ip address, loop over all nodes that can serve
1616 this ip and make sure that the difference between the node
1617 serving the most and the node serving the least ip's are
1620 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1621 if (tmp_ip->pnn == -1) {
1625 /* Get the highest and lowest number of ips's served by any
1626 valid node which can serve this ip.
1630 for (i=0; i<numnodes; i++) {
1631 /* only check nodes that can actually serve this ip */
1632 if (!can_node_takeover_ip(ctdb, i, ipflags[i], tmp_ip)) {
1633 /* no it couldnt so skip to the next node */
1637 num = node_ip_coverage(ctdb, i, all_ips);
1638 if (maxnode == -1) {
1647 if (minnode == -1) {
1657 if (maxnode == -1) {
1658 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
1659 ctdb_addr_to_str(&tmp_ip->addr)));
1664 /* if the spread between the smallest and largest coverage by
1665 a node is >=2 we steal one of the ips from the node with
1666 most coverage to even things out a bit.
1667 try to do this a limited number of times since we dont
1668 want to spend too much time balancing the ip coverage.
1670 if ( (maxnum > minnum+1)
1671 && (retries < (num_ips + 5)) ){
1672 struct ctdb_public_ip_list *tmp;
1674 /* Reassign one of maxnode's VNNs */
1675 for (tmp=all_ips;tmp;tmp=tmp->next) {
1676 if (tmp->pnn == maxnode) {
1677 (void)find_takeover_node(ctdb, ipflags, tmp, all_ips);
1686 static void lcp2_init(struct ctdb_context *tmp_ctx,
1687 struct ctdb_ipflags *ipflags,
1688 struct ctdb_public_ip_list *all_ips,
1689 uint32_t *force_rebalance_nodes,
1690 uint32_t **lcp2_imbalances,
1691 bool **rebalance_candidates)
1694 struct ctdb_public_ip_list *tmp_ip;
1696 numnodes = talloc_array_length(ipflags);
1698 *rebalance_candidates = talloc_array(tmp_ctx, bool, numnodes);
1699 CTDB_NO_MEMORY_FATAL(tmp_ctx, *rebalance_candidates);
1700 *lcp2_imbalances = talloc_array(tmp_ctx, uint32_t, numnodes);
1701 CTDB_NO_MEMORY_FATAL(tmp_ctx, *lcp2_imbalances);
1703 for (i=0; i<numnodes; i++) {
1704 (*lcp2_imbalances)[i] = lcp2_imbalance(all_ips, i);
1705 /* First step: assume all nodes are candidates */
1706 (*rebalance_candidates)[i] = true;
1709 /* 2nd step: if a node has IPs assigned then it must have been
1710 * healthy before, so we remove it from consideration. This
1711 * is overkill but is all we have because we don't maintain
1712 * state between takeover runs. An alternative would be to
1713 * keep state and invalidate it every time the recovery master
1716 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1717 if (tmp_ip->pnn != -1) {
1718 (*rebalance_candidates)[tmp_ip->pnn] = false;
1722 /* 3rd step: if a node is forced to re-balance then
1723 we allow failback onto the node */
1724 if (force_rebalance_nodes == NULL) {
1727 for (i = 0; i < talloc_array_length(force_rebalance_nodes); i++) {
1728 uint32_t pnn = force_rebalance_nodes[i];
1729 if (pnn >= numnodes) {
1731 (__location__ "unknown node %u\n", pnn));
1736 ("Forcing rebalancing of IPs to node %u\n", pnn));
1737 (*rebalance_candidates)[pnn] = true;
1741 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1742 * the IP/node combination that will cost the least.
1744 static void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1745 struct ctdb_ipflags *ipflags,
1746 struct ctdb_public_ip_list *all_ips,
1747 uint32_t *lcp2_imbalances)
1749 struct ctdb_public_ip_list *tmp_ip;
1750 int dstnode, numnodes;
1753 uint32_t mindsum, dstdsum, dstimbl, minimbl;
1754 struct ctdb_public_ip_list *minip;
1756 bool should_loop = true;
1757 bool have_unassigned = true;
1759 numnodes = talloc_array_length(ipflags);
1761 while (have_unassigned && should_loop) {
1762 should_loop = false;
1764 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1765 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1771 /* loop over each unassigned ip. */
1772 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1773 if (tmp_ip->pnn != -1) {
1777 for (dstnode=0; dstnode<numnodes; dstnode++) {
1778 /* only check nodes that can actually takeover this ip */
1779 if (!can_node_takeover_ip(ctdb, dstnode,
1782 /* no it couldnt so skip to the next node */
1786 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1787 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1788 DEBUG(DEBUG_DEBUG,(" %s -> %d [+%d]\n",
1789 ctdb_addr_to_str(&(tmp_ip->addr)),
1791 dstimbl - lcp2_imbalances[dstnode]));
1794 if ((minnode == -1) || (dstdsum < mindsum)) {
1804 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1806 /* If we found one then assign it to the given node. */
1807 if (minnode != -1) {
1808 minip->pnn = minnode;
1809 lcp2_imbalances[minnode] = minimbl;
1810 DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
1811 ctdb_addr_to_str(&(minip->addr)),
1816 /* There might be a better way but at least this is clear. */
1817 have_unassigned = false;
1818 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1819 if (tmp_ip->pnn == -1) {
1820 have_unassigned = true;
1825 /* We know if we have an unassigned addresses so we might as
1828 if (have_unassigned) {
1829 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1830 if (tmp_ip->pnn == -1) {
1831 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1832 ctdb_addr_to_str(&tmp_ip->addr)));
1838 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1839 * to move IPs from, determines the best IP/destination node
1840 * combination to move from the source node.
1842 static bool lcp2_failback_candidate(struct ctdb_context *ctdb,
1843 struct ctdb_ipflags *ipflags,
1844 struct ctdb_public_ip_list *all_ips,
1846 uint32_t *lcp2_imbalances,
1847 bool *rebalance_candidates)
1849 int dstnode, mindstnode, numnodes;
1850 uint32_t srcimbl, srcdsum, dstimbl, dstdsum;
1851 uint32_t minsrcimbl, mindstimbl;
1852 struct ctdb_public_ip_list *minip;
1853 struct ctdb_public_ip_list *tmp_ip;
1855 /* Find an IP and destination node that best reduces imbalance. */
1862 numnodes = talloc_array_length(ipflags);
1864 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1865 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES FROM %d [%d]\n",
1866 srcnode, lcp2_imbalances[srcnode]));
1868 for (tmp_ip=all_ips; tmp_ip; tmp_ip=tmp_ip->next) {
1869 /* Only consider addresses on srcnode. */
1870 if (tmp_ip->pnn != srcnode) {
1874 /* What is this IP address costing the source node? */
1875 srcdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, srcnode);
1876 srcimbl = lcp2_imbalances[srcnode] - srcdsum;
1878 /* Consider this IP address would cost each potential
1879 * destination node. Destination nodes are limited to
1880 * those that are newly healthy, since we don't want
1881 * to do gratuitous failover of IPs just to make minor
1882 * balance improvements.
1884 for (dstnode=0; dstnode<numnodes; dstnode++) {
1885 if (!rebalance_candidates[dstnode]) {
1889 /* only check nodes that can actually takeover this ip */
1890 if (!can_node_takeover_ip(ctdb, dstnode,
1891 ipflags[dstnode], tmp_ip)) {
1892 /* no it couldnt so skip to the next node */
1896 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1897 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1898 DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
1900 ctdb_addr_to_str(&(tmp_ip->addr)),
1903 if ((dstimbl < lcp2_imbalances[srcnode]) &&
1904 (dstdsum < srcdsum) && \
1905 ((mindstnode == -1) || \
1906 ((srcimbl + dstimbl) < (minsrcimbl + mindstimbl)))) {
1909 minsrcimbl = srcimbl;
1910 mindstnode = dstnode;
1911 mindstimbl = dstimbl;
1915 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1917 if (mindstnode != -1) {
1918 /* We found a move that makes things better... */
1919 DEBUG(DEBUG_INFO,("%d [%d] -> %s -> %d [+%d]\n",
1920 srcnode, minsrcimbl - lcp2_imbalances[srcnode],
1921 ctdb_addr_to_str(&(minip->addr)),
1922 mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));
1925 lcp2_imbalances[srcnode] = minsrcimbl;
1926 lcp2_imbalances[mindstnode] = mindstimbl;
1927 minip->pnn = mindstnode;
1936 struct lcp2_imbalance_pnn {
1941 static int lcp2_cmp_imbalance_pnn(const void * a, const void * b)
1943 const struct lcp2_imbalance_pnn * lipa = (const struct lcp2_imbalance_pnn *) a;
1944 const struct lcp2_imbalance_pnn * lipb = (const struct lcp2_imbalance_pnn *) b;
1946 if (lipa->imbalance > lipb->imbalance) {
1948 } else if (lipa->imbalance == lipb->imbalance) {
1955 /* LCP2 algorithm for rebalancing the cluster. This finds the source
1956 * node with the highest LCP2 imbalance, and then determines the best
1957 * IP/destination node combination to move from the source node.
1959 static void lcp2_failback(struct ctdb_context *ctdb,
1960 struct ctdb_ipflags *ipflags,
1961 struct ctdb_public_ip_list *all_ips,
1962 uint32_t *lcp2_imbalances,
1963 bool *rebalance_candidates)
1966 struct lcp2_imbalance_pnn * lips;
1969 numnodes = talloc_array_length(ipflags);
1972 /* Put the imbalances and nodes into an array, sort them and
1973 * iterate through candidates. Usually the 1st one will be
1974 * used, so this doesn't cost much...
1976 DEBUG(DEBUG_DEBUG,("+++++++++++++++++++++++++++++++++++++++++\n"));
1977 DEBUG(DEBUG_DEBUG,("Selecting most imbalanced node from:\n"));
1978 lips = talloc_array(ctdb, struct lcp2_imbalance_pnn, numnodes);
1979 for (i=0; i<numnodes; i++) {
1980 lips[i].imbalance = lcp2_imbalances[i];
1982 DEBUG(DEBUG_DEBUG,(" %d [%d]\n", i, lcp2_imbalances[i]));
1984 qsort(lips, numnodes, sizeof(struct lcp2_imbalance_pnn),
1985 lcp2_cmp_imbalance_pnn);
1988 for (i=0; i<numnodes; i++) {
1989 /* This means that all nodes had 0 or 1 addresses, so
1990 * can't be imbalanced.
1992 if (lips[i].imbalance == 0) {
1996 if (lcp2_failback_candidate(ctdb,
2001 rebalance_candidates)) {
2013 static void unassign_unsuitable_ips(struct ctdb_context *ctdb,
2014 struct ctdb_ipflags *ipflags,
2015 struct ctdb_public_ip_list *all_ips)
2017 struct ctdb_public_ip_list *tmp_ip;
2019 /* verify that the assigned nodes can serve that public ip
2020 and set it to -1 if not
2022 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2023 if (tmp_ip->pnn == -1) {
2026 if (!can_node_host_ip(ctdb, tmp_ip->pnn,
2027 ipflags[tmp_ip->pnn], tmp_ip) != 0) {
2028 /* this node can not serve this ip. */
2029 DEBUG(DEBUG_DEBUG,("Unassign IP: %s from %d\n",
2030 ctdb_addr_to_str(&(tmp_ip->addr)),
2037 static void ip_alloc_deterministic_ips(struct ctdb_context *ctdb,
2038 struct ctdb_ipflags *ipflags,
2039 struct ctdb_public_ip_list *all_ips)
2041 struct ctdb_public_ip_list *tmp_ip;
2044 numnodes = talloc_array_length(ipflags);
2046 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2047 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2048 * always be allocated the same way for a specific set of
2049 * available/unavailable nodes.
2052 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
2053 tmp_ip->pnn = i % numnodes;
2056 /* IP failback doesn't make sense with deterministic
2057 * IPs, since the modulo step above implicitly fails
2058 * back IPs to their "home" node.
2060 if (1 == ctdb->tunable.no_ip_failback) {
2061 DEBUG(DEBUG_WARNING, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2064 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2066 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2068 /* No failback here! */
2071 static void ip_alloc_nondeterministic_ips(struct ctdb_context *ctdb,
2072 struct ctdb_ipflags *ipflags,
2073 struct ctdb_public_ip_list *all_ips)
2075 /* This should be pushed down into basic_failback. */
2076 struct ctdb_public_ip_list *tmp_ip;
2078 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2082 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2084 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2086 /* If we don't want IPs to fail back then don't rebalance IPs. */
2087 if (1 == ctdb->tunable.no_ip_failback) {
2091 /* Now, try to make sure the ip adresses are evenly distributed
2094 basic_failback(ctdb, ipflags, all_ips, num_ips);
2097 static void ip_alloc_lcp2(struct ctdb_context *ctdb,
2098 struct ctdb_ipflags *ipflags,
2099 struct ctdb_public_ip_list *all_ips,
2100 uint32_t *force_rebalance_nodes)
2102 uint32_t *lcp2_imbalances;
2103 bool *rebalance_candidates;
2104 int numnodes, num_rebalance_candidates, i;
2106 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2108 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2110 lcp2_init(tmp_ctx, ipflags, all_ips,force_rebalance_nodes,
2111 &lcp2_imbalances, &rebalance_candidates);
2113 lcp2_allocate_unassigned(ctdb, ipflags, all_ips, lcp2_imbalances);
2115 /* If we don't want IPs to fail back then don't rebalance IPs. */
2116 if (1 == ctdb->tunable.no_ip_failback) {
2120 /* It is only worth continuing if we have suitable target
2121 * nodes to transfer IPs to. This check is much cheaper than
2124 numnodes = talloc_array_length(ipflags);
2125 num_rebalance_candidates = 0;
2126 for (i=0; i<numnodes; i++) {
2127 if (rebalance_candidates[i]) {
2128 num_rebalance_candidates++;
2131 if (num_rebalance_candidates == 0) {
2135 /* Now, try to make sure the ip adresses are evenly distributed
2138 lcp2_failback(ctdb, ipflags, all_ips,
2139 lcp2_imbalances, rebalance_candidates);
2142 talloc_free(tmp_ctx);
2145 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
2149 for (i=0;i<nodemap->num;i++) {
2150 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
2151 /* Found one completely healthy node */
2159 /* The calculation part of the IP allocation algorithm. */
2160 static void ctdb_takeover_run_core(struct ctdb_context *ctdb,
2161 struct ctdb_ipflags *ipflags,
2162 struct ctdb_public_ip_list **all_ips_p,
2163 uint32_t *force_rebalance_nodes)
2165 /* since nodes only know about those public addresses that
2166 can be served by that particular node, no single node has
2167 a full list of all public addresses that exist in the cluster.
2168 Walk over all node structures and create a merged list of
2169 all public addresses that exist in the cluster.
2171 keep the tree of ips around as ctdb->ip_tree
2173 *all_ips_p = create_merged_ip_list(ctdb);
2175 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2176 ip_alloc_lcp2(ctdb, ipflags, *all_ips_p, force_rebalance_nodes);
2177 } else if (1 == ctdb->tunable.deterministic_public_ips) {
2178 ip_alloc_deterministic_ips(ctdb, ipflags, *all_ips_p);
2180 ip_alloc_nondeterministic_ips(ctdb, ipflags, *all_ips_p);
2183 /* at this point ->pnn is the node which will own each IP
2184 or -1 if there is no node that can cover this ip
2190 struct get_tunable_callback_data {
2191 const char *tunable;
2196 static void get_tunable_callback(struct ctdb_context *ctdb, uint32_t pnn,
2197 int32_t res, TDB_DATA outdata,
2200 struct get_tunable_callback_data *cd =
2201 (struct get_tunable_callback_data *)callback;
2205 /* Already handled in fail callback */
2209 if (outdata.dsize != sizeof(uint32_t)) {
2210 DEBUG(DEBUG_ERR,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2211 cd->tunable, pnn, (int)sizeof(uint32_t),
2212 (int)outdata.dsize));
2217 size = talloc_array_length(cd->out);
2219 DEBUG(DEBUG_ERR,("Got %s reply from node %d but nodemap only has %d entries\n",
2220 cd->tunable, pnn, size));
2225 cd->out[pnn] = *(uint32_t *)outdata.dptr;
2228 static void get_tunable_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2229 int32_t res, TDB_DATA outdata,
2232 struct get_tunable_callback_data *cd =
2233 (struct get_tunable_callback_data *)callback;
2238 ("Timed out getting tunable \"%s\" from node %d\n",
2244 DEBUG(DEBUG_WARNING,
2245 ("Tunable \"%s\" not implemented on node %d\n",
2250 ("Unexpected error getting tunable \"%s\" from node %d\n",
2256 static uint32_t *get_tunable_from_nodes(struct ctdb_context *ctdb,
2257 TALLOC_CTX *tmp_ctx,
2258 struct ctdb_node_map *nodemap,
2259 const char *tunable,
2260 uint32_t default_value)
2263 struct ctdb_control_get_tunable *t;
2266 struct get_tunable_callback_data callback_data;
2269 tvals = talloc_array(tmp_ctx, uint32_t, nodemap->num);
2270 CTDB_NO_MEMORY_NULL(ctdb, tvals);
2271 for (i=0; i<nodemap->num; i++) {
2272 tvals[i] = default_value;
2275 callback_data.out = tvals;
2276 callback_data.tunable = tunable;
2277 callback_data.fatal = false;
2279 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen(tunable) + 1;
2280 data.dptr = talloc_size(tmp_ctx, data.dsize);
2281 t = (struct ctdb_control_get_tunable *)data.dptr;
2282 t->length = strlen(tunable)+1;
2283 memcpy(t->name, tunable, t->length);
2284 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2285 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_TUNABLE,
2286 nodes, 0, TAKEOVER_TIMEOUT(),
2288 get_tunable_callback,
2289 get_tunable_fail_callback,
2290 &callback_data) != 0) {
2291 if (callback_data.fatal) {
2297 talloc_free(data.dptr);
2302 struct get_runstate_callback_data {
2303 enum ctdb_runstate *out;
2307 static void get_runstate_callback(struct ctdb_context *ctdb, uint32_t pnn,
2308 int32_t res, TDB_DATA outdata,
2309 void *callback_data)
2311 struct get_runstate_callback_data *cd =
2312 (struct get_runstate_callback_data *)callback_data;
2316 /* Already handled in fail callback */
2320 if (outdata.dsize != sizeof(uint32_t)) {
2321 DEBUG(DEBUG_ERR,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2322 pnn, (int)sizeof(uint32_t),
2323 (int)outdata.dsize));
2328 size = talloc_array_length(cd->out);
2330 DEBUG(DEBUG_ERR,("Got reply from node %d but nodemap only has %d entries\n",
2335 cd->out[pnn] = (enum ctdb_runstate)*(uint32_t *)outdata.dptr;
2338 static void get_runstate_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2339 int32_t res, TDB_DATA outdata,
2342 struct get_runstate_callback_data *cd =
2343 (struct get_runstate_callback_data *)callback;
2348 ("Timed out getting runstate from node %d\n", pnn));
2352 DEBUG(DEBUG_WARNING,
2353 ("Error getting runstate from node %d - assuming runstates not supported\n",
2358 static enum ctdb_runstate * get_runstate_from_nodes(struct ctdb_context *ctdb,
2359 TALLOC_CTX *tmp_ctx,
2360 struct ctdb_node_map *nodemap,
2361 enum ctdb_runstate default_value)
2364 enum ctdb_runstate *rs;
2365 struct get_runstate_callback_data callback_data;
2368 rs = talloc_array(tmp_ctx, enum ctdb_runstate, nodemap->num);
2369 CTDB_NO_MEMORY_NULL(ctdb, rs);
2370 for (i=0; i<nodemap->num; i++) {
2371 rs[i] = default_value;
2374 callback_data.out = rs;
2375 callback_data.fatal = false;
2377 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2378 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_RUNSTATE,
2379 nodes, 0, TAKEOVER_TIMEOUT(),
2381 get_runstate_callback,
2382 get_runstate_fail_callback,
2383 &callback_data) != 0) {
2384 if (callback_data.fatal) {
2394 /* Set internal flags for IP allocation:
2396 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2397 * Set NOIPHOST ip flag for each INACTIVE node
2398 * if all nodes are disabled:
2399 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2401 * Set NOIPHOST ip flags for disabled nodes
2403 static struct ctdb_ipflags *
2404 set_ipflags_internal(struct ctdb_context *ctdb,
2405 TALLOC_CTX *tmp_ctx,
2406 struct ctdb_node_map *nodemap,
2407 uint32_t *tval_noiptakeover,
2408 uint32_t *tval_noiphostonalldisabled,
2409 enum ctdb_runstate *runstate)
2412 struct ctdb_ipflags *ipflags;
2414 /* Clear IP flags - implicit due to talloc_zero */
2415 ipflags = talloc_zero_array(tmp_ctx, struct ctdb_ipflags, nodemap->num);
2416 CTDB_NO_MEMORY_NULL(ctdb, ipflags);
2418 for (i=0;i<nodemap->num;i++) {
2419 /* Can not take IPs on node with NoIPTakeover set */
2420 if (tval_noiptakeover[i] != 0) {
2421 ipflags[i].noiptakeover = true;
2424 /* Can not host IPs on node not in RUNNING state */
2425 if (runstate[i] != CTDB_RUNSTATE_RUNNING) {
2426 ipflags[i].noiphost = true;
2429 /* Can not host IPs on INACTIVE node */
2430 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2431 ipflags[i].noiphost = true;
2433 /* Remember the runstate */
2434 ipflags[i].runstate = runstate[i];
2437 if (all_nodes_are_disabled(nodemap)) {
2438 /* If all nodes are disabled, can not host IPs on node
2439 * with NoIPHostOnAllDisabled set
2441 for (i=0;i<nodemap->num;i++) {
2442 if (tval_noiphostonalldisabled[i] != 0) {
2443 ipflags[i].noiphost = true;
2447 /* If some nodes are not disabled, then can not host
2448 * IPs on DISABLED node
2450 for (i=0;i<nodemap->num;i++) {
2451 if (nodemap->nodes[i].flags & NODE_FLAGS_DISABLED) {
2452 ipflags[i].noiphost = true;
2460 static struct ctdb_ipflags *set_ipflags(struct ctdb_context *ctdb,
2461 TALLOC_CTX *tmp_ctx,
2462 struct ctdb_node_map *nodemap)
2464 uint32_t *tval_noiptakeover;
2465 uint32_t *tval_noiphostonalldisabled;
2466 struct ctdb_ipflags *ipflags;
2467 enum ctdb_runstate *runstate;
2470 tval_noiptakeover = get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2472 if (tval_noiptakeover == NULL) {
2476 tval_noiphostonalldisabled =
2477 get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2478 "NoIPHostOnAllDisabled", 0);
2479 if (tval_noiphostonalldisabled == NULL) {
2480 /* Caller frees tmp_ctx */
2484 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2485 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2486 * reasonable behaviour on a mixed cluster during upgrade.
2488 runstate = get_runstate_from_nodes(ctdb, tmp_ctx, nodemap,
2489 CTDB_RUNSTATE_RUNNING);
2490 if (runstate == NULL) {
2491 /* Caller frees tmp_ctx */
2495 ipflags = set_ipflags_internal(ctdb, tmp_ctx, nodemap,
2497 tval_noiphostonalldisabled,
2500 talloc_free(tval_noiptakeover);
2501 talloc_free(tval_noiphostonalldisabled);
2502 talloc_free(runstate);
2507 struct iprealloc_callback_data {
2510 client_async_callback fail_callback;
2511 void *fail_callback_data;
2512 struct ctdb_node_map *nodemap;
2515 static void iprealloc_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2516 int32_t res, TDB_DATA outdata,
2520 struct iprealloc_callback_data *cd =
2521 (struct iprealloc_callback_data *)callback;
2523 numnodes = talloc_array_length(cd->retry_nodes);
2524 if (pnn > numnodes) {
2526 ("ipreallocated failure from node %d, "
2527 "but only %d nodes in nodemap\n",
2532 /* Can't run the "ipreallocated" event on a INACTIVE node */
2533 if (cd->nodemap->nodes[pnn].flags & NODE_FLAGS_INACTIVE) {
2534 DEBUG(DEBUG_WARNING,
2535 ("ipreallocated failed on inactive node %d, ignoring\n",
2542 /* If the control timed out then that's a real error,
2543 * so call the real fail callback
2545 if (cd->fail_callback) {
2546 cd->fail_callback(ctdb, pnn, res, outdata,
2547 cd->fail_callback_data);
2549 DEBUG(DEBUG_WARNING,
2550 ("iprealloc timed out but no callback registered\n"));
2554 /* If not a timeout then either the ipreallocated
2555 * eventscript (or some setup) failed. This might
2556 * have failed because the IPREALLOCATED control isn't
2557 * implemented - right now there is no way of knowing
2558 * because the error codes are all folded down to -1.
2559 * Consider retrying using EVENTSCRIPT control...
2561 DEBUG(DEBUG_WARNING,
2562 ("ipreallocated failure from node %d, flagging retry\n",
2564 cd->retry_nodes[pnn] = true;
2569 struct takeover_callback_data {
2571 client_async_callback fail_callback;
2572 void *fail_callback_data;
2573 struct ctdb_node_map *nodemap;
2576 static void takeover_run_fail_callback(struct ctdb_context *ctdb,
2577 uint32_t node_pnn, int32_t res,
2578 TDB_DATA outdata, void *callback_data)
2580 struct takeover_callback_data *cd =
2581 talloc_get_type_abort(callback_data,
2582 struct takeover_callback_data);
2585 for (i = 0; i < cd->nodemap->num; i++) {
2586 if (node_pnn == cd->nodemap->nodes[i].pnn) {
2591 if (i == cd->nodemap->num) {
2592 DEBUG(DEBUG_ERR, (__location__ " invalid PNN %u\n", node_pnn));
2596 if (!cd->node_failed[i]) {
2597 cd->node_failed[i] = true;
2598 cd->fail_callback(ctdb, node_pnn, res, outdata,
2599 cd->fail_callback_data);
2604 make any IP alias changes for public addresses that are necessary
2606 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
2607 uint32_t *force_rebalance_nodes,
2608 client_async_callback fail_callback, void *callback_data)
2611 struct ctdb_public_ip ip;
2612 struct ctdb_public_ipv4 ipv4;
2614 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2616 struct timeval timeout;
2617 struct client_async_data *async_data;
2618 struct ctdb_client_control_state *state;
2619 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2620 struct ctdb_ipflags *ipflags;
2621 struct takeover_callback_data *takeover_data;
2622 struct iprealloc_callback_data iprealloc_data;
2627 * ip failover is completely disabled, just send out the
2628 * ipreallocated event.
2630 if (ctdb->tunable.disable_ip_failover != 0) {
2634 ipflags = set_ipflags(ctdb, tmp_ctx, nodemap);
2635 if (ipflags == NULL) {
2636 DEBUG(DEBUG_ERR,("Failed to set IP flags - aborting takeover run\n"));
2637 talloc_free(tmp_ctx);
2641 /* Short-circuit IP allocation if no nodes are in the RUNNING
2642 * runstate yet, since no nodes will be able to host IPs */
2643 can_host_ips = false;
2644 for (i=0; i<nodemap->num; i++) {
2645 if (ipflags[i].runstate == CTDB_RUNSTATE_RUNNING) {
2646 can_host_ips = true;
2649 if (!can_host_ips) {
2650 DEBUG(DEBUG_WARNING,("No nodes available to host public IPs yet\n"));
2654 /* Do the IP reassignment calculations */
2655 ctdb_takeover_run_core(ctdb, ipflags, &all_ips, force_rebalance_nodes);
2657 /* Now tell all nodes to release any public IPs should not
2658 * host. This will be a NOOP on nodes that don't currently
2659 * hold the given IP.
2661 takeover_data = talloc_zero(tmp_ctx, struct takeover_callback_data);
2662 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data);
2664 takeover_data->node_failed = talloc_zero_array(tmp_ctx,
2665 bool, nodemap->num);
2666 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data->node_failed);
2667 takeover_data->fail_callback = fail_callback;
2668 takeover_data->fail_callback_data = callback_data;
2669 takeover_data->nodemap = nodemap;
2671 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2672 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2674 async_data->fail_callback = takeover_run_fail_callback;
2675 async_data->callback_data = takeover_data;
2677 ZERO_STRUCT(ip); /* Avoid valgrind warnings for union */
2679 /* Send a RELEASE_IP to all nodes that should not be hosting
2680 * each IP. For each IP, all but one of these will be
2681 * redundant. However, the redundant ones are used to tell
2682 * nodes which node should be hosting the IP so that commands
2683 * like "ctdb ip" can display a particular nodes idea of who
2684 * is hosting what. */
2685 for (i=0;i<nodemap->num;i++) {
2686 /* don't talk to unconnected nodes, but do talk to banned nodes */
2687 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2691 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2692 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
2693 /* This node should be serving this
2694 vnn so dont tell it to release the ip
2698 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2699 ipv4.pnn = tmp_ip->pnn;
2700 ipv4.sin = tmp_ip->addr.ip;
2702 timeout = TAKEOVER_TIMEOUT();
2703 data.dsize = sizeof(ipv4);
2704 data.dptr = (uint8_t *)&ipv4;
2705 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2706 0, CTDB_CONTROL_RELEASE_IPv4, 0,
2710 ip.pnn = tmp_ip->pnn;
2711 ip.addr = tmp_ip->addr;
2713 timeout = TAKEOVER_TIMEOUT();
2714 data.dsize = sizeof(ip);
2715 data.dptr = (uint8_t *)&ip;
2716 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2717 0, CTDB_CONTROL_RELEASE_IP, 0,
2722 if (state == NULL) {
2723 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
2724 talloc_free(tmp_ctx);
2728 ctdb_client_async_add(async_data, state);
2731 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2732 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2733 talloc_free(tmp_ctx);
2736 talloc_free(async_data);
2739 /* For each IP, send a TAKOVER_IP to the node that should be
2740 * hosting it. Many of these will often be redundant (since
2741 * the allocation won't have changed) but they can be useful
2742 * to recover from inconsistencies. */
2743 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2744 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2746 async_data->fail_callback = fail_callback;
2747 async_data->callback_data = callback_data;
2749 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2750 if (tmp_ip->pnn == -1) {
2751 /* this IP won't be taken over */
2755 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2756 ipv4.pnn = tmp_ip->pnn;
2757 ipv4.sin = tmp_ip->addr.ip;
2759 timeout = TAKEOVER_TIMEOUT();
2760 data.dsize = sizeof(ipv4);
2761 data.dptr = (uint8_t *)&ipv4;
2762 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2763 0, CTDB_CONTROL_TAKEOVER_IPv4, 0,
2767 ip.pnn = tmp_ip->pnn;
2768 ip.addr = tmp_ip->addr;
2770 timeout = TAKEOVER_TIMEOUT();
2771 data.dsize = sizeof(ip);
2772 data.dptr = (uint8_t *)&ip;
2773 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2774 0, CTDB_CONTROL_TAKEOVER_IP, 0,
2778 if (state == NULL) {
2779 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
2780 talloc_free(tmp_ctx);
2784 ctdb_client_async_add(async_data, state);
2786 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2787 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2788 talloc_free(tmp_ctx);
2794 * Tell all nodes to run eventscripts to process the
2795 * "ipreallocated" event. This can do a lot of things,
2796 * including restarting services to reconfigure them if public
2797 * IPs have moved. Once upon a time this event only used to
2800 retry_data = talloc_zero_array(tmp_ctx, bool, nodemap->num);
2801 CTDB_NO_MEMORY_FATAL(ctdb, retry_data);
2802 iprealloc_data.retry_nodes = retry_data;
2803 iprealloc_data.retry_count = 0;
2804 iprealloc_data.fail_callback = fail_callback;
2805 iprealloc_data.fail_callback_data = callback_data;
2806 iprealloc_data.nodemap = nodemap;
2808 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2809 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_IPREALLOCATED,
2810 nodes, 0, TAKEOVER_TIMEOUT(),
2812 NULL, iprealloc_fail_callback,
2815 /* If the control failed then we should retry to any
2816 * nodes flagged by iprealloc_fail_callback using the
2817 * EVENTSCRIPT control. This is a best-effort at
2818 * backward compatiblity when running a mixed cluster
2819 * where some nodes have not yet been upgraded to
2820 * support the IPREALLOCATED control.
2822 DEBUG(DEBUG_WARNING,
2823 ("Retry ipreallocated to some nodes using eventscript control\n"));
2825 nodes = talloc_array(tmp_ctx, uint32_t,
2826 iprealloc_data.retry_count);
2827 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2830 for (i=0; i<nodemap->num; i++) {
2831 if (iprealloc_data.retry_nodes[i]) {
2837 data.dptr = discard_const("ipreallocated");
2838 data.dsize = strlen((char *)data.dptr) + 1;
2839 ret = ctdb_client_async_control(ctdb,
2840 CTDB_CONTROL_RUN_EVENTSCRIPTS,
2841 nodes, 0, TAKEOVER_TIMEOUT(),
2843 NULL, fail_callback,
2846 DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
2850 talloc_free(tmp_ctx);
2856 destroy a ctdb_client_ip structure
2858 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
2860 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
2861 ctdb_addr_to_str(&ip->addr),
2862 ntohs(ip->addr.ip.sin_port),
2865 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
2870 called by a client to inform us of a TCP connection that it is managing
2871 that should tickled with an ACK when IP takeover is done
2873 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
2876 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
2877 struct ctdb_control_tcp_addr *tcp_sock = NULL;
2878 struct ctdb_tcp_list *tcp;
2879 struct ctdb_tcp_connection t;
2882 struct ctdb_client_ip *ip;
2883 struct ctdb_vnn *vnn;
2884 ctdb_sock_addr addr;
2886 /* If we don't have public IPs, tickles are useless */
2887 if (ctdb->vnn == NULL) {
2891 tcp_sock = (struct ctdb_control_tcp_addr *)indata.dptr;
2893 addr = tcp_sock->src;
2894 ctdb_canonicalize_ip(&addr, &tcp_sock->src);
2895 addr = tcp_sock->dest;
2896 ctdb_canonicalize_ip(&addr, &tcp_sock->dest);
2899 memcpy(&addr, &tcp_sock->dest, sizeof(addr));
2900 vnn = find_public_ip_vnn(ctdb, &addr);
2902 switch (addr.sa.sa_family) {
2904 if (ntohl(addr.ip.sin_addr.s_addr) != INADDR_LOOPBACK) {
2905 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public address.\n",
2906 ctdb_addr_to_str(&addr)));
2910 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public ipv6 address.\n",
2911 ctdb_addr_to_str(&addr)));
2914 DEBUG(DEBUG_ERR,(__location__ " Unknown family type %d\n", addr.sa.sa_family));
2920 if (vnn->pnn != ctdb->pnn) {
2921 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2922 ctdb_addr_to_str(&addr),
2923 client_id, client->pid));
2924 /* failing this call will tell smbd to die */
2928 ip = talloc(client, struct ctdb_client_ip);
2929 CTDB_NO_MEMORY(ctdb, ip);
2933 ip->client_id = client_id;
2934 talloc_set_destructor(ip, ctdb_client_ip_destructor);
2935 DLIST_ADD(ctdb->client_ip_list, ip);
2937 tcp = talloc(client, struct ctdb_tcp_list);
2938 CTDB_NO_MEMORY(ctdb, tcp);
2940 tcp->connection.src_addr = tcp_sock->src;
2941 tcp->connection.dst_addr = tcp_sock->dest;
2943 DLIST_ADD(client->tcp_list, tcp);
2945 t.src_addr = tcp_sock->src;
2946 t.dst_addr = tcp_sock->dest;
2948 data.dptr = (uint8_t *)&t;
2949 data.dsize = sizeof(t);
2951 switch (addr.sa.sa_family) {
2953 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2954 (unsigned)ntohs(tcp_sock->dest.ip.sin_port),
2955 ctdb_addr_to_str(&tcp_sock->src),
2956 (unsigned)ntohs(tcp_sock->src.ip.sin_port), client_id, client->pid));
2959 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2960 (unsigned)ntohs(tcp_sock->dest.ip6.sin6_port),
2961 ctdb_addr_to_str(&tcp_sock->src),
2962 (unsigned)ntohs(tcp_sock->src.ip6.sin6_port), client_id, client->pid));
2965 DEBUG(DEBUG_ERR,(__location__ " Unknown family %d\n", addr.sa.sa_family));
2969 /* tell all nodes about this tcp connection */
2970 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
2971 CTDB_CONTROL_TCP_ADD,
2972 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
2974 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
2982 find a tcp address on a list
2984 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
2985 struct ctdb_tcp_connection *tcp)
2989 if (array == NULL) {
2993 for (i=0;i<array->num;i++) {
2994 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
2995 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
2996 return &array->connections[i];
3005 called by a daemon to inform us of a TCP connection that one of its
3006 clients managing that should tickled with an ACK when IP takeover is
3009 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
3011 struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
3012 struct ctdb_tcp_array *tcparray;
3013 struct ctdb_tcp_connection tcp;
3014 struct ctdb_vnn *vnn;
3016 /* If we don't have public IPs, tickles are useless */
3017 if (ctdb->vnn == NULL) {
3021 vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
3023 DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
3024 ctdb_addr_to_str(&p->dst_addr)));
3030 tcparray = vnn->tcp_array;
3032 /* If this is the first tickle */
3033 if (tcparray == NULL) {
3034 tcparray = talloc(vnn, struct ctdb_tcp_array);
3035 CTDB_NO_MEMORY(ctdb, tcparray);
3036 vnn->tcp_array = tcparray;
3039 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
3040 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3042 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3043 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3046 if (tcp_update_needed) {
3047 vnn->tcp_update_needed = true;
3053 /* Do we already have this tickle ?*/
3054 tcp.src_addr = p->src_addr;
3055 tcp.dst_addr = p->dst_addr;
3056 if (ctdb_tcp_find(tcparray, &tcp) != NULL) {
3057 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
3058 ctdb_addr_to_str(&tcp.dst_addr),
3059 ntohs(tcp.dst_addr.ip.sin_port),
3064 /* A new tickle, we must add it to the array */
3065 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
3066 struct ctdb_tcp_connection,
3068 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3070 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3071 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3074 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
3075 ctdb_addr_to_str(&tcp.dst_addr),
3076 ntohs(tcp.dst_addr.ip.sin_port),
3079 if (tcp_update_needed) {
3080 vnn->tcp_update_needed = true;
3088 called by a daemon to inform us of a TCP connection that one of its
3089 clients managing that should tickled with an ACK when IP takeover is
3092 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
3094 struct ctdb_tcp_connection *tcpp;
3095 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
3098 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
3099 ctdb_addr_to_str(&conn->dst_addr)));
3103 /* if the array is empty we cant remove it
3104 and we dont need to do anything
3106 if (vnn->tcp_array == NULL) {
3107 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3108 ctdb_addr_to_str(&conn->dst_addr),
3109 ntohs(conn->dst_addr.ip.sin_port)));
3114 /* See if we know this connection
3115 if we dont know this connection then we dont need to do anything
3117 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
3119 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
3120 ctdb_addr_to_str(&conn->dst_addr),
3121 ntohs(conn->dst_addr.ip.sin_port)));
3126 /* We need to remove this entry from the array.
3127 Instead of allocating a new array and copying data to it
3128 we cheat and just copy the last entry in the existing array
3129 to the entry that is to be removed and just shring the
3132 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
3133 vnn->tcp_array->num--;
3135 /* If we deleted the last entry we also need to remove the entire array
3137 if (vnn->tcp_array->num == 0) {
3138 talloc_free(vnn->tcp_array);
3139 vnn->tcp_array = NULL;
3142 vnn->tcp_update_needed = true;
3144 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
3145 ctdb_addr_to_str(&conn->src_addr),
3146 ntohs(conn->src_addr.ip.sin_port)));
3151 called by a daemon to inform us of a TCP connection that one of its
3152 clients used are no longer needed in the tickle database
3154 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
3156 struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
3158 /* If we don't have public IPs, tickles are useless */
3159 if (ctdb->vnn == NULL) {
3163 ctdb_remove_tcp_connection(ctdb, conn);
3170 Called when another daemon starts - causes all tickles for all
3171 public addresses we are serving to be sent to the new node on the
3172 next check. This actually causes the next scheduled call to
3173 tdb_update_tcp_tickles() to update all nodes. This is simple and
3174 doesn't require careful error handling.
3176 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t pnn)
3178 struct ctdb_vnn *vnn;
3180 DEBUG(DEBUG_INFO, ("Received startup control from node %lu\n",
3181 (unsigned long) pnn));
3183 for (vnn = ctdb->vnn; vnn != NULL; vnn = vnn->next) {
3184 vnn->tcp_update_needed = true;
3192 called when a client structure goes away - hook to remove
3193 elements from the tcp_list in all daemons
3195 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
3197 while (client->tcp_list) {
3198 struct ctdb_tcp_list *tcp = client->tcp_list;
3199 DLIST_REMOVE(client->tcp_list, tcp);
3200 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
3206 release all IPs on shutdown
3208 void ctdb_release_all_ips(struct ctdb_context *ctdb)
3210 struct ctdb_vnn *vnn;
3213 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3214 if (!ctdb_sys_have_ip(&vnn->public_address)) {
3215 ctdb_vnn_unassign_iface(ctdb, vnn);
3222 DEBUG(DEBUG_INFO,("Release of IP %s/%u on interface %s node:-1\n",
3223 ctdb_addr_to_str(&vnn->public_address),
3224 vnn->public_netmask_bits,
3225 ctdb_vnn_iface_string(vnn)));
3227 ctdb_event_script_args(ctdb, CTDB_EVENT_RELEASE_IP, "%s %s %u",
3228 ctdb_vnn_iface_string(vnn),
3229 ctdb_addr_to_str(&vnn->public_address),
3230 vnn->public_netmask_bits);
3231 release_kill_clients(ctdb, &vnn->public_address);
3232 ctdb_vnn_unassign_iface(ctdb, vnn);
3236 DEBUG(DEBUG_NOTICE,(__location__ " Released %d public IPs\n", count));
3241 get list of public IPs
3243 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
3244 struct ctdb_req_control *c, TDB_DATA *outdata)
3247 struct ctdb_all_public_ips *ips;
3248 struct ctdb_vnn *vnn;
3249 bool only_available = false;
3251 if (c->flags & CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE) {
3252 only_available = true;
3255 /* count how many public ip structures we have */
3257 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3261 len = offsetof(struct ctdb_all_public_ips, ips) +
3262 num*sizeof(struct ctdb_public_ip);
3263 ips = talloc_zero_size(outdata, len);
3264 CTDB_NO_MEMORY(ctdb, ips);
3267 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3268 if (only_available && !ctdb_vnn_available(ctdb, vnn)) {
3271 ips->ips[i].pnn = vnn->pnn;
3272 ips->ips[i].addr = vnn->public_address;
3276 len = offsetof(struct ctdb_all_public_ips, ips) +
3277 i*sizeof(struct ctdb_public_ip);
3279 outdata->dsize = len;
3280 outdata->dptr = (uint8_t *)ips;
3287 get list of public IPs, old ipv4 style. only returns ipv4 addresses
3289 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb,
3290 struct ctdb_req_control *c, TDB_DATA *outdata)
3293 struct ctdb_all_public_ipsv4 *ips;
3294 struct ctdb_vnn *vnn;
3296 /* count how many public ip structures we have */
3298 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3299 if (vnn->public_address.sa.sa_family != AF_INET) {
3305 len = offsetof(struct ctdb_all_public_ipsv4, ips) +
3306 num*sizeof(struct ctdb_public_ipv4);
3307 ips = talloc_zero_size(outdata, len);
3308 CTDB_NO_MEMORY(ctdb, ips);
3310 outdata->dsize = len;
3311 outdata->dptr = (uint8_t *)ips;
3315 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3316 if (vnn->public_address.sa.sa_family != AF_INET) {
3319 ips->ips[i].pnn = vnn->pnn;
3320 ips->ips[i].sin = vnn->public_address.ip;
3327 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
3328 struct ctdb_req_control *c,
3333 ctdb_sock_addr *addr;
3334 struct ctdb_control_public_ip_info *info;
3335 struct ctdb_vnn *vnn;
3337 addr = (ctdb_sock_addr *)indata.dptr;
3339 vnn = find_public_ip_vnn(ctdb, addr);
3341 /* if it is not a public ip it could be our 'single ip' */
3342 if (ctdb->single_ip_vnn) {
3343 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
3344 vnn = ctdb->single_ip_vnn;
3349 DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
3350 "'%s'not a public address\n",
3351 ctdb_addr_to_str(addr)));
3355 /* count how many public ip structures we have */
3357 for (;vnn->ifaces[num];) {
3361 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3362 num*sizeof(struct ctdb_control_iface_info);
3363 info = talloc_zero_size(outdata, len);
3364 CTDB_NO_MEMORY(ctdb, info);
3366 info->ip.addr = vnn->public_address;
3367 info->ip.pnn = vnn->pnn;
3368 info->active_idx = 0xFFFFFFFF;
3370 for (i=0; vnn->ifaces[i]; i++) {
3371 struct ctdb_iface *cur;
3373 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
3375 DEBUG(DEBUG_CRIT, (__location__ " internal error iface[%s] unknown\n",
3379 if (vnn->iface == cur) {
3380 info->active_idx = i;
3382 strncpy(info->ifaces[i].name, cur->name, sizeof(info->ifaces[i].name)-1);
3383 info->ifaces[i].link_state = cur->link_up;
3384 info->ifaces[i].references = cur->references;
3387 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3388 i*sizeof(struct ctdb_control_iface_info);
3390 outdata->dsize = len;
3391 outdata->dptr = (uint8_t *)info;
3396 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
3397 struct ctdb_req_control *c,
3401 struct ctdb_control_get_ifaces *ifaces;
3402 struct ctdb_iface *cur;
3404 /* count how many public ip structures we have */
3406 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3410 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3411 num*sizeof(struct ctdb_control_iface_info);
3412 ifaces = talloc_zero_size(outdata, len);
3413 CTDB_NO_MEMORY(ctdb, ifaces);
3416 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3417 strcpy(ifaces->ifaces[i].name, cur->name);
3418 ifaces->ifaces[i].link_state = cur->link_up;
3419 ifaces->ifaces[i].references = cur->references;
3423 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3424 i*sizeof(struct ctdb_control_iface_info);
3426 outdata->dsize = len;
3427 outdata->dptr = (uint8_t *)ifaces;
3432 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
3433 struct ctdb_req_control *c,
3436 struct ctdb_control_iface_info *info;
3437 struct ctdb_iface *iface;
3438 bool link_up = false;
3440 info = (struct ctdb_control_iface_info *)indata.dptr;
3442 if (info->name[CTDB_IFACE_SIZE] != '\0') {
3443 int len = strnlen(info->name, CTDB_IFACE_SIZE);
3444 DEBUG(DEBUG_ERR, (__location__ " name[%*.*s] not terminated\n",
3445 len, len, info->name));
3449 switch (info->link_state) {
3457 DEBUG(DEBUG_ERR, (__location__ " link_state[%u] invalid\n",
3458 (unsigned int)info->link_state));
3462 if (info->references != 0) {
3463 DEBUG(DEBUG_ERR, (__location__ " references[%u] should be 0\n",
3464 (unsigned int)info->references));
3468 iface = ctdb_find_iface(ctdb, info->name);
3469 if (iface == NULL) {
3473 if (link_up == iface->link_up) {
3477 DEBUG(iface->link_up?DEBUG_ERR:DEBUG_NOTICE,
3478 ("iface[%s] has changed it's link status %s => %s\n",
3480 iface->link_up?"up":"down",
3481 link_up?"up":"down"));
3483 iface->link_up = link_up;
3489 structure containing the listening socket and the list of tcp connections
3490 that the ctdb daemon is to kill
3492 struct ctdb_kill_tcp {
3493 struct ctdb_vnn *vnn;
3494 struct ctdb_context *ctdb;
3496 struct fd_event *fde;
3497 trbt_tree_t *connections;
3502 a tcp connection that is to be killed
3504 struct ctdb_killtcp_con {
3505 ctdb_sock_addr src_addr;
3506 ctdb_sock_addr dst_addr;
3508 struct ctdb_kill_tcp *killtcp;
3511 /* this function is used to create a key to represent this socketpair
3512 in the killtcp tree.
3513 this key is used to insert and lookup matching socketpairs that are
3514 to be tickled and RST
3516 #define KILLTCP_KEYLEN 10
3517 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
3519 static uint32_t key[KILLTCP_KEYLEN];
3521 bzero(key, sizeof(key));
3523 if (src->sa.sa_family != dst->sa.sa_family) {
3524 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
3528 switch (src->sa.sa_family) {
3530 key[0] = dst->ip.sin_addr.s_addr;
3531 key[1] = src->ip.sin_addr.s_addr;
3532 key[2] = dst->ip.sin_port;
3533 key[3] = src->ip.sin_port;
3536 uint32_t *dst6_addr32 =
3537 (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
3538 uint32_t *src6_addr32 =
3539 (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
3540 key[0] = dst6_addr32[3];
3541 key[1] = src6_addr32[3];
3542 key[2] = dst6_addr32[2];
3543 key[3] = src6_addr32[2];
3544 key[4] = dst6_addr32[1];
3545 key[5] = src6_addr32[1];
3546 key[6] = dst6_addr32[0];
3547 key[7] = src6_addr32[0];
3548 key[8] = dst->ip6.sin6_port;
3549 key[9] = src->ip6.sin6_port;
3553 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
3561 called when we get a read event on the raw socket
3563 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
3564 uint16_t flags, void *private_data)
3566 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3567 struct ctdb_killtcp_con *con;
3568 ctdb_sock_addr src, dst;
3569 uint32_t ack_seq, seq;
3571 if (!(flags & EVENT_FD_READ)) {
3575 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
3576 killtcp->private_data,
3578 &ack_seq, &seq) != 0) {
3579 /* probably a non-tcp ACK packet */
3583 /* check if we have this guy in our list of connections
3586 con = trbt_lookuparray32(killtcp->connections,
3587 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
3589 /* no this was some other packet we can just ignore */
3593 /* This one has been tickled !
3594 now reset him and remove him from the list.
3596 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3597 ntohs(con->dst_addr.ip.sin_port),
3598 ctdb_addr_to_str(&con->src_addr),
3599 ntohs(con->src_addr.ip.sin_port)));
3601 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
3606 /* when traversing the list of all tcp connections to send tickle acks to
3607 (so that we can capture the ack coming back and kill the connection
3609 this callback is called for each connection we are currently trying to kill
3611 static int tickle_connection_traverse(void *param, void *data)
3613 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
3615 /* have tried too many times, just give up */
3616 if (con->count >= 5) {
3617 /* can't delete in traverse: reparent to delete_cons */
3618 talloc_steal(param, con);
3622 /* othervise, try tickling it again */
3625 (ctdb_sock_addr *)&con->dst_addr,
3626 (ctdb_sock_addr *)&con->src_addr,
3633 called every second until all sentenced connections have been reset
3635 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
3636 struct timeval t, void *private_data)
3638 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3639 void *delete_cons = talloc_new(NULL);
3641 /* loop over all connections sending tickle ACKs */
3642 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
3644 /* now we've finished traverse, it's safe to do deletion. */
3645 talloc_free(delete_cons);
3647 /* If there are no more connections to kill we can remove the
3648 entire killtcp structure
3650 if ( (killtcp->connections == NULL) ||
3651 (killtcp->connections->root == NULL) ) {
3652 talloc_free(killtcp);
3656 /* try tickling them again in a seconds time
3658 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3659 ctdb_tickle_sentenced_connections, killtcp);
3663 destroy the killtcp structure
3665 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
3667 struct ctdb_vnn *tmpvnn;
3669 /* verify that this vnn is still active */
3670 for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
3671 if (tmpvnn == killtcp->vnn) {
3676 if (tmpvnn == NULL) {
3680 if (killtcp->vnn->killtcp != killtcp) {
3684 killtcp->vnn->killtcp = NULL;
3690 /* nothing fancy here, just unconditionally replace any existing
3691 connection structure with the new one.
3693 dont even free the old one if it did exist, that one is talloc_stolen
3694 by the same node in the tree anyway and will be deleted when the new data
3697 static void *add_killtcp_callback(void *parm, void *data)
3703 add a tcp socket to the list of connections we want to RST
3705 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
3709 ctdb_sock_addr src, dst;
3710 struct ctdb_kill_tcp *killtcp;
3711 struct ctdb_killtcp_con *con;
3712 struct ctdb_vnn *vnn;
3714 ctdb_canonicalize_ip(s, &src);
3715 ctdb_canonicalize_ip(d, &dst);
3717 vnn = find_public_ip_vnn(ctdb, &dst);
3719 vnn = find_public_ip_vnn(ctdb, &src);
3722 /* if it is not a public ip it could be our 'single ip' */
3723 if (ctdb->single_ip_vnn) {
3724 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
3725 vnn = ctdb->single_ip_vnn;
3730 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
3734 killtcp = vnn->killtcp;
3736 /* If this is the first connection to kill we must allocate
3739 if (killtcp == NULL) {
3740 killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
3741 CTDB_NO_MEMORY(ctdb, killtcp);
3744 killtcp->ctdb = ctdb;
3745 killtcp->capture_fd = -1;
3746 killtcp->connections = trbt_create(killtcp, 0);
3748 vnn->killtcp = killtcp;
3749 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
3754 /* create a structure that describes this connection we want to
3755 RST and store it in killtcp->connections
3757 con = talloc(killtcp, struct ctdb_killtcp_con);
3758 CTDB_NO_MEMORY(ctdb, con);
3759 con->src_addr = src;
3760 con->dst_addr = dst;
3762 con->killtcp = killtcp;
3765 trbt_insertarray32_callback(killtcp->connections,
3766 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
3767 add_killtcp_callback, con);
3770 If we dont have a socket to listen on yet we must create it
3772 if (killtcp->capture_fd == -1) {
3773 const char *iface = ctdb_vnn_iface_string(vnn);
3774 killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
3775 if (killtcp->capture_fd == -1) {
3776 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
3777 "socket on iface '%s' for killtcp (%s)\n",
3778 iface, strerror(errno)));
3784 if (killtcp->fde == NULL) {
3785 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
3787 capture_tcp_handler, killtcp);
3788 tevent_fd_set_auto_close(killtcp->fde);
3790 /* We also need to set up some events to tickle all these connections
3791 until they are all reset
3793 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3794 ctdb_tickle_sentenced_connections, killtcp);
3797 /* tickle him once now */
3806 talloc_free(vnn->killtcp);
3807 vnn->killtcp = NULL;
3812 kill a TCP connection.
3814 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
3816 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
3818 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
3822 called by a daemon to inform us of the entire list of TCP tickles for
3823 a particular public address.
3824 this control should only be sent by the node that is currently serving
3825 that public address.
3827 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
3829 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
3830 struct ctdb_tcp_array *tcparray;
3831 struct ctdb_vnn *vnn;
3833 /* We must at least have tickles.num or else we cant verify the size
3834 of the received data blob
3836 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3837 tickles.connections)) {
3838 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3842 /* verify that the size of data matches what we expect */
3843 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3844 tickles.connections)
3845 + sizeof(struct ctdb_tcp_connection)
3846 * list->tickles.num) {
3847 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3851 DEBUG(DEBUG_INFO, ("Received tickle update for public address %s\n",
3852 ctdb_addr_to_str(&list->addr)));
3854 vnn = find_public_ip_vnn(ctdb, &list->addr);
3856 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
3857 ctdb_addr_to_str(&list->addr)));
3862 /* remove any old ticklelist we might have */
3863 talloc_free(vnn->tcp_array);
3864 vnn->tcp_array = NULL;
3866 tcparray = talloc(vnn, struct ctdb_tcp_array);
3867 CTDB_NO_MEMORY(ctdb, tcparray);
3869 tcparray->num = list->tickles.num;
3871 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
3872 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3874 memcpy(tcparray->connections, &list->tickles.connections[0],
3875 sizeof(struct ctdb_tcp_connection)*tcparray->num);
3877 /* We now have a new fresh tickle list array for this vnn */
3878 vnn->tcp_array = tcparray;
3884 called to return the full list of tickles for the puclic address associated
3885 with the provided vnn
3887 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
3889 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
3890 struct ctdb_control_tcp_tickle_list *list;
3891 struct ctdb_tcp_array *tcparray;
3893 struct ctdb_vnn *vnn;
3895 vnn = find_public_ip_vnn(ctdb, addr);
3897 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
3898 ctdb_addr_to_str(addr)));
3903 tcparray = vnn->tcp_array;
3905 num = tcparray->num;
3910 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3911 tickles.connections)
3912 + sizeof(struct ctdb_tcp_connection) * num;
3914 outdata->dptr = talloc_size(outdata, outdata->dsize);
3915 CTDB_NO_MEMORY(ctdb, outdata->dptr);
3916 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
3919 list->tickles.num = num;
3921 memcpy(&list->tickles.connections[0], tcparray->connections,
3922 sizeof(struct ctdb_tcp_connection) * num);
3930 set the list of all tcp tickles for a public address
3932 static int ctdb_send_set_tcp_tickles_for_ip(struct ctdb_context *ctdb,
3933 ctdb_sock_addr *addr,
3934 struct ctdb_tcp_array *tcparray)
3938 struct ctdb_control_tcp_tickle_list *list;
3941 num = tcparray->num;
3946 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3947 tickles.connections) +
3948 sizeof(struct ctdb_tcp_connection) * num;
3949 data.dptr = talloc_size(ctdb, data.dsize);
3950 CTDB_NO_MEMORY(ctdb, data.dptr);
3952 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
3954 list->tickles.num = num;
3956 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
3959 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL, 0,
3960 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
3961 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
3963 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
3967 talloc_free(data.dptr);
3974 perform tickle updates if required
3976 static void ctdb_update_tcp_tickles(struct event_context *ev,
3977 struct timed_event *te,
3978 struct timeval t, void *private_data)
3980 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
3982 struct ctdb_vnn *vnn;
3984 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3985 /* we only send out updates for public addresses that
3988 if (ctdb->pnn != vnn->pnn) {
3991 /* We only send out the updates if we need to */
3992 if (!vnn->tcp_update_needed) {
3995 ret = ctdb_send_set_tcp_tickles_for_ip(ctdb,
3996 &vnn->public_address,
3999 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
4000 ctdb_addr_to_str(&vnn->public_address)));
4003 ("Sent tickle update for public address %s\n",
4004 ctdb_addr_to_str(&vnn->public_address)));
4005 vnn->tcp_update_needed = false;
4009 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4010 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4011 ctdb_update_tcp_tickles, ctdb);
4016 start periodic update of tcp tickles
4018 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
4020 ctdb->tickle_update_context = talloc_new(ctdb);
4022 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4023 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4024 ctdb_update_tcp_tickles, ctdb);
4030 struct control_gratious_arp {
4031 struct ctdb_context *ctdb;
4032 ctdb_sock_addr addr;
4038 send a control_gratuitous arp
4040 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
4041 struct timeval t, void *private_data)
4044 struct control_gratious_arp *arp = talloc_get_type(private_data,
4045 struct control_gratious_arp);
4047 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
4049 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp on iface '%s' failed (%s)\n",
4050 arp->iface, strerror(errno)));
4055 if (arp->count == CTDB_ARP_REPEAT) {
4060 event_add_timed(arp->ctdb->ev, arp,
4061 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
4062 send_gratious_arp, arp);
4069 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
4071 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
4072 struct control_gratious_arp *arp;
4074 /* verify the size of indata */
4075 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
4076 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
4077 (unsigned)indata.dsize,
4078 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
4082 ( offsetof(struct ctdb_control_gratious_arp, iface)
4083 + gratious_arp->len ) ){
4085 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4086 "but should be %u bytes\n",
4087 (unsigned)indata.dsize,
4088 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
4093 arp = talloc(ctdb, struct control_gratious_arp);
4094 CTDB_NO_MEMORY(ctdb, arp);
4097 arp->addr = gratious_arp->addr;
4098 arp->iface = talloc_strdup(arp, gratious_arp->iface);
4099 CTDB_NO_MEMORY(ctdb, arp->iface);
4102 event_add_timed(arp->ctdb->ev, arp,
4103 timeval_zero(), send_gratious_arp, arp);
4108 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
4110 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4113 /* verify the size of indata */
4114 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4115 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4119 ( offsetof(struct ctdb_control_ip_iface, iface)
4122 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4123 "but should be %u bytes\n",
4124 (unsigned)indata.dsize,
4125 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4129 DEBUG(DEBUG_NOTICE,("Add IP %s\n", ctdb_addr_to_str(&pub->addr)));
4131 ret = ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0], true);
4134 DEBUG(DEBUG_ERR,(__location__ " Failed to add public address\n"));
4141 struct delete_ip_callback_state {
4142 struct ctdb_req_control *c;
4146 called when releaseip event finishes for del_public_address
4148 static void delete_ip_callback(struct ctdb_context *ctdb,
4149 int32_t status, TDB_DATA data,
4150 const char *errormsg,
4153 struct delete_ip_callback_state *state =
4154 talloc_get_type(private_data, struct delete_ip_callback_state);
4156 /* If release failed then fail. */
4157 ctdb_request_control_reply(ctdb, state->c, NULL, status, errormsg);
4158 talloc_free(private_data);
4161 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb,
4162 struct ctdb_req_control *c,
4163 TDB_DATA indata, bool *async_reply)
4165 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4166 struct ctdb_vnn *vnn;
4168 /* verify the size of indata */
4169 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4170 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4174 ( offsetof(struct ctdb_control_ip_iface, iface)
4177 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4178 "but should be %u bytes\n",
4179 (unsigned)indata.dsize,
4180 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4184 DEBUG(DEBUG_NOTICE,("Delete IP %s\n", ctdb_addr_to_str(&pub->addr)));
4186 /* walk over all public addresses until we find a match */
4187 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4188 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
4189 if (vnn->pnn == ctdb->pnn) {
4190 struct delete_ip_callback_state *state;
4191 struct ctdb_public_ip *ip;
4195 vnn->delete_pending = true;
4197 state = talloc(ctdb,
4198 struct delete_ip_callback_state);
4199 CTDB_NO_MEMORY(ctdb, state);
4202 ip = talloc(state, struct ctdb_public_ip);
4205 (__location__ " Out of memory\n"));
4210 ip->addr = pub->addr;
4212 data.dsize = sizeof(struct ctdb_public_ip);
4213 data.dptr = (unsigned char *)ip;
4215 ret = ctdb_daemon_send_control(ctdb,
4218 CTDB_CONTROL_RELEASE_IP,
4225 (__location__ "Unable to send "
4226 "CTDB_CONTROL_RELEASE_IP\n"));
4231 state->c = talloc_steal(state, c);
4232 *async_reply = true;
4234 /* This IP is not hosted on the
4235 * current node so just delete it
4237 do_delete_ip(ctdb, vnn);
4244 DEBUG(DEBUG_ERR,("Delete IP of unknown public IP address %s\n",
4245 ctdb_addr_to_str(&pub->addr)));
4250 struct ipreallocated_callback_state {
4251 struct ctdb_req_control *c;
4254 static void ctdb_ipreallocated_callback(struct ctdb_context *ctdb,
4255 int status, void *p)
4257 struct ipreallocated_callback_state *state =
4258 talloc_get_type(p, struct ipreallocated_callback_state);
4262 (" \"ipreallocated\" event script failed (status %d)\n",
4264 if (status == -ETIME) {
4265 ctdb_ban_self(ctdb);
4269 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
4273 /* A control to run the ipreallocated event */
4274 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb,
4275 struct ctdb_req_control *c,
4279 struct ipreallocated_callback_state *state;
4281 state = talloc(ctdb, struct ipreallocated_callback_state);
4282 CTDB_NO_MEMORY(ctdb, state);
4284 DEBUG(DEBUG_INFO,(__location__ " Running \"ipreallocated\" event\n"));
4286 ret = ctdb_event_script_callback(ctdb, state,
4287 ctdb_ipreallocated_callback, state,
4288 CTDB_EVENT_IPREALLOCATED,
4292 DEBUG(DEBUG_ERR,("Failed to run \"ipreallocated\" event \n"));
4297 /* tell the control that we will be reply asynchronously */
4298 state->c = talloc_steal(state, c);
4299 *async_reply = true;
4305 /* This function is called from the recovery daemon to verify that a remote
4306 node has the expected ip allocation.
4307 This is verified against ctdb->ip_tree
4309 int verify_remote_ip_allocation(struct ctdb_context *ctdb,
4310 struct ctdb_all_public_ips *ips,
4313 struct ctdb_public_ip_list *tmp_ip;
4316 if (ctdb->ip_tree == NULL) {
4317 /* dont know the expected allocation yet, assume remote node
4326 for (i=0; i<ips->num; i++) {
4327 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ips->ips[i].addr));
4328 if (tmp_ip == NULL) {
4329 DEBUG(DEBUG_ERR,("Node %u has new or unknown public IP %s\n", pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
4333 if (tmp_ip->pnn == -1 || ips->ips[i].pnn == -1) {
4337 if (tmp_ip->pnn != ips->ips[i].pnn) {
4339 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4341 ctdb_addr_to_str(&ips->ips[i].addr),
4342 ips->ips[i].pnn, tmp_ip->pnn));
4350 int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
4352 struct ctdb_public_ip_list *tmp_ip;
4354 if (ctdb->ip_tree == NULL) {
4355 DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4359 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
4360 if (tmp_ip == NULL) {
4361 DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
4365 DEBUG(DEBUG_NOTICE,("Updated ip assignment tree for ip : %s from node %u to node %u\n", ctdb_addr_to_str(&ip->addr), tmp_ip->pnn, ip->pnn));
4366 tmp_ip->pnn = ip->pnn;
4372 struct ctdb_reloadips_handle {
4373 struct ctdb_context *ctdb;
4374 struct ctdb_req_control *c;
4378 struct fd_event *fde;
4381 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle *h)
4383 if (h == h->ctdb->reload_ips) {
4384 h->ctdb->reload_ips = NULL;
4387 ctdb_request_control_reply(h->ctdb, h->c, NULL, h->status, NULL);
4390 ctdb_kill(h->ctdb, h->child, SIGKILL);
4394 static void ctdb_reloadips_timeout_event(struct event_context *ev,
4395 struct timed_event *te,
4396 struct timeval t, void *private_data)
4398 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4403 static void ctdb_reloadips_child_handler(struct event_context *ev, struct fd_event *fde,
4404 uint16_t flags, void *private_data)
4406 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4411 ret = sys_read(h->fd[0], &res, 1);
4412 if (ret < 1 || res != 0) {
4413 DEBUG(DEBUG_ERR, (__location__ " Reloadips child process returned error\n"));
4421 static int ctdb_reloadips_child(struct ctdb_context *ctdb)
4423 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4424 struct ctdb_all_public_ips *ips;
4425 struct ctdb_vnn *vnn;
4426 struct client_async_data *async_data;
4427 struct timeval timeout;
4429 struct ctdb_client_control_state *state;
4433 CTDB_NO_MEMORY(ctdb, mem_ctx);
4435 /* Read IPs from local node */
4436 ret = ctdb_ctrl_get_public_ips(ctdb, TAKEOVER_TIMEOUT(),
4437 CTDB_CURRENT_NODE, mem_ctx, &ips);
4440 ("Unable to fetch public IPs from local node\n"));
4441 talloc_free(mem_ctx);
4445 /* Read IPs file - this is safe since this is a child process */
4447 if (ctdb_set_public_addresses(ctdb, false) != 0) {
4448 DEBUG(DEBUG_ERR,("Failed to re-read public addresses file\n"));
4449 talloc_free(mem_ctx);
4453 async_data = talloc_zero(mem_ctx, struct client_async_data);
4454 CTDB_NO_MEMORY(ctdb, async_data);
4456 /* Compare IPs between node and file for IPs to be deleted */
4457 for (i = 0; i < ips->num; i++) {
4459 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4460 if (ctdb_same_ip(&vnn->public_address,
4461 &ips->ips[i].addr)) {
4462 /* IP is still in file */
4468 /* Delete IP ips->ips[i] */
4469 struct ctdb_control_ip_iface *pub;
4472 ("IP %s no longer configured, deleting it\n",
4473 ctdb_addr_to_str(&ips->ips[i].addr)));
4475 pub = talloc_zero(mem_ctx,
4476 struct ctdb_control_ip_iface);
4477 CTDB_NO_MEMORY(ctdb, pub);
4479 pub->addr = ips->ips[i].addr;
4483 timeout = TAKEOVER_TIMEOUT();
4485 data.dsize = offsetof(struct ctdb_control_ip_iface,
4487 data.dptr = (uint8_t *)pub;
4489 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4490 CTDB_CONTROL_DEL_PUBLIC_IP,
4491 0, data, async_data,
4493 if (state == NULL) {
4496 " failed sending CTDB_CONTROL_DEL_PUBLIC_IP\n"));
4500 ctdb_client_async_add(async_data, state);
4504 /* Compare IPs between node and file for IPs to be added */
4506 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4507 for (i = 0; i < ips->num; i++) {
4508 if (ctdb_same_ip(&vnn->public_address,
4509 &ips->ips[i].addr)) {
4510 /* IP already on node */
4514 if (i == ips->num) {
4515 /* Add IP ips->ips[i] */
4516 struct ctdb_control_ip_iface *pub;
4517 const char *ifaces = NULL;
4522 ("New IP %s configured, adding it\n",
4523 ctdb_addr_to_str(&vnn->public_address)));
4525 uint32_t pnn = ctdb_get_pnn(ctdb);
4527 data.dsize = sizeof(pnn);
4528 data.dptr = (uint8_t *)&pnn;
4530 ret = ctdb_client_send_message(
4532 CTDB_BROADCAST_CONNECTED,
4533 CTDB_SRVID_REBALANCE_NODE,
4536 DEBUG(DEBUG_WARNING,
4537 ("Failed to send message to force node reallocation - IPs may be unbalanced\n"));
4543 ifaces = vnn->ifaces[0];
4545 while (vnn->ifaces[iface] != NULL) {
4546 ifaces = talloc_asprintf(vnn, "%s,%s", ifaces,
4547 vnn->ifaces[iface]);
4551 len = strlen(ifaces) + 1;
4552 pub = talloc_zero_size(mem_ctx,
4553 offsetof(struct ctdb_control_ip_iface, iface) + len);
4554 CTDB_NO_MEMORY(ctdb, pub);
4556 pub->addr = vnn->public_address;
4557 pub->mask = vnn->public_netmask_bits;
4559 memcpy(&pub->iface[0], ifaces, pub->len);
4561 timeout = TAKEOVER_TIMEOUT();
4563 data.dsize = offsetof(struct ctdb_control_ip_iface,
4565 data.dptr = (uint8_t *)pub;
4567 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4568 CTDB_CONTROL_ADD_PUBLIC_IP,
4569 0, data, async_data,
4571 if (state == NULL) {
4574 " failed sending CTDB_CONTROL_ADD_PUBLIC_IP\n"));
4578 ctdb_client_async_add(async_data, state);
4582 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
4583 DEBUG(DEBUG_ERR,(__location__ " Add/delete IPs failed\n"));
4587 talloc_free(mem_ctx);
4591 talloc_free(mem_ctx);
4595 /* This control is sent to force the node to re-read the public addresses file
4596 and drop any addresses we should nnot longer host, and add new addresses
4597 that we are now able to host
4599 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
4601 struct ctdb_reloadips_handle *h;
4602 pid_t parent = getpid();
4604 if (ctdb->reload_ips != NULL) {
4605 talloc_free(ctdb->reload_ips);
4606 ctdb->reload_ips = NULL;
4609 h = talloc(ctdb, struct ctdb_reloadips_handle);
4610 CTDB_NO_MEMORY(ctdb, h);
4615 if (pipe(h->fd) == -1) {
4616 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
4621 h->child = ctdb_fork(ctdb);
4622 if (h->child == (pid_t)-1) {
4623 DEBUG(DEBUG_ERR, ("Failed to fork a child for reloadips\n"));
4631 if (h->child == 0) {
4632 signed char res = 0;
4635 debug_extra = talloc_asprintf(NULL, "reloadips:");
4637 ctdb_set_process_name("ctdb_reloadips");
4638 if (switch_from_server_to_client(ctdb, "reloadips-child") != 0) {
4639 DEBUG(DEBUG_CRIT,("ERROR: Failed to switch reloadips child into client mode\n"));
4642 res = ctdb_reloadips_child(ctdb);
4644 DEBUG(DEBUG_ERR,("Failed to reload ips on local node\n"));
4648 sys_write(h->fd[1], &res, 1);
4649 /* make sure we die when our parent dies */
4650 while (ctdb_kill(ctdb, parent, 0) == 0 || errno != ESRCH) {
4656 h->c = talloc_steal(h, c);
4659 set_close_on_exec(h->fd[0]);
4661 talloc_set_destructor(h, ctdb_reloadips_destructor);
4664 h->fde = event_add_fd(ctdb->ev, h, h->fd[0],
4665 EVENT_FD_READ, ctdb_reloadips_child_handler,
4667 tevent_fd_set_auto_close(h->fde);
4669 event_add_timed(ctdb->ev, h,
4670 timeval_current_ofs(120, 0),
4671 ctdb_reloadips_timeout_event, h);
4673 /* we reply later */
4674 *async_reply = true;