Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Program Lifecycle

In Aya, an instance of the Bpf type manages the lifetime of all the eBPF objects created through it.

Consider the following example:

use aya::Bpf;
use aya::programs::{Xdp, XdpFlags};

fn main() {
    {
        // (1)
        let mut bpf = Ebpf::load_file("bpf.o"))?;

        let program: &mut Xdp = bpf.program_mut("xdp").unwrap().try_into().unwrap();
        // (2)
        program.load()?;
        // (3)
        program.attach("eth0", XdpFlags::default()).unwrap();
    }
    // (4)

}
  1. When you call load or load_file, all the maps referenced by the eBPF code are created and stored inside the returned Bpf instance.
  2. Similarly when you load a program to the kernel, it’s stored inside the Bpf instance.
  3. When you attach a program, it stays attached until the parent Bpf instance gets dropped.
  4. At this point the bpf variable has been droppped. Our program and maps are detached/unloaded.