Skip to contentSkip to conversation

ip_gre: a bunch of fixes for mtu

PATCH In review William Tu 2 participants 8 messages Oct 9, 2017 – Oct 11, 2017

Subsystems (3)
lore
Share & tools
lore

William Tu

author
#0 lore raw

cover ip_gre: a bunch of fixes for mtu

The first two patches are to fix some issues for mtu and needed_headroom length calculation from the gre and erspan tunnel header. The last path tries to avoid arithmetic operation for every packet when checking for erspan truncate.

William Tu (3):
ip_gre: fix mtu and headroom size
ip_gre: fix erspan tunnel mtu calculation
ip_gre: cache the device mtu hard_header_len calc

 include/net/erspan.h     |  1 +
 include/net/ip_tunnels.h |  1 +
 net/ipv4/ip_gre.c        | 23 +++++++++++------------
 net/ipv4/ip_tunnel.c     |  3 +++
 4 files changed, 16 insertions(+), 12 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/3 ip_gre: fix mtu and headroom size

The ip_gre_calc_hlen() already counts gre's base header len (4-byte) plus optional header's len, so tunnel->hlen has the entire gre headeri + options len. Thus, remove the -4 and +4 when calculating the needed_headroom and mtu.

Fixes 4565e9919cda ("gre: Setup and TX path for gre/UDP foo-over-udp encapsulation")
Cc Tom Herbert <[email protected]>
---
 net/ipv4/ip_gre.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/ip_gre.c +2 -2
index c105a315b1a3..286065c35959 100644
@@ -947,8 +947,8 @@ static void __gre_tunnel_init(struct net_device *dev)
 
 	t_hlen = tunnel->hlen + sizeof(struct iphdr);
 
-	dev->needed_headroom	= LL_MAX_HEADER + t_hlen + 4;
-	dev->mtu		= ETH_DATA_LEN - t_hlen - 4;
+	dev->needed_headroom	= LL_MAX_HEADER + t_hlen;
+	dev->mtu		= ETH_DATA_LEN - t_hlen;
 
 	dev->features		|= GRE_FEATURES;
 	dev->hw_features	|= GRE_FEATURES;
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH 2/3 ip_gre: fix erspan tunnel mtu calculation

In reply to William Tu

Remove the unnecessary -4 and +4 bytes at mtu and headroom calculation. In addition, erspan uses fixed 8-byte gre header, so add ERSPAN_GREHDR_LEN macro for better readability.

Now tunnel->hlen = grehdr(8) + erspanhdr(8) = 16 byte. The mtu should be ETH_DATA_LEN - 16 - iph(20) = 1464. After the ip_tunnel_bind_dev(), the mtu is adjusted to 1464 - 14 (dev->hard_header_len) = 1450.
The maximum skb->len the erspan tunnel can carry without being truncated is 1450 + 14 = 1464 byte.

---
 include/net/erspan.h |  1 +
 net/ipv4/ip_gre.c    | 11 +++++------
 2 files changed, 6 insertions(+), 6 deletions(-)
include/net/erspan.h +1 -0
index ca94fc86865e..e28294e248d0 100644
@@ -28,6 +28,7 @@
  */
 
 #define ERSPAN_VERSION	0x1
+#define ERSPAN_GREHDR_LEN 8	/* ERSPAN has fixed 8-byte GRE header */
 
 #define VER_MASK	0xf000
 #define VLAN_MASK	0x0fff
net/ipv4/ip_gre.c +5 -6
index 286065c35959..6e6e4c4811cc 100644
@@ -569,8 +569,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
 
 	key = &tun_info->key;
 
-	/* ERSPAN has fixed 8 byte GRE header */
-	tunnel_hlen = 8 + sizeof(struct erspanhdr);
+	tunnel_hlen = ERSPAN_GREHDR_LEN + sizeof(struct erspanhdr);
 
 	rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
 	if (!rt)
@@ -591,7 +590,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
 	erspan_build_header(skb, tunnel_id_to_key32(key->tun_id),
 			    ntohl(md->index), truncate);
 
-	gre_build_header(skb, 8, TUNNEL_SEQ,
+	gre_build_header(skb, ERSPAN_GREHDR_LEN, TUNNEL_SEQ,
 			 htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++));
 
 	df = key->tun_flags & TUNNEL_DONT_FRAGMENT ?  htons(IP_DF) : 0;
@@ -1242,14 +1241,14 @@ static int erspan_tunnel_init(struct net_device *dev)
 	struct ip_tunnel *tunnel = netdev_priv(dev);
 	int t_hlen;
 
-	tunnel->tun_hlen = 8;
+	tunnel->tun_hlen = ERSPAN_GREHDR_LEN;
 	tunnel->parms.iph.protocol = IPPROTO_GRE;
 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
 		       sizeof(struct erspanhdr);
 	t_hlen = tunnel->hlen + sizeof(struct iphdr);
 
-	dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
-	dev->mtu = ETH_DATA_LEN - t_hlen - 4;
+	dev->needed_headroom = LL_MAX_HEADER + t_hlen;
+	dev->mtu = ETH_DATA_LEN - t_hlen;
 	dev->features		|= GRE_FEATURES;
 	dev->hw_features	|= GRE_FEATURES;
 	dev->priv_flags		|= IFF_LIVE_ADDR_CHANGE;
Signature · 1 line
2.7.4
author
#6 lore raw

PATCH 3/3 ip_gre: cache the device mtu hard_header_len calc

In reply to William Tu

The patch introduces ip_tunnel->ether_mtu fields to cache the value of dev->mtu + dev->hard_header_len. This avoids the arithmetic operation on every packet.

---
 include/net/ip_tunnels.h | 1 +
 net/ipv4/ip_gre.c        | 8 ++++----
 net/ipv4/ip_tunnel.c     | 3 +++
 3 files changed, 8 insertions(+), 4 deletions(-)
