[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