Skip to content

Instantly share code, notes, and snippets.

@keturn
Created August 20, 2010 22:35
Show Gist options
  • Save keturn/541339 to your computer and use it in GitHub Desktop.
Save keturn/541339 to your computer and use it in GitHub Desktop.
use netem to add latency to loopback network traffic
#!/bin/bash
#
# Add latency to all outgoing traffic on $DEV on tcp/udp $PORT,
# in the amount of $DELAY.
#
# This is matching on both source port and destination port, which
# may hit you twice if you're accessing a local resource.
#
# To see what's currently in effect,
# tc -s qdisc show dev lo
# tc -p filter show dev lo
#
# The quickest way to undo this is to delete all qdiscs on loopback:
# tc qdisc del dev lo root
#
# This is adding latency, which will make interactivity clunky, but
# it's *not* a cap on throughput, so it doesn't emulate a slow link in
# that respect.
#
# The best reference for this stuff is
# http://tcn.hypert.net/tcmanual.pdf
DEV=lo
PORT=5000
DELAY=150ms
# Create a priority-based queue.
tc qdisc add dev $DEV root handle 1: prio
# Delay everything in band 3
tc qdisc add dev $DEV parent 1:3 handle 30: netem \
delay $DELAY
# say traffic to $PORT is band 3
tc filter add dev $DEV parent 1:0 \
protocol ip \
u32 match ip dport $PORT 0xffff \
flowid 1:3
tc filter add dev $DEV parent 1:0 \
protocol ip \
u32 match ip sport $PORT 0xffff \
flowid 1:3
@danielcompton
Copy link

Thanks so much for this. Extremely helpful! When I was using this for a Vagrant VM, I needed to change $DEV to eth0.

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