【问题标题】:TC (traffic control) in LinuxLinux 中的 TC(流量控制)
【发布时间】:2017-10-09 05:13:31
【问题描述】:

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

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 

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

【问题讨论】:

  • 那是因为tc qdisc只能控制出站流量,不能控制入站流量。我相信你可以使用filterpolice rate 来做到这一点,但我不确定。我只有在网关中使用tc 的经验,限制了客户端的速度,但不限制自身。

标签: trafficshaping


【解决方案1】:

你是否使用了这个配置中的代码:

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

【讨论】:

  • 是的,完全正确!但是配置的下载部分不起作用!!!
【解决方案2】:

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

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

希望它对你有用!

【讨论】:

    猜你喜欢
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    相关资源
    最近更新 更多