
【資料圖】
通過對包的排隊,我們可以控制數據包的發送方式。這種控制,稱之為數據整形,shape the data,包括對數據的以下操作:
增加延時丟包重新排列重復、損壞速率控制 在 qdisc-class-filter 結構下,對流量進行控制需要進行三個步驟:創建 qdisc 隊列 上面提到 Linux 是通過包的排隊進行流量的控制,那么首先得有一個隊列。創建 class 分類 class 實際上,就是劃分流量策略分類。比如劃分兩檔流量限速 10MBps、20MBbs。創建 filter 過濾 雖然創建了 class 分類,但是并沒有將任何的 IP、Port 綁定到 class 上,此時并不會有控制作用。還需要創建 filter 將指定的 IP、Port 綁定到 class 上,才能使流量控制 class 生效于資源。TC 是 Linux 下提供的流量控制工具,也是 Cilium/eBPF 等網絡組件的核心基礎設施之一。
ifconfigeth0: flags=4163mtu 1500 inet 1.1.1.1 netmask 255.255.254.0 broadcast 1.1.1.1 inet6 1::1:1:1:1 prefixlen 64 scopeid 0x20 ether 1:1:1:1:1:1 txqueuelen 1000 (Ethernet) RX packets 2980910 bytes 2662352343 (2.4 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1475969 bytes 122254809 (116.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tc qdisc add dev eth0 root handle 1: htb default 1創建第一級 class 綁定所有帶寬資源 注意這里的單位是 6 MBps,也就是 48 Mbps。
tc class add dev eth0 parent 1:0 classid 1:1 htb rate 6MBps burst 15k創建子分類 class 可以創建多個子分類,對資源的流量進行精細化管理。
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 6MBps ceil 10MBps burst 15k
這里 ceil 設置的是上限,正常情況下限速為 6MBps,但網絡空閑時,可以達到 10 MBps。
創建過濾器 filter,限制 IPtc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 1.2.3.3 flowid 1:10
這里對 1.2.3.4 進行限制帶寬為 1:10,也就是 6MBps。當然,你也可以直接給網段 1.2.0.0/16 加 class 策略。
tc class show dev eth0class htb 1:10 parent 1:1 leaf 10: prio 0 rate 48Mbit ceil 80Mbit burst 15Kb cburst 1600b class htb 1:1 root rate 48Mbit ceil 48Mbit burst 15Kb cburst 1590b查看 filter 配置
tc filter show dev eth0filter parent 1: protocol ip pref 1 u32 chain 0 filter parent 1: protocol ip pref 1 u32 chain 0 fh 800: ht divisor 1 filter parent 1: protocol ip pref 1 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:10 not_in_hw match 01020303/ffffffff at 16清理全部配置
tc qdisc del dev eth0 root
由于排隊規則主要是基于出口方向,不能對入口方向的流量(Ingress)進行限制。因此,我們需要將流量重定向到 ifb 設備上,再對 ifb 的出口流量(Egress)進行限制,以最終達到控制的目的。
modprobe ifb numifbs=1啟用 ifb0 虛擬設備
ip link set dev ifb0 up
tc qdisc add dev eth0 handle ffff: ingress重定向網卡流量到 ifb0
tc filter add dev eth0 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0添加 class 和 filter
tc qdisc add dev ifb0 root handle 1: htb default 10tc class add dev ifb0 parent 1:0 classid 1:1 htb rate 6Mbpstc class add dev ifb0 parent 1:1 classid 1:10 htb rate 6Mbpstc filter add dev ifb0 parent 1:0 protocol ip prio 16 u32 match ip dst 1.2.3.4 flowid 1:10
進入的流量被限制在 6 MBps 以下,而出去的流量不被限制。
查看 class 配置tc class show dev ifb0class htb 1:10 parent 1:1 prio 0 rate 48Mbit ceil 48Mbit burst 1590b cburst 1590b class htb 1:1 root rate 48Mbit ceil 48Mbit burst 1590b cburst 1590b查看 filter 配置
tc filter show dev ifb0filter parent 1: protocol ip pref 16 u32 chain 0 filter parent 1: protocol ip pref 16 u32 chain 0 fh 800: ht divisor 1 filter parent 1: protocol ip pref 16 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:10 not_in_hw match 01020304/ffffffff at 16清理全部配置
tc qdisc del dev eth0 ingresstc qdisc del dev ifb0 rootmodprobe -r ifb
https://arthurchiao.art/blog/lartc-qdisc-zh/ https://serverfault.com/questions/350023/tc-ingress-policing-and-ifb-mirroring
鏈接:??https://url.hi-linux.com/UBhb0??
(版權歸原作者所有,侵刪)