RHEL6如何限制自己某个端口的上传带宽
juaby 2011-11-28 09:30:03 需求背景:
某项目,全国各地区部署分服务器,中心部署总服务器,分服务器24小时不间断的向中心服务器传输文件,单个 文件150K左右,地区到中心网络带宽2M,要求地区服务器向中心服务器传输文件所占用的带宽在512Kb以内,512/8,根据此需求,我在互联网搜肠刮肚,找到了wondershaper,其实就是iptables+tc的脚本,我目前是可以限制上传下载的带宽了,但是不幸的是,地区分服务器也是服务器,也部署了应用软件,客户端和他通信的时候,由于带宽限制了 ,所以很慢,wondershaper能否仅仅针对某个端口的上传带宽进行限制,望各位大侠指点,下面是我所做的,
操作系统:RHEL6
目标软件:wondershaper_1.1a-6_all.deb
依赖软件:
http://kitenet.net/~joey/code/alien/,下载alien_8.86.tar.gz
perl.x86_64 perl-devel.x86_64 (解决MakeMake.PL)(64位OS,32位同理)
安装步骤:
第一步,下载需要的软件及依赖软件
第二步,安装alien
1、http://kitenet.net/~joey/code/alien/,下载alien_8.83.tar.gz
2、yum install perl.x86_64 perl-devel.x86_64 (解决MakeMake.PL)
3、tar -zxvf alien_8.86.tar.gz
4、cd alien
5、perl Makefile.PL
6、make
7、make install
8、yum install rpmdevtools (解决rpmbulid)
第三部,deb转rpm
1、alien -r wondershaper_1.1a-6_all.deb,得到wondershaper-1.1a-7.noarch.rpm
2、yum localinstall PPStream-1.0.0-2.i386.rpm --nogpgcheck(解决依赖关系)
第四部,软件使用
启用 wondershaper eth0 1000 200
关闭 wondershaper clear eth0
查看 wondershaper eth0
附件:wondershaper 脚本,看着此脚本我脑袋疼
#!/bin/sh
# Wonder Shaper
# Modifications by Vince Mulhollon for debian package
if [ $# -eq 0 ]; then
echo Please read the man page for the wondershaper and
echo the file /usr/share/doc/wondershaper/README.Debian.gz
exit
fi
if [ $# -eq 1 ]; then
/sbin/tc -s qdisc ls dev $1
/sbin/tc -s class ls dev $1
exit
fi
if [ $# -eq 2 ]; then
/sbin/tc qdisc del dev $2 root 2> /dev/null > /dev/null
/sbin/tc qdisc del dev $2 ingress 2> /dev/null > /dev/null
echo Wondershaper queues have been cleared.
exit
fi
if [ $# -ne 3 ]; then
echo Please read the man page for the wondershaper and
echo the file /usr/share/doc/wondershaper/README.Debian.gz
exit
fi
# please read the README before filling out these values
#
# Set the following values to somewhat less than your actual download
# and uplink speed. In kilobits. Also set the device that is to be shaped.
DOWNLINK=$2
UPLINK=$3
DEV=$1
# low priority OUTGOING traffic - you can leave this blank if you want
# low priority source netmasks
NOPRIOHOSTSRC=
# low priority destination netmasks
NOPRIOHOSTDST=
# low priority source ports
NOPRIOPORTSRC=
# low priority destination ports
NOPRIOPORTDST=
# Now remove the following two lines :-)
#echo Please read the documentation in 'README' first :-\)
#exit
#########################################################
#if [ "$1" = "status" ]
#then
# /sbin/tc -s qdisc ls dev $DEV
# /sbin/tc -s class ls dev $DEV
# exit
#fi
# clean existing down- and uplink qdiscs, hide errors
/sbin/tc qdisc del dev $DEV root 2> /dev/null > /dev/null
/sbin/tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null
#if [ "$1" = "stop" ]
#then
# exit
#fi
###### uplink
# install root CBQ
/sbin/tc qdisc add dev $DEV root handle 1: cbq avpkt 1000 bandwidth 10mbit
# shape everything at $UPLINK speed - this prevents huge queues in your
# DSL modem which destroy latency:
# main class
/sbin/tc class add dev $DEV parent 1: classid 1:1 cbq rate ${UPLINK}kbit \
allot 1500 prio 5 bounded isolated
# high prio class 1:10:
/sbin/tc class add dev $DEV parent 1:1 classid 1:10 cbq rate ${UPLINK}kbit \
allot 1600 prio 1 avpkt 1000
# bulk and default class 1:20 - gets slightly less traffic,
# and a lower priority:
/sbin/tc class add dev $DEV parent 1:1 classid 1:20 cbq rate $((9*$UPLINK/10))kbit \
allot 1600 prio 2 avpkt 1000
# 'traffic we hate'
/sbin/tc class add dev $DEV parent 1:1 classid 1:30 cbq rate $((8*$UPLINK/10))kbit \
allot 1600 prio 2 avpkt 1000
# all get Stochastic Fairness:
/sbin/tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
/sbin/tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
/sbin/tc qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10
# start filters
# TOS Minimum Delay (ssh, NOT scp) in 1:10:
/sbin/tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
match ip tos 0x10 0xff flowid 1:10
# ICMP (ip protocol 1) in the interactive class 1:10 so we
# can do measurements & impress our friends:
/sbin/tc filter add dev $DEV parent 1:0 protocol ip prio 11 u32 \
match ip protocol 1 0xff flowid 1:10
# pablo.iranzo@uv.es provided a patch for the MLDonkey system
# The MLDonkey uses small UDP packets for source propogation
# which floods the wondershaper out.
/sbin/tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
match ip protocol 17 0xff \
match ip sport 4666 0xffff \
flowid 1:30
# prioritize small packets (<64 bytes)
/sbin/tc filter add dev $DEV parent 1: protocol ip prio 12 u32 \
match ip protocol 6 0xff \
match u8 0x05 0x0f at 0 \
match u16 0x0000 0xffc0 at 2 \
flowid 1:10
# some traffic however suffers a worse fate
for a in $NOPRIOPORTDST
do
/sbin/tc filter add dev $DEV parent 1: protocol ip prio 14 u32 \
match ip dport $a 0xffff flowid 1:30
done
for a in $NOPRIOPORTSRC
do
/sbin/tc filter add dev $DEV parent 1: protocol ip prio 15 u32 \
match ip sport $a 0xffff flowid 1:30
done
for a in $NOPRIOHOSTSRC
do
/sbin/tc filter add dev $DEV parent 1: protocol ip prio 16 u32 \
match ip src $a flowid 1:30
done
for a in $NOPRIOHOSTDST
do
/sbin/tc filter add dev $DEV parent 1: protocol ip prio 17 u32 \
match ip dst $a flowid 1:30
done
# rest is 'non-interactive' ie 'bulk' and ends up in 1:20
/sbin/tc filter add dev $DEV parent 1: protocol ip prio 18 u32 \
match ip dst 0.0.0.0/0 flowid 1:20
# 这里是从网上参考别人的
# 不限制内网从本服务器下载。
# 注意如本服务器上有代理,用户可通过代理绕过带宽限制,
# 可取消以下三句限制从本服务器下载。
#/sbin/tc class add dev $DEV parent 1:1 classid 1:40 cbq bandwidth 100Mbit rate 95Mbit allot 1514 weight 20Kbit prio 5 maxburst 20 avpkt 1000 bounded
#/sbin/tc qdisc add dev $DEV parent 1:40 sfq quantum 1514b perturb 10
#/sbin/tc filter add dev $DEV parent 1:0 protocol ip prio 19 u32 match ip src 10.125.221.152 flowid 1:10
########## downlink #############
# slow downloads down to somewhat less than the real speed to prevent
# queuing at our ISP. Tune to see how high you can set it.
# ISPs tend to have *huge* queues to make sure big downloads are fast
#
# attach ingress policer:
/sbin/tc qdisc add dev $DEV handle ffff: ingress
# filter *everything* to it (0.0.0.0/0), drop everything that's
# coming in too fast:
/sbin/tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1