Skip to contentSkip to conversation

hv_netvsc: Use simple parser for IPv4 and v6 headers

PATCH In review Haiyang Zhang 4 participants 7 messages Dec 16, 2015

Subsystems (3)
lore
Share & tools
lore

Haiyang Zhang

author maintainer
#0 lore raw

PATCH 1/1 hv_netvsc: Use simple parser for IPv4 and v6 headers

To avoid performance overhead when using skb_flow_dissect_flow_keys(), we switch to the simple parsers to get the IP and port numbers.

Performance comparison: throughput (Gbps):
Number of connections, before patch, after patch
1 8.56 10.18
4 11.17 14.07
16 12.21 21.78
64 18.71 32.08
256 15.92 26.32
1024 8.41 15.49
3000 7.82 11.58

---
 drivers/net/hyperv/netvsc_drv.c |   38 +++++++++++++++++++++++++++++---------
 1 files changed, 29 insertions(+), 9 deletions(-)
drivers/net/hyperv/netvsc_drv.c +29 -9
index 1c8db9a..e28951f 100644
@@ -237,20 +237,40 @@ static u32 comp_hash(u8 *key, int klen, void *data, int dlen)
 
 static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)
 {
-	struct flow_keys flow;
+	struct iphdr *iphdr;
+	struct ipv6hdr *ipv6hdr;
+	__be32 dbuf[9];
 	int data_len;
 
-	if (!skb_flow_dissect_flow_keys(skb, &flow, 0) ||
-	    !(flow.basic.n_proto == htons(ETH_P_IP) ||
-	      flow.basic.n_proto == htons(ETH_P_IPV6)))
+	if (eth_hdr(skb)->h_proto != htons(ETH_P_IP) &&
+	    eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))
 		return false;
 
-	if (flow.basic.ip_proto == IPPROTO_TCP)
-		data_len = 12;
-	else
-		data_len = 8;
+	iphdr = ip_hdr(skb);
+	ipv6hdr = ipv6_hdr(skb);
+
+	if (iphdr->version == 4) {
+		dbuf[0] = iphdr->saddr;
+		dbuf[1] = iphdr->daddr;
+		if (iphdr->protocol == IPPROTO_TCP) {
+			dbuf[2] = *(__be32 *)&tcp_hdr(skb)->source;
+			data_len = 12;
+		} else {
+			data_len = 8;
+		}
+	} else if (ipv6hdr->version == 6) {
+		memcpy(dbuf, &ipv6hdr->saddr, 32);
+		if (ipv6hdr->nexthdr == IPPROTO_TCP) {
+			dbuf[8] = *(__be32 *)&tcp_hdr(skb)->source;
+			data_len = 36;
+		} else {
+			data_len = 32;
+		}
+	} else {
+		return false;
+	}
 
-	*hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
+	*hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, dbuf, data_len);
 
 	return true;
 }
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 64 lines · Haiyang Zhang

To avoid performance overhead when using skb_flow_dissect_flow_keys(), we switch to the simple parsers to get the IP and port numbers.

Performance comparison: throughput (Gbps):
Number of connections, before patch, after patch
1 8.56 10.18
4 11.17 14.07
16 12.21 21.78
64 18.71 32.08
256 15.92 26.32
1024 8.41 15.49
3000 7.82 11.58

---
 drivers/net/hyperv/netvsc_drv.c |   38 +++++++++++++++++++++++++++++---------
 1 files changed, 29 insertions(+), 9 deletions(-)
drivers/net/hyperv/netvsc_drv.c +24 -8
index 1c8db9a..e28951f 100644
@@ -237,20 +237,40 @@ static u32 comp_hash(u8 *key, int klen, void *data, int dlen)
 
 static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)
 {
-	struct flow_keys flow;
+	struct iphdr *iphdr;
+	struct ipv6hdr *ipv6hdr;
+	__be32 dbuf[9];
 	int data_len;
 
-	if (!skb_flow_dissect_flow_keys(skb, &flow, 0) ||
-	    !(flow.basic.n_proto == htons(ETH_P_IP) ||
-	      flow.basic.n_proto == htons(ETH_P_IPV6)))
+	if (eth_hdr(skb)->h_proto != htons(ETH_P_IP) &&
+	    eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))
 		return false;
 
