BPF List
 help / color / mirror / Atom feed
From: Benjamin Coddington <ben.coddington@hammerspace.com>
To: Chuck Lever <cel@kernel.org>, Jeff Layton <jlayton@kernel.org>,
	NeilBrown <neil@brown.name>
Cc: linux-nfs@vger.kernel.org, Daire Byrne <daire@dneg.com>,
	bpf@vger.kernel.org, Martin KaFai Lau <martin.lau@linux.dev>
Subject: [PATCH RFC v2 7/9] selftests/bpf: add svc_classifier tests
Date: Wed,  7 Oct 2026 15:59:32 -0400	[thread overview]
Message-ID: <aeb501171cf5bbd04d324afefb39682387552c8c.1791402701.git.bcodding@hammerspace.com> (raw)
In-Reply-To: <cover.1791402701.git.bcodding@hammerspace.com>

From: Benjamin Coddington <bcodding@hammerspace.com>

A classifier for RPC service transports that looks the peer address
up in an LPM trie keyed by {family, address}, the reference for the
sunrpc svc_classifier struct_ops, and tests of the attach model: a
second attach in the same network namespace fails with -EBUSY, a link
update replaces the classifier, a detached instance can be attached
again, a classifier attached inside a namespace does not occupy the
initial one, and a namespace that exits leaves its link to detach into
nothing.  Skipped where the struct_ops is not registered.

Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
 tools/testing/selftests/bpf/config            |   2 +
 .../selftests/bpf/prog_tests/svc_classifier.c | 104 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_svc_classifier.c  |  80 ++++++++++++++
 3 files changed, 186 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/svc_classifier.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_svc_classifier.c

diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index ea7044f30adc..2cb7080d7da2 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -114,6 +114,7 @@ CONFIG_IP_NF_IPTABLES=y
 CONFIG_IP6_NF_IPTABLES=y
 CONFIG_IP6_NF_FILTER=y
 CONFIG_NF_NAT=y
+CONFIG_NFS_FS=y
 CONFIG_PACKET=y
 CONFIG_RC_CORE=y
 CONFIG_SAMPLES=y
@@ -133,6 +134,7 @@ CONFIG_TCP_CONG_BBR=y
 CONFIG_INFINIBAND=y
 CONFIG_SMC=y
 CONFIG_SMC_HS_CTRL_BPF=y
+CONFIG_SUNRPC_BPF_CLASSIFY=y
 CONFIG_DIBS=y
 CONFIG_DIBS_LO=y
 CONFIG_PM_WAKELOCKS=y
