1 /* ====================================================================
2 * Copyright (c) 1995 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission.
25 * 5. Redistributions of any form whatsoever must retain the following
27 * "This product includes software developed by the Apache Group
28 * for use in the Apache HTTP server project (http://www.apache.org/)."
30 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
31 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
34 * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 * ====================================================================
44 * This software consists of voluntary contributions made by many
45 * individuals on behalf of the Apache Group and was originally based
46 * on public domain software written at the National Center for
47 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
48 * For more information on the Apache Group and the Apache HTTP server
49 * project, please see <http://www.apache.org/>.
53 /****************************************************************************
54 * Title : Bandwidth management
55 * File : mod_bandwidth.c
56 * Author : Yann Stettler (stettler@cohprog.com)
57 * Date : 12 January 2003
58 * Version : 2.0.6 for Apache 1.3+
61 * Provide bandwidth usage limitation either on the whole server or
62 * one a per connection basis based on the size of files, directory
63 * location or remote domain/IP.
65 * Revision : 08/04/97 - "-1" value for LargeFileLimit to disable any
66 * limits for that kind of files.
67 * 01/26/98 - Include in_domain() and in_ip() in this file and
68 * make them static so it will work with Apache 1.3x
69 * 03/29/98 - Change to set bandwidth in VirtualHost directive
70 * 07/17/99 - Minor changes to remove warnings at compil time
72 * Allow the use of the network/mask format when
73 * setting a bandwidth for a remote host/network.
74 * (Thanks to Sami Kuhmonen for the patch)
76 * New directive BandWidthPulse
78 * 10/14/00 - Minor bug fixed
79 * 12/15/00 - Bug fix when using mmap
80 * 08/29/01 - Add a directive to change the data directory
81 * (Thanks to Awesome Walrus <walrus@amur.ru> )
82 * 01/12/03 - Add MaxConnection directive to limit the number
83 * of simultaneous connections.
85 ***************************************************************************
86 * Copyright (c)1997 Yann Stettler and CohProg SaRL. All rights reserved.
87 * Written for the Apache Group by :
90 * stettler@cohprog.com
91 * http://www.cohprog.com/
92 * http://www.animanga.com/
94 * Based on the original default_handler module and on bw_module 0.2 from
95 * Etienne BERNARD (eb@via.ecp.fr) at VIA Centrale Reseaux, ECP, France
97 * Many thanks to Colba Net Inc (Montreal) for their sponsoring of
98 * improvements to this module.
100 ***************************************************************************/
107 * Note : this module was writen for Apache 1.3.x and tested on it
111 * 1) Insert the following line at the end of the "Configuration" file :
112 * Module bandwidth_module mod_bandwidth.o
114 * WARNING : This behaviour is not the same between the various main versions
115 * of Apache. Please, read the instruction on our website for the
116 * instructions concerning the latest release :
117 * http://www.cohprog.com/v3/bandwidth/doc-en.html
119 * 2) Run the "Configure" program and re-compile the server with "make".
121 * 3) Create the following directories with "rwx" permission to everybody :
122 * (or rwx for the user under which Apache run : Usualy "nobody")
125 * /tmp/apachebw/master
127 * /==== by Awesome Walrus <walrus@amur.ru> =====================\
128 * Or you may change this name by using BandWidthDataDir global
129 * configuration directive. See below for details.
130 * \==== by Awesome Walrus <walrus@amur.ru> =====================/
132 * Note that if any of those directories doesn't exist, or if they can't
133 * be accessed by the server, the module is totaly disabled except for
134 * logging an error message in the logfile.
136 * Be careful that on some systems the content of the /tmp directory
137 * is deleted at boot time or every so often by a cronjob. If that the
138 * case, either disable this feature or change the location of the
139 * directories used by the module in the sources bellow.
141 * Server configuration directive :
142 * --------------------------------
144 * /==== by Awesome Walrus <walrus@amur.ru> =====================\
146 * Syntax : BandWidthDataDir <directory>
147 * Default : "/tmp/apachebw"
148 * Context : server config
150 * Sets the name of the directory used by mod_bandwidth to store
151 * its internal temporary information.
152 * \==== by Awesome Walrus <walrus@amur.ru> =====================/
155 * Syntax : BandWidthModule <On|Off>
157 * Context : per server config
159 * Enable or disable totaly the whole module. By default, the module is
160 * disable so it is safe to compile it in the server anyway.
162 * PLEASE, NOTE THAT IF YOU SET A BANDWIDTH LIMIT INSIDE A VIRTUALHOST
163 * BLOCK, YOU ALSO __NEED__ TO PUT THE "BandWidthModule On" DIRECTIVE
164 * INSIDE THAT VIRTUALHOST BLOCK !
166 * IF YOU SET BANDWIDTH LIMITS INSIDE DIRECTORY BLOCKS (OUTSIDE OF
167 * ANY VIRTUALHOST BLOCK), YOU ONLY NEED TO PUT THE "BandWidthModule On"
168 * DIRECTIVE ONCE, OUTSIDE OF ANY VIRTUALHOST OR DIRECTORY BLOCK.
171 * Syntax : BandWidthPulse <microseconds>
173 * Context : per server config
175 * Change the algorithm used to calculate bandwidth and transmit data.
176 * In normal mode (old mode), the module try to transmit data in packets
177 * of 1KB. That mean that if the bandwidth available is of 512B, the
178 * module will transmit 1KB, wait 2 seconds, transmit another 1KB and
181 * Seting a value with "BandWidthPulse", will change the algorithm so
182 * that the server will always wait the same amount of time between
183 * sending packets but the size of the packets will change.
184 * The value is in microseconds.
185 * For example, if you set "BandWidthPulse 1000000" (1 sec) and the
186 * bandwidth available is of 512B, the sever will transmit 512B,
187 * wait 1 second, transmit 512B and so on.
189 * The advantage is a smother flow of data. The disadvantage is
190 * a bigger overhead of data transmited for packet header.
191 * Setting too small a value (bellow 1/5 of a sec) is not realy
192 * useful and will put more load on the system and generate more
193 * traffic for packet header.
195 * Note also that the operating system may do some buffering on
196 * it's own and so defeat the purpose of setting small values.
198 * This may be very useful on especialy crowded network connection :
199 * In normal mode, several seconds may happen between the sending of
200 * a full packet. This may lead to timeout or people may believe that
201 * the connection is hanging. Seting a value of 1000000 (1 sec) would
202 * guarantee that some data are sent every seconds...
204 * Directory / Server / Virtual Server configuration directive :
205 * -------------------------------------------------------------
208 * Syntax : BandWidth <domain|ip|all> <rate>
210 * Context : per directory, .htaccess
212 * Limit the bandwidth for files in this directory and
213 * sub-directories based on the remote host <domain> or
214 * <ip> address or for <all> remote hosts.
216 * Ip addresses may now be specified in the network/mask format.
217 * (Ie: 192.168.0.0/21 )
219 * The <rate> is in Bytes/second.
220 * A <rate> of "0" means no bandwidth limit.
222 * Several BandWidth limits can be set for the same
223 * directory to set different limits for different
224 * hosts. In this case, the order of the "BandWidth"
225 * keywords is important as the module will take the
226 * first entry which matches the client address.
229 * <Directory /home/www>
231 * BandWidth 138.195 0
235 * This will limit the bandwith for directory /home/www and
236 * all it's subdirectories to 1024Bytes/sec, except for
237 * *.ecp.fr or 138.195.*.* where no limit is set.
240 * Syntax : LargeFileLimit <filesize> <rate>
242 * Context : per directory, .htaccess
244 * Set a maximal <rate> (in bytes/sec) to use when transfering
245 * a file of <filesize> KBytes or more.
247 * Several "LargeFileLimit" can be set for various files sizes
248 * to create range. The rate used for a given file size will be
249 * the one of the matching range.
251 * A <rate> of "0" mean that there isn't any limit based on
254 * A <rate> of "-1" mean that there isn't any limit for that type
255 * of file. It's override even a BandWidth limit. I found this usefull
256 * to give priority to very small files (html pages, very small pictures)
257 * while seting limits for larger files... (users with their video files
258 * can go to hell ! :)
261 * If the following limits are set :
262 * LargeFileLimit 200 3072
263 * LargeFileLimit 1024 2048
265 * That's mean that a file of less than 200KBytes won't be
266 * limited based on his size. A file with a size between
267 * 200KBytes (included) and 1023Kbytes (included) will be
268 * limited to 3072Bytes/sec and a file of 1024Kbytes or more
269 * will be limited to 2048Bytes/sec.
272 * Syntax : MinBandWidth <domain|ip|all> <rate>
274 * Context : per directory, .htaccess
276 * Set a minimal bandwidth to use for transfering data. This
277 * over-ride both BandWidth and LargeFileLimit rules as well
278 * as the calculated rate based on the number of connections.
280 * The first argument is used in the same way as the first
281 * argument of BandWidth.
283 * <rate> is in bytes per second.
285 * A rate of "0" explicitly means to use the default minimal
286 * value (256 Bytes/sec).
288 * A rate of "-1" means that the minimal rate is equal to the
289 * actual rate defined by BandWidth and LargeFileLimit.
290 * In fact, that means that the final rate won't depend
291 * of the number of connections but only on what was defined.
294 * If BandWidth is set to "3072" (3KBytes/sec) and MinBandWidth
295 * is set to "1024" (1KBytes/sec) that means :
296 * - if there is one connection, the file will be transfered
298 * - if there is two connections, each files will be transfered
300 * - if there is three or more connections, each files will be
301 * transfered at 1024 Bytes/sec. (Minimal of 1024 Bytes/sec).
303 * If MinBandWidth is set to "-1" that means :
304 * - if there is one connection, the file will be transfered
306 * - if there is two or more connections, each files will be
307 * transfered at 3072 Bytes/sec. In effect, the rate doesn't
308 * depend anymore on the number of connections but only on
309 * the configuration values.
311 * Note that the total transfer rate will never exceed your physical
312 * bandwidth limitation.
314 * Note : If both a "BandWidth" and a "LargeFileLimit" limit apply,
315 * the lowest one will be used. (But never lower than the
316 * "MinBandWidth" rate)
318 * If both a virtual server limit is defined and another
319 * apply for a directory under this virtual server, the
320 * directory limit will over-ride it.
322 * If a limit is defined outside a Directory or VirtualHost
323 * directive, it will act as default on a per virtual server
324 * basis. (Ie: each virtual server will have that limit,
325 * _independantly_ of the other servers)
328 * Syntax : MaxConnection <connections>
329 * Default : 0 (illimited)
330 * Context : per directory, .htaccess
332 * Restrict the number of maximum simultanous connections. If the
333 * limit is reached, new connections will be rejected.
335 * A value of 0 mean that there isn't any limits.
337 * Implementation notes :
338 * ----------------------
340 * 1) This module isn't called when serving a cgi-script. In this
341 * case, it's the functions in mod_cgi.c that handle the connection.
343 * That's not a bug : I didn't want to change the cgi_module and
344 * I was too lazy to do it anyway.
346 * 2) The rate of data transmission is only calculated. It isn't
347 * measured. Which mean that this module calculates the final
348 * rate that should apply for a file and simply divides this
349 * number by the number of actual connections subject to the
350 * same "per directory" directives.
352 * 3) On the "+" side, the module regulate the speed taking
353 * into account the actual time that was needed to send the
354 * data. Which also mean that if data are read by the client
355 * at a slowest rate than the limit, no time will be lost and
356 * data will be sent as fast as the client can read them (but
357 * no faster than the limited rate :) ...
359 * 4) Some kind of buffering is done as side effect. Data are
360 * sent in packet of 1024 Bytes which seems a good value
362 * If another packet size is wanted, change the value of
363 * "#define PACKET" in the codes bellow.
365 * 5) The default value for MinBandWidth is defined by :
366 * "#define MIN_BW_DEFAULT" (in Bytes/sec)
368 * 6) Don't define "BWDEBUG" for a production server :
369 * this would log a _lot_ of useless informations for