* [PATCH net v7 1/2] ipmr: set up GSO state for tunnel vifs
2026-11-28 16:47 [PATCH net v7 0/2] ipmr: forward GSO multicast packets Julius Bairaktaris
@ 2026-11-28 16:47 ` Julius Bairaktaris
2026-11-28 16:47 ` [PATCH net v7 2/2] ipmr: do not drop GSO packets that fit the MTU after segmentation Julius Bairaktaris
1 sibling, 0 replies; 3+ messages in thread
From: Julius Bairaktaris @ 2026-11-28 16:47 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, dsahern, idosch, willemb,
willemdebruijn.kernel, kuniyu, steffen.klassert
ip_encap() pushes the outer IPv4 header but does not set SKB_GSO_IPXIP4,
skb->encapsulation or the inner headers. A GSO packet forwarded to a
VIFF_TUNNEL vif is then rejected by ipip_gso_segment() and dropped in
validate_xmit_skb(), after being counted as sent. A device with UDP
segmentation offload is handed a GSO packet whose transport header
points at the inner IP header.
Call iptunnel_handle_offloads() and skb_set_inner_ipproto() as
ipip_tunnel_xmit() does, reserve one outer IP ID per segment as
iptunnel_xmit() does, and set the outer length with iph_set_totlen(),
since a GSO packet plus the outer header can exceed 64 KB.
A packet that is already encapsulated keeps the inner header offsets
and skb->inner_protocol of its own tunnel, and ip_encap() cannot
describe the header it adds without overwriting them: GRE and UDP
tunnel segmentation would read the payload type that ip_encap() sets,
and a device that segments IPXIP4 packets itself, such as i40e, would
prepare only the outermost and the innermost of three IP headers. An
ipip device offers no tunnel offloads, so validate_xmit_skb() segments
such a GSO packet in software before ipip_tunnel_xmit() adds its header.
Do the same in ipmr_queue_fwd_xmit() and ipmr_queue_output_xmit() and
send each segment on its own. The vif flags are read once per packet,
so that a GSO packet that was not segmented does not reach ip_encap()
when the vif becomes a tunnel vif meanwhile.
Complete the pending checksum of an encapsulated packet that is not GSO
with skb_csum_hwoffload_help(), as validate_xmit_skb() does for an ipip
device, which also computes an SCTP CRC32c. This is done after the last
use of the IP header pointer, as completing it can reallocate the head.
Tested in QEMU with network namespaces and veth: 1000 datagrams per
case in FOU carrying SIT or IPIP, in IPIP, GRE and VXLAN, aggregated by
GRO on the router and routed by an MFC entry to a plain and a tunnel
vif, whose underlay segments in software or offers segmentation (10
cases). On net 2 of the 10 cases deliver every datagram to both vifs
with distinct outer IP IDs (2 runs); with this change all 10, with
valid checksums, and the underlay is handed no GSO packet with three
IP headers (5 runs, one with KASAN). 50 SCTP INIT chunks encapsulated
in IPIP by a tc BPF program and routed to a tunnel vif arrive with a
valid CRC32c (5 runs).
iptunnel_handle_offloads() keeps SKB_GSO_TCP_FIXEDID of a TCP GSO
packet that is not encapsulated, so the bit then applies to the added
outer header, whose DF bit is clear. That holds for every caller of
iptunnel_handle_offloads(); "net: keep the TCP fixed-ID flags on the
header they describe", posted for net-next, clears the bit there.
Fixes: 9fd1ff5d2ac7 ("udp: Support UDP fraglist GRO/GSO.")
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
---
Changes in v7:
- Segment an encapsulated GSO packet with skb_gso_segment() before
ip_encap() on a tunnel vif instead of dropping those without
SKB_GSO_IPXIP4 (Willem de Bruijn, Sashiko).
- Complete the checksum with skb_csum_hwoffload_help() (Willem de
Bruijn, Sashiko).
- Read the vif flags once per packet.
- Name the fixed-ID case shared by every iptunnel_handle_offloads()
caller (Sashiko).
- v6: https://lore.kernel.org/netdev/ffd8ca7abd44882707fbca297889f6e43ced4a65.1795295554.git.julius@bairaktaris.de/
net/ipv4/ipmr.c | 78 ++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 70 insertions(+), 8 deletions(-)
diff --git a/net/ipv4/ipmr.c b/net/ipv4/ipmr.c
index 836f3f95b20c..fc2ba155a9dc 100644
--- a/net/ipv4/ipmr.c
+++ b/net/ipv4/ipmr.c
@@ -1869,6 +1869,10 @@ static void ip_encap(struct net *net, struct sk_buff *skb,
struct iphdr *iph;
const struct iphdr *old_iph = ip_hdr(skb);
+ /* skb_cow() in ipmr_prepare_xmit() leaves no header to unclone */
+ iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4);
+ skb_set_inner_ipproto(skb, IPPROTO_IPIP);
+
skb_push(skb, sizeof(struct iphdr));
skb->transport_header = skb->network_header;
skb_reset_network_header(skb);
@@ -1882,8 +1886,8 @@ static void ip_encap(struct net *net, struct sk_buff *skb,
iph->saddr = saddr;
iph->protocol = IPPROTO_IPIP;
iph->ihl = 5;
- iph->tot_len = htons(skb->len);
- ip_select_ident(net, skb, NULL);
+ iph_set_totlen(iph, skb->len);
+ ip_select_ident_segs(net, skb, NULL, skb_shinfo(skb)->gso_segs ?: 1);
ip_send_check(iph);
memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
@@ -1928,7 +1932,8 @@ static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
/* Processing handlers for ipmr_forward, under rcu_read_lock() */
static int ipmr_prepare_xmit(struct net *net, struct mr_table *mrt,
- struct sk_buff *skb, int vifi)
+ struct sk_buff *skb, int vifi,
+ unsigned short flags)
{
const struct iphdr *iph = ip_hdr(skb);
struct vif_device *vif = &mrt->vif_table[vifi];
@@ -1941,7 +1946,7 @@ static int ipmr_prepare_xmit(struct net *net, struct mr_table *mrt,
if (!vif_dev)
return -1;
- if (vif->flags & VIFF_REGISTER) {
+ if (flags & VIFF_REGISTER) {
WRITE_ONCE(vif->pkt_out, vif->pkt_out + 1);
WRITE_ONCE(vif->bytes_out, vif->bytes_out + skb->len);
DEV_STATS_ADD(vif_dev, tx_bytes, skb->len);
@@ -1950,7 +1955,7 @@ static int ipmr_prepare_xmit(struct net *net, struct mr_table *mrt,
return -1;
}
- if (vif->flags & VIFF_TUNNEL) {
+ if (flags & VIFF_TUNNEL) {
rt = ip_route_output_ports(net, &fl4, NULL,
vif->remote, vif->local,
0, 0,
@@ -1985,6 +1990,17 @@ static int ipmr_prepare_xmit(struct net *net, struct mr_table *mrt,
return -1;
}
+ /* Complete the pending checksum of an encapsulated packet, as for
+ * a packet sent through an ipip device. This may reallocate the
+ * head: no pointer into it is used after this point.
+ */
+ if ((flags & VIFF_TUNNEL) && skb->encapsulation &&
+ skb->ip_summed == CHECKSUM_PARTIAL &&
+ skb_csum_hwoffload_help(skb, 0)) {
+ ip_rt_put(rt);
+ return -1;
+ }
+
WRITE_ONCE(vif->pkt_out, vif->pkt_out + 1);
WRITE_ONCE(vif->bytes_out, vif->bytes_out + skb->len);
@@ -1995,7 +2011,7 @@ static int ipmr_prepare_xmit(struct net *net, struct mr_table *mrt,
/* FIXME: forward and output firewalls used to be called here.
* What do we do with netfilter? -- RR
*/
- if (vif->flags & VIFF_TUNNEL) {
+ if (flags & VIFF_TUNNEL) {
ip_encap(net, skb, vif->local, vif->remote);
/* FIXME: extra output firewall step used to be here. --RR */
DEV_STATS_INC(vif_dev, tx_packets);
@@ -2005,15 +2021,49 @@ static int ipmr_prepare_xmit(struct net *net, struct mr_table *mrt,
return 0;
}
+/* ip_encap() describes its IPIP header with the inner header offsets of
+ * the skb, which an encapsulated packet already uses for its own tunnel.
+ * An ipip device offers no tunnel offloads, so validate_xmit_skb()
+ * segments such a GSO packet before ipip_tunnel_xmit() adds the header.
+ * Do the same for a tunnel vif.
+ */
+static bool ipmr_needs_segment(const struct sk_buff *skb, unsigned short flags)
+{
+ return (flags & VIFF_TUNNEL) && skb->encapsulation && skb_is_gso(skb);
+}
+
+static struct sk_buff *ipmr_segment(struct sk_buff *skb)
+{
+ struct sk_buff *segs = skb_gso_segment(skb, 0);
+
+ if (IS_ERR_OR_NULL(segs)) {
+ kfree_skb(skb);
+ return NULL;
+ }
+ consume_skb(skb);
+ return segs;
+}
+
static void ipmr_queue_fwd_xmit(struct net *net, struct mr_table *mrt,
int in_vifi, struct sk_buff *skb, int vifi)
{
+ unsigned short flags = READ_ONCE(mrt->vif_table[vifi].flags);
+ struct sk_buff *segs, *next;
struct rtable *rt;
if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
goto out_free;
- if (ipmr_prepare_xmit(net, mrt, skb, vifi))
+ if (ipmr_needs_segment(skb, flags)) {
+ segs = ipmr_segment(skb);
+ skb_list_walk_safe(segs, skb, next) {
+ skb_mark_not_on_list(skb);
+ ipmr_queue_fwd_xmit(net, mrt, in_vifi, skb, vifi);
+ }
+ return;
+ }
+
+ if (ipmr_prepare_xmit(net, mrt, skb, vifi, flags))
goto out_free;
rt = skb_rtable(skb);
@@ -2042,7 +2092,19 @@ static void ipmr_queue_fwd_xmit(struct net *net, struct mr_table *mrt,
static void ipmr_queue_output_xmit(struct net *net, struct mr_table *mrt,
struct sk_buff *skb, int vifi)
{
- if (ipmr_prepare_xmit(net, mrt, skb, vifi))
+ unsigned short flags = READ_ONCE(mrt->vif_table[vifi].flags);
+ struct sk_buff *segs, *next;
+
+ if (ipmr_needs_segment(skb, flags)) {
+ segs = ipmr_segment(skb);
+ skb_list_walk_safe(segs, skb, next) {
+ skb_mark_not_on_list(skb);
+ ipmr_queue_output_xmit(net, mrt, skb, vifi);
+ }
+ return;
+ }
+
+ if (ipmr_prepare_xmit(net, mrt, skb, vifi, flags))
goto out_free;
ip_mc_output(net, NULL, skb);
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread