1 1.2 pooka /* $NetBSD: kern_rate.c,v 1.2 2012/12/12 11:10:56 pooka Exp $ */ 2 1.1 pooka 3 1.1 pooka /*- 4 1.1 pooka * Copyright (c) 2000, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc. 5 1.1 pooka * All rights reserved. 6 1.1 pooka * 7 1.1 pooka * This code is derived from software contributed to The NetBSD Foundation 8 1.1 pooka * by Christopher G. Demetriou. 9 1.1 pooka * 10 1.1 pooka * Redistribution and use in source and binary forms, with or without 11 1.1 pooka * modification, are permitted provided that the following conditions 12 1.1 pooka * are met: 13 1.1 pooka * 1. Redistributions of source code must retain the above copyright 14 1.1 pooka * notice, this list of conditions and the following disclaimer. 15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 pooka * notice, this list of conditions and the following disclaimer in the 17 1.1 pooka * documentation and/or other materials provided with the distribution. 18 1.1 pooka * 19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 pooka * POSSIBILITY OF SUCH DAMAGE. 30 1.1 pooka */ 31 1.1 pooka 32 1.1 pooka #include <sys/cdefs.h> 33 1.2 pooka __KERNEL_RCSID(0, "$NetBSD: kern_rate.c,v 1.2 2012/12/12 11:10:56 pooka Exp $"); 34 1.1 pooka 35 1.1 pooka #include <sys/param.h> 36 1.1 pooka #include <sys/time.h> 37 1.1 pooka 38 1.1 pooka /* 39 1.1 pooka * ratecheck(): simple time-based rate-limit checking. see ratecheck(9) 40 1.1 pooka * for usage and rationale. 41 1.1 pooka */ 42 1.1 pooka int 43 1.1 pooka ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 44 1.1 pooka { 45 1.1 pooka struct timeval tv, delta; 46 1.1 pooka int rv = 0; 47 1.1 pooka 48 1.1 pooka getmicrouptime(&tv); 49 1.1 pooka timersub(&tv, lasttime, &delta); 50 1.1 pooka 51 1.1 pooka /* 52 1.1 pooka * check for 0,0 is so that the message will be seen at least once, 53 1.1 pooka * even if interval is huge. 54 1.1 pooka */ 55 1.1 pooka if (timercmp(&delta, mininterval, >=) || 56 1.1 pooka (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 57 1.1 pooka *lasttime = tv; 58 1.1 pooka rv = 1; 59 1.1 pooka } 60 1.1 pooka 61 1.1 pooka return (rv); 62 1.1 pooka } 63 1.1 pooka 64 1.1 pooka /* 65 1.1 pooka * ppsratecheck(): packets (or events) per second limitation. 66 1.1 pooka */ 67 1.1 pooka int 68 1.1 pooka ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 69 1.1 pooka { 70 1.1 pooka struct timeval tv, delta; 71 1.1 pooka int rv; 72 1.1 pooka 73 1.1 pooka getmicrouptime(&tv); 74 1.1 pooka timersub(&tv, lasttime, &delta); 75 1.1 pooka 76 1.1 pooka /* 77 1.1 pooka * check for 0,0 is so that the message will be seen at least once. 78 1.1 pooka * if more than one second have passed since the last update of 79 1.1 pooka * lasttime, reset the counter. 80 1.1 pooka * 81 1.1 pooka * we do increment *curpps even in *curpps < maxpps case, as some may 82 1.1 pooka * try to use *curpps for stat purposes as well. 83 1.1 pooka */ 84 1.1 pooka if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 85 1.1 pooka delta.tv_sec >= 1) { 86 1.1 pooka *lasttime = tv; 87 1.1 pooka *curpps = 0; 88 1.1 pooka } 89 1.1 pooka if (maxpps < 0) 90 1.1 pooka rv = 1; 91 1.1 pooka else if (*curpps < maxpps) 92 1.1 pooka rv = 1; 93 1.1 pooka else 94 1.1 pooka rv = 0; 95 1.1 pooka 96 1.1 pooka #if 1 /*DIAGNOSTIC?*/ 97 1.1 pooka /* be careful about wrap-around */ 98 1.2 pooka if (__predict_true(*curpps != INT_MAX)) 99 1.1 pooka *curpps = *curpps + 1; 100 1.1 pooka #else 101 1.1 pooka /* 102 1.1 pooka * assume that there's not too many calls to this function. 103 1.1 pooka * not sure if the assumption holds, as it depends on *caller's* 104 1.1 pooka * behavior, not the behavior of this function. 105 1.1 pooka * IMHO it is wrong to make assumption on the caller's behavior, 106 1.1 pooka * so the above #if is #if 1, not #ifdef DIAGNOSTIC. 107 1.1 pooka */ 108 1.1 pooka *curpps = *curpps + 1; 109 1.1 pooka #endif 110 1.1 pooka 111 1.1 pooka return (rv); 112 1.1 pooka } 113