利用TC进行流量控制(Qos)

原文自: http://blog.chinaunix.net/u/9284/showart.php?id=220696

利用TC进行流量控制(Qos)

(<chsdate w:st="on" isrocdate="False" islunardate="False" day="13" month="12" year="2006">2006-12-13</chsdate>) 姜道友

前言:

我看到网络上关于linux系统中的流量控制文章很多不实用,即使参考也不能很快编写流量控制规则,且有很多错误。之前我也写过一篇文章,被很多网站引用,为感谢对我的支持,我把一个实例编写出来,供大家参考和学习(绝对实用!,绝对精华!大家可以把网上其它人的文章和我的文章比较一下就知道了!

需求:

目前公司专线带宽为<chmetcnv w:st="on" unitname="m" sourcevalue="4" hasspace="False" negative="False" numbertype="1" tcsc="0"><span lang="EN-US">4M</span></chmetcnv>,上下行对等,因近期内部人员使用P2P对网络造成较大影响,所以现在进行流量控制(大部人禁止使用P2P下载,但一些管理人员不能限制,无奈之下只有采取流量控制)。目的:即保证ERP系统的运行,又不影响其它用户的使用,换句话说,让他们没有感觉,同时达到了我们的目的。注:因为TC只能限制发送数据包,所以下载在内网卡上进行,而上传在外网卡上进行控制。

一、 下载设置

1、 规则 (eth0为内网卡,可以限制下载流量)

tc qdisc add dev eth0 root handle 2:0 htb default 30

tc class add dev eth0 parent 2:0 classid 2:1 htb rate 4Mbit burst 15k

tc class add dev eth0 parent 2:1 classid 2:10 htb rate 4Mbit burst 15k 最大流量4<chmetcnv w:st="on" unitname="m" sourcevalue="10" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; line-height: 150%;" lang="EN-US">M</span></chmetcnv>

tc class add dev eth0 parent 2:1 classid 2:20 htb rate 2000kbit ceil 2Mbit burst 15k 最大流量2M

tc class add dev eth0 parent 2:1 classid 2:30 htb rate 1000kbit ceil 1000kbit burst 15k 最大流量1M

tc qdisc add dev eth0 parent 2:10 handle 10: sfq perturb 10

tc qdisc add dev eth0 parent 2:20 handle 20: sfq perturb 10

tc qdisc add dev eth0 parent 2:30 handle 30: sfq perturb 10

U32_1="tc filter add dev eth0 protocol ip parent 2:0 prio 1 u32"

$U32_1 match ip src 192.168.9.0/24 flowid 2:10 192.168.9.0/24总公司ERP服务器所在网络)

使访问ERP服务器可用最大带宽<chmetcnv w:st="on" unitname="m" sourcevalue="4" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; color: red; line-height: 150%;" lang="EN-US"><font face="Times New Roman">4M</font></span></chmetcnv>,优先级为1

U32_2="tc filter add dev eth0 protocol ip parent 2:0 prio 2 u32"

$U32_2 match ip dst 192.168.1.172/32 flowid 2:20

$U32_2 match ip dst 192.168.1.82/32 flowid 2:20

$U32_2 match ip dst 192.168.1.200/32 flowid 2:20

以上3IP为总经办人员,限制他们下载流量为<chmetcnv w:st="on" unitname="m" sourcevalue="2" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; color: red; line-height: 150%;" lang="EN-US"><font face="Times New Roman">2M</font></span></chmetcnv>,优先级为2

tc filter add dev eth0 protocol ip parent 2:0 prio 3 u32 match ip dst 192.168.1.0/24 flowid 2:30 (因为有默认,此行可以省略,为了规则更清晰,还是设置为好)

以上只是设置默认情况下下载带宽只有1000K,即将近<chmetcnv w:st="on" unitname="m" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; color: red; line-height: 150%;" lang="EN-US">1M</span></chmetcnv>. 优先级为3

2、 注意事项:

优先级不要相同

二、 上传设置

1、 规则 (eth1为连接专线的网卡,可限制上传带宽)

iptables -F -t mangle

iptables -X -t mangle

iptables -Z -t mangle

iptables -A PREROUTING -t mangle -i eth0 -s 192.168.1.200/32 -j MARK --set-mark 1

iptables -A PREROUTING -t mangle -i eth0 -s 192.168.1.172/32 -j MARK --set-mark 1

iptables -A PREROUTING -t mangle -i eth0 -s 192.168.1.82/32 -j MARK --set-mark 1

#iptables -A PREROUTING -t mangle -i eth0 -s 192.168.1.0/24 -j MARK --set-mark 2 (可以不用此条)

tc qdisc add dev eth1 root handle 1:0 htb default 30

tc class add dev eth1 parent 1:0 classid 1:1 htb rate 4Mbit burst 15k

tc class add dev eth1 parent 1:1 classid 1:10 htb rate 4Mbit burst 15k 最大上传流量为<chmetcnv w:st="on" unitname="m" sourcevalue="4" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; line-height: 150%;" lang="EN-US">4M</span></chmetcnv>

tc class add dev eth1 parent 1:1 classid 1:20 htb rate 1000kbit ceil 2Mbit burst 15k 最大流量为<chmetcnv w:st="on" unitname="m" sourcevalue="2" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; line-height: 150%;" lang="EN-US">2M</span></chmetcnv>

tc class add dev eth1 parent 1:1 classid 1:30 htb rate 400kbit ceil 400kbit burst 15k 最大流量为400K

tc qdisc add dev eth1 parent 1:10 handle 10: sfq perturb 10

tc qdisc add dev eth1 parent 1:20 handle 20: sfq perturb 10

tc qdisc add dev eth1 parent 1:30 handle 30: sfq perturb 10

U32="tc filter add dev eth1 protocol ip parent 1:0 prio 1 u32"

tc filter add dev eth1 protocol ip parent 1:0 prio 1 u32 match ip dst 192.168.9.0/24 flowid 1:10

以上规则使用达到ERP服务器的流量不受限制,也即最大可为<chmetcnv w:st="on" unitname="m" sourcevalue="4" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; color: red; line-height: 150%;" lang="EN-US">4M</span></chmetcnv>

tc filter add dev eth1 parent 1:0 protocol ip prio 3 handle 1 fw classid 1:20

以上规则使总经办3IP地址的上传流量为<chmetcnv w:st="on" unitname="m" sourcevalue="2" hasspace="False" negative="False" numbertype="1" tcsc="0"><span style="font-size: 9pt; color: red; line-height: 150%;" lang="EN-US">2M</span></chmetcnv>

#tc filter add dev eth1 parent 1:0 protocol ip prio 4 handle 2 fw classid 1:30 (因为默认下使用此规则,所以可以省略)

默认下不符合以上两规则的全部使用此规则,上传带宽最大为400K(不要说我苛刻哟)

2、 注意事项

1) 因为内部IP地址在经过外网卡时会进行NAT,所以不能用源地址进行U32匹配,只能是FW规则进行流量标识

2) 优先级一定不能相同,否则fw规则将失效

3) 以上规则可以放到一个文件内运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值