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, ("netisr_register: bad flags 0x%x\n", flags)); 82 netisrs[num].ni_handler = handler; 83 netisrs[num].ni_queue = inq; 84 netisrs[num].ni_flags = flags; 85 } 86 87 void 88 netisr_unregister(int num) 89 { 90 struct netisr *ni; 91 92 KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))), 93 ("bad isr %d", num)); 94 ni = &netisrs[num]; 95 ni->ni_handler = NULL; 96 if (ni->ni_queue != NULL) 97 IF_DRAIN(ni->ni_queue); 98 ni->ni_queue = NULL; 99 } 100 101 struct isrstat { 102 int isrs_count; /* dispatch count */ 103 int isrs_directed; /* ...directly dispatched */ 104 int isrs_deferred; /* ...queued instead */ 105 int isrs_queued; /* intentionally queueued */ 106 int isrs_drop; /* dropped 'cuz no handler */ 107 int isrs_swi_count; /* swi_net handlers called */ 108 }; 109 static struct isrstat isrstat; 110 111 SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW, 0, "netisr counters"); 112 113 static int netisr_direct = 1; 114 SYSCTL_INT(_net_isr, OID_AUTO, direct, CTLFLAG_RW, 115 &netisr_direct, 0, "enable direct dispatch"); 116 TUNABLE_INT("net.isr.direct", &netisr_direct); 117 118 SYSCTL_INT(_net_isr, OID_AUTO, count, CTLFLAG_RD, 119 &isrstat.isrs_count, 0, ""); 120 SYSCTL_INT(_net_isr, OID_AUTO, directed, CTLFLAG_RD, 121 &isrstat.isrs_directed, 0, ""); 122 SYSCTL_INT(_net_isr, OID_AUTO, deferred, CTLFLAG_RD, 123 &isrstat.isrs_deferred, 0, ""); 124 SYSCTL_INT(_net_isr, OID_AUTO, queued, CTLFLAG_RD, 125 &isrstat.isrs_queued, 0, ""); 126 SYSCTL_INT(_net_isr, OID_AUTO, drop, CTLFLAG_RD, 127 &isrstat.isrs_drop, 0, ""); 128 SYSCTL_INT(_net_isr, OID_AUTO, swi_count, CTLFLAG_RD, 129 &isrstat.isrs_swi_count, 0, ""); 130 131 /* 132 * Process all packets currently present in a netisr queue. Used to 133 * drain an existing set of packets waiting for processing when we 134 * begin direct dispatch, to avoid processing packets out of order. 135 */ 136 static void 137 netisr_processqueue(struct netisr *ni) 138 { 139 struct mbuf *m; 140 141 for (;;) { 142 IF_DEQUEUE(ni->ni_queue, m); 143 if (m == NULL) 144 break; 145 VNET_ASSERT(m->m_pkthdr.rcvif != NULL); 146 CURVNET_SET(m->m_pkthdr.rcvif->if_vnet); 147 ni->ni_handler(m); 148 CURVNET_RESTORE(); 149 } 150 } 151 152 /* 153 * Call the netisr directly instead of queueing the packet, if possible. 154 */ 155 void 156 netisr_dispatch(int num, struct mbuf *m) 157 { 158 struct netisr *ni; 159 160 isrstat.isrs_count++; /* XXX redundant */ 161 KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))), 162 ("bad isr %d", num)); 163 ni = &netisrs[num]; 164 if (ni->ni_queue == NULL) { 165 isrstat.isrs_drop++; 166 m_freem(m); 167 return; 168 } 169 170 /* 171 * Directly dispatch handling of this packet, if permitted by global 172 * policy. Source ordering is maintained by virtue of callers 173 * consistently calling one of queued or direct dispatch. 174 */ 175 if (netisr_direct) { 176 isrstat.isrs_directed++; 177 ni->ni_handler(m); 178 } else { 179 isrstat.isrs_deferred++; 180 if (IF_HANDOFF(ni->ni_queue, m, NULL)) 181 schednetisr(num); 182 } 183 } 184 185 /* 186 * Same as above, but always queue. 187 * This is either used in places where we are not confident that 188 * direct dispatch is possible, or where queueing is required. 189 * It returns (0) on success and ERRNO on failure. On failure the 190 * mbuf has been free'd. 191 */ 192 int 193 netisr_queue(int num, struct mbuf *m) 194 { 195 struct netisr *ni; 196 197 KASSERT(!(num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs))), 198 ("bad isr %d", num)); 199 ni = &netisrs[num]; 200 if (ni->ni_queue == NULL) { 201 isrstat.isrs_drop++; 202 m_freem(m); 203 return (ENXIO); 204 } 205 isrstat.isrs_queued++; 206 if (!IF_HANDOFF(ni->ni_queue, m, NULL)) 207 return (ENOBUFS); /* IF_HANDOFF has free'd the mbuf */ 208 schednetisr(num); 209 return (0); 210 } 211 212 static void 213 swi_net(void *dummy) 214 { 215 struct netisr *ni; 216 u_int bits; 217 int i; 218 #ifdef DEVICE_POLLING 219 const int polling = 1; 220 #else 221 const int polling = 0; 222 #endif 223 224 do { 225 bits = atomic_readandclear_int(&netisr); 226 if (bits == 0) 227 break; 228 while ((i = ffs(bits)) != 0) { 229 isrstat.isrs_swi_count++; 230 i--; 231 bits &= ~(1 << i); 232 ni = &netisrs[i]; 233 if (ni->ni_handler == NULL) { 234 printf("swi_net: unregistered isr %d.\n", i); 235 continue; 236 } 237 if (ni->ni_queue == NULL) 238 ni->ni_handler(NULL); 239 else 240 netisr_processqueue(ni); 241 } 242 } while (polling); 243 } 244 245 static void 246 start_netisr(void *dummy) 247 { 248 249 if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, INTR_MPSAFE, &net_ih)) 250 panic("start_netisr"); 251 } 252 SYSINIT(start_netisr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_netisr, NULL); 253