Linux中的TC(流量控制)

时间:2017-05-10 13:48:58

标签: trafficshaping

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

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 

有人知道可能是什么问题吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

#!/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}

希望它对您有用!