tc filter


 

[root@localhost dpdk-19.11]#  tc qdisc show dev enp125s0f0
qdisc pfifo_fast 0: root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
[root@localhost dpdk-19.11]# 

 

[root@localhost dpdk-19.11]# ip link list dev  enp125s0f1
3: enp125s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether b0:08:75:5f:b7:da brd ff:ff:ff:ff:ff:ff
[root@localhost dpdk-19.11]# 

 

 

 

 

[root@localhost dpdk-19.11]#  ip link set dev enp125s0f1  txqueuelen 2000
[root@localhost dpdk-19.11]# ip link list dev  enp125s0f1
3: enp125s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 2000
    link/ether b0:08:75:5f:b7:da brd ff:ff:ff:ff:ff:ff
[root@localhost dpdk-19.11]# 

 

 

 

 

[root@localhost dpdk-19.11]#  ip link set dev enp125s0f1  txqueuelen 2000
[root@localhost dpdk-19.11]# ip link list dev  enp125s0f1
3: enp125s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 2000
    link/ether b0:08:75:5f:b7:da brd ff:ff:ff:ff:ff:ff
[root@localhost dpdk-19.11]# tc qdisc replace dev enp125s0f1 root prio
[root@localhost dpdk-19.11]# ip link list dev  enp125s0f1
3: enp125s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc prio state DOWN mode DEFAULT group default qlen 2000
    link/ether b0:08:75:5f:b7:da brd ff:ff:ff:ff:ff:ff
[root@localhost dpdk-19.11]# 

 

 

 

[root@localhost dpdk-19.11]#  tc qdisc show dev  enp125s0f1
qdisc prio 8001: root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
[root@localhost dpdk-19.11]# 

 

创建HTB队列

有关队列的TC命令的一般形式为:

#tc qdisc [add|change|replace|link] dev DEV [parent qdisk-id|root][handle qdisc-id] qdisc[qdisc specific parameters]

首先,需要为网卡eth0配置一个HTB队列,使用下列命令:

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

这里,命令中的”add 表示要添加,”dev eth0 表示要操作的网卡为eth0。”root 表示为网卡eth0添加的是一个根队列。”handle 1: 表示队列的句柄为1:。”htb 表示要添加的队列为HTB队列。命令最后的”default 11 是htb特有的队列参数,意思是所有未分类的流量都将分配给类别1:11。

为根队列创建相应的类别

有关类别的TC 命令的一般形式为:

#tc class [add|change|replace] dev DEV parent qdisc-id [classid class-id] qdisc [qdisc specific parameters]

