Skip to contentSkip to conversation

net: configurable extra TX headroom for local egress

RFC In review David Keogh 5 participants 13 messages Oct 15 – Nov 6

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

cover net: configurable extra TX headroom for local egress

A program attached at TC egress commonly pushes additional headers onto a packet, for example to encapsulate it. Locally generated UDP, raw and AF_PACKET traffic is allocated with exactly LL_RESERVED_SPACE() of headroom, which covers the link-layer header plus the driver's needed_headroom and alignment, leaving no slack for anything an egress program pushes, so that push reallocates the skb head on every packet. There is currently no in-tree way for an administrator to ask for more headroom at allocation time.

Add a per-device tx_extra_headroom, configured through a new IFLA_TX_EXTRA_HEADROOM rtnetlink attribute. It is a separate value from
needed_headroom: it is added on top of LL_RESERVED_SPACE() at the allocation sites only (LL_ALLOC_SPACE()), it can be set and cleared freely without recreating the device, it is reported through its own attribute so it is never confused with the driver's requirement, and it is not inherited by stacked devices. needed_headroom keeps its current meaning and remains the threshold for deciding whether an existing packet must be expanded, so the extra reservation never forces a reallocation on its own.

The value is bounded to NETDEV_TX_EXTRA_HEADROOM_MAX (512 bytes) by the netlink policy, enough for several stacked encapsulations (for example VXLAN, GRE or IPIP) while keeping the growth of per-packet allocations modest; a request above it is rejected. The attribute is purely additive, so an older iproute2 that does not know it simply never sends it; configuring the value requires both kernel and iproute2 support, and old userspace that ignores unknown dump attributes is unaffected.

Why a new value rather than letting userspace raise needed_headroom: that was the first approach and it was dropped. needed_headroom is a floor the device and stacked devices depend on and is consulted on the expand-on-demand path (ip_finish_output2()), so raising it reallocates every packet that lacks the full reservation, including TCP segments, and it cannot be lowered without recreating the device. tx_extra_headroom sidesteps both problems by only affecting fresh allocations and by being freely clearable.

The reservation applies to the device the packet is allocated against (the one the route selects) at skb-allocation time. A GSO super-skb is therefore allocated once with the headroom in place; software segmentation copies that headroom into each segment, so the push lands in reserved space for every segment without a per-segment realloc. net/xdp/xsk.c is included among the converted paths: AF_XDP copy mode open-codes the same needed_headroom calculation, so it uses netdev_tx_alloc_headroom() to pick up the extra reservation consistently with the socket paths.

Because the reservation is applied only at the allocation sites, TCP is left
alone: TCP segments are built with MAX_TCP_HEADER already reserved and are checked against LL_RESERVED_SPACE(), not LL_ALLOC_SPACE(), so raising the knob does not reallocate them. Covering the TCP allocation path is deliberately not part of this series.

Measurements (net-next at commit 05b5019b0, veth, 500000 UDP datagrams, a 20-byte ip-in-ip push attached at TC egress, only tx_extra_headroom varied):

  tx_extra_headroom   pskb_expand_head reallocs / 500k
  0 (off)             500003   (~1 per packet)
  64                  4        (background)
  128                 2        (background)

Idle case, same rig with the knob set but no TC program attached: 500000 UDP datagrams produce 0 reallocs on the device. With nothing pushing headers there is nothing to expand, so a set-but-unused value costs only the reserved bytes per allocation, with no added reallocation or copy.

The small background counts at 64 and 128 are control traffic on the device (ARP/ND and pre-neighbour-resolution skbs), not the UDP push path; the idle case reads 0 because that run primes a permanent neighbour first.

The data-plane selftest also sweeps the knob across the exact headroom the push needs and shows the realloc count flip at the predicted byte boundary (veth LL_RESERVED_SPACE is 16; a 14-byte L2 header leaves 2 free, so a 20-byte push needs 18 more: reallocs persist at 17 and vanish at 18). A UDP-only encap leaves a concurrent TCP stream delivering cleanly with the realloc count flat as the knob is raised, confirming the TCP path is untouched.

Patch 1 adds the field and the LL_ALLOC_SPACE() macros. Patch 2 switches the local-egress allocation sites to them (UDP, raw, AF_PACKET and AF_XDP). Patch 3 exposes the attribute through rtnetlink with documentation and the rt-link spec entry. Patch 4 tests the attribute itself through YNL. Patches 5 and 6 add the data-plane test: two BPF programs and the YNL test that drives them. The BPF programs live under selftests/bpf/progs because that tree supplies the clang -target bpf toolchain and generated vmlinux.h the CO-RE fentry counter needs, while the test driver lives in selftests/net; this is noted here because it has a bearing on how the series is applied.

As an RFC question: the data-plane test spans two selftest trees because the fentry realloc counter is a CO-RE program that needs the vmlinux.h generated under tools/testing/selftests/bpf, while the driver lives in tools/testing/selftests/net. I would appreciate guidance on the preferred
structure: consolidate the whole data-plane test under selftests/bpf, or keep it in selftests/net without CO-RE so it builds self-contained. The core change (patches 1-3) is independent of this and targets net-next regardless.

