ctdb-daemon: Drop interface monitoring
[Samba.git] / ctdb / server / ctdb_takeover.c
blob556b2479bf7290887a7716447afd6dd2e627ad2e
1 /*
2 ctdb ip takeover code
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/>.
21 #include "includes.h"
22 #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
36 /* Flags used in IP allocation algorithms. */
37 struct ctdb_ipflags {
38 bool noiptakeover;
39 bool noiphost;
40 enum ctdb_runstate runstate;
43 struct ctdb_iface {
44 struct ctdb_iface *prev, *next;
45 const char *name;
46 bool link_up;
47 uint32_t references;
50 static const char *ctdb_vnn_iface_string(const struct ctdb_vnn *vnn)
52 if (vnn->iface) {
53 return vnn->iface->name;
56 return "__none__";
59 static int ctdb_add_local_iface(struct ctdb_context *ctdb, const char *iface)
61 struct ctdb_iface *i;
63 /* Verify that we dont have an entry for this ip yet */
64 for (i=ctdb->ifaces;i;i=i->next) {
65 if (strcmp(i->name, iface) == 0) {
66 return 0;
70 /* create a new structure for this interface */
71 i = talloc_zero(ctdb, struct ctdb_iface);
72 CTDB_NO_MEMORY_FATAL(ctdb, i);
73 i->name = talloc_strdup(i, iface);
74 CTDB_NO_MEMORY(ctdb, i->name);
76 * If link_up defaults to true then IPs can be allocated to a
77 * node during the first recovery. However, then an interface
78 * could have its link marked down during the startup event,
79 * causing the IP to move almost immediately. If link_up
80 * defaults to false then, during normal operation, IPs added
81 * to a new interface can't be assigned until a monitor cycle
82 * has occurred and marked the new interfaces up. This makes
83 * IP allocation unpredictable. The following is a neat
84 * compromise: early in startup link_up defaults to false, so
85 * IPs can't be assigned, and after startup IPs can be
86 * assigned immediately.
88 i->link_up = (ctdb->runstate == CTDB_RUNSTATE_RUNNING);
90 DLIST_ADD(ctdb->ifaces, i);
92 return 0;
95 static bool vnn_has_interface_with_name(struct ctdb_vnn *vnn,
96 const char *name)
98 int n;
100 for (n = 0; vnn->ifaces[n] != NULL; n++) {
101 if (strcmp(name, vnn->ifaces[n]) == 0) {
102 return true;
106 return false;
109 /* If any interfaces now have no possible IPs then delete them. This
110 * implementation is naive (i.e. simple) rather than clever
111 * (i.e. complex). Given that this is run on delip and that operation
112 * is rare, this doesn't need to be efficient - it needs to be
113 * foolproof. One alternative is reference counting, where the logic
114 * is distributed and can, therefore, be broken in multiple places.
115 * Another alternative is to build a red-black tree of interfaces that
116 * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
117 * once) and then walking ctdb->ifaces once and deleting those not in
118 * the tree. Let's go to one of those if the naive implementation
119 * causes problems... :-)
121 static void ctdb_remove_orphaned_ifaces(struct ctdb_context *ctdb,
122 struct ctdb_vnn *vnn)
124 struct ctdb_iface *i, *next;
126 /* For each interface, check if there's an IP using it. */
127 for (i = ctdb->ifaces; i != NULL; i = next) {
128 struct ctdb_vnn *tv;
129 bool found;
130 next = i->next;
132 /* Only consider interfaces named in the given VNN. */
133 if (!vnn_has_interface_with_name(vnn, i->name)) {
134 continue;
137 /* Is the "single IP" on this interface? */
138 if ((ctdb->single_ip_vnn != NULL) &&
139 (ctdb->single_ip_vnn->ifaces[0] != NULL) &&
140 (strcmp(i->name, ctdb->single_ip_vnn->ifaces[0]) == 0)) {
141 /* Found, next interface please... */
142 continue;
144 /* Search for a vnn with this interface. */
145 found = false;
146 for (tv=ctdb->vnn; tv; tv=tv->next) {
147 if (vnn_has_interface_with_name(tv, i->name)) {
148 found = true;
149 break;
153 if (!found) {
154 /* None of the VNNs are using this interface. */
155 DLIST_REMOVE(ctdb->ifaces, i);
156 talloc_free(i);
162 static struct ctdb_iface *ctdb_find_iface(struct ctdb_context *ctdb,
163 const char *iface)
165 struct ctdb_iface *i;
167 for (i=ctdb->ifaces;i;i=i->next) {
168 if (strcmp(i->name, iface) == 0) {
169 return i;
173 return NULL;
176 static struct ctdb_iface *ctdb_vnn_best_iface(struct ctdb_context *ctdb,
177 struct ctdb_vnn *vnn)
179 int i;
180 struct ctdb_iface *cur = NULL;
181 struct ctdb_iface *best = NULL;
183 for (i=0; vnn->ifaces[i]; i++) {
185 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
186 if (cur == NULL) {
187 continue;
190 if (!cur->link_up) {
191 continue;
194 if (best == NULL) {
195 best = cur;
196 continue;
199 if (cur->references < best->references) {
200 best = cur;
201 continue;
205 return best;
208 static int32_t ctdb_vnn_assign_iface(struct ctdb_context *ctdb,
209 struct ctdb_vnn *vnn)
211 struct ctdb_iface *best = NULL;
213 if (vnn->iface) {
214 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
215 "still assigned to iface '%s'\n",
216 ctdb_addr_to_str(&vnn->public_address),
217 ctdb_vnn_iface_string(vnn)));
218 return 0;
221 best = ctdb_vnn_best_iface(ctdb, vnn);
222 if (best == NULL) {
223 DEBUG(DEBUG_ERR, (__location__ " public address '%s' "
224 "cannot assign to iface any iface\n",
225 ctdb_addr_to_str(&vnn->public_address)));
226 return -1;
229 vnn->iface = best;
230 best->references++;
231 vnn->pnn = ctdb->pnn;
233 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
234 "now assigned to iface '%s' refs[%d]\n",
235 ctdb_addr_to_str(&vnn->public_address),
236 ctdb_vnn_iface_string(vnn),
237 best->references));
238 return 0;
241 static void ctdb_vnn_unassign_iface(struct ctdb_context *ctdb,
242 struct ctdb_vnn *vnn)
244 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
245 "now unassigned (old iface '%s' refs[%d])\n",
246 ctdb_addr_to_str(&vnn->public_address),
247 ctdb_vnn_iface_string(vnn),
248 vnn->iface?vnn->iface->references:0));
249 if (vnn->iface) {
250 vnn->iface->references--;
252 vnn->iface = NULL;
253 if (vnn->pnn == ctdb->pnn) {
254 vnn->pnn = -1;
258 static bool ctdb_vnn_available(struct ctdb_context *ctdb,
259 struct ctdb_vnn *vnn)
261 int i;
263 if (vnn->delete_pending) {
264 return false;
267 if (vnn->iface && vnn->iface->link_up) {
268 return true;
271 for (i=0; vnn->ifaces[i]; i++) {
272 struct ctdb_iface *cur;
274 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
275 if (cur == NULL) {
276 continue;
279 if (cur->link_up) {
280 return true;
284 return false;
287 struct ctdb_takeover_arp {
288 struct ctdb_context *ctdb;
289 uint32_t count;
290 ctdb_sock_addr addr;
291 struct ctdb_tcp_array *tcparray;
292 struct ctdb_vnn *vnn;
297 lists of tcp endpoints
299 struct ctdb_tcp_list {
300 struct ctdb_tcp_list *prev, *next;
301 struct ctdb_tcp_connection connection;
305 list of clients to kill on IP release
307 struct ctdb_client_ip {
308 struct ctdb_client_ip *prev, *next;
309 struct ctdb_context *ctdb;
310 ctdb_sock_addr addr;
311 uint32_t client_id;
316 send a gratuitous arp
318 static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te,
319 struct timeval t, void *private_data)
321 struct ctdb_takeover_arp *arp = talloc_get_type(private_data,
322 struct ctdb_takeover_arp);
323 int i, ret;
324 struct ctdb_tcp_array *tcparray;
325 const char *iface = ctdb_vnn_iface_string(arp->vnn);
327 ret = ctdb_sys_send_arp(&arp->addr, iface);
328 if (ret != 0) {
329 DEBUG(DEBUG_CRIT,(__location__ " sending of arp failed on iface '%s' (%s)\n",
330 iface, strerror(errno)));
333 tcparray = arp->tcparray;
334 if (tcparray) {
335 for (i=0;i<tcparray->num;i++) {
336 struct ctdb_tcp_connection *tcon;
338 tcon = &tcparray->connections[i];
339 DEBUG(DEBUG_INFO,("sending tcp tickle ack for %u->%s:%u\n",
340 (unsigned)ntohs(tcon->dst_addr.ip.sin_port),
341 ctdb_addr_to_str(&tcon->src_addr),
342 (unsigned)ntohs(tcon->src_addr.ip.sin_port)));
343 ret = ctdb_sys_send_tcp(
344 &tcon->src_addr,
345 &tcon->dst_addr,
346 0, 0, 0);
347 if (ret != 0) {
348 DEBUG(DEBUG_CRIT,(__location__ " Failed to send tcp tickle ack for %s\n",
349 ctdb_addr_to_str(&tcon->src_addr)));
354 arp->count++;
356 if (arp->count == CTDB_ARP_REPEAT) {
357 talloc_free(arp);
358 return;
361 event_add_timed(arp->ctdb->ev, arp->vnn->takeover_ctx,
362 timeval_current_ofs(CTDB_ARP_INTERVAL, 100000),
363 ctdb_control_send_arp, arp);
366 static int32_t ctdb_announce_vnn_iface(struct ctdb_context *ctdb,
367 struct ctdb_vnn *vnn)
369 struct ctdb_takeover_arp *arp;
370 struct ctdb_tcp_array *tcparray;
372 if (!vnn->takeover_ctx) {
373 vnn->takeover_ctx = talloc_new(vnn);
374 if (!vnn->takeover_ctx) {
375 return -1;
379 arp = talloc_zero(vnn->takeover_ctx, struct ctdb_takeover_arp);
380 if (!arp) {
381 return -1;
384 arp->ctdb = ctdb;
385 arp->addr = vnn->public_address;
386 arp->vnn = vnn;
388 tcparray = vnn->tcp_array;
389 if (tcparray) {
390 /* add all of the known tcp connections for this IP to the
391 list of tcp connections to send tickle acks for */
392 arp->tcparray = talloc_steal(arp, tcparray);
394 vnn->tcp_array = NULL;
395 vnn->tcp_update_needed = true;
398 event_add_timed(arp->ctdb->ev, vnn->takeover_ctx,
399 timeval_zero(), ctdb_control_send_arp, arp);
401 return 0;
404 struct takeover_callback_state {
405 struct ctdb_req_control *c;
406 ctdb_sock_addr *addr;
407 struct ctdb_vnn *vnn;
410 struct ctdb_do_takeip_state {
411 struct ctdb_req_control *c;
412 struct ctdb_vnn *vnn;
416 called when takeip event finishes
418 static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
419 void *private_data)
421 struct ctdb_do_takeip_state *state =
422 talloc_get_type(private_data, struct ctdb_do_takeip_state);
423 int32_t ret;
424 TDB_DATA data;
426 if (status != 0) {
427 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
429 if (status == -ETIME) {
430 ctdb_ban_self(ctdb);
432 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
433 ctdb_addr_to_str(&state->vnn->public_address),
434 ctdb_vnn_iface_string(state->vnn)));
435 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
437 node->flags |= NODE_FLAGS_UNHEALTHY;
438 talloc_free(state);
439 return;
442 if (ctdb->do_checkpublicip) {
444 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
445 if (ret != 0) {
446 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
447 talloc_free(state);
448 return;
453 data.dptr = (uint8_t *)ctdb_addr_to_str(&state->vnn->public_address);
454 data.dsize = strlen((char *)data.dptr) + 1;
455 DEBUG(DEBUG_INFO,(__location__ " sending TAKE_IP for '%s'\n", data.dptr));
457 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_TAKE_IP, data);
460 /* the control succeeded */
461 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
462 talloc_free(state);
463 return;
466 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state *state)
468 state->vnn->update_in_flight = false;
469 return 0;
473 take over an ip address
475 static int32_t ctdb_do_takeip(struct ctdb_context *ctdb,
476 struct ctdb_req_control *c,
477 struct ctdb_vnn *vnn)
479 int ret;
480 struct ctdb_do_takeip_state *state;
482 if (vnn->update_in_flight) {
483 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u rejected "
484 "update for this IP already in flight\n",
485 ctdb_addr_to_str(&vnn->public_address),
486 vnn->public_netmask_bits));
487 return -1;
490 ret = ctdb_vnn_assign_iface(ctdb, vnn);
491 if (ret != 0) {
492 DEBUG(DEBUG_ERR,("Takeover of IP %s/%u failed to "
493 "assign a usable interface\n",
494 ctdb_addr_to_str(&vnn->public_address),
495 vnn->public_netmask_bits));
496 return -1;
499 state = talloc(vnn, struct ctdb_do_takeip_state);
500 CTDB_NO_MEMORY(ctdb, state);
502 state->c = talloc_steal(ctdb, c);
503 state->vnn = vnn;
505 vnn->update_in_flight = true;
506 talloc_set_destructor(state, ctdb_takeip_destructor);
508 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u on interface %s\n",
509 ctdb_addr_to_str(&vnn->public_address),
510 vnn->public_netmask_bits,
511 ctdb_vnn_iface_string(vnn)));
513 ret = ctdb_event_script_callback(ctdb,
514 state,
515 ctdb_do_takeip_callback,
516 state,
517 CTDB_EVENT_TAKE_IP,
518 "%s %s %u",
519 ctdb_vnn_iface_string(vnn),
520 ctdb_addr_to_str(&vnn->public_address),
521 vnn->public_netmask_bits);
523 if (ret != 0) {
524 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
525 ctdb_addr_to_str(&vnn->public_address),
526 ctdb_vnn_iface_string(vnn)));
527 talloc_free(state);
528 return -1;
531 return 0;
534 struct ctdb_do_updateip_state {
535 struct ctdb_req_control *c;
536 struct ctdb_iface *old;
537 struct ctdb_vnn *vnn;
541 called when updateip event finishes
543 static void ctdb_do_updateip_callback(struct ctdb_context *ctdb, int status,
544 void *private_data)
546 struct ctdb_do_updateip_state *state =
547 talloc_get_type(private_data, struct ctdb_do_updateip_state);
548 int32_t ret;
550 if (status != 0) {
551 if (status == -ETIME) {
552 ctdb_ban_self(ctdb);
554 DEBUG(DEBUG_ERR,(__location__ " Failed to move IP %s from interface %s to %s\n",
555 ctdb_addr_to_str(&state->vnn->public_address),
556 state->old->name,
557 ctdb_vnn_iface_string(state->vnn)));
560 * All we can do is reset the old interface
561 * and let the next run fix it
563 ctdb_vnn_unassign_iface(ctdb, state->vnn);
564 state->vnn->iface = state->old;
565 state->vnn->iface->references++;
567 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
568 talloc_free(state);
569 return;
572 if (ctdb->do_checkpublicip) {
574 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
575 if (ret != 0) {
576 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
577 talloc_free(state);
578 return;
583 /* the control succeeded */
584 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
585 talloc_free(state);
586 return;
589 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state *state)
591 state->vnn->update_in_flight = false;
592 return 0;
596 update (move) an ip address
598 static int32_t ctdb_do_updateip(struct ctdb_context *ctdb,
599 struct ctdb_req_control *c,
600 struct ctdb_vnn *vnn)
602 int ret;
603 struct ctdb_do_updateip_state *state;
604 struct ctdb_iface *old = vnn->iface;
605 const char *new_name;
607 if (vnn->update_in_flight) {
608 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u rejected "
609 "update for this IP already in flight\n",
610 ctdb_addr_to_str(&vnn->public_address),
611 vnn->public_netmask_bits));
612 return -1;
615 ctdb_vnn_unassign_iface(ctdb, vnn);
616 ret = ctdb_vnn_assign_iface(ctdb, vnn);
617 if (ret != 0) {
618 DEBUG(DEBUG_ERR,("update of IP %s/%u failed to "
619 "assin a usable interface (old iface '%s')\n",
620 ctdb_addr_to_str(&vnn->public_address),
621 vnn->public_netmask_bits,
622 old->name));
623 return -1;
626 new_name = ctdb_vnn_iface_string(vnn);
627 if (old->name != NULL && new_name != NULL && !strcmp(old->name, new_name)) {
628 /* A benign update from one interface onto itself.
629 * no need to run the eventscripts in this case, just return
630 * success.
632 ctdb_request_control_reply(ctdb, c, NULL, 0, NULL);
633 return 0;
636 state = talloc(vnn, struct ctdb_do_updateip_state);
637 CTDB_NO_MEMORY(ctdb, state);
639 state->c = talloc_steal(ctdb, c);
640 state->old = old;
641 state->vnn = vnn;
643 vnn->update_in_flight = true;
644 talloc_set_destructor(state, ctdb_updateip_destructor);
646 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u from "
647 "interface %s to %s\n",
648 ctdb_addr_to_str(&vnn->public_address),
649 vnn->public_netmask_bits,
650 old->name,
651 new_name));
653 ret = ctdb_event_script_callback(ctdb,
654 state,
655 ctdb_do_updateip_callback,
656 state,
657 CTDB_EVENT_UPDATE_IP,
658 "%s %s %s %u",
659 state->old->name,
660 new_name,
661 ctdb_addr_to_str(&vnn->public_address),
662 vnn->public_netmask_bits);
663 if (ret != 0) {
664 DEBUG(DEBUG_ERR,(__location__ " Failed update IP %s from interface %s to %s\n",
665 ctdb_addr_to_str(&vnn->public_address),
666 old->name, new_name));
667 talloc_free(state);
668 return -1;
671 return 0;
675 Find the vnn of the node that has a public ip address
676 returns -1 if the address is not known as a public address
678 static struct ctdb_vnn *find_public_ip_vnn(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
680 struct ctdb_vnn *vnn;
682 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
683 if (ctdb_same_ip(&vnn->public_address, addr)) {
684 return vnn;
688 return NULL;
692 take over an ip address
694 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
695 struct ctdb_req_control *c,
696 TDB_DATA indata,
697 bool *async_reply)
699 int ret;
700 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
701 struct ctdb_vnn *vnn;
702 bool have_ip = false;
703 bool do_updateip = false;
704 bool do_takeip = false;
705 struct ctdb_iface *best_iface = NULL;
707 if (pip->pnn != ctdb->pnn) {
708 DEBUG(DEBUG_ERR,(__location__" takeoverip called for an ip '%s' "
709 "with pnn %d, but we're node %d\n",
710 ctdb_addr_to_str(&pip->addr),
711 pip->pnn, ctdb->pnn));
712 return -1;
715 /* update out vnn list */
716 vnn = find_public_ip_vnn(ctdb, &pip->addr);
717 if (vnn == NULL) {
718 DEBUG(DEBUG_INFO,("takeoverip called for an ip '%s' that is not a public address\n",
719 ctdb_addr_to_str(&pip->addr)));
720 return 0;
723 if (ctdb->do_checkpublicip) {
724 have_ip = ctdb_sys_have_ip(&pip->addr);
726 best_iface = ctdb_vnn_best_iface(ctdb, vnn);
727 if (best_iface == NULL) {
728 DEBUG(DEBUG_ERR,("takeoverip of IP %s/%u failed to find"
729 "a usable interface (old %s, have_ip %d)\n",
730 ctdb_addr_to_str(&vnn->public_address),
731 vnn->public_netmask_bits,
732 ctdb_vnn_iface_string(vnn),
733 have_ip));
734 return -1;
737 if (vnn->iface == NULL && vnn->pnn == -1 && have_ip && best_iface != NULL) {
738 DEBUG(DEBUG_ERR,("Taking over newly created ip\n"));
739 have_ip = false;
743 if (vnn->iface == NULL && have_ip) {
744 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
745 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
746 ctdb_addr_to_str(&vnn->public_address)));
747 return 0;
750 if (vnn->pnn != ctdb->pnn && have_ip && vnn->pnn != -1) {
751 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
752 "and we have it on iface[%s], but it was assigned to node %d"
753 "and we are node %d, banning ourself\n",
754 ctdb_addr_to_str(&vnn->public_address),
755 ctdb_vnn_iface_string(vnn), vnn->pnn, ctdb->pnn));
756 ctdb_ban_self(ctdb);
757 return -1;
760 if (vnn->pnn == -1 && have_ip) {
761 vnn->pnn = ctdb->pnn;
762 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
763 "and we already have it on iface[%s], update local daemon\n",
764 ctdb_addr_to_str(&vnn->public_address),
765 ctdb_vnn_iface_string(vnn)));
766 return 0;
769 if (vnn->iface) {
770 if (vnn->iface != best_iface) {
771 if (!vnn->iface->link_up) {
772 do_updateip = true;
773 } else if (vnn->iface->references > (best_iface->references + 1)) {
774 /* only move when the rebalance gains something */
775 do_updateip = true;
780 if (!have_ip) {
781 if (do_updateip) {
782 ctdb_vnn_unassign_iface(ctdb, vnn);
783 do_updateip = false;
785 do_takeip = true;
788 if (do_takeip) {
789 ret = ctdb_do_takeip(ctdb, c, vnn);
790 if (ret != 0) {
791 return -1;
793 } else if (do_updateip) {
794 ret = ctdb_do_updateip(ctdb, c, vnn);
795 if (ret != 0) {
796 return -1;
798 } else {
800 * The interface is up and the kernel known the ip
801 * => do nothing
803 DEBUG(DEBUG_INFO,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
804 ctdb_addr_to_str(&pip->addr),
805 vnn->public_netmask_bits,
806 ctdb_vnn_iface_string(vnn)));
807 return 0;
810 /* tell ctdb_control.c that we will be replying asynchronously */
811 *async_reply = true;
813 return 0;
817 takeover an ip address old v4 style
819 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb,
820 struct ctdb_req_control *c,
821 TDB_DATA indata,
822 bool *async_reply)
824 TDB_DATA data;
826 data.dsize = sizeof(struct ctdb_public_ip);
827 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
828 CTDB_NO_MEMORY(ctdb, data.dptr);
830 memcpy(data.dptr, indata.dptr, indata.dsize);
831 return ctdb_control_takeover_ip(ctdb, c, data, async_reply);
835 kill any clients that are registered with a IP that is being released
837 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
839 struct ctdb_client_ip *ip;
841 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
842 ctdb_addr_to_str(addr)));
844 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
845 ctdb_sock_addr tmp_addr;
847 tmp_addr = ip->addr;
848 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
849 ip->client_id,
850 ctdb_addr_to_str(&ip->addr)));
852 if (ctdb_same_ip(&tmp_addr, addr)) {
853 struct ctdb_client *client = ctdb_reqid_find(ctdb,
854 ip->client_id,
855 struct ctdb_client);
856 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
857 ip->client_id,
858 ctdb_addr_to_str(&ip->addr),
859 client->pid));
861 if (client->pid != 0) {
862 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
863 (unsigned)client->pid,
864 ctdb_addr_to_str(addr),
865 ip->client_id));
866 kill(client->pid, SIGKILL);
872 static void do_delete_ip(struct ctdb_context *ctdb, struct ctdb_vnn *vnn)
874 DLIST_REMOVE(ctdb->vnn, vnn);
875 ctdb_vnn_unassign_iface(ctdb, vnn);
876 ctdb_remove_orphaned_ifaces(ctdb, vnn);
877 talloc_free(vnn);
881 called when releaseip event finishes
883 static void release_ip_callback(struct ctdb_context *ctdb, int status,
884 void *private_data)
886 struct takeover_callback_state *state =
887 talloc_get_type(private_data, struct takeover_callback_state);
888 TDB_DATA data;
890 if (status == -ETIME) {
891 ctdb_ban_self(ctdb);
894 if (ctdb->do_checkpublicip && ctdb_sys_have_ip(state->addr)) {
895 DEBUG(DEBUG_ERR, ("IP %s still hosted during release IP callback, failing\n",
896 ctdb_addr_to_str(state->addr)));
897 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
898 talloc_free(state);
899 return;
902 /* send a message to all clients of this node telling them
903 that the cluster has been reconfigured and they should
904 release any sockets on this IP */
905 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
906 CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
907 data.dsize = strlen((char *)data.dptr)+1;
909 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
911 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
913 /* kill clients that have registered with this IP */
914 release_kill_clients(ctdb, state->addr);
916 ctdb_vnn_unassign_iface(ctdb, state->vnn);
918 /* Process the IP if it has been marked for deletion */
919 if (state->vnn->delete_pending) {
920 do_delete_ip(ctdb, state->vnn);
921 state->vnn = NULL;
924 /* the control succeeded */
925 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
926 talloc_free(state);
929 static int ctdb_releaseip_destructor(struct takeover_callback_state *state)
931 if (state->vnn != NULL) {
932 state->vnn->update_in_flight = false;
934 return 0;
938 release an ip address
940 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
941 struct ctdb_req_control *c,
942 TDB_DATA indata,
943 bool *async_reply)
945 int ret;
946 struct takeover_callback_state *state;
947 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
948 struct ctdb_vnn *vnn;
949 char *iface;
951 /* update our vnn list */
952 vnn = find_public_ip_vnn(ctdb, &pip->addr);
953 if (vnn == NULL) {
954 DEBUG(DEBUG_INFO,("releaseip called for an ip '%s' that is not a public address\n",
955 ctdb_addr_to_str(&pip->addr)));
956 return 0;
958 vnn->pnn = pip->pnn;
960 /* stop any previous arps */
961 talloc_free(vnn->takeover_ctx);
962 vnn->takeover_ctx = NULL;
964 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
965 * lazy multicast to drop an IP from any node that isn't the
966 * intended new node. The following causes makes ctdbd ignore
967 * a release for any address it doesn't host.
969 if (ctdb->do_checkpublicip) {
970 if (!ctdb_sys_have_ip(&pip->addr)) {
971 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
972 ctdb_addr_to_str(&pip->addr),
973 vnn->public_netmask_bits,
974 ctdb_vnn_iface_string(vnn)));
975 ctdb_vnn_unassign_iface(ctdb, vnn);
976 return 0;
978 } else {
979 if (vnn->iface == NULL) {
980 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u (ip not held)\n",
981 ctdb_addr_to_str(&pip->addr),
982 vnn->public_netmask_bits));
983 return 0;
987 /* There is a potential race between take_ip and us because we
988 * update the VNN via a callback that run when the
989 * eventscripts have been run. Avoid the race by allowing one
990 * update to be in flight at a time.
992 if (vnn->update_in_flight) {
993 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u rejected "
994 "update for this IP already in flight\n",
995 ctdb_addr_to_str(&vnn->public_address),
996 vnn->public_netmask_bits));
997 return -1;
1000 iface = strdup(ctdb_vnn_iface_string(vnn));
1002 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s node:%d\n",
1003 ctdb_addr_to_str(&pip->addr),
1004 vnn->public_netmask_bits,
1005 iface,
1006 pip->pnn));
1008 state = talloc(ctdb, struct takeover_callback_state);
1009 if (state == NULL) {
1010 ctdb_set_error(ctdb, "Out of memory at %s:%d",
1011 __FILE__, __LINE__);
1012 free(iface);
1013 return -1;
1016 state->c = talloc_steal(state, c);
1017 state->addr = talloc(state, ctdb_sock_addr);
1018 if (state->addr == NULL) {
1019 ctdb_set_error(ctdb, "Out of memory at %s:%d",
1020 __FILE__, __LINE__);
1021 free(iface);
1022 talloc_free(state);
1023 return -1;
1025 *state->addr = pip->addr;
1026 state->vnn = vnn;
1028 vnn->update_in_flight = true;
1029 talloc_set_destructor(state, ctdb_releaseip_destructor);
1031 ret = ctdb_event_script_callback(ctdb,
1032 state, release_ip_callback, state,
1033 CTDB_EVENT_RELEASE_IP,
1034 "%s %s %u",
1035 iface,
1036 ctdb_addr_to_str(&pip->addr),
1037 vnn->public_netmask_bits);
1038 free(iface);
1039 if (ret != 0) {
1040 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
1041 ctdb_addr_to_str(&pip->addr),
1042 ctdb_vnn_iface_string(vnn)));
1043 talloc_free(state);
1044 return -1;
1047 /* tell the control that we will be reply asynchronously */
1048 *async_reply = true;
1049 return 0;
1053 release an ip address old v4 style
1055 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb,
1056 struct ctdb_req_control *c,
1057 TDB_DATA indata,
1058 bool *async_reply)
1060 TDB_DATA data;
1062 data.dsize = sizeof(struct ctdb_public_ip);
1063 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
1064 CTDB_NO_MEMORY(ctdb, data.dptr);
1066 memcpy(data.dptr, indata.dptr, indata.dsize);
1067 return ctdb_control_release_ip(ctdb, c, data, async_reply);
1071 static int ctdb_add_public_address(struct ctdb_context *ctdb,
1072 ctdb_sock_addr *addr,
1073 unsigned mask, const char *ifaces,
1074 bool check_address)
1076 struct ctdb_vnn *vnn;
1077 uint32_t num = 0;
1078 char *tmp;
1079 const char *iface;
1080 int i;
1081 int ret;
1083 tmp = strdup(ifaces);
1084 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1085 if (!ctdb_sys_check_iface_exists(iface)) {
1086 DEBUG(DEBUG_CRIT,("Interface %s does not exist. Can not add public-address : %s\n", iface, ctdb_addr_to_str(addr)));
1087 free(tmp);
1088 return -1;
1091 free(tmp);
1093 /* Verify that we dont have an entry for this ip yet */
1094 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1095 if (ctdb_same_sockaddr(addr, &vnn->public_address)) {
1096 DEBUG(DEBUG_CRIT,("Same ip '%s' specified multiple times in the public address list \n",
1097 ctdb_addr_to_str(addr)));
1098 return -1;
1102 /* create a new vnn structure for this ip address */
1103 vnn = talloc_zero(ctdb, struct ctdb_vnn);
1104 CTDB_NO_MEMORY_FATAL(ctdb, vnn);
1105 vnn->ifaces = talloc_array(vnn, const char *, num + 2);
1106 tmp = talloc_strdup(vnn, ifaces);
1107 CTDB_NO_MEMORY_FATAL(ctdb, tmp);
1108 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1109 vnn->ifaces = talloc_realloc(vnn, vnn->ifaces, const char *, num + 2);
1110 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces);
1111 vnn->ifaces[num] = talloc_strdup(vnn, iface);
1112 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces[num]);
1113 num++;
1115 talloc_free(tmp);
1116 vnn->ifaces[num] = NULL;
1117 vnn->public_address = *addr;
1118 vnn->public_netmask_bits = mask;
1119 vnn->pnn = -1;
1120 if (check_address) {
1121 if (ctdb_sys_have_ip(addr)) {
1122 DEBUG(DEBUG_ERR,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr), ctdb->pnn));
1123 vnn->pnn = ctdb->pnn;
1127 for (i=0; vnn->ifaces[i]; i++) {
1128 ret = ctdb_add_local_iface(ctdb, vnn->ifaces[i]);
1129 if (ret != 0) {
1130 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1131 "for public_address[%s]\n",
1132 vnn->ifaces[i], ctdb_addr_to_str(addr)));
1133 talloc_free(vnn);
1134 return -1;
1138 DLIST_ADD(ctdb->vnn, vnn);
1140 return 0;
1144 setup the public address lists from a file
1146 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
1148 char **lines;
1149 int nlines;
1150 int i;
1152 lines = file_lines_load(ctdb->public_addresses_file, &nlines, 0, ctdb);
1153 if (lines == NULL) {
1154 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", ctdb->public_addresses_file);
1155 return -1;
1157 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1158 nlines--;
1161 for (i=0;i<nlines;i++) {
1162 unsigned mask;
1163 ctdb_sock_addr addr;
1164 const char *addrstr;
1165 const char *ifaces;
1166 char *tok, *line;
1168 line = lines[i];
1169 while ((*line == ' ') || (*line == '\t')) {
1170 line++;
1172 if (*line == '#') {
1173 continue;
1175 if (strcmp(line, "") == 0) {
1176 continue;
1178 tok = strtok(line, " \t");
1179 addrstr = tok;
1180 tok = strtok(NULL, " \t");
1181 if (tok == NULL) {
1182 if (NULL == ctdb->default_public_interface) {
1183 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
1184 i+1));
1185 talloc_free(lines);
1186 return -1;
1188 ifaces = ctdb->default_public_interface;
1189 } else {
1190 ifaces = tok;
1193 if (!addrstr || !parse_ip_mask(addrstr, ifaces, &addr, &mask)) {
1194 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
1195 talloc_free(lines);
1196 return -1;
1198 if (ctdb_add_public_address(ctdb, &addr, mask, ifaces, check_addresses)) {
1199 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
1200 talloc_free(lines);
1201 return -1;
1206 talloc_free(lines);
1207 return 0;
1210 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1211 const char *iface,
1212 const char *ip)
1214 struct ctdb_vnn *svnn;
1215 struct ctdb_iface *cur = NULL;
1216 bool ok;
1217 int ret;
1219 svnn = talloc_zero(ctdb, struct ctdb_vnn);
1220 CTDB_NO_MEMORY(ctdb, svnn);
1222 svnn->ifaces = talloc_array(svnn, const char *, 2);
1223 CTDB_NO_MEMORY(ctdb, svnn->ifaces);
1224 svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
1225 CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
1226 svnn->ifaces[1] = NULL;
1228 ok = parse_ip(ip, iface, 0, &svnn->public_address);
1229 if (!ok) {
1230 talloc_free(svnn);
1231 return -1;
1234 ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
1235 if (ret != 0) {
1236 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1237 "for single_ip[%s]\n",
1238 svnn->ifaces[0],
1239 ctdb_addr_to_str(&svnn->public_address)));
1240 talloc_free(svnn);
1241 return -1;
1244 /* assume the single public ip interface is initially "good" */
1245 cur = ctdb_find_iface(ctdb, iface);
1246 if (cur == NULL) {
1247 DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
1248 return -1;
1250 cur->link_up = true;
1252 ret = ctdb_vnn_assign_iface(ctdb, svnn);
1253 if (ret != 0) {
1254 talloc_free(svnn);
1255 return -1;
1258 ctdb->single_ip_vnn = svnn;
1259 return 0;
1262 struct ctdb_public_ip_list {
1263 struct ctdb_public_ip_list *next;
1264 uint32_t pnn;
1265 ctdb_sock_addr addr;
1268 /* Given a physical node, return the number of
1269 public addresses that is currently assigned to this node.
1271 static int node_ip_coverage(struct ctdb_context *ctdb,
1272 int32_t pnn,
1273 struct ctdb_public_ip_list *ips)
1275 int num=0;
1277 for (;ips;ips=ips->next) {
1278 if (ips->pnn == pnn) {
1279 num++;
1282 return num;
1286 /* Can the given node host the given IP: is the public IP known to the
1287 * node and is NOIPHOST unset?
1289 static bool can_node_host_ip(struct ctdb_context *ctdb, int32_t pnn,
1290 struct ctdb_ipflags ipflags,
1291 struct ctdb_public_ip_list *ip)
1293 struct ctdb_all_public_ips *public_ips;
1294 int i;
1296 if (ipflags.noiphost) {
1297 return false;
1300 public_ips = ctdb->nodes[pnn]->available_public_ips;
1302 if (public_ips == NULL) {
1303 return false;
1306 for (i=0; i<public_ips->num; i++) {
1307 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
1308 /* yes, this node can serve this public ip */
1309 return true;
1313 return false;
1316 static bool can_node_takeover_ip(struct ctdb_context *ctdb, int32_t pnn,
1317 struct ctdb_ipflags ipflags,
1318 struct ctdb_public_ip_list *ip)
1320 if (ipflags.noiptakeover) {
1321 return false;
1324 return can_node_host_ip(ctdb, pnn, ipflags, ip);
1327 /* search the node lists list for a node to takeover this ip.
1328 pick the node that currently are serving the least number of ips
1329 so that the ips get spread out evenly.
1331 static int find_takeover_node(struct ctdb_context *ctdb,
1332 struct ctdb_ipflags *ipflags,
1333 struct ctdb_public_ip_list *ip,
1334 struct ctdb_public_ip_list *all_ips)
1336 int pnn, min=0, num;
1337 int i, numnodes;
1339 numnodes = talloc_array_length(ipflags);
1340 pnn = -1;
1341 for (i=0; i<numnodes; i++) {
1342 /* verify that this node can serve this ip */
1343 if (!can_node_takeover_ip(ctdb, i, ipflags[i], ip)) {
1344 /* no it couldnt so skip to the next node */
1345 continue;
1348 num = node_ip_coverage(ctdb, i, all_ips);
1349 /* was this the first node we checked ? */
1350 if (pnn == -1) {
1351 pnn = i;
1352 min = num;
1353 } else {
1354 if (num < min) {
1355 pnn = i;
1356 min = num;
1360 if (pnn == -1) {
1361 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
1362 ctdb_addr_to_str(&ip->addr)));
1364 return -1;
1367 ip->pnn = pnn;
1368 return 0;
1371 #define IP_KEYLEN 4
1372 static uint32_t *ip_key(ctdb_sock_addr *ip)
1374 static uint32_t key[IP_KEYLEN];
1376 bzero(key, sizeof(key));
1378 switch (ip->sa.sa_family) {
1379 case AF_INET:
1380 key[3] = htonl(ip->ip.sin_addr.s_addr);
1381 break;
1382 case AF_INET6: {
1383 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1384 key[0] = htonl(s6_a32[0]);
1385 key[1] = htonl(s6_a32[1]);
1386 key[2] = htonl(s6_a32[2]);
1387 key[3] = htonl(s6_a32[3]);
1388 break;
1390 default:
1391 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1392 return key;
1395 return key;
1398 static void *add_ip_callback(void *parm, void *data)
1400 struct ctdb_public_ip_list *this_ip = parm;
1401 struct ctdb_public_ip_list *prev_ip = data;
1403 if (prev_ip == NULL) {
1404 return parm;
1406 if (this_ip->pnn == -1) {
1407 this_ip->pnn = prev_ip->pnn;
1410 return parm;
1413 static int getips_count_callback(void *param, void *data)
1415 struct ctdb_public_ip_list **ip_list = (struct ctdb_public_ip_list **)param;
1416 struct ctdb_public_ip_list *new_ip = (struct ctdb_public_ip_list *)data;
1418 new_ip->next = *ip_list;
1419 *ip_list = new_ip;
1420 return 0;
1423 static struct ctdb_public_ip_list *
1424 create_merged_ip_list(struct ctdb_context *ctdb)
1426 int i, j;
1427 struct ctdb_public_ip_list *ip_list;
1428 struct ctdb_all_public_ips *public_ips;
1430 if (ctdb->ip_tree != NULL) {
1431 talloc_free(ctdb->ip_tree);
1432 ctdb->ip_tree = NULL;
1434 ctdb->ip_tree = trbt_create(ctdb, 0);
1436 for (i=0;i<ctdb->num_nodes;i++) {
1437 public_ips = ctdb->nodes[i]->known_public_ips;
1439 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
1440 continue;
1443 /* there were no public ips for this node */
1444 if (public_ips == NULL) {
1445 continue;
1448 for (j=0;j<public_ips->num;j++) {
1449 struct ctdb_public_ip_list *tmp_ip;
1451 tmp_ip = talloc_zero(ctdb->ip_tree, struct ctdb_public_ip_list);
1452 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
1453 /* Do not use information about IP addresses hosted
1454 * on other nodes, it may not be accurate */
1455 if (public_ips->ips[j].pnn == ctdb->nodes[i]->pnn) {
1456 tmp_ip->pnn = public_ips->ips[j].pnn;
1457 } else {
1458 tmp_ip->pnn = -1;
1460 tmp_ip->addr = public_ips->ips[j].addr;
1461 tmp_ip->next = NULL;
1463 trbt_insertarray32_callback(ctdb->ip_tree,
1464 IP_KEYLEN, ip_key(&public_ips->ips[j].addr),
1465 add_ip_callback,
1466 tmp_ip);
1470 ip_list = NULL;
1471 trbt_traversearray32(ctdb->ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
1473 return ip_list;
1477 * This is the length of the longtest common prefix between the IPs.
1478 * It is calculated by XOR-ing the 2 IPs together and counting the
1479 * number of leading zeroes. The implementation means that all
1480 * addresses end up being 128 bits long.
1482 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1483 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1484 * lots of nodes and IP addresses?
1486 static uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
1488 uint32_t ip1_k[IP_KEYLEN];
1489 uint32_t *t;
1490 int i;
1491 uint32_t x;
1493 uint32_t distance = 0;
1495 memcpy(ip1_k, ip_key(ip1), sizeof(ip1_k));
1496 t = ip_key(ip2);
1497 for (i=0; i<IP_KEYLEN; i++) {
1498 x = ip1_k[i] ^ t[i];
1499 if (x == 0) {
1500 distance += 32;
1501 } else {
1502 /* Count number of leading zeroes.
1503 * FIXME? This could be optimised...
1505 while ((x & (1 << 31)) == 0) {
1506 x <<= 1;
1507 distance += 1;
1512 return distance;
1515 /* Calculate the IP distance for the given IP relative to IPs on the
1516 given node. The ips argument is generally the all_ips variable
1517 used in the main part of the algorithm.
1519 static uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1520 struct ctdb_public_ip_list *ips,
1521 int pnn)
1523 struct ctdb_public_ip_list *t;
1524 uint32_t d;
1526 uint32_t sum = 0;
1528 for (t=ips; t != NULL; t=t->next) {
1529 if (t->pnn != pnn) {
1530 continue;
1533 /* Optimisation: We never calculate the distance
1534 * between an address and itself. This allows us to
1535 * calculate the effect of removing an address from a
1536 * node by simply calculating the distance between
1537 * that address and all of the exitsing addresses.
1538 * Moreover, we assume that we're only ever dealing
1539 * with addresses from all_ips so we can identify an
1540 * address via a pointer rather than doing a more
1541 * expensive address comparison. */
1542 if (&(t->addr) == ip) {
1543 continue;
1546 d = ip_distance(ip, &(t->addr));
1547 sum += d * d; /* Cheaper than pulling in math.h :-) */
1550 return sum;
1553 /* Return the LCP2 imbalance metric for addresses currently assigned
1554 to the given node.
1556 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn)
1558 struct ctdb_public_ip_list *t;
1560 uint32_t imbalance = 0;
1562 for (t=all_ips; t!=NULL; t=t->next) {
1563 if (t->pnn != pnn) {
1564 continue;
1566 /* Pass the rest of the IPs rather than the whole
1567 all_ips input list.
1569 imbalance += ip_distance_2_sum(&(t->addr), t->next, pnn);
1572 return imbalance;
1575 /* Allocate any unassigned IPs just by looping through the IPs and
1576 * finding the best node for each.
1578 static void basic_allocate_unassigned(struct ctdb_context *ctdb,
1579 struct ctdb_ipflags *ipflags,
1580 struct ctdb_public_ip_list *all_ips)
1582 struct ctdb_public_ip_list *tmp_ip;
1584 /* loop over all ip's and find a physical node to cover for
1585 each unassigned ip.
1587 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1588 if (tmp_ip->pnn == -1) {
1589 if (find_takeover_node(ctdb, ipflags, tmp_ip, all_ips)) {
1590 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1591 ctdb_addr_to_str(&tmp_ip->addr)));
1597 /* Basic non-deterministic rebalancing algorithm.
1599 static void basic_failback(struct ctdb_context *ctdb,
1600 struct ctdb_ipflags *ipflags,
1601 struct ctdb_public_ip_list *all_ips,
1602 int num_ips)
1604 int i, numnodes;
1605 int maxnode, maxnum, minnode, minnum, num, retries;
1606 struct ctdb_public_ip_list *tmp_ip;
1608 numnodes = talloc_array_length(ipflags);
1609 retries = 0;
1611 try_again:
1612 maxnum=0;
1613 minnum=0;
1615 /* for each ip address, loop over all nodes that can serve
1616 this ip and make sure that the difference between the node
1617 serving the most and the node serving the least ip's are
1618 not greater than 1.
1620 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1621 if (tmp_ip->pnn == -1) {
1622 continue;
1625 /* Get the highest and lowest number of ips's served by any
1626 valid node which can serve this ip.
1628 maxnode = -1;
1629 minnode = -1;
1630 for (i=0; i<numnodes; i++) {
1631 /* only check nodes that can actually serve this ip */
1632 if (!can_node_takeover_ip(ctdb, i, ipflags[i], tmp_ip)) {
1633 /* no it couldnt so skip to the next node */
1634 continue;
1637 num = node_ip_coverage(ctdb, i, all_ips);
1638 if (maxnode == -1) {
1639 maxnode = i;
1640 maxnum = num;
1641 } else {
1642 if (num > maxnum) {
1643 maxnode = i;
1644 maxnum = num;
1647 if (minnode == -1) {
1648 minnode = i;
1649 minnum = num;
1650 } else {
1651 if (num < minnum) {
1652 minnode = i;
1653 minnum = num;
1657 if (maxnode == -1) {
1658 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
1659 ctdb_addr_to_str(&tmp_ip->addr)));
1661 continue;
1664 /* if the spread between the smallest and largest coverage by
1665 a node is >=2 we steal one of the ips from the node with
1666 most coverage to even things out a bit.
1667 try to do this a limited number of times since we dont
1668 want to spend too much time balancing the ip coverage.
1670 if ( (maxnum > minnum+1)
1671 && (retries < (num_ips + 5)) ){
1672 struct ctdb_public_ip_list *tmp;
1674 /* Reassign one of maxnode's VNNs */
1675 for (tmp=all_ips;tmp;tmp=tmp->next) {
1676 if (tmp->pnn == maxnode) {
1677 (void)find_takeover_node(ctdb, ipflags, tmp, all_ips);
1678 retries++;
1679 goto try_again;;
1686 static void lcp2_init(struct ctdb_context *tmp_ctx,
1687 struct ctdb_ipflags *ipflags,
1688 struct ctdb_public_ip_list *all_ips,
1689 uint32_t *force_rebalance_nodes,
1690 uint32_t **lcp2_imbalances,
1691 bool **rebalance_candidates)
1693 int i, numnodes;
1694 struct ctdb_public_ip_list *tmp_ip;
1696 numnodes = talloc_array_length(ipflags);
1698 *rebalance_candidates = talloc_array(tmp_ctx, bool, numnodes);
1699 CTDB_NO_MEMORY_FATAL(tmp_ctx, *rebalance_candidates);
1700 *lcp2_imbalances = talloc_array(tmp_ctx, uint32_t, numnodes);
1701 CTDB_NO_MEMORY_FATAL(tmp_ctx, *lcp2_imbalances);
1703 for (i=0; i<numnodes; i++) {
1704 (*lcp2_imbalances)[i] = lcp2_imbalance(all_ips, i);
1705 /* First step: assume all nodes are candidates */
1706 (*rebalance_candidates)[i] = true;
1709 /* 2nd step: if a node has IPs assigned then it must have been
1710 * healthy before, so we remove it from consideration. This
1711 * is overkill but is all we have because we don't maintain
1712 * state between takeover runs. An alternative would be to
1713 * keep state and invalidate it every time the recovery master
1714 * changes.
1716 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1717 if (tmp_ip->pnn != -1) {
1718 (*rebalance_candidates)[tmp_ip->pnn] = false;
1722 /* 3rd step: if a node is forced to re-balance then
1723 we allow failback onto the node */
1724 if (force_rebalance_nodes == NULL) {
1725 return;
1727 for (i = 0; i < talloc_array_length(force_rebalance_nodes); i++) {
1728 uint32_t pnn = force_rebalance_nodes[i];
1729 if (pnn >= numnodes) {
1730 DEBUG(DEBUG_ERR,
1731 (__location__ "unknown node %u\n", pnn));
1732 continue;
1735 DEBUG(DEBUG_NOTICE,
1736 ("Forcing rebalancing of IPs to node %u\n", pnn));
1737 (*rebalance_candidates)[pnn] = true;
1741 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1742 * the IP/node combination that will cost the least.
1744 static void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1745 struct ctdb_ipflags *ipflags,
1746 struct ctdb_public_ip_list *all_ips,
1747 uint32_t *lcp2_imbalances)
1749 struct ctdb_public_ip_list *tmp_ip;
1750 int dstnode, numnodes;
1752 int minnode;
1753 uint32_t mindsum, dstdsum, dstimbl, minimbl;
1754 struct ctdb_public_ip_list *minip;
1756 bool should_loop = true;
1757 bool have_unassigned = true;
1759 numnodes = talloc_array_length(ipflags);
1761 while (have_unassigned && should_loop) {
1762 should_loop = false;
1764 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1765 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1767 minnode = -1;
1768 mindsum = 0;
1769 minip = NULL;
1771 /* loop over each unassigned ip. */
1772 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1773 if (tmp_ip->pnn != -1) {
1774 continue;
1777 for (dstnode=0; dstnode<numnodes; dstnode++) {
1778 /* only check nodes that can actually takeover this ip */
1779 if (!can_node_takeover_ip(ctdb, dstnode,
1780 ipflags[dstnode],
1781 tmp_ip)) {
1782 /* no it couldnt so skip to the next node */
1783 continue;
1786 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1787 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1788 DEBUG(DEBUG_DEBUG,(" %s -> %d [+%d]\n",
1789 ctdb_addr_to_str(&(tmp_ip->addr)),
1790 dstnode,
1791 dstimbl - lcp2_imbalances[dstnode]));
1794 if ((minnode == -1) || (dstdsum < mindsum)) {
1795 minnode = dstnode;
1796 minimbl = dstimbl;
1797 mindsum = dstdsum;
1798 minip = tmp_ip;
1799 should_loop = true;
1804 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1806 /* If we found one then assign it to the given node. */
1807 if (minnode != -1) {
1808 minip->pnn = minnode;
1809 lcp2_imbalances[minnode] = minimbl;
1810 DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
1811 ctdb_addr_to_str(&(minip->addr)),
1812 minnode,
1813 mindsum));
1816 /* There might be a better way but at least this is clear. */
1817 have_unassigned = false;
1818 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1819 if (tmp_ip->pnn == -1) {
1820 have_unassigned = true;
1825 /* We know if we have an unassigned addresses so we might as
1826 * well optimise.
1828 if (have_unassigned) {
1829 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1830 if (tmp_ip->pnn == -1) {
1831 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1832 ctdb_addr_to_str(&tmp_ip->addr)));
1838 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1839 * to move IPs from, determines the best IP/destination node
1840 * combination to move from the source node.
1842 static bool lcp2_failback_candidate(struct ctdb_context *ctdb,
1843 struct ctdb_ipflags *ipflags,
1844 struct ctdb_public_ip_list *all_ips,
1845 int srcnode,
1846 uint32_t *lcp2_imbalances,
1847 bool *rebalance_candidates)
1849 int dstnode, mindstnode, numnodes;
1850 uint32_t srcimbl, srcdsum, dstimbl, dstdsum;
1851 uint32_t minsrcimbl, mindstimbl;
1852 struct ctdb_public_ip_list *minip;
1853 struct ctdb_public_ip_list *tmp_ip;
1855 /* Find an IP and destination node that best reduces imbalance. */
1856 srcimbl = 0;
1857 minip = NULL;
1858 minsrcimbl = 0;
1859 mindstnode = -1;
1860 mindstimbl = 0;
1862 numnodes = talloc_array_length(ipflags);
1864 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1865 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES FROM %d [%d]\n",
1866 srcnode, lcp2_imbalances[srcnode]));
1868 for (tmp_ip=all_ips; tmp_ip; tmp_ip=tmp_ip->next) {
1869 /* Only consider addresses on srcnode. */
1870 if (tmp_ip->pnn != srcnode) {
1871 continue;
1874 /* What is this IP address costing the source node? */
1875 srcdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, srcnode);
1876 srcimbl = lcp2_imbalances[srcnode] - srcdsum;
1878 /* Consider this IP address would cost each potential
1879 * destination node. Destination nodes are limited to
1880 * those that are newly healthy, since we don't want
1881 * to do gratuitous failover of IPs just to make minor
1882 * balance improvements.
1884 for (dstnode=0; dstnode<numnodes; dstnode++) {
1885 if (!rebalance_candidates[dstnode]) {
1886 continue;
1889 /* only check nodes that can actually takeover this ip */
1890 if (!can_node_takeover_ip(ctdb, dstnode,
1891 ipflags[dstnode], tmp_ip)) {
1892 /* no it couldnt so skip to the next node */
1893 continue;
1896 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1897 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1898 DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
1899 srcnode, -srcdsum,
1900 ctdb_addr_to_str(&(tmp_ip->addr)),
1901 dstnode, dstdsum));
1903 if ((dstimbl < lcp2_imbalances[srcnode]) &&
1904 (dstdsum < srcdsum) && \
1905 ((mindstnode == -1) || \
1906 ((srcimbl + dstimbl) < (minsrcimbl + mindstimbl)))) {
1908 minip = tmp_ip;
1909 minsrcimbl = srcimbl;
1910 mindstnode = dstnode;
1911 mindstimbl = dstimbl;
1915 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1917 if (mindstnode != -1) {
1918 /* We found a move that makes things better... */
1919 DEBUG(DEBUG_INFO,("%d [%d] -> %s -> %d [+%d]\n",
1920 srcnode, minsrcimbl - lcp2_imbalances[srcnode],
1921 ctdb_addr_to_str(&(minip->addr)),
1922 mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));
1925 lcp2_imbalances[srcnode] = minsrcimbl;
1926 lcp2_imbalances[mindstnode] = mindstimbl;
1927 minip->pnn = mindstnode;
1929 return true;
1932 return false;
1936 struct lcp2_imbalance_pnn {
1937 uint32_t imbalance;
1938 int pnn;
1941 static int lcp2_cmp_imbalance_pnn(const void * a, const void * b)
1943 const struct lcp2_imbalance_pnn * lipa = (const struct lcp2_imbalance_pnn *) a;
1944 const struct lcp2_imbalance_pnn * lipb = (const struct lcp2_imbalance_pnn *) b;
1946 if (lipa->imbalance > lipb->imbalance) {
1947 return -1;
1948 } else if (lipa->imbalance == lipb->imbalance) {
1949 return 0;
1950 } else {
1951 return 1;
1955 /* LCP2 algorithm for rebalancing the cluster. This finds the source
1956 * node with the highest LCP2 imbalance, and then determines the best
1957 * IP/destination node combination to move from the source node.
1959 static void lcp2_failback(struct ctdb_context *ctdb,
1960 struct ctdb_ipflags *ipflags,
1961 struct ctdb_public_ip_list *all_ips,
1962 uint32_t *lcp2_imbalances,
1963 bool *rebalance_candidates)
1965 int i, numnodes;
1966 struct lcp2_imbalance_pnn * lips;
1967 bool again;
1969 numnodes = talloc_array_length(ipflags);
1971 try_again:
1972 /* Put the imbalances and nodes into an array, sort them and
1973 * iterate through candidates. Usually the 1st one will be
1974 * used, so this doesn't cost much...
1976 DEBUG(DEBUG_DEBUG,("+++++++++++++++++++++++++++++++++++++++++\n"));
1977 DEBUG(DEBUG_DEBUG,("Selecting most imbalanced node from:\n"));
1978 lips = talloc_array(ctdb, struct lcp2_imbalance_pnn, numnodes);
1979 for (i=0; i<numnodes; i++) {
1980 lips[i].imbalance = lcp2_imbalances[i];
1981 lips[i].pnn = i;
1982 DEBUG(DEBUG_DEBUG,(" %d [%d]\n", i, lcp2_imbalances[i]));
1984 qsort(lips, numnodes, sizeof(struct lcp2_imbalance_pnn),
1985 lcp2_cmp_imbalance_pnn);
1987 again = false;
1988 for (i=0; i<numnodes; i++) {
1989 /* This means that all nodes had 0 or 1 addresses, so
1990 * can't be imbalanced.
1992 if (lips[i].imbalance == 0) {
1993 break;
1996 if (lcp2_failback_candidate(ctdb,
1997 ipflags,
1998 all_ips,
1999 lips[i].pnn,
2000 lcp2_imbalances,
2001 rebalance_candidates)) {
2002 again = true;
2003 break;
2007 talloc_free(lips);
2008 if (again) {
2009 goto try_again;
2013 static void unassign_unsuitable_ips(struct ctdb_context *ctdb,
2014 struct ctdb_ipflags *ipflags,
2015 struct ctdb_public_ip_list *all_ips)
2017 struct ctdb_public_ip_list *tmp_ip;
2019 /* verify that the assigned nodes can serve that public ip
2020 and set it to -1 if not
2022 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2023 if (tmp_ip->pnn == -1) {
2024 continue;
2026 if (!can_node_host_ip(ctdb, tmp_ip->pnn,
2027 ipflags[tmp_ip->pnn], tmp_ip) != 0) {
2028 /* this node can not serve this ip. */
2029 DEBUG(DEBUG_DEBUG,("Unassign IP: %s from %d\n",
2030 ctdb_addr_to_str(&(tmp_ip->addr)),
2031 tmp_ip->pnn));
2032 tmp_ip->pnn = -1;
2037 static void ip_alloc_deterministic_ips(struct ctdb_context *ctdb,
2038 struct ctdb_ipflags *ipflags,
2039 struct ctdb_public_ip_list *all_ips)
2041 struct ctdb_public_ip_list *tmp_ip;
2042 int i, numnodes;
2044 numnodes = talloc_array_length(ipflags);
2046 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2047 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2048 * always be allocated the same way for a specific set of
2049 * available/unavailable nodes.
2052 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
2053 tmp_ip->pnn = i % numnodes;
2056 /* IP failback doesn't make sense with deterministic
2057 * IPs, since the modulo step above implicitly fails
2058 * back IPs to their "home" node.
2060 if (1 == ctdb->tunable.no_ip_failback) {
2061 DEBUG(DEBUG_WARNING, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2064 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2066 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2068 /* No failback here! */
2071 static void ip_alloc_nondeterministic_ips(struct ctdb_context *ctdb,
2072 struct ctdb_ipflags *ipflags,
2073 struct ctdb_public_ip_list *all_ips)
2075 /* This should be pushed down into basic_failback. */
2076 struct ctdb_public_ip_list *tmp_ip;
2077 int num_ips = 0;
2078 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2079 num_ips++;
2082 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2084 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2086 /* If we don't want IPs to fail back then don't rebalance IPs. */
2087 if (1 == ctdb->tunable.no_ip_failback) {
2088 return;
2091 /* Now, try to make sure the ip adresses are evenly distributed
2092 across the nodes.
2094 basic_failback(ctdb, ipflags, all_ips, num_ips);
2097 static void ip_alloc_lcp2(struct ctdb_context *ctdb,
2098 struct ctdb_ipflags *ipflags,
2099 struct ctdb_public_ip_list *all_ips,
2100 uint32_t *force_rebalance_nodes)
2102 uint32_t *lcp2_imbalances;
2103 bool *rebalance_candidates;
2104 int numnodes, num_rebalance_candidates, i;
2106 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2108 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2110 lcp2_init(tmp_ctx, ipflags, all_ips,force_rebalance_nodes,
2111 &lcp2_imbalances, &rebalance_candidates);
2113 lcp2_allocate_unassigned(ctdb, ipflags, all_ips, lcp2_imbalances);
2115 /* If we don't want IPs to fail back then don't rebalance IPs. */
2116 if (1 == ctdb->tunable.no_ip_failback) {
2117 goto finished;
2120 /* It is only worth continuing if we have suitable target
2121 * nodes to transfer IPs to. This check is much cheaper than
2122 * continuing on...
2124 numnodes = talloc_array_length(ipflags);
2125 num_rebalance_candidates = 0;
2126 for (i=0; i<numnodes; i++) {
2127 if (rebalance_candidates[i]) {
2128 num_rebalance_candidates++;
2131 if (num_rebalance_candidates == 0) {
2132 goto finished;
2135 /* Now, try to make sure the ip adresses are evenly distributed
2136 across the nodes.
2138 lcp2_failback(ctdb, ipflags, all_ips,
2139 lcp2_imbalances, rebalance_candidates);
2141 finished:
2142 talloc_free(tmp_ctx);
2145 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
2147 int i;
2149 for (i=0;i<nodemap->num;i++) {
2150 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
2151 /* Found one completely healthy node */
2152 return false;
2156 return true;
2159 /* The calculation part of the IP allocation algorithm. */
2160 static void ctdb_takeover_run_core(struct ctdb_context *ctdb,
2161 struct ctdb_ipflags *ipflags,
2162 struct ctdb_public_ip_list **all_ips_p,
2163 uint32_t *force_rebalance_nodes)
2165 /* since nodes only know about those public addresses that
2166 can be served by that particular node, no single node has
2167 a full list of all public addresses that exist in the cluster.
2168 Walk over all node structures and create a merged list of
2169 all public addresses that exist in the cluster.
2171 keep the tree of ips around as ctdb->ip_tree
2173 *all_ips_p = create_merged_ip_list(ctdb);
2175 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2176 ip_alloc_lcp2(ctdb, ipflags, *all_ips_p, force_rebalance_nodes);
2177 } else if (1 == ctdb->tunable.deterministic_public_ips) {
2178 ip_alloc_deterministic_ips(ctdb, ipflags, *all_ips_p);
2179 } else {
2180 ip_alloc_nondeterministic_ips(ctdb, ipflags, *all_ips_p);
2183 /* at this point ->pnn is the node which will own each IP
2184 or -1 if there is no node that can cover this ip
2187 return;
2190 struct get_tunable_callback_data {
2191 const char *tunable;
2192 uint32_t *out;
2193 bool fatal;
2196 static void get_tunable_callback(struct ctdb_context *ctdb, uint32_t pnn,
2197 int32_t res, TDB_DATA outdata,
2198 void *callback)
2200 struct get_tunable_callback_data *cd =
2201 (struct get_tunable_callback_data *)callback;
2202 int size;
2204 if (res != 0) {
2205 /* Already handled in fail callback */
2206 return;
2209 if (outdata.dsize != sizeof(uint32_t)) {
2210 DEBUG(DEBUG_ERR,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2211 cd->tunable, pnn, (int)sizeof(uint32_t),
2212 (int)outdata.dsize));
2213 cd->fatal = true;
2214 return;
2217 size = talloc_array_length(cd->out);
2218 if (pnn >= size) {
2219 DEBUG(DEBUG_ERR,("Got %s reply from node %d but nodemap only has %d entries\n",
2220 cd->tunable, pnn, size));
2221 return;
2225 cd->out[pnn] = *(uint32_t *)outdata.dptr;
2228 static void get_tunable_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2229 int32_t res, TDB_DATA outdata,
2230 void *callback)
2232 struct get_tunable_callback_data *cd =
2233 (struct get_tunable_callback_data *)callback;
2235 switch (res) {
2236 case -ETIME:
2237 DEBUG(DEBUG_ERR,
2238 ("Timed out getting tunable \"%s\" from node %d\n",
2239 cd->tunable, pnn));
2240 cd->fatal = true;
2241 break;
2242 case -EINVAL:
2243 case -1:
2244 DEBUG(DEBUG_WARNING,
2245 ("Tunable \"%s\" not implemented on node %d\n",
2246 cd->tunable, pnn));
2247 break;
2248 default:
2249 DEBUG(DEBUG_ERR,
2250 ("Unexpected error getting tunable \"%s\" from node %d\n",
2251 cd->tunable, pnn));
2252 cd->fatal = true;
2256 static uint32_t *get_tunable_from_nodes(struct ctdb_context *ctdb,
2257 TALLOC_CTX *tmp_ctx,
2258 struct ctdb_node_map *nodemap,
2259 const char *tunable,
2260 uint32_t default_value)
2262 TDB_DATA data;
2263 struct ctdb_control_get_tunable *t;
2264 uint32_t *nodes;
2265 uint32_t *tvals;
2266 struct get_tunable_callback_data callback_data;
2267 int i;
2269 tvals = talloc_array(tmp_ctx, uint32_t, nodemap->num);
2270 CTDB_NO_MEMORY_NULL(ctdb, tvals);
2271 for (i=0; i<nodemap->num; i++) {
2272 tvals[i] = default_value;
2275 callback_data.out = tvals;
2276 callback_data.tunable = tunable;
2277 callback_data.fatal = false;
2279 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen(tunable) + 1;
2280 data.dptr = talloc_size(tmp_ctx, data.dsize);
2281 t = (struct ctdb_control_get_tunable *)data.dptr;
2282 t->length = strlen(tunable)+1;
2283 memcpy(t->name, tunable, t->length);
2284 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2285 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_TUNABLE,
2286 nodes, 0, TAKEOVER_TIMEOUT(),
2287 false, data,
2288 get_tunable_callback,
2289 get_tunable_fail_callback,
2290 &callback_data) != 0) {
2291 if (callback_data.fatal) {
2292 talloc_free(tvals);
2293 tvals = NULL;
2296 talloc_free(nodes);
2297 talloc_free(data.dptr);
2299 return tvals;
2302 struct get_runstate_callback_data {
2303 enum ctdb_runstate *out;
2304 bool fatal;
2307 static void get_runstate_callback(struct ctdb_context *ctdb, uint32_t pnn,
2308 int32_t res, TDB_DATA outdata,
2309 void *callback_data)
2311 struct get_runstate_callback_data *cd =
2312 (struct get_runstate_callback_data *)callback_data;
2313 int size;
2315 if (res != 0) {
2316 /* Already handled in fail callback */
2317 return;
2320 if (outdata.dsize != sizeof(uint32_t)) {
2321 DEBUG(DEBUG_ERR,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2322 pnn, (int)sizeof(uint32_t),
2323 (int)outdata.dsize));
2324 cd->fatal = true;
2325 return;
2328 size = talloc_array_length(cd->out);
2329 if (pnn >= size) {
2330 DEBUG(DEBUG_ERR,("Got reply from node %d but nodemap only has %d entries\n",
2331 pnn, size));
2332 return;
2335 cd->out[pnn] = (enum ctdb_runstate)*(uint32_t *)outdata.dptr;
2338 static void get_runstate_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2339 int32_t res, TDB_DATA outdata,
2340 void *callback)
2342 struct get_runstate_callback_data *cd =
2343 (struct get_runstate_callback_data *)callback;
2345 switch (res) {
2346 case -ETIME:
2347 DEBUG(DEBUG_ERR,
2348 ("Timed out getting runstate from node %d\n", pnn));
2349 cd->fatal = true;
2350 break;
2351 default:
2352 DEBUG(DEBUG_WARNING,
2353 ("Error getting runstate from node %d - assuming runstates not supported\n",
2354 pnn));
2358 static enum ctdb_runstate * get_runstate_from_nodes(struct ctdb_context *ctdb,
2359 TALLOC_CTX *tmp_ctx,
2360 struct ctdb_node_map *nodemap,
2361 enum ctdb_runstate default_value)
2363 uint32_t *nodes;
2364 enum ctdb_runstate *rs;
2365 struct get_runstate_callback_data callback_data;
2366 int i;
2368 rs = talloc_array(tmp_ctx, enum ctdb_runstate, nodemap->num);
2369 CTDB_NO_MEMORY_NULL(ctdb, rs);
2370 for (i=0; i<nodemap->num; i++) {
2371 rs[i] = default_value;
2374 callback_data.out = rs;
2375 callback_data.fatal = false;
2377 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2378 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_RUNSTATE,
2379 nodes, 0, TAKEOVER_TIMEOUT(),
2380 true, tdb_null,
2381 get_runstate_callback,
2382 get_runstate_fail_callback,
2383 &callback_data) != 0) {
2384 if (callback_data.fatal) {
2385 free(rs);
2386 rs = NULL;
2389 talloc_free(nodes);
2391 return rs;
2394 /* Set internal flags for IP allocation:
2395 * Clear ip flags
2396 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2397 * Set NOIPHOST ip flag for each INACTIVE node
2398 * if all nodes are disabled:
2399 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2400 * else
2401 * Set NOIPHOST ip flags for disabled nodes
2403 static struct ctdb_ipflags *
2404 set_ipflags_internal(struct ctdb_context *ctdb,
2405 TALLOC_CTX *tmp_ctx,
2406 struct ctdb_node_map *nodemap,
2407 uint32_t *tval_noiptakeover,
2408 uint32_t *tval_noiphostonalldisabled,
2409 enum ctdb_runstate *runstate)
2411 int i;
2412 struct ctdb_ipflags *ipflags;
2414 /* Clear IP flags - implicit due to talloc_zero */
2415 ipflags = talloc_zero_array(tmp_ctx, struct ctdb_ipflags, nodemap->num);
2416 CTDB_NO_MEMORY_NULL(ctdb, ipflags);
2418 for (i=0;i<nodemap->num;i++) {
2419 /* Can not take IPs on node with NoIPTakeover set */
2420 if (tval_noiptakeover[i] != 0) {
2421 ipflags[i].noiptakeover = true;
2424 /* Can not host IPs on node not in RUNNING state */
2425 if (runstate[i] != CTDB_RUNSTATE_RUNNING) {
2426 ipflags[i].noiphost = true;
2427 continue;
2429 /* Can not host IPs on INACTIVE node */
2430 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2431 ipflags[i].noiphost = true;
2433 /* Remember the runstate */
2434 ipflags[i].runstate = runstate[i];
2437 if (all_nodes_are_disabled(nodemap)) {
2438 /* If all nodes are disabled, can not host IPs on node
2439 * with NoIPHostOnAllDisabled set
2441 for (i=0;i<nodemap->num;i++) {
2442 if (tval_noiphostonalldisabled[i] != 0) {
2443 ipflags[i].noiphost = true;
2446 } else {
2447 /* If some nodes are not disabled, then can not host
2448 * IPs on DISABLED node
2450 for (i=0;i<nodemap->num;i++) {
2451 if (nodemap->nodes[i].flags & NODE_FLAGS_DISABLED) {
2452 ipflags[i].noiphost = true;
2457 return ipflags;
2460 static struct ctdb_ipflags *set_ipflags(struct ctdb_context *ctdb,
2461 TALLOC_CTX *tmp_ctx,
2462 struct ctdb_node_map *nodemap)
2464 uint32_t *tval_noiptakeover;
2465 uint32_t *tval_noiphostonalldisabled;
2466 struct ctdb_ipflags *ipflags;
2467 enum ctdb_runstate *runstate;
2470 tval_noiptakeover = get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2471 "NoIPTakeover", 0);
2472 if (tval_noiptakeover == NULL) {
2473 return NULL;
2476 tval_noiphostonalldisabled =
2477 get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2478 "NoIPHostOnAllDisabled", 0);
2479 if (tval_noiphostonalldisabled == NULL) {
2480 /* Caller frees tmp_ctx */
2481 return NULL;
2484 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2485 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2486 * reasonable behaviour on a mixed cluster during upgrade.
2488 runstate = get_runstate_from_nodes(ctdb, tmp_ctx, nodemap,
2489 CTDB_RUNSTATE_RUNNING);
2490 if (runstate == NULL) {
2491 /* Caller frees tmp_ctx */
2492 return NULL;
2495 ipflags = set_ipflags_internal(ctdb, tmp_ctx, nodemap,
2496 tval_noiptakeover,
2497 tval_noiphostonalldisabled,
2498 runstate);
2500 talloc_free(tval_noiptakeover);
2501 talloc_free(tval_noiphostonalldisabled);
2502 talloc_free(runstate);
2504 return ipflags;
2507 struct iprealloc_callback_data {
2508 bool *retry_nodes;
2509 int retry_count;
2510 client_async_callback fail_callback;
2511 void *fail_callback_data;
2512 struct ctdb_node_map *nodemap;
2515 static void iprealloc_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2516 int32_t res, TDB_DATA outdata,
2517 void *callback)
2519 int numnodes;
2520 struct iprealloc_callback_data *cd =
2521 (struct iprealloc_callback_data *)callback;
2523 numnodes = talloc_array_length(cd->retry_nodes);
2524 if (pnn > numnodes) {
2525 DEBUG(DEBUG_ERR,
2526 ("ipreallocated failure from node %d, "
2527 "but only %d nodes in nodemap\n",
2528 pnn, numnodes));
2529 return;
2532 /* Can't run the "ipreallocated" event on a INACTIVE node */
2533 if (cd->nodemap->nodes[pnn].flags & NODE_FLAGS_INACTIVE) {
2534 DEBUG(DEBUG_WARNING,
2535 ("ipreallocated failed on inactive node %d, ignoring\n",
2536 pnn));
2537 return;
2540 switch (res) {
2541 case -ETIME:
2542 /* If the control timed out then that's a real error,
2543 * so call the real fail callback
2545 if (cd->fail_callback) {
2546 cd->fail_callback(ctdb, pnn, res, outdata,
2547 cd->fail_callback_data);
2548 } else {
2549 DEBUG(DEBUG_WARNING,
2550 ("iprealloc timed out but no callback registered\n"));
2552 break;
2553 default:
2554 /* If not a timeout then either the ipreallocated
2555 * eventscript (or some setup) failed. This might
2556 * have failed because the IPREALLOCATED control isn't
2557 * implemented - right now there is no way of knowing
2558 * because the error codes are all folded down to -1.
2559 * Consider retrying using EVENTSCRIPT control...
2561 DEBUG(DEBUG_WARNING,
2562 ("ipreallocated failure from node %d, flagging retry\n",
2563 pnn));
2564 cd->retry_nodes[pnn] = true;
2565 cd->retry_count++;
2569 struct takeover_callback_data {
2570 bool *node_failed;
2571 client_async_callback fail_callback;
2572 void *fail_callback_data;
2573 struct ctdb_node_map *nodemap;
2576 static void takeover_run_fail_callback(struct ctdb_context *ctdb,
2577 uint32_t node_pnn, int32_t res,
2578 TDB_DATA outdata, void *callback_data)
2580 struct takeover_callback_data *cd =
2581 talloc_get_type_abort(callback_data,
2582 struct takeover_callback_data);
2583 int i;
2585 for (i = 0; i < cd->nodemap->num; i++) {
2586 if (node_pnn == cd->nodemap->nodes[i].pnn) {
2587 break;
2591 if (i == cd->nodemap->num) {
2592 DEBUG(DEBUG_ERR, (__location__ " invalid PNN %u\n", node_pnn));
2593 return;
2596 if (!cd->node_failed[i]) {
2597 cd->node_failed[i] = true;
2598 cd->fail_callback(ctdb, node_pnn, res, outdata,
2599 cd->fail_callback_data);
2604 make any IP alias changes for public addresses that are necessary
2606 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
2607 uint32_t *force_rebalance_nodes,
2608 client_async_callback fail_callback, void *callback_data)
2610 int i, j, ret;
2611 struct ctdb_public_ip ip;
2612 struct ctdb_public_ipv4 ipv4;
2613 uint32_t *nodes;
2614 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2615 TDB_DATA data;
2616 struct timeval timeout;
2617 struct client_async_data *async_data;
2618 struct ctdb_client_control_state *state;
2619 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2620 struct ctdb_ipflags *ipflags;
2621 struct takeover_callback_data *takeover_data;
2622 struct iprealloc_callback_data iprealloc_data;
2623 bool *retry_data;
2624 bool can_host_ips;
2627 * ip failover is completely disabled, just send out the
2628 * ipreallocated event.
2630 if (ctdb->tunable.disable_ip_failover != 0) {
2631 goto ipreallocated;
2634 ipflags = set_ipflags(ctdb, tmp_ctx, nodemap);
2635 if (ipflags == NULL) {
2636 DEBUG(DEBUG_ERR,("Failed to set IP flags - aborting takeover run\n"));
2637 talloc_free(tmp_ctx);
2638 return -1;
2641 /* Short-circuit IP allocation if no nodes are in the RUNNING
2642 * runstate yet, since no nodes will be able to host IPs */
2643 can_host_ips = false;
2644 for (i=0; i<nodemap->num; i++) {
2645 if (ipflags[i].runstate == CTDB_RUNSTATE_RUNNING) {
2646 can_host_ips = true;
2649 if (!can_host_ips) {
2650 DEBUG(DEBUG_WARNING,("No nodes available to host public IPs yet\n"));
2651 return 0;
2654 /* Do the IP reassignment calculations */
2655 ctdb_takeover_run_core(ctdb, ipflags, &all_ips, force_rebalance_nodes);
2657 /* Now tell all nodes to release any public IPs should not
2658 * host. This will be a NOOP on nodes that don't currently
2659 * hold the given IP.
2661 takeover_data = talloc_zero(tmp_ctx, struct takeover_callback_data);
2662 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data);
2664 takeover_data->node_failed = talloc_zero_array(tmp_ctx,
2665 bool, nodemap->num);
2666 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data->node_failed);
2667 takeover_data->fail_callback = fail_callback;
2668 takeover_data->fail_callback_data = callback_data;
2669 takeover_data->nodemap = nodemap;
2671 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2672 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2674 async_data->fail_callback = takeover_run_fail_callback;
2675 async_data->callback_data = takeover_data;
2677 ZERO_STRUCT(ip); /* Avoid valgrind warnings for union */
2679 /* Send a RELEASE_IP to all nodes that should not be hosting
2680 * each IP. For each IP, all but one of these will be
2681 * redundant. However, the redundant ones are used to tell
2682 * nodes which node should be hosting the IP so that commands
2683 * like "ctdb ip" can display a particular nodes idea of who
2684 * is hosting what. */
2685 for (i=0;i<nodemap->num;i++) {
2686 /* don't talk to unconnected nodes, but do talk to banned nodes */
2687 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2688 continue;
2691 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2692 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
2693 /* This node should be serving this
2694 vnn so dont tell it to release the ip
2696 continue;
2698 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2699 ipv4.pnn = tmp_ip->pnn;
2700 ipv4.sin = tmp_ip->addr.ip;
2702 timeout = TAKEOVER_TIMEOUT();
2703 data.dsize = sizeof(ipv4);
2704 data.dptr = (uint8_t *)&ipv4;
2705 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2706 0, CTDB_CONTROL_RELEASE_IPv4, 0,
2707 data, async_data,
2708 &timeout, NULL);
2709 } else {
2710 ip.pnn = tmp_ip->pnn;
2711 ip.addr = tmp_ip->addr;
2713 timeout = TAKEOVER_TIMEOUT();
2714 data.dsize = sizeof(ip);
2715 data.dptr = (uint8_t *)&ip;
2716 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2717 0, CTDB_CONTROL_RELEASE_IP, 0,
2718 data, async_data,
2719 &timeout, NULL);
2722 if (state == NULL) {
2723 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
2724 talloc_free(tmp_ctx);
2725 return -1;
2728 ctdb_client_async_add(async_data, state);
2731 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2732 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2733 talloc_free(tmp_ctx);
2734 return -1;
2736 talloc_free(async_data);
2739 /* For each IP, send a TAKOVER_IP to the node that should be
2740 * hosting it. Many of these will often be redundant (since
2741 * the allocation won't have changed) but they can be useful
2742 * to recover from inconsistencies. */
2743 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2744 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2746 async_data->fail_callback = fail_callback;
2747 async_data->callback_data = callback_data;
2749 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2750 if (tmp_ip->pnn == -1) {
2751 /* this IP won't be taken over */
2752 continue;
2755 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2756 ipv4.pnn = tmp_ip->pnn;
2757 ipv4.sin = tmp_ip->addr.ip;
2759 timeout = TAKEOVER_TIMEOUT();
2760 data.dsize = sizeof(ipv4);
2761 data.dptr = (uint8_t *)&ipv4;
2762 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2763 0, CTDB_CONTROL_TAKEOVER_IPv4, 0,
2764 data, async_data,
2765 &timeout, NULL);
2766 } else {
2767 ip.pnn = tmp_ip->pnn;
2768 ip.addr = tmp_ip->addr;
2770 timeout = TAKEOVER_TIMEOUT();
2771 data.dsize = sizeof(ip);
2772 data.dptr = (uint8_t *)&ip;
2773 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2774 0, CTDB_CONTROL_TAKEOVER_IP, 0,
2775 data, async_data,
2776 &timeout, NULL);
2778 if (state == NULL) {
2779 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
2780 talloc_free(tmp_ctx);
2781 return -1;
2784 ctdb_client_async_add(async_data, state);
2786 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2787 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2788 talloc_free(tmp_ctx);
2789 return -1;
2792 ipreallocated:
2794 * Tell all nodes to run eventscripts to process the
2795 * "ipreallocated" event. This can do a lot of things,
2796 * including restarting services to reconfigure them if public
2797 * IPs have moved. Once upon a time this event only used to
2798 * update natgw.
2800 retry_data = talloc_zero_array(tmp_ctx, bool, nodemap->num);
2801 CTDB_NO_MEMORY_FATAL(ctdb, retry_data);
2802 iprealloc_data.retry_nodes = retry_data;
2803 iprealloc_data.retry_count = 0;
2804 iprealloc_data.fail_callback = fail_callback;
2805 iprealloc_data.fail_callback_data = callback_data;
2806 iprealloc_data.nodemap = nodemap;
2808 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2809 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_IPREALLOCATED,
2810 nodes, 0, TAKEOVER_TIMEOUT(),
2811 false, tdb_null,
2812 NULL, iprealloc_fail_callback,
2813 &iprealloc_data);
2814 if (ret != 0) {
2815 /* If the control failed then we should retry to any
2816 * nodes flagged by iprealloc_fail_callback using the
2817 * EVENTSCRIPT control. This is a best-effort at
2818 * backward compatiblity when running a mixed cluster
2819 * where some nodes have not yet been upgraded to
2820 * support the IPREALLOCATED control.
2822 DEBUG(DEBUG_WARNING,
2823 ("Retry ipreallocated to some nodes using eventscript control\n"));
2825 nodes = talloc_array(tmp_ctx, uint32_t,
2826 iprealloc_data.retry_count);
2827 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2829 j = 0;
2830 for (i=0; i<nodemap->num; i++) {
2831 if (iprealloc_data.retry_nodes[i]) {
2832 nodes[j] = i;
2833 j++;
2837 data.dptr = discard_const("ipreallocated");
2838 data.dsize = strlen((char *)data.dptr) + 1;
2839 ret = ctdb_client_async_control(ctdb,
2840 CTDB_CONTROL_RUN_EVENTSCRIPTS,
2841 nodes, 0, TAKEOVER_TIMEOUT(),
2842 false, data,
2843 NULL, fail_callback,
2844 callback_data);
2845 if (ret != 0) {
2846 DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
2850 talloc_free(tmp_ctx);
2851 return ret;
2856 destroy a ctdb_client_ip structure
2858 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
2860 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
2861 ctdb_addr_to_str(&ip->addr),
2862 ntohs(ip->addr.ip.sin_port),
2863 ip->client_id));
2865 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
2866 return 0;
2870 called by a client to inform us of a TCP connection that it is managing
2871 that should tickled with an ACK when IP takeover is done
2872 we handle both the old ipv4 style of packets as well as the new ipv4/6
2873 pdus.
2875 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
2876 TDB_DATA indata)
2878 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
2879 struct ctdb_control_tcp *old_addr = NULL;
2880 struct ctdb_control_tcp_addr new_addr;
2881 struct ctdb_control_tcp_addr *tcp_sock = NULL;
2882 struct ctdb_tcp_list *tcp;
2883 struct ctdb_tcp_connection t;
2884 int ret;
2885 TDB_DATA data;
2886 struct ctdb_client_ip *ip;
2887 struct ctdb_vnn *vnn;
2888 ctdb_sock_addr addr;
2890 /* If we don't have public IPs, tickles are useless */
2891 if (ctdb->vnn == NULL) {
2892 return 0;
2895 switch (indata.dsize) {
2896 case sizeof(struct ctdb_control_tcp):
2897 old_addr = (struct ctdb_control_tcp *)indata.dptr;
2898 ZERO_STRUCT(new_addr);
2899 tcp_sock = &new_addr;
2900 tcp_sock->src.ip = old_addr->src;
2901 tcp_sock->dest.ip = old_addr->dest;
2902 break;
2903 case sizeof(struct ctdb_control_tcp_addr):
2904 tcp_sock = (struct ctdb_control_tcp_addr *)indata.dptr;
2905 break;
2906 default:
2907 DEBUG(DEBUG_ERR,(__location__ " Invalid data structure passed "
2908 "to ctdb_control_tcp_client. size was %d but "
2909 "only allowed sizes are %lu and %lu\n",
2910 (int)indata.dsize,
2911 (long unsigned)sizeof(struct ctdb_control_tcp),
2912 (long unsigned)sizeof(struct ctdb_control_tcp_addr)));
2913 return -1;
2916 addr = tcp_sock->src;
2917 ctdb_canonicalize_ip(&addr, &tcp_sock->src);
2918 addr = tcp_sock->dest;
2919 ctdb_canonicalize_ip(&addr, &tcp_sock->dest);
2921 ZERO_STRUCT(addr);
2922 memcpy(&addr, &tcp_sock->dest, sizeof(addr));
2923 vnn = find_public_ip_vnn(ctdb, &addr);
2924 if (vnn == NULL) {
2925 switch (addr.sa.sa_family) {
2926 case AF_INET:
2927 if (ntohl(addr.ip.sin_addr.s_addr) != INADDR_LOOPBACK) {
2928 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public address.\n",
2929 ctdb_addr_to_str(&addr)));
2931 break;
2932 case AF_INET6:
2933 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public ipv6 address.\n",
2934 ctdb_addr_to_str(&addr)));
2935 break;
2936 default:
2937 DEBUG(DEBUG_ERR,(__location__ " Unknown family type %d\n", addr.sa.sa_family));
2940 return 0;
2943 if (vnn->pnn != ctdb->pnn) {
2944 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2945 ctdb_addr_to_str(&addr),
2946 client_id, client->pid));
2947 /* failing this call will tell smbd to die */
2948 return -1;
2951 ip = talloc(client, struct ctdb_client_ip);
2952 CTDB_NO_MEMORY(ctdb, ip);
2954 ip->ctdb = ctdb;
2955 ip->addr = addr;
2956 ip->client_id = client_id;
2957 talloc_set_destructor(ip, ctdb_client_ip_destructor);
2958 DLIST_ADD(ctdb->client_ip_list, ip);
2960 tcp = talloc(client, struct ctdb_tcp_list);
2961 CTDB_NO_MEMORY(ctdb, tcp);
2963 tcp->connection.src_addr = tcp_sock->src;
2964 tcp->connection.dst_addr = tcp_sock->dest;
2966 DLIST_ADD(client->tcp_list, tcp);
2968 t.src_addr = tcp_sock->src;
2969 t.dst_addr = tcp_sock->dest;
2971 data.dptr = (uint8_t *)&t;
2972 data.dsize = sizeof(t);
2974 switch (addr.sa.sa_family) {
2975 case AF_INET:
2976 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2977 (unsigned)ntohs(tcp_sock->dest.ip.sin_port),
2978 ctdb_addr_to_str(&tcp_sock->src),
2979 (unsigned)ntohs(tcp_sock->src.ip.sin_port), client_id, client->pid));
2980 break;
2981 case AF_INET6:
2982 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2983 (unsigned)ntohs(tcp_sock->dest.ip6.sin6_port),
2984 ctdb_addr_to_str(&tcp_sock->src),
2985 (unsigned)ntohs(tcp_sock->src.ip6.sin6_port), client_id, client->pid));
2986 break;
2987 default:
2988 DEBUG(DEBUG_ERR,(__location__ " Unknown family %d\n", addr.sa.sa_family));
2992 /* tell all nodes about this tcp connection */
2993 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
2994 CTDB_CONTROL_TCP_ADD,
2995 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
2996 if (ret != 0) {
2997 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
2998 return -1;
3001 return 0;
3005 find a tcp address on a list
3007 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
3008 struct ctdb_tcp_connection *tcp)
3010 int i;
3012 if (array == NULL) {
3013 return NULL;
3016 for (i=0;i<array->num;i++) {
3017 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
3018 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
3019 return &array->connections[i];
3022 return NULL;
3028 called by a daemon to inform us of a TCP connection that one of its
3029 clients managing that should tickled with an ACK when IP takeover is
3030 done
3032 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
3034 struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
3035 struct ctdb_tcp_array *tcparray;
3036 struct ctdb_tcp_connection tcp;
3037 struct ctdb_vnn *vnn;
3039 /* If we don't have public IPs, tickles are useless */
3040 if (ctdb->vnn == NULL) {
3041 return 0;
3044 vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
3045 if (vnn == NULL) {
3046 DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
3047 ctdb_addr_to_str(&p->dst_addr)));
3049 return -1;
3053 tcparray = vnn->tcp_array;
3055 /* If this is the first tickle */
3056 if (tcparray == NULL) {
3057 tcparray = talloc(vnn, struct ctdb_tcp_array);
3058 CTDB_NO_MEMORY(ctdb, tcparray);
3059 vnn->tcp_array = tcparray;
3061 tcparray->num = 0;
3062 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
3063 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3065 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3066 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3067 tcparray->num++;
3069 if (tcp_update_needed) {
3070 vnn->tcp_update_needed = true;
3072 return 0;
3076 /* Do we already have this tickle ?*/
3077 tcp.src_addr = p->src_addr;
3078 tcp.dst_addr = p->dst_addr;
3079 if (ctdb_tcp_find(tcparray, &tcp) != NULL) {
3080 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
3081 ctdb_addr_to_str(&tcp.dst_addr),
3082 ntohs(tcp.dst_addr.ip.sin_port),
3083 vnn->pnn));
3084 return 0;
3087 /* A new tickle, we must add it to the array */
3088 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
3089 struct ctdb_tcp_connection,
3090 tcparray->num+1);
3091 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3093 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3094 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3095 tcparray->num++;
3097 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
3098 ctdb_addr_to_str(&tcp.dst_addr),
3099 ntohs(tcp.dst_addr.ip.sin_port),
3100 vnn->pnn));
3102 if (tcp_update_needed) {
3103 vnn->tcp_update_needed = true;
3106 return 0;
3111 called by a daemon to inform us of a TCP connection that one of its
3112 clients managing that should tickled with an ACK when IP takeover is
3113 done
3115 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
3117 struct ctdb_tcp_connection *tcpp;
3118 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
3120 if (vnn == NULL) {
3121 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
3122 ctdb_addr_to_str(&conn->dst_addr)));
3123 return;
3126 /* if the array is empty we cant remove it
3127 and we dont need to do anything
3129 if (vnn->tcp_array == NULL) {
3130 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3131 ctdb_addr_to_str(&conn->dst_addr),
3132 ntohs(conn->dst_addr.ip.sin_port)));
3133 return;
3137 /* See if we know this connection
3138 if we dont know this connection then we dont need to do anything
3140 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
3141 if (tcpp == NULL) {
3142 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
3143 ctdb_addr_to_str(&conn->dst_addr),
3144 ntohs(conn->dst_addr.ip.sin_port)));
3145 return;
3149 /* We need to remove this entry from the array.
3150 Instead of allocating a new array and copying data to it
3151 we cheat and just copy the last entry in the existing array
3152 to the entry that is to be removed and just shring the
3153 ->num field
3155 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
3156 vnn->tcp_array->num--;
3158 /* If we deleted the last entry we also need to remove the entire array
3160 if (vnn->tcp_array->num == 0) {
3161 talloc_free(vnn->tcp_array);
3162 vnn->tcp_array = NULL;
3165 vnn->tcp_update_needed = true;
3167 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
3168 ctdb_addr_to_str(&conn->src_addr),
3169 ntohs(conn->src_addr.ip.sin_port)));
3174 called by a daemon to inform us of a TCP connection that one of its
3175 clients used are no longer needed in the tickle database
3177 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
3179 struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
3181 /* If we don't have public IPs, tickles are useless */
3182 if (ctdb->vnn == NULL) {
3183 return 0;
3186 ctdb_remove_tcp_connection(ctdb, conn);
3188 return 0;
3193 Called when another daemon starts - causes all tickles for all
3194 public addresses we are serving to be sent to the new node on the
3195 next check. This actually causes the next scheduled call to
3196 tdb_update_tcp_tickles() to update all nodes. This is simple and
3197 doesn't require careful error handling.
3199 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t pnn)
3201 struct ctdb_vnn *vnn;
3203 DEBUG(DEBUG_INFO, ("Received startup control from node %lu\n",
3204 (unsigned long) pnn));
3206 for (vnn = ctdb->vnn; vnn != NULL; vnn = vnn->next) {
3207 vnn->tcp_update_needed = true;
3210 return 0;
3215 called when a client structure goes away - hook to remove
3216 elements from the tcp_list in all daemons
3218 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
3220 while (client->tcp_list) {
3221 struct ctdb_tcp_list *tcp = client->tcp_list;
3222 DLIST_REMOVE(client->tcp_list, tcp);
3223 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
3229 release all IPs on shutdown
3231 void ctdb_release_all_ips(struct ctdb_context *ctdb)
3233 struct ctdb_vnn *vnn;
3234 int count = 0;
3236 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3237 if (!ctdb_sys_have_ip(&vnn->public_address)) {
3238 ctdb_vnn_unassign_iface(ctdb, vnn);
3239 continue;
3241 if (!vnn->iface) {
3242 continue;
3245 DEBUG(DEBUG_INFO,("Release of IP %s/%u on interface %s node:-1\n",
3246 ctdb_addr_to_str(&vnn->public_address),
3247 vnn->public_netmask_bits,
3248 ctdb_vnn_iface_string(vnn)));
3250 ctdb_event_script_args(ctdb, CTDB_EVENT_RELEASE_IP, "%s %s %u",
3251 ctdb_vnn_iface_string(vnn),
3252 ctdb_addr_to_str(&vnn->public_address),
3253 vnn->public_netmask_bits);
3254 release_kill_clients(ctdb, &vnn->public_address);
3255 ctdb_vnn_unassign_iface(ctdb, vnn);
3256 count++;
3259 DEBUG(DEBUG_NOTICE,(__location__ " Released %d public IPs\n", count));
3264 get list of public IPs
3266 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
3267 struct ctdb_req_control *c, TDB_DATA *outdata)
3269 int i, num, len;
3270 struct ctdb_all_public_ips *ips;
3271 struct ctdb_vnn *vnn;
3272 bool only_available = false;
3274 if (c->flags & CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE) {
3275 only_available = true;
3278 /* count how many public ip structures we have */
3279 num = 0;
3280 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3281 num++;
3284 len = offsetof(struct ctdb_all_public_ips, ips) +
3285 num*sizeof(struct ctdb_public_ip);
3286 ips = talloc_zero_size(outdata, len);
3287 CTDB_NO_MEMORY(ctdb, ips);
3289 i = 0;
3290 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3291 if (only_available && !ctdb_vnn_available(ctdb, vnn)) {
3292 continue;
3294 ips->ips[i].pnn = vnn->pnn;
3295 ips->ips[i].addr = vnn->public_address;
3296 i++;
3298 ips->num = i;
3299 len = offsetof(struct ctdb_all_public_ips, ips) +
3300 i*sizeof(struct ctdb_public_ip);
3302 outdata->dsize = len;
3303 outdata->dptr = (uint8_t *)ips;
3305 return 0;
3310 get list of public IPs, old ipv4 style. only returns ipv4 addresses
3312 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb,
3313 struct ctdb_req_control *c, TDB_DATA *outdata)
3315 int i, num, len;
3316 struct ctdb_all_public_ipsv4 *ips;
3317 struct ctdb_vnn *vnn;
3319 /* count how many public ip structures we have */
3320 num = 0;
3321 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3322 if (vnn->public_address.sa.sa_family != AF_INET) {
3323 continue;
3325 num++;
3328 len = offsetof(struct ctdb_all_public_ipsv4, ips) +
3329 num*sizeof(struct ctdb_public_ipv4);
3330 ips = talloc_zero_size(outdata, len);
3331 CTDB_NO_MEMORY(ctdb, ips);
3333 outdata->dsize = len;
3334 outdata->dptr = (uint8_t *)ips;
3336 ips->num = num;
3337 i = 0;
3338 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3339 if (vnn->public_address.sa.sa_family != AF_INET) {
3340 continue;
3342 ips->ips[i].pnn = vnn->pnn;
3343 ips->ips[i].sin = vnn->public_address.ip;
3344 i++;
3347 return 0;
3350 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
3351 struct ctdb_req_control *c,
3352 TDB_DATA indata,
3353 TDB_DATA *outdata)
3355 int i, num, len;
3356 ctdb_sock_addr *addr;
3357 struct ctdb_control_public_ip_info *info;
3358 struct ctdb_vnn *vnn;
3360 addr = (ctdb_sock_addr *)indata.dptr;
3362 vnn = find_public_ip_vnn(ctdb, addr);
3363 if (vnn == NULL) {
3364 /* if it is not a public ip it could be our 'single ip' */
3365 if (ctdb->single_ip_vnn) {
3366 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
3367 vnn = ctdb->single_ip_vnn;
3371 if (vnn == NULL) {
3372 DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
3373 "'%s'not a public address\n",
3374 ctdb_addr_to_str(addr)));
3375 return -1;
3378 /* count how many public ip structures we have */
3379 num = 0;
3380 for (;vnn->ifaces[num];) {
3381 num++;
3384 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3385 num*sizeof(struct ctdb_control_iface_info);
3386 info = talloc_zero_size(outdata, len);
3387 CTDB_NO_MEMORY(ctdb, info);
3389 info->ip.addr = vnn->public_address;
3390 info->ip.pnn = vnn->pnn;
3391 info->active_idx = 0xFFFFFFFF;
3393 for (i=0; vnn->ifaces[i]; i++) {
3394 struct ctdb_iface *cur;
3396 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
3397 if (cur == NULL) {
3398 DEBUG(DEBUG_CRIT, (__location__ " internal error iface[%s] unknown\n",
3399 vnn->ifaces[i]));
3400 return -1;
3402 if (vnn->iface == cur) {
3403 info->active_idx = i;
3405 strncpy(info->ifaces[i].name, cur->name, sizeof(info->ifaces[i].name)-1);
3406 info->ifaces[i].link_state = cur->link_up;
3407 info->ifaces[i].references = cur->references;
3409 info->num = i;
3410 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3411 i*sizeof(struct ctdb_control_iface_info);
3413 outdata->dsize = len;
3414 outdata->dptr = (uint8_t *)info;
3416 return 0;
3419 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
3420 struct ctdb_req_control *c,
3421 TDB_DATA *outdata)
3423 int i, num, len;
3424 struct ctdb_control_get_ifaces *ifaces;
3425 struct ctdb_iface *cur;
3427 /* count how many public ip structures we have */
3428 num = 0;
3429 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3430 num++;
3433 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3434 num*sizeof(struct ctdb_control_iface_info);
3435 ifaces = talloc_zero_size(outdata, len);
3436 CTDB_NO_MEMORY(ctdb, ifaces);
3438 i = 0;
3439 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3440 strcpy(ifaces->ifaces[i].name, cur->name);
3441 ifaces->ifaces[i].link_state = cur->link_up;
3442 ifaces->ifaces[i].references = cur->references;
3443 i++;
3445 ifaces->num = i;
3446 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3447 i*sizeof(struct ctdb_control_iface_info);
3449 outdata->dsize = len;
3450 outdata->dptr = (uint8_t *)ifaces;
3452 return 0;
3455 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
3456 struct ctdb_req_control *c,
3457 TDB_DATA indata)
3459 struct ctdb_control_iface_info *info;
3460 struct ctdb_iface *iface;
3461 bool link_up = false;
3463 info = (struct ctdb_control_iface_info *)indata.dptr;
3465 if (info->name[CTDB_IFACE_SIZE] != '\0') {
3466 int len = strnlen(info->name, CTDB_IFACE_SIZE);
3467 DEBUG(DEBUG_ERR, (__location__ " name[%*.*s] not terminated\n",
3468 len, len, info->name));
3469 return -1;
3472 switch (info->link_state) {
3473 case 0:
3474 link_up = false;
3475 break;
3476 case 1:
3477 link_up = true;
3478 break;
3479 default:
3480 DEBUG(DEBUG_ERR, (__location__ " link_state[%u] invalid\n",
3481 (unsigned int)info->link_state));
3482 return -1;
3485 if (info->references != 0) {
3486 DEBUG(DEBUG_ERR, (__location__ " references[%u] should be 0\n",
3487 (unsigned int)info->references));
3488 return -1;
3491 iface = ctdb_find_iface(ctdb, info->name);
3492 if (iface == NULL) {
3493 return -1;
3496 if (link_up == iface->link_up) {
3497 return 0;
3500 DEBUG(iface->link_up?DEBUG_ERR:DEBUG_NOTICE,
3501 ("iface[%s] has changed it's link status %s => %s\n",
3502 iface->name,
3503 iface->link_up?"up":"down",
3504 link_up?"up":"down"));
3506 iface->link_up = link_up;
3507 return 0;
3512 structure containing the listening socket and the list of tcp connections
3513 that the ctdb daemon is to kill
3515 struct ctdb_kill_tcp {
3516 struct ctdb_vnn *vnn;
3517 struct ctdb_context *ctdb;
3518 int capture_fd;
3519 struct fd_event *fde;
3520 trbt_tree_t *connections;
3521 void *private_data;
3525 a tcp connection that is to be killed
3527 struct ctdb_killtcp_con {
3528 ctdb_sock_addr src_addr;
3529 ctdb_sock_addr dst_addr;
3530 int count;
3531 struct ctdb_kill_tcp *killtcp;
3534 /* this function is used to create a key to represent this socketpair
3535 in the killtcp tree.
3536 this key is used to insert and lookup matching socketpairs that are
3537 to be tickled and RST
3539 #define KILLTCP_KEYLEN 10
3540 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
3542 static uint32_t key[KILLTCP_KEYLEN];
3544 bzero(key, sizeof(key));
3546 if (src->sa.sa_family != dst->sa.sa_family) {
3547 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
3548 return key;
3551 switch (src->sa.sa_family) {
3552 case AF_INET:
3553 key[0] = dst->ip.sin_addr.s_addr;
3554 key[1] = src->ip.sin_addr.s_addr;
3555 key[2] = dst->ip.sin_port;
3556 key[3] = src->ip.sin_port;
3557 break;
3558 case AF_INET6: {
3559 uint32_t *dst6_addr32 =
3560 (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
3561 uint32_t *src6_addr32 =
3562 (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
3563 key[0] = dst6_addr32[3];
3564 key[1] = src6_addr32[3];
3565 key[2] = dst6_addr32[2];
3566 key[3] = src6_addr32[2];
3567 key[4] = dst6_addr32[1];
3568 key[5] = src6_addr32[1];
3569 key[6] = dst6_addr32[0];
3570 key[7] = src6_addr32[0];
3571 key[8] = dst->ip6.sin6_port;
3572 key[9] = src->ip6.sin6_port;
3573 break;
3575 default:
3576 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
3577 return key;
3580 return key;
3584 called when we get a read event on the raw socket
3586 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
3587 uint16_t flags, void *private_data)
3589 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3590 struct ctdb_killtcp_con *con;
3591 ctdb_sock_addr src, dst;
3592 uint32_t ack_seq, seq;
3594 if (!(flags & EVENT_FD_READ)) {
3595 return;
3598 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
3599 killtcp->private_data,
3600 &src, &dst,
3601 &ack_seq, &seq) != 0) {
3602 /* probably a non-tcp ACK packet */
3603 return;
3606 /* check if we have this guy in our list of connections
3607 to kill
3609 con = trbt_lookuparray32(killtcp->connections,
3610 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
3611 if (con == NULL) {
3612 /* no this was some other packet we can just ignore */
3613 return;
3616 /* This one has been tickled !
3617 now reset him and remove him from the list.
3619 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3620 ntohs(con->dst_addr.ip.sin_port),
3621 ctdb_addr_to_str(&con->src_addr),
3622 ntohs(con->src_addr.ip.sin_port)));
3624 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
3625 talloc_free(con);
3629 /* when traversing the list of all tcp connections to send tickle acks to
3630 (so that we can capture the ack coming back and kill the connection
3631 by a RST)
3632 this callback is called for each connection we are currently trying to kill
3634 static int tickle_connection_traverse(void *param, void *data)
3636 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
3638 /* have tried too many times, just give up */
3639 if (con->count >= 5) {
3640 /* can't delete in traverse: reparent to delete_cons */
3641 talloc_steal(param, con);
3642 return 0;
3645 /* othervise, try tickling it again */
3646 con->count++;
3647 ctdb_sys_send_tcp(
3648 (ctdb_sock_addr *)&con->dst_addr,
3649 (ctdb_sock_addr *)&con->src_addr,
3650 0, 0, 0);
3651 return 0;
3656 called every second until all sentenced connections have been reset
3658 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
3659 struct timeval t, void *private_data)
3661 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3662 void *delete_cons = talloc_new(NULL);
3664 /* loop over all connections sending tickle ACKs */
3665 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
3667 /* now we've finished traverse, it's safe to do deletion. */
3668 talloc_free(delete_cons);
3670 /* If there are no more connections to kill we can remove the
3671 entire killtcp structure
3673 if ( (killtcp->connections == NULL) ||
3674 (killtcp->connections->root == NULL) ) {
3675 talloc_free(killtcp);
3676 return;
3679 /* try tickling them again in a seconds time
3681 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3682 ctdb_tickle_sentenced_connections, killtcp);
3686 destroy the killtcp structure
3688 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
3690 struct ctdb_vnn *tmpvnn;
3692 /* verify that this vnn is still active */
3693 for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
3694 if (tmpvnn == killtcp->vnn) {
3695 break;
3699 if (tmpvnn == NULL) {
3700 return 0;
3703 if (killtcp->vnn->killtcp != killtcp) {
3704 return 0;
3707 killtcp->vnn->killtcp = NULL;
3709 return 0;
3713 /* nothing fancy here, just unconditionally replace any existing
3714 connection structure with the new one.
3716 dont even free the old one if it did exist, that one is talloc_stolen
3717 by the same node in the tree anyway and will be deleted when the new data
3718 is deleted
3720 static void *add_killtcp_callback(void *parm, void *data)
3722 return parm;
3726 add a tcp socket to the list of connections we want to RST
3728 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
3729 ctdb_sock_addr *s,
3730 ctdb_sock_addr *d)
3732 ctdb_sock_addr src, dst;
3733 struct ctdb_kill_tcp *killtcp;
3734 struct ctdb_killtcp_con *con;
3735 struct ctdb_vnn *vnn;
3737 ctdb_canonicalize_ip(s, &src);
3738 ctdb_canonicalize_ip(d, &dst);
3740 vnn = find_public_ip_vnn(ctdb, &dst);
3741 if (vnn == NULL) {
3742 vnn = find_public_ip_vnn(ctdb, &src);
3744 if (vnn == NULL) {
3745 /* if it is not a public ip it could be our 'single ip' */
3746 if (ctdb->single_ip_vnn) {
3747 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
3748 vnn = ctdb->single_ip_vnn;
3752 if (vnn == NULL) {
3753 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
3754 return -1;
3757 killtcp = vnn->killtcp;
3759 /* If this is the first connection to kill we must allocate
3760 a new structure
3762 if (killtcp == NULL) {
3763 killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
3764 CTDB_NO_MEMORY(ctdb, killtcp);
3766 killtcp->vnn = vnn;
3767 killtcp->ctdb = ctdb;
3768 killtcp->capture_fd = -1;
3769 killtcp->connections = trbt_create(killtcp, 0);
3771 vnn->killtcp = killtcp;
3772 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
3777 /* create a structure that describes this connection we want to
3778 RST and store it in killtcp->connections
3780 con = talloc(killtcp, struct ctdb_killtcp_con);
3781 CTDB_NO_MEMORY(ctdb, con);
3782 con->src_addr = src;
3783 con->dst_addr = dst;
3784 con->count = 0;
3785 con->killtcp = killtcp;
3788 trbt_insertarray32_callback(killtcp->connections,
3789 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
3790 add_killtcp_callback, con);
3793 If we dont have a socket to listen on yet we must create it
3795 if (killtcp->capture_fd == -1) {
3796 const char *iface = ctdb_vnn_iface_string(vnn);
3797 killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
3798 if (killtcp->capture_fd == -1) {
3799 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
3800 "socket on iface '%s' for killtcp (%s)\n",
3801 iface, strerror(errno)));
3802 goto failed;
3807 if (killtcp->fde == NULL) {
3808 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
3809 EVENT_FD_READ,
3810 capture_tcp_handler, killtcp);
3811 tevent_fd_set_auto_close(killtcp->fde);
3813 /* We also need to set up some events to tickle all these connections
3814 until they are all reset
3816 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3817 ctdb_tickle_sentenced_connections, killtcp);
3820 /* tickle him once now */
3821 ctdb_sys_send_tcp(
3822 &con->dst_addr,
3823 &con->src_addr,
3824 0, 0, 0);
3826 return 0;
3828 failed:
3829 talloc_free(vnn->killtcp);
3830 vnn->killtcp = NULL;
3831 return -1;
3835 kill a TCP connection.
3837 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
3839 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
3841 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
3845 called by a daemon to inform us of the entire list of TCP tickles for
3846 a particular public address.
3847 this control should only be sent by the node that is currently serving
3848 that public address.
3850 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
3852 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
3853 struct ctdb_tcp_array *tcparray;
3854 struct ctdb_vnn *vnn;
3856 /* We must at least have tickles.num or else we cant verify the size
3857 of the received data blob
3859 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3860 tickles.connections)) {
3861 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3862 return -1;
3865 /* verify that the size of data matches what we expect */
3866 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3867 tickles.connections)
3868 + sizeof(struct ctdb_tcp_connection)
3869 * list->tickles.num) {
3870 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3871 return -1;
3874 DEBUG(DEBUG_INFO, ("Received tickle update for public address %s\n",
3875 ctdb_addr_to_str(&list->addr)));
3877 vnn = find_public_ip_vnn(ctdb, &list->addr);
3878 if (vnn == NULL) {
3879 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
3880 ctdb_addr_to_str(&list->addr)));
3882 return 1;
3885 /* remove any old ticklelist we might have */
3886 talloc_free(vnn->tcp_array);
3887 vnn->tcp_array = NULL;
3889 tcparray = talloc(vnn, struct ctdb_tcp_array);
3890 CTDB_NO_MEMORY(ctdb, tcparray);
3892 tcparray->num = list->tickles.num;
3894 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
3895 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3897 memcpy(tcparray->connections, &list->tickles.connections[0],
3898 sizeof(struct ctdb_tcp_connection)*tcparray->num);
3900 /* We now have a new fresh tickle list array for this vnn */
3901 vnn->tcp_array = tcparray;
3903 return 0;
3907 called to return the full list of tickles for the puclic address associated
3908 with the provided vnn
3910 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
3912 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
3913 struct ctdb_control_tcp_tickle_list *list;
3914 struct ctdb_tcp_array *tcparray;
3915 int num;
3916 struct ctdb_vnn *vnn;
3918 vnn = find_public_ip_vnn(ctdb, addr);
3919 if (vnn == NULL) {
3920 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
3921 ctdb_addr_to_str(addr)));
3923 return 1;
3926 tcparray = vnn->tcp_array;
3927 if (tcparray) {
3928 num = tcparray->num;
3929 } else {
3930 num = 0;
3933 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3934 tickles.connections)
3935 + sizeof(struct ctdb_tcp_connection) * num;
3937 outdata->dptr = talloc_size(outdata, outdata->dsize);
3938 CTDB_NO_MEMORY(ctdb, outdata->dptr);
3939 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
3941 list->addr = *addr;
3942 list->tickles.num = num;
3943 if (num) {
3944 memcpy(&list->tickles.connections[0], tcparray->connections,
3945 sizeof(struct ctdb_tcp_connection) * num);
3948 return 0;
3953 set the list of all tcp tickles for a public address
3955 static int ctdb_send_set_tcp_tickles_for_ip(struct ctdb_context *ctdb,
3956 ctdb_sock_addr *addr,
3957 struct ctdb_tcp_array *tcparray)
3959 int ret, num;
3960 TDB_DATA data;
3961 struct ctdb_control_tcp_tickle_list *list;
3963 if (tcparray) {
3964 num = tcparray->num;
3965 } else {
3966 num = 0;
3969 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3970 tickles.connections) +
3971 sizeof(struct ctdb_tcp_connection) * num;
3972 data.dptr = talloc_size(ctdb, data.dsize);
3973 CTDB_NO_MEMORY(ctdb, data.dptr);
3975 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
3976 list->addr = *addr;
3977 list->tickles.num = num;
3978 if (tcparray) {
3979 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
3982 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL, 0,
3983 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
3984 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
3985 if (ret != 0) {
3986 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
3987 return -1;
3990 talloc_free(data.dptr);
3992 return ret;
3997 perform tickle updates if required
3999 static void ctdb_update_tcp_tickles(struct event_context *ev,
4000 struct timed_event *te,
4001 struct timeval t, void *private_data)
4003 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
4004 int ret;
4005 struct ctdb_vnn *vnn;
4007 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4008 /* we only send out updates for public addresses that
4009 we have taken over
4011 if (ctdb->pnn != vnn->pnn) {
4012 continue;
4014 /* We only send out the updates if we need to */
4015 if (!vnn->tcp_update_needed) {
4016 continue;
4018 ret = ctdb_send_set_tcp_tickles_for_ip(ctdb,
4019 &vnn->public_address,
4020 vnn->tcp_array);
4021 if (ret != 0) {
4022 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
4023 ctdb_addr_to_str(&vnn->public_address)));
4024 } else {
4025 DEBUG(DEBUG_INFO,
4026 ("Sent tickle update for public address %s\n",
4027 ctdb_addr_to_str(&vnn->public_address)));
4028 vnn->tcp_update_needed = false;
4032 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4033 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4034 ctdb_update_tcp_tickles, ctdb);
4039 start periodic update of tcp tickles
4041 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
4043 ctdb->tickle_update_context = talloc_new(ctdb);
4045 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4046 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4047 ctdb_update_tcp_tickles, ctdb);
4053 struct control_gratious_arp {
4054 struct ctdb_context *ctdb;
4055 ctdb_sock_addr addr;
4056 const char *iface;
4057 int count;
4061 send a control_gratuitous arp
4063 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
4064 struct timeval t, void *private_data)
4066 int ret;
4067 struct control_gratious_arp *arp = talloc_get_type(private_data,
4068 struct control_gratious_arp);
4070 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
4071 if (ret != 0) {
4072 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp on iface '%s' failed (%s)\n",
4073 arp->iface, strerror(errno)));
4077 arp->count++;
4078 if (arp->count == CTDB_ARP_REPEAT) {
4079 talloc_free(arp);
4080 return;
4083 event_add_timed(arp->ctdb->ev, arp,
4084 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
4085 send_gratious_arp, arp);
4090 send a gratious arp
4092 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
4094 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
4095 struct control_gratious_arp *arp;
4097 /* verify the size of indata */
4098 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
4099 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
4100 (unsigned)indata.dsize,
4101 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
4102 return -1;
4104 if (indata.dsize !=
4105 ( offsetof(struct ctdb_control_gratious_arp, iface)
4106 + gratious_arp->len ) ){
4108 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4109 "but should be %u bytes\n",
4110 (unsigned)indata.dsize,
4111 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
4112 return -1;
4116 arp = talloc(ctdb, struct control_gratious_arp);
4117 CTDB_NO_MEMORY(ctdb, arp);
4119 arp->ctdb = ctdb;
4120 arp->addr = gratious_arp->addr;
4121 arp->iface = talloc_strdup(arp, gratious_arp->iface);
4122 CTDB_NO_MEMORY(ctdb, arp->iface);
4123 arp->count = 0;
4125 event_add_timed(arp->ctdb->ev, arp,
4126 timeval_zero(), send_gratious_arp, arp);
4128 return 0;
4131 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
4133 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4134 int ret;
4136 /* verify the size of indata */
4137 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4138 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4139 return -1;
4141 if (indata.dsize !=
4142 ( offsetof(struct ctdb_control_ip_iface, iface)
4143 + pub->len ) ){
4145 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4146 "but should be %u bytes\n",
4147 (unsigned)indata.dsize,
4148 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4149 return -1;
4152 DEBUG(DEBUG_NOTICE,("Add IP %s\n", ctdb_addr_to_str(&pub->addr)));
4154 ret = ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0], true);
4156 if (ret != 0) {
4157 DEBUG(DEBUG_ERR,(__location__ " Failed to add public address\n"));
4158 return -1;
4161 return 0;
4164 struct delete_ip_callback_state {
4165 struct ctdb_req_control *c;
4169 called when releaseip event finishes for del_public_address
4171 static void delete_ip_callback(struct ctdb_context *ctdb,
4172 int32_t status, TDB_DATA data,
4173 const char *errormsg,
4174 void *private_data)
4176 struct delete_ip_callback_state *state =
4177 talloc_get_type(private_data, struct delete_ip_callback_state);
4179 /* If release failed then fail. */
4180 ctdb_request_control_reply(ctdb, state->c, NULL, status, errormsg);
4181 talloc_free(private_data);
4184 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb,
4185 struct ctdb_req_control *c,
4186 TDB_DATA indata, bool *async_reply)
4188 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4189 struct ctdb_vnn *vnn;
4191 /* verify the size of indata */
4192 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4193 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4194 return -1;
4196 if (indata.dsize !=
4197 ( offsetof(struct ctdb_control_ip_iface, iface)
4198 + pub->len ) ){
4200 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4201 "but should be %u bytes\n",
4202 (unsigned)indata.dsize,
4203 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4204 return -1;
4207 DEBUG(DEBUG_NOTICE,("Delete IP %s\n", ctdb_addr_to_str(&pub->addr)));
4209 /* walk over all public addresses until we find a match */
4210 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4211 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
4212 if (vnn->pnn == ctdb->pnn) {
4213 struct delete_ip_callback_state *state;
4214 struct ctdb_public_ip *ip;
4215 TDB_DATA data;
4216 int ret;
4218 vnn->delete_pending = true;
4220 state = talloc(ctdb,
4221 struct delete_ip_callback_state);
4222 CTDB_NO_MEMORY(ctdb, state);
4223 state->c = c;
4225 ip = talloc(state, struct ctdb_public_ip);
4226 if (ip == NULL) {
4227 DEBUG(DEBUG_ERR,
4228 (__location__ " Out of memory\n"));
4229 talloc_free(state);
4230 return -1;
4232 ip->pnn = -1;
4233 ip->addr = pub->addr;
4235 data.dsize = sizeof(struct ctdb_public_ip);
4236 data.dptr = (unsigned char *)ip;
4238 ret = ctdb_daemon_send_control(ctdb,
4239 ctdb_get_pnn(ctdb),
4241 CTDB_CONTROL_RELEASE_IP,
4242 0, 0,
4243 data,
4244 delete_ip_callback,
4245 state);
4246 if (ret == -1) {
4247 DEBUG(DEBUG_ERR,
4248 (__location__ "Unable to send "
4249 "CTDB_CONTROL_RELEASE_IP\n"));
4250 talloc_free(state);
4251 return -1;
4254 state->c = talloc_steal(state, c);
4255 *async_reply = true;
4256 } else {
4257 /* This IP is not hosted on the
4258 * current node so just delete it
4259 * now. */
4260 do_delete_ip(ctdb, vnn);
4263 return 0;
4267 DEBUG(DEBUG_ERR,("Delete IP of unknown public IP address %s\n",
4268 ctdb_addr_to_str(&pub->addr)));
4269 return -1;
4273 struct ipreallocated_callback_state {
4274 struct ctdb_req_control *c;
4277 static void ctdb_ipreallocated_callback(struct ctdb_context *ctdb,
4278 int status, void *p)
4280 struct ipreallocated_callback_state *state =
4281 talloc_get_type(p, struct ipreallocated_callback_state);
4283 if (status != 0) {
4284 DEBUG(DEBUG_ERR,
4285 (" \"ipreallocated\" event script failed (status %d)\n",
4286 status));
4287 if (status == -ETIME) {
4288 ctdb_ban_self(ctdb);
4292 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
4293 talloc_free(state);
4296 /* A control to run the ipreallocated event */
4297 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb,
4298 struct ctdb_req_control *c,
4299 bool *async_reply)
4301 int ret;
4302 struct ipreallocated_callback_state *state;
4304 state = talloc(ctdb, struct ipreallocated_callback_state);
4305 CTDB_NO_MEMORY(ctdb, state);
4307 DEBUG(DEBUG_INFO,(__location__ " Running \"ipreallocated\" event\n"));
4309 ret = ctdb_event_script_callback(ctdb, state,
4310 ctdb_ipreallocated_callback, state,
4311 CTDB_EVENT_IPREALLOCATED,
4312 "%s", "");
4314 if (ret != 0) {
4315 DEBUG(DEBUG_ERR,("Failed to run \"ipreallocated\" event \n"));
4316 talloc_free(state);
4317 return -1;
4320 /* tell the control that we will be reply asynchronously */
4321 state->c = talloc_steal(state, c);
4322 *async_reply = true;
4324 return 0;
4328 /* This function is called from the recovery daemon to verify that a remote
4329 node has the expected ip allocation.
4330 This is verified against ctdb->ip_tree
4332 int verify_remote_ip_allocation(struct ctdb_context *ctdb,
4333 struct ctdb_all_public_ips *ips,
4334 uint32_t pnn)
4336 struct ctdb_public_ip_list *tmp_ip;
4337 int i;
4339 if (ctdb->ip_tree == NULL) {
4340 /* dont know the expected allocation yet, assume remote node
4341 is correct. */
4342 return 0;
4345 if (ips == NULL) {
4346 return 0;
4349 for (i=0; i<ips->num; i++) {
4350 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ips->ips[i].addr));
4351 if (tmp_ip == NULL) {
4352 DEBUG(DEBUG_ERR,("Node %u has new or unknown public IP %s\n", pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
4353 return -1;
4356 if (tmp_ip->pnn == -1 || ips->ips[i].pnn == -1) {
4357 continue;
4360 if (tmp_ip->pnn != ips->ips[i].pnn) {
4361 DEBUG(DEBUG_ERR,
4362 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4363 pnn,
4364 ctdb_addr_to_str(&ips->ips[i].addr),
4365 ips->ips[i].pnn, tmp_ip->pnn));
4366 return -1;
4370 return 0;
4373 int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
4375 struct ctdb_public_ip_list *tmp_ip;
4377 if (ctdb->ip_tree == NULL) {
4378 DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4379 return -1;
4382 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
4383 if (tmp_ip == NULL) {
4384 DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
4385 return -1;
4388 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));
4389 tmp_ip->pnn = ip->pnn;
4391 return 0;
4395 struct ctdb_reloadips_handle {
4396 struct ctdb_context *ctdb;
4397 struct ctdb_req_control *c;
4398 int status;
4399 int fd[2];
4400 pid_t child;
4401 struct fd_event *fde;
4404 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle *h)
4406 if (h == h->ctdb->reload_ips) {
4407 h->ctdb->reload_ips = NULL;
4409 if (h->c != NULL) {
4410 ctdb_request_control_reply(h->ctdb, h->c, NULL, h->status, NULL);
4411 h->c = NULL;
4413 ctdb_kill(h->ctdb, h->child, SIGKILL);
4414 return 0;
4417 static void ctdb_reloadips_timeout_event(struct event_context *ev,
4418 struct timed_event *te,
4419 struct timeval t, void *private_data)
4421 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4423 talloc_free(h);
4426 static void ctdb_reloadips_child_handler(struct event_context *ev, struct fd_event *fde,
4427 uint16_t flags, void *private_data)
4429 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4431 char res;
4432 int ret;
4434 ret = sys_read(h->fd[0], &res, 1);
4435 if (ret < 1 || res != 0) {
4436 DEBUG(DEBUG_ERR, (__location__ " Reloadips child process returned error\n"));
4437 res = 1;
4439 h->status = res;
4441 talloc_free(h);
4444 static int ctdb_reloadips_child(struct ctdb_context *ctdb)
4446 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4447 struct ctdb_all_public_ips *ips;
4448 struct ctdb_vnn *vnn;
4449 struct client_async_data *async_data;
4450 struct timeval timeout;
4451 TDB_DATA data;
4452 struct ctdb_client_control_state *state;
4453 bool first_add;
4454 int i, ret;
4456 CTDB_NO_MEMORY(ctdb, mem_ctx);
4458 /* Read IPs from local node */
4459 ret = ctdb_ctrl_get_public_ips(ctdb, TAKEOVER_TIMEOUT(),
4460 CTDB_CURRENT_NODE, mem_ctx, &ips);
4461 if (ret != 0) {
4462 DEBUG(DEBUG_ERR,
4463 ("Unable to fetch public IPs from local node\n"));
4464 talloc_free(mem_ctx);
4465 return -1;
4468 /* Read IPs file - this is safe since this is a child process */
4469 ctdb->vnn = NULL;
4470 if (ctdb_set_public_addresses(ctdb, false) != 0) {
4471 DEBUG(DEBUG_ERR,("Failed to re-read public addresses file\n"));
4472 talloc_free(mem_ctx);
4473 return -1;
4476 async_data = talloc_zero(mem_ctx, struct client_async_data);
4477 CTDB_NO_MEMORY(ctdb, async_data);
4479 /* Compare IPs between node and file for IPs to be deleted */
4480 for (i = 0; i < ips->num; i++) {
4481 /* */
4482 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4483 if (ctdb_same_ip(&vnn->public_address,
4484 &ips->ips[i].addr)) {
4485 /* IP is still in file */
4486 break;
4490 if (vnn == NULL) {
4491 /* Delete IP ips->ips[i] */
4492 struct ctdb_control_ip_iface *pub;
4494 DEBUG(DEBUG_NOTICE,
4495 ("IP %s no longer configured, deleting it\n",
4496 ctdb_addr_to_str(&ips->ips[i].addr)));
4498 pub = talloc_zero(mem_ctx,
4499 struct ctdb_control_ip_iface);
4500 CTDB_NO_MEMORY(ctdb, pub);
4502 pub->addr = ips->ips[i].addr;
4503 pub->mask = 0;
4504 pub->len = 0;
4506 timeout = TAKEOVER_TIMEOUT();
4508 data.dsize = offsetof(struct ctdb_control_ip_iface,
4509 iface) + pub->len;
4510 data.dptr = (uint8_t *)pub;
4512 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4513 CTDB_CONTROL_DEL_PUBLIC_IP,
4514 0, data, async_data,
4515 &timeout, NULL);
4516 if (state == NULL) {
4517 DEBUG(DEBUG_ERR,
4518 (__location__
4519 " failed sending CTDB_CONTROL_DEL_PUBLIC_IP\n"));
4520 goto failed;
4523 ctdb_client_async_add(async_data, state);
4527 /* Compare IPs between node and file for IPs to be added */
4528 first_add = true;
4529 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4530 for (i = 0; i < ips->num; i++) {
4531 if (ctdb_same_ip(&vnn->public_address,
4532 &ips->ips[i].addr)) {
4533 /* IP already on node */
4534 break;
4537 if (i == ips->num) {
4538 /* Add IP ips->ips[i] */
4539 struct ctdb_control_ip_iface *pub;
4540 const char *ifaces = NULL;
4541 uint32_t len;
4542 int iface = 0;
4544 DEBUG(DEBUG_NOTICE,
4545 ("New IP %s configured, adding it\n",
4546 ctdb_addr_to_str(&vnn->public_address)));
4547 if (first_add) {
4548 uint32_t pnn = ctdb_get_pnn(ctdb);
4550 data.dsize = sizeof(pnn);
4551 data.dptr = (uint8_t *)&pnn;
4553 ret = ctdb_client_send_message(
4554 ctdb,
4555 CTDB_BROADCAST_CONNECTED,
4556 CTDB_SRVID_REBALANCE_NODE,
4557 data);
4558 if (ret != 0) {
4559 DEBUG(DEBUG_WARNING,
4560 ("Failed to send message to force node reallocation - IPs may be unbalanced\n"));
4563 first_add = false;
4566 ifaces = vnn->ifaces[0];
4567 iface = 1;
4568 while (vnn->ifaces[iface] != NULL) {
4569 ifaces = talloc_asprintf(vnn, "%s,%s", ifaces,
4570 vnn->ifaces[iface]);
4571 iface++;
4574 len = strlen(ifaces) + 1;
4575 pub = talloc_zero_size(mem_ctx,
4576 offsetof(struct ctdb_control_ip_iface, iface) + len);
4577 CTDB_NO_MEMORY(ctdb, pub);
4579 pub->addr = vnn->public_address;
4580 pub->mask = vnn->public_netmask_bits;
4581 pub->len = len;
4582 memcpy(&pub->iface[0], ifaces, pub->len);
4584 timeout = TAKEOVER_TIMEOUT();
4586 data.dsize = offsetof(struct ctdb_control_ip_iface,
4587 iface) + pub->len;
4588 data.dptr = (uint8_t *)pub;
4590 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4591 CTDB_CONTROL_ADD_PUBLIC_IP,
4592 0, data, async_data,
4593 &timeout, NULL);
4594 if (state == NULL) {
4595 DEBUG(DEBUG_ERR,
4596 (__location__
4597 " failed sending CTDB_CONTROL_ADD_PUBLIC_IP\n"));
4598 goto failed;
4601 ctdb_client_async_add(async_data, state);
4605 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
4606 DEBUG(DEBUG_ERR,(__location__ " Add/delete IPs failed\n"));
4607 goto failed;
4610 talloc_free(mem_ctx);
4611 return 0;
4613 failed:
4614 talloc_free(mem_ctx);
4615 return -1;
4618 /* This control is sent to force the node to re-read the public addresses file
4619 and drop any addresses we should nnot longer host, and add new addresses
4620 that we are now able to host
4622 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
4624 struct ctdb_reloadips_handle *h;
4625 pid_t parent = getpid();
4627 if (ctdb->reload_ips != NULL) {
4628 talloc_free(ctdb->reload_ips);
4629 ctdb->reload_ips = NULL;
4632 h = talloc(ctdb, struct ctdb_reloadips_handle);
4633 CTDB_NO_MEMORY(ctdb, h);
4634 h->ctdb = ctdb;
4635 h->c = NULL;
4636 h->status = -1;
4638 if (pipe(h->fd) == -1) {
4639 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
4640 talloc_free(h);
4641 return -1;
4644 h->child = ctdb_fork(ctdb);
4645 if (h->child == (pid_t)-1) {
4646 DEBUG(DEBUG_ERR, ("Failed to fork a child for reloadips\n"));
4647 close(h->fd[0]);
4648 close(h->fd[1]);
4649 talloc_free(h);
4650 return -1;
4653 /* child process */
4654 if (h->child == 0) {
4655 signed char res = 0;
4657 close(h->fd[0]);
4658 debug_extra = talloc_asprintf(NULL, "reloadips:");
4660 ctdb_set_process_name("ctdb_reloadips");
4661 if (switch_from_server_to_client(ctdb, "reloadips-child") != 0) {
4662 DEBUG(DEBUG_CRIT,("ERROR: Failed to switch reloadips child into client mode\n"));
4663 res = -1;
4664 } else {
4665 res = ctdb_reloadips_child(ctdb);
4666 if (res != 0) {
4667 DEBUG(DEBUG_ERR,("Failed to reload ips on local node\n"));
4671 sys_write(h->fd[1], &res, 1);
4672 /* make sure we die when our parent dies */
4673 while (ctdb_kill(ctdb, parent, 0) == 0 || errno != ESRCH) {
4674 sleep(5);
4676 _exit(0);
4679 h->c = talloc_steal(h, c);
4681 close(h->fd[1]);
4682 set_close_on_exec(h->fd[0]);
4684 talloc_set_destructor(h, ctdb_reloadips_destructor);
4687 h->fde = event_add_fd(ctdb->ev, h, h->fd[0],
4688 EVENT_FD_READ, ctdb_reloadips_child_handler,
4689 (void *)h);
4690 tevent_fd_set_auto_close(h->fde);
4692 event_add_timed(ctdb->ev, h,
4693 timeval_current_ofs(120, 0),
4694 ctdb_reloadips_timeout_event, h);
4696 /* we reply later */
4697 *async_reply = true;
4698 return 0;