feat(aya-ebpf): Add integration test for memmove implementation

pull/983/head
Wouter Dullaert 5 months ago committed by Michal Rostecki
parent 7ad3926d99
commit fb0a339adf

@ -11,6 +11,7 @@ edition.workspace = true
[dependencies] [dependencies]
aya-ebpf = { path = "../../ebpf/aya-ebpf" } aya-ebpf = { path = "../../ebpf/aya-ebpf" }
aya-log-ebpf = { path = "../../ebpf/aya-log-ebpf" } aya-log-ebpf = { path = "../../ebpf/aya-log-ebpf" }
network-types = "0.0.6"
[build-dependencies] [build-dependencies]
which = { workspace = true } which = { workspace = true }
@ -59,3 +60,7 @@ path = "src/xdp_sec.rs"
[[bin]] [[bin]]
name = "ring_buf" name = "ring_buf"
path = "src/ring_buf.rs" path = "src/ring_buf.rs"
[[bin]]
name = "memmove_test"
path = "src/memmove_test.rs"

@ -0,0 +1,61 @@
#![no_std]
#![no_main]
use core::mem;
use aya_ebpf::{
bindings::{xdp_action, BPF_F_NO_PREALLOC},
macros::{map, xdp},
maps::HashMap,
programs::XdpContext,
};
use network_types::{
eth::{EthHdr, EtherType},
ip::Ipv6Hdr,
};
#[inline(always)]
fn ptr_at<T>(ctx: &XdpContext, offset: usize) -> Result<*const T, ()> {
let start = ctx.data();
let end = ctx.data_end();
let len = mem::size_of::<T>();
if start + offset + len > end {
return Err(());
}
Ok((start + offset) as *const T)
}
struct Value {
pub orig_ip: [u8; 16],
}
#[map]
static RULES: HashMap<u8, Value> = HashMap::<u8, Value>::with_max_entries(1, BPF_F_NO_PREALLOC);
#[xdp]
pub fn do_dnat(ctx: XdpContext) -> u32 {
try_do_dnat(ctx).unwrap_or(xdp_action::XDP_DROP)
}
fn try_do_dnat(ctx: XdpContext) -> Result<u32, ()> {
let index = 0;
if let Some(nat) = unsafe { RULES.get(&index) } {
let hproto: *const EtherType = ptr_at(&ctx, mem::offset_of!(EthHdr, ether_type))?;
match unsafe { *hproto } {
EtherType::Ipv6 => {
let ip_hdr: *const Ipv6Hdr = ptr_at(&ctx, EthHdr::LEN)?;
unsafe { (*ip_hdr.cast_mut()).dst_addr.in6_u.u6_addr8 = nat.orig_ip };
}
_ => return Ok(xdp_action::XDP_PASS),
}
}
Ok(xdp_action::XDP_PASS)
}
#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -22,6 +22,7 @@ pub const BPF_PROBE_READ: &[u8] =
pub const REDIRECT: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/redirect")); pub const REDIRECT: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/redirect"));
pub const XDP_SEC: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/xdp_sec")); pub const XDP_SEC: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/xdp_sec"));
pub const RING_BUF: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/ring_buf")); pub const RING_BUF: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/ring_buf"));
pub const MEMMOVE_TEST: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/memmove_test"));
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

@ -282,6 +282,15 @@ fn unload_kprobe() {
assert_unloaded("test_kprobe"); assert_unloaded("test_kprobe");
} }
#[test]
fn memmove() {
let mut bpf = Ebpf::load(crate::MEMMOVE_TEST).unwrap();
let prog: &mut Xdp = bpf.program_mut("do_dnat").unwrap().try_into().unwrap();
prog.load().unwrap();
assert_loaded("do_dnat");
}
#[test] #[test]
fn basic_tracepoint() { fn basic_tracepoint() {
let mut bpf = Ebpf::load(crate::TEST).unwrap(); let mut bpf = Ebpf::load(crate::TEST).unwrap();

Loading…
Cancel
Save