Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
reviewable/pr1319/r6
Xiaobo Liu 4 weeks ago
parent cb22d53ad3
commit a2959b3e02

@ -2,7 +2,7 @@
#![no_main]
use aya_ebpf::{
cty::{c_int, c_long},
cty::c_long,
macros::{map, uprobe},
maps::{Array, Queue},
programs::ProbeContext,
@ -14,33 +14,33 @@ const PEEK_INDEX: u32 = 0;
const POP_INDEX: u32 = 1;
#[map]
static RESULT: Array<c_int> = Array::<c_int>::with_max_entries(10, 0);
static RESULT: Array<u64> = Array::<u64>::with_max_entries(2, 0);
#[map]
static TEST_QUEUE: Queue<c_int> = Queue::with_max_entries(10, 0);
static TEST_QUEUE: Queue<u64> = Queue::with_max_entries(10, 0);
#[uprobe]
pub fn test_queue_push(ctx: ProbeContext) -> Result<(), c_long> {
let value: c_int = ctx.arg(0).ok_or(-2)?;
let value = 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);
result_set(PEEK_INDEX, value)?;
pub fn test_queue_pop(_: ProbeContext) -> Result<(), c_long> {
let value = TEST_QUEUE.pop().ok_or(-1)?;
result_set(POP_INDEX, value)?;
Ok(())
}
#[uprobe]
pub fn test_queue_pop(_: ProbeContext) -> Result<(), c_long> {
let value = TEST_QUEUE.pop().unwrap_or(-1);
result_set(POP_INDEX, value)?;
pub fn test_queue_peek(_: ProbeContext) -> Result<(), c_long> {
let value = TEST_QUEUE.peek().ok_or(-1)?;
result_set(PEEK_INDEX, value)?;
Ok(())
}
fn result_set(index: u32, value: c_int) -> Result<(), c_long> {
fn result_set(index: u32, value: u64) -> 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)?;

@ -1,5 +1,3 @@
use std::ffi::c_int;
use aya::{EbpfLoader, maps::Array, programs::UProbe};
const PEEK_INDEX: u32 = 0;
@ -7,7 +5,7 @@ const POP_INDEX: u32 = 1;
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_queue_push(arg: c_int) {
pub extern "C" fn trigger_queue_push(arg: u64) {
core::hint::black_box(arg);
}
@ -37,33 +35,33 @@ fn queue_basic() {
.unwrap();
let prog: &mut UProbe = bpf
.program_mut("test_queue_peek")
.program_mut("test_queue_pop")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_queue_peek", "/proc/self/exe", None, None)
prog.attach("trigger_queue_pop", "/proc/self/exe", None, None)
.unwrap();
let prog: &mut UProbe = bpf
.program_mut("test_queue_pop")
.program_mut("test_queue_peek")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("trigger_queue_pop", "/proc/self/exe", None, None)
prog.attach("trigger_queue_peek", "/proc/self/exe", None, None)
.unwrap();
let array_map = bpf.map("RESULT").unwrap();
let array = Array::<_, c_int>::try_from(array_map).unwrap();
let array = Array::<_, u64>::try_from(array_map).unwrap();
for i in 0..9 {
trigger_queue_push(i);
trigger_queue_peek();
assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i as c_int);
assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i);
trigger_queue_pop();
assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i as c_int);
assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i);
}
}

Loading…
Cancel
Save