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 */ 3540929967SGleb Smirnoff #include <sys/sockio.h> 36e4fc250cSLuigi Rizzo #include <sys/sysctl.h> 3716901c01SGleb Smirnoff #include <sys/syslog.h> 38e4fc250cSLuigi Rizzo 39e4fc250cSLuigi Rizzo #include <net/if.h> /* for IFF_* flags */ 40e4fc250cSLuigi Rizzo #include <net/netisr.h> /* for NETISR_POLL */ 41e4fc250cSLuigi Rizzo 42d105c784SLuigi Rizzo #include <sys/proc.h> 43d105c784SLuigi Rizzo #include <sys/resourcevar.h> 44d105c784SLuigi Rizzo #include <sys/kthread.h> 45d105c784SLuigi Rizzo 46daccb638SLuigi Rizzo static void netisr_poll(void); /* the two netisr handlers */ 471cafed39SJonathan Lemon static void netisr_pollmore(void); 4840929967SGleb Smirnoff static int poll_switch(SYSCTL_HANDLER_ARGS); 49daccb638SLuigi Rizzo 50daccb638SLuigi Rizzo void hardclock_device_poll(void); /* hook from hardclock */ 5140929967SGleb Smirnoff void ether_poll(int); /* polling in idle loop */ 52e4fc250cSLuigi Rizzo 53e4fc250cSLuigi Rizzo /* 54e4fc250cSLuigi Rizzo * Polling support for [network] device drivers. 55e4fc250cSLuigi Rizzo * 5640929967SGleb Smirnoff * Drivers which support this feature can register with the 57e4fc250cSLuigi Rizzo * polling code. 58e4fc250cSLuigi Rizzo * 59e4fc250cSLuigi Rizzo * If registration is successful, the driver must disable interrupts, 60e4fc250cSLuigi Rizzo * and further I/O is performed through the handler, which is invoked 61e4fc250cSLuigi Rizzo * (at least once per clock tick) with 3 arguments: the "arg" passed at 62e4fc250cSLuigi Rizzo * register time (a struct ifnet pointer), a command, and a "count" limit. 63e4fc250cSLuigi Rizzo * 64e4fc250cSLuigi Rizzo * The command can be one of the following: 65e4fc250cSLuigi Rizzo * POLL_ONLY: quick move of "count" packets from input/output queues. 66e4fc250cSLuigi Rizzo * POLL_AND_CHECK_STATUS: as above, plus check status registers or do 67e4fc250cSLuigi Rizzo * other more expensive operations. This command is issued periodically 68e4fc250cSLuigi Rizzo * but less frequently than POLL_ONLY. 69e4fc250cSLuigi Rizzo * 70e4fc250cSLuigi Rizzo * The count limit specifies how much work the handler can do during the 71e4fc250cSLuigi Rizzo * call -- typically this is the number of packets to be received, or 72e4fc250cSLuigi Rizzo * transmitted, etc. (drivers are free to interpret this number, as long 73e4fc250cSLuigi Rizzo * as the max time spent in the function grows roughly linearly with the 74e4fc250cSLuigi Rizzo * count). 75e4fc250cSLuigi Rizzo * 7640929967SGleb Smirnoff * Polling is enabled and disabled via setting IFCAP_POLLING flag on 7740929967SGleb Smirnoff * the interface. The driver ioctl handler should register interface 7840929967SGleb Smirnoff * with polling and disable interrupts, if registration was successful. 79e4fc250cSLuigi Rizzo * 80e4fc250cSLuigi Rizzo * A second variable controls the sharing of CPU between polling/kernel 81e4fc250cSLuigi Rizzo * network processing, and other activities (typically userlevel tasks): 82e4fc250cSLuigi Rizzo * kern.polling.user_frac (between 0 and 100, default 50) sets the share 83e4fc250cSLuigi Rizzo * of CPU allocated to user tasks. CPU is allocated proportionally to the 84e4fc250cSLuigi Rizzo * shares, by dynamically adjusting the "count" (poll_burst). 85e4fc250cSLuigi Rizzo * 86e4fc250cSLuigi Rizzo * Other parameters can should be left to their default values. 87e4fc250cSLuigi Rizzo * The following constraints hold 88e4fc250cSLuigi Rizzo * 89e4fc250cSLuigi Rizzo * 1 <= poll_each_burst <= poll_burst <= poll_burst_max 9040929967SGleb Smirnoff * 0 <= poll_each_burst 91e4fc250cSLuigi Rizzo * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX 92e4fc250cSLuigi Rizzo */ 93e4fc250cSLuigi Rizzo 94e4fc250cSLuigi Rizzo #define MIN_POLL_BURST_MAX 10 95e4fc250cSLuigi Rizzo #define MAX_POLL_BURST_MAX 1000 96e4fc250cSLuigi Rizzo 97e4fc250cSLuigi Rizzo SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0, 98e4fc250cSLuigi Rizzo "Device polling parameters"); 99e4fc250cSLuigi Rizzo 100e4fc250cSLuigi Rizzo static u_int32_t poll_burst = 5; 101daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RW, 102e4fc250cSLuigi Rizzo &poll_burst, 0, "Current polling burst size"); 103e4fc250cSLuigi Rizzo 104e4fc250cSLuigi Rizzo static u_int32_t poll_each_burst = 5; 105daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, each_burst, CTLFLAG_RW, 106e4fc250cSLuigi Rizzo &poll_each_burst, 0, "Max size of each burst"); 107e4fc250cSLuigi Rizzo 108e4fc250cSLuigi Rizzo static u_int32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ 109daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, burst_max, CTLFLAG_RW, 110e4fc250cSLuigi Rizzo &poll_burst_max, 0, "Max Polling burst size"); 111e4fc250cSLuigi Rizzo 1126c240564SSam Leffler static u_int32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */ 113daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW, 114daccb638SLuigi Rizzo &poll_in_idle_loop, 0, "Enable device polling in idle loop"); 115daccb638SLuigi Rizzo 116e4fc250cSLuigi Rizzo static u_int32_t user_frac = 50; 117daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, user_frac, CTLFLAG_RW, 118e4fc250cSLuigi Rizzo &user_frac, 0, "Desired user fraction of cpu time"); 119e4fc250cSLuigi Rizzo 120e4fc250cSLuigi Rizzo static u_int32_t reg_frac = 20 ; 121daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, reg_frac, CTLFLAG_RW, 122e4fc250cSLuigi Rizzo ®_frac, 0, "Every this many cycles poll register"); 123e4fc250cSLuigi Rizzo 124e4fc250cSLuigi Rizzo static u_int32_t short_ticks; 125daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RW, 126e4fc250cSLuigi Rizzo &short_ticks, 0, "Hardclock ticks shorter than they should be"); 127e4fc250cSLuigi Rizzo 128e4fc250cSLuigi Rizzo static u_int32_t lost_polls; 129daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RW, 130e4fc250cSLuigi Rizzo &lost_polls, 0, "How many times we would have lost a poll tick"); 131e4fc250cSLuigi Rizzo 132daccb638SLuigi Rizzo static u_int32_t pending_polls; 133daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RW, 134daccb638SLuigi Rizzo &pending_polls, 0, "Do we need to poll again"); 135daccb638SLuigi Rizzo 136daccb638SLuigi Rizzo static int residual_burst = 0; 137daccb638SLuigi Rizzo SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RW, 138daccb638SLuigi Rizzo &residual_burst, 0, "# of residual cycles in burst"); 139daccb638SLuigi Rizzo 140e4fc250cSLuigi Rizzo static u_int32_t poll_handlers; /* next free entry in pr[]. */ 141daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, 142e4fc250cSLuigi Rizzo &poll_handlers, 0, "Number of registered poll handlers"); 143e4fc250cSLuigi Rizzo 14440929967SGleb Smirnoff static int polling = 0; 14540929967SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, enable, CTLTYPE_UINT | CTLFLAG_RW, 14640929967SGleb Smirnoff 0, sizeof(int), poll_switch, "I", "Switch polling for all interfaces"); 147e4fc250cSLuigi Rizzo 1482dbd9d5bSLuigi Rizzo static u_int32_t phase; 149daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RW, 150daccb638SLuigi Rizzo &phase, 0, "Polling phase"); 151e4fc250cSLuigi Rizzo 152daccb638SLuigi Rizzo static u_int32_t suspect; 153daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RW, 154daccb638SLuigi Rizzo &suspect, 0, "suspect event"); 155daccb638SLuigi Rizzo 1562dbd9d5bSLuigi Rizzo static u_int32_t stalled; 157daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RW, 158daccb638SLuigi Rizzo &stalled, 0, "potential stalls"); 159daccb638SLuigi Rizzo 160daccb638SLuigi Rizzo static u_int32_t idlepoll_sleeping; /* idlepoll is sleeping */ 161daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD, 162daccb638SLuigi Rizzo &idlepoll_sleeping, 0, "idlepoll is sleeping"); 163daccb638SLuigi Rizzo 164e4fc250cSLuigi Rizzo 165e4fc250cSLuigi Rizzo #define POLL_LIST_LEN 128 166e4fc250cSLuigi Rizzo struct pollrec { 167e4fc250cSLuigi Rizzo poll_handler_t *handler; 168e4fc250cSLuigi Rizzo struct ifnet *ifp; 169e4fc250cSLuigi Rizzo }; 170e4fc250cSLuigi Rizzo 171e4fc250cSLuigi Rizzo static struct pollrec pr[POLL_LIST_LEN]; 17216901c01SGleb Smirnoff static struct mtx poll_mtx; 17316901c01SGleb Smirnoff 1741cafed39SJonathan Lemon static void 175daccb638SLuigi Rizzo init_device_poll(void) 176daccb638SLuigi Rizzo { 1771cafed39SJonathan Lemon 17816901c01SGleb Smirnoff mtx_init(&poll_mtx, "polling", NULL, MTX_DEF); 17916901c01SGleb Smirnoff netisr_register(NETISR_POLL, (netisr_t *)netisr_poll, NULL, 18016901c01SGleb Smirnoff NETISR_MPSAFE); 18116901c01SGleb Smirnoff netisr_register(NETISR_POLLMORE, (netisr_t *)netisr_pollmore, NULL, 18216901c01SGleb Smirnoff NETISR_MPSAFE); 183daccb638SLuigi Rizzo } 1841cafed39SJonathan Lemon SYSINIT(device_poll, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, init_device_poll, NULL) 1851cafed39SJonathan Lemon 186daccb638SLuigi Rizzo 187daccb638SLuigi Rizzo /* 188e4fc250cSLuigi Rizzo * Hook from hardclock. Tries to schedule a netisr, but keeps track 189e4fc250cSLuigi Rizzo * of lost ticks due to the previous handler taking too long. 190d26d355fSLuigi Rizzo * Normally, this should not happen, because polling handler should 191d26d355fSLuigi Rizzo * run for a short time. However, in some cases (e.g. when there are 192d26d355fSLuigi Rizzo * changes in link status etc.) the drivers take a very long time 193d26d355fSLuigi Rizzo * (even in the order of milliseconds) to reset and reconfigure the 194d26d355fSLuigi Rizzo * device, causing apparent lost polls. 195d26d355fSLuigi Rizzo * 196e4fc250cSLuigi Rizzo * The first part of the code is just for debugging purposes, and tries 197e4fc250cSLuigi Rizzo * to count how often hardclock ticks are shorter than they should, 198e4fc250cSLuigi Rizzo * meaning either stray interrupts or delayed events. 199e4fc250cSLuigi Rizzo */ 200e4fc250cSLuigi Rizzo void 201e4fc250cSLuigi Rizzo hardclock_device_poll(void) 202e4fc250cSLuigi Rizzo { 203e4fc250cSLuigi Rizzo static struct timeval prev_t, t; 204e4fc250cSLuigi Rizzo int delta; 205e4fc250cSLuigi Rizzo 206daccb638SLuigi Rizzo if (poll_handlers == 0) 207daccb638SLuigi Rizzo return; 208daccb638SLuigi Rizzo 209e4fc250cSLuigi Rizzo microuptime(&t); 210e4fc250cSLuigi Rizzo delta = (t.tv_usec - prev_t.tv_usec) + 211e4fc250cSLuigi Rizzo (t.tv_sec - prev_t.tv_sec)*1000000; 212e4fc250cSLuigi Rizzo if (delta * hz < 500000) 213e4fc250cSLuigi Rizzo short_ticks++; 214e4fc250cSLuigi Rizzo else 215e4fc250cSLuigi Rizzo prev_t = t; 216e4fc250cSLuigi Rizzo 217daccb638SLuigi Rizzo if (pending_polls > 100) { 218d26d355fSLuigi Rizzo /* 219d26d355fSLuigi Rizzo * Too much, assume it has stalled (not always true 220d26d355fSLuigi Rizzo * see comment above). 221d26d355fSLuigi Rizzo */ 222daccb638SLuigi Rizzo stalled++; 223daccb638SLuigi Rizzo pending_polls = 0; 224daccb638SLuigi Rizzo phase = 0; 225daccb638SLuigi Rizzo } 226daccb638SLuigi Rizzo 227daccb638SLuigi Rizzo if (phase <= 2) { 228daccb638SLuigi Rizzo if (phase != 0) 229daccb638SLuigi Rizzo suspect++; 230daccb638SLuigi Rizzo phase = 1; 2311cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 232daccb638SLuigi Rizzo phase = 2; 233e4fc250cSLuigi Rizzo } 234daccb638SLuigi Rizzo if (pending_polls++ > 0) 235daccb638SLuigi Rizzo lost_polls++; 236e4fc250cSLuigi Rizzo } 237e4fc250cSLuigi Rizzo 238e4fc250cSLuigi Rizzo /* 23940929967SGleb Smirnoff * ether_poll is called from the idle loop. 240e4fc250cSLuigi Rizzo */ 241e4fc250cSLuigi Rizzo void 242e4fc250cSLuigi Rizzo ether_poll(int count) 243e4fc250cSLuigi Rizzo { 244e4fc250cSLuigi Rizzo int i; 245e4fc250cSLuigi Rizzo 24640929967SGleb Smirnoff NET_LOCK_GIANT(); 24716901c01SGleb Smirnoff mtx_lock(&poll_mtx); 248e4fc250cSLuigi Rizzo 249e4fc250cSLuigi Rizzo if (count > poll_each_burst) 250e4fc250cSLuigi Rizzo count = poll_each_burst; 25116901c01SGleb Smirnoff 25240929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 25316901c01SGleb Smirnoff pr[i].handler(pr[i].ifp, POLL_ONLY, count); 25440929967SGleb Smirnoff 25516901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 25640929967SGleb Smirnoff NET_UNLOCK_GIANT(); 257e4fc250cSLuigi Rizzo } 258e4fc250cSLuigi Rizzo 259e4fc250cSLuigi Rizzo /* 260daccb638SLuigi Rizzo * netisr_pollmore is called after other netisr's, possibly scheduling 261e4fc250cSLuigi Rizzo * another NETISR_POLL call, or adapting the burst size for the next cycle. 262e4fc250cSLuigi Rizzo * 263e4fc250cSLuigi Rizzo * It is very bad to fetch large bursts of packets from a single card at once, 264e4fc250cSLuigi Rizzo * because the burst could take a long time to be completely processed, or 265e4fc250cSLuigi Rizzo * could saturate the intermediate queue (ipintrq or similar) leading to 266e4fc250cSLuigi Rizzo * losses or unfairness. To reduce the problem, and also to account better for 267daccb638SLuigi Rizzo * time spent in network-related processing, we split the burst in smaller 268e4fc250cSLuigi Rizzo * chunks of fixed size, giving control to the other netisr's between chunks. 269e4fc250cSLuigi Rizzo * This helps in improving the fairness, reducing livelock (because we 270e4fc250cSLuigi Rizzo * emulate more closely the "process to completion" that we have with 271e4fc250cSLuigi Rizzo * fastforwarding) and accounting for the work performed in low level 272e4fc250cSLuigi Rizzo * handling and forwarding. 273e4fc250cSLuigi Rizzo */ 274e4fc250cSLuigi Rizzo 275e4fc250cSLuigi Rizzo static struct timeval poll_start_t; 276e4fc250cSLuigi Rizzo 277e4fc250cSLuigi Rizzo void 278daccb638SLuigi Rizzo netisr_pollmore() 279e4fc250cSLuigi Rizzo { 280e4fc250cSLuigi Rizzo struct timeval t; 281e4fc250cSLuigi Rizzo int kern_load; 282e4fc250cSLuigi Rizzo 28316901c01SGleb Smirnoff NET_ASSERT_GIANT(); 28416901c01SGleb Smirnoff 28516901c01SGleb Smirnoff mtx_lock(&poll_mtx); 286daccb638SLuigi Rizzo phase = 5; 287e4fc250cSLuigi Rizzo if (residual_burst > 0) { 2881cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 28916901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 290e4fc250cSLuigi Rizzo /* will run immediately on return, followed by netisrs */ 291e4fc250cSLuigi Rizzo return; 292e4fc250cSLuigi Rizzo } 293e4fc250cSLuigi Rizzo /* here we can account time spent in netisr's in this tick */ 294e4fc250cSLuigi Rizzo microuptime(&t); 295e4fc250cSLuigi Rizzo kern_load = (t.tv_usec - poll_start_t.tv_usec) + 296e4fc250cSLuigi Rizzo (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ 297e4fc250cSLuigi Rizzo kern_load = (kern_load * hz) / 10000; /* 0..100 */ 298e4fc250cSLuigi Rizzo if (kern_load > (100 - user_frac)) { /* try decrease ticks */ 299e4fc250cSLuigi Rizzo if (poll_burst > 1) 300e4fc250cSLuigi Rizzo poll_burst--; 301e4fc250cSLuigi Rizzo } else { 302e4fc250cSLuigi Rizzo if (poll_burst < poll_burst_max) 303e4fc250cSLuigi Rizzo poll_burst++; 304e4fc250cSLuigi Rizzo } 305e4fc250cSLuigi Rizzo 306daccb638SLuigi Rizzo pending_polls--; 307daccb638SLuigi Rizzo if (pending_polls == 0) /* we are done */ 308daccb638SLuigi Rizzo phase = 0; 309daccb638SLuigi Rizzo else { 310e4fc250cSLuigi Rizzo /* 311e4fc250cSLuigi Rizzo * Last cycle was long and caused us to miss one or more 312daccb638SLuigi Rizzo * hardclock ticks. Restart processing again, but slightly 313e4fc250cSLuigi Rizzo * reduce the burst size to prevent that this happens again. 314e4fc250cSLuigi Rizzo */ 315e4fc250cSLuigi Rizzo poll_burst -= (poll_burst / 8); 316e4fc250cSLuigi Rizzo if (poll_burst < 1) 317e4fc250cSLuigi Rizzo poll_burst = 1; 3181cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 319daccb638SLuigi Rizzo phase = 6; 320daccb638SLuigi Rizzo } 32116901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 322e4fc250cSLuigi Rizzo } 323e4fc250cSLuigi Rizzo 324e4fc250cSLuigi Rizzo /* 325daccb638SLuigi Rizzo * netisr_poll is scheduled by schednetisr when appropriate, typically once 32616901c01SGleb Smirnoff * per tick. 327e4fc250cSLuigi Rizzo */ 328daccb638SLuigi Rizzo static void 329daccb638SLuigi Rizzo netisr_poll(void) 330e4fc250cSLuigi Rizzo { 331e4fc250cSLuigi Rizzo static int reg_frac_count; 332e4fc250cSLuigi Rizzo int i, cycles; 333e4fc250cSLuigi Rizzo enum poll_cmd arg = POLL_ONLY; 334e4fc250cSLuigi Rizzo 33516901c01SGleb Smirnoff NET_ASSERT_GIANT(); 33616901c01SGleb Smirnoff 33716901c01SGleb Smirnoff mtx_lock(&poll_mtx); 338daccb638SLuigi Rizzo phase = 3; 339e4fc250cSLuigi Rizzo if (residual_burst == 0) { /* first call in this tick */ 340e4fc250cSLuigi Rizzo microuptime(&poll_start_t); 341e4fc250cSLuigi Rizzo /* 342e4fc250cSLuigi Rizzo * Check that paremeters are consistent with runtime 343e4fc250cSLuigi Rizzo * variables. Some of these tests could be done at sysctl 344e4fc250cSLuigi Rizzo * time, but the savings would be very limited because we 345e4fc250cSLuigi Rizzo * still have to check against reg_frac_count and 346e4fc250cSLuigi Rizzo * poll_each_burst. So, instead of writing separate sysctl 347e4fc250cSLuigi Rizzo * handlers, we do all here. 348e4fc250cSLuigi Rizzo */ 349e4fc250cSLuigi Rizzo 350e4fc250cSLuigi Rizzo if (reg_frac > hz) 351e4fc250cSLuigi Rizzo reg_frac = hz; 352e4fc250cSLuigi Rizzo else if (reg_frac < 1) 353e4fc250cSLuigi Rizzo reg_frac = 1; 354e4fc250cSLuigi Rizzo if (reg_frac_count > reg_frac) 355e4fc250cSLuigi Rizzo reg_frac_count = reg_frac - 1; 356e4fc250cSLuigi Rizzo if (reg_frac_count-- == 0) { 357e4fc250cSLuigi Rizzo arg = POLL_AND_CHECK_STATUS; 358e4fc250cSLuigi Rizzo reg_frac_count = reg_frac - 1; 359e4fc250cSLuigi Rizzo } 360e4fc250cSLuigi Rizzo if (poll_burst_max < MIN_POLL_BURST_MAX) 361e4fc250cSLuigi Rizzo poll_burst_max = MIN_POLL_BURST_MAX; 362e4fc250cSLuigi Rizzo else if (poll_burst_max > MAX_POLL_BURST_MAX) 363e4fc250cSLuigi Rizzo poll_burst_max = MAX_POLL_BURST_MAX; 364e4fc250cSLuigi Rizzo 365e4fc250cSLuigi Rizzo if (poll_each_burst < 1) 366e4fc250cSLuigi Rizzo poll_each_burst = 1; 367e4fc250cSLuigi Rizzo else if (poll_each_burst > poll_burst_max) 368e4fc250cSLuigi Rizzo poll_each_burst = poll_burst_max; 369e4fc250cSLuigi Rizzo 37061f7581dSRuslan Ermilov if (poll_burst > poll_burst_max) 37161f7581dSRuslan Ermilov poll_burst = poll_burst_max; 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 37840929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 379e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, arg, cycles); 38016901c01SGleb Smirnoff 381daccb638SLuigi Rizzo phase = 4; 38216901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 383e4fc250cSLuigi Rizzo } 384e4fc250cSLuigi Rizzo 385e4fc250cSLuigi Rizzo /* 38640929967SGleb Smirnoff * Try to register routine for polling. Returns 0 if successful 38740929967SGleb Smirnoff * (and polling should be enabled), error code otherwise. 388e4fc250cSLuigi Rizzo * A device is not supposed to register itself multiple times. 389e4fc250cSLuigi Rizzo * 39040929967SGleb Smirnoff * This is called from within the *_ioctl() functions. 391e4fc250cSLuigi Rizzo */ 392e4fc250cSLuigi Rizzo int 393e4fc250cSLuigi Rizzo ether_poll_register(poll_handler_t *h, struct ifnet *ifp) 394e4fc250cSLuigi Rizzo { 39516901c01SGleb Smirnoff int i; 39616901c01SGleb Smirnoff 39740929967SGleb Smirnoff KASSERT(h != NULL, ("%s: handler is NULL", __func__)); 39840929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 399e4fc250cSLuigi Rizzo 40040929967SGleb Smirnoff NET_ASSERT_GIANT(); 401e4fc250cSLuigi Rizzo 40216901c01SGleb Smirnoff mtx_lock(&poll_mtx); 403e4fc250cSLuigi Rizzo if (poll_handlers >= POLL_LIST_LEN) { 404e4fc250cSLuigi Rizzo /* 405e4fc250cSLuigi Rizzo * List full, cannot register more entries. 406e4fc250cSLuigi Rizzo * This should never happen; if it does, it is probably a 407e4fc250cSLuigi Rizzo * broken driver trying to register multiple times. Checking 408e4fc250cSLuigi Rizzo * this at runtime is expensive, and won't solve the problem 409e4fc250cSLuigi Rizzo * anyways, so just report a few times and then give up. 410e4fc250cSLuigi Rizzo */ 411e4fc250cSLuigi Rizzo static int verbose = 10 ; 412e4fc250cSLuigi Rizzo if (verbose >0) { 41316901c01SGleb Smirnoff log(LOG_ERR, "poll handlers list full, " 414e4fc250cSLuigi Rizzo "maybe a broken driver ?\n"); 415e4fc250cSLuigi Rizzo verbose--; 416e4fc250cSLuigi Rizzo } 41716901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 41840929967SGleb Smirnoff return (ENOMEM); /* no polling for you */ 419e4fc250cSLuigi Rizzo } 420e4fc250cSLuigi Rizzo 42116901c01SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 42216901c01SGleb Smirnoff if (pr[i].ifp == ifp && pr[i].handler != NULL) { 42316901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 42416901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_register: %s: handler" 42516901c01SGleb Smirnoff " already registered\n", ifp->if_xname); 42640929967SGleb Smirnoff return (EEXIST); 42716901c01SGleb Smirnoff } 42816901c01SGleb Smirnoff 429e4fc250cSLuigi Rizzo pr[poll_handlers].handler = h; 430e4fc250cSLuigi Rizzo pr[poll_handlers].ifp = ifp; 431e4fc250cSLuigi Rizzo poll_handlers++; 43216901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 433d105c784SLuigi Rizzo if (idlepoll_sleeping) 434d105c784SLuigi Rizzo wakeup(&idlepoll_sleeping); 43540929967SGleb Smirnoff return (0); 436e4fc250cSLuigi Rizzo } 437e4fc250cSLuigi Rizzo 438e4fc250cSLuigi Rizzo /* 43940929967SGleb Smirnoff * Remove interface from the polling list. Called from *_ioctl(), too. 440e4fc250cSLuigi Rizzo */ 441e4fc250cSLuigi Rizzo int 442e4fc250cSLuigi Rizzo ether_poll_deregister(struct ifnet *ifp) 443e4fc250cSLuigi Rizzo { 444e4fc250cSLuigi Rizzo int i; 445e4fc250cSLuigi Rizzo 44640929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 44716901c01SGleb Smirnoff 44840929967SGleb Smirnoff NET_ASSERT_GIANT(); 44916901c01SGleb Smirnoff mtx_lock(&poll_mtx); 45040929967SGleb Smirnoff 451e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 452e4fc250cSLuigi Rizzo if (pr[i].ifp == ifp) /* found it */ 453e4fc250cSLuigi Rizzo break; 454e4fc250cSLuigi Rizzo if (i == poll_handlers) { 45516901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n", 45616901c01SGleb Smirnoff ifp->if_xname); 45740929967SGleb Smirnoff mtx_unlock(&poll_mtx); 45840929967SGleb Smirnoff return (ENOENT); 459e4fc250cSLuigi Rizzo } 460e4fc250cSLuigi Rizzo poll_handlers--; 461e4fc250cSLuigi Rizzo if (i < poll_handlers) { /* Last entry replaces this one. */ 462e4fc250cSLuigi Rizzo pr[i].handler = pr[poll_handlers].handler; 463e4fc250cSLuigi Rizzo pr[i].ifp = pr[poll_handlers].ifp; 464e4fc250cSLuigi Rizzo } 46516901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 46640929967SGleb Smirnoff return (0); 46740929967SGleb Smirnoff } 46840929967SGleb Smirnoff 46940929967SGleb Smirnoff /* 47040929967SGleb Smirnoff * Legacy interface for turning polling on all interfaces at one time. 47140929967SGleb Smirnoff */ 47240929967SGleb Smirnoff static int 47340929967SGleb Smirnoff poll_switch(SYSCTL_HANDLER_ARGS) 47440929967SGleb Smirnoff { 47540929967SGleb Smirnoff struct ifnet *ifp; 47640929967SGleb Smirnoff int error; 47740929967SGleb Smirnoff int val; 47840929967SGleb Smirnoff 47940929967SGleb Smirnoff mtx_lock(&poll_mtx); 48040929967SGleb Smirnoff val = polling; 48140929967SGleb Smirnoff mtx_unlock(&poll_mtx); 48240929967SGleb Smirnoff 48340929967SGleb Smirnoff error = sysctl_handle_int(oidp, &val, sizeof(int), req); 48440929967SGleb Smirnoff if (error || !req->newptr ) 48540929967SGleb Smirnoff return (error); 48640929967SGleb Smirnoff 48740929967SGleb Smirnoff if (val == polling) 48840929967SGleb Smirnoff return (0); 48940929967SGleb Smirnoff 49040929967SGleb Smirnoff if (val < 0 || val > 1) 49140929967SGleb Smirnoff return (EINVAL); 49240929967SGleb Smirnoff 49340929967SGleb Smirnoff mtx_lock(&poll_mtx); 49440929967SGleb Smirnoff polling = val; 49540929967SGleb Smirnoff mtx_unlock(&poll_mtx); 49640929967SGleb Smirnoff 49740929967SGleb Smirnoff NET_LOCK_GIANT(); 49840929967SGleb Smirnoff IFNET_RLOCK(); 49940929967SGleb Smirnoff TAILQ_FOREACH(ifp, &ifnet, if_link) { 50040929967SGleb Smirnoff if (ifp->if_capabilities & IFCAP_POLLING) { 50140929967SGleb Smirnoff struct ifreq ifr; 50240929967SGleb Smirnoff 50340929967SGleb Smirnoff if (val == 1) 50440929967SGleb Smirnoff ifr.ifr_reqcap = 50540929967SGleb Smirnoff ifp->if_capenable | IFCAP_POLLING; 50640929967SGleb Smirnoff else 50740929967SGleb Smirnoff ifr.ifr_reqcap = 50840929967SGleb Smirnoff ifp->if_capenable & ~IFCAP_POLLING; 50940929967SGleb Smirnoff IFF_LOCKGIANT(ifp); /* LOR here */ 51040929967SGleb Smirnoff (void) (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr); 51140929967SGleb Smirnoff IFF_UNLOCKGIANT(ifp); 51240929967SGleb Smirnoff } 51340929967SGleb Smirnoff } 51440929967SGleb Smirnoff IFNET_RUNLOCK(); 51540929967SGleb Smirnoff NET_UNLOCK_GIANT(); 51640929967SGleb Smirnoff 51740929967SGleb Smirnoff log(LOG_ERR, "kern.polling.enable is deprecated. Use ifconfig(8)"); 51840929967SGleb Smirnoff 51940929967SGleb Smirnoff return (0); 520e4fc250cSLuigi Rizzo } 521d105c784SLuigi Rizzo 522d105c784SLuigi Rizzo static void 523d105c784SLuigi Rizzo poll_idle(void) 524d105c784SLuigi Rizzo { 525d105c784SLuigi Rizzo struct thread *td = curthread; 526d105c784SLuigi Rizzo struct rtprio rtp; 527d105c784SLuigi Rizzo int pri; 528d105c784SLuigi Rizzo 529d105c784SLuigi Rizzo rtp.prio = RTP_PRIO_MAX; /* lowest priority */ 530d105c784SLuigi Rizzo rtp.type = RTP_PRIO_IDLE; 531d105c784SLuigi Rizzo mtx_lock_spin(&sched_lock); 532e5223044SLuigi Rizzo rtp_to_pri(&rtp, td->td_ksegrp); 5332c100766SJulian Elischer pri = td->td_priority; 534d105c784SLuigi Rizzo mtx_unlock_spin(&sched_lock); 535d105c784SLuigi Rizzo 536d105c784SLuigi Rizzo for (;;) { 537daccb638SLuigi Rizzo if (poll_in_idle_loop && poll_handlers > 0) { 538d105c784SLuigi Rizzo idlepoll_sleeping = 0; 539d105c784SLuigi Rizzo ether_poll(poll_each_burst); 540d105c784SLuigi Rizzo mtx_lock_spin(&sched_lock); 541b5cbda50SJohn Baldwin mi_switch(SW_VOL, NULL); 542d105c784SLuigi Rizzo mtx_unlock_spin(&sched_lock); 543d105c784SLuigi Rizzo } else { 544d105c784SLuigi Rizzo idlepoll_sleeping = 1; 545d105c784SLuigi Rizzo tsleep(&idlepoll_sleeping, pri, "pollid", hz * 3); 546d105c784SLuigi Rizzo } 547d105c784SLuigi Rizzo } 548d105c784SLuigi Rizzo } 549d105c784SLuigi Rizzo 550d105c784SLuigi Rizzo static struct proc *idlepoll; 551d105c784SLuigi Rizzo static struct kproc_desc idlepoll_kp = { 552d105c784SLuigi Rizzo "idlepoll", 553d105c784SLuigi Rizzo poll_idle, 554d105c784SLuigi Rizzo &idlepoll 555d105c784SLuigi Rizzo }; 556d105c784SLuigi Rizzo SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &idlepoll_kp) 557