关于五元组限速(openwrt+TC)的心得

网上关于openwrt的tc限速描述有很多,目前主流用的是IMQ和ifb,其中IMQ的设置比较复杂,ifb的描述相当于简单点,下面我简单总结下ifb的使用,

TC的大概流程如下,具体每条命令的作用我就不做详细介绍,大家可以百度

 tc qdisc add dev eth0 ingress
 tc filter add dev br-eth0 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0
 tc qdisc add dev ifb0 root handle 1: htb default 1000
 tc class add dev ifb0 parent 1: classid 1:1 htb rate 2000kbit ceil 2000kbit   #限速2M

tc class add dev ifb0 parent 1:1 classid 1:1000 htb rate 1kbit ceil 8000kbit prio 3
 tc qdisc add dev ifb0 parent 1:1000 handle 1000: sfq perturb 10

 

 tc class add dev ifb0 parent 1:1 classid 1:8 htb rate 1000kbit ceil 2000kbit prio 1
 tc qdisc add dev ifb0 parent 1:8 handle 8: sfq perturb 10
 tc filter add dev ifb0 parent 1:0 prio 1 protocol ip handle 8 fw flowid 1:8

 iptables -t mangle -A PREROUTING -p udp -j MARK --set-mark 8
 iptables -t mangle -A PREROUTING  -p udp -j RETURN

 

上述的使用的netfilter和tc相结合,通过fw,是的mark打入skb中,下面重点来了,

我发现在不同设备上,出现下面问题,百度了一下,居然老外也遇到过(be aware that if you use iptable to mark your packet and then filters them, you can't use ifb since all ingress trafic will be forwarded BEFORE any marking. so you will se your class stay at 0 and all forwarded to the default. IMQ seem the rigth solution for iptables users.)

为了解决这个问题,查了很多资料,终于get到了,在iptables中使用connmark这个参数进行mark的标记,从而使得skb头里也能成功打上mark

使用netfilter的原因是因为涉及到ipset 地址集,以及域名。

tc也提供了另外一种方法,下面方法比较简单了,也可以涉及到多地址,但可能比较繁琐点(至少我认为没有IPtables简便)

#tc qdisc add dev imq0 root handle 1: htb default 20
#tc class add dev imq0 parent 1: classid 1:1 htb rate 2mbit burst 15k
#tc class add dev imq0 parent 1:1 classid 1:10 htb rate 1mbit
#tc class add dev imq0 parent 1:1 classid 1:20 htb rate 1mbit
#tc qdisc add dev imq0 parent 1:10 handle 10: pfifo
#tc qdisc add dev imq0 parent 1:20 handle 20: sfq
#tc filter add dev imq0 parent 10:0 protocol ip prio 1 u32 match ip dst 10.0.0.230/32 flowid 1:10

这里其实也可以对protol协议进行限制,以及端口范围

协议”

tc filter add dev ifb0 parent 1: protocol ip prio 2 u32 match ip protocol 1 0xff flowid 1:8

1 0xff是icmp 协议,这里1 代表icmp,你可以在/etc/protocols查看每个协议具体是什么

关于端口范围

tc filter add dev eth2 protocol ip parent 1: prio 2 u32 match ip sport 22 0xffff flowid 1:12
0xffB2-0xffff的数值,表示从起始端口开始后多少。

开发过程中,主要是上述的一些困惑点

有兴趣的可以加qq一起研究 739980123或者私信

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章