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
27 * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
30 #include "opt_ifpoll.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/serialize.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <sys/thread2.h>
41 #include <sys/msgport2.h>
43 #include <machine/atomic.h>
44 #include <machine/clock.h>
45 #include <machine/smp.h>
48 #include <net/if_poll.h>
49 #include <net/netmsg2.h>
52 * Polling support for network device drivers.
54 * Drivers which support this feature try to register one status polling
55 * handler and several TX/RX polling handlers with the polling code.
56 * If interface's if_npoll is called with non-NULL second argument, then
57 * a register operation is requested, else a deregister operation is
58 * requested. If the requested operation is "register", driver should
59 * setup the ifpoll_info passed in accoding its own needs:
60 * ifpoll_info.ifpi_status.status_func == NULL
61 * No status polling handler will be installed on CPU(0)
62 * ifpoll_info.ifpi_rx[n].poll_func == NULL
63 * No RX polling handler will be installed on CPU(n)
64 * ifpoll_info.ifpi_tx[n].poll_func == NULL
65 * No TX polling handler will be installed on CPU(n)
67 * RX is polled at the specified polling frequency (net.ifpoll.X.pollhz).
68 * TX and status polling could be done at lower frequency than RX frequency
69 * (net.ifpoll.0.status_frac and net.ifpoll.X.tx_frac). To avoid systimer
70 * staggering at high frequency, RX systimer gives TX and status polling a
73 * All of the registered polling handlers are called only if the interface
74 * is marked as 'IFF_RUNNING and IFF_NPOLLING'. However, the interface's
75 * register and deregister function (ifnet.if_npoll) will be called even
76 * if interface is not marked with 'IFF_RUNNING'.
78 * If registration is successful, the driver must disable interrupts,
79 * and further I/O is performed through the TX/RX polling handler, which
80 * are invoked (at least once per clock tick) with 3 arguments: the "arg"
81 * passed at register time, a struct ifnet pointer, and a "count" limit.
82 * The registered serializer will be held before calling the related
85 * The count limit specifies how much work the handler can do during the
86 * call -- typically this is the number of packets to be received, or
87 * transmitted, etc. (drivers are free to interpret this number, as long
88 * as the max time spent in the function grows roughly linearly with the
91 * A second variable controls the sharing of CPU between polling/kernel
92 * network processing, and other activities (typically userlevel tasks):
93 * net.ifpoll.X.{rx,tx}.user_frac (between 0 and 100, default 50) sets the
94 * share of CPU allocated to user tasks. CPU is allocated proportionally
95 * to the shares, by dynamically adjusting the "count" (poll_burst).
97 * Other parameters can should be left to their default values.
98 * The following constraints hold
100 * 1 <= poll_burst <= poll_burst_max
101 * 1 <= poll_each_burst <= poll_burst_max
102 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
105 #define IFPOLL_LIST_LEN 128
106 #define IFPOLL_FREQ_MAX 30000
108 #define MIN_IOPOLL_BURST_MAX 10
109 #define MAX_IOPOLL_BURST_MAX 1000
110 #define IOPOLL_BURST_MAX 150 /* good for 100Mbit net and HZ=1000 */
112 #define IOPOLL_EACH_BURST 5
114 #define IFPOLL_FREQ_DEFAULT 2000
116 #define IFPOLL_TXFRAC_DEFAULT 1 /* 1/2 of the pollhz */
117 #define IFPOLL_STFRAC_DEFAULT 19 /* 1/20 of the pollhz */
119 #define IFPOLL_RX 0x1
120 #define IFPOLL_TX 0x2
128 struct lwkt_serialize *serializer;
131 ifpoll_iofn_t poll_func;
135 union ifpoll_time prev_t;
136 uint32_t short_ticks; /* statistics */
137 uint32_t lost_polls; /* statistics */
138 uint32_t suspect; /* statistics */
139 uint32_t stalled; /* statistics */
140 uint32_t pending_polls; /* state */
142 struct netmsg_base poll_netmsg;
146 uint32_t phase; /* state */
147 int residual_burst; /* state */
148 uint32_t poll_each_burst; /* tunable */
149 union ifpoll_time poll_start_t; /* state */
151 uint32_t poll_handlers; /* next free entry in pr[]. */
152 struct iopoll_rec pr[IFPOLL_LIST_LEN];
154 struct netmsg_base poll_more_netmsg;
156 uint32_t poll_burst; /* state */
157 uint32_t poll_burst_max; /* tunable */
158 uint32_t user_frac; /* tunable */
159 uint32_t kern_frac; /* state */
161 struct sysctl_ctx_list poll_sysctl_ctx;
162 struct sysctl_oid *poll_sysctl_tree;
166 struct systimer pollclock;
169 int stfrac_count; /* state */
170 int poll_stfrac; /* tunable */
172 int txfrac_count; /* state */
173 int poll_txfrac; /* tunable */
175 int pollhz; /* tunable */
177 struct sysctl_ctx_list sysctl_ctx;
178 struct sysctl_oid *sysctl_tree;
182 struct lwkt_serialize *serializer;
184 ifpoll_stfn_t status_func;
188 struct netmsg_base poll_netmsg;
192 uint32_t poll_handlers; /* next free entry in pr[]. */
193 struct stpoll_rec pr[IFPOLL_LIST_LEN];
195 struct sysctl_ctx_list poll_sysctl_ctx;
196 struct sysctl_oid *poll_sysctl_tree;
199 struct iopoll_sysctl_netmsg {
200 struct netmsg_base base;
201 struct iopoll_ctx *ctx;
204 void ifpoll_init_pcpu(int);
205 static void ifpoll_register_handler(netmsg_t);
206 static void ifpoll_deregister_handler(netmsg_t);
211 static void stpoll_init(void);
212 static void stpoll_handler(netmsg_t);
213 static void stpoll_clock(struct stpoll_ctx *);
214 static int stpoll_register(struct ifnet *, const struct ifpoll_status *);
215 static int stpoll_deregister(struct ifnet *);
220 static struct iopoll_ctx *iopoll_ctx_create(int, int);
221 static void iopoll_init(int);
222 static void iopoll_handler(netmsg_t);
223 static void iopollmore_handler(netmsg_t);
224 static void iopoll_clock(struct iopoll_ctx *);
225 static int iopoll_register(struct ifnet *, struct iopoll_ctx *,
226 const struct ifpoll_io *);
227 static int iopoll_deregister(struct ifnet *, struct iopoll_ctx *);
229 static void iopoll_add_sysctl(struct sysctl_ctx_list *,
230 struct sysctl_oid_list *, struct iopoll_ctx *);
231 static void sysctl_burstmax_handler(netmsg_t);
232 static int sysctl_burstmax(SYSCTL_HANDLER_ARGS);
233 static void sysctl_eachburst_handler(netmsg_t);
234 static int sysctl_eachburst(SYSCTL_HANDLER_ARGS);
239 static void poll_comm_init(int);
240 static void poll_comm_start(int);
241 static void poll_comm_adjust_pollhz(struct poll_comm *);
242 static void poll_comm_systimer0(systimer_t, int, struct intrframe *);
243 static void poll_comm_systimer(systimer_t, int, struct intrframe *);
244 static void sysctl_pollhz_handler(netmsg_t);
245 static void sysctl_stfrac_handler(netmsg_t);
246 static void sysctl_txfrac_handler(netmsg_t);
247 static int sysctl_pollhz(SYSCTL_HANDLER_ARGS);
248 static int sysctl_stfrac(SYSCTL_HANDLER_ARGS);
249 static int sysctl_txfrac(SYSCTL_HANDLER_ARGS);
251 static struct stpoll_ctx stpoll_context;
252 static struct poll_comm *poll_common[MAXCPU];
253 static struct iopoll_ctx *rxpoll_context[MAXCPU];
254 static struct iopoll_ctx *txpoll_context[MAXCPU];
256 SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
257 "Network device polling parameters");
259 static int iopoll_burst_max = IOPOLL_BURST_MAX;
260 static int iopoll_each_burst = IOPOLL_EACH_BURST;
262 static int ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
263 static int ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
264 static int ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
266 TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
267 TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
268 TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
269 TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
270 TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
273 ifpoll_sendmsg_oncpu(netmsg_t msg)
275 if (msg->lmsg.ms_flags & MSGF_DONE)
276 lwkt_sendmsg(netisr_portfn(mycpuid), &msg->lmsg);
280 sched_stpoll(struct stpoll_ctx *st_ctx)
282 ifpoll_sendmsg_oncpu((netmsg_t)&st_ctx->poll_netmsg);
286 sched_iopoll(struct iopoll_ctx *io_ctx)
288 ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_netmsg);
292 sched_iopollmore(struct iopoll_ctx *io_ctx)
294 ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_more_netmsg);
298 ifpoll_time_get(union ifpoll_time *t)
306 /* Return time diff in us */
308 ifpoll_time_diff(const union ifpoll_time *s, const union ifpoll_time *e)
311 return (((e->tsc - s->tsc) * 1000000) / tsc_frequency);
313 return ((e->tv.tv_usec - s->tv.tv_usec) +
314 (e->tv.tv_sec - s->tv.tv_sec) * 1000000);
319 * Initialize per-cpu qpolling(4) context. Called from kern_clock.c:
322 ifpoll_init_pcpu(int cpuid)
327 poll_comm_init(cpuid);
333 poll_comm_start(cpuid);
337 ifpoll_register(struct ifnet *ifp)
339 struct ifpoll_info *info;
340 struct netmsg_base nmsg;
343 if (ifp->if_npoll == NULL) {
344 /* Device does not support polling */
348 info = kmalloc(sizeof(*info), M_TEMP, M_WAITOK | M_ZERO);
351 * Attempt to register. Interlock with IFF_NPOLLING.
354 ifnet_serialize_all(ifp);
356 if (ifp->if_flags & IFF_NPOLLING) {
357 /* Already polling */
358 ifnet_deserialize_all(ifp);
363 info->ifpi_ifp = ifp;
365 ifp->if_flags |= IFF_NPOLLING;
366 ifp->if_npoll(ifp, info);
367 KASSERT(ifp->if_npoll_cpuid >= 0, ("invalid npoll cpuid"));
369 ifnet_deserialize_all(ifp);
371 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
372 0, ifpoll_register_handler);
373 nmsg.lmsg.u.ms_resultp = info;
375 error = lwkt_domsg(netisr_portfn(0), &nmsg.lmsg, 0);
377 if (!ifpoll_deregister(ifp)) {
378 if_printf(ifp, "ifpoll_register: "
379 "ifpoll_deregister failed!\n");
388 ifpoll_deregister(struct ifnet *ifp)
390 struct netmsg_base nmsg;
393 if (ifp->if_npoll == NULL)
396 ifnet_serialize_all(ifp);
398 if ((ifp->if_flags & IFF_NPOLLING) == 0) {
399 ifnet_deserialize_all(ifp);
402 ifp->if_flags &= ~IFF_NPOLLING;
404 ifnet_deserialize_all(ifp);
406 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
407 0, ifpoll_deregister_handler);
408 nmsg.lmsg.u.ms_resultp = ifp;
410 error = lwkt_domsg(netisr_portfn(0), &nmsg.lmsg, 0);
412 ifnet_serialize_all(ifp);
413 ifp->if_npoll(ifp, NULL);
414 KASSERT(ifp->if_npoll_cpuid < 0, ("invalid npoll cpuid"));
415 ifnet_deserialize_all(ifp);
421 ifpoll_register_handler(netmsg_t nmsg)
423 const struct ifpoll_info *info = nmsg->lmsg.u.ms_resultp;
424 int cpuid = mycpuid, nextcpu;
427 KKASSERT(cpuid < ncpus2);
428 KKASSERT(&curthread->td_msgport == netisr_portfn(cpuid));
431 error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
436 error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
437 &info->ifpi_rx[cpuid]);
441 error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
442 &info->ifpi_tx[cpuid]);
446 /* Adjust polling frequency, after all registration is done */
447 poll_comm_adjust_pollhz(poll_common[cpuid]);
450 if (nextcpu < ncpus2)
451 lwkt_forwardmsg(netisr_portfn(nextcpu), &nmsg->lmsg);
453 lwkt_replymsg(&nmsg->lmsg, 0);
456 lwkt_replymsg(&nmsg->lmsg, error);
460 ifpoll_deregister_handler(netmsg_t nmsg)
462 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
463 int cpuid = mycpuid, nextcpu;
465 KKASSERT(cpuid < ncpus2);
466 KKASSERT(&curthread->td_msgport == netisr_portfn(cpuid));
470 stpoll_deregister(ifp);
471 iopoll_deregister(ifp, rxpoll_context[cpuid]);
472 iopoll_deregister(ifp, txpoll_context[cpuid]);
474 /* Adjust polling frequency, after all deregistration is done */
475 poll_comm_adjust_pollhz(poll_common[cpuid]);
478 if (nextcpu < ncpus2)
479 lwkt_forwardmsg(netisr_portfn(nextcpu), &nmsg->lmsg);
481 lwkt_replymsg(&nmsg->lmsg, 0);
487 struct stpoll_ctx *st_ctx = &stpoll_context;
488 const struct poll_comm *comm = poll_common[0];
490 st_ctx->pollhz = comm->pollhz / (comm->poll_stfrac + 1);
492 sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
493 st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
494 SYSCTL_CHILDREN(comm->sysctl_tree),
495 OID_AUTO, "status", CTLFLAG_RD, 0, "");
497 SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
498 SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
499 OID_AUTO, "handlers", CTLFLAG_RD,
500 &st_ctx->poll_handlers, 0,
501 "Number of registered status poll handlers");
503 netmsg_init(&st_ctx->poll_netmsg, NULL, &netisr_adone_rport,
508 * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
509 * once per polling systimer tick.
512 stpoll_handler(netmsg_t msg)
514 struct stpoll_ctx *st_ctx = &stpoll_context;
515 struct thread *td = curthread;
518 KKASSERT(&td->td_msgport == netisr_portfn(0));
520 crit_enter_quick(td);
523 lwkt_replymsg(&msg->lmsg, 0);
525 if (st_ctx->poll_handlers == 0) {
530 for (i = 0; i < st_ctx->poll_handlers; ++i) {
531 const struct stpoll_rec *rec = &st_ctx->pr[i];
532 struct ifnet *ifp = rec->ifp;
534 if (!lwkt_serialize_try(rec->serializer))
537 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
538 (IFF_RUNNING | IFF_NPOLLING))
539 rec->status_func(ifp, st_ctx->pollhz);
541 lwkt_serialize_exit(rec->serializer);
548 * Hook from status poll systimer. Tries to schedule an status poll.
549 * NOTE: Caller should hold critical section.
552 stpoll_clock(struct stpoll_ctx *st_ctx)
554 KKASSERT(mycpuid == 0);
556 if (st_ctx->poll_handlers == 0)
558 sched_stpoll(st_ctx);
562 stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
564 struct stpoll_ctx *st_ctx = &stpoll_context;
567 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
569 if (st_rec->status_func == NULL)
573 * Check if there is room.
575 if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
577 * List full, cannot register more entries.
578 * This should never happen; if it does, it is probably a
579 * broken driver trying to register multiple times. Checking
580 * this at runtime is expensive, and won't solve the problem
581 * anyways, so just report a few times and then give up.
583 static int verbose = 10; /* XXX */
586 kprintf("status poll handlers list full, "
587 "maybe a broken driver ?\n");
592 struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
595 rec->serializer = st_rec->serializer;
596 rec->status_func = st_rec->status_func;
598 st_ctx->poll_handlers++;
605 stpoll_deregister(struct ifnet *ifp)
607 struct stpoll_ctx *st_ctx = &stpoll_context;
610 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
612 for (i = 0; i < st_ctx->poll_handlers; ++i) {
613 if (st_ctx->pr[i].ifp == ifp) /* Found it */
616 if (i == st_ctx->poll_handlers) {
617 kprintf("stpoll_deregister: ifp not found!!!\n");
620 st_ctx->poll_handlers--;
621 if (i < st_ctx->poll_handlers) {
622 /* Last entry replaces this one. */
623 st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
631 iopoll_reset_state(struct iopoll_ctx *io_ctx)
634 io_ctx->poll_burst = 5;
635 io_ctx->pending_polls = 0;
636 io_ctx->residual_burst = 0;
638 io_ctx->kern_frac = 0;
639 bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
640 bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
645 iopoll_init(int cpuid)
647 KKASSERT(cpuid < ncpus2);
649 rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
650 txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
653 static struct iopoll_ctx *
654 iopoll_ctx_create(int cpuid, int poll_type)
656 struct poll_comm *comm;
657 struct iopoll_ctx *io_ctx;
658 const char *poll_type_str;
660 KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
663 * Make sure that tunables are in sane state
665 if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
666 iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
667 else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
668 iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
670 if (iopoll_each_burst > iopoll_burst_max)
671 iopoll_each_burst = iopoll_burst_max;
673 comm = poll_common[cpuid];
676 * Create the per-cpu polling context
678 io_ctx = kmalloc(sizeof(*io_ctx), M_DEVBUF, M_WAITOK | M_ZERO);
680 io_ctx->poll_each_burst = iopoll_each_burst;
681 io_ctx->poll_burst_max = iopoll_burst_max;
682 io_ctx->user_frac = 50;
683 if (poll_type == IFPOLL_RX)
684 io_ctx->pollhz = comm->pollhz;
686 io_ctx->pollhz = comm->pollhz / (comm->poll_txfrac + 1);
687 io_ctx->poll_cpuid = cpuid;
688 iopoll_reset_state(io_ctx);
690 netmsg_init(&io_ctx->poll_netmsg, NULL, &netisr_adone_rport,
692 io_ctx->poll_netmsg.lmsg.u.ms_resultp = io_ctx;
694 netmsg_init(&io_ctx->poll_more_netmsg, NULL, &netisr_adone_rport,
695 0, iopollmore_handler);
696 io_ctx->poll_more_netmsg.lmsg.u.ms_resultp = io_ctx;
699 * Initialize per-cpu sysctl nodes
701 if (poll_type == IFPOLL_RX)
702 poll_type_str = "rx";
704 poll_type_str = "tx";
706 sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
707 io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
708 SYSCTL_CHILDREN(comm->sysctl_tree),
709 OID_AUTO, poll_type_str, CTLFLAG_RD, 0, "");
710 iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
711 SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx);
717 * Hook from iopoll systimer. Tries to schedule an iopoll, but keeps
718 * track of lost ticks due to the previous handler taking too long.
719 * Normally, this should not happen, because polling handler should
720 * run for a short time. However, in some cases (e.g. when there are
721 * changes in link status etc.) the drivers take a very long time
722 * (even in the order of milliseconds) to reset and reconfigure the
723 * device, causing apparent lost polls.
725 * The first part of the code is just for debugging purposes, and tries
726 * to count how often hardclock ticks are shorter than they should,
727 * meaning either stray interrupts or delayed events.
729 * WARNING! called from fastint or IPI, the MP lock might not be held.
730 * NOTE: Caller should hold critical section.
733 iopoll_clock(struct iopoll_ctx *io_ctx)
738 KKASSERT(mycpuid == io_ctx->poll_cpuid);
740 if (io_ctx->poll_handlers == 0)
744 delta = ifpoll_time_diff(&io_ctx->prev_t, &t);
745 if (delta * io_ctx->pollhz < 500000)
746 io_ctx->short_ticks++;
750 if (io_ctx->pending_polls > 100) {
752 * Too much, assume it has stalled (not always true
753 * see comment above).
756 io_ctx->pending_polls = 0;
760 if (io_ctx->phase <= 2) {
761 if (io_ctx->phase != 0)
764 sched_iopoll(io_ctx);
767 if (io_ctx->pending_polls++ > 0)
768 io_ctx->lost_polls++;
772 * iopoll_handler is scheduled by sched_iopoll when appropriate, typically
773 * once per polling systimer tick.
775 * Note that the message is replied immediately in order to allow a new
776 * ISR to be scheduled in the handler.
779 iopoll_handler(netmsg_t msg)
781 struct iopoll_ctx *io_ctx;
782 struct thread *td = curthread;
785 io_ctx = msg->lmsg.u.ms_resultp;
786 KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
788 crit_enter_quick(td);
791 lwkt_replymsg(&msg->lmsg, 0);
793 if (io_ctx->poll_handlers == 0) {
799 if (io_ctx->residual_burst == 0) {
800 /* First call in this tick */
801 ifpoll_time_get(&io_ctx->poll_start_t);
802 io_ctx->residual_burst = io_ctx->poll_burst;
804 cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
805 io_ctx->residual_burst : io_ctx->poll_each_burst;
806 io_ctx->residual_burst -= cycles;
808 for (i = 0; i < io_ctx->poll_handlers; i++) {
809 const struct iopoll_rec *rec = &io_ctx->pr[i];
810 struct ifnet *ifp = rec->ifp;
812 if (!lwkt_serialize_try(rec->serializer))
815 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
816 (IFF_RUNNING | IFF_NPOLLING))
817 rec->poll_func(ifp, rec->arg, cycles);
819 lwkt_serialize_exit(rec->serializer);
823 * Do a quick exit/enter to catch any higher-priority
827 crit_enter_quick(td);
829 sched_iopollmore(io_ctx);
836 * iopollmore_handler is called after other netisr's, possibly scheduling
837 * another iopoll_handler call, or adapting the burst size for the next cycle.
839 * It is very bad to fetch large bursts of packets from a single card at once,
840 * because the burst could take a long time to be completely processed leading
841 * to unfairness. To reduce the problem, and also to account better for time
842 * spent in network-related processing, we split the burst in smaller chunks
843 * of fixed size, giving control to the other netisr's between chunks. This
844 * helps in improving the fairness, reducing livelock and accounting for the
845 * work performed in low level handling.
848 iopollmore_handler(netmsg_t msg)
850 struct thread *td = curthread;
851 struct iopoll_ctx *io_ctx;
854 uint32_t pending_polls;
856 io_ctx = msg->lmsg.u.ms_resultp;
857 KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
859 crit_enter_quick(td);
862 lwkt_replymsg(&msg->lmsg, 0);
864 if (io_ctx->poll_handlers == 0) {
870 if (io_ctx->residual_burst > 0) {
871 sched_iopoll(io_ctx);
873 /* Will run immediately on return, followed by netisrs */
877 /* Here we can account time spent in iopoll's in this tick */
879 kern_load = ifpoll_time_diff(&io_ctx->poll_start_t, &t);
880 kern_load = (kern_load * io_ctx->pollhz) / 10000; /* 0..100 */
881 io_ctx->kern_frac = kern_load;
883 if (kern_load > (100 - io_ctx->user_frac)) {
884 /* Try decrease ticks */
885 if (io_ctx->poll_burst > 1)
886 io_ctx->poll_burst--;
888 if (io_ctx->poll_burst < io_ctx->poll_burst_max)
889 io_ctx->poll_burst++;
892 io_ctx->pending_polls--;
893 pending_polls = io_ctx->pending_polls;
895 if (pending_polls == 0) {
900 * Last cycle was long and caused us to miss one or more
901 * hardclock ticks. Restart processing again, but slightly
902 * reduce the burst size to prevent that this happens again.
904 io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
905 if (io_ctx->poll_burst < 1)
906 io_ctx->poll_burst = 1;
907 sched_iopoll(io_ctx);
915 iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
916 struct iopoll_ctx *io_ctx)
918 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
919 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
920 "IU", "Max Polling burst size");
922 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
923 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
924 "IU", "Max size of each burst");
926 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
927 &io_ctx->phase, 0, "Polling phase");
929 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
930 &io_ctx->suspect, 0, "suspect event");
932 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
933 &io_ctx->stalled, 0, "potential stalls");
935 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
936 &io_ctx->poll_burst, 0, "Current polling burst size");
938 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
939 &io_ctx->user_frac, 0,
940 "Desired user fraction of cpu time");
942 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "kern_frac", CTLFLAG_RD,
943 &io_ctx->kern_frac, 0,
944 "Kernel fraction of cpu time");
946 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
947 &io_ctx->short_ticks, 0,
948 "Hardclock ticks shorter than they should be");
950 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
951 &io_ctx->lost_polls, 0,
952 "How many times we would have lost a poll tick");
954 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
955 &io_ctx->pending_polls, 0, "Do we need to poll again");
957 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
958 &io_ctx->residual_burst, 0,
959 "# of residual cycles in burst");
961 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
962 &io_ctx->poll_handlers, 0,
963 "Number of registered poll handlers");
967 sysctl_burstmax_handler(netmsg_t nmsg)
969 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
970 struct iopoll_ctx *io_ctx;
973 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
975 io_ctx->poll_burst_max = nmsg->lmsg.u.ms_result;
976 if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
977 io_ctx->poll_each_burst = io_ctx->poll_burst_max;
978 if (io_ctx->poll_burst > io_ctx->poll_burst_max)
979 io_ctx->poll_burst = io_ctx->poll_burst_max;
980 if (io_ctx->residual_burst > io_ctx->poll_burst_max)
981 io_ctx->residual_burst = io_ctx->poll_burst_max;
983 lwkt_replymsg(&nmsg->lmsg, 0);
987 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
989 struct iopoll_ctx *io_ctx = arg1;
990 struct iopoll_sysctl_netmsg msg;
994 burst_max = io_ctx->poll_burst_max;
995 error = sysctl_handle_int(oidp, &burst_max, 0, req);
996 if (error || req->newptr == NULL)
998 if (burst_max < MIN_IOPOLL_BURST_MAX)
999 burst_max = MIN_IOPOLL_BURST_MAX;
1000 else if (burst_max > MAX_IOPOLL_BURST_MAX)
1001 burst_max = MAX_IOPOLL_BURST_MAX;
1003 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1004 0, sysctl_burstmax_handler);
1005 msg.base.lmsg.u.ms_result = burst_max;
1008 return lwkt_domsg(netisr_portfn(io_ctx->poll_cpuid), &msg.base.lmsg, 0);
1012 sysctl_eachburst_handler(netmsg_t nmsg)
1014 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1015 struct iopoll_ctx *io_ctx;
1016 uint32_t each_burst;
1019 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1021 each_burst = nmsg->lmsg.u.ms_result;
1022 if (each_burst > io_ctx->poll_burst_max)
1023 each_burst = io_ctx->poll_burst_max;
1024 else if (each_burst < 1)
1026 io_ctx->poll_each_burst = each_burst;
1028 lwkt_replymsg(&nmsg->lmsg, 0);
1032 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1034 struct iopoll_ctx *io_ctx = arg1;
1035 struct iopoll_sysctl_netmsg msg;
1036 uint32_t each_burst;
1039 each_burst = io_ctx->poll_each_burst;
1040 error = sysctl_handle_int(oidp, &each_burst, 0, req);
1041 if (error || req->newptr == NULL)
1044 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1045 0, sysctl_eachburst_handler);
1046 msg.base.lmsg.u.ms_result = each_burst;
1049 return lwkt_domsg(netisr_portfn(io_ctx->poll_cpuid), &msg.base.lmsg, 0);
1053 iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1054 const struct ifpoll_io *io_rec)
1058 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1060 if (io_rec->poll_func == NULL)
1064 * Check if there is room.
1066 if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1068 * List full, cannot register more entries.
1069 * This should never happen; if it does, it is probably a
1070 * broken driver trying to register multiple times. Checking
1071 * this at runtime is expensive, and won't solve the problem
1072 * anyways, so just report a few times and then give up.
1074 static int verbose = 10; /* XXX */
1076 kprintf("io poll handlers list full, "
1077 "maybe a broken driver ?\n");
1082 struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1085 rec->serializer = io_rec->serializer;
1086 rec->arg = io_rec->arg;
1087 rec->poll_func = io_rec->poll_func;
1089 io_ctx->poll_handlers++;
1096 iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1100 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1102 for (i = 0; i < io_ctx->poll_handlers; ++i) {
1103 if (io_ctx->pr[i].ifp == ifp) /* Found it */
1106 if (i == io_ctx->poll_handlers) {
1109 io_ctx->poll_handlers--;
1110 if (i < io_ctx->poll_handlers) {
1111 /* Last entry replaces this one. */
1112 io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1115 if (io_ctx->poll_handlers == 0)
1116 iopoll_reset_state(io_ctx);
1123 poll_comm_init(int cpuid)
1125 struct poll_comm *comm;
1128 comm = kmalloc(sizeof(*comm), M_DEVBUF, M_WAITOK | M_ZERO);
1130 if (ifpoll_stfrac < 0)
1131 ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
1132 if (ifpoll_txfrac < 0)
1133 ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
1135 comm->pollhz = ifpoll_pollhz;
1136 comm->poll_cpuid = cpuid;
1137 comm->poll_stfrac = ifpoll_stfrac;
1138 comm->poll_txfrac = ifpoll_txfrac;
1140 ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", cpuid);
1142 sysctl_ctx_init(&comm->sysctl_ctx);
1143 comm->sysctl_tree = SYSCTL_ADD_NODE(&comm->sysctl_ctx,
1144 SYSCTL_STATIC_CHILDREN(_net_ifpoll),
1145 OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
1147 SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1148 OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
1149 comm, 0, sysctl_pollhz,
1150 "I", "Device polling frequency");
1153 SYSCTL_ADD_PROC(&comm->sysctl_ctx,
1154 SYSCTL_CHILDREN(comm->sysctl_tree),
1155 OID_AUTO, "status_frac",
1156 CTLTYPE_INT | CTLFLAG_RW,
1157 comm, 0, sysctl_stfrac,
1158 "I", "# of cycles before status is polled");
1160 SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1161 OID_AUTO, "tx_frac", CTLTYPE_INT | CTLFLAG_RW,
1162 comm, 0, sysctl_txfrac,
1163 "I", "# of cycles before TX is polled");
1165 poll_common[cpuid] = comm;
1169 poll_comm_start(int cpuid)
1171 struct poll_comm *comm = poll_common[cpuid];
1172 systimer_func_t func;
1175 * Initialize systimer
1178 func = poll_comm_systimer0;
1180 func = poll_comm_systimer;
1181 systimer_init_periodic_nq(&comm->pollclock, func, comm, 1);
1185 _poll_comm_systimer(struct poll_comm *comm)
1187 if (comm->txfrac_count-- == 0) {
1188 comm->txfrac_count = comm->poll_txfrac;
1189 iopoll_clock(txpoll_context[comm->poll_cpuid]);
1191 iopoll_clock(rxpoll_context[comm->poll_cpuid]);
1195 poll_comm_systimer0(systimer_t info, int in_ipi __unused,
1196 struct intrframe *frame __unused)
1198 struct poll_comm *comm = info->data;
1199 globaldata_t gd = mycpu;
1201 KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid == 0);
1205 if (comm->stfrac_count-- == 0) {
1206 comm->stfrac_count = comm->poll_stfrac;
1207 stpoll_clock(&stpoll_context);
1209 _poll_comm_systimer(comm);
1215 poll_comm_systimer(systimer_t info, int in_ipi __unused,
1216 struct intrframe *frame __unused)
1218 struct poll_comm *comm = info->data;
1219 globaldata_t gd = mycpu;
1221 KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid != 0);
1224 _poll_comm_systimer(comm);
1229 poll_comm_adjust_pollhz(struct poll_comm *comm)
1234 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1237 * If there is no polling handler registered, set systimer
1238 * frequency to the lowest value. Polling systimer frequency
1239 * will be adjusted to the requested value, once there are
1240 * registered handlers.
1242 handlers = rxpoll_context[mycpuid]->poll_handlers +
1243 txpoll_context[mycpuid]->poll_handlers;
1244 if (comm->poll_cpuid == 0)
1245 handlers += stpoll_context.poll_handlers;
1247 pollhz = comm->pollhz;
1248 systimer_adjust_periodic(&comm->pollclock, pollhz);
1252 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
1254 struct poll_comm *comm = arg1;
1255 struct netmsg_base nmsg;
1259 error = sysctl_handle_int(oidp, &phz, 0, req);
1260 if (error || req->newptr == NULL)
1264 else if (phz > IFPOLL_FREQ_MAX)
1265 phz = IFPOLL_FREQ_MAX;
1267 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1268 0, sysctl_pollhz_handler);
1269 nmsg.lmsg.u.ms_result = phz;
1271 return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1275 sysctl_pollhz_handler(netmsg_t nmsg)
1277 struct poll_comm *comm = poll_common[mycpuid];
1279 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1281 /* Save polling frequency */
1282 comm->pollhz = nmsg->lmsg.u.ms_result;
1285 * Adjust cached pollhz
1287 rxpoll_context[mycpuid]->pollhz = comm->pollhz;
1288 txpoll_context[mycpuid]->pollhz =
1289 comm->pollhz / (comm->poll_txfrac + 1);
1291 stpoll_context.pollhz = comm->pollhz / (comm->poll_stfrac + 1);
1294 * Adjust polling frequency
1296 poll_comm_adjust_pollhz(comm);
1298 lwkt_replymsg(&nmsg->lmsg, 0);
1302 sysctl_stfrac(SYSCTL_HANDLER_ARGS)
1304 struct poll_comm *comm = arg1;
1305 struct netmsg_base nmsg;
1308 KKASSERT(comm->poll_cpuid == 0);
1310 stfrac = comm->poll_stfrac;
1311 error = sysctl_handle_int(oidp, &stfrac, 0, req);
1312 if (error || req->newptr == NULL)
1317 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1318 0, sysctl_stfrac_handler);
1319 nmsg.lmsg.u.ms_result = stfrac;
1321 return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1325 sysctl_stfrac_handler(netmsg_t nmsg)
1327 struct poll_comm *comm = poll_common[mycpuid];
1328 int stfrac = nmsg->lmsg.u.ms_result;
1330 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1333 comm->poll_stfrac = stfrac;
1334 if (comm->stfrac_count > comm->poll_stfrac)
1335 comm->stfrac_count = comm->poll_stfrac;
1338 lwkt_replymsg(&nmsg->lmsg, 0);
1342 sysctl_txfrac(SYSCTL_HANDLER_ARGS)
1344 struct poll_comm *comm = arg1;
1345 struct netmsg_base nmsg;
1348 txfrac = comm->poll_txfrac;
1349 error = sysctl_handle_int(oidp, &txfrac, 0, req);
1350 if (error || req->newptr == NULL)
1355 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1356 0, sysctl_txfrac_handler);
1357 nmsg.lmsg.u.ms_result = txfrac;
1359 return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1363 sysctl_txfrac_handler(netmsg_t nmsg)
1365 struct poll_comm *comm = poll_common[mycpuid];
1366 int txfrac = nmsg->lmsg.u.ms_result;
1368 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1371 comm->poll_txfrac = txfrac;
1372 if (comm->txfrac_count > comm->poll_txfrac)
1373 comm->txfrac_count = comm->poll_txfrac;
1376 lwkt_replymsg(&nmsg->lmsg, 0);