Linux tc 命令不限制下载速度



我已经写了一个脚本,可以通过Ubuntu 18.04.5 LTS上的ts工具限制我的带宽。

问题是,即使上传速度被正确地限制在我在脚本上设置的限制范围内,下载速度也不会受到限制,我也不明白为什么。

这是链接

#!/bin/bash  
# Full path to tc binary 
TC=$(which tc)
interface=ens33
interface_speed=100mbit
ip=192.168.42.130 # The IP address bound to the interface
download_limit=512kbit
upload_limit=1mbit    

# Filter options for limiting the intended interface.
FILTER="$TC filter add dev $interface protocol ip parent 1: prio 1 u32"
function start_tc { 
tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
tc qdisc del dev $interface root; sleep 1
#[ "$?" -gt "0" ] && tc qdisc del dev $interface root; sleep 1  
# start the tc configuration
$TC qdisc add dev $interface root handle 1: htb default 30
$TC class add dev $interface parent 1: classid 1:1 htb rate $interface_speed burst 15k
$TC class add dev $interface parent 1:1 classid 1:10 htb rate $download_limit burst 15k
$TC class add dev $interface parent 1:1 classid 1:20 htb rate $upload_limit burst 15k
$TC qdisc add dev $interface parent 1:10 handle 10: sfq perturb 10
$TC qdisc add dev $interface parent 1:20 handle 20: sfq perturb 10
# Apply the filter rules

# Catch-all IP rules, which will set global limit on the server
# for all IP addresses on the server. 
$FILTER match ip dst 0.0.0.0/0 flowid 1:10
$FILTER match ip src 0.0.0.0/0 flowid 1:20
# If you want to limit the upload/download limit based on specific IP address
# you can comment the above catch-all filter and uncomment these:
#
# $FILTER match ip dst $ip/32 flowid 1:10
# $FILTER match ip src $ip/32 flowid 1:20
}
function show_status {
$TC -s qdisc ls dev $interface
}
#
# Display help 
#
function display_help {
echo "Usage: tc [OPTION]"
echo -e "tstart - Apply the tc limit"
echo -e "tstatus - Show status"
}
# Start
if [ -z "$1" ]; then
display_help
elif [ "$1" == "start" ]; then
start_tc
elif [ "$1" == "status" ]; then
show_status
fi

一个可能的解决方案可以是

$TC qdisc del dev $IF root
echo "== DELETE DONE =="
$TC qdisc add dev $IF root handle 1:0 htb default 10
$TC class add dev $IF parent 1:0 classid 1:1 htb rate $LIMIT
$TC class add dev $IF parent 1:1 classid 1:10 htb rate 5mbit ceil $LIMIT_1
$TC class add dev $IF parent 1:1 classid 1:30 htb rate 5mbit ceil $LIMIT_2
$TC filter add dev $IF protocol ip parent 1:0 prio 2 u32 match ip dst x.x.x.x flowid 1:10
$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32 match ip dst x.x.x.x flowid 1:30
echo "== SHAPING DONE =="

其中$IF是您要使用的网络接口,$TC是TC命令的路径(确保在脚本顶部设置TC=/sbin/tc,因为在您的代码中,没有路径。

PS x.x.x.x是您希望与带宽限制绑定的IP地址

最新更新