linux 控制网卡流量控制,3. Linux网络

流量控制

流量控制(Traffic Control, tc)是Linux内核提供的流量限速、整形和策略控制机制。它以qdisc-class-filter的树形结构来实现对流量的分层控制 :

7f5b11a03226b558f12634a9ebdb32e8.png

290cd41e47f98d0fa3a846351a67a06f.png

基本组成

从上图中可以看到,tc由qdisc、fitler和class三部分组成:qdisc通过队列将数据包缓存起来,用来控制网络收发的速度

class用来表示控制策略

filter用来将数据包划分到具体的控制策略中

qdisc

qdisc通过队列将数据包缓存起来,用来控制网络收发的速度。实际上,每个网卡都有一个关联的qdisc。它包括以下几种:无分类qdisc(只能应用于root队列)[p|b]fifo:简单先进先出

pfifo_fast:根据数据包的tos将队列划分到3个band,每个band内部先进先出

red:Random Early Detection,带带宽接近限制时随机丢包,适合高带宽应用

sfq:Stochastic Fairness Queueing,按照会话对流量排序并循环发送每个会话的数据包

tbf:Token Bucket Filter,只允许以不超过事先设定的速率到来的数据包通过 , 但可能允许短暂突发流量朝过设定值

有分类qdisc(可以包括多个队列)cbq:Class Based Queueing,借助EWMA(exponential weighted moving average, 指数加权移动均值 ) 算法确认链路的闲置时间足够长 , 以达到降低链路实际带宽的目的。如果发生越限 ,CBQ 就会禁止发包一段时间。

htb:Hierarchy Token Bucket,在tbf的基础上增加了分层

prio:分类优先算法并不进行整形 , 它仅仅根据你配置的过滤器把流量进一步细分。缺省会自动创建三个FIFO类。

注意,一般说到qdisc都是指egress qdisc。每块网卡实际上还可以添加一个ingress qdisc,不过它有诸多的限制ingress qdisc不能包含子类,而只能作过滤

ingress qdisc只能用于简单的整形

如果相对ingress方向作流量控制的话,可以借助ifb( Intermediate Functional Block)内核模块。因为流入网络接口的流量是无法直接控制的,那么就需要把流入的包导入(通过 tc action)到一个中间的队列,该队列在 ifb 设备上,然后让这些包重走 tc 层,最后流入的包再重新入栈,流出的包重新出栈。

58e993b4df49fd13db5aea377bb434f8.png

filter

filter用来将数据包划分到具体的控制策略中,包括以下几种:u32:根据协议、IP、端口等过滤数据包

fwmark:根据iptables MARK来过滤数据包

tos:根据tos字段过滤数据包

class

class用来表示控制策略,只用于有分类的qdisc上。每个class要么包含多个子类,要么只包含一个子qdisc。当然,每个class还包括一些列的filter,控制数据包流向不同的子类,或者是直接丢掉。

htb示例

795fd26370b5b5febd051881925769f8.png# add qdisc

tc qdisc add dev eth0 root handle1:htb default2r2q100

# add default class

tc class add dev eth0 parent1:0classid1:1htb rate1000mbitceil1000mbit

tc class add dev eth0 parent1:1classid1:2htb prio5rate1000mbitceil1000mbit

tc qdisc add dev eth0 parent1:2handle2:pfifo limit500

# add default filter

tc filter add dev eth0 parent1:0prio5protocol ip u32

tc filter add dev eth0 parent1:0prio5handle3:protocol ip u32 divisor256

tc filter add dev eth0 parent1:0prio5protocol ip u32 ht800::match ip src192.168.0.0/16hashkey mask0x000000ffat12link3:

# add egress rules for 192.168.0.9

tc class add dev eth0 parent1:1classid1:9htb prio5rate3mbitceil3mbit

tc qdisc add dev eth0 parent1:9handle9:pfifo limit500

tc filter add dev eth0 parent1:protocol ip prio5u32 ht3:9:match ip src"192.168.0.9"flowid1:9

ifb示例# init ifb

modprobe ifb numifbs=1

ip linksetifb0 up

# redirect ingress to ifb0

tc qdisc add dev eth0 ingress handle ffff:

tc filter add dev eth0 parent ffff:protocol ip prio0u32 match u3200flowid ffff:action mirred egress redirect dev ifb0

# add qdisc

tc qdisc add dev ifb0 root handle1:htb default2r2q100

# add default class

tc class add dev ifb0 parent1:0classid1:1htb rate1000mbitceil1000mbit

tc class add dev ifb0 parent1:1classid1:2htb prio5rate1000mbitceil1000mbit

tc qdisc add dev ifb0 parent1:2handle2:pfifo limit500

# add default filter

tc filter add dev ifb0 parent1:0prio5protocol ip u32

tc filter add dev ifb0 parent1:0prio5handle4:protocol ip u32 divisor256

tc filter add dev ifb0 parent1:0prio5protocol ip u32 ht800::match ip dst192.168.0.0/16hashkey mask0x000000ffat16link4:

# add ingress rules for 192.168.0.9

tc class add dev ifb0 parent1:1classid1:9htb prio5rate3mbitceil3mbit

tc qdisc add dev ifb0 parent1:9handle9:pfifo limit500

tc filter add dev ifb0 parent1:protocol ip prio5u32 ht4:9:match ip dst"192.168.0.9"flowid1:9

参考文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值