Linux流量控制(TC)之表面(3)

分类只存在于可分类排队规则(classful qdisc)(例如,HTB和CBQ)中。分类可以很复杂,它可以包含多个子分类,也可以只包含一个子qdisc。在超级复杂的流量控制应用场景中,一个类中再包含一个可分类qdisc也是可以的。

任何一个分类都可以和任意多个filter相关联,这样就可以选择一个子分类或运用一个filter来重新排列或丢弃进入分类中的数据包。

叶子分类是qdisc中的最后一个分类,它包含一个qdisc(默认是pfifo)并且不包含任意子分类。任何包含子分类的分类都是内部分类而不是子分类。

Linux的过滤器可以允许用户利用一个或多个过滤器将数据包分类至输出队列上。它包含了一个分类器实现,常见的分类器如u32,u32分类器可以允许用户基于数据包的属性来选择数据包。

无论是qdisc,还是class, 都需要有一个唯一标识符。就是所说的句柄。它们都采用major:minor格式来命名,注意他们都是以十六进制解析。对于他们的使用,在栗子中会做具体说明。

接下来我们主要介绍一下classful qdisc的情况。看一下数据包的流程。

flow within classful qdisc & class

When traffic enters a classful qdisc, it needs to be sent to any of the classes within - it needs to be 'classified'. To determine what to do with a packet, the so called 'filters' are consulted. It is important to know that the filters are called from within a qdisc, and not the other way around!

The filters attached to that qdisc then return with a decision, and the qdisc uses this to enqueue the packet into one of the classes. Each subclass may try other filters to see if further instructions apply. If not, the class enqueues the packet to the qdisc it contains.

Besides containing other qdiscs, most classful qdiscs also perform shaping. This is useful to perform both packet scheduling (with SFQ, for example) and rate control. You need this in cases where you have a high speed interface (for example, ethernet) to a slower device (a cable modem).

How filters are used to classify traffic

Recapping, a typical hierarchy might look like this:

1: root qdisc | 1:1 child class / | \ / | \ / | \ / | \ 1:10 1:11 1:12 child classes | | | | 11: | leaf class | | 10: 12: qdisc / \ / \ 10:1 10:2 12:1 12:2 leaf classes

​ But don't let this tree fool you! You should not imagine the kernel to be at the apex of the tree and the network below, that is just not the case. Packets get enqueued and dequeued at the root qdisc, which is the only thing the kernel talks to.

​ A packet might get classified in a chain like this: 1: -> 1:1 -> 1:12 -> 12: -> 12:2

​ The packet now resides in a queue in a qdisc attached to class 12:2. In this example, a filter was attached to each 'node' in the tree, each choosing a branch to take next. This can make sense. However, this is also possible: 1: -> 12:2

​ In this case, a filter attached to the root decided to send the packet directly to 12:2.

How packets are dequeued to the hardware

When the kernel decides that it needs to extract packets to send to the interface, the root qdisc 1: gets a dequeue request, which is passed to 1:1, which is in turn passed to 10:, 11: and 12:, each of which queries its siblings, and tries to dequeue() from them. In this case, the kernel needs to walk the entire tree, because only 12:2 contains a packet.

In short, nested classes ONLY talk to their parent qdiscs, never to an interface. Only the root qdisc gets dequeued by the kernel!

The upshot of this is that classes never get dequeued faster than their parents allow. And this is exactly what we want: this way we can have SFQ in an inner class, which doesn't do any shaping, only scheduling, and have a shaping outer qdisc, which does the shaping.

1.3.3 HTB的配置使用

HTB是一种classful qdisc,是一种分层分类流控方法,是Linux常用的一种流控配置。接下来就来看一下使用配置:

配置HTB需要四个步骤:

创建root qdisc

创建class

创建filter,关联到class

添加leaf class disc(非必需)

#tc qdisc add dev eth0 root handle 1: htb default 30 //添加root qdisc, 1:是 1:0的简写 #tc class add dev eth0 parent 1: classid 1:1 htb rate 6mbit burst 15k //以根1:为根,创建class #tc class add dev eth0 parent 1:1 classid 1:10 htb rate 5mbit burst 15k #tc class add dev eth0 parent 1:1 classid 1:20 htb rate 3mbit ceil 6mbit burst 15k #tc class add dev eth0 parent 1:1 classid 1:30 htb rate 1kbit ceil 6mbit burst 15k #tc qdisc add dev eth0 parent 1:10 handle 10: sfq perturb 10 //为leaf class添加qdisc,默认为pfifo #tc qdisc add dev eth0 parent 1:20 handle 20: sfq perturb 10 #tc qdisc add dev eth0 parent 1:30 handle 30: sfq perturb 10 # 添加过滤器 , 直接把流量导向相应的类 : #U32="tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32" #$U32 match ip dport 80 0xffff flowid 1:10 //关联filter到class #$U32 match ip sport 25 0xffff flowid 1:20

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11708.html