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

Cache object: 7e7bfece7f230afbda6e797a102f54ef


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