netsim - A Rust library for network simulation and testing (currently linux-only).
netsim
is a crate for simulating networks for the sake of testing network-oriented Rust
code. You can use it to run Rust functions in network-isolated containers, and assemble
virtual networks for these functions to communicate over.
Spawning threads into isolated network namespaces
Network namespaces are a linux feature which can provide a thread or process with its own view
of the system's network interfaces and routing table. This crate's spawn
module provides
functions for spawning threads into their own network namespaces. The most primitive of these
functions is new_namespace
, which is demonstrated below. In this example we list the visible
network interfaces using the get_if_addrs
crate.
extern crate netsim;
extern crate get_if_addrs;
use spawn;
// First, check that there is more than one network interface. This will generally be true
// since there will at least be the loopback interface.
let interfaces = get_if_addrs.unwrap;
assert!;
// Now check how many network interfaces we can see inside a fresh network namespace. There
// should be zero.
let join_handle = new_namespace;
let interfaces = join_handle.join.unwrap;
assert!;
This demonstrates how to launch a thread - perhaps running an automated test - into a clean environment. However an environment with no network interfaces is pretty useless...
Creating virtual interfaces
We can create virtual IP and Ethernet interfaces using the types in the iface
module. For
example, Ipv4Iface
lets you create a new IP (TUN) interface and implements futures::{Stream, Sink}
so that you can read/write raw packets to it.
extern crate netsim;
extern crate tokio_core;
extern crate futures;
use Ipv4Addr;
use Core;
use ;
use Ipv4IfaceBuilder;
let mut core = new.unwrap;
let handle = core.handle;
// Create a network interface named "netsim"
let iface = ;
// Read the first `Ipv4Packet` sent from the interface.
let packet = core.run.unwrap;
However for simply testing network code, you don't need to create interfaces manually like this.
Sandboxing network code
Rather than performing the above two steps individually you can use the functions in the
spawn
module to set up various network environments for you. For example,
spawn::on_subnet_v4
will spawn a thread with a single network interface configured to use the
given subnet. It returns a JoinHandle
to join the thread with and an Ipv4Plug
to read/write
packets to the thread's network interface.
extern crate netsim;
extern crate tokio_core;
extern crate futures;
use UdpSocket;
use Core;
use ;
use ;
use Ipv4Payload;
let mut core = new.unwrap;
let handle = core.handle;
let subnet = local_10;
let = on_subnet_v4;
core.run.unwrap
Simulating networks of communicating nodes
To simulate a bunch of IPv4-connected nodes you can use the functions in the node
module
along with the spawn::network_v4
function to describe and launch a simluated network test.
extern crate tokio_core;
extern crate future_utils;
extern crate netsim;
use UdpSocket;
use Core;
use ;
let mut core = new.unwrap;
let handle = core.handle;
let = channel;
let node_a = endpoint_v4;
let node_b = endpoint_v4;
let router_node = router_v4;
let = network_v4;
let = core.run.unwrap;
assert_eq!;
Note that we need to make sure to drive the Core
while blocking on the JoinHandle
in a
separate thread. A future version of this library may clean this situation up.
All the rest
It's possible to set up more complicated (non-hierarchal) network topologies, ethernet networks, namespaces with multiple interfaces etc. by directly using the primitives in this library. Have an explore of the API, and if anything needs clarification or could be designed better then drop a message on the bug tracker :)