ctdb-daemon: Move port filtering to server side when getting tickles
authorMartin Schwenke <martin@meltin.net>
Mon, 23 Mar 2015 09:18:25 +0000 (20:18 +1100)
committerAmitay Isaacs <amitay@samba.org>
Mon, 25 Apr 2016 05:10:14 +0000 (07:10 +0200)
Why allocate all that memory and transfer all that data across the
socket?

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/server/ctdb_takeover.c
ctdb/tools/ctdb.c

index 88656f974444f093ca87bc0ff857719cf4aec327..0caf55bff339d9e5f1c780e789c97f063ca3ba19 100644 (file)
@@ -2633,22 +2633,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)
@@ -2660,9 +2672,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;
index 2d4610f12231861cf070c769b38dae7cb5a7555f..350e2dfe5c87aac56348ffee28c4cc98abe5c970 100644 (file)
@@ -1479,7 +1479,7 @@ static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char *
                port = atoi(argv[1]);
        }
 
-       if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
+       if (parse_ip(argv[0], NULL, port, &addr) == 0) {
                DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
                return -1;
        }
@@ -1493,9 +1493,6 @@ static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char *
        if (options.machinereadable){
                printm(":source ip:port:destination ip:port:\n");
                for (i=0;i<list->num;i++) {
-                       if (port && port != ntohs(list->connections[i].dst.ip.sin_port)) {
-                               continue;
-                       }
                        printm(":%s:%u", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
                        printm(":%s:%u:\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
                }
@@ -1503,16 +1500,13 @@ static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char *
                printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
                printf("Num tickles:%u\n", list->num);
                for (i=0;i<list->num;i++) {
-                       if (port && port != ntohs(list->connections[i].dst.ip.sin_port)) {
-                               continue;
-                       }
                        printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
                        printf("DST: %s:%u\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
                }
        }
 
        talloc_free(list);
-       
+
        return 0;
 }