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