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 void hardclock_device_poll(void); /* hook from hardclock */
51 static struct mtx poll_mtx;
54 * Polling support for [network] device drivers.
56 * Drivers which support this feature can register with the
59 * If registration is successful, the driver must disable interrupts,
60 * and further I/O is performed through the handler, which is invoked
61 * (at least once per clock tick) with 3 arguments: the "arg" passed at
62 * register time (a struct ifnet pointer), a command, and a "count" limit.
64 * The command can be one of the following:
65 * POLL_ONLY: quick move of "count" packets from input/output queues.
66 * POLL_AND_CHECK_STATUS: as above, plus check status registers or do
67 * other more expensive operations. This command is issued periodically
68 * but less frequently than POLL_ONLY.
70 * The count limit specifies how much work the handler can do during the
71 * call -- typically this is the number of packets to be received, or
72 * transmitted, etc. (drivers are free to interpret this number, as long
73 * as the max time spent in the function grows roughly linearly with the
76 * Polling is enabled and disabled via setting IFCAP_POLLING flag on
77 * the interface. The driver ioctl handler should register interface
78 * with polling and disable interrupts, if registration was successful.
80 * A second variable controls the sharing of CPU between polling/kernel
81 * network processing, and other activities (typically userlevel tasks):
82 * kern.polling.user_frac (between 0 and 100, default 50) sets the share
83 * of CPU allocated to user tasks. CPU is allocated proportionally to the
84 * shares, by dynamically adjusting the "count" (poll_burst).
86 * Other parameters can should be left to their default values.
87 * The following constraints hold
89 * 1 <= poll_each_burst <= poll_burst <= poll_burst_max
90 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
93 #define MIN_POLL_BURST_MAX 10
94 #define MAX_POLL_BURST_MAX 20000
96 static uint32_t poll_burst = 5;
97 static uint32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */
98 static uint32_t poll_each_burst = 5;
100 static SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
101 "Device polling parameters");
103 SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RD,
104 &poll_burst, 0, "Current polling burst size");
106 static int netisr_poll_scheduled;
107 static int netisr_pollmore_scheduled;
108 static int poll_shutting_down;
110 static int poll_burst_max_sysctl(SYSCTL_HANDLER_ARGS)
112 uint32_t val = poll_burst_max;
115 error = sysctl_handle_int(oidp, &val, 0, req);
116 if (error || !req->newptr )
118 if (val < MIN_POLL_BURST_MAX || val > MAX_POLL_BURST_MAX)
122 poll_burst_max = val;
123 if (poll_burst > poll_burst_max)
124 poll_burst = poll_burst_max;
125 if (poll_each_burst > poll_burst_max)
126 poll_each_burst = MIN_POLL_BURST_MAX;
127 mtx_unlock(&poll_mtx);
131 SYSCTL_PROC(_kern_polling, OID_AUTO, burst_max, CTLTYPE_UINT | CTLFLAG_RW,
132 0, sizeof(uint32_t), poll_burst_max_sysctl, "I", "Max Polling burst size");
134 static int poll_each_burst_sysctl(SYSCTL_HANDLER_ARGS)
136 uint32_t val = poll_each_burst;
139 error = sysctl_handle_int(oidp, &val, 0, req);
140 if (error || !req->newptr )
146 if (val > poll_burst_max) {
147 mtx_unlock(&poll_mtx);
150 poll_each_burst = val;
151 mtx_unlock(&poll_mtx);
155 SYSCTL_PROC(_kern_polling, OID_AUTO, each_burst, CTLTYPE_UINT | CTLFLAG_RW,
156 0, sizeof(uint32_t), poll_each_burst_sysctl, "I",
157 "Max size of each burst");
159 static uint32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */
160 SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW,
161 &poll_in_idle_loop, 0, "Enable device polling in idle loop");
163 static uint32_t user_frac = 50;
164 static int user_frac_sysctl(SYSCTL_HANDLER_ARGS)
166 uint32_t val = user_frac;
169 error = sysctl_handle_int(oidp, &val, 0, req);
170 if (error || !req->newptr )
177 mtx_unlock(&poll_mtx);
181 SYSCTL_PROC(_kern_polling, OID_AUTO, user_frac, CTLTYPE_UINT | CTLFLAG_RW,
182 0, sizeof(uint32_t), user_frac_sysctl, "I",
183 "Desired user fraction of cpu time");
185 static uint32_t reg_frac_count = 0;
186 static uint32_t reg_frac = 20 ;
187 static int reg_frac_sysctl(SYSCTL_HANDLER_ARGS)
189 uint32_t val = reg_frac;
192 error = sysctl_handle_int(oidp, &val, 0, req);
193 if (error || !req->newptr )
195 if (val < 1 || val > hz)
200 if (reg_frac_count >= reg_frac)
202 mtx_unlock(&poll_mtx);
206 SYSCTL_PROC(_kern_polling, OID_AUTO, reg_frac, CTLTYPE_UINT | CTLFLAG_RW,
207 0, sizeof(uint32_t), reg_frac_sysctl, "I",
208 "Every this many cycles check registers");
210 static uint32_t short_ticks;
211 SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RD,
212 &short_ticks, 0, "Hardclock ticks shorter than they should be");
214 static uint32_t lost_polls;
215 SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RD,
216 &lost_polls, 0, "How many times we would have lost a poll tick");
218 static uint32_t pending_polls;
219 SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RD,
220 &pending_polls, 0, "Do we need to poll again");
222 static int residual_burst = 0;
223 SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RD,
224 &residual_burst, 0, "# of residual cycles in burst");
226 static uint32_t poll_handlers; /* next free entry in pr[]. */
227 SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD,
228 &poll_handlers, 0, "Number of registered poll handlers");
230 static uint32_t phase;
231 SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RD,
232 &phase, 0, "Polling phase");
234 static uint32_t suspect;
235 SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RD,
236 &suspect, 0, "suspect event");
238 static uint32_t stalled;
239 SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RD,
240 &stalled, 0, "potential stalls");
242 static uint32_t idlepoll_sleeping; /* idlepoll is sleeping */
243 SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD,
244 &idlepoll_sleeping, 0, "idlepoll is sleeping");
247 #define POLL_LIST_LEN 128
249 poll_handler_t *handler;
253 static struct pollrec pr[POLL_LIST_LEN];
256 poll_shutdown(void *arg, int howto)
259 poll_shutting_down = 1;
263 init_device_poll(void)
266 mtx_init(&poll_mtx, "polling", NULL, MTX_DEF);
267 EVENTHANDLER_REGISTER(shutdown_post_sync, poll_shutdown, NULL,
270 SYSINIT(device_poll, SI_SUB_SOFTINTR, SI_ORDER_MIDDLE, init_device_poll, NULL);
274 * Hook from hardclock. Tries to schedule a netisr, but keeps track
275 * of lost ticks due to the previous handler taking too long.
276 * Normally, this should not happen, because polling handler should
277 * run for a short time. However, in some cases (e.g. when there are
278 * changes in link status etc.) the drivers take a very long time
279 * (even in the order of milliseconds) to reset and reconfigure the
280 * device, causing apparent lost polls.
282 * The first part of the code is just for debugging purposes, and tries
283 * to count how often hardclock ticks are shorter than they should,
284 * meaning either stray interrupts or delayed events.
287 hardclock_device_poll(void)
289 static struct timeval prev_t, t;
292 if (poll_handlers == 0 || poll_shutting_down)
296 delta = (t.tv_usec - prev_t.tv_usec) +
297 (t.tv_sec - prev_t.tv_sec)*1000000;
298 if (delta * hz < 500000)
303 if (pending_polls > 100) {
305 * Too much, assume it has stalled (not always true
306 * see comment above).
317 netisr_poll_scheduled = 1;
318 netisr_pollmore_scheduled = 1;
322 if (pending_polls++ > 0)
327 * ether_poll is called from the idle loop.
330 ether_poll(int count)
336 if (count > poll_each_burst)
337 count = poll_each_burst;
339 for (i = 0 ; i < poll_handlers ; i++)
340 pr[i].handler(pr[i].ifp, POLL_ONLY, count);
342 mtx_unlock(&poll_mtx);
346 * netisr_pollmore is called after other netisr's, possibly scheduling
347 * another NETISR_POLL call, or adapting the burst size for the next cycle.
349 * It is very bad to fetch large bursts of packets from a single card at once,
350 * because the burst could take a long time to be completely processed, or
351 * could saturate the intermediate queue (ipintrq or similar) leading to
352 * losses or unfairness. To reduce the problem, and also to account better for
353 * time spent in network-related processing, we split the burst in smaller
354 * chunks of fixed size, giving control to the other netisr's between chunks.
355 * This helps in improving the fairness, reducing livelock (because we
356 * emulate more closely the "process to completion" that we have with
357 * fastforwarding) and accounting for the work performed in low level
358 * handling and forwarding.
361 static struct timeval poll_start_t;
370 if (!netisr_pollmore_scheduled) {
371 mtx_unlock(&poll_mtx);
374 netisr_pollmore_scheduled = 0;
376 if (residual_burst > 0) {
377 netisr_poll_scheduled = 1;
378 netisr_pollmore_scheduled = 1;
380 mtx_unlock(&poll_mtx);
381 /* will run immediately on return, followed by netisrs */
384 /* here we can account time spent in netisr's in this tick */
386 kern_load = (t.tv_usec - poll_start_t.tv_usec) +
387 (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */
388 kern_load = (kern_load * hz) / 10000; /* 0..100 */
389 if (kern_load > (100 - user_frac)) { /* try decrease ticks */
393 if (poll_burst < poll_burst_max)
398 if (pending_polls == 0) /* we are done */
402 * Last cycle was long and caused us to miss one or more
403 * hardclock ticks. Restart processing again, but slightly
404 * reduce the burst size to prevent that this happens again.
406 poll_burst -= (poll_burst / 8);
409 netisr_poll_scheduled = 1;
410 netisr_pollmore_scheduled = 1;
414 mtx_unlock(&poll_mtx);
418 * netisr_poll is typically scheduled once per tick.
424 enum poll_cmd arg = POLL_ONLY;
427 if (!netisr_poll_scheduled) {
428 mtx_unlock(&poll_mtx);
431 netisr_poll_scheduled = 0;
433 if (residual_burst == 0) { /* first call in this tick */
434 microuptime(&poll_start_t);
435 if (++reg_frac_count == reg_frac) {
436 arg = POLL_AND_CHECK_STATUS;
440 residual_burst = poll_burst;
442 cycles = (residual_burst < poll_each_burst) ?
443 residual_burst : poll_each_burst;
444 residual_burst -= cycles;
446 for (i = 0 ; i < poll_handlers ; i++)
447 pr[i].handler(pr[i].ifp, arg, cycles);
450 mtx_unlock(&poll_mtx);
454 * Try to register routine for polling. Returns 0 if successful
455 * (and polling should be enabled), error code otherwise.
456 * A device is not supposed to register itself multiple times.
458 * This is called from within the *_ioctl() functions.
461 ether_poll_register(poll_handler_t *h, struct ifnet *ifp)
465 KASSERT(h != NULL, ("%s: handler is NULL", __func__));
466 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
469 if (poll_handlers >= POLL_LIST_LEN) {
471 * List full, cannot register more entries.
472 * This should never happen; if it does, it is probably a
473 * broken driver trying to register multiple times. Checking
474 * this at runtime is expensive, and won't solve the problem
475 * anyways, so just report a few times and then give up.
477 static int verbose = 10 ;
479 log(LOG_ERR, "poll handlers list full, "
480 "maybe a broken driver ?\n");
483 mtx_unlock(&poll_mtx);
484 return (ENOMEM); /* no polling for you */
487 for (i = 0 ; i < poll_handlers ; i++)
488 if (pr[i].ifp == ifp && pr[i].handler != NULL) {
489 mtx_unlock(&poll_mtx);
490 log(LOG_DEBUG, "ether_poll_register: %s: handler"
491 " already registered\n", ifp->if_xname);
495 pr[poll_handlers].handler = h;
496 pr[poll_handlers].ifp = ifp;
498 mtx_unlock(&poll_mtx);
499 if (idlepoll_sleeping)
500 wakeup(&idlepoll_sleeping);
505 * Remove interface from the polling list. Called from *_ioctl(), too.
508 ether_poll_deregister(struct ifnet *ifp)
512 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
516 for (i = 0 ; i < poll_handlers ; i++)
517 if (pr[i].ifp == ifp) /* found it */
519 if (i == poll_handlers) {
520 log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n",
522 mtx_unlock(&poll_mtx);
526 if (i < poll_handlers) { /* Last entry replaces this one. */
527 pr[i].handler = pr[poll_handlers].handler;
528 pr[i].ifp = pr[poll_handlers].ifp;
530 mtx_unlock(&poll_mtx);
537 struct thread *td = curthread;
540 rtp.prio = RTP_PRIO_MAX; /* lowest priority */
541 rtp.type = RTP_PRIO_IDLE;
542 PROC_SLOCK(td->td_proc);
543 rtp_to_pri(&rtp, td);
544 PROC_SUNLOCK(td->td_proc);
547 if (poll_in_idle_loop && poll_handlers > 0) {
548 idlepoll_sleeping = 0;
549 ether_poll(poll_each_burst);
551 mi_switch(SW_VOL, NULL);
554 idlepoll_sleeping = 1;
555 tsleep(&idlepoll_sleeping, 0, "pollid", hz * 3);
560 static struct proc *idlepoll;
561 static struct kproc_desc idlepoll_kp = {
566 SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start,