s3:smbd: pass smbXsrv_connection to smbd_[un]lock_socket()
[Samba.git] / ctdb / server / ctdb_takeover.c
blob8449288d36bf67a216090aaa566a385cb0c23478
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)
123 struct ctdb_iface *i, *next;
125 /* For each interface, check if there's an IP using it. */
126 for (i = ctdb->ifaces; i != NULL; i = next) {
127 struct ctdb_vnn *tv;
128 bool found;
129 next = i->next;
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 talloc_free(i);
161 static struct ctdb_iface *ctdb_find_iface(struct ctdb_context *ctdb,
162 const char *iface)
164 struct ctdb_iface *i;
166 for (i=ctdb->ifaces;i;i=i->next) {
167 if (strcmp(i->name, iface) == 0) {
168 return i;
172 return NULL;
175 static struct ctdb_iface *ctdb_vnn_best_iface(struct ctdb_context *ctdb,
176 struct ctdb_vnn *vnn)
178 int i;
179 struct ctdb_iface *cur = NULL;
180 struct ctdb_iface *best = NULL;
182 for (i=0; vnn->ifaces[i]; i++) {
184 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
185 if (cur == NULL) {
186 continue;
189 if (!cur->link_up) {
190 continue;
193 if (best == NULL) {
194 best = cur;
195 continue;
198 if (cur->references < best->references) {
199 best = cur;
200 continue;
204 return best;
207 static int32_t ctdb_vnn_assign_iface(struct ctdb_context *ctdb,
208 struct ctdb_vnn *vnn)
210 struct ctdb_iface *best = NULL;
212 if (vnn->iface) {
213 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
214 "still assigned to iface '%s'\n",
215 ctdb_addr_to_str(&vnn->public_address),
216 ctdb_vnn_iface_string(vnn)));
217 return 0;
220 best = ctdb_vnn_best_iface(ctdb, vnn);
221 if (best == NULL) {
222 DEBUG(DEBUG_ERR, (__location__ " public address '%s' "
223 "cannot assign to iface any iface\n",
224 ctdb_addr_to_str(&vnn->public_address)));
225 return -1;
228 vnn->iface = best;
229 best->references++;
230 vnn->pnn = ctdb->pnn;
232 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
233 "now assigned to iface '%s' refs[%d]\n",
234 ctdb_addr_to_str(&vnn->public_address),
235 ctdb_vnn_iface_string(vnn),
236 best->references));
237 return 0;
240 static void ctdb_vnn_unassign_iface(struct ctdb_context *ctdb,
241 struct ctdb_vnn *vnn)
243 DEBUG(DEBUG_INFO, (__location__ " public address '%s' "
244 "now unassigned (old iface '%s' refs[%d])\n",
245 ctdb_addr_to_str(&vnn->public_address),
246 ctdb_vnn_iface_string(vnn),
247 vnn->iface?vnn->iface->references:0));
248 if (vnn->iface) {
249 vnn->iface->references--;
251 vnn->iface = NULL;
252 if (vnn->pnn == ctdb->pnn) {
253 vnn->pnn = -1;
257 static bool ctdb_vnn_available(struct ctdb_context *ctdb,
258 struct ctdb_vnn *vnn)
260 int i;
262 if (vnn->delete_pending) {
263 return false;
266 if (vnn->iface && vnn->iface->link_up) {
267 return true;
270 for (i=0; vnn->ifaces[i]; i++) {
271 struct ctdb_iface *cur;
273 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
274 if (cur == NULL) {
275 continue;
278 if (cur->link_up) {
279 return true;
283 return false;
286 struct ctdb_takeover_arp {
287 struct ctdb_context *ctdb;
288 uint32_t count;
289 ctdb_sock_addr addr;
290 struct ctdb_tcp_array *tcparray;
291 struct ctdb_vnn *vnn;
296 lists of tcp endpoints
298 struct ctdb_tcp_list {
299 struct ctdb_tcp_list *prev, *next;
300 struct ctdb_tcp_connection connection;
304 list of clients to kill on IP release
306 struct ctdb_client_ip {
307 struct ctdb_client_ip *prev, *next;
308 struct ctdb_context *ctdb;
309 ctdb_sock_addr addr;
310 uint32_t client_id;
315 send a gratuitous arp
317 static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te,
318 struct timeval t, void *private_data)
320 struct ctdb_takeover_arp *arp = talloc_get_type(private_data,
321 struct ctdb_takeover_arp);
322 int i, ret;
323 struct ctdb_tcp_array *tcparray;
324 const char *iface = ctdb_vnn_iface_string(arp->vnn);
326 ret = ctdb_sys_send_arp(&arp->addr, iface);
327 if (ret != 0) {
328 DEBUG(DEBUG_CRIT,(__location__ " sending of arp failed on iface '%s' (%s)\n",
329 iface, strerror(errno)));
332 tcparray = arp->tcparray;
333 if (tcparray) {
334 for (i=0;i<tcparray->num;i++) {
335 struct ctdb_tcp_connection *tcon;
337 tcon = &tcparray->connections[i];
338 DEBUG(DEBUG_INFO,("sending tcp tickle ack for %u->%s:%u\n",
339 (unsigned)ntohs(tcon->dst_addr.ip.sin_port),
340 ctdb_addr_to_str(&tcon->src_addr),
341 (unsigned)ntohs(tcon->src_addr.ip.sin_port)));
342 ret = ctdb_sys_send_tcp(
343 &tcon->src_addr,
344 &tcon->dst_addr,
345 0, 0, 0);
346 if (ret != 0) {
347 DEBUG(DEBUG_CRIT,(__location__ " Failed to send tcp tickle ack for %s\n",
348 ctdb_addr_to_str(&tcon->src_addr)));
353 arp->count++;
355 if (arp->count == CTDB_ARP_REPEAT) {
356 talloc_free(arp);
357 return;
360 event_add_timed(arp->ctdb->ev, arp->vnn->takeover_ctx,
361 timeval_current_ofs(CTDB_ARP_INTERVAL, 100000),
362 ctdb_control_send_arp, arp);
365 static int32_t ctdb_announce_vnn_iface(struct ctdb_context *ctdb,
366 struct ctdb_vnn *vnn)
368 struct ctdb_takeover_arp *arp;
369 struct ctdb_tcp_array *tcparray;
371 if (!vnn->takeover_ctx) {
372 vnn->takeover_ctx = talloc_new(vnn);
373 if (!vnn->takeover_ctx) {
374 return -1;
378 arp = talloc_zero(vnn->takeover_ctx, struct ctdb_takeover_arp);
379 if (!arp) {
380 return -1;
383 arp->ctdb = ctdb;
384 arp->addr = vnn->public_address;
385 arp->vnn = vnn;
387 tcparray = vnn->tcp_array;
388 if (tcparray) {
389 /* add all of the known tcp connections for this IP to the
390 list of tcp connections to send tickle acks for */
391 arp->tcparray = talloc_steal(arp, tcparray);
393 vnn->tcp_array = NULL;
394 vnn->tcp_update_needed = true;
397 event_add_timed(arp->ctdb->ev, vnn->takeover_ctx,
398 timeval_zero(), ctdb_control_send_arp, arp);
400 return 0;
403 struct takeover_callback_state {
404 struct ctdb_req_control *c;
405 ctdb_sock_addr *addr;
406 struct ctdb_vnn *vnn;
409 struct ctdb_do_takeip_state {
410 struct ctdb_req_control *c;
411 struct ctdb_vnn *vnn;
415 called when takeip event finishes
417 static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
418 void *private_data)
420 struct ctdb_do_takeip_state *state =
421 talloc_get_type(private_data, struct ctdb_do_takeip_state);
422 int32_t ret;
423 TDB_DATA data;
425 if (status != 0) {
426 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
428 if (status == -ETIME) {
429 ctdb_ban_self(ctdb);
431 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
432 ctdb_addr_to_str(&state->vnn->public_address),
433 ctdb_vnn_iface_string(state->vnn)));
434 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
436 node->flags |= NODE_FLAGS_UNHEALTHY;
437 talloc_free(state);
438 return;
441 if (ctdb->do_checkpublicip) {
443 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
444 if (ret != 0) {
445 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
446 talloc_free(state);
447 return;
452 data.dptr = (uint8_t *)ctdb_addr_to_str(&state->vnn->public_address);
453 data.dsize = strlen((char *)data.dptr) + 1;
454 DEBUG(DEBUG_INFO,(__location__ " sending TAKE_IP for '%s'\n", data.dptr));
456 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_TAKE_IP, data);
459 /* the control succeeded */
460 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
461 talloc_free(state);
462 return;
465 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state *state)
467 state->vnn->update_in_flight = false;
468 return 0;
472 take over an ip address
474 static int32_t ctdb_do_takeip(struct ctdb_context *ctdb,
475 struct ctdb_req_control *c,
476 struct ctdb_vnn *vnn)
478 int ret;
479 struct ctdb_do_takeip_state *state;
481 if (vnn->update_in_flight) {
482 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u rejected "
483 "update for this IP already in flight\n",
484 ctdb_addr_to_str(&vnn->public_address),
485 vnn->public_netmask_bits));
486 return -1;
489 ret = ctdb_vnn_assign_iface(ctdb, vnn);
490 if (ret != 0) {
491 DEBUG(DEBUG_ERR,("Takeover of IP %s/%u failed to "
492 "assign a usable interface\n",
493 ctdb_addr_to_str(&vnn->public_address),
494 vnn->public_netmask_bits));
495 return -1;
498 state = talloc(vnn, struct ctdb_do_takeip_state);
499 CTDB_NO_MEMORY(ctdb, state);
501 state->c = talloc_steal(ctdb, c);
502 state->vnn = vnn;
504 vnn->update_in_flight = true;
505 talloc_set_destructor(state, ctdb_takeip_destructor);
507 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u on interface %s\n",
508 ctdb_addr_to_str(&vnn->public_address),
509 vnn->public_netmask_bits,
510 ctdb_vnn_iface_string(vnn)));
512 ret = ctdb_event_script_callback(ctdb,
513 state,
514 ctdb_do_takeip_callback,
515 state,
516 CTDB_EVENT_TAKE_IP,
517 "%s %s %u",
518 ctdb_vnn_iface_string(vnn),
519 ctdb_addr_to_str(&vnn->public_address),
520 vnn->public_netmask_bits);
522 if (ret != 0) {
523 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
524 ctdb_addr_to_str(&vnn->public_address),
525 ctdb_vnn_iface_string(vnn)));
526 talloc_free(state);
527 return -1;
530 return 0;
533 struct ctdb_do_updateip_state {
534 struct ctdb_req_control *c;
535 struct ctdb_iface *old;
536 struct ctdb_vnn *vnn;
540 called when updateip event finishes
542 static void ctdb_do_updateip_callback(struct ctdb_context *ctdb, int status,
543 void *private_data)
545 struct ctdb_do_updateip_state *state =
546 talloc_get_type(private_data, struct ctdb_do_updateip_state);
547 int32_t ret;
549 if (status != 0) {
550 if (status == -ETIME) {
551 ctdb_ban_self(ctdb);
553 DEBUG(DEBUG_ERR,(__location__ " Failed to move IP %s from interface %s to %s\n",
554 ctdb_addr_to_str(&state->vnn->public_address),
555 state->old->name,
556 ctdb_vnn_iface_string(state->vnn)));
559 * All we can do is reset the old interface
560 * and let the next run fix it
562 ctdb_vnn_unassign_iface(ctdb, state->vnn);
563 state->vnn->iface = state->old;
564 state->vnn->iface->references++;
566 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
567 talloc_free(state);
568 return;
571 if (ctdb->do_checkpublicip) {
573 ret = ctdb_announce_vnn_iface(ctdb, state->vnn);
574 if (ret != 0) {
575 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
576 talloc_free(state);
577 return;
582 /* the control succeeded */
583 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
584 talloc_free(state);
585 return;
588 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state *state)
590 state->vnn->update_in_flight = false;
591 return 0;
595 update (move) an ip address
597 static int32_t ctdb_do_updateip(struct ctdb_context *ctdb,
598 struct ctdb_req_control *c,
599 struct ctdb_vnn *vnn)
601 int ret;
602 struct ctdb_do_updateip_state *state;
603 struct ctdb_iface *old = vnn->iface;
604 const char *new_name;
606 if (vnn->update_in_flight) {
607 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u rejected "
608 "update for this IP already in flight\n",
609 ctdb_addr_to_str(&vnn->public_address),
610 vnn->public_netmask_bits));
611 return -1;
614 ctdb_vnn_unassign_iface(ctdb, vnn);
615 ret = ctdb_vnn_assign_iface(ctdb, vnn);
616 if (ret != 0) {
617 DEBUG(DEBUG_ERR,("update of IP %s/%u failed to "
618 "assin a usable interface (old iface '%s')\n",
619 ctdb_addr_to_str(&vnn->public_address),
620 vnn->public_netmask_bits,
621 old->name));
622 return -1;
625 new_name = ctdb_vnn_iface_string(vnn);
626 if (old->name != NULL && new_name != NULL && !strcmp(old->name, new_name)) {
627 /* A benign update from one interface onto itself.
628 * no need to run the eventscripts in this case, just return
629 * success.
631 ctdb_request_control_reply(ctdb, c, NULL, 0, NULL);
632 return 0;
635 state = talloc(vnn, struct ctdb_do_updateip_state);
636 CTDB_NO_MEMORY(ctdb, state);
638 state->c = talloc_steal(ctdb, c);
639 state->old = old;
640 state->vnn = vnn;
642 vnn->update_in_flight = true;
643 talloc_set_destructor(state, ctdb_updateip_destructor);
645 DEBUG(DEBUG_NOTICE,("Update of IP %s/%u from "
646 "interface %s to %s\n",
647 ctdb_addr_to_str(&vnn->public_address),
648 vnn->public_netmask_bits,
649 old->name,
650 new_name));
652 ret = ctdb_event_script_callback(ctdb,
653 state,
654 ctdb_do_updateip_callback,
655 state,
656 CTDB_EVENT_UPDATE_IP,
657 "%s %s %s %u",
658 state->old->name,
659 new_name,
660 ctdb_addr_to_str(&vnn->public_address),
661 vnn->public_netmask_bits);
662 if (ret != 0) {
663 DEBUG(DEBUG_ERR,(__location__ " Failed update IP %s from interface %s to %s\n",
664 ctdb_addr_to_str(&vnn->public_address),
665 old->name, new_name));
666 talloc_free(state);
667 return -1;
670 return 0;
674 Find the vnn of the node that has a public ip address
675 returns -1 if the address is not known as a public address
677 static struct ctdb_vnn *find_public_ip_vnn(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
679 struct ctdb_vnn *vnn;
681 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
682 if (ctdb_same_ip(&vnn->public_address, addr)) {
683 return vnn;
687 return NULL;
691 take over an ip address
693 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
694 struct ctdb_req_control *c,
695 TDB_DATA indata,
696 bool *async_reply)
698 int ret;
699 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
700 struct ctdb_vnn *vnn;
701 bool have_ip = false;
702 bool do_updateip = false;
703 bool do_takeip = false;
704 struct ctdb_iface *best_iface = NULL;
706 if (pip->pnn != ctdb->pnn) {
707 DEBUG(DEBUG_ERR,(__location__" takeoverip called for an ip '%s' "
708 "with pnn %d, but we're node %d\n",
709 ctdb_addr_to_str(&pip->addr),
710 pip->pnn, ctdb->pnn));
711 return -1;
714 /* update out vnn list */
715 vnn = find_public_ip_vnn(ctdb, &pip->addr);
716 if (vnn == NULL) {
717 DEBUG(DEBUG_INFO,("takeoverip called for an ip '%s' that is not a public address\n",
718 ctdb_addr_to_str(&pip->addr)));
719 return 0;
722 if (ctdb->do_checkpublicip) {
723 have_ip = ctdb_sys_have_ip(&pip->addr);
725 best_iface = ctdb_vnn_best_iface(ctdb, vnn);
726 if (best_iface == NULL) {
727 DEBUG(DEBUG_ERR,("takeoverip of IP %s/%u failed to find"
728 "a usable interface (old %s, have_ip %d)\n",
729 ctdb_addr_to_str(&vnn->public_address),
730 vnn->public_netmask_bits,
731 ctdb_vnn_iface_string(vnn),
732 have_ip));
733 return -1;
736 if (vnn->iface == NULL && vnn->pnn == -1 && have_ip && best_iface != NULL) {
737 DEBUG(DEBUG_ERR,("Taking over newly created ip\n"));
738 have_ip = false;
742 if (vnn->iface == NULL && have_ip) {
743 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
744 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
745 ctdb_addr_to_str(&vnn->public_address)));
746 return 0;
749 if (vnn->pnn != ctdb->pnn && have_ip && vnn->pnn != -1) {
750 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
751 "and we have it on iface[%s], but it was assigned to node %d"
752 "and we are node %d, banning ourself\n",
753 ctdb_addr_to_str(&vnn->public_address),
754 ctdb_vnn_iface_string(vnn), vnn->pnn, ctdb->pnn));
755 ctdb_ban_self(ctdb);
756 return -1;
759 if (vnn->pnn == -1 && have_ip) {
760 vnn->pnn = ctdb->pnn;
761 DEBUG(DEBUG_CRIT,(__location__ " takeoverip of IP %s is known to the kernel, "
762 "and we already have it on iface[%s], update local daemon\n",
763 ctdb_addr_to_str(&vnn->public_address),
764 ctdb_vnn_iface_string(vnn)));
765 return 0;
768 if (vnn->iface) {
769 if (vnn->iface != best_iface) {
770 if (!vnn->iface->link_up) {
771 do_updateip = true;
772 } else if (vnn->iface->references > (best_iface->references + 1)) {
773 /* only move when the rebalance gains something */
774 do_updateip = true;
779 if (!have_ip) {
780 if (do_updateip) {
781 ctdb_vnn_unassign_iface(ctdb, vnn);
782 do_updateip = false;
784 do_takeip = true;
787 if (do_takeip) {
788 ret = ctdb_do_takeip(ctdb, c, vnn);
789 if (ret != 0) {
790 return -1;
792 } else if (do_updateip) {
793 ret = ctdb_do_updateip(ctdb, c, vnn);
794 if (ret != 0) {
795 return -1;
797 } else {
799 * The interface is up and the kernel known the ip
800 * => do nothing
802 DEBUG(DEBUG_INFO,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
803 ctdb_addr_to_str(&pip->addr),
804 vnn->public_netmask_bits,
805 ctdb_vnn_iface_string(vnn)));
806 return 0;
809 /* tell ctdb_control.c that we will be replying asynchronously */
810 *async_reply = true;
812 return 0;
816 takeover an ip address old v4 style
818 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb,
819 struct ctdb_req_control *c,
820 TDB_DATA indata,
821 bool *async_reply)
823 TDB_DATA data;
825 data.dsize = sizeof(struct ctdb_public_ip);
826 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
827 CTDB_NO_MEMORY(ctdb, data.dptr);
829 memcpy(data.dptr, indata.dptr, indata.dsize);
830 return ctdb_control_takeover_ip(ctdb, c, data, async_reply);
834 kill any clients that are registered with a IP that is being released
836 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
838 struct ctdb_client_ip *ip;
840 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
841 ctdb_addr_to_str(addr)));
843 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
844 ctdb_sock_addr tmp_addr;
846 tmp_addr = ip->addr;
847 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
848 ip->client_id,
849 ctdb_addr_to_str(&ip->addr)));
851 if (ctdb_same_ip(&tmp_addr, addr)) {
852 struct ctdb_client *client = ctdb_reqid_find(ctdb,
853 ip->client_id,
854 struct ctdb_client);
855 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
856 ip->client_id,
857 ctdb_addr_to_str(&ip->addr),
858 client->pid));
860 if (client->pid != 0) {
861 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
862 (unsigned)client->pid,
863 ctdb_addr_to_str(addr),
864 ip->client_id));
865 kill(client->pid, SIGKILL);
871 static void do_delete_ip(struct ctdb_context *ctdb, struct ctdb_vnn *vnn)
873 DLIST_REMOVE(ctdb->vnn, vnn);
874 ctdb_vnn_unassign_iface(ctdb, vnn);
875 ctdb_remove_orphaned_ifaces(ctdb, vnn);
876 talloc_free(vnn);
880 called when releaseip event finishes
882 static void release_ip_callback(struct ctdb_context *ctdb, int status,
883 void *private_data)
885 struct takeover_callback_state *state =
886 talloc_get_type(private_data, struct takeover_callback_state);
887 TDB_DATA data;
889 if (status == -ETIME) {
890 ctdb_ban_self(ctdb);
893 if (ctdb->do_checkpublicip && ctdb_sys_have_ip(state->addr)) {
894 DEBUG(DEBUG_ERR, ("IP %s still hosted during release IP callback, failing\n",
895 ctdb_addr_to_str(state->addr)));
896 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
897 talloc_free(state);
898 return;
901 /* send a message to all clients of this node telling them
902 that the cluster has been reconfigured and they should
903 release any sockets on this IP */
904 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
905 CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
906 data.dsize = strlen((char *)data.dptr)+1;
908 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
910 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
912 /* kill clients that have registered with this IP */
913 release_kill_clients(ctdb, state->addr);
915 ctdb_vnn_unassign_iface(ctdb, state->vnn);
917 /* Process the IP if it has been marked for deletion */
918 if (state->vnn->delete_pending) {
919 do_delete_ip(ctdb, state->vnn);
920 state->vnn = NULL;
923 /* the control succeeded */
924 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
925 talloc_free(state);
928 static int ctdb_releaseip_destructor(struct takeover_callback_state *state)
930 if (state->vnn != NULL) {
931 state->vnn->update_in_flight = false;
933 return 0;
937 release an ip address
939 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
940 struct ctdb_req_control *c,
941 TDB_DATA indata,
942 bool *async_reply)
944 int ret;
945 struct takeover_callback_state *state;
946 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
947 struct ctdb_vnn *vnn;
948 char *iface;
950 /* update our vnn list */
951 vnn = find_public_ip_vnn(ctdb, &pip->addr);
952 if (vnn == NULL) {
953 DEBUG(DEBUG_INFO,("releaseip called for an ip '%s' that is not a public address\n",
954 ctdb_addr_to_str(&pip->addr)));
955 return 0;
957 vnn->pnn = pip->pnn;
959 /* stop any previous arps */
960 talloc_free(vnn->takeover_ctx);
961 vnn->takeover_ctx = NULL;
963 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
964 * lazy multicast to drop an IP from any node that isn't the
965 * intended new node. The following causes makes ctdbd ignore
966 * a release for any address it doesn't host.
968 if (ctdb->do_checkpublicip) {
969 if (!ctdb_sys_have_ip(&pip->addr)) {
970 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
971 ctdb_addr_to_str(&pip->addr),
972 vnn->public_netmask_bits,
973 ctdb_vnn_iface_string(vnn)));
974 ctdb_vnn_unassign_iface(ctdb, vnn);
975 return 0;
977 } else {
978 if (vnn->iface == NULL) {
979 DEBUG(DEBUG_DEBUG,("Redundant release of IP %s/%u (ip not held)\n",
980 ctdb_addr_to_str(&pip->addr),
981 vnn->public_netmask_bits));
982 return 0;
986 /* There is a potential race between take_ip and us because we
987 * update the VNN via a callback that run when the
988 * eventscripts have been run. Avoid the race by allowing one
989 * update to be in flight at a time.
991 if (vnn->update_in_flight) {
992 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u rejected "
993 "update for this IP already in flight\n",
994 ctdb_addr_to_str(&vnn->public_address),
995 vnn->public_netmask_bits));
996 return -1;
999 if (ctdb->do_checkpublicip) {
1000 iface = ctdb_sys_find_ifname(&pip->addr);
1001 if (iface == NULL) {
1002 DEBUG(DEBUG_ERR, ("Could not find which interface the ip address is hosted on. can not release it\n"));
1003 return 0;
1005 if (vnn->iface == NULL) {
1006 DEBUG(DEBUG_WARNING,
1007 ("Public IP %s is hosted on interface %s but we have no VNN\n",
1008 ctdb_addr_to_str(&pip->addr),
1009 iface));
1010 } else if (strcmp(iface, ctdb_vnn_iface_string(vnn)) != 0) {
1011 DEBUG(DEBUG_WARNING,
1012 ("Public IP %s is hosted on inteterface %s but VNN says %s\n",
1013 ctdb_addr_to_str(&pip->addr),
1014 iface,
1015 ctdb_vnn_iface_string(vnn)));
1016 /* Should we fix vnn->iface? If we do, what
1017 * happens to reference counts?
1020 } else {
1021 iface = strdup(ctdb_vnn_iface_string(vnn));
1024 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s node:%d\n",
1025 ctdb_addr_to_str(&pip->addr),
1026 vnn->public_netmask_bits,
1027 iface,
1028 pip->pnn));
1030 state = talloc(ctdb, struct takeover_callback_state);
1031 CTDB_NO_MEMORY(ctdb, state);
1033 state->c = talloc_steal(state, c);
1034 state->addr = talloc(state, ctdb_sock_addr);
1035 CTDB_NO_MEMORY(ctdb, state->addr);
1036 *state->addr = pip->addr;
1037 state->vnn = vnn;
1039 vnn->update_in_flight = true;
1040 talloc_set_destructor(state, ctdb_releaseip_destructor);
1042 ret = ctdb_event_script_callback(ctdb,
1043 state, release_ip_callback, state,
1044 CTDB_EVENT_RELEASE_IP,
1045 "%s %s %u",
1046 iface,
1047 ctdb_addr_to_str(&pip->addr),
1048 vnn->public_netmask_bits);
1049 free(iface);
1050 if (ret != 0) {
1051 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
1052 ctdb_addr_to_str(&pip->addr),
1053 ctdb_vnn_iface_string(vnn)));
1054 talloc_free(state);
1055 return -1;
1058 /* tell the control that we will be reply asynchronously */
1059 *async_reply = true;
1060 return 0;
1064 release an ip address old v4 style
1066 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb,
1067 struct ctdb_req_control *c,
1068 TDB_DATA indata,
1069 bool *async_reply)
1071 TDB_DATA data;
1073 data.dsize = sizeof(struct ctdb_public_ip);
1074 data.dptr = (uint8_t *)talloc_zero(c, struct ctdb_public_ip);
1075 CTDB_NO_MEMORY(ctdb, data.dptr);
1077 memcpy(data.dptr, indata.dptr, indata.dsize);
1078 return ctdb_control_release_ip(ctdb, c, data, async_reply);
1082 static int ctdb_add_public_address(struct ctdb_context *ctdb,
1083 ctdb_sock_addr *addr,
1084 unsigned mask, const char *ifaces,
1085 bool check_address)
1087 struct ctdb_vnn *vnn;
1088 uint32_t num = 0;
1089 char *tmp;
1090 const char *iface;
1091 int i;
1092 int ret;
1094 tmp = strdup(ifaces);
1095 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1096 if (!ctdb_sys_check_iface_exists(iface)) {
1097 DEBUG(DEBUG_CRIT,("Interface %s does not exist. Can not add public-address : %s\n", iface, ctdb_addr_to_str(addr)));
1098 free(tmp);
1099 return -1;
1102 free(tmp);
1104 /* Verify that we dont have an entry for this ip yet */
1105 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1106 if (ctdb_same_sockaddr(addr, &vnn->public_address)) {
1107 DEBUG(DEBUG_CRIT,("Same ip '%s' specified multiple times in the public address list \n",
1108 ctdb_addr_to_str(addr)));
1109 return -1;
1113 /* create a new vnn structure for this ip address */
1114 vnn = talloc_zero(ctdb, struct ctdb_vnn);
1115 CTDB_NO_MEMORY_FATAL(ctdb, vnn);
1116 vnn->ifaces = talloc_array(vnn, const char *, num + 2);
1117 tmp = talloc_strdup(vnn, ifaces);
1118 CTDB_NO_MEMORY_FATAL(ctdb, tmp);
1119 for (iface = strtok(tmp, ","); iface; iface = strtok(NULL, ",")) {
1120 vnn->ifaces = talloc_realloc(vnn, vnn->ifaces, const char *, num + 2);
1121 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces);
1122 vnn->ifaces[num] = talloc_strdup(vnn, iface);
1123 CTDB_NO_MEMORY_FATAL(ctdb, vnn->ifaces[num]);
1124 num++;
1126 talloc_free(tmp);
1127 vnn->ifaces[num] = NULL;
1128 vnn->public_address = *addr;
1129 vnn->public_netmask_bits = mask;
1130 vnn->pnn = -1;
1131 if (check_address) {
1132 if (ctdb_sys_have_ip(addr)) {
1133 DEBUG(DEBUG_ERR,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr), ctdb->pnn));
1134 vnn->pnn = ctdb->pnn;
1138 for (i=0; vnn->ifaces[i]; i++) {
1139 ret = ctdb_add_local_iface(ctdb, vnn->ifaces[i]);
1140 if (ret != 0) {
1141 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1142 "for public_address[%s]\n",
1143 vnn->ifaces[i], ctdb_addr_to_str(addr)));
1144 talloc_free(vnn);
1145 return -1;
1149 DLIST_ADD(ctdb->vnn, vnn);
1151 return 0;
1154 static void ctdb_check_interfaces_event(struct event_context *ev, struct timed_event *te,
1155 struct timeval t, void *private_data)
1157 struct ctdb_context *ctdb = talloc_get_type(private_data,
1158 struct ctdb_context);
1159 struct ctdb_vnn *vnn;
1161 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1162 int i;
1164 for (i=0; vnn->ifaces[i] != NULL; i++) {
1165 if (!ctdb_sys_check_iface_exists(vnn->ifaces[i])) {
1166 DEBUG(DEBUG_CRIT,("Interface %s does not exist but is used by public ip %s\n",
1167 vnn->ifaces[i],
1168 ctdb_addr_to_str(&vnn->public_address)));
1173 event_add_timed(ctdb->ev, ctdb->check_public_ifaces_ctx,
1174 timeval_current_ofs(30, 0),
1175 ctdb_check_interfaces_event, ctdb);
1179 int ctdb_start_monitoring_interfaces(struct ctdb_context *ctdb)
1181 if (ctdb->check_public_ifaces_ctx != NULL) {
1182 talloc_free(ctdb->check_public_ifaces_ctx);
1183 ctdb->check_public_ifaces_ctx = NULL;
1186 ctdb->check_public_ifaces_ctx = talloc_new(ctdb);
1187 if (ctdb->check_public_ifaces_ctx == NULL) {
1188 ctdb_fatal(ctdb, "failed to allocate context for checking interfaces");
1191 event_add_timed(ctdb->ev, ctdb->check_public_ifaces_ctx,
1192 timeval_current_ofs(30, 0),
1193 ctdb_check_interfaces_event, ctdb);
1195 return 0;
1200 setup the public address lists from a file
1202 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
1204 char **lines;
1205 int nlines;
1206 int i;
1208 lines = file_lines_load(ctdb->public_addresses_file, &nlines, ctdb);
1209 if (lines == NULL) {
1210 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", ctdb->public_addresses_file);
1211 return -1;
1213 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
1214 nlines--;
1217 for (i=0;i<nlines;i++) {
1218 unsigned mask;
1219 ctdb_sock_addr addr;
1220 const char *addrstr;
1221 const char *ifaces;
1222 char *tok, *line;
1224 line = lines[i];
1225 while ((*line == ' ') || (*line == '\t')) {
1226 line++;
1228 if (*line == '#') {
1229 continue;
1231 if (strcmp(line, "") == 0) {
1232 continue;
1234 tok = strtok(line, " \t");
1235 addrstr = tok;
1236 tok = strtok(NULL, " \t");
1237 if (tok == NULL) {
1238 if (NULL == ctdb->default_public_interface) {
1239 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
1240 i+1));
1241 talloc_free(lines);
1242 return -1;
1244 ifaces = ctdb->default_public_interface;
1245 } else {
1246 ifaces = tok;
1249 if (!addrstr || !parse_ip_mask(addrstr, ifaces, &addr, &mask)) {
1250 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
1251 talloc_free(lines);
1252 return -1;
1254 if (ctdb_add_public_address(ctdb, &addr, mask, ifaces, check_addresses)) {
1255 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
1256 talloc_free(lines);
1257 return -1;
1262 talloc_free(lines);
1263 return 0;
1266 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1267 const char *iface,
1268 const char *ip)
1270 struct ctdb_vnn *svnn;
1271 struct ctdb_iface *cur = NULL;
1272 bool ok;
1273 int ret;
1275 svnn = talloc_zero(ctdb, struct ctdb_vnn);
1276 CTDB_NO_MEMORY(ctdb, svnn);
1278 svnn->ifaces = talloc_array(svnn, const char *, 2);
1279 CTDB_NO_MEMORY(ctdb, svnn->ifaces);
1280 svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
1281 CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
1282 svnn->ifaces[1] = NULL;
1284 ok = parse_ip(ip, iface, 0, &svnn->public_address);
1285 if (!ok) {
1286 talloc_free(svnn);
1287 return -1;
1290 ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
1291 if (ret != 0) {
1292 DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
1293 "for single_ip[%s]\n",
1294 svnn->ifaces[0],
1295 ctdb_addr_to_str(&svnn->public_address)));
1296 talloc_free(svnn);
1297 return -1;
1300 /* assume the single public ip interface is initially "good" */
1301 cur = ctdb_find_iface(ctdb, iface);
1302 if (cur == NULL) {
1303 DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
1304 return -1;
1306 cur->link_up = true;
1308 ret = ctdb_vnn_assign_iface(ctdb, svnn);
1309 if (ret != 0) {
1310 talloc_free(svnn);
1311 return -1;
1314 ctdb->single_ip_vnn = svnn;
1315 return 0;
1318 struct ctdb_public_ip_list {
1319 struct ctdb_public_ip_list *next;
1320 uint32_t pnn;
1321 ctdb_sock_addr addr;
1324 /* Given a physical node, return the number of
1325 public addresses that is currently assigned to this node.
1327 static int node_ip_coverage(struct ctdb_context *ctdb,
1328 int32_t pnn,
1329 struct ctdb_public_ip_list *ips)
1331 int num=0;
1333 for (;ips;ips=ips->next) {
1334 if (ips->pnn == pnn) {
1335 num++;
1338 return num;
1342 /* Can the given node host the given IP: is the public IP known to the
1343 * node and is NOIPHOST unset?
1345 static bool can_node_host_ip(struct ctdb_context *ctdb, int32_t pnn,
1346 struct ctdb_ipflags ipflags,
1347 struct ctdb_public_ip_list *ip)
1349 struct ctdb_all_public_ips *public_ips;
1350 int i;
1352 if (ipflags.noiphost) {
1353 return false;
1356 public_ips = ctdb->nodes[pnn]->available_public_ips;
1358 if (public_ips == NULL) {
1359 return false;
1362 for (i=0; i<public_ips->num; i++) {
1363 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
1364 /* yes, this node can serve this public ip */
1365 return true;
1369 return false;
1372 static bool can_node_takeover_ip(struct ctdb_context *ctdb, int32_t pnn,
1373 struct ctdb_ipflags ipflags,
1374 struct ctdb_public_ip_list *ip)
1376 if (ipflags.noiptakeover) {
1377 return false;
1380 return can_node_host_ip(ctdb, pnn, ipflags, ip);
1383 /* search the node lists list for a node to takeover this ip.
1384 pick the node that currently are serving the least number of ips
1385 so that the ips get spread out evenly.
1387 static int find_takeover_node(struct ctdb_context *ctdb,
1388 struct ctdb_ipflags *ipflags,
1389 struct ctdb_public_ip_list *ip,
1390 struct ctdb_public_ip_list *all_ips)
1392 int pnn, min=0, num;
1393 int i, numnodes;
1395 numnodes = talloc_array_length(ipflags);
1396 pnn = -1;
1397 for (i=0; i<numnodes; i++) {
1398 /* verify that this node can serve this ip */
1399 if (!can_node_takeover_ip(ctdb, i, ipflags[i], ip)) {
1400 /* no it couldnt so skip to the next node */
1401 continue;
1404 num = node_ip_coverage(ctdb, i, all_ips);
1405 /* was this the first node we checked ? */
1406 if (pnn == -1) {
1407 pnn = i;
1408 min = num;
1409 } else {
1410 if (num < min) {
1411 pnn = i;
1412 min = num;
1416 if (pnn == -1) {
1417 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
1418 ctdb_addr_to_str(&ip->addr)));
1420 return -1;
1423 ip->pnn = pnn;
1424 return 0;
1427 #define IP_KEYLEN 4
1428 static uint32_t *ip_key(ctdb_sock_addr *ip)
1430 static uint32_t key[IP_KEYLEN];
1432 bzero(key, sizeof(key));
1434 switch (ip->sa.sa_family) {
1435 case AF_INET:
1436 key[3] = htonl(ip->ip.sin_addr.s_addr);
1437 break;
1438 case AF_INET6: {
1439 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1440 key[0] = htonl(s6_a32[0]);
1441 key[1] = htonl(s6_a32[1]);
1442 key[2] = htonl(s6_a32[2]);
1443 key[3] = htonl(s6_a32[3]);
1444 break;
1446 default:
1447 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1448 return key;
1451 return key;
1454 static void *add_ip_callback(void *parm, void *data)
1456 struct ctdb_public_ip_list *this_ip = parm;
1457 struct ctdb_public_ip_list *prev_ip = data;
1459 if (prev_ip == NULL) {
1460 return parm;
1462 if (this_ip->pnn == -1) {
1463 this_ip->pnn = prev_ip->pnn;
1466 return parm;
1469 static int getips_count_callback(void *param, void *data)
1471 struct ctdb_public_ip_list **ip_list = (struct ctdb_public_ip_list **)param;
1472 struct ctdb_public_ip_list *new_ip = (struct ctdb_public_ip_list *)data;
1474 new_ip->next = *ip_list;
1475 *ip_list = new_ip;
1476 return 0;
1479 static struct ctdb_public_ip_list *
1480 create_merged_ip_list(struct ctdb_context *ctdb)
1482 int i, j;
1483 struct ctdb_public_ip_list *ip_list;
1484 struct ctdb_all_public_ips *public_ips;
1486 if (ctdb->ip_tree != NULL) {
1487 talloc_free(ctdb->ip_tree);
1488 ctdb->ip_tree = NULL;
1490 ctdb->ip_tree = trbt_create(ctdb, 0);
1492 for (i=0;i<ctdb->num_nodes;i++) {
1493 public_ips = ctdb->nodes[i]->known_public_ips;
1495 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
1496 continue;
1499 /* there were no public ips for this node */
1500 if (public_ips == NULL) {
1501 continue;
1504 for (j=0;j<public_ips->num;j++) {
1505 struct ctdb_public_ip_list *tmp_ip;
1507 tmp_ip = talloc_zero(ctdb->ip_tree, struct ctdb_public_ip_list);
1508 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
1509 /* Do not use information about IP addresses hosted
1510 * on other nodes, it may not be accurate */
1511 if (public_ips->ips[j].pnn == ctdb->nodes[i]->pnn) {
1512 tmp_ip->pnn = public_ips->ips[j].pnn;
1513 } else {
1514 tmp_ip->pnn = -1;
1516 tmp_ip->addr = public_ips->ips[j].addr;
1517 tmp_ip->next = NULL;
1519 trbt_insertarray32_callback(ctdb->ip_tree,
1520 IP_KEYLEN, ip_key(&public_ips->ips[j].addr),
1521 add_ip_callback,
1522 tmp_ip);
1526 ip_list = NULL;
1527 trbt_traversearray32(ctdb->ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
1529 return ip_list;
1533 * This is the length of the longtest common prefix between the IPs.
1534 * It is calculated by XOR-ing the 2 IPs together and counting the
1535 * number of leading zeroes. The implementation means that all
1536 * addresses end up being 128 bits long.
1538 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1539 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1540 * lots of nodes and IP addresses?
1542 static uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
1544 uint32_t ip1_k[IP_KEYLEN];
1545 uint32_t *t;
1546 int i;
1547 uint32_t x;
1549 uint32_t distance = 0;
1551 memcpy(ip1_k, ip_key(ip1), sizeof(ip1_k));
1552 t = ip_key(ip2);
1553 for (i=0; i<IP_KEYLEN; i++) {
1554 x = ip1_k[i] ^ t[i];
1555 if (x == 0) {
1556 distance += 32;
1557 } else {
1558 /* Count number of leading zeroes.
1559 * FIXME? This could be optimised...
1561 while ((x & (1 << 31)) == 0) {
1562 x <<= 1;
1563 distance += 1;
1568 return distance;
1571 /* Calculate the IP distance for the given IP relative to IPs on the
1572 given node. The ips argument is generally the all_ips variable
1573 used in the main part of the algorithm.
1575 static uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1576 struct ctdb_public_ip_list *ips,
1577 int pnn)
1579 struct ctdb_public_ip_list *t;
1580 uint32_t d;
1582 uint32_t sum = 0;
1584 for (t=ips; t != NULL; t=t->next) {
1585 if (t->pnn != pnn) {
1586 continue;
1589 /* Optimisation: We never calculate the distance
1590 * between an address and itself. This allows us to
1591 * calculate the effect of removing an address from a
1592 * node by simply calculating the distance between
1593 * that address and all of the exitsing addresses.
1594 * Moreover, we assume that we're only ever dealing
1595 * with addresses from all_ips so we can identify an
1596 * address via a pointer rather than doing a more
1597 * expensive address comparison. */
1598 if (&(t->addr) == ip) {
1599 continue;
1602 d = ip_distance(ip, &(t->addr));
1603 sum += d * d; /* Cheaper than pulling in math.h :-) */
1606 return sum;
1609 /* Return the LCP2 imbalance metric for addresses currently assigned
1610 to the given node.
1612 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn)
1614 struct ctdb_public_ip_list *t;
1616 uint32_t imbalance = 0;
1618 for (t=all_ips; t!=NULL; t=t->next) {
1619 if (t->pnn != pnn) {
1620 continue;
1622 /* Pass the rest of the IPs rather than the whole
1623 all_ips input list.
1625 imbalance += ip_distance_2_sum(&(t->addr), t->next, pnn);
1628 return imbalance;
1631 /* Allocate any unassigned IPs just by looping through the IPs and
1632 * finding the best node for each.
1634 static void basic_allocate_unassigned(struct ctdb_context *ctdb,
1635 struct ctdb_ipflags *ipflags,
1636 struct ctdb_public_ip_list *all_ips)
1638 struct ctdb_public_ip_list *tmp_ip;
1640 /* loop over all ip's and find a physical node to cover for
1641 each unassigned ip.
1643 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1644 if (tmp_ip->pnn == -1) {
1645 if (find_takeover_node(ctdb, ipflags, tmp_ip, all_ips)) {
1646 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1647 ctdb_addr_to_str(&tmp_ip->addr)));
1653 /* Basic non-deterministic rebalancing algorithm.
1655 static void basic_failback(struct ctdb_context *ctdb,
1656 struct ctdb_ipflags *ipflags,
1657 struct ctdb_public_ip_list *all_ips,
1658 int num_ips)
1660 int i, numnodes;
1661 int maxnode, maxnum, minnode, minnum, num, retries;
1662 struct ctdb_public_ip_list *tmp_ip;
1664 numnodes = talloc_array_length(ipflags);
1665 retries = 0;
1667 try_again:
1668 maxnum=0;
1669 minnum=0;
1671 /* for each ip address, loop over all nodes that can serve
1672 this ip and make sure that the difference between the node
1673 serving the most and the node serving the least ip's are
1674 not greater than 1.
1676 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1677 if (tmp_ip->pnn == -1) {
1678 continue;
1681 /* Get the highest and lowest number of ips's served by any
1682 valid node which can serve this ip.
1684 maxnode = -1;
1685 minnode = -1;
1686 for (i=0; i<numnodes; i++) {
1687 /* only check nodes that can actually serve this ip */
1688 if (!can_node_takeover_ip(ctdb, i, ipflags[i], tmp_ip)) {
1689 /* no it couldnt so skip to the next node */
1690 continue;
1693 num = node_ip_coverage(ctdb, i, all_ips);
1694 if (maxnode == -1) {
1695 maxnode = i;
1696 maxnum = num;
1697 } else {
1698 if (num > maxnum) {
1699 maxnode = i;
1700 maxnum = num;
1703 if (minnode == -1) {
1704 minnode = i;
1705 minnum = num;
1706 } else {
1707 if (num < minnum) {
1708 minnode = i;
1709 minnum = num;
1713 if (maxnode == -1) {
1714 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
1715 ctdb_addr_to_str(&tmp_ip->addr)));
1717 continue;
1720 /* if the spread between the smallest and largest coverage by
1721 a node is >=2 we steal one of the ips from the node with
1722 most coverage to even things out a bit.
1723 try to do this a limited number of times since we dont
1724 want to spend too much time balancing the ip coverage.
1726 if ( (maxnum > minnum+1)
1727 && (retries < (num_ips + 5)) ){
1728 struct ctdb_public_ip_list *tmp;
1730 /* Reassign one of maxnode's VNNs */
1731 for (tmp=all_ips;tmp;tmp=tmp->next) {
1732 if (tmp->pnn == maxnode) {
1733 (void)find_takeover_node(ctdb, ipflags, tmp, all_ips);
1734 retries++;
1735 goto try_again;;
1742 static void lcp2_init(struct ctdb_context *tmp_ctx,
1743 struct ctdb_ipflags *ipflags,
1744 struct ctdb_public_ip_list *all_ips,
1745 uint32_t *force_rebalance_nodes,
1746 uint32_t **lcp2_imbalances,
1747 bool **rebalance_candidates)
1749 int i, numnodes;
1750 struct ctdb_public_ip_list *tmp_ip;
1752 numnodes = talloc_array_length(ipflags);
1754 *rebalance_candidates = talloc_array(tmp_ctx, bool, numnodes);
1755 CTDB_NO_MEMORY_FATAL(tmp_ctx, *rebalance_candidates);
1756 *lcp2_imbalances = talloc_array(tmp_ctx, uint32_t, numnodes);
1757 CTDB_NO_MEMORY_FATAL(tmp_ctx, *lcp2_imbalances);
1759 for (i=0; i<numnodes; i++) {
1760 (*lcp2_imbalances)[i] = lcp2_imbalance(all_ips, i);
1761 /* First step: assume all nodes are candidates */
1762 (*rebalance_candidates)[i] = true;
1765 /* 2nd step: if a node has IPs assigned then it must have been
1766 * healthy before, so we remove it from consideration. This
1767 * is overkill but is all we have because we don't maintain
1768 * state between takeover runs. An alternative would be to
1769 * keep state and invalidate it every time the recovery master
1770 * changes.
1772 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1773 if (tmp_ip->pnn != -1) {
1774 (*rebalance_candidates)[tmp_ip->pnn] = false;
1778 /* 3rd step: if a node is forced to re-balance then
1779 we allow failback onto the node */
1780 if (force_rebalance_nodes == NULL) {
1781 return;
1783 for (i = 0; i < talloc_array_length(force_rebalance_nodes); i++) {
1784 uint32_t pnn = force_rebalance_nodes[i];
1785 if (pnn >= numnodes) {
1786 DEBUG(DEBUG_ERR,
1787 (__location__ "unknown node %u\n", pnn));
1788 continue;
1791 DEBUG(DEBUG_NOTICE,
1792 ("Forcing rebalancing of IPs to node %u\n", pnn));
1793 (*rebalance_candidates)[pnn] = true;
1797 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1798 * the IP/node combination that will cost the least.
1800 static void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1801 struct ctdb_ipflags *ipflags,
1802 struct ctdb_public_ip_list *all_ips,
1803 uint32_t *lcp2_imbalances)
1805 struct ctdb_public_ip_list *tmp_ip;
1806 int dstnode, numnodes;
1808 int minnode;
1809 uint32_t mindsum, dstdsum, dstimbl, minimbl;
1810 struct ctdb_public_ip_list *minip;
1812 bool should_loop = true;
1813 bool have_unassigned = true;
1815 numnodes = talloc_array_length(ipflags);
1817 while (have_unassigned && should_loop) {
1818 should_loop = false;
1820 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1821 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1823 minnode = -1;
1824 mindsum = 0;
1825 minip = NULL;
1827 /* loop over each unassigned ip. */
1828 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1829 if (tmp_ip->pnn != -1) {
1830 continue;
1833 for (dstnode=0; dstnode<numnodes; dstnode++) {
1834 /* only check nodes that can actually takeover this ip */
1835 if (!can_node_takeover_ip(ctdb, dstnode,
1836 ipflags[dstnode],
1837 tmp_ip)) {
1838 /* no it couldnt so skip to the next node */
1839 continue;
1842 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1843 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1844 DEBUG(DEBUG_DEBUG,(" %s -> %d [+%d]\n",
1845 ctdb_addr_to_str(&(tmp_ip->addr)),
1846 dstnode,
1847 dstimbl - lcp2_imbalances[dstnode]));
1850 if ((minnode == -1) || (dstdsum < mindsum)) {
1851 minnode = dstnode;
1852 minimbl = dstimbl;
1853 mindsum = dstdsum;
1854 minip = tmp_ip;
1855 should_loop = true;
1860 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1862 /* If we found one then assign it to the given node. */
1863 if (minnode != -1) {
1864 minip->pnn = minnode;
1865 lcp2_imbalances[minnode] = minimbl;
1866 DEBUG(DEBUG_INFO,(" %s -> %d [+%d]\n",
1867 ctdb_addr_to_str(&(minip->addr)),
1868 minnode,
1869 mindsum));
1872 /* There might be a better way but at least this is clear. */
1873 have_unassigned = false;
1874 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1875 if (tmp_ip->pnn == -1) {
1876 have_unassigned = true;
1881 /* We know if we have an unassigned addresses so we might as
1882 * well optimise.
1884 if (have_unassigned) {
1885 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
1886 if (tmp_ip->pnn == -1) {
1887 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
1888 ctdb_addr_to_str(&tmp_ip->addr)));
1894 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1895 * to move IPs from, determines the best IP/destination node
1896 * combination to move from the source node.
1898 static bool lcp2_failback_candidate(struct ctdb_context *ctdb,
1899 struct ctdb_ipflags *ipflags,
1900 struct ctdb_public_ip_list *all_ips,
1901 int srcnode,
1902 uint32_t *lcp2_imbalances,
1903 bool *rebalance_candidates)
1905 int dstnode, mindstnode, numnodes;
1906 uint32_t srcimbl, srcdsum, dstimbl, dstdsum;
1907 uint32_t minsrcimbl, mindstimbl;
1908 struct ctdb_public_ip_list *minip;
1909 struct ctdb_public_ip_list *tmp_ip;
1911 /* Find an IP and destination node that best reduces imbalance. */
1912 srcimbl = 0;
1913 minip = NULL;
1914 minsrcimbl = 0;
1915 mindstnode = -1;
1916 mindstimbl = 0;
1918 numnodes = talloc_array_length(ipflags);
1920 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1921 DEBUG(DEBUG_DEBUG,(" CONSIDERING MOVES FROM %d [%d]\n",
1922 srcnode, lcp2_imbalances[srcnode]));
1924 for (tmp_ip=all_ips; tmp_ip; tmp_ip=tmp_ip->next) {
1925 /* Only consider addresses on srcnode. */
1926 if (tmp_ip->pnn != srcnode) {
1927 continue;
1930 /* What is this IP address costing the source node? */
1931 srcdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, srcnode);
1932 srcimbl = lcp2_imbalances[srcnode] - srcdsum;
1934 /* Consider this IP address would cost each potential
1935 * destination node. Destination nodes are limited to
1936 * those that are newly healthy, since we don't want
1937 * to do gratuitous failover of IPs just to make minor
1938 * balance improvements.
1940 for (dstnode=0; dstnode<numnodes; dstnode++) {
1941 if (!rebalance_candidates[dstnode]) {
1942 continue;
1945 /* only check nodes that can actually takeover this ip */
1946 if (!can_node_takeover_ip(ctdb, dstnode,
1947 ipflags[dstnode], tmp_ip)) {
1948 /* no it couldnt so skip to the next node */
1949 continue;
1952 dstdsum = ip_distance_2_sum(&(tmp_ip->addr), all_ips, dstnode);
1953 dstimbl = lcp2_imbalances[dstnode] + dstdsum;
1954 DEBUG(DEBUG_DEBUG,(" %d [%d] -> %s -> %d [+%d]\n",
1955 srcnode, -srcdsum,
1956 ctdb_addr_to_str(&(tmp_ip->addr)),
1957 dstnode, dstdsum));
1959 if ((dstimbl < lcp2_imbalances[srcnode]) &&
1960 (dstdsum < srcdsum) && \
1961 ((mindstnode == -1) || \
1962 ((srcimbl + dstimbl) < (minsrcimbl + mindstimbl)))) {
1964 minip = tmp_ip;
1965 minsrcimbl = srcimbl;
1966 mindstnode = dstnode;
1967 mindstimbl = dstimbl;
1971 DEBUG(DEBUG_DEBUG,(" ----------------------------------------\n"));
1973 if (mindstnode != -1) {
1974 /* We found a move that makes things better... */
1975 DEBUG(DEBUG_INFO,("%d [%d] -> %s -> %d [+%d]\n",
1976 srcnode, minsrcimbl - lcp2_imbalances[srcnode],
1977 ctdb_addr_to_str(&(minip->addr)),
1978 mindstnode, mindstimbl - lcp2_imbalances[mindstnode]));
1981 lcp2_imbalances[srcnode] = minsrcimbl;
1982 lcp2_imbalances[mindstnode] = mindstimbl;
1983 minip->pnn = mindstnode;
1985 return true;
1988 return false;
1992 struct lcp2_imbalance_pnn {
1993 uint32_t imbalance;
1994 int pnn;
1997 static int lcp2_cmp_imbalance_pnn(const void * a, const void * b)
1999 const struct lcp2_imbalance_pnn * lipa = (const struct lcp2_imbalance_pnn *) a;
2000 const struct lcp2_imbalance_pnn * lipb = (const struct lcp2_imbalance_pnn *) b;
2002 if (lipa->imbalance > lipb->imbalance) {
2003 return -1;
2004 } else if (lipa->imbalance == lipb->imbalance) {
2005 return 0;
2006 } else {
2007 return 1;
2011 /* LCP2 algorithm for rebalancing the cluster. This finds the source
2012 * node with the highest LCP2 imbalance, and then determines the best
2013 * IP/destination node combination to move from the source node.
2015 static void lcp2_failback(struct ctdb_context *ctdb,
2016 struct ctdb_ipflags *ipflags,
2017 struct ctdb_public_ip_list *all_ips,
2018 uint32_t *lcp2_imbalances,
2019 bool *rebalance_candidates)
2021 int i, numnodes;
2022 struct lcp2_imbalance_pnn * lips;
2023 bool again;
2025 numnodes = talloc_array_length(ipflags);
2027 try_again:
2028 /* Put the imbalances and nodes into an array, sort them and
2029 * iterate through candidates. Usually the 1st one will be
2030 * used, so this doesn't cost much...
2032 DEBUG(DEBUG_DEBUG,("+++++++++++++++++++++++++++++++++++++++++\n"));
2033 DEBUG(DEBUG_DEBUG,("Selecting most imbalanced node from:\n"));
2034 lips = talloc_array(ctdb, struct lcp2_imbalance_pnn, numnodes);
2035 for (i=0; i<numnodes; i++) {
2036 lips[i].imbalance = lcp2_imbalances[i];
2037 lips[i].pnn = i;
2038 DEBUG(DEBUG_DEBUG,(" %d [%d]\n", i, lcp2_imbalances[i]));
2040 qsort(lips, numnodes, sizeof(struct lcp2_imbalance_pnn),
2041 lcp2_cmp_imbalance_pnn);
2043 again = false;
2044 for (i=0; i<numnodes; i++) {
2045 /* This means that all nodes had 0 or 1 addresses, so
2046 * can't be imbalanced.
2048 if (lips[i].imbalance == 0) {
2049 break;
2052 if (lcp2_failback_candidate(ctdb,
2053 ipflags,
2054 all_ips,
2055 lips[i].pnn,
2056 lcp2_imbalances,
2057 rebalance_candidates)) {
2058 again = true;
2059 break;
2063 talloc_free(lips);
2064 if (again) {
2065 goto try_again;
2069 static void unassign_unsuitable_ips(struct ctdb_context *ctdb,
2070 struct ctdb_ipflags *ipflags,
2071 struct ctdb_public_ip_list *all_ips)
2073 struct ctdb_public_ip_list *tmp_ip;
2075 /* verify that the assigned nodes can serve that public ip
2076 and set it to -1 if not
2078 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2079 if (tmp_ip->pnn == -1) {
2080 continue;
2082 if (!can_node_host_ip(ctdb, tmp_ip->pnn,
2083 ipflags[tmp_ip->pnn], tmp_ip) != 0) {
2084 /* this node can not serve this ip. */
2085 DEBUG(DEBUG_DEBUG,("Unassign IP: %s from %d\n",
2086 ctdb_addr_to_str(&(tmp_ip->addr)),
2087 tmp_ip->pnn));
2088 tmp_ip->pnn = -1;
2093 static void ip_alloc_deterministic_ips(struct ctdb_context *ctdb,
2094 struct ctdb_ipflags *ipflags,
2095 struct ctdb_public_ip_list *all_ips)
2097 struct ctdb_public_ip_list *tmp_ip;
2098 int i, numnodes;
2100 numnodes = talloc_array_length(ipflags);
2102 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2103 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2104 * always be allocated the same way for a specific set of
2105 * available/unavailable nodes.
2108 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
2109 tmp_ip->pnn = i % numnodes;
2112 /* IP failback doesn't make sense with deterministic
2113 * IPs, since the modulo step above implicitly fails
2114 * back IPs to their "home" node.
2116 if (1 == ctdb->tunable.no_ip_failback) {
2117 DEBUG(DEBUG_WARNING, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2120 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2122 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2124 /* No failback here! */
2127 static void ip_alloc_nondeterministic_ips(struct ctdb_context *ctdb,
2128 struct ctdb_ipflags *ipflags,
2129 struct ctdb_public_ip_list *all_ips)
2131 /* This should be pushed down into basic_failback. */
2132 struct ctdb_public_ip_list *tmp_ip;
2133 int num_ips = 0;
2134 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2135 num_ips++;
2138 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2140 basic_allocate_unassigned(ctdb, ipflags, all_ips);
2142 /* If we don't want IPs to fail back then don't rebalance IPs. */
2143 if (1 == ctdb->tunable.no_ip_failback) {
2144 return;
2147 /* Now, try to make sure the ip adresses are evenly distributed
2148 across the nodes.
2150 basic_failback(ctdb, ipflags, all_ips, num_ips);
2153 static void ip_alloc_lcp2(struct ctdb_context *ctdb,
2154 struct ctdb_ipflags *ipflags,
2155 struct ctdb_public_ip_list *all_ips,
2156 uint32_t *force_rebalance_nodes)
2158 uint32_t *lcp2_imbalances;
2159 bool *rebalance_candidates;
2160 int numnodes, num_rebalance_candidates, i;
2162 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2164 unassign_unsuitable_ips(ctdb, ipflags, all_ips);
2166 lcp2_init(tmp_ctx, ipflags, all_ips,force_rebalance_nodes,
2167 &lcp2_imbalances, &rebalance_candidates);
2169 lcp2_allocate_unassigned(ctdb, ipflags, all_ips, lcp2_imbalances);
2171 /* If we don't want IPs to fail back then don't rebalance IPs. */
2172 if (1 == ctdb->tunable.no_ip_failback) {
2173 goto finished;
2176 /* It is only worth continuing if we have suitable target
2177 * nodes to transfer IPs to. This check is much cheaper than
2178 * continuing on...
2180 numnodes = talloc_array_length(ipflags);
2181 num_rebalance_candidates = 0;
2182 for (i=0; i<numnodes; i++) {
2183 if (rebalance_candidates[i]) {
2184 num_rebalance_candidates++;
2187 if (num_rebalance_candidates == 0) {
2188 goto finished;
2191 /* Now, try to make sure the ip adresses are evenly distributed
2192 across the nodes.
2194 lcp2_failback(ctdb, ipflags, all_ips,
2195 lcp2_imbalances, rebalance_candidates);
2197 finished:
2198 talloc_free(tmp_ctx);
2201 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
2203 int i;
2205 for (i=0;i<nodemap->num;i++) {
2206 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
2207 /* Found one completely healthy node */
2208 return false;
2212 return true;
2215 /* The calculation part of the IP allocation algorithm. */
2216 static void ctdb_takeover_run_core(struct ctdb_context *ctdb,
2217 struct ctdb_ipflags *ipflags,
2218 struct ctdb_public_ip_list **all_ips_p,
2219 uint32_t *force_rebalance_nodes)
2221 /* since nodes only know about those public addresses that
2222 can be served by that particular node, no single node has
2223 a full list of all public addresses that exist in the cluster.
2224 Walk over all node structures and create a merged list of
2225 all public addresses that exist in the cluster.
2227 keep the tree of ips around as ctdb->ip_tree
2229 *all_ips_p = create_merged_ip_list(ctdb);
2231 if (1 == ctdb->tunable.lcp2_public_ip_assignment) {
2232 ip_alloc_lcp2(ctdb, ipflags, *all_ips_p, force_rebalance_nodes);
2233 } else if (1 == ctdb->tunable.deterministic_public_ips) {
2234 ip_alloc_deterministic_ips(ctdb, ipflags, *all_ips_p);
2235 } else {
2236 ip_alloc_nondeterministic_ips(ctdb, ipflags, *all_ips_p);
2239 /* at this point ->pnn is the node which will own each IP
2240 or -1 if there is no node that can cover this ip
2243 return;
2246 struct get_tunable_callback_data {
2247 const char *tunable;
2248 uint32_t *out;
2249 bool fatal;
2252 static void get_tunable_callback(struct ctdb_context *ctdb, uint32_t pnn,
2253 int32_t res, TDB_DATA outdata,
2254 void *callback)
2256 struct get_tunable_callback_data *cd =
2257 (struct get_tunable_callback_data *)callback;
2258 int size;
2260 if (res != 0) {
2261 /* Already handled in fail callback */
2262 return;
2265 if (outdata.dsize != sizeof(uint32_t)) {
2266 DEBUG(DEBUG_ERR,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2267 cd->tunable, pnn, (int)sizeof(uint32_t),
2268 (int)outdata.dsize));
2269 cd->fatal = true;
2270 return;
2273 size = talloc_array_length(cd->out);
2274 if (pnn >= size) {
2275 DEBUG(DEBUG_ERR,("Got %s reply from node %d but nodemap only has %d entries\n",
2276 cd->tunable, pnn, size));
2277 return;
2281 cd->out[pnn] = *(uint32_t *)outdata.dptr;
2284 static void get_tunable_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2285 int32_t res, TDB_DATA outdata,
2286 void *callback)
2288 struct get_tunable_callback_data *cd =
2289 (struct get_tunable_callback_data *)callback;
2291 switch (res) {
2292 case -ETIME:
2293 DEBUG(DEBUG_ERR,
2294 ("Timed out getting tunable \"%s\" from node %d\n",
2295 cd->tunable, pnn));
2296 cd->fatal = true;
2297 break;
2298 case -EINVAL:
2299 case -1:
2300 DEBUG(DEBUG_WARNING,
2301 ("Tunable \"%s\" not implemented on node %d\n",
2302 cd->tunable, pnn));
2303 break;
2304 default:
2305 DEBUG(DEBUG_ERR,
2306 ("Unexpected error getting tunable \"%s\" from node %d\n",
2307 cd->tunable, pnn));
2308 cd->fatal = true;
2312 static uint32_t *get_tunable_from_nodes(struct ctdb_context *ctdb,
2313 TALLOC_CTX *tmp_ctx,
2314 struct ctdb_node_map *nodemap,
2315 const char *tunable,
2316 uint32_t default_value)
2318 TDB_DATA data;
2319 struct ctdb_control_get_tunable *t;
2320 uint32_t *nodes;
2321 uint32_t *tvals;
2322 struct get_tunable_callback_data callback_data;
2323 int i;
2325 tvals = talloc_array(tmp_ctx, uint32_t, nodemap->num);
2326 CTDB_NO_MEMORY_NULL(ctdb, tvals);
2327 for (i=0; i<nodemap->num; i++) {
2328 tvals[i] = default_value;
2331 callback_data.out = tvals;
2332 callback_data.tunable = tunable;
2333 callback_data.fatal = false;
2335 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen(tunable) + 1;
2336 data.dptr = talloc_size(tmp_ctx, data.dsize);
2337 t = (struct ctdb_control_get_tunable *)data.dptr;
2338 t->length = strlen(tunable)+1;
2339 memcpy(t->name, tunable, t->length);
2340 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2341 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_TUNABLE,
2342 nodes, 0, TAKEOVER_TIMEOUT(),
2343 false, data,
2344 get_tunable_callback,
2345 get_tunable_fail_callback,
2346 &callback_data) != 0) {
2347 if (callback_data.fatal) {
2348 talloc_free(tvals);
2349 tvals = NULL;
2352 talloc_free(nodes);
2353 talloc_free(data.dptr);
2355 return tvals;
2358 struct get_runstate_callback_data {
2359 enum ctdb_runstate *out;
2360 bool fatal;
2363 static void get_runstate_callback(struct ctdb_context *ctdb, uint32_t pnn,
2364 int32_t res, TDB_DATA outdata,
2365 void *callback_data)
2367 struct get_runstate_callback_data *cd =
2368 (struct get_runstate_callback_data *)callback_data;
2369 int size;
2371 if (res != 0) {
2372 /* Already handled in fail callback */
2373 return;
2376 if (outdata.dsize != sizeof(uint32_t)) {
2377 DEBUG(DEBUG_ERR,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2378 pnn, (int)sizeof(uint32_t),
2379 (int)outdata.dsize));
2380 cd->fatal = true;
2381 return;
2384 size = talloc_array_length(cd->out);
2385 if (pnn >= size) {
2386 DEBUG(DEBUG_ERR,("Got reply from node %d but nodemap only has %d entries\n",
2387 pnn, size));
2388 return;
2391 cd->out[pnn] = (enum ctdb_runstate)*(uint32_t *)outdata.dptr;
2394 static void get_runstate_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2395 int32_t res, TDB_DATA outdata,
2396 void *callback)
2398 struct get_runstate_callback_data *cd =
2399 (struct get_runstate_callback_data *)callback;
2401 switch (res) {
2402 case -ETIME:
2403 DEBUG(DEBUG_ERR,
2404 ("Timed out getting runstate from node %d\n", pnn));
2405 cd->fatal = true;
2406 break;
2407 default:
2408 DEBUG(DEBUG_WARNING,
2409 ("Error getting runstate from node %d - assuming runstates not supported\n",
2410 pnn));
2414 static enum ctdb_runstate * get_runstate_from_nodes(struct ctdb_context *ctdb,
2415 TALLOC_CTX *tmp_ctx,
2416 struct ctdb_node_map *nodemap,
2417 enum ctdb_runstate default_value)
2419 uint32_t *nodes;
2420 enum ctdb_runstate *rs;
2421 struct get_runstate_callback_data callback_data;
2422 int i;
2424 rs = talloc_array(tmp_ctx, enum ctdb_runstate, nodemap->num);
2425 CTDB_NO_MEMORY_NULL(ctdb, rs);
2426 for (i=0; i<nodemap->num; i++) {
2427 rs[i] = default_value;
2430 callback_data.out = rs;
2431 callback_data.fatal = false;
2433 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2434 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_RUNSTATE,
2435 nodes, 0, TAKEOVER_TIMEOUT(),
2436 true, tdb_null,
2437 get_runstate_callback,
2438 get_runstate_fail_callback,
2439 &callback_data) != 0) {
2440 if (callback_data.fatal) {
2441 free(rs);
2442 rs = NULL;
2445 talloc_free(nodes);
2447 return rs;
2450 /* Set internal flags for IP allocation:
2451 * Clear ip flags
2452 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2453 * Set NOIPHOST ip flag for each INACTIVE node
2454 * if all nodes are disabled:
2455 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2456 * else
2457 * Set NOIPHOST ip flags for disabled nodes
2459 static struct ctdb_ipflags *
2460 set_ipflags_internal(struct ctdb_context *ctdb,
2461 TALLOC_CTX *tmp_ctx,
2462 struct ctdb_node_map *nodemap,
2463 uint32_t *tval_noiptakeover,
2464 uint32_t *tval_noiphostonalldisabled,
2465 enum ctdb_runstate *runstate)
2467 int i;
2468 struct ctdb_ipflags *ipflags;
2470 /* Clear IP flags - implicit due to talloc_zero */
2471 ipflags = talloc_zero_array(tmp_ctx, struct ctdb_ipflags, nodemap->num);
2472 CTDB_NO_MEMORY_NULL(ctdb, ipflags);
2474 for (i=0;i<nodemap->num;i++) {
2475 /* Can not take IPs on node with NoIPTakeover set */
2476 if (tval_noiptakeover[i] != 0) {
2477 ipflags[i].noiptakeover = true;
2480 /* Can not host IPs on node not in RUNNING state */
2481 if (runstate[i] != CTDB_RUNSTATE_RUNNING) {
2482 ipflags[i].noiphost = true;
2483 continue;
2485 /* Can not host IPs on INACTIVE node */
2486 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2487 ipflags[i].noiphost = true;
2491 if (all_nodes_are_disabled(nodemap)) {
2492 /* If all nodes are disabled, can not host IPs on node
2493 * with NoIPHostOnAllDisabled set
2495 for (i=0;i<nodemap->num;i++) {
2496 if (tval_noiphostonalldisabled[i] != 0) {
2497 ipflags[i].noiphost = true;
2500 } else {
2501 /* If some nodes are not disabled, then can not host
2502 * IPs on DISABLED node
2504 for (i=0;i<nodemap->num;i++) {
2505 if (nodemap->nodes[i].flags & NODE_FLAGS_DISABLED) {
2506 ipflags[i].noiphost = true;
2511 return ipflags;
2514 static struct ctdb_ipflags *set_ipflags(struct ctdb_context *ctdb,
2515 TALLOC_CTX *tmp_ctx,
2516 struct ctdb_node_map *nodemap)
2518 uint32_t *tval_noiptakeover;
2519 uint32_t *tval_noiphostonalldisabled;
2520 struct ctdb_ipflags *ipflags;
2521 enum ctdb_runstate *runstate;
2524 tval_noiptakeover = get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2525 "NoIPTakeover", 0);
2526 if (tval_noiptakeover == NULL) {
2527 return NULL;
2530 tval_noiphostonalldisabled =
2531 get_tunable_from_nodes(ctdb, tmp_ctx, nodemap,
2532 "NoIPHostOnAllDisabled", 0);
2533 if (tval_noiphostonalldisabled == NULL) {
2534 /* Caller frees tmp_ctx */
2535 return NULL;
2538 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2539 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2540 * reasonable behaviour on a mixed cluster during upgrade.
2542 runstate = get_runstate_from_nodes(ctdb, tmp_ctx, nodemap,
2543 CTDB_RUNSTATE_RUNNING);
2544 if (runstate == NULL) {
2545 /* Caller frees tmp_ctx */
2546 return NULL;
2549 ipflags = set_ipflags_internal(ctdb, tmp_ctx, nodemap,
2550 tval_noiptakeover,
2551 tval_noiphostonalldisabled,
2552 runstate);
2554 talloc_free(tval_noiptakeover);
2555 talloc_free(tval_noiphostonalldisabled);
2556 talloc_free(runstate);
2558 return ipflags;
2561 struct iprealloc_callback_data {
2562 bool *retry_nodes;
2563 int retry_count;
2564 client_async_callback fail_callback;
2565 void *fail_callback_data;
2566 struct ctdb_node_map *nodemap;
2569 static void iprealloc_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
2570 int32_t res, TDB_DATA outdata,
2571 void *callback)
2573 int numnodes;
2574 struct iprealloc_callback_data *cd =
2575 (struct iprealloc_callback_data *)callback;
2577 numnodes = talloc_array_length(cd->retry_nodes);
2578 if (pnn > numnodes) {
2579 DEBUG(DEBUG_ERR,
2580 ("ipreallocated failure from node %d, "
2581 "but only %d nodes in nodemap\n",
2582 pnn, numnodes));
2583 return;
2586 /* Can't run the "ipreallocated" event on a INACTIVE node */
2587 if (cd->nodemap->nodes[pnn].flags & NODE_FLAGS_INACTIVE) {
2588 DEBUG(DEBUG_WARNING,
2589 ("ipreallocated failed on inactive node %d, ignoring\n",
2590 pnn));
2591 return;
2594 switch (res) {
2595 case -ETIME:
2596 /* If the control timed out then that's a real error,
2597 * so call the real fail callback
2599 if (cd->fail_callback) {
2600 cd->fail_callback(ctdb, pnn, res, outdata,
2601 cd->fail_callback_data);
2602 } else {
2603 DEBUG(DEBUG_WARNING,
2604 ("iprealloc timed out but no callback registered\n"));
2606 break;
2607 default:
2608 /* If not a timeout then either the ipreallocated
2609 * eventscript (or some setup) failed. This might
2610 * have failed because the IPREALLOCATED control isn't
2611 * implemented - right now there is no way of knowing
2612 * because the error codes are all folded down to -1.
2613 * Consider retrying using EVENTSCRIPT control...
2615 DEBUG(DEBUG_WARNING,
2616 ("ipreallocated failure from node %d, flagging retry\n",
2617 pnn));
2618 cd->retry_nodes[pnn] = true;
2619 cd->retry_count++;
2623 struct takeover_callback_data {
2624 bool *node_failed;
2625 client_async_callback fail_callback;
2626 void *fail_callback_data;
2627 struct ctdb_node_map *nodemap;
2630 static void takeover_run_fail_callback(struct ctdb_context *ctdb,
2631 uint32_t node_pnn, int32_t res,
2632 TDB_DATA outdata, void *callback_data)
2634 struct takeover_callback_data *cd =
2635 talloc_get_type_abort(callback_data,
2636 struct takeover_callback_data);
2637 int i;
2639 for (i = 0; i < cd->nodemap->num; i++) {
2640 if (node_pnn == cd->nodemap->nodes[i].pnn) {
2641 break;
2645 if (i == cd->nodemap->num) {
2646 DEBUG(DEBUG_ERR, (__location__ " invalid PNN %u\n", node_pnn));
2647 return;
2650 if (!cd->node_failed[i]) {
2651 cd->node_failed[i] = true;
2652 cd->fail_callback(ctdb, node_pnn, res, outdata,
2653 cd->fail_callback_data);
2658 make any IP alias changes for public addresses that are necessary
2660 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
2661 uint32_t *force_rebalance_nodes,
2662 client_async_callback fail_callback, void *callback_data)
2664 int i, j, ret;
2665 struct ctdb_public_ip ip;
2666 struct ctdb_public_ipv4 ipv4;
2667 uint32_t *nodes;
2668 struct ctdb_public_ip_list *all_ips, *tmp_ip;
2669 TDB_DATA data;
2670 struct timeval timeout;
2671 struct client_async_data *async_data;
2672 struct ctdb_client_control_state *state;
2673 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2674 struct ctdb_ipflags *ipflags;
2675 struct takeover_callback_data *takeover_data;
2676 struct iprealloc_callback_data iprealloc_data;
2677 bool *retry_data;
2680 * ip failover is completely disabled, just send out the
2681 * ipreallocated event.
2683 if (ctdb->tunable.disable_ip_failover != 0) {
2684 goto ipreallocated;
2687 ipflags = set_ipflags(ctdb, tmp_ctx, nodemap);
2688 if (ipflags == NULL) {
2689 DEBUG(DEBUG_ERR,("Failed to set IP flags - aborting takeover run\n"));
2690 talloc_free(tmp_ctx);
2691 return -1;
2694 /* Do the IP reassignment calculations */
2695 ctdb_takeover_run_core(ctdb, ipflags, &all_ips, force_rebalance_nodes);
2697 /* Now tell all nodes to release any public IPs should not
2698 * host. This will be a NOOP on nodes that don't currently
2699 * hold the given IP.
2701 takeover_data = talloc_zero(tmp_ctx, struct takeover_callback_data);
2702 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data);
2704 takeover_data->node_failed = talloc_zero_array(tmp_ctx,
2705 bool, nodemap->num);
2706 CTDB_NO_MEMORY_FATAL(ctdb, takeover_data->node_failed);
2707 takeover_data->fail_callback = fail_callback;
2708 takeover_data->fail_callback_data = callback_data;
2709 takeover_data->nodemap = nodemap;
2711 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2712 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2714 async_data->fail_callback = takeover_run_fail_callback;
2715 async_data->callback_data = takeover_data;
2717 ZERO_STRUCT(ip); /* Avoid valgrind warnings for union */
2719 /* Send a RELEASE_IP to all nodes that should not be hosting
2720 * each IP. For each IP, all but one of these will be
2721 * redundant. However, the redundant ones are used to tell
2722 * nodes which node should be hosting the IP so that commands
2723 * like "ctdb ip" can display a particular nodes idea of who
2724 * is hosting what. */
2725 for (i=0;i<nodemap->num;i++) {
2726 /* don't talk to unconnected nodes, but do talk to banned nodes */
2727 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2728 continue;
2731 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2732 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
2733 /* This node should be serving this
2734 vnn so dont tell it to release the ip
2736 continue;
2738 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2739 ipv4.pnn = tmp_ip->pnn;
2740 ipv4.sin = tmp_ip->addr.ip;
2742 timeout = TAKEOVER_TIMEOUT();
2743 data.dsize = sizeof(ipv4);
2744 data.dptr = (uint8_t *)&ipv4;
2745 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2746 0, CTDB_CONTROL_RELEASE_IPv4, 0,
2747 data, async_data,
2748 &timeout, NULL);
2749 } else {
2750 ip.pnn = tmp_ip->pnn;
2751 ip.addr = tmp_ip->addr;
2753 timeout = TAKEOVER_TIMEOUT();
2754 data.dsize = sizeof(ip);
2755 data.dptr = (uint8_t *)&ip;
2756 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
2757 0, CTDB_CONTROL_RELEASE_IP, 0,
2758 data, async_data,
2759 &timeout, NULL);
2762 if (state == NULL) {
2763 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
2764 talloc_free(tmp_ctx);
2765 return -1;
2768 ctdb_client_async_add(async_data, state);
2771 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2772 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2773 talloc_free(tmp_ctx);
2774 return -1;
2776 talloc_free(async_data);
2779 /* For each IP, send a TAKOVER_IP to the node that should be
2780 * hosting it. Many of these will often be redundant (since
2781 * the allocation won't have changed) but they can be useful
2782 * to recover from inconsistencies. */
2783 async_data = talloc_zero(tmp_ctx, struct client_async_data);
2784 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2786 async_data->fail_callback = fail_callback;
2787 async_data->callback_data = callback_data;
2789 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
2790 if (tmp_ip->pnn == -1) {
2791 /* this IP won't be taken over */
2792 continue;
2795 if (tmp_ip->addr.sa.sa_family == AF_INET) {
2796 ipv4.pnn = tmp_ip->pnn;
2797 ipv4.sin = tmp_ip->addr.ip;
2799 timeout = TAKEOVER_TIMEOUT();
2800 data.dsize = sizeof(ipv4);
2801 data.dptr = (uint8_t *)&ipv4;
2802 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2803 0, CTDB_CONTROL_TAKEOVER_IPv4, 0,
2804 data, async_data,
2805 &timeout, NULL);
2806 } else {
2807 ip.pnn = tmp_ip->pnn;
2808 ip.addr = tmp_ip->addr;
2810 timeout = TAKEOVER_TIMEOUT();
2811 data.dsize = sizeof(ip);
2812 data.dptr = (uint8_t *)&ip;
2813 state = ctdb_control_send(ctdb, tmp_ip->pnn,
2814 0, CTDB_CONTROL_TAKEOVER_IP, 0,
2815 data, async_data,
2816 &timeout, NULL);
2818 if (state == NULL) {
2819 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
2820 talloc_free(tmp_ctx);
2821 return -1;
2824 ctdb_client_async_add(async_data, state);
2826 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2827 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2828 talloc_free(tmp_ctx);
2829 return -1;
2832 ipreallocated:
2834 * Tell all nodes to run eventscripts to process the
2835 * "ipreallocated" event. This can do a lot of things,
2836 * including restarting services to reconfigure them if public
2837 * IPs have moved. Once upon a time this event only used to
2838 * update natwg.
2840 retry_data = talloc_zero_array(tmp_ctx, bool, nodemap->num);
2841 CTDB_NO_MEMORY_FATAL(ctdb, retry_data);
2842 iprealloc_data.retry_nodes = retry_data;
2843 iprealloc_data.retry_count = 0;
2844 iprealloc_data.fail_callback = fail_callback;
2845 iprealloc_data.fail_callback_data = callback_data;
2846 iprealloc_data.nodemap = nodemap;
2848 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2849 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_IPREALLOCATED,
2850 nodes, 0, TAKEOVER_TIMEOUT(),
2851 false, tdb_null,
2852 NULL, iprealloc_fail_callback,
2853 &iprealloc_data);
2854 if (ret != 0) {
2855 /* If the control failed then we should retry to any
2856 * nodes flagged by iprealloc_fail_callback using the
2857 * EVENTSCRIPT control. This is a best-effort at
2858 * backward compatiblity when running a mixed cluster
2859 * where some nodes have not yet been upgraded to
2860 * support the IPREALLOCATED control.
2862 DEBUG(DEBUG_WARNING,
2863 ("Retry ipreallocated to some nodes using eventscript control\n"));
2865 nodes = talloc_array(tmp_ctx, uint32_t,
2866 iprealloc_data.retry_count);
2867 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2869 j = 0;
2870 for (i=0; i<nodemap->num; i++) {
2871 if (iprealloc_data.retry_nodes[i]) {
2872 nodes[j] = i;
2873 j++;
2877 data.dptr = discard_const("ipreallocated");
2878 data.dsize = strlen((char *)data.dptr) + 1;
2879 ret = ctdb_client_async_control(ctdb,
2880 CTDB_CONTROL_RUN_EVENTSCRIPTS,
2881 nodes, 0, TAKEOVER_TIMEOUT(),
2882 false, data,
2883 NULL, fail_callback,
2884 callback_data);
2885 if (ret != 0) {
2886 DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
2890 talloc_free(tmp_ctx);
2891 return ret;
2896 destroy a ctdb_client_ip structure
2898 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
2900 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
2901 ctdb_addr_to_str(&ip->addr),
2902 ntohs(ip->addr.ip.sin_port),
2903 ip->client_id));
2905 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
2906 return 0;
2910 called by a client to inform us of a TCP connection that it is managing
2911 that should tickled with an ACK when IP takeover is done
2912 we handle both the old ipv4 style of packets as well as the new ipv4/6
2913 pdus.
2915 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
2916 TDB_DATA indata)
2918 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
2919 struct ctdb_control_tcp *old_addr = NULL;
2920 struct ctdb_control_tcp_addr new_addr;
2921 struct ctdb_control_tcp_addr *tcp_sock = NULL;
2922 struct ctdb_tcp_list *tcp;
2923 struct ctdb_tcp_connection t;
2924 int ret;
2925 TDB_DATA data;
2926 struct ctdb_client_ip *ip;
2927 struct ctdb_vnn *vnn;
2928 ctdb_sock_addr addr;
2930 /* If we don't have public IPs, tickles are useless */
2931 if (ctdb->vnn == NULL) {
2932 return 0;
2935 switch (indata.dsize) {
2936 case sizeof(struct ctdb_control_tcp):
2937 old_addr = (struct ctdb_control_tcp *)indata.dptr;
2938 ZERO_STRUCT(new_addr);
2939 tcp_sock = &new_addr;
2940 tcp_sock->src.ip = old_addr->src;
2941 tcp_sock->dest.ip = old_addr->dest;
2942 break;
2943 case sizeof(struct ctdb_control_tcp_addr):
2944 tcp_sock = (struct ctdb_control_tcp_addr *)indata.dptr;
2945 break;
2946 default:
2947 DEBUG(DEBUG_ERR,(__location__ " Invalid data structure passed "
2948 "to ctdb_control_tcp_client. size was %d but "
2949 "only allowed sizes are %lu and %lu\n",
2950 (int)indata.dsize,
2951 (long unsigned)sizeof(struct ctdb_control_tcp),
2952 (long unsigned)sizeof(struct ctdb_control_tcp_addr)));
2953 return -1;
2956 addr = tcp_sock->src;
2957 ctdb_canonicalize_ip(&addr, &tcp_sock->src);
2958 addr = tcp_sock->dest;
2959 ctdb_canonicalize_ip(&addr, &tcp_sock->dest);
2961 ZERO_STRUCT(addr);
2962 memcpy(&addr, &tcp_sock->dest, sizeof(addr));
2963 vnn = find_public_ip_vnn(ctdb, &addr);
2964 if (vnn == NULL) {
2965 switch (addr.sa.sa_family) {
2966 case AF_INET:
2967 if (ntohl(addr.ip.sin_addr.s_addr) != INADDR_LOOPBACK) {
2968 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public address.\n",
2969 ctdb_addr_to_str(&addr)));
2971 break;
2972 case AF_INET6:
2973 DEBUG(DEBUG_ERR,("Could not add client IP %s. This is not a public ipv6 address.\n",
2974 ctdb_addr_to_str(&addr)));
2975 break;
2976 default:
2977 DEBUG(DEBUG_ERR,(__location__ " Unknown family type %d\n", addr.sa.sa_family));
2980 return 0;
2983 if (vnn->pnn != ctdb->pnn) {
2984 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2985 ctdb_addr_to_str(&addr),
2986 client_id, client->pid));
2987 /* failing this call will tell smbd to die */
2988 return -1;
2991 ip = talloc(client, struct ctdb_client_ip);
2992 CTDB_NO_MEMORY(ctdb, ip);
2994 ip->ctdb = ctdb;
2995 ip->addr = addr;
2996 ip->client_id = client_id;
2997 talloc_set_destructor(ip, ctdb_client_ip_destructor);
2998 DLIST_ADD(ctdb->client_ip_list, ip);
3000 tcp = talloc(client, struct ctdb_tcp_list);
3001 CTDB_NO_MEMORY(ctdb, tcp);
3003 tcp->connection.src_addr = tcp_sock->src;
3004 tcp->connection.dst_addr = tcp_sock->dest;
3006 DLIST_ADD(client->tcp_list, tcp);
3008 t.src_addr = tcp_sock->src;
3009 t.dst_addr = tcp_sock->dest;
3011 data.dptr = (uint8_t *)&t;
3012 data.dsize = sizeof(t);
3014 switch (addr.sa.sa_family) {
3015 case AF_INET:
3016 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
3017 (unsigned)ntohs(tcp_sock->dest.ip.sin_port),
3018 ctdb_addr_to_str(&tcp_sock->src),
3019 (unsigned)ntohs(tcp_sock->src.ip.sin_port), client_id, client->pid));
3020 break;
3021 case AF_INET6:
3022 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
3023 (unsigned)ntohs(tcp_sock->dest.ip6.sin6_port),
3024 ctdb_addr_to_str(&tcp_sock->src),
3025 (unsigned)ntohs(tcp_sock->src.ip6.sin6_port), client_id, client->pid));
3026 break;
3027 default:
3028 DEBUG(DEBUG_ERR,(__location__ " Unknown family %d\n", addr.sa.sa_family));
3032 /* tell all nodes about this tcp connection */
3033 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
3034 CTDB_CONTROL_TCP_ADD,
3035 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
3036 if (ret != 0) {
3037 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
3038 return -1;
3041 return 0;
3045 find a tcp address on a list
3047 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
3048 struct ctdb_tcp_connection *tcp)
3050 int i;
3052 if (array == NULL) {
3053 return NULL;
3056 for (i=0;i<array->num;i++) {
3057 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
3058 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
3059 return &array->connections[i];
3062 return NULL;
3068 called by a daemon to inform us of a TCP connection that one of its
3069 clients managing that should tickled with an ACK when IP takeover is
3070 done
3072 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed)
3074 struct ctdb_tcp_connection *p = (struct ctdb_tcp_connection *)indata.dptr;
3075 struct ctdb_tcp_array *tcparray;
3076 struct ctdb_tcp_connection tcp;
3077 struct ctdb_vnn *vnn;
3079 /* If we don't have public IPs, tickles are useless */
3080 if (ctdb->vnn == NULL) {
3081 return 0;
3084 vnn = find_public_ip_vnn(ctdb, &p->dst_addr);
3085 if (vnn == NULL) {
3086 DEBUG(DEBUG_INFO,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
3087 ctdb_addr_to_str(&p->dst_addr)));
3089 return -1;
3093 tcparray = vnn->tcp_array;
3095 /* If this is the first tickle */
3096 if (tcparray == NULL) {
3097 tcparray = talloc(vnn, struct ctdb_tcp_array);
3098 CTDB_NO_MEMORY(ctdb, tcparray);
3099 vnn->tcp_array = tcparray;
3101 tcparray->num = 0;
3102 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
3103 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3105 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3106 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3107 tcparray->num++;
3109 if (tcp_update_needed) {
3110 vnn->tcp_update_needed = true;
3112 return 0;
3116 /* Do we already have this tickle ?*/
3117 tcp.src_addr = p->src_addr;
3118 tcp.dst_addr = p->dst_addr;
3119 if (ctdb_tcp_find(tcparray, &tcp) != NULL) {
3120 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
3121 ctdb_addr_to_str(&tcp.dst_addr),
3122 ntohs(tcp.dst_addr.ip.sin_port),
3123 vnn->pnn));
3124 return 0;
3127 /* A new tickle, we must add it to the array */
3128 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
3129 struct ctdb_tcp_connection,
3130 tcparray->num+1);
3131 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3133 tcparray->connections[tcparray->num].src_addr = p->src_addr;
3134 tcparray->connections[tcparray->num].dst_addr = p->dst_addr;
3135 tcparray->num++;
3137 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
3138 ctdb_addr_to_str(&tcp.dst_addr),
3139 ntohs(tcp.dst_addr.ip.sin_port),
3140 vnn->pnn));
3142 if (tcp_update_needed) {
3143 vnn->tcp_update_needed = true;
3146 return 0;
3151 called by a daemon to inform us of a TCP connection that one of its
3152 clients managing that should tickled with an ACK when IP takeover is
3153 done
3155 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
3157 struct ctdb_tcp_connection *tcpp;
3158 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
3160 if (vnn == NULL) {
3161 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
3162 ctdb_addr_to_str(&conn->dst_addr)));
3163 return;
3166 /* if the array is empty we cant remove it
3167 and we dont need to do anything
3169 if (vnn->tcp_array == NULL) {
3170 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3171 ctdb_addr_to_str(&conn->dst_addr),
3172 ntohs(conn->dst_addr.ip.sin_port)));
3173 return;
3177 /* See if we know this connection
3178 if we dont know this connection then we dont need to do anything
3180 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
3181 if (tcpp == NULL) {
3182 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
3183 ctdb_addr_to_str(&conn->dst_addr),
3184 ntohs(conn->dst_addr.ip.sin_port)));
3185 return;
3189 /* We need to remove this entry from the array.
3190 Instead of allocating a new array and copying data to it
3191 we cheat and just copy the last entry in the existing array
3192 to the entry that is to be removed and just shring the
3193 ->num field
3195 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
3196 vnn->tcp_array->num--;
3198 /* If we deleted the last entry we also need to remove the entire array
3200 if (vnn->tcp_array->num == 0) {
3201 talloc_free(vnn->tcp_array);
3202 vnn->tcp_array = NULL;
3205 vnn->tcp_update_needed = true;
3207 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
3208 ctdb_addr_to_str(&conn->src_addr),
3209 ntohs(conn->src_addr.ip.sin_port)));
3214 called by a daemon to inform us of a TCP connection that one of its
3215 clients used are no longer needed in the tickle database
3217 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata)
3219 struct ctdb_tcp_connection *conn = (struct ctdb_tcp_connection *)indata.dptr;
3221 /* If we don't have public IPs, tickles are useless */
3222 if (ctdb->vnn == NULL) {
3223 return 0;
3226 ctdb_remove_tcp_connection(ctdb, conn);
3228 return 0;
3233 Called when another daemon starts - causes all tickles for all
3234 public addresses we are serving to be sent to the new node on the
3235 next check. This actually causes the next scheduled call to
3236 tdb_update_tcp_tickles() to update all nodes. This is simple and
3237 doesn't require careful error handling.
3239 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t pnn)
3241 struct ctdb_vnn *vnn;
3243 DEBUG(DEBUG_INFO, ("Received startup control from node %lu\n",
3244 (unsigned long) pnn));
3246 for (vnn = ctdb->vnn; vnn != NULL; vnn = vnn->next) {
3247 vnn->tcp_update_needed = true;
3250 return 0;
3255 called when a client structure goes away - hook to remove
3256 elements from the tcp_list in all daemons
3258 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
3260 while (client->tcp_list) {
3261 struct ctdb_tcp_list *tcp = client->tcp_list;
3262 DLIST_REMOVE(client->tcp_list, tcp);
3263 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
3269 release all IPs on shutdown
3271 void ctdb_release_all_ips(struct ctdb_context *ctdb)
3273 struct ctdb_vnn *vnn;
3274 int count = 0;
3276 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3277 if (!ctdb_sys_have_ip(&vnn->public_address)) {
3278 ctdb_vnn_unassign_iface(ctdb, vnn);
3279 continue;
3281 if (!vnn->iface) {
3282 continue;
3285 DEBUG(DEBUG_INFO,("Release of IP %s/%u on interface %s node:-1\n",
3286 ctdb_addr_to_str(&vnn->public_address),
3287 vnn->public_netmask_bits,
3288 ctdb_vnn_iface_string(vnn)));
3290 ctdb_event_script_args(ctdb, CTDB_EVENT_RELEASE_IP, "%s %s %u",
3291 ctdb_vnn_iface_string(vnn),
3292 ctdb_addr_to_str(&vnn->public_address),
3293 vnn->public_netmask_bits);
3294 release_kill_clients(ctdb, &vnn->public_address);
3295 ctdb_vnn_unassign_iface(ctdb, vnn);
3296 count++;
3299 DEBUG(DEBUG_NOTICE,(__location__ " Released %d public IPs\n", count));
3304 get list of public IPs
3306 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
3307 struct ctdb_req_control *c, TDB_DATA *outdata)
3309 int i, num, len;
3310 struct ctdb_all_public_ips *ips;
3311 struct ctdb_vnn *vnn;
3312 bool only_available = false;
3314 if (c->flags & CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE) {
3315 only_available = true;
3318 /* count how many public ip structures we have */
3319 num = 0;
3320 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3321 num++;
3324 len = offsetof(struct ctdb_all_public_ips, ips) +
3325 num*sizeof(struct ctdb_public_ip);
3326 ips = talloc_zero_size(outdata, len);
3327 CTDB_NO_MEMORY(ctdb, ips);
3329 i = 0;
3330 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3331 if (only_available && !ctdb_vnn_available(ctdb, vnn)) {
3332 continue;
3334 ips->ips[i].pnn = vnn->pnn;
3335 ips->ips[i].addr = vnn->public_address;
3336 i++;
3338 ips->num = i;
3339 len = offsetof(struct ctdb_all_public_ips, ips) +
3340 i*sizeof(struct ctdb_public_ip);
3342 outdata->dsize = len;
3343 outdata->dptr = (uint8_t *)ips;
3345 return 0;
3350 get list of public IPs, old ipv4 style. only returns ipv4 addresses
3352 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb,
3353 struct ctdb_req_control *c, TDB_DATA *outdata)
3355 int i, num, len;
3356 struct ctdb_all_public_ipsv4 *ips;
3357 struct ctdb_vnn *vnn;
3359 /* count how many public ip structures we have */
3360 num = 0;
3361 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3362 if (vnn->public_address.sa.sa_family != AF_INET) {
3363 continue;
3365 num++;
3368 len = offsetof(struct ctdb_all_public_ipsv4, ips) +
3369 num*sizeof(struct ctdb_public_ipv4);
3370 ips = talloc_zero_size(outdata, len);
3371 CTDB_NO_MEMORY(ctdb, ips);
3373 outdata->dsize = len;
3374 outdata->dptr = (uint8_t *)ips;
3376 ips->num = num;
3377 i = 0;
3378 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
3379 if (vnn->public_address.sa.sa_family != AF_INET) {
3380 continue;
3382 ips->ips[i].pnn = vnn->pnn;
3383 ips->ips[i].sin = vnn->public_address.ip;
3384 i++;
3387 return 0;
3390 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
3391 struct ctdb_req_control *c,
3392 TDB_DATA indata,
3393 TDB_DATA *outdata)
3395 int i, num, len;
3396 ctdb_sock_addr *addr;
3397 struct ctdb_control_public_ip_info *info;
3398 struct ctdb_vnn *vnn;
3400 addr = (ctdb_sock_addr *)indata.dptr;
3402 vnn = find_public_ip_vnn(ctdb, addr);
3403 if (vnn == NULL) {
3404 /* if it is not a public ip it could be our 'single ip' */
3405 if (ctdb->single_ip_vnn) {
3406 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
3407 vnn = ctdb->single_ip_vnn;
3411 if (vnn == NULL) {
3412 DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
3413 "'%s'not a public address\n",
3414 ctdb_addr_to_str(addr)));
3415 return -1;
3418 /* count how many public ip structures we have */
3419 num = 0;
3420 for (;vnn->ifaces[num];) {
3421 num++;
3424 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3425 num*sizeof(struct ctdb_control_iface_info);
3426 info = talloc_zero_size(outdata, len);
3427 CTDB_NO_MEMORY(ctdb, info);
3429 info->ip.addr = vnn->public_address;
3430 info->ip.pnn = vnn->pnn;
3431 info->active_idx = 0xFFFFFFFF;
3433 for (i=0; vnn->ifaces[i]; i++) {
3434 struct ctdb_iface *cur;
3436 cur = ctdb_find_iface(ctdb, vnn->ifaces[i]);
3437 if (cur == NULL) {
3438 DEBUG(DEBUG_CRIT, (__location__ " internal error iface[%s] unknown\n",
3439 vnn->ifaces[i]));
3440 return -1;
3442 if (vnn->iface == cur) {
3443 info->active_idx = i;
3445 strncpy(info->ifaces[i].name, cur->name, sizeof(info->ifaces[i].name)-1);
3446 info->ifaces[i].link_state = cur->link_up;
3447 info->ifaces[i].references = cur->references;
3449 info->num = i;
3450 len = offsetof(struct ctdb_control_public_ip_info, ifaces) +
3451 i*sizeof(struct ctdb_control_iface_info);
3453 outdata->dsize = len;
3454 outdata->dptr = (uint8_t *)info;
3456 return 0;
3459 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
3460 struct ctdb_req_control *c,
3461 TDB_DATA *outdata)
3463 int i, num, len;
3464 struct ctdb_control_get_ifaces *ifaces;
3465 struct ctdb_iface *cur;
3467 /* count how many public ip structures we have */
3468 num = 0;
3469 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3470 num++;
3473 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3474 num*sizeof(struct ctdb_control_iface_info);
3475 ifaces = talloc_zero_size(outdata, len);
3476 CTDB_NO_MEMORY(ctdb, ifaces);
3478 i = 0;
3479 for (cur=ctdb->ifaces;cur;cur=cur->next) {
3480 strcpy(ifaces->ifaces[i].name, cur->name);
3481 ifaces->ifaces[i].link_state = cur->link_up;
3482 ifaces->ifaces[i].references = cur->references;
3483 i++;
3485 ifaces->num = i;
3486 len = offsetof(struct ctdb_control_get_ifaces, ifaces) +
3487 i*sizeof(struct ctdb_control_iface_info);
3489 outdata->dsize = len;
3490 outdata->dptr = (uint8_t *)ifaces;
3492 return 0;
3495 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
3496 struct ctdb_req_control *c,
3497 TDB_DATA indata)
3499 struct ctdb_control_iface_info *info;
3500 struct ctdb_iface *iface;
3501 bool link_up = false;
3503 info = (struct ctdb_control_iface_info *)indata.dptr;
3505 if (info->name[CTDB_IFACE_SIZE] != '\0') {
3506 int len = strnlen(info->name, CTDB_IFACE_SIZE);
3507 DEBUG(DEBUG_ERR, (__location__ " name[%*.*s] not terminated\n",
3508 len, len, info->name));
3509 return -1;
3512 switch (info->link_state) {
3513 case 0:
3514 link_up = false;
3515 break;
3516 case 1:
3517 link_up = true;
3518 break;
3519 default:
3520 DEBUG(DEBUG_ERR, (__location__ " link_state[%u] invalid\n",
3521 (unsigned int)info->link_state));
3522 return -1;
3525 if (info->references != 0) {
3526 DEBUG(DEBUG_ERR, (__location__ " references[%u] should be 0\n",
3527 (unsigned int)info->references));
3528 return -1;
3531 iface = ctdb_find_iface(ctdb, info->name);
3532 if (iface == NULL) {
3533 return -1;
3536 if (link_up == iface->link_up) {
3537 return 0;
3540 DEBUG(iface->link_up?DEBUG_ERR:DEBUG_NOTICE,
3541 ("iface[%s] has changed it's link status %s => %s\n",
3542 iface->name,
3543 iface->link_up?"up":"down",
3544 link_up?"up":"down"));
3546 iface->link_up = link_up;
3547 return 0;
3552 structure containing the listening socket and the list of tcp connections
3553 that the ctdb daemon is to kill
3555 struct ctdb_kill_tcp {
3556 struct ctdb_vnn *vnn;
3557 struct ctdb_context *ctdb;
3558 int capture_fd;
3559 struct fd_event *fde;
3560 trbt_tree_t *connections;
3561 void *private_data;
3565 a tcp connection that is to be killed
3567 struct ctdb_killtcp_con {
3568 ctdb_sock_addr src_addr;
3569 ctdb_sock_addr dst_addr;
3570 int count;
3571 struct ctdb_kill_tcp *killtcp;
3574 /* this function is used to create a key to represent this socketpair
3575 in the killtcp tree.
3576 this key is used to insert and lookup matching socketpairs that are
3577 to be tickled and RST
3579 #define KILLTCP_KEYLEN 10
3580 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
3582 static uint32_t key[KILLTCP_KEYLEN];
3584 bzero(key, sizeof(key));
3586 if (src->sa.sa_family != dst->sa.sa_family) {
3587 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
3588 return key;
3591 switch (src->sa.sa_family) {
3592 case AF_INET:
3593 key[0] = dst->ip.sin_addr.s_addr;
3594 key[1] = src->ip.sin_addr.s_addr;
3595 key[2] = dst->ip.sin_port;
3596 key[3] = src->ip.sin_port;
3597 break;
3598 case AF_INET6: {
3599 uint32_t *dst6_addr32 =
3600 (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
3601 uint32_t *src6_addr32 =
3602 (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
3603 key[0] = dst6_addr32[3];
3604 key[1] = src6_addr32[3];
3605 key[2] = dst6_addr32[2];
3606 key[3] = src6_addr32[2];
3607 key[4] = dst6_addr32[1];
3608 key[5] = src6_addr32[1];
3609 key[6] = dst6_addr32[0];
3610 key[7] = src6_addr32[0];
3611 key[8] = dst->ip6.sin6_port;
3612 key[9] = src->ip6.sin6_port;
3613 break;
3615 default:
3616 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
3617 return key;
3620 return key;
3624 called when we get a read event on the raw socket
3626 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
3627 uint16_t flags, void *private_data)
3629 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3630 struct ctdb_killtcp_con *con;
3631 ctdb_sock_addr src, dst;
3632 uint32_t ack_seq, seq;
3634 if (!(flags & EVENT_FD_READ)) {
3635 return;
3638 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
3639 killtcp->private_data,
3640 &src, &dst,
3641 &ack_seq, &seq) != 0) {
3642 /* probably a non-tcp ACK packet */
3643 return;
3646 /* check if we have this guy in our list of connections
3647 to kill
3649 con = trbt_lookuparray32(killtcp->connections,
3650 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
3651 if (con == NULL) {
3652 /* no this was some other packet we can just ignore */
3653 return;
3656 /* This one has been tickled !
3657 now reset him and remove him from the list.
3659 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3660 ntohs(con->dst_addr.ip.sin_port),
3661 ctdb_addr_to_str(&con->src_addr),
3662 ntohs(con->src_addr.ip.sin_port)));
3664 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
3665 talloc_free(con);
3669 /* when traversing the list of all tcp connections to send tickle acks to
3670 (so that we can capture the ack coming back and kill the connection
3671 by a RST)
3672 this callback is called for each connection we are currently trying to kill
3674 static int tickle_connection_traverse(void *param, void *data)
3676 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
3678 /* have tried too many times, just give up */
3679 if (con->count >= 5) {
3680 /* can't delete in traverse: reparent to delete_cons */
3681 talloc_steal(param, con);
3682 return 0;
3685 /* othervise, try tickling it again */
3686 con->count++;
3687 ctdb_sys_send_tcp(
3688 (ctdb_sock_addr *)&con->dst_addr,
3689 (ctdb_sock_addr *)&con->src_addr,
3690 0, 0, 0);
3691 return 0;
3696 called every second until all sentenced connections have been reset
3698 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
3699 struct timeval t, void *private_data)
3701 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
3702 void *delete_cons = talloc_new(NULL);
3704 /* loop over all connections sending tickle ACKs */
3705 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
3707 /* now we've finished traverse, it's safe to do deletion. */
3708 talloc_free(delete_cons);
3710 /* If there are no more connections to kill we can remove the
3711 entire killtcp structure
3713 if ( (killtcp->connections == NULL) ||
3714 (killtcp->connections->root == NULL) ) {
3715 talloc_free(killtcp);
3716 return;
3719 /* try tickling them again in a seconds time
3721 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3722 ctdb_tickle_sentenced_connections, killtcp);
3726 destroy the killtcp structure
3728 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
3730 struct ctdb_vnn *tmpvnn;
3732 /* verify that this vnn is still active */
3733 for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
3734 if (tmpvnn == killtcp->vnn) {
3735 break;
3739 if (tmpvnn == NULL) {
3740 return 0;
3743 if (killtcp->vnn->killtcp != killtcp) {
3744 return 0;
3747 killtcp->vnn->killtcp = NULL;
3749 return 0;
3753 /* nothing fancy here, just unconditionally replace any existing
3754 connection structure with the new one.
3756 dont even free the old one if it did exist, that one is talloc_stolen
3757 by the same node in the tree anyway and will be deleted when the new data
3758 is deleted
3760 static void *add_killtcp_callback(void *parm, void *data)
3762 return parm;
3766 add a tcp socket to the list of connections we want to RST
3768 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
3769 ctdb_sock_addr *s,
3770 ctdb_sock_addr *d)
3772 ctdb_sock_addr src, dst;
3773 struct ctdb_kill_tcp *killtcp;
3774 struct ctdb_killtcp_con *con;
3775 struct ctdb_vnn *vnn;
3777 ctdb_canonicalize_ip(s, &src);
3778 ctdb_canonicalize_ip(d, &dst);
3780 vnn = find_public_ip_vnn(ctdb, &dst);
3781 if (vnn == NULL) {
3782 vnn = find_public_ip_vnn(ctdb, &src);
3784 if (vnn == NULL) {
3785 /* if it is not a public ip it could be our 'single ip' */
3786 if (ctdb->single_ip_vnn) {
3787 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
3788 vnn = ctdb->single_ip_vnn;
3792 if (vnn == NULL) {
3793 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
3794 return -1;
3797 killtcp = vnn->killtcp;
3799 /* If this is the first connection to kill we must allocate
3800 a new structure
3802 if (killtcp == NULL) {
3803 killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
3804 CTDB_NO_MEMORY(ctdb, killtcp);
3806 killtcp->vnn = vnn;
3807 killtcp->ctdb = ctdb;
3808 killtcp->capture_fd = -1;
3809 killtcp->connections = trbt_create(killtcp, 0);
3811 vnn->killtcp = killtcp;
3812 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
3817 /* create a structure that describes this connection we want to
3818 RST and store it in killtcp->connections
3820 con = talloc(killtcp, struct ctdb_killtcp_con);
3821 CTDB_NO_MEMORY(ctdb, con);
3822 con->src_addr = src;
3823 con->dst_addr = dst;
3824 con->count = 0;
3825 con->killtcp = killtcp;
3828 trbt_insertarray32_callback(killtcp->connections,
3829 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
3830 add_killtcp_callback, con);
3833 If we dont have a socket to listen on yet we must create it
3835 if (killtcp->capture_fd == -1) {
3836 const char *iface = ctdb_vnn_iface_string(vnn);
3837 killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
3838 if (killtcp->capture_fd == -1) {
3839 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
3840 "socket on iface '%s' for killtcp (%s)\n",
3841 iface, strerror(errno)));
3842 goto failed;
3847 if (killtcp->fde == NULL) {
3848 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
3849 EVENT_FD_READ,
3850 capture_tcp_handler, killtcp);
3851 tevent_fd_set_auto_close(killtcp->fde);
3853 /* We also need to set up some events to tickle all these connections
3854 until they are all reset
3856 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
3857 ctdb_tickle_sentenced_connections, killtcp);
3860 /* tickle him once now */
3861 ctdb_sys_send_tcp(
3862 &con->dst_addr,
3863 &con->src_addr,
3864 0, 0, 0);
3866 return 0;
3868 failed:
3869 talloc_free(vnn->killtcp);
3870 vnn->killtcp = NULL;
3871 return -1;
3875 kill a TCP connection.
3877 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
3879 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
3881 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
3885 called by a daemon to inform us of the entire list of TCP tickles for
3886 a particular public address.
3887 this control should only be sent by the node that is currently serving
3888 that public address.
3890 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
3892 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
3893 struct ctdb_tcp_array *tcparray;
3894 struct ctdb_vnn *vnn;
3896 /* We must at least have tickles.num or else we cant verify the size
3897 of the received data blob
3899 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3900 tickles.connections)) {
3901 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3902 return -1;
3905 /* verify that the size of data matches what we expect */
3906 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
3907 tickles.connections)
3908 + sizeof(struct ctdb_tcp_connection)
3909 * list->tickles.num) {
3910 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3911 return -1;
3914 DEBUG(DEBUG_INFO, ("Received tickle update for public address %s\n",
3915 ctdb_addr_to_str(&list->addr)));
3917 vnn = find_public_ip_vnn(ctdb, &list->addr);
3918 if (vnn == NULL) {
3919 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
3920 ctdb_addr_to_str(&list->addr)));
3922 return 1;
3925 /* remove any old ticklelist we might have */
3926 talloc_free(vnn->tcp_array);
3927 vnn->tcp_array = NULL;
3929 tcparray = talloc(vnn, struct ctdb_tcp_array);
3930 CTDB_NO_MEMORY(ctdb, tcparray);
3932 tcparray->num = list->tickles.num;
3934 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
3935 CTDB_NO_MEMORY(ctdb, tcparray->connections);
3937 memcpy(tcparray->connections, &list->tickles.connections[0],
3938 sizeof(struct ctdb_tcp_connection)*tcparray->num);
3940 /* We now have a new fresh tickle list array for this vnn */
3941 vnn->tcp_array = tcparray;
3943 return 0;
3947 called to return the full list of tickles for the puclic address associated
3948 with the provided vnn
3950 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
3952 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
3953 struct ctdb_control_tcp_tickle_list *list;
3954 struct ctdb_tcp_array *tcparray;
3955 int num;
3956 struct ctdb_vnn *vnn;
3958 vnn = find_public_ip_vnn(ctdb, addr);
3959 if (vnn == NULL) {
3960 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
3961 ctdb_addr_to_str(addr)));
3963 return 1;
3966 tcparray = vnn->tcp_array;
3967 if (tcparray) {
3968 num = tcparray->num;
3969 } else {
3970 num = 0;
3973 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
3974 tickles.connections)
3975 + sizeof(struct ctdb_tcp_connection) * num;
3977 outdata->dptr = talloc_size(outdata, outdata->dsize);
3978 CTDB_NO_MEMORY(ctdb, outdata->dptr);
3979 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
3981 list->addr = *addr;
3982 list->tickles.num = num;
3983 if (num) {
3984 memcpy(&list->tickles.connections[0], tcparray->connections,
3985 sizeof(struct ctdb_tcp_connection) * num);
3988 return 0;
3993 set the list of all tcp tickles for a public address
3995 static int ctdb_send_set_tcp_tickles_for_ip(struct ctdb_context *ctdb,
3996 ctdb_sock_addr *addr,
3997 struct ctdb_tcp_array *tcparray)
3999 int ret, num;
4000 TDB_DATA data;
4001 struct ctdb_control_tcp_tickle_list *list;
4003 if (tcparray) {
4004 num = tcparray->num;
4005 } else {
4006 num = 0;
4009 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
4010 tickles.connections) +
4011 sizeof(struct ctdb_tcp_connection) * num;
4012 data.dptr = talloc_size(ctdb, data.dsize);
4013 CTDB_NO_MEMORY(ctdb, data.dptr);
4015 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
4016 list->addr = *addr;
4017 list->tickles.num = num;
4018 if (tcparray) {
4019 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
4022 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL, 0,
4023 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
4024 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
4025 if (ret != 0) {
4026 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
4027 return -1;
4030 talloc_free(data.dptr);
4032 return ret;
4037 perform tickle updates if required
4039 static void ctdb_update_tcp_tickles(struct event_context *ev,
4040 struct timed_event *te,
4041 struct timeval t, void *private_data)
4043 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
4044 int ret;
4045 struct ctdb_vnn *vnn;
4047 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4048 /* we only send out updates for public addresses that
4049 we have taken over
4051 if (ctdb->pnn != vnn->pnn) {
4052 continue;
4054 /* We only send out the updates if we need to */
4055 if (!vnn->tcp_update_needed) {
4056 continue;
4058 ret = ctdb_send_set_tcp_tickles_for_ip(ctdb,
4059 &vnn->public_address,
4060 vnn->tcp_array);
4061 if (ret != 0) {
4062 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
4063 ctdb_addr_to_str(&vnn->public_address)));
4064 } else {
4065 DEBUG(DEBUG_INFO,
4066 ("Sent tickle update for public address %s\n",
4067 ctdb_addr_to_str(&vnn->public_address)));
4068 vnn->tcp_update_needed = false;
4072 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4073 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4074 ctdb_update_tcp_tickles, ctdb);
4079 start periodic update of tcp tickles
4081 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
4083 ctdb->tickle_update_context = talloc_new(ctdb);
4085 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
4086 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
4087 ctdb_update_tcp_tickles, ctdb);
4093 struct control_gratious_arp {
4094 struct ctdb_context *ctdb;
4095 ctdb_sock_addr addr;
4096 const char *iface;
4097 int count;
4101 send a control_gratuitous arp
4103 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
4104 struct timeval t, void *private_data)
4106 int ret;
4107 struct control_gratious_arp *arp = talloc_get_type(private_data,
4108 struct control_gratious_arp);
4110 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
4111 if (ret != 0) {
4112 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp on iface '%s' failed (%s)\n",
4113 arp->iface, strerror(errno)));
4117 arp->count++;
4118 if (arp->count == CTDB_ARP_REPEAT) {
4119 talloc_free(arp);
4120 return;
4123 event_add_timed(arp->ctdb->ev, arp,
4124 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
4125 send_gratious_arp, arp);
4130 send a gratious arp
4132 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
4134 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
4135 struct control_gratious_arp *arp;
4137 /* verify the size of indata */
4138 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
4139 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
4140 (unsigned)indata.dsize,
4141 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
4142 return -1;
4144 if (indata.dsize !=
4145 ( offsetof(struct ctdb_control_gratious_arp, iface)
4146 + gratious_arp->len ) ){
4148 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4149 "but should be %u bytes\n",
4150 (unsigned)indata.dsize,
4151 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
4152 return -1;
4156 arp = talloc(ctdb, struct control_gratious_arp);
4157 CTDB_NO_MEMORY(ctdb, arp);
4159 arp->ctdb = ctdb;
4160 arp->addr = gratious_arp->addr;
4161 arp->iface = talloc_strdup(arp, gratious_arp->iface);
4162 CTDB_NO_MEMORY(ctdb, arp->iface);
4163 arp->count = 0;
4165 event_add_timed(arp->ctdb->ev, arp,
4166 timeval_zero(), send_gratious_arp, arp);
4168 return 0;
4171 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
4173 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4174 int ret;
4176 /* verify the size of indata */
4177 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4178 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4179 return -1;
4181 if (indata.dsize !=
4182 ( offsetof(struct ctdb_control_ip_iface, iface)
4183 + pub->len ) ){
4185 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4186 "but should be %u bytes\n",
4187 (unsigned)indata.dsize,
4188 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4189 return -1;
4192 DEBUG(DEBUG_NOTICE,("Add IP %s\n", ctdb_addr_to_str(&pub->addr)));
4194 ret = ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0], true);
4196 if (ret != 0) {
4197 DEBUG(DEBUG_ERR,(__location__ " Failed to add public address\n"));
4198 return -1;
4201 return 0;
4204 struct delete_ip_callback_state {
4205 struct ctdb_req_control *c;
4209 called when releaseip event finishes for del_public_address
4211 static void delete_ip_callback(struct ctdb_context *ctdb,
4212 int32_t status, TDB_DATA data,
4213 const char *errormsg,
4214 void *private_data)
4216 struct delete_ip_callback_state *state =
4217 talloc_get_type(private_data, struct delete_ip_callback_state);
4219 /* If release failed then fail. */
4220 ctdb_request_control_reply(ctdb, state->c, NULL, status, errormsg);
4221 talloc_free(private_data);
4224 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb,
4225 struct ctdb_req_control *c,
4226 TDB_DATA indata, bool *async_reply)
4228 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
4229 struct ctdb_vnn *vnn;
4231 /* verify the size of indata */
4232 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
4233 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
4234 return -1;
4236 if (indata.dsize !=
4237 ( offsetof(struct ctdb_control_ip_iface, iface)
4238 + pub->len ) ){
4240 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
4241 "but should be %u bytes\n",
4242 (unsigned)indata.dsize,
4243 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
4244 return -1;
4247 DEBUG(DEBUG_NOTICE,("Delete IP %s\n", ctdb_addr_to_str(&pub->addr)));
4249 /* walk over all public addresses until we find a match */
4250 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
4251 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
4252 if (vnn->pnn == ctdb->pnn) {
4253 struct delete_ip_callback_state *state;
4254 struct ctdb_public_ip *ip;
4255 TDB_DATA data;
4256 int ret;
4258 vnn->delete_pending = true;
4260 state = talloc(ctdb,
4261 struct delete_ip_callback_state);
4262 CTDB_NO_MEMORY(ctdb, state);
4263 state->c = c;
4265 ip = talloc(state, struct ctdb_public_ip);
4266 if (ip == NULL) {
4267 DEBUG(DEBUG_ERR,
4268 (__location__ " Out of memory\n"));
4269 talloc_free(state);
4270 return -1;
4272 ip->pnn = -1;
4273 ip->addr = pub->addr;
4275 data.dsize = sizeof(struct ctdb_public_ip);
4276 data.dptr = (unsigned char *)ip;
4278 ret = ctdb_daemon_send_control(ctdb,
4279 ctdb_get_pnn(ctdb),
4281 CTDB_CONTROL_RELEASE_IP,
4282 0, 0,
4283 data,
4284 delete_ip_callback,
4285 state);
4286 if (ret == -1) {
4287 DEBUG(DEBUG_ERR,
4288 (__location__ "Unable to send "
4289 "CTDB_CONTROL_RELEASE_IP\n"));
4290 talloc_free(state);
4291 return -1;
4294 state->c = talloc_steal(state, c);
4295 *async_reply = true;
4296 } else {
4297 /* This IP is not hosted on the
4298 * current node so just delete it
4299 * now. */
4300 do_delete_ip(ctdb, vnn);
4303 return 0;
4307 DEBUG(DEBUG_ERR,("Delete IP of unknown public IP address %s\n",
4308 ctdb_addr_to_str(&pub->addr)));
4309 return -1;
4313 struct ipreallocated_callback_state {
4314 struct ctdb_req_control *c;
4317 static void ctdb_ipreallocated_callback(struct ctdb_context *ctdb,
4318 int status, void *p)
4320 struct ipreallocated_callback_state *state =
4321 talloc_get_type(p, struct ipreallocated_callback_state);
4323 if (status != 0) {
4324 DEBUG(DEBUG_ERR,
4325 (" \"ipreallocated\" event script failed (status %d)\n",
4326 status));
4327 if (status == -ETIME) {
4328 ctdb_ban_self(ctdb);
4332 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
4333 talloc_free(state);
4336 /* A control to run the ipreallocated event */
4337 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb,
4338 struct ctdb_req_control *c,
4339 bool *async_reply)
4341 int ret;
4342 struct ipreallocated_callback_state *state;
4344 state = talloc(ctdb, struct ipreallocated_callback_state);
4345 CTDB_NO_MEMORY(ctdb, state);
4347 DEBUG(DEBUG_INFO,(__location__ " Running \"ipreallocated\" event\n"));
4349 ret = ctdb_event_script_callback(ctdb, state,
4350 ctdb_ipreallocated_callback, state,
4351 CTDB_EVENT_IPREALLOCATED,
4352 "%s", "");
4354 if (ret != 0) {
4355 DEBUG(DEBUG_ERR,("Failed to run \"ipreallocated\" event \n"));
4356 talloc_free(state);
4357 return -1;
4360 /* tell the control that we will be reply asynchronously */
4361 state->c = talloc_steal(state, c);
4362 *async_reply = true;
4364 return 0;
4368 /* This function is called from the recovery daemon to verify that a remote
4369 node has the expected ip allocation.
4370 This is verified against ctdb->ip_tree
4372 int verify_remote_ip_allocation(struct ctdb_context *ctdb,
4373 struct ctdb_all_public_ips *ips,
4374 uint32_t pnn)
4376 struct ctdb_public_ip_list *tmp_ip;
4377 int i;
4379 if (ctdb->ip_tree == NULL) {
4380 /* dont know the expected allocation yet, assume remote node
4381 is correct. */
4382 return 0;
4385 if (ips == NULL) {
4386 return 0;
4389 for (i=0; i<ips->num; i++) {
4390 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ips->ips[i].addr));
4391 if (tmp_ip == NULL) {
4392 DEBUG(DEBUG_ERR,("Node %u has new or unknown public IP %s\n", pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
4393 return -1;
4396 if (tmp_ip->pnn == -1 || ips->ips[i].pnn == -1) {
4397 continue;
4400 if (tmp_ip->pnn != ips->ips[i].pnn) {
4401 DEBUG(DEBUG_ERR,
4402 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4403 pnn,
4404 ctdb_addr_to_str(&ips->ips[i].addr),
4405 ips->ips[i].pnn, tmp_ip->pnn));
4406 return -1;
4410 return 0;
4413 int update_ip_assignment_tree(struct ctdb_context *ctdb, struct ctdb_public_ip *ip)
4415 struct ctdb_public_ip_list *tmp_ip;
4417 if (ctdb->ip_tree == NULL) {
4418 DEBUG(DEBUG_ERR,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4419 return -1;
4422 tmp_ip = trbt_lookuparray32(ctdb->ip_tree, IP_KEYLEN, ip_key(&ip->addr));
4423 if (tmp_ip == NULL) {
4424 DEBUG(DEBUG_ERR,(__location__ " Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip->addr)));
4425 return -1;
4428 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));
4429 tmp_ip->pnn = ip->pnn;
4431 return 0;
4435 struct ctdb_reloadips_handle {
4436 struct ctdb_context *ctdb;
4437 struct ctdb_req_control *c;
4438 int status;
4439 int fd[2];
4440 pid_t child;
4441 struct fd_event *fde;
4444 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle *h)
4446 if (h == h->ctdb->reload_ips) {
4447 h->ctdb->reload_ips = NULL;
4449 if (h->c != NULL) {
4450 ctdb_request_control_reply(h->ctdb, h->c, NULL, h->status, NULL);
4451 h->c = NULL;
4453 ctdb_kill(h->ctdb, h->child, SIGKILL);
4454 return 0;
4457 static void ctdb_reloadips_timeout_event(struct event_context *ev,
4458 struct timed_event *te,
4459 struct timeval t, void *private_data)
4461 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4463 talloc_free(h);
4466 static void ctdb_reloadips_child_handler(struct event_context *ev, struct fd_event *fde,
4467 uint16_t flags, void *private_data)
4469 struct ctdb_reloadips_handle *h = talloc_get_type(private_data, struct ctdb_reloadips_handle);
4471 char res;
4472 int ret;
4474 ret = read(h->fd[0], &res, 1);
4475 if (ret < 1 || res != 0) {
4476 DEBUG(DEBUG_ERR, (__location__ " Reloadips child process returned error\n"));
4477 res = 1;
4479 h->status = res;
4481 talloc_free(h);
4484 static int ctdb_reloadips_child(struct ctdb_context *ctdb)
4486 TALLOC_CTX *mem_ctx = talloc_new(NULL);
4487 struct ctdb_all_public_ips *ips;
4488 struct ctdb_vnn *vnn;
4489 struct client_async_data *async_data;
4490 struct timeval timeout;
4491 TDB_DATA data;
4492 struct ctdb_client_control_state *state;
4493 bool first_add;
4494 int i, ret;
4496 CTDB_NO_MEMORY(ctdb, mem_ctx);
4498 /* Read IPs from local node */
4499 ret = ctdb_ctrl_get_public_ips(ctdb, TAKEOVER_TIMEOUT(),
4500 CTDB_CURRENT_NODE, mem_ctx, &ips);
4501 if (ret != 0) {
4502 DEBUG(DEBUG_ERR,
4503 ("Unable to fetch public IPs from local node\n"));
4504 talloc_free(mem_ctx);
4505 return -1;
4508 /* Read IPs file - this is safe since this is a child process */
4509 ctdb->vnn = NULL;
4510 if (ctdb_set_public_addresses(ctdb, false) != 0) {
4511 DEBUG(DEBUG_ERR,("Failed to re-read public addresses file\n"));
4512 talloc_free(mem_ctx);
4513 return -1;
4516 async_data = talloc_zero(mem_ctx, struct client_async_data);
4517 CTDB_NO_MEMORY(ctdb, async_data);
4519 /* Compare IPs between node and file for IPs to be deleted */
4520 for (i = 0; i < ips->num; i++) {
4521 /* */
4522 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4523 if (ctdb_same_ip(&vnn->public_address,
4524 &ips->ips[i].addr)) {
4525 /* IP is still in file */
4526 break;
4530 if (vnn == NULL) {
4531 /* Delete IP ips->ips[i] */
4532 struct ctdb_control_ip_iface *pub;
4534 DEBUG(DEBUG_NOTICE,
4535 ("IP %s no longer configured, deleting it\n",
4536 ctdb_addr_to_str(&ips->ips[i].addr)));
4538 pub = talloc_zero(mem_ctx,
4539 struct ctdb_control_ip_iface);
4540 CTDB_NO_MEMORY(ctdb, pub);
4542 pub->addr = ips->ips[i].addr;
4543 pub->mask = 0;
4544 pub->len = 0;
4546 timeout = TAKEOVER_TIMEOUT();
4548 data.dsize = offsetof(struct ctdb_control_ip_iface,
4549 iface) + pub->len;
4550 data.dptr = (uint8_t *)pub;
4552 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4553 CTDB_CONTROL_DEL_PUBLIC_IP,
4554 0, data, async_data,
4555 &timeout, NULL);
4556 if (state == NULL) {
4557 DEBUG(DEBUG_ERR,
4558 (__location__
4559 " failed sending CTDB_CONTROL_DEL_PUBLIC_IP\n"));
4560 goto failed;
4563 ctdb_client_async_add(async_data, state);
4567 /* Compare IPs between node and file for IPs to be added */
4568 first_add = true;
4569 for (vnn = ctdb->vnn; vnn; vnn = vnn->next) {
4570 for (i = 0; i < ips->num; i++) {
4571 if (ctdb_same_ip(&vnn->public_address,
4572 &ips->ips[i].addr)) {
4573 /* IP already on node */
4574 break;
4577 if (i == ips->num) {
4578 /* Add IP ips->ips[i] */
4579 struct ctdb_control_ip_iface *pub;
4580 const char *ifaces = NULL;
4581 uint32_t len;
4582 int iface = 0;
4584 DEBUG(DEBUG_NOTICE,
4585 ("New IP %s configured, adding it\n",
4586 ctdb_addr_to_str(&vnn->public_address)));
4587 if (first_add) {
4588 uint32_t pnn = ctdb_get_pnn(ctdb);
4590 data.dsize = sizeof(pnn);
4591 data.dptr = (uint8_t *)&pnn;
4593 ret = ctdb_client_send_message(
4594 ctdb,
4595 CTDB_BROADCAST_CONNECTED,
4596 CTDB_SRVID_REBALANCE_NODE,
4597 data);
4598 if (ret != 0) {
4599 DEBUG(DEBUG_WARNING,
4600 ("Failed to send message to force node reallocation - IPs may be unbalanced\n"));
4603 first_add = false;
4606 ifaces = vnn->ifaces[0];
4607 iface = 1;
4608 while (vnn->ifaces[iface] != NULL) {
4609 ifaces = talloc_asprintf(vnn, "%s,%s", ifaces,
4610 vnn->ifaces[iface]);
4611 iface++;
4614 len = strlen(ifaces) + 1;
4615 pub = talloc_zero_size(mem_ctx,
4616 offsetof(struct ctdb_control_ip_iface, iface) + len);
4617 CTDB_NO_MEMORY(ctdb, pub);
4619 pub->addr = vnn->public_address;
4620 pub->mask = vnn->public_netmask_bits;
4621 pub->len = len;
4622 memcpy(&pub->iface[0], ifaces, pub->len);
4624 timeout = TAKEOVER_TIMEOUT();
4626 data.dsize = offsetof(struct ctdb_control_ip_iface,
4627 iface) + pub->len;
4628 data.dptr = (uint8_t *)pub;
4630 state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0,
4631 CTDB_CONTROL_ADD_PUBLIC_IP,
4632 0, data, async_data,
4633 &timeout, NULL);
4634 if (state == NULL) {
4635 DEBUG(DEBUG_ERR,
4636 (__location__
4637 " failed sending CTDB_CONTROL_ADD_PUBLIC_IP\n"));
4638 goto failed;
4641 ctdb_client_async_add(async_data, state);
4645 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
4646 DEBUG(DEBUG_ERR,(__location__ " Add/delete IPs failed\n"));
4647 goto failed;
4650 talloc_free(mem_ctx);
4651 return 0;
4653 failed:
4654 talloc_free(mem_ctx);
4655 return -1;
4658 /* This control is sent to force the node to re-read the public addresses file
4659 and drop any addresses we should nnot longer host, and add new addresses
4660 that we are now able to host
4662 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
4664 struct ctdb_reloadips_handle *h;
4665 pid_t parent = getpid();
4667 if (ctdb->reload_ips != NULL) {
4668 talloc_free(ctdb->reload_ips);
4669 ctdb->reload_ips = NULL;
4672 h = talloc(ctdb, struct ctdb_reloadips_handle);
4673 CTDB_NO_MEMORY(ctdb, h);
4674 h->ctdb = ctdb;
4675 h->c = NULL;
4676 h->status = -1;
4678 if (pipe(h->fd) == -1) {
4679 DEBUG(DEBUG_ERR,("Failed to create pipe for ctdb_freeze_lock\n"));
4680 talloc_free(h);
4681 return -1;
4684 h->child = ctdb_fork(ctdb);
4685 if (h->child == (pid_t)-1) {
4686 DEBUG(DEBUG_ERR, ("Failed to fork a child for reloadips\n"));
4687 close(h->fd[0]);
4688 close(h->fd[1]);
4689 talloc_free(h);
4690 return -1;
4693 /* child process */
4694 if (h->child == 0) {
4695 signed char res = 0;
4697 close(h->fd[0]);
4698 debug_extra = talloc_asprintf(NULL, "reloadips:");
4700 ctdb_set_process_name("ctdb_reloadips");
4701 if (switch_from_server_to_client(ctdb, "reloadips-child") != 0) {
4702 DEBUG(DEBUG_CRIT,("ERROR: Failed to switch reloadips child into client mode\n"));
4703 res = -1;
4704 } else {
4705 res = ctdb_reloadips_child(ctdb);
4706 if (res != 0) {
4707 DEBUG(DEBUG_ERR,("Failed to reload ips on local node\n"));
4711 write(h->fd[1], &res, 1);
4712 /* make sure we die when our parent dies */
4713 while (ctdb_kill(ctdb, parent, 0) == 0 || errno != ESRCH) {
4714 sleep(5);
4716 _exit(0);
4719 h->c = talloc_steal(h, c);
4721 close(h->fd[1]);
4722 set_close_on_exec(h->fd[0]);
4724 talloc_set_destructor(h, ctdb_reloadips_destructor);
4727 h->fde = event_add_fd(ctdb->ev, h, h->fd[0],
4728 EVENT_FD_READ, ctdb_reloadips_child_handler,
4729 (void *)h);
4730 tevent_fd_set_auto_close(h->fde);
4732 event_add_timed(ctdb->ev, h,
4733 timeval_current_ofs(120, 0),
4734 ctdb_reloadips_timeout_event, h);
4736 /* we reply later */
4737 *async_reply = true;
4738 return 0;