Skip to content

Instantly share code, notes, and snippets.

@digilist
Created June 2, 2015 14:29
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save digilist/d4b8ecd92b9af7aa0492 to your computer and use it in GitHub Desktop.
Save digilist/d4b8ecd92b9af7aa0492 to your computer and use it in GitHub Desktop.
Delaying network traffic to a specific IP
#!/bin/bash
interface=lo
ip=10.0.0.1
delay=100ms
tc qdisc add dev $interface root handle 1: prio
tc filter add dev $interface parent 1:0 protocol ip prio 1 u32 match ip dst $ip flowid 2:1
tc qdisc add dev $interface parent 1:1 handle 2: netem delay $delay
@digilist
Copy link
Author

digilist commented Jun 4, 2015

@simonmulser
Copy link

thx for the snippet!

small question: line 8, shouldn't be the last qdisc-id 1:1? because in line 9 you are using 1:1 to add the delay.

@GinesGarcia
Copy link

this is not working when using "ip src" instead

@leiless
Copy link

leiless commented Aug 19, 2021

HOWTO verify network delay between two servers

PORT=12345

# Server side
nc -vl $PORT

# Client side
SERVER_IP="..."
time nc -vz $SERVER_IP $PORT

@chuanwang66
Copy link

how to reset after testing??

@deepanshu-yadav
Copy link

how to reset after testing??

Use tc qdisc del dev $interface root
for resetting after testing.

@sm86
Copy link

sm86 commented Oct 15, 2022

how can we extend it two servers, i.e., different delays for different IPs?

@deepanshu-yadav
Copy link

I made a python wrapper for this https://github.com/fledge-iot/fledge/blob/3a92d1786fc58dec499f335b79106840d0c7c8bb/tests/system/python/packages/network_impairment.py#L110

call it two times giving suitable IPs for each of the servers. Hope that helps

@sm86
Copy link

sm86 commented Oct 15, 2022

thanks @deepanshu-yadav I'll go through the Python file. A quick qstn, do I need to change parentid/flow id? I see following error
Error: Exclusivity flag on, cannot modify. when I tried following code

delay.sh
tc qdisc add dev $interface root handle 1: prio
tc filter add dev $interface parent 1:0 protocol ip prio 1 u32 match ip dst $ip1 flowid 2:1
tc qdisc add dev $interface parent 1:1 handle 2: netem delay $delay1
tc filter add dev $interface parent 1:0 protocol ip prio 1 u32 match ip dst $ip2 flowid 2:1
tc qdisc add dev $interface parent 1:1 handle 2: netem delay $delay2

@deepanshu-yadav
Copy link

You need to divide your traffic into three parts packets for ip1 , packets for ip2 and packets for ip2.

This is possible please go through all the links I have given in the code (start).

There is a lot to learn and your problem is not a simple copy paste from stack overflow .

@viniciusd
Copy link

It seems all solutions are qdisc-prio-based, but qdisc-prio has a limit of 16 priorities, so one can't use it for 17 or more routes

@strNewBee
Copy link

This will somehow affects my SSH connect to the remote server where those command runs while successfully delaying certain IP flow. Don't know how to fix it. TAT

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