Skip to content

Commit

Permalink
add program_info api
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Andrew Stoycos committed Jul 10, 2023
1 parent 18c7802 commit 15ebdb9
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProgramInfo, ProgramError> {
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 {
Expand Down

0 comments on commit 15ebdb9

Please sign in to comment.