From 15ebdb92c171b46bb6c27b14a3b37441fdb8aa6b Mon Sep 17 00:00:00 2001 From: Andrew Stoycos Date: Wed, 5 Jul 2023 15:05:02 -0400 Subject: [PATCH] add program_info api Add a new API macro to each aya `Program` type to allow us to fetch it's accompanying `ProgramInfo` metadata after it's been loaded. Signed-off-by: Andrew Stoycos --- aya/src/programs/mod.rs | 54 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index b99780ef2..423858940 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -914,8 +914,60 @@ impl_try_from_program!( CgroupDevice, ); +/// Returns information about a loaded program with the [`ProgramInfo`] structure. +/// +/// This information is populated at load time by the kernel and can be used +/// to correlate a given [`Program`] to it's corresponding [`ProgramInfo`] +/// metadata. +macro_rules! impl_program_info { + ($($struct_name:ident),+ $(,)?) => { + $( + impl $struct_name { + /// Returns the file descriptor of this Program. + pub fn program_info(&self) -> Result { + let fd = self.fd().ok_or(ProgramError::NotLoaded)?; + + bpf_prog_get_info_by_fd(fd.as_raw_fd(), None) + .map_err(|io_error| ProgramError::SyscallError { + call: "bpf_prog_get_info_by_fd".to_owned(), + io_error, + }) + .map(ProgramInfo) + } + } + )+ + } +} + +impl_program_info!( + KProbe, + UProbe, + TracePoint, + SocketFilter, + Xdp, + SkMsg, + SkSkb, + SchedClassifier, + CgroupSkb, + CgroupSysctl, + CgroupSockopt, + LircMode2, + PerfEvent, + Lsm, + RawTracePoint, + BtfTracePoint, + FEntry, + FExit, + Extension, + CgroupSockAddr, + SkLookup, + SockOps, + CgroupSock, + CgroupDevice, +); + /// Provides information about a loaded program, like name, id and statistics -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ProgramInfo(bpf_prog_info); impl ProgramInfo {