use std::{hash::Hash, os::fd::AsFd, path::Path};
use aya_obj::generated::{
bpf_attach_type::{BPF_CGROUP_INET_EGRESS, BPF_CGROUP_INET_INGRESS},
bpf_prog_type::BPF_PROG_TYPE_CGROUP_SKB,
};
use crate::{
VerifierLogLevel,
programs::{
CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramType,
define_link_wrapper, id_as_key, impl_try_into_fdlink, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
util::KernelVersion,
};
#[derive(Debug)]
#[doc(alias = "BPF_PROG_TYPE_CGROUP_SKB")]
pub struct CgroupSkb {
pub(crate) data: ProgramData<CgroupSkbLink>,
pub(crate) attach_type: Option<CgroupSkbAttachType>,
}
impl CgroupSkb {
pub const PROGRAM_TYPE: ProgramType = ProgramType::CgroupSkb;
pub fn load(&mut self) -> Result<(), ProgramError> {
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)
}
pub const fn expected_attach_type(&self) -> &Option<CgroupSkbAttachType> {
&self.attach_type
}
pub fn attach<T: AsFd>(
&mut self,
cgroup: T,
attach_type: CgroupSkbAttachType,
mode: CgroupAttachMode,
) -> Result<CgroupSkbLinkId, ProgramError> {
let prog_fd = self.fd()?;
let prog_fd = prog_fd.as_fd();
let cgroup_fd = cgroup.as_fd();
let attach_type = match attach_type {
CgroupSkbAttachType::Ingress => BPF_CGROUP_INET_INGRESS,
CgroupSkbAttachType::Egress => BPF_CGROUP_INET_EGRESS,
};
if KernelVersion::at_least(5, 7, 0) {
let link_fd = bpf_link_create(
prog_fd,
LinkTarget::Fd(cgroup_fd),
attach_type,
mode.into(),
None,
)
.map_err(|io_error| SyscallError {
call: "bpf_link_create",
io_error,
})?;
self.data
.links
.insert(CgroupSkbLink::new(CgroupSkbLinkInner::Fd(FdLink::new(
link_fd,
))))
} else {
let link = ProgAttachLink::attach(prog_fd, cgroup_fd, attach_type, mode)?;
self.data
.links
.insert(CgroupSkbLink::new(CgroupSkbLinkInner::ProgAttach(link)))
}
}
pub fn from_pin<P: AsRef<Path>>(
path: P,
expected_attach_type: CgroupSkbAttachType,
) -> Result<Self, ProgramError> {
let data = ProgramData::from_pinned_path(path, VerifierLogLevel::default())?;
Ok(Self {
data,
attach_type: Some(expected_attach_type),
})
}
}
#[derive(Debug, Hash, Eq, PartialEq)]
enum CgroupSkbLinkIdInner {
Fd(<FdLink as Link>::Id),
ProgAttach(<ProgAttachLink as Link>::Id),
}
#[derive(Debug)]
enum CgroupSkbLinkInner {
Fd(FdLink),
ProgAttach(ProgAttachLink),
}
impl Link for CgroupSkbLinkInner {
type Id = CgroupSkbLinkIdInner;
fn id(&self) -> Self::Id {
match self {
Self::Fd(fd) => CgroupSkbLinkIdInner::Fd(fd.id()),
Self::ProgAttach(p) => CgroupSkbLinkIdInner::ProgAttach(p.id()),
}
}
fn detach(self) -> Result<(), ProgramError> {
match self {
Self::Fd(fd) => fd.detach(),
Self::ProgAttach(p) => p.detach(),
}
}
}
id_as_key!(CgroupSkbLinkInner, CgroupSkbLinkIdInner);
define_link_wrapper!(
CgroupSkbLink,
CgroupSkbLinkId,
CgroupSkbLinkInner,
CgroupSkbLinkIdInner,
CgroupSkb,
);
impl_try_into_fdlink!(CgroupSkbLink, CgroupSkbLinkInner);
#[derive(Copy, Clone, Debug)]
pub enum CgroupSkbAttachType {
Ingress,
Egress,
}