diff --git a/ebpf/aya-ebpf/src/maps/ring_buf.rs b/ebpf/aya-ebpf/src/maps/ring_buf.rs index c134be94..3de2eb15 100644 --- a/ebpf/aya-ebpf/src/maps/ring_buf.rs +++ b/ebpf/aya-ebpf/src/maps/ring_buf.rs @@ -32,6 +32,45 @@ pub struct RingBuf { unsafe impl Sync for RingBuf {} +/// A ring buffer entry, returned from [`RingBuf::reserve_bytes`]. +/// +/// You must [`submit`] or [`discard`] this entry before it gets dropped. +/// +/// [`submit`]: RingBufBytes::submit +/// [`discard`]: RingBufBytes::discard +#[must_use = "eBPF verifier requires ring buffer entries to be either submitted or discarded"] +pub struct RingBufBytes<'a>(&'a mut [u8]); + +impl Deref for RingBufBytes<'_> { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + let Self(inner) = self; + inner + } +} + +impl DerefMut for RingBufBytes<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + let Self(inner) = self; + inner + } +} + +impl RingBufBytes<'_> { + /// Commit this ring buffer entry. The entry will be made visible to the userspace reader. + pub fn submit(self, flags: u64) { + let Self(inner) = self; + unsafe { bpf_ringbuf_submit(inner.as_mut_ptr().cast(), flags) }; + } + + /// Discard this ring buffer entry. The entry will be skipped by the userspace reader. + pub fn discard(self, flags: u64) { + let Self(inner) = self; + unsafe { bpf_ringbuf_discard(inner.as_mut_ptr().cast(), flags) }; + } +} + /// A ring buffer entry, returned from [`RingBuf::reserve`]. /// /// You must [`submit`] or [`discard`] this entry before it gets dropped. @@ -102,6 +141,18 @@ impl RingBuf { } } + /// Reserve a dynamically sized byte buffer in the ring buffer. + /// + /// Returns `None` if the ring buffer is full. + pub fn reserve_bytes(&self, size: usize, flags: u64) -> Option> { + let ptr = + unsafe { bpf_ringbuf_reserve(self.def.get().cast(), size as u64, flags) }.cast::(); + (!ptr.is_null()).then(|| { + let inner = unsafe { core::slice::from_raw_parts_mut(ptr, size) }; + RingBufBytes(inner) + }) + } + /// Reserve memory in the ring buffer that can fit `T`. /// /// Returns `None` if the ring buffer is full. diff --git a/xtask/public-api/aya-ebpf.txt b/xtask/public-api/aya-ebpf.txt index 2d70495b..76b7cbf9 100644 --- a/xtask/public-api/aya-ebpf.txt +++ b/xtask/public-api/aya-ebpf.txt @@ -471,6 +471,7 @@ pub fn aya_ebpf::maps::ring_buf::RingBuf::output(&self, pub const fn aya_ebpf::maps::ring_buf::RingBuf::pinned(byte_size: u32, flags: u32) -> Self pub fn aya_ebpf::maps::ring_buf::RingBuf::query(&self, flags: u64) -> u64 pub fn aya_ebpf::maps::ring_buf::RingBuf::reserve(&self, flags: u64) -> core::option::Option> +pub fn aya_ebpf::maps::ring_buf::RingBuf::reserve_bytes(&self, size: usize, flags: u64) -> core::option::Option> pub const fn aya_ebpf::maps::ring_buf::RingBuf::with_byte_size(byte_size: u32, flags: u32) -> Self impl core::marker::Sync for aya_ebpf::maps::ring_buf::RingBuf impl !core::marker::Freeze for aya_ebpf::maps::ring_buf::RingBuf @@ -494,6 +495,39 @@ impl core::borrow::BorrowMut for aya_ebpf::maps::ring_buf::RingBuf where T pub fn aya_ebpf::maps::ring_buf::RingBuf::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_ebpf::maps::ring_buf::RingBuf pub fn aya_ebpf::maps::ring_buf::RingBuf::from(t: T) -> T +pub struct aya_ebpf::maps::ring_buf::RingBufBytes<'a>(_) +impl aya_ebpf::maps::ring_buf::RingBufBytes<'_> +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'_>::discard(self, flags: u64) +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'_>::submit(self, flags: u64) +impl core::ops::deref::Deref for aya_ebpf::maps::ring_buf::RingBufBytes<'_> +pub type aya_ebpf::maps::ring_buf::RingBufBytes<'_>::Target = [u8] +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'_>::deref(&self) -> &Self::Target +impl core::ops::deref::DerefMut for aya_ebpf::maps::ring_buf::RingBufBytes<'_> +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'_>::deref_mut(&mut self) -> &mut Self::Target +impl<'a> core::marker::Freeze for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +impl<'a> core::marker::Send for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +impl<'a> core::marker::Sync for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +impl<'a> core::marker::Unpin for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +impl<'a> core::panic::unwind_safe::RefUnwindSafe for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +impl<'a> !core::panic::unwind_safe::UnwindSafe for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +impl core::ops::deref::Receiver for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where P: core::ops::deref::Deref + ?core::marker::Sized, T: ?core::marker::Sized +pub type aya_ebpf::maps::ring_buf::RingBufBytes<'a>::Target = T +impl core::convert::Into for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where U: core::convert::From +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::into(self) -> U +impl core::convert::TryFrom for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where U: core::convert::Into +pub type aya_ebpf::maps::ring_buf::RingBufBytes<'a>::Error = core::convert::Infallible +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::try_from(value: U) -> core::result::Result>::Error> +impl core::convert::TryInto for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where U: core::convert::TryFrom +pub type aya_ebpf::maps::ring_buf::RingBufBytes<'a>::Error = >::Error +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::try_into(self) -> core::result::Result>::Error> +impl core::any::Any for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where T: 'static + ?core::marker::Sized +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::type_id(&self) -> core::any::TypeId +impl core::borrow::Borrow for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where T: ?core::marker::Sized +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::borrow(&self) -> &T +impl core::borrow::BorrowMut for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where T: ?core::marker::Sized +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::borrow_mut(&mut self) -> &mut T +impl core::convert::From for aya_ebpf::maps::ring_buf::RingBufBytes<'a> +pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::from(t: T) -> T pub struct aya_ebpf::maps::ring_buf::RingBufEntry(_) impl aya_ebpf::maps::ring_buf::RingBufEntry pub fn aya_ebpf::maps::ring_buf::RingBufEntry::discard(self, flags: u64) @@ -1198,6 +1232,7 @@ pub fn aya_ebpf::maps::ring_buf::RingBuf::output(&self, pub const fn aya_ebpf::maps::ring_buf::RingBuf::pinned(byte_size: u32, flags: u32) -> Self pub fn aya_ebpf::maps::ring_buf::RingBuf::query(&self, flags: u64) -> u64 pub fn aya_ebpf::maps::ring_buf::RingBuf::reserve(&self, flags: u64) -> core::option::Option> +pub fn aya_ebpf::maps::ring_buf::RingBuf::reserve_bytes(&self, size: usize, flags: u64) -> core::option::Option> pub const fn aya_ebpf::maps::ring_buf::RingBuf::with_byte_size(byte_size: u32, flags: u32) -> Self impl core::marker::Sync for aya_ebpf::maps::ring_buf::RingBuf impl !core::marker::Freeze for aya_ebpf::maps::ring_buf::RingBuf