From d80892cbf257a5d034351d52b754905f1ca46648 Mon Sep 17 00:00:00 2001 From: Thia Wyrod Date: Sun, 21 Nov 2021 15:58:15 -0800 Subject: [PATCH] aya: remove Rc> usage in BPF program links. This is a breaking change which transfers all ownership of a link to an eBPF program to the caller return value of the "attach" functions. This eliminates the overhead of Rc>, as well as prevents the links section of ProgramData from growing without bound as programs are attached and detached. --- aya/src/bpf.rs | 1 - aya/src/maps/sock/mod.rs | 6 ++ aya/src/programs/cgroup_skb.rs | 12 +-- aya/src/programs/kprobe.rs | 4 +- aya/src/programs/lirc_mode2.rs | 6 +- aya/src/programs/lsm.rs | 8 +- aya/src/programs/mod.rs | 145 ++++++++++++++++++++++++---- aya/src/programs/perf_attach.rs | 15 +-- aya/src/programs/perf_event.rs | 4 +- aya/src/programs/probe.rs | 4 +- aya/src/programs/raw_trace_point.rs | 6 +- aya/src/programs/sk_msg.rs | 8 +- aya/src/programs/sk_skb.rs | 8 +- aya/src/programs/sock_ops.rs | 8 +- aya/src/programs/socket_filter.rs | 11 ++- aya/src/programs/tc.rs | 11 ++- aya/src/programs/tp_btf.rs | 6 +- aya/src/programs/trace_point.rs | 4 +- aya/src/programs/uprobe.rs | 4 +- aya/src/programs/xdp.rs | 30 ++---- 20 files changed, 195 insertions(+), 106 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 0ae2bf61..533d488b 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -251,7 +251,6 @@ impl<'a> BpfLoader<'a> { let data = ProgramData { obj, fd: None, - links: Vec::new(), expected_attach_type: None, attach_btf_obj_fd: None, attach_btf_id: None, diff --git a/aya/src/maps/sock/mod.rs b/aya/src/maps/sock/mod.rs index df63c64b..d5f2c42a 100644 --- a/aya/src/maps/sock/mod.rs +++ b/aya/src/maps/sock/mod.rs @@ -11,3 +11,9 @@ pub use sock_map::SockMap; pub trait SocketMap { fn fd_or_err(&self) -> Result; } + +impl<'a, S: SocketMap> SocketMap for &'a S { + fn fd_or_err(&self) -> Result { + (*self).fd_or_err() + } +} diff --git a/aya/src/programs/cgroup_skb.rs b/aya/src/programs/cgroup_skb.rs index 9aff1295..63a8671d 100644 --- a/aya/src/programs/cgroup_skb.rs +++ b/aya/src/programs/cgroup_skb.rs @@ -5,12 +5,10 @@ use crate::{ bpf_attach_type::{BPF_CGROUP_INET_EGRESS, BPF_CGROUP_INET_INGRESS}, bpf_prog_type::BPF_PROG_TYPE_CGROUP_SKB, }, - programs::{load_program, LinkRef, ProgAttachLink, ProgramData, ProgramError}, + programs::{load_program, FdLink, OwnedLink, ProgAttachLink, ProgramData, ProgramError}, sys::{bpf_link_create, bpf_prog_attach, kernel_version}, }; -use super::FdLink; - /// A program used to inspect or filter network activity for a given cgroup. /// /// [`CgroupSkb`] programs can be used to inspect or filter network activity @@ -78,7 +76,7 @@ impl CgroupSkb { &mut self, cgroup: T, attach_type: CgroupSkbAttachType, - ) -> Result { + ) -> Result { let prog_fd = self.data.fd_or_err()?; let cgroup_fd = cgroup.as_raw_fd(); @@ -95,7 +93,7 @@ impl CgroupSkb { io_error, } })? as RawFd; - Ok(self.data.link(FdLink { fd: Some(link_fd) })) + Ok(FdLink { fd: Some(link_fd) }.into()) } else { bpf_prog_attach(prog_fd, cgroup_fd, attach_type).map_err(|(_, io_error)| { ProgramError::SyscallError { @@ -104,9 +102,7 @@ impl CgroupSkb { } })?; - Ok(self - .data - .link(ProgAttachLink::new(prog_fd, cgroup_fd, attach_type))) + Ok(ProgAttachLink::new(prog_fd, cgroup_fd, attach_type).into()) } } } diff --git a/aya/src/programs/kprobe.rs b/aya/src/programs/kprobe.rs index ca7cc963..53ec0340 100644 --- a/aya/src/programs/kprobe.rs +++ b/aya/src/programs/kprobe.rs @@ -7,7 +7,7 @@ use crate::{ programs::{ load_program, probe::{attach, ProbeKind}, - LinkRef, ProgramData, ProgramError, + OwnedLink, ProgramData, ProgramError, }, }; @@ -65,7 +65,7 @@ impl KProbe { /// If the program is a `kprobe`, it is attached to the *start* address of the target function. /// Conversely if the program is a `kretprobe`, it is attached to the return address of the /// target function. - pub fn attach(&mut self, fn_name: &str, offset: u64) -> Result { + pub fn attach(&mut self, fn_name: &str, offset: u64) -> Result { attach(&mut self.data, self.kind, fn_name, offset, None) } } diff --git a/aya/src/programs/lirc_mode2.rs b/aya/src/programs/lirc_mode2.rs index 25f30b53..e401aa09 100644 --- a/aya/src/programs/lirc_mode2.rs +++ b/aya/src/programs/lirc_mode2.rs @@ -2,7 +2,7 @@ use std::os::unix::prelude::{AsRawFd, RawFd}; use crate::{ generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2}, - programs::{load_program, query, Link, LinkRef, ProgramData, ProgramError, ProgramInfo}, + programs::{load_program, query, Link, OwnedLink, ProgramData, ProgramError, ProgramInfo}, sys::{bpf_obj_get_info_by_fd, bpf_prog_attach, bpf_prog_detach, bpf_prog_get_fd_by_id}, }; @@ -60,7 +60,7 @@ impl LircMode2 { } /// Attaches the program to the given lirc device. - pub fn attach(&mut self, lircdev: T) -> Result { + pub fn attach(&mut self, lircdev: T) -> Result { let prog_fd = self.data.fd_or_err()?; let lircdev_fd = lircdev.as_raw_fd(); @@ -71,7 +71,7 @@ impl LircMode2 { } })?; - Ok(self.data.link(LircLink::new(prog_fd, lircdev_fd))) + Ok(LircLink::new(prog_fd, lircdev_fd).into()) } /// Queries the lirc device for attached programs. diff --git a/aya/src/programs/lsm.rs b/aya/src/programs/lsm.rs index ef057d9b..bc8646e6 100644 --- a/aya/src/programs/lsm.rs +++ b/aya/src/programs/lsm.rs @@ -6,7 +6,7 @@ use thiserror::Error; use crate::{ generated::{bpf_attach_type::BPF_LSM_MAC, bpf_prog_type::BPF_PROG_TYPE_LSM}, obj::btf::{Btf, BtfError, BtfKind}, - programs::{load_program, FdLink, LinkRef, ProgramData, ProgramError}, + programs::{load_program, FdLink, OwnedLink, ProgramData, ProgramError}, sys::bpf_raw_tracepoint_open, }; @@ -84,13 +84,13 @@ impl Lsm { } /// Attaches the program. - pub fn attach(&mut self) -> Result { + pub fn attach(&mut self) -> Result { attach_btf_id(&mut self.data) } } /// Common logic for all BPF program types that attach to a BTF id. -pub(crate) fn attach_btf_id(program_data: &mut ProgramData) -> Result { +pub(crate) fn attach_btf_id(program_data: &mut ProgramData) -> Result { let prog_fd = program_data.fd_or_err()?; // Attaching LSM programs doesn't require providing attach name. LSM @@ -102,5 +102,5 @@ pub(crate) fn attach_btf_id(program_data: &mut ProgramData) -> Result, - pub(crate) links: Vec>>, pub(crate) expected_attach_type: Option, pub(crate) attach_btf_obj_fd: Option, pub(crate) attach_btf_id: Option, @@ -297,12 +298,6 @@ impl ProgramData { self.fd.ok_or(ProgramError::NotLoaded) } - pub fn link(&mut self, link: T) -> LinkRef { - let link: Rc> = Rc::new(RefCell::new(link)); - self.links.push(Rc::clone(&link)); - LinkRef::new(link) - } - pub fn pin>(&mut self, path: P) -> Result<(), ProgramError> { let fd = self.fd_or_err()?; let path_string = @@ -479,28 +474,140 @@ pub(crate) fn query( } /// 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 +/// [`OwnedLink`] 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>, +pub struct OwnedLink { + pub(crate) inner: OwnedLinkImpl, +} + +impl Link for OwnedLink { + fn detach(&mut self) -> Result<(), ProgramError> { + self.inner.detach() + } +} + +impl From for OwnedLink { + fn from(inner: OwnedLinkImpl) -> Self { + Self { inner } + } +} + +impl From for OwnedLink { + fn from(l: FdLink) -> Self { + Self { inner: l.into() } + } +} + +impl From for OwnedLink { + fn from(l: LircLink) -> Self { + Self { inner: l.into() } + } +} + +impl From for OwnedLink { + fn from(l: NlLink) -> Self { + Self { inner: l.into() } + } +} + +impl From for OwnedLink { + fn from(l: PerfLink) -> Self { + Self { inner: l.into() } + } +} + +impl From for OwnedLink { + fn from(l: ProgAttachLink) -> Self { + Self { inner: l.into() } + } +} + +impl From for OwnedLink { + fn from(l: SocketFilterLink) -> Self { + Self { inner: l.into() } + } } -impl LinkRef { - fn new(link: Rc>) -> LinkRef { - LinkRef { inner: link } +impl From for OwnedLink { + fn from(l: TcLink) -> Self { + 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 for OwnedLinkImpl { + fn from(l: FdLink) -> Self { + Self::Fd(l) + } +} + +impl From for OwnedLinkImpl { + fn from(l: LircLink) -> Self { + Self::Lirc(l) + } +} + +impl From for OwnedLinkImpl { + fn from(l: NlLink) -> Self { + Self::Nl(l) + } +} + +impl From for OwnedLinkImpl { + fn from(l: PerfLink) -> Self { + Self::Perf(l) + } +} + +impl From for OwnedLinkImpl { + fn from(l: ProgAttachLink) -> Self { + Self::ProgAttach(l) + } +} + +impl From for OwnedLinkImpl { + fn from(l: SocketFilterLink) -> Self { + Self::SocketFilter(l) + } +} + +impl From 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, target_fd: Option, attach_type: bpf_attach_type, diff --git a/aya/src/programs/perf_attach.rs b/aya/src/programs/perf_attach.rs index d5d12342..485f8a27 100644 --- a/aya/src/programs/perf_attach.rs +++ b/aya/src/programs/perf_attach.rs @@ -7,10 +7,10 @@ use crate::{ PERF_EVENT_IOC_DISABLE, PERF_EVENT_IOC_ENABLE, PERF_EVENT_IOC_SET_BPF, }; -use super::{Link, LinkRef, ProgramData, ProgramError}; +use super::{Link, OwnedLink, ProgramData, ProgramError}; #[derive(Debug)] -struct PerfLink { +pub(crate) struct PerfLink { perf_fd: Option, probe_kind: Option, event_alias: Option, @@ -41,7 +41,7 @@ impl Drop for PerfLink { } } -pub(crate) fn perf_attach(data: &mut ProgramData, fd: RawFd) -> Result { +pub(crate) fn perf_attach(data: &mut ProgramData, fd: RawFd) -> Result { perf_attach_either(data, fd, None, None) } @@ -50,7 +50,7 @@ pub(crate) fn perf_attach_debugfs( fd: RawFd, probe_kind: ProbeKind, event_alias: String, -) -> Result { +) -> Result { perf_attach_either(data, fd, Some(probe_kind), Some(event_alias)) } @@ -59,7 +59,7 @@ fn perf_attach_either( fd: RawFd, probe_kind: Option, event_alias: Option, -) -> Result { +) -> Result { let prog_fd = data.fd_or_err()?; perf_event_ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd).map_err(|(_, io_error)| { ProgramError::SyscallError { @@ -74,9 +74,10 @@ fn perf_attach_either( } })?; - Ok(data.link(PerfLink { + Ok(PerfLink { perf_fd: Some(fd), probe_kind, event_alias, - })) + } + .into()) } diff --git a/aya/src/programs/perf_event.rs b/aya/src/programs/perf_event.rs index d7c0a95d..fb93b5be 100644 --- a/aya/src/programs/perf_event.rs +++ b/aya/src/programs/perf_event.rs @@ -10,7 +10,7 @@ pub use crate::generated::{ perf_hw_cache_id, perf_hw_cache_op_id, perf_hw_cache_op_result_id, perf_hw_id, perf_sw_ids, }; -use super::{load_program, perf_attach, LinkRef, ProgramData, ProgramError}; +use super::{load_program, perf_attach, OwnedLink, ProgramData, ProgramError}; #[repr(u32)] #[derive(Debug, Clone)] @@ -104,7 +104,7 @@ impl PerfEvent { config: u64, scope: PerfEventScope, sample_policy: SamplePolicy, - ) -> Result { + ) -> Result { let (sample_period, sample_frequency) = match sample_policy { SamplePolicy::Period(period) => (period, None), SamplePolicy::Frequency(frequency) => (0, Some(frequency)), diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index 4e0fcacc..5fcbc97b 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -8,7 +8,7 @@ use std::{ use crate::{ programs::{ kprobe::KProbeError, perf_attach, perf_attach_debugfs, - trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, LinkRef, ProgramData, + trace_point::read_sys_fs_trace_point_id, uprobe::UProbeError, OwnedLink, ProgramData, ProgramError, }, sys::{kernel_version, perf_event_open_probe, perf_event_open_trace_point}, @@ -41,7 +41,7 @@ pub(crate) fn attach( fn_name: &str, offset: u64, pid: Option, -) -> Result { +) -> Result { // https://github.com/torvalds/linux/commit/e12f03d7031a977356e3d7b75a68c2185ff8d155 // Use debugfs to create probe let k_ver = kernel_version().unwrap(); diff --git a/aya/src/programs/raw_trace_point.rs b/aya/src/programs/raw_trace_point.rs index 81b0e393..6a43b364 100644 --- a/aya/src/programs/raw_trace_point.rs +++ b/aya/src/programs/raw_trace_point.rs @@ -3,7 +3,7 @@ use std::{ffi::CString, os::unix::io::RawFd}; use crate::{ generated::bpf_prog_type::BPF_PROG_TYPE_RAW_TRACEPOINT, - programs::{load_program, FdLink, LinkRef, ProgramData, ProgramError}, + programs::{load_program, FdLink, OwnedLink, ProgramData, ProgramError}, sys::bpf_raw_tracepoint_open, }; @@ -46,7 +46,7 @@ impl RawTracePoint { } /// Attaches the program to the given tracepoint. - pub fn attach(&mut self, tp_name: &str) -> Result { + pub fn attach(&mut self, tp_name: &str) -> Result { let prog_fd = self.data.fd_or_err()?; let name = CString::new(tp_name).unwrap(); @@ -57,6 +57,6 @@ impl RawTracePoint { } })? as RawFd; - Ok(self.data.link(FdLink { fd: Some(pfd) })) + Ok(FdLink { fd: Some(pfd) }.into()) } } diff --git a/aya/src/programs/sk_msg.rs b/aya/src/programs/sk_msg.rs index fcb50926..82223486 100644 --- a/aya/src/programs/sk_msg.rs +++ b/aya/src/programs/sk_msg.rs @@ -1,7 +1,7 @@ use crate::{ generated::{bpf_attach_type::BPF_SK_MSG_VERDICT, bpf_prog_type::BPF_PROG_TYPE_SK_MSG}, maps::sock::SocketMap, - programs::{load_program, LinkRef, ProgAttachLink, ProgramData, ProgramError}, + programs::{load_program, OwnedLink, ProgAttachLink, ProgramData, ProgramError}, sys::bpf_prog_attach, }; @@ -68,7 +68,7 @@ impl SkMsg { } /// Attaches the program to the given sockmap. - pub fn attach(&mut self, map: &dyn SocketMap) -> Result { + pub fn attach(&mut self, map: impl SocketMap) -> Result { let prog_fd = self.data.fd_or_err()?; let map_fd = map.fd_or_err()?; @@ -78,8 +78,6 @@ impl SkMsg { io_error, } })?; - Ok(self - .data - .link(ProgAttachLink::new(prog_fd, map_fd, BPF_SK_MSG_VERDICT))) + Ok(ProgAttachLink::new(prog_fd, map_fd, BPF_SK_MSG_VERDICT).into()) } } diff --git a/aya/src/programs/sk_skb.rs b/aya/src/programs/sk_skb.rs index bd07534f..ca69409d 100644 --- a/aya/src/programs/sk_skb.rs +++ b/aya/src/programs/sk_skb.rs @@ -4,7 +4,7 @@ use crate::{ bpf_prog_type::BPF_PROG_TYPE_SK_SKB, }, maps::sock::SocketMap, - programs::{load_program, LinkRef, ProgAttachLink, ProgramData, ProgramError}, + programs::{load_program, OwnedLink, ProgAttachLink, ProgramData, ProgramError}, sys::bpf_prog_attach, }; @@ -59,7 +59,7 @@ impl SkSkb { } /// Attaches the program to the given socket map. - pub fn attach(&mut self, map: &dyn SocketMap) -> Result { + pub fn attach(&mut self, map: impl SocketMap) -> Result { let prog_fd = self.data.fd_or_err()?; let map_fd = map.fd_or_err()?; @@ -73,8 +73,6 @@ impl SkSkb { io_error, } })?; - Ok(self - .data - .link(ProgAttachLink::new(prog_fd, map_fd, attach_type))) + Ok(ProgAttachLink::new(prog_fd, map_fd, attach_type).into()) } } diff --git a/aya/src/programs/sock_ops.rs b/aya/src/programs/sock_ops.rs index 579dafc3..836a54ab 100644 --- a/aya/src/programs/sock_ops.rs +++ b/aya/src/programs/sock_ops.rs @@ -2,7 +2,7 @@ use std::os::unix::io::AsRawFd; use crate::{ generated::{bpf_attach_type::BPF_CGROUP_SOCK_OPS, bpf_prog_type::BPF_PROG_TYPE_SOCK_OPS}, - programs::{load_program, LinkRef, ProgAttachLink, ProgramData, ProgramError}, + programs::{load_program, OwnedLink, ProgAttachLink, ProgramData, ProgramError}, sys::bpf_prog_attach, }; @@ -55,7 +55,7 @@ impl SockOps { } /// Attaches the program to the given cgroup. - pub fn attach(&mut self, cgroup: T) -> Result { + pub fn attach(&mut self, cgroup: T) -> Result { let prog_fd = self.data.fd_or_err()?; let cgroup_fd = cgroup.as_raw_fd(); @@ -65,8 +65,6 @@ impl SockOps { io_error, } })?; - Ok(self - .data - .link(ProgAttachLink::new(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS))) + Ok(ProgAttachLink::new(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS).into()) } } diff --git a/aya/src/programs/socket_filter.rs b/aya/src/programs/socket_filter.rs index 883ed5d2..249a8b34 100644 --- a/aya/src/programs/socket_filter.rs +++ b/aya/src/programs/socket_filter.rs @@ -7,7 +7,7 @@ use thiserror::Error; use crate::{ generated::{bpf_prog_type::BPF_PROG_TYPE_SOCKET_FILTER, SO_ATTACH_BPF, SO_DETACH_BPF}, - programs::{load_program, Link, LinkRef, ProgramData, ProgramError}, + programs::{load_program, Link, OwnedLink, ProgramData, ProgramError}, }; /// The type returned when attaching a [`SocketFilter`] fails. @@ -71,7 +71,7 @@ impl SocketFilter { } /// Attaches the filter on the given socket. - pub fn attach(&mut self, socket: T) -> Result { + pub fn attach(&mut self, socket: T) -> Result { let prog_fd = self.data.fd_or_err()?; let socket = socket.as_raw_fd(); @@ -91,15 +91,16 @@ impl SocketFilter { .into()); } - Ok(self.data.link(SocketFilterLink { + Ok(SocketFilterLink { socket, prog_fd: Some(prog_fd), - })) + } + .into()) } } #[derive(Debug)] -struct SocketFilterLink { +pub(crate) struct SocketFilterLink { socket: RawFd, prog_fd: Option, } diff --git a/aya/src/programs/tc.rs b/aya/src/programs/tc.rs index 7094c04a..40e48ee4 100644 --- a/aya/src/programs/tc.rs +++ b/aya/src/programs/tc.rs @@ -11,7 +11,7 @@ use crate::{ generated::{ bpf_prog_type::BPF_PROG_TYPE_SCHED_CLS, TC_H_CLSACT, TC_H_MIN_EGRESS, TC_H_MIN_INGRESS, }, - programs::{load_program, Link, LinkRef, ProgramData, ProgramError}, + programs::{load_program, Link, OwnedLink, ProgramData, ProgramError}, sys::{ netlink_find_filter_with_name, netlink_qdisc_add_clsact, netlink_qdisc_attach, netlink_qdisc_detach, @@ -88,7 +88,7 @@ pub enum TcError { } #[derive(Debug)] -struct TcLink { +pub(crate) struct TcLink { if_index: i32, attach_type: TcAttachType, prog_fd: Option, @@ -125,7 +125,7 @@ impl SchedClassifier { &mut self, interface: &str, attach_type: TcAttachType, - ) -> Result { + ) -> Result { let prog_fd = self.data.fd_or_err()?; let if_index = ifindex_from_ifname(interface) .map_err(|io_error| TcError::NetlinkError { io_error })?; @@ -133,12 +133,13 @@ impl SchedClassifier { unsafe { netlink_qdisc_attach(if_index as i32, &attach_type, prog_fd, &self.name) } .map_err(|io_error| TcError::NetlinkError { io_error })?; - Ok(self.data.link(TcLink { + Ok(TcLink { if_index: if_index as i32, attach_type, prog_fd: Some(prog_fd), priority, - })) + } + .into()) } } diff --git a/aya/src/programs/tp_btf.rs b/aya/src/programs/tp_btf.rs index 966414df..1f90351f 100644 --- a/aya/src/programs/tp_btf.rs +++ b/aya/src/programs/tp_btf.rs @@ -6,7 +6,7 @@ use thiserror::Error; use crate::{ generated::{bpf_attach_type::BPF_TRACE_RAW_TP, bpf_prog_type::BPF_PROG_TYPE_TRACING}, obj::btf::{Btf, BtfError, BtfKind}, - programs::{load_program, FdLink, LinkRef, ProgramData, ProgramError}, + programs::{load_program, FdLink, OwnedLink, ProgramData, ProgramError}, sys::bpf_raw_tracepoint_open, }; @@ -82,7 +82,7 @@ impl BtfTracePoint { } /// Attaches the program. - pub fn attach(&mut self) -> Result { + pub fn attach(&mut self) -> Result { let prog_fd = self.data.fd_or_err()?; // BTF programs specify their attach name at program load time @@ -93,6 +93,6 @@ impl BtfTracePoint { } })? as RawFd; - Ok(self.data.link(FdLink { fd: Some(pfd) })) + Ok(FdLink { fd: Some(pfd) }.into()) } } diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index 59fa145c..bf5f14a4 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -3,7 +3,7 @@ use thiserror::Error; use crate::{generated::bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT, sys::perf_event_open_trace_point}; -use super::{load_program, perf_attach, LinkRef, ProgramData, ProgramError}; +use super::{load_program, perf_attach, OwnedLink, ProgramData, ProgramError}; /// The type returned when attaching a [`TracePoint`] fails. #[derive(Debug, Error)] @@ -67,7 +67,7 @@ impl TracePoint { /// /// For a list of the available event categories and names, see /// `/sys/kernel/debug/tracing/events`. - pub fn attach(&mut self, category: &str, name: &str) -> Result { + pub fn attach(&mut self, category: &str, name: &str) -> Result { let id = read_sys_fs_trace_point_id(category, name)?; let fd = perf_event_open_trace_point(id, None).map_err(|(_code, io_error)| { ProgramError::SyscallError { diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index ed699f9f..28dbe779 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -18,7 +18,7 @@ use crate::{ programs::{ load_program, probe::{attach, ProbeKind}, - LinkRef, ProgramData, ProgramError, + OwnedLink, ProgramData, ProgramError, }, }; @@ -78,7 +78,7 @@ impl UProbe { offset: u64, target: T, pid: Option, - ) -> Result { + ) -> Result { let target = target.as_ref(); let target_str = &*target.as_os_str().to_string_lossy(); diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index 3cb75856..bc31431e 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -8,7 +8,7 @@ use crate::{ bpf_attach_type::BPF_XDP, bpf_prog_type::BPF_PROG_TYPE_XDP, XDP_FLAGS_DRV_MODE, XDP_FLAGS_HW_MODE, XDP_FLAGS_REPLACE, XDP_FLAGS_SKB_MODE, XDP_FLAGS_UPDATE_IF_NOEXIST, }, - programs::{load_program, FdLink, Link, LinkRef, ProgramData, ProgramError}, + programs::{load_program, FdLink, Link, OwnedLink, ProgramData, ProgramError}, sys::{bpf_link_create, kernel_version, netlink_set_xdp_fd}, }; @@ -86,7 +86,7 @@ impl Xdp { /// kernels `>= 5.9.0`, and instead /// [`XdpError::NetlinkError`] is returned for older /// kernels. - pub fn attach(&mut self, interface: &str, flags: XdpFlags) -> Result { + pub fn attach(&mut self, interface: &str, flags: XdpFlags) -> Result { let prog_fd = self.data.fd_or_err()?; let c_interface = CString::new(interface).unwrap(); @@ -105,24 +105,23 @@ impl Xdp { io_error, }, )? as RawFd; - Ok(self - .data - .link(XdpLink::FdLink(FdLink { fd: Some(link_fd) }))) + Ok(FdLink { fd: Some(link_fd) }.into()) } else { unsafe { netlink_set_xdp_fd(if_index, prog_fd, None, flags.bits) } .map_err(|io_error| XdpError::NetlinkError { io_error })?; - Ok(self.data.link(XdpLink::NlLink(NlLink { + Ok(NlLink { if_index, prog_fd: Some(prog_fd), flags, - }))) + } + .into()) } } } #[derive(Debug)] -struct NlLink { +pub(crate) struct NlLink { if_index: i32, prog_fd: Option, flags: XdpFlags, @@ -150,18 +149,3 @@ impl Drop for NlLink { let _ = self.detach(); } } - -#[derive(Debug)] -enum XdpLink { - FdLink(FdLink), - NlLink(NlLink), -} - -impl Link for XdpLink { - fn detach(&mut self) -> Result<(), ProgramError> { - match self { - XdpLink::FdLink(link) => link.detach(), - XdpLink::NlLink(link) => link.detach(), - } - } -}