Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ip_vs_est.c: simple rate estimator for IPVS |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 14 | |
| 15 | #define KMSG_COMPONENT "IPVS" |
| 16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 19 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/types.h> |
Adrian Bunk | 4ffd2e4 | 2006-01-05 12:14:43 -0800 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Pavel Emelyanov | 90754f8 | 2008-01-12 02:33:50 -0800 | [diff] [blame] | 22 | #include <linux/sysctl.h> |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 23 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 51 | static void estimation_timer(unsigned long arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 53 | static LIST_HEAD(est_list); |
| 54 | static DEFINE_SPINLOCK(est_lock); |
| 55 | static DEFINE_TIMER(est_timer, estimation_timer, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 66 | spin_lock(&est_lock); |
| 67 | list_for_each_entry(e, &est_list, list) { |
| 68 | s = container_of(e, struct ip_vs_stats, est); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | spin_lock(&s->lock); |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 81 | s->ustats.cps = (e->cps+0x1FF)>>10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | rate = (n_inpkts - e->last_inpkts)<<9; |
| 84 | e->last_inpkts = n_inpkts; |
| 85 | e->inpps += ((long)rate - (long)e->inpps)>>2; |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 86 | s->ustats.inpps = (e->inpps+0x1FF)>>10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | rate = (n_outpkts - e->last_outpkts)<<9; |
| 89 | e->last_outpkts = n_outpkts; |
| 90 | e->outpps += ((long)rate - (long)e->outpps)>>2; |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 91 | s->ustats.outpps = (e->outpps+0x1FF)>>10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | rate = (n_inbytes - e->last_inbytes)<<4; |
| 94 | e->last_inbytes = n_inbytes; |
| 95 | e->inbps += ((long)rate - (long)e->inbps)>>2; |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 96 | s->ustats.inbps = (e->inbps+0xF)>>5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | rate = (n_outbytes - e->last_outbytes)<<4; |
| 99 | e->last_outbytes = n_outbytes; |
| 100 | e->outbps += ((long)rate - (long)e->outbps)>>2; |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 101 | s->ustats.outbps = (e->outbps+0xF)>>5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | spin_unlock(&s->lock); |
| 103 | } |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 104 | spin_unlock(&est_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | mod_timer(&est_timer, jiffies + 2*HZ); |
| 106 | } |
| 107 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 108 | void ip_vs_new_estimator(struct ip_vs_stats *stats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 110 | struct ip_vs_estimator *est = &stats->est; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 112 | INIT_LIST_HEAD(&est->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 114 | est->last_conns = stats->ustats.conns; |
| 115 | est->cps = stats->ustats.cps<<10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 117 | est->last_inpkts = stats->ustats.inpkts; |
| 118 | est->inpps = stats->ustats.inpps<<10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 120 | est->last_outpkts = stats->ustats.outpkts; |
| 121 | est->outpps = stats->ustats.outpps<<10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 123 | est->last_inbytes = stats->ustats.inbytes; |
| 124 | est->inbps = stats->ustats.inbps<<5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Sven Wegener | e9c0ce2 | 2008-09-08 13:39:04 +0200 | [diff] [blame] | 126 | est->last_outbytes = stats->ustats.outbytes; |
| 127 | est->outbps = stats->ustats.outbps<<5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 129 | spin_lock_bh(&est_lock); |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 130 | list_add(&est->list, &est_list); |
| 131 | spin_unlock_bh(&est_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void ip_vs_kill_estimator(struct ip_vs_stats *stats) |
| 135 | { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 136 | struct ip_vs_estimator *est = &stats->est; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 138 | spin_lock_bh(&est_lock); |
| 139 | list_del(&est->list); |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 140 | spin_unlock_bh(&est_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void ip_vs_zero_estimator(struct ip_vs_stats *stats) |
| 144 | { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 145 | struct ip_vs_estimator *est = &stats->est; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 159 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame^] | 160 | static int __net_init __ip_vs_estimator_init(struct net *net) |
| 161 | { |
| 162 | if (!net_eq(net, &init_net)) /* netns not enabled yet */ |
| 163 | return -EPERM; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static struct pernet_operations ip_vs_app_ops = { |
| 169 | .init = __ip_vs_estimator_init, |
| 170 | }; |
| 171 | |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 172 | int __init ip_vs_estimator_init(void) |
| 173 | { |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame^] | 174 | int rv; |
| 175 | |
| 176 | rv = register_pernet_subsys(&ip_vs_app_ops); |
| 177 | if (rv < 0) |
| 178 | return rv; |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 179 | mod_timer(&est_timer, jiffies + 2 * HZ); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame^] | 180 | return rv; |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void ip_vs_estimator_cleanup(void) |
| 184 | { |
| 185 | del_timer_sync(&est_timer); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame^] | 186 | unregister_pernet_subsys(&ip_vs_app_ops); |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 187 | } |