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

Cache object: 1d3d09f4c690a59f89b67eab8d242ad8


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