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