Skip to content
How to Use Linux Network Namespaces to Benchmark Ethernet Switches with iperf3

How to Use Linux Network Namespaces to Benchmark Ethernet Switches with iperf3

May 5, 2026

I often need to measure the maximum bandwidth of an Ethernet switch or router. The standard tool for this is iperf3, which attempts to push as many data packets as possible through an Ethernet link.

However, this requires two Ethernet devices: one to generate test traffic and another to receive it. The first time I ran this test, I simply installed two Ethernet cards in a single PC and ran iperf. But this approach doesn’t work as expected. The PC’s network stack detects that both IP addresses belong to the same system, so it doesn’t send the packets over the physical wire. Instead, the traffic stays inside the PC, resulting in unrealistically high data rates.

For many years, I worked around this by using two separate devices—PCs, laptops, or Raspberry Pis. But managing multiple machines can be inconvenient.

A colleague of mine introduced me to Linux network namespaces (netns), and this turned out to be a game changer. With netns, you can isolate network interfaces from each other, effectively creating completely independent network environments within a single computer. As a side note, Docker uses this concept extensively, although users typically don’t see it directly.

To use network namespaces for benchmarking, follow these steps:

  1. Create two network namespaces:

    sudo ip netns add ns1
    sudo ip netns add ns2
  2. Move the physical Ethernet interfaces into the namespaces. Be aware that these interfaces will disappear from the default namespace. If you are connected through one of them, you will lose connectivity. (Replace enp4s0f0 and enp4s0f1 with your actual interface names.)

    sudo ip link set enp4s0f0 netns ns1
    sudo ip link set enp4s0f1 netns ns2
  3. Configure the interfaces inside the namespaces:

    sudo ip netns exec ns1 ip link set enp4s0f0 up
    sudo ip netns exec ns1 ip addr add 192.168.100.1/24 dev enp4s0f0
    
    sudo ip netns exec ns2 ip link set enp4s0f1 up
    sudo ip netns exec ns2 ip addr add 192.168.100.2/24 dev enp4s0f1
  4. Start iperf3 in each namespace (e.g., in separate terminals):

    sudo ip netns exec ns1 iperf3 -s
    sudo ip netns exec ns2 iperf3 -c 192.168.100.1 -t 10
  5. Delete the namespaces when you’re done:

    sudo ip netns del ns1
    sudo ip netns del ns2

That’s all! This approach makes benchmarking Ethernet devices much more flexible while reducing hardware requirements. In practice, I don’t run these commands manually one by one—I typically use shell or Python scripts to automate the process.

For more information about Linux network namespaces, see: https://blog.scottlowe.org/2013/09/04/introducing-linux-network-namespaces/