xref: /freebsd/sys/net/netisr.c (revision 82431678fce5c893ef9c7418ad6d998ad4187de6)
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$
28  */
29 
30 #include "opt_device_polling.h"
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/rtprio.h>
35 #include <sys/systm.h>
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/random.h>
43 #include <sys/resourcevar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/vimage.h>
47 #include <machine/atomic.h>
48 #include <machine/cpu.h>
49 #include <machine/stdarg.h>
50 
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 
54 #include <net/if.h>
55 #include <net/if_types.h>
56 #include <net/if_var.h>
57 #include <net/netisr.h>
58 
59 volatile unsigned int	netisr;	/* scheduling bits for network */
60 
61 struct netisr {
62 	netisr_t	*ni_handler;
63 	struct ifqueue	*ni_queue;
64 	int		ni_flags;
65 } netisrs[32];
66 
67 static void *net_ih;
68 
69 void
70 legacy_setsoftnet(void)
71 {
72 	swi_sched(net_ih, 0);
73 }
74 
75 void
76 netisr_register(int num, netisr_t *handler, struct ifqueue *inq, int flags)
77 {
78 
79 	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
80 	    ("bad isr %d", num));
81 	KASSERT(flags == 0 || flags == NETISR_FORCEQUEUE,
82 	    ("netisr_register: bad flags 0x%x\n", flags));
83 	netisrs[num].ni_handler = handler;
84 	netisrs[num].ni_queue = inq;
85 	netisrs[num].ni_flags = flags;
86 }
87 
88 void
89 netisr_unregister(int num)
90 {
91 	struct netisr *ni;
92 
93 	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
94 	    ("bad isr %d", num));
95 	ni = &netisrs[num];
96 	ni->ni_handler = NULL;
97 	if (ni->ni_queue != NULL)
98 		IF_DRAIN(ni->ni_queue);
99 	ni->ni_queue = NULL;
100 }
101 
102 struct isrstat {
103 	int	isrs_count;			/* dispatch count */
104 	int	isrs_directed;			/* ...directly dispatched */
105 	int	isrs_deferred;			/* ...queued instead */
106 	int	isrs_queued;			/* intentionally queueued */
107 	int	isrs_drop;			/* dropped 'cuz no handler */
108 	int	isrs_swi_count;			/* swi_net handlers called */
109 };
110 static struct isrstat isrstat;
111 
112 SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW, 0, "netisr counters");
113 
114 static int	netisr_direct = 1;
115 SYSCTL_INT(_net_isr, OID_AUTO, direct, CTLFLAG_RW,
116     &netisr_direct, 0, "enable direct dispatch");
117 TUNABLE_INT("net.isr.direct", &netisr_direct);
118 
119 SYSCTL_INT(_net_isr, OID_AUTO, count, CTLFLAG_RD,
120     &isrstat.isrs_count, 0, "");
121 SYSCTL_INT(_net_isr, OID_AUTO, directed, CTLFLAG_RD,
122     &isrstat.isrs_directed, 0, "");
123 SYSCTL_INT(_net_isr, OID_AUTO, deferred, CTLFLAG_RD,
124     &isrstat.isrs_deferred, 0, "");
125 SYSCTL_INT(_net_isr, OID_AUTO, queued, CTLFLAG_RD,
126     &isrstat.isrs_queued, 0, "");
127 SYSCTL_INT(_net_isr, OID_AUTO, drop, CTLFLAG_RD,
128     &isrstat.isrs_drop, 0, "");
129 SYSCTL_INT(_net_isr, OID_AUTO, swi_count, CTLFLAG_RD,
130     &isrstat.isrs_swi_count, 0, "");
131 
132 /*
133  * Process all packets currently present in a netisr queue.  Used to
134  * drain an existing set of packets waiting for processing when we
135  * begin direct dispatch, to avoid processing packets out of order.
136  */
137 static void
138 netisr_processqueue(struct netisr *ni)
139 {
140 	struct mbuf *m;
141 
142 	for (;;) {
143 		IF_DEQUEUE(ni->ni_queue, m);
144 		if (m == NULL)
145 			break;
146 		VNET_ASSERT(m->m_pkthdr.rcvif != NULL);
147 		CURVNET_SET(m->m_pkthdr.rcvif->if_vnet);
148 		ni->ni_handler(m);
149 		CURVNET_RESTORE();
150 	}
151 }
152 
153 /*
154  * Call the netisr directly instead of queueing the packet, if possible.
155  */
156 void
157 netisr_dispatch(int num, struct mbuf *m)
158 {
159 	struct netisr *ni;
160 
161 	isrstat.isrs_count++;		/* XXX redundant */
162 	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
163 	    ("bad isr %d", num));
164 	ni = &netisrs[num];
165 	if (ni->ni_queue == NULL) {
166 		isrstat.isrs_drop++;
167 		m_freem(m);
168 		return;
169 	}
170 
171 	/*
172 	 * Unless NETISR_FORCEQUEUE is set on the netisr (generally
173 	 * indicating that the handler still requires Giant, which cannot be
174 	 * acquired in arbitrary order with respect to a caller), directly
175 	 * dispatch handling of this packet.  Source ordering is maintained
176 	 * by virtue of callers consistently calling one of queued or direct
177 	 * dispatch, and the forcequeue flag being immutable after
178 	 * registration.
179 	 */
180 	if (netisr_direct && !(ni->ni_flags & NETISR_FORCEQUEUE)) {
181 		isrstat.isrs_directed++;
182 		ni->ni_handler(m);
183 	} else {
184 		isrstat.isrs_deferred++;
185 		if (IF_HANDOFF(ni->ni_queue, m, NULL))
186 			schednetisr(num);
187 	}
188 }
189 
190 /*
191  * Same as above, but always queue.
192  * This is either used in places where we are not confident that
193  * direct dispatch is possible, or where queueing is required.
194  * It returns (0) on success and ERRNO on failure.  On failure the
195  * mbuf has been free'd.
196  */
197 int
198 netisr_queue(int num, struct mbuf *m)
199 {
200 	struct netisr *ni;
201 
202 	KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))),
203 	    ("bad isr %d", num));
204 	ni = &netisrs[num];
205 	if (ni->ni_queue == NULL) {
206 		isrstat.isrs_drop++;
207 		m_freem(m);
208 		return (ENXIO);
209 	}
210 	isrstat.isrs_queued++;
211 	if (!IF_HANDOFF(ni->ni_queue, m, NULL))
212 		return (ENOBUFS);	/* IF_HANDOFF has free'd the mbuf */
213 	schednetisr(num);
214 	return (0);
215 }
216 
217 static void
218 swi_net(void *dummy)
219 {
220 	struct netisr *ni;
221 	u_int bits;
222 	int i;
223 #ifdef DEVICE_POLLING
224 	const int polling = 1;
225 #else
226 	const int polling = 0;
227 #endif
228 
229 	do {
230 		bits = atomic_readandclear_int(&netisr);
231 		if (bits == 0)
232 			break;
233 		while ((i = ffs(bits)) != 0) {
234 			isrstat.isrs_swi_count++;
235 			i--;
236 			bits &= ~(1 << i);
237 			ni = &netisrs[i];
238 			if (ni->ni_handler == NULL) {
239 				printf("swi_net: unregistered isr %d.\n", i);
240 				continue;
241 			}
242 			if (ni->ni_queue == NULL)
243 				ni->ni_handler(NULL);
244 			else
245 				netisr_processqueue(ni);
246 		}
247 	} while (polling);
248 }
249 
250 static void
251 start_netisr(void *dummy)
252 {
253 
254 	if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, INTR_MPSAFE, &net_ih))
255 		panic("start_netisr");
256 }
257 SYSINIT(start_netisr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_netisr, NULL);
258