]> granicus.if.org Git - libnl/commitdiff
ct_dump_stats: detect when stats are not available
authorКоренберг Марк (дома) <socketpair@gmail.com>
Mon, 27 Aug 2012 17:39:29 +0000 (23:39 +0600)
committerКоренберг Марк <mark@ideco.ru>
Tue, 28 Aug 2012 12:53:33 +0000 (18:53 +0600)
Since about 2.6.27 kernel, stats are not enabled by default.
Stats can be enabled using sysctl named

net.netfilter.nf_conntrack_acct

So, do not print zeroes in stats if it's not available.
When not checked, trash may appear in output

lib/netfilter/ct_obj.c

index c205427b96e040d8c619b9a2edb9901d8063b1ed..70a814f58976c68d6ae2b90b5464fb70500ba448 100644 (file)
@@ -256,18 +256,29 @@ static void ct_dump_stats(struct nl_object *a, struct nl_dump_params *p)
        struct nfnl_ct *ct = (struct nfnl_ct *) a;
        double res;
        char *unit;
+       uint64_t packets;
+       const char * const names[] = {"rx", "tx"};
+       int i;
 
        ct_dump_details(a, p);
 
-       nl_dump_line(p, "        # packets      volume\n");
-
-       res = nl_cancel_down_bytes(nfnl_ct_get_bytes(ct, 1), &unit);
-       nl_dump_line(p, "    rx %10llu %7.2f %s\n",
-               nfnl_ct_get_packets(ct, 1), res, unit);
+       if (!nfnl_ct_test_bytes(ct, 0) ||
+           !nfnl_ct_test_packets(ct, 0) ||
+           !nfnl_ct_test_bytes(ct, 1) ||
+           !nfnl_ct_test_packets(ct, 1))
+           {
+               nl_dump_line(p, "    Statics are not available.\n");
+               nl_dump_line(p, "    Please set sysctl net.netfilter.nf_conntrack_acct = 1\n");
+               nl_dump_line(p, "    (Require kernel 2.6.27)\n");
+               return;
+           }
 
-       res = nl_cancel_down_bytes(nfnl_ct_get_bytes(ct, 0), &unit);
-       nl_dump_line(p, "    tx %10llu %7.2f %s\n",
-               nfnl_ct_get_packets(ct, 0), res, unit);
+       nl_dump_line(p, "        # packets      volume\n");
+       for (i=0; i<=1; i++) {
+               res = nl_cancel_down_bytes(nfnl_ct_get_bytes(ct, i), &unit);
+               packets = nfnl_ct_get_packets(ct, i);
+               nl_dump_line(p, "    %s %10llu %7.2f %s\n", names[i], packets, res, unit);
+       }
 }
 
 static int ct_compare(struct nl_object *_a, struct nl_object *_b,