mirror of https://github.com/aya-rs/aya
aya: add support BPF_PROG_TYPE_SK_SKB programs and SockMaps
parent
b6cd813af5
commit
b57cace941
@ -0,0 +1,102 @@
|
||||
//! An array of eBPF program file descriptors used as a jump table.
|
||||
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
mem,
|
||||
ops::{Deref, DerefMut},
|
||||
os::unix::prelude::RawFd,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
generated::bpf_map_type::BPF_MAP_TYPE_SOCKMAP,
|
||||
maps::{Map, MapError, MapKeys, MapRef, MapRefMut},
|
||||
sys::{bpf_map_delete_elem, bpf_map_update_elem},
|
||||
};
|
||||
|
||||
pub struct SockMap<T: Deref<Target = Map>> {
|
||||
pub(crate) inner: T,
|
||||
}
|
||||
|
||||
impl<T: Deref<Target = Map>> SockMap<T> {
|
||||
fn new(map: T) -> Result<SockMap<T>, MapError> {
|
||||
let map_type = map.obj.def.map_type;
|
||||
if map_type != BPF_MAP_TYPE_SOCKMAP as u32 {
|
||||
return Err(MapError::InvalidMapType {
|
||||
map_type: map_type as u32,
|
||||
})?;
|
||||
}
|
||||
let expected = mem::size_of::<u32>();
|
||||
let size = map.obj.def.key_size as usize;
|
||||
if size != expected {
|
||||
return Err(MapError::InvalidKeySize { size, expected });
|
||||
}
|
||||
|
||||
let expected = mem::size_of::<RawFd>();
|
||||
let size = map.obj.def.value_size as usize;
|
||||
if size != expected {
|
||||
return Err(MapError::InvalidValueSize { size, expected });
|
||||
}
|
||||
let _fd = map.fd_or_err()?;
|
||||
|
||||
Ok(SockMap { inner: map })
|
||||
}
|
||||
|
||||
/// An iterator over the indices of the array that point to a program. The iterator item type
|
||||
/// is `Result<u32, MapError>`.
|
||||
pub unsafe fn indices(&self) -> MapKeys<'_, u32> {
|
||||
MapKeys::new(&self.inner)
|
||||
}
|
||||
|
||||
fn check_bounds(&self, index: u32) -> Result<(), MapError> {
|
||||
let max_entries = self.inner.obj.def.max_entries;
|
||||
if index >= self.inner.obj.def.max_entries {
|
||||
Err(MapError::OutOfBounds { index, max_entries })
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deref<Target = Map> + DerefMut<Target = Map>> SockMap<T> {
|
||||
pub fn set(&mut self, index: u32, tcp_fd: RawFd, flags: u64) -> Result<(), MapError> {
|
||||
let fd = self.inner.fd_or_err()?;
|
||||
self.check_bounds(index)?;
|
||||
bpf_map_update_elem(fd, &index, &tcp_fd, flags).map_err(|(code, io_error)| {
|
||||
MapError::SyscallError {
|
||||
call: "bpf_map_update_elem".to_owned(),
|
||||
code,
|
||||
io_error,
|
||||
}
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clears the value at index in the jump table.
|
||||
pub fn clear_index(&mut self, index: &u32) -> Result<(), MapError> {
|
||||
let fd = self.inner.fd_or_err()?;
|
||||
self.check_bounds(*index)?;
|
||||
bpf_map_delete_elem(fd, index)
|
||||
.map(|_| ())
|
||||
.map_err(|(code, io_error)| MapError::SyscallError {
|
||||
call: "bpf_map_delete_elem".to_owned(),
|
||||
code,
|
||||
io_error,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<MapRef> for SockMap<MapRef> {
|
||||
type Error = MapError;
|
||||
|
||||
fn try_from(a: MapRef) -> Result<SockMap<MapRef>, MapError> {
|
||||
SockMap::new(a)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<MapRefMut> for SockMap<MapRefMut> {
|
||||
type Error = MapError;
|
||||
|
||||
fn try_from(a: MapRefMut) -> Result<SockMap<MapRefMut>, MapError> {
|
||||
SockMap::new(a)
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{
|
||||
generated::{
|
||||
bpf_attach_type::{BPF_SK_SKB_STREAM_PARSER, BPF_SK_SKB_STREAM_VERDICT},
|
||||
bpf_prog_type::BPF_PROG_TYPE_SK_SKB,
|
||||
},
|
||||
maps::{Map, SockMap},
|
||||
programs::{load_program, LinkRef, ProgAttachLink, ProgramData, ProgramError},
|
||||
sys::bpf_prog_attach,
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum SkSkbKind {
|
||||
StreamParser,
|
||||
StreamVerdict,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SkSkb {
|
||||
pub(crate) data: ProgramData,
|
||||
pub(crate) kind: SkSkbKind,
|
||||
}
|
||||
|
||||
impl SkSkb {
|
||||
/// Loads the program inside the kernel.
|
||||
///
|
||||
/// See also [`Program::load`](crate::programs::Program::load).
|
||||
pub fn load(&mut self) -> Result<(), ProgramError> {
|
||||
load_program(BPF_PROG_TYPE_SK_SKB, &mut self.data)
|
||||
}
|
||||
|
||||
/// Returns the name of the program.
|
||||
pub fn name(&self) -> String {
|
||||
self.data.name.to_string()
|
||||
}
|
||||
|
||||
pub fn attach<T: Deref<Target = Map>>(
|
||||
&mut self,
|
||||
map: &SockMap<T>,
|
||||
) -> Result<LinkRef, ProgramError> {
|
||||
let prog_fd = self.data.fd_or_err()?;
|
||||
let map_fd = map.inner.fd_or_err()?;
|
||||
|
||||
let attach_type = match self.kind {
|
||||
SkSkbKind::StreamParser => BPF_SK_SKB_STREAM_PARSER,
|
||||
SkSkbKind::StreamVerdict => BPF_SK_SKB_STREAM_VERDICT,
|
||||
};
|
||||
bpf_prog_attach(prog_fd, map_fd, attach_type).map_err(|(_, io_error)| {
|
||||
ProgramError::SyscallError {
|
||||
call: "bpf_link_create".to_owned(),
|
||||
io_error,
|
||||
}
|
||||
})?;
|
||||
Ok(self.data.link(ProgAttachLink {
|
||||
prog_fd: Some(prog_fd),
|
||||
map_fd: Some(map_fd),
|
||||
attach_type,
|
||||
}))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue