Port based speed limit/traffic control on Linux
名词
- qdisc 是 Linux 流量控制系统的核心。qdisc 也被称为排队规则。
- class(类别)
- filter(过滤器)
- HTB (Hierarchical Token Bucket)
原理
- 网卡eth0上建立HTB队列
- 给HTB队列建立子类
- 建立过滤器,将源端口和目标端口流量定向到子类上
具体步骤
一、机器初始化(增加项,一次性)
开启内核模块:
modprobe sch_htb
lsmod |grep sch_htb
若需要开机加载该模块,在/etc/modules添加即可
echo 'sch_htb' >> /etc/modules
开启统计功能:
echo 1 >/sys/module/sch_htb/parameters/htb_rate_est
二、限速初始化(添加qdisc和默认类,一次性)
tc qdisc add dev eth0 root handle 1: htb default ffff
tc class add dev eth0 parent 1:1 classid 1:ffff htb rate 8gbit burst 25kb cburst 25kb
三、动态添加删除限速配置类
端口:8899
限速:10000b
tc class replace dev eth0 parent 1: classid 1:8899 htb rate 4gbit burst 10000b cburst 10000b
tc filter replace dev eth0 protocol ip parent 1: prio 1 u32 match ip sport 8899 0xffff flowid 1:8899
四、查看
tc -s -d class show dev eth0
五、如何卸载模块sch_htb
tc qdisc del dev eth0 root
modprobe -r sch_htb
参数解释
【Burst突发】
- 网络硬件只能在一个时间发送一个包这仅仅取决于一个硬件的速率. 链路共享软件可以利用这个能力动态产生多个连接运行在不同的速度. 所以速率和 ceil 不是一个即时度量只是一个在一个时间里发送包的平均值. 实际的情况是怎样使一个流量很小的类在某个时间类以最大的速率提供给其他类. burst 和 cburst 参数控制多少数据可以以硬件最大的速度不费力的发送给需要的其他类.
- 如果 cburst 小于一个理论上的数据包他形成的突发不会超过 ceil 速率, 同样的方法 TBF 的最高速率也是这样.
- 你可能会问, 为什么需要 bursts . 因为它可以很容易的提高向应速度在一个很拥挤的链路上.比如 WWW 流量是突发的. 你访问主页. 突发的获得并阅读. 在空闲的时间 burst 将再”charge”一次.
- 注: burst 和 cburst 至少要和其子类的值一样大.
- 建议内核版本 > 4.x
参考资料
https://wiki.debian.org/TrafficControl
http://luxik.cdi.cz/~devik/qos/htb/manual/userg.htm
https://events.static.linuxfound.org/sites/events/files/slides/Linux_traffic_control.pdf
http://www.wy182000.com/wordpress/wp-content/uploads/2013/04/components.html
https://netbeez.net/blog/how-to-use-the-linux-traffic-control
http://perthcharles.github.io/2015/06/12/tc-tutorial
https://wiki.linuxfoundation.org/networking/netem
http://www.ituring.com.cn/article/274015
https://my.oschina.net/u/990839/blog/886460
Disclaimer
- License under
CC BY-NC 4.0
- Copyright issue feedback
me#imzye.me
, replace # with @ - Not all the commands and scripts are tested in production environment, use at your own risk
- No privacy information is collected here