From 5e70b8f0fcf76af5352ac547d4800648d26cc225 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 24 Sep 2026 15:59:35 +0000 Subject: [PATCH] Tests: Port sixteen more kernel selftests now that scalar globals exist Every program on the selftest audit's Tier 1 and Tier 2 lists that the compiler can express today: xdp_dummy, priv_prog, test_xdp_link, xdp_tx, tc_dummy, test_tc_bpf, cgroup_mprog, cgroup_skb_direct_packet_access, test_signed_loader, test_signed_loader_data, test_netfilter_link_attach, kprobe_multi_empty, uprobe_multi_bench, uprobe_multi_usdt, test_link_pinning and test_xdp_devmap_helpers. Fifteen pass at every level, bringing tc, tcx, cgroup/getsockopt, cgroup_skb, socket, netfilter, kprobe.multi, uprobe.multi, usdt and tp_btf program types under test for the first time. test_xdp_devmap_helpers is a negative fixture upstream: reading egress_ifindex is only legal with expected_attach_type = BPF_XDP_DEVMAP and the driver asserts the plain load fails. It is a strict verifier-level xfail here for the same reason, until expected_attach_type can be expressed. The porting notes record the batch and why each remaining program on the two lists is still out of reach. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DLMW3X7opqAPH6bjmTVkr7 --- .../PORTING-NOTES.md | 48 +++++++++++++++ .../cgroup/cgroup_mprog.py | 41 +++++++++++++ .../netfilter/netfilter_link_attach.py | 23 ++++++++ .../socket/signed_loader.py | 23 ++++++++ .../socket/signed_loader_data.py | 37 ++++++++++++ .../kernel_selftest_equivalent/tc/tc_dummy.py | 22 +++++++ .../tracing/kprobe_multi_empty.py | 22 +++++++ .../tracing/link_pinning.py | 58 +++++++++++++++++++ .../tracing/uprobe_multi_bench.py | 39 +++++++++++++ .../tracing/uprobe_multi_usdt.py | 30 ++++++++++ .../cgroup_skb_direct_packet_access.py | 41 +++++++++++++ .../vmlinux/tc_bpf.py | 43 ++++++++++++++ .../vmlinux/xdp_devmap_helpers.py | 34 +++++++++++ .../vmlinux/xdp_tx.py | 26 +++++++++ .../xdp/priv_prog.py | 24 ++++++++ .../xdp/xdp_dummy.py | 31 ++++++++++ .../xdp/xdp_link.py | 29 ++++++++++ tests/test_config.toml | 7 +++ 18 files changed, 578 insertions(+) create mode 100644 tests/kernel_selftest_equivalent/cgroup/cgroup_mprog.py create mode 100644 tests/kernel_selftest_equivalent/netfilter/netfilter_link_attach.py create mode 100644 tests/kernel_selftest_equivalent/socket/signed_loader.py create mode 100644 tests/kernel_selftest_equivalent/socket/signed_loader_data.py create mode 100644 tests/kernel_selftest_equivalent/tc/tc_dummy.py create mode 100644 tests/kernel_selftest_equivalent/tracing/kprobe_multi_empty.py create mode 100644 tests/kernel_selftest_equivalent/tracing/link_pinning.py create mode 100644 tests/kernel_selftest_equivalent/tracing/uprobe_multi_bench.py create mode 100644 tests/kernel_selftest_equivalent/tracing/uprobe_multi_usdt.py create mode 100644 tests/kernel_selftest_equivalent/vmlinux/cgroup_skb_direct_packet_access.py create mode 100644 tests/kernel_selftest_equivalent/vmlinux/tc_bpf.py create mode 100644 tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py create mode 100644 tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py create mode 100644 tests/kernel_selftest_equivalent/xdp/priv_prog.py create mode 100644 tests/kernel_selftest_equivalent/xdp/xdp_dummy.py create mode 100644 tests/kernel_selftest_equivalent/xdp/xdp_link.py diff --git a/tests/kernel_selftest_equivalent/PORTING-NOTES.md b/tests/kernel_selftest_equivalent/PORTING-NOTES.md index 2e9b402..e3addc6 100644 --- a/tests/kernel_selftest_equivalent/PORTING-NOTES.md +++ b/tests/kernel_selftest_equivalent/PORTING-NOTES.md @@ -91,6 +91,54 @@ cursor, which together constrain the design more than anything here does. enums, then gives up), a Python-level surface for declaring one, and userspace access through `pylibbpf`. +## Second batch: everything portable after globals + +With scalar globals in, the audit's Tier 1 and Tier 2 lists were re-read against the +compiler and every program it can express was ported. Sixteen more, fifteen of which +pass at every level; the sixteenth passes at IR and llc and is rejected by the verifier +exactly as its upstream driver asserts it must be: + +| Port | Upstream | Section | Outcome | +|---|---|---|---| +| `xdp/xdp_dummy.py` | `xdp_dummy.c` | `xdp` x2 | passes | +| `xdp/priv_prog.py` | `priv_prog.c` | `xdp` | passes | +| `xdp/xdp_link.py` | `test_xdp_link.c` | `xdp`, `tc` | passes | +| `vmlinux/xdp_tx.py` | `xdp_tx.c` | `xdp` | passes | +| `tc/tc_dummy.py` | `tc_dummy.c` | `tc` | passes | +| `vmlinux/tc_bpf.py` | `test_tc_bpf.c` | `tc`, `tcx/ingress` | passes (direct packet access) | +| `cgroup/cgroup_mprog.py` | `cgroup_mprog.c` | `cgroup/getsockopt` x4 | passes | +| `vmlinux/cgroup_skb_direct_packet_access.py` | `cgroup_skb_direct_packet_access.c` | `cgroup_skb/ingress` | passes, after a compiler fix | +| `socket/signed_loader.py` | `test_signed_loader.c` | `socket` | passes | +| `socket/signed_loader_data.py` | `test_signed_loader_data.c` | `socket` | passes (.data global) | +| `netfilter/netfilter_link_attach.py` | `test_netfilter_link_attach.c` | `netfilter` | passes | +| `tracing/kprobe_multi_empty.py` | `kprobe_multi_empty.c` | `kprobe.multi/` | passes | +| `tracing/uprobe_multi_bench.py` | `uprobe_multi_bench.c` | `uprobe.multi/...` | passes (`count += 1`) | +| `tracing/uprobe_multi_usdt.py` | `uprobe_multi_usdt.c` | `usdt` | passes | +| `tracing/link_pinning.py` | `test_link_pinning.c` | `raw_tp/sys_enter`, `tp_btf/sys_enter` | passes | +| `vmlinux/xdp_devmap_helpers.py` | `test_xdp_devmap_helpers.c` | `xdp` | verifier xfail by design | + +**One compiler bug fell out.** `data_end = skb->data_end` into a `__u32` global failed +with `cannot store i64 to i32*`: context fields are loaded widened to i64, and the +assignment path only accepted them into 64-bit slots or slots of exactly the field's +type, never narrowing. It now goes through `convert()` like every other integer store. +`passing_tests/vmlinux/ctx_field_narrow_store.py` pins it. + +**What is still not portable, and why**, from the same two lists: + +| Program | Blocker | +|---|---| +| `metadata_used.c`, `metadata_unused.c` | `char[]` `.rodata` globals: only integer scalars can be globals | +| `test_log_buf.c`, `cgroup_preorder.c`, `uprobe_multi_pid_filter.c`, `test_build_id.c` | array globals | +| `token_kallsyms.c`, `test_btf_ext.c`, `test_static_linked*.c` | BPF-to-BPF calls (`__weak` / `__noinline` subprogs) | +| `test_trace_ext.c`, `freplace_get_constant.c` | `freplace` needs a target program to load against | +| `test_subskeleton*.c` | extern symbols, `__kconfig`, static linking | +| `test_pkt_md_access.c` | narrow type-punned loads of `__sk_buff` fields | +| `test_xdp_attach_fail.c` | tracepoint `__data_loc` pointer arithmetic on a custom ctx struct | +| `sockopt_multi.c` | writes to context fields and through `optval` | +| `tracing_struct_many_args.c` | `BPF_PROG2` multi-argument entry | +| `bpf_nop_bench.c` | `bpf_loop`-based benchmark macro | +| `test_tcp_estats.c` | large; inlinable helpers and struct-heavy, not attempted yet | + ## 3. Incidental findings - **Nested struct field access fails with a misleading error.** `ctx.regs.ip` reports diff --git a/tests/kernel_selftest_equivalent/cgroup/cgroup_mprog.py b/tests/kernel_selftest_equivalent/cgroup/cgroup_mprog.py new file mode 100644 index 0000000..f4717b5 --- /dev/null +++ b/tests/kernel_selftest_equivalent/cgroup/cgroup_mprog.py @@ -0,0 +1,41 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/cgroup_mprog.c +# +# Four identical cgroup/getsockopt programs. Upstream attaches them in +# various orders with BPF_F_BEFORE/BPF_F_AFTER and checks the resulting +# multi-prog chain; the bodies only need to exist and return "allow". + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("cgroup/getsockopt") +def getsockopt_1(ctx: c_void_p) -> c_int64: + return c_int64(1) + + +@bpf +@section("cgroup/getsockopt") +def getsockopt_2(ctx: c_void_p) -> c_int64: + return c_int64(1) + + +@bpf +@section("cgroup/getsockopt") +def getsockopt_3(ctx: c_void_p) -> c_int64: + return c_int64(1) + + +@bpf +@section("cgroup/getsockopt") +def getsockopt_4(ctx: c_void_p) -> c_int64: + return c_int64(1) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/netfilter/netfilter_link_attach.py b/tests/kernel_selftest_equivalent/netfilter/netfilter_link_attach.py new file mode 100644 index 0000000..5740890 --- /dev/null +++ b/tests/kernel_selftest_equivalent/netfilter/netfilter_link_attach.py @@ -0,0 +1,23 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c +# +# A netfilter program that accepts everything (NF_ACCEPT is 1). Upstream +# attaches it with every combination of protocol family, hook and priority +# and checks which the kernel rejects. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("netfilter") +def nf_link_attach_test(ctx: c_void_p) -> c_int64: + return c_int64(1) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/socket/signed_loader.py b/tests/kernel_selftest_equivalent/socket/signed_loader.py new file mode 100644 index 0000000..8f4974e --- /dev/null +++ b/tests/kernel_selftest_equivalent/socket/signed_loader.py @@ -0,0 +1,23 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_signed_loader.c +# +# A minimal, map-less socket filter. Upstream drives it through libbpf's +# light-skeleton loader to test signed-program loading; a socket filter +# needs no attach resolution and no maps keeps the loader trivial. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("socket") +def probe(ctx: c_void_p) -> c_int64: + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/socket/signed_loader_data.py b/tests/kernel_selftest_equivalent/socket/signed_loader_data.py new file mode 100644 index 0000000..43bc53d --- /dev/null +++ b/tests/kernel_selftest_equivalent/socket/signed_loader_data.py @@ -0,0 +1,37 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_signed_loader_data.c +# +# The signed-loader fixture with one initialised global, so the object has a +# .data map that the loader must seed. Upstream checks that a signed loader +# keeps the attested initial value: +# +# __u64 magic = 0x5eed1234abad1deaULL; +# +# SEC("socket") +# int probe(void *ctx) +# { +# return (int)magic; +# } + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int32, c_uint64 + + +@bpf +@bpfglobal +def magic() -> c_uint64: + return c_uint64(0x5EED1234ABAD1DEA) + + +@bpf +@section("socket") +def probe(ctx: c_void_p) -> c_int32: + return c_int32(magic) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/tc/tc_dummy.py b/tests/kernel_selftest_equivalent/tc/tc_dummy.py new file mode 100644 index 0000000..8f8797d --- /dev/null +++ b/tests/kernel_selftest_equivalent/tc/tc_dummy.py @@ -0,0 +1,22 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/tc_dummy.c +# +# A classifier that returns TC_ACT_OK for everything. Upstream is the fixture +# behind the tc_links and tc_opts attach-order tests. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tc") +def entry(skb: c_void_p) -> c_int64: + return c_int64(1) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/tracing/kprobe_multi_empty.py b/tests/kernel_selftest_equivalent/tracing/kprobe_multi_empty.py new file mode 100644 index 0000000..a7d8ffc --- /dev/null +++ b/tests/kernel_selftest_equivalent/tracing/kprobe_multi_empty.py @@ -0,0 +1,22 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/kprobe_multi_empty.c +# +# An empty kprobe.multi program. Upstream attaches it to every function in +# the kernel's available_filter_functions list to benchmark attach time. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("kprobe.multi/") +def test_kprobe_empty(ctx: c_void_p) -> c_int64: + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/tracing/link_pinning.py b/tests/kernel_selftest_equivalent/tracing/link_pinning.py new file mode 100644 index 0000000..9065e54 --- /dev/null +++ b/tests/kernel_selftest_equivalent/tracing/link_pinning.py @@ -0,0 +1,58 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_link_pinning.c +# +# Two programs, one raw_tp and one tp_btf on the same tracepoint, each +# copying a global set by userspace into a global read by userspace. +# Upstream pins the link, closes every fd, then checks the program still +# fires by bumping `in` and watching `out` follow: +# +# int in = 0; +# int out = 0; +# +# SEC("raw_tp/sys_enter") +# int raw_tp_prog(const void *ctx) +# { +# out = in; +# return 0; +# } +# +# `in` is renamed `in_val` because `in` is a Python keyword. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_int32 + + +@bpf +@bpfglobal +def in_val() -> c_int32: + return c_int32(0) + + +@bpf +@bpfglobal +def out() -> c_int32: + return c_int32(0) + + +@bpf +@section("raw_tp/sys_enter") +def raw_tp_prog(ctx: c_void_p) -> c_int64: + global out + out = in_val + return c_int64(0) + + +@bpf +@section("tp_btf/sys_enter") +def tp_btf_prog(ctx: c_void_p) -> c_int64: + global out + out = in_val + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/tracing/uprobe_multi_bench.py b/tests/kernel_selftest_equivalent/tracing/uprobe_multi_bench.py new file mode 100644 index 0000000..890bfc8 --- /dev/null +++ b/tests/kernel_selftest_equivalent/tracing/uprobe_multi_bench.py @@ -0,0 +1,39 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/uprobe_multi_bench.c +# +# Count how many times the multi-uprobe fires. Upstream attaches it to +# thousands of uprobe_multi_func_* symbols and reads `count` back: +# +# int count; +# +# SEC("uprobe.multi/./uprobe_multi:uprobe_multi_func_*") +# int uprobe_bench(struct pt_regs *ctx) +# { +# count++; +# return 0; +# } + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_int32 + + +@bpf +@bpfglobal +def count() -> c_int32: + return c_int32(0) + + +@bpf +@section("uprobe.multi/./uprobe_multi:uprobe_multi_func_*") +def uprobe_bench(ctx: c_void_p) -> c_int64: + global count + count += 1 + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/tracing/uprobe_multi_usdt.py b/tests/kernel_selftest_equivalent/tracing/uprobe_multi_usdt.py new file mode 100644 index 0000000..01ed34c --- /dev/null +++ b/tests/kernel_selftest_equivalent/tracing/uprobe_multi_usdt.py @@ -0,0 +1,30 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/uprobe_multi_usdt.c +# +# The USDT flavour of the multi-uprobe counter. Upstream attaches it to a +# USDT probe in the test binary and asserts on `count` from userspace. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64, c_int32 + + +@bpf +@bpfglobal +def count() -> c_int32: + return c_int32(0) + + +@bpf +@section("usdt") +def usdt0(ctx: c_void_p) -> c_int64: + global count + count += 1 + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/vmlinux/cgroup_skb_direct_packet_access.py b/tests/kernel_selftest_equivalent/vmlinux/cgroup_skb_direct_packet_access.py new file mode 100644 index 0000000..c290fb3 --- /dev/null +++ b/tests/kernel_selftest_equivalent/vmlinux/cgroup_skb_direct_packet_access.py @@ -0,0 +1,41 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/cgroup_skb_direct_packet_access.c +# +# A cgroup_skb program that records skb->data_end into a global. Upstream +# asserts from userspace that the value is non-zero, proving cgroup_skb +# programs get direct packet access: +# +# __u32 data_end; +# +# SEC("cgroup_skb/ingress") +# int direct_packet_access(struct __sk_buff *skb) +# { +# data_end = skb->data_end; +# return 1; +# } + +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct___sk_buff +from ctypes import c_int64, c_uint32 + + +@bpf +@bpfglobal +def data_end() -> c_uint32: + return c_uint32(0) + + +@bpf +@section("cgroup_skb/ingress") +def direct_packet_access(skb: struct___sk_buff) -> c_int64: + global data_end + data_end = skb.data_end + return c_int64(1) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/vmlinux/tc_bpf.py b/tests/kernel_selftest_equivalent/vmlinux/tc_bpf.py new file mode 100644 index 0000000..cf6710d --- /dev/null +++ b/tests/kernel_selftest_equivalent/vmlinux/tc_bpf.py @@ -0,0 +1,43 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_tc_bpf.c +# +# `cls` is a dummy classifier for the TC-BPF API test. `pkt_ptr` is the one +# that matters: it derives a packet pointer from skb->data, bounds-checks it +# against skb->data_end, and is loaded without CAP_SYS_ADMIN/CAP_PERFMON to +# prove direct packet access works for a plain tcx program. Upstream: +# +# struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr); +# +# if ((long)(iph + 1) > (long)skb->data_end) +# return 1; +# return 0; +# +# sizeof(struct ethhdr) + sizeof(struct iphdr) is 14 + 20 = 34. + +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import struct___sk_buff +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tc") +def cls(skb: c_void_p) -> c_int64: + return c_int64(0) + + +@bpf +@section("tcx/ingress") +def pkt_ptr(skb: struct___sk_buff) -> c_int64: + data = c_void_p(skb.data) + data_end = c_void_p(skb.data_end) + if data + 34 > data_end: + return c_int64(1) + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py b/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py new file mode 100644 index 0000000..1e9de0a --- /dev/null +++ b/tests/kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py @@ -0,0 +1,34 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_xdp_devmap_helpers.c +# +# Reads xdp_md->egress_ifindex, which only exists for programs loaded with +# expected_attach_type = BPF_XDP_DEVMAP. Upstream loads it *without* that +# type and asserts the load fails, so the program is a negative fixture: +# +# unsigned int len = data_end - data; +# bpf_trace_printk(fmt, sizeof(fmt), +# ctx->ingress_ifindex, ctx->egress_ifindex, len); +# return XDP_PASS; + +from pythonbpf import bpf, section, bpfglobal, compile +from pythonbpf.helper import XDP_PASS +from vmlinux import struct_xdp_md +from ctypes import c_int64 + + +@bpf +@section("xdp") +def xdpdm_devlog(ctx: struct_xdp_md) -> c_int64: + length = ctx.data_end - ctx.data + ingress = ctx.ingress_ifindex + egress = ctx.egress_ifindex + print(f"devmap redirect: dev {ingress} -> dev {egress} len {length}") + return XDP_PASS + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py b/tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py new file mode 100644 index 0000000..8d40ffe --- /dev/null +++ b/tests/kernel_selftest_equivalent/vmlinux/xdp_tx.py @@ -0,0 +1,26 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/xdp_tx.c +# +# Bounce every packet back out of the interface it arrived on. Upstream is +# the transmit side of the veth XDP tests. +# +# XDP_TX comes from vmlinux because pythonbpf.helper exports only XDP_PASS +# and XDP_DROP; that is why this lives under vmlinux/. + +from pythonbpf import bpf, section, bpfglobal, compile +from vmlinux import XDP_TX +from ctypes import c_void_p, c_int64 + + +@bpf +@section("xdp") +def xdp_tx(xdp: c_void_p) -> c_int64: + return c_int64(XDP_TX) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/xdp/priv_prog.py b/tests/kernel_selftest_equivalent/xdp/priv_prog.py new file mode 100644 index 0000000..181d684 --- /dev/null +++ b/tests/kernel_selftest_equivalent/xdp/priv_prog.py @@ -0,0 +1,24 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/priv_prog.c +# +# An XDP program that drops everything. Upstream loads it from an +# unprivileged process to check the CAP_BPF/CAP_NET_ADMIN gating; the +# program itself is the smallest privileged-type program there is. + +from pythonbpf import bpf, section, bpfglobal, compile +from pythonbpf.helper import XDP_DROP +from ctypes import c_void_p, c_int64 + + +@bpf +@section("xdp") +def xdp_prog1(xdp: c_void_p) -> c_int64: + return XDP_DROP + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/xdp/xdp_dummy.py b/tests/kernel_selftest_equivalent/xdp/xdp_dummy.py new file mode 100644 index 0000000..82686de --- /dev/null +++ b/tests/kernel_selftest_equivalent/xdp/xdp_dummy.py @@ -0,0 +1,31 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/xdp_dummy.c +# +# Two XDP programs that pass every packet. Upstream is the fixture that a +# dozen prog_tests attach and detach to exercise XDP link plumbing; the +# second program's odd name is deliberate, it is what the kallsyms test looks +# for. + +from pythonbpf import bpf, section, bpfglobal, compile +from pythonbpf.helper import XDP_PASS +from ctypes import c_void_p, c_int64 + + +@bpf +@section("xdp") +def xdp_dummy_prog(ctx: c_void_p) -> c_int64: + return XDP_PASS + + +@bpf +@section("xdp") +def __x64_sys_nop(ctx: c_void_p) -> c_int64: + return XDP_PASS + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/kernel_selftest_equivalent/xdp/xdp_link.py b/tests/kernel_selftest_equivalent/xdp/xdp_link.py new file mode 100644 index 0000000..d94df5a --- /dev/null +++ b/tests/kernel_selftest_equivalent/xdp/xdp_link.py @@ -0,0 +1,29 @@ +# Ported from Linux tools/testing/selftests/bpf/progs/test_xdp_link.c +# +# One XDP and one TC handler in the same object. Upstream attaches the XDP +# one through bpf_link and checks that legacy netlink attach of the same +# program is refused while the link exists. + +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("xdp") +def xdp_handler(xdp: c_void_p) -> c_int64: + return c_int64(0) + + +@bpf +@section("tc") +def tc_handler(skb: c_void_p) -> c_int64: + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() diff --git a/tests/test_config.toml b/tests/test_config.toml index 122c2be..fa8c9c8 100644 --- a/tests/test_config.toml +++ b/tests/test_config.toml @@ -66,3 +66,10 @@ "kernel_selftest_equivalent/ringbuf/reserve_submit_discard.py" = {reason = "RingBuffer reserve/typed record/discard workflow is planned but not implemented yet", level = "ir"} "kernel_selftest_equivalent/vmlinux/perf_skip.py" = {reason = "Nested struct field access (ctx.regs.ip) not supported; one level such as ctx.sample_period works", level = "ir"} + +# Upstream is a negative fixture: reading xdp_md->egress_ifindex is only legal +# for programs loaded with expected_attach_type = BPF_XDP_DEVMAP, and the +# prog_tests driver asserts that a plain load *fails*. The kernel rejects it +# here with "invalid bpf_context access off=20 size=4", which is the pass +# condition upstream. PythonBPF has no way to set expected_attach_type yet. +"kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py" = {reason = "Negative fixture: egress_ifindex needs expected_attach_type=BPF_XDP_DEVMAP, which cannot be set yet; the verifier rejection is upstream's pass condition", level = "verifier"}