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