pub struct Ebpf { /* private fields */ }Expand description
The main entry point into the library, used to work with eBPF programs and maps.
Implementations§
Source§impl Ebpf
impl Ebpf
Sourcepub fn load_file<P: AsRef<Path>>(path: P) -> Result<Self, EbpfError>
pub fn load_file<P: AsRef<Path>>(path: P) -> Result<Self, EbpfError>
Loads eBPF bytecode from a file.
Parses the given object code file and initializes the maps defined in it. If
the kernel supports BTF debug info, it is automatically loaded from
/sys/kernel/btf/vmlinux.
For more loading options, see EbpfLoader.
§Examples
use aya::Ebpf;
let bpf = Ebpf::load_file("file.o")?;Sourcepub fn load(data: &[u8]) -> Result<Self, EbpfError>
pub fn load(data: &[u8]) -> Result<Self, EbpfError>
Loads eBPF bytecode from a buffer.
Parses the object code contained in data and initializes the
maps defined in it. If the kernel supports BTF
debug info, it is automatically loaded from /sys/kernel/btf/vmlinux.
For more loading options, see EbpfLoader.
§Examples
use aya::{Ebpf, Btf};
use std::fs;
let data = fs::read("file.o").unwrap();
// load the BTF data from /sys/kernel/btf/vmlinux
let bpf = Ebpf::load(&data)?;Sourcepub fn map(&self, name: &str) -> Option<&Map>
pub fn map(&self, name: &str) -> Option<&Map>
Returns a reference to the map with the given name.
The returned type is mostly opaque. In order to do anything useful with it you need to convert it to a typed map.
For more details and examples on maps and their usage, see the maps module documentation.
Sourcepub fn map_mut(&mut self, name: &str) -> Option<&mut Map>
pub fn map_mut(&mut self, name: &str) -> Option<&mut Map>
Returns a mutable reference to the map with the given name.
The returned type is mostly opaque. In order to do anything useful with it you need to convert it to a typed map.
For more details and examples on maps and their usage, see the maps module documentation.
Sourcepub fn take_map(&mut self, name: &str) -> Option<Map>
pub fn take_map(&mut self, name: &str) -> Option<Map>
Takes ownership of a map with the given name.
Use this when borrowing with map or map_mut
is not possible (eg when using the map from an async task). The returned
map will be closed on Drop, therefore the caller is responsible for
managing its lifetime.
The returned type is mostly opaque. In order to do anything useful with it you need to convert it to a typed map.
For more details and examples on maps and their usage, see the maps module documentation.
Sourcepub fn maps(&self) -> impl Iterator<Item = (&str, &Map)>
pub fn maps(&self) -> impl Iterator<Item = (&str, &Map)>
An iterator over all the maps.
§Examples
for (name, map) in bpf.maps() {
println!(
"found map `{}`",
name,
);
}Sourcepub fn maps_mut(&mut self) -> impl Iterator<Item = (&str, &mut Map)>
pub fn maps_mut(&mut self) -> impl Iterator<Item = (&str, &mut Map)>
A mutable iterator over all the maps.
§Examples
for (_, map) in bpf.maps_mut() {
map.pin(pin_path)?;
}Sourcepub fn program(&self, name: &str) -> Option<&Program>
pub fn program(&self, name: &str) -> Option<&Program>
Returns a reference to the program with the given name.
You can use this to inspect a program and its properties. To load and attach a program, use program_mut instead.
For more details on programs and their usage, see the programs module documentation.
§Examples
let program = bpf.program("SSL_read").unwrap();
println!("program SSL_read is of type {:?}", program.prog_type());Sourcepub fn program_mut(&mut self, name: &str) -> Option<&mut Program>
pub fn program_mut(&mut self, name: &str) -> Option<&mut Program>
Returns a mutable reference to the program with the given name.
Used to get a program before loading and attaching it. For more details on programs and their usage, see the programs module documentation.
§Examples
use aya::programs::UProbe;
let program: &mut UProbe = bpf.program_mut("SSL_read").unwrap().try_into()?;
program.load()?;
program.attach(Some("SSL_read"), 0, "libssl", None)?;