-	if (flow.basic.ip_proto == IPPROTO_TCP)
-		data_len = 12;
-	else
-		data_len = 8;
+	iphdr = ip_hdr(skb);
+	ipv6hdr = ipv6_hdr(skb);
+
+	if (iphdr->version == 4) {
+		dbuf[0] = iphdr->saddr;
+		dbuf[1] = iphdr->daddr;
+		if (iphdr->protocol == IPPROTO_TCP) {
+			dbuf[2] = *(__be32 *)&tcp_hdr(skb)->source;
+			data_len = 12;
+		} else {
+			data_len = 8;
+		}
+	} else if (ipv6hdr->version == 6) {
+		memcpy(dbuf, &ipv6hdr->saddr, 32);
+		if (ipv6hdr->nexthdr == IPPROTO_TCP) {
+			dbuf[8] = *(__be32 *)&tcp_hdr(skb)->source;
+			data_len = 36;
+		} else {
+			data_len = 32;

drivers/net/hyperv/netvsc_drv.c static u32 comp_hash(u8 *key, int klen, void *data, int dlen) · line 276

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +5 -1
+		}
+	} else {
+		return false;
+	}
 
-	*hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
+	*hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, dbuf, data_len);
 
 	return true;
 }

This looks very very wrong to me.

How many times this is called per second, for the 'one flow' case ?

Don't you use TSO in this driver ?

What about encapsulation ?

I suspect you have a quite different issue here.

You simply could use skb_get_hash() since local TCP flows will provide a l4 skb->hash and you have no further flow dissection to do.

#5 lore raw
In reply to Haiyang Zhang

Hello.

Quoted 25 lines · Haiyang Zhang

To avoid performance overhead when using skb_flow_dissect_flow_keys(), we switch to the simple parsers to get the IP and port numbers.

Performance comparison: throughput (Gbps):
Number of connections, before patch, after patch
1 8.56 10.18
4 11.17 14.07
16 12.21 21.78
64 18.71 32.08
256 15.92 26.32
1024 8.41 15.49
3000 7.82 11.58

---
  drivers/net/hyperv/netvsc_drv.c |   38 +++++++++++++++++++++++++++++---------
  1 files changed, 29 insertions(+), 9 deletions(-)
drivers/net/hyperv/netvsc_drv.c +0 -0
index 1c8db9a..e28951f 100644
@@ -237,20 +237,40 @@ static u32 comp_hash(u8 *key, int klen, void *data, int dlen)

[...]

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +10 -0
+	if (iphdr->version == 4) {
+		dbuf[0] = iphdr->saddr;
+		dbuf[1] = iphdr->daddr;
+		if (iphdr->protocol == IPPROTO_TCP) {
+			dbuf[2] = *(__be32 *)&tcp_hdr(skb)->source;
+			data_len = 12;
+		} else {
+			data_len = 8;
+		}
+	} else if (ipv6hdr->version == 6) {

drivers/net/hyperv/netvsc_drv.c static u32 comp_hash(u8 *key, int klen, void *data, int dlen) · line 271

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +10 -0
+		memcpy(dbuf, &ipv6hdr->saddr, 32);
+		if (ipv6hdr->nexthdr == IPPROTO_TCP) {
+			dbuf[8] = *(__be32 *)&tcp_hdr(skb)->source;
+			data_len = 36;
+		} else {
+			data_len = 32;
+		}
+	} else {
+		return false;
+	}

This is asking to be a *switch* statement.

[...]

MBR, Sergei

#6 lore raw
Quoted 48 lines · Sergei Shtylyov
Quoted 26 lines

To avoid performance overhead when using skb_flow_dissect_flow_keys(), we switch to the simple parsers to get the IP and port numbers.

Performance comparison: throughput (Gbps):
Number of connections, before patch, after patch
1 8.56 10.18
4 11.17 14.07

16            12.21        21.78
64            18.71        32.08
256            15.92        26.32
1024            8.41        15.49
3000            7.82        11.58
---
  drivers/net/hyperv/netvsc_drv.c |   38 +++++++++++++++++++++++++++++---------
  1 files changed, 29 insertions(+), 9 deletions(-)
drivers/net/hyperv/netvsc_drv.c +0 -0
index 1c8db9a..e28951f 100644
@@ -237,20 +237,40 @@ static u32 comp_hash(u8 *key, int klen, void *data,
 int dlen)

[...]

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +10 -0
+    if (iphdr->version == 4) {
+        dbuf[0] = iphdr->saddr;
+        dbuf[1] = iphdr->daddr;
+        if (iphdr->protocol == IPPROTO_TCP) {
+            dbuf[2] = *(__be32 *)&tcp_hdr(skb)->source;
+            data_len = 12;
+        } else {
+            data_len = 8;
+        }
+    } else if (ipv6hdr->version == 6) {

drivers/net/hyperv/netvsc_drv.c static u32 comp_hash(u8 *key, int klen, void *data, int dlen) · line 271

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +10 -0
+        memcpy(dbuf, &ipv6hdr->saddr, 32);
+        if (ipv6hdr->nexthdr == IPPROTO_TCP) {
+            dbuf[8] = *(__be32 *)&tcp_hdr(skb)->source;
+            data_len = 36;
+        } else {
+            data_len = 32;
+        }
+    } else {
+        return false;
+    }
Quoted 1 line

This is asking to be a *switch* statement.

Oops, nevermind. I'd misread the code.

Quoted 1 line

[...]

MBR, Sergei

author maintainer
#2 lore raw
In reply to Eric Dumazet
Quoted 8 lines

-----Original Message-----
From: Eric Dumazet [mailto:[email protected]]
Sent: Wednesday, December 16, 2015 12:08 PM

This looks very very wrong to me.

How many times this is called per second, for the 'one flow' case ?

Quoted 8 lines

Don't you use TSO in this driver ?

What about encapsulation ?

I suspect you have a quite different issue here.

You simply could use skb_get_hash() since local TCP flows will provide a l4 skb->hash and you have no further flow dissection to do.

In our test, we have bisected and found the following patch introduced big overhead into skb_flow_dissect_flow_keys(), and caused performance regression:
commit: d34af823
net: Add VLAN ID to flow_keys

This patch didn't add too many instructions, but we think the change to the size of struct flow_keys may cause different cache missing rate...

To avoid affecting other drivers using this function, our patch limits the change inside our driver to fix this performance regression.

Regarding your suggestion on skb_get_hash(), I looked at the code and ran some tests, and found the skb->l4_hash and skb->sw_hash bits are not set, so it calls __skb_get_hash() which eventually calls skb_flow_dissect_flow_keys(). So it still includes the performance overhead mentioned above.

static inline __u32 skb_get_hash(struct sk_buff *skb)

{
        if (!skb->l4_hash && !skb->sw_hash)
                __skb_get_hash(skb);

        return skb->hash;
}

void __skb_get_hash(struct sk_buff *skb)

{
        struct flow_keys keys;

        __flow_hash_secret_init();

        __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
                          flow_keys_have_l4(&keys));
}

static inline u32 ___skb_get_hash(const struct sk_buff *skb,
struct flow_keys *keys, u32 keyval)

{
        skb_flow_dissect_flow_keys(skb, keys,
                                   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);

        return __flow_hash_from_keys(keys, keyval);
}

Thanks,
- Haiyang

#3 lore raw
Quoted 14 lines · Haiyang Zhang
Quoted 14 lines

-----Original Message-----
From: Eric Dumazet [mailto:[email protected]]
Sent: Wednesday, December 16, 2015 12:08 PM

This looks very very wrong to me.

How many times this is called per second, for the 'one flow' case ?

Don't you use TSO in this driver ?

What about encapsulation ?

I suspect you have a quite different issue here.

Quoted 8 lines
Quoted 2 lines

You simply could use skb_get_hash() since local TCP flows will provide a l4 skb->hash and you have no further flow dissection to do.

In our test, we have bisected and found the following patch introduced big overhead into skb_flow_dissect_flow_keys(), and caused performance regression:
commit: d34af823
net: Add VLAN ID to flow_keys

Adding Tom Herbert <[email protected]>

Your driver was assuming things about "struct flow_keys" layout. This is not permitted.

Magic numbers like 12 and 8 are really bad...

static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)

{
        struct flow_keys flow;
        int data_len;

        if (!skb_flow_dissect_flow_keys(skb, &flow, 0) ||
            !(flow.basic.n_proto == htons(ETH_P_IP) ||
              flow.basic.n_proto == htons(ETH_P_IPV6)))
                return false;

        if (flow.basic.ip_proto == IPPROTO_TCP)
                data_len = 12;
        else
                data_len = 8;

        *hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);

        return true;
}
Quoted 11 lines

This patch didn't add too many instructions, but we think the change to the size of struct flow_keys may cause different cache missing rate...

To avoid affecting other drivers using this function, our patch limits the change inside our driver to fix this performance regression.

Regarding your suggestion on skb_get_hash(), I looked at the code and ran some tests, and found the skb->l4_hash and skb->sw_hash bits are not set, so it calls __skb_get_hash() which eventually calls skb_flow_dissect_flow_keys(). So it still includes the performance overhead mentioned above.

Okay, but have you tried this instead of just guessing ?

Are you forwarding traffic, or is the traffic locally generated ?

TCP stack does set skb->l4_hash for sure in current kernels.

Your 'basic flow dissection' is very buggy and a step backward.

Just call skb_get_hash() : Not only your perf problem will vanish, but your driver will correctly work with all possible malformed packets (like pretending to be TCP packets but too small to even contain one byte of TCP header) and well formed ones, with all encapsulations.

maintainer
#4 lore raw
In reply to Haiyang Zhang

From: Haiyang Zhang <[email protected]>
Date: Wed, 16 Dec 2015 19:20:44 +0000

Quoted 5 lines

In our test, we have bisected and found the following patch introduced big overhead into skb_flow_dissect_flow_keys(), and caused performance regression:
commit: d34af823
net: Add VLAN ID to flow_keys

NEVER _EVER_ work around this kind of problem by bypassing the code in question in your driver.

ALWAYS work to fix the actual problem.