Linux下限制端口速度的方法

一些测试场景下,需要模拟端口拥堵的情况,为此,需要限制某个业务端口的速度,从而模拟端口网络拥塞的情况。

开始以为iptables可以限速,发现iptables只能限制到网络包上数量级别,并不能达到模拟网络拥塞的长时间不返回的场景。

搜索、尝试后,发现TC+iptables可以完美实现想要的目的。

tc是系统层的命令,通常安装完都会有。

假设要限速的端口是9000,对应的网卡为eth0,要限制9000端口速度,以此执行以下命令:

tc qdisc add dev eth0 root handle 1:0 htb default 1
tc class add dev eth0 parent 1:0 classid 1:1 htb rate 10Kbit
tc class add dev eth0 parent 1:1 classid 1:100 htb rate 100bit
tc qdisc add dev eth0 parent 1:100 handle 10: sfq perturb 10
tc filter add dev eth0 parent 1:0 protocol ip prio 1 handle 10 fw classid 1:100
iptables -A OUTPUT -t mangle -p tcp --sport 9000 -j MARK --set-mark 10
SuSEfirewall2 stop
SuSEfirewall2 start

这是再访问9000端口的业务,会处于加载中,直到超时。

删除限速:

tc qdisc del dev eth0 root handle 1:0
SuSEfirewall2 stop
SuSEfirewall2 start

你可能感兴趣的:(Linux,linux,网络,端口,限速,TC)