From 90059a9e70994c5cdab81ce453459b56210c2b7f Mon Sep 17 00:00:00 2001 From: Xiaobo Liu Date: Tue, 12 Aug 2025 20:36:21 +0800 Subject: [PATCH] ebpf: add peak() method to Queue and Stack Signed-off-by: Xiaobo Liu --- ebpf/aya-ebpf/src/maps/queue.rs | 10 +++++++++- ebpf/aya-ebpf/src/maps/stack.rs | 13 ++++++++++++- xtask/public-api/aya-ebpf.txt | 4 ++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ebpf/aya-ebpf/src/maps/queue.rs b/ebpf/aya-ebpf/src/maps/queue.rs index 8c8f0bb1..961ee8a1 100644 --- a/ebpf/aya-ebpf/src/maps/queue.rs +++ b/ebpf/aya-ebpf/src/maps/queue.rs @@ -2,7 +2,7 @@ use core::{cell::UnsafeCell, marker::PhantomData, mem}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_QUEUE}, - helpers::{bpf_map_pop_elem, bpf_map_push_elem}, + helpers::{bpf_map_peek_elem, bpf_map_pop_elem, bpf_map_push_elem}, maps::PinningType, }; @@ -63,4 +63,12 @@ impl Queue { (ret == 0).then_some(value.assume_init()) } } + + pub fn peek(&self) -> Option { + unsafe { + let mut value = mem::MaybeUninit::uninit(); + let ret = bpf_map_peek_elem(self.def.get() as *mut _, value.as_mut_ptr() as *mut _); + (ret == 0).then_some(value.assume_init()) + } + } } diff --git a/ebpf/aya-ebpf/src/maps/stack.rs b/ebpf/aya-ebpf/src/maps/stack.rs index 6328693d..f6756926 100644 --- a/ebpf/aya-ebpf/src/maps/stack.rs +++ b/ebpf/aya-ebpf/src/maps/stack.rs @@ -2,7 +2,7 @@ use core::{marker::PhantomData, mem}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_STACK}, - helpers::{bpf_map_pop_elem, bpf_map_push_elem}, + helpers::{bpf_map_peek_elem, bpf_map_pop_elem, bpf_map_push_elem}, maps::PinningType, }; @@ -64,4 +64,15 @@ impl Stack { (ret == 0).then_some(value.assume_init()) } } + + pub fn peek(&self) -> Option { + unsafe { + let mut value = mem::MaybeUninit::uninit(); + let ret = bpf_map_peek_elem( + &self.def as *const _ as *mut _, + value.as_mut_ptr() as *mut _, + ); + (ret == 0).then_some(value.assume_init()) + } + } } diff --git a/xtask/public-api/aya-ebpf.txt b/xtask/public-api/aya-ebpf.txt index 2d70495b..74a7551e 100644 --- a/xtask/public-api/aya-ebpf.txt +++ b/xtask/public-api/aya-ebpf.txt @@ -438,6 +438,7 @@ pub fn aya_ebpf::maps::program_array::ProgramArray::from(t: T) -> T pub mod aya_ebpf::maps::queue #[repr(transparent)] pub struct aya_ebpf::maps::queue::Queue impl aya_ebpf::maps::queue::Queue +pub fn aya_ebpf::maps::queue::Queue::peek(&self) -> core::option::Option pub const fn aya_ebpf::maps::queue::Queue::pinned(max_entries: u32, flags: u32) -> aya_ebpf::maps::queue::Queue pub fn aya_ebpf::maps::queue::Queue::pop(&self) -> core::option::Option pub fn aya_ebpf::maps::queue::Queue::push(&self, value: &T, flags: u64) -> core::result::Result<(), i64> @@ -592,6 +593,7 @@ pub fn aya_ebpf::maps::sock_map::SockMap::from(t: T) -> T pub mod aya_ebpf::maps::stack #[repr(transparent)] pub struct aya_ebpf::maps::stack::Stack impl aya_ebpf::maps::stack::Stack +pub fn aya_ebpf::maps::stack::Stack::peek(&self) -> core::option::Option pub const fn aya_ebpf::maps::stack::Stack::pinned(max_entries: u32, flags: u32) -> aya_ebpf::maps::stack::Stack pub fn aya_ebpf::maps::stack::Stack::pop(&mut self) -> core::option::Option pub fn aya_ebpf::maps::stack::Stack::push(&mut self, value: &T, flags: u64) -> core::result::Result<(), i64> @@ -1166,6 +1168,7 @@ impl core::convert::From for aya_ebpf::maps::program_array::ProgramArray pub fn aya_ebpf::maps::program_array::ProgramArray::from(t: T) -> T #[repr(transparent)] pub struct aya_ebpf::maps::Queue impl aya_ebpf::maps::queue::Queue +pub fn aya_ebpf::maps::queue::Queue::peek(&self) -> core::option::Option pub const fn aya_ebpf::maps::queue::Queue::pinned(max_entries: u32, flags: u32) -> aya_ebpf::maps::queue::Queue pub fn aya_ebpf::maps::queue::Queue::pop(&self) -> core::option::Option pub fn aya_ebpf::maps::queue::Queue::push(&self, value: &T, flags: u64) -> core::result::Result<(), i64> @@ -1283,6 +1286,7 @@ impl core::convert::From for aya_ebpf::maps::sock_map::SockMap pub fn aya_ebpf::maps::sock_map::SockMap::from(t: T) -> T #[repr(transparent)] pub struct aya_ebpf::maps::Stack impl aya_ebpf::maps::stack::Stack +pub fn aya_ebpf::maps::stack::Stack::peek(&self) -> core::option::Option pub const fn aya_ebpf::maps::stack::Stack::pinned(max_entries: u32, flags: u32) -> aya_ebpf::maps::stack::Stack pub fn aya_ebpf::maps::stack::Stack::pop(&mut self) -> core::option::Option pub fn aya_ebpf::maps::stack::Stack::push(&mut self, value: &T, flags: u64) -> core::result::Result<(), i64>