由 doooom 在 04-07-2003 10:55 发表:
疯狂电驴——linux下电驴上载带宽限制之探讨
电驴实在是个好东西,为什么我就不用说了吧。linux下面的电驴更是个好东西,因为她把我从vmware的魔爪里面解救出来了。linux版没有win版那么好用直接吧bot什么的嵌在里面但是就下载来说已经很不错了饿。
但是电驴也有些不劲人译的地方。比如它限制下载带宽不能高于上载的4倍。这个就让我的DSL很痛苦了。当我把上载设置到15的时候,连接就基本瘫痪了。为了不叫室友和我拼命,我只能把它设置在5以内,就是说我最多也就20的下载速度。这个要是去下载一个ASIAN什么什么的DVD岂不要等到我老死。
上载和下载的带宽是在我们机器上面的客户端限制的。就是说如果我将客户端的下载和上载速度放开,然后在linux里面限制上载速度,我就可以打破这个速度限制。
在研究了两篇非常优秀的文档,iptables tutorial 1.1.6 和 linux advanced routing & traffic control,之后写了下面这段代码。其主要目的就是把指向4662端口的流量过滤到一个我自定义的类里面。而在这个类上面家一个流量控制。对于我这样的dsl连接,就是纯粹的要限制流量的目的来说使用tbf是一个很好的选择;如果是ftp或者是大型的数据服务器又不愿意让一个连接把所有的带宽都占用就使用sqf的队列规则。在没有限制的时候,不符喝滤镜规则的数据包会被穿到1:1,1:2,1:3三个pfifo_fast类。当然也可以自己去定义对这些数据的处理。
>
> 源码:
>
> * * *
>
>
> >
> NETIF=ppp0
> >
> # outgoing net interface
> >
> tc qdisc add dev $NETIF root handle 1: htb default 10
> >
> tc class add dev $NETIF parent 1: classid 1:1 htb rate 100kbps ceil 100kbps
> >
> tc class add dev $NETIF parent 1:1 classid 1:10 htb \
> >
> rate 8kbps ceil 8kbps prio 1
> >
>
> >
> tc filter add dev $NETIF protocol ip parent 1:0 prio 1 u32 \
> >
> match ip dport 4662 0xffff flowid 1:10
> >
>
> >
> tc qdisc add dev $NETIF parent 1:10 handle 100: tbf rate \
> >
> 32kbit latency 150ms burst 1600
> >
>
>
> * * *
不幸的是,使用后发现效果不是很理想。上载速度又明显的变化,但是没有被很好的限制。看来电驴上面大家使用的端口不都是4662。本着宁可错杀1万,不能防走一个的方针。我准备把我觉得不用的端口都封掉。这个时候,只使用tc就显得力不从心。需要配合iptables来完成这个功能。具体实现就是用iptables将指向你看着不顺眼的端口的数据全打上标签。tc根据这些标签过滤。
最后完成的代码是这个样子。可以用edonkey stop 或者edonkey start 来控制开启或者关闭流量控制。具体的端口选择上面我没有细选,又兴趣可以把10000:11000这样的ftp passive端口也让过去。我是无所谓了。
>
> 源码:
>
> * * *
>
>
> >
> #! /bin/sh
> >
> # by doooom from www.linuxsir.com
> >
> source /etc/sysconfig/rc
> >
> source $rc_functions
> >
>
> >
> UPBW=10kbps
> >
> EDUPBW=4kbps
> >
> CEILBW=17kbps
> >
> # UPBW is the real upload bandwidth of the connection
> >
> # EDUPBW is the bandwidth assigned to edonky upload
> >
> # CEILBW is the maxium upload burst traffic
> >
>
> >
> NETIF=ppp0
> >
> # outgoing net interface
> >
>
> >
> case "$1" in
> >
> start)
> >
> echo "Turning on the Edonkey traffic control..."
> >
>
> >
> tc qdisc add dev $NETIF root handle 1: htb default 11
> >
> tc class add dev $NETIF parent 1: classid 1:1 htb \
> >
> rate $CEILBW ceil $CEILBW
> >
> tc class add dev $NETIF parent 1:1 classid 1:10 htb \
> >
> rate $EDUPBW ceil $UPBW prio 1
> >
>
> >
> tc filter add dev $NETIF protocol ip parent 1:0 prio 2 \
> >
> handle 1 fw flowid 1:10
> >
>
> >
> tc qdisc add dev $NETIF parent 1:10 handle 100: tbf rate \
> >
> $EDUPBW latency 120ms burst 1600
> >
>
> >
> iptables -t mangle -A OUTPUT -p tcp --dport 4000:65535 \
> >
> -j MARK --set-mark 1
> >
>
> >
> evaluate_retval
> >
> ;;
> >
>
> >
> stop)
> >
> echo "Turning off edonkey traffic control..."
> >
> tc qdisc del dev ppp0 root handle 1: htb default 11
> >
> iptables --flush -t mangle
> >
> evaluate_retval
> >
> ;;
> >
>
> >
> restart)
> >
> $0 stop
> >
> sleep 15
> >
> $0 start
> >
> ;;
> >
>
> >
> *)
> >
> echo "Usage: $0 {start|stop|restart}"
> >
> exit 1
> >
> ;;
> >
> esac
> >
>
> >
> # End
> >
>
> >
>
> >
>
>
> * * *
在端口上面可以再细一写比如限制的范围扩展到100:65535,然后从中间去掉那些又用的端口。
工欲善其实必先利其器。找一个实时的流量显示非常有用。推荐一个教wmnet的wmaker dock application.
哦没有说最后的效果怎么样,使用这个脚本,我把上行控制在5,下载速度就随意了。
发行版再好,不如自己做的lfs好。
由 iamok@ccf 在 04-07-2003 16:33 发表:
好帖子....
总算理解了一些结构了...被结构搞晕了...
实时监控流量...不需要x环境的工具是...
http://ethstatus.calle69.net/graphic/index.html
超爽哦...服务器必备利器...
由 dsj 在 04-07-2003 20:39 发表:
呵呵,老兄辛苦了!
闲聊空间:
http://www.wonyen.net/bbs/mboard.asp
由 Snoopy 在 04-07-2003 21:57 发表:
电炉是什么????
And then in the evening light, when the bars of freedom fall
I watch the two of you in the shadows on the wall
How in the darkness steals some of the choices from my hand
Then will I begin to under
由 dragonnapalm 在 04-07-2003 21:58 发表:
eDonkey2000
Come back to recall myself...
由 Snoopy 在 04-07-2003 22:01 发表:
下载东西的工具???是不是点对点那些啊 ~?~?
And then in the evening light, when the bars of