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 31f0796cd2SGleb Smirnoff #include "opt_device_polling.h" 32f0796cd2SGleb Smirnoff 33e4fc250cSLuigi Rizzo #include <sys/param.h> 34e4fc250cSLuigi Rizzo #include <sys/systm.h> 35e4fc250cSLuigi Rizzo #include <sys/kernel.h> 36e4fc250cSLuigi Rizzo #include <sys/socket.h> /* needed by net/if.h */ 3740929967SGleb Smirnoff #include <sys/sockio.h> 38e4fc250cSLuigi Rizzo #include <sys/sysctl.h> 3916901c01SGleb Smirnoff #include <sys/syslog.h> 40e4fc250cSLuigi Rizzo 41e4fc250cSLuigi Rizzo #include <net/if.h> /* for IFF_* flags */ 42e4fc250cSLuigi Rizzo #include <net/netisr.h> /* for NETISR_POLL */ 43e4fc250cSLuigi Rizzo 44d105c784SLuigi Rizzo #include <sys/proc.h> 45d105c784SLuigi Rizzo #include <sys/resourcevar.h> 46d105c784SLuigi Rizzo #include <sys/kthread.h> 47d105c784SLuigi Rizzo 48daccb638SLuigi Rizzo static void netisr_poll(void); /* the two netisr handlers */ 491cafed39SJonathan Lemon static void netisr_pollmore(void); 5040929967SGleb Smirnoff static int poll_switch(SYSCTL_HANDLER_ARGS); 51daccb638SLuigi Rizzo 52daccb638SLuigi Rizzo void hardclock_device_poll(void); /* hook from hardclock */ 5340929967SGleb Smirnoff void ether_poll(int); /* polling in idle loop */ 54e4fc250cSLuigi Rizzo 55e113edf3SGleb Smirnoff static struct mtx poll_mtx; 56e113edf3SGleb Smirnoff 57e4fc250cSLuigi Rizzo /* 58e4fc250cSLuigi Rizzo * Polling support for [network] device drivers. 59e4fc250cSLuigi Rizzo * 6040929967SGleb Smirnoff * Drivers which support this feature can register with the 61e4fc250cSLuigi Rizzo * polling code. 62e4fc250cSLuigi Rizzo * 63e4fc250cSLuigi Rizzo * If registration is successful, the driver must disable interrupts, 64e4fc250cSLuigi Rizzo * and further I/O is performed through the handler, which is invoked 65e4fc250cSLuigi Rizzo * (at least once per clock tick) with 3 arguments: the "arg" passed at 66e4fc250cSLuigi Rizzo * register time (a struct ifnet pointer), a command, and a "count" limit. 67e4fc250cSLuigi Rizzo * 68e4fc250cSLuigi Rizzo * The command can be one of the following: 69e4fc250cSLuigi Rizzo * POLL_ONLY: quick move of "count" packets from input/output queues. 70e4fc250cSLuigi Rizzo * POLL_AND_CHECK_STATUS: as above, plus check status registers or do 71e4fc250cSLuigi Rizzo * other more expensive operations. This command is issued periodically 72e4fc250cSLuigi Rizzo * but less frequently than POLL_ONLY. 73e4fc250cSLuigi Rizzo * 74e4fc250cSLuigi Rizzo * The count limit specifies how much work the handler can do during the 75e4fc250cSLuigi Rizzo * call -- typically this is the number of packets to be received, or 76e4fc250cSLuigi Rizzo * transmitted, etc. (drivers are free to interpret this number, as long 77e4fc250cSLuigi Rizzo * as the max time spent in the function grows roughly linearly with the 78e4fc250cSLuigi Rizzo * count). 79e4fc250cSLuigi Rizzo * 8040929967SGleb Smirnoff * Polling is enabled and disabled via setting IFCAP_POLLING flag on 8140929967SGleb Smirnoff * the interface. The driver ioctl handler should register interface 8240929967SGleb Smirnoff * with polling and disable interrupts, if registration was successful. 83e4fc250cSLuigi Rizzo * 84e4fc250cSLuigi Rizzo * A second variable controls the sharing of CPU between polling/kernel 85e4fc250cSLuigi Rizzo * network processing, and other activities (typically userlevel tasks): 86e4fc250cSLuigi Rizzo * kern.polling.user_frac (between 0 and 100, default 50) sets the share 87e4fc250cSLuigi Rizzo * of CPU allocated to user tasks. CPU is allocated proportionally to the 88e4fc250cSLuigi Rizzo * shares, by dynamically adjusting the "count" (poll_burst). 89e4fc250cSLuigi Rizzo * 90e4fc250cSLuigi Rizzo * Other parameters can should be left to their default values. 91e4fc250cSLuigi Rizzo * The following constraints hold 92e4fc250cSLuigi Rizzo * 93e4fc250cSLuigi Rizzo * 1 <= poll_each_burst <= poll_burst <= poll_burst_max 9440929967SGleb Smirnoff * 0 <= poll_each_burst 95e4fc250cSLuigi Rizzo * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX 96e4fc250cSLuigi Rizzo */ 97e4fc250cSLuigi Rizzo 98e4fc250cSLuigi Rizzo #define MIN_POLL_BURST_MAX 10 99e4fc250cSLuigi Rizzo #define MAX_POLL_BURST_MAX 1000 100e4fc250cSLuigi Rizzo 101e113edf3SGleb Smirnoff static uint32_t poll_burst = 5; 102e113edf3SGleb Smirnoff static uint32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ 103e113edf3SGleb Smirnoff static uint32_t poll_each_burst = 5; 104e113edf3SGleb Smirnoff 105e4fc250cSLuigi Rizzo SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0, 106e4fc250cSLuigi Rizzo "Device polling parameters"); 107e4fc250cSLuigi Rizzo 108e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RD, 109e4fc250cSLuigi Rizzo &poll_burst, 0, "Current polling burst size"); 110e4fc250cSLuigi Rizzo 111e113edf3SGleb Smirnoff static int poll_burst_max_sysctl(SYSCTL_HANDLER_ARGS) 112e113edf3SGleb Smirnoff { 113e113edf3SGleb Smirnoff uint32_t val = poll_burst_max; 114e113edf3SGleb Smirnoff int error; 115e4fc250cSLuigi Rizzo 116e113edf3SGleb Smirnoff error = sysctl_handle_int(oidp, &val, sizeof(int), req); 117e113edf3SGleb Smirnoff if (error || !req->newptr ) 118e113edf3SGleb Smirnoff return (error); 119e113edf3SGleb Smirnoff if (val < MIN_POLL_BURST_MAX || val > MAX_POLL_BURST_MAX) 120e113edf3SGleb Smirnoff return (EINVAL); 121e4fc250cSLuigi Rizzo 122e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 123e113edf3SGleb Smirnoff poll_burst_max = val; 124e113edf3SGleb Smirnoff if (poll_burst > poll_burst_max) 125e113edf3SGleb Smirnoff poll_burst = poll_burst_max; 126e113edf3SGleb Smirnoff if (poll_each_burst > poll_burst_max) 127e113edf3SGleb Smirnoff poll_each_burst = MIN_POLL_BURST_MAX; 128e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 129e113edf3SGleb Smirnoff 130e113edf3SGleb Smirnoff return (0); 131e113edf3SGleb Smirnoff } 132e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, burst_max, CTLTYPE_UINT | CTLFLAG_RW, 133e113edf3SGleb Smirnoff 0, sizeof(uint32_t), poll_burst_max_sysctl, "I", "Max Polling burst size"); 134e113edf3SGleb Smirnoff 135e113edf3SGleb Smirnoff static int poll_each_burst_sysctl(SYSCTL_HANDLER_ARGS) 136e113edf3SGleb Smirnoff { 137e113edf3SGleb Smirnoff uint32_t val = poll_each_burst; 138e113edf3SGleb Smirnoff int error; 139e113edf3SGleb Smirnoff 140e113edf3SGleb Smirnoff error = sysctl_handle_int(oidp, &val, sizeof(int), req); 141e113edf3SGleb Smirnoff if (error || !req->newptr ) 142e113edf3SGleb Smirnoff return (error); 143e113edf3SGleb Smirnoff if (val < 1) 144e113edf3SGleb Smirnoff return (EINVAL); 145e113edf3SGleb Smirnoff 146e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 147e113edf3SGleb Smirnoff if (val > poll_burst_max) { 148e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 149e113edf3SGleb Smirnoff return (EINVAL); 150e113edf3SGleb Smirnoff } 151e113edf3SGleb Smirnoff poll_each_burst = val; 152e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 153e113edf3SGleb Smirnoff 154e113edf3SGleb Smirnoff return (0); 155e113edf3SGleb Smirnoff } 156e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, each_burst, CTLTYPE_UINT | CTLFLAG_RW, 157e113edf3SGleb Smirnoff 0, sizeof(uint32_t), poll_each_burst_sysctl, "I", 158e113edf3SGleb Smirnoff "Max size of each burst"); 159e113edf3SGleb Smirnoff 160e113edf3SGleb Smirnoff static uint32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */ 161daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW, 162daccb638SLuigi Rizzo &poll_in_idle_loop, 0, "Enable device polling in idle loop"); 163daccb638SLuigi Rizzo 164e113edf3SGleb Smirnoff static uint32_t user_frac = 50; 165e113edf3SGleb Smirnoff static int user_frac_sysctl(SYSCTL_HANDLER_ARGS) 166e113edf3SGleb Smirnoff { 167e113edf3SGleb Smirnoff uint32_t val = user_frac; 168e113edf3SGleb Smirnoff int error; 169e4fc250cSLuigi Rizzo 170e113edf3SGleb Smirnoff error = sysctl_handle_int(oidp, &val, sizeof(int), req); 171e113edf3SGleb Smirnoff if (error || !req->newptr ) 172e113edf3SGleb Smirnoff return (error); 173e113edf3SGleb Smirnoff if (val < 0 || val > 99) 174e113edf3SGleb Smirnoff return (EINVAL); 175e4fc250cSLuigi Rizzo 176e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 177e113edf3SGleb Smirnoff user_frac = val; 178e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 179e113edf3SGleb Smirnoff 180e113edf3SGleb Smirnoff return (0); 181e113edf3SGleb Smirnoff } 182e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, user_frac, CTLTYPE_UINT | CTLFLAG_RW, 183e113edf3SGleb Smirnoff 0, sizeof(uint32_t), user_frac_sysctl, "I", 184e113edf3SGleb Smirnoff "Desired user fraction of cpu time"); 185e113edf3SGleb Smirnoff 186e113edf3SGleb Smirnoff static uint32_t reg_frac_count = 0; 187e113edf3SGleb Smirnoff static uint32_t reg_frac = 20 ; 188e113edf3SGleb Smirnoff static int reg_frac_sysctl(SYSCTL_HANDLER_ARGS) 189e113edf3SGleb Smirnoff { 190e113edf3SGleb Smirnoff uint32_t val = reg_frac; 191e113edf3SGleb Smirnoff int error; 192e113edf3SGleb Smirnoff 193e113edf3SGleb Smirnoff error = sysctl_handle_int(oidp, &val, sizeof(int), req); 194e113edf3SGleb Smirnoff if (error || !req->newptr ) 195e113edf3SGleb Smirnoff return (error); 196e113edf3SGleb Smirnoff if (val < 1 || val > hz) 197e113edf3SGleb Smirnoff return (EINVAL); 198e113edf3SGleb Smirnoff 199e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 200e113edf3SGleb Smirnoff reg_frac = val; 201e113edf3SGleb Smirnoff if (reg_frac_count >= reg_frac) 202e113edf3SGleb Smirnoff reg_frac_count = 0; 203e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 204e113edf3SGleb Smirnoff 205e113edf3SGleb Smirnoff return (0); 206e113edf3SGleb Smirnoff } 207e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, reg_frac, CTLTYPE_UINT | CTLFLAG_RW, 208e113edf3SGleb Smirnoff 0, sizeof(uint32_t), reg_frac_sysctl, "I", 209e113edf3SGleb Smirnoff "Every this many cycles check registers"); 210e113edf3SGleb Smirnoff 211e113edf3SGleb Smirnoff static uint32_t short_ticks; 212e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RD, 213e4fc250cSLuigi Rizzo &short_ticks, 0, "Hardclock ticks shorter than they should be"); 214e4fc250cSLuigi Rizzo 215e113edf3SGleb Smirnoff static uint32_t lost_polls; 216e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RD, 217e4fc250cSLuigi Rizzo &lost_polls, 0, "How many times we would have lost a poll tick"); 218e4fc250cSLuigi Rizzo 219e113edf3SGleb Smirnoff static uint32_t pending_polls; 220e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RD, 221daccb638SLuigi Rizzo &pending_polls, 0, "Do we need to poll again"); 222daccb638SLuigi Rizzo 223daccb638SLuigi Rizzo static int residual_burst = 0; 224e113edf3SGleb Smirnoff SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RD, 225daccb638SLuigi Rizzo &residual_burst, 0, "# of residual cycles in burst"); 226daccb638SLuigi Rizzo 227e113edf3SGleb Smirnoff static uint32_t poll_handlers; /* next free entry in pr[]. */ 228daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, 229e4fc250cSLuigi Rizzo &poll_handlers, 0, "Number of registered poll handlers"); 230e4fc250cSLuigi Rizzo 23140929967SGleb Smirnoff static int polling = 0; 23240929967SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, enable, CTLTYPE_UINT | CTLFLAG_RW, 23340929967SGleb Smirnoff 0, sizeof(int), poll_switch, "I", "Switch polling for all interfaces"); 234e4fc250cSLuigi Rizzo 235e113edf3SGleb Smirnoff static uint32_t phase; 236e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RD, 237daccb638SLuigi Rizzo &phase, 0, "Polling phase"); 238e4fc250cSLuigi Rizzo 239e113edf3SGleb Smirnoff static uint32_t suspect; 240e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RD, 241daccb638SLuigi Rizzo &suspect, 0, "suspect event"); 242daccb638SLuigi Rizzo 243e113edf3SGleb Smirnoff static uint32_t stalled; 244e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RD, 245daccb638SLuigi Rizzo &stalled, 0, "potential stalls"); 246daccb638SLuigi Rizzo 247e113edf3SGleb Smirnoff static uint32_t idlepoll_sleeping; /* idlepoll is sleeping */ 248daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD, 249daccb638SLuigi Rizzo &idlepoll_sleeping, 0, "idlepoll is sleeping"); 250daccb638SLuigi Rizzo 251e4fc250cSLuigi Rizzo 252e4fc250cSLuigi Rizzo #define POLL_LIST_LEN 128 253e4fc250cSLuigi Rizzo struct pollrec { 254e4fc250cSLuigi Rizzo poll_handler_t *handler; 255e4fc250cSLuigi Rizzo struct ifnet *ifp; 256e4fc250cSLuigi Rizzo }; 257e4fc250cSLuigi Rizzo 258e4fc250cSLuigi Rizzo static struct pollrec pr[POLL_LIST_LEN]; 25916901c01SGleb Smirnoff 2601cafed39SJonathan Lemon static void 261daccb638SLuigi Rizzo init_device_poll(void) 262daccb638SLuigi Rizzo { 2631cafed39SJonathan Lemon 26416901c01SGleb Smirnoff mtx_init(&poll_mtx, "polling", NULL, MTX_DEF); 26516901c01SGleb Smirnoff netisr_register(NETISR_POLL, (netisr_t *)netisr_poll, NULL, 26616901c01SGleb Smirnoff NETISR_MPSAFE); 26716901c01SGleb Smirnoff netisr_register(NETISR_POLLMORE, (netisr_t *)netisr_pollmore, NULL, 26816901c01SGleb Smirnoff NETISR_MPSAFE); 269daccb638SLuigi Rizzo } 2701cafed39SJonathan Lemon SYSINIT(device_poll, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, init_device_poll, NULL) 2711cafed39SJonathan Lemon 272daccb638SLuigi Rizzo 273daccb638SLuigi Rizzo /* 274e4fc250cSLuigi Rizzo * Hook from hardclock. Tries to schedule a netisr, but keeps track 275e4fc250cSLuigi Rizzo * of lost ticks due to the previous handler taking too long. 276d26d355fSLuigi Rizzo * Normally, this should not happen, because polling handler should 277d26d355fSLuigi Rizzo * run for a short time. However, in some cases (e.g. when there are 278d26d355fSLuigi Rizzo * changes in link status etc.) the drivers take a very long time 279d26d355fSLuigi Rizzo * (even in the order of milliseconds) to reset and reconfigure the 280d26d355fSLuigi Rizzo * device, causing apparent lost polls. 281d26d355fSLuigi Rizzo * 282e4fc250cSLuigi Rizzo * The first part of the code is just for debugging purposes, and tries 283e4fc250cSLuigi Rizzo * to count how often hardclock ticks are shorter than they should, 284e4fc250cSLuigi Rizzo * meaning either stray interrupts or delayed events. 285e4fc250cSLuigi Rizzo */ 286e4fc250cSLuigi Rizzo void 287e4fc250cSLuigi Rizzo hardclock_device_poll(void) 288e4fc250cSLuigi Rizzo { 289e4fc250cSLuigi Rizzo static struct timeval prev_t, t; 290e4fc250cSLuigi Rizzo int delta; 291e4fc250cSLuigi Rizzo 292daccb638SLuigi Rizzo if (poll_handlers == 0) 293daccb638SLuigi Rizzo return; 294daccb638SLuigi Rizzo 295e4fc250cSLuigi Rizzo microuptime(&t); 296e4fc250cSLuigi Rizzo delta = (t.tv_usec - prev_t.tv_usec) + 297e4fc250cSLuigi Rizzo (t.tv_sec - prev_t.tv_sec)*1000000; 298e4fc250cSLuigi Rizzo if (delta * hz < 500000) 299e4fc250cSLuigi Rizzo short_ticks++; 300e4fc250cSLuigi Rizzo else 301e4fc250cSLuigi Rizzo prev_t = t; 302e4fc250cSLuigi Rizzo 303daccb638SLuigi Rizzo if (pending_polls > 100) { 304d26d355fSLuigi Rizzo /* 305d26d355fSLuigi Rizzo * Too much, assume it has stalled (not always true 306d26d355fSLuigi Rizzo * see comment above). 307d26d355fSLuigi Rizzo */ 308daccb638SLuigi Rizzo stalled++; 309daccb638SLuigi Rizzo pending_polls = 0; 310daccb638SLuigi Rizzo phase = 0; 311daccb638SLuigi Rizzo } 312daccb638SLuigi Rizzo 313daccb638SLuigi Rizzo if (phase <= 2) { 314daccb638SLuigi Rizzo if (phase != 0) 315daccb638SLuigi Rizzo suspect++; 316daccb638SLuigi Rizzo phase = 1; 3171cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 318daccb638SLuigi Rizzo phase = 2; 319e4fc250cSLuigi Rizzo } 320daccb638SLuigi Rizzo if (pending_polls++ > 0) 321daccb638SLuigi Rizzo lost_polls++; 322e4fc250cSLuigi Rizzo } 323e4fc250cSLuigi Rizzo 324e4fc250cSLuigi Rizzo /* 32540929967SGleb Smirnoff * ether_poll is called from the idle loop. 326e4fc250cSLuigi Rizzo */ 327e4fc250cSLuigi Rizzo void 328e4fc250cSLuigi Rizzo ether_poll(int count) 329e4fc250cSLuigi Rizzo { 330e4fc250cSLuigi Rizzo int i; 331e4fc250cSLuigi Rizzo 33240929967SGleb Smirnoff NET_LOCK_GIANT(); 33316901c01SGleb Smirnoff mtx_lock(&poll_mtx); 334e4fc250cSLuigi Rizzo 335e4fc250cSLuigi Rizzo if (count > poll_each_burst) 336e4fc250cSLuigi Rizzo count = poll_each_burst; 33716901c01SGleb Smirnoff 33840929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 33916901c01SGleb Smirnoff pr[i].handler(pr[i].ifp, POLL_ONLY, count); 34040929967SGleb Smirnoff 34116901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 34240929967SGleb Smirnoff NET_UNLOCK_GIANT(); 343e4fc250cSLuigi Rizzo } 344e4fc250cSLuigi Rizzo 345e4fc250cSLuigi Rizzo /* 346daccb638SLuigi Rizzo * netisr_pollmore is called after other netisr's, possibly scheduling 347e4fc250cSLuigi Rizzo * another NETISR_POLL call, or adapting the burst size for the next cycle. 348e4fc250cSLuigi Rizzo * 349e4fc250cSLuigi Rizzo * It is very bad to fetch large bursts of packets from a single card at once, 350e4fc250cSLuigi Rizzo * because the burst could take a long time to be completely processed, or 351e4fc250cSLuigi Rizzo * could saturate the intermediate queue (ipintrq or similar) leading to 352e4fc250cSLuigi Rizzo * losses or unfairness. To reduce the problem, and also to account better for 353daccb638SLuigi Rizzo * time spent in network-related processing, we split the burst in smaller 354e4fc250cSLuigi Rizzo * chunks of fixed size, giving control to the other netisr's between chunks. 355e4fc250cSLuigi Rizzo * This helps in improving the fairness, reducing livelock (because we 356e4fc250cSLuigi Rizzo * emulate more closely the "process to completion" that we have with 357e4fc250cSLuigi Rizzo * fastforwarding) and accounting for the work performed in low level 358e4fc250cSLuigi Rizzo * handling and forwarding. 359e4fc250cSLuigi Rizzo */ 360e4fc250cSLuigi Rizzo 361e4fc250cSLuigi Rizzo static struct timeval poll_start_t; 362e4fc250cSLuigi Rizzo 363e4fc250cSLuigi Rizzo void 364daccb638SLuigi Rizzo netisr_pollmore() 365e4fc250cSLuigi Rizzo { 366e4fc250cSLuigi Rizzo struct timeval t; 367e4fc250cSLuigi Rizzo int kern_load; 368e4fc250cSLuigi Rizzo 36916901c01SGleb Smirnoff NET_ASSERT_GIANT(); 37016901c01SGleb Smirnoff 37116901c01SGleb Smirnoff mtx_lock(&poll_mtx); 372daccb638SLuigi Rizzo phase = 5; 373e4fc250cSLuigi Rizzo if (residual_burst > 0) { 3741cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 37516901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 376e4fc250cSLuigi Rizzo /* will run immediately on return, followed by netisrs */ 377e4fc250cSLuigi Rizzo return; 378e4fc250cSLuigi Rizzo } 379e4fc250cSLuigi Rizzo /* here we can account time spent in netisr's in this tick */ 380e4fc250cSLuigi Rizzo microuptime(&t); 381e4fc250cSLuigi Rizzo kern_load = (t.tv_usec - poll_start_t.tv_usec) + 382e4fc250cSLuigi Rizzo (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ 383e4fc250cSLuigi Rizzo kern_load = (kern_load * hz) / 10000; /* 0..100 */ 384e4fc250cSLuigi Rizzo if (kern_load > (100 - user_frac)) { /* try decrease ticks */ 385e4fc250cSLuigi Rizzo if (poll_burst > 1) 386e4fc250cSLuigi Rizzo poll_burst--; 387e4fc250cSLuigi Rizzo } else { 388e4fc250cSLuigi Rizzo if (poll_burst < poll_burst_max) 389e4fc250cSLuigi Rizzo poll_burst++; 390e4fc250cSLuigi Rizzo } 391e4fc250cSLuigi Rizzo 392daccb638SLuigi Rizzo pending_polls--; 393daccb638SLuigi Rizzo if (pending_polls == 0) /* we are done */ 394daccb638SLuigi Rizzo phase = 0; 395daccb638SLuigi Rizzo else { 396e4fc250cSLuigi Rizzo /* 397e4fc250cSLuigi Rizzo * Last cycle was long and caused us to miss one or more 398daccb638SLuigi Rizzo * hardclock ticks. Restart processing again, but slightly 399e4fc250cSLuigi Rizzo * reduce the burst size to prevent that this happens again. 400e4fc250cSLuigi Rizzo */ 401e4fc250cSLuigi Rizzo poll_burst -= (poll_burst / 8); 402e4fc250cSLuigi Rizzo if (poll_burst < 1) 403e4fc250cSLuigi Rizzo poll_burst = 1; 4041cafed39SJonathan Lemon schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 405daccb638SLuigi Rizzo phase = 6; 406daccb638SLuigi Rizzo } 40716901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 408e4fc250cSLuigi Rizzo } 409e4fc250cSLuigi Rizzo 410e4fc250cSLuigi Rizzo /* 411daccb638SLuigi Rizzo * netisr_poll is scheduled by schednetisr when appropriate, typically once 41216901c01SGleb Smirnoff * per tick. 413e4fc250cSLuigi Rizzo */ 414daccb638SLuigi Rizzo static void 415daccb638SLuigi Rizzo netisr_poll(void) 416e4fc250cSLuigi Rizzo { 417e4fc250cSLuigi Rizzo int i, cycles; 418e4fc250cSLuigi Rizzo enum poll_cmd arg = POLL_ONLY; 419e4fc250cSLuigi Rizzo 42016901c01SGleb Smirnoff NET_ASSERT_GIANT(); 42116901c01SGleb Smirnoff 42216901c01SGleb Smirnoff mtx_lock(&poll_mtx); 423daccb638SLuigi Rizzo phase = 3; 424e4fc250cSLuigi Rizzo if (residual_burst == 0) { /* first call in this tick */ 425e4fc250cSLuigi Rizzo microuptime(&poll_start_t); 426e113edf3SGleb Smirnoff if (++reg_frac_count == reg_frac) { 427e4fc250cSLuigi Rizzo arg = POLL_AND_CHECK_STATUS; 428e113edf3SGleb Smirnoff reg_frac_count = 0; 429e4fc250cSLuigi Rizzo } 430e4fc250cSLuigi Rizzo 431e4fc250cSLuigi Rizzo residual_burst = poll_burst; 432e4fc250cSLuigi Rizzo } 433e4fc250cSLuigi Rizzo cycles = (residual_burst < poll_each_burst) ? 434e4fc250cSLuigi Rizzo residual_burst : poll_each_burst; 435e4fc250cSLuigi Rizzo residual_burst -= cycles; 436e4fc250cSLuigi Rizzo 43740929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 438e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, arg, cycles); 43916901c01SGleb Smirnoff 440daccb638SLuigi Rizzo phase = 4; 44116901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 442e4fc250cSLuigi Rizzo } 443e4fc250cSLuigi Rizzo 444e4fc250cSLuigi Rizzo /* 44540929967SGleb Smirnoff * Try to register routine for polling. Returns 0 if successful 44640929967SGleb Smirnoff * (and polling should be enabled), error code otherwise. 447e4fc250cSLuigi Rizzo * A device is not supposed to register itself multiple times. 448e4fc250cSLuigi Rizzo * 44940929967SGleb Smirnoff * This is called from within the *_ioctl() functions. 450e4fc250cSLuigi Rizzo */ 451e4fc250cSLuigi Rizzo int 452e4fc250cSLuigi Rizzo ether_poll_register(poll_handler_t *h, struct ifnet *ifp) 453e4fc250cSLuigi Rizzo { 45416901c01SGleb Smirnoff int i; 45516901c01SGleb Smirnoff 45640929967SGleb Smirnoff KASSERT(h != NULL, ("%s: handler is NULL", __func__)); 45740929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 458e4fc250cSLuigi Rizzo 45940929967SGleb Smirnoff NET_ASSERT_GIANT(); 460e4fc250cSLuigi Rizzo 46116901c01SGleb Smirnoff mtx_lock(&poll_mtx); 462e4fc250cSLuigi Rizzo if (poll_handlers >= POLL_LIST_LEN) { 463e4fc250cSLuigi Rizzo /* 464e4fc250cSLuigi Rizzo * List full, cannot register more entries. 465e4fc250cSLuigi Rizzo * This should never happen; if it does, it is probably a 466e4fc250cSLuigi Rizzo * broken driver trying to register multiple times. Checking 467e4fc250cSLuigi Rizzo * this at runtime is expensive, and won't solve the problem 468e4fc250cSLuigi Rizzo * anyways, so just report a few times and then give up. 469e4fc250cSLuigi Rizzo */ 470e4fc250cSLuigi Rizzo static int verbose = 10 ; 471e4fc250cSLuigi Rizzo if (verbose >0) { 47216901c01SGleb Smirnoff log(LOG_ERR, "poll handlers list full, " 473e4fc250cSLuigi Rizzo "maybe a broken driver ?\n"); 474e4fc250cSLuigi Rizzo verbose--; 475e4fc250cSLuigi Rizzo } 47616901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 47740929967SGleb Smirnoff return (ENOMEM); /* no polling for you */ 478e4fc250cSLuigi Rizzo } 479e4fc250cSLuigi Rizzo 48016901c01SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 48116901c01SGleb Smirnoff if (pr[i].ifp == ifp && pr[i].handler != NULL) { 48216901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 48316901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_register: %s: handler" 48416901c01SGleb Smirnoff " already registered\n", ifp->if_xname); 48540929967SGleb Smirnoff return (EEXIST); 48616901c01SGleb Smirnoff } 48716901c01SGleb Smirnoff 488e4fc250cSLuigi Rizzo pr[poll_handlers].handler = h; 489e4fc250cSLuigi Rizzo pr[poll_handlers].ifp = ifp; 490e4fc250cSLuigi Rizzo poll_handlers++; 49116901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 492d105c784SLuigi Rizzo if (idlepoll_sleeping) 493d105c784SLuigi Rizzo wakeup(&idlepoll_sleeping); 49440929967SGleb Smirnoff return (0); 495e4fc250cSLuigi Rizzo } 496e4fc250cSLuigi Rizzo 497e4fc250cSLuigi Rizzo /* 49840929967SGleb Smirnoff * Remove interface from the polling list. Called from *_ioctl(), too. 499e4fc250cSLuigi Rizzo */ 500e4fc250cSLuigi Rizzo int 501e4fc250cSLuigi Rizzo ether_poll_deregister(struct ifnet *ifp) 502e4fc250cSLuigi Rizzo { 503e4fc250cSLuigi Rizzo int i; 504e4fc250cSLuigi Rizzo 50540929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 50616901c01SGleb Smirnoff 50740929967SGleb Smirnoff NET_ASSERT_GIANT(); 50816901c01SGleb Smirnoff mtx_lock(&poll_mtx); 50940929967SGleb Smirnoff 510e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 511e4fc250cSLuigi Rizzo if (pr[i].ifp == ifp) /* found it */ 512e4fc250cSLuigi Rizzo break; 513e4fc250cSLuigi Rizzo if (i == poll_handlers) { 51416901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n", 51516901c01SGleb Smirnoff ifp->if_xname); 51640929967SGleb Smirnoff mtx_unlock(&poll_mtx); 51740929967SGleb Smirnoff return (ENOENT); 518e4fc250cSLuigi Rizzo } 519e4fc250cSLuigi Rizzo poll_handlers--; 520e4fc250cSLuigi Rizzo if (i < poll_handlers) { /* Last entry replaces this one. */ 521e4fc250cSLuigi Rizzo pr[i].handler = pr[poll_handlers].handler; 522e4fc250cSLuigi Rizzo pr[i].ifp = pr[poll_handlers].ifp; 523e4fc250cSLuigi Rizzo } 52416901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 52540929967SGleb Smirnoff return (0); 52640929967SGleb Smirnoff } 52740929967SGleb Smirnoff 52840929967SGleb Smirnoff /* 52940929967SGleb Smirnoff * Legacy interface for turning polling on all interfaces at one time. 53040929967SGleb Smirnoff */ 53140929967SGleb Smirnoff static int 53240929967SGleb Smirnoff poll_switch(SYSCTL_HANDLER_ARGS) 53340929967SGleb Smirnoff { 53440929967SGleb Smirnoff struct ifnet *ifp; 53540929967SGleb Smirnoff int error; 536e113edf3SGleb Smirnoff int val = polling; 53740929967SGleb Smirnoff 53840929967SGleb Smirnoff error = sysctl_handle_int(oidp, &val, sizeof(int), req); 53940929967SGleb Smirnoff if (error || !req->newptr ) 54040929967SGleb Smirnoff return (error); 54140929967SGleb Smirnoff 54240929967SGleb Smirnoff if (val == polling) 54340929967SGleb Smirnoff return (0); 54440929967SGleb Smirnoff 54540929967SGleb Smirnoff if (val < 0 || val > 1) 54640929967SGleb Smirnoff return (EINVAL); 54740929967SGleb Smirnoff 54840929967SGleb Smirnoff polling = val; 54940929967SGleb Smirnoff 55040929967SGleb Smirnoff NET_LOCK_GIANT(); 55140929967SGleb Smirnoff IFNET_RLOCK(); 55240929967SGleb Smirnoff TAILQ_FOREACH(ifp, &ifnet, if_link) { 55340929967SGleb Smirnoff if (ifp->if_capabilities & IFCAP_POLLING) { 55440929967SGleb Smirnoff struct ifreq ifr; 55540929967SGleb Smirnoff 55640929967SGleb Smirnoff if (val == 1) 55740929967SGleb Smirnoff ifr.ifr_reqcap = 55840929967SGleb Smirnoff ifp->if_capenable | IFCAP_POLLING; 55940929967SGleb Smirnoff else 56040929967SGleb Smirnoff ifr.ifr_reqcap = 56140929967SGleb Smirnoff ifp->if_capenable & ~IFCAP_POLLING; 56240929967SGleb Smirnoff IFF_LOCKGIANT(ifp); /* LOR here */ 56340929967SGleb Smirnoff (void) (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr); 56440929967SGleb Smirnoff IFF_UNLOCKGIANT(ifp); 56540929967SGleb Smirnoff } 56640929967SGleb Smirnoff } 56740929967SGleb Smirnoff IFNET_RUNLOCK(); 56840929967SGleb Smirnoff NET_UNLOCK_GIANT(); 56940929967SGleb Smirnoff 57040929967SGleb Smirnoff log(LOG_ERR, "kern.polling.enable is deprecated. Use ifconfig(8)"); 57140929967SGleb Smirnoff 57240929967SGleb Smirnoff return (0); 573e4fc250cSLuigi Rizzo } 574d105c784SLuigi Rizzo 575d105c784SLuigi Rizzo static void 576d105c784SLuigi Rizzo poll_idle(void) 577d105c784SLuigi Rizzo { 578d105c784SLuigi Rizzo struct thread *td = curthread; 579d105c784SLuigi Rizzo struct rtprio rtp; 580d105c784SLuigi Rizzo int pri; 581d105c784SLuigi Rizzo 582d105c784SLuigi Rizzo rtp.prio = RTP_PRIO_MAX; /* lowest priority */ 583d105c784SLuigi Rizzo rtp.type = RTP_PRIO_IDLE; 584d105c784SLuigi Rizzo mtx_lock_spin(&sched_lock); 585e5223044SLuigi Rizzo rtp_to_pri(&rtp, td->td_ksegrp); 5862c100766SJulian Elischer pri = td->td_priority; 587d105c784SLuigi Rizzo mtx_unlock_spin(&sched_lock); 588d105c784SLuigi Rizzo 589d105c784SLuigi Rizzo for (;;) { 590daccb638SLuigi Rizzo if (poll_in_idle_loop && poll_handlers > 0) { 591d105c784SLuigi Rizzo idlepoll_sleeping = 0; 592d105c784SLuigi Rizzo ether_poll(poll_each_burst); 593d105c784SLuigi Rizzo mtx_lock_spin(&sched_lock); 594b5cbda50SJohn Baldwin mi_switch(SW_VOL, NULL); 595d105c784SLuigi Rizzo mtx_unlock_spin(&sched_lock); 596d105c784SLuigi Rizzo } else { 597d105c784SLuigi Rizzo idlepoll_sleeping = 1; 598d105c784SLuigi Rizzo tsleep(&idlepoll_sleeping, pri, "pollid", hz * 3); 599d105c784SLuigi Rizzo } 600d105c784SLuigi Rizzo } 601d105c784SLuigi Rizzo } 602d105c784SLuigi Rizzo 603d105c784SLuigi Rizzo static struct proc *idlepoll; 604d105c784SLuigi Rizzo static struct kproc_desc idlepoll_kp = { 605d105c784SLuigi Rizzo "idlepoll", 606d105c784SLuigi Rizzo poll_idle, 607d105c784SLuigi Rizzo &idlepoll 608d105c784SLuigi Rizzo }; 609d105c784SLuigi Rizzo SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, &idlepoll_kp) 610