Revert "net/sched: flower: Fix wrong handle assignment during filter change"
[linux-block.git] / net / core / gen_estimator.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * net/sched/gen_estimator.c Simple rate estimator.
4 *
1da177e4 5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
1c0d32fd 6 * Eric Dumazet <edumazet@google.com>
1da177e4
LT
7 *
8 * Changes:
9 * Jamal Hadi Salim - moved it to net/core and reshulfed
10 * names to make it usable in general net subsystem.
11 */
12
7c0f6ba6 13#include <linux/uaccess.h>
1977f032 14#include <linux/bitops.h>
1da177e4
LT
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/jiffies.h>
19#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/socket.h>
22#include <linux/sockios.h>
23#include <linux/in.h>
24#include <linux/errno.h>
25#include <linux/interrupt.h>
26#include <linux/netdevice.h>
27#include <linux/skbuff.h>
28#include <linux/rtnetlink.h>
29#include <linux/init.h>
5a0e3ad6 30#include <linux/slab.h>
1c0d32fd 31#include <linux/seqlock.h>
1da177e4
LT
32#include <net/sock.h>
33#include <net/gen_stats.h>
34
1c0d32fd
ED
35/* This code is NOT intended to be used for statistics collection,
36 * its purpose is to provide a base for statistical multiplexing
37 * for controlled load service.
38 * If you need only statistics, run a user level daemon which
39 * periodically reads byte counters.
1da177e4
LT
40 */
41
1c0d32fd 42struct net_rate_estimator {
50dc9a85 43 struct gnet_stats_basic_sync *bstats;
1da177e4 44 spinlock_t *stats_lock;
29cbcd85 45 bool running;
50dc9a85 46 struct gnet_stats_basic_sync __percpu *cpu_bstats;
1c0d32fd
ED
47 u8 ewma_log;
48 u8 intvl_log; /* period : (250ms << intvl_log) */
49
50 seqcount_t seq;
1c8dd9cb 51 u64 last_packets;
1da177e4 52 u64 last_bytes;
1c0d32fd
ED
53
54 u64 avpps;
511e11e3 55 u64 avbps;
1da177e4 56
1c0d32fd
ED
57 unsigned long next_jiffies;
58 struct timer_list timer;
59 struct rcu_head rcu;
1da177e4
LT
60};
61
1c0d32fd 62static void est_fetch_counters(struct net_rate_estimator *e,
50dc9a85 63 struct gnet_stats_basic_sync *b)
1da177e4 64{
50dc9a85 65 gnet_stats_basic_sync_init(b);
1c0d32fd
ED
66 if (e->stats_lock)
67 spin_lock(e->stats_lock);
1da177e4 68
29cbcd85 69 gnet_stats_add_basic(b, e->cpu_bstats, e->bstats, e->running);
1da177e4 70
1c0d32fd
ED
71 if (e->stats_lock)
72 spin_unlock(e->stats_lock);
12efa1fa 73
1da177e4
LT
74}
75
e99e88a9 76static void est_timer(struct timer_list *t)
4db0acf3 77{
e99e88a9 78 struct net_rate_estimator *est = from_timer(est, t, timer);
50dc9a85
AD
79 struct gnet_stats_basic_sync b;
80 u64 b_bytes, b_packets;
1c0d32fd 81 u64 rate, brate;
4db0acf3 82
1c0d32fd 83 est_fetch_counters(est, &b);
50dc9a85
AD
84 b_bytes = u64_stats_read(&b.bytes);
85 b_packets = u64_stats_read(&b.packets);
86
87 brate = (b_bytes - est->last_bytes) << (10 - est->intvl_log);
dd5e0733 88 brate = (brate >> est->ewma_log) - (est->avbps >> est->ewma_log);
4db0acf3 89
50dc9a85 90 rate = (b_packets - est->last_packets) << (10 - est->intvl_log);
dd5e0733 91 rate = (rate >> est->ewma_log) - (est->avpps >> est->ewma_log);
4db0acf3 92
1c0d32fd
ED
93 write_seqcount_begin(&est->seq);
94 est->avbps += brate;
95 est->avpps += rate;
96 write_seqcount_end(&est->seq);
4db0acf3 97
50dc9a85
AD
98 est->last_bytes = b_bytes;
99 est->last_packets = b_packets;
4db0acf3 100
1c0d32fd 101 est->next_jiffies += ((HZ/4) << est->intvl_log);
4db0acf3 102
1c0d32fd
ED
103 if (unlikely(time_after_eq(jiffies, est->next_jiffies))) {
104 /* Ouch... timer was delayed. */
105 est->next_jiffies = jiffies + 1;
4db0acf3 106 }
1c0d32fd 107 mod_timer(&est->timer, est->next_jiffies);
4db0acf3
JP
108}
109
1da177e4
LT
110/**
111 * gen_new_estimator - create a new rate estimator
112 * @bstats: basic statistics
e9fc2f05 113 * @cpu_bstats: bstats per cpu
1da177e4 114 * @rate_est: rate estimator statistics
51a9f5ae 115 * @lock: lock for statistics and control path
29cbcd85
AD
116 * @running: true if @bstats represents a running qdisc, thus @bstats'
117 * internal values might change during basic reads. Only used
118 * if @bstats_cpu is NULL
1da177e4
LT
119 * @opt: rate estimator configuration TLV
120 *
121 * Creates a new rate estimator with &bstats as source and &rate_est
122 * as destination. A new timer with the interval specified in the
123 * configuration TLV is created. Upon each interval, the latest statistics
124 * will be read from &bstats and the estimated rate will be stored in
e793c0f7 125 * &rate_est with the statistics lock grabbed during this period.
4ec93edb 126 *
1da177e4 127 * Returns 0 on success or a negative error code.
0929c2dd 128 *
1da177e4 129 */
50dc9a85
AD
130int gen_new_estimator(struct gnet_stats_basic_sync *bstats,
131 struct gnet_stats_basic_sync __percpu *cpu_bstats,
1c0d32fd 132 struct net_rate_estimator __rcu **rate_est,
51a9f5ae 133 spinlock_t *lock,
29cbcd85 134 bool running,
1e90474c 135 struct nlattr *opt)
1da177e4 136{
1e90474c 137 struct gnet_estimator *parm = nla_data(opt);
1c0d32fd 138 struct net_rate_estimator *old, *est;
50dc9a85 139 struct gnet_stats_basic_sync b;
1c0d32fd 140 int intvl_log;
1da177e4 141
1e90474c 142 if (nla_len(opt) < sizeof(*parm))
1da177e4
LT
143 return -EINVAL;
144
1c0d32fd
ED
145 /* allowed timer periods are :
146 * -2 : 250ms, -1 : 500ms, 0 : 1 sec
147 * 1 : 2 sec, 2 : 4 sec, 3 : 8 sec
148 */
1da177e4
LT
149 if (parm->interval < -2 || parm->interval > 3)
150 return -EINVAL;
151
dd5e0733
ED
152 if (parm->ewma_log == 0 || parm->ewma_log >= 31)
153 return -EINVAL;
154
77d04bd9 155 est = kzalloc(sizeof(*est), GFP_KERNEL);
1c0d32fd 156 if (!est)
1da177e4
LT
157 return -ENOBUFS;
158
1c0d32fd
ED
159 seqcount_init(&est->seq);
160 intvl_log = parm->interval + 2;
1da177e4 161 est->bstats = bstats;
51a9f5ae 162 est->stats_lock = lock;
edb09eb1 163 est->running = running;
1da177e4 164 est->ewma_log = parm->ewma_log;
1c0d32fd 165 est->intvl_log = intvl_log;
22e0f8b9 166 est->cpu_bstats = cpu_bstats;
1da177e4 167
51a9f5ae 168 if (lock)
40ca54e3 169 local_bh_disable();
1c0d32fd 170 est_fetch_counters(est, &b);
51a9f5ae 171 if (lock)
40ca54e3 172 local_bh_enable();
50dc9a85
AD
173 est->last_bytes = u64_stats_read(&b.bytes);
174 est->last_packets = u64_stats_read(&b.packets);
51a9f5ae
VB
175
176 if (lock)
177 spin_lock_bh(lock);
1c0d32fd
ED
178 old = rcu_dereference_protected(*rate_est, 1);
179 if (old) {
180 del_timer_sync(&old->timer);
181 est->avbps = old->avbps;
182 est->avpps = old->avpps;
1da177e4 183 }
0929c2dd 184
1c0d32fd 185 est->next_jiffies = jiffies + ((HZ/4) << intvl_log);
e99e88a9 186 timer_setup(&est->timer, est_timer, 0);
1c0d32fd 187 mod_timer(&est->timer, est->next_jiffies);
4db0acf3 188
1c0d32fd 189 rcu_assign_pointer(*rate_est, est);
51a9f5ae
VB
190 if (lock)
191 spin_unlock_bh(lock);
1c0d32fd
ED
192 if (old)
193 kfree_rcu(old, rcu);
1da177e4
LT
194 return 0;
195}
c1b56878 196EXPORT_SYMBOL(gen_new_estimator);
1da177e4
LT
197
198/**
199 * gen_kill_estimator - remove a rate estimator
1c0d32fd 200 * @rate_est: rate estimator
1da177e4 201 *
1c0d32fd 202 * Removes the rate estimator.
0929c2dd 203 *
1da177e4 204 */
1c0d32fd 205void gen_kill_estimator(struct net_rate_estimator __rcu **rate_est)
1da177e4 206{
1c0d32fd 207 struct net_rate_estimator *est;
1da177e4 208
1c0d32fd
ED
209 est = xchg((__force struct net_rate_estimator **)rate_est, NULL);
210 if (est) {
292a089d 211 timer_shutdown_sync(&est->timer);
1c0d32fd 212 kfree_rcu(est, rcu);
1da177e4
LT
213 }
214}
c1b56878 215EXPORT_SYMBOL(gen_kill_estimator);
1da177e4
LT
216
217/**
96750162 218 * gen_replace_estimator - replace rate estimator configuration
1da177e4 219 * @bstats: basic statistics
e9fc2f05 220 * @cpu_bstats: bstats per cpu
1da177e4 221 * @rate_est: rate estimator statistics
51a9f5ae 222 * @lock: lock for statistics and control path
29cbcd85
AD
223 * @running: true if @bstats represents a running qdisc, thus @bstats'
224 * internal values might change during basic reads. Only used
225 * if @cpu_bstats is NULL
1da177e4
LT
226 * @opt: rate estimator configuration TLV
227 *
228 * Replaces the configuration of a rate estimator by calling
229 * gen_kill_estimator() and gen_new_estimator().
4ec93edb 230 *
1da177e4
LT
231 * Returns 0 on success or a negative error code.
232 */
50dc9a85
AD
233int gen_replace_estimator(struct gnet_stats_basic_sync *bstats,
234 struct gnet_stats_basic_sync __percpu *cpu_bstats,
1c0d32fd 235 struct net_rate_estimator __rcu **rate_est,
51a9f5ae 236 spinlock_t *lock,
29cbcd85 237 bool running, struct nlattr *opt)
1da177e4 238{
1c0d32fd 239 return gen_new_estimator(bstats, cpu_bstats, rate_est,
51a9f5ae 240 lock, running, opt);
1da177e4 241}
c1b56878
SH
242EXPORT_SYMBOL(gen_replace_estimator);
243
244/**
245 * gen_estimator_active - test if estimator is currently in use
1c0d32fd 246 * @rate_est: rate estimator
c1b56878 247 *
244e6c2d 248 * Returns true if estimator is active, and false if not.
c1b56878 249 */
1c0d32fd 250bool gen_estimator_active(struct net_rate_estimator __rcu **rate_est)
c1b56878 251{
1c0d32fd
ED
252 return !!rcu_access_pointer(*rate_est);
253}
254EXPORT_SYMBOL(gen_estimator_active);
ae638c47 255
1c0d32fd
ED
256bool gen_estimator_read(struct net_rate_estimator __rcu **rate_est,
257 struct gnet_stats_rate_est64 *sample)
258{
259 struct net_rate_estimator *est;
260 unsigned seq;
261
262 rcu_read_lock();
263 est = rcu_dereference(*rate_est);
264 if (!est) {
265 rcu_read_unlock();
266 return false;
267 }
1da177e4 268
1c0d32fd
ED
269 do {
270 seq = read_seqcount_begin(&est->seq);
271 sample->bps = est->avbps >> 8;
272 sample->pps = est->avpps >> 8;
273 } while (read_seqcount_retry(&est->seq, seq));
ae638c47 274
1c0d32fd
ED
275 rcu_read_unlock();
276 return true;
c1b56878 277}
1c0d32fd 278EXPORT_SYMBOL(gen_estimator_read);