1e4fc250cSLuigi Rizzo /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 4daccb638SLuigi Rizzo * Copyright (c) 2001-2002 Luigi Rizzo 5daccb638SLuigi Rizzo * 6daccb638SLuigi Rizzo * Supported by: the Xorp Project (www.xorp.org) 7e4fc250cSLuigi Rizzo * 8e4fc250cSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 9e4fc250cSLuigi Rizzo * modification, are permitted provided that the following conditions 10e4fc250cSLuigi Rizzo * are met: 11e4fc250cSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 12e4fc250cSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 13e4fc250cSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 14e4fc250cSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 15e4fc250cSLuigi Rizzo * documentation and/or other materials provided with the distribution. 16e4fc250cSLuigi Rizzo * 17e4fc250cSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18e4fc250cSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19e4fc250cSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20e4fc250cSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21e4fc250cSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22e4fc250cSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23e4fc250cSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24e4fc250cSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25e4fc250cSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26e4fc250cSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27e4fc250cSLuigi Rizzo * SUCH DAMAGE. 28e4fc250cSLuigi Rizzo */ 29e4fc250cSLuigi Rizzo 30677b542eSDavid E. O'Brien #include <sys/cdefs.h> 31677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 32677b542eSDavid E. O'Brien 33f0796cd2SGleb Smirnoff #include "opt_device_polling.h" 34f0796cd2SGleb Smirnoff 35e4fc250cSLuigi Rizzo #include <sys/param.h> 36e4fc250cSLuigi Rizzo #include <sys/systm.h> 37e4fc250cSLuigi Rizzo #include <sys/kernel.h> 389ea9ef7eSBjoern A. Zeeb #include <sys/kthread.h> 399ea9ef7eSBjoern A. Zeeb #include <sys/proc.h> 40ad398012SGleb Smirnoff #include <sys/epoch.h> 41d4b5cae4SRobert Watson #include <sys/eventhandler.h> 429ea9ef7eSBjoern A. Zeeb #include <sys/resourcevar.h> 43e4fc250cSLuigi Rizzo #include <sys/socket.h> /* needed by net/if.h */ 4440929967SGleb Smirnoff #include <sys/sockio.h> 45e4fc250cSLuigi Rizzo #include <sys/sysctl.h> 4616901c01SGleb Smirnoff #include <sys/syslog.h> 47e4fc250cSLuigi Rizzo 4876039bc8SGleb Smirnoff #include <net/if.h> 4976039bc8SGleb Smirnoff #include <net/if_var.h> 50e4fc250cSLuigi Rizzo #include <net/netisr.h> /* for NETISR_POLL */ 514b79449eSBjoern A. Zeeb #include <net/vnet.h> 52e4fc250cSLuigi Rizzo 53daccb638SLuigi Rizzo void hardclock_device_poll(void); /* hook from hardclock */ 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 94e4fc250cSLuigi Rizzo * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX 95e4fc250cSLuigi Rizzo */ 96e4fc250cSLuigi Rizzo 97e4fc250cSLuigi Rizzo #define MIN_POLL_BURST_MAX 10 984b62214fSLuigi Rizzo #define MAX_POLL_BURST_MAX 20000 99e4fc250cSLuigi Rizzo 100e113edf3SGleb Smirnoff static uint32_t poll_burst = 5; 101e113edf3SGleb Smirnoff static uint32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ 102e113edf3SGleb Smirnoff static uint32_t poll_each_burst = 5; 103e113edf3SGleb Smirnoff 104*7029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 105e4fc250cSLuigi Rizzo "Device polling parameters"); 106e4fc250cSLuigi Rizzo 107e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RD, 108e4fc250cSLuigi Rizzo &poll_burst, 0, "Current polling burst size"); 109e4fc250cSLuigi Rizzo 110d4b5cae4SRobert Watson static int netisr_poll_scheduled; 111d4b5cae4SRobert Watson static int netisr_pollmore_scheduled; 112d4b5cae4SRobert Watson static int poll_shutting_down; 113d4b5cae4SRobert Watson 114e113edf3SGleb Smirnoff static int poll_burst_max_sysctl(SYSCTL_HANDLER_ARGS) 115e113edf3SGleb Smirnoff { 116e113edf3SGleb Smirnoff uint32_t val = poll_burst_max; 117e113edf3SGleb Smirnoff int error; 118e4fc250cSLuigi Rizzo 119041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 120e113edf3SGleb Smirnoff if (error || !req->newptr ) 121e113edf3SGleb Smirnoff return (error); 122e113edf3SGleb Smirnoff if (val < MIN_POLL_BURST_MAX || val > MAX_POLL_BURST_MAX) 123e113edf3SGleb Smirnoff return (EINVAL); 124e4fc250cSLuigi Rizzo 125e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 126e113edf3SGleb Smirnoff poll_burst_max = val; 127e113edf3SGleb Smirnoff if (poll_burst > poll_burst_max) 128e113edf3SGleb Smirnoff poll_burst = poll_burst_max; 129e113edf3SGleb Smirnoff if (poll_each_burst > poll_burst_max) 130e113edf3SGleb Smirnoff poll_each_burst = MIN_POLL_BURST_MAX; 131e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 132e113edf3SGleb Smirnoff 133e113edf3SGleb Smirnoff return (0); 134e113edf3SGleb Smirnoff } 135*7029da5cSPawel Biernacki SYSCTL_PROC(_kern_polling, OID_AUTO, burst_max, 136*7029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(uint32_t), 137*7029da5cSPawel Biernacki poll_burst_max_sysctl, "I", 138*7029da5cSPawel Biernacki "Max Polling burst size"); 139e113edf3SGleb Smirnoff 140e113edf3SGleb Smirnoff static int poll_each_burst_sysctl(SYSCTL_HANDLER_ARGS) 141e113edf3SGleb Smirnoff { 142e113edf3SGleb Smirnoff uint32_t val = poll_each_burst; 143e113edf3SGleb Smirnoff int error; 144e113edf3SGleb Smirnoff 145041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 146e113edf3SGleb Smirnoff if (error || !req->newptr ) 147e113edf3SGleb Smirnoff return (error); 148e113edf3SGleb Smirnoff if (val < 1) 149e113edf3SGleb Smirnoff return (EINVAL); 150e113edf3SGleb Smirnoff 151e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 152e113edf3SGleb Smirnoff if (val > poll_burst_max) { 153e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 154e113edf3SGleb Smirnoff return (EINVAL); 155e113edf3SGleb Smirnoff } 156e113edf3SGleb Smirnoff poll_each_burst = val; 157e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 158e113edf3SGleb Smirnoff 159e113edf3SGleb Smirnoff return (0); 160e113edf3SGleb Smirnoff } 161*7029da5cSPawel Biernacki SYSCTL_PROC(_kern_polling, OID_AUTO, each_burst, 162*7029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(uint32_t), 163*7029da5cSPawel Biernacki poll_each_burst_sysctl, "I", 164e113edf3SGleb Smirnoff "Max size of each burst"); 165e113edf3SGleb Smirnoff 166e113edf3SGleb Smirnoff static uint32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */ 167daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW, 168daccb638SLuigi Rizzo &poll_in_idle_loop, 0, "Enable device polling in idle loop"); 169daccb638SLuigi Rizzo 170e113edf3SGleb Smirnoff static uint32_t user_frac = 50; 171e113edf3SGleb Smirnoff static int user_frac_sysctl(SYSCTL_HANDLER_ARGS) 172e113edf3SGleb Smirnoff { 173e113edf3SGleb Smirnoff uint32_t val = user_frac; 174e113edf3SGleb Smirnoff int error; 175e4fc250cSLuigi Rizzo 176041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 177e113edf3SGleb Smirnoff if (error || !req->newptr ) 178e113edf3SGleb Smirnoff return (error); 1793de1bd95SKevin Lo if (val > 99) 180e113edf3SGleb Smirnoff return (EINVAL); 181e4fc250cSLuigi Rizzo 182e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 183e113edf3SGleb Smirnoff user_frac = val; 184e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 185e113edf3SGleb Smirnoff 186e113edf3SGleb Smirnoff return (0); 187e113edf3SGleb Smirnoff } 188*7029da5cSPawel Biernacki SYSCTL_PROC(_kern_polling, OID_AUTO, user_frac, 189*7029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(uint32_t), 190*7029da5cSPawel Biernacki user_frac_sysctl, "I", 191e113edf3SGleb Smirnoff "Desired user fraction of cpu time"); 192e113edf3SGleb Smirnoff 193e113edf3SGleb Smirnoff static uint32_t reg_frac_count = 0; 194e113edf3SGleb Smirnoff static uint32_t reg_frac = 20 ; 195e113edf3SGleb Smirnoff static int reg_frac_sysctl(SYSCTL_HANDLER_ARGS) 196e113edf3SGleb Smirnoff { 197e113edf3SGleb Smirnoff uint32_t val = reg_frac; 198e113edf3SGleb Smirnoff int error; 199e113edf3SGleb Smirnoff 200041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 201e113edf3SGleb Smirnoff if (error || !req->newptr ) 202e113edf3SGleb Smirnoff return (error); 203e113edf3SGleb Smirnoff if (val < 1 || val > hz) 204e113edf3SGleb Smirnoff return (EINVAL); 205e113edf3SGleb Smirnoff 206e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 207e113edf3SGleb Smirnoff reg_frac = val; 208e113edf3SGleb Smirnoff if (reg_frac_count >= reg_frac) 209e113edf3SGleb Smirnoff reg_frac_count = 0; 210e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 211e113edf3SGleb Smirnoff 212e113edf3SGleb Smirnoff return (0); 213e113edf3SGleb Smirnoff } 214*7029da5cSPawel Biernacki SYSCTL_PROC(_kern_polling, OID_AUTO, reg_frac, 215*7029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(uint32_t), 216*7029da5cSPawel Biernacki reg_frac_sysctl, "I", 217e113edf3SGleb Smirnoff "Every this many cycles check registers"); 218e113edf3SGleb Smirnoff 219e113edf3SGleb Smirnoff static uint32_t short_ticks; 220e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RD, 221e4fc250cSLuigi Rizzo &short_ticks, 0, "Hardclock ticks shorter than they should be"); 222e4fc250cSLuigi Rizzo 223e113edf3SGleb Smirnoff static uint32_t lost_polls; 224e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RD, 225e4fc250cSLuigi Rizzo &lost_polls, 0, "How many times we would have lost a poll tick"); 226e4fc250cSLuigi Rizzo 227e113edf3SGleb Smirnoff static uint32_t pending_polls; 228e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RD, 229daccb638SLuigi Rizzo &pending_polls, 0, "Do we need to poll again"); 230daccb638SLuigi Rizzo 231daccb638SLuigi Rizzo static int residual_burst = 0; 232e113edf3SGleb Smirnoff SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RD, 233daccb638SLuigi Rizzo &residual_burst, 0, "# of residual cycles in burst"); 234daccb638SLuigi Rizzo 235e113edf3SGleb Smirnoff static uint32_t poll_handlers; /* next free entry in pr[]. */ 236daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, 237e4fc250cSLuigi Rizzo &poll_handlers, 0, "Number of registered poll handlers"); 238e4fc250cSLuigi Rizzo 239e113edf3SGleb Smirnoff static uint32_t phase; 240e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RD, 241daccb638SLuigi Rizzo &phase, 0, "Polling phase"); 242e4fc250cSLuigi Rizzo 243e113edf3SGleb Smirnoff static uint32_t suspect; 244e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RD, 245daccb638SLuigi Rizzo &suspect, 0, "suspect event"); 246daccb638SLuigi Rizzo 247e113edf3SGleb Smirnoff static uint32_t stalled; 248e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RD, 249daccb638SLuigi Rizzo &stalled, 0, "potential stalls"); 250daccb638SLuigi Rizzo 251e113edf3SGleb Smirnoff static uint32_t idlepoll_sleeping; /* idlepoll is sleeping */ 252daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD, 253daccb638SLuigi Rizzo &idlepoll_sleeping, 0, "idlepoll is sleeping"); 254daccb638SLuigi Rizzo 255e4fc250cSLuigi Rizzo #define POLL_LIST_LEN 128 256e4fc250cSLuigi Rizzo struct pollrec { 257e4fc250cSLuigi Rizzo poll_handler_t *handler; 258e4fc250cSLuigi Rizzo struct ifnet *ifp; 259e4fc250cSLuigi Rizzo }; 260e4fc250cSLuigi Rizzo 261e4fc250cSLuigi Rizzo static struct pollrec pr[POLL_LIST_LEN]; 26216901c01SGleb Smirnoff 2631cafed39SJonathan Lemon static void 264d4b5cae4SRobert Watson poll_shutdown(void *arg, int howto) 265d4b5cae4SRobert Watson { 266d4b5cae4SRobert Watson 267d4b5cae4SRobert Watson poll_shutting_down = 1; 268d4b5cae4SRobert Watson } 269d4b5cae4SRobert Watson 270d4b5cae4SRobert Watson static void 271daccb638SLuigi Rizzo init_device_poll(void) 272daccb638SLuigi Rizzo { 2731cafed39SJonathan Lemon 27416901c01SGleb Smirnoff mtx_init(&poll_mtx, "polling", NULL, MTX_DEF); 275d4b5cae4SRobert Watson EVENTHANDLER_REGISTER(shutdown_post_sync, poll_shutdown, NULL, 276d4b5cae4SRobert Watson SHUTDOWN_PRI_LAST); 277daccb638SLuigi Rizzo } 27830f8de5aSBrooks Davis SYSINIT(device_poll, SI_SUB_SOFTINTR, SI_ORDER_MIDDLE, init_device_poll, NULL); 2791cafed39SJonathan Lemon 280daccb638SLuigi Rizzo /* 281e4fc250cSLuigi Rizzo * Hook from hardclock. Tries to schedule a netisr, but keeps track 282e4fc250cSLuigi Rizzo * of lost ticks due to the previous handler taking too long. 283d26d355fSLuigi Rizzo * Normally, this should not happen, because polling handler should 284d26d355fSLuigi Rizzo * run for a short time. However, in some cases (e.g. when there are 285d26d355fSLuigi Rizzo * changes in link status etc.) the drivers take a very long time 286d26d355fSLuigi Rizzo * (even in the order of milliseconds) to reset and reconfigure the 287d26d355fSLuigi Rizzo * device, causing apparent lost polls. 288d26d355fSLuigi Rizzo * 289e4fc250cSLuigi Rizzo * The first part of the code is just for debugging purposes, and tries 290e4fc250cSLuigi Rizzo * to count how often hardclock ticks are shorter than they should, 291e4fc250cSLuigi Rizzo * meaning either stray interrupts or delayed events. 292e4fc250cSLuigi Rizzo */ 293e4fc250cSLuigi Rizzo void 294e4fc250cSLuigi Rizzo hardclock_device_poll(void) 295e4fc250cSLuigi Rizzo { 296e4fc250cSLuigi Rizzo static struct timeval prev_t, t; 297e4fc250cSLuigi Rizzo int delta; 298e4fc250cSLuigi Rizzo 299d4b5cae4SRobert Watson if (poll_handlers == 0 || poll_shutting_down) 300daccb638SLuigi Rizzo return; 301daccb638SLuigi Rizzo 302e4fc250cSLuigi Rizzo microuptime(&t); 303e4fc250cSLuigi Rizzo delta = (t.tv_usec - prev_t.tv_usec) + 304e4fc250cSLuigi Rizzo (t.tv_sec - prev_t.tv_sec)*1000000; 305e4fc250cSLuigi Rizzo if (delta * hz < 500000) 306e4fc250cSLuigi Rizzo short_ticks++; 307e4fc250cSLuigi Rizzo else 308e4fc250cSLuigi Rizzo prev_t = t; 309e4fc250cSLuigi Rizzo 310daccb638SLuigi Rizzo if (pending_polls > 100) { 311d26d355fSLuigi Rizzo /* 312d26d355fSLuigi Rizzo * Too much, assume it has stalled (not always true 313d26d355fSLuigi Rizzo * see comment above). 314d26d355fSLuigi Rizzo */ 315daccb638SLuigi Rizzo stalled++; 316daccb638SLuigi Rizzo pending_polls = 0; 317daccb638SLuigi Rizzo phase = 0; 318daccb638SLuigi Rizzo } 319daccb638SLuigi Rizzo 320daccb638SLuigi Rizzo if (phase <= 2) { 321daccb638SLuigi Rizzo if (phase != 0) 322daccb638SLuigi Rizzo suspect++; 323daccb638SLuigi Rizzo phase = 1; 324d4b5cae4SRobert Watson netisr_poll_scheduled = 1; 325d4b5cae4SRobert Watson netisr_pollmore_scheduled = 1; 326d4b5cae4SRobert Watson netisr_sched_poll(); 327daccb638SLuigi Rizzo phase = 2; 328e4fc250cSLuigi Rizzo } 329daccb638SLuigi Rizzo if (pending_polls++ > 0) 330daccb638SLuigi Rizzo lost_polls++; 331e4fc250cSLuigi Rizzo } 332e4fc250cSLuigi Rizzo 333e4fc250cSLuigi Rizzo /* 33440929967SGleb Smirnoff * ether_poll is called from the idle loop. 335e4fc250cSLuigi Rizzo */ 33656a3c6d4SRobert Watson static void 337e4fc250cSLuigi Rizzo ether_poll(int count) 338e4fc250cSLuigi Rizzo { 339ad398012SGleb Smirnoff struct epoch_tracker et; 340e4fc250cSLuigi Rizzo int i; 341e4fc250cSLuigi Rizzo 34216901c01SGleb Smirnoff mtx_lock(&poll_mtx); 343e4fc250cSLuigi Rizzo 344e4fc250cSLuigi Rizzo if (count > poll_each_burst) 345e4fc250cSLuigi Rizzo count = poll_each_burst; 34616901c01SGleb Smirnoff 347ad398012SGleb Smirnoff NET_EPOCH_ENTER(et); 34840929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 34916901c01SGleb Smirnoff pr[i].handler(pr[i].ifp, POLL_ONLY, count); 350ad398012SGleb Smirnoff NET_EPOCH_EXIT(et); 35140929967SGleb Smirnoff 35216901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 353e4fc250cSLuigi Rizzo } 354e4fc250cSLuigi Rizzo 355e4fc250cSLuigi Rizzo /* 356daccb638SLuigi Rizzo * netisr_pollmore is called after other netisr's, possibly scheduling 357e4fc250cSLuigi Rizzo * another NETISR_POLL call, or adapting the burst size for the next cycle. 358e4fc250cSLuigi Rizzo * 359e4fc250cSLuigi Rizzo * It is very bad to fetch large bursts of packets from a single card at once, 360e4fc250cSLuigi Rizzo * because the burst could take a long time to be completely processed, or 361e4fc250cSLuigi Rizzo * could saturate the intermediate queue (ipintrq or similar) leading to 362e4fc250cSLuigi Rizzo * losses or unfairness. To reduce the problem, and also to account better for 363daccb638SLuigi Rizzo * time spent in network-related processing, we split the burst in smaller 364e4fc250cSLuigi Rizzo * chunks of fixed size, giving control to the other netisr's between chunks. 365e4fc250cSLuigi Rizzo * This helps in improving the fairness, reducing livelock (because we 366e4fc250cSLuigi Rizzo * emulate more closely the "process to completion" that we have with 367e4fc250cSLuigi Rizzo * fastforwarding) and accounting for the work performed in low level 368e4fc250cSLuigi Rizzo * handling and forwarding. 369e4fc250cSLuigi Rizzo */ 370e4fc250cSLuigi Rizzo 371e4fc250cSLuigi Rizzo static struct timeval poll_start_t; 372e4fc250cSLuigi Rizzo 373e4fc250cSLuigi Rizzo void 374daccb638SLuigi Rizzo netisr_pollmore() 375e4fc250cSLuigi Rizzo { 376e4fc250cSLuigi Rizzo struct timeval t; 377e4fc250cSLuigi Rizzo int kern_load; 378e4fc250cSLuigi Rizzo 37963bf2404SGeorge V. Neville-Neil if (poll_handlers == 0) 38063bf2404SGeorge V. Neville-Neil return; 38163bf2404SGeorge V. Neville-Neil 38216901c01SGleb Smirnoff mtx_lock(&poll_mtx); 383d4b5cae4SRobert Watson if (!netisr_pollmore_scheduled) { 384d4b5cae4SRobert Watson mtx_unlock(&poll_mtx); 385d4b5cae4SRobert Watson return; 386d4b5cae4SRobert Watson } 387d4b5cae4SRobert Watson netisr_pollmore_scheduled = 0; 388daccb638SLuigi Rizzo phase = 5; 389e4fc250cSLuigi Rizzo if (residual_burst > 0) { 390d4b5cae4SRobert Watson netisr_poll_scheduled = 1; 391d4b5cae4SRobert Watson netisr_pollmore_scheduled = 1; 392d4b5cae4SRobert Watson netisr_sched_poll(); 39316901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 394e4fc250cSLuigi Rizzo /* will run immediately on return, followed by netisrs */ 395e4fc250cSLuigi Rizzo return; 396e4fc250cSLuigi Rizzo } 397e4fc250cSLuigi Rizzo /* here we can account time spent in netisr's in this tick */ 398e4fc250cSLuigi Rizzo microuptime(&t); 399e4fc250cSLuigi Rizzo kern_load = (t.tv_usec - poll_start_t.tv_usec) + 400e4fc250cSLuigi Rizzo (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ 401e4fc250cSLuigi Rizzo kern_load = (kern_load * hz) / 10000; /* 0..100 */ 402e4fc250cSLuigi Rizzo if (kern_load > (100 - user_frac)) { /* try decrease ticks */ 403e4fc250cSLuigi Rizzo if (poll_burst > 1) 404e4fc250cSLuigi Rizzo poll_burst--; 405e4fc250cSLuigi Rizzo } else { 406e4fc250cSLuigi Rizzo if (poll_burst < poll_burst_max) 407e4fc250cSLuigi Rizzo poll_burst++; 408e4fc250cSLuigi Rizzo } 409e4fc250cSLuigi Rizzo 410daccb638SLuigi Rizzo pending_polls--; 411daccb638SLuigi Rizzo if (pending_polls == 0) /* we are done */ 412daccb638SLuigi Rizzo phase = 0; 413daccb638SLuigi Rizzo else { 414e4fc250cSLuigi Rizzo /* 415e4fc250cSLuigi Rizzo * Last cycle was long and caused us to miss one or more 416daccb638SLuigi Rizzo * hardclock ticks. Restart processing again, but slightly 417e4fc250cSLuigi Rizzo * reduce the burst size to prevent that this happens again. 418e4fc250cSLuigi Rizzo */ 419e4fc250cSLuigi Rizzo poll_burst -= (poll_burst / 8); 420e4fc250cSLuigi Rizzo if (poll_burst < 1) 421e4fc250cSLuigi Rizzo poll_burst = 1; 422d4b5cae4SRobert Watson netisr_poll_scheduled = 1; 423d4b5cae4SRobert Watson netisr_pollmore_scheduled = 1; 424d4b5cae4SRobert Watson netisr_sched_poll(); 425daccb638SLuigi Rizzo phase = 6; 426daccb638SLuigi Rizzo } 42716901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 428e4fc250cSLuigi Rizzo } 429e4fc250cSLuigi Rizzo 430e4fc250cSLuigi Rizzo /* 431d4b5cae4SRobert Watson * netisr_poll is typically scheduled once per tick. 432e4fc250cSLuigi Rizzo */ 433d4b5cae4SRobert Watson void 434daccb638SLuigi Rizzo netisr_poll(void) 435e4fc250cSLuigi Rizzo { 436e4fc250cSLuigi Rizzo int i, cycles; 437e4fc250cSLuigi Rizzo enum poll_cmd arg = POLL_ONLY; 438e4fc250cSLuigi Rizzo 439ad398012SGleb Smirnoff NET_EPOCH_ASSERT(); 440ad398012SGleb Smirnoff 44163bf2404SGeorge V. Neville-Neil if (poll_handlers == 0) 44263bf2404SGeorge V. Neville-Neil return; 44363bf2404SGeorge V. Neville-Neil 44416901c01SGleb Smirnoff mtx_lock(&poll_mtx); 445d4b5cae4SRobert Watson if (!netisr_poll_scheduled) { 446d4b5cae4SRobert Watson mtx_unlock(&poll_mtx); 447d4b5cae4SRobert Watson return; 448d4b5cae4SRobert Watson } 449d4b5cae4SRobert Watson netisr_poll_scheduled = 0; 450daccb638SLuigi Rizzo phase = 3; 451e4fc250cSLuigi Rizzo if (residual_burst == 0) { /* first call in this tick */ 452e4fc250cSLuigi Rizzo microuptime(&poll_start_t); 453e113edf3SGleb Smirnoff if (++reg_frac_count == reg_frac) { 454e4fc250cSLuigi Rizzo arg = POLL_AND_CHECK_STATUS; 455e113edf3SGleb Smirnoff reg_frac_count = 0; 456e4fc250cSLuigi Rizzo } 457e4fc250cSLuigi Rizzo 458e4fc250cSLuigi Rizzo residual_burst = poll_burst; 459e4fc250cSLuigi Rizzo } 460e4fc250cSLuigi Rizzo cycles = (residual_burst < poll_each_burst) ? 461e4fc250cSLuigi Rizzo residual_burst : poll_each_burst; 462e4fc250cSLuigi Rizzo residual_burst -= cycles; 463e4fc250cSLuigi Rizzo 46440929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 465e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, arg, cycles); 46616901c01SGleb Smirnoff 467daccb638SLuigi Rizzo phase = 4; 46816901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 469e4fc250cSLuigi Rizzo } 470e4fc250cSLuigi Rizzo 471e4fc250cSLuigi Rizzo /* 47240929967SGleb Smirnoff * Try to register routine for polling. Returns 0 if successful 47340929967SGleb Smirnoff * (and polling should be enabled), error code otherwise. 474e4fc250cSLuigi Rizzo * A device is not supposed to register itself multiple times. 475e4fc250cSLuigi Rizzo * 47640929967SGleb Smirnoff * This is called from within the *_ioctl() functions. 477e4fc250cSLuigi Rizzo */ 478e4fc250cSLuigi Rizzo int 479bd071d4dSGleb Smirnoff ether_poll_register(poll_handler_t *h, if_t ifp) 480e4fc250cSLuigi Rizzo { 48116901c01SGleb Smirnoff int i; 48216901c01SGleb Smirnoff 48340929967SGleb Smirnoff KASSERT(h != NULL, ("%s: handler is NULL", __func__)); 48440929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 485e4fc250cSLuigi Rizzo 48616901c01SGleb Smirnoff mtx_lock(&poll_mtx); 487e4fc250cSLuigi Rizzo if (poll_handlers >= POLL_LIST_LEN) { 488e4fc250cSLuigi Rizzo /* 489e4fc250cSLuigi Rizzo * List full, cannot register more entries. 490e4fc250cSLuigi Rizzo * This should never happen; if it does, it is probably a 491e4fc250cSLuigi Rizzo * broken driver trying to register multiple times. Checking 492e4fc250cSLuigi Rizzo * this at runtime is expensive, and won't solve the problem 493e4fc250cSLuigi Rizzo * anyways, so just report a few times and then give up. 494e4fc250cSLuigi Rizzo */ 495e4fc250cSLuigi Rizzo static int verbose = 10 ; 496e4fc250cSLuigi Rizzo if (verbose >0) { 49716901c01SGleb Smirnoff log(LOG_ERR, "poll handlers list full, " 498e4fc250cSLuigi Rizzo "maybe a broken driver ?\n"); 499e4fc250cSLuigi Rizzo verbose--; 500e4fc250cSLuigi Rizzo } 50116901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 50240929967SGleb Smirnoff return (ENOMEM); /* no polling for you */ 503e4fc250cSLuigi Rizzo } 504e4fc250cSLuigi Rizzo 50516901c01SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 50616901c01SGleb Smirnoff if (pr[i].ifp == ifp && pr[i].handler != NULL) { 50716901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 50816901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_register: %s: handler" 50916901c01SGleb Smirnoff " already registered\n", ifp->if_xname); 51040929967SGleb Smirnoff return (EEXIST); 51116901c01SGleb Smirnoff } 51216901c01SGleb Smirnoff 513e4fc250cSLuigi Rizzo pr[poll_handlers].handler = h; 514e4fc250cSLuigi Rizzo pr[poll_handlers].ifp = ifp; 515e4fc250cSLuigi Rizzo poll_handlers++; 51616901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 517d105c784SLuigi Rizzo if (idlepoll_sleeping) 518d105c784SLuigi Rizzo wakeup(&idlepoll_sleeping); 51940929967SGleb Smirnoff return (0); 520e4fc250cSLuigi Rizzo } 521e4fc250cSLuigi Rizzo 522e4fc250cSLuigi Rizzo /* 52340929967SGleb Smirnoff * Remove interface from the polling list. Called from *_ioctl(), too. 524e4fc250cSLuigi Rizzo */ 525e4fc250cSLuigi Rizzo int 526bd071d4dSGleb Smirnoff ether_poll_deregister(if_t ifp) 527e4fc250cSLuigi Rizzo { 528e4fc250cSLuigi Rizzo int i; 529e4fc250cSLuigi Rizzo 53040929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 53116901c01SGleb Smirnoff 53216901c01SGleb Smirnoff mtx_lock(&poll_mtx); 53340929967SGleb Smirnoff 534e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 535e4fc250cSLuigi Rizzo if (pr[i].ifp == ifp) /* found it */ 536e4fc250cSLuigi Rizzo break; 537e4fc250cSLuigi Rizzo if (i == poll_handlers) { 53816901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n", 53916901c01SGleb Smirnoff ifp->if_xname); 54040929967SGleb Smirnoff mtx_unlock(&poll_mtx); 54140929967SGleb Smirnoff return (ENOENT); 542e4fc250cSLuigi Rizzo } 543e4fc250cSLuigi Rizzo poll_handlers--; 544e4fc250cSLuigi Rizzo if (i < poll_handlers) { /* Last entry replaces this one. */ 545e4fc250cSLuigi Rizzo pr[i].handler = pr[poll_handlers].handler; 546e4fc250cSLuigi Rizzo pr[i].ifp = pr[poll_handlers].ifp; 547e4fc250cSLuigi Rizzo } 54816901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 54940929967SGleb Smirnoff return (0); 55040929967SGleb Smirnoff } 55140929967SGleb Smirnoff 552d105c784SLuigi Rizzo static void 553d105c784SLuigi Rizzo poll_idle(void) 554d105c784SLuigi Rizzo { 555d105c784SLuigi Rizzo struct thread *td = curthread; 556d105c784SLuigi Rizzo struct rtprio rtp; 557d105c784SLuigi Rizzo 558d105c784SLuigi Rizzo rtp.prio = RTP_PRIO_MAX; /* lowest priority */ 559d105c784SLuigi Rizzo rtp.type = RTP_PRIO_IDLE; 560982d11f8SJeff Roberson PROC_SLOCK(td->td_proc); 5618460a577SJohn Birrell rtp_to_pri(&rtp, td); 562982d11f8SJeff Roberson PROC_SUNLOCK(td->td_proc); 563d105c784SLuigi Rizzo 564d105c784SLuigi Rizzo for (;;) { 565daccb638SLuigi Rizzo if (poll_in_idle_loop && poll_handlers > 0) { 566d105c784SLuigi Rizzo idlepoll_sleeping = 0; 567d105c784SLuigi Rizzo ether_poll(poll_each_burst); 568982d11f8SJeff Roberson thread_lock(td); 569686bcb5cSJeff Roberson mi_switch(SW_VOL); 570d105c784SLuigi Rizzo } else { 571d105c784SLuigi Rizzo idlepoll_sleeping = 1; 5720f180a7cSJohn Baldwin tsleep(&idlepoll_sleeping, 0, "pollid", hz * 3); 573d105c784SLuigi Rizzo } 574d105c784SLuigi Rizzo } 575d105c784SLuigi Rizzo } 576d105c784SLuigi Rizzo 577d105c784SLuigi Rizzo static struct proc *idlepoll; 578d105c784SLuigi Rizzo static struct kproc_desc idlepoll_kp = { 579d105c784SLuigi Rizzo "idlepoll", 580d105c784SLuigi Rizzo poll_idle, 581d105c784SLuigi Rizzo &idlepoll 582d105c784SLuigi Rizzo }; 583237fdd78SRobert Watson SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, 584237fdd78SRobert Watson &idlepoll_kp); 585