Thread View: linux.kernel.netdev
6 messages
6 total messages
Started by Willem de Bruijn
Sat, 07 Nov 2026 14:51
Re: [PATCH net-next 1/8] net: add tx_extra_headroom and LL_ALLOC_SPACE()
Author: Willem de Bruijn
Date: Sat, 07 Nov 2026 14:51
Date: Sat, 07 Nov 2026 14:51
110 lines
18214 bytes
18214 bytes
Tianyi Gao wrote:
> From: David Keogh <dkeogh@amazon.com>
>
> Programs attached at TC egress commonly push additional headers onto a
> packet, for example to encapsulate it. When the packet does not carry
> enough headroom the BPF helpers reallocate the head, which costs an
> allocation and a copy per packet.
>
> Locally generated UDP, raw and AF_PACKET traffic is allocated with
> exactly LL_RESERVED_SPACE() of headroom, which only covers the
> link-layer header and the driver's needed_headroom. An administrator can
> only ask for more headroom at allocation time if the driver supports it,
> as netkit does with IFLA_NETKIT_HEADROOM. veth does not.
>
> Add a tx_extra_headroom field to struct net_device for that purpose,
Main high level concern is just adding an extra field. What's next,
do we need to add a field extra_extra_headroom?
> together with LL_ALLOC_SPACE() and LL_ALLOC_SPACE_EX(). These add the
Since this already changes callers from LL_RESERVED_SPACE to
LL_ALLOC_SPACE, could this additional use case be integrated more in
the struct fields and helpers that are already there?
Admittedly, I did not spend much time looking at that deeply, and you
likely did, so the answer is no. But I want to poke a little at that
before we add yet another optional length field.
> extra reservation to the header length before the HH_DATA_MOD alignment,
> so the alignment of the link-layer header does not change.
>
> The new macros are intended only for sites that allocate new packets.
> LL_RESERVED_SPACE() keeps its current meaning and remains the threshold
> used when checking whether an existing packet must be expanded, so the
> extra reservation never forces a reallocation on its own: packets that
> already have enough room for the headers being pushed, such as TCP
> segments built with MAX_TCP_HEADER, are left alone.
>
> Keep the extra reservation separate from needed_headroom so that driver
> updates of their own requirement do not clobber it and so that it can
> be cleared independently. Place the field in the padding that follows
> needed_headroom, which keeps it in the TX read-mostly cacheline group.
>
> The field is not set anywhere yet. A later patch exposes it through
> rtnetlink.
>
> Also remove LL_RESERVED_SPACE_EXTRA(). It has had no user since
> commit 6d5431555de0 ("caif: remove CAIF NETWORK LAYER"). It could add
> the extra headroom, but it reads dev->hard_header_len itself. AF_PACKET
> must use the value that it read once, see commit 03390aa32e66 ("packet:
> use consistent hard_header_len in non-ring send paths").
> LL_ALLOC_SPACE_EX() takes the header length as an argument instead.
>
> Signed-off-by: David Keogh <dkeogh@amazon.com>
> [tianyi: remove LL_RESERVED_SPACE_EXTRA()]
> Signed-off-by: Tianyi Gao <tianyi@cloudflare.com>
> ---
> checkpatch --strict reports "Macro argument reuse 'dev'" for
> LL_ALLOC_SPACE_EX() and LL_ALLOC_SPACE(). It reports the same for
> LL_RESERVED_SPACE().
> ---
> .../networking/net_cachelines/net_device.rst | 1 +
> include/linux/netdevice.h | 36 +++++++++++++++++--
> net/core/dev.c | 2 ++
> 3 files changed, 36 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/networking/net_cachelines/net_device.rst b/Documentation/networking/net_cachelines/net_device.rst
> index 8eceaa80b686..a7e311fb7875 100644
> --- a/Documentation/networking/net_cachelines/net_device.rst
> +++ b/Documentation/networking/net_cachelines/net_device.rst
> @@ -35,6 +35,7 @@ unsigned_short gflags
> unsigned_short hard_header_len read_mostly read_mostly ip6_xmit(tx);gro_list_prepare(rx)
> unsigned_int mtu read_mostly ip_finish_output2
> unsigned_short needed_headroom read_mostly LL_RESERVED_SPACE/ip_finish_output2
> +unsigned_short tx_extra_headroom read_mostly LL_ALLOC_SPACE/__ip_append_data
> unsigned_short needed_tailroom
> netdev_features_t features read_mostly read_mostly HARD_TX_LOCK,netif_skb_features,sk_setup_caps(tx);netif_elide_gro(rx)
> netdev_features_t hw_features
> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 435be7880d7e..98d1501d6fee 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -311,9 +311,24 @@ struct hh_cache {
> & ~(HH_DATA_MOD - 1)) + HH_DATA_MOD)
> #define LL_RESERVED_SPACE(dev) \
> LL_RESERVED_SPACE_EX(dev, (dev)->hard_header_len)
> -#define LL_RESERVED_SPACE_EXTRA(dev,extra) \
> - ((((dev)->hard_header_len + READ_ONCE((dev)->needed_headroom) + (extra)) \
> - & ~(HH_DATA_MOD - 1)) + HH_DATA_MOD)
> +
> +/*
> + * Headroom to reserve when allocating a locally generated packet for
> + * transmission through a device. On top of LL_RESERVED_SPACE() this adds
> + * the administrator-configured tx_extra_headroom, folded in before the
> + * alignment so that the link-layer header placement does not change.
> + *
> + * Only use this for new allocations. Code that checks whether an existing
> + * packet has enough headroom must keep using LL_RESERVED_SPACE(), so that
> + * the extra reservation can never force a reallocation by itself.
> + */
> +#define LL_ALLOC_SPACE_EX(dev, hlen) \
> + LL_RESERVED_SPACE_EX(dev, (hlen) + READ_ONCE((dev)->tx_extra_headroom))
> +#define LL_ALLOC_SPACE(dev) \
> + LL_ALLOC_SPACE_EX(dev, (dev)->hard_header_len)
> +
If having to modify/add these helpers, it's probably a good time to
switch to actual, type safe and readable, functions instead.
Re: [PATCH net-next 1/8] net: add tx_extra_headroom and LL_ALLOC_SPACE()
Author: Willem de Bruijn
Date: Sun, 08 Nov 2026 15:13
Date: Sun, 08 Nov 2026 15:13
58 lines
14678 bytes
14678 bytes
Tianyi Gao wrote: > On Sat, Nov 07, 2026 at 09:51:07AM -0500, Willem de Bruijn wrote: > > Tianyi Gao wrote: > > > From: David Keogh <dkeogh@amazon.com> > > > > > > Add a tx_extra_headroom field to struct net_device for that purpose, > > > > Main high level concern is just adding an extra field. What's next, > > do we need to add a field extra_extra_headroom? > > Thanks for the review. > > A user needs more room either in every packet or only in new skbs. > needed_headroom covers the first case and tx_extra_headroom the > second. I do not see a third case. > > needed_headroom does not only set the headroom of new skbs. It is also > the minimum that ip_finish_output2() and ip_forward() require of > existing skbs. Raising it for new skbs also raises that minimum. With > XDP on a veth peer and needed_headroom raised to 256, throughput drops > by 2.8% to 5.8% for local TCP and by 4.0% to 20.8% for forwarded > traffic. The cover letter has the detailed analysis. veth also cannot > set needed_headroom from user space today. > > pahole on x86_64 (defconfig, net/core/dev.o) shows that the field fills > the 2-byte hole after needed_headroom, so struct net_device does not > grow: > > before: > unsigned short needed_headroom; /* 60 2 */ > /* XXX 2 bytes hole, try to pack */ > /* size: 2688, cachelines: 42, members: 187 */ > > after: > unsigned short needed_headroom; /* 60 2 */ > unsigned short tx_extra_headroom; /* 62 2 */ > /* size: 2688, cachelines: 42, members: 188 */ Thanks. Pushing back on another design choice: the UAPI change. That both cannot be undone, and is fragile and laborious, requiring manual admin arithmetic. The RFC patch mentioned two other approaches: explicit headroom requirement labeling for tc and BPF, and dynamic adjustment of this field whenever a realloc in the stack is encountered. They are left for future work. But should those instead be implemented first? As said, this manual hackery cannot be undone. While in-kernel mechanisms can be refined and built upon. It seems to be that this series is trying to address a few related situations and argues that no single solution fixes all except for manual adjustment. But likely a combination of the above (and more) will. This does leave the veth peer to solve, but that does not seem fundamentally intractable.
Re: [PATCH net-next 1/8] net: add tx_extra_headroom and LL_ALLOC_SPACE()
Author: Stephen Hemminger
Date: Mon, 09 Nov 2026 03:08
Date: Mon, 09 Nov 2026 03:08
109 lines
17740 bytes
17740 bytes
On Sun, 08 Nov 2026 10:13:03 -0500 Willem de Bruijn <willemdebruijn.kernel@gmail.com> wrote: > Tianyi Gao wrote: > > On Sat, Nov 07, 2026 at 09:51:07AM -0500, Willem de Bruijn wrote: > > > Tianyi Gao wrote: > > > > From: David Keogh <dkeogh@amazon.com> > > > > > > > > Add a tx_extra_headroom field to struct net_device for that purpose, > > > > > > Main high level concern is just adding an extra field. What's next, > > > do we need to add a field extra_extra_headroom? > > > > Thanks for the review. > > > > A user needs more room either in every packet or only in new skbs. > > needed_headroom covers the first case and tx_extra_headroom the > > second. I do not see a third case. > > > > needed_headroom does not only set the headroom of new skbs. It is also > > the minimum that ip_finish_output2() and ip_forward() require of > > existing skbs. Raising it for new skbs also raises that minimum. With > > XDP on a veth peer and needed_headroom raised to 256, throughput drops > > by 2.8% to 5.8% for local TCP and by 4.0% to 20.8% for forwarded > > traffic. The cover letter has the detailed analysis. veth also cannot > > set needed_headroom from user space today. > > > > pahole on x86_64 (defconfig, net/core/dev.o) shows that the field fills > > the 2-byte hole after needed_headroom, so struct net_device does not > > grow: > > > > before: > > unsigned short needed_headroom; /* 60 2 */ > > /* XXX 2 bytes hole, try to pack */ > > /* size: 2688, cachelines: 42, members: 187 */ > > > > after: > > unsigned short needed_headroom; /* 60 2 */ > > unsigned short tx_extra_headroom; /* 62 2 */ > > /* size: 2688, cachelines: 42, members: 188 */ > > Thanks. > > Pushing back on another design choice: the UAPI change. That both cannot be > undone, and is fragile and laborious, requiring manual admin arithmetic. I agree with Willem. This is heading the wrong direction. The more wordy AI analysis is from the iproute2 patch side: Network configuration should not require pre-guessing this value. To set it correctly an administrator must know which TC egress programs are attached, how many bytes each pushes, and whether XDP is attached on the veth peer. That is a kernel-internal sizing decision being pushed out to user space, where the information is not reliably available. Worse, it goes stale silently: attach another encap action or move the peer to a different netns, and the value that was right at "ip link add" time is now wrong with nothing reporting it. The reply that a stale value only costs performance is true but does not rescue the design. The entire purpose of the field is performance. A setting that quietly stops delivering the benefit it exists for is one operators carry indefinitely without knowing whether it still helps. Failing quietly in the only dimension the knob was added for is the cost, not a mitigation. Since the motivating case is XDP, the mechanism should let the XDP program ask for the padding it needs, rather than making the operator translate the program's requirement into a device setting by hand. XDP already guarantees XDP_PACKET_HEADROOM to its programs. The requirement is known to the kernel, in the right units, at the moment of attach. Propagating it from the attach point beats an administrator's arithmetic and cannot go stale, because detach undoes it. Your own reply already works this out: > - XDP on a veth peer: veth_xdp_set() and generic_xdp_install() can > increase the headroom of the peer to XDP_PACKET_HEADROOM on attach > and reset it on detach. That is the mechanism, and it needs no uapi, no operator arithmetic, and no stale state. I would rather see it implemented for the cases it covers than add a permanent uapi that removes the incentive to do so. To be clear on what I am not asking for: this is not auto-tuning. Your objections to auto-tuning are good ones. The kmalloc-2048 to kmalloc-4096 cliff being crossed without anyone deciding to is a real problem, and the reset-on-detach bookkeeping across namespaces is real work. Attach-time propagation is a different thing: a known requirement, declared at a known point, with a natural reset. On the argument that program-side declaration is itself new uapi, and that the labeled device is often not the device the skb was allocated on: granted for the general TC egress case. It does not apply to the attach-time propagation above, which is in-kernel plumbing rather than uapi. Which leaves the genuinely hard case, the one you describe at Cloudflare: a service in a netns sends through a veth pair, the host forwards to the uplink, and a TC egress program there encapsulates, after ip_rcv_core() has orphaned the skb so the allocating device cannot be identified. If that is the real residue, make it the problem being solved and argue for the narrowest mechanism that solves it. As posted, the series leads with cases that in-kernel propagation handles and uses them to justify an interface sized for the case it does not.
Re: [PATCH net-next 1/8] net: add tx_extra_headroom and LL_ALLOC_SPACE()
Author: Jesper Dangaard Brouer
Date: Tue, 10 Nov 2026 14:48
Date: Tue, 10 Nov 2026 14:48
19 lines
10039 bytes
10039 bytes
On 11/8/26 22:43, Tianyi Gao wrote: > - That growth can move skbs into a larger kmalloc size without anyone > deciding it. On x86_64, for 1500-byte MTU UDP over IPv4 on an > Ethernet device, 194 bytes or more moves the skb head from > kmalloc-2048 to kmalloc-4096. That adds 2048 bytes to the memory and > the truesize of each such skb. The IFLA_TX_EXTRA_HEADROOM UAPI can > do the same, but then the operator chose to pay that cost. This reminds me that I have previously proposed changing both the generic XDP and veth headroom check to use 192 bytes instead of 256 bytes. This makes sense because Intel driver for native XDP already set this to 192 bytes. Exactly due to 1500 MTU packets will fit into a 2048 alignment. e.g. 2048 -192 -320 (shared_info) = 1536 bytes left. Reading about, this would also translate into a performance benefit? --Jesper
Re: [PATCH net-next 1/8] net: add tx_extra_headroom and LL_ALLOC_SPACE()
Author: Willem de Bruijn
Date: Tue, 10 Nov 2026 16:23
Date: Tue, 10 Nov 2026 16:23
64 lines
15756 bytes
15756 bytes
Tianyi Gao wrote: > Hi Stephen, > > Thanks for the invaluable and detailed review. > > On Sun, 8 Nov 2026 19:08:31 -0800, Stephen Hemminger wrote: > > Since the motivating case is XDP, the mechanism should let the XDP program > > ask for the padding it needs, rather than making the operator translate > > the program's requirement into a device setting by hand. > > The series started from David Keogh's TC egress case, as described in > the RFC [1] and in the first paragraph of the v1 cover letter. The veth > XDP case came later, when I found that the same field also avoids the > copy for generic XDP on veth. > > > Your own reply already works this out: > > > > > - XDP on a veth peer: veth_xdp_set() and generic_xdp_install() can > > > increase the headroom of the peer to XDP_PACKET_HEADROOM on attach > > > and reset it on detach. > > > > That is the mechanism, and it needs no uapi, no operator arithmetic, and > > no stale state. I would rather see it implemented for the cases it covers > > than add a permanent uapi that removes the incentive to do so. > > Agreed. For this case, I plan to move to this in v2. When an XDP program > is attached to a veth device, tx_extra_headroom of the peer is set to > XDP_PACKET_HEADROOM. > > > On the argument that program-side declaration is itself new uapi, and that > > the labeled device is often not the device the skb was allocated on: > > granted for the general TC egress case. > > For TC egress, the kernel does not know at attach time how many bytes > the program pushes. Only the program, or the software that loads it, > knows. Attach-time propagation works for XDP because XDP_PACKET_HEADROOM > is a fixed value. > > Since we both agree that auto-tuning has real problems, a narrow UAPI, > such as a BPF-side declaration, seems unavoidable for TC egress. The > program declares the headroom that it needs through this UAPI. The > initial plan is a BTF decl_tag, as Dragos Tatulea suggested on the RFC > [2]. On attach, the kernel increases tx_extra_headroom of the device > that the program is attached to. > > > Which leaves the genuinely hard case, the one you describe at Cloudflare: a > > service in a netns sends through a veth pair, the host forwards to the > > uplink, and a TC egress program there encapsulates, after ip_rcv_core() > > has orphaned the skb so the allocating device cannot be identified. If > > that is the real residue, make it the problem being solved and argue for > > the narrowest mechanism that solves it. > > This case only came up after I started to look at in-kernel approaches, > because the general IFLA_TX_EXTRA_HEADROOM UAPI already solves it. It is > not a new case, and it is not uncommon. Cilium, Flannel and Calico use a > tunnel by default [3][4][5], so the device that encapsulates is not the > device that allocated the skb. I have some hope that if there is sufficient reason, this backlinking to the originating netdev (or socket?) can eventually be addressed as well. Rather than work around it. The skb_orphan and iif issues are well know. But not set in stone.
Re: [PATCH net-next 1/8] net: add tx_extra_headroom and LL_ALLOC_SPACE()
Author: Willem de Bruijn
Date: Wed, 11 Nov 2026 03:27
Date: Wed, 11 Nov 2026 03:27
38 lines
14335 bytes
14335 bytes
Tianyi Gao wrote: > Hi Willem, > > On Tue, 10 Nov 2026 11:23:09 -0500, Willem de Bruijn wrote: > > I have some hope that if there is sufficient reason, this backlinking > > to the originating netdev (or socket?) can eventually be addressed as > > well. Rather than work around it. > > > > The skb_orphan and iif issues are well know. But not set in stone. > > Thank you for the suggestion. I looked at what backlinking would take. > > For the socket, ip_rcv_core() orphans the skb, and after that skb->sk > holds the receiving socket, for tproxy and early demux. Keeping the > sender's socket there would break that assumption. > > For the netdev, skb_iif is the last receiving device, not the > allocating one. So keeping the origin needs new state in the skb, > such as an skb extension. > > Even with a backlink, the kernel still needs to know when to adjust > tx_extra_headroom of the originating netdev. This brings back > auto-tuning, with the problems from my earlier reply. > > Taking a step back, IMHO it is more natural to set the extra headroom > indication on the netdev that the skb is allocated for, than to have > other devices raise it through a backlink, which can get messy. As said, the problem with that is that an admin has to do it, each time. Whereas the kernel has this information. > For v2, I would prefer the narrow VETH_INFO_TX_EXTRA_HEADROOM UAPI, > following what has been done for IFLA_NETKIT_HEADROOM. I will put it > in the last two patches, so the rest of the series can be taken > without it if you prefer. Ok. I don't expect this to be solved in this series.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads