1 /*        $NetBSD: if_agrtimer.c,v 1.7 2017/01/28 22:56:09 maya Exp $ */
2 
3 /*-
4  * Copyright (c)2005 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: if_agrtimer.c,v 1.7 2017/01/28 22:56:09 maya Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/callout.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 
37 #include <net/if.h>
38 
39 #include <net/agr/if_agrvar_impl.h>
40 #include <net/agr/if_agrsubr.h>
41 
42 static void agrtimer_tick(void *);
43 static void agrtimer_work(struct work *, void *);
44 static int agrtimer_port_tick(struct agr_port *, void *);
45 
46 int
agrtimer_init(struct agr_softc * sc)47 agrtimer_init(struct agr_softc *sc)
48 {
49           int error;
50 
51           error = workqueue_create(&sc->sc_wq, "agrmon",
52               agrtimer_work, sc, PRI_SOFTNET, IPL_SOFTNET, 0);
53           if (error)
54                     return error;
55 
56           callout_init(&sc->sc_callout, 0);
57           callout_setfunc(&sc->sc_callout, agrtimer_tick, sc);
58           return 0;
59 }
60 
61 void
agrtimer_destroy(struct agr_softc * sc)62 agrtimer_destroy(struct agr_softc *sc)
63 {
64 
65           callout_destroy(&sc->sc_callout);
66           workqueue_destroy(sc->sc_wq);
67 }
68 
69 void
agrtimer_start(struct agr_softc * sc)70 agrtimer_start(struct agr_softc *sc)
71 {
72 
73           callout_schedule(&sc->sc_callout, 0);
74 }
75 
76 void
agrtimer_stop(struct agr_softc * sc)77 agrtimer_stop(struct agr_softc *sc)
78 {
79 
80           callout_stop(&sc->sc_callout);
81 }
82 
83 static void
agrtimer_tick(void * arg)84 agrtimer_tick(void *arg)
85 {
86           struct agr_softc *sc = arg;
87 
88           workqueue_enqueue(sc->sc_wq, &sc->sc_wk, NULL);
89 }
90 
91 static void
agrtimer_work(struct work * wk,void * arg)92 agrtimer_work(struct work *wk, void *arg)
93 {
94           struct agr_softc *sc = arg;
95           const struct agr_iftype_ops *iftop = sc->sc_iftop;
96 
97           KASSERT(iftop);
98 
99           AGR_LOCK(sc);
100           if (iftop->iftop_tick) {
101                     (*iftop->iftop_tick)(sc);
102           }
103           if (iftop->iftop_porttick) {
104                     agr_port_foreach(sc, agrtimer_port_tick, sc);
105           }
106           callout_schedule(&sc->sc_callout, hz);
107           AGR_UNLOCK(sc);
108 }
109 
110 static int
agrtimer_port_tick(struct agr_port * port,void * arg)111 agrtimer_port_tick(struct agr_port *port, void *arg)
112 {
113           struct agr_softc *sc = arg;
114 
115           agrport_monitor(port);
116           (*sc->sc_iftop->iftop_porttick)(sc, port);
117 
118           return 0;
119 }
120