Skip to content

Commit

Permalink
WIP: auto-attach tracepoints based on SEC() data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ablu committed Oct 15, 2024
1 parent 758a4f9 commit 471e822
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
13 changes: 10 additions & 3 deletions aya-obj/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ pub enum ProgramSection {
URetProbe {
sleepable: bool,
},
TracePoint,
TracePoint {
category: Option<String>,
name: Option<String>,
},
SocketFilter,
Xdp {
frags: bool,
Expand Down Expand Up @@ -330,7 +333,11 @@ impl FromStr for ProgramSection {
},
},
"tp_btf" => BtfTracePoint,
"tracepoint" | "tp" => TracePoint,
"tracepoint" | "tp" => {
let category = pieces.next().map(|s| s.to_string());
let name = pieces.next().map(|s| s.to_string());
TracePoint { category, name }
}
"socket" => SocketFilter,
"sk_msg" => SkMsg,
"sk_skb" => {
Expand Down Expand Up @@ -2014,7 +2021,7 @@ mod tests {
assert_matches!(
obj.parse_section(fake_section(
EbpfSectionKind::Program,
"tracepoint/foo",
"tracepoint/cat/name",
bytes_of(&fake_ins()),
None
)),
Expand Down
30 changes: 22 additions & 8 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ use crate::{
Object, ParseError, ProgramSection,
},
programs::{
BtfTracePoint, CgroupDevice, CgroupSkb, CgroupSkbAttachType, CgroupSock, CgroupSockAddr,
CgroupSockopt, CgroupSysctl, Extension, FEntry, FExit, KProbe, LircMode2, Lsm, PerfEvent,
ProbeKind, Program, ProgramData, ProgramError, RawTracePoint, SchedClassifier, SkLookup,
SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter, TracePoint, UProbe, Xdp,
trace_point::TracePointAttachInfo, BtfTracePoint, CgroupDevice, CgroupSkb,
CgroupSkbAttachType, CgroupSock, CgroupSockAddr, CgroupSockopt, CgroupSysctl, Extension,
FEntry, FExit, KProbe, LircMode2, Lsm, PerfEvent, ProbeKind, Program, ProgramData,
ProgramError, RawTracePoint, SchedClassifier, SkLookup, SkMsg, SkSkb, SkSkbKind, SockOps,
SocketFilter, TracePoint, UProbe, Xdp,
},
sys::{
bpf_load_btf, is_bpf_cookie_supported, is_bpf_global_data_supported,
Expand Down Expand Up @@ -417,7 +418,10 @@ impl<'a> EbpfLoader<'a> {
| ProgramSection::KProbe
| ProgramSection::UProbe { sleepable: _ }
| ProgramSection::URetProbe { sleepable: _ }
| ProgramSection::TracePoint
| ProgramSection::TracePoint {
category: _,
name: _,
}
| ProgramSection::SocketFilter
| ProgramSection::Xdp {
frags: _,
Expand Down Expand Up @@ -572,9 +576,19 @@ impl<'a> EbpfLoader<'a> {
kind: ProbeKind::URetProbe,
})
}
ProgramSection::TracePoint => Program::TracePoint(TracePoint {
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
}),
ProgramSection::TracePoint { category, name } => {
let expected_attach_info = match (category, name) {
(Some(category), Some(name)) => Some(TracePointAttachInfo {
category: category.clone(),
name: name.clone(),
}),
_ => None,
};
Program::TracePoint(TracePoint {
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
expected_attach_info,
})
}
ProgramSection::SocketFilter => Program::SocketFilter(SocketFilter {
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
}),
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ pub enum ProgramError {
#[error("the program is not attached")]
NotAttached,

/// The program cannot be auto attached.
#[error("the program cannot be auto attached")]
CannotAutoAttach,

/// Loading the program failed.
#[error("the BPF_PROG_LOAD syscall failed. Verifier output: {verifier_log}")]
LoadError {
Expand Down Expand Up @@ -939,7 +943,6 @@ macro_rules! impl_from_pin {

// Use impl_from_pin if the program doesn't require additional data
impl_from_pin!(
TracePoint,
SocketFilter,
SkMsg,
CgroupSysctl,
Expand Down
37 changes: 37 additions & 0 deletions aya/src/programs/trace_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
FdLink, LinkError, ProgramData, ProgramError,
},
sys::{bpf_link_get_info_by_fd, perf_event_open_trace_point, SyscallError},
VerifierLogLevel,
};

/// The type returned when attaching a [`TracePoint`] fails.
Expand All @@ -28,6 +29,15 @@ pub enum TracePointError {
},
}

/// Defines where to attach trace point
#[derive(Debug)]
pub struct TracePointAttachInfo {
/// Category of trace point
pub category: String,
/// Name of trace point
pub name: String,
}

/// A program that can be attached at a pre-defined kernel trace point.
///
/// The kernel provides a set of pre-defined trace points that eBPF programs can
Expand All @@ -53,6 +63,7 @@ pub enum TracePointError {
#[doc(alias = "BPF_PROG_TYPE_TRACEPOINT")]
pub struct TracePoint {
pub(crate) data: ProgramData<TracePointLink>,
pub(crate) expected_attach_info: Option<TracePointAttachInfo>,
}

impl TracePoint {
Expand Down Expand Up @@ -82,6 +93,18 @@ impl TracePoint {
self.data.links.insert(TracePointLink::new(link))
}

/// Returns the attach info of the trace point
pub fn auto_attach(&mut self) -> Result<TracePointLinkId, ProgramError> {
let attach_info = self
.expected_attach_info
.as_ref()
.ok_or(ProgramError::CannotAutoAttach)?;
let category = attach_info.category.clone();
let name = attach_info.name.clone();

self.attach(&category, &name)
}

/// Detaches from a trace point.
///
/// See [TracePoint::attach].
Expand All @@ -96,6 +119,20 @@ impl TracePoint {
pub fn take_link(&mut self, link_id: TracePointLinkId) -> Result<TracePointLink, ProgramError> {
self.data.take_link(link_id)
}

/// Creates a program from a pinned entry on a bpffs.
///
/// Existing links will not be populated. To work with existing links you should use [`crate::programs::links::PinnedLink`].
///
/// On drop, any managed links are detached and the program is unloaded. This will not result in
/// the program being unloaded from the kernel if it is still pinned.
pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, ProgramError> {
let data = ProgramData::from_pinned_path(path, VerifierLogLevel::default())?;
Ok(Self {
data,
expected_attach_info: None,
})
}
}

define_link_wrapper!(
Expand Down

0 comments on commit 471e822

Please sign in to comment.