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/infiniband.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
61 #include <net/if_lagg.h>
62 #include <net/ieee8023ad_lacp.h>
65 * actor system priority and port priority.
66 * XXX should be configurable.
69 #define LACP_SYSTEM_PRIO 0x8000
70 #define LACP_PORT_PRIO 0x8000
72 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
73 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
75 static const struct tlv_template lacp_info_tlv_template[] = {
76 { LACP_TYPE_ACTORINFO,
77 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
78 { LACP_TYPE_PARTNERINFO,
79 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
80 { LACP_TYPE_COLLECTORINFO,
81 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
85 static const struct tlv_template marker_info_tlv_template[] = {
87 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
91 static const struct tlv_template marker_response_tlv_template[] = {
92 { MARKER_TYPE_RESPONSE,
93 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
97 typedef void (*lacp_timer_func_t)(struct lacp_port *);
99 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
100 static void lacp_fill_markerinfo(struct lacp_port *,
101 struct lacp_markerinfo *);
103 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
104 static void lacp_suppress_distributing(struct lacp_softc *,
105 struct lacp_aggregator *);
106 static void lacp_transit_expire(void *);
107 static void lacp_update_portmap(struct lacp_softc *);
108 static void lacp_select_active_aggregator(struct lacp_softc *);
109 static uint16_t lacp_compose_key(struct lacp_port *);
110 static int tlv_check(const void *, size_t, const struct tlvhdr *,
111 const struct tlv_template *, boolean_t);
112 static void lacp_tick(void *);
114 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
115 const struct lacp_port *);
116 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
117 const struct lacp_peerinfo *);
118 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
119 const struct lacp_port *);
120 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
121 const struct lacp_peerinfo *);
123 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
125 static void lacp_aggregator_addref(struct lacp_softc *,
126 struct lacp_aggregator *);
127 static void lacp_aggregator_delref(struct lacp_softc *,
128 struct lacp_aggregator *);
130 /* receive machine */
132 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
133 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
134 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
135 static void lacp_sm_rx_timer(struct lacp_port *);
136 static void lacp_sm_rx_set_expired(struct lacp_port *);
137 static void lacp_sm_rx_update_ntt(struct lacp_port *,
138 const struct lacpdu *);
139 static void lacp_sm_rx_record_pdu(struct lacp_port *,
140 const struct lacpdu *);
141 static void lacp_sm_rx_update_selected(struct lacp_port *,
142 const struct lacpdu *);
143 static void lacp_sm_rx_record_default(struct lacp_port *);
144 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
145 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
146 const struct lacp_peerinfo *);
150 static void lacp_sm_mux(struct lacp_port *);
151 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
152 static void lacp_sm_mux_timer(struct lacp_port *);
154 /* periodic transmit machine */
156 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
157 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
158 static void lacp_sm_ptx_timer(struct lacp_port *);
160 /* transmit machine */
162 static void lacp_sm_tx(struct lacp_port *);
163 static void lacp_sm_assert_ntt(struct lacp_port *);
165 static void lacp_run_timers(struct lacp_port *);
166 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
167 const struct lacp_peerinfo *);
168 static int lacp_compare_systemid(const struct lacp_systemid *,
169 const struct lacp_systemid *);
170 static void lacp_port_enable(struct lacp_port *);
171 static void lacp_port_disable(struct lacp_port *);
172 static void lacp_select(struct lacp_port *);
173 static void lacp_unselect(struct lacp_port *);
174 static void lacp_disable_collecting(struct lacp_port *);
175 static void lacp_enable_collecting(struct lacp_port *);
176 static void lacp_disable_distributing(struct lacp_port *);
177 static void lacp_enable_distributing(struct lacp_port *);
178 static int lacp_xmit_lacpdu(struct lacp_port *);
179 static int lacp_xmit_marker(struct lacp_port *);
183 static void lacp_dump_lacpdu(const struct lacpdu *);
184 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
186 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
187 const struct lacp_peerinfo *, char *, size_t);
188 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
190 static const char *lacp_format_state(uint8_t, char *, size_t);
191 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
192 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
194 static const char *lacp_format_portid(const struct lacp_portid *, char *,
196 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
197 __attribute__((__format__(__printf__, 2, 3)));
199 VNET_DEFINE_STATIC(int, lacp_debug);
200 #define V_lacp_debug VNET(lacp_debug)
201 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
203 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
204 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
206 VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
207 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
208 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
209 "LACP strict protocol compliance default");
210 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
211 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
212 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
215 * partner administration variables.
216 * XXX should be configurable.
219 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
220 .lip_systemid = { .lsi_prio = 0xffff },
221 .lip_portid = { .lpi_prio = 0xffff },
222 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
223 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
226 static const struct lacp_peerinfo lacp_partner_admin_strict = {
227 .lip_systemid = { .lsi_prio = 0xffff },
228 .lip_portid = { .lpi_prio = 0xffff },
232 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
233 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
234 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
235 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
239 lacp_input(struct lagg_port *lgp, struct mbuf *m)
241 struct lacp_port *lp = LACP_PORT(lgp);
244 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
249 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
251 case SLOWPROTOCOLS_SUBTYPE_LACP:
252 lacp_pdu_input(lp, m);
255 case SLOWPROTOCOLS_SUBTYPE_MARKER:
256 lacp_marker_input(lp, m);
260 /* Not a subtype we are interested in */
265 * lacp_pdu_input: process lacpdu
268 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
270 struct lacp_softc *lsc = lp->lp_lsc;
274 if (m->m_pkthdr.len != sizeof(*du)) {
278 if ((m->m_flags & M_MCAST) == 0) {
282 if (m->m_len < sizeof(*du)) {
283 m = m_pullup(m, sizeof(*du));
289 du = mtod(m, struct lacpdu *);
291 if (memcmp(&du->ldu_eh.ether_dhost,
292 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
297 * ignore the version for compatibility with
298 * the future protocol revisions.
301 if (du->ldu_sph.sph_version != 1) {
307 * ignore tlv types for compatibility with
308 * the future protocol revisions.
310 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
311 lacp_info_tlv_template, FALSE)) {
315 if (V_lacp_debug > 0) {
316 lacp_dprintf(lp, "lacpdu receive\n");
317 lacp_dump_lacpdu(du);
320 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
321 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
338 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
340 struct lagg_port *lgp = lp->lp_lagg;
341 struct lagg_softc *sc = lgp->lp_softc;
343 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
344 memcpy(&info->lip_systemid.lsi_mac,
345 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
346 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
347 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
348 info->lip_state = lp->lp_state;
352 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
354 struct ifnet *ifp = lp->lp_ifp;
356 /* Fill in the port index and system id (encoded as the MAC) */
357 info->mi_rq_port = htons(ifp->if_index);
358 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
359 info->mi_rq_xid = htonl(0);
363 lacp_xmit_lacpdu(struct lacp_port *lp)
365 struct lagg_port *lgp = lp->lp_lagg;
370 LACP_LOCK_ASSERT(lp->lp_lsc);
372 m = m_gethdr(M_NOWAIT, MT_DATA);
376 m->m_len = m->m_pkthdr.len = sizeof(*du);
378 du = mtod(m, struct lacpdu *);
379 memset(du, 0, sizeof(*du));
381 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
383 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
384 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
386 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
387 du->ldu_sph.sph_version = 1;
389 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
390 du->ldu_actor = lp->lp_actor;
392 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
393 sizeof(du->ldu_partner));
394 du->ldu_partner = lp->lp_partner;
396 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
397 sizeof(du->ldu_collector));
398 du->ldu_collector.lci_maxdelay = 0;
400 if (V_lacp_debug > 0) {
401 lacp_dprintf(lp, "lacpdu transmit\n");
402 lacp_dump_lacpdu(du);
405 m->m_flags |= M_MCAST;
408 * XXX should use higher priority queue.
409 * otherwise network congestion can break aggregation.
412 error = lagg_enqueue(lp->lp_ifp, m);
417 lacp_xmit_marker(struct lacp_port *lp)
419 struct lagg_port *lgp = lp->lp_lagg;
421 struct markerdu *mdu;
424 LACP_LOCK_ASSERT(lp->lp_lsc);
426 m = m_gethdr(M_NOWAIT, MT_DATA);
430 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
432 mdu = mtod(m, struct markerdu *);
433 memset(mdu, 0, sizeof(*mdu));
435 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
437 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
438 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
440 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
441 mdu->mdu_sph.sph_version = 1;
443 /* Bump the transaction id and copy over the marker info */
444 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
445 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
446 mdu->mdu_info = lp->lp_marker;
448 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
449 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
450 ntohl(mdu->mdu_info.mi_rq_xid)));
452 m->m_flags |= M_MCAST;
453 error = lagg_enqueue(lp->lp_ifp, m);
458 lacp_linkstate(struct lagg_port *lgp)
460 struct lacp_port *lp = LACP_PORT(lgp);
461 struct lacp_softc *lsc = lp->lp_lsc;
462 struct ifnet *ifp = lgp->lp_ifp;
463 struct ifmediareq ifmr;
469 bzero((char *)&ifmr, sizeof(ifmr));
470 error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
472 bzero((char *)&ifmr, sizeof(ifmr));
473 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
479 media = ifmr.ifm_active;
480 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
481 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
482 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
483 old_state = lp->lp_state;
484 old_key = lp->lp_key;
486 lp->lp_media = media;
488 * If the port is not an active full duplex Ethernet link then it can
491 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
492 ifp->if_link_state != LINK_STATE_UP) {
493 lacp_port_disable(lp);
495 lacp_port_enable(lp);
497 lp->lp_key = lacp_compose_key(lp);
499 if (old_state != lp->lp_state || old_key != lp->lp_key) {
500 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
501 lp->lp_selected = LACP_UNSELECTED;
509 struct lacp_softc *lsc = arg;
510 struct lacp_port *lp;
512 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
513 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
516 CURVNET_SET(lp->lp_ifp->if_vnet);
522 lacp_sm_ptx_tx_schedule(lp);
525 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
529 lacp_port_create(struct lagg_port *lgp)
531 struct lagg_softc *sc = lgp->lp_softc;
532 struct lacp_softc *lsc = LACP_SOFTC(sc);
533 struct lacp_port *lp;
534 struct ifnet *ifp = lgp->lp_ifp;
535 struct sockaddr_dl sdl;
536 struct ifmultiaddr *rifma = NULL;
539 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
540 sdl.sdl_alen = ETHER_ADDR_LEN;
542 bcopy(ðermulticastaddr_slowprotocols,
543 LLADDR(&sdl), ETHER_ADDR_LEN);
544 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
546 printf("%s: ADDMULTI failed on %s\n", __func__,
547 lgp->lp_ifp->if_xname);
551 lp = malloc(sizeof(struct lacp_port),
552 M_DEVBUF, M_NOWAIT|M_ZERO);
563 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
565 lacp_fill_actorinfo(lp, &lp->lp_actor);
566 lacp_fill_markerinfo(lp, &lp->lp_marker);
567 lp->lp_state = LACP_STATE_ACTIVITY;
568 lp->lp_aggregator = NULL;
569 lacp_sm_rx_set_expired(lp);
577 lacp_port_destroy(struct lagg_port *lgp)
579 struct lacp_port *lp = LACP_PORT(lgp);
580 struct lacp_softc *lsc = lp->lp_lsc;
584 for (i = 0; i < LACP_NTIMER; i++) {
585 LACP_TIMER_DISARM(lp, i);
588 lacp_disable_collecting(lp);
589 lacp_disable_distributing(lp);
592 LIST_REMOVE(lp, lp_next);
595 /* The address may have already been removed by if_purgemaddrs() */
596 if (!lgp->lp_detaching)
597 if_delmulti_ifma(lp->lp_ifma);
603 lacp_req(struct lagg_softc *sc, void *data)
605 struct lacp_opreq *req = (struct lacp_opreq *)data;
606 struct lacp_softc *lsc = LACP_SOFTC(sc);
607 struct lacp_aggregator *la;
609 bzero(req, sizeof(struct lacp_opreq));
612 * If the LACP softc is NULL, return with the opreq structure full of
613 * zeros. It is normal for the softc to be NULL while the lagg is
619 la = lsc->lsc_active_aggregator;
622 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
623 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
625 req->actor_key = ntohs(la->la_actor.lip_key);
626 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
627 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
628 req->actor_state = la->la_actor.lip_state;
630 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
631 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
633 req->partner_key = ntohs(la->la_partner.lip_key);
634 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
635 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
636 req->partner_state = la->la_partner.lip_state;
642 lacp_portreq(struct lagg_port *lgp, void *data)
644 struct lacp_opreq *req = (struct lacp_opreq *)data;
645 struct lacp_port *lp = LACP_PORT(lgp);
646 struct lacp_softc *lsc = lp->lp_lsc;
649 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
650 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
652 req->actor_key = ntohs(lp->lp_actor.lip_key);
653 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
654 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
655 req->actor_state = lp->lp_actor.lip_state;
657 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
658 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
660 req->partner_key = ntohs(lp->lp_partner.lip_key);
661 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
662 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
663 req->partner_state = lp->lp_partner.lip_state;
668 lacp_disable_collecting(struct lacp_port *lp)
670 LACP_DPRINTF((lp, "collecting disabled\n"));
671 lp->lp_state &= ~LACP_STATE_COLLECTING;
675 lacp_enable_collecting(struct lacp_port *lp)
677 LACP_DPRINTF((lp, "collecting enabled\n"));
678 lp->lp_state |= LACP_STATE_COLLECTING;
682 lacp_disable_distributing(struct lacp_port *lp)
684 struct lacp_aggregator *la = lp->lp_aggregator;
685 struct lacp_softc *lsc = lp->lp_lsc;
686 struct lagg_softc *sc = lsc->lsc_softc;
687 char buf[LACP_LAGIDSTR_MAX+1];
689 LACP_LOCK_ASSERT(lsc);
691 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
695 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
696 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
697 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
699 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
701 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
702 la->la_nports, la->la_nports - 1));
704 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
706 sc->sc_active = la->la_nports;
708 if (lsc->lsc_active_aggregator == la) {
709 lacp_suppress_distributing(lsc, la);
710 lacp_select_active_aggregator(lsc);
711 /* regenerate the port map, the active aggregator has changed */
712 lacp_update_portmap(lsc);
715 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
716 if_link_state_change(sc->sc_ifp,
717 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
721 lacp_enable_distributing(struct lacp_port *lp)
723 struct lacp_aggregator *la = lp->lp_aggregator;
724 struct lacp_softc *lsc = lp->lp_lsc;
725 struct lagg_softc *sc = lsc->lsc_softc;
726 char buf[LACP_LAGIDSTR_MAX+1];
728 LACP_LOCK_ASSERT(lsc);
730 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
734 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
736 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
737 la->la_nports, la->la_nports + 1));
739 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
740 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
742 sc->sc_active = la->la_nports;
744 lp->lp_state |= LACP_STATE_DISTRIBUTING;
746 if (lsc->lsc_active_aggregator == la) {
747 lacp_suppress_distributing(lsc, la);
748 lacp_update_portmap(lsc);
750 /* try to become the active aggregator */
751 lacp_select_active_aggregator(lsc);
753 if_link_state_change(sc->sc_ifp,
754 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
758 lacp_transit_expire(void *vp)
760 struct lacp_softc *lsc = vp;
762 LACP_LOCK_ASSERT(lsc);
764 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
768 lsc->lsc_suppress_distributing = FALSE;
772 lacp_attach(struct lagg_softc *sc)
774 struct lacp_softc *lsc;
776 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
781 lsc->lsc_hashkey = m_ether_tcpip_hash_init();
782 lsc->lsc_active_aggregator = NULL;
783 lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
785 TAILQ_INIT(&lsc->lsc_aggregators);
786 LIST_INIT(&lsc->lsc_ports);
788 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
789 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
791 /* if the lagg is already up then do the same */
792 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
797 lacp_detach(void *psc)
799 struct lacp_softc *lsc = (struct lacp_softc *)psc;
801 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
802 ("aggregators still active"));
803 KASSERT(lsc->lsc_active_aggregator == NULL,
804 ("aggregator still attached"));
806 callout_drain(&lsc->lsc_transit_callout);
807 callout_drain(&lsc->lsc_callout);
809 LACP_LOCK_DESTROY(lsc);
814 lacp_init(struct lagg_softc *sc)
816 struct lacp_softc *lsc = LACP_SOFTC(sc);
819 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
824 lacp_stop(struct lagg_softc *sc)
826 struct lacp_softc *lsc = LACP_SOFTC(sc);
829 callout_stop(&lsc->lsc_transit_callout);
830 callout_stop(&lsc->lsc_callout);
835 lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t hash, uint8_t numa_domain)
837 struct lacp_softc *lsc = LACP_SOFTC(sc);
838 struct lacp_portmap *pm;
839 struct lacp_port *lp;
840 struct lacp_port **map;
843 if (__predict_false(lsc->lsc_suppress_distributing)) {
844 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
848 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
849 if (pm->pm_count == 0) {
850 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
855 if ((sc->sc_opts & LAGG_OPT_USE_NUMA) &&
856 pm->pm_num_dom > 1 && numa_domain < MAXMEMDOM) {
857 count = pm->pm_numa[numa_domain].count;
859 map = pm->pm_numa[numa_domain].map;
861 /* No ports on this domain; use global hash. */
863 count = pm->pm_count;
869 count = pm->pm_count;
875 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
876 ("aggregated port is not distributing"));
878 return (lp->lp_lagg);
882 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
884 struct lacp_softc *lsc = LACP_SOFTC(sc);
888 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
889 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
890 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
892 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
894 numa_domain = m->m_pkthdr.numa_domain;
895 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain));
899 * lacp_suppress_distributing: drop transmit packets for a while
900 * to preserve packet ordering.
904 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
906 struct lacp_port *lp;
908 if (lsc->lsc_active_aggregator != la) {
914 lsc->lsc_suppress_distributing = TRUE;
916 /* send a marker frame down each port to verify the queues are empty */
917 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
918 lp->lp_flags |= LACP_PORT_MARK;
919 lacp_xmit_marker(lp);
922 /* set a timeout for the marker frames */
923 callout_reset(&lsc->lsc_transit_callout,
924 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
928 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
929 const struct lacp_peerinfo *b)
931 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
935 lacp_compare_systemid(const struct lacp_systemid *a,
936 const struct lacp_systemid *b)
938 return (memcmp(a, b, sizeof(*a)));
943 lacp_compare_portid(const struct lacp_portid *a,
944 const struct lacp_portid *b)
946 return (memcmp(a, b, sizeof(*a)));
951 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
953 struct lacp_port *lp;
956 lp = TAILQ_FIRST(&la->la_ports);
961 speed = ifmedia_baudrate(lp->lp_media);
962 speed *= la->la_nports;
964 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
965 lp->lp_media, la->la_nports));
972 * lacp_select_active_aggregator: select an aggregator to be used to transmit
973 * packets from lagg(4) interface.
977 lacp_select_active_aggregator(struct lacp_softc *lsc)
979 struct lacp_aggregator *la;
980 struct lacp_aggregator *best_la = NULL;
981 uint64_t best_speed = 0;
982 char buf[LACP_LAGIDSTR_MAX+1];
986 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
989 if (la->la_nports == 0) {
993 speed = lacp_aggregator_bandwidth(la);
994 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
995 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
996 speed, la->la_nports));
999 * This aggregator is chosen if the partner has a better
1000 * system priority or, the total aggregated speed is higher
1001 * or, it is already the chosen aggregator
1003 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1004 LACP_SYS_PRI(best_la->la_partner)) ||
1005 speed > best_speed ||
1006 (speed == best_speed &&
1007 la == lsc->lsc_active_aggregator)) {
1013 KASSERT(best_la == NULL || best_la->la_nports > 0,
1014 ("invalid aggregator refcnt"));
1015 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1016 ("invalid aggregator list"));
1018 if (lsc->lsc_active_aggregator != best_la) {
1019 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1020 LACP_DPRINTF((NULL, "old %s\n",
1021 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1022 buf, sizeof(buf))));
1024 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1026 LACP_DPRINTF((NULL, "new %s\n",
1027 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1029 if (lsc->lsc_active_aggregator != best_la) {
1030 lsc->lsc_active_aggregator = best_la;
1031 lacp_update_portmap(lsc);
1033 lacp_suppress_distributing(lsc, best_la);
1039 * Updated the inactive portmap array with the new list of ports and
1043 lacp_update_portmap(struct lacp_softc *lsc)
1045 struct lagg_softc *sc = lsc->lsc_softc;
1046 struct lacp_aggregator *la;
1047 struct lacp_portmap *p;
1048 struct lacp_port *lp;
1057 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1058 p = &lsc->lsc_pmap[newmap];
1059 la = lsc->lsc_active_aggregator;
1061 bzero(p, sizeof(struct lacp_portmap));
1063 if (la != NULL && la->la_nports > 0) {
1064 p->pm_count = la->la_nports;
1066 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) {
1067 p->pm_map[i++] = lp;
1069 domain = lp->lp_ifp->if_numa_domain;
1070 if (domain >= MAXMEMDOM)
1072 count = p->pm_numa[domain].count;
1073 p->pm_numa[domain].map[count] = lp;
1074 p->pm_numa[domain].count++;
1077 KASSERT(i == p->pm_count, ("Invalid port count"));
1080 for (i = 0; i < MAXMEMDOM; i++) {
1081 if (p->pm_numa[i].count != 0)
1085 speed = lacp_aggregator_bandwidth(la);
1087 sc->sc_ifp->if_baudrate = speed;
1089 /* switch the active portmap over */
1090 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1091 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1093 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1097 lacp_compose_key(struct lacp_port *lp)
1099 struct lagg_port *lgp = lp->lp_lagg;
1100 struct lagg_softc *sc = lgp->lp_softc;
1101 u_int media = lp->lp_media;
1104 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1106 * non-aggregatable links should have unique keys.
1108 * XXX this isn't really unique as if_index is 16 bit.
1111 /* bit 0..14: (some bits of) if_index of this port */
1112 key = lp->lp_ifp->if_index;
1116 u_int subtype = IFM_SUBTYPE(media);
1118 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1119 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1121 /* bit 0..4: IFM_SUBTYPE modulo speed */
1144 case IFM_1000_SGMII:
1145 case IFM_1000_CX_SGMII:
1151 case IFM_10G_TWINAX:
1152 case IFM_10G_TWINAX_LONG:
1183 case IFM_50G_LAUI2_AC:
1185 case IFM_50G_AUI2_AC:
1191 case IFM_50G_KR_PAM4:
1192 case IFM_50G_AUI1_AC:
1221 case IFM_40G_XLAUI_AC:
1229 case IFM_100G_CAUI4_AC:
1230 case IFM_100G_CAUI4:
1231 case IFM_100G_AUI4_AC:
1233 case IFM_100G_CR_PAM4:
1234 case IFM_100G_KR_PAM4:
1238 case IFM_100G_KR2_PAM4:
1239 case IFM_100G_CAUI2_AC:
1240 case IFM_100G_CAUI2:
1241 case IFM_100G_AUI2_AC:
1245 case IFM_200G_CR4_PAM4:
1250 case IFM_200G_KR4_PAM4:
1251 case IFM_200G_AUI4_AC:
1253 case IFM_200G_AUI8_AC:
1255 key = IFM_200G_CR4_PAM4;
1260 case IFM_400G_AUI8_AC:
1268 /* bit 5..14: (some bits of) if_index of lagg device */
1269 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1272 return (htons(key));
1276 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1278 char buf[LACP_LAGIDSTR_MAX+1];
1280 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1282 lacp_format_lagid(&la->la_actor, &la->la_partner,
1284 la->la_refcnt, la->la_refcnt + 1));
1286 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1288 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1292 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1294 char buf[LACP_LAGIDSTR_MAX+1];
1296 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1298 lacp_format_lagid(&la->la_actor, &la->la_partner,
1300 la->la_refcnt, la->la_refcnt - 1));
1302 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1304 if (la->la_refcnt > 0) {
1308 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1309 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1311 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1317 * lacp_aggregator_get: allocate an aggregator.
1320 static struct lacp_aggregator *
1321 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1323 struct lacp_aggregator *la;
1325 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1329 TAILQ_INIT(&la->la_ports);
1331 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1338 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1342 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1344 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1345 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1347 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1351 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1352 const struct lacp_peerinfo *lpi_port)
1354 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1355 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1356 lpi_aggr->lip_key = lpi_port->lip_key;
1360 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1364 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1365 const struct lacp_port *lp)
1367 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1368 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1372 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1376 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1380 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1388 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1389 const struct lacp_peerinfo *b)
1391 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1392 sizeof(a->lip_systemid))) {
1396 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1404 lacp_port_enable(struct lacp_port *lp)
1406 lp->lp_state |= LACP_STATE_AGGREGATION;
1410 lacp_port_disable(struct lacp_port *lp)
1412 lacp_set_mux(lp, LACP_MUX_DETACHED);
1414 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1415 lp->lp_selected = LACP_UNSELECTED;
1416 lacp_sm_rx_record_default(lp);
1417 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1418 lp->lp_state &= ~LACP_STATE_EXPIRED;
1422 * lacp_select: select an aggregator. create one if necessary.
1425 lacp_select(struct lacp_port *lp)
1427 struct lacp_softc *lsc = lp->lp_lsc;
1428 struct lacp_aggregator *la;
1429 char buf[LACP_LAGIDSTR_MAX+1];
1431 if (lp->lp_aggregator) {
1435 /* If we haven't heard from our peer, skip this step. */
1436 if (lp->lp_state & LACP_STATE_DEFAULTED)
1439 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1440 ("timer_wait_while still active"));
1442 LACP_DPRINTF((lp, "port lagid=%s\n",
1443 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1444 buf, sizeof(buf))));
1446 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1447 if (lacp_aggregator_is_compatible(la, lp)) {
1453 la = lacp_aggregator_get(lsc, lp);
1455 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1458 * will retry on the next tick.
1463 lacp_fill_aggregator_id(la, lp);
1464 LACP_DPRINTF((lp, "aggregator created\n"));
1466 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1467 if (la->la_refcnt == LACP_MAX_PORTS)
1469 lacp_aggregator_addref(lsc, la);
1472 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1473 lacp_format_lagid(&la->la_actor, &la->la_partner,
1474 buf, sizeof(buf))));
1476 lp->lp_aggregator = la;
1477 lp->lp_selected = LACP_SELECTED;
1481 * lacp_unselect: finish unselect/detach process.
1485 lacp_unselect(struct lacp_port *lp)
1487 struct lacp_softc *lsc = lp->lp_lsc;
1488 struct lacp_aggregator *la = lp->lp_aggregator;
1490 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1491 ("timer_wait_while still active"));
1497 lp->lp_aggregator = NULL;
1498 lacp_aggregator_delref(lsc, la);
1504 lacp_sm_mux(struct lacp_port *lp)
1506 struct lagg_port *lgp = lp->lp_lagg;
1507 struct lagg_softc *sc = lgp->lp_softc;
1508 enum lacp_mux_state new_state;
1510 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1511 boolean_t p_collecting =
1512 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1513 enum lacp_selected selected = lp->lp_selected;
1514 struct lacp_aggregator *la;
1516 if (V_lacp_debug > 1)
1517 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1518 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1519 lp->lp_mux_state, selected, p_sync, p_collecting);
1522 la = lp->lp_aggregator;
1523 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1524 ("MUX not detached"));
1525 new_state = lp->lp_mux_state;
1526 switch (lp->lp_mux_state) {
1527 case LACP_MUX_DETACHED:
1528 if (selected != LACP_UNSELECTED) {
1529 new_state = LACP_MUX_WAITING;
1532 case LACP_MUX_WAITING:
1533 KASSERT(la->la_pending > 0 ||
1534 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1535 ("timer_wait_while still active"));
1536 if (selected == LACP_SELECTED && la->la_pending == 0) {
1537 new_state = LACP_MUX_ATTACHED;
1538 } else if (selected == LACP_UNSELECTED) {
1539 new_state = LACP_MUX_DETACHED;
1542 case LACP_MUX_ATTACHED:
1543 if (selected == LACP_SELECTED && p_sync) {
1544 new_state = LACP_MUX_COLLECTING;
1545 } else if (selected != LACP_SELECTED) {
1546 new_state = LACP_MUX_DETACHED;
1549 case LACP_MUX_COLLECTING:
1550 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1551 new_state = LACP_MUX_DISTRIBUTING;
1552 } else if (selected != LACP_SELECTED || !p_sync) {
1553 new_state = LACP_MUX_ATTACHED;
1556 case LACP_MUX_DISTRIBUTING:
1557 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1558 new_state = LACP_MUX_COLLECTING;
1559 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1564 panic("%s: unknown state", __func__);
1567 if (lp->lp_mux_state == new_state) {
1571 lacp_set_mux(lp, new_state);
1576 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1578 struct lacp_aggregator *la = lp->lp_aggregator;
1580 if (lp->lp_mux_state == new_state) {
1584 switch (new_state) {
1585 case LACP_MUX_DETACHED:
1586 lp->lp_state &= ~LACP_STATE_SYNC;
1587 lacp_disable_distributing(lp);
1588 lacp_disable_collecting(lp);
1589 lacp_sm_assert_ntt(lp);
1591 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1592 KASSERT(la->la_pending > 0,
1593 ("timer_wait_while not active"));
1596 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1599 case LACP_MUX_WAITING:
1600 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1601 LACP_AGGREGATE_WAIT_TIME);
1604 case LACP_MUX_ATTACHED:
1605 lp->lp_state |= LACP_STATE_SYNC;
1606 lacp_disable_collecting(lp);
1607 lacp_sm_assert_ntt(lp);
1609 case LACP_MUX_COLLECTING:
1610 lacp_enable_collecting(lp);
1611 lacp_disable_distributing(lp);
1612 lacp_sm_assert_ntt(lp);
1614 case LACP_MUX_DISTRIBUTING:
1615 lacp_enable_distributing(lp);
1618 panic("%s: unknown state", __func__);
1621 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1623 lp->lp_mux_state = new_state;
1627 lacp_sm_mux_timer(struct lacp_port *lp)
1629 struct lacp_aggregator *la = lp->lp_aggregator;
1630 char buf[LACP_LAGIDSTR_MAX+1];
1632 KASSERT(la->la_pending > 0, ("no pending event"));
1634 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1635 lacp_format_lagid(&la->la_actor, &la->la_partner,
1637 la->la_pending, la->la_pending - 1));
1642 /* periodic transmit machine */
1645 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1647 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1648 LACP_STATE_TIMEOUT)) {
1652 LACP_DPRINTF((lp, "partner timeout changed\n"));
1655 * FAST_PERIODIC -> SLOW_PERIODIC
1657 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1659 * let lacp_sm_ptx_tx_schedule to update timeout.
1662 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1665 * if timeout has been shortened, assert NTT.
1668 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1669 lacp_sm_assert_ntt(lp);
1674 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1678 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1679 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1684 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1688 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1692 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1693 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1695 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1699 lacp_sm_ptx_timer(struct lacp_port *lp)
1701 lacp_sm_assert_ntt(lp);
1705 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1710 * check LACP_DISABLED first
1713 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1718 * check loopback condition.
1721 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1722 &lp->lp_actor.lip_systemid)) {
1727 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1730 lacp_sm_rx_update_selected(lp, du);
1731 lacp_sm_rx_update_ntt(lp, du);
1732 lacp_sm_rx_record_pdu(lp, du);
1734 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1735 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1736 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1738 lp->lp_state &= ~LACP_STATE_EXPIRED;
1741 * kick transmit machine without waiting the next tick.
1748 lacp_sm_rx_set_expired(struct lacp_port *lp)
1750 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1751 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1752 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1753 lp->lp_state |= LACP_STATE_EXPIRED;
1757 lacp_sm_rx_timer(struct lacp_port *lp)
1759 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1760 /* CURRENT -> EXPIRED */
1761 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1762 lacp_sm_rx_set_expired(lp);
1764 /* EXPIRED -> DEFAULTED */
1765 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1766 lacp_sm_rx_update_default_selected(lp);
1767 lacp_sm_rx_record_default(lp);
1768 lp->lp_state &= ~LACP_STATE_EXPIRED;
1773 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1777 char buf[LACP_STATESTR_MAX+1];
1781 oldpstate = lp->lp_partner.lip_state;
1783 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1784 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1785 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1787 lp->lp_partner = du->ldu_actor;
1789 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1790 LACP_STATE_AGGREGATION) &&
1791 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1792 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1794 * XXX Maintain legacy behavior of leaving the
1795 * LACP_STATE_SYNC bit unchanged from the partner's
1796 * advertisement if lsc_strict_mode is false.
1797 * TODO: We should re-examine the concept of the "strict mode"
1798 * to ensure it makes sense to maintain a non-strict mode.
1800 if (lp->lp_lsc->lsc_strict_mode)
1801 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1803 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1806 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1808 if (oldpstate != lp->lp_partner.lip_state) {
1809 LACP_DPRINTF((lp, "old pstate %s\n",
1810 lacp_format_state(oldpstate, buf, sizeof(buf))));
1811 LACP_DPRINTF((lp, "new pstate %s\n",
1812 lacp_format_state(lp->lp_partner.lip_state, buf,
1816 lacp_sm_ptx_update_timeout(lp, oldpstate);
1820 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1825 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1826 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1827 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1828 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1829 lacp_sm_assert_ntt(lp);
1834 lacp_sm_rx_record_default(struct lacp_port *lp)
1840 oldpstate = lp->lp_partner.lip_state;
1841 if (lp->lp_lsc->lsc_strict_mode)
1842 lp->lp_partner = lacp_partner_admin_strict;
1844 lp->lp_partner = lacp_partner_admin_optimistic;
1845 lp->lp_state |= LACP_STATE_DEFAULTED;
1846 lacp_sm_ptx_update_timeout(lp, oldpstate);
1850 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1851 const struct lacp_peerinfo *info)
1856 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1857 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1858 LACP_STATE_AGGREGATION)) {
1859 lp->lp_selected = LACP_UNSELECTED;
1860 /* mux machine will clean up lp->lp_aggregator */
1865 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1870 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1874 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1879 if (lp->lp_lsc->lsc_strict_mode)
1880 lacp_sm_rx_update_selected_from_peerinfo(lp,
1881 &lacp_partner_admin_strict);
1883 lacp_sm_rx_update_selected_from_peerinfo(lp,
1884 &lacp_partner_admin_optimistic);
1887 /* transmit machine */
1890 lacp_sm_tx(struct lacp_port *lp)
1894 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1896 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1897 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1900 lp->lp_flags &= ~LACP_PORT_NTT;
1903 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1907 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1908 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1909 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1910 LACP_DPRINTF((lp, "rate limited pdu\n"));
1914 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1915 error = lacp_xmit_lacpdu(lp);
1917 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1921 lp->lp_flags &= ~LACP_PORT_NTT;
1923 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1929 lacp_sm_assert_ntt(struct lacp_port *lp)
1932 lp->lp_flags |= LACP_PORT_NTT;
1936 lacp_run_timers(struct lacp_port *lp)
1940 for (i = 0; i < LACP_NTIMER; i++) {
1941 KASSERT(lp->lp_timer[i] >= 0,
1942 ("invalid timer value %d", lp->lp_timer[i]));
1943 if (lp->lp_timer[i] == 0) {
1945 } else if (--lp->lp_timer[i] <= 0) {
1946 if (lacp_timer_funcs[i]) {
1947 (*lacp_timer_funcs[i])(lp);
1954 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1956 struct lacp_softc *lsc = lp->lp_lsc;
1957 struct lagg_port *lgp = lp->lp_lagg;
1958 struct lacp_port *lp2;
1959 struct markerdu *mdu;
1963 if (m->m_pkthdr.len != sizeof(*mdu)) {
1967 if ((m->m_flags & M_MCAST) == 0) {
1971 if (m->m_len < sizeof(*mdu)) {
1972 m = m_pullup(m, sizeof(*mdu));
1978 mdu = mtod(m, struct markerdu *);
1980 if (memcmp(&mdu->mdu_eh.ether_dhost,
1981 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1985 if (mdu->mdu_sph.sph_version != 1) {
1989 switch (mdu->mdu_tlv.tlv_type) {
1990 case MARKER_TYPE_INFO:
1991 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1992 marker_info_tlv_template, TRUE)) {
1995 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1996 memcpy(&mdu->mdu_eh.ether_dhost,
1997 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1998 memcpy(&mdu->mdu_eh.ether_shost,
1999 lgp->lp_lladdr, ETHER_ADDR_LEN);
2000 error = lagg_enqueue(lp->lp_ifp, m);
2003 case MARKER_TYPE_RESPONSE:
2004 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2005 marker_response_tlv_template, TRUE)) {
2008 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
2009 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
2010 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
2012 /* Verify that it is the last marker we sent out */
2013 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
2014 sizeof(struct lacp_markerinfo)))
2018 lp->lp_flags &= ~LACP_PORT_MARK;
2020 if (lsc->lsc_suppress_distributing) {
2021 /* Check if any ports are waiting for a response */
2022 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
2023 if (lp2->lp_flags & LACP_PORT_MARK) {
2030 /* All interface queues are clear */
2031 LACP_DPRINTF((NULL, "queue flush complete\n"));
2032 lsc->lsc_suppress_distributing = FALSE;
2046 LACP_DPRINTF((lp, "bad marker frame\n"));
2052 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
2053 const struct tlv_template *tmpl, boolean_t check_type)
2055 while (/* CONSTCOND */ 1) {
2056 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
2059 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
2060 tlv->tlv_length != tmpl->tmpl_length) {
2063 if (tmpl->tmpl_type == 0) {
2066 tlv = (const struct tlvhdr *)
2067 ((const char *)tlv + tlv->tlv_length);
2076 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
2078 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2090 lacp_format_systemid(const struct lacp_systemid *sysid,
2091 char *buf, size_t buflen)
2093 char macbuf[LACP_MACSTR_MAX+1];
2095 snprintf(buf, buflen, "%04X,%s",
2096 ntohs(sysid->lsi_prio),
2097 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2103 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2105 snprintf(buf, buflen, "%04X,%04X",
2106 ntohs(portid->lpi_prio),
2107 ntohs(portid->lpi_portno));
2113 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2115 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2116 char portid[LACP_PORTIDSTR_MAX+1];
2118 snprintf(buf, buflen, "(%s,%04X,%s)",
2119 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2120 ntohs(peer->lip_key),
2121 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2127 lacp_format_lagid(const struct lacp_peerinfo *a,
2128 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2130 char astr[LACP_PARTNERSTR_MAX+1];
2131 char bstr[LACP_PARTNERSTR_MAX+1];
2135 * there's a convention to display small numbered peer
2139 if (lacp_compare_peerinfo(a, b) > 0) {
2140 const struct lacp_peerinfo *t;
2148 snprintf(buf, buflen, "[%s,%s]",
2149 lacp_format_partner(a, astr, sizeof(astr)),
2150 lacp_format_partner(b, bstr, sizeof(bstr)));
2156 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2157 char *buf, size_t buflen)
2163 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2167 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2169 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2174 lacp_dump_lacpdu(const struct lacpdu *du)
2176 char buf[LACP_PARTNERSTR_MAX+1];
2177 char buf2[LACP_STATESTR_MAX+1];
2179 printf("actor=%s\n",
2180 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2181 printf("actor.state=%s\n",
2182 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2183 printf("partner=%s\n",
2184 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2185 printf("partner.state=%s\n",
2186 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2188 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2192 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2197 printf("%s: ", lp->lp_ifp->if_xname);