recoverd: Fail takeover run if "ipreallocated" fails
[Samba.git] / ctdb / server / ctdb_takeover.c
blob15619f3e90c7cf1001b68c547db7161426f49e03
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;
42 struct ctdb_iface {
43 struct ctdb_iface *prev, *next;
44 const char *name;
45 bool link_up;
46 uint32_t references;
49 static const char *ctdb_vnn_iface_string(const struct ctdb_vnn *vnn)
51 if (vnn->iface) {
52 return vnn->iface->name;
55 return "__none__";
58 static int ctdb_add_local_iface(struct ctdb_context *ctdb, const char *iface)
60 struct ctdb_iface *i;
62 /* Verify that we dont have an entry for this ip yet */
63 for (i=ctdb->ifaces;i;i=i->next) {
64 if (strcmp(i->name, iface) == 0) {
65 return 0;
69 /* create a new structure for this interface */
70 i = talloc_zero(ctdb, struct ctdb_iface);
71 CTDB_NO_MEMORY_FATAL(ctdb, i);
72 i->name = talloc_strdup(i, iface);
73 CTDB_NO_MEMORY(ctdb, i->name);
75 * If link_up defaults to true then IPs can be allocated to a
76 * node during the first recovery. However, then an interface
77 * could have its link marked down during the startup event,
78 * causing the IP to move almost immediately. If link_up
79 * defaults to false then, during normal operation, IPs added
80 * to a new interface can't be assigned until a monitor cycle
81 * has occurred and marked the new interfaces up. This makes
82 * IP allocation unpredictable. The following is a neat
83 * compromise: early in startup link_up defaults to false, so
84 * IPs can't be assigned, and after startup IPs can be
85 * assigned immediately.
87 i->link_up = (ctdb->runstate == CTDB_RUNSTATE_RUNNING);
89 DLIST_ADD(ctdb->ifaces, i);
91 return 0;
94 static bool vnn_has_interface_with_name(struct ctdb_vnn *vnn,
95 const char *name)
97 int n;
99 for (n = 0; vnn->ifaces[n] != NULL; n++) {
100 if (strcmp(name, vnn->ifaces[n]) == 0) {
101 return true;
105 return false;
108 /* If any interfaces now have no possible IPs then delete them. This
109 * implementation is naive (i.e. simple) rather than clever
110 * (i.e. complex). Given that this is run on delip and that operation
111 * is rare, this doesn't need to be efficient - it needs to be
112 * foolproof. One alternative is reference counting, where the logic
113 * is distributed and can, therefore, be broken in multiple places.
114 * Another alternative is to build a red-black tree of interfaces that
115 * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
116 * once) and then walking ctdb->ifaces once and deleting those not in
117 * the tree. Let's go to one of those if the naive implementation
118 * causes problems... :-)
120 static void ctdb_remove_orphaned_ifaces(struct ctdb_context *ctdb,
121 struct ctdb_vnn *vnn,
122 TALLOC_CTX *mem_ctx)
124 struct ctdb_iface *i;
126 /* For each interface, check if there's an IP using it. */
127 for(i=ctdb->ifaces; i; i=i->next) {
128 struct ctdb_vnn *tv;
129 bool found;
131 /* Only consider interfaces named in the given VNN. */
132 if (!vnn_has_interface_with_name(vnn, i->name)) {
133 continue;
136 /* Is the "single IP" on this interface? */
137 if ((ctdb->single_ip_vnn != NULL) &&
138 (ctdb->single_ip_vnn->ifaces[0] != NULL) &&
139 (strcmp(i->name, ctdb->single_ip_vnn->ifaces[0]) == 0)) {
140 /* Found, next interface please... */
141 continue;
143 /* Search for a vnn with this interface. */
144 found = false;
145 for (tv=ctdb->vnn; tv; tv=tv->next) {
146 if (vnn_has_interface_with_name(tv, i->name)) {
147 found = true;
148 break;
152 if (!found) {
153 /* None of the VNNs are using this interface. */
154 DLIST_REMOVE(ctdb->ifaces, i);
155 /* Caller will free mem_ctx when convenient. */
156 talloc_steal(mem_ctx, 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->iface && vnn->iface->link_up) {
264 return true;
267 for (i=0; vnn->ifaces[i]; i++) {
268 struct ctdb_iface *cur;
270 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
271 if (cur == NULL) {
272 continue;
275 if (cur->link_up) {
276 return true;
280 return false;
283 struct ctdb_takeover_arp {
284 struct ctdb_context *ctdb;
285 uint32_t count;
286 ctdb_sock_addr addr;
287 struct ctdb_tcp_array *tcparray;
288 struct ctdb_vnn *vnn;
293 lists of tcp endpoints
295 struct ctdb_tcp_list {
296 struct ctdb_tcp_list *prev, *next;
297 struct ctdb_tcp_connection connection;
301 list of clients to kill on IP release
303 struct ctdb_client_ip {
304 struct ctdb_client_ip *prev, *next;
305 struct ctdb_context *ctdb;
306 ctdb_sock_addr addr;
307 uint32_t client_id;
312 send a gratuitous arp
314 static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te,
315 struct timeval t, void *private_data)
317 struct ctdb_takeover_arp *arp = talloc_get_type(private_data,
318 struct ctdb_takeover_arp);
319 int i, ret;
320 struct ctdb_tcp_array *tcparray;
321 const char *iface = ctdb_vnn_iface_string(arp->vnn);
323 ret = ctdb_sys_send_arp(&arp->addr, iface);
324 if (ret != 0) {
325 DEBUG(DEBUG_CRIT,(__location__ " sending of arp failed on iface '%s' (%s)\n",
326 iface, strerror(errno)));
329 tcparray = arp->tcparray;
330 if (tcparray) {
331 for (i=0;i<tcparray->num;i++) {
332 struct ctdb_tcp_connection *tcon;
334 tcon = &tcparray->connections[i];
335 DEBUG(DEBUG_INFO,("sending tcp tickle ack for %u->%s:%u\n",
336 (unsigned)ntohs(tcon->dst_addr.ip.sin_port),
337 ctdb_addr_to_str(&tcon->src_addr),
338 (unsigned)ntohs(tcon->src_addr.ip.sin_port)));
339 ret = ctdb_sys_send_tcp(
340 &tcon->src_addr,
341 &tcon->dst_addr,
342 0, 0, 0);
343 if (ret != 0) {
344 DEBUG(DEBUG_CRIT,(__location__ " Failed to send tcp tickle ack for %s\n",
345 ctdb_addr_to_str(&tcon->src_addr)));
350 arp->count++;
352 if (arp->count == CTDB_ARP_REPEAT) {
353 talloc_free(arp);
354 return;
357 event_add_timed(arp->ctdb->ev, arp->vnn->takeover_ctx,
358 timeval_current_ofs(CTDB_ARP_INTERVAL, 100000),
359 ctdb_control_send_arp, arp);
362 static int32_t ctdb_announce_vnn_iface(struct ctdb_context *ctdb,
363 struct ctdb_vnn *vnn)
365 struct ctdb_takeover_arp *arp;
366 struct ctdb_tcp_array *tcparray;
368 if (!vnn->takeover_ctx) {
369 vnn->takeover_ctx = talloc_new(vnn);
370 if (!vnn->takeover_ctx) {
371 return -1;
375 arp = talloc_zero(vnn->takeover_ctx, struct ctdb_takeover_arp);
376 if (!arp) {
377 return -1;
380 arp->ctdb = ctdb;
381 arp->addr = vnn->public_address;
382 arp->vnn = vnn;
384 tcparray = vnn->tcp_array;
385 if (tcparray) {
386 /* add all of the known tcp connections for this IP to the
387 list of tcp connections to send tickle acks for */
388 arp->tcparray = talloc_steal(arp, tcparray);
390 vnn->tcp_array = NULL;
391 vnn->tcp_update_needed = true;
394 event_add_timed(arp->ctdb->ev, vnn->takeover_ctx,
395 timeval_zero(), ctdb_control_send_arp, arp);
397 return 0;
400 struct takeover_callback_state {
401 struct ctdb_req_control *c;
402 ctdb_sock_addr *addr;
403 struct ctdb_vnn *vnn;
406 struct ctdb_do_takeip_state {
407 struct ctdb_req_control *c;
408 struct ctdb_vnn *vnn;
412 called when takeip event finishes
414 static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
415 void *private_data)
417 struct ctdb_do_takeip_state *state =
418 talloc_get_type(private_data, struct ctdb_do_takeip_state);
419 int32_t ret;
420 TDB_DATA data;
422 if (status != 0) {
423 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
425 if (status == -ETIME) {
426 ctdb_ban_self(ctdb);
428 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
429 ctdb_addr_to_str(&state->vnn->public_address),
430 ctdb_vnn_iface_string(state->vnn)));
431 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
433 node->flags |= NODE_FLAGS_UNHEALTHY;
434 talloc_free(state);
435 return;
438 if (ctdb->do_checkpublicip) {
440 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
441 if (ret != 0) {
442 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
443 talloc_free(state);
444 return;
449 data.dptr = (uint8_t *)ctdb_addr_to_str(&state->vnn->public_address);
450 data.dsize = strlen((char *)data.dptr) + 1;
451 DEBUG(DEBUG_INFO,(__location__ " sending TAKE_IP for '%s'\n", data.dptr));
453 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_TAKE_IP, data);
456 /* the control succeeded */
457 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
458 talloc_free(state);
459 return;
462 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state *state)
464 state->vnn->update_in_flight = false;
465 return 0;
469 take over an ip address
471 static int32_t ctdb_do_takeip(struct ctdb_context *ctdb,
472 struct ctdb_req_control *c,
473 struct ctdb_vnn *vnn)
475 int ret;
476 struct ctdb_do_takeip_state *state;
478 if (vnn->update_in_flight) {
479 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u rejected "
480 "update for this IP already in flight\n",
481 ctdb_addr_to_str(&vnn->public_address),
482 vnn->public_netmask_bits));
483 return -1;
486 ret = ctdb_vnn_assign_iface(ctdb, vnn);
487 if (ret != 0) {
488 DEBUG(DEBUG_ERR,("Takeover of IP %s/%u failed to "
489 "assign a usable interface\n",
490 ctdb_addr_to_str(&vnn->public_address),
491 vnn->public_netmask_bits));
492 return -1;
495 state = talloc(vnn, struct ctdb_do_takeip_state);
496 CTDB_NO_MEMORY(ctdb, state);
498 state->c = talloc_steal(ctdb, c);
499 state->vnn = vnn;
501 vnn->update_in_flight = true;
502 talloc_set_destructor(state, ctdb_takeip_destructor);
504 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u on interface %s\n",
505 ctdb_addr_to_str(&vnn->public_address),
506 vnn->public_netmask_bits,
507 ctdb_vnn_iface_string(vnn)));
509 ret = ctdb_event_script_callback(ctdb,
510 state,
511 ctdb_do_takeip_callback,
512 state,
513 false,
514 CTDB_EVENT_TAKE_IP,
515 "%s %s %u",
516 ctdb_vnn_iface_string(vnn),
517 ctdb_addr_to_str(&vnn->public_address),
518 vnn->public_netmask_bits);
520 if (ret != 0) {
521 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
522 ctdb_addr_to_str(&vnn->public_address),
523 ctdb_vnn_iface_string(vnn)));
524 talloc_free(state);
525 return -1;
528 return 0;
531 struct ctdb_do_updateip_state {
532 struct ctdb_req_control *c;
533 struct ctdb_iface *old;
534 struct ctdb_vnn *vnn;
538 called when updateip event finishes
540 static void ctdb_do_updateip_callback(struct ctdb_context *ctdb, int status,
541 void *private_data)
543 struct ctdb_do_updateip_state *state =
544 talloc_get_type(private_data, struct ctdb_do_updateip_state);
545 int32_t ret;
547 if (status != 0) {
548 if (status == -ETIME) {
549 ctdb_ban_self(ctdb);
551 DEBUG(DEBUG_ERR,(__location__ " Failed to move IP %s from interface %s to %s\n",
552 ctdb_addr_to_str(&state->vnn->public_address),
553 state->old->name,
554 ctdb_vnn_iface_string(state->vnn)));
557 * All we can do is reset the old interface
558 * and let the next run fix it
560 ctdb_vnn_unassign_iface(ctdb, state->vnn);
561 state->vnn->iface = state->old;
562 state->vnn->iface->references++;
564 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
565 talloc_free(state);
566 return;
569 if (ctdb->do_checkpublicip) {
571 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
572 if (ret != 0) {
573 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
574 talloc_free(state);
575 return;
580 /* the control succeeded */
581 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
582 talloc_free(state);
583 return;
586 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state *state)
588 state->vnn->update_in_flight = false;
589 return 0;
593 update (move) an ip address
595 static int32_t ctdb_do_updateip(struct ctdb_context *ctdb,
596 struct ctdb_req_control *c,
597 struct ctdb_vnn *vnn)
599 int ret;
600 struct ctdb_do_updateip_state *state;
601 struct ctdb_iface *old = vnn->iface;
602 const char *new_name;
604 if (vnn->update_in_flight) {
605 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u rejected "
606 "update for this IP already in flight\n",
607 ctdb_addr_to_str(&vnn->public_address),
608 vnn->public_netmask_bits));
609 return -1;
612 ctdb_vnn_unassign_iface(ctdb, vnn);
613 ret = ctdb_vnn_assign_iface(ctdb, vnn);
614 if (ret != 0) {
615 DEBUG(DEBUG_ERR,("update of IP %s/%u failed to "
616 "assin a usable interface (old iface '%s')\n",
617 ctdb_addr_to_str(&vnn->public_address),
618 vnn->public_netmask_bits,
619 old->name));
620 return -1;
623 new_name = ctdb_vnn_iface_string(vnn);
624 if (old->name != NULL && new_name != NULL && !strcmp(old->name, new_name)) {
625 /* A benign update from one interface onto itself.
626 * no need to run the eventscripts in this case, just return
627 * success.
629 ctdb_request_control_reply(ctdb, c, NULL, 0, NULL);
630 return 0;
633 state = talloc(vnn, struct ctdb_do_updateip_state);
634 CTDB_NO_MEMORY(ctdb, state);
636 state->c = talloc_steal(ctdb, c);
637 state->old = old;
638 state->vnn = vnn;
640 vnn->update_in_flight = true;
641 talloc_set_destructor(state, ctdb_updateip_destructor);
643 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u from "
644 "interface %s to %s\n",
645 ctdb_addr_to_str(&vnn->public_address),
646 vnn->public_netmask_bits,
647 old->name,
648 new_name));
650 ret = ctdb_event_script_callback(ctdb,
651 state,
652 ctdb_do_updateip_callback,
653 state,
654 false,
655 CTDB_EVENT_UPDATE_IP,
656 "%s %s %s %u",
657 state->old->name,
658 new_name,
659 ctdb_addr_to_str(&vnn->public_address),
660 vnn->public_netmask_bits);
661 if (ret != 0) {
662 DEBUG(DEBUG_ERR,(__location__ " Failed update IP %s from interface %s to %s\n",
663 ctdb_addr_to_str(&vnn->public_address),
664 old->name, new_name));
665 talloc_free(state);
666 return -1;
669 return 0;
673 Find the vnn of the node that has a public ip address
674 returns -1 if the address is not known as a public address
676 static struct ctdb_vnn *find_public_ip_vnn(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
678 struct ctdb_vnn *vnn;
680 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
681 if (ctdb_same_ip(&vnn->public_address, addr)) {
682 return vnn;
686 return NULL;
690 take over an ip address
692 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
693 struct ctdb_req_control *c,
694 TDB_DATA indata,
695 bool *async_reply)
697 int ret;
698 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
699 struct ctdb_vnn *vnn;
700 bool have_ip = false;
701 bool do_updateip = false;
702 bool do_takeip = false;
703 struct ctdb_iface *best_iface = NULL;
705 if (pip->pnn != ctdb->pnn) {
706 DEBUG(DEBUG_ERR,(__location__" takeoverip called for an ip '%s' "
707 "with pnn %d, but we're node %d\n",
708 ctdb_addr_to_str(&pip->addr),
709 pip->pnn, ctdb->pnn));
710 return -1;
713 /* update out vnn list */
714 vnn = find_public_ip_vnn(ctdb, &pip->addr);
715 if (vnn == NULL) {
716 DEBUG(DEBUG_INFO,("takeoverip called for an ip '%s' that is not a public address\n",
717 ctdb_addr_to_str(&pip->addr)));
718 return 0;
721 if (ctdb->do_checkpublicip) {
722 have_ip = ctdb_sys_have_ip(&pip->addr);
724 best_iface = ctdb_vnn_best_iface(ctdb, vnn);
725 if (best_iface == NULL) {
726 DEBUG(DEBUG_ERR,("takeoverip of IP %s/%u failed to find"
727 "a usable interface (old %s, have_ip %d)\n",
728 ctdb_addr_to_str(&vnn->public_address),
729 vnn->public_netmask_bits,
730 ctdb_vnn_iface_string(vnn),
731 have_ip));
732 return -1;
735 if (vnn->iface == NULL && vnn->pnn == -1 && have_ip && best_iface != NULL) {
736 DEBUG(DEBUG_ERR,("Taking over newly created ip\n"));
737 have_ip = false;
741 if (vnn->iface == NULL && have_ip) {
742 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
743 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
744 ctdb_addr_to_str(&vnn->public_address)));
745 return 0;
748 if (vnn->pnn != ctdb->pnn && have_ip && vnn->pnn != -1) {
749 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
750 "and we have it on iface[%s], but it was assigned to node %d"
751 "and we are node %d, banning ourself\n",
752 ctdb_addr_to_str(&vnn->public_address),
753 ctdb_vnn_iface_string(vnn), vnn->pnn, ctdb->pnn));
754 ctdb_ban_self(ctdb);
755 return -1;
758 if (vnn->pnn == -1 && have_ip) {
759 vnn->pnn = ctdb->pnn;
760 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
761 "and we already have it on iface[%s], update local daemon\n",
762 ctdb_addr_to_str(&vnn->public_address),
763 ctdb_vnn_iface_string(vnn)));
764 return 0;
767 if (vnn->iface) {
768 if (vnn->iface != best_iface) {
769 if (!vnn->iface->link_up) {
770 do_updateip = true;
771 } else if (vnn->iface->references > (best_iface->references + 1)) {
772 /* only move when the rebalance gains something */
773 do_updateip = true;
778 if (!have_ip) {
779 if (do_updateip) {
780 ctdb_vnn_unassign_iface(ctdb, vnn);
781 do_updateip = false;
783 do_takeip = true;
786 if (do_takeip) {
787 ret = ctdb_do_takeip(ctdb, c, vnn);
788 if (ret != 0) {
789 return -1;
791 } else if (do_updateip) {
792 ret = ctdb_do_updateip(ctdb, c, vnn);
793 if (ret != 0) {
794 return -1;
796 } else {
798 * The interface is up and the kernel known the ip
799 * => do nothing
801 DEBUG(DEBUG_INFO,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
802 ctdb_addr_to_str(&pip->addr),
803 vnn->public_netmask_bits,
804 ctdb_vnn_iface_string(vnn)));
805 return 0;
808 /* tell ctdb_control.c that we will be replying asynchronously */
809 *async_reply = true;
811 return 0;
815 takeover an ip address old v4 style
817 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb,
818 struct ctdb_req_control *c,
819 TDB_DATA indata,
820 bool *async_reply)
822 TDB_DATA data;
824 data.dsize = sizeof(struct ctdb_public_ip);
825 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
826 CTDB_NO_MEMORY(ctdb, data.dptr);
828 memcpy(data.dptr, indata.dptr, indata.dsize);
829 return ctdb_control_takeover_ip(ctdb, c, data, async_reply);
833 kill any clients that are registered with a IP that is being released
835 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
837 struct ctdb_client_ip *ip;
839 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
840 ctdb_addr_to_str(addr)));
842 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
843 ctdb_sock_addr tmp_addr;
845 tmp_addr = ip->addr;
846 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
847 ip->client_id,
848 ctdb_addr_to_str(&ip->addr)));
850 if (ctdb_same_ip(&tmp_addr, addr)) {
851 struct ctdb_client *client = ctdb_reqid_find(ctdb,
852 ip->client_id,
853 struct ctdb_client);
854 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
855 ip->client_id,
856 ctdb_addr_to_str(&ip->addr),
857 client->pid));
859 if (client->pid != 0) {
860 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
861 (unsigned)client->pid,
862 ctdb_addr_to_str(addr),
863 ip->client_id));
864 kill(client->pid, SIGKILL);
871 called when releaseip event finishes
873 static void release_ip_callback(struct ctdb_context *ctdb, int status,
874 void *private_data)
876 struct takeover_callback_state *state =
877 talloc_get_type(private_data, struct takeover_callback_state);
878 TDB_DATA data;
880 if (status == -ETIME) {
881 ctdb_ban_self(ctdb);
884 if (ctdb->do_checkpublicip && ctdb_sys_have_ip(state->addr)) {
885 DEBUG(DEBUG_ERR, ("IP %s still hosted during release IP callback, failing\n",
886 ctdb_addr_to_str(state->addr)));
887 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
888 talloc_free(state);
889 return;
892 /* send a message to all clients of this node telling them
893 that the cluster has been reconfigured and they should
894 release any sockets on this IP */
895 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
896 CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
897 data.dsize = strlen((char *)data.dptr)+1;
899 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
901 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
903 /* kill clients that have registered with this IP */
904 release_kill_clients(ctdb, state->addr);
906 ctdb_vnn_unassign_iface(ctdb, state->vnn);
908 /* the control succeeded */
909 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
910 talloc_free(state);
913 static int ctdb_releaseip_destructor(struct takeover_callback_state *state)
915 state->vnn->update_in_flight = false;
916 return 0;
920 release an ip address
922 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
923 struct ctdb_req_control *c,
924 TDB_DATA indata,
925 bool *async_reply)
927 int ret;
928 struct takeover_callback_state *state;
929 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
930 struct ctdb_vnn *vnn;
931 char *iface;
933 /* update our vnn list */
934 vnn = find_public_ip_vnn(ctdb, &pip->addr);
935 if (vnn == NULL) {
936 DEBUG(DEBUG_INFO,("releaseip called for an ip '%s' that is not a public address\n",
937 ctdb_addr_to_str(&pip->addr)));
938 return 0;
940 vnn->pnn = pip->pnn;
942 /* stop any previous arps */
943 talloc_free(vnn->takeover_ctx);
944 vnn->takeover_ctx = NULL;
946 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
947 * lazy multicast to drop an IP from any node that isn't the
948 * intended new node. The following causes makes ctdbd ignore
949 * a release for any address it doesn't host.
951 if (ctdb->do_checkpublicip) {
952 if (!ctdb_sys_have_ip(&pip->addr)) {
953 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
954 ctdb_addr_to_str(&pip->addr),
955 vnn->public_netmask_bits,
956 ctdb_vnn_iface_string(vnn)));
957 ctdb_vnn_unassign_iface(ctdb, vnn);
958 return 0;
960 } else {
961 if (vnn->iface == NULL) {
962 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u (ip not held)\n",
963 ctdb_addr_to_str(&pip->addr),
964 vnn->public_netmask_bits));
965 return 0;
969 /* There is a potential race between take_ip and us because we
970 * update the VNN via a callback that run when the
971 * eventscripts have been run. Avoid the race by allowing one
972 * update to be in flight at a time.
974 if (vnn->update_in_flight) {
975 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u rejected "
976 "update for this IP already in flight\n",
977 ctdb_addr_to_str(&vnn->public_address),
978 vnn->public_netmask_bits));
979 return -1;
982 if (ctdb->do_checkpublicip) {
983 iface = ctdb_sys_find_ifname(&pip->addr);
984 if (iface == NULL) {
985 DEBUG(DEBUG_ERR, ("Could not find which interface the ip address is hosted on. can not release it\n"));
986 return 0;
988 if (vnn->iface == NULL) {
989 DEBUG(DEBUG_WARNING,
990 ("Public IP %s is hosted on interface %s but we have no VNN\n",
991 ctdb_addr_to_str(&pip->addr),
992 iface));
993 } else if (strcmp(iface, ctdb_vnn_iface_string(vnn)) != 0) {
994 DEBUG(DEBUG_WARNING,
995 ("Public IP %s is hosted on inteterface %s but VNN says %s\n",
996 ctdb_addr_to_str(&pip->addr),
997 iface,
998 ctdb_vnn_iface_string(vnn)));
999 /* Should we fix vnn->iface? If we do, what
1000 * happens to reference counts?
1003 } else {
1004 iface = strdup(ctdb_vnn_iface_string(vnn));
1007 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s node:%d\n",
1008 ctdb_addr_to_str(&pip->addr),
1009 vnn->public_netmask_bits,
1010 iface,
1011 pip->pnn));
1013 state = talloc(ctdb, struct takeover_callback_state);
1014 CTDB_NO_MEMORY(ctdb, state);
1016 state->c = talloc_steal(state, c);
1017 state->addr = talloc(state, ctdb_sock_addr);
1018 CTDB_NO_MEMORY(ctdb, state->addr);
1019 *state->addr = pip->addr;
1020 state->vnn = vnn;
1022 vnn->update_in_flight = true;
1023 talloc_set_destructor(state, ctdb_releaseip_destructor);
1025 ret = ctdb_event_script_callback(ctdb,
1026 state, release_ip_callback, state,
1027 false,
1028 CTDB_EVENT_RELEASE_IP,
1029 "%s %s %u",
1030 iface,
1031 ctdb_addr_to_str(&pip->addr),
1032 vnn->public_netmask_bits);
1033 free(iface);
1034 if (ret != 0) {
1035 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
1036 ctdb_addr_to_str(&pip->addr),
1037 ctdb_vnn_iface_string(vnn)));
1038 talloc_free(state);
1039 return -1;
1042 /* tell the control that we will be reply asynchronously */
1043 *async_reply = true;
1044 return 0;
1048 release an ip address old v4 style
1050 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb,
1051 struct ctdb_req_control *c,
1052 TDB_DATA indata,
1053 bool *async_reply)
1055 TDB_DATA data;
1057 data.dsize = sizeof(struct ctdb_public_ip);
1058 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
1059 CTDB_NO_MEMORY(ctdb, data.dptr);
1061 memcpy(data.dptr, indata.dptr, indata.dsize);
1062 return ctdb_control_release_ip(ctdb, c, data, async_reply);
1066 static int ctdb_add_public_address(struct ctdb_context *ctdb,
1067 ctdb_sock_addr *addr,
1068 unsigned mask, const char *ifaces,
1069 bool check_address)
1071 struct ctdb_vnn *vnn;
1072 uint32_t num = 0;
1073 char *tmp;
1074 const char *iface;
1075 int i;
1076 int ret;
1078 tmp = strdup(ifaces);
1079 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1080 if (!ctdb_sys_check_iface_exists(iface)) {
1081 DEBUG(DEBUG_CRIT,("Interface %s does not exist. Can not add public-address : %s\n", iface, ctdb_addr_to_str(addr)));
1082 free(tmp);
1083 return -1;
1086 free(tmp);
1088 /* Verify that we dont have an entry for this ip yet */
1089 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1090 if (ctdb_same_sockaddr(addr, &vnn->public_address)) {
1091 DEBUG(DEBUG_CRIT,("Same ip '%s' specified multiple times in the public address list \n",
1092 ctdb_addr_to_str(addr)));
1093 return -1;
1097 /* create a new vnn structure for this ip address */
1098 vnn = talloc_zero(ctdb, struct ctdb_vnn);
1099 CTDB_NO_MEMORY_FATAL(ctdb, vnn);
1100 vnn->ifaces = talloc_array(vnn, const char *, num + 2);
1101 tmp = talloc_strdup(vnn, ifaces);
1102 CTDB_NO_MEMORY_FATAL(ctdb, tmp);
1103 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1104 vnn->ifaces = talloc_realloc(vnn, vnn->ifaces, const char *, num + 2);
1105 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces);
1106 vnn->ifaces[num] = talloc_strdup(vnn, iface);
1107 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces[num]);
1108 num++;
1110 talloc_free(tmp);
1111 vnn->ifaces[num] = NULL;
1112 vnn->public_address = *addr;
1113 vnn->public_netmask_bits = mask;
1114 vnn->pnn = -1;
1115 if (check_address) {
1116 if (ctdb_sys_have_ip(addr)) {
1117 DEBUG(DEBUG_ERR,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr), ctdb->pnn));
1118 vnn->pnn = ctdb->pnn;
1122 for (i=0; vnn->ifaces[i]; i++) {
1123 ret = ctdb_add_local_iface(ctdb, vnn->ifaces[i]);
1124 if (ret != 0) {
1125 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1126 "for public_address[%s]\n",
1127 vnn->ifaces[i], ctdb_addr_to_str(addr)));
1128 talloc_free(vnn);
1129 return -1;
1133 DLIST_ADD(ctdb->vnn, vnn);
1135 return 0;
1139 setup the event script directory
1141 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir)
1143 ctdb->event_script_dir = talloc_strdup(ctdb, script_dir);
1144 CTDB_NO_MEMORY(ctdb, ctdb->event_script_dir);
1145 return 0;
1148 static void ctdb_check_interfaces_event(struct event_context *ev, struct timed_event *te,
1149 struct timeval t, void *private_data)
1151 struct ctdb_context *ctdb = talloc_get_type(private_data,
1152 struct ctdb_context);
1153 struct ctdb_vnn *vnn;
1155 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1156 int i;
1158 for (i=0; vnn->ifaces[i] != NULL; i++) {
1159 if (!ctdb_sys_check_iface_exists(vnn->ifaces[i])) {
1160 DEBUG(DEBUG_CRIT,("Interface %s does not exist but is used by public ip %s\n",
1161 vnn->ifaces[i],
1162 ctdb_addr_to_str(&vnn->public_address)));
1167 event_add_timed(ctdb->ev, ctdb->check_public_ifaces_ctx,
1168 timeval_current_ofs(30, 0),
1169 ctdb_check_interfaces_event, ctdb);
1173 int ctdb_start_monitoring_interfaces(struct ctdb_context *ctdb)
1175 if (ctdb->check_public_ifaces_ctx != NULL) {
1176 talloc_free(ctdb->check_public_ifaces_ctx);
1177 ctdb->check_public_ifaces_ctx = NULL;
1180 ctdb->check_public_ifaces_ctx = talloc_new(ctdb);
1181 if (ctdb->check_public_ifaces_ctx == NULL) {
1182 ctdb_fatal(ctdb, "failed to allocate context for checking interfaces");
1185 event_add_timed(ctdb->ev, ctdb->check_public_ifaces_ctx,
1186 timeval_current_ofs(30, 0),
1187 ctdb_check_interfaces_event, ctdb);
1189 return 0;
1194 setup the public address lists from a file
1196 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
1198 char **lines;
1199 int nlines;
1200 int i;
1202 lines = file_lines_load(ctdb->public_addresses_file, &nlines, ctdb);
1203 if (lines == NULL) {
1204 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", ctdb->public_addresses_file);
1205 return -1;
1207 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1208 nlines--;
1211 for (i=0;i<nlines;i++) {
1212 unsigned mask;
1213 ctdb_sock_addr addr;
1214 const char *addrstr;
1215 const char *ifaces;
1216 char *tok, *line;
1218 line = lines[i];
1219 while ((*line == ' ') || (*line == '\t')) {
1220 line++;
1222 if (*line == '#') {
1223 continue;
1225 if (strcmp(line, "") == 0) {
1226 continue;
1228 tok = strtok(line, " \t");
1229 addrstr = tok;
1230 tok = strtok(NULL, " \t");
1231 if (tok == NULL) {
1232 if (NULL == ctdb->default_public_interface) {
1233 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
1234 i+1));
1235 talloc_free(lines);
1236 return -1;
1238 ifaces = ctdb->default_public_interface;
1239 } else {
1240 ifaces = tok;
1243 if (!addrstr || !parse_ip_mask(addrstr, ifaces, &addr, &mask)) {
1244 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
1245 talloc_free(lines);
1246 return -1;
1248 if (ctdb_add_public_address(ctdb, &addr, mask, ifaces, check_addresses)) {
1249 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
1250 talloc_free(lines);
1251 return -1;
1256 talloc_free(lines);
1257 return 0;
1260 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1261 const char *iface,
1262 const char *ip)
1264 struct ctdb_vnn *svnn;
1265 struct ctdb_iface *cur = NULL;
1266 bool ok;
1267 int ret;
1269 svnn = talloc_zero(ctdb, struct ctdb_vnn);
1270 CTDB_NO_MEMORY(ctdb, svnn);
1272 svnn->ifaces = talloc_array(svnn, const char *, 2);
1273 CTDB_NO_MEMORY(ctdb, svnn->ifaces);
1274 svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
1275 CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
1276 svnn->ifaces[1] = NULL;
1278 ok = parse_ip(ip, iface, 0, &svnn->public_address);
1279 if (!ok) {
1280 talloc_free(svnn);
1281 return -1;
1284 ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
1285 if (ret != 0) {
1286 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1287 "for single_ip[%s]\n",
1288 svnn->ifaces[0],
1289 ctdb_addr_to_str(&svnn->public_address)));
1290 talloc_free(svnn);
1291 return -1;
1294 /* assume the single public ip interface is initially "good" */
1295 cur = ctdb_find_iface(ctdb, iface);
1296 if (cur == NULL) {
1297 DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
1298 return -1;
1300 cur->link_up = true;
1302 ret = ctdb_vnn_assign_iface(ctdb, svnn);
1303 if (ret != 0) {
1304 talloc_free(svnn);
1305 return -1;
1308 ctdb->single_ip_vnn = svnn;
1309 return 0;
1312 struct ctdb_public_ip_list {
1313 struct ctdb_public_ip_list *next;
1314 uint32_t pnn;
1315 ctdb_sock_addr addr;
1318 /* Given a physical node, return the number of
1319 public addresses that is currently assigned to this node.
1321 static int node_ip_coverage(struct ctdb_context *ctdb,
1322 int32_t pnn,
1323 struct ctdb_public_ip_list *ips)
1325 int num=0;
1327 for (;ips;ips=ips->next) {
1328 if (ips->pnn == pnn) {
1329 num++;
1332 return num;
1336 /* Can the given node host the given IP: is the public IP known to the
1337 * node and is NOIPHOST unset?
1339 static bool can_node_host_ip(struct ctdb_context *ctdb, int32_t pnn,
1340 struct ctdb_ipflags ipflags,
1341 struct ctdb_public_ip_list *ip)
1343 struct ctdb_all_public_ips *public_ips;
1344 int i;
1346 if (ipflags.noiphost) {
1347 return false;
1350 public_ips = ctdb->nodes[pnn]->available_public_ips;
1352 if (public_ips == NULL) {
1353 return false;
1356 for (i=0; i<public_ips->num; i++) {
1357 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
1358 /* yes, this node can serve this public ip */
1359 return true;
1363 return false;
1366 static bool can_node_takeover_ip(struct ctdb_context *ctdb, int32_t pnn,
1367 struct ctdb_ipflags ipflags,
1368 struct ctdb_public_ip_list *ip)
1370 if (ipflags.noiptakeover) {
1371 return false;
1374 return can_node_host_ip(ctdb, pnn, ipflags, ip);
1377 /* search the node lists list for a node to takeover this ip.
1378 pick the node that currently are serving the least number of ips
1379 so that the ips get spread out evenly.
1381 static int find_takeover_node(struct ctdb_context *ctdb,
1382 struct ctdb_ipflags *ipflags,
1383 struct ctdb_public_ip_list *ip,
1384 struct ctdb_public_ip_list *all_ips)
1386 int pnn, min=0, num;
1387 int i, numnodes;
1389 numnodes = talloc_array_length(ipflags);
1390 pnn = -1;
1391 for (i=0; i<numnodes; i++) {
1392 /* verify that this node can serve this ip */
1393 if (!can_node_takeover_ip(ctdb, i, ipflags[i], ip)) {
1394 /* no it couldnt so skip to the next node */
1395 continue;
1398 num = node_ip_coverage(ctdb, i, all_ips);
1399 /* was this the first node we checked ? */
1400 if (pnn == -1) {
1401 pnn = i;
1402 min = num;
1403 } else {
1404 if (num < min) {
1405 pnn = i;
1406 min = num;
1410 if (pnn == -1) {
1411 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
1412 ctdb_addr_to_str(&ip->addr)));
1414 return -1;
1417 ip->pnn = pnn;
1418 return 0;
1421 #define IP_KEYLEN 4
1422 static uint32_t *ip_key(ctdb_sock_addr *ip)
1424 static uint32_t key[IP_KEYLEN];
1426 bzero(key, sizeof(key));
1428 switch (ip->sa.sa_family) {
1429 case AF_INET:
1430 key[3] = htonl(ip->ip.sin_addr.s_addr);
1431 break;
1432 case AF_INET6: {
1433 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1434 key[0] = htonl(s6_a32[0]);
1435 key[1] = htonl(s6_a32[1]);
1436 key[2] = htonl(s6_a32[2]);
1437 key[3] = htonl(s6_a32[3]);
1438 break;
1440 default:
1441 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1442 return key;
1445 return key;
1448 static void *add_ip_callback(void *parm, void *data)
1450 struct ctdb_public_ip_list *this_ip = parm;
1451 struct ctdb_public_ip_list *prev_ip = data;
1453 if (prev_ip == NULL) {
1454 return parm;
1456 if (this_ip->pnn == -1) {
1457 this_ip->pnn = prev_ip->pnn;
1460 return parm;
1463 static int getips_count_callback(void *param, void *data)
1465 struct ctdb_public_ip_list **ip_list = (struct ctdb_public_ip_list **)param;
1466 struct ctdb_public_ip_list *new_ip = (struct ctdb_public_ip_list *)data;
1468 new_ip->next = *ip_list;
1469 *ip_list = new_ip;
1470 return 0;
1473 static struct ctdb_public_ip_list *
1474 create_merged_ip_list(struct ctdb_context *ctdb)
1476 int i, j;
1477 struct ctdb_public_ip_list *ip_list;
1478 struct ctdb_all_public_ips *public_ips;
1480 if (ctdb->ip_tree != NULL) {
1481 talloc_free(ctdb->ip_tree);
1482 ctdb->ip_tree = NULL;
1484 ctdb->ip_tree = trbt_create(ctdb, 0);
1486 for (i=0;i<ctdb->num_nodes;i++) {
1487 public_ips = ctdb->nodes[i]->known_public_ips;
1489 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
1490 continue;
1493 /* there were no public ips for this node */
1494 if (public_ips == NULL) {
1495 continue;
1498 for (j=0;j<public_ips->num;j++) {
1499 struct ctdb_public_ip_list *tmp_ip;
1501 tmp_ip = talloc_zero(ctdb->ip_tree, struct ctdb_public_ip_list);
1502 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
1503 /* Do not use information about IP addresses hosted
1504 * on other nodes, it may not be accurate */
1505 if (public_ips->ips[j].pnn == ctdb->nodes[i]->pnn) {
1506 tmp_ip->pnn = public_ips->ips[j].pnn;
1507 } else {
1508 tmp_ip->pnn = -1;
1510 tmp_ip->addr = public_ips->ips[j].addr;
1511 tmp_ip->next = NULL;
1513 trbt_insertarray32_callback(ctdb->ip_tree,
1514 IP_KEYLEN, ip_key(&public_ips->ips[j].addr),
1515 add_ip_callback,
1516 tmp_ip);
1520 ip_list = NULL;
1521 trbt_traversearray32(ctdb->ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
1523 return ip_list;
1527 * This is the length of the longtest common prefix between the IPs.
1528 * It is calculated by XOR-ing the 2 IPs together and counting the
1529 * number of leading zeroes. The implementation means that all
1530 * addresses end up being 128 bits long.
1532 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1533 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1534 * lots of nodes and IP addresses?
1536 static uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
1538 uint32_t ip1_k[IP_KEYLEN];
1539 uint32_t *t;
1540 int i;
1541 uint32_t x;
1543 uint32_t distance = 0;
1545 memcpy(ip1_k, ip_key(ip1), sizeof(ip1_k));
1546 t = ip_key(ip2);
1547 for (i=0; i<IP_KEYLEN; i++) {
1548 x = ip1_k[i] ^ t[i];
1549 if (x == 0) {
1550 distance += 32;
1551 } else {
1552 /* Count number of leading zeroes.
1553 * FIXME? This could be optimised...
1555 while ((x & (1 << 31)) == 0) {
1556 x <<= 1;
1557 distance += 1;
1562 return distance;
1565 /* Calculate the IP distance for the given IP relative to IPs on the
1566 given node. The ips argument is generally the all_ips variable
1567 used in the main part of the algorithm.
1569 static uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1570 struct ctdb_public_ip_list *ips,
1571 int pnn)
1573 struct ctdb_public_ip_list *t;
1574 uint32_t d;
1576 uint32_t sum = 0;
1578 for (t=ips; t != NULL; t=t->next) {
1579 if (t->pnn != pnn) {
1580 continue;
1583 /* Optimisation: We never calculate the distance
1584 * between an address and itself. This allows us to
1585 * calculate the effect of removing an address from a
1586 * node by simply calculating the distance between
1587 * that address and all of the exitsing addresses.
1588 * Moreover, we assume that we're only ever dealing
1589 * with addresses from all_ips so we can identify an
1590 * address via a pointer rather than doing a more
1591 * expensive address comparison. */
1592 if (&(t->addr) == ip) {
1593 continue;
1596 d = ip_distance(ip, &(t->addr));
1597 sum += d * d; /* Cheaper than pulling in math.h :-) */
1600 return sum;
1603 /* Return the LCP2 imbalance metric for addresses currently assigned
1604 to the given node.
1606 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn)
1608 struct ctdb_public_ip_list *t;
1610 uint32_t imbalance = 0;
1612 for (t=all_ips; t!=NULL; t=t->next) {
1613 if (t->pnn != pnn) {
1614 continue;
1616 /* Pass the rest of the IPs rather than the whole
1617 all_ips input list.
1619 imbalance += ip_distance_2_sum(&(t->addr), t->next, pnn);
1622 return imbalance;
1625 /* Allocate any unassigned IPs just by looping through the IPs and
1626 * finding the best node for each.
1628 static void basic_allocate_unassigned(struct ctdb_context *ctdb,
1629 struct ctdb_ipflags *ipflags,
1630 struct ctdb_public_ip_list *all_ips)
1632 struct ctdb_public_ip_list *tmp_ip;
1634 /* loop over all ip's and find a physical node to cover for
1635 each unassigned ip.
1637 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1638 if (tmp_ip->pnn == -1) {
1639 if (find_takeover_node(ctdb, ipflags, tmp_ip, all_ips)) {
1640 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1641 ctdb_addr_to_str(&tmp_ip->addr)));
1647 /* Basic non-deterministic rebalancing algorithm.
1649 static void basic_failback(struct ctdb_context *ctdb,
1650 struct ctdb_ipflags *ipflags,
1651 struct ctdb_public_ip_list *all_ips,
1652 int num_ips)
1654 int i, numnodes;
1655 int maxnode, maxnum, minnode, minnum, num, retries;
1656 struct ctdb_public_ip_list *tmp_ip;
1658 numnodes = talloc_array_length(ipflags);
1659 retries = 0;
1661 try_again:
1662 maxnum=0;
1663 minnum=0;
1665 /* for each ip address, loop over all nodes that can serve
1666 this ip and make sure that the difference between the node
1667 serving the most and the node serving the least ip's are
1668 not greater than 1.
1670 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1671 if (tmp_ip->pnn == -1) {
1672 continue;
1675 /* Get the highest and lowest number of ips's served by any
1676 valid node which can serve this ip.
1678 maxnode = -1;
1679 minnode = -1;
1680 for (i=0; i<numnodes; i++) {
1681 /* only check nodes that can actually serve this ip */
1682 if (!can_node_takeover_ip(ctdb, i, ipflags[i], tmp_ip)) {
1683 /* no it couldnt so skip to the next node */
1684 continue;
1687 num = node_ip_coverage(ctdb, i, all_ips);
1688 if (maxnode == -1) {
1689 maxnode = i;
1690 maxnum = num;
1691 } else {
1692 if (num > maxnum) {
1693 maxnode = i;
1694 maxnum = num;
1697 if (minnode == -1) {
1698 minnode = i;
1699 minnum = num;
1700 } else {
1701 if (num < minnum) {
1702 minnode = i;
1703 minnum = num;
1707 if (maxnode == -1) {
1708 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
1709 ctdb_addr_to_str(&tmp_ip->addr)));
1711 continue;
1714 /* if the spread between the smallest and largest coverage by
1715 a node is >=2 we steal one of the ips from the node with
1716 most coverage to even things out a bit.
1717 try to do this a limited number of times since we dont
1718 want to spend too much time balancing the ip coverage.
1720 if ( (maxnum > minnum+1)
1721 && (retries < (num_ips + 5)) ){
1722 struct ctdb_public_ip_list *tmp;
1724 /* Reassign one of maxnode's VNNs */
1725 for (tmp=all_ips;tmp;tmp=tmp->next) {
1726 if (tmp->pnn == maxnode) {
1727 (void)find_takeover_node(ctdb, ipflags, tmp, all_ips);
1728 retries++;
1729 goto try_again;;
1736 struct ctdb_rebalancenodes {
1737 struct ctdb_rebalancenodes *next;
1738 uint32_t pnn;
1740 static struct ctdb_rebalancenodes *force_rebalance_list = NULL;
1743 /* set this flag to force the node to be rebalanced even if it just didnt
1744 become healthy again.
1746 void lcp2_forcerebalance(struct ctdb_context *ctdb, uint32_t pnn)
1748 struct ctdb_rebalancenodes *rebalance;
1750 for (rebalance = force_rebalance_list; rebalance; rebalance = rebalance->next) {
1751 if (rebalance->pnn == pnn) {
1752 return;
1756 rebalance = talloc(ctdb, struct ctdb_rebalancenodes);
1757 rebalance->pnn = pnn;
1758 rebalance->next = force_rebalance_list;
1759 force_rebalance_list = rebalance;
1762 /* Do necessary LCP2 initialisation. Bury it in a function here so
1763 * that we can unit test it.
1765 static void lcp2_init(struct ctdb_context *tmp_ctx,
1766 struct ctdb_ipflags *ipflags,
1767 struct ctdb_public_ip_list *all_ips,
1768 uint32_t **lcp2_imbalances,
1769 bool **rebalance_candidates)
1771 int i, numnodes;
1772 struct ctdb_public_ip_list *tmp_ip;
1774 numnodes = talloc_array_length(ipflags);
1776 *rebalance_candidates = talloc_array(tmp_ctx, bool, numnodes);
1777 CTDB_NO_MEMORY_FATAL(tmp_ctx, *rebalance_candidates);
1778 *lcp2_imbalances = talloc_array(tmp_ctx, uint32_t, numnodes);
1779 CTDB_NO_MEMORY_FATAL(tmp_ctx, *lcp2_imbalances);
1781 for (i=0; i<numnodes; i++) {
1782 (*lcp2_imbalances)[i] = lcp2_imbalance(all_ips, i);
1783 /* First step: assume all nodes are candidates */
1784 (*rebalance_candidates)[i] = true;
1787 /* 2nd step: if a node has IPs assigned then it must have been
1788 * healthy before, so we remove it from consideration. This
1789 * is overkill but is all we have because we don't maintain
1790 * state between takeover runs. An alternative would be to
1791 * keep state and invalidate it every time the recovery master
1792 * changes.
1794 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1795 if (tmp_ip->pnn != -1) {
1796 (*rebalance_candidates)[tmp_ip->pnn] = false;
1800 /* 3rd step: if a node is forced to re-balance then
1801 we allow failback onto the node */
1802 while (force_rebalance_list != NULL) {
1803 struct ctdb_rebalancenodes *next = force_rebalance_list->next;
1805 if (force_rebalance_list->pnn <= numnodes) {
1806 (*rebalance_candidates)[force_rebalance_list->pnn] = true;
1809 DEBUG(DEBUG_ERR,("During ipreallocation, forced rebalance of node %d\n", force_rebalance_list->pnn));
1810 talloc_free(force_rebalance_list);
1811 force_rebalance_list = next;
1815 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1816 * the IP/node combination that will cost the least.
1818 static void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1819 struct ctdb_ipflags *ipflags,
1820 struct ctdb_public_ip_list *all_ips,
1821 uint32_t *lcp2_imbalances)
1823 struct ctdb_public_ip_list *tmp_ip;
1824 int dstnode, numnodes;
1826 int minnode;
1827 uint32_t mindsum, dstdsum, dstimbl, minimbl;
1828 struct ctdb_public_ip_list *minip;
1830 bool should_loop = true;
1831 bool have_unassigned = true;
1833 numnodes = talloc_array_length(ipflags);
1835 while (have_unassigned && should_loop) {
1836 should_loop = false;
1838 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1839 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1841 minnode = -1;
1842 mindsum = 0;
1843 minip = NULL;
1845 /* loop over each unassigned ip. */
1846 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1847 if (tmp_ip->pnn != -1) {
1848 continue;
1851 for (dstnode=0; dstnode<numnodes; dstnode++) {
1852 /* only check nodes that can actually takeover this ip */
1853 if (!can_node_takeover_ip(ctdb, dstnode,
1854 ipflags[dstnode],
1855 tmp_ip)) {
1856 /* no it couldnt so skip to the next node */
1857 continue;
1860 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1861 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1862 DEBUG(DEBUG_DEBUG,(" %s -> %d [+%d]\n",
1863 ctdb_addr_to_str(&(tmp_ip->addr)),
1864 dstnode,
1865 dstimbl - lcp2_imbalances[dstnode]));
1868 if ((minnode == -1) || (dstdsum < mindsum)) {
1869 minnode = dstnode;
1870 minimbl = dstimbl;
1871 mindsum = dstdsum;
1872 minip = tmp_ip;
1873 should_loop = true;
1878 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1880 /* If we found one then assign it to the given node. */
1881 if (minnode != -1) {
1882 minip->pnn = minnode;
1883 lcp2_imbalances[minnode] = minimbl;
1884 DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
1885 ctdb_addr_to_str(&(minip->addr)),
1886 minnode,
1887 mindsum));
1890 /* There might be a better way but at least this is clear. */
1891 have_unassigned = false;
1892 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1893 if (tmp_ip->pnn == -1) {
1894 have_unassigned = true;
1899 /* We know if we have an unassigned addresses so we might as
1900 * well optimise.
1902 if (have_unassigned) {
1903 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1904 if (tmp_ip->pnn == -1) {
1905 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1906 ctdb_addr_to_str(&tmp_ip->addr)));
1912 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1913 * to move IPs from, determines the best IP/destination node
1914 * combination to move from the source node.
1916 static bool lcp2_failback_candidate(struct ctdb_context *ctdb,
1917 struct ctdb_ipflags *ipflags,
1918 struct ctdb_public_ip_list *all_ips,
1919 int srcnode,
1920 uint32_t candimbl,
1921 uint32_t *lcp2_imbalances,
1922 bool *rebalance_candidates)
1924 int dstnode, mindstnode, numnodes;
1925 uint32_t srcimbl, srcdsum, dstimbl, dstdsum;
1926 uint32_t minsrcimbl, mindstimbl;
1927 struct ctdb_public_ip_list *minip;
1928 struct ctdb_public_ip_list *tmp_ip;
1930 /* Find an IP and destination node that best reduces imbalance. */
1931 srcimbl = 0;
1932 minip = NULL;
1933 minsrcimbl = 0;
1934 mindstnode = -1;
1935 mindstimbl = 0;
1937 numnodes = talloc_array_length(ipflags);
1939 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1940 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES FROM %d [%d]\n", srcnode, candimbl));
1942 for (tmp_ip=all_ips; tmp_ip; tmp_ip=tmp_ip->next) {
1943 /* Only consider addresses on srcnode. */
1944 if (tmp_ip->pnn != srcnode) {
1945 continue;
1948 /* What is this IP address costing the source node? */
1949 srcdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, srcnode);
1950 srcimbl = candimbl - srcdsum;
1952 /* Consider this IP address would cost each potential
1953 * destination node. Destination nodes are limited to
1954 * those that are newly healthy, since we don't want
1955 * to do gratuitous failover of IPs just to make minor
1956 * balance improvements.
1958 for (dstnode=0; dstnode<numnodes; dstnode++) {
1959 if (!rebalance_candidates[dstnode]) {
1960 continue;
1963 /* only check nodes that can actually takeover this ip */
1964 if (!can_node_takeover_ip(ctdb, dstnode,
1965 ipflags[dstnode], tmp_ip)) {
1966 /* no it couldnt so skip to the next node */
1967 continue;
1970 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1971 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1972 DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
1973 srcnode, srcimbl - lcp2_imbalances[srcnode],
1974 ctdb_addr_to_str(&(tmp_ip->addr)),
1975 dstnode, dstimbl - lcp2_imbalances[dstnode]));
1977 if ((dstimbl < candimbl) && (dstdsum < srcdsum) && \
1978 ((mindstnode == -1) || \
1979 ((srcimbl + dstimbl) < (minsrcimbl + mindstimbl)))) {
1981 minip = tmp_ip;
1982 minsrcimbl = srcimbl;
1983 mindstnode = dstnode;
1984 mindstimbl = dstimbl;
1988 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1990 if (mindstnode != -1) {
1991 /* We found a move that makes things better... */
1992 DEBUG(DEBUG_INFO,("%d [%d] -> %s -> %d [+%d]\n",
1993 srcnode, minsrcimbl - lcp2_imbalances[srcnode],
1994 ctdb_addr_to_str(&(minip->addr)),
1995 mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));
1998 lcp2_imbalances[srcnode] = srcimbl;
1999 lcp2_imbalances[mindstnode] = mindstimbl;
2000 minip->pnn = mindstnode;
2002 return true;
2005 return false;
2009 struct lcp2_imbalance_pnn {
2010 uint32_t imbalance;
2011 int pnn;
2014 static int lcp2_cmp_imbalance_pnn(const void * a, const void * b)
2016 const struct lcp2_imbalance_pnn * lipa = (const struct lcp2_imbalance_pnn *) a;
2017 const struct lcp2_imbalance_pnn * lipb = (const struct lcp2_imbalance_pnn *) b;
2019 if (lipa->imbalance > lipb->imbalance) {
2020 return -1;
2021 } else if (lipa->imbalance == lipb->imbalance) {
2022 return 0;
2023 } else {
2024 return 1;
2028 /* LCP2 algorithm for rebalancing the cluster. This finds the source
2029 * node with the highest LCP2 imbalance, and then determines the best
2030 * IP/destination node combination to move from the source node.
2032 static void lcp2_failback(struct ctdb_context *ctdb,
2033 struct ctdb_ipflags *ipflags,
2034 struct ctdb_public_ip_list *all_ips,
2035 uint32_t *lcp2_imbalances,
2036 bool *rebalance_candidates)
2038 int i, num_rebalance_candidates, numnodes;
2039 struct lcp2_imbalance_pnn * lips;
2040 bool again;
2042 numnodes = talloc_array_length(ipflags);
2044 try_again:
2046 /* It is only worth continuing if we have suitable target
2047 * nodes to transfer IPs to. This check is much cheaper than
2048 * continuing on...
2050 num_rebalance_candidates = 0;
2051 for (i=0; i<numnodes; i++) {
2052 if (rebalance_candidates[i]) {
2053 num_rebalance_candidates++;
2056 if (num_rebalance_candidates == 0) {
2057 return;
2060 /* Put the imbalances and nodes into an array, sort them and
2061 * iterate through candidates. Usually the 1st one will be
2062 * used, so this doesn't cost much...
2064 lips = talloc_array(ctdb, struct lcp2_imbalance_pnn, numnodes);
2065 for (i=0; i<numnodes; i++) {
2066 lips[i].imbalance = lcp2_imbalances[i];
2067 lips[i].pnn = i;
2069 qsort(lips, numnodes, sizeof(struct lcp2_imbalance_pnn),
2070 lcp2_cmp_imbalance_pnn);
2072 again = false;
2073 for (i=0; i<numnodes; i++) {
2074 /* This means that all nodes had 0 or 1 addresses, so
2075 * can't be imbalanced.
2077 if (lips[i].imbalance == 0) {
2078 break;
2081 if (lcp2_failback_candidate(ctdb,
2082 ipflags,
2083 all_ips,
2084 lips[i].pnn,
2085 lips[i].imbalance,
2086 lcp2_imbalances,
2087 rebalance_candidates)) {
2088 again = true;
2089 break;
2093 talloc_free(lips);
2094 if (again) {
2095 goto try_again;
2099 static void unassign_unsuitable_ips(struct ctdb_context *ctdb,
2100 struct ctdb_ipflags *ipflags,
2101 struct ctdb_public_ip_list *all_ips)
2103 struct ctdb_public_ip_list *tmp_ip;
2105 /* verify that the assigned nodes can serve that public ip
2106 and set it to -1 if not
2108 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2109 if (tmp_ip->pnn == -1) {
2110 continue;
2112 if (!can_node_host_ip(ctdb, tmp_ip->pnn,
2113 ipflags[tmp_ip->pnn], tmp_ip) != 0) {
2114 /* this node can not serve this ip. */
2115 DEBUG(DEBUG_DEBUG,("Unassign IP: %s from %d\n",
2116 ctdb_addr_to_str(&(tmp_ip->addr)),
2117 tmp_ip->pnn));
2118 tmp_ip->pnn = -1;
2123 static void ip_alloc_deterministic_ips(struct ctdb_context *ctdb,
2124 struct ctdb_ipflags *ipflags,
2125 struct ctdb_public_ip_list *all_ips)
2127 struct ctdb_public_ip_list *tmp_ip;
2128 int i, numnodes;
2130 numnodes = talloc_array_length(ipflags);
2132 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2133 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2134 * always be allocated the same way for a specific set of
2135 * available/unavailable nodes.
2138 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
2139 tmp_ip->pnn = i % numnodes;
2142 /* IP failback doesn't make sense with deterministic
2143 * IPs, since the modulo step above implicitly fails
2144 * back IPs to their "home" node.
2146 if (1 == ctdb->tunable.no_ip_failback) {
2147 DEBUG(DEBUG_WARNING, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2150 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2152 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2154 /* No failback here! */
2157 static void ip_alloc_nondeterministic_ips(struct ctdb_context *ctdb,
2158 struct ctdb_ipflags *ipflags,
2159 struct ctdb_public_ip_list *all_ips)
2161 /* This should be pushed down into basic_failback. */
2162 struct ctdb_public_ip_list *tmp_ip;
2163 int num_ips = 0;
2164 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2165 num_ips++;
2168 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2170 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2172 /* If we don't want IPs to fail back then don't rebalance IPs. */
2173 if (1 == ctdb->tunable.no_ip_failback) {
2174 return;
2177 /* Now, try to make sure the ip adresses are evenly distributed
2178 across the nodes.
2180 basic_failback(ctdb, ipflags, all_ips, num_ips);
2183 static void ip_alloc_lcp2(struct ctdb_context *ctdb,
2184 struct ctdb_ipflags *ipflags,
2185 struct ctdb_public_ip_list *all_ips)
2187 uint32_t *lcp2_imbalances;
2188 bool *rebalance_candidates;
2190 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2192 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2194 lcp2_init(tmp_ctx, ipflags, all_ips,
2195 &lcp2_imbalances, &rebalance_candidates);
2197 lcp2_allocate_unassigned(ctdb, ipflags, all_ips, lcp2_imbalances);
2199 /* If we don't want IPs to fail back then don't rebalance IPs. */
2200 if (1 == ctdb->tunable.no_ip_failback) {
2201 goto finished;
2204 /* Now, try to make sure the ip adresses are evenly distributed
2205 across the nodes.
2207 lcp2_failback(ctdb, ipflags, all_ips,
2208 lcp2_imbalances, rebalance_candidates);
2210 finished:
2211 talloc_free(tmp_ctx);
2214 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
2216 int i, num_healthy;
2218 /* Count how many completely healthy nodes we have */
2219 num_healthy = 0;
2220 for (i=0;i<nodemap->num;i++) {
2221 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
2222 num_healthy++;
2226 return num_healthy == 0;
2229 /* The calculation part of the IP allocation algorithm. */
2230 static void ctdb_takeover_run_core(struct ctdb_context *ctdb,
2231 struct ctdb_ipflags *ipflags,
2232 struct ctdb_public_ip_list **all_ips_p)
2234 /* since nodes only know about those public addresses that
2235 can be served by that particular node, no single node has
2236 a full list of all public addresses that exist in the cluster.
2237 Walk over all node structures and create a merged list of
2238 all public addresses that exist in the cluster.
2240 keep the tree of ips around as ctdb->ip_tree
2242 *all_ips_p = create_merged_ip_list(ctdb);
2244 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2245 ip_alloc_lcp2(ctdb, ipflags, *all_ips_p);
2246 } else if (1 == ctdb->tunable.deterministic_public_ips) {
2247 ip_alloc_deterministic_ips(ctdb, ipflags, *all_ips_p);
2248 } else {
2249 ip_alloc_nondeterministic_ips(ctdb, ipflags, *all_ips_p);
2252 /* at this point ->pnn is the node which will own each IP
2253 or -1 if there is no node that can cover this ip
2256 return;
2259 struct get_tunable_callback_data {
2260 const char *tunable;
2261 uint32_t *out;
2262 bool fatal;
2265 static void get_tunable_callback(struct ctdb_context *ctdb, uint32_t pnn,
2266 int32_t res, TDB_DATA outdata,
2267 void *callback)
2269 struct get_tunable_callback_data *cd =
2270 (struct get_tunable_callback_data *)callback;
2271 int size;
2273 if (res != 0) {
2274 /* Already handled in fail callback */
2275 return;
2278 if (outdata.dsize != sizeof(uint32_t)) {
2279 DEBUG(DEBUG_ERR,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2280 cd->tunable, pnn, (int)sizeof(uint32_t),
2281 (int)outdata.dsize));
2282 cd->fatal = true;
2283 return;
2286 size = talloc_array_length(cd->out);
2287 if (pnn >= size) {
2288 DEBUG(DEBUG_ERR,("Got %s reply from node %d but nodemap only has %d entries\n",
2289 cd->tunable, pnn, size));
2290 return;
2294 cd->out[pnn] = *(uint32_t *)outdata.dptr;
2297 static void get_tunable_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2298 int32_t res, TDB_DATA outdata,
2299 void *callback)
2301 struct get_tunable_callback_data *cd =
2302 (struct get_tunable_callback_data *)callback;
2304 switch (res) {
2305 case -ETIME:
2306 DEBUG(DEBUG_ERR,
2307 ("Timed out getting tunable \"%s\" from node %d\n",
2308 cd->tunable, pnn));
2309 cd->fatal = true;
2310 break;
2311 case -EINVAL:
2312 case -1:
2313 DEBUG(DEBUG_WARNING,
2314 ("Tunable \"%s\" not implemented on node %d\n",
2315 cd->tunable, pnn));
2316 break;
2317 default:
2318 DEBUG(DEBUG_ERR,
2319 ("Unexpected error getting tunable \"%s\" from node %d\n",
2320 cd->tunable, pnn));
2321 cd->fatal = true;
2325 static uint32_t *get_tunable_from_nodes(struct ctdb_context *ctdb,
2326 TALLOC_CTX *tmp_ctx,
2327 struct ctdb_node_map *nodemap,
2328 const char *tunable,
2329 uint32_t default_value)
2331 TDB_DATA data;
2332 struct ctdb_control_get_tunable *t;
2333 uint32_t *nodes;
2334 uint32_t *tvals;
2335 struct get_tunable_callback_data callback_data;
2336 int i;
2338 tvals = talloc_array(tmp_ctx, uint32_t, nodemap->num);
2339 CTDB_NO_MEMORY_NULL(ctdb, tvals);
2340 for (i=0; i<nodemap->num; i++) {
2341 tvals[i] = default_value;
2344 callback_data.out = tvals;
2345 callback_data.tunable = tunable;
2346 callback_data.fatal = false;
2348 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen(tunable) + 1;
2349 data.dptr = talloc_size(tmp_ctx, data.dsize);
2350 t = (struct ctdb_control_get_tunable *)data.dptr;
2351 t->length = strlen(tunable)+1;
2352 memcpy(t->name, tunable, t->length);
2353 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2354 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_TUNABLE,
2355 nodes, 0, TAKEOVER_TIMEOUT(),
2356 false, data,
2357 get_tunable_callback,
2358 get_tunable_fail_callback,
2359 &callback_data) != 0) {
2360 if (callback_data.fatal) {
2361 talloc_free(tvals);
2362 tvals = NULL;
2365 talloc_free(nodes);
2366 talloc_free(data.dptr);
2368 return tvals;
2371 struct get_runstate_callback_data {
2372 enum ctdb_runstate *out;
2373 bool fatal;
2376 static void get_runstate_callback(struct ctdb_context *ctdb, uint32_t pnn,
2377 int32_t res, TDB_DATA outdata,
2378 void *callback_data)
2380 struct get_runstate_callback_data *cd =
2381 (struct get_runstate_callback_data *)callback_data;
2382 int size;
2384 if (res != 0) {
2385 /* Already handled in fail callback */
2386 return;
2389 if (outdata.dsize != sizeof(uint32_t)) {
2390 DEBUG(DEBUG_ERR,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2391 pnn, (int)sizeof(uint32_t),
2392 (int)outdata.dsize));
2393 cd->fatal = true;
2394 return;
2397 size = talloc_array_length(cd->out);
2398 if (pnn >= size) {
2399 DEBUG(DEBUG_ERR,("Got reply from node %d but nodemap only has %d entries\n",
2400 pnn, size));
2401 return;
2404 cd->out[pnn] = (enum ctdb_runstate)*(uint32_t *)outdata.dptr;
2407 static void get_runstate_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2408 int32_t res, TDB_DATA outdata,
2409 void *callback)
2411 struct get_runstate_callback_data *cd =
2412 (struct get_runstate_callback_data *)callback;
2414 switch (res) {
2415 case -ETIME:
2416 DEBUG(DEBUG_ERR,
2417 ("Timed out getting runstate from node %d\n", pnn));
2418 cd->fatal = true;
2419 break;
2420 default:
2421 DEBUG(DEBUG_WARNING,
2422 ("Error getting runstate from node %d - assuming runstates not supported\n",
2423 pnn));
2427 static enum ctdb_runstate * get_runstate_from_nodes(struct ctdb_context *ctdb,
2428 TALLOC_CTX *tmp_ctx,
2429 struct ctdb_node_map *nodemap,
2430 enum ctdb_runstate default_value)
2432 uint32_t *nodes;
2433 enum ctdb_runstate *rs;
2434 struct get_runstate_callback_data callback_data;
2435 int i;
2437 rs = talloc_array(tmp_ctx, enum ctdb_runstate, nodemap->num);
2438 CTDB_NO_MEMORY_NULL(ctdb, rs);
2439 for (i=0; i<nodemap->num; i++) {
2440 rs[i] = default_value;
2443 callback_data.out = rs;
2444 callback_data.fatal = false;
2446 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2447 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_RUNSTATE,
2448 nodes, 0, TAKEOVER_TIMEOUT(),
2449 true, tdb_null,
2450 get_runstate_callback,
2451 get_runstate_fail_callback,
2452 &callback_data) != 0) {
2453 if (callback_data.fatal) {
2454 free(rs);
2455 rs = NULL;
2458 talloc_free(nodes);
2460 return rs;
2463 /* Set internal flags for IP allocation:
2464 * Clear ip flags
2465 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2466 * Set NOIPHOST ip flag for each INACTIVE node
2467 * if all nodes are disabled:
2468 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2469 * else
2470 * Set NOIPHOST ip flags for disabled nodes
2472 static struct ctdb_ipflags *
2473 set_ipflags_internal(struct ctdb_context *ctdb,
2474 TALLOC_CTX *tmp_ctx,
2475 struct ctdb_node_map *nodemap,
2476 uint32_t *tval_noiptakeover,
2477 uint32_t *tval_noiphostonalldisabled,
2478 enum ctdb_runstate *runstate)
2480 int i;
2481 struct ctdb_ipflags *ipflags;
2483 /* Clear IP flags - implicit due to talloc_zero */
2484 ipflags = talloc_zero_array(tmp_ctx, struct ctdb_ipflags, nodemap->num);
2485 CTDB_NO_MEMORY_NULL(ctdb, ipflags);
2487 for (i=0;i<nodemap->num;i++) {
2488 /* Can not take IPs on node with NoIPTakeover set */
2489 if (tval_noiptakeover[i] != 0) {
2490 ipflags[i].noiptakeover = true;
2493 /* Can not host IPs on node not in RUNNING state */
2494 if (runstate[i] != CTDB_RUNSTATE_RUNNING) {
2495 ipflags[i].noiphost = true;
2496 continue;
2498 /* Can not host IPs on INACTIVE node */
2499 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2500 ipflags[i].noiphost = true;
2504 if (all_nodes_are_disabled(nodemap)) {
2505 /* If all nodes are disabled, can not host IPs on node
2506 * with NoIPHostOnAllDisabled set
2508 for (i=0;i<nodemap->num;i++) {
2509 if (tval_noiphostonalldisabled[i] != 0) {
2510 ipflags[i].noiphost = true;
2513 } else {
2514 /* If some nodes are not disabled, then can not host
2515 * IPs on DISABLED node
2517 for (i=0;i<nodemap->num;i++) {
2518 if (nodemap->nodes[i].flags & NODE_FLAGS_DISABLED) {
2519 ipflags[i].noiphost = true;
2524 return ipflags;
2527 static struct ctdb_ipflags *set_ipflags(struct ctdb_context *ctdb,
2528 TALLOC_CTX *tmp_ctx,
2529 struct ctdb_node_map *nodemap)
2531 uint32_t *tval_noiptakeover;
2532 uint32_t *tval_noiphostonalldisabled;
2533 struct ctdb_ipflags *ipflags;
2534 enum ctdb_runstate *runstate;
2537 tval_noiptakeover = get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2538 "NoIPTakeover", 0);
2539 if (tval_noiptakeover == NULL) {
2540 return NULL;
2543 tval_noiphostonalldisabled =
2544 get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2545 "NoIPHostOnAllDisabled", 0);
2546 if (tval_noiphostonalldisabled == NULL) {
2547 /* Caller frees tmp_ctx */
2548 return NULL;
2551 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2552 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2553 * reasonable behaviour on a mixed cluster during upgrade.
2555 runstate = get_runstate_from_nodes(ctdb, tmp_ctx, nodemap,
2556 CTDB_RUNSTATE_RUNNING);
2557 if (runstate == NULL) {
2558 /* Caller frees tmp_ctx */
2559 return NULL;
2562 ipflags = set_ipflags_internal(ctdb, tmp_ctx, nodemap,
2563 tval_noiptakeover,
2564 tval_noiphostonalldisabled,
2565 runstate);
2567 talloc_free(tval_noiptakeover);
2568 talloc_free(tval_noiphostonalldisabled);
2569 talloc_free(runstate);
2571 return ipflags;
2574 struct iprealloc_callback_data {
2575 bool *retry_nodes;
2576 int retry_count;
2577 client_async_callback fail_callback;
2578 void *fail_callback_data;
2579 struct ctdb_node_map *nodemap;
2582 static void iprealloc_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2583 int32_t res, TDB_DATA outdata,
2584 void *callback)
2586 int numnodes;
2587 struct iprealloc_callback_data *cd =
2588 (struct iprealloc_callback_data *)callback;
2590 switch (res) {
2591 case -ETIME:
2592 /* If the control timed out then that's a real error,
2593 * so call the real fail callback
2595 cd->fail_callback(ctdb, pnn, res, outdata,
2596 cd->fail_callback_data);
2597 break;
2598 default:
2599 /* If not a timeout then either the ipreallocated
2600 * eventscript (or some setup) failed. This might
2601 * have failed because the IPREALLOCATED control isn't
2602 * implemented - right now there is no way of knowing
2603 * because the error codes are all folded down to -1.
2604 * Consider retrying using EVENTSCRIPT control...
2607 numnodes = talloc_array_length(cd->retry_nodes);
2608 if (pnn > numnodes) {
2609 DEBUG(DEBUG_ERR,
2610 ("ipreallocated failure from node %d, but only %d nodes in nodemap\n",
2611 pnn, numnodes));
2612 return;
2615 /* Can't run the "ipreallocated" event on a INACTIVE node */
2616 if (cd->nodemap->nodes[pnn].flags & NODE_FLAGS_INACTIVE) {
2617 DEBUG(DEBUG_ERR,
2618 ("ipreallocated failure from node %d, but node is inactive - not flagging a retry\n",
2619 pnn));
2620 return;
2623 DEBUG(DEBUG_WARNING,
2624 ("ipreallocated failure from node %d, flagging retry\n",
2625 pnn));
2626 cd->retry_nodes[pnn] = true;
2627 cd->retry_count++;
2631 struct takeover_callback_data {
2632 bool *node_failed;
2633 client_async_callback fail_callback;
2634 void *fail_callback_data;
2635 struct ctdb_node_map *nodemap;
2638 static void takeover_run_fail_callback(struct ctdb_context *ctdb,
2639 uint32_t node_pnn, int32_t res,
2640 TDB_DATA outdata, void *callback_data)
2642 struct takeover_callback_data *cd =
2643 talloc_get_type_abort(callback_data,
2644 struct takeover_callback_data);
2645 int i;
2647 for (i = 0; i < cd->nodemap->num; i++) {
2648 if (node_pnn == cd->nodemap->nodes[i].pnn) {
2649 break;
2653 if (i == cd->nodemap->num) {
2654 DEBUG(DEBUG_ERR, (__location__ " invalid PNN %u\n", node_pnn));
2655 return;
2658 if (!cd->node_failed[i]) {
2659 cd->node_failed[i] = true;
2660 cd->fail_callback(ctdb, node_pnn, res, outdata,
2661 cd->fail_callback_data);
2666 make any IP alias changes for public addresses that are necessary
2668 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
2669 client_async_callback fail_callback, void *callback_data)
2671 int i, j, ret;
2672 struct ctdb_public_ip ip;
2673 struct ctdb_public_ipv4 ipv4;
2674 uint32_t *nodes;
2675 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2676 TDB_DATA data;
2677 struct timeval timeout;
2678 struct client_async_data *async_data;
2679 struct ctdb_client_control_state *state;
2680 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2681 uint32_t disable_timeout;
2682 struct ctdb_ipflags *ipflags;
2683 struct takeover_callback_data *takeover_data;
2684 struct iprealloc_callback_data iprealloc_data;
2685 bool *retry_data;
2688 * ip failover is completely disabled, just send out the
2689 * ipreallocated event.
2691 if (ctdb->tunable.disable_ip_failover != 0) {
2692 goto ipreallocated;
2695 ipflags = set_ipflags(ctdb, tmp_ctx, nodemap);
2696 if (ipflags == NULL) {
2697 DEBUG(DEBUG_ERR,("Failed to set IP flags - aborting takeover run\n"));
2698 talloc_free(tmp_ctx);
2699 return -1;
2702 ZERO_STRUCT(ip);
2704 /* Do the IP reassignment calculations */
2705 ctdb_takeover_run_core(ctdb, ipflags, &all_ips);
2707 /* The recovery daemon does regular sanity checks of the IPs.
2708 * However, sometimes it is overzealous and thinks changes are
2709 * required when they're already underway. This stops the
2710 * checks for a while before we start moving IPs.
2712 disable_timeout = ctdb->tunable.takeover_timeout;
2713 data.dptr = (uint8_t*)&disable_timeout;
2714 data.dsize = sizeof(disable_timeout);
2715 if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2716 CTDB_SRVID_DISABLE_IP_CHECK, data) != 0) {
2717 DEBUG(DEBUG_INFO,("Failed to disable ip verification\n"));
2720 /* Now tell all nodes to release any public IPs should not
2721 * host. This will be a NOOP on nodes that don't currently
2722 * hold the given IP.
2724 takeover_data = talloc_zero(tmp_ctx, struct takeover_callback_data);
2725 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data);
2727 takeover_data->node_failed = talloc_zero_array(tmp_ctx,
2728 bool, nodemap->num);
2729 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data->node_failed);
2730 takeover_data->fail_callback = fail_callback;
2731 takeover_data->fail_callback_data = callback_data;
2732 takeover_data->nodemap = nodemap;
2734 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2735 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2737 async_data->fail_callback = takeover_run_fail_callback;
2738 async_data->callback_data = takeover_data;
2740 for (i=0;i<nodemap->num;i++) {
2741 /* don't talk to unconnected nodes, but do talk to banned nodes */
2742 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2743 continue;
2746 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2747 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
2748 /* This node should be serving this
2749 vnn so dont tell it to release the ip
2751 continue;
2753 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2754 ipv4.pnn = tmp_ip->pnn;
2755 ipv4.sin = tmp_ip->addr.ip;
2757 timeout = TAKEOVER_TIMEOUT();
2758 data.dsize = sizeof(ipv4);
2759 data.dptr = (uint8_t *)&ipv4;
2760 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2761 0, CTDB_CONTROL_RELEASE_IPv4, 0,
2762 data, async_data,
2763 &timeout, NULL);
2764 } else {
2765 ip.pnn = tmp_ip->pnn;
2766 ip.addr = tmp_ip->addr;
2768 timeout = TAKEOVER_TIMEOUT();
2769 data.dsize = sizeof(ip);
2770 data.dptr = (uint8_t *)&ip;
2771 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2772 0, CTDB_CONTROL_RELEASE_IP, 0,
2773 data, async_data,
2774 &timeout, NULL);
2777 if (state == NULL) {
2778 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
2779 talloc_free(tmp_ctx);
2780 return -1;
2783 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_RELEASE_IP failed\n"));
2788 talloc_free(tmp_ctx);
2789 return -1;
2791 talloc_free(async_data);
2794 /* tell all nodes to get their own IPs */
2795 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2796 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2798 async_data->fail_callback = fail_callback;
2799 async_data->callback_data = callback_data;
2801 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2802 if (tmp_ip->pnn == -1) {
2803 /* this IP won't be taken over */
2804 continue;
2807 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2808 ipv4.pnn = tmp_ip->pnn;
2809 ipv4.sin = tmp_ip->addr.ip;
2811 timeout = TAKEOVER_TIMEOUT();
2812 data.dsize = sizeof(ipv4);
2813 data.dptr = (uint8_t *)&ipv4;
2814 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2815 0, CTDB_CONTROL_TAKEOVER_IPv4, 0,
2816 data, async_data,
2817 &timeout, NULL);
2818 } else {
2819 ip.pnn = tmp_ip->pnn;
2820 ip.addr = tmp_ip->addr;
2822 timeout = TAKEOVER_TIMEOUT();
2823 data.dsize = sizeof(ip);
2824 data.dptr = (uint8_t *)&ip;
2825 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2826 0, CTDB_CONTROL_TAKEOVER_IP, 0,
2827 data, async_data,
2828 &timeout, NULL);
2830 if (state == NULL) {
2831 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
2832 talloc_free(tmp_ctx);
2833 return -1;
2836 ctdb_client_async_add(async_data, state);
2838 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2839 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2840 talloc_free(tmp_ctx);
2841 return -1;
2844 ipreallocated:
2846 * Tell all nodes to run eventscripts to process the
2847 * "ipreallocated" event. This can do a lot of things,
2848 * including restarting services to reconfigure them if public
2849 * IPs have moved. Once upon a time this event only used to
2850 * update natwg.
2852 retry_data = talloc_zero_array(tmp_ctx, bool, nodemap->num);
2853 CTDB_NO_MEMORY_FATAL(ctdb, retry_data);
2854 iprealloc_data.retry_nodes = retry_data;
2855 iprealloc_data.retry_count = 0;
2856 iprealloc_data.fail_callback = fail_callback;
2857 iprealloc_data.fail_callback_data = callback_data;
2858 iprealloc_data.nodemap = nodemap;
2860 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2861 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_IPREALLOCATED,
2862 nodes, 0, TAKEOVER_TIMEOUT(),
2863 false, tdb_null,
2864 NULL, iprealloc_fail_callback,
2865 &iprealloc_data);
2866 if (ret != 0) {
2867 /* If the control failed then we should retry to any
2868 * nodes flagged by iprealloc_fail_callback using the
2869 * EVENTSCRIPT control. This is a best-effort at
2870 * backward compatiblity when running a mixed cluster
2871 * where some nodes have not yet been upgraded to
2872 * support the IPREALLOCATED control.
2874 DEBUG(DEBUG_WARNING,
2875 ("Retry ipreallocated to some nodes using eventscript control\n"));
2877 nodes = talloc_array(tmp_ctx, uint32_t,
2878 iprealloc_data.retry_count);
2879 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2881 j = 0;
2882 for (i=0; i<nodemap->num; i++) {
2883 if (iprealloc_data.retry_nodes[i]) {
2884 nodes[j] = i;
2885 j++;
2889 data.dptr = discard_const("ipreallocated");
2890 data.dsize = strlen((char *)data.dptr) + 1;
2891 ret = ctdb_client_async_control(ctdb,
2892 CTDB_CONTROL_RUN_EVENTSCRIPTS,
2893 nodes, 0, TAKEOVER_TIMEOUT(),
2894 false, data,
2895 NULL, fail_callback,
2896 callback_data);
2897 if (ret != 0) {
2898 DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
2902 talloc_free(tmp_ctx);
2903 return ret;
2908 destroy a ctdb_client_ip structure
2910 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
2912 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
2913 ctdb_addr_to_str(&ip->addr),
2914 ntohs(ip->addr.ip.sin_port),
2915 ip->client_id));
2917 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
2918 return 0;
2922 called by a client to inform us of a TCP connection that it is managing
2923 that should tickled with an ACK when IP takeover is done
2924 we handle both the old ipv4 style of packets as well as the new ipv4/6
2925 pdus.
2927 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
2928 TDB_DATA indata)
2930 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
2931 struct ctdb_control_tcp *old_addr = NULL;
2932 struct ctdb_control_tcp_addr new_addr;
2933 struct ctdb_control_tcp_addr *tcp_sock = NULL;
2934 struct ctdb_tcp_list *tcp;
2935 struct ctdb_tcp_connection t;
2936 int ret;
2937 TDB_DATA data;
2938 struct ctdb_client_ip *ip;
2939 struct ctdb_vnn *vnn;
2940 ctdb_sock_addr addr;
2942 switch (indata.dsize) {
2943 case sizeof(struct ctdb_control_tcp):
2944 old_addr = (struct ctdb_control_tcp *)indata.dptr;
2945 ZERO_STRUCT(new_addr);
2946 tcp_sock = &new_addr;
2947 tcp_sock->src.ip = old_addr->src;
2948 tcp_sock->dest.ip = old_addr->dest;
2949 break;
2950 case sizeof(struct ctdb_control_tcp_addr):
2951 tcp_sock = (struct ctdb_control_tcp_addr *)indata.dptr;
2952 break;
2953 default:
2954 DEBUG(DEBUG_ERR,(__location__ " Invalid data structure passed "
2955 "to ctdb_control_tcp_client. size was %d but "
2956 "only allowed sizes are %lu and %lu\n",
2957 (int)indata.dsize,
2958 (long unsigned)sizeof(struct ctdb_control_tcp),
2959 (long unsigned)sizeof(struct ctdb_control_tcp_addr)));
2960 return -1;
2963 addr = tcp_sock->src;
2964 ctdb_canonicalize_ip(&addr, &tcp_sock->src);
2965 addr = tcp_sock->dest;
2966 ctdb_canonicalize_ip(&addr, &tcp_sock->dest);
2968 ZERO_STRUCT(addr);
2969 memcpy(&addr, &tcp_sock->dest, sizeof(addr));
2970 vnn = find_public_ip_vnn(ctdb, &addr);
2971 if (vnn == NULL) {
2972 switch (addr.sa.sa_family) {
2973 case AF_INET:
2974 if (ntohl(addr.ip.sin_addr.s_addr) != INADDR_LOOPBACK) {
2975 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public address.\n",
2976 ctdb_addr_to_str(&addr)));
2978 break;
2979 case AF_INET6:
2980 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public ipv6 address.\n",
2981 ctdb_addr_to_str(&addr)));
2982 break;
2983 default:
2984 DEBUG(DEBUG_ERR,(__location__ " Unknown family type %d\n", addr.sa.sa_family));
2987 return 0;
2990 if (vnn->pnn != ctdb->pnn) {
2991 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2992 ctdb_addr_to_str(&addr),
2993 client_id, client->pid));
2994 /* failing this call will tell smbd to die */
2995 return -1;
2998 ip = talloc(client, struct ctdb_client_ip);
2999 CTDB_NO_MEMORY(ctdb, ip);
3001 ip->ctdb = ctdb;
3002 ip->addr = addr;
3003 ip->client_id = client_id;
3004 talloc_set_destructor(ip, ctdb_client_ip_destructor);
3005 DLIST_ADD(ctdb->client_ip_list, ip);
3007 tcp = talloc(client, struct ctdb_tcp_list);
3008 CTDB_NO_MEMORY(ctdb, tcp);
3010 tcp->connection.src_addr = tcp_sock->src;
3011 tcp->connection.dst_addr = tcp_sock->dest;
3013 DLIST_ADD(client->tcp_list, tcp);
3015 t.src_addr = tcp_sock->src;
3016 t.dst_addr = tcp_sock->dest;
3018 data.dptr = (uint8_t *)&t;
3019 data.dsize = sizeof(t);
3021 switch (addr.sa.sa_family) {
3022 case AF_INET:
3023 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
3024 (unsigned)ntohs(tcp_sock->dest.ip.sin_port),
3025 ctdb_addr_to_str(&tcp_sock->src),
3026 (unsigned)ntohs(tcp_sock->src.ip.sin_port), client_id, client->pid));
3027 break;
3028 case AF_INET6:
3029 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
3030 (unsigned)ntohs(tcp_sock->dest.ip6.sin6_port),
3031 ctdb_addr_to_str(&tcp_sock->src),
3032 (unsigned)ntohs(tcp_sock->src.ip6.sin6_port), client_id, client->pid));
3033 break;
3034 default:
3035 DEBUG(DEBUG_ERR,(__location__ " Unknown family %d\n", addr.sa.sa_family));
3039 /* tell all nodes about this tcp connection */
3040 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
3041 CTDB_CONTROL_TCP_ADD,
3042 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
3043 if (ret != 0) {
3044 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
3045 return -1;
3048 return 0;
3052 find a tcp address on a list
3054 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
3055 struct ctdb_tcp_connection *tcp)
3057 int i;
3059 if (array == NULL) {
3060 return NULL;
3063 for (i=0;i<array->num;i++) {
3064 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
3065 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
3066 return &array->connections[i];
3069 return NULL;
3075 called by a daemon to inform us of a TCP connection that one of its
3076 clients managing that should tickled with an ACK when IP takeover is
3077 done
3079 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
3081 struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
3082 struct ctdb_tcp_array *tcparray;
3083 struct ctdb_tcp_connection tcp;
3084 struct ctdb_vnn *vnn;
3086 vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
3087 if (vnn == NULL) {
3088 DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
3089 ctdb_addr_to_str(&p->dst_addr)));
3091 return -1;
3095 tcparray = vnn->tcp_array;
3097 /* If this is the first tickle */
3098 if (tcparray == NULL) {
3099 tcparray = talloc_size(ctdb->nodes,
3100 offsetof(struct ctdb_tcp_array, connections) +
3101 sizeof(struct ctdb_tcp_connection) * 1);
3102 CTDB_NO_MEMORY(ctdb, tcparray);
3103 vnn->tcp_array = tcparray;
3105 tcparray->num = 0;
3106 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
3107 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3109 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3110 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3111 tcparray->num++;
3113 if (tcp_update_needed) {
3114 vnn->tcp_update_needed = true;
3116 return 0;
3120 /* Do we already have this tickle ?*/
3121 tcp.src_addr = p->src_addr;
3122 tcp.dst_addr = p->dst_addr;
3123 if (ctdb_tcp_find(vnn->tcp_array, &tcp) != NULL) {
3124 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
3125 ctdb_addr_to_str(&tcp.dst_addr),
3126 ntohs(tcp.dst_addr.ip.sin_port),
3127 vnn->pnn));
3128 return 0;
3131 /* A new tickle, we must add it to the array */
3132 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
3133 struct ctdb_tcp_connection,
3134 tcparray->num+1);
3135 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3137 vnn->tcp_array = tcparray;
3138 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3139 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3140 tcparray->num++;
3142 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
3143 ctdb_addr_to_str(&tcp.dst_addr),
3144 ntohs(tcp.dst_addr.ip.sin_port),
3145 vnn->pnn));
3147 if (tcp_update_needed) {
3148 vnn->tcp_update_needed = true;
3151 return 0;
3156 called by a daemon to inform us of a TCP connection that one of its
3157 clients managing that should tickled with an ACK when IP takeover is
3158 done
3160 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
3162 struct ctdb_tcp_connection *tcpp;
3163 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
3165 if (vnn == NULL) {
3166 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
3167 ctdb_addr_to_str(&conn->dst_addr)));
3168 return;
3171 /* if the array is empty we cant remove it
3172 and we dont need to do anything
3174 if (vnn->tcp_array == NULL) {
3175 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3176 ctdb_addr_to_str(&conn->dst_addr),
3177 ntohs(conn->dst_addr.ip.sin_port)));
3178 return;
3182 /* See if we know this connection
3183 if we dont know this connection then we dont need to do anything
3185 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
3186 if (tcpp == NULL) {
3187 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
3188 ctdb_addr_to_str(&conn->dst_addr),
3189 ntohs(conn->dst_addr.ip.sin_port)));
3190 return;
3194 /* We need to remove this entry from the array.
3195 Instead of allocating a new array and copying data to it
3196 we cheat and just copy the last entry in the existing array
3197 to the entry that is to be removed and just shring the
3198 ->num field
3200 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
3201 vnn->tcp_array->num--;
3203 /* If we deleted the last entry we also need to remove the entire array
3205 if (vnn->tcp_array->num == 0) {
3206 talloc_free(vnn->tcp_array);
3207 vnn->tcp_array = NULL;
3210 vnn->tcp_update_needed = true;
3212 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
3213 ctdb_addr_to_str(&conn->src_addr),
3214 ntohs(conn->src_addr.ip.sin_port)));
3219 called by a daemon to inform us of a TCP connection that one of its
3220 clients used are no longer needed in the tickle database
3222 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
3224 struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
3226 ctdb_remove_tcp_connection(ctdb, conn);
3228 return 0;
3233 called when a daemon restarts - send all tickes for all public addresses
3234 we are serving immediately to the new node.
3236 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn)
3238 /*XXX here we should send all tickes we are serving to the new node */
3239 return 0;
3244 called when a client structure goes away - hook to remove
3245 elements from the tcp_list in all daemons
3247 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
3249 while (client->tcp_list) {
3250 struct ctdb_tcp_list *tcp = client->tcp_list;
3251 DLIST_REMOVE(client->tcp_list, tcp);
3252 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
3258 release all IPs on shutdown
3260 void ctdb_release_all_ips(struct ctdb_context *ctdb)
3262 struct ctdb_vnn *vnn;
3263 int count = 0;
3265 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3266 if (!ctdb_sys_have_ip(&vnn->public_address)) {
3267 ctdb_vnn_unassign_iface(ctdb, vnn);
3268 continue;
3270 if (!vnn->iface) {
3271 continue;
3274 DEBUG(DEBUG_INFO,("Release of IP %s/%u on interface %s node:-1\n",
3275 ctdb_addr_to_str(&vnn->public_address),
3276 vnn->public_netmask_bits,
3277 ctdb_vnn_iface_string(vnn)));
3279 ctdb_event_script_args(ctdb, CTDB_EVENT_RELEASE_IP, "%s %s %u",
3280 ctdb_vnn_iface_string(vnn),
3281 ctdb_addr_to_str(&vnn->public_address),
3282 vnn->public_netmask_bits);
3283 release_kill_clients(ctdb, &vnn->public_address);
3284 ctdb_vnn_unassign_iface(ctdb, vnn);
3285 count++;
3288 DEBUG(DEBUG_NOTICE,(__location__ " Released %d public IPs\n", count));
3293 get list of public IPs
3295 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
3296 struct ctdb_req_control *c, TDB_DATA *outdata)
3298 int i, num, len;
3299 struct ctdb_all_public_ips *ips;
3300 struct ctdb_vnn *vnn;
3301 bool only_available = false;
3303 if (c->flags & CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE) {
3304 only_available = true;
3307 /* count how many public ip structures we have */
3308 num = 0;
3309 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3310 num++;
3313 len = offsetof(struct ctdb_all_public_ips, ips) +
3314 num*sizeof(struct ctdb_public_ip);
3315 ips = talloc_zero_size(outdata, len);
3316 CTDB_NO_MEMORY(ctdb, ips);
3318 i = 0;
3319 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3320 if (only_available && !ctdb_vnn_available(ctdb, vnn)) {
3321 continue;
3323 ips->ips[i].pnn = vnn->pnn;
3324 ips->ips[i].addr = vnn->public_address;
3325 i++;
3327 ips->num = i;
3328 len = offsetof(struct ctdb_all_public_ips, ips) +
3329 i*sizeof(struct ctdb_public_ip);
3331 outdata->dsize = len;
3332 outdata->dptr = (uint8_t *)ips;
3334 return 0;
3339 get list of public IPs, old ipv4 style. only returns ipv4 addresses
3341 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb,
3342 struct ctdb_req_control *c, TDB_DATA *outdata)
3344 int i, num, len;
3345 struct ctdb_all_public_ipsv4 *ips;
3346 struct ctdb_vnn *vnn;
3348 /* count how many public ip structures we have */
3349 num = 0;
3350 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3351 if (vnn->public_address.sa.sa_family != AF_INET) {
3352 continue;
3354 num++;
3357 len = offsetof(struct ctdb_all_public_ipsv4, ips) +
3358 num*sizeof(struct ctdb_public_ipv4);
3359 ips = talloc_zero_size(outdata, len);
3360 CTDB_NO_MEMORY(ctdb, ips);
3362 outdata->dsize = len;
3363 outdata->dptr = (uint8_t *)ips;
3365 ips->num = num;
3366 i = 0;
3367 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3368 if (vnn->public_address.sa.sa_family != AF_INET) {
3369 continue;
3371 ips->ips[i].pnn = vnn->pnn;
3372 ips->ips[i].sin = vnn->public_address.ip;
3373 i++;
3376 return 0;
3379 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
3380 struct ctdb_req_control *c,
3381 TDB_DATA indata,
3382 TDB_DATA *outdata)
3384 int i, num, len;
3385 ctdb_sock_addr *addr;
3386 struct ctdb_control_public_ip_info *info;
3387 struct ctdb_vnn *vnn;
3389 addr = (ctdb_sock_addr *)indata.dptr;
3391 vnn = find_public_ip_vnn(ctdb, addr);
3392 if (vnn == NULL) {
3393 /* if it is not a public ip it could be our 'single ip' */
3394 if (ctdb->single_ip_vnn) {
3395 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
3396 vnn = ctdb->single_ip_vnn;
3400 if (vnn == NULL) {
3401 DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
3402 "'%s'not a public address\n",
3403 ctdb_addr_to_str(addr)));
3404 return -1;
3407 /* count how many public ip structures we have */
3408 num = 0;
3409 for (;vnn->ifaces[num];) {
3410 num++;
3413 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3414 num*sizeof(struct ctdb_control_iface_info);
3415 info = talloc_zero_size(outdata, len);
3416 CTDB_NO_MEMORY(ctdb, info);
3418 info->ip.addr = vnn->public_address;
3419 info->ip.pnn = vnn->pnn;
3420 info->active_idx = 0xFFFFFFFF;
3422 for (i=0; vnn->ifaces[i]; i++) {
3423 struct ctdb_iface *cur;
3425 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
3426 if (cur == NULL) {
3427 DEBUG(DEBUG_CRIT, (__location__ " internal error iface[%s] unknown\n",
3428 vnn->ifaces[i]));
3429 return -1;
3431 if (vnn->iface == cur) {
3432 info->active_idx = i;
3434 strcpy(info->ifaces[i].name, cur->name);
3435 info->ifaces[i].link_state = cur->link_up;
3436 info->ifaces[i].references = cur->references;
3438 info->num = i;
3439 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3440 i*sizeof(struct ctdb_control_iface_info);
3442 outdata->dsize = len;
3443 outdata->dptr = (uint8_t *)info;
3445 return 0;
3448 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
3449 struct ctdb_req_control *c,
3450 TDB_DATA *outdata)
3452 int i, num, len;
3453 struct ctdb_control_get_ifaces *ifaces;
3454 struct ctdb_iface *cur;
3456 /* count how many public ip structures we have */
3457 num = 0;
3458 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3459 num++;
3462 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3463 num*sizeof(struct ctdb_control_iface_info);
3464 ifaces = talloc_zero_size(outdata, len);
3465 CTDB_NO_MEMORY(ctdb, ifaces);
3467 i = 0;
3468 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3469 strcpy(ifaces->ifaces[i].name, cur->name);
3470 ifaces->ifaces[i].link_state = cur->link_up;
3471 ifaces->ifaces[i].references = cur->references;
3472 i++;
3474 ifaces->num = i;
3475 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3476 i*sizeof(struct ctdb_control_iface_info);
3478 outdata->dsize = len;
3479 outdata->dptr = (uint8_t *)ifaces;
3481 return 0;
3484 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
3485 struct ctdb_req_control *c,
3486 TDB_DATA indata)
3488 struct ctdb_control_iface_info *info;
3489 struct ctdb_iface *iface;
3490 bool link_up = false;
3492 info = (struct ctdb_control_iface_info *)indata.dptr;
3494 if (info->name[CTDB_IFACE_SIZE] != '\0') {
3495 int len = strnlen(info->name, CTDB_IFACE_SIZE);
3496 DEBUG(DEBUG_ERR, (__location__ " name[%*.*s] not terminated\n",
3497 len, len, info->name));
3498 return -1;
3501 switch (info->link_state) {
3502 case 0:
3503 link_up = false;
3504 break;
3505 case 1:
3506 link_up = true;
3507 break;
3508 default:
3509 DEBUG(DEBUG_ERR, (__location__ " link_state[%u] invalid\n",
3510 (unsigned int)info->link_state));
3511 return -1;
3514 if (info->references != 0) {
3515 DEBUG(DEBUG_ERR, (__location__ " references[%u] should be 0\n",
3516 (unsigned int)info->references));
3517 return -1;
3520 iface = ctdb_find_iface(ctdb, info->name);
3521 if (iface == NULL) {
3522 return -1;
3525 if (link_up == iface->link_up) {
3526 return 0;
3529 DEBUG(iface->link_up?DEBUG_ERR:DEBUG_NOTICE,
3530 ("iface[%s] has changed it's link status %s => %s\n",
3531 iface->name,
3532 iface->link_up?"up":"down",
3533 link_up?"up":"down"));
3535 iface->link_up = link_up;
3536 return 0;
3541 structure containing the listening socket and the list of tcp connections
3542 that the ctdb daemon is to kill
3544 struct ctdb_kill_tcp {
3545 struct ctdb_vnn *vnn;
3546 struct ctdb_context *ctdb;
3547 int capture_fd;
3548 struct fd_event *fde;
3549 trbt_tree_t *connections;
3550 void *private_data;
3554 a tcp connection that is to be killed
3556 struct ctdb_killtcp_con {
3557 ctdb_sock_addr src_addr;
3558 ctdb_sock_addr dst_addr;
3559 int count;
3560 struct ctdb_kill_tcp *killtcp;
3563 /* this function is used to create a key to represent this socketpair
3564 in the killtcp tree.
3565 this key is used to insert and lookup matching socketpairs that are
3566 to be tickled and RST
3568 #define KILLTCP_KEYLEN 10
3569 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
3571 static uint32_t key[KILLTCP_KEYLEN];
3573 bzero(key, sizeof(key));
3575 if (src->sa.sa_family != dst->sa.sa_family) {
3576 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
3577 return key;
3580 switch (src->sa.sa_family) {
3581 case AF_INET:
3582 key[0] = dst->ip.sin_addr.s_addr;
3583 key[1] = src->ip.sin_addr.s_addr;
3584 key[2] = dst->ip.sin_port;
3585 key[3] = src->ip.sin_port;
3586 break;
3587 case AF_INET6: {
3588 uint32_t *dst6_addr32 =
3589 (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
3590 uint32_t *src6_addr32 =
3591 (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
3592 key[0] = dst6_addr32[3];
3593 key[1] = src6_addr32[3];
3594 key[2] = dst6_addr32[2];
3595 key[3] = src6_addr32[2];
3596 key[4] = dst6_addr32[1];
3597 key[5] = src6_addr32[1];
3598 key[6] = dst6_addr32[0];
3599 key[7] = src6_addr32[0];
3600 key[8] = dst->ip6.sin6_port;
3601 key[9] = src->ip6.sin6_port;
3602 break;
3604 default:
3605 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
3606 return key;
3609 return key;
3613 called when we get a read event on the raw socket
3615 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
3616 uint16_t flags, void *private_data)
3618 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3619 struct ctdb_killtcp_con *con;
3620 ctdb_sock_addr src, dst;
3621 uint32_t ack_seq, seq;
3623 if (!(flags & EVENT_FD_READ)) {
3624 return;
3627 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
3628 killtcp->private_data,
3629 &src, &dst,
3630 &ack_seq, &seq) != 0) {
3631 /* probably a non-tcp ACK packet */
3632 return;
3635 /* check if we have this guy in our list of connections
3636 to kill
3638 con = trbt_lookuparray32(killtcp->connections,
3639 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
3640 if (con == NULL) {
3641 /* no this was some other packet we can just ignore */
3642 return;
3645 /* This one has been tickled !
3646 now reset him and remove him from the list.
3648 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3649 ntohs(con->dst_addr.ip.sin_port),
3650 ctdb_addr_to_str(&con->src_addr),
3651 ntohs(con->src_addr.ip.sin_port)));
3653 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
3654 talloc_free(con);
3658 /* when traversing the list of all tcp connections to send tickle acks to
3659 (so that we can capture the ack coming back and kill the connection
3660 by a RST)
3661 this callback is called for each connection we are currently trying to kill
3663 static int tickle_connection_traverse(void *param, void *data)
3665 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
3667 /* have tried too many times, just give up */
3668 if (con->count >= 5) {
3669 /* can't delete in traverse: reparent to delete_cons */
3670 talloc_steal(param, con);
3671 return 0;
3674 /* othervise, try tickling it again */
3675 con->count++;
3676 ctdb_sys_send_tcp(
3677 (ctdb_sock_addr *)&con->dst_addr,
3678 (ctdb_sock_addr *)&con->src_addr,
3679 0, 0, 0);
3680 return 0;
3685 called every second until all sentenced connections have been reset
3687 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
3688 struct timeval t, void *private_data)
3690 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3691 void *delete_cons = talloc_new(NULL);
3693 /* loop over all connections sending tickle ACKs */
3694 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
3696 /* now we've finished traverse, it's safe to do deletion. */
3697 talloc_free(delete_cons);
3699 /* If there are no more connections to kill we can remove the
3700 entire killtcp structure
3702 if ( (killtcp->connections == NULL) ||
3703 (killtcp->connections->root == NULL) ) {
3704 talloc_free(killtcp);
3705 return;
3708 /* try tickling them again in a seconds time
3710 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3711 ctdb_tickle_sentenced_connections, killtcp);
3715 destroy the killtcp structure
3717 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
3719 struct ctdb_vnn *tmpvnn;
3721 /* verify that this vnn is still active */
3722 for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
3723 if (tmpvnn == killtcp->vnn) {
3724 break;
3728 if (tmpvnn == NULL) {
3729 return 0;
3732 if (killtcp->vnn->killtcp != killtcp) {
3733 return 0;
3736 killtcp->vnn->killtcp = NULL;
3738 return 0;
3742 /* nothing fancy here, just unconditionally replace any existing
3743 connection structure with the new one.
3745 dont even free the old one if it did exist, that one is talloc_stolen
3746 by the same node in the tree anyway and will be deleted when the new data
3747 is deleted
3749 static void *add_killtcp_callback(void *parm, void *data)
3751 return parm;
3755 add a tcp socket to the list of connections we want to RST
3757 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
3758 ctdb_sock_addr *s,
3759 ctdb_sock_addr *d)
3761 ctdb_sock_addr src, dst;
3762 struct ctdb_kill_tcp *killtcp;
3763 struct ctdb_killtcp_con *con;
3764 struct ctdb_vnn *vnn;
3766 ctdb_canonicalize_ip(s, &src);
3767 ctdb_canonicalize_ip(d, &dst);
3769 vnn = find_public_ip_vnn(ctdb, &dst);
3770 if (vnn == NULL) {
3771 vnn = find_public_ip_vnn(ctdb, &src);
3773 if (vnn == NULL) {
3774 /* if it is not a public ip it could be our 'single ip' */
3775 if (ctdb->single_ip_vnn) {
3776 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
3777 vnn = ctdb->single_ip_vnn;
3781 if (vnn == NULL) {
3782 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
3783 return -1;
3786 killtcp = vnn->killtcp;
3788 /* If this is the first connection to kill we must allocate
3789 a new structure
3791 if (killtcp == NULL) {
3792 killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
3793 CTDB_NO_MEMORY(ctdb, killtcp);
3795 killtcp->vnn = vnn;
3796 killtcp->ctdb = ctdb;
3797 killtcp->capture_fd = -1;
3798 killtcp->connections = trbt_create(killtcp, 0);
3800 vnn->killtcp = killtcp;
3801 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
3806 /* create a structure that describes this connection we want to
3807 RST and store it in killtcp->connections
3809 con = talloc(killtcp, struct ctdb_killtcp_con);
3810 CTDB_NO_MEMORY(ctdb, con);
3811 con->src_addr = src;
3812 con->dst_addr = dst;
3813 con->count = 0;
3814 con->killtcp = killtcp;
3817 trbt_insertarray32_callback(killtcp->connections,
3818 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
3819 add_killtcp_callback, con);
3822 If we dont have a socket to listen on yet we must create it
3824 if (killtcp->capture_fd == -1) {
3825 const char *iface = ctdb_vnn_iface_string(vnn);
3826 killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
3827 if (killtcp->capture_fd == -1) {
3828 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
3829 "socket on iface '%s' for killtcp (%s)\n",
3830 iface, strerror(errno)));
3831 goto failed;
3836 if (killtcp->fde == NULL) {
3837 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
3838 EVENT_FD_READ,
3839 capture_tcp_handler, killtcp);
3840 tevent_fd_set_auto_close(killtcp->fde);
3842 /* We also need to set up some events to tickle all these connections
3843 until they are all reset
3845 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3846 ctdb_tickle_sentenced_connections, killtcp);
3849 /* tickle him once now */
3850 ctdb_sys_send_tcp(
3851 &con->dst_addr,
3852 &con->src_addr,
3853 0, 0, 0);
3855 return 0;
3857 failed:
3858 talloc_free(vnn->killtcp);
3859 vnn->killtcp = NULL;
3860 return -1;
3864 kill a TCP connection.
3866 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
3868 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
3870 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
3874 called by a daemon to inform us of the entire list of TCP tickles for
3875 a particular public address.
3876 this control should only be sent by the node that is currently serving
3877 that public address.
3879 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
3881 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
3882 struct ctdb_tcp_array *tcparray;
3883 struct ctdb_vnn *vnn;
3885 /* We must at least have tickles.num or else we cant verify the size
3886 of the received data blob
3888 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3889 tickles.connections)) {
3890 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3891 return -1;
3894 /* verify that the size of data matches what we expect */
3895 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3896 tickles.connections)
3897 + sizeof(struct ctdb_tcp_connection)
3898 * list->tickles.num) {
3899 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3900 return -1;
3903 vnn = find_public_ip_vnn(ctdb, &list->addr);
3904 if (vnn == NULL) {
3905 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
3906 ctdb_addr_to_str(&list->addr)));
3908 return 1;
3911 /* remove any old ticklelist we might have */
3912 talloc_free(vnn->tcp_array);
3913 vnn->tcp_array = NULL;
3915 tcparray = talloc(ctdb->nodes, struct ctdb_tcp_array);
3916 CTDB_NO_MEMORY(ctdb, tcparray);
3918 tcparray->num = list->tickles.num;
3920 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
3921 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3923 memcpy(tcparray->connections, &list->tickles.connections[0],
3924 sizeof(struct ctdb_tcp_connection)*tcparray->num);
3926 /* We now have a new fresh tickle list array for this vnn */
3927 vnn->tcp_array = talloc_steal(vnn, tcparray);
3929 return 0;
3933 called to return the full list of tickles for the puclic address associated
3934 with the provided vnn
3936 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
3938 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
3939 struct ctdb_control_tcp_tickle_list *list;
3940 struct ctdb_tcp_array *tcparray;
3941 int num;
3942 struct ctdb_vnn *vnn;
3944 vnn = find_public_ip_vnn(ctdb, addr);
3945 if (vnn == NULL) {
3946 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
3947 ctdb_addr_to_str(addr)));
3949 return 1;
3952 tcparray = vnn->tcp_array;
3953 if (tcparray) {
3954 num = tcparray->num;
3955 } else {
3956 num = 0;
3959 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3960 tickles.connections)
3961 + sizeof(struct ctdb_tcp_connection) * num;
3963 outdata->dptr = talloc_size(outdata, outdata->dsize);
3964 CTDB_NO_MEMORY(ctdb, outdata->dptr);
3965 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
3967 list->addr = *addr;
3968 list->tickles.num = num;
3969 if (num) {
3970 memcpy(&list->tickles.connections[0], tcparray->connections,
3971 sizeof(struct ctdb_tcp_connection) * num);
3974 return 0;
3979 set the list of all tcp tickles for a public address
3981 static int ctdb_ctrl_set_tcp_tickles(struct ctdb_context *ctdb,
3982 struct timeval timeout, uint32_t destnode,
3983 ctdb_sock_addr *addr,
3984 struct ctdb_tcp_array *tcparray)
3986 int ret, num;
3987 TDB_DATA data;
3988 struct ctdb_control_tcp_tickle_list *list;
3990 if (tcparray) {
3991 num = tcparray->num;
3992 } else {
3993 num = 0;
3996 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3997 tickles.connections) +
3998 sizeof(struct ctdb_tcp_connection) * num;
3999 data.dptr = talloc_size(ctdb, data.dsize);
4000 CTDB_NO_MEMORY(ctdb, data.dptr);
4002 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
4003 list->addr = *addr;
4004 list->tickles.num = num;
4005 if (tcparray) {
4006 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
4009 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
4010 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
4011 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
4012 if (ret != 0) {
4013 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
4014 return -1;
4017 talloc_free(data.dptr);
4019 return ret;
4024 perform tickle updates if required
4026 static void ctdb_update_tcp_tickles(struct event_context *ev,
4027 struct timed_event *te,
4028 struct timeval t, void *private_data)
4030 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
4031 int ret;
4032 struct ctdb_vnn *vnn;
4034 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4035 /* we only send out updates for public addresses that
4036 we have taken over
4038 if (ctdb->pnn != vnn->pnn) {
4039 continue;
4041 /* We only send out the updates if we need to */
4042 if (!vnn->tcp_update_needed) {
4043 continue;
4045 ret = ctdb_ctrl_set_tcp_tickles(ctdb,
4046 TAKEOVER_TIMEOUT(),
4047 CTDB_BROADCAST_CONNECTED,
4048 &vnn->public_address,
4049 vnn->tcp_array);
4050 if (ret != 0) {
4051 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
4052 ctdb_addr_to_str(&vnn->public_address)));
4056 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4057 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4058 ctdb_update_tcp_tickles, ctdb);
4063 start periodic update of tcp tickles
4065 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
4067 ctdb->tickle_update_context = talloc_new(ctdb);
4069 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4070 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4071 ctdb_update_tcp_tickles, ctdb);
4077 struct control_gratious_arp {
4078 struct ctdb_context *ctdb;
4079 ctdb_sock_addr addr;
4080 const char *iface;
4081 int count;
4085 send a control_gratuitous arp
4087 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
4088 struct timeval t, void *private_data)
4090 int ret;
4091 struct control_gratious_arp *arp = talloc_get_type(private_data,
4092 struct control_gratious_arp);
4094 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
4095 if (ret != 0) {
4096 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp on iface '%s' failed (%s)\n",
4097 arp->iface, strerror(errno)));
4101 arp->count++;
4102 if (arp->count == CTDB_ARP_REPEAT) {
4103 talloc_free(arp);
4104 return;
4107 event_add_timed(arp->ctdb->ev, arp,
4108 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
4109 send_gratious_arp, arp);
4114 send a gratious arp
4116 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
4118 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
4119 struct control_gratious_arp *arp;
4121 /* verify the size of indata */
4122 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
4123 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
4124 (unsigned)indata.dsize,
4125 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
4126 return -1;
4128 if (indata.dsize !=
4129 ( offsetof(struct ctdb_control_gratious_arp, iface)
4130 + gratious_arp->len ) ){
4132 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4133 "but should be %u bytes\n",
4134 (unsigned)indata.dsize,
4135 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
4136 return -1;
4140 arp = talloc(ctdb, struct control_gratious_arp);
4141 CTDB_NO_MEMORY(ctdb, arp);
4143 arp->ctdb = ctdb;
4144 arp->addr = gratious_arp->addr;
4145 arp->iface = talloc_strdup(arp, gratious_arp->iface);
4146 CTDB_NO_MEMORY(ctdb, arp->iface);
4147 arp->count = 0;
4149 event_add_timed(arp->ctdb->ev, arp,
4150 timeval_zero(), send_gratious_arp, arp);
4152 return 0;
4155 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
4157 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4158 int ret;
4160 /* verify the size of indata */
4161 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4162 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4163 return -1;
4165 if (indata.dsize !=
4166 ( offsetof(struct ctdb_control_ip_iface, iface)
4167 + pub->len ) ){
4169 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4170 "but should be %u bytes\n",
4171 (unsigned)indata.dsize,
4172 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4173 return -1;
4176 DEBUG(DEBUG_NOTICE,("Add IP %s\n", ctdb_addr_to_str(&pub->addr)));
4178 ret = ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0], true);
4180 if (ret != 0) {
4181 DEBUG(DEBUG_ERR,(__location__ " Failed to add public address\n"));
4182 return -1;
4185 return 0;
4189 called when releaseip event finishes for del_public_address
4191 static void delete_ip_callback(struct ctdb_context *ctdb, int status,
4192 void *private_data)
4194 talloc_free(private_data);
4197 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
4199 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4200 struct ctdb_vnn *vnn;
4201 int ret;
4203 /* verify the size of indata */
4204 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4205 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4206 return -1;
4208 if (indata.dsize !=
4209 ( offsetof(struct ctdb_control_ip_iface, iface)
4210 + pub->len ) ){
4212 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4213 "but should be %u bytes\n",
4214 (unsigned)indata.dsize,
4215 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4216 return -1;
4219 DEBUG(DEBUG_NOTICE,("Delete IP %s\n", ctdb_addr_to_str(&pub->addr)));
4221 /* walk over all public addresses until we find a match */
4222 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4223 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
4224 TALLOC_CTX *mem_ctx = talloc_new(ctdb);
4226 DLIST_REMOVE(ctdb->vnn, vnn);
4227 talloc_steal(mem_ctx, vnn);
4228 ctdb_remove_orphaned_ifaces(ctdb, vnn, mem_ctx);
4229 if (vnn->pnn != ctdb->pnn) {
4230 if (vnn->iface != NULL) {
4231 ctdb_vnn_unassign_iface(ctdb, vnn);
4233 talloc_free(mem_ctx);
4234 return 0;
4236 vnn->pnn = -1;
4238 ret = ctdb_event_script_callback(ctdb,
4239 mem_ctx, delete_ip_callback, mem_ctx,
4240 false,
4241 CTDB_EVENT_RELEASE_IP,
4242 "%s %s %u",
4243 ctdb_vnn_iface_string(vnn),
4244 ctdb_addr_to_str(&vnn->public_address),
4245 vnn->public_netmask_bits);
4246 if (vnn->iface != NULL) {
4247 ctdb_vnn_unassign_iface(ctdb, vnn);
4249 if (ret != 0) {
4250 return -1;
4252 return 0;
4256 return -1;
4260 struct ipreallocated_callback_state {
4261 struct ctdb_req_control *c;
4264 static void ctdb_ipreallocated_callback(struct ctdb_context *ctdb,
4265 int status, void *p)
4267 struct ipreallocated_callback_state *state =
4268 talloc_get_type(p, struct ipreallocated_callback_state);
4270 if (status != 0) {
4271 DEBUG(DEBUG_ERR,
4272 (" \"ipreallocated\" event script failed (status %d)\n",
4273 status));
4274 if (status == -ETIME) {
4275 ctdb_ban_self(ctdb);
4279 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
4280 talloc_free(state);
4283 /* A control to run the ipreallocated event */
4284 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb,
4285 struct ctdb_req_control *c,
4286 bool *async_reply)
4288 int ret;
4289 struct ipreallocated_callback_state *state;
4291 state = talloc(ctdb, struct ipreallocated_callback_state);
4292 CTDB_NO_MEMORY(ctdb, state);
4294 DEBUG(DEBUG_INFO,(__location__ " Running \"ipreallocated\" event\n"));
4296 ret = ctdb_event_script_callback(ctdb, state,
4297 ctdb_ipreallocated_callback, state,
4298 false, CTDB_EVENT_IPREALLOCATED,
4299 "%s", "");
4301 if (ret != 0) {
4302 DEBUG(DEBUG_ERR,("Failed to run \"ipreallocated\" event \n"));
4303 talloc_free(state);
4304 return -1;
4307 /* tell the control that we will be reply asynchronously */
4308 state->c = talloc_steal(state, c);
4309 *async_reply = true;
4311 return 0;
4315 /* This function is called from the recovery daemon to verify that a remote
4316 node has the expected ip allocation.
4317 This is verified against ctdb->ip_tree
4319 int verify_remote_ip_allocation(struct ctdb_context *ctdb,
4320 struct ctdb_all_public_ips *ips,
4321 uint32_t pnn)
4323 struct ctdb_public_ip_list *tmp_ip;
4324 int i;
4326 if (ctdb->ip_tree == NULL) {
4327 /* dont know the expected allocation yet, assume remote node
4328 is correct. */
4329 return 0;
4332 if (ips == NULL) {
4333 return 0;
4336 for (i=0; i<ips->num; i++) {
4337 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ips->ips[i].addr));
4338 if (tmp_ip == NULL) {
4339 DEBUG(DEBUG_ERR,("Node %u has new or unknown public IP %s\n", pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
4340 return -1;
4343 if (tmp_ip->pnn == -1 || ips->ips[i].pnn == -1) {
4344 continue;
4347 if (tmp_ip->pnn != ips->ips[i].pnn) {
4348 DEBUG(DEBUG_ERR,
4349 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4350 pnn,
4351 ctdb_addr_to_str(&ips->ips[i].addr),
4352 ips->ips[i].pnn, tmp_ip->pnn));
4353 return -1;
4357 return 0;
4360 int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
4362 struct ctdb_public_ip_list *tmp_ip;
4364 if (ctdb->ip_tree == NULL) {
4365 DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4366 return -1;
4369 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
4370 if (tmp_ip == NULL) {
4371 DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
4372 return -1;
4375 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));
4376 tmp_ip->pnn = ip->pnn;
4378 return 0;
4382 struct ctdb_reloadips_handle {
4383 struct ctdb_context *ctdb;
4384 struct ctdb_req_control *c;
4385 int status;
4386 int fd[2];
4387 pid_t child;
4388 struct fd_event *fde;
4391 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle *h)
4393 if (h == h->ctdb->reload_ips) {
4394 h->ctdb->reload_ips = NULL;
4396 if (h->c != NULL) {
4397 ctdb_request_control_reply(h->ctdb, h->c, NULL, h->status, NULL);
4398 h->c = NULL;
4400 ctdb_kill(h->ctdb, h->child, SIGKILL);
4401 return 0;
4404 static void ctdb_reloadips_timeout_event(struct event_context *ev,
4405 struct timed_event *te,
4406 struct timeval t, void *private_data)
4408 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4410 talloc_free(h);
4413 static void ctdb_reloadips_child_handler(struct event_context *ev, struct fd_event *fde,
4414 uint16_t flags, void *private_data)
4416 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4418 char res;
4419 int ret;
4421 ret = read(h->fd[0], &res, 1);
4422 if (ret < 1 || res != 0) {
4423 DEBUG(DEBUG_ERR, (__location__ " Reloadips child process returned error\n"));
4424 res = 1;
4426 h->status = res;
4428 talloc_free(h);
4431 static int ctdb_reloadips_child(struct ctdb_context *ctdb)
4433 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4434 struct ctdb_all_public_ips *ips;
4435 struct ctdb_vnn *vnn;
4436 int i, ret;
4438 CTDB_NO_MEMORY(ctdb, mem_ctx);
4440 /* read the ip allocation from the local node */
4441 ret = ctdb_ctrl_get_public_ips(ctdb, TAKEOVER_TIMEOUT(), CTDB_CURRENT_NODE, mem_ctx, &ips);
4442 if (ret != 0) {
4443 DEBUG(DEBUG_ERR, ("Unable to get public ips from local node\n"));
4444 talloc_free(mem_ctx);
4445 return -1;
4448 /* re-read the public ips file */
4449 ctdb->vnn = NULL;
4450 if (ctdb_set_public_addresses(ctdb, false) != 0) {
4451 DEBUG(DEBUG_ERR,("Failed to re-read public addresses file\n"));
4452 talloc_free(mem_ctx);
4453 return -1;
4457 /* check the previous list of ips and scan for ips that have been
4458 dropped.
4460 for (i = 0; i < ips->num; i++) {
4461 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4462 if (ctdb_same_ip(&vnn->public_address, &ips->ips[i].addr)) {
4463 break;
4467 /* we need to delete this ip, no longer available on this node */
4468 if (vnn == NULL) {
4469 struct ctdb_control_ip_iface pub;
4471 DEBUG(DEBUG_NOTICE,("RELOADIPS: IP%s is no longer available on this node. Deleting it.\n", ctdb_addr_to_str(&ips->ips[i].addr)));
4472 pub.addr = ips->ips[i].addr;
4473 pub.mask = 0;
4474 pub.len = 0;
4476 ret = ctdb_ctrl_del_public_ip(ctdb, TAKEOVER_TIMEOUT(), CTDB_CURRENT_NODE, &pub);
4477 if (ret != 0) {
4478 talloc_free(mem_ctx);
4479 DEBUG(DEBUG_ERR, ("RELOADIPS: Unable to del public ip:%s from local node\n", ctdb_addr_to_str(&ips->ips[i].addr)));
4480 return -1;
4486 /* loop over all new ones and check the ones we need to add */
4487 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4488 for (i = 0; i < ips->num; i++) {
4489 if (ctdb_same_ip(&vnn->public_address, &ips->ips[i].addr)) {
4490 break;
4493 if (i == ips->num) {
4494 struct ctdb_control_ip_iface *pub;
4495 const char *ifaces = NULL;
4496 int iface = 0;
4498 DEBUG(DEBUG_NOTICE,("RELOADIPS: New ip:%s found, adding it.\n", ctdb_addr_to_str(&vnn->public_address)));
4500 pub = talloc_zero(mem_ctx, struct ctdb_control_ip_iface);
4501 pub->addr = vnn->public_address;
4502 pub->mask = vnn->public_netmask_bits;
4504 ifaces = vnn->ifaces[0];
4505 iface = 1;
4506 while (vnn->ifaces[iface] != NULL) {
4507 ifaces = talloc_asprintf(vnn, "%s,%s", ifaces, vnn->ifaces[iface]);
4508 iface++;
4510 pub->len = strlen(ifaces)+1;
4511 pub = talloc_realloc_size(mem_ctx, pub,
4512 offsetof(struct ctdb_control_ip_iface, iface) + pub->len);
4513 if (pub == NULL) {
4514 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate memory\n"));
4515 talloc_free(mem_ctx);
4516 return -1;
4518 memcpy(&pub->iface[0], ifaces, pub->len);
4520 ret = ctdb_ctrl_add_public_ip(ctdb, TAKEOVER_TIMEOUT(),
4521 CTDB_CURRENT_NODE, pub);
4522 if (ret != 0) {
4523 DEBUG(DEBUG_ERR, ("RELOADIPS: Unable to add public ip:%s to local node\n", ctdb_addr_to_str(&vnn->public_address)));
4524 talloc_free(mem_ctx);
4525 return -1;
4530 talloc_free(mem_ctx);
4531 return 0;
4534 /* This control is sent to force the node to re-read the public addresses file
4535 and drop any addresses we should nnot longer host, and add new addresses
4536 that we are now able to host
4538 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
4540 struct ctdb_reloadips_handle *h;
4541 pid_t parent = getpid();
4543 if (ctdb->reload_ips != NULL) {
4544 talloc_free(ctdb->reload_ips);
4545 ctdb->reload_ips = NULL;
4548 h = talloc(ctdb, struct ctdb_reloadips_handle);
4549 CTDB_NO_MEMORY(ctdb, h);
4550 h->ctdb = ctdb;
4551 h->c = NULL;
4552 h->status = -1;
4554 if (pipe(h->fd) == -1) {
4555 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
4556 talloc_free(h);
4557 return -1;
4560 h->child = ctdb_fork(ctdb);
4561 if (h->child == (pid_t)-1) {
4562 DEBUG(DEBUG_ERR, ("Failed to fork a child for reloadips\n"));
4563 close(h->fd[0]);
4564 close(h->fd[1]);
4565 talloc_free(h);
4566 return -1;
4569 /* child process */
4570 if (h->child == 0) {
4571 signed char res = 0;
4573 close(h->fd[0]);
4574 debug_extra = talloc_asprintf(NULL, "reloadips:");
4576 ctdb_set_process_name("ctdb_reloadips");
4577 if (switch_from_server_to_client(ctdb, "reloadips-child") != 0) {
4578 DEBUG(DEBUG_CRIT,("ERROR: Failed to switch reloadips child into client mode\n"));
4579 res = -1;
4580 } else {
4581 res = ctdb_reloadips_child(ctdb);
4582 if (res != 0) {
4583 DEBUG(DEBUG_ERR,("Failed to reload ips on local node\n"));
4587 write(h->fd[1], &res, 1);
4588 /* make sure we die when our parent dies */
4589 while (ctdb_kill(ctdb, parent, 0) == 0 || errno != ESRCH) {
4590 sleep(5);
4592 _exit(0);
4595 h->c = talloc_steal(h, c);
4597 close(h->fd[1]);
4598 set_close_on_exec(h->fd[0]);
4600 talloc_set_destructor(h, ctdb_reloadips_destructor);
4603 h->fde = event_add_fd(ctdb->ev, h, h->fd[0],
4604 EVENT_FD_READ, ctdb_reloadips_child_handler,
4605 (void *)h);
4606 tevent_fd_set_auto_close(h->fde);
4608 event_add_timed(ctdb->ev, h,
4609 timeval_current_ofs(120, 0),
4610 ctdb_reloadips_timeout_event, h);
4612 /* we reply later */
4613 *async_reply = true;
4614 return 0;