guard/tests/xdp_percpu_sum.rs
loki5512344 8693551499
feat(xdp): ABI layout tests, canonical CIDR (mapped v4 folding), per-CPU sum helper
- tests/xdp_abi_layout.rs: size/align/offset of every Rust mirror of a BPF struct
- xdp::canonical_cidr folds ::ffff:a.b.c.d/(96+n) into the IPv4 trie as a.b.c.d/n
  for ban_cidr and whitelist_add_cidr; whitelist_remove folds mapped addresses
- Blacklist prefix table folds mapped prefixes the same way; checked_shl masks
- sum_percpu_u64 factored out of get_stats (no expect, wrapping sum)
- tests for /0, >width, v6 key byte order, per-CPU sums, manager CIDR rejection
2026-11-21 20:15:34 +01:00

32 lines
821 B
Rust

//! Per-CPU counter aggregation: libbpf returns one buffer per possible CPU.
use rampart::xdp::sum_percpu_u64;
fn slot(v: u64) -> Vec<u8> {
v.to_le_bytes().to_vec()
}
#[test]
fn sums_every_slot_regardless_of_cpu_count() {
for cpus in [1u64, 2, 7, 64, 512] {
let slots: Vec<Vec<u8>> = (1..=cpus).map(slot).collect();
assert_eq!(sum_percpu_u64(&slots), cpus * (cpus + 1) / 2, "{cpus} CPUs");
}
}
#[test]
fn offline_but_possible_cpus_contribute_zero() {
let slots = vec![slot(5), slot(0), slot(0), slot(7)];
assert_eq!(sum_percpu_u64(&slots), 12);
}
#[test]
fn empty_and_short_buffers_are_harmless() {
assert_eq!(sum_percpu_u64(&[]), 0);
assert_eq!(sum_percpu_u64(&[vec![1, 2, 3]]), 0);
}
#[test]
fn large_counters_wrap_instead_of_panicking() {
assert_eq!(sum_percpu_u64(&[slot(u64::MAX), slot(2)]), 1);
}