pub struct BpfLoader<'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 BpfLoader to customize some of the loading options.

Examples

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

let bpf = BpfLoader::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

Creates a new loader instance.

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::{BpfLoader, Btf, Endianness};

let bpf = BpfLoader::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")?;

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::BpfLoader;

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

Sets the value of a global variable

From Rust eBPF, a global variable would be constructed as follows:

#[no_mangle]
static VERSION: i32 = 0;

Then it would be accessed with core::ptr::read_volatile inside functions:

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

If using a struct, ensure that it is #[repr(C)] to ensure the size will match that of the corresponding ELF symbol.

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

Example
use aya::BpfLoader;

let bpf = BpfLoader::new()
    .set_global("VERSION", &2)
    .load_file("file.o")?;

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::BpfLoader;

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

Loads eBPF bytecode from a file.

Examples
use aya::BpfLoader;

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

Loads eBPF bytecode from a buffer.

Examples
use aya::BpfLoader;
use std::fs;

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

Trait Implementations

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.