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

Cache object: f7191e5b7295cd022aa06e4ea1598856


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