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 | * |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 11 | * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com> |
| 12 | * Network name space (netns) aware. |
| 13 | * Global data moved to netns i.e struct netns_ipvs |
| 14 | * Affected data: est_list and est_lock. |
| 15 | * estimation_timer() runs with timer per netns. |
| 16 | * get_stats()) do the per cpu summing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 18 | |
| 19 | #define KMSG_COMPONENT "IPVS" |
| 20 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 23 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/types.h> |
Adrian Bunk | 4ffd2e4 | 2006-01-05 12:14:43 -0800 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
Pavel Emelyanov | 90754f8 | 2008-01-12 02:33:50 -0800 | [diff] [blame] | 26 | #include <linux/sysctl.h> |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 27 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <net/ip_vs.h> |
| 30 | |
| 31 | /* |
| 32 | This code is to estimate rate in a shorter interval (such as 8 |
| 33 | seconds) for virtual services and real servers. For measure rate in a |
| 34 | long interval, it is easy to implement a user level daemon which |
| 35 | periodically reads those statistical counters and measure rate. |
| 36 | |
| 37 | Currently, the measurement is activated by slow timer handler. Hope |
| 38 | this measurement will not introduce too much load. |
| 39 | |
| 40 | We measure rate during the last 8 seconds every 2 seconds: |
| 41 | |
| 42 | avgrate = avgrate*(1-W) + rate*W |
| 43 | |
| 44 | where W = 2^(-2) |
| 45 | |
| 46 | NOTES. |
| 47 | |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 48 | * Average bps is scaled by 2^5, while average pps and cps are scaled by 2^10. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 50 | * Netlink users can see 64-bit values but sockopt users are restricted |
| 51 | to 32-bit values for conns, packets, bps, cps and pps. |
| 52 | |
| 53 | * A lot of code is taken from net/core/gen_estimator.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
| 55 | |
| 56 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 57 | /* |
| 58 | * Make a summary from each cpu |
| 59 | */ |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 60 | static void ip_vs_read_cpu_stats(struct ip_vs_kstats *sum, |
Julian Anastasov | b962abd | 2013-03-09 23:25:08 +0200 | [diff] [blame] | 61 | struct ip_vs_cpu_stats __percpu *stats) |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 62 | { |
| 63 | int i; |
Julian Anastasov | d1ee4fe | 2013-09-12 11:21:10 +0300 | [diff] [blame] | 64 | bool add = false; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 65 | |
| 66 | for_each_possible_cpu(i) { |
| 67 | struct ip_vs_cpu_stats *s = per_cpu_ptr(stats, i); |
| 68 | unsigned int start; |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 69 | u64 conns, inpkts, outpkts, inbytes, outbytes; |
| 70 | |
Julian Anastasov | d1ee4fe | 2013-09-12 11:21:10 +0300 | [diff] [blame] | 71 | if (add) { |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 72 | do { |
Julian Anastasov | 4a569c0 | 2011-03-04 12:28:20 +0200 | [diff] [blame] | 73 | start = u64_stats_fetch_begin(&s->syncp); |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 74 | conns = s->cnt.conns; |
| 75 | inpkts = s->cnt.inpkts; |
| 76 | outpkts = s->cnt.outpkts; |
| 77 | inbytes = s->cnt.inbytes; |
| 78 | outbytes = s->cnt.outbytes; |
Julian Anastasov | 4a569c0 | 2011-03-04 12:28:20 +0200 | [diff] [blame] | 79 | } while (u64_stats_fetch_retry(&s->syncp, start)); |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 80 | sum->conns += conns; |
| 81 | sum->inpkts += inpkts; |
| 82 | sum->outpkts += outpkts; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 83 | sum->inbytes += inbytes; |
| 84 | sum->outbytes += outbytes; |
| 85 | } else { |
Julian Anastasov | d1ee4fe | 2013-09-12 11:21:10 +0300 | [diff] [blame] | 86 | add = true; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 87 | do { |
Julian Anastasov | 4a569c0 | 2011-03-04 12:28:20 +0200 | [diff] [blame] | 88 | start = u64_stats_fetch_begin(&s->syncp); |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 89 | sum->conns = s->cnt.conns; |
| 90 | sum->inpkts = s->cnt.inpkts; |
| 91 | sum->outpkts = s->cnt.outpkts; |
| 92 | sum->inbytes = s->cnt.inbytes; |
| 93 | sum->outbytes = s->cnt.outbytes; |
Julian Anastasov | 4a569c0 | 2011-03-04 12:28:20 +0200 | [diff] [blame] | 94 | } while (u64_stats_fetch_retry(&s->syncp, start)); |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static void estimation_timer(unsigned long arg) |
| 101 | { |
| 102 | struct ip_vs_estimator *e; |
| 103 | struct ip_vs_stats *s; |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 104 | u64 rate; |
Eric W. Biederman | 70a131a | 2015-09-21 13:02:27 -0500 | [diff] [blame] | 105 | struct netns_ipvs *ipvs = (struct netns_ipvs *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 107 | spin_lock(&ipvs->est_lock); |
| 108 | list_for_each_entry(e, &ipvs->est_list, list) { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 109 | s = container_of(e, struct ip_vs_stats, est); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | spin_lock(&s->lock); |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 112 | ip_vs_read_cpu_stats(&s->kstats, s->cpustats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* scaled by 2^10, but divided 2 seconds */ |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 115 | rate = (s->kstats.conns - e->last_conns) << 9; |
| 116 | e->last_conns = s->kstats.conns; |
| 117 | e->cps += ((s64)rate - (s64)e->cps) >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 119 | rate = (s->kstats.inpkts - e->last_inpkts) << 9; |
| 120 | e->last_inpkts = s->kstats.inpkts; |
| 121 | e->inpps += ((s64)rate - (s64)e->inpps) >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 123 | rate = (s->kstats.outpkts - e->last_outpkts) << 9; |
| 124 | e->last_outpkts = s->kstats.outpkts; |
| 125 | e->outpps += ((s64)rate - (s64)e->outpps) >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 127 | /* scaled by 2^5, but divided 2 seconds */ |
| 128 | rate = (s->kstats.inbytes - e->last_inbytes) << 4; |
| 129 | e->last_inbytes = s->kstats.inbytes; |
| 130 | e->inbps += ((s64)rate - (s64)e->inbps) >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 132 | rate = (s->kstats.outbytes - e->last_outbytes) << 4; |
| 133 | e->last_outbytes = s->kstats.outbytes; |
| 134 | e->outbps += ((s64)rate - (s64)e->outbps) >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | spin_unlock(&s->lock); |
| 136 | } |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 137 | spin_unlock(&ipvs->est_lock); |
| 138 | mod_timer(&ipvs->est_timer, jiffies + 2*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Eric W. Biederman | 0f34d54 | 2015-09-21 13:02:23 -0500 | [diff] [blame] | 141 | void ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 143 | struct ip_vs_estimator *est = &stats->est; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 145 | INIT_LIST_HEAD(&est->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 147 | spin_lock_bh(&ipvs->est_lock); |
| 148 | list_add(&est->list, &ipvs->est_list); |
| 149 | spin_unlock_bh(&ipvs->est_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Eric W. Biederman | 0f34d54 | 2015-09-21 13:02:23 -0500 | [diff] [blame] | 152 | void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 154 | struct ip_vs_estimator *est = &stats->est; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 156 | spin_lock_bh(&ipvs->est_lock); |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 157 | list_del(&est->list); |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 158 | spin_unlock_bh(&ipvs->est_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void ip_vs_zero_estimator(struct ip_vs_stats *stats) |
| 162 | { |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 163 | struct ip_vs_estimator *est = &stats->est; |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 164 | struct ip_vs_kstats *k = &stats->kstats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Julian Anastasov | 55a3d4e | 2011-03-14 01:37:49 +0200 | [diff] [blame] | 166 | /* reset counters, caller must hold the stats->lock lock */ |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 167 | est->last_inbytes = k->inbytes; |
| 168 | est->last_outbytes = k->outbytes; |
| 169 | est->last_conns = k->conns; |
| 170 | est->last_inpkts = k->inpkts; |
| 171 | est->last_outpkts = k->outpkts; |
Sven Wegener | 3a14a313 | 2008-08-10 18:24:41 +0000 | [diff] [blame] | 172 | est->cps = 0; |
| 173 | est->inpps = 0; |
| 174 | est->outpps = 0; |
| 175 | est->inbps = 0; |
| 176 | est->outbps = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 178 | |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 179 | /* Get decoded rates */ |
Julian Anastasov | cd67cd5 | 2015-02-06 09:44:44 +0200 | [diff] [blame] | 180 | void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats) |
Julian Anastasov | ea9f22c | 2011-03-14 01:41:54 +0200 | [diff] [blame] | 181 | { |
| 182 | struct ip_vs_estimator *e = &stats->est; |
| 183 | |
| 184 | dst->cps = (e->cps + 0x1FF) >> 10; |
| 185 | dst->inpps = (e->inpps + 0x1FF) >> 10; |
| 186 | dst->outpps = (e->outpps + 0x1FF) >> 10; |
| 187 | dst->inbps = (e->inbps + 0xF) >> 5; |
| 188 | dst->outbps = (e->outbps + 0xF) >> 5; |
| 189 | } |
| 190 | |
Eric W. Biederman | a4dd036 | 2015-09-21 13:02:28 -0500 | [diff] [blame] | 191 | int __net_init ip_vs_estimator_net_init(struct netns_ipvs *ipvs) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 192 | { |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 193 | INIT_LIST_HEAD(&ipvs->est_list); |
| 194 | spin_lock_init(&ipvs->est_lock); |
Eric W. Biederman | 70a131a | 2015-09-21 13:02:27 -0500 | [diff] [blame] | 195 | setup_timer(&ipvs->est_timer, estimation_timer, (unsigned long)ipvs); |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 196 | mod_timer(&ipvs->est_timer, jiffies + 2 * HZ); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Eric W. Biederman | a4dd036 | 2015-09-21 13:02:28 -0500 | [diff] [blame] | 200 | void __net_exit ip_vs_estimator_net_cleanup(struct netns_ipvs *ipvs) |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 201 | { |
Eric W. Biederman | a4dd036 | 2015-09-21 13:02:28 -0500 | [diff] [blame] | 202 | del_timer_sync(&ipvs->est_timer); |
Hans Schillstrom | 29c2026 | 2011-01-03 14:44:54 +0100 | [diff] [blame] | 203 | } |