下面的例子是一个真实的应用环境,是一个大型的自助商场,拥有一个总部,一个
贮藏室,以及其它城市的几个贮藏室。
商场是使用MSSQL数据库,远程数据库包含了详细的储备品的工作人员信息,并需要每天复制总部数据库数据,复制储备品的详细更新信息,检验价格和更新信息以便总部数据库拥有每天的详细信息,以及每个商店的库存,应用程序是由第三方软件公司开发,还没有实现数据库管理和远程存储,因此,需要访问所有的数据库在每一个商店。
所有的地点都有IP电话适配器,在下面的例子中,就像实际中的应用,H.323作为VoIP协议,SIP,IAX,MGCP以及其它VoIP协议将在经过防火墙时被略为的修改。
总部和同一城市的各店连接至同一ISP,事实上,城域网访问要比广域网更加便宜,总部拥有10M的连接至internet以及100M的城域网,贮藏室只希望拥有100M的城域网连接,而没有连接至internet,其余的贮藏室通过它们自己城市的ISP连接至internet
网络结构如下图所示:
IP地址规则如下:
总部:
路由器外网IP:1.1.1.1,内网IP:192.168.1.1,具有10M的internet连接和100M的城域网连接。
MSSQL HQ :192.168.1.2。
IP ATA:192.168.1.3。
 
站点A:
路由器外网IP为:10.10.12.1,内网IP为192.168.2.1,100M的城域网连接。
MSSQL :192.168.2.2。
IP ATA:192.168.2.3。
 
站点B:
B路由器的外网IP:1.1.2.1内网地址为:192.168.3.1,拥有一条2M的到internet的连接
MSSQL:192.168.3.2。
IP ATAs:192.168.3.3。
 
站点C:
C路由器的外网IP:1.1.3.1内网地址为:192.168.4.1,拥有一条1M的到internet的连接
MSSQL:192.168.4.2。
IP ATAs:192.168.4.3。
 
QOS流量规划:
语音流量应该具备最高的优先级,因为丢失数据包对其影响最大,我们为语音流量设置优先级为0。
数据库复制非常重要,我们将其优先级设置为1。
远程数据库开发人员必须在低延迟的情况下进行工作,我们将优先级设置为2。
中心站点与分支机构间的流量应该优先于访问internet的流量,我们将优先级别为3。
internet上的用户流量相对来说具有最低的优先级将其设为4。
 
我们将在防火墙上创建QOS脚本,这样,可以在部署其它位置时,缓解压力,
在设置QOS时,没有最好的方法,只有更好的实现我们的需求。
 
从总带宽上来看,我们将为每条线路上的VOIP ATA设备分配32kbps的带宽,以确保它们不会出现问题,这此带宽应该有最高的优先级,同时,数据库复制也是非常重要的,因此,我们将至少分配剩下的3/8的带宽,并且可以借用其它数据的带宽除了VOIP之外。
 
远程开发者不需要占用太多的带宽,我们将为其分配在除去VOIP带宽情况下况带宽的1/8,并且可以借用其它数据带宽除了VOIP数据之外。
 
现在已经分配了在除了VOIP带宽的前提下的一半带宽了,剩余的流量正常通过站点路由器,只有站点和中心的流量,余下的为访问internet的流量,余下的流量将划分给站点也中心之前优先级为3,以及到internet的流量优先级为4,具体划分方法,如下图所示:
现在可以通过结合iptables的方法来为POSTROUTING打标记(MARK)来区分不同的服务和流量。
在B和C站点的防火墙中,创建下列脚本来MARK数据。
#Flush Mangle chain
$IPT -t mangle -F

#mark packets for the Voip Device - value 0
$IPT -t mangle -A POSTROUTING -d $PREFIX.3 -j MARK --set-mark 1

#mark packets for Database replication - value 1
$IPT -t mangle -A POSTROUTING -s 192.168.1.2 -d $PREFIX.2 -j MARK --set-mark 2

#mark packets for remote DB developers - value 2
$IPT -t mangle -A POSTROUTING -s 1.1.4.1 -d $PREFIX.2 -j MARK --set-mark 3

#mark packets for HQ traffic - value 3
$IPT -t mangle -A POSTROUTING -s 192.168.1.0/24 -d $PREFIX.0/24 -j MARK --set-mark 4
 
现在数据情况如下:
0: VoIP traffic
1: Database Replication
2: Remote DB Developers
3: Headquarters traffic
4: Everything else is Internet traffic
 
mangle表中的POSTROUTING中的内容如下:
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MARK all -- 0.0.0.0/0 192.168.4.3 MARK set 0x1
MARK all -- 192.168.1.2 192.168.4.2 MARK set 0x2
MARK all -- 1.1.4.1 192.168.4.2 MARK set 0x3
MARK all -- 192.168.1.0/24 192.168.4.0/24 MARK set 0x4
 
接下来,通过下列脚本来创建HTB class。
#!/bin/bash

#Edit these lines to suit location

TOTALBW=1024 #Total DOWNLOAD bandwidth for the location
VBW=128 #Voice bandwidth in this location
PREFIX=192.168.4 #IP addresses in this location

#no more editing is required below this line

let BW=$TOTALBW-$VBW

#delete root qdisc - this will drop all classes
tc qdisc del root dev eth1

#create root qdisc
tc qdisc add dev eth1 root handle 1: htb default 12

