source: rtems-libbsd/freebsd/net/netisr.c @ 31230c0

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 31230c0 was 31230c0, checked in by Jennifer Averett <jennifer.averett@…>, on 04/18/12 at 17:35:01

Commented out calls to pin and unpin.

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