diff --git a/Cargo.lock b/Cargo.lock index 3bea419..680fa39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,83 @@ dependencies = [ "syn", ] +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -43,6 +120,47 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "smallvec" +version = "1.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "syn" version = "3.0.10" @@ -60,7 +178,26 @@ version = "1.54.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -98,6 +235,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "tokio", "virtual_network_core", ] @@ -117,3 +255,24 @@ version = "0.1.0" dependencies = [ "virtual_network_core", ] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/virtual_network_driver/Cargo.toml b/virtual_network_driver/Cargo.toml index dd407d8..b53737e 100644 --- a/virtual_network_driver/Cargo.toml +++ b/virtual_network_driver/Cargo.toml @@ -7,3 +7,5 @@ edition = "2024" virtual_network_core = { path = "../virtual_network_core" } anyhow = "1.0.104" async-trait = "0.1.92" +tokio = { version = "1.54.1", features = ["full"] } + diff --git a/virtual_network_driver/src/lib.rs b/virtual_network_driver/src/lib.rs index 53aeb03..26f5116 100644 --- a/virtual_network_driver/src/lib.rs +++ b/virtual_network_driver/src/lib.rs @@ -1 +1,2 @@ -mod driver; \ No newline at end of file +pub mod driver; +pub mod mock; diff --git a/virtual_network_driver/src/mock.rs b/virtual_network_driver/src/mock.rs new file mode 100644 index 0000000..3321f88 --- /dev/null +++ b/virtual_network_driver/src/mock.rs @@ -0,0 +1,110 @@ +//! In-memory mock [`DeviceDriver`] that works without root privileges. +//! +//! [`MockDriver::new`] returns the driver together with a [`MockHandle`], which plays the role +//! of the operating system: +//! +//! - [`MockHandle::inject`] simulates a local application sending a packet into the virtual +//! interface. The packet comes out of [`DeviceDriver::recv`]. +//! - [`MockHandle::next_delivered`] returns packets that were written with [`DeviceDriver::send`], +//! i.e. packets the router delivered to the local OS. + +use std::net::Ipv4Addr; + +use anyhow::{Context, bail}; +use async_trait::async_trait; +use tokio::sync::{Mutex, mpsc}; +use virtual_network_core::RECV_BUFFER_SIZE; +use virtual_network_core::driver::DeviceDriver; + +pub struct MockDriver { + virtual_ipv4: Ipv4Addr, + /// Packets injected by the handle, returned from `recv`. + outbound: Mutex>>, + /// Packets written with `send`, read by the handle. + inbound: mpsc::UnboundedSender>, +} + +/// The "OS side" of a [`MockDriver`]. +pub struct MockHandle { + outbound: mpsc::UnboundedSender>, + inbound: mpsc::UnboundedReceiver>, +} + +impl MockDriver { + pub fn new(virtual_ipv4: Ipv4Addr) -> (Self, MockHandle) { + let (outbound_tx, outbound_rx) = mpsc::unbounded_channel(); + let (inbound_tx, inbound_rx) = mpsc::unbounded_channel(); + + let driver = MockDriver { + virtual_ipv4, + outbound: Mutex::new(outbound_rx), + inbound: inbound_tx, + }; + let handle = MockHandle { + outbound: outbound_tx, + inbound: inbound_rx, + }; + (driver, handle) + } +} + +#[async_trait] +impl DeviceDriver for MockDriver { + async fn recv(&self, buf: &mut [u8; RECV_BUFFER_SIZE]) -> anyhow::Result { + let packet = self + .outbound + .lock() + .await + .recv() + .await + .context("mock driver handle was dropped")?; + + // `MockHandle::inject` should reject packets that don't fit. + debug_assert!(packet.len() <= buf.len()); + buf[..packet.len()].copy_from_slice(&packet); + Ok(packet.len()) + } + + async fn send(&self, packet: &[u8]) -> anyhow::Result<()> { + self.inbound + .send(packet.to_vec()) + .context("mock driver handle was dropped") + } + + fn virtual_ipv4(&self) -> Ipv4Addr { + self.virtual_ipv4 + } +} + +impl MockHandle { + /// Simulates a local application sending `packet` into the virtual interface. + /// + /// Fails if `packet` is larger than [`RECV_BUFFER_SIZE`] or the driver was dropped. + pub fn inject(&self, packet: impl Into>) -> anyhow::Result<()> { + let packet = packet.into(); + if packet.len() > RECV_BUFFER_SIZE { + bail!( + "injected packet is {} bytes, larger than the {} byte receive buffer", + packet.len(), + RECV_BUFFER_SIZE + ); + } + + self.outbound + .send(packet) + .ok() + .context("mock driver was dropped") + } + + /// Waits for the next packet the driver delivered to the OS. + /// + /// Returns `None` once the driver is dropped and all delivered packets were read. + pub async fn next_delivered(&mut self) -> Option> { + self.inbound.recv().await + } + + /// Returns the next delivered packet without waiting, if there is one. + pub fn try_next_delivered(&mut self) -> Option> { + self.inbound.try_recv().ok() + } +}