Skip to contentSkip to conversation

my recent meddling with ip_conntrack

PATCH New Patrick Schaaf 1 participant 1 message Aug 3, 2002 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 my recent meddling with ip_conntrack

Hi netfilter-devel & netdev,

I have pulled my recent ip_conntrack patches up to 2.4.19, and have that merge running now on my shiny new dual P-MMX 200. No surprises. It's already up 40 minutes with hundreds of connections tracked!

Patch appended for curious people and would-be testers. All comments welcome. This is not meant for inclusion anywhere, right now, just looking for some eyeballs.

have a nice weekend
Patrick

Short Changelog, in order of probable importance:

- netfilter hook statistics, /proc/net/nf_hook_stat*, as a compile option found under "Networking Options". Per-hook-function rdtscll() based timing and occurrence counting. See netfilter in action for yourself!
- remove unneccessary add_timer() calls from per-packet processing. Introduces new ip_conntrack->timeout_target, 4 byte in size. The running timer is never disturbed when increasing monotonically. That covers the normal ESTABLISHED case. When the timer runs out, it possibly restarts itself to the then-current timeout_target.
- prefer to allocate the ip_conntrack hash using get_free_pages()
- use a single linked list to hash them. BTW, with bucket count autoselection, this change doubles the number of available buckets. Saves four byte per ip_conntrack_hash_tuple, 8 byte per ip_conntrack.
- in include/linux/skbuff.h, introduce nf_skb_forget(), and use that to cleanup several of places in ipv4/ core stack code.
- make init_conntrack() a bit more sane, removes unneccessary hash computations.

---------------------- Would send the following csets --------------------- [email protected], 2002-08-03 18:20:26+02:00, bof@cdr.(none)
Merge bkbits-linux-2.4 after 2.4.19

[email protected], 2002-08-02 09:02:19+02:00, bof@cdr.(none)
ip_conntrack_standalone.c:
cleanup /proc/net/ip_conntrack output - same as always, now.

[email protected], 2002-08-02 09:00:13+02:00, bof@cdr.(none)
ip_conntrack.h:
prototype for new ip_ct_sudden_death()
ip_conntrack_proto_icmp.c, ip_conntrack_proto_tcp.c:

    use ip_ct_sudden_death(), instead of fiddling with ct->timeout directly.
  ip_conntrack_core.c:
    introduce ct->timeout_target, makes add_timer() a rare event.
  ip_conntrack_standalone.c:
    show ct->timeout_target in /proc/net/ip_conntrack.

[email protected], 2002-08-01 22:52:01+02:00, bof@cdr.(none)
ip_conntrack_core.c:
more add_timer avoidance

[email protected], 2002-08-01 22:27:28+02:00, bof@cdr.(none)
ip_conntrack.h, ip_conntrack_core.c:
begin add_timer avoidance

[email protected], 2002-08-01 21:21:14+02:00, bof@cdr.(none)
ip_conntrack_core.c:
get_free_pages allocation for ip_conntrack_hash

[email protected], 2002-08-01 19:04:41+02:00, bof@cdr.(none)
netfilter.c:
remove KERN_NOTICE output

[email protected], 2002-08-01 09:45:24+02:00, bof@cdr.(none)
srlist.h:
fix single ring list code
ip_conntrack_core.c:
type agnostic ip_conntrack_hash allocation

[email protected], 2002-07-31 20:57:39+02:00, bof@cdr.(none)
include/linux/netfilter_ipv4/srlist.h:
introduced. a single ring list implementation with almost the same interface as the netfilter_ipv4/listhelp.h
include/linux/netfilter_ipv4/ip_conntrack*.h:
use srlist_head in place of list_head for conntrack tuple hashing.
net/ipv4/netfilter/ip_conntrack_{core,standalone}.c:
use srlist_head instead of list_head for conntrack tuple hashing.

[email protected], 2002-07-29 09:25:21+02:00, bof@cdr.(none)
netfilter.c:
some more comments, minimal cleanup, KERN_NOTICE upon (un)registration.
Configure.help:
friendly help and advise regarding CONFIG_NETFILTER_HOOK_STAT

