在/etc/ftpaccess里面加上:
throughput /home/ftp * * 64000 0.5 *
意思是下载带宽对所有ip(最后那个*)限制在64k,每下一个文件速度减半(那个0
.5).
我用的是2.5.x,man page里面很全的.
用man ftpaccess就可以.:-)
◇ proftpd 流量控制
----------------------------------------------------------------------
----------发信人: charley.bbs@swallow.twbbs.org (喘气中...), 看板: lin
ux
标 题: proftpd 流量控制
发信站: 临风小□ bbs 站 (tue mar 9 15:55:30 1999) @cyndi.cis.nctu.edu
.tw
转信站: maxwell!bbs.ee.ntu!freebsd.ntu!news.cs.nthu!news.cis.nctu!wind
这是自己改的 proftpd 1.2.0pre2 流量控制的 patch.
需要原来 proftpd source!
相关程式可以在 ftp://swallow.twbbs.org/linux/ftp/ 下找到
用法:
1. 如果你的 proftpd-1.2.0pre2.tar.gz 解在 proftpd-1.2.0pre2/
将本 patch 放跟 proftpd-1.2.0pre2/ 同一层目录, 打
patch < proftpd-1.2.0pre2-bandwidth.patch
如果放在 proftpd-1.2.0pre2/ 下
patch -p1 < proftpd-1.2.0pre2-bandwidth.patch
2. 重新 make 你的 source!
系统设定:
1. 提供三个控制流量功能(只有下传)
可以设在 server config,,
bandwidth : 总频宽
maxbandwidth : 每线最大频宽
minbandwidth : 每线最小频宽
必需为整数,代表 位元组/秒(bytes/second)
三个功能的关系: minbandwidth > maxbandwidth > bandwidth
2. □例1.
设定 bandwidth 4096
如果有4人在线上,每个人的流量为 4096%424 位元组/秒
□例2.
设定 bandwidth 4096
maxbandwidth 3072
如果有1人在线上,每个人的流量为 3072 位元组/秒
如果有2人在线上,每个人的流量为 4096%2 48 位元组/秒
以下类推
□例3.
设定 bandwidth 3072
minbandwidth 1024
如果有1人在线上,每个人的流量为 3072 位元组/秒
如果有2人在线上,每个人的流量为 3072%236 位元组/秒
如果有3人在线上,每个人的流量为 3072%324 位元组/秒
如果有4人在线上,每个人的流量为 1024 位元组/秒
以下类推
详细的说明请看 patch 过的 mod_xfer.c
--- start proftpd-1.2.0pre2-bandwidth.patch ---
--- proftpd-1.2.0pre2.orig/modules/mod_xfer.c fri feb 19 15:11:11 19
99
+++ proftpd-1.2.0pre2/modules/mod_xfer.c mon feb 22 14:38:19 19
99
@@ -31,10 +31,140 @@
*
*/
+/********************************************************************
********
+ * title : bandwidth management
+ * file : mod_xfer.c (need orig source from proftpd-1.2.0pre2.
tar.gz)
+ * author : albert chiu (albert@swallow.twbbs.org.tw)
+ * date : 22 feb 1999
+ * version : 0.9p2
+ *
+ * description :
+ * provide bandwidth usage limitation on per connection.
+ *
+ * revision :
+ * ver 0.9p2
+ * - add bandwidth and minbandwidth.
+ * ver 0.9p1
+ * - fix unpredictable disconnect bugs.
+ * - port patch to proftpd-1.2.0pre2
+ * ver 0.9
+ * - first patch version base on proftpd-1.2.0pre1.
+ * - only include maxbandwidth.
+ *
+ ********************************************************************
*******
+ * copyright (c)1999 albert chiu ( chiu yang-li ). all rights reserve
d.
+ * modify for the proftpd group by :
+ *
+ * albert chiu
+ * albert@swallow.twbbs.org.tw
+ * http://swallow.twbbs.org.tw/~albert
+ *
+ * i get some idea and good function from apache module collection. i
d looked
+ * at mod_bandwidth, this one is almost what i want, so i use the ide
a and
+ * rewrite it for proftpd 1.2.0pre1.
+ * but i find the select() function which mod_bandwidth use is usuall
y bad
+ * performance, so i use setitimer() to replace it.
+ *
+ * you can get the collection from apache homepage (http://www.apache
.org/).
+ * mod_bandwidth 1.0 is written by yann stettler (stettler@cohprog.co
m).
+ *
+ ********************************************************************
*******/
+
+/*
+ * instruction :
+ * -------------
+ *
+ * note : this modify was writen for proftpd 1.2.0pre2 and tested on
it
+ *
+ * installation :
+ *
+ * just make all source again is enough. :>
+ *
+ *
+ * server configuration directive :
+ * -----------------------------------
+ * - bandwidth
+ * syntax : bandwidth
+ * default : none
+ * context : server config,,
+ *
+ * limit the total bandwidth for file-transfer (retr only)
+ * final each rate is depend of the number of connections.
+ *
+ * the is in bytes/second.
+ * a of "0" means no bandwidth limit.
+ *
+ * example :
+ * bandwidth 4096
+ *
+ * if there are 4 users online , this will limit the bandwith
+ * for file-transfer to 4096/4 □24bytes/sec.
+ *
+ * note : if transfer file size is lower than the "bandwidth"/"users"
rate,
+ * it will ignore the limit.
+ *
+ * - maxbandwidth
+ * syntax : maxbandwidth
+ * default : none
+ * context : server config,,
+ *
+ * limit the bandwidth for file-transfer (retr only)
+ * this over-ride bandwidth rules as well as the calculated rate
+ * based on the number of connections.
+ *
+ * the is in bytes/second.
+ * a of "0" means no bandwidth limit.
+ *
+ * example :
+ * if bandwidth is set to "4096" (4kbytes/sec) and maxbandwidth
+ * is set to "3072" (2kbytes/sec) that means :
+ * - if there is one connection, the file will be transfered
+ * at 3072 bytes/sec. (maximal of 3072 bytes/sec).
+ * - if there is two connections, each files will be transfered
+ * at 2048 bytes/sec.
+ * - if there is three or more connections, each files will be
+ * transfered at "4096/connections" bytes/sec.
+ *
+ * note : if transfer file size is lower than the "maxbandwidth" rate
,
+ * it will ignore the limit.
+ *
+ * - minbandwidth
+ * syntax : minbandwidth
+ * default : none
+ * context : server config,,
+ *
+ * set a minimal bandwidth to use for file-transfer. (retr only)
+ * this over-ride both bandwidth and maxbandwidth rules as well
+ * as the calculated rate based on the number of connections.
+ *
+ * the is in bytes/second.
+ * a of "0" means no bandwidth limit.
+ *
+ * example :
+ * if bandwidth is set to "3072" (2kbytes/sec) and minbandwidth
+ * is set to "1024" (1kbytes/sec) that means :
+ * - if there is one connection, the file will be transfered
+ * at 3072 bytes/sec.
+ * - if there is two connections, each files will be transfered
+ * at 1536 bytes/sec.
+ * - if there is three or more connections, each files will be
+ * transfered at 1024 bytes/sec. (minimal of 1024 bytes/sec).
+ *
+ * note : if transfer file size is lower than the "minbandwidth" rate
,
+ * it will ignore the limit.
+ *
+ */
+
#include "conf.h"
#ifdef have_regex_h
#include
#endif
+#include
+
+#undef timerisset
+#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_us
ec)
+#undef timerclear
+#define timerclear(tvp) ((tvp)->tv_sec tvp)->tv_usec □
extern module auth_module;
extern pid_t mpid;
@@ -51,6 +181,7 @@
static int stor_fd;
static int retr_fd;
+
module xfer_module;
static void _log_transfer(char direction)
@@ -129,6 +260,48 @@
retr_file ull;
}
+static struct timeval timediff(struct timeval *a, struct timeval *b)
+{
+ struct timeval rslt, tmp;
+
+ tmp a;
+
+ if ((rslt.tv_usec mp.tv_usec - b->tv_usec) < 0) {
+ rslt.tv_usec +□00000;
+ --(tmp.tv_sec);
+ }
+ if ((rslt.tv_sec mp.tv_sec - b->tv_sec) < 0) {
+ rslt.tv_usec □
+ rslt.tv_sec □
+ }
+ return (rslt);
+}
+
+void handler(int sig)
+{
+ return;
+}
+
+int sleep_msec(struct timeval *a)
+{
+ struct timeval tmp;
+ struct itimerval itimer;
+ void handler();
+
+ tmp a;
+
+ if (timerisset(&tmp)) {
+ signal(sigalrm,handler);
+ timerclear(&itimer.it_interval);
+ itimer.it_value.tv_usecp.tv_usec;
+ itimer.it_value.tv_secp.tv_sec;
+ setitimer(itimer_real,&itimer,(struct itimerval *)0);
+ pause();
+ timerclear(&itimer.it_value);
+ setitimer(itimer_real,&itimer,(struct itimerval *)0);
+ }
+}
+
/* cmd_pre_stor is a pre_cmd handler which checks security, etc, and
* places the full filename to receive in cmd->private [note that we
cannot
* use cmd->tmp_pool for this, as tmp_pool only lasts for the duratio
n
@@ -384,9 +557,11 @@
struct stat sbuf;
char *lbuf;
int bufsize,len;
+ long cur_rate □max_rate □min_rate □cur_user □
unsigned long respos □cnt □cnt_steps □cnt_next □
privdata_t *p;
-
+ struct timeval opt_time, last_time, now, timespent, timeout;
+
p od_privdata_find(cmd,"retr_filename",null);
if(!p) {
@@ -433,10 +608,53 @@
cnt espos;
log_add_run(mpid,null,session.user,null,0,session.xfer.file_size,
0,null);
+ /* calculate bandwidth transfer time
+ *
+ * if "bandwidth" , "maxbandwidth" or "minbandwidth" not be defin
ed or
+ * be defined error. i will turn off bandwidth limit.
+ */
+
+ cur_user long)get_param_int(cmd->server->conf,"current-clients",f
alse)+1;
+ cur_rate long)get_param_int(cmd->server->conf,"bandwidth",false);
+ max_rate long)get_param_int(cmd->server->conf,"maxbandwidth",fals
e);
+ min_rate long)get_param_int(cmd->server->conf,"minbandwidth",fals
e);
+
+ cur_rate □r_rate / cur_user;
+
+ if(max_rate <□)
+ max_rate □;
+ if(min_rate <□)
+ min_rate □;
+ if(cur_rate <□)
+ {
+ if(max_rate > 0l)
+ cur_rate ax_rate;
+ else if(min_rate > 0l)
+ cur_rate in_rate;
+ else
+ cur_rate □;
+ }
+
+ if((cur_rate > max_rate) && (max_rate > 0l))
+ cur_rate ax_rate;
+ if(min_rate > cur_rate)
+ cur_rate in_rate;
+
+ if(cur_rate > 0l) {
+ opt_time.tv_sec long int) bufsize / cur_rate;
+ opt_time.tv_usec long int) ((float) bufsize * 1000000 / cur_rat
e - opt_time.tv_sec * 1000000);
+ log_debug(debug1, "current bandwidth : %ld bytes/sec",cur_rate)
;
+ }
+
while((len s_read(retr_file,retr_fd,lbuf,bufsize)) > 0) {
if(xfer_aborted)
break;
+ /* record transfer start time */
+ if(cur_rate > 0l) {
+ gettimeofday(&last_time, (struct timezone *) 0);
+ }
+
len □ta_xfer(lbuf,len);
if(len < 0) {
_retr_abort();
@@ -450,6 +668,21 @@
log_add_run(mpid,null,session.user,null,0,session.xfer.file_
size,cnt,null);
}
}
+
+ /* calculate spent time and set up timer */
+ if((cur_rate > 0l) && (session.xfer.file_size > cur_rate)) {
+ gettimeofday(&now, (struct timezone *) 0);
+ timespent imediff(&now, &last_time);
+ timeout imediff(&opt_time, ×pent);
+ log_debug(debug5, "bandwidth : sleep : %ld/%ld (op time : %ld
/%ld spent : %ld/%ld)",
+ timeout.tv_sec, timeout.tv_usec, opt_time.tv_sec,
opt_time.tv_usec,
+ timespent.tv_sec, timespent.tv_usec);
+ /* we sleep... */
+ sleep_msec(&timeout);
+ if(timeoutidle)
+ reset_timer(timer_idle,any_module);
+ }
+
}
if(xfer_aborted) {
@@ -519,6 +752,57 @@
return declined(cmd);
}
+modret set_bandwidth(cmd_rec *cmd)
+{
+ long s_cur_rate
+
+ check_args(cmd,1);
+ check_conf(cmd,conf_root|conf_virtual|conf_global);
+
+ s_cur_rate □oi(cmd->argv[1]);
+
+ if(s_cur_rate < 0)
+ conf_error(cmd,"value must be lager then 0");
+
+ add_config_param_set(&cmd->server->conf,"bandwidth",1,(void*)s_cur_
rate);
+
+ return handled(cmd);
+}
+
+modret set_maximumbandwidth(cmd_rec *cmd)
+{
+ long s_cur_rate
+
+ check_args(cmd,1);
+ check_conf(cmd,conf_root|conf_virtual|conf_global);
+
+ s_cur_rate □oi(cmd->argv[1]);
+
+ if(s_cur_rate < 0)
+ conf_error(cmd,"value must be lager then 0");
+
+ add_config_param_set(&cmd->server->conf,"maxbandwidth",1,(void*)s_c
ur_rate);
+
+ return handled(cmd);
+}
+
+modret set_minimumbandwidth(cmd_rec *cmd)
+{
+ long s_cur_rate
+
+ check_args(cmd,1);
+ check_conf(cmd,conf_root|conf_virtual|conf_global);
+
+ s_cur_rate □oi(cmd->argv[1]);
+
+ if(s_cur_rate < 0)
+ conf_error(cmd,"value must be lager then 0");
+
+ add_config_param_set(&cmd->server->conf,"minbandwidth",1,(void*)s_c
ur_rate);
+
+ return handled(cmd);
+}
+
static int _noxfer_timeout(callback_frame)
{
if(session.flags & sf_xfer)
@@ -542,6 +826,13 @@
return 0;
}
+static conftable xfer_config[]
+ { "bandwidth", set_bandwidth, null }
,
+ { "maxbandwidth", set_maximumbandwidth, null }
,
+ { "minbandwidth", set_minimumbandwidth, null }
,
+ { null, null, null }
+};
+
cmdtable xfer_commands[]
{ cmd, c_type, g_none, cmd_type, true, false, cl_misc
},
{ pre_cmd, c_retr, g_read, pre_cmd_retr, true, false },
@@ -562,7 +853,7 @@
null,null, /* always null */
0x20, /* api version */
"xfer", /* module name */
- null, /* no config */
+ xfer_config, /* config name */
xfer_commands,
null,
null,xfer_init_child
--- end proftpd-1.2.0pre2-bandwidth.patch ---
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


