Skip to content

Instantly share code, notes, and snippets.

@bremac
Last active October 2, 2015 20:44
Show Gist options
  • Save bremac/b47332ae94b74991a1fe to your computer and use it in GitHub Desktop.
Save bremac/b47332ae94b74991a1fe to your computer and use it in GitHub Desktop.
Throttle a single port to emulate a bandwidth-constrained network
#!/bin/sh -eux
iface=lo
host=127.0.0.1/32
port=80
netem_rule="delay 150ms 10ms loss 0.5%"
upstream_rule="rate 500kbit"
downstream_rule="rate 2mbit"
upstream=1:11
downstream=1:12
# Reset queuing discipline
tc qdisc del dev ${iface} root || true
tc qdisc add dev ${iface} root handle 1: htb
tc class add dev ${iface} parent 1: classid 1:1 htb rate 1Gbps
tc class add dev ${iface} parent 1:1 classid ${upstream} htb ${upstream_rule}
tc class add dev ${iface} parent 1:1 classid ${downstream} htb ${downstream_rule}
# Upstream network emulation
tc qdisc add dev ${iface} parent ${upstream} handle 11: netem ${netem_rule}
tc filter add dev ${iface} \
protocol ip \
u32 match ip dst ${host} match ip dport ${port} 0xffff \
flowid ${upstream}
# Downstream network emulation
tc qdisc add dev ${iface} parent ${downstream} handle 12: netem ${netem_rule}
tc filter add dev ${iface} \
protocol ip \
u32 match ip src ${host} match ip sport ${port} 0xffff \
flowid ${downstream}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment