seg6: reallocate the skb head on L2 encapsulation only when needed
Subsystems (3)
PATCH v3 1/1 seg6: reallocate the skb head on L2 encapsulation only when needed
The L2 encapsulation modes of the seg6 lwtunnel reallocate the skb head on every packet, where the IPv6 encapsulation modes reallocate only when they have to. Ask for the whole encapsulation up front instead, so that the reallocation happens at most once and only when the headroom really is too small:
skb->mac_len + sizeof(struct ipv6hdr) + ipv6_optlen(tinfo->srh)
+ dst_dev_overhead(cache_dst, skb)
__seg6_do_srh_encap() then finds the room it needs and its own skb_cow_head() becomes a no-op.
Drivers reserve more than that on the forwarding path, so the reallocation usually disappears altogether. A single-segment policy on ixgbe needs
14 (mac_len) + 40 (ipv6hdr) + 24 (SRH) + 16 (LL_RESERVED_SPACE) = 94
against the 206 bytes the driver leaves. Where the headroom is smaller, as on a veth pair, pskb_expand_head() is called once per forwarded packet instead of twice. Asking only for skb->mac_len would still take two whenever the skb is header-cloned, because the cow that unclones it does not also make room for the outer header.
The cost is amplified by CONFIG_INIT_ON_ALLOC_DEFAULT_ON, which many distributions enable: every new head is zeroed in full, and that memset alone accounts for 16% of the datapath profile.
Throughput at 0.5% packet loss, 64-byte frames forwarded through one 2.30 GHz core (Xeon E5-2650 v3, ixgbe 82599ES), offered by TRex and binary-searched over 10 runs of 10 s:
Before: 654.6 kpps
After: 965.7 kpps
---
Changes in v3:
- No code change. Rebased onto net-next, which now has 87cd6b717e40 ("net: ipv6: keep room for the mac header in dst_dev_overhead()"). That fixes the headroom shortfall Sashiko reported on v2, which predates this patch and affects every seg6, ioam6 and rpl encapsulation.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Ask for the whole encapsulation headroom at once, so that a cloned
skb no longer takes a second reallocation inside
__seg6_do_srh_encap() [Eric]
- Re-measure against unpatched net-next rather than an older base
- Link to v1: https://lore.kernel.org/r/[email protected]
--- net/ipv6/seg6_iptunnel.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
net/ipv6/seg6_iptunnel.c +8 -2
@@ -400,6 +400,7 @@ static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *cache_dst)struct dst_entry *dst =skb_dst (skb);struct seg6_iptunnel_encap *tinfo;struct seg6_lwt *slwt; +unsigned int headroom;int proto, err =0 ; slwt =seg6_lwt_lwtunnel (dst->lwtstate);
@@ -446,8 +447,13 @@ static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *cache_dst)if (!skb_mac_header_was_set (skb))return -EINVAL ; -if (pskb_expand_head (skb, skb->mac_len,0 ,GFP_ATOMIC ) <0 ) -return -ENOMEM ; + headroom = skb->mac_len +sizeof (struct ipv6hdr ) + +ipv6_optlen (tinfo->srh) + +dst_dev_overhead (cache_dst, skb); + + err =skb_cow_head (skb, headroom); +if (unlikely (err)) +return err;skb_mac_header_rebuild (skb);skb_push (skb, skb->mac_len);
---
base-commit: 42a9fb3382fc2573e92f41d203b095d9a372cfc9
change-id: 20260902-seg6-l2cow-77dc3ba41232
Best regards,
Signature · 1 line
Yuya Kusakabe <[email protected]>
Quoted 90 lines · Yuya Kusakabe
The L2 encapsulation modes of the seg6 lwtunnel reallocate the skb head on every packet, where the IPv6 encapsulation modes reallocate only when they have to. Ask for the whole encapsulation up front instead, so that the reallocation happens at most once and only when the headroom really is too small:
skb->mac_len+ sizeof(struct ipv6hdr) +ipv6_optlen(tinfo->srh)
+dst_dev_overhead(cache_dst, skb)
__seg6_do_srh_encap()then finds the room it needs and its ownskb_cow_head()becomes a no-op.Drivers reserve more than that on the forwarding path, so the reallocation usually disappears altogether. A single-segment policy on ixgbe needs
14 (
mac_len) + 40 (ipv6hdr) + 24 (SRH) + 16 (LL_RESERVED_SPACE) = 94against the 206 bytes the driver leaves. Where the headroom is smaller, as on a veth pair,
pskb_expand_head()is called once per forwarded packet instead of twice. Asking only forskb->mac_lenwould still take two whenever the skb is header-cloned, because the cow that unclones it does not also make room for the outer header.The cost is amplified by
CONFIG_INIT_ON_ALLOC_DEFAULT_ON, which many distributions enable: every new head is zeroed in full, and that memset alone accounts for 16% of the datapath profile.Throughput at 0.5% packet loss, 64-byte frames forwarded through one 2.30 GHz core (Xeon E5-2650 v3, ixgbe 82599ES), offered by TRex and binary-searched over 10 runs of 10 s:
Before: 654.6 kpps
After: 965.7 kppsAssisted-by LLMSigned-off-by Yuya Kusakabe <[email protected]>Reviewed-by Eric Dumazet <[email protected]>---
Changes in v3:
- No code change. Rebased onto net-next, which now has87cd6b717e40("net: ipv6: keep room for the mac header indst_dev_overhead()"). That fixes the headroom shortfall Sashiko reported on v2, which predates this patch and affects every seg6, ioam6 and rpl encapsulation.
- Link to v2: https://patch.msgid.link/[email protected]Changes in v2:
- Ask for the whole encapsulation headroom at once, so that a cloned
skb no longer takes a second reallocation inside
__seg6_do_srh_encap()[Eric]
- Re-measure against unpatched net-next rather than an older base
- Link to v1: https://lore.kernel.org/r/[email protected]--- net/ipv6/seg6_iptunnel.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)net/ipv6/seg6_iptunnel.c +8
-3@@ -400,6 +400,7 @@ static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *cache_dst)struct dst_entry *dst =skb_dst (skb);struct seg6_iptunnel_encap *tinfo;struct seg6_lwt *slwt; +unsigned int headroom;int proto, err =0 ; slwt =seg6_lwt_lwtunnel (dst->lwtstate);@@ -446,8 +447,13 @@ static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *cache_dst)if (!skb_mac_header_was_set (skb))return -EINVAL ; -if (pskb_expand_head (skb, skb->mac_len,0 ,GFP_ATOMIC ) <0 ) -return -ENOMEM ; + headroom = skb->mac_len +sizeof (struct ipv6hdr ) + +ipv6_optlen (tinfo->srh) + +dst_dev_overhead (cache_dst, skb); + + err =skb_cow_head (skb, headroom); +if (unlikely (err)) +return err;skb_mac_header_rebuild (skb);skb_push (skb, skb->mac_len); ---base-commit:
42a9fb3382fc2573e92f41d203b095d9a372cfc9
change-id: 20260902-seg6-l2cow-77dc3ba41232
Quoted 4 lines
Best regards,
Signature · 1 line
Yuya Kusakabe <[email protected]>
LGTM
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Fri, 25 Sep 2026 23:49:53 +0900 you wrote:
The L2 encapsulation modes of the seg6 lwtunnel reallocate the skb head on every packet, where the IPv6 encapsulation modes reallocate only when they have to. Ask for the whole encapsulation up front instead, so that the reallocation happens at most once and only when the headroom really is too small:
skb->mac_len+ sizeof(struct ipv6hdr) +ipv6_optlen(tinfo->srh)
+dst_dev_overhead(cache_dst, skb)[...]
Here is the summary with links:
- [net-next,v3] seg6: reallocate the skb head on L2 encapsulation only when needed
https://git.kernel.org/netdev/net-next/c/bf515c9fae87
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html