ctdb-ipalloc: Do not use node count or PNNs from CTDB context
[obnox/samba/samba-obnox.git] / ctdb / server / ctdb_takeover.c
index 4adbb1184370ea493adf48181ec8fa5421d3290c..bb548910dd04224bdd18ffb759a31e419a31ae83 100644 (file)
@@ -114,10 +114,9 @@ static bool vnn_has_interface_with_name(struct ctdb_vnn *vnn,
  * foolproof.  One alternative is reference counting, where the logic
  * is distributed and can, therefore, be broken in multiple places.
  * Another alternative is to build a red-black tree of interfaces that
- * can have addresses (by walking ctdb->vnn and ctdb->single_ip_vnn
- * once) and then walking ctdb->ifaces once and deleting those not in
- * the tree.  Let's go to one of those if the naive implementation
- * causes problems...  :-)
+ * can have addresses (by walking ctdb->vnn once) and then walking
+ * ctdb->ifaces once and deleting those not in the tree.  Let's go to
+ * one of those if the naive implementation causes problems...  :-)
  */
 static void ctdb_remove_orphaned_ifaces(struct ctdb_context *ctdb,
                                        struct ctdb_vnn *vnn)
@@ -135,13 +134,6 @@ static void ctdb_remove_orphaned_ifaces(struct ctdb_context *ctdb,
                        continue;
                }
 
-               /* Is the "single IP" on this interface? */
-               if ((ctdb->single_ip_vnn != NULL) &&
-                   (ctdb->single_ip_vnn->ifaces[0] != NULL) &&
-                   (strcmp(i->name, ctdb->single_ip_vnn->ifaces[0]) == 0)) {
-                       /* Found, next interface please... */
-                       continue;
-               }
                /* Search for a vnn with this interface. */
                found = false;
                for (tv=ctdb->vnn; tv; tv=tv->next) {
@@ -431,8 +423,6 @@ static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
        TDB_DATA data;
 
        if (status != 0) {
-               struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
-       
                if (status == -ETIME) {
                        ctdb_ban_self(ctdb);
                }
@@ -441,7 +431,6 @@ static void ctdb_do_takeip_callback(struct ctdb_context *ctdb, int status,
                                 ctdb_vnn_iface_string(state->vnn)));
                ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
 
-               node->flags |= NODE_FLAGS_UNHEALTHY;
                talloc_free(state);
                return;
        }
@@ -1140,58 +1129,6 @@ int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses)
        return 0;
 }
 
