-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds implementations for: - xskmap - devmap - devmap_hash - cpumap Which can all be used to redirect XDP packets to various different locations Signed-off-by: Dave Tucker <[email protected]>
- Loading branch information
1 parent
a047354
commit 0ef6d8b
Showing
6 changed files
with
486 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//! An array of available CPUs. | ||
use std::{ | ||
convert::TryFrom, | ||
mem, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use crate::{ | ||
generated::bpf_map_type::BPF_MAP_TYPE_CPUMAP, | ||
maps::{Map, MapError, MapRef, MapRefMut}, | ||
sys::bpf_map_update_elem, | ||
}; | ||
|
||
/// An array of available CPUs. | ||
/// | ||
/// XDP programs can use this map to redirect packets to a target | ||
/// CPU for processing. | ||
/// | ||
/// # Minimum kernel version | ||
/// | ||
/// The minimum kernel version required to use this feature is 4.2. | ||
/// | ||
/// # Examples | ||
/// ```no_run | ||
/// # let bpf = aya::Bpf::load(&[])?; | ||
/// use aya::maps::CpuMap; | ||
/// use std::convert::{TryFrom, TryInto}; | ||
/// | ||
/// let mut cpumap = CpuMap::try_from(bpf.map_mut("CPUS")?)?; | ||
/// let flags = 0; | ||
/// let queue_size = 2048; | ||
/// for i in 0u32..8u32 { | ||
/// cpumap.set(i, queue_size, flags); | ||
/// } | ||
/// | ||
/// # Ok::<(), aya::BpfError>(()) | ||
/// ``` | ||
#[doc(alias = "BPF_MAP_TYPE_CPUMAP")] | ||
pub struct CpuMap<T: Deref<Target = Map>> { | ||
inner: T, | ||
} | ||
|
||
impl<T: Deref<Target = Map>> CpuMap<T> { | ||
fn new(map: T) -> Result<CpuMap<T>, MapError> { | ||
let map_type = map.obj.def.map_type; | ||
if map_type != BPF_MAP_TYPE_CPUMAP 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::<u32>(); | ||
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(CpuMap { inner: map }) | ||
} | ||
|
||
/// Returns the number of elements in the array. | ||
/// | ||
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. | ||
pub fn len(&self) -> u32 { | ||
self.inner.obj.def.max_entries | ||
} | ||
|
||
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>> CpuMap<T> { | ||
/// Sets the value of the element at the given index. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] | ||
/// if `bpf_map_update_elem` fails. | ||
pub fn set(&mut self, index: u32, value: u32, flags: u64) -> Result<(), MapError> { | ||
let fd = self.inner.fd_or_err()?; | ||
self.check_bounds(index)?; | ||
bpf_map_update_elem(fd, &index, &value, flags).map_err(|(code, io_error)| { | ||
MapError::SyscallError { | ||
call: "bpf_map_update_elem".to_owned(), | ||
code, | ||
io_error, | ||
} | ||
})?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TryFrom<MapRef> for CpuMap<MapRef> { | ||
type Error = MapError; | ||
|
||
fn try_from(a: MapRef) -> Result<CpuMap<MapRef>, MapError> { | ||
CpuMap::new(a) | ||
} | ||
} | ||
|
||
impl TryFrom<MapRefMut> for CpuMap<MapRefMut> { | ||
type Error = MapError; | ||
|
||
fn try_from(a: MapRefMut) -> Result<CpuMap<MapRefMut>, MapError> { | ||
CpuMap::new(a) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//! An array of network devices. | ||
use std::{ | ||
convert::TryFrom, | ||
mem, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use crate::{ | ||
generated::bpf_map_type::BPF_MAP_TYPE_DEVMAP, | ||
maps::{Map, MapError, MapRef, MapRefMut}, | ||
sys::bpf_map_update_elem, | ||
}; | ||
|
||
/// An array of network devices. | ||
/// | ||
/// XDP programs can use this map to redirect to other network | ||
/// devices. | ||
/// | ||
/// # Minimum kernel version | ||
/// | ||
/// The minimum kernel version required to use this feature is 4.2. | ||
/// | ||
/// # Examples | ||
/// ```no_run | ||
/// # let bpf = aya::Bpf::load(&[])?; | ||
/// use aya::maps::DevMap; | ||
/// use std::convert::{TryFrom, TryInto}; | ||
/// | ||
/// let mut devmap = DevMap::try_from(bpf.map_mut("IFACES")?)?; | ||
/// let flags = 0; | ||
/// let ifindex = 32u32; | ||
/// devmap.set(ifindex, ifindex, flags); | ||
/// | ||
/// # Ok::<(), aya::BpfError>(()) | ||
/// ``` | ||
#[doc(alias = "BPF_MAP_TYPE_DEVMAP")] | ||
pub struct DevMap<T: Deref<Target = Map>> { | ||
inner: T, | ||
} | ||
|
||
impl<T: Deref<Target = Map>> DevMap<T> { | ||
fn new(map: T) -> Result<DevMap<T>, MapError> { | ||
let map_type = map.obj.def.map_type; | ||
if map_type != BPF_MAP_TYPE_DEVMAP 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::<u32>(); | ||
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(DevMap { inner: map }) | ||
} | ||
|
||
/// Returns the number of elements in the array. | ||
/// | ||
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. | ||
pub fn len(&self) -> u32 { | ||
self.inner.obj.def.max_entries | ||
} | ||
|
||
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>> DevMap<T> { | ||
/// Sets the value of the element at the given index. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] | ||
/// if `bpf_map_update_elem` fails. | ||
pub fn set(&mut self, index: u32, value: u32, flags: u64) -> Result<(), MapError> { | ||
let fd = self.inner.fd_or_err()?; | ||
self.check_bounds(index)?; | ||
bpf_map_update_elem(fd, &index, &value, flags).map_err(|(code, io_error)| { | ||
MapError::SyscallError { | ||
call: "bpf_map_update_elem".to_owned(), | ||
code, | ||
io_error, | ||
} | ||
})?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TryFrom<MapRef> for DevMap<MapRef> { | ||
type Error = MapError; | ||
|
||
fn try_from(a: MapRef) -> Result<DevMap<MapRef>, MapError> { | ||
DevMap::new(a) | ||
} | ||
} | ||
|
||
impl TryFrom<MapRefMut> for DevMap<MapRefMut> { | ||
type Error = MapError; | ||
|
||
fn try_from(a: MapRefMut) -> Result<DevMap<MapRefMut>, MapError> { | ||
DevMap::new(a) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//! An array of network devices. | ||
use std::{ | ||
convert::TryFrom, | ||
mem, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use crate::{ | ||
generated::bpf_map_type::BPF_MAP_TYPE_DEVMAP_HASH, | ||
maps::{Map, MapError, MapRef, MapRefMut}, | ||
sys::bpf_map_update_elem, | ||
}; | ||
|
||
/// An array of network devices. | ||
/// | ||
/// XDP programs can use this map to redirect to other network | ||
/// devices. | ||
/// | ||
/// # Minimum kernel version | ||
/// | ||
/// The minimum kernel version required to use this feature is 4.2. | ||
/// | ||
/// # Examples | ||
/// ```no_run | ||
/// # let bpf = aya::Bpf::load(&[])?; | ||
/// use aya::maps::DevMapHash; | ||
/// use std::convert::{TryFrom, TryInto}; | ||
/// | ||
/// let mut devmap = DevMapHash::try_from(bpf.map_mut("IFACES")?)?; | ||
/// let flags = 0; | ||
/// let ifindex = 32u32; | ||
/// devmap.set(ifindex, ifindex, flags); | ||
/// | ||
/// # Ok::<(), aya::BpfError>(()) | ||
/// ``` | ||
#[doc(alias = "BPF_MAP_TYPE_DEVMAP_HASH")] | ||
pub struct DevMapHash<T: Deref<Target = Map>> { | ||
inner: T, | ||
} | ||
|
||
impl<T: Deref<Target = Map>> DevMapHash<T> { | ||
fn new(map: T) -> Result<DevMapHash<T>, MapError> { | ||
let map_type = map.obj.def.map_type; | ||
if map_type != BPF_MAP_TYPE_DEVMAP_HASH 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::<u32>(); | ||
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(DevMapHash { inner: map }) | ||
} | ||
|
||
/// Returns the number of elements in the array. | ||
/// | ||
/// This corresponds to the value of `bpf_map_def::max_entries` on the eBPF side. | ||
pub fn len(&self) -> u32 { | ||
self.inner.obj.def.max_entries | ||
} | ||
|
||
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>> DevMapHash<T> { | ||
/// Sets the value of the element at the given index. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] | ||
/// if `bpf_map_update_elem` fails. | ||
pub fn set(&mut self, index: u32, value: u32, flags: u64) -> Result<(), MapError> { | ||
let fd = self.inner.fd_or_err()?; | ||
self.check_bounds(index)?; | ||
bpf_map_update_elem(fd, &index, &value, flags).map_err(|(code, io_error)| { | ||
MapError::SyscallError { | ||
call: "bpf_map_update_elem".to_owned(), | ||
code, | ||
io_error, | ||
} | ||
})?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TryFrom<MapRef> for DevMapHash<MapRef> { | ||
type Error = MapError; | ||
|
||
fn try_from(a: MapRef) -> Result<DevMapHash<MapRef>, MapError> { | ||
DevMapHash::new(a) | ||
} | ||
} | ||
|
||
impl TryFrom<MapRefMut> for DevMapHash<MapRefMut> { | ||
type Error = MapError; | ||
|
||
fn try_from(a: MapRefMut) -> Result<DevMapHash<MapRefMut>, MapError> { | ||
DevMapHash::new(a) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! XDP maps. | ||
mod cpu_map; | ||
mod dev_map; | ||
mod dev_map_hash; | ||
mod xsk_map; | ||
|
||
pub use cpu_map::CpuMap; | ||
pub use dev_map::DevMap; | ||
pub use dev_map_hash::DevMapHash; | ||
pub use xsk_map::XskMap; |
Oops, something went wrong.