<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何在 Linux 下使用 TC 優(yōu)雅的實(shí)現(xiàn)網(wǎng)絡(luò)限流

          共 5717字,需瀏覽 12分鐘

           ·

          2022-12-18 01:05

          1. Linux 下的流量控制原理

          通過(guò)對(duì)包的排隊(duì),我們可以控制數(shù)據(jù)包的發(fā)送方式。這種控制,稱之為數(shù)據(jù)整形,shape the data,包括對(duì)數(shù)據(jù)的以下操作:

          • 增加延時(shí)
          • 丟包
          • 重新排列
          • 重復(fù)、損壞
          • 速率控制 在 qdisc-class-filter 結(jié)構(gòu)下,對(duì)流量進(jìn)行控制需要進(jìn)行三個(gè)步驟:
          • 創(chuàng)建 qdisc 隊(duì)列 上面提到 Linux 是通過(guò)包的排隊(duì)進(jìn)行流量的控制,那么首先得有一個(gè)隊(duì)列。
          • 創(chuàng)建 class 分類 class 實(shí)際上,就是劃分流量策略分類。比如劃分兩檔流量限速 10MBps、20MBbs。
          • 創(chuàng)建 filter 過(guò)濾 雖然創(chuàng)建了 class 分類,但是并沒(méi)有將任何的 IP、Port 綁定到 class 上,此時(shí)并不會(huì)有控制作用。還需要?jiǎng)?chuàng)建 filter 將指定的 IP、Port 綁定到 class 上,才能使流量控制 class 生效于資源。

          TC 是 Linux 下提供的流量控制工具,也是 Cilium/eBPF 等網(wǎng)絡(luò)組件的核心基礎(chǔ)設(shè)施之一。

          2. 限制指定 IP、Port 對(duì)本機(jī)的訪問(wèn)速度

          2.1 查看網(wǎng)卡

          ifconfig

          eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 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<link>
                  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

          2.2 配置 qdisc-class-filter

          • 創(chuàng)建 qdisc 根隊(duì)列
          tc qdisc add dev eth0 root handle 1: htb default 1
          • 創(chuàng)建第一級(jí) class 綁定所有帶寬資源 注意這里的單位是 6 MBps,也就是 48 Mbps。
          tc class add dev eth0 parent 1:0 classid 1:1 htb rate 6MBps burst 15k
          • 創(chuàng)建子分類 class 可以創(chuàng)建多個(gè)子分類,對(duì)資源的流量進(jìn)行精細(xì)化管理。
          tc class add dev eth0 parent 1:1 classid 1:10 htb rate 6MBps ceil 10MBps burst 15k

          這里 ceil 設(shè)置的是上限,正常情況下限速為 6MBps,但網(wǎng)絡(luò)空閑時(shí),可以達(dá)到 10 MBps。

          • 創(chuàng)建過(guò)濾器 filter,限制 IP
          tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 1.2.3.3 flowid 1:10

          這里對(duì) 1.2.3.4 進(jìn)行限制帶寬為 1:10,也就是 6MBps。當(dāng)然,你也可以直接給網(wǎng)段 1.2.0.0/16 加 class 策略。

          2.3 查看并清理配置

          • 查看 class 配置
          tc class show dev eth0

          class 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 eth0

          filter 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

          3. 限制本機(jī)對(duì)指定 IP、Port 的訪問(wèn)速度

          由于排隊(duì)規(guī)則主要是基于出口方向,不能對(duì)入口方向的流量(Ingress)進(jìn)行限制。因此,我們需要將流量重定向到 ifb 設(shè)備上,再對(duì) ifb 的出口流量(Egress)進(jìn)行限制,以最終達(dá)到控制的目的。

          3.1 啟用虛擬網(wǎng)卡

          • 將在 ifb 設(shè)備
          modprobe ifb numifbs=1
          • 啟用 ifb0 虛擬設(shè)備
          ip link set dev ifb0 up

          3.2 配置 qdisc-class-filter

          • 添加 qdisc
          tc qdisc add dev eth0 handle ffff: ingress
          • 重定向網(wǎng)卡流量到 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 10
          tc class add dev ifb0 parent 1:0 classid 1:1 htb rate 6Mbps
          tc class add dev ifb0 parent 1:1 classid 1:10 htb rate 6Mbps
          tc filter add dev ifb0 parent 1:0 protocol ip prio 16 u32 match ip dst 1.2.3.4  flowid 1:10

          3.3 查看并清理配置

          • 下面是限速本機(jī)對(duì)指定 IP 訪問(wèn)的監(jiān)控圖 

          進(jìn)入的流量被限制在 6 MBps 以下,而出去的流量不被限制。

          • 查看 class 配置
          tc class show dev ifb0

          class 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 ifb0

          filter 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 ingress
          tc qdisc del dev ifb0 root
          modprobe -r ifb

          4. 參考

          https://arthurchiao.art/blog/lartc-qdisc-zh/ https://serverfault.com/questions/350023/tc-ingress-policing-and-ifb-mirroring

          鏈接:https://url.hi-linux.com/UBhb0

          (版權(quán)歸原作者所有,侵刪)


          瀏覽 57
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  内射网站在线看 | 黑人操屄视频 | 国产高清激情视频 | 欧美 日韩777 | 国产第八页 |