Blog

How to mock the Internet

Simulating the Internet on a laptop?

Let’s send two back-to-back packets from a datacenter on the US west coast and capture them as they arrive in eastern Europe.

This is the kind of test you run routinely when designing a large-scale network monitoring tool such as CloudyWithAChanceOfLatency.

Let’s redo this experiment for various link speeds and load-balancing configurations of the routers bridging the Atlantic.

¯\_(ツ)_/¯

Starting a cheap virtual machine anywhere to generate traffic is easy but few operators let you change the configuration of intercontinental routers.

These tests can’t be done comfortably from the lab either, even leaving aside the 5,000 km of fiber optics necessary to replicate the Atlantic gap. Buying, wiring and configuring the equipment involved in each test setup would require a ridiculous amount of money and time.

Fortunately, networks, like hosts, can be simulated.

Simulating the Internet

Network simulators come in all sorts of shapes and sizes.

Packet generators

The most common type of network simulators are packet generators.

They are large (and expensive) physical appliances which can generate more or less realistic flows of user traffic, at a very high speed, on actual wire or radio links. Telcos buy (or rent) them to load test a given piece of equipment.

When developing a networking probe though, these packet generators are of little use. They simulate the traffic hitting a single gear like a switch or a router, whereas what is needed is the other way around: a device that simulates the vast Internet in between two traffic-generating machines sitting in the lab.

Emulators

Another option: major networking manufacturers offer software images that are bit-for-bit emulators of a given model of router, down to its operating system and administration interface.

The issue with that type of simulation is that it requires as much configuration to set up a test as the real equipment would. A feature when you want to learn how a router works, but a major hindrance when you want to simulate a network of dozens of them.

On top of that, you can only include in your simulation the appliances for which you could find such an emulator.

Native network stack?

Finally, there is a third class of network simulators: those based on virtual machines (or containers) and virtual switches such as Open vSwitch. The Linux network stack these days includes all the tools to build any topology to connect virtual environments in any possible way. It’s not just a toy: it’s what underpins any Kubernetes pod.

The issue is that the undesirable physical characteristics of the real Internet (buffer queue size, latency due to distances, NAT table saturations, jitter…) have to be simulated with whatever parameters the virtual switch offers. If you want to simulate a faulty piece of equipment botching a specific packet, you will have to write some ingenious netfilter rule.

Sooner than expected, you will end up with your hands in the Linux kernel.

The ideal network simulator

Ideally, for this kind of task, the simulator that’s needed is:

  1. Software only

Software is much easier to manipulate (especially by an agent), and also cheaper.

When everything is simulated then speed is not really an issue anyway: if the simulation is so complex that the machine can’t keep up with real time then the simulated clock will just run slower than the wall clock. Conversely, if the simulation can run faster than the wall clock then the results are available quicker.

  1. A network stack that’s as simple as possible

Simplifications:

  • There is no need to emulate the CPU that runs the routers or the probes.
  • There is no need to reproduce the exact same configuration software that runs on an actual router or switch.
  • There is no need to emulate a complete Linux host just to run a traceroute.
  • There is no need, even, to implement every protocol under the sun with all their options and corner cases.

Requirements:

  • We need to emulate the most common protocols; say, everything to run a web service, that is: ARP, DHCP, Ethernet, MPLS, IPv4, IPv6, ICMP, UDP, BGP, DNS, TCP, HTTP, HTTPS.

  • Routers must behave realistically, timing included; their user interface need not.

  1. Packet capture and injection

We want to be able to capture packets at any location, or replay a pcap file.

We also want to open real network interfaces and exchange synthesized packets with real ones originating from the real network, if only to test the simulated protocol stack.

  1. Easy to use for an agent

We want an interface with the simulator that’s descriptive, not imperative.

So we can describe a network topology and a test to an agent, and the agent has all the tools necessary to build the network, perform the simulation, and come back with the test results within minutes.

Robinet

About 15 years ago, I needed a tool to manipulate very large pcap files, edit them, generate larger files by duplicating the flows etc. Scapy had all the features I needed, but would only generate my pcap file in exchange for hundreds of GiB of swap for days.

As I grew frustrated by Scapy’s inefficient implementation, I started to build my own packet manipulation library: Robinet.

As I needed to generate more and more realistic payloads, the packet manipulation library grew into a small network simulator.

Its network stack was handmade but realistic enough to withstand a live communication with an actual host over the Internet.

