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