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

Cache object: c0608c5645c81a1ea2ae97de56f49e18


[ 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.