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 kill any clients that are registered with a IP that is being released
819 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
821 struct ctdb_client_ip *ip;
823 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
824 ctdb_addr_to_str(addr)));
826 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
827 ctdb_sock_addr tmp_addr;
830 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
832 ctdb_addr_to_str(&ip->addr)));
834 if (ctdb_same_ip(&tmp_addr, addr)) {
835 struct ctdb_client *client = ctdb_reqid_find(ctdb,
838 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
840 ctdb_addr_to_str(&ip->addr),
843 if (client->pid != 0) {
844 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
845 (unsigned)client->pid,
846 ctdb_addr_to_str(addr),
848 kill(client->pid, SIGKILL);
854 static void do_delete_ip(struct ctdb_context *ctdb, struct ctdb_vnn *vnn)
856 DLIST_REMOVE(ctdb->vnn, vnn);
857 ctdb_vnn_unassign_iface(ctdb, vnn);
858 ctdb_remove_orphaned_ifaces(ctdb, vnn);
863 called when releaseip event finishes
865 static void release_ip_callback(struct ctdb_context *ctdb, int status,
868 struct takeover_callback_state *state =
869 talloc_get_type(private_data, struct takeover_callback_state);
872 if (status == -ETIME) {
876 if (ctdb->do_checkpublicip) {
877 if (ctdb_sys_have_ip(state->addr)) {
879 ("IP %s still hosted during release IP callback, failing\n",
880 ctdb_addr_to_str(state->addr)));
881 ctdb_request_control_reply(ctdb, state->c,
888 /* send a message to all clients of this node telling them
889 that the cluster has been reconfigured and they should
890 release any sockets on this IP */
891 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
892 CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
893 data.dsize = strlen((char *)data.dptr)+1;
895 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
897 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
899 /* kill clients that have registered with this IP */
900 release_kill_clients(ctdb, state->addr);
902 ctdb_vnn_unassign_iface(ctdb, state->vnn);
904 /* Process the IP if it has been marked for deletion */
905 if (state->vnn->delete_pending) {
906 do_delete_ip(ctdb, state->vnn);
910 /* the control succeeded */
911 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
915 static int ctdb_releaseip_destructor(struct takeover_callback_state *state)
917 if (state->vnn != NULL) {
918 state->vnn->update_in_flight = false;
924 release an ip address
926 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
927 struct ctdb_req_control *c,
932 struct takeover_callback_state *state;
933 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
934 struct ctdb_vnn *vnn;
937 /* update our vnn list */
938 vnn = find_public_ip_vnn(ctdb, &pip->addr);
940 DEBUG(DEBUG_INFO,("releaseip called for an ip '%s' that is not a public address\n",
941 ctdb_addr_to_str(&pip->addr)));
946 /* stop any previous arps */
947 talloc_free(vnn->takeover_ctx);
948 vnn->takeover_ctx = NULL;
950 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
951 * lazy multicast to drop an IP from any node that isn't the
952 * intended new node. The following causes makes ctdbd ignore
953 * a release for any address it doesn't host.
955 if (ctdb->do_checkpublicip) {
956 if (!ctdb_sys_have_ip(&pip->addr)) {
957 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
958 ctdb_addr_to_str(&pip->addr),
959 vnn->public_netmask_bits,
960 ctdb_vnn_iface_string(vnn)));
961 ctdb_vnn_unassign_iface(ctdb, vnn);
965 if (vnn->iface == NULL) {
966 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u (ip not held)\n",
967 ctdb_addr_to_str(&pip->addr),
968 vnn->public_netmask_bits));
973 /* There is a potential race between take_ip and us because we
974 * update the VNN via a callback that run when the
975 * eventscripts have been run. Avoid the race by allowing one
976 * update to be in flight at a time.
978 if (vnn->update_in_flight) {
979 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u rejected "
980 "update for this IP already in flight\n",
981 ctdb_addr_to_str(&vnn->public_address),
982 vnn->public_netmask_bits));
986 iface = strdup(ctdb_vnn_iface_string(vnn));
988 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s node:%d\n",
989 ctdb_addr_to_str(&pip->addr),
990 vnn->public_netmask_bits,
994 state = talloc(ctdb, struct takeover_callback_state);
996 ctdb_set_error(ctdb, "Out of memory at %s:%d",
1002 state->c = talloc_steal(state, c);
1003 state->addr = talloc(state, ctdb_sock_addr);
1004 if (state->addr == NULL) {
1005 ctdb_set_error(ctdb, "Out of memory at %s:%d",
1006 __FILE__, __LINE__);
1011 *state->addr = pip->addr;
1014 vnn->update_in_flight = true;
1015 talloc_set_destructor(state, ctdb_releaseip_destructor);
1017 ret = ctdb_event_script_callback(ctdb,
1018 state, release_ip_callback, state,
1019 CTDB_EVENT_RELEASE_IP,
1022 ctdb_addr_to_str(&pip->addr),
1023 vnn->public_netmask_bits);
1026 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
1027 ctdb_addr_to_str(&pip->addr),
1028 ctdb_vnn_iface_string(vnn)));
1033 /* tell the control that we will be reply asynchronously */
1034 *async_reply = true;
1038 static int ctdb_add_public_address(struct ctdb_context *ctdb,
1039 ctdb_sock_addr *addr,
1040 unsigned mask, const char *ifaces,
1043 struct ctdb_vnn *vnn;
1050 tmp = strdup(ifaces);
1051 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1052 if (!ctdb_sys_check_iface_exists(iface)) {
1053 DEBUG(DEBUG_CRIT,("Interface %s does not exist. Can not add public-address : %s\n", iface, ctdb_addr_to_str(addr)));
1060 /* Verify that we dont have an entry for this ip yet */
1061 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1062 if (ctdb_same_sockaddr(addr, &vnn->public_address)) {
1063 DEBUG(DEBUG_CRIT,("Same ip '%s' specified multiple times in the public address list \n",
1064 ctdb_addr_to_str(addr)));
1069 /* create a new vnn structure for this ip address */
1070 vnn = talloc_zero(ctdb, struct ctdb_vnn);
1071 CTDB_NO_MEMORY_FATAL(ctdb, vnn);
1072 vnn->ifaces = talloc_array(vnn, const char *, num + 2);
1073 tmp = talloc_strdup(vnn, ifaces);
1074 CTDB_NO_MEMORY_FATAL(ctdb, tmp);
1075 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1076 vnn->ifaces = talloc_realloc(vnn, vnn->ifaces, const char *, num + 2);
1077 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces);
1078 vnn->ifaces[num] = talloc_strdup(vnn, iface);
1079 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces[num]);
1083 vnn->ifaces[num] = NULL;
1084 vnn->public_address = *addr;
1085 vnn->public_netmask_bits = mask;
1087 if (check_address) {
1088 if (ctdb_sys_have_ip(addr)) {
1089 DEBUG(DEBUG_ERR,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr), ctdb->pnn));
1090 vnn->pnn = ctdb->pnn;
1094 for (i=0; vnn->ifaces[i]; i++) {
1095 ret = ctdb_add_local_iface(ctdb, vnn->ifaces[i]);
1097 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1098 "for public_address[%s]\n",
1099 vnn->ifaces[i], ctdb_addr_to_str(addr)));
1105 DLIST_ADD(ctdb->vnn, vnn);
1111 setup the public address lists from a file
1113 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
1119 lines = file_lines_load(ctdb->public_addresses_file, &nlines, 0, ctdb);
1120 if (lines == NULL) {
1121 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", ctdb->public_addresses_file);
1124 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1128 for (i=0;i<nlines;i++) {
1130 ctdb_sock_addr addr;
1131 const char *addrstr;
1136 while ((*line == ' ') || (*line == '\t')) {
1142 if (strcmp(line, "") == 0) {
1145 tok = strtok(line, " \t");
1147 tok = strtok(NULL, " \t");
1149 if (NULL == ctdb->default_public_interface) {
1150 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
1155 ifaces = ctdb->default_public_interface;
1160 if (!addrstr || !parse_ip_mask(addrstr, ifaces, &addr, &mask)) {
1161 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
1165 if (ctdb_add_public_address(ctdb, &addr, mask, ifaces, check_addresses)) {
1166 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
1177 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1181 struct ctdb_vnn *svnn;
1182 struct ctdb_iface *cur = NULL;
1186 svnn = talloc_zero(ctdb, struct ctdb_vnn);
1187 CTDB_NO_MEMORY(ctdb, svnn);
1189 svnn->ifaces = talloc_array(svnn, const char *, 2);
1190 CTDB_NO_MEMORY(ctdb, svnn->ifaces);
1191 svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
1192 CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
1193 svnn->ifaces[1] = NULL;
1195 ok = parse_ip(ip, iface, 0, &svnn->public_address);
1201 ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
1203 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1204 "for single_ip[%s]\n",
1206 ctdb_addr_to_str(&svnn->public_address)));
1211 /* assume the single public ip interface is initially "good" */
1212 cur = ctdb_find_iface(ctdb, iface);
1214 DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
1217 cur->link_up = true;
1219 ret = ctdb_vnn_assign_iface(ctdb, svnn);
1225 ctdb->single_ip_vnn = svnn;
1229 struct ctdb_public_ip_list {
1230 struct ctdb_public_ip_list *next;
1232 ctdb_sock_addr addr;
1235 /* Given a physical node, return the number of
1236 public addresses that is currently assigned to this node.
1238 static int node_ip_coverage(struct ctdb_context *ctdb,
1240 struct ctdb_public_ip_list *ips)
1244 for (;ips;ips=ips->next) {
1245 if (ips->pnn == pnn) {
1253 /* Can the given node host the given IP: is the public IP known to the
1254 * node and is NOIPHOST unset?
1256 static bool can_node_host_ip(struct ctdb_context *ctdb, int32_t pnn,
1257 struct ctdb_ipflags ipflags,
1258 struct ctdb_public_ip_list *ip)
1260 struct ctdb_all_public_ips *public_ips;
1263 if (ipflags.noiphost) {
1267 public_ips = ctdb->nodes[pnn]->available_public_ips;
1269 if (public_ips == NULL) {
1273 for (i=0; i<public_ips->num; i++) {
1274 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
1275 /* yes, this node can serve this public ip */
1283 static bool can_node_takeover_ip(struct ctdb_context *ctdb, int32_t pnn,
1284 struct ctdb_ipflags ipflags,
1285 struct ctdb_public_ip_list *ip)
1287 if (ipflags.noiptakeover) {
1291 return can_node_host_ip(ctdb, pnn, ipflags, ip);
1294 /* search the node lists list for a node to takeover this ip.
1295 pick the node that currently are serving the least number of ips
1296 so that the ips get spread out evenly.
1298 static int find_takeover_node(struct ctdb_context *ctdb,
1299 struct ctdb_ipflags *ipflags,
1300 struct ctdb_public_ip_list *ip,
1301 struct ctdb_public_ip_list *all_ips)
1303 int pnn, min=0, num;
1306 numnodes = talloc_array_length(ipflags);
1308 for (i=0; i<numnodes; i++) {
1309 /* verify that this node can serve this ip */
1310 if (!can_node_takeover_ip(ctdb, i, ipflags[i], ip)) {
1311 /* no it couldnt so skip to the next node */
1315 num = node_ip_coverage(ctdb, i, all_ips);
1316 /* was this the first node we checked ? */
1328 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
1329 ctdb_addr_to_str(&ip->addr)));
1339 static uint32_t *ip_key(ctdb_sock_addr *ip)
1341 static uint32_t key[IP_KEYLEN];
1343 bzero(key, sizeof(key));
1345 switch (ip->sa.sa_family) {
1347 key[3] = htonl(ip->ip.sin_addr.s_addr);
1350 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1351 key[0] = htonl(s6_a32[0]);
1352 key[1] = htonl(s6_a32[1]);
1353 key[2] = htonl(s6_a32[2]);
1354 key[3] = htonl(s6_a32[3]);
1358 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1365 static void *add_ip_callback(void *parm, void *data)
1367 struct ctdb_public_ip_list *this_ip = parm;
1368 struct ctdb_public_ip_list *prev_ip = data;
1370 if (prev_ip == NULL) {
1373 if (this_ip->pnn == -1) {
1374 this_ip->pnn = prev_ip->pnn;
1380 static int getips_count_callback(void *param, void *data)
1382 struct ctdb_public_ip_list **ip_list = (struct ctdb_public_ip_list **)param;
1383 struct ctdb_public_ip_list *new_ip = (struct ctdb_public_ip_list *)data;
1385 new_ip->next = *ip_list;
1390 static struct ctdb_public_ip_list *
1391 create_merged_ip_list(struct ctdb_context *ctdb)
1394 struct ctdb_public_ip_list *ip_list;
1395 struct ctdb_all_public_ips *public_ips;
1397 if (ctdb->ip_tree != NULL) {
1398 talloc_free(ctdb->ip_tree);
1399 ctdb->ip_tree = NULL;
1401 ctdb->ip_tree = trbt_create(ctdb, 0);
1403 for (i=0;i<ctdb->num_nodes;i++) {
1404 public_ips = ctdb->nodes[i]->known_public_ips;
1406 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
1410 /* there were no public ips for this node */
1411 if (public_ips == NULL) {
1415 for (j=0;j<public_ips->num;j++) {
1416 struct ctdb_public_ip_list *tmp_ip;
1418 tmp_ip = talloc_zero(ctdb->ip_tree, struct ctdb_public_ip_list);
1419 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
1420 /* Do not use information about IP addresses hosted
1421 * on other nodes, it may not be accurate */
1422 if (public_ips->ips[j].pnn == ctdb->nodes[i]->pnn) {
1423 tmp_ip->pnn = public_ips->ips[j].pnn;
1427 tmp_ip->addr = public_ips->ips[j].addr;
1428 tmp_ip->next = NULL;
1430 trbt_insertarray32_callback(ctdb->ip_tree,
1431 IP_KEYLEN, ip_key(&public_ips->ips[j].addr),
1438 trbt_traversearray32(ctdb->ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
1444 * This is the length of the longtest common prefix between the IPs.
1445 * It is calculated by XOR-ing the 2 IPs together and counting the
1446 * number of leading zeroes. The implementation means that all
1447 * addresses end up being 128 bits long.
1449 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1450 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1451 * lots of nodes and IP addresses?
1453 static uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
1455 uint32_t ip1_k[IP_KEYLEN];
1460 uint32_t distance = 0;
1462 memcpy(ip1_k, ip_key(ip1), sizeof(ip1_k));
1464 for (i=0; i<IP_KEYLEN; i++) {
1465 x = ip1_k[i] ^ t[i];
1469 /* Count number of leading zeroes.
1470 * FIXME? This could be optimised...
1472 while ((x & (1 << 31)) == 0) {
1482 /* Calculate the IP distance for the given IP relative to IPs on the
1483 given node. The ips argument is generally the all_ips variable
1484 used in the main part of the algorithm.
1486 static uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1487 struct ctdb_public_ip_list *ips,
1490 struct ctdb_public_ip_list *t;
1495 for (t=ips; t != NULL; t=t->next) {
1496 if (t->pnn != pnn) {
1500 /* Optimisation: We never calculate the distance
1501 * between an address and itself. This allows us to
1502 * calculate the effect of removing an address from a
1503 * node by simply calculating the distance between
1504 * that address and all of the exitsing addresses.
1505 * Moreover, we assume that we're only ever dealing
1506 * with addresses from all_ips so we can identify an
1507 * address via a pointer rather than doing a more
1508 * expensive address comparison. */
1509 if (&(t->addr) == ip) {
1513 d = ip_distance(ip, &(t->addr));
1514 sum += d * d; /* Cheaper than pulling in math.h :-) */
1520 /* Return the LCP2 imbalance metric for addresses currently assigned
1523 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn)
1525 struct ctdb_public_ip_list *t;
1527 uint32_t imbalance = 0;
1529 for (t=all_ips; t!=NULL; t=t->next) {
1530 if (t->pnn != pnn) {
1533 /* Pass the rest of the IPs rather than the whole
1536 imbalance += ip_distance_2_sum(&(t->addr), t->next, pnn);
1542 /* Allocate any unassigned IPs just by looping through the IPs and
1543 * finding the best node for each.
1545 static void basic_allocate_unassigned(struct ctdb_context *ctdb,
1546 struct ctdb_ipflags *ipflags,
1547 struct ctdb_public_ip_list *all_ips)
1549 struct ctdb_public_ip_list *tmp_ip;
1551 /* loop over all ip's and find a physical node to cover for
1554 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1555 if (tmp_ip->pnn == -1) {
1556 if (find_takeover_node(ctdb, ipflags, tmp_ip, all_ips)) {
1557 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1558 ctdb_addr_to_str(&tmp_ip->addr)));
1564 /* Basic non-deterministic rebalancing algorithm.
1566 static void basic_failback(struct ctdb_context *ctdb,
1567 struct ctdb_ipflags *ipflags,
1568 struct ctdb_public_ip_list *all_ips,
1572 int maxnode, maxnum, minnode, minnum, num, retries;
1573 struct ctdb_public_ip_list *tmp_ip;
1575 numnodes = talloc_array_length(ipflags);
1582 /* for each ip address, loop over all nodes that can serve
1583 this ip and make sure that the difference between the node
1584 serving the most and the node serving the least ip's are
1587 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1588 if (tmp_ip->pnn == -1) {
1592 /* Get the highest and lowest number of ips's served by any
1593 valid node which can serve this ip.
1597 for (i=0; i<numnodes; i++) {
1598 /* only check nodes that can actually serve this ip */
1599 if (!can_node_takeover_ip(ctdb, i, ipflags[i], tmp_ip)) {
1600 /* no it couldnt so skip to the next node */
1604 num = node_ip_coverage(ctdb, i, all_ips);
1605 if (maxnode == -1) {
1614 if (minnode == -1) {
1624 if (maxnode == -1) {
1625 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
1626 ctdb_addr_to_str(&tmp_ip->addr)));
1631 /* if the spread between the smallest and largest coverage by
1632 a node is >=2 we steal one of the ips from the node with
1633 most coverage to even things out a bit.
1634 try to do this a limited number of times since we dont
1635 want to spend too much time balancing the ip coverage.
1637 if ( (maxnum > minnum+1)
1638 && (retries < (num_ips + 5)) ){
1639 struct ctdb_public_ip_list *tmp;
1641 /* Reassign one of maxnode's VNNs */
1642 for (tmp=all_ips;tmp;tmp=tmp->next) {
1643 if (tmp->pnn == maxnode) {
1644 (void)find_takeover_node(ctdb, ipflags, tmp, all_ips);
1653 static void lcp2_init(struct ctdb_context *tmp_ctx,
1654 struct ctdb_ipflags *ipflags,
1655 struct ctdb_public_ip_list *all_ips,
1656 uint32_t *force_rebalance_nodes,
1657 uint32_t **lcp2_imbalances,
1658 bool **rebalance_candidates)
1661 struct ctdb_public_ip_list *tmp_ip;
1663 numnodes = talloc_array_length(ipflags);
1665 *rebalance_candidates = talloc_array(tmp_ctx, bool, numnodes);
1666 CTDB_NO_MEMORY_FATAL(tmp_ctx, *rebalance_candidates);
1667 *lcp2_imbalances = talloc_array(tmp_ctx, uint32_t, numnodes);
1668 CTDB_NO_MEMORY_FATAL(tmp_ctx, *lcp2_imbalances);
1670 for (i=0; i<numnodes; i++) {
1671 (*lcp2_imbalances)[i] = lcp2_imbalance(all_ips, i);
1672 /* First step: assume all nodes are candidates */
1673 (*rebalance_candidates)[i] = true;
1676 /* 2nd step: if a node has IPs assigned then it must have been
1677 * healthy before, so we remove it from consideration. This
1678 * is overkill but is all we have because we don't maintain
1679 * state between takeover runs. An alternative would be to
1680 * keep state and invalidate it every time the recovery master
1683 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1684 if (tmp_ip->pnn != -1) {
1685 (*rebalance_candidates)[tmp_ip->pnn] = false;
1689 /* 3rd step: if a node is forced to re-balance then
1690 we allow failback onto the node */
1691 if (force_rebalance_nodes == NULL) {
1694 for (i = 0; i < talloc_array_length(force_rebalance_nodes); i++) {
1695 uint32_t pnn = force_rebalance_nodes[i];
1696 if (pnn >= numnodes) {
1698 (__location__ "unknown node %u\n", pnn));
1703 ("Forcing rebalancing of IPs to node %u\n", pnn));
1704 (*rebalance_candidates)[pnn] = true;
1708 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1709 * the IP/node combination that will cost the least.
1711 static void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1712 struct ctdb_ipflags *ipflags,
1713 struct ctdb_public_ip_list *all_ips,
1714 uint32_t *lcp2_imbalances)
1716 struct ctdb_public_ip_list *tmp_ip;
1717 int dstnode, numnodes;
1720 uint32_t mindsum, dstdsum, dstimbl, minimbl;
1721 struct ctdb_public_ip_list *minip;
1723 bool should_loop = true;
1724 bool have_unassigned = true;
1726 numnodes = talloc_array_length(ipflags);
1728 while (have_unassigned && should_loop) {
1729 should_loop = false;
1731 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1732 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1738 /* loop over each unassigned ip. */
1739 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1740 if (tmp_ip->pnn != -1) {
1744 for (dstnode=0; dstnode<numnodes; dstnode++) {
1745 /* only check nodes that can actually takeover this ip */
1746 if (!can_node_takeover_ip(ctdb, dstnode,
1749 /* no it couldnt so skip to the next node */
1753 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1754 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1755 DEBUG(DEBUG_DEBUG,(" %s -> %d [+%d]\n",
1756 ctdb_addr_to_str(&(tmp_ip->addr)),
1758 dstimbl - lcp2_imbalances[dstnode]));
1761 if ((minnode == -1) || (dstdsum < mindsum)) {
1771 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1773 /* If we found one then assign it to the given node. */
1774 if (minnode != -1) {
1775 minip->pnn = minnode;
1776 lcp2_imbalances[minnode] = minimbl;
1777 DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
1778 ctdb_addr_to_str(&(minip->addr)),
1783 /* There might be a better way but at least this is clear. */
1784 have_unassigned = false;
1785 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1786 if (tmp_ip->pnn == -1) {
1787 have_unassigned = true;
1792 /* We know if we have an unassigned addresses so we might as
1795 if (have_unassigned) {
1796 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1797 if (tmp_ip->pnn == -1) {
1798 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1799 ctdb_addr_to_str(&tmp_ip->addr)));
1805 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1806 * to move IPs from, determines the best IP/destination node
1807 * combination to move from the source node.
1809 static bool lcp2_failback_candidate(struct ctdb_context *ctdb,
1810 struct ctdb_ipflags *ipflags,
1811 struct ctdb_public_ip_list *all_ips,
1813 uint32_t *lcp2_imbalances,
1814 bool *rebalance_candidates)
1816 int dstnode, mindstnode, numnodes;
1817 uint32_t srcimbl, srcdsum, dstimbl, dstdsum;
1818 uint32_t minsrcimbl, mindstimbl;
1819 struct ctdb_public_ip_list *minip;
1820 struct ctdb_public_ip_list *tmp_ip;
1822 /* Find an IP and destination node that best reduces imbalance. */
1829 numnodes = talloc_array_length(ipflags);
1831 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1832 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES FROM %d [%d]\n",
1833 srcnode, lcp2_imbalances[srcnode]));
1835 for (tmp_ip=all_ips; tmp_ip; tmp_ip=tmp_ip->next) {
1836 /* Only consider addresses on srcnode. */
1837 if (tmp_ip->pnn != srcnode) {
1841 /* What is this IP address costing the source node? */
1842 srcdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, srcnode);
1843 srcimbl = lcp2_imbalances[srcnode] - srcdsum;
1845 /* Consider this IP address would cost each potential
1846 * destination node. Destination nodes are limited to
1847 * those that are newly healthy, since we don't want
1848 * to do gratuitous failover of IPs just to make minor
1849 * balance improvements.
1851 for (dstnode=0; dstnode<numnodes; dstnode++) {
1852 if (!rebalance_candidates[dstnode]) {
1856 /* only check nodes that can actually takeover this ip */
1857 if (!can_node_takeover_ip(ctdb, dstnode,
1858 ipflags[dstnode], tmp_ip)) {
1859 /* no it couldnt so skip to the next node */
1863 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1864 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1865 DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
1867 ctdb_addr_to_str(&(tmp_ip->addr)),
1870 if ((dstimbl < lcp2_imbalances[srcnode]) &&
1871 (dstdsum < srcdsum) && \
1872 ((mindstnode == -1) || \
1873 ((srcimbl + dstimbl) < (minsrcimbl + mindstimbl)))) {
1876 minsrcimbl = srcimbl;
1877 mindstnode = dstnode;
1878 mindstimbl = dstimbl;
1882 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1884 if (mindstnode != -1) {
1885 /* We found a move that makes things better... */
1886 DEBUG(DEBUG_INFO,("%d [%d] -> %s -> %d [+%d]\n",
1887 srcnode, minsrcimbl - lcp2_imbalances[srcnode],
1888 ctdb_addr_to_str(&(minip->addr)),
1889 mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));
1892 lcp2_imbalances[srcnode] = minsrcimbl;
1893 lcp2_imbalances[mindstnode] = mindstimbl;
1894 minip->pnn = mindstnode;
1903 struct lcp2_imbalance_pnn {
1908 static int lcp2_cmp_imbalance_pnn(const void * a, const void * b)
1910 const struct lcp2_imbalance_pnn * lipa = (const struct lcp2_imbalance_pnn *) a;
1911 const struct lcp2_imbalance_pnn * lipb = (const struct lcp2_imbalance_pnn *) b;
1913 if (lipa->imbalance > lipb->imbalance) {
1915 } else if (lipa->imbalance == lipb->imbalance) {
1922 /* LCP2 algorithm for rebalancing the cluster. This finds the source
1923 * node with the highest LCP2 imbalance, and then determines the best
1924 * IP/destination node combination to move from the source node.
1926 static void lcp2_failback(struct ctdb_context *ctdb,
1927 struct ctdb_ipflags *ipflags,
1928 struct ctdb_public_ip_list *all_ips,
1929 uint32_t *lcp2_imbalances,
1930 bool *rebalance_candidates)
1933 struct lcp2_imbalance_pnn * lips;
1936 numnodes = talloc_array_length(ipflags);
1939 /* Put the imbalances and nodes into an array, sort them and
1940 * iterate through candidates. Usually the 1st one will be
1941 * used, so this doesn't cost much...
1943 DEBUG(DEBUG_DEBUG,("+++++++++++++++++++++++++++++++++++++++++\n"));
1944 DEBUG(DEBUG_DEBUG,("Selecting most imbalanced node from:\n"));
1945 lips = talloc_array(ctdb, struct lcp2_imbalance_pnn, numnodes);
1946 for (i=0; i<numnodes; i++) {
1947 lips[i].imbalance = lcp2_imbalances[i];
1949 DEBUG(DEBUG_DEBUG,(" %d [%d]\n", i, lcp2_imbalances[i]));
1951 qsort(lips, numnodes, sizeof(struct lcp2_imbalance_pnn),
1952 lcp2_cmp_imbalance_pnn);
1955 for (i=0; i<numnodes; i++) {
1956 /* This means that all nodes had 0 or 1 addresses, so
1957 * can't be imbalanced.
1959 if (lips[i].imbalance == 0) {
1963 if (lcp2_failback_candidate(ctdb,
1968 rebalance_candidates)) {
1980 static void unassign_unsuitable_ips(struct ctdb_context *ctdb,
1981 struct ctdb_ipflags *ipflags,
1982 struct ctdb_public_ip_list *all_ips)
1984 struct ctdb_public_ip_list *tmp_ip;
1986 /* verify that the assigned nodes can serve that public ip
1987 and set it to -1 if not
1989 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1990 if (tmp_ip->pnn == -1) {
1993 if (!can_node_host_ip(ctdb, tmp_ip->pnn,
1994 ipflags[tmp_ip->pnn], tmp_ip) != 0) {
1995 /* this node can not serve this ip. */
1996 DEBUG(DEBUG_DEBUG,("Unassign IP: %s from %d\n",
1997 ctdb_addr_to_str(&(tmp_ip->addr)),
2004 static void ip_alloc_deterministic_ips(struct ctdb_context *ctdb,
2005 struct ctdb_ipflags *ipflags,
2006 struct ctdb_public_ip_list *all_ips)
2008 struct ctdb_public_ip_list *tmp_ip;
2011 numnodes = talloc_array_length(ipflags);
2013 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2014 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2015 * always be allocated the same way for a specific set of
2016 * available/unavailable nodes.
2019 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
2020 tmp_ip->pnn = i % numnodes;
2023 /* IP failback doesn't make sense with deterministic
2024 * IPs, since the modulo step above implicitly fails
2025 * back IPs to their "home" node.
2027 if (1 == ctdb->tunable.no_ip_failback) {
2028 DEBUG(DEBUG_WARNING, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2031 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2033 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2035 /* No failback here! */
2038 static void ip_alloc_nondeterministic_ips(struct ctdb_context *ctdb,
2039 struct ctdb_ipflags *ipflags,
2040 struct ctdb_public_ip_list *all_ips)
2042 /* This should be pushed down into basic_failback. */
2043 struct ctdb_public_ip_list *tmp_ip;
2045 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2049 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2051 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2053 /* If we don't want IPs to fail back then don't rebalance IPs. */
2054 if (1 == ctdb->tunable.no_ip_failback) {
2058 /* Now, try to make sure the ip adresses are evenly distributed
2061 basic_failback(ctdb, ipflags, all_ips, num_ips);
2064 static void ip_alloc_lcp2(struct ctdb_context *ctdb,
2065 struct ctdb_ipflags *ipflags,
2066 struct ctdb_public_ip_list *all_ips,
2067 uint32_t *force_rebalance_nodes)
2069 uint32_t *lcp2_imbalances;
2070 bool *rebalance_candidates;
2071 int numnodes, num_rebalance_candidates, i;
2073 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2075 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2077 lcp2_init(tmp_ctx, ipflags, all_ips,force_rebalance_nodes,
2078 &lcp2_imbalances, &rebalance_candidates);
2080 lcp2_allocate_unassigned(ctdb, ipflags, all_ips, lcp2_imbalances);
2082 /* If we don't want IPs to fail back then don't rebalance IPs. */
2083 if (1 == ctdb->tunable.no_ip_failback) {
2087 /* It is only worth continuing if we have suitable target
2088 * nodes to transfer IPs to. This check is much cheaper than
2091 numnodes = talloc_array_length(ipflags);
2092 num_rebalance_candidates = 0;
2093 for (i=0; i<numnodes; i++) {
2094 if (rebalance_candidates[i]) {
2095 num_rebalance_candidates++;
2098 if (num_rebalance_candidates == 0) {
2102 /* Now, try to make sure the ip adresses are evenly distributed
2105 lcp2_failback(ctdb, ipflags, all_ips,
2106 lcp2_imbalances, rebalance_candidates);
2109 talloc_free(tmp_ctx);
2112 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
2116 for (i=0;i<nodemap->num;i++) {
2117 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
2118 /* Found one completely healthy node */
2126 /* The calculation part of the IP allocation algorithm. */
2127 static void ctdb_takeover_run_core(struct ctdb_context *ctdb,
2128 struct ctdb_ipflags *ipflags,
2129 struct ctdb_public_ip_list **all_ips_p,
2130 uint32_t *force_rebalance_nodes)
2132 /* since nodes only know about those public addresses that
2133 can be served by that particular node, no single node has
2134 a full list of all public addresses that exist in the cluster.
2135 Walk over all node structures and create a merged list of
2136 all public addresses that exist in the cluster.
2138 keep the tree of ips around as ctdb->ip_tree
2140 *all_ips_p = create_merged_ip_list(ctdb);
2142 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2143 ip_alloc_lcp2(ctdb, ipflags, *all_ips_p, force_rebalance_nodes);
2144 } else if (1 == ctdb->tunable.deterministic_public_ips) {
2145 ip_alloc_deterministic_ips(ctdb, ipflags, *all_ips_p);
2147 ip_alloc_nondeterministic_ips(ctdb, ipflags, *all_ips_p);
2150 /* at this point ->pnn is the node which will own each IP
2151 or -1 if there is no node that can cover this ip
2157 struct get_tunable_callback_data {
2158 const char *tunable;
2163 static void get_tunable_callback(struct ctdb_context *ctdb, uint32_t pnn,
2164 int32_t res, TDB_DATA outdata,
2167 struct get_tunable_callback_data *cd =
2168 (struct get_tunable_callback_data *)callback;
2172 /* Already handled in fail callback */
2176 if (outdata.dsize != sizeof(uint32_t)) {
2177 DEBUG(DEBUG_ERR,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2178 cd->tunable, pnn, (int)sizeof(uint32_t),
2179 (int)outdata.dsize));
2184 size = talloc_array_length(cd->out);
2186 DEBUG(DEBUG_ERR,("Got %s reply from node %d but nodemap only has %d entries\n",
2187 cd->tunable, pnn, size));
2192 cd->out[pnn] = *(uint32_t *)outdata.dptr;
2195 static void get_tunable_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2196 int32_t res, TDB_DATA outdata,
2199 struct get_tunable_callback_data *cd =
2200 (struct get_tunable_callback_data *)callback;
2205 ("Timed out getting tunable \"%s\" from node %d\n",
2211 DEBUG(DEBUG_WARNING,
2212 ("Tunable \"%s\" not implemented on node %d\n",
2217 ("Unexpected error getting tunable \"%s\" from node %d\n",
2223 static uint32_t *get_tunable_from_nodes(struct ctdb_context *ctdb,
2224 TALLOC_CTX *tmp_ctx,
2225 struct ctdb_node_map *nodemap,
2226 const char *tunable,
2227 uint32_t default_value)
2230 struct ctdb_control_get_tunable *t;
2233 struct get_tunable_callback_data callback_data;
2236 tvals = talloc_array(tmp_ctx, uint32_t, nodemap->num);
2237 CTDB_NO_MEMORY_NULL(ctdb, tvals);
2238 for (i=0; i<nodemap->num; i++) {
2239 tvals[i] = default_value;
2242 callback_data.out = tvals;
2243 callback_data.tunable = tunable;
2244 callback_data.fatal = false;
2246 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen(tunable) + 1;
2247 data.dptr = talloc_size(tmp_ctx, data.dsize);
2248 t = (struct ctdb_control_get_tunable *)data.dptr;
2249 t->length = strlen(tunable)+1;
2250 memcpy(t->name, tunable, t->length);
2251 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2252 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_TUNABLE,
2253 nodes, 0, TAKEOVER_TIMEOUT(),
2255 get_tunable_callback,
2256 get_tunable_fail_callback,
2257 &callback_data) != 0) {
2258 if (callback_data.fatal) {
2264 talloc_free(data.dptr);
2269 struct get_runstate_callback_data {
2270 enum ctdb_runstate *out;
2274 static void get_runstate_callback(struct ctdb_context *ctdb, uint32_t pnn,
2275 int32_t res, TDB_DATA outdata,
2276 void *callback_data)
2278 struct get_runstate_callback_data *cd =
2279 (struct get_runstate_callback_data *)callback_data;
2283 /* Already handled in fail callback */
2287 if (outdata.dsize != sizeof(uint32_t)) {
2288 DEBUG(DEBUG_ERR,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2289 pnn, (int)sizeof(uint32_t),
2290 (int)outdata.dsize));
2295 size = talloc_array_length(cd->out);
2297 DEBUG(DEBUG_ERR,("Got reply from node %d but nodemap only has %d entries\n",
2302 cd->out[pnn] = (enum ctdb_runstate)*(uint32_t *)outdata.dptr;
2305 static void get_runstate_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2306 int32_t res, TDB_DATA outdata,
2309 struct get_runstate_callback_data *cd =
2310 (struct get_runstate_callback_data *)callback;
2315 ("Timed out getting runstate from node %d\n", pnn));
2319 DEBUG(DEBUG_WARNING,
2320 ("Error getting runstate from node %d - assuming runstates not supported\n",
2325 static enum ctdb_runstate * get_runstate_from_nodes(struct ctdb_context *ctdb,
2326 TALLOC_CTX *tmp_ctx,
2327 struct ctdb_node_map *nodemap,
2328 enum ctdb_runstate default_value)
2331 enum ctdb_runstate *rs;
2332 struct get_runstate_callback_data callback_data;
2335 rs = talloc_array(tmp_ctx, enum ctdb_runstate, nodemap->num);
2336 CTDB_NO_MEMORY_NULL(ctdb, rs);
2337 for (i=0; i<nodemap->num; i++) {
2338 rs[i] = default_value;
2341 callback_data.out = rs;
2342 callback_data.fatal = false;
2344 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2345 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_RUNSTATE,
2346 nodes, 0, TAKEOVER_TIMEOUT(),
2348 get_runstate_callback,
2349 get_runstate_fail_callback,
2350 &callback_data) != 0) {
2351 if (callback_data.fatal) {
2361 /* Set internal flags for IP allocation:
2363 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2364 * Set NOIPHOST ip flag for each INACTIVE node
2365 * if all nodes are disabled:
2366 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2368 * Set NOIPHOST ip flags for disabled nodes
2370 static struct ctdb_ipflags *
2371 set_ipflags_internal(struct ctdb_context *ctdb,
2372 TALLOC_CTX *tmp_ctx,
2373 struct ctdb_node_map *nodemap,
2374 uint32_t *tval_noiptakeover,
2375 uint32_t *tval_noiphostonalldisabled,
2376 enum ctdb_runstate *runstate)
2379 struct ctdb_ipflags *ipflags;
2381 /* Clear IP flags - implicit due to talloc_zero */
2382 ipflags = talloc_zero_array(tmp_ctx, struct ctdb_ipflags, nodemap->num);
2383 CTDB_NO_MEMORY_NULL(ctdb, ipflags);
2385 for (i=0;i<nodemap->num;i++) {
2386 /* Can not take IPs on node with NoIPTakeover set */
2387 if (tval_noiptakeover[i] != 0) {
2388 ipflags[i].noiptakeover = true;
2391 /* Can not host IPs on node not in RUNNING state */
2392 if (runstate[i] != CTDB_RUNSTATE_RUNNING) {
2393 ipflags[i].noiphost = true;
2396 /* Can not host IPs on INACTIVE node */
2397 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2398 ipflags[i].noiphost = true;
2400 /* Remember the runstate */
2401 ipflags[i].runstate = runstate[i];
2404 if (all_nodes_are_disabled(nodemap)) {
2405 /* If all nodes are disabled, can not host IPs on node
2406 * with NoIPHostOnAllDisabled set
2408 for (i=0;i<nodemap->num;i++) {
2409 if (tval_noiphostonalldisabled[i] != 0) {
2410 ipflags[i].noiphost = true;
2414 /* If some nodes are not disabled, then can not host
2415 * IPs on DISABLED node
2417 for (i=0;i<nodemap->num;i++) {
2418 if (nodemap->nodes[i].flags & NODE_FLAGS_DISABLED) {
2419 ipflags[i].noiphost = true;
2427 static struct ctdb_ipflags *set_ipflags(struct ctdb_context *ctdb,
2428 TALLOC_CTX *tmp_ctx,
2429 struct ctdb_node_map *nodemap)
2431 uint32_t *tval_noiptakeover;
2432 uint32_t *tval_noiphostonalldisabled;
2433 struct ctdb_ipflags *ipflags;
2434 enum ctdb_runstate *runstate;
2437 tval_noiptakeover = get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2439 if (tval_noiptakeover == NULL) {
2443 tval_noiphostonalldisabled =
2444 get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2445 "NoIPHostOnAllDisabled", 0);
2446 if (tval_noiphostonalldisabled == NULL) {
2447 /* Caller frees tmp_ctx */
2451 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2452 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2453 * reasonable behaviour on a mixed cluster during upgrade.
2455 runstate = get_runstate_from_nodes(ctdb, tmp_ctx, nodemap,
2456 CTDB_RUNSTATE_RUNNING);
2457 if (runstate == NULL) {
2458 /* Caller frees tmp_ctx */
2462 ipflags = set_ipflags_internal(ctdb, tmp_ctx, nodemap,
2464 tval_noiphostonalldisabled,
2467 talloc_free(tval_noiptakeover);
2468 talloc_free(tval_noiphostonalldisabled);
2469 talloc_free(runstate);
2474 struct iprealloc_callback_data {
2477 client_async_callback fail_callback;
2478 void *fail_callback_data;
2479 struct ctdb_node_map *nodemap;
2482 static void iprealloc_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2483 int32_t res, TDB_DATA outdata,
2487 struct iprealloc_callback_data *cd =
2488 (struct iprealloc_callback_data *)callback;
2490 numnodes = talloc_array_length(cd->retry_nodes);
2491 if (pnn > numnodes) {
2493 ("ipreallocated failure from node %d, "
2494 "but only %d nodes in nodemap\n",
2499 /* Can't run the "ipreallocated" event on a INACTIVE node */
2500 if (cd->nodemap->nodes[pnn].flags & NODE_FLAGS_INACTIVE) {
2501 DEBUG(DEBUG_WARNING,
2502 ("ipreallocated failed on inactive node %d, ignoring\n",
2509 /* If the control timed out then that's a real error,
2510 * so call the real fail callback
2512 if (cd->fail_callback) {
2513 cd->fail_callback(ctdb, pnn, res, outdata,
2514 cd->fail_callback_data);
2516 DEBUG(DEBUG_WARNING,
2517 ("iprealloc timed out but no callback registered\n"));
2521 /* If not a timeout then either the ipreallocated
2522 * eventscript (or some setup) failed. This might
2523 * have failed because the IPREALLOCATED control isn't
2524 * implemented - right now there is no way of knowing
2525 * because the error codes are all folded down to -1.
2526 * Consider retrying using EVENTSCRIPT control...
2528 DEBUG(DEBUG_WARNING,
2529 ("ipreallocated failure from node %d, flagging retry\n",
2531 cd->retry_nodes[pnn] = true;
2536 struct takeover_callback_data {
2538 client_async_callback fail_callback;
2539 void *fail_callback_data;
2540 struct ctdb_node_map *nodemap;
2543 static void takeover_run_fail_callback(struct ctdb_context *ctdb,
2544 uint32_t node_pnn, int32_t res,
2545 TDB_DATA outdata, void *callback_data)
2547 struct takeover_callback_data *cd =
2548 talloc_get_type_abort(callback_data,
2549 struct takeover_callback_data);
2552 for (i = 0; i < cd->nodemap->num; i++) {
2553 if (node_pnn == cd->nodemap->nodes[i].pnn) {
2558 if (i == cd->nodemap->num) {
2559 DEBUG(DEBUG_ERR, (__location__ " invalid PNN %u\n", node_pnn));
2563 if (!cd->node_failed[i]) {
2564 cd->node_failed[i] = true;
2565 cd->fail_callback(ctdb, node_pnn, res, outdata,
2566 cd->fail_callback_data);
2571 make any IP alias changes for public addresses that are necessary
2573 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
2574 uint32_t *force_rebalance_nodes,
2575 client_async_callback fail_callback, void *callback_data)
2578 struct ctdb_public_ip ip;
2580 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2582 struct timeval timeout;
2583 struct client_async_data *async_data;
2584 struct ctdb_client_control_state *state;
2585 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2586 struct ctdb_ipflags *ipflags;
2587 struct takeover_callback_data *takeover_data;
2588 struct iprealloc_callback_data iprealloc_data;
2593 * ip failover is completely disabled, just send out the
2594 * ipreallocated event.
2596 if (ctdb->tunable.disable_ip_failover != 0) {
2600 ipflags = set_ipflags(ctdb, tmp_ctx, nodemap);
2601 if (ipflags == NULL) {
2602 DEBUG(DEBUG_ERR,("Failed to set IP flags - aborting takeover run\n"));
2603 talloc_free(tmp_ctx);
2607 /* Short-circuit IP allocation if no nodes are in the RUNNING
2608 * runstate yet, since no nodes will be able to host IPs */
2609 can_host_ips = false;
2610 for (i=0; i<nodemap->num; i++) {
2611 if (ipflags[i].runstate == CTDB_RUNSTATE_RUNNING) {
2612 can_host_ips = true;
2615 if (!can_host_ips) {
2616 DEBUG(DEBUG_WARNING,("No nodes available to host public IPs yet\n"));
2620 /* Do the IP reassignment calculations */
2621 ctdb_takeover_run_core(ctdb, ipflags, &all_ips, force_rebalance_nodes);
2623 /* Now tell all nodes to release any public IPs should not
2624 * host. This will be a NOOP on nodes that don't currently
2625 * hold the given IP.
2627 takeover_data = talloc_zero(tmp_ctx, struct takeover_callback_data);
2628 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data);
2630 takeover_data->node_failed = talloc_zero_array(tmp_ctx,
2631 bool, nodemap->num);
2632 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data->node_failed);
2633 takeover_data->fail_callback = fail_callback;
2634 takeover_data->fail_callback_data = callback_data;
2635 takeover_data->nodemap = nodemap;
2637 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2638 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2640 async_data->fail_callback = takeover_run_fail_callback;
2641 async_data->callback_data = takeover_data;
2643 ZERO_STRUCT(ip); /* Avoid valgrind warnings for union */
2645 /* Send a RELEASE_IP to all nodes that should not be hosting
2646 * each IP. For each IP, all but one of these will be
2647 * redundant. However, the redundant ones are used to tell
2648 * nodes which node should be hosting the IP so that commands
2649 * like "ctdb ip" can display a particular nodes idea of who
2650 * is hosting what. */
2651 for (i=0;i<nodemap->num;i++) {
2652 /* don't talk to unconnected nodes, but do talk to banned nodes */
2653 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2657 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2658 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
2659 /* This node should be serving this
2660 vnn so dont tell it to release the ip
2664 ip.pnn = tmp_ip->pnn;
2665 ip.addr = tmp_ip->addr;
2667 timeout = TAKEOVER_TIMEOUT();
2668 data.dsize = sizeof(ip);
2669 data.dptr = (uint8_t *)&ip;
2670 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2671 0, CTDB_CONTROL_RELEASE_IP, 0,
2674 if (state == NULL) {
2675 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
2676 talloc_free(tmp_ctx);
2680 ctdb_client_async_add(async_data, state);
2683 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2684 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2685 talloc_free(tmp_ctx);
2688 talloc_free(async_data);
2691 /* For each IP, send a TAKOVER_IP to the node that should be
2692 * hosting it. Many of these will often be redundant (since
2693 * the allocation won't have changed) but they can be useful
2694 * to recover from inconsistencies. */
2695 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2696 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2698 async_data->fail_callback = fail_callback;
2699 async_data->callback_data = callback_data;
2701 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2702 if (tmp_ip->pnn == -1) {
2703 /* this IP won't be taken over */
2707 ip.pnn = tmp_ip->pnn;
2708 ip.addr = tmp_ip->addr;
2710 timeout = TAKEOVER_TIMEOUT();
2711 data.dsize = sizeof(ip);
2712 data.dptr = (uint8_t *)&ip;
2713 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2714 0, CTDB_CONTROL_TAKEOVER_IP, 0,
2715 data, async_data, &timeout, NULL);
2716 if (state == NULL) {
2717 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
2718 talloc_free(tmp_ctx);
2722 ctdb_client_async_add(async_data, state);
2724 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2725 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2726 talloc_free(tmp_ctx);
2732 * Tell all nodes to run eventscripts to process the
2733 * "ipreallocated" event. This can do a lot of things,
2734 * including restarting services to reconfigure them if public
2735 * IPs have moved. Once upon a time this event only used to
2738 retry_data = talloc_zero_array(tmp_ctx, bool, nodemap->num);
2739 CTDB_NO_MEMORY_FATAL(ctdb, retry_data);
2740 iprealloc_data.retry_nodes = retry_data;
2741 iprealloc_data.retry_count = 0;
2742 iprealloc_data.fail_callback = fail_callback;
2743 iprealloc_data.fail_callback_data = callback_data;
2744 iprealloc_data.nodemap = nodemap;
2746 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2747 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_IPREALLOCATED,
2748 nodes, 0, TAKEOVER_TIMEOUT(),
2750 NULL, iprealloc_fail_callback,
2753 /* If the control failed then we should retry to any
2754 * nodes flagged by iprealloc_fail_callback using the
2755 * EVENTSCRIPT control. This is a best-effort at
2756 * backward compatiblity when running a mixed cluster
2757 * where some nodes have not yet been upgraded to
2758 * support the IPREALLOCATED control.
2760 DEBUG(DEBUG_WARNING,
2761 ("Retry ipreallocated to some nodes using eventscript control\n"));
2763 nodes = talloc_array(tmp_ctx, uint32_t,
2764 iprealloc_data.retry_count);
2765 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2768 for (i=0; i<nodemap->num; i++) {
2769 if (iprealloc_data.retry_nodes[i]) {
2775 data.dptr = discard_const("ipreallocated");
2776 data.dsize = strlen((char *)data.dptr) + 1;
2777 ret = ctdb_client_async_control(ctdb,
2778 CTDB_CONTROL_RUN_EVENTSCRIPTS,
2779 nodes, 0, TAKEOVER_TIMEOUT(),
2781 NULL, fail_callback,
2784 DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
2788 talloc_free(tmp_ctx);
2794 destroy a ctdb_client_ip structure
2796 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
2798 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
2799 ctdb_addr_to_str(&ip->addr),
2800 ntohs(ip->addr.ip.sin_port),
2803 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
2808 called by a client to inform us of a TCP connection that it is managing
2809 that should tickled with an ACK when IP takeover is done
2811 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
2814 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
2815 struct ctdb_control_tcp_addr *tcp_sock = NULL;
2816 struct ctdb_tcp_list *tcp;
2817 struct ctdb_tcp_connection t;
2820 struct ctdb_client_ip *ip;
2821 struct ctdb_vnn *vnn;
2822 ctdb_sock_addr addr;
2824 /* If we don't have public IPs, tickles are useless */
2825 if (ctdb->vnn == NULL) {
2829 tcp_sock = (struct ctdb_control_tcp_addr *)indata.dptr;
2831 addr = tcp_sock->src;
2832 ctdb_canonicalize_ip(&addr, &tcp_sock->src);
2833 addr = tcp_sock->dest;
2834 ctdb_canonicalize_ip(&addr, &tcp_sock->dest);
2837 memcpy(&addr, &tcp_sock->dest, sizeof(addr));
2838 vnn = find_public_ip_vnn(ctdb, &addr);
2840 switch (addr.sa.sa_family) {
2842 if (ntohl(addr.ip.sin_addr.s_addr) != INADDR_LOOPBACK) {
2843 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public address.\n",
2844 ctdb_addr_to_str(&addr)));
2848 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public ipv6 address.\n",
2849 ctdb_addr_to_str(&addr)));
2852 DEBUG(DEBUG_ERR,(__location__ " Unknown family type %d\n", addr.sa.sa_family));
2858 if (vnn->pnn != ctdb->pnn) {
2859 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2860 ctdb_addr_to_str(&addr),
2861 client_id, client->pid));
2862 /* failing this call will tell smbd to die */
2866 ip = talloc(client, struct ctdb_client_ip);
2867 CTDB_NO_MEMORY(ctdb, ip);
2871 ip->client_id = client_id;
2872 talloc_set_destructor(ip, ctdb_client_ip_destructor);
2873 DLIST_ADD(ctdb->client_ip_list, ip);
2875 tcp = talloc(client, struct ctdb_tcp_list);
2876 CTDB_NO_MEMORY(ctdb, tcp);
2878 tcp->connection.src_addr = tcp_sock->src;
2879 tcp->connection.dst_addr = tcp_sock->dest;
2881 DLIST_ADD(client->tcp_list, tcp);
2883 t.src_addr = tcp_sock->src;
2884 t.dst_addr = tcp_sock->dest;
2886 data.dptr = (uint8_t *)&t;
2887 data.dsize = sizeof(t);
2889 switch (addr.sa.sa_family) {
2891 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2892 (unsigned)ntohs(tcp_sock->dest.ip.sin_port),
2893 ctdb_addr_to_str(&tcp_sock->src),
2894 (unsigned)ntohs(tcp_sock->src.ip.sin_port), client_id, client->pid));
2897 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2898 (unsigned)ntohs(tcp_sock->dest.ip6.sin6_port),
2899 ctdb_addr_to_str(&tcp_sock->src),
2900 (unsigned)ntohs(tcp_sock->src.ip6.sin6_port), client_id, client->pid));
2903 DEBUG(DEBUG_ERR,(__location__ " Unknown family %d\n", addr.sa.sa_family));
2907 /* tell all nodes about this tcp connection */
2908 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
2909 CTDB_CONTROL_TCP_ADD,
2910 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
2912 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
2920 find a tcp address on a list
2922 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
2923 struct ctdb_tcp_connection *tcp)
2927 if (array == NULL) {
2931 for (i=0;i<array->num;i++) {
2932 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
2933 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
2934 return &array->connections[i];
2943 called by a daemon to inform us of a TCP connection that one of its
2944 clients managing that should tickled with an ACK when IP takeover is
2947 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
2949 struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
2950 struct ctdb_tcp_array *tcparray;
2951 struct ctdb_tcp_connection tcp;
2952 struct ctdb_vnn *vnn;
2954 /* If we don't have public IPs, tickles are useless */
2955 if (ctdb->vnn == NULL) {
2959 vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
2961 DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
2962 ctdb_addr_to_str(&p->dst_addr)));
2968 tcparray = vnn->tcp_array;
2970 /* If this is the first tickle */
2971 if (tcparray == NULL) {
2972 tcparray = talloc(vnn, struct ctdb_tcp_array);
2973 CTDB_NO_MEMORY(ctdb, tcparray);
2974 vnn->tcp_array = tcparray;
2977 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
2978 CTDB_NO_MEMORY(ctdb, tcparray->connections);
2980 tcparray->connections[tcparray->num].src_addr = p->src_addr;
2981 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
2984 if (tcp_update_needed) {
2985 vnn->tcp_update_needed = true;
2991 /* Do we already have this tickle ?*/
2992 tcp.src_addr = p->src_addr;
2993 tcp.dst_addr = p->dst_addr;
2994 if (ctdb_tcp_find(tcparray, &tcp) != NULL) {
2995 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
2996 ctdb_addr_to_str(&tcp.dst_addr),
2997 ntohs(tcp.dst_addr.ip.sin_port),
3002 /* A new tickle, we must add it to the array */
3003 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
3004 struct ctdb_tcp_connection,
3006 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3008 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3009 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3012 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
3013 ctdb_addr_to_str(&tcp.dst_addr),
3014 ntohs(tcp.dst_addr.ip.sin_port),
3017 if (tcp_update_needed) {
3018 vnn->tcp_update_needed = true;
3026 called by a daemon to inform us of a TCP connection that one of its
3027 clients managing that should tickled with an ACK when IP takeover is
3030 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
3032 struct ctdb_tcp_connection *tcpp;
3033 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
3036 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
3037 ctdb_addr_to_str(&conn->dst_addr)));
3041 /* if the array is empty we cant remove it
3042 and we dont need to do anything
3044 if (vnn->tcp_array == NULL) {
3045 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3046 ctdb_addr_to_str(&conn->dst_addr),
3047 ntohs(conn->dst_addr.ip.sin_port)));
3052 /* See if we know this connection
3053 if we dont know this connection then we dont need to do anything
3055 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
3057 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
3058 ctdb_addr_to_str(&conn->dst_addr),
3059 ntohs(conn->dst_addr.ip.sin_port)));
3064 /* We need to remove this entry from the array.
3065 Instead of allocating a new array and copying data to it
3066 we cheat and just copy the last entry in the existing array
3067 to the entry that is to be removed and just shring the
3070 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
3071 vnn->tcp_array->num--;
3073 /* If we deleted the last entry we also need to remove the entire array
3075 if (vnn->tcp_array->num == 0) {
3076 talloc_free(vnn->tcp_array);
3077 vnn->tcp_array = NULL;
3080 vnn->tcp_update_needed = true;
3082 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
3083 ctdb_addr_to_str(&conn->src_addr),
3084 ntohs(conn->src_addr.ip.sin_port)));
3089 called by a daemon to inform us of a TCP connection that one of its
3090 clients used are no longer needed in the tickle database
3092 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
3094 struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
3096 /* If we don't have public IPs, tickles are useless */
3097 if (ctdb->vnn == NULL) {
3101 ctdb_remove_tcp_connection(ctdb, conn);
3108 Called when another daemon starts - causes all tickles for all
3109 public addresses we are serving to be sent to the new node on the
3110 next check. This actually causes the next scheduled call to
3111 tdb_update_tcp_tickles() to update all nodes. This is simple and
3112 doesn't require careful error handling.
3114 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t pnn)
3116 struct ctdb_vnn *vnn;
3118 DEBUG(DEBUG_INFO, ("Received startup control from node %lu\n",
3119 (unsigned long) pnn));
3121 for (vnn = ctdb->vnn; vnn != NULL; vnn = vnn->next) {
3122 vnn->tcp_update_needed = true;
3130 called when a client structure goes away - hook to remove
3131 elements from the tcp_list in all daemons
3133 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
3135 while (client->tcp_list) {
3136 struct ctdb_tcp_list *tcp = client->tcp_list;
3137 DLIST_REMOVE(client->tcp_list, tcp);
3138 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
3144 release all IPs on shutdown
3146 void ctdb_release_all_ips(struct ctdb_context *ctdb)
3148 struct ctdb_vnn *vnn;
3151 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3152 if (!ctdb_sys_have_ip(&vnn->public_address)) {
3153 ctdb_vnn_unassign_iface(ctdb, vnn);
3160 DEBUG(DEBUG_INFO,("Release of IP %s/%u on interface %s node:-1\n",
3161 ctdb_addr_to_str(&vnn->public_address),
3162 vnn->public_netmask_bits,
3163 ctdb_vnn_iface_string(vnn)));
3165 ctdb_event_script_args(ctdb, CTDB_EVENT_RELEASE_IP, "%s %s %u",
3166 ctdb_vnn_iface_string(vnn),
3167 ctdb_addr_to_str(&vnn->public_address),
3168 vnn->public_netmask_bits);
3169 release_kill_clients(ctdb, &vnn->public_address);
3170 ctdb_vnn_unassign_iface(ctdb, vnn);
3174 DEBUG(DEBUG_NOTICE,(__location__ " Released %d public IPs\n", count));
3179 get list of public IPs
3181 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
3182 struct ctdb_req_control *c, TDB_DATA *outdata)
3185 struct ctdb_all_public_ips *ips;
3186 struct ctdb_vnn *vnn;
3187 bool only_available = false;
3189 if (c->flags & CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE) {
3190 only_available = true;
3193 /* count how many public ip structures we have */
3195 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3199 len = offsetof(struct ctdb_all_public_ips, ips) +
3200 num*sizeof(struct ctdb_public_ip);
3201 ips = talloc_zero_size(outdata, len);
3202 CTDB_NO_MEMORY(ctdb, ips);
3205 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3206 if (only_available && !ctdb_vnn_available(ctdb, vnn)) {
3209 ips->ips[i].pnn = vnn->pnn;
3210 ips->ips[i].addr = vnn->public_address;
3214 len = offsetof(struct ctdb_all_public_ips, ips) +
3215 i*sizeof(struct ctdb_public_ip);
3217 outdata->dsize = len;
3218 outdata->dptr = (uint8_t *)ips;
3224 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
3225 struct ctdb_req_control *c,
3230 ctdb_sock_addr *addr;
3231 struct ctdb_control_public_ip_info *info;
3232 struct ctdb_vnn *vnn;
3234 addr = (ctdb_sock_addr *)indata.dptr;
3236 vnn = find_public_ip_vnn(ctdb, addr);
3238 /* if it is not a public ip it could be our 'single ip' */
3239 if (ctdb->single_ip_vnn) {
3240 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
3241 vnn = ctdb->single_ip_vnn;
3246 DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
3247 "'%s'not a public address\n",
3248 ctdb_addr_to_str(addr)));
3252 /* count how many public ip structures we have */
3254 for (;vnn->ifaces[num];) {
3258 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3259 num*sizeof(struct ctdb_control_iface_info);
3260 info = talloc_zero_size(outdata, len);
3261 CTDB_NO_MEMORY(ctdb, info);
3263 info->ip.addr = vnn->public_address;
3264 info->ip.pnn = vnn->pnn;
3265 info->active_idx = 0xFFFFFFFF;
3267 for (i=0; vnn->ifaces[i]; i++) {
3268 struct ctdb_iface *cur;
3270 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
3272 DEBUG(DEBUG_CRIT, (__location__ " internal error iface[%s] unknown\n",
3276 if (vnn->iface == cur) {
3277 info->active_idx = i;
3279 strncpy(info->ifaces[i].name, cur->name, sizeof(info->ifaces[i].name)-1);
3280 info->ifaces[i].link_state = cur->link_up;
3281 info->ifaces[i].references = cur->references;
3284 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3285 i*sizeof(struct ctdb_control_iface_info);
3287 outdata->dsize = len;
3288 outdata->dptr = (uint8_t *)info;
3293 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
3294 struct ctdb_req_control *c,
3298 struct ctdb_control_get_ifaces *ifaces;
3299 struct ctdb_iface *cur;
3301 /* count how many public ip structures we have */
3303 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3307 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3308 num*sizeof(struct ctdb_control_iface_info);
3309 ifaces = talloc_zero_size(outdata, len);
3310 CTDB_NO_MEMORY(ctdb, ifaces);
3313 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3314 strcpy(ifaces->ifaces[i].name, cur->name);
3315 ifaces->ifaces[i].link_state = cur->link_up;
3316 ifaces->ifaces[i].references = cur->references;
3320 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3321 i*sizeof(struct ctdb_control_iface_info);
3323 outdata->dsize = len;
3324 outdata->dptr = (uint8_t *)ifaces;
3329 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
3330 struct ctdb_req_control *c,
3333 struct ctdb_control_iface_info *info;
3334 struct ctdb_iface *iface;
3335 bool link_up = false;
3337 info = (struct ctdb_control_iface_info *)indata.dptr;
3339 if (info->name[CTDB_IFACE_SIZE] != '\0') {
3340 int len = strnlen(info->name, CTDB_IFACE_SIZE);
3341 DEBUG(DEBUG_ERR, (__location__ " name[%*.*s] not terminated\n",
3342 len, len, info->name));
3346 switch (info->link_state) {
3354 DEBUG(DEBUG_ERR, (__location__ " link_state[%u] invalid\n",
3355 (unsigned int)info->link_state));
3359 if (info->references != 0) {
3360 DEBUG(DEBUG_ERR, (__location__ " references[%u] should be 0\n",
3361 (unsigned int)info->references));
3365 iface = ctdb_find_iface(ctdb, info->name);
3366 if (iface == NULL) {
3370 if (link_up == iface->link_up) {
3374 DEBUG(iface->link_up?DEBUG_ERR:DEBUG_NOTICE,
3375 ("iface[%s] has changed it's link status %s => %s\n",
3377 iface->link_up?"up":"down",
3378 link_up?"up":"down"));
3380 iface->link_up = link_up;
3386 structure containing the listening socket and the list of tcp connections
3387 that the ctdb daemon is to kill
3389 struct ctdb_kill_tcp {
3390 struct ctdb_vnn *vnn;
3391 struct ctdb_context *ctdb;
3393 struct fd_event *fde;
3394 trbt_tree_t *connections;
3399 a tcp connection that is to be killed
3401 struct ctdb_killtcp_con {
3402 ctdb_sock_addr src_addr;
3403 ctdb_sock_addr dst_addr;
3405 struct ctdb_kill_tcp *killtcp;
3408 /* this function is used to create a key to represent this socketpair
3409 in the killtcp tree.
3410 this key is used to insert and lookup matching socketpairs that are
3411 to be tickled and RST
3413 #define KILLTCP_KEYLEN 10
3414 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
3416 static uint32_t key[KILLTCP_KEYLEN];
3418 bzero(key, sizeof(key));
3420 if (src->sa.sa_family != dst->sa.sa_family) {
3421 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
3425 switch (src->sa.sa_family) {
3427 key[0] = dst->ip.sin_addr.s_addr;
3428 key[1] = src->ip.sin_addr.s_addr;
3429 key[2] = dst->ip.sin_port;
3430 key[3] = src->ip.sin_port;
3433 uint32_t *dst6_addr32 =
3434 (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
3435 uint32_t *src6_addr32 =
3436 (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
3437 key[0] = dst6_addr32[3];
3438 key[1] = src6_addr32[3];
3439 key[2] = dst6_addr32[2];
3440 key[3] = src6_addr32[2];
3441 key[4] = dst6_addr32[1];
3442 key[5] = src6_addr32[1];
3443 key[6] = dst6_addr32[0];
3444 key[7] = src6_addr32[0];
3445 key[8] = dst->ip6.sin6_port;
3446 key[9] = src->ip6.sin6_port;
3450 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
3458 called when we get a read event on the raw socket
3460 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
3461 uint16_t flags, void *private_data)
3463 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3464 struct ctdb_killtcp_con *con;
3465 ctdb_sock_addr src, dst;
3466 uint32_t ack_seq, seq;
3468 if (!(flags & EVENT_FD_READ)) {
3472 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
3473 killtcp->private_data,
3475 &ack_seq, &seq) != 0) {
3476 /* probably a non-tcp ACK packet */
3480 /* check if we have this guy in our list of connections
3483 con = trbt_lookuparray32(killtcp->connections,
3484 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
3486 /* no this was some other packet we can just ignore */
3490 /* This one has been tickled !
3491 now reset him and remove him from the list.
3493 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3494 ntohs(con->dst_addr.ip.sin_port),
3495 ctdb_addr_to_str(&con->src_addr),
3496 ntohs(con->src_addr.ip.sin_port)));
3498 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
3503 /* when traversing the list of all tcp connections to send tickle acks to
3504 (so that we can capture the ack coming back and kill the connection
3506 this callback is called for each connection we are currently trying to kill
3508 static int tickle_connection_traverse(void *param, void *data)
3510 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
3512 /* have tried too many times, just give up */
3513 if (con->count >= 5) {
3514 /* can't delete in traverse: reparent to delete_cons */
3515 talloc_steal(param, con);
3519 /* othervise, try tickling it again */
3522 (ctdb_sock_addr *)&con->dst_addr,
3523 (ctdb_sock_addr *)&con->src_addr,
3530 called every second until all sentenced connections have been reset
3532 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
3533 struct timeval t, void *private_data)
3535 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3536 void *delete_cons = talloc_new(NULL);
3538 /* loop over all connections sending tickle ACKs */
3539 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
3541 /* now we've finished traverse, it's safe to do deletion. */
3542 talloc_free(delete_cons);
3544 /* If there are no more connections to kill we can remove the
3545 entire killtcp structure
3547 if ( (killtcp->connections == NULL) ||
3548 (killtcp->connections->root == NULL) ) {
3549 talloc_free(killtcp);
3553 /* try tickling them again in a seconds time
3555 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3556 ctdb_tickle_sentenced_connections, killtcp);
3560 destroy the killtcp structure
3562 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
3564 struct ctdb_vnn *tmpvnn;
3566 /* verify that this vnn is still active */
3567 for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
3568 if (tmpvnn == killtcp->vnn) {
3573 if (tmpvnn == NULL) {
3577 if (killtcp->vnn->killtcp != killtcp) {
3581 killtcp->vnn->killtcp = NULL;
3587 /* nothing fancy here, just unconditionally replace any existing
3588 connection structure with the new one.
3590 dont even free the old one if it did exist, that one is talloc_stolen
3591 by the same node in the tree anyway and will be deleted when the new data
3594 static void *add_killtcp_callback(void *parm, void *data)
3600 add a tcp socket to the list of connections we want to RST
3602 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
3606 ctdb_sock_addr src, dst;
3607 struct ctdb_kill_tcp *killtcp;
3608 struct ctdb_killtcp_con *con;
3609 struct ctdb_vnn *vnn;
3611 ctdb_canonicalize_ip(s, &src);
3612 ctdb_canonicalize_ip(d, &dst);
3614 vnn = find_public_ip_vnn(ctdb, &dst);
3616 vnn = find_public_ip_vnn(ctdb, &src);
3619 /* if it is not a public ip it could be our 'single ip' */
3620 if (ctdb->single_ip_vnn) {
3621 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
3622 vnn = ctdb->single_ip_vnn;
3627 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
3631 killtcp = vnn->killtcp;
3633 /* If this is the first connection to kill we must allocate
3636 if (killtcp == NULL) {
3637 killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
3638 CTDB_NO_MEMORY(ctdb, killtcp);
3641 killtcp->ctdb = ctdb;
3642 killtcp->capture_fd = -1;
3643 killtcp->connections = trbt_create(killtcp, 0);
3645 vnn->killtcp = killtcp;
3646 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
3651 /* create a structure that describes this connection we want to
3652 RST and store it in killtcp->connections
3654 con = talloc(killtcp, struct ctdb_killtcp_con);
3655 CTDB_NO_MEMORY(ctdb, con);
3656 con->src_addr = src;
3657 con->dst_addr = dst;
3659 con->killtcp = killtcp;
3662 trbt_insertarray32_callback(killtcp->connections,
3663 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
3664 add_killtcp_callback, con);
3667 If we dont have a socket to listen on yet we must create it
3669 if (killtcp->capture_fd == -1) {
3670 const char *iface = ctdb_vnn_iface_string(vnn);
3671 killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
3672 if (killtcp->capture_fd == -1) {
3673 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
3674 "socket on iface '%s' for killtcp (%s)\n",
3675 iface, strerror(errno)));
3681 if (killtcp->fde == NULL) {
3682 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
3684 capture_tcp_handler, killtcp);
3685 tevent_fd_set_auto_close(killtcp->fde);
3687 /* We also need to set up some events to tickle all these connections
3688 until they are all reset
3690 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3691 ctdb_tickle_sentenced_connections, killtcp);
3694 /* tickle him once now */
3703 talloc_free(vnn->killtcp);
3704 vnn->killtcp = NULL;
3709 kill a TCP connection.
3711 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
3713 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
3715 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
3719 called by a daemon to inform us of the entire list of TCP tickles for
3720 a particular public address.
3721 this control should only be sent by the node that is currently serving
3722 that public address.
3724 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
3726 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
3727 struct ctdb_tcp_array *tcparray;
3728 struct ctdb_vnn *vnn;
3730 /* We must at least have tickles.num or else we cant verify the size
3731 of the received data blob
3733 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3734 tickles.connections)) {
3735 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3739 /* verify that the size of data matches what we expect */
3740 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3741 tickles.connections)
3742 + sizeof(struct ctdb_tcp_connection)
3743 * list->tickles.num) {
3744 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3748 DEBUG(DEBUG_INFO, ("Received tickle update for public address %s\n",
3749 ctdb_addr_to_str(&list->addr)));
3751 vnn = find_public_ip_vnn(ctdb, &list->addr);
3753 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
3754 ctdb_addr_to_str(&list->addr)));
3759 /* remove any old ticklelist we might have */
3760 talloc_free(vnn->tcp_array);
3761 vnn->tcp_array = NULL;
3763 tcparray = talloc(vnn, struct ctdb_tcp_array);
3764 CTDB_NO_MEMORY(ctdb, tcparray);
3766 tcparray->num = list->tickles.num;
3768 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
3769 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3771 memcpy(tcparray->connections, &list->tickles.connections[0],
3772 sizeof(struct ctdb_tcp_connection)*tcparray->num);
3774 /* We now have a new fresh tickle list array for this vnn */
3775 vnn->tcp_array = tcparray;
3781 called to return the full list of tickles for the puclic address associated
3782 with the provided vnn
3784 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
3786 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
3787 struct ctdb_control_tcp_tickle_list *list;
3788 struct ctdb_tcp_array *tcparray;
3790 struct ctdb_vnn *vnn;
3792 vnn = find_public_ip_vnn(ctdb, addr);
3794 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
3795 ctdb_addr_to_str(addr)));
3800 tcparray = vnn->tcp_array;
3802 num = tcparray->num;
3807 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3808 tickles.connections)
3809 + sizeof(struct ctdb_tcp_connection) * num;
3811 outdata->dptr = talloc_size(outdata, outdata->dsize);
3812 CTDB_NO_MEMORY(ctdb, outdata->dptr);
3813 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
3816 list->tickles.num = num;
3818 memcpy(&list->tickles.connections[0], tcparray->connections,
3819 sizeof(struct ctdb_tcp_connection) * num);
3827 set the list of all tcp tickles for a public address
3829 static int ctdb_send_set_tcp_tickles_for_ip(struct ctdb_context *ctdb,
3830 ctdb_sock_addr *addr,
3831 struct ctdb_tcp_array *tcparray)
3835 struct ctdb_control_tcp_tickle_list *list;
3838 num = tcparray->num;
3843 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3844 tickles.connections) +
3845 sizeof(struct ctdb_tcp_connection) * num;
3846 data.dptr = talloc_size(ctdb, data.dsize);
3847 CTDB_NO_MEMORY(ctdb, data.dptr);
3849 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
3851 list->tickles.num = num;
3853 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
3856 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL, 0,
3857 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
3858 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
3860 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
3864 talloc_free(data.dptr);
3871 perform tickle updates if required
3873 static void ctdb_update_tcp_tickles(struct event_context *ev,
3874 struct timed_event *te,
3875 struct timeval t, void *private_data)
3877 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
3879 struct ctdb_vnn *vnn;
3881 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3882 /* we only send out updates for public addresses that
3885 if (ctdb->pnn != vnn->pnn) {
3888 /* We only send out the updates if we need to */
3889 if (!vnn->tcp_update_needed) {
3892 ret = ctdb_send_set_tcp_tickles_for_ip(ctdb,
3893 &vnn->public_address,
3896 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
3897 ctdb_addr_to_str(&vnn->public_address)));
3900 ("Sent tickle update for public address %s\n",
3901 ctdb_addr_to_str(&vnn->public_address)));
3902 vnn->tcp_update_needed = false;
3906 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
3907 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
3908 ctdb_update_tcp_tickles, ctdb);
3913 start periodic update of tcp tickles
3915 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
3917 ctdb->tickle_update_context = talloc_new(ctdb);
3919 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
3920 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
3921 ctdb_update_tcp_tickles, ctdb);
3927 struct control_gratious_arp {
3928 struct ctdb_context *ctdb;
3929 ctdb_sock_addr addr;
3935 send a control_gratuitous arp
3937 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
3938 struct timeval t, void *private_data)
3941 struct control_gratious_arp *arp = talloc_get_type(private_data,
3942 struct control_gratious_arp);
3944 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
3946 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp on iface '%s' failed (%s)\n",
3947 arp->iface, strerror(errno)));
3952 if (arp->count == CTDB_ARP_REPEAT) {
3957 event_add_timed(arp->ctdb->ev, arp,
3958 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
3959 send_gratious_arp, arp);
3966 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
3968 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
3969 struct control_gratious_arp *arp;
3971 /* verify the size of indata */
3972 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
3973 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
3974 (unsigned)indata.dsize,
3975 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
3979 ( offsetof(struct ctdb_control_gratious_arp, iface)
3980 + gratious_arp->len ) ){
3982 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
3983 "but should be %u bytes\n",
3984 (unsigned)indata.dsize,
3985 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
3990 arp = talloc(ctdb, struct control_gratious_arp);
3991 CTDB_NO_MEMORY(ctdb, arp);
3994 arp->addr = gratious_arp->addr;
3995 arp->iface = talloc_strdup(arp, gratious_arp->iface);
3996 CTDB_NO_MEMORY(ctdb, arp->iface);
3999 event_add_timed(arp->ctdb->ev, arp,
4000 timeval_zero(), send_gratious_arp, arp);
4005 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
4007 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4010 /* verify the size of indata */
4011 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4012 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4016 ( offsetof(struct ctdb_control_ip_iface, iface)
4019 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4020 "but should be %u bytes\n",
4021 (unsigned)indata.dsize,
4022 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4026 DEBUG(DEBUG_NOTICE,("Add IP %s\n", ctdb_addr_to_str(&pub->addr)));
4028 ret = ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0], true);
4031 DEBUG(DEBUG_ERR,(__location__ " Failed to add public address\n"));
4038 struct delete_ip_callback_state {
4039 struct ctdb_req_control *c;
4043 called when releaseip event finishes for del_public_address
4045 static void delete_ip_callback(struct ctdb_context *ctdb,
4046 int32_t status, TDB_DATA data,
4047 const char *errormsg,
4050 struct delete_ip_callback_state *state =
4051 talloc_get_type(private_data, struct delete_ip_callback_state);
4053 /* If release failed then fail. */
4054 ctdb_request_control_reply(ctdb, state->c, NULL, status, errormsg);
4055 talloc_free(private_data);
4058 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb,
4059 struct ctdb_req_control *c,
4060 TDB_DATA indata, bool *async_reply)
4062 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4063 struct ctdb_vnn *vnn;
4065 /* verify the size of indata */
4066 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4067 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4071 ( offsetof(struct ctdb_control_ip_iface, iface)
4074 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4075 "but should be %u bytes\n",
4076 (unsigned)indata.dsize,
4077 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4081 DEBUG(DEBUG_NOTICE,("Delete IP %s\n", ctdb_addr_to_str(&pub->addr)));
4083 /* walk over all public addresses until we find a match */
4084 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4085 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
4086 if (vnn->pnn == ctdb->pnn) {
4087 struct delete_ip_callback_state *state;
4088 struct ctdb_public_ip *ip;
4092 vnn->delete_pending = true;
4094 state = talloc(ctdb,
4095 struct delete_ip_callback_state);
4096 CTDB_NO_MEMORY(ctdb, state);
4099 ip = talloc(state, struct ctdb_public_ip);
4102 (__location__ " Out of memory\n"));
4107 ip->addr = pub->addr;
4109 data.dsize = sizeof(struct ctdb_public_ip);
4110 data.dptr = (unsigned char *)ip;
4112 ret = ctdb_daemon_send_control(ctdb,
4115 CTDB_CONTROL_RELEASE_IP,
4122 (__location__ "Unable to send "
4123 "CTDB_CONTROL_RELEASE_IP\n"));
4128 state->c = talloc_steal(state, c);
4129 *async_reply = true;
4131 /* This IP is not hosted on the
4132 * current node so just delete it
4134 do_delete_ip(ctdb, vnn);
4141 DEBUG(DEBUG_ERR,("Delete IP of unknown public IP address %s\n",
4142 ctdb_addr_to_str(&pub->addr)));
4147 struct ipreallocated_callback_state {
4148 struct ctdb_req_control *c;
4151 static void ctdb_ipreallocated_callback(struct ctdb_context *ctdb,
4152 int status, void *p)
4154 struct ipreallocated_callback_state *state =
4155 talloc_get_type(p, struct ipreallocated_callback_state);
4159 (" \"ipreallocated\" event script failed (status %d)\n",
4161 if (status == -ETIME) {
4162 ctdb_ban_self(ctdb);
4166 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
4170 /* A control to run the ipreallocated event */
4171 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb,
4172 struct ctdb_req_control *c,
4176 struct ipreallocated_callback_state *state;
4178 state = talloc(ctdb, struct ipreallocated_callback_state);
4179 CTDB_NO_MEMORY(ctdb, state);
4181 DEBUG(DEBUG_INFO,(__location__ " Running \"ipreallocated\" event\n"));
4183 ret = ctdb_event_script_callback(ctdb, state,
4184 ctdb_ipreallocated_callback, state,
4185 CTDB_EVENT_IPREALLOCATED,
4189 DEBUG(DEBUG_ERR,("Failed to run \"ipreallocated\" event \n"));
4194 /* tell the control that we will be reply asynchronously */
4195 state->c = talloc_steal(state, c);
4196 *async_reply = true;
4202 /* This function is called from the recovery daemon to verify that a remote
4203 node has the expected ip allocation.
4204 This is verified against ctdb->ip_tree
4206 int verify_remote_ip_allocation(struct ctdb_context *ctdb,
4207 struct ctdb_all_public_ips *ips,
4210 struct ctdb_public_ip_list *tmp_ip;
4213 if (ctdb->ip_tree == NULL) {
4214 /* dont know the expected allocation yet, assume remote node
4223 for (i=0; i<ips->num; i++) {
4224 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ips->ips[i].addr));
4225 if (tmp_ip == NULL) {
4226 DEBUG(DEBUG_ERR,("Node %u has new or unknown public IP %s\n", pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
4230 if (tmp_ip->pnn == -1 || ips->ips[i].pnn == -1) {
4234 if (tmp_ip->pnn != ips->ips[i].pnn) {
4236 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4238 ctdb_addr_to_str(&ips->ips[i].addr),
4239 ips->ips[i].pnn, tmp_ip->pnn));
4247 int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
4249 struct ctdb_public_ip_list *tmp_ip;
4251 if (ctdb->ip_tree == NULL) {
4252 DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4256 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
4257 if (tmp_ip == NULL) {
4258 DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
4262 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));
4263 tmp_ip->pnn = ip->pnn;
4269 struct ctdb_reloadips_handle {
4270 struct ctdb_context *ctdb;
4271 struct ctdb_req_control *c;
4275 struct fd_event *fde;
4278 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle *h)
4280 if (h == h->ctdb->reload_ips) {
4281 h->ctdb->reload_ips = NULL;
4284 ctdb_request_control_reply(h->ctdb, h->c, NULL, h->status, NULL);
4287 ctdb_kill(h->ctdb, h->child, SIGKILL);
4291 static void ctdb_reloadips_timeout_event(struct event_context *ev,
4292 struct timed_event *te,
4293 struct timeval t, void *private_data)
4295 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4300 static void ctdb_reloadips_child_handler(struct event_context *ev, struct fd_event *fde,
4301 uint16_t flags, void *private_data)
4303 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4308 ret = sys_read(h->fd[0], &res, 1);
4309 if (ret < 1 || res != 0) {
4310 DEBUG(DEBUG_ERR, (__location__ " Reloadips child process returned error\n"));
4318 static int ctdb_reloadips_child(struct ctdb_context *ctdb)
4320 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4321 struct ctdb_all_public_ips *ips;
4322 struct ctdb_vnn *vnn;
4323 struct client_async_data *async_data;
4324 struct timeval timeout;
4326 struct ctdb_client_control_state *state;
4330 CTDB_NO_MEMORY(ctdb, mem_ctx);
4332 /* Read IPs from local node */
4333 ret = ctdb_ctrl_get_public_ips(ctdb, TAKEOVER_TIMEOUT(),
4334 CTDB_CURRENT_NODE, mem_ctx, &ips);
4337 ("Unable to fetch public IPs from local node\n"));
4338 talloc_free(mem_ctx);
4342 /* Read IPs file - this is safe since this is a child process */
4344 if (ctdb_set_public_addresses(ctdb, false) != 0) {
4345 DEBUG(DEBUG_ERR,("Failed to re-read public addresses file\n"));
4346 talloc_free(mem_ctx);
4350 async_data = talloc_zero(mem_ctx, struct client_async_data);
4351 CTDB_NO_MEMORY(ctdb, async_data);
4353 /* Compare IPs between node and file for IPs to be deleted */
4354 for (i = 0; i < ips->num; i++) {
4356 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4357 if (ctdb_same_ip(&vnn->public_address,
4358 &ips->ips[i].addr)) {
4359 /* IP is still in file */
4365 /* Delete IP ips->ips[i] */
4366 struct ctdb_control_ip_iface *pub;
4369 ("IP %s no longer configured, deleting it\n",
4370 ctdb_addr_to_str(&ips->ips[i].addr)));
4372 pub = talloc_zero(mem_ctx,
4373 struct ctdb_control_ip_iface);
4374 CTDB_NO_MEMORY(ctdb, pub);
4376 pub->addr = ips->ips[i].addr;
4380 timeout = TAKEOVER_TIMEOUT();
4382 data.dsize = offsetof(struct ctdb_control_ip_iface,
4384 data.dptr = (uint8_t *)pub;
4386 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4387 CTDB_CONTROL_DEL_PUBLIC_IP,
4388 0, data, async_data,
4390 if (state == NULL) {
4393 " failed sending CTDB_CONTROL_DEL_PUBLIC_IP\n"));
4397 ctdb_client_async_add(async_data, state);
4401 /* Compare IPs between node and file for IPs to be added */
4403 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4404 for (i = 0; i < ips->num; i++) {
4405 if (ctdb_same_ip(&vnn->public_address,
4406 &ips->ips[i].addr)) {
4407 /* IP already on node */
4411 if (i == ips->num) {
4412 /* Add IP ips->ips[i] */
4413 struct ctdb_control_ip_iface *pub;
4414 const char *ifaces = NULL;
4419 ("New IP %s configured, adding it\n",
4420 ctdb_addr_to_str(&vnn->public_address)));
4422 uint32_t pnn = ctdb_get_pnn(ctdb);
4424 data.dsize = sizeof(pnn);
4425 data.dptr = (uint8_t *)&pnn;
4427 ret = ctdb_client_send_message(
4429 CTDB_BROADCAST_CONNECTED,
4430 CTDB_SRVID_REBALANCE_NODE,
4433 DEBUG(DEBUG_WARNING,
4434 ("Failed to send message to force node reallocation - IPs may be unbalanced\n"));
4440 ifaces = vnn->ifaces[0];
4442 while (vnn->ifaces[iface] != NULL) {
4443 ifaces = talloc_asprintf(vnn, "%s,%s", ifaces,
4444 vnn->ifaces[iface]);
4448 len = strlen(ifaces) + 1;
4449 pub = talloc_zero_size(mem_ctx,
4450 offsetof(struct ctdb_control_ip_iface, iface) + len);
4451 CTDB_NO_MEMORY(ctdb, pub);
4453 pub->addr = vnn->public_address;
4454 pub->mask = vnn->public_netmask_bits;
4456 memcpy(&pub->iface[0], ifaces, pub->len);
4458 timeout = TAKEOVER_TIMEOUT();
4460 data.dsize = offsetof(struct ctdb_control_ip_iface,
4462 data.dptr = (uint8_t *)pub;
4464 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4465 CTDB_CONTROL_ADD_PUBLIC_IP,
4466 0, data, async_data,
4468 if (state == NULL) {
4471 " failed sending CTDB_CONTROL_ADD_PUBLIC_IP\n"));
4475 ctdb_client_async_add(async_data, state);
4479 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
4480 DEBUG(DEBUG_ERR,(__location__ " Add/delete IPs failed\n"));
4484 talloc_free(mem_ctx);
4488 talloc_free(mem_ctx);
4492 /* This control is sent to force the node to re-read the public addresses file
4493 and drop any addresses we should nnot longer host, and add new addresses
4494 that we are now able to host
4496 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
4498 struct ctdb_reloadips_handle *h;
4499 pid_t parent = getpid();
4501 if (ctdb->reload_ips != NULL) {
4502 talloc_free(ctdb->reload_ips);
4503 ctdb->reload_ips = NULL;
4506 h = talloc(ctdb, struct ctdb_reloadips_handle);
4507 CTDB_NO_MEMORY(ctdb, h);
4512 if (pipe(h->fd) == -1) {
4513 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
4518 h->child = ctdb_fork(ctdb);
4519 if (h->child == (pid_t)-1) {
4520 DEBUG(DEBUG_ERR, ("Failed to fork a child for reloadips\n"));
4528 if (h->child == 0) {
4529 signed char res = 0;
4532 debug_extra = talloc_asprintf(NULL, "reloadips:");
4534 ctdb_set_process_name("ctdb_reloadips");
4535 if (switch_from_server_to_client(ctdb, "reloadips-child") != 0) {
4536 DEBUG(DEBUG_CRIT,("ERROR: Failed to switch reloadips child into client mode\n"));
4539 res = ctdb_reloadips_child(ctdb);
4541 DEBUG(DEBUG_ERR,("Failed to reload ips on local node\n"));
4545 sys_write(h->fd[1], &res, 1);
4546 /* make sure we die when our parent dies */
4547 while (ctdb_kill(ctdb, parent, 0) == 0 || errno != ESRCH) {
4553 h->c = talloc_steal(h, c);
4556 set_close_on_exec(h->fd[0]);
4558 talloc_set_destructor(h, ctdb_reloadips_destructor);
4561 h->fde = event_add_fd(ctdb->ev, h, h->fd[0],
4562 EVENT_FD_READ, ctdb_reloadips_child_handler,
4564 tevent_fd_set_auto_close(h->fde);
4566 event_add_timed(ctdb->ev, h,
4567 timeval_current_ofs(120, 0),
4568 ctdb_reloadips_timeout_event, h);
4570 /* we reply later */
4571 *async_reply = true;