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>
35 #include <sys/eventhandler.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h> /* hz */
40 #include <sys/socket.h> /* for net/if.h */
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <machine/stdarg.h>
45 #include <sys/rwlock.h>
46 #include <sys/taskqueue.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/ethernet.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
55 #include <net/if_lagg.h>
56 #include <net/ieee8023ad_lacp.h>
59 * actor system priority and port priority.
60 * XXX should be configurable.
63 #define LACP_SYSTEM_PRIO 0x8000
64 #define LACP_PORT_PRIO 0x8000
66 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
67 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
69 static const struct tlv_template lacp_info_tlv_template[] = {
70 { LACP_TYPE_ACTORINFO,
71 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
72 { LACP_TYPE_PARTNERINFO,
73 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
74 { LACP_TYPE_COLLECTORINFO,
75 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
79 static const struct tlv_template marker_info_tlv_template[] = {
81 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
85 static const struct tlv_template marker_response_tlv_template[] = {
86 { MARKER_TYPE_RESPONSE,
87 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
91 typedef void (*lacp_timer_func_t)(struct lacp_port *);
93 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
94 static void lacp_fill_markerinfo(struct lacp_port *,
95 struct lacp_markerinfo *);
97 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
98 static void lacp_suppress_distributing(struct lacp_softc *,
99 struct lacp_aggregator *);
100 static void lacp_transit_expire(void *);
101 static void lacp_update_portmap(struct lacp_softc *);
102 static void lacp_select_active_aggregator(struct lacp_softc *);
103 static uint16_t lacp_compose_key(struct lacp_port *);
104 static int tlv_check(const void *, size_t, const struct tlvhdr *,
105 const struct tlv_template *, boolean_t);
106 static void lacp_tick(void *);
108 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
109 const struct lacp_port *);
110 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
111 const struct lacp_peerinfo *);
112 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
113 const struct lacp_port *);
114 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
115 const struct lacp_peerinfo *);
117 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
119 static void lacp_aggregator_addref(struct lacp_softc *,
120 struct lacp_aggregator *);
121 static void lacp_aggregator_delref(struct lacp_softc *,
122 struct lacp_aggregator *);
124 /* receive machine */
126 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
127 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
128 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
129 static void lacp_sm_rx_timer(struct lacp_port *);
130 static void lacp_sm_rx_set_expired(struct lacp_port *);
131 static void lacp_sm_rx_update_ntt(struct lacp_port *,
132 const struct lacpdu *);
133 static void lacp_sm_rx_record_pdu(struct lacp_port *,
134 const struct lacpdu *);
135 static void lacp_sm_rx_update_selected(struct lacp_port *,
136 const struct lacpdu *);
137 static void lacp_sm_rx_record_default(struct lacp_port *);
138 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
139 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
140 const struct lacp_peerinfo *);
144 static void lacp_sm_mux(struct lacp_port *);
145 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
146 static void lacp_sm_mux_timer(struct lacp_port *);
148 /* periodic transmit machine */
150 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
151 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
152 static void lacp_sm_ptx_timer(struct lacp_port *);
154 /* transmit machine */
156 static void lacp_sm_tx(struct lacp_port *);
157 static void lacp_sm_assert_ntt(struct lacp_port *);
159 static void lacp_run_timers(struct lacp_port *);
160 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
161 const struct lacp_peerinfo *);
162 static int lacp_compare_systemid(const struct lacp_systemid *,
163 const struct lacp_systemid *);
164 static void lacp_port_enable(struct lacp_port *);
165 static void lacp_port_disable(struct lacp_port *);
166 static void lacp_select(struct lacp_port *);
167 static void lacp_unselect(struct lacp_port *);
168 static void lacp_disable_collecting(struct lacp_port *);
169 static void lacp_enable_collecting(struct lacp_port *);
170 static void lacp_disable_distributing(struct lacp_port *);
171 static void lacp_enable_distributing(struct lacp_port *);
172 static int lacp_xmit_lacpdu(struct lacp_port *);
173 static int lacp_xmit_marker(struct lacp_port *);
177 static void lacp_dump_lacpdu(const struct lacpdu *);
178 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
180 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
181 const struct lacp_peerinfo *, char *, size_t);
182 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
184 static const char *lacp_format_state(uint8_t, char *, size_t);
185 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
186 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
188 static const char *lacp_format_portid(const struct lacp_portid *, char *,
190 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
191 __attribute__((__format__(__printf__, 2, 3)));
193 static VNET_DEFINE(int, lacp_debug);
194 #define V_lacp_debug VNET(lacp_debug)
195 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad");
196 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
197 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
199 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
200 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
201 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
204 * partner administration variables.
205 * XXX should be configurable.
208 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
209 .lip_systemid = { .lsi_prio = 0xffff },
210 .lip_portid = { .lpi_prio = 0xffff },
211 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
212 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
215 static const struct lacp_peerinfo lacp_partner_admin_strict = {
216 .lip_systemid = { .lsi_prio = 0xffff },
217 .lip_portid = { .lpi_prio = 0xffff },
221 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
222 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
223 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
224 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
228 lacp_input(struct lagg_port *lgp, struct mbuf *m)
230 struct lacp_port *lp = LACP_PORT(lgp);
233 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
238 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
240 case SLOWPROTOCOLS_SUBTYPE_LACP:
241 lacp_pdu_input(lp, m);
244 case SLOWPROTOCOLS_SUBTYPE_MARKER:
245 lacp_marker_input(lp, m);
249 /* Not a subtype we are interested in */
254 * lacp_pdu_input: process lacpdu
257 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
259 struct lacp_softc *lsc = lp->lp_lsc;
263 if (m->m_pkthdr.len != sizeof(*du)) {
267 if ((m->m_flags & M_MCAST) == 0) {
271 if (m->m_len < sizeof(*du)) {
272 m = m_pullup(m, sizeof(*du));
278 du = mtod(m, struct lacpdu *);
280 if (memcmp(&du->ldu_eh.ether_dhost,
281 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
286 * ignore the version for compatibility with
287 * the future protocol revisions.
290 if (du->ldu_sph.sph_version != 1) {
296 * ignore tlv types for compatibility with
297 * the future protocol revisions.
299 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
300 lacp_info_tlv_template, FALSE)) {
304 if (V_lacp_debug > 0) {
305 lacp_dprintf(lp, "lacpdu receive\n");
306 lacp_dump_lacpdu(du);
309 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
310 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
327 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
329 struct lagg_port *lgp = lp->lp_lagg;
330 struct lagg_softc *sc = lgp->lp_softc;
332 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
333 memcpy(&info->lip_systemid.lsi_mac,
334 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
335 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
336 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
337 info->lip_state = lp->lp_state;
341 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
343 struct ifnet *ifp = lp->lp_ifp;
345 /* Fill in the port index and system id (encoded as the MAC) */
346 info->mi_rq_port = htons(ifp->if_index);
347 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
348 info->mi_rq_xid = htonl(0);
352 lacp_xmit_lacpdu(struct lacp_port *lp)
354 struct lagg_port *lgp = lp->lp_lagg;
359 LACP_LOCK_ASSERT(lp->lp_lsc);
361 m = m_gethdr(M_NOWAIT, MT_DATA);
365 m->m_len = m->m_pkthdr.len = sizeof(*du);
367 du = mtod(m, struct lacpdu *);
368 memset(du, 0, sizeof(*du));
370 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
372 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
373 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
375 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
376 du->ldu_sph.sph_version = 1;
378 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
379 du->ldu_actor = lp->lp_actor;
381 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
382 sizeof(du->ldu_partner));
383 du->ldu_partner = lp->lp_partner;
385 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
386 sizeof(du->ldu_collector));
387 du->ldu_collector.lci_maxdelay = 0;
389 if (V_lacp_debug > 0) {
390 lacp_dprintf(lp, "lacpdu transmit\n");
391 lacp_dump_lacpdu(du);
394 m->m_flags |= M_MCAST;
397 * XXX should use higher priority queue.
398 * otherwise network congestion can break aggregation.
401 error = lagg_enqueue(lp->lp_ifp, m);
406 lacp_xmit_marker(struct lacp_port *lp)
408 struct lagg_port *lgp = lp->lp_lagg;
410 struct markerdu *mdu;
413 LACP_LOCK_ASSERT(lp->lp_lsc);
415 m = m_gethdr(M_NOWAIT, MT_DATA);
419 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
421 mdu = mtod(m, struct markerdu *);
422 memset(mdu, 0, sizeof(*mdu));
424 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
426 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
427 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
429 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
430 mdu->mdu_sph.sph_version = 1;
432 /* Bump the transaction id and copy over the marker info */
433 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
434 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
435 mdu->mdu_info = lp->lp_marker;
437 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
438 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
439 ntohl(mdu->mdu_info.mi_rq_xid)));
441 m->m_flags |= M_MCAST;
442 error = lagg_enqueue(lp->lp_ifp, m);
447 lacp_linkstate(struct lagg_port *lgp)
449 struct lacp_port *lp = LACP_PORT(lgp);
450 struct lacp_softc *lsc = lp->lp_lsc;
451 struct ifnet *ifp = lgp->lp_ifp;
452 struct ifmediareq ifmr;
458 bzero((char *)&ifmr, sizeof(ifmr));
459 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
464 media = ifmr.ifm_active;
465 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
466 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
467 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
468 old_state = lp->lp_state;
469 old_key = lp->lp_key;
471 lp->lp_media = media;
473 * If the port is not an active full duplex Ethernet link then it can
476 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
477 ifp->if_link_state != LINK_STATE_UP) {
478 lacp_port_disable(lp);
480 lacp_port_enable(lp);
482 lp->lp_key = lacp_compose_key(lp);
484 if (old_state != lp->lp_state || old_key != lp->lp_key) {
485 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
486 lp->lp_selected = LACP_UNSELECTED;
494 struct lacp_softc *lsc = arg;
495 struct lacp_port *lp;
497 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
498 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
501 CURVNET_SET(lp->lp_ifp->if_vnet);
507 lacp_sm_ptx_tx_schedule(lp);
510 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
514 lacp_port_create(struct lagg_port *lgp)
516 struct lagg_softc *sc = lgp->lp_softc;
517 struct lacp_softc *lsc = LACP_SOFTC(sc);
518 struct lacp_port *lp;
519 struct ifnet *ifp = lgp->lp_ifp;
520 struct sockaddr_dl sdl;
521 struct ifmultiaddr *rifma = NULL;
524 boolean_t active = TRUE; /* XXX should be configurable */
525 boolean_t fast = FALSE; /* Configurable via ioctl */
527 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
528 sdl.sdl_alen = ETHER_ADDR_LEN;
530 bcopy(ðermulticastaddr_slowprotocols,
531 LLADDR(&sdl), ETHER_ADDR_LEN);
532 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
534 printf("%s: ADDMULTI failed on %s\n", __func__,
535 lgp->lp_ifp->if_xname);
539 lp = malloc(sizeof(struct lacp_port),
540 M_DEVBUF, M_NOWAIT|M_ZERO);
551 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
553 lacp_fill_actorinfo(lp, &lp->lp_actor);
554 lacp_fill_markerinfo(lp, &lp->lp_marker);
556 (active ? LACP_STATE_ACTIVITY : 0) |
557 (fast ? LACP_STATE_TIMEOUT : 0);
558 lp->lp_aggregator = NULL;
559 lacp_sm_rx_set_expired(lp);
567 lacp_port_destroy(struct lagg_port *lgp)
569 struct lacp_port *lp = LACP_PORT(lgp);
570 struct lacp_softc *lsc = lp->lp_lsc;
574 for (i = 0; i < LACP_NTIMER; i++) {
575 LACP_TIMER_DISARM(lp, i);
578 lacp_disable_collecting(lp);
579 lacp_disable_distributing(lp);
582 LIST_REMOVE(lp, lp_next);
585 /* The address may have already been removed by if_purgemaddrs() */
586 if (!lgp->lp_detaching)
587 if_delmulti_ifma(lp->lp_ifma);
593 lacp_req(struct lagg_softc *sc, void *data)
595 struct lacp_opreq *req = (struct lacp_opreq *)data;
596 struct lacp_softc *lsc = LACP_SOFTC(sc);
597 struct lacp_aggregator *la;
599 bzero(req, sizeof(struct lacp_opreq));
602 * If the LACP softc is NULL, return with the opreq structure full of
603 * zeros. It is normal for the softc to be NULL while the lagg is
609 la = lsc->lsc_active_aggregator;
612 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
613 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
615 req->actor_key = ntohs(la->la_actor.lip_key);
616 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
617 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
618 req->actor_state = la->la_actor.lip_state;
620 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
621 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
623 req->partner_key = ntohs(la->la_partner.lip_key);
624 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
625 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
626 req->partner_state = la->la_partner.lip_state;
632 lacp_portreq(struct lagg_port *lgp, void *data)
634 struct lacp_opreq *req = (struct lacp_opreq *)data;
635 struct lacp_port *lp = LACP_PORT(lgp);
636 struct lacp_softc *lsc = lp->lp_lsc;
639 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
640 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
642 req->actor_key = ntohs(lp->lp_actor.lip_key);
643 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
644 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
645 req->actor_state = lp->lp_actor.lip_state;
647 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
648 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
650 req->partner_key = ntohs(lp->lp_partner.lip_key);
651 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
652 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
653 req->partner_state = lp->lp_partner.lip_state;
658 lacp_disable_collecting(struct lacp_port *lp)
660 LACP_DPRINTF((lp, "collecting disabled\n"));
661 lp->lp_state &= ~LACP_STATE_COLLECTING;
665 lacp_enable_collecting(struct lacp_port *lp)
667 LACP_DPRINTF((lp, "collecting enabled\n"));
668 lp->lp_state |= LACP_STATE_COLLECTING;
672 lacp_disable_distributing(struct lacp_port *lp)
674 struct lacp_aggregator *la = lp->lp_aggregator;
675 struct lacp_softc *lsc = lp->lp_lsc;
676 struct lagg_softc *sc = lsc->lsc_softc;
677 char buf[LACP_LAGIDSTR_MAX+1];
679 LACP_LOCK_ASSERT(lsc);
681 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
685 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
686 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
687 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
689 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
691 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
692 la->la_nports, la->la_nports - 1));
694 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
696 sc->sc_active = la->la_nports;
698 if (lsc->lsc_active_aggregator == la) {
699 lacp_suppress_distributing(lsc, la);
700 lacp_select_active_aggregator(lsc);
701 /* regenerate the port map, the active aggregator has changed */
702 lacp_update_portmap(lsc);
705 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
709 lacp_enable_distributing(struct lacp_port *lp)
711 struct lacp_aggregator *la = lp->lp_aggregator;
712 struct lacp_softc *lsc = lp->lp_lsc;
713 struct lagg_softc *sc = lsc->lsc_softc;
714 char buf[LACP_LAGIDSTR_MAX+1];
716 LACP_LOCK_ASSERT(lsc);
718 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
722 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
724 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
725 la->la_nports, la->la_nports + 1));
727 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
728 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
730 sc->sc_active = la->la_nports;
732 lp->lp_state |= LACP_STATE_DISTRIBUTING;
734 if (lsc->lsc_active_aggregator == la) {
735 lacp_suppress_distributing(lsc, la);
736 lacp_update_portmap(lsc);
738 /* try to become the active aggregator */
739 lacp_select_active_aggregator(lsc);
743 lacp_transit_expire(void *vp)
745 struct lacp_softc *lsc = vp;
747 LACP_LOCK_ASSERT(lsc);
749 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
753 lsc->lsc_suppress_distributing = FALSE;
757 lacp_attach(struct lagg_softc *sc)
759 struct lacp_softc *lsc;
761 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
766 lsc->lsc_hashkey = m_ether_tcpip_hash_init();
767 lsc->lsc_active_aggregator = NULL;
768 lsc->lsc_strict_mode = 1;
770 TAILQ_INIT(&lsc->lsc_aggregators);
771 LIST_INIT(&lsc->lsc_ports);
773 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
774 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
776 /* if the lagg is already up then do the same */
777 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
782 lacp_detach(void *psc)
784 struct lacp_softc *lsc = (struct lacp_softc *)psc;
786 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
787 ("aggregators still active"));
788 KASSERT(lsc->lsc_active_aggregator == NULL,
789 ("aggregator still attached"));
791 callout_drain(&lsc->lsc_transit_callout);
792 callout_drain(&lsc->lsc_callout);
794 LACP_LOCK_DESTROY(lsc);
799 lacp_init(struct lagg_softc *sc)
801 struct lacp_softc *lsc = LACP_SOFTC(sc);
804 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
809 lacp_stop(struct lagg_softc *sc)
811 struct lacp_softc *lsc = LACP_SOFTC(sc);
814 callout_stop(&lsc->lsc_transit_callout);
815 callout_stop(&lsc->lsc_callout);
820 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
822 struct lacp_softc *lsc = LACP_SOFTC(sc);
823 struct lacp_portmap *pm;
824 struct lacp_port *lp;
827 if (__predict_false(lsc->lsc_suppress_distributing)) {
828 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
832 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
833 if (pm->pm_count == 0) {
834 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
838 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
839 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
840 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
842 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
843 hash %= pm->pm_count;
844 lp = pm->pm_map[hash];
846 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
847 ("aggregated port is not distributing"));
849 return (lp->lp_lagg);
852 * lacp_suppress_distributing: drop transmit packets for a while
853 * to preserve packet ordering.
857 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
859 struct lacp_port *lp;
861 if (lsc->lsc_active_aggregator != la) {
867 lsc->lsc_suppress_distributing = TRUE;
869 /* send a marker frame down each port to verify the queues are empty */
870 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
871 lp->lp_flags |= LACP_PORT_MARK;
872 lacp_xmit_marker(lp);
875 /* set a timeout for the marker frames */
876 callout_reset(&lsc->lsc_transit_callout,
877 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
881 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
882 const struct lacp_peerinfo *b)
884 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
888 lacp_compare_systemid(const struct lacp_systemid *a,
889 const struct lacp_systemid *b)
891 return (memcmp(a, b, sizeof(*a)));
896 lacp_compare_portid(const struct lacp_portid *a,
897 const struct lacp_portid *b)
899 return (memcmp(a, b, sizeof(*a)));
904 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
906 struct lacp_port *lp;
909 lp = TAILQ_FIRST(&la->la_ports);
914 speed = ifmedia_baudrate(lp->lp_media);
915 speed *= la->la_nports;
917 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
918 lp->lp_media, la->la_nports));
925 * lacp_select_active_aggregator: select an aggregator to be used to transmit
926 * packets from lagg(4) interface.
930 lacp_select_active_aggregator(struct lacp_softc *lsc)
932 struct lacp_aggregator *la;
933 struct lacp_aggregator *best_la = NULL;
934 uint64_t best_speed = 0;
935 char buf[LACP_LAGIDSTR_MAX+1];
939 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
942 if (la->la_nports == 0) {
946 speed = lacp_aggregator_bandwidth(la);
947 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
948 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
949 speed, la->la_nports));
952 * This aggregator is chosen if the partner has a better
953 * system priority or, the total aggregated speed is higher
954 * or, it is already the chosen aggregator
956 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
957 LACP_SYS_PRI(best_la->la_partner)) ||
958 speed > best_speed ||
959 (speed == best_speed &&
960 la == lsc->lsc_active_aggregator)) {
966 KASSERT(best_la == NULL || best_la->la_nports > 0,
967 ("invalid aggregator refcnt"));
968 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
969 ("invalid aggregator list"));
971 if (lsc->lsc_active_aggregator != best_la) {
972 LACP_DPRINTF((NULL, "active aggregator changed\n"));
973 LACP_DPRINTF((NULL, "old %s\n",
974 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
977 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
979 LACP_DPRINTF((NULL, "new %s\n",
980 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
982 if (lsc->lsc_active_aggregator != best_la) {
983 lsc->lsc_active_aggregator = best_la;
984 lacp_update_portmap(lsc);
986 lacp_suppress_distributing(lsc, best_la);
992 * Updated the inactive portmap array with the new list of ports and
996 lacp_update_portmap(struct lacp_softc *lsc)
998 struct lagg_softc *sc = lsc->lsc_softc;
999 struct lacp_aggregator *la;
1000 struct lacp_portmap *p;
1001 struct lacp_port *lp;
1006 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1007 p = &lsc->lsc_pmap[newmap];
1008 la = lsc->lsc_active_aggregator;
1010 bzero(p, sizeof(struct lacp_portmap));
1012 if (la != NULL && la->la_nports > 0) {
1013 p->pm_count = la->la_nports;
1015 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
1016 p->pm_map[i++] = lp;
1017 KASSERT(i == p->pm_count, ("Invalid port count"));
1018 speed = lacp_aggregator_bandwidth(la);
1020 sc->sc_ifp->if_baudrate = speed;
1022 /* switch the active portmap over */
1023 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1024 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1026 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1030 lacp_compose_key(struct lacp_port *lp)
1032 struct lagg_port *lgp = lp->lp_lagg;
1033 struct lagg_softc *sc = lgp->lp_softc;
1034 u_int media = lp->lp_media;
1037 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1040 * non-aggregatable links should have unique keys.
1042 * XXX this isn't really unique as if_index is 16 bit.
1045 /* bit 0..14: (some bits of) if_index of this port */
1046 key = lp->lp_ifp->if_index;
1050 u_int subtype = IFM_SUBTYPE(media);
1052 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1053 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1055 /* bit 0..4: IFM_SUBTYPE modulo speed */
1077 case IFM_1000_SGMII:
1078 case IFM_1000_CX_SGMII:
1084 case IFM_10G_TWINAX:
1085 case IFM_10G_TWINAX_LONG:
1136 /* bit 5..14: (some bits of) if_index of lagg device */
1137 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1140 return (htons(key));
1144 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1146 char buf[LACP_LAGIDSTR_MAX+1];
1148 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1150 lacp_format_lagid(&la->la_actor, &la->la_partner,
1152 la->la_refcnt, la->la_refcnt + 1));
1154 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1156 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1160 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1162 char buf[LACP_LAGIDSTR_MAX+1];
1164 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1166 lacp_format_lagid(&la->la_actor, &la->la_partner,
1168 la->la_refcnt, la->la_refcnt - 1));
1170 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1172 if (la->la_refcnt > 0) {
1176 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1177 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1179 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1185 * lacp_aggregator_get: allocate an aggregator.
1188 static struct lacp_aggregator *
1189 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1191 struct lacp_aggregator *la;
1193 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1197 TAILQ_INIT(&la->la_ports);
1199 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1206 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1210 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1212 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1213 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1215 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1219 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1220 const struct lacp_peerinfo *lpi_port)
1222 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1223 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1224 lpi_aggr->lip_key = lpi_port->lip_key;
1228 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1232 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1233 const struct lacp_port *lp)
1235 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1236 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1240 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1244 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1248 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1256 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1257 const struct lacp_peerinfo *b)
1259 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1260 sizeof(a->lip_systemid))) {
1264 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1272 lacp_port_enable(struct lacp_port *lp)
1274 lp->lp_state |= LACP_STATE_AGGREGATION;
1278 lacp_port_disable(struct lacp_port *lp)
1280 lacp_set_mux(lp, LACP_MUX_DETACHED);
1282 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1283 lp->lp_selected = LACP_UNSELECTED;
1284 lacp_sm_rx_record_default(lp);
1285 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1286 lp->lp_state &= ~LACP_STATE_EXPIRED;
1290 * lacp_select: select an aggregator. create one if necessary.
1293 lacp_select(struct lacp_port *lp)
1295 struct lacp_softc *lsc = lp->lp_lsc;
1296 struct lacp_aggregator *la;
1297 char buf[LACP_LAGIDSTR_MAX+1];
1299 if (lp->lp_aggregator) {
1303 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1304 ("timer_wait_while still active"));
1306 LACP_DPRINTF((lp, "port lagid=%s\n",
1307 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1308 buf, sizeof(buf))));
1310 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1311 if (lacp_aggregator_is_compatible(la, lp)) {
1317 la = lacp_aggregator_get(lsc, lp);
1319 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1322 * will retry on the next tick.
1327 lacp_fill_aggregator_id(la, lp);
1328 LACP_DPRINTF((lp, "aggregator created\n"));
1330 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1331 if (la->la_refcnt == LACP_MAX_PORTS)
1333 lacp_aggregator_addref(lsc, la);
1336 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1337 lacp_format_lagid(&la->la_actor, &la->la_partner,
1338 buf, sizeof(buf))));
1340 lp->lp_aggregator = la;
1341 lp->lp_selected = LACP_SELECTED;
1345 * lacp_unselect: finish unselect/detach process.
1349 lacp_unselect(struct lacp_port *lp)
1351 struct lacp_softc *lsc = lp->lp_lsc;
1352 struct lacp_aggregator *la = lp->lp_aggregator;
1354 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1355 ("timer_wait_while still active"));
1361 lp->lp_aggregator = NULL;
1362 lacp_aggregator_delref(lsc, la);
1368 lacp_sm_mux(struct lacp_port *lp)
1370 struct lagg_port *lgp = lp->lp_lagg;
1371 struct lagg_softc *sc = lgp->lp_softc;
1372 enum lacp_mux_state new_state;
1374 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1375 boolean_t p_collecting =
1376 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1377 enum lacp_selected selected = lp->lp_selected;
1378 struct lacp_aggregator *la;
1380 if (V_lacp_debug > 1)
1381 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1382 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1383 lp->lp_mux_state, selected, p_sync, p_collecting);
1386 la = lp->lp_aggregator;
1387 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1388 ("MUX not detached"));
1389 new_state = lp->lp_mux_state;
1390 switch (lp->lp_mux_state) {
1391 case LACP_MUX_DETACHED:
1392 if (selected != LACP_UNSELECTED) {
1393 new_state = LACP_MUX_WAITING;
1396 case LACP_MUX_WAITING:
1397 KASSERT(la->la_pending > 0 ||
1398 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1399 ("timer_wait_while still active"));
1400 if (selected == LACP_SELECTED && la->la_pending == 0) {
1401 new_state = LACP_MUX_ATTACHED;
1402 } else if (selected == LACP_UNSELECTED) {
1403 new_state = LACP_MUX_DETACHED;
1406 case LACP_MUX_ATTACHED:
1407 if (selected == LACP_SELECTED && p_sync) {
1408 new_state = LACP_MUX_COLLECTING;
1409 } else if (selected != LACP_SELECTED) {
1410 new_state = LACP_MUX_DETACHED;
1413 case LACP_MUX_COLLECTING:
1414 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1415 new_state = LACP_MUX_DISTRIBUTING;
1416 } else if (selected != LACP_SELECTED || !p_sync) {
1417 new_state = LACP_MUX_ATTACHED;
1420 case LACP_MUX_DISTRIBUTING:
1421 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1422 new_state = LACP_MUX_COLLECTING;
1423 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1428 panic("%s: unknown state", __func__);
1431 if (lp->lp_mux_state == new_state) {
1435 lacp_set_mux(lp, new_state);
1440 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1442 struct lacp_aggregator *la = lp->lp_aggregator;
1444 if (lp->lp_mux_state == new_state) {
1448 switch (new_state) {
1449 case LACP_MUX_DETACHED:
1450 lp->lp_state &= ~LACP_STATE_SYNC;
1451 lacp_disable_distributing(lp);
1452 lacp_disable_collecting(lp);
1453 lacp_sm_assert_ntt(lp);
1455 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1456 KASSERT(la->la_pending > 0,
1457 ("timer_wait_while not active"));
1460 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1463 case LACP_MUX_WAITING:
1464 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1465 LACP_AGGREGATE_WAIT_TIME);
1468 case LACP_MUX_ATTACHED:
1469 lp->lp_state |= LACP_STATE_SYNC;
1470 lacp_disable_collecting(lp);
1471 lacp_sm_assert_ntt(lp);
1473 case LACP_MUX_COLLECTING:
1474 lacp_enable_collecting(lp);
1475 lacp_disable_distributing(lp);
1476 lacp_sm_assert_ntt(lp);
1478 case LACP_MUX_DISTRIBUTING:
1479 lacp_enable_distributing(lp);
1482 panic("%s: unknown state", __func__);
1485 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1487 lp->lp_mux_state = new_state;
1491 lacp_sm_mux_timer(struct lacp_port *lp)
1493 struct lacp_aggregator *la = lp->lp_aggregator;
1494 char buf[LACP_LAGIDSTR_MAX+1];
1496 KASSERT(la->la_pending > 0, ("no pending event"));
1498 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1499 lacp_format_lagid(&la->la_actor, &la->la_partner,
1501 la->la_pending, la->la_pending - 1));
1506 /* periodic transmit machine */
1509 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1511 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1512 LACP_STATE_TIMEOUT)) {
1516 LACP_DPRINTF((lp, "partner timeout changed\n"));
1519 * FAST_PERIODIC -> SLOW_PERIODIC
1521 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1523 * let lacp_sm_ptx_tx_schedule to update timeout.
1526 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1529 * if timeout has been shortened, assert NTT.
1532 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1533 lacp_sm_assert_ntt(lp);
1538 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1542 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1543 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1549 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1553 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1557 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1558 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1560 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1564 lacp_sm_ptx_timer(struct lacp_port *lp)
1566 lacp_sm_assert_ntt(lp);
1570 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1575 * check LACP_DISABLED first
1578 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1583 * check loopback condition.
1586 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1587 &lp->lp_actor.lip_systemid)) {
1592 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1595 lacp_sm_rx_update_selected(lp, du);
1596 lacp_sm_rx_update_ntt(lp, du);
1597 lacp_sm_rx_record_pdu(lp, du);
1599 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1600 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1601 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1603 lp->lp_state &= ~LACP_STATE_EXPIRED;
1606 * kick transmit machine without waiting the next tick.
1613 lacp_sm_rx_set_expired(struct lacp_port *lp)
1615 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1616 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1617 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1618 lp->lp_state |= LACP_STATE_EXPIRED;
1622 lacp_sm_rx_timer(struct lacp_port *lp)
1624 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1625 /* CURRENT -> EXPIRED */
1626 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1627 lacp_sm_rx_set_expired(lp);
1629 /* EXPIRED -> DEFAULTED */
1630 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1631 lacp_sm_rx_update_default_selected(lp);
1632 lacp_sm_rx_record_default(lp);
1633 lp->lp_state &= ~LACP_STATE_EXPIRED;
1638 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1642 char buf[LACP_STATESTR_MAX+1];
1646 oldpstate = lp->lp_partner.lip_state;
1648 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1649 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1650 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1652 lp->lp_partner = du->ldu_actor;
1654 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1655 LACP_STATE_AGGREGATION) &&
1656 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1657 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1660 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1663 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1665 if (oldpstate != lp->lp_partner.lip_state) {
1666 LACP_DPRINTF((lp, "old pstate %s\n",
1667 lacp_format_state(oldpstate, buf, sizeof(buf))));
1668 LACP_DPRINTF((lp, "new pstate %s\n",
1669 lacp_format_state(lp->lp_partner.lip_state, buf,
1673 /* XXX Hack, still need to implement 5.4.9 para 2,3,4 */
1674 if (lp->lp_lsc->lsc_strict_mode)
1675 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1677 lacp_sm_ptx_update_timeout(lp, oldpstate);
1681 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1686 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1687 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1688 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1689 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1690 lacp_sm_assert_ntt(lp);
1695 lacp_sm_rx_record_default(struct lacp_port *lp)
1701 oldpstate = lp->lp_partner.lip_state;
1702 if (lp->lp_lsc->lsc_strict_mode)
1703 lp->lp_partner = lacp_partner_admin_strict;
1705 lp->lp_partner = lacp_partner_admin_optimistic;;
1706 lp->lp_state |= LACP_STATE_DEFAULTED;
1707 lacp_sm_ptx_update_timeout(lp, oldpstate);
1711 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1712 const struct lacp_peerinfo *info)
1717 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1718 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1719 LACP_STATE_AGGREGATION)) {
1720 lp->lp_selected = LACP_UNSELECTED;
1721 /* mux machine will clean up lp->lp_aggregator */
1726 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1731 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1735 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1740 if (lp->lp_lsc->lsc_strict_mode)
1741 lacp_sm_rx_update_selected_from_peerinfo(lp,
1742 &lacp_partner_admin_strict);
1744 lacp_sm_rx_update_selected_from_peerinfo(lp,
1745 &lacp_partner_admin_optimistic);
1748 /* transmit machine */
1751 lacp_sm_tx(struct lacp_port *lp)
1755 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1757 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1758 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1761 lp->lp_flags &= ~LACP_PORT_NTT;
1764 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1768 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1769 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1770 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1771 LACP_DPRINTF((lp, "rate limited pdu\n"));
1775 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1776 error = lacp_xmit_lacpdu(lp);
1778 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1782 lp->lp_flags &= ~LACP_PORT_NTT;
1784 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1790 lacp_sm_assert_ntt(struct lacp_port *lp)
1793 lp->lp_flags |= LACP_PORT_NTT;
1797 lacp_run_timers(struct lacp_port *lp)
1801 for (i = 0; i < LACP_NTIMER; i++) {
1802 KASSERT(lp->lp_timer[i] >= 0,
1803 ("invalid timer value %d", lp->lp_timer[i]));
1804 if (lp->lp_timer[i] == 0) {
1806 } else if (--lp->lp_timer[i] <= 0) {
1807 if (lacp_timer_funcs[i]) {
1808 (*lacp_timer_funcs[i])(lp);
1815 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1817 struct lacp_softc *lsc = lp->lp_lsc;
1818 struct lagg_port *lgp = lp->lp_lagg;
1819 struct lacp_port *lp2;
1820 struct markerdu *mdu;
1824 if (m->m_pkthdr.len != sizeof(*mdu)) {
1828 if ((m->m_flags & M_MCAST) == 0) {
1832 if (m->m_len < sizeof(*mdu)) {
1833 m = m_pullup(m, sizeof(*mdu));
1839 mdu = mtod(m, struct markerdu *);
1841 if (memcmp(&mdu->mdu_eh.ether_dhost,
1842 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1846 if (mdu->mdu_sph.sph_version != 1) {
1850 switch (mdu->mdu_tlv.tlv_type) {
1851 case MARKER_TYPE_INFO:
1852 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1853 marker_info_tlv_template, TRUE)) {
1856 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1857 memcpy(&mdu->mdu_eh.ether_dhost,
1858 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1859 memcpy(&mdu->mdu_eh.ether_shost,
1860 lgp->lp_lladdr, ETHER_ADDR_LEN);
1861 error = lagg_enqueue(lp->lp_ifp, m);
1864 case MARKER_TYPE_RESPONSE:
1865 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1866 marker_response_tlv_template, TRUE)) {
1869 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
1870 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
1871 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
1873 /* Verify that it is the last marker we sent out */
1874 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
1875 sizeof(struct lacp_markerinfo)))
1879 lp->lp_flags &= ~LACP_PORT_MARK;
1881 if (lsc->lsc_suppress_distributing) {
1882 /* Check if any ports are waiting for a response */
1883 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
1884 if (lp2->lp_flags & LACP_PORT_MARK) {
1891 /* All interface queues are clear */
1892 LACP_DPRINTF((NULL, "queue flush complete\n"));
1893 lsc->lsc_suppress_distributing = FALSE;
1907 LACP_DPRINTF((lp, "bad marker frame\n"));
1913 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
1914 const struct tlv_template *tmpl, boolean_t check_type)
1916 while (/* CONSTCOND */ 1) {
1917 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
1920 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
1921 tlv->tlv_length != tmpl->tmpl_length) {
1924 if (tmpl->tmpl_type == 0) {
1927 tlv = (const struct tlvhdr *)
1928 ((const char *)tlv + tlv->tlv_length);
1937 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
1939 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
1951 lacp_format_systemid(const struct lacp_systemid *sysid,
1952 char *buf, size_t buflen)
1954 char macbuf[LACP_MACSTR_MAX+1];
1956 snprintf(buf, buflen, "%04X,%s",
1957 ntohs(sysid->lsi_prio),
1958 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
1964 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
1966 snprintf(buf, buflen, "%04X,%04X",
1967 ntohs(portid->lpi_prio),
1968 ntohs(portid->lpi_portno));
1974 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
1976 char sysid[LACP_SYSTEMIDSTR_MAX+1];
1977 char portid[LACP_PORTIDSTR_MAX+1];
1979 snprintf(buf, buflen, "(%s,%04X,%s)",
1980 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
1981 ntohs(peer->lip_key),
1982 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
1988 lacp_format_lagid(const struct lacp_peerinfo *a,
1989 const struct lacp_peerinfo *b, char *buf, size_t buflen)
1991 char astr[LACP_PARTNERSTR_MAX+1];
1992 char bstr[LACP_PARTNERSTR_MAX+1];
1996 * there's a convention to display small numbered peer
2000 if (lacp_compare_peerinfo(a, b) > 0) {
2001 const struct lacp_peerinfo *t;
2009 snprintf(buf, buflen, "[%s,%s]",
2010 lacp_format_partner(a, astr, sizeof(astr)),
2011 lacp_format_partner(b, bstr, sizeof(bstr)));
2017 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2018 char *buf, size_t buflen)
2024 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2028 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2030 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2035 lacp_dump_lacpdu(const struct lacpdu *du)
2037 char buf[LACP_PARTNERSTR_MAX+1];
2038 char buf2[LACP_STATESTR_MAX+1];
2040 printf("actor=%s\n",
2041 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2042 printf("actor.state=%s\n",
2043 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2044 printf("partner=%s\n",
2045 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2046 printf("partner.state=%s\n",
2047 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2049 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2053 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2058 printf("%s: ", lp->lp_ifp->if_xname);