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/smp.h>
47 #include <net/if_poll.h>
48 #include <net/netmsg2.h>
51 * Polling support for network device drivers.
53 * Drivers which support this feature try to register one status polling
54 * handler and several TX/RX polling handlers with the polling code.
55 * If interface's if_qpoll is called with non-NULL second argument, then
56 * a register operation is requested, else a deregister operation is
57 * requested. If the requested operation is "register", driver should
58 * setup the ifpoll_info passed in accoding its own needs:
59 * ifpoll_info.ifpi_status.status_func == NULL
60 * No status polling handler will be installed on CPU(0)
61 * ifpoll_info.ifpi_rx[n].poll_func == NULL
62 * No RX polling handler will be installed on CPU(n)
63 * ifpoll_info.ifpi_tx[n].poll_func == NULL
64 * No TX polling handler will be installed on CPU(n)
66 * All of the registered polling handlers are called only if the interface
67 * is marked as 'IFF_RUNNING and IFF_NPOLLING'. However, the interface's
68 * register and deregister function (ifnet.if_qpoll) will be called even
69 * if interface is not marked with 'IFF_RUNNING'.
71 * If registration is successful, the driver must disable interrupts,
72 * and further I/O is performed through the TX/RX polling handler, which
73 * are invoked (at least once per clock tick) with 3 arguments: the "arg"
74 * passed at register time, a struct ifnet pointer, and a "count" limit.
75 * The registered serializer will be held before calling the related
78 * The count limit specifies how much work the handler can do during the
79 * call -- typically this is the number of packets to be received, or
80 * transmitted, etc. (drivers are free to interpret this number, as long
81 * as the max time spent in the function grows roughly linearly with the
84 * A second variable controls the sharing of CPU between polling/kernel
85 * network processing, and other activities (typically userlevel tasks):
86 * net.ifpoll.{rxX,txX}.user_frac (between 0 and 100, default 50) sets the
87 * share of CPU allocated to user tasks. CPU is allocated proportionally
88 * to the shares, by dynamically adjusting the "count" (poll_burst).
90 * Other parameters can should be left to their default values.
91 * The following constraints hold
93 * 1 <= poll_burst <= poll_burst_max
94 * 1 <= poll_each_burst <= poll_burst_max
95 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
98 #define IFPOLL_LIST_LEN 128
99 #define IFPOLL_FREQ_MAX 30000
101 #define MIN_IOPOLL_BURST_MAX 10
102 #define MAX_IOPOLL_BURST_MAX 1000
103 #define IOPOLL_BURST_MAX 150 /* good for 100Mbit net and HZ=1000 */
105 #define IOPOLL_EACH_BURST 5
107 #define IFPOLL_FREQ_DEFAULT 2000
108 #define IOPOLL_FREQ_DEFAULT IFPOLL_FREQ_DEFAULT
109 #define STPOLL_FREQ_DEFAULT 100
111 #define IFPOLL_TXFRAC_DEFAULT 1
112 #define IFPOLL_STFRAC_DEFAULT 20
114 #define IFPOLL_RX 0x1
115 #define IFPOLL_TX 0x2
118 struct lwkt_serialize *serializer;
121 ifpoll_iofn_t poll_func;
125 #ifdef IFPOLL_MULTI_SYSTIMER
126 struct systimer pollclock;
129 struct timeval prev_t; /* state */
130 uint32_t short_ticks; /* statistics */
131 uint32_t lost_polls; /* statistics */
132 uint32_t suspect; /* statistics */
133 uint32_t stalled; /* statistics */
134 uint32_t pending_polls; /* state */
136 struct netmsg poll_netmsg;
139 #ifdef IFPOLL_MULTI_SYSTIMER
140 int pollhz; /* tunable */
142 int poll_type; /* IFPOLL_{RX,TX} */
144 uint32_t phase; /* state */
145 int residual_burst; /* state */
146 uint32_t poll_each_burst; /* tunable */
147 struct timeval poll_start_t; /* state */
149 uint32_t poll_handlers; /* next free entry in pr[]. */
150 struct iopoll_rec pr[IFPOLL_LIST_LEN];
152 struct netmsg poll_more_netmsg;
154 uint32_t poll_burst; /* state */
155 uint32_t poll_burst_max; /* tunable */
156 uint32_t user_frac; /* tunable */
158 struct sysctl_ctx_list poll_sysctl_ctx;
159 struct sysctl_oid *poll_sysctl_tree;
163 struct lwkt_serialize *serializer;
165 ifpoll_stfn_t status_func;
169 #ifdef IFPOLL_MULTI_SYSTIMER
170 struct systimer pollclock;
173 struct netmsg poll_netmsg;
175 #ifdef IFPOLL_MULTI_SYSTIMER
176 int pollhz; /* tunable */
178 uint32_t poll_handlers; /* next free entry in pr[]. */
179 struct stpoll_rec pr[IFPOLL_LIST_LEN];
181 struct sysctl_ctx_list poll_sysctl_ctx;
182 struct sysctl_oid *poll_sysctl_tree;
185 struct iopoll_sysctl_netmsg {
187 struct iopoll_ctx *ctx;
190 #ifndef IFPOLL_MULTI_SYSTIMER
193 struct systimer clock;
202 static struct stpoll_ctx stpoll_context;
203 static struct iopoll_ctx *rxpoll_context[IFPOLL_CTX_MAX];
204 static struct iopoll_ctx *txpoll_context[IFPOLL_CTX_MAX];
206 SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
207 "Network device polling parameters");
209 static int ifpoll_ncpus = IFPOLL_CTX_MAX;
211 static int iopoll_burst_max = IOPOLL_BURST_MAX;
212 static int iopoll_each_burst = IOPOLL_EACH_BURST;
214 TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
215 TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
217 #ifdef IFPOLL_MULTI_SYSTIMER
219 static int stpoll_hz = STPOLL_FREQ_DEFAULT;
220 static int iopoll_hz = IOPOLL_FREQ_DEFAULT;
222 TUNABLE_INT("net.ifpoll.stpoll_hz", &stpoll_hz);
223 TUNABLE_INT("net.ifpoll.iopoll_hz", &iopoll_hz);
225 #else /* !IFPOLL_MULTI_SYSTIMER */
227 static struct ifpoll_data ifpoll0;
228 static int ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
229 static int ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
230 static int ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
231 static int ifpoll_handlers;
233 TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
234 TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
235 TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
237 static void sysctl_ifpollhz_handler(struct netmsg *);
238 static int sysctl_ifpollhz(SYSCTL_HANDLER_ARGS);
240 SYSCTL_PROC(_net_ifpoll, OID_AUTO, pollhz, CTLTYPE_INT | CTLFLAG_RW,
241 0, 0, sysctl_ifpollhz, "I", "Polling frequency");
242 SYSCTL_INT(_net_ifpoll, OID_AUTO, tx_frac, CTLFLAG_RW,
243 &ifpoll_txfrac, 0, "Every this many cycles poll transmit");
244 SYSCTL_INT(_net_ifpoll, OID_AUTO, st_frac, CTLFLAG_RW,
245 &ifpoll_stfrac, 0, "Every this many cycles poll status");
247 #endif /* IFPOLL_MULTI_SYSTIMER */
249 void ifpoll_init_pcpu(int);
251 #ifndef IFPOLL_MULTI_SYSTIMER
252 static void ifpoll_start_handler(struct netmsg *);
253 static void ifpoll_stop_handler(struct netmsg *);
254 static void ifpoll_handler_addevent(void);
255 static void ifpoll_handler_delevent(void);
256 static void ifpoll_ipi_handler(void *, int);
257 static void ifpoll_systimer(systimer_t, struct intrframe *);
260 static void ifpoll_register_handler(struct netmsg *);
261 static void ifpoll_deregister_handler(struct netmsg *);
266 static void stpoll_init(void);
267 static void stpoll_handler(struct netmsg *);
268 static void stpoll_clock(struct stpoll_ctx *);
269 #ifdef IFPOLL_MULTI_SYSTIMER
270 static void stpoll_systimer(systimer_t, struct intrframe *);
272 static int stpoll_register(struct ifnet *, const struct ifpoll_status *);
273 static int stpoll_deregister(struct ifnet *);
275 #ifdef IFPOLL_MULTI_SYSTIMER
276 static void sysctl_stpollhz_handler(struct netmsg *);
277 static int sysctl_stpollhz(SYSCTL_HANDLER_ARGS);
283 static struct iopoll_ctx *iopoll_ctx_create(int, int);
284 static void iopoll_init(int);
285 static void iopoll_handler(struct netmsg *);
286 static void iopollmore_handler(struct netmsg *);
287 static void iopoll_clock(struct iopoll_ctx *);
288 #ifdef IFPOLL_MULTI_SYSTIMER
289 static void iopoll_systimer(systimer_t, struct intrframe *);
291 static int iopoll_register(struct ifnet *, struct iopoll_ctx *,
292 const struct ifpoll_io *);
293 static int iopoll_deregister(struct ifnet *, struct iopoll_ctx *);
295 static void iopoll_add_sysctl(struct sysctl_ctx_list *,
296 struct sysctl_oid_list *, struct iopoll_ctx *);
297 #ifdef IFPOLL_MULTI_SYSTIMER
298 static void sysctl_iopollhz_handler(struct netmsg *);
299 static int sysctl_iopollhz(SYSCTL_HANDLER_ARGS);
301 static void sysctl_burstmax_handler(struct netmsg *);
302 static int sysctl_burstmax(SYSCTL_HANDLER_ARGS);
303 static void sysctl_eachburst_handler(struct netmsg *);
304 static int sysctl_eachburst(SYSCTL_HANDLER_ARGS);
307 ifpoll_sendmsg_oncpu(struct netmsg *msg)
309 if (msg->nm_lmsg.ms_flags & MSGF_DONE)
310 ifnet_sendmsg(&msg->nm_lmsg, mycpuid);
314 sched_stpoll(struct stpoll_ctx *st_ctx)
316 ifpoll_sendmsg_oncpu(&st_ctx->poll_netmsg);
320 sched_iopoll(struct iopoll_ctx *io_ctx)
322 ifpoll_sendmsg_oncpu(&io_ctx->poll_netmsg);
326 sched_iopollmore(struct iopoll_ctx *io_ctx)
328 ifpoll_sendmsg_oncpu(&io_ctx->poll_more_netmsg);
332 * Initialize per-cpu qpolling(4) context. Called from kern_clock.c:
335 ifpoll_init_pcpu(int cpuid)
337 if (cpuid >= IFPOLL_CTX_MAX) {
339 } else if (cpuid == 0) {
340 if (ifpoll_ncpus > ncpus)
341 ifpoll_ncpus = ncpus;
342 kprintf("ifpoll_ncpus %d\n", ifpoll_ncpus);
344 #ifndef IFPOLL_MULTI_SYSTIMER
345 systimer_init_periodic_nq(&ifpoll0.clock,
346 ifpoll_systimer, NULL, 1);
354 #ifndef IFPOLL_MULTI_SYSTIMER
357 ifpoll_ipi_handler(void *arg __unused, int poll)
359 KKASSERT(mycpuid < ifpoll_ncpus);
361 if (poll & IFPOLL_TX)
362 iopoll_clock(txpoll_context[mycpuid]);
363 if (poll & IFPOLL_RX)
364 iopoll_clock(rxpoll_context[mycpuid]);
368 ifpoll_systimer(systimer_t info __unused, struct intrframe *frame __unused)
370 uint32_t cpumask = 0;
372 KKASSERT(mycpuid == 0);
374 if (ifpoll0.stfrac_count-- == 0) {
375 ifpoll0.stfrac_count = ifpoll_stfrac;
376 stpoll_clock(&stpoll_context);
379 if (ifpoll0.txfrac_count-- == 0) {
380 ifpoll0.txfrac_count = ifpoll_txfrac;
382 /* TODO: We may try to piggyback TX on RX */
383 cpumask = smp_active_mask & ifpoll0.tx_cpumask;
385 lwkt_send_ipiq2_mask(cpumask, ifpoll_ipi_handler,
390 cpumask = smp_active_mask & ifpoll0.rx_cpumask;
392 lwkt_send_ipiq2_mask(cpumask, ifpoll_ipi_handler,
398 ifpoll_start_handler(struct netmsg *nmsg)
400 KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
402 kprintf("ifpoll: start\n");
403 systimer_adjust_periodic(&ifpoll0.clock, ifpoll_pollhz);
404 lwkt_replymsg(&nmsg->nm_lmsg, 0);
408 ifpoll_stop_handler(struct netmsg *nmsg)
410 KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
412 kprintf("ifpoll: stop\n");
413 systimer_adjust_periodic(&ifpoll0.clock, 1);
414 lwkt_replymsg(&nmsg->nm_lmsg, 0);
418 ifpoll_handler_addevent(void)
420 if (atomic_fetchadd_int(&ifpoll_handlers, 1) == 0) {
424 nmsg = kmalloc(sizeof(*nmsg), M_LWKTMSG, M_WAITOK);
425 netmsg_init(nmsg, &netisr_afree_rport, 0, ifpoll_start_handler);
426 ifnet_sendmsg(&nmsg->nm_lmsg, 0);
431 ifpoll_handler_delevent(void)
433 KKASSERT(ifpoll_handlers > 0);
434 if (atomic_fetchadd_int(&ifpoll_handlers, -1) == 1) {
438 nmsg = kmalloc(sizeof(*nmsg), M_LWKTMSG, M_WAITOK);
439 netmsg_init(nmsg, &netisr_afree_rport, 0, ifpoll_stop_handler);
440 ifnet_sendmsg(&nmsg->nm_lmsg, 0);
445 sysctl_ifpollhz_handler(struct netmsg *nmsg)
447 KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
450 * If there is no handler registered, don't adjust polling
451 * systimer frequency; polling systimer frequency will be
452 * adjusted once there is registered handler.
454 ifpoll_pollhz = nmsg->nm_lmsg.u.ms_result;
456 systimer_adjust_periodic(&ifpoll0.clock, ifpoll_pollhz);
458 lwkt_replymsg(&nmsg->nm_lmsg, 0);
462 sysctl_ifpollhz(SYSCTL_HANDLER_ARGS)
468 error = sysctl_handle_int(oidp, &phz, 0, req);
469 if (error || req->newptr == NULL)
473 else if (phz > IFPOLL_FREQ_MAX)
474 phz = IFPOLL_FREQ_MAX;
476 netmsg_init(&nmsg, &curthread->td_msgport, MSGF_MPSAFE,
477 sysctl_ifpollhz_handler);
478 nmsg.nm_lmsg.u.ms_result = phz;
480 return ifnet_domsg(&nmsg.nm_lmsg, 0);
483 #endif /* !IFPOLL_MULTI_SYSTIMER */
486 ifpoll_register(struct ifnet *ifp)
488 struct ifpoll_info info;
492 if (ifp->if_qpoll == NULL) {
493 /* Device does not support polling */
498 * Attempt to register. Interlock with IFF_NPOLLING.
501 ifnet_serialize_all(ifp);
503 if (ifp->if_flags & IFF_NPOLLING) {
504 /* Already polling */
505 ifnet_deserialize_all(ifp);
509 bzero(&info, sizeof(info));
512 ifp->if_flags |= IFF_NPOLLING;
513 ifp->if_qpoll(ifp, &info);
515 ifnet_deserialize_all(ifp);
517 netmsg_init(&nmsg, &curthread->td_msgport, MSGF_MPSAFE,
518 ifpoll_register_handler);
519 nmsg.nm_lmsg.u.ms_resultp = &info;
521 error = ifnet_domsg(&nmsg.nm_lmsg, 0);
523 if (!ifpoll_deregister(ifp)) {
524 if_printf(ifp, "ifpoll_register: "
525 "ifpoll_deregister failed!\n");
532 ifpoll_deregister(struct ifnet *ifp)
537 if (ifp->if_qpoll == NULL)
540 ifnet_serialize_all(ifp);
542 if ((ifp->if_flags & IFF_NPOLLING) == 0) {
543 ifnet_deserialize_all(ifp);
546 ifp->if_flags &= ~IFF_NPOLLING;
548 ifnet_deserialize_all(ifp);
550 netmsg_init(&nmsg, &curthread->td_msgport, MSGF_MPSAFE,
551 ifpoll_deregister_handler);
552 nmsg.nm_lmsg.u.ms_resultp = ifp;
554 error = ifnet_domsg(&nmsg.nm_lmsg, 0);
556 ifnet_serialize_all(ifp);
557 ifp->if_qpoll(ifp, NULL);
558 ifnet_deserialize_all(ifp);
564 ifpoll_register_handler(struct netmsg *nmsg)
566 const struct ifpoll_info *info = nmsg->nm_lmsg.u.ms_resultp;
567 int cpuid = mycpuid, nextcpu;
570 KKASSERT(cpuid < ifpoll_ncpus);
571 KKASSERT(&curthread->td_msgport == ifnet_portfn(cpuid));
574 error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
579 error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
580 &info->ifpi_rx[cpuid]);
584 error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
585 &info->ifpi_tx[cpuid]);
590 if (nextcpu < ifpoll_ncpus)
591 ifnet_forwardmsg(&nmsg->nm_lmsg, nextcpu);
593 lwkt_replymsg(&nmsg->nm_lmsg, 0);
596 lwkt_replymsg(&nmsg->nm_lmsg, error);
600 ifpoll_deregister_handler(struct netmsg *nmsg)
602 struct ifnet *ifp = nmsg->nm_lmsg.u.ms_resultp;
603 int cpuid = mycpuid, nextcpu;
605 KKASSERT(cpuid < ifpoll_ncpus);
606 KKASSERT(&curthread->td_msgport == ifnet_portfn(cpuid));
610 stpoll_deregister(ifp);
611 iopoll_deregister(ifp, rxpoll_context[cpuid]);
612 iopoll_deregister(ifp, txpoll_context[cpuid]);
615 if (nextcpu < ifpoll_ncpus)
616 ifnet_forwardmsg(&nmsg->nm_lmsg, nextcpu);
618 lwkt_replymsg(&nmsg->nm_lmsg, 0);
624 struct stpoll_ctx *st_ctx = &stpoll_context;
626 #ifdef IFPOLL_MULTI_SYSTIMER
627 st_ctx->pollhz = stpoll_hz;
630 sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
631 st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
632 SYSCTL_STATIC_CHILDREN(_net_ifpoll),
633 OID_AUTO, "status", CTLFLAG_RD, 0, "");
635 #ifdef IFPOLL_MULTI_SYSTIMER
636 SYSCTL_ADD_PROC(&st_ctx->poll_sysctl_ctx,
637 SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
638 OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
639 st_ctx, 0, sysctl_stpollhz, "I",
640 "Status polling frequency");
643 SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
644 SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
645 OID_AUTO, "handlers", CTLFLAG_RD,
646 &st_ctx->poll_handlers, 0,
647 "Number of registered status poll handlers");
649 netmsg_init(&st_ctx->poll_netmsg, &netisr_adone_rport, MSGF_MPSAFE,
652 #ifdef IFPOLL_MULTI_SYSTIMER
653 systimer_init_periodic_nq(&st_ctx->pollclock,
654 stpoll_systimer, st_ctx, 1);
658 #ifdef IFPOLL_MULTI_SYSTIMER
661 sysctl_stpollhz_handler(struct netmsg *msg)
663 struct stpoll_ctx *st_ctx = &stpoll_context;
665 KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
668 * If there is no handler registered, don't adjust polling
669 * systimer frequency; polling systimer frequency will be
670 * adjusted once there is registered handler.
672 st_ctx->pollhz = msg->nm_lmsg.u.ms_result;
673 if (st_ctx->poll_handlers)
674 systimer_adjust_periodic(&st_ctx->pollclock, st_ctx->pollhz);
676 lwkt_replymsg(&msg->nm_lmsg, 0);
680 sysctl_stpollhz(SYSCTL_HANDLER_ARGS)
682 struct stpoll_ctx *st_ctx = arg1;
686 phz = st_ctx->pollhz;
687 error = sysctl_handle_int(oidp, &phz, 0, req);
688 if (error || req->newptr == NULL)
692 else if (phz > IFPOLL_FREQ_MAX)
693 phz = IFPOLL_FREQ_MAX;
695 netmsg_init(&msg, &curthread->td_msgport, MSGF_MPSAFE,
696 sysctl_stpollhz_handler);
697 msg.nm_lmsg.u.ms_result = phz;
699 return ifnet_domsg(&msg.nm_lmsg, 0);
702 #endif /* IFPOLL_MULTI_SYSTIMER */
705 * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
706 * once per polling systimer tick.
709 stpoll_handler(struct netmsg *msg)
711 struct stpoll_ctx *st_ctx = &stpoll_context;
712 struct thread *td = curthread;
715 KKASSERT(&td->td_msgport == ifnet_portfn(0));
717 crit_enter_quick(td);
720 lwkt_replymsg(&msg->nm_lmsg, 0);
722 if (st_ctx->poll_handlers == 0) {
727 #ifdef IFPOLL_MULTI_SYSTIMER
728 poll_hz = st_ctx->pollhz;
730 poll_hz = ifpoll_pollhz / (ifpoll_stfrac + 1);
733 for (i = 0; i < st_ctx->poll_handlers; ++i) {
734 const struct stpoll_rec *rec = &st_ctx->pr[i];
735 struct ifnet *ifp = rec->ifp;
737 if (!lwkt_serialize_try(rec->serializer))
740 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
741 (IFF_RUNNING | IFF_NPOLLING))
742 rec->status_func(ifp, poll_hz);
744 lwkt_serialize_exit(rec->serializer);
751 * Hook from status poll systimer. Tries to schedule an status poll.
754 stpoll_clock(struct stpoll_ctx *st_ctx)
756 KKASSERT(mycpuid == 0);
758 if (st_ctx->poll_handlers == 0)
762 sched_stpoll(st_ctx);
766 #ifdef IFPOLL_MULTI_SYSTIMER
768 stpoll_systimer(systimer_t info, struct intrframe *frame __unused)
770 stpoll_clock(info->data);
775 stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
777 struct stpoll_ctx *st_ctx = &stpoll_context;
780 KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
782 if (st_rec->status_func == NULL)
786 * Check if there is room.
788 if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
790 * List full, cannot register more entries.
791 * This should never happen; if it does, it is probably a
792 * broken driver trying to register multiple times. Checking
793 * this at runtime is expensive, and won't solve the problem
794 * anyways, so just report a few times and then give up.
796 static int verbose = 10; /* XXX */
799 kprintf("status poll handlers list full, "
800 "maybe a broken driver ?\n");
805 struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
808 rec->serializer = st_rec->serializer;
809 rec->status_func = st_rec->status_func;
811 st_ctx->poll_handlers++;
813 #ifdef IFPOLL_MULTI_SYSTIMER
814 if (st_ctx->poll_handlers == 1) {
815 systimer_adjust_periodic(&st_ctx->pollclock,
819 ifpoll_handler_addevent();
827 stpoll_deregister(struct ifnet *ifp)
829 struct stpoll_ctx *st_ctx = &stpoll_context;
832 KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
834 for (i = 0; i < st_ctx->poll_handlers; ++i) {
835 if (st_ctx->pr[i].ifp == ifp) /* Found it */
838 if (i == st_ctx->poll_handlers) {
839 kprintf("stpoll_deregister: ifp not found!!!\n");
842 st_ctx->poll_handlers--;
843 if (i < st_ctx->poll_handlers) {
844 /* Last entry replaces this one. */
845 st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
848 #ifdef IFPOLL_MULTI_SYSTIMER
849 if (st_ctx->poll_handlers == 0)
850 systimer_adjust_periodic(&st_ctx->pollclock, 1);
852 ifpoll_handler_delevent();
859 #ifndef IFPOLL_MULTI_SYSTIMER
861 iopoll_hz(struct iopoll_ctx *io_ctx)
865 poll_hz = ifpoll_pollhz;
866 if (io_ctx->poll_type == IFPOLL_TX)
867 poll_hz /= ifpoll_txfrac + 1;
873 iopoll_reset_state(struct iopoll_ctx *io_ctx)
876 io_ctx->poll_burst = 5;
877 io_ctx->pending_polls = 0;
878 io_ctx->residual_burst = 0;
880 bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
881 bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
886 iopoll_init(int cpuid)
888 KKASSERT(cpuid < IFPOLL_CTX_MAX);
890 rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
891 txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
894 static struct iopoll_ctx *
895 iopoll_ctx_create(int cpuid, int poll_type)
897 struct iopoll_ctx *io_ctx;
898 const char *poll_type_str;
901 KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
904 * Make sure that tunables are in sane state
906 if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
907 iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
908 else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
909 iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
911 if (iopoll_each_burst > iopoll_burst_max)
912 iopoll_each_burst = iopoll_burst_max;
915 * Create the per-cpu polling context
917 io_ctx = kmalloc(sizeof(*io_ctx), M_DEVBUF, M_WAITOK | M_ZERO);
919 io_ctx->poll_each_burst = iopoll_each_burst;
920 io_ctx->poll_burst_max = iopoll_burst_max;
921 io_ctx->user_frac = 50;
922 #ifdef IFPOLL_MULTI_SYSTIMER
923 io_ctx->pollhz = iopoll_hz;
925 io_ctx->poll_type = poll_type;
927 io_ctx->poll_cpuid = cpuid;
928 iopoll_reset_state(io_ctx);
930 netmsg_init(&io_ctx->poll_netmsg, &netisr_adone_rport, MSGF_MPSAFE,
932 io_ctx->poll_netmsg.nm_lmsg.u.ms_resultp = io_ctx;
934 netmsg_init(&io_ctx->poll_more_netmsg, &netisr_adone_rport, MSGF_MPSAFE,
936 io_ctx->poll_more_netmsg.nm_lmsg.u.ms_resultp = io_ctx;
939 * Initialize per-cpu sysctl nodes
941 if (poll_type == IFPOLL_RX)
942 poll_type_str = "rx";
944 poll_type_str = "tx";
945 ksnprintf(cpuid_str, sizeof(cpuid_str), "%s%d",
946 poll_type_str, io_ctx->poll_cpuid);
948 sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
949 io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
950 SYSCTL_STATIC_CHILDREN(_net_ifpoll),
951 OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
952 iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
953 SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx);
955 #ifdef IFPOLL_MULTI_SYSTIMER
957 * Initialize systimer
959 systimer_init_periodic_nq(&io_ctx->pollclock,
960 iopoll_systimer, io_ctx, 1);
967 * Hook from iopoll systimer. Tries to schedule an iopoll, but keeps
968 * track of lost ticks due to the previous handler taking too long.
969 * Normally, this should not happen, because polling handler should
970 * run for a short time. However, in some cases (e.g. when there are
971 * changes in link status etc.) the drivers take a very long time
972 * (even in the order of milliseconds) to reset and reconfigure the
973 * device, causing apparent lost polls.
975 * The first part of the code is just for debugging purposes, and tries
976 * to count how often hardclock ticks are shorter than they should,
977 * meaning either stray interrupts or delayed events.
979 * WARNING! called from fastint or IPI, the MP lock might not be held.
982 iopoll_clock(struct iopoll_ctx *io_ctx)
987 KKASSERT(mycpuid == io_ctx->poll_cpuid);
989 if (io_ctx->poll_handlers == 0)
992 #ifdef IFPOLL_MULTI_SYSTIMER
993 poll_hz = io_ctx->pollhz;
995 poll_hz = iopoll_hz(io_ctx);
999 delta = (t.tv_usec - io_ctx->prev_t.tv_usec) +
1000 (t.tv_sec - io_ctx->prev_t.tv_sec) * 1000000;
1001 if (delta * poll_hz < 500000)
1002 io_ctx->short_ticks++;
1006 if (io_ctx->pending_polls > 100) {
1008 * Too much, assume it has stalled (not always true
1009 * see comment above).
1012 io_ctx->pending_polls = 0;
1016 if (io_ctx->phase <= 2) {
1017 if (io_ctx->phase != 0)
1021 sched_iopoll(io_ctx);
1025 if (io_ctx->pending_polls++ > 0)
1026 io_ctx->lost_polls++;
1029 #ifdef IFPOLL_MULTI_SYSTIMER
1031 iopoll_systimer(systimer_t info, struct intrframe *frame __unused)
1033 iopoll_clock(info->data);
1038 * iopoll_handler is scheduled by sched_iopoll when appropriate, typically
1039 * once per polling systimer tick.
1041 * Note that the message is replied immediately in order to allow a new
1042 * ISR to be scheduled in the handler.
1045 iopoll_handler(struct netmsg *msg)
1047 struct iopoll_ctx *io_ctx;
1048 struct thread *td = curthread;
1051 io_ctx = msg->nm_lmsg.u.ms_resultp;
1052 KKASSERT(&td->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1054 crit_enter_quick(td);
1057 lwkt_replymsg(&msg->nm_lmsg, 0);
1059 if (io_ctx->poll_handlers == 0) {
1060 crit_exit_quick(td);
1065 if (io_ctx->residual_burst == 0) {
1066 /* First call in this tick */
1067 microuptime(&io_ctx->poll_start_t);
1068 io_ctx->residual_burst = io_ctx->poll_burst;
1070 cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
1071 io_ctx->residual_burst : io_ctx->poll_each_burst;
1072 io_ctx->residual_burst -= cycles;
1074 for (i = 0; i < io_ctx->poll_handlers; i++) {
1075 const struct iopoll_rec *rec = &io_ctx->pr[i];
1076 struct ifnet *ifp = rec->ifp;
1078 if (!lwkt_serialize_try(rec->serializer))
1081 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
1082 (IFF_RUNNING | IFF_NPOLLING))
1083 rec->poll_func(ifp, rec->arg, cycles);
1085 lwkt_serialize_exit(rec->serializer);
1088 crit_exit_quick(td);
1090 sched_iopollmore(io_ctx);
1095 * iopollmore_handler is called after other netisr's, possibly scheduling
1096 * another iopoll_handler call, or adapting the burst size for the next cycle.
1098 * It is very bad to fetch large bursts of packets from a single card at once,
1099 * because the burst could take a long time to be completely processed leading
1100 * to unfairness. To reduce the problem, and also to account better for time
1101 * spent in network-related processing, we split the burst in smaller chunks
1102 * of fixed size, giving control to the other netisr's between chunks. This
1103 * helps in improving the fairness, reducing livelock and accounting for the
1104 * work performed in low level handling.
1107 iopollmore_handler(struct netmsg *msg)
1109 struct iopoll_ctx *io_ctx;
1111 int kern_load, poll_hz;
1112 uint32_t pending_polls;
1114 io_ctx = msg->nm_lmsg.u.ms_resultp;
1115 KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1118 lwkt_replymsg(&msg->nm_lmsg, 0);
1120 if (io_ctx->poll_handlers == 0)
1123 #ifdef IFPOLL_MULTI_SYSTIMER
1124 poll_hz = io_ctx->pollhz;
1126 poll_hz = iopoll_hz(io_ctx);
1130 if (io_ctx->residual_burst > 0) {
1132 sched_iopoll(io_ctx);
1134 /* Will run immediately on return, followed by netisrs */
1138 /* Here we can account time spent in iopoll's in this tick */
1140 kern_load = (t.tv_usec - io_ctx->poll_start_t.tv_usec) +
1141 (t.tv_sec - io_ctx->poll_start_t.tv_sec) * 1000000; /* us */
1142 kern_load = (kern_load * poll_hz) / 10000; /* 0..100 */
1143 if (kern_load > (100 - io_ctx->user_frac)) {
1144 /* Try decrease ticks */
1145 if (io_ctx->poll_burst > 1)
1146 io_ctx->poll_burst--;
1148 if (io_ctx->poll_burst < io_ctx->poll_burst_max)
1149 io_ctx->poll_burst++;
1153 io_ctx->pending_polls--;
1154 pending_polls = io_ctx->pending_polls;
1157 if (pending_polls == 0) {
1162 * Last cycle was long and caused us to miss one or more
1163 * hardclock ticks. Restart processing again, but slightly
1164 * reduce the burst size to prevent that this happens again.
1166 io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
1167 if (io_ctx->poll_burst < 1)
1168 io_ctx->poll_burst = 1;
1170 sched_iopoll(io_ctx);
1177 iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
1178 struct iopoll_ctx *io_ctx)
1180 #ifdef IFPOLL_MULTI_SYSTIMER
1181 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "pollhz",
1182 CTLTYPE_INT | CTLFLAG_RW, io_ctx, 0, sysctl_iopollhz,
1183 "I", "Device polling frequency");
1186 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
1187 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
1188 "IU", "Max Polling burst size");
1190 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
1191 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
1192 "IU", "Max size of each burst");
1194 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
1195 &io_ctx->phase, 0, "Polling phase");
1197 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
1198 &io_ctx->suspect, 0, "suspect event");
1200 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
1201 &io_ctx->stalled, 0, "potential stalls");
1203 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
1204 &io_ctx->poll_burst, 0, "Current polling burst size");
1206 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
1207 &io_ctx->user_frac, 0,
1208 "Desired user fraction of cpu time");
1210 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
1211 &io_ctx->short_ticks, 0,
1212 "Hardclock ticks shorter than they should be");
1214 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
1215 &io_ctx->lost_polls, 0,
1216 "How many times we would have lost a poll tick");
1218 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
1219 &io_ctx->pending_polls, 0, "Do we need to poll again");
1221 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
1222 &io_ctx->residual_burst, 0,
1223 "# of residual cycles in burst");
1225 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
1226 &io_ctx->poll_handlers, 0,
1227 "Number of registered poll handlers");
1230 #ifdef IFPOLL_MULTI_SYSTIMER
1233 sysctl_iopollhz(SYSCTL_HANDLER_ARGS)
1235 struct iopoll_ctx *io_ctx = arg1;
1236 struct iopoll_sysctl_netmsg msg;
1237 struct netmsg *nmsg;
1240 phz = io_ctx->pollhz;
1241 error = sysctl_handle_int(oidp, &phz, 0, req);
1242 if (error || req->newptr == NULL)
1246 else if (phz > IFPOLL_FREQ_MAX)
1247 phz = IFPOLL_FREQ_MAX;
1250 netmsg_init(nmsg, &curthread->td_msgport, MSGF_MPSAFE,
1251 sysctl_iopollhz_handler);
1252 nmsg->nm_lmsg.u.ms_result = phz;
1255 return ifnet_domsg(&nmsg->nm_lmsg, io_ctx->poll_cpuid);
1259 sysctl_iopollhz_handler(struct netmsg *nmsg)
1261 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1262 struct iopoll_ctx *io_ctx;
1265 KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1268 * If polling is disabled or there is no polling handler
1269 * registered, don't adjust polling systimer frequency.
1270 * Polling systimer frequency will be adjusted once there
1271 * are registered handlers.
1273 io_ctx->pollhz = nmsg->nm_lmsg.u.ms_result;
1274 if (io_ctx->poll_handlers)
1275 systimer_adjust_periodic(&io_ctx->pollclock, io_ctx->pollhz);
1277 lwkt_replymsg(&nmsg->nm_lmsg, 0);
1280 #endif /* IFPOLL_MULTI_SYSTIMER */
1283 sysctl_burstmax_handler(struct netmsg *nmsg)
1285 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1286 struct iopoll_ctx *io_ctx;
1289 KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1291 io_ctx->poll_burst_max = nmsg->nm_lmsg.u.ms_result;
1292 if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
1293 io_ctx->poll_each_burst = io_ctx->poll_burst_max;
1294 if (io_ctx->poll_burst > io_ctx->poll_burst_max)
1295 io_ctx->poll_burst = io_ctx->poll_burst_max;
1296 if (io_ctx->residual_burst > io_ctx->poll_burst_max)
1297 io_ctx->residual_burst = io_ctx->poll_burst_max;
1299 lwkt_replymsg(&nmsg->nm_lmsg, 0);
1303 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
1305 struct iopoll_ctx *io_ctx = arg1;
1306 struct iopoll_sysctl_netmsg msg;
1307 struct netmsg *nmsg;
1311 burst_max = io_ctx->poll_burst_max;
1312 error = sysctl_handle_int(oidp, &burst_max, 0, req);
1313 if (error || req->newptr == NULL)
1315 if (burst_max < MIN_IOPOLL_BURST_MAX)
1316 burst_max = MIN_IOPOLL_BURST_MAX;
1317 else if (burst_max > MAX_IOPOLL_BURST_MAX)
1318 burst_max = MAX_IOPOLL_BURST_MAX;
1321 netmsg_init(nmsg, &curthread->td_msgport, MSGF_MPSAFE,
1322 sysctl_burstmax_handler);
1323 nmsg->nm_lmsg.u.ms_result = burst_max;
1326 return ifnet_domsg(&nmsg->nm_lmsg, io_ctx->poll_cpuid);
1330 sysctl_eachburst_handler(struct netmsg *nmsg)
1332 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1333 struct iopoll_ctx *io_ctx;
1334 uint32_t each_burst;
1337 KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1339 each_burst = nmsg->nm_lmsg.u.ms_result;
1340 if (each_burst > io_ctx->poll_burst_max)
1341 each_burst = io_ctx->poll_burst_max;
1342 else if (each_burst < 1)
1344 io_ctx->poll_each_burst = each_burst;
1346 lwkt_replymsg(&nmsg->nm_lmsg, 0);
1350 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1352 struct iopoll_ctx *io_ctx = arg1;
1353 struct iopoll_sysctl_netmsg msg;
1354 struct netmsg *nmsg;
1355 uint32_t each_burst;
1358 each_burst = io_ctx->poll_each_burst;
1359 error = sysctl_handle_int(oidp, &each_burst, 0, req);
1360 if (error || req->newptr == NULL)
1364 netmsg_init(nmsg, &curthread->td_msgport, MSGF_MPSAFE,
1365 sysctl_eachburst_handler);
1366 nmsg->nm_lmsg.u.ms_result = each_burst;
1369 return ifnet_domsg(&nmsg->nm_lmsg, io_ctx->poll_cpuid);
1373 iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1374 const struct ifpoll_io *io_rec)
1378 KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1380 if (io_rec->poll_func == NULL)
1384 * Check if there is room.
1386 if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1388 * List full, cannot register more entries.
1389 * This should never happen; if it does, it is probably a
1390 * broken driver trying to register multiple times. Checking
1391 * this at runtime is expensive, and won't solve the problem
1392 * anyways, so just report a few times and then give up.
1394 static int verbose = 10; /* XXX */
1396 kprintf("io poll handlers list full, "
1397 "maybe a broken driver ?\n");
1402 struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1405 rec->serializer = io_rec->serializer;
1406 rec->arg = io_rec->arg;
1407 rec->poll_func = io_rec->poll_func;
1409 io_ctx->poll_handlers++;
1410 if (io_ctx->poll_handlers == 1) {
1411 #ifdef IFPOLL_MULTI_SYSTIMER
1412 systimer_adjust_periodic(&io_ctx->pollclock,
1417 if (io_ctx->poll_type == IFPOLL_RX)
1418 mask = &ifpoll0.rx_cpumask;
1420 mask = &ifpoll0.tx_cpumask;
1421 KKASSERT((*mask & mycpu->gd_cpumask) == 0);
1422 atomic_set_int(mask, mycpu->gd_cpumask);
1425 #ifndef IFPOLL_MULTI_SYSTIMER
1426 ifpoll_handler_addevent();
1434 iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1438 KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1440 for (i = 0; i < io_ctx->poll_handlers; ++i) {
1441 if (io_ctx->pr[i].ifp == ifp) /* Found it */
1444 if (i == io_ctx->poll_handlers) {
1447 io_ctx->poll_handlers--;
1448 if (i < io_ctx->poll_handlers) {
1449 /* Last entry replaces this one. */
1450 io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1453 if (io_ctx->poll_handlers == 0) {
1454 #ifdef IFPOLL_MULTI_SYSTIMER
1455 systimer_adjust_periodic(&io_ctx->pollclock, 1);
1459 if (io_ctx->poll_type == IFPOLL_RX)
1460 mask = &ifpoll0.rx_cpumask;
1462 mask = &ifpoll0.tx_cpumask;
1463 KKASSERT(*mask & mycpu->gd_cpumask);
1464 atomic_clear_int(mask, mycpu->gd_cpumask);
1466 iopoll_reset_state(io_ctx);
1468 #ifndef IFPOLL_MULTI_SYSTIMER
1469 ifpoll_handler_delevent();