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_kern_tls.h"
36 #include "opt_ratelimit.h"
38 #include <sys/param.h>
39 #include <sys/callout.h>
40 #include <sys/eventhandler.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h> /* hz */
45 #include <sys/socket.h> /* for net/if.h */
46 #include <sys/sockio.h>
47 #include <sys/sysctl.h>
48 #include <machine/stdarg.h>
50 #include <sys/rwlock.h>
51 #include <sys/taskqueue.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/ethernet.h>
58 #include <net/infiniband.h>
59 #include <net/if_media.h>
60 #include <net/if_types.h>
62 #include <net/if_lagg.h>
63 #include <net/ieee8023ad_lacp.h>
66 * actor system priority and port priority.
67 * XXX should be configurable.
70 #define LACP_SYSTEM_PRIO 0x8000
71 #define LACP_PORT_PRIO 0x8000
73 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
74 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
76 static const struct tlv_template lacp_info_tlv_template[] = {
77 { LACP_TYPE_ACTORINFO,
78 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
79 { LACP_TYPE_PARTNERINFO,
80 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
81 { LACP_TYPE_COLLECTORINFO,
82 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
86 static const struct tlv_template marker_info_tlv_template[] = {
88 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
92 static const struct tlv_template marker_response_tlv_template[] = {
93 { MARKER_TYPE_RESPONSE,
94 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
98 typedef void (*lacp_timer_func_t)(struct lacp_port *);
100 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
101 static void lacp_fill_markerinfo(struct lacp_port *,
102 struct lacp_markerinfo *);
104 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
105 static void lacp_suppress_distributing(struct lacp_softc *,
106 struct lacp_aggregator *);
107 static void lacp_transit_expire(void *);
108 static void lacp_update_portmap(struct lacp_softc *);
109 static void lacp_select_active_aggregator(struct lacp_softc *);
110 static uint16_t lacp_compose_key(struct lacp_port *);
111 static int tlv_check(const void *, size_t, const struct tlvhdr *,
112 const struct tlv_template *, boolean_t);
113 static void lacp_tick(void *);
115 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
116 const struct lacp_port *);
117 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
118 const struct lacp_peerinfo *);
119 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
120 const struct lacp_port *);
121 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
122 const struct lacp_peerinfo *);
124 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
126 static void lacp_aggregator_addref(struct lacp_softc *,
127 struct lacp_aggregator *);
128 static void lacp_aggregator_delref(struct lacp_softc *,
129 struct lacp_aggregator *);
131 /* receive machine */
133 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
134 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
135 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
136 static void lacp_sm_rx_timer(struct lacp_port *);
137 static void lacp_sm_rx_set_expired(struct lacp_port *);
138 static void lacp_sm_rx_update_ntt(struct lacp_port *,
139 const struct lacpdu *);
140 static void lacp_sm_rx_record_pdu(struct lacp_port *,
141 const struct lacpdu *);
142 static void lacp_sm_rx_update_selected(struct lacp_port *,
143 const struct lacpdu *);
144 static void lacp_sm_rx_record_default(struct lacp_port *);
145 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
146 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
147 const struct lacp_peerinfo *);
151 static void lacp_sm_mux(struct lacp_port *);
152 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
153 static void lacp_sm_mux_timer(struct lacp_port *);
155 /* periodic transmit machine */
157 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
158 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
159 static void lacp_sm_ptx_timer(struct lacp_port *);
161 /* transmit machine */
163 static void lacp_sm_tx(struct lacp_port *);
164 static void lacp_sm_assert_ntt(struct lacp_port *);
166 static void lacp_run_timers(struct lacp_port *);
167 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
168 const struct lacp_peerinfo *);
169 static int lacp_compare_systemid(const struct lacp_systemid *,
170 const struct lacp_systemid *);
171 static void lacp_port_enable(struct lacp_port *);
172 static void lacp_port_disable(struct lacp_port *);
173 static void lacp_select(struct lacp_port *);
174 static void lacp_unselect(struct lacp_port *);
175 static void lacp_disable_collecting(struct lacp_port *);
176 static void lacp_enable_collecting(struct lacp_port *);
177 static void lacp_disable_distributing(struct lacp_port *);
178 static void lacp_enable_distributing(struct lacp_port *);
179 static int lacp_xmit_lacpdu(struct lacp_port *);
180 static int lacp_xmit_marker(struct lacp_port *);
184 static void lacp_dump_lacpdu(const struct lacpdu *);
185 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
187 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
188 const struct lacp_peerinfo *, char *, size_t);
189 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
191 static const char *lacp_format_state(uint8_t, char *, size_t);
192 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
193 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
195 static const char *lacp_format_portid(const struct lacp_portid *, char *,
197 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
198 __attribute__((__format__(__printf__, 2, 3)));
200 VNET_DEFINE_STATIC(int, lacp_debug);
201 #define V_lacp_debug VNET(lacp_debug)
202 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
204 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
205 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
207 VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
208 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
209 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
210 "LACP strict protocol compliance default");
211 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
212 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
213 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
216 * partner administration variables.
217 * XXX should be configurable.
220 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
221 .lip_systemid = { .lsi_prio = 0xffff },
222 .lip_portid = { .lpi_prio = 0xffff },
223 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
224 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
227 static const struct lacp_peerinfo lacp_partner_admin_strict = {
228 .lip_systemid = { .lsi_prio = 0xffff },
229 .lip_portid = { .lpi_prio = 0xffff },
233 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
234 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
235 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
236 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
240 lacp_input(struct lagg_port *lgp, struct mbuf *m)
242 struct lacp_port *lp = LACP_PORT(lgp);
245 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
250 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
252 case SLOWPROTOCOLS_SUBTYPE_LACP:
253 lacp_pdu_input(lp, m);
256 case SLOWPROTOCOLS_SUBTYPE_MARKER:
257 lacp_marker_input(lp, m);
261 /* Not a subtype we are interested in */
266 * lacp_pdu_input: process lacpdu
269 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
271 struct lacp_softc *lsc = lp->lp_lsc;
275 if (m->m_pkthdr.len != sizeof(*du)) {
279 if ((m->m_flags & M_MCAST) == 0) {
283 if (m->m_len < sizeof(*du)) {
284 m = m_pullup(m, sizeof(*du));
290 du = mtod(m, struct lacpdu *);
292 if (memcmp(&du->ldu_eh.ether_dhost,
293 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
298 * ignore the version for compatibility with
299 * the future protocol revisions.
302 if (du->ldu_sph.sph_version != 1) {
308 * ignore tlv types for compatibility with
309 * the future protocol revisions.
311 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
312 lacp_info_tlv_template, FALSE)) {
316 if (V_lacp_debug > 0) {
317 lacp_dprintf(lp, "lacpdu receive\n");
318 lacp_dump_lacpdu(du);
321 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
322 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
339 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
341 struct lagg_port *lgp = lp->lp_lagg;
342 struct lagg_softc *sc = lgp->lp_softc;
344 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
345 memcpy(&info->lip_systemid.lsi_mac,
346 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
347 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
348 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
349 info->lip_state = lp->lp_state;
353 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
355 struct ifnet *ifp = lp->lp_ifp;
357 /* Fill in the port index and system id (encoded as the MAC) */
358 info->mi_rq_port = htons(ifp->if_index);
359 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
360 info->mi_rq_xid = htonl(0);
364 lacp_xmit_lacpdu(struct lacp_port *lp)
366 struct lagg_port *lgp = lp->lp_lagg;
371 LACP_LOCK_ASSERT(lp->lp_lsc);
373 m = m_gethdr(M_NOWAIT, MT_DATA);
377 m->m_len = m->m_pkthdr.len = sizeof(*du);
379 du = mtod(m, struct lacpdu *);
380 memset(du, 0, sizeof(*du));
382 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
384 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
385 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
387 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
388 du->ldu_sph.sph_version = 1;
390 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
391 du->ldu_actor = lp->lp_actor;
393 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
394 sizeof(du->ldu_partner));
395 du->ldu_partner = lp->lp_partner;
397 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
398 sizeof(du->ldu_collector));
399 du->ldu_collector.lci_maxdelay = 0;
401 if (V_lacp_debug > 0) {
402 lacp_dprintf(lp, "lacpdu transmit\n");
403 lacp_dump_lacpdu(du);
406 m->m_flags |= M_MCAST;
409 * XXX should use higher priority queue.
410 * otherwise network congestion can break aggregation.
413 error = lagg_enqueue(lp->lp_ifp, m);
418 lacp_xmit_marker(struct lacp_port *lp)
420 struct lagg_port *lgp = lp->lp_lagg;
422 struct markerdu *mdu;
425 LACP_LOCK_ASSERT(lp->lp_lsc);
427 m = m_gethdr(M_NOWAIT, MT_DATA);
431 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
433 mdu = mtod(m, struct markerdu *);
434 memset(mdu, 0, sizeof(*mdu));
436 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
438 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
439 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
441 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
442 mdu->mdu_sph.sph_version = 1;
444 /* Bump the transaction id and copy over the marker info */
445 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
446 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
447 mdu->mdu_info = lp->lp_marker;
449 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
450 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
451 ntohl(mdu->mdu_info.mi_rq_xid)));
453 m->m_flags |= M_MCAST;
454 error = lagg_enqueue(lp->lp_ifp, m);
459 lacp_linkstate(struct lagg_port *lgp)
461 struct lacp_port *lp = LACP_PORT(lgp);
462 struct lacp_softc *lsc = lp->lp_lsc;
463 struct ifnet *ifp = lgp->lp_ifp;
464 struct ifmediareq ifmr;
470 bzero((char *)&ifmr, sizeof(ifmr));
471 error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
473 bzero((char *)&ifmr, sizeof(ifmr));
474 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
480 media = ifmr.ifm_active;
481 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
482 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
483 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
484 old_state = lp->lp_state;
485 old_key = lp->lp_key;
487 lp->lp_media = media;
489 * If the port is not an active full duplex Ethernet link then it can
492 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
493 ifp->if_link_state != LINK_STATE_UP) {
494 lacp_port_disable(lp);
496 lacp_port_enable(lp);
498 lp->lp_key = lacp_compose_key(lp);
500 if (old_state != lp->lp_state || old_key != lp->lp_key) {
501 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
502 lp->lp_selected = LACP_UNSELECTED;
510 struct lacp_softc *lsc = arg;
511 struct lacp_port *lp;
513 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
514 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
517 CURVNET_SET(lp->lp_ifp->if_vnet);
523 lacp_sm_ptx_tx_schedule(lp);
526 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
530 lacp_port_create(struct lagg_port *lgp)
532 struct lagg_softc *sc = lgp->lp_softc;
533 struct lacp_softc *lsc = LACP_SOFTC(sc);
534 struct lacp_port *lp;
535 struct ifnet *ifp = lgp->lp_ifp;
536 struct sockaddr_dl sdl;
537 struct ifmultiaddr *rifma = NULL;
540 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
541 sdl.sdl_alen = ETHER_ADDR_LEN;
543 bcopy(ðermulticastaddr_slowprotocols,
544 LLADDR(&sdl), ETHER_ADDR_LEN);
545 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
547 printf("%s: ADDMULTI failed on %s\n", __func__,
548 lgp->lp_ifp->if_xname);
552 lp = malloc(sizeof(struct lacp_port),
553 M_DEVBUF, M_NOWAIT|M_ZERO);
564 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
566 lacp_fill_actorinfo(lp, &lp->lp_actor);
567 lacp_fill_markerinfo(lp, &lp->lp_marker);
568 lp->lp_state = LACP_STATE_ACTIVITY;
569 lp->lp_aggregator = NULL;
570 lacp_sm_rx_set_expired(lp);
578 lacp_port_destroy(struct lagg_port *lgp)
580 struct lacp_port *lp = LACP_PORT(lgp);
581 struct lacp_softc *lsc = lp->lp_lsc;
585 for (i = 0; i < LACP_NTIMER; i++) {
586 LACP_TIMER_DISARM(lp, i);
589 lacp_disable_collecting(lp);
590 lacp_disable_distributing(lp);
593 LIST_REMOVE(lp, lp_next);
596 /* The address may have already been removed by if_purgemaddrs() */
597 if (!lgp->lp_detaching)
598 if_delmulti_ifma(lp->lp_ifma);
604 lacp_req(struct lagg_softc *sc, void *data)
606 struct lacp_opreq *req = (struct lacp_opreq *)data;
607 struct lacp_softc *lsc = LACP_SOFTC(sc);
608 struct lacp_aggregator *la;
610 bzero(req, sizeof(struct lacp_opreq));
613 * If the LACP softc is NULL, return with the opreq structure full of
614 * zeros. It is normal for the softc to be NULL while the lagg is
620 la = lsc->lsc_active_aggregator;
623 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
624 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
626 req->actor_key = ntohs(la->la_actor.lip_key);
627 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
628 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
629 req->actor_state = la->la_actor.lip_state;
631 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
632 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
634 req->partner_key = ntohs(la->la_partner.lip_key);
635 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
636 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
637 req->partner_state = la->la_partner.lip_state;
643 lacp_portreq(struct lagg_port *lgp, void *data)
645 struct lacp_opreq *req = (struct lacp_opreq *)data;
646 struct lacp_port *lp = LACP_PORT(lgp);
647 struct lacp_softc *lsc = lp->lp_lsc;
650 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
651 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
653 req->actor_key = ntohs(lp->lp_actor.lip_key);
654 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
655 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
656 req->actor_state = lp->lp_actor.lip_state;
658 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
659 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
661 req->partner_key = ntohs(lp->lp_partner.lip_key);
662 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
663 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
664 req->partner_state = lp->lp_partner.lip_state;
669 lacp_disable_collecting(struct lacp_port *lp)
671 LACP_DPRINTF((lp, "collecting disabled\n"));
672 lp->lp_state &= ~LACP_STATE_COLLECTING;
676 lacp_enable_collecting(struct lacp_port *lp)
678 LACP_DPRINTF((lp, "collecting enabled\n"));
679 lp->lp_state |= LACP_STATE_COLLECTING;
683 lacp_disable_distributing(struct lacp_port *lp)
685 struct lacp_aggregator *la = lp->lp_aggregator;
686 struct lacp_softc *lsc = lp->lp_lsc;
687 struct lagg_softc *sc = lsc->lsc_softc;
688 char buf[LACP_LAGIDSTR_MAX+1];
690 LACP_LOCK_ASSERT(lsc);
692 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
696 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
697 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
698 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
700 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
702 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
703 la->la_nports, la->la_nports - 1));
705 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
707 sc->sc_active = la->la_nports;
709 if (lsc->lsc_active_aggregator == la) {
710 lacp_suppress_distributing(lsc, la);
711 lacp_select_active_aggregator(lsc);
712 /* regenerate the port map, the active aggregator has changed */
713 lacp_update_portmap(lsc);
716 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
717 if_link_state_change(sc->sc_ifp,
718 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
722 lacp_enable_distributing(struct lacp_port *lp)
724 struct lacp_aggregator *la = lp->lp_aggregator;
725 struct lacp_softc *lsc = lp->lp_lsc;
726 struct lagg_softc *sc = lsc->lsc_softc;
727 char buf[LACP_LAGIDSTR_MAX+1];
729 LACP_LOCK_ASSERT(lsc);
731 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
735 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
737 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
738 la->la_nports, la->la_nports + 1));
740 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
741 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
743 sc->sc_active = la->la_nports;
745 lp->lp_state |= LACP_STATE_DISTRIBUTING;
747 if (lsc->lsc_active_aggregator == la) {
748 lacp_suppress_distributing(lsc, la);
749 lacp_update_portmap(lsc);
751 /* try to become the active aggregator */
752 lacp_select_active_aggregator(lsc);
754 if_link_state_change(sc->sc_ifp,
755 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
759 lacp_transit_expire(void *vp)
761 struct lacp_softc *lsc = vp;
763 LACP_LOCK_ASSERT(lsc);
765 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
769 lsc->lsc_suppress_distributing = FALSE;
773 lacp_attach(struct lagg_softc *sc)
775 struct lacp_softc *lsc;
777 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
782 lsc->lsc_hashkey = m_ether_tcpip_hash_init();
783 lsc->lsc_active_aggregator = NULL;
784 lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
786 TAILQ_INIT(&lsc->lsc_aggregators);
787 LIST_INIT(&lsc->lsc_ports);
789 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
790 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
792 /* if the lagg is already up then do the same */
793 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
798 lacp_detach(void *psc)
800 struct lacp_softc *lsc = (struct lacp_softc *)psc;
802 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
803 ("aggregators still active"));
804 KASSERT(lsc->lsc_active_aggregator == NULL,
805 ("aggregator still attached"));
807 callout_drain(&lsc->lsc_transit_callout);
808 callout_drain(&lsc->lsc_callout);
810 LACP_LOCK_DESTROY(lsc);
815 lacp_init(struct lagg_softc *sc)
817 struct lacp_softc *lsc = LACP_SOFTC(sc);
820 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
825 lacp_stop(struct lagg_softc *sc)
827 struct lacp_softc *lsc = LACP_SOFTC(sc);
830 callout_stop(&lsc->lsc_transit_callout);
831 callout_stop(&lsc->lsc_callout);
836 lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t hash,
837 uint8_t numa_domain, int *err)
839 struct lacp_softc *lsc = LACP_SOFTC(sc);
840 struct lacp_portmap *pm;
841 struct lacp_port *lp;
842 struct lacp_port **map;
845 if (__predict_false(lsc->lsc_suppress_distributing)) {
846 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
851 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
852 if (pm->pm_count == 0) {
853 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
859 if ((sc->sc_opts & LAGG_OPT_USE_NUMA) &&
860 pm->pm_num_dom > 1 && numa_domain < MAXMEMDOM) {
861 count = pm->pm_numa[numa_domain].count;
863 map = pm->pm_numa[numa_domain].map;
865 /* No ports on this domain; use global hash. */
867 count = pm->pm_count;
873 count = pm->pm_count;
879 return (lp->lp_lagg);
883 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m, int *err)
885 struct lacp_softc *lsc = LACP_SOFTC(sc);
889 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
890 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
891 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
893 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
895 numa_domain = m->m_pkthdr.numa_domain;
896 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, err));
900 * lacp_suppress_distributing: drop transmit packets for a while
901 * to preserve packet ordering.
905 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
907 struct lacp_port *lp;
909 if (lsc->lsc_active_aggregator != la) {
915 lsc->lsc_suppress_distributing = TRUE;
917 /* send a marker frame down each port to verify the queues are empty */
918 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
919 lp->lp_flags |= LACP_PORT_MARK;
920 if (lacp_xmit_marker(lp) != 0)
921 lp->lp_flags &= ~LACP_PORT_MARK;
924 /* set a timeout for the marker frames */
925 callout_reset(&lsc->lsc_transit_callout,
926 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
930 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
931 const struct lacp_peerinfo *b)
933 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
937 lacp_compare_systemid(const struct lacp_systemid *a,
938 const struct lacp_systemid *b)
940 return (memcmp(a, b, sizeof(*a)));
945 lacp_compare_portid(const struct lacp_portid *a,
946 const struct lacp_portid *b)
948 return (memcmp(a, b, sizeof(*a)));
953 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
955 struct lacp_port *lp;
958 lp = TAILQ_FIRST(&la->la_ports);
963 speed = ifmedia_baudrate(lp->lp_media);
964 speed *= la->la_nports;
966 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
967 lp->lp_media, la->la_nports));
974 * lacp_select_active_aggregator: select an aggregator to be used to transmit
975 * packets from lagg(4) interface.
979 lacp_select_active_aggregator(struct lacp_softc *lsc)
981 struct lacp_aggregator *la;
982 struct lacp_aggregator *best_la = NULL;
983 uint64_t best_speed = 0;
984 char buf[LACP_LAGIDSTR_MAX+1];
988 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
991 if (la->la_nports == 0) {
995 speed = lacp_aggregator_bandwidth(la);
996 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
997 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
998 speed, la->la_nports));
1001 * This aggregator is chosen if the partner has a better
1002 * system priority or, the total aggregated speed is higher
1003 * or, it is already the chosen aggregator
1005 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1006 LACP_SYS_PRI(best_la->la_partner)) ||
1007 speed > best_speed ||
1008 (speed == best_speed &&
1009 la == lsc->lsc_active_aggregator)) {
1015 KASSERT(best_la == NULL || best_la->la_nports > 0,
1016 ("invalid aggregator refcnt"));
1017 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1018 ("invalid aggregator list"));
1020 if (lsc->lsc_active_aggregator != best_la) {
1021 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1022 LACP_DPRINTF((NULL, "old %s\n",
1023 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1024 buf, sizeof(buf))));
1026 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1028 LACP_DPRINTF((NULL, "new %s\n",
1029 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1031 if (lsc->lsc_active_aggregator != best_la) {
1032 lsc->lsc_active_aggregator = best_la;
1033 lacp_update_portmap(lsc);
1035 lacp_suppress_distributing(lsc, best_la);
1041 * Updated the inactive portmap array with the new list of ports and
1045 lacp_update_portmap(struct lacp_softc *lsc)
1047 struct lagg_softc *sc = lsc->lsc_softc;
1048 struct lacp_aggregator *la;
1049 struct lacp_portmap *p;
1050 struct lacp_port *lp;
1059 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1060 p = &lsc->lsc_pmap[newmap];
1061 la = lsc->lsc_active_aggregator;
1063 bzero(p, sizeof(struct lacp_portmap));
1065 if (la != NULL && la->la_nports > 0) {
1066 p->pm_count = la->la_nports;
1068 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) {
1069 p->pm_map[i++] = lp;
1071 domain = lp->lp_ifp->if_numa_domain;
1072 if (domain >= MAXMEMDOM)
1074 count = p->pm_numa[domain].count;
1075 p->pm_numa[domain].map[count] = lp;
1076 p->pm_numa[domain].count++;
1079 KASSERT(i == p->pm_count, ("Invalid port count"));
1082 for (i = 0; i < MAXMEMDOM; i++) {
1083 if (p->pm_numa[i].count != 0)
1087 speed = lacp_aggregator_bandwidth(la);
1089 sc->sc_ifp->if_baudrate = speed;
1090 EVENTHANDLER_INVOKE(ifnet_event, sc->sc_ifp,
1091 IFNET_EVENT_UPDATE_BAUDRATE);
1093 /* switch the active portmap over */
1094 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1095 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1097 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1101 lacp_compose_key(struct lacp_port *lp)
1103 struct lagg_port *lgp = lp->lp_lagg;
1104 struct lagg_softc *sc = lgp->lp_softc;
1105 u_int media = lp->lp_media;
1108 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1110 * non-aggregatable links should have unique keys.
1112 * XXX this isn't really unique as if_index is 16 bit.
1115 /* bit 0..14: (some bits of) if_index of this port */
1116 key = lp->lp_ifp->if_index;
1120 u_int subtype = IFM_SUBTYPE(media);
1122 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1123 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1125 /* bit 0..4: IFM_SUBTYPE modulo speed */
1148 case IFM_1000_SGMII:
1149 case IFM_1000_CX_SGMII:
1155 case IFM_10G_TWINAX:
1156 case IFM_10G_TWINAX_LONG:
1187 case IFM_50G_LAUI2_AC:
1189 case IFM_50G_AUI2_AC:
1195 case IFM_50G_KR_PAM4:
1196 case IFM_50G_AUI1_AC:
1225 case IFM_40G_XLAUI_AC:
1233 case IFM_100G_CAUI4_AC:
1234 case IFM_100G_CAUI4:
1235 case IFM_100G_AUI4_AC:
1237 case IFM_100G_CR_PAM4:
1238 case IFM_100G_KR_PAM4:
1242 case IFM_100G_KR2_PAM4:
1243 case IFM_100G_CAUI2_AC:
1244 case IFM_100G_CAUI2:
1245 case IFM_100G_AUI2_AC:
1249 case IFM_200G_CR4_PAM4:
1254 case IFM_200G_KR4_PAM4:
1255 case IFM_200G_AUI4_AC:
1257 case IFM_200G_AUI8_AC:
1259 key = IFM_200G_CR4_PAM4;
1264 case IFM_400G_AUI8_AC:
1272 /* bit 5..14: (some bits of) if_index of lagg device */
1273 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1276 return (htons(key));
1280 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1282 char buf[LACP_LAGIDSTR_MAX+1];
1284 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1286 lacp_format_lagid(&la->la_actor, &la->la_partner,
1288 la->la_refcnt, la->la_refcnt + 1));
1290 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1292 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1296 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1298 char buf[LACP_LAGIDSTR_MAX+1];
1300 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1302 lacp_format_lagid(&la->la_actor, &la->la_partner,
1304 la->la_refcnt, la->la_refcnt - 1));
1306 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1308 if (la->la_refcnt > 0) {
1312 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1313 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1315 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1321 * lacp_aggregator_get: allocate an aggregator.
1324 static struct lacp_aggregator *
1325 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1327 struct lacp_aggregator *la;
1329 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1333 TAILQ_INIT(&la->la_ports);
1335 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1342 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1346 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1348 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1349 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1351 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1355 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1356 const struct lacp_peerinfo *lpi_port)
1358 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1359 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1360 lpi_aggr->lip_key = lpi_port->lip_key;
1364 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1368 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1369 const struct lacp_port *lp)
1371 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1372 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1376 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1380 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1384 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1392 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1393 const struct lacp_peerinfo *b)
1395 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1396 sizeof(a->lip_systemid))) {
1400 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1408 lacp_port_enable(struct lacp_port *lp)
1410 lp->lp_state |= LACP_STATE_AGGREGATION;
1414 lacp_port_disable(struct lacp_port *lp)
1416 lacp_set_mux(lp, LACP_MUX_DETACHED);
1418 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1419 lp->lp_selected = LACP_UNSELECTED;
1420 lacp_sm_rx_record_default(lp);
1421 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1422 lp->lp_state &= ~LACP_STATE_EXPIRED;
1426 * lacp_select: select an aggregator. create one if necessary.
1429 lacp_select(struct lacp_port *lp)
1431 struct lacp_softc *lsc = lp->lp_lsc;
1432 struct lacp_aggregator *la;
1433 char buf[LACP_LAGIDSTR_MAX+1];
1435 if (lp->lp_aggregator) {
1439 /* If we haven't heard from our peer, skip this step. */
1440 if (lp->lp_state & LACP_STATE_DEFAULTED)
1443 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1444 ("timer_wait_while still active"));
1446 LACP_DPRINTF((lp, "port lagid=%s\n",
1447 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1448 buf, sizeof(buf))));
1450 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1451 if (lacp_aggregator_is_compatible(la, lp)) {
1457 la = lacp_aggregator_get(lsc, lp);
1459 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1462 * will retry on the next tick.
1467 lacp_fill_aggregator_id(la, lp);
1468 LACP_DPRINTF((lp, "aggregator created\n"));
1470 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1471 if (la->la_refcnt == LACP_MAX_PORTS)
1473 lacp_aggregator_addref(lsc, la);
1476 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1477 lacp_format_lagid(&la->la_actor, &la->la_partner,
1478 buf, sizeof(buf))));
1480 lp->lp_aggregator = la;
1481 lp->lp_selected = LACP_SELECTED;
1485 * lacp_unselect: finish unselect/detach process.
1489 lacp_unselect(struct lacp_port *lp)
1491 struct lacp_softc *lsc = lp->lp_lsc;
1492 struct lacp_aggregator *la = lp->lp_aggregator;
1494 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1495 ("timer_wait_while still active"));
1501 lp->lp_aggregator = NULL;
1502 lacp_aggregator_delref(lsc, la);
1508 lacp_sm_mux(struct lacp_port *lp)
1510 struct lagg_port *lgp = lp->lp_lagg;
1511 struct lagg_softc *sc = lgp->lp_softc;
1512 enum lacp_mux_state new_state;
1514 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1515 boolean_t p_collecting =
1516 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1517 enum lacp_selected selected = lp->lp_selected;
1518 struct lacp_aggregator *la;
1520 if (V_lacp_debug > 1)
1521 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1522 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1523 lp->lp_mux_state, selected, p_sync, p_collecting);
1526 la = lp->lp_aggregator;
1527 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1528 ("MUX not detached"));
1529 new_state = lp->lp_mux_state;
1530 switch (lp->lp_mux_state) {
1531 case LACP_MUX_DETACHED:
1532 if (selected != LACP_UNSELECTED) {
1533 new_state = LACP_MUX_WAITING;
1536 case LACP_MUX_WAITING:
1537 KASSERT(la->la_pending > 0 ||
1538 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1539 ("timer_wait_while still active"));
1540 if (selected == LACP_SELECTED && la->la_pending == 0) {
1541 new_state = LACP_MUX_ATTACHED;
1542 } else if (selected == LACP_UNSELECTED) {
1543 new_state = LACP_MUX_DETACHED;
1546 case LACP_MUX_ATTACHED:
1547 if (selected == LACP_SELECTED && p_sync) {
1548 new_state = LACP_MUX_COLLECTING;
1549 } else if (selected != LACP_SELECTED) {
1550 new_state = LACP_MUX_DETACHED;
1553 case LACP_MUX_COLLECTING:
1554 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1555 new_state = LACP_MUX_DISTRIBUTING;
1556 } else if (selected != LACP_SELECTED || !p_sync) {
1557 new_state = LACP_MUX_ATTACHED;
1560 case LACP_MUX_DISTRIBUTING:
1561 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1562 new_state = LACP_MUX_COLLECTING;
1563 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1568 panic("%s: unknown state", __func__);
1571 if (lp->lp_mux_state == new_state) {
1575 lacp_set_mux(lp, new_state);
1580 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1582 struct lacp_aggregator *la = lp->lp_aggregator;
1584 if (lp->lp_mux_state == new_state) {
1588 switch (new_state) {
1589 case LACP_MUX_DETACHED:
1590 lp->lp_state &= ~LACP_STATE_SYNC;
1591 lacp_disable_distributing(lp);
1592 lacp_disable_collecting(lp);
1593 lacp_sm_assert_ntt(lp);
1595 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1596 KASSERT(la->la_pending > 0,
1597 ("timer_wait_while not active"));
1600 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1603 case LACP_MUX_WAITING:
1604 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1605 LACP_AGGREGATE_WAIT_TIME);
1608 case LACP_MUX_ATTACHED:
1609 lp->lp_state |= LACP_STATE_SYNC;
1610 lacp_disable_collecting(lp);
1611 lacp_sm_assert_ntt(lp);
1613 case LACP_MUX_COLLECTING:
1614 lacp_enable_collecting(lp);
1615 lacp_disable_distributing(lp);
1616 lacp_sm_assert_ntt(lp);
1618 case LACP_MUX_DISTRIBUTING:
1619 lacp_enable_distributing(lp);
1622 panic("%s: unknown state", __func__);
1625 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1627 lp->lp_mux_state = new_state;
1631 lacp_sm_mux_timer(struct lacp_port *lp)
1633 struct lacp_aggregator *la = lp->lp_aggregator;
1634 char buf[LACP_LAGIDSTR_MAX+1];
1636 KASSERT(la->la_pending > 0, ("no pending event"));
1638 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1639 lacp_format_lagid(&la->la_actor, &la->la_partner,
1641 la->la_pending, la->la_pending - 1));
1646 /* periodic transmit machine */
1649 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1651 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1652 LACP_STATE_TIMEOUT)) {
1656 LACP_DPRINTF((lp, "partner timeout changed\n"));
1659 * FAST_PERIODIC -> SLOW_PERIODIC
1661 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1663 * let lacp_sm_ptx_tx_schedule to update timeout.
1666 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1669 * if timeout has been shortened, assert NTT.
1672 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1673 lacp_sm_assert_ntt(lp);
1678 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1682 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1683 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1688 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1692 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1696 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1697 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1699 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1703 lacp_sm_ptx_timer(struct lacp_port *lp)
1705 lacp_sm_assert_ntt(lp);
1709 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1714 * check LACP_DISABLED first
1717 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1722 * check loopback condition.
1725 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1726 &lp->lp_actor.lip_systemid)) {
1731 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1734 microuptime(&lp->lp_last_lacpdu_rx);
1735 lacp_sm_rx_update_selected(lp, du);
1736 lacp_sm_rx_update_ntt(lp, du);
1737 lacp_sm_rx_record_pdu(lp, du);
1739 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1740 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1741 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1743 lp->lp_state &= ~LACP_STATE_EXPIRED;
1746 * kick transmit machine without waiting the next tick.
1753 lacp_sm_rx_set_expired(struct lacp_port *lp)
1755 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1756 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1757 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1758 lp->lp_state |= LACP_STATE_EXPIRED;
1762 lacp_sm_rx_timer(struct lacp_port *lp)
1764 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1765 /* CURRENT -> EXPIRED */
1766 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1767 lacp_sm_rx_set_expired(lp);
1769 /* EXPIRED -> DEFAULTED */
1770 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1771 lacp_sm_rx_update_default_selected(lp);
1772 lacp_sm_rx_record_default(lp);
1773 lp->lp_state &= ~LACP_STATE_EXPIRED;
1778 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1782 char buf[LACP_STATESTR_MAX+1];
1786 oldpstate = lp->lp_partner.lip_state;
1788 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1789 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1790 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1792 lp->lp_partner = du->ldu_actor;
1794 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1795 LACP_STATE_AGGREGATION) &&
1796 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1797 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1799 * XXX Maintain legacy behavior of leaving the
1800 * LACP_STATE_SYNC bit unchanged from the partner's
1801 * advertisement if lsc_strict_mode is false.
1802 * TODO: We should re-examine the concept of the "strict mode"
1803 * to ensure it makes sense to maintain a non-strict mode.
1805 if (lp->lp_lsc->lsc_strict_mode)
1806 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1808 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1811 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1813 if (oldpstate != lp->lp_partner.lip_state) {
1814 LACP_DPRINTF((lp, "old pstate %s\n",
1815 lacp_format_state(oldpstate, buf, sizeof(buf))));
1816 LACP_DPRINTF((lp, "new pstate %s\n",
1817 lacp_format_state(lp->lp_partner.lip_state, buf,
1821 lacp_sm_ptx_update_timeout(lp, oldpstate);
1825 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1830 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1831 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1832 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1833 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1834 lacp_sm_assert_ntt(lp);
1839 lacp_sm_rx_record_default(struct lacp_port *lp)
1845 oldpstate = lp->lp_partner.lip_state;
1846 if (lp->lp_lsc->lsc_strict_mode)
1847 lp->lp_partner = lacp_partner_admin_strict;
1849 lp->lp_partner = lacp_partner_admin_optimistic;
1850 lp->lp_state |= LACP_STATE_DEFAULTED;
1851 lacp_sm_ptx_update_timeout(lp, oldpstate);
1855 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1856 const struct lacp_peerinfo *info)
1861 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1862 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1863 LACP_STATE_AGGREGATION)) {
1864 lp->lp_selected = LACP_UNSELECTED;
1865 /* mux machine will clean up lp->lp_aggregator */
1870 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1875 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1879 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1884 if (lp->lp_lsc->lsc_strict_mode)
1885 lacp_sm_rx_update_selected_from_peerinfo(lp,
1886 &lacp_partner_admin_strict);
1888 lacp_sm_rx_update_selected_from_peerinfo(lp,
1889 &lacp_partner_admin_optimistic);
1892 /* transmit machine */
1895 lacp_sm_tx(struct lacp_port *lp)
1899 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1901 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1902 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1905 lp->lp_flags &= ~LACP_PORT_NTT;
1908 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1912 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1913 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1914 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1915 LACP_DPRINTF((lp, "rate limited pdu\n"));
1919 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1920 error = lacp_xmit_lacpdu(lp);
1922 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1926 lp->lp_flags &= ~LACP_PORT_NTT;
1928 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1934 lacp_sm_assert_ntt(struct lacp_port *lp)
1937 lp->lp_flags |= LACP_PORT_NTT;
1941 lacp_run_timers(struct lacp_port *lp)
1944 struct timeval time_diff;
1946 for (i = 0; i < LACP_NTIMER; i++) {
1947 KASSERT(lp->lp_timer[i] >= 0,
1948 ("invalid timer value %d", lp->lp_timer[i]));
1949 if (lp->lp_timer[i] == 0) {
1952 if (i == LACP_TIMER_CURRENT_WHILE) {
1953 microuptime(&time_diff);
1954 timevalsub(&time_diff, &lp->lp_last_lacpdu_rx);
1955 if (time_diff.tv_sec) {
1956 /* At least one sec has elapsed since last LACP packet. */
1963 if ((lp->lp_timer[i] <= 0) && (lacp_timer_funcs[i])) {
1964 (*lacp_timer_funcs[i])(lp);
1971 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1973 struct lacp_softc *lsc = lp->lp_lsc;
1974 struct lagg_port *lgp = lp->lp_lagg;
1975 struct lacp_port *lp2;
1976 struct markerdu *mdu;
1980 if (m->m_pkthdr.len != sizeof(*mdu)) {
1984 if ((m->m_flags & M_MCAST) == 0) {
1988 if (m->m_len < sizeof(*mdu)) {
1989 m = m_pullup(m, sizeof(*mdu));
1995 mdu = mtod(m, struct markerdu *);
1997 if (memcmp(&mdu->mdu_eh.ether_dhost,
1998 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
2002 if (mdu->mdu_sph.sph_version != 1) {
2006 switch (mdu->mdu_tlv.tlv_type) {
2007 case MARKER_TYPE_INFO:
2008 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2009 marker_info_tlv_template, TRUE)) {
2012 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
2013 memcpy(&mdu->mdu_eh.ether_dhost,
2014 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
2015 memcpy(&mdu->mdu_eh.ether_shost,
2016 lgp->lp_lladdr, ETHER_ADDR_LEN);
2017 error = lagg_enqueue(lp->lp_ifp, m);
2020 case MARKER_TYPE_RESPONSE:
2021 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2022 marker_response_tlv_template, TRUE)) {
2025 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
2026 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
2027 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
2029 /* Verify that it is the last marker we sent out */
2030 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
2031 sizeof(struct lacp_markerinfo)))
2035 lp->lp_flags &= ~LACP_PORT_MARK;
2037 if (lsc->lsc_suppress_distributing) {
2038 /* Check if any ports are waiting for a response */
2039 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
2040 if (lp2->lp_flags & LACP_PORT_MARK) {
2047 /* All interface queues are clear */
2048 LACP_DPRINTF((NULL, "queue flush complete\n"));
2049 lsc->lsc_suppress_distributing = FALSE;
2063 LACP_DPRINTF((lp, "bad marker frame\n"));
2069 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
2070 const struct tlv_template *tmpl, boolean_t check_type)
2072 while (/* CONSTCOND */ 1) {
2073 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
2076 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
2077 tlv->tlv_length != tmpl->tmpl_length) {
2080 if (tmpl->tmpl_type == 0) {
2083 tlv = (const struct tlvhdr *)
2084 ((const char *)tlv + tlv->tlv_length);
2093 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
2095 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2107 lacp_format_systemid(const struct lacp_systemid *sysid,
2108 char *buf, size_t buflen)
2110 char macbuf[LACP_MACSTR_MAX+1];
2112 snprintf(buf, buflen, "%04X,%s",
2113 ntohs(sysid->lsi_prio),
2114 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2120 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2122 snprintf(buf, buflen, "%04X,%04X",
2123 ntohs(portid->lpi_prio),
2124 ntohs(portid->lpi_portno));
2130 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2132 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2133 char portid[LACP_PORTIDSTR_MAX+1];
2135 snprintf(buf, buflen, "(%s,%04X,%s)",
2136 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2137 ntohs(peer->lip_key),
2138 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2144 lacp_format_lagid(const struct lacp_peerinfo *a,
2145 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2147 char astr[LACP_PARTNERSTR_MAX+1];
2148 char bstr[LACP_PARTNERSTR_MAX+1];
2152 * there's a convention to display small numbered peer
2156 if (lacp_compare_peerinfo(a, b) > 0) {
2157 const struct lacp_peerinfo *t;
2165 snprintf(buf, buflen, "[%s,%s]",
2166 lacp_format_partner(a, astr, sizeof(astr)),
2167 lacp_format_partner(b, bstr, sizeof(bstr)));
2173 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2174 char *buf, size_t buflen)
2180 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2184 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2186 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2191 lacp_dump_lacpdu(const struct lacpdu *du)
2193 char buf[LACP_PARTNERSTR_MAX+1];
2194 char buf2[LACP_STATESTR_MAX+1];
2196 printf("actor=%s\n",
2197 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2198 printf("actor.state=%s\n",
2199 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2200 printf("partner=%s\n",
2201 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2202 printf("partner.state=%s\n",
2203 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2205 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2209 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2214 printf("%s: ", lp->lp_ifp->if_xname);