Skip to content

Commit

Permalink
add basic tcx integration tests
Browse files Browse the repository at this point in the history
Add basic tcx integration tests to ensure the new
Mprog API is working as expected.

Signed-off-by: astoycos <astoycos@gmail.com>
  • Loading branch information
astoycos committed Aug 23, 2024
1 parent 5f99557 commit 05c2853
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ tokio = { version = "1.24.0", default-features = false }
which = { version = "6.0.0", default-features = false }
xdpilone = { version = "1.0.5", default-features = false }
xtask = { path = "xtask", default-features = false }
chrono = { version = "0.4" }

[profile.dev]
panic = "abort"
Expand Down
4 changes: 4 additions & 0 deletions test/integration-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ path = "src/pass.rs"
name = "test"
path = "src/test.rs"

[[bin]]
name = "tcx"
path = "src/tcx.rs"

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

use core::mem;

use aya_ebpf::{
bindings::tcx_action_base::{TCX_NEXT, TCX_PASS},
macros::classifier,
programs::TcContext,
};
use aya_log_ebpf::info;
use network_types::{
eth::{EthHdr, EtherType},
ip::{IpProto, Ipv4Hdr},
udp::UdpHdr,
};

#[no_mangle]
static ORDER: i32 = 0;

// Gives us raw pointers to a specific offset in the packet
#[inline(always)]
unsafe fn ptr_at<T>(ctx: &TcContext, offset: usize) -> Result<*mut T, i64> {
let start = ctx.data();
let end = ctx.data_end();
let len = mem::size_of::<T>();

if start + offset + len > end {
return Err(TCX_PASS.into());
}
Ok((start + offset) as *mut T)
}

#[classifier]
pub fn tcx_order(ctx: TcContext) -> i32 {
match try_tcxtest(ctx) {
Ok(ret) => ret,
Err(_ret) => TCX_PASS,
}
}

fn try_tcxtest(ctx: TcContext) -> Result<i32, i64> {
let eth_hdr: *const EthHdr = unsafe { ptr_at(&ctx, 0) }?;
let order = unsafe { core::ptr::read_volatile(&ORDER) };
match unsafe { *eth_hdr }.ether_type {
EtherType::Ipv4 => {
let ipv4_hdr: *const Ipv4Hdr = unsafe { ptr_at(&ctx, EthHdr::LEN)? };
let saddr = u32::from_be(unsafe { (*ipv4_hdr).src_addr });
let daddr = u32::from_be(unsafe { (*ipv4_hdr).dst_addr });
match unsafe { (*ipv4_hdr).proto } {
IpProto::Udp => {
let udphdr: *const UdpHdr =
unsafe { ptr_at(&ctx, EthHdr::LEN + Ipv4Hdr::LEN) }?;
let dport = u16::from_be(unsafe { (*udphdr).dest });
let sport = u16::from_be(unsafe { (*udphdr).source });
info!(
&ctx,
"order: {}, cookie: ({:i}, {:i}, {}, {})",
order,
daddr,
saddr,
dport,
sport
);

Ok(TCX_NEXT)
}
_ => Ok(TCX_PASS),
}
}
_ => Ok(TCX_PASS),
}
}

#[cfg(not(test))]
#[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 @@ -27,6 +27,7 @@ test-case = { workspace = true }
test-log = { workspace = true, features = ["log"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }
xdpilone = { workspace = true }
chrono = { workspace = true }

[build-dependencies]
cargo_metadata = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions test/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub const LOG: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/log"));
pub const MAP_TEST: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/map_test"));
pub const NAME_TEST: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/name_test"));
pub const PASS: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/pass"));
pub const TCX: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/tcx"));
pub const TEST: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/test"));
pub const RELOCATIONS: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/relocations"));
pub const TWO_PROGS: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/two_progs"));
Expand Down
1 change: 1 addition & 0 deletions test/integration-test/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ mod rbpf;
mod relocations;
mod ring_buf;
mod smoke;
mod tcx;
mod xdp;
27 changes: 21 additions & 6 deletions test/integration-test/src/tests/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use aya::{programs::UProbe, Ebpf};
use aya_log::EbpfLogger;
use chrono::{DateTime, Utc};
use log::{Level, Log, Record};
use test_log::test;

Expand All @@ -14,8 +15,8 @@ pub extern "C" fn trigger_ebpf_program() {
core::hint::black_box(trigger_ebpf_program);
}

struct TestingLogger<F> {
log: F,
pub(crate) struct TestingLogger<F> {
pub(crate) log: F,
}

impl<F: Send + Sync + Fn(&Record)> Log for TestingLogger<F> {
Expand All @@ -32,10 +33,11 @@ impl<F: Send + Sync + Fn(&Record)> Log for TestingLogger<F> {
}

#[derive(Debug, PartialEq)]
struct CapturedLog<'a> {
pub body: Cow<'a, str>,
pub level: Level,
pub target: Cow<'a, str>,
pub(crate) struct CapturedLog<'a> {
pub(crate) body: Cow<'a, str>,
pub(crate) level: Level,
pub(crate) target: Cow<'a, str>,
pub(crate) timestamp: Option<DateTime<Utc>>,
}

#[test(tokio::test)]
Expand All @@ -54,6 +56,7 @@ async fn log() {
body: format!("{}", record.args()).into(),
level: record.level(),
target: record.target().to_string().into(),
timestamp: None,
});
},
},
Expand Down Expand Up @@ -91,6 +94,7 @@ async fn log() {
body: "Hello from eBPF!".into(),
level: Level::Debug,
target: "log".into(),
timestamp: None,
}),
);

Expand All @@ -100,6 +104,7 @@ async fn log() {
body: "69, 420, wao, 77616f".into(),
level: Level::Error,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -109,6 +114,7 @@ async fn log() {
body: "ip structs, without format hint: ipv4: 10.0.0.1, ipv6: 2001:db8::1".into(),
level: Level::Info,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -118,6 +124,7 @@ async fn log() {
body: "ip structs, with format hint: ipv4: 10.0.0.1, ipv6: 2001:db8::1".into(),
level: Level::Info,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -127,6 +134,7 @@ async fn log() {
body: "ip enums, without format hint: ipv4: 10.0.0.1, ipv6: 2001:db8::1".into(),
level: Level::Info,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -136,6 +144,7 @@ async fn log() {
body: "ip enums, with format hint: ipv4: 10.0.0.1, ipv6: 2001:db8::1".into(),
level: Level::Info,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -145,6 +154,7 @@ async fn log() {
body: "ip as bits: ipv4: 10.0.0.1".into(),
level: Level::Info,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -154,6 +164,7 @@ async fn log() {
body: "ip as octets: ipv4: 10.0.0.1, ipv6: 2001:db8::1".into(),
level: Level::Info,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -163,6 +174,7 @@ async fn log() {
body: "mac lc: 04:20:06:09:00:40, mac uc: 04:20:06:09:00:40".into(),
level: Level::Trace,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -172,6 +184,7 @@ async fn log() {
body: "hex lc: 2f, hex uc: 2F".into(),
level: Level::Warn,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -181,6 +194,7 @@ async fn log() {
body: "hex lc: deadbeef, hex uc: DEADBEEF".into(),
level: Level::Debug,
target: "log".into(),
timestamp: None,
})
);

Expand All @@ -190,6 +204,7 @@ async fn log() {
body: "42 43 44 45".into(),
level: Level::Debug,
target: "log".into(),
timestamp: None,
})
);

Expand Down
Loading

0 comments on commit 05c2853

Please sign in to comment.