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/>.
22 #include "lib/tdb/include/tdb.h"
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
37 struct ctdb_iface *prev, *next;
43 static const char *ctdb_vnn_iface_string(const struct ctdb_vnn *vnn)
46 return vnn->iface->name;
52 static int ctdb_add_local_iface(struct ctdb_context *ctdb, const char *iface)
56 /* Verify that we dont have an entry for this ip yet */
57 for (i=ctdb->ifaces;i;i=i->next) {
58 if (strcmp(i->name, iface) == 0) {
63 /* create a new structure for this interface */
64 i = talloc_zero(ctdb, struct ctdb_iface);
65 CTDB_NO_MEMORY_FATAL(ctdb, i);
66 i->name = talloc_strdup(i, iface);
67 CTDB_NO_MEMORY(ctdb, i->name);
69 * If link_up defaults to true then IPs can be allocated to a
70 * node during the first recovery. However, then an interface
71 * could have its link marked down during the startup event,
72 * causing the IP to move almost immediately. If link_up
73 * defaults to false then, during normal operation, IPs added
74 * to a new interface can't be assigned until a monitor cycle
75 * has occurred and marked the new interfaces up. This makes
76 * IP allocation unpredictable. The following is a neat
77 * compromise: early in startup link_up defaults to false, so
78 * IPs can't be assigned, and after startup IPs can be
79 * assigned immediately.
81 i->link_up = ctdb->done_startup;
83 DLIST_ADD(ctdb->ifaces, i);
88 static bool vnn_has_interface_with_name(struct ctdb_vnn *vnn,
93 for (n = 0; vnn->ifaces[n] != NULL; n++) {
94 if (strcmp(name, vnn->ifaces[n]) == 0) {
102 /* If any interfaces now have no possible IPs then delete them. This
103 * implementation is naive (i.e. simple) rather than clever
104 * (i.e. complex). Given that this is run on delip and that operation
105 * is rare, this doesn't need to be efficient - it needs to be
106 * foolproof. One alternative is reference counting, where the logic
107 * is distributed and can, therefore, be broken in multiple places.
108 * Another alternative is to build a red-black tree of interfaces that
109 * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
110 * once) and then walking ctdb->ifaces once and deleting those not in
111 * the tree. Let's go to one of those if the naive implementation
112 * causes problems... :-)
114 static void ctdb_remove_orphaned_ifaces(struct ctdb_context *ctdb,
115 struct ctdb_vnn *vnn,
118 struct ctdb_iface *i;
120 /* For each interface, check if there's an IP using it. */
121 for(i=ctdb->ifaces; i; i=i->next) {
125 /* Only consider interfaces named in the given VNN. */
126 if (!vnn_has_interface_with_name(vnn, i->name)) {
130 /* Is the "single IP" on this interface? */
131 if ((ctdb->single_ip_vnn != NULL) &&
132 (ctdb->single_ip_vnn->ifaces[0] != NULL) &&
133 (strcmp(i->name, ctdb->single_ip_vnn->ifaces[0]) == 0)) {
134 /* Found, next interface please... */
137 /* Search for a vnn with this interface. */
139 for (tv=ctdb->vnn; tv; tv=tv->next) {
140 if (vnn_has_interface_with_name(tv, i->name)) {
147 /* None of the VNNs are using this interface. */
148 DLIST_REMOVE(ctdb->ifaces, i);
149 /* Caller will free mem_ctx when convenient. */
150 talloc_steal(mem_ctx, i);
156 static struct ctdb_iface *ctdb_find_iface(struct ctdb_context *ctdb,
159 struct ctdb_iface *i;
161 /* Verify that we dont have an entry for this ip yet */
162 for (i=ctdb->ifaces;i;i=i->next) {
163 if (strcmp(i->name, iface) == 0) {
171 static struct ctdb_iface *ctdb_vnn_best_iface(struct ctdb_context *ctdb,
172 struct ctdb_vnn *vnn)
175 struct ctdb_iface *cur = NULL;
176 struct ctdb_iface *best = NULL;
178 for (i=0; vnn->ifaces[i]; i++) {
180 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
194 if (cur->references < best->references) {
203 static int32_t ctdb_vnn_assign_iface(struct ctdb_context *ctdb,
204 struct ctdb_vnn *vnn)
206 struct ctdb_iface *best = NULL;
209 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
210 "still assigned to iface '%s'\n",
211 ctdb_addr_to_str(&vnn->public_address),
212 ctdb_vnn_iface_string(vnn)));
216 best = ctdb_vnn_best_iface(ctdb, vnn);
218 DEBUG(DEBUG_ERR, (__location__ " public address '%s' "
219 "cannot assign to iface any iface\n",
220 ctdb_addr_to_str(&vnn->public_address)));
226 vnn->pnn = ctdb->pnn;
228 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
229 "now assigned to iface '%s' refs[%d]\n",
230 ctdb_addr_to_str(&vnn->public_address),
231 ctdb_vnn_iface_string(vnn),
236 static void ctdb_vnn_unassign_iface(struct ctdb_context *ctdb,
237 struct ctdb_vnn *vnn)
239 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
240 "now unassigned (old iface '%s' refs[%d])\n",
241 ctdb_addr_to_str(&vnn->public_address),
242 ctdb_vnn_iface_string(vnn),
243 vnn->iface?vnn->iface->references:0));
245 vnn->iface->references--;
248 if (vnn->pnn == ctdb->pnn) {
253 static bool ctdb_vnn_available(struct ctdb_context *ctdb,
254 struct ctdb_vnn *vnn)
258 if (vnn->iface && vnn->iface->link_up) {
262 for (i=0; vnn->ifaces[i]; i++) {
263 struct ctdb_iface *cur;
265 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
278 struct ctdb_takeover_arp {
279 struct ctdb_context *ctdb;
282 struct ctdb_tcp_array *tcparray;
283 struct ctdb_vnn *vnn;
288 lists of tcp endpoints
290 struct ctdb_tcp_list {
291 struct ctdb_tcp_list *prev, *next;
292 struct ctdb_tcp_connection connection;
296 list of clients to kill on IP release
298 struct ctdb_client_ip {
299 struct ctdb_client_ip *prev, *next;
300 struct ctdb_context *ctdb;
307 send a gratuitous arp
309 static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te,
310 struct timeval t, void *private_data)
312 struct ctdb_takeover_arp *arp = talloc_get_type(private_data,
313 struct ctdb_takeover_arp);
315 struct ctdb_tcp_array *tcparray;
316 const char *iface = ctdb_vnn_iface_string(arp->vnn);
318 ret = ctdb_sys_send_arp(&arp->addr, iface);
320 DEBUG(DEBUG_CRIT,(__location__ " sending of arp failed on iface '%s' (%s)\n",
321 iface, strerror(errno)));
324 tcparray = arp->tcparray;
326 for (i=0;i<tcparray->num;i++) {
327 struct ctdb_tcp_connection *tcon;
329 tcon = &tcparray->connections[i];
330 DEBUG(DEBUG_INFO,("sending tcp tickle ack for %u->%s:%u\n",
331 (unsigned)ntohs(tcon->dst_addr.ip.sin_port),
332 ctdb_addr_to_str(&tcon->src_addr),
333 (unsigned)ntohs(tcon->src_addr.ip.sin_port)));
334 ret = ctdb_sys_send_tcp(
339 DEBUG(DEBUG_CRIT,(__location__ " Failed to send tcp tickle ack for %s\n",
340 ctdb_addr_to_str(&tcon->src_addr)));
347 if (arp->count == CTDB_ARP_REPEAT) {
352 event_add_timed(arp->ctdb->ev, arp->vnn->takeover_ctx,
353 timeval_current_ofs(CTDB_ARP_INTERVAL, 100000),
354 ctdb_control_send_arp, arp);
357 static int32_t ctdb_announce_vnn_iface(struct ctdb_context *ctdb,
358 struct ctdb_vnn *vnn)
360 struct ctdb_takeover_arp *arp;
361 struct ctdb_tcp_array *tcparray;
363 if (!vnn->takeover_ctx) {
364 vnn->takeover_ctx = talloc_new(vnn);
365 if (!vnn->takeover_ctx) {
370 arp = talloc_zero(vnn->takeover_ctx, struct ctdb_takeover_arp);
376 arp->addr = vnn->public_address;
379 tcparray = vnn->tcp_array;
381 /* add all of the known tcp connections for this IP to the
382 list of tcp connections to send tickle acks for */
383 arp->tcparray = talloc_steal(arp, tcparray);
385 vnn->tcp_array = NULL;
386 vnn->tcp_update_needed = true;
389 event_add_timed(arp->ctdb->ev, vnn->takeover_ctx,
390 timeval_zero(), ctdb_control_send_arp, arp);
395 struct takeover_callback_state {
396 struct ctdb_req_control *c;
397 ctdb_sock_addr *addr;
398 struct ctdb_vnn *vnn;
401 struct ctdb_do_takeip_state {
402 struct ctdb_req_control *c;
403 struct ctdb_vnn *vnn;
407 called when takeip event finishes
409 static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
412 struct ctdb_do_takeip_state *state =
413 talloc_get_type(private_data, struct ctdb_do_takeip_state);
418 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
420 if (status == -ETIME) {
423 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
424 ctdb_addr_to_str(&state->vnn->public_address),
425 ctdb_vnn_iface_string(state->vnn)));
426 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
428 node->flags |= NODE_FLAGS_UNHEALTHY;
433 if (ctdb->do_checkpublicip) {
435 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
437 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
444 data.dptr = (uint8_t *)ctdb_addr_to_str(&state->vnn->public_address);
445 data.dsize = strlen((char *)data.dptr) + 1;
446 DEBUG(DEBUG_INFO,(__location__ " sending TAKE_IP for '%s'\n", data.dptr));
448 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_TAKE_IP, data);
451 /* the control succeeded */
452 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
457 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state *state)
459 state->vnn->update_in_flight = false;
464 take over an ip address
466 static int32_t ctdb_do_takeip(struct ctdb_context *ctdb,
467 struct ctdb_req_control *c,
468 struct ctdb_vnn *vnn)
471 struct ctdb_do_takeip_state *state;
473 if (vnn->update_in_flight) {
474 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u rejected "
475 "update for this IP already in flight\n",
476 ctdb_addr_to_str(&vnn->public_address),
477 vnn->public_netmask_bits));
481 ret = ctdb_vnn_assign_iface(ctdb, vnn);
483 DEBUG(DEBUG_ERR,("Takeover of IP %s/%u failed to "
484 "assign a usable interface\n",
485 ctdb_addr_to_str(&vnn->public_address),
486 vnn->public_netmask_bits));
490 state = talloc(vnn, struct ctdb_do_takeip_state);
491 CTDB_NO_MEMORY(ctdb, state);
493 state->c = talloc_steal(ctdb, c);
496 vnn->update_in_flight = true;
497 talloc_set_destructor(state, ctdb_takeip_destructor);
499 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u on interface %s\n",
500 ctdb_addr_to_str(&vnn->public_address),
501 vnn->public_netmask_bits,
502 ctdb_vnn_iface_string(vnn)));
504 ret = ctdb_event_script_callback(ctdb,
506 ctdb_do_takeip_callback,
511 ctdb_vnn_iface_string(vnn),
512 ctdb_addr_to_str(&vnn->public_address),
513 vnn->public_netmask_bits);
516 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
517 ctdb_addr_to_str(&vnn->public_address),
518 ctdb_vnn_iface_string(vnn)));
526 struct ctdb_do_updateip_state {
527 struct ctdb_req_control *c;
528 struct ctdb_iface *old;
529 struct ctdb_vnn *vnn;
533 called when updateip event finishes
535 static void ctdb_do_updateip_callback(struct ctdb_context *ctdb, int status,
538 struct ctdb_do_updateip_state *state =
539 talloc_get_type(private_data, struct ctdb_do_updateip_state);
543 if (status == -ETIME) {
546 DEBUG(DEBUG_ERR,(__location__ " Failed to move IP %s from interface %s to %s\n",
547 ctdb_addr_to_str(&state->vnn->public_address),
549 ctdb_vnn_iface_string(state->vnn)));
552 * All we can do is reset the old interface
553 * and let the next run fix it
555 ctdb_vnn_unassign_iface(ctdb, state->vnn);
556 state->vnn->iface = state->old;
557 state->vnn->iface->references++;
559 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
564 if (ctdb->do_checkpublicip) {
566 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
568 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
575 /* the control succeeded */
576 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
581 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state *state)
583 state->vnn->update_in_flight = false;
588 update (move) an ip address
590 static int32_t ctdb_do_updateip(struct ctdb_context *ctdb,
591 struct ctdb_req_control *c,
592 struct ctdb_vnn *vnn)
595 struct ctdb_do_updateip_state *state;
596 struct ctdb_iface *old = vnn->iface;
597 const char *new_name;
599 if (vnn->update_in_flight) {
600 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u rejected "
601 "update for this IP already in flight\n",
602 ctdb_addr_to_str(&vnn->public_address),
603 vnn->public_netmask_bits));
607 ctdb_vnn_unassign_iface(ctdb, vnn);
608 ret = ctdb_vnn_assign_iface(ctdb, vnn);
610 DEBUG(DEBUG_ERR,("update of IP %s/%u failed to "
611 "assin a usable interface (old iface '%s')\n",
612 ctdb_addr_to_str(&vnn->public_address),
613 vnn->public_netmask_bits,
618 new_name = ctdb_vnn_iface_string(vnn);
619 if (old->name != NULL && new_name != NULL && !strcmp(old->name, new_name)) {
620 /* A benign update from one interface onto itself.
621 * no need to run the eventscripts in this case, just return
624 ctdb_request_control_reply(ctdb, c, NULL, 0, NULL);
628 state = talloc(vnn, struct ctdb_do_updateip_state);
629 CTDB_NO_MEMORY(ctdb, state);
631 state->c = talloc_steal(ctdb, c);
635 vnn->update_in_flight = true;
636 talloc_set_destructor(state, ctdb_updateip_destructor);
638 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u from "
639 "interface %s to %s\n",
640 ctdb_addr_to_str(&vnn->public_address),
641 vnn->public_netmask_bits,
645 ret = ctdb_event_script_callback(ctdb,
647 ctdb_do_updateip_callback,
650 CTDB_EVENT_UPDATE_IP,
654 ctdb_addr_to_str(&vnn->public_address),
655 vnn->public_netmask_bits);
657 DEBUG(DEBUG_ERR,(__location__ " Failed update IP %s from interface %s to %s\n",
658 ctdb_addr_to_str(&vnn->public_address),
659 old->name, new_name));
668 Find the vnn of the node that has a public ip address
669 returns -1 if the address is not known as a public address
671 static struct ctdb_vnn *find_public_ip_vnn(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
673 struct ctdb_vnn *vnn;
675 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
676 if (ctdb_same_ip(&vnn->public_address, addr)) {
685 take over an ip address
687 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
688 struct ctdb_req_control *c,
693 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
694 struct ctdb_vnn *vnn;
695 bool have_ip = false;
696 bool do_updateip = false;
697 bool do_takeip = false;
698 struct ctdb_iface *best_iface = NULL;
700 if (pip->pnn != ctdb->pnn) {
701 DEBUG(DEBUG_ERR,(__location__" takeoverip called for an ip '%s' "
702 "with pnn %d, but we're node %d\n",
703 ctdb_addr_to_str(&pip->addr),
704 pip->pnn, ctdb->pnn));
708 /* update out vnn list */
709 vnn = find_public_ip_vnn(ctdb, &pip->addr);
711 DEBUG(DEBUG_INFO,("takeoverip called for an ip '%s' that is not a public address\n",
712 ctdb_addr_to_str(&pip->addr)));
716 if (ctdb->do_checkpublicip) {
717 have_ip = ctdb_sys_have_ip(&pip->addr);
719 best_iface = ctdb_vnn_best_iface(ctdb, vnn);
720 if (best_iface == NULL) {
721 DEBUG(DEBUG_ERR,("takeoverip of IP %s/%u failed to find"
722 "a usable interface (old %s, have_ip %d)\n",
723 ctdb_addr_to_str(&vnn->public_address),
724 vnn->public_netmask_bits,
725 ctdb_vnn_iface_string(vnn),
730 if (vnn->iface == NULL && vnn->pnn == -1 && have_ip && best_iface != NULL) {
731 DEBUG(DEBUG_ERR,("Taking over newly created ip\n"));
736 if (vnn->iface == NULL && have_ip) {
737 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
738 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
739 ctdb_addr_to_str(&vnn->public_address)));
743 if (vnn->pnn != ctdb->pnn && have_ip && vnn->pnn != -1) {
744 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
745 "and we have it on iface[%s], but it was assigned to node %d"
746 "and we are node %d, banning ourself\n",
747 ctdb_addr_to_str(&vnn->public_address),
748 ctdb_vnn_iface_string(vnn), vnn->pnn, ctdb->pnn));
753 if (vnn->pnn == -1 && have_ip) {
754 vnn->pnn = ctdb->pnn;
755 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
756 "and we already have it on iface[%s], update local daemon\n",
757 ctdb_addr_to_str(&vnn->public_address),
758 ctdb_vnn_iface_string(vnn)));
763 if (vnn->iface != best_iface) {
764 if (!vnn->iface->link_up) {
766 } else if (vnn->iface->references > (best_iface->references + 1)) {
767 /* only move when the rebalance gains something */
775 ctdb_vnn_unassign_iface(ctdb, vnn);
782 ret = ctdb_do_takeip(ctdb, c, vnn);
786 } else if (do_updateip) {
787 ret = ctdb_do_updateip(ctdb, c, vnn);
793 * The interface is up and the kernel known the ip
796 DEBUG(DEBUG_INFO,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
797 ctdb_addr_to_str(&pip->addr),
798 vnn->public_netmask_bits,
799 ctdb_vnn_iface_string(vnn)));
803 /* tell ctdb_control.c that we will be replying asynchronously */
810 takeover an ip address old v4 style
812 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb,
813 struct ctdb_req_control *c,
819 data.dsize = sizeof(struct ctdb_public_ip);
820 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
821 CTDB_NO_MEMORY(ctdb, data.dptr);
823 memcpy(data.dptr, indata.dptr, indata.dsize);
824 return ctdb_control_takeover_ip(ctdb, c, data, async_reply);
828 kill any clients that are registered with a IP that is being released
830 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
832 struct ctdb_client_ip *ip;
834 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
835 ctdb_addr_to_str(addr)));
837 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
838 ctdb_sock_addr tmp_addr;
841 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
843 ctdb_addr_to_str(&ip->addr)));
845 if (ctdb_same_ip(&tmp_addr, addr)) {
846 struct ctdb_client *client = ctdb_reqid_find(ctdb,
849 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
851 ctdb_addr_to_str(&ip->addr),
854 if (client->pid != 0) {
855 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
856 (unsigned)client->pid,
857 ctdb_addr_to_str(addr),
859 ctdb_kill(ctdb, client->pid, SIGKILL);
866 called when releaseip event finishes
868 static void release_ip_callback(struct ctdb_context *ctdb, int status,
871 struct takeover_callback_state *state =
872 talloc_get_type(private_data, struct takeover_callback_state);
875 if (status == -ETIME) {
879 /* send a message to all clients of this node telling them
880 that the cluster has been reconfigured and they should
881 release any sockets on this IP */
882 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
883 CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
884 data.dsize = strlen((char *)data.dptr)+1;
886 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
888 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
890 /* kill clients that have registered with this IP */
891 release_kill_clients(ctdb, state->addr);
893 ctdb_vnn_unassign_iface(ctdb, state->vnn);
895 /* the control succeeded */
896 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
900 static int ctdb_releaseip_destructor(struct takeover_callback_state *state)
902 state->vnn->update_in_flight = false;
907 release an ip address
909 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
910 struct ctdb_req_control *c,
915 struct takeover_callback_state *state;
916 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
917 struct ctdb_vnn *vnn;
920 /* update our vnn list */
921 vnn = find_public_ip_vnn(ctdb, &pip->addr);
923 DEBUG(DEBUG_INFO,("releaseip called for an ip '%s' that is not a public address\n",
924 ctdb_addr_to_str(&pip->addr)));
929 /* stop any previous arps */
930 talloc_free(vnn->takeover_ctx);
931 vnn->takeover_ctx = NULL;
933 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
934 * lazy multicast to drop an IP from any node that isn't the
935 * intended new node. The following causes makes ctdbd ignore
936 * a release for any address it doesn't host.
938 if (ctdb->do_checkpublicip) {
939 if (!ctdb_sys_have_ip(&pip->addr)) {
940 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
941 ctdb_addr_to_str(&pip->addr),
942 vnn->public_netmask_bits,
943 ctdb_vnn_iface_string(vnn)));
944 ctdb_vnn_unassign_iface(ctdb, vnn);
948 if (vnn->iface == NULL) {
949 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u (ip not held)\n",
950 ctdb_addr_to_str(&pip->addr),
951 vnn->public_netmask_bits));
956 /* There is a potential race between take_ip and us because we
957 * update the VNN via a callback that run when the
958 * eventscripts have been run. Avoid the race by allowing one
959 * update to be in flight at a time.
961 if (vnn->update_in_flight) {
962 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u rejected "
963 "update for this IP already in flight\n",
964 ctdb_addr_to_str(&vnn->public_address),
965 vnn->public_netmask_bits));
969 if (ctdb->do_checkpublicip) {
970 iface = ctdb_sys_find_ifname(&pip->addr);
972 DEBUG(DEBUG_ERR, ("Could not find which interface the ip address is hosted on. can not release it\n"));
976 iface = strdup(ctdb_vnn_iface_string(vnn));
979 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s node:%d\n",
980 ctdb_addr_to_str(&pip->addr),
981 vnn->public_netmask_bits,
985 state = talloc(ctdb, struct takeover_callback_state);
986 CTDB_NO_MEMORY(ctdb, state);
988 state->c = talloc_steal(state, c);
989 state->addr = talloc(state, ctdb_sock_addr);
990 CTDB_NO_MEMORY(ctdb, state->addr);
991 *state->addr = pip->addr;
994 vnn->update_in_flight = true;
995 talloc_set_destructor(state, ctdb_releaseip_destructor);
997 ret = ctdb_event_script_callback(ctdb,
998 state, release_ip_callback, state,
1000 CTDB_EVENT_RELEASE_IP,
1003 ctdb_addr_to_str(&pip->addr),
1004 vnn->public_netmask_bits);
1007 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
1008 ctdb_addr_to_str(&pip->addr),
1009 ctdb_vnn_iface_string(vnn)));
1014 /* tell the control that we will be reply asynchronously */
1015 *async_reply = true;
1020 release an ip address old v4 style
1022 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb,
1023 struct ctdb_req_control *c,
1029 data.dsize = sizeof(struct ctdb_public_ip);
1030 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
1031 CTDB_NO_MEMORY(ctdb, data.dptr);
1033 memcpy(data.dptr, indata.dptr, indata.dsize);
1034 return ctdb_control_release_ip(ctdb, c, data, async_reply);
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 event script directory
1113 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir)
1115 ctdb->event_script_dir = talloc_strdup(ctdb, script_dir);
1116 CTDB_NO_MEMORY(ctdb, ctdb->event_script_dir);
1120 static void ctdb_check_interfaces_event(struct event_context *ev, struct timed_event *te,
1121 struct timeval t, void *private_data)
1123 struct ctdb_context *ctdb = talloc_get_type(private_data,
1124 struct ctdb_context);
1125 struct ctdb_vnn *vnn;
1127 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1130 for (i=0; vnn->ifaces[i] != NULL; i++) {
1131 if (!ctdb_sys_check_iface_exists(vnn->ifaces[i])) {
1132 DEBUG(DEBUG_CRIT,("Interface %s does not exist but is used by public ip %s\n",
1134 ctdb_addr_to_str(&vnn->public_address)));
1139 event_add_timed(ctdb->ev, ctdb->check_public_ifaces_ctx,
1140 timeval_current_ofs(30, 0),
1141 ctdb_check_interfaces_event, ctdb);
1145 int ctdb_start_monitoring_interfaces(struct ctdb_context *ctdb)
1147 if (ctdb->check_public_ifaces_ctx != NULL) {
1148 talloc_free(ctdb->check_public_ifaces_ctx);
1149 ctdb->check_public_ifaces_ctx = NULL;
1152 ctdb->check_public_ifaces_ctx = talloc_new(ctdb);
1153 if (ctdb->check_public_ifaces_ctx == NULL) {
1154 ctdb_fatal(ctdb, "failed to allocate context for checking interfaces");
1157 event_add_timed(ctdb->ev, ctdb->check_public_ifaces_ctx,
1158 timeval_current_ofs(30, 0),
1159 ctdb_check_interfaces_event, ctdb);
1166 setup the public address lists from a file
1168 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
1174 lines = file_lines_load(ctdb->public_addresses_file, &nlines, ctdb);
1175 if (lines == NULL) {
1176 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", ctdb->public_addresses_file);
1179 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1183 for (i=0;i<nlines;i++) {
1185 ctdb_sock_addr addr;
1186 const char *addrstr;
1191 while ((*line == ' ') || (*line == '\t')) {
1197 if (strcmp(line, "") == 0) {
1200 tok = strtok(line, " \t");
1202 tok = strtok(NULL, " \t");
1204 if (NULL == ctdb->default_public_interface) {
1205 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
1210 ifaces = ctdb->default_public_interface;
1215 if (!addrstr || !parse_ip_mask(addrstr, ifaces, &addr, &mask)) {
1216 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
1220 if (ctdb_add_public_address(ctdb, &addr, mask, ifaces, check_addresses)) {
1221 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
1232 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1236 struct ctdb_vnn *svnn;
1237 struct ctdb_iface *cur = NULL;
1241 svnn = talloc_zero(ctdb, struct ctdb_vnn);
1242 CTDB_NO_MEMORY(ctdb, svnn);
1244 svnn->ifaces = talloc_array(svnn, const char *, 2);
1245 CTDB_NO_MEMORY(ctdb, svnn->ifaces);
1246 svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
1247 CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
1248 svnn->ifaces[1] = NULL;
1250 ok = parse_ip(ip, iface, 0, &svnn->public_address);
1256 ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
1258 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1259 "for single_ip[%s]\n",
1261 ctdb_addr_to_str(&svnn->public_address)));
1266 /* assume the single public ip interface is initially "good" */
1267 cur = ctdb_find_iface(ctdb, iface);
1269 DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
1272 cur->link_up = true;
1274 ret = ctdb_vnn_assign_iface(ctdb, svnn);
1280 ctdb->single_ip_vnn = svnn;
1284 /* Given a physical node, return the number of
1285 public addresses that is currently assigned to this node.
1287 static int node_ip_coverage(struct ctdb_context *ctdb,
1289 struct ctdb_public_ip_list *ips)
1293 for (;ips;ips=ips->next) {
1294 if (ips->pnn == pnn) {
1302 /* Check if this is a public ip known to the node, i.e. can that
1303 node takeover this ip ?
1305 static int can_node_serve_ip(struct ctdb_context *ctdb, int32_t pnn,
1306 struct ctdb_public_ip_list *ip)
1308 struct ctdb_all_public_ips *public_ips;
1311 public_ips = ctdb->nodes[pnn]->available_public_ips;
1313 if (public_ips == NULL) {
1317 for (i=0;i<public_ips->num;i++) {
1318 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
1319 /* yes, this node can serve this public ip */
1328 /* search the node lists list for a node to takeover this ip.
1329 pick the node that currently are serving the least number of ips
1330 so that the ips get spread out evenly.
1332 static int find_takeover_node(struct ctdb_context *ctdb,
1333 struct ctdb_node_map *nodemap, uint32_t mask,
1334 struct ctdb_public_ip_list *ip,
1335 struct ctdb_public_ip_list *all_ips)
1337 int pnn, min=0, num;
1341 for (i=0;i<nodemap->num;i++) {
1342 if (nodemap->nodes[i].flags & NODE_FLAGS_NOIPTAKEOVER) {
1343 /* This node is not allowed to takeover any addresses
1348 if (nodemap->nodes[i].flags & mask) {
1349 /* This node is not healty and can not be used to serve
1355 /* verify that this node can serve this ip */
1356 if (can_node_serve_ip(ctdb, i, ip)) {
1357 /* no it couldnt so skip to the next node */
1361 num = node_ip_coverage(ctdb, i, all_ips);
1362 /* was this the first node we checked ? */
1374 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
1375 ctdb_addr_to_str(&ip->addr)));
1385 static uint32_t *ip_key(ctdb_sock_addr *ip)
1387 static uint32_t key[IP_KEYLEN];
1389 bzero(key, sizeof(key));
1391 switch (ip->sa.sa_family) {
1393 key[3] = htonl(ip->ip.sin_addr.s_addr);
1396 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1397 key[0] = htonl(s6_a32[0]);
1398 key[1] = htonl(s6_a32[1]);
1399 key[2] = htonl(s6_a32[2]);
1400 key[3] = htonl(s6_a32[3]);
1404 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1411 static void *add_ip_callback(void *parm, void *data)
1413 struct ctdb_public_ip_list *this_ip = parm;
1414 struct ctdb_public_ip_list *prev_ip = data;
1416 if (prev_ip == NULL) {
1419 if (this_ip->pnn == -1) {
1420 this_ip->pnn = prev_ip->pnn;
1426 static int getips_count_callback(void *param, void *data)
1428 struct ctdb_public_ip_list **ip_list = (struct ctdb_public_ip_list **)param;
1429 struct ctdb_public_ip_list *new_ip = (struct ctdb_public_ip_list *)data;
1431 new_ip->next = *ip_list;
1436 static struct ctdb_public_ip_list *
1437 create_merged_ip_list(struct ctdb_context *ctdb)
1440 struct ctdb_public_ip_list *ip_list;
1441 struct ctdb_all_public_ips *public_ips;
1443 if (ctdb->ip_tree != NULL) {
1444 talloc_free(ctdb->ip_tree);
1445 ctdb->ip_tree = NULL;
1447 ctdb->ip_tree = trbt_create(ctdb, 0);
1449 for (i=0;i<ctdb->num_nodes;i++) {
1450 public_ips = ctdb->nodes[i]->known_public_ips;
1452 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
1456 /* there were no public ips for this node */
1457 if (public_ips == NULL) {
1461 for (j=0;j<public_ips->num;j++) {
1462 struct ctdb_public_ip_list *tmp_ip;
1464 tmp_ip = talloc_zero(ctdb->ip_tree, struct ctdb_public_ip_list);
1465 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
1466 tmp_ip->pnn = public_ips->ips[j].pnn;
1467 tmp_ip->addr = public_ips->ips[j].addr;
1468 tmp_ip->next = NULL;
1470 trbt_insertarray32_callback(ctdb->ip_tree,
1471 IP_KEYLEN, ip_key(&public_ips->ips[j].addr),
1478 trbt_traversearray32(ctdb->ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
1484 * This is the length of the longtest common prefix between the IPs.
1485 * It is calculated by XOR-ing the 2 IPs together and counting the
1486 * number of leading zeroes. The implementation means that all
1487 * addresses end up being 128 bits long.
1489 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1490 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1491 * lots of nodes and IP addresses?
1493 static uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
1495 uint32_t ip1_k[IP_KEYLEN];
1500 uint32_t distance = 0;
1502 memcpy(ip1_k, ip_key(ip1), sizeof(ip1_k));
1504 for (i=0; i<IP_KEYLEN; i++) {
1505 x = ip1_k[i] ^ t[i];
1509 /* Count number of leading zeroes.
1510 * FIXME? This could be optimised...
1512 while ((x & (1 << 31)) == 0) {
1522 /* Calculate the IP distance for the given IP relative to IPs on the
1523 given node. The ips argument is generally the all_ips variable
1524 used in the main part of the algorithm.
1526 static uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1527 struct ctdb_public_ip_list *ips,
1530 struct ctdb_public_ip_list *t;
1535 for (t=ips; t != NULL; t=t->next) {
1536 if (t->pnn != pnn) {
1540 /* Optimisation: We never calculate the distance
1541 * between an address and itself. This allows us to
1542 * calculate the effect of removing an address from a
1543 * node by simply calculating the distance between
1544 * that address and all of the exitsing addresses.
1545 * Moreover, we assume that we're only ever dealing
1546 * with addresses from all_ips so we can identify an
1547 * address via a pointer rather than doing a more
1548 * expensive address comparison. */
1549 if (&(t->addr) == ip) {
1553 d = ip_distance(ip, &(t->addr));
1554 sum += d * d; /* Cheaper than pulling in math.h :-) */
1560 /* Return the LCP2 imbalance metric for addresses currently assigned
1563 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn)
1565 struct ctdb_public_ip_list *t;
1567 uint32_t imbalance = 0;
1569 for (t=all_ips; t!=NULL; t=t->next) {
1570 if (t->pnn != pnn) {
1573 /* Pass the rest of the IPs rather than the whole
1576 imbalance += ip_distance_2_sum(&(t->addr), t->next, pnn);
1582 /* Allocate any unassigned IPs just by looping through the IPs and
1583 * finding the best node for each.
1585 static void basic_allocate_unassigned(struct ctdb_context *ctdb,
1586 struct ctdb_node_map *nodemap,
1588 struct ctdb_public_ip_list *all_ips)
1590 struct ctdb_public_ip_list *tmp_ip;
1592 /* loop over all ip's and find a physical node to cover for
1595 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1596 if (tmp_ip->pnn == -1) {
1597 if (find_takeover_node(ctdb, nodemap, mask, tmp_ip, all_ips)) {
1598 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1599 ctdb_addr_to_str(&tmp_ip->addr)));
1605 /* Basic non-deterministic rebalancing algorithm.
1607 static bool basic_failback(struct ctdb_context *ctdb,
1608 struct ctdb_node_map *nodemap,
1610 struct ctdb_public_ip_list *all_ips,
1615 int maxnode, maxnum=0, minnode, minnum=0, num;
1616 struct ctdb_public_ip_list *tmp_ip;
1618 /* for each ip address, loop over all nodes that can serve
1619 this ip and make sure that the difference between the node
1620 serving the most and the node serving the least ip's are
1623 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1624 if (tmp_ip->pnn == -1) {
1628 /* Get the highest and lowest number of ips's served by any
1629 valid node which can serve this ip.
1633 for (i=0;i<nodemap->num;i++) {
1634 if (nodemap->nodes[i].flags & mask) {
1638 /* Only check nodes that are allowed to takeover an ip */
1639 if (nodemap->nodes[i].flags & NODE_FLAGS_NOIPTAKEOVER) {
1643 /* only check nodes that can actually serve this ip */
1644 if (can_node_serve_ip(ctdb, i, tmp_ip)) {
1645 /* no it couldnt so skip to the next node */
1649 num = node_ip_coverage(ctdb, i, all_ips);
1650 if (maxnode == -1) {
1659 if (minnode == -1) {
1669 if (maxnode == -1) {
1670 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
1671 ctdb_addr_to_str(&tmp_ip->addr)));
1676 /* if the spread between the smallest and largest coverage by
1677 a node is >=2 we steal one of the ips from the node with
1678 most coverage to even things out a bit.
1679 try to do this a limited number of times since we dont
1680 want to spend too much time balancing the ip coverage.
1682 if ( (maxnum > minnum+1)
1683 && (*retries < (num_ips + 5)) ){
1684 struct ctdb_public_ip_list *tmp;
1686 /* Reassign one of maxnode's VNNs */
1687 for (tmp=all_ips;tmp;tmp=tmp->next) {
1688 if (tmp->pnn == maxnode) {
1689 (void)find_takeover_node(ctdb, nodemap, mask, tmp, all_ips);
1700 struct ctdb_rebalancenodes {
1701 struct ctdb_rebalancenodes *next;
1704 static struct ctdb_rebalancenodes *force_rebalance_list = NULL;
1707 /* set this flag to force the node to be rebalanced even if it just didnt
1708 become healthy again.
1710 void lcp2_forcerebalance(struct ctdb_context *ctdb, uint32_t pnn)
1712 struct ctdb_rebalancenodes *rebalance;
1714 for (rebalance = force_rebalance_list; rebalance; rebalance = rebalance->next) {
1715 if (rebalance->pnn == pnn) {
1720 rebalance = talloc(ctdb, struct ctdb_rebalancenodes);
1721 rebalance->pnn = pnn;
1722 rebalance->next = force_rebalance_list;
1723 force_rebalance_list = rebalance;
1726 /* Do necessary LCP2 initialisation. Bury it in a function here so
1727 * that we can unit test it.
1729 static void lcp2_init(struct ctdb_context * tmp_ctx,
1730 struct ctdb_node_map * nodemap,
1732 struct ctdb_public_ip_list *all_ips,
1733 uint32_t **lcp2_imbalances,
1734 bool **newly_healthy)
1737 struct ctdb_public_ip_list *tmp_ip;
1739 *newly_healthy = talloc_array(tmp_ctx, bool, nodemap->num);
1740 CTDB_NO_MEMORY_FATAL(tmp_ctx, *newly_healthy);
1741 *lcp2_imbalances = talloc_array(tmp_ctx, uint32_t, nodemap->num);
1742 CTDB_NO_MEMORY_FATAL(tmp_ctx, *lcp2_imbalances);
1744 for (i=0;i<nodemap->num;i++) {
1745 (*lcp2_imbalances)[i] = lcp2_imbalance(all_ips, i);
1746 /* First step: is the node "healthy"? */
1747 (*newly_healthy)[i] = ! (bool)(nodemap->nodes[i].flags & mask);
1750 /* 2nd step: if a ndoe has IPs assigned then it must have been
1751 * healthy before, so we remove it from consideration... */
1752 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1753 if (tmp_ip->pnn != -1) {
1754 (*newly_healthy)[tmp_ip->pnn] = false;
1758 /* 3rd step: if a node is forced to re-balance then
1759 we allow failback onto the node */
1760 while (force_rebalance_list != NULL) {
1761 struct ctdb_rebalancenodes *next = force_rebalance_list->next;
1763 if (force_rebalance_list->pnn <= nodemap->num) {
1764 (*newly_healthy)[force_rebalance_list->pnn] = true;
1767 DEBUG(DEBUG_ERR,("During ipreallocation, forced rebalance of node %d\n", force_rebalance_list->pnn));
1768 talloc_free(force_rebalance_list);
1769 force_rebalance_list = next;
1773 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1774 * the IP/node combination that will cost the least.
1776 static void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1777 struct ctdb_node_map *nodemap,
1779 struct ctdb_public_ip_list *all_ips,
1780 uint32_t *lcp2_imbalances)
1782 struct ctdb_public_ip_list *tmp_ip;
1786 uint32_t mindsum, dstdsum, dstimbl, minimbl;
1787 struct ctdb_public_ip_list *minip;
1789 bool should_loop = true;
1790 bool have_unassigned = true;
1792 while (have_unassigned && should_loop) {
1793 should_loop = false;
1795 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1796 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1802 /* loop over each unassigned ip. */
1803 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1804 if (tmp_ip->pnn != -1) {
1808 for (dstnode=0; dstnode < nodemap->num; dstnode++) {
1809 /* Only check nodes that are allowed to takeover an ip */
1810 if (nodemap->nodes[dstnode].flags & NODE_FLAGS_NOIPTAKEOVER) {
1814 /* only check nodes that can actually serve this ip */
1815 if (can_node_serve_ip(ctdb, dstnode, tmp_ip)) {
1816 /* no it couldnt so skip to the next node */
1819 if (nodemap->nodes[dstnode].flags & mask) {
1823 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1824 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1825 DEBUG(DEBUG_DEBUG,(" %s -> %d [+%d]\n",
1826 ctdb_addr_to_str(&(tmp_ip->addr)),
1828 dstimbl - lcp2_imbalances[dstnode]));
1831 if ((minnode == -1) || (dstdsum < mindsum)) {
1841 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1843 /* If we found one then assign it to the given node. */
1844 if (minnode != -1) {
1845 minip->pnn = minnode;
1846 lcp2_imbalances[minnode] = minimbl;
1847 DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
1848 ctdb_addr_to_str(&(minip->addr)),
1853 /* There might be a better way but at least this is clear. */
1854 have_unassigned = false;
1855 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1856 if (tmp_ip->pnn == -1) {
1857 have_unassigned = true;
1862 /* We know if we have an unassigned addresses so we might as
1865 if (have_unassigned) {
1866 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1867 if (tmp_ip->pnn == -1) {
1868 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1869 ctdb_addr_to_str(&tmp_ip->addr)));
1875 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1876 * to move IPs from, determines the best IP/destination node
1877 * combination to move from the source node.
1879 static bool lcp2_failback_candidate(struct ctdb_context *ctdb,
1880 struct ctdb_node_map *nodemap,
1881 struct ctdb_public_ip_list *all_ips,
1884 uint32_t *lcp2_imbalances,
1885 bool *newly_healthy)
1887 int dstnode, mindstnode;
1888 uint32_t srcimbl, srcdsum, dstimbl, dstdsum;
1889 uint32_t minsrcimbl, mindstimbl;
1890 struct ctdb_public_ip_list *minip;
1891 struct ctdb_public_ip_list *tmp_ip;
1893 /* Find an IP and destination node that best reduces imbalance. */
1899 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1900 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES FROM %d [%d]\n", srcnode, candimbl));
1902 for (tmp_ip=all_ips; tmp_ip; tmp_ip=tmp_ip->next) {
1903 /* Only consider addresses on srcnode. */
1904 if (tmp_ip->pnn != srcnode) {
1908 /* What is this IP address costing the source node? */
1909 srcdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, srcnode);
1910 srcimbl = candimbl - srcdsum;
1912 /* Consider this IP address would cost each potential
1913 * destination node. Destination nodes are limited to
1914 * those that are newly healthy, since we don't want
1915 * to do gratuitous failover of IPs just to make minor
1916 * balance improvements.
1918 for (dstnode=0; dstnode < nodemap->num; dstnode++) {
1919 if (! newly_healthy[dstnode]) {
1923 /* Only check nodes that are allowed to takeover an ip */
1924 if (nodemap->nodes[dstnode].flags & NODE_FLAGS_NOIPTAKEOVER) {
1928 /* only check nodes that can actually serve this ip */
1929 if (can_node_serve_ip(ctdb, dstnode, tmp_ip)) {
1930 /* no it couldnt so skip to the next node */
1934 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1935 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1936 DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
1937 srcnode, srcimbl - lcp2_imbalances[srcnode],
1938 ctdb_addr_to_str(&(tmp_ip->addr)),
1939 dstnode, dstimbl - lcp2_imbalances[dstnode]));
1941 if ((dstimbl < candimbl) && (dstdsum < srcdsum) && \
1942 ((mindstnode == -1) || \
1943 ((srcimbl + dstimbl) < (minsrcimbl + mindstimbl)))) {
1946 minsrcimbl = srcimbl;
1947 mindstnode = dstnode;
1948 mindstimbl = dstimbl;
1952 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1954 if (mindstnode != -1) {
1955 /* We found a move that makes things better... */
1956 DEBUG(DEBUG_INFO,("%d [%d] -> %s -> %d [+%d]\n",
1957 srcnode, minsrcimbl - lcp2_imbalances[srcnode],
1958 ctdb_addr_to_str(&(minip->addr)),
1959 mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));
1962 lcp2_imbalances[srcnode] = srcimbl;
1963 lcp2_imbalances[mindstnode] = mindstimbl;
1964 minip->pnn = mindstnode;
1973 struct lcp2_imbalance_pnn {
1978 static int lcp2_cmp_imbalance_pnn(const void * a, const void * b)
1980 const struct lcp2_imbalance_pnn * lipa = (const struct lcp2_imbalance_pnn *) a;
1981 const struct lcp2_imbalance_pnn * lipb = (const struct lcp2_imbalance_pnn *) b;
1983 if (lipa->imbalance > lipb->imbalance) {
1985 } else if (lipa->imbalance == lipb->imbalance) {
1992 /* LCP2 algorithm for rebalancing the cluster. This finds the source
1993 * node with the highest LCP2 imbalance, and then determines the best
1994 * IP/destination node combination to move from the source node.
1996 static bool lcp2_failback(struct ctdb_context *ctdb,
1997 struct ctdb_node_map *nodemap,
1999 struct ctdb_public_ip_list *all_ips,
2000 uint32_t *lcp2_imbalances,
2001 bool *newly_healthy)
2003 int i, num_newly_healthy;
2004 struct lcp2_imbalance_pnn * lips;
2007 /* It is only worth continuing if we have suitable target
2008 * nodes to transfer IPs to. This check is much cheaper than
2011 num_newly_healthy = 0;
2012 for (i = 0; i < nodemap->num; i++) {
2013 if (newly_healthy[i]) {
2014 num_newly_healthy++;
2017 if (num_newly_healthy == 0) {
2021 /* Put the imbalances and nodes into an array, sort them and
2022 * iterate through candidates. Usually the 1st one will be
2023 * used, so this doesn't cost much...
2025 lips = talloc_array(ctdb, struct lcp2_imbalance_pnn, nodemap->num);
2026 for (i = 0; i < nodemap->num; i++) {
2027 lips[i].imbalance = lcp2_imbalances[i];
2030 qsort(lips, nodemap->num, sizeof(struct lcp2_imbalance_pnn),
2031 lcp2_cmp_imbalance_pnn);
2034 for (i = 0; i < nodemap->num; i++) {
2035 /* This means that all nodes had 0 or 1 addresses, so
2036 * can't be imbalanced.
2038 if (lips[i].imbalance == 0) {
2042 if (lcp2_failback_candidate(ctdb,
2058 /* The calculation part of the IP allocation algorithm. */
2059 static void ctdb_takeover_run_core(struct ctdb_context *ctdb,
2060 struct ctdb_node_map *nodemap,
2061 struct ctdb_public_ip_list **all_ips_p)
2063 int i, num_healthy, retries, num_ips;
2065 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2066 uint32_t *lcp2_imbalances;
2067 bool *newly_healthy;
2069 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2071 /* Count how many completely healthy nodes we have */
2073 for (i=0;i<nodemap->num;i++) {
2074 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
2079 /* If we have healthy nodes then we will only consider them
2080 for serving public addresses
2082 mask = NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED;
2083 if ((num_healthy == 0) &&
2084 (ctdb->tunable.no_ip_takeover_on_disabled == 0)) {
2085 /* We didnt have any completely healthy nodes so
2086 use "disabled" nodes as a fallback
2088 mask = NODE_FLAGS_INACTIVE;
2091 /* since nodes only know about those public addresses that
2092 can be served by that particular node, no single node has
2093 a full list of all public addresses that exist in the cluster.
2094 Walk over all node structures and create a merged list of
2095 all public addresses that exist in the cluster.
2097 keep the tree of ips around as ctdb->ip_tree
2099 all_ips = create_merged_ip_list(ctdb);
2100 *all_ips_p = all_ips; /* minimal code changes */
2102 /* Count how many ips we have */
2104 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2108 /* If we want deterministic ip allocations, i.e. that the ip addresses
2109 will always be allocated the same way for a specific set of
2110 available/unavailable nodes.
2112 if (1 == ctdb->tunable.deterministic_public_ips) {
2113 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2114 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
2115 tmp_ip->pnn = i%nodemap->num;
2118 /* IP failback doesn't make sense with deterministic
2119 * IPs, since the modulo step above implicitly fails
2120 * back IPs to their "home" node.
2122 if (1 == ctdb->tunable.no_ip_failback) {
2123 DEBUG(DEBUG_WARNING, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2128 /* mark all public addresses with a masked node as being served by
2131 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2132 if (tmp_ip->pnn == -1) {
2135 if (nodemap->nodes[tmp_ip->pnn].flags & mask) {
2140 /* verify that the assigned nodes can serve that public ip
2141 and set it to -1 if not
2143 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2144 if (tmp_ip->pnn == -1) {
2147 if (can_node_serve_ip(ctdb, tmp_ip->pnn, tmp_ip) != 0) {
2148 /* this node can not serve this ip. */
2153 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2154 lcp2_init(tmp_ctx, nodemap, mask, all_ips, &lcp2_imbalances, &newly_healthy);
2157 /* now we must redistribute all public addresses with takeover node
2158 -1 among the nodes available
2160 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2161 lcp2_allocate_unassigned(ctdb, nodemap, mask, all_ips, lcp2_imbalances);
2163 basic_allocate_unassigned(ctdb, nodemap, mask, all_ips);
2166 /* If we don't want IPs to fail back or if deterministic IPs
2167 * are being used, then don't rebalance IPs.
2169 if ((1 == ctdb->tunable.no_ip_failback) ||
2170 (1 == ctdb->tunable.deterministic_public_ips)) {
2175 /* now, try to make sure the ip adresses are evenly distributed
2180 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2181 if (lcp2_failback(ctdb, nodemap, mask, all_ips, lcp2_imbalances, newly_healthy)) {
2185 if (basic_failback(ctdb, nodemap, mask, all_ips, num_ips, &retries)) {
2190 /* finished distributing the public addresses, now just send the
2191 info out to the nodes */
2193 /* at this point ->pnn is the node which will own each IP
2194 or -1 if there is no node that can cover this ip
2197 talloc_free(tmp_ctx);
2202 static void noiptakeover_cb(struct ctdb_context *ctdb, uint32_t pnn, int32_t res, TDB_DATA outdata, void *callback)
2204 struct ctdb_node_map *nodemap = (struct ctdb_node_map *)callback;
2207 DEBUG(DEBUG_ERR,("Failure to read NoIPTakeover tunable from remote node %d\n", pnn));
2211 if (outdata.dsize != sizeof(uint32_t)) {
2212 DEBUG(DEBUG_ERR,("Wrong size of returned data when reading NoIPTakeover tunable from node %d. Expected %d bytes but received %d bytes\n", pnn, (int)sizeof(uint32_t), (int)outdata.dsize));
2216 if (pnn >= nodemap->num) {
2217 DEBUG(DEBUG_ERR,("Got NoIPTakeover reply from node %d but nodemap only has %d entries\n", pnn, nodemap->num));
2221 if (*(uint32_t *)outdata.dptr != 0) {
2222 nodemap->nodes[pnn].flags |= NODE_FLAGS_NOIPTAKEOVER;
2227 make any IP alias changes for public addresses that are necessary
2229 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
2230 client_async_callback fail_callback, void *callback_data)
2233 struct ctdb_public_ip ip;
2234 struct ctdb_public_ipv4 ipv4;
2235 struct ctdb_control_get_tunable *t;
2237 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2239 struct timeval timeout;
2240 struct client_async_data *async_data;
2241 struct ctdb_client_control_state *state;
2242 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2243 uint32_t disable_timeout;
2246 * ip failover is completely disabled, just send out the
2247 * ipreallocated event.
2249 if (ctdb->tunable.disable_ip_failover != 0) {
2254 /* assume all nodes do support failback */
2255 for (i=0;i<nodemap->num;i++) {
2256 nodemap->nodes[i].flags &= ~NODE_FLAGS_NOIPTAKEOVER;
2258 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen("NoIPTakeover") + 1;
2259 data.dptr = talloc_size(tmp_ctx, data.dsize);
2260 t = (struct ctdb_control_get_tunable *)data.dptr;
2261 t->length = strlen("NoIPTakeover")+1;
2262 memcpy(t->name, "NoIPTakeover", t->length);
2263 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2264 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_TUNABLE,
2265 nodes, 0, TAKEOVER_TIMEOUT(),
2267 noiptakeover_cb, NULL,
2269 DEBUG(DEBUG_ERR, (__location__ " ctdb_control to get noiptakeover tunable failed\n"));
2272 talloc_free(data.dptr);
2277 /* Do the IP reassignment calculations */
2278 ctdb_takeover_run_core(ctdb, nodemap, &all_ips);
2280 /* The recovery daemon does regular sanity checks of the IPs.
2281 * However, sometimes it is overzealous and thinks changes are
2282 * required when they're already underway. This stops the
2283 * checks for a while before we start moving IPs.
2285 disable_timeout = ctdb->tunable.takeover_timeout;
2286 data.dptr = (uint8_t*)&disable_timeout;
2287 data.dsize = sizeof(disable_timeout);
2288 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2289 CTDB_SRVID_DISABLE_IP_CHECK, data) != 0) {
2290 DEBUG(DEBUG_INFO,("Failed to disable ip verification\n"));
2293 /* now tell all nodes to delete any alias that they should not
2294 have. This will be a NOOP on nodes that don't currently
2295 hold the given alias */
2296 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2297 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2299 async_data->fail_callback = fail_callback;
2300 async_data->callback_data = callback_data;
2302 for (i=0;i<nodemap->num;i++) {
2303 /* don't talk to unconnected nodes, but do talk to banned nodes */
2304 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2308 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2309 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
2310 /* This node should be serving this
2311 vnn so dont tell it to release the ip
2315 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2316 ipv4.pnn = tmp_ip->pnn;
2317 ipv4.sin = tmp_ip->addr.ip;
2319 timeout = TAKEOVER_TIMEOUT();
2320 data.dsize = sizeof(ipv4);
2321 data.dptr = (uint8_t *)&ipv4;
2322 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2323 0, CTDB_CONTROL_RELEASE_IPv4, 0,
2327 ip.pnn = tmp_ip->pnn;
2328 ip.addr = tmp_ip->addr;
2330 timeout = TAKEOVER_TIMEOUT();
2331 data.dsize = sizeof(ip);
2332 data.dptr = (uint8_t *)&ip;
2333 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2334 0, CTDB_CONTROL_RELEASE_IP, 0,
2339 if (state == NULL) {
2340 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
2341 talloc_free(tmp_ctx);
2345 ctdb_client_async_add(async_data, state);
2348 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2349 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2350 talloc_free(tmp_ctx);
2353 talloc_free(async_data);
2356 /* tell all nodes to get their own IPs */
2357 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2358 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2360 async_data->fail_callback = fail_callback;
2361 async_data->callback_data = callback_data;
2363 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2364 if (tmp_ip->pnn == -1) {
2365 /* this IP won't be taken over */
2369 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2370 ipv4.pnn = tmp_ip->pnn;
2371 ipv4.sin = tmp_ip->addr.ip;
2373 timeout = TAKEOVER_TIMEOUT();
2374 data.dsize = sizeof(ipv4);
2375 data.dptr = (uint8_t *)&ipv4;
2376 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2377 0, CTDB_CONTROL_TAKEOVER_IPv4, 0,
2381 ip.pnn = tmp_ip->pnn;
2382 ip.addr = tmp_ip->addr;
2384 timeout = TAKEOVER_TIMEOUT();
2385 data.dsize = sizeof(ip);
2386 data.dptr = (uint8_t *)&ip;
2387 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2388 0, CTDB_CONTROL_TAKEOVER_IP, 0,
2392 if (state == NULL) {
2393 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
2394 talloc_free(tmp_ctx);
2398 ctdb_client_async_add(async_data, state);
2400 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2401 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2402 talloc_free(tmp_ctx);
2408 * Tell all nodes to run eventscripts to process the
2409 * "ipreallocated" event. This can do a lot of things,
2410 * including restarting services to reconfigure them if public
2411 * IPs have moved. Once upon a time this event only used to
2414 data.dptr = discard_const("ipreallocated");
2415 data.dsize = strlen((char *)data.dptr) + 1;
2416 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2417 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RUN_EVENTSCRIPTS,
2418 nodes, 0, TAKEOVER_TIMEOUT(),
2420 NULL, fail_callback,
2421 callback_data) != 0) {
2422 DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
2425 talloc_free(tmp_ctx);
2431 destroy a ctdb_client_ip structure
2433 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
2435 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
2436 ctdb_addr_to_str(&ip->addr),
2437 ntohs(ip->addr.ip.sin_port),
2440 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
2445 called by a client to inform us of a TCP connection that it is managing
2446 that should tickled with an ACK when IP takeover is done
2447 we handle both the old ipv4 style of packets as well as the new ipv4/6
2450 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
2453 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
2454 struct ctdb_control_tcp *old_addr = NULL;
2455 struct ctdb_control_tcp_addr new_addr;
2456 struct ctdb_control_tcp_addr *tcp_sock = NULL;
2457 struct ctdb_tcp_list *tcp;
2458 struct ctdb_tcp_connection t;
2461 struct ctdb_client_ip *ip;
2462 struct ctdb_vnn *vnn;
2463 ctdb_sock_addr addr;
2465 switch (indata.dsize) {
2466 case sizeof(struct ctdb_control_tcp):
2467 old_addr = (struct ctdb_control_tcp *)indata.dptr;
2468 ZERO_STRUCT(new_addr);
2469 tcp_sock = &new_addr;
2470 tcp_sock->src.ip = old_addr->src;
2471 tcp_sock->dest.ip = old_addr->dest;
2473 case sizeof(struct ctdb_control_tcp_addr):
2474 tcp_sock = (struct ctdb_control_tcp_addr *)indata.dptr;
2477 DEBUG(DEBUG_ERR,(__location__ " Invalid data structure passed "
2478 "to ctdb_control_tcp_client. size was %d but "
2479 "only allowed sizes are %lu and %lu\n",
2481 (long unsigned)sizeof(struct ctdb_control_tcp),
2482 (long unsigned)sizeof(struct ctdb_control_tcp_addr)));
2486 addr = tcp_sock->src;
2487 ctdb_canonicalize_ip(&addr, &tcp_sock->src);
2488 addr = tcp_sock->dest;
2489 ctdb_canonicalize_ip(&addr, &tcp_sock->dest);
2492 memcpy(&addr, &tcp_sock->dest, sizeof(addr));
2493 vnn = find_public_ip_vnn(ctdb, &addr);
2495 switch (addr.sa.sa_family) {
2497 if (ntohl(addr.ip.sin_addr.s_addr) != INADDR_LOOPBACK) {
2498 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public address.\n",
2499 ctdb_addr_to_str(&addr)));
2503 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public ipv6 address.\n",
2504 ctdb_addr_to_str(&addr)));
2507 DEBUG(DEBUG_ERR,(__location__ " Unknown family type %d\n", addr.sa.sa_family));
2513 if (vnn->pnn != ctdb->pnn) {
2514 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2515 ctdb_addr_to_str(&addr),
2516 client_id, client->pid));
2517 /* failing this call will tell smbd to die */
2521 ip = talloc(client, struct ctdb_client_ip);
2522 CTDB_NO_MEMORY(ctdb, ip);
2526 ip->client_id = client_id;
2527 talloc_set_destructor(ip, ctdb_client_ip_destructor);
2528 DLIST_ADD(ctdb->client_ip_list, ip);
2530 tcp = talloc(client, struct ctdb_tcp_list);
2531 CTDB_NO_MEMORY(ctdb, tcp);
2533 tcp->connection.src_addr = tcp_sock->src;
2534 tcp->connection.dst_addr = tcp_sock->dest;
2536 DLIST_ADD(client->tcp_list, tcp);
2538 t.src_addr = tcp_sock->src;
2539 t.dst_addr = tcp_sock->dest;
2541 data.dptr = (uint8_t *)&t;
2542 data.dsize = sizeof(t);
2544 switch (addr.sa.sa_family) {
2546 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2547 (unsigned)ntohs(tcp_sock->dest.ip.sin_port),
2548 ctdb_addr_to_str(&tcp_sock->src),
2549 (unsigned)ntohs(tcp_sock->src.ip.sin_port), client_id, client->pid));
2552 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2553 (unsigned)ntohs(tcp_sock->dest.ip6.sin6_port),
2554 ctdb_addr_to_str(&tcp_sock->src),
2555 (unsigned)ntohs(tcp_sock->src.ip6.sin6_port), client_id, client->pid));
2558 DEBUG(DEBUG_ERR,(__location__ " Unknown family %d\n", addr.sa.sa_family));
2562 /* tell all nodes about this tcp connection */
2563 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
2564 CTDB_CONTROL_TCP_ADD,
2565 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
2567 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
2575 find a tcp address on a list
2577 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
2578 struct ctdb_tcp_connection *tcp)
2582 if (array == NULL) {
2586 for (i=0;i<array->num;i++) {
2587 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
2588 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
2589 return &array->connections[i];
2598 called by a daemon to inform us of a TCP connection that one of its
2599 clients managing that should tickled with an ACK when IP takeover is
2602 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
2604 struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
2605 struct ctdb_tcp_array *tcparray;
2606 struct ctdb_tcp_connection tcp;
2607 struct ctdb_vnn *vnn;
2609 vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
2611 DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
2612 ctdb_addr_to_str(&p->dst_addr)));
2618 tcparray = vnn->tcp_array;
2620 /* If this is the first tickle */
2621 if (tcparray == NULL) {
2622 tcparray = talloc_size(ctdb->nodes,
2623 offsetof(struct ctdb_tcp_array, connections) +
2624 sizeof(struct ctdb_tcp_connection) * 1);
2625 CTDB_NO_MEMORY(ctdb, tcparray);
2626 vnn->tcp_array = tcparray;
2629 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
2630 CTDB_NO_MEMORY(ctdb, tcparray->connections);
2632 tcparray->connections[tcparray->num].src_addr = p->src_addr;
2633 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
2636 if (tcp_update_needed) {
2637 vnn->tcp_update_needed = true;
2643 /* Do we already have this tickle ?*/
2644 tcp.src_addr = p->src_addr;
2645 tcp.dst_addr = p->dst_addr;
2646 if (ctdb_tcp_find(vnn->tcp_array, &tcp) != NULL) {
2647 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
2648 ctdb_addr_to_str(&tcp.dst_addr),
2649 ntohs(tcp.dst_addr.ip.sin_port),
2654 /* A new tickle, we must add it to the array */
2655 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
2656 struct ctdb_tcp_connection,
2658 CTDB_NO_MEMORY(ctdb, tcparray->connections);
2660 vnn->tcp_array = tcparray;
2661 tcparray->connections[tcparray->num].src_addr = p->src_addr;
2662 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
2665 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
2666 ctdb_addr_to_str(&tcp.dst_addr),
2667 ntohs(tcp.dst_addr.ip.sin_port),
2670 if (tcp_update_needed) {
2671 vnn->tcp_update_needed = true;
2679 called by a daemon to inform us of a TCP connection that one of its
2680 clients managing that should tickled with an ACK when IP takeover is
2683 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
2685 struct ctdb_tcp_connection *tcpp;
2686 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
2689 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
2690 ctdb_addr_to_str(&conn->dst_addr)));
2694 /* if the array is empty we cant remove it
2695 and we dont need to do anything
2697 if (vnn->tcp_array == NULL) {
2698 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
2699 ctdb_addr_to_str(&conn->dst_addr),
2700 ntohs(conn->dst_addr.ip.sin_port)));
2705 /* See if we know this connection
2706 if we dont know this connection then we dont need to do anything
2708 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
2710 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
2711 ctdb_addr_to_str(&conn->dst_addr),
2712 ntohs(conn->dst_addr.ip.sin_port)));
2717 /* We need to remove this entry from the array.
2718 Instead of allocating a new array and copying data to it
2719 we cheat and just copy the last entry in the existing array
2720 to the entry that is to be removed and just shring the
2723 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
2724 vnn->tcp_array->num--;
2726 /* If we deleted the last entry we also need to remove the entire array
2728 if (vnn->tcp_array->num == 0) {
2729 talloc_free(vnn->tcp_array);
2730 vnn->tcp_array = NULL;
2733 vnn->tcp_update_needed = true;
2735 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
2736 ctdb_addr_to_str(&conn->src_addr),
2737 ntohs(conn->src_addr.ip.sin_port)));
2742 called by a daemon to inform us of a TCP connection that one of its
2743 clients used are no longer needed in the tickle database
2745 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
2747 struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
2749 ctdb_remove_tcp_connection(ctdb, conn);
2756 called when a daemon restarts - send all tickes for all public addresses
2757 we are serving immediately to the new node.
2759 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn)
2761 /*XXX here we should send all tickes we are serving to the new node */
2767 called when a client structure goes away - hook to remove
2768 elements from the tcp_list in all daemons
2770 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
2772 while (client->tcp_list) {
2773 struct ctdb_tcp_list *tcp = client->tcp_list;
2774 DLIST_REMOVE(client->tcp_list, tcp);
2775 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
2781 release all IPs on shutdown
2783 void ctdb_release_all_ips(struct ctdb_context *ctdb)
2785 struct ctdb_vnn *vnn;
2787 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
2788 if (!ctdb_sys_have_ip(&vnn->public_address)) {
2789 ctdb_vnn_unassign_iface(ctdb, vnn);
2795 ctdb_event_script_args(ctdb, CTDB_EVENT_RELEASE_IP, "%s %s %u",
2796 ctdb_vnn_iface_string(vnn),
2797 ctdb_addr_to_str(&vnn->public_address),
2798 vnn->public_netmask_bits);
2799 release_kill_clients(ctdb, &vnn->public_address);
2800 ctdb_vnn_unassign_iface(ctdb, vnn);
2806 get list of public IPs
2808 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
2809 struct ctdb_req_control *c, TDB_DATA *outdata)
2812 struct ctdb_all_public_ips *ips;
2813 struct ctdb_vnn *vnn;
2814 bool only_available = false;
2816 if (c->flags & CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE) {
2817 only_available = true;
2820 /* count how many public ip structures we have */
2822 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
2826 len = offsetof(struct ctdb_all_public_ips, ips) +
2827 num*sizeof(struct ctdb_public_ip);
2828 ips = talloc_zero_size(outdata, len);
2829 CTDB_NO_MEMORY(ctdb, ips);
2832 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
2833 if (only_available && !ctdb_vnn_available(ctdb, vnn)) {
2836 ips->ips[i].pnn = vnn->pnn;
2837 ips->ips[i].addr = vnn->public_address;
2841 len = offsetof(struct ctdb_all_public_ips, ips) +
2842 i*sizeof(struct ctdb_public_ip);
2844 outdata->dsize = len;
2845 outdata->dptr = (uint8_t *)ips;
2852 get list of public IPs, old ipv4 style. only returns ipv4 addresses
2854 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb,
2855 struct ctdb_req_control *c, TDB_DATA *outdata)
2858 struct ctdb_all_public_ipsv4 *ips;
2859 struct ctdb_vnn *vnn;
2861 /* count how many public ip structures we have */
2863 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
2864 if (vnn->public_address.sa.sa_family != AF_INET) {
2870 len = offsetof(struct ctdb_all_public_ipsv4, ips) +
2871 num*sizeof(struct ctdb_public_ipv4);
2872 ips = talloc_zero_size(outdata, len);
2873 CTDB_NO_MEMORY(ctdb, ips);
2875 outdata->dsize = len;
2876 outdata->dptr = (uint8_t *)ips;
2880 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
2881 if (vnn->public_address.sa.sa_family != AF_INET) {
2884 ips->ips[i].pnn = vnn->pnn;
2885 ips->ips[i].sin = vnn->public_address.ip;
2892 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
2893 struct ctdb_req_control *c,
2898 ctdb_sock_addr *addr;
2899 struct ctdb_control_public_ip_info *info;
2900 struct ctdb_vnn *vnn;
2902 addr = (ctdb_sock_addr *)indata.dptr;
2904 vnn = find_public_ip_vnn(ctdb, addr);
2906 /* if it is not a public ip it could be our 'single ip' */
2907 if (ctdb->single_ip_vnn) {
2908 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
2909 vnn = ctdb->single_ip_vnn;
2914 DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
2915 "'%s'not a public address\n",
2916 ctdb_addr_to_str(addr)));
2920 /* count how many public ip structures we have */
2922 for (;vnn->ifaces[num];) {
2926 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
2927 num*sizeof(struct ctdb_control_iface_info);
2928 info = talloc_zero_size(outdata, len);
2929 CTDB_NO_MEMORY(ctdb, info);
2931 info->ip.addr = vnn->public_address;
2932 info->ip.pnn = vnn->pnn;
2933 info->active_idx = 0xFFFFFFFF;
2935 for (i=0; vnn->ifaces[i]; i++) {
2936 struct ctdb_iface *cur;
2938 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
2940 DEBUG(DEBUG_CRIT, (__location__ " internal error iface[%s] unknown\n",
2944 if (vnn->iface == cur) {
2945 info->active_idx = i;
2947 strcpy(info->ifaces[i].name, cur->name);
2948 info->ifaces[i].link_state = cur->link_up;
2949 info->ifaces[i].references = cur->references;
2952 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
2953 i*sizeof(struct ctdb_control_iface_info);
2955 outdata->dsize = len;
2956 outdata->dptr = (uint8_t *)info;
2961 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
2962 struct ctdb_req_control *c,
2966 struct ctdb_control_get_ifaces *ifaces;
2967 struct ctdb_iface *cur;
2969 /* count how many public ip structures we have */
2971 for (cur=ctdb->ifaces;cur;cur=cur->next) {
2975 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
2976 num*sizeof(struct ctdb_control_iface_info);
2977 ifaces = talloc_zero_size(outdata, len);
2978 CTDB_NO_MEMORY(ctdb, ifaces);
2981 for (cur=ctdb->ifaces;cur;cur=cur->next) {
2982 strcpy(ifaces->ifaces[i].name, cur->name);
2983 ifaces->ifaces[i].link_state = cur->link_up;
2984 ifaces->ifaces[i].references = cur->references;
2988 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
2989 i*sizeof(struct ctdb_control_iface_info);
2991 outdata->dsize = len;
2992 outdata->dptr = (uint8_t *)ifaces;
2997 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
2998 struct ctdb_req_control *c,
3001 struct ctdb_control_iface_info *info;
3002 struct ctdb_iface *iface;
3003 bool link_up = false;
3005 info = (struct ctdb_control_iface_info *)indata.dptr;
3007 if (info->name[CTDB_IFACE_SIZE] != '\0') {
3008 int len = strnlen(info->name, CTDB_IFACE_SIZE);
3009 DEBUG(DEBUG_ERR, (__location__ " name[%*.*s] not terminated\n",
3010 len, len, info->name));
3014 switch (info->link_state) {
3022 DEBUG(DEBUG_ERR, (__location__ " link_state[%u] invalid\n",
3023 (unsigned int)info->link_state));
3027 if (info->references != 0) {
3028 DEBUG(DEBUG_ERR, (__location__ " references[%u] should be 0\n",
3029 (unsigned int)info->references));
3033 iface = ctdb_find_iface(ctdb, info->name);
3034 if (iface == NULL) {
3038 if (link_up == iface->link_up) {
3042 DEBUG(iface->link_up?DEBUG_ERR:DEBUG_NOTICE,
3043 ("iface[%s] has changed it's link status %s => %s\n",
3045 iface->link_up?"up":"down",
3046 link_up?"up":"down"));
3048 iface->link_up = link_up;
3054 structure containing the listening socket and the list of tcp connections
3055 that the ctdb daemon is to kill
3057 struct ctdb_kill_tcp {
3058 struct ctdb_vnn *vnn;
3059 struct ctdb_context *ctdb;
3061 struct fd_event *fde;
3062 trbt_tree_t *connections;
3067 a tcp connection that is to be killed
3069 struct ctdb_killtcp_con {
3070 ctdb_sock_addr src_addr;
3071 ctdb_sock_addr dst_addr;
3073 struct ctdb_kill_tcp *killtcp;
3076 /* this function is used to create a key to represent this socketpair
3077 in the killtcp tree.
3078 this key is used to insert and lookup matching socketpairs that are
3079 to be tickled and RST
3081 #define KILLTCP_KEYLEN 10
3082 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
3084 static uint32_t key[KILLTCP_KEYLEN];
3086 bzero(key, sizeof(key));
3088 if (src->sa.sa_family != dst->sa.sa_family) {
3089 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
3093 switch (src->sa.sa_family) {
3095 key[0] = dst->ip.sin_addr.s_addr;
3096 key[1] = src->ip.sin_addr.s_addr;
3097 key[2] = dst->ip.sin_port;
3098 key[3] = src->ip.sin_port;
3101 uint32_t *dst6_addr32 =
3102 (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
3103 uint32_t *src6_addr32 =
3104 (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
3105 key[0] = dst6_addr32[3];
3106 key[1] = src6_addr32[3];
3107 key[2] = dst6_addr32[2];
3108 key[3] = src6_addr32[2];
3109 key[4] = dst6_addr32[1];
3110 key[5] = src6_addr32[1];
3111 key[6] = dst6_addr32[0];
3112 key[7] = src6_addr32[0];
3113 key[8] = dst->ip6.sin6_port;
3114 key[9] = src->ip6.sin6_port;
3118 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
3126 called when we get a read event on the raw socket
3128 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
3129 uint16_t flags, void *private_data)
3131 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3132 struct ctdb_killtcp_con *con;
3133 ctdb_sock_addr src, dst;
3134 uint32_t ack_seq, seq;
3136 if (!(flags & EVENT_FD_READ)) {
3140 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
3141 killtcp->private_data,
3143 &ack_seq, &seq) != 0) {
3144 /* probably a non-tcp ACK packet */
3148 /* check if we have this guy in our list of connections
3151 con = trbt_lookuparray32(killtcp->connections,
3152 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
3154 /* no this was some other packet we can just ignore */
3158 /* This one has been tickled !
3159 now reset him and remove him from the list.
3161 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3162 ntohs(con->dst_addr.ip.sin_port),
3163 ctdb_addr_to_str(&con->src_addr),
3164 ntohs(con->src_addr.ip.sin_port)));
3166 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
3171 /* when traversing the list of all tcp connections to send tickle acks to
3172 (so that we can capture the ack coming back and kill the connection
3174 this callback is called for each connection we are currently trying to kill
3176 static int tickle_connection_traverse(void *param, void *data)
3178 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
3180 /* have tried too many times, just give up */
3181 if (con->count >= 5) {
3182 /* can't delete in traverse: reparent to delete_cons */
3183 talloc_steal(param, con);
3187 /* othervise, try tickling it again */
3190 (ctdb_sock_addr *)&con->dst_addr,
3191 (ctdb_sock_addr *)&con->src_addr,
3198 called every second until all sentenced connections have been reset
3200 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
3201 struct timeval t, void *private_data)
3203 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3204 void *delete_cons = talloc_new(NULL);
3206 /* loop over all connections sending tickle ACKs */
3207 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
3209 /* now we've finished traverse, it's safe to do deletion. */
3210 talloc_free(delete_cons);
3212 /* If there are no more connections to kill we can remove the
3213 entire killtcp structure
3215 if ( (killtcp->connections == NULL) ||
3216 (killtcp->connections->root == NULL) ) {
3217 talloc_free(killtcp);
3221 /* try tickling them again in a seconds time
3223 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3224 ctdb_tickle_sentenced_connections, killtcp);
3228 destroy the killtcp structure
3230 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
3232 struct ctdb_vnn *tmpvnn;
3234 /* verify that this vnn is still active */
3235 for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
3236 if (tmpvnn == killtcp->vnn) {
3241 if (tmpvnn == NULL) {
3245 if (killtcp->vnn->killtcp != killtcp) {
3249 killtcp->vnn->killtcp = NULL;
3255 /* nothing fancy here, just unconditionally replace any existing
3256 connection structure with the new one.
3258 dont even free the old one if it did exist, that one is talloc_stolen
3259 by the same node in the tree anyway and will be deleted when the new data
3262 static void *add_killtcp_callback(void *parm, void *data)
3268 add a tcp socket to the list of connections we want to RST
3270 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
3274 ctdb_sock_addr src, dst;
3275 struct ctdb_kill_tcp *killtcp;
3276 struct ctdb_killtcp_con *con;
3277 struct ctdb_vnn *vnn;
3279 ctdb_canonicalize_ip(s, &src);
3280 ctdb_canonicalize_ip(d, &dst);
3282 vnn = find_public_ip_vnn(ctdb, &dst);
3284 vnn = find_public_ip_vnn(ctdb, &src);
3287 /* if it is not a public ip it could be our 'single ip' */
3288 if (ctdb->single_ip_vnn) {
3289 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
3290 vnn = ctdb->single_ip_vnn;
3295 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
3299 killtcp = vnn->killtcp;
3301 /* If this is the first connection to kill we must allocate
3304 if (killtcp == NULL) {
3305 killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
3306 CTDB_NO_MEMORY(ctdb, killtcp);
3309 killtcp->ctdb = ctdb;
3310 killtcp->capture_fd = -1;
3311 killtcp->connections = trbt_create(killtcp, 0);
3313 vnn->killtcp = killtcp;
3314 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
3319 /* create a structure that describes this connection we want to
3320 RST and store it in killtcp->connections
3322 con = talloc(killtcp, struct ctdb_killtcp_con);
3323 CTDB_NO_MEMORY(ctdb, con);
3324 con->src_addr = src;
3325 con->dst_addr = dst;
3327 con->killtcp = killtcp;
3330 trbt_insertarray32_callback(killtcp->connections,
3331 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
3332 add_killtcp_callback, con);
3335 If we dont have a socket to listen on yet we must create it
3337 if (killtcp->capture_fd == -1) {
3338 const char *iface = ctdb_vnn_iface_string(vnn);
3339 killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
3340 if (killtcp->capture_fd == -1) {
3341 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
3342 "socket on iface '%s' for killtcp (%s)\n",
3343 iface, strerror(errno)));
3349 if (killtcp->fde == NULL) {
3350 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
3352 capture_tcp_handler, killtcp);
3353 tevent_fd_set_auto_close(killtcp->fde);
3355 /* We also need to set up some events to tickle all these connections
3356 until they are all reset
3358 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3359 ctdb_tickle_sentenced_connections, killtcp);
3362 /* tickle him once now */
3371 talloc_free(vnn->killtcp);
3372 vnn->killtcp = NULL;
3377 kill a TCP connection.
3379 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
3381 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
3383 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
3387 called by a daemon to inform us of the entire list of TCP tickles for
3388 a particular public address.
3389 this control should only be sent by the node that is currently serving
3390 that public address.
3392 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
3394 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
3395 struct ctdb_tcp_array *tcparray;
3396 struct ctdb_vnn *vnn;
3398 /* We must at least have tickles.num or else we cant verify the size
3399 of the received data blob
3401 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3402 tickles.connections)) {
3403 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3407 /* verify that the size of data matches what we expect */
3408 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3409 tickles.connections)
3410 + sizeof(struct ctdb_tcp_connection)
3411 * list->tickles.num) {
3412 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3416 vnn = find_public_ip_vnn(ctdb, &list->addr);
3418 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
3419 ctdb_addr_to_str(&list->addr)));
3424 /* remove any old ticklelist we might have */
3425 talloc_free(vnn->tcp_array);
3426 vnn->tcp_array = NULL;
3428 tcparray = talloc(ctdb->nodes, struct ctdb_tcp_array);
3429 CTDB_NO_MEMORY(ctdb, tcparray);
3431 tcparray->num = list->tickles.num;
3433 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
3434 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3436 memcpy(tcparray->connections, &list->tickles.connections[0],
3437 sizeof(struct ctdb_tcp_connection)*tcparray->num);
3439 /* We now have a new fresh tickle list array for this vnn */
3440 vnn->tcp_array = talloc_steal(vnn, tcparray);
3446 called to return the full list of tickles for the puclic address associated
3447 with the provided vnn
3449 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
3451 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
3452 struct ctdb_control_tcp_tickle_list *list;
3453 struct ctdb_tcp_array *tcparray;
3455 struct ctdb_vnn *vnn;
3457 vnn = find_public_ip_vnn(ctdb, addr);
3459 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
3460 ctdb_addr_to_str(addr)));
3465 tcparray = vnn->tcp_array;
3467 num = tcparray->num;
3472 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3473 tickles.connections)
3474 + sizeof(struct ctdb_tcp_connection) * num;
3476 outdata->dptr = talloc_size(outdata, outdata->dsize);
3477 CTDB_NO_MEMORY(ctdb, outdata->dptr);
3478 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
3481 list->tickles.num = num;
3483 memcpy(&list->tickles.connections[0], tcparray->connections,
3484 sizeof(struct ctdb_tcp_connection) * num);
3492 set the list of all tcp tickles for a public address
3494 static int ctdb_ctrl_set_tcp_tickles(struct ctdb_context *ctdb,
3495 struct timeval timeout, uint32_t destnode,
3496 ctdb_sock_addr *addr,
3497 struct ctdb_tcp_array *tcparray)
3501 struct ctdb_control_tcp_tickle_list *list;
3504 num = tcparray->num;
3509 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3510 tickles.connections) +
3511 sizeof(struct ctdb_tcp_connection) * num;
3512 data.dptr = talloc_size(ctdb, data.dsize);
3513 CTDB_NO_MEMORY(ctdb, data.dptr);
3515 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
3517 list->tickles.num = num;
3519 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
3522 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
3523 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
3524 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
3526 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
3530 talloc_free(data.dptr);
3537 perform tickle updates if required
3539 static void ctdb_update_tcp_tickles(struct event_context *ev,
3540 struct timed_event *te,
3541 struct timeval t, void *private_data)
3543 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
3545 struct ctdb_vnn *vnn;
3547 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3548 /* we only send out updates for public addresses that
3551 if (ctdb->pnn != vnn->pnn) {
3554 /* We only send out the updates if we need to */
3555 if (!vnn->tcp_update_needed) {
3558 ret = ctdb_ctrl_set_tcp_tickles(ctdb,
3560 CTDB_BROADCAST_CONNECTED,
3561 &vnn->public_address,
3564 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
3565 ctdb_addr_to_str(&vnn->public_address)));
3569 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
3570 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
3571 ctdb_update_tcp_tickles, ctdb);
3576 start periodic update of tcp tickles
3578 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
3580 ctdb->tickle_update_context = talloc_new(ctdb);
3582 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
3583 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
3584 ctdb_update_tcp_tickles, ctdb);
3590 struct control_gratious_arp {
3591 struct ctdb_context *ctdb;
3592 ctdb_sock_addr addr;
3598 send a control_gratuitous arp
3600 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
3601 struct timeval t, void *private_data)
3604 struct control_gratious_arp *arp = talloc_get_type(private_data,
3605 struct control_gratious_arp);
3607 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
3609 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp on iface '%s' failed (%s)\n",
3610 arp->iface, strerror(errno)));
3615 if (arp->count == CTDB_ARP_REPEAT) {
3620 event_add_timed(arp->ctdb->ev, arp,
3621 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
3622 send_gratious_arp, arp);
3629 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
3631 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
3632 struct control_gratious_arp *arp;
3634 /* verify the size of indata */
3635 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
3636 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
3637 (unsigned)indata.dsize,
3638 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
3642 ( offsetof(struct ctdb_control_gratious_arp, iface)
3643 + gratious_arp->len ) ){
3645 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
3646 "but should be %u bytes\n",
3647 (unsigned)indata.dsize,
3648 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
3653 arp = talloc(ctdb, struct control_gratious_arp);
3654 CTDB_NO_MEMORY(ctdb, arp);
3657 arp->addr = gratious_arp->addr;
3658 arp->iface = talloc_strdup(arp, gratious_arp->iface);
3659 CTDB_NO_MEMORY(ctdb, arp->iface);
3662 event_add_timed(arp->ctdb->ev, arp,
3663 timeval_zero(), send_gratious_arp, arp);
3668 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
3670 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
3673 /* verify the size of indata */
3674 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
3675 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
3679 ( offsetof(struct ctdb_control_ip_iface, iface)
3682 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
3683 "but should be %u bytes\n",
3684 (unsigned)indata.dsize,
3685 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
3689 ret = ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0], true);
3692 DEBUG(DEBUG_ERR,(__location__ " Failed to add public address\n"));
3700 called when releaseip event finishes for del_public_address
3702 static void delete_ip_callback(struct ctdb_context *ctdb, int status,
3705 talloc_free(private_data);
3708 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
3710 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
3711 struct ctdb_vnn *vnn;
3714 /* verify the size of indata */
3715 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
3716 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
3720 ( offsetof(struct ctdb_control_ip_iface, iface)
3723 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
3724 "but should be %u bytes\n",
3725 (unsigned)indata.dsize,
3726 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
3730 /* walk over all public addresses until we find a match */
3731 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3732 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
3733 TALLOC_CTX *mem_ctx = talloc_new(ctdb);
3735 DLIST_REMOVE(ctdb->vnn, vnn);
3736 talloc_steal(mem_ctx, vnn);
3737 ctdb_remove_orphaned_ifaces(ctdb, vnn, mem_ctx);
3738 if (vnn->pnn != ctdb->pnn) {
3739 if (vnn->iface != NULL) {
3740 ctdb_vnn_unassign_iface(ctdb, vnn);
3742 talloc_free(mem_ctx);
3747 ret = ctdb_event_script_callback(ctdb,
3748 mem_ctx, delete_ip_callback, mem_ctx,
3750 CTDB_EVENT_RELEASE_IP,
3752 ctdb_vnn_iface_string(vnn),
3753 ctdb_addr_to_str(&vnn->public_address),
3754 vnn->public_netmask_bits);
3755 if (vnn->iface != NULL) {
3756 ctdb_vnn_unassign_iface(ctdb, vnn);
3768 /* This function is called from the recovery daemon to verify that a remote
3769 node has the expected ip allocation.
3770 This is verified against ctdb->ip_tree
3772 int verify_remote_ip_allocation(struct ctdb_context *ctdb, struct ctdb_all_public_ips *ips)
3774 struct ctdb_public_ip_list *tmp_ip;
3777 if (ctdb->ip_tree == NULL) {
3778 /* dont know the expected allocation yet, assume remote node
3787 for (i=0; i<ips->num; i++) {
3788 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ips->ips[i].addr));
3789 if (tmp_ip == NULL) {
3790 DEBUG(DEBUG_ERR,(__location__ " Could not find host for address %s, reassign ips\n", ctdb_addr_to_str(&ips->ips[i].addr)));
3794 if (tmp_ip->pnn == -1 || ips->ips[i].pnn == -1) {
3798 if (tmp_ip->pnn != ips->ips[i].pnn) {
3799 DEBUG(DEBUG_ERR,("Inconsistent ip allocation. Trigger reallocation. Thinks %s is held by node %u while it is held by node %u\n", ctdb_addr_to_str(&ips->ips[i].addr), ips->ips[i].pnn, tmp_ip->pnn));
3807 int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
3809 struct ctdb_public_ip_list *tmp_ip;
3811 if (ctdb->ip_tree == NULL) {
3812 DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
3816 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
3817 if (tmp_ip == NULL) {
3818 DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
3822 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));
3823 tmp_ip->pnn = ip->pnn;
3829 struct ctdb_reloadips_handle {
3830 struct ctdb_context *ctdb;
3831 struct ctdb_req_control *c;
3835 struct fd_event *fde;
3838 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle *h)
3840 if (h == h->ctdb->reload_ips) {
3841 h->ctdb->reload_ips = NULL;
3844 ctdb_request_control_reply(h->ctdb, h->c, NULL, h->status, NULL);
3847 ctdb_kill(h->ctdb, h->child, SIGKILL);
3851 static void ctdb_reloadips_timeout_event(struct event_context *ev,
3852 struct timed_event *te,
3853 struct timeval t, void *private_data)
3855 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
3860 static void ctdb_reloadips_child_handler(struct event_context *ev, struct fd_event *fde,
3861 uint16_t flags, void *private_data)
3863 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
3868 ret = read(h->fd[0], &res, 1);
3869 if (ret < 1 || res != 0) {
3870 DEBUG(DEBUG_ERR, (__location__ " Reloadips child process returned error\n"));
3878 static int ctdb_reloadips_child(struct ctdb_context *ctdb)
3880 TALLOC_CTX *mem_ctx = talloc_new(NULL);
3881 struct ctdb_all_public_ips *ips;
3882 struct ctdb_vnn *vnn;
3885 /* read the ip allocation from the local node */
3886 ret = ctdb_ctrl_get_public_ips(ctdb, TAKEOVER_TIMEOUT(), CTDB_CURRENT_NODE, mem_ctx, &ips);
3888 DEBUG(DEBUG_ERR, ("Unable to get public ips from local node\n"));
3889 talloc_free(mem_ctx);
3893 /* re-read the public ips file */
3895 if (ctdb_set_public_addresses(ctdb, false) != 0) {
3896 DEBUG(DEBUG_ERR,("Failed to re-read public addresses file\n"));
3897 talloc_free(mem_ctx);
3902 /* check the previous list of ips and scan for ips that have been
3905 for (i = 0; i < ips->num; i++) {
3906 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
3907 if (ctdb_same_ip(&vnn->public_address, &ips->ips[i].addr)) {
3912 /* we need to delete this ip, no longer available on this node */
3914 struct ctdb_control_ip_iface pub;
3916 DEBUG(DEBUG_NOTICE,("RELOADIPS: IP%s is no longer available on this node. Deleting it.\n", ctdb_addr_to_str(&ips->ips[i].addr)));
3917 pub.addr = ips->ips[i].addr;
3921 ret = ctdb_ctrl_del_public_ip(ctdb, TAKEOVER_TIMEOUT(), CTDB_CURRENT_NODE, &pub);
3923 DEBUG(DEBUG_ERR, ("RELOADIPS: Unable to del public ip:%s from local node\n", ctdb_addr_to_str(&ips->ips[i].addr)));
3930 /* loop over all new ones and check the ones we need to add */
3931 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
3932 for (i = 0; i < ips->num; i++) {
3933 if (ctdb_same_ip(&vnn->public_address, &ips->ips[i].addr)) {
3937 if (i == ips->num) {
3938 struct ctdb_control_ip_iface pub;
3939 const char *ifaces = NULL;
3942 DEBUG(DEBUG_NOTICE,("RELOADIPS: New ip:%s found, adding it.\n", ctdb_addr_to_str(&vnn->public_address)));
3944 pub.addr = vnn->public_address;
3945 pub.mask = vnn->public_netmask_bits;
3948 ifaces = vnn->ifaces[0];
3950 while (vnn->ifaces[iface] != NULL) {
3951 ifaces = talloc_asprintf(vnn, "%s,%s", ifaces, vnn->ifaces[iface]);
3954 pub.len = strlen(ifaces)+1;
3955 memcpy(&pub.iface[0], ifaces, strlen(ifaces)+1);
3957 ret = ctdb_ctrl_add_public_ip(ctdb, TAKEOVER_TIMEOUT(), CTDB_CURRENT_NODE, &pub);
3959 DEBUG(DEBUG_ERR, ("RELOADIPS: Unable to add public ip:%s to local node\n", ctdb_addr_to_str(&vnn->public_address)));
3968 /* This control is sent to force the node to re-read the public addresses file
3969 and drop any addresses we should nnot longer host, and add new addresses
3970 that we are now able to host
3972 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
3974 struct ctdb_reloadips_handle *h;
3975 pid_t parent = getpid();
3977 if (ctdb->reload_ips != NULL) {
3978 talloc_free(ctdb->reload_ips);
3979 ctdb->reload_ips = NULL;
3982 h = talloc(ctdb, struct ctdb_reloadips_handle);
3983 CTDB_NO_MEMORY(ctdb, h);
3988 if (pipe(h->fd) == -1) {
3989 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
3994 h->child = ctdb_fork(ctdb);
3995 if (h->child == (pid_t)-1) {
3996 DEBUG(DEBUG_ERR, ("Failed to fork a child for reloadips\n"));
4004 if (h->child == 0) {
4005 signed char res = 0;
4008 debug_extra = talloc_asprintf(NULL, "reloadips:");
4010 if (switch_from_server_to_client(ctdb, "reloadips-child") != 0) {
4011 DEBUG(DEBUG_CRIT,("ERROR: Failed to switch reloadips child into client mode\n"));
4014 res = ctdb_reloadips_child(ctdb);
4016 DEBUG(DEBUG_ERR,("Failed to reload ips on local node\n"));
4020 write(h->fd[1], &res, 1);
4021 /* make sure we die when our parent dies */
4022 while (ctdb_kill(ctdb, parent, 0) == 0 || errno != ESRCH) {
4028 h->c = talloc_steal(h, c);
4031 set_close_on_exec(h->fd[0]);
4033 talloc_set_destructor(h, ctdb_reloadips_destructor);
4036 h->fde = event_add_fd(ctdb->ev, h, h->fd[0],
4037 EVENT_FD_READ, ctdb_reloadips_child_handler,
4039 tevent_fd_set_auto_close(h->fde);
4041 event_add_timed(ctdb->ev, h,
4042 timeval_current_ofs(120, 0),
4043 ctdb_reloadips_timeout_event, h);
4045 /* we reply later */
4046 *async_reply = true;