From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796075148; b=bAq3rGiuebmvAU7tNWN7jACK37nvPMcuWM3+lg+rNu8twsUyBgoBIV1reUimLrBSqHdI iS5dWlhPsgeEkKWPNGDTmTx1dh1tXpNi8y73WoH8dqiJPtIAQKZNbZ2PYcj4866Aa+7gI yMLHDIWSzOT1ctRtVow4adDV4xijVLyHAYjD5ONUcTQnLB8iWqSRce3g91m26F4E8djFY g60/6Llo6ejW7Ip9L0EG0pO43P1ctS063pzvjFTSdLzTyZnPi32STc10DOEV4gNV3yqcD WT3LFmluVXh/v0/zTbzB7rTNM0wCJrLOLrA99NvI37NSPadx4bdIhHSEXpk8faxID9A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796075148; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=G9whLGxaLrENj5h9Vz3HxbdgE83Ci0ydQPuR0aOX9u4=; b=tU1Bry7T9Zg1tVgSvVKCLO7gM4TaVeJKtWjWsk/Zh7LVT9eFKI3gnQmqKQeO3KFjbkLM vxNpRuxZT0WUk8+POMQwekKIe/6cBF7YvCze8t1RKs3sXW8KEsMg4Wq3EGRE6xjgn6o4/ JmQ2oB7Edsg20FPVp29hWGiwT9uZLf//TmEcmKi8Ptanm2OTRzDdT3ZItl7J9gxaAm+ig Dz3fzYoBquikO3fM3KQ+YPBOzMmozuAEQlCRP1p6mjnlFsS/gr/mUktGvf8GETT0IZJF7 35XMg7txsWyqavxrcIKbBw69R6koo4YIlcE3ssKlXtYg0Bq65RYcE9UuMLGWROUXKHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--sidnayyar.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=hivP1+Wy; arc=none smtp.remote-ip=209.85.222.198 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3CC51606A339 for ; Mon, 30 Nov 2026 21:45:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3CC51606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sidnayyar.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=hivP1+Wy Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-94975041698so2377684385a.0 for ; Mon, 30 Nov 2026 13:45:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796075147; x=1796679947; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=G9whLGxaLrENj5h9Vz3HxbdgE83Ci0ydQPuR0aOX9u4=; b=hivP1+WyshgFRI9i0sttLS/TQmUPb/zBXWVrPoKXXcX2oh3EUHEzdL9woYD7L9jugr rdmQHd3orbIamy8vtinwhAE4ma5UKQfcm7aKY3RuRtZrWOWIAZOA9o04PoDW8s0aS5Sf LuueAqO+2+B3QOyVi8w1Ql0e3H3/q4UKr6PSB7zLQi+Dj7+arpV3ewTANZ/DmG0zL6wR oU1BmElImgRIMHOdty32Gudp4KZVvQkryWEuoljpSlKoi+H6pAGIyvuSE4GT+DKvXWnJ cWu9HAd6fC1/5trtpT+mQX/RE1eYq15MbtCGj39II8E/4yIgy4kO62AfIrrjj0WvuS6J vDIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796075147; x=1796679947; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=G9whLGxaLrENj5h9Vz3HxbdgE83Ci0ydQPuR0aOX9u4=; b=itWjX9JDVek5BVmYtVczIj8AESwnUJWz2H8s89DEWAJdPypWWrpvRyoGomjEZsJXxw UHtwvCFCpp4wW3SKQvPnGKsoMsPe4P2/gtortLn7ZStZp4cXiFhPK2qRjjPzmMgTOo/A Nh9bgVJZWrXC3TM60ablR4LKVWkE3qxtQGUzhN+y/w+5Aygudeq4g2PmgpbknR04Vr/y h956bVolqKlff4WbZSWdR24Z9paxzkKC9Dt9w9rQSbj+9t3tBhjgVTO7YMwxuVj8Wkew obqBMyrwLpr5263D0xgZp4meTtVLUt2rl+TFMhmA4anU70z5YZbnfuF9BX4BHjUZqCp8 DWgw== X-Gm-Message-State: APuU4Z+jlJ22JPWXtuZN99Ph13F78tXhEQa2oq7RK8qMhtHLxQK3n+MR 8VfgeqX/4TsZrTNZJWX+t89nDLU5jv8FsWIEqyJbXy8YBEMT3SjEtT83+P7iOPXbK87X1rvVMiX VPRyaaKUSbdQaB9FIZA== X-Received: from qkak15-n1.prod.google.com ([2002:a05:620a:a0cf:10b0:949:3f0b:4cf2]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:130f:b0:93e:95ce:5394 with SMTP id af79cd13be357-949ba81953fmr6166909885a.2.1796075146816; Mon, 30 Nov 2026 13:45:46 -0800 (PST) Date: Mon, 30 Nov 2026 21:44:49 +0000 In-Reply-To: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> X-Mailer: b4 0.14.3 Message-ID: <20261130-lskel-struct-ops-v2-4-8b76e65073ab@google.com> Subject: [PATCH bpf-next v2 4/4] selftests/bpf: Add light skeleton struct_ops tests From: Siddharth Nayyar To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Anton Protopopov , Giuliano Procida , Neill Kapron , "=?utf-8?q?Thi=C3=A9baud_Weksteen?=" , Maxime Ripard , Siddharth Nayyar Content-Type: text/plain; charset="utf-8" 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 and a shared callback), testing kernel-side object-BTF resolution, scalar member copying, detach, and per-map re-attachment. Signed-off-by: Siddharth Nayyar --- 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 | 52 +++++++++ .../selftests/bpf/progs/struct_ops_vmlinux.c | 42 +++++++ 4 files changed, 218 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..588d9223bf81 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#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); + 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); +} + +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..1d22267baa6b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_mod.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#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_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 +#include + +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