Skip to content

Instantly share code, notes, and snippets.

@alexpyattaev
Created January 11, 2025 22:39
Show Gist options
  • Save alexpyattaev/c5d03696d6e39fb13711c87bdf8c305b to your computer and use it in GitHub Desktop.
Save alexpyattaev/c5d03696d6e39fb13711c87bdf8c305b to your computer and use it in GitHub Desktop.
Veth setup with packet loss
echo "This script should be run as root"
DELAY_MS=100
DELAY_DISTRIBUTION=50
LOSS_PERCENT=2
CLI="ip netns exec client"
SRV="ip netns exec server"
echo "Cleanup, ignore errors if this is first run"
ip netns del client
ip netns del server
echo "Add namespaces"
ip netns add client
ip netns add server
echo "Add virtual ethernet"
ip link add veth_srv type veth peer name veth_cli
ip link set dev veth_cli netns client
ip link set dev veth_srv netns server
echo "Configure IP 192.168.42.1/24 on server"
$SRV ip a a 192.168.42.1/24 dev veth_srv
$SRV ip link set veth_srv up
echo "Configure IP 192.168.42.2/24 on client"
$CLI ip a a 192.168.42.2/24 dev veth_cli
$CLI ip link set veth_cli up
echo "Set delay of ${DELAY_MS}ms, packet loss ${LOSS_PERCENT}%"
$SRV tc qdisc add dev veth_srv root handle 1: netem delay ${DELAY_MS}ms ${DELAY_DISTRIBUTION}ms distribution normal
$SRV tc qdisc add dev veth_srv parent 1: handle 2: netem loss ${LOSS_PERCENT}%
$CLI tc qdisc add dev veth_cli root handle 1: netem delay ${DELAY_MS}ms ${DELAY_DISTRIBUTION}ms distribution normal
$CLI tc qdisc add dev veth_cli parent 1: handle 2: netem loss ${LOSS_PERCENT}%
echo 'Run "ip netns exec client bash" to start a shell in namespace for CLIENT'
echo 'Run "ip netns exec server bash" to start a shell in namespace for SERVER'
echo 'Remember that you can not expect internet to work inside the namespace!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment