1 /* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $ */
4 * Copyright (c)2005 YAMAMOTO Takashi,
5 * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/callout.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h> /* hz */
39 #include <sys/socket.h> /* for net/if.h */
40 #include <sys/sockio.h>
41 #include <sys/sysctl.h>
42 #include <machine/stdarg.h>
44 #include <sys/rwlock.h>
47 #include <net/if_dl.h>
48 #include <net/ethernet.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
52 #include <net/if_lagg.h>
53 #include <net/ieee8023ad_lacp.h>
56 * actor system priority and port priority.
57 * XXX should be configurable.
60 #define LACP_SYSTEM_PRIO 0x8000
61 #define LACP_PORT_PRIO 0x8000
63 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
64 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
66 static const struct tlv_template lacp_info_tlv_template[] = {
67 { LACP_TYPE_ACTORINFO,
68 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
69 { LACP_TYPE_PARTNERINFO,
70 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
71 { LACP_TYPE_COLLECTORINFO,
72 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
76 static const struct tlv_template marker_info_tlv_template[] = {
78 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
82 static const struct tlv_template marker_response_tlv_template[] = {
83 { MARKER_TYPE_RESPONSE,
84 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
88 typedef void (*lacp_timer_func_t)(struct lacp_port *);
90 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
91 static void lacp_fill_markerinfo(struct lacp_port *,
92 struct lacp_markerinfo *);
94 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
95 static void lacp_suppress_distributing(struct lacp_softc *,
96 struct lacp_aggregator *);
97 static void lacp_transit_expire(void *);
98 static void lacp_update_portmap(struct lacp_softc *);
99 static void lacp_select_active_aggregator(struct lacp_softc *);
100 static uint16_t lacp_compose_key(struct lacp_port *);
101 static int tlv_check(const void *, size_t, const struct tlvhdr *,
102 const struct tlv_template *, boolean_t);
103 static void lacp_tick(void *);
105 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
106 const struct lacp_port *);
107 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
108 const struct lacp_peerinfo *);
109 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
110 const struct lacp_port *);
111 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
112 const struct lacp_peerinfo *);
114 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
116 static void lacp_aggregator_addref(struct lacp_softc *,
117 struct lacp_aggregator *);
118 static void lacp_aggregator_delref(struct lacp_softc *,
119 struct lacp_aggregator *);
121 /* receive machine */
123 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
124 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
125 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
126 static void lacp_sm_rx_timer(struct lacp_port *);
127 static void lacp_sm_rx_set_expired(struct lacp_port *);
128 static void lacp_sm_rx_update_ntt(struct lacp_port *,
129 const struct lacpdu *);
130 static void lacp_sm_rx_record_pdu(struct lacp_port *,
131 const struct lacpdu *);
132 static void lacp_sm_rx_update_selected(struct lacp_port *,
133 const struct lacpdu *);
134 static void lacp_sm_rx_record_default(struct lacp_port *);
135 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
136 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
137 const struct lacp_peerinfo *);
141 static void lacp_sm_mux(struct lacp_port *);
142 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
143 static void lacp_sm_mux_timer(struct lacp_port *);
145 /* periodic transmit machine */
147 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
148 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
149 static void lacp_sm_ptx_timer(struct lacp_port *);
151 /* transmit machine */
153 static void lacp_sm_tx(struct lacp_port *);
154 static void lacp_sm_assert_ntt(struct lacp_port *);
156 static void lacp_run_timers(struct lacp_port *);
157 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
158 const struct lacp_peerinfo *);
159 static int lacp_compare_systemid(const struct lacp_systemid *,
160 const struct lacp_systemid *);
161 static void lacp_port_enable(struct lacp_port *);
162 static void lacp_port_disable(struct lacp_port *);
163 static void lacp_select(struct lacp_port *);
164 static void lacp_unselect(struct lacp_port *);
165 static void lacp_disable_collecting(struct lacp_port *);
166 static void lacp_enable_collecting(struct lacp_port *);
167 static void lacp_disable_distributing(struct lacp_port *);
168 static void lacp_enable_distributing(struct lacp_port *);
169 static int lacp_xmit_lacpdu(struct lacp_port *);
170 static int lacp_xmit_marker(struct lacp_port *);
174 static void lacp_dump_lacpdu(const struct lacpdu *);
175 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
177 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
178 const struct lacp_peerinfo *, char *, size_t);
179 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
181 static const char *lacp_format_state(uint8_t, char *, size_t);
182 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
183 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
185 static const char *lacp_format_portid(const struct lacp_portid *, char *,
187 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
188 __attribute__((__format__(__printf__, 2, 3)));
190 static int lacp_debug = 0;
191 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad");
192 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
193 &lacp_debug, 0, "Enable LACP debug logging (1=debug, 2=trace)");
194 TUNABLE_INT("net.link.lagg.lacp.debug", &lacp_debug);
196 #define LACP_DPRINTF(a) if (lacp_debug & 0x01) { lacp_dprintf a ; }
197 #define LACP_TRACE(a) if (lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
198 #define LACP_TPRINTF(a) if (lacp_debug & 0x04) { lacp_dprintf a ; }
201 * partner administration variables.
202 * XXX should be configurable.
205 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
206 .lip_systemid = { .lsi_prio = 0xffff },
207 .lip_portid = { .lpi_prio = 0xffff },
208 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
209 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
212 static const struct lacp_peerinfo lacp_partner_admin_strict = {
213 .lip_systemid = { .lsi_prio = 0xffff },
214 .lip_portid = { .lpi_prio = 0xffff },
218 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
219 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
220 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
221 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
225 lacp_input(struct lagg_port *lgp, struct mbuf *m)
227 struct lacp_port *lp = LACP_PORT(lgp);
230 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
235 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
237 case SLOWPROTOCOLS_SUBTYPE_LACP:
238 lacp_pdu_input(lp, m);
241 case SLOWPROTOCOLS_SUBTYPE_MARKER:
242 lacp_marker_input(lp, m);
246 /* Not a subtype we are interested in */
251 * lacp_pdu_input: process lacpdu
254 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
256 struct lacp_softc *lsc = lp->lp_lsc;
260 if (m->m_pkthdr.len != sizeof(*du)) {
264 if ((m->m_flags & M_MCAST) == 0) {
268 if (m->m_len < sizeof(*du)) {
269 m = m_pullup(m, sizeof(*du));
275 du = mtod(m, struct lacpdu *);
277 if (memcmp(&du->ldu_eh.ether_dhost,
278 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
283 * ignore the version for compatibility with
284 * the future protocol revisions.
287 if (du->ldu_sph.sph_version != 1) {
293 * ignore tlv types for compatibility with
294 * the future protocol revisions.
296 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
297 lacp_info_tlv_template, FALSE)) {
301 if (lacp_debug > 0) {
302 lacp_dprintf(lp, "lacpdu receive\n");
303 lacp_dump_lacpdu(du);
306 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
307 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
324 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
326 struct lagg_port *lgp = lp->lp_lagg;
327 struct lagg_softc *sc = lgp->lp_softc;
329 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
330 memcpy(&info->lip_systemid.lsi_mac,
331 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
332 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
333 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
334 info->lip_state = lp->lp_state;
338 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
340 struct ifnet *ifp = lp->lp_ifp;
342 /* Fill in the port index and system id (encoded as the MAC) */
343 info->mi_rq_port = htons(ifp->if_index);
344 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
345 info->mi_rq_xid = htonl(0);
349 lacp_xmit_lacpdu(struct lacp_port *lp)
351 struct lagg_port *lgp = lp->lp_lagg;
356 LACP_LOCK_ASSERT(lp->lp_lsc);
358 m = m_gethdr(M_NOWAIT, MT_DATA);
362 m->m_len = m->m_pkthdr.len = sizeof(*du);
364 du = mtod(m, struct lacpdu *);
365 memset(du, 0, sizeof(*du));
367 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
369 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
370 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
372 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
373 du->ldu_sph.sph_version = 1;
375 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
376 du->ldu_actor = lp->lp_actor;
378 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
379 sizeof(du->ldu_partner));
380 du->ldu_partner = lp->lp_partner;
382 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
383 sizeof(du->ldu_collector));
384 du->ldu_collector.lci_maxdelay = 0;
386 if (lacp_debug > 0) {
387 lacp_dprintf(lp, "lacpdu transmit\n");
388 lacp_dump_lacpdu(du);
391 m->m_flags |= M_MCAST;
394 * XXX should use higher priority queue.
395 * otherwise network congestion can break aggregation.
398 error = lagg_enqueue(lp->lp_ifp, m);
403 lacp_xmit_marker(struct lacp_port *lp)
405 struct lagg_port *lgp = lp->lp_lagg;
407 struct markerdu *mdu;
410 LACP_LOCK_ASSERT(lp->lp_lsc);
412 m = m_gethdr(M_NOWAIT, MT_DATA);
416 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
418 mdu = mtod(m, struct markerdu *);
419 memset(mdu, 0, sizeof(*mdu));
421 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
423 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
424 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
426 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
427 mdu->mdu_sph.sph_version = 1;
429 /* Bump the transaction id and copy over the marker info */
430 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
431 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
432 mdu->mdu_info = lp->lp_marker;
434 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
435 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
436 ntohl(mdu->mdu_info.mi_rq_xid)));
438 m->m_flags |= M_MCAST;
439 error = lagg_enqueue(lp->lp_ifp, m);
444 lacp_linkstate(struct lagg_port *lgp)
446 struct lacp_port *lp = LACP_PORT(lgp);
447 struct lacp_softc *lsc = lp->lp_lsc;
448 struct ifnet *ifp = lgp->lp_ifp;
449 struct ifmediareq ifmr;
455 bzero((char *)&ifmr, sizeof(ifmr));
456 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
461 media = ifmr.ifm_active;
462 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
463 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
464 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
465 old_state = lp->lp_state;
466 old_key = lp->lp_key;
468 lp->lp_media = media;
470 * If the port is not an active full duplex Ethernet link then it can
473 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
474 ifp->if_link_state != LINK_STATE_UP) {
475 lacp_port_disable(lp);
477 lacp_port_enable(lp);
479 lp->lp_key = lacp_compose_key(lp);
481 if (old_state != lp->lp_state || old_key != lp->lp_key) {
482 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
483 lp->lp_selected = LACP_UNSELECTED;
491 struct lacp_softc *lsc = arg;
492 struct lacp_port *lp;
494 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
495 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
503 lacp_sm_ptx_tx_schedule(lp);
505 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
509 lacp_port_create(struct lagg_port *lgp)
511 struct lagg_softc *sc = lgp->lp_softc;
512 struct lacp_softc *lsc = LACP_SOFTC(sc);
513 struct lacp_port *lp;
514 struct ifnet *ifp = lgp->lp_ifp;
515 struct sockaddr_dl sdl;
516 struct ifmultiaddr *rifma = NULL;
519 boolean_t active = TRUE; /* XXX should be configurable */
520 boolean_t fast = FALSE; /* XXX should be configurable */
522 bzero((char *)&sdl, sizeof(sdl));
523 sdl.sdl_len = sizeof(sdl);
524 sdl.sdl_family = AF_LINK;
525 sdl.sdl_index = ifp->if_index;
526 sdl.sdl_type = IFT_ETHER;
527 sdl.sdl_alen = ETHER_ADDR_LEN;
529 bcopy(ðermulticastaddr_slowprotocols,
530 LLADDR(&sdl), ETHER_ADDR_LEN);
531 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
533 printf("%s: ADDMULTI failed on %s\n", __func__, lgp->lp_ifname);
537 lp = malloc(sizeof(struct lacp_port),
538 M_DEVBUF, M_NOWAIT|M_ZERO);
543 lgp->lp_psc = (caddr_t)lp;
549 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
551 lacp_fill_actorinfo(lp, &lp->lp_actor);
552 lacp_fill_markerinfo(lp, &lp->lp_marker);
554 (active ? LACP_STATE_ACTIVITY : 0) |
555 (fast ? LACP_STATE_TIMEOUT : 0);
556 lp->lp_aggregator = NULL;
557 lacp_sm_rx_set_expired(lp);
565 lacp_port_destroy(struct lagg_port *lgp)
567 struct lacp_port *lp = LACP_PORT(lgp);
568 struct lacp_softc *lsc = lp->lp_lsc;
572 for (i = 0; i < LACP_NTIMER; i++) {
573 LACP_TIMER_DISARM(lp, i);
576 lacp_disable_collecting(lp);
577 lacp_disable_distributing(lp);
580 /* The address may have already been removed by if_purgemaddrs() */
581 if (!lgp->lp_detaching)
582 if_delmulti_ifma(lp->lp_ifma);
584 LIST_REMOVE(lp, lp_next);
590 lacp_req(struct lagg_softc *sc, caddr_t data)
592 struct lacp_opreq *req = (struct lacp_opreq *)data;
593 struct lacp_softc *lsc = LACP_SOFTC(sc);
594 struct lacp_aggregator *la;
596 bzero(req, sizeof(struct lacp_opreq));
599 * If the LACP softc is NULL, return with the opreq structure full of
600 * zeros. It is normal for the softc to be NULL while the lagg is
606 la = lsc->lsc_active_aggregator;
609 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
610 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
612 req->actor_key = ntohs(la->la_actor.lip_key);
613 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
614 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
615 req->actor_state = la->la_actor.lip_state;
617 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
618 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
620 req->partner_key = ntohs(la->la_partner.lip_key);
621 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
622 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
623 req->partner_state = la->la_partner.lip_state;
629 lacp_portreq(struct lagg_port *lgp, caddr_t data)
631 struct lacp_opreq *req = (struct lacp_opreq *)data;
632 struct lacp_port *lp = LACP_PORT(lgp);
633 struct lacp_softc *lsc = lp->lp_lsc;
636 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
637 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
639 req->actor_key = ntohs(lp->lp_actor.lip_key);
640 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
641 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
642 req->actor_state = lp->lp_actor.lip_state;
644 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
645 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
647 req->partner_key = ntohs(lp->lp_partner.lip_key);
648 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
649 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
650 req->partner_state = lp->lp_partner.lip_state;
655 lacp_disable_collecting(struct lacp_port *lp)
657 LACP_DPRINTF((lp, "collecting disabled\n"));
658 lp->lp_state &= ~LACP_STATE_COLLECTING;
662 lacp_enable_collecting(struct lacp_port *lp)
664 LACP_DPRINTF((lp, "collecting enabled\n"));
665 lp->lp_state |= LACP_STATE_COLLECTING;
669 lacp_disable_distributing(struct lacp_port *lp)
671 struct lacp_aggregator *la = lp->lp_aggregator;
672 struct lacp_softc *lsc = lp->lp_lsc;
673 struct lagg_softc *sc = lsc->lsc_softc;
674 char buf[LACP_LAGIDSTR_MAX+1];
676 LACP_LOCK_ASSERT(lsc);
678 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
682 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
683 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
684 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
686 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
688 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
689 la->la_nports, la->la_nports - 1));
691 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
693 sc->sc_active = la->la_nports;
695 if (lsc->lsc_active_aggregator == la) {
696 lacp_suppress_distributing(lsc, la);
697 lacp_select_active_aggregator(lsc);
698 /* regenerate the port map, the active aggregator has changed */
699 lacp_update_portmap(lsc);
702 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
706 lacp_enable_distributing(struct lacp_port *lp)
708 struct lacp_aggregator *la = lp->lp_aggregator;
709 struct lacp_softc *lsc = lp->lp_lsc;
710 struct lagg_softc *sc = lsc->lsc_softc;
711 char buf[LACP_LAGIDSTR_MAX+1];
713 LACP_LOCK_ASSERT(lsc);
715 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
719 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
721 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
722 la->la_nports, la->la_nports + 1));
724 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
725 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
727 sc->sc_active = la->la_nports;
729 lp->lp_state |= LACP_STATE_DISTRIBUTING;
731 if (lsc->lsc_active_aggregator == la) {
732 lacp_suppress_distributing(lsc, la);
733 lacp_update_portmap(lsc);
735 /* try to become the active aggregator */
736 lacp_select_active_aggregator(lsc);
740 lacp_transit_expire(void *vp)
742 struct lacp_softc *lsc = vp;
744 LACP_LOCK_ASSERT(lsc);
748 lsc->lsc_suppress_distributing = FALSE;
752 lacp_attach_sysctl(struct lacp_softc *lsc, struct sysctl_oid *p_oid)
754 struct lagg_softc *sc = lsc->lsc_softc;
756 SYSCTL_ADD_UINT(&sc->ctx, SYSCTL_CHILDREN(p_oid), OID_AUTO,
759 &lsc->lsc_strict_mode,
760 lsc->lsc_strict_mode,
761 "Enable LACP strict mode");
765 lacp_attach_sysctl_debug(struct lacp_softc *lsc, struct sysctl_oid *p_oid)
767 struct lagg_softc *sc = lsc->lsc_softc;
768 struct sysctl_oid *oid;
770 /* Create a child of the parent lagg interface */
771 oid = SYSCTL_ADD_NODE(&sc->ctx, SYSCTL_CHILDREN(p_oid),
772 OID_AUTO, "debug", CTLFLAG_RD, NULL, "DEBUG");
774 SYSCTL_ADD_UINT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
777 &lsc->lsc_debug.lsc_rx_test,
778 lsc->lsc_debug.lsc_rx_test,
779 "Bitmap of if_dunit entries to drop RX frames for");
780 SYSCTL_ADD_UINT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
783 &lsc->lsc_debug.lsc_tx_test,
784 lsc->lsc_debug.lsc_tx_test,
785 "Bitmap of if_dunit entries to drop TX frames for");
789 lacp_attach(struct lagg_softc *sc)
791 struct lacp_softc *lsc;
792 struct sysctl_oid *oid;
794 lsc = malloc(sizeof(struct lacp_softc),
795 M_DEVBUF, M_NOWAIT|M_ZERO);
799 sc->sc_psc = (caddr_t)lsc;
802 lsc->lsc_hashkey = arc4random();
803 lsc->lsc_active_aggregator = NULL;
804 lsc->lsc_strict_mode = 1;
806 TAILQ_INIT(&lsc->lsc_aggregators);
807 LIST_INIT(&lsc->lsc_ports);
809 /* Create a child of the parent lagg interface */
810 oid = SYSCTL_ADD_NODE(&sc->ctx, SYSCTL_CHILDREN(sc->sc_oid),
811 OID_AUTO, "lacp", CTLFLAG_RD, NULL, "LACP");
813 /* Attach sysctl nodes */
814 lacp_attach_sysctl(lsc, oid);
815 lacp_attach_sysctl_debug(lsc, oid);
817 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
818 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
820 /* if the lagg is already up then do the same */
821 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
828 lacp_detach(struct lagg_softc *sc)
830 struct lacp_softc *lsc = LACP_SOFTC(sc);
832 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
833 ("aggregators still active"));
834 KASSERT(lsc->lsc_active_aggregator == NULL,
835 ("aggregator still attached"));
838 callout_drain(&lsc->lsc_transit_callout);
839 callout_drain(&lsc->lsc_callout);
841 LACP_LOCK_DESTROY(lsc);
847 lacp_init(struct lagg_softc *sc)
849 struct lacp_softc *lsc = LACP_SOFTC(sc);
852 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
857 lacp_stop(struct lagg_softc *sc)
859 struct lacp_softc *lsc = LACP_SOFTC(sc);
862 callout_stop(&lsc->lsc_transit_callout);
863 callout_stop(&lsc->lsc_callout);
868 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
870 struct lacp_softc *lsc = LACP_SOFTC(sc);
871 struct lacp_portmap *pm;
872 struct lacp_port *lp;
875 if (__predict_false(lsc->lsc_suppress_distributing)) {
876 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
880 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
881 if (pm->pm_count == 0) {
882 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
886 if (sc->use_flowid &&
887 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
888 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
890 hash = lagg_hashmbuf(sc, m, lsc->lsc_hashkey);
891 hash %= pm->pm_count;
892 lp = pm->pm_map[hash];
894 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
895 ("aggregated port is not distributing"));
897 return (lp->lp_lagg);
900 * lacp_suppress_distributing: drop transmit packets for a while
901 * to preserve packet ordering.
905 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
907 struct lacp_port *lp;
909 if (lsc->lsc_active_aggregator != la) {
915 lsc->lsc_suppress_distributing = TRUE;
917 /* send a marker frame down each port to verify the queues are empty */
918 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
919 lp->lp_flags |= LACP_PORT_MARK;
920 lacp_xmit_marker(lp);
923 /* set a timeout for the marker frames */
924 callout_reset(&lsc->lsc_transit_callout,
925 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
929 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
930 const struct lacp_peerinfo *b)
932 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
936 lacp_compare_systemid(const struct lacp_systemid *a,
937 const struct lacp_systemid *b)
939 return (memcmp(a, b, sizeof(*a)));
944 lacp_compare_portid(const struct lacp_portid *a,
945 const struct lacp_portid *b)
947 return (memcmp(a, b, sizeof(*a)));
952 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
954 struct lacp_port *lp;
957 lp = TAILQ_FIRST(&la->la_ports);
962 speed = ifmedia_baudrate(lp->lp_media);
963 speed *= la->la_nports;
965 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
966 lp->lp_media, la->la_nports));
973 * lacp_select_active_aggregator: select an aggregator to be used to transmit
974 * packets from lagg(4) interface.
978 lacp_select_active_aggregator(struct lacp_softc *lsc)
980 struct lacp_aggregator *la;
981 struct lacp_aggregator *best_la = NULL;
982 uint64_t best_speed = 0;
983 char buf[LACP_LAGIDSTR_MAX+1];
987 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
990 if (la->la_nports == 0) {
994 speed = lacp_aggregator_bandwidth(la);
995 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
996 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
997 speed, la->la_nports));
999 /* This aggregator is chosen if
1000 * the partner has a better system priority
1001 * or, the total aggregated speed is higher
1002 * or, it is already the chosen aggregator
1004 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1005 LACP_SYS_PRI(best_la->la_partner)) ||
1006 speed > best_speed ||
1007 (speed == best_speed &&
1008 la == lsc->lsc_active_aggregator)) {
1014 KASSERT(best_la == NULL || best_la->la_nports > 0,
1015 ("invalid aggregator refcnt"));
1016 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1017 ("invalid aggregator list"));
1019 if (lsc->lsc_active_aggregator != best_la) {
1020 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1021 LACP_DPRINTF((NULL, "old %s\n",
1022 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1023 buf, sizeof(buf))));
1025 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1027 LACP_DPRINTF((NULL, "new %s\n",
1028 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1030 if (lsc->lsc_active_aggregator != best_la) {
1031 lsc->lsc_active_aggregator = best_la;
1032 lacp_update_portmap(lsc);
1034 lacp_suppress_distributing(lsc, best_la);
1040 * Updated the inactive portmap array with the new list of ports and
1044 lacp_update_portmap(struct lacp_softc *lsc)
1046 struct lagg_softc *sc = lsc->lsc_softc;
1047 struct lacp_aggregator *la;
1048 struct lacp_portmap *p;
1049 struct lacp_port *lp;
1054 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1055 p = &lsc->lsc_pmap[newmap];
1056 la = lsc->lsc_active_aggregator;
1058 bzero(p, sizeof(struct lacp_portmap));
1060 if (la != NULL && la->la_nports > 0) {
1061 p->pm_count = la->la_nports;
1063 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
1064 p->pm_map[i++] = lp;
1065 KASSERT(i == p->pm_count, ("Invalid port count"));
1066 speed = lacp_aggregator_bandwidth(la);
1068 sc->sc_ifp->if_baudrate = speed;
1070 /* switch the active portmap over */
1071 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1072 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1074 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1078 lacp_compose_key(struct lacp_port *lp)
1080 struct lagg_port *lgp = lp->lp_lagg;
1081 struct lagg_softc *sc = lgp->lp_softc;
1082 u_int media = lp->lp_media;
1085 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1088 * non-aggregatable links should have unique keys.
1090 * XXX this isn't really unique as if_index is 16 bit.
1093 /* bit 0..14: (some bits of) if_index of this port */
1094 key = lp->lp_ifp->if_index;
1098 u_int subtype = IFM_SUBTYPE(media);
1100 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1101 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1103 /* bit 0..4: IFM_SUBTYPE modulo speed */
1125 case IFM_1000_SGMII:
1126 case IFM_1000_CX_SGMII:
1132 case IFM_10G_TWINAX:
1133 case IFM_10G_TWINAX_LONG:
1184 /* bit 5..14: (some bits of) if_index of lagg device */
1185 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1188 return (htons(key));
1192 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1194 char buf[LACP_LAGIDSTR_MAX+1];
1196 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1198 lacp_format_lagid(&la->la_actor, &la->la_partner,
1200 la->la_refcnt, la->la_refcnt + 1));
1202 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1204 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1208 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1210 char buf[LACP_LAGIDSTR_MAX+1];
1212 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1214 lacp_format_lagid(&la->la_actor, &la->la_partner,
1216 la->la_refcnt, la->la_refcnt - 1));
1218 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1220 if (la->la_refcnt > 0) {
1224 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1225 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1227 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1233 * lacp_aggregator_get: allocate an aggregator.
1236 static struct lacp_aggregator *
1237 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1239 struct lacp_aggregator *la;
1241 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1245 TAILQ_INIT(&la->la_ports);
1247 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1254 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1258 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1260 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1261 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1263 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1267 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1268 const struct lacp_peerinfo *lpi_port)
1270 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1271 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1272 lpi_aggr->lip_key = lpi_port->lip_key;
1276 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1280 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1281 const struct lacp_port *lp)
1283 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1284 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1288 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1292 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1296 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1304 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1305 const struct lacp_peerinfo *b)
1307 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1308 sizeof(a->lip_systemid))) {
1312 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1320 lacp_port_enable(struct lacp_port *lp)
1322 lp->lp_state |= LACP_STATE_AGGREGATION;
1326 lacp_port_disable(struct lacp_port *lp)
1328 lacp_set_mux(lp, LACP_MUX_DETACHED);
1330 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1331 lp->lp_selected = LACP_UNSELECTED;
1332 lacp_sm_rx_record_default(lp);
1333 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1334 lp->lp_state &= ~LACP_STATE_EXPIRED;
1338 * lacp_select: select an aggregator. create one if necessary.
1341 lacp_select(struct lacp_port *lp)
1343 struct lacp_softc *lsc = lp->lp_lsc;
1344 struct lacp_aggregator *la;
1345 char buf[LACP_LAGIDSTR_MAX+1];
1347 if (lp->lp_aggregator) {
1351 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1352 ("timer_wait_while still active"));
1354 LACP_DPRINTF((lp, "port lagid=%s\n",
1355 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1356 buf, sizeof(buf))));
1358 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1359 if (lacp_aggregator_is_compatible(la, lp)) {
1365 la = lacp_aggregator_get(lsc, lp);
1367 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1370 * will retry on the next tick.
1375 lacp_fill_aggregator_id(la, lp);
1376 LACP_DPRINTF((lp, "aggregator created\n"));
1378 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1379 if (la->la_refcnt == LACP_MAX_PORTS)
1381 lacp_aggregator_addref(lsc, la);
1384 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1385 lacp_format_lagid(&la->la_actor, &la->la_partner,
1386 buf, sizeof(buf))));
1388 lp->lp_aggregator = la;
1389 lp->lp_selected = LACP_SELECTED;
1393 * lacp_unselect: finish unselect/detach process.
1397 lacp_unselect(struct lacp_port *lp)
1399 struct lacp_softc *lsc = lp->lp_lsc;
1400 struct lacp_aggregator *la = lp->lp_aggregator;
1402 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1403 ("timer_wait_while still active"));
1409 lp->lp_aggregator = NULL;
1410 lacp_aggregator_delref(lsc, la);
1416 lacp_sm_mux(struct lacp_port *lp)
1418 struct lagg_port *lgp = lp->lp_lagg;
1419 struct lagg_softc *sc = lgp->lp_softc;
1420 enum lacp_mux_state new_state;
1422 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1423 boolean_t p_collecting =
1424 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1425 enum lacp_selected selected = lp->lp_selected;
1426 struct lacp_aggregator *la;
1429 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1430 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1431 lp->lp_mux_state, selected, p_sync, p_collecting);
1434 la = lp->lp_aggregator;
1435 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1436 ("MUX not detached"));
1437 new_state = lp->lp_mux_state;
1438 switch (lp->lp_mux_state) {
1439 case LACP_MUX_DETACHED:
1440 if (selected != LACP_UNSELECTED) {
1441 new_state = LACP_MUX_WAITING;
1444 case LACP_MUX_WAITING:
1445 KASSERT(la->la_pending > 0 ||
1446 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1447 ("timer_wait_while still active"));
1448 if (selected == LACP_SELECTED && la->la_pending == 0) {
1449 new_state = LACP_MUX_ATTACHED;
1450 } else if (selected == LACP_UNSELECTED) {
1451 new_state = LACP_MUX_DETACHED;
1454 case LACP_MUX_ATTACHED:
1455 if (selected == LACP_SELECTED && p_sync) {
1456 new_state = LACP_MUX_COLLECTING;
1457 } else if (selected != LACP_SELECTED) {
1458 new_state = LACP_MUX_DETACHED;
1461 case LACP_MUX_COLLECTING:
1462 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1463 new_state = LACP_MUX_DISTRIBUTING;
1464 } else if (selected != LACP_SELECTED || !p_sync) {
1465 new_state = LACP_MUX_ATTACHED;
1468 case LACP_MUX_DISTRIBUTING:
1469 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1470 new_state = LACP_MUX_COLLECTING;
1471 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1476 panic("%s: unknown state", __func__);
1479 if (lp->lp_mux_state == new_state) {
1483 lacp_set_mux(lp, new_state);
1488 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1490 struct lacp_aggregator *la = lp->lp_aggregator;
1492 if (lp->lp_mux_state == new_state) {
1496 switch (new_state) {
1497 case LACP_MUX_DETACHED:
1498 lp->lp_state &= ~LACP_STATE_SYNC;
1499 lacp_disable_distributing(lp);
1500 lacp_disable_collecting(lp);
1501 lacp_sm_assert_ntt(lp);
1503 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1504 KASSERT(la->la_pending > 0,
1505 ("timer_wait_while not active"));
1508 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1511 case LACP_MUX_WAITING:
1512 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1513 LACP_AGGREGATE_WAIT_TIME);
1516 case LACP_MUX_ATTACHED:
1517 lp->lp_state |= LACP_STATE_SYNC;
1518 lacp_disable_collecting(lp);
1519 lacp_sm_assert_ntt(lp);
1521 case LACP_MUX_COLLECTING:
1522 lacp_enable_collecting(lp);
1523 lacp_disable_distributing(lp);
1524 lacp_sm_assert_ntt(lp);
1526 case LACP_MUX_DISTRIBUTING:
1527 lacp_enable_distributing(lp);
1530 panic("%s: unknown state", __func__);
1533 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1535 lp->lp_mux_state = new_state;
1539 lacp_sm_mux_timer(struct lacp_port *lp)
1541 struct lacp_aggregator *la = lp->lp_aggregator;
1542 char buf[LACP_LAGIDSTR_MAX+1];
1544 KASSERT(la->la_pending > 0, ("no pending event"));
1546 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1547 lacp_format_lagid(&la->la_actor, &la->la_partner,
1549 la->la_pending, la->la_pending - 1));
1554 /* periodic transmit machine */
1557 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1559 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1560 LACP_STATE_TIMEOUT)) {
1564 LACP_DPRINTF((lp, "partner timeout changed\n"));
1567 * FAST_PERIODIC -> SLOW_PERIODIC
1569 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1571 * let lacp_sm_ptx_tx_schedule to update timeout.
1574 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1577 * if timeout has been shortened, assert NTT.
1580 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1581 lacp_sm_assert_ntt(lp);
1586 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1590 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1591 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1597 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1601 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1605 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1606 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1608 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1612 lacp_sm_ptx_timer(struct lacp_port *lp)
1614 lacp_sm_assert_ntt(lp);
1618 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1623 * check LACP_DISABLED first
1626 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1631 * check loopback condition.
1634 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1635 &lp->lp_actor.lip_systemid)) {
1640 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1643 lacp_sm_rx_update_selected(lp, du);
1644 lacp_sm_rx_update_ntt(lp, du);
1645 lacp_sm_rx_record_pdu(lp, du);
1647 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1648 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1649 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1651 lp->lp_state &= ~LACP_STATE_EXPIRED;
1654 * kick transmit machine without waiting the next tick.
1661 lacp_sm_rx_set_expired(struct lacp_port *lp)
1663 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1664 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1665 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1666 lp->lp_state |= LACP_STATE_EXPIRED;
1670 lacp_sm_rx_timer(struct lacp_port *lp)
1672 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1673 /* CURRENT -> EXPIRED */
1674 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1675 lacp_sm_rx_set_expired(lp);
1677 /* EXPIRED -> DEFAULTED */
1678 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1679 lacp_sm_rx_update_default_selected(lp);
1680 lacp_sm_rx_record_default(lp);
1681 lp->lp_state &= ~LACP_STATE_EXPIRED;
1686 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1690 char buf[LACP_STATESTR_MAX+1];
1694 oldpstate = lp->lp_partner.lip_state;
1696 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1697 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1698 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1700 lp->lp_partner = du->ldu_actor;
1702 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1703 LACP_STATE_AGGREGATION) &&
1704 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1705 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1708 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1711 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1713 if (oldpstate != lp->lp_partner.lip_state) {
1714 LACP_DPRINTF((lp, "old pstate %s\n",
1715 lacp_format_state(oldpstate, buf, sizeof(buf))));
1716 LACP_DPRINTF((lp, "new pstate %s\n",
1717 lacp_format_state(lp->lp_partner.lip_state, buf,
1721 /* XXX Hack, still need to implement 5.4.9 para 2,3,4 */
1722 if (lp->lp_lsc->lsc_strict_mode)
1723 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1725 lacp_sm_ptx_update_timeout(lp, oldpstate);
1729 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1734 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1735 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1736 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1737 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1738 lacp_sm_assert_ntt(lp);
1743 lacp_sm_rx_record_default(struct lacp_port *lp)
1749 oldpstate = lp->lp_partner.lip_state;
1750 if (lp->lp_lsc->lsc_strict_mode)
1751 lp->lp_partner = lacp_partner_admin_strict;
1753 lp->lp_partner = lacp_partner_admin_optimistic;;
1754 lp->lp_state |= LACP_STATE_DEFAULTED;
1755 lacp_sm_ptx_update_timeout(lp, oldpstate);
1759 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1760 const struct lacp_peerinfo *info)
1765 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1766 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1767 LACP_STATE_AGGREGATION)) {
1768 lp->lp_selected = LACP_UNSELECTED;
1769 /* mux machine will clean up lp->lp_aggregator */
1774 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1779 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1783 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1788 if (lp->lp_lsc->lsc_strict_mode)
1789 lacp_sm_rx_update_selected_from_peerinfo(lp,
1790 &lacp_partner_admin_strict);
1792 lacp_sm_rx_update_selected_from_peerinfo(lp,
1793 &lacp_partner_admin_optimistic);
1796 /* transmit machine */
1799 lacp_sm_tx(struct lacp_port *lp)
1803 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1805 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1806 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1809 lp->lp_flags &= ~LACP_PORT_NTT;
1812 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1816 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1817 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1818 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1819 LACP_DPRINTF((lp, "rate limited pdu\n"));
1823 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1824 error = lacp_xmit_lacpdu(lp);
1826 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1830 lp->lp_flags &= ~LACP_PORT_NTT;
1832 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1838 lacp_sm_assert_ntt(struct lacp_port *lp)
1841 lp->lp_flags |= LACP_PORT_NTT;
1845 lacp_run_timers(struct lacp_port *lp)
1849 for (i = 0; i < LACP_NTIMER; i++) {
1850 KASSERT(lp->lp_timer[i] >= 0,
1851 ("invalid timer value %d", lp->lp_timer[i]));
1852 if (lp->lp_timer[i] == 0) {
1854 } else if (--lp->lp_timer[i] <= 0) {
1855 if (lacp_timer_funcs[i]) {
1856 (*lacp_timer_funcs[i])(lp);
1863 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1865 struct lacp_softc *lsc = lp->lp_lsc;
1866 struct lagg_port *lgp = lp->lp_lagg;
1867 struct lacp_port *lp2;
1868 struct markerdu *mdu;
1872 if (m->m_pkthdr.len != sizeof(*mdu)) {
1876 if ((m->m_flags & M_MCAST) == 0) {
1880 if (m->m_len < sizeof(*mdu)) {
1881 m = m_pullup(m, sizeof(*mdu));
1887 mdu = mtod(m, struct markerdu *);
1889 if (memcmp(&mdu->mdu_eh.ether_dhost,
1890 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1894 if (mdu->mdu_sph.sph_version != 1) {
1898 switch (mdu->mdu_tlv.tlv_type) {
1899 case MARKER_TYPE_INFO:
1900 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1901 marker_info_tlv_template, TRUE)) {
1904 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1905 memcpy(&mdu->mdu_eh.ether_dhost,
1906 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1907 memcpy(&mdu->mdu_eh.ether_shost,
1908 lgp->lp_lladdr, ETHER_ADDR_LEN);
1909 error = lagg_enqueue(lp->lp_ifp, m);
1912 case MARKER_TYPE_RESPONSE:
1913 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1914 marker_response_tlv_template, TRUE)) {
1917 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
1918 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
1919 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
1921 /* Verify that it is the last marker we sent out */
1922 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
1923 sizeof(struct lacp_markerinfo)))
1927 lp->lp_flags &= ~LACP_PORT_MARK;
1929 if (lsc->lsc_suppress_distributing) {
1930 /* Check if any ports are waiting for a response */
1931 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
1932 if (lp2->lp_flags & LACP_PORT_MARK) {
1939 /* All interface queues are clear */
1940 LACP_DPRINTF((NULL, "queue flush complete\n"));
1941 lsc->lsc_suppress_distributing = FALSE;
1955 LACP_DPRINTF((lp, "bad marker frame\n"));
1961 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
1962 const struct tlv_template *tmpl, boolean_t check_type)
1964 while (/* CONSTCOND */ 1) {
1965 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
1968 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
1969 tlv->tlv_length != tmpl->tmpl_length) {
1972 if (tmpl->tmpl_type == 0) {
1975 tlv = (const struct tlvhdr *)
1976 ((const char *)tlv + tlv->tlv_length);
1985 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
1987 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
1999 lacp_format_systemid(const struct lacp_systemid *sysid,
2000 char *buf, size_t buflen)
2002 char macbuf[LACP_MACSTR_MAX+1];
2004 snprintf(buf, buflen, "%04X,%s",
2005 ntohs(sysid->lsi_prio),
2006 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2012 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2014 snprintf(buf, buflen, "%04X,%04X",
2015 ntohs(portid->lpi_prio),
2016 ntohs(portid->lpi_portno));
2022 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2024 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2025 char portid[LACP_PORTIDSTR_MAX+1];
2027 snprintf(buf, buflen, "(%s,%04X,%s)",
2028 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2029 ntohs(peer->lip_key),
2030 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2036 lacp_format_lagid(const struct lacp_peerinfo *a,
2037 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2039 char astr[LACP_PARTNERSTR_MAX+1];
2040 char bstr[LACP_PARTNERSTR_MAX+1];
2044 * there's a convention to display small numbered peer
2048 if (lacp_compare_peerinfo(a, b) > 0) {
2049 const struct lacp_peerinfo *t;
2057 snprintf(buf, buflen, "[%s,%s]",
2058 lacp_format_partner(a, astr, sizeof(astr)),
2059 lacp_format_partner(b, bstr, sizeof(bstr)));
2065 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2066 char *buf, size_t buflen)
2072 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2076 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2078 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2083 lacp_dump_lacpdu(const struct lacpdu *du)
2085 char buf[LACP_PARTNERSTR_MAX+1];
2086 char buf2[LACP_STATESTR_MAX+1];
2088 printf("actor=%s\n",
2089 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2090 printf("actor.state=%s\n",
2091 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2092 printf("partner=%s\n",
2093 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2094 printf("partner.state=%s\n",
2095 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2097 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2101 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2106 printf("%s: ", lp->lp_ifp->if_xname);