Crate netem_trace

Source
Expand description

This crate provides a set of tools to generate traces for network emulation.

§Examples

If you want to use the pre-defined models, please enable the model or bw-model feature.

And if you want read configuration from file, serde feature should also be enabled. We else recommend you to enable human feature to make the configuration file more human-readable.

An example to build model from configuration:

let mut static_bw = StaticBwConfig::new()
    .bw(Bandwidth::from_mbps(24))
    .duration(Duration::from_secs(1))
    .build();
assert_eq!(static_bw.next_bw(), Some((Bandwidth::from_mbps(24), Duration::from_secs(1))));
assert_eq!(static_bw.next_bw(), None);

A more common use case is to build model from a configuration file (e.g. json file):

// The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":{\"secs\":1,\"nanos\":0}}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":{\"secs\":1,\"nanos\":0}}}],\"count\":2}}"
// if the `human` feature is not enabled.
let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":{\"secs\":1,\"nanos\":0}}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":{\"secs\":1,\"nanos\":0}}}],\"count\":2}}";
let des: Box<dyn BwTraceConfig> = serde_json::from_str(config_file_content).unwrap();
let mut model = des.into_model();
assert_eq!(
    model.next_bw(),
    Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
);
assert_eq!(
    model.next_bw(),
    Some((Bandwidth::from_mbps(24), Duration::from_secs(1)))
);
assert_eq!(
    model.next_bw(),
    Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
);
assert_eq!(
    model.next_bw(),
    Some((Bandwidth::from_mbps(24), Duration::from_secs(1)))
);
assert_eq!(model.next_bw(), None);

§Make your own model

Here is an simple example of how to do this. For more complicated examples, please refer to our pre-defined models.

use netem_trace::BwTrace;
use netem_trace::{Bandwidth, Duration};

struct MyStaticBw {
   bw: Bandwidth,
   duration: Option<Duration>,
}

impl BwTrace for MyStaticBw {
    fn next_bw(&mut self) -> Option<(Bandwidth, Duration)> {
        if let Some(duration) = self.duration.take() {
            if duration.is_zero() {
                None
            } else {
                Some((self.bw, duration))
            }
        } else {
            None
        }
    }
}

This is almost the same as how this library implements the model::StaticBw model.

§Features

§Model Features

  • model: Enable this feature if you want to use all pre-defined models.
    • bw-model: Enable this feature if you want to use the pre-defined BwTrace models.
    • truncated-normal: Enable this feature if you want to use truncated normal distribution in model::NormalizedBw models.

§Trace Format Features

  • mahimahi: Enable this feature if you want to load or output traces in mahimahi format.

§Other Features

  • serde: Enable this features if you want some structs to be serializable/deserializable. Often used with model features.
  • human: Enable this feature if you want to use human-readable format in configuration files. Often used with model features.

Re-exports§

pub use mahimahi::load_mahimahi_trace;
pub use mahimahi::Mahimahi;
pub use mahimahi::MahimahiExt;

Modules§

mahimahi
This module can generate traces in mahimahi format for struct implementing BwTrace and load traces in mahimahi format to RepeatedBwPatternConfig.
model
This module contains pre-defined models for BwTrace, DelayTrace, LossTrace and DuplicateTrace.

Macros§

impl_forever
Implement the Forever trait for the bandwidth trace model config (any struct implements BwTraceConfig).
impl_forever_delay_per_packet
Implement the Forever trait for the per-packet delay trace model config (any struct implements DelayPerPacketTraceConfig).

Structs§

Bandwidth
A Bandwidth type to represent a link’s bandwidth(to describe how many bits can be sent on the link per second), typically used for network.
Duration
A Duration type to represent a span of time, typically used for system timeouts.

Traits§

BwTrace
This is a trait that represents a trace of bandwidths.
DelayPerPacketTrace
This is a trait that represents a trace of per-packet delays.
DelayTrace
This is a trait that represents a trace of delays.
DuplicateTrace
This is a trait that represents a trace of duplicate patterns.
LossTrace
This is a trait that represents a trace of loss patterns.

Type Aliases§

Delay
The delay describes how long a packet is delayed when going through.
DuplicatePattern
The duplicate_pattern describes how the packets are duplicated.
LossPattern
The loss_pattern describes how the packets are dropped when going through.