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