if_stats.c revision 1.1 1 /* $NetBSD: if_stats.c,v 1.1 2020/01/29 03:16:28 thorpej 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.1 2020/01/29 03:16:28 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/mbuf.h>
37 #include <sys/systm.h>
38
39 #include <net/if.h>
40
41 #define IF_STATS_SIZE (sizeof(uint64_t) * IF_NSTATS)
42
43 /*
44 * if_stats_init --
45 * Initialize statistics storage for a network interface.
46 */
47 int
48 if_stats_init(ifnet_t * const ifp)
49 {
50 #ifdef __IF_STATS_PERCPU
51 ifp->if_stats = percpu_alloc(IF_STATS_SIZE);
52 if (ifp->if_stats == NULL)
53 return ENOMEM;
54 #endif /* __IF_STATS_PERCPU */
55 return 0;
56 }
57
58 /*
59 * if_stats_fini --
60 * Tear down statistics storage for a network interface.
61 */
62 void
63 if_stats_fini(ifnet_t * const ifp)
64 {
65 #ifdef __IF_STATS_PERCPU
66 percpu_t *pc = ifp->if_stats;
67 ifp->if_stats = NULL;
68 if (pc) {
69 percpu_free(pc, IF_STATS_SIZE);
70 }
71 #endif /* __IF_STATS_PERCPU */
72 }
73
74 #ifdef __IF_STATS_PERCPU
75
76 struct if_stats_to_if_data_ctx {
77 struct if_data * const ifi;
78 const bool zero_stats;
79 };
80
81 static void
82 if_stats_to_if_data_cb(void *v1, void *v2, struct cpu_info *ci)
83 {
84 const uint64_t * const local_counters = v1;
85 struct if_stats_to_if_data_ctx *ctx = v2;
86
87 int s = splnet();
88
89 if (ctx->ifi) {
90 ctx->ifi->ifi_ipackets += local_counters[if_ipackets];
91 ctx->ifi->ifi_ierrors += local_counters[if_ierrors];
92 ctx->ifi->ifi_opackets += local_counters[if_opackets];
93 ctx->ifi->ifi_oerrors += local_counters[if_oerrors];
94 ctx->ifi->ifi_collisions += local_counters[if_collisions];
95 ctx->ifi->ifi_ibytes += local_counters[if_ibytes];
96 ctx->ifi->ifi_obytes += local_counters[if_obytes];
97 ctx->ifi->ifi_imcasts += local_counters[if_imcasts];
98 ctx->ifi->ifi_omcasts += local_counters[if_omcasts];
99 ctx->ifi->ifi_iqdrops += local_counters[if_iqdrops];
100 ctx->ifi->ifi_noproto += local_counters[if_noproto];
101 }
102
103 if (ctx->zero_stats) {
104 memset(v1, 0, IF_STATS_SIZE);
105 }
106
107 splx(s);
108 }
109
110 /*
111 * if_stats_to_if_data --
112 * Collect the interface statistics and place them into the
113 * legacy if_data structure for reportig to user space.
114 * Optionally zeros the stats after collection.
115 */
116 void
117 if_stats_to_if_data(ifnet_t * const ifp, struct if_data * const ifi,
118 const bool zero_stats)
119 {
120 struct if_stats_to_if_data_ctx ctx = {
121 .ifi = ifi,
122 .zero_stats = zero_stats,
123 };
124
125 memset(ifi, 0, sizeof(*ifi));
126 percpu_foreach(ifp->if_stats, if_stats_to_if_data_cb, &ctx);
127 }
128
129 #else /* ! __IF_STATS_PERCPU */
130
131 /*
132 * if_stats_to_if_data --
133 * Collect the interface statistics and place them into the
134 * legacy if_data structure for reportig to user space.
135 * Optionally zeros the stats after collection.
136 */
137 void
138 if_stats_to_if_data(ifnet_t * const ifp, struct if_data * const ifi,
139 const bool zero_stats)
140 {
141
142 memset(ifi, 0, sizeof(*ifi));
143
144 int s = splnet();
145
146 if (ifi) {
147 memcpy(&ifi->ifi_ipackets, &ifp->if_data.ifi_ipackets,
148 offsetof(struct if_data, ifi_lastchange) -
149 offsetof(struct if_data, ifi_ipackets));
150 }
151
152 if (zero_stats) {
153 memset(&ifp->if_data.ifi_ipackets, 0,
154 offsetof(struct if_data, ifi_lastchange) -
155 offsetof(struct if_data, ifi_ipackets));
156 }
157
158 splx(s);
159 }
160
161 #endif /* __IF_STATS_PERCPU */
162