-int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
-                             const char *iface,
-                             const char *ip)
-{
-       struct ctdb_vnn *svnn;
-       struct ctdb_interface *cur = NULL;
-       bool ok;
-       int ret;
-
-       svnn = talloc_zero(ctdb, struct ctdb_vnn);
-       CTDB_NO_MEMORY(ctdb, svnn);
-
-       svnn->ifaces = talloc_array(svnn, const char *, 2);
-       CTDB_NO_MEMORY(ctdb, svnn->ifaces);
-       svnn->ifaces[0] = talloc_strdup(svnn->ifaces, iface);
-       CTDB_NO_MEMORY(ctdb, svnn->ifaces[0]);
-       svnn->ifaces[1] = NULL;
-
-       ok = parse_ip(ip, iface, 0, &svnn->public_address);
-       if (!ok) {
-               talloc_free(svnn);
-               return -1;
-       }
-
-       ret = ctdb_add_local_iface(ctdb, svnn->ifaces[0]);
-       if (ret != 0) {
-               DEBUG(DEBUG_CRIT, (__location__ " failed to add iface[%s] "
-                                  "for single_ip[%s]\n",
-                                  svnn->ifaces[0],
-                                  ctdb_addr_to_str(&svnn->public_address)));
-               talloc_free(svnn);
-               return -1;
-       }
-
-       /* assume the single public ip interface is initially "good" */
-       cur = ctdb_find_iface(ctdb, iface);
-       if (cur == NULL) {
-               DEBUG(DEBUG_CRIT,("Can not find public interface %s used by --single-public-ip", iface));
-               return -1;
-       }
-       cur->link_up = true;
-
-       ret = ctdb_vnn_assign_iface(ctdb, svnn);
-       if (ret != 0) {
-               talloc_free(svnn);
-               return -1;
-       }
-
-       ctdb->single_ip_vnn = svnn;
-       return 0;
-}
-
 static void *add_ip_callback(void *parm, void *data)
 {
        struct public_ip_list *this_ip = parm;
@@ -1303,16 +1240,12 @@ create_merged_ip_list(struct ctdb_context *ctdb, struct ipalloc_state *ipalloc_s
        TALLOC_FREE(ctdb->ip_tree);
        ctdb->ip_tree = trbt_create(ctdb, 0);
 
-       for (i=0; i < ctdb->num_nodes; i++) {
-
-               if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
-                       continue;
-               }
+       if (ipalloc_state->known_public_ips == NULL) {
+               DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
+               return NULL;
+       }
 
-               /* there were no public ips for this node */
-               if (ipalloc_state->known_public_ips == NULL) {
-                       continue;
-               }
+       for (i=0; i < ipalloc_state->num; i++) {
 
                public_ips = &ipalloc_state->known_public_ips[i];
 
@@ -1323,7 +1256,7 @@ create_merged_ip_list(struct ctdb_context *ctdb, struct ipalloc_state *ipalloc_s
                        CTDB_NO_MEMORY_NULL(ctdb, tmp_ip);
                        /* Do not use information about IP addresses hosted
                         * on other nodes, it may not be accurate */
-                       if (public_ips->ip[j].pnn == ctdb->nodes[i]->pnn) {
+                       if (public_ips->ip[j].pnn == i) {
                                tmp_ip->pnn = public_ips->ip[j].pnn;
                        } else {
                                tmp_ip->pnn = -1;
@@ -1612,74 +1545,35 @@ fail:
        return NULL;
 }
 
-struct iprealloc_callback_data {
-       bool *retry_nodes;
-       int retry_count;
-       client_async_callback fail_callback;
-       void *fail_callback_data;
-       struct ctdb_node_map_old *nodemap;
+struct takeover_callback_data {
+       uint32_t num_nodes;
+       unsigned int *fail_count;
 };
 
-static void iprealloc_fail_callback(struct ctdb_context *ctdb, uint32_t pnn,
-                                       int32_t res, TDB_DATA outdata,
-                                       void *callback)
+static struct takeover_callback_data *
+takeover_callback_data_init(TALLOC_CTX *mem_ctx,
+                           uint32_t num_nodes)
 {
-       int numnodes;
-       struct iprealloc_callback_data *cd =
-               (struct iprealloc_callback_data *)callback;
+       static struct takeover_callback_data *takeover_data;
 
-       numnodes = talloc_array_length(cd->retry_nodes);
-       if (pnn > numnodes) {
-               DEBUG(DEBUG_ERR,
-                     ("ipreallocated failure from node %d, "
-                      "but only %d nodes in nodemap\n",
-                      pnn, numnodes));
-               return;
+       takeover_data = talloc_zero(mem_ctx, struct takeover_callback_data);
+       if (takeover_data == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
+               return NULL;
        }
 
-       /* Can't run the "ipreallocated" event on a INACTIVE node */
-       if (cd->nodemap->nodes[pnn].flags & NODE_FLAGS_INACTIVE) {
-               DEBUG(DEBUG_WARNING,
-                     ("ipreallocated failed on inactive node %d, ignoring\n",
-                      pnn));
-               return;
+       takeover_data->fail_count = talloc_zero_array(takeover_data,
+                                                     unsigned int, num_nodes);
+       if (takeover_data->fail_count == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
+               talloc_free(takeover_data);
+               return NULL;
        }
 
-       switch (res) {
-       case -ETIME:
-               /* If the control timed out then that's a real error,
-                * so call the real fail callback
-                */
-               if (cd->fail_callback) {
-                       cd->fail_callback(ctdb, pnn, res, outdata,
-                                         cd->fail_callback_data);
-               } else {
-                       DEBUG(DEBUG_WARNING,
-                             ("iprealloc timed out but no callback registered\n"));
-               }
-               break;
-       default:
-               /* If not a timeout then either the ipreallocated
-                * eventscript (or some setup) failed.  This might
-                * have failed because the IPREALLOCATED control isn't
-                * implemented - right now there is no way of knowing
-                * because the error codes are all folded down to -1.
-                * Consider retrying using EVENTSCRIPT control...
-                */
-               DEBUG(DEBUG_WARNING,
-                     ("ipreallocated failure from node %d, flagging retry\n",
-                      pnn));
-               cd->retry_nodes[pnn] = true;
-               cd->retry_count++;
-       }
-}
+       takeover_data->num_nodes = num_nodes;
 
-struct takeover_callback_data {
-       bool *node_failed;
-       client_async_callback fail_callback;
-       void *fail_callback_data;
-       struct ctdb_node_map_old *nodemap;
-};
+       return takeover_data;
+}
 
 static void takeover_run_fail_callback(struct ctdb_context *ctdb,
                                       uint32_t node_pnn, int32_t res,
@@ -1688,23 +1582,53 @@ static void takeover_run_fail_callback(struct ctdb_context *ctdb,
        struct takeover_callback_data *cd =
                talloc_get_type_abort(callback_data,
                                      struct takeover_callback_data);
-       int i;
 
-       for (i = 0; i < cd->nodemap->num; i++) {
-               if (node_pnn == cd->nodemap->nodes[i].pnn) {
-                       break;
-               }
-       }
-
-       if (i == cd->nodemap->num) {
+       if (node_pnn >= cd->num_nodes) {
                DEBUG(DEBUG_ERR, (__location__ " invalid PNN %u\n", node_pnn));
                return;
        }
 
-       if (!cd->node_failed[i]) {
-               cd->node_failed[i] = true;
-               cd->fail_callback(ctdb, node_pnn, res, outdata,
-                                 cd->fail_callback_data);
+       if (cd->fail_count[node_pnn] == 0) {
+               DEBUG(DEBUG_ERR,
+                     ("Node %u failed the takeover run\n", node_pnn));
+       }
+
+       cd->fail_count[node_pnn]++;
+}
+
+static void takeover_run_process_failures(struct ctdb_context *ctdb,
+                                         struct takeover_callback_data *tcd)
+{
+       unsigned int max_fails = 0;
+       uint32_t max_pnn = -1;
+       uint32_t i;
+
+       for (i = 0; i < tcd->num_nodes; i++) {
+               if (tcd->fail_count[i] > max_fails) {
+                       max_pnn = i;
+                       max_fails = tcd->fail_count[i];
+               }
+       }
+
+       if (max_fails > 0) {
+               int ret;
+               TDB_DATA data;
+
+               DEBUG(DEBUG_ERR,
+                     ("Sending banning credits to %u with fail count %u\n",
+                      max_pnn, max_fails));
+
+               data.dptr = (uint8_t *)&max_pnn;
+               data.dsize = sizeof(uint32_t);
+               ret = ctdb_client_send_message(ctdb,
+                                              CTDB_BROADCAST_CONNECTED,
+                                              CTDB_SRVID_BANNING,
+                                              data);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,
+                             ("Failed to set banning credits for node %u\n",
+                              max_pnn));
+               }
        }
 }
 
@@ -1737,10 +1661,9 @@ static void takeover_run_fail_callback(struct ctdb_context *ctdb,
  * - Send IPREALLOCATED to all nodes (with backward compatibility hack)
  */
 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodemap,
-                     uint32_t *force_rebalance_nodes,
-                     client_async_callback fail_callback, void *callback_data)
+                     uint32_t *force_rebalance_nodes)
 {
-       int i, j, ret;
+       int i, ret;
        struct ctdb_public_ip ip;
        uint32_t *nodes;
        struct public_ip_list *all_ips, *tmp_ip;
@@ -1751,10 +1674,19 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodem
        TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
        struct ipalloc_state *ipalloc_state;
        struct takeover_callback_data *takeover_data;
-       struct iprealloc_callback_data iprealloc_data;
-       bool *retry_data;
        bool can_host_ips;
 
+       /* Initialise fail callback data to be used with
+        * takeover_run_fail_callback().  A failure in any of the
+        * following steps will cause an early return, so this can be
+        * reused for each of those steps without re-initialising. */
+       takeover_data = takeover_callback_data_init(tmp_ctx,
+                                                   nodemap->num);
+       if (takeover_data == NULL) {
+               talloc_free(tmp_ctx);
+               return -1;
+       }
+
        /*
         * ip failover is completely disabled, just send out the 
         * ipreallocated event.
@@ -1791,7 +1723,7 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodem
        }
        if (!can_host_ips) {
                DEBUG(DEBUG_WARNING,("No nodes available to host public IPs yet\n"));
-               return 0;
+               goto ipreallocated;
        }
 
        /* since nodes only know about those public addresses that
@@ -1814,16 +1746,6 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodem
         * host.  This will be a NOOP on nodes that don't currently
         * hold the given IP.
         */
-       takeover_data = talloc_zero(tmp_ctx, struct takeover_callback_data);
-       CTDB_NO_MEMORY_FATAL(ctdb, takeover_data);
-
-       takeover_data->node_failed = talloc_zero_array(tmp_ctx,
-                                                      bool, nodemap->num);
-       CTDB_NO_MEMORY_FATAL(ctdb, takeover_data->node_failed);
-       takeover_data->fail_callback = fail_callback;
-       takeover_data->fail_callback_data = callback_data;
-       takeover_data->nodemap = nodemap;
-
        async_data = talloc_zero(tmp_ctx, struct client_async_data);
        CTDB_NO_MEMORY_FATAL(ctdb, async_data);
 
@@ -1871,9 +1793,9 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodem
                }
        }
        if (ctdb_client_async_wait(ctdb, async_data) != 0) {
-               DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_RELEASE_IP failed\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               DEBUG(DEBUG_ERR,
+                     ("Async control CTDB_CONTROL_RELEASE_IP failed\n"));
+               goto fail;
        }
        talloc_free(async_data);
 
@@ -1885,8 +1807,8 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodem
        async_data = talloc_zero(tmp_ctx, struct client_async_data);
        CTDB_NO_MEMORY_FATAL(ctdb, async_data);
 
-       async_data->fail_callback = fail_callback;
-       async_data->callback_data = callback_data;
+       async_data->fail_callback = takeover_run_fail_callback;
+       async_data->callback_data = takeover_data;
 
        for (tmp_ip=all_ips;tmp_ip;tmp_ip=tmp_ip->next) {
                if (tmp_ip->pnn == -1) {
@@ -1912,9 +1834,9 @@ int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodem
                ctdb_client_async_add(async_data, state);
        }
        if (ctdb_client_async_wait(ctdb, async_data) != 0) {
-               DEBUG(DEBUG_ERR,(__location__ " Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               DEBUG(DEBUG_ERR,
+                     ("Async control CTDB_CONTROL_TAKEOVER_IP failed\n"));
+               goto fail;
        }
 
 ipreallocated:
@@ -1925,58 +1847,25 @@ ipreallocated:
         * IPs have moved.  Once upon a time this event only used to
         * update natgw.
         */
-       retry_data = talloc_zero_array(tmp_ctx, bool, nodemap->num);
-       CTDB_NO_MEMORY_FATAL(ctdb, retry_data);
-       iprealloc_data.retry_nodes = retry_data;
-       iprealloc_data.retry_count = 0;
-       iprealloc_data.fail_callback = fail_callback;
-       iprealloc_data.fail_callback_data = callback_data;
-       iprealloc_data.nodemap = nodemap;
-
        nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
        ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_IPREALLOCATED,
                                        nodes, 0, TAKEOVER_TIMEOUT(),
                                        false, tdb_null,
-                                       NULL, iprealloc_fail_callback,
-                                       &iprealloc_data);
+                                       NULL, takeover_run_fail_callback,
+                                       takeover_data);
        if (ret != 0) {
-               /* If the control failed then we should retry to any
-                * nodes flagged by iprealloc_fail_callback using the
-                * EVENTSCRIPT control.  This is a best-effort at
-                * backward compatiblity when running a mixed cluster
-                * where some nodes have not yet been upgraded to
-                * support the IPREALLOCATED control.
-                */
-               DEBUG(DEBUG_WARNING,
-                     ("Retry ipreallocated to some nodes using eventscript control\n"));
-
-               nodes = talloc_array(tmp_ctx, uint32_t,
-                                    iprealloc_data.retry_count);
-               CTDB_NO_MEMORY_FATAL(ctdb, nodes);
-
-               j = 0;
-               for (i=0; i<nodemap->num; i++) {
-                       if (iprealloc_data.retry_nodes[i]) {
-                               nodes[j] = i;
-                               j++;
-                       }
-               }
-
-               data.dptr  = discard_const("ipreallocated");
-               data.dsize = strlen((char *)data.dptr) + 1; 
-               ret = ctdb_client_async_control(ctdb,
-                                               CTDB_CONTROL_RUN_EVENTSCRIPTS,
-                                               nodes, 0, TAKEOVER_TIMEOUT(),
-                                               false, data,
-                                               NULL, fail_callback,
-                                               callback_data);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, (__location__ " failed to send control to run eventscripts with \"ipreallocated\"\n"));
-               }
+               DEBUG(DEBUG_ERR,
+                     ("Async CTDB_CONTROL_IPREALLOCATED control failed\n"));
+               goto fail;
        }
 
        talloc_free(tmp_ctx);
        return ret;
+
+fail:
+       takeover_run_process_failures(ctdb, takeover_data);
+       talloc_free(tmp_ctx);
+       return -1;
 }
 
 
@@ -2474,14 +2363,6 @@ int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
        addr = (ctdb_sock_addr *)indata.dptr;
 
        vnn = find_public_ip_vnn(ctdb, addr);
-       if (vnn == NULL) {
-               /* if it is not a public ip   it could be our 'single ip' */
-               if (ctdb->single_ip_vnn) {
-                       if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, addr)) {
-                               vnn = ctdb->single_ip_vnn;
-                       }
-               }
-       }
        if (vnn == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Could not get public ip info, "
                                 "'%s'not a public address\n",
@@ -2516,7 +2397,9 @@ int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
                if (vnn->iface == cur) {
                        info->active_idx = i;
                }
-               strncpy(info->ifaces[i].name, cur->name, sizeof(info->ifaces[i].name)-1);
+               strncpy(info->ifaces[i].name, cur->name,
+                       sizeof(info->ifaces[i].name));
+               info->ifaces[i].name[sizeof(info->ifaces[i].name)-1] = '\0';
                info->ifaces[i].link_state = cur->link_up;
                info->ifaces[i].references = cur->references;
        }
@@ -2551,7 +2434,9 @@ int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
 
        i = 0;
        for (cur=ctdb->ifaces;cur;cur=cur->next) {
-               strcpy(ifaces->ifaces[i].name, cur->name);
+               strncpy(ifaces->ifaces[i].name, cur->name,
+                       sizeof(ifaces->ifaces[i].name));
+               ifaces->ifaces[i].name[sizeof(ifaces->ifaces[i].name)-1] = '\0';
                ifaces->ifaces[i].link_state = cur->link_up;
                ifaces->ifaces[i].references = cur->references;
                i++;
@@ -2622,343 +2507,6 @@ int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
 }
 
 
-/* 
-   structure containing the listening socket and the list of tcp connections
-   that the ctdb daemon is to kill
-*/
-struct ctdb_kill_tcp {
-       struct ctdb_vnn *vnn;
-       struct ctdb_context *ctdb;
-       int capture_fd;
-       struct tevent_fd *fde;
-       trbt_tree_t *connections;
-       void *private_data;
-};
-
-/*
-  a tcp connection that is to be killed
- */
-struct ctdb_killtcp_con {
-       ctdb_sock_addr src_addr;
-       ctdb_sock_addr dst_addr;
-       int count;
-       struct ctdb_kill_tcp *killtcp;
-};
-
-/* this function is used to create a key to represent this socketpair
-   in the killtcp tree.
-   this key is used to insert and lookup matching socketpairs that are
-   to be tickled and RST
-*/
-#define KILLTCP_KEYLEN 10
-static uint32_t *killtcp_key(ctdb_sock_addr *src, ctdb_sock_addr *dst)
-{
-       static uint32_t key[KILLTCP_KEYLEN];
-
-       bzero(key, sizeof(key));
-
-       if (src->sa.sa_family != dst->sa.sa_family) {
-               DEBUG(DEBUG_ERR, (__location__ " ERROR, different families passed :%u vs %u\n", src->sa.sa_family, dst->sa.sa_family));
-               return key;
-       }
-       
-       switch (src->sa.sa_family) {
-       case AF_INET:
-               key[0]  = dst->ip.sin_addr.s_addr;
-               key[1]  = src->ip.sin_addr.s_addr;
-               key[2]  = dst->ip.sin_port;
-               key[3]  = src->ip.sin_port;
-               break;
-       case AF_INET6: {
-               uint32_t *dst6_addr32 =
-                       (uint32_t *)&(dst->ip6.sin6_addr.s6_addr);
-               uint32_t *src6_addr32 =
-                       (uint32_t *)&(src->ip6.sin6_addr.s6_addr);
-               key[0]  = dst6_addr32[3];
-               key[1]  = src6_addr32[3];
-               key[2]  = dst6_addr32[2];
-               key[3]  = src6_addr32[2];
-               key[4]  = dst6_addr32[1];
-               key[5]  = src6_addr32[1];
-               key[6]  = dst6_addr32[0];
-               key[7]  = src6_addr32[0];
-               key[8]  = dst->ip6.sin6_port;
-               key[9]  = src->ip6.sin6_port;
-               break;
-       }
-       default:
-               DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", src->sa.sa_family));
-               return key;
-       }
-
-       return key;
-}
-
-/*
-  called when we get a read event on the raw socket
- */
-static void capture_tcp_handler(struct tevent_context *ev,
-                               struct tevent_fd *fde,
-                               uint16_t flags, void *private_data)
-{
-       struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
-       struct ctdb_killtcp_con *con;
-       ctdb_sock_addr src, dst;
-       uint32_t ack_seq, seq;
-
-       if (!(flags & TEVENT_FD_READ)) {
-               return;
-       }
-
-       if (ctdb_sys_read_tcp_packet(killtcp->capture_fd,
-                               killtcp->private_data,
-                               &src, &dst,
-                               &ack_seq, &seq) != 0) {
-               /* probably a non-tcp ACK packet */
-               return;
-       }
-
-       /* check if we have this guy in our list of connections
-          to kill
-       */
-       con = trbt_lookuparray32(killtcp->connections, 
-                       KILLTCP_KEYLEN, killtcp_key(&src, &dst));
-       if (con == NULL) {
-               /* no this was some other packet we can just ignore */
-               return;
-       }
-
-       /* This one has been tickled !
-          now reset him and remove him from the list.
-        */
-       DEBUG(DEBUG_INFO, ("sending a tcp reset to kill connection :%d -> %s:%d\n",
-               ntohs(con->dst_addr.ip.sin_port),
-               ctdb_addr_to_str(&con->src_addr),
-               ntohs(con->src_addr.ip.sin_port)));
-
-       ctdb_sys_send_tcp(&con->dst_addr, &con->src_addr, ack_seq, seq, 1);
-       talloc_free(con);
-}
-
-
-/* when traversing the list of all tcp connections to send tickle acks to
-   (so that we can capture the ack coming back and kill the connection
-    by a RST)
-   this callback is called for each connection we are currently trying to kill
-*/
-static int tickle_connection_traverse(void *param, void *data)
-{
-       struct ctdb_killtcp_con *con = talloc_get_type(data, struct ctdb_killtcp_con);
-
-       /* have tried too many times, just give up */
-       if (con->count >= 5) {
-               /* can't delete in traverse: reparent to delete_cons */
-               talloc_steal(param, con);
-               return 0;
-       }
-
-       /* othervise, try tickling it again */
-       con->count++;
-       ctdb_sys_send_tcp(
-               (ctdb_sock_addr *)&con->dst_addr,
-               (ctdb_sock_addr *)&con->src_addr,
-               0, 0, 0);
-       return 0;
-}
-
-
-/* 
-   called every second until all sentenced connections have been reset
- */
-static void ctdb_tickle_sentenced_connections(struct tevent_context *ev,
-                                             struct tevent_timer *te,
-                                             struct timeval t, void *private_data)
-{
-       struct ctdb_kill_tcp *killtcp = talloc_get_type(private_data, struct ctdb_kill_tcp);
-       void *delete_cons = talloc_new(NULL);
-
-       /* loop over all connections sending tickle ACKs */
-       trbt_traversearray32(killtcp->connections, KILLTCP_KEYLEN, tickle_connection_traverse, delete_cons);
-
-       /* now we've finished traverse, it's safe to do deletion. */
-       talloc_free(delete_cons);
-
-       /* If there are no more connections to kill we can remove the
-          entire killtcp structure
-        */
-       if ( (killtcp->connections == NULL) || 
-            (killtcp->connections->root == NULL) ) {
-               talloc_free(killtcp);
-               return;
-       }
-
-       /* try tickling them again in a seconds time
-        */
-       tevent_add_timer(killtcp->ctdb->ev, killtcp,
-                        timeval_current_ofs(1, 0),
-                        ctdb_tickle_sentenced_connections, killtcp);
-}
-
-/*
-  destroy the killtcp structure
- */
-static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp)
-{
-       struct ctdb_vnn *tmpvnn;
-
-       /* verify that this vnn is still active */
-       for (tmpvnn = killtcp->ctdb->vnn; tmpvnn; tmpvnn = tmpvnn->next) {
-               if (tmpvnn == killtcp->vnn) {
-                       break;
-               }
-       }
-
-       if (tmpvnn == NULL) {
-               return 0;
-       }
-
-       if (killtcp->vnn->killtcp != killtcp) {
-               return 0;
-       }
-
-       killtcp->vnn->killtcp = NULL;
-
-       return 0;
-}
-
-
-/* nothing fancy here, just unconditionally replace any existing
-   connection structure with the new one.
-
-   don't even free the old one if it did exist, that one is talloc_stolen
-   by the same node in the tree anyway and will be deleted when the new data 
-   is deleted
-*/
-static void *add_killtcp_callback(void *parm, void *data)
-{
-       return parm;
-}
-
-/*
-  add a tcp socket to the list of connections we want to RST
- */
-static int ctdb_killtcp_add_connection(struct ctdb_context *ctdb, 
-                                      ctdb_sock_addr *s,
-                                      ctdb_sock_addr *d)
-{
-       ctdb_sock_addr src, dst;
-       struct ctdb_kill_tcp *killtcp;
-       struct ctdb_killtcp_con *con;
-       struct ctdb_vnn *vnn;
-
-       ctdb_canonicalize_ip(s, &src);
-       ctdb_canonicalize_ip(d, &dst);
-
-       vnn = find_public_ip_vnn(ctdb, &dst);
-       if (vnn == NULL) {
-               vnn = find_public_ip_vnn(ctdb, &src);
-       }
-       if (vnn == NULL) {
-               /* if it is not a public ip   it could be our 'single ip' */
-               if (ctdb->single_ip_vnn) {
-                       if (ctdb_same_ip(&ctdb->single_ip_vnn->public_address, &dst)) {
-                               vnn = ctdb->single_ip_vnn;
-                       }
-               }
-       }
-       if (vnn == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Could not killtcp, not a public address\n")); 
-               return -1;
-       }
-
-       killtcp = vnn->killtcp;
-       
-       /* If this is the first connection to kill we must allocate
-          a new structure
-        */
-       if (killtcp == NULL) {
-               killtcp = talloc_zero(vnn, struct ctdb_kill_tcp);
-               CTDB_NO_MEMORY(ctdb, killtcp);
-
-               killtcp->vnn         = vnn;
-               killtcp->ctdb        = ctdb;
-               killtcp->capture_fd  = -1;
-               killtcp->connections = trbt_create(killtcp, 0);
-
-               vnn->killtcp         = killtcp;
-               talloc_set_destructor(killtcp, ctdb_killtcp_destructor);
-       }
-
-
-
-       /* create a structure that describes this connection we want to
-          RST and store it in killtcp->connections
-       */
-       con = talloc(killtcp, struct ctdb_killtcp_con);
-       CTDB_NO_MEMORY(ctdb, con);
-       con->src_addr = src;
-       con->dst_addr = dst;
-       con->count    = 0;
-       con->killtcp  = killtcp;
-
-
-       trbt_insertarray32_callback(killtcp->connections,
-                       KILLTCP_KEYLEN, killtcp_key(&con->dst_addr, &con->src_addr),
-                       add_killtcp_callback, con);
-
-       /* 
-          If we don't have a socket to listen on yet we must create it
-        */
-       if (killtcp->capture_fd == -1) {
-               const char *iface = ctdb_vnn_iface_string(vnn);
-               killtcp->capture_fd = ctdb_sys_open_capture_socket(iface, &killtcp->private_data);
-               if (killtcp->capture_fd == -1) {
-                       DEBUG(DEBUG_CRIT,(__location__ " Failed to open capturing "
-                                         "socket on iface '%s' for killtcp (%s)\n",
-                                         iface, strerror(errno)));
-                       goto failed;
-               }
-       }
-
-
-       if (killtcp->fde == NULL) {
-               killtcp->fde = tevent_add_fd(ctdb->ev, killtcp,
-                                            killtcp->capture_fd,
-                                            TEVENT_FD_READ,
-                                            capture_tcp_handler, killtcp);
-               tevent_fd_set_auto_close(killtcp->fde);
-
-               /* We also need to set up some events to tickle all these connections
-                  until they are all reset
-               */
-               tevent_add_timer(ctdb->ev, killtcp, timeval_current_ofs(1, 0),
-                                ctdb_tickle_sentenced_connections, killtcp);
-       }
-
-       /* tickle him once now */
-       ctdb_sys_send_tcp(
-               &con->dst_addr,
-               &con->src_addr,
-               0, 0, 0);
-
-       return 0;
-
-failed:
-       talloc_free(vnn->killtcp);
-       vnn->killtcp = NULL;
-       return -1;
-}
-
-/*
-  kill a TCP connection.
- */
-int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata)
-{
-       struct ctdb_connection *killtcp = (struct ctdb_connection *)indata.dptr;
-
-       return ctdb_killtcp_add_connection(ctdb, &killtcp->src, &killtcp->dst);
-}
-
 /*
   called by a daemon to inform us of the entire list of TCP tickles for
   a particular public address.
@@ -3034,22 +2582,34 @@ int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA ind
        ctdb_sock_addr *addr = (ctdb_sock_addr *)indata.dptr;
        struct ctdb_tickle_list_old *list;
        struct ctdb_tcp_array *tcparray;
-       int num;
+       int num, i;
        struct ctdb_vnn *vnn;
+       unsigned port;
 
        vnn = find_public_ip_vnn(ctdb, addr);
        if (vnn == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n", 
+               DEBUG(DEBUG_ERR,(__location__ " Could not get tcp tickle list, '%s' is not a public address\n",
                        ctdb_addr_to_str(addr)));
 
                return 1;
        }
 
+       port = ctdb_addr_to_port(addr);
+
        tcparray = vnn->tcp_array;
-       if (tcparray) {
-               num = tcparray->num;
-       } else {
-               num = 0;
+       num = 0;
+       if (tcparray != NULL) {
+               if (port == 0) {
+                       /* All connections */
+                       num = tcparray->num;
+               } else {
+                       /* Count connections for port */
+                       for (i = 0; i < tcparray->num; i++) {
+                               if (port == ctdb_addr_to_port(&tcparray->connections[i].dst)) {
+                                       num++;
+                               }
+                       }
+               }
        }
 
        outdata->dsize = offsetof(struct ctdb_tickle_list_old, connections)
@@ -3061,9 +2621,18 @@ int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA ind
 
        list->addr = *addr;
        list->num = num;
-       if (num) {
-               memcpy(&list->connections[0], tcparray->connections,
-                       sizeof(struct ctdb_connection) * num);
+
+       if (num == 0) {
+               return 0;
+       }
+
+       num = 0;
+       for (i = 0; i < tcparray->num; i++) {
+               if (port == 0 || \
+                   port == ctdb_addr_to_port(&tcparray->connections[i].dst)) {
+                       list->connections[num] = tcparray->connections[i];
+                       num++;
+               }
        }
 
        return 0;