Author Topic: Emulating network latency on loopback interface / LAN  (Read 3445 times)

tomreyn

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 2,764
    • View Profile
    • MegaGlest - the free and open source cross platform 3D real-time strategy game
Emulating network latency on loopback interface / LAN
« on: 22 March 2013, 21:17:38 »
Lately we tend to have the need to reproduce issues which are limited to WAN connections and are difficult to reproduce when running two instances of MegaGlest on the same host (or on a LAN). That's because there is (almost) no latency there:

Code: [Select]
root@atibox:~# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.025 ms
64 bytes from 127.0.0.1: icmp_req=2 ttl=64 time=0.025 ms
64 bytes from 127.0.0.1: icmp_req=3 ttl=64 time=0.022 ms
64 bytes from 127.0.0.1: icmp_req=4 ttl=64 time=0.024 ms
^C
--- 127.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2997ms
rtt min/avg/max/mdev = 0.022/0.024/0.025/0.001 ms
root@atibox:~#

Luckily, on Linux, it's rather easy to inject arbitrary latency on a given network device:
Code: [Select]
root@atibox:~# tc qdisc add dev lo root netem delay 50ms
root@atibox:~# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=100 ms
64 bytes from 127.0.0.1: icmp_req=2 ttl=64 time=100 ms
64 bytes from 127.0.0.1: icmp_req=3 ttl=64 time=100 ms
64 bytes from 127.0.0.1: icmp_req=4 ttl=64 time=100 ms
64 bytes from 127.0.0.1: icmp_req=5 ttl=64 time=100 ms
64 bytes from 127.0.0.1: icmp_req=6 ttl=64 time=100 ms
64 bytes from 127.0.0.1: icmp_req=7 ttl=64 time=100 ms
^C
--- 127.0.0.1 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6005ms
rtt min/avg/max/mdev = 100.046/100.062/100.075/0.239 ms
root@atibox:~#

To undo, run the sme command with s/add/del/.

This is not a fully adequate emulation, though, since you always have the same latency, so packets will still arrive in the same order, and you're not emulating any other properties which would be varying on a real network link. It's worth a shot, though.

For more complex tests there are WANEM and dummynet:
« Last Edit: 22 March 2013, 21:24:34 by tomreyn »
atibox: Ryzen 1800X (8 cores @3.6GHz), 32 GB RAM, MSI Radeon RX 580 Gaming X 8G, PCI subsystem ID [1462:3417], (Radeon RX 580 chipset, POLARIS10) @3440x1440; latest stable Ubuntu release, (open source) radeon (amdgpu) / mesa video driver
atibox (old): Core2Quad Q9400 (4 cores @2.66GHz), 8 GB RAM, XFX HD-467X-DDF2, PCI subsystem ID [1682:2931], (Radeon HD 4670, RV730 XT) @1680x1050; latest stable Ubuntu release, (open source) radeon / mesa video driver
notebook: HP envy13d020ng
internet access: VDSL2+

· · · How YOU can contribute to MG · Latest development snapshot · How to build yourself · Megapack techtree · Currently hosted MG games · · ·

tomreyn

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 2,764
    • View Profile
    • MegaGlest - the free and open source cross platform 3D real-time strategy game
Re: Emulating network latency on loopback interface / LAN
« Reply #1 on: 26 March 2013, 09:05:12 »
Here's a ittle bash script allowing you to have varying latencies on a given interface. Titi and I used it for testing today and it proved useful.

Example invocation + output:
Code: [Select]
tomreyn@atibox:~$  sudo bin/randomlatency eth0 500
Adding 92 ms of latency on eth0...
Sleeping for 5 seconds...
Resetting virtual latency on eth0...
Sleeping for 5 seconds (press 'q' to quit)...
Adding 429 ms of latency on eth0...
Sleeping for 5 seconds...
Resetting virtual latency on eth0...
Sleeping for 5 seconds (press 'q' to quit)...
Adding 369 ms of latency on eth0...
Sleeping for 5 seconds...
Resetting virtual latency on eth0...
Sleeping for 5 seconds (press 'q' to quit)...
q
Quitting.
tomreyn@atibox:~$

Source:
Code: [Select]
#!/bin/bash

# Interface to apply virtual delay on
interface=$1

# Maximum add-on latency
latency_max=$2

sleep=5

if [[ `id -u` != 0 ]]
then echo 'ERROR: Sorry, you must be root (UID 0) to run this script. Try sudo or su.' >&2; exit 1
fi

if [[ $interface = '' ]]
then interface=lo
fi

if [[ $latency_max = '' ]]
then latency_max=50
fi

key=''
while [[ "${key}" != 'q' ]]
do
  latency=$(( ( $RANDOM % $latency_max )  + 1 ))
  echo "Adding $latency ms of latency on $interface..."
  tc qdisc add dev $interface root netem delay ${latency}ms
  echo "Sleeping for $sleep seconds..."
  sleep $sleep
  echo "Resetting virtual latency on $interface..."
  tc qdisc del dev $interface root netem delay ${latency}ms
  echo "Sleeping for $sleep seconds (press 'q' to quit)..."
  read -n1 -t${sleep} key
done
echo
echo 'Quitting.'

atibox: Ryzen 1800X (8 cores @3.6GHz), 32 GB RAM, MSI Radeon RX 580 Gaming X 8G, PCI subsystem ID [1462:3417], (Radeon RX 580 chipset, POLARIS10) @3440x1440; latest stable Ubuntu release, (open source) radeon (amdgpu) / mesa video driver
atibox (old): Core2Quad Q9400 (4 cores @2.66GHz), 8 GB RAM, XFX HD-467X-DDF2, PCI subsystem ID [1682:2931], (Radeon HD 4670, RV730 XT) @1680x1050; latest stable Ubuntu release, (open source) radeon / mesa video driver
notebook: HP envy13d020ng
internet access: VDSL2+

· · · How YOU can contribute to MG · Latest development snapshot · How to build yourself · Megapack techtree · Currently hosted MG games · · ·

tomreyn

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 2,764
    • View Profile
    • MegaGlest - the free and open source cross platform 3D real-time strategy game
Re: Emulating network latency on loopback interface / LAN
« Reply #2 on: 25 June 2013, 00:56:04 »
As an update to this, Softcoder has since added two more INI options for testing network latency:

Code: [Select]
SimulateClientLag  - milliseconds to sleep
SimulateClientLagDurationSeconds - how long to lag for in seconds

Please do not use these options unless you actually want to break MegaGlest. And even then, make sure you will not forget to remove them once you broke it.
atibox: Ryzen 1800X (8 cores @3.6GHz), 32 GB RAM, MSI Radeon RX 580 Gaming X 8G, PCI subsystem ID [1462:3417], (Radeon RX 580 chipset, POLARIS10) @3440x1440; latest stable Ubuntu release, (open source) radeon (amdgpu) / mesa video driver
atibox (old): Core2Quad Q9400 (4 cores @2.66GHz), 8 GB RAM, XFX HD-467X-DDF2, PCI subsystem ID [1682:2931], (Radeon HD 4670, RV730 XT) @1680x1050; latest stable Ubuntu release, (open source) radeon / mesa video driver
notebook: HP envy13d020ng
internet access: VDSL2+

· · · How YOU can contribute to MG · Latest development snapshot · How to build yourself · Megapack techtree · Currently hosted MG games · · ·

 

anything