Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / kern / kern_poll.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 2001-2002 Luigi Rizzo
3 *
4 * Supported by: the Xorp Project (www.xorp.org)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
78195a76 28 * $DragonFly: src/sys/kern/kern_poll.c,v 1.22 2005/11/28 17:13:45 dillon Exp $
984263bc
MD
29 */
30
2b71c8f1
SZ
31#include "opt_polling.h"
32
984263bc
MD
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/socket.h> /* needed by net/if.h */
37#include <sys/sysctl.h>
38
df4596e1
JH
39#include <sys/thread2.h>
40#include <sys/msgport2.h>
41
984263bc
MD
42#include <i386/include/md_var.h> /* for vm_page_zero_idle() */
43#include <net/if.h> /* for IFF_* flags */
44#include <net/netisr.h> /* for NETISR_POLL */
45
9eeaa8a9 46/* the two netisr handlers */
3e61f60e
MD
47static int sysctl_pollhz(SYSCTL_HANDLER_ARGS);
48static int sysctl_polling(SYSCTL_HANDLER_ARGS);
b76bed62
MD
49static int netisr_poll(struct netmsg *);
50static int netisr_pollmore(struct netmsg *);
3e61f60e 51static void pollclock(systimer_t, struct intrframe *);
984263bc
MD
52
53void init_device_poll(void); /* init routine */
984263bc
MD
54
55/*
56 * Polling support for [network] device drivers.
57 *
58 * Drivers which support this feature try to register with the
59 * polling code.
60 *
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.
65 *
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.
71 * POLL_DEREGISTER: deregister and return to interrupt mode.
9c095379 72 * POLL_REGISTER: register and disable interrupts
984263bc
MD
73 *
74 * The first two commands are only issued if the interface is marked as
75 * 'IFF_UP and IFF_RUNNING', the last one only if IFF_RUNNING is set.
76 *
77 * The count limit specifies how much work the handler can do during the
78 * call -- typically this is the number of packets to be received, or
79 * transmitted, etc. (drivers are free to interpret this number, as long
80 * as the max time spent in the function grows roughly linearly with the
81 * count).
82 *
83 * Deregistration can be requested by the driver itself (typically in the
84 * *_stop() routine), or by the polling code, by invoking the handler.
85 *
86 * Polling can be globally enabled or disabled with the sysctl variable
87 * kern.polling.enable (default is 0, disabled)
88 *
89 * A second variable controls the sharing of CPU between polling/kernel
90 * network processing, and other activities (typically userlevel tasks):
91 * kern.polling.user_frac (between 0 and 100, default 50) sets the share
92 * of CPU allocated to user tasks. CPU is allocated proportionally to the
93 * shares, by dynamically adjusting the "count" (poll_burst).
94 *
95 * Other parameters can should be left to their default values.
96 * The following constraints hold
97 *
98 * 1 <= poll_each_burst <= poll_burst <= poll_burst_max
984263bc
MD
99 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
100 */
101
102#define MIN_POLL_BURST_MAX 10
103#define MAX_POLL_BURST_MAX 1000
104
3e61f60e
MD
105#ifndef DEVICE_POLLING_FREQ_MAX
106#define DEVICE_POLLING_FREQ_MAX 30000
107#endif
108#define DEVICE_POLLING_FREQ_DEFAULT 2000
109
984263bc
MD
110SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
111 "Device polling parameters");
112
113static u_int32_t poll_burst = 5;
114SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RW,
115 &poll_burst, 0, "Current polling burst size");
116
117static u_int32_t poll_each_burst = 5;
118SYSCTL_UINT(_kern_polling, OID_AUTO, each_burst, CTLFLAG_RW,
119 &poll_each_burst, 0, "Max size of each burst");
120
121static u_int32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */
122SYSCTL_UINT(_kern_polling, OID_AUTO, burst_max, CTLFLAG_RW,
123 &poll_burst_max, 0, "Max Polling burst size");
124
984263bc
MD
125static u_int32_t user_frac = 50;
126SYSCTL_UINT(_kern_polling, OID_AUTO, user_frac, CTLFLAG_RW,
127 &user_frac, 0, "Desired user fraction of cpu time");
128
129static u_int32_t reg_frac = 20 ;
130SYSCTL_UINT(_kern_polling, OID_AUTO, reg_frac, CTLFLAG_RW,
131 &reg_frac, 0, "Every this many cycles poll register");
132
133static u_int32_t short_ticks;
134SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RW,
135 &short_ticks, 0, "Hardclock ticks shorter than they should be");
136
137static u_int32_t lost_polls;
138SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RW,
139 &lost_polls, 0, "How many times we would have lost a poll tick");
140
141static u_int32_t pending_polls;
142SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RW,
143 &pending_polls, 0, "Do we need to poll again");
144
145static int residual_burst = 0;
146SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RW,
147 &residual_burst, 0, "# of residual cycles in burst");
148
149static u_int32_t poll_handlers; /* next free entry in pr[]. */
150SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD,
151 &poll_handlers, 0, "Number of registered poll handlers");
152
3e61f60e 153static int polling_enabled = 0; /* global polling enable */
320d152e
MD
154TUNABLE_INT("kern.polling.enable", &polling_enabled);
155SYSCTL_PROC(_kern_polling, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
3e61f60e 156 0, 0, sysctl_polling, "I", "Polling enabled");
984263bc 157
d072c8d2 158static u_int32_t phase;
984263bc
MD
159SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RW,
160 &phase, 0, "Polling phase");
161
162static u_int32_t suspect;
163SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RW,
164 &suspect, 0, "suspect event");
165
d072c8d2 166static u_int32_t stalled;
984263bc
MD
167SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RW,
168 &stalled, 0, "potential stalls");
169
3e61f60e
MD
170static int pollhz = DEVICE_POLLING_FREQ_DEFAULT;
171TUNABLE_INT("kern.polling.pollhz", &pollhz);
172SYSCTL_PROC(_kern_polling, OID_AUTO, pollhz, CTLTYPE_INT | CTLFLAG_RW,
173 0, 0, sysctl_pollhz, "I", "Device polling frequency");
b7572f04
JS
174
175#define POLL_LIST_LEN 128
176struct pollrec {
177 struct ifnet *ifp;
178};
179
180static struct pollrec pr[POLL_LIST_LEN];
3e61f60e 181static struct systimer gd0_pollclock;
984263bc
MD
182
183/*
184 * register relevant netisr. Called from kern_clock.c:
185 */
186void
187init_device_poll(void)
188{
bf82f9b7
MD
189 netisr_register(NETISR_POLL, cpu0_portfn, netisr_poll);
190 netisr_register(NETISR_POLLMORE, cpu0_portfn, netisr_pollmore);
3e61f60e
MD
191 systimer_init_periodic_nq(&gd0_pollclock, pollclock, NULL,
192 polling_enabled ? pollhz : 1);
193}
194
195/*
196 * Set the polling frequency
197 */
198static int
199sysctl_pollhz(SYSCTL_HANDLER_ARGS)
200{
201 int error, phz;
202
203 phz = pollhz;
204 error = sysctl_handle_int(oidp, &phz, 0, req);
205 if (error || req->newptr == NULL)
206 return error;
207 if (phz <= 0)
208 return EINVAL;
209 else if (phz > DEVICE_POLLING_FREQ_MAX)
210 phz = DEVICE_POLLING_FREQ_MAX;
211
212 crit_enter();
213 pollhz = phz;
214 if (polling_enabled)
215 gd0_pollclock.periodic = sys_cputimer->fromhz(phz);
216 crit_exit();
217 return 0;
218}
219
220/*
221 * Master enable. If polling is disabled, cut the polling systimer
222 * frequency to 1hz.
223 */
224static int
225sysctl_polling(SYSCTL_HANDLER_ARGS)
226{
227 int error, enabled;
228
229 enabled = polling_enabled;
230 error = sysctl_handle_int(oidp, &enabled, 0, req);
231 if (error || req->newptr == NULL)
232 return error;
233 polling_enabled = enabled;
234 if (polling_enabled)
235 gd0_pollclock.periodic = sys_cputimer->fromhz(pollhz);
236 else
237 gd0_pollclock.periodic = sys_cputimer->fromhz(1);
238 return 0;
984263bc
MD
239}
240
241/*
242 * Hook from hardclock. Tries to schedule a netisr, but keeps track
243 * of lost ticks due to the previous handler taking too long.
244 * Normally, this should not happen, because polling handler should
245 * run for a short time. However, in some cases (e.g. when there are
246 * changes in link status etc.) the drivers take a very long time
247 * (even in the order of milliseconds) to reset and reconfigure the
248 * device, causing apparent lost polls.
249 *
250 * The first part of the code is just for debugging purposes, and tries
251 * to count how often hardclock ticks are shorter than they should,
252 * meaning either stray interrupts or delayed events.
88c4d2f6
MD
253 *
254 * WARNING! called from fastint or IPI, the MP lock might not be held.
984263bc 255 */
3e61f60e
MD
256static void
257pollclock(systimer_t info __unused, struct intrframe *frame __unused)
984263bc
MD
258{
259 static struct timeval prev_t, t;
260 int delta;
261
262 if (poll_handlers == 0)
263 return;
264
265 microuptime(&t);
266 delta = (t.tv_usec - prev_t.tv_usec) +
267 (t.tv_sec - prev_t.tv_sec)*1000000;
268 if (delta * hz < 500000)
269 short_ticks++;
270 else
271 prev_t = t;
272
273 if (pending_polls > 100) {
274 /*
275 * Too much, assume it has stalled (not always true
276 * see comment above).
277 */
278 stalled++;
279 pending_polls = 0;
280 phase = 0;
281 }
282
283 if (phase <= 2) {
284 if (phase != 0)
285 suspect++;
286 phase = 1;
287 schednetisr(NETISR_POLL);
288 phase = 2;
289 }
290 if (pending_polls++ > 0)
291 lost_polls++;
292}
293
984263bc
MD
294/*
295 * netisr_pollmore is called after other netisr's, possibly scheduling
296 * another NETISR_POLL call, or adapting the burst size for the next cycle.
297 *
298 * It is very bad to fetch large bursts of packets from a single card at once,
299 * because the burst could take a long time to be completely processed, or
300 * could saturate the intermediate queue (ipintrq or similar) leading to
301 * losses or unfairness. To reduce the problem, and also to account better for
302 * time spent in network-related processing, we split the burst in smaller
303 * chunks of fixed size, giving control to the other netisr's between chunks.
304 * This helps in improving the fairness, reducing livelock (because we
305 * emulate more closely the "process to completion" that we have with
306 * fastforwarding) and accounting for the work performed in low level
307 * handling and forwarding.
308 */
309
984263bc
MD
310static struct timeval poll_start_t;
311
bf82f9b7 312/* ARGSUSED */
b76bed62 313static int
df4596e1 314netisr_pollmore(struct netmsg *msg)
984263bc
MD
315{
316 struct timeval t;
317 int kern_load;
984263bc 318
e43a034f 319 crit_enter();
a29576fc 320 lwkt_replymsg(&msg->nm_lmsg, 0);
984263bc
MD
321 phase = 5;
322 if (residual_burst > 0) {
323 schednetisr(NETISR_POLL);
324 /* will run immediately on return, followed by netisrs */
a80cf23b 325 goto out;
984263bc
MD
326 }
327 /* here we can account time spent in netisr's in this tick */
328 microuptime(&t);
329 kern_load = (t.tv_usec - poll_start_t.tv_usec) +
330 (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */
331 kern_load = (kern_load * hz) / 10000; /* 0..100 */
332 if (kern_load > (100 - user_frac)) { /* try decrease ticks */
333 if (poll_burst > 1)
334 poll_burst--;
335 } else {
336 if (poll_burst < poll_burst_max)
337 poll_burst++;
338 }
339
340 pending_polls--;
a29576fc 341 if (pending_polls == 0) { /* we are done */
984263bc 342 phase = 0;
a29576fc 343 } else {
984263bc
MD
344 /*
345 * Last cycle was long and caused us to miss one or more
346 * hardclock ticks. Restart processing again, but slightly
347 * reduce the burst size to prevent that this happens again.
348 */
349 poll_burst -= (poll_burst / 8);
350 if (poll_burst < 1)
351 poll_burst = 1;
352 schednetisr(NETISR_POLL);
353 phase = 6;
354 }
a80cf23b 355out:
e43a034f 356 crit_exit();
b76bed62 357 return(EASYNC);
984263bc
MD
358}
359
360/*
361 * netisr_poll is scheduled by schednetisr when appropriate, typically once
e43a034f 362 * per tick.
a29576fc
MD
363 *
364 * Note that the message is replied immediately in order to allow a new
365 * ISR to be scheduled in the handler.
e43a034f
MD
366 *
367 * XXX each registration should indicate whether it needs a critical
368 * section to operate.
984263bc 369 */
bf82f9b7 370/* ARGSUSED */
b76bed62 371static int
df4596e1 372netisr_poll(struct netmsg *msg)
984263bc
MD
373{
374 static int reg_frac_count;
b7572f04 375 int i, cycles;
984263bc 376 enum poll_cmd arg = POLL_ONLY;
984263bc 377
a29576fc 378 lwkt_replymsg(&msg->nm_lmsg, 0);
e43a034f 379 crit_enter();
984263bc
MD
380 phase = 3;
381 if (residual_burst == 0) { /* first call in this tick */
382 microuptime(&poll_start_t);
383 /*
384 * Check that paremeters are consistent with runtime
385 * variables. Some of these tests could be done at sysctl
386 * time, but the savings would be very limited because we
387 * still have to check against reg_frac_count and
388 * poll_each_burst. So, instead of writing separate sysctl
389 * handlers, we do all here.
390 */
391
392 if (reg_frac > hz)
393 reg_frac = hz;
394 else if (reg_frac < 1)
395 reg_frac = 1;
396 if (reg_frac_count > reg_frac)
397 reg_frac_count = reg_frac - 1;
398 if (reg_frac_count-- == 0) {
399 arg = POLL_AND_CHECK_STATUS;
400 reg_frac_count = reg_frac - 1;
401 }
402 if (poll_burst_max < MIN_POLL_BURST_MAX)
403 poll_burst_max = MIN_POLL_BURST_MAX;
404 else if (poll_burst_max > MAX_POLL_BURST_MAX)
405 poll_burst_max = MAX_POLL_BURST_MAX;
406
407 if (poll_each_burst < 1)
408 poll_each_burst = 1;
409 else if (poll_each_burst > poll_burst_max)
410 poll_each_burst = poll_burst_max;
411
412 residual_burst = poll_burst;
413 }
414 cycles = (residual_burst < poll_each_burst) ?
415 residual_burst : poll_each_burst;
416 residual_burst -= cycles;
417
3e61f60e 418 if (polling_enabled) {
b7572f04
JS
419 for (i = 0 ; i < poll_handlers ; i++) {
420 struct pollrec *p = &pr[i];
421 if ((p->ifp->if_flags & (IFF_UP|IFF_RUNNING|IFF_POLLING)) == (IFF_UP|IFF_RUNNING|IFF_POLLING)) {
78195a76
MD
422 if (lwkt_serialize_try(p->ifp->if_serializer)) {
423 p->ifp->if_poll(p->ifp, arg, cycles);
424 lwkt_serialize_exit(p->ifp->if_serializer);
425 }
b7572f04 426 }
a29576fc 427 }
984263bc 428 } else { /* unregister */
b7572f04
JS
429 for (i = 0 ; i < poll_handlers ; i++) {
430 struct pollrec *p = &pr[i];
78195a76
MD
431 if ((p->ifp->if_flags & IFF_POLLING) == 0)
432 continue;
433 /*
434 * Only call the interface deregistration
435 * function if the interface is still
436 * running.
437 */
438 lwkt_serialize_enter(p->ifp->if_serializer);
439 p->ifp->if_flags &= ~IFF_POLLING;
440 if (p->ifp->if_flags & IFF_RUNNING)
441 p->ifp->if_poll(p->ifp, POLL_DEREGISTER, 1);
442 lwkt_serialize_exit(p->ifp->if_serializer);
984263bc
MD
443 }
444 residual_burst = 0;
445 poll_handlers = 0;
446 }
447 schednetisr(NETISR_POLLMORE);
448 phase = 4;
e43a034f 449 crit_exit();
b76bed62 450 return(EASYNC);
984263bc
MD
451}
452
453/*
454 * Try to register routine for polling. Returns 1 if successful
455 * (and polling should be enabled), 0 otherwise.
984263bc 456 *
9c095379 457 * Called from mainline code only, not called from an interrupt.
984263bc
MD
458 */
459int
9c095379 460ether_poll_register(struct ifnet *ifp)
984263bc 461{
9c095379 462 int rc;
984263bc 463
3e61f60e 464 if (polling_enabled == 0) /* polling disabled, cannot register */
984263bc 465 return 0;
9c095379 466 if ((ifp->if_flags & IFF_UP) == 0) /* must be up */
984263bc 467 return 0;
46f25451 468 if (ifp->if_flags & IFF_POLLING) /* already polling */
984263bc 469 return 0;
9c095379
MD
470 if (ifp->if_poll == NULL) /* no polling support */
471 return 0;
472
473 /*
474 * Attempt to register. Interlock with IFF_POLLING.
475 */
476 crit_enter(); /* XXX MP - not mp safe */
78195a76 477 lwkt_serialize_enter(ifp->if_serializer);
9c095379
MD
478 ifp->if_flags |= IFF_POLLING;
479 ifp->if_poll(ifp, POLL_REGISTER, 0);
78195a76 480 lwkt_serialize_exit(ifp->if_serializer);
9c095379
MD
481 if ((ifp->if_flags & IFF_POLLING) == 0) {
482 crit_exit();
483 return 0;
484 }
984263bc 485
b7572f04
JS
486 /*
487 * Check if there is room. If there isn't, deregister.
488 */
489 if (poll_handlers >= POLL_LIST_LEN) {
490 /*
491 * List full, cannot register more entries.
492 * This should never happen; if it does, it is probably a
493 * broken driver trying to register multiple times. Checking
494 * this at runtime is expensive, and won't solve the problem
495 * anyways, so just report a few times and then give up.
496 */
497 static int verbose = 10 ;
498 if (verbose >0) {
499 printf("poll handlers list full, "
500 "maybe a broken driver ?\n");
501 verbose--;
502 }
503 ifp->if_flags &= ~IFF_POLLING;
78195a76 504 lwkt_serialize_enter(ifp->if_serializer);
b7572f04 505 ifp->if_poll(ifp, POLL_DEREGISTER, 0);
78195a76 506 lwkt_serialize_exit(ifp->if_serializer);
b7572f04
JS
507 rc = 0;
508 } else {
509 pr[poll_handlers].ifp = ifp;
510 poll_handlers++;
511 rc = 1;
512 }
9c095379
MD
513 crit_exit();
514 return (rc);
984263bc
MD
515}
516
517/*
9c095379
MD
518 * Remove interface from the polling list. Occurs when polling is turned
519 * off. Called from mainline code only, not called from an interrupt.
984263bc
MD
520 */
521int
522ether_poll_deregister(struct ifnet *ifp)
523{
b7572f04
JS
524 int i;
525
9c095379
MD
526 crit_enter();
527 if (ifp == NULL || (ifp->if_flags & IFF_POLLING) == 0) {
528 crit_exit();
984263bc
MD
529 return 0;
530 }
b7572f04
JS
531 for (i = 0 ; i < poll_handlers ; i++) {
532 if (pr[i].ifp == ifp) /* found it */
533 break;
534 }
535 ifp->if_flags &= ~IFF_POLLING; /* found or not... */
536 if (i == poll_handlers) {
537 crit_exit();
538 printf("ether_poll_deregister: ifp not found!!!\n");
539 return 0;
540 }
984263bc 541 poll_handlers--;
b7572f04
JS
542 if (i < poll_handlers) { /* Last entry replaces this one. */
543 pr[i].ifp = pr[poll_handlers].ifp;
544 }
9c095379
MD
545 crit_exit();
546
547 /*
548 * Only call the deregistration function if the interface is still
549 * in a run state.
550 */
78195a76
MD
551 if (ifp->if_flags & IFF_RUNNING) {
552 lwkt_serialize_enter(ifp->if_serializer);
9c095379 553 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
78195a76
MD
554 lwkt_serialize_exit(ifp->if_serializer);
555 }
9c095379 556 return (1);
984263bc 557}
a29576fc
MD
558
559void
560emergency_poll_enable(const char *name)
561{
3e61f60e
MD
562 if (polling_enabled == 0) {
563 polling_enabled = 1;
a29576fc
MD
564 printf("%s forced polling on\n", name);
565 }
566}