Skip to content

Commit

Permalink
integration-test: add a RingBuf test
Browse files Browse the repository at this point in the history
This simple test exercises sending data from a uprobe.
  • Loading branch information
ajwerner committed Jun 19, 2023
1 parent 1d73bf3 commit b797e81
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
4 changes: 4 additions & 0 deletions test/integration-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ path = "src/test.rs"
[[bin]]
name = "relocations"
path = "src/relocations.rs"

[[bin]]
name = "ring_buf"
path = "src/ring_buf.rs"
26 changes: 26 additions & 0 deletions test/integration-ebpf/src/ring_buf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![no_std]
#![no_main]

use aya_bpf::{
macros::{map, uprobe},
maps::RingBuf,
programs::ProbeContext,
};

#[map]
static RING_BUF: RingBuf = RingBuf::with_max_entries(4096, 0);

#[uprobe]
pub fn ring_buf_test(ctx: ProbeContext) {
// Write the first argument to the function back out to RING_BUF.
let Some(arg): Option<u64> = ctx.arg(0) else { return };
if let Some(mut entry) = RING_BUF.reserve::<u64>(0) {
entry.write(arg);
entry.submit(0)
};
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
1 change: 1 addition & 0 deletions test/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ regex = "1"
tempfile = "3.3.0"
libtest-mimic = "0.6.0"
tokio = { version = "1.24", features = ["rt", "rt-multi-thread", "sync", "time"] }
rand = { version = "0.8.5" }
2 changes: 1 addition & 1 deletion test/integration-test/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mod load;
pub mod log;
pub mod rbpf;
pub mod relocations;
pub mod ring_buf;
pub mod smoke;

pub use integration_test_macros::{integration_test, tokio_integration_test};

#[derive(Debug)]
Expand Down
52 changes: 52 additions & 0 deletions test/integration-test/src/tests/ring_buf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use aya::{include_bytes_aligned, maps::ringbuf::RingBuf, programs::UProbe, Bpf};

use super::integration_test;

#[integration_test]
fn ring_buf() {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/release/ring_buf");
let mut bpf = Bpf::load(bytes).unwrap();
let ring_buf = bpf.take_map("RING_BUF").unwrap();
let mut ring_buf = RingBuf::try_from(ring_buf).unwrap();

let prog: &mut UProbe = bpf
.program_mut("ring_buf_test")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach(
Some("ring_buf_trigger_ebpf_program"),
0,
"/proc/self/exe",
None,
)
.unwrap();

// Generate some random data.
let data: Vec<u64> = {
let mut rng = rand::thread_rng();
use rand::Rng as _;
let n = rng.gen_range(1..100);
std::iter::repeat(()).take(n).map(|_| rng.gen()).collect()
};
// Call the function that the uprobe is attached to with randomly generated data.
for val in &data {
ring_buf_trigger_ebpf_program(*val);
}
// Read the data back out of the ring buffer.
let mut seen = Vec::<u64>::new();
while seen.len() < data.len() {
if let Some(data) = ring_buf.next() {
let data: [u8; 8] = (*data).try_into().unwrap();
let arg = u64::from_ne_bytes(data);
seen.push(arg);
}
}
// Ensure that the data that was read matches what was passed.
assert!(seen == data);
}

#[no_mangle]
#[inline(never)]
pub extern "C" fn ring_buf_trigger_ebpf_program(_arg: u64) {}

0 comments on commit b797e81

Please sign in to comment.