David Keogh (6):
net: add tx_extra_headroom and LL_ALLOC_SPACE()
net: reserve extra TX headroom for local egress
rtnetlink: add IFLA_TX_EXTRA_HEADROOM
selftests: net: add tx_extra_headroom test selftests/bpf: add tx_extra_headroom data-plane programs selftests/net: add tx_extra_headroom data-plane test

 Documentation/netlink/specs/rt-link.yaml      |  12 ++
 .../networking/net_cachelines/net_device.rst  |   1 +
 Documentation/networking/netdevices.rst       |  34 +++
 include/linux/netdevice.h                     |  33 +++
 include/uapi/linux/if_link.h                  |   1 +
 net/core/dev.c                                |   2 +
 net/core/rtnetlink.c                          |  19 +-
 net/ipv4/ip_output.c                          |   2 +-
 net/ipv4/raw.c                                |   2 +-
 net/ipv6/ip6_output.c                         |   2 +-
 net/ipv6/raw.c                                |   2 +-
 net/packet/af_packet.c                        |   6 +-
 net/xdp/xsk.c                                 |   6 +-
 .../bpf/progs/tx_extra_headroom_probe.c       |  68 ++++++
 .../bpf/progs/tx_extra_headroom_tc.bpf.c      |  69 ++++++
 tools/testing/selftests/net/Makefile          |   2 +
 .../selftests/net/tx_extra_headroom.py        | 137 ++++++++++++
 .../net/tx_extra_headroom_dataplane.py        | 197 ++++++++++++++++++
 18 files changed, 585 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/tx_extra_headroom_probe.c
 create mode 100644 tools/testing/selftests/bpf/progs/tx_extra_headroom_tc.bpf.c
 create mode 100755 tools/testing/selftests/net/tx_extra_headroom.py
 create mode 100755 tools/testing/selftests/net/tx_extra_headroom_dataplane.py
Signature · 1 line
2.50.1
#1 lore raw

RFC 1/6 net: add tx_extra_headroom and LL_ALLOC_SPACE()

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 driver's own requirement in needed_headroom. There is currently no way for an administrator to ask for more headroom at allocation time without changing the driver.

Add a tx_extra_headroom field to struct net_device for that purpose, together with LL_ALLOC_SPACE() and LL_ALLOC_SPACE_EX(). These add the extra reservation on top of LL_RESERVED_SPACE() while keeping the link-layer header alignment unchanged.

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.

---
 .../networking/net_cachelines/net_device.rst  |  1 +
 include/linux/netdevice.h                     | 33 +++++++++++++++++++
 net/core/dev.c                                |  2 ++
 3 files changed, 36 insertions(+)
