Home | History | Annotate | Line # | Download | only in net
      1 /*	$NetBSD: if_stats.c,v 1.5 2024/06/29 02:18:35 riastradh 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.5 2024/06/29 02:18:35 riastradh Exp $");
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/mbuf.h>
     38 #include <sys/sdt.h>
     39 #include <sys/systm.h>
     40 #include <sys/xcall.h>
     41 
     42 #include <net/if.h>
     43 
     44 #define	IF_STATS_SIZE	(sizeof(uint64_t) * IF_NSTATS)
     45 
     46 SDT_PROBE_DEFINE3(sdt, net, interface, stat,
     47     "struct ifnet *"/*ifp*/,
     48     "if_stat_t"/*stat*/,
     49     "int64_t"/*delta*/);
     50 
     51 /*
     52  * if_stats_init --
     53  *	Initialize statistics storage for a network interface.
     54  */
     55 void
     56 if_stats_init(ifnet_t * const ifp)
     57 {
     58 	ifp->if_stats = percpu_alloc(IF_STATS_SIZE);
     59 }
     60 
     61 /*
     62  * if_stats_fini --
     63  *	Tear down statistics storage for a network interface.
     64  */
     65 void
     66 if_stats_fini(ifnet_t * const ifp)
     67 {
     68 	percpu_t *pc = ifp->if_stats;
     69 	ifp->if_stats = NULL;
     70 	if (pc) {
     71 		percpu_free(pc, IF_STATS_SIZE);
     72 	}
     73 }
     74 
     75 struct if_stats_to_if_data_ctx {
     76 	struct if_data * const ifi;
     77 	const bool zero_stats;
     78 };
     79 
     80 static void
     81 if_stats_to_if_data_cb(void *v1, void *v2, struct cpu_info *ci)
     82 {
     83 	const uint64_t * const local_counters = v1;
     84 	struct if_stats_to_if_data_ctx *ctx = v2;
     85 
     86 	int s = splnet();
     87 
     88 	if (ctx->ifi) {
     89 		ctx->ifi->ifi_ipackets   += local_counters[if_ipackets];
     90 		ctx->ifi->ifi_ierrors    += local_counters[if_ierrors];
     91 		ctx->ifi->ifi_opackets   += local_counters[if_opackets];
     92 		ctx->ifi->ifi_oerrors    += local_counters[if_oerrors];
     93 		ctx->ifi->ifi_collisions += local_counters[if_collisions];
     94 		ctx->ifi->ifi_ibytes     += local_counters[if_ibytes];
     95 		ctx->ifi->ifi_obytes     += local_counters[if_obytes];
     96 		ctx->ifi->ifi_imcasts    += local_counters[if_imcasts];
     97 		ctx->ifi->ifi_omcasts    += local_counters[if_omcasts];
     98 		ctx->ifi->ifi_iqdrops    += local_counters[if_iqdrops];
     99 		ctx->ifi->ifi_noproto    += local_counters[if_noproto];
    100 	}
    101 
    102 	if (ctx->zero_stats) {
    103 		memset(v1, 0, IF_STATS_SIZE);
    104 	}
    105 
    106 	splx(s);
    107 }
    108 
    109 /*
    110  * if_stats_to_if_data --
    111  *	Collect the interface statistics and place them into the
    112  *	legacy if_data structure for reportig to user space.
    113  *	Optionally zeros the stats after collection.
    114  */
    115 void
    116 if_stats_to_if_data(ifnet_t * const ifp, struct if_data * const ifi,
    117 		    const bool zero_stats)
    118 {
    119 	struct if_stats_to_if_data_ctx ctx = {
    120 		.ifi = ifi,
    121 		.zero_stats = zero_stats,
    122 	};
    123 
    124 	memset(ifi, 0, sizeof(*ifi));
    125 	percpu_foreach_xcall(ifp->if_stats, XC_HIGHPRI_IPL(IPL_SOFTNET),
    126 	    if_stats_to_if_data_cb, &ctx);
    127 }
    128