Skip to contentSkip to conversation

: 1st step to remove skb_linearize() in ip6_tables.c and optimization

PATCH In review Yasuyuki Kozakai 7 participants 29 messages Jun 24, 2004 – Aug 9, 2004

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : 1st step to remove skb_linearize() in ip6_tables.c and optimization

Hi, folks,

In the current kernel, skb is linearized by skb_linearize() in ip6_tables.c. I suggest removing this, and this patch is the 1st step to do it.

To remove skb_linearize(), this patch changes the API of match() like ip_tables.h

int (*match)(const struct sk_buff *skb,
const struct net_device *in, const struct net_device *out, const void *matchinfo, int offset,
unsigned int protoff, int *hotdrop);

"protoff" is the offset of transport protocol header from skb->data. match modules can get the transport protocol header without skipping IPv6 extension headers.

This patch also changes

- ip6_packet_match(), tcp_match(), udp_match(), icmp_match() in ip6_tables.c are changed to follow the above API.

- In all match module, the only arguments of match() are changed.

This patch doesn't remove skb_linearize() yet since more changes are needed to every match modules. After all modules are changed, we'll be able to just remove skb_linearize().

Moreover, I optimize and ip6_packet_match() in this patch. In this current kernel, IPv6 extension headers are skipped many times since ip6t_do_table() calls ip6_packet_match() per filtering rule.

This patch changes this behavior so that IPv6 extension headers are skipped at once in ip6t_do_table() unless IP6T_CONTINUE is returned from target.

I know that this optimization is not related with removing skb_linearize(). But I don't want to change ip6_packet_match() many time.

If no objections and no bugs, I want this patch to be applied to mainline kernel.

comments ?

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

include/linux/netfilter_ipv6/ip6_tables.h +1 -2
diff -Nur linux-2.6.7/include/linux/netfilter_ipv6/ip6_tables.h linux-2.6.7-ip6tables/include/linux/netfilter_ipv6/ip6_tables.h
@@ -361,8 +361,7 @@
 		     const struct net_device *out,
 		     const void *matchinfo,
 		     int offset,
-		     const void *hdr,
-		     u_int16_t datalen,
+		     unsigned int protoff,
 		     int *hotdrop);
 
 	/* Called when user tries to insert an entry of this type. */
net/ipv6/netfilter/ip6_tables.c +121 -129

401 lines not on this page · raw message

net/ipv6/netfilter/ip6t_ah.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_ah.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_ah.c
@@ -45,8 +45,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *protohdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
        struct ip_auth_hdr *ah = NULL;
net/ipv6/netfilter/ip6t_dst.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_dst.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_dst.c
@@ -60,8 +60,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *protohdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
        struct ipv6_opt_hdr *optsh = NULL;
net/ipv6/netfilter/ip6t_esp.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_esp.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_esp.c
@@ -45,8 +45,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *protohdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
 	struct ip_esp_hdr *esp = NULL;
net/ipv6/netfilter/ip6t_eui64.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_eui64.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_eui64.c
@@ -24,8 +24,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *hdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
 
net/ipv6/netfilter/ip6t_frag.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_frag.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_frag.c
@@ -70,8 +70,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *protohdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
        struct fraghdr *frag = NULL;
net/ipv6/netfilter/ip6t_hbh.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_hbh.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_hbh.c
@@ -59,8 +59,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *protohdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
        struct ipv6_opt_hdr *optsh = NULL;
