Repository navigation
Conversation
The eBPF device filter used libbpf-sys only for five thin wrappers (prog load, attach, detach, query, get fd by id), which made every cgroupsv2_devices build compile libbpf from C source and need the libelf and zlib headers on every host and cross target. The new bpf_sys module issues those commands with libc::syscall(SYS_bpf), using the kernel ABI types generated in aya-obj (pure Rust, MIT/Apache-2.0); program loads retry EAGAIN as libbpf does. The program is still attached with BPF_PROG_ATTACH, so it outlives the runtime process. The license string is now passed NUL-terminated (it was a Rust &str). libelf is dropped from the build dependency lists in the README and the setup guide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Marco Napetti <marco.napetti@proton.me>
There was a problem hiding this comment.
🟡 Changes recommended
The default systemd dependency requires Rust 1.98 despite the advertised 1.85 minimum, breaking supported builds, and the new syscall implementation lacks unit coverage.
2 open issues
What changed in this PR
This PR replaces libcgroups’ libbpf dependency with direct bpf(2) syscalls for cgroup v2 device filtering, removing the C-library build requirement while retaining the existing feature wiring.
Changes:
- Add five syscall wrappers using
aya-objkernel bindings, including program-load retries. - NUL-terminate program licenses and adapt existing mocks and tests.
- Replace dependency declarations and remove libelf from documented build prerequisites.
| File | Description |
|---|---|
README.md |
Remove libelf build prerequisites. |
docs/src/user/basic_setup.md |
Update build dependency instructions. |
crates/libcgroups/src/v2/devices/program.rs |
Use device constants from aya-obj. |
crates/libcgroups/src/v2/devices/mod.rs |
Register the syscall module outside unit-test builds. |
crates/libcgroups/src/v2/devices/mocks.rs |
Adapt mocks to the new wrapper signatures. |
crates/libcgroups/src/v2/devices/bpf.rs |
Call new wrappers and NUL-terminate licenses. |
crates/libcgroups/src/v2/devices/bpf_sys.rs |
Implement direct BPF syscall wrappers. |
crates/libcgroups/Cargo.toml |
Wire device filtering to aya-obj. |
Cargo.toml |
Replace the workspace libbpf dependency. |
Cargo.lock |
Record the dependency replacement and Windows dependency updates. |
Review effort: Balanced
| serde = { workspace = true, features = ["derive"] } | ||
| rbpf = { workspace = true, optional = true } | ||
| libbpf-sys = { workspace = true, optional = true } | ||
| aya-obj = { workspace = true, optional = true } |
There was a problem hiding this comment.
aya-obj is gone (b9191f2): the few UAPI structs and constants are defined locally in bpf_uapi.rs, with offset_of! assertions against the kernel layout, so there is no bindings dependency and no new MSRV.
If you'd rather keep bindings from a crate: aya-obj 0.2.1 declares no rust-version and builds on 1.85 (0.3.0 needs 1.87, 0.5+ needs 1.98). I preferred local definitions because 0.2.1 brings about ten crates (object, hashbrown, thiserror 1, ...) for four structs, and any later bump would break the MSRV again. Happy to switch if you prefer it.
Note that 1.85 isn't met on main either once cgroupsv2_devices is on: rbpf 0.4.3 uses let chains and is_multiple_of (≈1.88), and cargo +1.85.0 check -p libcgroups fails on main the same way. That's outside this PR, but I'm happy to help if you want to raise rust-version or deal with rbpf.
| @@ -1,4 +1,6 @@ | |||
| pub mod bpf; | |||
| #[cfg(not(test))] | |||
There was a problem hiding this comment.
Added (b9191f2, f4eb3f9): bpf_sys is now compiled in tests with a test-only syscall hook. Ten tests cover:
- the attr encoding per command;
- success after
EAGAIN, the five-attempt limit and immediate return on other errors; E2BIGfor an oversized instruction count;- the
prog_cnt/attach_flagswrite-back, including onENOSPC.
Every command is issued from a zeroed 256-byte attr buffer, because the kernel writes query results back through the pointer (recent kernels also write query.revision). One test has the fake kernel fill the whole buffer and checks that nothing beside the caller's struct changes.
aya-obj 0.6 requires Rust 1.98 while libcgroups declares 1.85. Drop it and declare the needed kernel UAPI types in v2/devices/bpf_uapi.rs (per-command attr structs, bpf_insn, command/prog-type/attach-type values, BPF_F_ALLOW_MULTI, BPF_DEVCG_*), with const size/offset assertions against include/uapi/linux/bpf.h. Each command now passes size_of its own attr struct; the kernel zero-fills the rest. bpf_sys is now compiled in tests with a thread-local syscall hook (test builds only) and unit-tested: attr encoding per command, retry after EAGAIN, immediate return on other errors, the five-attempt limit and query output propagation including on ENOSPC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Marco Napetti <marco.napetti@proton.me>
The kernel writes BPF_PROG_QUERY results back through the attr pointer whatever size it is given: attach_flags and prog_cnt, and since the cgroup revisions also query.revision at offset 56. A command struct shorter than that would be overrun. Every command is now copied into a zeroed, 8-byte aligned 256-byte buffer and issued with that size (the kernel only requires the tail beyond its own union to be zero); the struct's fields are copied back afterwards. ProgQueryAttr declares the fields up to revision, with offset assertions. Tests: the fake syscall checks the size and the zeroed tail, writes revision on success and ENOSPC, and a 0xFF-filling kernel leaves memory beside the caller's struct untouched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Marco Napetti <marco.napetti@proton.me>
|
Thank you for working on this!
Once they're merged, could you rebase this PR onto them? We'll make the final call on merging after confirming that the tests pass with this change. |


