Iptables – I need it simulate a delay to a certain IP over as certain port

iptablestc

I need to simulate a traffic slow down to a certain service. That means I need to slow connection on a certain port. I have this script that introduces a delay to a certain IP on all ports, but other services run on that system that I cannot slow down.

echo "simulating slowdown infrstructure on interface $DEV to IP:$IP for port:$PORT";
tc qdisc del dev $DEV root
tc qdisc add dev $DEV handle 1: root htb
tc class add dev $DEV parent 1: classid 1:15 htb rate 1000Mbps
tc qdisc add dev $DEV parent 1:15 handle 11 netem delay 4000ms 2000ms distribution normal
tc filter add dev $DEV parent 1:0 prio 1 protocol ip handle 11 fw flowid 1:15
iptables -A OUTPUT -t mangle -d "$IP" -j MARK --set-mark 11

I can't really figure out what the last line here does because it seems to work without it.

((the undo script is here:))

iptables -D OUTPUT -t mangle -d "$IP" -j MARK --set-mark 11
tc qdisc del dev $DEV root

Best Answer

The last line catch all packets to the specific IP (on all ports), and add a mark on it (inside the kernel only).

Every packets with the mark will be slow down. You just need to specify a destination port in this rule and it should be fine :

iptables -A OUTPUT -t mangle -d "$IP" -p tcp -- dport ${Port} -j MARK --set-mark 11
Related Topic