Home | History | Annotate | Line # | Download | only in agr
if_agrether.c revision 1.6.26.1
      1  1.6.26.1      yamt /*	$NetBSD: if_agrether.c,v 1.6.26.1 2009/06/20 07:20:33 yamt Exp $	*/
      2       1.1      yamt 
      3       1.1      yamt /*-
      4       1.1      yamt  * Copyright (c)2005 YAMAMOTO Takashi,
      5       1.1      yamt  * All rights reserved.
      6       1.1      yamt  *
      7       1.1      yamt  * Redistribution and use in source and binary forms, with or without
      8       1.1      yamt  * modification, are permitted provided that the following conditions
      9       1.1      yamt  * are met:
     10       1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     11       1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     12       1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      yamt  *    documentation and/or other materials provided with the distribution.
     15       1.1      yamt  *
     16       1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1      yamt  * SUCH DAMAGE.
     27       1.1      yamt  */
     28       1.1      yamt 
     29       1.1      yamt #include <sys/cdefs.h>
     30  1.6.26.1      yamt __KERNEL_RCSID(0, "$NetBSD: if_agrether.c,v 1.6.26.1 2009/06/20 07:20:33 yamt Exp $");
     31       1.1      yamt 
     32       1.1      yamt #include <sys/param.h>
     33       1.2      yamt #include <sys/callout.h>
     34       1.1      yamt #include <sys/mbuf.h>
     35       1.1      yamt #include <sys/sockio.h>
     36       1.1      yamt 
     37       1.1      yamt #include <net/if.h>
     38       1.1      yamt #include <net/if_dl.h>
     39       1.1      yamt #include <net/if_ether.h>
     40       1.1      yamt #include <net/if_media.h>
     41       1.1      yamt 
     42       1.1      yamt #include <net/agr/if_agrvar_impl.h>
     43       1.1      yamt #include <net/agr/if_agrethervar.h>
     44       1.1      yamt #include <net/agr/if_agrsubr.h>
     45       1.1      yamt 
     46       1.1      yamt #include <net/agr/ieee8023_slowprotocols.h>
     47       1.1      yamt #include <net/agr/ieee8023_tlv.h>
     48       1.1      yamt #include <net/agr/ieee8023ad.h>
     49       1.1      yamt #include <net/agr/ieee8023ad_lacp.h>
     50       1.1      yamt #include <net/agr/ieee8023ad_lacp_impl.h>
     51       1.1      yamt #include <net/agr/ieee8023ad_impl.h>
     52       1.1      yamt 
     53       1.1      yamt static int agrether_ctor(struct agr_softc *, struct ifnet *);
     54       1.1      yamt static void agrether_dtor(struct agr_softc *);
     55       1.1      yamt static int agrether_portinit(struct agr_softc *, struct agr_port *);
     56       1.1      yamt static int agrether_portfini(struct agr_softc *, struct agr_port *);
     57       1.1      yamt static struct agr_port *agrether_select_tx_port(struct agr_softc *,
     58       1.1      yamt     struct mbuf *);
     59       1.1      yamt static int agrether_configmulti_port(struct agr_softc *, struct agr_port *,
     60       1.4   thorpej     bool);
     61       1.1      yamt static int agrether_configmulti_ifreq(struct agr_softc *, struct ifreq *,
     62       1.4   thorpej     bool);
     63       1.1      yamt 
     64       1.1      yamt const struct agr_iftype_ops agrether_ops = {
     65       1.1      yamt 	.iftop_tick = NULL,
     66       1.1      yamt 	.iftop_porttick = ieee8023ad_lacp_porttick,
     67       1.1      yamt 	.iftop_portstate = ieee8023ad_lacp_portstate,
     68       1.1      yamt 	.iftop_ctor = agrether_ctor,
     69       1.1      yamt 	.iftop_dtor = agrether_dtor,
     70       1.1      yamt 	.iftop_portinit = agrether_portinit,
     71       1.1      yamt 	.iftop_portfini = agrether_portfini,
     72       1.1      yamt 	.iftop_hashmbuf = agrether_hashmbuf,
     73       1.1      yamt 	.iftop_select_tx_port = agrether_select_tx_port,
     74       1.1      yamt 	.iftop_configmulti_port = agrether_configmulti_port,
     75       1.1      yamt 	.iftop_configmulti_ifreq = agrether_configmulti_ifreq
     76       1.1      yamt };
     77       1.1      yamt 
     78       1.1      yamt struct agrether_private {
     79       1.1      yamt 	struct ieee8023ad_softc aep_ieee8023ad_softc;
     80       1.1      yamt 	struct agr_multiaddrs aep_multiaddrs;
     81       1.1      yamt };
     82       1.1      yamt 
     83       1.1      yamt struct agrether_port_private {
     84       1.1      yamt 	struct ieee8023ad_port aepp_ieee8023ad_port;
     85       1.1      yamt };
     86       1.1      yamt 
     87       1.1      yamt static int
     88       1.1      yamt agrether_ctor(struct agr_softc *sc, struct ifnet *ifp_port)
     89       1.1      yamt {
     90       1.1      yamt 	struct ifnet *ifp = &sc->sc_if;
     91       1.1      yamt 	struct ethercom *ec = (void *)ifp;
     92       1.1      yamt 	struct agrether_private *priv;
     93       1.1      yamt 
     94       1.1      yamt 	priv = malloc(sizeof(*priv), M_DEVBUF, M_NOWAIT | M_ZERO);
     95       1.1      yamt 	if (!priv)
     96       1.1      yamt 		return ENOMEM;
     97       1.1      yamt 
     98       1.1      yamt 	agr_mc_init(sc, &priv->aep_multiaddrs);
     99       1.1      yamt 
    100       1.1      yamt 	sc->sc_iftprivate = priv;
    101  1.6.26.1      yamt 	/*
    102  1.6.26.1      yamt 	 * inherit ports capabilities
    103  1.6.26.1      yamt 	 * XXX this really needs to be the intersection of all
    104  1.6.26.1      yamt 	 * ports capabilities, not just the latest port.
    105  1.6.26.1      yamt 	 * Okay if ports are the same.
    106  1.6.26.1      yamt 	 */
    107  1.6.26.1      yamt 	ifp->if_capabilities = ifp_port->if_capabilities &
    108  1.6.26.1      yamt 			(IFCAP_TSOv4 | IFCAP_TSOv6 |
    109  1.6.26.1      yamt 			IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    110  1.6.26.1      yamt 			IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    111  1.6.26.1      yamt 			IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
    112  1.6.26.1      yamt 			IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
    113  1.6.26.1      yamt 			IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx);
    114       1.1      yamt 
    115       1.6    dyoung 	ether_ifattach(ifp, CLLADDR(ifp_port->if_sadl));
    116       1.1      yamt 	ec->ec_capabilities =
    117       1.1      yamt 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING | ETHERCAP_JUMBO_MTU;
    118       1.1      yamt 
    119       1.1      yamt 	ieee8023ad_ctor(sc);
    120       1.1      yamt 
    121       1.1      yamt 	return 0;
    122       1.1      yamt }
    123       1.1      yamt 
    124       1.1      yamt static void
    125       1.1      yamt agrether_dtor(struct agr_softc *sc)
    126       1.1      yamt {
    127       1.1      yamt 	struct agrether_private *priv = sc->sc_iftprivate;
    128       1.1      yamt 
    129       1.1      yamt 	if (priv == NULL) {
    130       1.1      yamt 		return;
    131       1.1      yamt 	}
    132       1.1      yamt 
    133       1.1      yamt 	ieee8023ad_dtor(sc);
    134       1.1      yamt 	agr_mc_purgeall(sc, &priv->aep_multiaddrs);
    135       1.1      yamt 	free(priv, M_DEVBUF);
    136       1.1      yamt 	sc->sc_iftprivate = NULL;
    137       1.1      yamt }
    138       1.1      yamt 
    139       1.1      yamt static int
    140       1.1      yamt agrether_portinit(struct agr_softc *sc, struct agr_port *port)
    141       1.1      yamt {
    142       1.1      yamt 	struct ifreq ifr;
    143       1.1      yamt 	struct agrether_port_private *priv;
    144       1.1      yamt 	int error;
    145       1.1      yamt 	struct ethercom *ec = (void *)&sc->sc_if;
    146       1.1      yamt 	struct ethercom *ec_port = (void *)port->port_ifp;
    147       1.1      yamt 
    148       1.1      yamt 	port->port_media = IFM_ETHER | IFM_NONE;
    149       1.1      yamt 
    150       1.1      yamt 	/*
    151       1.1      yamt 	 * XXX it's better to always advertise ETHERCAP_VLAN_HWTAGGING
    152       1.1      yamt 	 * and do tag insertion by ourselves if necessary,
    153       1.1      yamt 	 * so that we can mix devices with different capabilities.
    154       1.1      yamt 	 * ditto about if_capabilities.
    155       1.1      yamt 	 */
    156       1.1      yamt 
    157       1.1      yamt 	if (ec->ec_capabilities &
    158       1.1      yamt 	    ~ec_port->ec_capabilities &
    159       1.1      yamt 	    (ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING)) {
    160       1.1      yamt 		if (ec->ec_nvlans > 0) {
    161       1.1      yamt 			return EINVAL;
    162       1.1      yamt 		}
    163       1.1      yamt 		ec->ec_capabilities &=
    164       1.1      yamt 		    ec_port->ec_capabilities |
    165  1.6.26.1      yamt 		    ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING);
    166       1.1      yamt 	}
    167       1.1      yamt 
    168  1.6.26.1      yamt 	/* Enable vlan support */
    169  1.6.26.1      yamt 	if ((ec->ec_nvlans > 0) &&
    170  1.6.26.1      yamt 	     ec_port->ec_nvlans++ == 0 &&
    171  1.6.26.1      yamt 	    (ec_port->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    172  1.6.26.1      yamt 		struct ifnet *p = port->port_ifp;
    173  1.6.26.1      yamt 		/*
    174  1.6.26.1      yamt 		 * Enable Tx/Rx of VLAN-sized frames.
    175  1.6.26.1      yamt 		 */
    176  1.6.26.1      yamt 		ec_port->ec_capenable |= ETHERCAP_VLAN_MTU;
    177  1.6.26.1      yamt 		if (p->if_flags & IFF_UP) {
    178  1.6.26.1      yamt 			ifr.ifr_flags = p->if_flags;
    179  1.6.26.1      yamt 			error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
    180  1.6.26.1      yamt 			    (void *) &ifr);
    181  1.6.26.1      yamt 			if (error) {
    182  1.6.26.1      yamt 				if (ec_port->ec_nvlans-- == 1)
    183  1.6.26.1      yamt 					ec_port->ec_capenable &=
    184  1.6.26.1      yamt 					    ~ETHERCAP_VLAN_MTU;
    185  1.6.26.1      yamt 				return (error);
    186  1.6.26.1      yamt 			}
    187  1.6.26.1      yamt 		}
    188  1.6.26.1      yamt 	}
    189       1.1      yamt 	/* XXX ETHERCAP_JUMBO_MTU */
    190       1.1      yamt 
    191       1.1      yamt 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
    192       1.1      yamt 	if (priv == NULL) {
    193       1.1      yamt 		return ENOMEM;
    194       1.1      yamt 	}
    195       1.1      yamt 
    196       1.1      yamt 	port->port_iftprivate = priv;
    197       1.1      yamt 
    198       1.1      yamt 	memset(&ifr, 0, sizeof(ifr));
    199       1.1      yamt 	ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr);
    200       1.1      yamt 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    201       1.1      yamt 	KASSERT(sizeof(ifr.ifr_addr) >=
    202       1.1      yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    203       1.1      yamt 	memcpy(&ifr.ifr_addr.sa_data,
    204       1.1      yamt 	    &ethermulticastaddr_slowprotocols,
    205       1.1      yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    206       1.5  christos 	error = agrport_ioctl(port, SIOCADDMULTI, (void *)&ifr);
    207       1.1      yamt 	if (error) {
    208       1.1      yamt 		free(port->port_iftprivate, M_DEVBUF);
    209       1.1      yamt 		port->port_iftprivate = NULL;
    210       1.1      yamt 		return error;
    211       1.1      yamt 	}
    212       1.1      yamt 
    213       1.1      yamt 	ieee8023ad_portinit(port);
    214       1.1      yamt 
    215       1.1      yamt 	return error;
    216       1.1      yamt }
    217       1.1      yamt 
    218       1.1      yamt static int
    219       1.1      yamt agrether_portfini(struct agr_softc *sc, struct agr_port *port)
    220       1.1      yamt {
    221       1.1      yamt 	struct ifreq ifr;
    222  1.6.26.1      yamt 	struct ethercom *ec_port = (void *)port->port_ifp;
    223       1.1      yamt 	int error;
    224       1.1      yamt 
    225       1.1      yamt 	if (port->port_iftprivate == NULL) {
    226       1.1      yamt 		return 0;
    227       1.1      yamt 	}
    228       1.1      yamt 
    229  1.6.26.1      yamt 	if (ec_port->ec_nvlans > 0) {
    230  1.6.26.1      yamt 		/* Disable vlan support */
    231  1.6.26.1      yamt 		ec_port->ec_nvlans = 0;
    232  1.6.26.1      yamt 		/*
    233  1.6.26.1      yamt 		 * Disable Tx/Rx of VLAN-sized frames.
    234  1.6.26.1      yamt 		 */
    235  1.6.26.1      yamt 		ec_port->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    236  1.6.26.1      yamt 		if (port->port_ifp->if_flags & IFF_UP) {
    237  1.6.26.1      yamt 			ifr.ifr_flags = port->port_ifp->if_flags;
    238  1.6.26.1      yamt 			(void) (*port->port_ifp->if_ioctl)(port->port_ifp,
    239  1.6.26.1      yamt 			    SIOCSIFFLAGS, (void *) &ifr);
    240  1.6.26.1      yamt 		}
    241  1.6.26.1      yamt 	}
    242  1.6.26.1      yamt 
    243       1.1      yamt 	memset(&ifr, 0, sizeof(ifr));
    244       1.1      yamt 	ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr);
    245       1.1      yamt 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    246       1.1      yamt 	KASSERT(sizeof(ifr.ifr_addr) >=
    247       1.1      yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    248       1.1      yamt 	memcpy(&ifr.ifr_addr.sa_data,
    249       1.1      yamt 	    &ethermulticastaddr_slowprotocols,
    250       1.1      yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    251       1.5  christos 	error = agrport_ioctl(port, SIOCDELMULTI, (void *)&ifr);
    252       1.1      yamt 	if (error) {
    253       1.1      yamt 		return error;
    254       1.1      yamt 	}
    255       1.1      yamt 
    256       1.1      yamt 	ieee8023ad_portfini(port);
    257       1.1      yamt 
    258       1.1      yamt 	free(port->port_iftprivate, M_DEVBUF);
    259       1.1      yamt 	port->port_iftprivate = NULL;
    260       1.1      yamt 
    261       1.1      yamt 	return error;
    262       1.1      yamt }
    263       1.1      yamt 
    264       1.1      yamt static int
    265       1.1      yamt agrether_configmulti_port(struct agr_softc *sc, struct agr_port *port,
    266       1.4   thorpej     bool add)
    267       1.1      yamt {
    268       1.1      yamt 	struct agrether_private *priv = sc->sc_iftprivate;
    269       1.1      yamt 
    270       1.1      yamt 	return agr_configmulti_port(&priv->aep_multiaddrs, port, add);
    271       1.1      yamt }
    272       1.1      yamt 
    273       1.1      yamt static int
    274       1.1      yamt agrether_configmulti_ifreq(struct agr_softc *sc, struct ifreq *ifr,
    275       1.4   thorpej     bool add)
    276       1.1      yamt {
    277       1.1      yamt 	struct agrether_private *priv = sc->sc_iftprivate;
    278       1.1      yamt 
    279       1.1      yamt 	return agr_configmulti_ifreq(sc, &priv->aep_multiaddrs, ifr, add);
    280       1.1      yamt }
    281       1.1      yamt 
    282       1.1      yamt /* -------------------- */
    283       1.1      yamt 
    284       1.1      yamt static struct agr_port *
    285       1.1      yamt agrether_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    286       1.1      yamt {
    287       1.1      yamt 
    288       1.1      yamt 	return ieee8023ad_select_tx_port(sc, m);
    289       1.1      yamt }
    290