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 5000
110 #define IOPOLL_BURST_MAX 375 /* good for 1000Mbit net and HZ=4000 */
112 #define IOPOLL_EACH_BURST 15
113 #define IOPOLL_USER_FRAC 50
115 #define IFPOLL_FREQ_DEFAULT 4000
117 #define IFPOLL_TXFRAC_DEFAULT 1 /* 1/1 of the pollhz */
118 #define IFPOLL_STFRAC_DEFAULT 80 /* 1/80 of the pollhz */
120 #define IFPOLL_RX 0x1
121 #define IFPOLL_TX 0x2
129 struct lwkt_serialize *serializer;
132 ifpoll_iofn_t poll_func;
136 union ifpoll_time prev_t;
137 u_long short_ticks; /* statistics */
138 u_long lost_polls; /* statistics */
139 u_long suspect; /* statistics */
140 u_long stalled; /* statistics */
141 uint32_t pending_polls; /* state */
143 struct netmsg_base poll_netmsg;
144 struct netmsg_base poll_more_netmsg;
148 uint32_t phase; /* state */
149 int residual_burst; /* state */
150 uint32_t poll_each_burst; /* tunable */
151 union ifpoll_time poll_start_t; /* state */
153 uint32_t poll_burst; /* state */
154 uint32_t poll_burst_max; /* tunable */
155 uint32_t user_frac; /* tunable */
156 uint32_t kern_frac; /* state */
158 uint32_t poll_handlers; /* next free entry in pr[]. */
159 struct iopoll_rec pr[IFPOLL_LIST_LEN];
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;
190 uint32_t poll_handlers; /* next free entry in pr[]. */
191 struct stpoll_rec pr[IFPOLL_LIST_LEN];
193 struct sysctl_ctx_list poll_sysctl_ctx;
194 struct sysctl_oid *poll_sysctl_tree;
197 struct iopoll_sysctl_netmsg {
198 struct netmsg_base base;
199 struct iopoll_ctx *ctx;
202 void ifpoll_init_pcpu(int);
203 static void ifpoll_register_handler(netmsg_t);
204 static void ifpoll_deregister_handler(netmsg_t);
209 static void stpoll_init(void);
210 static void stpoll_handler(netmsg_t);
211 static void stpoll_clock(struct stpoll_ctx *);
212 static int stpoll_register(struct ifnet *, const struct ifpoll_status *);
213 static int stpoll_deregister(struct ifnet *);
218 static struct iopoll_ctx *iopoll_ctx_create(int, int);
219 static void iopoll_init(int);
220 static void rxpoll_handler(netmsg_t);
221 static void txpoll_handler(netmsg_t);
222 static void rxpollmore_handler(netmsg_t);
223 static void txpollmore_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 *, int);
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);
250 static int sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS);
251 static int sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS);
253 static struct stpoll_ctx stpoll_context;
254 static struct poll_comm *poll_common[MAXCPU];
255 static struct iopoll_ctx *rxpoll_context[MAXCPU];
256 static struct iopoll_ctx *txpoll_context[MAXCPU];
258 SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
259 "Network device polling parameters");
261 static int iopoll_burst_max = IOPOLL_BURST_MAX;
262 static int iopoll_each_burst = IOPOLL_EACH_BURST;
263 static int iopoll_user_frac = IOPOLL_USER_FRAC;
265 static int ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
266 static int ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
267 static int ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
269 TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
270 TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
271 TUNABLE_INT("net.ifpoll.user_frac", &iopoll_user_frac);
272 TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
273 TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
274 TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
276 #define IFPOLL_FREQ_ADJ(comm) (((comm)->poll_cpuid * 3) % 50)
279 poll_comm_pollhz_div(const struct poll_comm *comm, int pollhz)
281 return pollhz + IFPOLL_FREQ_ADJ(comm);
285 poll_comm_pollhz_conv(const struct poll_comm *comm, int pollhz)
287 return pollhz - IFPOLL_FREQ_ADJ(comm);
291 ifpoll_sendmsg_oncpu(netmsg_t msg)
293 if (msg->lmsg.ms_flags & MSGF_DONE)
294 lwkt_sendmsg(netisr_portfn(mycpuid), &msg->lmsg);
298 sched_stpoll(struct stpoll_ctx *st_ctx)
300 ifpoll_sendmsg_oncpu((netmsg_t)&st_ctx->poll_netmsg);
304 sched_iopoll(struct iopoll_ctx *io_ctx)
306 ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_netmsg);
310 sched_iopollmore(struct iopoll_ctx *io_ctx)
312 ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_more_netmsg);
316 ifpoll_time_get(union ifpoll_time *t)
318 if (__predict_true(tsc_present))
324 /* Return time diff in us */
326 ifpoll_time_diff(const union ifpoll_time *s, const union ifpoll_time *e)
328 if (__predict_true(tsc_present)) {
329 return (((e->tsc - s->tsc) * 1000000) / tsc_frequency);
331 return ((e->tv.tv_usec - s->tv.tv_usec) +
332 (e->tv.tv_sec - s->tv.tv_sec) * 1000000);
337 * Initialize per-cpu qpolling(4) context. Called from kern_clock.c:
340 ifpoll_init_pcpu(int cpuid)
345 poll_comm_init(cpuid);
351 poll_comm_start(cpuid);
355 ifpoll_register(struct ifnet *ifp)
357 struct ifpoll_info *info;
358 struct netmsg_base nmsg;
361 if (ifp->if_npoll == NULL) {
362 /* Device does not support polling */
366 info = kmalloc(sizeof(*info), M_TEMP, M_WAITOK | M_ZERO);
369 * Attempt to register. Interlock with IFF_NPOLLING.
372 ifnet_serialize_all(ifp);
374 if (ifp->if_flags & IFF_NPOLLING) {
375 /* Already polling */
376 ifnet_deserialize_all(ifp);
381 info->ifpi_ifp = ifp;
383 ifp->if_flags |= IFF_NPOLLING;
384 ifp->if_npoll(ifp, info);
385 KASSERT(ifp->if_npoll_cpuid >= 0, ("invalid npoll cpuid"));
387 ifnet_deserialize_all(ifp);
389 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
390 0, ifpoll_register_handler);
391 nmsg.lmsg.u.ms_resultp = info;
393 error = lwkt_domsg(netisr_portfn(0), &nmsg.lmsg, 0);
395 if (!ifpoll_deregister(ifp)) {
396 if_printf(ifp, "ifpoll_register: "
397 "ifpoll_deregister failed!\n");
406 ifpoll_deregister(struct ifnet *ifp)
408 struct netmsg_base nmsg;
411 if (ifp->if_npoll == NULL)
414 ifnet_serialize_all(ifp);
416 if ((ifp->if_flags & IFF_NPOLLING) == 0) {
417 ifnet_deserialize_all(ifp);
420 ifp->if_flags &= ~IFF_NPOLLING;
422 ifnet_deserialize_all(ifp);
424 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
425 0, ifpoll_deregister_handler);
426 nmsg.lmsg.u.ms_resultp = ifp;
428 error = lwkt_domsg(netisr_portfn(0), &nmsg.lmsg, 0);
430 ifnet_serialize_all(ifp);
431 ifp->if_npoll(ifp, NULL);
432 KASSERT(ifp->if_npoll_cpuid < 0, ("invalid npoll cpuid"));
433 ifnet_deserialize_all(ifp);
439 ifpoll_register_handler(netmsg_t nmsg)
441 const struct ifpoll_info *info = nmsg->lmsg.u.ms_resultp;
442 int cpuid = mycpuid, nextcpu;
445 KKASSERT(cpuid < ncpus2);
446 KKASSERT(&curthread->td_msgport == netisr_portfn(cpuid));
449 error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
454 error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
455 &info->ifpi_rx[cpuid]);
459 error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
460 &info->ifpi_tx[cpuid]);
464 /* Adjust polling frequency, after all registration is done */
465 poll_comm_adjust_pollhz(poll_common[cpuid]);
468 if (nextcpu < ncpus2)
469 lwkt_forwardmsg(netisr_portfn(nextcpu), &nmsg->lmsg);
471 lwkt_replymsg(&nmsg->lmsg, 0);
474 lwkt_replymsg(&nmsg->lmsg, error);
478 ifpoll_deregister_handler(netmsg_t nmsg)
480 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
481 int cpuid = mycpuid, nextcpu;
483 KKASSERT(cpuid < ncpus2);
484 KKASSERT(&curthread->td_msgport == netisr_portfn(cpuid));
488 stpoll_deregister(ifp);
489 iopoll_deregister(ifp, rxpoll_context[cpuid]);
490 iopoll_deregister(ifp, txpoll_context[cpuid]);
492 /* Adjust polling frequency, after all deregistration is done */
493 poll_comm_adjust_pollhz(poll_common[cpuid]);
496 if (nextcpu < ncpus2)
497 lwkt_forwardmsg(netisr_portfn(nextcpu), &nmsg->lmsg);
499 lwkt_replymsg(&nmsg->lmsg, 0);
505 struct stpoll_ctx *st_ctx = &stpoll_context;
506 const struct poll_comm *comm = poll_common[0];
508 sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
509 st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
510 SYSCTL_CHILDREN(comm->sysctl_tree),
511 OID_AUTO, "status", CTLFLAG_RD, 0, "");
513 SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
514 SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
515 OID_AUTO, "handlers", CTLFLAG_RD,
516 &st_ctx->poll_handlers, 0,
517 "Number of registered status poll handlers");
519 netmsg_init(&st_ctx->poll_netmsg, NULL, &netisr_adone_rport,
524 * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
525 * once per polling systimer tick.
528 stpoll_handler(netmsg_t msg)
530 struct stpoll_ctx *st_ctx = &stpoll_context;
531 struct thread *td = curthread;
534 KKASSERT(&td->td_msgport == netisr_portfn(0));
536 crit_enter_quick(td);
539 lwkt_replymsg(&msg->lmsg, 0);
541 if (st_ctx->poll_handlers == 0) {
546 for (i = 0; i < st_ctx->poll_handlers; ++i) {
547 const struct stpoll_rec *rec = &st_ctx->pr[i];
548 struct ifnet *ifp = rec->ifp;
550 if (!lwkt_serialize_try(rec->serializer))
553 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
554 (IFF_RUNNING | IFF_NPOLLING))
555 rec->status_func(ifp);
557 lwkt_serialize_exit(rec->serializer);
564 * Hook from status poll systimer. Tries to schedule an status poll.
565 * NOTE: Caller should hold critical section.
568 stpoll_clock(struct stpoll_ctx *st_ctx)
570 KKASSERT(mycpuid == 0);
572 if (st_ctx->poll_handlers == 0)
574 sched_stpoll(st_ctx);
578 stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
580 struct stpoll_ctx *st_ctx = &stpoll_context;
583 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
585 if (st_rec->status_func == NULL)
589 * Check if there is room.
591 if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
593 * List full, cannot register more entries.
594 * This should never happen; if it does, it is probably a
595 * broken driver trying to register multiple times. Checking
596 * this at runtime is expensive, and won't solve the problem
597 * anyways, so just report a few times and then give up.
599 static int verbose = 10; /* XXX */
602 kprintf("status poll handlers list full, "
603 "maybe a broken driver ?\n");
608 struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
611 rec->serializer = st_rec->serializer;
612 rec->status_func = st_rec->status_func;
614 st_ctx->poll_handlers++;
621 stpoll_deregister(struct ifnet *ifp)
623 struct stpoll_ctx *st_ctx = &stpoll_context;
626 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
628 for (i = 0; i < st_ctx->poll_handlers; ++i) {
629 if (st_ctx->pr[i].ifp == ifp) /* Found it */
632 if (i == st_ctx->poll_handlers) {
635 st_ctx->poll_handlers--;
636 if (i < st_ctx->poll_handlers) {
637 /* Last entry replaces this one. */
638 st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
646 iopoll_reset_state(struct iopoll_ctx *io_ctx)
649 io_ctx->poll_burst = io_ctx->poll_each_burst;
650 io_ctx->pending_polls = 0;
651 io_ctx->residual_burst = 0;
653 io_ctx->kern_frac = 0;
654 bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
655 bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
660 iopoll_init(int cpuid)
662 KKASSERT(cpuid < ncpus2);
664 rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
665 txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
668 static struct iopoll_ctx *
669 iopoll_ctx_create(int cpuid, int poll_type)
671 struct poll_comm *comm;
672 struct iopoll_ctx *io_ctx;
673 const char *poll_type_str;
674 netisr_fn_t handler, more_handler;
676 KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
679 * Make sure that tunables are in sane state
681 if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
682 iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
683 else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
684 iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
686 if (iopoll_each_burst > iopoll_burst_max)
687 iopoll_each_burst = iopoll_burst_max;
689 comm = poll_common[cpuid];
692 * Create the per-cpu polling context
694 io_ctx = kmalloc_cachealign(sizeof(*io_ctx), M_DEVBUF,
697 io_ctx->poll_each_burst = iopoll_each_burst;
698 io_ctx->poll_burst_max = iopoll_burst_max;
699 io_ctx->user_frac = iopoll_user_frac;
700 if (poll_type == IFPOLL_RX)
701 io_ctx->pollhz = comm->pollhz;
703 io_ctx->pollhz = comm->pollhz / (comm->poll_txfrac + 1);
704 io_ctx->poll_cpuid = cpuid;
705 iopoll_reset_state(io_ctx);
707 if (poll_type == IFPOLL_RX) {
708 handler = rxpoll_handler;
709 more_handler = rxpollmore_handler;
711 handler = txpoll_handler;
712 more_handler = txpollmore_handler;
715 netmsg_init(&io_ctx->poll_netmsg, NULL, &netisr_adone_rport,
717 io_ctx->poll_netmsg.lmsg.u.ms_resultp = io_ctx;
719 netmsg_init(&io_ctx->poll_more_netmsg, NULL, &netisr_adone_rport,
721 io_ctx->poll_more_netmsg.lmsg.u.ms_resultp = io_ctx;
724 * Initialize per-cpu sysctl nodes
726 if (poll_type == IFPOLL_RX)
727 poll_type_str = "rx";
729 poll_type_str = "tx";
731 sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
732 io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
733 SYSCTL_CHILDREN(comm->sysctl_tree),
734 OID_AUTO, poll_type_str, CTLFLAG_RD, 0, "");
735 iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
736 SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx, poll_type);
742 * Hook from iopoll systimer. Tries to schedule an iopoll, but keeps
743 * track of lost ticks due to the previous handler taking too long.
744 * Normally, this should not happen, because polling handler should
745 * run for a short time. However, in some cases (e.g. when there are
746 * changes in link status etc.) the drivers take a very long time
747 * (even in the order of milliseconds) to reset and reconfigure the
748 * device, causing apparent lost polls.
750 * The first part of the code is just for debugging purposes, and tries
751 * to count how often hardclock ticks are shorter than they should,
752 * meaning either stray interrupts or delayed events.
754 * WARNING! called from fastint or IPI, the MP lock might not be held.
755 * NOTE: Caller should hold critical section.
758 iopoll_clock(struct iopoll_ctx *io_ctx)
763 KKASSERT(mycpuid == io_ctx->poll_cpuid);
765 if (io_ctx->poll_handlers == 0)
769 delta = ifpoll_time_diff(&io_ctx->prev_t, &t);
770 if (delta * io_ctx->pollhz < 500000)
771 io_ctx->short_ticks++;
775 if (io_ctx->pending_polls > 100) {
777 * Too much, assume it has stalled (not always true
778 * see comment above).
781 io_ctx->pending_polls = 0;
785 if (io_ctx->phase <= 2) {
786 if (io_ctx->phase != 0)
789 sched_iopoll(io_ctx);
792 if (io_ctx->pending_polls++ > 0)
793 io_ctx->lost_polls++;
797 * rxpoll_handler and txpoll_handler are scheduled by sched_iopoll when
798 * appropriate, typically once per polling systimer tick.
800 * Note that the message is replied immediately in order to allow a new
801 * ISR to be scheduled in the handler.
804 rxpoll_handler(netmsg_t msg)
806 struct iopoll_ctx *io_ctx;
807 struct thread *td = curthread;
810 io_ctx = msg->lmsg.u.ms_resultp;
811 KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
813 crit_enter_quick(td);
816 lwkt_replymsg(&msg->lmsg, 0);
818 if (io_ctx->poll_handlers == 0) {
824 if (io_ctx->residual_burst == 0) {
825 /* First call in this tick */
826 ifpoll_time_get(&io_ctx->poll_start_t);
827 io_ctx->residual_burst = io_ctx->poll_burst;
829 cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
830 io_ctx->residual_burst : io_ctx->poll_each_burst;
831 io_ctx->residual_burst -= cycles;
833 for (i = 0; i < io_ctx->poll_handlers; i++) {
834 const struct iopoll_rec *rec = &io_ctx->pr[i];
835 struct ifnet *ifp = rec->ifp;
837 if (!lwkt_serialize_try(rec->serializer))
840 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
841 (IFF_RUNNING | IFF_NPOLLING))
842 rec->poll_func(ifp, rec->arg, cycles);
844 lwkt_serialize_exit(rec->serializer);
848 * Do a quick exit/enter to catch any higher-priority
852 crit_enter_quick(td);
854 sched_iopollmore(io_ctx);
861 txpoll_handler(netmsg_t msg)
863 struct iopoll_ctx *io_ctx;
864 struct thread *td = curthread;
867 io_ctx = msg->lmsg.u.ms_resultp;
868 KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
870 crit_enter_quick(td);
873 lwkt_replymsg(&msg->lmsg, 0);
875 if (io_ctx->poll_handlers == 0) {
882 for (i = 0; i < io_ctx->poll_handlers; i++) {
883 const struct iopoll_rec *rec = &io_ctx->pr[i];
884 struct ifnet *ifp = rec->ifp;
886 if (!lwkt_serialize_try(rec->serializer))
889 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
890 (IFF_RUNNING | IFF_NPOLLING))
891 rec->poll_func(ifp, rec->arg, -1);
893 lwkt_serialize_exit(rec->serializer);
897 * Do a quick exit/enter to catch any higher-priority
901 crit_enter_quick(td);
903 sched_iopollmore(io_ctx);
910 * rxpollmore_handler and txpollmore_handler are called after other netisr's,
911 * possibly scheduling another rxpoll_handler or txpoll_handler call, or
912 * adapting the burst size for the next cycle.
914 * It is very bad to fetch large bursts of packets from a single card at once,
915 * because the burst could take a long time to be completely processed leading
916 * to unfairness. To reduce the problem, and also to account better for time
917 * spent in network-related processing, we split the burst in smaller chunks
918 * of fixed size, giving control to the other netisr's between chunks. This
919 * helps in improving the fairness, reducing livelock and accounting for the
920 * work performed in low level handling.
923 rxpollmore_handler(netmsg_t msg)
925 struct thread *td = curthread;
926 struct iopoll_ctx *io_ctx;
929 uint32_t pending_polls;
931 io_ctx = msg->lmsg.u.ms_resultp;
932 KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
934 crit_enter_quick(td);
937 lwkt_replymsg(&msg->lmsg, 0);
939 if (io_ctx->poll_handlers == 0) {
945 if (io_ctx->residual_burst > 0) {
946 sched_iopoll(io_ctx);
948 /* Will run immediately on return, followed by netisrs */
952 /* Here we can account time spent in iopoll's in this tick */
954 kern_load = ifpoll_time_diff(&io_ctx->poll_start_t, &t);
955 kern_load = (kern_load * io_ctx->pollhz) / 10000; /* 0..100 */
956 io_ctx->kern_frac = kern_load;
958 if (kern_load > (100 - io_ctx->user_frac)) {
959 /* Try decrease ticks */
960 if (io_ctx->poll_burst > 1)
961 io_ctx->poll_burst--;
963 if (io_ctx->poll_burst < io_ctx->poll_burst_max)
964 io_ctx->poll_burst++;
967 io_ctx->pending_polls--;
968 pending_polls = io_ctx->pending_polls;
970 if (pending_polls == 0) {
975 * Last cycle was long and caused us to miss one or more
976 * hardclock ticks. Restart processing again, but slightly
977 * reduce the burst size to prevent that this happens again.
979 io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
980 if (io_ctx->poll_burst < 1)
981 io_ctx->poll_burst = 1;
982 sched_iopoll(io_ctx);
990 txpollmore_handler(netmsg_t msg)
992 struct thread *td = curthread;
993 struct iopoll_ctx *io_ctx;
994 uint32_t pending_polls;
996 io_ctx = msg->lmsg.u.ms_resultp;
997 KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
999 crit_enter_quick(td);
1002 lwkt_replymsg(&msg->lmsg, 0);
1004 if (io_ctx->poll_handlers == 0) {
1005 crit_exit_quick(td);
1011 io_ctx->pending_polls--;
1012 pending_polls = io_ctx->pending_polls;
1014 if (pending_polls == 0) {
1019 * Last cycle was long and caused us to miss one or more
1020 * hardclock ticks. Restart processing again.
1022 sched_iopoll(io_ctx);
1026 crit_exit_quick(td);
1030 iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
1031 struct iopoll_ctx *io_ctx, int poll_type)
1033 if (poll_type == IFPOLL_RX) {
1034 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
1035 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
1036 "IU", "Max Polling burst size");
1038 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
1039 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
1040 "IU", "Max size of each burst");
1042 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
1043 &io_ctx->poll_burst, 0, "Current polling burst size");
1045 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
1046 &io_ctx->user_frac, 0, "Desired user fraction of cpu time");
1048 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "kern_frac", CTLFLAG_RD,
1049 &io_ctx->kern_frac, 0, "Kernel fraction of cpu time");
1051 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
1052 &io_ctx->residual_burst, 0,
1053 "# of residual cycles in burst");
1056 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
1057 &io_ctx->phase, 0, "Polling phase");
1059 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
1060 &io_ctx->suspect, "Suspected events");
1062 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
1063 &io_ctx->stalled, "Potential stalls");
1065 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
1066 &io_ctx->short_ticks,
1067 "Hardclock ticks shorter than they should be");
1069 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
1070 &io_ctx->lost_polls,
1071 "How many times we would have lost a poll tick");
1073 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
1074 &io_ctx->pending_polls, 0, "Do we need to poll again");
1076 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
1077 &io_ctx->poll_handlers, 0, "Number of registered poll handlers");
1081 sysctl_burstmax_handler(netmsg_t nmsg)
1083 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1084 struct iopoll_ctx *io_ctx;
1087 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1089 io_ctx->poll_burst_max = nmsg->lmsg.u.ms_result;
1090 if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
1091 io_ctx->poll_each_burst = io_ctx->poll_burst_max;
1092 if (io_ctx->poll_burst > io_ctx->poll_burst_max)
1093 io_ctx->poll_burst = io_ctx->poll_burst_max;
1094 if (io_ctx->residual_burst > io_ctx->poll_burst_max)
1095 io_ctx->residual_burst = io_ctx->poll_burst_max;
1097 lwkt_replymsg(&nmsg->lmsg, 0);
1101 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
1103 struct iopoll_ctx *io_ctx = arg1;
1104 struct iopoll_sysctl_netmsg msg;
1108 burst_max = io_ctx->poll_burst_max;
1109 error = sysctl_handle_int(oidp, &burst_max, 0, req);
1110 if (error || req->newptr == NULL)
1112 if (burst_max < MIN_IOPOLL_BURST_MAX)
1113 burst_max = MIN_IOPOLL_BURST_MAX;
1114 else if (burst_max > MAX_IOPOLL_BURST_MAX)
1115 burst_max = MAX_IOPOLL_BURST_MAX;
1117 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1118 0, sysctl_burstmax_handler);
1119 msg.base.lmsg.u.ms_result = burst_max;
1122 return lwkt_domsg(netisr_portfn(io_ctx->poll_cpuid), &msg.base.lmsg, 0);
1126 sysctl_eachburst_handler(netmsg_t nmsg)
1128 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1129 struct iopoll_ctx *io_ctx;
1130 uint32_t each_burst;
1133 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1135 each_burst = nmsg->lmsg.u.ms_result;
1136 if (each_burst > io_ctx->poll_burst_max)
1137 each_burst = io_ctx->poll_burst_max;
1138 else if (each_burst < 1)
1140 io_ctx->poll_each_burst = each_burst;
1142 lwkt_replymsg(&nmsg->lmsg, 0);
1146 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1148 struct iopoll_ctx *io_ctx = arg1;
1149 struct iopoll_sysctl_netmsg msg;
1150 uint32_t each_burst;
1153 each_burst = io_ctx->poll_each_burst;
1154 error = sysctl_handle_int(oidp, &each_burst, 0, req);
1155 if (error || req->newptr == NULL)
1158 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1159 0, sysctl_eachburst_handler);
1160 msg.base.lmsg.u.ms_result = each_burst;
1163 return lwkt_domsg(netisr_portfn(io_ctx->poll_cpuid), &msg.base.lmsg, 0);
1167 iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1168 const struct ifpoll_io *io_rec)
1172 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1174 if (io_rec->poll_func == NULL)
1178 * Check if there is room.
1180 if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1182 * List full, cannot register more entries.
1183 * This should never happen; if it does, it is probably a
1184 * broken driver trying to register multiple times. Checking
1185 * this at runtime is expensive, and won't solve the problem
1186 * anyways, so just report a few times and then give up.
1188 static int verbose = 10; /* XXX */
1190 kprintf("io poll handlers list full, "
1191 "maybe a broken driver ?\n");
1196 struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1199 rec->serializer = io_rec->serializer;
1200 rec->arg = io_rec->arg;
1201 rec->poll_func = io_rec->poll_func;
1203 io_ctx->poll_handlers++;
1210 iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1214 KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1216 for (i = 0; i < io_ctx->poll_handlers; ++i) {
1217 if (io_ctx->pr[i].ifp == ifp) /* Found it */
1220 if (i == io_ctx->poll_handlers) {
1223 io_ctx->poll_handlers--;
1224 if (i < io_ctx->poll_handlers) {
1225 /* Last entry replaces this one. */
1226 io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1229 if (io_ctx->poll_handlers == 0)
1230 iopoll_reset_state(io_ctx);
1237 poll_comm_init(int cpuid)
1239 struct poll_comm *comm;
1242 comm = kmalloc_cachealign(sizeof(*comm), M_DEVBUF, M_WAITOK | M_ZERO);
1244 if (ifpoll_stfrac < 1)
1245 ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
1246 if (ifpoll_txfrac < 1)
1247 ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
1249 comm->poll_cpuid = cpuid;
1250 comm->pollhz = poll_comm_pollhz_div(comm, ifpoll_pollhz);
1251 comm->poll_stfrac = ifpoll_stfrac - 1;
1252 comm->poll_txfrac = ifpoll_txfrac - 1;
1254 ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", cpuid);
1256 sysctl_ctx_init(&comm->sysctl_ctx);
1257 comm->sysctl_tree = SYSCTL_ADD_NODE(&comm->sysctl_ctx,
1258 SYSCTL_STATIC_CHILDREN(_net_ifpoll),
1259 OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
1261 SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1262 OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
1263 comm, 0, sysctl_pollhz,
1264 "I", "Device polling frequency");
1267 SYSCTL_ADD_PROC(&comm->sysctl_ctx,
1268 SYSCTL_CHILDREN(comm->sysctl_tree),
1269 OID_AUTO, "status_frac",
1270 CTLTYPE_INT | CTLFLAG_RW,
1271 comm, 0, sysctl_stfrac,
1272 "I", "# of cycles before status is polled");
1274 SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1275 OID_AUTO, "tx_frac", CTLTYPE_INT | CTLFLAG_RW,
1276 comm, 0, sysctl_txfrac,
1277 "I", "# of cycles before TX is polled");
1279 poll_common[cpuid] = comm;
1283 poll_comm_start(int cpuid)
1285 struct poll_comm *comm = poll_common[cpuid];
1286 systimer_func_t func;
1289 * Initialize systimer
1292 func = poll_comm_systimer0;
1294 func = poll_comm_systimer;
1295 systimer_init_periodic_nq(&comm->pollclock, func, comm, 1);
1299 _poll_comm_systimer(struct poll_comm *comm)
1301 if (comm->txfrac_count-- == 0) {
1302 comm->txfrac_count = comm->poll_txfrac;
1303 iopoll_clock(txpoll_context[comm->poll_cpuid]);
1305 iopoll_clock(rxpoll_context[comm->poll_cpuid]);
1309 poll_comm_systimer0(systimer_t info, int in_ipi __unused,
1310 struct intrframe *frame __unused)
1312 struct poll_comm *comm = info->data;
1313 globaldata_t gd = mycpu;
1315 KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid == 0);
1319 if (comm->stfrac_count-- == 0) {
1320 comm->stfrac_count = comm->poll_stfrac;
1321 stpoll_clock(&stpoll_context);
1323 _poll_comm_systimer(comm);
1329 poll_comm_systimer(systimer_t info, int in_ipi __unused,
1330 struct intrframe *frame __unused)
1332 struct poll_comm *comm = info->data;
1333 globaldata_t gd = mycpu;
1335 KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid != 0);
1338 _poll_comm_systimer(comm);
1343 poll_comm_adjust_pollhz(struct poll_comm *comm)
1348 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1351 * If there is no polling handler registered, set systimer
1352 * frequency to the lowest value. Polling systimer frequency
1353 * will be adjusted to the requested value, once there are
1354 * registered handlers.
1356 handlers = rxpoll_context[mycpuid]->poll_handlers +
1357 txpoll_context[mycpuid]->poll_handlers;
1358 if (comm->poll_cpuid == 0)
1359 handlers += stpoll_context.poll_handlers;
1361 pollhz = comm->pollhz;
1362 systimer_adjust_periodic(&comm->pollclock, pollhz);
1366 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
1368 struct poll_comm *comm = arg1;
1369 struct netmsg_base nmsg;
1372 phz = poll_comm_pollhz_conv(comm, comm->pollhz);
1373 error = sysctl_handle_int(oidp, &phz, 0, req);
1374 if (error || req->newptr == NULL)
1378 else if (phz > IFPOLL_FREQ_MAX)
1379 phz = IFPOLL_FREQ_MAX;
1381 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1382 0, sysctl_pollhz_handler);
1383 nmsg.lmsg.u.ms_result = phz;
1385 return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1389 sysctl_pollhz_handler(netmsg_t nmsg)
1391 struct poll_comm *comm = poll_common[mycpuid];
1393 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1395 /* Save polling frequency */
1396 comm->pollhz = poll_comm_pollhz_div(comm, nmsg->lmsg.u.ms_result);
1399 * Adjust cached pollhz
1401 rxpoll_context[mycpuid]->pollhz = comm->pollhz;
1402 txpoll_context[mycpuid]->pollhz =
1403 comm->pollhz / (comm->poll_txfrac + 1);
1406 * Adjust polling frequency
1408 poll_comm_adjust_pollhz(comm);
1410 lwkt_replymsg(&nmsg->lmsg, 0);
1414 sysctl_stfrac(SYSCTL_HANDLER_ARGS)
1416 struct poll_comm *comm = arg1;
1417 struct netmsg_base nmsg;
1420 KKASSERT(comm->poll_cpuid == 0);
1422 stfrac = comm->poll_stfrac + 1;
1423 error = sysctl_handle_int(oidp, &stfrac, 0, req);
1424 if (error || req->newptr == NULL)
1429 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1430 0, sysctl_stfrac_handler);
1431 nmsg.lmsg.u.ms_result = stfrac - 1;
1433 return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1437 sysctl_stfrac_handler(netmsg_t nmsg)
1439 struct poll_comm *comm = poll_common[mycpuid];
1440 int stfrac = nmsg->lmsg.u.ms_result;
1442 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1445 comm->poll_stfrac = stfrac;
1446 if (comm->stfrac_count > comm->poll_stfrac)
1447 comm->stfrac_count = comm->poll_stfrac;
1450 lwkt_replymsg(&nmsg->lmsg, 0);
1454 sysctl_txfrac(SYSCTL_HANDLER_ARGS)
1456 struct poll_comm *comm = arg1;
1457 struct netmsg_base nmsg;
1460 txfrac = comm->poll_txfrac + 1;
1461 error = sysctl_handle_int(oidp, &txfrac, 0, req);
1462 if (error || req->newptr == NULL)
1467 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1468 0, sysctl_txfrac_handler);
1469 nmsg.lmsg.u.ms_result = txfrac - 1;
1471 return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1475 sysctl_txfrac_handler(netmsg_t nmsg)
1477 struct poll_comm *comm = poll_common[mycpuid];
1478 int txfrac = nmsg->lmsg.u.ms_result;
1480 KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1483 comm->poll_txfrac = txfrac;
1484 if (comm->txfrac_count > comm->poll_txfrac)
1485 comm->txfrac_count = comm->poll_txfrac;
1488 lwkt_replymsg(&nmsg->lmsg, 0);
1492 ifpoll_compat_setup(struct ifpoll_compat *cp,
1493 struct sysctl_ctx_list *sysctl_ctx,
1494 struct sysctl_oid *sysctl_tree,
1495 int unit, struct lwkt_serialize *slz)
1497 cp->ifpc_stcount = 0;
1498 cp->ifpc_stfrac = ((poll_common[0]->poll_stfrac + 1) *
1499 howmany(IOPOLL_BURST_MAX, IOPOLL_EACH_BURST)) - 1;
1501 cp->ifpc_cpuid = unit % ncpus2;
1502 cp->ifpc_serializer = slz;
1504 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1505 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1506 OID_AUTO, "npoll_stfrac", CTLTYPE_INT | CTLFLAG_RW,
1507 cp, 0, sysctl_compat_npoll_stfrac, "I",
1508 "polling status frac");
1509 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1510 OID_AUTO, "npoll_cpuid", CTLTYPE_INT | CTLFLAG_RW,
1511 cp, 0, sysctl_compat_npoll_cpuid, "I",
1517 sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS)
1519 struct ifpoll_compat *cp = arg1;
1520 int error = 0, stfrac;
1522 lwkt_serialize_enter(cp->ifpc_serializer);
1524 stfrac = cp->ifpc_stfrac + 1;
1525 error = sysctl_handle_int(oidp, &stfrac, 0, req);
1526 if (!error && req->newptr != NULL) {
1530 cp->ifpc_stfrac = stfrac - 1;
1531 if (cp->ifpc_stcount > cp->ifpc_stfrac)
1532 cp->ifpc_stcount = cp->ifpc_stfrac;
1536 lwkt_serialize_exit(cp->ifpc_serializer);
1541 sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS)
1543 struct ifpoll_compat *cp = arg1;
1544 int error = 0, cpuid;
1546 lwkt_serialize_enter(cp->ifpc_serializer);
1548 cpuid = cp->ifpc_cpuid;
1549 error = sysctl_handle_int(oidp, &cpuid, 0, req);
1550 if (!error && req->newptr != NULL) {
1551 if (cpuid < 0 || cpuid >= ncpus2)
1554 cp->ifpc_cpuid = cpuid;
1557 lwkt_serialize_exit(cp->ifpc_serializer);