2012-06-18

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

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

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

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

そして設定ファイルのコンパイル
  1. cbq compile  
  2. 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.  
  3. ...(以下略)  
・・・なにやらエラーが(;´Д`)
調べてみると、findコマンドの仕様が変わってwrapperスクリプトに記述されているfindコマンドの書式ではエラーとなる模様
どんな記述になっているか確認
  1. grep maxdepth /sbin/cbq  
  2.   -not -name '*~' -maxdepth 1 -printf "%f\n"| sort`  
  3.     -not -name '*~' -maxdepth 1| xargs sed -n 's/#.*//; \  
  4.   [ `find $CBQ_PATH -maxdepth 1 -newer $CBQ_CACHE| \  
maxdepthの位置が悪いみたいなのだけど、これならなくても問題ないよ・・・ね?
てことで削除
  1. sed -i "s/ -maxdepth 1//" /sbin/cbq  
そして、コンパイルのリトライ
  1. cbq compile  
  2. /sbin/tc qdisc del dev eth0 root  
  3. /sbin/tc qdisc add dev eth0 root handle 1 cbq bandwidth 1000Mbit avpkt 3000 cell 8  
  4. /sbin/tc class change dev eth0 root cbq weight 100Mbit allot 1514  
  5.   
  6. **CBQ: class ID of cbq-hoge must be in range <0002-FFFF>!  
  7. ...(以下略)  
すると、また違うエラーが(;´Д`)
設定ファイルの-hogeって名前がいけないようで0002-FFFFの範囲に収めないといけないらしい?
なので0002にリネーム
  1. mv /etc/sysconfig/cbq/cbq{-hoge,-0002}  
そして、今度こそ!
  1. cbq compile        
  2. /sbin/tc qdisc del dev eth0 root  
  3. /sbin/tc qdisc add dev eth0 root handle 1 cbq bandwidth 1000Mbit avpkt 3000 cell 8  
  4. /sbin/tc class change dev eth0 root cbq weight 100Mbit allot 1514  
  5.   
  6. /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  
  7. /sbin/tc qdisc add dev eth0 parent 1:2 handle 2 tbf rate 100Kbit buffer 10Kb/8 limit 15Kb mtu 1500  
  8. /sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip dport 80 0xffff classid 1:2  
  9. /sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip dport 443 0xffff classid 1:2  
  10. /sbin/tc filter add dev eth0 parent 1:0 protocol ip prio 100 u32 match ip sport 20 0xffff classid 1:2  
  11. /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の設定が効いて速度が抑えられてることが確認できます。
  1. wget http://hoge.com/piyo.zip  
  2. 0% [                 ] 1,520,399    510K/s   

0 件のコメント:

コメントを投稿