diff --git a/tools/testing/selftests/bpf/prog_tests/svc_classifier.c b/tools/testing/selftests/bpf/prog_tests/svc_classifier.c
new file mode 100644
index 000000000000..887af29b21c7
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/svc_classifier.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * The svc_classifier struct_ops: one per network namespace, bound to
+ * the attaching task's namespace; replace by link update; detach by
+ * link close; a namespace that exits leaves its link empty.
+ */
+#include <test_progs.h>
+#include "bpf_svc_classifier.skel.h"
+
+static struct bpf_svc_classifier *load(void)
+{
+	struct bpf_svc_classifier *skel;
+
+	skel = bpf_svc_classifier__open_and_load();
+	if (!skel && (errno == ENOENT || errno == EINVAL || errno == EOPNOTSUPP)) {
+		test__skip();
+		return NULL;
+	}
+	ASSERT_OK_PTR(skel, "bpf_svc_classifier__open_and_load");
+	return skel;
+}
+
+static void test_attach(void)
+{
+	struct bpf_svc_classifier *one, *two;
+	struct bpf_link *link, *busy;
+	int err;
+
+	one = load();
+	if (!one)
+		return;
+	two = load();
+	if (!two)
+		goto out_one;
+
+	link = bpf_map__attach_struct_ops(one->maps.prefix);
+	if (!ASSERT_OK_PTR(link, "attach"))
+		goto out_two;
+
+	busy = bpf_map__attach_struct_ops(two->maps.prefix);
+	ASSERT_ERR_PTR(busy, "second attach");
+	ASSERT_EQ(errno, EBUSY, "second attach errno");
+
+	err = bpf_link__update_map(link, two->maps.prefix);
+	ASSERT_OK(err, "replace");
+
+	/* the replaced instance can be attached again once released */
+	bpf_link__destroy(link);
+	link = bpf_map__attach_struct_ops(one->maps.prefix);
+	ASSERT_OK_PTR(link, "attach after detach");
+	bpf_link__destroy(link);
+out_two:
+	bpf_svc_classifier__destroy(two);
+out_one:
+	bpf_svc_classifier__destroy(one);
+}
+
+static void test_netns(void)
+{
+	struct bpf_svc_classifier *inner, *outer;
+	struct netns_obj *ns;
+	struct bpf_link *link, *link2;
+
+	inner = load();
+	if (!inner)
+		return;
+	outer = load();
+	if (!outer)
+		goto out_inner;
+
+	ns = netns_new("svc_classifier_ns", true);
+	if (!ASSERT_OK_PTR(ns, "netns_new"))
+		goto out_outer;
+
+	/* attached inside the namespace */
+	link = bpf_map__attach_struct_ops(inner->maps.prefix);
+	if (!ASSERT_OK_PTR(link, "attach in netns"))
+		goto out_ns;
+
+	/* the initial namespace is free */
+	netns_free(ns);
+	ns = NULL;
+	link2 = bpf_map__attach_struct_ops(outer->maps.prefix);
+	ASSERT_OK_PTR(link2, "attach in init_net");
+	bpf_link__destroy(link2);
+
+	/* the link of the dead namespace detaches into nothing */
+	bpf_link__destroy(link);
+out_ns:
+	if (ns)
+		netns_free(ns);
+out_outer:
+	bpf_svc_classifier__destroy(outer);
+out_inner:
+	bpf_svc_classifier__destroy(inner);
+}
+
+void test_svc_classifier(void)
+{
+	if (test__start_subtest("attach"))
+		test_attach();
+	if (test__start_subtest("netns"))
+		test_netns();
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_svc_classifier.c b/tools/testing/selftests/bpf/progs/bpf_svc_classifier.c
new file mode 100644
index 000000000000..bae9d115ee74
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_svc_classifier.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A svc_classifier for RPC service transports: the peer address is looked
+ * up in an LPM trie keyed by {family, address}; a hit returns the class
+ * word stored there, a miss returns 0, the anonymous client.  A key of
+ * prefixlen 0 is the default for every family, 8 the default for one.
+ */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define AF_INET			2
+#define AF_INET6		10
+
+#define SVC_CLASSIFIER_NAME_MAX	16
+
+struct svc_xprt___local {
+	struct __kernel_sockaddr_storage xpt_remote;
+} __attribute__((preserve_access_index));
+
+struct svc_classifier___local {
+	char name[SVC_CLASSIFIER_NAME_MAX];
+	__u32 (*classify)(const struct svc_xprt___local *xprt);
+};
+
+struct prefix_key {
+	__u32 prefixlen;	/* 8 (the family byte) + address bits */
+	__u8 family;
+	__u8 addr[16];
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_LPM_TRIE);
+	__type(key, struct prefix_key);
+	__type(value, __u32);
+	__uint(max_entries, 1024);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+} prefixes SEC(".maps");
+
+__u64 classified;
+
+SEC("struct_ops/classify")
+__u32 BPF_PROG(prefix_classify, const struct svc_xprt___local *xprt)
+{
+	struct __kernel_sockaddr_storage ss;
+	struct prefix_key key = {};
+	__u32 *class;
+
+	if (bpf_core_read(&ss, sizeof(ss), &xprt->xpt_remote))
+		return 0;
+
+	key.family = ss.ss_family;
+	switch (ss.ss_family) {
+	case AF_INET:
+		/* struct sockaddr_in: port at 2, address at 4 */
+		__builtin_memcpy(key.addr, &ss.__data[2], 4);
+		key.prefixlen = 8 + 32;
+		break;
+	case AF_INET6:
+		/* struct sockaddr_in6: port at 2, flowinfo at 4, address at 8 */
+		__builtin_memcpy(key.addr, &ss.__data[6], 16);
+		key.prefixlen = 8 + 128;
+		break;
+	default:
+		return 0;
+	}
+
+	__sync_fetch_and_add(&classified, 1);
+	class = bpf_map_lookup_elem(&prefixes, &key);
+	return class ? *class : 0;
+}
+
+SEC(".struct_ops.link")
+struct svc_classifier___local prefix = {
+	.name		= "prefix",
+	.classify	= (void *)prefix_classify,
+};
-- 
2.53.0


  parent reply	other threads:[~2026-10-07 19:59 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-07 19:59 [PATCH RFC v2 0/9] SUNRPC: dispatch ready transports round-robin across clients, classes from BPF Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 1/9] SUNRPC: track service clients by class Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 2/9] SUNRPC: dispatch ready transports round-robin across clients Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 3/9] SUNRPC: add a BPF struct_ops hook to classify accepted transports Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 4/9] SUNRPC: report the transport's class in the svc_xprt_dequeue tracepoint Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 5/9] SUNRPC: dispatch control events ahead of data within a client Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 6/9] SUNRPC: keep the single transport queue while no classifier is attached Benjamin Coddington
2026-10-07 19:59 ` Benjamin Coddington [this message]
2026-10-07 19:59 ` [PATCH RFC v2 8/9] tools/net/sunrpc: add svc-classify, a prefix classifier and its loader Benjamin Coddington
2026-10-08 18:36   ` Jeff Layton
2026-10-08 19:38     ` Benjamin Coddington
2026-10-07 19:59 ` [PATCH RFC v2 9/9] Documentation: describe RPC server transport classes and the BPF classifier Benjamin Coddington
2026-10-08 18:42   ` Jeff Layton
2026-10-08 19:40     ` Benjamin Coddington
2026-10-08 15:19 ` [PATCH RFC v2 0/9] SUNRPC: dispatch ready transports round-robin across clients, classes from BPF Chuck Lever
2026-10-08 15:28   ` Benjamin Coddington
2026-10-08 18:48 ` Jeff Layton

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=aeb501171cf5bbd04d324afefb39682387552c8c.1791402701.git.bcodding@hammerspace.com \
    --to=ben.coddington@hammerspace.com \
    --cc=bpf@vger.kernel.org \
    --cc=cel@kernel.org \
    --cc=daire@dneg.com \
    --cc=jlayton@kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=neil@brown.name \
    /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