Skip to content

Commit

Permalink
add filter for packet counting, release 0.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuhn committed Jan 21, 2024
1 parent 0d5b9b5 commit 040bef5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ members = ["cli", "core", "machine", "macros", "nat", "router", "."]

[package]
name = "netsim-embed"
version = "0.9.0"
authors = ["David Craven <[email protected]>"]
version = "0.9.1"
authors = ["David Craven <[email protected]>", "Roland Kuhn <[email protected]>"]
edition = "2018"
description = "Network simulator."
license = "MIT"
Expand Down Expand Up @@ -32,7 +32,7 @@ netsim-embed-core = { version = "0.4.3", path = "core" }
netsim-embed-machine = { version = "0.6.4", path = "machine" }
netsim-embed-macros = { version = "0.2.0", path = "macros", optional = true }
netsim-embed-nat = { version = "0.4.2", path = "nat" }
netsim-embed-router = { version = "0.4.6", path = "router" }
netsim-embed-router = { version = "0.4.7", path = "router" }
serde = { version = "1.0.158", optional = true }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netsim-embed-router"
version = "0.4.6"
version = "0.4.7"
authors = ["David Craven <[email protected]>"]
edition = "2018"
description = "Implementation of a router for netsim embed."
Expand Down
41 changes: 34 additions & 7 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
net::Ipv4Addr,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
Arc, Mutex,
},
task::Poll,
};
Expand All @@ -31,14 +31,28 @@ pub struct Ipv4Router {
counters: Arc<Counters>,
}

#[derive(Debug, Default)]
pub type Filter = Box<dyn Fn(&[u8]) -> bool + Send + Sync + 'static>;

#[derive(Default)]
struct Counters {
filter: Mutex<Option<Filter>>,
forwarded: AtomicUsize,
invalid: AtomicUsize,
disabled: AtomicUsize,
unroutable: AtomicUsize,
}

impl std::fmt::Debug for Counters {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Counters")
.field("forwarded", &self.forwarded)
.field("invalid", &self.invalid)
.field("disabled", &self.disabled)
.field("unroutable", &self.unroutable)
.finish()
}
}

impl Ipv4Router {
pub fn new(addr: Ipv4Addr) -> Self {
let (tx, rx) = mpsc::unbounded();
Expand Down Expand Up @@ -67,6 +81,10 @@ impl Ipv4Router {
self.counters.unroutable.load(Ordering::Relaxed)
}

pub fn set_filter(&self, filter: Option<Filter>) {
*self.counters.filter.lock().unwrap() = filter;
}

pub fn add_connection(&self, id: usize, plug: Plug, routes: Vec<Ipv4Route>) {
self.ctrl
.unbounded_send(RouterCtrl::AddRoute(id, plug, routes))
Expand Down Expand Up @@ -153,10 +171,13 @@ fn forward_packet(
conns: &mut [(usize, Plug, Vec<Ipv4Route>, bool)],
bytes: Vec<u8>,
) {
let count = counters.filter.lock().unwrap().iter().all(|f| f(&bytes));
let packet = if let Some(packet) = Ipv4Packet::new(&bytes) {
packet
} else {
counters.invalid.fetch_add(1, Ordering::Relaxed);
if count {
counters.invalid.fetch_add(1, Ordering::Relaxed);
}
log::info!("router {}: dropping invalid ipv4 packet", addr);
return;
};
Expand All @@ -170,11 +191,15 @@ fn forward_packet(
for route in routes {
if route.dest().contains(dest) || dest.is_broadcast() || dest.is_multicast() {
if !*en {
counters.disabled.fetch_add(1, Ordering::Relaxed);
if count {
counters.disabled.fetch_add(1, Ordering::Relaxed);
}
log::trace!("router {}: route {:?} disabled", addr, route);
} else {
counters.forwarded.fetch_add(1, Ordering::Relaxed);
log::trace!("router {}: routing packet on route {:?}", addr, route);
if count {
counters.forwarded.fetch_add(1, Ordering::Relaxed);
}
log::trace!("router {}: routing packet on route {:?}", addr, route,);
tx.unbounded_send(bytes.clone());
forwarded = true;
}
Expand All @@ -183,7 +208,9 @@ fn forward_packet(
}
if !forwarded {
let src = packet.get_source();
counters.unroutable.fetch_add(1, Ordering::Relaxed);
if count {
counters.unroutable.fetch_add(1, Ordering::Relaxed);
}
log::debug!(
"router {}: dropping unroutable packet from {} to {}",
addr,
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use netsim_embed_core::*;
pub use netsim_embed_core::{DelayBuffer, Ipv4Range, Protocol};
pub use netsim_embed_machine::{unshare_user, Machine, MachineId, Namespace};
use netsim_embed_nat::*;
pub use netsim_embed_router::Filter;
use netsim_embed_router::*;
use std::fmt::Display;
use std::net::{Ipv4Addr, SocketAddrV4};
Expand Down Expand Up @@ -239,6 +240,10 @@ impl Network {
self.range
}

pub fn set_count_filter(&self, filter: Option<Filter>) {
self.router.set_filter(filter);
}

pub fn num_forwarded(&self) -> usize {
self.router.forwarded()
}
Expand Down

0 comments on commit 040bef5

Please sign in to comment.