NEW: ctstat
Subsystems (2)
Hi
Here's a small patch I wrote a week ago (5 min before the first machine it's used on went into production so it will have bugs :)
It's a complete ripoff of rtstat but for conntrack. (rtstat userspace can be found here:
ftp://robur.slu.se/pub/Linux/net-development/rt_cache_stat/rtstat.c kernelspace has been included in kernel for a long time now)
What it does is to keep per-cpu statistics of diffrent conntrack events such as connection found in hashtable, new entry allocated etc, in /proc/net/ip_conntrack_stat
I've attached the patch and userspace-program to make sense of it.
example output:
$ ctstat -i 1 size found new ins del ign inv insf drop edrop ie en ec ed 16062 3782 91 81 115 2 7 32 0 0 0 0 0 0 16051 4000 96 88 107 2 5 42 0 0 0 0 0 0 16058 4042 89 80 82 0 4 33 0 0 0 0 0 0 16065 4065 97 85 90 4 11 35 0 0 0 0 0 0 16079 3589 103 99 89 0 11 48 0 0 0 0 0 0
A description of the diffrent fields can be found in the usage-message of ctstat.
I can't get some values to add up, like the insf (insert into hashtable failed because one of the tuples already exists) values.
ins + insf <= new should be true but what I'm seeing is
ins + insf > new.
I havn't looked into it yet.
del should be split up into two fields, removal from hashtable and deallocation, right now it's just deallocation.
There's probably more that should be changed. This is something I'd like to see in kernel some day since it makes it a lot easier to see what's going on and if some extra tuning is needed. It shouldn't add many cycles to the processing of a packet.
Signature · 1 line
/Martin
include/linux/netfilter_ipv4/ip_conntrack.h renamed from linux-2.4.21-rc2/include/linux/netfilter_ipv4/ip_conntrack.h +18 -0
@@ -266,5 +266,23 @@ static inline int is_confirmed(struct ip }extern unsigned int ip_conntrack_htable_size; + +struct ip_conntrack_stat +{ +unsigned int found; +unsigned int new; +unsigned int invalid; +unsigned int ignore; +unsigned int delete; +unsigned int insert; +unsigned int insert_failed; +unsigned int drop; +unsigned int early_drop; +unsigned int icmp_error; +unsigned int expect_new; +unsigned int expect_create; +unsigned int expect_delete; +}____cacheline_aligned_in_smp ; +#endif /* __KERNEL__ */ #endif /* _IP_CONNTRACK_H */
net/ipv4/netfilter/ip_conntrack_core.c renamed from linux-2.4.21-rc2/net/ipv4/netfilter/ip_conntrack_core.c +31 -7
@@ -61,10 +61,12 @@ LIST_HEAD(protocol_list);static LIST_HEAD (helpers);unsigned int ip_conntrack_htable_size =0 ;static int ip_conntrack_max =0 ; -static atomic_t ip_conntrack_count =ATOMIC_INIT (0 ); +atomic_t ip_conntrack_count =ATOMIC_INIT (0 );struct list_head *ip_conntrack_hash;static kmem_cache_t *ip_conntrack_cachep; +struct ip_conntrack_stat ip_conntrack_stat[NR_CPUS ]; +extern struct ip_conntrack_protocol ip_conntrack_generic_protocol; static inline int proto_cmpfn(const struct ip_conntrack_protocol *curr,
@@ -179,6 +181,7 @@ destroy_expect(struct ip_conntrack_expecIP_NF_ASSERT (!timer_pending (&exp->timeout));kfree (exp); + ip_conntrack_stat[smp_processor_id ()].expect_delete++; }
@@ -342,6 +345,7 @@ destroy_conntrack(struct nf_conntrack *nDEBUGP ("destroy_conntrack: returning ct=%p to slab\n" , ct);kmem_cache_free (ip_conntrack_cachep, ct);atomic_dec (&ip_conntrack_count); + ip_conntrack_stat[smp_processor_id ()].delete++; }static void death_by_timeout (unsigned long ul_conntrack)
@@ -469,10 +473,12 @@ __ip_conntrack_confirm(struct nf_ct_infoatomic_inc (&ct->ct_general.use);set_bit (IPS_CONFIRMED_BIT , &ct->status);WRITE_UNLOCK (&ip_conntrack_lock); + ip_conntrack_stat[smp_processor_id ()].insert++;return NF_ACCEPT ; }WRITE_UNLOCK (&ip_conntrack_lock); + ip_conntrack_stat[smp_processor_id ()].insert_failed++;return NF_DROP ; }
@@ -607,6 +613,7 @@ static int early_drop(struct list_head *if (del_timer (&h->ctrack->timeout)) {death_by_timeout ((unsigned long )h->ctrack); dropped =1 ; + ip_conntrack_stat[smp_processor_id ()].early_drop++; }ip_conntrack_put (h->ctrack);return dropped;
@@ -731,12 +738,16 @@ init_conntrack(const struct ip_conntrackLIST_DELETE (&ip_conntrack_expect_list, expected); expected->expectant->expecting--;nf_conntrack_get (&master_ct (conntrack)->infos[0 ]); - } + ip_conntrack_stat[smp_processor_id ()].expect_new++; + }else + ip_conntrack_stat[smp_processor_id ()].new++; +atomic_inc (&ip_conntrack_count);WRITE_UNLOCK (&ip_conntrack_lock);if (expected && expected->expectfn) expected->expectfn (conntrack); +return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL ]; }
@@ -764,7 +775,8 @@ resolve_normal_ct(struct sk_buff *skb,return NULL ;if (IS_ERR (h))return (void *)h; - } + }else + ip_conntrack_stat[smp_processor_id ()].found++;/* It exists; we have (non-exclusive) reference. */ if (DIRECTION (h) ==IP_CT_DIR_REPLY ) {
@@ -825,8 +837,10 @@ unsigned int ip_conntrack_in(unsigned in/* Previously seen (loopback)? Ignore. Do this before fragment check. */ -if ((*pskb)->nfct) +if ((*pskb)->nfct) { + ip_conntrack_stat[smp_processor_id ()].ignore++;return NF_ACCEPT ; + }/* Gather fragments. */ if ((*pskb)->nh.iph->frag_off &htons (IP_MF |IP_OFFSET )) {
@@ -839,22 +853,29 @@ unsigned int ip_conntrack_in(unsigned in/* It may be an icmp error... */ if ((*pskb)->nh.iph->protocol ==IPPROTO_ICMP - &&icmp_error_track (*pskb, &ctinfo, hooknum)) + &&icmp_error_track (*pskb, &ctinfo, hooknum)) { + ip_conntrack_stat[smp_processor_id ()].icmp_error++;return NF_ACCEPT ; + } -if (!(ct =resolve_normal_ct (*pskb, proto,&set_reply,hooknum,&ctinfo))) +if (!(ct =resolve_normal_ct (*pskb, proto,&set_reply,hooknum,&ctinfo))) {/* Not valid part of a connection */ + ip_conntrack_stat[smp_processor_id ()].invalid++;return NF_ACCEPT ; + } -if (IS_ERR (ct)) +if (IS_ERR (ct)) {/* Too stressed to deal. */ + ip_conntrack_stat[smp_processor_id ()].drop;return NF_DROP ; + }IP_NF_ASSERT ((*pskb)->nfct); ret = proto->packet (ct, (*pskb)->nh.iph, (*pskb)->len, ctinfo);if (ret ==-1 ) {/* Invalid */ + ip_conntrack_stat[smp_processor_id ()].invalid++;nf_conntrack_put ((*pskb)->nfct); (*pskb)->nfct =NULL ;return NF_ACCEPT ;
@@ -865,6 +886,7 @@ unsigned int ip_conntrack_in(unsigned in ct, ctinfo);if (ret ==-1 ) {/* Invalid */ + ip_conntrack_stat[smp_processor_id ()].invalid++;nf_conntrack_put ((*pskb)->nfct); (*pskb)->nfct =NULL ;return NF_ACCEPT ;
@@ -1063,6 +1085,8 @@ int ip_conntrack_expect_related(struct iWRITE_UNLOCK (&ip_conntrack_lock); + ip_conntrack_stat[smp_processor_id ()].expect_create++; +return ret; }
net/ipv4/netfilter/ip_conntrack_standalone.c renamed from linux-2.4.21-rc2/net/ipv4/netfilter/ip_conntrack_standalone.c +52 -3
90 lines not on this page · raw message
/* ctstat.c: A program for ip_conntrack monitoring
*
* Copyright 2001 by Robert Olsson <[email protected]>
* Uppsala University, Sweden
* Modified from rtstat.c into ctstat.c by Martin Josefsson
* <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Additional credits:
* Martin Josefsson <[email protected]> 010828 bug fix
* 030420 cleanup
*
*/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#define VERSION "0.35-ct1 030430"
extern char *optarg;
extern int optind, opterr, optopt;
FILE *fp;
unsigned ct_size, found[2], new[2], invalid[2],
ignore[2], delete[2], insert[2], insert_failed[2], drop[2], early_drop[2], icmp_error[2], expect_new[2], expect_create[2], expect_delete[2];
/* Read (and summarize for SMP) the different stats vars. */
void scan_line(int i)
{
unsigned temp[14];
i %= 2;
found[i] = 0;
new[i] = 0;
invalid[i] = 0;
ignore[i] = 0;
delete[i] = 0;
insert[i] = 0;
insert_failed[i] = 0;
drop[i] = 0;
early_drop[i] = 0;
icmp_error[i] = 0;
expect_new[i] = 0;
expect_create[i] = 0;
expect_delete[i] = 0;
while(!feof(fp)) {
fscanf(fp, "%x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
&ct_size,
&temp[0], /* found */
&temp[1], /* new */
&temp[2], /* invalid */
&temp[3], /* ignore */
&temp[4], /* delete */
&temp[5], /* insert */
&temp[6], /* insert_failed */
&temp[7], /* drop */
&temp[8], /* early_drop */
&temp[9], /* icmp_error */
&temp[10], /* expect_new */
&temp[11], /* expect_create */
&temp[12] /* expect_delete */
);
found[i] += temp[0];
new[i] += temp[1];
invalid[i] += temp[2];
ignore[i] += temp[3];
delete[i] += temp[4];
insert[i] += temp[5];
insert_failed[i] += temp[6];
drop[i] += temp[7];
early_drop[i] += temp[8];
icmp_error[i] += temp[9];
expect_new[i] += temp[10];
expect_create[i] += temp[11];
expect_delete[i] += temp[12];
}
}void print_hdr_line(void)
{
printf(" size found new ins del ign inv insf drop edrop ie en ec ed\n");
}int usage(int exit_code)
{
fprintf(stderr, "ctstat Version %s\n", VERSION);
fprintf(stderr, " -help\n");
fprintf(stderr, " -i interval\n");
fprintf(stderr, " -s subject [0-2]\n");
fprintf(stderr, "\n");
print_hdr_line();
fprintf(stderr, "\n");
fprintf(stderr, "size == ip_conntrack size\n");
fprintf(stderr, "found == found conntrack entries per sec\n");
fprintf(stderr, "new == newly allocated entries per sec\n");
fprintf(stderr, "ins == inserts into hashtable per sec\n");
fprintf(stderr, "del == deletions from hashtable per sec\n");
fprintf(stderr, "ign == ignored packets per sec\n");
fprintf(stderr, "inv == invalid packets per sec\n");
fprintf(stderr, "insf == failed inserts into hashtable per sec\n");
fprintf(stderr, "drop == dropped packets per sec\n");
fprintf(stderr, "edrop == early_dropped packets per sec\n");
fprintf(stderr, "ie == icmp errors per sec\n");
fprintf(stderr, "en == expectations used per sec\n");
fprintf(stderr, "ec == expectations created per sec\n");
fprintf(stderr, "ed == expectations (unused) deleted per sec\n");
exit(exit_code);
}void print(int i, int interval)
{
int val_new = i % 2;
int val_old = !val_new;
printf("%6u %6u %5u %5u %5u %7u %5u %5u %7u %5u %5u %7u %5u %5u\n",
ct_size,
(found[val_new] - found[val_old])/interval, (new[val_new] - new[val_old])/interval, (insert[val_new] - insert[val_old])/interval, (delete[val_new] - delete[val_old])/interval,
(ignore[val_new] - ignore[val_old])/interval, (invalid[val_new] - invalid[val_old])/interval, (icmp_error[val_new] - icmp_error[val_old])/interval,
(insert_failed[val_new] - insert_failed[val_old])/interval, (drop[val_new] - drop[val_old])/interval, (early_drop[val_new] - early_drop[val_old])/interval,
(expect_new[val_new] - expect_new[val_old])/interval, (expect_create[val_new] - expect_create[val_old])/interval, (expect_delete[val_new] - expect_delete[val_old])/interval);
}
int main(int argc, char **argv)
{
int c, i, interval = 2, hdr = 2;
while ((c=getopt(argc, argv,"h?s:i:")) != EOF) {
switch (c)
{
case '?':
case 'h':
usage(0);
break;
case 'i':
sscanf(optarg, "%u", &interval);
break;
case 's':
sscanf(optarg, "%u", &hdr);
break;
default:
usage(1);
}
}
if (interval < 1)
interval = 1;
fp = fopen("/proc/net/ip_conntrack_stat", "r");
if (!fp) {
perror("fopen");
exit(-1);
}
if (hdr > 0)
print_hdr_line();
for(i = 0;;i++) {
if(hdr > 1 && i != 0 && (i % 20) == 0)
print_hdr_line();
scan_line(i);
rewind(fp);
if (i != 0)
print(i, interval);
sleep(interval);
}
return 1;
}/*
* Compile:
gcc -g -O2 -Wall -o ctstat ctstat.c
*/