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 */
1140 case IFM_10G_TWINAX:
1141 case IFM_10G_TWINAX_LONG:
1154 /* bit 5..14: (some bits of) if_index of lagg device */
1155 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1158 return (htons(key));
1162 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1164 char buf[LACP_LAGIDSTR_MAX+1];
1166 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1168 lacp_format_lagid(&la->la_actor, &la->la_partner,
1170 la->la_refcnt, la->la_refcnt + 1));
1172 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1174 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1178 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1180 char buf[LACP_LAGIDSTR_MAX+1];
1182 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1184 lacp_format_lagid(&la->la_actor, &la->la_partner,
1186 la->la_refcnt, la->la_refcnt - 1));
1188 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1190 if (la->la_refcnt > 0) {
1194 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1195 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1197 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1199 kfree(la, M_DEVBUF);
1203 * lacp_aggregator_get: allocate an aggregator.
1206 static struct lacp_aggregator *
1207 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1209 struct lacp_aggregator *la;
1211 la = kmalloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1215 TAILQ_INIT(&la->la_ports);
1217 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1224 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1228 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1230 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1231 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1233 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1237 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1238 const struct lacp_peerinfo *lpi_port)
1240 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1241 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1242 lpi_aggr->lip_key = lpi_port->lip_key;
1246 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1250 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1251 const struct lacp_port *lp)
1253 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1254 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1258 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1262 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1266 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1274 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1275 const struct lacp_peerinfo *b)
1277 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1278 sizeof(a->lip_systemid))) {
1282 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1290 lacp_port_enable(struct lacp_port *lp)
1292 lp->lp_state |= LACP_STATE_AGGREGATION;
1296 lacp_port_disable(struct lacp_port *lp)
1298 lacp_set_mux(lp, LACP_MUX_DETACHED);
1300 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1301 lp->lp_selected = LACP_UNSELECTED;
1302 lacp_sm_rx_record_default(lp);
1303 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1304 lp->lp_state &= ~LACP_STATE_EXPIRED;
1308 * lacp_select: select an aggregator. create one if necessary.
1311 lacp_select(struct lacp_port *lp)
1313 struct lacp_softc *lsc = lp->lp_lsc;
1314 struct lacp_aggregator *la;
1315 char buf[LACP_LAGIDSTR_MAX+1];
1317 if (lp->lp_aggregator) {
1321 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1322 ("timer_wait_while still active"));
1324 LACP_DPRINTF((lp, "port lagid=%s\n",
1325 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1326 buf, sizeof(buf))));
1328 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1329 if (lacp_aggregator_is_compatible(la, lp)) {
1335 la = lacp_aggregator_get(lsc, lp);
1337 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1340 * will retry on the next tick.
1345 lacp_fill_aggregator_id(la, lp);
1346 LACP_DPRINTF((lp, "aggregator created\n"));
1348 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1349 if (la->la_refcnt == LACP_MAX_PORTS)
1351 lacp_aggregator_addref(lsc, la);
1354 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1355 lacp_format_lagid(&la->la_actor, &la->la_partner,
1356 buf, sizeof(buf))));
1358 lp->lp_aggregator = la;
1359 lp->lp_selected = LACP_SELECTED;
1363 * lacp_unselect: finish unselect/detach process.
1367 lacp_unselect(struct lacp_port *lp)
1369 struct lacp_softc *lsc = lp->lp_lsc;
1370 struct lacp_aggregator *la = lp->lp_aggregator;
1372 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1373 ("timer_wait_while still active"));
1379 lp->lp_aggregator = NULL;
1380 lacp_aggregator_delref(lsc, la);
1386 lacp_sm_mux(struct lacp_port *lp)
1388 struct lagg_port *lgp = lp->lp_lagg;
1389 struct lagg_softc *sc = lgp->lp_softc;
1390 enum lacp_mux_state new_state;
1392 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1393 boolean_t p_collecting =
1394 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1395 enum lacp_selected selected = lp->lp_selected;
1396 struct lacp_aggregator *la;
1399 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1400 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1401 lp->lp_mux_state, selected, p_sync, p_collecting);
1404 la = lp->lp_aggregator;
1405 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1406 ("MUX not detached"));
1407 new_state = lp->lp_mux_state;
1408 switch (lp->lp_mux_state) {
1409 case LACP_MUX_DETACHED:
1410 if (selected != LACP_UNSELECTED) {
1411 new_state = LACP_MUX_WAITING;
1414 case LACP_MUX_WAITING:
1415 KASSERT(la->la_pending > 0 ||
1416 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1417 ("timer_wait_while still active"));
1418 if (selected == LACP_SELECTED && la->la_pending == 0) {
1419 new_state = LACP_MUX_ATTACHED;
1420 } else if (selected == LACP_UNSELECTED) {
1421 new_state = LACP_MUX_DETACHED;
1424 case LACP_MUX_ATTACHED:
1425 if (selected == LACP_SELECTED && p_sync) {
1426 new_state = LACP_MUX_COLLECTING;
1427 } else if (selected != LACP_SELECTED) {
1428 new_state = LACP_MUX_DETACHED;
1431 case LACP_MUX_COLLECTING:
1432 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1433 new_state = LACP_MUX_DISTRIBUTING;
1434 } else if (selected != LACP_SELECTED || !p_sync) {
1435 new_state = LACP_MUX_ATTACHED;
1438 case LACP_MUX_DISTRIBUTING:
1439 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1440 new_state = LACP_MUX_COLLECTING;
1441 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1446 panic("%s: unknown state", __func__);
1449 if (lp->lp_mux_state == new_state) {
1453 lacp_set_mux(lp, new_state);
1458 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1460 struct lacp_aggregator *la = lp->lp_aggregator;
1462 if (lp->lp_mux_state == new_state) {
1466 switch (new_state) {
1467 case LACP_MUX_DETACHED:
1468 lp->lp_state &= ~LACP_STATE_SYNC;
1469 lacp_disable_distributing(lp);
1470 lacp_disable_collecting(lp);
1471 lacp_sm_assert_ntt(lp);
1473 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1474 KASSERT(la->la_pending > 0,
1475 ("timer_wait_while not active"));
1478 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1481 case LACP_MUX_WAITING:
1482 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1483 LACP_AGGREGATE_WAIT_TIME);
1486 case LACP_MUX_ATTACHED:
1487 lp->lp_state |= LACP_STATE_SYNC;
1488 lacp_disable_collecting(lp);
1489 lacp_sm_assert_ntt(lp);
1491 case LACP_MUX_COLLECTING:
1492 lacp_enable_collecting(lp);
1493 lacp_disable_distributing(lp);
1494 lacp_sm_assert_ntt(lp);
1496 case LACP_MUX_DISTRIBUTING:
1497 lacp_enable_distributing(lp);
1500 panic("%s: unknown state", __func__);
1503 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1505 lp->lp_mux_state = new_state;
1509 lacp_sm_mux_timer(struct lacp_port *lp)
1511 struct lacp_aggregator *la = lp->lp_aggregator;
1512 char buf[LACP_LAGIDSTR_MAX+1];
1514 KASSERT(la->la_pending > 0, ("no pending event"));
1516 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1517 lacp_format_lagid(&la->la_actor, &la->la_partner,
1519 la->la_pending, la->la_pending - 1));
1524 /* periodic transmit machine */
1527 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1529 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1530 LACP_STATE_TIMEOUT)) {
1534 LACP_DPRINTF((lp, "partner timeout changed\n"));
1537 * FAST_PERIODIC -> SLOW_PERIODIC
1539 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1541 * let lacp_sm_ptx_tx_schedule to update timeout.
1544 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1547 * if timeout has been shortened, assert NTT.
1550 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1551 lacp_sm_assert_ntt(lp);
1556 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1560 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1561 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1567 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1571 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1575 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1576 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1578 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1582 lacp_sm_ptx_timer(struct lacp_port *lp)
1584 lacp_sm_assert_ntt(lp);
1588 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1593 * check LACP_DISABLED first
1596 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1601 * check loopback condition.
1604 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1605 &lp->lp_actor.lip_systemid)) {
1610 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1613 lacp_sm_rx_update_selected(lp, du);
1614 lacp_sm_rx_update_ntt(lp, du);
1615 lacp_sm_rx_record_pdu(lp, du);
1617 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1618 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1619 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1621 lp->lp_state &= ~LACP_STATE_EXPIRED;
1624 * kick transmit machine without waiting the next tick.
1631 lacp_sm_rx_set_expired(struct lacp_port *lp)
1633 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1634 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1635 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1636 lp->lp_state |= LACP_STATE_EXPIRED;
1640 lacp_sm_rx_timer(struct lacp_port *lp)
1642 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1643 /* CURRENT -> EXPIRED */
1644 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1645 lacp_sm_rx_set_expired(lp);
1647 /* EXPIRED -> DEFAULTED */
1648 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1649 lacp_sm_rx_update_default_selected(lp);
1650 lacp_sm_rx_record_default(lp);
1651 lp->lp_state &= ~LACP_STATE_EXPIRED;
1656 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1660 char buf[LACP_STATESTR_MAX+1];
1664 oldpstate = lp->lp_partner.lip_state;
1666 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1667 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1668 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1670 lp->lp_partner = du->ldu_actor;
1672 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1673 LACP_STATE_AGGREGATION) &&
1674 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1675 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1678 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1681 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1683 if (oldpstate != lp->lp_partner.lip_state) {
1684 LACP_DPRINTF((lp, "old pstate %s\n",
1685 lacp_format_state(oldpstate, buf, sizeof(buf))));
1686 LACP_DPRINTF((lp, "new pstate %s\n",
1687 lacp_format_state(lp->lp_partner.lip_state, buf,
1691 /* XXX Hack, still need to implement 5.4.9 para 2,3,4 */
1692 if (lp->lp_lsc->lsc_strict_mode)
1693 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1695 lacp_sm_ptx_update_timeout(lp, oldpstate);
1699 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1704 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1705 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1706 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1707 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1708 lacp_sm_assert_ntt(lp);
1713 lacp_sm_rx_record_default(struct lacp_port *lp)
1719 oldpstate = lp->lp_partner.lip_state;
1720 if (lp->lp_lsc->lsc_strict_mode)
1721 lp->lp_partner = lacp_partner_admin_strict;
1723 lp->lp_partner = lacp_partner_admin_optimistic;;
1724 lp->lp_state |= LACP_STATE_DEFAULTED;
1725 lacp_sm_ptx_update_timeout(lp, oldpstate);
1729 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1730 const struct lacp_peerinfo *info)
1735 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1736 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1737 LACP_STATE_AGGREGATION)) {
1738 lp->lp_selected = LACP_UNSELECTED;
1739 /* mux machine will clean up lp->lp_aggregator */
1744 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1749 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1753 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1758 if (lp->lp_lsc->lsc_strict_mode)
1759 lacp_sm_rx_update_selected_from_peerinfo(lp,
1760 &lacp_partner_admin_strict);
1762 lacp_sm_rx_update_selected_from_peerinfo(lp,
1763 &lacp_partner_admin_optimistic);
1766 /* transmit machine */
1769 lacp_sm_tx(struct lacp_port *lp)
1773 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1775 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1776 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1779 lp->lp_flags &= ~LACP_PORT_NTT;
1782 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1786 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1787 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1788 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1789 LACP_DPRINTF((lp, "rate limited pdu\n"));
1793 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1794 error = lacp_xmit_lacpdu(lp);
1796 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1800 lp->lp_flags &= ~LACP_PORT_NTT;
1802 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1808 lacp_sm_assert_ntt(struct lacp_port *lp)
1811 lp->lp_flags |= LACP_PORT_NTT;
1815 lacp_run_timers(struct lacp_port *lp)
1819 for (i = 0; i < LACP_NTIMER; i++) {
1820 KASSERT(lp->lp_timer[i] >= 0,
1821 ("invalid timer value %d", lp->lp_timer[i]));
1822 if (lp->lp_timer[i] == 0) {
1824 } else if (--lp->lp_timer[i] <= 0) {
1825 if (lacp_timer_funcs[i]) {
1826 (*lacp_timer_funcs[i])(lp);
1833 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1835 struct lacp_softc *lsc = lp->lp_lsc;
1836 struct lagg_port *lgp = lp->lp_lagg;
1837 struct lacp_port *lp2;
1838 struct markerdu *mdu;
1842 if (m->m_pkthdr.len != sizeof(*mdu)) {
1846 if ((m->m_flags & M_MCAST) == 0) {
1850 if (m->m_len < sizeof(*mdu)) {
1851 m = m_pullup(m, sizeof(*mdu));
1857 mdu = mtod(m, struct markerdu *);
1859 if (memcmp(&mdu->mdu_eh.ether_dhost,
1860 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1864 if (mdu->mdu_sph.sph_version != 1) {
1868 switch (mdu->mdu_tlv.tlv_type) {
1869 case MARKER_TYPE_INFO:
1870 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1871 marker_info_tlv_template, TRUE)) {
1874 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1875 memcpy(&mdu->mdu_eh.ether_dhost,
1876 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1877 memcpy(&mdu->mdu_eh.ether_shost,
1878 lgp->lp_lladdr, ETHER_ADDR_LEN);
1879 error = lagg_enqueue(lp->lp_ifp, m);
1882 case MARKER_TYPE_RESPONSE:
1883 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1884 marker_response_tlv_template, TRUE)) {
1887 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6d, id=%u\n",
1888 ntohs(mdu->mdu_info.mi_rq_port), *mdu->mdu_info.mi_rq_system,
1889 ntohl(mdu->mdu_info.mi_rq_xid)));
1891 /* Verify that it is the last marker we sent out */
1892 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
1893 sizeof(struct lacp_markerinfo)))
1897 lp->lp_flags &= ~LACP_PORT_MARK;
1899 if (lsc->lsc_suppress_distributing) {
1900 /* Check if any ports are waiting for a response */
1901 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
1902 if (lp2->lp_flags & LACP_PORT_MARK) {
1909 /* All interface queues are clear */
1910 LACP_DPRINTF((NULL, "queue flush complete\n"));
1911 lsc->lsc_suppress_distributing = FALSE;
1925 LACP_DPRINTF((lp, "bad marker frame\n"));
1931 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
1932 const struct tlv_template *tmpl, boolean_t check_type)
1934 while (/* CONSTCOND */ 1) {
1935 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
1938 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
1939 tlv->tlv_length != tmpl->tmpl_length) {
1942 if (tmpl->tmpl_type == 0) {
1945 tlv = (const struct tlvhdr *)
1946 ((const char *)tlv + tlv->tlv_length);
1955 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
1957 ksnprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
1969 lacp_format_systemid(const struct lacp_systemid *sysid,
1970 char *buf, size_t buflen)
1972 char macbuf[LACP_MACSTR_MAX+1];
1974 ksnprintf(buf, buflen, "%04X,%s",
1975 ntohs(sysid->lsi_prio),
1976 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
1982 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
1984 ksnprintf(buf, buflen, "%04X,%04X",
1985 ntohs(portid->lpi_prio),
1986 ntohs(portid->lpi_portno));
1992 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
1994 char sysid[LACP_SYSTEMIDSTR_MAX+1];
1995 char portid[LACP_PORTIDSTR_MAX+1];
1997 ksnprintf(buf, buflen, "(%s,%04X,%s)",
1998 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
1999 ntohs(peer->lip_key),
2000 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2006 lacp_format_lagid(const struct lacp_peerinfo *a,
2007 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2009 char astr[LACP_PARTNERSTR_MAX+1];
2010 char bstr[LACP_PARTNERSTR_MAX+1];
2014 * there's a convention to display small numbered peer
2018 if (lacp_compare_peerinfo(a, b) > 0) {
2019 const struct lacp_peerinfo *t;
2027 ksnprintf(buf, buflen, "[%s,%s]",
2028 lacp_format_partner(a, astr, sizeof(astr)),
2029 lacp_format_partner(b, bstr, sizeof(bstr)));
2035 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2036 char *buf, size_t buflen)
2042 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2046 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2048 ksnprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2053 lacp_dump_lacpdu(const struct lacpdu *du)
2055 char buf[LACP_PARTNERSTR_MAX+1];
2056 char buf2[LACP_STATESTR_MAX+1];
2058 kprintf("actor=%s\n",
2059 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2060 kprintf("actor.state=%s\n",
2061 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2062 kprintf("partner=%s\n",
2063 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2064 kprintf("partner.state=%s\n",
2065 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2067 kprintf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2071 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2076 kprintf("%s: ", lp->lp_ifp->if_xname);
2079 __va_start(va, fmt);