Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow conversions to Program from ProgramInfo #1209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,15 @@ impl<'a> EbpfLoader<'a> {
}
ProgramSection::CgroupSkb => Program::CgroupSkb(CgroupSkb {
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
expected_attach_type: None,
attach_type: None,
}),
ProgramSection::CgroupSkbIngress => Program::CgroupSkb(CgroupSkb {
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
expected_attach_type: Some(CgroupSkbAttachType::Ingress),
attach_type: Some(CgroupSkbAttachType::Ingress),
}),
ProgramSection::CgroupSkbEgress => Program::CgroupSkb(CgroupSkb {
data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
expected_attach_type: Some(CgroupSkbAttachType::Egress),
attach_type: Some(CgroupSkbAttachType::Egress),
}),
ProgramSection::CgroupSockAddr { attach_type, .. } => {
Program::CgroupSockAddr(CgroupSockAddr {
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/cgroup_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aya_obj::generated::{
use crate::{
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramFd,
bpf_prog_get_fd_by_id, define_link_wrapper, id_as_key, load_program, query,
ProgramType, bpf_prog_get_fd_by_id, define_link_wrapper, id_as_key, load_program, query,
},
sys::{LinkTarget, ProgQueryTarget, SyscallError, bpf_link_create},
util::KernelVersion,
Expand Down Expand Up @@ -56,6 +56,9 @@ pub struct CgroupDevice {
}

impl CgroupDevice {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupDevice;

/// Loads the program inside the kernel
pub fn load(&mut self) -> Result<(), ProgramError> {
load_program(BPF_PROG_TYPE_CGROUP_DEVICE, &mut self.data)
Expand Down
21 changes: 11 additions & 10 deletions aya/src/programs/cgroup_skb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use aya_obj::generated::{
use crate::{
VerifierLogLevel,
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
define_link_wrapper, id_as_key, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
Expand Down Expand Up @@ -57,18 +57,19 @@ use crate::{
#[doc(alias = "BPF_PROG_TYPE_CGROUP_SKB")]
pub struct CgroupSkb {
pub(crate) data: ProgramData<CgroupSkbLink>,
pub(crate) expected_attach_type: Option<CgroupSkbAttachType>,
pub(crate) attach_type: Option<CgroupSkbAttachType>,
}

impl CgroupSkb {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSkb;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
self.data.expected_attach_type =
self.expected_attach_type
.map(|attach_type| match attach_type {
CgroupSkbAttachType::Ingress => BPF_CGROUP_INET_INGRESS,
CgroupSkbAttachType::Egress => BPF_CGROUP_INET_EGRESS,
});
self.data.expected_attach_type = self.attach_type.map(|attach_type| match attach_type {
CgroupSkbAttachType::Ingress => BPF_CGROUP_INET_INGRESS,
CgroupSkbAttachType::Egress => BPF_CGROUP_INET_EGRESS,
});
load_program(BPF_PROG_TYPE_CGROUP_SKB, &mut self.data)
}

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

/// Attaches the program to the given cgroup.
Expand Down Expand Up @@ -138,7 +139,7 @@ impl CgroupSkb {
let data = ProgramData::from_pinned_path(path, VerifierLogLevel::default())?;
Ok(Self {
data,
expected_attach_type: Some(expected_attach_type),
attach_type: Some(expected_attach_type),
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/cgroup_sock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use aya_obj::programs::CgroupSockAttachType;
use crate::{
VerifierLogLevel,
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
define_link_wrapper, id_as_key, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
Expand Down Expand Up @@ -58,6 +58,9 @@ pub struct CgroupSock {
}

impl CgroupSock {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSock;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
self.data.expected_attach_type = Some(self.attach_type.into());
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/cgroup_sock_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use aya_obj::programs::CgroupSockAddrAttachType;
use crate::{
VerifierLogLevel,
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
define_link_wrapper, id_as_key, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
Expand Down Expand Up @@ -59,6 +59,9 @@ pub struct CgroupSockAddr {
}

impl CgroupSockAddr {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSockAddr;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
self.data.expected_attach_type = Some(self.attach_type.into());
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/cgroup_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use aya_obj::programs::CgroupSockoptAttachType;
use crate::{
VerifierLogLevel,
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
define_link_wrapper, id_as_key, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
Expand Down Expand Up @@ -56,6 +56,9 @@ pub struct CgroupSockopt {
}

impl CgroupSockopt {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSockopt;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
self.data.expected_attach_type = Some(self.attach_type.into());
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/cgroup_sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use aya_obj::generated::{

use crate::{
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError,
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
define_link_wrapper, id_as_key, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
Expand Down Expand Up @@ -55,6 +55,9 @@ pub struct CgroupSysctl {
}

impl CgroupSysctl {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSysctl;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
load_program(BPF_PROG_TYPE_CGROUP_SYSCTL, &mut self.data)
Expand Down
6 changes: 5 additions & 1 deletion aya/src/programs/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use thiserror::Error;
use crate::{
Btf,
programs::{
FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd, define_link_wrapper, load_program,
FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd, ProgramType, define_link_wrapper,
load_program,
},
sys::{self, BpfLinkCreateArgs, LinkTarget, SyscallError, bpf_link_create},
};
Expand Down Expand Up @@ -58,6 +59,9 @@ pub struct Extension {
}

impl Extension {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::Extension;

/// Loads the extension inside the kernel.
///
/// Prepares the code included in the extension to replace the code of the function
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/fentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aya_obj::{
};

use crate::programs::{
FdLink, FdLinkId, ProgramData, ProgramError, define_link_wrapper, load_program,
FdLink, FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
utils::attach_raw_tracepoint,
};

Expand Down Expand Up @@ -51,6 +51,9 @@ pub struct FEntry {
}

impl FEntry {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::Tracing;

/// Loads the program inside the kernel.
///
/// Loads the program so it's executed when the kernel function `fn_name`
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/fexit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aya_obj::{
};

use crate::programs::{
FdLink, FdLinkId, ProgramData, ProgramError, define_link_wrapper, load_program,
FdLink, FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
utils::attach_raw_tracepoint,
};

Expand Down Expand Up @@ -51,6 +51,9 @@ pub struct FExit {
}

impl FExit {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::Tracing;

/// Loads the program inside the kernel.
///
/// Loads the program so it's executed when the kernel function `fn_name`
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use aya_obj::{

use crate::{
programs::{
FdLink, LinkError, PerfLinkIdInner, PerfLinkInner, ProgramData, ProgramError,
FdLink, LinkError, PerfLinkIdInner, PerfLinkInner, ProgramData, ProgramError, ProgramType,
define_link_wrapper, load_program,
},
sys::{LinkTarget, SyscallError, bpf_create_iter, bpf_link_create, bpf_link_get_info_by_fd},
Expand Down Expand Up @@ -60,6 +60,9 @@ pub struct Iter {
}

impl Iter {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::Tracing;

/// Loads the program inside the kernel.
pub fn load(&mut self, iter_type: &str, btf: &Btf) -> Result<(), ProgramError> {
self.data.expected_attach_type = Some(BPF_TRACE_ITER);
Expand Down
6 changes: 5 additions & 1 deletion aya/src/programs/kprobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use thiserror::Error;
use crate::{
VerifierLogLevel,
programs::{
FdLink, LinkError, ProgramData, ProgramError, define_link_wrapper, load_program,
FdLink, LinkError, ProgramData, ProgramError, ProgramType, define_link_wrapper,
load_program,
perf_attach::{PerfLinkIdInner, PerfLinkInner},
probe::{ProbeKind, attach},
},
Expand Down Expand Up @@ -50,6 +51,9 @@ pub struct KProbe {
}

impl KProbe {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::KProbe;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
load_program(BPF_PROG_TYPE_KPROBE, &mut self.data)
Expand Down
7 changes: 5 additions & 2 deletions aya/src/programs/lirc_mode2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use aya_obj::generated::{

use crate::{
programs::{
CgroupAttachMode, Link, ProgramData, ProgramError, ProgramFd, ProgramInfo, id_as_key,
load_program, query,
CgroupAttachMode, Link, ProgramData, ProgramError, ProgramFd, ProgramInfo, ProgramType,
id_as_key, load_program, query,
},
sys::{ProgQueryTarget, bpf_prog_attach, bpf_prog_detach, bpf_prog_get_fd_by_id},
};
Expand Down Expand Up @@ -56,6 +56,9 @@ pub struct LircMode2 {
}

impl LircMode2 {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::LircMode2;

/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
load_program(BPF_PROG_TYPE_LIRC_MODE2, &mut self.data)
Expand Down
5 changes: 4 additions & 1 deletion aya/src/programs/lsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aya_obj::{
};

use crate::programs::{
FdLink, FdLinkId, ProgramData, ProgramError, define_link_wrapper, load_program,
FdLink, FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
utils::attach_raw_tracepoint,
};

Expand Down Expand Up @@ -54,6 +54,9 @@ pub struct Lsm {
}

impl Lsm {
/// The type of the program according to the kernel.
pub const PROGRAM_TYPE: ProgramType = ProgramType::Lsm;

/// Loads the program inside the kernel.
///
/// # Arguments
Expand Down
Loading
Loading