Struct libbpf_rs::Map

source ·
pub struct Map { /* private fields */ }
Expand description

Represents a created map.

Some methods require working with raw bytes. You may find libraries such as plain helpful.

Implementations§

source§

impl Map

source

pub fn from_pinned_path<P: AsRef<Path>>(path: P) -> Result<Self>

Open a previously pinned map from its path.

Panics

If the path contains null bytes.

source

pub fn from_map_id(id: u32) -> Result<Self>

Open a loaded map from its map id.

source

pub fn info(&self) -> Result<MapInfo>

Fetch extra map information

source

pub fn name(&self) -> &str

Retrieve the Map’s name.

source

pub fn fd(&self) -> i32

Returns a file descriptor to the underlying map.

source

pub fn map_type(&self) -> MapType

Retrieve type of the map.

source

pub fn key_size(&self) -> u32

Key size in bytes

source

pub fn value_size(&self) -> u32

Value size in bytes

source

pub fn pin<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Pin this map to bpffs.

source

pub fn unpin<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Unpin from bpffs

source

pub fn lookup(&self, key: &[u8], flags: MapFlags) -> Result<Option<Vec<u8>>>

Returns map value as Vec of u8.

key must have exactly Map::key_size() elements.

If the map is one of the per-cpu data structures, the function Map::lookup_percpu() must be used.

source

pub fn lookup_percpu( &self, key: &[u8], flags: MapFlags ) -> Result<Option<Vec<Vec<u8>>>>

Returns one value per cpu as Vec of Vec of u8 for per per-cpu maps.

For normal maps, Map::lookup() must be used.

source

pub fn delete(&self, key: &[u8]) -> Result<()>

Deletes an element from the map.

key must have exactly Map::key_size() elements.

source

pub fn lookup_and_delete(&self, key: &[u8]) -> Result<Option<Vec<u8>>>

Same as Map::lookup() except this also deletes the key from the map.

Note that this operation is currently only implemented in the kernel for MapType::Queue and MapType::Stack.

key must have exactly Map::key_size() elements.

source

pub fn update(&self, key: &[u8], value: &[u8], flags: MapFlags) -> Result<()>

Update an element.

key must have exactly Map::key_size() elements. value must have exactly Map::value_size() elements.

For per-cpu maps, Map::update_percpu() must be used.

source

pub fn update_percpu( &self, key: &[u8], values: &[Vec<u8>], flags: MapFlags ) -> Result<()>

Update an element in an per-cpu map with one value per cpu.

key must have exactly Map::key_size() elements. value must have one element per cpu (see num_possible_cpus) with exactly Map::value_size() elements each.

For per-cpu maps, Map::update_percpu() must be used.

source

pub fn freeze(&self) -> Result<()>

Freeze the map as read-only from user space.

Entries from a frozen map can no longer be updated or deleted with the bpf() system call. This operation is not reversible, and the map remains immutable from user space until its destruction. However, read and write permissions for BPF programs to the map remain unchanged.

source

pub fn keys(&self) -> MapKeyIter<'_>

Returns an iterator over keys in this map

Note that if the map is not stable (stable meaning no updates or deletes) during iteration, iteration can skip keys, restart from the beginning, or duplicate keys. In other words, iteration becomes unpredictable.

source

pub fn create<T: AsRef<str>>( map_type: MapType, name: Option<T>, key_size: u32, value_size: u32, max_entries: u32, opts: &bpf_map_create_opts ) -> Result<Map>

Create the bpf map standalone.

source

pub fn attach_struct_ops(&self) -> Result<Link>

Attach a struct ops map

source

pub fn as_libbpf_bpf_map_ptr(&self) -> Option<NonNull<bpf_map>>

Retrieve the underlying libbpf_sys::bpf_map.

Trait Implementations§

source§

impl Debug for Map

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Map

§

impl !Send for Map

§

impl !Sync for Map

§

impl Unpin for Map

§

impl UnwindSafe for Map

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.