Skip to content

Commit 55a3d4e

Browse files
Julian Anastasovhorms
authored andcommitted
ipvs: properly zero stats and rates
Currently, the new percpu counters are not zeroed and the zero commands do not work as expected, we still show the old sum of percpu values. OTOH, we can not reset the percpu counters from user context without causing the incrementing to use old and bogus values. So, as Eric Dumazet suggested fix that by moving all overhead to stats reading in user context. Do not introduce overhead in timer context (estimator) and incrementing (packet handling in softirqs). The new ustats0 field holds the zero point for all counter values, the rates always use 0 as base value as before. When showing the values to user space just give the difference between counters and the base values. The only drawback is that percpu stats are not zeroed, they are accessible only from /proc and are new interface, so it should not be a compatibility problem as long as the sum stats are correct after zeroing. Signed-off-by: Julian Anastasov <ja@ssi.bg> Acked-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: Simon Horman <horms@verge.net.au>
1 parent 2a0751a commit 55a3d4e

3 files changed

Lines changed: 69 additions & 43 deletions

File tree

‎include/net/ip_vs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ struct ip_vs_stats {
374374
struct ip_vs_estimator est; /* estimator */
375375
struct ip_vs_cpu_stats *cpustats; /* per cpu counters */
376376
spinlock_t lock; /* spin lock */
377+
struct ip_vs_stats_user ustats0; /* reset values */
377378
};
378379

379380
/*

‎net/netfilter/ipvs/ip_vs_ctl.c‎

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,51 @@ static void ip_vs_trash_cleanup(struct net *net)
711711
}
712712
}
713713

714+
static void
715+
ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
716+
{
717+
#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.c
718+
#define IP_VS_SHOW_STATS_RATE(r) dst->r = src->ustats.r
719+
720+
spin_lock_bh(&src->lock);
721+
722+
IP_VS_SHOW_STATS_COUNTER(conns);
723+
IP_VS_SHOW_STATS_COUNTER(inpkts);
724+
IP_VS_SHOW_STATS_COUNTER(outpkts);
725+
IP_VS_SHOW_STATS_COUNTER(inbytes);
726+
IP_VS_SHOW_STATS_COUNTER(outbytes);
727+
728+
IP_VS_SHOW_STATS_RATE(cps);
729+
IP_VS_SHOW_STATS_RATE(inpps);
730+
IP_VS_SHOW_STATS_RATE(outpps);
731+
IP_VS_SHOW_STATS_RATE(inbps);
732+
IP_VS_SHOW_STATS_RATE(outbps);
733+
734+
spin_unlock_bh(&src->lock);
735+
}
714736

715737
static void
716738
ip_vs_zero_stats(struct ip_vs_stats *stats)
717739
{
718740
spin_lock_bh(&stats->lock);
719741

720-
memset(&stats->ustats, 0, sizeof(stats->ustats));
742+
/* get current counters as zero point, rates are zeroed */
743+
744+
#define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c
745+
#define IP_VS_ZERO_STATS_RATE(r) stats->ustats.r = 0
746+
747+
IP_VS_ZERO_STATS_COUNTER(conns);
748+
IP_VS_ZERO_STATS_COUNTER(inpkts);
749+
IP_VS_ZERO_STATS_COUNTER(outpkts);
750+
IP_VS_ZERO_STATS_COUNTER(inbytes);
751+
IP_VS_ZERO_STATS_COUNTER(outbytes);
752+
753+
IP_VS_ZERO_STATS_RATE(cps);
754+
IP_VS_ZERO_STATS_RATE(inpps);
755+
IP_VS_ZERO_STATS_RATE(outpps);
756+
IP_VS_ZERO_STATS_RATE(inbps);
757+
IP_VS_ZERO_STATS_RATE(outbps);
758+
721759
ip_vs_zero_estimator(stats);
722760

