Date: Fri, 21 Jan 2000 00:03:04 -0500 (EST) From: Garrett Wollman <wollman@khavrinen.lcs.mit.edu> To: Alfred Perlstein <bright@wintelcom.net> Cc: net@FreeBSD.ORG Subject: half-fix for stream.c Message-ID: <200001210503.AAA41221@khavrinen.lcs.mit.edu> In-Reply-To: <20000120174115.C14030@fw.wintelcom.net> References: <20000120174115.C14030@fw.wintelcom.net>
next in thread | previous in thread | raw e-mail | index | archive | help
<<On Thu, 20 Jan 2000 17:41:17 -0800, Alfred Perlstein <bright@wintelcom.net> said: > you can find it at: > http://www.freebsd.org/~alfred/tcp_fix.diff Am I the only person who thinks we could use a generic kernel TBF module for all of these random rate-limiters? The copyright notices are longer than the code.... # This is a shell archive. Save it in a file, remove anything before # this line, and then unpack it by entering "sh file". Note, it may # create directories; files and directories will be owned by you and # have default permissions. # # This archive contains: # # tbf.c # tbf.h # echo x - tbf.c sed 's/^X//' >tbf.c << 'END-of-tbf.c' X/* X * Copyright 2000 Massachusetts Institute of Technology X * X * Permission to use, copy, modify, and distribute this software and X * its documentation for any purpose and without fee is hereby X * granted, provided that both the above copyright notice and this X * permission notice appear in all copies, that both the above X * copyright notice and this permission notice appear in all X * supporting documentation, and that the name of M.I.T. not be used X * in advertising or publicity pertaining to distribution of the X * software without specific, written prior permission. M.I.T. makes X * no representations about the suitability of this software for any X * purpose. It is provided "as is" without express or implied X * warranty. X * X * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS X * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, X * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF X * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT X * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, X * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT X * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF X * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND X * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, X * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT X * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X X/* X * This module implements a generic token-bucket filter (TBF). X * This is a basic element of queueing systems. Tokens accumulate X * at a rate r (here called tbf_rate and measured in zorkmids per X * second) in a bucket with maximum size B (tbf_max). When an activity X * of cost n (zorkmids) is requested, n tokens are drawn from the bucket. X * (If fewer than n zorkmids are available at that time, the request X * fails.) The result limits the long-term rate of activity, X * while allowing for some amount of burstiness. X */ X X#include <sys/systm.h> X#include <sys/callout.h> X#include <sys/tbf.h> X Xstatic LIST_HEAD(, tbf) tbfs; Xstatic struct callout callout; Xstatic int callout_started; X Xstatic void tbf_incr(void *dummy); X Xvoid Xtbf_init(struct tbf *f, u_long rate, u_long max) X{ X if (!callout_started) { X LIST_INIT(&tbfs); X callout_init(&callout); X callout_reset(&callout, hz, tbf_incr, (void *)0); X callout_started = 1; X } X X f->tbf_rate = rate; X f->tbf_max = max; X f->tbf_cur = rate; X LIST_INSERT_HEAD(&tbfs, f, tbf_link); X} X Xint Xtbf_check(struct tbf *f, u_long n) X{ X return (TBF_CHECK(f, n)); X} X Xstatic void Xtbf_incr(void *dummy) X{ X struct tbf *f; X int s; X X for (f = tbfs->lh_first; f != 0; f = f->tbf_link.le_next) { X s = splhigh(); /* splimp() might be good enough? */ X f->tbf_cur = ulmin(f->tbf_max, f->tbf_cur + f->tbf_rate); X splx(s); X } X callout_reset(&callout, hz, tbf_incr, (void *)0); X} X Xvoid Xtbf_remove(struct tbf *f) X{ X int s = splclock(); /* XXX ??? */ X LIST_REMOVE(f, tbf_link); X splx(s); X} END-of-tbf.c echo x - tbf.h sed 's/^X//' >tbf.h << 'END-of-tbf.h' X/* X * Copyright 2000 Massachusetts Institute of Technology X * X * Permission to use, copy, modify, and distribute this software and X * its documentation for any purpose and without fee is hereby X * granted, provided that both the above copyright notice and this X * permission notice appear in all copies, that both the above X * copyright notice and this permission notice appear in all X * supporting documentation, and that the name of M.I.T. not be used X * in advertising or publicity pertaining to distribution of the X * software without specific, written prior permission. M.I.T. makes X * no representations about the suitability of this software for any X * purpose. It is provided "as is" without express or implied X * warranty. X * X * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS X * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, X * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF X * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT X * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, X * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT X * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF X * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND X * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, X * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT X * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X#ifndef _SYS_TBF_H_ X#define _SYS_TBF_H_ 1 X X#include <sys/queue.h> X Xstruct tbf { X u_long tbf_cur; X LIST_ENTRY(tbf) tbf_link; X u_long tbf_rate; X u_long tbf_max; X}; X X#define TBF_CHECK(f, n) ((f)->tbf_cur >= n ? ((f)->tbf_cur -= n, 1) : 0) X Xint tbf_check(struct tbf *f, u_long n); Xvoid tbf_init(struct tbf *f, u_long tbf_rate, u_long tbf_max); Xvoid tbf_remove(struct tbf *f); X X#endif /* _SYS_TBF_H_ */ END-of-tbf.h exit To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001210503.AAA41221>