2 * Copyright (c) 2010-2011 Juniper Networks, Inc.
5 * This software was developed by Robert N. M. Watson under contract
6 * to Juniper Networks, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
37 #include <sys/_lock.h>
38 #include <sys/_mutex.h>
40 #define _WANT_NETISR_INTERNAL
41 #include <net/netisr.h>
42 #include <net/netisr_internal.h>
54 * Print statistics for the kernel netisr subsystem.
56 static u_int bindthreads;
57 static u_int maxthreads;
58 static u_int numthreads;
60 static u_int defaultqlimit;
61 static u_int maxqlimit;
63 static char dispatch_policy[20];
65 static struct sysctl_netisr_proto *proto_array;
66 static u_int proto_array_len;
68 static struct sysctl_netisr_workstream *workstream_array;
69 static u_int workstream_array_len;
71 static struct sysctl_netisr_work *work_array;
72 static u_int work_array_len;
74 static u_int *nws_array;
79 netisr_dispatch_policy_to_string(u_int dispatch_policy, char *buf,
84 switch (dispatch_policy) {
85 case NETISR_DISPATCH_DEFAULT:
88 case NETISR_DISPATCH_DEFERRED:
91 case NETISR_DISPATCH_HYBRID:
94 case NETISR_DISPATCH_DIRECT:
101 snprintf(buf, buflen, "%s", str);
105 netisr_load_kvm_uint(kvm_t *kd, char *name, u_int *p)
107 struct nlist nl[] = {
113 ret = kvm_nlist(kd, nl);
115 errx(-1, "%s: kvm_nlist(%s): %s", __func__, name,
118 errx(-1, "%s: kvm_nlist(%s): unresolved symbol", __func__,
120 if (kvm_read(kd, nl[0].n_value, p, sizeof(*p)) != sizeof(*p))
121 errx(-1, "%s: kvm_read(%s): %s", __func__, name,
126 * Load a nul-terminated string from KVM up to 'limit', guarantee that the
127 * string in local memory is nul-terminated.
130 netisr_load_kvm_string(kvm_t *kd, uintptr_t addr, char *dest, u_int limit)
134 for (i = 0; i < limit; i++) {
135 if (kvm_read(kd, addr + i, &dest[i], sizeof(dest[i])) !=
137 err(-1, "%s: kvm_read: %s", __func__,
142 dest[limit - 1] = '\0';
146 netisr_proto2name(u_int proto)
150 for (i = 0; i < proto_array_len; i++) {
151 if (proto_array[i].snp_proto == proto)
152 return (proto_array[i].snp_name);
158 netisr_protoispresent(u_int proto)
162 for (i = 0; i < proto_array_len; i++) {
163 if (proto_array[i].snp_proto == proto)
170 netisr_load_kvm_config(kvm_t *kd)
174 netisr_load_kvm_uint(kd, "_netisr_bindthreads", &bindthreads);
175 netisr_load_kvm_uint(kd, "_netisr_maxthreads", &maxthreads);
176 netisr_load_kvm_uint(kd, "_nws_count", &numthreads);
178 netisr_load_kvm_uint(kd, "_netisr_defaultqlimit", &defaultqlimit);
179 netisr_load_kvm_uint(kd, "_netisr_maxqlimit", &maxqlimit);
181 netisr_load_kvm_uint(kd, "_netisr_dispatch_policy", &tmp);
182 netisr_dispatch_policy_to_string(tmp, dispatch_policy,
183 sizeof(dispatch_policy));
187 netisr_load_sysctl_uint(const char *name, u_int *p)
191 retlen = sizeof(u_int);
192 if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
194 if (retlen != sizeof(u_int))
195 errx(-1, "%s: invalid len %ju", name, (uintmax_t)retlen);
199 netisr_load_sysctl_string(const char *name, char *p, size_t len)
204 if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
210 netisr_load_sysctl_config(void)
213 netisr_load_sysctl_uint("net.isr.bindthreads", &bindthreads);
214 netisr_load_sysctl_uint("net.isr.maxthreads", &maxthreads);
215 netisr_load_sysctl_uint("net.isr.numthreads", &numthreads);
217 netisr_load_sysctl_uint("net.isr.defaultqlimit", &defaultqlimit);
218 netisr_load_sysctl_uint("net.isr.maxqlimit", &maxqlimit);
220 netisr_load_sysctl_string("net.isr.dispatch", dispatch_policy,
221 sizeof(dispatch_policy));
225 netisr_load_kvm_proto(kvm_t *kd)
227 struct nlist nl[] = {
228 #define NLIST_NETISR_PROTO 0
229 { .n_name = "_netisr_proto" },
232 struct netisr_proto *np_array, *npp;
234 struct sysctl_netisr_proto *snpp;
239 * Kernel compile-time and user compile-time definitions of
240 * NETISR_MAXPROT must match, as we use that to size work arrays.
242 netisr_load_kvm_uint(kd, "_netisr_maxprot", &maxprot);
243 if (maxprot != NETISR_MAXPROT)
244 errx(-1, "%s: NETISR_MAXPROT mismatch", __func__);
245 len = maxprot * sizeof(*np_array);
246 np_array = malloc(len);
247 if (np_array == NULL)
248 err(-1, "%s: malloc", __func__);
249 ret = kvm_nlist(kd, nl);
251 errx(-1, "%s: kvm_nlist(_netisr_proto): %s", __func__,
254 errx(-1, "%s: kvm_nlist(_netisr_proto): unresolved symbol",
256 if (kvm_read(kd, nl[NLIST_NETISR_PROTO].n_value, np_array, len) !=
258 errx(-1, "%s: kvm_read(_netisr_proto): %s", __func__,
262 * Size and allocate memory to hold only live protocols.
265 for (i = 0; i < maxprot; i++) {
266 if (np_array[i].np_name == NULL)
270 proto_array = calloc(protocount, sizeof(*proto_array));
271 if (proto_array == NULL)
274 for (i = 0; i < maxprot; i++) {
276 if (npp->np_name == NULL)
278 snpp = &proto_array[protocount];
279 snpp->snp_version = sizeof(*snpp);
280 netisr_load_kvm_string(kd, (uintptr_t)npp->np_name,
281 snpp->snp_name, sizeof(snpp->snp_name));
283 snpp->snp_qlimit = npp->np_qlimit;
284 snpp->snp_policy = npp->np_policy;
285 snpp->snp_dispatch = npp->np_dispatch;
286 if (npp->np_m2flow != NULL)
287 snpp->snp_flags |= NETISR_SNP_FLAGS_M2FLOW;
288 if (npp->np_m2cpuid != NULL)
289 snpp->snp_flags |= NETISR_SNP_FLAGS_M2CPUID;
290 if (npp->np_drainedcpu != NULL)
291 snpp->snp_flags |= NETISR_SNP_FLAGS_DRAINEDCPU;
294 proto_array_len = protocount;
299 netisr_load_sysctl_proto(void)
303 if (sysctlbyname("net.isr.proto", NULL, &len, NULL, 0) < 0)
304 err(-1, "net.isr.proto: query len");
305 if (len % sizeof(*proto_array) != 0)
306 errx(-1, "net.isr.proto: invalid len");
307 proto_array = malloc(len);
308 if (proto_array == NULL)
310 if (sysctlbyname("net.isr.proto", proto_array, &len, NULL, 0) < 0)
311 err(-1, "net.isr.proto: query data");
312 if (len % sizeof(*proto_array) != 0)
313 errx(-1, "net.isr.proto: invalid len");
314 proto_array_len = len / sizeof(*proto_array);
315 if (proto_array_len < 1)
316 errx(-1, "net.isr.proto: no data");
317 if (proto_array[0].snp_version != sizeof(proto_array[0]))
318 errx(-1, "net.isr.proto: invalid version");
322 netisr_load_kvm_workstream(kvm_t *kd)
324 struct nlist nl[] = {
325 #define NLIST_NWS_ARRAY 0
326 { .n_name = "_nws_array" },
329 struct netisr_workstream nws;
330 struct sysctl_netisr_workstream *snwsp;
331 struct sysctl_netisr_work *snwp;
332 struct netisr_work *nwp;
333 struct nlist nl_nws[2];
334 u_int counter, cpuid, proto, wsid;
338 len = numthreads * sizeof(*nws_array);
339 nws_array = malloc(len);
340 if (nws_array == NULL)
342 ret = kvm_nlist(kd, nl);
344 errx(-1, "%s: kvm_nlist: %s", __func__, kvm_geterr(kd));
346 errx(-1, "%s: kvm_nlist: unresolved symbol", __func__);
347 if (kvm_read(kd, nl[NLIST_NWS_ARRAY].n_value, nws_array, len) !=
349 errx(-1, "%s: kvm_read(_nws_array): %s", __func__,
351 workstream_array = calloc(numthreads, sizeof(*workstream_array));
352 if (workstream_array == NULL)
354 workstream_array_len = numthreads;
355 work_array = calloc(numthreads * proto_array_len, sizeof(*work_array));
356 if (work_array == NULL)
359 for (wsid = 0; wsid < numthreads; wsid++) {
360 cpuid = nws_array[wsid];
361 if (kvm_dpcpu_setcpu(kd, cpuid) < 0)
362 errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
363 cpuid, kvm_geterr(kd));
364 bzero(nl_nws, sizeof(nl_nws));
365 nl_nws[0].n_name = "_nws";
366 ret = kvm_nlist(kd, nl_nws);
368 errx(-1, "%s: kvm_nlist looking up nws on CPU %u: %s",
369 __func__, cpuid, kvm_geterr(kd));
371 errx(-1, "%s: kvm_nlist(nws): unresolved symbol on "
372 "CPU %u", __func__, cpuid);
373 if (kvm_read(kd, nl_nws[0].n_value, &nws, sizeof(nws)) !=
375 errx(-1, "%s: kvm_read(nw): %s", __func__,
377 snwsp = &workstream_array[wsid];
378 snwsp->snws_version = sizeof(*snwsp);
379 snwsp->snws_wsid = cpuid;
380 snwsp->snws_cpu = cpuid;
381 if (nws.nws_intr_event != NULL)
382 snwsp->snws_flags |= NETISR_SNWS_FLAGS_INTR;
385 * Extract the CPU's per-protocol work information.
387 printf("counting to maxprot: %u\n", maxprot);
388 for (proto = 0; proto < maxprot; proto++) {
389 if (!netisr_protoispresent(proto))
391 nwp = &nws.nws_work[proto];
392 snwp = &work_array[counter];
393 snwp->snw_version = sizeof(*snwp);
394 snwp->snw_wsid = cpuid;
395 snwp->snw_proto = proto;
396 snwp->snw_len = nwp->nw_len;
397 snwp->snw_watermark = nwp->nw_watermark;
398 snwp->snw_dispatched = nwp->nw_dispatched;
399 snwp->snw_hybrid_dispatched =
400 nwp->nw_hybrid_dispatched;
401 snwp->snw_qdrops = nwp->nw_qdrops;
402 snwp->snw_queued = nwp->nw_queued;
403 snwp->snw_handled = nwp->nw_handled;
407 work_array_len = counter;
411 netisr_load_sysctl_workstream(void)
415 if (sysctlbyname("net.isr.workstream", NULL, &len, NULL, 0) < 0)
416 err(-1, "net.isr.workstream: query len");
417 if (len % sizeof(*workstream_array) != 0)
418 errx(-1, "net.isr.workstream: invalid len");
419 workstream_array = malloc(len);
420 if (workstream_array == NULL)
422 if (sysctlbyname("net.isr.workstream", workstream_array, &len, NULL,
424 err(-1, "net.isr.workstream: query data");
425 if (len % sizeof(*workstream_array) != 0)
426 errx(-1, "net.isr.workstream: invalid len");
427 workstream_array_len = len / sizeof(*workstream_array);
428 if (workstream_array_len < 1)
429 errx(-1, "net.isr.workstream: no data");
430 if (workstream_array[0].snws_version != sizeof(workstream_array[0]))
431 errx(-1, "net.isr.workstream: invalid version");
435 netisr_load_sysctl_work(void)
439 if (sysctlbyname("net.isr.work", NULL, &len, NULL, 0) < 0)
440 err(-1, "net.isr.work: query len");
441 if (len % sizeof(*work_array) != 0)
442 errx(-1, "net.isr.work: invalid len");
443 work_array = malloc(len);
444 if (work_array == NULL)
446 if (sysctlbyname("net.isr.work", work_array, &len, NULL, 0) < 0)
447 err(-1, "net.isr.work: query data");
448 if (len % sizeof(*work_array) != 0)
449 errx(-1, "net.isr.work: invalid len");
450 work_array_len = len / sizeof(*work_array);
451 if (work_array_len < 1)
452 errx(-1, "net.isr.work: no data");
453 if (work_array[0].snw_version != sizeof(work_array[0]))
454 errx(-1, "net.isr.work: invalid version");
458 netisr_print_proto(struct sysctl_netisr_proto *snpp)
462 printf("%-6s", snpp->snp_name);
463 printf(" %5u", snpp->snp_proto);
464 printf(" %6u", snpp->snp_qlimit);
466 (snpp->snp_policy == NETISR_POLICY_SOURCE) ? "source" :
467 (snpp->snp_policy == NETISR_POLICY_FLOW) ? "flow" :
468 (snpp->snp_policy == NETISR_POLICY_CPU) ? "cpu" : "-");
469 netisr_dispatch_policy_to_string(snpp->snp_dispatch, tmp,
473 (snpp->snp_flags & NETISR_SNP_FLAGS_M2CPUID) ? "C" : "-",
474 (snpp->snp_flags & NETISR_SNP_FLAGS_DRAINEDCPU) ? "D" : "-",
475 (snpp->snp_flags & NETISR_SNP_FLAGS_M2FLOW) ? "F" : "-");
479 netisr_print_workstream(struct sysctl_netisr_workstream *snwsp)
481 struct sysctl_netisr_work *snwp;
484 for (i = 0; i < work_array_len; i++) {
485 snwp = &work_array[i];
486 if (snwp->snw_wsid != snwsp->snws_wsid)
488 printf("%4u ", snwsp->snws_wsid);
489 printf("%3u ", snwsp->snws_cpu);
491 printf("%-6s", netisr_proto2name(snwp->snw_proto));
492 printf(" %5u", snwp->snw_len);
493 printf(" %5u", snwp->snw_watermark);
494 printf(" %8ju", snwp->snw_dispatched);
495 printf(" %8ju", snwp->snw_hybrid_dispatched);
496 printf(" %8ju", snwp->snw_qdrops);
497 printf(" %8ju", snwp->snw_queued);
498 printf(" %8ju", snwp->snw_handled);
504 netisr_stats(void *kvmd)
506 struct sysctl_netisr_workstream *snwsp;
507 struct sysctl_netisr_proto *snpp;
512 netisr_load_sysctl_config();
513 netisr_load_sysctl_proto();
514 netisr_load_sysctl_workstream();
515 netisr_load_sysctl_work();
518 errx(-1, "netisr_stats: !live but !kd");
519 netisr_load_kvm_config(kd);
520 netisr_load_kvm_proto(kd);
521 netisr_load_kvm_workstream(kd); /* Also does work. */
524 printf("Configuration:\n");
525 printf("%-25s %12s %12s\n", "Setting", "Current", "Limit");
526 printf("%-25s %12u %12u\n", "Thread count", numthreads, maxthreads);
527 printf("%-25s %12u %12u\n", "Default queue limit", defaultqlimit,
529 printf("%-25s %12s %12s\n", "Dispatch policy", dispatch_policy,
531 printf("%-25s %12s %12s\n", "Threads bound to CPUs",
532 bindthreads ? "enabled" : "disabled", "n/a");
535 printf("Protocols:\n");
536 printf("%-6s %5s %6s %-6s %-8s %-5s\n", "Name", "Proto", "QLimit",
537 "Policy", "Dispatch", "Flags");
538 for (i = 0; i < proto_array_len; i++) {
539 snpp = &proto_array[i];
540 netisr_print_proto(snpp);
544 printf("Workstreams:\n");
545 printf("%4s %3s ", "WSID", "CPU");
547 printf("%-6s %5s %5s %8s %8s %8s %8s %8s\n", "Name", "Len", "WMark",
548 "Disp'd", "HDisp'd", "QDrops", "Queued", "Handled");
549 for (i = 0; i < workstream_array_len; i++) {
550 snwsp = &workstream_array[i];
551 netisr_print_workstream(snwsp);