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


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

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

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

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

Cache object: f31d0e7350d638fc698ada55c2563efa


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