@ -214,10 +214,14 @@ pub enum ProgramError {
} ,
} ,
}
}
/// Allows the Fd of a loaded [`Program`] to be retrieved
/// A [`Program`] file descriptor.
pub trait ProgramFd {
#[ derive(Copy, Clone) ]
/// Returns the [`RawFd`] of the program if it has been loaded, or `None`
pub struct ProgramFd ( RawFd ) ;
fn fd ( & self ) -> Option < RawFd > ;
impl AsRawFd for ProgramFd {
fn as_raw_fd ( & self ) -> RawFd {
self . 0
}
}
}
/// eBPF program type.
/// eBPF program type.
@ -359,6 +363,38 @@ impl Program {
Program ::CgroupSock ( p ) = > p . unload ( ) ,
Program ::CgroupSock ( p ) = > p . unload ( ) ,
}
}
}
}
/// Returns the file descriptor of a program.
///
/// Can be used to add a program to a [`crate::maps::ProgramArray`] or attach an [`Extension`] program.
/// Can be converted to [`RawFd`] using [`AsRawFd`].
pub fn fd ( & self ) -> Option < ProgramFd > {
match self {
Program ::KProbe ( p ) = > p . fd ( ) ,
Program ::UProbe ( p ) = > p . fd ( ) ,
Program ::TracePoint ( p ) = > p . fd ( ) ,
Program ::SocketFilter ( p ) = > p . fd ( ) ,
Program ::Xdp ( p ) = > p . fd ( ) ,
Program ::SkMsg ( p ) = > p . fd ( ) ,
Program ::SkSkb ( p ) = > p . fd ( ) ,
Program ::SockOps ( p ) = > p . fd ( ) ,
Program ::SchedClassifier ( p ) = > p . fd ( ) ,
Program ::CgroupSkb ( p ) = > p . fd ( ) ,
Program ::CgroupSysctl ( p ) = > p . fd ( ) ,
Program ::CgroupSockopt ( p ) = > p . fd ( ) ,
Program ::LircMode2 ( p ) = > p . fd ( ) ,
Program ::PerfEvent ( p ) = > p . fd ( ) ,
Program ::RawTracePoint ( p ) = > p . fd ( ) ,
Program ::Lsm ( p ) = > p . fd ( ) ,
Program ::BtfTracePoint ( p ) = > p . fd ( ) ,
Program ::FEntry ( p ) = > p . fd ( ) ,
Program ::FExit ( p ) = > p . fd ( ) ,
Program ::Extension ( p ) = > p . fd ( ) ,
Program ::CgroupSockAddr ( p ) = > p . fd ( ) ,
Program ::SkLookup ( p ) = > p . fd ( ) ,
Program ::CgroupSock ( p ) = > p . fd ( ) ,
}
}
}
}
impl Drop for Program {
impl Drop for Program {
@ -555,42 +591,6 @@ pub(crate) fn query<T: AsRawFd>(
}
}
}
}
impl ProgramFd for Program {
fn fd ( & self ) -> Option < RawFd > {
match self {
Program ::KProbe ( p ) = > p . data . fd ,
Program ::UProbe ( p ) = > p . data . fd ,
Program ::TracePoint ( p ) = > p . data . fd ,
Program ::SocketFilter ( p ) = > p . data . fd ,
Program ::Xdp ( p ) = > p . data . fd ,
Program ::SkMsg ( p ) = > p . data . fd ,
Program ::SkSkb ( p ) = > p . data . fd ,
Program ::SockOps ( p ) = > p . data . fd ,
Program ::SchedClassifier ( p ) = > p . data . fd ,
Program ::CgroupSkb ( p ) = > p . data . fd ,
Program ::CgroupSysctl ( p ) = > p . data . fd ,
Program ::CgroupSockopt ( p ) = > p . data . fd ,
Program ::LircMode2 ( p ) = > p . data . fd ,
Program ::PerfEvent ( p ) = > p . data . fd ,
Program ::RawTracePoint ( p ) = > p . data . fd ,
Program ::Lsm ( p ) = > p . data . fd ,
Program ::BtfTracePoint ( p ) = > p . data . fd ,
Program ::FEntry ( p ) = > p . data . fd ,
Program ::FExit ( p ) = > p . data . fd ,
Program ::Extension ( p ) = > p . data . fd ,
Program ::CgroupSockAddr ( p ) = > p . data . fd ,
Program ::SkLookup ( p ) = > p . data . fd ,
Program ::CgroupSock ( p ) = > p . data . fd ,
}
}
}
impl < ' a , P : ProgramFd > ProgramFd for & ' a P {
fn fd ( & self ) -> Option < RawFd > {
( * self ) . fd ( )
}
}
macro_rules! impl_program_unload {
macro_rules! impl_program_unload {
( $( $struct_name :ident ) , + $(, ) ? ) = > {
( $( $struct_name :ident ) , + $(, ) ? ) = > {
$(
$(
@ -634,25 +634,20 @@ impl_program_unload!(
CgroupSock ,
CgroupSock ,
) ;
) ;
macro_rules! impl_ program_ fd {
macro_rules! impl_ fd {
( $( $struct_name :ident ) , + $(, ) ? ) = > {
( $( $struct_name :ident ) , + $(, ) ? ) = > {
$(
$(
impl ProgramFd for $struct_name {
impl $struct_name {
fn fd ( & self ) -> Option < RawFd > {
/// Returns the file descriptor of this Program.
self . data . fd
pub fn fd ( & self ) -> Option < ProgramFd > {
}
self . data . fd . map ( | fd | ProgramFd ( fd ) )
}
impl ProgramFd for & mut $struct_name {
fn fd ( & self ) -> Option < RawFd > {
self . data . fd
}
}
}
}
) +
) +
}
}
}
}
impl_ program_ fd! (
impl_ fd! (
KProbe ,
KProbe ,
UProbe ,
UProbe ,
TracePoint ,
TracePoint ,
@ -674,6 +669,7 @@ impl_program_fd!(
Extension ,
Extension ,
CgroupSockAddr ,
CgroupSockAddr ,
SkLookup ,
SkLookup ,
SockOps ,
CgroupSock ,
CgroupSock ,
) ;
) ;