假设目前有1Mbps的下载速度,想使每个设备都有合理的下载速度,首先需要决定如何分配带宽。网络结构如下:
laptop :192.168.1.2-192.168.1.10.
desktop computer :192.168.1.11.
children's computer:192.168.1.55.
network printer:192.168.1.100.
gaming device :192.168.1.200.
gaming device需要128kbps带宽当在线游戏时,因此我们必须分配128kbps带宽给它,但是我们想当使其在下载的时候可以使用最大带宽。
children's和desktop每台拥有256kbps的带宽,并希望给予更多的带宽,如果整个带宽有余的话.
laptop和friedns将分配384kbps的带宽,尽可能使用1Mbps带宽。
在SOHO应用程序中,通常不需要为上传限制带宽分配,因此我们仅仅限制下载带宽。
需要创建1Mbps的HTB类和四个子类(child classes)。
首先,我们需要在eth1接口上创建root qdisc创建root class,网卡接口速度为100Mbps.
tc qdisc add dev eth1 root handle 1: htb
tc class add dev eth1 parent 1:0 classid 1:10 htb rate 100Mbit
创建1Mbps带宽的类,并分配给所有的设备
tc class add dev eth1 parent 1:10 classid 1:20 htb rate 1Mbit
此行创建了ID为1:20的HTB class,其父类ID为1:10
 
创建一个ID为1:100的HTB class,作为1:20的子类
tc class add dev eth1 parent 1:20 classid 1:100 htb rate 128Kbit ceil 1Mbit
为该类设置排序规则,我们在这节中使用sfq.
tc qdisc add dev eth1 parent 1:100 sfq quantum 1514b perturb 15
添加tc filter并指定哪些数据包属于这些类。
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.200 flowid 1:100
 
For the children's computer:
tc class add dev eth1 parent 1:20 classid 1:200 htb rate 256Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:200 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.55 flowid 1:200

For the desktop:
tc class add dev eth1 parent 1:20 classid 1:300 htb rate 256Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:300 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.11 flowid 1:300

For the laptop and friends, we will create a class just like the others, but we want to filter all the IP addresses in the 192.168.1.0/24 network. Since we have already created the earlier classes, IP packets for 192.168.1.200, 192.168.1.55, and 192.168.1.11 will match those classes and not this .e, even if the filter contains the whole network:
tc class add dev eth1 parent 1:20 classid 1:400 htb rate 256Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:400 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.0/24 flowid 1:400
 
 
The QoS .
#!/bin/bash
#delete root qdisc (this will destroy all classes)
tc qdisc del dev eth1 root
#attach root qdisc and create the 100Mbps root class
tc qdisc add dev eth1 root handle 1: htb
tc class add dev eth1 parent 1:0 classid 1:10 htb rate 100Mbit
#create the 1Mbps class for the whole bandwidth
tc class add dev eth1 parent 1:10 classid 1:20 htb rate 1Mbit
#Xbox - 128kbit
tc class add dev eth1 parent 1:20 classid 1:100 htb rate 128Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:100 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.200 flowid 1:100
#children - 256kbit
tc class add dev eth1 parent 1:20 classid 1:200 htb rate 256Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:200 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.55 flowid 1:200
#desktop - 256kbit
tc class add dev eth1 parent 1:20 classid 1:300 htb rate 256Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:300 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.11 flowid 1:300
#all other IPs from this class - 384kbit
tc class add dev eth1 parent 1:20 classid 1:400 htb rate 256Kbit ceil 1Mbit
tc qdisc add dev eth1 parent 1:400 sfq quantum 1514b perturb 15
tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 match ip dst 192.168.1.0/24 flowid 1:400
 
检验QOS配置
We need to verify the configuration we created by looking at the HTB classes. This is done with tc class show:
root@router :~# tc class show dev eth1
class htb 1:10 root rate 100000Kbit ceil 100000Kbit burst 126575b cburst 126575b
class htb 1:100 parent 1:20 leaf 8081: prio 0 rate 128000bit ceil 1000Kbit burst 1759b cburst 2849b
class htb 1:20 parent 1:10 rate 1000Kbit ceil 1000Kbit burst 2849b cburst 2849b
class htb 1:200 parent 1:20 leaf 8082: prio 0 rate 256000bit ceil 1000Kbit burst 1919b cburst 2849b
class htb 1:300 parent 1:20 leaf 8083: prio 0 rate 256000bit ceil 1000Kbit burst 1919b cburst 2849b
class htb 1:400 parent 1:20 leaf 8084: prio 0 rate 256000bit ceil 1000Kbit burst 1919b cburst 2849b
So we see the root class 1:10, and the 1000Kbit (1Mbit) class 1:20 having the parent 1:10, and the other classes with their bandwidths having the parent 1:20.
We need to see if packets match our classes. In case of zero traffic, we will verify
that by pinging their IP address and seeing if they made a match for the corresponding class:
root@router :~# ping -f -c 10 192.168.1.200
PING 192.168.1.200 (192.168.1.200) 56(84) bytes of data.
--- 192.168.1.200 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.198/0.210/0.300/0.033 ms, ipg/ewma 0.394/0.230 ms
root@router :~# tc -s class show dev eth1 | fgrep -A 4 1:100
class htb 1:100 parent 1:20 leaf 808d: prio 0 rate 128000bit ceil 1000Kbit burst 1759b cburst 2849b
Sent 980 bytes 10 pkts (dropped 0, overlimits 0)
lended: 10 borrowed: 0 giants: 0
tokens: 40962 ctokens: 12387
What we did was to send a flood ping with 10 packets to 192.168.1.200, and then check at the 1:100 class for the gaming device to see if those packets matched the class for the gaming device, and, as we expected, they did.