EbpfLoader

Struct EbpfLoader 

Source
pub struct EbpfLoader<'a> { /* private fields */ }
Expand description

Builder style API for advanced loading of eBPF programs.

Loading eBPF code involves a few steps, including loading maps and applying relocations. You can use EbpfLoader to customize some of the loading options.

§Examples

use aya::{EbpfLoader, Btf};
use std::fs;

let bpf = EbpfLoader::new()
    // load the BTF data from /sys/kernel/btf/vmlinux
    .btf(Btf::from_sys_fs().ok().as_ref())
    // load pinned maps from /sys/fs/bpf/my-program
    .map_pin_path("/sys/fs/bpf/my-program")
    // finally load the code
    .load_file("file.o")?;

Implementations§

Source§

impl<'a> EbpfLoader<'a>

Source

pub fn new() -> Self

Creates a new loader instance.

Source

pub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut Self

Sets the target BTF info.

The loader defaults to loading BTF info using Btf::from_sys_fs. Use this method if you want to load BTF from a custom location or pass None to disable BTF relocations entirely.

§Example
use aya::{EbpfLoader, Btf, Endianness};

let bpf = EbpfLoader::new()
    // load the BTF data from a custom location
    .btf(Btf::parse_file("/custom_btf_file", Endianness::default()).ok().as_ref())
    .load_file("file.o")?;
Source

pub fn allow_unsupported_maps(&mut self) -> &mut Self

Allows programs containing unsupported maps to be loaded.

By default programs containing unsupported maps will fail to load. This method can be used to configure the loader so that unsupported maps will be loaded, but won’t be accessible from userspace. Can be useful when using unsupported maps that are only accessed from eBPF code and don’t require any userspace interaction.

§Example
use aya::EbpfLoader;

let bpf = EbpfLoader::new()
    .allow_unsupported_maps()
    .load_file("file.o")?;
Source

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

Sets the base directory path for pinned maps.

Pinned maps will be loaded from path/MAP_NAME. The caller is responsible for ensuring the directory exists.

§Example
use aya::EbpfLoader;

let bpf = EbpfLoader::new()
    .map_pin_path("/sys/fs/bpf/my-program")
    .load_file("file.o")?;
Source

pub fn set_global<T: Into<GlobalData<'a>>>( &mut self, name: &'a str, value: T, must_exist: bool, ) -> &mut Self

Sets the value of a global variable.

If the must_exist argument is true, EbpfLoader::load will fail with ParseError::SymbolNotFound if the loaded object code does not contain the variable.

From Rust eBPF, a global variable can be defined as follows:

#[no_mangle]
static VERSION: i32 = 0;

Then it can be accessed using core::ptr::read_volatile:

let version = core::ptr::read_volatile(&VERSION);

The type of a global variable must be Pod (plain old data), for instance u8, u32 and all other primitive types. You may use custom types as well, but you must ensure that those types are #[repr(C)] and only contain other Pod types.

From C eBPF, you would annotate a global variable as volatile const.

§Example
use aya::EbpfLoader;

let bpf = EbpfLoader::new()
    .set_global("VERSION", &2, true)
    .set_global("PIDS", &[1234u16, 5678], true)
    .load_file("file.o")?;
Source

pub fn set_max_entries(&mut self, name: &'a str, size: u32) -> &mut Self

Set the max_entries for specified map.

Overwrite the value of max_entries of the map that matches the provided name before the map is created.

§Example
use aya::EbpfLoader;

let bpf = EbpfLoader::new()
    .set_max_entries("map", 64)
    .load_file("file.o")?;
Source

pub fn extension(&mut self, name: &'a str) -> &mut Self

Treat the provided program as an Extension

When attempting to load the program with the provided name the program type is forced to be ] Extension and is not inferred from the ELF section name.

§Example
use aya::EbpfLoader;

let bpf = EbpfLoader::new()
    .extension("myfunc")
    .load_file("file.o")?;
Source

pub fn verifier_log_level(&mut self, level: VerifierLogLevel) -> &mut Self

Sets BPF verifier log level.

§Example
use aya::{EbpfLoader, VerifierLogLevel};

let bpf = EbpfLoader::new()
    .verifier_log_level(VerifierLogLevel::VERBOSE | VerifierLogLevel::STATS)
    .load_file("file.o")?;
Source

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

Loads eBPF bytecode from a file.

§Examples
use aya::EbpfLoader;

let bpf = EbpfLoader::new().load_file("file.o")?;
Source

pub fn load(&mut self, data: &[u8]) -> Result<Ebpf, EbpfError>

Loads eBPF bytecode from a buffer.

§Examples
use aya::EbpfLoader;
use std::fs;

let data = fs::read("file.o").unwrap();
let bpf = EbpfLoader::new().load(&data)?;

Trait Implementations§

Source§

impl<'a> Debug for EbpfLoader<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for EbpfLoader<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for EbpfLoader<'a>

§

impl<'a> RefUnwindSafe for EbpfLoader<'a>

§

impl<'a> Send for EbpfLoader<'a>

§

impl<'a> Sync for EbpfLoader<'a>

§

impl<'a> Unpin for EbpfLoader<'a>

§

impl<'a> UnwindSafe for EbpfLoader<'a>

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.