tc 简介
tc 并不是一个单独的指令,它是隶属于一个家族,iproute2 中的一个成员,与 tc 同属于同一个家族的程序还有大名鼎鼎的 ip 指令。
Linux 内核将 iproute2 称做是 Traffic Control 体系,事实上 iproute2 中的主要两个指令 ip 以及 tc 是相互协调工作的。iproute2实际上主要的应用对象是路由器以及交换机,在今天许多路由器以及交换机或多或少都借鉴了 iproute2 的实现。
tc 则是属于 iproute2 中的 流量控制 部分,正如 tc 的全称 “traffic control” 。有关于 tc 的介绍现存的资料不仅又少而且又旧,经过近一周的努力,在这里笔者自己给出tc的定义,tc 是一个我们用户态能够直接接触到的命令行工具,tc的工作实际上就是简单的解析一下我们的指令,并且根据解析的结果将内容交付给内核模块。所以,在此可以简单的认为tc是一个比较复杂的"gui",之所以把它称做为"gui",是因为它实际上不参与任何实现。(这也很真实,在内核模块缺少的情况下,tc也能够正常的编译)。
tc 使用帮助
NAME
tc # − 显示/操作 流量控制的设置
SYNOPSIS
tc qdisc [ add | change | replace | link ] dev DEV [ parent qdisc-id | root ] [ handle qdisc-id ] qdisc [ qdisc specific parameters ]
tc class [ add | change | replace ] dev DEV parent qdisc-id [ classid class-id ] qdisc [ qdisc specific parameters ]
tc filter [ add | change | replace ] dev DEV [ parent qdisc-id | root ] protocol protocol prio priority filtertype [ filtertype specific parameters ] flowid flow-id
tc [-s | -d ] qdisc show [ dev DEV ]
tc [-s | -d ] class show dev DEV
tc filter show dev DEV
DESCRIPTION
tc 用于 Linux内核 的流量控制配置。流量控制包含以下内容:
SHAPING
当流量被重塑时,网络的传输速率将会在控制之下。重塑不仅仅能够减低可用的网络
带宽————它还能用于平滑网络突发的情况,从而改善网络的状况。重塑发生在
egress 处。
SCHEDULING
通过重新调整 packets 的传输调度策略,这可能能够改善网络的交互性,同时仍然
保证批量传输的带宽。重新排序也被称为优先级排序,只发生在 egress 处。
POLICING
当流量控制被重塑时,policing 也会参与到流量控制中。 Policiing 仅仅发生在
ingress。
DROPPING
可以设置流量的带宽,当流量超过设定值时,则立即丢弃。egress 和 ingress 处
均发生。
QDISCS
qdisc 是 queueing discipline 的简称, qdisc 是理解流量控制的基础。每当内核
需要发送一个 packet 到接口时,packet 都会入队到与接口绑定的 qdisc。紧接着,
内核会从 qdisc 获取尽可能多的数据包,并将它们提供给网络适配器驱动程序。
一个简单的 qdisc 是 pfifo 型,它根本不进行任何处理,是一个纯粹的先进先出队
列。但是,当网络接口无法立即处理流量时,它会存储流量。
CLASSES
一些 qdiscs 可以包含类 ———— 流量可以排队到内部任意某个 class 里,也就是在
qdiscs 中。当内核试图从一个 具有类的qdiscs 中取出一个 packet
时,这个 packet 可能来自于 qdiscs 中的任意一个类。
FILTERS
一个 具有类的qdiscs 使用过滤器来决定 packet 究竟该入队到哪一个类。每当流量
到达一个具有子类的类时,都需要对其进行分类。许多办法都可以达到这样一个效果,
其中的一个方案就是使用过滤器。所有附加在类上的过滤器都会被调用,直到一个
过滤器返回结果才停止,也有可能存在其他的标准根据具体的 qdiscs。
又一点是值得注意的,就是过滤器是不知道发生了什么事情的,它们只是依附在
qdiscs 上而已。
CLASSLESS QDISCS
具有类的 qdisc 分别是:
[p|b]fifo
Simplest usable qdisc, pure First In, First Out behaviour. Limited in packets or in bytes.
pfifo_fast
Standard qdisc for ’Advanced Router’ enabled kernels. Consists of a three-band queue which honors Type of Service flags, as well as the priority that may be assigned to a packet.
red Random Early Detection simulates physical congestion by randomly dropping packets when nearing configured bandwidth allocation. Well suited to very large bandwidth applications.
sfq Stochastic Fairness Queueing reorders queued traffic so each ’session’ gets to send a packet in turn.
tbf The Token Bucket Filter is suited for slowing traffic down to a precisely configured rate. Scales well to large bandwidths.
CONFIGURING CLASSLESS QDISCS
在 无类的qdiscs 的情况下,无类的qdiscs 只能连接在设备的根目录下。完整语法:
tc qdisc add dev DEV root QDISC QDISC-PARAMETERS
在没有配置qdisc的情况下,pfifo_fast qdisc是自动默认值。
CLASSFUL QDISCS
有类的qdisc 分别是:
CBQ Class Based Queueing implements a rich linksharing hierarchy of classes. It contains shaping elements as well as prioritizing capabilities. Shaping is performed using link idle time calculations based on average packet size and underlying link bandwidth. The latter may be ill-defined for some interfaces.
HTB The Hierarchy Token Bucket implements a rich linksharing hierarchy of classes with an emphasis on conforming to existing practices. HTB facilitates guaranteeing bandwidth to classes, while also allowing specification of upper limits to inter-class sharing. It contains shaping elements, based on TBF and can prioritize classes.
PRIO The PRIO qdisc is a non-shaping container for a configurable number of classes which are dequeued in order. This allows for easy prioritization of traffic, where lower classes are only able to send if higher ones have no packets available. To facilitate configuration, Type Of Service bits are honored by default.
忠告
此章的第二部分翻译 man tc, 对于一个接触于 tc的人来说,专研于此是毫无意义的,因为这不是一个简单的工具,它需要你了解它的原理,否则你将无法很好地使用它。
但是你应该也已经注意到了几个重要的名词了,他们分别是:
- qdiscs : 队列记录,全称为 “queue discipline”
- filters : 分类器
- class : 类
以上这些名词都会有专门的章节来讲解。