Skip to content

Instantly share code, notes, and snippets.

@vandorjw
Forked from keturn/loopback-latency.sh
Created July 30, 2021 20:25
Show Gist options
  • Save vandorjw/deaa8ca34f760ae2d0162089d9cb0f0a to your computer and use it in GitHub Desktop.
Save vandorjw/deaa8ca34f760ae2d0162089d9cb0f0a to your computer and use it in GitHub Desktop.
use netem to add latency to loopback network traffic
#!/bin/bash
#
# https://wiki.linuxfoundation.org/networking/netem#Emulating_wide_area_network_delays
#
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment