ring_buf: add RingBufBytes for raw byte slices

reviewable/pr1294/r8
Tamir Duberstein 2 weeks ago
parent 04a8a80b80
commit 8354cbcece
No known key found for this signature in database

@ -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<RingBufBytes<'_>> {
let ptr =
unsafe { bpf_ringbuf_reserve(self.def.get().cast(), size as u64, flags) }.cast::<u8>();
(!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.

@ -471,6 +471,7 @@ pub fn aya_ebpf::maps::ring_buf::RingBuf::output<T: ?core::marker::Sized>(&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<T: 'static>(&self, flags: u64) -> core::option::Option<aya_ebpf::maps::ring_buf::RingBufEntry<T>>
pub fn aya_ebpf::maps::ring_buf::RingBuf::reserve_bytes(&self, size: usize, flags: u64) -> core::option::Option<aya_ebpf::maps::ring_buf::RingBufBytes<'_>>
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<T> core::borrow::BorrowMut<T> for aya_ebpf::maps::ring_buf::RingBuf where T
pub fn aya_ebpf::maps::ring_buf::RingBuf::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> 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<P, T> core::ops::deref::Receiver for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where P: core::ops::deref::Deref<Target = T> + ?core::marker::Sized, T: ?core::marker::Sized
pub type aya_ebpf::maps::ring_buf::RingBufBytes<'a>::Target = T
impl<T, U> core::convert::Into<U> for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where U: core::convert::From<T>
pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::into(self) -> U
impl<T, U> core::convert::TryFrom<U> for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where U: core::convert::Into<T>
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<T, <T as core::convert::TryFrom<U>>::Error>
impl<T, U> core::convert::TryInto<U> for aya_ebpf::maps::ring_buf::RingBufBytes<'a> where U: core::convert::TryFrom<T>
pub type aya_ebpf::maps::ring_buf::RingBufBytes<'a>::Error = <U as core::convert::TryFrom<T>>::Error
pub fn aya_ebpf::maps::ring_buf::RingBufBytes<'a>::try_into(self) -> core::result::Result<U, <U as core::convert::TryFrom<T>>::Error>
impl<T> 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<T> core::borrow::Borrow<T> 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<T> core::borrow::BorrowMut<T> 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<T> core::convert::From<T> 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<T: 'static>(_)
impl<T> aya_ebpf::maps::ring_buf::RingBufEntry<T>
pub fn aya_ebpf::maps::ring_buf::RingBufEntry<T>::discard(self, flags: u64)
@ -1198,6 +1232,7 @@ pub fn aya_ebpf::maps::ring_buf::RingBuf::output<T: ?core::marker::Sized>(&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<T: 'static>(&self, flags: u64) -> core::option::Option<aya_ebpf::maps::ring_buf::RingBufEntry<T>>
pub fn aya_ebpf::maps::ring_buf::RingBuf::reserve_bytes(&self, size: usize, flags: u64) -> core::option::Option<aya_ebpf::maps::ring_buf::RingBufBytes<'_>>
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

Loading…
Cancel
Save