include/net/ip_tunnels.h +1 -0
index b41a1e057fce..19565be26e13 100644
@@ -117,6 +117,7 @@ struct ip_tunnel {
 
 	/* This field used only by ERSPAN */
 	u32		index;		/* ERSPAN type II index */
+	unsigned int	ether_mtu;	/* The mtu including the ether hdr */
 
 	struct dst_cache dst_cache;
 
net/ipv4/ip_gre.c +4 -4
index 6e6e4c4811cc..994b8ddea0b1 100644
@@ -578,8 +578,8 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
 	if (gre_handle_offloads(skb, false))
 		goto err_free_rt;
 
-	if (skb->len > dev->mtu + dev->hard_header_len) {
-		pskb_trim(skb, dev->mtu + dev->hard_header_len);
+	if (skb->len > tunnel->ether_mtu) {
+		pskb_trim(skb, tunnel->ether_mtu);
 		truncate = true;
 	}
 
@@ -730,8 +730,8 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb,
 	if (skb_cow_head(skb, dev->needed_headroom))
 		goto free_skb;
 
-	if (skb->len > dev->mtu + dev->hard_header_len) {
-		pskb_trim(skb, dev->mtu + dev->hard_header_len);
+	if (skb->len > tunnel->ether_mtu) {
+		pskb_trim(skb, tunnel->ether_mtu);
 		truncate = true;
 	}
 
net/ipv4/ip_tunnel.c +3 -0
index fe6fee728ce4..859af5b86802 100644
@@ -348,6 +348,7 @@ static int ip_tunnel_bind_dev(struct net_device *dev)
 
 	dev->needed_headroom = t_hlen + hlen;
 	mtu -= (dev->hard_header_len + t_hlen);
+	tunnel->ether_mtu = mtu + dev->hard_header_len;
 
 	if (mtu < 68)
 		mtu = 68;
@@ -952,6 +953,7 @@ int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict)
 	}
 
 	dev->mtu = new_mtu;
+	tunnel->ether_mtu = new_mtu + dev->hard_header_len;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(__ip_tunnel_change_mtu);
@@ -1183,6 +1185,7 @@ int ip_tunnel_init(struct net_device *dev)
 
 	tunnel->dev = dev;
 	tunnel->net = dev_net(dev);
+	tunnel->ether_mtu = dev->mtu + dev->hard_header_len;
 	strcpy(tunnel->parms.name, dev->name);
 	iph->version		= 4;
 	iph->ihl		= 5;
Signature · 1 line
2.7.4
#3 lore raw

Re: [net-next 2/3] ip_gre: fix erspan tunnel mtu calculation

In reply to William Tu
Quoted 58 lines · William Tu

Remove the unnecessary -4 and +4 bytes at mtu and headroom calculation. In addition, erspan uses fixed 8-byte gre header, so add ERSPAN_GREHDR_LEN macro for better readability.

Now tunnel->hlen = grehdr(8) + erspanhdr(8) = 16 byte. The mtu should be ETH_DATA_LEN - 16 - iph(20) = 1464. After the ip_tunnel_bind_dev(), the mtu is adjusted to 1464 - 14 (dev->hard_header_len) = 1450.
The maximum skb->len the erspan tunnel can carry without being truncated is 1450 + 14 = 1464 byte.

---
 include/net/erspan.h |  1 +
 net/ipv4/ip_gre.c    | 11 +++++------
 2 files changed, 6 insertions(+), 6 deletions(-)
include/net/erspan.h +1 -0
index ca94fc86865e..e28294e248d0 100644
@@ -28,6 +28,7 @@
  */
 
 #define ERSPAN_VERSION 0x1
+#define ERSPAN_GREHDR_LEN 8    /* ERSPAN has fixed 8-byte GRE header */
 
 #define VER_MASK       0xf000
 #define VLAN_MASK      0x0fff
net/ipv4/ip_gre.c +2 -4
index 286065c35959..6e6e4c4811cc 100644
@@ -569,8 +569,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
 
        key = &tun_info->key;
 
-       /* ERSPAN has fixed 8 byte GRE header */
-       tunnel_hlen = 8 + sizeof(struct erspanhdr);
+       tunnel_hlen = ERSPAN_GREHDR_LEN + sizeof(struct erspanhdr);
 
        rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
        if (!rt)
@@ -591,7 +590,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
        erspan_build_header(skb, tunnel_id_to_key32(key->tun_id),
                            ntohl(md->index), truncate);
 
-       gre_build_header(skb, 8, TUNNEL_SEQ,
+       gre_build_header(skb, ERSPAN_GREHDR_LEN, TUNNEL_SEQ,
                         htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++));
 
        df = key->tun_flags & TUNNEL_DONT_FRAGMENT ?  htons(IP_DF) : 0;
@@ -1242,14 +1241,14 @@ static int erspan_tunnel_init(struct net_device *dev)
        struct ip_tunnel *tunnel = netdev_priv(dev);
        int t_hlen;
 
-       tunnel->tun_hlen = 8;

net/ipv4/ip_gre.c static int erspan_tunnel_init(struct net_device *dev) · line 1251

Quoted 10 lines
net/ipv4/ip_gre.c +3 -2
+       tunnel->tun_hlen = ERSPAN_GREHDR_LEN;
        tunnel->parms.iph.protocol = IPPROTO_GRE;
        tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
                       sizeof(struct erspanhdr);
        t_hlen = tunnel->hlen + sizeof(struct iphdr);
 
-       dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
-       dev->mtu = ETH_DATA_LEN - t_hlen - 4;
+       dev->needed_headroom = LL_MAX_HEADER + t_hlen;
+       dev->mtu = ETH_DATA_LEN - t_hlen;

1. I guess '+4-4' stuff was copied from __gre_tunnel_init(), I'm thinking it may be there for some reason.

2. 'dev->needed_headroom =' and 'dev->mtu =' are really needed ? As I've seen both will be updated in .newlink:
ipgre_newlink() -> ip_tunnel_newlink() -> ip_tunnel_bind_dev()

Quoted 6 lines
net/ipv4/ip_gre.c +0 -1
        dev->features           |= GRE_FEATURES;
        dev->hw_features        |= GRE_FEATURES;
        dev->priv_flags         |= IFF_LIVE_ADDR_CHANGE;
--

2.7.4

author
#4 lore raw

Re: [net-next 2/3] ip_gre: fix erspan tunnel mtu calculation

Quoted 11 lines
Quoted 11 lines
net/ipv4/ip_gre.c +1 -1
@@ -1242,14 +1241,14 @@ static int erspan_tunnel_init(struct net_device *dev)
        struct ip_tunnel *tunnel = netdev_priv(dev);
        int t_hlen;
 
-       tunnel->tun_hlen = 8;
+       tunnel->tun_hlen = ERSPAN_GREHDR_LEN;
        tunnel->parms.iph.protocol = IPPROTO_GRE;
        tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
                       sizeof(struct erspanhdr);
        t_hlen = tunnel->hlen + sizeof(struct iphdr);
 
Quoted 7 lines

net/ipv4/ip_gre.c static int erspan_tunnel_init(struct net_device *dev) · line 1251

Quoted 4 lines
net/ipv4/ip_gre.c +2 -2
-       dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
-       dev->mtu = ETH_DATA_LEN - t_hlen - 4;
+       dev->needed_headroom = LL_MAX_HEADER + t_hlen;
+       dev->mtu = ETH_DATA_LEN - t_hlen;

1. I guess '+4-4' stuff was copied from __gre_tunnel_init(), I'm thinking it may be there for some reason.

I traced back to
4565e9919cda ("gre: Setup and TX path for gre/UDP foo-over-udp encapsulation") and I think '+4-4' is there for GRE base header length.

Since now we do

    dev->mtu = ETH_DATA_LEN - t_hlen;

and t_hlen already counts the the gre base header + optional header len, I think it's not needed.

Quoted 4 lines

2. 'dev->needed_headroom =' and 'dev->mtu =' are really needed ? As I've seen both will be updated in .newlink:
ipgre_newlink() -> ip_tunnel_newlink() -> ip_tunnel_bind_dev()

right, I also find both values gets overwritten by ip_tunnel_bind_dev() using my test cases. Maybe we can remove them?

Thanks
William

#7 lore raw

Re: [net-next 3/3] ip_gre: cache the device mtu hard_header_len calc

In reply to William Tu
Quoted 17 lines · William Tu

The patch introduces ip_tunnel->ether_mtu fields to cache the value of dev->mtu + dev->hard_header_len. This avoids the arithmetic operation on every packet.

---
 include/net/ip_tunnels.h | 1 +
 net/ipv4/ip_gre.c        | 8 ++++----
 net/ipv4/ip_tunnel.c     | 3 +++
 3 files changed, 8 insertions(+), 4 deletions(-)
include/net/ip_tunnels.h +0 -0
index b41a1e057fce..19565be26e13 100644

include/net/ip_tunnels.h struct ip_tunnel { · line 122

Quoted 6 lines
include/net/ip_tunnels.h +1 -0
@@ -117,6 +117,7 @@ struct ip_tunnel {
 
        /* This field used only by ERSPAN */
        u32             index;          /* ERSPAN type II index */
+       unsigned int    ether_mtu;      /* The mtu including the ether hdr */
 
        struct dst_cache dst_cache;

ip_tunnel is a very common structure for various tunnels. Adding ether_mtu into it to avoid ONLY an addition operation ONLY for erspan, I think it's not worth it.

Quoted 58 lines
net/ipv4/ip_gre.c +4 -4
index 6e6e4c4811cc..994b8ddea0b1 100644
@@ -578,8 +578,8 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
        if (gre_handle_offloads(skb, false))
                goto err_free_rt;
 
-       if (skb->len > dev->mtu + dev->hard_header_len) {
-               pskb_trim(skb, dev->mtu + dev->hard_header_len);
+       if (skb->len > tunnel->ether_mtu) {
+               pskb_trim(skb, tunnel->ether_mtu);
                truncate = true;
        }
 
@@ -730,8 +730,8 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb,
        if (skb_cow_head(skb, dev->needed_headroom))
                goto free_skb;
 
-       if (skb->len > dev->mtu + dev->hard_header_len) {
-               pskb_trim(skb, dev->mtu + dev->hard_header_len);
+       if (skb->len > tunnel->ether_mtu) {
+               pskb_trim(skb, tunnel->ether_mtu);
                truncate = true;
        }
 
net/ipv4/ip_tunnel.c +3 -1
index fe6fee728ce4..859af5b86802 100644
@@ -348,6 +348,7 @@ static int ip_tunnel_bind_dev(struct net_device *dev)
 
        dev->needed_headroom = t_hlen + hlen;
        mtu -= (dev->hard_header_len + t_hlen);
+       tunnel->ether_mtu = mtu + dev->hard_header_len;
 
        if (mtu < 68)
                mtu = 68;
@@ -952,6 +953,7 @@ int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict)
        }
 
        dev->mtu = new_mtu;
+       tunnel->ether_mtu = new_mtu + dev->hard_header_len;
        return 0;
 }
 EXPORT_SYMBOL_GPL(__ip_tunnel_change_mtu);
@@ -1183,6 +1185,7 @@ int ip_tunnel_init(struct net_device *dev)
 
        tunnel->dev = dev;
        tunnel->net = dev_net(dev);
+       tunnel->ether_mtu = dev->mtu + dev->hard_header_len;
        strcpy(tunnel->parms.name, dev->name);
        iph->version            = 4;
        iph->ihl                = 5;
--

2.7.4

#5 lore raw

Re: [net-next 2/3] ip_gre: fix erspan tunnel mtu calculation

In reply to William Tu
Quoted 18 lines · William Tu
Quoted 11 lines
Quoted 11 lines
net/ipv4/ip_gre.c +1 -1
@@ -1242,14 +1241,14 @@ static int erspan_tunnel_init(struct net_device *dev)
        struct ip_tunnel *tunnel = netdev_priv(dev);
        int t_hlen;
 
-       tunnel->tun_hlen = 8;
+       tunnel->tun_hlen = ERSPAN_GREHDR_LEN;
        tunnel->parms.iph.protocol = IPPROTO_GRE;
        tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
                       sizeof(struct erspanhdr);
        t_hlen = tunnel->hlen + sizeof(struct iphdr);
 
Quoted 7 lines

net/ipv4/ip_gre.c static int erspan_tunnel_init(struct net_device *dev) · line 1251

Quoted 4 lines
net/ipv4/ip_gre.c +2 -2
-       dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
-       dev->mtu = ETH_DATA_LEN - t_hlen - 4;
+       dev->needed_headroom = LL_MAX_HEADER + t_hlen;
+       dev->mtu = ETH_DATA_LEN - t_hlen;

1. I guess '+4-4' stuff was copied from __gre_tunnel_init(), I'm thinking it may be there for some reason.

Quoted 8 lines

I traced back to
4565e9919cda ("gre: Setup and TX path for gre/UDP foo-over-udp encapsulation") and I think '+4-4' is there for GRE base header length.

Since now we do

    dev->mtu = ETH_DATA_LEN - t_hlen;

and t_hlen already counts the the gre base header + optional header len, I think it's not needed.

okay. thanks.

Quoted 7 lines
Quoted 5 lines

2. 'dev->needed_headroom =' and 'dev->mtu =' are really needed ? As I've seen both will be updated in .newlink:
ipgre_newlink() -> ip_tunnel_newlink() -> ip_tunnel_bind_dev()

right, I also find both values gets overwritten by ip_tunnel_bind_dev() using my test cases. Maybe we can remove them?

It's there just in case that there is no lower dev found, but ip_tunnel_newlink/create always updates dev->mtu even if there is no lower dev found.

let's leave as it is for now, ipgre may just not be sure ip_tunnel_xxx would do it when no lower dev found.