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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Simulates packet loss.
//!
//! The loss layer is a simple wrapper around another layer which simulates a lossy connection.
//! This works by dropping ingress packets or canceling the sending of egress packets.
use crate::nic;
use crate::layer::{eth, ip};
use crate::wire::Payload;

/// Simple pseudo-random loss.
///
/// Can simulate burst-losses and uniform losses by dropping packets based on a pulse design.
#[derive(Copy, Clone, Debug, Hash)]
pub struct PrngLoss {
    /// Threshold for dropping the packet.
    pub threshold: u32,
    /// The packet is never dropped while `count` at least as large as `threshold`.
    pub count: u32,
    /// Reset value for `count` when it reaches `0`.
    pub reset: u32,
    /// Loss rate as a (0, 32)-bit fixed point number.
    ///
    /// Or `None` for no loss at all, which can be used to temporarily turn loss off.
    pub lossrate: Option<u32>,
    /// The current prng state (or seed at the start).
    ///
    /// Xoroshiro256**, yes this is far too good.
    pub prng: Xoroshiro256,
}

/// An adaptor simulating loss to and from the wrapped layer.
///
/// All incoming packets are filter according to the loss characteristics of the `PrngLoss`. The
/// layer also switches the device handle to one that randomly and silently ignores commands to
/// queue the packet for transmission. This does not perfectly prevent packets from being sent but
/// succeeds for all standard layers and handler implementations.
pub struct Lossy<'a, I>(pub I, pub &'a mut PrngLoss);

/// A handle wrapper that sometimes doesn't queue packets.
///
/// This pretends to be static but internally wraps a reference to the underlying handle. This is
/// of course unsafe but `Device` requires us to specify a *single* associated type as the handle
/// so that it can not include a lifetime parameter.
///
/// However, the implementation ensures that the `LossyHandle` is itself only visible behind a
/// mutable reference with the lifetime of the wrapped handle. It further does not allow to be
/// copied or cloned. This ensures that no reference with larger lifetime can be created.
pub struct LossyHandle<H: ?Sized> {
    prng: *mut PrngLoss,
    handle: *mut H,
}

/// A pseudo-random generator implementing Xoroshiro-256.
///
/// This should be initialized with a random or a deterministic seed depending on the use case. For
/// testing with specific expected error distributions a common seed will guarantee reproducible
/// results. When the test is however too short to approach the expected values and variances then
/// a true random seed could find otherwise undetected patterns by chance.
#[derive(Copy, Clone, Debug, Hash)]
pub struct Xoroshiro256 {
    state: [u64; 4],
}

impl PrngLoss {
    /// A uniform loss simulator.
    pub fn uniform(rate: Option<u32>, seed: u64) -> Self {
        PrngLoss {
            // Threshold always greater than count
            threshold: 1,
            count: 0,
            reset: 0,
            lossrate: rate,
            prng: Xoroshiro256::new(seed),
        }
    }

    /// Wrap a layer to make it lossy.
    pub fn lossy<I>(&mut self, layer: I) -> Lossy<I> {
        Lossy(layer, self)
    }

    /// Simulate burst losses as pulses.
    ///
    /// Drops all packets while in a high state, lets packets pass while in low state.
    pub fn pulsed(high: u32, length: u32) -> Self {
        assert!(length > 0, "Pulse length must not be zero");
        assert!(high <= length, "Length of high signals must be shorter than total length");
        PrngLoss {
            threshold: high,
            count: length - 1,
            reset: length - 1,
            // Packet always lost when pulse condition is true.
            lossrate: Some(u32::max_value()),
            prng: Xoroshiro256::new(0),
        }
    }

    /// Determine the fate for the next packet.
    pub fn next_pass(&mut self) -> bool {
        let in_window = self.count < self.threshold;
        let fate_drop = Some(self.roll()) <= self.lossrate;

        let ncount = self.count.checked_sub(1)
            .unwrap_or(self.reset);
        self.count = ncount;

        !in_window || !fate_drop
    }

    /// Generate the next value of the prng.
    fn roll(&mut self) -> u32 {
        (self.prng.next() & u64::from(!0u32)) as u32
    }
}

impl Xoroshiro256 {
    /// Initialize from a seed.
    ///
    /// Although the seed is smaller than the internal state it is easily large enough to be almost
    /// guaranteed to be unique if generated by random.
    pub fn new(seed: u64) -> Self {
        Xoroshiro256 {
            state: [seed, 0, 0, 0],
        }
    }

    /// Advance the internal state and output the next value.
    pub fn next(&mut self) -> u64 {
        let s = &mut self.state;
		let result_starstar = s[1]
            .wrapping_mul(5)
            .rotate_left(7)
            .wrapping_mul(9);

		let t = s[1] << 17;

		s[2] ^= s[0];
		s[3] ^= s[1];
		s[1] ^= s[2];
		s[0] ^= s[3];

		s[2] ^= t;

		s[3] = s[3].rotate_left(45);

		result_starstar
    }
}

impl<H: ?Sized> LossyHandle<H> {
    /// Instantiate behind a reference with short enough lifetime to ensure it doesn't escape.
    fn new<'a>(
        uninit: &'a mut core::mem::MaybeUninit<Self>,
        prng: &'a mut PrngLoss,
        handle: &'a mut H,
    ) -> &'a mut Self {
        unsafe {
            (*uninit.as_mut_ptr()).prng = prng;
            (*uninit.as_mut_ptr()).handle = handle;
            // Initialized all fields
            &mut *uninit.as_mut_ptr()
        }
    }
}

