mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Keogh <dkeogh@amazon.com>
To: <netdev@vger.kernel.org>, Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Eric Dumazet <edumazet@kernel.org>
Cc: Dragos Tatulea <dtatulea@nvidia.com>,
	Nimrod Oren <noren@nvidia.com>, <nh-open-source@amazon.com>,
	<linux-kernel@vger.kernel.org>
Subject: [RFC net-next 5/6] selftests/bpf: add tx_extra_headroom data-plane programs
Date: Thu, 15 Oct 2026 23:53:34 +0000	[thread overview]
Message-ID: <20261015235335.4162993-6-dkeogh@amazon.com> (raw)
In-Reply-To: <20261015235335.4162993-1-dkeogh@amazon.com>

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@amazon.com>
---
 .../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


  parent reply	other threads:[~2026-10-15 23:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15 23:53 [RFC net-next 0/6] net: configurable extra TX headroom for local egress David Keogh
2026-10-15 23:53 ` [RFC net-next 1/6] net: add tx_extra_headroom and LL_ALLOC_SPACE() David Keogh
2026-10-15 23:53 ` [RFC net-next 2/6] net: reserve extra TX headroom for local egress David Keogh
2026-10-15 23:53 ` [RFC net-next 3/6] rtnetlink: add IFLA_TX_EXTRA_HEADROOM David Keogh
2026-10-15 23:53 ` [RFC net-next 4/6] selftests: net: add tx_extra_headroom test David Keogh
2026-10-15 23:53 ` David Keogh [this message]
2026-10-15 23:53 ` [RFC net-next 6/6] selftests/net: add tx_extra_headroom data-plane test David Keogh
2026-10-16  8:02 ` [RFC net-next 0/6] net: configurable extra TX headroom for local egress David Woodhouse
2026-10-16 18:48   ` Dragos Tatulea
2026-10-16 19:01     ` Daniel Borkmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261015235335.4162993-6-dkeogh@amazon.com \
    --to=dkeogh@amazon.com \
    --cc=dtatulea@nvidia.com \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    --cc=noren@nvidia.com \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®