blob: ff28801962e05883d1a469ac3b2848b51e4429fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_est.c: simple rate estimator for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 */
Hannes Eder9aada7a2009-07-30 14:29:44 -070014
15#define KMSG_COMPONENT "IPVS"
16#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020019#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/types.h>
Adrian Bunk4ffd2e42006-01-05 12:14:43 -080021#include <linux/interrupt.h>
Pavel Emelyanov90754f82008-01-12 02:33:50 -080022#include <linux/sysctl.h>
Sven Wegener3a14a3132008-08-10 18:24:41 +000023#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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
Sven Wegener3a14a3132008-08-10 18:24:41 +000051static void estimation_timer(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Sven Wegener3a14a3132008-08-10 18:24:41 +000053static LIST_HEAD(est_list);
54static DEFINE_SPINLOCK(est_lock);
55static DEFINE_TIMER(est_timer, estimation_timer, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static 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
Sven Wegener3a14a3132008-08-10 18:24:41 +000066 spin_lock(&est_lock);
67 list_for_each_entry(e, &est_list, list) {
68 s = container_of(e, struct ip_vs_stats, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 spin_lock(&s->lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +020071 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
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;
Sven Wegenere9c0ce22008-09-08 13:39:04 +020081 s->ustats.cps = (e->cps+0x1FF)>>10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 rate = (n_inpkts - e->last_inpkts)<<9;
84 e->last_inpkts = n_inpkts;
85 e->inpps += ((long)rate - (long)e->inpps)>>2;
Sven Wegenere9c0ce22008-09-08 13:39:04 +020086 s->ustats.inpps = (e->inpps+0x1FF)>>10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 rate = (n_outpkts - e->last_outpkts)<<9;
89 e->last_outpkts = n_outpkts;
90 e->outpps += ((long)rate - (long)e->outpps)>>2;
Sven Wegenere9c0ce22008-09-08 13:39:04 +020091 s->ustats.outpps = (e->outpps+0x1FF)>>10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 rate = (n_inbytes - e->last_inbytes)<<4;
94 e->last_inbytes = n_inbytes;
95 e->inbps += ((long)rate - (long)e->inbps)>>2;
Sven Wegenere9c0ce22008-09-08 13:39:04 +020096 s->ustats.inbps = (e->inbps+0xF)>>5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 rate = (n_outbytes - e->last_outbytes)<<4;
99 e->last_outbytes = n_outbytes;
100 e->outbps += ((long)rate - (long)e->outbps)>>2;
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200101 s->ustats.outbps = (e->outbps+0xF)>>5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spin_unlock(&s->lock);
103 }
Sven Wegener3a14a3132008-08-10 18:24:41 +0000104 spin_unlock(&est_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 mod_timer(&est_timer, jiffies + 2*HZ);
106}
107
Sven Wegener3a14a3132008-08-10 18:24:41 +0000108void ip_vs_new_estimator(struct ip_vs_stats *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Sven Wegener3a14a3132008-08-10 18:24:41 +0000110 struct ip_vs_estimator *est = &stats->est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Sven Wegener3a14a3132008-08-10 18:24:41 +0000112 INIT_LIST_HEAD(&est->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200114 est->last_conns = stats->ustats.conns;
115 est->cps = stats->ustats.cps<<10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200117 est->last_inpkts = stats->ustats.inpkts;
118 est->inpps = stats->ustats.inpps<<10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200120 est->last_outpkts = stats->ustats.outpkts;
121 est->outpps = stats->ustats.outpps<<10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200123 est->last_inbytes = stats->ustats.inbytes;
124 est->inbps = stats->ustats.inbps<<5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200126 est->last_outbytes = stats->ustats.outbytes;
127 est->outbps = stats->ustats.outbps<<5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Sven Wegener3a14a3132008-08-10 18:24:41 +0000129 spin_lock_bh(&est_lock);
Sven Wegener3a14a3132008-08-10 18:24:41 +0000130 list_add(&est->list, &est_list);
131 spin_unlock_bh(&est_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134void ip_vs_kill_estimator(struct ip_vs_stats *stats)
135{
Sven Wegener3a14a3132008-08-10 18:24:41 +0000136 struct ip_vs_estimator *est = &stats->est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Sven Wegener3a14a3132008-08-10 18:24:41 +0000138 spin_lock_bh(&est_lock);
139 list_del(&est->list);
Sven Wegener3a14a3132008-08-10 18:24:41 +0000140 spin_unlock_bh(&est_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143void ip_vs_zero_estimator(struct ip_vs_stats *stats)
144{
Sven Wegener3a14a3132008-08-10 18:24:41 +0000145 struct ip_vs_estimator *est = &stats->est;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Sven Wegener3a14a3132008-08-10 18:24:41 +0000147 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
Sven Wegenera919cf42008-08-14 00:47:16 +0200159
160int __init ip_vs_estimator_init(void)
161{
162 mod_timer(&est_timer, jiffies + 2 * HZ);
163 return 0;
164}
165
166void ip_vs_estimator_cleanup(void)
167{
168 del_timer_sync(&est_timer);
169}