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. */
43 struct ctdb_iface
*prev
, *next
;
49 static const char *ctdb_vnn_iface_string(const struct ctdb_vnn
*vnn
)
52 return vnn
->iface
->name
;
58 static int ctdb_add_local_iface(struct ctdb_context
*ctdb
, const char *iface
)
62 /* Verify that we dont have an entry for this ip yet */
63 for (i
=ctdb
->ifaces
;i
;i
=i
->next
) {
64 if (strcmp(i
->name
, iface
) == 0) {
69 /* create a new structure for this interface */
70 i
= talloc_zero(ctdb
, struct ctdb_iface
);
71 CTDB_NO_MEMORY_FATAL(ctdb
, i
);
72 i
->name
= talloc_strdup(i
, iface
);
73 CTDB_NO_MEMORY(ctdb
, i
->name
);
75 * If link_up defaults to true then IPs can be allocated to a
76 * node during the first recovery. However, then an interface
77 * could have its link marked down during the startup event,
78 * causing the IP to move almost immediately. If link_up
79 * defaults to false then, during normal operation, IPs added
80 * to a new interface can't be assigned until a monitor cycle
81 * has occurred and marked the new interfaces up. This makes
82 * IP allocation unpredictable. The following is a neat
83 * compromise: early in startup link_up defaults to false, so
84 * IPs can't be assigned, and after startup IPs can be
85 * assigned immediately.
87 i
->link_up
= (ctdb
->runstate
== CTDB_RUNSTATE_RUNNING
);
89 DLIST_ADD(ctdb
->ifaces
, i
);
94 static bool vnn_has_interface_with_name(struct ctdb_vnn
*vnn
,
99 for (n
= 0; vnn
->ifaces
[n
] != NULL
; n
++) {
100 if (strcmp(name
, vnn
->ifaces
[n
]) == 0) {
108 /* If any interfaces now have no possible IPs then delete them. This
109 * implementation is naive (i.e. simple) rather than clever
110 * (i.e. complex). Given that this is run on delip and that operation
111 * is rare, this doesn't need to be efficient - it needs to be
112 * foolproof. One alternative is reference counting, where the logic
113 * is distributed and can, therefore, be broken in multiple places.
114 * Another alternative is to build a red-black tree of interfaces that
115 * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
116 * once) and then walking ctdb->ifaces once and deleting those not in
117 * the tree. Let's go to one of those if the naive implementation
118 * causes problems... :-)
120 static void ctdb_remove_orphaned_ifaces(struct ctdb_context
*ctdb
,
121 struct ctdb_vnn
*vnn
,
124 struct ctdb_iface
*i
;
126 /* For each interface, check if there's an IP using it. */
127 for(i
=ctdb
->ifaces
; i
; i
=i
->next
) {
131 /* Only consider interfaces named in the given VNN. */
132 if (!vnn_has_interface_with_name(vnn
, i
->name
)) {
136 /* Is the "single IP" on this interface? */
137 if ((ctdb
->single_ip_vnn
!= NULL
) &&
138 (ctdb
->single_ip_vnn
->ifaces
[0] != NULL
) &&
139 (strcmp(i
->name
, ctdb
->single_ip_vnn
->ifaces
[0]) == 0)) {
140 /* Found, next interface please... */
143 /* Search for a vnn with this interface. */
145 for (tv
=ctdb
->vnn
; tv
; tv
=tv
->next
) {
146 if (vnn_has_interface_with_name(tv
, i
->name
)) {
153 /* None of the VNNs are using this interface. */
154 DLIST_REMOVE(ctdb
->ifaces
, i
);
155 /* Caller will free mem_ctx when convenient. */
156 talloc_steal(mem_ctx
, i
);
162 static struct ctdb_iface
*ctdb_find_iface(struct ctdb_context
*ctdb
,
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
->iface
&& vnn
->iface
->link_up
) {
267 for (i
=0; vnn
->ifaces
[i
]; i
++) {
268 struct ctdb_iface
*cur
;
270 cur
= ctdb_find_iface(ctdb
, vnn
->ifaces
[i
]);
283 struct ctdb_takeover_arp
{
284 struct ctdb_context
*ctdb
;
287 struct ctdb_tcp_array
*tcparray
;
288 struct ctdb_vnn
*vnn
;
293 lists of tcp endpoints
295 struct ctdb_tcp_list
{
296 struct ctdb_tcp_list
*prev
, *next
;
297 struct ctdb_tcp_connection connection
;
301 list of clients to kill on IP release
303 struct ctdb_client_ip
{
304 struct ctdb_client_ip
*prev
, *next
;
305 struct ctdb_context
*ctdb
;
312 send a gratuitous arp
314 static void ctdb_control_send_arp(struct event_context
*ev
, struct timed_event
*te
,
315 struct timeval t
, void *private_data
)
317 struct ctdb_takeover_arp
*arp
= talloc_get_type(private_data
,
318 struct ctdb_takeover_arp
);
320 struct ctdb_tcp_array
*tcparray
;
321 const char *iface
= ctdb_vnn_iface_string(arp
->vnn
);
323 ret
= ctdb_sys_send_arp(&arp
->addr
, iface
);
325 DEBUG(DEBUG_CRIT
,(__location__
" sending of arp failed on iface '%s' (%s)\n",
326 iface
, strerror(errno
)));
329 tcparray
= arp
->tcparray
;
331 for (i
=0;i
<tcparray
->num
;i
++) {
332 struct ctdb_tcp_connection
*tcon
;
334 tcon
= &tcparray
->connections
[i
];
335 DEBUG(DEBUG_INFO
,("sending tcp tickle ack for %u->%s:%u\n",
336 (unsigned)ntohs(tcon
->dst_addr
.ip
.sin_port
),
337 ctdb_addr_to_str(&tcon
->src_addr
),
338 (unsigned)ntohs(tcon
->src_addr
.ip
.sin_port
)));
339 ret
= ctdb_sys_send_tcp(
344 DEBUG(DEBUG_CRIT
,(__location__
" Failed to send tcp tickle ack for %s\n",
345 ctdb_addr_to_str(&tcon
->src_addr
)));
352 if (arp
->count
== CTDB_ARP_REPEAT
) {
357 event_add_timed(arp
->ctdb
->ev
, arp
->vnn
->takeover_ctx
,
358 timeval_current_ofs(CTDB_ARP_INTERVAL
, 100000),
359 ctdb_control_send_arp
, arp
);
362 static int32_t ctdb_announce_vnn_iface(struct ctdb_context
*ctdb
,
363 struct ctdb_vnn
*vnn
)
365 struct ctdb_takeover_arp
*arp
;
366 struct ctdb_tcp_array
*tcparray
;
368 if (!vnn
->takeover_ctx
) {
369 vnn
->takeover_ctx
= talloc_new(vnn
);
370 if (!vnn
->takeover_ctx
) {
375 arp
= talloc_zero(vnn
->takeover_ctx
, struct ctdb_takeover_arp
);
381 arp
->addr
= vnn
->public_address
;
384 tcparray
= vnn
->tcp_array
;
386 /* add all of the known tcp connections for this IP to the
387 list of tcp connections to send tickle acks for */
388 arp
->tcparray
= talloc_steal(arp
, tcparray
);
390 vnn
->tcp_array
= NULL
;
391 vnn
->tcp_update_needed
= true;
394 event_add_timed(arp
->ctdb
->ev
, vnn
->takeover_ctx
,
395 timeval_zero(), ctdb_control_send_arp
, arp
);
400 struct takeover_callback_state
{
401 struct ctdb_req_control
*c
;
402 ctdb_sock_addr
*addr
;
403 struct ctdb_vnn
*vnn
;
406 struct ctdb_do_takeip_state
{
407 struct ctdb_req_control
*c
;
408 struct ctdb_vnn
*vnn
;
412 called when takeip event finishes
414 static void ctdb_do_takeip_callback(struct ctdb_context
*ctdb
, int status
,
417 struct ctdb_do_takeip_state
*state
=
418 talloc_get_type(private_data
, struct ctdb_do_takeip_state
);
423 struct ctdb_node
*node
= ctdb
->nodes
[ctdb
->pnn
];
425 if (status
== -ETIME
) {
428 DEBUG(DEBUG_ERR
,(__location__
" Failed to takeover IP %s on interface %s\n",
429 ctdb_addr_to_str(&state
->vnn
->public_address
),
430 ctdb_vnn_iface_string(state
->vnn
)));
431 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
433 node
->flags
|= NODE_FLAGS_UNHEALTHY
;
438 if (ctdb
->do_checkpublicip
) {
440 ret
= ctdb_announce_vnn_iface(ctdb
, state
->vnn
);
442 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, -1, NULL
);
449 data
.dptr
= (uint8_t *)ctdb_addr_to_str(&state
->vnn
->public_address
);
450 data
.dsize
= strlen((char *)data
.dptr
) + 1;
451 DEBUG(DEBUG_INFO
,(__location__
" sending TAKE_IP for '%s'\n", data
.dptr
));
453 ctdb_daemon_send_message(ctdb
, ctdb
->pnn
, CTDB_SRVID_TAKE_IP
, data
);
456 /* the control succeeded */
457 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 0, NULL
);
462 static int ctdb_takeip_destructor(struct ctdb_do_takeip_state
*state
)
464 state
->vnn
->update_in_flight
= false;
469 take over an ip address
471 static int32_t ctdb_do_takeip(struct ctdb_context
*ctdb
,
472 struct ctdb_req_control
*c
,
473 struct ctdb_vnn
*vnn
)
476 struct ctdb_do_takeip_state
*state
;
478 if (vnn
->update_in_flight
) {
479 DEBUG(DEBUG_NOTICE
,("Takeover of IP %s/%u rejected "
480 "update for this IP already in flight\n",
481 ctdb_addr_to_str(&vnn
->public_address
),
482 vnn
->public_netmask_bits
));
486 ret
= ctdb_vnn_assign_iface(ctdb
, vnn
);
488 DEBUG(DEBUG_ERR
,("Takeover of IP %s/%u failed to "
489 "assign a usable interface\n",
490 ctdb_addr_to_str(&vnn
->public_address
),
491 vnn
->public_netmask_bits
));
495 state
= talloc(vnn
, struct ctdb_do_takeip_state
);
496 CTDB_NO_MEMORY(ctdb
, state
);
498 state
->c
= talloc_steal(ctdb
, c
);
501 vnn
->update_in_flight
= true;
502 talloc_set_destructor(state
, ctdb_takeip_destructor
);
504 DEBUG(DEBUG_NOTICE
,("Takeover of IP %s/%u on interface %s\n",
505 ctdb_addr_to_str(&vnn
->public_address
),
506 vnn
->public_netmask_bits
,
507 ctdb_vnn_iface_string(vnn
)));
509 ret
= ctdb_event_script_callback(ctdb
,
511 ctdb_do_takeip_callback
,
515 ctdb_vnn_iface_string(vnn
),
516 ctdb_addr_to_str(&vnn
->public_address
),
517 vnn
->public_netmask_bits
);
520 DEBUG(DEBUG_ERR
,(__location__
" Failed to takeover IP %s on interface %s\n",
521 ctdb_addr_to_str(&vnn
->public_address
),
522 ctdb_vnn_iface_string(vnn
)));
530 struct ctdb_do_updateip_state
{
531 struct ctdb_req_control
*c
;
532 struct ctdb_iface
*old
;
533 struct ctdb_vnn
*vnn
;
537 called when updateip event finishes
539 static void ctdb_do_updateip_callback(struct ctdb_context
*ctdb
, int status
,
542 struct ctdb_do_updateip_state
*state
=
543 talloc_get_type(private_data
, struct ctdb_do_updateip_state
);
547 if (status
== -ETIME
) {
550 DEBUG(DEBUG_ERR
,(__location__
" Failed to move IP %s from interface %s to %s\n",
551 ctdb_addr_to_str(&state
->vnn
->public_address
),
553 ctdb_vnn_iface_string(state
->vnn
)));
556 * All we can do is reset the old interface
557 * and let the next run fix it
559 ctdb_vnn_unassign_iface(ctdb
, state
->vnn
);
560 state
->vnn
->iface
= state
->old
;
561 state
->vnn
->iface
->references
++;
563 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
568 if (ctdb
->do_checkpublicip
) {
570 ret
= ctdb_announce_vnn_iface(ctdb
, state
->vnn
);
572 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, -1, NULL
);
579 /* the control succeeded */
580 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 0, NULL
);
585 static int ctdb_updateip_destructor(struct ctdb_do_updateip_state
*state
)
587 state
->vnn
->update_in_flight
= false;
592 update (move) an ip address
594 static int32_t ctdb_do_updateip(struct ctdb_context
*ctdb
,
595 struct ctdb_req_control
*c
,
596 struct ctdb_vnn
*vnn
)
599 struct ctdb_do_updateip_state
*state
;
600 struct ctdb_iface
*old
= vnn
->iface
;
601 const char *new_name
;
603 if (vnn
->update_in_flight
) {
604 DEBUG(DEBUG_NOTICE
,("Update of IP %s/%u rejected "
605 "update for this IP already in flight\n",
606 ctdb_addr_to_str(&vnn
->public_address
),
607 vnn
->public_netmask_bits
));
611 ctdb_vnn_unassign_iface(ctdb
, vnn
);
612 ret
= ctdb_vnn_assign_iface(ctdb
, vnn
);
614 DEBUG(DEBUG_ERR
,("update of IP %s/%u failed to "
615 "assin a usable interface (old iface '%s')\n",
616 ctdb_addr_to_str(&vnn
->public_address
),
617 vnn
->public_netmask_bits
,
622 new_name
= ctdb_vnn_iface_string(vnn
);
623 if (old
->name
!= NULL
&& new_name
!= NULL
&& !strcmp(old
->name
, new_name
)) {
624 /* A benign update from one interface onto itself.
625 * no need to run the eventscripts in this case, just return
628 ctdb_request_control_reply(ctdb
, c
, NULL
, 0, NULL
);
632 state
= talloc(vnn
, struct ctdb_do_updateip_state
);
633 CTDB_NO_MEMORY(ctdb
, state
);
635 state
->c
= talloc_steal(ctdb
, c
);
639 vnn
->update_in_flight
= true;
640 talloc_set_destructor(state
, ctdb_updateip_destructor
);
642 DEBUG(DEBUG_NOTICE
,("Update of IP %s/%u from "
643 "interface %s to %s\n",
644 ctdb_addr_to_str(&vnn
->public_address
),
645 vnn
->public_netmask_bits
,
649 ret
= ctdb_event_script_callback(ctdb
,
651 ctdb_do_updateip_callback
,
653 CTDB_EVENT_UPDATE_IP
,
657 ctdb_addr_to_str(&vnn
->public_address
),
658 vnn
->public_netmask_bits
);
660 DEBUG(DEBUG_ERR
,(__location__
" Failed update IP %s from interface %s to %s\n",
661 ctdb_addr_to_str(&vnn
->public_address
),
662 old
->name
, new_name
));
671 Find the vnn of the node that has a public ip address
672 returns -1 if the address is not known as a public address
674 static struct ctdb_vnn
*find_public_ip_vnn(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
676 struct ctdb_vnn
*vnn
;
678 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
679 if (ctdb_same_ip(&vnn
->public_address
, addr
)) {
688 take over an ip address
690 int32_t ctdb_control_takeover_ip(struct ctdb_context
*ctdb
,
691 struct ctdb_req_control
*c
,
696 struct ctdb_public_ip
*pip
= (struct ctdb_public_ip
*)indata
.dptr
;
697 struct ctdb_vnn
*vnn
;
698 bool have_ip
= false;
699 bool do_updateip
= false;
700 bool do_takeip
= false;
701 struct ctdb_iface
*best_iface
= NULL
;
703 if (pip
->pnn
!= ctdb
->pnn
) {
704 DEBUG(DEBUG_ERR
,(__location__
" takeoverip called for an ip '%s' "
705 "with pnn %d, but we're node %d\n",
706 ctdb_addr_to_str(&pip
->addr
),
707 pip
->pnn
, ctdb
->pnn
));
711 /* update out vnn list */
712 vnn
= find_public_ip_vnn(ctdb
, &pip
->addr
);
714 DEBUG(DEBUG_INFO
,("takeoverip called for an ip '%s' that is not a public address\n",
715 ctdb_addr_to_str(&pip
->addr
)));
719 if (ctdb
->do_checkpublicip
) {
720 have_ip
= ctdb_sys_have_ip(&pip
->addr
);
722 best_iface
= ctdb_vnn_best_iface(ctdb
, vnn
);
723 if (best_iface
== NULL
) {
724 DEBUG(DEBUG_ERR
,("takeoverip of IP %s/%u failed to find"
725 "a usable interface (old %s, have_ip %d)\n",
726 ctdb_addr_to_str(&vnn
->public_address
),
727 vnn
->public_netmask_bits
,
728 ctdb_vnn_iface_string(vnn
),
733 if (vnn
->iface
== NULL
&& vnn
->pnn
== -1 && have_ip
&& best_iface
!= NULL
) {
734 DEBUG(DEBUG_ERR
,("Taking over newly created ip\n"));
739 if (vnn
->iface
== NULL
&& have_ip
) {
740 DEBUG(DEBUG_CRIT
,(__location__
" takeoverip of IP %s is known to the kernel, "
741 "but we have no interface assigned, has someone manually configured it? Ignore for now.\n",
742 ctdb_addr_to_str(&vnn
->public_address
)));
746 if (vnn
->pnn
!= ctdb
->pnn
&& have_ip
&& vnn
->pnn
!= -1) {
747 DEBUG(DEBUG_CRIT
,(__location__
" takeoverip of IP %s is known to the kernel, "
748 "and we have it on iface[%s], but it was assigned to node %d"
749 "and we are node %d, banning ourself\n",
750 ctdb_addr_to_str(&vnn
->public_address
),
751 ctdb_vnn_iface_string(vnn
), vnn
->pnn
, ctdb
->pnn
));
756 if (vnn
->pnn
== -1 && have_ip
) {
757 vnn
->pnn
= ctdb
->pnn
;
758 DEBUG(DEBUG_CRIT
,(__location__
" takeoverip of IP %s is known to the kernel, "
759 "and we already have it on iface[%s], update local daemon\n",
760 ctdb_addr_to_str(&vnn
->public_address
),
761 ctdb_vnn_iface_string(vnn
)));
766 if (vnn
->iface
!= best_iface
) {
767 if (!vnn
->iface
->link_up
) {
769 } else if (vnn
->iface
->references
> (best_iface
->references
+ 1)) {
770 /* only move when the rebalance gains something */
778 ctdb_vnn_unassign_iface(ctdb
, vnn
);
785 ret
= ctdb_do_takeip(ctdb
, c
, vnn
);
789 } else if (do_updateip
) {
790 ret
= ctdb_do_updateip(ctdb
, c
, vnn
);
796 * The interface is up and the kernel known the ip
799 DEBUG(DEBUG_INFO
,("Redundant takeover of IP %s/%u on interface %s (ip already held)\n",
800 ctdb_addr_to_str(&pip
->addr
),
801 vnn
->public_netmask_bits
,
802 ctdb_vnn_iface_string(vnn
)));
806 /* tell ctdb_control.c that we will be replying asynchronously */
813 takeover an ip address old v4 style
815 int32_t ctdb_control_takeover_ipv4(struct ctdb_context
*ctdb
,
816 struct ctdb_req_control
*c
,
822 data
.dsize
= sizeof(struct ctdb_public_ip
);
823 data
.dptr
= (uint8_t *)talloc_zero(c
, struct ctdb_public_ip
);
824 CTDB_NO_MEMORY(ctdb
, data
.dptr
);
826 memcpy(data
.dptr
, indata
.dptr
, indata
.dsize
);
827 return ctdb_control_takeover_ip(ctdb
, c
, data
, async_reply
);
831 kill any clients that are registered with a IP that is being released
833 static void release_kill_clients(struct ctdb_context
*ctdb
, ctdb_sock_addr
*addr
)
835 struct ctdb_client_ip
*ip
;
837 DEBUG(DEBUG_INFO
,("release_kill_clients for ip %s\n",
838 ctdb_addr_to_str(addr
)));
840 for (ip
=ctdb
->client_ip_list
; ip
; ip
=ip
->next
) {
841 ctdb_sock_addr tmp_addr
;
844 DEBUG(DEBUG_INFO
,("checking for client %u with IP %s\n",
846 ctdb_addr_to_str(&ip
->addr
)));
848 if (ctdb_same_ip(&tmp_addr
, addr
)) {
849 struct ctdb_client
*client
= ctdb_reqid_find(ctdb
,
852 DEBUG(DEBUG_INFO
,("matched client %u with IP %s and pid %u\n",
854 ctdb_addr_to_str(&ip
->addr
),
857 if (client
->pid
!= 0) {
858 DEBUG(DEBUG_INFO
,(__location__
" Killing client pid %u for IP %s on client_id %u\n",
859 (unsigned)client
->pid
,
860 ctdb_addr_to_str(addr
),
862 kill(client
->pid
, SIGKILL
);
869 called when releaseip event finishes
871 static void release_ip_callback(struct ctdb_context
*ctdb
, int status
,
874 struct takeover_callback_state
*state
=
875 talloc_get_type(private_data
, struct takeover_callback_state
);
878 if (status
== -ETIME
) {
882 if (ctdb
->do_checkpublicip
&& ctdb_sys_have_ip(state
->addr
)) {
883 DEBUG(DEBUG_ERR
, ("IP %s still hosted during release IP callback, failing\n",
884 ctdb_addr_to_str(state
->addr
)));
885 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, -1, NULL
);
890 /* send a message to all clients of this node telling them
891 that the cluster has been reconfigured and they should
892 release any sockets on this IP */
893 data
.dptr
= (uint8_t *)talloc_strdup(state
, ctdb_addr_to_str(state
->addr
));
894 CTDB_NO_MEMORY_VOID(ctdb
, data
.dptr
);
895 data
.dsize
= strlen((char *)data
.dptr
)+1;
897 DEBUG(DEBUG_INFO
,(__location__
" sending RELEASE_IP for '%s'\n", data
.dptr
));
899 ctdb_daemon_send_message(ctdb
, ctdb
->pnn
, CTDB_SRVID_RELEASE_IP
, data
);
901 /* kill clients that have registered with this IP */
902 release_kill_clients(ctdb
, state
->addr
);
904 ctdb_vnn_unassign_iface(ctdb
, state
->vnn
);
906 /* the control succeeded */
907 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, 0, NULL
);
911 static int ctdb_releaseip_destructor(struct takeover_callback_state
*state
)
913 state
->vnn
->update_in_flight
= false;
918 release an ip address
920 int32_t ctdb_control_release_ip(struct ctdb_context
*ctdb
,
921 struct ctdb_req_control
*c
,
926 struct takeover_callback_state
*state
;
927 struct ctdb_public_ip
*pip
= (struct ctdb_public_ip
*)indata
.dptr
;
928 struct ctdb_vnn
*vnn
;
931 /* update our vnn list */
932 vnn
= find_public_ip_vnn(ctdb
, &pip
->addr
);
934 DEBUG(DEBUG_INFO
,("releaseip called for an ip '%s' that is not a public address\n",
935 ctdb_addr_to_str(&pip
->addr
)));
940 /* stop any previous arps */
941 talloc_free(vnn
->takeover_ctx
);
942 vnn
->takeover_ctx
= NULL
;
944 /* Some ctdb tool commands (e.g. moveip, rebalanceip) send
945 * lazy multicast to drop an IP from any node that isn't the
946 * intended new node. The following causes makes ctdbd ignore
947 * a release for any address it doesn't host.
949 if (ctdb
->do_checkpublicip
) {
950 if (!ctdb_sys_have_ip(&pip
->addr
)) {
951 DEBUG(DEBUG_DEBUG
,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
952 ctdb_addr_to_str(&pip
->addr
),
953 vnn
->public_netmask_bits
,
954 ctdb_vnn_iface_string(vnn
)));
955 ctdb_vnn_unassign_iface(ctdb
, vnn
);
959 if (vnn
->iface
== NULL
) {
960 DEBUG(DEBUG_DEBUG
,("Redundant release of IP %s/%u (ip not held)\n",
961 ctdb_addr_to_str(&pip
->addr
),
962 vnn
->public_netmask_bits
));
967 /* There is a potential race between take_ip and us because we
968 * update the VNN via a callback that run when the
969 * eventscripts have been run. Avoid the race by allowing one
970 * update to be in flight at a time.
972 if (vnn
->update_in_flight
) {
973 DEBUG(DEBUG_NOTICE
,("Release of IP %s/%u rejected "
974 "update for this IP already in flight\n",
975 ctdb_addr_to_str(&vnn
->public_address
),
976 vnn
->public_netmask_bits
));
980 if (ctdb
->do_checkpublicip
) {
981 iface
= ctdb_sys_find_ifname(&pip
->addr
);
983 DEBUG(DEBUG_ERR
, ("Could not find which interface the ip address is hosted on. can not release it\n"));
986 if (vnn
->iface
== NULL
) {
988 ("Public IP %s is hosted on interface %s but we have no VNN\n",
989 ctdb_addr_to_str(&pip
->addr
),
991 } else if (strcmp(iface
, ctdb_vnn_iface_string(vnn
)) != 0) {
993 ("Public IP %s is hosted on inteterface %s but VNN says %s\n",
994 ctdb_addr_to_str(&pip
->addr
),
996 ctdb_vnn_iface_string(vnn
)));
997 /* Should we fix vnn->iface? If we do, what
998 * happens to reference counts?
1002 iface
= strdup(ctdb_vnn_iface_string(vnn
));
1005 DEBUG(DEBUG_NOTICE
,("Release of IP %s/%u on interface %s node:%d\n",
1006 ctdb_addr_to_str(&pip
->addr
),
1007 vnn
->public_netmask_bits
,
1011 state
= talloc(ctdb
, struct takeover_callback_state
);
1012 CTDB_NO_MEMORY(ctdb
, state
);
1014 state
->c
= talloc_steal(state
, c
);
1015 state
->addr
= talloc(state
, ctdb_sock_addr
);
1016 CTDB_NO_MEMORY(ctdb
, state
->addr
);
1017 *state
->addr
= pip
->addr
;
1020 vnn
->update_in_flight
= true;
1021 talloc_set_destructor(state
, ctdb_releaseip_destructor
);
1023 ret
= ctdb_event_script_callback(ctdb
,
1024 state
, release_ip_callback
, state
,
1025 CTDB_EVENT_RELEASE_IP
,
1028 ctdb_addr_to_str(&pip
->addr
),
1029 vnn
->public_netmask_bits
);
1032 DEBUG(DEBUG_ERR
,(__location__
" Failed to release IP %s on interface %s\n",
1033 ctdb_addr_to_str(&pip
->addr
),
1034 ctdb_vnn_iface_string(vnn
)));
1039 /* tell the control that we will be reply asynchronously */
1040 *async_reply
= true;
1045 release an ip address old v4 style
1047 int32_t ctdb_control_release_ipv4(struct ctdb_context
*ctdb
,
1048 struct ctdb_req_control
*c
,
1054 data
.dsize
= sizeof(struct ctdb_public_ip
);
1055 data
.dptr
= (uint8_t *)talloc_zero(c
, struct ctdb_public_ip
);
1056 CTDB_NO_MEMORY(ctdb
, data
.dptr
);
1058 memcpy(data
.dptr
, indata
.dptr
, indata
.dsize
);
1059 return ctdb_control_release_ip(ctdb
, c
, data
, async_reply
);
1063 static int ctdb_add_public_address(struct ctdb_context
*ctdb
,
1064 ctdb_sock_addr
*addr
,
1065 unsigned mask
, const char *ifaces
,
1068 struct ctdb_vnn
*vnn
;
1075 tmp
= strdup(ifaces
);
1076 for (iface
= strtok(tmp
, ","); iface
; iface
= strtok(NULL
, ",")) {
1077 if (!ctdb_sys_check_iface_exists(iface
)) {
1078 DEBUG(DEBUG_CRIT
,("Interface %s does not exist. Can not add public-address : %s\n", iface
, ctdb_addr_to_str(addr
)));
1085 /* Verify that we dont have an entry for this ip yet */
1086 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
1087 if (ctdb_same_sockaddr(addr
, &vnn
->public_address
)) {
1088 DEBUG(DEBUG_CRIT
,("Same ip '%s' specified multiple times in the public address list \n",
1089 ctdb_addr_to_str(addr
)));
1094 /* create a new vnn structure for this ip address */
1095 vnn
= talloc_zero(ctdb
, struct ctdb_vnn
);
1096 CTDB_NO_MEMORY_FATAL(ctdb
, vnn
);
1097 vnn
->ifaces
= talloc_array(vnn
, const char *, num
+ 2);
1098 tmp
= talloc_strdup(vnn
, ifaces
);
1099 CTDB_NO_MEMORY_FATAL(ctdb
, tmp
);
1100 for (iface
= strtok(tmp
, ","); iface
; iface
= strtok(NULL
, ",")) {
1101 vnn
->ifaces
= talloc_realloc(vnn
, vnn
->ifaces
, const char *, num
+ 2);
1102 CTDB_NO_MEMORY_FATAL(ctdb
, vnn
->ifaces
);
1103 vnn
->ifaces
[num
] = talloc_strdup(vnn
, iface
);
1104 CTDB_NO_MEMORY_FATAL(ctdb
, vnn
->ifaces
[num
]);
1108 vnn
->ifaces
[num
] = NULL
;
1109 vnn
->public_address
= *addr
;
1110 vnn
->public_netmask_bits
= mask
;
1112 if (check_address
) {
1113 if (ctdb_sys_have_ip(addr
)) {
1114 DEBUG(DEBUG_ERR
,("We are already hosting public address '%s'. setting PNN to ourself:%d\n", ctdb_addr_to_str(addr
), ctdb
->pnn
));
1115 vnn
->pnn
= ctdb
->pnn
;
1119 for (i
=0; vnn
->ifaces
[i
]; i
++) {
1120 ret
= ctdb_add_local_iface(ctdb
, vnn
->ifaces
[i
]);
1122 DEBUG(DEBUG_CRIT
, (__location__
" failed to add iface[%s] "
1123 "for public_address[%s]\n",
1124 vnn
->ifaces
[i
], ctdb_addr_to_str(addr
)));
1130 DLIST_ADD(ctdb
->vnn
, vnn
);
1135 static void ctdb_check_interfaces_event(struct event_context
*ev
, struct timed_event
*te
,
1136 struct timeval t
, void *private_data
)
1138 struct ctdb_context
*ctdb
= talloc_get_type(private_data
,
1139 struct ctdb_context
);
1140 struct ctdb_vnn
*vnn
;
1142 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
1145 for (i
=0; vnn
->ifaces
[i
] != NULL
; i
++) {
1146 if (!ctdb_sys_check_iface_exists(vnn
->ifaces
[i
])) {
1147 DEBUG(DEBUG_CRIT
,("Interface %s does not exist but is used by public ip %s\n",
1149 ctdb_addr_to_str(&vnn
->public_address
)));
1154 event_add_timed(ctdb
->ev
, ctdb
->check_public_ifaces_ctx
,
1155 timeval_current_ofs(30, 0),
1156 ctdb_check_interfaces_event
, ctdb
);
1160 int ctdb_start_monitoring_interfaces(struct ctdb_context
*ctdb
)
1162 if (ctdb
->check_public_ifaces_ctx
!= NULL
) {
1163 talloc_free(ctdb
->check_public_ifaces_ctx
);
1164 ctdb
->check_public_ifaces_ctx
= NULL
;
1167 ctdb
->check_public_ifaces_ctx
= talloc_new(ctdb
);
1168 if (ctdb
->check_public_ifaces_ctx
== NULL
) {
1169 ctdb_fatal(ctdb
, "failed to allocate context for checking interfaces");
1172 event_add_timed(ctdb
->ev
, ctdb
->check_public_ifaces_ctx
,
1173 timeval_current_ofs(30, 0),
1174 ctdb_check_interfaces_event
, ctdb
);
1181 setup the public address lists from a file
1183 int ctdb_set_public_addresses(struct ctdb_context
*ctdb
, bool check_addresses
)
1189 lines
= file_lines_load(ctdb
->public_addresses_file
, &nlines
, ctdb
);
1190 if (lines
== NULL
) {
1191 ctdb_set_error(ctdb
, "Failed to load public address list '%s'\n", ctdb
->public_addresses_file
);
1194 while (nlines
> 0 && strcmp(lines
[nlines
-1], "") == 0) {
1198 for (i
=0;i
<nlines
;i
++) {
1200 ctdb_sock_addr addr
;
1201 const char *addrstr
;
1206 while ((*line
== ' ') || (*line
== '\t')) {
1212 if (strcmp(line
, "") == 0) {
1215 tok
= strtok(line
, " \t");
1217 tok
= strtok(NULL
, " \t");
1219 if (NULL
== ctdb
->default_public_interface
) {
1220 DEBUG(DEBUG_CRIT
,("No default public interface and no interface specified at line %u of public address list\n",
1225 ifaces
= ctdb
->default_public_interface
;
1230 if (!addrstr
|| !parse_ip_mask(addrstr
, ifaces
, &addr
, &mask
)) {
1231 DEBUG(DEBUG_CRIT
,("Badly formed line %u in public address list\n", i
+1));
1235 if (ctdb_add_public_address(ctdb
, &addr
, mask
, ifaces
, check_addresses
)) {
1236 DEBUG(DEBUG_CRIT
,("Failed to add line %u to the public address list\n", i
+1));
1247 int ctdb_set_single_public_ip(struct ctdb_context
*ctdb
,
1251 struct ctdb_vnn
*svnn
;
1252 struct ctdb_iface
*cur
= NULL
;
1256 svnn
= talloc_zero(ctdb
, struct ctdb_vnn
);
1257 CTDB_NO_MEMORY(ctdb
, svnn
);
1259 svnn
->ifaces
= talloc_array(svnn
, const char *, 2);
1260 CTDB_NO_MEMORY(ctdb
, svnn
->ifaces
);
1261 svnn
->ifaces
[0] = talloc_strdup(svnn
->ifaces
, iface
);
1262 CTDB_NO_MEMORY(ctdb
, svnn
->ifaces
[0]);
1263 svnn
->ifaces
[1] = NULL
;
1265 ok
= parse_ip(ip
, iface
, 0, &svnn
->public_address
);
1271 ret
= ctdb_add_local_iface(ctdb
, svnn
->ifaces
[0]);
1273 DEBUG(DEBUG_CRIT
, (__location__
" failed to add iface[%s] "
1274 "for single_ip[%s]\n",
1276 ctdb_addr_to_str(&svnn
->public_address
)));
1281 /* assume the single public ip interface is initially "good" */
1282 cur
= ctdb_find_iface(ctdb
, iface
);
1284 DEBUG(DEBUG_CRIT
,("Can not find public interface %s used by --single-public-ip", iface
));
1287 cur
->link_up
= true;
1289 ret
= ctdb_vnn_assign_iface(ctdb
, svnn
);
1295 ctdb
->single_ip_vnn
= svnn
;
1299 struct ctdb_public_ip_list
{
1300 struct ctdb_public_ip_list
*next
;
1302 ctdb_sock_addr addr
;
1305 /* Given a physical node, return the number of
1306 public addresses that is currently assigned to this node.
1308 static int node_ip_coverage(struct ctdb_context
*ctdb
,
1310 struct ctdb_public_ip_list
*ips
)
1314 for (;ips
;ips
=ips
->next
) {
1315 if (ips
->pnn
== pnn
) {
1323 /* Can the given node host the given IP: is the public IP known to the
1324 * node and is NOIPHOST unset?
1326 static bool can_node_host_ip(struct ctdb_context
*ctdb
, int32_t pnn
,
1327 struct ctdb_ipflags ipflags
,
1328 struct ctdb_public_ip_list
*ip
)
1330 struct ctdb_all_public_ips
*public_ips
;
1333 if (ipflags
.noiphost
) {
1337 public_ips
= ctdb
->nodes
[pnn
]->available_public_ips
;
1339 if (public_ips
== NULL
) {
1343 for (i
=0; i
<public_ips
->num
; i
++) {
1344 if (ctdb_same_ip(&ip
->addr
, &public_ips
->ips
[i
].addr
)) {
1345 /* yes, this node can serve this public ip */
1353 static bool can_node_takeover_ip(struct ctdb_context
*ctdb
, int32_t pnn
,
1354 struct ctdb_ipflags ipflags
,
1355 struct ctdb_public_ip_list
*ip
)
1357 if (ipflags
.noiptakeover
) {
1361 return can_node_host_ip(ctdb
, pnn
, ipflags
, ip
);
1364 /* search the node lists list for a node to takeover this ip.
1365 pick the node that currently are serving the least number of ips
1366 so that the ips get spread out evenly.
1368 static int find_takeover_node(struct ctdb_context
*ctdb
,
1369 struct ctdb_ipflags
*ipflags
,
1370 struct ctdb_public_ip_list
*ip
,
1371 struct ctdb_public_ip_list
*all_ips
)
1373 int pnn
, min
=0, num
;
1376 numnodes
= talloc_array_length(ipflags
);
1378 for (i
=0; i
<numnodes
; i
++) {
1379 /* verify that this node can serve this ip */
1380 if (!can_node_takeover_ip(ctdb
, i
, ipflags
[i
], ip
)) {
1381 /* no it couldnt so skip to the next node */
1385 num
= node_ip_coverage(ctdb
, i
, all_ips
);
1386 /* was this the first node we checked ? */
1398 DEBUG(DEBUG_WARNING
,(__location__
" Could not find node to take over public address '%s'\n",
1399 ctdb_addr_to_str(&ip
->addr
)));
1409 static uint32_t *ip_key(ctdb_sock_addr
*ip
)
1411 static uint32_t key
[IP_KEYLEN
];
1413 bzero(key
, sizeof(key
));
1415 switch (ip
->sa
.sa_family
) {
1417 key
[3] = htonl(ip
->ip
.sin_addr
.s_addr
);
1420 uint32_t *s6_a32
= (uint32_t *)&(ip
->ip6
.sin6_addr
.s6_addr
);
1421 key
[0] = htonl(s6_a32
[0]);
1422 key
[1] = htonl(s6_a32
[1]);
1423 key
[2] = htonl(s6_a32
[2]);
1424 key
[3] = htonl(s6_a32
[3]);
1428 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", ip
->sa
.sa_family
));
1435 static void *add_ip_callback(void *parm
, void *data
)
1437 struct ctdb_public_ip_list
*this_ip
= parm
;
1438 struct ctdb_public_ip_list
*prev_ip
= data
;
1440 if (prev_ip
== NULL
) {
1443 if (this_ip
->pnn
== -1) {
1444 this_ip
->pnn
= prev_ip
->pnn
;
1450 static int getips_count_callback(void *param
, void *data
)
1452 struct ctdb_public_ip_list
**ip_list
= (struct ctdb_public_ip_list
**)param
;
1453 struct ctdb_public_ip_list
*new_ip
= (struct ctdb_public_ip_list
*)data
;
1455 new_ip
->next
= *ip_list
;
1460 static struct ctdb_public_ip_list
*
1461 create_merged_ip_list(struct ctdb_context
*ctdb
)
1464 struct ctdb_public_ip_list
*ip_list
;
1465 struct ctdb_all_public_ips
*public_ips
;
1467 if (ctdb
->ip_tree
!= NULL
) {
1468 talloc_free(ctdb
->ip_tree
);
1469 ctdb
->ip_tree
= NULL
;
1471 ctdb
->ip_tree
= trbt_create(ctdb
, 0);
1473 for (i
=0;i
<ctdb
->num_nodes
;i
++) {
1474 public_ips
= ctdb
->nodes
[i
]->known_public_ips
;
1476 if (ctdb
->nodes
[i
]->flags
& NODE_FLAGS_DELETED
) {
1480 /* there were no public ips for this node */
1481 if (public_ips
== NULL
) {
1485 for (j
=0;j
<public_ips
->num
;j
++) {
1486 struct ctdb_public_ip_list
*tmp_ip
;
1488 tmp_ip
= talloc_zero(ctdb
->ip_tree
, struct ctdb_public_ip_list
);
1489 CTDB_NO_MEMORY_NULL(ctdb
, tmp_ip
);
1490 /* Do not use information about IP addresses hosted
1491 * on other nodes, it may not be accurate */
1492 if (public_ips
->ips
[j
].pnn
== ctdb
->nodes
[i
]->pnn
) {
1493 tmp_ip
->pnn
= public_ips
->ips
[j
].pnn
;
1497 tmp_ip
->addr
= public_ips
->ips
[j
].addr
;
1498 tmp_ip
->next
= NULL
;
1500 trbt_insertarray32_callback(ctdb
->ip_tree
,
1501 IP_KEYLEN
, ip_key(&public_ips
->ips
[j
].addr
),
1508 trbt_traversearray32(ctdb
->ip_tree
, IP_KEYLEN
, getips_count_callback
, &ip_list
);
1514 * This is the length of the longtest common prefix between the IPs.
1515 * It is calculated by XOR-ing the 2 IPs together and counting the
1516 * number of leading zeroes. The implementation means that all
1517 * addresses end up being 128 bits long.
1519 * FIXME? Should we consider IPv4 and IPv6 separately given that the
1520 * 12 bytes of 0 prefix padding will hurt the algorithm if there are
1521 * lots of nodes and IP addresses?
1523 static uint32_t ip_distance(ctdb_sock_addr
*ip1
, ctdb_sock_addr
*ip2
)
1525 uint32_t ip1_k
[IP_KEYLEN
];
1530 uint32_t distance
= 0;
1532 memcpy(ip1_k
, ip_key(ip1
), sizeof(ip1_k
));
1534 for (i
=0; i
<IP_KEYLEN
; i
++) {
1535 x
= ip1_k
[i
] ^ t
[i
];
1539 /* Count number of leading zeroes.
1540 * FIXME? This could be optimised...
1542 while ((x
& (1 << 31)) == 0) {
1552 /* Calculate the IP distance for the given IP relative to IPs on the
1553 given node. The ips argument is generally the all_ips variable
1554 used in the main part of the algorithm.
1556 static uint32_t ip_distance_2_sum(ctdb_sock_addr
*ip
,
1557 struct ctdb_public_ip_list
*ips
,
1560 struct ctdb_public_ip_list
*t
;
1565 for (t
=ips
; t
!= NULL
; t
=t
->next
) {
1566 if (t
->pnn
!= pnn
) {
1570 /* Optimisation: We never calculate the distance
1571 * between an address and itself. This allows us to
1572 * calculate the effect of removing an address from a
1573 * node by simply calculating the distance between
1574 * that address and all of the exitsing addresses.
1575 * Moreover, we assume that we're only ever dealing
1576 * with addresses from all_ips so we can identify an
1577 * address via a pointer rather than doing a more
1578 * expensive address comparison. */
1579 if (&(t
->addr
) == ip
) {
1583 d
= ip_distance(ip
, &(t
->addr
));
1584 sum
+= d
* d
; /* Cheaper than pulling in math.h :-) */
1590 /* Return the LCP2 imbalance metric for addresses currently assigned
1593 static uint32_t lcp2_imbalance(struct ctdb_public_ip_list
* all_ips
, int pnn
)
1595 struct ctdb_public_ip_list
*t
;
1597 uint32_t imbalance
= 0;
1599 for (t
=all_ips
; t
!=NULL
; t
=t
->next
) {
1600 if (t
->pnn
!= pnn
) {
1603 /* Pass the rest of the IPs rather than the whole
1606 imbalance
+= ip_distance_2_sum(&(t
->addr
), t
->next
, pnn
);
1612 /* Allocate any unassigned IPs just by looping through the IPs and
1613 * finding the best node for each.
1615 static void basic_allocate_unassigned(struct ctdb_context
*ctdb
,
1616 struct ctdb_ipflags
*ipflags
,
1617 struct ctdb_public_ip_list
*all_ips
)
1619 struct ctdb_public_ip_list
*tmp_ip
;
1621 /* loop over all ip's and find a physical node to cover for
1624 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1625 if (tmp_ip
->pnn
== -1) {
1626 if (find_takeover_node(ctdb
, ipflags
, tmp_ip
, all_ips
)) {
1627 DEBUG(DEBUG_WARNING
,("Failed to find node to cover ip %s\n",
1628 ctdb_addr_to_str(&tmp_ip
->addr
)));
1634 /* Basic non-deterministic rebalancing algorithm.
1636 static void basic_failback(struct ctdb_context
*ctdb
,
1637 struct ctdb_ipflags
*ipflags
,
1638 struct ctdb_public_ip_list
*all_ips
,
1642 int maxnode
, maxnum
, minnode
, minnum
, num
, retries
;
1643 struct ctdb_public_ip_list
*tmp_ip
;
1645 numnodes
= talloc_array_length(ipflags
);
1652 /* for each ip address, loop over all nodes that can serve
1653 this ip and make sure that the difference between the node
1654 serving the most and the node serving the least ip's are
1657 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1658 if (tmp_ip
->pnn
== -1) {
1662 /* Get the highest and lowest number of ips's served by any
1663 valid node which can serve this ip.
1667 for (i
=0; i
<numnodes
; i
++) {
1668 /* only check nodes that can actually serve this ip */
1669 if (!can_node_takeover_ip(ctdb
, i
, ipflags
[i
], tmp_ip
)) {
1670 /* no it couldnt so skip to the next node */
1674 num
= node_ip_coverage(ctdb
, i
, all_ips
);
1675 if (maxnode
== -1) {
1684 if (minnode
== -1) {
1694 if (maxnode
== -1) {
1695 DEBUG(DEBUG_WARNING
,(__location__
" Could not find maxnode. May not be able to serve ip '%s'\n",
1696 ctdb_addr_to_str(&tmp_ip
->addr
)));
1701 /* if the spread between the smallest and largest coverage by
1702 a node is >=2 we steal one of the ips from the node with
1703 most coverage to even things out a bit.
1704 try to do this a limited number of times since we dont
1705 want to spend too much time balancing the ip coverage.
1707 if ( (maxnum
> minnum
+1)
1708 && (retries
< (num_ips
+ 5)) ){
1709 struct ctdb_public_ip_list
*tmp
;
1711 /* Reassign one of maxnode's VNNs */
1712 for (tmp
=all_ips
;tmp
;tmp
=tmp
->next
) {
1713 if (tmp
->pnn
== maxnode
) {
1714 (void)find_takeover_node(ctdb
, ipflags
, tmp
, all_ips
);
1723 static void lcp2_init(struct ctdb_context
*tmp_ctx
,
1724 struct ctdb_ipflags
*ipflags
,
1725 struct ctdb_public_ip_list
*all_ips
,
1726 uint32_t *force_rebalance_nodes
,
1727 uint32_t **lcp2_imbalances
,
1728 bool **rebalance_candidates
)
1731 struct ctdb_public_ip_list
*tmp_ip
;
1733 numnodes
= talloc_array_length(ipflags
);
1735 *rebalance_candidates
= talloc_array(tmp_ctx
, bool, numnodes
);
1736 CTDB_NO_MEMORY_FATAL(tmp_ctx
, *rebalance_candidates
);
1737 *lcp2_imbalances
= talloc_array(tmp_ctx
, uint32_t, numnodes
);
1738 CTDB_NO_MEMORY_FATAL(tmp_ctx
, *lcp2_imbalances
);
1740 for (i
=0; i
<numnodes
; i
++) {
1741 (*lcp2_imbalances
)[i
] = lcp2_imbalance(all_ips
, i
);
1742 /* First step: assume all nodes are candidates */
1743 (*rebalance_candidates
)[i
] = true;
1746 /* 2nd step: if a node has IPs assigned then it must have been
1747 * healthy before, so we remove it from consideration. This
1748 * is overkill but is all we have because we don't maintain
1749 * state between takeover runs. An alternative would be to
1750 * keep state and invalidate it every time the recovery master
1753 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1754 if (tmp_ip
->pnn
!= -1) {
1755 (*rebalance_candidates
)[tmp_ip
->pnn
] = false;
1759 /* 3rd step: if a node is forced to re-balance then
1760 we allow failback onto the node */
1761 if (force_rebalance_nodes
== NULL
) {
1764 for (i
= 0; i
< talloc_array_length(force_rebalance_nodes
); i
++) {
1765 uint32_t pnn
= force_rebalance_nodes
[i
];
1766 if (pnn
>= numnodes
) {
1768 (__location__
"unknown node %u\n", pnn
));
1773 ("Forcing rebalancing of IPs to node %u\n", pnn
));
1774 (*rebalance_candidates
)[pnn
] = true;
1778 /* Allocate any unassigned addresses using the LCP2 algorithm to find
1779 * the IP/node combination that will cost the least.
1781 static void lcp2_allocate_unassigned(struct ctdb_context
*ctdb
,
1782 struct ctdb_ipflags
*ipflags
,
1783 struct ctdb_public_ip_list
*all_ips
,
1784 uint32_t *lcp2_imbalances
)
1786 struct ctdb_public_ip_list
*tmp_ip
;
1787 int dstnode
, numnodes
;
1790 uint32_t mindsum
, dstdsum
, dstimbl
, minimbl
;
1791 struct ctdb_public_ip_list
*minip
;
1793 bool should_loop
= true;
1794 bool have_unassigned
= true;
1796 numnodes
= talloc_array_length(ipflags
);
1798 while (have_unassigned
&& should_loop
) {
1799 should_loop
= false;
1801 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1802 DEBUG(DEBUG_DEBUG
,(" CONSIDERING MOVES (UNASSIGNED)\n"));
1808 /* loop over each unassigned ip. */
1809 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1810 if (tmp_ip
->pnn
!= -1) {
1814 for (dstnode
=0; dstnode
<numnodes
; dstnode
++) {
1815 /* only check nodes that can actually takeover this ip */
1816 if (!can_node_takeover_ip(ctdb
, dstnode
,
1819 /* no it couldnt so skip to the next node */
1823 dstdsum
= ip_distance_2_sum(&(tmp_ip
->addr
), all_ips
, dstnode
);
1824 dstimbl
= lcp2_imbalances
[dstnode
] + dstdsum
;
1825 DEBUG(DEBUG_DEBUG
,(" %s -> %d [+%d]\n",
1826 ctdb_addr_to_str(&(tmp_ip
->addr
)),
1828 dstimbl
- lcp2_imbalances
[dstnode
]));
1831 if ((minnode
== -1) || (dstdsum
< mindsum
)) {
1841 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1843 /* If we found one then assign it to the given node. */
1844 if (minnode
!= -1) {
1845 minip
->pnn
= minnode
;
1846 lcp2_imbalances
[minnode
] = minimbl
;
1847 DEBUG(DEBUG_INFO
,(" %s -> %d [+%d]\n",
1848 ctdb_addr_to_str(&(minip
->addr
)),
1853 /* There might be a better way but at least this is clear. */
1854 have_unassigned
= false;
1855 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1856 if (tmp_ip
->pnn
== -1) {
1857 have_unassigned
= true;
1862 /* We know if we have an unassigned addresses so we might as
1865 if (have_unassigned
) {
1866 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
1867 if (tmp_ip
->pnn
== -1) {
1868 DEBUG(DEBUG_WARNING
,("Failed to find node to cover ip %s\n",
1869 ctdb_addr_to_str(&tmp_ip
->addr
)));
1875 /* LCP2 algorithm for rebalancing the cluster. Given a candidate node
1876 * to move IPs from, determines the best IP/destination node
1877 * combination to move from the source node.
1879 static bool lcp2_failback_candidate(struct ctdb_context
*ctdb
,
1880 struct ctdb_ipflags
*ipflags
,
1881 struct ctdb_public_ip_list
*all_ips
,
1883 uint32_t *lcp2_imbalances
,
1884 bool *rebalance_candidates
)
1886 int dstnode
, mindstnode
, numnodes
;
1887 uint32_t srcimbl
, srcdsum
, dstimbl
, dstdsum
;
1888 uint32_t minsrcimbl
, mindstimbl
;
1889 struct ctdb_public_ip_list
*minip
;
1890 struct ctdb_public_ip_list
*tmp_ip
;
1892 /* Find an IP and destination node that best reduces imbalance. */
1899 numnodes
= talloc_array_length(ipflags
);
1901 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1902 DEBUG(DEBUG_DEBUG
,(" CONSIDERING MOVES FROM %d [%d]\n",
1903 srcnode
, lcp2_imbalances
[srcnode
]));
1905 for (tmp_ip
=all_ips
; tmp_ip
; tmp_ip
=tmp_ip
->next
) {
1906 /* Only consider addresses on srcnode. */
1907 if (tmp_ip
->pnn
!= srcnode
) {
1911 /* What is this IP address costing the source node? */
1912 srcdsum
= ip_distance_2_sum(&(tmp_ip
->addr
), all_ips
, srcnode
);
1913 srcimbl
= lcp2_imbalances
[srcnode
] - srcdsum
;
1915 /* Consider this IP address would cost each potential
1916 * destination node. Destination nodes are limited to
1917 * those that are newly healthy, since we don't want
1918 * to do gratuitous failover of IPs just to make minor
1919 * balance improvements.
1921 for (dstnode
=0; dstnode
<numnodes
; dstnode
++) {
1922 if (!rebalance_candidates
[dstnode
]) {
1926 /* only check nodes that can actually takeover this ip */
1927 if (!can_node_takeover_ip(ctdb
, dstnode
,
1928 ipflags
[dstnode
], tmp_ip
)) {
1929 /* no it couldnt so skip to the next node */
1933 dstdsum
= ip_distance_2_sum(&(tmp_ip
->addr
), all_ips
, dstnode
);
1934 dstimbl
= lcp2_imbalances
[dstnode
] + dstdsum
;
1935 DEBUG(DEBUG_DEBUG
,(" %d [%d] -> %s -> %d [+%d]\n",
1937 ctdb_addr_to_str(&(tmp_ip
->addr
)),
1940 if ((dstimbl
< lcp2_imbalances
[srcnode
]) &&
1941 (dstdsum
< srcdsum
) && \
1942 ((mindstnode
== -1) || \
1943 ((srcimbl
+ dstimbl
) < (minsrcimbl
+ mindstimbl
)))) {
1946 minsrcimbl
= srcimbl
;
1947 mindstnode
= dstnode
;
1948 mindstimbl
= dstimbl
;
1952 DEBUG(DEBUG_DEBUG
,(" ----------------------------------------\n"));
1954 if (mindstnode
!= -1) {
1955 /* We found a move that makes things better... */
1956 DEBUG(DEBUG_INFO
,("%d [%d] -> %s -> %d [+%d]\n",
1957 srcnode
, minsrcimbl
- lcp2_imbalances
[srcnode
],
1958 ctdb_addr_to_str(&(minip
->addr
)),
1959 mindstnode
, mindstimbl
- lcp2_imbalances
[mindstnode
]));
1962 lcp2_imbalances
[srcnode
] = minsrcimbl
;
1963 lcp2_imbalances
[mindstnode
] = mindstimbl
;
1964 minip
->pnn
= mindstnode
;
1973 struct lcp2_imbalance_pnn
{
1978 static int lcp2_cmp_imbalance_pnn(const void * a
, const void * b
)
1980 const struct lcp2_imbalance_pnn
* lipa
= (const struct lcp2_imbalance_pnn
*) a
;
1981 const struct lcp2_imbalance_pnn
* lipb
= (const struct lcp2_imbalance_pnn
*) b
;
1983 if (lipa
->imbalance
> lipb
->imbalance
) {
1985 } else if (lipa
->imbalance
== lipb
->imbalance
) {
1992 /* LCP2 algorithm for rebalancing the cluster. This finds the source
1993 * node with the highest LCP2 imbalance, and then determines the best
1994 * IP/destination node combination to move from the source node.
1996 static void lcp2_failback(struct ctdb_context
*ctdb
,
1997 struct ctdb_ipflags
*ipflags
,
1998 struct ctdb_public_ip_list
*all_ips
,
1999 uint32_t *lcp2_imbalances
,
2000 bool *rebalance_candidates
)
2003 struct lcp2_imbalance_pnn
* lips
;
2006 numnodes
= talloc_array_length(ipflags
);
2009 /* Put the imbalances and nodes into an array, sort them and
2010 * iterate through candidates. Usually the 1st one will be
2011 * used, so this doesn't cost much...
2013 DEBUG(DEBUG_DEBUG
,("+++++++++++++++++++++++++++++++++++++++++\n"));
2014 DEBUG(DEBUG_DEBUG
,("Selecting most imbalanced node from:\n"));
2015 lips
= talloc_array(ctdb
, struct lcp2_imbalance_pnn
, numnodes
);
2016 for (i
=0; i
<numnodes
; i
++) {
2017 lips
[i
].imbalance
= lcp2_imbalances
[i
];
2019 DEBUG(DEBUG_DEBUG
,(" %d [%d]\n", i
, lcp2_imbalances
[i
]));
2021 qsort(lips
, numnodes
, sizeof(struct lcp2_imbalance_pnn
),
2022 lcp2_cmp_imbalance_pnn
);
2025 for (i
=0; i
<numnodes
; i
++) {
2026 /* This means that all nodes had 0 or 1 addresses, so
2027 * can't be imbalanced.
2029 if (lips
[i
].imbalance
== 0) {
2033 if (lcp2_failback_candidate(ctdb
,
2038 rebalance_candidates
)) {
2050 static void unassign_unsuitable_ips(struct ctdb_context
*ctdb
,
2051 struct ctdb_ipflags
*ipflags
,
2052 struct ctdb_public_ip_list
*all_ips
)
2054 struct ctdb_public_ip_list
*tmp_ip
;
2056 /* verify that the assigned nodes can serve that public ip
2057 and set it to -1 if not
2059 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2060 if (tmp_ip
->pnn
== -1) {
2063 if (!can_node_host_ip(ctdb
, tmp_ip
->pnn
,
2064 ipflags
[tmp_ip
->pnn
], tmp_ip
) != 0) {
2065 /* this node can not serve this ip. */
2066 DEBUG(DEBUG_DEBUG
,("Unassign IP: %s from %d\n",
2067 ctdb_addr_to_str(&(tmp_ip
->addr
)),
2074 static void ip_alloc_deterministic_ips(struct ctdb_context
*ctdb
,
2075 struct ctdb_ipflags
*ipflags
,
2076 struct ctdb_public_ip_list
*all_ips
)
2078 struct ctdb_public_ip_list
*tmp_ip
;
2081 numnodes
= talloc_array_length(ipflags
);
2083 DEBUG(DEBUG_NOTICE
,("Deterministic IPs enabled. Resetting all ip allocations\n"));
2084 /* Allocate IPs to nodes in a modulo fashion so that IPs will
2085 * always be allocated the same way for a specific set of
2086 * available/unavailable nodes.
2089 for (i
=0,tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
,i
++) {
2090 tmp_ip
->pnn
= i
% numnodes
;
2093 /* IP failback doesn't make sense with deterministic
2094 * IPs, since the modulo step above implicitly fails
2095 * back IPs to their "home" node.
2097 if (1 == ctdb
->tunable
.no_ip_failback
) {
2098 DEBUG(DEBUG_WARNING
, ("WARNING: 'NoIPFailback' set but ignored - incompatible with 'DeterministicIPs\n"));
2101 unassign_unsuitable_ips(ctdb
, ipflags
, all_ips
);
2103 basic_allocate_unassigned(ctdb
, ipflags
, all_ips
);
2105 /* No failback here! */
2108 static void ip_alloc_nondeterministic_ips(struct ctdb_context
*ctdb
,
2109 struct ctdb_ipflags
*ipflags
,
2110 struct ctdb_public_ip_list
*all_ips
)
2112 /* This should be pushed down into basic_failback. */
2113 struct ctdb_public_ip_list
*tmp_ip
;
2115 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2119 unassign_unsuitable_ips(ctdb
, ipflags
, all_ips
);
2121 basic_allocate_unassigned(ctdb
, ipflags
, all_ips
);
2123 /* If we don't want IPs to fail back then don't rebalance IPs. */
2124 if (1 == ctdb
->tunable
.no_ip_failback
) {
2128 /* Now, try to make sure the ip adresses are evenly distributed
2131 basic_failback(ctdb
, ipflags
, all_ips
, num_ips
);
2134 static void ip_alloc_lcp2(struct ctdb_context
*ctdb
,
2135 struct ctdb_ipflags
*ipflags
,
2136 struct ctdb_public_ip_list
*all_ips
,
2137 uint32_t *force_rebalance_nodes
)
2139 uint32_t *lcp2_imbalances
;
2140 bool *rebalance_candidates
;
2141 int numnodes
, num_rebalance_candidates
, i
;
2143 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2145 unassign_unsuitable_ips(ctdb
, ipflags
, all_ips
);
2147 lcp2_init(tmp_ctx
, ipflags
, all_ips
,force_rebalance_nodes
,
2148 &lcp2_imbalances
, &rebalance_candidates
);
2150 lcp2_allocate_unassigned(ctdb
, ipflags
, all_ips
, lcp2_imbalances
);
2152 /* If we don't want IPs to fail back then don't rebalance IPs. */
2153 if (1 == ctdb
->tunable
.no_ip_failback
) {
2157 /* It is only worth continuing if we have suitable target
2158 * nodes to transfer IPs to. This check is much cheaper than
2161 numnodes
= talloc_array_length(ipflags
);
2162 num_rebalance_candidates
= 0;
2163 for (i
=0; i
<numnodes
; i
++) {
2164 if (rebalance_candidates
[i
]) {
2165 num_rebalance_candidates
++;
2168 if (num_rebalance_candidates
== 0) {
2172 /* Now, try to make sure the ip adresses are evenly distributed
2175 lcp2_failback(ctdb
, ipflags
, all_ips
,
2176 lcp2_imbalances
, rebalance_candidates
);
2179 talloc_free(tmp_ctx
);
2182 static bool all_nodes_are_disabled(struct ctdb_node_map
*nodemap
)
2186 /* Count how many completely healthy nodes we have */
2188 for (i
=0;i
<nodemap
->num
;i
++) {
2189 if (!(nodemap
->nodes
[i
].flags
& (NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
))) {
2194 return num_healthy
== 0;
2197 /* The calculation part of the IP allocation algorithm. */
2198 static void ctdb_takeover_run_core(struct ctdb_context
*ctdb
,
2199 struct ctdb_ipflags
*ipflags
,
2200 struct ctdb_public_ip_list
**all_ips_p
,
2201 uint32_t *force_rebalance_nodes
)
2203 /* since nodes only know about those public addresses that
2204 can be served by that particular node, no single node has
2205 a full list of all public addresses that exist in the cluster.
2206 Walk over all node structures and create a merged list of
2207 all public addresses that exist in the cluster.
2209 keep the tree of ips around as ctdb->ip_tree
2211 *all_ips_p
= create_merged_ip_list(ctdb
);
2213 if (1 == ctdb
->tunable
.lcp2_public_ip_assignment
) {
2214 ip_alloc_lcp2(ctdb
, ipflags
, *all_ips_p
, force_rebalance_nodes
);
2215 } else if (1 == ctdb
->tunable
.deterministic_public_ips
) {
2216 ip_alloc_deterministic_ips(ctdb
, ipflags
, *all_ips_p
);
2218 ip_alloc_nondeterministic_ips(ctdb
, ipflags
, *all_ips_p
);
2221 /* at this point ->pnn is the node which will own each IP
2222 or -1 if there is no node that can cover this ip
2228 struct get_tunable_callback_data
{
2229 const char *tunable
;
2234 static void get_tunable_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2235 int32_t res
, TDB_DATA outdata
,
2238 struct get_tunable_callback_data
*cd
=
2239 (struct get_tunable_callback_data
*)callback
;
2243 /* Already handled in fail callback */
2247 if (outdata
.dsize
!= sizeof(uint32_t)) {
2248 DEBUG(DEBUG_ERR
,("Wrong size of returned data when reading \"%s\" tunable from node %d. Expected %d bytes but received %d bytes\n",
2249 cd
->tunable
, pnn
, (int)sizeof(uint32_t),
2250 (int)outdata
.dsize
));
2255 size
= talloc_array_length(cd
->out
);
2257 DEBUG(DEBUG_ERR
,("Got %s reply from node %d but nodemap only has %d entries\n",
2258 cd
->tunable
, pnn
, size
));
2263 cd
->out
[pnn
] = *(uint32_t *)outdata
.dptr
;
2266 static void get_tunable_fail_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2267 int32_t res
, TDB_DATA outdata
,
2270 struct get_tunable_callback_data
*cd
=
2271 (struct get_tunable_callback_data
*)callback
;
2276 ("Timed out getting tunable \"%s\" from node %d\n",
2282 DEBUG(DEBUG_WARNING
,
2283 ("Tunable \"%s\" not implemented on node %d\n",
2288 ("Unexpected error getting tunable \"%s\" from node %d\n",
2294 static uint32_t *get_tunable_from_nodes(struct ctdb_context
*ctdb
,
2295 TALLOC_CTX
*tmp_ctx
,
2296 struct ctdb_node_map
*nodemap
,
2297 const char *tunable
,
2298 uint32_t default_value
)
2301 struct ctdb_control_get_tunable
*t
;
2304 struct get_tunable_callback_data callback_data
;
2307 tvals
= talloc_array(tmp_ctx
, uint32_t, nodemap
->num
);
2308 CTDB_NO_MEMORY_NULL(ctdb
, tvals
);
2309 for (i
=0; i
<nodemap
->num
; i
++) {
2310 tvals
[i
] = default_value
;
2313 callback_data
.out
= tvals
;
2314 callback_data
.tunable
= tunable
;
2315 callback_data
.fatal
= false;
2317 data
.dsize
= offsetof(struct ctdb_control_get_tunable
, name
) + strlen(tunable
) + 1;
2318 data
.dptr
= talloc_size(tmp_ctx
, data
.dsize
);
2319 t
= (struct ctdb_control_get_tunable
*)data
.dptr
;
2320 t
->length
= strlen(tunable
)+1;
2321 memcpy(t
->name
, tunable
, t
->length
);
2322 nodes
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2323 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_GET_TUNABLE
,
2324 nodes
, 0, TAKEOVER_TIMEOUT(),
2326 get_tunable_callback
,
2327 get_tunable_fail_callback
,
2328 &callback_data
) != 0) {
2329 if (callback_data
.fatal
) {
2335 talloc_free(data
.dptr
);
2340 struct get_runstate_callback_data
{
2341 enum ctdb_runstate
*out
;
2345 static void get_runstate_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2346 int32_t res
, TDB_DATA outdata
,
2347 void *callback_data
)
2349 struct get_runstate_callback_data
*cd
=
2350 (struct get_runstate_callback_data
*)callback_data
;
2354 /* Already handled in fail callback */
2358 if (outdata
.dsize
!= sizeof(uint32_t)) {
2359 DEBUG(DEBUG_ERR
,("Wrong size of returned data when getting runstate from node %d. Expected %d bytes but received %d bytes\n",
2360 pnn
, (int)sizeof(uint32_t),
2361 (int)outdata
.dsize
));
2366 size
= talloc_array_length(cd
->out
);
2368 DEBUG(DEBUG_ERR
,("Got reply from node %d but nodemap only has %d entries\n",
2373 cd
->out
[pnn
] = (enum ctdb_runstate
)*(uint32_t *)outdata
.dptr
;
2376 static void get_runstate_fail_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2377 int32_t res
, TDB_DATA outdata
,
2380 struct get_runstate_callback_data
*cd
=
2381 (struct get_runstate_callback_data
*)callback
;
2386 ("Timed out getting runstate from node %d\n", pnn
));
2390 DEBUG(DEBUG_WARNING
,
2391 ("Error getting runstate from node %d - assuming runstates not supported\n",
2396 static enum ctdb_runstate
* get_runstate_from_nodes(struct ctdb_context
*ctdb
,
2397 TALLOC_CTX
*tmp_ctx
,
2398 struct ctdb_node_map
*nodemap
,
2399 enum ctdb_runstate default_value
)
2402 enum ctdb_runstate
*rs
;
2403 struct get_runstate_callback_data callback_data
;
2406 rs
= talloc_array(tmp_ctx
, enum ctdb_runstate
, nodemap
->num
);
2407 CTDB_NO_MEMORY_NULL(ctdb
, rs
);
2408 for (i
=0; i
<nodemap
->num
; i
++) {
2409 rs
[i
] = default_value
;
2412 callback_data
.out
= rs
;
2413 callback_data
.fatal
= false;
2415 nodes
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2416 if (ctdb_client_async_control(ctdb
, CTDB_CONTROL_GET_RUNSTATE
,
2417 nodes
, 0, TAKEOVER_TIMEOUT(),
2419 get_runstate_callback
,
2420 get_runstate_fail_callback
,
2421 &callback_data
) != 0) {
2422 if (callback_data
.fatal
) {
2432 /* Set internal flags for IP allocation:
2434 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
2435 * Set NOIPHOST ip flag for each INACTIVE node
2436 * if all nodes are disabled:
2437 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
2439 * Set NOIPHOST ip flags for disabled nodes
2441 static struct ctdb_ipflags
*
2442 set_ipflags_internal(struct ctdb_context
*ctdb
,
2443 TALLOC_CTX
*tmp_ctx
,
2444 struct ctdb_node_map
*nodemap
,
2445 uint32_t *tval_noiptakeover
,
2446 uint32_t *tval_noiphostonalldisabled
,
2447 enum ctdb_runstate
*runstate
)
2450 struct ctdb_ipflags
*ipflags
;
2452 /* Clear IP flags - implicit due to talloc_zero */
2453 ipflags
= talloc_zero_array(tmp_ctx
, struct ctdb_ipflags
, nodemap
->num
);
2454 CTDB_NO_MEMORY_NULL(ctdb
, ipflags
);
2456 for (i
=0;i
<nodemap
->num
;i
++) {
2457 /* Can not take IPs on node with NoIPTakeover set */
2458 if (tval_noiptakeover
[i
] != 0) {
2459 ipflags
[i
].noiptakeover
= true;
2462 /* Can not host IPs on node not in RUNNING state */
2463 if (runstate
[i
] != CTDB_RUNSTATE_RUNNING
) {
2464 ipflags
[i
].noiphost
= true;
2467 /* Can not host IPs on INACTIVE node */
2468 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) {
2469 ipflags
[i
].noiphost
= true;
2473 if (all_nodes_are_disabled(nodemap
)) {
2474 /* If all nodes are disabled, can not host IPs on node
2475 * with NoIPHostOnAllDisabled set
2477 for (i
=0;i
<nodemap
->num
;i
++) {
2478 if (tval_noiphostonalldisabled
[i
] != 0) {
2479 ipflags
[i
].noiphost
= true;
2483 /* If some nodes are not disabled, then can not host
2484 * IPs on DISABLED node
2486 for (i
=0;i
<nodemap
->num
;i
++) {
2487 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISABLED
) {
2488 ipflags
[i
].noiphost
= true;
2496 static struct ctdb_ipflags
*set_ipflags(struct ctdb_context
*ctdb
,
2497 TALLOC_CTX
*tmp_ctx
,
2498 struct ctdb_node_map
*nodemap
)
2500 uint32_t *tval_noiptakeover
;
2501 uint32_t *tval_noiphostonalldisabled
;
2502 struct ctdb_ipflags
*ipflags
;
2503 enum ctdb_runstate
*runstate
;
2506 tval_noiptakeover
= get_tunable_from_nodes(ctdb
, tmp_ctx
, nodemap
,
2508 if (tval_noiptakeover
== NULL
) {
2512 tval_noiphostonalldisabled
=
2513 get_tunable_from_nodes(ctdb
, tmp_ctx
, nodemap
,
2514 "NoIPHostOnAllDisabled", 0);
2515 if (tval_noiphostonalldisabled
== NULL
) {
2516 /* Caller frees tmp_ctx */
2520 /* Any nodes where CTDB_CONTROL_GET_RUNSTATE is not supported
2521 * will default to CTDB_RUNSTATE_RUNNING. This ensures
2522 * reasonable behaviour on a mixed cluster during upgrade.
2524 runstate
= get_runstate_from_nodes(ctdb
, tmp_ctx
, nodemap
,
2525 CTDB_RUNSTATE_RUNNING
);
2526 if (runstate
== NULL
) {
2527 /* Caller frees tmp_ctx */
2531 ipflags
= set_ipflags_internal(ctdb
, tmp_ctx
, nodemap
,
2533 tval_noiphostonalldisabled
,
2536 talloc_free(tval_noiptakeover
);
2537 talloc_free(tval_noiphostonalldisabled
);
2538 talloc_free(runstate
);
2543 struct iprealloc_callback_data
{
2546 client_async_callback fail_callback
;
2547 void *fail_callback_data
;
2548 struct ctdb_node_map
*nodemap
;
2551 static void iprealloc_fail_callback(struct ctdb_context
*ctdb
, uint32_t pnn
,
2552 int32_t res
, TDB_DATA outdata
,
2556 struct iprealloc_callback_data
*cd
=
2557 (struct iprealloc_callback_data
*)callback
;
2559 numnodes
= talloc_array_length(cd
->retry_nodes
);
2560 if (pnn
> numnodes
) {
2562 ("ipreallocated failure from node %d, "
2563 "but only %d nodes in nodemap\n",
2568 /* Can't run the "ipreallocated" event on a INACTIVE node */
2569 if (cd
->nodemap
->nodes
[pnn
].flags
& NODE_FLAGS_INACTIVE
) {
2570 DEBUG(DEBUG_WARNING
,
2571 ("ipreallocated failed on inactive node %d, ignoring\n",
2578 /* If the control timed out then that's a real error,
2579 * so call the real fail callback
2581 cd
->fail_callback(ctdb
, pnn
, res
, outdata
,
2582 cd
->fail_callback_data
);
2585 /* If not a timeout then either the ipreallocated
2586 * eventscript (or some setup) failed. This might
2587 * have failed because the IPREALLOCATED control isn't
2588 * implemented - right now there is no way of knowing
2589 * because the error codes are all folded down to -1.
2590 * Consider retrying using EVENTSCRIPT control...
2592 DEBUG(DEBUG_WARNING
,
2593 ("ipreallocated failure from node %d, flagging retry\n",
2595 cd
->retry_nodes
[pnn
] = true;
2600 struct takeover_callback_data
{
2602 client_async_callback fail_callback
;
2603 void *fail_callback_data
;
2604 struct ctdb_node_map
*nodemap
;
2607 static void takeover_run_fail_callback(struct ctdb_context
*ctdb
,
2608 uint32_t node_pnn
, int32_t res
,
2609 TDB_DATA outdata
, void *callback_data
)
2611 struct takeover_callback_data
*cd
=
2612 talloc_get_type_abort(callback_data
,
2613 struct takeover_callback_data
);
2616 for (i
= 0; i
< cd
->nodemap
->num
; i
++) {
2617 if (node_pnn
== cd
->nodemap
->nodes
[i
].pnn
) {
2622 if (i
== cd
->nodemap
->num
) {
2623 DEBUG(DEBUG_ERR
, (__location__
" invalid PNN %u\n", node_pnn
));
2627 if (!cd
->node_failed
[i
]) {
2628 cd
->node_failed
[i
] = true;
2629 cd
->fail_callback(ctdb
, node_pnn
, res
, outdata
,
2630 cd
->fail_callback_data
);
2635 make any IP alias changes for public addresses that are necessary
2637 int ctdb_takeover_run(struct ctdb_context
*ctdb
, struct ctdb_node_map
*nodemap
,
2638 uint32_t *force_rebalance_nodes
,
2639 client_async_callback fail_callback
, void *callback_data
)
2642 struct ctdb_public_ip ip
;
2643 struct ctdb_public_ipv4 ipv4
;
2645 struct ctdb_public_ip_list
*all_ips
, *tmp_ip
;
2647 struct timeval timeout
;
2648 struct client_async_data
*async_data
;
2649 struct ctdb_client_control_state
*state
;
2650 TALLOC_CTX
*tmp_ctx
= talloc_new(ctdb
);
2651 struct ctdb_ipflags
*ipflags
;
2652 struct takeover_callback_data
*takeover_data
;
2653 struct iprealloc_callback_data iprealloc_data
;
2657 * ip failover is completely disabled, just send out the
2658 * ipreallocated event.
2660 if (ctdb
->tunable
.disable_ip_failover
!= 0) {
2664 ipflags
= set_ipflags(ctdb
, tmp_ctx
, nodemap
);
2665 if (ipflags
== NULL
) {
2666 DEBUG(DEBUG_ERR
,("Failed to set IP flags - aborting takeover run\n"));
2667 talloc_free(tmp_ctx
);
2673 /* Do the IP reassignment calculations */
2674 ctdb_takeover_run_core(ctdb
, ipflags
, &all_ips
, force_rebalance_nodes
);
2676 /* Now tell all nodes to release any public IPs should not
2677 * host. This will be a NOOP on nodes that don't currently
2678 * hold the given IP.
2680 takeover_data
= talloc_zero(tmp_ctx
, struct takeover_callback_data
);
2681 CTDB_NO_MEMORY_FATAL(ctdb
, takeover_data
);
2683 takeover_data
->node_failed
= talloc_zero_array(tmp_ctx
,
2684 bool, nodemap
->num
);
2685 CTDB_NO_MEMORY_FATAL(ctdb
, takeover_data
->node_failed
);
2686 takeover_data
->fail_callback
= fail_callback
;
2687 takeover_data
->fail_callback_data
= callback_data
;
2688 takeover_data
->nodemap
= nodemap
;
2690 async_data
= talloc_zero(tmp_ctx
, struct client_async_data
);
2691 CTDB_NO_MEMORY_FATAL(ctdb
, async_data
);
2693 async_data
->fail_callback
= takeover_run_fail_callback
;
2694 async_data
->callback_data
= takeover_data
;
2696 for (i
=0;i
<nodemap
->num
;i
++) {
2697 /* don't talk to unconnected nodes, but do talk to banned nodes */
2698 if (nodemap
->nodes
[i
].flags
& NODE_FLAGS_DISCONNECTED
) {
2702 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2703 if (tmp_ip
->pnn
== nodemap
->nodes
[i
].pnn
) {
2704 /* This node should be serving this
2705 vnn so dont tell it to release the ip
2709 if (tmp_ip
->addr
.sa
.sa_family
== AF_INET
) {
2710 ipv4
.pnn
= tmp_ip
->pnn
;
2711 ipv4
.sin
= tmp_ip
->addr
.ip
;
2713 timeout
= TAKEOVER_TIMEOUT();
2714 data
.dsize
= sizeof(ipv4
);
2715 data
.dptr
= (uint8_t *)&ipv4
;
2716 state
= ctdb_control_send(ctdb
, nodemap
->nodes
[i
].pnn
,
2717 0, CTDB_CONTROL_RELEASE_IPv4
, 0,
2721 ip
.pnn
= tmp_ip
->pnn
;
2722 ip
.addr
= tmp_ip
->addr
;
2724 timeout
= TAKEOVER_TIMEOUT();
2725 data
.dsize
= sizeof(ip
);
2726 data
.dptr
= (uint8_t *)&ip
;
2727 state
= ctdb_control_send(ctdb
, nodemap
->nodes
[i
].pnn
,
2728 0, CTDB_CONTROL_RELEASE_IP
, 0,
2733 if (state
== NULL
) {
2734 DEBUG(DEBUG_ERR
,(__location__
" Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap
->nodes
[i
].pnn
));
2735 talloc_free(tmp_ctx
);
2739 ctdb_client_async_add(async_data
, state
);
2742 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2743 DEBUG(DEBUG_ERR
,(__location__
" Async control CTDB_CONTROL_RELEASE_IP failed\n"));
2744 talloc_free(tmp_ctx
);
2747 talloc_free(async_data
);
2750 /* tell all nodes to get their own IPs */
2751 async_data
= talloc_zero(tmp_ctx
, struct client_async_data
);
2752 CTDB_NO_MEMORY_FATAL(ctdb
, async_data
);
2754 async_data
->fail_callback
= fail_callback
;
2755 async_data
->callback_data
= callback_data
;
2757 for (tmp_ip
=all_ips
;tmp_ip
;tmp_ip
=tmp_ip
->next
) {
2758 if (tmp_ip
->pnn
== -1) {
2759 /* this IP won't be taken over */
2763 if (tmp_ip
->addr
.sa
.sa_family
== AF_INET
) {
2764 ipv4
.pnn
= tmp_ip
->pnn
;
2765 ipv4
.sin
= tmp_ip
->addr
.ip
;
2767 timeout
= TAKEOVER_TIMEOUT();
2768 data
.dsize
= sizeof(ipv4
);
2769 data
.dptr
= (uint8_t *)&ipv4
;
2770 state
= ctdb_control_send(ctdb
, tmp_ip
->pnn
,
2771 0, CTDB_CONTROL_TAKEOVER_IPv4
, 0,
2775 ip
.pnn
= tmp_ip
->pnn
;
2776 ip
.addr
= tmp_ip
->addr
;
2778 timeout
= TAKEOVER_TIMEOUT();
2779 data
.dsize
= sizeof(ip
);
2780 data
.dptr
= (uint8_t *)&ip
;
2781 state
= ctdb_control_send(ctdb
, tmp_ip
->pnn
,
2782 0, CTDB_CONTROL_TAKEOVER_IP
, 0,
2786 if (state
== NULL
) {
2787 DEBUG(DEBUG_ERR
,(__location__
" Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip
->pnn
));
2788 talloc_free(tmp_ctx
);
2792 ctdb_client_async_add(async_data
, state
);
2794 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
2795 DEBUG(DEBUG_ERR
,(__location__
" Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
2796 talloc_free(tmp_ctx
);
2802 * Tell all nodes to run eventscripts to process the
2803 * "ipreallocated" event. This can do a lot of things,
2804 * including restarting services to reconfigure them if public
2805 * IPs have moved. Once upon a time this event only used to
2808 retry_data
= talloc_zero_array(tmp_ctx
, bool, nodemap
->num
);
2809 CTDB_NO_MEMORY_FATAL(ctdb
, retry_data
);
2810 iprealloc_data
.retry_nodes
= retry_data
;
2811 iprealloc_data
.retry_count
= 0;
2812 iprealloc_data
.fail_callback
= fail_callback
;
2813 iprealloc_data
.fail_callback_data
= callback_data
;
2814 iprealloc_data
.nodemap
= nodemap
;
2816 nodes
= list_of_connected_nodes(ctdb
, nodemap
, tmp_ctx
, true);
2817 ret
= ctdb_client_async_control(ctdb
, CTDB_CONTROL_IPREALLOCATED
,
2818 nodes
, 0, TAKEOVER_TIMEOUT(),
2820 NULL
, iprealloc_fail_callback
,
2823 /* If the control failed then we should retry to any
2824 * nodes flagged by iprealloc_fail_callback using the
2825 * EVENTSCRIPT control. This is a best-effort at
2826 * backward compatiblity when running a mixed cluster
2827 * where some nodes have not yet been upgraded to
2828 * support the IPREALLOCATED control.
2830 DEBUG(DEBUG_WARNING
,
2831 ("Retry ipreallocated to some nodes using eventscript control\n"));
2833 nodes
= talloc_array(tmp_ctx
, uint32_t,
2834 iprealloc_data
.retry_count
);
2835 CTDB_NO_MEMORY_FATAL(ctdb
, nodes
);
2838 for (i
=0; i
<nodemap
->num
; i
++) {
2839 if (iprealloc_data
.retry_nodes
[i
]) {
2845 data
.dptr
= discard_const("ipreallocated");
2846 data
.dsize
= strlen((char *)data
.dptr
) + 1;
2847 ret
= ctdb_client_async_control(ctdb
,
2848 CTDB_CONTROL_RUN_EVENTSCRIPTS
,
2849 nodes
, 0, TAKEOVER_TIMEOUT(),
2851 NULL
, fail_callback
,
2854 DEBUG(DEBUG_ERR
, (__location__
" failed to send control to run eventscripts with \"ipreallocated\"\n"));
2858 talloc_free(tmp_ctx
);
2864 destroy a ctdb_client_ip structure
2866 static int ctdb_client_ip_destructor(struct ctdb_client_ip
*ip
)
2868 DEBUG(DEBUG_DEBUG
,("destroying client tcp for %s:%u (client_id %u)\n",
2869 ctdb_addr_to_str(&ip
->addr
),
2870 ntohs(ip
->addr
.ip
.sin_port
),
2873 DLIST_REMOVE(ip
->ctdb
->client_ip_list
, ip
);
2878 called by a client to inform us of a TCP connection that it is managing
2879 that should tickled with an ACK when IP takeover is done
2880 we handle both the old ipv4 style of packets as well as the new ipv4/6
2883 int32_t ctdb_control_tcp_client(struct ctdb_context
*ctdb
, uint32_t client_id
,
2886 struct ctdb_client
*client
= ctdb_reqid_find(ctdb
, client_id
, struct ctdb_client
);
2887 struct ctdb_control_tcp
*old_addr
= NULL
;
2888 struct ctdb_control_tcp_addr new_addr
;
2889 struct ctdb_control_tcp_addr
*tcp_sock
= NULL
;
2890 struct ctdb_tcp_list
*tcp
;
2891 struct ctdb_tcp_connection t
;
2894 struct ctdb_client_ip
*ip
;
2895 struct ctdb_vnn
*vnn
;
2896 ctdb_sock_addr addr
;
2898 switch (indata
.dsize
) {
2899 case sizeof(struct ctdb_control_tcp
):
2900 old_addr
= (struct ctdb_control_tcp
*)indata
.dptr
;
2901 ZERO_STRUCT(new_addr
);
2902 tcp_sock
= &new_addr
;
2903 tcp_sock
->src
.ip
= old_addr
->src
;
2904 tcp_sock
->dest
.ip
= old_addr
->dest
;
2906 case sizeof(struct ctdb_control_tcp_addr
):
2907 tcp_sock
= (struct ctdb_control_tcp_addr
*)indata
.dptr
;
2910 DEBUG(DEBUG_ERR
,(__location__
" Invalid data structure passed "
2911 "to ctdb_control_tcp_client. size was %d but "
2912 "only allowed sizes are %lu and %lu\n",
2914 (long unsigned)sizeof(struct ctdb_control_tcp
),
2915 (long unsigned)sizeof(struct ctdb_control_tcp_addr
)));
2919 addr
= tcp_sock
->src
;
2920 ctdb_canonicalize_ip(&addr
, &tcp_sock
->src
);
2921 addr
= tcp_sock
->dest
;
2922 ctdb_canonicalize_ip(&addr
, &tcp_sock
->dest
);
2925 memcpy(&addr
, &tcp_sock
->dest
, sizeof(addr
));
2926 vnn
= find_public_ip_vnn(ctdb
, &addr
);
2928 switch (addr
.sa
.sa_family
) {
2930 if (ntohl(addr
.ip
.sin_addr
.s_addr
) != INADDR_LOOPBACK
) {
2931 DEBUG(DEBUG_ERR
,("Could not add client IP %s. This is not a public address.\n",
2932 ctdb_addr_to_str(&addr
)));
2936 DEBUG(DEBUG_ERR
,("Could not add client IP %s. This is not a public ipv6 address.\n",
2937 ctdb_addr_to_str(&addr
)));
2940 DEBUG(DEBUG_ERR
,(__location__
" Unknown family type %d\n", addr
.sa
.sa_family
));
2946 if (vnn
->pnn
!= ctdb
->pnn
) {
2947 DEBUG(DEBUG_ERR
,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
2948 ctdb_addr_to_str(&addr
),
2949 client_id
, client
->pid
));
2950 /* failing this call will tell smbd to die */
2954 ip
= talloc(client
, struct ctdb_client_ip
);
2955 CTDB_NO_MEMORY(ctdb
, ip
);
2959 ip
->client_id
= client_id
;
2960 talloc_set_destructor(ip
, ctdb_client_ip_destructor
);
2961 DLIST_ADD(ctdb
->client_ip_list
, ip
);
2963 tcp
= talloc(client
, struct ctdb_tcp_list
);
2964 CTDB_NO_MEMORY(ctdb
, tcp
);
2966 tcp
->connection
.src_addr
= tcp_sock
->src
;
2967 tcp
->connection
.dst_addr
= tcp_sock
->dest
;
2969 DLIST_ADD(client
->tcp_list
, tcp
);
2971 t
.src_addr
= tcp_sock
->src
;
2972 t
.dst_addr
= tcp_sock
->dest
;
2974 data
.dptr
= (uint8_t *)&t
;
2975 data
.dsize
= sizeof(t
);
2977 switch (addr
.sa
.sa_family
) {
2979 DEBUG(DEBUG_INFO
,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2980 (unsigned)ntohs(tcp_sock
->dest
.ip
.sin_port
),
2981 ctdb_addr_to_str(&tcp_sock
->src
),
2982 (unsigned)ntohs(tcp_sock
->src
.ip
.sin_port
), client_id
, client
->pid
));
2985 DEBUG(DEBUG_INFO
,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
2986 (unsigned)ntohs(tcp_sock
->dest
.ip6
.sin6_port
),
2987 ctdb_addr_to_str(&tcp_sock
->src
),
2988 (unsigned)ntohs(tcp_sock
->src
.ip6
.sin6_port
), client_id
, client
->pid
));
2991 DEBUG(DEBUG_ERR
,(__location__
" Unknown family %d\n", addr
.sa
.sa_family
));
2995 /* tell all nodes about this tcp connection */
2996 ret
= ctdb_daemon_send_control(ctdb
, CTDB_BROADCAST_CONNECTED
, 0,
2997 CTDB_CONTROL_TCP_ADD
,
2998 0, CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
);
3000 DEBUG(DEBUG_ERR
,(__location__
" Failed to send CTDB_CONTROL_TCP_ADD\n"));
3008 find a tcp address on a list
3010 static struct ctdb_tcp_connection
*ctdb_tcp_find(struct ctdb_tcp_array
*array
,
3011 struct ctdb_tcp_connection
*tcp
)
3015 if (array
== NULL
) {
3019 for (i
=0;i
<array
->num
;i
++) {
3020 if (ctdb_same_sockaddr(&array
->connections
[i
].src_addr
, &tcp
->src_addr
) &&
3021 ctdb_same_sockaddr(&array
->connections
[i
].dst_addr
, &tcp
->dst_addr
)) {
3022 return &array
->connections
[i
];
3031 called by a daemon to inform us of a TCP connection that one of its
3032 clients managing that should tickled with an ACK when IP takeover is
3035 int32_t ctdb_control_tcp_add(struct ctdb_context
*ctdb
, TDB_DATA indata
, bool tcp_update_needed
)
3037 struct ctdb_tcp_connection
*p
= (struct ctdb_tcp_connection
*)indata
.dptr
;
3038 struct ctdb_tcp_array
*tcparray
;
3039 struct ctdb_tcp_connection tcp
;
3040 struct ctdb_vnn
*vnn
;
3042 vnn
= find_public_ip_vnn(ctdb
, &p
->dst_addr
);
3044 DEBUG(DEBUG_INFO
,(__location__
" got TCP_ADD control for an address which is not a public address '%s'\n",
3045 ctdb_addr_to_str(&p
->dst_addr
)));
3051 tcparray
= vnn
->tcp_array
;
3053 /* If this is the first tickle */
3054 if (tcparray
== NULL
) {
3055 tcparray
= talloc(vnn
, struct ctdb_tcp_array
);
3056 CTDB_NO_MEMORY(ctdb
, tcparray
);
3057 vnn
->tcp_array
= tcparray
;
3060 tcparray
->connections
= talloc_size(tcparray
, sizeof(struct ctdb_tcp_connection
));
3061 CTDB_NO_MEMORY(ctdb
, tcparray
->connections
);
3063 tcparray
->connections
[tcparray
->num
].src_addr
= p
->src_addr
;
3064 tcparray
->connections
[tcparray
->num
].dst_addr
= p
->dst_addr
;
3067 if (tcp_update_needed
) {
3068 vnn
->tcp_update_needed
= true;
3074 /* Do we already have this tickle ?*/
3075 tcp
.src_addr
= p
->src_addr
;
3076 tcp
.dst_addr
= p
->dst_addr
;
3077 if (ctdb_tcp_find(tcparray
, &tcp
) != NULL
) {
3078 DEBUG(DEBUG_DEBUG
,("Already had tickle info for %s:%u for vnn:%u\n",
3079 ctdb_addr_to_str(&tcp
.dst_addr
),
3080 ntohs(tcp
.dst_addr
.ip
.sin_port
),
3085 /* A new tickle, we must add it to the array */
3086 tcparray
->connections
= talloc_realloc(tcparray
, tcparray
->connections
,
3087 struct ctdb_tcp_connection
,
3089 CTDB_NO_MEMORY(ctdb
, tcparray
->connections
);
3091 tcparray
->connections
[tcparray
->num
].src_addr
= p
->src_addr
;
3092 tcparray
->connections
[tcparray
->num
].dst_addr
= p
->dst_addr
;
3095 DEBUG(DEBUG_INFO
,("Added tickle info for %s:%u from vnn %u\n",
3096 ctdb_addr_to_str(&tcp
.dst_addr
),
3097 ntohs(tcp
.dst_addr
.ip
.sin_port
),
3100 if (tcp_update_needed
) {
3101 vnn
->tcp_update_needed
= true;
3109 called by a daemon to inform us of a TCP connection that one of its
3110 clients managing that should tickled with an ACK when IP takeover is
3113 static void ctdb_remove_tcp_connection(struct ctdb_context
*ctdb
, struct ctdb_tcp_connection
*conn
)
3115 struct ctdb_tcp_connection
*tcpp
;
3116 struct ctdb_vnn
*vnn
= find_public_ip_vnn(ctdb
, &conn
->dst_addr
);
3119 DEBUG(DEBUG_ERR
,(__location__
" unable to find public address %s\n",
3120 ctdb_addr_to_str(&conn
->dst_addr
)));
3124 /* if the array is empty we cant remove it
3125 and we dont need to do anything
3127 if (vnn
->tcp_array
== NULL
) {
3128 DEBUG(DEBUG_INFO
,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
3129 ctdb_addr_to_str(&conn
->dst_addr
),
3130 ntohs(conn
->dst_addr
.ip
.sin_port
)));
3135 /* See if we know this connection
3136 if we dont know this connection then we dont need to do anything
3138 tcpp
= ctdb_tcp_find(vnn
->tcp_array
, conn
);
3140 DEBUG(DEBUG_INFO
,("Trying to remove tickle that doesnt exist %s:%u\n",
3141 ctdb_addr_to_str(&conn
->dst_addr
),
3142 ntohs(conn
->dst_addr
.ip
.sin_port
)));
3147 /* We need to remove this entry from the array.
3148 Instead of allocating a new array and copying data to it
3149 we cheat and just copy the last entry in the existing array
3150 to the entry that is to be removed and just shring the
3153 *tcpp
= vnn
->tcp_array
->connections
[vnn
->tcp_array
->num
- 1];
3154 vnn
->tcp_array
->num
--;
3156 /* If we deleted the last entry we also need to remove the entire array
3158 if (vnn
->tcp_array
->num
== 0) {
3159 talloc_free(vnn
->tcp_array
);
3160 vnn
->tcp_array
= NULL
;
3163 vnn
->tcp_update_needed
= true;
3165 DEBUG(DEBUG_INFO
,("Removed tickle info for %s:%u\n",
3166 ctdb_addr_to_str(&conn
->src_addr
),
3167 ntohs(conn
->src_addr
.ip
.sin_port
)));
3172 called by a daemon to inform us of a TCP connection that one of its
3173 clients used are no longer needed in the tickle database
3175 int32_t ctdb_control_tcp_remove(struct ctdb_context
*ctdb
, TDB_DATA indata
)
3177 struct ctdb_tcp_connection
*conn
= (struct ctdb_tcp_connection
*)indata
.dptr
;
3179 ctdb_remove_tcp_connection(ctdb
, conn
);
3186 Called when another daemon starts - caises all tickles for all
3187 public addresses we are serving to be sent to the new node on the
3188 next check. This actually causes the next scheduled call to
3189 tdb_update_tcp_tickles() to update all nodes. This is simple and
3190 doesn't require careful error handling.
3192 int32_t ctdb_control_startup(struct ctdb_context
*ctdb
, uint32_t pnn
)
3194 struct ctdb_vnn
*vnn
;
3196 for (vnn
= ctdb
->vnn
; vnn
!= NULL
; vnn
= vnn
->next
) {
3197 vnn
->tcp_update_needed
= true;
3205 called when a client structure goes away - hook to remove
3206 elements from the tcp_list in all daemons
3208 void ctdb_takeover_client_destructor_hook(struct ctdb_client
*client
)
3210 while (client
->tcp_list
) {
3211 struct ctdb_tcp_list
*tcp
= client
->tcp_list
;
3212 DLIST_REMOVE(client
->tcp_list
, tcp
);
3213 ctdb_remove_tcp_connection(client
->ctdb
, &tcp
->connection
);
3219 release all IPs on shutdown
3221 void ctdb_release_all_ips(struct ctdb_context
*ctdb
)
3223 struct ctdb_vnn
*vnn
;
3226 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3227 if (!ctdb_sys_have_ip(&vnn
->public_address
)) {
3228 ctdb_vnn_unassign_iface(ctdb
, vnn
);
3235 DEBUG(DEBUG_INFO
,("Release of IP %s/%u on interface %s node:-1\n",
3236 ctdb_addr_to_str(&vnn
->public_address
),
3237 vnn
->public_netmask_bits
,
3238 ctdb_vnn_iface_string(vnn
)));
3240 ctdb_event_script_args(ctdb
, CTDB_EVENT_RELEASE_IP
, "%s %s %u",
3241 ctdb_vnn_iface_string(vnn
),
3242 ctdb_addr_to_str(&vnn
->public_address
),
3243 vnn
->public_netmask_bits
);
3244 release_kill_clients(ctdb
, &vnn
->public_address
);
3245 ctdb_vnn_unassign_iface(ctdb
, vnn
);
3249 DEBUG(DEBUG_NOTICE
,(__location__
" Released %d public IPs\n", count
));
3254 get list of public IPs
3256 int32_t ctdb_control_get_public_ips(struct ctdb_context
*ctdb
,
3257 struct ctdb_req_control
*c
, TDB_DATA
*outdata
)
3260 struct ctdb_all_public_ips
*ips
;
3261 struct ctdb_vnn
*vnn
;
3262 bool only_available
= false;
3264 if (c
->flags
& CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE
) {
3265 only_available
= true;
3268 /* count how many public ip structures we have */
3270 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3274 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
3275 num
*sizeof(struct ctdb_public_ip
);
3276 ips
= talloc_zero_size(outdata
, len
);
3277 CTDB_NO_MEMORY(ctdb
, ips
);
3280 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3281 if (only_available
&& !ctdb_vnn_available(ctdb
, vnn
)) {
3284 ips
->ips
[i
].pnn
= vnn
->pnn
;
3285 ips
->ips
[i
].addr
= vnn
->public_address
;
3289 len
= offsetof(struct ctdb_all_public_ips
, ips
) +
3290 i
*sizeof(struct ctdb_public_ip
);
3292 outdata
->dsize
= len
;
3293 outdata
->dptr
= (uint8_t *)ips
;
3300 get list of public IPs, old ipv4 style. only returns ipv4 addresses
3302 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context
*ctdb
,
3303 struct ctdb_req_control
*c
, TDB_DATA
*outdata
)
3306 struct ctdb_all_public_ipsv4
*ips
;
3307 struct ctdb_vnn
*vnn
;
3309 /* count how many public ip structures we have */
3311 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3312 if (vnn
->public_address
.sa
.sa_family
!= AF_INET
) {
3318 len
= offsetof(struct ctdb_all_public_ipsv4
, ips
) +
3319 num
*sizeof(struct ctdb_public_ipv4
);
3320 ips
= talloc_zero_size(outdata
, len
);
3321 CTDB_NO_MEMORY(ctdb
, ips
);
3323 outdata
->dsize
= len
;
3324 outdata
->dptr
= (uint8_t *)ips
;
3328 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3329 if (vnn
->public_address
.sa
.sa_family
!= AF_INET
) {
3332 ips
->ips
[i
].pnn
= vnn
->pnn
;
3333 ips
->ips
[i
].sin
= vnn
->public_address
.ip
;
3340 int32_t ctdb_control_get_public_ip_info(struct ctdb_context
*ctdb
,
3341 struct ctdb_req_control
*c
,
3346 ctdb_sock_addr
*addr
;
3347 struct ctdb_control_public_ip_info
*info
;
3348 struct ctdb_vnn
*vnn
;
3350 addr
= (ctdb_sock_addr
*)indata
.dptr
;
3352 vnn
= find_public_ip_vnn(ctdb
, addr
);
3354 /* if it is not a public ip it could be our 'single ip' */
3355 if (ctdb
->single_ip_vnn
) {
3356 if (ctdb_same_ip(&ctdb
->single_ip_vnn
->public_address
, addr
)) {
3357 vnn
= ctdb
->single_ip_vnn
;
3362 DEBUG(DEBUG_ERR
,(__location__
" Could not get public ip info, "
3363 "'%s'not a public address\n",
3364 ctdb_addr_to_str(addr
)));
3368 /* count how many public ip structures we have */
3370 for (;vnn
->ifaces
[num
];) {
3374 len
= offsetof(struct ctdb_control_public_ip_info
, ifaces
) +
3375 num
*sizeof(struct ctdb_control_iface_info
);
3376 info
= talloc_zero_size(outdata
, len
);
3377 CTDB_NO_MEMORY(ctdb
, info
);
3379 info
->ip
.addr
= vnn
->public_address
;
3380 info
->ip
.pnn
= vnn
->pnn
;
3381 info
->active_idx
= 0xFFFFFFFF;
3383 for (i
=0; vnn
->ifaces
[i
]; i
++) {
3384 struct ctdb_iface
*cur
;
3386 cur
= ctdb_find_iface(ctdb
, vnn
->ifaces
[i
]);
3388 DEBUG(DEBUG_CRIT
, (__location__
" internal error iface[%s] unknown\n",
3392 if (vnn
->iface
== cur
) {
3393 info
->active_idx
= i
;
3395 strncpy(info
->ifaces
[i
].name
, cur
->name
, sizeof(info
->ifaces
[i
].name
)-1);
3396 info
->ifaces
[i
].link_state
= cur
->link_up
;
3397 info
->ifaces
[i
].references
= cur
->references
;
3400 len
= offsetof(struct ctdb_control_public_ip_info
, ifaces
) +
3401 i
*sizeof(struct ctdb_control_iface_info
);
3403 outdata
->dsize
= len
;
3404 outdata
->dptr
= (uint8_t *)info
;
3409 int32_t ctdb_control_get_ifaces(struct ctdb_context
*ctdb
,
3410 struct ctdb_req_control
*c
,
3414 struct ctdb_control_get_ifaces
*ifaces
;
3415 struct ctdb_iface
*cur
;
3417 /* count how many public ip structures we have */
3419 for (cur
=ctdb
->ifaces
;cur
;cur
=cur
->next
) {
3423 len
= offsetof(struct ctdb_control_get_ifaces
, ifaces
) +
3424 num
*sizeof(struct ctdb_control_iface_info
);
3425 ifaces
= talloc_zero_size(outdata
, len
);
3426 CTDB_NO_MEMORY(ctdb
, ifaces
);
3429 for (cur
=ctdb
->ifaces
;cur
;cur
=cur
->next
) {
3430 strcpy(ifaces
->ifaces
[i
].name
, cur
->name
);
3431 ifaces
->ifaces
[i
].link_state
= cur
->link_up
;
3432 ifaces
->ifaces
[i
].references
= cur
->references
;
3436 len
= offsetof(struct ctdb_control_get_ifaces
, ifaces
) +
3437 i
*sizeof(struct ctdb_control_iface_info
);
3439 outdata
->dsize
= len
;
3440 outdata
->dptr
= (uint8_t *)ifaces
;
3445 int32_t ctdb_control_set_iface_link(struct ctdb_context
*ctdb
,
3446 struct ctdb_req_control
*c
,
3449 struct ctdb_control_iface_info
*info
;
3450 struct ctdb_iface
*iface
;
3451 bool link_up
= false;
3453 info
= (struct ctdb_control_iface_info
*)indata
.dptr
;
3455 if (info
->name
[CTDB_IFACE_SIZE
] != '\0') {
3456 int len
= strnlen(info
->name
, CTDB_IFACE_SIZE
);
3457 DEBUG(DEBUG_ERR
, (__location__
" name[%*.*s] not terminated\n",
3458 len
, len
, info
->name
));
3462 switch (info
->link_state
) {
3470 DEBUG(DEBUG_ERR
, (__location__
" link_state[%u] invalid\n",
3471 (unsigned int)info
->link_state
));
3475 if (info
->references
!= 0) {
3476 DEBUG(DEBUG_ERR
, (__location__
" references[%u] should be 0\n",
3477 (unsigned int)info
->references
));
3481 iface
= ctdb_find_iface(ctdb
, info
->name
);
3482 if (iface
== NULL
) {
3486 if (link_up
== iface
->link_up
) {
3490 DEBUG(iface
->link_up
?DEBUG_ERR
:DEBUG_NOTICE
,
3491 ("iface[%s] has changed it's link status %s => %s\n",
3493 iface
->link_up
?"up":"down",
3494 link_up
?"up":"down"));
3496 iface
->link_up
= link_up
;
3502 structure containing the listening socket and the list of tcp connections
3503 that the ctdb daemon is to kill
3505 struct ctdb_kill_tcp
{
3506 struct ctdb_vnn
*vnn
;
3507 struct ctdb_context
*ctdb
;
3509 struct fd_event
*fde
;
3510 trbt_tree_t
*connections
;
3515 a tcp connection that is to be killed
3517 struct ctdb_killtcp_con
{
3518 ctdb_sock_addr src_addr
;
3519 ctdb_sock_addr dst_addr
;
3521 struct ctdb_kill_tcp
*killtcp
;
3524 /* this function is used to create a key to represent this socketpair
3525 in the killtcp tree.
3526 this key is used to insert and lookup matching socketpairs that are
3527 to be tickled and RST
3529 #define KILLTCP_KEYLEN 10
3530 static uint32_t *killtcp_key(ctdb_sock_addr
*src
, ctdb_sock_addr
*dst
)
3532 static uint32_t key
[KILLTCP_KEYLEN
];
3534 bzero(key
, sizeof(key
));
3536 if (src
->sa
.sa_family
!= dst
->sa
.sa_family
) {
3537 DEBUG(DEBUG_ERR
, (__location__
" ERROR, different families passed :%u vs %u\n", src
->sa
.sa_family
, dst
->sa
.sa_family
));
3541 switch (src
->sa
.sa_family
) {
3543 key
[0] = dst
->ip
.sin_addr
.s_addr
;
3544 key
[1] = src
->ip
.sin_addr
.s_addr
;
3545 key
[2] = dst
->ip
.sin_port
;
3546 key
[3] = src
->ip
.sin_port
;
3549 uint32_t *dst6_addr32
=
3550 (uint32_t *)&(dst
->ip6
.sin6_addr
.s6_addr
);
3551 uint32_t *src6_addr32
=
3552 (uint32_t *)&(src
->ip6
.sin6_addr
.s6_addr
);
3553 key
[0] = dst6_addr32
[3];
3554 key
[1] = src6_addr32
[3];
3555 key
[2] = dst6_addr32
[2];
3556 key
[3] = src6_addr32
[2];
3557 key
[4] = dst6_addr32
[1];
3558 key
[5] = src6_addr32
[1];
3559 key
[6] = dst6_addr32
[0];
3560 key
[7] = src6_addr32
[0];
3561 key
[8] = dst
->ip6
.sin6_port
;
3562 key
[9] = src
->ip6
.sin6_port
;
3566 DEBUG(DEBUG_ERR
, (__location__
" ERROR, unknown family passed :%u\n", src
->sa
.sa_family
));
3574 called when we get a read event on the raw socket
3576 static void capture_tcp_handler(struct event_context
*ev
, struct fd_event
*fde
,
3577 uint16_t flags
, void *private_data
)
3579 struct ctdb_kill_tcp
*killtcp
= talloc_get_type(private_data
, struct ctdb_kill_tcp
);
3580 struct ctdb_killtcp_con
*con
;
3581 ctdb_sock_addr src
, dst
;
3582 uint32_t ack_seq
, seq
;
3584 if (!(flags
& EVENT_FD_READ
)) {
3588 if (ctdb_sys_read_tcp_packet(killtcp
->capture_fd
,
3589 killtcp
->private_data
,
3591 &ack_seq
, &seq
) != 0) {
3592 /* probably a non-tcp ACK packet */
3596 /* check if we have this guy in our list of connections
3599 con
= trbt_lookuparray32(killtcp
->connections
,
3600 KILLTCP_KEYLEN
, killtcp_key(&src
, &dst
));
3602 /* no this was some other packet we can just ignore */
3606 /* This one has been tickled !
3607 now reset him and remove him from the list.
3609 DEBUG(DEBUG_INFO
, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
3610 ntohs(con
->dst_addr
.ip
.sin_port
),
3611 ctdb_addr_to_str(&con
->src_addr
),
3612 ntohs(con
->src_addr
.ip
.sin_port
)));
3614 ctdb_sys_send_tcp(&con
->dst_addr
, &con
->src_addr
, ack_seq
, seq
, 1);
3619 /* when traversing the list of all tcp connections to send tickle acks to
3620 (so that we can capture the ack coming back and kill the connection
3622 this callback is called for each connection we are currently trying to kill
3624 static int tickle_connection_traverse(void *param
, void *data
)
3626 struct ctdb_killtcp_con
*con
= talloc_get_type(data
, struct ctdb_killtcp_con
);
3628 /* have tried too many times, just give up */
3629 if (con
->count
>= 5) {
3630 /* can't delete in traverse: reparent to delete_cons */
3631 talloc_steal(param
, con
);
3635 /* othervise, try tickling it again */
3638 (ctdb_sock_addr
*)&con
->dst_addr
,
3639 (ctdb_sock_addr
*)&con
->src_addr
,
3646 called every second until all sentenced connections have been reset
3648 static void ctdb_tickle_sentenced_connections(struct event_context
*ev
, struct timed_event
*te
,
3649 struct timeval t
, void *private_data
)
3651 struct ctdb_kill_tcp
*killtcp
= talloc_get_type(private_data
, struct ctdb_kill_tcp
);
3652 void *delete_cons
= talloc_new(NULL
);
3654 /* loop over all connections sending tickle ACKs */
3655 trbt_traversearray32(killtcp
->connections
, KILLTCP_KEYLEN
, tickle_connection_traverse
, delete_cons
);
3657 /* now we've finished traverse, it's safe to do deletion. */
3658 talloc_free(delete_cons
);
3660 /* If there are no more connections to kill we can remove the
3661 entire killtcp structure
3663 if ( (killtcp
->connections
== NULL
) ||
3664 (killtcp
->connections
->root
== NULL
) ) {
3665 talloc_free(killtcp
);
3669 /* try tickling them again in a seconds time
3671 event_add_timed(killtcp
->ctdb
->ev
, killtcp
, timeval_current_ofs(1, 0),
3672 ctdb_tickle_sentenced_connections
, killtcp
);
3676 destroy the killtcp structure
3678 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp
*killtcp
)
3680 struct ctdb_vnn
*tmpvnn
;
3682 /* verify that this vnn is still active */
3683 for (tmpvnn
= killtcp
->ctdb
->vnn
; tmpvnn
; tmpvnn
= tmpvnn
->next
) {
3684 if (tmpvnn
== killtcp
->vnn
) {
3689 if (tmpvnn
== NULL
) {
3693 if (killtcp
->vnn
->killtcp
!= killtcp
) {
3697 killtcp
->vnn
->killtcp
= NULL
;
3703 /* nothing fancy here, just unconditionally replace any existing
3704 connection structure with the new one.
3706 dont even free the old one if it did exist, that one is talloc_stolen
3707 by the same node in the tree anyway and will be deleted when the new data
3710 static void *add_killtcp_callback(void *parm
, void *data
)
3716 add a tcp socket to the list of connections we want to RST
3718 static int ctdb_killtcp_add_connection(struct ctdb_context
*ctdb
,
3722 ctdb_sock_addr src
, dst
;
3723 struct ctdb_kill_tcp
*killtcp
;
3724 struct ctdb_killtcp_con
*con
;
3725 struct ctdb_vnn
*vnn
;
3727 ctdb_canonicalize_ip(s
, &src
);
3728 ctdb_canonicalize_ip(d
, &dst
);
3730 vnn
= find_public_ip_vnn(ctdb
, &dst
);
3732 vnn
= find_public_ip_vnn(ctdb
, &src
);
3735 /* if it is not a public ip it could be our 'single ip' */
3736 if (ctdb
->single_ip_vnn
) {
3737 if (ctdb_same_ip(&ctdb
->single_ip_vnn
->public_address
, &dst
)) {
3738 vnn
= ctdb
->single_ip_vnn
;
3743 DEBUG(DEBUG_ERR
,(__location__
" Could not killtcp, not a public address\n"));
3747 killtcp
= vnn
->killtcp
;
3749 /* If this is the first connection to kill we must allocate
3752 if (killtcp
== NULL
) {
3753 killtcp
= talloc_zero(vnn
, struct ctdb_kill_tcp
);
3754 CTDB_NO_MEMORY(ctdb
, killtcp
);
3757 killtcp
->ctdb
= ctdb
;
3758 killtcp
->capture_fd
= -1;
3759 killtcp
->connections
= trbt_create(killtcp
, 0);
3761 vnn
->killtcp
= killtcp
;
3762 talloc_set_destructor(killtcp
, ctdb_killtcp_destructor
);
3767 /* create a structure that describes this connection we want to
3768 RST and store it in killtcp->connections
3770 con
= talloc(killtcp
, struct ctdb_killtcp_con
);
3771 CTDB_NO_MEMORY(ctdb
, con
);
3772 con
->src_addr
= src
;
3773 con
->dst_addr
= dst
;
3775 con
->killtcp
= killtcp
;
3778 trbt_insertarray32_callback(killtcp
->connections
,
3779 KILLTCP_KEYLEN
, killtcp_key(&con
->dst_addr
, &con
->src_addr
),
3780 add_killtcp_callback
, con
);
3783 If we dont have a socket to listen on yet we must create it
3785 if (killtcp
->capture_fd
== -1) {
3786 const char *iface
= ctdb_vnn_iface_string(vnn
);
3787 killtcp
->capture_fd
= ctdb_sys_open_capture_socket(iface
, &killtcp
->private_data
);
3788 if (killtcp
->capture_fd
== -1) {
3789 DEBUG(DEBUG_CRIT
,(__location__
" Failed to open capturing "
3790 "socket on iface '%s' for killtcp (%s)\n",
3791 iface
, strerror(errno
)));
3797 if (killtcp
->fde
== NULL
) {
3798 killtcp
->fde
= event_add_fd(ctdb
->ev
, killtcp
, killtcp
->capture_fd
,
3800 capture_tcp_handler
, killtcp
);
3801 tevent_fd_set_auto_close(killtcp
->fde
);
3803 /* We also need to set up some events to tickle all these connections
3804 until they are all reset
3806 event_add_timed(ctdb
->ev
, killtcp
, timeval_current_ofs(1, 0),
3807 ctdb_tickle_sentenced_connections
, killtcp
);
3810 /* tickle him once now */
3819 talloc_free(vnn
->killtcp
);
3820 vnn
->killtcp
= NULL
;
3825 kill a TCP connection.
3827 int32_t ctdb_control_kill_tcp(struct ctdb_context
*ctdb
, TDB_DATA indata
)
3829 struct ctdb_control_killtcp
*killtcp
= (struct ctdb_control_killtcp
*)indata
.dptr
;
3831 return ctdb_killtcp_add_connection(ctdb
, &killtcp
->src_addr
, &killtcp
->dst_addr
);
3835 called by a daemon to inform us of the entire list of TCP tickles for
3836 a particular public address.
3837 this control should only be sent by the node that is currently serving
3838 that public address.
3840 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context
*ctdb
, TDB_DATA indata
)
3842 struct ctdb_control_tcp_tickle_list
*list
= (struct ctdb_control_tcp_tickle_list
*)indata
.dptr
;
3843 struct ctdb_tcp_array
*tcparray
;
3844 struct ctdb_vnn
*vnn
;
3846 /* We must at least have tickles.num or else we cant verify the size
3847 of the received data blob
3849 if (indata
.dsize
< offsetof(struct ctdb_control_tcp_tickle_list
,
3850 tickles
.connections
)) {
3851 DEBUG(DEBUG_ERR
,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
3855 /* verify that the size of data matches what we expect */
3856 if (indata
.dsize
< offsetof(struct ctdb_control_tcp_tickle_list
,
3857 tickles
.connections
)
3858 + sizeof(struct ctdb_tcp_connection
)
3859 * list
->tickles
.num
) {
3860 DEBUG(DEBUG_ERR
,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
3864 vnn
= find_public_ip_vnn(ctdb
, &list
->addr
);
3866 DEBUG(DEBUG_INFO
,(__location__
" Could not set tcp tickle list, '%s' is not a public address\n",
3867 ctdb_addr_to_str(&list
->addr
)));
3872 /* remove any old ticklelist we might have */
3873 talloc_free(vnn
->tcp_array
);
3874 vnn
->tcp_array
= NULL
;
3876 tcparray
= talloc(vnn
, struct ctdb_tcp_array
);
3877 CTDB_NO_MEMORY(ctdb
, tcparray
);
3879 tcparray
->num
= list
->tickles
.num
;
3881 tcparray
->connections
= talloc_array(tcparray
, struct ctdb_tcp_connection
, tcparray
->num
);
3882 CTDB_NO_MEMORY(ctdb
, tcparray
->connections
);
3884 memcpy(tcparray
->connections
, &list
->tickles
.connections
[0],
3885 sizeof(struct ctdb_tcp_connection
)*tcparray
->num
);
3887 /* We now have a new fresh tickle list array for this vnn */
3888 vnn
->tcp_array
= tcparray
;
3894 called to return the full list of tickles for the puclic address associated
3895 with the provided vnn
3897 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context
*ctdb
, TDB_DATA indata
, TDB_DATA
*outdata
)
3899 ctdb_sock_addr
*addr
= (ctdb_sock_addr
*)indata
.dptr
;
3900 struct ctdb_control_tcp_tickle_list
*list
;
3901 struct ctdb_tcp_array
*tcparray
;
3903 struct ctdb_vnn
*vnn
;
3905 vnn
= find_public_ip_vnn(ctdb
, addr
);
3907 DEBUG(DEBUG_ERR
,(__location__
" Could not get tcp tickle list, '%s' is not a public address\n",
3908 ctdb_addr_to_str(addr
)));
3913 tcparray
= vnn
->tcp_array
;
3915 num
= tcparray
->num
;
3920 outdata
->dsize
= offsetof(struct ctdb_control_tcp_tickle_list
,
3921 tickles
.connections
)
3922 + sizeof(struct ctdb_tcp_connection
) * num
;
3924 outdata
->dptr
= talloc_size(outdata
, outdata
->dsize
);
3925 CTDB_NO_MEMORY(ctdb
, outdata
->dptr
);
3926 list
= (struct ctdb_control_tcp_tickle_list
*)outdata
->dptr
;
3929 list
->tickles
.num
= num
;
3931 memcpy(&list
->tickles
.connections
[0], tcparray
->connections
,
3932 sizeof(struct ctdb_tcp_connection
) * num
);
3940 set the list of all tcp tickles for a public address
3942 static int ctdb_ctrl_set_tcp_tickles(struct ctdb_context
*ctdb
,
3943 struct timeval timeout
, uint32_t destnode
,
3944 ctdb_sock_addr
*addr
,
3945 struct ctdb_tcp_array
*tcparray
)
3949 struct ctdb_control_tcp_tickle_list
*list
;
3952 num
= tcparray
->num
;
3957 data
.dsize
= offsetof(struct ctdb_control_tcp_tickle_list
,
3958 tickles
.connections
) +
3959 sizeof(struct ctdb_tcp_connection
) * num
;
3960 data
.dptr
= talloc_size(ctdb
, data
.dsize
);
3961 CTDB_NO_MEMORY(ctdb
, data
.dptr
);
3963 list
= (struct ctdb_control_tcp_tickle_list
*)data
.dptr
;
3965 list
->tickles
.num
= num
;
3967 memcpy(&list
->tickles
.connections
[0], tcparray
->connections
, sizeof(struct ctdb_tcp_connection
) * num
);
3970 ret
= ctdb_daemon_send_control(ctdb
, CTDB_BROADCAST_CONNECTED
, 0,
3971 CTDB_CONTROL_SET_TCP_TICKLE_LIST
,
3972 0, CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
);
3974 DEBUG(DEBUG_ERR
,(__location__
" ctdb_control for set tcp tickles failed\n"));
3978 talloc_free(data
.dptr
);
3985 perform tickle updates if required
3987 static void ctdb_update_tcp_tickles(struct event_context
*ev
,
3988 struct timed_event
*te
,
3989 struct timeval t
, void *private_data
)
3991 struct ctdb_context
*ctdb
= talloc_get_type(private_data
, struct ctdb_context
);
3993 struct ctdb_vnn
*vnn
;
3995 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
3996 /* we only send out updates for public addresses that
3999 if (ctdb
->pnn
!= vnn
->pnn
) {
4002 /* We only send out the updates if we need to */
4003 if (!vnn
->tcp_update_needed
) {
4006 ret
= ctdb_ctrl_set_tcp_tickles(ctdb
,
4008 CTDB_BROADCAST_CONNECTED
,
4009 &vnn
->public_address
,
4012 DEBUG(DEBUG_ERR
,("Failed to send the tickle update for public address %s\n",
4013 ctdb_addr_to_str(&vnn
->public_address
)));
4017 event_add_timed(ctdb
->ev
, ctdb
->tickle_update_context
,
4018 timeval_current_ofs(ctdb
->tunable
.tickle_update_interval
, 0),
4019 ctdb_update_tcp_tickles
, ctdb
);
4024 start periodic update of tcp tickles
4026 void ctdb_start_tcp_tickle_update(struct ctdb_context
*ctdb
)
4028 ctdb
->tickle_update_context
= talloc_new(ctdb
);
4030 event_add_timed(ctdb
->ev
, ctdb
->tickle_update_context
,
4031 timeval_current_ofs(ctdb
->tunable
.tickle_update_interval
, 0),
4032 ctdb_update_tcp_tickles
, ctdb
);
4038 struct control_gratious_arp
{
4039 struct ctdb_context
*ctdb
;
4040 ctdb_sock_addr addr
;
4046 send a control_gratuitous arp
4048 static void send_gratious_arp(struct event_context
*ev
, struct timed_event
*te
,
4049 struct timeval t
, void *private_data
)
4052 struct control_gratious_arp
*arp
= talloc_get_type(private_data
,
4053 struct control_gratious_arp
);
4055 ret
= ctdb_sys_send_arp(&arp
->addr
, arp
->iface
);
4057 DEBUG(DEBUG_ERR
,(__location__
" sending of gratious arp on iface '%s' failed (%s)\n",
4058 arp
->iface
, strerror(errno
)));
4063 if (arp
->count
== CTDB_ARP_REPEAT
) {
4068 event_add_timed(arp
->ctdb
->ev
, arp
,
4069 timeval_current_ofs(CTDB_ARP_INTERVAL
, 0),
4070 send_gratious_arp
, arp
);
4077 int32_t ctdb_control_send_gratious_arp(struct ctdb_context
*ctdb
, TDB_DATA indata
)
4079 struct ctdb_control_gratious_arp
*gratious_arp
= (struct ctdb_control_gratious_arp
*)indata
.dptr
;
4080 struct control_gratious_arp
*arp
;
4082 /* verify the size of indata */
4083 if (indata
.dsize
< offsetof(struct ctdb_control_gratious_arp
, iface
)) {
4084 DEBUG(DEBUG_ERR
,(__location__
" Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
4085 (unsigned)indata
.dsize
,
4086 (unsigned)offsetof(struct ctdb_control_gratious_arp
, iface
)));
4090 ( offsetof(struct ctdb_control_gratious_arp
, iface
)
4091 + gratious_arp
->len
) ){
4093 DEBUG(DEBUG_ERR
,(__location__
" Wrong size of indata. Was %u bytes "
4094 "but should be %u bytes\n",
4095 (unsigned)indata
.dsize
,
4096 (unsigned)(offsetof(struct ctdb_control_gratious_arp
, iface
)+gratious_arp
->len
)));
4101 arp
= talloc(ctdb
, struct control_gratious_arp
);
4102 CTDB_NO_MEMORY(ctdb
, arp
);
4105 arp
->addr
= gratious_arp
->addr
;
4106 arp
->iface
= talloc_strdup(arp
, gratious_arp
->iface
);
4107 CTDB_NO_MEMORY(ctdb
, arp
->iface
);
4110 event_add_timed(arp
->ctdb
->ev
, arp
,
4111 timeval_zero(), send_gratious_arp
, arp
);
4116 int32_t ctdb_control_add_public_address(struct ctdb_context
*ctdb
, TDB_DATA indata
)
4118 struct ctdb_control_ip_iface
*pub
= (struct ctdb_control_ip_iface
*)indata
.dptr
;
4121 /* verify the size of indata */
4122 if (indata
.dsize
< offsetof(struct ctdb_control_ip_iface
, iface
)) {
4123 DEBUG(DEBUG_ERR
,(__location__
" Too small indata to hold a ctdb_control_ip_iface structure\n"));
4127 ( offsetof(struct ctdb_control_ip_iface
, iface
)
4130 DEBUG(DEBUG_ERR
,(__location__
" Wrong size of indata. Was %u bytes "
4131 "but should be %u bytes\n",
4132 (unsigned)indata
.dsize
,
4133 (unsigned)(offsetof(struct ctdb_control_ip_iface
, iface
)+pub
->len
)));
4137 DEBUG(DEBUG_NOTICE
,("Add IP %s\n", ctdb_addr_to_str(&pub
->addr
)));
4139 ret
= ctdb_add_public_address(ctdb
, &pub
->addr
, pub
->mask
, &pub
->iface
[0], true);
4142 DEBUG(DEBUG_ERR
,(__location__
" Failed to add public address\n"));
4150 called when releaseip event finishes for del_public_address
4152 static void delete_ip_callback(struct ctdb_context
*ctdb
, int status
,
4155 talloc_free(private_data
);
4158 int32_t ctdb_control_del_public_address(struct ctdb_context
*ctdb
, TDB_DATA indata
)
4160 struct ctdb_control_ip_iface
*pub
= (struct ctdb_control_ip_iface
*)indata
.dptr
;
4161 struct ctdb_vnn
*vnn
;
4164 /* verify the size of indata */
4165 if (indata
.dsize
< offsetof(struct ctdb_control_ip_iface
, iface
)) {
4166 DEBUG(DEBUG_ERR
,(__location__
" Too small indata to hold a ctdb_control_ip_iface structure\n"));
4170 ( offsetof(struct ctdb_control_ip_iface
, iface
)
4173 DEBUG(DEBUG_ERR
,(__location__
" Wrong size of indata. Was %u bytes "
4174 "but should be %u bytes\n",
4175 (unsigned)indata
.dsize
,
4176 (unsigned)(offsetof(struct ctdb_control_ip_iface
, iface
)+pub
->len
)));
4180 DEBUG(DEBUG_NOTICE
,("Delete IP %s\n", ctdb_addr_to_str(&pub
->addr
)));
4182 /* walk over all public addresses until we find a match */
4183 for (vnn
=ctdb
->vnn
;vnn
;vnn
=vnn
->next
) {
4184 if (ctdb_same_ip(&vnn
->public_address
, &pub
->addr
)) {
4185 TALLOC_CTX
*mem_ctx
= talloc_new(ctdb
);
4187 DLIST_REMOVE(ctdb
->vnn
, vnn
);
4188 talloc_steal(mem_ctx
, vnn
);
4189 ctdb_remove_orphaned_ifaces(ctdb
, vnn
, mem_ctx
);
4190 if (vnn
->pnn
!= ctdb
->pnn
) {
4191 if (vnn
->iface
!= NULL
) {
4192 ctdb_vnn_unassign_iface(ctdb
, vnn
);
4194 talloc_free(mem_ctx
);
4199 ret
= ctdb_event_script_callback(ctdb
,
4200 mem_ctx
, delete_ip_callback
, mem_ctx
,
4201 CTDB_EVENT_RELEASE_IP
,
4203 ctdb_vnn_iface_string(vnn
),
4204 ctdb_addr_to_str(&vnn
->public_address
),
4205 vnn
->public_netmask_bits
);
4206 if (vnn
->iface
!= NULL
) {
4207 ctdb_vnn_unassign_iface(ctdb
, vnn
);
4220 struct ipreallocated_callback_state
{
4221 struct ctdb_req_control
*c
;
4224 static void ctdb_ipreallocated_callback(struct ctdb_context
*ctdb
,
4225 int status
, void *p
)
4227 struct ipreallocated_callback_state
*state
=
4228 talloc_get_type(p
, struct ipreallocated_callback_state
);
4232 (" \"ipreallocated\" event script failed (status %d)\n",
4234 if (status
== -ETIME
) {
4235 ctdb_ban_self(ctdb
);
4239 ctdb_request_control_reply(ctdb
, state
->c
, NULL
, status
, NULL
);
4243 /* A control to run the ipreallocated event */
4244 int32_t ctdb_control_ipreallocated(struct ctdb_context
*ctdb
,
4245 struct ctdb_req_control
*c
,
4249 struct ipreallocated_callback_state
*state
;
4251 state
= talloc(ctdb
, struct ipreallocated_callback_state
);
4252 CTDB_NO_MEMORY(ctdb
, state
);
4254 DEBUG(DEBUG_INFO
,(__location__
" Running \"ipreallocated\" event\n"));
4256 ret
= ctdb_event_script_callback(ctdb
, state
,
4257 ctdb_ipreallocated_callback
, state
,
4258 CTDB_EVENT_IPREALLOCATED
,
4262 DEBUG(DEBUG_ERR
,("Failed to run \"ipreallocated\" event \n"));
4267 /* tell the control that we will be reply asynchronously */
4268 state
->c
= talloc_steal(state
, c
);
4269 *async_reply
= true;
4275 /* This function is called from the recovery daemon to verify that a remote
4276 node has the expected ip allocation.
4277 This is verified against ctdb->ip_tree
4279 int verify_remote_ip_allocation(struct ctdb_context
*ctdb
,
4280 struct ctdb_all_public_ips
*ips
,
4283 struct ctdb_public_ip_list
*tmp_ip
;
4286 if (ctdb
->ip_tree
== NULL
) {
4287 /* dont know the expected allocation yet, assume remote node
4296 for (i
=0; i
<ips
->num
; i
++) {
4297 tmp_ip
= trbt_lookuparray32(ctdb
->ip_tree
, IP_KEYLEN
, ip_key(&ips
->ips
[i
].addr
));
4298 if (tmp_ip
== NULL
) {
4299 DEBUG(DEBUG_ERR
,("Node %u has new or unknown public IP %s\n", pnn
, ctdb_addr_to_str(&ips
->ips
[i
].addr
)));
4303 if (tmp_ip
->pnn
== -1 || ips
->ips
[i
].pnn
== -1) {
4307 if (tmp_ip
->pnn
!= ips
->ips
[i
].pnn
) {
4309 ("Inconsistent IP allocation - node %u thinks %s is held by node %u while it is assigned to node %u\n",
4311 ctdb_addr_to_str(&ips
->ips
[i
].addr
),
4312 ips
->ips
[i
].pnn
, tmp_ip
->pnn
));
4320 int update_ip_assignment_tree(struct ctdb_context
*ctdb
, struct ctdb_public_ip
*ip
)
4322 struct ctdb_public_ip_list
*tmp_ip
;
4324 if (ctdb
->ip_tree
== NULL
) {
4325 DEBUG(DEBUG_ERR
,("No ctdb->ip_tree yet. Failed to update ip assignment\n"));
4329 tmp_ip
= trbt_lookuparray32(ctdb
->ip_tree
, IP_KEYLEN
, ip_key(&ip
->addr
));
4330 if (tmp_ip
== NULL
) {
4331 DEBUG(DEBUG_ERR
,(__location__
" Could not find record for address %s, update ip\n", ctdb_addr_to_str(&ip
->addr
)));
4335 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
));
4336 tmp_ip
->pnn
= ip
->pnn
;
4342 struct ctdb_reloadips_handle
{
4343 struct ctdb_context
*ctdb
;
4344 struct ctdb_req_control
*c
;
4348 struct fd_event
*fde
;
4351 static int ctdb_reloadips_destructor(struct ctdb_reloadips_handle
*h
)
4353 if (h
== h
->ctdb
->reload_ips
) {
4354 h
->ctdb
->reload_ips
= NULL
;
4357 ctdb_request_control_reply(h
->ctdb
, h
->c
, NULL
, h
->status
, NULL
);
4360 ctdb_kill(h
->ctdb
, h
->child
, SIGKILL
);
4364 static void ctdb_reloadips_timeout_event(struct event_context
*ev
,
4365 struct timed_event
*te
,
4366 struct timeval t
, void *private_data
)
4368 struct ctdb_reloadips_handle
*h
= talloc_get_type(private_data
, struct ctdb_reloadips_handle
);
4373 static void ctdb_reloadips_child_handler(struct event_context
*ev
, struct fd_event
*fde
,
4374 uint16_t flags
, void *private_data
)
4376 struct ctdb_reloadips_handle
*h
= talloc_get_type(private_data
, struct ctdb_reloadips_handle
);
4381 ret
= read(h
->fd
[0], &res
, 1);
4382 if (ret
< 1 || res
!= 0) {
4383 DEBUG(DEBUG_ERR
, (__location__
" Reloadips child process returned error\n"));
4391 static int ctdb_reloadips_child(struct ctdb_context
*ctdb
)
4393 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
4394 struct ctdb_all_public_ips
*ips
;
4395 struct ctdb_vnn
*vnn
;
4396 struct client_async_data
*async_data
;
4397 struct timeval timeout
;
4399 struct ctdb_client_control_state
*state
;
4403 CTDB_NO_MEMORY(ctdb
, mem_ctx
);
4405 /* Read IPs from local node */
4406 ret
= ctdb_ctrl_get_public_ips(ctdb
, TAKEOVER_TIMEOUT(),
4407 CTDB_CURRENT_NODE
, mem_ctx
, &ips
);
4410 ("Unable to fetch public IPs from local node\n"));
4411 talloc_free(mem_ctx
);
4415 /* Read IPs file - this is safe since this is a child process */
4417 if (ctdb_set_public_addresses(ctdb
, false) != 0) {
4418 DEBUG(DEBUG_ERR
,("Failed to re-read public addresses file\n"));
4419 talloc_free(mem_ctx
);
4423 async_data
= talloc_zero(mem_ctx
, struct client_async_data
);
4424 CTDB_NO_MEMORY(ctdb
, async_data
);
4426 /* Compare IPs between node and file for IPs to be deleted */
4427 for (i
= 0; i
< ips
->num
; i
++) {
4429 for (vnn
= ctdb
->vnn
; vnn
; vnn
= vnn
->next
) {
4430 if (ctdb_same_ip(&vnn
->public_address
,
4431 &ips
->ips
[i
].addr
)) {
4432 /* IP is still in file */
4438 /* Delete IP ips->ips[i] */
4439 struct ctdb_control_ip_iface
*pub
;
4442 ("IP %s no longer configured, deleting it\n",
4443 ctdb_addr_to_str(&ips
->ips
[i
].addr
)));
4445 pub
= talloc_zero(mem_ctx
,
4446 struct ctdb_control_ip_iface
);
4447 CTDB_NO_MEMORY(ctdb
, pub
);
4449 pub
->addr
= ips
->ips
[i
].addr
;
4453 timeout
= TAKEOVER_TIMEOUT();
4455 data
.dsize
= offsetof(struct ctdb_control_ip_iface
,
4457 data
.dptr
= (uint8_t *)pub
;
4459 state
= ctdb_control_send(ctdb
, CTDB_CURRENT_NODE
, 0,
4460 CTDB_CONTROL_DEL_PUBLIC_IP
,
4461 0, data
, async_data
,
4463 if (state
== NULL
) {
4466 " failed sending CTDB_CONTROL_DEL_PUBLIC_IP\n"));
4470 ctdb_client_async_add(async_data
, state
);
4474 /* Compare IPs between node and file for IPs to be added */
4476 for (vnn
= ctdb
->vnn
; vnn
; vnn
= vnn
->next
) {
4477 for (i
= 0; i
< ips
->num
; i
++) {
4478 if (ctdb_same_ip(&vnn
->public_address
,
4479 &ips
->ips
[i
].addr
)) {
4480 /* IP already on node */
4484 if (i
== ips
->num
) {
4485 /* Add IP ips->ips[i] */
4486 struct ctdb_control_ip_iface
*pub
;
4487 const char *ifaces
= NULL
;
4492 ("New IP %s configured, adding it\n",
4493 ctdb_addr_to_str(&vnn
->public_address
)));
4495 uint32_t pnn
= ctdb_get_pnn(ctdb
);
4497 data
.dsize
= sizeof(pnn
);
4498 data
.dptr
= (uint8_t *)&pnn
;
4500 ret
= ctdb_client_send_message(
4502 CTDB_BROADCAST_CONNECTED
,
4503 CTDB_SRVID_REBALANCE_NODE
,
4506 DEBUG(DEBUG_WARNING
,
4507 ("Failed to send message to force node reallocation - IPs may be unbalanced\n"));
4513 ifaces
= vnn
->ifaces
[0];
4515 while (vnn
->ifaces
[iface
] != NULL
) {
4516 ifaces
= talloc_asprintf(vnn
, "%s,%s", ifaces
,
4517 vnn
->ifaces
[iface
]);
4521 len
= strlen(ifaces
) + 1;
4522 pub
= talloc_zero_size(mem_ctx
,
4523 offsetof(struct ctdb_control_ip_iface
, iface
) + len
);
4524 CTDB_NO_MEMORY(ctdb
, pub
);
4526 pub
->addr
= vnn
->public_address
;
4527 pub
->mask
= vnn
->public_netmask_bits
;
4529 memcpy(&pub
->iface
[0], ifaces
, pub
->len
);
4531 timeout
= TAKEOVER_TIMEOUT();
4533 data
.dsize
= offsetof(struct ctdb_control_ip_iface
,
4535 data
.dptr
= (uint8_t *)pub
;
4537 state
= ctdb_control_send(ctdb
, CTDB_CURRENT_NODE
, 0,
4538 CTDB_CONTROL_ADD_PUBLIC_IP
,
4539 0, data
, async_data
,
4541 if (state
== NULL
) {
4544 " failed sending CTDB_CONTROL_ADD_PUBLIC_IP\n"));
4548 ctdb_client_async_add(async_data
, state
);
4552 if (ctdb_client_async_wait(ctdb
, async_data
) != 0) {
4553 DEBUG(DEBUG_ERR
,(__location__
" Add/delete IPs failed\n"));
4557 talloc_free(mem_ctx
);
4561 talloc_free(mem_ctx
);
4565 /* This control is sent to force the node to re-read the public addresses file
4566 and drop any addresses we should nnot longer host, and add new addresses
4567 that we are now able to host
4569 int32_t ctdb_control_reload_public_ips(struct ctdb_context
*ctdb
, struct ctdb_req_control
*c
, bool *async_reply
)
4571 struct ctdb_reloadips_handle
*h
;
4572 pid_t parent
= getpid();
4574 if (ctdb
->reload_ips
!= NULL
) {
4575 talloc_free(ctdb
->reload_ips
);
4576 ctdb
->reload_ips
= NULL
;
4579 h
= talloc(ctdb
, struct ctdb_reloadips_handle
);
4580 CTDB_NO_MEMORY(ctdb
, h
);
4585 if (pipe(h
->fd
) == -1) {
4586 DEBUG(DEBUG_ERR
,("Failed to create pipe for ctdb_freeze_lock\n"));
4591 h
->child
= ctdb_fork(ctdb
);
4592 if (h
->child
== (pid_t
)-1) {
4593 DEBUG(DEBUG_ERR
, ("Failed to fork a child for reloadips\n"));
4601 if (h
->child
== 0) {
4602 signed char res
= 0;
4605 debug_extra
= talloc_asprintf(NULL
, "reloadips:");
4607 ctdb_set_process_name("ctdb_reloadips");
4608 if (switch_from_server_to_client(ctdb
, "reloadips-child") != 0) {
4609 DEBUG(DEBUG_CRIT
,("ERROR: Failed to switch reloadips child into client mode\n"));
4612 res
= ctdb_reloadips_child(ctdb
);
4614 DEBUG(DEBUG_ERR
,("Failed to reload ips on local node\n"));
4618 write(h
->fd
[1], &res
, 1);
4619 /* make sure we die when our parent dies */
4620 while (ctdb_kill(ctdb
, parent
, 0) == 0 || errno
!= ESRCH
) {
4626 h
->c
= talloc_steal(h
, c
);
4629 set_close_on_exec(h
->fd
[0]);
4631 talloc_set_destructor(h
, ctdb_reloadips_destructor
);
4634 h
->fde
= event_add_fd(ctdb
->ev
, h
, h
->fd
[0],
4635 EVENT_FD_READ
, ctdb_reloadips_child_handler
,
4637 tevent_fd_set_auto_close(h
->fde
);
4639 event_add_timed(ctdb
->ev
, h
,
4640 timeval_current_ofs(120, 0),
4641 ctdb_reloadips_timeout_event
, h
);
4643 /* we reply later */
4644 *async_reply
= true;