LoginSignup
0
0

Linuxでインバウンド帯域制限

Posted at

の写経です。

まず、tcコマンドを使えるようにするにはiproute-tcをインストールします。

# dnf install iproute-tc

nwlimit.shとしてファイルを保存します。

nwlimit.sh
#!/bin/bash
SWITCH=$1        # "on" or "off"
DEVICE=$2        # "eth0", "br0", etc.
RATE=$3        # for example "1000m"
 
if [ "$SWITCH" = "on" -o "$SWITCH" = "off" ]; then
   echo "--- Initialize $DEVICE and ifb0 ---"
   tc qdisc del dev $DEVICE ingress handle ffff:
   tc qdisc del dev ifb0 root handle 1: htb
   sleep 3
   rmmod ifb
else
   echo "Usage: $0 <on|off> <interface> <rate>"
fi
  
if [ "$SWITCH" = "on" ]; then
   echo "--- Load ifb module ---"
   sleep 3
   modprobe ifb
   sleep 3
   ip link set dev ifb0 up
  
   echo "--- Mirror $DEVICE to ifb0 ---"
   modprobe act_mirred
   sleep 3
   tc qdisc add dev $DEVICE ingress handle ffff:
   tc filter add dev $DEVICE parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0
  
   echo "--- Traffic Shape ifb0 ---"
   tc qdisc add dev ifb0 root handle 1: htb default 10
   tc class add dev ifb0 parent 1:1 classid 1:10 htb rate ${RATE}bit
fi
 
exit

実行権限を付与して準備完了です。

# chmod +x nwlimit.sh

10mbitに帯域制限

# ./nwlimit.sh on eth0 10m

帯域制限確認

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip install speedtest-cli
(venv) $ speedtest --no-upload
Download: 9.32 Mbit/s

帯域制限解除

# ./nwlimit.sh off eth0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0