netisr.c revision 122320
1/*-
2 * Copyright (c) 2001,2002,2003 Jonathan Lemon <jlemon@FreeBSD.org>
3 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/net/netisr.c 122320 2003-11-08 22:28:40Z sam $
28 */
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/rtprio.h>
33#include <sys/systm.h>
34#include <sys/interrupt.h>
35#include <sys/kernel.h>
36#include <sys/kthread.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/proc.h>
40#include <sys/random.h>
41#include <sys/resourcevar.h>
42#include <sys/sysctl.h>
43#include <sys/unistd.h>
44#include <machine/atomic.h>
45#include <machine/cpu.h>
46#include <machine/stdarg.h>
47
48#include <sys/mbuf.h>
49#include <sys/socket.h>
50
51#include <net/if.h>
52#include <net/if_types.h>
53#include <net/if_var.h>
54#include <net/netisr.h>
55
56/*
57 * XXX this is a temporary measure to allow folks to
58 * XXX disable Giant locking in the network code without
59 * XXX recompiling--in case of problems.
60 */
61int	debug_mpsafenet = 0;
62TUNABLE_INT("debug.mpsafenet", &debug_mpsafenet);
63SYSCTL_INT(_debug, OID_AUTO, mpsafenet, CTLFLAG_RD, &debug_mpsafenet, 0,
64    "Enable/disable MPSAFE network support");
65
66volatile unsigned int	netisr;	/* scheduling bits for network */
67
68struct netisr {
69	netisr_t	*ni_handler;
70	struct ifqueue	*ni_queue;
71	int		ni_flags;
72} netisrs[32];
73
74static void *net_ih;
75
76void
77legacy_setsoftnet(void)
78{
79	swi_sched(net_ih, 0);
80}
81
82void
83netisr_register(int num, netisr_t *handler, struct ifqueue *inq, int flags)
84{
85
86	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
87	    ("bad isr %d", num));
88	netisrs[num].ni_handler = handler;
89	netisrs[num].ni_queue = inq;
90	if ((flags & NETISR_MPSAFE) && !debug_mpsafenet)
91		flags &= ~NETISR_MPSAFE;
92	netisrs[num].ni_flags = flags;
93}
94
95void
96netisr_unregister(int num)
97{
98	struct netisr *ni;
99
100	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
101	    ("bad isr %d", num));
102	ni = &netisrs[num];
103	ni->ni_handler = NULL;
104	if (ni->ni_queue != NULL)
105		IF_DRAIN(ni->ni_queue);
106}
107
108struct isrstat {
109	int	isrs_count;			/* dispatch count */
110	int	isrs_directed;			/* ...directly dispatched */
111	int	isrs_deferred;			/* ...queued instead */
112	int	isrs_queued;			/* intentionally queueued */
113	int	isrs_drop;			/* dropped 'cuz no handler */
114	int	isrs_swi_count;			/* swi_net handlers called */
115};
116static struct isrstat isrstat;
117
118SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW, 0, "netisr counters");
119
120static int	netisr_enable = 0;
121SYSCTL_INT(_net_isr, OID_AUTO, enable, CTLFLAG_RW,
122    &netisr_enable, 0, "enable direct dispatch");
123TUNABLE_INT("net.isr.enable", &netisr_enable);
124
125SYSCTL_INT(_net_isr, OID_AUTO, count, CTLFLAG_RD,
126    &isrstat.isrs_count, 0, "");
127SYSCTL_INT(_net_isr, OID_AUTO, directed, CTLFLAG_RD,
128    &isrstat.isrs_directed, 0, "");
129SYSCTL_INT(_net_isr, OID_AUTO, deferred, CTLFLAG_RD,
130    &isrstat.isrs_deferred, 0, "");
131SYSCTL_INT(_net_isr, OID_AUTO, queued, CTLFLAG_RD,
132    &isrstat.isrs_queued, 0, "");
133SYSCTL_INT(_net_isr, OID_AUTO, drop, CTLFLAG_RD,
134    &isrstat.isrs_drop, 0, "");
135SYSCTL_INT(_net_isr, OID_AUTO, swi_count, CTLFLAG_RD,
136    &isrstat.isrs_swi_count, 0, "");
137
138/*
139 * Process all packets currently present in a netisr queue.  Used to
140 * drain an existing set of packets waiting for processing when we
141 * begin direct dispatch, to avoid processing packets out of order.
142 */
143static void
144netisr_processqueue(struct netisr *ni)
145{
146	struct mbuf *m;
147
148	for (;;) {
149		IF_DEQUEUE(ni->ni_queue, m);
150		if (m == NULL)
151			break;
152		ni->ni_handler(m);
153	}
154}
155
156/*
157 * Call the netisr directly instead of queueing the packet, if possible.
158 */
159void
160netisr_dispatch(int num, struct mbuf *m)
161{
162	struct netisr *ni;
163
164	isrstat.isrs_count++;		/* XXX redundant */
165	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
166	    ("bad isr %d", num));
167	ni = &netisrs[num];
168	if (ni->ni_queue == NULL) {
169		isrstat.isrs_drop++;
170		m_freem(m);
171		return;
172	}
173	/*
174	 * Do direct dispatch only for MPSAFE netisrs (and
175	 * only when enabled).  Note that when a netisr is
176	 * marked MPSAFE we permit multiple concurrent instances
177	 * to run.  We guarantee only the order in which
178	 * packets are processed for each "dispatch point" in
179	 * the system (i.e. call to netisr_dispatch or
180	 * netisr_queue).  This insures ordering of packets
181	 * from an interface but does not guarantee ordering
182	 * between multiple places in the system (e.g. IP
183	 * dispatched from interfaces vs. IP queued from IPSec).
184	 */
185	if (netisr_enable && (ni->ni_flags & NETISR_MPSAFE)) {
186		isrstat.isrs_directed++;
187		/*
188		 * NB: We used to drain the queue before handling
189		 * the packet but now do not.  Doing so here will
190		 * not preserve ordering so instead we fallback to
191		 * guaranteeing order only from dispatch points
192		 * in the system (see above).
193		 */
194		ni->ni_handler(m);
195	} else {
196		isrstat.isrs_deferred++;
197		if (IF_HANDOFF(ni->ni_queue, m, NULL))
198			schednetisr(num);
199	}
200}
201
202/*
203 * Same as above, but always queue.
204 * This is either used in places where we are not confident that
205 * direct dispatch is possible, or where queueing is required.
206 */
207int
208netisr_queue(int num, struct mbuf *m)
209{
210	struct netisr *ni;
211
212	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
213	    ("bad isr %d", num));
214	ni = &netisrs[num];
215	if (ni->ni_queue == NULL) {
216		isrstat.isrs_drop++;
217		m_freem(m);
218		return (1);
219	}
220	isrstat.isrs_queued++;
221	if (!IF_HANDOFF(ni->ni_queue, m, NULL))
222		return (0);
223	schednetisr(num);
224	return (1);
225}
226
227static void
228swi_net(void *dummy)
229{
230	struct netisr *ni;
231	u_int bits;
232	int i;
233#ifdef DEVICE_POLLING
234	const int polling = 1;
235#else
236	const int polling = 0;
237#endif
238
239	do {
240		bits = atomic_readandclear_int(&netisr);
241		if (bits == 0)
242			break;
243		while ((i = ffs(bits)) != 0) {
244			isrstat.isrs_swi_count++;
245			i--;
246			bits &= ~(1 << i);
247			ni = &netisrs[i];
248			if (ni->ni_handler == NULL) {
249				printf("swi_net: unregistered isr %d.\n", i);
250				continue;
251			}
252			if ((ni->ni_flags & NETISR_MPSAFE) == 0) {
253				mtx_lock(&Giant);
254				if (ni->ni_queue == NULL)
255					ni->ni_handler(NULL);
256				else
257					netisr_processqueue(ni);
258				mtx_unlock(&Giant);
259			} else {
260				if (ni->ni_queue == NULL)
261					ni->ni_handler(NULL);
262				else
263					netisr_processqueue(ni);
264			}
265		}
266	} while (polling);
267}
268
269static void
270start_netisr(void *dummy)
271{
272
273	if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, INTR_MPSAFE, &net_ih))
274		panic("start_netisr");
275}
276SYSINIT(start_netisr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_netisr, NULL)
277