As years passed this little Swiss Army knife for network impersonation grew many unforeseen features. At times it was a network simulator, a honeypot, a web scraper…

Like Scapy, it was mainly a library: you had to program it for it to do anything.

Unfortunately, a network is a graph, and it’s quite hard to understand a graph without visualizing it.

So over the years I’ve tried several times to endow Robinet with some sort of UI. None of them have ever been useful enough to survive; building a proper UI for a network editor was just too much work for a side project.

Until recently, when Opus lowered the bar of what’s reasonable for a side project. Adding a simple layer of abstraction in the simulator to decouple it from a REST API manipulating it was a few hours’ work; from there a coding agent could take over first the API, then the web UI using it.

Robinet's web UI editing a simulated network

Two weeks later, the API and the UI are there, and one can ask an LLM:

Claude Code
Claude Code v2.1.278
Opus 5 · Claude Pro
~/cloudy-tests/robinet
❯Let's build a network with twelve routers: six spanning the US, fully connected, and six in Europe also fully connected. Then, fully connect two in the eastern US to two in western Europe. Parameterize the speed and load-balancing configuration of those routers with one pair of parameters for land routers and one for the four routers crossing the Atlantic. Then let's send two 1 KiB Ethernet frames back to back from the western US to eastern Europe, and capture the frames both at departure and on arrival.
✻Churned for 2m 37s
(...)
❯Let's redo this experiment for various link speeds and load-balancing configurations of the routers bridging the Atlantic. Can we reproduce the packet-pair effect1? How does this effect depend on load-balancing configurations?
●Both answers are yes. The packet-pair effect reproduces exactly as long as the two frames take the same way. Load balancing that splits them wipes the signal out, by 2 to 4 orders of magnitude.
1. The effect reproduces. When both frames take the same way, the arrival gap equals the time the slowest link takes to send one frame: (1024 bytes + 96 bits of inter-frame gap) / min(land speed, Atlantic speed). This held in all 243 same-way runs:
┌───────────────┬───────┬──────┬─────┬──────┬─────┬─────┬──────┐
│  bottleneck   │  10M  │ 100M │ 1G  │ 2.5G │ 5G  │ 10G │ 25G+ │
├───────────────┼───────┼──────┼─────┼──────┼─────┼─────┼──────┤
│ expected (µs) │ 828.8 │ 82.9 │ 8.3 │ 3.3  │ 1.7 │ 0.8 │ ≤0.3 │
├───────────────┼───────┼──────┼─────┼──────┼─────┼─────┼──────┤
│ measured (µs) │ 829   │ 83   │ 8   │ 3    │ 2   │ 1   │ 0    │
└───────────────┴───────┴──────┴─────┴──────┴─────┴─────┴──────┘
Captures have 1µs resolution, so above about 5Gbps the numbers are only good to within a microsecond.
2. Load balancing hides it whenever the two frames are sent different ways. The gap then becomes the difference in length between the two ways, whatever the speeds:
┌────────────────┬────────────┬────────────┬────────────┬─────────────────┐
│   land LB \    │   first    │    flow    │   random   │   round robin   │
│  Atlantic LB   │            │            │            │                 │
├────────────────┼────────────┼────────────┼────────────┼─────────────────┤
│ first          │ bottleneck │ bottleneck │ bottleneck │ +3.5 ms         │
├────────────────┼────────────┼────────────┼────────────┼─────────────────┤
│ flow           │ bottleneck │ bottleneck │ bottleneck │ +3.5 ms         │
├────────────────┼────────────┼────────────┼────────────┼─────────────────┤
│ random         │ bottleneck │ bottleneck │ +3.5 ms    │ −3.5 ms         │
│                │            │            │            │ (reordered)     │
├────────────────┼────────────┼────────────┼────────────┼─────────────────┤
│ round robin    │ +0.30 ms   │ +0.30 ms   │ +0.30 ms   │ +0.30 ms        │
└────────────────┴────────────┴────────────┴────────────┴─────────────────┘
- First matching and track flow never split the pair, so they always show the bottleneck.
- Round robin always splits it. On land, los-angeles sends one frame through new-york and one through ashburn, 0.30ms apart. At sea, the london and lisbon crossings are 3.5ms apart.
✻Brewed for 11m 25s

It’s open-source, hosted on GitHub. Have a look if you (or your agent) are tired of waiting for Scapy.

Footnotes

  1. The packet-pair effect: two packets sent back to back arrive spaced out by the slowest link on the path, which lets you estimate the bottleneck bandwidth without saturating it. ↩