Home | History | Annotate | Line # | Download | only in agr
if_agrether.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: if_agrether.c,v 1.4 2007/02/21 23:00:07 thorpej 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.4  thorpej __KERNEL_RCSID(0, "$NetBSD: if_agrether.c,v 1.4 2007/02/21 23:00:07 thorpej 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.1     yamt 
    102  1.1     yamt 	ether_ifattach(ifp, LLADDR(ifp_port->if_sadl));
    103  1.1     yamt 	ec->ec_capabilities =
    104  1.1     yamt 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING | ETHERCAP_JUMBO_MTU;
    105  1.1     yamt 
    106  1.1     yamt 	ieee8023ad_ctor(sc);
    107  1.1     yamt 
    108  1.1     yamt 	return 0;
    109  1.1     yamt }
    110  1.1     yamt 
    111  1.1     yamt static void
    112  1.1     yamt agrether_dtor(struct agr_softc *sc)
    113  1.1     yamt {
    114  1.1     yamt 	struct agrether_private *priv = sc->sc_iftprivate;
    115  1.1     yamt 
    116  1.1     yamt 	if (priv == NULL) {
    117  1.1     yamt 		return;
    118  1.1     yamt 	}
    119  1.1     yamt 
    120  1.1     yamt 	ieee8023ad_dtor(sc);
    121  1.1     yamt 	agr_mc_purgeall(sc, &priv->aep_multiaddrs);
    122  1.1     yamt 	free(priv, M_DEVBUF);
    123  1.1     yamt 	sc->sc_iftprivate = NULL;
    124  1.1     yamt }
    125  1.1     yamt 
    126  1.1     yamt static int
    127  1.1     yamt agrether_portinit(struct agr_softc *sc, struct agr_port *port)
    128  1.1     yamt {
    129  1.1     yamt 	struct ifreq ifr;
    130  1.1     yamt 	struct agrether_port_private *priv;
    131  1.1     yamt 	int error;
    132  1.1     yamt 	struct ethercom *ec = (void *)&sc->sc_if;
    133  1.1     yamt 	struct ethercom *ec_port = (void *)port->port_ifp;
    134  1.1     yamt 
    135  1.1     yamt 	port->port_media = IFM_ETHER | IFM_NONE;
    136  1.1     yamt 
    137  1.1     yamt 	/*
    138  1.1     yamt 	 * XXX it's better to always advertise ETHERCAP_VLAN_HWTAGGING
    139  1.1     yamt 	 * and do tag insertion by ourselves if necessary,
    140  1.1     yamt 	 * so that we can mix devices with different capabilities.
    141  1.1     yamt 	 * ditto about if_capabilities.
    142  1.1     yamt 	 */
    143  1.1     yamt 
    144  1.1     yamt 	if (ec->ec_capabilities &
    145  1.1     yamt 	    ~ec_port->ec_capabilities &
    146  1.1     yamt 	    (ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING)) {
    147  1.1     yamt 		if (ec->ec_nvlans > 0) {
    148  1.1     yamt 			return EINVAL;
    149  1.1     yamt 		}
    150  1.1     yamt 		ec->ec_capabilities &=
    151  1.1     yamt 		    ec_port->ec_capabilities |
    152  1.1     yamt 		    ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_MTU);
    153  1.1     yamt 	}
    154  1.1     yamt 
    155  1.1     yamt 	/* XXX ETHERCAP_JUMBO_MTU */
    156  1.1     yamt 
    157  1.1     yamt 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
    158  1.1     yamt 	if (priv == NULL) {
    159  1.1     yamt 		return ENOMEM;
    160  1.1     yamt 	}
    161  1.1     yamt 
    162  1.1     yamt 	port->port_iftprivate = priv;
    163  1.1     yamt 
    164  1.1     yamt 	memset(&ifr, 0, sizeof(ifr));
    165  1.1     yamt 	ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr);
    166  1.1     yamt 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    167  1.1     yamt 	KASSERT(sizeof(ifr.ifr_addr) >=
    168  1.1     yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    169  1.1     yamt 	memcpy(&ifr.ifr_addr.sa_data,
    170  1.1     yamt 	    &ethermulticastaddr_slowprotocols,
    171  1.1     yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    172  1.1     yamt 	error = agrport_ioctl(port, SIOCADDMULTI, (caddr_t)&ifr);
    173  1.1     yamt 	if (error) {
    174  1.1     yamt 		free(port->port_iftprivate, M_DEVBUF);
    175  1.1     yamt 		port->port_iftprivate = NULL;
    176  1.1     yamt 		return error;
    177  1.1     yamt 	}
    178  1.1     yamt 
    179  1.1     yamt 	ieee8023ad_portinit(port);
    180  1.1     yamt 
    181  1.1     yamt 	return error;
    182  1.1     yamt }
    183  1.1     yamt 
    184  1.1     yamt static int
    185  1.1     yamt agrether_portfini(struct agr_softc *sc, struct agr_port *port)
    186  1.1     yamt {
    187  1.1     yamt 	struct ifreq ifr;
    188  1.1     yamt 	int error;
    189  1.1     yamt 
    190  1.1     yamt 	if (port->port_iftprivate == NULL) {
    191  1.1     yamt 		return 0;
    192  1.1     yamt 	}
    193  1.1     yamt 
    194  1.1     yamt 	memset(&ifr, 0, sizeof(ifr));
    195  1.1     yamt 	ifr.ifr_addr.sa_len = sizeof(ifr.ifr_addr);
    196  1.1     yamt 	ifr.ifr_addr.sa_family = AF_UNSPEC;
    197  1.1     yamt 	KASSERT(sizeof(ifr.ifr_addr) >=
    198  1.1     yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    199  1.1     yamt 	memcpy(&ifr.ifr_addr.sa_data,
    200  1.1     yamt 	    &ethermulticastaddr_slowprotocols,
    201  1.1     yamt 	    sizeof(ethermulticastaddr_slowprotocols));
    202  1.1     yamt 	error = agrport_ioctl(port, SIOCDELMULTI, (caddr_t)&ifr);
    203  1.1     yamt 	if (error) {
    204  1.1     yamt 		return error;
    205  1.1     yamt 	}
    206  1.1     yamt 
    207  1.1     yamt 	ieee8023ad_portfini(port);
    208  1.1     yamt 
    209  1.1     yamt 	free(port->port_iftprivate, M_DEVBUF);
    210  1.1     yamt 	port->port_iftprivate = NULL;
    211  1.1     yamt 
    212  1.1     yamt 	return error;
    213  1.1     yamt }
    214  1.1     yamt 
    215  1.1     yamt static int
    216  1.1     yamt agrether_configmulti_port(struct agr_softc *sc, struct agr_port *port,
    217  1.4  thorpej     bool add)
    218  1.1     yamt {
    219  1.1     yamt 	struct agrether_private *priv = sc->sc_iftprivate;
    220  1.1     yamt 
    221  1.1     yamt 	return agr_configmulti_port(&priv->aep_multiaddrs, port, add);
    222  1.1     yamt }
    223  1.1     yamt 
    224  1.1     yamt static int
    225  1.1     yamt agrether_configmulti_ifreq(struct agr_softc *sc, struct ifreq *ifr,
    226  1.4  thorpej     bool add)
    227  1.1     yamt {
    228  1.1     yamt 	struct agrether_private *priv = sc->sc_iftprivate;
    229  1.1     yamt 
    230  1.1     yamt 	return agr_configmulti_ifreq(sc, &priv->aep_multiaddrs, ifr, add);
    231  1.1     yamt }
    232  1.1     yamt 
    233  1.1     yamt /* -------------------- */
    234  1.1     yamt 
    235  1.1     yamt static struct agr_port *
    236  1.1     yamt agrether_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    237  1.1     yamt {
    238  1.1     yamt 
    239  1.1     yamt 	return ieee8023ad_select_tx_port(sc, m);
    240  1.1     yamt }
    241