| 1 | /* $NetBSD: if_agrether.c,v 1.9 2011/10/19 01:49:50 dyoung 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.9 2011/10/19 01:49:50 dyoung Exp $" ); |
| 31 | |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/callout.h> |
| 34 | #include <sys/mbuf.h> |
| 35 | #include <sys/sockio.h> |
| 36 | |
| 37 | #include <net/if.h> |
| 38 | #include <net/if_dl.h> |
| 39 | #include <net/if_ether.h> |
| 40 | #include <net/if_media.h> |
| 41 | |
| 42 | #include <net/agr/if_agrvar_impl.h> |
| 43 | #include <net/agr/if_agrethervar.h> |
| 44 | #include <net/agr/if_agrsubr.h> |
| 45 | |
| 46 | #include <net/agr/ieee8023_slowprotocols.h> |
| 47 | #include <net/agr/ieee8023_tlv.h> |
| 48 | #include <net/agr/ieee8023ad.h> |
| 49 | #include <net/agr/ieee8023ad_lacp.h> |
| 50 | #include <net/agr/ieee8023ad_lacp_impl.h> |
| 51 | #include <net/agr/ieee8023ad_impl.h> |
| 52 | |
| 53 | static int agrether_ctor(struct agr_softc *, struct ifnet *); |
| 54 | static void agrether_dtor(struct agr_softc *); |
| 55 | static int agrether_portinit(struct agr_softc *, struct agr_port *); |
| 56 | static int agrether_portfini(struct agr_softc *, struct agr_port *); |
| 57 | static struct agr_port *agrether_select_tx_port(struct agr_softc *, |
| 58 | struct mbuf *); |
| 59 | static int agrether_configmulti_port(struct agr_softc *, struct agr_port *, |
| 60 | bool); |
| 61 | static int agrether_configmulti_ifreq(struct agr_softc *, struct ifreq *, |
| 62 | bool); |
| 63 | |
| 64 | const struct agr_iftype_ops agrether_ops = { |
| 65 | .iftop_tick = NULL, |
| 66 | .iftop_porttick = ieee8023ad_lacp_porttick, |
| 67 | .iftop_portstate = ieee8023ad_lacp_portstate, |
| 68 | .iftop_ctor = agrether_ctor, |
| 69 | .iftop_dtor = agrether_dtor, |
| 70 | .iftop_portinit = agrether_portinit, |
| 71 | .iftop_portfini = agrether_portfini, |
| 72 | .iftop_hashmbuf = agrether_hashmbuf, |
| 73 | .iftop_select_tx_port = agrether_select_tx_port, |
| 74 | .iftop_configmulti_port = agrether_configmulti_port, |
| 75 | .iftop_configmulti_ifreq = agrether_configmulti_ifreq |
| 76 | }; |
| 77 | |
| 78 | struct agrether_private { |
| 79 | struct ieee8023ad_softc aep_ieee8023ad_softc; |
| 80 | struct agr_multiaddrs aep_multiaddrs; |
| 81 | }; |
| 82 | |
| 83 | struct agrether_port_private { |
| 84 | struct ieee8023ad_port aepp_ieee8023ad_port; |
| 85 | }; |
| 86 | |
| 87 | static int |
| 88 | agrether_ctor(struct agr_softc *sc, struct ifnet *ifp_port) |
| 89 | { |
| 90 | struct ifnet *ifp = &sc->sc_if; |
| 91 | struct ethercom *ec = (void *)ifp; |
| 92 | struct agrether_private *priv; |
| 93 | |
| 94 | priv = malloc(sizeof(*priv), M_DEVBUF, M_NOWAIT | M_ZERO); |
| 95 | if (!priv) |
| 96 | return ENOMEM; |
| 97 | |
| 98 | agr_mc_init(sc, &priv->aep_multiaddrs); |
| 99 | |
| 100 | sc->sc_iftprivate = priv; |
| 101 | /* |
| 102 | * inherit ports capabilities |
| 103 | * XXX this really needs to be the intersection of all |
| 104 | * ports capabilities, not just the latest port. |
| 105 | * Okay if ports are the same. |
| 106 | */ |
| 107 | ifp->if_capabilities = ifp_port->if_capabilities & |
| 108 | (IFCAP_TSOv4 | IFCAP_TSOv6 | |
| 109 | IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | |
| 110 | IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | |
| 111 | IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx | |
| 112 | IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx | |
| 113 | IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx); |
| 114 | |
| 115 | ether_ifattach(ifp, CLLADDR(ifp_port->if_sadl)); |
| 116 | ec->ec_capabilities = |
| 117 | ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING | ETHERCAP_JUMBO_MTU; |
| 118 | |
| 119 | ieee8023ad_ctor(sc); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | agrether_dtor(struct agr_softc *sc) |
| 126 | { |
| 127 | struct agrether_private *priv = sc->sc_iftprivate; |
| 128 | |
| 129 | if (priv == NULL) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | ieee8023ad_dtor(sc); |
| 134 | agr_mc_purgeall(sc, &priv->aep_multiaddrs); |
| 135 | free(priv, M_DEVBUF); |
| 136 | sc->sc_iftprivate = NULL; |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | agrether_portinit(struct agr_softc *sc, struct agr_port *port) |
| 141 | { |
| 142 | struct ifreq ifr; |
| 143 | struct agrether_port_private *priv; |
| 144 | int error; |
| 145 | struct ethercom *ec = (void *)&sc->sc_if; |
| 146 | struct ethercom *ec_port = (void *)port->port_ifp; |
| 147 | |
| 148 | port->port_media = IFM_ETHER | IFM_NONE; |
| 149 | |
| 150 | /* |
| 151 | * XXX it's better to always advertise ETHERCAP_VLAN_HWTAGGING |
| 152 | * and do tag insertion by ourselves if necessary, |
| 153 | * so that we can mix devices with different capabilities. |
| 154 | * ditto about if_capabilities. |
| 155 | */ |
| 156 | |
| 157 | if (ec->ec_capabilities & |
| 158 | ~ec_port->ec_capabilities & |
| 159 | (ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING)) { |
| 160 | if (ec->ec_nvlans > 0) { |
| 161 | return EINVAL; |
| 162 | } |
| 163 | ec->ec_capabilities &= |
| 164 | ec_port->ec_capabilities | |
| 165 | ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING); |
| 166 | } |
| 167 | |
| 168 | /* Enable vlan support */ |
| 169 | if ((ec->ec_nvlans > 0) && |
| 170 | ec_port->ec_nvlans++ == 0 && |
| 171 | (ec_port->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) { |
| 172 | struct ifnet *p = port->port_ifp; |
| 173 | /* |
| 174 | * Enable Tx/Rx of VLAN-sized frames. |
| 175 | */ |
| 176 | ec_port->ec_capenable |= ETHERCAP_VLAN_MTU; |
| 177 | if (p->if_flags & IFF_UP) { |
| 178 | error = if_flags_set(p, p->if_flags); |
| 179 | if (error) { |
| 180 | if (ec_port->ec_nvlans-- == 1) |
| 181 | ec_port->ec_capenable &= |
| 182 | ~ETHERCAP_VLAN_MTU; |
| 183 | return (error); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | /* XXX ETHERCAP_JUMBO_MTU */ |
| 188 | |
| 189 | priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO); |
| 190 | if (priv == NULL) { |
| 191 | return ENOMEM; |
| 192 | } |
| 193 | |
| 194 | port->port_iftprivate = priv; |
| 195 | |
| 196 | memset(&ifr, 0, sizeof(ifr)); |
| 197 | ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr); |
| 198 | ifr.ifr_addr.sa_family = AF_UNSPEC; |
| 199 | KASSERT(sizeof(ifr.ifr_addr) >= |
| 200 | sizeof(ethermulticastaddr_slowprotocols)); |
| 201 | memcpy(&ifr.ifr_addr.sa_data, |
| 202 | ðermulticastaddr_slowprotocols, |
| 203 | sizeof(ethermulticastaddr_slowprotocols)); |
| 204 | error = agrport_ioctl(port, SIOCADDMULTI, (void *)&ifr); |
| 205 | if (error) { |
| 206 | free(port->port_iftprivate, M_DEVBUF); |
| 207 | port->port_iftprivate = NULL; |
| 208 | return error; |
| 209 | } |
| 210 | |
| 211 | ieee8023ad_portinit(port); |
| 212 | |
| 213 | return error; |
| 214 | } |
| 215 | |
| 216 | static int |
| 217 | agrether_portfini(struct agr_softc *sc, struct agr_port *port) |
| 218 | { |
| 219 | struct ifreq ifr; |
| 220 | struct ethercom *ec_port = (void *)port->port_ifp; |
| 221 | int error; |
| 222 | |
| 223 | if (port->port_iftprivate == NULL) { |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | if (ec_port->ec_nvlans > 0) { |
| 228 | /* Disable vlan support */ |
| 229 | ec_port->ec_nvlans = 0; |
| 230 | /* |
| 231 | * Disable Tx/Rx of VLAN-sized frames. |
| 232 | */ |
| 233 | ec_port->ec_capenable &= ~ETHERCAP_VLAN_MTU; |
| 234 | if (port->port_ifp->if_flags & IFF_UP) { |
| 235 | (void)if_flags_set(port->port_ifp, |
| 236 | port->port_ifp->if_flags); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | memset(&ifr, 0, sizeof(ifr)); |
| 241 | ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr); |
| 242 | ifr.ifr_addr.sa_family = AF_UNSPEC; |
| 243 | KASSERT(sizeof(ifr.ifr_addr) >= |
| 244 | sizeof(ethermulticastaddr_slowprotocols)); |
| 245 | memcpy(&ifr.ifr_addr.sa_data, |
| 246 | ðermulticastaddr_slowprotocols, |
| 247 | sizeof(ethermulticastaddr_slowprotocols)); |
| 248 | error = agrport_ioctl(port, SIOCDELMULTI, (void *)&ifr); |
| 249 | if (error) { |
| 250 | return error; |
| 251 | } |
| 252 | |
| 253 | ieee8023ad_portfini(port); |
| 254 | |
| 255 | free(port->port_iftprivate, M_DEVBUF); |
| 256 | port->port_iftprivate = NULL; |
| 257 | |
| 258 | return error; |
| 259 | } |
| 260 | |
| 261 | static int |
| 262 | agrether_configmulti_port(struct agr_softc *sc, struct agr_port *port, |
| 263 | bool add) |
| 264 | { |
| 265 | struct agrether_private *priv = sc->sc_iftprivate; |
| 266 | |
| 267 | return agr_configmulti_port(&priv->aep_multiaddrs, port, add); |
| 268 | } |
| 269 | |
| 270 | static int |
| 271 | agrether_configmulti_ifreq(struct agr_softc *sc, struct ifreq *ifr, |
| 272 | bool add) |
| 273 | { |
| 274 | struct agrether_private *priv = sc->sc_iftprivate; |
| 275 | |
| 276 | return agr_configmulti_ifreq(sc, &priv->aep_multiaddrs, ifr, add); |
| 277 | } |
| 278 | |
| 279 | /* -------------------- */ |
| 280 | |
| 281 | static struct agr_port * |
| 282 | agrether_select_tx_port(struct agr_softc *sc, struct mbuf *m) |
| 283 | { |
| 284 | |
| 285 | return ieee8023ad_select_tx_port(sc, m); |
| 286 | } |
| 287 | |