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