1 /* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $
2 * $FreeBSD: head/sys/net/ieee8023ad_lacp.c 253687 2013-07-26 19:41:13Z adrian $
6 * Copyright (c)2005 YAMAMOTO Takashi,
7 * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/callout.h>
36 #include <sys/eventhandler.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h> /* hz */
41 #include <sys/socket.h> /* for net/if.h */
42 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
44 #include <machine/stdarg.h>
46 #include <sys/taskqueue.h>
49 #include <net/if_arp.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/ethernet.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
56 #include <net/lagg/if_lagg.h>
57 #include <net/lagg/ieee8023ad_lacp.h>
60 * actor system priority and port priority.
61 * XXX should be configurable.
64 #define LACP_SYSTEM_PRIO 0x8000
65 #define LACP_PORT_PRIO 0x8000
67 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
68 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
70 static const struct tlv_template lacp_info_tlv_template[] = {
71 { LACP_TYPE_ACTORINFO,
72 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
73 { LACP_TYPE_PARTNERINFO,
74 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
75 { LACP_TYPE_COLLECTORINFO,
76 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
80 static const struct tlv_template marker_info_tlv_template[] = {
82 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
86 static const struct tlv_template marker_response_tlv_template[] = {
87 { MARKER_TYPE_RESPONSE,
88 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
92 typedef void (*lacp_timer_func_t)(struct lacp_port *);
94 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
95 static void lacp_fill_markerinfo(struct lacp_port *,
96 struct lacp_markerinfo *);
98 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
99 static void lacp_suppress_distributing(struct lacp_softc *,
100 struct lacp_aggregator *);
101 static void lacp_transit_expire(void *);
102 static void lacp_update_portmap(struct lacp_softc *);
103 static void lacp_select_active_aggregator(struct lacp_softc *);
104 static uint16_t lacp_compose_key(struct lacp_port *);
105 static int tlv_check(const void *, size_t, const struct tlvhdr *,
106 const struct tlv_template *, boolean_t);
107 static void lacp_tick(void *);
109 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
110 const struct lacp_port *);
111 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
112 const struct lacp_peerinfo *);
113 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
114 const struct lacp_port *);
115 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
116 const struct lacp_peerinfo *);
118 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
120 static void lacp_aggregator_addref(struct lacp_softc *,
121 struct lacp_aggregator *);
122 static void lacp_aggregator_delref(struct lacp_softc *,
123 struct lacp_aggregator *);
125 /* receive machine */
127 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
128 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
129 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
130 static void lacp_sm_rx_timer(struct lacp_port *);
131 static void lacp_sm_rx_set_expired(struct lacp_port *);
132 static void lacp_sm_rx_update_ntt(struct lacp_port *,
133 const struct lacpdu *);
134 static void lacp_sm_rx_record_pdu(struct lacp_port *,
135 const struct lacpdu *);
136 static void lacp_sm_rx_update_selected(struct lacp_port *,
137 const struct lacpdu *);
138 static void lacp_sm_rx_record_default(struct lacp_port *);
139 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
140 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
141 const struct lacp_peerinfo *);
145 static void lacp_sm_mux(struct lacp_port *);
146 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
147 static void lacp_sm_mux_timer(struct lacp_port *);
149 /* periodic transmit machine */
151 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
152 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
153 static void lacp_sm_ptx_timer(struct lacp_port *);
155 /* transmit machine */
157 static void lacp_sm_tx(struct lacp_port *);
158 static void lacp_sm_assert_ntt(struct lacp_port *);
160 static void lacp_run_timers(struct lacp_port *);
161 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
162 const struct lacp_peerinfo *);
163 static int lacp_compare_systemid(const struct lacp_systemid *,
164 const struct lacp_systemid *);
165 static void lacp_port_enable(struct lacp_port *);
166 static void lacp_port_disable(struct lacp_port *);
167 static void lacp_select(struct lacp_port *);
168 static void lacp_unselect(struct lacp_port *);
169 static void lacp_disable_collecting(struct lacp_port *);
170 static void lacp_enable_collecting(struct lacp_port *);
171 static void lacp_disable_distributing(struct lacp_port *);
172 static void lacp_enable_distributing(struct lacp_port *);
173 static int lacp_xmit_lacpdu(struct lacp_port *);
174 static int lacp_xmit_marker(struct lacp_port *);
178 static void lacp_dump_lacpdu(const struct lacpdu *);
179 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
181 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
182 const struct lacp_peerinfo *, char *, size_t);
183 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
185 static const char *lacp_format_state(uint8_t, char *, size_t);
186 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
187 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
189 static const char *lacp_format_portid(const struct lacp_portid *, char *,
191 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
194 static int lacp_debug = 0;
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_RW,
197 &lacp_debug, 0, "Enable LACP debug logging (1=debug, 2=trace)");
198 TUNABLE_INT("net.link.lagg.lacp.debug", &lacp_debug);
200 #define LACP_DPRINTF(a) if (lacp_debug & 0x01) { lacp_dprintf a ; }
201 #define LACP_TRACE(a) if (lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
202 #define LACP_TPRINTF(a) if (lacp_debug & 0x04) { lacp_dprintf a ; }
205 * partner administration variables.
206 * XXX should be configurable.
209 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
210 .lip_systemid = { .lsi_prio = 0xffff },
211 .lip_portid = { .lpi_prio = 0xffff },
212 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
213 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
216 static const struct lacp_peerinfo lacp_partner_admin_strict = {
217 .lip_systemid = { .lsi_prio = 0xffff },
218 .lip_portid = { .lpi_prio = 0xffff },
222 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
223 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
224 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
225 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
229 lacp_input(struct lagg_port *lgp, struct mbuf *m)
231 struct lacp_port *lp = LACP_PORT(lgp);
234 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
239 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
241 case SLOWPROTOCOLS_SUBTYPE_LACP:
242 lacp_pdu_input(lp, m);
245 case SLOWPROTOCOLS_SUBTYPE_MARKER:
246 lacp_marker_input(lp, m);
250 /* Not a subtype we are interested in */
255 * lacp_pdu_input: process lacpdu
258 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
260 struct lacp_softc *lsc = lp->lp_lsc;
264 if (m->m_pkthdr.len != sizeof(*du)) {
268 if ((m->m_flags & M_MCAST) == 0) {
272 if (m->m_len < sizeof(*du)) {
273 m = m_pullup(m, sizeof(*du));
279 du = mtod(m, struct lacpdu *);
281 if (memcmp(&du->ldu_eh.ether_dhost,
282 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
287 * ignore the version for compatibility with
288 * the future protocol revisions.
291 if (du->ldu_sph.sph_version != 1) {
297 * ignore tlv types for compatibility with
298 * the future protocol revisions.
300 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
301 lacp_info_tlv_template, FALSE)) {
305 if (lacp_debug > 0) {
306 lacp_dprintf(lp, "lacpdu receive\n");
307 lacp_dump_lacpdu(du);
310 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
311 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
328 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
330 struct lagg_port *lgp = lp->lp_lagg;
331 struct lagg_softc *sc = lgp->lp_softc;
333 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
334 memcpy(&info->lip_systemid.lsi_mac,
335 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
336 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
337 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
338 info->lip_state = lp->lp_state;
342 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
344 struct ifnet *ifp = lp->lp_ifp;
346 /* Fill in the port index and system id (encoded as the MAC) */
347 info->mi_rq_port = htons(ifp->if_index);
348 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
349 info->mi_rq_xid = htonl(0);
353 lacp_xmit_lacpdu(struct lacp_port *lp)
355 struct lagg_port *lgp = lp->lp_lagg;
360 LACP_LOCK_ASSERT(lp->lp_lsc);
362 m = m_gethdr(M_NOWAIT, MT_DATA);
366 m->m_len = m->m_pkthdr.len = sizeof(*du);
368 du = mtod(m, struct lacpdu *);
369 memset(du, 0, sizeof(*du));
371 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
373 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
374 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
376 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
377 du->ldu_sph.sph_version = 1;
379 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
380 du->ldu_actor = lp->lp_actor;
382 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
383 sizeof(du->ldu_partner));
384 du->ldu_partner = lp->lp_partner;
386 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
387 sizeof(du->ldu_collector));
388 du->ldu_collector.lci_maxdelay = 0;
390 if (lacp_debug > 0) {
391 lacp_dprintf(lp, "lacpdu transmit\n");
392 lacp_dump_lacpdu(du);
395 m->m_flags |= M_MCAST;
398 * XXX should use higher priority queue.
399 * otherwise network congestion can break aggregation.
402 error = lagg_enqueue(lp->lp_ifp, m);
407 lacp_xmit_marker(struct lacp_port *lp)
409 struct lagg_port *lgp = lp->lp_lagg;
411 struct markerdu *mdu;
414 LACP_LOCK_ASSERT(lp->lp_lsc);
416 m = m_gethdr(M_NOWAIT, MT_DATA);
420 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
422 mdu = mtod(m, struct markerdu *);
423 memset(mdu, 0, sizeof(*mdu));
425 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
427 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
428 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
430 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
431 mdu->mdu_sph.sph_version = 1;
433 /* Bump the transaction id and copy over the marker info */
434 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
435 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
436 mdu->mdu_info = lp->lp_marker;
438 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6d, id=%u\n",
439 ntohs(mdu->mdu_info.mi_rq_port), *mdu->mdu_info.mi_rq_system,
440 ntohl(mdu->mdu_info.mi_rq_xid)));
442 m->m_flags |= M_MCAST;
443 error = lagg_enqueue(lp->lp_ifp, m);
448 lacp_linkstate(struct lagg_port *lgp)
450 struct lacp_port *lp = LACP_PORT(lgp);
451 struct lacp_softc *lsc = lp->lp_lsc;
452 struct ifnet *ifp = lgp->lp_ifp;
453 struct ifmediareq ifmr;
459 bzero((char *)&ifmr, sizeof(ifmr));
460 /* ifnet_deserialize_all(ifp); */
461 ifnet_serialize_all(ifp);
462 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
463 ifnet_deserialize_all(ifp);
464 /* ifnet_serialize_all(ifp); */
470 media = ifmr.ifm_active;
471 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
472 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
473 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
474 old_state = lp->lp_state;
475 old_key = lp->lp_key;
478 lp->lp_media = media;
480 * If the port is not an active full duplex Ethernet link then it can
483 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
484 ifp->if_link_state != LINK_STATE_UP) {
485 lacp_port_disable(lp);
487 lacp_port_enable(lp);
489 lp->lp_key = lacp_compose_key(lp);
491 if (old_state != lp->lp_state || old_key != lp->lp_key) {
492 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
493 lp->lp_selected = LACP_UNSELECTED;
501 struct lacp_softc *lsc = arg;
502 struct lacp_port *lp;
505 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
506 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
514 lacp_sm_ptx_tx_schedule(lp);
517 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
521 lacp_port_create(struct lagg_port *lgp)
523 struct lagg_softc *sc = lgp->lp_softc;
524 struct lacp_softc *lsc = LACP_SOFTC(sc);
525 struct lacp_port *lp;
526 struct ifnet *ifp = lgp->lp_ifp;
527 struct sockaddr_dl sdl;
528 struct ifmultiaddr *rifma = NULL;
531 boolean_t active = TRUE; /* XXX should be configurable */
532 boolean_t fast = FALSE; /* XXX should be configurable */
534 bzero((char *)&sdl, sizeof(sdl));
535 sdl.sdl_len = sizeof(sdl);
536 sdl.sdl_family = AF_LINK;
537 sdl.sdl_index = ifp->if_index;
538 sdl.sdl_type = IFT_ETHER;
539 sdl.sdl_alen = ETHER_ADDR_LEN;
541 bcopy(ðermulticastaddr_slowprotocols,
542 LLADDR(&sdl), ETHER_ADDR_LEN);
544 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
546 kprintf("%s: ADDMULTI failed on %s\n", __func__, lgp->lp_ifname);
549 lp = kmalloc(sizeof(struct lacp_port),
550 M_DEVBUF, M_NOWAIT|M_ZERO);
555 lgp->lp_psc = (caddr_t)lp;
561 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
563 lacp_fill_actorinfo(lp, &lp->lp_actor);
564 lacp_fill_markerinfo(lp, &lp->lp_marker);
566 (active ? LACP_STATE_ACTIVITY : 0) |
567 (fast ? LACP_STATE_TIMEOUT : 0);
568 lp->lp_aggregator = NULL;
569 lacp_sm_rx_set_expired(lp);
577 lacp_port_destroy(struct lagg_port *lgp)
579 struct lacp_port *lp = LACP_PORT(lgp);
580 struct lacp_softc *lsc = lp->lp_lsc;
584 for (i = 0; i < LACP_NTIMER; i++) {
585 LACP_TIMER_DISARM(lp, i);
588 lacp_disable_collecting(lp);
589 lacp_disable_distributing(lp);
592 /* The address may have already been removed by if_purgemaddrs() */
594 if (!lgp->lp_detaching)
595 if_delmulti_ifma(lp->lp_ifma);
597 LIST_REMOVE(lp, lp_next);
603 lacp_req(struct lagg_softc *sc, caddr_t data)
605 struct lacp_opreq *req = (struct lacp_opreq *)data;
606 struct lacp_softc *lsc = LACP_SOFTC(sc);
607 struct lacp_aggregator *la = lsc->lsc_active_aggregator;
610 bzero(req, sizeof(struct lacp_opreq));
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, caddr_t 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;
748 LACP_LOCK_ASSERT(lsc);
752 lsc->lsc_suppress_distributing = FALSE;
758 lacp_attach_sysctl(struct lacp_softc *lsc, struct sysctl_oid *p_oid)
760 struct lagg_softc *sc = lsc->lsc_softc;
762 SYSCTL_ADD_UINT(&sc->ctx, SYSCTL_CHILDREN(p_oid), OID_AUTO,
765 &lsc->lsc_strict_mode,
766 lsc->lsc_strict_mode,
767 "Enable LACP strict mode");
771 lacp_attach_sysctl_debug(struct lacp_softc *lsc, struct sysctl_oid *p_oid)
773 struct lagg_softc *sc = lsc->lsc_softc;
774 struct sysctl_oid *oid;
776 /* Create a child of the parent lagg interface */
777 oid = SYSCTL_ADD_NODE(&sc->ctx, SYSCTL_CHILDREN(p_oid),
778 OID_AUTO, "debug", CTLFLAG_RD, NULL, "DEBUG");
780 SYSCTL_ADD_UINT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
783 &lsc->lsc_debug.lsc_rx_test,
784 lsc->lsc_debug.lsc_rx_test,
785 "Bitmap of if_dunit entries to drop RX frames for");
786 SYSCTL_ADD_UINT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
789 &lsc->lsc_debug.lsc_tx_test,
790 lsc->lsc_debug.lsc_tx_test,
791 "Bitmap of if_dunit entries to drop TX frames for");
795 lacp_attach(struct lagg_softc *sc)
797 struct lacp_softc *lsc;
798 struct sysctl_oid *oid;
800 lsc = kmalloc(sizeof(struct lacp_softc),
801 M_DEVBUF, M_NOWAIT|M_ZERO);
805 sc->sc_psc = (caddr_t)lsc;
808 lsc->lsc_hashkey = karc4random();
809 lsc->lsc_active_aggregator = NULL;
810 lsc->lsc_strict_mode = 1;
812 TAILQ_INIT(&lsc->lsc_aggregators);
813 LIST_INIT(&lsc->lsc_ports);
815 /* Create a child of the parent lagg interface */
816 oid = SYSCTL_ADD_NODE(&sc->ctx, SYSCTL_CHILDREN(sc->sc_oid),
817 OID_AUTO, "lacp", CTLFLAG_RD, NULL, "LACP");
819 /* Attach sysctl nodes */
820 lacp_attach_sysctl(lsc, oid);
821 lacp_attach_sysctl_debug(lsc, oid);
824 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_lock, 0);
825 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_lock, 0);
828 callout_init(&lsc->lsc_transit_callout);
829 callout_init(&lsc->lsc_callout);
831 /* if the lagg is already up then do the same */
832 if (sc->sc_ifp->if_flags & IFF_RUNNING)
839 lacp_detach(struct lagg_softc *sc)
841 struct lacp_softc *lsc = LACP_SOFTC(sc);
843 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
844 ("aggregators still active"));
845 KASSERT(lsc->lsc_active_aggregator == NULL,
846 ("aggregator still attached"));
849 callout_drain(&lsc->lsc_transit_callout);
850 callout_drain(&lsc->lsc_callout);
852 LACP_LOCK_DESTROY(lsc);
853 kfree(lsc, M_DEVBUF);
858 lacp_init(struct lagg_softc *sc)
860 struct lacp_softc *lsc = LACP_SOFTC(sc);
863 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
868 lacp_stop(struct lagg_softc *sc)
870 struct lacp_softc *lsc = LACP_SOFTC(sc);
873 callout_stop(&lsc->lsc_transit_callout);
874 callout_stop(&lsc->lsc_callout);
879 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
881 struct lacp_softc *lsc = LACP_SOFTC(sc);
882 struct lacp_portmap *pm;
883 struct lacp_port *lp;
886 if (__predict_false(lsc->lsc_suppress_distributing)) {
887 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
891 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
892 if (pm->pm_count == 0) {
893 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
898 if (sc->use_flowid && (m->m_flags & M_FLOWID))
899 hash = m->m_pkthdr.flowid;
902 hash = lagg_hashmbuf(sc, m, lsc->lsc_hashkey);
903 hash %= pm->pm_count;
904 lp = pm->pm_map[hash];
906 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
907 ("aggregated port is not distributing"));
909 return (lp->lp_lagg);
912 * lacp_suppress_distributing: drop transmit packets for a while
913 * to preserve packet ordering.
917 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
919 struct lacp_port *lp;
921 if (lsc->lsc_active_aggregator != la) {
927 lsc->lsc_suppress_distributing = TRUE;
929 /* send a marker frame down each port to verify the queues are empty */
930 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
931 lp->lp_flags |= LACP_PORT_MARK;
932 lacp_xmit_marker(lp);
935 /* set a timeout for the marker frames */
936 callout_reset(&lsc->lsc_transit_callout,
937 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
941 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
942 const struct lacp_peerinfo *b)
944 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
948 lacp_compare_systemid(const struct lacp_systemid *a,
949 const struct lacp_systemid *b)
951 return (memcmp(a, b, sizeof(*a)));
956 lacp_compare_portid(const struct lacp_portid *a,
957 const struct lacp_portid *b)
959 return (memcmp(a, b, sizeof(*a)));
964 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
966 struct lacp_port *lp;
969 lp = TAILQ_FIRST(&la->la_ports);
974 speed = ifmedia_baudrate(lp->lp_media);
975 speed *= la->la_nports;
977 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
978 lp->lp_media, la->la_nports));
985 * lacp_select_active_aggregator: select an aggregator to be used to transmit
986 * packets from lagg(4) interface.
990 lacp_select_active_aggregator(struct lacp_softc *lsc)
992 struct lacp_aggregator *la;
993 struct lacp_aggregator *best_la = NULL;
994 uint64_t best_speed = 0;
995 char buf[LACP_LAGIDSTR_MAX+1];
999 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1002 if (la->la_nports == 0) {
1006 speed = lacp_aggregator_bandwidth(la);
1007 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
1008 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
1009 speed, la->la_nports));
1011 /* This aggregator is chosen if
1012 * the partner has a better system priority
1013 * or, the total aggregated speed is higher
1014 * or, it is already the chosen aggregator
1016 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1017 LACP_SYS_PRI(best_la->la_partner)) ||
1018 speed > best_speed ||
1019 (speed == best_speed &&
1020 la == lsc->lsc_active_aggregator)) {
1026 KASSERT(best_la == NULL || best_la->la_nports > 0,
1027 ("invalid aggregator refcnt"));
1028 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1029 ("invalid aggregator list"));
1031 if (lsc->lsc_active_aggregator != best_la) {
1032 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1033 LACP_DPRINTF((NULL, "old %s\n",
1034 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1035 buf, sizeof(buf))));
1037 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1039 LACP_DPRINTF((NULL, "new %s\n",
1040 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1042 if (lsc->lsc_active_aggregator != best_la) {
1043 lsc->lsc_active_aggregator = best_la;
1044 lacp_update_portmap(lsc);
1046 lacp_suppress_distributing(lsc, best_la);
1052 * Updated the inactive portmap array with the new list of ports and
1056 lacp_update_portmap(struct lacp_softc *lsc)
1058 struct lagg_softc *sc = lsc->lsc_softc;
1059 struct lacp_aggregator *la;
1060 struct lacp_portmap *p;
1061 struct lacp_port *lp;
1066 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1067 p = &lsc->lsc_pmap[newmap];
1068 la = lsc->lsc_active_aggregator;
1070 bzero(p, sizeof(struct lacp_portmap));
1072 if (la != NULL && la->la_nports > 0) {
1073 p->pm_count = la->la_nports;
1075 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
1076 p->pm_map[i++] = lp;
1077 KASSERT(i == p->pm_count, ("Invalid port count"));
1078 speed = lacp_aggregator_bandwidth(la);
1080 sc->sc_ifp->if_baudrate = speed;
1082 /* switch the active portmap over */
1083 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1084 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1086 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1090 lacp_compose_key(struct lacp_port *lp)
1092 struct lagg_port *lgp = lp->lp_lagg;
1093 struct lagg_softc *sc = lgp->lp_softc;
1094 u_int media = lp->lp_media;
1097 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1100 * non-aggregatable links should have unique keys.
1102 * XXX this isn't really unique as if_index is 16 bit.
1105 /* bit 0..14: (some bits of) if_index of this port */
1106 key = lp->lp_ifp->if_index;
1110 u_int subtype = IFM_SUBTYPE(media);
1112 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1113 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1115 /* bit 0..4: IFM_SUBTYPE modulo speed */
1137 case IFM_1000_SGMII:
1138 case IFM_1000_CX_SGMII:
1144 case IFM_10G_TWINAX:
1145 case IFM_10G_TWINAX_LONG:
1196 /* bit 5..14: (some bits of) if_index of lagg device */
1197 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1200 return (htons(key));
1204 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1206 char buf[LACP_LAGIDSTR_MAX+1];
1208 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1210 lacp_format_lagid(&la->la_actor, &la->la_partner,
1212 la->la_refcnt, la->la_refcnt + 1));
1214 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1216 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1220 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1222 char buf[LACP_LAGIDSTR_MAX+1];
1224 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1226 lacp_format_lagid(&la->la_actor, &la->la_partner,
1228 la->la_refcnt, la->la_refcnt - 1));
1230 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1232 if (la->la_refcnt > 0) {
1236 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1237 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1239 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1241 kfree(la, M_DEVBUF);
1245 * lacp_aggregator_get: allocate an aggregator.
1248 static struct lacp_aggregator *
1249 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1251 struct lacp_aggregator *la;
1253 la = kmalloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1257 TAILQ_INIT(&la->la_ports);
1259 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1266 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1270 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1272 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1273 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1275 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1279 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1280 const struct lacp_peerinfo *lpi_port)
1282 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1283 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1284 lpi_aggr->lip_key = lpi_port->lip_key;
1288 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1292 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1293 const struct lacp_port *lp)
1295 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1296 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1300 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1304 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1308 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1316 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1317 const struct lacp_peerinfo *b)
1319 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1320 sizeof(a->lip_systemid))) {
1324 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1332 lacp_port_enable(struct lacp_port *lp)
1334 lp->lp_state |= LACP_STATE_AGGREGATION;
1338 lacp_port_disable(struct lacp_port *lp)
1340 lacp_set_mux(lp, LACP_MUX_DETACHED);
1342 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1343 lp->lp_selected = LACP_UNSELECTED;
1344 lacp_sm_rx_record_default(lp);
1345 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1346 lp->lp_state &= ~LACP_STATE_EXPIRED;
1350 * lacp_select: select an aggregator. create one if necessary.
1353 lacp_select(struct lacp_port *lp)
1355 struct lacp_softc *lsc = lp->lp_lsc;
1356 struct lacp_aggregator *la;
1357 char buf[LACP_LAGIDSTR_MAX+1];
1359 if (lp->lp_aggregator) {
1363 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1364 ("timer_wait_while still active"));
1366 LACP_DPRINTF((lp, "port lagid=%s\n",
1367 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1368 buf, sizeof(buf))));
1370 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1371 if (lacp_aggregator_is_compatible(la, lp)) {
1377 la = lacp_aggregator_get(lsc, lp);
1379 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1382 * will retry on the next tick.
1387 lacp_fill_aggregator_id(la, lp);
1388 LACP_DPRINTF((lp, "aggregator created\n"));
1390 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1391 if (la->la_refcnt == LACP_MAX_PORTS)
1393 lacp_aggregator_addref(lsc, la);
1396 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1397 lacp_format_lagid(&la->la_actor, &la->la_partner,
1398 buf, sizeof(buf))));
1400 lp->lp_aggregator = la;
1401 lp->lp_selected = LACP_SELECTED;
1405 * lacp_unselect: finish unselect/detach process.
1409 lacp_unselect(struct lacp_port *lp)
1411 struct lacp_softc *lsc = lp->lp_lsc;
1412 struct lacp_aggregator *la = lp->lp_aggregator;
1414 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1415 ("timer_wait_while still active"));
1421 lp->lp_aggregator = NULL;
1422 lacp_aggregator_delref(lsc, la);
1428 lacp_sm_mux(struct lacp_port *lp)
1430 struct lagg_port *lgp = lp->lp_lagg;
1431 struct lagg_softc *sc = lgp->lp_softc;
1432 enum lacp_mux_state new_state;
1434 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1435 boolean_t p_collecting =
1436 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1437 enum lacp_selected selected = lp->lp_selected;
1438 struct lacp_aggregator *la;
1441 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1442 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1443 lp->lp_mux_state, selected, p_sync, p_collecting);
1446 la = lp->lp_aggregator;
1447 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1448 ("MUX not detached"));
1449 new_state = lp->lp_mux_state;
1450 switch (lp->lp_mux_state) {
1451 case LACP_MUX_DETACHED:
1452 if (selected != LACP_UNSELECTED) {
1453 new_state = LACP_MUX_WAITING;
1456 case LACP_MUX_WAITING:
1457 KASSERT(la->la_pending > 0 ||
1458 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1459 ("timer_wait_while still active"));
1460 if (selected == LACP_SELECTED && la->la_pending == 0) {
1461 new_state = LACP_MUX_ATTACHED;
1462 } else if (selected == LACP_UNSELECTED) {
1463 new_state = LACP_MUX_DETACHED;
1466 case LACP_MUX_ATTACHED:
1467 if (selected == LACP_SELECTED && p_sync) {
1468 new_state = LACP_MUX_COLLECTING;
1469 } else if (selected != LACP_SELECTED) {
1470 new_state = LACP_MUX_DETACHED;
1473 case LACP_MUX_COLLECTING:
1474 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1475 new_state = LACP_MUX_DISTRIBUTING;
1476 } else if (selected != LACP_SELECTED || !p_sync) {
1477 new_state = LACP_MUX_ATTACHED;
1480 case LACP_MUX_DISTRIBUTING:
1481 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1482 new_state = LACP_MUX_COLLECTING;
1483 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1488 panic("%s: unknown state", __func__);
1491 if (lp->lp_mux_state == new_state) {
1495 lacp_set_mux(lp, new_state);
1500 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1502 struct lacp_aggregator *la = lp->lp_aggregator;
1504 if (lp->lp_mux_state == new_state) {
1508 switch (new_state) {
1509 case LACP_MUX_DETACHED:
1510 lp->lp_state &= ~LACP_STATE_SYNC;
1511 lacp_disable_distributing(lp);
1512 lacp_disable_collecting(lp);
1513 lacp_sm_assert_ntt(lp);
1515 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1516 KASSERT(la->la_pending > 0,
1517 ("timer_wait_while not active"));
1520 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1523 case LACP_MUX_WAITING:
1524 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1525 LACP_AGGREGATE_WAIT_TIME);
1528 case LACP_MUX_ATTACHED:
1529 lp->lp_state |= LACP_STATE_SYNC;
1530 lacp_disable_collecting(lp);
1531 lacp_sm_assert_ntt(lp);
1533 case LACP_MUX_COLLECTING:
1534 lacp_enable_collecting(lp);
1535 lacp_disable_distributing(lp);
1536 lacp_sm_assert_ntt(lp);
1538 case LACP_MUX_DISTRIBUTING:
1539 lacp_enable_distributing(lp);
1542 panic("%s: unknown state", __func__);
1545 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1547 lp->lp_mux_state = new_state;
1551 lacp_sm_mux_timer(struct lacp_port *lp)
1553 struct lacp_aggregator *la = lp->lp_aggregator;
1554 char buf[LACP_LAGIDSTR_MAX+1];
1556 KASSERT(la->la_pending > 0, ("no pending event"));
1558 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1559 lacp_format_lagid(&la->la_actor, &la->la_partner,
1561 la->la_pending, la->la_pending - 1));
1566 /* periodic transmit machine */
1569 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1571 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1572 LACP_STATE_TIMEOUT)) {
1576 LACP_DPRINTF((lp, "partner timeout changed\n"));
1579 * FAST_PERIODIC -> SLOW_PERIODIC
1581 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1583 * let lacp_sm_ptx_tx_schedule to update timeout.
1586 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1589 * if timeout has been shortened, assert NTT.
1592 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1593 lacp_sm_assert_ntt(lp);
1598 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1602 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1603 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1609 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1613 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1617 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1618 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1620 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1624 lacp_sm_ptx_timer(struct lacp_port *lp)
1626 lacp_sm_assert_ntt(lp);
1630 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1635 * check LACP_DISABLED first
1638 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1643 * check loopback condition.
1646 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1647 &lp->lp_actor.lip_systemid)) {
1652 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1655 lacp_sm_rx_update_selected(lp, du);
1656 lacp_sm_rx_update_ntt(lp, du);
1657 lacp_sm_rx_record_pdu(lp, du);
1659 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1660 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1661 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1663 lp->lp_state &= ~LACP_STATE_EXPIRED;
1666 * kick transmit machine without waiting the next tick.
1673 lacp_sm_rx_set_expired(struct lacp_port *lp)
1675 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1676 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1677 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1678 lp->lp_state |= LACP_STATE_EXPIRED;
1682 lacp_sm_rx_timer(struct lacp_port *lp)
1684 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1685 /* CURRENT -> EXPIRED */
1686 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1687 lacp_sm_rx_set_expired(lp);
1689 /* EXPIRED -> DEFAULTED */
1690 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1691 lacp_sm_rx_update_default_selected(lp);
1692 lacp_sm_rx_record_default(lp);
1693 lp->lp_state &= ~LACP_STATE_EXPIRED;
1698 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1702 char buf[LACP_STATESTR_MAX+1];
1706 oldpstate = lp->lp_partner.lip_state;
1708 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1709 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1710 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1712 lp->lp_partner = du->ldu_actor;
1714 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1715 LACP_STATE_AGGREGATION) &&
1716 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1717 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1720 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1723 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1725 if (oldpstate != lp->lp_partner.lip_state) {
1726 LACP_DPRINTF((lp, "old pstate %s\n",
1727 lacp_format_state(oldpstate, buf, sizeof(buf))));
1728 LACP_DPRINTF((lp, "new pstate %s\n",
1729 lacp_format_state(lp->lp_partner.lip_state, buf,
1733 /* XXX Hack, still need to implement 5.4.9 para 2,3,4 */
1734 if (lp->lp_lsc->lsc_strict_mode)
1735 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1737 lacp_sm_ptx_update_timeout(lp, oldpstate);
1741 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1746 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1747 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1748 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1749 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1750 lacp_sm_assert_ntt(lp);
1755 lacp_sm_rx_record_default(struct lacp_port *lp)
1761 oldpstate = lp->lp_partner.lip_state;
1762 if (lp->lp_lsc->lsc_strict_mode)
1763 lp->lp_partner = lacp_partner_admin_strict;
1765 lp->lp_partner = lacp_partner_admin_optimistic;;
1766 lp->lp_state |= LACP_STATE_DEFAULTED;
1767 lacp_sm_ptx_update_timeout(lp, oldpstate);
1771 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1772 const struct lacp_peerinfo *info)
1777 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1778 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1779 LACP_STATE_AGGREGATION)) {
1780 lp->lp_selected = LACP_UNSELECTED;
1781 /* mux machine will clean up lp->lp_aggregator */
1786 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1791 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1795 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1800 if (lp->lp_lsc->lsc_strict_mode)
1801 lacp_sm_rx_update_selected_from_peerinfo(lp,
1802 &lacp_partner_admin_strict);
1804 lacp_sm_rx_update_selected_from_peerinfo(lp,
1805 &lacp_partner_admin_optimistic);
1808 /* transmit machine */
1811 lacp_sm_tx(struct lacp_port *lp)
1815 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1817 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1818 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1821 lp->lp_flags &= ~LACP_PORT_NTT;
1824 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1828 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1829 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1830 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1831 LACP_DPRINTF((lp, "rate limited pdu\n"));
1835 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1836 error = lacp_xmit_lacpdu(lp);
1838 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1842 lp->lp_flags &= ~LACP_PORT_NTT;
1844 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1850 lacp_sm_assert_ntt(struct lacp_port *lp)
1853 lp->lp_flags |= LACP_PORT_NTT;
1857 lacp_run_timers(struct lacp_port *lp)
1861 for (i = 0; i < LACP_NTIMER; i++) {
1862 KASSERT(lp->lp_timer[i] >= 0,
1863 ("invalid timer value %d", lp->lp_timer[i]));
1864 if (lp->lp_timer[i] == 0) {
1866 } else if (--lp->lp_timer[i] <= 0) {
1867 if (lacp_timer_funcs[i]) {
1868 (*lacp_timer_funcs[i])(lp);
1875 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1877 struct lacp_softc *lsc = lp->lp_lsc;
1878 struct lagg_port *lgp = lp->lp_lagg;
1879 struct lacp_port *lp2;
1880 struct markerdu *mdu;
1884 if (m->m_pkthdr.len != sizeof(*mdu)) {
1888 if ((m->m_flags & M_MCAST) == 0) {
1892 if (m->m_len < sizeof(*mdu)) {
1893 m = m_pullup(m, sizeof(*mdu));
1899 mdu = mtod(m, struct markerdu *);
1901 if (memcmp(&mdu->mdu_eh.ether_dhost,
1902 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1906 if (mdu->mdu_sph.sph_version != 1) {
1910 switch (mdu->mdu_tlv.tlv_type) {
1911 case MARKER_TYPE_INFO:
1912 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1913 marker_info_tlv_template, TRUE)) {
1916 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1917 memcpy(&mdu->mdu_eh.ether_dhost,
1918 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1919 memcpy(&mdu->mdu_eh.ether_shost,
1920 lgp->lp_lladdr, ETHER_ADDR_LEN);
1921 error = lagg_enqueue(lp->lp_ifp, m);
1924 case MARKER_TYPE_RESPONSE:
1925 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1926 marker_response_tlv_template, TRUE)) {
1929 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6d, id=%u\n",
1930 ntohs(mdu->mdu_info.mi_rq_port), *mdu->mdu_info.mi_rq_system,
1931 ntohl(mdu->mdu_info.mi_rq_xid)));
1933 /* Verify that it is the last marker we sent out */
1934 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
1935 sizeof(struct lacp_markerinfo)))
1939 lp->lp_flags &= ~LACP_PORT_MARK;
1941 if (lsc->lsc_suppress_distributing) {
1942 /* Check if any ports are waiting for a response */
1943 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
1944 if (lp2->lp_flags & LACP_PORT_MARK) {
1951 /* All interface queues are clear */
1952 LACP_DPRINTF((NULL, "queue flush complete\n"));
1953 lsc->lsc_suppress_distributing = FALSE;
1967 LACP_DPRINTF((lp, "bad marker frame\n"));
1973 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
1974 const struct tlv_template *tmpl, boolean_t check_type)
1976 while (/* CONSTCOND */ 1) {
1977 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
1980 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
1981 tlv->tlv_length != tmpl->tmpl_length) {
1984 if (tmpl->tmpl_type == 0) {
1987 tlv = (const struct tlvhdr *)
1988 ((const char *)tlv + tlv->tlv_length);
1997 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
1999 ksnprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2011 lacp_format_systemid(const struct lacp_systemid *sysid,
2012 char *buf, size_t buflen)
2014 char macbuf[LACP_MACSTR_MAX+1];
2016 ksnprintf(buf, buflen, "%04X,%s",
2017 ntohs(sysid->lsi_prio),
2018 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2024 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2026 ksnprintf(buf, buflen, "%04X,%04X",
2027 ntohs(portid->lpi_prio),
2028 ntohs(portid->lpi_portno));
2034 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2036 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2037 char portid[LACP_PORTIDSTR_MAX+1];
2039 ksnprintf(buf, buflen, "(%s,%04X,%s)",
2040 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2041 ntohs(peer->lip_key),
2042 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2048 lacp_format_lagid(const struct lacp_peerinfo *a,
2049 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2051 char astr[LACP_PARTNERSTR_MAX+1];
2052 char bstr[LACP_PARTNERSTR_MAX+1];
2056 * there's a convention to display small numbered peer
2060 if (lacp_compare_peerinfo(a, b) > 0) {
2061 const struct lacp_peerinfo *t;
2069 ksnprintf(buf, buflen, "[%s,%s]",
2070 lacp_format_partner(a, astr, sizeof(astr)),
2071 lacp_format_partner(b, bstr, sizeof(bstr)));
2077 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2078 char *buf, size_t buflen)
2084 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2088 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2090 ksnprintf(buf, buflen, "%pb%i", LACP_STATE_BITS, state);
2095 lacp_dump_lacpdu(const struct lacpdu *du)
2097 char buf[LACP_PARTNERSTR_MAX+1];
2098 char buf2[LACP_STATESTR_MAX+1];
2100 kprintf("actor=%s\n",
2101 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2102 kprintf("actor.state=%s\n",
2103 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2104 kprintf("partner=%s\n",
2105 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2106 kprintf("partner.state=%s\n",
2107 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2109 kprintf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2113 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2118 kprintf("%s: ", lp->lp_ifp->if_xname);
2121 __va_start(va, fmt);