可以利用下面这三个命令为根队列1创建三个类别,分别是1:1 1、1:12和1:13,它们分别占用40、40和20mb[t的带宽。

#tc class add dev eth0 parent 1: classid 1:1 htb rate 40mbit ceil 40mbit

#tc class add dev eth0 parent 1: classid 1:12 htb rate 40mbit ceil 40mbit

#tc class add dev eth0 parent 1: cllassid 1:13 htb rate 20mbit ceil 20mbit

命令中,”parent 1:”表示类别的父亲为根队列1:。”classid1:11″表示创建一个标识为1:11的类别,”rate 40mbit”表示系统

将为该类别确保带宽40mbit,”ceil 40mbit”,表示该类别的最高可占用带宽为40mbit。

为各个类别设置过滤器

有关过滤器的TC 命令的一般形式为:

#tc filter [add|change|replace] dev DEV [parent qdisc-id|root] protocol protocol prio priority filtertype [filtertype specific parameters] flowid flow-id

由于需要将WWW、E-mail、Telnet三种流量分配到三个类别,即上述1:11、1:12和1:13,因此,需要创建三个过滤器,如下面的三个命令:

#tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 80 0xffff flowid 1:11

#tc filter add dev eth0 prtocol ip parent 1:0 prio 1 u32 match ip dport 25 0xffff flowid 1:12

#tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 23 oxffff flowid 1:13

这里,”protocol ip”表示该过滤器应该检查报文分组的协议字段。”pr[o 1″ 表示它们对报文处理的优先级是相同的,对于不同优先级的过滤器, 系统将按照从小到大的优先级。

顺序来执行过滤器, 对于相同的优先级,系统将按照命令的先后顺序执行。这几个过滤器还用到了u32选择器(命令中u32后面的部分)来匹配不同的数据流。以第一个命令为例,判断的是dport字段,如果该字段与Oxffff进行与操作的结果是8O,则”flowid 1:11″ 表示将把该数据流分配给类别1:1 1。更加详细的有关TC的用法可以参考TC 的手册页。

RTNETLINK answers: File exists

[root@localhost dpdk-19.11]# tc -s class ls dev enp125s0f0
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0 root handle 1:0 htb default 1
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0 root handle 1:0 htb default 3
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0 root handle 1:0 htb default 30
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0 root handle 9:0 htb default 30
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc del dev enp125s0f0 root
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0 root handle 1: htb default 1
[root@localhost dpdk-19.11]#

 

[root@localhost dpdk-19.11]# tc -s class ls dev  enp125s0f0
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  root handle 1:0 htb default 1
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  root handle 1:0 htb default 3
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  root handle 1:0 htb default 30
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  root handle 9:0 htb default 30
RTNETLINK answers: File exists
[root@localhost dpdk-19.11]# tc qdisc del dev enp125s0f0  root
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  root handle 1: htb default 1
[root@localhost dpdk-19.11]# tc class add dev eth0 parent 1:0 classid 1:1 htb rate 20Mbit burst 15k
Cannot find device "eth0"
[root@localhost dpdk-19.11]# tc class add dev enp125s0f0  parent 1:0 classid 1:1 htb rate 20Mbit burst 15k
[root@localhost dpdk-19.11]# tc class add dev enp125s0f0  parent 1:1 classid 1:10 htb rate 20Mbit ceil 10Mbit burst 15k
[root@localhost dpdk-19.11]# tc class add dev enp125s0f0  parent 1:1 classid 1:20 htb rate 20Mbit ceil 10Mbit burst 15k
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  parent 1:10 handle 10: sfq perturb 10
[root@localhost dpdk-19.11]# tc qdisc add dev 0  parent 1:1 classid 1:20 htb rate 20Mbit ceil 10Mbit burst 15k parent 1:20 handle 20: sfq perturb 10
Unknown qdisc "classid", hence option "1:20" is unparsable
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0 parent 1:1 classid 1:20 htb rate 20Mbit ceil 10Mbit burst 15k parent 1:20 handle 20: sfq perturb 10
Unknown qdisc "classid", hence option "1:20" is unparsable
[root@localhost dpdk-19.11]# tc qdisc add dev enp125s0f0  parent 1:20 handle 20: sfq perturb 10
[root@localhost dpdk-19.11]# 

 

 

#!/bin/bash
#针对不同的ip进行限速

#清空原有规则
tc qdisc del dev eth0 root

#创建根序列
tc qdisc add dev eth0 root handle 1: htb default 1

#创建一个主分类绑定所有带宽资源(20M)
tc class add dev eth0 parent 1:0 classid 1:1 htb rate 20Mbit burst 15k

#创建子分类
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 20Mbit ceil 10Mbit burst 15k
tc class add dev eth0 parent 1:1 classid 1:20 htb rate 20Mbit ceil 20Mbit burst 15k

#避免一个ip霸占带宽资源(git1有讲到)
tc qdisc add dev eth0 parent 1:10 handle 10: sfq perturb 10
tc qdisc add dev eth0 parent 1:20 handle 20: sfq perturb 10

#创建过滤器
#对所有ip限速
tc filter add dev eth0 protocol ip parent 1:0 prio 2 u32 match ip dst 0.0.0.0/0 flowid 1:10
#对内网ip放行
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 12.0.0.0/8 flowid 1:20

 

 

 

如何用过滤器(filters )对流量进行分类

综上,一个典型的 handle 层级如下:
 
 
     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
 

 

但不要被这棵树迷惑!不要以为内核位于树的顶点,网络位于下面。包只会通过 root qdisc 入队或出队(get enqueued and dequeued),这也是内核唯一与之交互的部分( the only thing the kernel talks to)。

一个包可能会被链式地分类如下(get classified in a chain):

1: -> 1:1 -> 1:12 -> 12: -> 12:2

 

最后到达 attach 到 class 12:2 的 qdisc 的队列。在这个例子中,树的每个“节点”( node)上都 attach 了一个 filter,每个 filter 都会给出一个判断结果,根据判断结果 选择一个合适的分支将包发送过去。这是常规的流程。但下面这种流程也是有可能的:

1: -> 12:2

在这种情况下,attach 到 root qdisc 的 filter 决定直接将包发给 12:2

 

http://arthurchiao.art/blog/lartc-qdisc-zh/

 

 

TC(Traffic Control)命令—linux自带高级流控

https://cloud.tencent.com/developer/article/1409664

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM