From a0f1c0e281ee78ab8ee874bbb6c2140c12101284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=BA=20=28=D0=B4=D0=BE=D0=BC=D0=B0=29?= Date: Mon, 27 Aug 2012 23:39:29 +0600 Subject: ct_dump_stats: detect when stats are not available 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 | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/netfilter/ct_obj.c b/lib/netfilter/ct_obj.c index c205427..70a814f 100644 --- a/lib/netfilter/ct_obj.c +++ b/lib/netfilter/ct_obj.c @@ -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, -- cgit v1.2.1