summaryrefslogtreecommitdiffstats
path: root/third_party/rust/neqo-transport/tests/sim/mod.rs
blob: 9ab9d57a4a4a1a746a5f4b1c097878e3ba0b12dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Tests with simulated network
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![warn(clippy::pedantic)]

pub mod connection;
mod delay;
mod drop;
pub mod rng;
mod taildrop;

use std::{
    cell::RefCell,
    cmp::min,
    convert::TryFrom,
    fmt::Debug,
    rc::Rc,
    time::{Duration, Instant},
};

use neqo_common::{qdebug, qinfo, qtrace, Datagram, Encoder};
use neqo_transport::Output;
use rng::Random;
use test_fixture::{self, now};
use NodeState::{Active, Idle, Waiting};

pub mod network {
    pub use super::{delay::Delay, drop::Drop, taildrop::TailDrop};
}

type Rng = Rc<RefCell<Random>>;

/// A macro that turns a list of values into boxed versions of the same.
#[macro_export]
macro_rules! boxed {
    [$($v:expr),+ $(,)?] => {
        vec![ $( Box::new($v) as _ ),+ ]
    };
}

/// Create a simulation test case.  This takes either two or three arguments.
/// The two argument form takes a bare name (`ident`), a comma, and an array of
/// items that implement `Node`.
/// The three argument form adds a setup block that can be used to construct a
/// complex value that is then shared between all nodes.  The values in the
/// three-argument form have to be closures (or functions) that accept a reference
/// to the value returned by the setup.
#[macro_export]
macro_rules! simulate {
    ($n:ident, [ $($v:expr),+ $(,)? ] $(,)?) => {
        simulate!($n, (), [ $(|_| $v),+ ]);
    };
    ($n:ident, $setup:expr, [ $( $v:expr ),+ $(,)? ] $(,)?) => {
        #[test]
        fn $n() {
            let fixture = $setup;
            let mut nodes: Vec<Box<dyn $crate::sim::Node>> = Vec::new();
            $(
                let f: Box<dyn FnOnce(&_) -> _> = Box::new($v);
                nodes.push(Box::new(f(&fixture)));
            )*
            let mut sim = Simulator::new(stringify!($n), nodes);
            if let Ok(seed) = std::env::var("SIMULATION_SEED") {
                sim.seed_str(seed);
            }
            sim.run();
        }
    };
}

pub trait Node: Debug {
    fn init(&mut self, _rng: Rng, _now: Instant) {}
    /// Perform processing.  This optionally takes a datagram and produces either
    /// another data, a time that the simulator needs to wait, or nothing.
    fn process(&mut self, d: Option<Datagram>, now: Instant) -> Output;
    /// An node can report when it considers itself "done".
    fn done(&self) -> bool {
        true
    }
    fn print_summary(&self, _test_name: &str) {}
}

/// The state of a single node.  Nodes will be activated if they are `Active`
/// or if the previous node in the loop generated a datagram.  Nodes that return
/// `true` from `Node::done` will be activated as normal.
#[derive(Debug, PartialEq)]
enum NodeState {
    /// The node just produced a datagram.  It should be activated again as soon as possible.
    Active,
    /// The node is waiting.
    Waiting(Instant),
    /// The node became idle.
    Idle,
}

#[derive(Debug)]
struct NodeHolder {
    node: Box<dyn Node>,
    state: NodeState,
}

impl NodeHolder {
    fn ready(&self, now: Instant) -> bool {
        match self.state {
            Active => true,
            Waiting(t) => t >= now,
            Idle => false,
        }
    }
}

pub struct Simulator {
    name: String,
    nodes: Vec<NodeHolder>,
    rng: Rng,
}

impl Simulator {
    pub fn new(name: impl AsRef<str>, nodes: impl IntoIterator<Item = Box<dyn Node>>) -> Self {
        let name = String::from(name.as_ref());
        // The first node is marked as Active, the rest are idle.
        let mut it = nodes.into_iter();
        let nodes = it
            .next()
            .map(|node| NodeHolder {
                node,
                state: Active,
            })
            .into_iter()
            .chain(it.map(|node| NodeHolder { node, state: Idle }))
            .collect::<Vec<_>>();
        Self {
            name,
            nodes,
            rng: Rc::default(),
        }
    }

    pub fn seed(&mut self, seed: [u8; 32]) {
        self.rng = Rc::new(RefCell::new(Random::new(seed)));
    }

    /// Seed from a hex string.
    /// Though this is convenient, it panics if this isn't a 64 character hex string.
    pub fn seed_str(&mut self, seed: impl AsRef<str>) {
        let seed = Encoder::from_hex(seed);
        self.seed(<[u8; 32]>::try_from(seed.as_ref()).unwrap());
    }

    fn next_time(&self, now: Instant) -> Instant {
        let mut next = None;
        for n in &self.nodes {
            match n.state {
                Idle => continue,
                Active => return now,
                Waiting(a) => next = Some(next.map_or(a, |b| min(a, b))),
            }
        }
        next.expect("a node cannot be idle and not done")
    }

    /// Runs the simulation.
    pub fn run(mut self) -> Duration {
        let start = now();
        let mut now = start;
        let mut dgram = None;

        for n in &mut self.nodes {
            n.node.init(self.rng.clone(), now);
        }
        println!("{}: seed {}", self.name, self.rng.borrow().seed_str());

        let real_start = Instant::now();
        loop {
            for n in &mut self.nodes {
                if dgram.is_none() && !n.ready(now) {
                    qdebug!([self.name], "skipping {:?}", n.node);
                    continue;
                }

                qdebug!([self.name], "processing {:?}", n.node);
                let res = n.node.process(dgram.take(), now);
                n.state = match res {
                    Output::Datagram(d) => {
                        qtrace!([self.name], " => datagram {}", d.len());
                        dgram = Some(d);
                        Active
                    }
                    Output::Callback(delay) => {
                        qtrace!([self.name], " => callback {:?}", delay);
                        assert_ne!(delay, Duration::new(0, 0));
                        Waiting(now + delay)
                    }
                    Output::None => {
                        qtrace!([self.name], " => nothing");
                        assert!(n.node.done(), "nodes have to be done when they go idle");
                        Idle
                    }
                };
            }

            if self.nodes.iter().all(|n| n.node.done()) {
                let real_elapsed = real_start.elapsed();
                println!("{}: real elapsed time: {:?}", self.name, real_elapsed);
                let elapsed = now - start;
                println!("{}: simulated elapsed time: {:?}", self.name, elapsed);
                for n in &self.nodes {
                    n.node.print_summary(&self.name);
                }
                return elapsed;
            }

            if dgram.is_none() {
                let next = self.next_time(now);
                if next > now {
                    qinfo!(
                        [self.name],
                        "advancing time by {:?} to {:?}",
                        next - now,
                        next - start
                    );
                    now = next;
                }
            }
        }
    }
}