Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 21 additions & 77 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ anyhow = { version = "1.0.100" }
aya = { version = "0.16.0", default-features = false }
aya-build = { version = "0.2.1", default-features = false }
aya-ebpf = { version = "0.3.0", default-features = false }
aya-log = { version = "0.4.0", default-features = false }
aya-log-ebpf = { version = "0.3.0", default-features = false }
cargo_metadata = { version = "0.23.1", default-features = false }
log = { version = "0.4.28", default-features = false }
which = { version = "8.0.0", features=["real-sys"], default-features = false }
Expand Down
1 change: 0 additions & 1 deletion snoopy-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition.workspace = true

[dependencies]
aya-ebpf = { workspace = true }
aya-log-ebpf = { workspace = true }

[build-dependencies]
which = { workspace = true }
Expand Down
119 changes: 20 additions & 99 deletions snoopy-ebpf/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#![no_std]
#![no_main]

use aya_ebpf::bindings::xdp_action;
use aya_ebpf::macros::map;
use aya_ebpf::maps::PerCpuArray;
use aya_ebpf::programs::TcContext;
use aya_ebpf::programs::XdpContext;
use aya_log_ebpf as log;

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
Expand All @@ -21,113 +18,37 @@ static INGRESS_COUNTER: PerCpuArray<Counter> = PerCpuArray::<Counter>::with_max_
#[map(name = "EGRESS_COUNTER")]
static EGRESS_COUNTER: PerCpuArray<Counter> = PerCpuArray::<Counter>::with_max_entries(1, 0);

// Both directions are counted with TC classifiers only. XDP was tried first
// for ingress before, but attaching an XDP program to a veth clears TSO on
// its peer (`tx-tcp-segmentation: off [fixed]`), so the host had to segment
// every packet sent into the pod, and on `lo` it ran as generic XDP on every
// loopback packet. A classifier sees the same bytes without either cost.
//
// There is no aya-log here: the only failure path is a lookup of index 0 in
// a 1-entry array, which cannot fail, and each loaded object paid a 128 KiB
// AYA_LOGS ring buffer for it.

#[aya_ebpf::macros::classifier]
pub fn update_tc_ingress(ctx: TcContext) -> i32 {
match try_update_tc_ingress(&ctx) {
Ok(_) => {
// log::debug!(
// &ctx,
// "TC INGRESS: added packet with {} bytes",
// ctx.data_end() - ctx.data()
// );
}
Err(msg) => {
log::error!(&ctx, "TC INGRESS: failed to add packet: {}", msg);
}
}

count(&INGRESS_COUNTER, ctx.len() as u64);
aya_ebpf::bindings::TC_ACT_PIPE
}

#[inline]
pub fn try_update_tc_ingress(ctx: &TcContext) -> Result<(), &'static str> {
let bytes = ctx.len() as u64;

let counter = INGRESS_COUNTER
.get_ptr_mut(0)
.ok_or("failed to get pointer to first entry in INGRESS_COUNTER map")?;

unsafe {
(*counter).packets = (*counter).packets.wrapping_add(1);
(*counter).bytes = (*counter).bytes.wrapping_add(bytes);
}

Ok(())
}

#[aya_ebpf::macros::xdp]
pub fn update_xdp_ingress(ctx: XdpContext) -> u32 {
match try_update_xdp_ingress(&ctx) {
Ok(_) => {
// log::debug!(
// &ctx,
// "XDP INGRESS: added packet with {} bytes",
// ctx.data_end() - ctx.data()
// );
}
Err(msg) => {
log::error!(&ctx, "XDP INGRESS: failed to add packet: {}", msg);
}
}

xdp_action::XDP_PASS
}

#[inline]
fn try_update_xdp_ingress(ctx: &XdpContext) -> Result<(), &'static str> {
// On-wire bytes only — XDP metadata (bpf_xdp_adjust_meta region) is driver/HW
// scratch space, not part of the packet, so omit it to match the TC variant
// which uses ctx.len().
let bytes = ctx.data_end() as u64 - ctx.data() as u64;

let counter = INGRESS_COUNTER
.get_ptr_mut(0)
.ok_or("failed to get pointer to first entry in INGRESS_COUNTER map")?;

unsafe {
(*counter).packets = (*counter).packets.wrapping_add(1);
(*counter).bytes = (*counter).bytes.wrapping_add(bytes);

// info!(ctx, "XDP INGRESS: Packets: {}, Bytes: {}", (*counter).packets, (*counter).bytes);
}

Ok(())
}

#[aya_ebpf::macros::classifier]
pub fn update_tc_egress(ctx: TcContext) -> i32 {
match try_update_tc_egress(&ctx) {
Ok(_) => {
// log::debug!(
// &ctx,
// "TC EGRESS: added packet with {} bytes",
// ctx.data_end() - ctx.data()
// );
}
Err(msg) => {
log::error!(&ctx, "TC EGRESS: failed to add packet: {}", msg);
}
};

count(&EGRESS_COUNTER, ctx.len() as u64);
aya_ebpf::bindings::TC_ACT_PIPE
}

#[inline]
pub fn try_update_tc_egress(ctx: &TcContext) -> Result<(), &'static str> {
let data = ctx.data() as u64;
let data_end = ctx.data_end() as u64;
let bytes = data_end - data;

let counter = EGRESS_COUNTER
.get_ptr_mut(0)
.ok_or("failed to get pointer to first entry in EGRESS_COUNTER map")?;

unsafe {
(*counter).packets = (*counter).packets.wrapping_add(1);
(*counter).bytes = (*counter).bytes.wrapping_add(bytes);
#[inline(always)]
fn count(map: &PerCpuArray<Counter>, bytes: u64) {
if let Some(counter) = map.get_ptr_mut(0) {
// SAFETY: per-CPU map slot; programs on one CPU never run concurrently.
unsafe {
(*counter).packets = (*counter).packets.wrapping_add(1);
(*counter).bytes = (*counter).bytes.wrapping_add(bytes);
}
}

Ok(())
}

#[cfg(not(test))]
Expand Down
3 changes: 1 addition & 2 deletions snoopy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition.workspace = true
[dependencies]
anyhow = { workspace = true }
aya = { workspace = true }
aya-log = { workspace = true }
clap = { version = "4.5.53", features = ["derive"] }
env_logger = { version = "0.11.8", default-features = false }
libc = { version = "0.2.177", default-features = false }
Expand All @@ -15,7 +14,7 @@ pnet = { version = "0.35.0", features = ["serde"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
serde_with = "3.16.0"
tokio = { version = "1.48.0", default-features = false, features = ["macros", "rt", "time", "sync", "net"] }
tokio = { version = "1.48.0", default-features = false, features = ["macros", "rt", "time", "sync", "net", "signal"] }

[build-dependencies]
anyhow = { workspace = true }
Expand Down
Loading
Loading