mirror of https://github.com/aya-rs/aya
macros: add 'map' option to xdp macro
This option allows to place the program in the specific sections to chain programs with devmaps and cpumaps.reviewable/pr527/r18
parent
ad3087d7eb
commit
4452364c41
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use aya_bpf::{
|
||||
bindings::xdp_action,
|
||||
macros::{map, xdp},
|
||||
maps::{CpuMap, DevMap, DevMapHash, XskMap},
|
||||
programs::XdpContext,
|
||||
};
|
||||
|
||||
#[map]
|
||||
static SOCKS: XskMap = XskMap::with_max_entries(1, 0);
|
||||
#[map]
|
||||
static DEVS: DevMap = DevMap::with_max_entries(1, 0);
|
||||
#[map]
|
||||
static DEVS_HASH: DevMapHash = DevMapHash::with_max_entries(1, 0);
|
||||
#[map]
|
||||
static CPUS: CpuMap = CpuMap::with_max_entries(1, 0);
|
||||
|
||||
#[xdp]
|
||||
pub fn redirect_sock(_ctx: XdpContext) -> u32 {
|
||||
SOCKS.redirect(0, xdp_action::XDP_ABORTED as u64)
|
||||
}
|
||||
|
||||
#[xdp]
|
||||
pub fn redirect_dev(_ctx: XdpContext) -> u32 {
|
||||
DEVS.redirect(0, xdp_action::XDP_ABORTED as u64)
|
||||
}
|
||||
|
||||
#[xdp]
|
||||
pub fn redirect_dev_hash(_ctx: XdpContext) -> u32 {
|
||||
DEVS_HASH.redirect(10, xdp_action::XDP_ABORTED as u64)
|
||||
}
|
||||
|
||||
#[xdp]
|
||||
pub fn redirect_cpu(_ctx: XdpContext) -> u32 {
|
||||
CPUS.redirect(0, xdp_action::XDP_ABORTED as u64)
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use aya_bpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext};
|
||||
|
||||
macro_rules! probe {
|
||||
($name:ident, ($($arg:ident $(= $value:literal)?),*) ) => {
|
||||
#[xdp($($arg $(= $value)?),*)]
|
||||
pub fn $name(_ctx: XdpContext) -> u32 {
|
||||
XDP_PASS
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
probe!(xdp_plain, ());
|
||||
probe!(xdp_frags, (frags));
|
||||
probe!(xdp_cpumap, (map = "cpumap"));
|
||||
probe!(xdp_devmap, (map = "devmap"));
|
||||
probe!(xdp_frags_cpumap, (frags, map = "cpumap"));
|
||||
probe!(xdp_frags_devmap, (frags, map = "devmap"));
|
||||
|
||||
#[cfg(not(test))]
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
use object::{Object, ObjectSection, ObjectSymbol, SymbolSection};
|
||||
|
||||
#[test]
|
||||
fn prog_sections() {
|
||||
let obj_file = object::File::parse(crate::XDP_SEC).unwrap();
|
||||
|
||||
ensure_symbol(&obj_file, "xdp", "xdp_plain");
|
||||
ensure_symbol(&obj_file, "xdp.frags", "xdp_frags");
|
||||
ensure_symbol(&obj_file, "xdp/cpumap", "xdp_cpumap");
|
||||
ensure_symbol(&obj_file, "xdp/devmap", "xdp_devmap");
|
||||
ensure_symbol(&obj_file, "xdp.frags/cpumap", "xdp_frags_cpumap");
|
||||
ensure_symbol(&obj_file, "xdp.frags/devmap", "xdp_frags_devmap");
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn ensure_symbol(obj_file: &object::File, sec_name: &str, sym_name: &str) {
|
||||
let sec = obj_file.section_by_name(sec_name).unwrap_or_else(|| {
|
||||
let secs = obj_file
|
||||
.sections()
|
||||
.flat_map(|sec| sec.name().ok().map(|name| name.to_owned()))
|
||||
.collect::<Vec<_>>();
|
||||
panic!("section {sec_name} not found. available sections: {secs:?}");
|
||||
});
|
||||
let sec = SymbolSection::Section(sec.index());
|
||||
|
||||
let syms = obj_file
|
||||
.symbols()
|
||||
.filter(|sym| sym.section() == sec)
|
||||
.filter_map(|sym| sym.name().ok())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(
|
||||
syms.contains(&sym_name),
|
||||
"symbol not found. available symbols in section: {syms:?}"
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue