Skip to content

Commit

Permalink
bpf: Implement XDP maps
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Tucker <[email protected]>
  • Loading branch information
dave-tucker committed May 30, 2022
1 parent 2cb67eb commit 464102b
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bpf/aya-bpf/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod queue;
pub mod sock_hash;
pub mod sock_map;
pub mod stack_trace;
pub mod xdp;

pub use array::Array;
pub use hash_map::{HashMap, LruHashMap, LruPerCpuHashMap, PerCpuHashMap};
Expand All @@ -26,3 +27,4 @@ pub use queue::Queue;
pub use sock_hash::SockHash;
pub use sock_map::SockMap;
pub use stack_trace::StackTrace;
pub use xdp::{CpuMap, DevMap, DevMapHash, XskMap};
55 changes: 55 additions & 0 deletions bpf/aya-bpf/src/maps/xdp/cpu_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use core::{mem, ptr::NonNull};

use aya_bpf_cty::c_void;

use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_CPUMAP},
helpers::bpf_map_lookup_elem,
maps::PinningType,
};

#[repr(transparent)]
pub struct CpuMap {
def: bpf_map_def,
}

impl CpuMap {
pub const fn with_max_entries(max_entries: u32, flags: u32) -> CpuMap {
CpuMap {
def: bpf_map_def {
type_: BPF_MAP_TYPE_CPUMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub const fn pinned(max_entries: u32, flags: u32) -> CpuMap {
CpuMap {
def: bpf_map_def {
type_: BPF_MAP_TYPE_CPUMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub fn get(&mut self, index: u32) -> Option<&u32> {
unsafe {
let value = bpf_map_lookup_elem(
&mut self.def as *mut _ as *mut _,
&index as *const _ as *const c_void,
);
// FIXME: alignment
NonNull::new(value as *mut u32).map(|p| p.as_ref())
}
}
}
55 changes: 55 additions & 0 deletions bpf/aya-bpf/src/maps/xdp/dev_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use core::{mem, ptr::NonNull};

use aya_bpf_cty::c_void;

use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_DEVMAP},
helpers::bpf_map_lookup_elem,
maps::PinningType,
};

#[repr(transparent)]
pub struct DevMap {
def: bpf_map_def,
}

impl DevMap {
pub const fn with_max_entries(max_entries: u32, flags: u32) -> DevMap {
DevMap {
def: bpf_map_def {
type_: BPF_MAP_TYPE_DEVMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub const fn pinned(max_entries: u32, flags: u32) -> DevMap {
DevMap {
def: bpf_map_def {
type_: BPF_MAP_TYPE_DEVMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub fn get(&mut self, index: u32) -> Option<&u32> {
unsafe {
let value = bpf_map_lookup_elem(
&mut self.def as *mut _ as *mut _,
&index as *const _ as *const c_void,
);
// FIXME: alignment
NonNull::new(value as *mut u32).map(|p| p.as_ref())
}
}
}
55 changes: 55 additions & 0 deletions bpf/aya-bpf/src/maps/xdp/dev_map_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use core::{mem, ptr::NonNull};

use aya_bpf_cty::c_void;

use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_DEVMAP_HASH},
helpers::bpf_map_lookup_elem,
maps::PinningType,
};

#[repr(transparent)]
pub struct DevMapHash {
def: bpf_map_def,
}

impl DevMapHash {
pub const fn with_max_entries(max_entries: u32, flags: u32) -> DevMapHash {
DevMapHash {
def: bpf_map_def {
type_: BPF_MAP_TYPE_DEVMAP_HASH,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub const fn pinned(max_entries: u32, flags: u32) -> DevMapHash {
DevMapHash {
def: bpf_map_def {
type_: BPF_MAP_TYPE_DEVMAP_HASH,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub fn get(&mut self, index: u32) -> Option<&u32> {
unsafe {
let value = bpf_map_lookup_elem(
&mut self.def as *mut _ as *mut _,
&index as *const _ as *const c_void,
);
// FIXME: alignment
NonNull::new(value as *mut u32).map(|p| p.as_ref())
}
}
}
9 changes: 9 additions & 0 deletions bpf/aya-bpf/src/maps/xdp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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;
55 changes: 55 additions & 0 deletions bpf/aya-bpf/src/maps/xdp/xsk_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use core::{mem, ptr::NonNull};

use aya_bpf_cty::c_void;

use crate::{
bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_XSKMAP},
helpers::bpf_map_lookup_elem,
maps::PinningType,
};

#[repr(transparent)]
pub struct XskMap {
def: bpf_map_def,
}

impl XskMap {
pub const fn with_max_entries(max_entries: u32, flags: u32) -> XskMap {
XskMap {
def: bpf_map_def {
type_: BPF_MAP_TYPE_XSKMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub const fn pinned(max_entries: u32, flags: u32) -> XskMap {
XskMap {
def: bpf_map_def {
type_: BPF_MAP_TYPE_XSKMAP,
key_size: mem::size_of::<u32>() as u32,
value_size: mem::size_of::<u32>() as u32,
max_entries,
map_flags: flags,
id: 0,
pinning: PinningType::None as u32,
},
}
}

pub fn get(&mut self, index: u32) -> Option<&u32> {
unsafe {
let value = bpf_map_lookup_elem(
&mut self.def as *mut _ as *mut _,
&index as *const _ as *const c_void,
);
// FIXME: alignment
NonNull::new(value as *mut u32).map(|p| p.as_ref())
}
}
}

0 comments on commit 464102b

Please sign in to comment.