#create root class
tc class add dev eth1 parent 1: classid 1:1 htb rate ${TOTALBW}kbit ceil ${TOTALBW}kbit

#add class, Add Qdisc, Add Filter
AC="tc class add dev eth1 parent"
AQ="tc qdisc add dev eth1 parent"
AF="tc filter add dev eth1 protocol ip parent 1:0 prio 1"

#VoIP should have the highest priority
$AC 1:1 classid 1:10 htb rate ${VBW}kbit ceil ${VBW}kbit prio 0
$AQ 1:10 handle 100: pfifo limit 5
$AF handle 1 fw classid 1:10

#Database Replication
#we allow a minimum of 3/8 of total bandwidth for replication
let DBW=3*$BW/8

$AC 1:1 classid 1:20 htb rate ${DBW}kbit ceil ${BW}kbit prio 1
$AQ 1:20 handle 200: pfifo limit 5
$AF handle 2 fw classid 1:20

#Remote DB application developers
 
#we allow a minimum of 1/8 of total bandwidth for remote developers

let RBW=$BW/8

$AC 1:1 classid 1:30 htb rate ${RBW}kbit ceil ${RBW}kbit prio 2
$AQ 1:30 handle 300: pfifo limit 5
$AF handle 3 fw classid 1:30

#traffic between HQ and this location
#we allow a minimum of 1/4 of total bandwidth for traffic with HQ

let I=$BW/4
$AC 1:1 classid 1:40 htb rate ${I}kbit ceil ${BW}kbit prio 3
$AQ 1:40 handle 400: pfifo limit 5
$AF handle 4 fw classid 1:40

#Internet Traffic for users
#we allow a minimum of 1/4 of total bandwidth for internet traffic
$AC 1:1 classid 1:50 htb rate ${I}kbit ceil ${BW}kbit prio 4
$AQ 1:50 handle 500: pfifo limit 5
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst $PREFIX.0/24 flowid 1:50
 
used $TOTALBW (X in the earlier diagram) 1024 kbps = 1 Mbps, and $VBW (Y in the same diagram) 128 kbps
 
检验队列:
sitec~# tc -s class show dev eth1
 
为了限制上传功能,我们将对mangle表下的POSTROUTING表中的数据打标记。将在防火墙上添加下列脚本。
 
#mark packets from the Voip Device - value 0
$IPT -t mangle -A PREROUTING -s $PREFIX.3 -j MARK --set-mark 1

#mark packets for Database replication - value 1
$IPT -t mangle -A PREROUTING -d 192.168.1.2 -s $PREFIX.2 -j MARK --set-mark 2

#mark packets for remote DB developers - value 2
$IPT -t mangle -A PREROUTING -d 1.1.4.1 -s $PREFIX.2 -j MARK --set-mark 3
 
#mark packets for HQ traffic - value 3
$IPT -t mangle -A PREROUTING -d 192.168.1.0/24 -s $PREFIX.0/24 -j MARK --set-mark 4
 
具体QOS脚本如下:
#!/bin/bash

#Edit these lines to suit location

TOTALBW=1024 #Total UPLOAD bandwidth for the location
VBW=128 #Voice bandwidth in this location
PREFIX=192.168.4 #IP addresses in this location

#no more editing is required below this line

let BW=$TOTALBW-$VBW

#delete root qdisc - this will drop all classes
tc qdisc del root dev eth0

#create root qdisc
tc qdisc add dev eth0 root handle 1: htb default 12

#create root class
tc class add dev eth0 parent 1: classid 1:1 htb rate ${TOTALBW}kbit ceil ${TOTALBW}kbit

#add class, Add Qdisc, Add Filter
AC="tc class add dev eth0 parent"
AQ="tc qdisc add dev eth0 parent"
AF="tc filter add dev eth0 protocol ip parent 1:0 prio 1"

#VoIP should have the highest priority
$AC 1:1 classid 1:10 htb rate ${VBW}kbit ceil ${VBW}kbit prio 0
$AQ 1:10 handle 100: pfifo limit 5
$AF handle 1 fw classid 1:10

#Database Replication
#we allow a minimum of 3/8 of total bandwidth for replication
let DBW=3*$BW/8

$AC 1:1 classid 1:20 htb rate ${DBW}kbit ceil ${BW}kbit prio 1
$AQ 1:20 handle 200: pfifo limit 5
$AF handle 2 fw classid 1:20
 
#Remote DB application developers
#we allow a minimum of 1/8 of total bandwidth for remote developers

let RBW=$BW/8

$AC 1:1 classid 1:30 htb rate ${RBW}kbit ceil ${RBW}kbit prio 2
$AQ 1:30 handle 300: pfifo limit 5
$AF handle 3 fw classid 1:30

#traffic between HQ and this location
#we allow a minimum of 1/4 of total bandwidth for traffic with HQ

let I=$BW/4
$AC 1:1 classid 1:40 htb rate ${I}kbit ceil ${BW}kbit prio 3
$AQ 1:40 handle 400: pfifo limit 5
$AF handle 4 fw classid 1:40

#Internet Traffic for users
#we allow a minimum of 1/4 of total bandwidth for internet traffic
$AC 1:1 classid 1:50 htb rate ${I}kbit ceil ${BW}kbit prio 4
$AQ 1:50 handle 500: pfifo limit 5
tc filter add dev eth0 protocol ip parent 1:0 prio 5 u32 match ip src $PREFIX.0/24 flowid 1:50
 
更多的关于HTB的配置请参考: