aya/programs/
fexit.rs

1//! Fexit programs.
2
3use crate::{
4    generated::{bpf_attach_type::BPF_TRACE_FEXIT, bpf_prog_type::BPF_PROG_TYPE_TRACING},
5    obj::btf::{Btf, BtfKind},
6    programs::{
7        define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId,
8        ProgramData, ProgramError,
9    },
10};
11
12/// A program that can be attached to the exit point of (almost) anny kernel
13/// function.
14///
15/// [`FExit`] programs are similar to [kretprobes](crate::programs::KProbe),
16/// but the difference is that fexit has practically zero overhead to call
17/// before kernel function. Fexit programs can be also attached to other eBPF
18/// programs.
19///
20/// # Minimum kernel version
21///
22/// The minimum kernel version required to use this feature is 5.5.
23///
24/// # Examples
25///
26/// ```no_run
27/// # #[derive(thiserror::Error, Debug)]
28/// # enum Error {
29/// #     #[error(transparent)]
30/// #     BtfError(#[from] aya::BtfError),
31/// #     #[error(transparent)]
32/// #     Program(#[from] aya::programs::ProgramError),
33/// #     #[error(transparent)]
34/// #     Ebpf(#[from] aya::EbpfError),
35/// # }
36/// # let mut bpf = Ebpf::load_file("ebpf_programs.o")?;
37/// use aya::{Ebpf, programs::FExit, BtfError, Btf};
38///
39/// let btf = Btf::from_sys_fs()?;
40/// let program: &mut FExit = bpf.program_mut("filename_lookup").unwrap().try_into()?;
41/// program.load("filename_lookup", &btf)?;
42/// program.attach()?;
43/// # Ok::<(), Error>(())
44/// ```
45#[derive(Debug)]
46#[doc(alias = "BPF_TRACE_FEXIT")]
47#[doc(alias = "BPF_PROG_TYPE_TRACING")]
48pub struct FExit {
49    pub(crate) data: ProgramData<FExitLink>,
50}
51
52impl FExit {
53    /// Loads the program inside the kernel.
54    ///
55    /// Loads the program so it's executed when the kernel function `fn_name`
56    /// is exited. The `btf` argument must contain the BTF info for the running
57    /// kernel.
58    pub fn load(&mut self, fn_name: &str, btf: &Btf) -> Result<(), ProgramError> {
59        self.data.expected_attach_type = Some(BPF_TRACE_FEXIT);
60        self.data.attach_btf_id = Some(btf.id_by_type_name_kind(fn_name, BtfKind::Func)?);
61        load_program(BPF_PROG_TYPE_TRACING, &mut self.data)
62    }
63
64    /// Attaches the program.
65    ///
66    /// The returned value can be used to detach, see [FExit::detach].
67    pub fn attach(&mut self) -> Result<FExitLinkId, ProgramError> {
68        attach_raw_tracepoint(&mut self.data, None)
69    }
70
71    /// Detaches the program.
72    ///
73    /// See [FExit::attach].
74    pub fn detach(&mut self, link_id: FExitLinkId) -> Result<(), ProgramError> {
75        self.data.links.remove(link_id)
76    }
77
78    /// Takes ownership of the link referenced by the provided link_id.
79    ///
80    /// The link will be detached on `Drop` and the caller is now responsible
81    /// for managing its lifetime.
82    pub fn take_link(&mut self, link_id: FExitLinkId) -> Result<FExitLink, ProgramError> {
83        self.data.take_link(link_id)
84    }
85}
86
87define_link_wrapper!(
88    /// The link used by [FExit] programs.
89    FExitLink,
90    /// The type returned by [FExit::attach]. Can be passed to [FExit::detach].
91    FExitLinkId,
92    FdLink,
93    FdLinkId
94);