4 Copyright (C) Ronnie Sahlberg 2007
5 Copyright (C) Andrew Tridgell 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "lib/events/events.h"
22 #include "lib/tdb/include/tdb.h"
23 #include "lib/util/dlinklist.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/wait.h"
27 #include "../include/ctdb_private.h"
28 #include "../common/rb_tree.h"
31 #define TAKEOVER_TIMEOUT() timeval_current_ofs(ctdb->tunable.takeover_timeout,0)
33 #define CTDB_ARP_INTERVAL 1
34 #define CTDB_ARP_REPEAT 3
36 struct ctdb_takeover_arp {
37 struct ctdb_context *ctdb;
40 struct ctdb_tcp_array *tcparray;
46 lists of tcp endpoints
48 struct ctdb_tcp_list {
49 struct ctdb_tcp_list *prev, *next;
50 struct ctdb_tcp_connection connection;
54 list of clients to kill on IP release
56 struct ctdb_client_ip {
57 struct ctdb_client_ip *prev, *next;
58 struct ctdb_context *ctdb;
67 static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te,
68 struct timeval t, void *private_data)
70 struct ctdb_takeover_arp *arp = talloc_get_type(private_data,
71 struct ctdb_takeover_arp);
73 struct ctdb_tcp_array *tcparray;
75 ret = ctdb_sys_send_arp(&arp->addr, arp->vnn->iface);
77 DEBUG(DEBUG_CRIT,(__location__ " sending of arp failed (%s)\n", strerror(errno)));
80 tcparray = arp->tcparray;
82 for (i=0;i<tcparray->num;i++) {
83 struct ctdb_tcp_connection *tcon;
85 tcon = &tcparray->connections[i];
86 DEBUG(DEBUG_INFO,("sending tcp tickle ack for %u->%s:%u\n",
87 (unsigned)ntohs(tcon->dst_addr.ip.sin_port),
88 ctdb_addr_to_str(&tcon->src_addr),
89 (unsigned)ntohs(tcon->src_addr.ip.sin_port)));
90 ret = ctdb_sys_send_tcp(
95 DEBUG(DEBUG_CRIT,(__location__ " Failed to send tcp tickle ack for %s\n",
96 ctdb_addr_to_str(&tcon->src_addr)));
103 if (arp->count == CTDB_ARP_REPEAT) {
108 event_add_timed(arp->ctdb->ev, arp->vnn->takeover_ctx,
109 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
110 ctdb_control_send_arp, arp);
113 struct takeover_callback_state {
114 struct ctdb_req_control *c;
115 ctdb_sock_addr *addr;
116 struct ctdb_vnn *vnn;
120 called when takeip event finishes
122 static void takeover_ip_callback(struct ctdb_context *ctdb, int status,
125 struct takeover_callback_state *state =
126 talloc_get_type(private_data, struct takeover_callback_state);
127 struct ctdb_takeover_arp *arp;
128 struct ctdb_tcp_array *tcparray;
131 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
132 ctdb_addr_to_str(state->addr),
134 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
139 if (!state->vnn->takeover_ctx) {
140 state->vnn->takeover_ctx = talloc_new(ctdb);
141 if (!state->vnn->takeover_ctx) {
146 arp = talloc_zero(state->vnn->takeover_ctx, struct ctdb_takeover_arp);
147 if (!arp) goto failed;
150 arp->addr = *state->addr;
151 arp->vnn = state->vnn;
153 tcparray = state->vnn->tcp_array;
155 /* add all of the known tcp connections for this IP to the
156 list of tcp connections to send tickle acks for */
157 arp->tcparray = talloc_steal(arp, tcparray);
159 state->vnn->tcp_array = NULL;
160 state->vnn->tcp_update_needed = true;
163 event_add_timed(arp->ctdb->ev, state->vnn->takeover_ctx,
164 timeval_zero(), ctdb_control_send_arp, arp);
166 /* the control succeeded */
167 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
172 ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL);
178 Find the vnn of the node that has a public ip address
179 returns -1 if the address is not known as a public address
181 static struct ctdb_vnn *find_public_ip_vnn(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
183 struct ctdb_vnn *vnn;
185 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
186 if (ctdb_same_ip(&vnn->public_address, addr)) {
196 take over an ip address
198 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
199 struct ctdb_req_control *c,
204 struct takeover_callback_state *state;
205 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
206 struct ctdb_vnn *vnn;
208 /* update out vnn list */
209 vnn = find_public_ip_vnn(ctdb, &pip->addr);
211 DEBUG(DEBUG_ERR,("takeoverip called for an ip '%s' that is not a public address\n",
212 ctdb_addr_to_str(&pip->addr)));
217 /* if our kernel already has this IP, do nothing */
218 if (ctdb_sys_have_ip(&pip->addr)) {
222 state = talloc(ctdb, struct takeover_callback_state);
223 CTDB_NO_MEMORY(ctdb, state);
225 state->c = talloc_steal(ctdb, c);
226 state->addr = talloc(ctdb, ctdb_sock_addr);
227 CTDB_NO_MEMORY(ctdb, state->addr);
229 *state->addr = pip->addr;
232 DEBUG(DEBUG_NOTICE,("Takeover of IP %s/%u on interface %s\n",
233 ctdb_addr_to_str(&pip->addr),
234 vnn->public_netmask_bits,
237 ret = ctdb_event_script_callback(ctdb,
238 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
239 state, takeover_ip_callback, state,
242 talloc_strdup(state, ctdb_addr_to_str(&pip->addr)),
243 vnn->public_netmask_bits);
246 DEBUG(DEBUG_ERR,(__location__ " Failed to takeover IP %s on interface %s\n",
247 ctdb_addr_to_str(&pip->addr),
253 /* tell ctdb_control.c that we will be replying asynchronously */
260 kill any clients that are registered with a IP that is being released
262 static void release_kill_clients(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
264 struct ctdb_client_ip *ip;
266 DEBUG(DEBUG_INFO,("release_kill_clients for ip %s\n",
267 ctdb_addr_to_str(addr)));
269 for (ip=ctdb->client_ip_list; ip; ip=ip->next) {
270 ctdb_sock_addr tmp_addr;
273 DEBUG(DEBUG_INFO,("checking for client %u with IP %s\n",
275 ctdb_addr_to_str(&ip->addr)));
277 if (ctdb_same_ip(&tmp_addr, addr)) {
278 struct ctdb_client *client = ctdb_reqid_find(ctdb,
281 DEBUG(DEBUG_INFO,("matched client %u with IP %s and pid %u\n",
283 ctdb_addr_to_str(&ip->addr),
286 if (client->pid != 0) {
287 DEBUG(DEBUG_INFO,(__location__ " Killing client pid %u for IP %s on client_id %u\n",
288 (unsigned)client->pid,
289 ctdb_addr_to_str(addr),
291 kill(client->pid, SIGKILL);
298 called when releaseip event finishes
300 static void release_ip_callback(struct ctdb_context *ctdb, int status,
303 struct takeover_callback_state *state =
304 talloc_get_type(private_data, struct takeover_callback_state);
307 /* send a message to all clients of this node telling them
308 that the cluster has been reconfigured and they should
309 release any sockets on this IP */
310 data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
311 data.dsize = strlen((char *)data.dptr)+1;
313 DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
315 ctdb_daemon_send_message(ctdb, ctdb->pnn, CTDB_SRVID_RELEASE_IP, data);
317 /* kill clients that have registered with this IP */
318 release_kill_clients(ctdb, state->addr);
320 /* the control succeeded */
321 ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
326 release an ip address
328 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
329 struct ctdb_req_control *c,
334 struct takeover_callback_state *state;
335 struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr;
336 struct ctdb_vnn *vnn;
338 /* update our vnn list */
339 vnn = find_public_ip_vnn(ctdb, &pip->addr);
341 DEBUG(DEBUG_ERR,("takeoverip called for an ip '%s' that is not a public address\n",
342 ctdb_addr_to_str(&pip->addr)));
347 /* stop any previous arps */
348 talloc_free(vnn->takeover_ctx);
349 vnn->takeover_ctx = NULL;
351 if (!ctdb_sys_have_ip(&pip->addr)) {
352 DEBUG(DEBUG_INFO,("Redundant release of IP %s/%u on interface %s (ip not held)\n",
353 ctdb_addr_to_str(&pip->addr),
354 vnn->public_netmask_bits,
359 DEBUG(DEBUG_NOTICE,("Release of IP %s/%u on interface %s\n",
360 ctdb_addr_to_str(&pip->addr),
361 vnn->public_netmask_bits,
364 state = talloc(ctdb, struct takeover_callback_state);
365 CTDB_NO_MEMORY(ctdb, state);
367 state->c = talloc_steal(state, c);
368 state->addr = talloc(state, ctdb_sock_addr);
369 CTDB_NO_MEMORY(ctdb, state->addr);
370 *state->addr = pip->addr;
373 ret = ctdb_event_script_callback(ctdb,
374 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
375 state, release_ip_callback, state,
376 "releaseip %s %s %u",
378 talloc_strdup(state, ctdb_addr_to_str(&pip->addr)),
379 vnn->public_netmask_bits);
381 DEBUG(DEBUG_ERR,(__location__ " Failed to release IP %s on interface %s\n",
382 ctdb_addr_to_str(&pip->addr),
388 /* tell the control that we will be reply asynchronously */
395 static int ctdb_add_public_address(struct ctdb_context *ctdb, ctdb_sock_addr *addr, unsigned mask, const char *iface)
397 struct ctdb_vnn *vnn;
399 /* Verify that we dont have an entry for this ip yet */
400 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
401 if (ctdb_same_sockaddr(addr, &vnn->public_address)) {
402 DEBUG(DEBUG_CRIT,("Same ip '%s' specified multiple times in the public address list \n",
403 ctdb_addr_to_str(addr)));
408 /* create a new vnn structure for this ip address */
409 vnn = talloc_zero(ctdb, struct ctdb_vnn);
410 CTDB_NO_MEMORY_FATAL(ctdb, vnn);
411 vnn->iface = talloc_strdup(vnn, iface);
412 vnn->public_address = *addr;
413 vnn->public_netmask_bits = mask;
416 DLIST_ADD(ctdb->vnn, vnn);
423 setup the event script directory
425 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir)
427 ctdb->event_script_dir = talloc_strdup(ctdb, script_dir);
428 CTDB_NO_MEMORY(ctdb, ctdb->event_script_dir);
433 setup the public address lists from a file
435 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist)
441 lines = file_lines_load(alist, &nlines, ctdb);
443 ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", alist);
446 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
450 for (i=0;i<nlines;i++) {
456 tok = strtok(lines[i], " \t");
457 if (!tok || !parse_ip_mask(tok, &addr, &mask)) {
458 DEBUG(DEBUG_CRIT,("Badly formed line %u in public address list\n", i+1));
462 tok = strtok(NULL, " \t");
464 if (NULL == ctdb->default_public_interface) {
465 DEBUG(DEBUG_CRIT,("No default public interface and no interface specified at line %u of public address list\n",
470 iface = ctdb->default_public_interface;
475 if (ctdb_add_public_address(ctdb, &addr, mask, iface)) {
476 DEBUG(DEBUG_CRIT,("Failed to add line %u to the public address list\n", i+1));
489 struct ctdb_public_ip_list {
490 struct ctdb_public_ip_list *next;
496 /* Given a physical node, return the number of
497 public addresses that is currently assigned to this node.
499 static int node_ip_coverage(struct ctdb_context *ctdb,
501 struct ctdb_public_ip_list *ips)
505 for (;ips;ips=ips->next) {
506 if (ips->pnn == pnn) {
514 /* Check if this is a public ip known to the node, i.e. can that
515 node takeover this ip ?
517 static int can_node_serve_ip(struct ctdb_context *ctdb, int32_t pnn,
518 struct ctdb_public_ip_list *ip)
520 struct ctdb_all_public_ips *public_ips;
523 public_ips = ctdb->nodes[pnn]->public_ips;
525 if (public_ips == NULL) {
529 for (i=0;i<public_ips->num;i++) {
530 if (ctdb_same_ip(&ip->addr, &public_ips->ips[i].addr)) {
531 /* yes, this node can serve this public ip */
540 /* search the node lists list for a node to takeover this ip.
541 pick the node that currently are serving the least number of ips
542 so that the ips get spread out evenly.
544 static int find_takeover_node(struct ctdb_context *ctdb,
545 struct ctdb_node_map *nodemap, uint32_t mask,
546 struct ctdb_public_ip_list *ip,
547 struct ctdb_public_ip_list *all_ips)
553 for (i=0;i<nodemap->num;i++) {
554 if (nodemap->nodes[i].flags & mask) {
555 /* This node is not healty and can not be used to serve
561 /* verify that this node can serve this ip */
562 if (can_node_serve_ip(ctdb, i, ip)) {
563 /* no it couldnt so skip to the next node */
567 num = node_ip_coverage(ctdb, i, all_ips);
568 /* was this the first node we checked ? */
580 DEBUG(DEBUG_WARNING,(__location__ " Could not find node to take over public address '%s'\n",
581 ctdb_addr_to_str(&ip->addr)));
590 struct ctdb_public_ip_list *
591 add_ip_to_merged_list(struct ctdb_context *ctdb,
593 struct ctdb_public_ip_list *ip_list,
594 struct ctdb_public_ip *ip)
596 struct ctdb_public_ip_list *tmp_ip;
598 /* do we already have this ip in our merged list ?*/
599 for (tmp_ip=ip_list;tmp_ip;tmp_ip=tmp_ip->next) {
601 /* we already have this public ip in the list */
602 if (ctdb_same_ip(&tmp_ip->addr, &ip->addr)) {
607 /* this is a new public ip, we must add it to the list */
608 tmp_ip = talloc_zero(tmp_ctx, struct ctdb_public_ip_list);
609 CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
610 tmp_ip->pnn = ip->pnn;
611 tmp_ip->addr = ip->addr;
612 tmp_ip->next = ip_list;
617 struct ctdb_public_ip_list *
618 create_merged_ip_list(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx)
621 struct ctdb_public_ip_list *ip_list = NULL;
622 struct ctdb_all_public_ips *public_ips;
624 for (i=0;i<ctdb->num_nodes;i++) {
625 public_ips = ctdb->nodes[i]->public_ips;
627 /* there were no public ips for this node */
628 if (public_ips == NULL) {
632 for (j=0;j<public_ips->num;j++) {
633 ip_list = add_ip_to_merged_list(ctdb, tmp_ctx,
634 ip_list, &public_ips->ips[j]);
642 make any IP alias changes for public addresses that are necessary
644 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap)
646 int i, num_healthy, retries;
647 struct ctdb_public_ip ip;
649 struct ctdb_public_ip_list *all_ips, *tmp_ip;
650 int maxnode, maxnum=0, minnode, minnum=0, num;
652 struct timeval timeout;
653 struct client_async_data *async_data;
654 struct ctdb_client_control_state *state;
655 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
660 /* Count how many completely healthy nodes we have */
662 for (i=0;i<nodemap->num;i++) {
663 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
668 if (num_healthy > 0) {
669 /* We have healthy nodes, so only consider them for
670 serving public addresses
672 mask = NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED;
674 /* We didnt have any completely healthy nodes so
675 use "disabled" nodes as a fallback
677 mask = NODE_FLAGS_INACTIVE;
680 /* since nodes only know about those public addresses that
681 can be served by that particular node, no single node has
682 a full list of all public addresses that exist in the cluster.
683 Walk over all node structures and create a merged list of
684 all public addresses that exist in the cluster.
686 all_ips = create_merged_ip_list(ctdb, tmp_ctx);
688 /* If we want deterministic ip allocations, i.e. that the ip addresses
689 will always be allocated the same way for a specific set of
690 available/unavailable nodes.
692 if (1 == ctdb->tunable.deterministic_public_ips) {
693 DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
694 for (i=0,tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next,i++) {
695 tmp_ip->pnn = i%nodemap->num;
700 /* mark all public addresses with a masked node as being served by
703 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
704 if (tmp_ip->pnn == -1) {
707 if (nodemap->nodes[tmp_ip->pnn].flags & mask) {
712 /* verify that the assigned nodes can serve that public ip
713 and set it to -1 if not
715 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
716 if (tmp_ip->pnn == -1) {
719 if (can_node_serve_ip(ctdb, tmp_ip->pnn, tmp_ip) != 0) {
720 /* this node can not serve this ip. */
726 /* now we must redistribute all public addresses with takeover node
727 -1 among the nodes available
731 /* loop over all ip's and find a physical node to cover for
734 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
735 if (tmp_ip->pnn == -1) {
736 if (find_takeover_node(ctdb, nodemap, mask, tmp_ip, all_ips)) {
737 DEBUG(DEBUG_WARNING,("Failed to find node to cover ip %s\n",
738 ctdb_addr_to_str(&tmp_ip->addr)));
743 /* If we dont want ips to fail back after a node becomes healthy
744 again, we wont even try to reallocat the ip addresses so that
745 they are evenly spread out.
746 This can NOT be used at the same time as DeterministicIPs !
748 if (1 == ctdb->tunable.no_ip_failback) {
749 if (1 == ctdb->tunable.deterministic_public_ips) {
750 DEBUG(DEBUG_ERR, ("ERROR: You can not use 'DeterministicIPs' and 'NoIPFailback' at the same time\n"));
756 /* now, try to make sure the ip adresses are evenly distributed
758 for each ip address, loop over all nodes that can serve this
759 ip and make sure that the difference between the node
760 serving the most and the node serving the least ip's are not greater
763 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
764 if (tmp_ip->pnn == -1) {
768 /* Get the highest and lowest number of ips's served by any
769 valid node which can serve this ip.
773 for (i=0;i<nodemap->num;i++) {
774 if (nodemap->nodes[i].flags & mask) {
778 /* only check nodes that can actually serve this ip */
779 if (can_node_serve_ip(ctdb, i, tmp_ip)) {
780 /* no it couldnt so skip to the next node */
784 num = node_ip_coverage(ctdb, i, all_ips);
805 DEBUG(DEBUG_WARNING,(__location__ " Could not find maxnode. May not be able to serve ip '%s'\n",
806 ctdb_addr_to_str(&tmp_ip->addr)));
811 /* If we want deterministic IPs then dont try to reallocate
812 them to spread out the load.
814 if (1 == ctdb->tunable.deterministic_public_ips) {
818 /* if the spread between the smallest and largest coverage by
819 a node is >=2 we steal one of the ips from the node with
820 most coverage to even things out a bit.
821 try to do this at most 5 times since we dont want to spend
822 too much time balancing the ip coverage.
824 if ( (maxnum > minnum+1)
826 struct ctdb_public_ip_list *tmp;
828 /* mark one of maxnode's vnn's as unassigned and try
831 for (tmp=all_ips;tmp;tmp=tmp->next) {
832 if (tmp->pnn == maxnode) {
842 /* finished distributing the public addresses, now just send the
843 info out to the nodes
847 /* at this point ->pnn is the node which will own each IP
848 or -1 if there is no node that can cover this ip
851 /* now tell all nodes to delete any alias that they should not
852 have. This will be a NOOP on nodes that don't currently
853 hold the given alias */
854 async_data = talloc_zero(tmp_ctx, struct client_async_data);
855 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
857 for (i=0;i<nodemap->num;i++) {
858 /* don't talk to unconnected nodes, but do talk to banned nodes */
859 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
863 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
864 if (tmp_ip->pnn == nodemap->nodes[i].pnn) {
865 /* This node should be serving this
866 vnn so dont tell it to release the ip
870 ip.pnn = tmp_ip->pnn;
871 ip.addr = tmp_ip->addr;
873 timeout = TAKEOVER_TIMEOUT();
874 data.dsize = sizeof(ip);
875 data.dptr = (uint8_t *)&ip;
876 state = ctdb_control_send(ctdb, nodemap->nodes[i].pnn,
877 0, CTDB_CONTROL_RELEASE_IP, 0,
881 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_RELEASE_IP to node %u\n", nodemap->nodes[i].pnn));
882 talloc_free(tmp_ctx);
886 ctdb_client_async_add(async_data, state);
889 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
890 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
891 talloc_free(tmp_ctx);
894 talloc_free(async_data);
897 /* tell all nodes to get their own IPs */
898 async_data = talloc_zero(tmp_ctx, struct client_async_data);
899 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
900 for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
901 if (tmp_ip->pnn == -1) {
902 /* this IP won't be taken over */
905 ip.pnn = tmp_ip->pnn;
906 ip.addr = tmp_ip->addr;
908 timeout = TAKEOVER_TIMEOUT();
909 data.dsize = sizeof(ip);
910 data.dptr = (uint8_t *)&ip;
911 state = ctdb_control_send(ctdb, tmp_ip->pnn,
912 0, CTDB_CONTROL_TAKEOVER_IP, 0,
916 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control CTDB_CONTROL_TAKEOVER_IP to node %u\n", tmp_ip->pnn));
917 talloc_free(tmp_ctx);
921 ctdb_client_async_add(async_data, state);
923 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
924 DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
925 talloc_free(tmp_ctx);
929 talloc_free(tmp_ctx);
935 destroy a ctdb_client_ip structure
937 static int ctdb_client_ip_destructor(struct ctdb_client_ip *ip)
939 DEBUG(DEBUG_DEBUG,("destroying client tcp for %s:%u (client_id %u)\n",
940 ctdb_addr_to_str(&ip->addr),
941 ntohs(ip->addr.ip.sin_port),
944 DLIST_REMOVE(ip->ctdb->client_ip_list, ip);
949 called by a client to inform us of a TCP connection that it is managing
950 that should tickled with an ACK when IP takeover is done
952 //qqq we need a new version of this control that takes ctdb_sock_addr
953 //and have samba move to that instead.
955 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
958 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
959 struct ctdb_control_tcp *p = (struct ctdb_control_tcp *)indata.dptr;
960 struct ctdb_tcp_list *tcp;
961 struct ctdb_control_tcp_vnn t;
964 struct ctdb_client_ip *ip;
965 struct ctdb_vnn *vnn;
970 vnn = find_public_ip_vnn(ctdb, &addr);
972 if (ntohl(p->dest.sin_addr.s_addr) != INADDR_LOOPBACK) {
973 DEBUG(DEBUG_INFO,("Could not add client IP %s. This is not a public address.\n",
974 ctdb_addr_to_str((ctdb_sock_addr *)&p->dest)));
979 if (vnn->pnn != ctdb->pnn) {
980 DEBUG(DEBUG_ERR,("Attempt to register tcp client for IP %s we don't hold - failing (client_id %u pid %u)\n",
981 ctdb_addr_to_str((ctdb_sock_addr *)&p->dest),
982 client_id, client->pid));
983 /* failing this call will tell smbd to die */
987 ip = talloc(client, struct ctdb_client_ip);
988 CTDB_NO_MEMORY(ctdb, ip);
991 ip->addr.ip = p->dest;
992 ip->client_id = client_id;
993 talloc_set_destructor(ip, ctdb_client_ip_destructor);
994 DLIST_ADD(ctdb->client_ip_list, ip);
996 tcp = talloc(client, struct ctdb_tcp_list);
997 CTDB_NO_MEMORY(ctdb, tcp);
999 tcp->connection.src_addr.ip = p->src;
1000 tcp->connection.dst_addr.ip = p->dest;
1002 DLIST_ADD(client->tcp_list, tcp);
1005 t.dest.ip = p->dest;
1007 data.dptr = (uint8_t *)&t;
1008 data.dsize = sizeof(t);
1010 DEBUG(DEBUG_INFO,("registered tcp client for %u->%s:%u (client_id %u pid %u)\n",
1011 (unsigned)ntohs(p->dest.sin_port),
1012 ctdb_addr_to_str((ctdb_sock_addr *)&p->src),
1013 (unsigned)ntohs(p->src.sin_port), client_id, client->pid));
1015 /* tell all nodes about this tcp connection */
1016 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
1017 CTDB_CONTROL_TCP_ADD,
1018 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
1020 DEBUG(DEBUG_ERR,(__location__ " Failed to send CTDB_CONTROL_TCP_ADD\n"));
1028 find a tcp address on a list
1030 static struct ctdb_tcp_connection *ctdb_tcp_find(struct ctdb_tcp_array *array,
1031 struct ctdb_tcp_connection *tcp)
1035 if (array == NULL) {
1039 for (i=0;i<array->num;i++) {
1040 if (ctdb_same_sockaddr(&array->connections[i].src_addr, &tcp->src_addr) &&
1041 ctdb_same_sockaddr(&array->connections[i].dst_addr, &tcp->dst_addr)) {
1042 return &array->connections[i];
1049 called by a daemon to inform us of a TCP connection that one of its
1050 clients managing that should tickled with an ACK when IP takeover is
1053 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata)
1055 struct ctdb_control_tcp_vnn *p = (struct ctdb_control_tcp_vnn *)indata.dptr;
1056 struct ctdb_tcp_array *tcparray;
1057 struct ctdb_tcp_connection tcp;
1058 struct ctdb_vnn *vnn;
1060 vnn = find_public_ip_vnn(ctdb, &p->dest);
1062 DEBUG(DEBUG_ERR,(__location__ " got TCP_ADD control for an address which is not a public address '%s'\n",
1063 ctdb_addr_to_str(&p->dest)));
1069 tcparray = vnn->tcp_array;
1071 /* If this is the first tickle */
1072 if (tcparray == NULL) {
1073 tcparray = talloc_size(ctdb->nodes,
1074 offsetof(struct ctdb_tcp_array, connections) +
1075 sizeof(struct ctdb_tcp_connection) * 1);
1076 CTDB_NO_MEMORY(ctdb, tcparray);
1077 vnn->tcp_array = tcparray;
1080 tcparray->connections = talloc_size(tcparray, sizeof(struct ctdb_tcp_connection));
1081 CTDB_NO_MEMORY(ctdb, tcparray->connections);
1083 tcparray->connections[tcparray->num].src_addr = p->src;
1084 tcparray->connections[tcparray->num].dst_addr = p->dest;
1090 /* Do we already have this tickle ?*/
1091 tcp.src_addr = p->src;
1092 tcp.dst_addr = p->dest;
1093 if (ctdb_tcp_find(vnn->tcp_array, &tcp) != NULL) {
1094 DEBUG(DEBUG_DEBUG,("Already had tickle info for %s:%u for vnn:%u\n",
1095 ctdb_addr_to_str(&tcp.dst_addr),
1096 ntohs(tcp.dst_addr.ip.sin_port),
1101 /* A new tickle, we must add it to the array */
1102 tcparray->connections = talloc_realloc(tcparray, tcparray->connections,
1103 struct ctdb_tcp_connection,
1105 CTDB_NO_MEMORY(ctdb, tcparray->connections);
1107 vnn->tcp_array = tcparray;
1108 tcparray->connections[tcparray->num].src_addr = p->src;
1109 tcparray->connections[tcparray->num].dst_addr = p->dest;
1112 DEBUG(DEBUG_INFO,("Added tickle info for %s:%u from vnn %u\n",
1113 ctdb_addr_to_str(&tcp.dst_addr),
1114 ntohs(tcp.dst_addr.ip.sin_port),
1122 called by a daemon to inform us of a TCP connection that one of its
1123 clients managing that should tickled with an ACK when IP takeover is
1126 static void ctdb_remove_tcp_connection(struct ctdb_context *ctdb, struct ctdb_tcp_connection *conn)
1128 struct ctdb_tcp_connection *tcpp;
1129 struct ctdb_vnn *vnn = find_public_ip_vnn(ctdb, &conn->dst_addr);
1132 DEBUG(DEBUG_ERR,(__location__ " unable to find public address %s\n",
1133 ctdb_addr_to_str(&conn->dst_addr)));
1137 /* if the array is empty we cant remove it
1138 and we dont need to do anything
1140 if (vnn->tcp_array == NULL) {
1141 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist (array is empty) %s:%u\n",
1142 ctdb_addr_to_str(&conn->dst_addr),
1143 ntohs(conn->dst_addr.ip.sin_port)));
1148 /* See if we know this connection
1149 if we dont know this connection then we dont need to do anything
1151 tcpp = ctdb_tcp_find(vnn->tcp_array, conn);
1153 DEBUG(DEBUG_INFO,("Trying to remove tickle that doesnt exist %s:%u\n",
1154 ctdb_addr_to_str(&conn->dst_addr),
1155 ntohs(conn->dst_addr.ip.sin_port)));
1160 /* We need to remove this entry from the array.
1161 Instead of allocating a new array and copying data to it
1162 we cheat and just copy the last entry in the existing array
1163 to the entry that is to be removed and just shring the
1166 *tcpp = vnn->tcp_array->connections[vnn->tcp_array->num - 1];
1167 vnn->tcp_array->num--;
1169 /* If we deleted the last entry we also need to remove the entire array
1171 if (vnn->tcp_array->num == 0) {
1172 talloc_free(vnn->tcp_array);
1173 vnn->tcp_array = NULL;
1176 vnn->tcp_update_needed = true;
1178 DEBUG(DEBUG_INFO,("Removed tickle info for %s:%u\n",
1179 ctdb_addr_to_str(&conn->src_addr),
1180 ntohs(conn->src_addr.ip.sin_port)));
1185 called when a daemon restarts - send all tickes for all public addresses
1186 we are serving immediately to the new node.
1188 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn)
1190 /*XXX here we should send all tickes we are serving to the new node */
1196 called when a client structure goes away - hook to remove
1197 elements from the tcp_list in all daemons
1199 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client)
1201 while (client->tcp_list) {
1202 struct ctdb_tcp_list *tcp = client->tcp_list;
1203 DLIST_REMOVE(client->tcp_list, tcp);
1204 ctdb_remove_tcp_connection(client->ctdb, &tcp->connection);
1210 release all IPs on shutdown
1212 void ctdb_release_all_ips(struct ctdb_context *ctdb)
1214 struct ctdb_vnn *vnn;
1216 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1217 if (!ctdb_sys_have_ip(&vnn->public_address)) {
1220 ctdb_event_script(ctdb, "releaseip %s %s %u",
1222 talloc_strdup(ctdb, ctdb_addr_to_str(&vnn->public_address)),
1223 vnn->public_netmask_bits);
1224 release_kill_clients(ctdb, &vnn->public_address);
1230 get list of public IPs
1232 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
1233 struct ctdb_req_control *c, TDB_DATA *outdata)
1236 struct ctdb_all_public_ips *ips;
1237 struct ctdb_vnn *vnn;
1239 /* count how many public ip structures we have */
1241 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1245 len = offsetof(struct ctdb_all_public_ips, ips) +
1246 num*sizeof(struct ctdb_public_ip);
1247 ips = talloc_zero_size(outdata, len);
1248 CTDB_NO_MEMORY(ctdb, ips);
1250 outdata->dsize = len;
1251 outdata->dptr = (uint8_t *)ips;
1255 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1256 ips->ips[i].pnn = vnn->pnn;
1257 ips->ips[i].addr = vnn->public_address;
1267 structure containing the listening socket and the list of tcp connections
1268 that the ctdb daemon is to kill
1270 struct ctdb_kill_tcp {
1271 struct ctdb_vnn *vnn;
1272 struct ctdb_context *ctdb;
1274 struct fd_event *fde;
1275 trbt_tree_t *connections;
1280 a tcp connection that is to be killed
1282 struct ctdb_killtcp_con {
1283 ctdb_sock_addr src_addr;
1284 ctdb_sock_addr dst_addr;
1286 struct ctdb_kill_tcp *killtcp;
1289 /* this function is used to create a key to represent this socketpair
1290 in the killtcp tree.
1291 this key is used to insert and lookup matching socketpairs that are
1292 to be tickled and RST
1294 #define KILLTCP_KEYLEN 10
1295 static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
1297 static uint32_t key[KILLTCP_KEYLEN];
1299 bzero(key, sizeof(key));
1301 if (src->sa.sa_family != dst->sa.sa_family) {
1302 DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
1306 switch (src->sa.sa_family) {
1308 key[0] = dst->ip.sin_addr.s_addr;
1309 key[1] = src->ip.sin_addr.s_addr;
1310 key[2] = dst->ip.sin_port;
1311 key[3] = src->ip.sin_port;
1314 key[0] = dst->ip6.sin6_addr.s6_addr32[3];
1315 key[1] = src->ip6.sin6_addr.s6_addr32[3];
1316 key[2] = dst->ip6.sin6_addr.s6_addr32[2];
1317 key[3] = src->ip6.sin6_addr.s6_addr32[2];
1318 key[4] = dst->ip6.sin6_addr.s6_addr32[1];
1319 key[5] = src->ip6.sin6_addr.s6_addr32[1];
1320 key[6] = dst->ip6.sin6_addr.s6_addr32[0];
1321 key[7] = src->ip6.sin6_addr.s6_addr32[0];
1322 key[8] = dst->ip6.sin6_port;
1323 key[9] = src->ip6.sin6_port;
1326 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
1334 called when we get a read event on the raw socket
1336 static void capture_tcp_handler(struct event_context *ev, struct fd_event *fde,
1337 uint16_t flags, void *private_data)
1339 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
1340 struct ctdb_killtcp_con *con;
1341 ctdb_sock_addr src, dst;
1342 uint32_t ack_seq, seq;
1344 if (!(flags & EVENT_FD_READ)) {
1348 if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
1349 killtcp->private_data,
1351 &ack_seq, &seq) != 0) {
1352 /* probably a non-tcp ACK packet */
1356 /* check if we have this guy in our list of connections
1359 con = trbt_lookuparray32(killtcp->connections,
1360 KILLTCP_KEYLEN, killtcp_key(&src, &dst));
1362 /* no this was some other packet we can just ignore */
1366 /* This one has been tickled !
1367 now reset him and remove him from the list.
1369 DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
1370 ntohs(con->dst_addr.ip.sin_port),
1371 ctdb_addr_to_str(&con->src_addr),
1372 ntohs(con->src_addr.ip.sin_port)));
1374 ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
1379 /* when traversing the list of all tcp connections to send tickle acks to
1380 (so that we can capture the ack coming back and kill the connection
1382 this callback is called for each connection we are currently trying to kill
1384 static void tickle_connection_traverse(void *param, void *data)
1386 struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
1388 /* have tried too many times, just give up */
1389 if (con->count >= 5) {
1394 /* othervise, try tickling it again */
1397 (ctdb_sock_addr *)&con->dst_addr,
1398 (ctdb_sock_addr *)&con->src_addr,
1404 called every second until all sentenced connections have been reset
1406 static void ctdb_tickle_sentenced_connections(struct event_context *ev, struct timed_event *te,
1407 struct timeval t, void *private_data)
1409 struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
1412 /* loop over all connections sending tickle ACKs */
1413 trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, NULL);
1416 /* If there are no more connections to kill we can remove the
1417 entire killtcp structure
1419 if ( (killtcp->connections == NULL) ||
1420 (killtcp->connections->root == NULL) ) {
1421 talloc_free(killtcp);
1425 /* try tickling them again in a seconds time
1427 event_add_timed(killtcp->ctdb->ev, killtcp, timeval_current_ofs(1, 0),
1428 ctdb_tickle_sentenced_connections, killtcp);
1432 destroy the killtcp structure
1434 static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
1436 killtcp->vnn->killtcp = NULL;
1441 /* nothing fancy here, just unconditionally replace any existing
1442 connection structure with the new one.
1444 dont even free the old one if it did exist, that one is talloc_stolen
1445 by the same node in the tree anyway and will be deleted when the new data
1448 static void *add_killtcp_callback(void *parm, void *data)
1454 add a tcp socket to the list of connections we want to RST
1456 static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb,
1460 ctdb_sock_addr src, dst;
1461 struct ctdb_kill_tcp *killtcp;
1462 struct ctdb_killtcp_con *con;
1463 struct ctdb_vnn *vnn;
1465 ctdb_canonicalize_ip(s, &src);
1466 ctdb_canonicalize_ip(d, &dst);
1468 vnn = find_public_ip_vnn(ctdb, &dst);
1470 vnn = find_public_ip_vnn(ctdb, &src);
1473 /* if it is not a public ip it could be our 'single ip' */
1474 if (ctdb->single_ip_vnn) {
1475 if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
1476 vnn = ctdb->single_ip_vnn;
1481 DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n"));
1485 killtcp = vnn->killtcp;
1487 /* If this is the first connection to kill we must allocate
1490 if (killtcp == NULL) {
1491 killtcp = talloc_zero(ctdb, struct ctdb_kill_tcp);
1492 CTDB_NO_MEMORY(ctdb, killtcp);
1495 killtcp->ctdb = ctdb;
1496 killtcp->capture_fd = -1;
1497 killtcp->connections = trbt_create(killtcp, 0);
1499 vnn->killtcp = killtcp;
1500 talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
1505 /* create a structure that describes this connection we want to
1506 RST and store it in killtcp->connections
1508 con = talloc(killtcp, struct ctdb_killtcp_con);
1509 CTDB_NO_MEMORY(ctdb, con);
1510 con->src_addr = src;
1511 con->dst_addr = dst;
1513 con->killtcp = killtcp;
1516 trbt_insertarray32_callback(killtcp->connections,
1517 KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
1518 add_killtcp_callback, con);
1521 If we dont have a socket to listen on yet we must create it
1523 if (killtcp->capture_fd == -1) {
1524 killtcp->capture_fd = ctdb_sys_open_capture_socket(vnn->iface, &killtcp->private_data);
1525 if (killtcp->capture_fd == -1) {
1526 DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing socket for killtcp\n"));
1532 if (killtcp->fde == NULL) {
1533 killtcp->fde = event_add_fd(ctdb->ev, killtcp, killtcp->capture_fd,
1534 EVENT_FD_READ | EVENT_FD_AUTOCLOSE,
1535 capture_tcp_handler, killtcp);
1537 /* We also need to set up some events to tickle all these connections
1538 until they are all reset
1540 event_add_timed(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
1541 ctdb_tickle_sentenced_connections, killtcp);
1544 /* tickle him once now */
1553 talloc_free(vnn->killtcp);
1554 vnn->killtcp = NULL;
1559 kill a TCP connection.
1561 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
1563 struct ctdb_control_killtcp *killtcp = (struct ctdb_control_killtcp *)indata.dptr;
1565 return ctdb_killtcp_add_connection(ctdb, &killtcp->src_addr, &killtcp->dst_addr);
1569 called by a daemon to inform us of the entire list of TCP tickles for
1570 a particular public address.
1571 this control should only be sent by the node that is currently serving
1572 that public address.
1574 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata)
1576 struct ctdb_control_tcp_tickle_list *list = (struct ctdb_control_tcp_tickle_list *)indata.dptr;
1577 struct ctdb_tcp_array *tcparray;
1578 struct ctdb_vnn *vnn;
1580 /* We must at least have tickles.num or else we cant verify the size
1581 of the received data blob
1583 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
1584 tickles.connections)) {
1585 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list. Not enough data for the tickle.num field\n"));
1589 /* verify that the size of data matches what we expect */
1590 if (indata.dsize < offsetof(struct ctdb_control_tcp_tickle_list,
1591 tickles.connections)
1592 + sizeof(struct ctdb_tcp_connection)
1593 * list->tickles.num) {
1594 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tcp_tickle_list\n"));
1598 vnn = find_public_ip_vnn(ctdb, &list->addr);
1600 DEBUG(DEBUG_INFO,(__location__ " Could not set tcp tickle list, '%s' is not a public address\n",
1601 ctdb_addr_to_str(&list->addr)));
1606 /* remove any old ticklelist we might have */
1607 talloc_free(vnn->tcp_array);
1608 vnn->tcp_array = NULL;
1610 tcparray = talloc(ctdb->nodes, struct ctdb_tcp_array);
1611 CTDB_NO_MEMORY(ctdb, tcparray);
1613 tcparray->num = list->tickles.num;
1615 tcparray->connections = talloc_array(tcparray, struct ctdb_tcp_connection, tcparray->num);
1616 CTDB_NO_MEMORY(ctdb, tcparray->connections);
1618 memcpy(tcparray->connections, &list->tickles.connections[0],
1619 sizeof(struct ctdb_tcp_connection)*tcparray->num);
1621 /* We now have a new fresh tickle list array for this vnn */
1622 vnn->tcp_array = talloc_steal(vnn, tcparray);
1628 called to return the full list of tickles for the puclic address associated
1629 with the provided vnn
1631 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
1633 ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
1634 struct ctdb_control_tcp_tickle_list *list;
1635 struct ctdb_tcp_array *tcparray;
1637 struct ctdb_vnn *vnn;
1639 vnn = find_public_ip_vnn(ctdb, addr);
1641 DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
1642 ctdb_addr_to_str(addr)));
1647 tcparray = vnn->tcp_array;
1649 num = tcparray->num;
1654 outdata->dsize = offsetof(struct ctdb_control_tcp_tickle_list,
1655 tickles.connections)
1656 + sizeof(struct ctdb_tcp_connection) * num;
1658 outdata->dptr = talloc_size(outdata, outdata->dsize);
1659 CTDB_NO_MEMORY(ctdb, outdata->dptr);
1660 list = (struct ctdb_control_tcp_tickle_list *)outdata->dptr;
1663 list->tickles.num = num;
1665 memcpy(&list->tickles.connections[0], tcparray->connections,
1666 sizeof(struct ctdb_tcp_connection) * num);
1674 set the list of all tcp tickles for a public address
1676 static int ctdb_ctrl_set_tcp_tickles(struct ctdb_context *ctdb,
1677 struct timeval timeout, uint32_t destnode,
1678 ctdb_sock_addr *addr,
1679 struct ctdb_tcp_array *tcparray)
1683 struct ctdb_control_tcp_tickle_list *list;
1686 num = tcparray->num;
1691 data.dsize = offsetof(struct ctdb_control_tcp_tickle_list,
1692 tickles.connections) +
1693 sizeof(struct ctdb_tcp_connection) * num;
1694 data.dptr = talloc_size(ctdb, data.dsize);
1695 CTDB_NO_MEMORY(ctdb, data.dptr);
1697 list = (struct ctdb_control_tcp_tickle_list *)data.dptr;
1699 list->tickles.num = num;
1701 memcpy(&list->tickles.connections[0], tcparray->connections, sizeof(struct ctdb_tcp_connection) * num);
1704 ret = ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0,
1705 CTDB_CONTROL_SET_TCP_TICKLE_LIST,
1706 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL);
1708 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set tcp tickles failed\n"));
1712 talloc_free(data.dptr);
1719 perform tickle updates if required
1721 static void ctdb_update_tcp_tickles(struct event_context *ev,
1722 struct timed_event *te,
1723 struct timeval t, void *private_data)
1725 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
1727 struct ctdb_vnn *vnn;
1729 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1730 /* we only send out updates for public addresses that
1733 if (ctdb->pnn != vnn->pnn) {
1736 /* We only send out the updates if we need to */
1737 if (!vnn->tcp_update_needed) {
1740 ret = ctdb_ctrl_set_tcp_tickles(ctdb,
1742 CTDB_BROADCAST_CONNECTED,
1743 &vnn->public_address,
1746 DEBUG(DEBUG_ERR,("Failed to send the tickle update for public address %s\n",
1747 ctdb_addr_to_str(&vnn->public_address)));
1751 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
1752 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
1753 ctdb_update_tcp_tickles, ctdb);
1758 start periodic update of tcp tickles
1760 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb)
1762 ctdb->tickle_update_context = talloc_new(ctdb);
1764 event_add_timed(ctdb->ev, ctdb->tickle_update_context,
1765 timeval_current_ofs(ctdb->tunable.tickle_update_interval, 0),
1766 ctdb_update_tcp_tickles, ctdb);
1772 struct control_gratious_arp {
1773 struct ctdb_context *ctdb;
1774 ctdb_sock_addr addr;
1780 send a control_gratuitous arp
1782 static void send_gratious_arp(struct event_context *ev, struct timed_event *te,
1783 struct timeval t, void *private_data)
1786 struct control_gratious_arp *arp = talloc_get_type(private_data,
1787 struct control_gratious_arp);
1789 ret = ctdb_sys_send_arp(&arp->addr, arp->iface);
1791 DEBUG(DEBUG_ERR,(__location__ " sending of gratious arp failed (%s)\n", strerror(errno)));
1796 if (arp->count == CTDB_ARP_REPEAT) {
1801 event_add_timed(arp->ctdb->ev, arp,
1802 timeval_current_ofs(CTDB_ARP_INTERVAL, 0),
1803 send_gratious_arp, arp);
1810 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata)
1812 struct ctdb_control_gratious_arp *gratious_arp = (struct ctdb_control_gratious_arp *)indata.dptr;
1813 struct control_gratious_arp *arp;
1815 /* verify the size of indata */
1816 if (indata.dsize < offsetof(struct ctdb_control_gratious_arp, iface)) {
1817 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_gratious_arp structure. Got %u require %u bytes\n",
1818 (unsigned)indata.dsize,
1819 (unsigned)offsetof(struct ctdb_control_gratious_arp, iface)));
1823 ( offsetof(struct ctdb_control_gratious_arp, iface)
1824 + gratious_arp->len ) ){
1826 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
1827 "but should be %u bytes\n",
1828 (unsigned)indata.dsize,
1829 (unsigned)(offsetof(struct ctdb_control_gratious_arp, iface)+gratious_arp->len)));
1834 arp = talloc(ctdb, struct control_gratious_arp);
1835 CTDB_NO_MEMORY(ctdb, arp);
1838 arp->addr = gratious_arp->addr;
1839 arp->iface = talloc_strdup(arp, gratious_arp->iface);
1840 CTDB_NO_MEMORY(ctdb, arp->iface);
1843 event_add_timed(arp->ctdb->ev, arp,
1844 timeval_zero(), send_gratious_arp, arp);
1849 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
1851 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
1854 /* verify the size of indata */
1855 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
1856 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
1860 ( offsetof(struct ctdb_control_ip_iface, iface)
1863 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
1864 "but should be %u bytes\n",
1865 (unsigned)indata.dsize,
1866 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
1870 return ctdb_add_public_address(ctdb, &pub->addr, pub->mask, &pub->iface[0]);
1874 called when releaseip event finishes for del_public_address
1876 static void delete_ip_callback(struct ctdb_context *ctdb, int status,
1879 talloc_free(private_data);
1882 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata)
1884 struct ctdb_control_ip_iface *pub = (struct ctdb_control_ip_iface *)indata.dptr;
1885 struct ctdb_vnn *vnn;
1888 /* verify the size of indata */
1889 if (indata.dsize < offsetof(struct ctdb_control_ip_iface, iface)) {
1890 DEBUG(DEBUG_ERR,(__location__ " Too small indata to hold a ctdb_control_ip_iface structure\n"));
1894 ( offsetof(struct ctdb_control_ip_iface, iface)
1897 DEBUG(DEBUG_ERR,(__location__ " Wrong size of indata. Was %u bytes "
1898 "but should be %u bytes\n",
1899 (unsigned)indata.dsize,
1900 (unsigned)(offsetof(struct ctdb_control_ip_iface, iface)+pub->len)));
1904 /* walk over all public addresses until we find a match */
1905 for (vnn=ctdb->vnn;vnn;vnn=vnn->next) {
1906 if (ctdb_same_ip(&vnn->public_address, &pub->addr)) {
1907 TALLOC_CTX *mem_ctx = talloc_new(ctdb);
1909 DLIST_REMOVE(ctdb->vnn, vnn);
1911 ret = ctdb_event_script_callback(ctdb,
1912 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
1913 mem_ctx, delete_ip_callback, mem_ctx,
1914 "releaseip %s %s %u",
1916 talloc_strdup(mem_ctx, ctdb_addr_to_str(&vnn->public_address)),
1917 vnn->public_netmask_bits);