shadow_rs/network/router/
mod.rs

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
use std::cell::RefCell;
use std::net::Ipv4Addr;

use self::codel_queue::CoDelQueue;
use crate::core::worker::Worker;
use crate::cshadow as c;
use crate::network::packet::PacketRc;
use crate::network::PacketDevice;
use crate::utility::{Magic, ObjectCounter};
mod codel_queue;

use shadow_shim_helper_rs::emulated_time::EmulatedTime;

/// A router assists with moving packets between hosts across the simulated
/// network.
pub struct Router {
    magic: Magic<Self>,
    _counter: ObjectCounter,
    address: Ipv4Addr,
    /// Packets inbound to the host from the simulated network.
    inbound_packets: RefCell<CoDelQueue>,
}

impl Router {
    /// Create a new router for a host that will help route packets between it
    /// and other hosts. The `address` must uniquely identify this router to the
    /// host that owns it.
    pub fn new(address: Ipv4Addr) -> Router {
        Router {
            magic: Magic::new(),
            address,
            _counter: ObjectCounter::new("Router"),
            inbound_packets: RefCell::new(CoDelQueue::new()),
        }
    }

    fn push_inner(&self, packet: PacketRc, now: EmulatedTime) {
        self.magic.debug_check();
        self.inbound_packets.borrow_mut().push(packet, now);
    }

    fn pop_inner(&self, now: EmulatedTime) -> Option<PacketRc> {
        self.magic.debug_check();
        self.inbound_packets.borrow_mut().pop(now)
    }

    /// Routes the packet from the source host through the virtual internet to
    /// the destination host.
    fn route_outgoing_packet(&self, packet: PacketRc) {
        // TODO: move Worker::send_packet to here?
        let cpacket = packet.into_inner();
        Worker::with_active_host(|src_host| unsafe { Worker::send_packet(src_host, cpacket) })
            .unwrap();
        unsafe { c::packet_unref(cpacket) };
    }

    /// Routes the packet from the virtual internet into our CoDel queue, which
    /// can then be received by the destiantion host by calling pop().
    pub fn route_incoming_packet(&self, packet: PacketRc) {
        self.push_inner(packet, Worker::current_time().unwrap())
    }
}

impl PacketDevice for Router {
    fn get_address(&self) -> Ipv4Addr {
        self.address
    }

    fn pop(&self) -> Option<PacketRc> {
        // When the host calls pop, we provide the next packet from the CoDel queue.
        self.pop_inner(Worker::current_time().unwrap())
    }

    fn push(&self, packet: PacketRc) {
        // When the host calls push, we send to the virtual internet.
        self.route_outgoing_packet(packet);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::network::tests::mock_time_millis;

    #[test]
    fn empty() {
        let now = mock_time_millis(1000);
        let router = Router::new(Ipv4Addr::UNSPECIFIED);
        assert!(router.inbound_packets.borrow().peek().is_none());
        assert!(router.pop_inner(now).is_none());
    }

    #[test]
    // Ignore in miri for use of c::packet* functions.
    #[cfg_attr(miri, ignore)]
    fn push_pop_simple() {
        let now = mock_time_millis(1000);
        let router = Router::new(Ipv4Addr::UNSPECIFIED);

        const N: usize = 10;

        for _ in 1..=N {
            router.push_inner(PacketRc::mock_new(), now);
            assert!(router.inbound_packets.borrow().peek().is_some());
        }
        for _ in 1..=N {
            assert!(router.inbound_packets.borrow().peek().is_some());
            assert!(router.pop_inner(now).is_some());
        }

        assert!(router.inbound_packets.borrow().peek().is_none());
        assert!(router.pop_inner(now).is_none());
    }
}