Fix handling of GRO skbs in forwarding path
Subsystems (4)
cover Fix handling of GRO skbs in forwarding path
Marcelo Ricardo Leitner reported problems when the forwarding link path has a lower mtu than the incoming link if the inbound interface supports GRO.
Currently GSO/GRO skbs bypass all dst MTU checks, i.e. forwarding of such skbs fails in case the outgoing link mtu is smaller than the one of the incoming interface: We neither generate an icmp error nor will the packet be fragmented if ipv4 would permit it.
The first patch moves part of Eric Dumazets skb_gso_seglen helper from sch_tbf to skbuff core for re-use in forwarding path.
The 2nd change then alters forwarding path to handle GRO skbs.
It is not 100% correct, since the icmp error will contain the headers of the GRO skb instead of the original/segmented one, but it seems to work fine in my (limited) tests.
Software segmentation is done for ipv4 if the DF bit is not set.
If you think this is -next material just set patchwork state to "deferred", I'll resend then once -next is open again.
include/linux/skbuff.h | 18 +++++++++++++++ net/core/skbuff.c | 26 +++++++++++++++++++++ net/ipv4/ip_forward.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- net/ipv6/ip6_output.c | 18 +++++++++++++-- net/sched/sch_tbf.c | 12 ++-------- 5 files changed, 119 insertions(+), 13 deletions(-)
PATCH 1/2 net: add and use skb_gso_transport_seglen()
This moves part of Eric Dumazets skb_gso_seglen helper from tbf sched to skbuff core so it may be reused by upcoming ip forwarding path patch.
--- include/linux/skbuff.h | 1 + net/core/skbuff.c | 26 ++++++++++++++++++++++++++ net/sched/sch_tbf.c | 12 +++--------- 3 files changed, 30 insertions(+), 9 deletions(-)
include/linux/skbuff.h +1 -0
@@ -2371,6 +2371,7 @@ void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);void skb_split (struct sk_buff *skb,struct sk_buff *skb1,const u32 len);int skb_shift (struct sk_buff *tgt,struct sk_buff *skb,int shiftlen);void skb_scrub_packet (struct sk_buff *skb,bool xnet); +unsigned int skb_gso_transport_seglen (const struct sk_buff *skb);struct sk_buff *skb_segment (struct sk_buff *skb,netdev_features_t features);struct skb_checksum_ops {
net/core/skbuff.c +26 -0
@@ -45,6 +45,7 @@#include <linux/mm.h> #include <linux/interrupt.h> #include <linux/in.h> +#include <linux/tcp.h> #include <linux/inet.h> #include <linux/slab.h> #include <linux/netdevice.h>
@@ -71,6 +72,8 @@#include <trace/events/skb.h> #include <linux/highmem.h> +#include <uapi/linux/udp.h> +struct kmem_cache *skbuff_head_cache__read_mostly ;static struct kmem_cache *skbuff_fclone_cache__read_mostly ;
@@ -3592,3 +3595,26 @@ void skb_scrub_packet(struct sk_buff *skb, bool xnet)nf_reset_trace (skb); }EXPORT_SYMBOL_GPL (skb_scrub_packet); + +/** +* skb_gso_transport_seglen - Return length of individual segments of a gso packet +* +* @skb: GSO skb +* +* skb_gso_transport_seglen is used to determine the real size of the +* individual segments, including Layer4 headers (TCP/UDP). +* +* The MAC/L2 or network (IP, IPv6) headers are not accounted for. +*/ +unsigned int skb_gso_transport_seglen (const struct sk_buff *skb) +{ +const struct skb_shared_info *shinfo =skb_shinfo (skb); +unsigned int hdr_len; + +if (likely (shinfo->gso_type & (SKB_GSO_TCPV4 |SKB_GSO_TCPV6 ))) + hdr_len =tcp_hdrlen (skb); +else + hdr_len =sizeof (struct udphdr ); +return hdr_len + shinfo->gso_size; +} +EXPORT_SYMBOL_GPL (skb_gso_transport_seglen);
net/sched/sch_tbf.c +3 -9
@@ -148,16 +148,10 @@ static u64 psched_ns_t2l(const struct psched_ratecfg *r,* Return length of individual segments of a gso packet, * including all headers (MAC, IP, TCP/UDP) */ -static unsigned int skb_gso_seglen (const struct sk_buff *skb) +static unsigned int skb_gso_mac_seglen (const struct sk_buff *skb) {unsigned int hdr_len =skb_transport_header (skb) -skb_mac_header (skb); -const struct skb_shared_info *shinfo =skb_shinfo (skb); - -if (likely (shinfo->gso_type & (SKB_GSO_TCPV4 |SKB_GSO_TCPV6 ))) - hdr_len +=tcp_hdrlen (skb); -else - hdr_len +=sizeof (struct udphdr ); -return hdr_len + shinfo->gso_size; +return hdr_len +skb_gso_transport_seglen (skb); }/* GSO packet is too big, segment it so that tbf can transmit
@@ -202,7 +196,7 @@ static int tbf_enqueue(struct sk_buff *skb, struct Qdisc *sch)int ret;if (qdisc_pkt_len (skb) > q->max_size) { -if (skb_is_gso (skb) &&skb_gso_seglen (skb) <= q->max_size) +if (skb_is_gso (skb) &&skb_gso_mac_seglen (skb) <= q->max_size)return tbf_segment (skb, sch);return qdisc_reshape_fail (skb, sch); }
Signature · 1 line
1.8.1.5
PATCH 2/2 net: ip, ipv6: handle gso skbs in forwarding path
In reply to Florian WestphalGiven:
Host <mtu1500> R1 <mtu1200> R2
Host sends tcp stream which is routed via R1 and R2. R1 nics perform GRO.
In this case, the kernel will fail to send ICMP fragmentation needed messages (or pkt too big for ipv6), as gso packets currently bypass the dst mtu checks in forward path. Instead, Linux tries to send out packets exceeding R1-R2 link mtu.
When locking route MTU on Host (i.e., no ipv4 DF bit set), R1 does not fragment the packets when forwarding, and again tries to send out packets exceeding R1-R2 link mtu.
This alters the forwarding dstmtu checks to take the individual gso segment lengths into account.
For ipv6, we send out pkt too big error for gso if the individual segments are too big.
For ipv4, we either send icmp fragmentation needed, or, if the DF bit is not set, perform software segmentation and let the output path create fragments when the packet is leaving the machine. This is technically not 100% accurate, as the error message will contain the gro-skb tcp header (we'd have to segment unconditionally and use first segment instead), but it does seem to work.
--- include/linux/skbuff.h | 17 +++++++++++++++ net/ipv4/ip_forward.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- net/ipv6/ip6_output.c | 18 ++++++++++++++-- 3 files changed, 89 insertions(+), 4 deletions(-)
include/linux/skbuff.h +17 -0
@@ -2811,5 +2811,22 @@ static inline bool skb_head_is_locked(const struct sk_buff *skb) {return !skb->head_frag ||skb_cloned (skb); } + +/** +* skb_gso_network_seglen - Return length of individual segments of a gso packet +* +* @skb: GSO skb +* +* skb_gso_network_seglen is used to determine the real size of the +* individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). +* +* The MAC/L2 header is not accounted for. +*/ +static inline unsigned int skb_gso_network_seglen (const struct sk_buff *skb) +{ +unsigned int hdr_len =skb_transport_header (skb) - +skb_network_header (skb); +return hdr_len +skb_gso_transport_seglen (skb); +}#endif /* __KERNEL__ */ #endif /* _LINUX_SKBUFF_H */
net/ipv4/ip_forward.c +56 -2
76 lines not on this page · raw message
net/ipv6/ip6_output.c +16 -2
@@ -321,6 +321,21 @@ static inline int ip6_forward_finish(struct sk_buff *skb)return dst_output (skb); } +static bool ip6_pkt_too_big (const struct sk_buff *skb,unsigned int mtu) +{ +unsigned int len; + +if (skb->local_df) +return false; + +if (IP6CB (skb)->frag_max_size &&IP6CB (skb)->frag_max_size > mtu) +return true; + + len =skb_is_gso (skb) ?skb_gso_network_seglen (skb) : skb->len; + +return len > mtu; +} +int ip6_forward (struct sk_buff *skb) {struct dst_entry *dst =skb_dst (skb);
@@ -443,8 +458,7 @@ int ip6_forward(struct sk_buff *skb)if (mtu <IPV6_MIN_MTU ) mtu =IPV6_MIN_MTU ; -if ((!skb->local_df && skb->len > mtu && !skb_is_gso (skb)) || - (IP6CB (skb)->frag_max_size &&IP6CB (skb)->frag_max_size > mtu)) { +if (ip6_pkt_too_big (skb, mtu)) {/* Again, force OUTPUT device used as source address */ skb->dev = dst->dev;icmpv6_send (skb,ICMPV6_PKT_TOOBIG ,0 , mtu);
Signature · 1 line
1.8.1.5
Re: [PATCH 1/2] net: add and use skb_gso_transport_seglen()
In reply to Florian WestphalQuoted 35 lines · Florian Westphal
This moves part of Eric Dumazets
skb_gso_seglenhelper from tbf sched to skbuff core so it may be reused by upcoming ip forwarding path patch.Signed-off-by Florian Westphal <[email protected]>--- include/linux/skbuff.h | 1 + net/core/skbuff.c | 26 ++++++++++++++++++++++++++ net/sched/sch_tbf.c | 12 +++--------- 3 files changed, 30 insertions(+), 9 deletions(-)include/linux/skbuff.h +1
-0@@ -2371,6 +2371,7 @@ void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);void skb_split (struct sk_buff *skb,struct sk_buff *skb1,const u32 len);int skb_shift (struct sk_buff *tgt,struct sk_buff *skb,int shiftlen);void skb_scrub_packet (struct sk_buff *skb,bool xnet); +unsigned int skb_gso_transport_seglen (const struct sk_buff *skb);struct sk_buff *skb_segment (struct sk_buff *skb,netdev_features_t features);struct skb_checksum_ops {net/core/skbuff.c +1
-0@@ -45,6 +45,7 @@#include <linux/mm.h> #include <linux/interrupt.h> #include <linux/in.h> +#include <linux/tcp.h> #include <linux/inet.h> #include <linux/slab.h> #include <linux/netdevice.h>
net/core/skbuff.c line 75
Quoted 4 lines
net/core/skbuff.c +1
-0@@ -71,6 +72,8 @@#include <trace/events/skb.h> #include <linux/highmem.h> +#include <uapi/linux/udp.h>
Normally you should not use uapi/
#include <linux/udp.h>
net/sched/sch_tbf.c
Quoted 35 lines
net/core/skbuff.c +24
-0+struct kmem_cache *skbuff_head_cache__read_mostly ;static struct kmem_cache *skbuff_fclone_cache__read_mostly ;@@ -3592,3 +3595,26 @@ void skb_scrub_packet(struct sk_buff *skb, bool xnet)nf_reset_trace (skb); }EXPORT_SYMBOL_GPL (skb_scrub_packet); + +/** +* skb_gso_transport_seglen - Return length of individual segments of a gso packet +* +* @skb: GSO skb +* +* skb_gso_transport_seglen is used to determine the real size of the +* individual segments, including Layer4 headers (TCP/UDP). +* +* The MAC/L2 or network (IP, IPv6) headers are not accounted for. +*/ +unsigned int skb_gso_transport_seglen (const struct sk_buff *skb) +{ +const struct skb_shared_info *shinfo =skb_shinfo (skb); +unsigned int hdr_len; + +if (likely (shinfo->gso_type & (SKB_GSO_TCPV4 |SKB_GSO_TCPV6 ))) + hdr_len =tcp_hdrlen (skb); +else + hdr_len =sizeof (struct udphdr ); +return hdr_len + shinfo->gso_size; +} +EXPORT_SYMBOL_GPL (skb_gso_transport_seglen);net/sched/sch_tbf.c +0
-0
It seems you forgot to remove from this file this include :
#include <net/tcp.h>
Quoted 20 lines
net/sched/sch_tbf.c +2
-8@@ -148,16 +148,10 @@ static u64 psched_ns_t2l(const struct psched_ratecfg *r,* Return length of individual segments of a gso packet, * including all headers (MAC, IP, TCP/UDP) */ -static unsigned int skb_gso_seglen (const struct sk_buff *skb) +static unsigned int skb_gso_mac_seglen (const struct sk_buff *skb) {unsigned int hdr_len =skb_transport_header (skb) -skb_mac_header (skb); -const struct skb_shared_info *shinfo =skb_shinfo (skb); - -if (likely (shinfo->gso_type & (SKB_GSO_TCPV4 |SKB_GSO_TCPV6 ))) - hdr_len +=tcp_hdrlen (skb); -else - hdr_len +=sizeof (struct udphdr ); -return hdr_len + shinfo->gso_size; +return hdr_len +skb_gso_transport_seglen (skb); }/* GSO packet is too big, segment it so that tbf can transmit
net/sched/sch_tbf.c static int tbf_enqueue(struct sk_buff *skb, struct Qdisc *sch) · line 202
Quoted 8 lines
net/sched/sch_tbf.c +1
-1@@ -202,7 +196,7 @@ static int tbf_enqueue(struct sk_buff *skb, struct Qdisc *sch)int ret;if (qdisc_pkt_len (skb) > q->max_size) { -if (skb_is_gso (skb) &&skb_gso_seglen (skb) <= q->max_size) +if (skb_is_gso (skb) &&skb_gso_mac_seglen (skb) <= q->max_size)return tbf_segment (skb, sch);return qdisc_reshape_fail (skb, sch); }
Otherwise, this seems good, thanks !
Re: [PATCH 2/2] net: ip, ipv6: handle gso skbs in forwarding path
In reply to Florian WestphalQuoted 82 lines · Florian Westphal
Given:
Host <mtu1500> R1 <mtu1200> R2Host sends tcp stream which is routed via R1 and R2. R1 nics perform GRO.
In this case, the kernel will fail to send ICMP fragmentation needed messages (or pkt too big for ipv6), as gso packets currently bypass the dst mtu checks in forward path. Instead, Linux tries to send out packets exceeding R1-R2 link mtu.
When locking route MTU on Host (i.e., no ipv4 DF bit set), R1 does not fragment the packets when forwarding, and again tries to send out packets exceeding R1-R2 link mtu.
This alters the forwarding dstmtu checks to take the individual gso segment lengths into account.
For ipv6, we send out pkt too big error for gso if the individual segments are too big.
For ipv4, we either send icmp fragmentation needed, or, if the DF bit is not set, perform software segmentation and let the output path create fragments when the packet is leaving the machine. This is technically not 100% accurate, as the error message will contain the gro-skb tcp header (we'd have to segment unconditionally and use first segment instead), but it does seem to work.
Signed-off-by Florian Westphal <[email protected]>--- include/linux/skbuff.h | 17 +++++++++++++++ net/ipv4/ip_forward.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- net/ipv6/ip6_output.c | 18 ++++++++++++++-- 3 files changed, 89 insertions(+), 4 deletions(-)include/linux/skbuff.h +17
-0@@ -2811,5 +2811,22 @@ static inline bool skb_head_is_locked(const struct sk_buff *skb) {return !skb->head_frag ||skb_cloned (skb); } + +/** +* skb_gso_network_seglen - Return length of individual segments of a gso packet +* +* @skb: GSO skb +* +* skb_gso_network_seglen is used to determine the real size of the +* individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). +* +* The MAC/L2 header is not accounted for. +*/ +static inline unsigned int skb_gso_network_seglen (const struct sk_buff *skb) +{ +unsigned int hdr_len =skb_transport_header (skb) - +skb_network_header (skb); +return hdr_len +skb_gso_transport_seglen (skb); +}#endif /* __KERNEL__ */ #endif /* _LINUX_SKBUFF_H */ net/ipv4/ip_forward.c +12
-0@@ -39,6 +39,58 @@#include <net/route.h> #include <net/xfrm.h> +static bool ip_may_fragment (const struct sk_buff *skb) +{ +return unlikely ((ip_hdr (skb)->frag_off &htons (IP_DF )) ==0 ) || + !skb->local_df; +} + +static bool ip_gso_exceeds_dst_mtu (const struct sk_buff *skb) +{ +if (skb->local_df || !skb_is_gso (skb)) +return false; +return skb_gso_network_seglen (skb) >dst_mtu (skb_dst (skb)); +}
net/ipv4/ip_forward.c line 63
Quoted 10 lines
net/ipv4/ip_forward.c +10
-0+ +static bool ip_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) +{ +unsigned len; + +if (skb->local_df) +return false; + len =skb_is_gso (skb) ?skb_gso_network_seglen (skb) : skb->len; + +return len > mtu;
The function should avoid extra computation/tests for small packets.
if (skb->len <= mtu || skb->local_df)
return false;
if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
return false;
return true;
net/ipv4/ip_forward.c line 69
Quoted 6 lines
net/ipv4/ip_forward.c +6
-0+} + +/* called if GSO skb needs to be fragmented on forward. */ +static int ip_forward_finish_gso (struct sk_buff *skb) +{ +struct sk_buff *segs =skb_gso_segment (skb,0 );
0 is very pessimistic.
Have you tried :
netdev_features_t features = netif_skb_features(skb);
struct sk_buff *segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
Quoted 24 lines
net/ipv4/ip_forward.c +24
-0+int ret =0 ; + +if (IS_ERR (segs)) { +kfree_skb (skb); +return -ENOMEM ; + } + +consume_skb (skb); + +do { +struct sk_buff *nskb = segs->next; +int err; + + segs->next =NULL ; + err =dst_output (segs); + +if (err && ret ==0 ) + ret = err; + segs = nskb; + }while (segs); + +return ret; +} +
Re: [PATCH 1/2] net: add and use skb_gso_transport_seglen()
In reply to Eric DumazetQuoted 15 lines · Eric Dumazet
Quoted 14 lines · Florian Westphal
net/core/skbuff.c +1
-0@@ -45,6 +45,7 @@#include <linux/mm.h> #include <linux/interrupt.h> #include <linux/in.h> +#include <linux/tcp.h> #include <linux/inet.h> #include <linux/slab.h> #include <linux/netdevice.h> @@ -71,6 +72,8 @@#include <trace/events/skb.h> #include <linux/highmem.h>
Quoted 4 lines
net/core/skbuff.c line 75
Quoted 1 line
net/core/skbuff.c +1
-0+#include <uapi/linux/udp.h> Normally you should not use uapi/
I added this include to ensure sizeof(struct udphdr) works. I'll change it to linux/udp.h
Quoted 8 lines
net/sched/sch_tbf.c
Quoted 4 lines
net/sched/sch_tbf.c +0
-0It seems you forgot to remove from this file this include :
#include <
net/tcp.h>
Indeed, thanks for spotting this.
Quoted 1 line
Otherwise, this seems good, thanks !
Thanks for reviewing Eric!
Re: [PATCH 2/2] net: ip, ipv6: handle gso skbs in forwarding path
In reply to Eric DumazetQuoted 12 lines · Eric Dumazet
net/ipv4/ip_forward.c line 63
Quoted 9 lines
net/ipv4/ip_forward.c +9
-0+static bool ip_exceeds_mtu (const struct sk_buff *skb,unsigned int mtu) +{ +unsigned len; + +if (skb->local_df) +return false; + len =skb_is_gso (skb) ?skb_gso_network_seglen (skb) : skb->len; + +return len > mtu;The function should avoid extra computation/tests for small packets.
Quoted 2 lines
if (
skb->len<= mtu ||skb->local_df)return false;
Good idea! Will change it as per your suggestion.
Quoted 14 lines
if (
skb_is_gso(skb)&&skb_gso_network_seglen(skb)<= mtu)return false;return true;
net/ipv4/ip_forward.c line 69
Quoted 6 lines
net/ipv4/ip_forward.c +6
-0+} + +/* called if GSO skb needs to be fragmented on forward. */ +static int ip_forward_finish_gso (struct sk_buff *skb) +{ +struct sk_buff *segs =skb_gso_segment (skb,0 );0 is very pessimistic.
Quoted 4 lines
Have you tried :
netdev_features_tfeatures =netif_skb_features(skb);
structsk_buff*segs =skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
No. I'll see if this works for me, then include it in V2.
Thanks Eric.