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/ieee8023ad_lacp.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 /*      $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $     */
    2 
    3 /*-
    4  * Copyright (c)2005 YAMAMOTO Takashi,
    5  * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/11.2/sys/net/ieee8023ad_lacp.c 332834 2018-04-20 18:37:19Z jtl $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/callout.h>
   35 #include <sys/eventhandler.h>
   36 #include <sys/mbuf.h>
   37 #include <sys/systm.h>
   38 #include <sys/malloc.h>
   39 #include <sys/kernel.h> /* hz */
   40 #include <sys/socket.h> /* for net/if.h */
   41 #include <sys/sockio.h>
   42 #include <sys/sysctl.h>
   43 #include <machine/stdarg.h>
   44 #include <sys/lock.h>
   45 #include <sys/rwlock.h>
   46 #include <sys/taskqueue.h>
   47 
   48 #include <net/if.h>
   49 #include <net/if_var.h>
   50 #include <net/if_dl.h>
   51 #include <net/ethernet.h>
   52 #include <net/if_media.h>
   53 #include <net/if_types.h>
   54 
   55 #include <net/if_lagg.h>
   56 #include <net/ieee8023ad_lacp.h>
   57 
   58 /*
   59  * actor system priority and port priority.
   60  * XXX should be configurable.
   61  */
   62 
   63 #define LACP_SYSTEM_PRIO        0x8000
   64 #define LACP_PORT_PRIO          0x8000
   65 
   66 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
   67     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
   68 
   69 static const struct tlv_template lacp_info_tlv_template[] = {
   70         { LACP_TYPE_ACTORINFO,
   71             sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
   72         { LACP_TYPE_PARTNERINFO,
   73             sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
   74         { LACP_TYPE_COLLECTORINFO,
   75             sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
   76         { 0, 0 },
   77 };
   78 
   79 static const struct tlv_template marker_info_tlv_template[] = {
   80         { MARKER_TYPE_INFO,
   81             sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
   82         { 0, 0 },
   83 };
   84 
   85 static const struct tlv_template marker_response_tlv_template[] = {
   86         { MARKER_TYPE_RESPONSE,
   87             sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
   88         { 0, 0 },
   89 };
   90 
   91 typedef void (*lacp_timer_func_t)(struct lacp_port *);
   92 
   93 static void     lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
   94 static void     lacp_fill_markerinfo(struct lacp_port *,
   95                     struct lacp_markerinfo *);
   96 
   97 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
   98 static void     lacp_suppress_distributing(struct lacp_softc *,
   99                     struct lacp_aggregator *);
  100 static void     lacp_transit_expire(void *);
  101 static void     lacp_update_portmap(struct lacp_softc *);
  102 static void     lacp_select_active_aggregator(struct lacp_softc *);
  103 static uint16_t lacp_compose_key(struct lacp_port *);
  104 static int      tlv_check(const void *, size_t, const struct tlvhdr *,
  105                     const struct tlv_template *, boolean_t);
  106 static void     lacp_tick(void *);
  107 
  108 static void     lacp_fill_aggregator_id(struct lacp_aggregator *,
  109                     const struct lacp_port *);
  110 static void     lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
  111                     const struct lacp_peerinfo *);
  112 static int      lacp_aggregator_is_compatible(const struct lacp_aggregator *,
  113                     const struct lacp_port *);
  114 static int      lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
  115                     const struct lacp_peerinfo *);
  116 
  117 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
  118                     struct lacp_port *);
  119 static void     lacp_aggregator_addref(struct lacp_softc *,
  120                     struct lacp_aggregator *);
  121 static void     lacp_aggregator_delref(struct lacp_softc *,
  122                     struct lacp_aggregator *);
  123 
  124 /* receive machine */
  125 
  126 static int      lacp_pdu_input(struct lacp_port *, struct mbuf *);
  127 static int      lacp_marker_input(struct lacp_port *, struct mbuf *);
  128 static void     lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
  129 static void     lacp_sm_rx_timer(struct lacp_port *);
  130 static void     lacp_sm_rx_set_expired(struct lacp_port *);
  131 static void     lacp_sm_rx_update_ntt(struct lacp_port *,
  132                     const struct lacpdu *);
  133 static void     lacp_sm_rx_record_pdu(struct lacp_port *,
  134                     const struct lacpdu *);
  135 static void     lacp_sm_rx_update_selected(struct lacp_port *,
  136                     const struct lacpdu *);
  137 static void     lacp_sm_rx_record_default(struct lacp_port *);
  138 static void     lacp_sm_rx_update_default_selected(struct lacp_port *);
  139 static void     lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
  140                     const struct lacp_peerinfo *);
  141 
  142 /* mux machine */
  143 
  144 static void     lacp_sm_mux(struct lacp_port *);
  145 static void     lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
  146 static void     lacp_sm_mux_timer(struct lacp_port *);
  147 
  148 /* periodic transmit machine */
  149 
  150 static void     lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
  151 static void     lacp_sm_ptx_tx_schedule(struct lacp_port *);
  152 static void     lacp_sm_ptx_timer(struct lacp_port *);
  153 
  154 /* transmit machine */
  155 
  156 static void     lacp_sm_tx(struct lacp_port *);
  157 static void     lacp_sm_assert_ntt(struct lacp_port *);
  158 
  159 static void     lacp_run_timers(struct lacp_port *);
  160 static int      lacp_compare_peerinfo(const struct lacp_peerinfo *,
  161                     const struct lacp_peerinfo *);
  162 static int      lacp_compare_systemid(const struct lacp_systemid *,
  163                     const struct lacp_systemid *);
  164 static void     lacp_port_enable(struct lacp_port *);
  165 static void     lacp_port_disable(struct lacp_port *);
  166 static void     lacp_select(struct lacp_port *);
  167 static void     lacp_unselect(struct lacp_port *);
  168 static void     lacp_disable_collecting(struct lacp_port *);
  169 static void     lacp_enable_collecting(struct lacp_port *);
  170 static void     lacp_disable_distributing(struct lacp_port *);
  171 static void     lacp_enable_distributing(struct lacp_port *);
  172 static int      lacp_xmit_lacpdu(struct lacp_port *);
  173 static int      lacp_xmit_marker(struct lacp_port *);
  174 
  175 /* Debugging */
  176 
  177 static void     lacp_dump_lacpdu(const struct lacpdu *);
  178 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
  179                     size_t);
  180 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
  181                     const struct lacp_peerinfo *, char *, size_t);
  182 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
  183                     char *, size_t);
  184 static const char *lacp_format_state(uint8_t, char *, size_t);
  185 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
  186 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
  187                     size_t);
  188 static const char *lacp_format_portid(const struct lacp_portid *, char *,
  189                     size_t);
  190 static void     lacp_dprintf(const struct lacp_port *, const char *, ...)
  191                     __attribute__((__format__(__printf__, 2, 3)));
  192 
  193 static VNET_DEFINE(int, lacp_debug);
  194 #define V_lacp_debug    VNET(lacp_debug)
  195 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad");
  196 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
  197     &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
  198 
  199 static VNET_DEFINE(int, lacp_default_strict_mode) = 1;
  200 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
  201     CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
  202     "LACP strict protocol compliance default");
  203 
  204 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
  205 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
  206 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
  207 
  208 /*
  209  * partner administration variables.
  210  * XXX should be configurable.
  211  */
  212 
  213 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
  214         .lip_systemid = { .lsi_prio = 0xffff },
  215         .lip_portid = { .lpi_prio = 0xffff },
  216         .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
  217             LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
  218 };
  219 
  220 static const struct lacp_peerinfo lacp_partner_admin_strict = {
  221         .lip_systemid = { .lsi_prio = 0xffff },
  222         .lip_portid = { .lpi_prio = 0xffff },
  223         .lip_state = 0,
  224 };
  225 
  226 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
  227         [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
  228         [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
  229         [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
  230 };
  231 
  232 struct mbuf *
  233 lacp_input(struct lagg_port *lgp, struct mbuf *m)
  234 {
  235         struct lacp_port *lp = LACP_PORT(lgp);
  236         uint8_t subtype;
  237 
  238         if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
  239                 m_freem(m);
  240                 return (NULL);
  241         }
  242 
  243         m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
  244         switch (subtype) {
  245                 case SLOWPROTOCOLS_SUBTYPE_LACP:
  246                         lacp_pdu_input(lp, m);
  247                         return (NULL);
  248 
  249                 case SLOWPROTOCOLS_SUBTYPE_MARKER:
  250                         lacp_marker_input(lp, m);
  251                         return (NULL);
  252         }
  253 
  254         /* Not a subtype we are interested in */
  255         return (m);
  256 }
  257 
  258 /*
  259  * lacp_pdu_input: process lacpdu
  260  */
  261 static int
  262 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
  263 {
  264         struct lacp_softc *lsc = lp->lp_lsc;
  265         struct lacpdu *du;
  266         int error = 0;
  267 
  268         if (m->m_pkthdr.len != sizeof(*du)) {
  269                 goto bad;
  270         }
  271 
  272         if ((m->m_flags & M_MCAST) == 0) {
  273                 goto bad;
  274         }
  275 
  276         if (m->m_len < sizeof(*du)) {
  277                 m = m_pullup(m, sizeof(*du));
  278                 if (m == NULL) {
  279                         return (ENOMEM);
  280                 }
  281         }
  282 
  283         du = mtod(m, struct lacpdu *);
  284 
  285         if (memcmp(&du->ldu_eh.ether_dhost,
  286             &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
  287                 goto bad;
  288         }
  289 
  290         /*
  291          * ignore the version for compatibility with
  292          * the future protocol revisions.
  293          */
  294 #if 0
  295         if (du->ldu_sph.sph_version != 1) {
  296                 goto bad;
  297         }
  298 #endif
  299 
  300         /*
  301          * ignore tlv types for compatibility with
  302          * the future protocol revisions.
  303          */
  304         if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
  305             lacp_info_tlv_template, FALSE)) {
  306                 goto bad;
  307         }
  308 
  309         if (V_lacp_debug > 0) {
  310                 lacp_dprintf(lp, "lacpdu receive\n");
  311                 lacp_dump_lacpdu(du);
  312         }
  313 
  314         if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
  315                 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
  316                 goto bad;
  317         }
  318 
  319         LACP_LOCK(lsc);
  320         lacp_sm_rx(lp, du);
  321         LACP_UNLOCK(lsc);
  322 
  323         m_freem(m);
  324         return (error);
  325 
  326 bad:
  327         m_freem(m);
  328         return (EINVAL);
  329 }
  330 
  331 static void
  332 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
  333 {
  334         struct lagg_port *lgp = lp->lp_lagg;
  335         struct lagg_softc *sc = lgp->lp_softc;
  336 
  337         info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
  338         memcpy(&info->lip_systemid.lsi_mac,
  339             IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
  340         info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
  341         info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
  342         info->lip_state = lp->lp_state;
  343 }
  344 
  345 static void
  346 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
  347 {
  348         struct ifnet *ifp = lp->lp_ifp;
  349 
  350         /* Fill in the port index and system id (encoded as the MAC) */
  351         info->mi_rq_port = htons(ifp->if_index);
  352         memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
  353         info->mi_rq_xid = htonl(0);
  354 }
  355 
  356 static int
  357 lacp_xmit_lacpdu(struct lacp_port *lp)
  358 {
  359         struct lagg_port *lgp = lp->lp_lagg;
  360         struct mbuf *m;
  361         struct lacpdu *du;
  362         int error;
  363 
  364         LACP_LOCK_ASSERT(lp->lp_lsc);
  365 
  366         m = m_gethdr(M_NOWAIT, MT_DATA);
  367         if (m == NULL) {
  368                 return (ENOMEM);
  369         }
  370         m->m_len = m->m_pkthdr.len = sizeof(*du);
  371 
  372         du = mtod(m, struct lacpdu *);
  373         memset(du, 0, sizeof(*du));
  374 
  375         memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
  376             ETHER_ADDR_LEN);
  377         memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
  378         du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
  379 
  380         du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
  381         du->ldu_sph.sph_version = 1;
  382 
  383         TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
  384         du->ldu_actor = lp->lp_actor;
  385 
  386         TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
  387             sizeof(du->ldu_partner));
  388         du->ldu_partner = lp->lp_partner;
  389 
  390         TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
  391             sizeof(du->ldu_collector));
  392         du->ldu_collector.lci_maxdelay = 0;
  393 
  394         if (V_lacp_debug > 0) {
  395                 lacp_dprintf(lp, "lacpdu transmit\n");
  396                 lacp_dump_lacpdu(du);
  397         }
  398 
  399         m->m_flags |= M_MCAST;
  400 
  401         /*
  402          * XXX should use higher priority queue.
  403          * otherwise network congestion can break aggregation.
  404          */
  405 
  406         error = lagg_enqueue(lp->lp_ifp, m);
  407         return (error);
  408 }
  409 
  410 static int
  411 lacp_xmit_marker(struct lacp_port *lp)
  412 {
  413         struct lagg_port *lgp = lp->lp_lagg;
  414         struct mbuf *m;
  415         struct markerdu *mdu;
  416         int error;
  417 
  418         LACP_LOCK_ASSERT(lp->lp_lsc);
  419 
  420         m = m_gethdr(M_NOWAIT, MT_DATA);
  421         if (m == NULL) {
  422                 return (ENOMEM);
  423         }
  424         m->m_len = m->m_pkthdr.len = sizeof(*mdu);
  425 
  426         mdu = mtod(m, struct markerdu *);
  427         memset(mdu, 0, sizeof(*mdu));
  428 
  429         memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
  430             ETHER_ADDR_LEN);
  431         memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
  432         mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
  433 
  434         mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
  435         mdu->mdu_sph.sph_version = 1;
  436 
  437         /* Bump the transaction id and copy over the marker info */
  438         lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
  439         TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
  440         mdu->mdu_info = lp->lp_marker;
  441 
  442         LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
  443             ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
  444             ntohl(mdu->mdu_info.mi_rq_xid)));
  445 
  446         m->m_flags |= M_MCAST;
  447         error = lagg_enqueue(lp->lp_ifp, m);
  448         return (error);
  449 }
  450 
  451 void
  452 lacp_linkstate(struct lagg_port *lgp)
  453 {
  454         struct lacp_port *lp = LACP_PORT(lgp);
  455         struct lacp_softc *lsc = lp->lp_lsc;
  456         struct ifnet *ifp = lgp->lp_ifp;
  457         struct ifmediareq ifmr;
  458         int error = 0;
  459         u_int media;
  460         uint8_t old_state;
  461         uint16_t old_key;
  462 
  463         bzero((char *)&ifmr, sizeof(ifmr));
  464         error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
  465         if (error != 0) {
  466                 bzero((char *)&ifmr, sizeof(ifmr));
  467                 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
  468         }
  469         if (error != 0)
  470                 return;
  471 
  472         LACP_LOCK(lsc);
  473         media = ifmr.ifm_active;
  474         LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
  475             "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
  476             (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
  477         old_state = lp->lp_state;
  478         old_key = lp->lp_key;
  479 
  480         lp->lp_media = media;
  481         /*
  482          * If the port is not an active full duplex Ethernet link then it can
  483          * not be aggregated.
  484          */
  485         if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
  486             ifp->if_link_state != LINK_STATE_UP) {
  487                 lacp_port_disable(lp);
  488         } else {
  489                 lacp_port_enable(lp);
  490         }
  491         lp->lp_key = lacp_compose_key(lp);
  492 
  493         if (old_state != lp->lp_state || old_key != lp->lp_key) {
  494                 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
  495                 lp->lp_selected = LACP_UNSELECTED;
  496         }
  497         LACP_UNLOCK(lsc);
  498 }
  499 
  500 static void
  501 lacp_tick(void *arg)
  502 {
  503         struct lacp_softc *lsc = arg;
  504         struct lacp_port *lp;
  505 
  506         LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
  507                 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
  508                         continue;
  509 
  510                 CURVNET_SET(lp->lp_ifp->if_vnet);
  511                 lacp_run_timers(lp);
  512 
  513                 lacp_select(lp);
  514                 lacp_sm_mux(lp);
  515                 lacp_sm_tx(lp);
  516                 lacp_sm_ptx_tx_schedule(lp);
  517                 CURVNET_RESTORE();
  518         }
  519         callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
  520 }
  521 
  522 int
  523 lacp_port_create(struct lagg_port *lgp)
  524 {
  525         struct lagg_softc *sc = lgp->lp_softc;
  526         struct lacp_softc *lsc = LACP_SOFTC(sc);
  527         struct lacp_port *lp;
  528         struct ifnet *ifp = lgp->lp_ifp;
  529         struct sockaddr_dl sdl;
  530         struct ifmultiaddr *rifma = NULL;
  531         int error;
  532 
  533         link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
  534         sdl.sdl_alen = ETHER_ADDR_LEN;
  535 
  536         bcopy(&ethermulticastaddr_slowprotocols,
  537             LLADDR(&sdl), ETHER_ADDR_LEN);
  538         error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
  539         if (error) {
  540                 printf("%s: ADDMULTI failed on %s\n", __func__,
  541                     lgp->lp_ifp->if_xname);
  542                 return (error);
  543         }
  544 
  545         lp = malloc(sizeof(struct lacp_port),
  546             M_DEVBUF, M_NOWAIT|M_ZERO);
  547         if (lp == NULL)
  548                 return (ENOMEM);
  549 
  550         LACP_LOCK(lsc);
  551         lgp->lp_psc = lp;
  552         lp->lp_ifp = ifp;
  553         lp->lp_lagg = lgp;
  554         lp->lp_lsc = lsc;
  555         lp->lp_ifma = rifma;
  556 
  557         LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
  558 
  559         lacp_fill_actorinfo(lp, &lp->lp_actor);
  560         lacp_fill_markerinfo(lp, &lp->lp_marker);
  561         lp->lp_state = LACP_STATE_ACTIVITY;
  562         lp->lp_aggregator = NULL;
  563         lacp_sm_rx_set_expired(lp);
  564         LACP_UNLOCK(lsc);
  565         lacp_linkstate(lgp);
  566 
  567         return (0);
  568 }
  569 
  570 void
  571 lacp_port_destroy(struct lagg_port *lgp)
  572 {
  573         struct lacp_port *lp = LACP_PORT(lgp);
  574         struct lacp_softc *lsc = lp->lp_lsc;
  575         int i;
  576 
  577         LACP_LOCK(lsc);
  578         for (i = 0; i < LACP_NTIMER; i++) {
  579                 LACP_TIMER_DISARM(lp, i);
  580         }
  581 
  582         lacp_disable_collecting(lp);
  583         lacp_disable_distributing(lp);
  584         lacp_unselect(lp);
  585 
  586         LIST_REMOVE(lp, lp_next);
  587         LACP_UNLOCK(lsc);
  588 
  589         /* The address may have already been removed by if_purgemaddrs() */
  590         if (!lgp->lp_detaching)
  591                 if_delmulti_ifma(lp->lp_ifma);
  592 
  593         free(lp, M_DEVBUF);
  594 }
  595 
  596 void
  597 lacp_req(struct lagg_softc *sc, void *data)
  598 {
  599         struct lacp_opreq *req = (struct lacp_opreq *)data;
  600         struct lacp_softc *lsc = LACP_SOFTC(sc);
  601         struct lacp_aggregator *la;
  602 
  603         bzero(req, sizeof(struct lacp_opreq));
  604         
  605         /*
  606          * If the LACP softc is NULL, return with the opreq structure full of
  607          * zeros.  It is normal for the softc to be NULL while the lagg is
  608          * being destroyed.
  609          */
  610         if (NULL == lsc)
  611                 return;
  612 
  613         la = lsc->lsc_active_aggregator;
  614         LACP_LOCK(lsc);
  615         if (la != NULL) {
  616                 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
  617                 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
  618                     ETHER_ADDR_LEN);
  619                 req->actor_key = ntohs(la->la_actor.lip_key);
  620                 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
  621                 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
  622                 req->actor_state = la->la_actor.lip_state;
  623 
  624                 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
  625                 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
  626                     ETHER_ADDR_LEN);
  627                 req->partner_key = ntohs(la->la_partner.lip_key);
  628                 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
  629                 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
  630                 req->partner_state = la->la_partner.lip_state;
  631         }
  632         LACP_UNLOCK(lsc);
  633 }
  634 
  635 void
  636 lacp_portreq(struct lagg_port *lgp, void *data)
  637 {
  638         struct lacp_opreq *req = (struct lacp_opreq *)data;
  639         struct lacp_port *lp = LACP_PORT(lgp);
  640         struct lacp_softc *lsc = lp->lp_lsc;
  641 
  642         LACP_LOCK(lsc);
  643         req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
  644         memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
  645             ETHER_ADDR_LEN);
  646         req->actor_key = ntohs(lp->lp_actor.lip_key);
  647         req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
  648         req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
  649         req->actor_state = lp->lp_actor.lip_state;
  650 
  651         req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
  652         memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
  653             ETHER_ADDR_LEN);
  654         req->partner_key = ntohs(lp->lp_partner.lip_key);
  655         req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
  656         req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
  657         req->partner_state = lp->lp_partner.lip_state;
  658         LACP_UNLOCK(lsc);
  659 }
  660 
  661 static void
  662 lacp_disable_collecting(struct lacp_port *lp)
  663 {
  664         LACP_DPRINTF((lp, "collecting disabled\n"));
  665         lp->lp_state &= ~LACP_STATE_COLLECTING;
  666 }
  667 
  668 static void
  669 lacp_enable_collecting(struct lacp_port *lp)
  670 {
  671         LACP_DPRINTF((lp, "collecting enabled\n"));
  672         lp->lp_state |= LACP_STATE_COLLECTING;
  673 }
  674 
  675 static void
  676 lacp_disable_distributing(struct lacp_port *lp)
  677 {
  678         struct lacp_aggregator *la = lp->lp_aggregator;
  679         struct lacp_softc *lsc = lp->lp_lsc;
  680         struct lagg_softc *sc = lsc->lsc_softc;
  681         char buf[LACP_LAGIDSTR_MAX+1];
  682 
  683         LACP_LOCK_ASSERT(lsc);
  684 
  685         if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
  686                 return;
  687         }
  688 
  689         KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
  690         KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
  691         KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
  692 
  693         LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
  694             "nports %d -> %d\n",
  695             lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
  696             la->la_nports, la->la_nports - 1));
  697 
  698         TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
  699         la->la_nports--;
  700         sc->sc_active = la->la_nports;
  701 
  702         if (lsc->lsc_active_aggregator == la) {
  703                 lacp_suppress_distributing(lsc, la);
  704                 lacp_select_active_aggregator(lsc);
  705                 /* regenerate the port map, the active aggregator has changed */
  706                 lacp_update_portmap(lsc);
  707         }
  708 
  709         lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
  710 }
  711 
  712 static void
  713 lacp_enable_distributing(struct lacp_port *lp)
  714 {
  715         struct lacp_aggregator *la = lp->lp_aggregator;
  716         struct lacp_softc *lsc = lp->lp_lsc;
  717         struct lagg_softc *sc = lsc->lsc_softc;
  718         char buf[LACP_LAGIDSTR_MAX+1];
  719 
  720         LACP_LOCK_ASSERT(lsc);
  721 
  722         if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
  723                 return;
  724         }
  725 
  726         LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
  727             "nports %d -> %d\n",
  728             lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
  729             la->la_nports, la->la_nports + 1));
  730 
  731         KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
  732         TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
  733         la->la_nports++;
  734         sc->sc_active = la->la_nports;
  735 
  736         lp->lp_state |= LACP_STATE_DISTRIBUTING;
  737 
  738         if (lsc->lsc_active_aggregator == la) {
  739                 lacp_suppress_distributing(lsc, la);
  740                 lacp_update_portmap(lsc);
  741         } else
  742                 /* try to become the active aggregator */
  743                 lacp_select_active_aggregator(lsc);
  744 }
  745 
  746 static void
  747 lacp_transit_expire(void *vp)
  748 {
  749         struct lacp_softc *lsc = vp;
  750 
  751         LACP_LOCK_ASSERT(lsc);
  752 
  753         CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
  754         LACP_TRACE(NULL);
  755         CURVNET_RESTORE();
  756 
  757         lsc->lsc_suppress_distributing = FALSE;
  758 }
  759 
  760 void
  761 lacp_attach(struct lagg_softc *sc)
  762 {
  763         struct lacp_softc *lsc;
  764 
  765         lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
  766 
  767         sc->sc_psc = lsc;
  768         lsc->lsc_softc = sc;
  769 
  770         lsc->lsc_hashkey = m_ether_tcpip_hash_init();
  771         lsc->lsc_active_aggregator = NULL;
  772         lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
  773         LACP_LOCK_INIT(lsc);
  774         TAILQ_INIT(&lsc->lsc_aggregators);
  775         LIST_INIT(&lsc->lsc_ports);
  776 
  777         callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
  778         callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
  779 
  780         /* if the lagg is already up then do the same */
  781         if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
  782                 lacp_init(sc);
  783 }
  784 
  785 void
  786 lacp_detach(void *psc)
  787 {
  788         struct lacp_softc *lsc = (struct lacp_softc *)psc;
  789 
  790         KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
  791             ("aggregators still active"));
  792         KASSERT(lsc->lsc_active_aggregator == NULL,
  793             ("aggregator still attached"));
  794 
  795         callout_drain(&lsc->lsc_transit_callout);
  796         callout_drain(&lsc->lsc_callout);
  797 
  798         LACP_LOCK_DESTROY(lsc);
  799         free(lsc, M_DEVBUF);
  800 }
  801 
  802 void
  803 lacp_init(struct lagg_softc *sc)
  804 {
  805         struct lacp_softc *lsc = LACP_SOFTC(sc);
  806 
  807         LACP_LOCK(lsc);
  808         callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
  809         LACP_UNLOCK(lsc);
  810 }
  811 
  812 void
  813 lacp_stop(struct lagg_softc *sc)
  814 {
  815         struct lacp_softc *lsc = LACP_SOFTC(sc);
  816 
  817         LACP_LOCK(lsc);
  818         callout_stop(&lsc->lsc_transit_callout);
  819         callout_stop(&lsc->lsc_callout);
  820         LACP_UNLOCK(lsc);
  821 }
  822 
  823 struct lagg_port *
  824 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
  825 {
  826         struct lacp_softc *lsc = LACP_SOFTC(sc);
  827         struct lacp_portmap *pm;
  828         struct lacp_port *lp;
  829         uint32_t hash;
  830 
  831         if (__predict_false(lsc->lsc_suppress_distributing)) {
  832                 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
  833                 return (NULL);
  834         }
  835 
  836         pm = &lsc->lsc_pmap[lsc->lsc_activemap];
  837         if (pm->pm_count == 0) {
  838                 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
  839                 return (NULL);
  840         }
  841 
  842         if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
  843             M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
  844                 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
  845         else
  846                 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
  847         hash %= pm->pm_count;
  848         lp = pm->pm_map[hash];
  849 
  850         KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
  851             ("aggregated port is not distributing"));
  852 
  853         return (lp->lp_lagg);
  854 }
  855 /*
  856  * lacp_suppress_distributing: drop transmit packets for a while
  857  * to preserve packet ordering.
  858  */
  859 
  860 static void
  861 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
  862 {
  863         struct lacp_port *lp;
  864 
  865         if (lsc->lsc_active_aggregator != la) {
  866                 return;
  867         }
  868 
  869         LACP_TRACE(NULL);
  870 
  871         lsc->lsc_suppress_distributing = TRUE;
  872 
  873         /* send a marker frame down each port to verify the queues are empty */
  874         LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
  875                 lp->lp_flags |= LACP_PORT_MARK;
  876                 lacp_xmit_marker(lp);
  877         }
  878 
  879         /* set a timeout for the marker frames */
  880         callout_reset(&lsc->lsc_transit_callout,
  881             LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
  882 }
  883 
  884 static int
  885 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
  886     const struct lacp_peerinfo *b)
  887 {
  888         return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
  889 }
  890 
  891 static int
  892 lacp_compare_systemid(const struct lacp_systemid *a,
  893     const struct lacp_systemid *b)
  894 {
  895         return (memcmp(a, b, sizeof(*a)));
  896 }
  897 
  898 #if 0   /* unused */
  899 static int
  900 lacp_compare_portid(const struct lacp_portid *a,
  901     const struct lacp_portid *b)
  902 {
  903         return (memcmp(a, b, sizeof(*a)));
  904 }
  905 #endif
  906 
  907 static uint64_t
  908 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
  909 {
  910         struct lacp_port *lp;
  911         uint64_t speed;
  912 
  913         lp = TAILQ_FIRST(&la->la_ports);
  914         if (lp == NULL) {
  915                 return (0);
  916         }
  917 
  918         speed = ifmedia_baudrate(lp->lp_media);
  919         speed *= la->la_nports;
  920         if (speed == 0) {
  921                 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
  922                     lp->lp_media, la->la_nports));
  923         }
  924 
  925         return (speed);
  926 }
  927 
  928 /*
  929  * lacp_select_active_aggregator: select an aggregator to be used to transmit
  930  * packets from lagg(4) interface.
  931  */
  932 
  933 static void
  934 lacp_select_active_aggregator(struct lacp_softc *lsc)
  935 {
  936         struct lacp_aggregator *la;
  937         struct lacp_aggregator *best_la = NULL;
  938         uint64_t best_speed = 0;
  939         char buf[LACP_LAGIDSTR_MAX+1];
  940 
  941         LACP_TRACE(NULL);
  942 
  943         TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
  944                 uint64_t speed;
  945 
  946                 if (la->la_nports == 0) {
  947                         continue;
  948                 }
  949 
  950                 speed = lacp_aggregator_bandwidth(la);
  951                 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
  952                     lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
  953                     speed, la->la_nports));
  954 
  955                 /*
  956                  * This aggregator is chosen if the partner has a better
  957                  * system priority or, the total aggregated speed is higher
  958                  * or, it is already the chosen aggregator
  959                  */
  960                 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
  961                     LACP_SYS_PRI(best_la->la_partner)) ||
  962                     speed > best_speed ||
  963                     (speed == best_speed &&
  964                     la == lsc->lsc_active_aggregator)) {
  965                         best_la = la;
  966                         best_speed = speed;
  967                 }
  968         }
  969 
  970         KASSERT(best_la == NULL || best_la->la_nports > 0,
  971             ("invalid aggregator refcnt"));
  972         KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
  973             ("invalid aggregator list"));
  974 
  975         if (lsc->lsc_active_aggregator != best_la) {
  976                 LACP_DPRINTF((NULL, "active aggregator changed\n"));
  977                 LACP_DPRINTF((NULL, "old %s\n",
  978                     lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
  979                     buf, sizeof(buf))));
  980         } else {
  981                 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
  982         }
  983         LACP_DPRINTF((NULL, "new %s\n",
  984             lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
  985 
  986         if (lsc->lsc_active_aggregator != best_la) {
  987                 lsc->lsc_active_aggregator = best_la;
  988                 lacp_update_portmap(lsc);
  989                 if (best_la) {
  990                         lacp_suppress_distributing(lsc, best_la);
  991                 }
  992         }
  993 }
  994 
  995 /*
  996  * Updated the inactive portmap array with the new list of ports and
  997  * make it live.
  998  */
  999 static void
 1000 lacp_update_portmap(struct lacp_softc *lsc)
 1001 {
 1002         struct lagg_softc *sc = lsc->lsc_softc;
 1003         struct lacp_aggregator *la;
 1004         struct lacp_portmap *p;
 1005         struct lacp_port *lp;
 1006         uint64_t speed;
 1007         u_int newmap;
 1008         int i;
 1009 
 1010         newmap = lsc->lsc_activemap == 0 ? 1 : 0;
 1011         p = &lsc->lsc_pmap[newmap];
 1012         la = lsc->lsc_active_aggregator;
 1013         speed = 0;
 1014         bzero(p, sizeof(struct lacp_portmap));
 1015 
 1016         if (la != NULL && la->la_nports > 0) {
 1017                 p->pm_count = la->la_nports;
 1018                 i = 0;
 1019                 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
 1020                         p->pm_map[i++] = lp;
 1021                 KASSERT(i == p->pm_count, ("Invalid port count"));
 1022                 speed = lacp_aggregator_bandwidth(la);
 1023         }
 1024         sc->sc_ifp->if_baudrate = speed;
 1025 
 1026         /* switch the active portmap over */
 1027         atomic_store_rel_int(&lsc->lsc_activemap, newmap);
 1028         LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
 1029                     lsc->lsc_activemap,
 1030                     lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
 1031 }
 1032 
 1033 static uint16_t
 1034 lacp_compose_key(struct lacp_port *lp)
 1035 {
 1036         struct lagg_port *lgp = lp->lp_lagg;
 1037         struct lagg_softc *sc = lgp->lp_softc;
 1038         u_int media = lp->lp_media;
 1039         uint16_t key;
 1040 
 1041         if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
 1042 
 1043                 /*
 1044                  * non-aggregatable links should have unique keys.
 1045                  *
 1046                  * XXX this isn't really unique as if_index is 16 bit.
 1047                  */
 1048 
 1049                 /* bit 0..14:   (some bits of) if_index of this port */
 1050                 key = lp->lp_ifp->if_index;
 1051                 /* bit 15:      1 */
 1052                 key |= 0x8000;
 1053         } else {
 1054                 u_int subtype = IFM_SUBTYPE(media);
 1055 
 1056                 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
 1057                 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
 1058 
 1059                 /* bit 0..4:    IFM_SUBTYPE modulo speed */
 1060                 switch (subtype) {
 1061                 case IFM_10_T:
 1062                 case IFM_10_2:
 1063                 case IFM_10_5:
 1064                 case IFM_10_STP:
 1065                 case IFM_10_FL:
 1066                         key = IFM_10_T;
 1067                         break;
 1068                 case IFM_100_TX:
 1069                 case IFM_100_FX:
 1070                 case IFM_100_T4:
 1071                 case IFM_100_VG:
 1072                 case IFM_100_T2:
 1073                 case IFM_100_T:
 1074                         key = IFM_100_TX;
 1075                         break;
 1076                 case IFM_1000_SX:
 1077                 case IFM_1000_LX:
 1078                 case IFM_1000_CX:
 1079                 case IFM_1000_T:
 1080                 case IFM_1000_KX:
 1081                 case IFM_1000_SGMII:
 1082                 case IFM_1000_CX_SGMII:
 1083                         key = IFM_1000_SX;
 1084                         break;
 1085                 case IFM_10G_LR:
 1086                 case IFM_10G_SR:
 1087                 case IFM_10G_CX4:
 1088                 case IFM_10G_TWINAX:
 1089                 case IFM_10G_TWINAX_LONG:
 1090                 case IFM_10G_LRM:
 1091                 case IFM_10G_T:
 1092                 case IFM_10G_KX4:
 1093                 case IFM_10G_KR:
 1094                 case IFM_10G_CR1:
 1095                 case IFM_10G_ER:
 1096                 case IFM_10G_SFI:
 1097                 case IFM_10G_AOC:
 1098                         key = IFM_10G_LR;
 1099                         break;
 1100                 case IFM_20G_KR2:
 1101                         key = IFM_20G_KR2;
 1102                         break;
 1103                 case IFM_2500_KX:
 1104                 case IFM_2500_T:
 1105                         key = IFM_2500_KX;
 1106                         break;
 1107                 case IFM_5000_T:
 1108                         key = IFM_5000_T;
 1109                         break;
 1110                 case IFM_50G_PCIE:
 1111                 case IFM_50G_CR2:
 1112                 case IFM_50G_KR2:
 1113                         key = IFM_50G_PCIE;
 1114                         break;
 1115                 case IFM_56G_R4:
 1116                         key = IFM_56G_R4;
 1117                         break;
 1118                 case IFM_25G_PCIE:
 1119                 case IFM_25G_CR:
 1120                 case IFM_25G_KR:
 1121                 case IFM_25G_SR:
 1122                 case IFM_25G_LR:
 1123                 case IFM_25G_ACC:
 1124                 case IFM_25G_AOC:
 1125                         key = IFM_25G_PCIE;
 1126                         break;
 1127                 case IFM_40G_CR4:
 1128                 case IFM_40G_SR4:
 1129                 case IFM_40G_LR4:
 1130                 case IFM_40G_XLPPI:
 1131                 case IFM_40G_KR4:
 1132                         key = IFM_40G_CR4;
 1133                         break;
 1134                 case IFM_100G_CR4:
 1135                 case IFM_100G_SR4:
 1136                 case IFM_100G_KR4:
 1137                 case IFM_100G_LR4:
 1138                         key = IFM_100G_CR4;
 1139                         break;
 1140                 default:
 1141                         key = subtype;
 1142                         break;
 1143                 }
 1144                 /* bit 5..14:   (some bits of) if_index of lagg device */
 1145                 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
 1146                 /* bit 15:      0 */
 1147         }
 1148         return (htons(key));
 1149 }
 1150 
 1151 static void
 1152 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
 1153 {
 1154         char buf[LACP_LAGIDSTR_MAX+1];
 1155 
 1156         LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
 1157             __func__,
 1158             lacp_format_lagid(&la->la_actor, &la->la_partner,
 1159             buf, sizeof(buf)),
 1160             la->la_refcnt, la->la_refcnt + 1));
 1161 
 1162         KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
 1163         la->la_refcnt++;
 1164         KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
 1165 }
 1166 
 1167 static void
 1168 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
 1169 {
 1170         char buf[LACP_LAGIDSTR_MAX+1];
 1171 
 1172         LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
 1173             __func__,
 1174             lacp_format_lagid(&la->la_actor, &la->la_partner,
 1175             buf, sizeof(buf)),
 1176             la->la_refcnt, la->la_refcnt - 1));
 1177 
 1178         KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
 1179         la->la_refcnt--;
 1180         if (la->la_refcnt > 0) {
 1181                 return;
 1182         }
 1183 
 1184         KASSERT(la->la_refcnt == 0, ("refcount not zero"));
 1185         KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
 1186 
 1187         TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
 1188 
 1189         free(la, M_DEVBUF);
 1190 }
 1191 
 1192 /*
 1193  * lacp_aggregator_get: allocate an aggregator.
 1194  */
 1195 
 1196 static struct lacp_aggregator *
 1197 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
 1198 {
 1199         struct lacp_aggregator *la;
 1200 
 1201         la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
 1202         if (la) {
 1203                 la->la_refcnt = 1;
 1204                 la->la_nports = 0;
 1205                 TAILQ_INIT(&la->la_ports);
 1206                 la->la_pending = 0;
 1207                 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
 1208         }
 1209 
 1210         return (la);
 1211 }
 1212 
 1213 /*
 1214  * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
 1215  */
 1216 
 1217 static void
 1218 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
 1219 {
 1220         lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
 1221         lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
 1222 
 1223         la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
 1224 }
 1225 
 1226 static void
 1227 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
 1228     const struct lacp_peerinfo *lpi_port)
 1229 {
 1230         memset(lpi_aggr, 0, sizeof(*lpi_aggr));
 1231         lpi_aggr->lip_systemid = lpi_port->lip_systemid;
 1232         lpi_aggr->lip_key = lpi_port->lip_key;
 1233 }
 1234 
 1235 /*
 1236  * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
 1237  */
 1238 
 1239 static int
 1240 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
 1241     const struct lacp_port *lp)
 1242 {
 1243         if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
 1244             !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
 1245                 return (0);
 1246         }
 1247 
 1248         if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
 1249                 return (0);
 1250         }
 1251 
 1252         if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
 1253                 return (0);
 1254         }
 1255 
 1256         if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
 1257                 return (0);
 1258         }
 1259 
 1260         return (1);
 1261 }
 1262 
 1263 static int
 1264 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
 1265     const struct lacp_peerinfo *b)
 1266 {
 1267         if (memcmp(&a->lip_systemid, &b->lip_systemid,
 1268             sizeof(a->lip_systemid))) {
 1269                 return (0);
 1270         }
 1271 
 1272         if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
 1273                 return (0);
 1274         }
 1275 
 1276         return (1);
 1277 }
 1278 
 1279 static void
 1280 lacp_port_enable(struct lacp_port *lp)
 1281 {
 1282         lp->lp_state |= LACP_STATE_AGGREGATION;
 1283 }
 1284 
 1285 static void
 1286 lacp_port_disable(struct lacp_port *lp)
 1287 {
 1288         lacp_set_mux(lp, LACP_MUX_DETACHED);
 1289 
 1290         lp->lp_state &= ~LACP_STATE_AGGREGATION;
 1291         lp->lp_selected = LACP_UNSELECTED;
 1292         lacp_sm_rx_record_default(lp);
 1293         lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
 1294         lp->lp_state &= ~LACP_STATE_EXPIRED;
 1295 }
 1296 
 1297 /*
 1298  * lacp_select: select an aggregator.  create one if necessary.
 1299  */
 1300 static void
 1301 lacp_select(struct lacp_port *lp)
 1302 {
 1303         struct lacp_softc *lsc = lp->lp_lsc;
 1304         struct lacp_aggregator *la;
 1305         char buf[LACP_LAGIDSTR_MAX+1];
 1306 
 1307         if (lp->lp_aggregator) {
 1308                 return;
 1309         }
 1310 
 1311         /* If we haven't heard from our peer, skip this step. */
 1312         if (lp->lp_state & LACP_STATE_DEFAULTED)
 1313                 return;
 1314 
 1315         KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
 1316             ("timer_wait_while still active"));
 1317 
 1318         LACP_DPRINTF((lp, "port lagid=%s\n",
 1319             lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
 1320             buf, sizeof(buf))));
 1321 
 1322         TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
 1323                 if (lacp_aggregator_is_compatible(la, lp)) {
 1324                         break;
 1325                 }
 1326         }
 1327 
 1328         if (la == NULL) {
 1329                 la = lacp_aggregator_get(lsc, lp);
 1330                 if (la == NULL) {
 1331                         LACP_DPRINTF((lp, "aggregator creation failed\n"));
 1332 
 1333                         /*
 1334                          * will retry on the next tick.
 1335                          */
 1336 
 1337                         return;
 1338                 }
 1339                 lacp_fill_aggregator_id(la, lp);
 1340                 LACP_DPRINTF((lp, "aggregator created\n"));
 1341         } else {
 1342                 LACP_DPRINTF((lp, "compatible aggregator found\n"));
 1343                 if (la->la_refcnt == LACP_MAX_PORTS)
 1344                         return;
 1345                 lacp_aggregator_addref(lsc, la);
 1346         }
 1347 
 1348         LACP_DPRINTF((lp, "aggregator lagid=%s\n",
 1349             lacp_format_lagid(&la->la_actor, &la->la_partner,
 1350             buf, sizeof(buf))));
 1351 
 1352         lp->lp_aggregator = la;
 1353         lp->lp_selected = LACP_SELECTED;
 1354 }
 1355 
 1356 /*
 1357  * lacp_unselect: finish unselect/detach process.
 1358  */
 1359 
 1360 static void
 1361 lacp_unselect(struct lacp_port *lp)
 1362 {
 1363         struct lacp_softc *lsc = lp->lp_lsc;
 1364         struct lacp_aggregator *la = lp->lp_aggregator;
 1365 
 1366         KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
 1367             ("timer_wait_while still active"));
 1368 
 1369         if (la == NULL) {
 1370                 return;
 1371         }
 1372 
 1373         lp->lp_aggregator = NULL;
 1374         lacp_aggregator_delref(lsc, la);
 1375 }
 1376 
 1377 /* mux machine */
 1378 
 1379 static void
 1380 lacp_sm_mux(struct lacp_port *lp)
 1381 {
 1382         struct lagg_port *lgp = lp->lp_lagg;
 1383         struct lagg_softc *sc = lgp->lp_softc;
 1384         enum lacp_mux_state new_state;
 1385         boolean_t p_sync =
 1386                     (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
 1387         boolean_t p_collecting =
 1388             (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
 1389         enum lacp_selected selected = lp->lp_selected;
 1390         struct lacp_aggregator *la;
 1391 
 1392         if (V_lacp_debug > 1)
 1393                 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
 1394                     "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
 1395                     lp->lp_mux_state, selected, p_sync, p_collecting);
 1396 
 1397 re_eval:
 1398         la = lp->lp_aggregator;
 1399         KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
 1400             ("MUX not detached"));
 1401         new_state = lp->lp_mux_state;
 1402         switch (lp->lp_mux_state) {
 1403         case LACP_MUX_DETACHED:
 1404                 if (selected != LACP_UNSELECTED) {
 1405                         new_state = LACP_MUX_WAITING;
 1406                 }
 1407                 break;
 1408         case LACP_MUX_WAITING:
 1409                 KASSERT(la->la_pending > 0 ||
 1410                     !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
 1411                     ("timer_wait_while still active"));
 1412                 if (selected == LACP_SELECTED && la->la_pending == 0) {
 1413                         new_state = LACP_MUX_ATTACHED;
 1414                 } else if (selected == LACP_UNSELECTED) {
 1415                         new_state = LACP_MUX_DETACHED;
 1416                 }
 1417                 break;
 1418         case LACP_MUX_ATTACHED:
 1419                 if (selected == LACP_SELECTED && p_sync) {
 1420                         new_state = LACP_MUX_COLLECTING;
 1421                 } else if (selected != LACP_SELECTED) {
 1422                         new_state = LACP_MUX_DETACHED;
 1423                 }
 1424                 break;
 1425         case LACP_MUX_COLLECTING:
 1426                 if (selected == LACP_SELECTED && p_sync && p_collecting) {
 1427                         new_state = LACP_MUX_DISTRIBUTING;
 1428                 } else if (selected != LACP_SELECTED || !p_sync) {
 1429                         new_state = LACP_MUX_ATTACHED;
 1430                 }
 1431                 break;
 1432         case LACP_MUX_DISTRIBUTING:
 1433                 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
 1434                         new_state = LACP_MUX_COLLECTING;
 1435                         lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
 1436                         sc->sc_flapping++;
 1437                 }
 1438                 break;
 1439         default:
 1440                 panic("%s: unknown state", __func__);
 1441         }
 1442 
 1443         if (lp->lp_mux_state == new_state) {
 1444                 return;
 1445         }
 1446 
 1447         lacp_set_mux(lp, new_state);
 1448         goto re_eval;
 1449 }
 1450 
 1451 static void
 1452 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
 1453 {
 1454         struct lacp_aggregator *la = lp->lp_aggregator;
 1455 
 1456         if (lp->lp_mux_state == new_state) {
 1457                 return;
 1458         }
 1459 
 1460         switch (new_state) {
 1461         case LACP_MUX_DETACHED:
 1462                 lp->lp_state &= ~LACP_STATE_SYNC;
 1463                 lacp_disable_distributing(lp);
 1464                 lacp_disable_collecting(lp);
 1465                 lacp_sm_assert_ntt(lp);
 1466                 /* cancel timer */
 1467                 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
 1468                         KASSERT(la->la_pending > 0,
 1469                             ("timer_wait_while not active"));
 1470                         la->la_pending--;
 1471                 }
 1472                 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
 1473                 lacp_unselect(lp);
 1474                 break;
 1475         case LACP_MUX_WAITING:
 1476                 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
 1477                     LACP_AGGREGATE_WAIT_TIME);
 1478                 la->la_pending++;
 1479                 break;
 1480         case LACP_MUX_ATTACHED:
 1481                 lp->lp_state |= LACP_STATE_SYNC;
 1482                 lacp_disable_collecting(lp);
 1483                 lacp_sm_assert_ntt(lp);
 1484                 break;
 1485         case LACP_MUX_COLLECTING:
 1486                 lacp_enable_collecting(lp);
 1487                 lacp_disable_distributing(lp);
 1488                 lacp_sm_assert_ntt(lp);
 1489                 break;
 1490         case LACP_MUX_DISTRIBUTING:
 1491                 lacp_enable_distributing(lp);
 1492                 break;
 1493         default:
 1494                 panic("%s: unknown state", __func__);
 1495         }
 1496 
 1497         LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
 1498 
 1499         lp->lp_mux_state = new_state;
 1500 }
 1501 
 1502 static void
 1503 lacp_sm_mux_timer(struct lacp_port *lp)
 1504 {
 1505         struct lacp_aggregator *la = lp->lp_aggregator;
 1506         char buf[LACP_LAGIDSTR_MAX+1];
 1507 
 1508         KASSERT(la->la_pending > 0, ("no pending event"));
 1509 
 1510         LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
 1511             lacp_format_lagid(&la->la_actor, &la->la_partner,
 1512             buf, sizeof(buf)),
 1513             la->la_pending, la->la_pending - 1));
 1514 
 1515         la->la_pending--;
 1516 }
 1517 
 1518 /* periodic transmit machine */
 1519 
 1520 static void
 1521 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
 1522 {
 1523         if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
 1524             LACP_STATE_TIMEOUT)) {
 1525                 return;
 1526         }
 1527 
 1528         LACP_DPRINTF((lp, "partner timeout changed\n"));
 1529 
 1530         /*
 1531          * FAST_PERIODIC -> SLOW_PERIODIC
 1532          * or
 1533          * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
 1534          *
 1535          * let lacp_sm_ptx_tx_schedule to update timeout.
 1536          */
 1537 
 1538         LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
 1539 
 1540         /*
 1541          * if timeout has been shortened, assert NTT.
 1542          */
 1543 
 1544         if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
 1545                 lacp_sm_assert_ntt(lp);
 1546         }
 1547 }
 1548 
 1549 static void
 1550 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
 1551 {
 1552         int timeout;
 1553 
 1554         if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
 1555             !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
 1556 
 1557                 /*
 1558                  * NO_PERIODIC
 1559                  */
 1560 
 1561                 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
 1562                 return;
 1563         }
 1564 
 1565         if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
 1566                 return;
 1567         }
 1568 
 1569         timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
 1570             LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
 1571 
 1572         LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
 1573 }
 1574 
 1575 static void
 1576 lacp_sm_ptx_timer(struct lacp_port *lp)
 1577 {
 1578         lacp_sm_assert_ntt(lp);
 1579 }
 1580 
 1581 static void
 1582 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
 1583 {
 1584         int timeout;
 1585 
 1586         /*
 1587          * check LACP_DISABLED first
 1588          */
 1589 
 1590         if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
 1591                 return;
 1592         }
 1593 
 1594         /*
 1595          * check loopback condition.
 1596          */
 1597 
 1598         if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
 1599             &lp->lp_actor.lip_systemid)) {
 1600                 return;
 1601         }
 1602 
 1603         /*
 1604          * EXPIRED, DEFAULTED, CURRENT -> CURRENT
 1605          */
 1606 
 1607         lacp_sm_rx_update_selected(lp, du);
 1608         lacp_sm_rx_update_ntt(lp, du);
 1609         lacp_sm_rx_record_pdu(lp, du);
 1610 
 1611         timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
 1612             LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
 1613         LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
 1614 
 1615         lp->lp_state &= ~LACP_STATE_EXPIRED;
 1616 
 1617         /*
 1618          * kick transmit machine without waiting the next tick.
 1619          */
 1620 
 1621         lacp_sm_tx(lp);
 1622 }
 1623 
 1624 static void
 1625 lacp_sm_rx_set_expired(struct lacp_port *lp)
 1626 {
 1627         lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
 1628         lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
 1629         LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
 1630         lp->lp_state |= LACP_STATE_EXPIRED;
 1631 }
 1632 
 1633 static void
 1634 lacp_sm_rx_timer(struct lacp_port *lp)
 1635 {
 1636         if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
 1637                 /* CURRENT -> EXPIRED */
 1638                 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
 1639                 lacp_sm_rx_set_expired(lp);
 1640         } else {
 1641                 /* EXPIRED -> DEFAULTED */
 1642                 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
 1643                 lacp_sm_rx_update_default_selected(lp);
 1644                 lacp_sm_rx_record_default(lp);
 1645                 lp->lp_state &= ~LACP_STATE_EXPIRED;
 1646         }
 1647 }
 1648 
 1649 static void
 1650 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
 1651 {
 1652         boolean_t active;
 1653         uint8_t oldpstate;
 1654         char buf[LACP_STATESTR_MAX+1];
 1655 
 1656         LACP_TRACE(lp);
 1657 
 1658         oldpstate = lp->lp_partner.lip_state;
 1659 
 1660         active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
 1661             || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
 1662             (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
 1663 
 1664         lp->lp_partner = du->ldu_actor;
 1665         if (active &&
 1666             ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
 1667             LACP_STATE_AGGREGATION) &&
 1668             !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
 1669             || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
 1670                 /*
 1671                  * XXX Maintain legacy behavior of leaving the
 1672                  * LACP_STATE_SYNC bit unchanged from the partner's
 1673                  * advertisement if lsc_strict_mode is false.
 1674                  * TODO: We should re-examine the concept of the "strict mode"
 1675                  * to ensure it makes sense to maintain a non-strict mode.
 1676                  */
 1677                 if (lp->lp_lsc->lsc_strict_mode)
 1678                         lp->lp_partner.lip_state |= LACP_STATE_SYNC;
 1679         } else {
 1680                 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
 1681         }
 1682 
 1683         lp->lp_state &= ~LACP_STATE_DEFAULTED;
 1684 
 1685         if (oldpstate != lp->lp_partner.lip_state) {
 1686                 LACP_DPRINTF((lp, "old pstate %s\n",
 1687                     lacp_format_state(oldpstate, buf, sizeof(buf))));
 1688                 LACP_DPRINTF((lp, "new pstate %s\n",
 1689                     lacp_format_state(lp->lp_partner.lip_state, buf,
 1690                     sizeof(buf))));
 1691         }
 1692 
 1693         lacp_sm_ptx_update_timeout(lp, oldpstate);
 1694 }
 1695 
 1696 static void
 1697 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
 1698 {
 1699 
 1700         LACP_TRACE(lp);
 1701 
 1702         if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
 1703             !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
 1704             LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
 1705                 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
 1706                 lacp_sm_assert_ntt(lp);
 1707         }
 1708 }
 1709 
 1710 static void
 1711 lacp_sm_rx_record_default(struct lacp_port *lp)
 1712 {
 1713         uint8_t oldpstate;
 1714 
 1715         LACP_TRACE(lp);
 1716 
 1717         oldpstate = lp->lp_partner.lip_state;
 1718         if (lp->lp_lsc->lsc_strict_mode)
 1719                 lp->lp_partner = lacp_partner_admin_strict;
 1720         else
 1721                 lp->lp_partner = lacp_partner_admin_optimistic;
 1722         lp->lp_state |= LACP_STATE_DEFAULTED;
 1723         lacp_sm_ptx_update_timeout(lp, oldpstate);
 1724 }
 1725 
 1726 static void
 1727 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
 1728     const struct lacp_peerinfo *info)
 1729 {
 1730 
 1731         LACP_TRACE(lp);
 1732 
 1733         if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
 1734             !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
 1735             LACP_STATE_AGGREGATION)) {
 1736                 lp->lp_selected = LACP_UNSELECTED;
 1737                 /* mux machine will clean up lp->lp_aggregator */
 1738         }
 1739 }
 1740 
 1741 static void
 1742 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
 1743 {
 1744 
 1745         LACP_TRACE(lp);
 1746 
 1747         lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
 1748 }
 1749 
 1750 static void
 1751 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
 1752 {
 1753 
 1754         LACP_TRACE(lp);
 1755 
 1756         if (lp->lp_lsc->lsc_strict_mode)
 1757                 lacp_sm_rx_update_selected_from_peerinfo(lp,
 1758                     &lacp_partner_admin_strict);
 1759         else
 1760                 lacp_sm_rx_update_selected_from_peerinfo(lp,
 1761                     &lacp_partner_admin_optimistic);
 1762 }
 1763 
 1764 /* transmit machine */
 1765 
 1766 static void
 1767 lacp_sm_tx(struct lacp_port *lp)
 1768 {
 1769         int error = 0;
 1770 
 1771         if (!(lp->lp_state & LACP_STATE_AGGREGATION)
 1772 #if 1
 1773             || (!(lp->lp_state & LACP_STATE_ACTIVITY)
 1774             && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
 1775 #endif
 1776             ) {
 1777                 lp->lp_flags &= ~LACP_PORT_NTT;
 1778         }
 1779 
 1780         if (!(lp->lp_flags & LACP_PORT_NTT)) {
 1781                 return;
 1782         }
 1783 
 1784         /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
 1785         if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
 1786                     (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
 1787                 LACP_DPRINTF((lp, "rate limited pdu\n"));
 1788                 return;
 1789         }
 1790 
 1791         if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
 1792                 error = lacp_xmit_lacpdu(lp);
 1793         } else {
 1794                 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
 1795         }
 1796 
 1797         if (error == 0) {
 1798                 lp->lp_flags &= ~LACP_PORT_NTT;
 1799         } else {
 1800                 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
 1801                     error));
 1802         }
 1803 }
 1804 
 1805 static void
 1806 lacp_sm_assert_ntt(struct lacp_port *lp)
 1807 {
 1808 
 1809         lp->lp_flags |= LACP_PORT_NTT;
 1810 }
 1811 
 1812 static void
 1813 lacp_run_timers(struct lacp_port *lp)
 1814 {
 1815         int i;
 1816 
 1817         for (i = 0; i < LACP_NTIMER; i++) {
 1818                 KASSERT(lp->lp_timer[i] >= 0,
 1819                     ("invalid timer value %d", lp->lp_timer[i]));
 1820                 if (lp->lp_timer[i] == 0) {
 1821                         continue;
 1822                 } else if (--lp->lp_timer[i] <= 0) {
 1823                         if (lacp_timer_funcs[i]) {
 1824                                 (*lacp_timer_funcs[i])(lp);
 1825                         }
 1826                 }
 1827         }
 1828 }
 1829 
 1830 int
 1831 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
 1832 {
 1833         struct lacp_softc *lsc = lp->lp_lsc;
 1834         struct lagg_port *lgp = lp->lp_lagg;
 1835         struct lacp_port *lp2;
 1836         struct markerdu *mdu;
 1837         int error = 0;
 1838         int pending = 0;
 1839 
 1840         if (m->m_pkthdr.len != sizeof(*mdu)) {
 1841                 goto bad;
 1842         }
 1843 
 1844         if ((m->m_flags & M_MCAST) == 0) {
 1845                 goto bad;
 1846         }
 1847 
 1848         if (m->m_len < sizeof(*mdu)) {
 1849                 m = m_pullup(m, sizeof(*mdu));
 1850                 if (m == NULL) {
 1851                         return (ENOMEM);
 1852                 }
 1853         }
 1854 
 1855         mdu = mtod(m, struct markerdu *);
 1856 
 1857         if (memcmp(&mdu->mdu_eh.ether_dhost,
 1858             &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
 1859                 goto bad;
 1860         }
 1861 
 1862         if (mdu->mdu_sph.sph_version != 1) {
 1863                 goto bad;
 1864         }
 1865 
 1866         switch (mdu->mdu_tlv.tlv_type) {
 1867         case MARKER_TYPE_INFO:
 1868                 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
 1869                     marker_info_tlv_template, TRUE)) {
 1870                         goto bad;
 1871                 }
 1872                 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
 1873                 memcpy(&mdu->mdu_eh.ether_dhost,
 1874                     &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
 1875                 memcpy(&mdu->mdu_eh.ether_shost,
 1876                     lgp->lp_lladdr, ETHER_ADDR_LEN);
 1877                 error = lagg_enqueue(lp->lp_ifp, m);
 1878                 break;
 1879 
 1880         case MARKER_TYPE_RESPONSE:
 1881                 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
 1882                     marker_response_tlv_template, TRUE)) {
 1883                         goto bad;
 1884                 }
 1885                 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
 1886                     ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
 1887                     ":", ntohl(mdu->mdu_info.mi_rq_xid)));
 1888 
 1889                 /* Verify that it is the last marker we sent out */
 1890                 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
 1891                     sizeof(struct lacp_markerinfo)))
 1892                         goto bad;
 1893 
 1894                 LACP_LOCK(lsc);
 1895                 lp->lp_flags &= ~LACP_PORT_MARK;
 1896 
 1897                 if (lsc->lsc_suppress_distributing) {
 1898                         /* Check if any ports are waiting for a response */
 1899                         LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
 1900                                 if (lp2->lp_flags & LACP_PORT_MARK) {
 1901                                         pending = 1;
 1902                                         break;
 1903                                 }
 1904                         }
 1905 
 1906                         if (pending == 0) {
 1907                                 /* All interface queues are clear */
 1908                                 LACP_DPRINTF((NULL, "queue flush complete\n"));
 1909                                 lsc->lsc_suppress_distributing = FALSE;
 1910                         }
 1911                 }
 1912                 LACP_UNLOCK(lsc);
 1913                 m_freem(m);
 1914                 break;
 1915 
 1916         default:
 1917                 goto bad;
 1918         }
 1919 
 1920         return (error);
 1921 
 1922 bad:
 1923         LACP_DPRINTF((lp, "bad marker frame\n"));
 1924         m_freem(m);
 1925         return (EINVAL);
 1926 }
 1927 
 1928 static int
 1929 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
 1930     const struct tlv_template *tmpl, boolean_t check_type)
 1931 {
 1932         while (/* CONSTCOND */ 1) {
 1933                 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
 1934                         return (EINVAL);
 1935                 }
 1936                 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
 1937                     tlv->tlv_length != tmpl->tmpl_length) {
 1938                         return (EINVAL);
 1939                 }
 1940                 if (tmpl->tmpl_type == 0) {
 1941                         break;
 1942                 }
 1943                 tlv = (const struct tlvhdr *)
 1944                     ((const char *)tlv + tlv->tlv_length);
 1945                 tmpl++;
 1946         }
 1947 
 1948         return (0);
 1949 }
 1950 
 1951 /* Debugging */
 1952 const char *
 1953 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
 1954 {
 1955         snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
 1956             (int)mac[0],
 1957             (int)mac[1],
 1958             (int)mac[2],
 1959             (int)mac[3],
 1960             (int)mac[4],
 1961             (int)mac[5]);
 1962 
 1963         return (buf);
 1964 }
 1965 
 1966 const char *
 1967 lacp_format_systemid(const struct lacp_systemid *sysid,
 1968     char *buf, size_t buflen)
 1969 {
 1970         char macbuf[LACP_MACSTR_MAX+1];
 1971 
 1972         snprintf(buf, buflen, "%04X,%s",
 1973             ntohs(sysid->lsi_prio),
 1974             lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
 1975 
 1976         return (buf);
 1977 }
 1978 
 1979 const char *
 1980 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
 1981 {
 1982         snprintf(buf, buflen, "%04X,%04X",
 1983             ntohs(portid->lpi_prio),
 1984             ntohs(portid->lpi_portno));
 1985 
 1986         return (buf);
 1987 }
 1988 
 1989 const char *
 1990 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
 1991 {
 1992         char sysid[LACP_SYSTEMIDSTR_MAX+1];
 1993         char portid[LACP_PORTIDSTR_MAX+1];
 1994 
 1995         snprintf(buf, buflen, "(%s,%04X,%s)",
 1996             lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
 1997             ntohs(peer->lip_key),
 1998             lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
 1999 
 2000         return (buf);
 2001 }
 2002 
 2003 const char *
 2004 lacp_format_lagid(const struct lacp_peerinfo *a,
 2005     const struct lacp_peerinfo *b, char *buf, size_t buflen)
 2006 {
 2007         char astr[LACP_PARTNERSTR_MAX+1];
 2008         char bstr[LACP_PARTNERSTR_MAX+1];
 2009 
 2010 #if 0
 2011         /*
 2012          * there's a convention to display small numbered peer
 2013          * in the left.
 2014          */
 2015 
 2016         if (lacp_compare_peerinfo(a, b) > 0) {
 2017                 const struct lacp_peerinfo *t;
 2018 
 2019                 t = a;
 2020                 a = b;
 2021                 b = t;
 2022         }
 2023 #endif
 2024 
 2025         snprintf(buf, buflen, "[%s,%s]",
 2026             lacp_format_partner(a, astr, sizeof(astr)),
 2027             lacp_format_partner(b, bstr, sizeof(bstr)));
 2028 
 2029         return (buf);
 2030 }
 2031 
 2032 const char *
 2033 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
 2034     char *buf, size_t buflen)
 2035 {
 2036         if (la == NULL) {
 2037                 return ("(none)");
 2038         }
 2039 
 2040         return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
 2041 }
 2042 
 2043 const char *
 2044 lacp_format_state(uint8_t state, char *buf, size_t buflen)
 2045 {
 2046         snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
 2047         return (buf);
 2048 }
 2049 
 2050 static void
 2051 lacp_dump_lacpdu(const struct lacpdu *du)
 2052 {
 2053         char buf[LACP_PARTNERSTR_MAX+1];
 2054         char buf2[LACP_STATESTR_MAX+1];
 2055 
 2056         printf("actor=%s\n",
 2057             lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
 2058         printf("actor.state=%s\n",
 2059             lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
 2060         printf("partner=%s\n",
 2061             lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
 2062         printf("partner.state=%s\n",
 2063             lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
 2064 
 2065         printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
 2066 }
 2067 
 2068 static void
 2069 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
 2070 {
 2071         va_list va;
 2072 
 2073         if (lp) {
 2074                 printf("%s: ", lp->lp_ifp->if_xname);
 2075         }
 2076 
 2077         va_start(va, fmt);
 2078         vprintf(fmt, va);
 2079         va_end(va);
 2080 }

Cache object: 6af04e8af343856d60e4ba2e72c03624


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