-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integration-test: add a RingBuf test
This simple test exercises sending data from a uprobe.
- Loading branch information
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |