Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src pullup (approved by releng-1-5)



details:   https://anonhg.NetBSD.org/src/rev/ba4086ec09aa
branches:  netbsd-1-5
changeset: 489122:ba4086ec09aa
user:      itojun <itojun%NetBSD.org@localhost>
date:      Wed Aug 16 01:20:29 2000 +0000

description:
pullup (approved by releng-1-5)

add ppsratecheck(9).

distrib/sets/lists/comp/mi      1.251 -> 1.252
share/man/man9/Makefile         1.67 -> 1.68 (equivalent to)
share/man/man9/ppsratecheck.9   (new)
sys/kern/kern_time.c            1.49 -> 1.51, 1.52 -> 1.53
sys/sys/time.h                  1.29 -> 1.30

diffstat:

 distrib/sets/lists/comp/mi    |   4 +-
 share/man/man9/Makefile       |   5 +-
 share/man/man9/ppsratecheck.9 |  95 +++++++++++++++++++++++++++++++++++++++++++
 sys/kern/kern_time.c          |  68 ++++++++++++++++++++++++++++--
 sys/sys/time.h                |   3 +-
 5 files changed, 166 insertions(+), 9 deletions(-)

diffs (254 lines):

diff -r 0b829b7a1c12 -r ba4086ec09aa distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Tue Aug 15 21:49:53 2000 +0000
+++ b/distrib/sets/lists/comp/mi        Wed Aug 16 01:20:29 2000 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.231.2.5 2000/08/05 11:10:44 wiz Exp $
+#      $NetBSD: mi,v 1.231.2.6 2000/08/16 01:20:29 itojun Exp $
 ./sys                  comp-sysutil-root
 ./usr/bin/addr2line    comp-miscutil-bin
 ./usr/bin/ar           comp-util-bin
@@ -2878,6 +2878,7 @@
 ./usr/share/man/cat9/pool_setlowat.0           comp-kern-catman
 ./usr/share/man/cat9/powerhook_disestablish.0          comp-kern-catman
 ./usr/share/man/cat9/powerhook_establish.0             comp-kern-catman
+./usr/share/man/cat9/ppsratecheck.0            comp-kern-catman
 ./usr/share/man/cat9/printf.0          comp-kern-catman
 ./usr/share/man/cat9/psignal.0         comp-kern-catman
 ./usr/share/man/cat9/ratecheck.0               comp-kern-catman
@@ -4480,6 +4481,7 @@
 ./usr/share/man/man9/pool_setlowat.9           comp-kern-man
 ./usr/share/man/man9/powerhook_disestablish.9          comp-kern-man
 ./usr/share/man/man9/powerhook_establish.9             comp-kern-man
+./usr/share/man/man9/ppsratecheck.9            comp-kern-man
 ./usr/share/man/man9/printf.9          comp-kern-man
 ./usr/share/man/man9/psignal.9         comp-kern-man
 ./usr/share/man/man9/ratecheck.9               comp-kern-man
diff -r 0b829b7a1c12 -r ba4086ec09aa share/man/man9/Makefile
--- a/share/man/man9/Makefile   Tue Aug 15 21:49:53 2000 +0000
+++ b/share/man/man9/Makefile   Wed Aug 16 01:20:29 2000 +0000
@@ -1,4 +1,4 @@
-#       $NetBSD: Makefile,v 1.62 2000/06/04 19:14:16 cgd Exp $
+#       $NetBSD: Makefile,v 1.62.2.1 2000/08/16 01:20:31 itojun Exp $
 
 #      Makefile for section 9 (kernel function and variable) manual pages.
 
@@ -7,7 +7,8 @@
        doshutdownhooks.9 ethersubr.9 evcnt.9 extent.9 fetch.9 fork1.9 \
        humanize_number.9 inittodr.9 intro.9 ioctl.9 kprintf.9 \
        log.9 malloc.9 mbuf.9 microtime.9 panic.9 pfil.9 physio.9 pool.9 \
-       powerhook_establish.9 psignal.9 ratecheck.9 resettodr.9 rnd.9 \
+       powerhook_establish.9 ppsratecheck.9 psignal.9 \
+       ratecheck.9 resettodr.9 rnd.9 \
        rt_timer.9 shutdownhook_establish.9 \
        sleep.9 softintr.9 spl.9 store.9 time.9 uiomove.9 ucom.9 usbdi.9 uvm.9 \
        wdc.9
diff -r 0b829b7a1c12 -r ba4086ec09aa share/man/man9/ppsratecheck.9
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man9/ppsratecheck.9     Wed Aug 16 01:20:29 2000 +0000
@@ -0,0 +1,95 @@
+.\"    $NetBSD: ppsratecheck.9,v 1.1.2.2 2000/08/16 01:20:31 itojun Exp $
+.\"
+.\" Copyright (c) 2000 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Jun-ichiro itojun Hagino.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"        This product includes software developed by the NetBSD
+.\"        Foundation, Inc. and its contributors.
+.\" 4. Neither the name of The NetBSD Foundation nor the names of its
+.\"    contributors may be used to endorse or promote products derived
+.\"    from this software without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd August 3, 2000
+.Dt PPSRATECHECK 9
+.Os
+.Sh NAME
+.Nm ppsratecheck
+.Nd function to help implement rate-limited actions
+.Sh SYNOPSIS
+.Fd #include <sys/time.h>
+.Ft int
+.Fn ppsratecheck "struct timeval *lasttime" "int *curpps" "int maxpps"
+.Sh DESCRIPTION
+The
+.Fn ppsratecheck
+function provides easy way to perform packet-per-sec,
+or event-per-sec, rate limitation.
+The motivation for implementing
+.Fn ppsratecheck
+was to provide a mechanism that could be used to add rate limitation to
+network packet output.
+For certain network packets, we may want to impose rate limitation,
+to avoid denial-of-service attack possibilities.
+.Pp
+.Fa maxpps
+specifies maximum permitted packets, or events, per second.
+If
+.Fn ppsratecheck
+is called more than
+.Fa maxpps
+times in a given one second period,
+the function will return 0, indicating that we exceeded the limit.
+If we are below the limit, the function will return 1.
+If
+.Fa maxpps
+is set to 0, the function will always return 0
+.Pq no packets/events are permitted .
+Negative
+.Fa maxpps
+indicates that rate limitation is disabled, and
+.Fa ppsratecheck
+will always return 1.
+.Pp
+.Fa curpps
+and
+.Fa lasttime
+are used to maintain the number of recent calls.
+.Fa curpps
+will be incremented every time
+.Fn ppsratecheck
+is called, and will be reset whenever necessary.
+.Sh SEE ALSO
+.Xr log 9 ,
+.Xr printf 9 ,
+.Xr ratecheck 9 ,
+.Xr time 9
+.Sh HISTORY
+The
+.Fn ppsratecheck
+function appeared in
+.Nx 1.5 .
diff -r 0b829b7a1c12 -r ba4086ec09aa sys/kern/kern_time.c
--- a/sys/kern/kern_time.c      Tue Aug 15 21:49:53 2000 +0000
+++ b/sys/kern/kern_time.c      Wed Aug 16 01:20:29 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_time.c,v 1.47.2.1 2000/07/13 20:18:12 thorpej Exp $       */
+/*     $NetBSD: kern_time.c,v 1.47.2.2 2000/08/16 01:20:31 itojun Exp $        */
 
 /*-
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -653,11 +653,14 @@
        struct timeval *lasttime;
        const struct timeval *mininterval;
 {
-       struct timeval delta;
+       struct timeval tv, delta;
        int s, rv = 0;
 
        s = splclock(); 
-       timersub(&mono_time, lasttime, &delta);
+       tv = mono_time;
+       splx(s);
+
+       timersub(&tv, lasttime, &delta);
 
        /*
         * check for 0,0 is so that the message will be seen at least once,
@@ -665,10 +668,65 @@
         */
        if (timercmp(&delta, mininterval, >=) ||
            (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
-               *lasttime = mono_time;
+               *lasttime = tv;
                rv = 1;
        }
-       splx(s);
 
        return (rv);
 }
+
+/*
+ * ppsratecheck(): packets (or events) per second limitation.
+ */
+int
+ppsratecheck(lasttime, curpps, maxpps)
+       struct timeval *lasttime;
+       int *curpps;
+       int maxpps;     /* maximum pps allowed */
+{
+       struct timeval tv, delta;
+       int s, rv;
+
+       s = splclock(); 
+       tv = mono_time;
+       splx(s);
+
+       timersub(&tv, lasttime, &delta);
+
+       /*
+        * check for 0,0 is so that the message will be seen at least once.
+        * if more than one second have passed since the last update of
+        * lasttime, reset the counter.
+        *
+        * we do increment *curpps even in *curpps < maxpps case, as some may
+        * try to use *curpps for stat purposes as well.
+        */
+       if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
+           delta.tv_sec >= 1) {
+               *lasttime = tv;
+               *curpps = 0;
+               rv = 1;
+       } else if (maxpps < 0)
+               rv = 1;
+       else if (*curpps < maxpps)
+               rv = 1;
+       else
+               rv = 0;
+
+#if 1 /*DIAGNOSTIC?*/
+       /* be careful about wrap-around */
+       if (*curpps + 1 > *curpps)
+               *curpps = *curpps + 1;
+#else
+       /*
+        * assume that there's not too many calls to this function.
+        * not sure if the assumption holds, as it depends on *caller's*
+        * behavior, not the behavior of this function.
+        * IMHO it is wrong to make assumption on the caller's behavior,
+        * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
+        */
+       *curpps = *curpps + 1;
+#endif
+
+       return (rv);
+}
diff -r 0b829b7a1c12 -r ba4086ec09aa sys/sys/time.h
--- a/sys/sys/time.h    Tue Aug 15 21:49:53 2000 +0000
+++ b/sys/sys/time.h    Wed Aug 16 01:20:29 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: time.h,v 1.29 2000/02/03 23:04:45 cgd Exp $    */
+/*     $NetBSD: time.h,v 1.29.4.1 2000/08/16 01:20:32 itojun Exp $     */
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -164,6 +164,7 @@
 void   microtime __P((struct timeval *tv));
 int    settime __P((struct timeval *));
 int    ratecheck __P((struct timeval *, const struct timeval *));
+int    ppsratecheck __P((struct timeval *, int *, int));
 #else /* !_KERNEL */
 
 #ifndef _STANDALONE



Home | Main Index | Thread Index | Old Index