Skip to content

Commit

Permalink
Define {detach,take_link} in macro when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Nov 24, 2024
1 parent 85c73af commit 119049f
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 468 deletions.
22 changes: 2 additions & 20 deletions aya/src/programs/cgroup_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,6 @@ impl CgroupDevice {
}
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`CgroupDeviceLink`] is dropped, the link is
/// detached.
pub fn take_link(
&mut self,
link_id: CgroupDeviceLinkId,
) -> Result<CgroupDeviceLink, ProgramError> {
self.data.take_link(link_id)
}

/// Detaches the program
///
/// See [CgroupDevice::attach].
pub fn detach(&mut self, link_id: CgroupDeviceLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Queries the cgroup for attached programs.
pub fn query<T: AsFd>(target_fd: T) -> Result<Vec<CgroupDeviceLink>, ProgramError> {
let target_fd = target_fd.as_fd();
Expand Down Expand Up @@ -179,5 +160,6 @@ define_link_wrapper!(
/// The type returned by [CgroupDevice::attach]. Can be passed to [CgroupDevice::detach].
CgroupDeviceLinkId,
CgroupDeviceLinkInner,
CgroupDeviceLinkIdInner
CgroupDeviceLinkIdInner,
CgroupDevice,
);
19 changes: 2 additions & 17 deletions aya/src/programs/cgroup_skb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,6 @@ impl CgroupSkb {
}
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`CgroupSkbLink`] is dropped, the link is
/// detached.
pub fn take_link(&mut self, link_id: CgroupSkbLinkId) -> Result<CgroupSkbLink, ProgramError> {
self.data.take_link(link_id)
}

/// Detaches the program.
///
/// See [CgroupSkb::attach].
pub fn detach(&mut self, link_id: CgroupSkbLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Creates a program from a pinned entry on a bpffs.
///
/// Existing links will not be populated. To work with existing links you should use [`crate::programs::links::PinnedLink`].
Expand Down Expand Up @@ -195,7 +179,8 @@ define_link_wrapper!(
/// The type returned by [CgroupSkb::attach]. Can be passed to [CgroupSkb::detach].
CgroupSkbLinkId,
CgroupSkbLinkInner,
CgroupSkbLinkIdInner
CgroupSkbLinkIdInner,
CgroupSkb,
);

/// Defines where to attach a [`CgroupSkb`] program.
Expand Down
19 changes: 2 additions & 17 deletions aya/src/programs/cgroup_sock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ impl CgroupSock {
}
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`CgroupSockLink`] is dropped, the link is
/// detached.
pub fn take_link(&mut self, link_id: CgroupSockLinkId) -> Result<CgroupSockLink, ProgramError> {
self.data.take_link(link_id)
}

/// Detaches the program.
///
/// See [CgroupSock::attach].
pub fn detach(&mut self, link_id: CgroupSockLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Creates a program from a pinned entry on a bpffs.
///
/// Existing links will not be populated. To work with existing links you should use [`crate::programs::links::PinnedLink`].
Expand Down Expand Up @@ -170,5 +154,6 @@ define_link_wrapper!(
/// The type returned by [CgroupSock::attach]. Can be passed to [CgroupSock::detach].
CgroupSockLinkId,
CgroupSockLinkInner,
CgroupSockLinkIdInner
CgroupSockLinkIdInner,
CgroupSock,
);
22 changes: 2 additions & 20 deletions aya/src/programs/cgroup_sock_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,6 @@ impl CgroupSockAddr {
}
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`CgroupSockAddrLink`] is dropped, the link is
/// detached.
pub fn take_link(
&mut self,
link_id: CgroupSockAddrLinkId,
) -> Result<CgroupSockAddrLink, ProgramError> {
self.data.take_link(link_id)
}

/// Detaches the program.
///
/// See [CgroupSockAddr::attach].
pub fn detach(&mut self, link_id: CgroupSockAddrLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Creates a program from a pinned entry on a bpffs.
///
/// Existing links will not be populated. To work with existing links you should use [`crate::programs::links::PinnedLink`].
Expand Down Expand Up @@ -174,5 +155,6 @@ define_link_wrapper!(
/// The type returned by [CgroupSockAddr::attach]. Can be passed to [CgroupSockAddr::detach].
CgroupSockAddrLinkId,
CgroupSockAddrLinkInner,
CgroupSockAddrLinkIdInner
CgroupSockAddrLinkIdInner,
CgroupSockAddr,
);
22 changes: 2 additions & 20 deletions aya/src/programs/cgroup_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,6 @@ impl CgroupSockopt {
}
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`CgroupSockoptLink`] is dropped, the link is
/// detached.
pub fn take_link(
&mut self,
link_id: CgroupSockoptLinkId,
) -> Result<CgroupSockoptLink, ProgramError> {
self.data.take_link(link_id)
}

/// Detaches the program.
///
/// See [CgroupSockopt::attach].
pub fn detach(&mut self, link_id: CgroupSockoptLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Creates a program from a pinned entry on a bpffs.
///
/// Existing links will not be populated. To work with existing links you should use [`crate::programs::links::PinnedLink`].
Expand Down Expand Up @@ -173,5 +154,6 @@ define_link_wrapper!(
/// The type returned by [CgroupSockopt::attach]. Can be passed to [CgroupSockopt::detach].
CgroupSockoptLinkId,
CgroupSockoptLinkInner,
CgroupSockoptLinkIdInner
CgroupSockoptLinkIdInner,
CgroupSockopt,
);
22 changes: 2 additions & 20 deletions aya/src/programs/cgroup_sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,6 @@ impl CgroupSysctl {
)))
}
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`CgroupSysctlLink`] is dropped, the link is
/// detached.
pub fn take_link(
&mut self,
link_id: CgroupSysctlLinkId,
) -> Result<CgroupSysctlLink, ProgramError> {
self.data.take_link(link_id)
}

