Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
reviewable/pr1319/r2
Xiaobo Liu 4 weeks ago
parent 90059a9e70
commit 13afb1c642

@ -64,3 +64,12 @@ pub mod strncmp {
#[cfg(feature = "user")]
unsafe impl aya::Pod for TestResult {}
}
pub mod queue {
#[derive(Copy, Clone)]
#[repr(C)]
pub struct TestResult(pub u64);
#[cfg(feature = "user")]
unsafe impl aya::Pod for TestResult {}
}

@ -91,3 +91,7 @@ path = "src/xdp_sec.rs"
[[bin]]
name = "uprobe_cookie"
path = "src/uprobe_cookie.rs"
[[bin]]
name = "queue_test"
path = "src/queue_test.rs"

@ -0,0 +1,47 @@
#![no_std]
#![no_main]
use aya_ebpf::maps::{Array, Queue};
use aya_ebpf::{
cty::c_long,
macros::{map, uprobe},
programs::ProbeContext,
};
#[cfg(not(test))]
extern crate ebpf_panic;
const PEEK_INDEX: u32 = 0;
const POP_INDEX: u32 = 1;
#[map]
static RESULT: Array<i64> = Array::<i64>::with_max_entries(2, 0);
#[map]
static TEST_QUEUE: Queue<i64> = Queue::with_max_entries(10, 0);
#[uprobe]
pub fn test_queue_push(ctx: ProbeContext) -> Result<(), c_long> {
let value: i64 = ctx.arg(0).ok_or(-1)?;
TEST_QUEUE.push(&value, 0)?;
Ok(())
}
#[uprobe]
pub fn test_queue_peek(_: ProbeContext) -> Result<(), c_long> {
let value = TEST_QUEUE.peek().unwrap_or(-1);
let result = RESULT.get_ptr_mut(PEEK_INDEX).ok_or(-1)?;
unsafe {
*result = value;
}
Ok(())
}
#[uprobe]
pub fn test_queue_pop(_: ProbeContext) -> Result<(), c_long> {
let value = TEST_QUEUE.pop().unwrap_or(-1);
let result = RESULT.get_ptr_mut(POP_INDEX).ok_or(-1)?;
unsafe {
*result = value;
}
Ok(())
}

@ -49,6 +49,7 @@ bpf_file!(
RELOCATIONS => "relocations",
RING_BUF => "ring_buf",
SIMPLE_PROG => "simple_prog",
QUEUE_TEST => "queue_test",
STRNCMP => "strncmp",
TCX => "tcx",
TEST => "test",

@ -6,6 +6,7 @@ mod info;
mod iter;
mod load;
mod log;
mod queue;
mod raw_tracepoint;
mod rbpf;
mod relocations;

@ -0,0 +1,66 @@
use aya::{EbpfLoader, maps::Array, programs::UProbe};
const PEEK_INDEX: u32 = 0;
const POP_INDEX: u32 = 1;
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_push(arg: i64) {
core::hint::black_box(arg);
}
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_peek() {
core::hint::black_box(trigger_peek);
}
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_pop() {
core::hint::black_box(trigger_pop);
}
#[test_log::test]
fn queue_basic() {
let mut bpf = EbpfLoader::new().load(crate::QUEUE_TEST).unwrap();
let prog: &mut UProbe = bpf
.program_mut("test_queue_push")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_push", "/proc/self/exe", None, None)
.unwrap();
let prog: &mut UProbe = bpf
.program_mut("test_queue_peek")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_peek", "/proc/self/exe", None, None)
.unwrap();
let prog: &mut UProbe = bpf
.program_mut("test_queue_pop")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_pop", "/proc/self/exe", None, None)
.unwrap();
//let queue = Queue::<_, u64>::try_from(bpf.take_map("TEST_QUEUE").unwrap()).unwrap();
let array = Array::<_, i64>::try_from(bpf.map("RESULT").unwrap()).unwrap();
for i in 0..5 {
trigger_push(i);
trigger_peek();
trigger_pop();
assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i);
assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i);
}
}
Loading…
Cancel
Save