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>
39 #include <sys/microtime_pcpu.h>
41 #include <sys/thread2.h>
42 #include <sys/msgport2.h>
45 #include <net/if_var.h>
46 #include <net/if_poll.h>
47 #include <net/netmsg2.h>
48 #include <net/netisr2.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_npoll 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 * Serializer field of ifpoll_info.ifpi_status and ifpoll_info.ifpi_tx[n]
67 * must _not_ be NULL. The serializer will be held before the status_func
68 * and poll_func being called. Serializer field of ifpoll_info.ifpi_rx[n]
69 * can be NULL, but the interface's if_flags must have IFF_IDIRECT set,
70 * which indicates that the network processing of the input packets is
71 * running directly instead of being redispatched.
73 * RX is polled at the specified polling frequency (net.ifpoll.X.pollhz).
74 * TX and status polling could be done at lower frequency than RX frequency
75 * (net.ifpoll.0.status_frac and net.ifpoll.X.tx_frac). To avoid systimer
76 * staggering at high frequency, RX systimer gives TX and status polling a
79 * All of the registered polling handlers are called only if the interface
80 * is marked as IFF_UP, IFF_RUNNING and IFF_NPOLLING. However, the
81 * interface's register and deregister function (ifnet.if_npoll) will be
82 * called even if interface is not marked with IFF_RUNNING or IFF_UP.
84 * If registration is successful, the driver must disable interrupts,
85 * and further I/O is performed through the TX/RX polling handler, which
86 * are invoked (at least once per clock tick) with 3 arguments: the "arg"
87 * passed at register time, a struct ifnet pointer, and a "count" limit.
88 * The registered serializer will be held before calling the related
91 * The count limit specifies how much work the handler can do during the
92 * call -- typically this is the number of packets to be received, or
93 * transmitted, etc. (drivers are free to interpret this number, as long
94 * as the max time spent in the function grows roughly linearly with the
97 * A second variable controls the sharing of CPU between polling/kernel
98 * network processing, and other activities (typically userlevel tasks):
99 * net.ifpoll.X.{rx,tx}.user_frac (between 0 and 100, default 50) sets the
100 * share of CPU allocated to user tasks. CPU is allocated proportionally
101 * to the shares, by dynamically adjusting the "count" (poll_burst).
103 * Other parameters can should be left to their default values.
104 * The following constraints hold
106 * 1 <= poll_burst <= poll_burst_max
107 * 1 <= poll_each_burst <= poll_burst_max
108 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
111 #define IFPOLL_LIST_LEN 128
112 #define IFPOLL_FREQ_MAX 30000
114 #define MIN_IOPOLL_BURST_MAX 10
115 #define MAX_IOPOLL_BURST_MAX 5000
116 #define IOPOLL_BURST_MAX 250 /* good for 1000Mbit net and HZ=6000 */
118 #define IOPOLL_EACH_BURST 50
119 #define IOPOLL_USER_FRAC 50
121 #define IFPOLL_FREQ_DEFAULT 6000
123 #define IFPOLL_TXFRAC_DEFAULT 1 /* 1/1 of the pollhz */
124 #define IFPOLL_STFRAC_DEFAULT 120 /* 1/120 of the pollhz */
126 #define IFPOLL_RX 0x1
127 #define IFPOLL_TX 0x2
130 struct lwkt_serialize *serializer;
133 ifpoll_iofn_t poll_func;
137 union microtime_pcpu prev_t;
138 u_long short_ticks; /* statistics */
139 u_long lost_polls; /* statistics */
140 u_long suspect; /* statistics */
141 u_long stalled; /* statistics */
142 uint32_t pending_polls; /* state */
144 struct netmsg_base poll_netmsg;
145 struct netmsg_base poll_more_netmsg;
149 uint32_t phase; /* state */
150 int residual_burst; /* state */
151 uint32_t poll_each_burst; /* tunable */
152 union microtime_pcpu poll_start_t; /* state */
154 uint32_t poll_burst; /* state */
155 uint32_t poll_burst_max; /* tunable */
156 uint32_t user_frac; /* tunable */
157 uint32_t kern_frac; /* state */
159 uint32_t poll_handlers; /* next free entry in pr[]. */
160 struct iopoll_rec pr[IFPOLL_LIST_LEN];
162 struct sysctl_ctx_list poll_sysctl_ctx;
163 struct sysctl_oid *poll_sysctl_tree;
167 struct systimer pollclock;
170 int stfrac_count; /* state */
171 int poll_stfrac; /* tunable */
173 int txfrac_count; /* state */
174 int poll_txfrac; /* tunable */
176 int pollhz; /* tunable */
178 struct sysctl_ctx_list sysctl_ctx;
179 struct sysctl_oid *sysctl_tree;
183 struct lwkt_serialize *serializer;
185 ifpoll_stfn_t status_func;
189 struct netmsg_base poll_netmsg;
191 uint32_t poll_handlers; /* next free entry in pr[]. */
192 struct stpoll_rec pr[IFPOLL_LIST_LEN];
194 struct sysctl_ctx_list poll_sysctl_ctx;
195 struct sysctl_oid *poll_sysctl_tree;
198 struct iopoll_sysctl_netmsg {
199 struct netmsg_base base;
200 struct iopoll_ctx *ctx;
203 static void ifpoll_init_pcpu(int);
204 static void ifpoll_register_handler(netmsg_t);
205 static void ifpoll_deregister_handler(netmsg_t);
210 static void stpoll_init(void);
211 static void stpoll_handler(netmsg_t);
212 static void stpoll_clock(struct stpoll_ctx *);
213 static int stpoll_register(struct ifnet *, const struct ifpoll_status *);
214 static int stpoll_deregister(struct ifnet *);
219 static struct iopoll_ctx *iopoll_ctx_create(int, int);
220 static void iopoll_init(int);
221 static void rxpoll_handler(netmsg_t);
222 static void txpoll_handler(netmsg_t);
223 static void rxpollmore_handler(netmsg_t);
224 static void txpollmore_handler(netmsg_t);
225 static void iopoll_clock(struct iopoll_ctx *);
226 static int iopoll_register(struct ifnet *, struct iopoll_ctx *,
227 const struct ifpoll_io *);
228 static int iopoll_deregister(struct ifnet *, struct iopoll_ctx *);
230 static void iopoll_add_sysctl(struct sysctl_ctx_list *,
231 struct sysctl_oid_list *, struct iopoll_ctx *, int);
232 static void sysctl_burstmax_handler(netmsg_t);
233 static int sysctl_burstmax(SYSCTL_HANDLER_ARGS);
234 static void sysctl_eachburst_handler(netmsg_t);
235 static int sysctl_eachburst(SYSCTL_HANDLER_ARGS);
240 static void poll_comm_init(int);
241 static void poll_comm_start(int);
242 static void poll_comm_adjust_pollhz(struct poll_comm *);
243 static void poll_comm_systimer0(systimer_t, int, struct intrframe *);
244 static void poll_comm_systimer(systimer_t, int, struct intrframe *);
245 static void sysctl_pollhz_handler(netmsg_t);
246 static void sysctl_stfrac_handler(netmsg_t);
247 static void sysctl_txfrac_handler(netmsg_t);
248 static int sysctl_pollhz(SYSCTL_HANDLER_ARGS);
249 static int sysctl_stfrac(SYSCTL_HANDLER_ARGS);
250 static int sysctl_txfrac(SYSCTL_HANDLER_ARGS);
251 static int sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS);
252 static int sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS);
254 static struct stpoll_ctx stpoll_context;
255 static struct poll_comm *poll_common[MAXCPU];
256 static struct iopoll_ctx *rxpoll_context[MAXCPU];
257 static struct iopoll_ctx *txpoll_context[MAXCPU];
259 SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
260 "Network device polling parameters");
262 static int iopoll_burst_max = IOPOLL_BURST_MAX;
263 static int iopoll_each_burst = IOPOLL_EACH_BURST;
264 static int iopoll_user_frac = IOPOLL_USER_FRAC;
266 static int ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
267 static int ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
268 static int ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
270 TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
271 TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
272 TUNABLE_INT("net.ifpoll.user_frac", &iopoll_user_frac);
273 TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
274 TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
275 TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
277 #if !defined(KTR_IF_POLL)
278 #define KTR_IF_POLL KTR_ALL
280 KTR_INFO_MASTER(if_poll);
281 KTR_INFO(KTR_IF_POLL, if_poll, rx_start, 0, "rx start");
282 KTR_INFO(KTR_IF_POLL, if_poll, rx_end, 1, "rx end");
283 KTR_INFO(KTR_IF_POLL, if_poll, tx_start, 2, "tx start");
284 KTR_INFO(KTR_IF_POLL, if_poll, tx_end, 3, "tx end");
285 KTR_INFO(KTR_IF_POLL, if_poll, rx_mstart, 4, "rx more start");
286 KTR_INFO(KTR_IF_POLL, if_poll, rx_mend, 5, "rx more end");
287 KTR_INFO(KTR_IF_POLL, if_poll, tx_mstart, 6, "tx more start");
288 KTR_INFO(KTR_IF_POLL, if_poll, tx_mend, 7, "tx more end");
289 KTR_INFO(KTR_IF_POLL, if_poll, ioclock_start, 8, "ioclock start");
290 KTR_INFO(KTR_IF_POLL, if_poll, ioclock_end, 9, "ioclock end");
291 #define logpoll(name) KTR_LOG(if_poll_ ## name)
293 #define IFPOLL_FREQ_ADJ(comm) (((comm)->poll_cpuid * 3) % 50)
296 poll_comm_pollhz_div(const struct poll_comm *comm, int pollhz)
298 return pollhz + IFPOLL_FREQ_ADJ(comm);
302 poll_comm_pollhz_conv(const struct poll_comm *comm, int pollhz)
304 return pollhz - IFPOLL_FREQ_ADJ(comm);
308 ifpoll_sendmsg_oncpu(netmsg_t msg)
310 if (msg->lmsg.ms_flags & MSGF_DONE)
311 netisr_sendmsg_oncpu(&msg->base);
315 sched_stpoll(struct stpoll_ctx *st_ctx)
317 ifpoll_sendmsg_oncpu((netmsg_t)&st_ctx->poll_netmsg);
321 sched_iopoll(struct iopoll_ctx *io_ctx)
323 ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_netmsg);
327 sched_iopollmore(struct iopoll_ctx *io_ctx, boolean_t direct)
331 ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_more_netmsg);
333 struct netmsg_base *nmsg = &io_ctx->poll_more_netmsg;
335 nmsg->lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
336 nmsg->lmsg.ms_flags |= MSGF_SYNC;
337 nmsg->nm_dispatch((netmsg_t)nmsg);
338 KKASSERT(nmsg->lmsg.ms_flags & MSGF_DONE);
343 * Initialize per-cpu polling(4) context.
346 ifpoll_init_pcpu(int cpuid)
349 poll_comm_init(cpuid);
355 poll_comm_start(cpuid);
359 ifpoll_init_handler(netmsg_t msg)
363 ifpoll_init_pcpu(cpu);
364 netisr_forwardmsg(&msg->base, cpu + 1);
368 ifpoll_sysinit(void *dummy __unused)
370 struct netmsg_base msg;
372 netmsg_init(&msg, NULL, &curthread->td_msgport, 0, ifpoll_init_handler);
373 netisr_domsg_global(&msg);
375 SYSINIT(ifpoll, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifpoll_sysinit, NULL);
378 ifpoll_register(struct ifnet *ifp)
380 struct ifpoll_info *info;
381 struct netmsg_base nmsg;
384 if (ifp->if_npoll == NULL) {
385 /* Device does not support polling */
389 info = kmalloc(sizeof(*info), M_TEMP, M_WAITOK | M_ZERO);
392 * Attempt to register. Interlock with IFF_NPOLLING.
395 ifnet_serialize_all(ifp);
397 if (ifp->if_flags & IFF_NPOLLING) {
398 /* Already polling */
399 ifnet_deserialize_all(ifp);
404 info->ifpi_ifp = ifp;
406 ifp->if_flags |= IFF_NPOLLING;
407 ifp->if_npoll(ifp, info);
409 ifnet_deserialize_all(ifp);
411 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
412 0, ifpoll_register_handler);
413 nmsg.lmsg.u.ms_resultp = info;
415 error = netisr_domsg_global(&nmsg);
417 if (!ifpoll_deregister(ifp)) {
418 if_printf(ifp, "ifpoll_register: "
419 "ifpoll_deregister failed!\n");
428 ifpoll_deregister(struct ifnet *ifp)
430 struct netmsg_base nmsg;
433 if (ifp->if_npoll == NULL)
436 ifnet_serialize_all(ifp);
438 if ((ifp->if_flags & IFF_NPOLLING) == 0) {
439 ifnet_deserialize_all(ifp);
442 ifp->if_flags &= ~IFF_NPOLLING;
444 ifnet_deserialize_all(ifp);
446 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
447 0, ifpoll_deregister_handler);
448 nmsg.lmsg.u.ms_resultp = ifp;
450 error = netisr_domsg_global(&nmsg);
452 ifnet_serialize_all(ifp);
453 ifp->if_npoll(ifp, NULL);
454 ifnet_deserialize_all(ifp);
460 ifpoll_register_handler(netmsg_t nmsg)
462 const struct ifpoll_info *info = nmsg->lmsg.u.ms_resultp;
466 KKASSERT(cpuid < netisr_ncpus);
467 KKASSERT(&curthread->td_msgport == netisr_cpuport(cpuid));
470 error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
475 error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
476 &info->ifpi_rx[cpuid]);
480 error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
481 &info->ifpi_tx[cpuid]);
485 /* Adjust polling frequency, after all registration is done */
486 poll_comm_adjust_pollhz(poll_common[cpuid]);
488 netisr_forwardmsg(&nmsg->base, cpuid + 1);
491 netisr_replymsg(&nmsg->base, error);
495 ifpoll_deregister_handler(netmsg_t nmsg)
497 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
500 KKASSERT(cpuid < netisr_ncpus);
501 KKASSERT(&curthread->td_msgport == netisr_cpuport(cpuid));
505 stpoll_deregister(ifp);
506 iopoll_deregister(ifp, rxpoll_context[cpuid]);
507 iopoll_deregister(ifp, txpoll_context[cpuid]);
509 /* Adjust polling frequency, after all deregistration is done */
510 poll_comm_adjust_pollhz(poll_common[cpuid]);
512 netisr_forwardmsg(&nmsg->base, cpuid + 1);
518 struct stpoll_ctx *st_ctx = &stpoll_context;
519 const struct poll_comm *comm = poll_common[0];
521 sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
522 st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
523 SYSCTL_CHILDREN(comm->sysctl_tree),
524 OID_AUTO, "status", CTLFLAG_RD, 0, "");
526 SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
527 SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
528 OID_AUTO, "handlers", CTLFLAG_RD,
529 &st_ctx->poll_handlers, 0,
530 "Number of registered status poll handlers");
532 netmsg_init(&st_ctx->poll_netmsg, NULL, &netisr_adone_rport,
537 * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
538 * once per polling systimer tick.
541 stpoll_handler(netmsg_t msg)
543 struct stpoll_ctx *st_ctx = &stpoll_context;
544 struct thread *td = curthread;
549 crit_enter_quick(td);
552 netisr_replymsg(&msg->base, 0);
554 if (st_ctx->poll_handlers == 0) {
559 for (i = 0; i < st_ctx->poll_handlers; ++i) {
560 const struct stpoll_rec *rec = &st_ctx->pr[i];
561 struct ifnet *ifp = rec->ifp;
563 if (!lwkt_serialize_try(rec->serializer))
566 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
567 (IFF_RUNNING | IFF_NPOLLING))
568 rec->status_func(ifp);
570 lwkt_serialize_exit(rec->serializer);
577 * Hook from status poll systimer. Tries to schedule an status poll.
578 * NOTE: Caller should hold critical section.
581 stpoll_clock(struct stpoll_ctx *st_ctx)
583 KKASSERT(mycpuid == 0);
585 if (st_ctx->poll_handlers == 0)
587 sched_stpoll(st_ctx);
591 stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
593 struct stpoll_ctx *st_ctx = &stpoll_context;
598 if (st_rec->status_func == NULL)
602 * Check if there is room.
604 if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
606 * List full, cannot register more entries.
607 * This should never happen; if it does, it is probably a
608 * broken driver trying to register multiple times. Checking
609 * this at runtime is expensive, and won't solve the problem
610 * anyways, so just report a few times and then give up.
612 static int verbose = 10; /* XXX */
615 kprintf("status poll handlers list full, "
616 "maybe a broken driver ?\n");
621 struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
624 rec->serializer = st_rec->serializer;
625 rec->status_func = st_rec->status_func;
627 st_ctx->poll_handlers++;
634 stpoll_deregister(struct ifnet *ifp)
636 struct stpoll_ctx *st_ctx = &stpoll_context;
641 for (i = 0; i < st_ctx->poll_handlers; ++i) {
642 if (st_ctx->pr[i].ifp == ifp) /* Found it */
645 if (i == st_ctx->poll_handlers) {
648 st_ctx->poll_handlers--;
649 if (i < st_ctx->poll_handlers) {
650 /* Last entry replaces this one. */
651 st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
659 iopoll_reset_state(struct iopoll_ctx *io_ctx)
662 io_ctx->poll_burst = io_ctx->poll_each_burst;
663 io_ctx->pending_polls = 0;
664 io_ctx->residual_burst = 0;
666 io_ctx->kern_frac = 0;
667 bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
668 bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
673 iopoll_init(int cpuid)
675 KKASSERT(cpuid < netisr_ncpus);
677 rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
678 txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
681 static struct iopoll_ctx *
682 iopoll_ctx_create(int cpuid, int poll_type)
684 struct poll_comm *comm;
685 struct iopoll_ctx *io_ctx;
686 const char *poll_type_str;
687 netisr_fn_t handler, more_handler;
689 KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
692 * Make sure that tunables are in sane state
694 if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
695 iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
696 else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
697 iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
699 if (iopoll_each_burst > iopoll_burst_max)
700 iopoll_each_burst = iopoll_burst_max;
702 comm = poll_common[cpuid];
705 * Create the per-cpu polling context
707 io_ctx = kmalloc(sizeof(*io_ctx), M_DEVBUF, M_WAITOK | M_ZERO);
709 io_ctx->poll_each_burst = iopoll_each_burst;
710 io_ctx->poll_burst_max = iopoll_burst_max;
711 io_ctx->user_frac = iopoll_user_frac;
712 if (poll_type == IFPOLL_RX)
713 io_ctx->pollhz = comm->pollhz;
715 io_ctx->pollhz = comm->pollhz / (comm->poll_txfrac + 1);
716 io_ctx->poll_cpuid = cpuid;
717 iopoll_reset_state(io_ctx);
719 if (poll_type == IFPOLL_RX) {
720 handler = rxpoll_handler;
721 more_handler = rxpollmore_handler;
723 handler = txpoll_handler;
724 more_handler = txpollmore_handler;
727 netmsg_init(&io_ctx->poll_netmsg, NULL, &netisr_adone_rport,
729 io_ctx->poll_netmsg.lmsg.u.ms_resultp = io_ctx;
731 netmsg_init(&io_ctx->poll_more_netmsg, NULL, &netisr_adone_rport,
733 io_ctx->poll_more_netmsg.lmsg.u.ms_resultp = io_ctx;
736 * Initialize per-cpu sysctl nodes
738 if (poll_type == IFPOLL_RX)
739 poll_type_str = "rx";
741 poll_type_str = "tx";
743 sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
744 io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
745 SYSCTL_CHILDREN(comm->sysctl_tree),
746 OID_AUTO, poll_type_str, CTLFLAG_RD, 0, "");
747 iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
748 SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx, poll_type);
754 * Hook from iopoll systimer. Tries to schedule an iopoll, but keeps
755 * track of lost ticks due to the previous handler taking too long.
756 * Normally, this should not happen, because polling handler should
757 * run for a short time. However, in some cases (e.g. when there are
758 * changes in link status etc.) the drivers take a very long time
759 * (even in the order of milliseconds) to reset and reconfigure the
760 * device, causing apparent lost polls.
762 * The first part of the code is just for debugging purposes, and tries
763 * to count how often hardclock ticks are shorter than they should,
764 * meaning either stray interrupts or delayed events.
766 * WARNING! called from fastint or IPI, the MP lock might not be held.
767 * NOTE: Caller should hold critical section.
770 iopoll_clock(struct iopoll_ctx *io_ctx)
772 union microtime_pcpu t;
775 KKASSERT(mycpuid == io_ctx->poll_cpuid);
777 if (io_ctx->poll_handlers == 0)
780 logpoll(ioclock_start);
782 microtime_pcpu_get(&t);
783 delta = microtime_pcpu_diff(&io_ctx->prev_t, &t);
784 if (delta * io_ctx->pollhz < 500000)
785 io_ctx->short_ticks++;
789 if (io_ctx->pending_polls > 100) {
791 * Too much, assume it has stalled (not always true
792 * see comment above).
795 io_ctx->pending_polls = 0;
799 if (io_ctx->phase <= 2) {
800 if (io_ctx->phase != 0)
803 sched_iopoll(io_ctx);
806 if (io_ctx->pending_polls++ > 0)
807 io_ctx->lost_polls++;
809 logpoll(ioclock_end);
813 * rxpoll_handler and txpoll_handler are scheduled by sched_iopoll when
814 * appropriate, typically once per polling systimer tick.
816 * Note that the message is replied immediately in order to allow a new
817 * ISR to be scheduled in the handler.
820 rxpoll_handler(netmsg_t msg)
822 struct iopoll_ctx *io_ctx;
823 struct thread *td = curthread;
824 boolean_t direct = TRUE, crit;
829 io_ctx = msg->lmsg.u.ms_resultp;
830 KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
833 crit_enter_quick(td);
836 netisr_replymsg(&msg->base, 0);
838 if (io_ctx->poll_handlers == 0) {
845 if (io_ctx->residual_burst == 0) {
846 /* First call in this tick */
847 microtime_pcpu_get(&io_ctx->poll_start_t);
848 io_ctx->residual_burst = io_ctx->poll_burst;
850 cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
851 io_ctx->residual_burst : io_ctx->poll_each_burst;
852 io_ctx->residual_burst -= cycles;
854 for (i = 0; i < io_ctx->poll_handlers; i++) {
855 const struct iopoll_rec *rec = &io_ctx->pr[i];
856 struct ifnet *ifp = rec->ifp;
858 if (rec->serializer != NULL) {
861 crit_enter_quick(td);
864 !lwkt_serialize_try(rec->serializer))) {
865 /* RX serializer generally will not fail. */
870 * Exit critical section, if the RX polling
871 * handler does not require serialization,
872 * i.e. RX polling is doing direct input.
878 if ((ifp->if_flags & IFF_IDIRECT) == 0) {
880 KASSERT(rec->serializer != NULL,
881 ("rx polling handler is not serialized"));
885 KASSERT(rec->serializer == NULL,
886 ("serialized direct input"));
890 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING | IFF_NPOLLING)) ==
891 (IFF_UP | IFF_RUNNING | IFF_NPOLLING))
892 rec->poll_func(ifp, rec->arg, cycles);
894 if (rec->serializer != NULL)
895 lwkt_serialize_exit(rec->serializer);
900 * Do a quick exit/enter to catch any higher-priority
905 crit_enter_quick(td);
908 sched_iopollmore(io_ctx, direct);
916 txpoll_handler(netmsg_t msg)
918 struct iopoll_ctx *io_ctx;
919 struct thread *td = curthread;
924 io_ctx = msg->lmsg.u.ms_resultp;
925 KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
927 crit_enter_quick(td);
930 netisr_replymsg(&msg->base, 0);
932 if (io_ctx->poll_handlers == 0) {
940 for (i = 0; i < io_ctx->poll_handlers; i++) {
941 const struct iopoll_rec *rec = &io_ctx->pr[i];
942 struct ifnet *ifp = rec->ifp;
944 if (!lwkt_serialize_try(rec->serializer))
947 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING | IFF_NPOLLING)) ==
948 (IFF_UP | IFF_RUNNING | IFF_NPOLLING))
949 rec->poll_func(ifp, rec->arg, -1);
951 lwkt_serialize_exit(rec->serializer);
955 * Do a quick exit/enter to catch any higher-priority
959 crit_enter_quick(td);
962 sched_iopollmore(io_ctx, TRUE);
970 * rxpollmore_handler and txpollmore_handler are called after other netisr's,
971 * possibly scheduling another rxpoll_handler or txpoll_handler call, or
972 * adapting the burst size for the next cycle.
974 * It is very bad to fetch large bursts of packets from a single card at once,
975 * because the burst could take a long time to be completely processed leading
976 * to unfairness. To reduce the problem, and also to account better for time
977 * spent in network-related processing, we split the burst in smaller chunks
978 * of fixed size, giving control to the other netisr's between chunks. This
979 * helps in improving the fairness, reducing livelock and accounting for the
980 * work performed in low level handling.
983 rxpollmore_handler(netmsg_t msg)
985 struct thread *td = curthread;
986 struct iopoll_ctx *io_ctx;
987 union microtime_pcpu t;
989 uint32_t pending_polls;
993 io_ctx = msg->lmsg.u.ms_resultp;
994 KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
996 crit_enter_quick(td);
999 netisr_replymsg(&msg->base, 0);
1001 if (io_ctx->poll_handlers == 0) {
1002 crit_exit_quick(td);
1008 if (io_ctx->residual_burst > 0) {
1009 sched_iopoll(io_ctx);
1010 crit_exit_quick(td);
1011 /* Will run immediately on return, followed by netisrs */
1016 /* Here we can account time spent in iopoll's in this tick */
1017 microtime_pcpu_get(&t);
1018 kern_load = microtime_pcpu_diff(&io_ctx->poll_start_t, &t);
1019 kern_load = (kern_load * io_ctx->pollhz) / 10000; /* 0..100 */
1020 io_ctx->kern_frac = kern_load;
1022 if (kern_load > (100 - io_ctx->user_frac)) {
1023 /* Try decrease ticks */
1024 if (io_ctx->poll_burst > 1)
1025 io_ctx->poll_burst--;
1027 if (io_ctx->poll_burst < io_ctx->poll_burst_max)
1028 io_ctx->poll_burst++;
1031 io_ctx->pending_polls--;
1032 pending_polls = io_ctx->pending_polls;
1034 if (pending_polls == 0) {
1039 * Last cycle was long and caused us to miss one or more
1040 * hardclock ticks. Restart processing again, but slightly
1041 * reduce the burst size to prevent that this happens again.
1043 io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
1044 if (io_ctx->poll_burst < 1)
1045 io_ctx->poll_burst = 1;
1046 sched_iopoll(io_ctx);
1050 crit_exit_quick(td);
1056 txpollmore_handler(netmsg_t msg)
1058 struct thread *td = curthread;
1059 struct iopoll_ctx *io_ctx;
1060 uint32_t pending_polls;
1064 io_ctx = msg->lmsg.u.ms_resultp;
1065 KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1067 crit_enter_quick(td);
1070 netisr_replymsg(&msg->base, 0);
1072 if (io_ctx->poll_handlers == 0) {
1073 crit_exit_quick(td);
1080 io_ctx->pending_polls--;
1081 pending_polls = io_ctx->pending_polls;
1083 if (pending_polls == 0) {
1088 * Last cycle was long and caused us to miss one or more
1089 * hardclock ticks. Restart processing again.
1091 sched_iopoll(io_ctx);
1095 crit_exit_quick(td);
1101 iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
1102 struct iopoll_ctx *io_ctx, int poll_type)
1104 if (poll_type == IFPOLL_RX) {
1105 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
1106 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
1107 "IU", "Max Polling burst size");
1109 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
1110 CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
1111 "IU", "Max size of each burst");
1113 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
1114 &io_ctx->poll_burst, 0, "Current polling burst size");
1116 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
1117 &io_ctx->user_frac, 0, "Desired user fraction of cpu time");
1119 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "kern_frac", CTLFLAG_RD,
1120 &io_ctx->kern_frac, 0, "Kernel fraction of cpu time");
1122 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
1123 &io_ctx->residual_burst, 0,
1124 "# of residual cycles in burst");
1127 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
1128 &io_ctx->phase, 0, "Polling phase");
1130 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
1131 &io_ctx->suspect, "Suspected events");
1133 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
1134 &io_ctx->stalled, "Potential stalls");
1136 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
1137 &io_ctx->short_ticks,
1138 "Hardclock ticks shorter than they should be");
1140 SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
1141 &io_ctx->lost_polls,
1142 "How many times we would have lost a poll tick");
1144 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
1145 &io_ctx->pending_polls, 0, "Do we need to poll again");
1147 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
1148 &io_ctx->poll_handlers, 0, "Number of registered poll handlers");
1152 sysctl_burstmax_handler(netmsg_t nmsg)
1154 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1155 struct iopoll_ctx *io_ctx;
1158 KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1160 io_ctx->poll_burst_max = nmsg->lmsg.u.ms_result;
1161 if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
1162 io_ctx->poll_each_burst = io_ctx->poll_burst_max;
1163 if (io_ctx->poll_burst > io_ctx->poll_burst_max)
1164 io_ctx->poll_burst = io_ctx->poll_burst_max;
1165 if (io_ctx->residual_burst > io_ctx->poll_burst_max)
1166 io_ctx->residual_burst = io_ctx->poll_burst_max;
1168 netisr_replymsg(&nmsg->base, 0);
1172 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
1174 struct iopoll_ctx *io_ctx = arg1;
1175 struct iopoll_sysctl_netmsg msg;
1179 burst_max = io_ctx->poll_burst_max;
1180 error = sysctl_handle_int(oidp, &burst_max, 0, req);
1181 if (error || req->newptr == NULL)
1183 if (burst_max < MIN_IOPOLL_BURST_MAX)
1184 burst_max = MIN_IOPOLL_BURST_MAX;
1185 else if (burst_max > MAX_IOPOLL_BURST_MAX)
1186 burst_max = MAX_IOPOLL_BURST_MAX;
1188 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1189 0, sysctl_burstmax_handler);
1190 msg.base.lmsg.u.ms_result = burst_max;
1193 return netisr_domsg(&msg.base, io_ctx->poll_cpuid);
1197 sysctl_eachburst_handler(netmsg_t nmsg)
1199 struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1200 struct iopoll_ctx *io_ctx;
1201 uint32_t each_burst;
1204 KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1206 each_burst = nmsg->lmsg.u.ms_result;
1207 if (each_burst > io_ctx->poll_burst_max)
1208 each_burst = io_ctx->poll_burst_max;
1209 else if (each_burst < 1)
1211 io_ctx->poll_each_burst = each_burst;
1213 netisr_replymsg(&nmsg->base, 0);
1217 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1219 struct iopoll_ctx *io_ctx = arg1;
1220 struct iopoll_sysctl_netmsg msg;
1221 uint32_t each_burst;
1224 each_burst = io_ctx->poll_each_burst;
1225 error = sysctl_handle_int(oidp, &each_burst, 0, req);
1226 if (error || req->newptr == NULL)
1229 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1230 0, sysctl_eachburst_handler);
1231 msg.base.lmsg.u.ms_result = each_burst;
1234 return netisr_domsg(&msg.base, io_ctx->poll_cpuid);
1238 iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1239 const struct ifpoll_io *io_rec)
1243 KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1245 if (io_rec->poll_func == NULL)
1249 * Check if there is room.
1251 if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1253 * List full, cannot register more entries.
1254 * This should never happen; if it does, it is probably a
1255 * broken driver trying to register multiple times. Checking
1256 * this at runtime is expensive, and won't solve the problem
1257 * anyways, so just report a few times and then give up.
1259 static int verbose = 10; /* XXX */
1261 kprintf("io poll handlers list full, "
1262 "maybe a broken driver ?\n");
1267 struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1270 rec->serializer = io_rec->serializer;
1271 rec->arg = io_rec->arg;
1272 rec->poll_func = io_rec->poll_func;
1274 io_ctx->poll_handlers++;
1281 iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1285 KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1287 for (i = 0; i < io_ctx->poll_handlers; ++i) {
1288 if (io_ctx->pr[i].ifp == ifp) /* Found it */
1291 if (i == io_ctx->poll_handlers) {
1294 io_ctx->poll_handlers--;
1295 if (i < io_ctx->poll_handlers) {
1296 /* Last entry replaces this one. */
1297 io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1300 if (io_ctx->poll_handlers == 0)
1301 iopoll_reset_state(io_ctx);
1308 poll_comm_init(int cpuid)
1310 struct poll_comm *comm;
1313 comm = kmalloc(sizeof(*comm), M_DEVBUF, M_WAITOK | M_ZERO);
1315 if (ifpoll_stfrac < 1)
1316 ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
1317 if (ifpoll_txfrac < 1)
1318 ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
1320 comm->poll_cpuid = cpuid;
1321 comm->pollhz = poll_comm_pollhz_div(comm, ifpoll_pollhz);
1322 comm->poll_stfrac = ifpoll_stfrac - 1;
1323 comm->poll_txfrac = ifpoll_txfrac - 1;
1325 ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", cpuid);
1327 sysctl_ctx_init(&comm->sysctl_ctx);
1328 comm->sysctl_tree = SYSCTL_ADD_NODE(&comm->sysctl_ctx,
1329 SYSCTL_STATIC_CHILDREN(_net_ifpoll),
1330 OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
1332 SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1333 OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
1334 comm, 0, sysctl_pollhz,
1335 "I", "Device polling frequency");
1338 SYSCTL_ADD_PROC(&comm->sysctl_ctx,
1339 SYSCTL_CHILDREN(comm->sysctl_tree),
1340 OID_AUTO, "status_frac",
1341 CTLTYPE_INT | CTLFLAG_RW,
1342 comm, 0, sysctl_stfrac,
1343 "I", "# of cycles before status is polled");
1345 SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1346 OID_AUTO, "tx_frac", CTLTYPE_INT | CTLFLAG_RW,
1347 comm, 0, sysctl_txfrac,
1348 "I", "# of cycles before TX is polled");
1350 poll_common[cpuid] = comm;
1354 poll_comm_start(int cpuid)
1356 struct poll_comm *comm = poll_common[cpuid];
1357 systimer_func_t func;
1360 * Initialize systimer
1363 func = poll_comm_systimer0;
1365 func = poll_comm_systimer;
1366 systimer_init_periodic_nq(&comm->pollclock, func, comm, 1);
1370 _poll_comm_systimer(struct poll_comm *comm)
1372 iopoll_clock(rxpoll_context[comm->poll_cpuid]);
1373 if (comm->txfrac_count-- == 0) {
1374 comm->txfrac_count = comm->poll_txfrac;
1375 iopoll_clock(txpoll_context[comm->poll_cpuid]);
1380 poll_comm_systimer0(systimer_t info, int in_ipi __unused,
1381 struct intrframe *frame __unused)
1383 struct poll_comm *comm = info->data;
1384 globaldata_t gd = mycpu;
1386 KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid == 0);
1390 if (comm->stfrac_count-- == 0) {
1391 comm->stfrac_count = comm->poll_stfrac;
1392 stpoll_clock(&stpoll_context);
1394 _poll_comm_systimer(comm);
1400 poll_comm_systimer(systimer_t info, int in_ipi __unused,
1401 struct intrframe *frame __unused)
1403 struct poll_comm *comm = info->data;
1404 globaldata_t gd = mycpu;
1406 KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid != 0);
1409 _poll_comm_systimer(comm);
1414 poll_comm_adjust_pollhz(struct poll_comm *comm)
1419 KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
1422 * If there is no polling handler registered, set systimer
1423 * frequency to the lowest value. Polling systimer frequency
1424 * will be adjusted to the requested value, once there are
1425 * registered handlers.
1427 handlers = rxpoll_context[mycpuid]->poll_handlers +
1428 txpoll_context[mycpuid]->poll_handlers;
1429 if (comm->poll_cpuid == 0)
1430 handlers += stpoll_context.poll_handlers;
1432 pollhz = comm->pollhz;
1433 systimer_adjust_periodic(&comm->pollclock, pollhz);
1437 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
1439 struct poll_comm *comm = arg1;
1440 struct netmsg_base nmsg;
1443 phz = poll_comm_pollhz_conv(comm, comm->pollhz);
1444 error = sysctl_handle_int(oidp, &phz, 0, req);
1445 if (error || req->newptr == NULL)
1449 else if (phz > IFPOLL_FREQ_MAX)
1450 phz = IFPOLL_FREQ_MAX;
1452 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1453 0, sysctl_pollhz_handler);
1454 nmsg.lmsg.u.ms_result = phz;
1456 return netisr_domsg(&nmsg, comm->poll_cpuid);
1460 sysctl_pollhz_handler(netmsg_t nmsg)
1462 struct poll_comm *comm = poll_common[mycpuid];
1464 KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
1466 /* Save polling frequency */
1467 comm->pollhz = poll_comm_pollhz_div(comm, nmsg->lmsg.u.ms_result);
1470 * Adjust cached pollhz
1472 rxpoll_context[mycpuid]->pollhz = comm->pollhz;
1473 txpoll_context[mycpuid]->pollhz =
1474 comm->pollhz / (comm->poll_txfrac + 1);
1477 * Adjust polling frequency
1479 poll_comm_adjust_pollhz(comm);
1481 netisr_replymsg(&nmsg->base, 0);
1485 sysctl_stfrac(SYSCTL_HANDLER_ARGS)
1487 struct poll_comm *comm = arg1;
1488 struct netmsg_base nmsg;
1491 KKASSERT(comm->poll_cpuid == 0);
1493 stfrac = comm->poll_stfrac + 1;
1494 error = sysctl_handle_int(oidp, &stfrac, 0, req);
1495 if (error || req->newptr == NULL)
1500 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1501 0, sysctl_stfrac_handler);
1502 nmsg.lmsg.u.ms_result = stfrac - 1;
1504 return netisr_domsg(&nmsg, comm->poll_cpuid);
1508 sysctl_stfrac_handler(netmsg_t nmsg)
1510 struct poll_comm *comm = poll_common[mycpuid];
1511 int stfrac = nmsg->lmsg.u.ms_result;
1513 KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
1516 comm->poll_stfrac = stfrac;
1517 if (comm->stfrac_count > comm->poll_stfrac)
1518 comm->stfrac_count = comm->poll_stfrac;
1521 netisr_replymsg(&nmsg->base, 0);
1525 sysctl_txfrac(SYSCTL_HANDLER_ARGS)
1527 struct poll_comm *comm = arg1;
1528 struct netmsg_base nmsg;
1531 txfrac = comm->poll_txfrac + 1;
1532 error = sysctl_handle_int(oidp, &txfrac, 0, req);
1533 if (error || req->newptr == NULL)
1538 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1539 0, sysctl_txfrac_handler);
1540 nmsg.lmsg.u.ms_result = txfrac - 1;
1542 return netisr_domsg(&nmsg, comm->poll_cpuid);
1546 sysctl_txfrac_handler(netmsg_t nmsg)
1548 struct poll_comm *comm = poll_common[mycpuid];
1549 int txfrac = nmsg->lmsg.u.ms_result;
1551 KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
1554 comm->poll_txfrac = txfrac;
1555 if (comm->txfrac_count > comm->poll_txfrac)
1556 comm->txfrac_count = comm->poll_txfrac;
1559 netisr_replymsg(&nmsg->base, 0);
1563 ifpoll_compat_setup(struct ifpoll_compat *cp,
1564 struct sysctl_ctx_list *sysctl_ctx,
1565 struct sysctl_oid *sysctl_tree,
1566 int unit, struct lwkt_serialize *slz)
1568 cp->ifpc_stcount = 0;
1569 cp->ifpc_stfrac = ((poll_common[0]->poll_stfrac + 1) *
1570 howmany(IOPOLL_BURST_MAX, IOPOLL_EACH_BURST)) - 1;
1572 cp->ifpc_cpuid = unit % netisr_ncpus;
1573 cp->ifpc_serializer = slz;
1575 if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1576 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1577 OID_AUTO, "npoll_stfrac", CTLTYPE_INT | CTLFLAG_RW,
1578 cp, 0, sysctl_compat_npoll_stfrac, "I",
1579 "polling status frac");
1580 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1581 OID_AUTO, "npoll_cpuid", CTLTYPE_INT | CTLFLAG_RW,
1582 cp, 0, sysctl_compat_npoll_cpuid, "I",
1588 sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS)
1590 struct ifpoll_compat *cp = arg1;
1591 int error = 0, stfrac;
1593 lwkt_serialize_enter(cp->ifpc_serializer);
1595 stfrac = cp->ifpc_stfrac + 1;
1596 error = sysctl_handle_int(oidp, &stfrac, 0, req);
1597 if (!error && req->newptr != NULL) {
1601 cp->ifpc_stfrac = stfrac - 1;
1602 if (cp->ifpc_stcount > cp->ifpc_stfrac)
1603 cp->ifpc_stcount = cp->ifpc_stfrac;
1607 lwkt_serialize_exit(cp->ifpc_serializer);
1612 sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS)
1614 struct ifpoll_compat *cp = arg1;
1615 int error = 0, cpuid;
1617 lwkt_serialize_enter(cp->ifpc_serializer);
1619 cpuid = cp->ifpc_cpuid;
1620 error = sysctl_handle_int(oidp, &cpuid, 0, req);
1621 if (!error && req->newptr != NULL) {
1622 if (cpuid < 0 || cpuid >= netisr_ncpus)
1625 cp->ifpc_cpuid = cpuid;
1628 lwkt_serialize_exit(cp->ifpc_serializer);