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 <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 */ 61 int debug_mpsafenet = 0; 62 TUNABLE_INT("debug.mpsafenet", &debug_mpsafenet); 63 SYSCTL_INT(_debug, OID_AUTO, mpsafenet, CTLFLAG_RD, &debug_mpsafenet, 0, 64 "Enable/disable MPSAFE network support"); 65 66 volatile unsigned int netisr; /* scheduling bits for network */ 67 68 struct netisr { 69 netisr_t *ni_handler; 70 struct ifqueue *ni_queue; 71 } netisrs[32]; 72 73 static struct mtx netisr_mtx; 74 static void *net_ih; 75 76 void 77 legacy_setsoftnet(void) 78 { 79 swi_sched(net_ih, 0); 80 } 81 82 void 83 netisr_register(int num, netisr_t *handler, struct ifqueue *inq) 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 } 91 92 void 93 netisr_unregister(int num) 94 { 95 struct netisr *ni; 96 int s; 97 98 KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))), 99 ("bad isr %d", num)); 100 ni = &netisrs[num]; 101 ni->ni_handler = NULL; 102 if (ni->ni_queue != NULL) { 103 s = splimp(); 104 IF_DRAIN(ni->ni_queue); 105 splx(s); 106 } 107 } 108 109 struct isrstat { 110 int isrs_count; /* dispatch count */ 111 int isrs_directed; /* ...successfully dispatched */ 112 int isrs_deferred; /* ...queued instead */ 113 int isrs_queued; /* intentionally queueued */ 114 int isrs_swi_count; /* swi_net handlers called */ 115 }; 116 static struct isrstat isrstat; 117 118 SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW, 0, "netisr counters"); 119 120 static int netisr_enable = 0; 121 SYSCTL_INT(_net_isr, OID_AUTO, enable, CTLFLAG_RW, 122 &netisr_enable, 0, "enable direct dispatch"); 123 TUNABLE_INT("net.isr.enable", &netisr_enable); 124 125 SYSCTL_INT(_net_isr, OID_AUTO, count, CTLFLAG_RD, 126 &isrstat.isrs_count, 0, ""); 127 SYSCTL_INT(_net_isr, OID_AUTO, directed, CTLFLAG_RD, 128 &isrstat.isrs_directed, 0, ""); 129 SYSCTL_INT(_net_isr, OID_AUTO, deferred, CTLFLAG_RD, 130 &isrstat.isrs_deferred, 0, ""); 131 SYSCTL_INT(_net_isr, OID_AUTO, queued, CTLFLAG_RD, 132 &isrstat.isrs_queued, 0, ""); 133 SYSCTL_INT(_net_isr, OID_AUTO, swi_count, CTLFLAG_RD, 134 &isrstat.isrs_swi_count, 0, ""); 135 136 /* 137 * Process all packets currently present in a netisr queue. Used to 138 * drain an existing set of packets waiting for processing when we 139 * begin direct dispatch, to avoid processing packets out of order. 140 */ 141 static void 142 netisr_processqueue(struct netisr *ni) 143 { 144 struct mbuf *m; 145 146 for (;;) { 147 IF_DEQUEUE(ni->ni_queue, m); 148 if (m == NULL) 149 break; 150 ni->ni_handler(m); 151 } 152 } 153 154 /* 155 * Call the netisr directly instead of queueing the packet, if possible. 156 * 157 * Ideally, the permissibility of calling the routine would be determined 158 * by checking if splnet() was asserted at the time the device interrupt 159 * occurred; if so, this indicates that someone is in the network stack. 160 * 161 * However, bus_setup_intr uses INTR_TYPE_NET, which sets splnet before 162 * calling the interrupt handler, so the previous mask is unavailable. 163 * Approximate this by checking intr_nesting_level instead; if any SWI 164 * handlers are running, the packet is queued instead. 165 */ 166 void 167 netisr_dispatch(int num, struct mbuf *m) 168 { 169 struct netisr *ni; 170 171 isrstat.isrs_count++; 172 KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))), 173 ("bad isr %d", num)); 174 ni = &netisrs[num]; 175 if (ni->ni_queue == NULL) { 176 m_freem(m); 177 return; 178 } 179 if (netisr_enable && mtx_trylock(&netisr_mtx)) { 180 isrstat.isrs_directed++; 181 /* 182 * One slight problem here is that packets might bypass 183 * each other in the stack, if an earlier one happened 184 * to get stuck in the queue. 185 * 186 * we can either: 187 * a. drain the queue before handling this packet, 188 * b. fallback to queueing the packet, 189 * c. sweep the issue under the rug and ignore it. 190 * 191 * Currently, we do a). Previously, we did c). 192 */ 193 netisr_processqueue(ni); 194 ni->ni_handler(m); 195 mtx_unlock(&netisr_mtx); 196 } else { 197 isrstat.isrs_deferred++; 198 if (IF_HANDOFF(ni->ni_queue, m, NULL)) 199 schednetisr(num); 200 } 201 } 202 203 /* 204 * Same as above, but always queue. 205 * This is either used in places where we are not confident that 206 * direct dispatch is possible, or where queueing is required. 207 */ 208 int 209 netisr_queue(int num, struct mbuf *m) 210 { 211 struct netisr *ni; 212 213 KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))), 214 ("bad isr %d", num)); 215 ni = &netisrs[num]; 216 if (ni->ni_queue == NULL) { 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 227 static void 228 swi_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 mtx_lock(&netisr_mtx); 240 do { 241 bits = atomic_readandclear_int(&netisr); 242 if (bits == 0) 243 break; 244 while ((i = ffs(bits)) != 0) { 245 isrstat.isrs_swi_count++; 246 i--; 247 bits &= ~(1 << i); 248 ni = &netisrs[i]; 249 if (ni->ni_handler == NULL) { 250 printf("swi_net: unregistered isr %d.\n", i); 251 continue; 252 } 253 if (ni->ni_queue == NULL) 254 ni->ni_handler(NULL); 255 else 256 netisr_processqueue(ni); 257 } 258 } while (polling); 259 mtx_unlock(&netisr_mtx); 260 } 261 262 static void 263 start_netisr(void *dummy) 264 { 265 266 mtx_init(&netisr_mtx, "netisr lock", NULL, MTX_DEF); 267 if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, 0, &net_ih)) 268 panic("start_netisr"); 269 } 270 SYSINIT(start_netisr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_netisr, NULL) 271