Skip to content

Commit 35423b4

Browse files
committed
feat: Allow conversions to Program from ProgramInfo
Allow for a ProgramInfo to be converted into one of the program types that we support. This allows for a user of Aya access to reattach, pin or unload a program that was either, previously loaded, or was loaded by another process. Signed-off-by: Dave Tucker <[email protected]>
1 parent 65489e1 commit 35423b4

29 files changed

+323
-43
lines changed

aya/src/bpf.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,15 @@ impl<'a> EbpfLoader<'a> {
609609
}
610610
ProgramSection::CgroupSkb => Program::CgroupSkb(CgroupSkb {
611611
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
612-
expected_attach_type: None,
612+
attach_type: None,
613613
}),
614614
ProgramSection::CgroupSkbIngress => Program::CgroupSkb(CgroupSkb {
615615
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
616-
expected_attach_type: Some(CgroupSkbAttachType::Ingress),
616+
attach_type: Some(CgroupSkbAttachType::Ingress),
617617
}),
618618
ProgramSection::CgroupSkbEgress => Program::CgroupSkb(CgroupSkb {
619619
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
620-
expected_attach_type: Some(CgroupSkbAttachType::Egress),
620+
attach_type: Some(CgroupSkbAttachType::Egress),
621621
}),
622622
ProgramSection::CgroupSockAddr { attach_type, .. } => {
623623
Program::CgroupSockAddr(CgroupSockAddr {

aya/src/programs/cgroup_device.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use aya_obj::generated::{
99
use crate::{
1010
programs::{
1111
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramFd,
12-
bpf_prog_get_fd_by_id, define_link_wrapper, id_as_key, load_program, query,
12+
ProgramType, bpf_prog_get_fd_by_id, define_link_wrapper, id_as_key, load_program, query,
1313
},
1414
sys::{LinkTarget, ProgQueryTarget, SyscallError, bpf_link_create},
1515
util::KernelVersion,
@@ -56,6 +56,9 @@ pub struct CgroupDevice {
5656
}
5757

5858
impl CgroupDevice {
59+
/// The type of the program according to the kernel.
60+
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupDevice;
61+
5962
/// Loads the program inside the kernel
6063
pub fn load(&mut self) -> Result<(), ProgramError> {
6164
load_program(BPF_PROG_TYPE_CGROUP_DEVICE, &mut self.data)

aya/src/programs/cgroup_skb.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use aya_obj::generated::{
1010
use crate::{
1111
VerifierLogLevel,
1212
programs::{
13-
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
13+
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
1414
define_link_wrapper, id_as_key, load_program,
1515
},
1616
sys::{LinkTarget, SyscallError, bpf_link_create},
@@ -57,18 +57,19 @@ use crate::{
5757
#[doc(alias = "BPF_PROG_TYPE_CGROUP_SKB")]
5858
pub struct CgroupSkb {
5959
pub(crate) data: ProgramData<CgroupSkbLink>,
60-
pub(crate) expected_attach_type: Option<CgroupSkbAttachType>,
60+
pub(crate) attach_type: Option<CgroupSkbAttachType>,
6161
}
6262

6363
impl CgroupSkb {
64+
/// The type of the program according to the kernel.
65+
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSkb;
66+
6467
/// Loads the program inside the kernel.
6568
pub fn load(&mut self) -> Result<(), ProgramError> {
66-
self.data.expected_attach_type =
67-
self.expected_attach_type
68-
.map(|attach_type| match attach_type {
69-
CgroupSkbAttachType::Ingress => BPF_CGROUP_INET_INGRESS,
70-
CgroupSkbAttachType::Egress => BPF_CGROUP_INET_EGRESS,
71-
});
69+
self.data.expected_attach_type = self.attach_type.map(|attach_type| match attach_type {
70+
CgroupSkbAttachType::Ingress => BPF_CGROUP_INET_INGRESS,
71+
CgroupSkbAttachType::Egress => BPF_CGROUP_INET_EGRESS,
72+
});
7273
load_program(BPF_PROG_TYPE_CGROUP_SKB, &mut self.data)
7374
}
7475

@@ -79,7 +80,7 @@ impl CgroupSkb {
7980
/// method returns `None` for programs defined with the generic section
8081
/// `cgroup/skb`.
8182
pub fn expected_attach_type(&self) -> &Option<CgroupSkbAttachType> {
82-
&self.expected_attach_type
83+
&self.attach_type
8384
}
8485

8586
/// Attaches the program to the given cgroup.
@@ -138,7 +139,7 @@ impl CgroupSkb {
138139
let data = ProgramData::from_pinned_path(path, VerifierLogLevel::default())?;
139140
Ok(Self {
140141
data,
141-
expected_attach_type: Some(expected_attach_type),
142+
attach_type: Some(expected_attach_type),
142143
})
143144
}
144145
}

aya/src/programs/cgroup_sock.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use aya_obj::programs::CgroupSockAttachType;
88
use crate::{
99
VerifierLogLevel,
1010
programs::{
11-
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
11+
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
1212
define_link_wrapper, id_as_key, load_program,
1313
},
1414
sys::{LinkTarget, SyscallError, bpf_link_create},
@@ -58,6 +58,9 @@ pub struct CgroupSock {
5858
}
5959

6060
impl CgroupSock {
61+
/// The type of the program according to the kernel.
62+
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSock;
63+
6164
/// Loads the program inside the kernel.
6265
pub fn load(&mut self) -> Result<(), ProgramError> {
6366
self.data.expected_attach_type = Some(self.attach_type.into());

aya/src/programs/cgroup_sock_addr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use aya_obj::programs::CgroupSockAddrAttachType;
88
use crate::{
99
VerifierLogLevel,
1010
programs::{
11-
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
11+
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
1212
define_link_wrapper, id_as_key, load_program,
1313
},
1414
sys::{LinkTarget, SyscallError, bpf_link_create},
@@ -59,6 +59,9 @@ pub struct CgroupSockAddr {
5959
}
6060

6161
impl CgroupSockAddr {
62+
/// The type of the program according to the kernel.
63+
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSockAddr;
64+
6265
/// Loads the program inside the kernel.
6366
pub fn load(&mut self) -> Result<(), ProgramError> {
6467
self.data.expected_attach_type = Some(self.attach_type.into());

aya/src/programs/cgroup_sockopt.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use aya_obj::programs::CgroupSockoptAttachType;
88
use crate::{
99
VerifierLogLevel,
1010
programs::{
11-
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
11+
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
1212
define_link_wrapper, id_as_key, load_program,
1313
},
1414
sys::{LinkTarget, SyscallError, bpf_link_create},
@@ -56,6 +56,9 @@ pub struct CgroupSockopt {
5656
}
5757

5858
impl CgroupSockopt {
59+
/// The type of the program according to the kernel.
60+
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSockopt;
61+
5962
/// Loads the program inside the kernel.
6063
pub fn load(&mut self) -> Result<(), ProgramError> {
6164
self.data.expected_attach_type = Some(self.attach_type.into());

aya/src/programs/cgroup_sysctl.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use aya_obj::generated::{
88

99
use crate::{
1010
programs::{
11-
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
11+
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
1212
define_link_wrapper, id_as_key, load_program,
1313
},
1414
sys::{LinkTarget, SyscallError, bpf_link_create},
@@ -55,6 +55,9 @@ pub struct CgroupSysctl {
5555
}
5656

5757
impl CgroupSysctl {
58+
/// The type of the program according to the kernel.
59+
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSysctl;
60+
5861
/// Loads the program inside the kernel.
5962
pub fn load(&mut self) -> Result<(), ProgramError> {
6063
load_program(BPF_PROG_TYPE_CGROUP_SYSCTL, &mut self.data)

aya/src/programs/extension.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use thiserror::Error;
1212
use crate::{
1313
Btf,
1414
programs::{
15-
FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd, define_link_wrapper, load_program,
15+
FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd, ProgramType, define_link_wrapper,
16+
load_program,
1617
},
1718
sys::{self, BpfLinkCreateArgs, LinkTarget, SyscallError, bpf_link_create},
1819
};
@@ -58,6 +59,9 @@ pub struct Extension {
5859
}
5960

6061
impl Extension {
62+
/// The type of the program according to the kernel.
63+
pub const PROGRAM_TYPE: ProgramType = ProgramType::Extension;
64+
6165
/// Loads the extension inside the kernel.
6266
///
6367
/// Prepares the code included in the extension to replace the code of the function

aya/src/programs/fentry.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use aya_obj::{
66
};
77

88
use crate::programs::{
9-
FdLink, FdLinkId, ProgramData, ProgramError, define_link_wrapper, load_program,
9+
FdLink, FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
1010
utils::attach_raw_tracepoint,
1111
};
1212

@@ -51,6 +51,9 @@ pub struct FEntry {
5151
}
5252

5353
impl FEntry {
54+
/// The type of the program according to the kernel.
55+
pub const PROGRAM_TYPE: ProgramType = ProgramType::Tracing;
56+
5457
/// Loads the program inside the kernel.
5558
///
5659
/// Loads the program so it's executed when the kernel function `fn_name`

aya/src/programs/fexit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use aya_obj::{
66
};
77

88
use crate::programs::{
9-
FdLink, FdLinkId, ProgramData, ProgramError, define_link_wrapper, load_program,
9+
FdLink, FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
1010
utils::attach_raw_tracepoint,
1111
};
1212

@@ -51,6 +51,9 @@ pub struct FExit {
5151
}
5252

5353
impl FExit {
54+
/// The type of the program according to the kernel.
55+
pub const PROGRAM_TYPE: ProgramType = ProgramType::Tracing;
56+
5457
/// Loads the program inside the kernel.
5558
///
5659
/// Loads the program so it's executed when the kernel function `fn_name`

aya/src/programs/iter.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use aya_obj::{
1414

1515
use crate::{
1616
programs::{
17-
FdLink, LinkError, PerfLinkIdInner, PerfLinkInner, ProgramData, ProgramError,
17+
FdLink, LinkError, PerfLinkIdInner, PerfLinkInner, ProgramData, ProgramError, ProgramType,
1818
define_link_wrapper, load_program,
1919
},
2020
sys::{LinkTarget, SyscallError, bpf_create_iter, bpf_link_create, bpf_link_get_info_by_fd},
@@ -60,6 +60,9 @@ pub struct Iter {
6060
}
6161

6262
impl Iter {
63+
/// The type of the program according to the kernel.
64+
pub const PROGRAM_TYPE: ProgramType = ProgramType::Tracing;
65+
6366
/// Loads the program inside the kernel.
6467
pub fn load(&mut self, iter_type: &str, btf: &Btf) -> Result<(), ProgramError> {
6568
self.data.expected_attach_type = Some(BPF_TRACE_ITER);

aya/src/programs/kprobe.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use thiserror::Error;
1212
use crate::{
1313
VerifierLogLevel,
1414
programs::{
15-
FdLink, LinkError, ProgramData, ProgramError, define_link_wrapper, load_program,
15+
FdLink, LinkError, ProgramData, ProgramError, ProgramType, define_link_wrapper,
16+
load_program,
1617
perf_attach::{PerfLinkIdInner, PerfLinkInner},
1718
probe::{ProbeKind, attach},
1819
},
@@ -50,6 +51,9 @@ pub struct KProbe {
5051
}
5152

5253
impl KProbe {
54+
/// The type of the program according to the kernel.
55+
pub const PROGRAM_TYPE: ProgramType = ProgramType::KProbe;
56+
5357
/// Loads the program inside the kernel.
5458
pub fn load(&mut self) -> Result<(), ProgramError> {
5559
load_program(BPF_PROG_TYPE_KPROBE, &mut self.data)

aya/src/programs/lirc_mode2.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use aya_obj::generated::{
77

88
use crate::{
99
programs::{
10-
CgroupAttachMode, Link, ProgramData, ProgramError, ProgramFd, ProgramInfo, id_as_key,
11-
load_program, query,
10+
CgroupAttachMode, Link, ProgramData, ProgramError, ProgramFd, ProgramInfo, ProgramType,
11+
id_as_key, load_program, query,
1212
},
1313
sys::{ProgQueryTarget, bpf_prog_attach, bpf_prog_detach, bpf_prog_get_fd_by_id},
1414
};
@@ -56,6 +56,9 @@ pub struct LircMode2 {
5656
}
5757

5858
impl LircMode2 {
59+
/// The type of the program according to the kernel.
60+
pub const PROGRAM_TYPE: ProgramType = ProgramType::LircMode2;
61+
5962
/// Loads the program inside the kernel.
6063
pub fn load(&mut self) -> Result<(), ProgramError> {
6164
load_program(BPF_PROG_TYPE_LIRC_MODE2, &mut self.data)

aya/src/programs/lsm.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use aya_obj::{
66
};
77

88
use crate::programs::{
9-
FdLink, FdLinkId, ProgramData, ProgramError, define_link_wrapper, load_program,
9+
FdLink, FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
1010
utils::attach_raw_tracepoint,
1111
};
1212

@@ -54,6 +54,9 @@ pub struct Lsm {
5454
}
5555

5656
impl Lsm {
57+
/// The type of the program according to the kernel.
58+
pub const PROGRAM_TYPE: ProgramType = ProgramType::Lsm;
59+
5760
/// Loads the program inside the kernel.
5861
///
5962
/// # Arguments

0 commit comments

Comments
 (0)