Title: refactor(libcgroups): replace libbpf-sys with direct bpf(2) syscalls
Description
The eBPF device filter (
cgroupsv2_devices, whichsystemdimplies since #3721) useslibbpf-sysfor only five thin wrappers around thebpf(2)system call:bpf_prog_load,bpf_prog_attach,bpf_prog_detach2,bpf_prog_queryandbpf_prog_get_fd_by_id. The program itself is already assembled in Rust withrbpf. Yetlibbpf-syscompiles libbpf from C source, so every build needs a C toolchain,make, and the libelf and zlib headers, for the host and for every cross target.This PR replaces
libbpf-syswith a smallbpf_sysmodule:libc::syscall(libc::SYS_bpf, ...).bpf_uapi.rs): the attr structs of the five commands,bpf_insn, the command, program-type and attach-type values,BPF_F_ALLOW_MULTIand theBPF_DEVCG_*constants, copied frominclude/uapi/linux/bpf.hwithsize_of/offset_of!assertions pinning every field to its kernel offset. No bindings crate is added, so the MSRV is unchanged.query.revision).errnoset), sobpf.rskeeps its logic, its error handling (includingENOSPCon query) and its mockall structure. Only the mock module changes, to the new signatures.EAGAINup to five times, as libbpf and aya do.BPF_PROG_ATTACH, not a BPF link, so it outlives the youki process as before. This is why I didn't use aya's high-levelCgroupDevice: on kernels from 5.7 it attaches throughbpf_link_create, and the link would close when youki exits. It also loads only from ELF objects.Two small related changes:
&str"Apache", which isn't NUL-terminated, so the kernel read past its end. It's now passed as aCString.libelf-devandelfutils-libelf-develare removed from the build dependency lists inREADME.mdanddocs/src/user/basic_setup.md.Nothing changes in behaviour or features:
cgroupsv2_devicesandsystemdmean what they meant. This PR is independent of #3939: either can be merged without the other.Type of Change
Testing
Added new unit tests
Added new integration tests
Ran existing test suite
Tested manually (please provide steps)
Lint:
cargo +nightly fmt --all -- --checkandcargo clippy --all --all-targets --all-features -- -D warningspass.Tests:
bpf_sysis compiled in tests with a test-only syscall hook, and 10 new unit tests cover the attr encoding, theEAGAINretry and its limit, error passthrough,E2BIG, the query write-back (including onENOSPC) and that kernel writes never leave the attr buffer.cargo test -p libcgroups --libgives 270 passed; the same 9 systemd and D-Bus tests fail on my machine before and after, because it has no systemd.Builds:
libcgroupsbuilds withv1,v2,systemdandv2 cgroupsv2_devices, andlibcontainerwithv2 cgroupsv2_devicesandsystemd.Cargo.lockonly dropslibbpf-sys.libcgroupsalso builds in a stockrust:1container without libelf headers; its only-sysdependency left is the pure-Rustlinux-raw-sys.Not tested on a real kernel: loading and attaching the program needs root, and I couldn't run that on my machine. The
contestdevice tests in the e2e workflow exercise it, and I'll follow their results on this PR.MSRV: with
cgroupsv2_deviceson,rbpf0.4.3 already needs about Rust 1.88 onmain, above the declared 1.85; this PR doesn't change that. If you prefer bindings from a crate,aya-obj0.2.1 builds on 1.85.Related Issues
Related: #3721.
Additional Context
The CI workflows and container images (
cross/Dockerfile.gnu,cross/Dockerfile.musl,.devcontainer,tests/k8s,tools/youki-deployand the e2e, podman and runc workflows) still install libelf and zlib. I left them alone because some may be needed by other components, such as wasmedge for zlib, and I can't test those images here. I'm happy to clean them up in this PR or a follow-up if you confirm which ones were only for libbpf-sys.🤖 Generated with Claude Code