1e4fc250cSLuigi Rizzo /*- 2daccb638SLuigi Rizzo * Copyright (c) 2001-2002 Luigi Rizzo 3daccb638SLuigi Rizzo * 4daccb638SLuigi Rizzo * Supported by: the Xorp Project (www.xorp.org) 5e4fc250cSLuigi Rizzo * 6e4fc250cSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 7e4fc250cSLuigi Rizzo * modification, are permitted provided that the following conditions 8e4fc250cSLuigi Rizzo * are met: 9e4fc250cSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 10e4fc250cSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 11e4fc250cSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 12e4fc250cSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 13e4fc250cSLuigi Rizzo * documentation and/or other materials provided with the distribution. 14e4fc250cSLuigi Rizzo * 15e4fc250cSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16e4fc250cSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17e4fc250cSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18e4fc250cSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19e4fc250cSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20e4fc250cSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21e4fc250cSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22e4fc250cSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23e4fc250cSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24e4fc250cSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25e4fc250cSLuigi Rizzo * SUCH DAMAGE. 26e4fc250cSLuigi Rizzo */ 27e4fc250cSLuigi Rizzo 28677b542eSDavid E. O'Brien #include <sys/cdefs.h> 29677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 30677b542eSDavid E. O'Brien 31e4fc250cSLuigi Rizzo #include <sys/param.h> 32e4fc250cSLuigi Rizzo #include <sys/systm.h> 33e4fc250cSLuigi Rizzo #include <sys/kernel.h> 34e4fc250cSLuigi Rizzo #include <sys/socket.h> /* needed by net/if.h */ 35e4fc250cSLuigi Rizzo #include <sys/sysctl.h> 36e4fc250cSLuigi Rizzo 37e4fc250cSLuigi Rizzo #include <net/if.h> /* for IFF_* flags */ 38e4fc250cSLuigi Rizzo #include <net/netisr.h> /* for NETISR_POLL */ 39e4fc250cSLuigi Rizzo 40d105c784SLuigi Rizzo #include <sys/proc.h> 41d105c784SLuigi Rizzo #include <sys/resourcevar.h> 42d105c784SLuigi Rizzo #include <sys/kthread.h> 43d105c784SLuigi Rizzo 44e4fc250cSLuigi Rizzo #ifdef SMP 452dbd9d5bSLuigi Rizzo #ifndef COMPILING_LINT 46e4fc250cSLuigi Rizzo #error DEVICE_POLLING is not compatible with SMP 47e4fc250cSLuigi Rizzo #endif 482dbd9d5bSLuigi Rizzo #endif 49e4fc250cSLuigi Rizzo 50daccb638SLuigi Rizzo static void netisr_poll(void); /* the two netisr handlers */ 511cafed39SJonathan Lemon static void netisr_pollmore(void); 52daccb638SLuigi Rizzo 53daccb638SLuigi Rizzo void hardclock_device_poll(void); /* hook from hardclock */ 54e4fc250cSLuigi Rizzo void ether_poll(int); /* polling while in trap */ 55e4fc250cSLuigi Rizzo 56e4fc250cSLuigi Rizzo /* 57e4fc250cSLuigi Rizzo * Polling support for [network] device drivers. 58e4fc250cSLuigi Rizzo * 59e4fc250cSLuigi Rizzo * Drivers which support this feature try to register with the 60e4fc250cSLuigi Rizzo * polling code. 61e4fc250cSLuigi Rizzo * 62e4fc250cSLuigi Rizzo * If registration is successful, the driver must disable interrupts, 63e4fc250cSLuigi Rizzo * and further I/O is performed through the handler, which is invoked 64e4fc250cSLuigi Rizzo * (at least once per clock tick) with 3 arguments: the "arg" passed at 65e4fc250cSLuigi Rizzo * register time (a struct ifnet pointer), a command, and a "count" limit. 66e4fc250cSLuigi Rizzo * 67e4fc250cSLuigi Rizzo * The command can be one of the following: 68e4fc250cSLuigi Rizzo * POLL_ONLY: quick move of "count" packets from input/output queues. 69e4fc250cSLuigi Rizzo * POLL_AND_CHECK_STATUS: as above, plus check status registers or do 70e4fc250cSLuigi Rizzo * other more expensive operations. This command is issued periodically 71e4fc250cSLuigi Rizzo * but less frequently than POLL_ONLY. 72e4fc250cSLuigi Rizzo * POLL_DEREGISTER: deregister and return to interrupt mode. 73e4fc250cSLuigi Rizzo * 74e4fc250cSLuigi Rizzo * The first two commands are only issued if the interface is marked as 75e4fc250cSLuigi Rizzo * 'IFF_UP and IFF_RUNNING', the last one only if IFF_RUNNING is set. 76e4fc250cSLuigi Rizzo * 77e4fc250cSLuigi Rizzo * The count limit specifies how much work the handler can do during the 78e4fc250cSLuigi Rizzo * call -- typically this is the number of packets to be received, or 79e4fc250cSLuigi Rizzo * transmitted, etc. (drivers are free to interpret this number, as long 80e4fc250cSLuigi Rizzo * as the max time spent in the function grows roughly linearly with the 81e4fc250cSLuigi Rizzo * count). 82e4fc250cSLuigi Rizzo * 83e4fc250cSLuigi Rizzo * Deregistration can be requested by the driver itself (typically in the 84e4fc250cSLuigi Rizzo * *_stop() routine), or by the polling code, by invoking the handler. 85e4fc250cSLuigi Rizzo * 86e4fc250cSLuigi Rizzo * Polling can be globally enabled or disabled with the sysctl variable 87e4fc250cSLuigi Rizzo * kern.polling.enable (default is 0, disabled) 88e4fc250cSLuigi Rizzo * 89e4fc250cSLuigi Rizzo * A second variable controls the sharing of CPU between polling/kernel 90e4fc250cSLuigi Rizzo * network processing, and other activities (typically userlevel tasks): 91e4fc250cSLuigi Rizzo * kern.polling.user_frac (between 0 and 100, default 50) sets the share 92e4fc250cSLuigi Rizzo * of CPU allocated to user tasks. CPU is allocated proportionally to the 93e4fc250cSLuigi Rizzo * shares, by dynamically adjusting the "count" (poll_burst). 94e4fc250cSLuigi Rizzo * 95e4fc250cSLuigi Rizzo * Other parameters can should be left to their default values. 96e4fc250cSLuigi Rizzo * The following constraints hold 97e4fc250cSLuigi Rizzo * 98e4fc250cSLuigi Rizzo * 1 <= poll_each_burst <= poll_burst <= poll_burst_max 99e4fc250cSLuigi Rizzo * 0 <= poll_in_trap <= poll_each_burst 100e4fc250cSLuigi Rizzo * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX 101e4fc250cSLuigi Rizzo */ 102e4fc250cSLuigi Rizzo 103e4fc250cSLuigi Rizzo #define MIN_POLL_BURST_MAX 10 104e4fc250cSLuigi Rizzo #define MAX_POLL_BURST_MAX 1000 105e4fc250cSLuigi Rizzo 106e4fc250cSLuigi Rizzo SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0, 107e4fc250cSLuigi Rizzo "Device polling parameters"); 108e4fc250cSLuigi Rizzo 109e4fc250cSLuigi Rizzo static u_int32_t poll_burst = 5; 110daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RW, 111e4fc250cSLuigi Rizzo &poll_burst, 0, "Current polling burst size"); 112e4fc250cSLuigi Rizzo 113e4fc250cSLuigi Rizzo static u_int32_t poll_each_burst = 5; 114daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, each_burst, CTLFLAG_RW, 115e4fc250cSLuigi Rizzo &poll_each_burst, 0, "Max size of each burst"); 116e4fc250cSLuigi Rizzo 117e4fc250cSLuigi Rizzo static u_int32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ 118daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, burst_max, CTLFLAG_RW, 119e4fc250cSLuigi Rizzo &poll_burst_max, 0, "Max Polling burst size"); 120e4fc250cSLuigi Rizzo 121fc1c73c2SLuigi Rizzo static u_int32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */ 122daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW, 123daccb638SLuigi Rizzo &poll_in_idle_loop, 0, "Enable device polling in idle loop"); 124daccb638SLuigi Rizzo 125e4fc250cSLuigi Rizzo u_int32_t poll_in_trap; /* used in trap.c */ 126daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, poll_in_trap, CTLFLAG_RW, 127e4fc250cSLuigi Rizzo &poll_in_trap, 0, "Poll burst size during a trap"); 128e4fc250cSLuigi Rizzo 129e4fc250cSLuigi Rizzo static u_int32_t user_frac = 50; 130daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, user_frac, CTLFLAG_RW, 131e4fc250cSLuigi Rizzo &user_frac, 0, "Desired user fraction of cpu time"); 132e4fc250cSLuigi Rizzo 133e4fc250cSLuigi Rizzo static u_int32_t reg_frac = 20 ; 134daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, reg_frac, CTLFLAG_RW, 135e4fc250cSLuigi Rizzo ®_frac, 0, "Every this many cycles poll register"); 136e4fc250cSLuigi Rizzo 137e4fc250cSLuigi Rizzo static u_int32_t short_ticks; 138daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RW, 139e4fc250cSLuigi Rizzo &short_ticks, 0, "Hardclock ticks shorter than they should be"); 140e4fc250cSLuigi Rizzo 141e4fc250cSLuigi Rizzo static u_int32_t lost_polls; 142daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RW, 143e4fc250cSLuigi Rizzo &lost_polls, 0, "How many times we would have lost a poll tick"); 144e4fc250cSLuigi Rizzo 145daccb638SLuigi Rizzo static u_int32_t pending_polls; 146daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RW, 147daccb638SLuigi Rizzo &pending_polls, 0, "Do we need to poll again"); 148daccb638SLuigi Rizzo 149daccb638SLuigi Rizzo static int residual_burst = 0; 150daccb638SLuigi Rizzo SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RW, 151daccb638SLuigi Rizzo &residual_burst, 0, "# of residual cycles in burst"); 152daccb638SLuigi Rizzo 153e4fc250cSLuigi Rizzo static u_int32_t poll_handlers; /* next free entry in pr[]. */ 154daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, 155e4fc250cSLuigi Rizzo &poll_handlers, 0, "Number of registered poll handlers"); 156e4fc250cSLuigi Rizzo 157e4fc250cSLuigi Rizzo static int polling = 0; /* global polling enable */ 158daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, enable, CTLFLAG_RW, 159e4fc250cSLuigi Rizzo &polling, 0, "Polling enabled"); 160e4fc250cSLuigi Rizzo 1612dbd9d5bSLuigi Rizzo static u_int32_t phase; 162daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RW, 163daccb638SLuigi Rizzo &phase, 0, "Polling phase"); 164e4fc250cSLuigi Rizzo 165daccb638SLuigi Rizzo static u_int32_t suspect; 166daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RW, 167daccb638SLuigi Rizzo &suspect, 0, "suspect event"); 168daccb638SLuigi Rizzo 1692dbd9d5bSLuigi Rizzo static u_int32_t stalled; 170daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RW, 171daccb638SLuigi Rizzo &stalled, 0, "potential stalls"); 172daccb638SLuigi Rizzo 173daccb638SLuigi Rizzo static u_int32_t idlepoll_sleeping; /* idlepoll is sleeping */ 174daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD, 175daccb638SLuigi Rizzo &idlepoll_sleeping, 0, "idlepoll is sleeping"); 176daccb638SLuigi Rizzo 177e4fc250cSLuigi Rizzo 178e4fc250cSLuigi Rizzo #define POLL_LIST_LEN 128 179e4fc250cSLuigi Rizzo struct pollrec { 180e4fc250cSLuigi Rizzo poll_handler_t *handler; 181e4fc250cSLuigi Rizzo struct ifnet *ifp; 182e4fc250cSLuigi Rizzo }; 183e4fc250cSLuigi Rizzo 184e4fc250cSLuigi Rizzo static struct pollrec pr[POLL_LIST_LEN]; 185e4fc250cSLuigi Rizzo 1861cafed39SJonathan Lemon static void 187daccb638SLuigi Rizzo init_device_poll(void) 188daccb638SLuigi Rizzo { 1891cafed39SJonathan Lemon 1901cafed39SJonathan Lemon netisr_register(NETISR_POLL, (netisr_t *)netisr_poll, NULL); 1911cafed39SJonathan Lemon netisr_register(NETISR_POLLMORE, (netisr_t *)netisr_pollmore, NULL); 192daccb638SLuigi Rizzo } 1931cafed39SJonathan Lemon SYSINIT(device_poll, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, init_device_poll, NULL) 1941cafed39SJonathan Lemon 195daccb638SLuigi Rizzo 196daccb638SLuigi Rizzo /* 197e4fc250cSLuigi Rizzo * Hook from hardclock. Tries to schedule a netisr, but keeps track 198e4fc250cSLuigi Rizzo * of lost ticks due to the previous handler taking too long. 199d26d355fSLuigi Rizzo * Normally, this should not happen, because polling handler should 200d26d355fSLuigi Rizzo * run for a short time. However, in some cases (e.g. when there are 201d26d355fSLuigi Rizzo * changes in link status etc.) the drivers take a very long time 202d26d355fSLuigi Rizzo * (even in the order of milliseconds) to reset and reconfigure the 203d26d355fSLuigi Rizzo * device, causing apparent lost polls. 204d26d355fSLuigi Rizzo * 205e4fc250cSLuigi Rizzo * The first part of the code is just for debugging purposes, and tries 206e4fc250cSLuigi Rizzo * to count how often hardclock ticks are shorter than they should, 207e4fc250cSLuigi Rizzo * meaning either stray interrupts or delayed events. 208e4fc250cSLuigi Rizzo */ 209e4fc250cSLuigi Rizzo void 210e4fc250cSLuigi Rizzo hardclock_device_poll(void) 211e4fc250cSLuigi Rizzo { 212e4fc250cSLuigi Rizzo static struct timeval prev_t, t; 213e4fc250cSLuigi Rizzo int delta; 214e4fc250cSLuigi Rizzo 215daccb638SLuigi Rizzo if (poll_handlers == 0) 216daccb638SLuigi Rizzo return; 217daccb638SLuigi Rizzo 218e4fc250cSLuigi Rizzo microuptime(&t); 219e4fc250cSLuigi Rizzo delta = (t.tv_usec - prev_t.tv_usec) + 220e4fc250cSLuigi Rizzo (t.tv_sec - prev_t.tv_sec)*1000000; 221e4fc250cSLuigi Rizzo if (delta * hz < 500000) 222e4fc250cSLuigi Rizzo short_ticks++; 223e4fc250cSLuigi Rizzo else 224e4fc250cSLuigi Rizzo prev_t = t; 225e4fc250cSLuigi Rizzo 226daccb638SLuigi Rizzo if (pending_polls > 100) { 227d26d355fSLuigi Rizzo /* 228d26d355fSLuigi Rizzo * Too much, assume it has stalled (not always true 229d26d355fSLuigi Rizzo * see comment above). 230d26d355fSLuigi Rizzo */ 231daccb638SLuigi Rizzo stalled++; 232daccb638SLuigi Rizzo pending_polls = 0; 233daccb638SLuigi Rizzo phase = 0; 234daccb638SLuigi Rizzo } 235daccb638SLuigi Rizzo 236daccb638SLuigi Rizzo if (phase <= 2) { 237daccb638SLuigi Rizzo if (phase != 0) 238daccb638SLuigi Rizzo suspect++; 239daccb638SLuigi Rizzo phase = 1; 2401cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 241daccb638SLuigi Rizzo phase = 2; 242e4fc250cSLuigi Rizzo } 243daccb638SLuigi Rizzo if (pending_polls++ > 0) 244daccb638SLuigi Rizzo lost_polls++; 245e4fc250cSLuigi Rizzo } 246e4fc250cSLuigi Rizzo 247e4fc250cSLuigi Rizzo /* 248e4fc250cSLuigi Rizzo * ether_poll is called from the idle loop or from the trap handler. 249e4fc250cSLuigi Rizzo */ 250e4fc250cSLuigi Rizzo void 251e4fc250cSLuigi Rizzo ether_poll(int count) 252e4fc250cSLuigi Rizzo { 253e4fc250cSLuigi Rizzo int i; 254e4fc250cSLuigi Rizzo 255e4fc250cSLuigi Rizzo mtx_lock(&Giant); 256e4fc250cSLuigi Rizzo 257e4fc250cSLuigi Rizzo if (count > poll_each_burst) 258e4fc250cSLuigi Rizzo count = poll_each_burst; 259e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 260e4fc250cSLuigi Rizzo if (pr[i].handler && (IFF_UP|IFF_RUNNING) == 261e4fc250cSLuigi Rizzo (pr[i].ifp->if_flags & (IFF_UP|IFF_RUNNING)) ) 262e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, 0, count); /* quick check */ 263e4fc250cSLuigi Rizzo mtx_unlock(&Giant); 264e4fc250cSLuigi Rizzo } 265e4fc250cSLuigi Rizzo 266e4fc250cSLuigi Rizzo /* 267daccb638SLuigi Rizzo * netisr_pollmore is called after other netisr's, possibly scheduling 268e4fc250cSLuigi Rizzo * another NETISR_POLL call, or adapting the burst size for the next cycle. 269e4fc250cSLuigi Rizzo * 270e4fc250cSLuigi Rizzo * It is very bad to fetch large bursts of packets from a single card at once, 271e4fc250cSLuigi Rizzo * because the burst could take a long time to be completely processed, or 272e4fc250cSLuigi Rizzo * could saturate the intermediate queue (ipintrq or similar) leading to 273e4fc250cSLuigi Rizzo * losses or unfairness. To reduce the problem, and also to account better for 274daccb638SLuigi Rizzo * time spent in network-related processing, we split the burst in smaller 275e4fc250cSLuigi Rizzo * chunks of fixed size, giving control to the other netisr's between chunks. 276e4fc250cSLuigi Rizzo * This helps in improving the fairness, reducing livelock (because we 277e4fc250cSLuigi Rizzo * emulate more closely the "process to completion" that we have with 278e4fc250cSLuigi Rizzo * fastforwarding) and accounting for the work performed in low level 279e4fc250cSLuigi Rizzo * handling and forwarding. 280e4fc250cSLuigi Rizzo */ 281e4fc250cSLuigi Rizzo 282e4fc250cSLuigi Rizzo static struct timeval poll_start_t; 283e4fc250cSLuigi Rizzo 284e4fc250cSLuigi Rizzo void 285daccb638SLuigi Rizzo netisr_pollmore() 286e4fc250cSLuigi Rizzo { 287e4fc250cSLuigi Rizzo struct timeval t; 288e4fc250cSLuigi Rizzo int kern_load; 289daccb638SLuigi Rizzo /* XXX run at splhigh() or equivalent */ 290e4fc250cSLuigi Rizzo 291daccb638SLuigi Rizzo phase = 5; 292e4fc250cSLuigi Rizzo if (residual_burst > 0) { 2931cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 294e4fc250cSLuigi Rizzo /* will run immediately on return, followed by netisrs */ 295e4fc250cSLuigi Rizzo return; 296e4fc250cSLuigi Rizzo } 297e4fc250cSLuigi Rizzo /* here we can account time spent in netisr's in this tick */ 298e4fc250cSLuigi Rizzo microuptime(&t); 299e4fc250cSLuigi Rizzo kern_load = (t.tv_usec - poll_start_t.tv_usec) + 300e4fc250cSLuigi Rizzo (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ 301e4fc250cSLuigi Rizzo kern_load = (kern_load * hz) / 10000; /* 0..100 */ 302e4fc250cSLuigi Rizzo if (kern_load > (100 - user_frac)) { /* try decrease ticks */ 303e4fc250cSLuigi Rizzo if (poll_burst > 1) 304e4fc250cSLuigi Rizzo poll_burst--; 305e4fc250cSLuigi Rizzo } else { 306e4fc250cSLuigi Rizzo if (poll_burst < poll_burst_max) 307e4fc250cSLuigi Rizzo poll_burst++; 308e4fc250cSLuigi Rizzo } 309e4fc250cSLuigi Rizzo 310daccb638SLuigi Rizzo pending_polls--; 311daccb638SLuigi Rizzo if (pending_polls == 0) /* we are done */ 312daccb638SLuigi Rizzo phase = 0; 313daccb638SLuigi Rizzo else { 314e4fc250cSLuigi Rizzo /* 315e4fc250cSLuigi Rizzo * Last cycle was long and caused us to miss one or more 316daccb638SLuigi Rizzo * hardclock ticks. Restart processing again, but slightly 317e4fc250cSLuigi Rizzo * reduce the burst size to prevent that this happens again. 318e4fc250cSLuigi Rizzo */ 319e4fc250cSLuigi Rizzo poll_burst -= (poll_burst / 8); 320e4fc250cSLuigi Rizzo if (poll_burst < 1) 321e4fc250cSLuigi Rizzo poll_burst = 1; 3221cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 323daccb638SLuigi Rizzo phase = 6; 324daccb638SLuigi Rizzo } 325e4fc250cSLuigi Rizzo } 326e4fc250cSLuigi Rizzo 327e4fc250cSLuigi Rizzo /* 328daccb638SLuigi Rizzo * netisr_poll is scheduled by schednetisr when appropriate, typically once 329e4fc250cSLuigi Rizzo * per tick. It is called at splnet() so first thing to do is to upgrade to 330e4fc250cSLuigi Rizzo * splimp(), and call all registered handlers. 331e4fc250cSLuigi Rizzo */ 332daccb638SLuigi Rizzo static void 333daccb638SLuigi Rizzo netisr_poll(void) 334e4fc250cSLuigi Rizzo { 335e4fc250cSLuigi Rizzo static int reg_frac_count; 336e4fc250cSLuigi Rizzo int i, cycles; 337e4fc250cSLuigi Rizzo enum poll_cmd arg = POLL_ONLY; 338e4fc250cSLuigi Rizzo mtx_lock(&Giant); 339e4fc250cSLuigi Rizzo 340daccb638SLuigi Rizzo phase = 3; 341e4fc250cSLuigi Rizzo if (residual_burst == 0) { /* first call in this tick */ 342e4fc250cSLuigi Rizzo microuptime(&poll_start_t); 343e4fc250cSLuigi Rizzo /* 344e4fc250cSLuigi Rizzo * Check that paremeters are consistent with runtime 345e4fc250cSLuigi Rizzo * variables. Some of these tests could be done at sysctl 346e4fc250cSLuigi Rizzo * time, but the savings would be very limited because we 347e4fc250cSLuigi Rizzo * still have to check against reg_frac_count and 348e4fc250cSLuigi Rizzo * poll_each_burst. So, instead of writing separate sysctl 349e4fc250cSLuigi Rizzo * handlers, we do all here. 350e4fc250cSLuigi Rizzo */ 351e4fc250cSLuigi Rizzo 352e4fc250cSLuigi Rizzo if (reg_frac > hz) 353e4fc250cSLuigi Rizzo reg_frac = hz; 354e4fc250cSLuigi Rizzo else if (reg_frac < 1) 355e4fc250cSLuigi Rizzo reg_frac = 1; 356e4fc250cSLuigi Rizzo if (reg_frac_count > reg_frac) 357e4fc250cSLuigi Rizzo reg_frac_count = reg_frac - 1; 358e4fc250cSLuigi Rizzo if (reg_frac_count-- == 0) { 359e4fc250cSLuigi Rizzo arg = POLL_AND_CHECK_STATUS; 360e4fc250cSLuigi Rizzo reg_frac_count = reg_frac - 1; 361e4fc250cSLuigi Rizzo } 362e4fc250cSLuigi Rizzo if (poll_burst_max < MIN_POLL_BURST_MAX) 363e4fc250cSLuigi Rizzo poll_burst_max = MIN_POLL_BURST_MAX; 364e4fc250cSLuigi Rizzo else if (poll_burst_max > MAX_POLL_BURST_MAX) 365e4fc250cSLuigi Rizzo poll_burst_max = MAX_POLL_BURST_MAX; 366e4fc250cSLuigi Rizzo 367e4fc250cSLuigi Rizzo if (poll_each_burst < 1) 368e4fc250cSLuigi Rizzo poll_each_burst = 1; 369e4fc250cSLuigi Rizzo else if (poll_each_burst > poll_burst_max) 370e4fc250cSLuigi Rizzo poll_each_burst = poll_burst_max; 371e4fc250cSLuigi Rizzo 372e4fc250cSLuigi Rizzo residual_burst = poll_burst; 373e4fc250cSLuigi Rizzo } 374e4fc250cSLuigi Rizzo cycles = (residual_burst < poll_each_burst) ? 375e4fc250cSLuigi Rizzo residual_burst : poll_each_burst; 376e4fc250cSLuigi Rizzo residual_burst -= cycles; 377e4fc250cSLuigi Rizzo 378e4fc250cSLuigi Rizzo if (polling) { 379e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 380e4fc250cSLuigi Rizzo if (pr[i].handler && (IFF_UP|IFF_RUNNING) == 381e4fc250cSLuigi Rizzo (pr[i].ifp->if_flags & (IFF_UP|IFF_RUNNING)) ) 382e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, arg, cycles); 383e4fc250cSLuigi Rizzo } else { /* unregister */ 384e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) { 385e4fc250cSLuigi Rizzo if (pr[i].handler && 386e4fc250cSLuigi Rizzo pr[i].ifp->if_flags & IFF_RUNNING) { 38762f76486SMaxim Sobolev pr[i].ifp->if_flags &= ~IFF_POLLING; 388e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, POLL_DEREGISTER, 1); 389e4fc250cSLuigi Rizzo } 390e4fc250cSLuigi Rizzo pr[i].handler=NULL; 391e4fc250cSLuigi Rizzo } 392e4fc250cSLuigi Rizzo residual_burst = 0; 393e4fc250cSLuigi Rizzo poll_handlers = 0; 394e4fc250cSLuigi Rizzo } 395e4fc250cSLuigi Rizzo /* on -stable, schednetisr(NETISR_POLLMORE); */ 396daccb638SLuigi Rizzo phase = 4; 397e4fc250cSLuigi Rizzo mtx_unlock(&Giant); 398e4fc250cSLuigi Rizzo } 399e4fc250cSLuigi Rizzo 400e4fc250cSLuigi Rizzo /* 401e4fc250cSLuigi Rizzo * Try to register routine for polling. Returns 1 if successful 402e4fc250cSLuigi Rizzo * (and polling should be enabled), 0 otherwise. 403e4fc250cSLuigi Rizzo * A device is not supposed to register itself multiple times. 404e4fc250cSLuigi Rizzo * 405daccb638SLuigi Rizzo * This is called from within the *_intr() functions, so we do not need 406daccb638SLuigi Rizzo * further locking. 407e4fc250cSLuigi Rizzo */ 408e4fc250cSLuigi Rizzo int 409e4fc250cSLuigi Rizzo ether_poll_register(poll_handler_t *h, struct ifnet *ifp) 410e4fc250cSLuigi Rizzo { 411e4fc250cSLuigi Rizzo int s; 412e4fc250cSLuigi Rizzo 413e4fc250cSLuigi Rizzo if (polling == 0) /* polling disabled, cannot register */ 414e4fc250cSLuigi Rizzo return 0; 415e4fc250cSLuigi Rizzo if (h == NULL || ifp == NULL) /* bad arguments */ 416e4fc250cSLuigi Rizzo return 0; 417e4fc250cSLuigi Rizzo if ( !(ifp->if_flags & IFF_UP) ) /* must be up */ 418e4fc250cSLuigi Rizzo return 0; 41962f76486SMaxim Sobolev if (ifp->if_flags & IFF_POLLING) /* already polling */ 420e4fc250cSLuigi Rizzo return 0; 421e4fc250cSLuigi Rizzo 422e4fc250cSLuigi Rizzo s = splhigh(); 423e4fc250cSLuigi Rizzo if (poll_handlers >= POLL_LIST_LEN) { 424e4fc250cSLuigi Rizzo /* 425e4fc250cSLuigi Rizzo * List full, cannot register more entries. 426e4fc250cSLuigi Rizzo * This should never happen; if it does, it is probably a 427e4fc250cSLuigi Rizzo * broken driver trying to register multiple times. Checking 428e4fc250cSLuigi Rizzo * this at runtime is expensive, and won't solve the problem 429e4fc250cSLuigi Rizzo * anyways, so just report a few times and then give up. 430e4fc250cSLuigi Rizzo */ 431e4fc250cSLuigi Rizzo static int verbose = 10 ; 432e4fc250cSLuigi Rizzo splx(s); 433e4fc250cSLuigi Rizzo if (verbose >0) { 434e4fc250cSLuigi Rizzo printf("poll handlers list full, " 435e4fc250cSLuigi Rizzo "maybe a broken driver ?\n"); 436e4fc250cSLuigi Rizzo verbose--; 437e4fc250cSLuigi Rizzo } 438e4fc250cSLuigi Rizzo return 0; /* no polling for you */ 439e4fc250cSLuigi Rizzo } 440e4fc250cSLuigi Rizzo 441e4fc250cSLuigi Rizzo pr[poll_handlers].handler = h; 442e4fc250cSLuigi Rizzo pr[poll_handlers].ifp = ifp; 443e4fc250cSLuigi Rizzo poll_handlers++; 44462f76486SMaxim Sobolev ifp->if_flags |= IFF_POLLING; 445e4fc250cSLuigi Rizzo splx(s); 446d105c784SLuigi Rizzo if (idlepoll_sleeping) 447d105c784SLuigi Rizzo wakeup(&idlepoll_sleeping); 448e4fc250cSLuigi Rizzo return 1; /* polling enabled in next call */ 449e4fc250cSLuigi Rizzo } 450e4fc250cSLuigi Rizzo 451e4fc250cSLuigi Rizzo /* 452daccb638SLuigi Rizzo * Remove interface from the polling list. Normally called by *_stop(). 453daccb638SLuigi Rizzo * It is not an error to call it with IFF_POLLING clear, the call is 454daccb638SLuigi Rizzo * sufficiently rare to be preferable to save the space for the extra 455daccb638SLuigi Rizzo * test in each driver in exchange of one additional function call. 456e4fc250cSLuigi Rizzo */ 457e4fc250cSLuigi Rizzo int 458e4fc250cSLuigi Rizzo ether_poll_deregister(struct ifnet *ifp) 459e4fc250cSLuigi Rizzo { 460e4fc250cSLuigi Rizzo int i; 461e4fc250cSLuigi Rizzo 462e4fc250cSLuigi Rizzo mtx_lock(&Giant); 46362f76486SMaxim Sobolev if ( !ifp || !(ifp->if_flags & IFF_POLLING) ) { 464e4fc250cSLuigi Rizzo mtx_unlock(&Giant); 465e4fc250cSLuigi Rizzo return 0; 466e4fc250cSLuigi Rizzo } 467e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 468e4fc250cSLuigi Rizzo if (pr[i].ifp == ifp) /* found it */ 469e4fc250cSLuigi Rizzo break; 47062f76486SMaxim Sobolev ifp->if_flags &= ~IFF_POLLING; /* found or not... */ 471e4fc250cSLuigi Rizzo if (i == poll_handlers) { 472e4fc250cSLuigi Rizzo mtx_unlock(&Giant); 473e4fc250cSLuigi Rizzo printf("ether_poll_deregister: ifp not found!!!\n"); 474e4fc250cSLuigi Rizzo return 0; 475e4fc250cSLuigi Rizzo } 476e4fc250cSLuigi Rizzo poll_handlers--; 477e4fc250cSLuigi Rizzo if (i < poll_handlers) { /* Last entry replaces this one. */ 478e4fc250cSLuigi Rizzo pr[i].handler = pr[poll_handlers].handler; 479e4fc250cSLuigi Rizzo pr[i].ifp = pr[poll_handlers].ifp; 480e4fc250cSLuigi Rizzo } 481e4fc250cSLuigi Rizzo mtx_unlock(&Giant); 482e4fc250cSLuigi Rizzo return 1; 483e4fc250cSLuigi Rizzo } 484d105c784SLuigi Rizzo 485d105c784SLuigi Rizzo static void 486d105c784SLuigi Rizzo poll_idle(void) 487d105c784SLuigi Rizzo { 488d105c784SLuigi Rizzo struct thread *td = curthread; 489d105c784SLuigi Rizzo struct rtprio rtp; 490d105c784SLuigi Rizzo int pri; 491d105c784SLuigi Rizzo 492d105c784SLuigi Rizzo rtp.prio = RTP_PRIO_MAX; /* lowest priority */ 493d105c784SLuigi Rizzo rtp.type = RTP_PRIO_IDLE; 494d105c784SLuigi Rizzo mtx_lock_spin(&sched_lock); 495e5223044SLuigi Rizzo rtp_to_pri(&rtp, td->td_ksegrp); 4962c100766SJulian Elischer pri = td->td_priority; 497d105c784SLuigi Rizzo mtx_unlock_spin(&sched_lock); 498d105c784SLuigi Rizzo 499d105c784SLuigi Rizzo for (;;) { 500daccb638SLuigi Rizzo if (poll_in_idle_loop && poll_handlers > 0) { 501d105c784SLuigi Rizzo idlepoll_sleeping = 0; 502d105c784SLuigi Rizzo mtx_lock(&Giant); 503d105c784SLuigi Rizzo ether_poll(poll_each_burst); 504d105c784SLuigi Rizzo mtx_unlock(&Giant); 505d105c784SLuigi Rizzo mtx_assert(&Giant, MA_NOTOWNED); 506d105c784SLuigi Rizzo mtx_lock_spin(&sched_lock); 507d105c784SLuigi Rizzo td->td_proc->p_stats->p_ru.ru_nvcsw++; 508d105c784SLuigi Rizzo mi_switch(); 509d105c784SLuigi Rizzo mtx_unlock_spin(&sched_lock); 510d105c784SLuigi Rizzo } else { 511d105c784SLuigi Rizzo idlepoll_sleeping = 1; 512d105c784SLuigi Rizzo tsleep(&idlepoll_sleeping, pri, "pollid", hz * 3); 513d105c784SLuigi Rizzo } 514d105c784SLuigi Rizzo } 515d105c784SLuigi Rizzo } 516d105c784SLuigi Rizzo 517d105c784SLuigi Rizzo static struct proc *idlepoll; 518d105c784SLuigi Rizzo static struct kproc_desc idlepoll_kp = { 519d105c784SLuigi Rizzo "idlepoll", 520d105c784SLuigi Rizzo poll_idle, 521d105c784SLuigi Rizzo &idlepoll 522d105c784SLuigi Rizzo }; 523d105c784SLuigi Rizzo SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &idlepoll_kp) 524