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