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/trunklacp.c

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

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

Cache object: ebb43d0fd81426b3a31f3129b80502d0


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