Merge branch 'drm-ttm-unmappable' into drm-core-next
[pandora-kernel.git] / net / netfilter / ipvs / ip_vs_est.c
1 /*
2  * ip_vs_est.c: simple rate estimator for IPVS
3  *
4  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
5  *
6  *              This program is free software; you can redistribute it and/or
7  *              modify it under the terms of the GNU General Public License
8  *              as published by the Free Software Foundation; either version
9  *              2 of the License, or (at your option) any later version.
10  *
11  * Changes:
12  *
13  */
14
15 #define KMSG_COMPONENT "IPVS"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
18 #include <linux/kernel.h>
19 #include <linux/jiffies.h>
20 #include <linux/types.h>
21 #include <linux/interrupt.h>
22 #include <linux/sysctl.h>
23 #include <linux/list.h>
24
25 #include <net/ip_vs.h>
26
27 /*
28   This code is to estimate rate in a shorter interval (such as 8
29   seconds) for virtual services and real servers. For measure rate in a
30   long interval, it is easy to implement a user level daemon which
31   periodically reads those statistical counters and measure rate.
32
33   Currently, the measurement is activated by slow timer handler. Hope
34   this measurement will not introduce too much load.
35
36   We measure rate during the last 8 seconds every 2 seconds:
37
38     avgrate = avgrate*(1-W) + rate*W
39
40     where W = 2^(-2)
41
42   NOTES.
43
44   * The stored value for average bps is scaled by 2^5, so that maximal
45     rate is ~2.15Gbits/s, average pps and cps are scaled by 2^10.
46
47   * A lot code is taken from net/sched/estimator.c
48  */
49
50
51 static void estimation_timer(unsigned long arg);
52
53 static LIST_HEAD(est_list);
54 static DEFINE_SPINLOCK(est_lock);
55 static DEFINE_TIMER(est_timer, estimation_timer, 0, 0);
56
57 static void estimation_timer(unsigned long arg)
58 {
59         struct ip_vs_estimator *e;
60         struct ip_vs_stats *s;
61         u32 n_conns;
62         u32 n_inpkts, n_outpkts;
63         u64 n_inbytes, n_outbytes;
64         u32 rate;
65
66         spin_lock(&est_lock);
67         list_for_each_entry(e, &est_list, list) {
68                 s = container_of(e, struct ip_vs_stats, est);
69
70                 spin_lock(&s->lock);
71                 n_conns = s->ustats.conns;
72                 n_inpkts = s->ustats.inpkts;
73                 n_outpkts = s->ustats.outpkts;
74                 n_inbytes = s->ustats.inbytes;
75                 n_outbytes = s->ustats.outbytes;
76
77                 /* scaled by 2^10, but divided 2 seconds */
78                 rate = (n_conns - e->last_conns)<<9;
79                 e->last_conns = n_conns;
80                 e->cps += ((long)rate - (long)e->cps)>>2;
81                 s->ustats.cps = (e->cps+0x1FF)>>10;
82
83                 rate = (n_inpkts - e->last_inpkts)<<9;
84                 e->last_inpkts = n_inpkts;
85                 e->inpps += ((long)rate - (long)e->inpps)>>2;
86                 s->ustats.inpps = (e->inpps+0x1FF)>>10;
87
88                 rate = (n_outpkts - e->last_outpkts)<<9;
89                 e->last_outpkts = n_outpkts;
90                 e->outpps += ((long)rate - (long)e->outpps)>>2;
91                 s->ustats.outpps = (e->outpps+0x1FF)>>10;
92
93                 rate = (n_inbytes - e->last_inbytes)<<4;
94                 e->last_inbytes = n_inbytes;
95                 e->inbps += ((long)rate - (long)e->inbps)>>2;
96                 s->ustats.inbps = (e->inbps+0xF)>>5;
97
98                 rate = (n_outbytes - e->last_outbytes)<<4;
99                 e->last_outbytes = n_outbytes;
100                 e->outbps += ((long)rate - (long)e->outbps)>>2;
101                 s->ustats.outbps = (e->outbps+0xF)>>5;
102                 spin_unlock(&s->lock);
103         }
104         spin_unlock(&est_lock);
105         mod_timer(&est_timer, jiffies + 2*HZ);
106 }
107
108 void ip_vs_new_estimator(struct ip_vs_stats *stats)
109 {
110         struct ip_vs_estimator *est = &stats->est;
111
112         INIT_LIST_HEAD(&est->list);
113
114         est->last_conns = stats->ustats.conns;
115         est->cps = stats->ustats.cps<<10;
116
117         est->last_inpkts = stats->ustats.inpkts;
118         est->inpps = stats->ustats.inpps<<10;
119
120         est->last_outpkts = stats->ustats.outpkts;
121         est->outpps = stats->ustats.outpps<<10;
122
123         est->last_inbytes = stats->ustats.inbytes;
124         est->inbps = stats->ustats.inbps<<5;
125
126         est->last_outbytes = stats->ustats.outbytes;
127         est->outbps = stats->ustats.outbps<<5;
128
129         spin_lock_bh(&est_lock);
130         list_add(&est->list, &est_list);
131         spin_unlock_bh(&est_lock);
132 }
133
134 void ip_vs_kill_estimator(struct ip_vs_stats *stats)
135 {
136         struct ip_vs_estimator *est = &stats->est;
137
138         spin_lock_bh(&est_lock);
139         list_del(&est->list);
140         spin_unlock_bh(&est_lock);
141 }
142
143 void ip_vs_zero_estimator(struct ip_vs_stats *stats)
144 {
145         struct ip_vs_estimator *est = &stats->est;
146
147         /* set counters zero, caller must hold the stats->lock lock */
148         est->last_inbytes = 0;
149         est->last_outbytes = 0;
150         est->last_conns = 0;
151         est->last_inpkts = 0;
152         est->last_outpkts = 0;
153         est->cps = 0;
154         est->inpps = 0;
155         est->outpps = 0;
156         est->inbps = 0;
157         est->outbps = 0;
158 }
159
160 int __init ip_vs_estimator_init(void)
161 {
162         mod_timer(&est_timer, jiffies + 2 * HZ);
163         return 0;
164 }
165
166 void ip_vs_estimator_cleanup(void)
167 {
168         del_timer_sync(&est_timer);
169 }