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 <machine/stdarg.h>
43 #include <sys/rwlock.h>
46 #include <net/if_dl.h>
47 #include <net/ethernet.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
51 #include <net/if_lagg.h>
52 #include <net/ieee8023ad_lacp.h>
55 * actor system priority and port priority.
56 * XXX should be configurable.
59 #define LACP_SYSTEM_PRIO 0x8000
60 #define LACP_PORT_PRIO 0x8000
62 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
63 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
65 static const struct tlv_template lacp_info_tlv_template[] = {
66 { LACP_TYPE_ACTORINFO,
67 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
68 { LACP_TYPE_PARTNERINFO,
69 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
70 { LACP_TYPE_COLLECTORINFO,
71 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
75 static const struct tlv_template marker_info_tlv_template[] = {
77 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
81 static const struct tlv_template marker_response_tlv_template[] = {
82 { MARKER_TYPE_RESPONSE,
83 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
87 typedef void (*lacp_timer_func_t)(struct lacp_port *);
89 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
90 static void lacp_fill_markerinfo(struct lacp_port *,
91 struct lacp_markerinfo *);
93 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
94 static void lacp_suppress_distributing(struct lacp_softc *,
95 struct lacp_aggregator *);
96 static void lacp_transit_expire(void *);
97 static void lacp_update_portmap(struct lacp_softc *);
98 static void lacp_select_active_aggregator(struct lacp_softc *);
99 static uint16_t lacp_compose_key(struct lacp_port *);
100 static int tlv_check(const void *, size_t, const struct tlvhdr *,
101 const struct tlv_template *, boolean_t);
102 static void lacp_tick(void *);
104 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
105 const struct lacp_port *);
106 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
107 const struct lacp_peerinfo *);
108 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
109 const struct lacp_port *);
110 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
111 const struct lacp_peerinfo *);
113 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
115 static void lacp_aggregator_addref(struct lacp_softc *,
116 struct lacp_aggregator *);
117 static void lacp_aggregator_delref(struct lacp_softc *,
118 struct lacp_aggregator *);
120 /* receive machine */
122 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
123 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
124 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
125 static void lacp_sm_rx_timer(struct lacp_port *);
126 static void lacp_sm_rx_set_expired(struct lacp_port *);
127 static void lacp_sm_rx_update_ntt(struct lacp_port *,
128 const struct lacpdu *);
129 static void lacp_sm_rx_record_pdu(struct lacp_port *,
130 const struct lacpdu *);
131 static void lacp_sm_rx_update_selected(struct lacp_port *,
132 const struct lacpdu *);
133 static void lacp_sm_rx_record_default(struct lacp_port *);
134 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
135 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
136 const struct lacp_peerinfo *);
140 static void lacp_sm_mux(struct lacp_port *);
141 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
142 static void lacp_sm_mux_timer(struct lacp_port *);
144 /* periodic transmit machine */
146 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
147 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
148 static void lacp_sm_ptx_timer(struct lacp_port *);
150 /* transmit machine */
152 static void lacp_sm_tx(struct lacp_port *);
153 static void lacp_sm_assert_ntt(struct lacp_port *);
155 static void lacp_run_timers(struct lacp_port *);
156 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
157 const struct lacp_peerinfo *);
158 static int lacp_compare_systemid(const struct lacp_systemid *,
159 const struct lacp_systemid *);
160 static void lacp_port_enable(struct lacp_port *);
161 static void lacp_port_disable(struct lacp_port *);
162 static void lacp_select(struct lacp_port *);
163 static void lacp_unselect(struct lacp_port *);
164 static void lacp_disable_collecting(struct lacp_port *);
165 static void lacp_enable_collecting(struct lacp_port *);
166 static void lacp_disable_distributing(struct lacp_port *);
167 static void lacp_enable_distributing(struct lacp_port *);
168 static int lacp_xmit_lacpdu(struct lacp_port *);
169 static int lacp_xmit_marker(struct lacp_port *);
171 #if defined(LACP_DEBUG)
172 static void lacp_dump_lacpdu(const struct lacpdu *);
173 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
175 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
176 const struct lacp_peerinfo *, char *, size_t);
177 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
179 static const char *lacp_format_state(uint8_t, char *, size_t);
180 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
181 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
183 static const char *lacp_format_portid(const struct lacp_portid *, char *,
185 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
186 __attribute__((__format__(__printf__, 2, 3)));
187 #define LACP_DPRINTF(a) lacp_dprintf a
189 #define LACP_DPRINTF(a) /* nothing */
193 * partner administration variables.
194 * XXX should be configurable.
197 static const struct lacp_peerinfo lacp_partner_admin = {
198 .lip_systemid = { .lsi_prio = 0xffff },
199 .lip_portid = { .lpi_prio = 0xffff },
202 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
203 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
210 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
211 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
212 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
213 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
217 lacp_input(struct lagg_port *lgp, struct mbuf *m)
219 struct lacp_port *lp = LACP_PORT(lgp);
222 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
227 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
229 case SLOWPROTOCOLS_SUBTYPE_LACP:
230 lacp_pdu_input(lp, m);
233 case SLOWPROTOCOLS_SUBTYPE_MARKER:
234 lacp_marker_input(lp, m);
238 /* Not a subtype we are interested in */
243 * lacp_pdu_input: process lacpdu
246 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
248 struct lacp_softc *lsc = lp->lp_lsc;
252 if (m->m_pkthdr.len != sizeof(*du)) {
256 if ((m->m_flags & M_MCAST) == 0) {
260 if (m->m_len < sizeof(*du)) {
261 m = m_pullup(m, sizeof(*du));
267 du = mtod(m, struct lacpdu *);
269 if (memcmp(&du->ldu_eh.ether_dhost,
270 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
275 * ignore the version for compatibility with
276 * the future protocol revisions.
279 if (du->ldu_sph.sph_version != 1) {
285 * ignore tlv types for compatibility with
286 * the future protocol revisions.
288 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
289 lacp_info_tlv_template, FALSE)) {
293 #if defined(LACP_DEBUG)
294 LACP_DPRINTF((lp, "lacpdu receive\n"));
295 lacp_dump_lacpdu(du);
296 #endif /* defined(LACP_DEBUG) */
311 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
313 struct lagg_port *lgp = lp->lp_lagg;
314 struct lagg_softc *sc = lgp->lp_softc;
316 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
317 memcpy(&info->lip_systemid.lsi_mac,
318 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
319 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
320 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
321 info->lip_state = lp->lp_state;
325 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
327 struct ifnet *ifp = lp->lp_ifp;
329 /* Fill in the port index and system id (encoded as the MAC) */
330 info->mi_rq_port = htons(ifp->if_index);
331 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
332 info->mi_rq_xid = htonl(0);
336 lacp_xmit_lacpdu(struct lacp_port *lp)
338 struct lagg_port *lgp = lp->lp_lagg;
343 LACP_LOCK_ASSERT(lp->lp_lsc);
345 m = m_gethdr(M_DONTWAIT, MT_DATA);
349 m->m_len = m->m_pkthdr.len = sizeof(*du);
351 du = mtod(m, struct lacpdu *);
352 memset(du, 0, sizeof(*du));
354 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
356 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
357 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
359 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
360 du->ldu_sph.sph_version = 1;
362 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
363 du->ldu_actor = lp->lp_actor;
365 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
366 sizeof(du->ldu_partner));
367 du->ldu_partner = lp->lp_partner;
369 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
370 sizeof(du->ldu_collector));
371 du->ldu_collector.lci_maxdelay = 0;
373 #if defined(LACP_DEBUG)
374 LACP_DPRINTF((lp, "lacpdu transmit\n"));
375 lacp_dump_lacpdu(du);
376 #endif /* defined(LACP_DEBUG) */
378 m->m_flags |= M_MCAST;
381 * XXX should use higher priority queue.
382 * otherwise network congestion can break aggregation.
385 error = lagg_enqueue(lp->lp_ifp, m);
390 lacp_xmit_marker(struct lacp_port *lp)
392 struct lagg_port *lgp = lp->lp_lagg;
394 struct markerdu *mdu;
397 LACP_LOCK_ASSERT(lp->lp_lsc);
399 m = m_gethdr(M_DONTWAIT, MT_DATA);
403 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
405 mdu = mtod(m, struct markerdu *);
406 memset(mdu, 0, sizeof(*mdu));
408 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
410 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
411 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
413 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
414 mdu->mdu_sph.sph_version = 1;
416 /* Bump the transaction id and copy over the marker info */
417 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
418 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
419 mdu->mdu_info = lp->lp_marker;
421 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
422 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
423 ntohl(mdu->mdu_info.mi_rq_xid)));
425 m->m_flags |= M_MCAST;
426 error = lagg_enqueue(lp->lp_ifp, m);
431 lacp_linkstate(struct lagg_port *lgp)
433 struct lacp_port *lp = LACP_PORT(lgp);
434 struct lacp_softc *lsc = lp->lp_lsc;
435 struct ifnet *ifp = lgp->lp_ifp;
436 struct ifmediareq ifmr;
442 bzero((char *)&ifmr, sizeof(ifmr));
443 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
448 media = ifmr.ifm_active;
449 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
450 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
451 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
452 old_state = lp->lp_state;
453 old_key = lp->lp_key;
455 lp->lp_media = media;
457 * If the port is not an active full duplex Ethernet link then it can
460 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
461 ifp->if_link_state != LINK_STATE_UP) {
462 lacp_port_disable(lp);
464 lacp_port_enable(lp);
466 lp->lp_key = lacp_compose_key(lp);
468 if (old_state != lp->lp_state || old_key != lp->lp_key) {
469 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
470 lp->lp_selected = LACP_UNSELECTED;
478 struct lacp_softc *lsc = arg;
479 struct lacp_port *lp;
481 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
482 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
490 lacp_sm_ptx_tx_schedule(lp);
492 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
496 lacp_port_create(struct lagg_port *lgp)
498 struct lagg_softc *sc = lgp->lp_softc;
499 struct lacp_softc *lsc = LACP_SOFTC(sc);
500 struct lacp_port *lp;
501 struct ifnet *ifp = lgp->lp_ifp;
502 struct sockaddr_dl sdl;
503 struct ifmultiaddr *rifma = NULL;
506 boolean_t active = TRUE; /* XXX should be configurable */
507 boolean_t fast = FALSE; /* XXX should be configurable */
509 bzero((char *)&sdl, sizeof(sdl));
510 sdl.sdl_len = sizeof(sdl);
511 sdl.sdl_family = AF_LINK;
512 sdl.sdl_index = ifp->if_index;
513 sdl.sdl_type = IFT_ETHER;
514 sdl.sdl_alen = ETHER_ADDR_LEN;
516 bcopy(ðermulticastaddr_slowprotocols,
517 LLADDR(&sdl), ETHER_ADDR_LEN);
518 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
520 printf("%s: ADDMULTI failed on %s\n", __func__, lgp->lp_ifname);
524 lp = malloc(sizeof(struct lacp_port),
525 M_DEVBUF, M_NOWAIT|M_ZERO);
530 lgp->lp_psc = (caddr_t)lp;
536 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
538 lacp_fill_actorinfo(lp, &lp->lp_actor);
539 lacp_fill_markerinfo(lp, &lp->lp_marker);
541 (active ? LACP_STATE_ACTIVITY : 0) |
542 (fast ? LACP_STATE_TIMEOUT : 0);
543 lp->lp_aggregator = NULL;
544 lacp_sm_rx_set_expired(lp);
552 lacp_port_destroy(struct lagg_port *lgp)
554 struct lacp_port *lp = LACP_PORT(lgp);
555 struct lacp_softc *lsc = lp->lp_lsc;
559 for (i = 0; i < LACP_NTIMER; i++) {
560 LACP_TIMER_DISARM(lp, i);
563 lacp_disable_collecting(lp);
564 lacp_disable_distributing(lp);
567 /* The address may have already been removed by if_purgemaddrs() */
568 if (!lgp->lp_detaching)
569 if_delmulti_ifma(lp->lp_ifma);
571 LIST_REMOVE(lp, lp_next);
577 lacp_req(struct lagg_softc *sc, caddr_t data)
579 struct lacp_opreq *req = (struct lacp_opreq *)data;
580 struct lacp_softc *lsc = LACP_SOFTC(sc);
581 struct lacp_aggregator *la = lsc->lsc_active_aggregator;
584 bzero(req, sizeof(struct lacp_opreq));
586 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
587 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
589 req->actor_key = ntohs(la->la_actor.lip_key);
590 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
591 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
592 req->actor_state = la->la_actor.lip_state;
594 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
595 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
597 req->partner_key = ntohs(la->la_partner.lip_key);
598 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
599 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
600 req->partner_state = la->la_partner.lip_state;
606 lacp_portreq(struct lagg_port *lgp, caddr_t data)
608 struct lacp_opreq *req = (struct lacp_opreq *)data;
609 struct lacp_port *lp = LACP_PORT(lgp);
610 struct lacp_softc *lsc = lp->lp_lsc;
613 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
614 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
616 req->actor_key = ntohs(lp->lp_actor.lip_key);
617 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
618 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
619 req->actor_state = lp->lp_actor.lip_state;
621 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
622 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
624 req->partner_key = ntohs(lp->lp_partner.lip_key);
625 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
626 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
627 req->partner_state = lp->lp_partner.lip_state;
632 lacp_disable_collecting(struct lacp_port *lp)
634 LACP_DPRINTF((lp, "collecting disabled\n"));
635 lp->lp_state &= ~LACP_STATE_COLLECTING;
639 lacp_enable_collecting(struct lacp_port *lp)
641 LACP_DPRINTF((lp, "collecting enabled\n"));
642 lp->lp_state |= LACP_STATE_COLLECTING;
646 lacp_disable_distributing(struct lacp_port *lp)
648 struct lacp_aggregator *la = lp->lp_aggregator;
649 struct lacp_softc *lsc = lp->lp_lsc;
650 #if defined(LACP_DEBUG)
651 char buf[LACP_LAGIDSTR_MAX+1];
652 #endif /* defined(LACP_DEBUG) */
654 LACP_LOCK_ASSERT(lsc);
656 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
660 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
661 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
662 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
664 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
666 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
667 la->la_nports, la->la_nports - 1));
669 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
672 if (lsc->lsc_active_aggregator == la) {
673 lacp_suppress_distributing(lsc, la);
674 lacp_select_active_aggregator(lsc);
675 /* regenerate the port map, the active aggregator has changed */
676 lacp_update_portmap(lsc);
679 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
683 lacp_enable_distributing(struct lacp_port *lp)
685 struct lacp_aggregator *la = lp->lp_aggregator;
686 struct lacp_softc *lsc = lp->lp_lsc;
687 #if defined(LACP_DEBUG)
688 char buf[LACP_LAGIDSTR_MAX+1];
689 #endif /* defined(LACP_DEBUG) */
691 LACP_LOCK_ASSERT(lsc);
693 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
697 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
699 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
700 la->la_nports, la->la_nports + 1));
702 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
703 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
706 lp->lp_state |= LACP_STATE_DISTRIBUTING;
708 if (lsc->lsc_active_aggregator == la) {
709 lacp_suppress_distributing(lsc, la);
710 lacp_update_portmap(lsc);
712 /* try to become the active aggregator */
713 lacp_select_active_aggregator(lsc);
717 lacp_transit_expire(void *vp)
719 struct lacp_softc *lsc = vp;
721 LACP_LOCK_ASSERT(lsc);
723 LACP_DPRINTF((NULL, "%s\n", __func__));
724 lsc->lsc_suppress_distributing = FALSE;
728 lacp_attach(struct lagg_softc *sc)
730 struct lacp_softc *lsc;
732 lsc = malloc(sizeof(struct lacp_softc),
733 M_DEVBUF, M_NOWAIT|M_ZERO);
737 sc->sc_psc = (caddr_t)lsc;
740 lsc->lsc_hashkey = arc4random();
741 lsc->lsc_active_aggregator = NULL;
743 TAILQ_INIT(&lsc->lsc_aggregators);
744 LIST_INIT(&lsc->lsc_ports);
746 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
747 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
749 /* if the lagg is already up then do the same */
750 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
757 lacp_detach(struct lagg_softc *sc)
759 struct lacp_softc *lsc = LACP_SOFTC(sc);
761 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
762 ("aggregators still active"));
763 KASSERT(lsc->lsc_active_aggregator == NULL,
764 ("aggregator still attached"));
767 callout_drain(&lsc->lsc_transit_callout);
768 callout_drain(&lsc->lsc_callout);
770 LACP_LOCK_DESTROY(lsc);
776 lacp_init(struct lagg_softc *sc)
778 struct lacp_softc *lsc = LACP_SOFTC(sc);
781 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
786 lacp_stop(struct lagg_softc *sc)
788 struct lacp_softc *lsc = LACP_SOFTC(sc);
791 callout_stop(&lsc->lsc_transit_callout);
792 callout_stop(&lsc->lsc_callout);
797 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
799 struct lacp_softc *lsc = LACP_SOFTC(sc);
800 struct lacp_portmap *pm;
801 struct lacp_port *lp;
804 if (__predict_false(lsc->lsc_suppress_distributing)) {
805 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
809 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
810 if (pm->pm_count == 0) {
811 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
815 if (sc->use_flowid && (m->m_flags & M_FLOWID))
816 hash = m->m_pkthdr.flowid;
818 hash = lagg_hashmbuf(sc, m, lsc->lsc_hashkey);
819 hash %= pm->pm_count;
820 lp = pm->pm_map[hash];
822 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
823 ("aggregated port is not distributing"));
825 return (lp->lp_lagg);
828 * lacp_suppress_distributing: drop transmit packets for a while
829 * to preserve packet ordering.
833 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
835 struct lacp_port *lp;
837 if (lsc->lsc_active_aggregator != la) {
841 LACP_DPRINTF((NULL, "%s\n", __func__));
842 lsc->lsc_suppress_distributing = TRUE;
844 /* send a marker frame down each port to verify the queues are empty */
845 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
846 lp->lp_flags |= LACP_PORT_MARK;
847 lacp_xmit_marker(lp);
850 /* set a timeout for the marker frames */
851 callout_reset(&lsc->lsc_transit_callout,
852 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
856 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
857 const struct lacp_peerinfo *b)
859 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
863 lacp_compare_systemid(const struct lacp_systemid *a,
864 const struct lacp_systemid *b)
866 return (memcmp(a, b, sizeof(*a)));
871 lacp_compare_portid(const struct lacp_portid *a,
872 const struct lacp_portid *b)
874 return (memcmp(a, b, sizeof(*a)));
879 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
881 struct lacp_port *lp;
884 lp = TAILQ_FIRST(&la->la_ports);
889 speed = ifmedia_baudrate(lp->lp_media);
890 speed *= la->la_nports;
892 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
893 lp->lp_media, la->la_nports));
900 * lacp_select_active_aggregator: select an aggregator to be used to transmit
901 * packets from lagg(4) interface.
905 lacp_select_active_aggregator(struct lacp_softc *lsc)
907 struct lagg_softc *sc = lsc->lsc_softc;
908 struct lacp_aggregator *la;
909 struct lacp_aggregator *best_la = NULL;
910 uint64_t best_speed = 0;
911 #if defined(LACP_DEBUG)
912 char buf[LACP_LAGIDSTR_MAX+1];
913 #endif /* defined(LACP_DEBUG) */
915 LACP_DPRINTF((NULL, "%s:\n", __func__));
917 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
920 if (la->la_nports == 0) {
924 speed = lacp_aggregator_bandwidth(la);
925 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
926 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
927 speed, la->la_nports));
929 /* This aggregator is chosen if
930 * the partner has a better system priority
931 * or, the total aggregated speed is higher
932 * or, it is already the chosen aggregator
934 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
935 LACP_SYS_PRI(best_la->la_partner)) ||
936 speed > best_speed ||
937 (speed == best_speed &&
938 la == lsc->lsc_active_aggregator)) {
944 KASSERT(best_la == NULL || best_la->la_nports > 0,
945 ("invalid aggregator refcnt"));
946 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
947 ("invalid aggregator list"));
949 #if defined(LACP_DEBUG)
950 if (lsc->lsc_active_aggregator != best_la) {
951 LACP_DPRINTF((NULL, "active aggregator changed\n"));
952 LACP_DPRINTF((NULL, "old %s\n",
953 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
956 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
958 LACP_DPRINTF((NULL, "new %s\n",
959 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
960 #endif /* defined(LACP_DEBUG) */
962 if (lsc->lsc_active_aggregator != best_la) {
963 sc->sc_ifp->if_baudrate = best_speed;
964 lsc->lsc_active_aggregator = best_la;
965 lacp_update_portmap(lsc);
967 lacp_suppress_distributing(lsc, best_la);
973 * Updated the inactive portmap array with the new list of ports and
977 lacp_update_portmap(struct lacp_softc *lsc)
979 struct lacp_aggregator *la;
980 struct lacp_portmap *p;
981 struct lacp_port *lp;
985 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
986 p = &lsc->lsc_pmap[newmap];
987 la = lsc->lsc_active_aggregator;
988 bzero(p, sizeof(struct lacp_portmap));
990 if (la != NULL && la->la_nports > 0) {
991 p->pm_count = la->la_nports;
993 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
995 KASSERT(i == p->pm_count, ("Invalid port count"));
998 /* switch the active portmap over */
999 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1000 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1002 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1006 lacp_compose_key(struct lacp_port *lp)
1008 struct lagg_port *lgp = lp->lp_lagg;
1009 struct lagg_softc *sc = lgp->lp_softc;
1010 u_int media = lp->lp_media;
1013 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1016 * non-aggregatable links should have unique keys.
1018 * XXX this isn't really unique as if_index is 16 bit.
1021 /* bit 0..14: (some bits of) if_index of this port */
1022 key = lp->lp_ifp->if_index;
1026 u_int subtype = IFM_SUBTYPE(media);
1028 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1029 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1031 /* bit 0..4: IFM_SUBTYPE modulo speed */
1056 case IFM_10G_TWINAX:
1057 case IFM_10G_TWINAX_LONG:
1065 /* bit 5..14: (some bits of) if_index of lagg device */
1066 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1069 return (htons(key));
1073 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1075 #if defined(LACP_DEBUG)
1076 char buf[LACP_LAGIDSTR_MAX+1];
1079 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1081 lacp_format_lagid(&la->la_actor, &la->la_partner,
1083 la->la_refcnt, la->la_refcnt + 1));
1085 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1087 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1091 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1093 #if defined(LACP_DEBUG)
1094 char buf[LACP_LAGIDSTR_MAX+1];
1097 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1099 lacp_format_lagid(&la->la_actor, &la->la_partner,
1101 la->la_refcnt, la->la_refcnt - 1));
1103 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1105 if (la->la_refcnt > 0) {
1109 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1110 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1112 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1118 * lacp_aggregator_get: allocate an aggregator.
1121 static struct lacp_aggregator *
1122 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1124 struct lacp_aggregator *la;
1126 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1130 TAILQ_INIT(&la->la_ports);
1132 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1139 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1143 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1145 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1146 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1148 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1152 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1153 const struct lacp_peerinfo *lpi_port)
1155 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1156 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1157 lpi_aggr->lip_key = lpi_port->lip_key;
1161 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1165 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1166 const struct lacp_port *lp)
1168 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1169 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1173 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1177 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1181 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1189 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1190 const struct lacp_peerinfo *b)
1192 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1193 sizeof(a->lip_systemid))) {
1197 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1205 lacp_port_enable(struct lacp_port *lp)
1207 lp->lp_state |= LACP_STATE_AGGREGATION;
1211 lacp_port_disable(struct lacp_port *lp)
1213 lacp_set_mux(lp, LACP_MUX_DETACHED);
1215 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1216 lp->lp_selected = LACP_UNSELECTED;
1217 lacp_sm_rx_record_default(lp);
1218 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1219 lp->lp_state &= ~LACP_STATE_EXPIRED;
1223 * lacp_select: select an aggregator. create one if necessary.
1226 lacp_select(struct lacp_port *lp)
1228 struct lacp_softc *lsc = lp->lp_lsc;
1229 struct lacp_aggregator *la;
1230 #if defined(LACP_DEBUG)
1231 char buf[LACP_LAGIDSTR_MAX+1];
1234 if (lp->lp_aggregator) {
1238 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1239 ("timer_wait_while still active"));
1241 LACP_DPRINTF((lp, "port lagid=%s\n",
1242 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1243 buf, sizeof(buf))));
1245 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1246 if (lacp_aggregator_is_compatible(la, lp)) {
1252 la = lacp_aggregator_get(lsc, lp);
1254 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1257 * will retry on the next tick.
1262 lacp_fill_aggregator_id(la, lp);
1263 LACP_DPRINTF((lp, "aggregator created\n"));
1265 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1266 if (la->la_refcnt == LACP_MAX_PORTS)
1268 lacp_aggregator_addref(lsc, la);
1271 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1272 lacp_format_lagid(&la->la_actor, &la->la_partner,
1273 buf, sizeof(buf))));
1275 lp->lp_aggregator = la;
1276 lp->lp_selected = LACP_SELECTED;
1280 * lacp_unselect: finish unselect/detach process.
1284 lacp_unselect(struct lacp_port *lp)
1286 struct lacp_softc *lsc = lp->lp_lsc;
1287 struct lacp_aggregator *la = lp->lp_aggregator;
1289 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1290 ("timer_wait_while still active"));
1296 lp->lp_aggregator = NULL;
1297 lacp_aggregator_delref(lsc, la);
1303 lacp_sm_mux(struct lacp_port *lp)
1305 enum lacp_mux_state new_state;
1307 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1308 boolean_t p_collecting =
1309 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1310 enum lacp_selected selected = lp->lp_selected;
1311 struct lacp_aggregator *la;
1313 /* LACP_DPRINTF((lp, "%s: state %d\n", __func__, lp->lp_mux_state)); */
1316 la = lp->lp_aggregator;
1317 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1318 ("MUX not detached"));
1319 new_state = lp->lp_mux_state;
1320 switch (lp->lp_mux_state) {
1321 case LACP_MUX_DETACHED:
1322 if (selected != LACP_UNSELECTED) {
1323 new_state = LACP_MUX_WAITING;
1326 case LACP_MUX_WAITING:
1327 KASSERT(la->la_pending > 0 ||
1328 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1329 ("timer_wait_while still active"));
1330 if (selected == LACP_SELECTED && la->la_pending == 0) {
1331 new_state = LACP_MUX_ATTACHED;
1332 } else if (selected == LACP_UNSELECTED) {
1333 new_state = LACP_MUX_DETACHED;
1336 case LACP_MUX_ATTACHED:
1337 if (selected == LACP_SELECTED && p_sync) {
1338 new_state = LACP_MUX_COLLECTING;
1339 } else if (selected != LACP_SELECTED) {
1340 new_state = LACP_MUX_DETACHED;
1343 case LACP_MUX_COLLECTING:
1344 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1345 new_state = LACP_MUX_DISTRIBUTING;
1346 } else if (selected != LACP_SELECTED || !p_sync) {
1347 new_state = LACP_MUX_ATTACHED;
1350 case LACP_MUX_DISTRIBUTING:
1351 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1352 new_state = LACP_MUX_COLLECTING;
1356 panic("%s: unknown state", __func__);
1359 if (lp->lp_mux_state == new_state) {
1363 lacp_set_mux(lp, new_state);
1368 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1370 struct lacp_aggregator *la = lp->lp_aggregator;
1372 if (lp->lp_mux_state == new_state) {
1376 switch (new_state) {
1377 case LACP_MUX_DETACHED:
1378 lp->lp_state &= ~LACP_STATE_SYNC;
1379 lacp_disable_distributing(lp);
1380 lacp_disable_collecting(lp);
1381 lacp_sm_assert_ntt(lp);
1383 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1384 KASSERT(la->la_pending > 0,
1385 ("timer_wait_while not active"));
1388 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1391 case LACP_MUX_WAITING:
1392 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1393 LACP_AGGREGATE_WAIT_TIME);
1396 case LACP_MUX_ATTACHED:
1397 lp->lp_state |= LACP_STATE_SYNC;
1398 lacp_disable_collecting(lp);
1399 lacp_sm_assert_ntt(lp);
1401 case LACP_MUX_COLLECTING:
1402 lacp_enable_collecting(lp);
1403 lacp_disable_distributing(lp);
1404 lacp_sm_assert_ntt(lp);
1406 case LACP_MUX_DISTRIBUTING:
1407 lacp_enable_distributing(lp);
1410 panic("%s: unknown state", __func__);
1413 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1415 lp->lp_mux_state = new_state;
1419 lacp_sm_mux_timer(struct lacp_port *lp)
1421 struct lacp_aggregator *la = lp->lp_aggregator;
1422 #if defined(LACP_DEBUG)
1423 char buf[LACP_LAGIDSTR_MAX+1];
1426 KASSERT(la->la_pending > 0, ("no pending event"));
1428 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1429 lacp_format_lagid(&la->la_actor, &la->la_partner,
1431 la->la_pending, la->la_pending - 1));
1436 /* periodic transmit machine */
1439 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1441 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1442 LACP_STATE_TIMEOUT)) {
1446 LACP_DPRINTF((lp, "partner timeout changed\n"));
1449 * FAST_PERIODIC -> SLOW_PERIODIC
1451 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1453 * let lacp_sm_ptx_tx_schedule to update timeout.
1456 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1459 * if timeout has been shortened, assert NTT.
1462 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1463 lacp_sm_assert_ntt(lp);
1468 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1472 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1473 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1479 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1483 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1487 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1488 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1490 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1494 lacp_sm_ptx_timer(struct lacp_port *lp)
1496 lacp_sm_assert_ntt(lp);
1500 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1505 * check LACP_DISABLED first
1508 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1513 * check loopback condition.
1516 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1517 &lp->lp_actor.lip_systemid)) {
1522 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1525 lacp_sm_rx_update_selected(lp, du);
1526 lacp_sm_rx_update_ntt(lp, du);
1527 lacp_sm_rx_record_pdu(lp, du);
1529 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1530 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1531 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1533 lp->lp_state &= ~LACP_STATE_EXPIRED;
1536 * kick transmit machine without waiting the next tick.
1543 lacp_sm_rx_set_expired(struct lacp_port *lp)
1545 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1546 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1547 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1548 lp->lp_state |= LACP_STATE_EXPIRED;
1552 lacp_sm_rx_timer(struct lacp_port *lp)
1554 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1555 /* CURRENT -> EXPIRED */
1556 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1557 lacp_sm_rx_set_expired(lp);
1559 /* EXPIRED -> DEFAULTED */
1560 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1561 lacp_sm_rx_update_default_selected(lp);
1562 lacp_sm_rx_record_default(lp);
1563 lp->lp_state &= ~LACP_STATE_EXPIRED;
1568 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1572 #if defined(LACP_DEBUG)
1573 char buf[LACP_STATESTR_MAX+1];
1576 /* LACP_DPRINTF((lp, "%s\n", __func__)); */
1578 oldpstate = lp->lp_partner.lip_state;
1580 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1581 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1582 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1584 lp->lp_partner = du->ldu_actor;
1586 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1587 LACP_STATE_AGGREGATION) &&
1588 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1589 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1592 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1595 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1597 if (oldpstate != lp->lp_partner.lip_state) {
1598 LACP_DPRINTF((lp, "old pstate %s\n",
1599 lacp_format_state(oldpstate, buf, sizeof(buf))));
1600 LACP_DPRINTF((lp, "new pstate %s\n",
1601 lacp_format_state(lp->lp_partner.lip_state, buf,
1605 lacp_sm_ptx_update_timeout(lp, oldpstate);
1609 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1611 /* LACP_DPRINTF((lp, "%s\n", __func__)); */
1613 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1614 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1615 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1616 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1617 lacp_sm_assert_ntt(lp);
1622 lacp_sm_rx_record_default(struct lacp_port *lp)
1626 /* LACP_DPRINTF((lp, "%s\n", __func__)); */
1628 oldpstate = lp->lp_partner.lip_state;
1629 lp->lp_partner = lacp_partner_admin;
1630 lp->lp_state |= LACP_STATE_DEFAULTED;
1631 lacp_sm_ptx_update_timeout(lp, oldpstate);
1635 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1636 const struct lacp_peerinfo *info)
1638 /* LACP_DPRINTF((lp, "%s\n", __func__)); */
1640 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1641 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1642 LACP_STATE_AGGREGATION)) {
1643 lp->lp_selected = LACP_UNSELECTED;
1644 /* mux machine will clean up lp->lp_aggregator */
1649 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1651 /* LACP_DPRINTF((lp, "%s\n", __func__)); */
1653 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1657 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1659 /* LACP_DPRINTF((lp, "%s\n", __func__)); */
1661 lacp_sm_rx_update_selected_from_peerinfo(lp, &lacp_partner_admin);
1664 /* transmit machine */
1667 lacp_sm_tx(struct lacp_port *lp)
1671 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1673 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1674 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1677 lp->lp_flags &= ~LACP_PORT_NTT;
1680 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1684 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1685 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1686 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1687 LACP_DPRINTF((lp, "rate limited pdu\n"));
1691 error = lacp_xmit_lacpdu(lp);
1694 lp->lp_flags &= ~LACP_PORT_NTT;
1696 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1702 lacp_sm_assert_ntt(struct lacp_port *lp)
1705 lp->lp_flags |= LACP_PORT_NTT;
1709 lacp_run_timers(struct lacp_port *lp)
1713 for (i = 0; i < LACP_NTIMER; i++) {
1714 KASSERT(lp->lp_timer[i] >= 0,
1715 ("invalid timer value %d", lp->lp_timer[i]));
1716 if (lp->lp_timer[i] == 0) {
1718 } else if (--lp->lp_timer[i] <= 0) {
1719 if (lacp_timer_funcs[i]) {
1720 (*lacp_timer_funcs[i])(lp);
1727 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1729 struct lacp_softc *lsc = lp->lp_lsc;
1730 struct lagg_port *lgp = lp->lp_lagg;
1731 struct lacp_port *lp2;
1732 struct markerdu *mdu;
1736 if (m->m_pkthdr.len != sizeof(*mdu)) {
1740 if ((m->m_flags & M_MCAST) == 0) {
1744 if (m->m_len < sizeof(*mdu)) {
1745 m = m_pullup(m, sizeof(*mdu));
1751 mdu = mtod(m, struct markerdu *);
1753 if (memcmp(&mdu->mdu_eh.ether_dhost,
1754 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1758 if (mdu->mdu_sph.sph_version != 1) {
1762 switch (mdu->mdu_tlv.tlv_type) {
1763 case MARKER_TYPE_INFO:
1764 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1765 marker_info_tlv_template, TRUE)) {
1768 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1769 memcpy(&mdu->mdu_eh.ether_dhost,
1770 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1771 memcpy(&mdu->mdu_eh.ether_shost,
1772 lgp->lp_lladdr, ETHER_ADDR_LEN);
1773 error = lagg_enqueue(lp->lp_ifp, m);
1776 case MARKER_TYPE_RESPONSE:
1777 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1778 marker_response_tlv_template, TRUE)) {
1781 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
1782 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
1783 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
1785 /* Verify that it is the last marker we sent out */
1786 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
1787 sizeof(struct lacp_markerinfo)))
1791 lp->lp_flags &= ~LACP_PORT_MARK;
1793 if (lsc->lsc_suppress_distributing) {
1794 /* Check if any ports are waiting for a response */
1795 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
1796 if (lp2->lp_flags & LACP_PORT_MARK) {
1803 /* All interface queues are clear */
1804 LACP_DPRINTF((NULL, "queue flush complete\n"));
1805 lsc->lsc_suppress_distributing = FALSE;
1819 LACP_DPRINTF((lp, "bad marker frame\n"));
1825 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
1826 const struct tlv_template *tmpl, boolean_t check_type)
1828 while (/* CONSTCOND */ 1) {
1829 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
1832 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
1833 tlv->tlv_length != tmpl->tmpl_length) {
1836 if (tmpl->tmpl_type == 0) {
1839 tlv = (const struct tlvhdr *)
1840 ((const char *)tlv + tlv->tlv_length);
1847 #if defined(LACP_DEBUG)
1849 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
1851 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
1863 lacp_format_systemid(const struct lacp_systemid *sysid,
1864 char *buf, size_t buflen)
1866 char macbuf[LACP_MACSTR_MAX+1];
1868 snprintf(buf, buflen, "%04X,%s",
1869 ntohs(sysid->lsi_prio),
1870 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
1876 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
1878 snprintf(buf, buflen, "%04X,%04X",
1879 ntohs(portid->lpi_prio),
1880 ntohs(portid->lpi_portno));
1886 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
1888 char sysid[LACP_SYSTEMIDSTR_MAX+1];
1889 char portid[LACP_PORTIDSTR_MAX+1];
1891 snprintf(buf, buflen, "(%s,%04X,%s)",
1892 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
1893 ntohs(peer->lip_key),
1894 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
1900 lacp_format_lagid(const struct lacp_peerinfo *a,
1901 const struct lacp_peerinfo *b, char *buf, size_t buflen)
1903 char astr[LACP_PARTNERSTR_MAX+1];
1904 char bstr[LACP_PARTNERSTR_MAX+1];
1908 * there's a convention to display small numbered peer
1912 if (lacp_compare_peerinfo(a, b) > 0) {
1913 const struct lacp_peerinfo *t;
1921 snprintf(buf, buflen, "[%s,%s]",
1922 lacp_format_partner(a, astr, sizeof(astr)),
1923 lacp_format_partner(b, bstr, sizeof(bstr)));
1929 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
1930 char *buf, size_t buflen)
1936 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
1940 lacp_format_state(uint8_t state, char *buf, size_t buflen)
1942 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
1947 lacp_dump_lacpdu(const struct lacpdu *du)
1949 char buf[LACP_PARTNERSTR_MAX+1];
1950 char buf2[LACP_STATESTR_MAX+1];
1952 printf("actor=%s\n",
1953 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
1954 printf("actor.state=%s\n",
1955 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
1956 printf("partner=%s\n",
1957 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
1958 printf("partner.state=%s\n",
1959 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
1961 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
1965 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
1970 printf("%s: ", lp->lp_ifp->if_xname);