Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DuplicateDevice (#33) #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ctrlc = "3.2.5"
figment = { version = "0.10", features = ["toml", "env", "json"] }
futures = "0.3.29"
human-bandwidth = { version = "0.1.1", features = ["serde"] }
netem-trace = { version = "0.3.3", features = ["serde", "human", "mahimahi"] }
netem-trace = { version = "0.3.4", features = ["serde", "human", "mahimahi"] }
nix = "0.26.2"
once_cell = "1.19"
rand = "0.8.5"
Expand Down
75 changes: 75 additions & 0 deletions rattan-core/src/config/duplicate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use figment::{
providers::{Format, Json, Toml},
Figment,
};
use netem_trace::{model::DuplicateTraceConfig, DuplicatePattern, DuplicateTrace};
use rand::{rngs::StdRng, SeedableRng};
use serde::{Deserialize, Serialize};

use crate::{
core::DeviceFactory,
devices::{duplicate, Packet},
error::Error,
};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DuplicateDeviceBuildConfig {
pub pattern: DuplicatePattern,
pub seed: Option<u64>,
}

impl DuplicateDeviceBuildConfig {
pub fn into_factory<P: Packet>(
self,
) -> impl DeviceFactory<duplicate::DuplicateDevice<P, StdRng>> {
move |handle| {
let _guard = handle.enter();
let rng = StdRng::seed_from_u64(self.seed.unwrap_or(42));
duplicate::DuplicateDevice::new(self.pattern, rng)
}
}
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DuplicateReplayDeviceBuildConfig {
pub trace: String,
pub seed: Option<u64>,
}

impl DuplicateReplayDeviceBuildConfig {
fn get_trace(&self) -> Result<Box<dyn DuplicateTrace>, Error> {
let file_path = std::path::Path::new(&self.trace);
if let Some(ext) = file_path.extension() {
if ext == "json" {
let trace: Box<dyn DuplicateTraceConfig> = Figment::new()
.merge(Json::file(file_path))
.extract()
.map_err(|e| Error::ConfigError(e.to_string()))?;
return Ok(trace.into_model());
} else if ext == "toml" {
let trace: Box<dyn DuplicateTraceConfig> = Figment::new()
.merge(Toml::file(file_path))
.extract()
.map_err(|e| Error::ConfigError(e.to_string()))?;
return Ok(trace.into_model());
}
}
Err(Error::ConfigError(format!(
"Unknown trace file format: {:?}",
file_path
)))
}

pub fn into_factory<P: Packet>(
self,
) -> impl DeviceFactory<duplicate::DuplicateReplayDevice<P, StdRng>> {
move |handle| {
let _guard = handle.enter();
let trace = self.get_trace()?;
let rng = StdRng::seed_from_u64(self.seed.unwrap_or(42));
duplicate::DuplicateReplayDevice::new(trace, rng)
}
}
}
4 changes: 4 additions & 0 deletions rattan-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use crate::control::http::HttpConfig;

mod bandwidth;
mod delay;
mod duplicate;
mod loss;

pub use bandwidth::*;
pub use delay::*;
pub use duplicate::*;
pub use loss::*;

/// Configuration for the whole Rattan system.
Expand Down Expand Up @@ -74,5 +76,7 @@ pub enum DeviceBuildConfig<P: Packet> {
DelayReplay(DelayReplayDeviceBuildConfig),
Loss(LossDeviceBuildConfig),
LossReplay(LossReplayDeviceBuildConfig),
Duplicate(DuplicateDeviceBuildConfig),
DuplicateReplay(DuplicateReplayDeviceBuildConfig),
Custom,
}
Loading