From: Siddharth Nayyar <sidnayyar@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Quentin Monnet <qmo@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
"Anton Protopopov" <a.s.protopopov@gmail.com>,
"Giuliano Procida" <gprocida@google.com>,
"Neill Kapron" <nkapron@google.com>,
"Thiébaud Weksteen" <tweek@google.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Siddharth Nayyar" <sidnayyar@google.com>
Subject: [PATCH bpf-next v3 4/4] selftests/bpf: Add light skeleton struct_ops tests
Date: Tue, 01 Dec 2026 17:33:40 +0000 [thread overview]
Message-ID: <20261201-lskel-struct-ops-v3-4-057f3d23ac0c@google.com> (raw)
In-Reply-To: <20261201-lskel-struct-ops-v3-0-057f3d23ac0c@google.com>
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 struct
bpf_testmod_ops___v2 with a zeroed field absent from the kernel, a
NULL void * callback placeholder, and a shared callback), testing
kernel-side object-BTF resolution, scalar member copying, detach, and
per-map re-attachment.
- invalid_local_btf verifies that BPF_MAP_UPDATE_ELEM rejects crafted
local BTF structs whose INT members extend past the local struct size,
use non-byte-aligned bit offsets, or encode non-kflag bitfields.
Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
tools/testing/selftests/bpf/Makefile.skel | 3 +-
.../selftests/bpf/prog_tests/struct_ops_lskel.c | 200 +++++++++++++++++++++
tools/testing/selftests/bpf/progs/struct_ops_mod.c | 53 ++++++
.../selftests/bpf/progs/struct_ops_vmlinux.c | 42 +++++
4 files changed, 297 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 45e4ce5dca4d..334342928d01 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 many_progs.c
+ test_ringbuf_overwrite.c callx_rodata.c many_progs.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..a1fe16af6934
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/btf.h>
+#include <test_progs.h>
+#include "network_helpers.h"
+#include "test_btf.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);
+ ASSERT_EQ(skel->links.lskel_ca_fd, 0, "lskel_ca_fd_detached");
+
+ /* 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);
+ ASSERT_EQ(skel->links.testmod_1_fd, 0, "testmod_1_fd_detached");
+ ASSERT_EQ(skel->links.testmod_2_fd, 0, "testmod_2_fd_detached");
+ 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);
+}
+
+static void check_crafted_btf(const char *field_name, __u32 int_off,
+ __u32 int_bits, __u32 int_sz, __u32 member_bit_off)
+{
+ const char *tname = "tcp_congestion_ops";
+ const char *iname = "u32";
+ struct {
+ struct btf_header hdr;
+ __u32 types[10];
+ char strs[64];
+ } raw = {};
+ LIBBPF_OPTS(bpf_map_create_opts, opts,
+ .map_flags = BPF_F_LINK,
+ .btf_value_type_id = 2,
+ );
+ __u32 iname_off, tname_off, fname_off, str_len;
+ __u32 key = 0, val = 0;
+ int btf_fd = -1, map_fd = -1, err;
+
+ str_len = 1;
+ iname_off = str_len;
+ strcpy(raw.strs + str_len, iname);
+ str_len += strlen(iname) + 1;
+ tname_off = str_len;
+ strcpy(raw.strs + str_len, tname);
+ str_len += strlen(tname) + 1;
+ fname_off = str_len;
+ strcpy(raw.strs + str_len, field_name);
+ str_len += strlen(field_name) + 1;
+
+ raw.hdr.magic = BTF_MAGIC;
+ raw.hdr.version = BTF_VERSION;
+ raw.hdr.hdr_len = sizeof(raw.hdr);
+ raw.hdr.type_len = sizeof(raw.types);
+ raw.hdr.str_off = sizeof(raw.types);
+ raw.hdr.str_len = str_len;
+
+ memcpy(raw.types, (__u32 []) {
+ BTF_TYPE_INT_ENC(iname_off, 0, int_off, int_bits, int_sz),
+ BTF_STRUCT_ENC(tname_off, 1, sizeof(val)),
+ BTF_MEMBER_ENC(fname_off, 1, member_bit_off),
+ }, sizeof(raw.types));
+
+ btf_fd = bpf_btf_load(&raw, sizeof(raw.hdr) + sizeof(raw.types) + str_len, NULL);
+ if (!ASSERT_GE(btf_fd, 0, "bpf_btf_load"))
+ return;
+
+ opts.btf_fd = btf_fd;
+ map_fd = bpf_map_create(BPF_MAP_TYPE_STRUCT_OPS, "crafted_st_ops",
+ sizeof(key), sizeof(val), 1, &opts);
+ if (!ASSERT_GE(map_fd, 0, "bpf_map_create"))
+ goto cleanup;
+
+ err = bpf_map_update_elem(map_fd, &key, &val, 0);
+ ASSERT_EQ(err, -EINVAL, "map_update_elem_rejected");
+
+cleanup:
+ if (map_fd >= 0)
+ close(map_fd);
+ if (btf_fd >= 0)
+ close(btf_fd);
+}
+
+static void test_invalid_local_btf(void)
+{
+ /* Matched member whose INT size extends past the local struct */
+ check_crafted_btf("flags", 0, 8, 4, 24);
+ /* Absent member whose INT size extends past the local struct */
+ check_crafted_btf("absent", 0, 8, 4096, 0);
+ /* Non-kflag non-byte-aligned member bit offset */
+ check_crafted_btf("absent", 0, 4, 1, 4);
+ /* Non-kflag bitfield INT with non-zero bit offset */
+ check_crafted_btf("flags", 8, 8, 4, 0);
+}
+
+void test_struct_ops_lskel(void)
+{
+ if (test__start_subtest("vmlinux"))
+ test_vmlinux();
+ if (test__start_subtest("module"))
+ test_module();
+ if (test__start_subtest("invalid_local_btf"))
+ test_invalid_local_btf();
+}
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..01cbdd39f60b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_mod.c
@@ -0,0 +1,53 @@
+// 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;
+ int zeroed_missing;
+ void *test_refcounted;
+ 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.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-12-01 17:33 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 17:33 [PATCH bpf-next v3 0/4] bpf: Support struct_ops in light skeletons Siddharth Nayyar
2026-12-01 17:33 ` [PATCH bpf-next v3 1/4] bpf: Support struct_ops with object BTF Siddharth Nayyar
2026-12-01 17:33 ` [PATCH bpf-next v3 2/4] libbpf: Support struct_ops in light skeleton loader Siddharth Nayyar
2026-12-01 17:33 ` [PATCH bpf-next v3 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons Siddharth Nayyar
2026-12-01 17:33 ` Siddharth Nayyar [this message]
2026-12-01 21:00 ` [PATCH bpf-next v3 0/4] bpf: Support " patchwork-bot+netdevbpf
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=20261201-lskel-struct-ops-v3-4-057f3d23ac0c@google.com \
--to=sidnayyar@google.com \
--cc=a.s.protopopov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=gprocida@google.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mripard@kernel.org \
--cc=nkapron@google.com \
--cc=qmo@kernel.org \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tweek@google.com \
--cc=yonghong.song@linux.dev \
/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®