aya/programs/fentry.rs
1//! Fentry programs.
2
3use crate::{
4 generated::{bpf_attach_type::BPF_TRACE_FENTRY, 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 entry point of (almost) any kernel
13/// function.
14///
15/// [`FEntry`] programs are similar to [kprobes](crate::programs::KProbe), but
16/// the difference is that fentry has practically zero overhead to call before
17/// kernel function. Fentry 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::FEntry, BtfError, Btf};
38///
39/// let btf = Btf::from_sys_fs()?;
40/// let program: &mut FEntry = 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_FENTRY")]
47#[doc(alias = "BPF_PROG_TYPE_TRACING")]
48pub struct FEntry {
49 pub(crate) data: ProgramData<FEntryLink>,
50}
51
52impl FEntry {
53 /// Loads the program inside the kernel.
54 ///
55 /// Loads the program so it's executed when the kernel function `fn_name`
56 /// is entered. The `btf` argument must contain the BTF info for the
57 /// running kernel.
58 pub fn load(&mut self, fn_name: &str, btf: &Btf) -> Result<(), ProgramError> {
59 self.data.expected_attach_type = Some(BPF_TRACE_FENTRY);
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 [FEntry::detach].
67 pub fn attach(&mut self) -> Result<FEntryLinkId, ProgramError> {
68 attach_raw_tracepoint(&mut self.data, None)
69 }
70
71 /// Detaches the program.
72 ///
73 /// See [FEntry::attach].
74 pub fn detach(&mut self, link_id: FEntryLinkId) -> 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: FEntryLinkId) -> Result<FEntryLink, ProgramError> {
83 self.data.take_link(link_id)
84 }
85}
86
87define_link_wrapper!(
88 /// The link used by [FEntry] programs.
89 FEntryLink,
90 /// The type returned by [FEntry::attach]. Can be passed to [FEntry::detach].
91 FEntryLinkId,
92 FdLink,
93 FdLinkId
94);