|
| 1 | +// aya is Linux-only. |
| 2 | +#![cfg(target_os = "linux")] |
| 3 | + |
| 4 | +//! Integration test: load the fast-path BPF ELF, create a veth pair, |
| 5 | +//! attach to one end (native XDP, falling back to generic — veth |
| 6 | +//! supports native on modern kernels), detach, and clean up. |
| 7 | +//! |
| 8 | +//! This is the lightweight version of a full netns routing test — the |
| 9 | +//! point is to exercise the aya attach/detach round-trip against a real |
| 10 | +//! ifindex, which `bpf_prog_test_run` can't do. Full end-to-end |
| 11 | +//! forwarding (packet in → redirect → packet out) lands in a later PR |
| 12 | +//! with a netns harness and synthetic traffic. |
| 13 | +//! |
| 14 | +//! Requires CAP_NET_ADMIN + CAP_BPF; CI runs this under `sudo`. |
| 15 | +
|
| 16 | +use std::process::Command; |
| 17 | + |
| 18 | +use packetframe_fast_path::{FAST_PATH_BPF, FAST_PATH_BPF_AVAILABLE}; |
| 19 | + |
| 20 | +const PEER_A: &str = "pf-veth0"; |
| 21 | +const PEER_B: &str = "pf-veth1"; |
| 22 | + |
| 23 | +struct Cleanup; |
| 24 | + |
| 25 | +impl Drop for Cleanup { |
| 26 | + fn drop(&mut self) { |
| 27 | + // `ip link del <peer>` removes the whole pair. |
| 28 | + let _ = Command::new("ip").args(["link", "del", PEER_A]).status(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +fn run(cmd: &[&str]) { |
| 33 | + let status = Command::new(cmd[0]) |
| 34 | + .args(&cmd[1..]) |
| 35 | + .status() |
| 36 | + .unwrap_or_else(|e| panic!("spawning `{}`: {e}", cmd.join(" "))); |
| 37 | + assert!(status.success(), "`{}` failed: {status}", cmd.join(" ")); |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +#[ignore = "needs CAP_NET_ADMIN + BPF build; run via `sudo -E cargo test ... -- --ignored`"] |
| 42 | +fn attach_detach_roundtrip_on_veth() { |
| 43 | + if !FAST_PATH_BPF_AVAILABLE { |
| 44 | + eprintln!("BPF stub in effect (no rustup); skipping attach test."); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Clean any leftover from a prior aborted run. Idempotent. |
| 49 | + let _ = Command::new("ip").args(["link", "del", PEER_A]).status(); |
| 50 | + |
| 51 | + run(&[ |
| 52 | + "ip", "link", "add", PEER_A, "type", "veth", "peer", "name", PEER_B, |
| 53 | + ]); |
| 54 | + run(&["ip", "link", "set", PEER_A, "up"]); |
| 55 | + run(&["ip", "link", "set", PEER_B, "up"]); |
| 56 | + |
| 57 | + // Ensure cleanup even on panic. |
| 58 | + let _cleanup = Cleanup; |
| 59 | + |
| 60 | + let ifindex = { |
| 61 | + let c = std::ffi::CString::new(PEER_A).unwrap(); |
| 62 | + let idx = unsafe { libc::if_nametoindex(c.as_ptr()) }; |
| 63 | + assert!(idx > 0, "if_nametoindex({PEER_A}) failed"); |
| 64 | + idx |
| 65 | + }; |
| 66 | + |
| 67 | + let mut bpf = aya::Ebpf::load(FAST_PATH_BPF).expect("aya::Ebpf::load"); |
| 68 | + let prog: &mut aya::programs::Xdp = bpf |
| 69 | + .program_mut("fast_path") |
| 70 | + .expect("fast_path program present") |
| 71 | + .try_into() |
| 72 | + .expect("fast_path is XDP"); |
| 73 | + prog.load().expect("verifier"); |
| 74 | + |
| 75 | + // Try native first; fall back to generic. veth *should* support |
| 76 | + // native XDP on any remotely modern kernel, but we don't gate on it. |
| 77 | + use aya::programs::xdp::XdpFlags; |
| 78 | + let link_id = prog |
| 79 | + .attach_to_if_index(ifindex, XdpFlags::DRV_MODE) |
| 80 | + .or_else(|native_err| { |
| 81 | + eprintln!("veth native XDP attach failed ({native_err}); trying generic"); |
| 82 | + prog.attach_to_if_index(ifindex, XdpFlags::SKB_MODE) |
| 83 | + }) |
| 84 | + .expect("attach XDP to veth"); |
| 85 | + |
| 86 | + prog.detach(link_id).expect("detach"); |
| 87 | +} |
0 commit comments