netisr.c revision 193219
1231057Sdim/*-
2231057Sdim * Copyright (c) 2007-2009 Robert N. M. Watson
3246705Sandrew * All rights reserved.
4246705Sandrew *
5231057Sdim * Redistribution and use in source and binary forms, with or without
6231057Sdim * modification, are permitted provided that the following conditions
7231057Sdim * are met:
8231057Sdim * 1. Redistributions of source code must retain the above copyright
9231057Sdim *    notice, this list of conditions and the following disclaimer.
10231057Sdim * 2. Redistributions in binary form must reproduce the above copyright
11231057Sdim *    notice, this list of conditions and the following disclaimer in the
12231057Sdim *    documentation and/or other materials provided with the distribution.
13249423Sdim *
14249423Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15231057Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16231057Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17231057Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18231057Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19231057Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net/netisr.c 193219 2009-06-01 10:41:38Z rwatson $");
29
30/*
31 * netisr is a packet dispatch service, allowing synchronous (directly
32 * dispatched) and asynchronous (deferred dispatch) processing of packets by
33 * registered protocol handlers.  Callers pass a protocol identifier and
34 * packet to netisr, along with a direct dispatch hint, and work will either
35 * be immediately processed with the registered handler, or passed to a
36 * kernel software interrupt (SWI) thread for deferred dispatch.  Callers
37 * will generally select one or the other based on:
38 *
39 * - Might directly dispatching a netisr handler lead to code reentrance or
40 *   lock recursion, such as entering the socket code from the socket code.
41 * - Might directly dispatching a netisr handler lead to recursive
42 *   processing, such as when decapsulating several wrapped layers of tunnel
43 *   information (IPSEC within IPSEC within ...).
44 *
45 * Maintaining ordering for protocol streams is a critical design concern.
46 * Enforcing ordering limits the opportunity for concurrency, but maintains
47 * the strong ordering requirements found in some protocols, such as TCP.  Of
48 * related concern is CPU affinity--it is desirable to process all data
49 * associated with a particular stream on the same CPU over time in order to
50 * avoid acquiring locks associated with the connection on different CPUs,
51 * keep connection data in one cache, and to generally encourage associated
52 * user threads to live on the same CPU as the stream.  It's also desirable
53 * to avoid lock migration and contention where locks are associated with
54 * more than one flow.
55 *
56 * netisr supports several policy variations, represented by the
57 * NETISR_POLICY_* constants, allowing protocols to play a varying role in
58 * identifying flows, assigning work to CPUs, etc.  These are described in
59 * detail in netisr.h.
60 */
61
62#include "opt_ddb.h"
63#include "opt_device_polling.h"
64
65#include <sys/param.h>
66#include <sys/bus.h>
67#include <sys/kernel.h>
68#include <sys/kthread.h>
69#include <sys/interrupt.h>
70#include <sys/lock.h>
71#include <sys/mbuf.h>
72#include <sys/mutex.h>
73#include <sys/proc.h>
74#include <sys/rmlock.h>
75#include <sys/sched.h>
76#include <sys/smp.h>
77#include <sys/socket.h>
78#include <sys/sysctl.h>
79#include <sys/systm.h>
80#include <sys/vimage.h>
81
82#ifdef DDB
83#include <ddb/ddb.h>
84#endif
85
86#include <net/if.h>
87#include <net/if_var.h>
88#include <net/netisr.h>
89
90/*-
91 * Synchronize use and modification of the registered netisr data structures;
92 * acquire a read lock while modifying the set of registered protocols to
93 * prevent partially registered or unregistered protocols from being run.
94 *
95 * The following data structures and fields are protected by this lock:
96 *
97 * - The np array, including all fields of struct netisr_proto.
98 * - The nws array, including all fields of struct netisr_worker.
99 * - The nws_array array.
100 *
101 * Note: the NETISR_LOCKING define controls whether read locks are acquired
102 * in packet processing paths requiring netisr registration stability.  This
103 * is disabled by default as it can lead to a measurable performance
104 * degradation even with rmlocks (3%-6% for loopback ping-pong traffic), and
105 * because netisr registration and unregistration is extremely rare at
106 * runtime.  If it becomes more common, this decision should be revisited.
107 *
108 * XXXRW: rmlocks don't support assertions.
109 */
110static struct rmlock	netisr_rmlock;
111#define	NETISR_LOCK_INIT()	rm_init_flags(&netisr_rmlock, "netisr", \
112				    RM_NOWITNESS)
113#define	NETISR_LOCK_ASSERT()
114#define	NETISR_RLOCK(tracker)	rm_rlock(&netisr_rmlock, (tracker))
115#define	NETISR_RUNLOCK(tracker)	rm_runlock(&netisr_rmlock, (tracker))
116#define	NETISR_WLOCK()		rm_wlock(&netisr_rmlock)
117#define	NETISR_WUNLOCK()	rm_wunlock(&netisr_rmlock)
118/* #define	NETISR_LOCKING */
119
120SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW, 0, "netisr");
121
122/*-
123 * Three direct dispatch policies are supported:
124 *
125 * - Always defer: all work is scheduled for a netisr, regardless of context.
126 *   (!direct)
127 *
128 * - Hybrid: if the executing context allows direct dispatch, and we're
129 *   running on the CPU the work would be done on, then direct dispatch if it
130 *   wouldn't violate ordering constraints on the workstream.
131 *   (direct && !direct_force)
132 *
133 * - Always direct: if the executing context allows direct dispatch, always
134 *   direct dispatch.  (direct && direct_force)
135 *
136 * Notice that changing the global policy could lead to short periods of
137 * misordered processing, but this is considered acceptable as compared to
138 * the complexity of enforcing ordering during policy changes.
139 */
140static int	netisr_direct_force = 1;	/* Always direct dispatch. */
141TUNABLE_INT("net.isr.direct_force", &netisr_direct_force);
142SYSCTL_INT(_net_isr, OID_AUTO, direct_force, CTLFLAG_RW,
143    &netisr_direct_force, 0, "Force direct dispatch");
144
145static int	netisr_direct = 1;	/* Enable direct dispatch. */
146TUNABLE_INT("net.isr.direct", &netisr_direct);
147SYSCTL_INT(_net_isr, OID_AUTO, direct, CTLFLAG_RW,
148    &netisr_direct, 0, "Enable direct dispatch");
149
150/*
151 * Allow the administrator to limit the number of threads (CPUs) to use for
152 * netisr.  We don't check netisr_maxthreads before creating the thread for
153 * CPU 0, so in practice we ignore values <= 1.  This must be set at boot.
154 * We will create at most one thread per CPU.
155 */
156static int	netisr_maxthreads = 1;		/* Max number of threads. */
157TUNABLE_INT("net.isr.maxthreads", &netisr_maxthreads);
158SYSCTL_INT(_net_isr, OID_AUTO, maxthreads, CTLFLAG_RD,
159    &netisr_maxthreads, 0,
160    "Use at most this many CPUs for netisr processing");
161
162static int	netisr_bindthreads = 0;		/* Bind threads to CPUs. */
163TUNABLE_INT("net.isr.bindthreads", &netisr_bindthreads);
164SYSCTL_INT(_net_isr, OID_AUTO, bindthreads, CTLFLAG_RD,
165    &netisr_bindthreads, 0, "Bind netisr threads to CPUs.");
166
167/*
168 * Limit per-workstream queues to at most net.isr.maxqlimit, both for initial
169 * configuration and later modification using netisr_setqlimit().
170 */
171#define	NETISR_DEFAULT_MAXQLIMIT	10240
172static u_int	netisr_maxqlimit = NETISR_DEFAULT_MAXQLIMIT;
173TUNABLE_INT("net.isr.maxqlimit", &netisr_maxqlimit);
174SYSCTL_INT(_net_isr, OID_AUTO, maxqlimit, CTLFLAG_RD,
175    &netisr_maxqlimit, 0,
176    "Maximum netisr per-protocol, per-CPU queue depth.");
177
178/*
179 * The default per-workstream queue limit for protocols that don't initialize
180 * the nh_qlimit field of their struct netisr_handler.  If this is set above
181 * netisr_maxqlimit, we truncate it to the maximum during boot.
182 */
183#define	NETISR_DEFAULT_DEFAULTQLIMIT	256
184static u_int	netisr_defaultqlimit = NETISR_DEFAULT_DEFAULTQLIMIT;
185TUNABLE_INT("net.isr.defaultqlimit", &netisr_defaultqlimit);
186SYSCTL_INT(_net_isr, OID_AUTO, defaultqlimit, CTLFLAG_RD,
187    &netisr_defaultqlimit, 0,
188    "Default netisr per-protocol, per-CPU queue limit if not set by protocol");
189
190/*
191 * Each protocol is described by a struct netisr_proto, which holds all
192 * global per-protocol information.  This data structure is set up by
193 * netisr_register(), and derived from the public struct netisr_handler.
194 */
195struct netisr_proto {
196	const char	*np_name;	/* Character string protocol name. */
197	netisr_handler_t *np_handler;	/* Protocol handler. */
198	netisr_m2flow_t	*np_m2flow;	/* Query flow for untagged packet. */
199	netisr_m2cpuid_t *np_m2cpuid;	/* Query CPU to process packet on. */
200	u_int		 np_qlimit;	/* Maximum per-CPU queue depth. */
201	u_int		 np_policy;	/* Work placement policy. */
202};
203
204#define	NETISR_MAXPROT		32		/* Compile-time limit. */
205
206/*
207 * The np array describes all registered protocols, indexed by protocol
208 * number.
209 */
210static struct netisr_proto	np[NETISR_MAXPROT];
211
212/*
213 * Protocol-specific work for each workstream is described by struct
214 * netisr_work.  Each work descriptor consists of an mbuf queue and
215 * statistics.
216 */
217struct netisr_work {
218	/*
219	 * Packet queue, linked by m_nextpkt.
220	 */
221	struct mbuf	*nw_head;
222	struct mbuf	*nw_tail;
223	u_int		 nw_len;
224	u_int		 nw_qlimit;
225	u_int		 nw_watermark;
226
227	/*
228	 * Statistics -- written unlocked, but mostly from curcpu.
229	 */
230	u_int64_t	 nw_dispatched; /* Number of direct dispatches. */
231	u_int64_t	 nw_hybrid_dispatched; /* "" hybrid dispatches. */
232	u_int64_t	 nw_qdrops;	/* "" drops. */
233	u_int64_t	 nw_queued;	/* "" enqueues. */
234	u_int64_t	 nw_handled;	/* "" handled in worker. */
235};
236
237/*
238 * Workstreams hold a set of ordered work across each protocol, and are
239 * described by netisr_workstream.  Each workstream is associated with a
240 * worker thread, which in turn is pinned to a CPU.  Work associated with a
241 * workstream can be processd in other threads during direct dispatch;
242 * concurrent processing is prevented by the NWS_RUNNING flag, which
243 * indicates that a thread is already processing the work queue.
244 */
245struct netisr_workstream {
246	struct intr_event *nws_intr_event;	/* Handler for stream. */
247	void		*nws_swi_cookie;	/* swi(9) cookie for stream. */
248	struct mtx	 nws_mtx;		/* Synchronize work. */
249	u_int		 nws_cpu;		/* CPU pinning. */
250	u_int		 nws_flags;		/* Wakeup flags. */
251	u_int		 nws_pendingbits;	/* Scheduled protocols. */
252
253	/*
254	 * Each protocol has per-workstream data.
255	 */
256	struct netisr_work	nws_work[NETISR_MAXPROT];
257} __aligned(CACHE_LINE_SIZE);
258
259/*
260 * Per-CPU workstream data, indexed by CPU ID.
261 */
262static struct netisr_workstream		 nws[MAXCPU];
263
264/*
265 * Map contiguous values between 0 and nws_count into CPU IDs appropriate for
266 * indexing the nws[] array.  This allows constructions of the form
267 * nws[nws_array(arbitraryvalue % nws_count)].
268 */
269static u_int				 nws_array[MAXCPU];
270
271/*
272 * Number of registered workstreams.  Will be at most the number of running
273 * CPUs once fully started.
274 */
275static u_int				 nws_count;
276SYSCTL_INT(_net_isr, OID_AUTO, numthreads, CTLFLAG_RD,
277    &nws_count, 0, "Number of extant netisr threads.");
278
279/*
280 * Per-workstream flags.
281 */
282#define	NWS_RUNNING	0x00000001	/* Currently running in a thread. */
283#define	NWS_DISPATCHING	0x00000002	/* Currently being direct-dispatched. */
284#define	NWS_SCHEDULED	0x00000004	/* Signal issued. */
285
286/*
287 * Synchronization for each workstream: a mutex protects all mutable fields
288 * in each stream, including per-protocol state (mbuf queues).  The SWI is
289 * woken up if asynchronous dispatch is required.
290 */
291#define	NWS_LOCK(s)		mtx_lock(&(s)->nws_mtx)
292#define	NWS_LOCK_ASSERT(s)	mtx_assert(&(s)->nws_mtx, MA_OWNED)
293#define	NWS_UNLOCK(s)		mtx_unlock(&(s)->nws_mtx)
294#define	NWS_SIGNAL(s)		swi_sched((s)->nws_swi_cookie, 0)
295
296/*
297 * Utility routines for protocols that implement their own mapping of flows
298 * to CPUs.
299 */
300u_int
301netisr_get_cpucount(void)
302{
303
304	return (nws_count);
305}
306
307u_int
308netisr_get_cpuid(u_int cpunumber)
309{
310
311	KASSERT(cpunumber < nws_count, ("%s: %u > %u", __func__, cpunumber,
312	    nws_count));
313
314	return (nws_array[cpunumber]);
315}
316
317/*
318 * The default implementation of -> CPU ID mapping.
319 *
320 * Non-static so that protocols can use it to map their own work to specific
321 * CPUs in a manner consistent to netisr for affinity purposes.
322 */
323u_int
324netisr_default_flow2cpu(u_int flowid)
325{
326
327	return (nws_array[flowid % nws_count]);
328}
329
330/*
331 * Register a new netisr handler, which requires initializing per-protocol
332 * fields for each workstream.  All netisr work is briefly suspended while
333 * the protocol is installed.
334 */
335void
336netisr_register(const struct netisr_handler *nhp)
337{
338	struct netisr_work *npwp;
339	const char *name;
340	u_int i, proto;
341
342	proto = nhp->nh_proto;
343	name = nhp->nh_name;
344
345	/*
346	 * Test that the requested registration is valid.
347	 */
348	KASSERT(nhp->nh_name != NULL,
349	    ("%s: nh_name NULL for %u", __func__, proto));
350	KASSERT(nhp->nh_handler != NULL,
351	    ("%s: nh_handler NULL for %s", __func__, name));
352	KASSERT(nhp->nh_policy == NETISR_POLICY_SOURCE ||
353	    nhp->nh_policy == NETISR_POLICY_FLOW ||
354	    nhp->nh_policy == NETISR_POLICY_CPU,
355	    ("%s: unsupported nh_policy %u for %s", __func__,
356	    nhp->nh_policy, name));
357	KASSERT(nhp->nh_policy == NETISR_POLICY_FLOW ||
358	    nhp->nh_m2flow == NULL,
359	    ("%s: nh_policy != FLOW but m2flow defined for %s", __func__,
360	    name));
361	KASSERT(nhp->nh_policy == NETISR_POLICY_CPU || nhp->nh_m2cpuid == NULL,
362	    ("%s: nh_policy != CPU but m2cpuid defined for %s", __func__,
363	    name));
364	KASSERT(nhp->nh_policy != NETISR_POLICY_CPU || nhp->nh_m2cpuid != NULL,
365	    ("%s: nh_policy == CPU but m2cpuid not defined for %s", __func__,
366	    name));
367	KASSERT(proto < NETISR_MAXPROT,
368	    ("%s(%u, %s): protocol too big", __func__, proto, name));
369
370	/*
371	 * Test that no existing registration exists for this protocol.
372	 */
373	NETISR_WLOCK();
374	KASSERT(np[proto].np_name == NULL,
375	    ("%s(%u, %s): name present", __func__, proto, name));
376	KASSERT(np[proto].np_handler == NULL,
377	    ("%s(%u, %s): handler present", __func__, proto, name));
378
379	np[proto].np_name = name;
380	np[proto].np_handler = nhp->nh_handler;
381	np[proto].np_m2flow = nhp->nh_m2flow;
382	np[proto].np_m2cpuid = nhp->nh_m2cpuid;
383	if (nhp->nh_qlimit == 0)
384		np[proto].np_qlimit = netisr_defaultqlimit;
385	else if (nhp->nh_qlimit > netisr_maxqlimit) {
386		printf("%s: %s requested queue limit %u capped to "
387		    "net.isr.maxqlimit %u\n", __func__, name, nhp->nh_qlimit,
388		    netisr_maxqlimit);
389		np[proto].np_qlimit = netisr_maxqlimit;
390	} else
391		np[proto].np_qlimit = nhp->nh_qlimit;
392	np[proto].np_policy = nhp->nh_policy;
393	for (i = 0; i < MAXCPU; i++) {
394		npwp = &nws[i].nws_work[proto];
395		bzero(npwp, sizeof(*npwp));
396		npwp->nw_qlimit = np[proto].np_qlimit;
397	}
398	NETISR_WUNLOCK();
399}
400
401/*
402 * Clear drop counters across all workstreams for a protocol.
403 */
404void
405netisr_clearqdrops(const struct netisr_handler *nhp)
406{
407	struct netisr_work *npwp;
408#ifdef INVARIANTS
409	const char *name;
410#endif
411	u_int i, proto;
412
413	proto = nhp->nh_proto;
414#ifdef INVARIANTS
415	name = nhp->nh_name;
416#endif
417	KASSERT(proto < NETISR_MAXPROT,
418	    ("%s(%u): protocol too big for %s", __func__, proto, name));
419
420	NETISR_WLOCK();
421	KASSERT(np[proto].np_handler != NULL,
422	    ("%s(%u): protocol not registered for %s", __func__, proto,
423	    name));
424
425	for (i = 0; i < MAXCPU; i++) {
426		npwp = &nws[i].nws_work[proto];
427		npwp->nw_qdrops = 0;
428	}
429	NETISR_WUNLOCK();
430}
431
432/*
433 * Query the current drop counters across all workstreams for a protocol.
434 */
435void
436netisr_getqdrops(const struct netisr_handler *nhp, u_int64_t *qdropp)
437{
438	struct netisr_work *npwp;
439	struct rm_priotracker tracker;
440#ifdef INVARIANTS
441	const char *name;
442#endif
443	u_int i, proto;
444
445	*qdropp = 0;
446	proto = nhp->nh_proto;
447#ifdef INVARIANTS
448	name = nhp->nh_name;
449#endif
450	KASSERT(proto < NETISR_MAXPROT,
451	    ("%s(%u): protocol too big for %s", __func__, proto, name));
452
453	NETISR_RLOCK(&tracker);
454	KASSERT(np[proto].np_handler != NULL,
455	    ("%s(%u): protocol not registered for %s", __func__, proto,
456	    name));
457
458	for (i = 0; i < MAXCPU; i++) {
459		npwp = &nws[i].nws_work[proto];
460		*qdropp += npwp->nw_qdrops;
461	}
462	NETISR_RUNLOCK(&tracker);
463}
464
465/*
466 * Query the current queue limit for per-workstream queues for a protocol.
467 */
468void
469netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp)
470{
471	struct rm_priotracker tracker;
472#ifdef INVARIANTS
473	const char *name;
474#endif
475	u_int proto;
476
477	proto = nhp->nh_proto;
478#ifdef INVARIANTS
479	name = nhp->nh_name;
480#endif
481	KASSERT(proto < NETISR_MAXPROT,
482	    ("%s(%u): protocol too big for %s", __func__, proto, name));
483
484	NETISR_RLOCK(&tracker);
485	KASSERT(np[proto].np_handler != NULL,
486	    ("%s(%u): protocol not registered for %s", __func__, proto,
487	    name));
488	*qlimitp = np[proto].np_qlimit;
489	NETISR_RUNLOCK(&tracker);
490}
491
492/*
493 * Update the queue limit across per-workstream queues for a protocol.  We
494 * simply change the limits, and don't drain overflowed packets as they will
495 * (hopefully) take care of themselves shortly.
496 */
497int
498netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit)
499{
500	struct netisr_work *npwp;
501#ifdef INVARIANTS
502	const char *name;
503#endif
504	u_int i, proto;
505
506	if (qlimit > netisr_maxqlimit)
507		return (EINVAL);
508
509	proto = nhp->nh_proto;
510#ifdef INVARIANTS
511	name = nhp->nh_name;
512#endif
513	KASSERT(proto < NETISR_MAXPROT,
514	    ("%s(%u): protocol too big for %s", __func__, proto, name));
515
516	NETISR_WLOCK();
517	KASSERT(np[proto].np_handler != NULL,
518	    ("%s(%u): protocol not registered for %s", __func__, proto,
519	    name));
520
521	np[proto].np_qlimit = qlimit;
522	for (i = 0; i < MAXCPU; i++) {
523		npwp = &nws[i].nws_work[proto];
524		npwp->nw_qlimit = qlimit;
525	}
526	NETISR_WUNLOCK();
527	return (0);
528}
529
530/*
531 * Drain all packets currently held in a particular protocol work queue.
532 */
533static void
534netisr_drain_proto(struct netisr_work *npwp)
535{
536	struct mbuf *m;
537
538	/*
539	 * We would assert the lock on the workstream but it's not passed in.
540	 */
541	while ((m = npwp->nw_head) != NULL) {
542		npwp->nw_head = m->m_nextpkt;
543		m->m_nextpkt = NULL;
544		if (npwp->nw_head == NULL)
545			npwp->nw_tail = NULL;
546		npwp->nw_len--;
547		m_freem(m);
548	}
549	KASSERT(npwp->nw_tail == NULL, ("%s: tail", __func__));
550	KASSERT(npwp->nw_len == 0, ("%s: len", __func__));
551}
552
553/*
554 * Remove the registration of a network protocol, which requires clearing
555 * per-protocol fields across all workstreams, including freeing all mbufs in
556 * the queues at time of unregister.  All work in netisr is briefly suspended
557 * while this takes place.
558 */
559void
560netisr_unregister(const struct netisr_handler *nhp)
561{
562	struct netisr_work *npwp;
563#ifdef INVARIANTS
564	const char *name;
565#endif
566	u_int i, proto;
567
568	proto = nhp->nh_proto;
569#ifdef INVARIANTS
570	name = nhp->nh_name;
571#endif
572	KASSERT(proto < NETISR_MAXPROT,
573	    ("%s(%u): protocol too big for %s", __func__, proto, name));
574
575	NETISR_WLOCK();
576	KASSERT(np[proto].np_handler != NULL,
577	    ("%s(%u): protocol not registered for %s", __func__, proto,
578	    name));
579
580	np[proto].np_name = NULL;
581	np[proto].np_handler = NULL;
582	np[proto].np_m2flow = NULL;
583	np[proto].np_m2cpuid = NULL;
584	np[proto].np_qlimit = 0;
585	np[proto].np_policy = 0;
586	for (i = 0; i < MAXCPU; i++) {
587		npwp = &nws[i].nws_work[proto];
588		netisr_drain_proto(npwp);
589		bzero(npwp, sizeof(*npwp));
590	}
591	NETISR_WUNLOCK();
592}
593
594/*
595 * Look up the workstream given a packet and source identifier.  Do this by
596 * checking the protocol's policy, and optionally call out to the protocol
597 * for assistance if required.
598 */
599static struct mbuf *
600netisr_select_cpuid(struct netisr_proto *npp, uintptr_t source,
601    struct mbuf *m, u_int *cpuidp)
602{
603	struct ifnet *ifp;
604
605	NETISR_LOCK_ASSERT();
606
607	/*
608	 * In the event we have only one worker, shortcut and deliver to it
609	 * without further ado.
610	 */
611	if (nws_count == 1) {
612		*cpuidp = nws_array[0];
613		return (m);
614	}
615
616	/*
617	 * What happens next depends on the policy selected by the protocol.
618	 * If we want to support per-interface policies, we should do that
619	 * here first.
620	 */
621	switch (npp->np_policy) {
622	case NETISR_POLICY_CPU:
623		return (npp->np_m2cpuid(m, source, cpuidp));
624
625	case NETISR_POLICY_FLOW:
626		if (!(m->m_flags & M_FLOWID) && npp->np_m2flow != NULL) {
627			m = npp->np_m2flow(m, source);
628			if (m == NULL)
629				return (NULL);
630		}
631		if (m->m_flags & M_FLOWID) {
632			*cpuidp =
633			    netisr_default_flow2cpu(m->m_pkthdr.flowid);
634			return (m);
635		}
636		/* FALLTHROUGH */
637
638	case NETISR_POLICY_SOURCE:
639		ifp = m->m_pkthdr.rcvif;
640		if (ifp != NULL)
641			*cpuidp = nws_array[(ifp->if_index + source) %
642			    nws_count];
643		else
644			*cpuidp = nws_array[source % nws_count];
645		return (m);
646
647	default:
648		panic("%s: invalid policy %u for %s", __func__,
649		    npp->np_policy, npp->np_name);
650	}
651}
652
653/*
654 * Process packets associated with a workstream and protocol.  For reasons of
655 * fairness, we process up to one complete netisr queue at a time, moving the
656 * queue to a stack-local queue for processing, but do not loop refreshing
657 * from the global queue.  The caller is responsible for deciding whether to
658 * loop, and for setting the NWS_RUNNING flag.  The passed workstream will be
659 * locked on entry and relocked before return, but will be released while
660 * processing.  The number of packets processed is returned.
661 */
662static u_int
663netisr_process_workstream_proto(struct netisr_workstream *nwsp, u_int proto)
664{
665	struct netisr_work local_npw, *npwp;
666	u_int handled;
667	struct mbuf *m;
668
669	NETISR_LOCK_ASSERT();
670	NWS_LOCK_ASSERT(nwsp);
671
672	KASSERT(nwsp->nws_flags & NWS_RUNNING,
673	    ("%s(%u): not running", __func__, proto));
674	KASSERT(proto >= 0 && proto < NETISR_MAXPROT,
675	    ("%s(%u): invalid proto\n", __func__, proto));
676
677	npwp = &nwsp->nws_work[proto];
678	if (npwp->nw_len == 0)
679		return (0);
680
681	/*
682	 * Move the global work queue to a thread-local work queue.
683	 *
684	 * Notice that this means the effective maximum length of the queue
685	 * is actually twice that of the maximum queue length specified in
686	 * the protocol registration call.
687	 */
688	handled = npwp->nw_len;
689	local_npw = *npwp;
690	npwp->nw_head = NULL;
691	npwp->nw_tail = NULL;
692	npwp->nw_len = 0;
693	nwsp->nws_pendingbits &= ~(1 << proto);
694	NWS_UNLOCK(nwsp);
695	while ((m = local_npw.nw_head) != NULL) {
696		local_npw.nw_head = m->m_nextpkt;
697		m->m_nextpkt = NULL;
698		if (local_npw.nw_head == NULL)
699			local_npw.nw_tail = NULL;
700		local_npw.nw_len--;
701		VNET_ASSERT(m->m_pkthdr.rcvif != NULL);
702		CURVNET_SET(m->m_pkthdr.rcvif->if_vnet);
703		np[proto].np_handler(m);
704		CURVNET_RESTORE();
705	}
706	KASSERT(local_npw.nw_len == 0,
707	    ("%s(%u): len %u", __func__, proto, local_npw.nw_len));
708	NWS_LOCK(nwsp);
709	npwp->nw_handled += handled;
710	return (handled);
711}
712
713/*
714 * SWI handler for netisr -- processes prackets in a set of workstreams that
715 * it owns, woken up by calls to NWS_SIGNAL().  If this workstream is already
716 * being direct dispatched, go back to sleep and wait for the dispatching
717 * thread to wake us up again.
718 */
719static void
720swi_net(void *arg)
721{
722#ifdef NETISR_LOCKING
723	struct rm_priotracker tracker;
724#endif
725	struct netisr_workstream *nwsp;
726	u_int bits, prot;
727
728	nwsp = arg;
729
730#ifdef DEVICE_POLLING
731	KASSERT(nws_count == 1,
732	    ("%s: device_polling but nws_count != 1", __func__));
733	netisr_poll();
734#endif
735#ifdef NETISR_LOCKING
736	NETISR_RLOCK(&tracker);
737#endif
738	NWS_LOCK(nwsp);
739	KASSERT(!(nwsp->nws_flags & NWS_RUNNING), ("swi_net: running"));
740	if (nwsp->nws_flags & NWS_DISPATCHING)
741		goto out;
742	nwsp->nws_flags |= NWS_RUNNING;
743	nwsp->nws_flags &= ~NWS_SCHEDULED;
744	while ((bits = nwsp->nws_pendingbits) != 0) {
745		while ((prot = ffs(bits)) != 0) {
746			prot--;
747			bits &= ~(1 << prot);
748			(void)netisr_process_workstream_proto(nwsp, prot);
749		}
750	}
751	nwsp->nws_flags &= ~NWS_RUNNING;
752out:
753	NWS_UNLOCK(nwsp);
754#ifdef NETISR_LOCKING
755	NETISR_RUNLOCK(&tracker);
756#endif
757#ifdef DEVICE_POLLING
758	netisr_pollmore();
759#endif
760}
761
762static int
763netisr_queue_workstream(struct netisr_workstream *nwsp, u_int proto,
764    struct netisr_work *npwp, struct mbuf *m, int *dosignalp)
765{
766
767	NWS_LOCK_ASSERT(nwsp);
768
769	*dosignalp = 0;
770	if (npwp->nw_len < npwp->nw_qlimit) {
771		m->m_nextpkt = NULL;
772		if (npwp->nw_head == NULL) {
773			npwp->nw_head = m;
774			npwp->nw_tail = m;
775		} else {
776			npwp->nw_tail->m_nextpkt = m;
777			npwp->nw_tail = m;
778		}
779		npwp->nw_len++;
780		if (npwp->nw_len > npwp->nw_watermark)
781			npwp->nw_watermark = npwp->nw_len;
782		nwsp->nws_pendingbits |= (1 << proto);
783		if (!(nwsp->nws_flags &
784		    (NWS_RUNNING | NWS_DISPATCHING | NWS_SCHEDULED))) {
785			nwsp->nws_flags |= NWS_SCHEDULED;
786			*dosignalp = 1;	/* Defer until unlocked. */
787		}
788		npwp->nw_queued++;
789		return (0);
790	} else {
791		npwp->nw_qdrops++;
792		return (ENOBUFS);
793	}
794}
795
796static int
797netisr_queue_internal(u_int proto, struct mbuf *m, u_int cpuid)
798{
799	struct netisr_workstream *nwsp;
800	struct netisr_work *npwp;
801	int dosignal, error;
802
803#ifdef NETISR_LOCKING
804	NETISR_LOCK_ASSERT();
805#endif
806	KASSERT(cpuid < MAXCPU, ("%s: cpuid too big (%u, %u)", __func__,
807	    cpuid, MAXCPU));
808
809	dosignal = 0;
810	error = 0;
811	nwsp = &nws[cpuid];
812	npwp = &nwsp->nws_work[proto];
813	NWS_LOCK(nwsp);
814	error = netisr_queue_workstream(nwsp, proto, npwp, m, &dosignal);
815	NWS_UNLOCK(nwsp);
816	if (dosignal)
817		NWS_SIGNAL(nwsp);
818	return (error);
819}
820
821int
822netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m)
823{
824#ifdef NETISR_LOCKING
825	struct rm_priotracker tracker;
826#endif
827	u_int cpuid;
828	int error;
829
830	KASSERT(proto < NETISR_MAXPROT,
831	    ("%s: invalid proto %u", __func__, proto));
832
833#ifdef NETISR_LOCKING
834	NETISR_RLOCK(&tracker);
835#endif
836	KASSERT(np[proto].np_handler != NULL,
837	    ("%s: invalid proto %u", __func__, proto));
838
839	m = netisr_select_cpuid(&np[proto], source, m, &cpuid);
840	if (m != NULL)
841		error = netisr_queue_internal(proto, m, cpuid);
842	else
843		error = ENOBUFS;
844#ifdef NETISR_LOCKING
845	NETISR_RUNLOCK(&tracker);
846#endif
847	return (error);
848}
849
850int
851netisr_queue(u_int proto, struct mbuf *m)
852{
853
854	return (netisr_queue_src(proto, 0, m));
855}
856
857/*
858 * Dispatch a packet for netisr processing, direct dispatch permitted by
859 * calling context.
860 */
861int
862netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m)
863{
864#ifdef NETISR_LOCKING
865	struct rm_priotracker tracker;
866#endif
867	struct netisr_workstream *nwsp;
868	struct netisr_work *npwp;
869	int dosignal, error;
870	u_int cpuid;
871
872	/*
873	 * If direct dispatch is entirely disabled, fall back on queueing.
874	 */
875	if (!netisr_direct)
876		return (netisr_queue_src(proto, source, m));
877
878	KASSERT(proto < NETISR_MAXPROT,
879	    ("%s: invalid proto %u", __func__, proto));
880#ifdef NETISR_LOCKING
881	NETISR_RLOCK(&tracker);
882#endif
883	KASSERT(np[proto].np_handler != NULL,
884	    ("%s: invalid proto %u", __func__, proto));
885
886	/*
887	 * If direct dispatch is forced, then unconditionally dispatch
888	 * without a formal CPU selection.  Borrow the current CPU's stats,
889	 * even if there's no worker on it.  In this case we don't update
890	 * nws_flags because all netisr processing will be source ordered due
891	 * to always being forced to directly dispatch.
892	 */
893	if (netisr_direct_force) {
894		nwsp = &nws[curcpu];
895		npwp = &nwsp->nws_work[proto];
896		NWS_LOCK(nwsp);
897		npwp->nw_dispatched++;
898		npwp->nw_handled++;
899		NWS_UNLOCK(nwsp);
900		np[proto].np_handler(m);
901		error = 0;
902		goto out_unlock;
903	}
904
905	/*
906	 * Otherwise, we execute in a hybrid mode where we will try to direct
907	 * dispatch if we're on the right CPU and the netisr worker isn't
908	 * already running.
909	 */
910	m = netisr_select_cpuid(&np[proto], source, m, &cpuid);
911	if (m == NULL) {
912		error = ENOBUFS;
913		goto out_unlock;
914	}
915	sched_pin();
916	if (cpuid != curcpu)
917		goto queue_fallback;
918	nwsp = &nws[cpuid];
919	npwp = &nwsp->nws_work[proto];
920
921	/*-
922	 * We are willing to direct dispatch only if three conditions hold:
923	 *
924	 * (1) The netisr worker isn't already running,
925	 * (2) Another thread isn't already directly dispatching, and
926	 * (3) The netisr hasn't already been woken up.
927	 */
928	NWS_LOCK(nwsp);
929	if (nwsp->nws_flags & (NWS_RUNNING | NWS_DISPATCHING | NWS_SCHEDULED)) {
930		error = netisr_queue_workstream(nwsp, proto, npwp, m,
931		    &dosignal);
932		NWS_UNLOCK(nws);
933		if (dosignal)
934			NWS_SIGNAL(nwsp);
935		goto out_unpin;
936	}
937
938	/*
939	 * The current thread is now effectively the netisr worker, so set
940	 * the dispatching flag to prevent concurrent processing of the
941	 * stream from another thread (even the netisr worker), which could
942	 * otherwise lead to effective misordering of the stream.
943	 */
944	nwsp->nws_flags |= NWS_DISPATCHING;
945	NWS_UNLOCK(nwsp);
946	np[proto].np_handler(m);
947	NWS_LOCK(nwsp);
948	nwsp->nws_flags &= ~NWS_DISPATCHING;
949	npwp->nw_handled++;
950	npwp->nw_hybrid_dispatched++;
951
952	/*
953	 * If other work was enqueued by another thread while we were direct
954	 * dispatching, we need to signal the netisr worker to do that work.
955	 * In the future, we might want to do some of that work in the
956	 * current thread, rather than trigger further context switches.  If
957	 * so, we'll want to establish a reasonable bound on the work done in
958	 * the "borrowed" context.
959	 */
960	if (nwsp->nws_pendingbits != 0) {
961		nwsp->nws_flags |= NWS_SCHEDULED;
962		dosignal = 1;
963	} else
964		dosignal = 0;
965	NWS_UNLOCK(nwsp);
966	if (dosignal)
967		NWS_SIGNAL(nwsp);
968	error = 0;
969	goto out_unpin;
970
971queue_fallback:
972	error = netisr_queue_internal(proto, m, cpuid);
973out_unpin:
974	sched_unpin();
975out_unlock:
976#ifdef NETISR_LOCKING
977	NETISR_RUNLOCK(&tracker);
978#endif
979	return (error);
980}
981
982int
983netisr_dispatch(u_int proto, struct mbuf *m)
984{
985
986	return (netisr_dispatch_src(proto, 0, m));
987}
988
989#ifdef DEVICE_POLLING
990/*
991 * Kernel polling borrows a netisr thread to run interface polling in; this
992 * function allows kernel polling to request that the netisr thread be
993 * scheduled even if no packets are pending for protocols.
994 */
995void
996netisr_sched_poll(void)
997{
998	struct netisr_workstream *nwsp;
999
1000	nwsp = &nws[nws_array[0]];
1001	NWS_SIGNAL(nwsp);
1002}
1003#endif
1004
1005static void
1006netisr_start_swi(u_int cpuid, struct pcpu *pc)
1007{
1008	char swiname[12];
1009	struct netisr_workstream *nwsp;
1010	int error;
1011
1012	nwsp = &nws[cpuid];
1013	mtx_init(&nwsp->nws_mtx, "netisr_mtx", NULL, MTX_DEF);
1014	nwsp->nws_cpu = cpuid;
1015	snprintf(swiname, sizeof(swiname), "netisr %u", cpuid);
1016	error = swi_add(&nwsp->nws_intr_event, swiname, swi_net, nwsp,
1017	    SWI_NET, INTR_MPSAFE, &nwsp->nws_swi_cookie);
1018	if (error)
1019		panic("%s: swi_add %d", __func__, error);
1020	pc->pc_netisr = nwsp->nws_intr_event;
1021	if (netisr_bindthreads) {
1022		error = intr_event_bind(nwsp->nws_intr_event, cpuid);
1023		if (error != 0)
1024			printf("%s: cpu %u: intr_event_bind: %d", __func__,
1025			    cpuid, error);
1026	}
1027	NETISR_WLOCK();
1028	nws_array[nws_count] = nwsp->nws_cpu;
1029	nws_count++;
1030	NETISR_WUNLOCK();
1031}
1032
1033/*
1034 * Initialize the netisr subsystem.  We rely on BSS and static initialization
1035 * of most fields in global data structures.
1036 *
1037 * Start a worker thread for the boot CPU so that we can support network
1038 * traffic immediately in case the network stack is used before additional
1039 * CPUs are started (for example, diskless boot).
1040 */
1041static void
1042netisr_init(void *arg)
1043{
1044
1045	KASSERT(curcpu == 0, ("%s: not on CPU 0", __func__));
1046
1047	NETISR_LOCK_INIT();
1048	if (netisr_maxthreads < 1)
1049		netisr_maxthreads = 1;
1050	if (netisr_maxthreads > MAXCPU)
1051		netisr_maxthreads = MAXCPU;
1052	if (netisr_defaultqlimit > netisr_maxqlimit)
1053		netisr_defaultqlimit = netisr_maxqlimit;
1054#ifdef DEVICE_POLLING
1055	/*
1056	 * The device polling code is not yet aware of how to deal with
1057	 * multiple netisr threads, so for the time being compiling in device
1058	 * polling disables parallel netisr workers.
1059	 */
1060	netisr_maxthreads = 1;
1061	netisr_bindthreads = 0;
1062#endif
1063
1064	netisr_start_swi(curcpu, pcpu_find(curcpu));
1065}
1066SYSINIT(netisr_init, SI_SUB_SOFTINTR, SI_ORDER_FIRST, netisr_init, NULL);
1067
1068/*
1069 * Start worker threads for additional CPUs.  No attempt to gracefully handle
1070 * work reassignment, we don't yet support dynamic reconfiguration.
1071 */
1072static void
1073netisr_start(void *arg)
1074{
1075	struct pcpu *pc;
1076
1077	SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
1078		if (nws_count >= netisr_maxthreads)
1079			break;
1080		/* XXXRW: Is skipping absent CPUs still required here? */
1081		if (CPU_ABSENT(pc->pc_cpuid))
1082			continue;
1083		/* Worker will already be present for boot CPU. */
1084		if (pc->pc_netisr != NULL)
1085			continue;
1086		netisr_start_swi(pc->pc_cpuid, pc);
1087	}
1088}
1089SYSINIT(netisr_start, SI_SUB_SMP, SI_ORDER_MIDDLE, netisr_start, NULL);
1090
1091#ifdef DDB
1092DB_SHOW_COMMAND(netisr, db_show_netisr)
1093{
1094	struct netisr_workstream *nwsp;
1095	struct netisr_work *nwp;
1096	int first, proto;
1097	u_int cpu;
1098
1099	db_printf("%3s %6s %5s %5s %5s %8s %8s %8s %8s\n", "CPU", "Proto",
1100	    "Len", "WMark", "Max", "Disp", "HDisp", "Drop", "Queue");
1101	for (cpu = 0; cpu < MAXCPU; cpu++) {
1102		nwsp = &nws[cpu];
1103		if (nwsp->nws_intr_event == NULL)
1104			continue;
1105		first = 1;
1106		for (proto = 0; proto < NETISR_MAXPROT; proto++) {
1107			if (np[proto].np_handler == NULL)
1108				continue;
1109			nwp = &nwsp->nws_work[proto];
1110			if (first) {
1111				db_printf("%3d ", cpu);
1112				first = 0;
1113			} else
1114				db_printf("%3s ", "");
1115			db_printf(
1116			    "%6s %5d %5d %5d %8ju %8ju %8ju %8ju\n",
1117			    np[proto].np_name, nwp->nw_len,
1118			    nwp->nw_watermark, nwp->nw_qlimit,
1119			    nwp->nw_dispatched, nwp->nw_hybrid_dispatched,
1120			    nwp->nw_qdrops, nwp->nw_queued);
1121		}
1122	}
1123}
1124#endif
1125