2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* net/sched/gen_estimator.c Simple rate estimator.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
* Jamal Hadi Salim - moved it to net/core and reshulfed
|
|
|
|
* names to make it usable in general net subsystem.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
2007-10-19 06:40:25 +00:00
|
|
|
#include <linux/bitops.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <linux/init.h>
|
2008-11-24 23:48:05 +00:00
|
|
|
#include <linux/rbtree.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/gen_stats.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
This code is NOT intended to be used for statistics collection,
|
|
|
|
its purpose is to provide a base for statistical multiplexing
|
|
|
|
for controlled load service.
|
|
|
|
If you need only statistics, run a user level daemon which
|
|
|
|
periodically reads byte counters.
|
|
|
|
|
|
|
|
Unfortunately, rate estimation is not a very easy task.
|
|
|
|
F.e. I did not find a simple way to estimate the current peak rate
|
|
|
|
and even failed to formulate the problem 8)8)
|
|
|
|
|
|
|
|
So I preferred not to built an estimator into the scheduler,
|
|
|
|
but run this task separately.
|
|
|
|
Ideally, it should be kernel thread(s), but for now it runs
|
|
|
|
from timers, which puts apparent top bounds on the number of rated
|
|
|
|
flows, has minimal overhead on small, but is enough
|
|
|
|
to handle controlled load service, sets of aggregates.
|
|
|
|
|
|
|
|
We measure rate over A=(1<<interval) seconds and evaluate EWMA:
|
|
|
|
|
|
|
|
avrate = avrate*(1-W) + rate*W
|
|
|
|
|
|
|
|
where W is chosen as negative power of 2: W = 2^(-ewma_log)
|
|
|
|
|
|
|
|
The resulting time constant is:
|
|
|
|
|
|
|
|
T = A/(-ln(1-W))
|
|
|
|
|
|
|
|
|
|
|
|
NOTES.
|
|
|
|
|
2009-05-19 02:26:37 +00:00
|
|
|
* avbps is scaled by 2^5, avpps is scaled by 2^10.
|
|
|
|
* both values are reported as 32 bit unsigned values. bps can
|
|
|
|
overflow for fast links : max speed being 34360Mbit/sec
|
2005-04-16 22:20:36 +00:00
|
|
|
* Minimal interval is HZ/4=250msec (it is the greatest common divisor
|
|
|
|
for HZ=100 and HZ=1024 8)), maximal interval
|
|
|
|
is (HZ*2^EST_MAX_INTERVAL)/4 = 8sec. Shorter intervals
|
|
|
|
are too expensive, longer ones can be implemented
|
|
|
|
at user level painlessly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define EST_MAX_INTERVAL 5
|
|
|
|
|
|
|
|
struct gen_estimator
|
|
|
|
{
|
2007-07-17 01:28:32 +00:00
|
|
|
struct list_head list;
|
2009-08-16 09:36:49 +00:00
|
|
|
struct gnet_stats_basic_packed *bstats;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct gnet_stats_rate_est *rate_est;
|
|
|
|
spinlock_t *stats_lock;
|
|
|
|
int ewma_log;
|
|
|
|
u64 last_bytes;
|
2009-05-19 02:26:37 +00:00
|
|
|
u64 avbps;
|
2005-04-16 22:20:36 +00:00
|
|
|
u32 last_packets;
|
|
|
|
u32 avpps;
|
2007-07-17 01:28:32 +00:00
|
|
|
struct rcu_head e_rcu;
|
2008-11-24 23:48:05 +00:00
|
|
|
struct rb_node node;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gen_estimator_head
|
|
|
|
{
|
|
|
|
struct timer_list timer;
|
2007-07-17 01:28:32 +00:00
|
|
|
struct list_head list;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct gen_estimator_head elist[EST_MAX_INTERVAL+1];
|
|
|
|
|
2008-08-19 05:32:10 +00:00
|
|
|
/* Protects against NULL dereference */
|
2005-04-16 22:20:36 +00:00
|
|
|
static DEFINE_RWLOCK(est_lock);
|
|
|
|
|
2008-11-24 23:48:05 +00:00
|
|
|
/* Protects against soft lockup during large deletion */
|
|
|
|
static struct rb_root est_root = RB_ROOT;
|
2010-06-08 23:39:10 +00:00
|
|
|
static DEFINE_SPINLOCK(est_tree_lock);
|
2008-11-24 23:48:05 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void est_timer(unsigned long arg)
|
|
|
|
{
|
|
|
|
int idx = (int)arg;
|
|
|
|
struct gen_estimator *e;
|
|
|
|
|
2007-07-17 01:28:32 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(e, &elist[idx].list, list) {
|
2005-04-16 22:20:36 +00:00
|
|
|
u64 nbytes;
|
2009-05-19 02:26:37 +00:00
|
|
|
u64 brate;
|
2005-04-16 22:20:36 +00:00
|
|
|
u32 npackets;
|
|
|
|
u32 rate;
|
|
|
|
|
|
|
|
spin_lock(e->stats_lock);
|
2007-07-17 01:28:32 +00:00
|
|
|
read_lock(&est_lock);
|
|
|
|
if (e->bstats == NULL)
|
|
|
|
goto skip;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
nbytes = e->bstats->bytes;
|
|
|
|
npackets = e->bstats->packets;
|
2009-05-19 02:26:37 +00:00
|
|
|
brate = (nbytes - e->last_bytes)<<(7 - idx);
|
2005-04-16 22:20:36 +00:00
|
|
|
e->last_bytes = nbytes;
|
2009-05-26 05:47:01 +00:00
|
|
|
e->avbps += (brate >> e->ewma_log) - (e->avbps >> e->ewma_log);
|
2005-04-16 22:20:36 +00:00
|
|
|
e->rate_est->bps = (e->avbps+0xF)>>5;
|
|
|
|
|
|
|
|
rate = (npackets - e->last_packets)<<(12 - idx);
|
|
|
|
e->last_packets = npackets;
|
2009-05-26 05:47:01 +00:00
|
|
|
e->avpps += (rate >> e->ewma_log) - (e->avpps >> e->ewma_log);
|
2005-04-16 22:20:36 +00:00
|
|
|
e->rate_est->pps = (e->avpps+0x1FF)>>10;
|
2007-07-17 01:28:32 +00:00
|
|
|
skip:
|
|
|
|
read_unlock(&est_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
spin_unlock(e->stats_lock);
|
|
|
|
}
|
|
|
|
|
2007-07-17 01:28:32 +00:00
|
|
|
if (!list_empty(&elist[idx].list))
|
2008-01-04 04:40:01 +00:00
|
|
|
mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
|
2007-07-17 01:28:32 +00:00
|
|
|
rcu_read_unlock();
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-11-24 23:48:05 +00:00
|
|
|
static void gen_add_node(struct gen_estimator *est)
|
|
|
|
{
|
|
|
|
struct rb_node **p = &est_root.rb_node, *parent = NULL;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
struct gen_estimator *e;
|
|
|
|
|
|
|
|
parent = *p;
|
|
|
|
e = rb_entry(parent, struct gen_estimator, node);
|
|
|
|
|
|
|
|
if (est->bstats > e->bstats)
|
|
|
|
p = &parent->rb_right;
|
|
|
|
else
|
|
|
|
p = &parent->rb_left;
|
|
|
|
}
|
|
|
|
rb_link_node(&est->node, parent, p);
|
|
|
|
rb_insert_color(&est->node, &est_root);
|
|
|
|
}
|
|
|
|
|
2008-11-26 23:24:32 +00:00
|
|
|
static
|
2009-08-16 09:36:49 +00:00
|
|
|
struct gen_estimator *gen_find_node(const struct gnet_stats_basic_packed *bstats,
|
2008-11-26 23:24:32 +00:00
|
|
|
const struct gnet_stats_rate_est *rate_est)
|
2008-11-24 23:48:05 +00:00
|
|
|
{
|
|
|
|
struct rb_node *p = est_root.rb_node;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
struct gen_estimator *e;
|
|
|
|
|
|
|
|
e = rb_entry(p, struct gen_estimator, node);
|
|
|
|
|
|
|
|
if (bstats > e->bstats)
|
|
|
|
p = p->rb_right;
|
|
|
|
else if (bstats < e->bstats || rate_est != e->rate_est)
|
|
|
|
p = p->rb_left;
|
|
|
|
else
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* gen_new_estimator - create a new rate estimator
|
|
|
|
* @bstats: basic statistics
|
|
|
|
* @rate_est: rate estimator statistics
|
|
|
|
* @stats_lock: statistics lock
|
|
|
|
* @opt: rate estimator configuration TLV
|
|
|
|
*
|
|
|
|
* Creates a new rate estimator with &bstats as source and &rate_est
|
|
|
|
* as destination. A new timer with the interval specified in the
|
|
|
|
* configuration TLV is created. Upon each interval, the latest statistics
|
|
|
|
* will be read from &bstats and the estimated rate will be stored in
|
|
|
|
* &rate_est with the statistics lock grabed during this period.
|
2007-02-09 14:24:36 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* Returns 0 on success or a negative error code.
|
2007-07-17 01:28:32 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2009-08-16 09:36:49 +00:00
|
|
|
int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
|
2007-07-17 01:28:32 +00:00
|
|
|
struct gnet_stats_rate_est *rate_est,
|
|
|
|
spinlock_t *stats_lock,
|
2008-01-23 06:11:17 +00:00
|
|
|
struct nlattr *opt)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct gen_estimator *est;
|
2008-01-23 06:11:17 +00:00
|
|
|
struct gnet_estimator *parm = nla_data(opt);
|
2007-07-17 01:28:32 +00:00
|
|
|
int idx;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-23 06:11:17 +00:00
|
|
|
if (nla_len(opt) < sizeof(*parm))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (parm->interval < -2 || parm->interval > 3)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2006-04-07 21:52:59 +00:00
|
|
|
est = kzalloc(sizeof(*est), GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (est == NULL)
|
|
|
|
return -ENOBUFS;
|
|
|
|
|
2007-07-17 01:28:32 +00:00
|
|
|
idx = parm->interval + 2;
|
2005-04-16 22:20:36 +00:00
|
|
|
est->bstats = bstats;
|
|
|
|
est->rate_est = rate_est;
|
|
|
|
est->stats_lock = stats_lock;
|
|
|
|
est->ewma_log = parm->ewma_log;
|
|
|
|
est->last_bytes = bstats->bytes;
|
|
|
|
est->avbps = rate_est->bps<<5;
|
|
|
|
est->last_packets = bstats->packets;
|
|
|
|
est->avpps = rate_est->pps<<10;
|
|
|
|
|
2010-09-02 20:22:11 +00:00
|
|
|
spin_lock_bh(&est_tree_lock);
|
2007-07-17 01:28:32 +00:00
|
|
|
if (!elist[idx].timer.function) {
|
|
|
|
INIT_LIST_HEAD(&elist[idx].list);
|
|
|
|
setup_timer(&elist[idx].timer, est_timer, idx);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-07-17 01:28:32 +00:00
|
|
|
|
|
|
|
if (list_empty(&elist[idx].list))
|
2008-01-04 04:40:01 +00:00
|
|
|
mod_timer(&elist[idx].timer, jiffies + ((HZ/4) << idx));
|
2007-07-17 01:28:32 +00:00
|
|
|
|
|
|
|
list_add_rcu(&est->list, &elist[idx].list);
|
2008-11-24 23:48:05 +00:00
|
|
|
gen_add_node(est);
|
2010-09-02 20:22:11 +00:00
|
|
|
spin_unlock_bh(&est_tree_lock);
|
2008-11-24 23:48:05 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-11-26 05:14:06 +00:00
|
|
|
EXPORT_SYMBOL(gen_new_estimator);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_kill_estimator - remove a rate estimator
|
|
|
|
* @bstats: basic statistics
|
|
|
|
* @rate_est: rate estimator statistics
|
|
|
|
*
|
2008-11-24 23:48:05 +00:00
|
|
|
* Removes the rate estimator specified by &bstats and &rate_est.
|
2007-07-17 01:28:32 +00:00
|
|
|
*
|
2010-06-09 02:09:23 +00:00
|
|
|
* Note : Caller should respect an RCU grace period before freeing stats_lock
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2009-08-16 09:36:49 +00:00
|
|
|
void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
|
2008-11-24 23:48:05 +00:00
|
|
|
struct gnet_stats_rate_est *rate_est)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-11-24 23:48:05 +00:00
|
|
|
struct gen_estimator *e;
|
2007-07-17 01:28:32 +00:00
|
|
|
|
2010-09-02 20:22:11 +00:00
|
|
|
spin_lock_bh(&est_tree_lock);
|
2008-11-24 23:48:05 +00:00
|
|
|
while ((e = gen_find_node(bstats, rate_est))) {
|
|
|
|
rb_erase(&e->node, &est_root);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-09-08 09:16:28 +00:00
|
|
|
write_lock(&est_lock);
|
2008-11-24 23:48:05 +00:00
|
|
|
e->bstats = NULL;
|
2010-09-08 09:16:28 +00:00
|
|
|
write_unlock(&est_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-11-24 23:48:05 +00:00
|
|
|
list_del_rcu(&e->list);
|
2011-03-18 03:43:26 +00:00
|
|
|
kfree_rcu(e, e_rcu);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-09-02 20:22:11 +00:00
|
|
|
spin_unlock_bh(&est_tree_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-11-26 05:14:06 +00:00
|
|
|
EXPORT_SYMBOL(gen_kill_estimator);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2008-01-21 10:36:02 +00:00
|
|
|
* gen_replace_estimator - replace rate estimator configuration
|
2005-04-16 22:20:36 +00:00
|
|
|
* @bstats: basic statistics
|
|
|
|
* @rate_est: rate estimator statistics
|
|
|
|
* @stats_lock: statistics lock
|
|
|
|
* @opt: rate estimator configuration TLV
|
|
|
|
*
|
|
|
|
* Replaces the configuration of a rate estimator by calling
|
|
|
|
* gen_kill_estimator() and gen_new_estimator().
|
2007-02-09 14:24:36 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* Returns 0 on success or a negative error code.
|
|
|
|
*/
|
2009-08-16 09:36:49 +00:00
|
|
|
int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
|
2008-01-21 10:36:02 +00:00
|
|
|
struct gnet_stats_rate_est *rate_est,
|
2008-01-23 06:11:17 +00:00
|
|
|
spinlock_t *stats_lock, struct nlattr *opt)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-21 10:36:02 +00:00
|
|
|
gen_kill_estimator(bstats, rate_est);
|
|
|
|
return gen_new_estimator(bstats, rate_est, stats_lock, opt);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-11-26 05:14:06 +00:00
|
|
|
EXPORT_SYMBOL(gen_replace_estimator);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_estimator_active - test if estimator is currently in use
|
2008-11-26 23:24:32 +00:00
|
|
|
* @bstats: basic statistics
|
2008-11-26 05:14:06 +00:00
|
|
|
* @rate_est: rate estimator statistics
|
|
|
|
*
|
2008-11-26 23:24:32 +00:00
|
|
|
* Returns true if estimator is active, and false if not.
|
2008-11-26 05:14:06 +00:00
|
|
|
*/
|
2009-08-16 09:36:49 +00:00
|
|
|
bool gen_estimator_active(const struct gnet_stats_basic_packed *bstats,
|
2008-11-26 23:24:32 +00:00
|
|
|
const struct gnet_stats_rate_est *rate_est)
|
2008-11-26 05:14:06 +00:00
|
|
|
{
|
2010-06-08 23:39:10 +00:00
|
|
|
bool res;
|
|
|
|
|
2008-11-26 05:14:06 +00:00
|
|
|
ASSERT_RTNL();
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-09-02 20:22:11 +00:00
|
|
|
spin_lock_bh(&est_tree_lock);
|
2010-06-08 23:39:10 +00:00
|
|
|
res = gen_find_node(bstats, rate_est) != NULL;
|
2010-09-02 20:22:11 +00:00
|
|
|
spin_unlock_bh(&est_tree_lock);
|
2010-06-08 23:39:10 +00:00
|
|
|
|
|
|
|
return res;
|
2008-11-26 05:14:06 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_estimator_active);
|