[email protected], 2002-07-28 11:58:38+02:00, bof@cdr.(none)
net/core/netfilter.c:
remove debug printks related to slabifying hook statistic counters.

[email protected], 2002-07-28 11:54:23+02:00, bof@cdr.(none)
net/core/netfilter.c:
slabify, make per-cpu counters.

[email protected], 2002-07-27 19:16:23+02:00, bof@cdr.(none)
Config.in, netfilter.h, netfilter.c:
netfilter hook statistics

[email protected], 2002-07-22 21:07:02+02:00, bof@cdr.(none)
overall:
compiles now, skb_nf_forget() introduction probably OK.
skbuff.h:
sk_buff speling fix

[email protected], 2002-07-22 20:45:22+02:00, bof@cdr.(none)
skbuff.h:
define skb_nf_forget()
skbuff.c, ip_conntrack_core.c, ipt_REJECT.c, ipip.c, ip_gre.c, sit.c:
use skb_nf_forget()
ip_input.c, ipmr.c:
use skb_nf_forget()
NOTE: original code did not clear nf_debug. Now it will.

[email protected], 2002-07-22 09:32:20+02:00, bof@cdr.(none)
ip_conntrack_core.c:
in init_conntrack(), rename drop_next to drop_rotor: document recent change.

[email protected], 2002-07-22 09:31:29+02:00, bof@cdr.(none)
ip_conntrack_core.c:
in init_conntrack(), narrow scope of static drop_next.

[email protected], 2002-07-22 09:30:29+02:00, bof@cdr.(none)
ip_conntrack_core.c:
sanitize typing for hash_conntrack() return value: always use u_int32_t.

[email protected], 2002-07-22 09:24:39+02:00, bof@cdr.(none)
ip_conntrack_core.c:
remove hash calculation from unconditional part of init_conntrack(), to the rare place where it is needed.

[email protected], 2002-07-22 09:23:20+02:00, bof@cdr.(none)
ip_conntrack_core.c:
remove repl_hash calculation in init_conntrack(): it was not used.

---------------------------------------------------------------------------

Documentation/Configure.help +17 -0
diff -urN ex1/Documentation/Configure.help ex2/Documentation/Configure.help
@@ -2429,6 +2429,23 @@
   You can say Y here if you want to get additional messages useful in
   debugging the netfilter code.
 
+Netfilter hook statistics
+CONFIG_NETFILTER_HOOK_STAT
+  If you say Y here, the time spent in the various netfilter hook
+  functions is measured, using the TSC of your processor. Your
+  kernel won't boot when you don't have a working TSC.
+  Say N when you don't have a modern Intel/AMD processor.
+
+  When enabled, look at /proc/net/nf_stat_hook_* for the actual
+  measurement results, presented in a format easy to guess by
+  any well-calibrated crystal ball.
+
+  The timing imposes a processing overhead that may be relevant
+  on machines with high packet rates. The overhead is estimated
+  at about 5% of the time used by the hook functions, themselves.
+
+  The safe thing is to say N.
+
 Connection tracking (required for masq/NAT)
 CONFIG_IP_NF_CONNTRACK
   Connection tracking keeps a record of what packets have passed
include/linux/netfilter.h +3 -0
diff -urN ex1/include/linux/netfilter.h ex2/include/linux/netfilter.h
@@ -51,6 +51,9 @@
 	int hooknum;
 	/* Hooks are ordered in ascending priority. */
 	int priority;
+#ifdef CONFIG_NETFILTER_HOOK_STAT
+	void *hook_stat;
+#endif
 };
 
 struct nf_sockopt_ops
include/linux/netfilter_ipv4/ip_conntrack.h +4 -0
diff -urN ex1/include/linux/netfilter_ipv4/ip_conntrack.h ex2/include/linux/netfilter_ipv4/ip_conntrack.h
@@ -97,6 +97,7 @@
 	volatile unsigned long status;
 
 	/* Timer function; drops refcnt when it goes off. */
+	unsigned long timeout_target;
 	struct timer_list timeout;
 
 	/* If we're expecting another related connection, this will be
@@ -160,6 +161,9 @@
 
 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
 			  const struct ip_conntrack_tuple *orig);
+
+/* Kill this conntrack immediately, without regard to timeouts. */
+extern int ip_ct_sudden_death(struct ip_conntrack *ct);
 
 /* Refresh conntrack for this many jiffies */
 extern void ip_ct_refresh(struct ip_conntrack *ct,
include/linux/netfilter_ipv4/ip_conntrack_core.h +2 -1
diff -urN ex1/include/linux/netfilter_ipv4/ip_conntrack_core.h ex2/include/linux/netfilter_ipv4/ip_conntrack_core.h
@@ -1,6 +1,7 @@
 #ifndef _IP_CONNTRACK_CORE_H
 #define _IP_CONNTRACK_CORE_H
 #include <linux/netfilter_ipv4/lockhelp.h>
+#include <linux/netfilter_ipv4/srlist.h>
 
 /* This header is used to share core functionality between the
    standalone connection tracking module, and the compatibility layer's use
@@ -44,7 +45,7 @@
 	return NF_ACCEPT;
 }
 
-extern struct list_head *ip_conntrack_hash;
+extern struct srlist_head *ip_conntrack_hash;
 extern struct list_head expect_list;
 DECLARE_RWLOCK_EXTERN(ip_conntrack_lock);
 #endif /* _IP_CONNTRACK_CORE_H */
include/linux/netfilter_ipv4/ip_conntrack_tuple.h +3 -1
diff -urN ex1/include/linux/netfilter_ipv4/ip_conntrack_tuple.h ex2/include/linux/netfilter_ipv4/ip_conntrack_tuple.h
@@ -1,6 +1,8 @@
 #ifndef _IP_CONNTRACK_TUPLE_H
 #define _IP_CONNTRACK_TUPLE_H
 
+#include <linux/netfilter_ipv4/srlist.h>
+
 /* A `tuple' is a structure containing the information to uniquely
   identify a connection.  ie. if two packets have the same tuple, they
   are in the same connection; if not, they are not.
@@ -85,7 +87,7 @@
 /* Connections have two entries in the hash table: one for each way */
 struct ip_conntrack_tuple_hash
 {
-	struct list_head list;
+	struct srlist_head list;
 
 	struct ip_conntrack_tuple tuple;
 
include/linux/netfilter_ipv4/srlist.h +78 -0

78 lines not on this page · raw message

include/linux/skbuff.h +11 -0
diff -urN ex1/include/linux/skbuff.h ex2/include/linux/skbuff.h
@@ -1144,6 +1144,17 @@
 	if (nfct)
 		atomic_inc(&nfct->master->use);
 }
+static inline void
+skb_nf_forget(struct sk_buff *skb)
+{
+	nf_conntrack_put(skb->nfct);
+	skb->nfct = NULL;
+#ifdef CONFIG_NETFILTER_DEBUG
+	skb->nf_debug = 0;
+#endif
+}
+#else
+static inline void skb_nf_forget(struct sk_buff *skb) {}
 #endif
 
 #endif	/* __KERNEL__ */
net/Config.in +1 -0
diff -urN ex1/net/Config.in ex2/net/Config.in
@@ -13,6 +13,7 @@
 bool 'Network packet filtering (replaces ipchains)' CONFIG_NETFILTER
 if [ "$CONFIG_NETFILTER" = "y" ]; then
    bool '  Network packet filtering debugging' CONFIG_NETFILTER_DEBUG
+   bool '  Netfilter hook statistics' CONFIG_NETFILTER_HOOK_STAT
 fi
 bool 'Socket Filtering'  CONFIG_FILTER
 tristate 'Unix domain sockets' CONFIG_UNIX
net/core/netfilter.c +296 -0

338 lines not on this page · raw message

net/core/skbuff.c +1 -3
diff -urN ex1/net/core/skbuff.c ex2/net/core/skbuff.c
@@ -323,9 +323,7 @@
 		}
 		skb->destructor(skb);
 	}
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-#endif
+	skb_nf_forget(skb);
 	skb_headerinit(skb, NULL, 0);  /* clean state */
 	kfree_skbmem(skb);
 }