impl<H, P, I> nic::Recv<H, P> for Lossy<'_, I>
where
    H: nic::Handle + ?Sized,
    P: Payload + ?Sized,
    I: nic::Recv<LossyHandle<H>, P>,
{
    fn receive(&mut self, packet: nic::Packet<H, P>) {
        if !self.1.next_pass() {
            return;
        }

        let nic::Packet { handle, payload } = packet;
        let mut handle_mem = core::mem::MaybeUninit::uninit();
        let handle = LossyHandle::new(
            &mut handle_mem,
            &mut *self.1,
            handle);

        let packet = nic::Packet {
            handle,
            payload,
        };

        self.0.receive(packet)
    }
}

impl<H, P, I> nic::Send<H, P> for Lossy<'_, I>
where
    H: nic::Handle + ?Sized,
    P: Payload + ?Sized,
    I: nic::Send<LossyHandle<H>, P>,
{
    fn send(&mut self, packet: nic::Packet<H, P>) {
        let nic::Packet { handle, payload } = packet;

        let mut handle_mem = core::mem::MaybeUninit::uninit();
        let handle = LossyHandle::new(
            &mut handle_mem,
            &mut *self.1,
            handle);

        self.0.send(nic::Packet {
            handle: &mut *handle,
            payload: &mut *payload,
        });
    }
}

impl<H: nic::Handle + ?Sized> nic::Handle for LossyHandle<H> {
    fn queue(&mut self) -> crate::layer::Result<()> {
        if unsafe { &mut *self.prng }.next_pass() {
            unsafe { &mut *self.handle }.queue()
        } else {
            Ok(())
        }
    }

    fn info(&self) -> &dyn nic::Info {
        unsafe { &*self.handle }.info()
    }
}

impl<D> nic::Device for Lossy<'_, D>
where
    D: nic::Device,
{
    type Handle = LossyHandle<D::Handle>;
    type Payload = D::Payload;

    fn personality(&self) -> nic::Personality {
        self.0.personality()
    }

    fn tx(&mut self, max: usize, sender: impl nic::Send<Self::Handle, Self::Payload>)
        -> crate::layer::Result<usize>
    {
        self.0.tx(max, Lossy(sender, self.1))
    }

    fn rx(&mut self, max: usize, receptor: impl nic::Recv<Self::Handle, Self::Payload>)
        -> crate::layer::Result<usize>
    {
        self.0.rx(max, Lossy(receptor, self.1))
    }
}

impl<P, I> eth::Recv<P> for Lossy<'_, I>
where
    P: Payload,
    I: eth::Recv<P>,
{
    fn receive(&mut self, packet: eth::InPacket<P>) {
        if !self.1.next_pass() {
            return;
        }

        let mut handle_mem = core::mem::MaybeUninit::uninit();
        let loss = &mut self.1;

        // Reconstruct packet with change handle.
        let eth::InPacket { mut control, frame } = packet;
        let control = control
            .borrow_mut()
            .wrap(|inner| LossyHandle::new(
                &mut handle_mem, loss, inner));
        let packet = eth::InPacket { control, frame, };

        self.0.receive(packet)
    }
}

impl<P, I> eth::Send<P> for Lossy<'_, I>
where
    P: Payload,
    I: eth::Send<P>,
{
    fn send(&mut self, packet: eth::RawPacket<P>) {
        let mut handle_mem = core::mem::MaybeUninit::uninit();
        let loss = &mut self.1;

        // Reconstruct packet with changed handle.
        let eth::RawPacket { mut control, payload } = packet;
        let control = control
            .borrow_mut()
            .wrap(|inner| LossyHandle::new(
                &mut handle_mem, loss, inner));
        let packet = eth::RawPacket { control, payload, };

        self.0.send(packet);
    }
}

impl<P, I> ip::Recv<P> for Lossy<'_, I>
where
    P: Payload,
    I: ip::Recv<P>,
{
    fn receive(&mut self, packet: ip::InPacket<P>) {
        if !self.1.next_pass() {
            return;
        }

        let mut handle_mem = core::mem::MaybeUninit::uninit();
        let loss = &mut self.1;

        // Reconstruct packet with change handle.
        let ip::InPacket { mut control, packet } = packet;
        let control = control
            .borrow_mut()
            .wrap(|inner| LossyHandle::new(
                &mut handle_mem, loss, inner));
        let packet = ip::InPacket { control, packet, };

        self.0.receive(packet)
    }
}

impl<P, I> ip::Send<P> for Lossy<'_, I>
where
    P: Payload,
    I: ip::Send<P>,
{
    fn send(&mut self, packet: ip::RawPacket<P>) {
        let mut handle_mem = core::mem::MaybeUninit::uninit();
        let loss = &mut self.1;

        // Reconstruct packet with changed handle.
        let ip::RawPacket { mut control, payload } = packet;
        let control = control
            .borrow_mut()
            .wrap(|inner| LossyHandle::new(
                &mut handle_mem, loss, inner));
        let packet = ip::RawPacket { control, payload, };

        self.0.send(packet);
    }
}

#[cfg(test)]
mod tests {
    use super::PrngLoss;

    #[test]
    fn pulsed() {
        // Drops one out of 10 packets.
        let mut prng = PrngLoss::pulsed(1, 10);
        let count = (0..100)
            .filter(|_| !prng.next_pass())
            .count();
        assert_eq!(count, 10);

        // Drops all packets.
        prng = PrngLoss::pulsed(1, 1);
        let count = (0..100)
            .filter(|_| !prng.next_pass())
            .count();
        assert_eq!(count, 100);

        // Drops at most one out of 10 packets.
        prng = PrngLoss::pulsed(1, 10);
        prng.lossrate = Some(!0 >> 1);
        let count = (0..100)
            .filter(|_| !prng.next_pass())
            .count();
        assert!(count <= 10);
    }
}