[RFC net-next 5/6] selftests/bpf: add tx_extra_headroom data-plane programs
From: David Keogh
Date: Thu Oct 15 2026 - 19:57:16 EST
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.
Signed-off-by: David Keogh <dkeogh@xxxxxxxxxx>
---
.../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
diff --git a/tools/testing/selftests/bpf/progs/tx_extra_headroom_probe.c b/tools/testing/selftests/bpf/progs/tx_extra_headroom_probe.c
new file mode 100644
index 000000000..a6ca66ef0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tx_extra_headroom_probe.c
@@ -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";
diff --git a/tools/testing/selftests/bpf/progs/tx_extra_headroom_tc.bpf.c b/tools/testing/selftests/bpf/progs/tx_extra_headroom_tc.bpf.c
new file mode 100644
index 000000000..43e3bea60
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tx_extra_headroom_tc.bpf.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * tc-egress pusher for the tx_extra_headroom data-plane selftest.
+ *
+ * On egress, pushes OUTER_LEN bytes of outer header via
+ * bpf_skb_adjust_room(..., BPF_ADJ_ROOM_MAC, ...). On an skb allocated with only
+ * LL_RESERVED_SPACE() of headroom this forces skb_cow_head() ->
+ * pskb_expand_head() (a realloc-and-copy) on every packet; when the device has
+ * reserved extra headroom via tx_extra_headroom the push finds room already
+ * present and no realloc happens. The reallocation is counted by the companion
+ * fentry program in tx_extra_headroom_probe.bpf.c.
+ *
+ * UDP only, so TCP control traffic and IPv6 NDP are not corrupted. OUTER_LEN
+ * defaults to 20 (one IPv4 header) and can be overridden at build time.
+ */
+#include <linux/bpf.h>
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
+#include <linux/pkt_cls.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_endian.h>
+
+#ifndef OUTER_LEN
+#define OUTER_LEN 20
+#endif
+
+SEC("tc")
+int push_outer(struct __sk_buff *skb)
+{
+ void *data = (void *)(long)skb->data;
+ void *data_end = (void *)(long)skb->data_end;
+ struct ethhdr *eth = data;
+ __u8 l4 = 0;
+
+ if ((void *)(eth + 1) > data_end)
+ return TC_ACT_OK;
+
+ if (eth->h_proto == bpf_htons(ETH_P_IP)) {
+ struct iphdr *ip = (void *)(eth + 1);
+
+ if ((void *)(ip + 1) > data_end)
+ return TC_ACT_OK;
+ l4 = ip->protocol;
+ } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
+ struct ipv6hdr *ip6 = (void *)(eth + 1);
+
+ if ((void *)(ip6 + 1) > data_end)
+ return TC_ACT_OK;
+ l4 = ip6->nexthdr;
+ } else {
+ return TC_ACT_OK;
+ }
+
+ /* UDP only: do not corrupt TCP or ICMPv6/NDP control traffic. */
+ if (l4 != IPPROTO_UDP)
+ return TC_ACT_OK;
+
+ /*
+ * Grow at the MAC layer by OUTER_LEN. The helper's internal
+ * skb_cow_head() reallocates when headroom is insufficient; that
+ * realloc is exactly what tx_extra_headroom is meant to avoid.
+ */
+ bpf_skb_adjust_room(skb, OUTER_LEN, BPF_ADJ_ROOM_MAC, 0);
+ return TC_ACT_OK;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.50.1