xref: /freebsd/sys/net/ieee8023ad_lacp.c (revision 6044931335ca0fe7ecf61d2dcebc520c7a557ce6)
1 /*	$NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c)2005 YAMAMOTO Takashi,
7  * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_ratelimit.h"
36 
37 #include <sys/param.h>
38 #include <sys/callout.h>
39 #include <sys/eventhandler.h>
40 #include <sys/mbuf.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h> /* hz */
44 #include <sys/socket.h> /* for net/if.h */
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
47 #include <machine/stdarg.h>
48 #include <sys/lock.h>
49 #include <sys/rwlock.h>
50 #include <sys/taskqueue.h>
51 
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/ethernet.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
58 
59 #include <net/if_lagg.h>
60 #include <net/ieee8023ad_lacp.h>
61 
62 /*
63  * actor system priority and port priority.
64  * XXX should be configurable.
65  */
66 
67 #define	LACP_SYSTEM_PRIO	0x8000
68 #define	LACP_PORT_PRIO		0x8000
69 
70 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
71     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
72 
73 static const struct tlv_template lacp_info_tlv_template[] = {
74 	{ LACP_TYPE_ACTORINFO,
75 	    sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
76 	{ LACP_TYPE_PARTNERINFO,
77 	    sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
78 	{ LACP_TYPE_COLLECTORINFO,
79 	    sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
80 	{ 0, 0 },
81 };
82 
83 static const struct tlv_template marker_info_tlv_template[] = {
84 	{ MARKER_TYPE_INFO,
85 	    sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
86 	{ 0, 0 },
87 };
88 
89 static const struct tlv_template marker_response_tlv_template[] = {
90 	{ MARKER_TYPE_RESPONSE,
91 	    sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
92 	{ 0, 0 },
93 };
94 
95 typedef void (*lacp_timer_func_t)(struct lacp_port *);
96 
97 static void	lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
98 static void	lacp_fill_markerinfo(struct lacp_port *,
99 		    struct lacp_markerinfo *);
100 
101 static uint64_t	lacp_aggregator_bandwidth(struct lacp_aggregator *);
102 static void	lacp_suppress_distributing(struct lacp_softc *,
103 		    struct lacp_aggregator *);
104 static void	lacp_transit_expire(void *);
105 static void	lacp_update_portmap(struct lacp_softc *);
106 static void	lacp_select_active_aggregator(struct lacp_softc *);
107 static uint16_t	lacp_compose_key(struct lacp_port *);
108 static int	tlv_check(const void *, size_t, const struct tlvhdr *,
109 		    const struct tlv_template *, boolean_t);
110 static void	lacp_tick(void *);
111 
112 static void	lacp_fill_aggregator_id(struct lacp_aggregator *,
113 		    const struct lacp_port *);
114 static void	lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
115 		    const struct lacp_peerinfo *);
116 static int	lacp_aggregator_is_compatible(const struct lacp_aggregator *,
117 		    const struct lacp_port *);
118 static int	lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
119 		    const struct lacp_peerinfo *);
120 
121 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
122 		    struct lacp_port *);
123 static void	lacp_aggregator_addref(struct lacp_softc *,
124 		    struct lacp_aggregator *);
125 static void	lacp_aggregator_delref(struct lacp_softc *,
126 		    struct lacp_aggregator *);
127 
128 /* receive machine */
129 
130 static int	lacp_pdu_input(struct lacp_port *, struct mbuf *);
131 static int	lacp_marker_input(struct lacp_port *, struct mbuf *);
132 static void	lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
133 static void	lacp_sm_rx_timer(struct lacp_port *);
134 static void	lacp_sm_rx_set_expired(struct lacp_port *);
135 static void	lacp_sm_rx_update_ntt(struct lacp_port *,
136 		    const struct lacpdu *);
137 static void	lacp_sm_rx_record_pdu(struct lacp_port *,
138 		    const struct lacpdu *);
139 static void	lacp_sm_rx_update_selected(struct lacp_port *,
140 		    const struct lacpdu *);
141 static void	lacp_sm_rx_record_default(struct lacp_port *);
142 static void	lacp_sm_rx_update_default_selected(struct lacp_port *);
143 static void	lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
144 		    const struct lacp_peerinfo *);
145 
146 /* mux machine */
147 
148 static void	lacp_sm_mux(struct lacp_port *);
149 static void	lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
150 static void	lacp_sm_mux_timer(struct lacp_port *);
151 
152 /* periodic transmit machine */
153 
154 static void	lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
155 static void	lacp_sm_ptx_tx_schedule(struct lacp_port *);
156 static void	lacp_sm_ptx_timer(struct lacp_port *);
157 
158 /* transmit machine */
159 
160 static void	lacp_sm_tx(struct lacp_port *);
161 static void	lacp_sm_assert_ntt(struct lacp_port *);
162 
163 static void	lacp_run_timers(struct lacp_port *);
164 static int	lacp_compare_peerinfo(const struct lacp_peerinfo *,
165 		    const struct lacp_peerinfo *);
166 static int	lacp_compare_systemid(const struct lacp_systemid *,
167 		    const struct lacp_systemid *);
168 static void	lacp_port_enable(struct lacp_port *);
169 static void	lacp_port_disable(struct lacp_port *);
170 static void	lacp_select(struct lacp_port *);
171 static void	lacp_unselect(struct lacp_port *);
172 static void	lacp_disable_collecting(struct lacp_port *);
173 static void	lacp_enable_collecting(struct lacp_port *);
174 static void	lacp_disable_distributing(struct lacp_port *);
175 static void	lacp_enable_distributing(struct lacp_port *);
176 static int	lacp_xmit_lacpdu(struct lacp_port *);
177 static int	lacp_xmit_marker(struct lacp_port *);
178 
179 /* Debugging */
180 
181 static void	lacp_dump_lacpdu(const struct lacpdu *);
182 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
183 		    size_t);
184 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
185 		    const struct lacp_peerinfo *, char *, size_t);
186 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
187 		    char *, size_t);
188 static const char *lacp_format_state(uint8_t, char *, size_t);
189 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
190 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
191 		    size_t);
192 static const char *lacp_format_portid(const struct lacp_portid *, char *,
193 		    size_t);
194 static void	lacp_dprintf(const struct lacp_port *, const char *, ...)
195 		    __attribute__((__format__(__printf__, 2, 3)));
196 
197 VNET_DEFINE_STATIC(int, lacp_debug);
198 #define	V_lacp_debug	VNET(lacp_debug)
199 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad");
200 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
201     &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
202 
203 VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
204 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
205     CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
206     "LACP strict protocol compliance default");
207 
208 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
209 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
210 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
211 
212 /*
213  * partner administration variables.
214  * XXX should be configurable.
215  */
216 
217 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
218 	.lip_systemid = { .lsi_prio = 0xffff },
219 	.lip_portid = { .lpi_prio = 0xffff },
220 	.lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
221 	    LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
222 };
223 
224 static const struct lacp_peerinfo lacp_partner_admin_strict = {
225 	.lip_systemid = { .lsi_prio = 0xffff },
226 	.lip_portid = { .lpi_prio = 0xffff },
227 	.lip_state = 0,
228 };
229 
230 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
231 	[LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
232 	[LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
233 	[LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
234 };
235 
236 struct mbuf *
237 lacp_input(struct lagg_port *lgp, struct mbuf *m)
238 {
239 	struct lacp_port *lp = LACP_PORT(lgp);
240 	uint8_t subtype;
241 
242 	if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
243 		m_freem(m);
244 		return (NULL);
245 	}
246 
247 	m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
248 	switch (subtype) {
249 		case SLOWPROTOCOLS_SUBTYPE_LACP:
250 			lacp_pdu_input(lp, m);
251 			return (NULL);
252 
253 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
254 			lacp_marker_input(lp, m);
255 			return (NULL);
256 	}
257 
258 	/* Not a subtype we are interested in */
259 	return (m);
260 }
261 
262 /*
263  * lacp_pdu_input: process lacpdu
264  */
265 static int
266 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
267 {
268 	struct lacp_softc *lsc = lp->lp_lsc;
269 	struct lacpdu *du;
270 	int error = 0;
271 
272 	if (m->m_pkthdr.len != sizeof(*du)) {
273 		goto bad;
274 	}
275 
276 	if ((m->m_flags & M_MCAST) == 0) {
277 		goto bad;
278 	}
279 
280 	if (m->m_len < sizeof(*du)) {
281 		m = m_pullup(m, sizeof(*du));
282 		if (m == NULL) {
283 			return (ENOMEM);
284 		}
285 	}
286 
287 	du = mtod(m, struct lacpdu *);
288 
289 	if (memcmp(&du->ldu_eh.ether_dhost,
290 	    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
291 		goto bad;
292 	}
293 
294 	/*
295 	 * ignore the version for compatibility with
296 	 * the future protocol revisions.
297 	 */
298 #if 0
299 	if (du->ldu_sph.sph_version != 1) {
300 		goto bad;
301 	}
302 #endif
303 
304 	/*
305 	 * ignore tlv types for compatibility with
306 	 * the future protocol revisions.
307 	 */
308 	if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
309 	    lacp_info_tlv_template, FALSE)) {
310 		goto bad;
311 	}
312 
313         if (V_lacp_debug > 0) {
314 		lacp_dprintf(lp, "lacpdu receive\n");
315 		lacp_dump_lacpdu(du);
316 	}
317 
318 	if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
319 		LACP_TPRINTF((lp, "Dropping RX PDU\n"));
320 		goto bad;
321 	}
322 
323 	LACP_LOCK(lsc);
324 	lacp_sm_rx(lp, du);
325 	LACP_UNLOCK(lsc);
326 
327 	m_freem(m);
328 	return (error);
329 
330 bad:
331 	m_freem(m);
332 	return (EINVAL);
333 }
334 
335 static void
336 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
337 {
338 	struct lagg_port *lgp = lp->lp_lagg;
339 	struct lagg_softc *sc = lgp->lp_softc;
340 
341 	info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
342 	memcpy(&info->lip_systemid.lsi_mac,
343 	    IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
344 	info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
345 	info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
346 	info->lip_state = lp->lp_state;
347 }
348 
349 static void
350 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
351 {
352 	struct ifnet *ifp = lp->lp_ifp;
353 
354 	/* Fill in the port index and system id (encoded as the MAC) */
355 	info->mi_rq_port = htons(ifp->if_index);
356 	memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
357 	info->mi_rq_xid = htonl(0);
358 }
359 
360 static int
361 lacp_xmit_lacpdu(struct lacp_port *lp)
362 {
363 	struct lagg_port *lgp = lp->lp_lagg;
364 	struct mbuf *m;
365 	struct lacpdu *du;
366 	int error;
367 
368 	LACP_LOCK_ASSERT(lp->lp_lsc);
369 
370 	m = m_gethdr(M_NOWAIT, MT_DATA);
371 	if (m == NULL) {
372 		return (ENOMEM);
373 	}
374 	m->m_len = m->m_pkthdr.len = sizeof(*du);
375 
376 	du = mtod(m, struct lacpdu *);
377 	memset(du, 0, sizeof(*du));
378 
379 	memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
380 	    ETHER_ADDR_LEN);
381 	memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
382 	du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
383 
384 	du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
385 	du->ldu_sph.sph_version = 1;
386 
387 	TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
388 	du->ldu_actor = lp->lp_actor;
389 
390 	TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
391 	    sizeof(du->ldu_partner));
392 	du->ldu_partner = lp->lp_partner;
393 
394 	TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
395 	    sizeof(du->ldu_collector));
396 	du->ldu_collector.lci_maxdelay = 0;
397 
398 	if (V_lacp_debug > 0) {
399 		lacp_dprintf(lp, "lacpdu transmit\n");
400 		lacp_dump_lacpdu(du);
401 	}
402 
403 	m->m_flags |= M_MCAST;
404 
405 	/*
406 	 * XXX should use higher priority queue.
407 	 * otherwise network congestion can break aggregation.
408 	 */
409 
410 	error = lagg_enqueue(lp->lp_ifp, m);
411 	return (error);
412 }
413 
414 static int
415 lacp_xmit_marker(struct lacp_port *lp)
416 {
417 	struct lagg_port *lgp = lp->lp_lagg;
418 	struct mbuf *m;
419 	struct markerdu *mdu;
420 	int error;
421 
422 	LACP_LOCK_ASSERT(lp->lp_lsc);
423 
424 	m = m_gethdr(M_NOWAIT, MT_DATA);
425 	if (m == NULL) {
426 		return (ENOMEM);
427 	}
428 	m->m_len = m->m_pkthdr.len = sizeof(*mdu);
429 
430 	mdu = mtod(m, struct markerdu *);
431 	memset(mdu, 0, sizeof(*mdu));
432 
433 	memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
434 	    ETHER_ADDR_LEN);
435 	memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
436 	mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
437 
438 	mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
439 	mdu->mdu_sph.sph_version = 1;
440 
441 	/* Bump the transaction id and copy over the marker info */
442 	lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
443 	TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
444 	mdu->mdu_info = lp->lp_marker;
445 
446 	LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
447 	    ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
448 	    ntohl(mdu->mdu_info.mi_rq_xid)));
449 
450 	m->m_flags |= M_MCAST;
451 	error = lagg_enqueue(lp->lp_ifp, m);
452 	return (error);
453 }
454 
455 void
456 lacp_linkstate(struct lagg_port *lgp)
457 {
458 	struct lacp_port *lp = LACP_PORT(lgp);
459 	struct lacp_softc *lsc = lp->lp_lsc;
460 	struct ifnet *ifp = lgp->lp_ifp;
461 	struct ifmediareq ifmr;
462 	int error = 0;
463 	u_int media;
464 	uint8_t old_state;
465 	uint16_t old_key;
466 
467 	bzero((char *)&ifmr, sizeof(ifmr));
468 	error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
469 	if (error != 0) {
470 		bzero((char *)&ifmr, sizeof(ifmr));
471 		error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
472 	}
473 	if (error != 0)
474 		return;
475 
476 	LACP_LOCK(lsc);
477 	media = ifmr.ifm_active;
478 	LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
479 	    "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
480 	    (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
481 	old_state = lp->lp_state;
482 	old_key = lp->lp_key;
483 
484 	lp->lp_media = media;
485 	/*
486 	 * If the port is not an active full duplex Ethernet link then it can
487 	 * not be aggregated.
488 	 */
489 	if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
490 	    ifp->if_link_state != LINK_STATE_UP) {
491 		lacp_port_disable(lp);
492 	} else {
493 		lacp_port_enable(lp);
494 	}
495 	lp->lp_key = lacp_compose_key(lp);
496 
497 	if (old_state != lp->lp_state || old_key != lp->lp_key) {
498 		LACP_DPRINTF((lp, "-> UNSELECTED\n"));
499 		lp->lp_selected = LACP_UNSELECTED;
500 	}
501 	LACP_UNLOCK(lsc);
502 }
503 
504 static void
505 lacp_tick(void *arg)
506 {
507 	struct lacp_softc *lsc = arg;
508 	struct lacp_port *lp;
509 
510 	LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
511 		if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
512 			continue;
513 
514 		CURVNET_SET(lp->lp_ifp->if_vnet);
515 		lacp_run_timers(lp);
516 
517 		lacp_select(lp);
518 		lacp_sm_mux(lp);
519 		lacp_sm_tx(lp);
520 		lacp_sm_ptx_tx_schedule(lp);
521 		CURVNET_RESTORE();
522 	}
523 	callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
524 }
525 
526 int
527 lacp_port_create(struct lagg_port *lgp)
528 {
529 	struct lagg_softc *sc = lgp->lp_softc;
530 	struct lacp_softc *lsc = LACP_SOFTC(sc);
531 	struct lacp_port *lp;
532 	struct ifnet *ifp = lgp->lp_ifp;
533 	struct sockaddr_dl sdl;
534 	struct ifmultiaddr *rifma = NULL;
535 	int error;
536 
537 	link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
538 	sdl.sdl_alen = ETHER_ADDR_LEN;
539 
540 	bcopy(&ethermulticastaddr_slowprotocols,
541 	    LLADDR(&sdl), ETHER_ADDR_LEN);
542 	error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
543 	if (error) {
544 		printf("%s: ADDMULTI failed on %s\n", __func__,
545 		    lgp->lp_ifp->if_xname);
546 		return (error);
547 	}
548 
549 	lp = malloc(sizeof(struct lacp_port),
550 	    M_DEVBUF, M_NOWAIT|M_ZERO);
551 	if (lp == NULL)
552 		return (ENOMEM);
553 
554 	LACP_LOCK(lsc);
555 	lgp->lp_psc = lp;
556 	lp->lp_ifp = ifp;
557 	lp->lp_lagg = lgp;
558 	lp->lp_lsc = lsc;
559 	lp->lp_ifma = rifma;
560 
561 	LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
562 
563 	lacp_fill_actorinfo(lp, &lp->lp_actor);
564 	lacp_fill_markerinfo(lp, &lp->lp_marker);
565 	lp->lp_state = LACP_STATE_ACTIVITY;
566 	lp->lp_aggregator = NULL;
567 	lacp_sm_rx_set_expired(lp);
568 	LACP_UNLOCK(lsc);
569 	lacp_linkstate(lgp);
570 
571 	return (0);
572 }
573 
574 void
575 lacp_port_destroy(struct lagg_port *lgp)
576 {
577 	struct lacp_port *lp = LACP_PORT(lgp);
578 	struct lacp_softc *lsc = lp->lp_lsc;
579 	int i;
580 
581 	LACP_LOCK(lsc);
582 	for (i = 0; i < LACP_NTIMER; i++) {
583 		LACP_TIMER_DISARM(lp, i);
584 	}
585 
586 	lacp_disable_collecting(lp);
587 	lacp_disable_distributing(lp);
588 	lacp_unselect(lp);
589 
590 	LIST_REMOVE(lp, lp_next);
591 	LACP_UNLOCK(lsc);
592 
593 	/* The address may have already been removed by if_purgemaddrs() */
594 	if (!lgp->lp_detaching)
595 		if_delmulti_ifma(lp->lp_ifma);
596 
597 	free(lp, M_DEVBUF);
598 }
599 
600 void
601 lacp_req(struct lagg_softc *sc, void *data)
602 {
603 	struct lacp_opreq *req = (struct lacp_opreq *)data;
604 	struct lacp_softc *lsc = LACP_SOFTC(sc);
605 	struct lacp_aggregator *la;
606 
607 	bzero(req, sizeof(struct lacp_opreq));
608 
609 	/*
610 	 * If the LACP softc is NULL, return with the opreq structure full of
611 	 * zeros.  It is normal for the softc to be NULL while the lagg is
612 	 * being destroyed.
613 	 */
614 	if (NULL == lsc)
615 		return;
616 
617 	la = lsc->lsc_active_aggregator;
618 	LACP_LOCK(lsc);
619 	if (la != NULL) {
620 		req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
621 		memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
622 		    ETHER_ADDR_LEN);
623 		req->actor_key = ntohs(la->la_actor.lip_key);
624 		req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
625 		req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
626 		req->actor_state = la->la_actor.lip_state;
627 
628 		req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
629 		memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
630 		    ETHER_ADDR_LEN);
631 		req->partner_key = ntohs(la->la_partner.lip_key);
632 		req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
633 		req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
634 		req->partner_state = la->la_partner.lip_state;
635 	}
636 	LACP_UNLOCK(lsc);
637 }
638 
639 void
640 lacp_portreq(struct lagg_port *lgp, void *data)
641 {
642 	struct lacp_opreq *req = (struct lacp_opreq *)data;
643 	struct lacp_port *lp = LACP_PORT(lgp);
644 	struct lacp_softc *lsc = lp->lp_lsc;
645 
646 	LACP_LOCK(lsc);
647 	req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
648 	memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
649 	    ETHER_ADDR_LEN);
650 	req->actor_key = ntohs(lp->lp_actor.lip_key);
651 	req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
652 	req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
653 	req->actor_state = lp->lp_actor.lip_state;
654 
655 	req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
656 	memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
657 	    ETHER_ADDR_LEN);
658 	req->partner_key = ntohs(lp->lp_partner.lip_key);
659 	req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
660 	req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
661 	req->partner_state = lp->lp_partner.lip_state;
662 	LACP_UNLOCK(lsc);
663 }
664 
665 static void
666 lacp_disable_collecting(struct lacp_port *lp)
667 {
668 	LACP_DPRINTF((lp, "collecting disabled\n"));
669 	lp->lp_state &= ~LACP_STATE_COLLECTING;
670 }
671 
672 static void
673 lacp_enable_collecting(struct lacp_port *lp)
674 {
675 	LACP_DPRINTF((lp, "collecting enabled\n"));
676 	lp->lp_state |= LACP_STATE_COLLECTING;
677 }
678 
679 static void
680 lacp_disable_distributing(struct lacp_port *lp)
681 {
682 	struct lacp_aggregator *la = lp->lp_aggregator;
683 	struct lacp_softc *lsc = lp->lp_lsc;
684 	struct lagg_softc *sc = lsc->lsc_softc;
685 	char buf[LACP_LAGIDSTR_MAX+1];
686 
687 	LACP_LOCK_ASSERT(lsc);
688 
689 	if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
690 		return;
691 	}
692 
693 	KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
694 	KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
695 	KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
696 
697 	LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
698 	    "nports %d -> %d\n",
699 	    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
700 	    la->la_nports, la->la_nports - 1));
701 
702 	TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
703 	la->la_nports--;
704 	sc->sc_active = la->la_nports;
705 
706 	if (lsc->lsc_active_aggregator == la) {
707 		lacp_suppress_distributing(lsc, la);
708 		lacp_select_active_aggregator(lsc);
709 		/* regenerate the port map, the active aggregator has changed */
710 		lacp_update_portmap(lsc);
711 	}
712 
713 	lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
714 	if_link_state_change(sc->sc_ifp,
715 	    sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
716 }
717 
718 static void
719 lacp_enable_distributing(struct lacp_port *lp)
720 {
721 	struct lacp_aggregator *la = lp->lp_aggregator;
722 	struct lacp_softc *lsc = lp->lp_lsc;
723 	struct lagg_softc *sc = lsc->lsc_softc;
724 	char buf[LACP_LAGIDSTR_MAX+1];
725 
726 	LACP_LOCK_ASSERT(lsc);
727 
728 	if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
729 		return;
730 	}
731 
732 	LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
733 	    "nports %d -> %d\n",
734 	    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
735 	    la->la_nports, la->la_nports + 1));
736 
737 	KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
738 	TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
739 	la->la_nports++;
740 	sc->sc_active = la->la_nports;
741 
742 	lp->lp_state |= LACP_STATE_DISTRIBUTING;
743 
744 	if (lsc->lsc_active_aggregator == la) {
745 		lacp_suppress_distributing(lsc, la);
746 		lacp_update_portmap(lsc);
747 	} else
748 		/* try to become the active aggregator */
749 		lacp_select_active_aggregator(lsc);
750 
751 	if_link_state_change(sc->sc_ifp,
752 	    sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
753 }
754 
755 static void
756 lacp_transit_expire(void *vp)
757 {
758 	struct lacp_softc *lsc = vp;
759 
760 	LACP_LOCK_ASSERT(lsc);
761 
762 	CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
763 	LACP_TRACE(NULL);
764 	CURVNET_RESTORE();
765 
766 	lsc->lsc_suppress_distributing = FALSE;
767 }
768 
769 void
770 lacp_attach(struct lagg_softc *sc)
771 {
772 	struct lacp_softc *lsc;
773 
774 	lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
775 
776 	sc->sc_psc = lsc;
777 	lsc->lsc_softc = sc;
778 
779 	lsc->lsc_hashkey = m_ether_tcpip_hash_init();
780 	lsc->lsc_active_aggregator = NULL;
781 	lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
782 	LACP_LOCK_INIT(lsc);
783 	TAILQ_INIT(&lsc->lsc_aggregators);
784 	LIST_INIT(&lsc->lsc_ports);
785 
786 	callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
787 	callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
788 
789 	/* if the lagg is already up then do the same */
790 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
791 		lacp_init(sc);
792 }
793 
794 void
795 lacp_detach(void *psc)
796 {
797 	struct lacp_softc *lsc = (struct lacp_softc *)psc;
798 
799 	KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
800 	    ("aggregators still active"));
801 	KASSERT(lsc->lsc_active_aggregator == NULL,
802 	    ("aggregator still attached"));
803 
804 	callout_drain(&lsc->lsc_transit_callout);
805 	callout_drain(&lsc->lsc_callout);
806 
807 	LACP_LOCK_DESTROY(lsc);
808 	free(lsc, M_DEVBUF);
809 }
810 
811 void
812 lacp_init(struct lagg_softc *sc)
813 {
814 	struct lacp_softc *lsc = LACP_SOFTC(sc);
815 
816 	LACP_LOCK(lsc);
817 	callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
818 	LACP_UNLOCK(lsc);
819 }
820 
821 void
822 lacp_stop(struct lagg_softc *sc)
823 {
824 	struct lacp_softc *lsc = LACP_SOFTC(sc);
825 
826 	LACP_LOCK(lsc);
827 	callout_stop(&lsc->lsc_transit_callout);
828 	callout_stop(&lsc->lsc_callout);
829 	LACP_UNLOCK(lsc);
830 }
831 
832 struct lagg_port *
833 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
834 {
835 	struct lacp_softc *lsc = LACP_SOFTC(sc);
836 	struct lacp_portmap *pm;
837 	struct lacp_port *lp;
838 	uint32_t hash;
839 
840 	if (__predict_false(lsc->lsc_suppress_distributing)) {
841 		LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
842 		return (NULL);
843 	}
844 
845 	pm = &lsc->lsc_pmap[lsc->lsc_activemap];
846 	if (pm->pm_count == 0) {
847 		LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
848 		return (NULL);
849 	}
850 
851 	if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
852 	    M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
853 		hash = m->m_pkthdr.flowid >> sc->flowid_shift;
854 	else
855 		hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
856 	hash %= pm->pm_count;
857 	lp = pm->pm_map[hash];
858 
859 	KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0,
860 	    ("aggregated port is not distributing"));
861 
862 	return (lp->lp_lagg);
863 }
864 
865 #ifdef RATELIMIT
866 struct lagg_port *
867 lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t flowid)
868 {
869 	struct lacp_softc *lsc = LACP_SOFTC(sc);
870 	struct lacp_portmap *pm;
871 	struct lacp_port *lp;
872 	uint32_t hash;
873 
874 	if (__predict_false(lsc->lsc_suppress_distributing)) {
875 		LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
876 		return (NULL);
877 	}
878 
879 	pm = &lsc->lsc_pmap[lsc->lsc_activemap];
880 	if (pm->pm_count == 0) {
881 		LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
882 		return (NULL);
883 	}
884 
885 	hash = flowid >> sc->flowid_shift;
886 	hash %= pm->pm_count;
887 	lp = pm->pm_map[hash];
888 
889 	return (lp->lp_lagg);
890 }
891 #endif
892 
893 /*
894  * lacp_suppress_distributing: drop transmit packets for a while
895  * to preserve packet ordering.
896  */
897 
898 static void
899 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
900 {
901 	struct lacp_port *lp;
902 
903 	if (lsc->lsc_active_aggregator != la) {
904 		return;
905 	}
906 
907 	LACP_TRACE(NULL);
908 
909 	lsc->lsc_suppress_distributing = TRUE;
910 
911 	/* send a marker frame down each port to verify the queues are empty */
912 	LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
913 		lp->lp_flags |= LACP_PORT_MARK;
914 		lacp_xmit_marker(lp);
915 	}
916 
917 	/* set a timeout for the marker frames */
918 	callout_reset(&lsc->lsc_transit_callout,
919 	    LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
920 }
921 
922 static int
923 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
924     const struct lacp_peerinfo *b)
925 {
926 	return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
927 }
928 
929 static int
930 lacp_compare_systemid(const struct lacp_systemid *a,
931     const struct lacp_systemid *b)
932 {
933 	return (memcmp(a, b, sizeof(*a)));
934 }
935 
936 #if 0	/* unused */
937 static int
938 lacp_compare_portid(const struct lacp_portid *a,
939     const struct lacp_portid *b)
940 {
941 	return (memcmp(a, b, sizeof(*a)));
942 }
943 #endif
944 
945 static uint64_t
946 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
947 {
948 	struct lacp_port *lp;
949 	uint64_t speed;
950 
951 	lp = TAILQ_FIRST(&la->la_ports);
952 	if (lp == NULL) {
953 		return (0);
954 	}
955 
956 	speed = ifmedia_baudrate(lp->lp_media);
957 	speed *= la->la_nports;
958 	if (speed == 0) {
959 		LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
960 		    lp->lp_media, la->la_nports));
961 	}
962 
963 	return (speed);
964 }
965 
966 /*
967  * lacp_select_active_aggregator: select an aggregator to be used to transmit
968  * packets from lagg(4) interface.
969  */
970 
971 static void
972 lacp_select_active_aggregator(struct lacp_softc *lsc)
973 {
974 	struct lacp_aggregator *la;
975 	struct lacp_aggregator *best_la = NULL;
976 	uint64_t best_speed = 0;
977 	char buf[LACP_LAGIDSTR_MAX+1];
978 
979 	LACP_TRACE(NULL);
980 
981 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
982 		uint64_t speed;
983 
984 		if (la->la_nports == 0) {
985 			continue;
986 		}
987 
988 		speed = lacp_aggregator_bandwidth(la);
989 		LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
990 		    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
991 		    speed, la->la_nports));
992 
993 		/*
994 		 * This aggregator is chosen if the partner has a better
995 		 * system priority or, the total aggregated speed is higher
996 		 * or, it is already the chosen aggregator
997 		 */
998 		if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
999 		    LACP_SYS_PRI(best_la->la_partner)) ||
1000 		    speed > best_speed ||
1001 		    (speed == best_speed &&
1002 		    la == lsc->lsc_active_aggregator)) {
1003 			best_la = la;
1004 			best_speed = speed;
1005 		}
1006 	}
1007 
1008 	KASSERT(best_la == NULL || best_la->la_nports > 0,
1009 	    ("invalid aggregator refcnt"));
1010 	KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1011 	    ("invalid aggregator list"));
1012 
1013 	if (lsc->lsc_active_aggregator != best_la) {
1014 		LACP_DPRINTF((NULL, "active aggregator changed\n"));
1015 		LACP_DPRINTF((NULL, "old %s\n",
1016 		    lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1017 		    buf, sizeof(buf))));
1018 	} else {
1019 		LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1020 	}
1021 	LACP_DPRINTF((NULL, "new %s\n",
1022 	    lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1023 
1024 	if (lsc->lsc_active_aggregator != best_la) {
1025 		lsc->lsc_active_aggregator = best_la;
1026 		lacp_update_portmap(lsc);
1027 		if (best_la) {
1028 			lacp_suppress_distributing(lsc, best_la);
1029 		}
1030 	}
1031 }
1032 
1033 /*
1034  * Updated the inactive portmap array with the new list of ports and
1035  * make it live.
1036  */
1037 static void
1038 lacp_update_portmap(struct lacp_softc *lsc)
1039 {
1040 	struct lagg_softc *sc = lsc->lsc_softc;
1041 	struct lacp_aggregator *la;
1042 	struct lacp_portmap *p;
1043 	struct lacp_port *lp;
1044 	uint64_t speed;
1045 	u_int newmap;
1046 	int i;
1047 
1048 	newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1049 	p = &lsc->lsc_pmap[newmap];
1050 	la = lsc->lsc_active_aggregator;
1051 	speed = 0;
1052 	bzero(p, sizeof(struct lacp_portmap));
1053 
1054 	if (la != NULL && la->la_nports > 0) {
1055 		p->pm_count = la->la_nports;
1056 		i = 0;
1057 		TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
1058 			p->pm_map[i++] = lp;
1059 		KASSERT(i == p->pm_count, ("Invalid port count"));
1060 		speed = lacp_aggregator_bandwidth(la);
1061 	}
1062 	sc->sc_ifp->if_baudrate = speed;
1063 
1064 	/* switch the active portmap over */
1065 	atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1066 	LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1067 		    lsc->lsc_activemap,
1068 		    lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1069 }
1070 
1071 static uint16_t
1072 lacp_compose_key(struct lacp_port *lp)
1073 {
1074 	struct lagg_port *lgp = lp->lp_lagg;
1075 	struct lagg_softc *sc = lgp->lp_softc;
1076 	u_int media = lp->lp_media;
1077 	uint16_t key;
1078 
1079 	if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1080 
1081 		/*
1082 		 * non-aggregatable links should have unique keys.
1083 		 *
1084 		 * XXX this isn't really unique as if_index is 16 bit.
1085 		 */
1086 
1087 		/* bit 0..14:	(some bits of) if_index of this port */
1088 		key = lp->lp_ifp->if_index;
1089 		/* bit 15:	1 */
1090 		key |= 0x8000;
1091 	} else {
1092 		u_int subtype = IFM_SUBTYPE(media);
1093 
1094 		KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1095 		KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1096 
1097 		/* bit 0..4:	IFM_SUBTYPE modulo speed */
1098 		switch (subtype) {
1099 		case IFM_10_T:
1100 		case IFM_10_2:
1101 		case IFM_10_5:
1102 		case IFM_10_STP:
1103 		case IFM_10_FL:
1104 			key = IFM_10_T;
1105 			break;
1106 		case IFM_100_TX:
1107 		case IFM_100_FX:
1108 		case IFM_100_T4:
1109 		case IFM_100_VG:
1110 		case IFM_100_T2:
1111 		case IFM_100_T:
1112 			key = IFM_100_TX;
1113 			break;
1114 		case IFM_1000_SX:
1115 		case IFM_1000_LX:
1116 		case IFM_1000_CX:
1117 		case IFM_1000_T:
1118 		case IFM_1000_KX:
1119 		case IFM_1000_SGMII:
1120 		case IFM_1000_CX_SGMII:
1121 			key = IFM_1000_SX;
1122 			break;
1123 		case IFM_10G_LR:
1124 		case IFM_10G_SR:
1125 		case IFM_10G_CX4:
1126 		case IFM_10G_TWINAX:
1127 		case IFM_10G_TWINAX_LONG:
1128 		case IFM_10G_LRM:
1129 		case IFM_10G_T:
1130 		case IFM_10G_KX4:
1131 		case IFM_10G_KR:
1132 		case IFM_10G_CR1:
1133 		case IFM_10G_ER:
1134 		case IFM_10G_SFI:
1135 		case IFM_10G_AOC:
1136 			key = IFM_10G_LR;
1137 			break;
1138 		case IFM_20G_KR2:
1139 			key = IFM_20G_KR2;
1140 			break;
1141 		case IFM_2500_KX:
1142 		case IFM_2500_T:
1143 			key = IFM_2500_KX;
1144 			break;
1145 		case IFM_5000_T:
1146 			key = IFM_5000_T;
1147 			break;
1148 		case IFM_50G_PCIE:
1149 		case IFM_50G_CR2:
1150 		case IFM_50G_KR2:
1151 			key = IFM_50G_PCIE;
1152 			break;
1153 		case IFM_56G_R4:
1154 			key = IFM_56G_R4;
1155 			break;
1156 		case IFM_25G_PCIE:
1157 		case IFM_25G_CR:
1158 		case IFM_25G_KR:
1159 		case IFM_25G_SR:
1160 		case IFM_25G_LR:
1161 		case IFM_25G_ACC:
1162 		case IFM_25G_AOC:
1163 			key = IFM_25G_PCIE;
1164 			break;
1165 		case IFM_40G_CR4:
1166 		case IFM_40G_SR4:
1167 		case IFM_40G_LR4:
1168 		case IFM_40G_XLPPI:
1169 		case IFM_40G_KR4:
1170 			key = IFM_40G_CR4;
1171 			break;
1172 		case IFM_100G_CR4:
1173 		case IFM_100G_SR4:
1174 		case IFM_100G_KR4:
1175 		case IFM_100G_LR4:
1176 			key = IFM_100G_CR4;
1177 			break;
1178 		default:
1179 			key = subtype;
1180 			break;
1181 		}
1182 		/* bit 5..14:	(some bits of) if_index of lagg device */
1183 		key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1184 		/* bit 15:	0 */
1185 	}
1186 	return (htons(key));
1187 }
1188 
1189 static void
1190 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1191 {
1192 	char buf[LACP_LAGIDSTR_MAX+1];
1193 
1194 	LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1195 	    __func__,
1196 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1197 	    buf, sizeof(buf)),
1198 	    la->la_refcnt, la->la_refcnt + 1));
1199 
1200 	KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1201 	la->la_refcnt++;
1202 	KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1203 }
1204 
1205 static void
1206 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1207 {
1208 	char buf[LACP_LAGIDSTR_MAX+1];
1209 
1210 	LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1211 	    __func__,
1212 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1213 	    buf, sizeof(buf)),
1214 	    la->la_refcnt, la->la_refcnt - 1));
1215 
1216 	KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1217 	la->la_refcnt--;
1218 	if (la->la_refcnt > 0) {
1219 		return;
1220 	}
1221 
1222 	KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1223 	KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1224 
1225 	TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1226 
1227 	free(la, M_DEVBUF);
1228 }
1229 
1230 /*
1231  * lacp_aggregator_get: allocate an aggregator.
1232  */
1233 
1234 static struct lacp_aggregator *
1235 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1236 {
1237 	struct lacp_aggregator *la;
1238 
1239 	la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1240 	if (la) {
1241 		la->la_refcnt = 1;
1242 		la->la_nports = 0;
1243 		TAILQ_INIT(&la->la_ports);
1244 		la->la_pending = 0;
1245 		TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1246 	}
1247 
1248 	return (la);
1249 }
1250 
1251 /*
1252  * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1253  */
1254 
1255 static void
1256 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1257 {
1258 	lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1259 	lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1260 
1261 	la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1262 }
1263 
1264 static void
1265 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1266     const struct lacp_peerinfo *lpi_port)
1267 {
1268 	memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1269 	lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1270 	lpi_aggr->lip_key = lpi_port->lip_key;
1271 }
1272 
1273 /*
1274  * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1275  */
1276 
1277 static int
1278 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1279     const struct lacp_port *lp)
1280 {
1281 	if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1282 	    !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1283 		return (0);
1284 	}
1285 
1286 	if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
1287 		return (0);
1288 	}
1289 
1290 	if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
1291 		return (0);
1292 	}
1293 
1294 	if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
1295 		return (0);
1296 	}
1297 
1298 	return (1);
1299 }
1300 
1301 static int
1302 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1303     const struct lacp_peerinfo *b)
1304 {
1305 	if (memcmp(&a->lip_systemid, &b->lip_systemid,
1306 	    sizeof(a->lip_systemid))) {
1307 		return (0);
1308 	}
1309 
1310 	if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
1311 		return (0);
1312 	}
1313 
1314 	return (1);
1315 }
1316 
1317 static void
1318 lacp_port_enable(struct lacp_port *lp)
1319 {
1320 	lp->lp_state |= LACP_STATE_AGGREGATION;
1321 }
1322 
1323 static void
1324 lacp_port_disable(struct lacp_port *lp)
1325 {
1326 	lacp_set_mux(lp, LACP_MUX_DETACHED);
1327 
1328 	lp->lp_state &= ~LACP_STATE_AGGREGATION;
1329 	lp->lp_selected = LACP_UNSELECTED;
1330 	lacp_sm_rx_record_default(lp);
1331 	lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1332 	lp->lp_state &= ~LACP_STATE_EXPIRED;
1333 }
1334 
1335 /*
1336  * lacp_select: select an aggregator.  create one if necessary.
1337  */
1338 static void
1339 lacp_select(struct lacp_port *lp)
1340 {
1341 	struct lacp_softc *lsc = lp->lp_lsc;
1342 	struct lacp_aggregator *la;
1343 	char buf[LACP_LAGIDSTR_MAX+1];
1344 
1345 	if (lp->lp_aggregator) {
1346 		return;
1347 	}
1348 
1349 	/* If we haven't heard from our peer, skip this step. */
1350 	if (lp->lp_state & LACP_STATE_DEFAULTED)
1351 		return;
1352 
1353 	KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1354 	    ("timer_wait_while still active"));
1355 
1356 	LACP_DPRINTF((lp, "port lagid=%s\n",
1357 	    lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1358 	    buf, sizeof(buf))));
1359 
1360 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1361 		if (lacp_aggregator_is_compatible(la, lp)) {
1362 			break;
1363 		}
1364 	}
1365 
1366 	if (la == NULL) {
1367 		la = lacp_aggregator_get(lsc, lp);
1368 		if (la == NULL) {
1369 			LACP_DPRINTF((lp, "aggregator creation failed\n"));
1370 
1371 			/*
1372 			 * will retry on the next tick.
1373 			 */
1374 
1375 			return;
1376 		}
1377 		lacp_fill_aggregator_id(la, lp);
1378 		LACP_DPRINTF((lp, "aggregator created\n"));
1379 	} else {
1380 		LACP_DPRINTF((lp, "compatible aggregator found\n"));
1381 		if (la->la_refcnt == LACP_MAX_PORTS)
1382 			return;
1383 		lacp_aggregator_addref(lsc, la);
1384 	}
1385 
1386 	LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1387 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1388 	    buf, sizeof(buf))));
1389 
1390 	lp->lp_aggregator = la;
1391 	lp->lp_selected = LACP_SELECTED;
1392 }
1393 
1394 /*
1395  * lacp_unselect: finish unselect/detach process.
1396  */
1397 
1398 static void
1399 lacp_unselect(struct lacp_port *lp)
1400 {
1401 	struct lacp_softc *lsc = lp->lp_lsc;
1402 	struct lacp_aggregator *la = lp->lp_aggregator;
1403 
1404 	KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1405 	    ("timer_wait_while still active"));
1406 
1407 	if (la == NULL) {
1408 		return;
1409 	}
1410 
1411 	lp->lp_aggregator = NULL;
1412 	lacp_aggregator_delref(lsc, la);
1413 }
1414 
1415 /* mux machine */
1416 
1417 static void
1418 lacp_sm_mux(struct lacp_port *lp)
1419 {
1420 	struct lagg_port *lgp = lp->lp_lagg;
1421 	struct lagg_softc *sc = lgp->lp_softc;
1422 	enum lacp_mux_state new_state;
1423 	boolean_t p_sync =
1424 		    (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1425 	boolean_t p_collecting =
1426 	    (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1427 	enum lacp_selected selected = lp->lp_selected;
1428 	struct lacp_aggregator *la;
1429 
1430 	if (V_lacp_debug > 1)
1431 		lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1432 		    "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1433 		    lp->lp_mux_state, selected, p_sync, p_collecting);
1434 
1435 re_eval:
1436 	la = lp->lp_aggregator;
1437 	KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1438 	    ("MUX not detached"));
1439 	new_state = lp->lp_mux_state;
1440 	switch (lp->lp_mux_state) {
1441 	case LACP_MUX_DETACHED:
1442 		if (selected != LACP_UNSELECTED) {
1443 			new_state = LACP_MUX_WAITING;
1444 		}
1445 		break;
1446 	case LACP_MUX_WAITING:
1447 		KASSERT(la->la_pending > 0 ||
1448 		    !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1449 		    ("timer_wait_while still active"));
1450 		if (selected == LACP_SELECTED && la->la_pending == 0) {
1451 			new_state = LACP_MUX_ATTACHED;
1452 		} else if (selected == LACP_UNSELECTED) {
1453 			new_state = LACP_MUX_DETACHED;
1454 		}
1455 		break;
1456 	case LACP_MUX_ATTACHED:
1457 		if (selected == LACP_SELECTED && p_sync) {
1458 			new_state = LACP_MUX_COLLECTING;
1459 		} else if (selected != LACP_SELECTED) {
1460 			new_state = LACP_MUX_DETACHED;
1461 		}
1462 		break;
1463 	case LACP_MUX_COLLECTING:
1464 		if (selected == LACP_SELECTED && p_sync && p_collecting) {
1465 			new_state = LACP_MUX_DISTRIBUTING;
1466 		} else if (selected != LACP_SELECTED || !p_sync) {
1467 			new_state = LACP_MUX_ATTACHED;
1468 		}
1469 		break;
1470 	case LACP_MUX_DISTRIBUTING:
1471 		if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1472 			new_state = LACP_MUX_COLLECTING;
1473 			lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1474 			sc->sc_flapping++;
1475 		}
1476 		break;
1477 	default:
1478 		panic("%s: unknown state", __func__);
1479 	}
1480 
1481 	if (lp->lp_mux_state == new_state) {
1482 		return;
1483 	}
1484 
1485 	lacp_set_mux(lp, new_state);
1486 	goto re_eval;
1487 }
1488 
1489 static void
1490 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1491 {
1492 	struct lacp_aggregator *la = lp->lp_aggregator;
1493 
1494 	if (lp->lp_mux_state == new_state) {
1495 		return;
1496 	}
1497 
1498 	switch (new_state) {
1499 	case LACP_MUX_DETACHED:
1500 		lp->lp_state &= ~LACP_STATE_SYNC;
1501 		lacp_disable_distributing(lp);
1502 		lacp_disable_collecting(lp);
1503 		lacp_sm_assert_ntt(lp);
1504 		/* cancel timer */
1505 		if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1506 			KASSERT(la->la_pending > 0,
1507 			    ("timer_wait_while not active"));
1508 			la->la_pending--;
1509 		}
1510 		LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1511 		lacp_unselect(lp);
1512 		break;
1513 	case LACP_MUX_WAITING:
1514 		LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1515 		    LACP_AGGREGATE_WAIT_TIME);
1516 		la->la_pending++;
1517 		break;
1518 	case LACP_MUX_ATTACHED:
1519 		lp->lp_state |= LACP_STATE_SYNC;
1520 		lacp_disable_collecting(lp);
1521 		lacp_sm_assert_ntt(lp);
1522 		break;
1523 	case LACP_MUX_COLLECTING:
1524 		lacp_enable_collecting(lp);
1525 		lacp_disable_distributing(lp);
1526 		lacp_sm_assert_ntt(lp);
1527 		break;
1528 	case LACP_MUX_DISTRIBUTING:
1529 		lacp_enable_distributing(lp);
1530 		break;
1531 	default:
1532 		panic("%s: unknown state", __func__);
1533 	}
1534 
1535 	LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1536 
1537 	lp->lp_mux_state = new_state;
1538 }
1539 
1540 static void
1541 lacp_sm_mux_timer(struct lacp_port *lp)
1542 {
1543 	struct lacp_aggregator *la = lp->lp_aggregator;
1544 	char buf[LACP_LAGIDSTR_MAX+1];
1545 
1546 	KASSERT(la->la_pending > 0, ("no pending event"));
1547 
1548 	LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1549 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1550 	    buf, sizeof(buf)),
1551 	    la->la_pending, la->la_pending - 1));
1552 
1553 	la->la_pending--;
1554 }
1555 
1556 /* periodic transmit machine */
1557 
1558 static void
1559 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1560 {
1561 	if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1562 	    LACP_STATE_TIMEOUT)) {
1563 		return;
1564 	}
1565 
1566 	LACP_DPRINTF((lp, "partner timeout changed\n"));
1567 
1568 	/*
1569 	 * FAST_PERIODIC -> SLOW_PERIODIC
1570 	 * or
1571 	 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1572 	 *
1573 	 * let lacp_sm_ptx_tx_schedule to update timeout.
1574 	 */
1575 
1576 	LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1577 
1578 	/*
1579 	 * if timeout has been shortened, assert NTT.
1580 	 */
1581 
1582 	if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1583 		lacp_sm_assert_ntt(lp);
1584 	}
1585 }
1586 
1587 static void
1588 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1589 {
1590 	int timeout;
1591 
1592 	if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1593 	    !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1594 
1595 		/*
1596 		 * NO_PERIODIC
1597 		 */
1598 
1599 		LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1600 		return;
1601 	}
1602 
1603 	if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1604 		return;
1605 	}
1606 
1607 	timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1608 	    LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1609 
1610 	LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1611 }
1612 
1613 static void
1614 lacp_sm_ptx_timer(struct lacp_port *lp)
1615 {
1616 	lacp_sm_assert_ntt(lp);
1617 }
1618 
1619 static void
1620 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1621 {
1622 	int timeout;
1623 
1624 	/*
1625 	 * check LACP_DISABLED first
1626 	 */
1627 
1628 	if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1629 		return;
1630 	}
1631 
1632 	/*
1633 	 * check loopback condition.
1634 	 */
1635 
1636 	if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1637 	    &lp->lp_actor.lip_systemid)) {
1638 		return;
1639 	}
1640 
1641 	/*
1642 	 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1643 	 */
1644 
1645 	lacp_sm_rx_update_selected(lp, du);
1646 	lacp_sm_rx_update_ntt(lp, du);
1647 	lacp_sm_rx_record_pdu(lp, du);
1648 
1649 	timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1650 	    LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1651 	LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1652 
1653 	lp->lp_state &= ~LACP_STATE_EXPIRED;
1654 
1655 	/*
1656 	 * kick transmit machine without waiting the next tick.
1657 	 */
1658 
1659 	lacp_sm_tx(lp);
1660 }
1661 
1662 static void
1663 lacp_sm_rx_set_expired(struct lacp_port *lp)
1664 {
1665 	lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1666 	lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1667 	LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1668 	lp->lp_state |= LACP_STATE_EXPIRED;
1669 }
1670 
1671 static void
1672 lacp_sm_rx_timer(struct lacp_port *lp)
1673 {
1674 	if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1675 		/* CURRENT -> EXPIRED */
1676 		LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1677 		lacp_sm_rx_set_expired(lp);
1678 	} else {
1679 		/* EXPIRED -> DEFAULTED */
1680 		LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1681 		lacp_sm_rx_update_default_selected(lp);
1682 		lacp_sm_rx_record_default(lp);
1683 		lp->lp_state &= ~LACP_STATE_EXPIRED;
1684 	}
1685 }
1686 
1687 static void
1688 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1689 {
1690 	boolean_t active;
1691 	uint8_t oldpstate;
1692 	char buf[LACP_STATESTR_MAX+1];
1693 
1694 	LACP_TRACE(lp);
1695 
1696 	oldpstate = lp->lp_partner.lip_state;
1697 
1698 	active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1699 	    || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1700 	    (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1701 
1702 	lp->lp_partner = du->ldu_actor;
1703 	if (active &&
1704 	    ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1705 	    LACP_STATE_AGGREGATION) &&
1706 	    !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1707 	    || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1708 		/*
1709 		 * XXX Maintain legacy behavior of leaving the
1710 		 * LACP_STATE_SYNC bit unchanged from the partner's
1711 		 * advertisement if lsc_strict_mode is false.
1712 		 * TODO: We should re-examine the concept of the "strict mode"
1713 		 * to ensure it makes sense to maintain a non-strict mode.
1714 		 */
1715 		if (lp->lp_lsc->lsc_strict_mode)
1716 			lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1717 	} else {
1718 		lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1719 	}
1720 
1721 	lp->lp_state &= ~LACP_STATE_DEFAULTED;
1722 
1723 	if (oldpstate != lp->lp_partner.lip_state) {
1724 		LACP_DPRINTF((lp, "old pstate %s\n",
1725 		    lacp_format_state(oldpstate, buf, sizeof(buf))));
1726 		LACP_DPRINTF((lp, "new pstate %s\n",
1727 		    lacp_format_state(lp->lp_partner.lip_state, buf,
1728 		    sizeof(buf))));
1729 	}
1730 
1731 	lacp_sm_ptx_update_timeout(lp, oldpstate);
1732 }
1733 
1734 static void
1735 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1736 {
1737 
1738 	LACP_TRACE(lp);
1739 
1740 	if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1741 	    !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1742 	    LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1743 		LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1744 		lacp_sm_assert_ntt(lp);
1745 	}
1746 }
1747 
1748 static void
1749 lacp_sm_rx_record_default(struct lacp_port *lp)
1750 {
1751 	uint8_t oldpstate;
1752 
1753 	LACP_TRACE(lp);
1754 
1755 	oldpstate = lp->lp_partner.lip_state;
1756 	if (lp->lp_lsc->lsc_strict_mode)
1757 		lp->lp_partner = lacp_partner_admin_strict;
1758 	else
1759 		lp->lp_partner = lacp_partner_admin_optimistic;
1760 	lp->lp_state |= LACP_STATE_DEFAULTED;
1761 	lacp_sm_ptx_update_timeout(lp, oldpstate);
1762 }
1763 
1764 static void
1765 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1766     const struct lacp_peerinfo *info)
1767 {
1768 
1769 	LACP_TRACE(lp);
1770 
1771 	if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1772 	    !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1773 	    LACP_STATE_AGGREGATION)) {
1774 		lp->lp_selected = LACP_UNSELECTED;
1775 		/* mux machine will clean up lp->lp_aggregator */
1776 	}
1777 }
1778 
1779 static void
1780 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1781 {
1782 
1783 	LACP_TRACE(lp);
1784 
1785 	lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1786 }
1787 
1788 static void
1789 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1790 {
1791 
1792 	LACP_TRACE(lp);
1793 
1794 	if (lp->lp_lsc->lsc_strict_mode)
1795 		lacp_sm_rx_update_selected_from_peerinfo(lp,
1796 		    &lacp_partner_admin_strict);
1797 	else
1798 		lacp_sm_rx_update_selected_from_peerinfo(lp,
1799 		    &lacp_partner_admin_optimistic);
1800 }
1801 
1802 /* transmit machine */
1803 
1804 static void
1805 lacp_sm_tx(struct lacp_port *lp)
1806 {
1807 	int error = 0;
1808 
1809 	if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1810 #if 1
1811 	    || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1812 	    && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1813 #endif
1814 	    ) {
1815 		lp->lp_flags &= ~LACP_PORT_NTT;
1816 	}
1817 
1818 	if (!(lp->lp_flags & LACP_PORT_NTT)) {
1819 		return;
1820 	}
1821 
1822 	/* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1823 	if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1824 		    (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1825 		LACP_DPRINTF((lp, "rate limited pdu\n"));
1826 		return;
1827 	}
1828 
1829 	if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1830 		error = lacp_xmit_lacpdu(lp);
1831 	} else {
1832 		LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1833 	}
1834 
1835 	if (error == 0) {
1836 		lp->lp_flags &= ~LACP_PORT_NTT;
1837 	} else {
1838 		LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1839 		    error));
1840 	}
1841 }
1842 
1843 static void
1844 lacp_sm_assert_ntt(struct lacp_port *lp)
1845 {
1846 
1847 	lp->lp_flags |= LACP_PORT_NTT;
1848 }
1849 
1850 static void
1851 lacp_run_timers(struct lacp_port *lp)
1852 {
1853 	int i;
1854 
1855 	for (i = 0; i < LACP_NTIMER; i++) {
1856 		KASSERT(lp->lp_timer[i] >= 0,
1857 		    ("invalid timer value %d", lp->lp_timer[i]));
1858 		if (lp->lp_timer[i] == 0) {
1859 			continue;
1860 		} else if (--lp->lp_timer[i] <= 0) {
1861 			if (lacp_timer_funcs[i]) {
1862 				(*lacp_timer_funcs[i])(lp);
1863 			}
1864 		}
1865 	}
1866 }
1867 
1868 int
1869 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1870 {
1871 	struct lacp_softc *lsc = lp->lp_lsc;
1872 	struct lagg_port *lgp = lp->lp_lagg;
1873 	struct lacp_port *lp2;
1874 	struct markerdu *mdu;
1875 	int error = 0;
1876 	int pending = 0;
1877 
1878 	if (m->m_pkthdr.len != sizeof(*mdu)) {
1879 		goto bad;
1880 	}
1881 
1882 	if ((m->m_flags & M_MCAST) == 0) {
1883 		goto bad;
1884 	}
1885 
1886 	if (m->m_len < sizeof(*mdu)) {
1887 		m = m_pullup(m, sizeof(*mdu));
1888 		if (m == NULL) {
1889 			return (ENOMEM);
1890 		}
1891 	}
1892 
1893 	mdu = mtod(m, struct markerdu *);
1894 
1895 	if (memcmp(&mdu->mdu_eh.ether_dhost,
1896 	    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1897 		goto bad;
1898 	}
1899 
1900 	if (mdu->mdu_sph.sph_version != 1) {
1901 		goto bad;
1902 	}
1903 
1904 	switch (mdu->mdu_tlv.tlv_type) {
1905 	case MARKER_TYPE_INFO:
1906 		if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1907 		    marker_info_tlv_template, TRUE)) {
1908 			goto bad;
1909 		}
1910 		mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
1911 		memcpy(&mdu->mdu_eh.ether_dhost,
1912 		    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
1913 		memcpy(&mdu->mdu_eh.ether_shost,
1914 		    lgp->lp_lladdr, ETHER_ADDR_LEN);
1915 		error = lagg_enqueue(lp->lp_ifp, m);
1916 		break;
1917 
1918 	case MARKER_TYPE_RESPONSE:
1919 		if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
1920 		    marker_response_tlv_template, TRUE)) {
1921 			goto bad;
1922 		}
1923 		LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
1924 		    ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
1925 		    ":", ntohl(mdu->mdu_info.mi_rq_xid)));
1926 
1927 		/* Verify that it is the last marker we sent out */
1928 		if (memcmp(&mdu->mdu_info, &lp->lp_marker,
1929 		    sizeof(struct lacp_markerinfo)))
1930 			goto bad;
1931 
1932 		LACP_LOCK(lsc);
1933 		lp->lp_flags &= ~LACP_PORT_MARK;
1934 
1935 		if (lsc->lsc_suppress_distributing) {
1936 			/* Check if any ports are waiting for a response */
1937 			LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
1938 				if (lp2->lp_flags & LACP_PORT_MARK) {
1939 					pending = 1;
1940 					break;
1941 				}
1942 			}
1943 
1944 			if (pending == 0) {
1945 				/* All interface queues are clear */
1946 				LACP_DPRINTF((NULL, "queue flush complete\n"));
1947 				lsc->lsc_suppress_distributing = FALSE;
1948 			}
1949 		}
1950 		LACP_UNLOCK(lsc);
1951 		m_freem(m);
1952 		break;
1953 
1954 	default:
1955 		goto bad;
1956 	}
1957 
1958 	return (error);
1959 
1960 bad:
1961 	LACP_DPRINTF((lp, "bad marker frame\n"));
1962 	m_freem(m);
1963 	return (EINVAL);
1964 }
1965 
1966 static int
1967 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
1968     const struct tlv_template *tmpl, boolean_t check_type)
1969 {
1970 	while (/* CONSTCOND */ 1) {
1971 		if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
1972 			return (EINVAL);
1973 		}
1974 		if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
1975 		    tlv->tlv_length != tmpl->tmpl_length) {
1976 			return (EINVAL);
1977 		}
1978 		if (tmpl->tmpl_type == 0) {
1979 			break;
1980 		}
1981 		tlv = (const struct tlvhdr *)
1982 		    ((const char *)tlv + tlv->tlv_length);
1983 		tmpl++;
1984 	}
1985 
1986 	return (0);
1987 }
1988 
1989 /* Debugging */
1990 const char *
1991 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
1992 {
1993 	snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
1994 	    (int)mac[0],
1995 	    (int)mac[1],
1996 	    (int)mac[2],
1997 	    (int)mac[3],
1998 	    (int)mac[4],
1999 	    (int)mac[5]);
2000 
2001 	return (buf);
2002 }
2003 
2004 const char *
2005 lacp_format_systemid(const struct lacp_systemid *sysid,
2006     char *buf, size_t buflen)
2007 {
2008 	char macbuf[LACP_MACSTR_MAX+1];
2009 
2010 	snprintf(buf, buflen, "%04X,%s",
2011 	    ntohs(sysid->lsi_prio),
2012 	    lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2013 
2014 	return (buf);
2015 }
2016 
2017 const char *
2018 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2019 {
2020 	snprintf(buf, buflen, "%04X,%04X",
2021 	    ntohs(portid->lpi_prio),
2022 	    ntohs(portid->lpi_portno));
2023 
2024 	return (buf);
2025 }
2026 
2027 const char *
2028 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2029 {
2030 	char sysid[LACP_SYSTEMIDSTR_MAX+1];
2031 	char portid[LACP_PORTIDSTR_MAX+1];
2032 
2033 	snprintf(buf, buflen, "(%s,%04X,%s)",
2034 	    lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2035 	    ntohs(peer->lip_key),
2036 	    lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2037 
2038 	return (buf);
2039 }
2040 
2041 const char *
2042 lacp_format_lagid(const struct lacp_peerinfo *a,
2043     const struct lacp_peerinfo *b, char *buf, size_t buflen)
2044 {
2045 	char astr[LACP_PARTNERSTR_MAX+1];
2046 	char bstr[LACP_PARTNERSTR_MAX+1];
2047 
2048 #if 0
2049 	/*
2050 	 * there's a convention to display small numbered peer
2051 	 * in the left.
2052 	 */
2053 
2054 	if (lacp_compare_peerinfo(a, b) > 0) {
2055 		const struct lacp_peerinfo *t;
2056 
2057 		t = a;
2058 		a = b;
2059 		b = t;
2060 	}
2061 #endif
2062 
2063 	snprintf(buf, buflen, "[%s,%s]",
2064 	    lacp_format_partner(a, astr, sizeof(astr)),
2065 	    lacp_format_partner(b, bstr, sizeof(bstr)));
2066 
2067 	return (buf);
2068 }
2069 
2070 const char *
2071 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2072     char *buf, size_t buflen)
2073 {
2074 	if (la == NULL) {
2075 		return ("(none)");
2076 	}
2077 
2078 	return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2079 }
2080 
2081 const char *
2082 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2083 {
2084 	snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2085 	return (buf);
2086 }
2087 
2088 static void
2089 lacp_dump_lacpdu(const struct lacpdu *du)
2090 {
2091 	char buf[LACP_PARTNERSTR_MAX+1];
2092 	char buf2[LACP_STATESTR_MAX+1];
2093 
2094 	printf("actor=%s\n",
2095 	    lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2096 	printf("actor.state=%s\n",
2097 	    lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2098 	printf("partner=%s\n",
2099 	    lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2100 	printf("partner.state=%s\n",
2101 	    lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2102 
2103 	printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2104 }
2105 
2106 static void
2107 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2108 {
2109 	va_list va;
2110 
2111 	if (lp) {
2112 		printf("%s: ", lp->lp_ifp->if_xname);
2113 	}
2114 
2115 	va_start(va, fmt);
2116 	vprintf(fmt, va);
2117 	va_end(va);
2118 }
2119