Documentation/networking/net_cachelines/net_device.rst +1 -0
index 8eceaa80b..a7e311fb7 100644
@@ -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
include/linux/netdevice.h +33 -0
index 8fbe3a5d8..9a2a12db1 100644
@@ -309,6 +309,24 @@ struct hh_cache {
 	((((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)
+
+/* Upper bound for tx_extra_headroom, in bytes. */
+#define NETDEV_TX_EXTRA_HEADROOM_MAX	512
+
 struct header_ops {
 	int	(*create) (struct sk_buff *skb, struct net_device *dev,
 			   unsigned short type, const void *daddr,
@@ -1925,6 +1943,10 @@ enum netdev_reg_state {
  *
  *	@needed_headroom: Extra headroom the hardware may need, but not in all
  *			  cases can this be guaranteed
+ *	@tx_extra_headroom: Additional headroom reserved when allocating
+ *			    locally generated packets for transmission,
+ *			    configured by the administrator. Not a driver
+ *			    requirement; see LL_ALLOC_SPACE()
  *	@needed_tailroom: Extra tailroom the hardware may need, but not in all
  *			  cases can this be guaranteed. Some cases also use
  *			  LL_MAX_HEADER instead to allocate the skb
@@ -2187,6 +2209,7 @@ struct net_device {
 	 */
 	unsigned int		mtu;
 	unsigned short		needed_headroom;
+	unsigned short		tx_extra_headroom;
 	struct netdev_tc_txq	tc_to_txq[TC_MAX_QUEUE];
 #ifdef CONFIG_XPS
 	struct xps_dev_maps __rcu *xps_maps[XPS_MAPS_MAX];
@@ -2758,6 +2781,16 @@ struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
 					 struct sk_buff *skb,
 					 struct net_device *sb_dev);
 
+/* Same as LL_ALLOC_SPACE() without link-layer header and alignment, for
+ * callers that size the headroom themselves.
+ */
+static inline unsigned int
+netdev_tx_alloc_headroom(const struct net_device *dev)
+{
+	return READ_ONCE(dev->needed_headroom) +
+	       READ_ONCE(dev->tx_extra_headroom);
+}
+
 /* returns the headroom that the master device needs to take in account
  * when forwarding to this dev
  */
net/core/dev.c +2 -0
index 8eb9bf1ff..a79f55b85 100644
@@ -13501,6 +13501,8 @@ static void __init net_dev_struct_check(void)
 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, num_tc);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, mtu);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, needed_headroom);
+	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx,
+				      tx_extra_headroom);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tc_to_txq);
 #ifdef CONFIG_XPS
 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, xps_maps);
Signature · 1 line
2.50.1
#2 lore raw

RFC 2/6 net: reserve extra TX headroom for local egress

In reply to David Keogh

Use LL_ALLOC_SPACE() instead of LL_RESERVED_SPACE() where the stack allocates packets on behalf of a local sender and sizes them for the output device:

- __ip_append_data() and __ip6_append_data(), covering UDP, ICMP, ping and raw sockets without IP_HDRINCL;
- raw_send_hdrinc() and rawv6_send_hdrinc();
- the three AF_PACKET transmit paths;
- AF_XDP copy mode, which open-codes the same calculation from needed_headroom.

These are the main paths where a packet's headroom is decided by the device's advertised requirement and nothing else. Adding the administrator's extra reservation here means such packets reach the TC egress hook with room for additional headers already in place.

Paths that check an existing packet against LL_RESERVED_SPACE() and expand it on demand, such as ip_finish_output2() and the tunnel transmit functions, are deliberately left alone. Using the larger value there would force a reallocation of every packet that lacks the full extra reservation, including TCP segments that already have enough room for the headers an egress program actually pushes.

---
 net/ipv4/ip_output.c   | 2 +-
 net/ipv4/raw.c         | 2 +-
 net/ipv6/ip6_output.c  | 2 +-
 net/ipv6/raw.c         | 2 +-
 net/packet/af_packet.c | 6 +++---
 net/xdp/xsk.c          | 6 ++++--
 6 files changed, 11 insertions(+), 9 deletions(-)
net/ipv4/ip_output.c +1 -1
index 3396f177a..bf718b8f6 100644
@@ -988,7 +988,7 @@ static int __ip_append_data(struct sock *sk,
 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
 	paged = !!cork->gso_size;
 
-	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
+	hh_len = LL_ALLOC_SPACE(rt->dst.dev);
 
 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
net/ipv4/raw.c +1 -1
index 9d01006d8..a1e72e634 100644
@@ -355,7 +355,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
 	if (flags&MSG_PROBE)
 		goto out;
 
-	hlen = LL_RESERVED_SPACE(rt->dst.dev);
+	hlen = LL_ALLOC_SPACE(rt->dst.dev);
 	tlen = rt->dst.dev->needed_tailroom;
 	skb = sock_alloc_send_skb(sk,
 				  length + hlen + tlen + 15,
net/ipv6/ip6_output.c +1 -1
index 25fe0ba98..a3626d5f0 100644
@@ -1490,7 +1490,7 @@ static int __ip6_append_data(struct sock *sk,
 	mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize;
 	orig_mtu = mtu;
 
-	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
+	hh_len = LL_ALLOC_SPACE(rt->dst.dev);
 
 	fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
 			(opt ? opt->opt_nflen : 0);
net/ipv6/raw.c +1 -1
index b965258cf..7f2da5581 100644
@@ -601,7 +601,7 @@ static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
 	struct sk_buff *skb;
 	int err;
 	struct rt6_info *rt = dst_rt6_info(*dstp);
-	int hlen = LL_RESERVED_SPACE(rt->dst.dev);
+	int hlen = LL_ALLOC_SPACE(rt->dst.dev);
 	int tlen = rt->dst.dev->needed_tailroom;
 
 	if (length > rt->dst.dev->mtu) {
net/packet/af_packet.c +3 -3
index 823e0911c..8790fd038 100644
@@ -2031,7 +2031,7 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 		goto out_unlock;
 
 	if (!skb) {
-		size_t reserved = LL_RESERVED_SPACE_EX(dev, hard_header_len);
+		size_t reserved = LL_ALLOC_SPACE_EX(dev, hard_header_len);
 		int tlen = dev->needed_tailroom;
 		unsigned int hhlen = dev->header_ops ? hard_header_len : 0;
 
@@ -2863,7 +2863,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
 			goto tpacket_error;
 
 		status = TP_STATUS_SEND_REQUEST;
-		hlen = LL_RESERVED_SPACE_EX(dev, hard_header_len);
+		hlen = LL_ALLOC_SPACE_EX(dev, hard_header_len);
 		tlen = dev->needed_tailroom;
 		if (vnet_hdr_sz) {
 			data += vnet_hdr_sz;
@@ -3090,7 +3090,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 		goto out_unlock;
 
 	err = -ENOBUFS;
-	hlen = LL_RESERVED_SPACE_EX(dev, hard_header_len);
+	hlen = LL_ALLOC_SPACE_EX(dev, hard_header_len);
 	tlen = dev->needed_tailroom;
 	linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
 	linear = max(linear, min_t(int, len, hard_header_len));
net/xdp/xsk.c +4 -2
index 33475b180..58d64025d 100644
@@ -1045,7 +1045,8 @@ static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
 	buffer = xsk_buff_raw_get_data(pool, addr);
 
 	if (!skb) {
-		hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(xs->dev->needed_headroom));
+		hr = max(NET_SKB_PAD,
+			 L1_CACHE_ALIGN(netdev_tx_alloc_headroom(xs->dev)));
 
 		skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
 		if (unlikely(!skb))
@@ -1128,7 +1129,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
 		len = desc->len;
 
 		if (!skb) {
-			hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
+			hr = max(NET_SKB_PAD,
+				 L1_CACHE_ALIGN(netdev_tx_alloc_headroom(dev)));
 			tr = dev->needed_tailroom;
 			skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
 			if (unlikely(!skb))
Signature · 1 line
2.50.1
#3 lore raw

RFC 3/6 rtnetlink: add IFLA_TX_EXTRA_HEADROOM

In reply to David Keogh

Expose tx_extra_headroom through a new IFLA_TX_EXTRA_HEADROOM u16 attribute. The value is bounded to NETDEV_TX_EXTRA_HEADROOM_MAX (512 bytes) by the netlink policy, which is plenty for several layers of encapsulation while keeping the growth of per-packet allocations modest.

The attribute can be given when creating a link and when changing one. It is included in link dumps and a change triggers the usual link notification. Zero is the default and resets the reservation.

IFLA_HEADROOM keeps reporting needed_headroom, the driver's own requirement, so userspace can tell the two apart.

The setting applies to the device the packet is allocated for, that is the device selected by the route, and is not inherited by stacked devices. Document that together with the allocation-only semantics in netdevices.rst and add the attribute to the rt-link spec.

---
 Documentation/netlink/specs/rt-link.yaml | 12 +++++++++
 Documentation/networking/netdevices.rst  | 34 ++++++++++++++++++++++++
 include/uapi/linux/if_link.h             |  1 +
 net/core/rtnetlink.c                     | 19 ++++++++++++-
 4 files changed, 65 insertions(+), 1 deletion(-)
Documentation/netlink/specs/rt-link.yaml +12 -0
index 81b586339..9da363df5 100644
@@ -1098,6 +1098,16 @@ attribute-sets:
         name: pacing-offload
         type: u32
         doc: Enable EDT pacing offload (0 - disabled, 1 - enabled).
+      -
+        name: tx-extra-headroom
+        type: u16
+        doc: |
+          Additional headroom, in bytes, reserved on top of the driver's
+          requirement when allocating locally generated packets for
+          transmission through this device. Zero (the default) disables
+          the extra reservation.
+        checks:
+          max: 512
   -
     name: prop-list-link-attrs
     subset-of: link-attrs
@@ -2471,6 +2481,7 @@ operations:
             - gso-ipv4-max-size
             - gro-ipv4-max-size
             - af-spec
+            - tx-extra-headroom
     -
       name: newlink-ntf
       doc: Notify that a link has been created
@@ -2567,6 +2578,7 @@ operations:
             - gso-ipv4-max-size
             - gro-ipv4-max-size
             - pacing-offload
+            - tx-extra-headroom
       dump:
         request:
           value: 18
Documentation/networking/netdevices.rst +34 -0
index 6a87f09e7..02d6b4d4b 100644
@@ -441,6 +441,40 @@ from a notifier.
 
 Drivers **must not** generate nested notifications of the ops-locked types.
 
+TX headroom reservation
+=======================
+
+``needed_headroom`` is the driver's own additional header-space requirement.
+It is written by the driver, or by a stacked device inheriting the value of
+the device below it, and is reported read-only through ``IFLA_HEADROOM``.
+
+``tx_extra_headroom`` is a separate reservation configured by the
+administrator through the ``IFLA_TX_EXTRA_HEADROOM`` rtnetlink attribute, a
+byte count from 0 to 512 that defaults to 0. It can be supplied when a link
+is created or changed, is reported in link dumps and change notifications,
+and setting it back to 0 removes it.
+
+The extra reservation is only applied when the stack allocates a locally
+generated packet for transmission through the device. ``LL_ALLOC_SPACE()``
+is ``LL_RESERVED_SPACE()`` plus ``tx_extra_headroom`` and is used by the
+UDP, raw, AF_PACKET and AF_XDP transmit paths. Its purpose is to give
+programs attached at TC egress room to push additional headers without
+having to reallocate the packet first.
+
+The setting is deliberately not part of ``LL_RESERVED_SPACE()``, which
+remains the threshold for deciding whether an already existing packet must
+be expanded. Packets that reach the device with less than the extra
+reservation, such as forwarded packets or TCP segments, are therefore not
+reallocated because of it. Consumers must keep their usual headroom and
+clone checks.
+
+The setting is not inherited through stacked devices. Packets are allocated
+against the device selected by the route, so configure it on the VLAN,
+bond or bridge device the route points at rather than on the device
+beneath it. It survives link down/up and network namespace moves and is
+independent of any TC or BPF attachment. It does not change the MTU or
+the bytes on the wire.
+
 NETDEV_INTERNAL symbol namespace
 ================================
 
include/uapi/linux/if_link.h +1 -0
index 245b36204..fb3a6b382 100644
@@ -382,6 +382,7 @@ enum {
 	IFLA_HEADROOM,
 	IFLA_TAILROOM,
 	IFLA_PACING_OFFLOAD,
+	IFLA_TX_EXTRA_HEADROOM,
 	__IFLA_MAX
 };
 
net/core/rtnetlink.c +18 -1
index 15bffafbf..fb691db8e 100644
@@ -1403,6 +1403,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
 	       + nla_total_size(4)  /* IFLA_PACING_OFFLOAD */
 	       + nla_total_size(2)  /* IFLA_HEADROOM */
 	       + nla_total_size(2)  /* IFLA_TAILROOM */
+	       + nla_total_size(2)  /* IFLA_TX_EXTRA_HEADROOM */
 	       + rtnl_dev_parent_size(dev)
 	       + 0;
 
@@ -2200,7 +2201,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
 	    nla_put_u16(skb, IFLA_HEADROOM,
 			READ_ONCE(dev->needed_headroom)) ||
 	    nla_put_u16(skb, IFLA_TAILROOM,
-			READ_ONCE(dev->needed_tailroom)))
+			READ_ONCE(dev->needed_tailroom)) ||
+	    nla_put_u16(skb, IFLA_TX_EXTRA_HEADROOM,
+			READ_ONCE(dev->tx_extra_headroom)))
 		goto nla_put_failure;
 
 	if (rtnl_fill_proto_down(skb, dev))
@@ -2361,6 +2364,8 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
 	[IFLA_HEADROOM]		= { .type = NLA_REJECT },
 	[IFLA_TAILROOM]		= { .type = NLA_REJECT },
 	[IFLA_PACING_OFFLOAD]	= NLA_POLICY_MAX(NLA_U32, 1),
+	[IFLA_TX_EXTRA_HEADROOM] =
+		NLA_POLICY_MAX(NLA_U16, NETDEV_TX_EXTRA_HEADROOM_MAX),
 };
 
 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
@@ -3368,6 +3373,15 @@ static int do_setlink(const struct sk_buff *skb, struct net_device *dev,
 		}
 	}
 
+	if (tb[IFLA_TX_EXTRA_HEADROOM]) {
+		u16 val = nla_get_u16(tb[IFLA_TX_EXTRA_HEADROOM]);
+
+		if (dev->tx_extra_headroom != val) {
+			WRITE_ONCE(dev->tx_extra_headroom, val);
+			status |= DO_SETLINK_NOTIFY;
+		}
+	}
+
 	if (tb[IFLA_OPERSTATE])
 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
 
@@ -3859,6 +3873,9 @@ struct net_device *rtnl_create_link(struct net *net, const char *ifname,
 		netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]));
 	if (tb[IFLA_GRO_IPV4_MAX_SIZE])
 		netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]));
