基於OpenWRT之QOS排程|設定QOS優化家用網路

2023/10/12閱讀時間約 10 分鐘
當大量的網路封包在有限的頻寬中傳輸,總有一些服務的封包會因為搶不過其他封包而造成卡頓,例如玩遊戲時,若同時也在下載其他大型檔案,就會造成遊戲卡頓甚至斷線。這時候客製化QOS來決定哪些封包能優先被傳送,就能有效地提升指定服務的順暢度,進而提升使用者在網路上的體驗。


本篇文章接介紹如何在OpenWRT系統上設置QOS

延伸閱讀

於OpenWRT系統實現OpenVPN客户端


OpenWRT的QOS架構

要掌握QOS的設定方式,首先要了解Linux系統的netfilter、iptables、routing table及tc。

  • iptables : 屬於user space,定義iptable指令集的下法,內建三種以上不同的table可以使用,分別為1)專門進行封包過濾用的filter表、2)進行nat封包網址轉換用的nat表,以及3)進行封包修改用的mangle表。還可將符合條件的封包標記(MARK)起來,再可配合routing table去指定路由,功能非常強大。編譯出來的檔案為.so檔。
    (封包標記的實際用法可參考於OpenWRT系統實現OpenVPN客户端文章裡的配置iptables及iptables分流)
  • netfilter : 屬於Kernel space,定義iptable指令集的匹配規則,若封包匹配此規則,回傳成功。編譯出來檔案為.ko檔。
  • routing table : 定義路由表,當iptables裡的MARK與fwmark相等時,封包就會走fwmark所指定的路由表。
  • tc : 流量控管(Traffic Control),用來調整流量的配置,提供限速等功能。


iptables及netfilter的詳細用法這邊不贅述,有時間會再寫一篇介紹。


流量控管(tc)指令

以下是tc的設定方式,在指定的網卡介面中產生若干個佇列,並定義這些佇列的頻寬、優先權及handle number。假設想要把封包導到handle number為100的佇列,只要在iptables裡使用MARK來將該封包標記為100,當MARK與handle number相等時,封包就會被成功導入handle number佇列。

  1. 將指定要配置的兩張網卡名稱存到變數$DEVe及$DEVt,$DEVe為實體網卡eth0;$DEVt為VPN網卡tun0。
#!/bin/sh
DEVe=`uci get network.wan.ifname`
DEVt=tun0
  1. 清空網卡裡的tc規則
#Flush
tc qdisc del dev $DEVe root > /dev/null 2>&1
tc qdisc del dev $DEVt root > /dev/null 2>&1
  1. 建立需要的QOS佇列種類
#Classify for Up link
tc qdisc add dev $DEVt root handle 1: htb default 11 #root 1
tc qdisc add dev $DEVe root handle 1: htb default 31 #root 1
tc class add dev $DEVt parent 1: classid 1:1 htb rate 8Mbit ceil 8Mbit prio 0 #class 1.1
tc class add dev $DEVt parent 1:1 classid 1:11 htb rate 8Mbit ceil 8Mbit prio 1 #1.1.1 Game
tc class add dev $DEVe parent 1: classid 1:2 htb rate 20Mbit ceil 20Mbit prio 2 #class 1.2
tc class add dev $DEVe parent 1:2 classid 1:21 htb rate 20Mbit ceil 20Mbit prio 3 #1.2.1 youtube, live
tc class add dev $DEVe parent 1: classid 1:3 htb rate 10Mbit ceil 10Mbit prio 4 #class 1.3
tc class add dev $DEVe parent 1:3 classid 1:31 htb rate 1Mbit ceil 1Mbit prio 5 #1.3.1 SpeedTest, default
tc class add dev $DEVe parent 1:3 classid 1:32 htb rate 6Mbit ceil 6Mbit prio 6 #1.3.2 https, mail, smtp
tc class add dev $DEVe parent 1:3 classid 1:33 htb rate 2Mbit ceil 2Mbit prio 7 #1.3.3 ftp-data
tc class add dev $DEVe parent 1:3 classid 1:34 htb rate ${DL134}Kbit ceil ${DL134}Kbit prio 8 #1.3.4 80port
#Classify for Down link
#tc class add dev $DEVe parent 1: classid 1:4 htb rate 500Kbit ceil 500Kbit prio 9 #class 1.4
#tc class add dev $DEVe parent 1:4 classid 1:41 htb rate 500Kbit ceil 500Kbit prio 10 #1.4.1
echo "Class OK"
QOS佇列示意圖

QOS佇列示意圖

  1. 設定QOS的演算法SFQ(隨機公平佇列)的重置時間
#SFQ perturb 10 : reset the algorithm after 10 secs
tc qdisc add dev $DEVt parent 1:11 handle 111: sfq perturb 10
tc qdisc add dev $DEVe parent 1:21 handle 121: sfq perturb 10
tc qdisc add dev $DEVe parent 1:31 handle 131: sfq perturb 50
tc qdisc add dev $DEVe parent 1:32 handle 132: sfq perturb 50
tc qdisc add dev $DEVe parent 1:33 handle 133: sfq perturb 50
tc qdisc add dev $DEVe parent 1:34 handle 134: sfq perturb 50
echo "SFQ OK"
  1. 定義iptables裡,被MARK標記過的封包所對應的QOS佇列
#Filter for UL
tc filter add dev $DEVt parent 1:0 protocol ip prio 1 handle 1 fw classid 1:11 #1.1.1 Game
tc filter add dev $DEVe parent 1:0 protocol ip prio 2 handle 2 fw classid 1:21 #1.2.1 youtube, live
tc filter add dev $DEVe parent 1:0 protocol ip prio 3 handle 3 fw classid 1:31 #1.3.1 default
tc filter add dev $DEVe parent 1:0 protocol ip prio 4 handle 4 fw classid 1:32 #1.3.2 http, https, mail, smtp
tc filter add dev $DEVe parent 1:0 protocol ip prio 5 handle 5 fw classid 1:33 #1.3.3 ftp-data
tc filter add dev $DEVe parent 1:0 protocol ip prio 6 handle 6 fw classid 1:34 #1.3.4 80port
tc filter add dev $DEVe parent 1: prio 7 protocol ip u32 match ip dport 80 0xffff flowid 1:34
echo "filter UL OK"

#Filter for DL
#tc qdisc add dev $DEVe ingress
#tc qdisc add dev $DEVe parent 1:41 handle 141: sfq perturb 50
#tc filter add dev $DEVe parent 1:0 protocol ip prio 7 handle 7 fw police rate 100Mbit burst 100M drop flowid 1:41
#echo "filter DL OK"


設定完成後,再回去確認iptables及routing table是否都有做相對應的設置,接著就能利用tcpdump指令來觀察封包是否有被正確的導入所指定的網卡中。


感謝您耐心閱讀~
本專題將會持續收錄Violet的學習筆記,主題不一。
若有理解不對的地方,歡迎留言指教!
若喜歡我的文章,歡迎贊助支持,您的支持將會成為我繼續創作的動力!

7會員
50內容數
記錄每一次珍貴的旅行,讓回憶變成永恆 這裡沒有華麗的詞藻,卻有最真實的筆記 札記撰寫耗時,願我有動力一直寫下去!
留言0
查看全部
發表第一個留言支持創作者!