Last active
November 6, 2018 04:06
-
-
Save dan82840/a373ecc3efe718a536c4c04fcd680ef1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Run this script under openwrt box | |
# | |
MAX_DL_RATE=1000 | |
MAX_UP_RATE=1000 | |
tc() { | |
local ret | |
/usr/sbin/tc "$@" | |
ret=$? | |
echo "[$ret] tc $@" >> /tmp/qos.log | |
return $ret | |
} | |
iptables() { | |
local ret | |
/usr/sbin/iptables "$@" | |
ret=$? | |
echo "[$ret] iptables $@" >> /tmp/qos.log | |
return $ret | |
} | |
ip() { | |
local ret | |
/usr/sbin/ip "$@" | |
ret=$? | |
echo "[$ret] ip $@" >> /tmp/qos.log | |
return $ret | |
} | |
insmod() { | |
local ret | |
/usr/sbin/insmod "$@" | |
ret=$? | |
echo "[$ret] insmod $@" >> /tmp/qos.log | |
return $ret | |
} | |
rmmod() { | |
local ret | |
/usr/sbin/rmmod "$@" | |
ret=$? | |
echo "[$ret] rmmod $@" >> /tmp/qos.log | |
return $ret | |
} | |
# $1: ifname to rate limit | |
# $2: download IFB ifname | |
# $3: upload IFB ifname | |
# $4: upload min rate (Mbit) | |
# $5: upload max rate (Mbit) | |
# $6: download min rate (Mbit) | |
# $7: download max rate (Mbit) | |
# $8: priority | |
_set_interface_qos() { | |
local ifname=$1 | |
local dl_ifname=$2 | |
local up_ifname=$3 | |
local download_min_rate=$4 | |
local download_max_rate=$5 | |
local upload_min_rate=$6 | |
local upload_max_rate=$7 | |
local priority=$8 | |
local cnt=0 | |
tc qdisc del dev $ifname root | |
tc qdisc del dev $ifname ingress | |
# Download | |
cnt=$(tc class show dev ${dl_ifname} | wc -l) | |
tc qdisc add dev ${ifname} root handle 1: htb | |
tc filter add dev ${ifname} parent 1: protocol all prio 1 u32 match u32 0 0 action connmark action mirred egress redirect dev ${dl_ifname} | |
tc class add dev ${dl_ifname} parent 1:1 classid 1:1$cnt htb rate ${download_min_rate}mbit ceil ${download_max_rate}mbit prio ${priority} | |
tc qdisc add dev ${dl_ifname} handle 1${cnt}:0 parent 1:1${cnt} sfq perturb 5 | |
tc filter add dev ${dl_ifname} pref 1 protocol ip handle 1${cnt} fw flowid 1:1${cnt} | |
iptables -t mangle -A qosmark -m physdev --physdev-in ${ifname} -j MARK --set-mark 1${cnt} | |
iptables -t mangle -A qosmark -m mark --mark 1${cnt} -j RETURN | |
# Upload | |
cnt=$(tc class show dev ${up_ifname} | wc -l) | |
tc qdisc add dev ${ifname} handle ffff: ingress | |
tc filter add dev ${ifname} parent ffff: protocol all prio 1 u32 match u32 0 0 action connmark action mirred egress redirect dev ${up_ifname} | |
tc class add dev ${up_ifname} parent 1:1 classid 1:1${cnt} htb rate ${upload_min_rate}mbit ceil ${upload_max_rate}mbit prio ${priority} | |
tc qdisc add dev ${up_ifname} handle 1${cnt}:0 parent 1:1${cnt} sfq perturb 5 | |
tc filter add dev ${up_ifname} pref 1 protocol ip handle 1${cnt} fw flowid 1:1${cnt} | |
} | |
stop_qos() { | |
rmmod ifb | |
insmod ifb numifbs=2 | |
ip link del dev ifb0 | |
ip link del dev ifb1 | |
iptables -t mangle -F qosmark | |
iptables -t mangle -D PREROUTING -j qosmark | |
iptables -t mangle -X qosmark | |
iptables -t mangle -D POSTROUTING -j CONNMARK --save-mark | |
tc qdisc del dev download-ifb root 2>/dev/null | |
tc qdisc del dev upload-ifb root 2>/dev/null | |
ip link set dev download-ifb down 2>/dev/null | |
ip link set dev upload-ifb down 2>/dev/null | |
ip link del dev download-ifb 2>/dev/null | |
ip link del dev upload-ifb 2>/dev/null | |
} | |
init_qos() { | |
ip link add dev download-ifb name download-ifb type ifb | |
ip link set dev download-ifb up | |
ip link add dev upload-ifb name upload-ifb type ifb | |
ip link set dev upload-ifb up | |
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables | |
# Download | |
tc qdisc add dev download-ifb root handle 1: htb | |
tc class add dev download-ifb parent 1: classid 1:1 htb rate 1mbit ceil ${MAX_DL_RATE}mbit | |
# Upload | |
tc qdisc add dev upload-ifb root handle 1: htb | |
tc class add dev upload-ifb parent 1: classid 1:1 htb rate 1mbit ceil ${MAX_UP_RATE}mbit | |
iptables -t mangle -N qosmark | |
iptables -t mangle -A qosmark -j CONNMARK --restore-mark | |
iptables -t mangle -A qosmark -m mark ! --mark 0 -j ACCEPT | |
iptables -t mangle -A PREROUTING -j qosmark | |
iptables -t mangle -A POSTROUTING -j CONNMARK --save-mark | |
} | |
setup_qos() { | |
rm -f /tmp/qos.log | |
stop_qos | |
init_qos | |
_set_interface_qos ath0 download-ifb upload-ifb 20 20 10 10 0 | |
_set_interface_qos ath1 download-ifb upload-ifb 40 40 20 20 1 | |
_set_interface_qos ath2 download-ifb upload-ifb 80 80 40 40 2 | |
} | |
show_qos() { | |
local ifnames="ath0 ath1 ath2" | |
echo "tc -s -d qdisc list | grep htb" | |
echo "------------------------------" | |
tc -s -d qdisc list | grep htb | |
echo "" | |
echo "tc -s -d qdisc list | grep ingress" | |
echo "----------------------------------" | |
tc -s -d qdisc list | grep ingress | |
echo "" | |
echo "tc -s -d cl show dev download-ifb" | |
echo "---------------------------------" | |
tc -s -d cl show dev download-ifb | |
echo "" | |
echo "tc -s -d cl show dev upload-ifb" | |
echo "-------------------------------" | |
tc -s -d cl show dev upload-ifb | |
echo "" | |
for i in $(echo $ifnames); do | |
echo "tc -s -d fi show dev $i" | |
echo "---------------------------" | |
tc -s -d fi show dev $i | |
echo "" | |
done | |
echo "tc -s -d fi show dev download-ifb" | |
echo "---------------------------------" | |
tc -s -d fi show dev download-ifb | |
echo "" | |
echo "tc -s -d fi show dev upload-ifb" | |
echo "-------------------------------" | |
tc -s -d fi show dev upload-ifb | |
echo "" | |
echo "iptables -t mangle -L qosmark -nv" | |
echo "----------------------------------" | |
iptables -t mangle -L qosmark -nv | |
echo "" | |
} | |
main() { | |
local argc=$1; shift | |
case "$1" in | |
"") | |
setup_qos | |
;; | |
"show") | |
show_qos | |
;; | |
esac | |
} | |
main $# "$@" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup following QoS Setting
ath0 - Download Max/Min Rate: 20M/20M , Upload Max/Min Rate: (10M/10M) , Priority 0
ath1 - Download Max/Min Rate: 40M/40M , Upload Max/Min Rate: (20M/20M) , Priority 1
ath2 - Download Max/Min Rate: 80M/80M , Upload Max/Min Rate: (40M/40M) , Priority 2