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