Add subroutines to support collating per-cpu-gathered network statistics.

This commit is contained in:
thorpej 2008-04-23 05:21:17 +00:00
parent 3d7d158b1c
commit 34908fe541
5 changed files with 199 additions and 4 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1132 2008/04/22 10:42:17 rmind Exp $
# $NetBSD: mi,v 1.1133 2008/04/23 05:21:17 thorpej Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@ -1379,6 +1379,7 @@
./usr/include/net/if_tun.h comp-c-include
./usr/include/net/if_types.h comp-c-include
./usr/include/net/if_vlanvar.h comp-c-include
./usr/include/net/net_stats.h comp-c-include
./usr/include/net/netisr.h comp-c-include
./usr/include/net/pfil.h comp-c-include
./usr/include/net/pfkeyv2.h comp-c-include

View File

@ -1,4 +1,4 @@
# $NetBSD: files,v 1.898 2008/03/30 12:32:13 skrll Exp $
# $NetBSD: files,v 1.899 2008/04/23 05:21:17 thorpej Exp $
# @(#)files.newconf 7.5 (Berkeley) 5/10/93
@ -1522,6 +1522,7 @@ file net/if_pppoe.c pppoe needs-flag
file net/pfil.c pfil_hooks | ipfilter | pf
file net/ppp-deflate.c ppp & ppp_deflate
file net/ppp_tty.c ppp
file net/net_stats.c
file net/radix.c
file net/raw_cb.c
file net/raw_usrreq.c

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.22 2007/01/13 14:13:54 isaki Exp $
# $NetBSD: Makefile,v 1.23 2008/04/23 05:21:17 thorpej Exp $
INCSDIR= /usr/include/net
@ -6,7 +6,7 @@ INCS= bpf.h bpfdesc.h dlt.h ethertypes.h if.h if_arc.h if_arp.h \
if_atm.h if_bridgevar.h if_dl.h if_ether.h if_etherip.h if_fddi.h if_gif.h \
if_gre.h if_hippi.h if_ieee1394.h if_llc.h if_media.h \
if_pflog.h if_ppp.h if_pppoe.h if_sppp.h if_srt.h if_stf.h \
if_tap.h if_token.h if_tun.h if_types.h if_vlanvar.h \
if_tap.h if_token.h if_tun.h if_types.h if_vlanvar.h net_stats.h \
netisr.h pfil.h pfkeyv2.h pfvar.h ppp-comp.h ppp_defs.h radix.h \
raw_cb.h route.h slcompress.h slip.h zlib.h

92
sys/net/net_stats.c Normal file
View File

@ -0,0 +1,92 @@
/* $NetBSD: net_stats.c,v 1.1 2008/04/23 05:21:17 thorpej Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: net_stats.c,v 1.1 2008/04/23 05:21:17 thorpej Exp $");
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <net/net_stats.h>
/*
* netstat_convert_to_user_cb --
* Internal routine used as a call-back for percpu data enumeration.
*/
static void
netstat_convert_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
{
const uint64_t *local_counters = v1;
netstat_sysctl_context *ctx = v2;
u_int i;
for (i = 0; i < ctx->ctx_ncounters; i++)
ctx->ctx_counters[i] += local_counters[i];
}
/*
* netstat_convert_to_user --
* Internal routine to convert per-CPU network statistics into
* collated form.
*/
static void
netstat_convert_to_user(netstat_sysctl_context *ctx)
{
memset(ctx->ctx_counters, 0, sizeof(uint64_t) * ctx->ctx_ncounters);
percpu_foreach(ctx->ctx_stat, netstat_convert_to_user_cb, ctx);
}
/*
* netstat_sysctl --
* Common routine for collating and reporting network statistics
* that are gathered per-CPU. Statistics counters are assumed
* to be arrays of uint64_t's.
*/
int
netstat_sysctl(netstat_sysctl_context *ctx, SYSCTLFN_ARGS)
{
struct sysctlnode node;
netstat_convert_to_user(ctx);
node = *rnode;
node.sysctl_data = ctx->ctx_counters;
node.sysctl_size = ctx->ctx_ncounters;
return (sysctl_lookup(SYSCTLFN_CALL(&node)));
}

101
sys/net/net_stats.h Normal file
View File

@ -0,0 +1,101 @@
/* $NetBSD: net_stats.h,v 1.1 2008/04/23 05:21:17 thorpej Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _NET_NET_STATS_H_
#define _NET_NET_STATS_H_
#ifdef _KERNEL
#include <sys/percpu.h>
#define _NET_STAT_GETREF(stat) percpu_getref((stat))
#define _NET_STAT_PUTREF(stat) percpu_putref((stat))
#define _NET_STATINC(stat, x) \
do { \
uint64_t *_stat_ = _NET_STAT_GETREF(stat); \
_stat_[x]++; \
_NET_STAT_PUTREF(stat); \
} while (/*CONSTCOND*/0)
#define _NET_STATDEC(stat, x) \
do { \
uint64_t *_stat_ = _NET_STAT_GETREF(stat); \
_stat_[x]--; \
_NET_STAT_PUTREF(stat); \
} while (/*CONSTCOND*/0)
#define _NET_STATADD(stat, x, v) \
do { \
uint64_t *_stat_ = _NET_STAT_GETREF(stat); \
_stat_[x] += (v); \
_NET_STAT_PUTREF(stat); \
} while (/*CONSTCOND*/0)
#define _NET_STATSUB(stat, x, v) \
do { \
uint64_t *_stat_ = _NET_STAT_GETREF(stat); \
_stat_[x] -= (v); \
_NET_STAT_PUTREF(stat); \
} while (/*CONSTCOND*/0)
/*
* netstat_sysctl_context --
* Context passed to netstat_sysctl().
*/
typedef struct {
percpu_t *ctx_stat; /* stat's percpu context */
uint64_t *ctx_counters; /* pointer to collated counter array */
u_int ctx_ncounters; /* number of counters in array */
} netstat_sysctl_context;
__BEGIN_DECLS
struct lwp;
struct sysctlnode;
int netstat_sysctl(netstat_sysctl_context *,
const int *, u_int, void *,
size_t *, const void *, size_t,
const int *, struct lwp *, const struct sysctlnode *);
#define NETSTAT_SYSCTL(ctx) \
netstat_sysctl((ctx), name, namelen, oldp, oldlenp, \
newp, newlen, oname, l, rnode)
__END_DECLS
#endif /* _KERNEL */
#endif /* !_NET_NET_STATS_H_ */