Linux:ネットワークの帯域制御をtcコマンドで!

Linuxサーバーでネットワークトラフィックを制御したいってとき、「tc」コマンドで実現ができます。

今回は、トラフィック発生コマンドの「iperf3」を利用して、設定を確認してみました。

目次

準備します

iperf3」はサーバー/クライアントで動作します。

今回は、出力の方の制御を確認したかったので、トラフィックを受ける側(サーバー)のPCを準備します。
※今回はCentOS7を準備しました。クライアントはubuntu20です。

・クライアント側
$ iperf3 -c サーバーのアドレス

・サーバー側
$ iperf3 -s

・ファイアウォールの設定

ポートとして、5201 / tcp,udp を利用しますのでfirewallやufwなどで制限されている場合は開けといてください。

・サーバー側のfirewalld設定
# firewall-cmd –add-port=5201/tcp
# firewall-cmd –add-port=5201/udp

測定中だけ必要なんで、ランタイムに設定して、終わったらfirewalldを再起動する。

・クライアント側のufwの設定
$ sudo ufw allow 5201/tcp
$ sudo ufw allow 5201/udp

測定を終わったら、番号指定で削除します。
$ sudo ufw status numbered
ufw delete 2

帯域制御をやってみましょう!

制御をしない状態を計測してみます。

まずは、素のままの状態を図ってみます。

・サーバー側
$ iperf3 -s
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
Accepted connection from xxx.xxx.xxx.xxx, port 41922
[ 5] local xxx.xx.xxx.xxx port 5201 connected to xxx.xxx.xxx.xxx port 41924
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-1.00 sec 11.0 MBytes 91.9 Mbits/sec
[ 5] 1.00-2.00 sec 11.4 MBytes 95.7 Mbits/sec
[ 5] 2.00-3.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 3.00-4.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 4.00-5.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 5.00-6.00 sec 11.4 MBytes 95.7 Mbits/sec
[ 5] 6.00-7.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 7.00-8.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 8.00-9.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 9.00-10.00 sec 11.4 MBytes 95.6 Mbits/sec
[ 5] 10.00-10.04 sec 505 KBytes 95.4 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-10.04 sec 0.00 Bytes 0.00 bits/sec sender
[ 5] 0.00-10.04 sec 114 MBytes 95.3 Mbits/sec receiver

・クライアント側
$ iperf3 -c サーバー側アドレス
Connecting to host xxxx.xxx.xxx.xxx, port 5201
[ 4] local xxxx.xxx.xxx.xxx port 41924 connected to xxxx.xxx.xxx.xxx port 5201
[ ID] Interval Transfer Bandwidth Retr Cwnd
[ 4] 0.00-1.00 sec 13.9 MBytes 116 Mbits/sec 192 157 KBytes
[ 4] 1.00-2.00 sec 11.4 MBytes 95.9 Mbits/sec 0 206 KBytes
[ 4] 2.00-3.00 sec 11.4 MBytes 95.4 Mbits/sec 0 245 KBytes
[ 4] 3.00-4.00 sec 11.4 MBytes 95.9 Mbits/sec 11 136 KBytes
[ 4] 4.00-5.00 sec 11.4 MBytes 95.4 Mbits/sec 0 189 KBytes
[ 4] 5.00-6.00 sec 11.4 MBytes 95.9 Mbits/sec 0 232 KBytes
[ 4] 6.00-7.00 sec 11.4 MBytes 95.4 Mbits/sec 3 266 KBytes
[ 4] 7.00-8.00 sec 11.4 MBytes 95.4 Mbits/sec 21 146 KBytes
[ 4] 8.00-9.00 sec 11.4 MBytes 95.9 Mbits/sec 0 198 KBytes
[ 4] 9.00-10.00 sec 11.4 MBytes 95.4 Mbits/sec 0 238 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.00 sec 117 MBytes 97.7 Mbits/sec 227 sender
[ 4] 0.00-10.00 sec 114 MBytes 95.6 Mbits/sec receiver

まぁ、100Mbpsの速度が出ています(^^)

帯域制御をしてみます

tcコマンドのざっとした説明

制限のパターン
tbf:トークンバケットフィルター
パケットシェーピング機能
制限レートを超えたパケットはキューに保留されます。
htb:ヒエラルキートークンバケット
各サービスに最低限必要な帯域を割り当てることができます。
1つのリンクを複数のサービス(www,ftp等)で共有する

今回は tbf を利用します。
※他にもありますので、調べてみてください(^^)

50mbit(50Mbps)に制限します。

$ sudo tc qdisc add dev ens3 root handle 1:0 tbf rate 50mbit burst 25kb limit 250kb
※50mbpsって指定すると、50Mbyte/secとなります(^^;

・burstはこんな計算式

burst(byte) = rate(byte) / HZ
burst = (50 * 10^6 / 8) / 250 = 25 * 10^3

HZはこうやって確認します。
$ cat /boot/config-uname -r | grep ‘CONFIG_HZ=’
CONFIG_HZ=250

帯域制御中の測定値

$ iperf3 -c xxx.xxx.xxx.xxx
Connecting to host xxx.xxx.xxx.xxx, port 5201
[ 4] local xxx.xxx.xxx.xxx port 42682 connected to xxx.xxx.xxx.xxx port 5201
[ ID] Interval Transfer Bandwidth Retr Cwnd
[ 4] 0.00-1.00 sec 4.99 MBytes 41.7 Mbits/sec 0 93.3 KBytes
[ 4] 1.00-2.00 sec 4.16 MBytes 34.9 Mbits/sec 0 93.3 KBytes
[ 4] 2.00-3.00 sec 4.23 MBytes 35.4 Mbits/sec 0 93.3 KBytes
[ 4] 3.00-4.00 sec 4.35 MBytes 36.5 Mbits/sec 0 97.6 KBytes
[ 4] 4.00-5.00 sec 4.23 MBytes 35.4 Mbits/sec 0 97.6 KBytes
[ 4] 5.00-6.00 sec 4.10 MBytes 34.4 Mbits/sec 0 97.6 KBytes
[ 4] 6.00-7.00 sec 4.29 MBytes 36.0 Mbits/sec 0 97.6 KBytes
[ 4] 7.00-8.00 sec 4.10 MBytes 34.4 Mbits/sec 0 97.6 KBytes
[ 4] 8.00-9.00 sec 4.23 MBytes 35.4 Mbits/sec 0 97.6 KBytes
[ 4] 9.00-10.00 sec 4.23 MBytes 35.6 Mbits/sec 0 97.6 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.00 sec 42.9 MBytes 36.0 Mbits/sec 0 sender
[ 4] 0.00-10.00 sec 42.3 MBytes 35.5 Mbits/sec receiver

50Mbitsあたりに収まってくれてます。

終了したら帯域制御を解除しときましょう

$ sudo tc qdisc del dev ens3 root

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です