12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * net/sched/gen_estimator.c Simple rate estimator.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
61c0d32fdSEric Dumazet * Eric Dumazet <edumazet@google.com>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * Changes:
91da177e4SLinus Torvalds * Jamal Hadi Salim - moved it to net/core and reshulfed
101da177e4SLinus Torvalds * names to make it usable in general net subsystem.
111da177e4SLinus Torvalds */
121da177e4SLinus Torvalds
137c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
141977f032SJiri Slaby #include <linux/bitops.h>
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/jiffies.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
211da177e4SLinus Torvalds #include <linux/socket.h>
221da177e4SLinus Torvalds #include <linux/sockios.h>
231da177e4SLinus Torvalds #include <linux/in.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/interrupt.h>
261da177e4SLinus Torvalds #include <linux/netdevice.h>
271da177e4SLinus Torvalds #include <linux/skbuff.h>
281da177e4SLinus Torvalds #include <linux/rtnetlink.h>
291da177e4SLinus Torvalds #include <linux/init.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
311c0d32fdSEric Dumazet #include <linux/seqlock.h>
321da177e4SLinus Torvalds #include <net/sock.h>
331da177e4SLinus Torvalds #include <net/gen_stats.h>
341da177e4SLinus Torvalds
351c0d32fdSEric Dumazet /* This code is NOT intended to be used for statistics collection,
361c0d32fdSEric Dumazet * its purpose is to provide a base for statistical multiplexing
371c0d32fdSEric Dumazet * for controlled load service.
381c0d32fdSEric Dumazet * If you need only statistics, run a user level daemon which
391c0d32fdSEric Dumazet * periodically reads byte counters.
401da177e4SLinus Torvalds */
411da177e4SLinus Torvalds
421c0d32fdSEric Dumazet struct net_rate_estimator {
4350dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync *bstats;
441da177e4SLinus Torvalds spinlock_t *stats_lock;
4529cbcd85SAhmed S. Darwish bool running;
4650dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu_bstats;
471c0d32fdSEric Dumazet u8 ewma_log;
481c0d32fdSEric Dumazet u8 intvl_log; /* period : (250ms << intvl_log) */
491da177e4SLinus Torvalds
501c0d32fdSEric Dumazet seqcount_t seq;
511c8dd9cbSEric Dumazet u64 last_packets;
521c0d32fdSEric Dumazet u64 last_bytes;
531c0d32fdSEric Dumazet
541c0d32fdSEric Dumazet u64 avpps;
551c0d32fdSEric Dumazet u64 avbps;
561c0d32fdSEric Dumazet
5712efa1faSEric Dumazet unsigned long next_jiffies;
581da177e4SLinus Torvalds struct timer_list timer;
591c0d32fdSEric Dumazet struct rcu_head rcu;
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds
est_fetch_counters(struct net_rate_estimator * e,struct gnet_stats_basic_sync * b)621c0d32fdSEric Dumazet static void est_fetch_counters(struct net_rate_estimator *e,
6350dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync *b)
641c0d32fdSEric Dumazet {
6550dc9a85SAhmed S. Darwish gnet_stats_basic_sync_init(b);
661c0d32fdSEric Dumazet if (e->stats_lock)
671c0d32fdSEric Dumazet spin_lock(e->stats_lock);
681da177e4SLinus Torvalds
6929cbcd85SAhmed S. Darwish gnet_stats_add_basic(b, e->cpu_bstats, e->bstats, e->running);
701da177e4SLinus Torvalds
711c0d32fdSEric Dumazet if (e->stats_lock)
721c0d32fdSEric Dumazet spin_unlock(e->stats_lock);
731c0d32fdSEric Dumazet
741c0d32fdSEric Dumazet }
754db0acf3SJarek Poplawski
est_timer(struct timer_list * t)76e99e88a9SKees Cook static void est_timer(struct timer_list *t)
771da177e4SLinus Torvalds {
78e99e88a9SKees Cook struct net_rate_estimator *est = from_timer(est, t, timer);
7950dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync b;
8050dc9a85SAhmed S. Darwish u64 b_bytes, b_packets;
811c0d32fdSEric Dumazet u64 rate, brate;
821da177e4SLinus Torvalds
831c0d32fdSEric Dumazet est_fetch_counters(est, &b);
8450dc9a85SAhmed S. Darwish b_bytes = u64_stats_read(&b.bytes);
8550dc9a85SAhmed S. Darwish b_packets = u64_stats_read(&b.packets);
8650dc9a85SAhmed S. Darwish
8750dc9a85SAhmed S. Darwish brate = (b_bytes - est->last_bytes) << (10 - est->intvl_log);
88dd5e0733SEric Dumazet brate = (brate >> est->ewma_log) - (est->avbps >> est->ewma_log);
891da177e4SLinus Torvalds
9050dc9a85SAhmed S. Darwish rate = (b_packets - est->last_packets) << (10 - est->intvl_log);
91dd5e0733SEric Dumazet rate = (rate >> est->ewma_log) - (est->avpps >> est->ewma_log);
920929c2ddSRanko Zivojnovic
931c0d32fdSEric Dumazet write_seqcount_begin(&est->seq);
941c0d32fdSEric Dumazet est->avbps += brate;
951c0d32fdSEric Dumazet est->avpps += rate;
961c0d32fdSEric Dumazet write_seqcount_end(&est->seq);
9722e0f8b9SJohn Fastabend
9850dc9a85SAhmed S. Darwish est->last_bytes = b_bytes;
9950dc9a85SAhmed S. Darwish est->last_packets = b_packets;
1001da177e4SLinus Torvalds
1011c0d32fdSEric Dumazet est->next_jiffies += ((HZ/4) << est->intvl_log);
1021da177e4SLinus Torvalds
1031c0d32fdSEric Dumazet if (unlikely(time_after_eq(jiffies, est->next_jiffies))) {
10412efa1faSEric Dumazet /* Ouch... timer was delayed. */
1051c0d32fdSEric Dumazet est->next_jiffies = jiffies + 1;
10612efa1faSEric Dumazet }
1071c0d32fdSEric Dumazet mod_timer(&est->timer, est->next_jiffies);
1084db0acf3SJarek Poplawski }
1094db0acf3SJarek Poplawski
1101da177e4SLinus Torvalds /**
1111da177e4SLinus Torvalds * gen_new_estimator - create a new rate estimator
1121da177e4SLinus Torvalds * @bstats: basic statistics
113e9fc2f05SLuis de Bethencourt * @cpu_bstats: bstats per cpu
1141da177e4SLinus Torvalds * @rate_est: rate estimator statistics
11551a9f5aeSVlad Buslov * @lock: lock for statistics and control path
11629cbcd85SAhmed S. Darwish * @running: true if @bstats represents a running qdisc, thus @bstats'
11729cbcd85SAhmed S. Darwish * internal values might change during basic reads. Only used
11829cbcd85SAhmed S. Darwish * if @bstats_cpu is NULL
1191da177e4SLinus Torvalds * @opt: rate estimator configuration TLV
1201da177e4SLinus Torvalds *
1211da177e4SLinus Torvalds * Creates a new rate estimator with &bstats as source and &rate_est
1221da177e4SLinus Torvalds * as destination. A new timer with the interval specified in the
1231da177e4SLinus Torvalds * configuration TLV is created. Upon each interval, the latest statistics
1241da177e4SLinus Torvalds * will be read from &bstats and the estimated rate will be stored in
125e793c0f7SMasanari Iida * &rate_est with the statistics lock grabbed during this period.
1261da177e4SLinus Torvalds *
1271da177e4SLinus Torvalds * Returns 0 on success or a negative error code.
1280929c2ddSRanko Zivojnovic *
1291da177e4SLinus Torvalds */
gen_new_estimator(struct gnet_stats_basic_sync * bstats,struct gnet_stats_basic_sync __percpu * cpu_bstats,struct net_rate_estimator __rcu ** rate_est,spinlock_t * lock,bool running,struct nlattr * opt)13050dc9a85SAhmed S. Darwish int gen_new_estimator(struct gnet_stats_basic_sync *bstats,
13150dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu_bstats,
1321c0d32fdSEric Dumazet struct net_rate_estimator __rcu **rate_est,
13351a9f5aeSVlad Buslov spinlock_t *lock,
13429cbcd85SAhmed S. Darwish bool running,
1351e90474cSPatrick McHardy struct nlattr *opt)
1361da177e4SLinus Torvalds {
1371e90474cSPatrick McHardy struct gnet_estimator *parm = nla_data(opt);
1381c0d32fdSEric Dumazet struct net_rate_estimator *old, *est;
13950dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync b;
1401c0d32fdSEric Dumazet int intvl_log;
1411da177e4SLinus Torvalds
1421e90474cSPatrick McHardy if (nla_len(opt) < sizeof(*parm))
1431da177e4SLinus Torvalds return -EINVAL;
1441da177e4SLinus Torvalds
1451c0d32fdSEric Dumazet /* allowed timer periods are :
1461c0d32fdSEric Dumazet * -2 : 250ms, -1 : 500ms, 0 : 1 sec
1471c0d32fdSEric Dumazet * 1 : 2 sec, 2 : 4 sec, 3 : 8 sec
1481c0d32fdSEric Dumazet */
1491da177e4SLinus Torvalds if (parm->interval < -2 || parm->interval > 3)
1501da177e4SLinus Torvalds return -EINVAL;
1511da177e4SLinus Torvalds
152dd5e0733SEric Dumazet if (parm->ewma_log == 0 || parm->ewma_log >= 31)
153dd5e0733SEric Dumazet return -EINVAL;
154dd5e0733SEric Dumazet
15577d04bd9SAndrew Morton est = kzalloc(sizeof(*est), GFP_KERNEL);
1561c0d32fdSEric Dumazet if (!est)
1571da177e4SLinus Torvalds return -ENOBUFS;
1581da177e4SLinus Torvalds
1591c0d32fdSEric Dumazet seqcount_init(&est->seq);
1601c0d32fdSEric Dumazet intvl_log = parm->interval + 2;
1611da177e4SLinus Torvalds est->bstats = bstats;
16251a9f5aeSVlad Buslov est->stats_lock = lock;
163edb09eb1SEric Dumazet est->running = running;
1641da177e4SLinus Torvalds est->ewma_log = parm->ewma_log;
1651c0d32fdSEric Dumazet est->intvl_log = intvl_log;
16622e0f8b9SJohn Fastabend est->cpu_bstats = cpu_bstats;
1671da177e4SLinus Torvalds
16851a9f5aeSVlad Buslov if (lock)
16940ca54e3SEric Dumazet local_bh_disable();
1701c0d32fdSEric Dumazet est_fetch_counters(est, &b);
17151a9f5aeSVlad Buslov if (lock)
17240ca54e3SEric Dumazet local_bh_enable();
17350dc9a85SAhmed S. Darwish est->last_bytes = u64_stats_read(&b.bytes);
17450dc9a85SAhmed S. Darwish est->last_packets = u64_stats_read(&b.packets);
17551a9f5aeSVlad Buslov
17651a9f5aeSVlad Buslov if (lock)
17751a9f5aeSVlad Buslov spin_lock_bh(lock);
1781c0d32fdSEric Dumazet old = rcu_dereference_protected(*rate_est, 1);
1791c0d32fdSEric Dumazet if (old) {
1801c0d32fdSEric Dumazet del_timer_sync(&old->timer);
1811c0d32fdSEric Dumazet est->avbps = old->avbps;
1821c0d32fdSEric Dumazet est->avpps = old->avpps;
1831da177e4SLinus Torvalds }
1840929c2ddSRanko Zivojnovic
1851c0d32fdSEric Dumazet est->next_jiffies = jiffies + ((HZ/4) << intvl_log);
186e99e88a9SKees Cook timer_setup(&est->timer, est_timer, 0);
1871c0d32fdSEric Dumazet mod_timer(&est->timer, est->next_jiffies);
1884db0acf3SJarek Poplawski
1891c0d32fdSEric Dumazet rcu_assign_pointer(*rate_est, est);
19051a9f5aeSVlad Buslov if (lock)
19151a9f5aeSVlad Buslov spin_unlock_bh(lock);
1921c0d32fdSEric Dumazet if (old)
1931c0d32fdSEric Dumazet kfree_rcu(old, rcu);
1941da177e4SLinus Torvalds return 0;
1951da177e4SLinus Torvalds }
196c1b56878SStephen Hemminger EXPORT_SYMBOL(gen_new_estimator);
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds /**
1991da177e4SLinus Torvalds * gen_kill_estimator - remove a rate estimator
2001c0d32fdSEric Dumazet * @rate_est: rate estimator
2011da177e4SLinus Torvalds *
2021c0d32fdSEric Dumazet * Removes the rate estimator.
2030929c2ddSRanko Zivojnovic *
2041da177e4SLinus Torvalds */
gen_kill_estimator(struct net_rate_estimator __rcu ** rate_est)2051c0d32fdSEric Dumazet void gen_kill_estimator(struct net_rate_estimator __rcu **rate_est)
2061da177e4SLinus Torvalds {
2071c0d32fdSEric Dumazet struct net_rate_estimator *est;
2081da177e4SLinus Torvalds
209*70530a2fSEric Dumazet est = unrcu_pointer(xchg(rate_est, NULL));
2101c0d32fdSEric Dumazet if (est) {
211292a089dSSteven Rostedt (Google) timer_shutdown_sync(&est->timer);
2121c0d32fdSEric Dumazet kfree_rcu(est, rcu);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds }
215c1b56878SStephen Hemminger EXPORT_SYMBOL(gen_kill_estimator);
2161da177e4SLinus Torvalds
2171da177e4SLinus Torvalds /**
21896750162SJarek Poplawski * gen_replace_estimator - replace rate estimator configuration
2191da177e4SLinus Torvalds * @bstats: basic statistics
220e9fc2f05SLuis de Bethencourt * @cpu_bstats: bstats per cpu
2211da177e4SLinus Torvalds * @rate_est: rate estimator statistics
22251a9f5aeSVlad Buslov * @lock: lock for statistics and control path
22329cbcd85SAhmed S. Darwish * @running: true if @bstats represents a running qdisc, thus @bstats'
22429cbcd85SAhmed S. Darwish * internal values might change during basic reads. Only used
22529cbcd85SAhmed S. Darwish * if @cpu_bstats is NULL
2261da177e4SLinus Torvalds * @opt: rate estimator configuration TLV
2271da177e4SLinus Torvalds *
2281da177e4SLinus Torvalds * Replaces the configuration of a rate estimator by calling
2291da177e4SLinus Torvalds * gen_kill_estimator() and gen_new_estimator().
2301da177e4SLinus Torvalds *
2311da177e4SLinus Torvalds * Returns 0 on success or a negative error code.
2321da177e4SLinus Torvalds */
gen_replace_estimator(struct gnet_stats_basic_sync * bstats,struct gnet_stats_basic_sync __percpu * cpu_bstats,struct net_rate_estimator __rcu ** rate_est,spinlock_t * lock,bool running,struct nlattr * opt)23350dc9a85SAhmed S. Darwish int gen_replace_estimator(struct gnet_stats_basic_sync *bstats,
23450dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu_bstats,
2351c0d32fdSEric Dumazet struct net_rate_estimator __rcu **rate_est,
23651a9f5aeSVlad Buslov spinlock_t *lock,
23729cbcd85SAhmed S. Darwish bool running, struct nlattr *opt)
2381da177e4SLinus Torvalds {
2391c0d32fdSEric Dumazet return gen_new_estimator(bstats, cpu_bstats, rate_est,
24051a9f5aeSVlad Buslov lock, running, opt);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds EXPORT_SYMBOL(gen_replace_estimator);
243c1b56878SStephen Hemminger
244c1b56878SStephen Hemminger /**
245c1b56878SStephen Hemminger * gen_estimator_active - test if estimator is currently in use
2461c0d32fdSEric Dumazet * @rate_est: rate estimator
247c1b56878SStephen Hemminger *
248244e6c2dSJarek Poplawski * Returns true if estimator is active, and false if not.
249c1b56878SStephen Hemminger */
gen_estimator_active(struct net_rate_estimator __rcu ** rate_est)2501c0d32fdSEric Dumazet bool gen_estimator_active(struct net_rate_estimator __rcu **rate_est)
251c1b56878SStephen Hemminger {
2521c0d32fdSEric Dumazet return !!rcu_access_pointer(*rate_est);
253c1b56878SStephen Hemminger }
254c1b56878SStephen Hemminger EXPORT_SYMBOL(gen_estimator_active);
2551c0d32fdSEric Dumazet
gen_estimator_read(struct net_rate_estimator __rcu ** rate_est,struct gnet_stats_rate_est64 * sample)2561c0d32fdSEric Dumazet bool gen_estimator_read(struct net_rate_estimator __rcu **rate_est,
2571c0d32fdSEric Dumazet struct gnet_stats_rate_est64 *sample)
2581c0d32fdSEric Dumazet {
2591c0d32fdSEric Dumazet struct net_rate_estimator *est;
2601c0d32fdSEric Dumazet unsigned seq;
2611c0d32fdSEric Dumazet
2621c0d32fdSEric Dumazet rcu_read_lock();
2631c0d32fdSEric Dumazet est = rcu_dereference(*rate_est);
2641c0d32fdSEric Dumazet if (!est) {
2651c0d32fdSEric Dumazet rcu_read_unlock();
2661c0d32fdSEric Dumazet return false;
2671c0d32fdSEric Dumazet }
2681c0d32fdSEric Dumazet
2691c0d32fdSEric Dumazet do {
2701c0d32fdSEric Dumazet seq = read_seqcount_begin(&est->seq);
2711c0d32fdSEric Dumazet sample->bps = est->avbps >> 8;
2721c0d32fdSEric Dumazet sample->pps = est->avpps >> 8;
2731c0d32fdSEric Dumazet } while (read_seqcount_retry(&est->seq, seq));
2741c0d32fdSEric Dumazet
2751c0d32fdSEric Dumazet rcu_read_unlock();
2761c0d32fdSEric Dumazet return true;
2771c0d32fdSEric Dumazet }
2781c0d32fdSEric Dumazet EXPORT_SYMBOL(gen_estimator_read);
279