ifpoll: Don't limit number of CPUs that perform polling
[dragonfly.git] / sys / net / if_poll.c
1 /*-
2  * Copyright (c) 2001-2002 Luigi Rizzo
3  *
4  * Supported by: the Xorp Project (www.xorp.org)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
28  */
29
30 #include "opt_ifpoll.h"
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/ktr.h>
35 #include <sys/malloc.h>
36 #include <sys/serialize.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39
40 #include <sys/thread2.h>
41 #include <sys/msgport2.h>
42
43 #include <machine/atomic.h>
44 #include <machine/clock.h>
45 #include <machine/smp.h>
46
47 #include <net/if.h>
48 #include <net/if_poll.h>
49 #include <net/netmsg2.h>
50
51 /*
52  * Polling support for network device drivers.
53  *
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_qpoll 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)
66  *
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
71  * piggyback (XXX).
72  *
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_qpoll) will be called even
76  * if interface is not marked with 'IFF_RUNNING'.
77  *
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
83  * polling handler.
84  *
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
89  * count).
90  *
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).
96  *
97  * Other parameters can should be left to their default values.
98  * The following constraints hold
99  *
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
103  */
104
105 #define IFPOLL_LIST_LEN         128
106 #define IFPOLL_FREQ_MAX         30000
107
108 #define MIN_IOPOLL_BURST_MAX    10
109 #define MAX_IOPOLL_BURST_MAX    1000
110 #define IOPOLL_BURST_MAX        150     /* good for 100Mbit net and HZ=1000 */
111
112 #define IOPOLL_EACH_BURST       5
113
114 #define IFPOLL_FREQ_DEFAULT     2000
115
116 #define IFPOLL_TXFRAC_DEFAULT   1       /* 1/2 of the pollhz */
117 #define IFPOLL_STFRAC_DEFAULT   19      /* 1/20 of the pollhz */
118
119 #define IFPOLL_RX               0x1
120 #define IFPOLL_TX               0x2
121
122 union ifpoll_time {
123         struct timeval          tv;
124         uint64_t                tsc;
125 };
126
127 struct iopoll_rec {
128         struct lwkt_serialize   *serializer;
129         struct ifnet            *ifp;
130         void                    *arg;
131         ifpoll_iofn_t           poll_func;
132 };
133
134 struct iopoll_ctx {
135         union ifpoll_time       prev_t;
136         uint32_t                short_ticks;            /* statistics */
137         uint32_t                lost_polls;             /* statistics */
138         uint32_t                suspect;                /* statistics */
139         uint32_t                stalled;                /* statistics */
140         uint32_t                pending_polls;          /* state */
141
142         struct netmsg_base      poll_netmsg;
143
144         int                     poll_cpuid;
145         int                     pollhz;
146         uint32_t                phase;                  /* state */
147         int                     residual_burst;         /* state */
148         uint32_t                poll_each_burst;        /* tunable */
149         union ifpoll_time       poll_start_t;           /* state */
150
151         uint32_t                poll_handlers; /* next free entry in pr[]. */
152         struct iopoll_rec       pr[IFPOLL_LIST_LEN];
153
154         struct netmsg_base      poll_more_netmsg;
155
156         uint32_t                poll_burst;             /* state */
157         uint32_t                poll_burst_max;         /* tunable */
158         uint32_t                user_frac;              /* tunable */
159         uint32_t                kern_frac;              /* state */
160
161         struct sysctl_ctx_list  poll_sysctl_ctx;
162         struct sysctl_oid       *poll_sysctl_tree;
163 } __cachealign;
164
165 struct poll_comm {
166         struct systimer         pollclock;
167         int                     poll_cpuid;
168
169         int                     stfrac_count;           /* state */
170         int                     poll_stfrac;            /* tunable */
171
172         int                     txfrac_count;           /* state */
173         int                     poll_txfrac;            /* tunable */
174
175         int                     pollhz;                 /* tunable */
176
177         struct sysctl_ctx_list  sysctl_ctx;
178         struct sysctl_oid       *sysctl_tree;
179 } __cachealign;
180
181 struct stpoll_rec {
182         struct lwkt_serialize   *serializer;
183         struct ifnet            *ifp;
184         ifpoll_stfn_t           status_func;
185 };
186
187 struct stpoll_ctx {
188         struct netmsg_base      poll_netmsg;
189
190         int                     pollhz;
191
192         uint32_t                poll_handlers; /* next free entry in pr[]. */
193         struct stpoll_rec       pr[IFPOLL_LIST_LEN];
194
195         struct sysctl_ctx_list  poll_sysctl_ctx;
196         struct sysctl_oid       *poll_sysctl_tree;
197 };
198
199 struct iopoll_sysctl_netmsg {
200         struct netmsg_base      base;
201         struct iopoll_ctx       *ctx;
202 };
203
204 void            ifpoll_init_pcpu(int);
205 static void     ifpoll_register_handler(netmsg_t);
206 static void     ifpoll_deregister_handler(netmsg_t);
207
208 /*
209  * Status polling
210  */
211 static void     stpoll_init(void);
212 static void     stpoll_handler(netmsg_t);
213 static void     stpoll_clock(struct stpoll_ctx *);
214 static int      stpoll_register(struct ifnet *, const struct ifpoll_status *);
215 static int      stpoll_deregister(struct ifnet *);
216
217 /*
218  * RX/TX polling
219  */
220 static struct iopoll_ctx *iopoll_ctx_create(int, int);
221 static void     iopoll_init(int);
222 static void     iopoll_handler(netmsg_t);
223 static void     iopollmore_handler(netmsg_t);
224 static void     iopoll_clock(struct iopoll_ctx *);
225 static int      iopoll_register(struct ifnet *, struct iopoll_ctx *,
226                     const struct ifpoll_io *);
227 static int      iopoll_deregister(struct ifnet *, struct iopoll_ctx *);
228
229 static void     iopoll_add_sysctl(struct sysctl_ctx_list *,
230                     struct sysctl_oid_list *, struct iopoll_ctx *);
231 static void     sysctl_burstmax_handler(netmsg_t);
232 static int      sysctl_burstmax(SYSCTL_HANDLER_ARGS);
233 static void     sysctl_eachburst_handler(netmsg_t);
234 static int      sysctl_eachburst(SYSCTL_HANDLER_ARGS);
235
236 /*
237  * Common functions
238  */
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
251 static struct stpoll_ctx        stpoll_context;
252 static struct poll_comm         *poll_common[MAXCPU];
253 static struct iopoll_ctx        *rxpoll_context[MAXCPU];
254 static struct iopoll_ctx        *txpoll_context[MAXCPU];
255
256 SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
257             "Network device polling parameters");
258
259 static int      iopoll_burst_max = IOPOLL_BURST_MAX;
260 static int      iopoll_each_burst = IOPOLL_EACH_BURST;
261
262 static int      ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
263 static int      ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
264 static int      ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
265
266 TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
267 TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
268 TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
269 TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
270 TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
271
272 static __inline void
273 ifpoll_sendmsg_oncpu(netmsg_t msg)
274 {
275         if (msg->lmsg.ms_flags & MSGF_DONE)
276                 lwkt_sendmsg(netisr_portfn(mycpuid), &msg->lmsg);
277 }
278
279 static __inline void
280 sched_stpoll(struct stpoll_ctx *st_ctx)
281 {
282         ifpoll_sendmsg_oncpu((netmsg_t)&st_ctx->poll_netmsg);
283 }
284
285 static __inline void
286 sched_iopoll(struct iopoll_ctx *io_ctx)
287 {
288         ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_netmsg);
289 }
290
291 static __inline void
292 sched_iopollmore(struct iopoll_ctx *io_ctx)
293 {
294         ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_more_netmsg);
295 }
296
297 static __inline void
298 ifpoll_time_get(union ifpoll_time *t)
299 {
300         if (tsc_present)
301                 t->tsc = rdtsc();
302         else
303                 microuptime(&t->tv);
304 }
305
306 /* Return time diff in us */
307 static __inline int
308 ifpoll_time_diff(const union ifpoll_time *s, const union ifpoll_time *e)
309 {
310         if (tsc_present) {
311                 return (((e->tsc - s->tsc) * 1000000) / tsc_frequency);
312         } else {
313                 return ((e->tv.tv_usec - s->tv.tv_usec) +
314                         (e->tv.tv_sec - s->tv.tv_sec) * 1000000);
315         }
316 }
317
318 /*
319  * Initialize per-cpu qpolling(4) context.  Called from kern_clock.c:
320  */
321 void
322 ifpoll_init_pcpu(int cpuid)
323 {
324         if (cpuid >= ncpus2)
325                 return;
326
327         poll_comm_init(cpuid);
328
329         if (cpuid == 0)
330                 stpoll_init();
331         iopoll_init(cpuid);
332
333         poll_comm_start(cpuid);
334 }
335
336 int
337 ifpoll_register(struct ifnet *ifp)
338 {
339         struct ifpoll_info *info;
340         struct netmsg_base nmsg;
341         int error;
342
343         if (ifp->if_qpoll == NULL) {
344                 /* Device does not support polling */
345                 return EOPNOTSUPP;
346         }
347
348         info = kmalloc(sizeof(*info), M_TEMP, M_WAITOK | M_ZERO);
349
350         /*
351          * Attempt to register.  Interlock with IFF_NPOLLING.
352          */
353
354         ifnet_serialize_all(ifp);
355
356         if (ifp->if_flags & IFF_NPOLLING) {
357                 /* Already polling */
358                 ifnet_deserialize_all(ifp);
359                 kfree(info, M_TEMP);
360                 return EBUSY;
361         }
362
363         info->ifpi_ifp = ifp;
364
365         ifp->if_flags |= IFF_NPOLLING;
366         ifp->if_qpoll(ifp, info);
367
368         ifnet_deserialize_all(ifp);
369
370         netmsg_init(&nmsg, NULL, &curthread->td_msgport,
371                     0, ifpoll_register_handler);
372         nmsg.lmsg.u.ms_resultp = info;
373
374         error = lwkt_domsg(netisr_portfn(0), &nmsg.lmsg, 0);
375         if (error) {
376                 if (!ifpoll_deregister(ifp)) {
377                         if_printf(ifp, "ifpoll_register: "
378                                   "ifpoll_deregister failed!\n");
379                 }
380         }
381
382         kfree(info, M_TEMP);
383         return error;
384 }
385
386 int
387 ifpoll_deregister(struct ifnet *ifp)
388 {
389         struct netmsg_base nmsg;
390         int error;
391
392         if (ifp->if_qpoll == NULL)
393                 return EOPNOTSUPP;
394
395         ifnet_serialize_all(ifp);
396
397         if ((ifp->if_flags & IFF_NPOLLING) == 0) {
398                 ifnet_deserialize_all(ifp);
399                 return EINVAL;
400         }
401         ifp->if_flags &= ~IFF_NPOLLING;
402
403         ifnet_deserialize_all(ifp);
404
405         netmsg_init(&nmsg, NULL, &curthread->td_msgport,
406                     0, ifpoll_deregister_handler);
407         nmsg.lmsg.u.ms_resultp = ifp;
408
409         error = lwkt_domsg(netisr_portfn(0), &nmsg.lmsg, 0);
410         if (!error) {
411                 ifnet_serialize_all(ifp);
412                 ifp->if_qpoll(ifp, NULL);
413                 ifnet_deserialize_all(ifp);
414         }
415         return error;
416 }
417
418 static void
419 ifpoll_register_handler(netmsg_t nmsg)
420 {
421         const struct ifpoll_info *info = nmsg->lmsg.u.ms_resultp;
422         int cpuid = mycpuid, nextcpu;
423         int error;
424
425         KKASSERT(cpuid < ncpus2);
426         KKASSERT(&curthread->td_msgport == netisr_portfn(cpuid));
427
428         if (cpuid == 0) {
429                 error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
430                 if (error)
431                         goto failed;
432         }
433
434         error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
435                                 &info->ifpi_rx[cpuid]);
436         if (error)
437                 goto failed;
438
439         error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
440                                 &info->ifpi_tx[cpuid]);
441         if (error)
442                 goto failed;
443
444         /* Adjust polling frequency, after all registration is done */
445         poll_comm_adjust_pollhz(poll_common[cpuid]);
446
447         nextcpu = cpuid + 1;
448         if (nextcpu < ncpus2)
449                 lwkt_forwardmsg(netisr_portfn(nextcpu), &nmsg->lmsg);
450         else
451                 lwkt_replymsg(&nmsg->lmsg, 0);
452         return;
453 failed:
454         lwkt_replymsg(&nmsg->lmsg, error);
455 }
456
457 static void
458 ifpoll_deregister_handler(netmsg_t nmsg)
459 {
460         struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
461         int cpuid = mycpuid, nextcpu;
462
463         KKASSERT(cpuid < ncpus2);
464         KKASSERT(&curthread->td_msgport == netisr_portfn(cpuid));
465
466         /* Ignore errors */
467         if (cpuid == 0)
468                 stpoll_deregister(ifp);
469         iopoll_deregister(ifp, rxpoll_context[cpuid]);
470         iopoll_deregister(ifp, txpoll_context[cpuid]);
471
472         /* Adjust polling frequency, after all deregistration is done */
473         poll_comm_adjust_pollhz(poll_common[cpuid]);
474
475         nextcpu = cpuid + 1;
476         if (nextcpu < ncpus2)
477                 lwkt_forwardmsg(netisr_portfn(nextcpu), &nmsg->lmsg);
478         else
479                 lwkt_replymsg(&nmsg->lmsg, 0);
480 }
481
482 static void
483 stpoll_init(void)
484 {
485         struct stpoll_ctx *st_ctx = &stpoll_context;
486         const struct poll_comm *comm = poll_common[0];
487
488         st_ctx->pollhz = comm->pollhz / (comm->poll_stfrac + 1);
489
490         sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
491         st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
492                                    SYSCTL_CHILDREN(comm->sysctl_tree),
493                                    OID_AUTO, "status", CTLFLAG_RD, 0, "");
494
495         SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
496                         SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
497                         OID_AUTO, "handlers", CTLFLAG_RD,
498                         &st_ctx->poll_handlers, 0,
499                         "Number of registered status poll handlers");
500
501         netmsg_init(&st_ctx->poll_netmsg, NULL, &netisr_adone_rport,
502                     0, stpoll_handler);
503 }
504
505 /*
506  * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
507  * once per polling systimer tick.
508  */
509 static void
510 stpoll_handler(netmsg_t msg)
511 {
512         struct stpoll_ctx *st_ctx = &stpoll_context;
513         struct thread *td = curthread;
514         int i;
515
516         KKASSERT(&td->td_msgport == netisr_portfn(0));
517
518         crit_enter_quick(td);
519
520         /* Reply ASAP */
521         lwkt_replymsg(&msg->lmsg, 0);
522
523         if (st_ctx->poll_handlers == 0) {
524                 crit_exit_quick(td);
525                 return;
526         }
527
528         for (i = 0; i < st_ctx->poll_handlers; ++i) {
529                 const struct stpoll_rec *rec = &st_ctx->pr[i];
530                 struct ifnet *ifp = rec->ifp;
531
532                 if (!lwkt_serialize_try(rec->serializer))
533                         continue;
534
535                 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
536                     (IFF_RUNNING | IFF_NPOLLING))
537                         rec->status_func(ifp, st_ctx->pollhz);
538
539                 lwkt_serialize_exit(rec->serializer);
540         }
541
542         crit_exit_quick(td);
543 }
544
545 /*
546  * Hook from status poll systimer.  Tries to schedule an status poll.
547  * NOTE: Caller should hold critical section.
548  */
549 static void
550 stpoll_clock(struct stpoll_ctx *st_ctx)
551 {
552         KKASSERT(mycpuid == 0);
553
554         if (st_ctx->poll_handlers == 0)
555                 return;
556         sched_stpoll(st_ctx);
557 }
558
559 static int
560 stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
561 {
562         struct stpoll_ctx *st_ctx = &stpoll_context;
563         int error;
564
565         KKASSERT(&curthread->td_msgport == netisr_portfn(0));
566
567         if (st_rec->status_func == NULL)
568                 return 0;
569
570         /*
571          * Check if there is room.
572          */
573         if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
574                 /*
575                  * List full, cannot register more entries.
576                  * This should never happen; if it does, it is probably a
577                  * broken driver trying to register multiple times. Checking
578                  * this at runtime is expensive, and won't solve the problem
579                  * anyways, so just report a few times and then give up.
580                  */
581                 static int verbose = 10; /* XXX */
582
583                 if (verbose > 0) {
584                         kprintf("status poll handlers list full, "
585                                 "maybe a broken driver ?\n");
586                         verbose--;
587                 }
588                 error = ENOENT;
589         } else {
590                 struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
591
592                 rec->ifp = ifp;
593                 rec->serializer = st_rec->serializer;
594                 rec->status_func = st_rec->status_func;
595
596                 st_ctx->poll_handlers++;
597                 error = 0;
598         }
599         return error;
600 }
601
602 static int
603 stpoll_deregister(struct ifnet *ifp)
604 {
605         struct stpoll_ctx *st_ctx = &stpoll_context;
606         int i, error;
607
608         KKASSERT(&curthread->td_msgport == netisr_portfn(0));
609
610         for (i = 0; i < st_ctx->poll_handlers; ++i) {
611                 if (st_ctx->pr[i].ifp == ifp) /* Found it */
612                         break;
613         }
614         if (i == st_ctx->poll_handlers) {
615                 kprintf("stpoll_deregister: ifp not found!!!\n");
616                 error = ENOENT;
617         } else {
618                 st_ctx->poll_handlers--;
619                 if (i < st_ctx->poll_handlers) {
620                         /* Last entry replaces this one. */
621                         st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
622                 }
623                 error = 0;
624         }
625         return error;
626 }
627
628 static __inline void
629 iopoll_reset_state(struct iopoll_ctx *io_ctx)
630 {
631         crit_enter();
632         io_ctx->poll_burst = 5;
633         io_ctx->pending_polls = 0;
634         io_ctx->residual_burst = 0;
635         io_ctx->phase = 0;
636         io_ctx->kern_frac = 0;
637         bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
638         bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
639         crit_exit();
640 }
641
642 static void
643 iopoll_init(int cpuid)
644 {
645         KKASSERT(cpuid < ncpus2);
646
647         rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
648         txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
649 }
650
651 static struct iopoll_ctx *
652 iopoll_ctx_create(int cpuid, int poll_type)
653 {
654         struct poll_comm *comm;
655         struct iopoll_ctx *io_ctx;
656         const char *poll_type_str;
657
658         KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
659
660         /*
661          * Make sure that tunables are in sane state
662          */
663         if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
664                 iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
665         else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
666                 iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
667
668         if (iopoll_each_burst > iopoll_burst_max)
669                 iopoll_each_burst = iopoll_burst_max;
670
671         comm = poll_common[cpuid];
672
673         /*
674          * Create the per-cpu polling context
675          */
676         io_ctx = kmalloc(sizeof(*io_ctx), M_DEVBUF, M_WAITOK | M_ZERO);
677
678         io_ctx->poll_each_burst = iopoll_each_burst;
679         io_ctx->poll_burst_max = iopoll_burst_max;
680         io_ctx->user_frac = 50;
681         if (poll_type == IFPOLL_RX)
682                 io_ctx->pollhz = comm->pollhz;
683         else
684                 io_ctx->pollhz = comm->pollhz / (comm->poll_txfrac + 1);
685         io_ctx->poll_cpuid = cpuid;
686         iopoll_reset_state(io_ctx);
687
688         netmsg_init(&io_ctx->poll_netmsg, NULL, &netisr_adone_rport,
689                     0, iopoll_handler);
690         io_ctx->poll_netmsg.lmsg.u.ms_resultp = io_ctx;
691
692         netmsg_init(&io_ctx->poll_more_netmsg, NULL, &netisr_adone_rport,
693                     0, iopollmore_handler);
694         io_ctx->poll_more_netmsg.lmsg.u.ms_resultp = io_ctx;
695
696         /*
697          * Initialize per-cpu sysctl nodes
698          */
699         if (poll_type == IFPOLL_RX)
700                 poll_type_str = "rx";
701         else
702                 poll_type_str = "tx";
703
704         sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
705         io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
706                                    SYSCTL_CHILDREN(comm->sysctl_tree),
707                                    OID_AUTO, poll_type_str, CTLFLAG_RD, 0, "");
708         iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
709                           SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx);
710
711         return io_ctx;
712 }
713
714 /*
715  * Hook from iopoll systimer.  Tries to schedule an iopoll, but keeps
716  * track of lost ticks due to the previous handler taking too long.
717  * Normally, this should not happen, because polling handler should
718  * run for a short time.  However, in some cases (e.g. when there are
719  * changes in link status etc.) the drivers take a very long time
720  * (even in the order of milliseconds) to reset and reconfigure the
721  * device, causing apparent lost polls.
722  *
723  * The first part of the code is just for debugging purposes, and tries
724  * to count how often hardclock ticks are shorter than they should,
725  * meaning either stray interrupts or delayed events.
726  *
727  * WARNING! called from fastint or IPI, the MP lock might not be held.
728  * NOTE: Caller should hold critical section.
729  */
730 static void
731 iopoll_clock(struct iopoll_ctx *io_ctx)
732 {
733         union ifpoll_time t;
734         int delta;
735
736         KKASSERT(mycpuid == io_ctx->poll_cpuid);
737
738         if (io_ctx->poll_handlers == 0)
739                 return;
740
741         ifpoll_time_get(&t);
742         delta = ifpoll_time_diff(&io_ctx->prev_t, &t);
743         if (delta * io_ctx->pollhz < 500000)
744                 io_ctx->short_ticks++;
745         else
746                 io_ctx->prev_t = t;
747
748         if (io_ctx->pending_polls > 100) {
749                 /*
750                  * Too much, assume it has stalled (not always true
751                  * see comment above).
752                  */
753                 io_ctx->stalled++;
754                 io_ctx->pending_polls = 0;
755                 io_ctx->phase = 0;
756         }
757
758         if (io_ctx->phase <= 2) {
759                 if (io_ctx->phase != 0)
760                         io_ctx->suspect++;
761                 io_ctx->phase = 1;
762                 sched_iopoll(io_ctx);
763                 io_ctx->phase = 2;
764         }
765         if (io_ctx->pending_polls++ > 0)
766                 io_ctx->lost_polls++;
767 }
768
769 /*
770  * iopoll_handler is scheduled by sched_iopoll when appropriate, typically
771  * once per polling systimer tick.
772  *
773  * Note that the message is replied immediately in order to allow a new
774  * ISR to be scheduled in the handler.
775  */
776 static void
777 iopoll_handler(netmsg_t msg)
778 {
779         struct iopoll_ctx *io_ctx;
780         struct thread *td = curthread;
781         int i, cycles;
782
783         io_ctx = msg->lmsg.u.ms_resultp;
784         KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
785
786         crit_enter_quick(td);
787
788         /* Reply ASAP */
789         lwkt_replymsg(&msg->lmsg, 0);
790
791         if (io_ctx->poll_handlers == 0) {
792                 crit_exit_quick(td);
793                 return;
794         }
795
796         io_ctx->phase = 3;
797         if (io_ctx->residual_burst == 0) {
798                 /* First call in this tick */
799                 ifpoll_time_get(&io_ctx->poll_start_t);
800                 io_ctx->residual_burst = io_ctx->poll_burst;
801         }
802         cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
803                  io_ctx->residual_burst : io_ctx->poll_each_burst;
804         io_ctx->residual_burst -= cycles;
805
806         for (i = 0; i < io_ctx->poll_handlers; i++) {
807                 const struct iopoll_rec *rec = &io_ctx->pr[i];
808                 struct ifnet *ifp = rec->ifp;
809
810                 if (!lwkt_serialize_try(rec->serializer))
811                         continue;
812
813                 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
814                     (IFF_RUNNING | IFF_NPOLLING))
815                         rec->poll_func(ifp, rec->arg, cycles);
816
817                 lwkt_serialize_exit(rec->serializer);
818         }
819
820         /*
821          * Do a quick exit/enter to catch any higher-priority
822          * interrupt sources.
823          */
824         crit_exit_quick(td);
825         crit_enter_quick(td);
826
827         sched_iopollmore(io_ctx);
828         io_ctx->phase = 4;
829
830         crit_exit_quick(td);
831 }
832
833 /*
834  * iopollmore_handler is called after other netisr's, possibly scheduling
835  * another iopoll_handler call, or adapting the burst size for the next cycle.
836  *
837  * It is very bad to fetch large bursts of packets from a single card at once,
838  * because the burst could take a long time to be completely processed leading
839  * to unfairness.  To reduce the problem, and also to account better for time
840  * spent in network-related processing, we split the burst in smaller chunks
841  * of fixed size, giving control to the other netisr's between chunks.  This
842  * helps in improving the fairness, reducing livelock and accounting for the
843  * work performed in low level handling.
844  */
845 static void
846 iopollmore_handler(netmsg_t msg)
847 {
848         struct thread *td = curthread;
849         struct iopoll_ctx *io_ctx;
850         union ifpoll_time t;
851         int kern_load;
852         uint32_t pending_polls;
853
854         io_ctx = msg->lmsg.u.ms_resultp;
855         KKASSERT(&td->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
856
857         crit_enter_quick(td);
858
859         /* Replay ASAP */
860         lwkt_replymsg(&msg->lmsg, 0);
861
862         if (io_ctx->poll_handlers == 0) {
863                 crit_exit_quick(td);
864                 return;
865         }
866
867         io_ctx->phase = 5;
868         if (io_ctx->residual_burst > 0) {
869                 sched_iopoll(io_ctx);
870                 crit_exit_quick(td);
871                 /* Will run immediately on return, followed by netisrs */
872                 return;
873         }
874
875         /* Here we can account time spent in iopoll's in this tick */
876         ifpoll_time_get(&t);
877         kern_load = ifpoll_time_diff(&io_ctx->poll_start_t, &t);
878         kern_load = (kern_load * io_ctx->pollhz) / 10000; /* 0..100 */
879         io_ctx->kern_frac = kern_load;
880
881         if (kern_load > (100 - io_ctx->user_frac)) {
882                 /* Try decrease ticks */
883                 if (io_ctx->poll_burst > 1)
884                         io_ctx->poll_burst--;
885         } else {
886                 if (io_ctx->poll_burst < io_ctx->poll_burst_max)
887                         io_ctx->poll_burst++;
888         }
889
890         io_ctx->pending_polls--;
891         pending_polls = io_ctx->pending_polls;
892
893         if (pending_polls == 0) {
894                 /* We are done */
895                 io_ctx->phase = 0;
896         } else {
897                 /*
898                  * Last cycle was long and caused us to miss one or more
899                  * hardclock ticks.  Restart processing again, but slightly
900                  * reduce the burst size to prevent that this happens again.
901                  */
902                 io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
903                 if (io_ctx->poll_burst < 1)
904                         io_ctx->poll_burst = 1;
905                 sched_iopoll(io_ctx);
906                 io_ctx->phase = 6;
907         }
908
909         crit_exit_quick(td);
910 }
911
912 static void
913 iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
914                   struct iopoll_ctx *io_ctx)
915 {
916         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
917                         CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
918                         "IU", "Max Polling burst size");
919
920         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
921                         CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
922                         "IU", "Max size of each burst");
923
924         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
925                         &io_ctx->phase, 0, "Polling phase");
926
927         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
928                         &io_ctx->suspect, 0, "suspect event");
929
930         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
931                         &io_ctx->stalled, 0, "potential stalls");
932
933         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
934                         &io_ctx->poll_burst, 0, "Current polling burst size");
935
936         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
937                         &io_ctx->user_frac, 0,
938                         "Desired user fraction of cpu time");
939
940         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "kern_frac", CTLFLAG_RD,
941                         &io_ctx->kern_frac, 0,
942                         "Kernel fraction of cpu time");
943
944         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
945                         &io_ctx->short_ticks, 0,
946                         "Hardclock ticks shorter than they should be");
947
948         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
949                         &io_ctx->lost_polls, 0,
950                         "How many times we would have lost a poll tick");
951
952         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
953                         &io_ctx->pending_polls, 0, "Do we need to poll again");
954
955         SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
956                        &io_ctx->residual_burst, 0,
957                        "# of residual cycles in burst");
958
959         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
960                         &io_ctx->poll_handlers, 0,
961                         "Number of registered poll handlers");
962 }
963
964 static void
965 sysctl_burstmax_handler(netmsg_t nmsg)
966 {
967         struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
968         struct iopoll_ctx *io_ctx;
969
970         io_ctx = msg->ctx;
971         KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
972
973         io_ctx->poll_burst_max = nmsg->lmsg.u.ms_result;
974         if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
975                 io_ctx->poll_each_burst = io_ctx->poll_burst_max;
976         if (io_ctx->poll_burst > io_ctx->poll_burst_max)
977                 io_ctx->poll_burst = io_ctx->poll_burst_max;
978         if (io_ctx->residual_burst > io_ctx->poll_burst_max)
979                 io_ctx->residual_burst = io_ctx->poll_burst_max;
980
981         lwkt_replymsg(&nmsg->lmsg, 0);
982 }
983
984 static int
985 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
986 {
987         struct iopoll_ctx *io_ctx = arg1;
988         struct iopoll_sysctl_netmsg msg;
989         uint32_t burst_max;
990         int error;
991
992         burst_max = io_ctx->poll_burst_max;
993         error = sysctl_handle_int(oidp, &burst_max, 0, req);
994         if (error || req->newptr == NULL)
995                 return error;
996         if (burst_max < MIN_IOPOLL_BURST_MAX)
997                 burst_max = MIN_IOPOLL_BURST_MAX;
998         else if (burst_max > MAX_IOPOLL_BURST_MAX)
999                 burst_max = MAX_IOPOLL_BURST_MAX;
1000
1001         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1002                     0, sysctl_burstmax_handler);
1003         msg.base.lmsg.u.ms_result = burst_max;
1004         msg.ctx = io_ctx;
1005
1006         return lwkt_domsg(netisr_portfn(io_ctx->poll_cpuid), &msg.base.lmsg, 0);
1007 }
1008
1009 static void
1010 sysctl_eachburst_handler(netmsg_t nmsg)
1011 {
1012         struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1013         struct iopoll_ctx *io_ctx;
1014         uint32_t each_burst;
1015
1016         io_ctx = msg->ctx;
1017         KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1018
1019         each_burst = nmsg->lmsg.u.ms_result;
1020         if (each_burst > io_ctx->poll_burst_max)
1021                 each_burst = io_ctx->poll_burst_max;
1022         else if (each_burst < 1)
1023                 each_burst = 1;
1024         io_ctx->poll_each_burst = each_burst;
1025
1026         lwkt_replymsg(&nmsg->lmsg, 0);
1027 }
1028
1029 static int
1030 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1031 {
1032         struct iopoll_ctx *io_ctx = arg1;
1033         struct iopoll_sysctl_netmsg msg;
1034         uint32_t each_burst;
1035         int error;
1036
1037         each_burst = io_ctx->poll_each_burst;
1038         error = sysctl_handle_int(oidp, &each_burst, 0, req);
1039         if (error || req->newptr == NULL)
1040                 return error;
1041
1042         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1043                     0, sysctl_eachburst_handler);
1044         msg.base.lmsg.u.ms_result = each_burst;
1045         msg.ctx = io_ctx;
1046
1047         return lwkt_domsg(netisr_portfn(io_ctx->poll_cpuid), &msg.base.lmsg, 0);
1048 }
1049
1050 static int
1051 iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1052                 const struct ifpoll_io *io_rec)
1053 {
1054         int error;
1055
1056         KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1057
1058         if (io_rec->poll_func == NULL)
1059                 return 0;
1060
1061         /*
1062          * Check if there is room.
1063          */
1064         if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1065                 /*
1066                  * List full, cannot register more entries.
1067                  * This should never happen; if it does, it is probably a
1068                  * broken driver trying to register multiple times. Checking
1069                  * this at runtime is expensive, and won't solve the problem
1070                  * anyways, so just report a few times and then give up.
1071                  */
1072                 static int verbose = 10; /* XXX */
1073                 if (verbose > 0) {
1074                         kprintf("io poll handlers list full, "
1075                                 "maybe a broken driver ?\n");
1076                         verbose--;
1077                 }
1078                 error = ENOENT;
1079         } else {
1080                 struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1081
1082                 rec->ifp = ifp;
1083                 rec->serializer = io_rec->serializer;
1084                 rec->arg = io_rec->arg;
1085                 rec->poll_func = io_rec->poll_func;
1086
1087                 io_ctx->poll_handlers++;
1088                 error = 0;
1089         }
1090         return error;
1091 }
1092
1093 static int
1094 iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1095 {
1096         int i, error;
1097
1098         KKASSERT(&curthread->td_msgport == netisr_portfn(io_ctx->poll_cpuid));
1099
1100         for (i = 0; i < io_ctx->poll_handlers; ++i) {
1101                 if (io_ctx->pr[i].ifp == ifp) /* Found it */
1102                         break;
1103         }
1104         if (i == io_ctx->poll_handlers) {
1105                 error = ENOENT;
1106         } else {
1107                 io_ctx->poll_handlers--;
1108                 if (i < io_ctx->poll_handlers) {
1109                         /* Last entry replaces this one. */
1110                         io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1111                 }
1112
1113                 if (io_ctx->poll_handlers == 0)
1114                         iopoll_reset_state(io_ctx);
1115                 error = 0;
1116         }
1117         return error;
1118 }
1119
1120 static void
1121 poll_comm_init(int cpuid)
1122 {
1123         struct poll_comm *comm;
1124         char cpuid_str[16];
1125
1126         comm = kmalloc(sizeof(*comm), M_DEVBUF, M_WAITOK | M_ZERO);
1127
1128         if (ifpoll_stfrac < 0)
1129                 ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
1130         if (ifpoll_txfrac < 0)
1131                 ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
1132
1133         comm->pollhz = ifpoll_pollhz;
1134         comm->poll_cpuid = cpuid;
1135         comm->poll_stfrac = ifpoll_stfrac;
1136         comm->poll_txfrac = ifpoll_txfrac;
1137
1138         ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", cpuid);
1139
1140         sysctl_ctx_init(&comm->sysctl_ctx);
1141         comm->sysctl_tree = SYSCTL_ADD_NODE(&comm->sysctl_ctx,
1142                             SYSCTL_STATIC_CHILDREN(_net_ifpoll),
1143                             OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
1144
1145         SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1146                         OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
1147                         comm, 0, sysctl_pollhz,
1148                         "I", "Device polling frequency");
1149
1150         if (cpuid == 0) {
1151                 SYSCTL_ADD_PROC(&comm->sysctl_ctx,
1152                                 SYSCTL_CHILDREN(comm->sysctl_tree),
1153                                 OID_AUTO, "status_frac",
1154                                 CTLTYPE_INT | CTLFLAG_RW,
1155                                 comm, 0, sysctl_stfrac,
1156                                 "I", "# of cycles before status is polled");
1157         }
1158         SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1159                         OID_AUTO, "tx_frac", CTLTYPE_INT | CTLFLAG_RW,
1160                         comm, 0, sysctl_txfrac,
1161                         "I", "# of cycles before TX is polled");
1162
1163         poll_common[cpuid] = comm;
1164 }
1165
1166 static void
1167 poll_comm_start(int cpuid)
1168 {
1169         struct poll_comm *comm = poll_common[cpuid];
1170         systimer_func_t func;
1171
1172         /*
1173          * Initialize systimer
1174          */
1175         if (cpuid == 0)
1176                 func = poll_comm_systimer0;
1177         else
1178                 func = poll_comm_systimer;
1179         systimer_init_periodic_nq(&comm->pollclock, func, comm, 1);
1180 }
1181
1182 static void
1183 _poll_comm_systimer(struct poll_comm *comm)
1184 {
1185         if (comm->txfrac_count-- == 0) {
1186                 comm->txfrac_count = comm->poll_txfrac;
1187                 iopoll_clock(txpoll_context[comm->poll_cpuid]);
1188         }
1189         iopoll_clock(rxpoll_context[comm->poll_cpuid]);
1190 }
1191
1192 static void
1193 poll_comm_systimer0(systimer_t info, int in_ipi __unused,
1194     struct intrframe *frame __unused)
1195 {
1196         struct poll_comm *comm = info->data;
1197         globaldata_t gd = mycpu;
1198
1199         KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid == 0);
1200
1201         crit_enter_gd(gd);
1202
1203         if (comm->stfrac_count-- == 0) {
1204                 comm->stfrac_count = comm->poll_stfrac;
1205                 stpoll_clock(&stpoll_context);
1206         }
1207         _poll_comm_systimer(comm);
1208
1209         crit_exit_gd(gd);
1210 }
1211
1212 static void
1213 poll_comm_systimer(systimer_t info, int in_ipi __unused,
1214     struct intrframe *frame __unused)
1215 {
1216         struct poll_comm *comm = info->data;
1217         globaldata_t gd = mycpu;
1218
1219         KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid != 0);
1220
1221         crit_enter_gd(gd);
1222         _poll_comm_systimer(comm);
1223         crit_exit_gd(gd);
1224 }
1225
1226 static void
1227 poll_comm_adjust_pollhz(struct poll_comm *comm)
1228 {
1229         uint32_t handlers;
1230         int pollhz = 1;
1231
1232         KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1233
1234         /*
1235          * If there is no polling handler registered, set systimer
1236          * frequency to the lowest value.  Polling systimer frequency
1237          * will be adjusted to the requested value, once there are
1238          * registered handlers.
1239          */
1240         handlers = rxpoll_context[mycpuid]->poll_handlers +
1241                    txpoll_context[mycpuid]->poll_handlers;
1242         if (comm->poll_cpuid == 0)
1243                 handlers += stpoll_context.poll_handlers;
1244         if (handlers)
1245                 pollhz = comm->pollhz;
1246         systimer_adjust_periodic(&comm->pollclock, pollhz);
1247 }
1248
1249 static int
1250 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
1251 {
1252         struct poll_comm *comm = arg1;
1253         struct netmsg_base nmsg;
1254         int error, phz;
1255
1256         phz = comm->pollhz;
1257         error = sysctl_handle_int(oidp, &phz, 0, req);
1258         if (error || req->newptr == NULL)
1259                 return error;
1260         if (phz <= 0)
1261                 return EINVAL;
1262         else if (phz > IFPOLL_FREQ_MAX)
1263                 phz = IFPOLL_FREQ_MAX;
1264
1265         netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1266                     0, sysctl_pollhz_handler);
1267         nmsg.lmsg.u.ms_result = phz;
1268
1269         return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1270 }
1271
1272 static void
1273 sysctl_pollhz_handler(netmsg_t nmsg)
1274 {
1275         struct poll_comm *comm = poll_common[mycpuid];
1276
1277         KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1278
1279         /* Save polling frequency */
1280         comm->pollhz = nmsg->lmsg.u.ms_result;
1281
1282         /*
1283          * Adjust cached pollhz
1284          */
1285         rxpoll_context[mycpuid]->pollhz = comm->pollhz;
1286         txpoll_context[mycpuid]->pollhz =
1287             comm->pollhz / (comm->poll_txfrac + 1);
1288         if (mycpuid == 0)
1289                 stpoll_context.pollhz = comm->pollhz / (comm->poll_stfrac + 1);
1290
1291         /*
1292          * Adjust polling frequency
1293          */
1294         poll_comm_adjust_pollhz(comm);
1295
1296         lwkt_replymsg(&nmsg->lmsg, 0);
1297 }
1298
1299 static int
1300 sysctl_stfrac(SYSCTL_HANDLER_ARGS)
1301 {
1302         struct poll_comm *comm = arg1;
1303         struct netmsg_base nmsg;
1304         int error, stfrac;
1305
1306         KKASSERT(comm->poll_cpuid == 0);
1307
1308         stfrac = comm->poll_stfrac;
1309         error = sysctl_handle_int(oidp, &stfrac, 0, req);
1310         if (error || req->newptr == NULL)
1311                 return error;
1312         if (stfrac < 0)
1313                 return EINVAL;
1314
1315         netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1316                     0, sysctl_stfrac_handler);
1317         nmsg.lmsg.u.ms_result = stfrac;
1318
1319         return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1320 }
1321
1322 static void
1323 sysctl_stfrac_handler(netmsg_t nmsg)
1324 {
1325         struct poll_comm *comm = poll_common[mycpuid];
1326         int stfrac = nmsg->lmsg.u.ms_result;
1327
1328         KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1329
1330         crit_enter();
1331         comm->poll_stfrac = stfrac;
1332         if (comm->stfrac_count > comm->poll_stfrac)
1333                 comm->stfrac_count = comm->poll_stfrac;
1334         crit_exit();
1335
1336         lwkt_replymsg(&nmsg->lmsg, 0);
1337 }
1338
1339 static int
1340 sysctl_txfrac(SYSCTL_HANDLER_ARGS)
1341 {
1342         struct poll_comm *comm = arg1;
1343         struct netmsg_base nmsg;
1344         int error, txfrac;
1345
1346         txfrac = comm->poll_txfrac;
1347         error = sysctl_handle_int(oidp, &txfrac, 0, req);
1348         if (error || req->newptr == NULL)
1349                 return error;
1350         if (txfrac < 0)
1351                 return EINVAL;
1352
1353         netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1354                     0, sysctl_txfrac_handler);
1355         nmsg.lmsg.u.ms_result = txfrac;
1356
1357         return lwkt_domsg(netisr_portfn(comm->poll_cpuid), &nmsg.lmsg, 0);
1358 }
1359
1360 static void
1361 sysctl_txfrac_handler(netmsg_t nmsg)
1362 {
1363         struct poll_comm *comm = poll_common[mycpuid];
1364         int txfrac = nmsg->lmsg.u.ms_result;
1365
1366         KKASSERT(&curthread->td_msgport == netisr_portfn(comm->poll_cpuid));
1367
1368         crit_enter();
1369         comm->poll_txfrac = txfrac;
1370         if (comm->txfrac_count > comm->poll_txfrac)
1371                 comm->txfrac_count = comm->poll_txfrac;
1372         crit_exit();
1373
1374         lwkt_replymsg(&nmsg->lmsg, 0);
1375 }