if_agrether.c revision 1.1 1 /* $NetBSD: if_agrether.c,v 1.1 2005/03/18 11:11:50 yamt 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_agrether.c,v 1.1 2005/03/18 11:11:50 yamt Exp $");
31
32 #include <sys/param.h>
33 #include <sys/mbuf.h>
34 #include <sys/sockio.h>
35
36 #include <net/if.h>
37 #include <net/if_dl.h>
38 #include <net/if_ether.h>
39 #include <net/if_media.h>
40
41 #include <net/agr/if_agrvar_impl.h>
42 #include <net/agr/if_agrethervar.h>
43 #include <net/agr/if_agrsubr.h>
44
45 #include <net/agr/ieee8023_slowprotocols.h>
46 #include <net/agr/ieee8023_tlv.h>
47 #include <net/agr/ieee8023ad.h>
48 #include <net/agr/ieee8023ad_lacp.h>
49 #include <net/agr/ieee8023ad_lacp_impl.h>
50 #include <net/agr/ieee8023ad_impl.h>
51
52 static int agrether_ctor(struct agr_softc *, struct ifnet *);
53 static void agrether_dtor(struct agr_softc *);
54 static int agrether_portinit(struct agr_softc *, struct agr_port *);
55 static int agrether_portfini(struct agr_softc *, struct agr_port *);
56 static struct agr_port *agrether_select_tx_port(struct agr_softc *,
57 struct mbuf *);
58 static int agrether_configmulti_port(struct agr_softc *, struct agr_port *,
59 boolean_t);
60 static int agrether_configmulti_ifreq(struct agr_softc *, struct ifreq *,
61 boolean_t);
62
63 const struct agr_iftype_ops agrether_ops = {
64 .iftop_tick = NULL,
65 .iftop_porttick = ieee8023ad_lacp_porttick,
66 .iftop_portstate = ieee8023ad_lacp_portstate,
67 .iftop_ctor = agrether_ctor,
68 .iftop_dtor = agrether_dtor,
69 .iftop_portinit = agrether_portinit,
70 .iftop_portfini = agrether_portfini,
71 .iftop_hashmbuf = agrether_hashmbuf,
72 .iftop_select_tx_port = agrether_select_tx_port,
73 .iftop_configmulti_port = agrether_configmulti_port,
74 .iftop_configmulti_ifreq = agrether_configmulti_ifreq
75 };
76
77 struct agrether_private {
78 struct ieee8023ad_softc aep_ieee8023ad_softc;
79 struct agr_multiaddrs aep_multiaddrs;
80 };
81
82 struct agrether_port_private {
83 struct ieee8023ad_port aepp_ieee8023ad_port;
84 };
85
86 static int
87 agrether_ctor(struct agr_softc *sc, struct ifnet *ifp_port)
88 {
89 struct ifnet *ifp = &sc->sc_if;
90 struct ethercom *ec = (void *)ifp;
91 struct agrether_private *priv;
92
93 priv = malloc(sizeof(*priv), M_DEVBUF, M_NOWAIT | M_ZERO);
94 if (!priv)
95 return ENOMEM;
96
97 agr_mc_init(sc, &priv->aep_multiaddrs);
98
99 sc->sc_iftprivate = priv;
100
101 ether_ifattach(ifp, LLADDR(ifp_port->if_sadl));
102 ec->ec_capabilities =
103 ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING | ETHERCAP_JUMBO_MTU;
104
105 ieee8023ad_ctor(sc);
106
107 return 0;
108 }
109
110 static void
111 agrether_dtor(struct agr_softc *sc)
112 {
113 struct agrether_private *priv = sc->sc_iftprivate;
114
115 if (priv == NULL) {
116 return;
117 }
118
119 ieee8023ad_dtor(sc);
120 agr_mc_purgeall(sc, &priv->aep_multiaddrs);
121 free(priv, M_DEVBUF);
122 sc->sc_iftprivate = NULL;
123 }
124
125 static int
126 agrether_portinit(struct agr_softc *sc, struct agr_port *port)
127 {
128 struct ifreq ifr;
129 struct agrether_port_private *priv;
130 int error;
131 struct ethercom *ec = (void *)&sc->sc_if;
132 struct ethercom *ec_port = (void *)port->port_ifp;
133
134 port->port_media = IFM_ETHER | IFM_NONE;
135
136 /*
137 * XXX it's better to always advertise ETHERCAP_VLAN_HWTAGGING
138 * and do tag insertion by ourselves if necessary,
139 * so that we can mix devices with different capabilities.
140 * ditto about if_capabilities.
141 */
142
143 if (ec->ec_capabilities &
144 ~ec_port->ec_capabilities &
145 (ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING)) {
146 if (ec->ec_nvlans > 0) {
147 return EINVAL;
148 }
149 ec->ec_capabilities &=
150 ec_port->ec_capabilities |
151 ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_MTU);
152 }
153
154 /* XXX ETHERCAP_JUMBO_MTU */
155
156 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
157 if (priv == NULL) {
158 return ENOMEM;
159 }
160
161 port->port_iftprivate = priv;
162
163 memset(&ifr, 0, sizeof(ifr));
164 ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr);
165 ifr.ifr_addr.sa_family = AF_UNSPEC;
166 KASSERT(sizeof(ifr.ifr_addr) >=
167 sizeof(ethermulticastaddr_slowprotocols));
168 memcpy(&ifr.ifr_addr.sa_data,
169 ðermulticastaddr_slowprotocols,
170 sizeof(ethermulticastaddr_slowprotocols));
171 error = agrport_ioctl(port, SIOCADDMULTI, (caddr_t)&ifr);
172 if (error) {
173 free(port->port_iftprivate, M_DEVBUF);
174 port->port_iftprivate = NULL;
175 return error;
176 }
177
178 ieee8023ad_portinit(port);
179
180 return error;
181 }
182
183 static int
184 agrether_portfini(struct agr_softc *sc, struct agr_port *port)
185 {
186 struct ifreq ifr;
187 int error;
188
189 if (port->port_iftprivate == NULL) {
190 return 0;
191 }
192
193 memset(&ifr, 0, sizeof(ifr));
194 ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr);
195 ifr.ifr_addr.sa_family = AF_UNSPEC;
196 KASSERT(sizeof(ifr.ifr_addr) >=
197 sizeof(ethermulticastaddr_slowprotocols));
198 memcpy(&ifr.ifr_addr.sa_data,
199 ðermulticastaddr_slowprotocols,
200 sizeof(ethermulticastaddr_slowprotocols));
201 error = agrport_ioctl(port, SIOCDELMULTI, (caddr_t)&ifr);
202 if (error) {
203 return error;
204 }
205
206 ieee8023ad_portfini(port);
207
208 free(port->port_iftprivate, M_DEVBUF);
209 port->port_iftprivate = NULL;
210
211 return error;
212 }
213
214 static int
215 agrether_configmulti_port(struct agr_softc *sc, struct agr_port *port,
216 boolean_t add)
217 {
218 struct agrether_private *priv = sc->sc_iftprivate;
219
220 return agr_configmulti_port(&priv->aep_multiaddrs, port, add);
221 }
222
223 static int
224 agrether_configmulti_ifreq(struct agr_softc *sc, struct ifreq *ifr,
225 boolean_t add)
226 {
227 struct agrether_private *priv = sc->sc_iftprivate;
228
229 return agr_configmulti_ifreq(sc, &priv->aep_multiaddrs, ifr, add);
230 }
231
232 /* -------------------- */
233
234 static struct agr_port *
235 agrether_select_tx_port(struct agr_softc *sc, struct mbuf *m)
236 {
237
238 return ieee8023ad_select_tx_port(sc, m);
239 }
240