Home | History | Annotate | Line # | Download | only in net
if_stats.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: if_stats.c,v 1.1 2020/01/29 03:16:28 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  thorpej  */
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/cdefs.h>
     33  1.1  thorpej __KERNEL_RCSID(0, "$NetBSD: if_stats.c,v 1.1 2020/01/29 03:16:28 thorpej Exp $");
     34  1.1  thorpej 
     35  1.1  thorpej #include <sys/param.h>
     36  1.1  thorpej #include <sys/mbuf.h>
     37  1.1  thorpej #include <sys/systm.h>
     38  1.1  thorpej 
     39  1.1  thorpej #include <net/if.h>
     40  1.1  thorpej 
     41  1.1  thorpej #define	IF_STATS_SIZE	(sizeof(uint64_t) * IF_NSTATS)
     42  1.1  thorpej 
     43  1.1  thorpej /*
     44  1.1  thorpej  * if_stats_init --
     45  1.1  thorpej  *	Initialize statistics storage for a network interface.
     46  1.1  thorpej  */
     47  1.1  thorpej int
     48  1.1  thorpej if_stats_init(ifnet_t * const ifp)
     49  1.1  thorpej {
     50  1.1  thorpej #ifdef __IF_STATS_PERCPU
     51  1.1  thorpej 	ifp->if_stats = percpu_alloc(IF_STATS_SIZE);
     52  1.1  thorpej 	if (ifp->if_stats == NULL)
     53  1.1  thorpej 		return ENOMEM;
     54  1.1  thorpej #endif /* __IF_STATS_PERCPU */
     55  1.1  thorpej 	return 0;
     56  1.1  thorpej }
     57  1.1  thorpej 
     58  1.1  thorpej /*
     59  1.1  thorpej  * if_stats_fini --
     60  1.1  thorpej  *	Tear down statistics storage for a network interface.
     61  1.1  thorpej  */
     62  1.1  thorpej void
     63  1.1  thorpej if_stats_fini(ifnet_t * const ifp)
     64  1.1  thorpej {
     65  1.1  thorpej #ifdef __IF_STATS_PERCPU
     66  1.1  thorpej 	percpu_t *pc = ifp->if_stats;
     67  1.1  thorpej 	ifp->if_stats = NULL;
     68  1.1  thorpej 	if (pc) {
     69  1.1  thorpej 		percpu_free(pc, IF_STATS_SIZE);
     70  1.1  thorpej 	}
     71  1.1  thorpej #endif /* __IF_STATS_PERCPU */
     72  1.1  thorpej }
     73  1.1  thorpej 
     74  1.1  thorpej #ifdef __IF_STATS_PERCPU
     75  1.1  thorpej 
     76  1.1  thorpej struct if_stats_to_if_data_ctx {
     77  1.1  thorpej 	struct if_data * const ifi;
     78  1.1  thorpej 	const bool zero_stats;
     79  1.1  thorpej };
     80  1.1  thorpej 
     81  1.1  thorpej static void
     82  1.1  thorpej if_stats_to_if_data_cb(void *v1, void *v2, struct cpu_info *ci)
     83  1.1  thorpej {
     84  1.1  thorpej 	const uint64_t * const local_counters = v1;
     85  1.1  thorpej 	struct if_stats_to_if_data_ctx *ctx = v2;
     86  1.1  thorpej 
     87  1.1  thorpej 	int s = splnet();
     88  1.1  thorpej 
     89  1.1  thorpej 	if (ctx->ifi) {
     90  1.1  thorpej 		ctx->ifi->ifi_ipackets   += local_counters[if_ipackets];
     91  1.1  thorpej 		ctx->ifi->ifi_ierrors    += local_counters[if_ierrors];
     92  1.1  thorpej 		ctx->ifi->ifi_opackets   += local_counters[if_opackets];
     93  1.1  thorpej 		ctx->ifi->ifi_oerrors    += local_counters[if_oerrors];
     94  1.1  thorpej 		ctx->ifi->ifi_collisions += local_counters[if_collisions];
     95  1.1  thorpej 		ctx->ifi->ifi_ibytes     += local_counters[if_ibytes];
     96  1.1  thorpej 		ctx->ifi->ifi_obytes     += local_counters[if_obytes];
     97  1.1  thorpej 		ctx->ifi->ifi_imcasts    += local_counters[if_imcasts];
     98  1.1  thorpej 		ctx->ifi->ifi_omcasts    += local_counters[if_omcasts];
     99  1.1  thorpej 		ctx->ifi->ifi_iqdrops    += local_counters[if_iqdrops];
    100  1.1  thorpej 		ctx->ifi->ifi_noproto    += local_counters[if_noproto];
    101  1.1  thorpej 	}
    102  1.1  thorpej 
    103  1.1  thorpej 	if (ctx->zero_stats) {
    104  1.1  thorpej 		memset(v1, 0, IF_STATS_SIZE);
    105  1.1  thorpej 	}
    106  1.1  thorpej 
    107  1.1  thorpej 	splx(s);
    108  1.1  thorpej }
    109  1.1  thorpej 
    110  1.1  thorpej /*
    111  1.1  thorpej  * if_stats_to_if_data --
    112  1.1  thorpej  *	Collect the interface statistics and place them into the
    113  1.1  thorpej  *	legacy if_data structure for reportig to user space.
    114  1.1  thorpej  *	Optionally zeros the stats after collection.
    115  1.1  thorpej  */
    116  1.1  thorpej void
    117  1.1  thorpej if_stats_to_if_data(ifnet_t * const ifp, struct if_data * const ifi,
    118  1.1  thorpej 		    const bool zero_stats)
    119  1.1  thorpej {
    120  1.1  thorpej 	struct if_stats_to_if_data_ctx ctx = {
    121  1.1  thorpej 		.ifi = ifi,
    122  1.1  thorpej 		.zero_stats = zero_stats,
    123  1.1  thorpej 	};
    124  1.1  thorpej 
    125  1.1  thorpej 	memset(ifi, 0, sizeof(*ifi));
    126  1.1  thorpej 	percpu_foreach(ifp->if_stats, if_stats_to_if_data_cb, &ctx);
    127  1.1  thorpej }
    128  1.1  thorpej 
    129  1.1  thorpej #else /* ! __IF_STATS_PERCPU */
    130  1.1  thorpej 
    131  1.1  thorpej /*
    132  1.1  thorpej  * if_stats_to_if_data --
    133  1.1  thorpej  *	Collect the interface statistics and place them into the
    134  1.1  thorpej  *	legacy if_data structure for reportig to user space.
    135  1.1  thorpej  *	Optionally zeros the stats after collection.
    136  1.1  thorpej  */
    137  1.1  thorpej void
    138  1.1  thorpej if_stats_to_if_data(ifnet_t * const ifp, struct if_data * const ifi,
    139  1.1  thorpej 		    const bool zero_stats)
    140  1.1  thorpej {
    141  1.1  thorpej 
    142  1.1  thorpej 	memset(ifi, 0, sizeof(*ifi));
    143  1.1  thorpej 
    144  1.1  thorpej 	int s = splnet();
    145  1.1  thorpej 
    146  1.1  thorpej 	if (ifi) {
    147  1.1  thorpej 		memcpy(&ifi->ifi_ipackets, &ifp->if_data.ifi_ipackets,
    148  1.1  thorpej 		    offsetof(struct if_data, ifi_lastchange) -
    149  1.1  thorpej 		    offsetof(struct if_data, ifi_ipackets));
    150  1.1  thorpej 	}
    151  1.1  thorpej 
    152  1.1  thorpej 	if (zero_stats) {
    153  1.1  thorpej 		memset(&ifp->if_data.ifi_ipackets, 0,
    154  1.1  thorpej 		    offsetof(struct if_data, ifi_lastchange) -
    155  1.1  thorpej 		    offsetof(struct if_data, ifi_ipackets));
    156  1.1  thorpej 	}
    157  1.1  thorpej 
    158  1.1  thorpej 	splx(s);
    159  1.1  thorpej }
    160  1.1  thorpej 
    161  1.1  thorpej #endif /* __IF_STATS_PERCPU */
    162