The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_lagg.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $      */
    2 
    3 /*
    4  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
    5  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
    6  *
    7  * Permission to use, copy, modify, and distribute this software for any
    8  * purpose with or without fee is hereby granted, provided that the above
    9  * copyright notice and this permission notice appear in all copies.
   10  *
   11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   18  */
   19 
   20 #include <sys/cdefs.h>
   21 __FBSDID("$FreeBSD$");
   22 
   23 #include "opt_inet.h"
   24 #include "opt_inet6.h"
   25 
   26 #include <sys/param.h>
   27 #include <sys/kernel.h>
   28 #include <sys/malloc.h>
   29 #include <sys/mbuf.h>
   30 #include <sys/queue.h>
   31 #include <sys/socket.h>
   32 #include <sys/sockio.h>
   33 #include <sys/sysctl.h>
   34 #include <sys/module.h>
   35 #include <sys/priv.h>
   36 #include <sys/systm.h>
   37 #include <sys/proc.h>
   38 #include <sys/hash.h>
   39 #include <sys/lock.h>
   40 #include <sys/rwlock.h>
   41 #include <sys/taskqueue.h>
   42 #include <sys/eventhandler.h>
   43 
   44 #include <net/ethernet.h>
   45 #include <net/if.h>
   46 #include <net/if_clone.h>
   47 #include <net/if_arp.h>
   48 #include <net/if_dl.h>
   49 #include <net/if_llc.h>
   50 #include <net/if_media.h>
   51 #include <net/if_types.h>
   52 #include <net/if_var.h>
   53 #include <net/bpf.h>
   54 
   55 #if defined(INET) || defined(INET6)
   56 #include <netinet/in.h>
   57 #include <netinet/ip.h>
   58 #endif
   59 #ifdef INET
   60 #include <netinet/in_systm.h>
   61 #include <netinet/if_ether.h>
   62 #endif
   63 
   64 #ifdef INET6
   65 #include <netinet/ip6.h>
   66 #include <netinet6/in6_var.h>
   67 #include <netinet6/in6_ifattach.h>
   68 #endif
   69 
   70 #include <net/if_vlan_var.h>
   71 #include <net/if_lagg.h>
   72 #include <net/ieee8023ad_lacp.h>
   73 
   74 /* Special flags we should propagate to the lagg ports. */
   75 static struct {
   76         int flag;
   77         int (*func)(struct ifnet *, int);
   78 } lagg_pflags[] = {
   79         {IFF_PROMISC, ifpromisc},
   80         {IFF_ALLMULTI, if_allmulti},
   81         {0, NULL}
   82 };
   83 
   84 SLIST_HEAD(__trhead, lagg_softc) lagg_list;     /* list of laggs */
   85 static struct mtx       lagg_list_mtx;
   86 eventhandler_tag        lagg_detach_cookie = NULL;
   87 
   88 static int      lagg_clone_create(struct if_clone *, int, caddr_t);
   89 static void     lagg_clone_destroy(struct ifnet *);
   90 static void     lagg_lladdr(struct lagg_softc *, uint8_t *);
   91 static void     lagg_capabilities(struct lagg_softc *);
   92 static void     lagg_port_lladdr(struct lagg_port *, uint8_t *);
   93 static void     lagg_port_setlladdr(void *, int);
   94 static int      lagg_port_create(struct lagg_softc *, struct ifnet *);
   95 static int      lagg_port_destroy(struct lagg_port *, int);
   96 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
   97 static void     lagg_linkstate(struct lagg_softc *);
   98 static void     lagg_port_state(struct ifnet *, int);
   99 static int      lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
  100 static int      lagg_port_output(struct ifnet *, struct mbuf *,
  101                     struct sockaddr *, struct route *);
  102 static void     lagg_port_ifdetach(void *arg __unused, struct ifnet *);
  103 #ifdef LAGG_PORT_STACKING
  104 static int      lagg_port_checkstacking(struct lagg_softc *);
  105 #endif
  106 static void     lagg_port2req(struct lagg_port *, struct lagg_reqport *);
  107 static void     lagg_init(void *);
  108 static void     lagg_stop(struct lagg_softc *);
  109 static int      lagg_ioctl(struct ifnet *, u_long, caddr_t);
  110 static int      lagg_ether_setmulti(struct lagg_softc *);
  111 static int      lagg_ether_cmdmulti(struct lagg_port *, int);
  112 static  int     lagg_setflag(struct lagg_port *, int, int,
  113                     int (*func)(struct ifnet *, int));
  114 static  int     lagg_setflags(struct lagg_port *, int status);
  115 static int      lagg_transmit(struct ifnet *, struct mbuf *);
  116 static void     lagg_qflush(struct ifnet *);
  117 static int      lagg_media_change(struct ifnet *);
  118 static void     lagg_media_status(struct ifnet *, struct ifmediareq *);
  119 static struct lagg_port *lagg_link_active(struct lagg_softc *,
  120             struct lagg_port *);
  121 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
  122 
  123 IFC_SIMPLE_DECLARE(lagg, 0);
  124 
  125 /* Simple round robin */
  126 static int      lagg_rr_attach(struct lagg_softc *);
  127 static int      lagg_rr_detach(struct lagg_softc *);
  128 static int      lagg_rr_start(struct lagg_softc *, struct mbuf *);
  129 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
  130                     struct mbuf *);
  131 
  132 /* Active failover */
  133 static int      lagg_fail_attach(struct lagg_softc *);
  134 static int      lagg_fail_detach(struct lagg_softc *);
  135 static int      lagg_fail_start(struct lagg_softc *, struct mbuf *);
  136 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
  137                     struct mbuf *);
  138 
  139 /* Loadbalancing */
  140 static int      lagg_lb_attach(struct lagg_softc *);
  141 static int      lagg_lb_detach(struct lagg_softc *);
  142 static int      lagg_lb_port_create(struct lagg_port *);
  143 static void     lagg_lb_port_destroy(struct lagg_port *);
  144 static int      lagg_lb_start(struct lagg_softc *, struct mbuf *);
  145 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
  146                     struct mbuf *);
  147 static int      lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
  148 
  149 /* 802.3ad LACP */
  150 static int      lagg_lacp_attach(struct lagg_softc *);
  151 static int      lagg_lacp_detach(struct lagg_softc *);
  152 static int      lagg_lacp_start(struct lagg_softc *, struct mbuf *);
  153 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
  154                     struct mbuf *);
  155 static void     lagg_lacp_lladdr(struct lagg_softc *);
  156 
  157 /* lagg protocol table */
  158 static const struct {
  159         int                     ti_proto;
  160         int                     (*ti_attach)(struct lagg_softc *);
  161 } lagg_protos[] = {
  162         { LAGG_PROTO_ROUNDROBIN,        lagg_rr_attach },
  163         { LAGG_PROTO_FAILOVER,          lagg_fail_attach },
  164         { LAGG_PROTO_LOADBALANCE,       lagg_lb_attach },
  165         { LAGG_PROTO_ETHERCHANNEL,      lagg_lb_attach },
  166         { LAGG_PROTO_LACP,              lagg_lacp_attach },
  167         { LAGG_PROTO_NONE,              NULL }
  168 };
  169 
  170 SYSCTL_DECL(_net_link);
  171 static SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
  172     "Link Aggregation");
  173 
  174 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
  175 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
  176     &lagg_failover_rx_all, 0,
  177     "Accept input from any interface in a failover lagg");
  178 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
  179 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
  180 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
  181     &def_use_flowid, 0,
  182     "Default setting for using flow id for load sharing");
  183 
  184 static int
  185 lagg_modevent(module_t mod, int type, void *data)
  186 {
  187 
  188         switch (type) {
  189         case MOD_LOAD:
  190                 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
  191                 SLIST_INIT(&lagg_list);
  192                 if_clone_attach(&lagg_cloner);
  193                 lagg_input_p = lagg_input;
  194                 lagg_linkstate_p = lagg_port_state;
  195                 lagg_detach_cookie = EVENTHANDLER_REGISTER(
  196                     ifnet_departure_event, lagg_port_ifdetach, NULL,
  197                     EVENTHANDLER_PRI_ANY);
  198                 break;
  199         case MOD_UNLOAD:
  200                 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
  201                     lagg_detach_cookie);
  202                 if_clone_detach(&lagg_cloner);
  203                 lagg_input_p = NULL;
  204                 lagg_linkstate_p = NULL;
  205                 mtx_destroy(&lagg_list_mtx);
  206                 break;
  207         default:
  208                 return (EOPNOTSUPP);
  209         }
  210         return (0);
  211 }
  212 
  213 static moduledata_t lagg_mod = {
  214         "if_lagg",
  215         lagg_modevent,
  216         0
  217 };
  218 
  219 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  220 MODULE_VERSION(if_lagg, 1);
  221 
  222 #if __FreeBSD_version >= 800000
  223 /*
  224  * This routine is run via an vlan
  225  * config EVENT
  226  */
  227 static void
  228 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
  229 {
  230         struct lagg_softc       *sc = ifp->if_softc;
  231         struct lagg_port        *lp;
  232 
  233         if (ifp->if_softc !=  arg)   /* Not our event */
  234                 return;
  235 
  236         LAGG_RLOCK(sc);
  237         if (!SLIST_EMPTY(&sc->sc_ports)) {
  238                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
  239                         EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
  240         }
  241         LAGG_RUNLOCK(sc);
  242 }
  243 
  244 /*
  245  * This routine is run via an vlan
  246  * unconfig EVENT
  247  */
  248 static void
  249 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
  250 {
  251         struct lagg_softc       *sc = ifp->if_softc;
  252         struct lagg_port        *lp;
  253 
  254         if (ifp->if_softc !=  arg)   /* Not our event */
  255                 return;
  256 
  257         LAGG_RLOCK(sc);
  258         if (!SLIST_EMPTY(&sc->sc_ports)) {
  259                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
  260                         EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
  261         }
  262         LAGG_RUNLOCK(sc);
  263 }
  264 #endif
  265 
  266 static int
  267 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
  268 {
  269         struct lagg_softc *sc;
  270         struct ifnet *ifp;
  271         int i, error = 0;
  272         static const u_char eaddr[6];   /* 00:00:00:00:00:00 */
  273         struct sysctl_oid *oid;
  274         char num[14];                   /* sufficient for 32 bits */
  275 
  276         sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
  277         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
  278         if (ifp == NULL) {
  279                 free(sc, M_DEVBUF);
  280                 return (ENOSPC);
  281         }
  282 
  283         sysctl_ctx_init(&sc->ctx);
  284         snprintf(num, sizeof(num), "%u", unit);
  285         sc->use_flowid = def_use_flowid;
  286         oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
  287                 OID_AUTO, num, CTLFLAG_RD, NULL, "");
  288         SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  289                 "use_flowid", CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
  290                 "Use flow id for load sharing");
  291         SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
  292                 "count", CTLFLAG_RD, &sc->sc_count, sc->sc_count,
  293                 "Total number of ports");
  294         /* Hash all layers by default */
  295         sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
  296 
  297         sc->sc_proto = LAGG_PROTO_NONE;
  298         for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
  299                 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
  300                         sc->sc_proto = lagg_protos[i].ti_proto;
  301                         if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
  302                                 if_free_type(ifp, IFT_ETHER);
  303                                 free(sc, M_DEVBUF);
  304                                 return (error);
  305                         }
  306                         break;
  307                 }
  308         }
  309         LAGG_LOCK_INIT(sc);
  310         SLIST_INIT(&sc->sc_ports);
  311         TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
  312 
  313         /* Initialise pseudo media types */
  314         ifmedia_init(&sc->sc_media, 0, lagg_media_change,
  315             lagg_media_status);
  316         ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
  317         ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
  318 
  319         if_initname(ifp, ifc->ifc_name, unit);
  320         ifp->if_type = IFT_ETHER;
  321         ifp->if_softc = sc;
  322         ifp->if_transmit = lagg_transmit;
  323         ifp->if_qflush = lagg_qflush;
  324         ifp->if_init = lagg_init;
  325         ifp->if_ioctl = lagg_ioctl;
  326         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
  327 
  328         /*
  329          * Attach as an ordinary ethernet device, childs will be attached
  330          * as special device IFT_IEEE8023ADLAG.
  331          */
  332         ether_ifattach(ifp, eaddr);
  333 
  334 #if __FreeBSD_version >= 800000
  335         sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
  336                 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
  337         sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
  338                 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
  339 #endif
  340 
  341         /* Insert into the global list of laggs */
  342         mtx_lock(&lagg_list_mtx);
  343         SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
  344         mtx_unlock(&lagg_list_mtx);
  345 
  346         return (0);
  347 }
  348 
  349 static void
  350 lagg_clone_destroy(struct ifnet *ifp)
  351 {
  352         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
  353         struct lagg_port *lp;
  354 
  355         LAGG_WLOCK(sc);
  356 
  357         lagg_stop(sc);
  358         ifp->if_flags &= ~IFF_UP;
  359 
  360 #if __FreeBSD_version >= 800000
  361         EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
  362         EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
  363 #endif
  364 
  365         /* Shutdown and remove lagg ports */
  366         while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
  367                 lagg_port_destroy(lp, 1);
  368         /* Unhook the aggregation protocol */
  369         if (sc->sc_detach != NULL)
  370                 (*sc->sc_detach)(sc);
  371 
  372         LAGG_WUNLOCK(sc);
  373 
  374         sysctl_ctx_free(&sc->ctx);
  375         ifmedia_removeall(&sc->sc_media);
  376         ether_ifdetach(ifp);
  377         if_free_type(ifp, IFT_ETHER);
  378 
  379         mtx_lock(&lagg_list_mtx);
  380         SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
  381         mtx_unlock(&lagg_list_mtx);
  382 
  383         taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
  384         LAGG_LOCK_DESTROY(sc);
  385         free(sc, M_DEVBUF);
  386 }
  387 
  388 static void
  389 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
  390 {
  391         struct ifnet *ifp = sc->sc_ifp;
  392 
  393         if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
  394                 return;
  395 
  396         bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
  397         /* Let the protocol know the MAC has changed */
  398         if (sc->sc_lladdr != NULL)
  399                 (*sc->sc_lladdr)(sc);
  400         EVENTHANDLER_INVOKE(iflladdr_event, ifp);
  401 }
  402 
  403 static void
  404 lagg_capabilities(struct lagg_softc *sc)
  405 {
  406         struct lagg_port *lp;
  407         int cap = ~0, ena = ~0;
  408         u_long hwa = ~0UL;
  409         struct ifnet_hw_tsomax hw_tsomax;
  410 
  411         LAGG_WLOCK_ASSERT(sc);
  412 
  413         memset(&hw_tsomax, 0, sizeof(hw_tsomax));
  414 
  415         /* Get capabilities from the lagg ports */
  416         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
  417                 cap &= lp->lp_ifp->if_capabilities;
  418                 ena &= lp->lp_ifp->if_capenable;
  419                 hwa &= lp->lp_ifp->if_hwassist;
  420                 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
  421         }
  422         cap = (cap == ~0 ? 0 : cap);
  423         ena = (ena == ~0 ? 0 : ena);
  424         hwa = (hwa == ~0 ? 0 : hwa);
  425 
  426         if (sc->sc_ifp->if_capabilities != cap ||
  427             sc->sc_ifp->if_capenable != ena ||
  428             sc->sc_ifp->if_hwassist != hwa ||
  429             if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
  430                 sc->sc_ifp->if_capabilities = cap;
  431                 sc->sc_ifp->if_capenable = ena;
  432                 sc->sc_ifp->if_hwassist = hwa;
  433                 getmicrotime(&sc->sc_ifp->if_lastchange);
  434 
  435                 if (sc->sc_ifflags & IFF_DEBUG)
  436                         if_printf(sc->sc_ifp,
  437                             "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
  438         }
  439 }
  440 
  441 static void
  442 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
  443 {
  444         struct lagg_softc *sc = lp->lp_softc;
  445         struct ifnet *ifp = lp->lp_ifp;
  446         struct lagg_llq *llq;
  447         int pending = 0;
  448 
  449         LAGG_WLOCK_ASSERT(sc);
  450 
  451         if (lp->lp_detaching ||
  452             memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
  453                 return;
  454 
  455         /* Check to make sure its not already queued to be changed */
  456         SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
  457                 if (llq->llq_ifp == ifp) {
  458                         pending = 1;
  459                         break;
  460                 }
  461         }
  462 
  463         if (!pending) {
  464                 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
  465                 if (llq == NULL)        /* XXX what to do */
  466                         return;
  467         }
  468 
  469         /* Update the lladdr even if pending, it may have changed */
  470         llq->llq_ifp = ifp;
  471         bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
  472 
  473         if (!pending)
  474                 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
  475 
  476         taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
  477 }
  478 
  479 /*
  480  * Set the interface MAC address from a taskqueue to avoid a LOR.
  481  */
  482 static void
  483 lagg_port_setlladdr(void *arg, int pending)
  484 {
  485         struct lagg_softc *sc = (struct lagg_softc *)arg;
  486         struct lagg_llq *llq, *head;
  487         struct ifnet *ifp;
  488         int error;
  489 
  490         /* Grab a local reference of the queue and remove it from the softc */
  491         LAGG_WLOCK(sc);
  492         head = SLIST_FIRST(&sc->sc_llq_head);
  493         SLIST_FIRST(&sc->sc_llq_head) = NULL;
  494         LAGG_WUNLOCK(sc);
  495 
  496         /*
  497          * Traverse the queue and set the lladdr on each ifp. It is safe to do
  498          * unlocked as we have the only reference to it.
  499          */
  500         for (llq = head; llq != NULL; llq = head) {
  501                 ifp = llq->llq_ifp;
  502 
  503                 /* Set the link layer address */
  504                 CURVNET_SET(ifp->if_vnet);
  505                 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
  506                 CURVNET_RESTORE();
  507                 if (error)
  508                         printf("%s: setlladdr failed on %s\n", __func__,
  509                             ifp->if_xname);
  510 
  511                 head = SLIST_NEXT(llq, llq_entries);
  512                 free(llq, M_DEVBUF);
  513         }
  514 }
  515 
  516 static int
  517 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
  518 {
  519         struct lagg_softc *sc_ptr;
  520         struct lagg_port *lp, *tlp;
  521         int error = 0;
  522 
  523         LAGG_WLOCK_ASSERT(sc);
  524 
  525         /* Limit the maximal number of lagg ports */
  526         if (sc->sc_count >= LAGG_MAX_PORTS)
  527                 return (ENOSPC);
  528 
  529         /* Check if port has already been associated to a lagg */
  530         if (ifp->if_lagg != NULL) {
  531                 /* Port is already in the current lagg? */
  532                 lp = (struct lagg_port *)ifp->if_lagg;
  533                 if (lp->lp_softc == sc)
  534                         return (EEXIST);
  535                 return (EBUSY);
  536         }
  537 
  538         /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
  539         if (ifp->if_type != IFT_ETHER)
  540                 return (EPROTONOSUPPORT);
  541 
  542 #ifdef INET6
  543         /*
  544          * The member interface should not have inet6 address because
  545          * two interfaces with a valid link-local scope zone must not be
  546          * merged in any form.  This restriction is needed to
  547          * prevent violation of link-local scope zone.  Attempts to
  548          * add a member interface which has inet6 addresses triggers
  549          * removal of all inet6 addresses on the member interface.
  550          */
  551         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
  552                 if (in6ifa_llaonifp(lp->lp_ifp)) {
  553                         in6_ifdetach(lp->lp_ifp);
  554                         if_printf(sc->sc_ifp,
  555                             "IPv6 addresses on %s have been removed "
  556                             "before adding it as a member to prevent "
  557                             "IPv6 address scope violation.\n",
  558                             lp->lp_ifp->if_xname);
  559                 }
  560         }
  561         if (in6ifa_llaonifp(ifp)) {
  562                 in6_ifdetach(ifp);
  563                 if_printf(sc->sc_ifp,
  564                     "IPv6 addresses on %s have been removed "
  565                     "before adding it as a member to prevent "
  566                     "IPv6 address scope violation.\n",
  567                     ifp->if_xname);
  568         }
  569 #endif
  570         /* Allow the first Ethernet member to define the MTU */
  571         if (SLIST_EMPTY(&sc->sc_ports))
  572                 sc->sc_ifp->if_mtu = ifp->if_mtu;
  573         else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
  574                 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
  575                     ifp->if_xname);
  576                 return (EINVAL);
  577         }
  578 
  579         if ((lp = malloc(sizeof(struct lagg_port),
  580             M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
  581                 return (ENOMEM);
  582 
  583         /* Check if port is a stacked lagg */
  584         mtx_lock(&lagg_list_mtx);
  585         SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
  586                 if (ifp == sc_ptr->sc_ifp) {
  587                         mtx_unlock(&lagg_list_mtx);
  588                         free(lp, M_DEVBUF);
  589                         return (EINVAL);
  590                         /* XXX disable stacking for the moment, its untested */
  591 #ifdef LAGG_PORT_STACKING
  592                         lp->lp_flags |= LAGG_PORT_STACK;
  593                         if (lagg_port_checkstacking(sc_ptr) >=
  594                             LAGG_MAX_STACKING) {
  595                                 mtx_unlock(&lagg_list_mtx);
  596                                 free(lp, M_DEVBUF);
  597                                 return (E2BIG);
  598                         }
  599 #endif
  600                 }
  601         }
  602         mtx_unlock(&lagg_list_mtx);
  603 
  604         /* Change the interface type */
  605         lp->lp_iftype = ifp->if_type;
  606         ifp->if_type = IFT_IEEE8023ADLAG;
  607         ifp->if_lagg = lp;
  608         lp->lp_ioctl = ifp->if_ioctl;
  609         ifp->if_ioctl = lagg_port_ioctl;
  610         lp->lp_output = ifp->if_output;
  611         ifp->if_output = lagg_port_output;
  612 
  613         lp->lp_ifp = ifp;
  614         lp->lp_softc = sc;
  615 
  616         /* Save port link layer address */
  617         bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
  618 
  619         if (SLIST_EMPTY(&sc->sc_ports)) {
  620                 sc->sc_primary = lp;
  621                 lagg_lladdr(sc, IF_LLADDR(ifp));
  622         } else {
  623                 /* Update link layer address for this port */
  624                 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
  625         }
  626 
  627         /*
  628          * Insert into the list of ports.
  629          * Keep ports sorted by if_index. It is handy, when configuration
  630          * is predictable and `ifconfig laggN create ...` command
  631          * will lead to the same result each time.
  632          */
  633         SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
  634                 if (tlp->lp_ifp->if_index < ifp->if_index && (
  635                     SLIST_NEXT(tlp, lp_entries) == NULL ||
  636                     SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
  637                     ifp->if_index))
  638                         break;
  639         }
  640         if (tlp != NULL)
  641                 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
  642         else
  643                 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
  644         sc->sc_count++;
  645 
  646         /* Update lagg capabilities */
  647         lagg_capabilities(sc);
  648         lagg_linkstate(sc);
  649 
  650         /* Add multicast addresses and interface flags to this port */
  651         lagg_ether_cmdmulti(lp, 1);
  652         lagg_setflags(lp, 1);
  653 
  654         if (sc->sc_port_create != NULL)
  655                 error = (*sc->sc_port_create)(lp);
  656         if (error) {
  657                 /* remove the port again, without calling sc_port_destroy */
  658                 lagg_port_destroy(lp, 0);
  659                 return (error);
  660         }
  661 
  662         return (error);
  663 }
  664 
  665 #ifdef LAGG_PORT_STACKING
  666 static int
  667 lagg_port_checkstacking(struct lagg_softc *sc)
  668 {
  669         struct lagg_softc *sc_ptr;
  670         struct lagg_port *lp;
  671         int m = 0;
  672 
  673         LAGG_WLOCK_ASSERT(sc);
  674 
  675         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
  676                 if (lp->lp_flags & LAGG_PORT_STACK) {
  677                         sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
  678                         m = MAX(m, lagg_port_checkstacking(sc_ptr));
  679                 }
  680         }
  681 
  682         return (m + 1);
  683 }
  684 #endif
  685 
  686 static int
  687 lagg_port_destroy(struct lagg_port *lp, int runpd)
  688 {
  689         struct lagg_softc *sc = lp->lp_softc;
  690         struct lagg_port *lp_ptr;
  691         struct lagg_llq *llq;
  692         struct ifnet *ifp = lp->lp_ifp;
  693 
  694         LAGG_WLOCK_ASSERT(sc);
  695 
  696         if (runpd && sc->sc_port_destroy != NULL)
  697                 (*sc->sc_port_destroy)(lp);
  698 
  699         /*
  700          * Remove multicast addresses and interface flags from this port and
  701          * reset the MAC address, skip if the interface is being detached.
  702          */
  703         if (!lp->lp_detaching) {
  704                 lagg_ether_cmdmulti(lp, 0);
  705                 lagg_setflags(lp, 0);
  706                 lagg_port_lladdr(lp, lp->lp_lladdr);
  707         }
  708 
  709         /* Restore interface */
  710         ifp->if_type = lp->lp_iftype;
  711         ifp->if_ioctl = lp->lp_ioctl;
  712         ifp->if_output = lp->lp_output;
  713         ifp->if_lagg = NULL;
  714 
  715         /* Finally, remove the port from the lagg */
  716         SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
  717         sc->sc_count--;
  718 
  719         /* Update the primary interface */
  720         if (lp == sc->sc_primary) {
  721                 uint8_t lladdr[ETHER_ADDR_LEN];
  722 
  723                 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
  724                         bzero(&lladdr, ETHER_ADDR_LEN);
  725                 } else {
  726                         bcopy(lp_ptr->lp_lladdr,
  727                             lladdr, ETHER_ADDR_LEN);
  728                 }
  729                 lagg_lladdr(sc, lladdr);
  730                 sc->sc_primary = lp_ptr;
  731 
  732                 /* Update link layer address for each port */
  733                 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
  734                         lagg_port_lladdr(lp_ptr, lladdr);
  735         }
  736 
  737         /* Remove any pending lladdr changes from the queue */
  738         if (lp->lp_detaching) {
  739                 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
  740                         if (llq->llq_ifp == ifp) {
  741                                 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
  742                                     llq_entries);
  743                                 free(llq, M_DEVBUF);
  744                                 break;  /* Only appears once */
  745                         }
  746                 }
  747         }
  748 
  749         if (lp->lp_ifflags)
  750                 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
  751 
  752         free(lp, M_DEVBUF);
  753 
  754         /* Update lagg capabilities */
  755         lagg_capabilities(sc);
  756         lagg_linkstate(sc);
  757 
  758         return (0);
  759 }
  760 
  761 static int
  762 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  763 {
  764         struct lagg_reqport *rp = (struct lagg_reqport *)data;
  765         struct lagg_softc *sc;
  766         struct lagg_port *lp = NULL;
  767         int error = 0;
  768 
  769         /* Should be checked by the caller */
  770         if (ifp->if_type != IFT_IEEE8023ADLAG ||
  771             (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
  772                 goto fallback;
  773 
  774         switch (cmd) {
  775         case SIOCGLAGGPORT:
  776                 if (rp->rp_portname[0] == '\0' ||
  777                     ifunit(rp->rp_portname) != ifp) {
  778                         error = EINVAL;
  779                         break;
  780                 }
  781 
  782                 LAGG_RLOCK(sc);
  783                 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
  784                         error = ENOENT;
  785                         LAGG_RUNLOCK(sc);
  786                         break;
  787                 }
  788 
  789                 lagg_port2req(lp, rp);
  790                 LAGG_RUNLOCK(sc);
  791                 break;
  792 
  793         case SIOCSIFCAP:
  794                 if (lp->lp_ioctl == NULL) {
  795                         error = EINVAL;
  796                         break;
  797                 }
  798                 error = (*lp->lp_ioctl)(ifp, cmd, data);
  799                 if (error)
  800                         break;
  801 
  802                 /* Update lagg interface capabilities */
  803                 LAGG_WLOCK(sc);
  804                 lagg_capabilities(sc);
  805                 LAGG_WUNLOCK(sc);
  806                 break;
  807 
  808         case SIOCSIFMTU:
  809                 /* Do not allow the MTU to be changed once joined */
  810                 error = EINVAL;
  811                 break;
  812 
  813         default:
  814                 goto fallback;
  815         }
  816 
  817         return (error);
  818 
  819 fallback:
  820         if (lp->lp_ioctl != NULL)
  821                 return ((*lp->lp_ioctl)(ifp, cmd, data));
  822 
  823         return (EINVAL);
  824 }
  825 
  826 /*
  827  * For direct output to child ports.
  828  */
  829 static int
  830 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
  831         struct sockaddr *dst, struct route *ro)
  832 {
  833         struct lagg_port *lp = ifp->if_lagg;
  834 
  835         switch (dst->sa_family) {
  836                 case pseudo_AF_HDRCMPLT:
  837                 case AF_UNSPEC:
  838                         return ((*lp->lp_output)(ifp, m, dst, ro));
  839         }
  840 
  841         /* drop any other frames */
  842         m_freem(m);
  843         return (ENETDOWN);
  844 }
  845 
  846 static void
  847 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
  848 {
  849         struct lagg_port *lp;
  850         struct lagg_softc *sc;
  851 
  852         if ((lp = ifp->if_lagg) == NULL)
  853                 return;
  854         /* If the ifnet is just being renamed, don't do anything. */
  855         if (ifp->if_flags & IFF_RENAMING)
  856                 return;
  857 
  858         sc = lp->lp_softc;
  859 
  860         LAGG_WLOCK(sc);
  861         lp->lp_detaching = 1;
  862         lagg_port_destroy(lp, 1);
  863         LAGG_WUNLOCK(sc);
  864 }
  865 
  866 static void
  867 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
  868 {
  869         struct lagg_softc *sc = lp->lp_softc;
  870 
  871         strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
  872         strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
  873         rp->rp_prio = lp->lp_prio;
  874         rp->rp_flags = lp->lp_flags;
  875         if (sc->sc_portreq != NULL)
  876                 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
  877 
  878         /* Add protocol specific flags */
  879         switch (sc->sc_proto) {
  880                 case LAGG_PROTO_FAILOVER:
  881                         if (lp == sc->sc_primary)
  882                                 rp->rp_flags |= LAGG_PORT_MASTER;
  883                         if (lp == lagg_link_active(sc, sc->sc_primary))
  884                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
  885                         break;
  886 
  887                 case LAGG_PROTO_ROUNDROBIN:
  888                 case LAGG_PROTO_LOADBALANCE:
  889                 case LAGG_PROTO_ETHERCHANNEL:
  890                         if (LAGG_PORTACTIVE(lp))
  891                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
  892                         break;
  893 
  894                 case LAGG_PROTO_LACP:
  895                         /* LACP has a different definition of active */
  896                         if (lacp_isactive(lp))
  897                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
  898                         if (lacp_iscollecting(lp))
  899                                 rp->rp_flags |= LAGG_PORT_COLLECTING;
  900                         if (lacp_isdistributing(lp))
  901                                 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
  902                         break;
  903         }
  904 
  905 }
  906 
  907 static void
  908 lagg_init(void *xsc)
  909 {
  910         struct lagg_softc *sc = (struct lagg_softc *)xsc;
  911         struct lagg_port *lp;
  912         struct ifnet *ifp = sc->sc_ifp;
  913 
  914         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
  915                 return;
  916 
  917         LAGG_WLOCK(sc);
  918 
  919         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  920         /* Update the port lladdrs */
  921         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
  922                 lagg_port_lladdr(lp, IF_LLADDR(ifp));
  923 
  924         if (sc->sc_init != NULL)
  925                 (*sc->sc_init)(sc);
  926 
  927         LAGG_WUNLOCK(sc);
  928 }
  929 
  930 static void
  931 lagg_stop(struct lagg_softc *sc)
  932 {
  933         struct ifnet *ifp = sc->sc_ifp;
  934 
  935         LAGG_WLOCK_ASSERT(sc);
  936 
  937         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
  938                 return;
  939 
  940         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  941 
  942         if (sc->sc_stop != NULL)
  943                 (*sc->sc_stop)(sc);
  944 }
  945 
  946 static int
  947 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  948 {
  949         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
  950         struct lagg_reqall *ra = (struct lagg_reqall *)data;
  951         struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
  952         struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
  953         struct ifreq *ifr = (struct ifreq *)data;
  954         struct lagg_port *lp;
  955         struct ifnet *tpif;
  956         struct thread *td = curthread;
  957         char *buf, *outbuf;
  958         int count, buflen, len, error = 0;
  959 
  960         bzero(&rpbuf, sizeof(rpbuf));
  961 
  962         switch (cmd) {
  963         case SIOCGLAGG:
  964                 LAGG_RLOCK(sc);
  965                 count = 0;
  966                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
  967                         count++;
  968                 buflen = count * sizeof(struct lagg_reqport);
  969                 LAGG_RUNLOCK(sc);
  970 
  971                 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
  972 
  973                 LAGG_RLOCK(sc);
  974                 ra->ra_proto = sc->sc_proto;
  975                 if (sc->sc_req != NULL)
  976                         (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
  977 
  978                 count = 0;
  979                 buf = outbuf;
  980                 len = min(ra->ra_size, buflen);
  981                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
  982                         if (len < sizeof(rpbuf))
  983                                 break;
  984 
  985                         lagg_port2req(lp, &rpbuf);
  986                         memcpy(buf, &rpbuf, sizeof(rpbuf));
  987                         count++;
  988                         buf += sizeof(rpbuf);
  989                         len -= sizeof(rpbuf);
  990                 }
  991                 LAGG_RUNLOCK(sc);
  992                 ra->ra_ports = count;
  993                 ra->ra_size = count * sizeof(rpbuf);
  994                 error = copyout(outbuf, ra->ra_port, ra->ra_size);
  995                 free(outbuf, M_TEMP);
  996                 break;
  997         case SIOCSLAGG:
  998                 error = priv_check(td, PRIV_NET_LAGG);
  999                 if (error)
 1000                         break;
 1001                 if (ra->ra_proto >= LAGG_PROTO_MAX) {
 1002                         error = EPROTONOSUPPORT;
 1003                         break;
 1004                 }
 1005                 LAGG_WLOCK(sc);
 1006                 if (sc->sc_proto != LAGG_PROTO_NONE) {
 1007                         /* Reset protocol first in case detach unlocks */
 1008                         sc->sc_proto = LAGG_PROTO_NONE;
 1009                         error = sc->sc_detach(sc);
 1010                         sc->sc_detach = NULL;
 1011                         sc->sc_start = NULL;
 1012                         sc->sc_input = NULL;
 1013                         sc->sc_port_create = NULL;
 1014                         sc->sc_port_destroy = NULL;
 1015                         sc->sc_linkstate = NULL;
 1016                         sc->sc_init = NULL;
 1017                         sc->sc_stop = NULL;
 1018                         sc->sc_lladdr = NULL;
 1019                         sc->sc_req = NULL;
 1020                         sc->sc_portreq = NULL;
 1021                 } else if (sc->sc_input != NULL) {
 1022                         /* Still detaching */
 1023                         error = EBUSY;
 1024                 }
 1025                 if (error != 0) {
 1026                         LAGG_WUNLOCK(sc);
 1027                         break;
 1028                 }
 1029                 for (int i = 0; i < (sizeof(lagg_protos) /
 1030                     sizeof(lagg_protos[0])); i++) {
 1031                         if (lagg_protos[i].ti_proto == ra->ra_proto) {
 1032                                 if (sc->sc_ifflags & IFF_DEBUG)
 1033                                         printf("%s: using proto %u\n",
 1034                                             sc->sc_ifname,
 1035                                             lagg_protos[i].ti_proto);
 1036                                 sc->sc_proto = lagg_protos[i].ti_proto;
 1037                                 if (sc->sc_proto != LAGG_PROTO_NONE)
 1038                                         error = lagg_protos[i].ti_attach(sc);
 1039                                 LAGG_WUNLOCK(sc);
 1040                                 return (error);
 1041                         }
 1042                 }
 1043                 LAGG_WUNLOCK(sc);
 1044                 error = EPROTONOSUPPORT;
 1045                 break;
 1046         case SIOCGLAGGFLAGS:
 1047                 rf->rf_flags = sc->sc_flags;
 1048                 break;
 1049         case SIOCSLAGGHASH:
 1050                 error = priv_check(td, PRIV_NET_LAGG);
 1051                 if (error)
 1052                         break;
 1053                 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
 1054                         error = EINVAL;
 1055                         break;
 1056                 }
 1057                 LAGG_WLOCK(sc);
 1058                 sc->sc_flags &= ~LAGG_F_HASHMASK;
 1059                 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
 1060                 LAGG_WUNLOCK(sc);
 1061                 break;
 1062         case SIOCGLAGGPORT:
 1063                 if (rp->rp_portname[0] == '\0' ||
 1064                     (tpif = ifunit(rp->rp_portname)) == NULL) {
 1065                         error = EINVAL;
 1066                         break;
 1067                 }
 1068 
 1069                 LAGG_RLOCK(sc);
 1070                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
 1071                     lp->lp_softc != sc) {
 1072                         error = ENOENT;
 1073                         LAGG_RUNLOCK(sc);
 1074                         break;
 1075                 }
 1076 
 1077                 lagg_port2req(lp, rp);
 1078                 LAGG_RUNLOCK(sc);
 1079                 break;
 1080         case SIOCSLAGGPORT:
 1081                 error = priv_check(td, PRIV_NET_LAGG);
 1082                 if (error)
 1083                         break;
 1084                 if (rp->rp_portname[0] == '\0' ||
 1085                     (tpif = ifunit(rp->rp_portname)) == NULL) {
 1086                         error = EINVAL;
 1087                         break;
 1088                 }
 1089                 LAGG_WLOCK(sc);
 1090                 error = lagg_port_create(sc, tpif);
 1091                 LAGG_WUNLOCK(sc);
 1092                 break;
 1093         case SIOCSLAGGDELPORT:
 1094                 error = priv_check(td, PRIV_NET_LAGG);
 1095                 if (error)
 1096                         break;
 1097                 if (rp->rp_portname[0] == '\0' ||
 1098                     (tpif = ifunit(rp->rp_portname)) == NULL) {
 1099                         error = EINVAL;
 1100                         break;
 1101                 }
 1102 
 1103                 LAGG_WLOCK(sc);
 1104                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
 1105                     lp->lp_softc != sc) {
 1106                         error = ENOENT;
 1107                         LAGG_WUNLOCK(sc);
 1108                         break;
 1109                 }
 1110 
 1111                 error = lagg_port_destroy(lp, 1);
 1112                 LAGG_WUNLOCK(sc);
 1113                 break;
 1114         case SIOCSIFFLAGS:
 1115                 /* Set flags on ports too */
 1116                 LAGG_WLOCK(sc);
 1117                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
 1118                         lagg_setflags(lp, 1);
 1119                 }
 1120                 LAGG_WUNLOCK(sc);
 1121 
 1122                 if (!(ifp->if_flags & IFF_UP) &&
 1123                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
 1124                         /*
 1125                          * If interface is marked down and it is running,
 1126                          * then stop and disable it.
 1127                          */
 1128                         LAGG_WLOCK(sc);
 1129                         lagg_stop(sc);
 1130                         LAGG_WUNLOCK(sc);
 1131                 } else if ((ifp->if_flags & IFF_UP) &&
 1132                     !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
 1133                         /*
 1134                          * If interface is marked up and it is stopped, then
 1135                          * start it.
 1136                          */
 1137                         (*ifp->if_init)(sc);
 1138                 }
 1139                 break;
 1140         case SIOCADDMULTI:
 1141         case SIOCDELMULTI:
 1142                 LAGG_WLOCK(sc);
 1143                 error = lagg_ether_setmulti(sc);
 1144                 LAGG_WUNLOCK(sc);
 1145                 break;
 1146         case SIOCSIFMEDIA:
 1147         case SIOCGIFMEDIA:
 1148                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
 1149                 break;
 1150 
 1151         case SIOCSIFCAP:
 1152         case SIOCSIFMTU:
 1153                 /* Do not allow the MTU or caps to be directly changed */
 1154                 error = EINVAL;
 1155                 break;
 1156 
 1157         default:
 1158                 error = ether_ioctl(ifp, cmd, data);
 1159                 break;
 1160         }
 1161         return (error);
 1162 }
 1163 
 1164 static int
 1165 lagg_ether_setmulti(struct lagg_softc *sc)
 1166 {
 1167         struct lagg_port *lp;
 1168 
 1169         LAGG_WLOCK_ASSERT(sc);
 1170 
 1171         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
 1172                 /* First, remove any existing filter entries. */
 1173                 lagg_ether_cmdmulti(lp, 0);
 1174                 /* copy all addresses from the lagg interface to the port */
 1175                 lagg_ether_cmdmulti(lp, 1);
 1176         }
 1177         return (0);
 1178 }
 1179 
 1180 static int
 1181 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
 1182 {
 1183         struct lagg_softc *sc = lp->lp_softc;
 1184         struct ifnet *ifp = lp->lp_ifp;
 1185         struct ifnet *scifp = sc->sc_ifp;
 1186         struct lagg_mc *mc;
 1187         struct ifmultiaddr *ifma, *rifma = NULL;
 1188         struct sockaddr_dl sdl;
 1189         int error;
 1190 
 1191         LAGG_WLOCK_ASSERT(sc);
 1192 
 1193         bzero((char *)&sdl, sizeof(sdl));
 1194         sdl.sdl_len = sizeof(sdl);
 1195         sdl.sdl_family = AF_LINK;
 1196         sdl.sdl_type = IFT_ETHER;
 1197         sdl.sdl_alen = ETHER_ADDR_LEN;
 1198         sdl.sdl_index = ifp->if_index;
 1199 
 1200         if (set) {
 1201                 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
 1202                         if (ifma->ifma_addr->sa_family != AF_LINK)
 1203                                 continue;
 1204                         bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
 1205                             LLADDR(&sdl), ETHER_ADDR_LEN);
 1206 
 1207                         error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
 1208                         if (error)
 1209                                 return (error);
 1210                         mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
 1211                         if (mc == NULL)
 1212                                 return (ENOMEM);
 1213                         mc->mc_ifma = rifma;
 1214                         SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
 1215                 }
 1216         } else {
 1217                 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
 1218                         SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
 1219                         if_delmulti_ifma(mc->mc_ifma);
 1220                         free(mc, M_DEVBUF);
 1221                 }
 1222         }
 1223         return (0);
 1224 }
 1225 
 1226 /* Handle a ref counted flag that should be set on the lagg port as well */
 1227 static int
 1228 lagg_setflag(struct lagg_port *lp, int flag, int status,
 1229              int (*func)(struct ifnet *, int))
 1230 {
 1231         struct lagg_softc *sc = lp->lp_softc;
 1232         struct ifnet *scifp = sc->sc_ifp;
 1233         struct ifnet *ifp = lp->lp_ifp;
 1234         int error;
 1235 
 1236         LAGG_WLOCK_ASSERT(sc);
 1237 
 1238         status = status ? (scifp->if_flags & flag) : 0;
 1239         /* Now "status" contains the flag value or 0 */
 1240 
 1241         /*
 1242          * See if recorded ports status is different from what
 1243          * we want it to be.  If it is, flip it.  We record ports
 1244          * status in lp_ifflags so that we won't clear ports flag
 1245          * we haven't set.  In fact, we don't clear or set ports
 1246          * flags directly, but get or release references to them.
 1247          * That's why we can be sure that recorded flags still are
 1248          * in accord with actual ports flags.
 1249          */
 1250         if (status != (lp->lp_ifflags & flag)) {
 1251                 error = (*func)(ifp, status);
 1252                 if (error)
 1253                         return (error);
 1254                 lp->lp_ifflags &= ~flag;
 1255                 lp->lp_ifflags |= status;
 1256         }
 1257         return (0);
 1258 }
 1259 
 1260 /*
 1261  * Handle IFF_* flags that require certain changes on the lagg port
 1262  * if "status" is true, update ports flags respective to the lagg
 1263  * if "status" is false, forcedly clear the flags set on port.
 1264  */
 1265 static int
 1266 lagg_setflags(struct lagg_port *lp, int status)
 1267 {
 1268         int error, i;
 1269 
 1270         for (i = 0; lagg_pflags[i].flag; i++) {
 1271                 error = lagg_setflag(lp, lagg_pflags[i].flag,
 1272                     status, lagg_pflags[i].func);
 1273                 if (error)
 1274                         return (error);
 1275         }
 1276         return (0);
 1277 }
 1278 
 1279 static int
 1280 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
 1281 {
 1282         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
 1283         int error, len, mcast;
 1284 
 1285         len = m->m_pkthdr.len;
 1286         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
 1287 
 1288         LAGG_RLOCK(sc);
 1289         /* We need a Tx algorithm and at least one port */
 1290         if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
 1291                 LAGG_RUNLOCK(sc);
 1292                 m_freem(m);
 1293                 ifp->if_oerrors++;
 1294                 return (ENXIO);
 1295         }
 1296 
 1297         ETHER_BPF_MTAP(ifp, m);
 1298 
 1299         error = (*sc->sc_start)(sc, m);
 1300         LAGG_RUNLOCK(sc);
 1301 
 1302         if (error == 0) {
 1303                 ifp->if_opackets++;
 1304                 ifp->if_omcasts += mcast;
 1305                 ifp->if_obytes += len;
 1306         } else
 1307                 ifp->if_oerrors++;
 1308 
 1309         return (error);
 1310 }
 1311 
 1312 /*
 1313  * The ifp->if_qflush entry point for lagg(4) is no-op.
 1314  */
 1315 static void
 1316 lagg_qflush(struct ifnet *ifp __unused)
 1317 {
 1318 }
 1319 
 1320 static struct mbuf *
 1321 lagg_input(struct ifnet *ifp, struct mbuf *m)
 1322 {
 1323         struct lagg_port *lp = ifp->if_lagg;
 1324         struct lagg_softc *sc = lp->lp_softc;
 1325         struct ifnet *scifp = sc->sc_ifp;
 1326 
 1327         LAGG_RLOCK(sc);
 1328         if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
 1329             (lp->lp_flags & LAGG_PORT_DISABLED) ||
 1330             sc->sc_proto == LAGG_PROTO_NONE) {
 1331                 LAGG_RUNLOCK(sc);
 1332                 m_freem(m);
 1333                 return (NULL);
 1334         }
 1335 
 1336         ETHER_BPF_MTAP(scifp, m);
 1337 
 1338         m = (*sc->sc_input)(sc, lp, m);
 1339 
 1340         if (m != NULL) {
 1341                 scifp->if_ipackets++;
 1342                 scifp->if_ibytes += m->m_pkthdr.len;
 1343 
 1344                 if (scifp->if_flags & IFF_MONITOR) {
 1345                         m_freem(m);
 1346                         m = NULL;
 1347                 }
 1348         }
 1349 
 1350         LAGG_RUNLOCK(sc);
 1351         return (m);
 1352 }
 1353 
 1354 static int
 1355 lagg_media_change(struct ifnet *ifp)
 1356 {
 1357         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
 1358 
 1359         if (sc->sc_ifflags & IFF_DEBUG)
 1360                 printf("%s\n", __func__);
 1361 
 1362         /* Ignore */
 1363         return (0);
 1364 }
 1365 
 1366 static void
 1367 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
 1368 {
 1369         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
 1370         struct lagg_port *lp;
 1371 
 1372         imr->ifm_status = IFM_AVALID;
 1373         imr->ifm_active = IFM_ETHER | IFM_AUTO;
 1374 
 1375         LAGG_RLOCK(sc);
 1376         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
 1377                 if (LAGG_PORTACTIVE(lp))
 1378                         imr->ifm_status |= IFM_ACTIVE;
 1379         }
 1380         LAGG_RUNLOCK(sc);
 1381 }
 1382 
 1383 static void
 1384 lagg_linkstate(struct lagg_softc *sc)
 1385 {
 1386         struct lagg_port *lp;
 1387         int new_link = LINK_STATE_DOWN;
 1388         uint64_t speed;
 1389 
 1390         /* Our link is considered up if at least one of our ports is active */
 1391         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
 1392                 if (lp->lp_link_state == LINK_STATE_UP) {
 1393                         new_link = LINK_STATE_UP;
 1394                         break;
 1395                 }
 1396         }
 1397         if_link_state_change(sc->sc_ifp, new_link);
 1398 
 1399         /* Update if_baudrate to reflect the max possible speed */
 1400         switch (sc->sc_proto) {
 1401                 case LAGG_PROTO_FAILOVER:
 1402                         sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
 1403                             sc->sc_primary->lp_ifp->if_baudrate : 0;
 1404                         break;
 1405                 case LAGG_PROTO_ROUNDROBIN:
 1406                 case LAGG_PROTO_LOADBALANCE:
 1407                 case LAGG_PROTO_ETHERCHANNEL:
 1408                         speed = 0;
 1409                         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
 1410                                 speed += lp->lp_ifp->if_baudrate;
 1411                         sc->sc_ifp->if_baudrate = speed;
 1412                         break;
 1413                 case LAGG_PROTO_LACP:
 1414                         /* LACP updates if_baudrate itself */
 1415                         break;
 1416         }
 1417 }
 1418 
 1419 static void
 1420 lagg_port_state(struct ifnet *ifp, int state)
 1421 {
 1422         struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
 1423         struct lagg_softc *sc = NULL;
 1424 
 1425         if (lp != NULL)
 1426                 sc = lp->lp_softc;
 1427         if (sc == NULL)
 1428                 return;
 1429 
 1430         LAGG_WLOCK(sc);
 1431         lagg_linkstate(sc);
 1432         if (sc->sc_linkstate != NULL)
 1433                 (*sc->sc_linkstate)(lp);
 1434         LAGG_WUNLOCK(sc);
 1435 }
 1436 
 1437 struct lagg_port *
 1438 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
 1439 {
 1440         struct lagg_port *lp_next, *rval = NULL;
 1441         // int new_link = LINK_STATE_DOWN;
 1442 
 1443         LAGG_RLOCK_ASSERT(sc);
 1444         /*
 1445          * Search a port which reports an active link state.
 1446          */
 1447 
 1448         if (lp == NULL)
 1449                 goto search;
 1450         if (LAGG_PORTACTIVE(lp)) {
 1451                 rval = lp;
 1452                 goto found;
 1453         }
 1454         if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
 1455             LAGG_PORTACTIVE(lp_next)) {
 1456                 rval = lp_next;
 1457                 goto found;
 1458         }
 1459 
 1460 search:
 1461         SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
 1462                 if (LAGG_PORTACTIVE(lp_next)) {
 1463                         rval = lp_next;
 1464                         goto found;
 1465                 }
 1466         }
 1467 
 1468 found:
 1469         if (rval != NULL) {
 1470                 /*
 1471                  * The IEEE 802.1D standard assumes that a lagg with
 1472                  * multiple ports is always full duplex. This is valid
 1473                  * for load sharing laggs and if at least two links
 1474                  * are active. Unfortunately, checking the latter would
 1475                  * be too expensive at this point.
 1476                  XXX
 1477                 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
 1478                     (sc->sc_count > 1))
 1479                         new_link = LINK_STATE_FULL_DUPLEX;
 1480                 else
 1481                         new_link = rval->lp_link_state;
 1482                  */
 1483         }
 1484 
 1485         return (rval);
 1486 }
 1487 
 1488 static const void *
 1489 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
 1490 {
 1491         if (m->m_pkthdr.len < (off + len)) {
 1492                 return (NULL);
 1493         } else if (m->m_len < (off + len)) {
 1494                 m_copydata(m, off, len, buf);
 1495                 return (buf);
 1496         }
 1497         return (mtod(m, char *) + off);
 1498 }
 1499 
 1500 uint32_t
 1501 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
 1502 {
 1503         uint16_t etype;
 1504         uint32_t p = key;
 1505         int off;
 1506         struct ether_header *eh;
 1507         const struct ether_vlan_header *vlan;
 1508 #ifdef INET
 1509         const struct ip *ip;
 1510         const uint32_t *ports;
 1511         int iphlen;
 1512 #endif
 1513 #ifdef INET6
 1514         const struct ip6_hdr *ip6;
 1515         uint32_t flow;
 1516 #endif
 1517         union {
 1518 #ifdef INET
 1519                 struct ip ip;
 1520 #endif
 1521 #ifdef INET6
 1522                 struct ip6_hdr ip6;
 1523 #endif
 1524                 struct ether_vlan_header vlan;
 1525                 uint32_t port;
 1526         } buf;
 1527 
 1528 
 1529         off = sizeof(*eh);
 1530         if (m->m_len < off)
 1531                 goto out;
 1532         eh = mtod(m, struct ether_header *);
 1533         etype = ntohs(eh->ether_type);
 1534         if (sc->sc_flags & LAGG_F_HASHL2) {
 1535                 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
 1536                 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
 1537         }
 1538 
 1539         /* Special handling for encapsulating VLAN frames */
 1540         if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
 1541                 p = hash32_buf(&m->m_pkthdr.ether_vtag,
 1542                     sizeof(m->m_pkthdr.ether_vtag), p);
 1543         } else if (etype == ETHERTYPE_VLAN) {
 1544                 vlan = lagg_gethdr(m, off,  sizeof(*vlan), &buf);
 1545                 if (vlan == NULL)
 1546                         goto out;
 1547 
 1548                 if (sc->sc_flags & LAGG_F_HASHL2)
 1549                         p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
 1550                 etype = ntohs(vlan->evl_proto);
 1551                 off += sizeof(*vlan) - sizeof(*eh);
 1552         }
 1553 
 1554         switch (etype) {
 1555 #ifdef INET
 1556         case ETHERTYPE_IP:
 1557                 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
 1558                 if (ip == NULL)
 1559                         goto out;
 1560 
 1561                 if (sc->sc_flags & LAGG_F_HASHL3) {
 1562                         p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
 1563                         p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
 1564                 }
 1565                 if (!(sc->sc_flags & LAGG_F_HASHL4))
 1566                         break;
 1567                 switch (ip->ip_p) {
 1568                         case IPPROTO_TCP:
 1569                         case IPPROTO_UDP:
 1570                         case IPPROTO_SCTP:
 1571                                 iphlen = ip->ip_hl << 2;
 1572                                 if (iphlen < sizeof(*ip))
 1573                                         break;
 1574                                 off += iphlen;
 1575                                 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
 1576                                 if (ports == NULL)
 1577                                         break;
 1578                                 p = hash32_buf(ports, sizeof(*ports), p);
 1579                                 break;
 1580                 }
 1581                 break;
 1582 #endif
 1583 #ifdef INET6
 1584         case ETHERTYPE_IPV6:
 1585                 if (!(sc->sc_flags & LAGG_F_HASHL3))
 1586                         break;
 1587                 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
 1588                 if (ip6 == NULL)
 1589                         goto out;
 1590 
 1591                 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
 1592                 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
 1593                 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
 1594                 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
 1595                 break;
 1596 #endif
 1597         }
 1598 out:
 1599         return (p);
 1600 }
 1601 
 1602 int
 1603 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
 1604 {
 1605 
 1606         return (ifp->if_transmit)(ifp, m);
 1607 }
 1608 
 1609 /*
 1610  * Simple round robin aggregation
 1611  */
 1612 
 1613 static int
 1614 lagg_rr_attach(struct lagg_softc *sc)
 1615 {
 1616         sc->sc_detach = lagg_rr_detach;
 1617         sc->sc_start = lagg_rr_start;
 1618         sc->sc_input = lagg_rr_input;
 1619         sc->sc_port_create = NULL;
 1620         sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
 1621         sc->sc_seq = 0;
 1622 
 1623         return (0);
 1624 }
 1625 
 1626 static int
 1627 lagg_rr_detach(struct lagg_softc *sc)
 1628 {
 1629         return (0);
 1630 }
 1631 
 1632 static int
 1633 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
 1634 {
 1635         struct lagg_port *lp;
 1636         uint32_t p;
 1637 
 1638         p = atomic_fetchadd_32(&sc->sc_seq, 1);
 1639         p %= sc->sc_count;
 1640         lp = SLIST_FIRST(&sc->sc_ports);
 1641         while (p--)
 1642                 lp = SLIST_NEXT(lp, lp_entries);
 1643 
 1644         /*
 1645          * Check the port's link state. This will return the next active
 1646          * port if the link is down or the port is NULL.
 1647          */
 1648         if ((lp = lagg_link_active(sc, lp)) == NULL) {
 1649                 m_freem(m);
 1650                 return (ENETDOWN);
 1651         }
 1652 
 1653         /* Send mbuf */
 1654         return (lagg_enqueue(lp->lp_ifp, m));
 1655 }
 1656 
 1657 static struct mbuf *
 1658 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
 1659 {
 1660         struct ifnet *ifp = sc->sc_ifp;
 1661 
 1662         /* Just pass in the packet to our lagg device */
 1663         m->m_pkthdr.rcvif = ifp;
 1664 
 1665         return (m);
 1666 }
 1667 
 1668 /*
 1669  * Active failover
 1670  */
 1671 
 1672 static int
 1673 lagg_fail_attach(struct lagg_softc *sc)
 1674 {
 1675         sc->sc_detach = lagg_fail_detach;
 1676         sc->sc_start = lagg_fail_start;
 1677         sc->sc_input = lagg_fail_input;
 1678         sc->sc_port_create = NULL;
 1679         sc->sc_port_destroy = NULL;
 1680 
 1681         return (0);
 1682 }
 1683 
 1684 static int
 1685 lagg_fail_detach(struct lagg_softc *sc)
 1686 {
 1687         return (0);
 1688 }
 1689 
 1690 static int
 1691 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
 1692 {
 1693         struct lagg_port *lp;
 1694 
 1695         /* Use the master port if active or the next available port */
 1696         if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
 1697                 m_freem(m);
 1698                 return (ENETDOWN);
 1699         }
 1700 
 1701         /* Send mbuf */
 1702         return (lagg_enqueue(lp->lp_ifp, m));
 1703 }
 1704 
 1705 static struct mbuf *
 1706 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
 1707 {
 1708         struct ifnet *ifp = sc->sc_ifp;
 1709         struct lagg_port *tmp_tp;
 1710 
 1711         if (lp == sc->sc_primary || lagg_failover_rx_all) {
 1712                 m->m_pkthdr.rcvif = ifp;
 1713                 return (m);
 1714         }
 1715 
 1716         if (!LAGG_PORTACTIVE(sc->sc_primary)) {
 1717                 tmp_tp = lagg_link_active(sc, sc->sc_primary);
 1718                 /*
 1719                  * If tmp_tp is null, we've recieved a packet when all
 1720                  * our links are down. Weird, but process it anyways.
 1721                  */
 1722                 if ((tmp_tp == NULL || tmp_tp == lp)) {
 1723                         m->m_pkthdr.rcvif = ifp;
 1724                         return (m);
 1725                 }
 1726         }
 1727 
 1728         m_freem(m);
 1729         return (NULL);
 1730 }
 1731 
 1732 /*
 1733  * Loadbalancing
 1734  */
 1735 
 1736 static int
 1737 lagg_lb_attach(struct lagg_softc *sc)
 1738 {
 1739         struct lagg_port *lp;
 1740         struct lagg_lb *lb;
 1741 
 1742         if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
 1743             M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
 1744                 return (ENOMEM);
 1745 
 1746         sc->sc_detach = lagg_lb_detach;
 1747         sc->sc_start = lagg_lb_start;
 1748         sc->sc_input = lagg_lb_input;
 1749         sc->sc_port_create = lagg_lb_port_create;
 1750         sc->sc_port_destroy = lagg_lb_port_destroy;
 1751         sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
 1752 
 1753         lb->lb_key = arc4random();
 1754         sc->sc_psc = (caddr_t)lb;
 1755 
 1756         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
 1757                 lagg_lb_port_create(lp);
 1758 
 1759         return (0);
 1760 }
 1761 
 1762 static int
 1763 lagg_lb_detach(struct lagg_softc *sc)
 1764 {
 1765         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
 1766         if (lb != NULL)
 1767                 free(lb, M_DEVBUF);
 1768         return (0);
 1769 }
 1770 
 1771 static int
 1772 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
 1773 {
 1774         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
 1775         struct lagg_port *lp_next;
 1776         int i = 0;
 1777 
 1778         bzero(&lb->lb_ports, sizeof(lb->lb_ports));
 1779         SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
 1780                 if (lp_next == lp)
 1781                         continue;
 1782                 if (i >= LAGG_MAX_PORTS)
 1783                         return (EINVAL);
 1784                 if (sc->sc_ifflags & IFF_DEBUG)
 1785                         printf("%s: port %s at index %d\n",
 1786                             sc->sc_ifname, lp_next->lp_ifname, i);
 1787                 lb->lb_ports[i++] = lp_next;
 1788         }
 1789 
 1790         return (0);
 1791 }
 1792 
 1793 static int
 1794 lagg_lb_port_create(struct lagg_port *lp)
 1795 {
 1796         struct lagg_softc *sc = lp->lp_softc;
 1797         return (lagg_lb_porttable(sc, NULL));
 1798 }
 1799 
 1800 static void
 1801 lagg_lb_port_destroy(struct lagg_port *lp)
 1802 {
 1803         struct lagg_softc *sc = lp->lp_softc;
 1804         lagg_lb_porttable(sc, lp);
 1805 }
 1806 
 1807 static int
 1808 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
 1809 {
 1810         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
 1811         struct lagg_port *lp = NULL;
 1812         uint32_t p = 0;
 1813 
 1814         if (sc->use_flowid && (m->m_flags & M_FLOWID))
 1815                 p = m->m_pkthdr.flowid;
 1816         else
 1817                 p = lagg_hashmbuf(sc, m, lb->lb_key);
 1818         p %= sc->sc_count;
 1819         lp = lb->lb_ports[p];
 1820 
 1821         /*
 1822          * Check the port's link state. This will return the next active
 1823          * port if the link is down or the port is NULL.
 1824          */
 1825         if ((lp = lagg_link_active(sc, lp)) == NULL) {
 1826                 m_freem(m);
 1827                 return (ENETDOWN);
 1828         }
 1829 
 1830         /* Send mbuf */
 1831         return (lagg_enqueue(lp->lp_ifp, m));
 1832 }
 1833 
 1834 static struct mbuf *
 1835 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
 1836 {
 1837         struct ifnet *ifp = sc->sc_ifp;
 1838 
 1839         /* Just pass in the packet to our lagg device */
 1840         m->m_pkthdr.rcvif = ifp;
 1841 
 1842         return (m);
 1843 }
 1844 
 1845 /*
 1846  * 802.3ad LACP
 1847  */
 1848 
 1849 static int
 1850 lagg_lacp_attach(struct lagg_softc *sc)
 1851 {
 1852         struct lagg_port *lp;
 1853         int error;
 1854 
 1855         sc->sc_detach = lagg_lacp_detach;
 1856         sc->sc_port_create = lacp_port_create;
 1857         sc->sc_port_destroy = lacp_port_destroy;
 1858         sc->sc_linkstate = lacp_linkstate;
 1859         sc->sc_start = lagg_lacp_start;
 1860         sc->sc_input = lagg_lacp_input;
 1861         sc->sc_init = lacp_init;
 1862         sc->sc_stop = lacp_stop;
 1863         sc->sc_lladdr = lagg_lacp_lladdr;
 1864         sc->sc_req = lacp_req;
 1865         sc->sc_portreq = lacp_portreq;
 1866 
 1867         error = lacp_attach(sc);
 1868         if (error)
 1869                 return (error);
 1870 
 1871         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
 1872                 lacp_port_create(lp);
 1873 
 1874         return (error);
 1875 }
 1876 
 1877 static int
 1878 lagg_lacp_detach(struct lagg_softc *sc)
 1879 {
 1880         struct lagg_port *lp;
 1881         int error;
 1882 
 1883         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
 1884                 lacp_port_destroy(lp);
 1885 
 1886         /* unlocking is safe here */
 1887         LAGG_WUNLOCK(sc);
 1888         error = lacp_detach(sc);
 1889         LAGG_WLOCK(sc);
 1890 
 1891         return (error);
 1892 }
 1893 
 1894 static void
 1895 lagg_lacp_lladdr(struct lagg_softc *sc)
 1896 {
 1897         struct lagg_port *lp;
 1898 
 1899         /* purge all the lacp ports */
 1900         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
 1901                 lacp_port_destroy(lp);
 1902 
 1903         /* add them back in */
 1904         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
 1905                 lacp_port_create(lp);
 1906 }
 1907 
 1908 static int
 1909 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
 1910 {
 1911         struct lagg_port *lp;
 1912 
 1913         lp = lacp_select_tx_port(sc, m);
 1914         if (lp == NULL) {
 1915                 m_freem(m);
 1916                 return (ENETDOWN);
 1917         }
 1918 
 1919         /* Send mbuf */
 1920         return (lagg_enqueue(lp->lp_ifp, m));
 1921 }
 1922 
 1923 static struct mbuf *
 1924 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
 1925 {
 1926         struct ifnet *ifp = sc->sc_ifp;
 1927         struct ether_header *eh;
 1928         u_short etype;
 1929 
 1930         eh = mtod(m, struct ether_header *);
 1931         etype = ntohs(eh->ether_type);
 1932 
 1933         /* Tap off LACP control messages */
 1934         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
 1935                 m = lacp_input(lp, m);
 1936                 if (m == NULL)
 1937                         return (NULL);
 1938         }
 1939 
 1940         /*
 1941          * If the port is not collecting or not in the active aggregator then
 1942          * free and return.
 1943          */
 1944         if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
 1945                 m_freem(m);
 1946                 return (NULL);
 1947         }
 1948 
 1949         m->m_pkthdr.rcvif = ifp;
 1950         return (m);
 1951 }

Cache object: 599878566994f596c147d8f631cd254b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.