2 * Copyright (c) 2007-2009 Robert N. M. Watson
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * This software was developed by Robert N. M. Watson under contract
7 * to Juniper Networks, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * netisr is a packet dispatch service, allowing synchronous (directly
36 * dispatched) and asynchronous (deferred dispatch) processing of packets by
37 * registered protocol handlers. Callers pass a protocol identifier and
38 * packet to netisr, along with a direct dispatch hint, and work will either
39 * be immediately processed by the registered handler, or passed to a
40 * software interrupt (SWI) thread for deferred dispatch. Callers will
41 * generally select one or the other based on:
43 * - Whether directly dispatching a netisr handler lead to code reentrance or
44 * lock recursion, such as entering the socket code from the socket code.
45 * - Whether directly dispatching a netisr handler lead to recursive
46 * processing, such as when decapsulating several wrapped layers of tunnel
47 * information (IPSEC within IPSEC within ...).
49 * Maintaining ordering for protocol streams is a critical design concern.
50 * Enforcing ordering limits the opportunity for concurrency, but maintains
51 * the strong ordering requirements found in some protocols, such as TCP. Of
52 * related concern is CPU affinity--it is desirable to process all data
53 * associated with a particular stream on the same CPU over time in order to
54 * avoid acquiring locks associated with the connection on different CPUs,
55 * keep connection data in one cache, and to generally encourage associated
56 * user threads to live on the same CPU as the stream. It's also desirable
57 * to avoid lock migration and contention where locks are associated with
60 * netisr supports several policy variations, represented by the
61 * NETISR_POLICY_* constants, allowing protocols to play various roles in
62 * identifying flows, assigning work to CPUs, etc. These are described in
67 #include "opt_device_polling.h"
69 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/kthread.h>
73 #include <sys/interrupt.h>
76 #include <sys/mutex.h>
79 #include <sys/rmlock.h>
80 #include <sys/sched.h>
82 #include <sys/socket.h>
83 #include <sys/sysctl.h>
84 #include <sys/systm.h>
90 #define _WANT_NETISR_INTERNAL /* Enable definitions from netisr_internal.h */
92 #include <net/if_var.h>
93 #include <net/netisr.h>
94 #include <net/netisr_internal.h>
98 * Synchronize use and modification of the registered netisr data structures;
99 * acquire a read lock while modifying the set of registered protocols to
100 * prevent partially registered or unregistered protocols from being run.
102 * The following data structures and fields are protected by this lock:
104 * - The netisr_proto array, including all fields of struct netisr_proto.
105 * - The nws array, including all fields of struct netisr_worker.
106 * - The nws_array array.
108 * Note: the NETISR_LOCKING define controls whether read locks are acquired
109 * in packet processing paths requiring netisr registration stability. This
110 * is disabled by default as it can lead to measurable performance
111 * degradation even with rmlocks (3%-6% for loopback ping-pong traffic), and
112 * because netisr registration and unregistration is extremely rare at
113 * runtime. If it becomes more common, this decision should be revisited.
115 * XXXRW: rmlocks don't support assertions.
117 static struct rmlock netisr_rmlock;
118 #define NETISR_LOCK_INIT() rm_init_flags(&netisr_rmlock, "netisr", \
120 #define NETISR_LOCK_ASSERT()
121 #define NETISR_RLOCK(tracker) rm_rlock(&netisr_rmlock, (tracker))
122 #define NETISR_RUNLOCK(tracker) rm_runlock(&netisr_rmlock, (tracker))
123 #define NETISR_WLOCK() rm_wlock(&netisr_rmlock)
124 #define NETISR_WUNLOCK() rm_wunlock(&netisr_rmlock)
125 /* #define NETISR_LOCKING */
127 static SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW, 0, "netisr");
130 * Three global direct dispatch policies are supported:
132 * NETISR_DISPATCH_DEFERRED: All work is deferred for a netisr, regardless of
133 * context (may be overriden by protocols).
135 * NETISR_DISPATCH_HYBRID: If the executing context allows direct dispatch,
136 * and we're running on the CPU the work would be performed on, then direct
137 * dispatch it if it wouldn't violate ordering constraints on the workstream.
139 * NETISR_DISPATCH_DIRECT: If the executing context allows direct dispatch,
140 * always direct dispatch. (The default.)
142 * Notice that changing the global policy could lead to short periods of
143 * misordered processing, but this is considered acceptable as compared to
144 * the complexity of enforcing ordering during policy changes. Protocols can
145 * override the global policy (when they're not doing that, they select
146 * NETISR_DISPATCH_DEFAULT).
148 #define NETISR_DISPATCH_POLICY_DEFAULT NETISR_DISPATCH_DIRECT
149 #define NETISR_DISPATCH_POLICY_MAXSTR 20 /* Used for temporary buffers. */
150 static u_int netisr_dispatch_policy = NETISR_DISPATCH_POLICY_DEFAULT;
151 static int sysctl_netisr_dispatch_policy(SYSCTL_HANDLER_ARGS);
152 SYSCTL_PROC(_net_isr, OID_AUTO, dispatch, CTLTYPE_STRING | CTLFLAG_RWTUN,
153 0, 0, sysctl_netisr_dispatch_policy, "A",
154 "netisr dispatch policy");
157 * Allow the administrator to limit the number of threads (CPUs) to use for
158 * netisr. We don't check netisr_maxthreads before creating the thread for
159 * CPU 0, so in practice we ignore values <= 1. This must be set at boot.
160 * We will create at most one thread per CPU.
162 static int netisr_maxthreads = -1; /* Max number of threads. */
163 SYSCTL_INT(_net_isr, OID_AUTO, maxthreads, CTLFLAG_RDTUN,
164 &netisr_maxthreads, 0,
165 "Use at most this many CPUs for netisr processing");
167 static int netisr_bindthreads = 0; /* Bind threads to CPUs. */
168 SYSCTL_INT(_net_isr, OID_AUTO, bindthreads, CTLFLAG_RDTUN,
169 &netisr_bindthreads, 0, "Bind netisr threads to CPUs.");
172 * Limit per-workstream mbuf queue limits s to at most net.isr.maxqlimit,
173 * both for initial configuration and later modification using
174 * netisr_setqlimit().
176 #define NETISR_DEFAULT_MAXQLIMIT 10240
177 static u_int netisr_maxqlimit = NETISR_DEFAULT_MAXQLIMIT;
178 SYSCTL_UINT(_net_isr, OID_AUTO, maxqlimit, CTLFLAG_RDTUN,
179 &netisr_maxqlimit, 0,
180 "Maximum netisr per-protocol, per-CPU queue depth.");
183 * The default per-workstream mbuf queue limit for protocols that don't
184 * initialize the nh_qlimit field of their struct netisr_handler. If this is
185 * set above netisr_maxqlimit, we truncate it to the maximum during boot.
187 #define NETISR_DEFAULT_DEFAULTQLIMIT 256
188 static u_int netisr_defaultqlimit = NETISR_DEFAULT_DEFAULTQLIMIT;
189 SYSCTL_UINT(_net_isr, OID_AUTO, defaultqlimit, CTLFLAG_RDTUN,
190 &netisr_defaultqlimit, 0,
191 "Default netisr per-protocol, per-CPU queue limit if not set by protocol");
194 * Store and export the compile-time constant NETISR_MAXPROT limit on the
195 * number of protocols that can register with netisr at a time. This is
196 * required for crashdump analysis, as it sizes netisr_proto[].
198 static u_int netisr_maxprot = NETISR_MAXPROT;
199 SYSCTL_UINT(_net_isr, OID_AUTO, maxprot, CTLFLAG_RD,
201 "Compile-time limit on the number of protocols supported by netisr.");
204 * The netisr_proto array describes all registered protocols, indexed by
205 * protocol number. See netisr_internal.h for more details.
207 static struct netisr_proto netisr_proto[NETISR_MAXPROT];
210 * Per-CPU workstream data. See netisr_internal.h for more details.
212 DPCPU_DEFINE(struct netisr_workstream, nws);
215 * Map contiguous values between 0 and nws_count into CPU IDs appropriate for
216 * accessing workstreams. This allows constructions of the form
217 * DPCPU_ID_GET(nws_array[arbitraryvalue % nws_count], nws).
219 static u_int nws_array[MAXCPU];
222 * Number of registered workstreams. Will be at most the number of running
223 * CPUs once fully started.
225 static u_int nws_count;
226 SYSCTL_UINT(_net_isr, OID_AUTO, numthreads, CTLFLAG_RD,
227 &nws_count, 0, "Number of extant netisr threads.");
230 * Synchronization for each workstream: a mutex protects all mutable fields
231 * in each stream, including per-protocol state (mbuf queues). The SWI is
232 * woken up if asynchronous dispatch is required.
234 #define NWS_LOCK(s) mtx_lock(&(s)->nws_mtx)
235 #define NWS_LOCK_ASSERT(s) mtx_assert(&(s)->nws_mtx, MA_OWNED)
236 #define NWS_UNLOCK(s) mtx_unlock(&(s)->nws_mtx)
237 #define NWS_SIGNAL(s) swi_sched((s)->nws_swi_cookie, 0)
240 * Utility routines for protocols that implement their own mapping of flows
244 netisr_get_cpucount(void)
251 netisr_get_cpuid(u_int cpunumber)
254 KASSERT(cpunumber < nws_count, ("%s: %u > %u", __func__, cpunumber,
257 return (nws_array[cpunumber]);
261 * The default implementation of flow -> CPU ID mapping.
263 * Non-static so that protocols can use it to map their own work to specific
264 * CPUs in a manner consistent to netisr for affinity purposes.
267 netisr_default_flow2cpu(u_int flowid)
270 return (nws_array[flowid % nws_count]);
274 * Dispatch tunable and sysctl configuration.
276 struct netisr_dispatch_table_entry {
278 const char *ndte_policy_str;
280 static const struct netisr_dispatch_table_entry netisr_dispatch_table[] = {
281 { NETISR_DISPATCH_DEFAULT, "default" },
282 { NETISR_DISPATCH_DEFERRED, "deferred" },
283 { NETISR_DISPATCH_HYBRID, "hybrid" },
284 { NETISR_DISPATCH_DIRECT, "direct" },
286 static const u_int netisr_dispatch_table_len =
287 (sizeof(netisr_dispatch_table) / sizeof(netisr_dispatch_table[0]));
290 netisr_dispatch_policy_to_str(u_int dispatch_policy, char *buffer,
293 const struct netisr_dispatch_table_entry *ndtep;
298 for (i = 0; i < netisr_dispatch_table_len; i++) {
299 ndtep = &netisr_dispatch_table[i];
300 if (ndtep->ndte_policy == dispatch_policy) {
301 str = ndtep->ndte_policy_str;
305 snprintf(buffer, buflen, "%s", str);
309 netisr_dispatch_policy_from_str(const char *str, u_int *dispatch_policyp)
311 const struct netisr_dispatch_table_entry *ndtep;
314 for (i = 0; i < netisr_dispatch_table_len; i++) {
315 ndtep = &netisr_dispatch_table[i];
316 if (strcmp(ndtep->ndte_policy_str, str) == 0) {
317 *dispatch_policyp = ndtep->ndte_policy;
325 sysctl_netisr_dispatch_policy(SYSCTL_HANDLER_ARGS)
327 char tmp[NETISR_DISPATCH_POLICY_MAXSTR];
328 u_int dispatch_policy;
331 netisr_dispatch_policy_to_str(netisr_dispatch_policy, tmp,
333 error = sysctl_handle_string(oidp, tmp, sizeof(tmp), req);
334 if (error == 0 && req->newptr != NULL) {
335 error = netisr_dispatch_policy_from_str(tmp,
337 if (error == 0 && dispatch_policy == NETISR_DISPATCH_DEFAULT)
340 netisr_dispatch_policy = dispatch_policy;
346 * Register a new netisr handler, which requires initializing per-protocol
347 * fields for each workstream. All netisr work is briefly suspended while
348 * the protocol is installed.
351 netisr_register(const struct netisr_handler *nhp)
353 struct netisr_work *npwp;
357 proto = nhp->nh_proto;
361 * Test that the requested registration is valid.
363 KASSERT(nhp->nh_name != NULL,
364 ("%s: nh_name NULL for %u", __func__, proto));
365 KASSERT(nhp->nh_handler != NULL,
366 ("%s: nh_handler NULL for %s", __func__, name));
367 KASSERT(nhp->nh_policy == NETISR_POLICY_SOURCE ||
368 nhp->nh_policy == NETISR_POLICY_FLOW ||
369 nhp->nh_policy == NETISR_POLICY_CPU,
370 ("%s: unsupported nh_policy %u for %s", __func__,
371 nhp->nh_policy, name));
372 KASSERT(nhp->nh_policy == NETISR_POLICY_FLOW ||
373 nhp->nh_m2flow == NULL,
374 ("%s: nh_policy != FLOW but m2flow defined for %s", __func__,
376 KASSERT(nhp->nh_policy == NETISR_POLICY_CPU || nhp->nh_m2cpuid == NULL,
377 ("%s: nh_policy != CPU but m2cpuid defined for %s", __func__,
379 KASSERT(nhp->nh_policy != NETISR_POLICY_CPU || nhp->nh_m2cpuid != NULL,
380 ("%s: nh_policy == CPU but m2cpuid not defined for %s", __func__,
382 KASSERT(nhp->nh_dispatch == NETISR_DISPATCH_DEFAULT ||
383 nhp->nh_dispatch == NETISR_DISPATCH_DEFERRED ||
384 nhp->nh_dispatch == NETISR_DISPATCH_HYBRID ||
385 nhp->nh_dispatch == NETISR_DISPATCH_DIRECT,
386 ("%s: invalid nh_dispatch (%u)", __func__, nhp->nh_dispatch));
388 KASSERT(proto < NETISR_MAXPROT,
389 ("%s(%u, %s): protocol too big", __func__, proto, name));
392 * Test that no existing registration exists for this protocol.
395 KASSERT(netisr_proto[proto].np_name == NULL,
396 ("%s(%u, %s): name present", __func__, proto, name));
397 KASSERT(netisr_proto[proto].np_handler == NULL,
398 ("%s(%u, %s): handler present", __func__, proto, name));
400 netisr_proto[proto].np_name = name;
401 netisr_proto[proto].np_handler = nhp->nh_handler;
402 netisr_proto[proto].np_m2flow = nhp->nh_m2flow;
403 netisr_proto[proto].np_m2cpuid = nhp->nh_m2cpuid;
404 netisr_proto[proto].np_drainedcpu = nhp->nh_drainedcpu;
405 if (nhp->nh_qlimit == 0)
406 netisr_proto[proto].np_qlimit = netisr_defaultqlimit;
407 else if (nhp->nh_qlimit > netisr_maxqlimit) {
408 printf("%s: %s requested queue limit %u capped to "
409 "net.isr.maxqlimit %u\n", __func__, name, nhp->nh_qlimit,
411 netisr_proto[proto].np_qlimit = netisr_maxqlimit;
413 netisr_proto[proto].np_qlimit = nhp->nh_qlimit;
414 netisr_proto[proto].np_policy = nhp->nh_policy;
415 netisr_proto[proto].np_dispatch = nhp->nh_dispatch;
417 npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
418 bzero(npwp, sizeof(*npwp));
419 npwp->nw_qlimit = netisr_proto[proto].np_qlimit;
425 * Clear drop counters across all workstreams for a protocol.
428 netisr_clearqdrops(const struct netisr_handler *nhp)
430 struct netisr_work *npwp;
436 proto = nhp->nh_proto;
440 KASSERT(proto < NETISR_MAXPROT,
441 ("%s(%u): protocol too big for %s", __func__, proto, name));
444 KASSERT(netisr_proto[proto].np_handler != NULL,
445 ("%s(%u): protocol not registered for %s", __func__, proto,
449 npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
456 * Query current drop counters across all workstreams for a protocol.
459 netisr_getqdrops(const struct netisr_handler *nhp, u_int64_t *qdropp)
461 struct netisr_work *npwp;
462 struct rm_priotracker tracker;
469 proto = nhp->nh_proto;
473 KASSERT(proto < NETISR_MAXPROT,
474 ("%s(%u): protocol too big for %s", __func__, proto, name));
476 NETISR_RLOCK(&tracker);
477 KASSERT(netisr_proto[proto].np_handler != NULL,
478 ("%s(%u): protocol not registered for %s", __func__, proto,
482 npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
483 *qdropp += npwp->nw_qdrops;
485 NETISR_RUNLOCK(&tracker);
489 * Query current per-workstream queue limit for a protocol.
492 netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp)
494 struct rm_priotracker tracker;
500 proto = nhp->nh_proto;
504 KASSERT(proto < NETISR_MAXPROT,
505 ("%s(%u): protocol too big for %s", __func__, proto, name));
507 NETISR_RLOCK(&tracker);
508 KASSERT(netisr_proto[proto].np_handler != NULL,
509 ("%s(%u): protocol not registered for %s", __func__, proto,
511 *qlimitp = netisr_proto[proto].np_qlimit;
512 NETISR_RUNLOCK(&tracker);
516 * Update the queue limit across per-workstream queues for a protocol. We
517 * simply change the limits, and don't drain overflowed packets as they will
518 * (hopefully) take care of themselves shortly.
521 netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit)
523 struct netisr_work *npwp;
529 if (qlimit > netisr_maxqlimit)
532 proto = nhp->nh_proto;
536 KASSERT(proto < NETISR_MAXPROT,
537 ("%s(%u): protocol too big for %s", __func__, proto, name));
540 KASSERT(netisr_proto[proto].np_handler != NULL,
541 ("%s(%u): protocol not registered for %s", __func__, proto,
544 netisr_proto[proto].np_qlimit = qlimit;
546 npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
547 npwp->nw_qlimit = qlimit;
554 * Drain all packets currently held in a particular protocol work queue.
557 netisr_drain_proto(struct netisr_work *npwp)
562 * We would assert the lock on the workstream but it's not passed in.
564 while ((m = npwp->nw_head) != NULL) {
565 npwp->nw_head = m->m_nextpkt;
567 if (npwp->nw_head == NULL)
568 npwp->nw_tail = NULL;
572 KASSERT(npwp->nw_tail == NULL, ("%s: tail", __func__));
573 KASSERT(npwp->nw_len == 0, ("%s: len", __func__));
577 * Remove the registration of a network protocol, which requires clearing
578 * per-protocol fields across all workstreams, including freeing all mbufs in
579 * the queues at time of unregister. All work in netisr is briefly suspended
580 * while this takes place.
583 netisr_unregister(const struct netisr_handler *nhp)
585 struct netisr_work *npwp;
591 proto = nhp->nh_proto;
595 KASSERT(proto < NETISR_MAXPROT,
596 ("%s(%u): protocol too big for %s", __func__, proto, name));
599 KASSERT(netisr_proto[proto].np_handler != NULL,
600 ("%s(%u): protocol not registered for %s", __func__, proto,
603 netisr_proto[proto].np_name = NULL;
604 netisr_proto[proto].np_handler = NULL;
605 netisr_proto[proto].np_m2flow = NULL;
606 netisr_proto[proto].np_m2cpuid = NULL;
607 netisr_proto[proto].np_qlimit = 0;
608 netisr_proto[proto].np_policy = 0;
610 npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
611 netisr_drain_proto(npwp);
612 bzero(npwp, sizeof(*npwp));
618 * Compose the global and per-protocol policies on dispatch, and return the
619 * dispatch policy to use.
622 netisr_get_dispatch(struct netisr_proto *npp)
626 * Protocol-specific configuration overrides the global default.
628 if (npp->np_dispatch != NETISR_DISPATCH_DEFAULT)
629 return (npp->np_dispatch);
630 return (netisr_dispatch_policy);
634 * Look up the workstream given a packet and source identifier. Do this by
635 * checking the protocol's policy, and optionally call out to the protocol
636 * for assistance if required.
639 netisr_select_cpuid(struct netisr_proto *npp, u_int dispatch_policy,
640 uintptr_t source, struct mbuf *m, u_int *cpuidp)
645 NETISR_LOCK_ASSERT();
648 * In the event we have only one worker, shortcut and deliver to it
649 * without further ado.
651 if (nws_count == 1) {
652 *cpuidp = nws_array[0];
657 * What happens next depends on the policy selected by the protocol.
658 * If we want to support per-interface policies, we should do that
661 policy = npp->np_policy;
662 if (policy == NETISR_POLICY_CPU) {
663 m = npp->np_m2cpuid(m, source, cpuidp);
668 * It's possible for a protocol not to have a good idea about
669 * where to process a packet, in which case we fall back on
670 * the netisr code to decide. In the hybrid case, return the
671 * current CPU ID, which will force an immediate direct
672 * dispatch. In the queued case, fall back on the SOURCE
675 if (*cpuidp != NETISR_CPUID_NONE)
677 if (dispatch_policy == NETISR_DISPATCH_HYBRID) {
681 policy = NETISR_POLICY_SOURCE;
684 if (policy == NETISR_POLICY_FLOW) {
685 if (M_HASHTYPE_GET(m) == M_HASHTYPE_NONE &&
686 npp->np_m2flow != NULL) {
687 m = npp->np_m2flow(m, source);
691 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
693 netisr_default_flow2cpu(m->m_pkthdr.flowid);
696 policy = NETISR_POLICY_SOURCE;
699 KASSERT(policy == NETISR_POLICY_SOURCE,
700 ("%s: invalid policy %u for %s", __func__, npp->np_policy,
703 ifp = m->m_pkthdr.rcvif;
705 *cpuidp = nws_array[(ifp->if_index + source) % nws_count];
707 *cpuidp = nws_array[source % nws_count];
712 * Process packets associated with a workstream and protocol. For reasons of
713 * fairness, we process up to one complete netisr queue at a time, moving the
714 * queue to a stack-local queue for processing, but do not loop refreshing
715 * from the global queue. The caller is responsible for deciding whether to
716 * loop, and for setting the NWS_RUNNING flag. The passed workstream will be
717 * locked on entry and relocked before return, but will be released while
718 * processing. The number of packets processed is returned.
721 netisr_process_workstream_proto(struct netisr_workstream *nwsp, u_int proto)
723 struct netisr_work local_npw, *npwp;
727 NETISR_LOCK_ASSERT();
728 NWS_LOCK_ASSERT(nwsp);
730 KASSERT(nwsp->nws_flags & NWS_RUNNING,
731 ("%s(%u): not running", __func__, proto));
732 KASSERT(proto >= 0 && proto < NETISR_MAXPROT,
733 ("%s(%u): invalid proto\n", __func__, proto));
735 npwp = &nwsp->nws_work[proto];
736 if (npwp->nw_len == 0)
740 * Move the global work queue to a thread-local work queue.
742 * Notice that this means the effective maximum length of the queue
743 * is actually twice that of the maximum queue length specified in
744 * the protocol registration call.
746 handled = npwp->nw_len;
748 npwp->nw_head = NULL;
749 npwp->nw_tail = NULL;
751 nwsp->nws_pendingbits &= ~(1 << proto);
753 while ((m = local_npw.nw_head) != NULL) {
754 local_npw.nw_head = m->m_nextpkt;
756 if (local_npw.nw_head == NULL)
757 local_npw.nw_tail = NULL;
759 VNET_ASSERT(m->m_pkthdr.rcvif != NULL,
760 ("%s:%d rcvif == NULL: m=%p", __func__, __LINE__, m));
761 CURVNET_SET(m->m_pkthdr.rcvif->if_vnet);
762 netisr_proto[proto].np_handler(m);
765 KASSERT(local_npw.nw_len == 0,
766 ("%s(%u): len %u", __func__, proto, local_npw.nw_len));
767 if (netisr_proto[proto].np_drainedcpu)
768 netisr_proto[proto].np_drainedcpu(nwsp->nws_cpu);
770 npwp->nw_handled += handled;
775 * SWI handler for netisr -- processes packets in a set of workstreams that
776 * it owns, woken up by calls to NWS_SIGNAL(). If this workstream is already
777 * being direct dispatched, go back to sleep and wait for the dispatching
778 * thread to wake us up again.
783 #ifdef NETISR_LOCKING
784 struct rm_priotracker tracker;
786 struct netisr_workstream *nwsp;
791 #ifdef DEVICE_POLLING
792 KASSERT(nws_count == 1,
793 ("%s: device_polling but nws_count != 1", __func__));
796 #ifdef NETISR_LOCKING
797 NETISR_RLOCK(&tracker);
800 KASSERT(!(nwsp->nws_flags & NWS_RUNNING), ("swi_net: running"));
801 if (nwsp->nws_flags & NWS_DISPATCHING)
803 nwsp->nws_flags |= NWS_RUNNING;
804 nwsp->nws_flags &= ~NWS_SCHEDULED;
805 while ((bits = nwsp->nws_pendingbits) != 0) {
806 while ((prot = ffs(bits)) != 0) {
808 bits &= ~(1 << prot);
809 (void)netisr_process_workstream_proto(nwsp, prot);
812 nwsp->nws_flags &= ~NWS_RUNNING;
815 #ifdef NETISR_LOCKING
816 NETISR_RUNLOCK(&tracker);
818 #ifdef DEVICE_POLLING
824 netisr_queue_workstream(struct netisr_workstream *nwsp, u_int proto,
825 struct netisr_work *npwp, struct mbuf *m, int *dosignalp)
828 NWS_LOCK_ASSERT(nwsp);
831 if (npwp->nw_len < npwp->nw_qlimit) {
833 if (npwp->nw_head == NULL) {
837 npwp->nw_tail->m_nextpkt = m;
841 if (npwp->nw_len > npwp->nw_watermark)
842 npwp->nw_watermark = npwp->nw_len;
845 * We must set the bit regardless of NWS_RUNNING, so that
846 * swi_net() keeps calling netisr_process_workstream_proto().
848 nwsp->nws_pendingbits |= (1 << proto);
849 if (!(nwsp->nws_flags &
850 (NWS_RUNNING | NWS_DISPATCHING | NWS_SCHEDULED))) {
851 nwsp->nws_flags |= NWS_SCHEDULED;
852 *dosignalp = 1; /* Defer until unlocked. */
864 netisr_queue_internal(u_int proto, struct mbuf *m, u_int cpuid)
866 struct netisr_workstream *nwsp;
867 struct netisr_work *npwp;
870 #ifdef NETISR_LOCKING
871 NETISR_LOCK_ASSERT();
873 KASSERT(cpuid <= mp_maxid, ("%s: cpuid too big (%u, %u)", __func__,
875 KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__, cpuid));
879 nwsp = DPCPU_ID_PTR(cpuid, nws);
880 npwp = &nwsp->nws_work[proto];
882 error = netisr_queue_workstream(nwsp, proto, npwp, m, &dosignal);
890 netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m)
892 #ifdef NETISR_LOCKING
893 struct rm_priotracker tracker;
898 KASSERT(proto < NETISR_MAXPROT,
899 ("%s: invalid proto %u", __func__, proto));
901 #ifdef NETISR_LOCKING
902 NETISR_RLOCK(&tracker);
904 KASSERT(netisr_proto[proto].np_handler != NULL,
905 ("%s: invalid proto %u", __func__, proto));
907 m = netisr_select_cpuid(&netisr_proto[proto], NETISR_DISPATCH_DEFERRED,
910 KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__,
912 error = netisr_queue_internal(proto, m, cpuid);
915 #ifdef NETISR_LOCKING
916 NETISR_RUNLOCK(&tracker);
922 netisr_queue(u_int proto, struct mbuf *m)
925 return (netisr_queue_src(proto, 0, m));
929 * Dispatch a packet for netisr processing; direct dispatch is permitted by
933 netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m)
935 #ifdef NETISR_LOCKING
936 struct rm_priotracker tracker;
938 struct netisr_workstream *nwsp;
939 struct netisr_proto *npp;
940 struct netisr_work *npwp;
942 u_int cpuid, dispatch_policy;
944 KASSERT(proto < NETISR_MAXPROT,
945 ("%s: invalid proto %u", __func__, proto));
946 #ifdef NETISR_LOCKING
947 NETISR_RLOCK(&tracker);
949 npp = &netisr_proto[proto];
950 KASSERT(npp->np_handler != NULL, ("%s: invalid proto %u", __func__,
953 dispatch_policy = netisr_get_dispatch(npp);
954 if (dispatch_policy == NETISR_DISPATCH_DEFERRED)
955 return (netisr_queue_src(proto, source, m));
958 * If direct dispatch is forced, then unconditionally dispatch
959 * without a formal CPU selection. Borrow the current CPU's stats,
960 * even if there's no worker on it. In this case we don't update
961 * nws_flags because all netisr processing will be source ordered due
962 * to always being forced to directly dispatch.
964 if (dispatch_policy == NETISR_DISPATCH_DIRECT) {
965 nwsp = DPCPU_PTR(nws);
966 npwp = &nwsp->nws_work[proto];
967 npwp->nw_dispatched++;
969 netisr_proto[proto].np_handler(m);
974 KASSERT(dispatch_policy == NETISR_DISPATCH_HYBRID,
975 ("%s: unknown dispatch policy (%u)", __func__, dispatch_policy));
978 * Otherwise, we execute in a hybrid mode where we will try to direct
979 * dispatch if we're on the right CPU and the netisr worker isn't
983 m = netisr_select_cpuid(&netisr_proto[proto], NETISR_DISPATCH_HYBRID,
989 KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__, cpuid));
992 nwsp = DPCPU_PTR(nws);
993 npwp = &nwsp->nws_work[proto];
996 * We are willing to direct dispatch only if three conditions hold:
998 * (1) The netisr worker isn't already running,
999 * (2) Another thread isn't already directly dispatching, and
1000 * (3) The netisr hasn't already been woken up.
1003 if (nwsp->nws_flags & (NWS_RUNNING | NWS_DISPATCHING | NWS_SCHEDULED)) {
1004 error = netisr_queue_workstream(nwsp, proto, npwp, m,
1013 * The current thread is now effectively the netisr worker, so set
1014 * the dispatching flag to prevent concurrent processing of the
1015 * stream from another thread (even the netisr worker), which could
1016 * otherwise lead to effective misordering of the stream.
1018 nwsp->nws_flags |= NWS_DISPATCHING;
1020 netisr_proto[proto].np_handler(m);
1022 nwsp->nws_flags &= ~NWS_DISPATCHING;
1024 npwp->nw_hybrid_dispatched++;
1027 * If other work was enqueued by another thread while we were direct
1028 * dispatching, we need to signal the netisr worker to do that work.
1029 * In the future, we might want to do some of that work in the
1030 * current thread, rather than trigger further context switches. If
1031 * so, we'll want to establish a reasonable bound on the work done in
1032 * the "borrowed" context.
1034 if (nwsp->nws_pendingbits != 0) {
1035 nwsp->nws_flags |= NWS_SCHEDULED;
1046 error = netisr_queue_internal(proto, m, cpuid);
1050 #ifdef NETISR_LOCKING
1051 NETISR_RUNLOCK(&tracker);
1057 netisr_dispatch(u_int proto, struct mbuf *m)
1060 return (netisr_dispatch_src(proto, 0, m));
1063 #ifdef DEVICE_POLLING
1065 * Kernel polling borrows a netisr thread to run interface polling in; this
1066 * function allows kernel polling to request that the netisr thread be
1067 * scheduled even if no packets are pending for protocols.
1070 netisr_sched_poll(void)
1072 struct netisr_workstream *nwsp;
1074 nwsp = DPCPU_ID_PTR(nws_array[0], nws);
1080 netisr_start_swi(u_int cpuid, struct pcpu *pc)
1083 struct netisr_workstream *nwsp;
1086 KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__, cpuid));
1088 nwsp = DPCPU_ID_PTR(cpuid, nws);
1089 mtx_init(&nwsp->nws_mtx, "netisr_mtx", NULL, MTX_DEF);
1090 nwsp->nws_cpu = cpuid;
1091 snprintf(swiname, sizeof(swiname), "netisr %u", cpuid);
1092 error = swi_add(&nwsp->nws_intr_event, swiname, swi_net, nwsp,
1093 SWI_NET, INTR_MPSAFE, &nwsp->nws_swi_cookie);
1095 panic("%s: swi_add %d", __func__, error);
1096 pc->pc_netisr = nwsp->nws_intr_event;
1097 if (netisr_bindthreads) {
1098 error = intr_event_bind(nwsp->nws_intr_event, cpuid);
1100 printf("%s: cpu %u: intr_event_bind: %d", __func__,
1104 nws_array[nws_count] = nwsp->nws_cpu;
1110 * Initialize the netisr subsystem. We rely on BSS and static initialization
1111 * of most fields in global data structures.
1113 * Start a worker thread for the boot CPU so that we can support network
1114 * traffic immediately in case the network stack is used before additional
1115 * CPUs are started (for example, diskless boot).
1118 netisr_init(void *arg)
1120 KASSERT(curcpu == 0, ("%s: not on CPU 0", __func__));
1123 if (netisr_maxthreads < 1)
1124 netisr_maxthreads = 1;
1125 if (netisr_maxthreads > mp_ncpus) {
1126 printf("netisr_init: forcing maxthreads from %d to %d\n",
1127 netisr_maxthreads, mp_ncpus);
1128 netisr_maxthreads = mp_ncpus;
1130 if (netisr_defaultqlimit > netisr_maxqlimit) {
1131 printf("netisr_init: forcing defaultqlimit from %d to %d\n",
1132 netisr_defaultqlimit, netisr_maxqlimit);
1133 netisr_defaultqlimit = netisr_maxqlimit;
1135 #ifdef DEVICE_POLLING
1137 * The device polling code is not yet aware of how to deal with
1138 * multiple netisr threads, so for the time being compiling in device
1139 * polling disables parallel netisr workers.
1141 if (netisr_maxthreads != 1 || netisr_bindthreads != 0) {
1142 printf("netisr_init: forcing maxthreads to 1 and "
1143 "bindthreads to 0 for device polling\n");
1144 netisr_maxthreads = 1;
1145 netisr_bindthreads = 0;
1148 netisr_start_swi(curcpu, pcpu_find(curcpu));
1150 SYSINIT(netisr_init, SI_SUB_SOFTINTR, SI_ORDER_FIRST, netisr_init, NULL);
1153 * Start worker threads for additional CPUs. No attempt to gracefully handle
1154 * work reassignment, we don't yet support dynamic reconfiguration.
1157 netisr_start(void *arg)
1161 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
1162 if (nws_count >= netisr_maxthreads)
1164 /* XXXRW: Is skipping absent CPUs still required here? */
1165 if (CPU_ABSENT(pc->pc_cpuid))
1167 /* Worker will already be present for boot CPU. */
1168 if (pc->pc_netisr != NULL)
1170 netisr_start_swi(pc->pc_cpuid, pc);
1173 SYSINIT(netisr_start, SI_SUB_SMP, SI_ORDER_MIDDLE, netisr_start, NULL);
1176 * Sysctl monitoring for netisr: query a list of registered protocols.
1179 sysctl_netisr_proto(SYSCTL_HANDLER_ARGS)
1181 struct rm_priotracker tracker;
1182 struct sysctl_netisr_proto *snpp, *snp_array;
1183 struct netisr_proto *npp;
1184 u_int counter, proto;
1187 if (req->newptr != NULL)
1189 snp_array = malloc(sizeof(*snp_array) * NETISR_MAXPROT, M_TEMP,
1192 NETISR_RLOCK(&tracker);
1193 for (proto = 0; proto < NETISR_MAXPROT; proto++) {
1194 npp = &netisr_proto[proto];
1195 if (npp->np_name == NULL)
1197 snpp = &snp_array[counter];
1198 snpp->snp_version = sizeof(*snpp);
1199 strlcpy(snpp->snp_name, npp->np_name, NETISR_NAMEMAXLEN);
1200 snpp->snp_proto = proto;
1201 snpp->snp_qlimit = npp->np_qlimit;
1202 snpp->snp_policy = npp->np_policy;
1203 snpp->snp_dispatch = npp->np_dispatch;
1204 if (npp->np_m2flow != NULL)
1205 snpp->snp_flags |= NETISR_SNP_FLAGS_M2FLOW;
1206 if (npp->np_m2cpuid != NULL)
1207 snpp->snp_flags |= NETISR_SNP_FLAGS_M2CPUID;
1208 if (npp->np_drainedcpu != NULL)
1209 snpp->snp_flags |= NETISR_SNP_FLAGS_DRAINEDCPU;
1212 NETISR_RUNLOCK(&tracker);
1213 KASSERT(counter <= NETISR_MAXPROT,
1214 ("sysctl_netisr_proto: counter too big (%d)", counter));
1215 error = SYSCTL_OUT(req, snp_array, sizeof(*snp_array) * counter);
1216 free(snp_array, M_TEMP);
1220 SYSCTL_PROC(_net_isr, OID_AUTO, proto,
1221 CTLFLAG_RD|CTLTYPE_STRUCT|CTLFLAG_MPSAFE, 0, 0, sysctl_netisr_proto,
1222 "S,sysctl_netisr_proto",
1223 "Return list of protocols registered with netisr");
1226 * Sysctl monitoring for netisr: query a list of workstreams.
1229 sysctl_netisr_workstream(SYSCTL_HANDLER_ARGS)
1231 struct rm_priotracker tracker;
1232 struct sysctl_netisr_workstream *snwsp, *snws_array;
1233 struct netisr_workstream *nwsp;
1234 u_int counter, cpuid;
1237 if (req->newptr != NULL)
1239 snws_array = malloc(sizeof(*snws_array) * MAXCPU, M_TEMP,
1242 NETISR_RLOCK(&tracker);
1243 CPU_FOREACH(cpuid) {
1244 nwsp = DPCPU_ID_PTR(cpuid, nws);
1245 if (nwsp->nws_intr_event == NULL)
1248 snwsp = &snws_array[counter];
1249 snwsp->snws_version = sizeof(*snwsp);
1252 * For now, we equate workstream IDs and CPU IDs in the
1253 * kernel, but expose them independently to userspace in case
1254 * that assumption changes in the future.
1256 snwsp->snws_wsid = cpuid;
1257 snwsp->snws_cpu = cpuid;
1258 if (nwsp->nws_intr_event != NULL)
1259 snwsp->snws_flags |= NETISR_SNWS_FLAGS_INTR;
1263 NETISR_RUNLOCK(&tracker);
1264 KASSERT(counter <= MAXCPU,
1265 ("sysctl_netisr_workstream: counter too big (%d)", counter));
1266 error = SYSCTL_OUT(req, snws_array, sizeof(*snws_array) * counter);
1267 free(snws_array, M_TEMP);
1271 SYSCTL_PROC(_net_isr, OID_AUTO, workstream,
1272 CTLFLAG_RD|CTLTYPE_STRUCT|CTLFLAG_MPSAFE, 0, 0, sysctl_netisr_workstream,
1273 "S,sysctl_netisr_workstream",
1274 "Return list of workstreams implemented by netisr");
1277 * Sysctl monitoring for netisr: query per-protocol data across all
1281 sysctl_netisr_work(SYSCTL_HANDLER_ARGS)
1283 struct rm_priotracker tracker;
1284 struct sysctl_netisr_work *snwp, *snw_array;
1285 struct netisr_workstream *nwsp;
1286 struct netisr_proto *npp;
1287 struct netisr_work *nwp;
1288 u_int counter, cpuid, proto;
1291 if (req->newptr != NULL)
1293 snw_array = malloc(sizeof(*snw_array) * MAXCPU * NETISR_MAXPROT,
1294 M_TEMP, M_ZERO | M_WAITOK);
1296 NETISR_RLOCK(&tracker);
1297 CPU_FOREACH(cpuid) {
1298 nwsp = DPCPU_ID_PTR(cpuid, nws);
1299 if (nwsp->nws_intr_event == NULL)
1302 for (proto = 0; proto < NETISR_MAXPROT; proto++) {
1303 npp = &netisr_proto[proto];
1304 if (npp->np_name == NULL)
1306 nwp = &nwsp->nws_work[proto];
1307 snwp = &snw_array[counter];
1308 snwp->snw_version = sizeof(*snwp);
1309 snwp->snw_wsid = cpuid; /* See comment above. */
1310 snwp->snw_proto = proto;
1311 snwp->snw_len = nwp->nw_len;
1312 snwp->snw_watermark = nwp->nw_watermark;
1313 snwp->snw_dispatched = nwp->nw_dispatched;
1314 snwp->snw_hybrid_dispatched =
1315 nwp->nw_hybrid_dispatched;
1316 snwp->snw_qdrops = nwp->nw_qdrops;
1317 snwp->snw_queued = nwp->nw_queued;
1318 snwp->snw_handled = nwp->nw_handled;
1323 KASSERT(counter <= MAXCPU * NETISR_MAXPROT,
1324 ("sysctl_netisr_work: counter too big (%d)", counter));
1325 NETISR_RUNLOCK(&tracker);
1326 error = SYSCTL_OUT(req, snw_array, sizeof(*snw_array) * counter);
1327 free(snw_array, M_TEMP);
1331 SYSCTL_PROC(_net_isr, OID_AUTO, work,
1332 CTLFLAG_RD|CTLTYPE_STRUCT|CTLFLAG_MPSAFE, 0, 0, sysctl_netisr_work,
1333 "S,sysctl_netisr_work",
1334 "Return list of per-workstream, per-protocol work in netisr");
1337 DB_SHOW_COMMAND(netisr, db_show_netisr)
1339 struct netisr_workstream *nwsp;
1340 struct netisr_work *nwp;
1344 db_printf("%3s %6s %5s %5s %5s %8s %8s %8s %8s\n", "CPU", "Proto",
1345 "Len", "WMark", "Max", "Disp", "HDisp", "Drop", "Queue");
1346 CPU_FOREACH(cpuid) {
1347 nwsp = DPCPU_ID_PTR(cpuid, nws);
1348 if (nwsp->nws_intr_event == NULL)
1351 for (proto = 0; proto < NETISR_MAXPROT; proto++) {
1352 if (netisr_proto[proto].np_handler == NULL)
1354 nwp = &nwsp->nws_work[proto];
1356 db_printf("%3d ", cpuid);
1359 db_printf("%3s ", "");
1361 "%6s %5d %5d %5d %8ju %8ju %8ju %8ju\n",
1362 netisr_proto[proto].np_name, nwp->nw_len,
1363 nwp->nw_watermark, nwp->nw_qlimit,
1364 nwp->nw_dispatched, nwp->nw_hybrid_dispatched,
1365 nwp->nw_qdrops, nwp->nw_queued);