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