From d46fc8991bc3fb8368f5ec27c9f74926132715e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20DEROIDE?= Date: Mon, 15 Sep 2025 16:56:04 +0200 Subject: [PATCH 1/3] aya-ebpf: Add BPF_F_ADJ_ROOM_ENCAP_L2 macro This macro is needed to properly add a L2 header when using bpf_skb_adjust_room [1]. As it is originally a C macro, it isn't automatically generated in the `bindings` crate [1]: https://docs.ebpf.io/linux/helper-function/bpf_skb_adjust_room/ --- ebpf/aya-ebpf/src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ebpf/aya-ebpf/src/lib.rs b/ebpf/aya-ebpf/src/lib.rs index da969290..f6356f9e 100644 --- a/ebpf/aya-ebpf/src/lib.rs +++ b/ebpf/aya-ebpf/src/lib.rs @@ -114,6 +114,15 @@ mod intrinsics { } } +// A reimplementation of the BPF_F_ADJ_ROOM_ENCAP_L2(len) macro of the kernel, to use to construct +// flags to pass to bpf_skb_adjust_room. +// https://elixir.bootlin.com/linux/v6.16.4/source/include/uapi/linux/bpf.h#L6149 +#[inline(always)] +#[allow(non_snake_case)] +pub fn BPF_F_ADJ_ROOM_ENCAP_L2(len: u64) -> u64 { + (len & u64::from(bindings::BPF_ADJ_ROOM_ENCAP_L2_MASK)) << bindings::BPF_ADJ_ROOM_ENCAP_L2_SHIFT +} + /// Check if a value is within a range, using conditional forms compatible with /// the verifier. #[inline(always)] From e5e59982606a9f1302bace5a4fe575a2c0cbcacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20DEROIDE?= Date: Mon, 15 Sep 2025 17:30:45 +0200 Subject: [PATCH 2/3] Fix public-api definition --- xtask/public-api/aya-ebpf.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/xtask/public-api/aya-ebpf.txt b/xtask/public-api/aya-ebpf.txt index ea62c132..544c4933 100644 --- a/xtask/public-api/aya-ebpf.txt +++ b/xtask/public-api/aya-ebpf.txt @@ -3039,4 +3039,5 @@ impl aya_ebpf::EbpfContext for aya_ebpf::programs::tracepoint::TracePointContext pub fn aya_ebpf::programs::tracepoint::TracePointContext::as_ptr(&self) -> *mut core::ffi::c_void impl aya_ebpf::EbpfContext for aya_ebpf::programs::xdp::XdpContext pub fn aya_ebpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void +pub fn aya_ebpf::BPF_F_ADJ_ROOM_ENCAP_L2(len: u64) -> u64 pub fn aya_ebpf::check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool From 0812d52618ece8ad1c080bc44c3d4f1cc84bb3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20DEROIDE?= <34481271+aureliar8@users.noreply.github.com> Date: Thu, 16 Oct 2025 10:10:27 +0200 Subject: [PATCH 3/3] Update ebpf/aya-ebpf/src/lib.rs Co-authored-by: Michal R --- ebpf/aya-ebpf/src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ebpf/aya-ebpf/src/lib.rs b/ebpf/aya-ebpf/src/lib.rs index f6356f9e..dd60bb58 100644 --- a/ebpf/aya-ebpf/src/lib.rs +++ b/ebpf/aya-ebpf/src/lib.rs @@ -114,9 +114,13 @@ mod intrinsics { } } -// A reimplementation of the BPF_F_ADJ_ROOM_ENCAP_L2(len) macro of the kernel, to use to construct -// flags to pass to bpf_skb_adjust_room. -// https://elixir.bootlin.com/linux/v6.16.4/source/include/uapi/linux/bpf.h#L6149 +/// Builds a flag for [`SkBuffContext::adjust_room`] that defines L2 encapsulation, +/// using `len` as the inner MAC header length. +/// +/// Equivalent to the [`BPF_F_ADJ_ROOM_ENCAP_L2`][uapi-bpf-adj-room-encap-l2] macro +/// in the Linux user-space API. +/// +/// [uapi-bpf-adj-room-encap-l2]: https://elixir.bootlin.com/linux/v6.16.4/source/include/uapi/linux/bpf.h#L6149 #[inline(always)] #[allow(non_snake_case)] pub fn BPF_F_ADJ_ROOM_ENCAP_L2(len: u64) -> u64 {