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

Cache object: bfd81c6ab954cf50b9980f55c61c5a67


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