1e4fc250cSLuigi Rizzo /*- 2*8a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*8a36da99SPedro 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> 40d4b5cae4SRobert Watson #include <sys/eventhandler.h> 419ea9ef7eSBjoern A. Zeeb #include <sys/resourcevar.h> 42e4fc250cSLuigi Rizzo #include <sys/socket.h> /* needed by net/if.h */ 4340929967SGleb Smirnoff #include <sys/sockio.h> 44e4fc250cSLuigi Rizzo #include <sys/sysctl.h> 4516901c01SGleb Smirnoff #include <sys/syslog.h> 46e4fc250cSLuigi Rizzo 4776039bc8SGleb Smirnoff #include <net/if.h> 4876039bc8SGleb Smirnoff #include <net/if_var.h> 49e4fc250cSLuigi Rizzo #include <net/netisr.h> /* for NETISR_POLL */ 504b79449eSBjoern A. Zeeb #include <net/vnet.h> 51e4fc250cSLuigi Rizzo 52daccb638SLuigi Rizzo void hardclock_device_poll(void); /* hook from hardclock */ 53e4fc250cSLuigi Rizzo 54e113edf3SGleb Smirnoff static struct mtx poll_mtx; 55e113edf3SGleb Smirnoff 56e4fc250cSLuigi Rizzo /* 57e4fc250cSLuigi Rizzo * Polling support for [network] device drivers. 58e4fc250cSLuigi Rizzo * 5940929967SGleb Smirnoff * Drivers which support this feature can 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 * 73e4fc250cSLuigi Rizzo * The count limit specifies how much work the handler can do during the 74e4fc250cSLuigi Rizzo * call -- typically this is the number of packets to be received, or 75e4fc250cSLuigi Rizzo * transmitted, etc. (drivers are free to interpret this number, as long 76e4fc250cSLuigi Rizzo * as the max time spent in the function grows roughly linearly with the 77e4fc250cSLuigi Rizzo * count). 78e4fc250cSLuigi Rizzo * 7940929967SGleb Smirnoff * Polling is enabled and disabled via setting IFCAP_POLLING flag on 8040929967SGleb Smirnoff * the interface. The driver ioctl handler should register interface 8140929967SGleb Smirnoff * with polling and disable interrupts, if registration was successful. 82e4fc250cSLuigi Rizzo * 83e4fc250cSLuigi Rizzo * A second variable controls the sharing of CPU between polling/kernel 84e4fc250cSLuigi Rizzo * network processing, and other activities (typically userlevel tasks): 85e4fc250cSLuigi Rizzo * kern.polling.user_frac (between 0 and 100, default 50) sets the share 86e4fc250cSLuigi Rizzo * of CPU allocated to user tasks. CPU is allocated proportionally to the 87e4fc250cSLuigi Rizzo * shares, by dynamically adjusting the "count" (poll_burst). 88e4fc250cSLuigi Rizzo * 89e4fc250cSLuigi Rizzo * Other parameters can should be left to their default values. 90e4fc250cSLuigi Rizzo * The following constraints hold 91e4fc250cSLuigi Rizzo * 92e4fc250cSLuigi Rizzo * 1 <= poll_each_burst <= poll_burst <= poll_burst_max 93e4fc250cSLuigi Rizzo * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX 94e4fc250cSLuigi Rizzo */ 95e4fc250cSLuigi Rizzo 96e4fc250cSLuigi Rizzo #define MIN_POLL_BURST_MAX 10 974b62214fSLuigi Rizzo #define MAX_POLL_BURST_MAX 20000 98e4fc250cSLuigi Rizzo 99e113edf3SGleb Smirnoff static uint32_t poll_burst = 5; 100e113edf3SGleb Smirnoff static uint32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ 101e113edf3SGleb Smirnoff static uint32_t poll_each_burst = 5; 102e113edf3SGleb Smirnoff 1036472ac3dSEd Schouten static SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0, 104e4fc250cSLuigi Rizzo "Device polling parameters"); 105e4fc250cSLuigi Rizzo 106e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RD, 107e4fc250cSLuigi Rizzo &poll_burst, 0, "Current polling burst size"); 108e4fc250cSLuigi Rizzo 109d4b5cae4SRobert Watson static int netisr_poll_scheduled; 110d4b5cae4SRobert Watson static int netisr_pollmore_scheduled; 111d4b5cae4SRobert Watson static int poll_shutting_down; 112d4b5cae4SRobert Watson 113e113edf3SGleb Smirnoff static int poll_burst_max_sysctl(SYSCTL_HANDLER_ARGS) 114e113edf3SGleb Smirnoff { 115e113edf3SGleb Smirnoff uint32_t val = poll_burst_max; 116e113edf3SGleb Smirnoff int error; 117e4fc250cSLuigi Rizzo 118041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 119e113edf3SGleb Smirnoff if (error || !req->newptr ) 120e113edf3SGleb Smirnoff return (error); 121e113edf3SGleb Smirnoff if (val < MIN_POLL_BURST_MAX || val > MAX_POLL_BURST_MAX) 122e113edf3SGleb Smirnoff return (EINVAL); 123e4fc250cSLuigi Rizzo 124e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 125e113edf3SGleb Smirnoff poll_burst_max = val; 126e113edf3SGleb Smirnoff if (poll_burst > poll_burst_max) 127e113edf3SGleb Smirnoff poll_burst = poll_burst_max; 128e113edf3SGleb Smirnoff if (poll_each_burst > poll_burst_max) 129e113edf3SGleb Smirnoff poll_each_burst = MIN_POLL_BURST_MAX; 130e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 131e113edf3SGleb Smirnoff 132e113edf3SGleb Smirnoff return (0); 133e113edf3SGleb Smirnoff } 134e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, burst_max, CTLTYPE_UINT | CTLFLAG_RW, 135e113edf3SGleb Smirnoff 0, sizeof(uint32_t), poll_burst_max_sysctl, "I", "Max Polling burst size"); 136e113edf3SGleb Smirnoff 137e113edf3SGleb Smirnoff static int poll_each_burst_sysctl(SYSCTL_HANDLER_ARGS) 138e113edf3SGleb Smirnoff { 139e113edf3SGleb Smirnoff uint32_t val = poll_each_burst; 140e113edf3SGleb Smirnoff int error; 141e113edf3SGleb Smirnoff 142041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 143e113edf3SGleb Smirnoff if (error || !req->newptr ) 144e113edf3SGleb Smirnoff return (error); 145e113edf3SGleb Smirnoff if (val < 1) 146e113edf3SGleb Smirnoff return (EINVAL); 147e113edf3SGleb Smirnoff 148e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 149e113edf3SGleb Smirnoff if (val > poll_burst_max) { 150e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 151e113edf3SGleb Smirnoff return (EINVAL); 152e113edf3SGleb Smirnoff } 153e113edf3SGleb Smirnoff poll_each_burst = val; 154e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 155e113edf3SGleb Smirnoff 156e113edf3SGleb Smirnoff return (0); 157e113edf3SGleb Smirnoff } 158e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, each_burst, CTLTYPE_UINT | CTLFLAG_RW, 159e113edf3SGleb Smirnoff 0, sizeof(uint32_t), poll_each_burst_sysctl, "I", 160e113edf3SGleb Smirnoff "Max size of each burst"); 161e113edf3SGleb Smirnoff 162e113edf3SGleb Smirnoff static uint32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */ 163daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW, 164daccb638SLuigi Rizzo &poll_in_idle_loop, 0, "Enable device polling in idle loop"); 165daccb638SLuigi Rizzo 166e113edf3SGleb Smirnoff static uint32_t user_frac = 50; 167e113edf3SGleb Smirnoff static int user_frac_sysctl(SYSCTL_HANDLER_ARGS) 168e113edf3SGleb Smirnoff { 169e113edf3SGleb Smirnoff uint32_t val = user_frac; 170e113edf3SGleb Smirnoff int error; 171e4fc250cSLuigi Rizzo 172041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 173e113edf3SGleb Smirnoff if (error || !req->newptr ) 174e113edf3SGleb Smirnoff return (error); 1753de1bd95SKevin Lo if (val > 99) 176e113edf3SGleb Smirnoff return (EINVAL); 177e4fc250cSLuigi Rizzo 178e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 179e113edf3SGleb Smirnoff user_frac = val; 180e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 181e113edf3SGleb Smirnoff 182e113edf3SGleb Smirnoff return (0); 183e113edf3SGleb Smirnoff } 184e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, user_frac, CTLTYPE_UINT | CTLFLAG_RW, 185e113edf3SGleb Smirnoff 0, sizeof(uint32_t), user_frac_sysctl, "I", 186e113edf3SGleb Smirnoff "Desired user fraction of cpu time"); 187e113edf3SGleb Smirnoff 188e113edf3SGleb Smirnoff static uint32_t reg_frac_count = 0; 189e113edf3SGleb Smirnoff static uint32_t reg_frac = 20 ; 190e113edf3SGleb Smirnoff static int reg_frac_sysctl(SYSCTL_HANDLER_ARGS) 191e113edf3SGleb Smirnoff { 192e113edf3SGleb Smirnoff uint32_t val = reg_frac; 193e113edf3SGleb Smirnoff int error; 194e113edf3SGleb Smirnoff 195041b706bSDavid Malone error = sysctl_handle_int(oidp, &val, 0, req); 196e113edf3SGleb Smirnoff if (error || !req->newptr ) 197e113edf3SGleb Smirnoff return (error); 198e113edf3SGleb Smirnoff if (val < 1 || val > hz) 199e113edf3SGleb Smirnoff return (EINVAL); 200e113edf3SGleb Smirnoff 201e113edf3SGleb Smirnoff mtx_lock(&poll_mtx); 202e113edf3SGleb Smirnoff reg_frac = val; 203e113edf3SGleb Smirnoff if (reg_frac_count >= reg_frac) 204e113edf3SGleb Smirnoff reg_frac_count = 0; 205e113edf3SGleb Smirnoff mtx_unlock(&poll_mtx); 206e113edf3SGleb Smirnoff 207e113edf3SGleb Smirnoff return (0); 208e113edf3SGleb Smirnoff } 209e113edf3SGleb Smirnoff SYSCTL_PROC(_kern_polling, OID_AUTO, reg_frac, CTLTYPE_UINT | CTLFLAG_RW, 210e113edf3SGleb Smirnoff 0, sizeof(uint32_t), reg_frac_sysctl, "I", 211e113edf3SGleb Smirnoff "Every this many cycles check registers"); 212e113edf3SGleb Smirnoff 213e113edf3SGleb Smirnoff static uint32_t short_ticks; 214e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RD, 215e4fc250cSLuigi Rizzo &short_ticks, 0, "Hardclock ticks shorter than they should be"); 216e4fc250cSLuigi Rizzo 217e113edf3SGleb Smirnoff static uint32_t lost_polls; 218e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RD, 219e4fc250cSLuigi Rizzo &lost_polls, 0, "How many times we would have lost a poll tick"); 220e4fc250cSLuigi Rizzo 221e113edf3SGleb Smirnoff static uint32_t pending_polls; 222e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RD, 223daccb638SLuigi Rizzo &pending_polls, 0, "Do we need to poll again"); 224daccb638SLuigi Rizzo 225daccb638SLuigi Rizzo static int residual_burst = 0; 226e113edf3SGleb Smirnoff SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RD, 227daccb638SLuigi Rizzo &residual_burst, 0, "# of residual cycles in burst"); 228daccb638SLuigi Rizzo 229e113edf3SGleb Smirnoff static uint32_t poll_handlers; /* next free entry in pr[]. */ 230daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, 231e4fc250cSLuigi Rizzo &poll_handlers, 0, "Number of registered poll handlers"); 232e4fc250cSLuigi Rizzo 233e113edf3SGleb Smirnoff static uint32_t phase; 234e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RD, 235daccb638SLuigi Rizzo &phase, 0, "Polling phase"); 236e4fc250cSLuigi Rizzo 237e113edf3SGleb Smirnoff static uint32_t suspect; 238e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RD, 239daccb638SLuigi Rizzo &suspect, 0, "suspect event"); 240daccb638SLuigi Rizzo 241e113edf3SGleb Smirnoff static uint32_t stalled; 242e113edf3SGleb Smirnoff SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RD, 243daccb638SLuigi Rizzo &stalled, 0, "potential stalls"); 244daccb638SLuigi Rizzo 245e113edf3SGleb Smirnoff static uint32_t idlepoll_sleeping; /* idlepoll is sleeping */ 246daccb638SLuigi Rizzo SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD, 247daccb638SLuigi Rizzo &idlepoll_sleeping, 0, "idlepoll is sleeping"); 248daccb638SLuigi Rizzo 249e4fc250cSLuigi Rizzo 250e4fc250cSLuigi Rizzo #define POLL_LIST_LEN 128 251e4fc250cSLuigi Rizzo struct pollrec { 252e4fc250cSLuigi Rizzo poll_handler_t *handler; 253e4fc250cSLuigi Rizzo struct ifnet *ifp; 254e4fc250cSLuigi Rizzo }; 255e4fc250cSLuigi Rizzo 256e4fc250cSLuigi Rizzo static struct pollrec pr[POLL_LIST_LEN]; 25716901c01SGleb Smirnoff 2581cafed39SJonathan Lemon static void 259d4b5cae4SRobert Watson poll_shutdown(void *arg, int howto) 260d4b5cae4SRobert Watson { 261d4b5cae4SRobert Watson 262d4b5cae4SRobert Watson poll_shutting_down = 1; 263d4b5cae4SRobert Watson } 264d4b5cae4SRobert Watson 265d4b5cae4SRobert Watson static void 266daccb638SLuigi Rizzo init_device_poll(void) 267daccb638SLuigi Rizzo { 2681cafed39SJonathan Lemon 26916901c01SGleb Smirnoff mtx_init(&poll_mtx, "polling", NULL, MTX_DEF); 270d4b5cae4SRobert Watson EVENTHANDLER_REGISTER(shutdown_post_sync, poll_shutdown, NULL, 271d4b5cae4SRobert Watson SHUTDOWN_PRI_LAST); 272daccb638SLuigi Rizzo } 27330f8de5aSBrooks Davis SYSINIT(device_poll, SI_SUB_SOFTINTR, SI_ORDER_MIDDLE, init_device_poll, NULL); 2741cafed39SJonathan Lemon 275daccb638SLuigi Rizzo 276daccb638SLuigi Rizzo /* 277e4fc250cSLuigi Rizzo * Hook from hardclock. Tries to schedule a netisr, but keeps track 278e4fc250cSLuigi Rizzo * of lost ticks due to the previous handler taking too long. 279d26d355fSLuigi Rizzo * Normally, this should not happen, because polling handler should 280d26d355fSLuigi Rizzo * run for a short time. However, in some cases (e.g. when there are 281d26d355fSLuigi Rizzo * changes in link status etc.) the drivers take a very long time 282d26d355fSLuigi Rizzo * (even in the order of milliseconds) to reset and reconfigure the 283d26d355fSLuigi Rizzo * device, causing apparent lost polls. 284d26d355fSLuigi Rizzo * 285e4fc250cSLuigi Rizzo * The first part of the code is just for debugging purposes, and tries 286e4fc250cSLuigi Rizzo * to count how often hardclock ticks are shorter than they should, 287e4fc250cSLuigi Rizzo * meaning either stray interrupts or delayed events. 288e4fc250cSLuigi Rizzo */ 289e4fc250cSLuigi Rizzo void 290e4fc250cSLuigi Rizzo hardclock_device_poll(void) 291e4fc250cSLuigi Rizzo { 292e4fc250cSLuigi Rizzo static struct timeval prev_t, t; 293e4fc250cSLuigi Rizzo int delta; 294e4fc250cSLuigi Rizzo 295d4b5cae4SRobert Watson if (poll_handlers == 0 || poll_shutting_down) 296daccb638SLuigi Rizzo return; 297daccb638SLuigi Rizzo 298e4fc250cSLuigi Rizzo microuptime(&t); 299e4fc250cSLuigi Rizzo delta = (t.tv_usec - prev_t.tv_usec) + 300e4fc250cSLuigi Rizzo (t.tv_sec - prev_t.tv_sec)*1000000; 301e4fc250cSLuigi Rizzo if (delta * hz < 500000) 302e4fc250cSLuigi Rizzo short_ticks++; 303e4fc250cSLuigi Rizzo else 304e4fc250cSLuigi Rizzo prev_t = t; 305e4fc250cSLuigi Rizzo 306daccb638SLuigi Rizzo if (pending_polls > 100) { 307d26d355fSLuigi Rizzo /* 308d26d355fSLuigi Rizzo * Too much, assume it has stalled (not always true 309d26d355fSLuigi Rizzo * see comment above). 310d26d355fSLuigi Rizzo */ 311daccb638SLuigi Rizzo stalled++; 312daccb638SLuigi Rizzo pending_polls = 0; 313daccb638SLuigi Rizzo phase = 0; 314daccb638SLuigi Rizzo } 315daccb638SLuigi Rizzo 316daccb638SLuigi Rizzo if (phase <= 2) { 317daccb638SLuigi Rizzo if (phase != 0) 318daccb638SLuigi Rizzo suspect++; 319daccb638SLuigi Rizzo phase = 1; 320d4b5cae4SRobert Watson netisr_poll_scheduled = 1; 321d4b5cae4SRobert Watson netisr_pollmore_scheduled = 1; 322d4b5cae4SRobert Watson netisr_sched_poll(); 323daccb638SLuigi Rizzo phase = 2; 324e4fc250cSLuigi Rizzo } 325daccb638SLuigi Rizzo if (pending_polls++ > 0) 326daccb638SLuigi Rizzo lost_polls++; 327e4fc250cSLuigi Rizzo } 328e4fc250cSLuigi Rizzo 329e4fc250cSLuigi Rizzo /* 33040929967SGleb Smirnoff * ether_poll is called from the idle loop. 331e4fc250cSLuigi Rizzo */ 33256a3c6d4SRobert Watson static void 333e4fc250cSLuigi Rizzo ether_poll(int count) 334e4fc250cSLuigi Rizzo { 335e4fc250cSLuigi Rizzo int i; 336e4fc250cSLuigi Rizzo 33716901c01SGleb Smirnoff mtx_lock(&poll_mtx); 338e4fc250cSLuigi Rizzo 339e4fc250cSLuigi Rizzo if (count > poll_each_burst) 340e4fc250cSLuigi Rizzo count = poll_each_burst; 34116901c01SGleb Smirnoff 34240929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 34316901c01SGleb Smirnoff pr[i].handler(pr[i].ifp, POLL_ONLY, count); 34440929967SGleb Smirnoff 34516901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 346e4fc250cSLuigi Rizzo } 347e4fc250cSLuigi Rizzo 348e4fc250cSLuigi Rizzo /* 349daccb638SLuigi Rizzo * netisr_pollmore is called after other netisr's, possibly scheduling 350e4fc250cSLuigi Rizzo * another NETISR_POLL call, or adapting the burst size for the next cycle. 351e4fc250cSLuigi Rizzo * 352e4fc250cSLuigi Rizzo * It is very bad to fetch large bursts of packets from a single card at once, 353e4fc250cSLuigi Rizzo * because the burst could take a long time to be completely processed, or 354e4fc250cSLuigi Rizzo * could saturate the intermediate queue (ipintrq or similar) leading to 355e4fc250cSLuigi Rizzo * losses or unfairness. To reduce the problem, and also to account better for 356daccb638SLuigi Rizzo * time spent in network-related processing, we split the burst in smaller 357e4fc250cSLuigi Rizzo * chunks of fixed size, giving control to the other netisr's between chunks. 358e4fc250cSLuigi Rizzo * This helps in improving the fairness, reducing livelock (because we 359e4fc250cSLuigi Rizzo * emulate more closely the "process to completion" that we have with 360e4fc250cSLuigi Rizzo * fastforwarding) and accounting for the work performed in low level 361e4fc250cSLuigi Rizzo * handling and forwarding. 362e4fc250cSLuigi Rizzo */ 363e4fc250cSLuigi Rizzo 364e4fc250cSLuigi Rizzo static struct timeval poll_start_t; 365e4fc250cSLuigi Rizzo 366e4fc250cSLuigi Rizzo void 367daccb638SLuigi Rizzo netisr_pollmore() 368e4fc250cSLuigi Rizzo { 369e4fc250cSLuigi Rizzo struct timeval t; 370e4fc250cSLuigi Rizzo int kern_load; 371e4fc250cSLuigi Rizzo 37263bf2404SGeorge V. Neville-Neil if (poll_handlers == 0) 37363bf2404SGeorge V. Neville-Neil return; 37463bf2404SGeorge V. Neville-Neil 37516901c01SGleb Smirnoff mtx_lock(&poll_mtx); 376d4b5cae4SRobert Watson if (!netisr_pollmore_scheduled) { 377d4b5cae4SRobert Watson mtx_unlock(&poll_mtx); 378d4b5cae4SRobert Watson return; 379d4b5cae4SRobert Watson } 380d4b5cae4SRobert Watson netisr_pollmore_scheduled = 0; 381daccb638SLuigi Rizzo phase = 5; 382e4fc250cSLuigi Rizzo if (residual_burst > 0) { 383d4b5cae4SRobert Watson netisr_poll_scheduled = 1; 384d4b5cae4SRobert Watson netisr_pollmore_scheduled = 1; 385d4b5cae4SRobert Watson netisr_sched_poll(); 38616901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 387e4fc250cSLuigi Rizzo /* will run immediately on return, followed by netisrs */ 388e4fc250cSLuigi Rizzo return; 389e4fc250cSLuigi Rizzo } 390e4fc250cSLuigi Rizzo /* here we can account time spent in netisr's in this tick */ 391e4fc250cSLuigi Rizzo microuptime(&t); 392e4fc250cSLuigi Rizzo kern_load = (t.tv_usec - poll_start_t.tv_usec) + 393e4fc250cSLuigi Rizzo (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ 394e4fc250cSLuigi Rizzo kern_load = (kern_load * hz) / 10000; /* 0..100 */ 395e4fc250cSLuigi Rizzo if (kern_load > (100 - user_frac)) { /* try decrease ticks */ 396e4fc250cSLuigi Rizzo if (poll_burst > 1) 397e4fc250cSLuigi Rizzo poll_burst--; 398e4fc250cSLuigi Rizzo } else { 399e4fc250cSLuigi Rizzo if (poll_burst < poll_burst_max) 400e4fc250cSLuigi Rizzo poll_burst++; 401e4fc250cSLuigi Rizzo } 402e4fc250cSLuigi Rizzo 403daccb638SLuigi Rizzo pending_polls--; 404daccb638SLuigi Rizzo if (pending_polls == 0) /* we are done */ 405daccb638SLuigi Rizzo phase = 0; 406daccb638SLuigi Rizzo else { 407e4fc250cSLuigi Rizzo /* 408e4fc250cSLuigi Rizzo * Last cycle was long and caused us to miss one or more 409daccb638SLuigi Rizzo * hardclock ticks. Restart processing again, but slightly 410e4fc250cSLuigi Rizzo * reduce the burst size to prevent that this happens again. 411e4fc250cSLuigi Rizzo */ 412e4fc250cSLuigi Rizzo poll_burst -= (poll_burst / 8); 413e4fc250cSLuigi Rizzo if (poll_burst < 1) 414e4fc250cSLuigi Rizzo poll_burst = 1; 415d4b5cae4SRobert Watson netisr_poll_scheduled = 1; 416d4b5cae4SRobert Watson netisr_pollmore_scheduled = 1; 417d4b5cae4SRobert Watson netisr_sched_poll(); 418daccb638SLuigi Rizzo phase = 6; 419daccb638SLuigi Rizzo } 42016901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 421e4fc250cSLuigi Rizzo } 422e4fc250cSLuigi Rizzo 423e4fc250cSLuigi Rizzo /* 424d4b5cae4SRobert Watson * netisr_poll is typically scheduled once per tick. 425e4fc250cSLuigi Rizzo */ 426d4b5cae4SRobert Watson void 427daccb638SLuigi Rizzo netisr_poll(void) 428e4fc250cSLuigi Rizzo { 429e4fc250cSLuigi Rizzo int i, cycles; 430e4fc250cSLuigi Rizzo enum poll_cmd arg = POLL_ONLY; 431e4fc250cSLuigi Rizzo 43263bf2404SGeorge V. Neville-Neil if (poll_handlers == 0) 43363bf2404SGeorge V. Neville-Neil return; 43463bf2404SGeorge V. Neville-Neil 43516901c01SGleb Smirnoff mtx_lock(&poll_mtx); 436d4b5cae4SRobert Watson if (!netisr_poll_scheduled) { 437d4b5cae4SRobert Watson mtx_unlock(&poll_mtx); 438d4b5cae4SRobert Watson return; 439d4b5cae4SRobert Watson } 440d4b5cae4SRobert Watson netisr_poll_scheduled = 0; 441daccb638SLuigi Rizzo phase = 3; 442e4fc250cSLuigi Rizzo if (residual_burst == 0) { /* first call in this tick */ 443e4fc250cSLuigi Rizzo microuptime(&poll_start_t); 444e113edf3SGleb Smirnoff if (++reg_frac_count == reg_frac) { 445e4fc250cSLuigi Rizzo arg = POLL_AND_CHECK_STATUS; 446e113edf3SGleb Smirnoff reg_frac_count = 0; 447e4fc250cSLuigi Rizzo } 448e4fc250cSLuigi Rizzo 449e4fc250cSLuigi Rizzo residual_burst = poll_burst; 450e4fc250cSLuigi Rizzo } 451e4fc250cSLuigi Rizzo cycles = (residual_burst < poll_each_burst) ? 452e4fc250cSLuigi Rizzo residual_burst : poll_each_burst; 453e4fc250cSLuigi Rizzo residual_burst -= cycles; 454e4fc250cSLuigi Rizzo 45540929967SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 456e4fc250cSLuigi Rizzo pr[i].handler(pr[i].ifp, arg, cycles); 45716901c01SGleb Smirnoff 458daccb638SLuigi Rizzo phase = 4; 45916901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 460e4fc250cSLuigi Rizzo } 461e4fc250cSLuigi Rizzo 462e4fc250cSLuigi Rizzo /* 46340929967SGleb Smirnoff * Try to register routine for polling. Returns 0 if successful 46440929967SGleb Smirnoff * (and polling should be enabled), error code otherwise. 465e4fc250cSLuigi Rizzo * A device is not supposed to register itself multiple times. 466e4fc250cSLuigi Rizzo * 46740929967SGleb Smirnoff * This is called from within the *_ioctl() functions. 468e4fc250cSLuigi Rizzo */ 469e4fc250cSLuigi Rizzo int 470bd071d4dSGleb Smirnoff ether_poll_register(poll_handler_t *h, if_t ifp) 471e4fc250cSLuigi Rizzo { 47216901c01SGleb Smirnoff int i; 47316901c01SGleb Smirnoff 47440929967SGleb Smirnoff KASSERT(h != NULL, ("%s: handler is NULL", __func__)); 47540929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 476e4fc250cSLuigi Rizzo 47716901c01SGleb Smirnoff mtx_lock(&poll_mtx); 478e4fc250cSLuigi Rizzo if (poll_handlers >= POLL_LIST_LEN) { 479e4fc250cSLuigi Rizzo /* 480e4fc250cSLuigi Rizzo * List full, cannot register more entries. 481e4fc250cSLuigi Rizzo * This should never happen; if it does, it is probably a 482e4fc250cSLuigi Rizzo * broken driver trying to register multiple times. Checking 483e4fc250cSLuigi Rizzo * this at runtime is expensive, and won't solve the problem 484e4fc250cSLuigi Rizzo * anyways, so just report a few times and then give up. 485e4fc250cSLuigi Rizzo */ 486e4fc250cSLuigi Rizzo static int verbose = 10 ; 487e4fc250cSLuigi Rizzo if (verbose >0) { 48816901c01SGleb Smirnoff log(LOG_ERR, "poll handlers list full, " 489e4fc250cSLuigi Rizzo "maybe a broken driver ?\n"); 490e4fc250cSLuigi Rizzo verbose--; 491e4fc250cSLuigi Rizzo } 49216901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 49340929967SGleb Smirnoff return (ENOMEM); /* no polling for you */ 494e4fc250cSLuigi Rizzo } 495e4fc250cSLuigi Rizzo 49616901c01SGleb Smirnoff for (i = 0 ; i < poll_handlers ; i++) 49716901c01SGleb Smirnoff if (pr[i].ifp == ifp && pr[i].handler != NULL) { 49816901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 49916901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_register: %s: handler" 50016901c01SGleb Smirnoff " already registered\n", ifp->if_xname); 50140929967SGleb Smirnoff return (EEXIST); 50216901c01SGleb Smirnoff } 50316901c01SGleb Smirnoff 504e4fc250cSLuigi Rizzo pr[poll_handlers].handler = h; 505e4fc250cSLuigi Rizzo pr[poll_handlers].ifp = ifp; 506e4fc250cSLuigi Rizzo poll_handlers++; 50716901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 508d105c784SLuigi Rizzo if (idlepoll_sleeping) 509d105c784SLuigi Rizzo wakeup(&idlepoll_sleeping); 51040929967SGleb Smirnoff return (0); 511e4fc250cSLuigi Rizzo } 512e4fc250cSLuigi Rizzo 513e4fc250cSLuigi Rizzo /* 51440929967SGleb Smirnoff * Remove interface from the polling list. Called from *_ioctl(), too. 515e4fc250cSLuigi Rizzo */ 516e4fc250cSLuigi Rizzo int 517bd071d4dSGleb Smirnoff ether_poll_deregister(if_t ifp) 518e4fc250cSLuigi Rizzo { 519e4fc250cSLuigi Rizzo int i; 520e4fc250cSLuigi Rizzo 52140929967SGleb Smirnoff KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 52216901c01SGleb Smirnoff 52316901c01SGleb Smirnoff mtx_lock(&poll_mtx); 52440929967SGleb Smirnoff 525e4fc250cSLuigi Rizzo for (i = 0 ; i < poll_handlers ; i++) 526e4fc250cSLuigi Rizzo if (pr[i].ifp == ifp) /* found it */ 527e4fc250cSLuigi Rizzo break; 528e4fc250cSLuigi Rizzo if (i == poll_handlers) { 52916901c01SGleb Smirnoff log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n", 53016901c01SGleb Smirnoff ifp->if_xname); 53140929967SGleb Smirnoff mtx_unlock(&poll_mtx); 53240929967SGleb Smirnoff return (ENOENT); 533e4fc250cSLuigi Rizzo } 534e4fc250cSLuigi Rizzo poll_handlers--; 535e4fc250cSLuigi Rizzo if (i < poll_handlers) { /* Last entry replaces this one. */ 536e4fc250cSLuigi Rizzo pr[i].handler = pr[poll_handlers].handler; 537e4fc250cSLuigi Rizzo pr[i].ifp = pr[poll_handlers].ifp; 538e4fc250cSLuigi Rizzo } 53916901c01SGleb Smirnoff mtx_unlock(&poll_mtx); 54040929967SGleb Smirnoff return (0); 54140929967SGleb Smirnoff } 54240929967SGleb Smirnoff 543d105c784SLuigi Rizzo static void 544d105c784SLuigi Rizzo poll_idle(void) 545d105c784SLuigi Rizzo { 546d105c784SLuigi Rizzo struct thread *td = curthread; 547d105c784SLuigi Rizzo struct rtprio rtp; 548d105c784SLuigi Rizzo 549d105c784SLuigi Rizzo rtp.prio = RTP_PRIO_MAX; /* lowest priority */ 550d105c784SLuigi Rizzo rtp.type = RTP_PRIO_IDLE; 551982d11f8SJeff Roberson PROC_SLOCK(td->td_proc); 5528460a577SJohn Birrell rtp_to_pri(&rtp, td); 553982d11f8SJeff Roberson PROC_SUNLOCK(td->td_proc); 554d105c784SLuigi Rizzo 555d105c784SLuigi Rizzo for (;;) { 556daccb638SLuigi Rizzo if (poll_in_idle_loop && poll_handlers > 0) { 557d105c784SLuigi Rizzo idlepoll_sleeping = 0; 558d105c784SLuigi Rizzo ether_poll(poll_each_burst); 559982d11f8SJeff Roberson thread_lock(td); 560b5cbda50SJohn Baldwin mi_switch(SW_VOL, NULL); 561982d11f8SJeff Roberson thread_unlock(td); 562d105c784SLuigi Rizzo } else { 563d105c784SLuigi Rizzo idlepoll_sleeping = 1; 5640f180a7cSJohn Baldwin tsleep(&idlepoll_sleeping, 0, "pollid", hz * 3); 565d105c784SLuigi Rizzo } 566d105c784SLuigi Rizzo } 567d105c784SLuigi Rizzo } 568d105c784SLuigi Rizzo 569d105c784SLuigi Rizzo static struct proc *idlepoll; 570d105c784SLuigi Rizzo static struct kproc_desc idlepoll_kp = { 571d105c784SLuigi Rizzo "idlepoll", 572d105c784SLuigi Rizzo poll_idle, 573d105c784SLuigi Rizzo &idlepoll 574d105c784SLuigi Rizzo }; 575237fdd78SRobert Watson SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, 576237fdd78SRobert Watson &idlepoll_kp); 577