/// Detaches the program.
///
/// See [CgroupSysctl::attach].
pub fn detach(&mut self, link_id: CgroupSysctlLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}
}

#[derive(Debug, Hash, Eq, PartialEq)]
Expand Down Expand Up @@ -154,5 +135,6 @@ define_link_wrapper!(
/// The type returned by [CgroupSysctl::attach]. Can be passed to [CgroupSysctl::detach].
CgroupSysctlLinkId,
CgroupSysctlLinkInner,
CgroupSysctlLinkIdInner
CgroupSysctlLinkIdInner,
CgroupSysctl,
);
20 changes: 2 additions & 18 deletions aya/src/programs/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,6 @@ impl Extension {
.links
.insert(ExtensionLink::new(FdLink::new(link_fd)))
}

/// Detaches the extension.
///
/// Detaching restores the original code overridden by the extension program.
/// See [Extension::attach].
pub fn detach(&mut self, link_id: ExtensionLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`ExtensionLink`] is dropped, the link is
/// detached.
pub fn take_link(&mut self, link_id: ExtensionLinkId) -> Result<ExtensionLink, ProgramError> {
self.data.take_link(link_id)
}
}

/// Retrieves the FD of the BTF object for the provided `prog_fd` and the BTF ID of the function
Expand Down Expand Up @@ -216,5 +199,6 @@ define_link_wrapper!(
/// The type returned by [Extension::attach]. Can be passed to [Extension::detach].
ExtensionLinkId,
FdLink,
FdLinkId
FdLinkId,
Extension,
);
19 changes: 2 additions & 17 deletions aya/src/programs/fentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ impl FEntry {
pub fn attach(&mut self) -> Result<FEntryLinkId, ProgramError> {
attach_raw_tracepoint(&mut self.data, None)
}

/// Detaches the program.
///
/// See [FEntry::attach].
pub fn detach(&mut self, link_id: FEntryLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`FEntryLink`] is dropped, the link is
/// detached.
pub fn take_link(&mut self, link_id: FEntryLinkId) -> Result<FEntryLink, ProgramError> {
self.data.take_link(link_id)
}
}

