Struct RingBuffer

Source
pub struct RingBuffer { /* private fields */ }
Expand description

An interface for using BPF ringbuffer maps.

Implementations§

Source§

impl RingBuffer

Source

pub fn with_capacity(min_capacity: u32) -> Result<Self>

Creates a new ring buffer with the given number of pages. The capacity of a BPF ring buffer has to be a power of 2 pages. The min value given is rounded up to this capacity; get_capacity will return the actual allocated capacity of the ring buffer.

§Arguments
  • min_capacity - The minimum capacity of the ringbuffer.
§Example
use bpf_api::collections::RingBuffer;

for capacity in [ 1, 4095, 4096, 8191, 8192, 16535, 16536, 40000 ] {
    let ringbuffer = RingBuffer::with_capacity(capacity).expect("Failed to create ringbuffer");
    assert!(ringbuffer.get_capacity() >= capacity as usize);
}

let ringbuffer = RingBuffer::with_capacity(4096).expect("Failed to create ringbuffer");
assert_eq!(ringbuffer.get_capacity(), 4096);
Source

pub fn get_capacity(&self) -> usize

Returns the capacity of the ring buffer.

Source

pub fn len(&self) -> usize

Returns the amount of data available for reading.

§Example
use bpf_api::collections::RingBuffer;

let ringbuffer = RingBuffer::with_capacity(4096).expect("Failed to create ringbuffer");
assert_eq!(ringbuffer.len(), 0);
Source

pub fn is_empty(&self) -> bool

Returns whether the ring buffer is empty or not.

§Example
use bpf_api::collections::RingBuffer;

let ringbuffer = RingBuffer::with_capacity(4096).expect("Failed to create ringbuffer");
assert!(ringbuffer.is_empty());
Source

pub fn get_buf(&self) -> &[u8]

Returns a slice that represents the readable range.

§Example
use bpf_api::collections::RingBuffer;

let ringbuffer = RingBuffer::with_capacity(4096).expect("Failed to create ringbuffer");
assert_eq!(ringbuffer.get_buf().len(), 0);
Source

pub fn consume(&mut self, size: usize)

Advances the read position by the given size. If the size is more than the number of bytes available, the read position is advanced to the write position, clearing the buffer.

§Example
use bpf_api::collections::RingBuffer;

let mut ringbuffer = RingBuffer::with_capacity(4096).expect("Failed to create ringbuffer");
ringbuffer.consume(100);
Source

pub fn get_identifier(&self) -> u32

Retrieve the BPF identifier for this map. This is the underlying file descriptor that’s used in eBPF programs.

§Example
use bpf_api::collections::RingBuffer;

let ringbuffer = RingBuffer::with_capacity(8).expect("Failed to create ringbuffer");
ringbuffer.get_identifier();

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.