2 * Copyright (c) 2001-2002 Luigi Rizzo
4 * Supported by: the Xorp Project (www.xorp.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_device_polling.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
38 #include <sys/eventhandler.h>
39 #include <sys/resourcevar.h>
40 #include <sys/socket.h> /* needed by net/if.h */
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/syslog.h>
45 #include <net/if.h> /* for IFF_* flags */
46 #include <net/netisr.h> /* for NETISR_POLL */
49 static int poll_switch(SYSCTL_HANDLER_ARGS);
51 void hardclock_device_poll(void); /* hook from hardclock */
53 static struct mtx poll_mtx;
56 * Polling support for [network] device drivers.
58 * Drivers which support this feature can register with the
61 * If registration is successful, the driver must disable interrupts,
62 * and further I/O is performed through the handler, which is invoked
63 * (at least once per clock tick) with 3 arguments: the "arg" passed at
64 * register time (a struct ifnet pointer), a command, and a "count" limit.
66 * The command can be one of the following:
67 * POLL_ONLY: quick move of "count" packets from input/output queues.
68 * POLL_AND_CHECK_STATUS: as above, plus check status registers or do
69 * other more expensive operations. This command is issued periodically
70 * but less frequently than POLL_ONLY.
72 * The count limit specifies how much work the handler can do during the
73 * call -- typically this is the number of packets to be received, or
74 * transmitted, etc. (drivers are free to interpret this number, as long
75 * as the max time spent in the function grows roughly linearly with the
78 * Polling is enabled and disabled via setting IFCAP_POLLING flag on
79 * the interface. The driver ioctl handler should register interface
80 * with polling and disable interrupts, if registration was successful.
82 * A second variable controls the sharing of CPU between polling/kernel
83 * network processing, and other activities (typically userlevel tasks):
84 * kern.polling.user_frac (between 0 and 100, default 50) sets the share
85 * of CPU allocated to user tasks. CPU is allocated proportionally to the
86 * shares, by dynamically adjusting the "count" (poll_burst).
88 * Other parameters can should be left to their default values.
89 * The following constraints hold
91 * 1 <= poll_each_burst <= poll_burst <= poll_burst_max
92 * 0 <= poll_each_burst
93 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
96 #define MIN_POLL_BURST_MAX 10
97 #define MAX_POLL_BURST_MAX 1000
99 static uint32_t poll_burst = 5;
100 static uint32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */
101 static uint32_t poll_each_burst = 5;
103 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
104 "Device polling parameters");
106 SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RD,
107 &poll_burst, 0, "Current polling burst size");
109 static int netisr_poll_scheduled;
110 static int netisr_pollmore_scheduled;
111 static int poll_shutting_down;
113 static int poll_burst_max_sysctl(SYSCTL_HANDLER_ARGS)
115 uint32_t val = poll_burst_max;
118 error = sysctl_handle_int(oidp, &val, 0, req);
119 if (error || !req->newptr )
121 if (val < MIN_POLL_BURST_MAX || val > MAX_POLL_BURST_MAX)
125 poll_burst_max = val;
126 if (poll_burst > poll_burst_max)
127 poll_burst = poll_burst_max;
128 if (poll_each_burst > poll_burst_max)
129 poll_each_burst = MIN_POLL_BURST_MAX;
130 mtx_unlock(&poll_mtx);
134 SYSCTL_PROC(_kern_polling, OID_AUTO, burst_max, CTLTYPE_UINT | CTLFLAG_RW,
135 0, sizeof(uint32_t), poll_burst_max_sysctl, "I", "Max Polling burst size");
137 static int poll_each_burst_sysctl(SYSCTL_HANDLER_ARGS)
139 uint32_t val = poll_each_burst;
142 error = sysctl_handle_int(oidp, &val, 0, req);
143 if (error || !req->newptr )
149 if (val > poll_burst_max) {
150 mtx_unlock(&poll_mtx);
153 poll_each_burst = val;
154 mtx_unlock(&poll_mtx);
158 SYSCTL_PROC(_kern_polling, OID_AUTO, each_burst, CTLTYPE_UINT | CTLFLAG_RW,
159 0, sizeof(uint32_t), poll_each_burst_sysctl, "I",
160 "Max size of each burst");
162 static uint32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */
163 SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW,
164 &poll_in_idle_loop, 0, "Enable device polling in idle loop");
166 static uint32_t user_frac = 50;
167 static int user_frac_sysctl(SYSCTL_HANDLER_ARGS)
169 uint32_t val = user_frac;
172 error = sysctl_handle_int(oidp, &val, 0, req);
173 if (error || !req->newptr )
175 if (val < 0 || val > 99)
180 mtx_unlock(&poll_mtx);
184 SYSCTL_PROC(_kern_polling, OID_AUTO, user_frac, CTLTYPE_UINT | CTLFLAG_RW,
185 0, sizeof(uint32_t), user_frac_sysctl, "I",
186 "Desired user fraction of cpu time");
188 static uint32_t reg_frac_count = 0;
189 static uint32_t reg_frac = 20 ;
190 static int reg_frac_sysctl(SYSCTL_HANDLER_ARGS)
192 uint32_t val = reg_frac;
195 error = sysctl_handle_int(oidp, &val, 0, req);
196 if (error || !req->newptr )
198 if (val < 1 || val > hz)
203 if (reg_frac_count >= reg_frac)
205 mtx_unlock(&poll_mtx);
209 SYSCTL_PROC(_kern_polling, OID_AUTO, reg_frac, CTLTYPE_UINT | CTLFLAG_RW,
210 0, sizeof(uint32_t), reg_frac_sysctl, "I",
211 "Every this many cycles check registers");
213 static uint32_t short_ticks;
214 SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RD,
215 &short_ticks, 0, "Hardclock ticks shorter than they should be");
217 static uint32_t lost_polls;
218 SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RD,
219 &lost_polls, 0, "How many times we would have lost a poll tick");
221 static uint32_t pending_polls;
222 SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RD,
223 &pending_polls, 0, "Do we need to poll again");
225 static int residual_burst = 0;
226 SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RD,
227 &residual_burst, 0, "# of residual cycles in burst");
229 static uint32_t poll_handlers; /* next free entry in pr[]. */
230 SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD,
231 &poll_handlers, 0, "Number of registered poll handlers");
233 static int polling = 0;
234 SYSCTL_PROC(_kern_polling, OID_AUTO, enable, CTLTYPE_UINT | CTLFLAG_RW,
235 0, sizeof(int), poll_switch, "I", "Switch polling for all interfaces");
237 static uint32_t phase;
238 SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RD,
239 &phase, 0, "Polling phase");
241 static uint32_t suspect;
242 SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RD,
243 &suspect, 0, "suspect event");
245 static uint32_t stalled;
246 SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RD,
247 &stalled, 0, "potential stalls");
249 static uint32_t idlepoll_sleeping; /* idlepoll is sleeping */
250 SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD,
251 &idlepoll_sleeping, 0, "idlepoll is sleeping");
254 #define POLL_LIST_LEN 128
256 poll_handler_t *handler;
260 static struct pollrec pr[POLL_LIST_LEN];
263 poll_shutdown(void *arg, int howto)
266 poll_shutting_down = 1;
270 init_device_poll(void)
273 mtx_init(&poll_mtx, "polling", NULL, MTX_DEF);
274 EVENTHANDLER_REGISTER(shutdown_post_sync, poll_shutdown, NULL,
277 SYSINIT(device_poll, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, init_device_poll, NULL);
281 * Hook from hardclock. Tries to schedule a netisr, but keeps track
282 * of lost ticks due to the previous handler taking too long.
283 * Normally, this should not happen, because polling handler should
284 * run for a short time. However, in some cases (e.g. when there are
285 * changes in link status etc.) the drivers take a very long time
286 * (even in the order of milliseconds) to reset and reconfigure the
287 * device, causing apparent lost polls.
289 * The first part of the code is just for debugging purposes, and tries
290 * to count how often hardclock ticks are shorter than they should,
291 * meaning either stray interrupts or delayed events.
294 hardclock_device_poll(void)
296 static struct timeval prev_t, t;
299 if (poll_handlers == 0 || poll_shutting_down)
303 delta = (t.tv_usec - prev_t.tv_usec) +
304 (t.tv_sec - prev_t.tv_sec)*1000000;
305 if (delta * hz < 500000)
310 if (pending_polls > 100) {
312 * Too much, assume it has stalled (not always true
313 * see comment above).
324 netisr_poll_scheduled = 1;
325 netisr_pollmore_scheduled = 1;
329 if (pending_polls++ > 0)
334 * ether_poll is called from the idle loop.
337 ether_poll(int count)
343 if (count > poll_each_burst)
344 count = poll_each_burst;
346 for (i = 0 ; i < poll_handlers ; i++)
347 pr[i].handler(pr[i].ifp, POLL_ONLY, count);
349 mtx_unlock(&poll_mtx);
353 * netisr_pollmore is called after other netisr's, possibly scheduling
354 * another NETISR_POLL call, or adapting the burst size for the next cycle.
356 * It is very bad to fetch large bursts of packets from a single card at once,
357 * because the burst could take a long time to be completely processed, or
358 * could saturate the intermediate queue (ipintrq or similar) leading to
359 * losses or unfairness. To reduce the problem, and also to account better for
360 * time spent in network-related processing, we split the burst in smaller
361 * chunks of fixed size, giving control to the other netisr's between chunks.
362 * This helps in improving the fairness, reducing livelock (because we
363 * emulate more closely the "process to completion" that we have with
364 * fastforwarding) and accounting for the work performed in low level
365 * handling and forwarding.
368 static struct timeval poll_start_t;
377 if (!netisr_pollmore_scheduled) {
378 mtx_unlock(&poll_mtx);
381 netisr_pollmore_scheduled = 0;
383 if (residual_burst > 0) {
384 netisr_poll_scheduled = 1;
385 netisr_pollmore_scheduled = 1;
387 mtx_unlock(&poll_mtx);
388 /* will run immediately on return, followed by netisrs */
391 /* here we can account time spent in netisr's in this tick */
393 kern_load = (t.tv_usec - poll_start_t.tv_usec) +
394 (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */
395 kern_load = (kern_load * hz) / 10000; /* 0..100 */
396 if (kern_load > (100 - user_frac)) { /* try decrease ticks */
400 if (poll_burst < poll_burst_max)
405 if (pending_polls == 0) /* we are done */
409 * Last cycle was long and caused us to miss one or more
410 * hardclock ticks. Restart processing again, but slightly
411 * reduce the burst size to prevent that this happens again.
413 poll_burst -= (poll_burst / 8);
416 netisr_poll_scheduled = 1;
417 netisr_pollmore_scheduled = 1;
421 mtx_unlock(&poll_mtx);
425 * netisr_poll is typically scheduled once per tick.
431 enum poll_cmd arg = POLL_ONLY;
434 if (!netisr_poll_scheduled) {
435 mtx_unlock(&poll_mtx);
438 netisr_poll_scheduled = 0;
440 if (residual_burst == 0) { /* first call in this tick */
441 microuptime(&poll_start_t);
442 if (++reg_frac_count == reg_frac) {
443 arg = POLL_AND_CHECK_STATUS;
447 residual_burst = poll_burst;
449 cycles = (residual_burst < poll_each_burst) ?
450 residual_burst : poll_each_burst;
451 residual_burst -= cycles;
453 for (i = 0 ; i < poll_handlers ; i++)
454 pr[i].handler(pr[i].ifp, arg, cycles);
457 mtx_unlock(&poll_mtx);
461 * Try to register routine for polling. Returns 0 if successful
462 * (and polling should be enabled), error code otherwise.
463 * A device is not supposed to register itself multiple times.
465 * This is called from within the *_ioctl() functions.
468 ether_poll_register(poll_handler_t *h, struct ifnet *ifp)
472 KASSERT(h != NULL, ("%s: handler is NULL", __func__));
473 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
476 if (poll_handlers >= POLL_LIST_LEN) {
478 * List full, cannot register more entries.
479 * This should never happen; if it does, it is probably a
480 * broken driver trying to register multiple times. Checking
481 * this at runtime is expensive, and won't solve the problem
482 * anyways, so just report a few times and then give up.
484 static int verbose = 10 ;
486 log(LOG_ERR, "poll handlers list full, "
487 "maybe a broken driver ?\n");
490 mtx_unlock(&poll_mtx);
491 return (ENOMEM); /* no polling for you */
494 for (i = 0 ; i < poll_handlers ; i++)
495 if (pr[i].ifp == ifp && pr[i].handler != NULL) {
496 mtx_unlock(&poll_mtx);
497 log(LOG_DEBUG, "ether_poll_register: %s: handler"
498 " already registered\n", ifp->if_xname);
502 pr[poll_handlers].handler = h;
503 pr[poll_handlers].ifp = ifp;
505 mtx_unlock(&poll_mtx);
506 if (idlepoll_sleeping)
507 wakeup(&idlepoll_sleeping);
512 * Remove interface from the polling list. Called from *_ioctl(), too.
515 ether_poll_deregister(struct ifnet *ifp)
519 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
523 for (i = 0 ; i < poll_handlers ; i++)
524 if (pr[i].ifp == ifp) /* found it */
526 if (i == poll_handlers) {
527 log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n",
529 mtx_unlock(&poll_mtx);
533 if (i < poll_handlers) { /* Last entry replaces this one. */
534 pr[i].handler = pr[poll_handlers].handler;
535 pr[i].ifp = pr[poll_handlers].ifp;
537 mtx_unlock(&poll_mtx);
542 * Legacy interface for turning polling on all interfaces at one time.
545 poll_switch(SYSCTL_HANDLER_ARGS)
551 error = sysctl_handle_int(oidp, &val, 0, req);
552 if (error || !req->newptr )
558 if (val < 0 || val > 1)
564 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
565 if (ifp->if_capabilities & IFCAP_POLLING) {
570 ifp->if_capenable | IFCAP_POLLING;
573 ifp->if_capenable & ~IFCAP_POLLING;
574 (void) (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
579 log(LOG_ERR, "kern.polling.enable is deprecated. Use ifconfig(8)");
587 struct thread *td = curthread;
590 rtp.prio = RTP_PRIO_MAX; /* lowest priority */
591 rtp.type = RTP_PRIO_IDLE;
592 PROC_SLOCK(td->td_proc);
593 rtp_to_pri(&rtp, td);
594 PROC_SUNLOCK(td->td_proc);
597 if (poll_in_idle_loop && poll_handlers > 0) {
598 idlepoll_sleeping = 0;
599 ether_poll(poll_each_burst);
601 mi_switch(SW_VOL, NULL);
604 idlepoll_sleeping = 1;
605 tsleep(&idlepoll_sleeping, 0, "pollid", hz * 3);
610 static struct proc *idlepoll;
611 static struct kproc_desc idlepoll_kp = {
616 SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start,