define_link_wrapper!(
Expand All @@ -91,5 +75,6 @@ define_link_wrapper!(
/// The type returned by [FEntry::attach]. Can be passed to [FEntry::detach].
FEntryLinkId,
FdLink,
FdLinkId
FdLinkId,
FEntry,
);
18 changes: 2 additions & 16 deletions aya/src/programs/fexit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@ impl FExit {
pub fn attach(&mut self) -> Result<FExitLinkId, ProgramError> {
attach_raw_tracepoint(&mut self.data, None)
}

/// Detaches the program.
///
/// See [FExit::attach].
pub fn detach(&mut self, link_id: FExitLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned `FExitLink` is dropped, the link is detached.
pub fn take_link(&mut self, link_id: FExitLinkId) -> Result<FExitLink, ProgramError> {
self.data.take_link(link_id)
}
}

define_link_wrapper!(
Expand All @@ -90,5 +75,6 @@ define_link_wrapper!(
/// The type returned by [FExit::attach]. Can be passed to [FExit::detach].
FExitLinkId,
FdLink,
FdLinkId
FdLinkId,
FExit,
);
18 changes: 2 additions & 16 deletions aya/src/programs/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,6 @@ impl Iter {
.links
.insert(IterLink::new(PerfLinkInner::FdLink(FdLink::new(link_fd))))
}

/// Detaches the program.
///
/// See [`Self::attach`].
pub fn detach(&mut self, link_id: IterLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`IterLink`] is dropped, the link is detached.
pub fn take_link(&mut self, link_id: IterLinkId) -> Result<IterLink, ProgramError> {
self.data.take_link(link_id)
}
}

/// An iterator descriptor.
Expand Down Expand Up @@ -142,7 +127,8 @@ define_link_wrapper!(
/// The type returned by [`Iter::attach`]. Can be passed to [`Iter::detach`].
IterLinkId,
PerfLinkInner,
PerfLinkIdInner
PerfLinkIdInner,
Iter,
);

impl IterLink {
Expand Down
19 changes: 2 additions & 17 deletions aya/src/programs/kprobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ impl KProbe {
attach(&mut self.data, self.kind, fn_name.as_ref(), offset, None)
}

/// Detaches the program.
///
/// See [KProbe::attach].
pub fn detach(&mut self, link_id: KProbeLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the
/// link. When the returned [`KProbeLink`] is dropped, the link is
/// detached.
pub fn take_link(&mut self, link_id: KProbeLinkId) -> Result<KProbeLink, ProgramError> {
self.data.take_link(link_id)
}

/// Creates a program from a pinned entry on a bpffs.
///
/// Existing links will not be populated. To work with existing links you should use [`crate::programs::links::PinnedLink`].
Expand All @@ -115,7 +99,8 @@ define_link_wrapper!(
/// The type returned by [KProbe::attach]. Can be passed to [KProbe::detach].
KProbeLinkId,
PerfLinkInner,
PerfLinkIdInner
PerfLinkIdInner,
KProbe,
);

/// The type returned when attaching a [`KProbe`] fails.
Expand Down
19 changes: 18 additions & 1 deletion aya/src/programs/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl Link for ProgAttachLink {
}

macro_rules! define_link_wrapper {
(#[$doc1:meta] $wrapper:ident, #[$doc2:meta] $wrapper_id:ident, $base:ident, $base_id:ident) => {
(#[$doc1:meta] $wrapper:ident, #[$doc2:meta] $wrapper_id:ident, $base:ident, $base_id:ident, $program:ident,) => {
#[$doc2]
#[derive(Debug, Hash, Eq, PartialEq)]
pub struct $wrapper_id($base_id);
Expand Down Expand Up @@ -381,6 +381,23 @@ macro_rules! define_link_wrapper {
w.0.take().unwrap()
}
}

impl $program {
/// Detaches the program.
///
/// See [`Self::attach`].
pub fn detach(&mut self, link_id: $wrapper_id) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided `link_id`.
///
/// The caller takes the responsibility of managing the lifetime of the link. When the
/// returned [`$wrapper`] is dropped, the link is detached.
pub fn take_link(&mut self, link_id: $wrapper_id) -> Result<$wrapper, ProgramError> {
self.data.links.forget(link_id)
}
}
};
}

Expand Down
Loading

0 comments on commit 119049f

Please sign in to comment.