pub struct RingBuffer { /* private fields */ }
Expand description
An interface for using BPF ringbuffer maps.
Implementations§
Source§impl RingBuffer
impl RingBuffer
Sourcepub fn with_capacity(min_capacity: u32) -> Result<Self>
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);
Sourcepub fn get_capacity(&self) -> usize
pub fn get_capacity(&self) -> usize
Returns the capacity of the ring buffer.
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn get_buf(&self) -> &[u8] ⓘ
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);
Sourcepub fn consume(&mut self, size: usize)
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);
Sourcepub fn get_identifier(&self) -> u32
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();