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