2

我正在使用此页面中的代码来限制我的上传和下载带宽。

https://help.atmail.com/hc/en-us/articles/201566464-Throttling-Bandwidth-using-Traffic-Controller-for-Linux

编码:

TC=/sbin/tc

IF=eth0             # Interface

DNLD=1mbit          # DOWNLOAD Limit

UPLD=1mbit          # UPLOAD Limit

IP=0.0.0.0     # Host IP

U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u0"

start() {

$TC qdisc add dev $IF root handle 1: htb default 30
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD
$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD
$U32 match ip dst $IP/0 flowid 1:1
$U32 match ip src $IP/0 flowid 1:2

}

stop() {

$TC qdisc del dev $IF root

}

restart() {

stop
sleep 1
start

}

show() {

$TC -s qdisc ls dev $IF

}

case "$1" in

start)

echo -n "Starting bandwidth shaping: "
start
echo "done"
;;

stop)

echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;

restart)

echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;

show)

echo "Bandwidth shaping status for $IF:"
show
echo ""
;;

*)

pwd=$(pwd)
echo "Usage: tc.bash {start|stop|restart|show}"
;;

esac

我更改了“IP=0.0.0.0”和“$IP/0”,因此它可以在任何机器上使用(具有所有 IP)。

当我用http://www.speedtest.net/之类的网站对其进行测试以查看效果时,我的上传速度为 1mbps(正如我在文件中配置的那样),但下载不听我的命令。下载带宽远高于配置值。

默认值:

eth0 的带宽整形状态:

qdisc pfifo_fast 0: root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
Sent 108 bytes 2 pkt (dropped 0, overlimits 0 requeues 0) 
backlog 0b 0p requeues 0 

过滤器后的值:

eth0 的带宽整形状态:

qdisc htb 1: root refcnt 2 r2q 10 default 30 direct_packets_stat 0 direct_qlen 1000
Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) 
backlog 0b 0p requeues 0 

有谁知道可能是什么问题?

4

2 回答 2

2

您是否在此配置中使用了代码:

TC=/sbin/tc

IF=eth0             # Interface

DNLD=1mbit          # DOWNLOAD Limit

UPLD=1mbit          # UPLOAD Limit

IP=0.0.0.0     # Host IP

U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u0"

start() {

$TC qdisc add dev $IF root handle 1: htb default 30
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD
$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD
$U32 match ip dst $IP/0 flowid 1:1
$U32 match ip src $IP/0 flowid 1:2

}

stop() {

$TC qdisc del dev $IF root

}

restart() {

stop
sleep 1
start

}

show() {

$TC -s qdisc ls dev $IF

}

case "$1" in

start)

echo -n "Starting bandwidth shaping: "
start
echo "done"
;;

stop)

echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;

restart)

echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;

show)

echo "Bandwidth shaping status for $IF:"
show
echo ""
;;

*)

pwd=$(pwd)
echo "Usage: tc.bash {start|stop|restart|show}"
;;

esac
于 2017-05-10T13:57:08.947 回答
1

我遇到了完全相同的问题,我使用这个脚本来限制下载带宽。

#!/bin/bash
export IF_INET=enp2s0
export LIMIT=300kbit

tc qdisc add dev ${IF_INET} ingress

tc filter add dev ${IF_INET} protocol ip ingress prio 2 u32 match ip dst 0.0.0.0/0 action police rate ${LIMIT} burst ${LIMIT}
tc filter add dev ${IF_INET} protocol ip ingress prio 2 u32 match ip src 0.0.0.0/0 action police rate ${LIMIT} burst ${LIMIT}

希望这对你有用 !

于 2020-12-11T09:21:29.887 回答