xref: /freebsd/sys/net/ieee8023ad_lacp.c (revision 685dc743)
1b47888ceSAndrew Thompson /*	$NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $	*/
2b47888ceSAndrew Thompson 
3b47888ceSAndrew Thompson /*-
4b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5fe267a55SPedro F. Giffuni  *
6b47888ceSAndrew Thompson  * Copyright (c)2005 YAMAMOTO Takashi,
73de18008SAndrew Thompson  * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
8b47888ceSAndrew Thompson  * All rights reserved.
9b47888ceSAndrew Thompson  *
10b47888ceSAndrew Thompson  * Redistribution and use in source and binary forms, with or without
11b47888ceSAndrew Thompson  * modification, are permitted provided that the following conditions
12b47888ceSAndrew Thompson  * are met:
13b47888ceSAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
14b47888ceSAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
15b47888ceSAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
16b47888ceSAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
17b47888ceSAndrew Thompson  *    documentation and/or other materials provided with the distribution.
18b47888ceSAndrew Thompson  *
19b47888ceSAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20b47888ceSAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21b47888ceSAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22b47888ceSAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23b47888ceSAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24b47888ceSAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25b47888ceSAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26b47888ceSAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27b47888ceSAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28b47888ceSAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29b47888ceSAndrew Thompson  * SUCH DAMAGE.
30b47888ceSAndrew Thompson  */
31b47888ceSAndrew Thompson 
32b47888ceSAndrew Thompson #include <sys/cdefs.h>
33b2e60773SJohn Baldwin #include "opt_kern_tls.h"
34f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
35f3e7afe2SHans Petter Selasky 
36b47888ceSAndrew Thompson #include <sys/param.h>
37b47888ceSAndrew Thompson #include <sys/callout.h>
38c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
39b47888ceSAndrew Thompson #include <sys/mbuf.h>
40b47888ceSAndrew Thompson #include <sys/systm.h>
41b47888ceSAndrew Thompson #include <sys/malloc.h>
42b47888ceSAndrew Thompson #include <sys/kernel.h> /* hz */
43b47888ceSAndrew Thompson #include <sys/socket.h> /* for net/if.h */
44b47888ceSAndrew Thompson #include <sys/sockio.h>
455fc4c149SAndrew Thompson #include <sys/sysctl.h>
46b47888ceSAndrew Thompson #include <machine/stdarg.h>
473bf517e3SAndrew Thompson #include <sys/lock.h>
483bf517e3SAndrew Thompson #include <sys/rwlock.h>
4976039bc8SGleb Smirnoff #include <sys/taskqueue.h>
5000a80538SGreg Foster #include <sys/time.h>
51b47888ceSAndrew Thompson 
52b47888ceSAndrew Thompson #include <net/if.h>
5376039bc8SGleb Smirnoff #include <net/if_var.h>
542c2b37adSJustin Hibbits #include <net/if_private.h>
55b47888ceSAndrew Thompson #include <net/if_dl.h>
56b47888ceSAndrew Thompson #include <net/ethernet.h>
57a92c4bb6SHans Petter Selasky #include <net/infiniband.h>
58b47888ceSAndrew Thompson #include <net/if_media.h>
59b47888ceSAndrew Thompson #include <net/if_types.h>
60b47888ceSAndrew Thompson 
6118242d3bSAndrew Thompson #include <net/if_lagg.h>
62b47888ceSAndrew Thompson #include <net/ieee8023ad_lacp.h>
63b47888ceSAndrew Thompson 
64b47888ceSAndrew Thompson /*
65b47888ceSAndrew Thompson  * actor system priority and port priority.
66b47888ceSAndrew Thompson  * XXX should be configurable.
67b47888ceSAndrew Thompson  */
68b47888ceSAndrew Thompson 
69b47888ceSAndrew Thompson #define	LACP_SYSTEM_PRIO	0x8000
70b47888ceSAndrew Thompson #define	LACP_PORT_PRIO		0x8000
71b47888ceSAndrew Thompson 
72b47888ceSAndrew Thompson const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
73b47888ceSAndrew Thompson     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
74b47888ceSAndrew Thompson 
75b47888ceSAndrew Thompson static const struct tlv_template lacp_info_tlv_template[] = {
76b47888ceSAndrew Thompson 	{ LACP_TYPE_ACTORINFO,
77b47888ceSAndrew Thompson 	    sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
78b47888ceSAndrew Thompson 	{ LACP_TYPE_PARTNERINFO,
79b47888ceSAndrew Thompson 	    sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
80b47888ceSAndrew Thompson 	{ LACP_TYPE_COLLECTORINFO,
81b47888ceSAndrew Thompson 	    sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
82b47888ceSAndrew Thompson 	{ 0, 0 },
83b47888ceSAndrew Thompson };
84b47888ceSAndrew Thompson 
85b47888ceSAndrew Thompson static const struct tlv_template marker_info_tlv_template[] = {
86998971a7SAndrew Thompson 	{ MARKER_TYPE_INFO,
87998971a7SAndrew Thompson 	    sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
88b47888ceSAndrew Thompson 	{ 0, 0 },
89b47888ceSAndrew Thompson };
90b47888ceSAndrew Thompson 
91b47888ceSAndrew Thompson static const struct tlv_template marker_response_tlv_template[] = {
92998971a7SAndrew Thompson 	{ MARKER_TYPE_RESPONSE,
93998971a7SAndrew Thompson 	    sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
94b47888ceSAndrew Thompson 	{ 0, 0 },
95b47888ceSAndrew Thompson };
96b47888ceSAndrew Thompson 
973de18008SAndrew Thompson typedef void (*lacp_timer_func_t)(struct lacp_port *);
983de18008SAndrew Thompson 
99b47888ceSAndrew Thompson static void	lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
100998971a7SAndrew Thompson static void	lacp_fill_markerinfo(struct lacp_port *,
101998971a7SAndrew Thompson 		    struct lacp_markerinfo *);
102b47888ceSAndrew Thompson 
103b47888ceSAndrew Thompson static uint64_t	lacp_aggregator_bandwidth(struct lacp_aggregator *);
104b47888ceSAndrew Thompson static void	lacp_suppress_distributing(struct lacp_softc *,
105b47888ceSAndrew Thompson 		    struct lacp_aggregator *);
106b47888ceSAndrew Thompson static void	lacp_transit_expire(void *);
1073de18008SAndrew Thompson static void	lacp_update_portmap(struct lacp_softc *);
108b47888ceSAndrew Thompson static void	lacp_select_active_aggregator(struct lacp_softc *);
109b47888ceSAndrew Thompson static uint16_t	lacp_compose_key(struct lacp_port *);
110b47888ceSAndrew Thompson static int	tlv_check(const void *, size_t, const struct tlvhdr *,
111b47888ceSAndrew Thompson 		    const struct tlv_template *, boolean_t);
112b47888ceSAndrew Thompson static void	lacp_tick(void *);
113b47888ceSAndrew Thompson 
114b47888ceSAndrew Thompson static void	lacp_fill_aggregator_id(struct lacp_aggregator *,
115b47888ceSAndrew Thompson 		    const struct lacp_port *);
116b47888ceSAndrew Thompson static void	lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
117b47888ceSAndrew Thompson 		    const struct lacp_peerinfo *);
1185a8abd0aSZhenlei Huang static bool	lacp_aggregator_is_compatible(const struct lacp_aggregator *,
119b47888ceSAndrew Thompson 		    const struct lacp_port *);
1205a8abd0aSZhenlei Huang static bool	lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
121b47888ceSAndrew Thompson 		    const struct lacp_peerinfo *);
122b47888ceSAndrew Thompson 
123b47888ceSAndrew Thompson static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
124b47888ceSAndrew Thompson 		    struct lacp_port *);
125b47888ceSAndrew Thompson static void	lacp_aggregator_addref(struct lacp_softc *,
126b47888ceSAndrew Thompson 		    struct lacp_aggregator *);
127b47888ceSAndrew Thompson static void	lacp_aggregator_delref(struct lacp_softc *,
128b47888ceSAndrew Thompson 		    struct lacp_aggregator *);
129b47888ceSAndrew Thompson 
130b47888ceSAndrew Thompson /* receive machine */
131b47888ceSAndrew Thompson 
1323de18008SAndrew Thompson static int	lacp_pdu_input(struct lacp_port *, struct mbuf *);
1333de18008SAndrew Thompson static int	lacp_marker_input(struct lacp_port *, struct mbuf *);
134b47888ceSAndrew Thompson static void	lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
135b47888ceSAndrew Thompson static void	lacp_sm_rx_timer(struct lacp_port *);
136b47888ceSAndrew Thompson static void	lacp_sm_rx_set_expired(struct lacp_port *);
137b47888ceSAndrew Thompson static void	lacp_sm_rx_update_ntt(struct lacp_port *,
138b47888ceSAndrew Thompson 		    const struct lacpdu *);
139b47888ceSAndrew Thompson static void	lacp_sm_rx_record_pdu(struct lacp_port *,
140b47888ceSAndrew Thompson 		    const struct lacpdu *);
141b47888ceSAndrew Thompson static void	lacp_sm_rx_update_selected(struct lacp_port *,
142b47888ceSAndrew Thompson 		    const struct lacpdu *);
143b47888ceSAndrew Thompson static void	lacp_sm_rx_record_default(struct lacp_port *);
144b47888ceSAndrew Thompson static void	lacp_sm_rx_update_default_selected(struct lacp_port *);
145b47888ceSAndrew Thompson static void	lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
146b47888ceSAndrew Thompson 		    const struct lacp_peerinfo *);
147b47888ceSAndrew Thompson 
148b47888ceSAndrew Thompson /* mux machine */
149b47888ceSAndrew Thompson 
150b47888ceSAndrew Thompson static void	lacp_sm_mux(struct lacp_port *);
151b47888ceSAndrew Thompson static void	lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
152b47888ceSAndrew Thompson static void	lacp_sm_mux_timer(struct lacp_port *);
153b47888ceSAndrew Thompson 
154b47888ceSAndrew Thompson /* periodic transmit machine */
155b47888ceSAndrew Thompson 
156b47888ceSAndrew Thompson static void	lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
157b47888ceSAndrew Thompson static void	lacp_sm_ptx_tx_schedule(struct lacp_port *);
158b47888ceSAndrew Thompson static void	lacp_sm_ptx_timer(struct lacp_port *);
159b47888ceSAndrew Thompson 
160b47888ceSAndrew Thompson /* transmit machine */
161b47888ceSAndrew Thompson 
162b47888ceSAndrew Thompson static void	lacp_sm_tx(struct lacp_port *);
163b47888ceSAndrew Thompson static void	lacp_sm_assert_ntt(struct lacp_port *);
164b47888ceSAndrew Thompson 
165b47888ceSAndrew Thompson static void	lacp_run_timers(struct lacp_port *);
166b47888ceSAndrew Thompson static int	lacp_compare_peerinfo(const struct lacp_peerinfo *,
167b47888ceSAndrew Thompson 		    const struct lacp_peerinfo *);
168b47888ceSAndrew Thompson static int	lacp_compare_systemid(const struct lacp_systemid *,
169b47888ceSAndrew Thompson 		    const struct lacp_systemid *);
170b47888ceSAndrew Thompson static void	lacp_port_enable(struct lacp_port *);
171b47888ceSAndrew Thompson static void	lacp_port_disable(struct lacp_port *);
172b47888ceSAndrew Thompson static void	lacp_select(struct lacp_port *);
173b47888ceSAndrew Thompson static void	lacp_unselect(struct lacp_port *);
174b47888ceSAndrew Thompson static void	lacp_disable_collecting(struct lacp_port *);
175b47888ceSAndrew Thompson static void	lacp_enable_collecting(struct lacp_port *);
176b47888ceSAndrew Thompson static void	lacp_disable_distributing(struct lacp_port *);
177b47888ceSAndrew Thompson static void	lacp_enable_distributing(struct lacp_port *);
178b47888ceSAndrew Thompson static int	lacp_xmit_lacpdu(struct lacp_port *);
179998971a7SAndrew Thompson static int	lacp_xmit_marker(struct lacp_port *);
180b47888ceSAndrew Thompson 
1815fc4c149SAndrew Thompson /* Debugging */
1825fc4c149SAndrew Thompson 
183b47888ceSAndrew Thompson static void	lacp_dump_lacpdu(const struct lacpdu *);
184b47888ceSAndrew Thompson static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
185b47888ceSAndrew Thompson 		    size_t);
186b47888ceSAndrew Thompson static const char *lacp_format_lagid(const struct lacp_peerinfo *,
187b47888ceSAndrew Thompson 		    const struct lacp_peerinfo *, char *, size_t);
188b47888ceSAndrew Thompson static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
189b47888ceSAndrew Thompson 		    char *, size_t);
190b47888ceSAndrew Thompson static const char *lacp_format_state(uint8_t, char *, size_t);
191b47888ceSAndrew Thompson static const char *lacp_format_mac(const uint8_t *, char *, size_t);
192b47888ceSAndrew Thompson static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
193b47888ceSAndrew Thompson 		    size_t);
194b47888ceSAndrew Thompson static const char *lacp_format_portid(const struct lacp_portid *, char *,
195b47888ceSAndrew Thompson 		    size_t);
196b47888ceSAndrew Thompson static void	lacp_dprintf(const struct lacp_port *, const char *, ...)
197b47888ceSAndrew Thompson 		    __attribute__((__format__(__printf__, 2, 3)));
1985fc4c149SAndrew Thompson 
1995f901c92SAndrew Turner VNET_DEFINE_STATIC(int, lacp_debug);
200939a050aSHiroki Sato #define	V_lacp_debug	VNET(lacp_debug)
2017029da5cSPawel Biernacki SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
2027029da5cSPawel Biernacki     "ieee802.3ad");
203939a050aSHiroki Sato SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
204939a050aSHiroki Sato     &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
2055fc4c149SAndrew Thompson 
2065f901c92SAndrew Turner VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
2076fb1399aSSteven Hartland SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
2086fb1399aSSteven Hartland     CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
209c1be893cSSteven Hartland     "LACP strict protocol compliance default");
210939a050aSHiroki Sato #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
211939a050aSHiroki Sato #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
212939a050aSHiroki Sato #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
213b47888ceSAndrew Thompson 
214b47888ceSAndrew Thompson /*
215b47888ceSAndrew Thompson  * partner administration variables.
216b47888ceSAndrew Thompson  * XXX should be configurable.
217b47888ceSAndrew Thompson  */
218b47888ceSAndrew Thompson 
21931402c27SAdrian Chadd static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
220b47888ceSAndrew Thompson 	.lip_systemid = { .lsi_prio = 0xffff },
221b47888ceSAndrew Thompson 	.lip_portid = { .lpi_prio = 0xffff },
222b47888ceSAndrew Thompson 	.lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
223b47888ceSAndrew Thompson 	    LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
22431402c27SAdrian Chadd };
22531402c27SAdrian Chadd 
22631402c27SAdrian Chadd static const struct lacp_peerinfo lacp_partner_admin_strict = {
22731402c27SAdrian Chadd 	.lip_systemid = { .lsi_prio = 0xffff },
22831402c27SAdrian Chadd 	.lip_portid = { .lpi_prio = 0xffff },
229b47888ceSAndrew Thompson 	.lip_state = 0,
230b47888ceSAndrew Thompson };
231b47888ceSAndrew Thompson 
232b47888ceSAndrew Thompson static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
233b47888ceSAndrew Thompson 	[LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
234b47888ceSAndrew Thompson 	[LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
235b47888ceSAndrew Thompson 	[LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
236b47888ceSAndrew Thompson };
237b47888ceSAndrew Thompson 
238af0084c9SAndrew Thompson struct mbuf *
lacp_input(struct lagg_port * lgp,struct mbuf * m)23918242d3bSAndrew Thompson lacp_input(struct lagg_port *lgp, struct mbuf *m)
240b47888ceSAndrew Thompson {
2413de18008SAndrew Thompson 	struct lacp_port *lp = LACP_PORT(lgp);
2423bf517e3SAndrew Thompson 	uint8_t subtype;
2433bf517e3SAndrew Thompson 
2443bf517e3SAndrew Thompson 	if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
2453bf517e3SAndrew Thompson 		m_freem(m);
246af0084c9SAndrew Thompson 		return (NULL);
2473bf517e3SAndrew Thompson 	}
2483bf517e3SAndrew Thompson 
2493bf517e3SAndrew Thompson 	m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
2503bf517e3SAndrew Thompson 	switch (subtype) {
2513bf517e3SAndrew Thompson 		case SLOWPROTOCOLS_SUBTYPE_LACP:
2523de18008SAndrew Thompson 			lacp_pdu_input(lp, m);
2533de18008SAndrew Thompson 			return (NULL);
2543bf517e3SAndrew Thompson 
2553bf517e3SAndrew Thompson 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
2563de18008SAndrew Thompson 			lacp_marker_input(lp, m);
257af0084c9SAndrew Thompson 			return (NULL);
2583bf517e3SAndrew Thompson 	}
2593bf517e3SAndrew Thompson 
2603de18008SAndrew Thompson 	/* Not a subtype we are interested in */
2613de18008SAndrew Thompson 	return (m);
2623bf517e3SAndrew Thompson }
2633bf517e3SAndrew Thompson 
2643bf517e3SAndrew Thompson /*
2653bf517e3SAndrew Thompson  * lacp_pdu_input: process lacpdu
2663bf517e3SAndrew Thompson  */
2673bf517e3SAndrew Thompson static int
lacp_pdu_input(struct lacp_port * lp,struct mbuf * m)2683de18008SAndrew Thompson lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
2693bf517e3SAndrew Thompson {
2703de18008SAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
271b47888ceSAndrew Thompson 	struct lacpdu *du;
272b47888ceSAndrew Thompson 	int error = 0;
273b47888ceSAndrew Thompson 
274b47888ceSAndrew Thompson 	if (m->m_pkthdr.len != sizeof(*du)) {
275b47888ceSAndrew Thompson 		goto bad;
276b47888ceSAndrew Thompson 	}
277b47888ceSAndrew Thompson 
278b47888ceSAndrew Thompson 	if ((m->m_flags & M_MCAST) == 0) {
279b47888ceSAndrew Thompson 		goto bad;
280b47888ceSAndrew Thompson 	}
281b47888ceSAndrew Thompson 
282b47888ceSAndrew Thompson 	if (m->m_len < sizeof(*du)) {
283b47888ceSAndrew Thompson 		m = m_pullup(m, sizeof(*du));
284b47888ceSAndrew Thompson 		if (m == NULL) {
285b47888ceSAndrew Thompson 			return (ENOMEM);
286b47888ceSAndrew Thompson 		}
287b47888ceSAndrew Thompson 	}
288b47888ceSAndrew Thompson 
289b47888ceSAndrew Thompson 	du = mtod(m, struct lacpdu *);
290b47888ceSAndrew Thompson 
291b47888ceSAndrew Thompson 	if (memcmp(&du->ldu_eh.ether_dhost,
292b47888ceSAndrew Thompson 	    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
293b47888ceSAndrew Thompson 		goto bad;
294b47888ceSAndrew Thompson 	}
295b47888ceSAndrew Thompson 
296b47888ceSAndrew Thompson 	/*
297b47888ceSAndrew Thompson 	 * ignore the version for compatibility with
298b47888ceSAndrew Thompson 	 * the future protocol revisions.
299b47888ceSAndrew Thompson 	 */
300b47888ceSAndrew Thompson #if 0
301b47888ceSAndrew Thompson 	if (du->ldu_sph.sph_version != 1) {
302b47888ceSAndrew Thompson 		goto bad;
303b47888ceSAndrew Thompson 	}
304b47888ceSAndrew Thompson #endif
305b47888ceSAndrew Thompson 
306b47888ceSAndrew Thompson 	/*
307b47888ceSAndrew Thompson 	 * ignore tlv types for compatibility with
308b47888ceSAndrew Thompson 	 * the future protocol revisions.
309b47888ceSAndrew Thompson 	 */
310b47888ceSAndrew Thompson 	if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
311b47888ceSAndrew Thompson 	    lacp_info_tlv_template, FALSE)) {
312b47888ceSAndrew Thompson 		goto bad;
313b47888ceSAndrew Thompson 	}
314b47888ceSAndrew Thompson 
315939a050aSHiroki Sato         if (V_lacp_debug > 0) {
3165fc4c149SAndrew Thompson 		lacp_dprintf(lp, "lacpdu receive\n");
317b47888ceSAndrew Thompson 		lacp_dump_lacpdu(du);
3185fc4c149SAndrew Thompson 	}
3193de18008SAndrew Thompson 
32049de4f22SAdrian Chadd 	if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
32131402c27SAdrian Chadd 		LACP_TPRINTF((lp, "Dropping RX PDU\n"));
32231402c27SAdrian Chadd 		goto bad;
32331402c27SAdrian Chadd 	}
32431402c27SAdrian Chadd 
3253de18008SAndrew Thompson 	LACP_LOCK(lsc);
326b47888ceSAndrew Thompson 	lacp_sm_rx(lp, du);
3273de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
328b47888ceSAndrew Thompson 
329b47888ceSAndrew Thompson 	m_freem(m);
330b47888ceSAndrew Thompson 	return (error);
331b47888ceSAndrew Thompson 
332b47888ceSAndrew Thompson bad:
333b47888ceSAndrew Thompson 	m_freem(m);
334b47888ceSAndrew Thompson 	return (EINVAL);
335b47888ceSAndrew Thompson }
336b47888ceSAndrew Thompson 
337b47888ceSAndrew Thompson static void
lacp_fill_actorinfo(struct lacp_port * lp,struct lacp_peerinfo * info)338b47888ceSAndrew Thompson lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
339b47888ceSAndrew Thompson {
34018242d3bSAndrew Thompson 	struct lagg_port *lgp = lp->lp_lagg;
341ec32b37eSAndrew Thompson 	struct lagg_softc *sc = lgp->lp_softc;
342b47888ceSAndrew Thompson 
343b47888ceSAndrew Thompson 	info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
344b47888ceSAndrew Thompson 	memcpy(&info->lip_systemid.lsi_mac,
345ec32b37eSAndrew Thompson 	    IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
346b47888ceSAndrew Thompson 	info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
347b47888ceSAndrew Thompson 	info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
348b47888ceSAndrew Thompson 	info->lip_state = lp->lp_state;
349b47888ceSAndrew Thompson }
350b47888ceSAndrew Thompson 
351998971a7SAndrew Thompson static void
lacp_fill_markerinfo(struct lacp_port * lp,struct lacp_markerinfo * info)352998971a7SAndrew Thompson lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
353998971a7SAndrew Thompson {
354998971a7SAndrew Thompson 	struct ifnet *ifp = lp->lp_ifp;
355998971a7SAndrew Thompson 
356998971a7SAndrew Thompson 	/* Fill in the port index and system id (encoded as the MAC) */
357998971a7SAndrew Thompson 	info->mi_rq_port = htons(ifp->if_index);
358998971a7SAndrew Thompson 	memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
359998971a7SAndrew Thompson 	info->mi_rq_xid = htonl(0);
360998971a7SAndrew Thompson }
361998971a7SAndrew Thompson 
362b47888ceSAndrew Thompson static int
lacp_xmit_lacpdu(struct lacp_port * lp)363b47888ceSAndrew Thompson lacp_xmit_lacpdu(struct lacp_port *lp)
364b47888ceSAndrew Thompson {
36518242d3bSAndrew Thompson 	struct lagg_port *lgp = lp->lp_lagg;
366b47888ceSAndrew Thompson 	struct mbuf *m;
367b47888ceSAndrew Thompson 	struct lacpdu *du;
368b47888ceSAndrew Thompson 	int error;
369b47888ceSAndrew Thompson 
3703de18008SAndrew Thompson 	LACP_LOCK_ASSERT(lp->lp_lsc);
371b47888ceSAndrew Thompson 
372eb1b1807SGleb Smirnoff 	m = m_gethdr(M_NOWAIT, MT_DATA);
373b47888ceSAndrew Thompson 	if (m == NULL) {
374b47888ceSAndrew Thompson 		return (ENOMEM);
375b47888ceSAndrew Thompson 	}
376b47888ceSAndrew Thompson 	m->m_len = m->m_pkthdr.len = sizeof(*du);
377b47888ceSAndrew Thompson 
378b47888ceSAndrew Thompson 	du = mtod(m, struct lacpdu *);
379b47888ceSAndrew Thompson 	memset(du, 0, sizeof(*du));
380b47888ceSAndrew Thompson 
381b47888ceSAndrew Thompson 	memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
382b47888ceSAndrew Thompson 	    ETHER_ADDR_LEN);
38318242d3bSAndrew Thompson 	memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
384b47888ceSAndrew Thompson 	du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
385b47888ceSAndrew Thompson 
386b47888ceSAndrew Thompson 	du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
387b47888ceSAndrew Thompson 	du->ldu_sph.sph_version = 1;
388b47888ceSAndrew Thompson 
389b47888ceSAndrew Thompson 	TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
390b47888ceSAndrew Thompson 	du->ldu_actor = lp->lp_actor;
391b47888ceSAndrew Thompson 
392b47888ceSAndrew Thompson 	TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
393b47888ceSAndrew Thompson 	    sizeof(du->ldu_partner));
394b47888ceSAndrew Thompson 	du->ldu_partner = lp->lp_partner;
395b47888ceSAndrew Thompson 
396b47888ceSAndrew Thompson 	TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
397b47888ceSAndrew Thompson 	    sizeof(du->ldu_collector));
398b47888ceSAndrew Thompson 	du->ldu_collector.lci_maxdelay = 0;
399b47888ceSAndrew Thompson 
400939a050aSHiroki Sato 	if (V_lacp_debug > 0) {
4015fc4c149SAndrew Thompson 		lacp_dprintf(lp, "lacpdu transmit\n");
402b47888ceSAndrew Thompson 		lacp_dump_lacpdu(du);
4035fc4c149SAndrew Thompson 	}
404b47888ceSAndrew Thompson 
405b47888ceSAndrew Thompson 	m->m_flags |= M_MCAST;
406b47888ceSAndrew Thompson 
407b47888ceSAndrew Thompson 	/*
408b47888ceSAndrew Thompson 	 * XXX should use higher priority queue.
409b47888ceSAndrew Thompson 	 * otherwise network congestion can break aggregation.
410b47888ceSAndrew Thompson 	 */
411b47888ceSAndrew Thompson 
41218242d3bSAndrew Thompson 	error = lagg_enqueue(lp->lp_ifp, m);
413b47888ceSAndrew Thompson 	return (error);
414b47888ceSAndrew Thompson }
415b47888ceSAndrew Thompson 
416998971a7SAndrew Thompson static int
lacp_xmit_marker(struct lacp_port * lp)417998971a7SAndrew Thompson lacp_xmit_marker(struct lacp_port *lp)
418998971a7SAndrew Thompson {
419998971a7SAndrew Thompson 	struct lagg_port *lgp = lp->lp_lagg;
420998971a7SAndrew Thompson 	struct mbuf *m;
421998971a7SAndrew Thompson 	struct markerdu *mdu;
422998971a7SAndrew Thompson 	int error;
423998971a7SAndrew Thompson 
4243de18008SAndrew Thompson 	LACP_LOCK_ASSERT(lp->lp_lsc);
425998971a7SAndrew Thompson 
426eb1b1807SGleb Smirnoff 	m = m_gethdr(M_NOWAIT, MT_DATA);
427998971a7SAndrew Thompson 	if (m == NULL) {
428998971a7SAndrew Thompson 		return (ENOMEM);
429998971a7SAndrew Thompson 	}
430998971a7SAndrew Thompson 	m->m_len = m->m_pkthdr.len = sizeof(*mdu);
431998971a7SAndrew Thompson 
432998971a7SAndrew Thompson 	mdu = mtod(m, struct markerdu *);
433998971a7SAndrew Thompson 	memset(mdu, 0, sizeof(*mdu));
434998971a7SAndrew Thompson 
435998971a7SAndrew Thompson 	memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
436998971a7SAndrew Thompson 	    ETHER_ADDR_LEN);
437998971a7SAndrew Thompson 	memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
438998971a7SAndrew Thompson 	mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
439998971a7SAndrew Thompson 
440998971a7SAndrew Thompson 	mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
441998971a7SAndrew Thompson 	mdu->mdu_sph.sph_version = 1;
442998971a7SAndrew Thompson 
443998971a7SAndrew Thompson 	/* Bump the transaction id and copy over the marker info */
444998971a7SAndrew Thompson 	lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
445998971a7SAndrew Thompson 	TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
446998971a7SAndrew Thompson 	mdu->mdu_info = lp->lp_marker;
447998971a7SAndrew Thompson 
448998971a7SAndrew Thompson 	LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
449998971a7SAndrew Thompson 	    ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
450998971a7SAndrew Thompson 	    ntohl(mdu->mdu_info.mi_rq_xid)));
451998971a7SAndrew Thompson 
452998971a7SAndrew Thompson 	m->m_flags |= M_MCAST;
453998971a7SAndrew Thompson 	error = lagg_enqueue(lp->lp_ifp, m);
454998971a7SAndrew Thompson 	return (error);
455998971a7SAndrew Thompson }
4563de18008SAndrew Thompson 
457b47888ceSAndrew Thompson void
lacp_linkstate(struct lagg_port * lgp)45818242d3bSAndrew Thompson lacp_linkstate(struct lagg_port *lgp)
459b47888ceSAndrew Thompson {
46018242d3bSAndrew Thompson 	struct lacp_port *lp = LACP_PORT(lgp);
4613de18008SAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
46218242d3bSAndrew Thompson 	struct ifnet *ifp = lgp->lp_ifp;
463b47888ceSAndrew Thompson 	struct ifmediareq ifmr;
464b47888ceSAndrew Thompson 	int error = 0;
465b47888ceSAndrew Thompson 	u_int media;
466b47888ceSAndrew Thompson 	uint8_t old_state;
467b47888ceSAndrew Thompson 	uint16_t old_key;
468b47888ceSAndrew Thompson 
469b47888ceSAndrew Thompson 	bzero((char *)&ifmr, sizeof(ifmr));
470aa0186bcSNavdeep Parhar 	error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
471aa0186bcSNavdeep Parhar 	if (error != 0) {
472aa0186bcSNavdeep Parhar 		bzero((char *)&ifmr, sizeof(ifmr));
473b47888ceSAndrew Thompson 		error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
474aa0186bcSNavdeep Parhar 	}
475b47888ceSAndrew Thompson 	if (error != 0)
476b47888ceSAndrew Thompson 		return;
477b47888ceSAndrew Thompson 
4783de18008SAndrew Thompson 	LACP_LOCK(lsc);
479b47888ceSAndrew Thompson 	media = ifmr.ifm_active;
480e3163ef6SAndrew Thompson 	LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
481e3163ef6SAndrew Thompson 	    "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
482e3163ef6SAndrew Thompson 	    (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
483b47888ceSAndrew Thompson 	old_state = lp->lp_state;
484b47888ceSAndrew Thompson 	old_key = lp->lp_key;
485b47888ceSAndrew Thompson 
486b47888ceSAndrew Thompson 	lp->lp_media = media;
487e3163ef6SAndrew Thompson 	/*
488e3163ef6SAndrew Thompson 	 * If the port is not an active full duplex Ethernet link then it can
489e3163ef6SAndrew Thompson 	 * not be aggregated.
490e3163ef6SAndrew Thompson 	 */
491e3163ef6SAndrew Thompson 	if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
492e3163ef6SAndrew Thompson 	    ifp->if_link_state != LINK_STATE_UP) {
493b47888ceSAndrew Thompson 		lacp_port_disable(lp);
494b47888ceSAndrew Thompson 	} else {
495b47888ceSAndrew Thompson 		lacp_port_enable(lp);
496b47888ceSAndrew Thompson 	}
497b47888ceSAndrew Thompson 	lp->lp_key = lacp_compose_key(lp);
498b47888ceSAndrew Thompson 
499b47888ceSAndrew Thompson 	if (old_state != lp->lp_state || old_key != lp->lp_key) {
500b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "-> UNSELECTED\n"));
501b47888ceSAndrew Thompson 		lp->lp_selected = LACP_UNSELECTED;
502b47888ceSAndrew Thompson 	}
5033de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
504b47888ceSAndrew Thompson }
505b47888ceSAndrew Thompson 
506b47888ceSAndrew Thompson static void
lacp_tick(void * arg)507b47888ceSAndrew Thompson lacp_tick(void *arg)
508b47888ceSAndrew Thompson {
509b47888ceSAndrew Thompson 	struct lacp_softc *lsc = arg;
510b47888ceSAndrew Thompson 	struct lacp_port *lp;
511b47888ceSAndrew Thompson 
512b47888ceSAndrew Thompson 	LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
513b47888ceSAndrew Thompson 		if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
514b47888ceSAndrew Thompson 			continue;
515b47888ceSAndrew Thompson 
516939a050aSHiroki Sato 		CURVNET_SET(lp->lp_ifp->if_vnet);
517b47888ceSAndrew Thompson 		lacp_run_timers(lp);
518b47888ceSAndrew Thompson 
519b47888ceSAndrew Thompson 		lacp_select(lp);
520b47888ceSAndrew Thompson 		lacp_sm_mux(lp);
521b47888ceSAndrew Thompson 		lacp_sm_tx(lp);
522b47888ceSAndrew Thompson 		lacp_sm_ptx_tx_schedule(lp);
523939a050aSHiroki Sato 		CURVNET_RESTORE();
524b47888ceSAndrew Thompson 	}
525b47888ceSAndrew Thompson 	callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
526b47888ceSAndrew Thompson }
527b47888ceSAndrew Thompson 
528b47888ceSAndrew Thompson int
lacp_port_create(struct lagg_port * lgp)52918242d3bSAndrew Thompson lacp_port_create(struct lagg_port *lgp)
530b47888ceSAndrew Thompson {
531ec32b37eSAndrew Thompson 	struct lagg_softc *sc = lgp->lp_softc;
532ec32b37eSAndrew Thompson 	struct lacp_softc *lsc = LACP_SOFTC(sc);
533b47888ceSAndrew Thompson 	struct lacp_port *lp;
53418242d3bSAndrew Thompson 	struct ifnet *ifp = lgp->lp_ifp;
535b47888ceSAndrew Thompson 	struct sockaddr_dl sdl;
536b47888ceSAndrew Thompson 	struct ifmultiaddr *rifma = NULL;
537b47888ceSAndrew Thompson 	int error;
538b47888ceSAndrew Thompson 
53995fbe4d0SAlexander V. Chernikov 	link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
540b47888ceSAndrew Thompson 	sdl.sdl_alen = ETHER_ADDR_LEN;
541b47888ceSAndrew Thompson 
542b47888ceSAndrew Thompson 	bcopy(&ethermulticastaddr_slowprotocols,
543b47888ceSAndrew Thompson 	    LLADDR(&sdl), ETHER_ADDR_LEN);
544b47888ceSAndrew Thompson 	error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
545b47888ceSAndrew Thompson 	if (error) {
546eade13f9SGleb Smirnoff 		printf("%s: ADDMULTI failed on %s\n", __func__,
547eade13f9SGleb Smirnoff 		    lgp->lp_ifp->if_xname);
548b47888ceSAndrew Thompson 		return (error);
549b47888ceSAndrew Thompson 	}
550b47888ceSAndrew Thompson 
551b47888ceSAndrew Thompson 	lp = malloc(sizeof(struct lacp_port),
552b47888ceSAndrew Thompson 	    M_DEVBUF, M_NOWAIT|M_ZERO);
553b47888ceSAndrew Thompson 	if (lp == NULL)
554b47888ceSAndrew Thompson 		return (ENOMEM);
555b47888ceSAndrew Thompson 
5563de18008SAndrew Thompson 	LACP_LOCK(lsc);
5576900d0d3SGleb Smirnoff 	lgp->lp_psc = lp;
558b47888ceSAndrew Thompson 	lp->lp_ifp = ifp;
55918242d3bSAndrew Thompson 	lp->lp_lagg = lgp;
560b47888ceSAndrew Thompson 	lp->lp_lsc = lsc;
561d74fd345SAndrew Thompson 	lp->lp_ifma = rifma;
562b47888ceSAndrew Thompson 
563b47888ceSAndrew Thompson 	LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
564b47888ceSAndrew Thompson 
565b47888ceSAndrew Thompson 	lacp_fill_actorinfo(lp, &lp->lp_actor);
566998971a7SAndrew Thompson 	lacp_fill_markerinfo(lp, &lp->lp_marker);
567d592868eSRavi Pokala 	lp->lp_state = LACP_STATE_ACTIVITY;
568b47888ceSAndrew Thompson 	lp->lp_aggregator = NULL;
569b47888ceSAndrew Thompson 	lacp_sm_rx_set_expired(lp);
5703de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
5713de18008SAndrew Thompson 	lacp_linkstate(lgp);
572b47888ceSAndrew Thompson 
573b47888ceSAndrew Thompson 	return (0);
574b47888ceSAndrew Thompson }
575b47888ceSAndrew Thompson 
576b47888ceSAndrew Thompson void
lacp_port_destroy(struct lagg_port * lgp)57718242d3bSAndrew Thompson lacp_port_destroy(struct lagg_port *lgp)
578b47888ceSAndrew Thompson {
57918242d3bSAndrew Thompson 	struct lacp_port *lp = LACP_PORT(lgp);
5803de18008SAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
581d74fd345SAndrew Thompson 	int i;
582b47888ceSAndrew Thompson 
5833de18008SAndrew Thompson 	LACP_LOCK(lsc);
584b47888ceSAndrew Thompson 	for (i = 0; i < LACP_NTIMER; i++) {
585b47888ceSAndrew Thompson 		LACP_TIMER_DISARM(lp, i);
586b47888ceSAndrew Thompson 	}
587b47888ceSAndrew Thompson 
588b47888ceSAndrew Thompson 	lacp_disable_collecting(lp);
589b47888ceSAndrew Thompson 	lacp_disable_distributing(lp);
590b47888ceSAndrew Thompson 	lacp_unselect(lp);
591b47888ceSAndrew Thompson 
5926d478167SHiroki Sato 	LIST_REMOVE(lp, lp_next);
5936d478167SHiroki Sato 	LACP_UNLOCK(lsc);
5946d478167SHiroki Sato 
595108fe96aSAndrew Thompson 	/* The address may have already been removed by if_purgemaddrs() */
596108fe96aSAndrew Thompson 	if (!lgp->lp_detaching)
597d74fd345SAndrew Thompson 		if_delmulti_ifma(lp->lp_ifma);
598b47888ceSAndrew Thompson 
599b47888ceSAndrew Thompson 	free(lp, M_DEVBUF);
600b47888ceSAndrew Thompson }
601b47888ceSAndrew Thompson 
602b3d37ca5SAndrew Thompson void
lacp_req(struct lagg_softc * sc,void * data)6036900d0d3SGleb Smirnoff lacp_req(struct lagg_softc *sc, void *data)
604b3d37ca5SAndrew Thompson {
605b3d37ca5SAndrew Thompson 	struct lacp_opreq *req = (struct lacp_opreq *)data;
606b3d37ca5SAndrew Thompson 	struct lacp_softc *lsc = LACP_SOFTC(sc);
607f544a748SAlan Somers 	struct lacp_aggregator *la;
608b3d37ca5SAndrew Thompson 
609b3d37ca5SAndrew Thompson 	bzero(req, sizeof(struct lacp_opreq));
610f544a748SAlan Somers 
611f544a748SAlan Somers 	/*
612f544a748SAlan Somers 	 * If the LACP softc is NULL, return with the opreq structure full of
613f544a748SAlan Somers 	 * zeros.  It is normal for the softc to be NULL while the lagg is
614f544a748SAlan Somers 	 * being destroyed.
615f544a748SAlan Somers 	 */
616f544a748SAlan Somers 	if (NULL == lsc)
617f544a748SAlan Somers 		return;
618f544a748SAlan Somers 
619f544a748SAlan Somers 	la = lsc->lsc_active_aggregator;
620f544a748SAlan Somers 	LACP_LOCK(lsc);
621b3d37ca5SAndrew Thompson 	if (la != NULL) {
622b3d37ca5SAndrew Thompson 		req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
623b3d37ca5SAndrew Thompson 		memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
624b3d37ca5SAndrew Thompson 		    ETHER_ADDR_LEN);
625b3d37ca5SAndrew Thompson 		req->actor_key = ntohs(la->la_actor.lip_key);
626b3d37ca5SAndrew Thompson 		req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
627b3d37ca5SAndrew Thompson 		req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
628b3d37ca5SAndrew Thompson 		req->actor_state = la->la_actor.lip_state;
629b3d37ca5SAndrew Thompson 
630b3d37ca5SAndrew Thompson 		req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
631b3d37ca5SAndrew Thompson 		memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
632b3d37ca5SAndrew Thompson 		    ETHER_ADDR_LEN);
633b3d37ca5SAndrew Thompson 		req->partner_key = ntohs(la->la_partner.lip_key);
634b3d37ca5SAndrew Thompson 		req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
635b3d37ca5SAndrew Thompson 		req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
636b3d37ca5SAndrew Thompson 		req->partner_state = la->la_partner.lip_state;
637b3d37ca5SAndrew Thompson 	}
6383de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
639b3d37ca5SAndrew Thompson }
640b3d37ca5SAndrew Thompson 
641b3d37ca5SAndrew Thompson void
lacp_portreq(struct lagg_port * lgp,void * data)6426900d0d3SGleb Smirnoff lacp_portreq(struct lagg_port *lgp, void *data)
643b3d37ca5SAndrew Thompson {
644b3d37ca5SAndrew Thompson 	struct lacp_opreq *req = (struct lacp_opreq *)data;
645b3d37ca5SAndrew Thompson 	struct lacp_port *lp = LACP_PORT(lgp);
6463de18008SAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
647b3d37ca5SAndrew Thompson 
6483de18008SAndrew Thompson 	LACP_LOCK(lsc);
649b3d37ca5SAndrew Thompson 	req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
650b3d37ca5SAndrew Thompson 	memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
651b3d37ca5SAndrew Thompson 	    ETHER_ADDR_LEN);
652b3d37ca5SAndrew Thompson 	req->actor_key = ntohs(lp->lp_actor.lip_key);
653b3d37ca5SAndrew Thompson 	req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
654b3d37ca5SAndrew Thompson 	req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
655b3d37ca5SAndrew Thompson 	req->actor_state = lp->lp_actor.lip_state;
656b3d37ca5SAndrew Thompson 
657b3d37ca5SAndrew Thompson 	req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
658b3d37ca5SAndrew Thompson 	memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
659b3d37ca5SAndrew Thompson 	    ETHER_ADDR_LEN);
660b3d37ca5SAndrew Thompson 	req->partner_key = ntohs(lp->lp_partner.lip_key);
661b3d37ca5SAndrew Thompson 	req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
662b3d37ca5SAndrew Thompson 	req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
663b3d37ca5SAndrew Thompson 	req->partner_state = lp->lp_partner.lip_state;
6643de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
665b3d37ca5SAndrew Thompson }
666b3d37ca5SAndrew Thompson 
667b47888ceSAndrew Thompson static void
lacp_disable_collecting(struct lacp_port * lp)668b47888ceSAndrew Thompson lacp_disable_collecting(struct lacp_port *lp)
669b47888ceSAndrew Thompson {
670b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "collecting disabled\n"));
671b47888ceSAndrew Thompson 	lp->lp_state &= ~LACP_STATE_COLLECTING;
672b47888ceSAndrew Thompson }
673b47888ceSAndrew Thompson 
674b47888ceSAndrew Thompson static void
lacp_enable_collecting(struct lacp_port * lp)675b47888ceSAndrew Thompson lacp_enable_collecting(struct lacp_port *lp)
676b47888ceSAndrew Thompson {
677b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "collecting enabled\n"));
678b47888ceSAndrew Thompson 	lp->lp_state |= LACP_STATE_COLLECTING;
679b47888ceSAndrew Thompson }
680b47888ceSAndrew Thompson 
681b47888ceSAndrew Thompson static void
lacp_disable_distributing(struct lacp_port * lp)682b47888ceSAndrew Thompson lacp_disable_distributing(struct lacp_port *lp)
683b47888ceSAndrew Thompson {
684b47888ceSAndrew Thompson 	struct lacp_aggregator *la = lp->lp_aggregator;
685b47888ceSAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
68631402c27SAdrian Chadd 	struct lagg_softc *sc = lsc->lsc_softc;
687b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
688b47888ceSAndrew Thompson 
6893de18008SAndrew Thompson 	LACP_LOCK_ASSERT(lsc);
690b47888ceSAndrew Thompson 
691b47888ceSAndrew Thompson 	if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
692b47888ceSAndrew Thompson 		return;
693b47888ceSAndrew Thompson 	}
694b47888ceSAndrew Thompson 
695b47888ceSAndrew Thompson 	KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
696b47888ceSAndrew Thompson 	KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
697b47888ceSAndrew Thompson 	KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
698b47888ceSAndrew Thompson 
699b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
700b47888ceSAndrew Thompson 	    "nports %d -> %d\n",
701b47888ceSAndrew Thompson 	    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
702b47888ceSAndrew Thompson 	    la->la_nports, la->la_nports - 1));
703b47888ceSAndrew Thompson 
704b47888ceSAndrew Thompson 	TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
705b47888ceSAndrew Thompson 	la->la_nports--;
70631402c27SAdrian Chadd 	sc->sc_active = la->la_nports;
707b47888ceSAndrew Thompson 
7083de18008SAndrew Thompson 	if (lsc->lsc_active_aggregator == la) {
709b47888ceSAndrew Thompson 		lacp_suppress_distributing(lsc, la);
7103de18008SAndrew Thompson 		lacp_select_active_aggregator(lsc);
7113de18008SAndrew Thompson 		/* regenerate the port map, the active aggregator has changed */
7123de18008SAndrew Thompson 		lacp_update_portmap(lsc);
7133de18008SAndrew Thompson 	}
714b47888ceSAndrew Thompson 
715b47888ceSAndrew Thompson 	lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
7165ccac9f9SAndrew Gallatin 	if_link_state_change(sc->sc_ifp,
7175ccac9f9SAndrew Gallatin 	    sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
718b47888ceSAndrew Thompson }
719b47888ceSAndrew Thompson 
720b47888ceSAndrew Thompson static void
lacp_enable_distributing(struct lacp_port * lp)721b47888ceSAndrew Thompson lacp_enable_distributing(struct lacp_port *lp)
722b47888ceSAndrew Thompson {
723b47888ceSAndrew Thompson 	struct lacp_aggregator *la = lp->lp_aggregator;
724b47888ceSAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
72531402c27SAdrian Chadd 	struct lagg_softc *sc = lsc->lsc_softc;
726b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
727b47888ceSAndrew Thompson 
7283de18008SAndrew Thompson 	LACP_LOCK_ASSERT(lsc);
729b47888ceSAndrew Thompson 
730b47888ceSAndrew Thompson 	if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
731b47888ceSAndrew Thompson 		return;
732b47888ceSAndrew Thompson 	}
733b47888ceSAndrew Thompson 
734b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
735b47888ceSAndrew Thompson 	    "nports %d -> %d\n",
736b47888ceSAndrew Thompson 	    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
737b47888ceSAndrew Thompson 	    la->la_nports, la->la_nports + 1));
738b47888ceSAndrew Thompson 
739b47888ceSAndrew Thompson 	KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
740b47888ceSAndrew Thompson 	TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
741b47888ceSAndrew Thompson 	la->la_nports++;
74231402c27SAdrian Chadd 	sc->sc_active = la->la_nports;
743b47888ceSAndrew Thompson 
744b47888ceSAndrew Thompson 	lp->lp_state |= LACP_STATE_DISTRIBUTING;
745b47888ceSAndrew Thompson 
7463de18008SAndrew Thompson 	if (lsc->lsc_active_aggregator == la) {
7473de18008SAndrew Thompson 		lacp_suppress_distributing(lsc, la);
7483de18008SAndrew Thompson 		lacp_update_portmap(lsc);
7493de18008SAndrew Thompson 	} else
7503de18008SAndrew Thompson 		/* try to become the active aggregator */
751b47888ceSAndrew Thompson 		lacp_select_active_aggregator(lsc);
7525ccac9f9SAndrew Gallatin 
7535ccac9f9SAndrew Gallatin 	if_link_state_change(sc->sc_ifp,
7545ccac9f9SAndrew Gallatin 	    sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
755b47888ceSAndrew Thompson }
756b47888ceSAndrew Thompson 
757b47888ceSAndrew Thompson static void
lacp_transit_expire(void * vp)758b47888ceSAndrew Thompson lacp_transit_expire(void *vp)
759b47888ceSAndrew Thompson {
760b47888ceSAndrew Thompson 	struct lacp_softc *lsc = vp;
761b47888ceSAndrew Thompson 
7623de18008SAndrew Thompson 	LACP_LOCK_ASSERT(lsc);
7633de18008SAndrew Thompson 
7646d478167SHiroki Sato 	CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
7655fc4c149SAndrew Thompson 	LACP_TRACE(NULL);
7666d478167SHiroki Sato 	CURVNET_RESTORE();
7675fc4c149SAndrew Thompson 
768b47888ceSAndrew Thompson 	lsc->lsc_suppress_distributing = FALSE;
769b47888ceSAndrew Thompson }
770b47888ceSAndrew Thompson 
77109c7577eSGleb Smirnoff void
lacp_attach(struct lagg_softc * sc)772ec32b37eSAndrew Thompson lacp_attach(struct lagg_softc *sc)
773b47888ceSAndrew Thompson {
774b47888ceSAndrew Thompson 	struct lacp_softc *lsc;
775b47888ceSAndrew Thompson 
77609c7577eSGleb Smirnoff 	lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
777b47888ceSAndrew Thompson 
7786900d0d3SGleb Smirnoff 	sc->sc_psc = lsc;
779ec32b37eSAndrew Thompson 	lsc->lsc_softc = sc;
780b47888ceSAndrew Thompson 
781b7ba031fSHans Petter Selasky 	lsc->lsc_hashkey = m_ether_tcpip_hash_init();
782b47888ceSAndrew Thompson 	lsc->lsc_active_aggregator = NULL;
783c1be893cSSteven Hartland 	lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
7843de18008SAndrew Thompson 	LACP_LOCK_INIT(lsc);
785b47888ceSAndrew Thompson 	TAILQ_INIT(&lsc->lsc_aggregators);
786b47888ceSAndrew Thompson 	LIST_INIT(&lsc->lsc_ports);
787b47888ceSAndrew Thompson 
7883de18008SAndrew Thompson 	callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
7893de18008SAndrew Thompson 	callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
790b47888ceSAndrew Thompson 
79118242d3bSAndrew Thompson 	/* if the lagg is already up then do the same */
792ec32b37eSAndrew Thompson 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
793ec32b37eSAndrew Thompson 		lacp_init(sc);
794b47888ceSAndrew Thompson }
795b47888ceSAndrew Thompson 
796b1bbc5b3SGleb Smirnoff void
lacp_detach(void * psc)79709c7577eSGleb Smirnoff lacp_detach(void *psc)
798b47888ceSAndrew Thompson {
79909c7577eSGleb Smirnoff 	struct lacp_softc *lsc = (struct lacp_softc *)psc;
800b47888ceSAndrew Thompson 
801b47888ceSAndrew Thompson 	KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
802b47888ceSAndrew Thompson 	    ("aggregators still active"));
803b47888ceSAndrew Thompson 	KASSERT(lsc->lsc_active_aggregator == NULL,
804b47888ceSAndrew Thompson 	    ("aggregator still attached"));
805b47888ceSAndrew Thompson 
806b47888ceSAndrew Thompson 	callout_drain(&lsc->lsc_transit_callout);
807b47888ceSAndrew Thompson 	callout_drain(&lsc->lsc_callout);
808b47888ceSAndrew Thompson 
8093de18008SAndrew Thompson 	LACP_LOCK_DESTROY(lsc);
810b47888ceSAndrew Thompson 	free(lsc, M_DEVBUF);
811b47888ceSAndrew Thompson }
812b47888ceSAndrew Thompson 
813b47888ceSAndrew Thompson void
lacp_init(struct lagg_softc * sc)814ec32b37eSAndrew Thompson lacp_init(struct lagg_softc *sc)
815b47888ceSAndrew Thompson {
816ec32b37eSAndrew Thompson 	struct lacp_softc *lsc = LACP_SOFTC(sc);
817b47888ceSAndrew Thompson 
8183de18008SAndrew Thompson 	LACP_LOCK(lsc);
819b47888ceSAndrew Thompson 	callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
8203de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
821b47888ceSAndrew Thompson }
822b47888ceSAndrew Thompson 
823b47888ceSAndrew Thompson void
lacp_stop(struct lagg_softc * sc)824ec32b37eSAndrew Thompson lacp_stop(struct lagg_softc *sc)
825b47888ceSAndrew Thompson {
826ec32b37eSAndrew Thompson 	struct lacp_softc *lsc = LACP_SOFTC(sc);
827b47888ceSAndrew Thompson 
8283de18008SAndrew Thompson 	LACP_LOCK(lsc);
829b47888ceSAndrew Thompson 	callout_stop(&lsc->lsc_transit_callout);
830b47888ceSAndrew Thompson 	callout_stop(&lsc->lsc_callout);
8313de18008SAndrew Thompson 	LACP_UNLOCK(lsc);
832b47888ceSAndrew Thompson }
833b47888ceSAndrew Thompson 
83418242d3bSAndrew Thompson struct lagg_port *
lacp_select_tx_port_by_hash(struct lagg_softc * sc,uint32_t hash,uint8_t numa_domain,int * err)8358732245dSAndrew Gallatin lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t hash,
8368732245dSAndrew Gallatin     uint8_t numa_domain, int *err)
837b47888ceSAndrew Thompson {
838ec32b37eSAndrew Thompson 	struct lacp_softc *lsc = LACP_SOFTC(sc);
8393de18008SAndrew Thompson 	struct lacp_portmap *pm;
840b47888ceSAndrew Thompson 	struct lacp_port *lp;
84135961dceSAndrew Gallatin 	struct lacp_port **map;
84235961dceSAndrew Gallatin 	int count;
843b47888ceSAndrew Thompson 
844b47888ceSAndrew Thompson 	if (__predict_false(lsc->lsc_suppress_distributing)) {
845b47888ceSAndrew Thompson 		LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
8468732245dSAndrew Gallatin 		*err = ENOBUFS;
847b47888ceSAndrew Thompson 		return (NULL);
848b47888ceSAndrew Thompson 	}
849b47888ceSAndrew Thompson 
8503de18008SAndrew Thompson 	pm = &lsc->lsc_pmap[lsc->lsc_activemap];
8513de18008SAndrew Thompson 	if (pm->pm_count == 0) {
852b47888ceSAndrew Thompson 		LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
8538732245dSAndrew Gallatin 		*err = ENETDOWN;
854b47888ceSAndrew Thompson 		return (NULL);
855b47888ceSAndrew Thompson 	}
856b47888ceSAndrew Thompson 
85735961dceSAndrew Gallatin #ifdef NUMA
85835961dceSAndrew Gallatin 	if ((sc->sc_opts & LAGG_OPT_USE_NUMA) &&
85998085baeSAndrew Gallatin 	    pm->pm_num_dom > 1 && numa_domain < MAXMEMDOM) {
86098085baeSAndrew Gallatin 		count = pm->pm_numa[numa_domain].count;
86135961dceSAndrew Gallatin 		if (count > 0) {
86298085baeSAndrew Gallatin 			map = pm->pm_numa[numa_domain].map;
86335961dceSAndrew Gallatin 		} else {
86435961dceSAndrew Gallatin 			/* No ports on this domain; use global hash. */
86535961dceSAndrew Gallatin 			map = pm->pm_map;
86635961dceSAndrew Gallatin 			count = pm->pm_count;
86735961dceSAndrew Gallatin 		}
86835961dceSAndrew Gallatin 	} else
86935961dceSAndrew Gallatin #endif
87035961dceSAndrew Gallatin 	{
87135961dceSAndrew Gallatin 		map = pm->pm_map;
87235961dceSAndrew Gallatin 		count = pm->pm_count;
87335961dceSAndrew Gallatin 	}
87435961dceSAndrew Gallatin 
87535961dceSAndrew Gallatin 	hash %= count;
87635961dceSAndrew Gallatin 	lp = map[hash];
877b47888ceSAndrew Thompson 
87818242d3bSAndrew Thompson 	return (lp->lp_lagg);
879b47888ceSAndrew Thompson }
880f3e7afe2SHans Petter Selasky 
881f3e7afe2SHans Petter Selasky struct lagg_port *
lacp_select_tx_port(struct lagg_softc * sc,struct mbuf * m,int * err)8828732245dSAndrew Gallatin lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m, int *err)
883f3e7afe2SHans Petter Selasky {
884f3e7afe2SHans Petter Selasky 	struct lacp_softc *lsc = LACP_SOFTC(sc);
885f3e7afe2SHans Petter Selasky 	uint32_t hash;
88698085baeSAndrew Gallatin 	uint8_t numa_domain;
887f3e7afe2SHans Petter Selasky 
88898085baeSAndrew Gallatin 	if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
88998085baeSAndrew Gallatin 	    M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
89098085baeSAndrew Gallatin 		hash = m->m_pkthdr.flowid >> sc->flowid_shift;
89198085baeSAndrew Gallatin 	else
89298085baeSAndrew Gallatin 		hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
89398085baeSAndrew Gallatin 
89498085baeSAndrew Gallatin 	numa_domain = m->m_pkthdr.numa_domain;
8958732245dSAndrew Gallatin 	return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, err));
896f3e7afe2SHans Petter Selasky }
897f3e7afe2SHans Petter Selasky 
898b47888ceSAndrew Thompson /*
899b47888ceSAndrew Thompson  * lacp_suppress_distributing: drop transmit packets for a while
900b47888ceSAndrew Thompson  * to preserve packet ordering.
901b47888ceSAndrew Thompson  */
902b47888ceSAndrew Thompson 
903b47888ceSAndrew Thompson static void
lacp_suppress_distributing(struct lacp_softc * lsc,struct lacp_aggregator * la)904b47888ceSAndrew Thompson lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
905b47888ceSAndrew Thompson {
906998971a7SAndrew Thompson 	struct lacp_port *lp;
907998971a7SAndrew Thompson 
908b47888ceSAndrew Thompson 	if (lsc->lsc_active_aggregator != la) {
909b47888ceSAndrew Thompson 		return;
910b47888ceSAndrew Thompson 	}
911b47888ceSAndrew Thompson 
9125fc4c149SAndrew Thompson 	LACP_TRACE(NULL);
9135fc4c149SAndrew Thompson 
914b47888ceSAndrew Thompson 	lsc->lsc_suppress_distributing = TRUE;
915998971a7SAndrew Thompson 
916998971a7SAndrew Thompson 	/* send a marker frame down each port to verify the queues are empty */
917998971a7SAndrew Thompson 	LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
918998971a7SAndrew Thompson 		lp->lp_flags |= LACP_PORT_MARK;
9190b92a7feSArnaud Ysmal 		if (lacp_xmit_marker(lp) != 0)
9200b92a7feSArnaud Ysmal 			lp->lp_flags &= ~LACP_PORT_MARK;
921998971a7SAndrew Thompson 	}
922998971a7SAndrew Thompson 
923998971a7SAndrew Thompson 	/* set a timeout for the marker frames */
924b47888ceSAndrew Thompson 	callout_reset(&lsc->lsc_transit_callout,
925b47888ceSAndrew Thompson 	    LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
926b47888ceSAndrew Thompson }
927b47888ceSAndrew Thompson 
928b47888ceSAndrew Thompson static int
lacp_compare_peerinfo(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b)929b47888ceSAndrew Thompson lacp_compare_peerinfo(const struct lacp_peerinfo *a,
930b47888ceSAndrew Thompson     const struct lacp_peerinfo *b)
931b47888ceSAndrew Thompson {
932b47888ceSAndrew Thompson 	return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
933b47888ceSAndrew Thompson }
934b47888ceSAndrew Thompson 
935b47888ceSAndrew Thompson static int
lacp_compare_systemid(const struct lacp_systemid * a,const struct lacp_systemid * b)936b47888ceSAndrew Thompson lacp_compare_systemid(const struct lacp_systemid *a,
937b47888ceSAndrew Thompson     const struct lacp_systemid *b)
938b47888ceSAndrew Thompson {
939b47888ceSAndrew Thompson 	return (memcmp(a, b, sizeof(*a)));
940b47888ceSAndrew Thompson }
941b47888ceSAndrew Thompson 
942b47888ceSAndrew Thompson #if 0	/* unused */
943b47888ceSAndrew Thompson static int
944b47888ceSAndrew Thompson lacp_compare_portid(const struct lacp_portid *a,
945b47888ceSAndrew Thompson     const struct lacp_portid *b)
946b47888ceSAndrew Thompson {
947b47888ceSAndrew Thompson 	return (memcmp(a, b, sizeof(*a)));
948b47888ceSAndrew Thompson }
949b47888ceSAndrew Thompson #endif
950b47888ceSAndrew Thompson 
951b47888ceSAndrew Thompson static uint64_t
lacp_aggregator_bandwidth(struct lacp_aggregator * la)952b47888ceSAndrew Thompson lacp_aggregator_bandwidth(struct lacp_aggregator *la)
953b47888ceSAndrew Thompson {
954b47888ceSAndrew Thompson 	struct lacp_port *lp;
955b47888ceSAndrew Thompson 	uint64_t speed;
956b47888ceSAndrew Thompson 
957b47888ceSAndrew Thompson 	lp = TAILQ_FIRST(&la->la_ports);
958b47888ceSAndrew Thompson 	if (lp == NULL) {
959b47888ceSAndrew Thompson 		return (0);
960b47888ceSAndrew Thompson 	}
961b47888ceSAndrew Thompson 
962b47888ceSAndrew Thompson 	speed = ifmedia_baudrate(lp->lp_media);
963b47888ceSAndrew Thompson 	speed *= la->la_nports;
964b47888ceSAndrew Thompson 	if (speed == 0) {
965b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
966b47888ceSAndrew Thompson 		    lp->lp_media, la->la_nports));
967b47888ceSAndrew Thompson 	}
968b47888ceSAndrew Thompson 
969b47888ceSAndrew Thompson 	return (speed);
970b47888ceSAndrew Thompson }
971b47888ceSAndrew Thompson 
972b47888ceSAndrew Thompson /*
973b47888ceSAndrew Thompson  * lacp_select_active_aggregator: select an aggregator to be used to transmit
97418242d3bSAndrew Thompson  * packets from lagg(4) interface.
975b47888ceSAndrew Thompson  */
976b47888ceSAndrew Thompson 
977b47888ceSAndrew Thompson static void
lacp_select_active_aggregator(struct lacp_softc * lsc)978b47888ceSAndrew Thompson lacp_select_active_aggregator(struct lacp_softc *lsc)
979b47888ceSAndrew Thompson {
980b47888ceSAndrew Thompson 	struct lacp_aggregator *la;
981b47888ceSAndrew Thompson 	struct lacp_aggregator *best_la = NULL;
982b47888ceSAndrew Thompson 	uint64_t best_speed = 0;
983b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
984b47888ceSAndrew Thompson 
9855fc4c149SAndrew Thompson 	LACP_TRACE(NULL);
986b47888ceSAndrew Thompson 
987b47888ceSAndrew Thompson 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
988b47888ceSAndrew Thompson 		uint64_t speed;
989b47888ceSAndrew Thompson 
990b47888ceSAndrew Thompson 		if (la->la_nports == 0) {
991b47888ceSAndrew Thompson 			continue;
992b47888ceSAndrew Thompson 		}
993b47888ceSAndrew Thompson 
994b47888ceSAndrew Thompson 		speed = lacp_aggregator_bandwidth(la);
995b47888ceSAndrew Thompson 		LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
996b47888ceSAndrew Thompson 		    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
997b47888ceSAndrew Thompson 		    speed, la->la_nports));
998fe45e65fSAndrew Thompson 
9996900d0d3SGleb Smirnoff 		/*
10006900d0d3SGleb Smirnoff 		 * This aggregator is chosen if the partner has a better
10016900d0d3SGleb Smirnoff 		 * system priority or, the total aggregated speed is higher
1002fe45e65fSAndrew Thompson 		 * or, it is already the chosen aggregator
1003fe45e65fSAndrew Thompson 		 */
1004fe45e65fSAndrew Thompson 		if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1005fe45e65fSAndrew Thompson 		    LACP_SYS_PRI(best_la->la_partner)) ||
1006fe45e65fSAndrew Thompson 		    speed > best_speed ||
1007b47888ceSAndrew Thompson 		    (speed == best_speed &&
1008b47888ceSAndrew Thompson 		    la == lsc->lsc_active_aggregator)) {
1009b47888ceSAndrew Thompson 			best_la = la;
1010b47888ceSAndrew Thompson 			best_speed = speed;
1011b47888ceSAndrew Thompson 		}
1012b47888ceSAndrew Thompson 	}
1013b47888ceSAndrew Thompson 
1014b47888ceSAndrew Thompson 	KASSERT(best_la == NULL || best_la->la_nports > 0,
1015b47888ceSAndrew Thompson 	    ("invalid aggregator refcnt"));
1016b47888ceSAndrew Thompson 	KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1017b47888ceSAndrew Thompson 	    ("invalid aggregator list"));
1018b47888ceSAndrew Thompson 
1019b47888ceSAndrew Thompson 	if (lsc->lsc_active_aggregator != best_la) {
1020b47888ceSAndrew Thompson 		LACP_DPRINTF((NULL, "active aggregator changed\n"));
1021b47888ceSAndrew Thompson 		LACP_DPRINTF((NULL, "old %s\n",
1022b47888ceSAndrew Thompson 		    lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1023b47888ceSAndrew Thompson 		    buf, sizeof(buf))));
1024b47888ceSAndrew Thompson 	} else {
1025b47888ceSAndrew Thompson 		LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1026b47888ceSAndrew Thompson 	}
1027b47888ceSAndrew Thompson 	LACP_DPRINTF((NULL, "new %s\n",
1028b47888ceSAndrew Thompson 	    lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1029b47888ceSAndrew Thompson 
1030b47888ceSAndrew Thompson 	if (lsc->lsc_active_aggregator != best_la) {
1031b47888ceSAndrew Thompson 		lsc->lsc_active_aggregator = best_la;
10323de18008SAndrew Thompson 		lacp_update_portmap(lsc);
1033b47888ceSAndrew Thompson 		if (best_la) {
1034b47888ceSAndrew Thompson 			lacp_suppress_distributing(lsc, best_la);
1035b47888ceSAndrew Thompson 		}
1036b47888ceSAndrew Thompson 	}
1037b47888ceSAndrew Thompson }
1038b47888ceSAndrew Thompson 
10393de18008SAndrew Thompson /*
10403de18008SAndrew Thompson  * Updated the inactive portmap array with the new list of ports and
10413de18008SAndrew Thompson  * make it live.
10423de18008SAndrew Thompson  */
10433de18008SAndrew Thompson static void
lacp_update_portmap(struct lacp_softc * lsc)10443de18008SAndrew Thompson lacp_update_portmap(struct lacp_softc *lsc)
10453de18008SAndrew Thompson {
104631402c27SAdrian Chadd 	struct lagg_softc *sc = lsc->lsc_softc;
10473de18008SAndrew Thompson 	struct lacp_aggregator *la;
10483de18008SAndrew Thompson 	struct lacp_portmap *p;
10493de18008SAndrew Thompson 	struct lacp_port *lp;
105031402c27SAdrian Chadd 	uint64_t speed;
10513de18008SAndrew Thompson 	u_int newmap;
10523de18008SAndrew Thompson 	int i;
105335961dceSAndrew Gallatin #ifdef NUMA
105435961dceSAndrew Gallatin 	int count;
105535961dceSAndrew Gallatin 	uint8_t domain;
105635961dceSAndrew Gallatin #endif
10573de18008SAndrew Thompson 
10583de18008SAndrew Thompson 	newmap = lsc->lsc_activemap == 0 ? 1 : 0;
10593de18008SAndrew Thompson 	p = &lsc->lsc_pmap[newmap];
10603de18008SAndrew Thompson 	la = lsc->lsc_active_aggregator;
106131402c27SAdrian Chadd 	speed = 0;
10623de18008SAndrew Thompson 	bzero(p, sizeof(struct lacp_portmap));
10633de18008SAndrew Thompson 
10643de18008SAndrew Thompson 	if (la != NULL && la->la_nports > 0) {
10653de18008SAndrew Thompson 		p->pm_count = la->la_nports;
10663de18008SAndrew Thompson 		i = 0;
106735961dceSAndrew Gallatin 		TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) {
10683de18008SAndrew Thompson 			p->pm_map[i++] = lp;
106935961dceSAndrew Gallatin #ifdef NUMA
107035961dceSAndrew Gallatin 			domain = lp->lp_ifp->if_numa_domain;
107135961dceSAndrew Gallatin 			if (domain >= MAXMEMDOM)
107235961dceSAndrew Gallatin 				continue;
107335961dceSAndrew Gallatin 			count = p->pm_numa[domain].count;
107435961dceSAndrew Gallatin 			p->pm_numa[domain].map[count] = lp;
107535961dceSAndrew Gallatin 			p->pm_numa[domain].count++;
107635961dceSAndrew Gallatin #endif
107735961dceSAndrew Gallatin 		}
10783de18008SAndrew Thompson 		KASSERT(i == p->pm_count, ("Invalid port count"));
107935961dceSAndrew Gallatin 
108035961dceSAndrew Gallatin #ifdef NUMA
108135961dceSAndrew Gallatin 		for (i = 0; i < MAXMEMDOM; i++) {
108235961dceSAndrew Gallatin 			if (p->pm_numa[i].count != 0)
108335961dceSAndrew Gallatin 				p->pm_num_dom++;
108435961dceSAndrew Gallatin 		}
108535961dceSAndrew Gallatin #endif
108631402c27SAdrian Chadd 		speed = lacp_aggregator_bandwidth(la);
10873de18008SAndrew Thompson 	}
108831402c27SAdrian Chadd 	sc->sc_ifp->if_baudrate = speed;
1089f2ab9160SAndrey V. Elsukov 	EVENTHANDLER_INVOKE(ifnet_event, sc->sc_ifp,
1090f2ab9160SAndrey V. Elsukov 	    IFNET_EVENT_UPDATE_BAUDRATE);
10913de18008SAndrew Thompson 
10923de18008SAndrew Thompson 	/* switch the active portmap over */
10933de18008SAndrew Thompson 	atomic_store_rel_int(&lsc->lsc_activemap, newmap);
10943de18008SAndrew Thompson 	LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
10953de18008SAndrew Thompson 		    lsc->lsc_activemap,
10963de18008SAndrew Thompson 		    lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
10973de18008SAndrew Thompson }
10983de18008SAndrew Thompson 
1099b47888ceSAndrew Thompson static uint16_t
lacp_compose_key(struct lacp_port * lp)1100b47888ceSAndrew Thompson lacp_compose_key(struct lacp_port *lp)
1101b47888ceSAndrew Thompson {
110218242d3bSAndrew Thompson 	struct lagg_port *lgp = lp->lp_lagg;
1103ec32b37eSAndrew Thompson 	struct lagg_softc *sc = lgp->lp_softc;
1104b47888ceSAndrew Thompson 	u_int media = lp->lp_media;
1105b47888ceSAndrew Thompson 	uint16_t key;
1106b47888ceSAndrew Thompson 
1107b47888ceSAndrew Thompson 	if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1108b47888ceSAndrew Thompson 		/*
1109b47888ceSAndrew Thompson 		 * non-aggregatable links should have unique keys.
1110b47888ceSAndrew Thompson 		 *
1111b47888ceSAndrew Thompson 		 * XXX this isn't really unique as if_index is 16 bit.
1112b47888ceSAndrew Thompson 		 */
1113b47888ceSAndrew Thompson 
1114b47888ceSAndrew Thompson 		/* bit 0..14:	(some bits of) if_index of this port */
1115b47888ceSAndrew Thompson 		key = lp->lp_ifp->if_index;
1116b47888ceSAndrew Thompson 		/* bit 15:	1 */
1117b47888ceSAndrew Thompson 		key |= 0x8000;
1118b47888ceSAndrew Thompson 	} else {
1119b47888ceSAndrew Thompson 		u_int subtype = IFM_SUBTYPE(media);
1120b47888ceSAndrew Thompson 
1121e3163ef6SAndrew Thompson 		KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1122e3163ef6SAndrew Thompson 		KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1123b47888ceSAndrew Thompson 
1124d5773da8SAndrey V. Elsukov 		/* bit 0..4:	IFM_SUBTYPE modulo speed */
1125d5773da8SAndrey V. Elsukov 		switch (subtype) {
1126d5773da8SAndrey V. Elsukov 		case IFM_10_T:
1127d5773da8SAndrey V. Elsukov 		case IFM_10_2:
1128d5773da8SAndrey V. Elsukov 		case IFM_10_5:
1129d5773da8SAndrey V. Elsukov 		case IFM_10_STP:
1130d5773da8SAndrey V. Elsukov 		case IFM_10_FL:
1131d5773da8SAndrey V. Elsukov 			key = IFM_10_T;
1132d5773da8SAndrey V. Elsukov 			break;
1133d5773da8SAndrey V. Elsukov 		case IFM_100_TX:
1134d5773da8SAndrey V. Elsukov 		case IFM_100_FX:
1135d5773da8SAndrey V. Elsukov 		case IFM_100_T4:
1136d5773da8SAndrey V. Elsukov 		case IFM_100_VG:
1137d5773da8SAndrey V. Elsukov 		case IFM_100_T2:
1138eb7e25b2SEric Joyner 		case IFM_100_T:
1139fe2bf351SEric Joyner 		case IFM_100_SGMII:
1140d5773da8SAndrey V. Elsukov 			key = IFM_100_TX;
1141d5773da8SAndrey V. Elsukov 			break;
1142d5773da8SAndrey V. Elsukov 		case IFM_1000_SX:
1143d5773da8SAndrey V. Elsukov 		case IFM_1000_LX:
1144d5773da8SAndrey V. Elsukov 		case IFM_1000_CX:
1145d5773da8SAndrey V. Elsukov 		case IFM_1000_T:
1146eb7e25b2SEric Joyner 		case IFM_1000_KX:
1147eb7e25b2SEric Joyner 		case IFM_1000_SGMII:
1148eb7e25b2SEric Joyner 		case IFM_1000_CX_SGMII:
1149d5773da8SAndrey V. Elsukov 			key = IFM_1000_SX;
1150d5773da8SAndrey V. Elsukov 			break;
1151d5773da8SAndrey V. Elsukov 		case IFM_10G_LR:
1152d5773da8SAndrey V. Elsukov 		case IFM_10G_SR:
1153d5773da8SAndrey V. Elsukov 		case IFM_10G_CX4:
1154d5773da8SAndrey V. Elsukov 		case IFM_10G_TWINAX:
1155d5773da8SAndrey V. Elsukov 		case IFM_10G_TWINAX_LONG:
1156d5773da8SAndrey V. Elsukov 		case IFM_10G_LRM:
1157d5773da8SAndrey V. Elsukov 		case IFM_10G_T:
1158eb7e25b2SEric Joyner 		case IFM_10G_KX4:
1159eb7e25b2SEric Joyner 		case IFM_10G_KR:
1160eb7e25b2SEric Joyner 		case IFM_10G_CR1:
1161eb7e25b2SEric Joyner 		case IFM_10G_ER:
1162eb7e25b2SEric Joyner 		case IFM_10G_SFI:
11636e105d4eSEric Joyner 		case IFM_10G_AOC:
1164d5773da8SAndrey V. Elsukov 			key = IFM_10G_LR;
1165d5773da8SAndrey V. Elsukov 			break;
1166eb7e25b2SEric Joyner 		case IFM_20G_KR2:
1167eb7e25b2SEric Joyner 			key = IFM_20G_KR2;
1168eb7e25b2SEric Joyner 			break;
1169eb7e25b2SEric Joyner 		case IFM_2500_KX:
1170eb7e25b2SEric Joyner 		case IFM_2500_T:
1171fe2bf351SEric Joyner 		case IFM_2500_X:
1172eb7e25b2SEric Joyner 			key = IFM_2500_KX;
1173eb7e25b2SEric Joyner 			break;
1174eb7e25b2SEric Joyner 		case IFM_5000_T:
1175fe2bf351SEric Joyner 		case IFM_5000_KR:
1176fe2bf351SEric Joyner 		case IFM_5000_KR_S:
1177fe2bf351SEric Joyner 		case IFM_5000_KR1:
1178eb7e25b2SEric Joyner 			key = IFM_5000_T;
1179eb7e25b2SEric Joyner 			break;
1180eb7e25b2SEric Joyner 		case IFM_50G_PCIE:
1181eb7e25b2SEric Joyner 		case IFM_50G_CR2:
1182eb7e25b2SEric Joyner 		case IFM_50G_KR2:
11835d1277caSKonstantin Belousov 		case IFM_50G_KR4:
1184fe2bf351SEric Joyner 		case IFM_50G_SR2:
1185fe2bf351SEric Joyner 		case IFM_50G_LR2:
1186fe2bf351SEric Joyner 		case IFM_50G_LAUI2_AC:
1187fe2bf351SEric Joyner 		case IFM_50G_LAUI2:
1188fe2bf351SEric Joyner 		case IFM_50G_AUI2_AC:
1189fe2bf351SEric Joyner 		case IFM_50G_AUI2:
1190fe2bf351SEric Joyner 		case IFM_50G_CP:
1191fe2bf351SEric Joyner 		case IFM_50G_SR:
1192fe2bf351SEric Joyner 		case IFM_50G_LR:
1193fe2bf351SEric Joyner 		case IFM_50G_FR:
1194fe2bf351SEric Joyner 		case IFM_50G_KR_PAM4:
1195fe2bf351SEric Joyner 		case IFM_50G_AUI1_AC:
1196fe2bf351SEric Joyner 		case IFM_50G_AUI1:
1197eb7e25b2SEric Joyner 			key = IFM_50G_PCIE;
1198eb7e25b2SEric Joyner 			break;
1199eb7e25b2SEric Joyner 		case IFM_56G_R4:
1200eb7e25b2SEric Joyner 			key = IFM_56G_R4;
1201eb7e25b2SEric Joyner 			break;
1202eb7e25b2SEric Joyner 		case IFM_25G_PCIE:
1203eb7e25b2SEric Joyner 		case IFM_25G_CR:
1204eb7e25b2SEric Joyner 		case IFM_25G_KR:
1205eb7e25b2SEric Joyner 		case IFM_25G_SR:
12066e105d4eSEric Joyner 		case IFM_25G_LR:
12076e105d4eSEric Joyner 		case IFM_25G_ACC:
12086e105d4eSEric Joyner 		case IFM_25G_AOC:
1209fe2bf351SEric Joyner 		case IFM_25G_T:
1210fe2bf351SEric Joyner 		case IFM_25G_CR_S:
1211fe2bf351SEric Joyner 		case IFM_25G_CR1:
1212fe2bf351SEric Joyner 		case IFM_25G_KR_S:
1213fe2bf351SEric Joyner 		case IFM_25G_AUI:
1214fe2bf351SEric Joyner 		case IFM_25G_KR1:
1215eb7e25b2SEric Joyner 			key = IFM_25G_PCIE;
1216eb7e25b2SEric Joyner 			break;
1217d5773da8SAndrey V. Elsukov 		case IFM_40G_CR4:
1218d5773da8SAndrey V. Elsukov 		case IFM_40G_SR4:
1219d5773da8SAndrey V. Elsukov 		case IFM_40G_LR4:
1220ceff9b9dSMitchell Horne 		case IFM_40G_LM4:
1221eb7e25b2SEric Joyner 		case IFM_40G_XLPPI:
1222eb7e25b2SEric Joyner 		case IFM_40G_KR4:
1223fe2bf351SEric Joyner 		case IFM_40G_XLAUI:
1224fe2bf351SEric Joyner 		case IFM_40G_XLAUI_AC:
1225fe2bf351SEric Joyner 		case IFM_40G_ER4:
1226d5773da8SAndrey V. Elsukov 			key = IFM_40G_CR4;
1227d5773da8SAndrey V. Elsukov 			break;
1228eb7e25b2SEric Joyner 		case IFM_100G_CR4:
1229eb7e25b2SEric Joyner 		case IFM_100G_SR4:
1230eb7e25b2SEric Joyner 		case IFM_100G_KR4:
1231eb7e25b2SEric Joyner 		case IFM_100G_LR4:
1232fe2bf351SEric Joyner 		case IFM_100G_CAUI4_AC:
1233fe2bf351SEric Joyner 		case IFM_100G_CAUI4:
1234fe2bf351SEric Joyner 		case IFM_100G_AUI4_AC:
1235fe2bf351SEric Joyner 		case IFM_100G_AUI4:
1236fe2bf351SEric Joyner 		case IFM_100G_CR_PAM4:
1237fe2bf351SEric Joyner 		case IFM_100G_KR_PAM4:
1238fe2bf351SEric Joyner 		case IFM_100G_CP2:
1239fe2bf351SEric Joyner 		case IFM_100G_SR2:
1240fe2bf351SEric Joyner 		case IFM_100G_DR:
1241fe2bf351SEric Joyner 		case IFM_100G_KR2_PAM4:
1242fe2bf351SEric Joyner 		case IFM_100G_CAUI2_AC:
1243fe2bf351SEric Joyner 		case IFM_100G_CAUI2:
1244fe2bf351SEric Joyner 		case IFM_100G_AUI2_AC:
1245fe2bf351SEric Joyner 		case IFM_100G_AUI2:
1246eb7e25b2SEric Joyner 			key = IFM_100G_CR4;
1247eb7e25b2SEric Joyner 			break;
1248fe2bf351SEric Joyner 		case IFM_200G_CR4_PAM4:
1249fe2bf351SEric Joyner 		case IFM_200G_SR4:
1250fe2bf351SEric Joyner 		case IFM_200G_FR4:
1251fe2bf351SEric Joyner 		case IFM_200G_LR4:
1252fe2bf351SEric Joyner 		case IFM_200G_DR4:
1253fe2bf351SEric Joyner 		case IFM_200G_KR4_PAM4:
1254fe2bf351SEric Joyner 		case IFM_200G_AUI4_AC:
1255fe2bf351SEric Joyner 		case IFM_200G_AUI4:
1256fe2bf351SEric Joyner 		case IFM_200G_AUI8_AC:
1257fe2bf351SEric Joyner 		case IFM_200G_AUI8:
1258fe2bf351SEric Joyner 			key = IFM_200G_CR4_PAM4;
1259fe2bf351SEric Joyner 			break;
1260fe2bf351SEric Joyner 		case IFM_400G_FR8:
1261fe2bf351SEric Joyner 		case IFM_400G_LR8:
1262fe2bf351SEric Joyner 		case IFM_400G_DR4:
1263fe2bf351SEric Joyner 		case IFM_400G_AUI8_AC:
1264fe2bf351SEric Joyner 		case IFM_400G_AUI8:
1265fe2bf351SEric Joyner 			key = IFM_400G_FR8;
1266fe2bf351SEric Joyner 			break;
1267d5773da8SAndrey V. Elsukov 		default:
1268b47888ceSAndrew Thompson 			key = subtype;
1269eb7e25b2SEric Joyner 			break;
1270d5773da8SAndrey V. Elsukov 		}
127118242d3bSAndrew Thompson 		/* bit 5..14:	(some bits of) if_index of lagg device */
1272ec32b37eSAndrew Thompson 		key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1273b47888ceSAndrew Thompson 		/* bit 15:	0 */
1274b47888ceSAndrew Thompson 	}
1275b47888ceSAndrew Thompson 	return (htons(key));
1276b47888ceSAndrew Thompson }
1277b47888ceSAndrew Thompson 
1278b47888ceSAndrew Thompson static void
lacp_aggregator_addref(struct lacp_softc * lsc,struct lacp_aggregator * la)1279b47888ceSAndrew Thompson lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1280b47888ceSAndrew Thompson {
1281b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
1282b47888ceSAndrew Thompson 
1283b47888ceSAndrew Thompson 	LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1284b47888ceSAndrew Thompson 	    __func__,
1285b47888ceSAndrew Thompson 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1286b47888ceSAndrew Thompson 	    buf, sizeof(buf)),
1287b47888ceSAndrew Thompson 	    la->la_refcnt, la->la_refcnt + 1));
1288b47888ceSAndrew Thompson 
1289b47888ceSAndrew Thompson 	KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1290b47888ceSAndrew Thompson 	la->la_refcnt++;
1291b47888ceSAndrew Thompson 	KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1292b47888ceSAndrew Thompson }
1293b47888ceSAndrew Thompson 
1294b47888ceSAndrew Thompson static void
lacp_aggregator_delref(struct lacp_softc * lsc,struct lacp_aggregator * la)1295b47888ceSAndrew Thompson lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1296b47888ceSAndrew Thompson {
1297b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
1298b47888ceSAndrew Thompson 
1299b47888ceSAndrew Thompson 	LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1300b47888ceSAndrew Thompson 	    __func__,
1301b47888ceSAndrew Thompson 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1302b47888ceSAndrew Thompson 	    buf, sizeof(buf)),
1303b47888ceSAndrew Thompson 	    la->la_refcnt, la->la_refcnt - 1));
1304b47888ceSAndrew Thompson 
1305b47888ceSAndrew Thompson 	KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1306b47888ceSAndrew Thompson 	la->la_refcnt--;
1307b47888ceSAndrew Thompson 	if (la->la_refcnt > 0) {
1308b47888ceSAndrew Thompson 		return;
1309b47888ceSAndrew Thompson 	}
1310b47888ceSAndrew Thompson 
1311b47888ceSAndrew Thompson 	KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1312b47888ceSAndrew Thompson 	KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1313b47888ceSAndrew Thompson 
1314b47888ceSAndrew Thompson 	TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1315b47888ceSAndrew Thompson 
1316b47888ceSAndrew Thompson 	free(la, M_DEVBUF);
1317b47888ceSAndrew Thompson }
1318b47888ceSAndrew Thompson 
1319b47888ceSAndrew Thompson /*
1320b47888ceSAndrew Thompson  * lacp_aggregator_get: allocate an aggregator.
1321b47888ceSAndrew Thompson  */
1322b47888ceSAndrew Thompson 
1323b47888ceSAndrew Thompson static struct lacp_aggregator *
lacp_aggregator_get(struct lacp_softc * lsc,struct lacp_port * lp)1324b47888ceSAndrew Thompson lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1325b47888ceSAndrew Thompson {
1326b47888ceSAndrew Thompson 	struct lacp_aggregator *la;
1327b47888ceSAndrew Thompson 
1328b47888ceSAndrew Thompson 	la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1329b47888ceSAndrew Thompson 	if (la) {
1330b47888ceSAndrew Thompson 		la->la_refcnt = 1;
1331b47888ceSAndrew Thompson 		la->la_nports = 0;
1332b47888ceSAndrew Thompson 		TAILQ_INIT(&la->la_ports);
1333b47888ceSAndrew Thompson 		la->la_pending = 0;
1334b47888ceSAndrew Thompson 		TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1335b47888ceSAndrew Thompson 	}
1336b47888ceSAndrew Thompson 
1337b47888ceSAndrew Thompson 	return (la);
1338b47888ceSAndrew Thompson }
1339b47888ceSAndrew Thompson 
1340b47888ceSAndrew Thompson /*
1341b47888ceSAndrew Thompson  * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1342b47888ceSAndrew Thompson  */
1343b47888ceSAndrew Thompson 
1344b47888ceSAndrew Thompson static void
lacp_fill_aggregator_id(struct lacp_aggregator * la,const struct lacp_port * lp)1345b47888ceSAndrew Thompson lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1346b47888ceSAndrew Thompson {
1347b47888ceSAndrew Thompson 	lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1348b47888ceSAndrew Thompson 	lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1349b47888ceSAndrew Thompson 
1350b47888ceSAndrew Thompson 	la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1351b47888ceSAndrew Thompson }
1352b47888ceSAndrew Thompson 
1353b47888ceSAndrew Thompson static void
lacp_fill_aggregator_id_peer(struct lacp_peerinfo * lpi_aggr,const struct lacp_peerinfo * lpi_port)1354b47888ceSAndrew Thompson lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1355b47888ceSAndrew Thompson     const struct lacp_peerinfo *lpi_port)
1356b47888ceSAndrew Thompson {
1357b47888ceSAndrew Thompson 	memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1358b47888ceSAndrew Thompson 	lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1359b47888ceSAndrew Thompson 	lpi_aggr->lip_key = lpi_port->lip_key;
1360b47888ceSAndrew Thompson }
1361b47888ceSAndrew Thompson 
1362b47888ceSAndrew Thompson /*
1363b47888ceSAndrew Thompson  * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1364b47888ceSAndrew Thompson  */
1365b47888ceSAndrew Thompson 
13665a8abd0aSZhenlei Huang static bool
lacp_aggregator_is_compatible(const struct lacp_aggregator * la,const struct lacp_port * lp)1367b47888ceSAndrew Thompson lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1368b47888ceSAndrew Thompson     const struct lacp_port *lp)
1369b47888ceSAndrew Thompson {
1370b47888ceSAndrew Thompson 	if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1371b47888ceSAndrew Thompson 	    !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
13725a8abd0aSZhenlei Huang 		return (false);
1373b47888ceSAndrew Thompson 	}
1374b47888ceSAndrew Thompson 
13755a8abd0aSZhenlei Huang 	if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION))
13765a8abd0aSZhenlei Huang 		return (false);
13775a8abd0aSZhenlei Huang 
13785a8abd0aSZhenlei Huang 	if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner))
13795a8abd0aSZhenlei Huang 		return (false);
13805a8abd0aSZhenlei Huang 
13815a8abd0aSZhenlei Huang 	if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor))
13825a8abd0aSZhenlei Huang 		return (false);
13835a8abd0aSZhenlei Huang 
13845a8abd0aSZhenlei Huang 	return (true);
1385b47888ceSAndrew Thompson }
1386b47888ceSAndrew Thompson 
13875a8abd0aSZhenlei Huang static bool
lacp_peerinfo_is_compatible(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b)1388b47888ceSAndrew Thompson lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1389b47888ceSAndrew Thompson     const struct lacp_peerinfo *b)
1390b47888ceSAndrew Thompson {
1391b47888ceSAndrew Thompson 	if (memcmp(&a->lip_systemid, &b->lip_systemid,
13925a8abd0aSZhenlei Huang 	    sizeof(a->lip_systemid)) != 0) {
13935a8abd0aSZhenlei Huang 		return (false);
1394b47888ceSAndrew Thompson 	}
1395b47888ceSAndrew Thompson 
13965a8abd0aSZhenlei Huang 	if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key)) != 0)
13975a8abd0aSZhenlei Huang 		return (false);
1398b47888ceSAndrew Thompson 
13995a8abd0aSZhenlei Huang 	return (true);
1400b47888ceSAndrew Thompson }
1401b47888ceSAndrew Thompson 
1402b47888ceSAndrew Thompson static void
lacp_port_enable(struct lacp_port * lp)1403b47888ceSAndrew Thompson lacp_port_enable(struct lacp_port *lp)
1404b47888ceSAndrew Thompson {
1405b47888ceSAndrew Thompson 	lp->lp_state |= LACP_STATE_AGGREGATION;
1406b47888ceSAndrew Thompson }
1407b47888ceSAndrew Thompson 
1408b47888ceSAndrew Thompson static void
lacp_port_disable(struct lacp_port * lp)1409b47888ceSAndrew Thompson lacp_port_disable(struct lacp_port *lp)
1410b47888ceSAndrew Thompson {
1411b47888ceSAndrew Thompson 	lacp_set_mux(lp, LACP_MUX_DETACHED);
1412b47888ceSAndrew Thompson 
1413b47888ceSAndrew Thompson 	lp->lp_state &= ~LACP_STATE_AGGREGATION;
1414b47888ceSAndrew Thompson 	lp->lp_selected = LACP_UNSELECTED;
1415b47888ceSAndrew Thompson 	lacp_sm_rx_record_default(lp);
1416b47888ceSAndrew Thompson 	lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1417b47888ceSAndrew Thompson 	lp->lp_state &= ~LACP_STATE_EXPIRED;
1418b47888ceSAndrew Thompson }
1419b47888ceSAndrew Thompson 
1420b47888ceSAndrew Thompson /*
1421b47888ceSAndrew Thompson  * lacp_select: select an aggregator.  create one if necessary.
1422b47888ceSAndrew Thompson  */
1423b47888ceSAndrew Thompson static void
lacp_select(struct lacp_port * lp)1424b47888ceSAndrew Thompson lacp_select(struct lacp_port *lp)
1425b47888ceSAndrew Thompson {
1426b47888ceSAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
1427b47888ceSAndrew Thompson 	struct lacp_aggregator *la;
1428b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
1429b47888ceSAndrew Thompson 
1430b47888ceSAndrew Thompson 	if (lp->lp_aggregator) {
1431b47888ceSAndrew Thompson 		return;
1432b47888ceSAndrew Thompson 	}
1433b47888ceSAndrew Thompson 
1434e8003900SJonathan T. Looney 	/* If we haven't heard from our peer, skip this step. */
1435e8003900SJonathan T. Looney 	if (lp->lp_state & LACP_STATE_DEFAULTED)
1436e8003900SJonathan T. Looney 		return;
1437e8003900SJonathan T. Looney 
1438b47888ceSAndrew Thompson 	KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1439b47888ceSAndrew Thompson 	    ("timer_wait_while still active"));
1440b47888ceSAndrew Thompson 
1441b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "port lagid=%s\n",
1442b47888ceSAndrew Thompson 	    lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1443b47888ceSAndrew Thompson 	    buf, sizeof(buf))));
1444b47888ceSAndrew Thompson 
1445b47888ceSAndrew Thompson 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1446b47888ceSAndrew Thompson 		if (lacp_aggregator_is_compatible(la, lp)) {
1447b47888ceSAndrew Thompson 			break;
1448b47888ceSAndrew Thompson 		}
1449b47888ceSAndrew Thompson 	}
1450b47888ceSAndrew Thompson 
1451b47888ceSAndrew Thompson 	if (la == NULL) {
1452b47888ceSAndrew Thompson 		la = lacp_aggregator_get(lsc, lp);
1453b47888ceSAndrew Thompson 		if (la == NULL) {
1454b47888ceSAndrew Thompson 			LACP_DPRINTF((lp, "aggregator creation failed\n"));
1455b47888ceSAndrew Thompson 
1456b47888ceSAndrew Thompson 			/*
1457b47888ceSAndrew Thompson 			 * will retry on the next tick.
1458b47888ceSAndrew Thompson 			 */
1459b47888ceSAndrew Thompson 
1460b47888ceSAndrew Thompson 			return;
1461b47888ceSAndrew Thompson 		}
1462b47888ceSAndrew Thompson 		lacp_fill_aggregator_id(la, lp);
1463b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "aggregator created\n"));
1464b47888ceSAndrew Thompson 	} else {
1465b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "compatible aggregator found\n"));
14663de18008SAndrew Thompson 		if (la->la_refcnt == LACP_MAX_PORTS)
14673de18008SAndrew Thompson 			return;
1468b47888ceSAndrew Thompson 		lacp_aggregator_addref(lsc, la);
1469b47888ceSAndrew Thompson 	}
1470b47888ceSAndrew Thompson 
1471b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1472b47888ceSAndrew Thompson 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1473b47888ceSAndrew Thompson 	    buf, sizeof(buf))));
1474b47888ceSAndrew Thompson 
1475b47888ceSAndrew Thompson 	lp->lp_aggregator = la;
1476b47888ceSAndrew Thompson 	lp->lp_selected = LACP_SELECTED;
1477b47888ceSAndrew Thompson }
1478b47888ceSAndrew Thompson 
1479b47888ceSAndrew Thompson /*
1480b47888ceSAndrew Thompson  * lacp_unselect: finish unselect/detach process.
1481b47888ceSAndrew Thompson  */
1482b47888ceSAndrew Thompson 
1483b47888ceSAndrew Thompson static void
lacp_unselect(struct lacp_port * lp)1484b47888ceSAndrew Thompson lacp_unselect(struct lacp_port *lp)
1485b47888ceSAndrew Thompson {
1486b47888ceSAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
1487b47888ceSAndrew Thompson 	struct lacp_aggregator *la = lp->lp_aggregator;
1488b47888ceSAndrew Thompson 
1489b47888ceSAndrew Thompson 	KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1490b47888ceSAndrew Thompson 	    ("timer_wait_while still active"));
1491b47888ceSAndrew Thompson 
1492b47888ceSAndrew Thompson 	if (la == NULL) {
1493b47888ceSAndrew Thompson 		return;
1494b47888ceSAndrew Thompson 	}
1495b47888ceSAndrew Thompson 
1496b47888ceSAndrew Thompson 	lp->lp_aggregator = NULL;
1497b47888ceSAndrew Thompson 	lacp_aggregator_delref(lsc, la);
1498b47888ceSAndrew Thompson }
1499b47888ceSAndrew Thompson 
1500b47888ceSAndrew Thompson /* mux machine */
1501b47888ceSAndrew Thompson 
1502b47888ceSAndrew Thompson static void
lacp_sm_mux(struct lacp_port * lp)1503b47888ceSAndrew Thompson lacp_sm_mux(struct lacp_port *lp)
1504b47888ceSAndrew Thompson {
150531402c27SAdrian Chadd 	struct lagg_port *lgp = lp->lp_lagg;
150631402c27SAdrian Chadd 	struct lagg_softc *sc = lgp->lp_softc;
1507b47888ceSAndrew Thompson 	enum lacp_mux_state new_state;
1508b47888ceSAndrew Thompson 	boolean_t p_sync =
1509b47888ceSAndrew Thompson 		    (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1510b47888ceSAndrew Thompson 	boolean_t p_collecting =
1511b47888ceSAndrew Thompson 	    (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1512b47888ceSAndrew Thompson 	enum lacp_selected selected = lp->lp_selected;
1513b47888ceSAndrew Thompson 	struct lacp_aggregator *la;
1514b47888ceSAndrew Thompson 
1515939a050aSHiroki Sato 	if (V_lacp_debug > 1)
151631402c27SAdrian Chadd 		lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
151731402c27SAdrian Chadd 		    "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
151831402c27SAdrian Chadd 		    lp->lp_mux_state, selected, p_sync, p_collecting);
1519b47888ceSAndrew Thompson 
1520b47888ceSAndrew Thompson re_eval:
1521b47888ceSAndrew Thompson 	la = lp->lp_aggregator;
1522b47888ceSAndrew Thompson 	KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1523b47888ceSAndrew Thompson 	    ("MUX not detached"));
1524b47888ceSAndrew Thompson 	new_state = lp->lp_mux_state;
1525b47888ceSAndrew Thompson 	switch (lp->lp_mux_state) {
1526b47888ceSAndrew Thompson 	case LACP_MUX_DETACHED:
1527b47888ceSAndrew Thompson 		if (selected != LACP_UNSELECTED) {
1528b47888ceSAndrew Thompson 			new_state = LACP_MUX_WAITING;
1529b47888ceSAndrew Thompson 		}
1530b47888ceSAndrew Thompson 		break;
1531b47888ceSAndrew Thompson 	case LACP_MUX_WAITING:
1532b47888ceSAndrew Thompson 		KASSERT(la->la_pending > 0 ||
1533b47888ceSAndrew Thompson 		    !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1534b47888ceSAndrew Thompson 		    ("timer_wait_while still active"));
1535b47888ceSAndrew Thompson 		if (selected == LACP_SELECTED && la->la_pending == 0) {
1536b47888ceSAndrew Thompson 			new_state = LACP_MUX_ATTACHED;
1537b47888ceSAndrew Thompson 		} else if (selected == LACP_UNSELECTED) {
1538b47888ceSAndrew Thompson 			new_state = LACP_MUX_DETACHED;
1539b47888ceSAndrew Thompson 		}
1540b47888ceSAndrew Thompson 		break;
1541b47888ceSAndrew Thompson 	case LACP_MUX_ATTACHED:
1542b47888ceSAndrew Thompson 		if (selected == LACP_SELECTED && p_sync) {
1543b47888ceSAndrew Thompson 			new_state = LACP_MUX_COLLECTING;
1544b47888ceSAndrew Thompson 		} else if (selected != LACP_SELECTED) {
1545b47888ceSAndrew Thompson 			new_state = LACP_MUX_DETACHED;
1546b47888ceSAndrew Thompson 		}
1547b47888ceSAndrew Thompson 		break;
1548b47888ceSAndrew Thompson 	case LACP_MUX_COLLECTING:
1549b47888ceSAndrew Thompson 		if (selected == LACP_SELECTED && p_sync && p_collecting) {
1550b47888ceSAndrew Thompson 			new_state = LACP_MUX_DISTRIBUTING;
1551b47888ceSAndrew Thompson 		} else if (selected != LACP_SELECTED || !p_sync) {
1552b47888ceSAndrew Thompson 			new_state = LACP_MUX_ATTACHED;
1553b47888ceSAndrew Thompson 		}
1554b47888ceSAndrew Thompson 		break;
1555b47888ceSAndrew Thompson 	case LACP_MUX_DISTRIBUTING:
1556b47888ceSAndrew Thompson 		if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1557b47888ceSAndrew Thompson 			new_state = LACP_MUX_COLLECTING;
1558387e754aSAdrian Chadd 			lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
155931402c27SAdrian Chadd 			sc->sc_flapping++;
1560b47888ceSAndrew Thompson 		}
1561b47888ceSAndrew Thompson 		break;
1562b47888ceSAndrew Thompson 	default:
1563b47888ceSAndrew Thompson 		panic("%s: unknown state", __func__);
1564b47888ceSAndrew Thompson 	}
1565b47888ceSAndrew Thompson 
1566b47888ceSAndrew Thompson 	if (lp->lp_mux_state == new_state) {
1567b47888ceSAndrew Thompson 		return;
1568b47888ceSAndrew Thompson 	}
1569b47888ceSAndrew Thompson 
1570b47888ceSAndrew Thompson 	lacp_set_mux(lp, new_state);
1571b47888ceSAndrew Thompson 	goto re_eval;
1572b47888ceSAndrew Thompson }
1573b47888ceSAndrew Thompson 
1574b47888ceSAndrew Thompson static void
lacp_set_mux(struct lacp_port * lp,enum lacp_mux_state new_state)1575b47888ceSAndrew Thompson lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1576b47888ceSAndrew Thompson {
1577b47888ceSAndrew Thompson 	struct lacp_aggregator *la = lp->lp_aggregator;
1578b47888ceSAndrew Thompson 
1579b47888ceSAndrew Thompson 	if (lp->lp_mux_state == new_state) {
1580b47888ceSAndrew Thompson 		return;
1581b47888ceSAndrew Thompson 	}
1582b47888ceSAndrew Thompson 
1583b47888ceSAndrew Thompson 	switch (new_state) {
1584b47888ceSAndrew Thompson 	case LACP_MUX_DETACHED:
1585b47888ceSAndrew Thompson 		lp->lp_state &= ~LACP_STATE_SYNC;
1586b47888ceSAndrew Thompson 		lacp_disable_distributing(lp);
1587b47888ceSAndrew Thompson 		lacp_disable_collecting(lp);
1588b47888ceSAndrew Thompson 		lacp_sm_assert_ntt(lp);
1589b47888ceSAndrew Thompson 		/* cancel timer */
1590b47888ceSAndrew Thompson 		if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1591b47888ceSAndrew Thompson 			KASSERT(la->la_pending > 0,
1592b47888ceSAndrew Thompson 			    ("timer_wait_while not active"));
1593b47888ceSAndrew Thompson 			la->la_pending--;
1594b47888ceSAndrew Thompson 		}
1595b47888ceSAndrew Thompson 		LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1596b47888ceSAndrew Thompson 		lacp_unselect(lp);
1597b47888ceSAndrew Thompson 		break;
1598b47888ceSAndrew Thompson 	case LACP_MUX_WAITING:
1599b47888ceSAndrew Thompson 		LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1600b47888ceSAndrew Thompson 		    LACP_AGGREGATE_WAIT_TIME);
1601b47888ceSAndrew Thompson 		la->la_pending++;
1602b47888ceSAndrew Thompson 		break;
1603b47888ceSAndrew Thompson 	case LACP_MUX_ATTACHED:
1604b47888ceSAndrew Thompson 		lp->lp_state |= LACP_STATE_SYNC;
1605b47888ceSAndrew Thompson 		lacp_disable_collecting(lp);
1606b47888ceSAndrew Thompson 		lacp_sm_assert_ntt(lp);
1607b47888ceSAndrew Thompson 		break;
1608b47888ceSAndrew Thompson 	case LACP_MUX_COLLECTING:
1609b47888ceSAndrew Thompson 		lacp_enable_collecting(lp);
1610b47888ceSAndrew Thompson 		lacp_disable_distributing(lp);
1611b47888ceSAndrew Thompson 		lacp_sm_assert_ntt(lp);
1612b47888ceSAndrew Thompson 		break;
1613b47888ceSAndrew Thompson 	case LACP_MUX_DISTRIBUTING:
1614b47888ceSAndrew Thompson 		lacp_enable_distributing(lp);
1615b47888ceSAndrew Thompson 		break;
1616b47888ceSAndrew Thompson 	default:
1617b47888ceSAndrew Thompson 		panic("%s: unknown state", __func__);
1618b47888ceSAndrew Thompson 	}
1619b47888ceSAndrew Thompson 
1620b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1621b47888ceSAndrew Thompson 
1622b47888ceSAndrew Thompson 	lp->lp_mux_state = new_state;
1623b47888ceSAndrew Thompson }
1624b47888ceSAndrew Thompson 
1625b47888ceSAndrew Thompson static void
lacp_sm_mux_timer(struct lacp_port * lp)1626b47888ceSAndrew Thompson lacp_sm_mux_timer(struct lacp_port *lp)
1627b47888ceSAndrew Thompson {
1628b47888ceSAndrew Thompson 	struct lacp_aggregator *la = lp->lp_aggregator;
1629b47888ceSAndrew Thompson 	char buf[LACP_LAGIDSTR_MAX+1];
1630b47888ceSAndrew Thompson 
1631b47888ceSAndrew Thompson 	KASSERT(la->la_pending > 0, ("no pending event"));
1632b47888ceSAndrew Thompson 
1633b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1634b47888ceSAndrew Thompson 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
1635b47888ceSAndrew Thompson 	    buf, sizeof(buf)),
1636b47888ceSAndrew Thompson 	    la->la_pending, la->la_pending - 1));
1637b47888ceSAndrew Thompson 
1638b47888ceSAndrew Thompson 	la->la_pending--;
1639b47888ceSAndrew Thompson }
1640b47888ceSAndrew Thompson 
1641b47888ceSAndrew Thompson /* periodic transmit machine */
1642b47888ceSAndrew Thompson 
1643b47888ceSAndrew Thompson static void
lacp_sm_ptx_update_timeout(struct lacp_port * lp,uint8_t oldpstate)1644b47888ceSAndrew Thompson lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1645b47888ceSAndrew Thompson {
1646b47888ceSAndrew Thompson 	if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1647b47888ceSAndrew Thompson 	    LACP_STATE_TIMEOUT)) {
1648b47888ceSAndrew Thompson 		return;
1649b47888ceSAndrew Thompson 	}
1650b47888ceSAndrew Thompson 
1651b47888ceSAndrew Thompson 	LACP_DPRINTF((lp, "partner timeout changed\n"));
1652b47888ceSAndrew Thompson 
1653b47888ceSAndrew Thompson 	/*
1654b47888ceSAndrew Thompson 	 * FAST_PERIODIC -> SLOW_PERIODIC
1655b47888ceSAndrew Thompson 	 * or
1656b47888ceSAndrew Thompson 	 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1657b47888ceSAndrew Thompson 	 *
1658b47888ceSAndrew Thompson 	 * let lacp_sm_ptx_tx_schedule to update timeout.
1659b47888ceSAndrew Thompson 	 */
1660b47888ceSAndrew Thompson 
1661b47888ceSAndrew Thompson 	LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1662b47888ceSAndrew Thompson 
1663b47888ceSAndrew Thompson 	/*
1664b47888ceSAndrew Thompson 	 * if timeout has been shortened, assert NTT.
1665b47888ceSAndrew Thompson 	 */
1666b47888ceSAndrew Thompson 
1667b47888ceSAndrew Thompson 	if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1668b47888ceSAndrew Thompson 		lacp_sm_assert_ntt(lp);
1669b47888ceSAndrew Thompson 	}
1670b47888ceSAndrew Thompson }
1671b47888ceSAndrew Thompson 
1672b47888ceSAndrew Thompson static void
lacp_sm_ptx_tx_schedule(struct lacp_port * lp)1673b47888ceSAndrew Thompson lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1674b47888ceSAndrew Thompson {
1675b47888ceSAndrew Thompson 	int timeout;
1676b47888ceSAndrew Thompson 
1677b47888ceSAndrew Thompson 	if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1678b47888ceSAndrew Thompson 	    !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1679b47888ceSAndrew Thompson 		/*
1680b47888ceSAndrew Thompson 		 * NO_PERIODIC
1681b47888ceSAndrew Thompson 		 */
1682b47888ceSAndrew Thompson 
1683b47888ceSAndrew Thompson 		LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1684b47888ceSAndrew Thompson 		return;
1685b47888ceSAndrew Thompson 	}
1686b47888ceSAndrew Thompson 
1687b47888ceSAndrew Thompson 	if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1688b47888ceSAndrew Thompson 		return;
1689b47888ceSAndrew Thompson 	}
1690b47888ceSAndrew Thompson 
1691b47888ceSAndrew Thompson 	timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1692b47888ceSAndrew Thompson 	    LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1693b47888ceSAndrew Thompson 
1694b47888ceSAndrew Thompson 	LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1695b47888ceSAndrew Thompson }
1696b47888ceSAndrew Thompson 
1697b47888ceSAndrew Thompson static void
lacp_sm_ptx_timer(struct lacp_port * lp)1698b47888ceSAndrew Thompson lacp_sm_ptx_timer(struct lacp_port *lp)
1699b47888ceSAndrew Thompson {
1700b47888ceSAndrew Thompson 	lacp_sm_assert_ntt(lp);
1701b47888ceSAndrew Thompson }
1702b47888ceSAndrew Thompson 
1703b47888ceSAndrew Thompson static void
lacp_sm_rx(struct lacp_port * lp,const struct lacpdu * du)1704b47888ceSAndrew Thompson lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1705b47888ceSAndrew Thompson {
1706b47888ceSAndrew Thompson 	int timeout;
1707b47888ceSAndrew Thompson 
1708b47888ceSAndrew Thompson 	/*
1709b47888ceSAndrew Thompson 	 * check LACP_DISABLED first
1710b47888ceSAndrew Thompson 	 */
1711b47888ceSAndrew Thompson 
1712b47888ceSAndrew Thompson 	if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1713b47888ceSAndrew Thompson 		return;
1714b47888ceSAndrew Thompson 	}
1715b47888ceSAndrew Thompson 
1716b47888ceSAndrew Thompson 	/*
1717b47888ceSAndrew Thompson 	 * check loopback condition.
1718b47888ceSAndrew Thompson 	 */
1719b47888ceSAndrew Thompson 
1720b47888ceSAndrew Thompson 	if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1721b47888ceSAndrew Thompson 	    &lp->lp_actor.lip_systemid)) {
1722b47888ceSAndrew Thompson 		return;
1723b47888ceSAndrew Thompson 	}
1724b47888ceSAndrew Thompson 
1725b47888ceSAndrew Thompson 	/*
1726b47888ceSAndrew Thompson 	 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1727b47888ceSAndrew Thompson 	 */
1728b47888ceSAndrew Thompson 
172900a80538SGreg Foster 	microuptime(&lp->lp_last_lacpdu_rx);
1730b47888ceSAndrew Thompson 	lacp_sm_rx_update_selected(lp, du);
1731b47888ceSAndrew Thompson 	lacp_sm_rx_update_ntt(lp, du);
1732b47888ceSAndrew Thompson 	lacp_sm_rx_record_pdu(lp, du);
1733b47888ceSAndrew Thompson 
1734b47888ceSAndrew Thompson 	timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1735b47888ceSAndrew Thompson 	    LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1736b47888ceSAndrew Thompson 	LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1737b47888ceSAndrew Thompson 
1738b47888ceSAndrew Thompson 	lp->lp_state &= ~LACP_STATE_EXPIRED;
1739b47888ceSAndrew Thompson 
1740b47888ceSAndrew Thompson 	/*
1741b47888ceSAndrew Thompson 	 * kick transmit machine without waiting the next tick.
1742b47888ceSAndrew Thompson 	 */
1743b47888ceSAndrew Thompson 
1744b47888ceSAndrew Thompson 	lacp_sm_tx(lp);
1745b47888ceSAndrew Thompson }
1746b47888ceSAndrew Thompson 
1747b47888ceSAndrew Thompson static void
lacp_sm_rx_set_expired(struct lacp_port * lp)1748b47888ceSAndrew Thompson lacp_sm_rx_set_expired(struct lacp_port *lp)
1749b47888ceSAndrew Thompson {
1750b47888ceSAndrew Thompson 	lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1751b47888ceSAndrew Thompson 	lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1752b47888ceSAndrew Thompson 	LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1753b47888ceSAndrew Thompson 	lp->lp_state |= LACP_STATE_EXPIRED;
1754b47888ceSAndrew Thompson }
1755b47888ceSAndrew Thompson 
1756b47888ceSAndrew Thompson static void
lacp_sm_rx_timer(struct lacp_port * lp)1757b47888ceSAndrew Thompson lacp_sm_rx_timer(struct lacp_port *lp)
1758b47888ceSAndrew Thompson {
1759b47888ceSAndrew Thompson 	if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1760b47888ceSAndrew Thompson 		/* CURRENT -> EXPIRED */
1761b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1762b47888ceSAndrew Thompson 		lacp_sm_rx_set_expired(lp);
1763b47888ceSAndrew Thompson 	} else {
1764b47888ceSAndrew Thompson 		/* EXPIRED -> DEFAULTED */
1765b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1766b47888ceSAndrew Thompson 		lacp_sm_rx_update_default_selected(lp);
1767b47888ceSAndrew Thompson 		lacp_sm_rx_record_default(lp);
1768b47888ceSAndrew Thompson 		lp->lp_state &= ~LACP_STATE_EXPIRED;
1769b47888ceSAndrew Thompson 	}
1770b47888ceSAndrew Thompson }
1771b47888ceSAndrew Thompson 
1772b47888ceSAndrew Thompson static void
lacp_sm_rx_record_pdu(struct lacp_port * lp,const struct lacpdu * du)1773b47888ceSAndrew Thompson lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1774b47888ceSAndrew Thompson {
1775b47888ceSAndrew Thompson 	boolean_t active;
1776b47888ceSAndrew Thompson 	uint8_t oldpstate;
1777b47888ceSAndrew Thompson 	char buf[LACP_STATESTR_MAX+1];
1778b47888ceSAndrew Thompson 
17795fc4c149SAndrew Thompson 	LACP_TRACE(lp);
1780b47888ceSAndrew Thompson 
1781b47888ceSAndrew Thompson 	oldpstate = lp->lp_partner.lip_state;
1782b47888ceSAndrew Thompson 
1783b47888ceSAndrew Thompson 	active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1784b47888ceSAndrew Thompson 	    || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1785b47888ceSAndrew Thompson 	    (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1786b47888ceSAndrew Thompson 
1787b47888ceSAndrew Thompson 	lp->lp_partner = du->ldu_actor;
1788b47888ceSAndrew Thompson 	if (active &&
1789b47888ceSAndrew Thompson 	    ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1790b47888ceSAndrew Thompson 	    LACP_STATE_AGGREGATION) &&
1791b47888ceSAndrew Thompson 	    !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1792b47888ceSAndrew Thompson 	    || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1793e8003900SJonathan T. Looney 		/*
1794e8003900SJonathan T. Looney 		 * XXX Maintain legacy behavior of leaving the
1795e8003900SJonathan T. Looney 		 * LACP_STATE_SYNC bit unchanged from the partner's
1796e8003900SJonathan T. Looney 		 * advertisement if lsc_strict_mode is false.
1797e8003900SJonathan T. Looney 		 * TODO: We should re-examine the concept of the "strict mode"
1798e8003900SJonathan T. Looney 		 * to ensure it makes sense to maintain a non-strict mode.
1799e8003900SJonathan T. Looney 		 */
1800e8003900SJonathan T. Looney 		if (lp->lp_lsc->lsc_strict_mode)
1801e8003900SJonathan T. Looney 			lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1802b47888ceSAndrew Thompson 	} else {
1803b47888ceSAndrew Thompson 		lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1804b47888ceSAndrew Thompson 	}
1805b47888ceSAndrew Thompson 
1806b47888ceSAndrew Thompson 	lp->lp_state &= ~LACP_STATE_DEFAULTED;
1807b47888ceSAndrew Thompson 
1808b47888ceSAndrew Thompson 	if (oldpstate != lp->lp_partner.lip_state) {
1809b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "old pstate %s\n",
1810b47888ceSAndrew Thompson 		    lacp_format_state(oldpstate, buf, sizeof(buf))));
1811b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "new pstate %s\n",
1812b47888ceSAndrew Thompson 		    lacp_format_state(lp->lp_partner.lip_state, buf,
1813b47888ceSAndrew Thompson 		    sizeof(buf))));
1814b47888ceSAndrew Thompson 	}
1815b47888ceSAndrew Thompson 
1816b47888ceSAndrew Thompson 	lacp_sm_ptx_update_timeout(lp, oldpstate);
1817b47888ceSAndrew Thompson }
1818b47888ceSAndrew Thompson 
1819b47888ceSAndrew Thompson static void
lacp_sm_rx_update_ntt(struct lacp_port * lp,const struct lacpdu * du)1820b47888ceSAndrew Thompson lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1821b47888ceSAndrew Thompson {
18225fc4c149SAndrew Thompson 
18235fc4c149SAndrew Thompson 	LACP_TRACE(lp);
1824b47888ceSAndrew Thompson 
1825b47888ceSAndrew Thompson 	if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1826b47888ceSAndrew Thompson 	    !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1827b47888ceSAndrew Thompson 	    LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1828b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1829b47888ceSAndrew Thompson 		lacp_sm_assert_ntt(lp);
1830b47888ceSAndrew Thompson 	}
1831b47888ceSAndrew Thompson }
1832b47888ceSAndrew Thompson 
1833b47888ceSAndrew Thompson static void
lacp_sm_rx_record_default(struct lacp_port * lp)1834b47888ceSAndrew Thompson lacp_sm_rx_record_default(struct lacp_port *lp)
1835b47888ceSAndrew Thompson {
1836b47888ceSAndrew Thompson 	uint8_t oldpstate;
1837b47888ceSAndrew Thompson 
18385fc4c149SAndrew Thompson 	LACP_TRACE(lp);
1839b47888ceSAndrew Thompson 
1840b47888ceSAndrew Thompson 	oldpstate = lp->lp_partner.lip_state;
184149de4f22SAdrian Chadd 	if (lp->lp_lsc->lsc_strict_mode)
184231402c27SAdrian Chadd 		lp->lp_partner = lacp_partner_admin_strict;
184331402c27SAdrian Chadd 	else
1844c1be893cSSteven Hartland 		lp->lp_partner = lacp_partner_admin_optimistic;
1845b47888ceSAndrew Thompson 	lp->lp_state |= LACP_STATE_DEFAULTED;
1846b47888ceSAndrew Thompson 	lacp_sm_ptx_update_timeout(lp, oldpstate);
1847b47888ceSAndrew Thompson }
1848b47888ceSAndrew Thompson 
1849b47888ceSAndrew Thompson static void
lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port * lp,const struct lacp_peerinfo * info)1850b47888ceSAndrew Thompson lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1851b47888ceSAndrew Thompson     const struct lacp_peerinfo *info)
1852b47888ceSAndrew Thompson {
18535fc4c149SAndrew Thompson 
18545fc4c149SAndrew Thompson 	LACP_TRACE(lp);
1855b47888ceSAndrew Thompson 
1856b47888ceSAndrew Thompson 	if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1857b47888ceSAndrew Thompson 	    !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1858b47888ceSAndrew Thompson 	    LACP_STATE_AGGREGATION)) {
1859b47888ceSAndrew Thompson 		lp->lp_selected = LACP_UNSELECTED;
1860b47888ceSAndrew Thompson 		/* mux machine will clean up lp->lp_aggregator */
1861b47888ceSAndrew Thompson 	}
1862b47888ceSAndrew Thompson }
1863b47888ceSAndrew Thompson 
1864b47888ceSAndrew Thompson static void
lacp_sm_rx_update_selected(struct lacp_port * lp,const struct lacpdu * du)1865b47888ceSAndrew Thompson lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1866b47888ceSAndrew Thompson {
18675fc4c149SAndrew Thompson 
18685fc4c149SAndrew Thompson 	LACP_TRACE(lp);
1869b47888ceSAndrew Thompson 
1870b47888ceSAndrew Thompson 	lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1871b47888ceSAndrew Thompson }
1872b47888ceSAndrew Thompson 
1873b47888ceSAndrew Thompson static void
lacp_sm_rx_update_default_selected(struct lacp_port * lp)1874b47888ceSAndrew Thompson lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1875b47888ceSAndrew Thompson {
18765fc4c149SAndrew Thompson 
18775fc4c149SAndrew Thompson 	LACP_TRACE(lp);
1878b47888ceSAndrew Thompson 
187949de4f22SAdrian Chadd 	if (lp->lp_lsc->lsc_strict_mode)
188031402c27SAdrian Chadd 		lacp_sm_rx_update_selected_from_peerinfo(lp,
188131402c27SAdrian Chadd 		    &lacp_partner_admin_strict);
188231402c27SAdrian Chadd 	else
188331402c27SAdrian Chadd 		lacp_sm_rx_update_selected_from_peerinfo(lp,
188431402c27SAdrian Chadd 		    &lacp_partner_admin_optimistic);
1885b47888ceSAndrew Thompson }
1886b47888ceSAndrew Thompson 
1887b47888ceSAndrew Thompson /* transmit machine */
1888b47888ceSAndrew Thompson 
1889b47888ceSAndrew Thompson static void
lacp_sm_tx(struct lacp_port * lp)1890b47888ceSAndrew Thompson lacp_sm_tx(struct lacp_port *lp)
1891b47888ceSAndrew Thompson {
189231402c27SAdrian Chadd 	int error = 0;
1893b47888ceSAndrew Thompson 
1894b47888ceSAndrew Thompson 	if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1895b47888ceSAndrew Thompson #if 1
1896b47888ceSAndrew Thompson 	    || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1897b47888ceSAndrew Thompson 	    && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1898b47888ceSAndrew Thompson #endif
1899b47888ceSAndrew Thompson 	    ) {
1900b47888ceSAndrew Thompson 		lp->lp_flags &= ~LACP_PORT_NTT;
1901b47888ceSAndrew Thompson 	}
1902b47888ceSAndrew Thompson 
1903b47888ceSAndrew Thompson 	if (!(lp->lp_flags & LACP_PORT_NTT)) {
1904b47888ceSAndrew Thompson 		return;
1905b47888ceSAndrew Thompson 	}
1906b47888ceSAndrew Thompson 
1907b47888ceSAndrew Thompson 	/* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1908b47888ceSAndrew Thompson 	if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1909b47888ceSAndrew Thompson 		    (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1910b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "rate limited pdu\n"));
1911b47888ceSAndrew Thompson 		return;
1912b47888ceSAndrew Thompson 	}
1913b47888ceSAndrew Thompson 
191449de4f22SAdrian Chadd 	if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1915b47888ceSAndrew Thompson 		error = lacp_xmit_lacpdu(lp);
191649de4f22SAdrian Chadd 	} else {
191731402c27SAdrian Chadd 		LACP_TPRINTF((lp, "Dropping TX PDU\n"));
191849de4f22SAdrian Chadd 	}
1919b47888ceSAndrew Thompson 
1920b47888ceSAndrew Thompson 	if (error == 0) {
1921b47888ceSAndrew Thompson 		lp->lp_flags &= ~LACP_PORT_NTT;
1922b47888ceSAndrew Thompson 	} else {
1923b47888ceSAndrew Thompson 		LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1924b47888ceSAndrew Thompson 		    error));
1925b47888ceSAndrew Thompson 	}
1926b47888ceSAndrew Thompson }
1927b47888ceSAndrew Thompson 
1928b47888ceSAndrew Thompson static void
lacp_sm_assert_ntt(struct lacp_port * lp)1929b47888ceSAndrew Thompson lacp_sm_assert_ntt(struct lacp_port *lp)
1930b47888ceSAndrew Thompson {
1931b47888ceSAndrew Thompson 
1932b47888ceSAndrew Thompson 	lp->lp_flags |= LACP_PORT_NTT;
1933b47888ceSAndrew Thompson }
1934b47888ceSAndrew Thompson 
1935b47888ceSAndrew Thompson static void
lacp_run_timers(struct lacp_port * lp)1936b47888ceSAndrew Thompson lacp_run_timers(struct lacp_port *lp)
1937b47888ceSAndrew Thompson {
1938b47888ceSAndrew Thompson 	int i;
193900a80538SGreg Foster 	struct timeval time_diff;
1940b47888ceSAndrew Thompson 
1941b47888ceSAndrew Thompson 	for (i = 0; i < LACP_NTIMER; i++) {
1942b47888ceSAndrew Thompson 		KASSERT(lp->lp_timer[i] >= 0,
1943b47888ceSAndrew Thompson 		    ("invalid timer value %d", lp->lp_timer[i]));
1944b47888ceSAndrew Thompson 		if (lp->lp_timer[i] == 0) {
1945b47888ceSAndrew Thompson 			continue;
194600a80538SGreg Foster 		} else {
194700a80538SGreg Foster 			if (i == LACP_TIMER_CURRENT_WHILE) {
194800a80538SGreg Foster 				microuptime(&time_diff);
194900a80538SGreg Foster 				timevalsub(&time_diff, &lp->lp_last_lacpdu_rx);
195000a80538SGreg Foster 				if (time_diff.tv_sec) {
195100a80538SGreg Foster 					/* At least one sec has elapsed since last LACP packet. */
195200a80538SGreg Foster 					--lp->lp_timer[i];
195300a80538SGreg Foster 				}
195400a80538SGreg Foster 			} else {
195500a80538SGreg Foster 				--lp->lp_timer[i];
195600a80538SGreg Foster 			}
195700a80538SGreg Foster 
195800a80538SGreg Foster 			if ((lp->lp_timer[i] <= 0) && (lacp_timer_funcs[i])) {
1959b47888ceSAndrew Thompson 				(*lacp_timer_funcs[i])(lp);
1960b47888ceSAndrew Thompson 			}
1961b47888ceSAndrew Thompson 		}
1962b47888ceSAndrew Thompson 	}
1963b47888ceSAndrew Thompson }
1964b47888ceSAndrew Thompson 
1965b47888ceSAndrew Thompson int
lacp_marker_input(struct lacp_port * lp,struct mbuf * m)19663de18008SAndrew Thompson lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1967b47888ceSAndrew Thompson {
1968998971a7SAndrew Thompson 	struct lacp_softc *lsc = lp->lp_lsc;
19693de18008SAndrew Thompson 	struct lagg_port *lgp = lp->lp_lagg;
19703de18008SAndrew Thompson 	struct lacp_port *lp2;
1971b47888ceSAndrew Thompson 	struct markerdu *mdu;
1972b47888ceSAndrew Thompson 	int error = 0;
1973998971a7SAndrew Thompson 	int pending = 0;
1974b47888ceSAndrew Thompson 
1975b47888ceSAndrew Thompson 	if (m->m_pkthdr.len != sizeof(*mdu)) {
1976b47888ceSAndrew Thompson 		goto bad;
1977b47888ceSAndrew Thompson 	}
1978b47888ceSAndrew Thompson 
1979b47888ceSAndrew Thompson 	if ((m->m_flags & M_MCAST) == 0) {
1980b47888ceSAndrew Thompson 		goto bad;
1981b47888ceSAndrew Thompson 	}
1982b47888ceSAndrew Thompson 
1983b47888ceSAndrew Thompson 	if (m->m_len < sizeof(*mdu)) {
1984b47888ceSAndrew Thompson 		m = m_pullup(m, sizeof(*mdu));
1985b47888ceSAndrew Thompson 		if (m == NULL) {
1986b47888ceSAndrew Thompson 			return (ENOMEM);
1987b47888ceSAndrew Thompson 		}
1988b47888ceSAndrew Thompson 	}
1989b47888ceSAndrew Thompson 
1990b47888ceSAndrew Thompson 	mdu = mtod(m, struct markerdu *);
1991b47888ceSAndrew Thompson 
1992b47888ceSAndrew Thompson 	if (memcmp(&mdu->mdu_eh.ether_dhost,
1993b47888ceSAndrew Thompson 	    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
1994b47888ceSAndrew Thompson 		goto bad;
1995b47888ceSAndrew Thompson 	}
1996b47888ceSAndrew Thompson 
1997b47888ceSAndrew Thompson 	if (mdu->mdu_sph.sph_version != 1) {
1998b47888ceSAndrew Thompson 		goto bad;
1999b47888ceSAndrew Thompson 	}
2000b47888ceSAndrew Thompson 
2001b47888ceSAndrew Thompson 	switch (mdu->mdu_tlv.tlv_type) {
2002b47888ceSAndrew Thompson 	case MARKER_TYPE_INFO:
2003b47888ceSAndrew Thompson 		if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2004b47888ceSAndrew Thompson 		    marker_info_tlv_template, TRUE)) {
2005b47888ceSAndrew Thompson 			goto bad;
2006b47888ceSAndrew Thompson 		}
2007b47888ceSAndrew Thompson 		mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
2008b47888ceSAndrew Thompson 		memcpy(&mdu->mdu_eh.ether_dhost,
2009b47888ceSAndrew Thompson 		    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
2010b47888ceSAndrew Thompson 		memcpy(&mdu->mdu_eh.ether_shost,
201118242d3bSAndrew Thompson 		    lgp->lp_lladdr, ETHER_ADDR_LEN);
201218242d3bSAndrew Thompson 		error = lagg_enqueue(lp->lp_ifp, m);
2013b47888ceSAndrew Thompson 		break;
2014b47888ceSAndrew Thompson 
2015b47888ceSAndrew Thompson 	case MARKER_TYPE_RESPONSE:
2016b47888ceSAndrew Thompson 		if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2017b47888ceSAndrew Thompson 		    marker_response_tlv_template, TRUE)) {
2018b47888ceSAndrew Thompson 			goto bad;
2019b47888ceSAndrew Thompson 		}
2020998971a7SAndrew Thompson 		LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
2021998971a7SAndrew Thompson 		    ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
2022998971a7SAndrew Thompson 		    ":", ntohl(mdu->mdu_info.mi_rq_xid)));
2023998971a7SAndrew Thompson 
2024998971a7SAndrew Thompson 		/* Verify that it is the last marker we sent out */
2025998971a7SAndrew Thompson 		if (memcmp(&mdu->mdu_info, &lp->lp_marker,
2026998971a7SAndrew Thompson 		    sizeof(struct lacp_markerinfo)))
2027998971a7SAndrew Thompson 			goto bad;
2028998971a7SAndrew Thompson 
20293de18008SAndrew Thompson 		LACP_LOCK(lsc);
2030998971a7SAndrew Thompson 		lp->lp_flags &= ~LACP_PORT_MARK;
2031998971a7SAndrew Thompson 
2032998971a7SAndrew Thompson 		if (lsc->lsc_suppress_distributing) {
2033998971a7SAndrew Thompson 			/* Check if any ports are waiting for a response */
2034998971a7SAndrew Thompson 			LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
2035998971a7SAndrew Thompson 				if (lp2->lp_flags & LACP_PORT_MARK) {
2036998971a7SAndrew Thompson 					pending = 1;
2037998971a7SAndrew Thompson 					break;
2038998971a7SAndrew Thompson 				}
2039998971a7SAndrew Thompson 			}
2040998971a7SAndrew Thompson 
2041998971a7SAndrew Thompson 			if (pending == 0) {
2042998971a7SAndrew Thompson 				/* All interface queues are clear */
2043998971a7SAndrew Thompson 				LACP_DPRINTF((NULL, "queue flush complete\n"));
2044998971a7SAndrew Thompson 				lsc->lsc_suppress_distributing = FALSE;
2045998971a7SAndrew Thompson 			}
2046998971a7SAndrew Thompson 		}
20473de18008SAndrew Thompson 		LACP_UNLOCK(lsc);
2048998971a7SAndrew Thompson 		m_freem(m);
2049998971a7SAndrew Thompson 		break;
2050998971a7SAndrew Thompson 
2051b47888ceSAndrew Thompson 	default:
2052b47888ceSAndrew Thompson 		goto bad;
2053b47888ceSAndrew Thompson 	}
2054b47888ceSAndrew Thompson 
2055b47888ceSAndrew Thompson 	return (error);
2056b47888ceSAndrew Thompson 
2057b47888ceSAndrew Thompson bad:
2058998971a7SAndrew Thompson 	LACP_DPRINTF((lp, "bad marker frame\n"));
2059b47888ceSAndrew Thompson 	m_freem(m);
2060b47888ceSAndrew Thompson 	return (EINVAL);
2061b47888ceSAndrew Thompson }
2062b47888ceSAndrew Thompson 
2063b47888ceSAndrew Thompson static int
tlv_check(const void * p,size_t size,const struct tlvhdr * tlv,const struct tlv_template * tmpl,boolean_t check_type)2064b47888ceSAndrew Thompson tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
2065b47888ceSAndrew Thompson     const struct tlv_template *tmpl, boolean_t check_type)
2066b47888ceSAndrew Thompson {
2067b47888ceSAndrew Thompson 	while (/* CONSTCOND */ 1) {
2068b47888ceSAndrew Thompson 		if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
2069b47888ceSAndrew Thompson 			return (EINVAL);
2070b47888ceSAndrew Thompson 		}
2071b47888ceSAndrew Thompson 		if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
2072b47888ceSAndrew Thompson 		    tlv->tlv_length != tmpl->tmpl_length) {
2073b47888ceSAndrew Thompson 			return (EINVAL);
2074b47888ceSAndrew Thompson 		}
2075b47888ceSAndrew Thompson 		if (tmpl->tmpl_type == 0) {
2076b47888ceSAndrew Thompson 			break;
2077b47888ceSAndrew Thompson 		}
2078b47888ceSAndrew Thompson 		tlv = (const struct tlvhdr *)
2079b47888ceSAndrew Thompson 		    ((const char *)tlv + tlv->tlv_length);
2080b47888ceSAndrew Thompson 		tmpl++;
2081b47888ceSAndrew Thompson 	}
2082b47888ceSAndrew Thompson 
2083b47888ceSAndrew Thompson 	return (0);
2084b47888ceSAndrew Thompson }
2085b47888ceSAndrew Thompson 
20865fc4c149SAndrew Thompson /* Debugging */
2087b47888ceSAndrew Thompson const char *
lacp_format_mac(const uint8_t * mac,char * buf,size_t buflen)2088b47888ceSAndrew Thompson lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
2089b47888ceSAndrew Thompson {
2090b47888ceSAndrew Thompson 	snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2091b47888ceSAndrew Thompson 	    (int)mac[0],
2092b47888ceSAndrew Thompson 	    (int)mac[1],
2093b47888ceSAndrew Thompson 	    (int)mac[2],
2094b47888ceSAndrew Thompson 	    (int)mac[3],
2095b47888ceSAndrew Thompson 	    (int)mac[4],
2096b47888ceSAndrew Thompson 	    (int)mac[5]);
2097b47888ceSAndrew Thompson 
2098b47888ceSAndrew Thompson 	return (buf);
2099b47888ceSAndrew Thompson }
2100b47888ceSAndrew Thompson 
2101b47888ceSAndrew Thompson const char *
lacp_format_systemid(const struct lacp_systemid * sysid,char * buf,size_t buflen)2102b47888ceSAndrew Thompson lacp_format_systemid(const struct lacp_systemid *sysid,
2103b47888ceSAndrew Thompson     char *buf, size_t buflen)
2104b47888ceSAndrew Thompson {
2105b47888ceSAndrew Thompson 	char macbuf[LACP_MACSTR_MAX+1];
2106b47888ceSAndrew Thompson 
2107b47888ceSAndrew Thompson 	snprintf(buf, buflen, "%04X,%s",
2108b47888ceSAndrew Thompson 	    ntohs(sysid->lsi_prio),
2109b47888ceSAndrew Thompson 	    lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2110b47888ceSAndrew Thompson 
2111b47888ceSAndrew Thompson 	return (buf);
2112b47888ceSAndrew Thompson }
2113b47888ceSAndrew Thompson 
2114b47888ceSAndrew Thompson const char *
lacp_format_portid(const struct lacp_portid * portid,char * buf,size_t buflen)2115b47888ceSAndrew Thompson lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2116b47888ceSAndrew Thompson {
2117b47888ceSAndrew Thompson 	snprintf(buf, buflen, "%04X,%04X",
2118b47888ceSAndrew Thompson 	    ntohs(portid->lpi_prio),
2119b47888ceSAndrew Thompson 	    ntohs(portid->lpi_portno));
2120b47888ceSAndrew Thompson 
2121b47888ceSAndrew Thompson 	return (buf);
2122b47888ceSAndrew Thompson }
2123b47888ceSAndrew Thompson 
2124b47888ceSAndrew Thompson const char *
lacp_format_partner(const struct lacp_peerinfo * peer,char * buf,size_t buflen)2125b47888ceSAndrew Thompson lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2126b47888ceSAndrew Thompson {
2127b47888ceSAndrew Thompson 	char sysid[LACP_SYSTEMIDSTR_MAX+1];
2128b47888ceSAndrew Thompson 	char portid[LACP_PORTIDSTR_MAX+1];
2129b47888ceSAndrew Thompson 
2130b47888ceSAndrew Thompson 	snprintf(buf, buflen, "(%s,%04X,%s)",
2131b47888ceSAndrew Thompson 	    lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2132b47888ceSAndrew Thompson 	    ntohs(peer->lip_key),
2133b47888ceSAndrew Thompson 	    lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2134b47888ceSAndrew Thompson 
2135b47888ceSAndrew Thompson 	return (buf);
2136b47888ceSAndrew Thompson }
2137b47888ceSAndrew Thompson 
2138b47888ceSAndrew Thompson const char *
lacp_format_lagid(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b,char * buf,size_t buflen)2139b47888ceSAndrew Thompson lacp_format_lagid(const struct lacp_peerinfo *a,
2140b47888ceSAndrew Thompson     const struct lacp_peerinfo *b, char *buf, size_t buflen)
2141b47888ceSAndrew Thompson {
2142b47888ceSAndrew Thompson 	char astr[LACP_PARTNERSTR_MAX+1];
2143b47888ceSAndrew Thompson 	char bstr[LACP_PARTNERSTR_MAX+1];
2144b47888ceSAndrew Thompson 
2145b47888ceSAndrew Thompson #if 0
2146b47888ceSAndrew Thompson 	/*
2147b47888ceSAndrew Thompson 	 * there's a convention to display small numbered peer
2148b47888ceSAndrew Thompson 	 * in the left.
2149b47888ceSAndrew Thompson 	 */
2150b47888ceSAndrew Thompson 
2151b47888ceSAndrew Thompson 	if (lacp_compare_peerinfo(a, b) > 0) {
2152b47888ceSAndrew Thompson 		const struct lacp_peerinfo *t;
2153b47888ceSAndrew Thompson 
2154b47888ceSAndrew Thompson 		t = a;
2155b47888ceSAndrew Thompson 		a = b;
2156b47888ceSAndrew Thompson 		b = t;
2157b47888ceSAndrew Thompson 	}
2158b47888ceSAndrew Thompson #endif
2159b47888ceSAndrew Thompson 
2160b47888ceSAndrew Thompson 	snprintf(buf, buflen, "[%s,%s]",
2161b47888ceSAndrew Thompson 	    lacp_format_partner(a, astr, sizeof(astr)),
2162b47888ceSAndrew Thompson 	    lacp_format_partner(b, bstr, sizeof(bstr)));
2163b47888ceSAndrew Thompson 
2164b47888ceSAndrew Thompson 	return (buf);
2165b47888ceSAndrew Thompson }
2166b47888ceSAndrew Thompson 
2167b47888ceSAndrew Thompson const char *
lacp_format_lagid_aggregator(const struct lacp_aggregator * la,char * buf,size_t buflen)2168b47888ceSAndrew Thompson lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2169b47888ceSAndrew Thompson     char *buf, size_t buflen)
2170b47888ceSAndrew Thompson {
2171b47888ceSAndrew Thompson 	if (la == NULL) {
2172b47888ceSAndrew Thompson 		return ("(none)");
2173b47888ceSAndrew Thompson 	}
2174b47888ceSAndrew Thompson 
2175b47888ceSAndrew Thompson 	return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2176b47888ceSAndrew Thompson }
2177b47888ceSAndrew Thompson 
2178b47888ceSAndrew Thompson const char *
lacp_format_state(uint8_t state,char * buf,size_t buflen)2179b47888ceSAndrew Thompson lacp_format_state(uint8_t state, char *buf, size_t buflen)
2180b47888ceSAndrew Thompson {
2181b47888ceSAndrew Thompson 	snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2182b47888ceSAndrew Thompson 	return (buf);
2183b47888ceSAndrew Thompson }
2184b47888ceSAndrew Thompson 
2185b47888ceSAndrew Thompson static void
lacp_dump_lacpdu(const struct lacpdu * du)2186b47888ceSAndrew Thompson lacp_dump_lacpdu(const struct lacpdu *du)
2187b47888ceSAndrew Thompson {
2188b47888ceSAndrew Thompson 	char buf[LACP_PARTNERSTR_MAX+1];
2189b47888ceSAndrew Thompson 	char buf2[LACP_STATESTR_MAX+1];
2190b47888ceSAndrew Thompson 
2191b47888ceSAndrew Thompson 	printf("actor=%s\n",
2192b47888ceSAndrew Thompson 	    lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2193b47888ceSAndrew Thompson 	printf("actor.state=%s\n",
2194b47888ceSAndrew Thompson 	    lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2195b47888ceSAndrew Thompson 	printf("partner=%s\n",
2196b47888ceSAndrew Thompson 	    lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2197b47888ceSAndrew Thompson 	printf("partner.state=%s\n",
2198b47888ceSAndrew Thompson 	    lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2199b47888ceSAndrew Thompson 
2200b47888ceSAndrew Thompson 	printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2201b47888ceSAndrew Thompson }
2202b47888ceSAndrew Thompson 
2203b47888ceSAndrew Thompson static void
lacp_dprintf(const struct lacp_port * lp,const char * fmt,...)2204b47888ceSAndrew Thompson lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2205b47888ceSAndrew Thompson {
2206b47888ceSAndrew Thompson 	va_list va;
2207b47888ceSAndrew Thompson 
2208b47888ceSAndrew Thompson 	if (lp) {
2209b47888ceSAndrew Thompson 		printf("%s: ", lp->lp_ifp->if_xname);
2210b47888ceSAndrew Thompson 	}
2211b47888ceSAndrew Thompson 
2212b47888ceSAndrew Thompson 	va_start(va, fmt);
2213b47888ceSAndrew Thompson 	vprintf(fmt, va);
2214b47888ceSAndrew Thompson 	va_end(va);
2215b47888ceSAndrew Thompson }
2216