net/ipv6/netfilter/ip6t_hl.c +1 -1
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_hl.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_hl.c
@@ -20,7 +20,7 @@
 
 static int match(const struct sk_buff *skb, const struct net_device *in,
 		 const struct net_device *out, const void *matchinfo,
-		 int offset, const void *hdr, u_int16_t datalen,
+		 int offset, unsigned int protoff,
 		 int *hotdrop)
 {
 	const struct ip6t_hl_info *info = matchinfo;
net/ipv6/netfilter/ip6t_ipv6header.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_ipv6header.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_ipv6header.c
@@ -31,8 +31,7 @@
 		 const struct net_device *out,
 		 const void *matchinfo,
 		 int offset,
-		 const void *protohdr,
-		 u_int16_t datalen,
+		 unsigned int protoff,
 		 int *hotdrop)
 {
 	const struct ip6t_ipv6header_info *info = matchinfo;
net/ipv6/netfilter/ip6t_length.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_length.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_length.c
@@ -23,8 +23,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *hdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
 	const struct ip6t_length_info *info = matchinfo;
net/ipv6/netfilter/ip6t_limit.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_limit.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_limit.c
@@ -57,8 +57,7 @@
 		const struct net_device *out,
 		const void *matchinfo,
 		int offset,
-		const void *hdr,
-		u_int16_t datalen,
+		unsigned int protoff,
 		int *hotdrop)
 {
 	struct ip6t_rateinfo *r = ((struct ip6t_rateinfo *)matchinfo)->master;
net/ipv6/netfilter/ip6t_mac.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_mac.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_mac.c
@@ -25,8 +25,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *hdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
     const struct ip6t_mac_info *info = matchinfo;
net/ipv6/netfilter/ip6t_mark.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_mark.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_mark.c
@@ -24,8 +24,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *hdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
 	const struct ip6t_mark_info *info = matchinfo;
net/ipv6/netfilter/ip6t_multiport.c +3 -4
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_multiport.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_multiport.c
@@ -53,15 +53,14 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *hdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
-	const struct udphdr *udp = hdr;
+	const struct udphdr *udp = (const struct udphdr *)(skb->data + protoff);
 	const struct ip6t_multiport *multiinfo = matchinfo;
 
 	/* Must be big enough to read ports. */
-	if (offset == 0 && datalen < sizeof(struct udphdr)) {
+	if (offset == 0 && skb->len - protoff < sizeof(struct udphdr)) {
 		/* We've been asked to examine this packet, and we
 		   can't.  Hence, no choice but to drop. */
 			duprintf("ip6t_multiport:"
net/ipv6/netfilter/ip6t_owner.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_owner.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_owner.c
@@ -92,8 +92,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *hdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
 	const struct ip6t_owner_info *info = matchinfo;
net/ipv6/netfilter/ip6t_rt.c +1 -2
diff -Nur linux-2.6.7/net/ipv6/netfilter/ip6t_rt.c linux-2.6.7-ip6tables/net/ipv6/netfilter/ip6t_rt.c
@@ -47,8 +47,7 @@
       const struct net_device *out,
       const void *matchinfo,
       int offset,
-      const void *protohdr,
-      u_int16_t datalen,
+      unsigned int protoff,
       int *hotdrop)
 {
        struct ipv6_rt_hdr *route = NULL;
#1 lore raw

Hi Yasuyuki,

you saved my life :)
When I tried to do the same a was not able to track behaviour of skb->nh. (There was some grey point, like when the packet leaves the machine over the tunnel interface.)

Quoted 3 lines

"protoff" is the offset of transport protocol header from skb->data. match modules can get the transport protocol header without skipping IPv6 extension headers.

(Its OK, since we have some code which uses the extension headers.)

Quoted 1 line

- In all match module, the only arguments of match() are changed.

I think it is not a problem since the ip6tables - as I know - not a 'core' product. We can break the compatibility / usability of these extensions. (And it will push us to fix the problems and sync the matches with the IPv4 parts.)
Could you add an #error string right after the definitions in the matches?
In this case the compilation of the matches will cause strange error messages - and someone have to answer all of them. An error message should help a little bit. (Eg.: Known error point. This part is under reconstruction. Please, be patient. etc.)

Many thanks again,

Best regards,

Andras

Signature · 1 line
Andras Kis-Szabo <[email protected]>
#2 lore raw

Hi, Andras,

From: Andras Kis-Szabo <[email protected]>
Date: Thu, 24 Jun 2004 10:13:50 +0200

Quoted 3 lines

Hi Yasuyuki,

you saved my life :)

I'm glad.

Quoted 3 lines

When I tried to do the same a was not able to track behaviour of skb->nh. (There was some grey point, like when the packet leaves the machine over the tunnel interface.)

Tunnel interfaces reset nh.raw to new address of network protocol header. Otherwide kernel can be down in many parts of IPv6 stack.

BTW, if I remember correctly, you also tryed to rewrite match modules which find extension headers. Have you already finished ? If so, you'll save my life, too :)

Quoted 8 lines
Quoted 1 line

- In all match module, the only arguments of match() are changed.

I think it is not a problem since the ip6tables - as I know - not a 'core' product. We can break the compatibility / usability of these extensions. (And it will push us to fix the problems and sync the matches with the IPv4 parts.)
Could you add an #error string right after the definitions in the matches?

Quoted 4 lines

In this case the compilation of the matches will cause strange error messages - and someone have to answer all of them. An error message should help a little bit. (Eg.: Known error point. This part is under reconstruction. Please, be patient. etc.)

my patch doesn't remove skb_linearize() and I'll keep it until all IPv6 modules in main kernel has changed. and if match module uses old API, compilation errors are occurred and user can notice and report that. Then I think adding #error string to match module is not needed.

But I'm reminded by you that target modules doesn't cause compiling error.

OK, I try to solve this problem... But how same problem was managed when removing skb_linearize() from "ip_tables.c" ? Maybe the order of arguments of target() were changed ?

Anyone knows about that ?

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#3 lore raw
Quoted 3 lines · Yasuyuki Kozakai

OK, I try to solve this problem... But how same problem was managed when removing skb_linearize() from "ip_tables.c" ? Maybe the order of arguments of target() were changed ?

Exactly!

/* Registration hooks for targets. */
struct ipt_target
{
[...]
/* Returns verdict. Argument order changed since 2.4, as this
must now handle non-linear skbs, using skb_copy_bits and skb_ip_make_writable. */
unsigned int (*target)(struct sk_buff **pskb,
[...]

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#4 lore raw

Jozsef, thank you for your information!

I'll change target() in ip6_tables.h like that.

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

From: Jozsef Kadlecsik <[email protected]>
Subject: Re: [PATCH]: 1st step to remove skb_linearize() in ip6_tables.c and optimization
Date: Thu, 24 Jun 2004 12:24:15 +0200 (CEST)

Quoted 27 lines · Yasuyuki Kozakai
Quoted 4 lines

OK, I try to solve this problem... But how same problem was managed when removing skb_linearize() from "ip_tables.c" ? Maybe the order of arguments of target() were changed ?

Exactly!

/* Registration hooks for targets. */
struct ipt_target
{
[...]
/* Returns verdict. Argument order changed since 2.4, as this
must now handle non-linear skbs, using skb_copy_bits and skb_ip_make_writable. */
unsigned int (*target)(struct sk_buff **pskb,
[...]

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#5 lore raw
In reply to Yasuyuki Kozakai
Quoted 7 lines · Yasuyuki Kozakai

Hi, folks,

In the current kernel, skb is linearized by skb_linearize() in ip6_tables.c. I suggest removing this, and this patch is the 1st step to do it.

To remove skb_linearize(), this patch changes the API of match() like ip_tables.h

I'm not sure the way iptables does it is really the right way. We call skb_copy_bits for anything that needs to be matched after the ip_header. Think of 100 rules matching "-p tcp --dport X". We copy the tcp header 100 times, for a total of 2000 bytes. One call to skb_linearize would most likely be less expensive. I'm thinking about putting the copied protocol header in the control buffer, this would reduce this extensive copying a lot. We could also do some common preprocessing steps in one place, like converting things to host byte order.

Comments anyone ?

Regards
Patrick

#6 lore raw
Quoted 9 lines · Patrick McHardy
Quoted 7 lines · Yasuyuki Kozakai

Hi, folks,

In the current kernel, skb is linearized by skb_linearize() in ip6_tables.c. I suggest removing this, and this patch is the 1st step to do it.

To remove skb_linearize(), this patch changes the API of match() like ip_tables.h

Quoted 8 lines

I'm not sure the way iptables does it is really the right way. We call skb_copy_bits for anything that needs to be matched after the ip_header. Think of 100 rules matching "-p tcp --dport X". We copy the tcp header 100 times, for a total of 2000 bytes. One call to skb_linearize would most likely be less expensive. I'm thinking about putting the copied protocol header in the control buffer, this would reduce this extensive copying a lot. We could also do some common preprocessing steps in one place, like converting things to host byte order.

Wouldn't an skb_ip[6]_make_headers_writable function based on skb_ip_make_writable more optimal?

The function would make the full IP[v6], transport protocol (and IPv6 option protocol) headers writable and could be called from nf_hook_slow.

Opinions?

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#7 lore raw

Hi,

From: Jozsef Kadlecsik <[email protected]>
Date: Thu, 24 Jun 2004 13:50:27 +0200 (CEST)

Quoted 11 lines · Patrick McHardy
Quoted 9 lines

I'm not sure the way iptables does it is really the right way. We call skb_copy_bits for anything that needs to be matched after the ip_header. Think of 100 rules matching "-p tcp --dport X". We copy the tcp header 100 times, for a total of 2000 bytes. One call to skb_linearize would most likely be less expensive. I'm thinking about putting the copied protocol header in the control buffer, this would reduce this extensive copying a lot. We could also do some common preprocessing steps in one place, like converting things to host byte order.

Quoted 2 lines

Wouldn't an skb_ip[6]_make_headers_writable function based on skb_ip_make_writable more optimal?

I think so in the case of IPv4.

Quoted 4 lines

The function would make the full IP[v6], transport protocol (and IPv6 option protocol) headers writable and could be called from nf_hook_slow.

Opinions?

skb is not required to be writable. if skb_headlen(skb) is greater than the length required to be linear, copying is not needed even though skb is shared or clone. And one issue is that the argument "skb" in match() is const. Can we really remove it ?

And I'm not sure about IPv6. At least, I don't like to skip IPv6 extension header in skb_ip6_make_writable() to figure out that skb should be copied or pulled.

Regards,
-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

Quoted 8 lines

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#8 lore raw

Hi,

Quoted 17 lines · Yasuyuki Kozakai

From: Jozsef Kadlecsik <[email protected]>
Date: Thu, 24 Jun 2004 13:50:27 +0200 (CEST)

Quoted 11 lines · Patrick McHardy
Quoted 9 lines

I'm not sure the way iptables does it is really the right way. We call skb_copy_bits for anything that needs to be matched after the ip_header. Think of 100 rules matching "-p tcp --dport X". We copy the tcp header 100 times, for a total of 2000 bytes. One call to skb_linearize would most likely be less expensive. I'm thinking about putting the copied protocol header in the control buffer, this would reduce this extensive copying a lot. We could also do some common preprocessing steps in one place, like converting things to host byte order.

Quoted 2 lines

Wouldn't an skb_ip[6]_make_headers_writable function based on skb_ip_make_writable more optimal?

Quoted 8 lines

I think so in the case of IPv4.

Quoted 2 lines

The function would make the full IP[v6], transport protocol (and IPv6 option protocol) headers writable and could be called from nf_hook_slow.

skb is not required to be writable. if skb_headlen(skb) is greater than the length required to be linear, copying is not needed even though skb is shared or clone.

Your're of course right. For matching we don't really need the headers to be writable, linear is just enough. However mangle and NAT requires writable headers because typically we modify the headers only (let's not consider the conntrack/NAT protocol helpers).

Quoted 2 lines

And one issue is that the argument "skb" in match() is const. Can we really remove it ?

I'm confused: why should we remove it? The function would be called directly from nf_hook_slow and not from the matches.

Quoted 3 lines

And I'm not sure about IPv6. At least, I don't like to skip IPv6 extension header in skb_ip6_make_writable() to figure out that skb should be copied or pulled.

I'm unfamiliar with the deep magic behind IPv6: I simply assumed that linear headers up to the transport protocol headers in IPv6 would just be fine and enough for the matches. That'd require an ugly processing header by header in skb_ip6_make_headers_linear ;-), I think.

I'll try to cook some code to see how it'd come out.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#9 lore raw

Re: (usagi-core 18584) Re: [PATCH]: 1st step to remove skb_linearize() in ip6_tables.c and optimization

In article <[email protected]> (at Thu, 24 Jun 2004 15:25:54 +0200 (CEST)), Jozsef Kadlecsik <[email protected]> says:

Quoted 4 lines

I'm unfamiliar with the deep magic behind IPv6: I simply assumed that linear headers up to the transport protocol headers in IPv6 would just be fine and enough for the matches. That'd require an ugly processing header by header in skb_ip6_make_headers_linear ;-), I think.

It is full of beauty to process header by header. Skip processing extension header is BAD idea. You cannot (and shouldn't) see next header (or payload) until you really process the current header.

--yoshfuji

#10 lore raw
In reply to Jozsef Kadlecsik

Hi,

From: Jozsef Kadlecsik <[email protected]>
Date: Thu, 24 Jun 2004 15:25:54 +0200 (CEST)

Quoted 12 lines
Quoted 6 lines
Quoted 2 lines

The function would make the full IP[v6], transport protocol (and IPv6 option protocol) headers writable and could be called from nf_hook_slow.

skb is not required to be writable. if skb_headlen(skb) is greater than the length required to be linear, copying is not needed even though skb is shared or clone.

Your're of course right. For matching we don't really need the headers to be writable, linear is just enough. However mangle and NAT requires writable headers because typically we modify the headers only (let's not consider the conntrack/NAT protocol helpers).

Quoted 5 lines
Quoted 2 lines

And one issue is that the argument "skb" in match() is const. Can we really remove it ?

I'm confused: why should we remove it? The function would be called directly from nf_hook_slow and not from the matches.

Sorry, I misstook. But why nf_hook_slow() ? I think users expect that skb is not linearized if ip_tables.ko is not loaded.
Then how about ipt_do_table() or hooks in iptable_filter.c ?

Quoted 8 lines
Quoted 3 lines

And I'm not sure about IPv6. At least, I don't like to skip IPv6 extension header in skb_ip6_make_writable() to figure out that skb should be copied or pulled.

I'm unfamiliar with the deep magic behind IPv6: I simply assumed that linear headers up to the transport protocol headers in IPv6 would just be fine and enough for the matches. That'd require an ugly processing header by header in skb_ip6_make_headers_linear ;-), I think.

In the first place, I don't understand why skb_ip_make_writable() checks required length is up to the transport protocol header or not.
If skb_ip6_make_headers_linear() will check that too, extension headers are skipped many times. That's why I don't like it. I want to reduce the number of times of skipping extension headers as possible.

Regards,

Quoted 9 lines

I'll try to cook some code to see how it'd come out.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#11 lore raw
Quoted 8 lines · Yasuyuki Kozakai
Quoted 3 lines

I'm confused: why should we remove it? The function would be called directly from nf_hook_slow and not from the matches.

Sorry, I misstook. But why nf_hook_slow() ? I think users expect that skb is not linearized if ip_tables.ko is not loaded.
Then how about ipt_do_table() or hooks in iptable_filter.c ?

It would be nice if the skb_copy_bits calls in conntrack and nat copying only the transport protocol headers could be removed as well. We could register for the PREROUTING hook with highest priority and linearize the headers when a global flag is set. ip_conntrack and ip_tables would both set this flag.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

And I'm not sure about IPv6. At least, I don't like to skip IPv6 extension header in skb_ip6_make_writable() to figure out that skb should be copied or pulled.

I'm unfamiliar with the deep magic behind IPv6: I simply assumed that linear headers up to the transport protocol headers in IPv6 would just be fine and enough for the matches. That'd require an ugly processing header by header in skb_ip6_make_headers_linear ;-), I think.

Quoted 6 lines

In the first place, I don't understand why skb_ip_make_writable() checks required length is up to the transport protocol header or not.
If skb_ip6_make_headers_linear() will check that too, extension headers are skipped many times. That's why I don't like it. I want to reduce the number of times of skipping extension headers as possible.

I also don't understand this, I thought the linear part of an skb is skb_headlen(skb), why the distinction between different protocols ?

Regards
Patrick

#12 lore raw

Hi,

From: Patrick McHardy <[email protected]>
Date: Thu, 24 Jun 2004 18:50:32 +0200

Quoted 11 lines
Quoted 5 lines · Yasuyuki Kozakai

Sorry, I misstook. But why nf_hook_slow() ? I think users expect that skb is not linearized if ip_tables.ko is not loaded.
Then how about ipt_do_table() or hooks in iptable_filter.c ?

It would be nice if the skb_copy_bits calls in conntrack and nat copying only the transport protocol headers could be removed as well. We could register for the PREROUTING hook with highest priority and linearize the headers when a global flag is set. ip_conntrack and ip_tables would both set this flag.

flag is good idea. But in the case of IPv6, the fact remains that skipping IPv6 ext headers are needed to find transport protocol.

Quoted 8 lines
Quoted 6 lines

In the first place, I don't understand why skb_ip_make_writable() checks required length is up to the transport protocol header or not.
If skb_ip6_make_headers_linear() will check that too, extension headers are skipped many times. That's why I don't like it. I want to reduce the number of times of skipping extension headers as possible.

I also don't understand this, I thought the linear part of an skb is skb_headlen(skb), why the distinction between different protocols ?

It looks like this function was introduced in 2.5.70 by Rusty Russel.

Anyway, I think the function which requires to skip IPv6 extension headers should not be called in nf_hook_slow() since skipping is occurred 2 times
- nf_hook_slow() and ip6_do_table(). It's better to linearize header by header while finding transport protocol header in ip6_do_table().

Quoted 2 lines

Regards
Patrick

Regards,
-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#13 lore raw

Hi,

Quoted 4 lines · Yasuyuki Kozakai
Quoted 4 lines
Quoted 4 lines

Sorry, I misstook. But why nf_hook_slow() ? I think users expect that skb is not linearized if ip_tables.ko is not loaded.
Then how about ipt_do_table() or hooks in iptable_filter.c ?

Quoted 8 lines
Quoted 5 lines

It would be nice if the skb_copy_bits calls in conntrack and nat copying only the transport protocol headers could be removed as well. We could register for the PREROUTING hook with highest priority and linearize the headers when a global flag is set. ip_conntrack and ip_tables would both set this flag.

flag is good idea. But in the case of IPv6, the fact remains that skipping IPv6 ext headers are needed to find transport protocol.

- conntrack requires linearized protocol headers
- with the TCP window tracking code it means the complete TCP header, including the options due to the SACK support
- nat requires writable protocol headers, including the TCP options due to the SACK support
- raw/filter tables require linearized protocol headers in general (we can safely assume port matching rules :-)
- mark table requires writable protocol headers if we mangle the packet headers

So I think when any component of netfilter is enabled, we can assume that at least linearized protocol headers are required. Therefore I suggested to add such a function to nf_hook_slow.

We cannot add (back) a general skb_linearize: that was a constant complain against netfilter from performance reason and was therefore removed.

In order to spare skipping again and again the extension headers in IPv6, wouldn't it make sense to add a new argument to the hook functions which argument would point then to the protocol header?

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#14 lore raw

Hi,

From: Jozsef Kadlecsik <[email protected]>
Date: Fri, 25 Jun 2004 12:01:37 +0200 (CEST)

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Sorry, I misstook. But why nf_hook_slow() ? I think users expect that skb is not linearized if ip_tables.ko is not loaded.
Then how about ipt_do_table() or hooks in iptable_filter.c ?

Quoted 8 lines
Quoted 5 lines

It would be nice if the skb_copy_bits calls in conntrack and nat copying only the transport protocol headers could be removed as well. We could register for the PREROUTING hook with highest priority and linearize the headers when a global flag is set. ip_conntrack and ip_tables would both set this flag.

flag is good idea. But in the case of IPv6, the fact remains that skipping IPv6 ext headers are needed to find transport protocol.

Quoted 9 lines

- conntrack requires linearized protocol headers
- with the TCP window tracking code it means the complete TCP header, including the options due to the SACK support
- nat requires writable protocol headers, including the TCP options due to the SACK support
- raw/filter tables require linearized protocol headers in general (we can safely assume port matching rules :-)
- mark table requires writable protocol headers if we mangle the packet headers

That's right at this point.

Quoted 3 lines

So I think when any component of netfilter is enabled, we can assume that at least linearized protocol headers are required. Therefore I suggested to add such a function to nf_hook_slow.

As you said, I think nf_hook_slow is best point for IPv4 modules. And I found that the issue of IPv6 can be solved by adding flag to the argument nf_hook_register(). This flag tell whether request to linearize or not.

And I have a (maybe) last issue. Are we allowed to add protocol dependent codes to nf_hook_slow() ?

Quoted 2 lines

We cannot add (back) a general skb_linearize: that was a constant complain against netfilter from performance reason and was therefore removed.

I think so, too.

Quoted 3 lines

In order to spare skipping again and again the extension headers in IPv6, wouldn't it make sense to add a new argument to the hook functions which argument would point then to the protocol header?

If so, the transport protocol number is needed, too. You can find the transport protocol number at Next Header field in previous header.

Quoted 7 lines

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary

Best regards,
-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#15 lore raw
In reply to Jozsef Kadlecsik
Quoted 9 lines · Jozsef Kadlecsik

- conntrack requires linearized protocol headers
- with the TCP window tracking code it means the complete TCP header, including the options due to the SACK support
- nat requires writable protocol headers, including the TCP options due to the SACK support
- raw/filter tables require linearized protocol headers in general (we can safely assume port matching rules :-)
- mark table requires writable protocol headers if we mangle the packet headers

yes, it's time to implement my old idea of a function like
skb_linearize_partial(skb, len)

This could then be called by a skb_linearize_l4() function, that would linearize IP header, ip options, tcp header.

Ideally a hook-registering function should specify what it needs to have linearized. netfilter.c would then calculate the per-hook maximum and call the linearize function with that maximum.

Quoted 3 lines

So I think when any component of netfilter is enabled, we can assume that at least linearized protocol headers are required. Therefore I suggested to add such a function to nf_hook_slow.

yes, I agree. Does anybody want to hack up a patch? The most interesting part will be testing of that feature...

Quoted 2 lines

Best regards,
Jozsef

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
#16 lore raw

Hi,

I got time to implement your idea. How about this ? (not tested)

BTW this code copies skb in the worst case. While implementing I got a idea about this issue. How about introducing the function like that

	struct tcphdr hdr;
	struct tcphdr *tcph

	tcph = skb_get_bits(skb, &hdr, skb->nh.iph->ihl*4, sizeof(hdr));

If skb is neither shared nor cloned, this function linearize up to tcp header and returns the pointer to tcp header in skb.

Otherwise, copies tcp header to "hdr" and return the pointer to it. If error, return NULL.

This function will also eliminate the needs to linearization at nf_hook_slow() but it is needed to modify all modules which read contents (but not write).

Do you think which is better ?

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

From: Harald Welte <[email protected]>
Date: Wed, 21 Jul 2004 17:36:54 -0400

Quoted 26 lines · Jozsef Kadlecsik
Quoted 10 lines

- conntrack requires linearized protocol headers
- with the TCP window tracking code it means the complete TCP header, including the options due to the SACK support
- nat requires writable protocol headers, including the TCP options due to the SACK support
- raw/filter tables require linearized protocol headers in general (we can safely assume port matching rules :-)
- mark table requires writable protocol headers if we mangle the packet headers

yes, it's time to implement my old idea of a function like
skb_linearize_partial(skb, len)

This could then be called by a skb_linearize_l4() function, that would linearize IP header, ip options, tcp header.

Ideally a hook-registering function should specify what it needs to have linearized. netfilter.c would then calculate the per-hook maximum and call the linearize function with that maximum.

Quoted 3 lines

So I think when any component of netfilter is enabled, we can assume that at least linearized protocol headers are required. Therefore I suggested to add such a function to nf_hook_slow.

Quoted 12 lines

yes, I agree. Does anybody want to hack up a patch? The most interesting part will be testing of that feature...

Quoted 2 lines

Best regards,
Jozsef

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
include/linux/netfilter.h +4 -0
diff -Nurp -X dontdiff linux-2.6.7/include/linux/netfilter.h linux-2.6.7-linearize/include/linux/netfilter.h
@@ -53,8 +53,12 @@ struct nf_hook_ops
 	int hooknum;
 	/* Hooks are ordered in ascending priority. */
 	int priority;
+	int linearize_mode;
+	int linearize_layer;
 };
 
+#define NF_LINEARIZE_NONE	0
+
 struct nf_sockopt_ops
 {
 	struct list_head list;
include/linux/netfilter_ipv4.h +23 -0
diff -Nurp -X dontdiff linux-2.6.7/include/linux/netfilter_ipv4.h linux-2.6.7-linearize/include/linux/netfilter_ipv4.h
@@ -78,6 +78,29 @@ void nf_debug_ip_loopback_xmit(struct sk
 void nf_debug_ip_finish_output2(struct sk_buff *skb);
 #endif /*CONFIG_NETFILTER_DEBUG*/
 
+/* linearize skb up to required header */
+extern int ip_linearize_headers(struct sk_buff **pskb, int mode, int layer);
+
+/* mode */
+
+/* not linearize */
+#define NF_IP_LINEARIZE_NONE			NF_LINEARIZE_NONE
+/* make skb readable without copying if possible */
+#define NF_IP_LINEARIZE_READABLE_NO_COPY	100
+/* make skb readable. skb is copied if needed */
+#define NF_IP_LINEARIZE_READABLE		200
+/* make skb readable. skb is copied if needed */
+#define NF_IP_LINEARIZE_WRITABLE		300
+/* linearize skb */
+#define NF_IP_LINEARIZE_ALL			65534
+
+/* layer */
+/* up to IP header */
+#define NF_IP_LINEARIZE_IPHDR			100
+/* up to transport header */
+#define NF_IP_LINEARIZE_TRANSPORT		200
+
+
 extern int ip_route_me_harder(struct sk_buff **pskb);
 
 /* Call this before modifying an existing IP packet: ensures it is
net/core/netfilter.c +67 -0

107 lines not on this page · raw message

net/ipv4/netfilter/Makefile +2 -0
diff -Nurp -X dontdiff linux-2.6.7/net/ipv4/netfilter/Makefile linux-2.6.7-linearize/net/ipv4/netfilter/Makefile
@@ -96,3 +96,5 @@ obj-$(CONFIG_IP_NF_COMPAT_IPCHAINS) += i
 obj-$(CONFIG_IP_NF_COMPAT_IPFWADM) += ipfwadm.o
 
 obj-$(CONFIG_IP_NF_QUEUE) += ip_queue.o
+
+obj-y += ip_linearize.o
net/ipv4/netfilter/ip_linearize.c +87 -0

87 lines not on this page · raw message

#17 lore raw
Quoted 7 lines · Yasuyuki Kozakai

I got time to implement your idea. How about this ? (not tested)

	struct tcphdr hdr;
	struct tcphdr *tcph

	tcph = skb_get_bits(skb, &hdr, skb->nh.iph->ihl*4, sizeof(hdr));
Quoted 5 lines

If skb is neither shared nor cloned, this function linearize up to tcp header and returns the pointer to tcp header in skb.

Otherwise, copies tcp header to "hdr" and return the pointer to it. If error, return NULL.

This sounds like a very sane approach to me.

Since we don't have stable API's in 2.6.x anymore (kernel summit decision), we could even put this in our pending queue of patches for something like 2.6.10/2.6.11

What does eveybody else think? Comments?

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
#18 lore raw
Quoted 18 lines · Harald Welte
Quoted 7 lines · Yasuyuki Kozakai

I got time to implement your idea. How about this ? (not tested)

	struct tcphdr hdr;
	struct tcphdr *tcph

	tcph = skb_get_bits(skb, &hdr, skb->nh.iph->ihl*4, sizeof(hdr));
Quoted 7 lines

If skb is neither shared nor cloned, this function linearize up to tcp header and returns the pointer to tcp header in skb.

Otherwise, copies tcp header to "hdr" and return the pointer to it. If error, return NULL.

Quoted 9 lines

This sounds like a very sane approach to me.

Since we don't have stable API's in 2.6.x anymore (kernel summit decision), we could even put this in our pending queue of patches for something like 2.6.10/2.6.11

What does eveybody else think? Comments?

The number of copies will still depend on the ruleset with non-linear skbs. skb_linearize_partial sounds like a much better idea to me.

Regards
Patrick

#19 lore raw
Quoted 5 lines · Patrick McHardy
Quoted 5 lines
Quoted 5 lines
	struct tcphdr hdr;
	struct tcphdr *tcph

	tcph = skb_get_bits(skb, &hdr, skb->nh.iph->ihl*4, sizeof(hdr));
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

If skb is neither shared nor cloned, this function linearize up to tcp header and returns the pointer to tcp header in skb. Otherwise, copies tcp header to "hdr" and return the pointer to it. If error, return NULL.

The number of copies will still depend on the ruleset with non-linear skbs. skb_linearize_partial sounds like a much better idea to me.

Oh yes, indeed. I somehow mis-interpreted Yasuyiki's approach. We should linearize with the first call, so that every 2nd, 3rd, ... call do nothing but immediately return.

Quoted 2 lines

Regards
Patrick

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
#20 lore raw

Hi,

From: Harald Welte <[email protected]>
Date: Sun, 1 Aug 2004 20:11:50 +0200

Quoted 12 lines · Patrick McHardy
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
	struct tcphdr hdr;
	struct tcphdr *tcph

	tcph = skb_get_bits(skb, &hdr, skb->nh.iph->ihl*4, sizeof(hdr));

If skb is neither shared nor cloned, this function linearize up to tcp header and returns the pointer to tcp header in skb. Otherwise, copies tcp header to "hdr" and return the pointer to it. If error, return NULL.

Quoted 6 lines
Quoted 2 lines

The number of copies will still depend on the ruleset with non-linear skbs. skb_linearize_partial sounds like a much better idea to me.

Oh yes, indeed. I somehow mis-interpreted Yasuyiki's approach. We should linearize with the first call, so that every 2nd, 3rd, ... call do nothing but immediately return.

Oh, sorry. I misstook about pskb_may_pull(). And please forget my patch because it's inefficient.

Quoted 10 lines
Quoted 3 lines

Regards
Patrick

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
#21 lore raw

Hi, all

I rewrote the codes which partially linearize skb before executing hook functions. This solves some new issues. And I got good results with ordinary situations.

The details are following. sorry for long message.

1. Changes

The changes are following.
- If hook function rearranges skb, skb may be fragmented. (e.g. ip_conntrack_defrag()) Then I introduce the flag NF_LIN_MAY_FRAG. If this flag is set on nf_hook_ops->lin_flag, check and linearization are tried once more.
- ip_linearize_headers(), which partially linearizes skb up to the transport header, doesn't linearize if IPv4 packet is fragmented and isn't 1st fragment.
- deleted writable mode because I'm not sure it's efficient or not.
- My understanding about pskb_may_pull() was wrong in the previous patch. In this patch, skb is copied if skb_shared() is true. Otherwise, pskb_may_pull() is used.

2. Tests
I tested in 2 situations with 1st and 2nd patches attached to this mail.

2.1 On router
I applied the patches to kernel on a router, inserted 2000 non-matching rules
for tcp destination port to FORWARD chain on it, and pass 500MB stream from one side to another. The result is

$ iperf -c 192.168.1.2 -n 500M
------------------------------------------------------------
Client connecting to 192.168.1.2, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.2.2 port 32830 connected with 192.168.1.2 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-130.9 sec   500 MBytes  32.0 Mbits/sec

In the case of vanilla kernel,

$ iperf -c 192.168.1.2 -n 500M
------------------------------------------------------------
Client connecting to 192.168.1.2, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.2.2 port 32834 connected with 192.168.1.2 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-199.1 sec   500 MBytes  21.1 Mbits/sec

The parformance is improbed in this case.

2.2 On host
I applied the patches to kernel on a host, inserted 1000 non-matching rules
for tcp destination port to INPUT chain on it, and pass 500MB data from one host to another. These hosts are on same link. The result is

$ iperf -c 192.168.1.1 -n 500M
------------------------------------------------------------
Client connecting to 192.168.1.1, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.1.2 port 32770 connected with 192.168.1.1 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-117.6 sec   500 MBytes  35.7 Mbits/sec

In the case of vanilla kernel,

$ iperf -c 192.168.1.1 -n 500M
------------------------------------------------------------
Client connecting to 192.168.1.1, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.1.2 port 32773 connected with 192.168.1.1 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-132.2 sec   500 MBytes  31.7 Mbits/sec

The parformance is improbed in this case, too.

3. Debugging

At least, I saw this patch works well with skb which is ...
- fragmented at TCP header.
- already linearized up to required header (and may be cloned or shared).
- fragmented IPv4 packet.

and __calc_lin_max_layer(), which calculates the highest layer header required to linearize, works well in the case that ip_conntrack.ko and iptable_filter.ko are inserted to kernel. But I didn't test with SMP machine.

4. Issues

4.1 some elimination
In the current, the header required to linearize is only transport header. How about the other layer ? If we need only transport header, we can eliminate
- some "if" from ip_linearize_headers()
- calculation of the highest header to linearize

4.2 writable mode
"writable mode" is useful ? In the current, the linearization assures that skb is readable but not writable. If nat/mangle modules decide to mangle packet but skb isn't writable, skb will be copied by skb_make_writable(). But I don't know part of skb may be copied twice or not. Various situations are needed to test.

Regards,

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

include/linux/netfilter.h +11 -0
diff -X dontdiff -Nurp linux-2.6.8-rc3/include/linux/netfilter.h linux-2.6.8-rc3-linearize/include/linux/netfilter.h
@@ -46,6 +46,9 @@ typedef unsigned int nf_hookfn(unsigned 
 struct nf_hook_ops
 {
 	struct list_head list;
+	/* max protocol header required to linearize before executing hook
+	   functions. */
+	unsigned int max_lin_layer;
 
 	/* User fills in from here down. */
 	nf_hookfn *hook;
@@ -54,8 +57,14 @@ struct nf_hook_ops
 	int hooknum;
 	/* Hooks are ordered in ascending priority. */
 	int priority;
+	/* protocol header required to linearize */
+	unsigned int lin_layer;
+	unsigned int lin_flags;
 };
 
+/* for lin_flags */
+#define NF_LIN_MAY_FRAG	0x0001 /* skb may be fragmented after executing hook */
+
 struct nf_sockopt_ops
 {
 	struct list_head list;
@@ -187,6 +196,8 @@ extern void nf_dump_skb(int pf, struct s
 /* FIXME: Before cache is ever used, this must be implemented for real. */
 extern void nf_invalidate_cache(int pf);
 
+extern int skb_make_readable(struct sk_buff **pskb, unsigned int readable_len);
+
 #else /* !CONFIG_NETFILTER */
 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
 #endif /*CONFIG_NETFILTER*/
include/linux/netfilter_ipv4.h +9 -0
diff -X dontdiff -Nurp linux-2.6.8-rc3/include/linux/netfilter_ipv4.h linux-2.6.8-rc3-linearize/include/linux/netfilter_ipv4.h
@@ -85,6 +85,15 @@ extern int ip_route_me_harder(struct sk_
    Returns true or false. */
 extern int skb_ip_make_writable(struct sk_buff **pskb,
 				unsigned int writable_len);
+
+/* Header required to linearize */
+/* Network protocol header */
+#define NF_IP_LIN_NET	100
+/* Transport protocol header */
+#define NF_IP_LIN_TRANS	200
+/* Whole of packet */
+#define NF_IP_LIN_ALL	UINT_MAX
+
 #endif /*__KERNEL__*/
 
 #endif /*__LINUX_IP_NETFILTER_H*/
net/core/netfilter.c +76 -0

132 lines not on this page · raw message

net/ipv4/netfilter/Makefile +2 -0
diff -X dontdiff -Nurp linux-2.6.8-rc3/net/ipv4/netfilter/Makefile linux-2.6.8-rc3-linearize/net/ipv4/netfilter/Makefile
@@ -98,3 +98,5 @@ obj-$(CONFIG_IP_NF_COMPAT_IPCHAINS) += i
 obj-$(CONFIG_IP_NF_COMPAT_IPFWADM) += ipfwadm.o
 
 obj-$(CONFIG_IP_NF_QUEUE) += ip_queue.o
+
+obj-y += ip_linearize.o
net/ipv4/netfilter/ip_linearize.c +101 -0

101 lines not on this page · raw message

net/ipv4/netfilter/ip_tables.c +25 -17

129 lines not on this page · raw message

net/ipv4/netfilter/iptable_filter.c +3 -0
diff -X dontdiff -Nurp linux-2.6.8-rc3/net/ipv4/netfilter/iptable_filter.c linux-2.6.8-rc3-linearize/net/ipv4/netfilter/iptable_filter.c
@@ -136,6 +136,7 @@ static struct nf_hook_ops ipt_ops[] = {
 		.pf		= PF_INET,
 		.hooknum	= NF_IP_LOCAL_IN,
 		.priority	= NF_IP_PRI_FILTER,
+		.lin_layer	= NF_IP_LIN_TRANS,
 	},
 	{
 		.hook		= ipt_hook,
@@ -143,6 +144,7 @@ static struct nf_hook_ops ipt_ops[] = {
 		.pf		= PF_INET,
 		.hooknum	= NF_IP_FORWARD,
 		.priority	= NF_IP_PRI_FILTER,
+		.lin_layer	= NF_IP_LIN_TRANS,
 	},
 	{
 		.hook		= ipt_local_out_hook,
@@ -150,6 +152,7 @@ static struct nf_hook_ops ipt_ops[] = {
 		.pf		= PF_INET,
 		.hooknum	= NF_IP_LOCAL_OUT,
 		.priority	= NF_IP_PRI_FILTER,
+		.lin_layer	= NF_IP_LIN_TRANS,
 	},
 };
 
net/ipv4/netfilter/ip_conntrack_standalone.c +5 -0
@@ -263,6 +263,7 @@
 	.pf		= PF_INET,
 	.hooknum	= NF_IP_PRE_ROUTING,
 	.priority	= NF_IP_PRI_CONNTRACK_DEFRAG,
+	.lin_flags	= NF_LIN_MAY_FRAG,
 };
 
 static struct nf_hook_ops ip_conntrack_in_ops = {
@@ -271,6 +272,7 @@
 	.pf		= PF_INET,
 	.hooknum	= NF_IP_PRE_ROUTING,
 	.priority	= NF_IP_PRI_CONNTRACK,
+	.lin_layer	= NF_IP_LIN_TRANS,
 };
 
 static struct nf_hook_ops ip_conntrack_defrag_local_out_ops = {
@@ -279,6 +281,7 @@
 	.pf		= PF_INET,
 	.hooknum	= NF_IP_LOCAL_OUT,
 	.priority	= NF_IP_PRI_CONNTRACK_DEFRAG,
+	.lin_flags	= NF_LIN_MAY_FRAG,
 };
 
 static struct nf_hook_ops ip_conntrack_local_out_ops = {
@@ -287,6 +290,7 @@
 	.pf		= PF_INET,
 	.hooknum	= NF_IP_LOCAL_OUT,
 	.priority	= NF_IP_PRI_CONNTRACK,
+	.lin_layer	= NF_IP_LIN_TRANS,
 };
 
 /* Refragmenter; last chance. */
@@ -296,6 +300,7 @@
 	.pf		= PF_INET,
 	.hooknum	= NF_IP_POST_ROUTING,
 	.priority	= NF_IP_PRI_LAST,
+	.lin_flags	= NF_LIN_MAY_FRAG,
 };
 
 static struct nf_hook_ops ip_conntrack_local_in_ops = {
#22 lore raw

Sorry for my misstakes ...

From: Yasuyuki Kozakai <[email protected]>
Date: Sun, 08 Aug 2004 06:05:35 +0900 (JST)

Quoted 2 lines

2.1 On router
I applied the patches to kernel on a router, inserted 2000 non-matching rules

                                                        ^^^^

This is 1000.

Quoted 2 lines

2.2 On host
I applied the patches to kernel on a host, inserted 1000 non-matching rules

                                                      ^^^^

This is 2000.

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#23 lore raw
In reply to Patrick McHardy
Quoted 7 lines · Patrick McHardy
Quoted 6 lines · Yasuyuki Kozakai

Hi, folks,

In the current kernel, skb is linearized by skb_linearize() in ip6_tables.c.
I suggest removing this, and this patch is the 1st step to do it.

Quoted 5 lines
Quoted 2 lines

To remove skb_linearize(), this patch changes the API of match() like ip_tables.h

I'm not sure the way iptables does it is really the right way. We call skb_copy_bits for anything that needs to be matched after the ip_header.

yes.

Quoted 2 lines

Think of 100 rules matching "-p tcp --dport X". We copy the tcp header 100 times, for a total of 2000 bytes.

but we're talking about local copies to the stack. those copies are 20 byte copies to the cache, and most likely would never have to hit memory at all.

Quoted 5 lines

One call to skb_linearize would most likely be less expensive. I'm thinking about putting the copied protocol header in the control buffer, this would reduce this extensive copying a lot. We could also do some common preprocessing steps in one place, like converting things to host byte order.

yes, it would be only one copy... but to real memory..

Quoted 1 line

Comments anyone ?

Maybe we need some benchmarking on this.

Quoted 2 lines

Regards
Patrick

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
#24 lore raw
Quoted 11 lines · Harald Welte
Quoted 5 lines · Patrick McHardy

One call to skb_linearize would most likely be less expensive. I'm thinking about putting the copied protocol header in the control buffer, this would reduce this extensive copying a lot. We could also do some common preprocessing steps in one place, like converting things to host byte order.

yes, it would be only one copy... but to real memory.. Maybe we need some benchmarking on this.

It may be only to cache, but in case the skb is non-linear skb_copy_bits calls local_bh_disable and kmap_atomic for the paged part, and walks frag_list. I don't expect skb_ip_make_writable to be much faster in all cases, but a new function which would only linearize the transport headers, without copying the entire skb, probably would be. Unfortunately I don't have time to implement and benchmark it right now.

Quoted 4 lines
Quoted 2 lines

Regards
Patrick

#25 lore raw
In reply to Harald Welte
Quoted 9 lines · Harald Welte
Quoted 2 lines · Patrick McHardy

Think of 100 rules matching "-p tcp --dport X". We copy the tcp header 100 times, for a total of 2000 bytes.

but we're talking about local copies to the stack. those copies are 20 byte copies to the cache, and most likely would never have to hit memory at all.

I did some profiling:

1000 non-matching rules for tcp-sport, ~40mbit tcp transfer, about 10 minutes

The top three entries are:

CPU: Athlon, speed 1667.51 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Cycles outside of halt state) with a unit mask of 0x00 (No unit mask) count 100000

samples  %        linenr info                 image name app name                 symbol name
3489310  50.0932  (no location information)   ip_tables.ko ip_tables                ipt_do_table
1405809  20.1821  skbuff.c:858                vmlinux vmlinux                  skb_copy_bits
755945   10.8525  ip_tables.c:1488            ip_tables.ko ip_tables                tcp_match

skb_copy_bits is taking about twice as much cycles as tcp_match and 20% of total cycles.

Regards
Patrick

#26 lore raw
Quoted 2 lines · Patrick McHardy

skb_copy_bits is taking about twice as much cycles as tcp_match and 20% of total cycles.

Well, I am convinced ;)

Quoted 2 lines

Regards
Patrick

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
#27 lore raw
In reply to Patrick McHardy

Hi Patrick,

Quoted 2 lines · Patrick McHardy

skb_copy_bits is taking about twice as much cycles as tcp_match and 20% of total cycles.

AFAIK we are using skb_copy_bits because it walks through fragment list in a safe way, I read an old thread (Harald and DaveM discussion) about this. If I'm not wrong, that's why this was the method selected when conntrack started handling fragments in 2.6 in a different way.

Well, I wonder if we could check if current skb is fragmented (something like skb->data_len!=0), in that case use skb_copy_bits, if not walk through skb payload directly as we use to do in 2.4 and save that memcpy.

regards,
Pablo

#28 lore raw
Quoted 8 lines · Pablo Neira

Hi Patrick,

Quoted 2 lines · Patrick McHardy

skb_copy_bits is taking about twice as much cycles as tcp_match and 20% of total cycles.

AFAIK we are using skb_copy_bits because it walks through fragment list in a safe way, I read an old thread (Harald and DaveM discussion) about this. If I'm not wrong, that's why this was the method selected when conntrack started handling fragments in 2.6 in a different way.

skb_copy_bits does three things: it copies memory from the linear part between skb->head and skb->tail, it copies memory from the paged part in skb->frags[], and it walks skb->frag_list for defragmented packets.

Quoted 2 lines

Well, I wonder if we could check if current skb is fragmented (something like skb->data_len!=0), in that case use skb_copy_bits, if not walk through skb payload directly as we use to do in 2.4 and save that memcpy.

I don't think we should do that, the number of copies would still be dependant on the ruleset. I see two possibilities: We could linearize the transport protocol headers or we could copy it to skb->cb.

Linearizing has three cases:
- data is already linear: nothing to do
- parts of data are in paged part in skb->frags[]: append data of frags to skb->end until we have enough
- parts of data are in frag_list: walk frag_list until enough linear data is in first skb, copy linear data, handle paged data like described above

I don't know if this turns out to be much more efficient than the old call to skb_linearize. Copying to skb->cb is simpler, but the frequent case of already linear data is handled less efficient than with linearizing.

Regards
Patrick