Skip to content

Instantly share code, notes, and snippets.

@unverbraucht
Last active January 24, 2023 18:16
Show Gist options
  • Save unverbraucht/118117ab66ac142f4eda to your computer and use it in GitHub Desktop.
Save unverbraucht/118117ab66ac142f4eda to your computer and use it in GitHub Desktop.
Script for OpenWRT that adds delay, bandwidth limiting and packet loss to a Wi-Fi router connection. See http://kevin-read.com/post/86601925386/simulating-a-slow-network-connection-when-testing-on for details
#!/bin/sh
# The bandwidth to simulate, here about 56kilobit per second. This is layer 2 bandwidth, so TCP/UDP and IP overhead will apply
BW="56kbps"
# _Half_ the latency that we aim for. Since this applies to both the WAN port and Wi-Fi, the delay is applied twice, so this actually puts it at around 120+ms
LATENCY="60ms"
# Chance of packet loss. Also applied to both interfaces, so it is 1%.
LOSS="0.5%"
# The device name of your wifi device.
WIFI="wlan0"
# The device name of your wan port
WAN="eth0.2"
# Delete existing traffic control rules
tc qdisc del root dev $WIFI
tc qdisc del root dev $WAN
# Create a basic tc rule for wifi
tc qdisc add dev $WIFI root handle 1: htb default 12
# First apply the rate limiting through a HTC scheduler
tc class add dev $WIFI parent 1:1 classid 1:12 htb rate $BW ceil $BW
# Then apply the netem scheduler which handles delay and packet loss
tc qdisc add dev $WIFI parent 1:12 netem delay $LATENCY 10ms 25% loss $LOSS
# The same for WAN
tc qdisc add dev $WAN root handle 2: htb default 12
tc class add dev $WAN parent 2:1 classid 2:12 htb rate $BW ceil $BW
tc qdisc add dev $WAN parent 2:12 netem delay $LATENCY 10ms 25% loss $LOSS
@HenkPoley
Copy link

WAN=uci show network.wan.ifname

There doesn't appear to be an uci entry for wlan. Maybe they are just defined as wlan{0..} in OpenWRT.

Also note: "The NetEm qdisc does not work in conjunction with other qdiscs" ~ http://www.bufferbloat.net/projects/codel/wiki/Best_practices_for_benchmarking_Codel_and_FQ_Codel#The-NetEm-qdisc-does-not-work-in-conjunction-with-other-qdiscs

I looked this up to see if I could add delay to port 80 and/or IPv4. To discourage unencrypted traffic a bit. Still not sure how do that.

@PierBover
Copy link

@unverbraucht How difficult would it be to control those parameters via a web browser? I have zero experience with OpenWRT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment