@ -56,19 +56,18 @@ mod xdp;
use libc ::{ close , dup , ENOSPC } ;
use std ::{
cell ::RefCell ,
cmp ,
convert ::TryFrom ,
ffi ::{ CStr , CString } ,
io ,
os ::unix ::io ::{ AsRawFd , RawFd } ,
path ::Path ,
rc ::Rc ,
} ;
use thiserror ::Error ;
pub use cgroup_skb ::{ CgroupSkb , CgroupSkbAttachType } ;
pub use kprobe ::{ KProbe , KProbeError } ;
use lirc_mode2 ::LircLink ;
pub use lirc_mode2 ::LircMode2 ;
pub use lsm ::{ Lsm , LsmLoadError } ;
use perf_attach ::* ;
@ -78,11 +77,14 @@ pub use raw_trace_point::RawTracePoint;
pub use sk_msg ::SkMsg ;
pub use sk_skb ::{ SkSkb , SkSkbKind } ;
pub use sock_ops ::SockOps ;
use socket_filter ::SocketFilterLink ;
pub use socket_filter ::{ SocketFilter , SocketFilterError } ;
use tc ::TcLink ;
pub use tc ::{ SchedClassifier , TcAttachType , TcError } ;
pub use tp_btf ::{ BtfTracePoint , BtfTracePointError } ;
pub use trace_point ::{ TracePoint , TracePointError } ;
pub use uprobe ::{ UProbe , UProbeError } ;
use xdp ::NlLink ;
pub use xdp ::{ Xdp , XdpError , XdpFlags } ;
use crate ::{
@ -286,7 +288,6 @@ impl Program {
pub ( crate ) struct ProgramData {
pub ( crate ) obj : obj ::Program ,
pub ( crate ) fd : Option < RawFd > ,
pub ( crate ) links : Vec < Rc < RefCell < dyn Link > > > ,
pub ( crate ) expected_attach_type : Option < bpf_attach_type > ,
pub ( crate ) attach_btf_obj_fd : Option < u32 > ,
pub ( crate ) attach_btf_id : Option < u32 > ,
@ -297,12 +298,6 @@ impl ProgramData {
self . fd . ok_or ( ProgramError ::NotLoaded )
}
pub fn link < T : Link + ' static > ( & mut self , link : T ) -> LinkRef {
let link : Rc < RefCell < dyn Link > > = Rc ::new ( RefCell ::new ( link ) ) ;
self . links . push ( Rc ::clone ( & link ) ) ;
LinkRef ::new ( link )
}
pub fn pin < P : AsRef < Path > > ( & mut self , path : P ) -> Result < ( ) , ProgramError > {
let fd = self . fd_or_err ( ) ? ;
let path_string =
@ -479,28 +474,140 @@ pub(crate) fn query<T: AsRawFd>(
}
/// Detach an attached program.
pub trait Link : std ::fmt ::Debug {
pub trait Link {
fn detach ( & mut self ) -> Result < ( ) , ProgramError > ;
}
/// The return type of `program.attach(...)`.
///
/// [` LinkRef `] implements the [`Link`] trait and can be used to detach a
/// [` Owned Link`] implements the [`Link`] trait and can be used to detach a
/// program.
/// An eBPF program's lifetime is directly connected to the OwnedLink's; it must
/// be in scope for as long as one wants the program to remain attached. When
/// dropped, OwnedLink will detach the program.
#[ derive(Debug) ]
pub struct LinkRef {
inner : Rc < RefCell < dyn Link > > ,
pub struct OwnedLink {
pub ( crate ) inner : OwnedLinkImpl ,
}
impl Link for OwnedLink {
fn detach ( & mut self ) -> Result < ( ) , ProgramError > {
self . inner . detach ( )
}
}
impl From < OwnedLinkImpl > for OwnedLink {
fn from ( inner : OwnedLinkImpl ) -> Self {
Self { inner }
}
}
impl From < FdLink > for OwnedLink {
fn from ( l : FdLink ) -> Self {
Self { inner : l . into ( ) }
}
}
impl From < LircLink > for OwnedLink {
fn from ( l : LircLink ) -> Self {
Self { inner : l . into ( ) }
}
}
impl From < NlLink > for OwnedLink {
fn from ( l : NlLink ) -> Self {
Self { inner : l . into ( ) }
}
}
impl From < PerfLink > for OwnedLink {
fn from ( l : PerfLink ) -> Self {
Self { inner : l . into ( ) }
}
}
impl From < ProgAttachLink > for OwnedLink {
fn from ( l : ProgAttachLink ) -> Self {
Self { inner : l . into ( ) }
}
}
impl From < SocketFilterLink > for OwnedLink {
fn from ( l : SocketFilterLink ) -> Self {
Self { inner : l . into ( ) }
}
}
impl LinkRef {
fn new ( link : Rc < RefCell < dyn Link > > ) -> LinkRef {
LinkRef { inner : link }
impl From < TcLink > for OwnedLink {
fn from( l : TcLink ) -> Sel f {
Self { inner : l . into ( ) }
}
}
impl Link for LinkRef {
#[ derive(Debug) ]
pub ( crate ) enum OwnedLinkImpl {
Fd ( FdLink ) ,
Lirc ( LircLink ) ,
Nl ( NlLink ) ,
Perf ( PerfLink ) ,
ProgAttach ( ProgAttachLink ) ,
SocketFilter ( SocketFilterLink ) ,
Tc ( TcLink ) ,
}
impl Link for OwnedLinkImpl {
fn detach ( & mut self ) -> Result < ( ) , ProgramError > {
self . inner . borrow_mut ( ) . detach ( )
match self {
Self ::Fd ( link ) = > link . detach ( ) ,
Self ::Lirc ( link ) = > link . detach ( ) ,
Self ::Nl ( link ) = > link . detach ( ) ,
Self ::Perf ( link ) = > link . detach ( ) ,
Self ::ProgAttach ( link ) = > link . detach ( ) ,
Self ::SocketFilter ( link ) = > link . detach ( ) ,
Self ::Tc ( link ) = > link . detach ( ) ,
}
}
}
impl From < FdLink > for OwnedLinkImpl {
fn from ( l : FdLink ) -> Self {
Self ::Fd ( l )
}
}
impl From < LircLink > for OwnedLinkImpl {
fn from ( l : LircLink ) -> Self {
Self ::Lirc ( l )
}
}
impl From < NlLink > for OwnedLinkImpl {
fn from ( l : NlLink ) -> Self {
Self ::Nl ( l )
}
}
impl From < PerfLink > for OwnedLinkImpl {
fn from ( l : PerfLink ) -> Self {
Self ::Perf ( l )
}
}
impl From < ProgAttachLink > for OwnedLinkImpl {
fn from ( l : ProgAttachLink ) -> Self {
Self ::ProgAttach ( l )
}
}
impl From < SocketFilterLink > for OwnedLinkImpl {
fn from ( l : SocketFilterLink ) -> Self {
Self ::SocketFilter ( l )
}
}
impl From < TcLink > for OwnedLinkImpl {
fn from ( l : TcLink ) -> Self {
Self ::Tc ( l )
}
}
@ -527,7 +634,7 @@ impl Drop for FdLink {
}
#[ derive(Debug) ]
struct ProgAttachLink {
pub ( crate ) struct ProgAttachLink {
prog_fd : Option < RawFd > ,
target_fd : Option < RawFd > ,
attach_type : bpf_attach_type ,