2012-06-18

Linux(CentOS)で利用ポートごとの帯域制御を行う

tc(traffic control)コマンドを用いると制御できるようなのでこれを使うことにしました。
この設定のwrapperとしてcbqというコマンドがあったのでこれを用いて設定します。

まずは、既存設定ファイルの確認
ls -l /etc/sysconfig/cbq/     
合計 8
-rw-r--r-- 1 root root 11  2月 23 23:32 avpkt
-rw-r--r-- 1 root root 79  2月 23 23:32 cbq-0000.example
これはサンプルなので消しちゃいました
rm -f /etc/sysconfig/cbq/*
そして、新たに設定ファイルの書き出し
cat <<"EOF" >> /etc/sysconfig/cbq/cbq-hoge
DEVICE=eth0,1000Mbit,100Mbit
RATE=100Kbit
WEIGHT=10Kbit
PRIO=1
RULE=:80
RULE=:443
RULE=:20,
RULE=:21
EOF
DEVICEで対象デバイスと速度を指定
/dev/eth0を1Gbpsのデバイスってことで設定

RATEで制限したい値を設定
KbitとなっているけれどKByte/secだそうなのでbpsの1/8の値を設定
WEIGHTはRATEの1/10の値を設定しておくと良いらしい
PRIOで複数の設定をした場合の優先順位を指定
RULEは複数定義できて、:に続けてポート番号を指定する
最後に「,」をつけるとsource port,つけないとdestination portになる模様

そして設定ファイルのコンパイル
cbq compile
find: warning: you have specified the -maxdepth option after a non-option argument (, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
...(以下略)
・・・なにやらエラーが(;´Д`)
調べてみると、findコマンドの仕様が変わってwrapperスクリプトに記述されているfindコマンドの書式ではエラーとなる模様
どんな記述になっているか確認
grep maxdepth /sbin/cbq
  -not -name '*~' -maxdepth 1 -printf "%f\n"| sort`
    -not -name '*~' -maxdepth 1| xargs sed -n 's/#.*//; \
  [ `find $CBQ_PATH -maxdepth 1 -newer $CBQ_CACHE| \
maxdepthの位置が悪いみたいなのだけど、これならなくても問題ないよ・・・ね?
てことで削除
sed -i "s/ -maxdepth 1//" /sbin/cbq
そして、コンパイルのリトライ
cbq compile
/sbin/tc qdisc del dev eth0 root
/sbin/tc qdisc add dev eth0 root handle 1 cbq bandwidth 1000Mbit avpkt 3000 cell 8
/sbin/tc class change dev eth0 root cbq weight 100Mbit allot 1514

**CBQ: class ID of cbq-hoge must be in range <0002-FFFF>!
...(以下略)
すると、また違うエラーが(;´Д`)
設定ファイルの-hogeって名前がいけないようで0002-FFFFの範囲に収めないといけないらしい?
なので0002にリネーム
mv /etc/sysconfig/cbq/cbq{-hoge,-0002}
そして、今度こそ!
cbq compile      
/sbin/tc qdisc del dev eth0 root
/sbin/tc qdisc add dev eth0 root handle 1 cbq bandwidth 1000Mbit avpkt 3000 cell 8
/sbin/tc class change dev eth0 root cbq weight 100Mbit allot 1514

/sbin/tc class add dev eth0 parent 1: classid 1:2 cbq bandwidth 1000Mbit rate 100Kbit weight 10Kbit prio 1 allot 1514 cell 8 maxburst 20 avpkt 3000 bounded
/sbin/tc qdisc add dev eth0 parent 1:2 handle 2 tbf rate 100Kbit buffer 10Kb/8 limit 15Kb mtu 1500
/sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip dport 80 0xffff classid 1:2
/sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip dport 443 0xffff classid 1:2
/sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip sport 20 0xffff classid 1:2
/sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip dport 21 0xffff classid 1:2
通ったようですヾ(*・ω・)シ
これでwgetとかやってみればdport 80の設定が効いて速度が抑えられてることが確認できます。
wget http://hoge.com/piyo.zip
0% [                 ] 1,520,399    510K/s 

0 件のコメント:

コメントを投稿