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 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):

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}}";
// The content would be "{\"RepeatedBwPatternConfig\":{\"pattern\":[{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":12000000},\"duration\":\"1s\"}},{\"StaticBwConfig\":{\"bw\":{\"gbps\":0,\"bps\":24000000},\"duration\":\"1s\"}}],\"count\":2}}"
// if the `human` feature is enabled.
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.

§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§

Modules§

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

Macros§

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 Aliases§

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