4 Copyright (C) Ronnie Sahlberg 2007
5 Copyright (C) Andrew Tridgell 2007
6 Copyright (C) Martin Schwenke 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/dlinklist.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/wait.h"
27 #include "../include/ctdb_private.h"
28 #include "../common/rb_tree.h"
31 #define TAKEOVER_TIMEOUT() timeval_current_ofs(ctdb->tunable.takeover_timeout,0)
33 #define CTDB_ARP_INTERVAL 1
34 #define CTDB_ARP_REPEAT 3
36 /* Flags used in IP allocation algorithms. */
40 enum ctdb_runstate runstate
;
44 struct ctdb_iface
*prev
, *next
;
50 static const char *ctdb_vnn_iface_string(const struct ctdb_vnn
*vnn
)
53 return vnn
->iface
->name
;
59 static int ctdb_add_local_iface(struct ctdb_context
*ctdb
, const char *iface
)
63 /* Verify that we dont have an entry for this ip yet */
64 for (i
=ctdb
->ifaces
;i
;i
=i
->next
) {
65 if (strcmp(i
->name
, iface
) == 0) {
70 /* create a new structure for this interface */
71 i
= talloc_zero(ctdb
, struct ctdb_iface
);
72 CTDB_NO_MEMORY_FATAL(ctdb
, i
);
73 i
->name
= talloc_strdup(i
, iface
);
74 CTDB_NO_MEMORY(ctdb
, i
->name
);
76 * If link_up defaults to true then IPs can be allocated to a
77 * node during the first recovery. However, then an interface
78 * could have its link marked down during the startup event,
79 * causing the IP to move almost immediately. If link_up
80 * defaults to false then, during normal operation, IPs added
81 * to a new interface can't be assigned until a monitor cycle
82 * has occurred and marked the new interfaces up. This makes
83 * IP allocation unpredictable. The following is a neat
84 * compromise: early in startup link_up defaults to false, so
85 * IPs can't be assigned, and after startup IPs can be
86 * assigned immediately.
88 i
->link_up
= (ctdb
->runstate
== CTDB_RUNSTATE_RUNNING
);
90 DLIST_ADD(ctdb
->ifaces
, i
);
95 static bool vnn_has_interface_with_name(struct ctdb_vnn
*vnn
,
100 for (n
= 0; vnn
->ifaces
[n
] != NULL
; n
++) {
101 if (strcmp(name
, vnn
->ifaces
[n
]) == 0) {
109 /* If any interfaces now have no possible IPs then delete them. This
110 * implementation is naive (i.e. simple) rather than clever
111 * (i.e. complex). Given that this is run on delip and that operation
112 * is rare, this doesn't need to be efficient - it needs to be
113 * foolproof. One alternative is reference counting, where the logic
114 * is distributed and can, therefore, be broken in multiple places.
115 * Another alternative is to build a red-black tree of interfaces that
116 * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
117 * once) and then walking ctdb->ifaces once and deleting those not in
118 * the tree. Let's go to one of those if the naive implementation
119 * causes problems... :-)
121 static void ctdb_remove_orphaned_ifaces(struct ctdb_context
*ctdb
,
122 struct ctdb_vnn
*vnn
)
124 struct ctdb_iface
*i
, *next
;
126 /* For each interface, check if there's an IP using it. */
127 for (i
= ctdb
->ifaces
; i
!= NULL
; i
= next
) {
132 /* Only consider interfaces named in the given VNN. */
133 if (!vnn_has_interface_with_name(vnn
, i
->name
)) {
137 /* Is the "single IP" on this interface? */
138 if ((ctdb
->single_ip_vnn
!= NULL
) &&
139 (ctdb
->single_ip_vnn
->ifaces
[0] != NULL
) &&
140 (strcmp(i
->name
, ctdb
->single_ip_vnn
->ifaces
[0]) == 0)) {
141 /* Found, next interface please... */
144 /* Search for a vnn with this interface. */
146 for (tv
=ctdb
->vnn
; tv
; tv
=tv
->next
) {
147 if (vnn_has_interface_with_name(tv
, i
->name
)) {
154 /* None of the VNNs are using this interface. */
155 DLIST_REMOVE(ctdb
->ifaces
, i
);
162 static struct ctdb_iface
*ctdb_find_iface(struct ctdb_context
*ctdb
,
165 struct ctdb_iface
*i
;
167 for (i
=ctdb
->ifaces
;i
;i
=i
->next
) {
168 if (strcmp(i
->name
, iface
) == 0) {
176 static struct ctdb_iface
*ctdb_vnn_best_iface(struct ctdb_context
*ctdb
,
177 struct ctdb_vnn
*vnn
)
180 struct ctdb_iface
*cur
= NULL
;
181 struct ctdb_iface
*best
= NULL
;
183 for (i
=0; vnn
->ifaces
[i
]; i
++) {
185 cur
= ctdb_find_iface(ctdb
, vnn
->ifaces
[i
]);
199 if (cur
->references
< best
->references
) {
208 static int32_t ctdb_vnn_assign_iface(struct ctdb_context
*ctdb
,
209 struct ctdb_vnn
*vnn
)
211 struct ctdb_iface
*best
= NULL
;
214 DEBUG(DEBUG_INFO
, (__location__
" public address '%s' "
215 "still assigned to iface '%s'\n",
216 ctdb_addr_to_str(&vnn
->public_address
),
217 ctdb_vnn_iface_string(vnn
)));
221 best
= ctdb_vnn_best_iface(ctdb
, vnn
);
223 DEBUG(DEBUG_ERR
, (__location__
" public address '%s' "
224 "cannot assign to iface any iface\n",
225 ctdb_addr_to_str(&vnn
->public_address
)));
231 vnn
->pnn
= ctdb
->pnn
;
233 DEBUG(DEBUG_INFO
, (__location__
" public address '%s' "
234 "now assigned to iface '%s' refs[%d]\n",
235 ctdb_addr_to_str(&vnn
->public_address
),
236 ctdb_vnn_iface_string(vnn
),
241 static void ctdb_vnn_unassign_iface(struct ctdb_context
*ctdb
,
242 struct ctdb_vnn
*vnn
)
244 DEBUG(DEBUG_INFO
, (__location__
" public address '%s' "
245 "now unassigned (old iface '%s' refs[%d])\n",
246 ctdb_addr_to_str(&vnn
->public_address
),
247 ctdb_vnn_iface_string(vnn
),
248 vnn
->iface
?vnn
->iface
->references
:0));
250 vnn
->iface
->references
--;
253 if (vnn
->pnn
== ctdb
->pnn
) {
258 static bool ctdb_vnn_available(struct ctdb_context
*ctdb
,
259 struct ctdb_vnn
*vnn
)
263 if (vnn
->delete_pending
) {
267 if (vnn
->iface
&& vnn
->iface
->link_up
) {
271 for (i
=0; vnn
->ifaces
[i
]; i
++) {
272 struct ctdb_iface
*cur
;
274 cur
= ctdb_find_iface(ctdb
, vnn
->ifaces
[i
]);
287 struct ctdb_takeover_arp
{
288 struct ctdb_context
*ctdb
;
291 struct ctdb_tcp_array
*tcparray
;
292 struct ctdb_vnn
*vnn
;
297 lists of tcp endpoints
299 struct ctdb_tcp_list
{
300 struct ctdb_tcp_list
*prev
, *next
;
301 struct ctdb_tcp_connection connection
;
305 list of clients to kill on IP release
307 struct ctdb_client_ip
{
308 struct ctdb_client_ip
*prev
, *next
;
309 struct ctdb_context
*ctdb
;
316 send a gratuitous arp
318 static void ctdb_control_send_arp(struct event_context
*ev
, struct timed_event
*te
,
319 struct timeval t
, void *private_data
)
321 struct ctdb_takeover_arp
*arp
= talloc_get_type(private_data
,
322 struct ctdb_takeover_arp
);
324 struct ctdb_tcp_array
*tcparray
;
325 const char *iface
= ctdb_vnn_iface_string(arp
->vnn
);
327 ret
= ctdb_sys_send_arp(&arp
->addr
, iface
);
329 DEBUG(DEBUG_CRIT
,(__location__
" sending of arp failed on iface '%s' (%s)\n",
330 iface
, strerror(errno
)));
333 tcparray
= arp
->tcparray
;
335 for (i
=0;i
<tcparray
->num
;i
++) {
336 struct ctdb_tcp_connection
*tcon
;
338 tcon
= &tcparray
->connections
[i
];
339 DEBUG(DEBUG_INFO
,("sending tcp tickle ack for %u->%s:%u\n",
340 (unsigned)ntohs(tcon
->dst_addr
.ip
.sin_port
),
341 ctdb_addr_to_str(&tcon
->src_addr
),
342 (unsigned)ntohs(tcon
->src_addr
.ip
.sin_port
)));
343 ret
= ctdb_sys_send_tcp(
348 DEBUG(DEBUG_CRIT
,(__location__
" Failed to send tcp tickle ack for %s\n",
349 ctdb_addr_to_str(&tcon
->src_addr
)));
356 if (arp
->count
== CTDB_ARP_REPEAT
) {
361 event_add_timed(arp
->ctdb
->ev
, arp
->vnn
->takeover_ctx
,
362 timeval_current_ofs(CTDB_ARP_INTERVAL
, 100000),
363 ctdb_control_send_arp
, arp
);
366 static int32_t ctdb_announce_vnn_iface(struct ctdb_context
*ctdb
,
367 struct ctdb_vnn
*vnn
)
369 struct ctdb_takeover_arp
*arp
;
370 struct ctdb_tcp_array
*tcparray
;
372 if (!vnn
->takeover_ctx
) {
373 vnn
->takeover_ctx
= talloc_new(vnn
);
374 if (!vnn
->takeover_ctx
) {
379 arp
= talloc_zero(vnn
->takeover_ctx
, struct ctdb_takeover_arp
);
385 arp
->addr
= vnn
->public_address
;
388 tcparray
= vnn
->tcp_array
;
390 /* add all of the known tcp connections for this IP to the
391 list of tcp connections to send tickle acks for */
392 arp
->tcparray
= talloc_steal(arp
, tcparray
);
394 vnn
->tcp_array
= NULL
;
395 vnn
->tcp_update_needed
= true;
398 event_add_timed(arp
->ctdb
->ev
, vnn
->takeover_ctx
,
399 timeval_zero(), ctdb_control_send_arp
, arp
);
404 struct takeover_callback_state
{
405 struct ctdb_req_control
*c
;
406 ctdb_sock_addr
*addr
;
407 struct ctdb_vnn
*vnn
;
410 struct ctdb_do_takeip_state
{
411 struct ctdb_req_control
*c
;
412 struct ctdb_vnn
*vnn
;
416 called when takeip event finishes
418 static void ctdb_do_takeip_callback(struct ctdb_context
*ctdb
, int status
,
421 struct ctdb_do_takeip_state
*state
=
422 talloc_get_type(private_data
, struct ctdb_do_takeip_state
);
427 struct ctdb_node
*node
= ctdb
->nodes
[ctdb
->pnn
];
429 if (status
== -ETIME
) {
432 DEBUG(DEBUG_ERR
,(__location__
" Failed to takeover IP %s on interface %s\n",
433 ctdb_addr_to_str(&state
->vnn
->public_address
),
434 ctdb_vnn_iface_string(state
->vnn
)));
435 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
437 node
->flags
|= NODE_FLAGS_UNHEALTHY
;
442 if (ctdb
->do_checkpublicip
) {
444 ret
= ctdb_announce_vnn_iface(ctdb
, state
->vnn
);
446 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, -1, NULL
);
453 data
.dptr
= (uint8_t *)ctdb_addr_to_str(&state
->vnn
->public_address
);
454 data
.dsize
= strlen((char *)data
.dptr
) + 1;
455 DEBUG(DEBUG_INFO
,(__location__
" sending TAKE_IP for '%s'\n", data
.dptr
));
457 ctdb_daemon_send_message(ctdb
, ctdb
->pnn
, CTDB_SRVID_TAKE_IP
, data
);
460 /* the control succeeded */
461 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 0, NULL
);
466 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state
*state
)
468 state
->vnn
->update_in_flight
= false;
473 take over an ip address
475 static int32_t ctdb_do_takeip(struct ctdb_context
*ctdb
,
476 struct ctdb_req_control
*c
,
477 struct ctdb_vnn
*vnn
)
480 struct ctdb_do_takeip_state
*state
;
482 if (vnn
->update_in_flight
) {
483 DEBUG(DEBUG_NOTICE
,("Takeover of IP %s/%u rejected "
484 "update for this IP already in flight\n",
485 ctdb_addr_to_str(&vnn
->public_address
),
486 vnn
->public_netmask_bits
));
490 ret
= ctdb_vnn_assign_iface(ctdb
, vnn
);
492 DEBUG(DEBUG_ERR
,("Takeover of IP %s/%u failed to "
493 "assign a usable interface\n",
494 ctdb_addr_to_str(&vnn
->public_address
),
495 vnn
->public_netmask_bits
));
499 state
= talloc(vnn
, struct ctdb_do_takeip_state
);
500 CTDB_NO_MEMORY(ctdb
, state
);
502 state
->c
= talloc_steal(ctdb
, c
);
505 vnn
->update_in_flight
= true;
506 talloc_set_destructor(state
, ctdb_takeip_destructor
);
508 DEBUG(DEBUG_NOTICE
,("Takeover of IP %s/%u on interface %s\n",
509 ctdb_addr_to_str(&vnn
->public_address
),
510 vnn
->public_netmask_bits
,
511 ctdb_vnn_iface_string(vnn
)));
513 ret
= ctdb_event_script_callback(ctdb
,
515 ctdb_do_takeip_callback
,
519 ctdb_vnn_iface_string(vnn
),
520 ctdb_addr_to_str(&vnn
->public_address
),
521 vnn
->public_netmask_bits
);
524 DEBUG(DEBUG_ERR
,(__location__
" Failed to takeover IP %s on interface %s\n",
525 ctdb_addr_to_str(&vnn
->public_address
),
526 ctdb_vnn_iface_string(vnn
)));
534 struct ctdb_do_updateip_state
{
535 struct ctdb_req_control
*c
;
536 struct ctdb_iface
*old
;
537 struct ctdb_vnn
*vnn
;
541 called when updateip event finishes
543 static void ctdb_do_updateip_callback(struct ctdb_context
*ctdb
, int status
,
546 struct ctdb_do_updateip_state
*state
=
547 talloc_get_type(private_data
, struct ctdb_do_updateip_state
);
551 if (status
== -ETIME
) {
554 DEBUG(DEBUG_ERR
,(__location__
" Failed to move IP %s from interface %s to %s\n",
555 ctdb_addr_to_str(&state
->vnn
->public_address
),
557 ctdb_vnn_iface_string(state
->vnn
)));
560 * All we can do is reset the old interface
561 * and let the next run fix it
563 ctdb_vnn_unassign_iface(ctdb
, state
->vnn
);
564 state
->vnn
->iface
= state
->old
;
565 state
->vnn
->iface
->references
++;
567 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
572 if (ctdb
->do_checkpublicip
) {
574 ret
= ctdb_announce_vnn_iface(ctdb
, state
->vnn
);
576 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, -1, NULL
);
583 /* the control succeeded */
584 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 0, NULL
);
589 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state
*state
)
591 state
->vnn
->update_in_flight
= false;
596 update (move) an ip address
598 static int32_t ctdb_do_updateip(struct ctdb_context
*ctdb
,
599 struct ctdb_req_control
*c
,
600 struct ctdb_vnn
*vnn
)
603 struct ctdb_do_updateip_state
*state
;
604 struct ctdb_iface
*old
= vnn
->iface
;
605 const char *new_name
;
607 if (vnn
->update_in_flight
) {
608 DEBUG(DEBUG_NOTICE
,("Update of IP %s/%u rejected "
609 "update for this IP already in flight\n",
610 ctdb_addr_to_str(&vnn
->public_address
),
611 vnn
->public_netmask_bits
));
615 ctdb_vnn_unassign_iface(ctdb
, vnn
);
616 ret
= ctdb_vnn_assign_iface(ctdb
, vnn
);
618 DEBUG(DEBUG_ERR
,("update of IP %s/%u failed to "
619 "assin a usable interface (old iface '%s')\n",
620 ctdb_addr_to_str(&vnn
->public_address
),
621 vnn
->public_netmask_bits
,
626 new_name
= ctdb_vnn_iface_string(vnn
);
627 if (old
->name
!= NULL
&& new_name
!= NULL
&& !strcmp(old
->name
, new_name
)) {
628 /* A benign update from one interface onto itself.
629 * no need to run the eventscripts in this case, just return
632 ctdb_request_control_reply(ctdb
, c
, NULL
, 0, NULL
);
636 state
= talloc(vnn
, struct ctdb_do_updateip_state
);
637 CTDB_NO_MEMORY(ctdb
, state
);
639 state
->c
= talloc_steal(ctdb
, c
);
643 vnn
->update_in_flight
= true;
644 talloc_set_destructor(state
, ctdb_updateip_destructor
);
646 DEBUG(DEBUG_NOTICE
,("Update of IP %s/%u from "
647 "interface %s to %s\n",
648 ctdb_addr_to_str(&vnn
->public_address
),
649 vnn
->public_netmask_bits
,
653 ret
= ctdb_event_script_callback(ctdb
,
655 ctdb_do_updateip_callback
,
657 CTDB_EVENT_UPDATE_IP
,
661 ctdb_addr_to_str(&vnn
->public_address
),
662 vnn
->public_netmask_bits
);
664 DEBUG(DEBUG_ERR
,(__location__
" Failed update IP %s from interface %s to %s\n",
665 ctdb_addr_to_str(&vnn
->public_address
),
666 old
->name
, new_name
));
675 Find the vnn of the node that has a public ip address
676 returns -1 if the address is not known as a public address
678 static struct ctdb_vnn
*find_public_ip_vnn(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
680 struct ctdb_vnn
*vnn
;
682 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
683 if (ctdb_same_ip(&vnn
->public_address
, addr
)) {
692 take over an ip address
694 int32_t ctdb_control_takeover_ip(struct ctdb_context
*ctdb
,
695 struct ctdb_req_control
*c
,
700 struct ctdb_public_ip
*pip
= (struct ctdb_public_ip
*)indata
.dptr
;
701 struct ctdb_vnn
*vnn
;
702 bool have_ip
= false;
703 bool do_updateip
= false;
704 bool do_takeip
= false;
705 struct ctdb_iface
*best_iface
= NULL
;
707 if (pip
->pnn
!= ctdb
->pnn
) {
708 DEBUG(DEBUG_ERR
,(__location__
" takeoverip called for an ip '%s' "
709 "with pnn %d, but we're node %d\n",
710 ctdb_addr_to_str(&pip
->addr
),
711 pip
->pnn
, ctdb
->pnn
));
715 /* update out vnn list */
716 vnn
= find_public_ip_vnn(ctdb
, &pip
->addr
);
718 DEBUG(DEBUG_INFO
,("takeoverip called for an ip '%s' that is not a public address\n",
719 ctdb_addr_to_str(&pip
->addr
)));
723 if (ctdb
->do_checkpublicip
) {
724 have_ip
= ctdb_sys_have_ip(&pip
->addr
);
726 best_iface
= ctdb_vnn_best_iface(ctdb
, vnn
);
727 if (best_iface
== NULL
) {
728 DEBUG(DEBUG_ERR
,("takeoverip of IP %s/%u failed to find"
729 "a usable interface (old %s, have_ip %d)\n",
730 ctdb_addr_to_str(&vnn
->public_address
),
731 vnn
->public_netmask_bits
,
732 ctdb_vnn_iface_string(vnn
),
737 if (vnn
->iface
== NULL
&& vnn
->pnn
== -1 && have_ip
&& best_iface
!= NULL
) {
738 DEBUG(DEBUG_ERR
,("Taking over newly created ip\n"));
743 if (vnn
->iface
== NULL
&& have_ip
) {
744 DEBUG(DEBUG_CRIT
,(__location__
" takeoverip of IP %s is known to the kernel, "
745 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
746 ctdb_addr_to_str(&vnn
->public_address
)));
750 if (vnn
->pnn
!= ctdb
->pnn
&& have_ip
&& vnn
->pnn
!= -1) {
751 DEBUG(DEBUG_CRIT
,(__location__
" takeoverip of IP %s is known to the kernel, "
752 "and we have it on iface[%s], but it was assigned to node %d"
753 "and we are node %d, banning ourself\n",
754 ctdb_addr_to_str(&vnn
->public_address
),
755 ctdb_vnn_iface_string(vnn
), vnn
->pnn
, ctdb
->pnn
));
760 if (vnn
->pnn
== -1 && have_ip
) {
761 vnn
->pnn
= ctdb
->pnn
;
762 DEBUG(DEBUG_CRIT
,(__location__
" takeoverip of IP %s is known to the kernel, "
763 "and we already have it on iface[%s], update local daemon\n",
764 ctdb_addr_to_str(&vnn
->public_address
),
765 ctdb_vnn_iface_string(vnn
)));
770 if (vnn
->iface
!= best_iface
) {
771 if (!vnn
->iface
->link_up
) {
773 } else if (vnn
->iface
->references
> (best_iface
->references
+ 1)) {
774 /* only move when the rebalance gains something */
782 ctdb_vnn_unassign_iface(ctdb
, vnn
);
789 ret
= ctdb_do_takeip(ctdb
, c
, vnn
);
793 } else if (do_updateip
) {
794 ret
= ctdb_do_updateip(ctdb
, c
, vnn
);
800 * The interface is up and the kernel known the ip
803 DEBUG(DEBUG_INFO
,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
804 ctdb_addr_to_str(&pip
->addr
),
805 vnn
->public_netmask_bits
,
806 ctdb_vnn_iface_string(vnn
)));
810 /* tell ctdb_control.c that we will be replying asynchronously */
817 takeover an ip address old v4 style
819 int32_t ctdb_control_takeover_ipv4(struct ctdb_context
*ctdb
,
820 struct ctdb_req_control
*c
,
826 data
.dsize
= sizeof(struct ctdb_public_ip
);
827 data
.dptr
= (uint8_t *)talloc_zero(c
, struct ctdb_public_ip
);
828 CTDB_NO_MEMORY(ctdb
, data
.dptr
);
830 memcpy(data
.dptr
, indata
.dptr
, indata
.dsize
);
831 return ctdb_control_takeover_ip(ctdb
, c
, data
, async_reply
);
835 kill any clients that are registered with a IP that is being released
837 static void release_kill_clients(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
839 struct ctdb_client_ip
*ip
;
841 DEBUG(DEBUG_INFO
,("release_kill_clients for ip %s\n",
842 ctdb_addr_to_str(addr
)));
844 for (ip
=ctdb
->client_ip_list
; ip
; ip
=ip
->next
) {
845 ctdb_sock_addr tmp_addr
;
848 DEBUG(DEBUG_INFO
,("checking for client %u with IP %s\n",
850 ctdb_addr_to_str(&ip
->addr
)));
852 if (ctdb_same_ip(&tmp_addr
, addr
)) {
853 struct ctdb_client
*client
= ctdb_reqid_find(ctdb
,
856 DEBUG(DEBUG_INFO
,("matched client %u with IP %s and pid %u\n",
858 ctdb_addr_to_str(&ip
->addr
),
861 if (client
->pid
!= 0) {
862 DEBUG(DEBUG_INFO
,(__location__
" Killing client pid %u for IP %s on client_id %u\n",
863 (unsigned)client
->pid
,
864 ctdb_addr_to_str(addr
),
866 kill(client
->pid
, SIGKILL
);
872 static void do_delete_ip(struct ctdb_context
*ctdb
, struct ctdb_vnn
*vnn
)
874 DLIST_REMOVE(ctdb
->vnn
, vnn
);
875 ctdb_vnn_unassign_iface(ctdb
, vnn
);
876 ctdb_remove_orphaned_ifaces(ctdb
, vnn
);
881 called when releaseip event finishes
883 static void release_ip_callback(struct ctdb_context
*ctdb
, int status
,
886 struct takeover_callback_state
*state
=
887 talloc_get_type(private_data
, struct takeover_callback_state
);
890 if (status
== -ETIME
) {
894 if (ctdb
->do_checkpublicip
&& ctdb_sys_have_ip(state
->addr
)) {
895 DEBUG(DEBUG_ERR
, ("IP %s still hosted during release IP callback, failing\n",
896 ctdb_addr_to_str(state
->addr
)));
897 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, -1, NULL
);
902 /* send a message to all clients of this node telling them
903 that the cluster has been reconfigured and they should
904 release any sockets on this IP */
905 data
.dptr
= (uint8_t *)talloc_strdup(state
, ctdb_addr_to_str(state
->addr
));
906 CTDB_NO_MEMORY_VOID(ctdb
, data
.dptr
);
907 data
.dsize
= strlen((char *)data
.dptr
)+1;
909 DEBUG(DEBUG_INFO
,(__location__
" sending RELEASE_IP for '%s'\n", data
.dptr
));
911 ctdb_daemon_send_message(ctdb
, ctdb
->pnn
, CTDB_SRVID_RELEASE_IP
, data
);
913 /* kill clients that have registered with this IP */
914 release_kill_clients(ctdb
, state
->addr
);
916 ctdb_vnn_unassign_iface(ctdb
, state
->vnn
);
918 /* Process the IP if it has been marked for deletion */
919 if (state
->vnn
->delete_pending
) {
920 do_delete_ip(ctdb
, state
->vnn
);
924 /* the control succeeded */
925 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 0, NULL
);
929 static int ctdb_releaseip_destructor(struct takeover_callback_state
*state
)
931 if (state
->vnn
!= NULL
) {
932 state
->vnn
->update_in_flight
= false;
938 release an ip address
940 int32_t ctdb_control_release_ip(struct ctdb_context
*ctdb
,
941 struct ctdb_req_control
*c
,
946 struct takeover_callback_state
*state
;
947 struct ctdb_public_ip
*pip
= (struct ctdb_public_ip
*)indata
.dptr
;
948 struct ctdb_vnn
*vnn
;
951 /* update our vnn list */
952 vnn
= find_public_ip_vnn(ctdb
, &pip
->addr
);
954 DEBUG(DEBUG_INFO
,("releaseip called for an ip '%s' that is not a public address\n",
955 ctdb_addr_to_str(&pip
->addr
)));
960 /* stop any previous arps */
961 talloc_free(vnn
->takeover_ctx
);
962 vnn
->takeover_ctx
= NULL
;
964 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
965 * lazy multicast to drop an IP from any node that isn't the
966 * intended new node. The following causes makes ctdbd ignore
967 * a release for any address it doesn't host.
969 if (ctdb
->do_checkpublicip
) {
970 if (!ctdb_sys_have_ip(&pip
->addr
)) {
971 DEBUG(DEBUG_DEBUG
,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
972 ctdb_addr_to_str(&pip
->addr
),
973 vnn
->public_netmask_bits
,
974 ctdb_vnn_iface_string(vnn
)));
975 ctdb_vnn_unassign_iface(ctdb
, vnn
);
979 if (vnn
->iface
== NULL
) {
980 DEBUG(DEBUG_DEBUG
,("Redundant release of IP %s/%u (ip not held)\n",
981 ctdb_addr_to_str(&pip
->addr
),
982 vnn
->public_netmask_bits
));
987 /* There is a potential race between take_ip and us because we
988 * update the VNN via a callback that run when the
989 * eventscripts have been run. Avoid the race by allowing one
990 * update to be in flight at a time.
992 if (vnn
->update_in_flight
) {
993 DEBUG(DEBUG_NOTICE
,("Release of IP %s/%u rejected "
994 "update for this IP already in flight\n",
995 ctdb_addr_to_str(&vnn
->public_address
),
996 vnn
->public_netmask_bits
));
1000 iface
= strdup(ctdb_vnn_iface_string(vnn
));
1002 DEBUG(DEBUG_NOTICE
,("Release of IP %s/%u on interface %s node:%d\n",
1003 ctdb_addr_to_str(&pip
->addr
),
1004 vnn
->public_netmask_bits
,
1008 state
= talloc(ctdb
, struct takeover_callback_state
);
1009 if (state
== NULL
) {
1010 ctdb_set_error(ctdb
, "Out of memory at %s:%d",
1011 __FILE__
, __LINE__
);
1016 state
->c
= talloc_steal(state
, c
);
1017 state
->addr
= talloc(state
, ctdb_sock_addr
);
1018 if (state
->addr
== NULL
) {
1019 ctdb_set_error(ctdb
, "Out of memory at %s:%d",
1020 __FILE__
, __LINE__
);
1025 *state
->addr
= pip
->addr
;
1028 vnn
->update_in_flight
= true;
1029 talloc_set_destructor(state
, ctdb_releaseip_destructor
);
1031 ret
= ctdb_event_script_callback(ctdb
,
1032 state
, release_ip_callback
, state
,
1033 CTDB_EVENT_RELEASE_IP
,
1036 ctdb_addr_to_str(&pip
->addr
),
1037 vnn
->public_netmask_bits
);
1040 DEBUG(DEBUG_ERR
,(__location__
" Failed to release IP %s on interface %s\n",
1041 ctdb_addr_to_str(&pip
->addr
),
1042 ctdb_vnn_iface_string(vnn
)));
1047 /* tell the control that we will be reply asynchronously */
1048 *async_reply
= true;
1053 release an ip address old v4 style
1055 int32_t ctdb_control_release_ipv4(struct ctdb_context
*ctdb
,
1056 struct ctdb_req_control
*c
,
1062 data
.dsize
= sizeof(struct ctdb_public_ip
);
1063 data
.dptr
= (uint8_t *)talloc_zero(c
, struct ctdb_public_ip
);
1064 CTDB_NO_MEMORY(ctdb
, data
.dptr
);
1066 memcpy(data
.dptr
, indata
.dptr
, indata
.dsize
);
1067 return ctdb_control_release_ip(ctdb
, c
, data
, async_reply
);
1071 static int ctdb_add_public_address(struct ctdb_context
*ctdb
,
1072 ctdb_sock_addr
*addr
,
1073 unsigned mask
, const char *ifaces
,
1076 struct ctdb_vnn
*vnn
;
1083 tmp
= strdup(ifaces
);
1084 for (iface
= strtok(tmp
, ","); iface
; iface
= strtok(NULL
, ",")) {
1085 if (!ctdb_sys_check_iface_exists(iface
)) {
1086 DEBUG(DEBUG_CRIT
,("Interface %s does not exist. Can not add public-address : %s\n", iface
, ctdb_addr_to_str(addr
)));
1093 /* Verify that we dont have an entry for this ip yet */
1094 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
1095 if (ctdb_same_sockaddr(addr
, &vnn
->public_address
)) {
1096 DEBUG(DEBUG_CRIT
,("Same ip '%s' specified multiple times in the public address list \n",
1097 ctdb_addr_to_str(addr
)));
1102 /* create a new vnn structure for this ip address */
1103 vnn
= talloc_zero(ctdb
, struct ctdb_vnn
);
1104 CTDB_NO_MEMORY_FATAL(ctdb
, vnn
);
1105 vnn
->ifaces
= talloc_array(vnn
, const char *, num
+ 2);
1106 tmp
= talloc_strdup(vnn
, ifaces
);
1107 CTDB_NO_MEMORY_FATAL(ctdb
, tmp
);
1108 for (iface
= strtok(tmp
, ","); iface
; iface
= strtok(NULL
, ",")) {
1109 vnn
->ifaces
= talloc_realloc(vnn
, vnn
->ifaces
, const char *, num
+ 2);
1110 CTDB_NO_MEMORY_FATAL(ctdb
, vnn
->ifaces
);
1111 vnn
->ifaces
[num
] = talloc_strdup(vnn
, iface
);
1112 CTDB_NO_MEMORY_FATAL(ctdb
, vnn
->ifaces
[num
]);
1116 vnn
->ifaces
[num
] = NULL
;
1117 vnn
->public_address
= *addr
;
1118 vnn
->public_netmask_bits
= mask
;
1120 if (check_address
) {
1121 if (ctdb_sys_have_ip(addr
)) {
1122 DEBUG(DEBUG_ERR
,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr
), ctdb
->pnn
));
1123 vnn
->pnn
= ctdb
->pnn
;
1127 for (i
=0; vnn
->ifaces
[i
]; i
++) {
1128 ret
= ctdb_add_local_iface(ctdb
, vnn
->ifaces
[i
]);
1130 DEBUG(DEBUG_CRIT
, (__location__
" failed to add iface[%s] "
1131 "for public_address[%s]\n",
1132 vnn
->ifaces
[i
], ctdb_addr_to_str(addr
)));
1138 DLIST_ADD(ctdb
->vnn
, vnn
);
1144 setup the public address lists from a file
1146 int ctdb_set_public_addresses(struct ctdb_context
*ctdb
, bool check_addresses
)
1152 lines
= file_lines_load(ctdb
->public_addresses_file
, &nlines
, 0, ctdb
);
1153 if (lines
== NULL
) {
1154 ctdb_set_error(ctdb
, "Failed to load public address list '%s'\n", ctdb
->public_addresses_file
);
1157 while (nlines
> 0 && strcmp(lines
[nlines
-1], "") == 0) {
1161 for (i
=0;i
<nlines
;i
++) {
1163 ctdb_sock_addr addr
;
1164 const char *addrstr
;
1169 while ((*line
== ' ') || (*line
== '\t')) {
1175 if (strcmp(line
, "") == 0) {
1178 tok
= strtok(line
, " \t");
1180 tok
= strtok(NULL
, " \t");
1182 if (NULL
== ctdb
->default_public_interface
) {
1183 DEBUG(DEBUG_CRIT
,("No default public interface and no interface specified at line %u of public address list\n",
1188 ifaces
= ctdb
->default_public_interface
;
1193 if (!addrstr
|| !parse_ip_mask(addrstr
, ifaces
, &addr
, &mask
)) {
1194 DEBUG(DEBUG_CRIT
,("Badly formed line %u in public address list\n", i
+1));
1198 if (ctdb_add_public_address(ctdb
, &addr
, mask
, ifaces
, check_addresses
)) {
1199 DEBUG(DEBUG_CRIT
,("Failed to add line %u to the public address list\n", i
+1));
1210 int ctdb_set_single_public_ip(struct ctdb_context
*ctdb
,
1214 struct ctdb_vnn
*svnn
;
1215 struct ctdb_iface
*cur
= NULL
;
1219 svnn
= talloc_zero(ctdb
, struct ctdb_vnn
);
1220 CTDB_NO_MEMORY(ctdb
, svnn
);
1222 svnn
->ifaces
= talloc_array(svnn
, const char *, 2);
1223 CTDB_NO_MEMORY(ctdb
, svnn
->ifaces
);
1224 svnn
->ifaces
[0] = talloc_strdup(svnn
->ifaces
, iface
);
1225 CTDB_NO_MEMORY(ctdb
, svnn
->ifaces
[0]);
1226 svnn
->ifaces
[1] = NULL
;
1228 ok
= parse_ip(ip
, iface
, 0, &svnn
->public_address
);
1234 ret
= ctdb_add_local_iface(ctdb
, svnn
->ifaces
[0]);
1236 DEBUG(DEBUG_CRIT
, (__location__
" failed to add iface[%s] "
1237 "for single_ip[%s]\n",
1239 ctdb_addr_to_str(&svnn
->public_address
)));
1244 /* assume the single public ip interface is initially "good" */
1245 cur
= ctdb_find_iface(ctdb
, iface
);
1247 DEBUG(DEBUG_CRIT
,("Can not find public interface %s used by --single-public-ip", iface
));
1250 cur
->link_up
= true;
1252 ret
= ctdb_vnn_assign_iface(ctdb
, svnn
);
1258 ctdb
->single_ip_vnn
= svnn
;
1262 struct ctdb_public_ip_list
{
1263 struct ctdb_public_ip_list
*next
;
1265 ctdb_sock_addr addr
;
1268 /* Given a physical node, return the number of
1269 public addresses that is currently assigned to this node.
1271 static int node_ip_coverage(struct ctdb_context
*ctdb
,
1273 struct ctdb_public_ip_list
*ips
)
1277 for (;ips
;ips
=ips
->next
) {
1278 if (ips
->pnn
== pnn
) {
1286 /* Can the given node host the given IP: is the public IP known to the
1287 * node and is NOIPHOST unset?
1289 static bool can_node_host_ip(struct ctdb_context
*ctdb
, int32_t pnn
,
1290 struct ctdb_ipflags ipflags
,
1291 struct ctdb_public_ip_list
*ip
)
1293 struct ctdb_all_public_ips
*public_ips
;
1296 if (ipflags
.noiphost
) {
1300 public_ips
= ctdb
->nodes
[pnn
]->available_public_ips
;
1302 if (public_ips
== NULL
) {
1306 for (i
=0; i
<public_ips
->num
; i
++) {
1307 if (ctdb_same_ip(&ip
->addr
, &public_ips
->ips
[i
].addr
)) {
1308 /* yes, this node can serve this public ip */
1316 static bool can_node_takeover_ip(struct ctdb_context
*ctdb
, int32_t pnn
,
1317 struct ctdb_ipflags ipflags
,
1318 struct ctdb_public_ip_list
*ip
)
1320 if (ipflags
.noiptakeover
) {
1324 return can_node_host_ip(ctdb
, pnn
, ipflags
, ip
);
1327 /* search the node lists list for a node to takeover this ip.
1328 pick the node that currently are serving the least number of ips
1329 so that the ips get spread out evenly.
1331 static int find_takeover_node(struct ctdb_context
*ctdb
,
1332 struct ctdb_ipflags
*ipflags
,
1333 struct ctdb_public_ip_list
*ip
,
1334 struct ctdb_public_ip_list
*all_ips
)
1336 int pnn
, min
=0, num
;
1339 numnodes
= talloc_array_length(ipflags
);
1341 for (i
=0; i
<numnodes
; i
++) {
1342 /* verify that this node can serve this ip */
1343 if (!can_node_takeover_ip(ctdb
, i
, ipflags
[i
], ip
)) {
1344 /* no it couldnt so skip to the next node */
1348 num
= node_ip_coverage(ctdb
, i
, all_ips
);
1349 /* was this the first node we checked ? */
1361 DEBUG(DEBUG_WARNING
,(__location__
" Could not find node to take over public address '%s'\n",
1362 ctdb_addr_to_str(&ip
->addr
)));
1372 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
1374 static uint32_t key
[IP_KEYLEN
];
1376 bzero(key
, sizeof(key
));
1378 switch (ip
->sa
.sa_family
) {
1380 key
[3] = htonl(ip
->ip
.sin_addr
.s_addr
);
1383 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
1384 key
[0] = htonl(s6_a32
[0]);
1385 key
[1] = htonl(s6_a32
[1]);
1386 key
[2] = htonl(s6_a32
[2]);
1387 key
[3] = htonl(s6_a32
[3]);
1391 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
1398 static void *add_ip_callback(void *parm
, void *data
)
1400 struct ctdb_public_ip_list
*this_ip
= parm
;
1401 struct ctdb_public_ip_list
*prev_ip
= data
;
1403 if (prev_ip
== NULL
) {
1406 if (this_ip
->pnn
== -1) {
1407 this_ip
->pnn
= prev_ip
->pnn
;
1413 static int getips_count_callback(void *param
, void *data
)
1415 struct ctdb_public_ip_list
**ip_list
= (struct ctdb_public_ip_list
**)param
;
1416 struct ctdb_public_ip_list
*new_ip
= (struct ctdb_public_ip_list
*)data
;
1418 new_ip
->next
= *ip_list
;
1423 static struct ctdb_public_ip_list
*
1424 create_merged_ip_list(struct ctdb_context
*ctdb
)
1427 struct ctdb_public_ip_list
*ip_list
;
1428 struct ctdb_all_public_ips
*public_ips
;
1430 if (ctdb
->ip_tree
!= NULL
) {
1431 talloc_free(ctdb
->ip_tree
);
1432 ctdb
->ip_tree
= NULL
;
1434 ctdb
->ip_tree
= trbt_create(ctdb
, 0);
1436 for (i
=0;i
<ctdb
->num_nodes
;i
++) {
1437 public_ips
= ctdb
->nodes
[i
]->known_public_ips
;
1439 if (ctdb
->nodes
[i
]->flags
& NODE_FLAGS_DELETED
) {
1443 /* there were no public ips for this node */
1444 if (public_ips
== NULL
) {
1448 for (j
=0;j
<public_ips
->num
;j
++) {
1449 struct ctdb_public_ip_list
*tmp_ip
;
1451 tmp_ip
= talloc_zero(ctdb
->ip_tree
, struct ctdb_public_ip_list
);
1452 CTDB_NO_MEMORY_NULL(ctdb
, tmp_ip
);
1453 /* Do not use information about IP addresses hosted
1454 * on other nodes, it may not be accurate */
1455 if (public_ips
->ips
[j
].pnn
== ctdb
->nodes
[i
]->pnn
) {
1456 tmp_ip
->pnn
= public_ips
->ips
[j
].pnn
;
1460 tmp_ip
->addr
= public_ips
->ips
[j
].addr
;
1461 tmp_ip
->next
= NULL
;
1463 trbt_insertarray32_callback(ctdb
->ip_tree
,
1464 IP_KEYLEN
, ip_key(&public_ips
->ips
[j
].addr
),
1471 trbt_traversearray32(ctdb
->ip_tree
, IP_KEYLEN
, getips_count_callback
, &ip_list
);
1477 * This is the length of the longtest common prefix between the IPs.
1478 * It is calculated by XOR-ing the 2 IPs together and counting the
1479 * number of leading zeroes. The implementation means that all
1480 * addresses end up being 128 bits long.
1482 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1483 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1484 * lots of nodes and IP addresses?
1486 static uint32_t ip_distance(ctdb_sock_addr
*ip1
, ctdb_sock_addr
*ip2
)
1488 uint32_t ip1_k
[IP_KEYLEN
];
1493 uint32_t distance
= 0;
1495 memcpy(ip1_k
, ip_key(ip1
), sizeof(ip1_k
));
1497 for (i
=0; i
<IP_KEYLEN
; i
++) {
1498 x
= ip1_k
[i
] ^ t
[i
];
1502 /* Count number of leading zeroes.
1503 * FIXME? This could be optimised...
1505 while ((x
& (1 << 31)) == 0) {
1515 /* Calculate the IP distance for the given IP relative to IPs on the
1516 given node. The ips argument is generally the all_ips variable
1517 used in the main part of the algorithm.
1519 static uint32_t ip_distance_2_sum(ctdb_sock_addr
*ip
,
1520 struct ctdb_public_ip_list
*ips
,
1523 struct ctdb_public_ip_list
*t
;
1528 for (t
=ips
; t
!= NULL
; t
=t
->next
) {
1529 if (t
->pnn
!= pnn
) {
1533 /* Optimisation: We never calculate the distance
1534 * between an address and itself. This allows us to
1535 * calculate the effect of removing an address from a
1536 * node by simply calculating the distance between
1537 * that address and all of the exitsing addresses.
1538 * Moreover, we assume that we're only ever dealing
1539 * with addresses from all_ips so we can identify an
1540 * address via a pointer rather than doing a more
1541 * expensive address comparison. */
1542 if (&(t
->addr
) == ip
) {
1546 d
= ip_distance(ip
, &(t
->addr
));
1547 sum
+= d
* d
; /* Cheaper than pulling in math.h :-) */
1553 /* Return the LCP2 imbalance metric for addresses currently assigned
1556 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list
* all_ips
, int pnn
)
1558 struct ctdb_public_ip_list
*t
;
1560 uint32_t imbalance
= 0;
1562 for (t
=all_ips
; t
!=NULL
; t
=t
->next
) {
1563 if (t
->pnn
!= pnn
) {
1566 /* Pass the rest of the IPs rather than the whole
1569 imbalance
+= ip_distance_2_sum(&(t
->addr
), t
->next
, pnn
);
1575 /* Allocate any unassigned IPs just by looping through the IPs and
1576 * finding the best node for each.
1578 static void basic_allocate_unassigned(struct ctdb_context
*ctdb
,
1579 struct ctdb_ipflags
*ipflags
,
1580 struct ctdb_public_ip_list
*all_ips
)
1582 struct ctdb_public_ip_list
*tmp_ip
;
1584 /* loop over all ip's and find a physical node to cover for
1587 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1588 if (tmp_ip
->pnn
== -1) {
1589 if (find_takeover_node(ctdb
, ipflags
, tmp_ip
, all_ips
)) {
1590 DEBUG(DEBUG_WARNING
,("Failed to find node to cover ip %s\n",
1591 ctdb_addr_to_str(&tmp_ip
->addr
)));
1597 /* Basic non-deterministic rebalancing algorithm.
1599 static void basic_failback(struct ctdb_context
*ctdb
,
1600 struct ctdb_ipflags
*ipflags
,
1601 struct ctdb_public_ip_list
*all_ips
,
1605 int maxnode
, maxnum
, minnode
, minnum
, num
, retries
;
1606 struct ctdb_public_ip_list
*tmp_ip
;
1608 numnodes
= talloc_array_length(ipflags
);
1615 /* for each ip address, loop over all nodes that can serve
1616 this ip and make sure that the difference between the node
1617 serving the most and the node serving the least ip's are
1620 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1621 if (tmp_ip
->pnn
== -1) {
1625 /* Get the highest and lowest number of ips's served by any
1626 valid node which can serve this ip.
1630 for (i
=0; i
<numnodes
; i
++) {
1631 /* only check nodes that can actually serve this ip */
1632 if (!can_node_takeover_ip(ctdb
, i
, ipflags
[i
], tmp_ip
)) {
1633 /* no it couldnt so skip to the next node */
1637 num
= node_ip_coverage(ctdb
, i
, all_ips
);
1638 if (maxnode
== -1) {
1647 if (minnode
== -1) {
1657 if (maxnode
== -1) {
1658 DEBUG(DEBUG_WARNING
,(__location__
" Could not find maxnode. May not be able to serve ip '%s'\n",
1659 ctdb_addr_to_str(&tmp_ip
->addr
)));
1664 /* if the spread between the smallest and largest coverage by
1665 a node is >=2 we steal one of the ips from the node with
1666 most coverage to even things out a bit.
1667 try to do this a limited number of times since we dont
1668 want to spend too much time balancing the ip coverage.
1670 if ( (maxnum
> minnum
+1)
1671 && (retries
< (num_ips
+ 5)) ){
1672 struct ctdb_public_ip_list
*tmp
;
1674 /* Reassign one of maxnode's VNNs */
1675 for (tmp
=all_ips
;tmp
;tmp
=tmp
->next
) {
1676 if (tmp
->pnn
== maxnode
) {
1677 (void)find_takeover_node(ctdb
, ipflags
, tmp
, all_ips
);
1686 static void lcp2_init(struct ctdb_context
*tmp_ctx
,
1687 struct ctdb_ipflags
*ipflags
,
1688 struct ctdb_public_ip_list
*all_ips
,
1689 uint32_t *force_rebalance_nodes
,
1690 uint32_t **lcp2_imbalances
,
1691 bool **rebalance_candidates
)
1694 struct ctdb_public_ip_list
*tmp_ip
;
1696 numnodes
= talloc_array_length(ipflags
);
1698 *rebalance_candidates
= talloc_array(tmp_ctx
, bool, numnodes
);
1699 CTDB_NO_MEMORY_FATAL(tmp_ctx
, *rebalance_candidates
);
1700 *lcp2_imbalances
= talloc_array(tmp_ctx
, uint32_t, numnodes
);
1701 CTDB_NO_MEMORY_FATAL(tmp_ctx
, *lcp2_imbalances
);
1703 for (i
=0; i
<numnodes
; i
++) {
1704 (*lcp2_imbalances
)[i
] = lcp2_imbalance(all_ips
, i
);
1705 /* First step: assume all nodes are candidates */
1706 (*rebalance_candidates
)[i
] = true;
1709 /* 2nd step: if a node has IPs assigned then it must have been
1710 * healthy before, so we remove it from consideration. This
1711 * is overkill but is all we have because we don't maintain
1712 * state between takeover runs. An alternative would be to
1713 * keep state and invalidate it every time the recovery master
1716 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1717 if (tmp_ip
->pnn
!= -1) {
1718 (*rebalance_candidates
)[tmp_ip
->pnn
] = false;
1722 /* 3rd step: if a node is forced to re-balance then
1723 we allow failback onto the node */
1724 if (force_rebalance_nodes
== NULL
) {
1727 for (i
= 0; i
< talloc_array_length(force_rebalance_nodes
); i
++) {
1728 uint32_t pnn
= force_rebalance_nodes
[i
];
1729 if (pnn
>= numnodes
) {
1731 (__location__
"unknown node %u\n", pnn
));
1736 ("Forcing rebalancing of IPs to node %u\n", pnn
));
1737 (*rebalance_candidates
)[pnn
] = true;
1741 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1742 * the IP/node combination that will cost the least.
1744 static void lcp2_allocate_unassigned(struct ctdb_context
*ctdb
,
1745 struct ctdb_ipflags
*ipflags
,
1746 struct ctdb_public_ip_list
*all_ips
,
1747 uint32_t *lcp2_imbalances
)
1749 struct ctdb_public_ip_list
*tmp_ip
;
1750 int dstnode
, numnodes
;
1753 uint32_t mindsum
, dstdsum
, dstimbl
, minimbl
;
1754 struct ctdb_public_ip_list
*minip
;
1756 bool should_loop
= true;
1757 bool have_unassigned
= true;
1759 numnodes
= talloc_array_length(ipflags
);
1761 while (have_unassigned
&& should_loop
) {
1762 should_loop
= false;
1764 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1765 DEBUG(DEBUG_DEBUG
,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1771 /* loop over each unassigned ip. */
1772 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1773 if (tmp_ip
->pnn
!= -1) {
1777 for (dstnode
=0; dstnode
<numnodes
; dstnode
++) {
1778 /* only check nodes that can actually takeover this ip */
1779 if (!can_node_takeover_ip(ctdb
, dstnode
,
1782 /* no it couldnt so skip to the next node */
1786 dstdsum
= ip_distance_2_sum(&(tmp_ip
->addr
), all_ips
, dstnode
);
1787 dstimbl
= lcp2_imbalances
[dstnode
] + dstdsum
;
1788 DEBUG(DEBUG_DEBUG
,(" %s -> %d [+%d]\n",
1789 ctdb_addr_to_str(&(tmp_ip
->addr
)),
1791 dstimbl
- lcp2_imbalances
[dstnode
]));
1794 if ((minnode
== -1) || (dstdsum
< mindsum
)) {
1804 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1806 /* If we found one then assign it to the given node. */
1807 if (minnode
!= -1) {
1808 minip
->pnn
= minnode
;
1809 lcp2_imbalances
[minnode
] = minimbl
;
1810 DEBUG(DEBUG_INFO
,(" %s -> %d [+%d]\n",
1811 ctdb_addr_to_str(&(minip
->addr
)),
1816 /* There might be a better way but at least this is clear. */
1817 have_unassigned
= false;
1818 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1819 if (tmp_ip
->pnn
== -1) {
1820 have_unassigned
= true;
1825 /* We know if we have an unassigned addresses so we might as
1828 if (have_unassigned
) {
1829 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1830 if (tmp_ip
->pnn
== -1) {
1831 DEBUG(DEBUG_WARNING
,("Failed to find node to cover ip %s\n",
1832 ctdb_addr_to_str(&tmp_ip
->addr
)));
1838 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1839 * to move IPs from, determines the best IP/destination node
1840 * combination to move from the source node.
1842 static bool lcp2_failback_candidate(struct ctdb_context
*ctdb
,
1843 struct ctdb_ipflags
*ipflags
,
1844 struct ctdb_public_ip_list
*all_ips
,
1846 uint32_t *lcp2_imbalances
,
1847 bool *rebalance_candidates
)
1849 int dstnode
, mindstnode
, numnodes
;
1850 uint32_t srcimbl
, srcdsum
, dstimbl
, dstdsum
;
1851 uint32_t minsrcimbl
, mindstimbl
;
1852 struct ctdb_public_ip_list
*minip
;
1853 struct ctdb_public_ip_list
*tmp_ip
;
1855 /* Find an IP and destination node that best reduces imbalance. */
1862 numnodes
= talloc_array_length(ipflags
);
1864 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1865 DEBUG(DEBUG_DEBUG
,(" CONSIDERING MOVES FROM %d [%d]\n",
1866 srcnode
, lcp2_imbalances
[srcnode
]));
1868 for (tmp_ip
=all_ips
; tmp_ip
; tmp_ip
=tmp_ip
->next
) {
1869 /* Only consider addresses on srcnode. */
1870 if (tmp_ip
->pnn
!= srcnode
) {
1874 /* What is this IP address costing the source node? */
1875 srcdsum
= ip_distance_2_sum(&(tmp_ip
->addr
), all_ips
, srcnode
);
1876 srcimbl
= lcp2_imbalances
[srcnode
] - srcdsum
;
1878 /* Consider this IP address would cost each potential
1879 * destination node. Destination nodes are limited to
1880 * those that are newly healthy, since we don't want
1881 * to do gratuitous failover of IPs just to make minor
1882 * balance improvements.
1884 for (dstnode
=0; dstnode
<numnodes
; dstnode
++) {
1885 if (!rebalance_candidates
[dstnode
]) {
1889 /* only check nodes that can actually takeover this ip */
1890 if (!can_node_takeover_ip(ctdb
, dstnode
,
1891 ipflags
[dstnode
], tmp_ip
)) {
1892 /* no it couldnt so skip to the next node */
1896 dstdsum
= ip_distance_2_sum(&(tmp_ip
->addr
), all_ips
, dstnode
);
1897 dstimbl
= lcp2_imbalances
[dstnode
] + dstdsum
;
1898 DEBUG(DEBUG_DEBUG
,(" %d [%d] -> %s -> %d [+%d]\n",
1900 ctdb_addr_to_str(&(tmp_ip
->addr
)),
1903 if ((dstimbl
< lcp2_imbalances
[srcnode
]) &&
1904 (dstdsum
< srcdsum
) && \
1905 ((mindstnode
== -1) || \
1906 ((srcimbl
+ dstimbl
) < (minsrcimbl
+ mindstimbl
)))) {
1909 minsrcimbl
= srcimbl
;
1910 mindstnode
= dstnode
;
1911 mindstimbl
= dstimbl
;
1915 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1917 if (mindstnode
!= -1) {
1918 /* We found a move that makes things better... */
1919 DEBUG(DEBUG_INFO
,("%d [%d] -> %s -> %d [+%d]\n",
1920 srcnode
, minsrcimbl
- lcp2_imbalances
[srcnode
],
1921 ctdb_addr_to_str(&(minip
->addr
)),
1922 mindstnode
, mindstimbl
- lcp2_imbalances
[mindstnode
]));
1925 lcp2_imbalances
[srcnode
] = minsrcimbl
;
1926 lcp2_imbalances
[mindstnode
] = mindstimbl
;
1927 minip
->pnn
= mindstnode
;
1936 struct lcp2_imbalance_pnn
{
1941 static int lcp2_cmp_imbalance_pnn(const void * a
, const void * b
)
1943 const struct lcp2_imbalance_pnn
* lipa
= (const struct lcp2_imbalance_pnn
*) a
;
1944 const struct lcp2_imbalance_pnn
* lipb
= (const struct lcp2_imbalance_pnn
*) b
;
1946 if (lipa
->imbalance
> lipb
->imbalance
) {
1948 } else if (lipa
->imbalance
== lipb
->imbalance
) {
1955 /* LCP2 algorithm for rebalancing the cluster. This finds the source
1956 * node with the highest LCP2 imbalance, and then determines the best
1957 * IP/destination node combination to move from the source node.
1959 static void lcp2_failback(struct ctdb_context
*ctdb
,
1960 struct ctdb_ipflags
*ipflags
,
1961 struct ctdb_public_ip_list
*all_ips
,
1962 uint32_t *lcp2_imbalances
,
1963 bool *rebalance_candidates
)
1966 struct lcp2_imbalance_pnn
* lips
;
1969 numnodes
= talloc_array_length(ipflags
);
1972 /* Put the imbalances and nodes into an array, sort them and
1973 * iterate through candidates. Usually the 1st one will be
1974 * used, so this doesn't cost much...
1976 DEBUG(DEBUG_DEBUG
,("+++++++++++++++++++++++++++++++++++++++++\n"));
1977 DEBUG(DEBUG_DEBUG
,("Selecting most imbalanced node from:\n"));
1978 lips
= talloc_array(ctdb
, struct lcp2_imbalance_pnn
, numnodes
);
1979 for (i
=0; i
<numnodes
; i
++) {
1980 lips
[i
].imbalance
= lcp2_imbalances
[i
];
1982 DEBUG(DEBUG_DEBUG
,(" %d [%d]\n", i
, lcp2_imbalances
[i
]));
1984 qsort(lips
, numnodes
, sizeof(struct lcp2_imbalance_pnn
),
1985 lcp2_cmp_imbalance_pnn
);
1988 for (i
=0; i
<numnodes
; i
++) {
1989 /* This means that all nodes had 0 or 1 addresses, so
1990 * can't be imbalanced.
1992 if (lips
[i
].imbalance
== 0) {
1996 if (lcp2_failback_candidate(ctdb
,
2001 rebalance_candidates
)) {
2013 static void unassign_unsuitable_ips(struct ctdb_context
*ctdb
,
2014 struct ctdb_ipflags
*ipflags
,
2015 struct ctdb_public_ip_list
*all_ips
)
2017 struct ctdb_public_ip_list
*tmp_ip
;
2019 /* verify that the assigned nodes can serve that public ip
2020 and set it to -1 if not
2022 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2023 if (tmp_ip
->pnn
== -1) {
2026 if (!can_node_host_ip(ctdb
, tmp_ip
->pnn
,
2027 ipflags
[tmp_ip
->pnn
], tmp_ip
) != 0) {
2028 /* this node can not serve this ip. */
2029 DEBUG(DEBUG_DEBUG
,("Unassign IP: %s from %d\n",
2030 ctdb_addr_to_str(&(tmp_ip
->addr
)),
2037 static void ip_alloc_deterministic_ips(struct ctdb_context
*ctdb
,
2038 struct ctdb_ipflags
*ipflags
,
2039 struct ctdb_public_ip_list
*all_ips
)
2041 struct ctdb_public_ip_list
*tmp_ip
;
2044 numnodes
= talloc_array_length(ipflags
);
2046 DEBUG(DEBUG_NOTICE
,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2047 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2048 * always be allocated the same way for a specific set of
2049 * available/unavailable nodes.
2052 for (i
=0,tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
,i
++) {
2053 tmp_ip
->pnn
= i
% numnodes
;
2056 /* IP failback doesn't make sense with deterministic
2057 * IPs, since the modulo step above implicitly fails
2058 * back IPs to their "home" node.
2060 if (1 == ctdb
->tunable
.no_ip_failback
) {
2061 DEBUG(DEBUG_WARNING
, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2064 unassign_unsuitable_ips(ctdb
, ipflags
, all_ips
);
2066 basic_allocate_unassigned(ctdb
, ipflags
, all_ips
);
2068 /* No failback here! */
2071 static void ip_alloc_nondeterministic_ips(struct ctdb_context
*ctdb
,
2072 struct ctdb_ipflags
*ipflags
,
2073 struct ctdb_public_ip_list
*all_ips
)
2075 /* This should be pushed down into basic_failback. */
2076 struct ctdb_public_ip_list
*tmp_ip
;
2078 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2082 unassign_unsuitable_ips(ctdb
, ipflags
, all_ips
);
2084 basic_allocate_unassigned(ctdb
, ipflags
, all_ips
);
2086 /* If we don't want IPs to fail back then don't rebalance IPs. */
2087 if (1 == ctdb
->tunable
.no_ip_failback
) {
2091 /* Now, try to make sure the ip adresses are evenly distributed
2094 basic_failback(ctdb
, ipflags
, all_ips
, num_ips
);
2097 static void ip_alloc_lcp2(struct ctdb_context
*ctdb
,
2098 struct ctdb_ipflags
*ipflags
,
2099 struct ctdb_public_ip_list
*all_ips
,
2100 uint32_t *force_rebalance_nodes
)
2102 uint32_t *lcp2_imbalances
;
2103 bool *rebalance_candidates
;
2104 int numnodes
, num_rebalance_candidates
, i
;
2106 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2108 unassign_unsuitable_ips(ctdb
, ipflags
, all_ips
);
2110 lcp2_init(tmp_ctx
, ipflags
, all_ips
,force_rebalance_nodes
,
2111 &lcp2_imbalances
, &rebalance_candidates
);
2113 lcp2_allocate_unassigned(ctdb
, ipflags
, all_ips
, lcp2_imbalances
);
2115 /* If we don't want IPs to fail back then don't rebalance IPs. */
2116 if (1 == ctdb
->tunable
.no_ip_failback
) {
2120 /* It is only worth continuing if we have suitable target
2121 * nodes to transfer IPs to. This check is much cheaper than
2124 numnodes
= talloc_array_length(ipflags
);
2125 num_rebalance_candidates
= 0;
2126 for (i
=0; i
<numnodes
; i
++) {
2127 if (rebalance_candidates
[i
]) {
2128 num_rebalance_candidates
++;
2131 if (num_rebalance_candidates
== 0) {
2135 /* Now, try to make sure the ip adresses are evenly distributed
2138 lcp2_failback(ctdb
, ipflags
, all_ips
,
2139 lcp2_imbalances
, rebalance_candidates
);
2142 talloc_free(tmp_ctx
);
2145 static bool all_nodes_are_disabled(struct ctdb_node_map
*nodemap
)
2149 for (i
=0;i
<nodemap
->num
;i
++) {
2150 if (!(nodemap
->nodes
[i
].flags
& (NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
))) {
2151 /* Found one completely healthy node */
2159 /* The calculation part of the IP allocation algorithm. */
2160 static void ctdb_takeover_run_core(struct ctdb_context
*ctdb
,
2161 struct ctdb_ipflags
*ipflags
,
2162 struct ctdb_public_ip_list
**all_ips_p
,
2163 uint32_t *force_rebalance_nodes
)
2165 /* since nodes only know about those public addresses that
2166 can be served by that particular node, no single node has
2167 a full list of all public addresses that exist in the cluster.
2168 Walk over all node structures and create a merged list of
2169 all public addresses that exist in the cluster.
2171 keep the tree of ips around as ctdb->ip_tree
2173 *all_ips_p
= create_merged_ip_list(ctdb
);
2175 if (1 == ctdb
->tunable
.lcp2_public_ip_assignment
) {
2176 ip_alloc_lcp2(ctdb
, ipflags
, *all_ips_p
, force_rebalance_nodes
);
2177 } else if (1 == ctdb
->tunable
.deterministic_public_ips
) {
2178 ip_alloc_deterministic_ips(ctdb
, ipflags
, *all_ips_p
);
2180 ip_alloc_nondeterministic_ips(ctdb
, ipflags
, *all_ips_p
);
2183 /* at this point ->pnn is the node which will own each IP
2184 or -1 if there is no node that can cover this ip
2190 struct get_tunable_callback_data
{
2191 const char *tunable
;
2196 static void get_tunable_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2197 int32_t res
, TDB_DATA outdata
,
2200 struct get_tunable_callback_data
*cd
=
2201 (struct get_tunable_callback_data
*)callback
;
2205 /* Already handled in fail callback */
2209 if (outdata
.dsize
!= sizeof(uint32_t)) {
2210 DEBUG(DEBUG_ERR
,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2211 cd
->tunable
, pnn
, (int)sizeof(uint32_t),
2212 (int)outdata
.dsize
));
2217 size
= talloc_array_length(cd
->out
);
2219 DEBUG(DEBUG_ERR
,("Got %s reply from node %d but nodemap only has %d entries\n",
2220 cd
->tunable
, pnn
, size
));
2225 cd
->out
[pnn
] = *(uint32_t *)outdata
.dptr
;
2228 static void get_tunable_fail_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2229 int32_t res
, TDB_DATA outdata
,
2232 struct get_tunable_callback_data
*cd
=
2233 (struct get_tunable_callback_data
*)callback
;
2238 ("Timed out getting tunable \"%s\" from node %d\n",
2244 DEBUG(DEBUG_WARNING
,
2245 ("Tunable \"%s\" not implemented on node %d\n",
2250 ("Unexpected error getting tunable \"%s\" from node %d\n",
2256 static uint32_t *get_tunable_from_nodes(struct ctdb_context
*ctdb
,
2257 TALLOC_CTX
*tmp_ctx
,
2258 struct ctdb_node_map
*nodemap
,
2259 const char *tunable
,
2260 uint32_t default_value
)
2263 struct ctdb_control_get_tunable
*t
;
2266 struct get_tunable_callback_data callback_data
;
2269 tvals
= talloc_array(tmp_ctx
, uint32_t, nodemap
->num
);
2270 CTDB_NO_MEMORY_NULL(ctdb
, tvals
);
2271 for (i
=0; i
<nodemap
->num
; i
++) {
2272 tvals
[i
] = default_value
;
2275 callback_data
.out
= tvals
;
2276 callback_data
.tunable
= tunable
;
2277 callback_data
.fatal
= false;
2279 data
.dsize
= offsetof(struct ctdb_control_get_tunable
, name
) + strlen(tunable
) + 1;
2280 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
2281 t
= (struct ctdb_control_get_tunable
*)data
.dptr
;
2282 t
->length
= strlen(tunable
)+1;
2283 memcpy(t
->name
, tunable
, t
->length
);
2284 nodes
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2285 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_GET_TUNABLE
,
2286 nodes
, 0, TAKEOVER_TIMEOUT(),
2288 get_tunable_callback
,
2289 get_tunable_fail_callback
,
2290 &callback_data
) != 0) {
2291 if (callback_data
.fatal
) {
2297 talloc_free(data
.dptr
);
2302 struct get_runstate_callback_data
{
2303 enum ctdb_runstate
*out
;
2307 static void get_runstate_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2308 int32_t res
, TDB_DATA outdata
,
2309 void *callback_data
)
2311 struct get_runstate_callback_data
*cd
=
2312 (struct get_runstate_callback_data
*)callback_data
;
2316 /* Already handled in fail callback */
2320 if (outdata
.dsize
!= sizeof(uint32_t)) {
2321 DEBUG(DEBUG_ERR
,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2322 pnn
, (int)sizeof(uint32_t),
2323 (int)outdata
.dsize
));
2328 size
= talloc_array_length(cd
->out
);
2330 DEBUG(DEBUG_ERR
,("Got reply from node %d but nodemap only has %d entries\n",
2335 cd
->out
[pnn
] = (enum ctdb_runstate
)*(uint32_t *)outdata
.dptr
;
2338 static void get_runstate_fail_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2339 int32_t res
, TDB_DATA outdata
,
2342 struct get_runstate_callback_data
*cd
=
2343 (struct get_runstate_callback_data
*)callback
;
2348 ("Timed out getting runstate from node %d\n", pnn
));
2352 DEBUG(DEBUG_WARNING
,
2353 ("Error getting runstate from node %d - assuming runstates not supported\n",
2358 static enum ctdb_runstate
* get_runstate_from_nodes(struct ctdb_context
*ctdb
,
2359 TALLOC_CTX
*tmp_ctx
,
2360 struct ctdb_node_map
*nodemap
,
2361 enum ctdb_runstate default_value
)
2364 enum ctdb_runstate
*rs
;
2365 struct get_runstate_callback_data callback_data
;
2368 rs
= talloc_array(tmp_ctx
, enum ctdb_runstate
, nodemap
->num
);
2369 CTDB_NO_MEMORY_NULL(ctdb
, rs
);
2370 for (i
=0; i
<nodemap
->num
; i
++) {
2371 rs
[i
] = default_value
;
2374 callback_data
.out
= rs
;
2375 callback_data
.fatal
= false;
2377 nodes
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2378 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_GET_RUNSTATE
,
2379 nodes
, 0, TAKEOVER_TIMEOUT(),
2381 get_runstate_callback
,
2382 get_runstate_fail_callback
,
2383 &callback_data
) != 0) {
2384 if (callback_data
.fatal
) {
2394 /* Set internal flags for IP allocation:
2396 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2397 * Set NOIPHOST ip flag for each INACTIVE node
2398 * if all nodes are disabled:
2399 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2401 * Set NOIPHOST ip flags for disabled nodes
2403 static struct ctdb_ipflags
*
2404 set_ipflags_internal(struct ctdb_context
*ctdb
,
2405 TALLOC_CTX
*tmp_ctx
,
2406 struct ctdb_node_map
*nodemap
,
2407 uint32_t *tval_noiptakeover
,
2408 uint32_t *tval_noiphostonalldisabled
,
2409 enum ctdb_runstate
*runstate
)
2412 struct ctdb_ipflags
*ipflags
;
2414 /* Clear IP flags - implicit due to talloc_zero */
2415 ipflags
= talloc_zero_array(tmp_ctx
, struct ctdb_ipflags
, nodemap
->num
);
2416 CTDB_NO_MEMORY_NULL(ctdb
, ipflags
);
2418 for (i
=0;i
<nodemap
->num
;i
++) {
2419 /* Can not take IPs on node with NoIPTakeover set */
2420 if (tval_noiptakeover
[i
] != 0) {
2421 ipflags
[i
].noiptakeover
= true;
2424 /* Can not host IPs on node not in RUNNING state */
2425 if (runstate
[i
] != CTDB_RUNSTATE_RUNNING
) {
2426 ipflags
[i
].noiphost
= true;
2429 /* Can not host IPs on INACTIVE node */
2430 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2431 ipflags
[i
].noiphost
= true;
2433 /* Remember the runstate */
2434 ipflags
[i
].runstate
= runstate
[i
];
2437 if (all_nodes_are_disabled(nodemap
)) {
2438 /* If all nodes are disabled, can not host IPs on node
2439 * with NoIPHostOnAllDisabled set
2441 for (i
=0;i
<nodemap
->num
;i
++) {
2442 if (tval_noiphostonalldisabled
[i
] != 0) {
2443 ipflags
[i
].noiphost
= true;
2447 /* If some nodes are not disabled, then can not host
2448 * IPs on DISABLED node
2450 for (i
=0;i
<nodemap
->num
;i
++) {
2451 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISABLED
) {
2452 ipflags
[i
].noiphost
= true;
2460 static struct ctdb_ipflags
*set_ipflags(struct ctdb_context
*ctdb
,
2461 TALLOC_CTX
*tmp_ctx
,
2462 struct ctdb_node_map
*nodemap
)
2464 uint32_t *tval_noiptakeover
;
2465 uint32_t *tval_noiphostonalldisabled
;
2466 struct ctdb_ipflags
*ipflags
;
2467 enum ctdb_runstate
*runstate
;
2470 tval_noiptakeover
= get_tunable_from_nodes(ctdb
, tmp_ctx
, nodemap
,
2472 if (tval_noiptakeover
== NULL
) {
2476 tval_noiphostonalldisabled
=
2477 get_tunable_from_nodes(ctdb
, tmp_ctx
, nodemap
,
2478 "NoIPHostOnAllDisabled", 0);
2479 if (tval_noiphostonalldisabled
== NULL
) {
2480 /* Caller frees tmp_ctx */
2484 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2485 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2486 * reasonable behaviour on a mixed cluster during upgrade.
2488 runstate
= get_runstate_from_nodes(ctdb
, tmp_ctx
, nodemap
,
2489 CTDB_RUNSTATE_RUNNING
);
2490 if (runstate
== NULL
) {
2491 /* Caller frees tmp_ctx */
2495 ipflags
= set_ipflags_internal(ctdb
, tmp_ctx
, nodemap
,
2497 tval_noiphostonalldisabled
,
2500 talloc_free(tval_noiptakeover
);
2501 talloc_free(tval_noiphostonalldisabled
);
2502 talloc_free(runstate
);
2507 struct iprealloc_callback_data
{
2510 client_async_callback fail_callback
;
2511 void *fail_callback_data
;
2512 struct ctdb_node_map
*nodemap
;
2515 static void iprealloc_fail_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2516 int32_t res
, TDB_DATA outdata
,
2520 struct iprealloc_callback_data
*cd
=
2521 (struct iprealloc_callback_data
*)callback
;
2523 numnodes
= talloc_array_length(cd
->retry_nodes
);
2524 if (pnn
> numnodes
) {
2526 ("ipreallocated failure from node %d, "
2527 "but only %d nodes in nodemap\n",
2532 /* Can't run the "ipreallocated" event on a INACTIVE node */
2533 if (cd
->nodemap
->nodes
[pnn
].flags
& NODE_FLAGS_INACTIVE
) {
2534 DEBUG(DEBUG_WARNING
,
2535 ("ipreallocated failed on inactive node %d, ignoring\n",
2542 /* If the control timed out then that's a real error,
2543 * so call the real fail callback
2545 if (cd
->fail_callback
) {
2546 cd
->fail_callback(ctdb
, pnn
, res
, outdata
,
2547 cd
->fail_callback_data
);
2549 DEBUG(DEBUG_WARNING
,
2550 ("iprealloc timed out but no callback registered\n"));
2554 /* If not a timeout then either the ipreallocated
2555 * eventscript (or some setup) failed. This might
2556 * have failed because the IPREALLOCATED control isn't
2557 * implemented - right now there is no way of knowing
2558 * because the error codes are all folded down to -1.
2559 * Consider retrying using EVENTSCRIPT control...
2561 DEBUG(DEBUG_WARNING
,
2562 ("ipreallocated failure from node %d, flagging retry\n",
2564 cd
->retry_nodes
[pnn
] = true;
2569 struct takeover_callback_data
{
2571 client_async_callback fail_callback
;
2572 void *fail_callback_data
;
2573 struct ctdb_node_map
*nodemap
;
2576 static void takeover_run_fail_callback(struct ctdb_context
*ctdb
,
2577 uint32_t node_pnn
, int32_t res
,
2578 TDB_DATA outdata
, void *callback_data
)
2580 struct takeover_callback_data
*cd
=
2581 talloc_get_type_abort(callback_data
,
2582 struct takeover_callback_data
);
2585 for (i
= 0; i
< cd
->nodemap
->num
; i
++) {
2586 if (node_pnn
== cd
->nodemap
->nodes
[i
].pnn
) {
2591 if (i
== cd
->nodemap
->num
) {
2592 DEBUG(DEBUG_ERR
, (__location__
" invalid PNN %u\n", node_pnn
));
2596 if (!cd
->node_failed
[i
]) {
2597 cd
->node_failed
[i
] = true;
2598 cd
->fail_callback(ctdb
, node_pnn
, res
, outdata
,
2599 cd
->fail_callback_data
);
2604 make any IP alias changes for public addresses that are necessary
2606 int ctdb_takeover_run(struct ctdb_context
*ctdb
, struct ctdb_node_map
*nodemap
,
2607 uint32_t *force_rebalance_nodes
,
2608 client_async_callback fail_callback
, void *callback_data
)
2611 struct ctdb_public_ip ip
;
2612 struct ctdb_public_ipv4 ipv4
;
2614 struct ctdb_public_ip_list
*all_ips
, *tmp_ip
;
2616 struct timeval timeout
;
2617 struct client_async_data
*async_data
;
2618 struct ctdb_client_control_state
*state
;
2619 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2620 struct ctdb_ipflags
*ipflags
;
2621 struct takeover_callback_data
*takeover_data
;
2622 struct iprealloc_callback_data iprealloc_data
;
2627 * ip failover is completely disabled, just send out the
2628 * ipreallocated event.
2630 if (ctdb
->tunable
.disable_ip_failover
!= 0) {
2634 ipflags
= set_ipflags(ctdb
, tmp_ctx
, nodemap
);
2635 if (ipflags
== NULL
) {
2636 DEBUG(DEBUG_ERR
,("Failed to set IP flags - aborting takeover run\n"));
2637 talloc_free(tmp_ctx
);
2641 /* Short-circuit IP allocation if no nodes are in the RUNNING
2642 * runstate yet, since no nodes will be able to host IPs */
2643 can_host_ips
= false;
2644 for (i
=0; i
<nodemap
->num
; i
++) {
2645 if (ipflags
[i
].runstate
== CTDB_RUNSTATE_RUNNING
) {
2646 can_host_ips
= true;
2649 if (!can_host_ips
) {
2650 DEBUG(DEBUG_WARNING
,("No nodes available to host public IPs yet\n"));
2654 /* Do the IP reassignment calculations */
2655 ctdb_takeover_run_core(ctdb
, ipflags
, &all_ips
, force_rebalance_nodes
);
2657 /* Now tell all nodes to release any public IPs should not
2658 * host. This will be a NOOP on nodes that don't currently
2659 * hold the given IP.
2661 takeover_data
= talloc_zero(tmp_ctx
, struct takeover_callback_data
);
2662 CTDB_NO_MEMORY_FATAL(ctdb
, takeover_data
);
2664 takeover_data
->node_failed
= talloc_zero_array(tmp_ctx
,
2665 bool, nodemap
->num
);
2666 CTDB_NO_MEMORY_FATAL(ctdb
, takeover_data
->node_failed
);
2667 takeover_data
->fail_callback
= fail_callback
;
2668 takeover_data
->fail_callback_data
= callback_data
;
2669 takeover_data
->nodemap
= nodemap
;
2671 async_data
= talloc_zero(tmp_ctx
, struct client_async_data
);
2672 CTDB_NO_MEMORY_FATAL(ctdb
, async_data
);
2674 async_data
->fail_callback
= takeover_run_fail_callback
;
2675 async_data
->callback_data
= takeover_data
;
2677 ZERO_STRUCT(ip
); /* Avoid valgrind warnings for union */
2679 /* Send a RELEASE_IP to all nodes that should not be hosting
2680 * each IP. For each IP, all but one of these will be
2681 * redundant. However, the redundant ones are used to tell
2682 * nodes which node should be hosting the IP so that commands
2683 * like "ctdb ip" can display a particular nodes idea of who
2684 * is hosting what. */
2685 for (i
=0;i
<nodemap
->num
;i
++) {
2686 /* don't talk to unconnected nodes, but do talk to banned nodes */
2687 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2691 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2692 if (tmp_ip
->pnn
== nodemap
->nodes
[i
].pnn
) {
2693 /* This node should be serving this
2694 vnn so dont tell it to release the ip
2698 if (tmp_ip
->addr
.sa
.sa_family
== AF_INET
) {
2699 ipv4
.pnn
= tmp_ip
->pnn
;
2700 ipv4
.sin
= tmp_ip
->addr
.ip
;
2702 timeout
= TAKEOVER_TIMEOUT();
2703 data
.dsize
= sizeof(ipv4
);
2704 data
.dptr
= (uint8_t *)&ipv4
;
2705 state
= ctdb_control_send(ctdb
, nodemap
->nodes
[i
].pnn
,
2706 0, CTDB_CONTROL_RELEASE_IPv4
, 0,
2710 ip
.pnn
= tmp_ip
->pnn
;
2711 ip
.addr
= tmp_ip
->addr
;
2713 timeout
= TAKEOVER_TIMEOUT();
2714 data
.dsize
= sizeof(ip
);
2715 data
.dptr
= (uint8_t *)&ip
;
2716 state
= ctdb_control_send(ctdb
, nodemap
->nodes
[i
].pnn
,
2717 0, CTDB_CONTROL_RELEASE_IP
, 0,
2722 if (state
== NULL
) {
2723 DEBUG(DEBUG_ERR
,(__location__
" Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap
->nodes
[i
].pnn
));
2724 talloc_free(tmp_ctx
);
2728 ctdb_client_async_add(async_data
, state
);
2731 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2732 DEBUG(DEBUG_ERR
,(__location__
" Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2733 talloc_free(tmp_ctx
);
2736 talloc_free(async_data
);
2739 /* For each IP, send a TAKOVER_IP to the node that should be
2740 * hosting it. Many of these will often be redundant (since
2741 * the allocation won't have changed) but they can be useful
2742 * to recover from inconsistencies. */
2743 async_data
= talloc_zero(tmp_ctx
, struct client_async_data
);
2744 CTDB_NO_MEMORY_FATAL(ctdb
, async_data
);
2746 async_data
->fail_callback
= fail_callback
;
2747 async_data
->callback_data
= callback_data
;
2749 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2750 if (tmp_ip
->pnn
== -1) {
2751 /* this IP won't be taken over */
2755 if (tmp_ip
->addr
.sa
.sa_family
== AF_INET
) {
2756 ipv4
.pnn
= tmp_ip
->pnn
;
2757 ipv4
.sin
= tmp_ip
->addr
.ip
;
2759 timeout
= TAKEOVER_TIMEOUT();
2760 data
.dsize
= sizeof(ipv4
);
2761 data
.dptr
= (uint8_t *)&ipv4
;
2762 state
= ctdb_control_send(ctdb
, tmp_ip
->pnn
,
2763 0, CTDB_CONTROL_TAKEOVER_IPv4
, 0,
2767 ip
.pnn
= tmp_ip
->pnn
;
2768 ip
.addr
= tmp_ip
->addr
;
2770 timeout
= TAKEOVER_TIMEOUT();
2771 data
.dsize
= sizeof(ip
);
2772 data
.dptr
= (uint8_t *)&ip
;
2773 state
= ctdb_control_send(ctdb
, tmp_ip
->pnn
,
2774 0, CTDB_CONTROL_TAKEOVER_IP
, 0,
2778 if (state
== NULL
) {
2779 DEBUG(DEBUG_ERR
,(__location__
" Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip
->pnn
));
2780 talloc_free(tmp_ctx
);
2784 ctdb_client_async_add(async_data
, state
);
2786 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2787 DEBUG(DEBUG_ERR
,(__location__
" Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2788 talloc_free(tmp_ctx
);
2794 * Tell all nodes to run eventscripts to process the
2795 * "ipreallocated" event. This can do a lot of things,
2796 * including restarting services to reconfigure them if public
2797 * IPs have moved. Once upon a time this event only used to
2800 retry_data
= talloc_zero_array(tmp_ctx
, bool, nodemap
->num
);
2801 CTDB_NO_MEMORY_FATAL(ctdb
, retry_data
);
2802 iprealloc_data
.retry_nodes
= retry_data
;
2803 iprealloc_data
.retry_count
= 0;
2804 iprealloc_data
.fail_callback
= fail_callback
;
2805 iprealloc_data
.fail_callback_data
= callback_data
;
2806 iprealloc_data
.nodemap
= nodemap
;
2808 nodes
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2809 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_IPREALLOCATED
,
2810 nodes
, 0, TAKEOVER_TIMEOUT(),
2812 NULL
, iprealloc_fail_callback
,
2815 /* If the control failed then we should retry to any
2816 * nodes flagged by iprealloc_fail_callback using the
2817 * EVENTSCRIPT control. This is a best-effort at
2818 * backward compatiblity when running a mixed cluster
2819 * where some nodes have not yet been upgraded to
2820 * support the IPREALLOCATED control.
2822 DEBUG(DEBUG_WARNING
,
2823 ("Retry ipreallocated to some nodes using eventscript control\n"));
2825 nodes
= talloc_array(tmp_ctx
, uint32_t,
2826 iprealloc_data
.retry_count
);
2827 CTDB_NO_MEMORY_FATAL(ctdb
, nodes
);
2830 for (i
=0; i
<nodemap
->num
; i
++) {
2831 if (iprealloc_data
.retry_nodes
[i
]) {
2837 data
.dptr
= discard_const("ipreallocated");
2838 data
.dsize
= strlen((char *)data
.dptr
) + 1;
2839 ret
= ctdb_client_async_control(ctdb
,
2840 CTDB_CONTROL_RUN_EVENTSCRIPTS
,
2841 nodes
, 0, TAKEOVER_TIMEOUT(),
2843 NULL
, fail_callback
,
2846 DEBUG(DEBUG_ERR
, (__location__
" failed to send control to run eventscripts with \"ipreallocated\"\n"));
2850 talloc_free(tmp_ctx
);
2856 destroy a ctdb_client_ip structure
2858 static int ctdb_client_ip_destructor(struct ctdb_client_ip
*ip
)
2860 DEBUG(DEBUG_DEBUG
,("destroying client tcp for %s:%u (client_id %u)\n",
2861 ctdb_addr_to_str(&ip
->addr
),
2862 ntohs(ip
->addr
.ip
.sin_port
),
2865 DLIST_REMOVE(ip
->ctdb
->client_ip_list
, ip
);
2870 called by a client to inform us of a TCP connection that it is managing
2871 that should tickled with an ACK when IP takeover is done
2872 we handle both the old ipv4 style of packets as well as the new ipv4/6
2875 int32_t ctdb_control_tcp_client(struct ctdb_context
*ctdb
, uint32_t client_id
,
2878 struct ctdb_client
*client
= ctdb_reqid_find(ctdb
, client_id
, struct ctdb_client
);
2879 struct ctdb_control_tcp
*old_addr
= NULL
;
2880 struct ctdb_control_tcp_addr new_addr
;
2881 struct ctdb_control_tcp_addr
*tcp_sock
= NULL
;
2882 struct ctdb_tcp_list
*tcp
;
2883 struct ctdb_tcp_connection t
;
2886 struct ctdb_client_ip
*ip
;
2887 struct ctdb_vnn
*vnn
;
2888 ctdb_sock_addr addr
;
2890 /* If we don't have public IPs, tickles are useless */
2891 if (ctdb
->vnn
== NULL
) {
2895 switch (indata
.dsize
) {
2896 case sizeof(struct ctdb_control_tcp
):
2897 old_addr
= (struct ctdb_control_tcp
*)indata
.dptr
;
2898 ZERO_STRUCT(new_addr
);
2899 tcp_sock
= &new_addr
;
2900 tcp_sock
->src
.ip
= old_addr
->src
;
2901 tcp_sock
->dest
.ip
= old_addr
->dest
;
2903 case sizeof(struct ctdb_control_tcp_addr
):
2904 tcp_sock
= (struct ctdb_control_tcp_addr
*)indata
.dptr
;
2907 DEBUG(DEBUG_ERR
,(__location__
" Invalid data structure passed "
2908 "to ctdb_control_tcp_client. size was %d but "
2909 "only allowed sizes are %lu and %lu\n",
2911 (long unsigned)sizeof(struct ctdb_control_tcp
),
2912 (long unsigned)sizeof(struct ctdb_control_tcp_addr
)));
2916 addr
= tcp_sock
->src
;
2917 ctdb_canonicalize_ip(&addr
, &tcp_sock
->src
);
2918 addr
= tcp_sock
->dest
;
2919 ctdb_canonicalize_ip(&addr
, &tcp_sock
->dest
);
2922 memcpy(&addr
, &tcp_sock
->dest
, sizeof(addr
));
2923 vnn
= find_public_ip_vnn(ctdb
, &addr
);
2925 switch (addr
.sa
.sa_family
) {
2927 if (ntohl(addr
.ip
.sin_addr
.s_addr
) != INADDR_LOOPBACK
) {
2928 DEBUG(DEBUG_ERR
,("Could not add client IP %s. This is not a public address.\n",
2929 ctdb_addr_to_str(&addr
)));
2933 DEBUG(DEBUG_ERR
,("Could not add client IP %s. This is not a public ipv6 address.\n",
2934 ctdb_addr_to_str(&addr
)));
2937 DEBUG(DEBUG_ERR
,(__location__
" Unknown family type %d\n", addr
.sa
.sa_family
));
2943 if (vnn
->pnn
!= ctdb
->pnn
) {
2944 DEBUG(DEBUG_ERR
,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2945 ctdb_addr_to_str(&addr
),
2946 client_id
, client
->pid
));
2947 /* failing this call will tell smbd to die */
2951 ip
= talloc(client
, struct ctdb_client_ip
);
2952 CTDB_NO_MEMORY(ctdb
, ip
);
2956 ip
->client_id
= client_id
;
2957 talloc_set_destructor(ip
, ctdb_client_ip_destructor
);
2958 DLIST_ADD(ctdb
->client_ip_list
, ip
);
2960 tcp
= talloc(client
, struct ctdb_tcp_list
);
2961 CTDB_NO_MEMORY(ctdb
, tcp
);
2963 tcp
->connection
.src_addr
= tcp_sock
->src
;
2964 tcp
->connection
.dst_addr
= tcp_sock
->dest
;
2966 DLIST_ADD(client
->tcp_list
, tcp
);
2968 t
.src_addr
= tcp_sock
->src
;
2969 t
.dst_addr
= tcp_sock
->dest
;
2971 data
.dptr
= (uint8_t *)&t
;
2972 data
.dsize
= sizeof(t
);
2974 switch (addr
.sa
.sa_family
) {
2976 DEBUG(DEBUG_INFO
,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2977 (unsigned)ntohs(tcp_sock
->dest
.ip
.sin_port
),
2978 ctdb_addr_to_str(&tcp_sock
->src
),
2979 (unsigned)ntohs(tcp_sock
->src
.ip
.sin_port
), client_id
, client
->pid
));
2982 DEBUG(DEBUG_INFO
,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2983 (unsigned)ntohs(tcp_sock
->dest
.ip6
.sin6_port
),
2984 ctdb_addr_to_str(&tcp_sock
->src
),
2985 (unsigned)ntohs(tcp_sock
->src
.ip6
.sin6_port
), client_id
, client
->pid
));
2988 DEBUG(DEBUG_ERR
,(__location__
" Unknown family %d\n", addr
.sa
.sa_family
));
2992 /* tell all nodes about this tcp connection */
2993 ret
= ctdb_daemon_send_control(ctdb
, CTDB_BROADCAST_CONNECTED
, 0,
2994 CTDB_CONTROL_TCP_ADD
,
2995 0, CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
);
2997 DEBUG(DEBUG_ERR
,(__location__
" Failed to send CTDB_CONTROL_TCP_ADD\n"));
3005 find a tcp address on a list
3007 static struct ctdb_tcp_connection
*ctdb_tcp_find(struct ctdb_tcp_array
*array
,
3008 struct ctdb_tcp_connection
*tcp
)
3012 if (array
== NULL
) {
3016 for (i
=0;i
<array
->num
;i
++) {
3017 if (ctdb_same_sockaddr(&array
->connections
[i
].src_addr
, &tcp
->src_addr
) &&
3018 ctdb_same_sockaddr(&array
->connections
[i
].dst_addr
, &tcp
->dst_addr
)) {
3019 return &array
->connections
[i
];
3028 called by a daemon to inform us of a TCP connection that one of its
3029 clients managing that should tickled with an ACK when IP takeover is
3032 int32_t ctdb_control_tcp_add(struct ctdb_context
*ctdb
, TDB_DATA indata
, bool tcp_update_needed
)
3034 struct ctdb_tcp_connection
*p
= (struct ctdb_tcp_connection
*)indata
.dptr
;
3035 struct ctdb_tcp_array
*tcparray
;
3036 struct ctdb_tcp_connection tcp
;
3037 struct ctdb_vnn
*vnn
;
3039 /* If we don't have public IPs, tickles are useless */
3040 if (ctdb
->vnn
== NULL
) {
3044 vnn
= find_public_ip_vnn(ctdb
, &p
->dst_addr
);
3046 DEBUG(DEBUG_INFO
,(__location__
" got TCP_ADD control for an address which is not a public address '%s'\n",
3047 ctdb_addr_to_str(&p
->dst_addr
)));
3053 tcparray
= vnn
->tcp_array
;
3055 /* If this is the first tickle */
3056 if (tcparray
== NULL
) {
3057 tcparray
= talloc(vnn
, struct ctdb_tcp_array
);
3058 CTDB_NO_MEMORY(ctdb
, tcparray
);
3059 vnn
->tcp_array
= tcparray
;
3062 tcparray
->connections
= talloc_size(tcparray
, sizeof(struct ctdb_tcp_connection
));
3063 CTDB_NO_MEMORY(ctdb
, tcparray
->connections
);
3065 tcparray
->connections
[tcparray
->num
].src_addr
= p
->src_addr
;
3066 tcparray
->connections
[tcparray
->num
].dst_addr
= p
->dst_addr
;
3069 if (tcp_update_needed
) {
3070 vnn
->tcp_update_needed
= true;
3076 /* Do we already have this tickle ?*/
3077 tcp
.src_addr
= p
->src_addr
;
3078 tcp
.dst_addr
= p
->dst_addr
;
3079 if (ctdb_tcp_find(tcparray
, &tcp
) != NULL
) {
3080 DEBUG(DEBUG_DEBUG
,("Already had tickle info for %s:%u for vnn:%u\n",
3081 ctdb_addr_to_str(&tcp
.dst_addr
),
3082 ntohs(tcp
.dst_addr
.ip
.sin_port
),
3087 /* A new tickle, we must add it to the array */
3088 tcparray
->connections
= talloc_realloc(tcparray
, tcparray
->connections
,
3089 struct ctdb_tcp_connection
,
3091 CTDB_NO_MEMORY(ctdb
, tcparray
->connections
);
3093 tcparray
->connections
[tcparray
->num
].src_addr
= p
->src_addr
;
3094 tcparray
->connections
[tcparray
->num
].dst_addr
= p
->dst_addr
;
3097 DEBUG(DEBUG_INFO
,("Added tickle info for %s:%u from vnn %u\n",
3098 ctdb_addr_to_str(&tcp
.dst_addr
),
3099 ntohs(tcp
.dst_addr
.ip
.sin_port
),
3102 if (tcp_update_needed
) {
3103 vnn
->tcp_update_needed
= true;
3111 called by a daemon to inform us of a TCP connection that one of its
3112 clients managing that should tickled with an ACK when IP takeover is
3115 static void ctdb_remove_tcp_connection(struct ctdb_context
*ctdb
, struct ctdb_tcp_connection
*conn
)
3117 struct ctdb_tcp_connection
*tcpp
;
3118 struct ctdb_vnn
*vnn
= find_public_ip_vnn(ctdb
, &conn
->dst_addr
);
3121 DEBUG(DEBUG_ERR
,(__location__
" unable to find public address %s\n",
3122 ctdb_addr_to_str(&conn
->dst_addr
)));
3126 /* if the array is empty we cant remove it
3127 and we dont need to do anything
3129 if (vnn
->tcp_array
== NULL
) {
3130 DEBUG(DEBUG_INFO
,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3131 ctdb_addr_to_str(&conn
->dst_addr
),
3132 ntohs(conn
->dst_addr
.ip
.sin_port
)));
3137 /* See if we know this connection
3138 if we dont know this connection then we dont need to do anything
3140 tcpp
= ctdb_tcp_find(vnn
->tcp_array
, conn
);
3142 DEBUG(DEBUG_INFO
,("Trying to remove tickle that doesnt exist %s:%u\n",
3143 ctdb_addr_to_str(&conn
->dst_addr
),
3144 ntohs(conn
->dst_addr
.ip
.sin_port
)));
3149 /* We need to remove this entry from the array.
3150 Instead of allocating a new array and copying data to it
3151 we cheat and just copy the last entry in the existing array
3152 to the entry that is to be removed and just shring the
3155 *tcpp
= vnn
->tcp_array
->connections
[vnn
->tcp_array
->num
- 1];
3156 vnn
->tcp_array
->num
--;
3158 /* If we deleted the last entry we also need to remove the entire array
3160 if (vnn
->tcp_array
->num
== 0) {
3161 talloc_free(vnn
->tcp_array
);
3162 vnn
->tcp_array
= NULL
;
3165 vnn
->tcp_update_needed
= true;
3167 DEBUG(DEBUG_INFO
,("Removed tickle info for %s:%u\n",
3168 ctdb_addr_to_str(&conn
->src_addr
),
3169 ntohs(conn
->src_addr
.ip
.sin_port
)));
3174 called by a daemon to inform us of a TCP connection that one of its
3175 clients used are no longer needed in the tickle database
3177 int32_t ctdb_control_tcp_remove(struct ctdb_context
*ctdb
, TDB_DATA indata
)
3179 struct ctdb_tcp_connection
*conn
= (struct ctdb_tcp_connection
*)indata
.dptr
;
3181 /* If we don't have public IPs, tickles are useless */
3182 if (ctdb
->vnn
== NULL
) {
3186 ctdb_remove_tcp_connection(ctdb
, conn
);
3193 Called when another daemon starts - causes all tickles for all
3194 public addresses we are serving to be sent to the new node on the
3195 next check. This actually causes the next scheduled call to
3196 tdb_update_tcp_tickles() to update all nodes. This is simple and
3197 doesn't require careful error handling.
3199 int32_t ctdb_control_startup(struct ctdb_context
*ctdb
, uint32_t pnn
)
3201 struct ctdb_vnn
*vnn
;
3203 DEBUG(DEBUG_INFO
, ("Received startup control from node %lu\n",
3204 (unsigned long) pnn
));
3206 for (vnn
= ctdb
->vnn
; vnn
!= NULL
; vnn
= vnn
->next
) {
3207 vnn
->tcp_update_needed
= true;
3215 called when a client structure goes away - hook to remove
3216 elements from the tcp_list in all daemons
3218 void ctdb_takeover_client_destructor_hook(struct ctdb_client
*client
)
3220 while (client
->tcp_list
) {
3221 struct ctdb_tcp_list
*tcp
= client
->tcp_list
;
3222 DLIST_REMOVE(client
->tcp_list
, tcp
);
3223 ctdb_remove_tcp_connection(client
->ctdb
, &tcp
->connection
);
3229 release all IPs on shutdown
3231 void ctdb_release_all_ips(struct ctdb_context
*ctdb
)
3233 struct ctdb_vnn
*vnn
;
3236 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3237 if (!ctdb_sys_have_ip(&vnn
->public_address
)) {
3238 ctdb_vnn_unassign_iface(ctdb
, vnn
);
3245 DEBUG(DEBUG_INFO
,("Release of IP %s/%u on interface %s node:-1\n",
3246 ctdb_addr_to_str(&vnn
->public_address
),
3247 vnn
->public_netmask_bits
,
3248 ctdb_vnn_iface_string(vnn
)));
3250 ctdb_event_script_args(ctdb
, CTDB_EVENT_RELEASE_IP
, "%s %s %u",
3251 ctdb_vnn_iface_string(vnn
),
3252 ctdb_addr_to_str(&vnn
->public_address
),
3253 vnn
->public_netmask_bits
);
3254 release_kill_clients(ctdb
, &vnn
->public_address
);
3255 ctdb_vnn_unassign_iface(ctdb
, vnn
);
3259 DEBUG(DEBUG_NOTICE
,(__location__
" Released %d public IPs\n", count
));
3264 get list of public IPs
3266 int32_t ctdb_control_get_public_ips(struct ctdb_context
*ctdb
,
3267 struct ctdb_req_control
*c
, TDB_DATA
*outdata
)
3270 struct ctdb_all_public_ips
*ips
;
3271 struct ctdb_vnn
*vnn
;
3272 bool only_available
= false;
3274 if (c
->flags
& CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE
) {
3275 only_available
= true;
3278 /* count how many public ip structures we have */
3280 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3284 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
3285 num
*sizeof(struct ctdb_public_ip
);
3286 ips
= talloc_zero_size(outdata
, len
);
3287 CTDB_NO_MEMORY(ctdb
, ips
);
3290 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3291 if (only_available
&& !ctdb_vnn_available(ctdb
, vnn
)) {
3294 ips
->ips
[i
].pnn
= vnn
->pnn
;
3295 ips
->ips
[i
].addr
= vnn
->public_address
;
3299 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
3300 i
*sizeof(struct ctdb_public_ip
);
3302 outdata
->dsize
= len
;
3303 outdata
->dptr
= (uint8_t *)ips
;
3310 get list of public IPs, old ipv4 style. only returns ipv4 addresses
3312 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context
*ctdb
,
3313 struct ctdb_req_control
*c
, TDB_DATA
*outdata
)
3316 struct ctdb_all_public_ipsv4
*ips
;
3317 struct ctdb_vnn
*vnn
;
3319 /* count how many public ip structures we have */
3321 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3322 if (vnn
->public_address
.sa
.sa_family
!= AF_INET
) {
3328 len
= offsetof(struct ctdb_all_public_ipsv4
, ips
) +
3329 num
*sizeof(struct ctdb_public_ipv4
);
3330 ips
= talloc_zero_size(outdata
, len
);
3331 CTDB_NO_MEMORY(ctdb
, ips
);
3333 outdata
->dsize
= len
;
3334 outdata
->dptr
= (uint8_t *)ips
;
3338 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3339 if (vnn
->public_address
.sa
.sa_family
!= AF_INET
) {
3342 ips
->ips
[i
].pnn
= vnn
->pnn
;
3343 ips
->ips
[i
].sin
= vnn
->public_address
.ip
;
3350 int32_t ctdb_control_get_public_ip_info(struct ctdb_context
*ctdb
,
3351 struct ctdb_req_control
*c
,
3356 ctdb_sock_addr
*addr
;
3357 struct ctdb_control_public_ip_info
*info
;
3358 struct ctdb_vnn
*vnn
;
3360 addr
= (ctdb_sock_addr
*)indata
.dptr
;
3362 vnn
= find_public_ip_vnn(ctdb
, addr
);
3364 /* if it is not a public ip it could be our 'single ip' */
3365 if (ctdb
->single_ip_vnn
) {
3366 if (ctdb_same_ip(&ctdb
->single_ip_vnn
->public_address
, addr
)) {
3367 vnn
= ctdb
->single_ip_vnn
;
3372 DEBUG(DEBUG_ERR
,(__location__
" Could not get public ip info, "
3373 "'%s'not a public address\n",
3374 ctdb_addr_to_str(addr
)));
3378 /* count how many public ip structures we have */
3380 for (;vnn
->ifaces
[num
];) {
3384 len
= offsetof(struct ctdb_control_public_ip_info
, ifaces
) +
3385 num
*sizeof(struct ctdb_control_iface_info
);
3386 info
= talloc_zero_size(outdata
, len
);
3387 CTDB_NO_MEMORY(ctdb
, info
);
3389 info
->ip
.addr
= vnn
->public_address
;
3390 info
->ip
.pnn
= vnn
->pnn
;
3391 info
->active_idx
= 0xFFFFFFFF;
3393 for (i
=0; vnn
->ifaces
[i
]; i
++) {
3394 struct ctdb_iface
*cur
;
3396 cur
= ctdb_find_iface(ctdb
, vnn
->ifaces
[i
]);
3398 DEBUG(DEBUG_CRIT
, (__location__
" internal error iface[%s] unknown\n",
3402 if (vnn
->iface
== cur
) {
3403 info
->active_idx
= i
;
3405 strncpy(info
->ifaces
[i
].name
, cur
->name
, sizeof(info
->ifaces
[i
].name
)-1);
3406 info
->ifaces
[i
].link_state
= cur
->link_up
;
3407 info
->ifaces
[i
].references
= cur
->references
;
3410 len
= offsetof(struct ctdb_control_public_ip_info
, ifaces
) +
3411 i
*sizeof(struct ctdb_control_iface_info
);
3413 outdata
->dsize
= len
;
3414 outdata
->dptr
= (uint8_t *)info
;
3419 int32_t ctdb_control_get_ifaces(struct ctdb_context
*ctdb
,
3420 struct ctdb_req_control
*c
,
3424 struct ctdb_control_get_ifaces
*ifaces
;
3425 struct ctdb_iface
*cur
;
3427 /* count how many public ip structures we have */
3429 for (cur
=ctdb
->ifaces
;cur
;cur
=cur
->next
) {
3433 len
= offsetof(struct ctdb_control_get_ifaces
, ifaces
) +
3434 num
*sizeof(struct ctdb_control_iface_info
);
3435 ifaces
= talloc_zero_size(outdata
, len
);
3436 CTDB_NO_MEMORY(ctdb
, ifaces
);
3439 for (cur
=ctdb
->ifaces
;cur
;cur
=cur
->next
) {
3440 strcpy(ifaces
->ifaces
[i
].name
, cur
->name
);
3441 ifaces
->ifaces
[i
].link_state
= cur
->link_up
;
3442 ifaces
->ifaces
[i
].references
= cur
->references
;
3446 len
= offsetof(struct ctdb_control_get_ifaces
, ifaces
) +
3447 i
*sizeof(struct ctdb_control_iface_info
);
3449 outdata
->dsize
= len
;
3450 outdata
->dptr
= (uint8_t *)ifaces
;
3455 int32_t ctdb_control_set_iface_link(struct ctdb_context
*ctdb
,
3456 struct ctdb_req_control
*c
,
3459 struct ctdb_control_iface_info
*info
;
3460 struct ctdb_iface
*iface
;
3461 bool link_up
= false;
3463 info
= (struct ctdb_control_iface_info
*)indata
.dptr
;
3465 if (info
->name
[CTDB_IFACE_SIZE
] != '\0') {
3466 int len
= strnlen(info
->name
, CTDB_IFACE_SIZE
);
3467 DEBUG(DEBUG_ERR
, (__location__
" name[%*.*s] not terminated\n",
3468 len
, len
, info
->name
));
3472 switch (info
->link_state
) {
3480 DEBUG(DEBUG_ERR
, (__location__
" link_state[%u] invalid\n",
3481 (unsigned int)info
->link_state
));
3485 if (info
->references
!= 0) {
3486 DEBUG(DEBUG_ERR
, (__location__
" references[%u] should be 0\n",
3487 (unsigned int)info
->references
));
3491 iface
= ctdb_find_iface(ctdb
, info
->name
);
3492 if (iface
== NULL
) {
3496 if (link_up
== iface
->link_up
) {
3500 DEBUG(iface
->link_up
?DEBUG_ERR
:DEBUG_NOTICE
,
3501 ("iface[%s] has changed it's link status %s => %s\n",
3503 iface
->link_up
?"up":"down",
3504 link_up
?"up":"down"));
3506 iface
->link_up
= link_up
;
3512 structure containing the listening socket and the list of tcp connections
3513 that the ctdb daemon is to kill
3515 struct ctdb_kill_tcp
{
3516 struct ctdb_vnn
*vnn
;
3517 struct ctdb_context
*ctdb
;
3519 struct fd_event
*fde
;
3520 trbt_tree_t
*connections
;
3525 a tcp connection that is to be killed
3527 struct ctdb_killtcp_con
{
3528 ctdb_sock_addr src_addr
;
3529 ctdb_sock_addr dst_addr
;
3531 struct ctdb_kill_tcp
*killtcp
;
3534 /* this function is used to create a key to represent this socketpair
3535 in the killtcp tree.
3536 this key is used to insert and lookup matching socketpairs that are
3537 to be tickled and RST
3539 #define KILLTCP_KEYLEN 10
3540 static uint32_t *killtcp_key(ctdb_sock_addr
*src
, ctdb_sock_addr
*dst
)
3542 static uint32_t key
[KILLTCP_KEYLEN
];
3544 bzero(key
, sizeof(key
));
3546 if (src
->sa
.sa_family
!= dst
->sa
.sa_family
) {
3547 DEBUG(DEBUG_ERR
, (__location__
" ERROR, different families passed :%u vs %u\n", src
->sa
.sa_family
, dst
->sa
.sa_family
));
3551 switch (src
->sa
.sa_family
) {
3553 key
[0] = dst
->ip
.sin_addr
.s_addr
;
3554 key
[1] = src
->ip
.sin_addr
.s_addr
;
3555 key
[2] = dst
->ip
.sin_port
;
3556 key
[3] = src
->ip
.sin_port
;
3559 uint32_t *dst6_addr32
=
3560 (uint32_t *)&(dst
->ip6
.sin6_addr
.s6_addr
);
3561 uint32_t *src6_addr32
=
3562 (uint32_t *)&(src
->ip6
.sin6_addr
.s6_addr
);
3563 key
[0] = dst6_addr32
[3];
3564 key
[1] = src6_addr32
[3];
3565 key
[2] = dst6_addr32
[2];
3566 key
[3] = src6_addr32
[2];
3567 key
[4] = dst6_addr32
[1];
3568 key
[5] = src6_addr32
[1];
3569 key
[6] = dst6_addr32
[0];
3570 key
[7] = src6_addr32
[0];
3571 key
[8] = dst
->ip6
.sin6_port
;
3572 key
[9] = src
->ip6
.sin6_port
;
3576 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", src
->sa
.sa_family
));
3584 called when we get a read event on the raw socket
3586 static void capture_tcp_handler(struct event_context
*ev
, struct fd_event
*fde
,
3587 uint16_t flags
, void *private_data
)
3589 struct ctdb_kill_tcp
*killtcp
= talloc_get_type(private_data
, struct ctdb_kill_tcp
);
3590 struct ctdb_killtcp_con
*con
;
3591 ctdb_sock_addr src
, dst
;
3592 uint32_t ack_seq
, seq
;
3594 if (!(flags
& EVENT_FD_READ
)) {
3598 if (ctdb_sys_read_tcp_packet(killtcp
->capture_fd
,
3599 killtcp
->private_data
,
3601 &ack_seq
, &seq
) != 0) {
3602 /* probably a non-tcp ACK packet */
3606 /* check if we have this guy in our list of connections
3609 con
= trbt_lookuparray32(killtcp
->connections
,
3610 KILLTCP_KEYLEN
, killtcp_key(&src
, &dst
));
3612 /* no this was some other packet we can just ignore */
3616 /* This one has been tickled !
3617 now reset him and remove him from the list.
3619 DEBUG(DEBUG_INFO
, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3620 ntohs(con
->dst_addr
.ip
.sin_port
),
3621 ctdb_addr_to_str(&con
->src_addr
),
3622 ntohs(con
->src_addr
.ip
.sin_port
)));
3624 ctdb_sys_send_tcp(&con
->dst_addr
, &con
->src_addr
, ack_seq
, seq
, 1);
3629 /* when traversing the list of all tcp connections to send tickle acks to
3630 (so that we can capture the ack coming back and kill the connection
3632 this callback is called for each connection we are currently trying to kill
3634 static int tickle_connection_traverse(void *param
, void *data
)
3636 struct ctdb_killtcp_con
*con
= talloc_get_type(data
, struct ctdb_killtcp_con
);
3638 /* have tried too many times, just give up */
3639 if (con
->count
>= 5) {
3640 /* can't delete in traverse: reparent to delete_cons */
3641 talloc_steal(param
, con
);
3645 /* othervise, try tickling it again */
3648 (ctdb_sock_addr
*)&con
->dst_addr
,
3649 (ctdb_sock_addr
*)&con
->src_addr
,
3656 called every second until all sentenced connections have been reset
3658 static void ctdb_tickle_sentenced_connections(struct event_context
*ev
, struct timed_event
*te
,
3659 struct timeval t
, void *private_data
)
3661 struct ctdb_kill_tcp
*killtcp
= talloc_get_type(private_data
, struct ctdb_kill_tcp
);
3662 void *delete_cons
= talloc_new(NULL
);
3664 /* loop over all connections sending tickle ACKs */
3665 trbt_traversearray32(killtcp
->connections
, KILLTCP_KEYLEN
, tickle_connection_traverse
, delete_cons
);
3667 /* now we've finished traverse, it's safe to do deletion. */
3668 talloc_free(delete_cons
);
3670 /* If there are no more connections to kill we can remove the
3671 entire killtcp structure
3673 if ( (killtcp
->connections
== NULL
) ||
3674 (killtcp
->connections
->root
== NULL
) ) {
3675 talloc_free(killtcp
);
3679 /* try tickling them again in a seconds time
3681 event_add_timed(killtcp
->ctdb
->ev
, killtcp
, timeval_current_ofs(1, 0),
3682 ctdb_tickle_sentenced_connections
, killtcp
);
3686 destroy the killtcp structure
3688 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp
*killtcp
)
3690 struct ctdb_vnn
*tmpvnn
;
3692 /* verify that this vnn is still active */
3693 for (tmpvnn
= killtcp
->ctdb
->vnn
; tmpvnn
; tmpvnn
= tmpvnn
->next
) {
3694 if (tmpvnn
== killtcp
->vnn
) {
3699 if (tmpvnn
== NULL
) {
3703 if (killtcp
->vnn
->killtcp
!= killtcp
) {
3707 killtcp
->vnn
->killtcp
= NULL
;
3713 /* nothing fancy here, just unconditionally replace any existing
3714 connection structure with the new one.
3716 dont even free the old one if it did exist, that one is talloc_stolen
3717 by the same node in the tree anyway and will be deleted when the new data
3720 static void *add_killtcp_callback(void *parm
, void *data
)
3726 add a tcp socket to the list of connections we want to RST
3728 static int ctdb_killtcp_add_connection(struct ctdb_context
*ctdb
,
3732 ctdb_sock_addr src
, dst
;
3733 struct ctdb_kill_tcp
*killtcp
;
3734 struct ctdb_killtcp_con
*con
;
3735 struct ctdb_vnn
*vnn
;
3737 ctdb_canonicalize_ip(s
, &src
);
3738 ctdb_canonicalize_ip(d
, &dst
);
3740 vnn
= find_public_ip_vnn(ctdb
, &dst
);
3742 vnn
= find_public_ip_vnn(ctdb
, &src
);
3745 /* if it is not a public ip it could be our 'single ip' */
3746 if (ctdb
->single_ip_vnn
) {
3747 if (ctdb_same_ip(&ctdb
->single_ip_vnn
->public_address
, &dst
)) {
3748 vnn
= ctdb
->single_ip_vnn
;
3753 DEBUG(DEBUG_ERR
,(__location__
" Could not killtcp, not a public address\n"));
3757 killtcp
= vnn
->killtcp
;
3759 /* If this is the first connection to kill we must allocate
3762 if (killtcp
== NULL
) {
3763 killtcp
= talloc_zero(vnn
, struct ctdb_kill_tcp
);
3764 CTDB_NO_MEMORY(ctdb
, killtcp
);
3767 killtcp
->ctdb
= ctdb
;
3768 killtcp
->capture_fd
= -1;
3769 killtcp
->connections
= trbt_create(killtcp
, 0);
3771 vnn
->killtcp
= killtcp
;
3772 talloc_set_destructor(killtcp
, ctdb_killtcp_destructor
);
3777 /* create a structure that describes this connection we want to
3778 RST and store it in killtcp->connections
3780 con
= talloc(killtcp
, struct ctdb_killtcp_con
);
3781 CTDB_NO_MEMORY(ctdb
, con
);
3782 con
->src_addr
= src
;
3783 con
->dst_addr
= dst
;
3785 con
->killtcp
= killtcp
;
3788 trbt_insertarray32_callback(killtcp
->connections
,
3789 KILLTCP_KEYLEN
, killtcp_key(&con
->dst_addr
, &con
->src_addr
),
3790 add_killtcp_callback
, con
);
3793 If we dont have a socket to listen on yet we must create it
3795 if (killtcp
->capture_fd
== -1) {
3796 const char *iface
= ctdb_vnn_iface_string(vnn
);
3797 killtcp
->capture_fd
= ctdb_sys_open_capture_socket(iface
, &killtcp
->private_data
);
3798 if (killtcp
->capture_fd
== -1) {
3799 DEBUG(DEBUG_CRIT
,(__location__
" Failed to open capturing "
3800 "socket on iface '%s' for killtcp (%s)\n",
3801 iface
, strerror(errno
)));
3807 if (killtcp
->fde
== NULL
) {
3808 killtcp
->fde
= event_add_fd(ctdb
->ev
, killtcp
, killtcp
->capture_fd
,
3810 capture_tcp_handler
, killtcp
);
3811 tevent_fd_set_auto_close(killtcp
->fde
);
3813 /* We also need to set up some events to tickle all these connections
3814 until they are all reset
3816 event_add_timed(ctdb
->ev
, killtcp
, timeval_current_ofs(1, 0),
3817 ctdb_tickle_sentenced_connections
, killtcp
);
3820 /* tickle him once now */
3829 talloc_free(vnn
->killtcp
);
3830 vnn
->killtcp
= NULL
;
3835 kill a TCP connection.
3837 int32_t ctdb_control_kill_tcp(struct ctdb_context
*ctdb
, TDB_DATA indata
)
3839 struct ctdb_control_killtcp
*killtcp
= (struct ctdb_control_killtcp
*)indata
.dptr
;
3841 return ctdb_killtcp_add_connection(ctdb
, &killtcp
->src_addr
, &killtcp
->dst_addr
);
3845 called by a daemon to inform us of the entire list of TCP tickles for
3846 a particular public address.
3847 this control should only be sent by the node that is currently serving
3848 that public address.
3850 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context
*ctdb
, TDB_DATA indata
)
3852 struct ctdb_control_tcp_tickle_list
*list
= (struct ctdb_control_tcp_tickle_list
*)indata
.dptr
;
3853 struct ctdb_tcp_array
*tcparray
;
3854 struct ctdb_vnn
*vnn
;
3856 /* We must at least have tickles.num or else we cant verify the size
3857 of the received data blob
3859 if (indata
.dsize
< offsetof(struct ctdb_control_tcp_tickle_list
,
3860 tickles
.connections
)) {
3861 DEBUG(DEBUG_ERR
,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3865 /* verify that the size of data matches what we expect */
3866 if (indata
.dsize
< offsetof(struct ctdb_control_tcp_tickle_list
,
3867 tickles
.connections
)
3868 + sizeof(struct ctdb_tcp_connection
)
3869 * list
->tickles
.num
) {
3870 DEBUG(DEBUG_ERR
,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3874 DEBUG(DEBUG_INFO
, ("Received tickle update for public address %s\n",
3875 ctdb_addr_to_str(&list
->addr
)));
3877 vnn
= find_public_ip_vnn(ctdb
, &list
->addr
);
3879 DEBUG(DEBUG_INFO
,(__location__
" Could not set tcp tickle list, '%s' is not a public address\n",
3880 ctdb_addr_to_str(&list
->addr
)));
3885 /* remove any old ticklelist we might have */
3886 talloc_free(vnn
->tcp_array
);
3887 vnn
->tcp_array
= NULL
;
3889 tcparray
= talloc(vnn
, struct ctdb_tcp_array
);
3890 CTDB_NO_MEMORY(ctdb
, tcparray
);
3892 tcparray
->num
= list
->tickles
.num
;
3894 tcparray
->connections
= talloc_array(tcparray
, struct ctdb_tcp_connection
, tcparray
->num
);
3895 CTDB_NO_MEMORY(ctdb
, tcparray
->connections
);
3897 memcpy(tcparray
->connections
, &list
->tickles
.connections
[0],
3898 sizeof(struct ctdb_tcp_connection
)*tcparray
->num
);
3900 /* We now have a new fresh tickle list array for this vnn */
3901 vnn
->tcp_array
= tcparray
;
3907 called to return the full list of tickles for the puclic address associated
3908 with the provided vnn
3910 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context
*ctdb
, TDB_DATA indata
, TDB_DATA
*outdata
)
3912 ctdb_sock_addr
*addr
= (ctdb_sock_addr
*)indata
.dptr
;
3913 struct ctdb_control_tcp_tickle_list
*list
;
3914 struct ctdb_tcp_array
*tcparray
;
3916 struct ctdb_vnn
*vnn
;
3918 vnn
= find_public_ip_vnn(ctdb
, addr
);
3920 DEBUG(DEBUG_ERR
,(__location__
" Could not get tcp tickle list, '%s' is not a public address\n",
3921 ctdb_addr_to_str(addr
)));
3926 tcparray
= vnn
->tcp_array
;
3928 num
= tcparray
->num
;
3933 outdata
->dsize
= offsetof(struct ctdb_control_tcp_tickle_list
,
3934 tickles
.connections
)
3935 + sizeof(struct ctdb_tcp_connection
) * num
;
3937 outdata
->dptr
= talloc_size(outdata
, outdata
->dsize
);
3938 CTDB_NO_MEMORY(ctdb
, outdata
->dptr
);
3939 list
= (struct ctdb_control_tcp_tickle_list
*)outdata
->dptr
;
3942 list
->tickles
.num
= num
;
3944 memcpy(&list
->tickles
.connections
[0], tcparray
->connections
,
3945 sizeof(struct ctdb_tcp_connection
) * num
);
3953 set the list of all tcp tickles for a public address
3955 static int ctdb_send_set_tcp_tickles_for_ip(struct ctdb_context
*ctdb
,
3956 ctdb_sock_addr
*addr
,
3957 struct ctdb_tcp_array
*tcparray
)
3961 struct ctdb_control_tcp_tickle_list
*list
;
3964 num
= tcparray
->num
;
3969 data
.dsize
= offsetof(struct ctdb_control_tcp_tickle_list
,
3970 tickles
.connections
) +
3971 sizeof(struct ctdb_tcp_connection
) * num
;
3972 data
.dptr
= talloc_size(ctdb
, data
.dsize
);
3973 CTDB_NO_MEMORY(ctdb
, data
.dptr
);
3975 list
= (struct ctdb_control_tcp_tickle_list
*)data
.dptr
;
3977 list
->tickles
.num
= num
;
3979 memcpy(&list
->tickles
.connections
[0], tcparray
->connections
, sizeof(struct ctdb_tcp_connection
) * num
);
3982 ret
= ctdb_daemon_send_control(ctdb
, CTDB_BROADCAST_ALL
, 0,
3983 CTDB_CONTROL_SET_TCP_TICKLE_LIST
,
3984 0, CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
);
3986 DEBUG(DEBUG_ERR
,(__location__
" ctdb_control for set tcp tickles failed\n"));
3990 talloc_free(data
.dptr
);
3997 perform tickle updates if required
3999 static void ctdb_update_tcp_tickles(struct event_context
*ev
,
4000 struct timed_event
*te
,
4001 struct timeval t
, void *private_data
)
4003 struct ctdb_context
*ctdb
= talloc_get_type(private_data
, struct ctdb_context
);
4005 struct ctdb_vnn
*vnn
;
4007 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
4008 /* we only send out updates for public addresses that
4011 if (ctdb
->pnn
!= vnn
->pnn
) {
4014 /* We only send out the updates if we need to */
4015 if (!vnn
->tcp_update_needed
) {
4018 ret
= ctdb_send_set_tcp_tickles_for_ip(ctdb
,
4019 &vnn
->public_address
,
4022 DEBUG(DEBUG_ERR
,("Failed to send the tickle update for public address %s\n",
4023 ctdb_addr_to_str(&vnn
->public_address
)));
4026 ("Sent tickle update for public address %s\n",
4027 ctdb_addr_to_str(&vnn
->public_address
)));
4028 vnn
->tcp_update_needed
= false;
4032 event_add_timed(ctdb
->ev
, ctdb
->tickle_update_context
,
4033 timeval_current_ofs(ctdb
->tunable
.tickle_update_interval
, 0),
4034 ctdb_update_tcp_tickles
, ctdb
);
4039 start periodic update of tcp tickles
4041 void ctdb_start_tcp_tickle_update(struct ctdb_context
*ctdb
)
4043 ctdb
->tickle_update_context
= talloc_new(ctdb
);
4045 event_add_timed(ctdb
->ev
, ctdb
->tickle_update_context
,
4046 timeval_current_ofs(ctdb
->tunable
.tickle_update_interval
, 0),
4047 ctdb_update_tcp_tickles
, ctdb
);
4053 struct control_gratious_arp
{
4054 struct ctdb_context
*ctdb
;
4055 ctdb_sock_addr addr
;
4061 send a control_gratuitous arp
4063 static void send_gratious_arp(struct event_context
*ev
, struct timed_event
*te
,
4064 struct timeval t
, void *private_data
)
4067 struct control_gratious_arp
*arp
= talloc_get_type(private_data
,
4068 struct control_gratious_arp
);
4070 ret
= ctdb_sys_send_arp(&arp
->addr
, arp
->iface
);
4072 DEBUG(DEBUG_ERR
,(__location__
" sending of gratious arp on iface '%s' failed (%s)\n",
4073 arp
->iface
, strerror(errno
)));
4078 if (arp
->count
== CTDB_ARP_REPEAT
) {
4083 event_add_timed(arp
->ctdb
->ev
, arp
,
4084 timeval_current_ofs(CTDB_ARP_INTERVAL
, 0),
4085 send_gratious_arp
, arp
);
4092 int32_t ctdb_control_send_gratious_arp(struct ctdb_context
*ctdb
, TDB_DATA indata
)
4094 struct ctdb_control_gratious_arp
*gratious_arp
= (struct ctdb_control_gratious_arp
*)indata
.dptr
;
4095 struct control_gratious_arp
*arp
;
4097 /* verify the size of indata */
4098 if (indata
.dsize
< offsetof(struct ctdb_control_gratious_arp
, iface
)) {
4099 DEBUG(DEBUG_ERR
,(__location__
" Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
4100 (unsigned)indata
.dsize
,
4101 (unsigned)offsetof(struct ctdb_control_gratious_arp
, iface
)));
4105 ( offsetof(struct ctdb_control_gratious_arp
, iface
)
4106 + gratious_arp
->len
) ){
4108 DEBUG(DEBUG_ERR
,(__location__
" Wrong size of indata. Was %u bytes "
4109 "but should be %u bytes\n",
4110 (unsigned)indata
.dsize
,
4111 (unsigned)(offsetof(struct ctdb_control_gratious_arp
, iface
)+gratious_arp
->len
)));
4116 arp
= talloc(ctdb
, struct control_gratious_arp
);
4117 CTDB_NO_MEMORY(ctdb
, arp
);
4120 arp
->addr
= gratious_arp
->addr
;
4121 arp
->iface
= talloc_strdup(arp
, gratious_arp
->iface
);
4122 CTDB_NO_MEMORY(ctdb
, arp
->iface
);
4125 event_add_timed(arp
->ctdb
->ev
, arp
,
4126 timeval_zero(), send_gratious_arp
, arp
);
4131 int32_t ctdb_control_add_public_address(struct ctdb_context
*ctdb
, TDB_DATA indata
)
4133 struct ctdb_control_ip_iface
*pub
= (struct ctdb_control_ip_iface
*)indata
.dptr
;
4136 /* verify the size of indata */
4137 if (indata
.dsize
< offsetof(struct ctdb_control_ip_iface
, iface
)) {
4138 DEBUG(DEBUG_ERR
,(__location__
" Too small indata to hold a ctdb_control_ip_iface structure\n"));
4142 ( offsetof(struct ctdb_control_ip_iface
, iface
)
4145 DEBUG(DEBUG_ERR
,(__location__
" Wrong size of indata. Was %u bytes "
4146 "but should be %u bytes\n",
4147 (unsigned)indata
.dsize
,
4148 (unsigned)(offsetof(struct ctdb_control_ip_iface
, iface
)+pub
->len
)));
4152 DEBUG(DEBUG_NOTICE
,("Add IP %s\n", ctdb_addr_to_str(&pub
->addr
)));
4154 ret
= ctdb_add_public_address(ctdb
, &pub
->addr
, pub
->mask
, &pub
->iface
[0], true);
4157 DEBUG(DEBUG_ERR
,(__location__
" Failed to add public address\n"));
4164 struct delete_ip_callback_state
{
4165 struct ctdb_req_control
*c
;
4169 called when releaseip event finishes for del_public_address
4171 static void delete_ip_callback(struct ctdb_context
*ctdb
,
4172 int32_t status
, TDB_DATA data
,
4173 const char *errormsg
,
4176 struct delete_ip_callback_state
*state
=
4177 talloc_get_type(private_data
, struct delete_ip_callback_state
);
4179 /* If release failed then fail. */
4180 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, errormsg
);
4181 talloc_free(private_data
);
4184 int32_t ctdb_control_del_public_address(struct ctdb_context
*ctdb
,
4185 struct ctdb_req_control
*c
,
4186 TDB_DATA indata
, bool *async_reply
)
4188 struct ctdb_control_ip_iface
*pub
= (struct ctdb_control_ip_iface
*)indata
.dptr
;
4189 struct ctdb_vnn
*vnn
;
4191 /* verify the size of indata */
4192 if (indata
.dsize
< offsetof(struct ctdb_control_ip_iface
, iface
)) {
4193 DEBUG(DEBUG_ERR
,(__location__
" Too small indata to hold a ctdb_control_ip_iface structure\n"));
4197 ( offsetof(struct ctdb_control_ip_iface
, iface
)
4200 DEBUG(DEBUG_ERR
,(__location__
" Wrong size of indata. Was %u bytes "
4201 "but should be %u bytes\n",
4202 (unsigned)indata
.dsize
,
4203 (unsigned)(offsetof(struct ctdb_control_ip_iface
, iface
)+pub
->len
)));
4207 DEBUG(DEBUG_NOTICE
,("Delete IP %s\n", ctdb_addr_to_str(&pub
->addr
)));
4209 /* walk over all public addresses until we find a match */
4210 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
4211 if (ctdb_same_ip(&vnn
->public_address
, &pub
->addr
)) {
4212 if (vnn
->pnn
== ctdb
->pnn
) {
4213 struct delete_ip_callback_state
*state
;
4214 struct ctdb_public_ip
*ip
;
4218 vnn
->delete_pending
= true;
4220 state
= talloc(ctdb
,
4221 struct delete_ip_callback_state
);
4222 CTDB_NO_MEMORY(ctdb
, state
);
4225 ip
= talloc(state
, struct ctdb_public_ip
);
4228 (__location__
" Out of memory\n"));
4233 ip
->addr
= pub
->addr
;
4235 data
.dsize
= sizeof(struct ctdb_public_ip
);
4236 data
.dptr
= (unsigned char *)ip
;
4238 ret
= ctdb_daemon_send_control(ctdb
,
4241 CTDB_CONTROL_RELEASE_IP
,
4248 (__location__
"Unable to send "
4249 "CTDB_CONTROL_RELEASE_IP\n"));
4254 state
->c
= talloc_steal(state
, c
);
4255 *async_reply
= true;
4257 /* This IP is not hosted on the
4258 * current node so just delete it
4260 do_delete_ip(ctdb
, vnn
);
4267 DEBUG(DEBUG_ERR
,("Delete IP of unknown public IP address %s\n",
4268 ctdb_addr_to_str(&pub
->addr
)));
4273 struct ipreallocated_callback_state
{
4274 struct ctdb_req_control
*c
;
4277 static void ctdb_ipreallocated_callback(struct ctdb_context
*ctdb
,
4278 int status
, void *p
)
4280 struct ipreallocated_callback_state
*state
=
4281 talloc_get_type(p
, struct ipreallocated_callback_state
);
4285 (" \"ipreallocated\" event script failed (status %d)\n",
4287 if (status
== -ETIME
) {
4288 ctdb_ban_self(ctdb
);
4292 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
4296 /* A control to run the ipreallocated event */
4297 int32_t ctdb_control_ipreallocated(struct ctdb_context
*ctdb
,
4298 struct ctdb_req_control
*c
,
4302 struct ipreallocated_callback_state
*state
;
4304 state
= talloc(ctdb
, struct ipreallocated_callback_state
);
4305 CTDB_NO_MEMORY(ctdb
, state
);
4307 DEBUG(DEBUG_INFO
,(__location__
" Running \"ipreallocated\" event\n"));
4309 ret
= ctdb_event_script_callback(ctdb
, state
,
4310 ctdb_ipreallocated_callback
, state
,
4311 CTDB_EVENT_IPREALLOCATED
,
4315 DEBUG(DEBUG_ERR
,("Failed to run \"ipreallocated\" event \n"));
4320 /* tell the control that we will be reply asynchronously */
4321 state
->c
= talloc_steal(state
, c
);
4322 *async_reply
= true;
4328 /* This function is called from the recovery daemon to verify that a remote
4329 node has the expected ip allocation.
4330 This is verified against ctdb->ip_tree
4332 int verify_remote_ip_allocation(struct ctdb_context
*ctdb
,
4333 struct ctdb_all_public_ips
*ips
,
4336 struct ctdb_public_ip_list
*tmp_ip
;
4339 if (ctdb
->ip_tree
== NULL
) {
4340 /* dont know the expected allocation yet, assume remote node
4349 for (i
=0; i
<ips
->num
; i
++) {
4350 tmp_ip
= trbt_lookuparray32(ctdb
->ip_tree
, IP_KEYLEN
, ip_key(&ips
->ips
[i
].addr
));
4351 if (tmp_ip
== NULL
) {
4352 DEBUG(DEBUG_ERR
,("Node %u has new or unknown public IP %s\n", pnn
, ctdb_addr_to_str(&ips
->ips
[i
].addr
)));
4356 if (tmp_ip
->pnn
== -1 || ips
->ips
[i
].pnn
== -1) {
4360 if (tmp_ip
->pnn
!= ips
->ips
[i
].pnn
) {
4362 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4364 ctdb_addr_to_str(&ips
->ips
[i
].addr
),
4365 ips
->ips
[i
].pnn
, tmp_ip
->pnn
));
4373 int update_ip_assignment_tree(struct ctdb_context
*ctdb
, struct ctdb_public_ip
*ip
)
4375 struct ctdb_public_ip_list
*tmp_ip
;
4377 if (ctdb
->ip_tree
== NULL
) {
4378 DEBUG(DEBUG_ERR
,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4382 tmp_ip
= trbt_lookuparray32(ctdb
->ip_tree
, IP_KEYLEN
, ip_key(&ip
->addr
));
4383 if (tmp_ip
== NULL
) {
4384 DEBUG(DEBUG_ERR
,(__location__
" Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip
->addr
)));
4388 DEBUG(DEBUG_NOTICE
,("Updated ip assignment tree for ip : %s from node %u to node %u\n", ctdb_addr_to_str(&ip
->addr
), tmp_ip
->pnn
, ip
->pnn
));
4389 tmp_ip
->pnn
= ip
->pnn
;
4395 struct ctdb_reloadips_handle
{
4396 struct ctdb_context
*ctdb
;
4397 struct ctdb_req_control
*c
;
4401 struct fd_event
*fde
;
4404 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle
*h
)
4406 if (h
== h
->ctdb
->reload_ips
) {
4407 h
->ctdb
->reload_ips
= NULL
;
4410 ctdb_request_control_reply(h
->ctdb
, h
->c
, NULL
, h
->status
, NULL
);
4413 ctdb_kill(h
->ctdb
, h
->child
, SIGKILL
);
4417 static void ctdb_reloadips_timeout_event(struct event_context
*ev
,
4418 struct timed_event
*te
,
4419 struct timeval t
, void *private_data
)
4421 struct ctdb_reloadips_handle
*h
= talloc_get_type(private_data
, struct ctdb_reloadips_handle
);
4426 static void ctdb_reloadips_child_handler(struct event_context
*ev
, struct fd_event
*fde
,
4427 uint16_t flags
, void *private_data
)
4429 struct ctdb_reloadips_handle
*h
= talloc_get_type(private_data
, struct ctdb_reloadips_handle
);
4434 ret
= sys_read(h
->fd
[0], &res
, 1);
4435 if (ret
< 1 || res
!= 0) {
4436 DEBUG(DEBUG_ERR
, (__location__
" Reloadips child process returned error\n"));
4444 static int ctdb_reloadips_child(struct ctdb_context
*ctdb
)
4446 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
4447 struct ctdb_all_public_ips
*ips
;
4448 struct ctdb_vnn
*vnn
;
4449 struct client_async_data
*async_data
;
4450 struct timeval timeout
;
4452 struct ctdb_client_control_state
*state
;
4456 CTDB_NO_MEMORY(ctdb
, mem_ctx
);
4458 /* Read IPs from local node */
4459 ret
= ctdb_ctrl_get_public_ips(ctdb
, TAKEOVER_TIMEOUT(),
4460 CTDB_CURRENT_NODE
, mem_ctx
, &ips
);
4463 ("Unable to fetch public IPs from local node\n"));
4464 talloc_free(mem_ctx
);
4468 /* Read IPs file - this is safe since this is a child process */
4470 if (ctdb_set_public_addresses(ctdb
, false) != 0) {
4471 DEBUG(DEBUG_ERR
,("Failed to re-read public addresses file\n"));
4472 talloc_free(mem_ctx
);
4476 async_data
= talloc_zero(mem_ctx
, struct client_async_data
);
4477 CTDB_NO_MEMORY(ctdb
, async_data
);
4479 /* Compare IPs between node and file for IPs to be deleted */
4480 for (i
= 0; i
< ips
->num
; i
++) {
4482 for (vnn
= ctdb
->vnn
; vnn
; vnn
= vnn
->next
) {
4483 if (ctdb_same_ip(&vnn
->public_address
,
4484 &ips
->ips
[i
].addr
)) {
4485 /* IP is still in file */
4491 /* Delete IP ips->ips[i] */
4492 struct ctdb_control_ip_iface
*pub
;
4495 ("IP %s no longer configured, deleting it\n",
4496 ctdb_addr_to_str(&ips
->ips
[i
].addr
)));
4498 pub
= talloc_zero(mem_ctx
,
4499 struct ctdb_control_ip_iface
);
4500 CTDB_NO_MEMORY(ctdb
, pub
);
4502 pub
->addr
= ips
->ips
[i
].addr
;
4506 timeout
= TAKEOVER_TIMEOUT();
4508 data
.dsize
= offsetof(struct ctdb_control_ip_iface
,
4510 data
.dptr
= (uint8_t *)pub
;
4512 state
= ctdb_control_send(ctdb
, CTDB_CURRENT_NODE
, 0,
4513 CTDB_CONTROL_DEL_PUBLIC_IP
,
4514 0, data
, async_data
,
4516 if (state
== NULL
) {
4519 " failed sending CTDB_CONTROL_DEL_PUBLIC_IP\n"));
4523 ctdb_client_async_add(async_data
, state
);
4527 /* Compare IPs between node and file for IPs to be added */
4529 for (vnn
= ctdb
->vnn
; vnn
; vnn
= vnn
->next
) {
4530 for (i
= 0; i
< ips
->num
; i
++) {
4531 if (ctdb_same_ip(&vnn
->public_address
,
4532 &ips
->ips
[i
].addr
)) {
4533 /* IP already on node */
4537 if (i
== ips
->num
) {
4538 /* Add IP ips->ips[i] */
4539 struct ctdb_control_ip_iface
*pub
;
4540 const char *ifaces
= NULL
;
4545 ("New IP %s configured, adding it\n",
4546 ctdb_addr_to_str(&vnn
->public_address
)));
4548 uint32_t pnn
= ctdb_get_pnn(ctdb
);
4550 data
.dsize
= sizeof(pnn
);
4551 data
.dptr
= (uint8_t *)&pnn
;
4553 ret
= ctdb_client_send_message(
4555 CTDB_BROADCAST_CONNECTED
,
4556 CTDB_SRVID_REBALANCE_NODE
,
4559 DEBUG(DEBUG_WARNING
,
4560 ("Failed to send message to force node reallocation - IPs may be unbalanced\n"));
4566 ifaces
= vnn
->ifaces
[0];
4568 while (vnn
->ifaces
[iface
] != NULL
) {
4569 ifaces
= talloc_asprintf(vnn
, "%s,%s", ifaces
,
4570 vnn
->ifaces
[iface
]);
4574 len
= strlen(ifaces
) + 1;
4575 pub
= talloc_zero_size(mem_ctx
,
4576 offsetof(struct ctdb_control_ip_iface
, iface
) + len
);
4577 CTDB_NO_MEMORY(ctdb
, pub
);
4579 pub
->addr
= vnn
->public_address
;
4580 pub
->mask
= vnn
->public_netmask_bits
;
4582 memcpy(&pub
->iface
[0], ifaces
, pub
->len
);
4584 timeout
= TAKEOVER_TIMEOUT();
4586 data
.dsize
= offsetof(struct ctdb_control_ip_iface
,
4588 data
.dptr
= (uint8_t *)pub
;
4590 state
= ctdb_control_send(ctdb
, CTDB_CURRENT_NODE
, 0,
4591 CTDB_CONTROL_ADD_PUBLIC_IP
,
4592 0, data
, async_data
,
4594 if (state
== NULL
) {
4597 " failed sending CTDB_CONTROL_ADD_PUBLIC_IP\n"));
4601 ctdb_client_async_add(async_data
, state
);
4605 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
4606 DEBUG(DEBUG_ERR
,(__location__
" Add/delete IPs failed\n"));
4610 talloc_free(mem_ctx
);
4614 talloc_free(mem_ctx
);
4618 /* This control is sent to force the node to re-read the public addresses file
4619 and drop any addresses we should nnot longer host, and add new addresses
4620 that we are now able to host
4622 int32_t ctdb_control_reload_public_ips(struct ctdb_context
*ctdb
, struct ctdb_req_control
*c
, bool *async_reply
)
4624 struct ctdb_reloadips_handle
*h
;
4625 pid_t parent
= getpid();
4627 if (ctdb
->reload_ips
!= NULL
) {
4628 talloc_free(ctdb
->reload_ips
);
4629 ctdb
->reload_ips
= NULL
;
4632 h
= talloc(ctdb
, struct ctdb_reloadips_handle
);
4633 CTDB_NO_MEMORY(ctdb
, h
);
4638 if (pipe(h
->fd
) == -1) {
4639 DEBUG(DEBUG_ERR
,("Failed to create pipe for ctdb_freeze_lock\n"));
4644 h
->child
= ctdb_fork(ctdb
);
4645 if (h
->child
== (pid_t
)-1) {
4646 DEBUG(DEBUG_ERR
, ("Failed to fork a child for reloadips\n"));
4654 if (h
->child
== 0) {
4655 signed char res
= 0;
4658 debug_extra
= talloc_asprintf(NULL
, "reloadips:");
4660 ctdb_set_process_name("ctdb_reloadips");
4661 if (switch_from_server_to_client(ctdb
, "reloadips-child") != 0) {
4662 DEBUG(DEBUG_CRIT
,("ERROR: Failed to switch reloadips child into client mode\n"));
4665 res
= ctdb_reloadips_child(ctdb
);
4667 DEBUG(DEBUG_ERR
,("Failed to reload ips on local node\n"));
4671 sys_write(h
->fd
[1], &res
, 1);
4672 /* make sure we die when our parent dies */
4673 while (ctdb_kill(ctdb
, parent
, 0) == 0 || errno
!= ESRCH
) {
4679 h
->c
= talloc_steal(h
, c
);
4682 set_close_on_exec(h
->fd
[0]);
4684 talloc_set_destructor(h
, ctdb_reloadips_destructor
);
4687 h
->fde
= event_add_fd(ctdb
->ev
, h
, h
->fd
[0],
4688 EVENT_FD_READ
, ctdb_reloadips_child_handler
,
4690 tevent_fd_set_auto_close(h
->fde
);
4692 event_add_timed(ctdb
->ev
, h
,
4693 timeval_current_ofs(120, 0),
4694 ctdb_reloadips_timeout_event
, h
);
4696 /* we reply later */
4697 *async_reply
= true;