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

Cache object: d91f1fc9ac27813fca4ba9242b5e710d


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