Skip to content

Commit

Permalink
feat(sim): docs, more scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Bostoen committed Jan 7, 2024
1 parent 488ad32 commit da30b36
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions msg-sim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# `msg-sim`

## Overview
This crate provides functionality to simulate real-world network conditions over an interface (e.g. `lo0`) for testing and benchmarking purposes.
It only works on MacOS and Linux.

## Implementation

### MacOS
* https://gist.github.com/tellyworth/2ce28add99fe743c702c090c8144355e

* `dnctl` for creating a dummynet pipe

Example:
```bash
dnctl pipe 1 config bw 10Kbit/s delay 300 plr 0.1 noerror`
```

* `pfctl` for creating a rule to match traffic and send it through the pipe

Example:
```bash
echo "dummynet out proto tcp from any to any pipe 1" | sudo pfctl -f -
pfctl -e
```
Empty file added msg-sim/src/cmd/linux.rs
Empty file.
4 changes: 4 additions & 0 deletions msg-sim/src/cmd/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! This module contains all the commands necessary to start and stop a simulation.
//!
//! ## Implementation
//! Under the hood, this module uses the `pfctl` command.
5 changes: 5 additions & 0 deletions msg-sim/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[cfg(target_os = "linux")]
pub mod linux;

#[cfg(target_os = "macos")]
pub mod macos;
7 changes: 7 additions & 0 deletions msg-sim/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::{collections::HashMap, time::Duration};

use protocol::Protocol;

mod cmd;
mod protocol;

/// A type alias for a network device.
pub type Device = String;

Expand All @@ -9,8 +14,10 @@ pub struct SimConfig {
target_bw: u64,
default_bw: u64,
packet_loss: f64,
protocols: Vec<Protocol>,
}

#[derive(Default)]
pub struct Simulator {
/// A map of active simulations.
active_sims: HashMap<Device, Simulation>,
Expand Down
18 changes: 18 additions & 0 deletions msg-sim/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[derive(Debug, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
pub enum Protocol {
TCP,
UDP,
ICMP,
}

impl From<&str> for Protocol {
fn from(s: &str) -> Self {
match s {
"tcp" => Self::TCP,
"udp" => Self::UDP,
"icmp" => Self::ICMP,
_ => panic!("invalid protocol"),
}
}
}

0 comments on commit da30b36

Please sign in to comment.