Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
reviewable/pr1319/r4
Xiaobo Liu 4 weeks ago
parent ea4459a669
commit 5124bb95f7

@ -2,7 +2,7 @@
#![no_main]
use aya_ebpf::{
cty::c_long,
cty::{c_int, c_long},
macros::{map, uprobe},
maps::{Array, Queue},
programs::ProbeContext,
@ -14,14 +14,14 @@ const PEEK_INDEX: u32 = 0;
const POP_INDEX: u32 = 1;
#[map]
static RESULT: Array<i64> = Array::<i64>::with_max_entries(2, 0);
static RESULT: Array<c_int> = Array::<c_int>::with_max_entries(10, 0);
#[map]
static TEST_QUEUE: Queue<i64> = Queue::with_max_entries(10, 0);
static TEST_QUEUE: Queue<c_int> = 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)?;
let value: c_int = ctx.arg(0).ok_or(-2)?;
TEST_QUEUE.push(&value, 0)?;
Ok(())
}
@ -29,19 +29,21 @@ pub fn test_queue_push(ctx: ProbeContext) -> Result<(), c_long> {
#[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;
}
result_set(PEEK_INDEX, 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;
}
result_set(POP_INDEX, value)?;
Ok(())
}
fn result_set(index: u32, value: c_int) -> Result<(), c_long> {
let ptr = RESULT.get_ptr_mut(index).ok_or(-1)?;
let dst = unsafe { ptr.as_mut() };
let dst_res = dst.ok_or(-1)?;
*dst_res = value;
Ok(())
}

@ -1,24 +1,25 @@
use aya::{EbpfLoader, maps::Array, programs::UProbe};
use std::ffi::c_int;
const PEEK_INDEX: u32 = 0;
const POP_INDEX: u32 = 1;
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_push(arg: i64) {
pub extern "C" fn trigger_queue_push(arg: c_int) {
core::hint::black_box(arg);
}
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_peek() {
core::hint::black_box(trigger_peek);
pub extern "C" fn trigger_queue_peek() {
core::hint::black_box(trigger_queue_peek);
}
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_pop() {
core::hint::black_box(trigger_pop);
pub extern "C" fn trigger_queue_pop() {
core::hint::black_box(trigger_queue_pop);
}
#[test_log::test]
@ -31,7 +32,7 @@ fn queue_basic() {
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_push", "/proc/self/exe", None, None)
prog.attach("trigger_queue_push", "/proc/self/exe", None, None)
.unwrap();
let prog: &mut UProbe = bpf
@ -40,7 +41,7 @@ fn queue_basic() {
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_peek", "/proc/self/exe", None, None)
prog.attach("trigger_queue_peek", "/proc/self/exe", None, None)
.unwrap();
let prog: &mut UProbe = bpf
@ -49,18 +50,19 @@ fn queue_basic() {
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_pop", "/proc/self/exe", None, None)
prog.attach("trigger_queue_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();
let array_map = bpf.map("RESULT").unwrap();
let array = Array::<_, c_int>::try_from(array_map).unwrap();
for i in 0..5 {
trigger_push(i);
trigger_peek();
trigger_pop();
for i in 0..9 {
trigger_queue_push(i);
assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i);
assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i);
trigger_queue_peek();
assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i as c_int);
trigger_queue_pop();
assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i as c_int);
}
}

Loading…
Cancel
Save