+	if (tb[IFLA_TX_EXTRA_HEADROOM])
+		dev->tx_extra_headroom =
+			nla_get_u16(tb[IFLA_TX_EXTRA_HEADROOM]);
 
 	return dev;
 }
Signature · 1 line
2.50.1
#4 lore raw

RFC 4/6 selftests: net: add tx_extra_headroom test

In reply to David Keogh

Exercise the IFLA_TX_EXTRA_HEADROOM attribute through YNL: the default of zero, setting values up to the maximum and back to zero, rejection of a value above the maximum, supplying the attribute at link creation, the change notification, persistence across link down/up and across a network namespace move, and that a VLAN created on top of a configured device does not inherit the setting.

---
 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/tx_extra_headroom.py        | 137 ++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100755 tools/testing/selftests/net/tx_extra_headroom.py
tools/testing/selftests/net/Makefile +1 -0
index 54beea2e3..0eb9a69e9 100644
@@ -120,6 +120,7 @@ TEST_PROGS := \
 	test_vxlan_vnifiltering.sh \
 	tfo_passive.sh \
 	traceroute.sh \
+	tx_extra_headroom.py \
 	txtimestamp.sh \
 	udpgro.sh \
 	udpgro_bench.sh \
tools/testing/selftests/net/tx_extra_headroom.py new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.50.1
#5 lore raw

RFC 5/6 selftests/bpf: add tx_extra_headroom data-plane programs

In reply to David Keogh

Add two BPF programs for a tx_extra_headroom data-plane test:

* push_outer (SEC "tc") pushes a 20-byte outer header on UDP egress, forcing a head reallocation when the skb lacks the headroom.

* count_realloc (SEC "fentry/pskb_expand_head") counts those reallocs for one ifindex, selected via the cfg_ifindex map.

The test that loads them is added to selftests/net in the next patch.

---
 .../bpf/progs/tx_extra_headroom_probe.c       | 68 ++++++++++++++++++
 .../bpf/progs/tx_extra_headroom_tc.bpf.c      | 69 +++++++++++++++++++
 2 files changed, 137 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/tx_extra_headroom_probe.c
 create mode 100644 tools/testing/selftests/bpf/progs/tx_extra_headroom_tc.bpf.c
tools/testing/selftests/bpf/progs/tx_extra_headroom_probe.c new file +68 -0
new file mode 100644
index 000000000..a6ca66ef0
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * fentry realloc counter for the tx_extra_headroom data-plane selftest.
+ *
+ * count_realloc (fentry/pskb_expand_head) increments realloc_cnt[0] each time
+ * pskb_expand_head() runs for an skb whose egress device ifindex matches
+ * cfg_ifindex[0]. The ifindex filter scopes the count to the test interface so
+ * unrelated background reallocs are not counted. fentry is BTF/CO-RE based, so
+ * this attaches by its SEC name via "bpftool prog loadall ... autoattach" with
+ * no raw kprobe name, no bpftrace, and no user-space loader.
+ *
+ * The companion tc program (tx_extra_headroom_tc.bpf.c) is what forces the
+ * reallocation this program observes. Userspace writes cfg_ifindex[0] (the
+ * device under test) and resets realloc_cnt[0] before each run via bpftool,
+ * then reads realloc_cnt[0] after.
+ */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_tracing.h>
+
+/* realloc_cnt[0] = number of pskb_expand_head calls on the test interface. */
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u64);
+} realloc_cnt SEC(".maps");
+
+/* cfg_ifindex[0] = ifindex of the device under test (set by userspace). */
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} cfg_ifindex SEC(".maps");
+
+/*
+ * BPF_PROG unpacks the trampoline args, so skb is the first argument of
+ * pskb_expand_head(struct sk_buff *, int, int, gfp_t). Count only reallocs
+ * whose egress device matches the configured ifindex.
+ */
+SEC("fentry/pskb_expand_head")
+int BPF_PROG(count_realloc, struct sk_buff *skb)
+{
+	__u32 key = 0;
+	__u32 *want;
+	__u64 *cnt;
+	__u32 ifi;
+
+	want = bpf_map_lookup_elem(&cfg_ifindex, &key);
+	if (!want || *want == 0)
+		return 0;
+
+	/* CO-RE read: skb->dev may be NULL on some paths. */
+	if (!BPF_CORE_READ(skb, dev))
+		return 0;
+	ifi = BPF_CORE_READ(skb, dev, ifindex);
+	if (ifi != *want)
+		return 0;
+
+	cnt = bpf_map_lookup_elem(&realloc_cnt, &key);
+	if (cnt)
+		__sync_fetch_and_add(cnt, 1);
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/tx_extra_headroom_tc.bpf.c new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.50.1
#6 lore raw

RFC 6/6 selftests/net: add tx_extra_headroom data-plane test

In reply to David Keogh

The existing tx_extra_headroom test checks the IFLA_TX_EXTRA_HEADROOM attribute itself. Add a test for its effect: with the knob at zero an egress header-push reallocates the skb head on every packet, and with the knob raised the realloc is eliminated. A second case checks that the cliff sits at exactly the headroom the push needs.

The test loads the two BPF programs from the previous patch, attaching count_realloc by SEC name with "bpftool prog loadall ... autoattach" and reading realloc_cnt via bpf_map_dump(). The veth peer is kept in its own netns so the datagram egresses the device under test, and a permanent neighbor is primed so UDP is not dropped before egress.

---
 tools/testing/selftests/net/Makefile          |   1 +
 .../net/tx_extra_headroom_dataplane.py        | 197 ++++++++++++++++++
 2 files changed, 198 insertions(+)
 create mode 100755 tools/testing/selftests/net/tx_extra_headroom_dataplane.py
tools/testing/selftests/net/Makefile +1 -0
index 0eb9a69e9..28b0444ba 100644
@@ -121,6 +121,7 @@ TEST_PROGS := \
 	tfo_passive.sh \
 	traceroute.sh \
 	tx_extra_headroom.py \
+	tx_extra_headroom_dataplane.py \
 	txtimestamp.sh \
 	udpgro.sh \
 	udpgro_bench.sh \
tools/testing/selftests/net/tx_extra_headroom_dataplane.py new file +197 -0

197 lines not on this page · raw message

Signature · 1 line
2.50.1
#7 lore raw
In reply to David Keogh
Quoted 8 lines · David Keogh

Measurements (net-next at commit 05b5019b0, veth, 500000 UDP datagrams, a 20-byte ip-in-ip push attached at TC egress, only tx_extra_headroom varied):

  tx_extra_headroom   pskb_expand_head reallocs / 500k 0 (off)             500003   (~1 per packet) 64                  4        (background) 128                 2        (background)

I remember frowning at this years ago, in the context of PPPoE.

I have the same question I had back then: why, if *every* packet ends up having to call pskb_expand_head() because it didn't have enough headroom, does our kernel need to be *told* by userspace that it needs to expand the allocations?

We gained dev->needed_headroom and ip_tunnel_adj_headroom() since then, and the kernel doesn't actually need to be told.

You explained why you don't want to use dev->needed_headroom; I don't have much of an opinion on that front. But if you do want to introduce a new tx_extra_headroom, could we try really hard to have it auto- tuning?

#8 lore raw
Quoted 17 lines · David Woodhouse
Quoted 8 lines · David Keogh

Measurements (net-next at commit 05b5019b0, veth, 500000 UDP datagrams, a 20-byte ip-in-ip push attached at TC egress, only tx_extra_headroom varied):

  tx_extra_headroom   pskb_expand_head reallocs / 500k 0 (off)             500003   (~1 per packet) 64                  4        (background) 128                 2        (background)

I remember frowning at this years ago, in the context of PPPoE.

I have the same question I had back then: why, if *every* packet ends up having to call pskb_expand_head() because it didn't have enough headroom, does our kernel need to be *told* by userspace that it needs to expand the allocations?

Quoted 7 lines

We gained dev->needed_headroom and ip_tunnel_adj_headroom() since then, and the kernel doesn't actually need to be told.

You explained why you don't want to use dev->needed_headroom; I don't have much of an opinion on that front. But if you do want to introduce a new tx_extra_headroom, could we try really hard to have it auto- tuning?

For TC-BPF I suppose only the BPF program knows how much extra headroom is needed.

Maybe this tx_extra_headroom could be declared as a BTF decl tag in the program. If this is the only usecase there's no need for a rtnetlink uAPI.

Thanks,
Dragos

maintainer
#9 lore raw
Quoted 25 lines · Dragos Tatulea
Quoted 17 lines · David Woodhouse
Quoted 8 lines · David Keogh

Measurements (net-next at commit 05b5019b0, veth, 500000 UDP datagrams, a 20-byte ip-in-ip push attached at TC egress, only tx_extra_headroom varied):

  tx_extra_headroom   pskb_expand_head reallocs / 500k 0 (off)             500003   (~1 per packet) 64                  4        (background) 128                 2        (background)

I remember frowning at this years ago, in the context of PPPoE.

I have the same question I had back then: why, if *every* packet ends up having to call pskb_expand_head() because it didn't have enough headroom, does our kernel need to be *told* by userspace that it needs to expand the allocations?

Quoted 7 lines

We gained dev->needed_headroom and ip_tunnel_adj_headroom() since then, and the kernel doesn't actually need to be told.

You explained why you don't want to use dev->needed_headroom; I don't have much of an opinion on that front. But if you do want to introduce a new tx_extra_headroom, could we try really hard to have it auto- tuning?

Quoted 6 lines

For TC-BPF I suppose only the BPF program knows how much extra headroom is needed.

Maybe this tx_extra_headroom could be declared as a BTF decl tag in the program. If this is the only usecase there's no need for a rtnetlink uAPI.

Fwiw, there was some work in this area here in the past ...

- b9ed315d3c4c ("netkit: Allow for configuring needed_{head,tail}room")
- b73b8146d7ff ("rtnetlink: add needed_{head,tail}room attributes")

... which we've integrated into Cilium which orchestrates the K8s Pods + BPF progs, so it can calc needed headroom for the Pod's netdev based on the config/needs in the hostns (e.g. tunneling).

Cheers,
Daniel

#10 lore raw

Thanks David, Dragos and Daniel for the feedback, and for the pointers to prior art. There are really two questions in your replies: can the kernel derive the reservation instead of an operator setting it, and is a new value even needed given what already exists. Let me take both, and correct one thing in my own cover letter while I am at it.

First, one correction to my cover letter: I overstated the TCP case. TCP already reserves MAX_TCP_HEADER of headroom, so raising needed_headroom only reallocates a TCP segment if the push would exceed that existing room, which a modest bump does not. The packets that actually lose are the low-headroom ones the program does not encapsulate (forwarded and received skbs) and stacked devices that snapshot the value.

David (Woodhouse):

Quoted 1 line

could we try really hard to have it auto-tuning?

Auto-tuning is a reasonable generalisation and I want to be honest about why I have not gone there. It is tractable in principle: the push helper, skb_cow_head() in the encap path, knows the exact bytes it adds and whether it reallocated, so a per-device high-water mark learned there would be attributable, roughly the monotonic-raise shape of ip_tunnel_adj_headroom(). But making that correct brings in bounding, decay when a map-driven push depth shrinks, per-program attribution when several programs are attached, and a chunk of runtime state to get wrong, and it still eats one reallocation on the first packet of every new depth.

That is a lot of machinery for what I actually need, which is narrow: reserve a small fixed amount of egress headroom, on the order of 128 bytes, so a UDP encap push stops reallocating every packet. It is set once for performance and never touched at runtime. A static reservation matches that: no learning, no per-packet state, nothing to converge, and it is trivially observable because the value is exactly what the operator set. I would rather keep the mechanism that simple than build an adaptive one to avoid configuring a constant. If the consensus is that only an auto-tuning version is acceptable upstream I will take that on board, but I think the static reserve is the proportionate fix for this problem and I would like to make that case before reaching for a learning loop.

Dragos:

Quoted 2 lines

Maybe this tx_extra_headroom could be declared as a BTF decl tag in the program. If this is the only usecase there's no need for a rtnetlink uAPI.

A BTF tag is an interesting way to attach the value to the program, and for a purely BPF deployment it may be enough. The one caveat I would flag is the one you raise yourself: a decl tag is static per program, while a program's push depth can depend on map state at runtime, so the tag is really an upper bound rather than an exact figure. For my case that is fine, since I want a single fixed reservation rather than a per-program one, but it is worth bearing in mind if the tag is meant to be tight. Whether a device-level attribute still earns its place for non-BPF egress transforms is really a question for you and the other maintainers; I do not have a stake in those paths myself.

Daniel:

Quoted 3 lines

there was some work in this area here in the past ...
- b9ed315d3c4c ("netkit: Allow for configuring needed_{head,tail}room")
- b73b8146d7ff ("rtnetlink: add needed_{head,tail}room attributes")

Thanks, these are the right references and I had not connected them. For traffic generated inside a Pod on netkit, the creation-time needed_headroom already feeds LL_RESERVED_SPACE() at the allocation sites, so Cilium setting it from the control plane does solve that case today. The gap I am trying to close is the same value on an ordinary egress device (a physical NIC, plain veth) without raising the expand-on-demand threshold for traffic that is not being encapsulated. Do you see needed_headroom as sufficient for that too, or is a separate allocation-only reserve worth it there?

On scope: the core patches (the field and the allocation-site macros) have no in-tree consumer without a setter, so they do not stand alone for net-next, and I will repost them together with whatever configuration surface we land on rather than ahead of it. What I want to avoid is turning a fixed performance reservation into an adaptive subsystem it does not need. If a static per-device value is acceptable, the open question is only how it is set (the rtnetlink attribute as posted, a BPF-side declaration for the BPF case, or reusing a control-plane path like Cilium's), and I am happy to follow the list on that.

Thanks again, this was useful.

David

#11 lore raw

Tianyi, Jesper (added to Cc),

Tianyi got in touch off-list offering to help get this series merged, and sent a draft v1 built on the comments in this thread. Thanks, and sorry for the slow reply. I would be glad to work on it together, and I would like to keep the discussion on the list from here so everyone can follow it.

Tianyi, you have a second use case for this: an XDP program on a veth peer. Please describe it here, as it bears directly on the points raised above.

I suggest you post v1, with patches 1-4 from me and 5-8 from you, and the cover letter under your own name, since the veth XDP case and the new measurements are yours. Please drop my Co-developed-by and Signed-off-by from patch 5. It is your test, and little of the RFC test it replaces is left.

I rebased your draft on net-next 0822d2fe3826 and ran it on x86_64 and arm64. tx_extra_headroom.py passes on both, and with patch 2 reverted the push and XDP cases fail as expected. A few things to fold in before posting:

- The note below the --- that native XDP on veth still copies belongs in the body. The motivation says veth copies every packet, and this series only removes the copy for generic XDP.

- checkpatch --strict reports "Macro argument reuse 'dev'" twice on patch 1, for LL_ALLOC_SPACE_EX(). It is the same pattern as LL_RESERVED_SPACE_EX() and I am fine leaving it, but NIPA will flag it, so a line under the --- may save a question.

- The comment above main() in xdp_helper.c still says the helper does not process packets. Something like "with -t it sends one frame through the TX ring and exits once the frame completes" would match patch 6.

- The iproute2 patch does not build against iproute2-next yet, because its uapi copy has no IFLA_TX_EXTRA_HEADROOM. Worth saying in that patch that it depends on the kernel side and a header sync.

On the questions you raised:

1. Should removing LL_RESERVED_SPACE_EXTRA() go in patch 1? Yes. The reason it cannot be reused is tied to the new macro, so it reads well there.

2. Should new fragments from ip_do_fragment() and ip6_fragment() also get the extra headroom? No, leave them. A larger value would send more packets to the slow path, which copies them.

3. Is patch 5 fine with you as author and my Co-developed-by? As above, please drop my trailers from patch 5.

4. Should there be a test that TCP is not reallocated? Yes, I have one. It runs one TCP connection over IPv4 and one over IPv6 with tx_extra_headroom 0 and 512, and checks that the headroom of the TCP packets is the same. With ip_finish_output2() changed to use LL_ALLOC_SPACE(), the IPv4 case fails (headroom 246-254 becomes 566-574). It applies on top of your patches, so I will send it as a reply to v1.

Daniel, Dragos, David: unless you see a reason to hold off, the plan is for Tianyi to post v1, addressing the comments in this thread.

Thanks,
David

#12 lore raw

Hi David,

Thanks for the review, and for testing it on x86_64 and arm64.

Quoted 3 lines · David Keogh

Tianyi, you have a second use case for this: an XDP program on a veth peer. Please describe it here, as it bears directly on the points raised above.

At Cloudflare we run some services each in its own network namespace. A veth pair connects the namespace to the host: veth0 is in the host and veth1 is in the namespace, with an XDP program on veth1. XDP needs XDP_PACKET_HEADROOM (256 bytes) in front of each packet. The packets that the host sends into veth0 have much less: 2 bytes from a UDP socket and 64 bytes from AF_XDP. So veth1 copies each of them into a new buffer before the program runs.

Raising needed_headroom on veth0 is not an option. The v1 cover letter has the details.

Quoted 3 lines

I suggest you post v1, with patches 1-4 from me and 5-8 from you, and the cover letter under your own name, since the veth XDP case and the new measurements are yours.

I am happy to post v1 as you suggest. I will fold in the points you listed and post it on the latest net-next. Thanks for the TCP test as well.

Thanks,
Tianyi