Skip to contentSkip to conversation

replace flags test with lookups (was: CLOSE_WAIT..)

PATCH-2.4-POM Willy Tarreau 2 participants 3 messages Oct 8, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw
Quoted 4 lines · Harald Welte
Quoted 4 lines

For example, either something like this (byte-aligned) :
u8 tcp_conntracks[6][10] = { (REPLY_STATE)<<4 | (ORIGINAL_STATE), ... }
-> read it from (tcp_conntrack[flag][old_state] >> dir?4:0) & 0xF

Quoted 8 lines
Quoted 5 lines

or (u32 aligned) 80 bytes :
int tcp_conntracks[2][10] = { /* original */

                                { /* old sNO */
                                  sSS << SYN | sTW << FIN | sES << ACK ...

-> read id from (tcp_conntrack[dir][old_state] >> flag*4) & 0xF.

this makes the code really hard to read. If you can come up with some easy-to-read macros, I'd be willing to give it a try.

Well, I've just benchmarked both a u32 and a u8 version against the original one, on x86. The shift operations are enough to slow them down, so it's not worth optimizing this way. The u8 implementation is already 50% slower than the original one, and the u32 implementation is twice as slow as the original one.

But I think that porting latest ipt_unclean changes to ip_conntrack_proto_tcp should at least give a slight boost (just compiled it, did not test) :

net/ipv4/netfilter/ip_conntrack_proto_tcp.c renamed from net/ipv4/netfilter/ip_conntrack_proto_tcp.c.old +19 -13
@@ -657,6 +657,24 @@
 #define	TH_ECE	0x40
 #define	TH_CWR	0x80
 
+/* table of valid flag combinations - ECE and CWR are always valid */
+static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
+{
+	[TH_SYN]			= 1,
+	[TH_SYN|TH_ACK]			= 1,
+	[TH_RST]			= 1,
+	[TH_RST|TH_ACK]			= 1,
+	[TH_RST|TH_ACK|TH_PUSH]		= 1,
+	[TH_FIN|TH_ACK]			= 1,
+	[TH_ACK]			= 1,
+	[TH_ACK|TH_PUSH]		= 1,
+	[TH_ACK|TH_URG]			= 1,
+	[TH_ACK|TH_URG|TH_PUSH]		= 1,
+	[TH_FIN|TH_ACK|TH_PUSH]		= 1,
+	[TH_FIN|TH_ACK|TH_URG]		= 1,
+	[TH_FIN|TH_ACK|TH_URG|TH_PUSH]	= 1
+};
+
 /* Protect conntrack agaist unclean packets. Code taken from ipt_unclean.c.  */
 static int unclean(struct iphdr *iph, size_t len)
 {
@@ -684,19 +702,7 @@
 
 	/* CHECK: TCP flags. */
 	tcpflags = (((u_int8_t *)tcph)[13] & ~(TH_ECE|TH_CWR));
-	if (tcpflags != TH_SYN
-	    && tcpflags != (TH_SYN|TH_ACK)
-	    && tcpflags != TH_RST
-	    && tcpflags != (TH_RST|TH_ACK)
-	    && tcpflags != (TH_RST|TH_ACK|TH_PUSH)
-	    && tcpflags != (TH_FIN|TH_ACK)
-	    && tcpflags != TH_ACK
-	    && tcpflags != (TH_ACK|TH_PUSH)
-	    && tcpflags != (TH_ACK|TH_URG)
-	    && tcpflags != (TH_ACK|TH_URG|TH_PUSH)
-	    && tcpflags != (TH_FIN|TH_ACK|TH_PUSH)
-	    && tcpflags != (TH_FIN|TH_ACK|TH_URG)
-	    && tcpflags != (TH_FIN|TH_ACK|TH_URG|TH_PUSH)) {
+	if (!tcp_valid_flags[tcpflags]) {
 		if (NET_RATELIMIT(ip_ct_tcp_log_out_of_window))
 			nf_log_ip((char *)iph, len, 
 				  "ip_conntrack_tcp: INVALID: invalid TCP flag combination ");

Regards,
Willy

#1 lore raw
Quoted 5 lines · Willy Tarreau

Well, I've just benchmarked both a u32 and a u8 version against the original one, on x86. The shift operations are enough to slow them down, so it's not worth optimizing this way. The u8 implementation is already 50% slower than the original one, and the u32 implementation is twice as slow as the original one.

Mh, as I assumed :(

Quoted 2 lines

But I think that porting latest ipt_unclean changes to ip_conntrack_proto_tcp should at least give a slight boost (just compiled it, did not test) :

Erm, are you aware of the
http://cvs.netfilter.org/netfilter/patch-o-matic/submitted/81_ipt_unclean-tcp-flag-table.patch
that has already been submitted for kernel inclusion?

Quoted 2 lines

Regards,
Willy

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#2 lore raw
Quoted 3 lines · Harald Welte

Erm, are you aware of the
http://cvs.netfilter.org/netfilter/patch-o-matic/submitted/81_ipt_unclean-tcp-flag-table.patch
that has already been submitted for kernel inclusion?

Yes I am, but this one is against ipt_unclean while the one I just sent does the same to ip_conntrack_proto_tcp after tcp_window_tracking. So the one you point to is used with "-m unclean", while mine is used on every TCP packet which goes through the conntrack code. But I agree it's exactly the same (should we merge them in some ways ? I don't really think so). Where do you believed I stole the code ? :-)

BTW, I've finally recompiled and tested it now. It's running fine, but seems to exhibit no performance gain at all (OK, on average, mesured hits/s seem to be less than 0.1% higher with the patch). I cannot decently attribute this to the code, but mainly to measuring errors. So I'd say that it doesn't change things for this type of machine machine (P3/800).

Willy