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>
impl<'a> EbpfLoader<'a>
Sourcepub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut Self
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")?;
Sourcepub fn allow_unsupported_maps(&mut self) -> &mut Self
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")?;Sourcepub fn map_pin_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Self
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")?;Sourcepub fn set_global<T: Into<GlobalData<'a>>>(
&mut self,
name: &'a str,
value: T,
must_exist: bool,
) -> &mut Self
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")?;Sourcepub fn set_max_entries(&mut self, name: &'a str, size: u32) -> &mut Self
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")?;Sourcepub fn verifier_log_level(&mut self, level: VerifierLogLevel) -> &mut Self
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")?;