net/ipv4/ip_gre.c +2 -14
diff -urN ex1/net/ipv4/ip_gre.c ex2/net/ipv4/ip_gre.c
@@ -644,13 +644,7 @@
 		skb->dev = tunnel->dev;
 		dst_release(skb->dst);
 		skb->dst = NULL;
-#ifdef CONFIG_NETFILTER
-		nf_conntrack_put(skb->nfct);
-		skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-		skb->nf_debug = 0;
-#endif
-#endif
+		skb_nf_forget(skb);
 		ipgre_ecn_decapsulate(iph, skb);
 		netif_rx(skb);
 		read_unlock(&ipgre_lock);
@@ -876,13 +870,7 @@
 		}
 	}
 
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-	skb->nf_debug = 0;
-#endif
-#endif
+	skb_nf_forget(skb);
 
 	IPTUNNEL_XMIT();
 	tunnel->recursion--;
net/ipv4/ip_input.c +1 -4
diff -urN ex1/net/ipv4/ip_input.c ex2/net/ipv4/ip_input.c
@@ -226,12 +226,9 @@
 
 	__skb_pull(skb, ihl);
 
-#ifdef CONFIG_NETFILTER
 	/* Free reference early: we don't need it any more, and it may
            hold ip_conntrack module loaded indefinitely. */
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#endif /*CONFIG_NETFILTER*/
+	skb_nf_forget(skb);
 
         /* Point into the IP datagram, just past the header. */
         skb->h.raw = skb->data;
net/ipv4/ipip.c +2 -14
diff -urN ex1/net/ipv4/ipip.c ex2/net/ipv4/ipip.c
@@ -493,13 +493,7 @@
 		skb->dev = tunnel->dev;
 		dst_release(skb->dst);
 		skb->dst = NULL;
-#ifdef CONFIG_NETFILTER
-		nf_conntrack_put(skb->nfct);
-		skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-		skb->nf_debug = 0;
-#endif
-#endif
+		skb_nf_forget(skb);
 		ipip_ecn_decapsulate(iph, skb);
 		netif_rx(skb);
 		read_unlock(&ipip_lock);
@@ -644,13 +638,7 @@
 	if ((iph->ttl = tiph->ttl) == 0)
 		iph->ttl	=	old_iph->ttl;
 
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-	skb->nf_debug = 0;
-#endif
-#endif
+	skb_nf_forget(skb);
 
 	IPTUNNEL_XMIT();
 	tunnel->recursion--;
net/ipv4/ipmr.c +3 -12
diff -urN ex1/net/ipv4/ipmr.c ex2/net/ipv4/ipmr.c
@@ -1096,10 +1096,7 @@
 
 	skb->h.ipiph = skb->nh.iph;
 	skb->nh.iph = iph;
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#endif
+	skb_nf_forget(skb);
 }
 
 static inline int ipmr_forward_finish(struct sk_buff *skb)
@@ -1441,10 +1438,7 @@
 	skb->dst = NULL;
 	((struct net_device_stats*)reg_dev->priv)->rx_bytes += skb->len;
 	((struct net_device_stats*)reg_dev->priv)->rx_packets++;
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#endif
+	skb_nf_forget(skb);
 	netif_rx(skb);
 	dev_put(reg_dev);
 	return 0;
@@ -1508,10 +1502,7 @@
 	((struct net_device_stats*)reg_dev->priv)->rx_bytes += skb->len;
 	((struct net_device_stats*)reg_dev->priv)->rx_packets++;
 	skb->dst = NULL;
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#endif
+	skb_nf_forget(skb);
 	netif_rx(skb);
 	dev_put(reg_dev);
 	return 0;
net/ipv4/netfilter/ip_conntrack_core.c +94 -58

329 lines not on this page · raw message

net/ipv4/netfilter/ip_conntrack_proto_icmp.c +2 -3
diff -urN ex1/net/ipv4/netfilter/ip_conntrack_proto_icmp.c ex2/net/ipv4/netfilter/ip_conntrack_proto_icmp.c
@@ -77,9 +77,8 @@
            means this will only run once even if count hits zero twice
            (theoretically possible with SMP) */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
