1 /* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c)2005 YAMAMOTO Takashi,
7 * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_ratelimit.h"
37 #include <sys/param.h>
38 #include <sys/callout.h>
39 #include <sys/eventhandler.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h> /* hz */
44 #include <sys/socket.h> /* for net/if.h */
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
47 #include <machine/stdarg.h>
49 #include <sys/rwlock.h>
50 #include <sys/taskqueue.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/ethernet.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
59 #include <net/if_lagg.h>
60 #include <net/ieee8023ad_lacp.h>
63 * actor system priority and port priority.
64 * XXX should be configurable.
67 #define LACP_SYSTEM_PRIO 0x8000
68 #define LACP_PORT_PRIO 0x8000
70 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
71 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
73 static const struct tlv_template lacp_info_tlv_template[] = {
74 { LACP_TYPE_ACTORINFO,
75 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
76 { LACP_TYPE_PARTNERINFO,
77 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
78 { LACP_TYPE_COLLECTORINFO,
79 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
83 static const struct tlv_template marker_info_tlv_template[] = {
85 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
89 static const struct tlv_template marker_response_tlv_template[] = {
90 { MARKER_TYPE_RESPONSE,
91 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
95 typedef void (*lacp_timer_func_t)(struct lacp_port *);
97 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
98 static void lacp_fill_markerinfo(struct lacp_port *,
99 struct lacp_markerinfo *);
101 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
102 static void lacp_suppress_distributing(struct lacp_softc *,
103 struct lacp_aggregator *);
104 static void lacp_transit_expire(void *);
105 static void lacp_update_portmap(struct lacp_softc *);
106 static void lacp_select_active_aggregator(struct lacp_softc *);
107 static uint16_t lacp_compose_key(struct lacp_port *);
108 static int tlv_check(const void *, size_t, const struct tlvhdr *,
109 const struct tlv_template *, boolean_t);
110 static void lacp_tick(void *);
112 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
113 const struct lacp_port *);
114 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
115 const struct lacp_peerinfo *);
116 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
117 const struct lacp_port *);
118 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
119 const struct lacp_peerinfo *);
121 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
123 static void lacp_aggregator_addref(struct lacp_softc *,
124 struct lacp_aggregator *);
125 static void lacp_aggregator_delref(struct lacp_softc *,
126 struct lacp_aggregator *);
128 /* receive machine */
130 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
131 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
132 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
133 static void lacp_sm_rx_timer(struct lacp_port *);
134 static void lacp_sm_rx_set_expired(struct lacp_port *);
135 static void lacp_sm_rx_update_ntt(struct lacp_port *,
136 const struct lacpdu *);
137 static void lacp_sm_rx_record_pdu(struct lacp_port *,
138 const struct lacpdu *);
139 static void lacp_sm_rx_update_selected(struct lacp_port *,
140 const struct lacpdu *);
141 static void lacp_sm_rx_record_default(struct lacp_port *);
142 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
143 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
144 const struct lacp_peerinfo *);
148 static void lacp_sm_mux(struct lacp_port *);
149 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
150 static void lacp_sm_mux_timer(struct lacp_port *);
152 /* periodic transmit machine */
154 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
155 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
156 static void lacp_sm_ptx_timer(struct lacp_port *);
158 /* transmit machine */
160 static void lacp_sm_tx(struct lacp_port *);
161 static void lacp_sm_assert_ntt(struct lacp_port *);
163 static void lacp_run_timers(struct lacp_port *);
164 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
165 const struct lacp_peerinfo *);
166 static int lacp_compare_systemid(const struct lacp_systemid *,
167 const struct lacp_systemid *);
168 static void lacp_port_enable(struct lacp_port *);
169 static void lacp_port_disable(struct lacp_port *);
170 static void lacp_select(struct lacp_port *);
171 static void lacp_unselect(struct lacp_port *);
172 static void lacp_disable_collecting(struct lacp_port *);
173 static void lacp_enable_collecting(struct lacp_port *);
174 static void lacp_disable_distributing(struct lacp_port *);
175 static void lacp_enable_distributing(struct lacp_port *);
176 static int lacp_xmit_lacpdu(struct lacp_port *);
177 static int lacp_xmit_marker(struct lacp_port *);
181 static void lacp_dump_lacpdu(const struct lacpdu *);
182 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
184 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
185 const struct lacp_peerinfo *, char *, size_t);
186 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
188 static const char *lacp_format_state(uint8_t, char *, size_t);
189 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
190 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
192 static const char *lacp_format_portid(const struct lacp_portid *, char *,
194 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
195 __attribute__((__format__(__printf__, 2, 3)));
197 VNET_DEFINE_STATIC(int, lacp_debug);
198 #define V_lacp_debug VNET(lacp_debug)
199 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad");
200 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
201 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
203 VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
204 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
205 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
206 "LACP strict protocol compliance default");
208 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
209 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
210 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
213 * partner administration variables.
214 * XXX should be configurable.
217 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
218 .lip_systemid = { .lsi_prio = 0xffff },
219 .lip_portid = { .lpi_prio = 0xffff },
220 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
221 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
224 static const struct lacp_peerinfo lacp_partner_admin_strict = {
225 .lip_systemid = { .lsi_prio = 0xffff },
226 .lip_portid = { .lpi_prio = 0xffff },
230 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
231 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
232 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
233 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
237 lacp_input(struct lagg_port *lgp, struct mbuf *m)
239 struct lacp_port *lp = LACP_PORT(lgp);
242 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
247 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
249 case SLOWPROTOCOLS_SUBTYPE_LACP:
250 lacp_pdu_input(lp, m);
253 case SLOWPROTOCOLS_SUBTYPE_MARKER:
254 lacp_marker_input(lp, m);
258 /* Not a subtype we are interested in */
263 * lacp_pdu_input: process lacpdu
266 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
268 struct lacp_softc *lsc = lp->lp_lsc;
272 if (m->m_pkthdr.len != sizeof(*du)) {
276 if ((m->m_flags & M_MCAST) == 0) {
280 if (m->m_len < sizeof(*du)) {
281 m = m_pullup(m, sizeof(*du));
287 du = mtod(m, struct lacpdu *);
289 if (memcmp(&du->ldu_eh.ether_dhost,
290 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
295 * ignore the version for compatibility with
296 * the future protocol revisions.
299 if (du->ldu_sph.sph_version != 1) {
305 * ignore tlv types for compatibility with
306 * the future protocol revisions.
308 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
309 lacp_info_tlv_template, FALSE)) {
313 if (V_lacp_debug > 0) {
314 lacp_dprintf(lp, "lacpdu receive\n");
315 lacp_dump_lacpdu(du);
318 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
319 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
336 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
338 struct lagg_port *lgp = lp->lp_lagg;
339 struct lagg_softc *sc = lgp->lp_softc;
341 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
342 memcpy(&info->lip_systemid.lsi_mac,
343 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
344 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
345 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
346 info->lip_state = lp->lp_state;
350 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
352 struct ifnet *ifp = lp->lp_ifp;
354 /* Fill in the port index and system id (encoded as the MAC) */
355 info->mi_rq_port = htons(ifp->if_index);
356 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
357 info->mi_rq_xid = htonl(0);
361 lacp_xmit_lacpdu(struct lacp_port *lp)
363 struct lagg_port *lgp = lp->lp_lagg;
368 LACP_LOCK_ASSERT(lp->lp_lsc);
370 m = m_gethdr(M_NOWAIT, MT_DATA);
374 m->m_len = m->m_pkthdr.len = sizeof(*du);
376 du = mtod(m, struct lacpdu *);
377 memset(du, 0, sizeof(*du));
379 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
381 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
382 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
384 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
385 du->ldu_sph.sph_version = 1;
387 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
388 du->ldu_actor = lp->lp_actor;
390 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
391 sizeof(du->ldu_partner));
392 du->ldu_partner = lp->lp_partner;
394 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
395 sizeof(du->ldu_collector));
396 du->ldu_collector.lci_maxdelay = 0;
398 if (V_lacp_debug > 0) {
399 lacp_dprintf(lp, "lacpdu transmit\n");
400 lacp_dump_lacpdu(du);
403 m->m_flags |= M_MCAST;
406 * XXX should use higher priority queue.
407 * otherwise network congestion can break aggregation.
410 error = lagg_enqueue(lp->lp_ifp, m);
415 lacp_xmit_marker(struct lacp_port *lp)
417 struct lagg_port *lgp = lp->lp_lagg;
419 struct markerdu *mdu;
422 LACP_LOCK_ASSERT(lp->lp_lsc);
424 m = m_gethdr(M_NOWAIT, MT_DATA);
428 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
430 mdu = mtod(m, struct markerdu *);
431 memset(mdu, 0, sizeof(*mdu));
433 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
435 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
436 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
438 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
439 mdu->mdu_sph.sph_version = 1;
441 /* Bump the transaction id and copy over the marker info */
442 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
443 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
444 mdu->mdu_info = lp->lp_marker;
446 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
447 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
448 ntohl(mdu->mdu_info.mi_rq_xid)));
450 m->m_flags |= M_MCAST;
451 error = lagg_enqueue(lp->lp_ifp, m);
456 lacp_linkstate(struct lagg_port *lgp)
458 struct lacp_port *lp = LACP_PORT(lgp);
459 struct lacp_softc *lsc = lp->lp_lsc;
460 struct ifnet *ifp = lgp->lp_ifp;
461 struct ifmediareq ifmr;
467 bzero((char *)&ifmr, sizeof(ifmr));
468 error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
470 bzero((char *)&ifmr, sizeof(ifmr));
471 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
477 media = ifmr.ifm_active;
478 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
479 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
480 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
481 old_state = lp->lp_state;
482 old_key = lp->lp_key;
484 lp->lp_media = media;
486 * If the port is not an active full duplex Ethernet link then it can
489 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
490 ifp->if_link_state != LINK_STATE_UP) {
491 lacp_port_disable(lp);
493 lacp_port_enable(lp);
495 lp->lp_key = lacp_compose_key(lp);
497 if (old_state != lp->lp_state || old_key != lp->lp_key) {
498 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
499 lp->lp_selected = LACP_UNSELECTED;
507 struct lacp_softc *lsc = arg;
508 struct lacp_port *lp;
510 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
511 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
514 CURVNET_SET(lp->lp_ifp->if_vnet);
520 lacp_sm_ptx_tx_schedule(lp);
523 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
527 lacp_port_create(struct lagg_port *lgp)
529 struct lagg_softc *sc = lgp->lp_softc;
530 struct lacp_softc *lsc = LACP_SOFTC(sc);
531 struct lacp_port *lp;
532 struct ifnet *ifp = lgp->lp_ifp;
533 struct sockaddr_dl sdl;
534 struct ifmultiaddr *rifma = NULL;
537 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
538 sdl.sdl_alen = ETHER_ADDR_LEN;
540 bcopy(ðermulticastaddr_slowprotocols,
541 LLADDR(&sdl), ETHER_ADDR_LEN);
542 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
544 printf("%s: ADDMULTI failed on %s\n", __func__,
545 lgp->lp_ifp->if_xname);
549 lp = malloc(sizeof(struct lacp_port),
550 M_DEVBUF, M_NOWAIT|M_ZERO);
561 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
563 lacp_fill_actorinfo(lp, &lp->lp_actor);
564 lacp_fill_markerinfo(lp, &lp->lp_marker);
565 lp->lp_state = LACP_STATE_ACTIVITY;
566 lp->lp_aggregator = NULL;
567 lacp_sm_rx_set_expired(lp);
575 lacp_port_destroy(struct lagg_port *lgp)
577 struct lacp_port *lp = LACP_PORT(lgp);
578 struct lacp_softc *lsc = lp->lp_lsc;
582 for (i = 0; i < LACP_NTIMER; i++) {
583 LACP_TIMER_DISARM(lp, i);
586 lacp_disable_collecting(lp);
587 lacp_disable_distributing(lp);
590 LIST_REMOVE(lp, lp_next);
593 /* The address may have already been removed by if_purgemaddrs() */
594 if (!lgp->lp_detaching)
595 if_delmulti_ifma(lp->lp_ifma);
601 lacp_req(struct lagg_softc *sc, void *data)
603 struct lacp_opreq *req = (struct lacp_opreq *)data;
604 struct lacp_softc *lsc = LACP_SOFTC(sc);
605 struct lacp_aggregator *la;
607 bzero(req, sizeof(struct lacp_opreq));
610 * If the LACP softc is NULL, return with the opreq structure full of
611 * zeros. It is normal for the softc to be NULL while the lagg is
617 la = lsc->lsc_active_aggregator;
620 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
621 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
623 req->actor_key = ntohs(la->la_actor.lip_key);
624 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
625 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
626 req->actor_state = la->la_actor.lip_state;
628 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
629 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
631 req->partner_key = ntohs(la->la_partner.lip_key);
632 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
633 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
634 req->partner_state = la->la_partner.lip_state;
640 lacp_portreq(struct lagg_port *lgp, void *data)
642 struct lacp_opreq *req = (struct lacp_opreq *)data;
643 struct lacp_port *lp = LACP_PORT(lgp);
644 struct lacp_softc *lsc = lp->lp_lsc;
647 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
648 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
650 req->actor_key = ntohs(lp->lp_actor.lip_key);
651 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
652 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
653 req->actor_state = lp->lp_actor.lip_state;
655 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
656 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
658 req->partner_key = ntohs(lp->lp_partner.lip_key);
659 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
660 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
661 req->partner_state = lp->lp_partner.lip_state;
666 lacp_disable_collecting(struct lacp_port *lp)
668 LACP_DPRINTF((lp, "collecting disabled\n"));
669 lp->lp_state &= ~LACP_STATE_COLLECTING;
673 lacp_enable_collecting(struct lacp_port *lp)
675 LACP_DPRINTF((lp, "collecting enabled\n"));
676 lp->lp_state |= LACP_STATE_COLLECTING;
680 lacp_disable_distributing(struct lacp_port *lp)
682 struct lacp_aggregator *la = lp->lp_aggregator;
683 struct lacp_softc *lsc = lp->lp_lsc;
684 struct lagg_softc *sc = lsc->lsc_softc;
685 char buf[LACP_LAGIDSTR_MAX+1];
687 LACP_LOCK_ASSERT(lsc);
689 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
693 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
694 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
695 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
697 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
699 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
700 la->la_nports, la->la_nports - 1));
702 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
704 sc->sc_active = la->la_nports;
706 if (lsc->lsc_active_aggregator == la) {
707 lacp_suppress_distributing(lsc, la);
708 lacp_select_active_aggregator(lsc);
709 /* regenerate the port map, the active aggregator has changed */
710 lacp_update_portmap(lsc);
713 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
714 if_link_state_change(sc->sc_ifp,
715 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
719 lacp_enable_distributing(struct lacp_port *lp)
721 struct lacp_aggregator *la = lp->lp_aggregator;
722 struct lacp_softc *lsc = lp->lp_lsc;
723 struct lagg_softc *sc = lsc->lsc_softc;
724 char buf[LACP_LAGIDSTR_MAX+1];
726 LACP_LOCK_ASSERT(lsc);
728 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
732 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
734 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
735 la->la_nports, la->la_nports + 1));
737 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
738 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
740 sc->sc_active = la->la_nports;
742 lp->lp_state |= LACP_STATE_DISTRIBUTING;
744 if (lsc->lsc_active_aggregator == la) {
745 lacp_suppress_distributing(lsc, la);
746 lacp_update_portmap(lsc);
748 /* try to become the active aggregator */
749 lacp_select_active_aggregator(lsc);
751 if_link_state_change(sc->sc_ifp,
752 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
756 lacp_transit_expire(void *vp)
758 struct lacp_softc *lsc = vp;
760 LACP_LOCK_ASSERT(lsc);
762 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
766 lsc->lsc_suppress_distributing = FALSE;
770 lacp_attach(struct lagg_softc *sc)
772 struct lacp_softc *lsc;
774 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
779 lsc->lsc_hashkey = m_ether_tcpip_hash_init();
780 lsc->lsc_active_aggregator = NULL;
781 lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
783 TAILQ_INIT(&lsc->lsc_aggregators);
784 LIST_INIT(&lsc->lsc_ports);
786 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
787 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
789 /* if the lagg is already up then do the same */
790 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
795 lacp_detach(void *psc)
797 struct lacp_softc *lsc = (struct lacp_softc *)psc;
799 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
800 ("aggregators still active"));
801 KASSERT(lsc->lsc_active_aggregator == NULL,
802 ("aggregator still attached"));
804 callout_drain(&lsc->lsc_transit_callout);
805 callout_drain(&lsc->lsc_callout);
807 LACP_LOCK_DESTROY(lsc);
812 lacp_init(struct lagg_softc *sc)
814 struct lacp_softc *lsc = LACP_SOFTC(sc);
817 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
822 lacp_stop(struct lagg_softc *sc)
824 struct lacp_softc *lsc = LACP_SOFTC(sc);
827 callout_stop(&lsc->lsc_transit_callout);
828 callout_stop(&lsc->lsc_callout);
833 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
835 struct lacp_softc *lsc = LACP_SOFTC(sc);
836 struct lacp_portmap *pm;
837 struct lacp_port *lp;
838 struct lacp_port **map;
842 if (__predict_false(lsc->lsc_suppress_distributing)) {
843 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
847 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
848 if (pm->pm_count == 0) {
849 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
854 if ((sc->sc_opts & LAGG_OPT_USE_NUMA) &&
855 pm->pm_num_dom > 1 && m->m_pkthdr.numa_domain < MAXMEMDOM) {
856 count = pm->pm_numa[m->m_pkthdr.numa_domain].count;
858 map = pm->pm_numa[m->m_pkthdr.numa_domain].map;
860 /* No ports on this domain; use global hash. */
862 count = pm->pm_count;
868 count = pm->pm_count;
870 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
871 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
872 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
874 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
879 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
880 ("aggregated port is not distributing"));
882 return (lp->lp_lagg);
887 lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t flowid)
889 struct lacp_softc *lsc = LACP_SOFTC(sc);
890 struct lacp_portmap *pm;
891 struct lacp_port *lp;
894 if (__predict_false(lsc->lsc_suppress_distributing)) {
895 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
899 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
900 if (pm->pm_count == 0) {
901 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
905 hash = flowid >> sc->flowid_shift;
906 hash %= pm->pm_count;
907 lp = pm->pm_map[hash];
909 return (lp->lp_lagg);
914 * lacp_suppress_distributing: drop transmit packets for a while
915 * to preserve packet ordering.
919 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
921 struct lacp_port *lp;
923 if (lsc->lsc_active_aggregator != la) {
929 lsc->lsc_suppress_distributing = TRUE;
931 /* send a marker frame down each port to verify the queues are empty */
932 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
933 lp->lp_flags |= LACP_PORT_MARK;
934 lacp_xmit_marker(lp);
937 /* set a timeout for the marker frames */
938 callout_reset(&lsc->lsc_transit_callout,
939 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
943 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
944 const struct lacp_peerinfo *b)
946 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
950 lacp_compare_systemid(const struct lacp_systemid *a,
951 const struct lacp_systemid *b)
953 return (memcmp(a, b, sizeof(*a)));
958 lacp_compare_portid(const struct lacp_portid *a,
959 const struct lacp_portid *b)
961 return (memcmp(a, b, sizeof(*a)));
966 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
968 struct lacp_port *lp;
971 lp = TAILQ_FIRST(&la->la_ports);
976 speed = ifmedia_baudrate(lp->lp_media);
977 speed *= la->la_nports;
979 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
980 lp->lp_media, la->la_nports));
987 * lacp_select_active_aggregator: select an aggregator to be used to transmit
988 * packets from lagg(4) interface.
992 lacp_select_active_aggregator(struct lacp_softc *lsc)
994 struct lacp_aggregator *la;
995 struct lacp_aggregator *best_la = NULL;
996 uint64_t best_speed = 0;
997 char buf[LACP_LAGIDSTR_MAX+1];
1001 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1004 if (la->la_nports == 0) {
1008 speed = lacp_aggregator_bandwidth(la);
1009 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
1010 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
1011 speed, la->la_nports));
1014 * This aggregator is chosen if the partner has a better
1015 * system priority or, the total aggregated speed is higher
1016 * or, it is already the chosen aggregator
1018 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1019 LACP_SYS_PRI(best_la->la_partner)) ||
1020 speed > best_speed ||
1021 (speed == best_speed &&
1022 la == lsc->lsc_active_aggregator)) {
1028 KASSERT(best_la == NULL || best_la->la_nports > 0,
1029 ("invalid aggregator refcnt"));
1030 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1031 ("invalid aggregator list"));
1033 if (lsc->lsc_active_aggregator != best_la) {
1034 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1035 LACP_DPRINTF((NULL, "old %s\n",
1036 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1037 buf, sizeof(buf))));
1039 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1041 LACP_DPRINTF((NULL, "new %s\n",
1042 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1044 if (lsc->lsc_active_aggregator != best_la) {
1045 lsc->lsc_active_aggregator = best_la;
1046 lacp_update_portmap(lsc);
1048 lacp_suppress_distributing(lsc, best_la);
1054 * Updated the inactive portmap array with the new list of ports and
1058 lacp_update_portmap(struct lacp_softc *lsc)
1060 struct lagg_softc *sc = lsc->lsc_softc;
1061 struct lacp_aggregator *la;
1062 struct lacp_portmap *p;
1063 struct lacp_port *lp;
1072 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1073 p = &lsc->lsc_pmap[newmap];
1074 la = lsc->lsc_active_aggregator;
1076 bzero(p, sizeof(struct lacp_portmap));
1078 if (la != NULL && la->la_nports > 0) {
1079 p->pm_count = la->la_nports;
1081 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) {
1082 p->pm_map[i++] = lp;
1084 domain = lp->lp_ifp->if_numa_domain;
1085 if (domain >= MAXMEMDOM)
1087 count = p->pm_numa[domain].count;
1088 p->pm_numa[domain].map[count] = lp;
1089 p->pm_numa[domain].count++;
1092 KASSERT(i == p->pm_count, ("Invalid port count"));
1095 for (i = 0; i < MAXMEMDOM; i++) {
1096 if (p->pm_numa[i].count != 0)
1100 speed = lacp_aggregator_bandwidth(la);
1102 sc->sc_ifp->if_baudrate = speed;
1104 /* switch the active portmap over */
1105 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1106 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1108 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1112 lacp_compose_key(struct lacp_port *lp)
1114 struct lagg_port *lgp = lp->lp_lagg;
1115 struct lagg_softc *sc = lgp->lp_softc;
1116 u_int media = lp->lp_media;
1119 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1122 * non-aggregatable links should have unique keys.
1124 * XXX this isn't really unique as if_index is 16 bit.
1127 /* bit 0..14: (some bits of) if_index of this port */
1128 key = lp->lp_ifp->if_index;
1132 u_int subtype = IFM_SUBTYPE(media);
1134 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1135 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1137 /* bit 0..4: IFM_SUBTYPE modulo speed */
1160 case IFM_1000_SGMII:
1161 case IFM_1000_CX_SGMII:
1167 case IFM_10G_TWINAX:
1168 case IFM_10G_TWINAX_LONG:
1198 case IFM_50G_LAUI2_AC:
1200 case IFM_50G_AUI2_AC:
1206 case IFM_50G_KR_PAM4:
1207 case IFM_50G_AUI1_AC:
1235 case IFM_40G_XLAUI_AC:
1243 case IFM_100G_CAUI4_AC:
1244 case IFM_100G_CAUI4:
1245 case IFM_100G_AUI4_AC:
1247 case IFM_100G_CR_PAM4:
1248 case IFM_100G_KR_PAM4:
1252 case IFM_100G_KR2_PAM4:
1253 case IFM_100G_CAUI2_AC:
1254 case IFM_100G_CAUI2:
1255 case IFM_100G_AUI2_AC:
1259 case IFM_200G_CR4_PAM4:
1264 case IFM_200G_KR4_PAM4:
1265 case IFM_200G_AUI4_AC:
1267 case IFM_200G_AUI8_AC:
1269 key = IFM_200G_CR4_PAM4;
1274 case IFM_400G_AUI8_AC:
1282 /* bit 5..14: (some bits of) if_index of lagg device */
1283 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1286 return (htons(key));
1290 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1292 char buf[LACP_LAGIDSTR_MAX+1];
1294 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1296 lacp_format_lagid(&la->la_actor, &la->la_partner,
1298 la->la_refcnt, la->la_refcnt + 1));
1300 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1302 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1306 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1308 char buf[LACP_LAGIDSTR_MAX+1];
1310 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1312 lacp_format_lagid(&la->la_actor, &la->la_partner,
1314 la->la_refcnt, la->la_refcnt - 1));
1316 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1318 if (la->la_refcnt > 0) {
1322 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1323 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1325 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1331 * lacp_aggregator_get: allocate an aggregator.
1334 static struct lacp_aggregator *
1335 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1337 struct lacp_aggregator *la;
1339 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1343 TAILQ_INIT(&la->la_ports);
1345 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1352 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1356 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1358 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1359 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1361 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1365 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1366 const struct lacp_peerinfo *lpi_port)
1368 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1369 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1370 lpi_aggr->lip_key = lpi_port->lip_key;
1374 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1378 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1379 const struct lacp_port *lp)
1381 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1382 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1386 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1390 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1394 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1402 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1403 const struct lacp_peerinfo *b)
1405 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1406 sizeof(a->lip_systemid))) {
1410 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1418 lacp_port_enable(struct lacp_port *lp)
1420 lp->lp_state |= LACP_STATE_AGGREGATION;
1424 lacp_port_disable(struct lacp_port *lp)
1426 lacp_set_mux(lp, LACP_MUX_DETACHED);
1428 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1429 lp->lp_selected = LACP_UNSELECTED;
1430 lacp_sm_rx_record_default(lp);
1431 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1432 lp->lp_state &= ~LACP_STATE_EXPIRED;
1436 * lacp_select: select an aggregator. create one if necessary.
1439 lacp_select(struct lacp_port *lp)
1441 struct lacp_softc *lsc = lp->lp_lsc;
1442 struct lacp_aggregator *la;
1443 char buf[LACP_LAGIDSTR_MAX+1];
1445 if (lp->lp_aggregator) {
1449 /* If we haven't heard from our peer, skip this step. */
1450 if (lp->lp_state & LACP_STATE_DEFAULTED)
1453 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1454 ("timer_wait_while still active"));
1456 LACP_DPRINTF((lp, "port lagid=%s\n",
1457 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1458 buf, sizeof(buf))));
1460 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1461 if (lacp_aggregator_is_compatible(la, lp)) {
1467 la = lacp_aggregator_get(lsc, lp);
1469 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1472 * will retry on the next tick.
1477 lacp_fill_aggregator_id(la, lp);
1478 LACP_DPRINTF((lp, "aggregator created\n"));
1480 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1481 if (la->la_refcnt == LACP_MAX_PORTS)
1483 lacp_aggregator_addref(lsc, la);
1486 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1487 lacp_format_lagid(&la->la_actor, &la->la_partner,
1488 buf, sizeof(buf))));
1490 lp->lp_aggregator = la;
1491 lp->lp_selected = LACP_SELECTED;
1495 * lacp_unselect: finish unselect/detach process.
1499 lacp_unselect(struct lacp_port *lp)
1501 struct lacp_softc *lsc = lp->lp_lsc;
1502 struct lacp_aggregator *la = lp->lp_aggregator;
1504 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1505 ("timer_wait_while still active"));
1511 lp->lp_aggregator = NULL;
1512 lacp_aggregator_delref(lsc, la);
1518 lacp_sm_mux(struct lacp_port *lp)
1520 struct lagg_port *lgp = lp->lp_lagg;
1521 struct lagg_softc *sc = lgp->lp_softc;
1522 enum lacp_mux_state new_state;
1524 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1525 boolean_t p_collecting =
1526 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1527 enum lacp_selected selected = lp->lp_selected;
1528 struct lacp_aggregator *la;
1530 if (V_lacp_debug > 1)
1531 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1532 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1533 lp->lp_mux_state, selected, p_sync, p_collecting);
1536 la = lp->lp_aggregator;
1537 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1538 ("MUX not detached"));
1539 new_state = lp->lp_mux_state;
1540 switch (lp->lp_mux_state) {
1541 case LACP_MUX_DETACHED:
1542 if (selected != LACP_UNSELECTED) {
1543 new_state = LACP_MUX_WAITING;
1546 case LACP_MUX_WAITING:
1547 KASSERT(la->la_pending > 0 ||
1548 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1549 ("timer_wait_while still active"));
1550 if (selected == LACP_SELECTED && la->la_pending == 0) {
1551 new_state = LACP_MUX_ATTACHED;
1552 } else if (selected == LACP_UNSELECTED) {
1553 new_state = LACP_MUX_DETACHED;
1556 case LACP_MUX_ATTACHED:
1557 if (selected == LACP_SELECTED && p_sync) {
1558 new_state = LACP_MUX_COLLECTING;
1559 } else if (selected != LACP_SELECTED) {
1560 new_state = LACP_MUX_DETACHED;
1563 case LACP_MUX_COLLECTING:
1564 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1565 new_state = LACP_MUX_DISTRIBUTING;
1566 } else if (selected != LACP_SELECTED || !p_sync) {
1567 new_state = LACP_MUX_ATTACHED;
1570 case LACP_MUX_DISTRIBUTING:
1571 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1572 new_state = LACP_MUX_COLLECTING;
1573 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1578 panic("%s: unknown state", __func__);
1581 if (lp->lp_mux_state == new_state) {
1585 lacp_set_mux(lp, new_state);
1590 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1592 struct lacp_aggregator *la = lp->lp_aggregator;
1594 if (lp->lp_mux_state == new_state) {
1598 switch (new_state) {
1599 case LACP_MUX_DETACHED:
1600 lp->lp_state &= ~LACP_STATE_SYNC;
1601 lacp_disable_distributing(lp);
1602 lacp_disable_collecting(lp);
1603 lacp_sm_assert_ntt(lp);
1605 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1606 KASSERT(la->la_pending > 0,
1607 ("timer_wait_while not active"));
1610 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1613 case LACP_MUX_WAITING:
1614 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1615 LACP_AGGREGATE_WAIT_TIME);
1618 case LACP_MUX_ATTACHED:
1619 lp->lp_state |= LACP_STATE_SYNC;
1620 lacp_disable_collecting(lp);
1621 lacp_sm_assert_ntt(lp);
1623 case LACP_MUX_COLLECTING:
1624 lacp_enable_collecting(lp);
1625 lacp_disable_distributing(lp);
1626 lacp_sm_assert_ntt(lp);
1628 case LACP_MUX_DISTRIBUTING:
1629 lacp_enable_distributing(lp);
1632 panic("%s: unknown state", __func__);
1635 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1637 lp->lp_mux_state = new_state;
1641 lacp_sm_mux_timer(struct lacp_port *lp)
1643 struct lacp_aggregator *la = lp->lp_aggregator;
1644 char buf[LACP_LAGIDSTR_MAX+1];
1646 KASSERT(la->la_pending > 0, ("no pending event"));
1648 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1649 lacp_format_lagid(&la->la_actor, &la->la_partner,
1651 la->la_pending, la->la_pending - 1));
1656 /* periodic transmit machine */
1659 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1661 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1662 LACP_STATE_TIMEOUT)) {
1666 LACP_DPRINTF((lp, "partner timeout changed\n"));
1669 * FAST_PERIODIC -> SLOW_PERIODIC
1671 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1673 * let lacp_sm_ptx_tx_schedule to update timeout.
1676 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1679 * if timeout has been shortened, assert NTT.
1682 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1683 lacp_sm_assert_ntt(lp);
1688 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1692 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1693 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1699 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1703 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1707 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1708 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1710 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1714 lacp_sm_ptx_timer(struct lacp_port *lp)
1716 lacp_sm_assert_ntt(lp);
1720 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1725 * check LACP_DISABLED first
1728 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1733 * check loopback condition.
1736 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1737 &lp->lp_actor.lip_systemid)) {
1742 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1745 lacp_sm_rx_update_selected(lp, du);
1746 lacp_sm_rx_update_ntt(lp, du);
1747 lacp_sm_rx_record_pdu(lp, du);
1749 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1750 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1751 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1753 lp->lp_state &= ~LACP_STATE_EXPIRED;
1756 * kick transmit machine without waiting the next tick.
1763 lacp_sm_rx_set_expired(struct lacp_port *lp)
1765 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1766 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1767 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1768 lp->lp_state |= LACP_STATE_EXPIRED;
1772 lacp_sm_rx_timer(struct lacp_port *lp)
1774 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1775 /* CURRENT -> EXPIRED */
1776 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1777 lacp_sm_rx_set_expired(lp);
1779 /* EXPIRED -> DEFAULTED */
1780 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1781 lacp_sm_rx_update_default_selected(lp);
1782 lacp_sm_rx_record_default(lp);
1783 lp->lp_state &= ~LACP_STATE_EXPIRED;
1788 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1792 char buf[LACP_STATESTR_MAX+1];
1796 oldpstate = lp->lp_partner.lip_state;
1798 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1799 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1800 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1802 lp->lp_partner = du->ldu_actor;
1804 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1805 LACP_STATE_AGGREGATION) &&
1806 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1807 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1809 * XXX Maintain legacy behavior of leaving the
1810 * LACP_STATE_SYNC bit unchanged from the partner's
1811 * advertisement if lsc_strict_mode is false.
1812 * TODO: We should re-examine the concept of the "strict mode"
1813 * to ensure it makes sense to maintain a non-strict mode.
1815 if (lp->lp_lsc->lsc_strict_mode)
1816 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1818 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1821 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1823 if (oldpstate != lp->lp_partner.lip_state) {
1824 LACP_DPRINTF((lp, "old pstate %s\n",
1825 lacp_format_state(oldpstate, buf, sizeof(buf))));
1826 LACP_DPRINTF((lp, "new pstate %s\n",
1827 lacp_format_state(lp->lp_partner.lip_state, buf,
1831 lacp_sm_ptx_update_timeout(lp, oldpstate);
1835 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1840 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1841 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1842 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1843 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1844 lacp_sm_assert_ntt(lp);
1849 lacp_sm_rx_record_default(struct lacp_port *lp)
1855 oldpstate = lp->lp_partner.lip_state;
1856 if (lp->lp_lsc->lsc_strict_mode)
1857 lp->lp_partner = lacp_partner_admin_strict;
1859 lp->lp_partner = lacp_partner_admin_optimistic;
1860 lp->lp_state |= LACP_STATE_DEFAULTED;
1861 lacp_sm_ptx_update_timeout(lp, oldpstate);
1865 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1866 const struct lacp_peerinfo *info)
1871 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1872 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1873 LACP_STATE_AGGREGATION)) {
1874 lp->lp_selected = LACP_UNSELECTED;
1875 /* mux machine will clean up lp->lp_aggregator */
1880 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1885 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1889 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1894 if (lp->lp_lsc->lsc_strict_mode)
1895 lacp_sm_rx_update_selected_from_peerinfo(lp,
1896 &lacp_partner_admin_strict);
1898 lacp_sm_rx_update_selected_from_peerinfo(lp,
1899 &lacp_partner_admin_optimistic);
1902 /* transmit machine */
1905 lacp_sm_tx(struct lacp_port *lp)
1909 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1911 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1912 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1915 lp->lp_flags &= ~LACP_PORT_NTT;
1918 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1922 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1923 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1924 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1925 LACP_DPRINTF((lp, "rate limited pdu\n"));
1929 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1930 error = lacp_xmit_lacpdu(lp);
1932 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1936 lp->lp_flags &= ~LACP_PORT_NTT;
1938 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1944 lacp_sm_assert_ntt(struct lacp_port *lp)
1947 lp->lp_flags |= LACP_PORT_NTT;
1951 lacp_run_timers(struct lacp_port *lp)
1955 for (i = 0; i < LACP_NTIMER; i++) {
1956 KASSERT(lp->lp_timer[i] >= 0,
1957 ("invalid timer value %d", lp->lp_timer[i]));
1958 if (lp->lp_timer[i] == 0) {
1960 } else if (--lp->lp_timer[i] <= 0) {
1961 if (lacp_timer_funcs[i]) {
1962 (*lacp_timer_funcs[i])(lp);
1969 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1971 struct lacp_softc *lsc = lp->lp_lsc;
1972 struct lagg_port *lgp = lp->lp_lagg;
1973 struct lacp_port *lp2;
1974 struct markerdu *mdu;
1978 if (m->m_pkthdr.len != sizeof(*mdu)) {
1982 if ((m->m_flags & M_MCAST) == 0) {
1986 if (m->m_len < sizeof(*mdu)) {
1987 m = m_pullup(m, sizeof(*mdu));
1993 mdu = mtod(m, struct markerdu *);
1995 if (memcmp(&mdu->mdu_eh.ether_dhost,
1996 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
2000 if (mdu->mdu_sph.sph_version != 1) {
2004 switch (mdu->mdu_tlv.tlv_type) {
2005 case MARKER_TYPE_INFO:
2006 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2007 marker_info_tlv_template, TRUE)) {
2010 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
2011 memcpy(&mdu->mdu_eh.ether_dhost,
2012 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
2013 memcpy(&mdu->mdu_eh.ether_shost,
2014 lgp->lp_lladdr, ETHER_ADDR_LEN);
2015 error = lagg_enqueue(lp->lp_ifp, m);
2018 case MARKER_TYPE_RESPONSE:
2019 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2020 marker_response_tlv_template, TRUE)) {
2023 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
2024 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
2025 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
2027 /* Verify that it is the last marker we sent out */
2028 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
2029 sizeof(struct lacp_markerinfo)))
2033 lp->lp_flags &= ~LACP_PORT_MARK;
2035 if (lsc->lsc_suppress_distributing) {
2036 /* Check if any ports are waiting for a response */
2037 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
2038 if (lp2->lp_flags & LACP_PORT_MARK) {
2045 /* All interface queues are clear */
2046 LACP_DPRINTF((NULL, "queue flush complete\n"));
2047 lsc->lsc_suppress_distributing = FALSE;
2061 LACP_DPRINTF((lp, "bad marker frame\n"));
2067 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
2068 const struct tlv_template *tmpl, boolean_t check_type)
2070 while (/* CONSTCOND */ 1) {
2071 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
2074 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
2075 tlv->tlv_length != tmpl->tmpl_length) {
2078 if (tmpl->tmpl_type == 0) {
2081 tlv = (const struct tlvhdr *)
2082 ((const char *)tlv + tlv->tlv_length);
2091 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
2093 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2105 lacp_format_systemid(const struct lacp_systemid *sysid,
2106 char *buf, size_t buflen)
2108 char macbuf[LACP_MACSTR_MAX+1];
2110 snprintf(buf, buflen, "%04X,%s",
2111 ntohs(sysid->lsi_prio),
2112 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2118 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2120 snprintf(buf, buflen, "%04X,%04X",
2121 ntohs(portid->lpi_prio),
2122 ntohs(portid->lpi_portno));
2128 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2130 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2131 char portid[LACP_PORTIDSTR_MAX+1];
2133 snprintf(buf, buflen, "(%s,%04X,%s)",
2134 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2135 ntohs(peer->lip_key),
2136 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2142 lacp_format_lagid(const struct lacp_peerinfo *a,
2143 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2145 char astr[LACP_PARTNERSTR_MAX+1];
2146 char bstr[LACP_PARTNERSTR_MAX+1];
2150 * there's a convention to display small numbered peer
2154 if (lacp_compare_peerinfo(a, b) > 0) {
2155 const struct lacp_peerinfo *t;
2163 snprintf(buf, buflen, "[%s,%s]",
2164 lacp_format_partner(a, astr, sizeof(astr)),
2165 lacp_format_partner(b, bstr, sizeof(bstr)));
2171 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2172 char *buf, size_t buflen)
2178 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2182 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2184 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2189 lacp_dump_lacpdu(const struct lacpdu *du)
2191 char buf[LACP_PARTNERSTR_MAX+1];
2192 char buf2[LACP_STATESTR_MAX+1];
2194 printf("actor=%s\n",
2195 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2196 printf("actor.state=%s\n",
2197 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2198 printf("partner=%s\n",
2199 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2200 printf("partner.state=%s\n",
2201 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2203 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2207 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2212 printf("%s: ", lp->lp_ifp->if_xname);