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
sourceimpl<'a> BpfLoader<'a>
impl<'a> BpfLoader<'a>
sourcepub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut BpfLoader<'a>
pub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut BpfLoader<'a>
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")?;
sourcepub fn map_pin_path<P: AsRef<Path>>(&mut self, path: P) -> &mut BpfLoader<'a>
pub fn map_pin_path<P: AsRef<Path>>(&mut self, path: P) -> &mut BpfLoader<'a>
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")?;
sourcepub fn set_global<V: Pod>(
&mut self,
name: &'a str,
value: &'a V
) -> &mut BpfLoader<'a>
pub fn set_global<V: Pod>(
&mut self,
name: &'a str,
value: &'a V
) -> &mut BpfLoader<'a>
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")?;
Trait Implementations
Auto Trait Implementations
impl<'a> RefUnwindSafe for BpfLoader<'a>
impl<'a> Send for BpfLoader<'a>
impl<'a> Sync for BpfLoader<'a>
impl<'a> Unpin for BpfLoader<'a>
impl<'a> UnwindSafe for BpfLoader<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more