-		if (atomic_dec_and_test(&ct->proto.icmp.count)
-		    && del_timer(&ct->timeout))
-			ct->timeout.function((unsigned long)ct);
+		if (atomic_dec_and_test(&ct->proto.icmp.count))
+			ip_ct_sudden_death(ct);
 	} else {
 		atomic_inc(&ct->proto.icmp.count);
 		ip_ct_refresh(ct, ICMP_TIMEOUT);
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +1 -2
diff -urN ex1/net/ipv4/netfilter/ip_conntrack_proto_tcp.c ex2/net/ipv4/netfilter/ip_conntrack_proto_tcp.c
@@ -189,8 +189,7 @@
 	   problem case, so we can delete the conntrack
 	   immediately.  --RR */
 	if (!(conntrack->status & IPS_SEEN_REPLY) && tcph->rst) {
-		if (del_timer(&conntrack->timeout))
-			conntrack->timeout.function((unsigned long)conntrack);
+		ip_ct_sudden_death(conntrack);
 	} else {
 		/* Set ASSURED if we see see valid ack in ESTABLISHED after SYN_RECV */
 		if (oldtcpstate == TCP_CONNTRACK_SYN_RECV
net/ipv4/netfilter/ip_conntrack_standalone.c +2 -2
diff -urN ex1/net/ipv4/netfilter/ip_conntrack_standalone.c ex2/net/ipv4/netfilter/ip_conntrack_standalone.c
@@ -83,7 +83,7 @@
 		      conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
 		      .tuple.dst.protonum,
 		      timer_pending(&conntrack->timeout)
-		      ? (conntrack->timeout.expires - jiffies)/HZ : 0);
+		      ? (conntrack->timeout_target - jiffies)/HZ : 0);
 
 	len += proto->print_conntrack(buffer + len, conntrack);
 	len += print_tuple(buffer + len,
@@ -140,7 +140,7 @@
 	READ_LOCK(&ip_conntrack_lock);
 	/* Traverse hash; print originals then reply. */
 	for (i = 0; i < ip_conntrack_htable_size; i++) {
-		if (LIST_FIND(&ip_conntrack_hash[i], conntrack_iterate,
+		if (SRLIST_FIND(&ip_conntrack_hash[i], conntrack_iterate,
 			      struct ip_conntrack_tuple_hash *,
 			      buffer, offset, &upto, &len, length))
 			goto finished;
net/ipv4/netfilter/ipt_REJECT.c +1 -5
diff -urN ex1/net/ipv4/netfilter/ipt_REJECT.c ex2/net/ipv4/netfilter/ipt_REJECT.c
@@ -69,12 +69,8 @@
 		return;
 
 	/* This packet will not be the same as the other: clear nf fields */
-	nf_conntrack_put(nskb->nfct);
-	nskb->nfct = NULL;
 	nskb->nfcache = 0;
-#ifdef CONFIG_NETFILTER_DEBUG
-	nskb->nf_debug = 0;
-#endif
+	skb_nf_forget(nskb);
 
 	tcph = (struct tcphdr *)((u_int32_t*)nskb->nh.iph + nskb->nh.iph->ihl);
 
net/ipv6/sit.c +2 -14
diff -urN ex1/net/ipv6/sit.c ex2/net/ipv6/sit.c
@@ -403,13 +403,7 @@
 		skb->dev = tunnel->dev;
 		dst_release(skb->dst);
 		skb->dst = NULL;
-#ifdef CONFIG_NETFILTER
-		nf_conntrack_put(skb->nfct);
-		skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-		skb->nf_debug = 0;
-#endif
-#endif
+		skb_nf_forget(skb);
 		ipip6_ecn_decapsulate(iph, skb);
 		netif_rx(skb);
 		read_unlock(&ipip6_lock);
@@ -600,13 +594,7 @@
 	if ((iph->ttl = tiph->ttl) == 0)
 		iph->ttl	=	iph6->hop_limit;
 
-#ifdef CONFIG_NETFILTER
-	nf_conntrack_put(skb->nfct);
-	skb->nfct = NULL;
-#ifdef CONFIG_NETFILTER_DEBUG
-	skb->nf_debug = 0;
-#endif
-#endif
+	skb_nf_forget(skb);
 
 	IPTUNNEL_XMIT();
 	tunnel->recursion--;