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>
54 #include <net/if_var.h>
55 #include <net/if_dl.h>
56 #include <net/ethernet.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
60 #include <net/if_lagg.h>
61 #include <net/ieee8023ad_lacp.h>
64 * actor system priority and port priority.
65 * XXX should be configurable.
68 #define LACP_SYSTEM_PRIO 0x8000
69 #define LACP_PORT_PRIO 0x8000
71 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
72 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
74 static const struct tlv_template lacp_info_tlv_template[] = {
75 { LACP_TYPE_ACTORINFO,
76 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
77 { LACP_TYPE_PARTNERINFO,
78 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
79 { LACP_TYPE_COLLECTORINFO,
80 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
84 static const struct tlv_template marker_info_tlv_template[] = {
86 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
90 static const struct tlv_template marker_response_tlv_template[] = {
91 { MARKER_TYPE_RESPONSE,
92 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
96 typedef void (*lacp_timer_func_t)(struct lacp_port *);
98 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
99 static void lacp_fill_markerinfo(struct lacp_port *,
100 struct lacp_markerinfo *);
102 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
103 static void lacp_suppress_distributing(struct lacp_softc *,
104 struct lacp_aggregator *);
105 static void lacp_transit_expire(void *);
106 static void lacp_update_portmap(struct lacp_softc *);
107 static void lacp_select_active_aggregator(struct lacp_softc *);
108 static uint16_t lacp_compose_key(struct lacp_port *);
109 static int tlv_check(const void *, size_t, const struct tlvhdr *,
110 const struct tlv_template *, boolean_t);
111 static void lacp_tick(void *);
113 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
114 const struct lacp_port *);
115 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
116 const struct lacp_peerinfo *);
117 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
118 const struct lacp_port *);
119 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
120 const struct lacp_peerinfo *);
122 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
124 static void lacp_aggregator_addref(struct lacp_softc *,
125 struct lacp_aggregator *);
126 static void lacp_aggregator_delref(struct lacp_softc *,
127 struct lacp_aggregator *);
129 /* receive machine */
131 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
132 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
133 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
134 static void lacp_sm_rx_timer(struct lacp_port *);
135 static void lacp_sm_rx_set_expired(struct lacp_port *);
136 static void lacp_sm_rx_update_ntt(struct lacp_port *,
137 const struct lacpdu *);
138 static void lacp_sm_rx_record_pdu(struct lacp_port *,
139 const struct lacpdu *);
140 static void lacp_sm_rx_update_selected(struct lacp_port *,
141 const struct lacpdu *);
142 static void lacp_sm_rx_record_default(struct lacp_port *);
143 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
144 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
145 const struct lacp_peerinfo *);
149 static void lacp_sm_mux(struct lacp_port *);
150 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
151 static void lacp_sm_mux_timer(struct lacp_port *);
153 /* periodic transmit machine */
155 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
156 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
157 static void lacp_sm_ptx_timer(struct lacp_port *);
159 /* transmit machine */
161 static void lacp_sm_tx(struct lacp_port *);
162 static void lacp_sm_assert_ntt(struct lacp_port *);
164 static void lacp_run_timers(struct lacp_port *);
165 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
166 const struct lacp_peerinfo *);
167 static int lacp_compare_systemid(const struct lacp_systemid *,
168 const struct lacp_systemid *);
169 static void lacp_port_enable(struct lacp_port *);
170 static void lacp_port_disable(struct lacp_port *);
171 static void lacp_select(struct lacp_port *);
172 static void lacp_unselect(struct lacp_port *);
173 static void lacp_disable_collecting(struct lacp_port *);
174 static void lacp_enable_collecting(struct lacp_port *);
175 static void lacp_disable_distributing(struct lacp_port *);
176 static void lacp_enable_distributing(struct lacp_port *);
177 static int lacp_xmit_lacpdu(struct lacp_port *);
178 static int lacp_xmit_marker(struct lacp_port *);
182 static void lacp_dump_lacpdu(const struct lacpdu *);
183 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
185 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
186 const struct lacp_peerinfo *, char *, size_t);
187 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
189 static const char *lacp_format_state(uint8_t, char *, size_t);
190 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
191 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
193 static const char *lacp_format_portid(const struct lacp_portid *, char *,
195 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
196 __attribute__((__format__(__printf__, 2, 3)));
198 VNET_DEFINE_STATIC(int, lacp_debug);
199 #define V_lacp_debug VNET(lacp_debug)
200 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
202 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
203 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
205 VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
206 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
207 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
208 "LACP strict protocol compliance default");
209 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
210 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
211 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
214 * partner administration variables.
215 * XXX should be configurable.
218 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
219 .lip_systemid = { .lsi_prio = 0xffff },
220 .lip_portid = { .lpi_prio = 0xffff },
221 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
222 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
225 static const struct lacp_peerinfo lacp_partner_admin_strict = {
226 .lip_systemid = { .lsi_prio = 0xffff },
227 .lip_portid = { .lpi_prio = 0xffff },
231 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
232 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
233 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
234 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
238 lacp_input(struct lagg_port *lgp, struct mbuf *m)
240 struct lacp_port *lp = LACP_PORT(lgp);
243 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
248 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
250 case SLOWPROTOCOLS_SUBTYPE_LACP:
251 lacp_pdu_input(lp, m);
254 case SLOWPROTOCOLS_SUBTYPE_MARKER:
255 lacp_marker_input(lp, m);
259 /* Not a subtype we are interested in */
264 * lacp_pdu_input: process lacpdu
267 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
269 struct lacp_softc *lsc = lp->lp_lsc;
273 if (m->m_pkthdr.len != sizeof(*du)) {
277 if ((m->m_flags & M_MCAST) == 0) {
281 if (m->m_len < sizeof(*du)) {
282 m = m_pullup(m, sizeof(*du));
288 du = mtod(m, struct lacpdu *);
290 if (memcmp(&du->ldu_eh.ether_dhost,
291 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
296 * ignore the version for compatibility with
297 * the future protocol revisions.
300 if (du->ldu_sph.sph_version != 1) {
306 * ignore tlv types for compatibility with
307 * the future protocol revisions.
309 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
310 lacp_info_tlv_template, FALSE)) {
314 if (V_lacp_debug > 0) {
315 lacp_dprintf(lp, "lacpdu receive\n");
316 lacp_dump_lacpdu(du);
319 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
320 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
337 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
339 struct lagg_port *lgp = lp->lp_lagg;
340 struct lagg_softc *sc = lgp->lp_softc;
342 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
343 memcpy(&info->lip_systemid.lsi_mac,
344 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
345 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
346 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
347 info->lip_state = lp->lp_state;
351 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
353 struct ifnet *ifp = lp->lp_ifp;
355 /* Fill in the port index and system id (encoded as the MAC) */
356 info->mi_rq_port = htons(ifp->if_index);
357 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
358 info->mi_rq_xid = htonl(0);
362 lacp_xmit_lacpdu(struct lacp_port *lp)
364 struct lagg_port *lgp = lp->lp_lagg;
369 LACP_LOCK_ASSERT(lp->lp_lsc);
371 m = m_gethdr(M_NOWAIT, MT_DATA);
375 m->m_len = m->m_pkthdr.len = sizeof(*du);
377 du = mtod(m, struct lacpdu *);
378 memset(du, 0, sizeof(*du));
380 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
382 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
383 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
385 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
386 du->ldu_sph.sph_version = 1;
388 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
389 du->ldu_actor = lp->lp_actor;
391 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
392 sizeof(du->ldu_partner));
393 du->ldu_partner = lp->lp_partner;
395 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
396 sizeof(du->ldu_collector));
397 du->ldu_collector.lci_maxdelay = 0;
399 if (V_lacp_debug > 0) {
400 lacp_dprintf(lp, "lacpdu transmit\n");
401 lacp_dump_lacpdu(du);
404 m->m_flags |= M_MCAST;
407 * XXX should use higher priority queue.
408 * otherwise network congestion can break aggregation.
411 error = lagg_enqueue(lp->lp_ifp, m);
416 lacp_xmit_marker(struct lacp_port *lp)
418 struct lagg_port *lgp = lp->lp_lagg;
420 struct markerdu *mdu;
423 LACP_LOCK_ASSERT(lp->lp_lsc);
425 m = m_gethdr(M_NOWAIT, MT_DATA);
429 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
431 mdu = mtod(m, struct markerdu *);
432 memset(mdu, 0, sizeof(*mdu));
434 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
436 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
437 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
439 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
440 mdu->mdu_sph.sph_version = 1;
442 /* Bump the transaction id and copy over the marker info */
443 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
444 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
445 mdu->mdu_info = lp->lp_marker;
447 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
448 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
449 ntohl(mdu->mdu_info.mi_rq_xid)));
451 m->m_flags |= M_MCAST;
452 error = lagg_enqueue(lp->lp_ifp, m);
457 lacp_linkstate(struct lagg_port *lgp)
459 struct lacp_port *lp = LACP_PORT(lgp);
460 struct lacp_softc *lsc = lp->lp_lsc;
461 struct ifnet *ifp = lgp->lp_ifp;
462 struct ifmediareq ifmr;
468 bzero((char *)&ifmr, sizeof(ifmr));
469 error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
471 bzero((char *)&ifmr, sizeof(ifmr));
472 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
478 media = ifmr.ifm_active;
479 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
480 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
481 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
482 old_state = lp->lp_state;
483 old_key = lp->lp_key;
485 lp->lp_media = media;
487 * If the port is not an active full duplex Ethernet link then it can
490 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
491 ifp->if_link_state != LINK_STATE_UP) {
492 lacp_port_disable(lp);
494 lacp_port_enable(lp);
496 lp->lp_key = lacp_compose_key(lp);
498 if (old_state != lp->lp_state || old_key != lp->lp_key) {
499 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
500 lp->lp_selected = LACP_UNSELECTED;
508 struct lacp_softc *lsc = arg;
509 struct lacp_port *lp;
511 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
512 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
515 CURVNET_SET(lp->lp_ifp->if_vnet);
521 lacp_sm_ptx_tx_schedule(lp);
524 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
528 lacp_port_create(struct lagg_port *lgp)
530 struct lagg_softc *sc = lgp->lp_softc;
531 struct lacp_softc *lsc = LACP_SOFTC(sc);
532 struct lacp_port *lp;
533 struct ifnet *ifp = lgp->lp_ifp;
534 struct sockaddr_dl sdl;
535 struct ifmultiaddr *rifma = NULL;
538 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
539 sdl.sdl_alen = ETHER_ADDR_LEN;
541 bcopy(ðermulticastaddr_slowprotocols,
542 LLADDR(&sdl), ETHER_ADDR_LEN);
543 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
545 printf("%s: ADDMULTI failed on %s\n", __func__,
546 lgp->lp_ifp->if_xname);
550 lp = malloc(sizeof(struct lacp_port),
551 M_DEVBUF, M_NOWAIT|M_ZERO);
562 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
564 lacp_fill_actorinfo(lp, &lp->lp_actor);
565 lacp_fill_markerinfo(lp, &lp->lp_marker);
566 lp->lp_state = LACP_STATE_ACTIVITY;
567 lp->lp_aggregator = NULL;
568 lacp_sm_rx_set_expired(lp);
576 lacp_port_destroy(struct lagg_port *lgp)
578 struct lacp_port *lp = LACP_PORT(lgp);
579 struct lacp_softc *lsc = lp->lp_lsc;
583 for (i = 0; i < LACP_NTIMER; i++) {
584 LACP_TIMER_DISARM(lp, i);
587 lacp_disable_collecting(lp);
588 lacp_disable_distributing(lp);
591 LIST_REMOVE(lp, lp_next);
594 /* The address may have already been removed by if_purgemaddrs() */
595 if (!lgp->lp_detaching)
596 if_delmulti_ifma(lp->lp_ifma);
602 lacp_req(struct lagg_softc *sc, void *data)
604 struct lacp_opreq *req = (struct lacp_opreq *)data;
605 struct lacp_softc *lsc = LACP_SOFTC(sc);
606 struct lacp_aggregator *la;
608 bzero(req, sizeof(struct lacp_opreq));
611 * If the LACP softc is NULL, return with the opreq structure full of
612 * zeros. It is normal for the softc to be NULL while the lagg is
618 la = lsc->lsc_active_aggregator;
621 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
622 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
624 req->actor_key = ntohs(la->la_actor.lip_key);
625 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
626 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
627 req->actor_state = la->la_actor.lip_state;
629 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
630 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
632 req->partner_key = ntohs(la->la_partner.lip_key);
633 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
634 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
635 req->partner_state = la->la_partner.lip_state;
641 lacp_portreq(struct lagg_port *lgp, void *data)
643 struct lacp_opreq *req = (struct lacp_opreq *)data;
644 struct lacp_port *lp = LACP_PORT(lgp);
645 struct lacp_softc *lsc = lp->lp_lsc;
648 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
649 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
651 req->actor_key = ntohs(lp->lp_actor.lip_key);
652 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
653 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
654 req->actor_state = lp->lp_actor.lip_state;
656 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
657 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
659 req->partner_key = ntohs(lp->lp_partner.lip_key);
660 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
661 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
662 req->partner_state = lp->lp_partner.lip_state;
667 lacp_disable_collecting(struct lacp_port *lp)
669 LACP_DPRINTF((lp, "collecting disabled\n"));
670 lp->lp_state &= ~LACP_STATE_COLLECTING;
674 lacp_enable_collecting(struct lacp_port *lp)
676 LACP_DPRINTF((lp, "collecting enabled\n"));
677 lp->lp_state |= LACP_STATE_COLLECTING;
681 lacp_disable_distributing(struct lacp_port *lp)
683 struct lacp_aggregator *la = lp->lp_aggregator;
684 struct lacp_softc *lsc = lp->lp_lsc;
685 struct lagg_softc *sc = lsc->lsc_softc;
686 char buf[LACP_LAGIDSTR_MAX+1];
688 LACP_LOCK_ASSERT(lsc);
690 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
694 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
695 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
696 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
698 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
700 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
701 la->la_nports, la->la_nports - 1));
703 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
705 sc->sc_active = la->la_nports;
707 if (lsc->lsc_active_aggregator == la) {
708 lacp_suppress_distributing(lsc, la);
709 lacp_select_active_aggregator(lsc);
710 /* regenerate the port map, the active aggregator has changed */
711 lacp_update_portmap(lsc);
714 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
715 if_link_state_change(sc->sc_ifp,
716 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
720 lacp_enable_distributing(struct lacp_port *lp)
722 struct lacp_aggregator *la = lp->lp_aggregator;
723 struct lacp_softc *lsc = lp->lp_lsc;
724 struct lagg_softc *sc = lsc->lsc_softc;
725 char buf[LACP_LAGIDSTR_MAX+1];
727 LACP_LOCK_ASSERT(lsc);
729 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
733 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
735 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
736 la->la_nports, la->la_nports + 1));
738 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
739 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
741 sc->sc_active = la->la_nports;
743 lp->lp_state |= LACP_STATE_DISTRIBUTING;
745 if (lsc->lsc_active_aggregator == la) {
746 lacp_suppress_distributing(lsc, la);
747 lacp_update_portmap(lsc);
749 /* try to become the active aggregator */
750 lacp_select_active_aggregator(lsc);
752 if_link_state_change(sc->sc_ifp,
753 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
757 lacp_transit_expire(void *vp)
759 struct lacp_softc *lsc = vp;
761 LACP_LOCK_ASSERT(lsc);
763 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
767 lsc->lsc_suppress_distributing = FALSE;
771 lacp_attach(struct lagg_softc *sc)
773 struct lacp_softc *lsc;
775 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
780 lsc->lsc_hashkey = m_ether_tcpip_hash_init();
781 lsc->lsc_active_aggregator = NULL;
782 lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
784 TAILQ_INIT(&lsc->lsc_aggregators);
785 LIST_INIT(&lsc->lsc_ports);
787 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
788 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
790 /* if the lagg is already up then do the same */
791 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
796 lacp_detach(void *psc)
798 struct lacp_softc *lsc = (struct lacp_softc *)psc;
800 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
801 ("aggregators still active"));
802 KASSERT(lsc->lsc_active_aggregator == NULL,
803 ("aggregator still attached"));
805 callout_drain(&lsc->lsc_transit_callout);
806 callout_drain(&lsc->lsc_callout);
808 LACP_LOCK_DESTROY(lsc);
813 lacp_init(struct lagg_softc *sc)
815 struct lacp_softc *lsc = LACP_SOFTC(sc);
818 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
823 lacp_stop(struct lagg_softc *sc)
825 struct lacp_softc *lsc = LACP_SOFTC(sc);
828 callout_stop(&lsc->lsc_transit_callout);
829 callout_stop(&lsc->lsc_callout);
834 lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t hash, uint8_t numa_domain)
836 struct lacp_softc *lsc = LACP_SOFTC(sc);
837 struct lacp_portmap *pm;
838 struct lacp_port *lp;
839 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 && numa_domain < MAXMEMDOM) {
856 count = pm->pm_numa[numa_domain].count;
858 map = pm->pm_numa[numa_domain].map;
860 /* No ports on this domain; use global hash. */
862 count = pm->pm_count;
868 count = pm->pm_count;
874 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
875 ("aggregated port is not distributing"));
877 return (lp->lp_lagg);
881 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
883 struct lacp_softc *lsc = LACP_SOFTC(sc);
887 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
888 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
889 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
891 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
893 numa_domain = m->m_pkthdr.numa_domain;
894 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain));
898 * lacp_suppress_distributing: drop transmit packets for a while
899 * to preserve packet ordering.
903 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
905 struct lacp_port *lp;
907 if (lsc->lsc_active_aggregator != la) {
913 lsc->lsc_suppress_distributing = TRUE;
915 /* send a marker frame down each port to verify the queues are empty */
916 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
917 lp->lp_flags |= LACP_PORT_MARK;
918 lacp_xmit_marker(lp);
921 /* set a timeout for the marker frames */
922 callout_reset(&lsc->lsc_transit_callout,
923 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
927 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
928 const struct lacp_peerinfo *b)
930 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
934 lacp_compare_systemid(const struct lacp_systemid *a,
935 const struct lacp_systemid *b)
937 return (memcmp(a, b, sizeof(*a)));
942 lacp_compare_portid(const struct lacp_portid *a,
943 const struct lacp_portid *b)
945 return (memcmp(a, b, sizeof(*a)));
950 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
952 struct lacp_port *lp;
955 lp = TAILQ_FIRST(&la->la_ports);
960 speed = ifmedia_baudrate(lp->lp_media);
961 speed *= la->la_nports;
963 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
964 lp->lp_media, la->la_nports));
971 * lacp_select_active_aggregator: select an aggregator to be used to transmit
972 * packets from lagg(4) interface.
976 lacp_select_active_aggregator(struct lacp_softc *lsc)
978 struct lacp_aggregator *la;
979 struct lacp_aggregator *best_la = NULL;
980 uint64_t best_speed = 0;
981 char buf[LACP_LAGIDSTR_MAX+1];
985 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
988 if (la->la_nports == 0) {
992 speed = lacp_aggregator_bandwidth(la);
993 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
994 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
995 speed, la->la_nports));
998 * This aggregator is chosen if the partner has a better
999 * system priority or, the total aggregated speed is higher
1000 * or, it is already the chosen aggregator
1002 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1003 LACP_SYS_PRI(best_la->la_partner)) ||
1004 speed > best_speed ||
1005 (speed == best_speed &&
1006 la == lsc->lsc_active_aggregator)) {
1012 KASSERT(best_la == NULL || best_la->la_nports > 0,
1013 ("invalid aggregator refcnt"));
1014 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1015 ("invalid aggregator list"));
1017 if (lsc->lsc_active_aggregator != best_la) {
1018 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1019 LACP_DPRINTF((NULL, "old %s\n",
1020 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1021 buf, sizeof(buf))));
1023 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1025 LACP_DPRINTF((NULL, "new %s\n",
1026 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1028 if (lsc->lsc_active_aggregator != best_la) {
1029 lsc->lsc_active_aggregator = best_la;
1030 lacp_update_portmap(lsc);
1032 lacp_suppress_distributing(lsc, best_la);
1038 * Updated the inactive portmap array with the new list of ports and
1042 lacp_update_portmap(struct lacp_softc *lsc)
1044 struct lagg_softc *sc = lsc->lsc_softc;
1045 struct lacp_aggregator *la;
1046 struct lacp_portmap *p;
1047 struct lacp_port *lp;
1056 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1057 p = &lsc->lsc_pmap[newmap];
1058 la = lsc->lsc_active_aggregator;
1060 bzero(p, sizeof(struct lacp_portmap));
1062 if (la != NULL && la->la_nports > 0) {
1063 p->pm_count = la->la_nports;
1065 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) {
1066 p->pm_map[i++] = lp;
1068 domain = lp->lp_ifp->if_numa_domain;
1069 if (domain >= MAXMEMDOM)
1071 count = p->pm_numa[domain].count;
1072 p->pm_numa[domain].map[count] = lp;
1073 p->pm_numa[domain].count++;
1076 KASSERT(i == p->pm_count, ("Invalid port count"));
1079 for (i = 0; i < MAXMEMDOM; i++) {
1080 if (p->pm_numa[i].count != 0)
1084 speed = lacp_aggregator_bandwidth(la);
1086 sc->sc_ifp->if_baudrate = speed;
1088 /* switch the active portmap over */
1089 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1090 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1092 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1096 lacp_compose_key(struct lacp_port *lp)
1098 struct lagg_port *lgp = lp->lp_lagg;
1099 struct lagg_softc *sc = lgp->lp_softc;
1100 u_int media = lp->lp_media;
1103 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1105 * non-aggregatable links should have unique keys.
1107 * XXX this isn't really unique as if_index is 16 bit.
1110 /* bit 0..14: (some bits of) if_index of this port */
1111 key = lp->lp_ifp->if_index;
1115 u_int subtype = IFM_SUBTYPE(media);
1117 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1118 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1120 /* bit 0..4: IFM_SUBTYPE modulo speed */
1143 case IFM_1000_SGMII:
1144 case IFM_1000_CX_SGMII:
1150 case IFM_10G_TWINAX:
1151 case IFM_10G_TWINAX_LONG:
1182 case IFM_50G_LAUI2_AC:
1184 case IFM_50G_AUI2_AC:
1190 case IFM_50G_KR_PAM4:
1191 case IFM_50G_AUI1_AC:
1219 case IFM_40G_XLAUI_AC:
1227 case IFM_100G_CAUI4_AC:
1228 case IFM_100G_CAUI4:
1229 case IFM_100G_AUI4_AC:
1231 case IFM_100G_CR_PAM4:
1232 case IFM_100G_KR_PAM4:
1236 case IFM_100G_KR2_PAM4:
1237 case IFM_100G_CAUI2_AC:
1238 case IFM_100G_CAUI2:
1239 case IFM_100G_AUI2_AC:
1243 case IFM_200G_CR4_PAM4:
1248 case IFM_200G_KR4_PAM4:
1249 case IFM_200G_AUI4_AC:
1251 case IFM_200G_AUI8_AC:
1253 key = IFM_200G_CR4_PAM4;
1258 case IFM_400G_AUI8_AC:
1266 /* bit 5..14: (some bits of) if_index of lagg device */
1267 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1270 return (htons(key));
1274 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1276 char buf[LACP_LAGIDSTR_MAX+1];
1278 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1280 lacp_format_lagid(&la->la_actor, &la->la_partner,
1282 la->la_refcnt, la->la_refcnt + 1));
1284 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1286 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1290 lacp_aggregator_delref(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 > la->la_nports, ("invalid refcnt"));
1302 if (la->la_refcnt > 0) {
1306 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1307 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1309 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1315 * lacp_aggregator_get: allocate an aggregator.
1318 static struct lacp_aggregator *
1319 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1321 struct lacp_aggregator *la;
1323 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1327 TAILQ_INIT(&la->la_ports);
1329 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1336 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1340 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1342 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1343 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1345 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1349 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1350 const struct lacp_peerinfo *lpi_port)
1352 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1353 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1354 lpi_aggr->lip_key = lpi_port->lip_key;
1358 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1362 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1363 const struct lacp_port *lp)
1365 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1366 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1370 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1374 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1378 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1386 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1387 const struct lacp_peerinfo *b)
1389 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1390 sizeof(a->lip_systemid))) {
1394 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1402 lacp_port_enable(struct lacp_port *lp)
1404 lp->lp_state |= LACP_STATE_AGGREGATION;
1408 lacp_port_disable(struct lacp_port *lp)
1410 lacp_set_mux(lp, LACP_MUX_DETACHED);
1412 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1413 lp->lp_selected = LACP_UNSELECTED;
1414 lacp_sm_rx_record_default(lp);
1415 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1416 lp->lp_state &= ~LACP_STATE_EXPIRED;
1420 * lacp_select: select an aggregator. create one if necessary.
1423 lacp_select(struct lacp_port *lp)
1425 struct lacp_softc *lsc = lp->lp_lsc;
1426 struct lacp_aggregator *la;
1427 char buf[LACP_LAGIDSTR_MAX+1];
1429 if (lp->lp_aggregator) {
1433 /* If we haven't heard from our peer, skip this step. */
1434 if (lp->lp_state & LACP_STATE_DEFAULTED)
1437 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1438 ("timer_wait_while still active"));
1440 LACP_DPRINTF((lp, "port lagid=%s\n",
1441 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1442 buf, sizeof(buf))));
1444 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1445 if (lacp_aggregator_is_compatible(la, lp)) {
1451 la = lacp_aggregator_get(lsc, lp);
1453 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1456 * will retry on the next tick.
1461 lacp_fill_aggregator_id(la, lp);
1462 LACP_DPRINTF((lp, "aggregator created\n"));
1464 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1465 if (la->la_refcnt == LACP_MAX_PORTS)
1467 lacp_aggregator_addref(lsc, la);
1470 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1471 lacp_format_lagid(&la->la_actor, &la->la_partner,
1472 buf, sizeof(buf))));
1474 lp->lp_aggregator = la;
1475 lp->lp_selected = LACP_SELECTED;
1479 * lacp_unselect: finish unselect/detach process.
1483 lacp_unselect(struct lacp_port *lp)
1485 struct lacp_softc *lsc = lp->lp_lsc;
1486 struct lacp_aggregator *la = lp->lp_aggregator;
1488 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1489 ("timer_wait_while still active"));
1495 lp->lp_aggregator = NULL;
1496 lacp_aggregator_delref(lsc, la);
1502 lacp_sm_mux(struct lacp_port *lp)
1504 struct lagg_port *lgp = lp->lp_lagg;
1505 struct lagg_softc *sc = lgp->lp_softc;
1506 enum lacp_mux_state new_state;
1508 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1509 boolean_t p_collecting =
1510 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1511 enum lacp_selected selected = lp->lp_selected;
1512 struct lacp_aggregator *la;
1514 if (V_lacp_debug > 1)
1515 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1516 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1517 lp->lp_mux_state, selected, p_sync, p_collecting);
1520 la = lp->lp_aggregator;
1521 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1522 ("MUX not detached"));
1523 new_state = lp->lp_mux_state;
1524 switch (lp->lp_mux_state) {
1525 case LACP_MUX_DETACHED:
1526 if (selected != LACP_UNSELECTED) {
1527 new_state = LACP_MUX_WAITING;
1530 case LACP_MUX_WAITING:
1531 KASSERT(la->la_pending > 0 ||
1532 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1533 ("timer_wait_while still active"));
1534 if (selected == LACP_SELECTED && la->la_pending == 0) {
1535 new_state = LACP_MUX_ATTACHED;
1536 } else if (selected == LACP_UNSELECTED) {
1537 new_state = LACP_MUX_DETACHED;
1540 case LACP_MUX_ATTACHED:
1541 if (selected == LACP_SELECTED && p_sync) {
1542 new_state = LACP_MUX_COLLECTING;
1543 } else if (selected != LACP_SELECTED) {
1544 new_state = LACP_MUX_DETACHED;
1547 case LACP_MUX_COLLECTING:
1548 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1549 new_state = LACP_MUX_DISTRIBUTING;
1550 } else if (selected != LACP_SELECTED || !p_sync) {
1551 new_state = LACP_MUX_ATTACHED;
1554 case LACP_MUX_DISTRIBUTING:
1555 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1556 new_state = LACP_MUX_COLLECTING;
1557 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1562 panic("%s: unknown state", __func__);
1565 if (lp->lp_mux_state == new_state) {
1569 lacp_set_mux(lp, new_state);
1574 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1576 struct lacp_aggregator *la = lp->lp_aggregator;
1578 if (lp->lp_mux_state == new_state) {
1582 switch (new_state) {
1583 case LACP_MUX_DETACHED:
1584 lp->lp_state &= ~LACP_STATE_SYNC;
1585 lacp_disable_distributing(lp);
1586 lacp_disable_collecting(lp);
1587 lacp_sm_assert_ntt(lp);
1589 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1590 KASSERT(la->la_pending > 0,
1591 ("timer_wait_while not active"));
1594 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1597 case LACP_MUX_WAITING:
1598 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1599 LACP_AGGREGATE_WAIT_TIME);
1602 case LACP_MUX_ATTACHED:
1603 lp->lp_state |= LACP_STATE_SYNC;
1604 lacp_disable_collecting(lp);
1605 lacp_sm_assert_ntt(lp);
1607 case LACP_MUX_COLLECTING:
1608 lacp_enable_collecting(lp);
1609 lacp_disable_distributing(lp);
1610 lacp_sm_assert_ntt(lp);
1612 case LACP_MUX_DISTRIBUTING:
1613 lacp_enable_distributing(lp);
1616 panic("%s: unknown state", __func__);
1619 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1621 lp->lp_mux_state = new_state;
1625 lacp_sm_mux_timer(struct lacp_port *lp)
1627 struct lacp_aggregator *la = lp->lp_aggregator;
1628 char buf[LACP_LAGIDSTR_MAX+1];
1630 KASSERT(la->la_pending > 0, ("no pending event"));
1632 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1633 lacp_format_lagid(&la->la_actor, &la->la_partner,
1635 la->la_pending, la->la_pending - 1));
1640 /* periodic transmit machine */
1643 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1645 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1646 LACP_STATE_TIMEOUT)) {
1650 LACP_DPRINTF((lp, "partner timeout changed\n"));
1653 * FAST_PERIODIC -> SLOW_PERIODIC
1655 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1657 * let lacp_sm_ptx_tx_schedule to update timeout.
1660 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1663 * if timeout has been shortened, assert NTT.
1666 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1667 lacp_sm_assert_ntt(lp);
1672 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1676 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1677 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1682 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1686 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1690 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1691 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1693 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1697 lacp_sm_ptx_timer(struct lacp_port *lp)
1699 lacp_sm_assert_ntt(lp);
1703 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1708 * check LACP_DISABLED first
1711 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1716 * check loopback condition.
1719 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1720 &lp->lp_actor.lip_systemid)) {
1725 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1728 lacp_sm_rx_update_selected(lp, du);
1729 lacp_sm_rx_update_ntt(lp, du);
1730 lacp_sm_rx_record_pdu(lp, du);
1732 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1733 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1734 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1736 lp->lp_state &= ~LACP_STATE_EXPIRED;
1739 * kick transmit machine without waiting the next tick.
1746 lacp_sm_rx_set_expired(struct lacp_port *lp)
1748 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1749 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1750 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1751 lp->lp_state |= LACP_STATE_EXPIRED;
1755 lacp_sm_rx_timer(struct lacp_port *lp)
1757 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1758 /* CURRENT -> EXPIRED */
1759 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1760 lacp_sm_rx_set_expired(lp);
1762 /* EXPIRED -> DEFAULTED */
1763 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1764 lacp_sm_rx_update_default_selected(lp);
1765 lacp_sm_rx_record_default(lp);
1766 lp->lp_state &= ~LACP_STATE_EXPIRED;
1771 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1775 char buf[LACP_STATESTR_MAX+1];
1779 oldpstate = lp->lp_partner.lip_state;
1781 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1782 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1783 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1785 lp->lp_partner = du->ldu_actor;
1787 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1788 LACP_STATE_AGGREGATION) &&
1789 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1790 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1792 * XXX Maintain legacy behavior of leaving the
1793 * LACP_STATE_SYNC bit unchanged from the partner's
1794 * advertisement if lsc_strict_mode is false.
1795 * TODO: We should re-examine the concept of the "strict mode"
1796 * to ensure it makes sense to maintain a non-strict mode.
1798 if (lp->lp_lsc->lsc_strict_mode)
1799 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1801 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1804 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1806 if (oldpstate != lp->lp_partner.lip_state) {
1807 LACP_DPRINTF((lp, "old pstate %s\n",
1808 lacp_format_state(oldpstate, buf, sizeof(buf))));
1809 LACP_DPRINTF((lp, "new pstate %s\n",
1810 lacp_format_state(lp->lp_partner.lip_state, buf,
1814 lacp_sm_ptx_update_timeout(lp, oldpstate);
1818 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1823 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1824 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1825 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1826 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1827 lacp_sm_assert_ntt(lp);
1832 lacp_sm_rx_record_default(struct lacp_port *lp)
1838 oldpstate = lp->lp_partner.lip_state;
1839 if (lp->lp_lsc->lsc_strict_mode)
1840 lp->lp_partner = lacp_partner_admin_strict;
1842 lp->lp_partner = lacp_partner_admin_optimistic;
1843 lp->lp_state |= LACP_STATE_DEFAULTED;
1844 lacp_sm_ptx_update_timeout(lp, oldpstate);
1848 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1849 const struct lacp_peerinfo *info)
1854 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1855 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1856 LACP_STATE_AGGREGATION)) {
1857 lp->lp_selected = LACP_UNSELECTED;
1858 /* mux machine will clean up lp->lp_aggregator */
1863 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1868 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1872 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1877 if (lp->lp_lsc->lsc_strict_mode)
1878 lacp_sm_rx_update_selected_from_peerinfo(lp,
1879 &lacp_partner_admin_strict);
1881 lacp_sm_rx_update_selected_from_peerinfo(lp,
1882 &lacp_partner_admin_optimistic);
1885 /* transmit machine */
1888 lacp_sm_tx(struct lacp_port *lp)
1892 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1894 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1895 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1898 lp->lp_flags &= ~LACP_PORT_NTT;
1901 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1905 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1906 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1907 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1908 LACP_DPRINTF((lp, "rate limited pdu\n"));
1912 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1913 error = lacp_xmit_lacpdu(lp);
1915 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1919 lp->lp_flags &= ~LACP_PORT_NTT;
1921 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1927 lacp_sm_assert_ntt(struct lacp_port *lp)
1930 lp->lp_flags |= LACP_PORT_NTT;
1934 lacp_run_timers(struct lacp_port *lp)
1938 for (i = 0; i < LACP_NTIMER; i++) {
1939 KASSERT(lp->lp_timer[i] >= 0,
1940 ("invalid timer value %d", lp->lp_timer[i]));
1941 if (lp->lp_timer[i] == 0) {
1943 } else if (--lp->lp_timer[i] <= 0) {
1944 if (lacp_timer_funcs[i]) {
1945 (*lacp_timer_funcs[i])(lp);
1952 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1954 struct lacp_softc *lsc = lp->lp_lsc;
1955 struct lagg_port *lgp = lp->lp_lagg;
1956 struct lacp_port *lp2;
1957 struct markerdu *mdu;
1961 if (m->m_pkthdr.len != sizeof(*mdu)) {
1965 if ((m->m_flags & M_MCAST) == 0) {
1969 if (m->m_len < sizeof(*mdu)) {
1970 m = m_pullup(m, sizeof(*mdu));
1976 mdu = mtod(m, struct markerdu *);
1978 if (memcmp(&mdu->mdu_eh.ether_dhost,
1979 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1983 if (mdu->mdu_sph.sph_version != 1) {
1987 switch (mdu->mdu_tlv.tlv_type) {
1988 case MARKER_TYPE_INFO:
1989 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1990 marker_info_tlv_template, TRUE)) {
1993 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1994 memcpy(&mdu->mdu_eh.ether_dhost,
1995 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1996 memcpy(&mdu->mdu_eh.ether_shost,
1997 lgp->lp_lladdr, ETHER_ADDR_LEN);
1998 error = lagg_enqueue(lp->lp_ifp, m);
2001 case MARKER_TYPE_RESPONSE:
2002 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2003 marker_response_tlv_template, TRUE)) {
2006 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
2007 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
2008 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
2010 /* Verify that it is the last marker we sent out */
2011 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
2012 sizeof(struct lacp_markerinfo)))
2016 lp->lp_flags &= ~LACP_PORT_MARK;
2018 if (lsc->lsc_suppress_distributing) {
2019 /* Check if any ports are waiting for a response */
2020 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
2021 if (lp2->lp_flags & LACP_PORT_MARK) {
2028 /* All interface queues are clear */
2029 LACP_DPRINTF((NULL, "queue flush complete\n"));
2030 lsc->lsc_suppress_distributing = FALSE;
2044 LACP_DPRINTF((lp, "bad marker frame\n"));
2050 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
2051 const struct tlv_template *tmpl, boolean_t check_type)
2053 while (/* CONSTCOND */ 1) {
2054 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
2057 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
2058 tlv->tlv_length != tmpl->tmpl_length) {
2061 if (tmpl->tmpl_type == 0) {
2064 tlv = (const struct tlvhdr *)
2065 ((const char *)tlv + tlv->tlv_length);
2074 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
2076 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2088 lacp_format_systemid(const struct lacp_systemid *sysid,
2089 char *buf, size_t buflen)
2091 char macbuf[LACP_MACSTR_MAX+1];
2093 snprintf(buf, buflen, "%04X,%s",
2094 ntohs(sysid->lsi_prio),
2095 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2101 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2103 snprintf(buf, buflen, "%04X,%04X",
2104 ntohs(portid->lpi_prio),
2105 ntohs(portid->lpi_portno));
2111 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2113 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2114 char portid[LACP_PORTIDSTR_MAX+1];
2116 snprintf(buf, buflen, "(%s,%04X,%s)",
2117 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2118 ntohs(peer->lip_key),
2119 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2125 lacp_format_lagid(const struct lacp_peerinfo *a,
2126 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2128 char astr[LACP_PARTNERSTR_MAX+1];
2129 char bstr[LACP_PARTNERSTR_MAX+1];
2133 * there's a convention to display small numbered peer
2137 if (lacp_compare_peerinfo(a, b) > 0) {
2138 const struct lacp_peerinfo *t;
2146 snprintf(buf, buflen, "[%s,%s]",
2147 lacp_format_partner(a, astr, sizeof(astr)),
2148 lacp_format_partner(b, bstr, sizeof(bstr)));
2154 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2155 char *buf, size_t buflen)
2161 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2165 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2167 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2172 lacp_dump_lacpdu(const struct lacpdu *du)
2174 char buf[LACP_PARTNERSTR_MAX+1];
2175 char buf2[LACP_STATESTR_MAX+1];
2177 printf("actor=%s\n",
2178 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2179 printf("actor.state=%s\n",
2180 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2181 printf("partner=%s\n",
2182 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2183 printf("partner.state=%s\n",
2184 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2186 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2190 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2195 printf("%s: ", lp->lp_ifp->if_xname);