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>
55 * Print statistics for the kernel netisr subsystem.
57 static u_int bindthreads;
58 static u_int maxthreads;
59 static u_int numthreads;
61 static u_int defaultqlimit;
62 static u_int maxqlimit;
64 static char dispatch_policy[20];
66 static struct sysctl_netisr_proto *proto_array;
67 static u_int proto_array_len;
69 static struct sysctl_netisr_workstream *workstream_array;
70 static u_int workstream_array_len;
72 static struct sysctl_netisr_work *work_array;
73 static u_int work_array_len;
75 static u_int *nws_array;
80 netisr_dispatch_policy_to_string(u_int policy, char *buf,
86 case NETISR_DISPATCH_DEFAULT:
89 case NETISR_DISPATCH_DEFERRED:
92 case NETISR_DISPATCH_HYBRID:
95 case NETISR_DISPATCH_DIRECT:
102 snprintf(buf, buflen, "%s", str);
106 netisr_load_kvm_uint(kvm_t *kd, const char *name, u_int *p)
108 struct nlist nl[] = {
114 ret = kvm_nlist(kd, nl);
116 xo_errx(-1, "%s: kvm_nlist(%s): %s", __func__, name,
119 xo_errx(-1, "%s: kvm_nlist(%s): unresolved symbol", __func__,
121 if (kvm_read(kd, nl[0].n_value, p, sizeof(*p)) != sizeof(*p))
122 xo_errx(-1, "%s: kvm_read(%s): %s", __func__, name,
127 * Load a nul-terminated string from KVM up to 'limit', guarantee that the
128 * string in local memory is nul-terminated.
131 netisr_load_kvm_string(kvm_t *kd, uintptr_t addr, char *dest, u_int limit)
135 for (i = 0; i < limit; i++) {
136 if (kvm_read(kd, addr + i, &dest[i], sizeof(dest[i])) !=
138 xo_err(-1, "%s: kvm_read: %s", __func__,
143 dest[limit - 1] = '\0';
147 netisr_proto2name(u_int proto)
151 for (i = 0; i < proto_array_len; i++) {
152 if (proto_array[i].snp_proto == proto)
153 return (proto_array[i].snp_name);
159 netisr_protoispresent(u_int proto)
163 for (i = 0; i < proto_array_len; i++) {
164 if (proto_array[i].snp_proto == proto)
171 netisr_load_kvm_config(kvm_t *kd)
175 netisr_load_kvm_uint(kd, "_netisr_bindthreads", &bindthreads);
176 netisr_load_kvm_uint(kd, "_netisr_maxthreads", &maxthreads);
177 netisr_load_kvm_uint(kd, "_nws_count", &numthreads);
179 netisr_load_kvm_uint(kd, "_netisr_defaultqlimit", &defaultqlimit);
180 netisr_load_kvm_uint(kd, "_netisr_maxqlimit", &maxqlimit);
182 netisr_load_kvm_uint(kd, "_netisr_dispatch_policy", &tmp);
183 netisr_dispatch_policy_to_string(tmp, dispatch_policy,
184 sizeof(dispatch_policy));
188 netisr_load_sysctl_uint(const char *name, u_int *p)
192 retlen = sizeof(u_int);
193 if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
194 xo_err(-1, "%s", name);
195 if (retlen != sizeof(u_int))
196 xo_errx(-1, "%s: invalid len %ju", name, (uintmax_t)retlen);
200 netisr_load_sysctl_string(const char *name, char *p, size_t len)
205 if (sysctlbyname(name, p, &retlen, NULL, 0) < 0)
206 xo_err(-1, "%s", name);
211 netisr_load_sysctl_config(void)
214 netisr_load_sysctl_uint("net.isr.bindthreads", &bindthreads);
215 netisr_load_sysctl_uint("net.isr.maxthreads", &maxthreads);
216 netisr_load_sysctl_uint("net.isr.numthreads", &numthreads);
218 netisr_load_sysctl_uint("net.isr.defaultqlimit", &defaultqlimit);
219 netisr_load_sysctl_uint("net.isr.maxqlimit", &maxqlimit);
221 netisr_load_sysctl_string("net.isr.dispatch", dispatch_policy,
222 sizeof(dispatch_policy));
226 netisr_load_kvm_proto(kvm_t *kd)
228 struct nlist nl[] = {
229 #define NLIST_NETISR_PROTO 0
230 { .n_name = "_netisr_proto" },
233 struct netisr_proto *np_array, *npp;
235 struct sysctl_netisr_proto *snpp;
240 * Kernel compile-time and user compile-time definitions of
241 * NETISR_MAXPROT must match, as we use that to size work arrays.
243 netisr_load_kvm_uint(kd, "_netisr_maxprot", &maxprot);
244 if (maxprot != NETISR_MAXPROT)
245 xo_errx(-1, "%s: NETISR_MAXPROT mismatch", __func__);
246 len = maxprot * sizeof(*np_array);
247 np_array = malloc(len);
248 if (np_array == NULL)
249 xo_err(-1, "%s: malloc", __func__);
250 ret = kvm_nlist(kd, nl);
252 xo_errx(-1, "%s: kvm_nlist(_netisr_proto): %s", __func__,
255 xo_errx(-1, "%s: kvm_nlist(_netisr_proto): unresolved symbol",
257 if (kvm_read(kd, nl[NLIST_NETISR_PROTO].n_value, np_array, len) !=
259 xo_errx(-1, "%s: kvm_read(_netisr_proto): %s", __func__,
263 * Size and allocate memory to hold only live protocols.
266 for (i = 0; i < maxprot; i++) {
267 if (np_array[i].np_name == NULL)
271 proto_array = calloc(protocount, sizeof(*proto_array));
272 if (proto_array == NULL)
275 for (i = 0; i < maxprot; i++) {
277 if (npp->np_name == NULL)
279 snpp = &proto_array[protocount];
280 snpp->snp_version = sizeof(*snpp);
281 netisr_load_kvm_string(kd, (uintptr_t)npp->np_name,
282 snpp->snp_name, sizeof(snpp->snp_name));
284 snpp->snp_qlimit = npp->np_qlimit;
285 snpp->snp_policy = npp->np_policy;
286 snpp->snp_dispatch = npp->np_dispatch;
287 if (npp->np_m2flow != NULL)
288 snpp->snp_flags |= NETISR_SNP_FLAGS_M2FLOW;
289 if (npp->np_m2cpuid != NULL)
290 snpp->snp_flags |= NETISR_SNP_FLAGS_M2CPUID;
291 if (npp->np_drainedcpu != NULL)
292 snpp->snp_flags |= NETISR_SNP_FLAGS_DRAINEDCPU;
295 proto_array_len = protocount;
300 netisr_load_sysctl_proto(void)
304 if (sysctlbyname("net.isr.proto", NULL, &len, NULL, 0) < 0)
305 xo_err(-1, "net.isr.proto: query len");
306 if (len % sizeof(*proto_array) != 0)
307 xo_errx(-1, "net.isr.proto: invalid len");
308 proto_array = malloc(len);
309 if (proto_array == NULL)
310 xo_err(-1, "malloc");
311 if (sysctlbyname("net.isr.proto", proto_array, &len, NULL, 0) < 0)
312 xo_err(-1, "net.isr.proto: query data");
313 if (len % sizeof(*proto_array) != 0)
314 xo_errx(-1, "net.isr.proto: invalid len");
315 proto_array_len = len / sizeof(*proto_array);
316 if (proto_array_len < 1)
317 xo_errx(-1, "net.isr.proto: no data");
318 if (proto_array[0].snp_version != sizeof(proto_array[0]))
319 xo_errx(-1, "net.isr.proto: invalid version");
323 netisr_load_kvm_workstream(kvm_t *kd)
325 struct nlist nl[] = {
326 #define NLIST_NWS_ARRAY 0
327 { .n_name = "_nws_array" },
330 struct netisr_workstream nws;
331 struct sysctl_netisr_workstream *snwsp;
332 struct sysctl_netisr_work *snwp;
333 struct netisr_work *nwp;
334 struct nlist nl_nws[2];
335 u_int counter, cpuid, proto, wsid;
339 len = numthreads * sizeof(*nws_array);
340 nws_array = malloc(len);
341 if (nws_array == NULL)
342 xo_err(-1, "malloc");
343 ret = kvm_nlist(kd, nl);
345 xo_errx(-1, "%s: kvm_nlist: %s", __func__, kvm_geterr(kd));
347 xo_errx(-1, "%s: kvm_nlist: unresolved symbol", __func__);
348 if (kvm_read(kd, nl[NLIST_NWS_ARRAY].n_value, nws_array, len) !=
350 xo_errx(-1, "%s: kvm_read(_nws_array): %s", __func__,
352 workstream_array = calloc(numthreads, sizeof(*workstream_array));
353 if (workstream_array == NULL)
354 xo_err(-1, "calloc");
355 workstream_array_len = numthreads;
356 work_array = calloc(numthreads * proto_array_len, sizeof(*work_array));
357 if (work_array == NULL)
358 xo_err(-1, "calloc");
360 for (wsid = 0; wsid < numthreads; wsid++) {
361 cpuid = nws_array[wsid];
362 if (kvm_dpcpu_setcpu(kd, cpuid) < 0)
363 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
364 cpuid, kvm_geterr(kd));
365 bzero(nl_nws, sizeof(nl_nws));
366 nl_nws[0].n_name = "_nws";
367 ret = kvm_nlist(kd, nl_nws);
369 xo_errx(-1, "%s: kvm_nlist looking up nws on CPU "
370 "%u: %s", __func__, cpuid, kvm_geterr(kd));
372 xo_errx(-1, "%s: kvm_nlist(nws): unresolved symbol on "
373 "CPU %u", __func__, cpuid);
374 if (kvm_read(kd, nl_nws[0].n_value, &nws, sizeof(nws)) !=
376 xo_errx(-1, "%s: kvm_read(nw): %s", __func__,
378 snwsp = &workstream_array[wsid];
379 snwsp->snws_version = sizeof(*snwsp);
380 snwsp->snws_wsid = cpuid;
381 snwsp->snws_cpu = cpuid;
382 if (nws.nws_intr_event != NULL)
383 snwsp->snws_flags |= NETISR_SNWS_FLAGS_INTR;
386 * Extract the CPU's per-protocol work information.
388 xo_emit("counting to maxprot: {:maxprot/%u}\n", maxprot);
389 for (proto = 0; proto < maxprot; proto++) {
390 if (!netisr_protoispresent(proto))
392 nwp = &nws.nws_work[proto];
393 snwp = &work_array[counter];
394 snwp->snw_version = sizeof(*snwp);
395 snwp->snw_wsid = cpuid;
396 snwp->snw_proto = proto;
397 snwp->snw_len = nwp->nw_len;
398 snwp->snw_watermark = nwp->nw_watermark;
399 snwp->snw_dispatched = nwp->nw_dispatched;
400 snwp->snw_hybrid_dispatched =
401 nwp->nw_hybrid_dispatched;
402 snwp->snw_qdrops = nwp->nw_qdrops;
403 snwp->snw_queued = nwp->nw_queued;
404 snwp->snw_handled = nwp->nw_handled;
408 work_array_len = counter;
412 netisr_load_sysctl_workstream(void)
416 if (sysctlbyname("net.isr.workstream", NULL, &len, NULL, 0) < 0)
417 xo_err(-1, "net.isr.workstream: query len");
418 if (len % sizeof(*workstream_array) != 0)
419 xo_errx(-1, "net.isr.workstream: invalid len");
420 workstream_array = malloc(len);
421 if (workstream_array == NULL)
422 xo_err(-1, "malloc");
423 if (sysctlbyname("net.isr.workstream", workstream_array, &len, NULL,
425 xo_err(-1, "net.isr.workstream: query data");
426 if (len % sizeof(*workstream_array) != 0)
427 xo_errx(-1, "net.isr.workstream: invalid len");
428 workstream_array_len = len / sizeof(*workstream_array);
429 if (workstream_array_len < 1)
430 xo_errx(-1, "net.isr.workstream: no data");
431 if (workstream_array[0].snws_version != sizeof(workstream_array[0]))
432 xo_errx(-1, "net.isr.workstream: invalid version");
436 netisr_load_sysctl_work(void)
440 if (sysctlbyname("net.isr.work", NULL, &len, NULL, 0) < 0)
441 xo_err(-1, "net.isr.work: query len");
442 if (len % sizeof(*work_array) != 0)
443 xo_errx(-1, "net.isr.work: invalid len");
444 work_array = malloc(len);
445 if (work_array == NULL)
446 xo_err(-1, "malloc");
447 if (sysctlbyname("net.isr.work", work_array, &len, NULL, 0) < 0)
448 xo_err(-1, "net.isr.work: query data");
449 if (len % sizeof(*work_array) != 0)
450 xo_errx(-1, "net.isr.work: invalid len");
451 work_array_len = len / sizeof(*work_array);
452 if (work_array_len < 1)
453 xo_errx(-1, "net.isr.work: no data");
454 if (work_array[0].snw_version != sizeof(work_array[0]))
455 xo_errx(-1, "net.isr.work: invalid version");
459 netisr_print_proto(struct sysctl_netisr_proto *snpp)
463 xo_emit("{[:-6}{k:name/%s}{]:}", snpp->snp_name);
464 xo_emit(" {:protocol/%5u}", snpp->snp_proto);
465 xo_emit(" {:queue-limit/%6u}", snpp->snp_qlimit);
466 xo_emit(" {:policy-type/%6s}",
467 (snpp->snp_policy == NETISR_POLICY_SOURCE) ? "source" :
468 (snpp->snp_policy == NETISR_POLICY_FLOW) ? "flow" :
469 (snpp->snp_policy == NETISR_POLICY_CPU) ? "cpu" : "-");
470 netisr_dispatch_policy_to_string(snpp->snp_dispatch, tmp,
472 xo_emit(" {:policy/%8s}", tmp);
473 xo_emit(" {:flags/%s%s%s}\n",
474 (snpp->snp_flags & NETISR_SNP_FLAGS_M2CPUID) ? "C" : "-",
475 (snpp->snp_flags & NETISR_SNP_FLAGS_DRAINEDCPU) ? "D" : "-",
476 (snpp->snp_flags & NETISR_SNP_FLAGS_M2FLOW) ? "F" : "-");
480 netisr_print_workstream(struct sysctl_netisr_workstream *snwsp)
482 struct sysctl_netisr_work *snwp;
485 xo_open_list("work");
486 for (i = 0; i < work_array_len; i++) {
487 snwp = &work_array[i];
488 if (snwp->snw_wsid != snwsp->snws_wsid)
490 xo_open_instance("work");
491 xo_emit("{t:workstream/%4u} ", snwsp->snws_wsid);
492 xo_emit("{t:cpu/%3u} ", snwsp->snws_cpu);
494 xo_emit("{t:name/%-6s}", netisr_proto2name(snwp->snw_proto));
495 xo_emit(" {t:length/%5u}", snwp->snw_len);
496 xo_emit(" {t:watermark/%5u}", snwp->snw_watermark);
497 xo_emit(" {t:dispatched/%8ju}", snwp->snw_dispatched);
498 xo_emit(" {t:hybrid-dispatched/%8ju}",
499 snwp->snw_hybrid_dispatched);
500 xo_emit(" {t:queue-drops/%8ju}", snwp->snw_qdrops);
501 xo_emit(" {t:queued/%8ju}", snwp->snw_queued);
502 xo_emit(" {t:handled/%8ju}", snwp->snw_handled);
504 xo_close_instance("work");
506 xo_close_list("work");
510 netisr_stats(void *kvmd)
512 struct sysctl_netisr_workstream *snwsp;
513 struct sysctl_netisr_proto *snpp;
518 netisr_load_sysctl_config();
519 netisr_load_sysctl_proto();
520 netisr_load_sysctl_workstream();
521 netisr_load_sysctl_work();
524 xo_errx(-1, "netisr_stats: !live but !kd");
525 netisr_load_kvm_config(kd);
526 netisr_load_kvm_proto(kd);
527 netisr_load_kvm_workstream(kd); /* Also does work. */
530 xo_open_container("netisr");
532 xo_emit("{T:Configuration}:\n");
533 xo_emit("{T:/%-25s} {T:/%12s} {T:/%12s}\n",
534 "Setting", "Current", "Limit");
535 xo_emit("{T:/%-25s} {T:/%12u} {T:/%12u}\n",
536 "Thread count", numthreads, maxthreads);
537 xo_emit("{T:/%-25s} {T:/%12u} {T:/%12u}\n",
538 "Default queue limit", defaultqlimit, maxqlimit);
539 xo_emit("{T:/%-25s} {T:/%12s} {T:/%12s}\n",
540 "Dispatch policy", dispatch_policy, "n/a");
541 xo_emit("{T:/%-25s} {T:/%12s} {T:/%12s}\n",
542 "Threads bound to CPUs", bindthreads ? "enabled" : "disabled",
546 xo_emit("{T:Protocols}:\n");
547 xo_emit("{T:/%-6s} {T:/%5s} {T:/%6s} {T:/%-6s} {T:/%-8s} {T:/%-5s}\n",
548 "Name", "Proto", "QLimit", "Policy", "Dispatch", "Flags");
549 xo_open_list("protocol");
550 for (i = 0; i < proto_array_len; i++) {
551 xo_open_instance("protocol");
552 snpp = &proto_array[i];
553 netisr_print_proto(snpp);
554 xo_close_instance("protocol");
556 xo_close_list("protocol");
559 xo_emit("{T:Workstreams}:\n");
560 xo_emit("{T:/%4s} {T:/%3s} ", "WSID", "CPU");
561 xo_emit("{P:/%2s}", "");
562 xo_emit("{T:/%-6s} {T:/%5s} {T:/%5s} {T:/%8s} {T:/%8s} {T:/%8s} "
563 "{T:/%8s} {T:/%8s}\n",
564 "Name", "Len", "WMark", "Disp'd", "HDisp'd", "QDrops", "Queued",
566 xo_open_list("workstream");
567 for (i = 0; i < workstream_array_len; i++) {
568 xo_open_instance("workstream");
569 snwsp = &workstream_array[i];
570 netisr_print_workstream(snwsp);
571 xo_close_instance("workstream");
573 xo_close_list("workstream");
574 xo_close_container("netisr");