Use a macro for program unload api

pull/264/head
Davide Bertola 3 years ago
parent 08d5697948
commit 4abaad6a46

@ -10,8 +10,8 @@ use crate::{
bpf_prog_type::BPF_PROG_TYPE_CGROUP_SKB,
},
programs::{
define_link_wrapper, load_program, unload_program, FdLink, Link, OwnedLink, ProgAttachLink,
ProgramData, ProgramError,
define_link_wrapper, load_program, FdLink, Link, OwnedLink, ProgAttachLink, ProgramData,
ProgramError,
},
sys::{bpf_link_create, bpf_prog_attach, kernel_version},
};
@ -72,14 +72,6 @@ impl CgroupSkb {
load_program(BPF_PROG_TYPE_CGROUP_SKB, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Returns the expected attach type of the program.
///
/// [`CgroupSkb`] programs can specify the expected attach type in their ELF

@ -7,8 +7,8 @@ use std::{
use crate::{
generated::{bpf_attach_type::BPF_CGROUP_SYSCTL, bpf_prog_type::BPF_PROG_TYPE_CGROUP_SYSCTL},
programs::{
define_link_wrapper, load_program, unload_program, FdLink, Link, OwnedLink, ProgAttachLink,
ProgramData, ProgramError,
define_link_wrapper, load_program, FdLink, Link, OwnedLink, ProgAttachLink, ProgramData,
ProgramError,
},
sys::{bpf_link_create, bpf_prog_attach, kernel_version},
};
@ -59,14 +59,6 @@ impl CgroupSysctl {
load_program(BPF_PROG_TYPE_CGROUP_SYSCTL, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given cgroup.
///
/// The returned value can be used to detach, see [CgroupSysctl::detach].

@ -4,8 +4,8 @@ use crate::{
generated::{bpf_attach_type::BPF_TRACE_FENTRY, bpf_prog_type::BPF_PROG_TYPE_TRACING},
obj::btf::{Btf, BtfKind},
programs::{
define_link_wrapper, load_program, unload_program, utils::attach_raw_tracepoint, FdLink,
FdLinkId, OwnedLink, ProgramData, ProgramError,
define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId,
OwnedLink, ProgramData, ProgramError,
},
};
@ -62,14 +62,6 @@ impl FEntry {
load_program(BPF_PROG_TYPE_TRACING, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program.
///
/// The returned value can be used to detach, see [FEntry::detach].

@ -4,8 +4,8 @@ use crate::{
generated::{bpf_attach_type::BPF_TRACE_FEXIT, bpf_prog_type::BPF_PROG_TYPE_TRACING},
obj::btf::{Btf, BtfKind},
programs::{
define_link_wrapper, load_program, unload_program, utils::attach_raw_tracepoint, FdLink,
FdLinkId, OwnedLink, ProgramData, ProgramError,
define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId,
OwnedLink, ProgramData, ProgramError,
},
};
@ -62,14 +62,6 @@ impl FExit {
load_program(BPF_PROG_TYPE_TRACING, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program.
///
/// The returned value can be used to detach, see [FExit::detach].

@ -12,8 +12,6 @@ use crate::{
},
};
use super::unload_program;
/// A kernel probe.
///
/// Kernel probes are eBPF programs that can be attached to almost any function inside
@ -51,14 +49,6 @@ impl KProbe {
load_program(BPF_PROG_TYPE_KPROBE, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Returns `KProbe` if the program is a `kprobe`, or `KRetProbe` if the
/// program is a `kretprobe`.
pub fn kind(&self) -> ProbeKind {

@ -3,10 +3,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, unload_program, Link, OwnedLink, 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},
};
@ -61,14 +58,6 @@ impl LircMode2 {
load_program(BPF_PROG_TYPE_LIRC_MODE2, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given lirc device.
///
/// The returned value can be used to detach, see [LircMode2::detach].

@ -3,8 +3,8 @@ use crate::{
generated::{bpf_attach_type::BPF_LSM_MAC, bpf_prog_type::BPF_PROG_TYPE_LSM},
obj::btf::{Btf, BtfKind},
programs::{
define_link_wrapper, load_program, unload_program, utils::attach_raw_tracepoint, FdLink,
FdLinkId, OwnedLink, ProgramData, ProgramError,
define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId,
OwnedLink, ProgramData, ProgramError,
},
};
@ -67,14 +67,6 @@ impl Lsm {
load_program(BPF_PROG_TYPE_LSM, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program.
///
/// The returned value can be used to detach, see [Lsm::detach].

@ -552,6 +552,47 @@ impl<'a, P: ProgramFd> ProgramFd for &'a P {
}
}
macro_rules! impl_program_unload {
($($struct_name:ident),+ $(,)?) => {
$(
impl $struct_name {
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
}
)+
}
}
impl_program_unload!(
KProbe,
UProbe,
TracePoint,
SocketFilter,
Xdp,
SkMsg,
SkSkb,
SchedClassifier,
CgroupSkb,
CgroupSysctl,
CgroupSockopt,
LircMode2,
PerfEvent,
Lsm,
RawTracePoint,
BtfTracePoint,
FEntry,
FExit,
Extension,
CgroupSockAddr,
SkLookup,
SockOps
);
macro_rules! impl_program_fd {
($($struct_name:ident),+ $(,)?) => {
$(

@ -14,7 +14,7 @@ use crate::{
programs::{
load_program, perf_attach,
perf_attach::{PerfLink, PerfLinkId},
unload_program, OwnedLink, ProgramData, ProgramError,
OwnedLink, ProgramData, ProgramError,
},
sys::perf_event_open,
};
@ -128,14 +128,6 @@ impl PerfEvent {
load_program(BPF_PROG_TYPE_PERF_EVENT, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches to the given perf event.
///
/// The possible values and encoding of the `config` argument depends on the

@ -4,8 +4,8 @@ use std::ffi::CString;
use crate::{
generated::bpf_prog_type::BPF_PROG_TYPE_RAW_TRACEPOINT,
programs::{
define_link_wrapper, load_program, unload_program, utils::attach_raw_tracepoint, FdLink,
FdLinkId, OwnedLink, ProgramData, ProgramError,
define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId,
OwnedLink, ProgramData, ProgramError,
},
};
@ -45,14 +45,6 @@ impl RawTracePoint {
load_program(BPF_PROG_TYPE_RAW_TRACEPOINT, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given tracepoint.
///
/// The returned value can be used to detach, see [RawTracePoint::detach].

@ -3,8 +3,8 @@ use crate::{
generated::{bpf_attach_type::BPF_SK_MSG_VERDICT, bpf_prog_type::BPF_PROG_TYPE_SK_MSG},
maps::sock::SocketMap,
programs::{
define_link_wrapper, load_program, unload_program, OwnedLink, ProgAttachLink,
ProgAttachLinkId, ProgramData, ProgramError,
define_link_wrapper, load_program, OwnedLink, ProgAttachLink, ProgAttachLinkId,
ProgramData, ProgramError,
},
sys::bpf_prog_attach,
};
@ -69,14 +69,6 @@ impl SkMsg {
load_program(BPF_PROG_TYPE_SK_MSG, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given sockmap.
///
/// The returned value can be used to detach, see [SkMsg::detach].

@ -6,8 +6,8 @@ use crate::{
},
maps::sock::SocketMap,
programs::{
define_link_wrapper, load_program, unload_program, OwnedLink, ProgAttachLink,
ProgAttachLinkId, ProgramData, ProgramError,
define_link_wrapper, load_program, OwnedLink, ProgAttachLink, ProgAttachLinkId,
ProgramData, ProgramError,
},
sys::bpf_prog_attach,
};
@ -62,14 +62,6 @@ impl SkSkb {
load_program(BPF_PROG_TYPE_SK_SKB, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given socket map.
///
/// The returned value can be used to detach, see [SkSkb::detach].

@ -4,8 +4,8 @@ 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::{
define_link_wrapper, load_program, unload_program, OwnedLink, ProgAttachLink,
ProgAttachLinkId, ProgramData, ProgramError,
define_link_wrapper, load_program, OwnedLink, ProgAttachLink, ProgAttachLinkId,
ProgramData, ProgramError,
},
sys::bpf_prog_attach,
};
@ -56,14 +56,6 @@ impl SockOps {
load_program(BPF_PROG_TYPE_SOCK_OPS, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given cgroup.
///
/// The returned value can be used to detach, see [SockOps::detach].

@ -8,7 +8,7 @@ use thiserror::Error;
use crate::{
generated::{bpf_prog_type::BPF_PROG_TYPE_SOCKET_FILTER, SO_ATTACH_BPF, SO_DETACH_BPF},
programs::{load_program, unload_program, Link, OwnedLink, ProgramData, ProgramError},
programs::{load_program, Link, OwnedLink, ProgramData, ProgramError},
};
/// The type returned when attaching a [`SocketFilter`] fails.
@ -70,14 +70,6 @@ impl SocketFilter {
load_program(BPF_PROG_TYPE_SOCKET_FILTER, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the filter on the given socket.
///
/// The returned value can be used to detach from the socket, see [SocketFilter::detach].

@ -10,10 +10,7 @@ use crate::{
generated::{
bpf_prog_type::BPF_PROG_TYPE_SCHED_CLS, TC_H_CLSACT, TC_H_MIN_EGRESS, TC_H_MIN_INGRESS,
},
programs::{
define_link_wrapper, load_program, unload_program, Link, OwnedLink, ProgramData,
ProgramError,
},
programs::{define_link_wrapper, load_program, Link, OwnedLink, ProgramData, ProgramError},
sys::{
netlink_find_filter_with_name, netlink_qdisc_add_clsact, netlink_qdisc_attach,
netlink_qdisc_detach,
@ -109,14 +106,6 @@ impl SchedClassifier {
load_program(BPF_PROG_TYPE_SCHED_CLS, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given `interface`.
///
/// The returned value can be used to detach, see [SchedClassifier::detach].

@ -3,8 +3,8 @@ use crate::{
generated::{bpf_attach_type::BPF_TRACE_RAW_TP, bpf_prog_type::BPF_PROG_TYPE_TRACING},
obj::btf::{Btf, BtfKind},
programs::{
define_link_wrapper, load_program, unload_program, utils::attach_raw_tracepoint, FdLink,
FdLinkId, OwnedLink, ProgramData, ProgramError,
define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId,
OwnedLink, ProgramData, ProgramError,
},
};
@ -65,14 +65,6 @@ impl BtfTracePoint {
load_program(BPF_PROG_TYPE_TRACING, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program.
///
/// The returned value can be used to detach, see [BtfTracePoint::detach].

@ -12,8 +12,6 @@ use crate::{
sys::perf_event_open_trace_point,
};
use super::unload_program;
/// The type returned when attaching a [`TracePoint`] fails.
#[derive(Debug, Error)]
pub enum TracePointError {
@ -73,14 +71,6 @@ impl TracePoint {
load_program(BPF_PROG_TYPE_TRACEPOINT, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches to a given trace point.
///
/// For a list of the available event categories and names, see

@ -19,7 +19,7 @@ use crate::{
define_link_wrapper, load_program,
perf_attach::{PerfLink, PerfLinkId},
probe::{attach, ProbeKind},
unload_program, OwnedLink, ProgramData, ProgramError,
OwnedLink, ProgramData, ProgramError,
},
};
@ -51,14 +51,6 @@ impl UProbe {
load_program(BPF_PROG_TYPE_KPROBE, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Returns `UProbe` if the program is a `uprobe`, or `URetProbe` if the
/// program is a `uretprobe`.
pub fn kind(&self) -> ProbeKind {

@ -12,8 +12,7 @@ use crate::{
XDP_FLAGS_UPDATE_IF_NOEXIST,
},
programs::{
define_link_wrapper, load_program, unload_program, FdLink, Link, OwnedLink, ProgramData,
ProgramError,
define_link_wrapper, load_program, FdLink, Link, OwnedLink, ProgramData, ProgramError,
},
sys::{bpf_link_create, kernel_version, netlink_set_xdp_fd},
};
@ -82,14 +81,6 @@ impl Xdp {
load_program(BPF_PROG_TYPE_XDP, &mut self.data)
}
/// Unloads the program from the kernel.
///
/// If `detach` is true, links will be detached before unloading the program.
/// Note that OwnedLinks you obtained using [KProbe::forget_link] will not be detached.
pub fn unload(&mut self, detach: bool) -> Result<(), ProgramError> {
unload_program(&mut self.data, detach)
}
/// Attaches the program to the given `interface`.
///
/// The returned value can be used to detach, see [Xdp::detach].

Loading…
Cancel
Save