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