723761
spin_unlock_bh(&stats->lock);
@@ -1963,30 +2001,26 @@ static const struct file_operations ip_vs_info_fops = {
19632001
static int ip_vs_stats_show(struct seq_file *seq, void *v)
19642002
{
19652003
struct net *net = seq_file_single_net(seq);
1966-
struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2004+
struct ip_vs_stats_user show;
19672005

19682006
/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
19692007
seq_puts(seq,
19702008
" Total Incoming Outgoing Incoming Outgoing\n");
19712009
seq_printf(seq,
19722010
" Conns Packets Packets Bytes Bytes\n");
19732011

1974-
spin_lock_bh(&tot_stats->lock);
1975-
seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", tot_stats->ustats.conns,
1976-
tot_stats->ustats.inpkts, tot_stats->ustats.outpkts,
1977-
(unsigned long long) tot_stats->ustats.inbytes,
1978-
(unsigned long long) tot_stats->ustats.outbytes);
2012+
ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2013+
seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns,
2014+
show.inpkts, show.outpkts,
2015+
(unsigned long long) show.inbytes,
2016+
(unsigned long long) show.outbytes);
19792017

19802018
/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
19812019
seq_puts(seq,
19822020
" Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
1983-
seq_printf(seq,"%8X %8X %8X %16X %16X\n",
1984-
tot_stats->ustats.cps,
1985-
tot_stats->ustats.inpps,
1986-
tot_stats->ustats.outpps,
1987-
tot_stats->ustats.inbps,
1988-
tot_stats->ustats.outbps);
1989-
spin_unlock_bh(&tot_stats->lock);
2021+
seq_printf(seq, "%8X %8X %8X %16X %16X\n",
2022+
show.cps, show.inpps, show.outpps,
2023+
show.inbps, show.outbps);
19902024

19912025
return 0;
19922026
}
@@ -2297,14 +2331,6 @@ do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
22972331
}
22982332

22992333

2300-
static void
2301-
ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
2302-
{
2303-
spin_lock_bh(&src->lock);
2304-
memcpy(dst, &src->ustats, sizeof(*dst));
2305-
spin_unlock_bh(&src->lock);
2306-
}
2307-
23082334
static void
23092335
ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
23102336
{
@@ -2691,31 +2717,29 @@ static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
26912717
static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
26922718
struct ip_vs_stats *stats)
26932719
{
2720+
struct ip_vs_stats_user ustats;
26942721
struct nlattr *nl_stats = nla_nest_start(skb, container_type);
26952722
if (!nl_stats)
26962723
return -EMSGSIZE;
26972724

2698-
spin_lock_bh(&stats->lock);
2699-
2700-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, stats->ustats.conns);
2701-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, stats->ustats.inpkts);
2702-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, stats->ustats.outpkts);
2703-
NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, stats->ustats.inbytes);
2704-
NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, stats->ustats.outbytes);
2705-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, stats->ustats.cps);
2706-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, stats->ustats.inpps);
2707-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, stats->ustats.outpps);
2708-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, stats->ustats.inbps);
2709-
NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, stats->ustats.outbps);
2725+
ip_vs_copy_stats(&ustats, stats);
27102726

2711-
spin_unlock_bh(&stats->lock);
2727+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns);
2728+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts);
2729+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts);
2730+
NLA_PUT_U64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes);
2731+
NLA_PUT_U64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes);
2732+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_CPS, ustats.cps);
2733+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps);
2734+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps);
2735+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps);
2736+
NLA_PUT_U32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps);
27122737

27132738
nla_nest_end(skb, nl_stats);
27142739

27152740
return 0;
27162741

27172742
nla_put_failure:
2718-
spin_unlock_bh(&stats->lock);
27192743
nla_nest_cancel(skb, nl_stats);
27202744
return -EMSGSIZE;
27212745
}

‎net/netfilter/ipvs/ip_vs_est.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ void ip_vs_kill_estimator(struct net *net, struct ip_vs_stats *stats)
184184
void ip_vs_zero_estimator(struct ip_vs_stats *stats)
185185
{
186186
struct ip_vs_estimator *est = &stats->est;
187-
188-
/* set counters zero, caller must hold the stats->lock lock */
189-
est->last_inbytes = 0;
190-
est->last_outbytes = 0;
191-
est->last_conns = 0;
192-
est->last_inpkts = 0;
193-
est->last_outpkts = 0;
187+
struct ip_vs_stats_user *u = &stats->ustats;
188+
189+
/* reset counters, caller must hold the stats->lock lock */
190+
est->last_inbytes = u->inbytes;
191+
est->last_outbytes = u->outbytes;
192+
est->last_conns = u->conns;
193+
est->last_inpkts = u->inpkts;
194+
est->last_outpkts = u->outpkts;
194195
est->cps = 0;
195196
est->inpps = 0;
196197
est->outpps = 0;

0 commit comments

Comments
 (0)