2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* ip_vs_est.c: simple rate estimator for IPVS
|
|
|
|
*
|
|
|
|
* Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2011-01-03 14:44:54 +01:00
|
|
|
* Changes: Hans Schillstrom <hans.schillstrom@ericsson.com>
|
|
|
|
* Network name space (netns) aware.
|
|
|
|
* Global data moved to netns i.e struct netns_ipvs
|
|
|
|
* Affected data: est_list and est_lock.
|
|
|
|
* estimation_timer() runs with timer per netns.
|
|
|
|
* get_stats()) do the per cpu summing.
|
2005-04-17 00:20:36 +02:00
|
|
|
*/
|
2009-07-30 23:29:44 +02:00
|
|
|
|
|
|
|
#define KMSG_COMPONENT "IPVS"
|
|
|
|
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/kernel.h>
|
2005-12-27 05:43:12 +01:00
|
|
|
#include <linux/jiffies.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/types.h>
|
2006-01-05 21:14:43 +01:00
|
|
|
#include <linux/interrupt.h>
|
2008-01-12 11:33:50 +01:00
|
|
|
#include <linux/sysctl.h>
|
2008-08-10 20:24:41 +02:00
|
|
|
#include <linux/list.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
#include <net/ip_vs.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
This code is to estimate rate in a shorter interval (such as 8
|
|
|
|
seconds) for virtual services and real servers. For measure rate in a
|
|
|
|
long interval, it is easy to implement a user level daemon which
|
|
|
|
periodically reads those statistical counters and measure rate.
|
|
|
|
|
|
|
|
Currently, the measurement is activated by slow timer handler. Hope
|
|
|
|
this measurement will not introduce too much load.
|
|
|
|
|
|
|
|
We measure rate during the last 8 seconds every 2 seconds:
|
|
|
|
|
|
|
|
avgrate = avgrate*(1-W) + rate*W
|
|
|
|
|
|
|
|
where W = 2^(-2)
|
|
|
|
|
|
|
|
NOTES.
|
|
|
|
|
|
|
|
* The stored value for average bps is scaled by 2^5, so that maximal
|
|
|
|
rate is ~2.15Gbits/s, average pps and cps are scaled by 2^10.
|
|
|
|
|
|
|
|
* A lot code is taken from net/sched/estimator.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-01-03 14:44:56 +01:00
|
|
|
/*
|
|
|
|
* Make a summary from each cpu
|
|
|
|
*/
|
|
|
|
static void ip_vs_read_cpu_stats(struct ip_vs_stats_user *sum,
|
|
|
|
struct ip_vs_cpu_stats *stats)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_possible_cpu(i) {
|
|
|
|
struct ip_vs_cpu_stats *s = per_cpu_ptr(stats, i);
|
|
|
|
unsigned int start;
|
|
|
|
__u64 inbytes, outbytes;
|
|
|
|
if (i) {
|
|
|
|
sum->conns += s->ustats.conns;
|
|
|
|
sum->inpkts += s->ustats.inpkts;
|
|
|
|
sum->outpkts += s->ustats.outpkts;
|
|
|
|
do {
|
2011-03-04 11:28:20 +01:00
|
|
|
start = u64_stats_fetch_begin(&s->syncp);
|
2011-01-03 14:44:56 +01:00
|
|
|
inbytes = s->ustats.inbytes;
|
|
|
|
outbytes = s->ustats.outbytes;
|
2011-03-04 11:28:20 +01:00
|
|
|
} while (u64_stats_fetch_retry(&s->syncp, start));
|
2011-01-03 14:44:56 +01:00
|
|
|
sum->inbytes += inbytes;
|
|
|
|
sum->outbytes += outbytes;
|
|
|
|
} else {
|
|
|
|
sum->conns = s->ustats.conns;
|
|
|
|
sum->inpkts = s->ustats.inpkts;
|
|
|
|
sum->outpkts = s->ustats.outpkts;
|
|
|
|
do {
|
2011-03-04 11:28:20 +01:00
|
|
|
start = u64_stats_fetch_begin(&s->syncp);
|
2011-01-03 14:44:56 +01:00
|
|
|
sum->inbytes = s->ustats.inbytes;
|
|
|
|
sum->outbytes = s->ustats.outbytes;
|
2011-03-04 11:28:20 +01:00
|
|
|
} while (u64_stats_fetch_retry(&s->syncp, start));
|
2011-01-03 14:44:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static void estimation_timer(unsigned long arg)
|
|
|
|
{
|
|
|
|
struct ip_vs_estimator *e;
|
|
|
|
struct ip_vs_stats *s;
|
|
|
|
u32 n_conns;
|
|
|
|
u32 n_inpkts, n_outpkts;
|
|
|
|
u64 n_inbytes, n_outbytes;
|
|
|
|
u32 rate;
|
2011-01-03 14:44:54 +01:00
|
|
|
struct net *net = (struct net *)arg;
|
|
|
|
struct netns_ipvs *ipvs;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
ipvs = net_ipvs(net);
|
|
|
|
spin_lock(&ipvs->est_lock);
|
|
|
|
list_for_each_entry(e, &ipvs->est_list, list) {
|
2008-08-10 20:24:41 +02:00
|
|
|
s = container_of(e, struct ip_vs_stats, est);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
spin_lock(&s->lock);
|
2011-03-04 11:20:35 +01:00
|
|
|
ip_vs_read_cpu_stats(&s->ustats, s->cpustats);
|
2008-09-08 13:39:04 +02:00
|
|
|
n_conns = s->ustats.conns;
|
|
|
|
n_inpkts = s->ustats.inpkts;
|
|
|
|
n_outpkts = s->ustats.outpkts;
|
|
|
|
n_inbytes = s->ustats.inbytes;
|
|
|
|
n_outbytes = s->ustats.outbytes;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* scaled by 2^10, but divided 2 seconds */
|
2011-01-03 14:44:54 +01:00
|
|
|
rate = (n_conns - e->last_conns) << 9;
|
2005-04-17 00:20:36 +02:00
|
|
|
e->last_conns = n_conns;
|
2011-01-03 14:44:54 +01:00
|
|
|
e->cps += ((long)rate - (long)e->cps) >> 2;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
rate = (n_inpkts - e->last_inpkts) << 9;
|
2005-04-17 00:20:36 +02:00
|
|
|
e->last_inpkts = n_inpkts;
|
2011-01-03 14:44:54 +01:00
|
|
|
e->inpps += ((long)rate - (long)e->inpps) >> 2;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
rate = (n_outpkts - e->last_outpkts) << 9;
|
2005-04-17 00:20:36 +02:00
|
|
|
e->last_outpkts = n_outpkts;
|
2011-01-03 14:44:54 +01:00
|
|
|
e->outpps += ((long)rate - (long)e->outpps) >> 2;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
rate = (n_inbytes - e->last_inbytes) << 4;
|
2005-04-17 00:20:36 +02:00
|
|
|
e->last_inbytes = n_inbytes;
|
2011-01-03 14:44:54 +01:00
|
|
|
e->inbps += ((long)rate - (long)e->inbps) >> 2;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
rate = (n_outbytes - e->last_outbytes) << 4;
|
2005-04-17 00:20:36 +02:00
|
|
|
e->last_outbytes = n_outbytes;
|
2011-01-03 14:44:54 +01:00
|
|
|
e->outbps += ((long)rate - (long)e->outbps) >> 2;
|
2005-04-17 00:20:36 +02:00
|
|
|
spin_unlock(&s->lock);
|
|
|
|
}
|
2011-01-03 14:44:54 +01:00
|
|
|
spin_unlock(&ipvs->est_lock);
|
|
|
|
mod_timer(&ipvs->est_timer, jiffies + 2*HZ);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2011-03-14 00:44:28 +01:00
|
|
|
void ip_vs_start_estimator(struct net *net, struct ip_vs_stats *stats)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2011-01-03 14:44:54 +01:00
|
|
|
struct netns_ipvs *ipvs = net_ipvs(net);
|
2008-08-10 20:24:41 +02:00
|
|
|
struct ip_vs_estimator *est = &stats->est;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-08-10 20:24:41 +02:00
|
|
|
INIT_LIST_HEAD(&est->list);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
spin_lock_bh(&ipvs->est_lock);
|
|
|
|
list_add(&est->list, &ipvs->est_list);
|
|
|
|
spin_unlock_bh(&ipvs->est_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2011-03-14 00:44:28 +01:00
|
|
|
void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2011-01-03 14:44:54 +01:00
|
|
|
struct netns_ipvs *ipvs = net_ipvs(net);
|
2008-08-10 20:24:41 +02:00
|
|
|
struct ip_vs_estimator *est = &stats->est;
|
|
|
|
|
2011-01-03 14:44:54 +01:00
|
|
|
spin_lock_bh(&ipvs->est_lock);
|
2008-08-10 20:24:41 +02:00
|
|
|
list_del(&est->list);
|
2011-01-03 14:44:54 +01:00
|
|
|
spin_unlock_bh(&ipvs->est_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ip_vs_zero_estimator(struct ip_vs_stats *stats)
|
|
|
|
{
|
2008-08-10 20:24:41 +02:00
|
|
|
struct ip_vs_estimator *est = &stats->est;
|
2011-03-14 00:37:49 +01:00
|
|
|
struct ip_vs_stats_user *u = &stats->ustats;
|
|
|
|
|
|
|
|
/* reset counters, caller must hold the stats->lock lock */
|
|
|
|
est->last_inbytes = u->inbytes;
|
|
|
|
est->last_outbytes = u->outbytes;
|
|
|
|
est->last_conns = u->conns;
|
|
|
|
est->last_inpkts = u->inpkts;
|
|
|
|
est->last_outpkts = u->outpkts;
|
2008-08-10 20:24:41 +02:00
|
|
|
est->cps = 0;
|
|
|
|
est->inpps = 0;
|
|
|
|
est->outpps = 0;
|
|
|
|
est->inbps = 0;
|
|
|
|
est->outbps = 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2008-08-14 00:47:16 +02:00
|
|
|
|
2011-03-14 00:41:54 +01:00
|
|
|
/* Get decoded rates */
|
|
|
|
void ip_vs_read_estimator(struct ip_vs_stats_user *dst,
|
|
|
|
struct ip_vs_stats *stats)
|
|
|
|
{
|
|
|
|
struct ip_vs_estimator *e = &stats->est;
|
|
|
|
|
|
|
|
dst->cps = (e->cps + 0x1FF) >> 10;
|
|
|
|
dst->inpps = (e->inpps + 0x1FF) >> 10;
|
|
|
|
dst->outpps = (e->outpps + 0x1FF) >> 10;
|
|
|
|
dst->inbps = (e->inbps + 0xF) >> 5;
|
|
|
|
dst->outbps = (e->outbps + 0xF) >> 5;
|
|
|
|
}
|
|
|
|
|
2011-05-01 18:50:16 +02:00
|
|
|
int __net_init ip_vs_estimator_net_init(struct net *net)
|
2011-01-03 14:44:42 +01:00
|
|
|
{
|
2011-01-03 14:44:54 +01:00
|
|
|
struct netns_ipvs *ipvs = net_ipvs(net);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&ipvs->est_list);
|
|
|
|
spin_lock_init(&ipvs->est_lock);
|
|
|
|
setup_timer(&ipvs->est_timer, estimation_timer, (unsigned long)net);
|
|
|
|
mod_timer(&ipvs->est_timer, jiffies + 2 * HZ);
|
2011-01-03 14:44:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-01 18:50:16 +02:00
|
|
|
void __net_exit ip_vs_estimator_net_cleanup(struct net *net)
|
2011-01-03 14:44:54 +01:00
|
|
|
{
|
|
|
|
del_timer_sync(&net_ipvs(net)->est_timer);
|
|
|
|
}
|