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.

An example to build model from configuration:

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

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

let config_file_content = "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"FixedBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":{\"secs\":1,\"nanos\":0}}},{\"FixedBwConfig\":{\"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 MyFixedBw {
   bw: Bandwidth,
   duration: Option<Duration>,
}

impl BwTrace for MyFixedBw {
    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 [FixedBw] 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.

Trace Format Features

  • mahimahi: Enable this feature if you want to 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.

Re-exports

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

Modules

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

Structs

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.
A Duration type to represent a span of time, typically used for system timeouts.

Traits

This is a trait that represents a trace of bandwidths.
This is a trait that represents a trace of delays.
This is a trait that represents a trace of loss patterns.

Type Definitions

The delay describes how long a packet is delayed when going through.
The loss_pattern describes how the packets are dropped when going through.