33.7. tc-ctinfo ユーティリティーを使用したパケットのレート制限の設定
レート制限を使用すると、特定の時間枠内の反復的なパケット要求を制限することで、ネットワークトラフィックを制限し、ネットワーク内のリソースの枯渇を防ぎ、サーバーの負荷を軽減できます。
接続追跡エントリーには、Netfilter マークと接続情報が格納されます。ルーターがファイアウォールからパケットを転送するとき、ルーターはパケットから接続追跡エントリーを削除または変更します。接続追跡情報 (ctinfo) モジュールは、接続追跡マークからさまざまなフィールドにデータを取り出します。このカーネルモジュールは、Netfilter マークを、ソケットバッファー (skb) マークメタデータフィールドにコピーすることで保存します。
前提条件
-
iperf3ユーティリティーがサーバーとクライアントにインストールされている。
手順
サーバーで次の手順を実行します。
ネットワークインターフェイスに仮想リンクを追加します。
# ip link add name ifb4eth0 numtxqueues 48 numrxqueues 48 type ifbこのコマンドには次のパラメーターがあります。
name ifb4eth0- 新しい仮想デバイスインターフェイスを設定します。
numtxqueues 48- 送信キューの数を設定します。
numrxqueues 48- 受信キューの数を設定します。
type ifb- 新しいデバイスのタイプを設定します。
インターフェイスの状態を変更します。
# ip link set dev ifb4eth0 up物理ネットワークインターフェイスに
qdisc属性を追加し、それを受信トラフィックに適用します。# tc qdisc add dev enp1s0 handle ffff: ingresshandle ffff:オプションでは、handleパラメーターはデフォルト値としてメジャー番号ffff:をenp1s0物理ネットワークインターフェイス上のクラスフルqdiscに割り当てます。ここで、qdiscは、トラフィック制御を分析するためのキューイング規則パラメーターになります。パケットを分類するために、
ipプロトコルの物理インターフェイスにフィルターを追加します。# tc filter add dev enp1s0 parent ffff: protocol ip u32 match u32 0 0 action ctinfo cpmark 100 action mirred egress redirect dev ifb4eth0このコマンドには次の属性があります。
parent ffff:-
親
qdiscのメジャー番号ffff:を設定します。 u32 match u32 0 0-
u32パターンの IP ヘッダーと一致 (match) するように、u32フィルターを設定します。最初の0は、IP ヘッダーの 2 番目のバイトを表し、もう 1 つの0は、一致させるビットがどれであるかをフィルターに指示するマスク照合です。 action ctinfo- 接続追跡マークからさまざまなフィールドにデータを取り出すアクションを設定します。
cpmark 100-
接続追跡マーク (connmark)
100をパケットの IP ヘッダーフィールドにコピーします。 action mirred egress redirect dev ifb4eth0-
actionをmirredに設定して、受信したパケットを宛先インターフェイスifb4eth0にリダイレクトします。
クラスフル
qdiscをインターフェイスに追加します。# tc qdisc add dev ifb4eth0 root handle 1: htb default 1000このコマンドは、メジャー番号
1を rootqdiscに設定し、マイナー ID1000のクラスフルqdiscを持つhtb階層トークンバケットを使用します。インターフェイス上のトラフィックを 1 Mbit/s に制限し、上限を 2 Mbit/s にします。
# tc class add dev ifb4eth0 parent 1:1 classid 1:100 htb ceil 2mbit rate 1mbit prio 100このコマンドには次のパラメーターがあります。
parent 1:1-
classidを1、rootを1としてparentを設定します。 classid 1:100-
classidを1:100に設定します。ここで、1は親qdiscの数で、100は親qdiscのクラスの数です。 htb ceil 2mbit-
htbのクラスフルなqdiscでは、ceilレート制限として2 Mbit/sの上限帯域幅が許可されます。
クラスレス
qdiscの Stochastic Fairness Queuing (sfq) を、60秒の時間間隔でインターフェイスに適用して、キューアルゴリズムの摂動を軽減します。# tc qdisc add dev ifb4eth0 parent 1:100 sfq perturb 60ファイアウォールマーク (
fw) フィルターをインターフェイスに追加します。# tc filter add dev ifb4eth0 parent 1:0 protocol ip prio 100 handle 100 fw classid 1:100接続マーク (
CONNMARK) からパケットのメタマークを復元します。# nft add rule ip mangle PREROUTING counter meta mark set ct markこのコマンドでは、
nftユーティリティーにはチェーンルール仕様PREROUTINGを含むmangleテーブルがあり、ルーティング前に受信パケットを変更してパケットマークをCONNMARKに置き換えます。nftテーブルとチェーンが存在しない場合は、以下のようにテーブルを作成してチェーンルールを追加します。# nft add table ip mangle # nft add chain ip mangle PREROUTING {type filter hook prerouting priority mangle \;}指定された宛先アドレス
192.0.2.3で受信したtcpパケットにメタマークを設定します。# nft add rule ip mangle PREROUTING ip daddr 192.0.2.3 counter meta mark set 0x64パケットマークを接続マークに保存します。
# nft add rule ip mangle PREROUTING counter ct mark set mark-sパラメーターを使用して、iperf3ユーティリティーをシステム上のサーバーとして実行すると、サーバーはクライアント接続の応答を待ちます。# iperf3 -s
クライアント上で、
iperf3をクライアントとして実行し、IP アドレス192.0.2.3で定期的な HTTP 要求と応答のタイムスタンプをリッスンするサーバーに接続します。# iperf3 -c 192.0.2.3 -t TCP_STREAM | tee rate192.0.2.3はサーバーの IP アドレスで、192.0.2.4はクライアントの IP アドレスです。Ctrl+C を押して、サーバー上の
iperf3ユーティリティーを終了します。Accepted connection from 192.0.2.4, port 52128 [5] local 192.0.2.3 port 5201 connected to 192.0.2.4 port 52130 [ID] Interval Transfer Bitrate [5] 0.00-1.00 sec 119 KBytes 973 Kbits/sec [5] 1.00-2.00 sec 116 KBytes 950 Kbits/sec ... [ID] Interval Transfer Bitrate [5] 0.00-14.81 sec 1.51 MBytes 853 Kbits/sec receiver iperf3: interrupt - the server has terminatedCtrl+C を押して、クライアント上の
iperf3ユーティリティーを終了します。Connecting to host 192.0.2.3, port 5201 [5] local 192.0.2.4 port 52130 connected to 192.0.2.3 port 5201 [ID] Interval Transfer Bitrate Retr Cwnd [5] 0.00-1.00 sec 481 KBytes 3.94 Mbits/sec 0 76.4 KBytes [5] 1.00-2.00 sec 223 KBytes 1.83 Mbits/sec 0 82.0 KBytes ... [ID] Interval Transfer Bitrate Retr [5] 0.00-14.00 sec 3.92 MBytes 2.35 Mbits/sec 32 sender [5] 0.00-14.00 sec 0.00 Bytes 0.00 bits/sec receiver iperf3: error - the server has terminated
検証
インターフェイス上の
htbクラスとsfqクラスのパケット数に関する統計情報を表示します。# tc -s qdisc show dev ifb4eth0 qdisc htb 1: root ... Sent 26611455 bytes 3054 pkt (dropped 76, overlimits 4887 requeues 0) ... qdisc sfq 8001: parent ... Sent 26535030 bytes 2296 pkt (dropped 76, overlimits 0 requeues 0) ...mirredアクションとctinfoアクションのパケット数の統計情報を表示します。# tc -s filter show dev enp1s0 ingress filter parent ffff: protocol ip pref 49152 u32 chain 0 filter parent ffff: protocol ip pref 49152 u32 chain 0 fh 800: ht divisor 1 filter parent ffff: protocol ip pref 49152 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 terminal flowid not_in_hw (rule hit 8075 success 8075) match 00000000/00000000 at 0 (success 8075 ) action order 1: ctinfo zone 0 pipe index 1 ref 1 bind 1 cpmark 0x00000064 installed 3105 sec firstused 3105 sec DSCP set 0 error 0 CPMARK set 7712 Action statistics: Sent 25891504 bytes 3137 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0 action order 2: mirred (Egress Redirect to device ifb4eth0) stolen index 1 ref 1 bind 1 installed 3105 sec firstused 3105 sec Action statistics: Sent 25891504 bytes 3137 pkt (dropped 0, overlimits 61 requeues 0) backlog 0b 0p requeues 0htbレートリミッターとその設定の統計情報を表示します。# tc -s class show dev ifb4eth0 class htb 1:100 root leaf 8001: prio 7 rate 1Mbit ceil 2Mbit burst 1600b cburst 1600b Sent 26541716 bytes 2373 pkt (dropped 61, overlimits 4887 requeues 0) backlog 0b 0p requeues 0 lended: 7248 borrowed: 0 giants: 0 tokens: 187250 ctokens: 93625