use std::os::fd::AsFd;
use aya_obj::generated::{bpf_attach_type::BPF_SK_LOOKUP, bpf_prog_type::BPF_PROG_TYPE_SK_LOOKUP};
use super::links::FdLink;
use crate::{
programs::{
FdLinkId, ProgramData, ProgramError, ProgramType, define_link_wrapper, load_program,
},
sys::{LinkTarget, SyscallError, bpf_link_create},
};
#[derive(Debug)]
#[doc(alias = "BPF_PROG_TYPE_SK_LOOKUP")]
pub struct SkLookup {
pub(crate) data: ProgramData<SkLookupLink>,
}
impl SkLookup {
pub const PROGRAM_TYPE: ProgramType = ProgramType::SkLookup;
pub fn load(&mut self) -> Result<(), ProgramError> {
self.data.expected_attach_type = Some(BPF_SK_LOOKUP);
load_program(BPF_PROG_TYPE_SK_LOOKUP, &mut self.data)
}
pub fn attach<T: AsFd>(&mut self, netns: T) -> Result<SkLookupLinkId, ProgramError> {
let prog_fd = self.fd()?;
let prog_fd = prog_fd.as_fd();
let netns_fd = netns.as_fd();
let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(netns_fd), BPF_SK_LOOKUP, 0, None)
.map_err(|io_error| SyscallError {
call: "bpf_link_create",
io_error,
})?;
self.data
.links
.insert(SkLookupLink::new(FdLink::new(link_fd)))
}
}
define_link_wrapper!(SkLookupLink, SkLookupLinkId, FdLink, FdLinkId, SkLookup);