[PATCH RFC bpf-next 7/7] selftests/bpf: Add light skeleton struct_ops tests
From: Siddharth Nayyar
Date: Fri Nov 06 2026 - 15:24:50 EST
Add selftests exercising BPF_MAP_TYPE_STRUCT_OPS support in light
skeletons across both vmlinux and kernel module BTF targets:
- struct_ops_vmlinux.c defines a SEC(".struct_ops.link")
tcp_congestion_ops map in vmlinux BTF, testing open/load, link
attachment, callback invocation over a loopback TCP connection,
and unregistration upon detach.
- struct_ops_mod.c defines two SEC(".struct_ops.link") maps targeting
bpf_testmod_ops in module BTF (including a reordered subset struct
bpf_testmod_ops___v2 sharing a callback), testing module BTF FD
resolution, runtime member offset/index lookup via
bpf_btf_get_struct_member_info(), scalar member copying, detach,
and per-map re-attachment.
Signed-off-by: Siddharth Nayyar <sidnayyar@xxxxxxxxxx>
---
tools/testing/selftests/bpf/Makefile.skel | 3 +-
.../selftests/bpf/prog_tests/struct_ops_lskel.c | 122 +++++++++++++++++++++
tools/testing/selftests/bpf/progs/struct_ops_mod.c | 51 +++++++++
.../selftests/bpf/progs/struct_ops_vmlinux.c | 42 +++++++
4 files changed, 217 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index b8eaecc7f429..2726f9081e35 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -34,7 +34,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \
core_kern.c core_kern_overflow.c test_ringbuf.c \
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \
- test_ringbuf_overwrite.c callx_rodata.c
+ test_ringbuf_overwrite.c callx_rodata.c struct_ops_vmlinux.c \
+ struct_ops_mod.c
LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
new file mode 100644
index 000000000000..4e9bac460953
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "network_helpers.h"
+#include "struct_ops_vmlinux.lskel.h"
+#include "struct_ops_mod.lskel.h"
+
+static int set_tcp_ca_cb(int fd, void *opts)
+{
+ const char *tcp_ca = opts;
+
+ if (!ASSERT_OK(setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION,
+ tcp_ca, strlen(tcp_ca)), "setsockopt"))
+ return -1;
+ return 0;
+}
+
+static void test_vmlinux(void)
+{
+ char tcp_ca[] = "bpf_lskel_ca";
+ struct network_helper_opts opts = {
+ .post_socket_cb = set_tcp_ca_cb,
+ .cb_opts = tcp_ca,
+ };
+ struct struct_ops_vmlinux_lskel *skel;
+ int srv_fd = -1, cli_fd = -1, sock_fd = -1;
+ int err;
+
+ skel = struct_ops_vmlinux_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ err = struct_ops_vmlinux_lskel__attach(skel);
+ if (!ASSERT_OK(err, "attach"))
+ goto cleanup;
+
+ srv_fd = start_server_str(AF_INET6, SOCK_STREAM, NULL, 0, &opts);
+ if (!ASSERT_NEQ(srv_fd, -1, "start_server_str"))
+ goto cleanup;
+
+ cli_fd = connect_to_fd_opts(srv_fd, &opts);
+ if (!ASSERT_NEQ(cli_fd, -1, "connect_to_fd_opts"))
+ goto cleanup;
+
+ ASSERT_GT(skel->bss->ca_init_cnt, 0, "ca_init_cnt");
+
+ close(cli_fd);
+ cli_fd = -1;
+ close(srv_fd);
+ srv_fd = -1;
+
+ struct_ops_vmlinux_lskel__detach(skel);
+ skel->links.lskel_ca_fd = 0;
+
+ /* Verify congestion control was unregistered on detach */
+ sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
+ if (!ASSERT_GE(sock_fd, 0, "socket"))
+ goto cleanup;
+
+ err = setsockopt(sock_fd, IPPROTO_TCP, TCP_CONGESTION,
+ tcp_ca, strlen(tcp_ca));
+ ASSERT_EQ(err, -1, "setsockopt_after_detach");
+ ASSERT_EQ(errno, ENOENT, "errno_after_detach");
+
+cleanup:
+ if (sock_fd != -1)
+ close(sock_fd);
+ if (cli_fd != -1)
+ close(cli_fd);
+ if (srv_fd != -1)
+ close(srv_fd);
+ struct_ops_vmlinux_lskel__destroy(skel);
+}
+
+static void test_module(void)
+{
+ struct struct_ops_mod_lskel *skel;
+ int err, link_fd;
+
+ if (!env.has_testmod) {
+ test__skip();
+ return;
+ }
+
+ skel = struct_ops_mod_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ err = struct_ops_mod_lskel__attach(skel);
+ if (!ASSERT_OK(err, "attach"))
+ goto cleanup;
+
+ ASSERT_GT(skel->links.testmod_1_fd, 0, "testmod_1_fd");
+ ASSERT_GT(skel->links.testmod_2_fd, 0, "testmod_2_fd");
+ ASSERT_EQ(skel->bss->test_1_cnt, 2, "test_1_cnt");
+ ASSERT_EQ(skel->bss->testmod_1_result, 17, "testmod_1_result");
+ ASSERT_EQ(skel->bss->testmod_2_result, 28, "testmod_2_result");
+
+ struct_ops_mod_lskel__detach(skel);
+ skel->links.testmod_1_fd = 0;
+ skel->links.testmod_2_fd = 0;
+ memset(skel->bss, 0, sizeof(*skel->bss));
+
+ link_fd = struct_ops_mod_lskel__testmod_1__attach(skel);
+ if (!ASSERT_GT(link_fd, 0, "testmod_1_attach"))
+ goto cleanup;
+
+ ASSERT_EQ(skel->bss->test_1_cnt, 1, "reattach_test_1_cnt");
+ ASSERT_EQ(skel->bss->testmod_1_result, 17, "reattach_testmod_1_result");
+ ASSERT_EQ(skel->bss->testmod_2_result, 0, "reattach_testmod_2_result");
+
+cleanup:
+ struct_ops_mod_lskel__destroy(skel);
+}
+
+void test_struct_ops_lskel(void)
+{
+ if (test__start_subtest("vmlinux"))
+ test_vmlinux();
+ if (test__start_subtest("module"))
+ test_module();
+}
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_mod.c b/tools/testing/selftests/bpf/progs/struct_ops_mod.c
new file mode 100644
index 000000000000..b1bd7429149d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_mod.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../test_kmods/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+int test_1_cnt = 0;
+int testmod_1_result = 0;
+int testmod_2_result = 0;
+
+SEC("struct_ops/test_1")
+int BPF_PROG(test_1)
+{
+ test_1_cnt++;
+ return 0;
+}
+
+SEC("struct_ops/test_2")
+void BPF_PROG(test_2, int a, int b)
+{
+ testmod_1_result = a + b;
+}
+
+SEC("struct_ops/test_2_v2")
+void BPF_PROG(test_2_v2, int a, int b)
+{
+ testmod_2_result = a * b;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+ .test_1 = (void *)test_1,
+ .test_2 = (void *)test_2,
+ .data = 13,
+};
+
+struct bpf_testmod_ops___v2 {
+ int data;
+ void (*test_2)(int a, int b);
+ int (*test_1)(void);
+};
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops___v2 testmod_2 = {
+ .data = 7,
+ .test_2 = (void *)test_2_v2,
+ .test_1 = (void *)test_1,
+};
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c b/tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c
new file mode 100644
index 000000000000..1d8b573ad799
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "bpf_tracing_net.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+int ca_init_cnt = 0;
+
+SEC("struct_ops/ca_lskel_init")
+void BPF_PROG(ca_lskel_init, struct sock *sk)
+{
+ ca_init_cnt++;
+}
+
+SEC("struct_ops/ca_lskel_cong_control")
+void BPF_PROG(ca_lskel_cong_control, struct sock *sk, __u32 ack, int flag,
+ const struct rate_sample *rs)
+{
+}
+
+SEC("struct_ops/ca_lskel_ssthresh")
+__u32 BPF_PROG(ca_lskel_ssthresh, struct sock *sk)
+{
+ return tcp_sk(sk)->snd_ssthresh;
+}
+
+SEC("struct_ops/ca_lskel_undo_cwnd")
+__u32 BPF_PROG(ca_lskel_undo_cwnd, struct sock *sk)
+{
+ return tcp_sk(sk)->snd_cwnd;
+}
+
+SEC(".struct_ops.link")
+struct tcp_congestion_ops lskel_ca = {
+ .init = (void *)ca_lskel_init,
+ .cong_control = (void *)ca_lskel_cong_control,
+ .ssthresh = (void *)ca_lskel_ssthresh,
+ .undo_cwnd = (void *)ca_lskel_undo_cwnd,
+ .name = "bpf_lskel_ca",
+};
--
2.56.0.455.g6c5a503b38-goog