1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010-2011 Juniper Networks, Inc.
5 * All rights reserved.
6 *
7 * This software was developed by Robert N. M. Watson under contract
8 * to Juniper Networks, Inc.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
38
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41
42 #define _WANT_NETISR_INTERNAL
43 #include <net/netisr.h>
44 #include <net/netisr_internal.h>
45
46 #include <err.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdbool.h>
51 #include <string.h>
52 #include <libxo/xo.h>
53 #include "netstat.h"
54 #include "nl_defs.h"
55
56 /*
57 * Print statistics for the kernel netisr subsystem.
58 */
59 static u_int bindthreads;
60 static u_int maxthreads;
61 static u_int numthreads;
62
63 static u_int defaultqlimit;
64 static u_int maxqlimit;
65
66 static char dispatch_policy[20];
67
68 static struct sysctl_netisr_proto *proto_array;
69 static u_int proto_array_len;
70
71 static struct sysctl_netisr_workstream *workstream_array;
72 static u_int workstream_array_len;
73
74 static struct sysctl_netisr_work *work_array;
75 static u_int work_array_len;
76
77 #ifndef FSTACK
78 static u_int *nws_array;
79
80 static u_int maxprot;
81 #endif
82
83 static void
netisr_dispatch_policy_to_string(u_int policy,char * buf,size_t buflen)84 netisr_dispatch_policy_to_string(u_int policy, char *buf,
85 size_t buflen)
86 {
87 const char *str;
88
89 switch (policy) {
90 case NETISR_DISPATCH_DEFAULT:
91 str = "default";
92 break;
93 case NETISR_DISPATCH_DEFERRED:
94 str = "deferred";
95 break;
96 case NETISR_DISPATCH_HYBRID:
97 str = "hybrid";
98 break;
99 case NETISR_DISPATCH_DIRECT:
100 str = "direct";
101 break;
102 default:
103 str = "unknown";
104 break;
105 }
106 snprintf(buf, buflen, "%s", str);
107 }
108
109 #ifndef FSTACK
110 /*
111 * Load a nul-terminated string from KVM up to 'limit', guarantee that the
112 * string in local memory is nul-terminated.
113 */
114 static void
netisr_load_kvm_string(uintptr_t addr,char * dest,u_int limit)115 netisr_load_kvm_string(uintptr_t addr, char *dest, u_int limit)
116 {
117 u_int i;
118
119 for (i = 0; i < limit; i++) {
120 if (kread(addr + i, &dest[i], sizeof(dest[i])) != 0)
121 xo_errx(-1, "%s: kread()", __func__);
122 if (dest[i] == '\0')
123 break;
124 }
125 dest[limit - 1] = '\0';
126 }
127 #endif
128
129 static const char *
netisr_proto2name(u_int proto)130 netisr_proto2name(u_int proto)
131 {
132 u_int i;
133
134 for (i = 0; i < proto_array_len; i++) {
135 if (proto_array[i].snp_proto == proto)
136 return (proto_array[i].snp_name);
137 }
138 return ("unknown");
139 }
140
141 #ifndef FSTACK
142 static int
netisr_protoispresent(u_int proto)143 netisr_protoispresent(u_int proto)
144 {
145 u_int i;
146
147 for (i = 0; i < proto_array_len; i++) {
148 if (proto_array[i].snp_proto == proto)
149 return (1);
150 }
151 return (0);
152 }
153
154 static void
netisr_load_kvm_config(void)155 netisr_load_kvm_config(void)
156 {
157 u_int tmp;
158
159 kread(nl[N_NETISR_BINDTHREADS].n_value, &bindthreads, sizeof(u_int));
160 kread(nl[N_NETISR_MAXTHREADS].n_value, &maxthreads, sizeof(u_int));
161 kread(nl[N_NWS_COUNT].n_value, &numthreads, sizeof(u_int));
162 kread(nl[N_NETISR_DEFAULTQLIMIT].n_value, &defaultqlimit,
163 sizeof(u_int));
164 kread(nl[N_NETISR_MAXQLIMIT].n_value, &maxqlimit, sizeof(u_int));
165 kread(nl[N_NETISR_DISPATCH_POLICY].n_value, &tmp, sizeof(u_int));
166
167 netisr_dispatch_policy_to_string(tmp, dispatch_policy,
168 sizeof(dispatch_policy));
169 }
170 #endif
171
172 static void
netisr_load_sysctl_uint(const char * name,u_int * p)173 netisr_load_sysctl_uint(const char *name, u_int *p)
174 {
175 size_t retlen;
176
177 retlen = sizeof(u_int);
178 if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
179 xo_err(-1, "%s", name);
180 if (retlen != sizeof(u_int))
181 xo_errx(-1, "%s: invalid len %ju", name, (uintmax_t)retlen);
182 }
183
184 static void
netisr_load_sysctl_string(const char * name,char * p,size_t len)185 netisr_load_sysctl_string(const char *name, char *p, size_t len)
186 {
187 size_t retlen;
188
189 retlen = len;
190 if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
191 xo_err(-1, "%s", name);
192 p[len - 1] = '\0';
193 }
194
195 static void
netisr_load_sysctl_config(void)196 netisr_load_sysctl_config(void)
197 {
198
199 netisr_load_sysctl_uint("net.isr.bindthreads", &bindthreads);
200 netisr_load_sysctl_uint("net.isr.maxthreads", &maxthreads);
201 netisr_load_sysctl_uint("net.isr.numthreads", &numthreads);
202
203 netisr_load_sysctl_uint("net.isr.defaultqlimit", &defaultqlimit);
204 netisr_load_sysctl_uint("net.isr.maxqlimit", &maxqlimit);
205
206 netisr_load_sysctl_string("net.isr.dispatch", dispatch_policy,
207 sizeof(dispatch_policy));
208 }
209
210 #ifndef FSTACK
211 static void
netisr_load_kvm_proto(void)212 netisr_load_kvm_proto(void)
213 {
214 struct netisr_proto *np_array, *npp;
215 u_int i, protocount;
216 struct sysctl_netisr_proto *snpp;
217 size_t len;
218
219 /*
220 * Kernel compile-time and user compile-time definitions of
221 * NETISR_MAXPROT must match, as we use that to size work arrays.
222 */
223 kread(nl[N_NETISR_MAXPROT].n_value, &maxprot, sizeof(u_int));
224 if (maxprot != NETISR_MAXPROT)
225 xo_errx(-1, "%s: NETISR_MAXPROT mismatch", __func__);
226 len = maxprot * sizeof(*np_array);
227 np_array = malloc(len);
228 if (np_array == NULL)
229 xo_err(-1, "%s: malloc", __func__);
230 if (kread(nl[N_NETISR_PROTO].n_value, np_array, len) != 0)
231 xo_errx(-1, "%s: kread(_netisr_proto)", __func__);
232
233 /*
234 * Size and allocate memory to hold only live protocols.
235 */
236 protocount = 0;
237 for (i = 0; i < maxprot; i++) {
238 if (np_array[i].np_name == NULL)
239 continue;
240 protocount++;
241 }
242 proto_array = calloc(protocount, sizeof(*proto_array));
243 if (proto_array == NULL)
244 err(-1, "malloc");
245 protocount = 0;
246 for (i = 0; i < maxprot; i++) {
247 npp = &np_array[i];
248 if (npp->np_name == NULL)
249 continue;
250 snpp = &proto_array[protocount];
251 snpp->snp_version = sizeof(*snpp);
252 netisr_load_kvm_string((uintptr_t)npp->np_name,
253 snpp->snp_name, sizeof(snpp->snp_name));
254 snpp->snp_proto = i;
255 snpp->snp_qlimit = npp->np_qlimit;
256 snpp->snp_policy = npp->np_policy;
257 snpp->snp_dispatch = npp->np_dispatch;
258 if (npp->np_m2flow != NULL)
259 snpp->snp_flags |= NETISR_SNP_FLAGS_M2FLOW;
260 if (npp->np_m2cpuid != NULL)
261 snpp->snp_flags |= NETISR_SNP_FLAGS_M2CPUID;
262 if (npp->np_drainedcpu != NULL)
263 snpp->snp_flags |= NETISR_SNP_FLAGS_DRAINEDCPU;
264 protocount++;
265 }
266 proto_array_len = protocount;
267 free(np_array);
268 }
269 #endif
270
271 static void
netisr_load_sysctl_proto(void)272 netisr_load_sysctl_proto(void)
273 {
274 size_t len;
275
276 if (sysctlbyname("net.isr.proto", NULL, &len, NULL, 0) < 0)
277 xo_err(-1, "net.isr.proto: query len");
278 if (len % sizeof(*proto_array) != 0)
279 xo_errx(-1, "net.isr.proto: invalid len");
280 proto_array = malloc(len);
281 if (proto_array == NULL)
282 xo_err(-1, "malloc");
283 if (sysctlbyname("net.isr.proto", proto_array, &len, NULL, 0) < 0)
284 xo_err(-1, "net.isr.proto: query data");
285 if (len % sizeof(*proto_array) != 0)
286 xo_errx(-1, "net.isr.proto: invalid len");
287 proto_array_len = len / sizeof(*proto_array);
288 if (proto_array_len < 1)
289 xo_errx(-1, "net.isr.proto: no data");
290 if (proto_array[0].snp_version != sizeof(proto_array[0]))
291 xo_errx(-1, "net.isr.proto: invalid version");
292 }
293
294 #ifndef FSTACK
295 static void
netisr_load_kvm_workstream(void)296 netisr_load_kvm_workstream(void)
297 {
298 struct netisr_workstream nws;
299 struct sysctl_netisr_workstream *snwsp;
300 struct sysctl_netisr_work *snwp;
301 struct netisr_work *nwp;
302 u_int counter, cpuid, proto, wsid;
303 size_t len;
304
305 len = numthreads * sizeof(*nws_array);
306 nws_array = malloc(len);
307 if (nws_array == NULL)
308 xo_err(-1, "malloc");
309 if (kread(nl[N_NWS_ARRAY].n_value, nws_array, len) != 0)
310 xo_errx(-1, "%s: kread(_nws_array)", __func__);
311 workstream_array = calloc(numthreads, sizeof(*workstream_array));
312 if (workstream_array == NULL)
313 xo_err(-1, "calloc");
314 workstream_array_len = numthreads;
315 work_array = calloc(numthreads * proto_array_len, sizeof(*work_array));
316 if (work_array == NULL)
317 xo_err(-1, "calloc");
318 counter = 0;
319 for (wsid = 0; wsid < numthreads; wsid++) {
320 cpuid = nws_array[wsid];
321 kset_dpcpu(cpuid);
322 if (kread(nl[N_NWS].n_value, &nws, sizeof(nws)) != 0)
323 xo_errx(-1, "%s: kread(nw)", __func__);
324 snwsp = &workstream_array[wsid];
325 snwsp->snws_version = sizeof(*snwsp);
326 snwsp->snws_wsid = cpuid;
327 snwsp->snws_cpu = cpuid;
328 if (nws.nws_intr_event != NULL)
329 snwsp->snws_flags |= NETISR_SNWS_FLAGS_INTR;
330
331 /*
332 * Extract the CPU's per-protocol work information.
333 */
334 xo_emit("counting to maxprot: {:maxprot/%u}\n", maxprot);
335 for (proto = 0; proto < maxprot; proto++) {
336 if (!netisr_protoispresent(proto))
337 continue;
338 nwp = &nws.nws_work[proto];
339 snwp = &work_array[counter];
340 snwp->snw_version = sizeof(*snwp);
341 snwp->snw_wsid = cpuid;
342 snwp->snw_proto = proto;
343 snwp->snw_len = nwp->nw_len;
344 snwp->snw_watermark = nwp->nw_watermark;
345 snwp->snw_dispatched = nwp->nw_dispatched;
346 snwp->snw_hybrid_dispatched =
347 nwp->nw_hybrid_dispatched;
348 snwp->snw_qdrops = nwp->nw_qdrops;
349 snwp->snw_queued = nwp->nw_queued;
350 snwp->snw_handled = nwp->nw_handled;
351 counter++;
352 }
353 }
354 work_array_len = counter;
355 }
356 #endif
357
358 static void
netisr_load_sysctl_workstream(void)359 netisr_load_sysctl_workstream(void)
360 {
361 size_t len;
362
363 if (sysctlbyname("net.isr.workstream", NULL, &len, NULL, 0) < 0)
364 xo_err(-1, "net.isr.workstream: query len");
365 if (len % sizeof(*workstream_array) != 0)
366 xo_errx(-1, "net.isr.workstream: invalid len");
367 workstream_array = malloc(len);
368 if (workstream_array == NULL)
369 xo_err(-1, "malloc");
370 if (sysctlbyname("net.isr.workstream", workstream_array, &len, NULL,
371 0) < 0)
372 xo_err(-1, "net.isr.workstream: query data");
373 if (len % sizeof(*workstream_array) != 0)
374 xo_errx(-1, "net.isr.workstream: invalid len");
375 workstream_array_len = len / sizeof(*workstream_array);
376 if (workstream_array_len < 1)
377 xo_errx(-1, "net.isr.workstream: no data");
378 if (workstream_array[0].snws_version != sizeof(workstream_array[0]))
379 xo_errx(-1, "net.isr.workstream: invalid version");
380 }
381
382 static void
netisr_load_sysctl_work(void)383 netisr_load_sysctl_work(void)
384 {
385 size_t len;
386
387 if (sysctlbyname("net.isr.work", NULL, &len, NULL, 0) < 0)
388 xo_err(-1, "net.isr.work: query len");
389 if (len % sizeof(*work_array) != 0)
390 xo_errx(-1, "net.isr.work: invalid len");
391 work_array = malloc(len);
392 if (work_array == NULL)
393 xo_err(-1, "malloc");
394 if (sysctlbyname("net.isr.work", work_array, &len, NULL, 0) < 0)
395 xo_err(-1, "net.isr.work: query data");
396 if (len % sizeof(*work_array) != 0)
397 xo_errx(-1, "net.isr.work: invalid len");
398 work_array_len = len / sizeof(*work_array);
399 if (work_array_len < 1)
400 xo_errx(-1, "net.isr.work: no data");
401 if (work_array[0].snw_version != sizeof(work_array[0]))
402 xo_errx(-1, "net.isr.work: invalid version");
403 }
404
405 static void
netisr_print_proto(struct sysctl_netisr_proto * snpp)406 netisr_print_proto(struct sysctl_netisr_proto *snpp)
407 {
408 char tmp[20];
409
410 xo_emit("{[:-6}{k:name/%s}{]:}", snpp->snp_name);
411 xo_emit(" {:protocol/%5u}", snpp->snp_proto);
412 xo_emit(" {:queue-limit/%6u}", snpp->snp_qlimit);
413 xo_emit(" {:policy-type/%6s}",
414 (snpp->snp_policy == NETISR_POLICY_SOURCE) ? "source" :
415 (snpp->snp_policy == NETISR_POLICY_FLOW) ? "flow" :
416 (snpp->snp_policy == NETISR_POLICY_CPU) ? "cpu" : "-");
417 netisr_dispatch_policy_to_string(snpp->snp_dispatch, tmp,
418 sizeof(tmp));
419 xo_emit(" {:policy/%8s}", tmp);
420 xo_emit(" {:flags/%s%s%s}\n",
421 (snpp->snp_flags & NETISR_SNP_FLAGS_M2CPUID) ? "C" : "-",
422 (snpp->snp_flags & NETISR_SNP_FLAGS_DRAINEDCPU) ? "D" : "-",
423 (snpp->snp_flags & NETISR_SNP_FLAGS_M2FLOW) ? "F" : "-");
424 }
425
426 static void
netisr_print_workstream(struct sysctl_netisr_workstream * snwsp)427 netisr_print_workstream(struct sysctl_netisr_workstream *snwsp)
428 {
429 struct sysctl_netisr_work *snwp;
430 u_int i;
431
432 xo_open_list("work");
433 for (i = 0; i < work_array_len; i++) {
434 snwp = &work_array[i];
435 if (snwp->snw_wsid != snwsp->snws_wsid)
436 continue;
437 xo_open_instance("work");
438 xo_emit("{t:workstream/%4u} ", snwsp->snws_wsid);
439 xo_emit("{t:cpu/%3u} ", snwsp->snws_cpu);
440 xo_emit("{P: }");
441 xo_emit("{t:name/%-6s}", netisr_proto2name(snwp->snw_proto));
442 xo_emit(" {t:length/%5u}", snwp->snw_len);
443 xo_emit(" {t:watermark/%5u}", snwp->snw_watermark);
444 xo_emit(" {t:dispatched/%8ju}", snwp->snw_dispatched);
445 xo_emit(" {t:hybrid-dispatched/%8ju}",
446 snwp->snw_hybrid_dispatched);
447 xo_emit(" {t:queue-drops/%8ju}", snwp->snw_qdrops);
448 xo_emit(" {t:queued/%8ju}", snwp->snw_queued);
449 xo_emit(" {t:handled/%8ju}", snwp->snw_handled);
450 xo_emit("\n");
451 xo_close_instance("work");
452 }
453 xo_close_list("work");
454 }
455
456 void
netisr_stats(void)457 netisr_stats(void)
458 {
459 struct sysctl_netisr_workstream *snwsp;
460 struct sysctl_netisr_proto *snpp;
461 u_int i;
462
463 if (live) {
464 netisr_load_sysctl_config();
465 netisr_load_sysctl_proto();
466 netisr_load_sysctl_workstream();
467 netisr_load_sysctl_work();
468 } else {
469 #ifndef FSTACK
470 netisr_load_kvm_config();
471 netisr_load_kvm_proto();
472 netisr_load_kvm_workstream(); /* Also does work. */
473 #endif
474 }
475
476 xo_open_container("netisr");
477
478 xo_emit("{T:Configuration}:\n");
479 xo_emit("{T:/%-25s} {T:/%12s} {T:/%12s}\n",
480 "Setting", "Current", "Limit");
481 xo_emit("{T:/%-25s} {T:/%12u} {T:/%12u}\n",
482 "Thread count", numthreads, maxthreads);
483 xo_emit("{T:/%-25s} {T:/%12u} {T:/%12u}\n",
484 "Default queue limit", defaultqlimit, maxqlimit);
485 xo_emit("{T:/%-25s} {T:/%12s} {T:/%12s}\n",
486 "Dispatch policy", dispatch_policy, "n/a");
487 xo_emit("{T:/%-25s} {T:/%12s} {T:/%12s}\n",
488 "Threads bound to CPUs", bindthreads ? "enabled" : "disabled",
489 "n/a");
490 xo_emit("\n");
491
492 xo_emit("{T:Protocols}:\n");
493 xo_emit("{T:/%-6s} {T:/%5s} {T:/%6s} {T:/%-6s} {T:/%-8s} {T:/%-5s}\n",
494 "Name", "Proto", "QLimit", "Policy", "Dispatch", "Flags");
495 xo_open_list("protocol");
496 for (i = 0; i < proto_array_len; i++) {
497 xo_open_instance("protocol");
498 snpp = &proto_array[i];
499 netisr_print_proto(snpp);
500 xo_close_instance("protocol");
501 }
502 xo_close_list("protocol");
503 xo_emit("\n");
504
505 xo_emit("{T:Workstreams}:\n");
506 xo_emit("{T:/%4s} {T:/%3s} ", "WSID", "CPU");
507 xo_emit("{P:/%2s}", "");
508 xo_emit("{T:/%-6s} {T:/%5s} {T:/%5s} {T:/%8s} {T:/%8s} {T:/%8s} "
509 "{T:/%8s} {T:/%8s}\n",
510 "Name", "Len", "WMark", "Disp'd", "HDisp'd", "QDrops", "Queued",
511 "Handled");
512 xo_open_list("workstream");
513 for (i = 0; i < workstream_array_len; i++) {
514 xo_open_instance("workstream");
515 snwsp = &workstream_array[i];
516 netisr_print_workstream(snwsp);
517 xo_close_instance("workstream");
518 }
519 xo_close_list("workstream");
520 xo_close_container("netisr");
521 }
522