mirror of https://github.com/aya-rs/aya
Add support for BPF_PROG_TYPE_CGROUP_SYSCTL (#256)
* Add support for BPF_PROG_TYPE_CGROUP_SYSCTL This patch adds support for `BPF_PROG_TYPE_CGROUP_SYSCTL`. * Parse unnamed macro * Fix docspull/259/head
parent
2fca4aee4e
commit
f721021a0a
@ -0,0 +1,153 @@
|
||||
use std::{
|
||||
hash::Hash,
|
||||
os::unix::prelude::{AsRawFd, RawFd},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
generated::{bpf_attach_type::BPF_CGROUP_SYSCTL, bpf_prog_type::BPF_PROG_TYPE_CGROUP_SYSCTL},
|
||||
programs::{
|
||||
define_link_wrapper, load_program, FdLink, Link, OwnedLink, ProgAttachLink, ProgramData,
|
||||
ProgramError,
|
||||
},
|
||||
sys::{bpf_link_create, bpf_prog_attach, kernel_version},
|
||||
};
|
||||
|
||||
/// A program used to watch for sysctl changes.
|
||||
///
|
||||
/// [`CgroupSysctl`] programs can be attached to a cgroup and will be called every
|
||||
/// time a process inside that cgroup tries to read from or write to a sysctl knob in proc.
|
||||
///
|
||||
/// # Minimum kernel version
|
||||
///
|
||||
/// The minimum kernel version required to use this feature is 5.2.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// # #[derive(Debug, thiserror::Error)]
|
||||
/// # enum Error {
|
||||
/// # #[error(transparent)]
|
||||
/// # IO(#[from] std::io::Error),
|
||||
/// # #[error(transparent)]
|
||||
/// # Map(#[from] aya::maps::MapError),
|
||||
/// # #[error(transparent)]
|
||||
/// # Program(#[from] aya::programs::ProgramError),
|
||||
/// # #[error(transparent)]
|
||||
/// # Bpf(#[from] aya::BpfError)
|
||||
/// # }
|
||||
/// # let mut bpf = aya::Bpf::load(&[])?;
|
||||
/// use std::fs::File;
|
||||
/// use std::convert::TryInto;
|
||||
/// use aya::programs::CgroupSysctl;
|
||||
///
|
||||
/// let file = File::open("/sys/fs/cgroup/unified")?;
|
||||
/// let program: &mut CgroupSysctl = bpf.program_mut("cgroup_sysctl").unwrap().try_into()?;
|
||||
/// program.load()?;
|
||||
/// program.attach(file)?;
|
||||
/// # Ok::<(), Error>(())
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
#[doc(alias = "BPF_PROG_TYPE_CGROUP_SYSCTL")]
|
||||
pub struct CgroupSysctl {
|
||||
pub(crate) data: ProgramData<CgroupSysctlLink>,
|
||||
}
|
||||
|
||||
impl CgroupSysctl {
|
||||
/// Loads the program inside the kernel.
|
||||
pub fn load(&mut self) -> Result<(), ProgramError> {
|
||||
load_program(BPF_PROG_TYPE_CGROUP_SYSCTL, &mut self.data)
|
||||
}
|
||||
|
||||
/// Attaches the program to the given cgroup.
|
||||
///
|
||||
/// The returned value can be used to detach, see [CgroupSysctl::detach].
|
||||
pub fn attach<T: AsRawFd>(&mut self, cgroup: T) -> Result<CgroupSysctlLinkId, ProgramError> {
|
||||
let prog_fd = self.data.fd_or_err()?;
|
||||
let cgroup_fd = cgroup.as_raw_fd();
|
||||
|
||||
let k_ver = kernel_version().unwrap();
|
||||
if k_ver >= (5, 7, 0) {
|
||||
let link_fd = bpf_link_create(prog_fd, cgroup_fd, BPF_CGROUP_SYSCTL, None, 0).map_err(
|
||||
|(_, io_error)| ProgramError::SyscallError {
|
||||
call: "bpf_link_create".to_owned(),
|
||||
io_error,
|
||||
},
|
||||
)? as RawFd;
|
||||
self.data
|
||||
.links
|
||||
.insert(CgroupSysctlLink(CgroupSysctlLinkInner::Fd(FdLink::new(
|
||||
link_fd,
|
||||
))))
|
||||
} else {
|
||||
bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SYSCTL).map_err(|(_, io_error)| {
|
||||
ProgramError::SyscallError {
|
||||
call: "bpf_prog_attach".to_owned(),
|
||||
io_error,
|
||||
}
|
||||
})?;
|
||||
|
||||
self.data
|
||||
.links
|
||||
.insert(CgroupSysctlLink(CgroupSysctlLinkInner::ProgAttach(
|
||||
ProgAttachLink::new(prog_fd, cgroup_fd, BPF_CGROUP_SYSCTL),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes ownership of the link referenced by the provided link_id.
|
||||
///
|
||||
/// The link will be detached on `Drop` and the caller is now responsible
|
||||
/// for managing its lifetime.
|
||||
pub fn forget_link(
|
||||
&mut self,
|
||||
link_id: CgroupSysctlLinkId,
|
||||
) -> Result<OwnedLink<CgroupSysctlLink>, ProgramError> {
|
||||
Ok(OwnedLink::new(self.data.forget_link(link_id)?))
|
||||
}
|
||||
|
||||
/// Detaches the program.
|
||||
///
|
||||
/// See [CgroupSysctl::attach].
|
||||
pub fn detach(&mut self, link_id: CgroupSysctlLinkId) -> Result<(), ProgramError> {
|
||||
self.data.links.remove(link_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, Eq, PartialEq)]
|
||||
enum CgroupSysctlLinkIdInner {
|
||||
Fd(<FdLink as Link>::Id),
|
||||
ProgAttach(<ProgAttachLink as Link>::Id),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CgroupSysctlLinkInner {
|
||||
Fd(FdLink),
|
||||
ProgAttach(ProgAttachLink),
|
||||
}
|
||||
|
||||
impl Link for CgroupSysctlLinkInner {
|
||||
type Id = CgroupSysctlLinkIdInner;
|
||||
|
||||
fn id(&self) -> Self::Id {
|
||||
match self {
|
||||
CgroupSysctlLinkInner::Fd(fd) => CgroupSysctlLinkIdInner::Fd(fd.id()),
|
||||
CgroupSysctlLinkInner::ProgAttach(p) => CgroupSysctlLinkIdInner::ProgAttach(p.id()),
|
||||
}
|
||||
}
|
||||
|
||||
fn detach(self) -> Result<(), ProgramError> {
|
||||
match self {
|
||||
CgroupSysctlLinkInner::Fd(fd) => fd.detach(),
|
||||
CgroupSysctlLinkInner::ProgAttach(p) => p.detach(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define_link_wrapper!(
|
||||
/// The link used by [CgroupSysctl] programs.
|
||||
CgroupSysctlLink,
|
||||
/// The type returned by [CgroupSysctl::attach]. Can be passed to [CgroupSysctl::detach].
|
||||
CgroupSysctlLinkId,
|
||||
CgroupSysctlLinkInner,
|
||||
CgroupSysctlLinkIdInner
|
||||
);
|
@ -0,0 +1,19 @@
|
||||
use core::ffi::c_void;
|
||||
|
||||
use crate::{bindings::bpf_sysctl, BpfContext};
|
||||
|
||||
pub struct SysctlContext {
|
||||
pub sysctl: *mut bpf_sysctl,
|
||||
}
|
||||
|
||||
impl SysctlContext {
|
||||
pub fn new(sysctl: *mut bpf_sysctl) -> SysctlContext {
|
||||
SysctlContext { sysctl }
|
||||
}
|
||||
}
|
||||
|
||||
impl BpfContext for SysctlContext {
|
||||
fn as_ptr(&self) -> *mut c_void {
|
||||
self.sysctl as *mut _
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue