Home | History | Annotate | Line # | Download | only in lagg
if_lagg_lacp.c revision 1.37
      1 /*	$NetBSD: if_lagg_lacp.c,v 1.37 2024/04/04 09:19:42 yamaguchi Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
      5  *
      6  * Copyright (c)2005 YAMAMOTO Takashi,
      7  * Copyright (c)2008 Andrew Thompson <thompsa (at) FreeBSD.org>
      8  * Copyright (c)2021 Internet Initiative Japan, Inc.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_lagg_lacp.c,v 1.37 2024/04/04 09:19:42 yamaguchi Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_lagg.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/types.h>
     42 
     43 #include <sys/evcnt.h>
     44 #include <sys/kmem.h>
     45 #include <sys/pcq.h>
     46 #include <sys/pslist.h>
     47 #include <sys/sysctl.h>
     48 #include <sys/syslog.h>
     49 #include <sys/workqueue.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_dl.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_media.h>
     55 
     56 #include <net/ether_slowprotocols.h>
     57 
     58 #include <net/lagg/if_lagg.h>
     59 #include <net/lagg/if_laggproto.h>
     60 #include <net/lagg/if_lagg_lacp.h>
     61 
     62 #define LACP_SYSTEMIDSTR_LEN	32
     63 
     64 enum {
     65 	LACP_TIMER_CURRENT_WHILE = 0,
     66 	LACP_TIMER_PERIODIC,
     67 	LACP_TIMER_WAIT_WHILE,
     68 	LACP_NTIMER
     69 };
     70 
     71 enum {
     72 	LACP_PTIMER_DISTRIBUTING = 0,
     73 	LACP_NPTIMER
     74 };
     75 
     76 enum lacp_selected {
     77 	LACP_UNSELECTED,
     78 	LACP_READY,
     79 	LACP_STANDBY,
     80 	LACP_SELECTED,
     81 };
     82 
     83 enum lacp_mux_state {
     84 	LACP_MUX_DETACHED,
     85 	LACP_MUX_WAITING,
     86 	LACP_MUX_STANDBY,
     87 	LACP_MUX_ATTACHED,
     88 	LACP_MUX_COLLECTING,
     89 	LACP_MUX_DISTRIBUTING,
     90 	LACP_MUX_INIT,
     91 };
     92 
     93 struct lacp_aggregator_systemid {
     94 	uint16_t	 sid_prio;
     95 	uint16_t	 sid_key;
     96 	uint8_t		 sid_mac[LACP_MAC_LEN];
     97 };
     98 
     99 struct lacp_aggregator {
    100 	TAILQ_ENTRY(lacp_aggregator)
    101 			 la_q;
    102 	LIST_HEAD(, lacp_port)
    103 			 la_ports;
    104 	ssize_t		 la_attached_port;
    105 
    106 	struct lacp_aggregator_systemid
    107 			 la_sid;
    108 };
    109 
    110 struct lacp_portinfo {
    111 	uint8_t		 lpi_state;
    112 	uint16_t	 lpi_portno;
    113 #define LACP_PORTNO_NONE	0
    114 	uint16_t	 lpi_portprio;
    115 };
    116 
    117 struct lacp_port {
    118 	struct lagg_port	*lp_laggport;
    119 	bool			 lp_added_multi;
    120 	int			 lp_timer[LACP_NTIMER];
    121 	uint32_t		 lp_marker_xid;
    122 	enum lacp_selected	 lp_selected;
    123 	enum lacp_mux_state	 lp_mux_state;
    124 
    125 	struct lacp_portinfo	 lp_actor;
    126 	struct lacp_portinfo	 lp_partner;
    127 	struct lacp_aggregator	*lp_aggregator;
    128 	struct lacp_aggregator_systemid
    129 				 lp_aggregator_sidbuf;
    130 	uint64_t		 lp_linkspeed;
    131 	int			 lp_pending;
    132 	LIST_ENTRY(lacp_port)	 lp_entry_la;
    133 	struct timeval		 lp_last_lacpdu;
    134 	int			 lp_lacpdu_sent;
    135 	bool			 lp_collector;
    136 
    137 	unsigned int		 lp_flags;
    138 #define LACP_PORT_NTT		__BIT(0)
    139 #define LACP_PORT_MARK		__BIT(1)
    140 
    141 	struct lagg_work	 lp_work_smtx;
    142 	struct lagg_work	 lp_work_marker;
    143 };
    144 
    145 struct lacp_portmap {
    146 	size_t			 pm_count;
    147 	struct lagg_port	*pm_ports[LACP_MAX_PORTS];
    148 };
    149 
    150 struct lacp_softc {
    151 	struct lagg_softc	*lsc_softc;
    152 	kmutex_t		 lsc_lock;
    153 	pserialize_t		 lsc_psz;
    154 	bool			 lsc_running;
    155 	bool			 lsc_suppress_distributing;
    156 	int			 lsc_timer[LACP_NPTIMER];
    157 	uint8_t			 lsc_system_mac[LACP_MAC_LEN];
    158 	uint16_t		 lsc_system_prio;
    159 	uint16_t		 lsc_key;
    160 	size_t			 lsc_max_ports;
    161 	size_t			 lsc_activemap;
    162 	struct lacp_portmap	 lsc_portmaps[2];	/* active & idle */
    163 	struct lacp_aggregator	*lsc_aggregator;
    164 	TAILQ_HEAD(, lacp_aggregator)
    165 				 lsc_aggregators;
    166 	struct workqueue	*lsc_workq;
    167 	struct lagg_work	 lsc_work_tick;
    168 	struct lagg_work	 lsc_work_rcvdu;
    169 	struct lagg_work	 lsc_work_linkspeed;
    170 	callout_t		 lsc_tick;
    171 	pcq_t			*lsc_du_q;
    172 
    173 	char			 lsc_evgroup[32];
    174 	struct evcnt		 lsc_mgethdr_failed;
    175 	struct evcnt		 lsc_mpullup_failed;
    176 	struct evcnt		 lsc_badlacpdu;
    177 	struct evcnt		 lsc_badmarkerdu;
    178 	struct evcnt		 lsc_norcvif;
    179 	struct evcnt		 lsc_nolaggport;
    180 	struct evcnt		 lsc_duq_nospc;
    181 
    182 	bool			 lsc_optimistic;
    183 	bool			 lsc_stop_lacpdu;
    184 	bool			 lsc_dump_du;
    185 	bool			 lsc_multi_linkspeed;
    186 };
    187 
    188 /*
    189  * Locking notes:
    190  * - Items in struct lacp_softc are protected by
    191  *   lsc_lock (an adaptive mutex)
    192  * - lsc_activemap is protected by pserialize (lsc_psz)
    193  * - Items of struct lagg_port in lsc_portmaps are protected by
    194  *   protected by both pserialize (lsc_psz) and psref (lp_psref)
    195  * - Updates for lsc_activemap and lsc_portmaps is serialized by
    196  *   sc_lock in struct lagg_softc
    197  * - Other locking notes are described in if_laggproto.h
    198  */
    199 
    200 static void	lacp_dprintf(const struct lacp_softc *,
    201 		    const struct lacp_port *, const char *, ...)
    202 		    __attribute__((__format__(__printf__, 3, 4)));
    203 
    204 #ifdef LACP_DEBUG
    205 #define LACP_DPRINTF(a)	do { lacp_dprintf a; } while (/*CONSTCOND*/ 0)
    206 #define LACP_PEERINFO_IDSTR(_pi, _b, _bs)			\
    207 	lacp_peerinfo_idstr(_pi, _b, _bs)
    208 #define LACP_STATE_STR(_s, _b, _bs)		lacp_state_str(_s, _b, _bs)
    209 #define LACP_AGGREGATOR_STR(_a, _b, _bs)			\
    210 	lacp_aggregator_str(_a, _b, _bs)
    211 #define __LACPDEBUGUSED
    212 #else
    213 #define LACP_DPRINTF(a)				__nothing
    214 #define LACP_PEERINFO_IDSTR(_pi, _b, _bs)	__nothing
    215 #define LACP_STATE_STR(_s, _b, _bs)		__nothing
    216 #define LACP_AGGREGATOR_STR(_a, _b, _bs)	__nothing
    217 #define __LACPDEBUGUSED __unused
    218 #endif
    219 
    220 #define LACP_LOCK(_sc)		mutex_enter(&(_sc)->lsc_lock)
    221 #define LACP_UNLOCK(_sc)	mutex_exit(&(_sc)->lsc_lock)
    222 #define LACP_LOCKED(_sc)	mutex_owned(&(_sc)->lsc_lock)
    223 #define LACP_TIMER_ARM(_lacpp, _timer, _val)		\
    224 	(_lacpp)->lp_timer[(_timer)] = (_val)
    225 #define LACP_TIMER_DISARM(_lacpp, _timer)		\
    226 	LACP_TIMER_ARM((_lacpp), (_timer), 0)
    227 #define LACP_TIMER_ISARMED(_lacpp, _timer)		\
    228 	((_lacpp)->lp_timer[(_timer)] > 0)
    229 #define LACP_PTIMER_ARM(_sc, _timer, _val)		\
    230 	(_sc)->lsc_timer[(_timer)] = (_val)
    231 #define LACP_PTIMER_DISARM(_sc, _timer)			\
    232 	LACP_PTIMER_ARM((_sc), (_timer), 0)
    233 #define LACP_PTIMER_ISARMED(_sc, _timer)		\
    234 	((_sc)->lsc_timer[(_timer)] > 0)
    235 #define LACP_STATE_EQ(_s1, _s2, _mask)	(!ISSET((_s1) ^ (_s2), (_mask)))
    236 #define LACP_PORT_XNAME(_lacpp)	(_lacpp != NULL) ?	\
    237 	    (_lacpp)->lp_laggport->lp_ifp->if_xname : "(unknown)"
    238 #define LACP_ISDUMPING(_sc)	(_sc)->lsc_dump_du
    239 #define LACP_PORTMAP_ACTIVE(_sc)			\
    240 	    atomic_load_consume(&(_sc)->lsc_activemap)
    241 #define LACP_PORTMAP_NEXT(_sc)				\
    242 	    (((LACP_PORTMAP_ACTIVE((_sc))) ^ 0x01) &0x01)
    243 #define LACP_SYS_PRI(_la)	ntohs((_la)->la_sid.sid_prio)
    244 #define LACP_TLV_PARSE(_du, _st, _name, _tlvlist)	\
    245 	    tlv_parse(&(_du)->_name,			\
    246 	    sizeof(_st) - offsetof(_st, _name),		\
    247 	    (_tlvlist))
    248 
    249 static void	lacp_tick(void *);
    250 static void	lacp_tick_work(struct lagg_work *, void *);
    251 static void	lacp_linkstate(struct lagg_proto_softc *, struct lagg_port *);
    252 static void	lacp_port_disable(struct lacp_softc *, struct lacp_port *);
    253 static void	lacp_port_enable(struct lacp_softc *, struct lacp_port *);
    254 static void	lacp_peerinfo_actor(struct lacp_softc *, struct lacp_port *,
    255 		    struct lacpdu_peerinfo *);
    256 static void	lacp_peerinfo_partner(struct lacp_port *,
    257 		    struct lacpdu_peerinfo *);
    258 static struct lagg_port *
    259 		lacp_select_tx_port(struct lacp_softc *, struct mbuf *,
    260 		    struct psref *);
    261 static void	lacp_suppress_distributing(struct lacp_softc *);
    262 static void	lacp_distributing_timer(struct lacp_softc *);
    263 
    264 static void	lacp_select(struct lacp_softc *, struct lacp_port *);
    265 static void	lacp_unselect(struct lacp_softc *, struct lacp_port *);
    266 static void	lacp_selected_update(struct lacp_softc *,
    267 		    struct lacp_aggregator *);
    268 static void	lacp_sm_port_init(struct lacp_softc *,
    269 		    struct lacp_port *, struct lagg_port *);
    270 static int	lacp_set_mux(struct lacp_softc *,
    271 		    struct lacp_port *, enum lacp_mux_state);
    272 static void	lacp_sm_mux(struct lacp_softc *, struct lacp_port *);
    273 static void	lacp_sm_mux_timer(struct lacp_softc *, struct lacp_port *);
    274 static void	lacp_sm_rx(struct lacp_softc *, struct lacp_port *,
    275 		    struct lacpdu_peerinfo *, struct lacpdu_peerinfo *);
    276 static void	lacp_sm_rx_set_expired(struct lacp_port *);
    277 static void	lacp_sm_rx_timer(struct lacp_softc *, struct lacp_port *);
    278 static void	lacp_sm_rx_record_default(struct lacp_softc *,
    279 		    struct lacp_port *);
    280 
    281 static void	lacp_sm_tx(struct lacp_softc *, struct lacp_port *);
    282 static void	lacp_sm_tx_work(struct lagg_work *, void *);
    283 static void	lacp_sm_ptx_timer(struct lacp_softc *, struct lacp_port *);
    284 static void	lacp_sm_ptx_schedule(struct lacp_port *);
    285 static void	lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
    286 
    287 static void	lacp_rcvdu_work(struct lagg_work *, void *);
    288 static void	lacp_marker_work(struct lagg_work *, void *);
    289 static void	lacp_linkspeed_work(struct lagg_work *, void *);
    290 static void	lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *,
    291 		    const struct lacpdu_peerinfo *,
    292 		    const struct lacpdu_collectorinfo *);
    293 static void	lacp_dump_markertlv(const struct markerdu_info *,
    294 		    const struct markerdu_info *);
    295 
    296 typedef void (*lacp_timer_func_t)(struct lacp_softc *, struct lacp_port *);
    297 static const lacp_timer_func_t	 lacp_timer_funcs[] = {
    298 	[LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
    299 	[LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
    300 	[LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
    301 };
    302 typedef void (*lacp_prototimer_func_t)(struct lacp_softc *);
    303 static const lacp_prototimer_func_t	 lacp_ptimer_funcs[] = {
    304 	[LACP_PTIMER_DISTRIBUTING] = lacp_distributing_timer,
    305 };
    306 
    307 static void
    308 lacp_dprintf(const struct lacp_softc *lsc, const struct lacp_port *lacpp,
    309     const char *fmt, ...)
    310 {
    311 	struct lagg_softc *sc;
    312 	va_list va;
    313 
    314 	if (lsc != NULL && lsc->lsc_softc != NULL) {
    315 		sc = lsc->lsc_softc;
    316 		printf("%s", sc->sc_if.if_xname);
    317 	} else {
    318 		printf("lacp");
    319 	}
    320 
    321 	if (lacpp != NULL)
    322 		printf("(%s)", LACP_PORT_XNAME(lacpp));
    323 
    324 	printf(": ");
    325 
    326 	va_start(va, fmt);
    327 	vprintf(fmt, va);
    328 	va_end(va);
    329 }
    330 
    331 static inline void
    332 lacp_evcnt_attach(struct lacp_softc *lsc,
    333     struct evcnt *ev, const char *name)
    334 {
    335 
    336 	evcnt_attach_dynamic(ev, EVCNT_TYPE_MISC, NULL,
    337 	    lsc->lsc_evgroup, name);
    338 }
    339 
    340 static inline bool
    341 lacp_iscollecting(struct lacp_port *lacpp)
    342 {
    343 
    344 	return atomic_load_relaxed(&lacpp->lp_collector);
    345 }
    346 
    347 static inline bool
    348 lacp_isdistributing(struct lacp_port *lacpp)
    349 {
    350 
    351 	return ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
    352 }
    353 
    354 static inline bool
    355 lacp_isactive(struct lacp_softc *lsc, struct lacp_port *lacpp)
    356 {
    357 
    358 	if (lacpp->lp_selected != LACP_SELECTED)
    359 		return false;
    360 
    361 	if (lacpp->lp_aggregator == NULL)
    362 		return false;
    363 
    364 	if (lacpp->lp_aggregator != lsc->lsc_aggregator)
    365 		return false;
    366 
    367 	return true;
    368 }
    369 
    370 static inline void
    371 lacp_mcastaddr(struct ifreq *ifr, const char *if_xname)
    372 {
    373 	static const uint8_t addr[ETHER_ADDR_LEN] =
    374 	    { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
    375 
    376 	memset(ifr, 0, sizeof(*ifr));
    377 
    378 	strlcpy(ifr->ifr_name, if_xname,
    379 	    sizeof(ifr->ifr_name));
    380 	ifr->ifr_addr.sa_len = sizeof(ifr->ifr_addr);
    381 	ifr->ifr_addr.sa_family = AF_UNSPEC;
    382 
    383 	CTASSERT(sizeof(ifr->ifr_addr) >= sizeof(addr));
    384 	memcpy(&ifr->ifr_addr.sa_data, addr, sizeof(addr));
    385 }
    386 
    387 static inline u_int
    388 lacp_portmap_linkstate(struct lacp_portmap *pm)
    389 {
    390 
    391 	if (pm->pm_count == 0)
    392 		return LINK_STATE_DOWN;
    393 
    394 	return LINK_STATE_UP;
    395 }
    396 
    397 static inline struct lacp_port *
    398 lacp_port_priority_max(struct lacp_port *a, struct lacp_port *b)
    399 {
    400 	uint16_t pri_a, pri_b;
    401 
    402 	pri_a = ntohs(a->lp_actor.lpi_portprio);
    403 	pri_b = ntohs(b->lp_actor.lpi_portprio);
    404 
    405 	if (pri_a < pri_b)
    406 		return a;
    407 	if (pri_b < pri_a)
    408 		return b;
    409 
    410 	pri_a = ntohs(a->lp_partner.lpi_portprio);
    411 	pri_b = ntohs(b->lp_partner.lpi_portprio);
    412 
    413 	if (pri_a < pri_b)
    414 		return a;
    415 	if (pri_b < pri_a)
    416 		return b;
    417 
    418 	if (a->lp_linkspeed > b->lp_linkspeed)
    419 		return a;
    420 	if (b->lp_linkspeed > a->lp_linkspeed)
    421 		return b;
    422 
    423 	return a;
    424 }
    425 
    426 static void
    427 tlv_parse(void *vp, size_t len, struct tlv *list)
    428 {
    429 	struct tlvhdr *th;
    430 	uint8_t *p;
    431 	size_t l, i;
    432 
    433 	th = (struct tlvhdr *)vp;
    434 	p = (uint8_t *)vp;
    435 
    436 	for (l = 0; l < len; l += th->tlv_length) {
    437 		th = (struct tlvhdr *)(p + l);
    438 
    439 		if (th->tlv_type == TLV_TYPE_TERMINATE)
    440 			break;
    441 
    442 		if (th->tlv_length <= 0)
    443 			break;
    444 
    445 		for (i = 0; list[i].tlv_t != TLV_TYPE_TERMINATE; i++) {
    446 			if (th->tlv_type != list[i].tlv_t)
    447 				continue;
    448 
    449 			if (th->tlv_length - sizeof(*th) != list[i].tlv_l)
    450 				break;
    451 
    452 			if (list[i].tlv_v == NULL) {
    453 				list[i].tlv_v =
    454 				    (void *)((uint8_t *)th + sizeof(*th));
    455 			}
    456 
    457 			break;
    458 		}
    459 	}
    460 }
    461 
    462 int
    463 lacp_attach(struct lagg_softc *sc, struct lagg_proto_softc **lscp)
    464 {
    465 	struct lacp_softc *lsc;
    466 	char xnamebuf[MAXCOMLEN];
    467 	int error;
    468 
    469 	KASSERT(LAGG_LOCKED(sc));
    470 
    471 	lsc = kmem_zalloc(sizeof(*lsc), KM_NOSLEEP);
    472 	if (lsc == NULL)
    473 		return ENOMEM;
    474 
    475 	lsc->lsc_du_q = pcq_create(LACP_RCVDU_LIMIT, KM_NOSLEEP);
    476 	if (lsc->lsc_du_q == NULL) {
    477 		error = ENOMEM;
    478 		goto free_lsc;
    479 	}
    480 
    481 	mutex_init(&lsc->lsc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    482 	lsc->lsc_softc = sc;
    483 	lsc->lsc_key = htons(if_get_index(&sc->sc_if));
    484 	lsc->lsc_system_prio = htons(LACP_SYSTEM_PRIO);
    485 	lsc->lsc_running = false;
    486 	lsc->lsc_max_ports = LACP_MAX_PORTS;
    487 	lsc->lsc_multi_linkspeed = true;
    488 	TAILQ_INIT(&lsc->lsc_aggregators);
    489 
    490 	lagg_work_set(&lsc->lsc_work_tick, lacp_tick_work, lsc);
    491 	lagg_work_set(&lsc->lsc_work_rcvdu, lacp_rcvdu_work, lsc);
    492 	lagg_work_set(&lsc->lsc_work_linkspeed,
    493 	    lacp_linkspeed_work, lsc);
    494 
    495 	snprintf(xnamebuf, sizeof(xnamebuf), "%s.lacp",
    496 	    sc->sc_if.if_xname);
    497 	lsc->lsc_workq = lagg_workq_create(xnamebuf,
    498 	    PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    499 	if (lsc->lsc_workq == NULL) {
    500 		LAGG_LOG(sc, LOG_ERR, "workqueue create failed\n");
    501 		error = ENOMEM;
    502 		goto destroy_lock;
    503 	}
    504 
    505 	lsc->lsc_psz = pserialize_create();
    506 
    507 	callout_init(&lsc->lsc_tick, CALLOUT_MPSAFE);
    508 	callout_setfunc(&lsc->lsc_tick, lacp_tick, lsc);
    509 
    510 	snprintf(lsc->lsc_evgroup, sizeof(lsc->lsc_evgroup),
    511 	    "%s-lacp", sc->sc_if.if_xname);
    512 	lacp_evcnt_attach(lsc, &lsc->lsc_mgethdr_failed, "MGETHDR failed");
    513 	lacp_evcnt_attach(lsc, &lsc->lsc_mpullup_failed, "m_pullup failed");
    514 	lacp_evcnt_attach(lsc, &lsc->lsc_badlacpdu, "Bad LACPDU received");
    515 	lacp_evcnt_attach(lsc, &lsc->lsc_badmarkerdu, "Bad MarkerDU received");
    516 	lacp_evcnt_attach(lsc, &lsc->lsc_norcvif, "No received interface");
    517 	lacp_evcnt_attach(lsc, &lsc->lsc_nolaggport, "No lagg context");
    518 	lacp_evcnt_attach(lsc, &lsc->lsc_duq_nospc, "No space left on queues");
    519 
    520 	if_link_state_change(&sc->sc_if, LINK_STATE_DOWN);
    521 
    522 	*lscp = (struct lagg_proto_softc *)lsc;
    523 	return 0;
    524 destroy_lock:
    525 	mutex_destroy(&lsc->lsc_lock);
    526 	pcq_destroy(lsc->lsc_du_q);
    527 free_lsc:
    528 	kmem_free(lsc, sizeof(*lsc));
    529 
    530 	return error;
    531 }
    532 
    533 void
    534 lacp_detach(struct lagg_proto_softc *xlsc)
    535 {
    536 	struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
    537 	struct lagg_softc *sc __diagused = lsc->lsc_softc;
    538 
    539 	KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators));
    540 	KASSERT(SIMPLEQ_EMPTY(&sc->sc_ports));
    541 
    542 	LAGG_LOCK(lsc->lsc_softc);
    543 	lacp_down(xlsc);
    544 	LAGG_UNLOCK(lsc->lsc_softc);
    545 
    546 	lagg_workq_wait(lsc->lsc_workq, &lsc->lsc_work_rcvdu);
    547 	evcnt_detach(&lsc->lsc_mgethdr_failed);
    548 	evcnt_detach(&lsc->lsc_mpullup_failed);
    549 	evcnt_detach(&lsc->lsc_badlacpdu);
    550 	evcnt_detach(&lsc->lsc_badmarkerdu);
    551 	evcnt_detach(&lsc->lsc_norcvif);
    552 	evcnt_detach(&lsc->lsc_nolaggport);
    553 	evcnt_detach(&lsc->lsc_duq_nospc);
    554 	lagg_workq_destroy(lsc->lsc_workq);
    555 	pserialize_destroy(lsc->lsc_psz);
    556 	mutex_destroy(&lsc->lsc_lock);
    557 	pcq_destroy(lsc->lsc_du_q);
    558 	kmem_free(lsc, sizeof(*lsc));
    559 }
    560 
    561 int
    562 lacp_up(struct lagg_proto_softc *xlsc)
    563 {
    564 	struct lagg_softc *sc;
    565 	struct lagg_port *lp;
    566 	struct lacp_softc *lsc;
    567 
    568 	lsc = (struct lacp_softc *)xlsc;
    569 	sc = lsc->lsc_softc;
    570 
    571 	KASSERT(LAGG_LOCKED(sc));
    572 
    573 	LACP_LOCK(lsc);
    574 	if (memcmp(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
    575 	    sizeof(lsc->lsc_system_mac)) != 0) {
    576 		memcpy(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
    577 		    sizeof(lsc->lsc_system_mac));
    578 	}
    579 	lsc->lsc_running = true;
    580 	callout_schedule(&lsc->lsc_tick, hz);
    581 	LACP_UNLOCK(lsc);
    582 
    583 	LAGG_PORTS_FOREACH(sc, lp) {
    584 		lacp_linkstate(xlsc, lp);
    585 	}
    586 
    587 	LACP_DPRINTF((lsc, NULL, "lacp start\n"));
    588 
    589 	return 0;
    590 }
    591 
    592 static void
    593 lacp_down_locked(struct lacp_softc *lsc)
    594 {
    595 	struct lagg_softc *sc;
    596 	struct lagg_port *lp;
    597 
    598 	sc = lsc->lsc_softc;
    599 
    600 	KASSERT(LAGG_LOCKED(sc));
    601 	KASSERT(LACP_LOCKED(lsc));
    602 
    603 	lsc->lsc_running = false;
    604 	callout_halt(&lsc->lsc_tick, &lsc->lsc_lock);
    605 
    606 	LAGG_PORTS_FOREACH(sc, lp) {
    607 		lacp_port_disable(lsc, lp->lp_proto_ctx);
    608 	}
    609 
    610 	memset(lsc->lsc_system_mac, 0,
    611 	    sizeof(lsc->lsc_system_mac));
    612 
    613 	LACP_DPRINTF((lsc, NULL, "lacp stopped\n"));
    614 }
    615 
    616 void
    617 lacp_down(struct lagg_proto_softc *xlsc)
    618 {
    619 	struct lacp_softc *lsc;
    620 
    621 	lsc = (struct lacp_softc *)xlsc;
    622 
    623 	KASSERT(LAGG_LOCKED(lsc->lsc_softc));
    624 
    625 	LACP_LOCK(lsc);
    626 	lacp_down_locked(lsc);
    627 	LACP_UNLOCK(lsc);
    628 }
    629 
    630 int
    631 lacp_transmit(struct lagg_proto_softc *xlsc, struct mbuf *m)
    632 {
    633 	struct lacp_softc *lsc;
    634 	struct lagg_port *lp;
    635 	struct ifnet *ifp;
    636 	struct psref psref;
    637 
    638 	lsc = (struct lacp_softc *)xlsc;
    639 
    640 	if (__predict_false(lsc->lsc_suppress_distributing)) {
    641 		LACP_DPRINTF((lsc, NULL, "waiting transit\n"));
    642 		m_freem(m);
    643 		return EBUSY;
    644 	}
    645 
    646 	lp = lacp_select_tx_port(lsc, m, &psref);
    647 	if (__predict_false(lp == NULL)) {
    648 		LACP_DPRINTF((lsc, NULL, "no distributing port\n"));
    649 		ifp = &lsc->lsc_softc->sc_if;
    650 		if_statinc(ifp, if_oerrors);
    651 		m_freem(m);
    652 		return ENOENT;
    653 	}
    654 
    655 	lagg_output(lsc->lsc_softc, lp, m);
    656 	lagg_port_putref(lp, &psref);
    657 
    658 	return 0;
    659 }
    660 
    661 int
    662 lacp_allocport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
    663 {
    664 	struct lagg_softc *sc;
    665 	struct lacp_softc *lsc;
    666 	struct lacp_port *lacpp;
    667 	struct ifreq ifr;
    668 	bool added_multi;
    669 	int error;
    670 
    671 	lsc = (struct lacp_softc *)xlsc;
    672 	sc = lsc->lsc_softc;
    673 
    674 	KASSERT(LAGG_LOCKED(sc));
    675 	KASSERT(IFNET_LOCKED(lp->lp_ifp));
    676 
    677 	lacpp = kmem_zalloc(sizeof(*lacpp), KM_NOSLEEP);
    678 	if (lacpp == NULL)
    679 		return ENOMEM;
    680 
    681 	lacp_mcastaddr(&ifr, lp->lp_ifp->if_xname);
    682 	error = lp->lp_ioctl(lp->lp_ifp, SIOCADDMULTI, (void *)&ifr);
    683 
    684 	switch (error) {
    685 	case 0:
    686 		added_multi = true;
    687 		break;
    688 	case EAFNOSUPPORT:
    689 		added_multi = false;
    690 		break;
    691 	default:
    692 		LAGG_LOG(sc, LOG_ERR, "SIOCADDMULTI failed on %s\n",
    693 		    lp->lp_ifp->if_xname);
    694 		kmem_free(lacpp, sizeof(*lacpp));
    695 		return error;
    696 	}
    697 
    698 	lacpp->lp_added_multi = added_multi;
    699 	lagg_work_set(&lacpp->lp_work_smtx, lacp_sm_tx_work, lsc);
    700 	lagg_work_set(&lacpp->lp_work_marker, lacp_marker_work, lsc);
    701 
    702 	LACP_LOCK(lsc);
    703 	lacp_sm_port_init(lsc, lacpp, lp);
    704 	LACP_UNLOCK(lsc);
    705 
    706 	lp->lp_proto_ctx = (void *)lacpp;
    707 	lp->lp_prio = ntohs(lacpp->lp_actor.lpi_portprio);
    708 
    709 	return 0;
    710 }
    711 
    712 void
    713 lacp_startport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
    714 {
    715 	struct lacp_port *lacpp;
    716 	uint16_t prio;
    717 
    718 	lacpp = lp->lp_proto_ctx;
    719 
    720 	prio = (uint16_t)MIN(lp->lp_prio, UINT16_MAX);
    721 	lacpp->lp_actor.lpi_portprio = htons(prio);
    722 
    723 	lacp_linkstate(xlsc, lp);
    724 }
    725 
    726 void
    727 lacp_stopport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
    728 {
    729 	struct lacp_softc *lsc;
    730 	struct lacp_port *lacpp;
    731 	int i;
    732 
    733 	lsc = (struct lacp_softc *)xlsc;
    734 	lacpp = lp->lp_proto_ctx;
    735 
    736 	KASSERT(LAGG_LOCKED(lsc->lsc_softc));
    737 
    738 	LACP_LOCK(lsc);
    739 	for (i = 0; i < LACP_NTIMER; i++) {
    740 		LACP_TIMER_DISARM(lacpp, i);
    741 	}
    742 
    743 	lacp_port_disable(lsc, lacpp);
    744 	LACP_UNLOCK(lsc);
    745 }
    746 
    747 void
    748 lacp_freeport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
    749 {
    750 	struct lacp_softc *lsc;
    751 	struct lacp_port *lacpp;
    752 	struct ifreq ifr;
    753 
    754 	lsc = (struct lacp_softc *)xlsc;
    755 	lacpp = lp->lp_proto_ctx;
    756 
    757 	KASSERT(LAGG_LOCKED(lsc->lsc_softc));
    758 	KASSERT(IFNET_LOCKED(lp->lp_ifp));
    759 
    760 	lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_smtx);
    761 	lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_marker);
    762 
    763 	if (lacpp->lp_added_multi) {
    764 		lacp_mcastaddr(&ifr, LACP_PORT_XNAME(lacpp));
    765 
    766 		(void)lp->lp_ioctl(lp->lp_ifp, SIOCDELMULTI, (void *)&ifr);
    767 	}
    768 
    769 	lp->lp_proto_ctx = NULL;
    770 	kmem_free(lacpp, sizeof(*lacpp));
    771 }
    772 
    773 void
    774 lacp_protostat(struct lagg_proto_softc *xlsc, struct laggreqproto *resp)
    775 {
    776 	struct laggreq_lacp *rplacp;
    777 	struct lacp_softc *lsc;
    778 	struct lacp_aggregator *la;
    779 	struct lacp_aggregator_systemid *sid;
    780 
    781 	lsc = (struct lacp_softc *)xlsc;
    782 
    783 	LACP_LOCK(lsc);
    784 	la = lsc->lsc_aggregator;
    785 	rplacp = &resp->rp_lacp;
    786 
    787 	if (lsc->lsc_optimistic)
    788 		SET(rplacp->flags, LAGGREQLACP_OPTIMISTIC);
    789 	if (lsc->lsc_dump_du)
    790 		SET(rplacp->flags, LAGGREQLACP_DUMPDU);
    791 	if (lsc->lsc_stop_lacpdu)
    792 		SET(rplacp->flags, LAGGREQLACP_STOPDU);
    793 	if (lsc->lsc_multi_linkspeed)
    794 		SET(rplacp->flags, LAGGREQLACP_MULTILS);
    795 
    796 	rplacp->maxports = lsc->lsc_max_ports;
    797 	rplacp->actor_prio = ntohs(lsc->lsc_system_prio);
    798 	memcpy(rplacp->actor_mac, lsc->lsc_system_mac,
    799 	    sizeof(rplacp->actor_mac));
    800 	rplacp->actor_key = ntohs(lsc->lsc_key);
    801 
    802 	if (la != NULL) {
    803 		sid = &la->la_sid;
    804 		rplacp->partner_prio = ntohs(sid->sid_prio);
    805 		memcpy(rplacp->partner_mac, sid->sid_mac,
    806 		    sizeof(rplacp->partner_mac));
    807 		rplacp->partner_key = ntohs(sid->sid_key);
    808 	}
    809 	LACP_UNLOCK(lsc);
    810 }
    811 
    812 void
    813 lacp_portstat(struct lagg_proto_softc *xlsc, struct lagg_port *lp,
    814     struct laggreqport *resp)
    815 {
    816 	struct laggreq_lacpport *llp;
    817 	struct lacp_softc *lsc;
    818 	struct lacp_port *lacpp;
    819 	struct lacp_aggregator *la;
    820 	struct lacp_aggregator_systemid *sid;
    821 
    822 	lsc = (struct lacp_softc *)xlsc;
    823 	lacpp = lp->lp_proto_ctx;
    824 	la = lacpp->lp_aggregator;
    825 	llp = &resp->rp_lacpport;
    826 
    827 	if (lacp_isactive(lsc, lacpp))
    828 		SET(resp->rp_flags, LAGG_PORT_ACTIVE);
    829 	if (lacp_iscollecting(lacpp))
    830 		SET(resp->rp_flags, LAGG_PORT_COLLECTING);
    831 	if (lacp_isdistributing(lacpp))
    832 		SET(resp->rp_flags, LAGG_PORT_DISTRIBUTING);
    833 	if (lacpp->lp_selected == LACP_STANDBY)
    834 		SET(resp->rp_flags, LAGG_PORT_STANDBY);
    835 
    836 	if (la != NULL) {
    837 		sid = &la->la_sid;
    838 		llp->partner_prio = ntohs(sid->sid_prio);
    839 		memcpy(llp->partner_mac, sid->sid_mac,
    840 		    sizeof(llp->partner_mac));
    841 		llp->partner_key = ntohs(sid->sid_key);
    842 	}
    843 
    844 	llp->actor_portprio = ntohs(lacpp->lp_actor.lpi_portprio);
    845 	llp->actor_portno = ntohs(lacpp->lp_actor.lpi_portno);
    846 	llp->actor_state = lacpp->lp_actor.lpi_state;
    847 
    848 	llp->partner_portprio = ntohs(lacpp->lp_partner.lpi_portprio);
    849 	llp->partner_portno = ntohs(lacpp->lp_partner.lpi_portno);
    850 	llp->partner_state = lacpp->lp_partner.lpi_state;
    851 }
    852 
    853 void
    854 lacp_linkstate_ifnet_locked(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
    855 {
    856 	struct lacp_softc *lsc;
    857 	struct lacp_port *lacpp;
    858 	struct ifmediareq ifmr;
    859 	struct ifnet *ifp_port;
    860 	uint8_t old_state;
    861 	uint64_t old_linkspeed, new_linkspeed;
    862 	int error;
    863 
    864 	KASSERT(IFNET_LOCKED(lp->lp_ifp));
    865 
    866 	lsc = (struct lacp_softc *)xlsc;
    867 
    868 	ifp_port = lp->lp_ifp;
    869 	lacpp = lp->lp_proto_ctx;
    870 
    871 	memset(&ifmr, 0, sizeof(ifmr));
    872 	ifmr.ifm_count = 0;
    873 	error = if_ioctl(ifp_port, SIOCGIFMEDIA, (void *)&ifmr);
    874 	if (error == 0) {
    875 		new_linkspeed = ifmedia_baudrate(ifmr.ifm_active);
    876 #ifdef LACP_NOFDX
    877 		/*
    878 		 * some driver that has no media, e.g. vioif(4),
    879 		 * returns (IFM_ETHER | IFM_AUTO)
    880 		 */
    881 		if ((ifmr.ifm_active & ~(IFM_ETHER | IFM_AUTO)) == 0)
    882 			ifmr.ifm_active |= IFM_FDX;
    883 #endif
    884 	} else if (error == ENOTTY) {
    885 		ifmr.ifm_active = IFM_FDX | IF_Gbps(0);
    886 		new_linkspeed = 0;
    887 	} else {
    888 		LACP_DPRINTF((lsc, lacpp,
    889 		    "SIOCGIFMEDIA failed (%d)\n", error));
    890 		return;
    891 	}
    892 
    893 	LACP_LOCK(lsc);
    894 	if (lsc->lsc_running) {
    895 		old_linkspeed = lacpp->lp_linkspeed;
    896 		old_state = lacpp->lp_actor.lpi_state;
    897 
    898 		if (new_linkspeed != old_linkspeed) {
    899 			LACP_DPRINTF((lsc, lacpp,
    900 			    "linkspeed changed %"PRIu64" -> %"PRIu64"\n",
    901 			    old_linkspeed, new_linkspeed));
    902 			lacpp->lp_linkspeed = new_linkspeed;
    903 		}
    904 
    905 		if (ISSET(ifmr.ifm_active, IFM_FDX) &&
    906 		    ISSET(ifp_port->if_flags, IFF_RUNNING) &&
    907 		    ifp_port->if_link_state != LINK_STATE_DOWN) {
    908 			lacp_port_enable(lsc, lacpp);
    909 		} else {
    910 			LACP_DPRINTF((lsc, lacpp,
    911 			    "FDX=%d, RUNNING=%d, link=%d\n",
    912 			    ISSET(ifmr.ifm_active, IFM_FDX) != 0,
    913 			    ISSET(ifp_port->if_flags, IFF_RUNNING) != 0,
    914 			    ifp_port->if_link_state != LINK_STATE_DOWN));
    915 			lacp_port_disable(lsc, lacpp);
    916 		}
    917 
    918 		if (old_state != lacpp->lp_actor.lpi_state ||
    919 		    old_linkspeed != new_linkspeed) {
    920 			LACP_DPRINTF((lsc, lacpp,
    921 			    "state changed to UNSELECTED\n"));
    922 			lacpp->lp_selected = LACP_UNSELECTED;
    923 		}
    924 	} else {
    925 		LACP_DPRINTF((lsc, lacpp,
    926 		    "LACP is inactive, skip linkstate\n"));
    927 	}
    928 	LACP_UNLOCK(lsc);
    929 }
    930 
    931 int
    932 lacp_ioctl(struct lagg_proto_softc *xlsc, struct laggreqproto *lreq)
    933 {
    934 	struct lacp_softc *lsc;
    935 	struct laggreq_lacp *rplacp;
    936 	struct lacp_aggregator *la;
    937 	int error;
    938 	size_t maxports;
    939 	bool set;
    940 
    941 	lsc = (struct lacp_softc *)xlsc;
    942 	rplacp = &lreq->rp_lacp;
    943 	error = 0;
    944 
    945 	switch (rplacp->command) {
    946 	case LAGGIOC_LACPSETFLAGS:
    947 	case LAGGIOC_LACPCLRFLAGS:
    948 		set = (rplacp->command == LAGGIOC_LACPSETFLAGS) ?
    949 		    true : false;
    950 
    951 		LACP_LOCK(lsc);
    952 
    953 		if (ISSET(rplacp->flags, LAGGREQLACP_OPTIMISTIC))
    954 			lsc->lsc_optimistic = set;
    955 		if (ISSET(rplacp->flags, LAGGREQLACP_DUMPDU))
    956 			lsc->lsc_dump_du = set;
    957 		if (ISSET(rplacp->flags, LAGGREQLACP_STOPDU))
    958 			lsc->lsc_stop_lacpdu = set;
    959 
    960 		if (ISSET(rplacp->flags, LAGGREQLACP_MULTILS) &&
    961 		    lsc->lsc_multi_linkspeed != set) {
    962 			lsc->lsc_multi_linkspeed = set;
    963 			TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
    964 				lacp_selected_update(lsc, la);
    965 			}
    966 		}
    967 
    968 		LACP_UNLOCK(lsc);
    969 		break;
    970 	case LAGGIOC_LACPSETMAXPORTS:
    971 	case LAGGIOC_LACPCLRMAXPORTS:
    972 		maxports = (rplacp->command == LAGGIOC_LACPSETMAXPORTS) ?
    973 		    rplacp->maxports : LACP_MAX_PORTS;
    974 		if (0 == maxports || LACP_MAX_PORTS < maxports) {
    975 			error = ERANGE;
    976 			break;
    977 		}
    978 
    979 		LACP_LOCK(lsc);
    980 		if (lsc->lsc_max_ports != maxports) {
    981 			lsc->lsc_max_ports = maxports;
    982 			TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
    983 				lacp_selected_update(lsc, la);
    984 			}
    985 		}
    986 		LACP_UNLOCK(lsc);
    987 		break;
    988 	default:
    989 		error = ENOTTY;
    990 	}
    991 
    992 	return error;
    993 }
    994 
    995 static int
    996 lacp_pdu_input(struct lacp_softc *lsc, struct lacp_port *lacpp, struct mbuf *m)
    997 {
    998 	enum {
    999 		LACP_TLV_ACTOR = 0,
   1000 		LACP_TLV_PARTNER,
   1001 		LACP_TLV_COLLECTOR,
   1002 		LACP_TLV_TERM,
   1003 		LACP_TLV_NUM
   1004 	};
   1005 
   1006 	struct lacpdu *du;
   1007 	struct lacpdu_peerinfo *pi_actor, *pi_partner;
   1008 	struct lacpdu_collectorinfo *lci;
   1009 	struct tlv tlvlist_lacp[LACP_TLV_NUM] = {
   1010 		[LACP_TLV_ACTOR] = {
   1011 		    .tlv_t = LACP_TYPE_ACTORINFO,
   1012 		    .tlv_l = sizeof(*pi_actor)},
   1013 		[LACP_TLV_PARTNER] = {
   1014 		    .tlv_t = LACP_TYPE_PARTNERINFO,
   1015 		    .tlv_l = sizeof(*pi_partner)},
   1016 		[LACP_TLV_COLLECTOR] = {
   1017 		    .tlv_t = LACP_TYPE_COLLECTORINFO,
   1018 		    .tlv_l = sizeof(*lci)},
   1019 		[LACP_TLV_TERM] = {
   1020 		    .tlv_t = TLV_TYPE_TERMINATE,
   1021 		    .tlv_l = 0},
   1022 	};
   1023 
   1024 	if (m->m_pkthdr.len != sizeof(*du))
   1025 		goto bad;
   1026 
   1027 	if (m->m_len < (int)sizeof(*du)) {
   1028 		m = m_pullup(m, sizeof(*du));
   1029 		if (m == NULL) {
   1030 			lsc->lsc_mpullup_failed.ev_count++;
   1031 			return ENOMEM;
   1032 		}
   1033 	}
   1034 
   1035 	du = mtod(m, struct lacpdu *);
   1036 
   1037 	if (memcmp(&du->ldu_eh.ether_dhost,
   1038 	    ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN) != 0)
   1039 		goto bad;
   1040 
   1041 	LACP_TLV_PARSE(du, struct lacpdu, ldu_tlv_actor,
   1042 	    tlvlist_lacp);
   1043 
   1044 	pi_actor = tlvlist_lacp[LACP_TLV_ACTOR].tlv_v;
   1045 	pi_partner = tlvlist_lacp[LACP_TLV_PARTNER].tlv_v;
   1046 	lci = tlvlist_lacp[LACP_TLV_COLLECTOR].tlv_v;
   1047 
   1048 	if (pi_actor == NULL || pi_partner == NULL)
   1049 		goto bad;
   1050 
   1051 	if (LACP_ISDUMPING(lsc)) {
   1052 		lacp_dprintf(lsc, lacpp, "lacpdu received\n");
   1053 		lacp_dump_lacpdutlv(pi_actor, pi_partner, lci);
   1054 	}
   1055 
   1056 	LACP_LOCK(lsc);
   1057 	lacp_sm_rx(lsc, lacpp, pi_partner, pi_actor);
   1058 	LACP_UNLOCK(lsc);
   1059 
   1060 	m_freem(m);
   1061 	return 0;
   1062 bad:
   1063 	lsc->lsc_badlacpdu.ev_count++;
   1064 	m_freem(m);
   1065 	return EINVAL;
   1066 }
   1067 
   1068 static int
   1069 marker_cmp(struct markerdu_info *mi,
   1070     struct lacp_softc *lsc, struct lacp_port *lacpp)
   1071 {
   1072 
   1073 	KASSERT(LACP_LOCKED(lsc));
   1074 
   1075 	if (mi->mi_rq_port != lacpp->lp_actor.lpi_portno)
   1076 		return -1;
   1077 
   1078 	if (ntohl(mi->mi_rq_xid) != lacpp->lp_marker_xid)
   1079 		return -1;
   1080 
   1081 	return  memcmp(mi->mi_rq_system, lsc->lsc_system_mac,
   1082 	    LACP_MAC_LEN);
   1083 }
   1084 
   1085 static void
   1086 lacp_marker_reply(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1087     struct mbuf *m_info)
   1088 {
   1089 	struct lagg_port *lp;
   1090 	struct markerdu *mdu;
   1091 	struct ifnet *ifp_port;
   1092 	struct psref psref;
   1093 
   1094 	LACP_LOCK(lsc);
   1095 	lp = lacpp->lp_laggport;
   1096 	lagg_port_getref(lp, &psref);
   1097 	LACP_UNLOCK(lsc);
   1098 
   1099 	ifp_port = lp->lp_ifp;
   1100 	mdu = mtod(m_info, struct markerdu *);
   1101 
   1102 	mdu->mdu_tlv_info.tlv_type = MARKER_TYPE_RESPONSE;
   1103 	/* ether_dhost is already ethermulticastaddr_slowprotocols */
   1104 	m_info->m_flags |= M_MCAST;
   1105 	memcpy(mdu->mdu_eh.ether_shost,
   1106 	    CLLADDR(ifp_port->if_sadl), ETHER_ADDR_LEN);
   1107 
   1108 	if (LACP_ISDUMPING(lsc)) {
   1109 		lacp_dprintf(lsc, lacpp, "markerdu reply\n");
   1110 		lacp_dump_markertlv(NULL, &mdu->mdu_info);
   1111 	}
   1112 
   1113 	lagg_port_xmit(lp, m_info);
   1114 	lagg_port_putref(lp, &psref);
   1115 }
   1116 
   1117 static int
   1118 lacp_marker_recv_response(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1119     struct markerdu_info *mi_res)
   1120 {
   1121 	struct lagg_softc *sc;
   1122 	struct lagg_port *lp0;
   1123 	struct lacp_port *lacpp0;
   1124 	bool pending;
   1125 
   1126 	sc = lsc->lsc_softc;
   1127 
   1128 	LACP_LOCK(lsc);
   1129 	if (marker_cmp(mi_res, lsc, lacpp) != 0) {
   1130 		LACP_UNLOCK(lsc);
   1131 		return -1;
   1132 	}
   1133 	CLR(lacpp->lp_flags, LACP_PORT_MARK);
   1134 	LACP_UNLOCK(lsc);
   1135 
   1136 	LAGG_LOCK(sc);
   1137 	LACP_LOCK(lsc);
   1138 
   1139 	if (lsc->lsc_suppress_distributing) {
   1140 		pending = false;
   1141 		LAGG_PORTS_FOREACH(sc, lp0) {
   1142 			lacpp0 = lp0->lp_proto_ctx;
   1143 			if (ISSET(lacpp0->lp_flags, LACP_PORT_MARK)) {
   1144 				pending = true;
   1145 				break;
   1146 			}
   1147 		}
   1148 
   1149 		if (!pending) {
   1150 			LACP_DPRINTF((lsc, NULL, "queue flush complete\n"));
   1151 			LACP_PTIMER_DISARM(lsc, LACP_PTIMER_DISTRIBUTING);
   1152 			lsc->lsc_suppress_distributing = false;
   1153 		}
   1154 	}
   1155 
   1156 	LACP_UNLOCK(lsc);
   1157 	LAGG_UNLOCK(sc);
   1158 
   1159 	return 0;
   1160 }
   1161 
   1162 static int
   1163 lacp_marker_input(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1164     struct mbuf *m)
   1165 {
   1166 	enum {
   1167 		MARKER_TLV_INFO = 0,
   1168 		MARKER_TLV_RESPONSE,
   1169 		MARKER_TLV_TERM,
   1170 		MARKER_TLV_NUM
   1171 	};
   1172 
   1173 	struct markerdu *mdu;
   1174 	struct markerdu_info *mi_info, *mi_res;
   1175 	int error;
   1176 	struct tlv tlvlist_marker[MARKER_TLV_NUM] = {
   1177 		[MARKER_TLV_INFO] = {
   1178 		    .tlv_t = MARKER_TYPE_INFO,
   1179 		    .tlv_l = sizeof(*mi_info)},
   1180 		[MARKER_TLV_RESPONSE] = {
   1181 		    .tlv_t = MARKER_TYPE_RESPONSE,
   1182 		    .tlv_l = sizeof(*mi_res)},
   1183 		[MARKER_TLV_TERM] = {
   1184 		    .tlv_t = TLV_TYPE_TERMINATE,
   1185 		    .tlv_l = 0},
   1186 	};
   1187 
   1188 	if (m->m_pkthdr.len != sizeof(*mdu))
   1189 		goto bad;
   1190 
   1191 	if (m->m_len < (int)sizeof(*mdu)) {
   1192 		m = m_pullup(m, sizeof(*mdu));
   1193 		if (m == NULL) {
   1194 			lsc->lsc_mpullup_failed.ev_count++;
   1195 			return ENOMEM;
   1196 		}
   1197 	}
   1198 
   1199 	mdu = mtod(m, struct markerdu *);
   1200 
   1201 	if (memcmp(mdu->mdu_eh.ether_dhost,
   1202 	    ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN) != 0)
   1203 		goto bad;
   1204 
   1205 	LACP_TLV_PARSE(mdu, struct markerdu, mdu_tlv_info,
   1206 	    tlvlist_marker);
   1207 
   1208 	mi_info = tlvlist_marker[MARKER_TLV_INFO].tlv_v;
   1209 	mi_res = tlvlist_marker[MARKER_TLV_RESPONSE].tlv_v;
   1210 
   1211 	if (LACP_ISDUMPING(lsc)) {
   1212 		lacp_dprintf(lsc, lacpp, "markerdu received\n");
   1213 		lacp_dump_markertlv(mi_info, mi_res);
   1214 	}
   1215 
   1216 	if (mi_info != NULL && mi_res == NULL) {
   1217 		lacp_marker_reply(lsc, lacpp, m);
   1218 	} else if (mi_info == NULL && mi_res != NULL) {
   1219 		error = lacp_marker_recv_response(lsc, lacpp,
   1220 		    mi_res);
   1221 		if (error != 0) {
   1222 			goto bad;
   1223 		} else {
   1224 			m_freem(m);
   1225 		}
   1226 	} else {
   1227 		goto bad;
   1228 	}
   1229 
   1230 	return 0;
   1231 bad:
   1232 	lsc->lsc_badmarkerdu.ev_count++;
   1233 	m_freem(m);
   1234 	return EINVAL;
   1235 }
   1236 
   1237 struct mbuf *
   1238 lacp_input(struct lagg_proto_softc *xlsc, struct lagg_port *lp, struct mbuf *m)
   1239 {
   1240 	struct ifnet *ifp;
   1241 	struct lacp_softc *lsc;
   1242 	struct lacp_port *lacpp;
   1243 	struct ether_header *eh;
   1244 	uint8_t subtype;
   1245 
   1246 	eh = mtod(m, struct ether_header *);
   1247 	lsc = (struct lacp_softc *)xlsc;
   1248 	ifp = &lsc->lsc_softc->sc_if;
   1249 	lacpp = lp->lp_proto_ctx;
   1250 
   1251 	if (!vlan_has_tag(m) &&
   1252 	    eh->ether_type == htons(ETHERTYPE_SLOWPROTOCOLS)) {
   1253 		if (m->m_pkthdr.len < (int)(sizeof(*eh) + sizeof(subtype))) {
   1254 			m_freem(m);
   1255 			return NULL;
   1256 		}
   1257 
   1258 		m_copydata(m, sizeof(struct ether_header),
   1259 		    sizeof(subtype), &subtype);
   1260 
   1261 		switch (subtype) {
   1262 		case SLOWPROTOCOLS_SUBTYPE_LACP:
   1263 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
   1264 			if (pcq_put(lsc->lsc_du_q, (void *)m)) {
   1265 				lagg_workq_add(lsc->lsc_workq,
   1266 				    &lsc->lsc_work_rcvdu);
   1267 			} else {
   1268 				m_freem(m);
   1269 				lsc->lsc_duq_nospc.ev_count++;
   1270 			}
   1271 			return NULL;
   1272 		}
   1273 	}
   1274 
   1275 	if (!lacp_iscollecting(lacpp) || !lacp_isactive(lsc, lacpp)) {
   1276 		if_statinc(ifp, if_ierrors);
   1277 		m_freem(m);
   1278 		return NULL;
   1279 	}
   1280 
   1281 	return m;
   1282 }
   1283 
   1284 static void
   1285 lacp_rcvdu_work(struct lagg_work *lw __unused, void *xlsc)
   1286 {
   1287 	struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
   1288 	struct ifnet *ifp;
   1289 	struct psref psref_lp;
   1290 	struct lagg_port *lp;
   1291 	struct mbuf *m;
   1292 	uint8_t subtype;
   1293 	int bound, s;
   1294 
   1295 	bound = curlwp_bind();
   1296 
   1297 	for (;;) {
   1298 		m = pcq_get(lsc->lsc_du_q);
   1299 		if (m == NULL)
   1300 			break;
   1301 
   1302 		ifp = m_get_rcvif(m, &s);
   1303 		if (ifp == NULL) {
   1304 			m_freem(m);
   1305 			lsc->lsc_norcvif.ev_count++;
   1306 			continue;
   1307 		}
   1308 
   1309 		lp = atomic_load_consume(&ifp->if_lagg);
   1310 		if (lp == NULL) {
   1311 			m_put_rcvif(ifp, &s);
   1312 			m_freem(m);
   1313 			lsc->lsc_norcvif.ev_count++;
   1314 			continue;
   1315 		}
   1316 
   1317 		lagg_port_getref(lp, &psref_lp);
   1318 		m_put_rcvif(ifp, &s);
   1319 
   1320 		m_copydata(m, sizeof(struct ether_header),
   1321 		    sizeof(subtype), &subtype);
   1322 
   1323 		switch (subtype) {
   1324 		case SLOWPROTOCOLS_SUBTYPE_LACP:
   1325 			(void)lacp_pdu_input(lsc,
   1326 			    lp->lp_proto_ctx, m);
   1327 			break;
   1328 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
   1329 			(void)lacp_marker_input(lsc,
   1330 			    lp->lp_proto_ctx, m);
   1331 			break;
   1332 		}
   1333 
   1334 		lagg_port_putref(lp, &psref_lp);
   1335 	}
   1336 
   1337 	curlwp_bindx(bound);
   1338 }
   1339 
   1340 static bool
   1341 lacp_port_need_to_tell(struct lacp_port *lacpp)
   1342 {
   1343 
   1344 	if (!ISSET(lacpp->lp_actor.lpi_state,
   1345 	    LACP_STATE_AGGREGATION)) {
   1346 		return false;
   1347 	}
   1348 
   1349 	if (!ISSET(lacpp->lp_actor.lpi_state,
   1350 	    LACP_STATE_ACTIVITY)
   1351 	    && !ISSET(lacpp->lp_partner.lpi_state,
   1352 	    LACP_STATE_ACTIVITY)) {
   1353 		return false;
   1354 	}
   1355 
   1356 	if (!ISSET(lacpp->lp_flags, LACP_PORT_NTT))
   1357 		return false;
   1358 
   1359 	if (ppsratecheck(&lacpp->lp_last_lacpdu, &lacpp->lp_lacpdu_sent,
   1360 	    (LACP_SENDDU_PPS / LACP_FAST_PERIODIC_TIME)) == 0)
   1361 		return false;
   1362 
   1363 	return true;
   1364 }
   1365 
   1366 static void
   1367 lacp_sm_assert_ntt(struct lacp_port *lacpp)
   1368 {
   1369 
   1370 	SET(lacpp->lp_flags, LACP_PORT_NTT);
   1371 }
   1372 
   1373 static void
   1374 lacp_sm_negate_ntt(struct lacp_port *lacpp)
   1375 {
   1376 
   1377 	CLR(lacpp->lp_flags, LACP_PORT_NTT);
   1378 }
   1379 
   1380 static struct mbuf *
   1381 lacp_lacpdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1382 {
   1383 	struct ifnet *ifp_port;
   1384 	struct mbuf *m;
   1385 	struct lacpdu *du;
   1386 
   1387 	KASSERT(LACP_LOCKED(lsc));
   1388 
   1389 	ifp_port = lacpp->lp_laggport->lp_ifp;
   1390 
   1391 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1392 	if (m == NULL) {
   1393 		lsc->lsc_mgethdr_failed.ev_count++;
   1394 		return NULL;
   1395 	}
   1396 
   1397 	m->m_pkthdr.len = m->m_len = sizeof(*du);
   1398 	m_reset_rcvif(m);
   1399 
   1400 	du = mtod(m, struct lacpdu *);
   1401 	memset(du, 0, sizeof(*du));
   1402 
   1403 	m->m_flags |= M_MCAST;
   1404 	memcpy(du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
   1405 	    ETHER_ADDR_LEN);
   1406 	memcpy(du->ldu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
   1407 	    ETHER_ADDR_LEN);
   1408 	du->ldu_eh.ether_type = htons(ETHERTYPE_SLOWPROTOCOLS);
   1409 	du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
   1410 	du->ldu_sph.sph_version = 1;
   1411 
   1412 	tlv_set(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO,
   1413 	    sizeof(du->ldu_actor));
   1414 	lacp_peerinfo_actor(lsc, lacpp, &du->ldu_actor);
   1415 
   1416 	tlv_set(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
   1417 	    sizeof(du->ldu_partner));
   1418 	lacp_peerinfo_partner(lacpp, &du->ldu_partner);
   1419 
   1420 	tlv_set(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
   1421 	    sizeof(du->ldu_collector));
   1422 	du->ldu_collector.lci_maxdelay = 0;
   1423 
   1424 	du->ldu_tlv_term.tlv_type = LACP_TYPE_TERMINATE;
   1425 	du->ldu_tlv_term.tlv_length = 0;
   1426 
   1427 	return m;
   1428 }
   1429 
   1430 static void
   1431 lacp_sm_tx_work(struct lagg_work *lw, void *xlsc)
   1432 {
   1433 	struct lacp_softc *lsc;
   1434 	struct lacp_port *lacpp;
   1435 	struct lagg_port *lp;
   1436 	struct lacpdu *du;
   1437 	struct mbuf *m;
   1438 	struct psref psref;
   1439 	int bound;
   1440 
   1441 	lsc = xlsc;
   1442 	lacpp = container_of(lw, struct lacp_port, lp_work_smtx);
   1443 
   1444 	if (lsc->lsc_stop_lacpdu)
   1445 		return;
   1446 
   1447 	LACP_LOCK(lsc);
   1448 	m = lacp_lacpdu_mbuf(lsc, lacpp);
   1449 	if (m == NULL) {
   1450 		LACP_UNLOCK(lsc);
   1451 		return;
   1452 	}
   1453 	lacp_sm_negate_ntt(lacpp);
   1454 	lp = lacpp->lp_laggport;
   1455 	bound = curlwp_bind();
   1456 	lagg_port_getref(lp, &psref);
   1457 	LACP_UNLOCK(lsc);
   1458 
   1459 	if (LACP_ISDUMPING(lsc)) {
   1460 		lacp_dprintf(lsc, lacpp, "lacpdu transmit\n");
   1461 		du = mtod(m, struct lacpdu *);
   1462 		lacp_dump_lacpdutlv(&du->ldu_actor,
   1463 		    &du->ldu_partner, &du->ldu_collector);
   1464 	}
   1465 
   1466 	lagg_port_xmit(lp, m);
   1467 	lagg_port_putref(lp, &psref);
   1468 	curlwp_bindx(bound);
   1469 }
   1470 
   1471 static void
   1472 lacp_sm_tx(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1473 {
   1474 
   1475 	if (!lacp_port_need_to_tell(lacpp))
   1476 		return;
   1477 
   1478 	lagg_workq_add(lsc->lsc_workq, &lacpp->lp_work_smtx);
   1479 }
   1480 
   1481 static void
   1482 lacp_tick(void *xlsc)
   1483 {
   1484 	struct lacp_softc *lsc;
   1485 
   1486 	lsc = xlsc;
   1487 
   1488 	lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_tick);
   1489 
   1490 	LACP_LOCK(lsc);
   1491 	callout_schedule(&lsc->lsc_tick, hz);
   1492 	LACP_UNLOCK(lsc);
   1493 }
   1494 
   1495 static void
   1496 lacp_run_timers(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1497 {
   1498 	size_t i;
   1499 
   1500 	KASSERT(LACP_LOCKED(lsc));
   1501 
   1502 	for (i = 0; i < LACP_NTIMER; i++) {
   1503 		KASSERT(lacpp->lp_timer[i] >= 0);
   1504 
   1505 		if (lacpp->lp_timer[i] == 0)
   1506 			continue;
   1507 		if (--lacpp->lp_timer[i] > 0)
   1508 			continue;
   1509 
   1510 		KASSERT(lacp_timer_funcs[i] != NULL);
   1511 		lacp_timer_funcs[i](lsc, lacpp);
   1512 	}
   1513 }
   1514 
   1515 static void
   1516 lacp_run_prototimers(struct lacp_softc *lsc)
   1517 {
   1518 	size_t i;
   1519 
   1520 	for (i = 0; i < LACP_NPTIMER; i++) {
   1521 		KASSERT(lsc->lsc_timer[i] >= 0);
   1522 
   1523 		if (lsc->lsc_timer[i] == 0)
   1524 			continue;
   1525 		if (--lsc->lsc_timer[i] > 0)
   1526 			continue;
   1527 
   1528 		KASSERT(lacp_ptimer_funcs[i] != NULL);
   1529 		lacp_ptimer_funcs[i](lsc);
   1530 	}
   1531 }
   1532 
   1533 static void
   1534 lacp_tick_work(struct lagg_work *lw __unused, void *xlsc)
   1535 {
   1536 	struct lacp_softc *lsc;
   1537 	struct lacp_port *lacpp;
   1538 	struct lagg_softc *sc;
   1539 	struct lagg_port *lp;
   1540 
   1541 	lsc = xlsc;
   1542 	sc = lsc->lsc_softc;
   1543 
   1544 	LACP_LOCK(lsc);
   1545 	lacp_run_prototimers(lsc);
   1546 	LACP_UNLOCK(lsc);
   1547 
   1548 	LAGG_LOCK(sc);
   1549 	LACP_LOCK(lsc);
   1550 	LAGG_PORTS_FOREACH(sc, lp) {
   1551 		lacpp = lp->lp_proto_ctx;
   1552 		if (!ISSET(lacpp->lp_actor.lpi_state,
   1553 		    LACP_STATE_AGGREGATION)) {
   1554 			continue;
   1555 		}
   1556 
   1557 		lacp_run_timers(lsc, lacpp);
   1558 		lacp_select(lsc, lacpp);
   1559 		lacp_sm_mux(lsc, lacpp);
   1560 		lacp_sm_tx(lsc, lacpp);
   1561 		lacp_sm_ptx_schedule(lacpp);
   1562 	}
   1563 
   1564 	LACP_UNLOCK(lsc);
   1565 	LAGG_UNLOCK(sc);
   1566 }
   1567 
   1568 static void
   1569 lacp_systemid_str(char *buf, size_t buflen,
   1570     uint16_t prio, const uint8_t *mac, uint16_t key)
   1571 {
   1572 
   1573 	snprintf(buf, buflen,
   1574 	    "%04X,"
   1575 	    "%02X-%02X-%02X-%02X-%02X-%02X,"
   1576 	    "%04X",
   1577 	    (unsigned int)ntohs(prio),
   1578 	    (int)mac[0], (int)mac[1], (int)mac[2],
   1579 	    (int)mac[3], (int)mac[4], (int)mac[5],
   1580 	    (unsigned int)htons(key));
   1581 
   1582 }
   1583 
   1584 __LACPDEBUGUSED static void
   1585 lacp_aggregator_str(struct lacp_aggregator *la, char *buf, size_t buflen)
   1586 {
   1587 
   1588 	lacp_systemid_str(buf, buflen, la->la_sid.sid_prio,
   1589 	    la->la_sid.sid_mac, la->la_sid.sid_key);
   1590 }
   1591 
   1592 static void
   1593 lacp_peerinfo_idstr(const struct lacpdu_peerinfo *pi,
   1594     char *buf, size_t buflen)
   1595 {
   1596 
   1597 	lacp_systemid_str(buf, buflen, pi->lpi_system_prio,
   1598 	    pi->lpi_system_mac, pi->lpi_key);
   1599 }
   1600 
   1601 static void
   1602 lacp_state_str(uint8_t state, char *buf, size_t buflen)
   1603 {
   1604 
   1605 	snprintb(buf, buflen, LACP_STATE_BITS, state);
   1606 }
   1607 
   1608 static struct lagg_port *
   1609 lacp_select_tx_port(struct lacp_softc *lsc, struct mbuf *m,
   1610     struct psref *psref)
   1611 {
   1612 	struct lacp_portmap *pm;
   1613 	struct lagg_port *lp;
   1614 	uint32_t hash;
   1615 	size_t act;
   1616 	int s;
   1617 
   1618 	hash = lagg_hashmbuf(lsc->lsc_softc, m);
   1619 
   1620 	s = pserialize_read_enter();
   1621 	act = LACP_PORTMAP_ACTIVE(lsc);
   1622 	pm = &lsc->lsc_portmaps[act];
   1623 
   1624 	if (pm->pm_count == 0) {
   1625 		pserialize_read_exit(s);
   1626 		return NULL;
   1627 	}
   1628 
   1629 	hash %= pm->pm_count;
   1630 	lp = pm->pm_ports[hash];
   1631 	lagg_port_getref(lp, psref);
   1632 
   1633 	pserialize_read_exit(s);
   1634 
   1635 	return lp;
   1636 }
   1637 
   1638 static void
   1639 lacp_peerinfo_actor(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1640     struct lacpdu_peerinfo *dst)
   1641 {
   1642 
   1643 	memcpy(dst->lpi_system_mac, lsc->lsc_system_mac, LACP_MAC_LEN);
   1644 	dst->lpi_system_prio = lsc->lsc_system_prio;
   1645 	dst->lpi_key = lsc->lsc_key;
   1646 	dst->lpi_port_no = lacpp->lp_actor.lpi_portno;
   1647 	dst->lpi_port_prio = lacpp->lp_actor.lpi_portprio;
   1648 	dst->lpi_state = lacpp->lp_actor.lpi_state;
   1649 }
   1650 
   1651 static void
   1652 lacp_peerinfo_partner(struct lacp_port *lacpp, struct lacpdu_peerinfo *dst)
   1653 {
   1654 	struct lacp_aggregator *la;
   1655 
   1656 	la = lacpp->lp_aggregator;
   1657 
   1658 	if (la != NULL) {
   1659 		memcpy(dst->lpi_system_mac, la->la_sid.sid_mac, LACP_MAC_LEN);
   1660 		dst->lpi_system_prio = la->la_sid.sid_prio;
   1661 		dst->lpi_key = la->la_sid.sid_key;
   1662 	} else {
   1663 		memset(dst->lpi_system_mac, 0, LACP_MAC_LEN);
   1664 		dst->lpi_system_prio = 0;
   1665 		dst->lpi_key = 0;
   1666 	}
   1667 	dst->lpi_port_no = lacpp->lp_partner.lpi_portno;
   1668 	dst->lpi_port_prio = lacpp->lp_partner.lpi_portprio;
   1669 	dst->lpi_state = lacpp->lp_partner.lpi_state;
   1670 }
   1671 
   1672 static int
   1673 lacp_compare_peerinfo(struct lacpdu_peerinfo *a, struct lacpdu_peerinfo *b)
   1674 {
   1675 
   1676 	return memcmp(a, b, offsetof(struct lacpdu_peerinfo, lpi_state));
   1677 }
   1678 
   1679 static void
   1680 lacp_sm_rx_record_default(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1681 {
   1682 	uint8_t oldpstate;
   1683 	struct lacp_portinfo *pi;
   1684 	char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
   1685 
   1686 	pi = &lacpp->lp_partner;
   1687 
   1688 	oldpstate = pi->lpi_state;
   1689 	pi->lpi_portno = htons(LACP_PORTNO_NONE);
   1690 	pi->lpi_portprio = htons(0xffff);
   1691 
   1692 	if (lsc->lsc_optimistic)
   1693 		pi->lpi_state = LACP_PARTNER_ADMIN_OPTIMISTIC;
   1694 	else
   1695 		pi->lpi_state = LACP_PARTNER_ADMIN_STRICT;
   1696 
   1697 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
   1698 
   1699 	if (oldpstate != pi->lpi_state) {
   1700 		LACP_STATE_STR(oldpstate, buf, sizeof(buf));
   1701 		LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
   1702 
   1703 		LACP_STATE_STR(pi->lpi_state, buf, sizeof(buf));
   1704 		LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
   1705 	}
   1706 
   1707 	lacp_sm_ptx_update_timeout(lacpp, oldpstate);
   1708 }
   1709 
   1710 static inline bool
   1711 lacp_port_is_synced(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1712     struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
   1713 {
   1714 	struct lacpdu_peerinfo actor;
   1715 
   1716 	if (!ISSET(peer_pi->lpi_state, LACP_STATE_ACTIVITY) &&
   1717 	    (!ISSET(my_pi->lpi_state, LACP_STATE_ACTIVITY) ||
   1718 	    !ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY)))
   1719 		return false;
   1720 
   1721 	if (!ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION))
   1722 		return false;
   1723 
   1724 	lacp_peerinfo_actor(lsc, lacpp, &actor);
   1725 	if (lacp_compare_peerinfo(&actor, my_pi) != 0)
   1726 		return false;
   1727 
   1728 	if (!LACP_STATE_EQ(actor.lpi_state, my_pi->lpi_state,
   1729 	    LACP_STATE_AGGREGATION)) {
   1730 		return false;
   1731 	}
   1732 
   1733 	return true;
   1734 }
   1735 
   1736 static void
   1737 lacp_sm_rx_record_peerinfo(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1738     struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
   1739 {
   1740 	char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
   1741 	uint8_t oldpstate;
   1742 	struct lacp_portinfo *pi;
   1743 	struct lacp_aggregator_systemid *sid;
   1744 
   1745 	pi = &lacpp->lp_partner;
   1746 	sid = &lacpp->lp_aggregator_sidbuf;
   1747 
   1748 	oldpstate = lacpp->lp_partner.lpi_state;
   1749 
   1750 	sid->sid_prio = peer_pi->lpi_system_prio;
   1751 	sid->sid_key = peer_pi->lpi_key;
   1752 	memcpy(sid->sid_mac, peer_pi->lpi_system_mac,
   1753 	    sizeof(sid->sid_mac));
   1754 
   1755 	pi->lpi_portno = peer_pi->lpi_port_no;
   1756 	pi->lpi_portprio = peer_pi->lpi_port_prio;
   1757 	pi->lpi_state = peer_pi->lpi_state;
   1758 
   1759 	if (lacp_port_is_synced(lsc, lacpp, my_pi, peer_pi)) {
   1760 		if (lsc->lsc_optimistic)
   1761 			SET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   1762 	} else {
   1763 		CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   1764 	}
   1765 
   1766 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
   1767 
   1768 	if (oldpstate != lacpp->lp_partner.lpi_state) {
   1769 		LACP_STATE_STR(oldpstate, buf, sizeof(buf));
   1770 		LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
   1771 
   1772 		LACP_STATE_STR(lacpp->lp_partner.lpi_state,
   1773 		    buf, sizeof(buf));
   1774 		LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
   1775 	}
   1776 
   1777 	lacp_sm_ptx_update_timeout(lacpp, oldpstate);
   1778 }
   1779 
   1780 static void
   1781 lacp_sm_rx_set_expired(struct lacp_port *lacpp)
   1782 {
   1783 	uint8_t oldpstate;
   1784 
   1785 	oldpstate = lacpp->lp_partner.lpi_state;
   1786 
   1787 	CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   1788 	SET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT);
   1789 	LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE,
   1790 	    LACP_SHORT_TIMEOUT_TIME);
   1791 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
   1792 
   1793 	lacp_sm_ptx_update_timeout(lacpp, oldpstate);
   1794 }
   1795 
   1796 static void
   1797 lacp_sm_port_init(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1798     struct lagg_port *lp)
   1799 {
   1800 
   1801 	KASSERT(LACP_LOCKED(lsc));
   1802 
   1803 	lacpp->lp_laggport = lp;
   1804 	lacpp->lp_actor.lpi_state = LACP_STATE_ACTIVITY;
   1805 	lacpp->lp_actor.lpi_portno = htons(if_get_index(lp->lp_ifp));
   1806 	lacpp->lp_actor.lpi_portprio = htons(LACP_PORT_PRIO);
   1807 	lacpp->lp_partner.lpi_state = LACP_STATE_TIMEOUT;
   1808 	lacpp->lp_aggregator = NULL;
   1809 	lacpp->lp_marker_xid = 0;
   1810 	lacpp->lp_mux_state = LACP_MUX_INIT;
   1811 
   1812 	lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
   1813 	lacp_sm_rx_record_default(lsc, lacpp);
   1814 }
   1815 
   1816 static void
   1817 lacp_port_disable(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1818 {
   1819 
   1820 	KASSERT(LACP_LOCKED(lsc));
   1821 
   1822 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
   1823 		LACP_DPRINTF((lsc, lacpp, "enable -> disable\n"));
   1824 
   1825 	lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
   1826 	lacp_sm_rx_record_default(lsc, lacpp);
   1827 	CLR(lacpp->lp_actor.lpi_state,
   1828 	    LACP_STATE_AGGREGATION | LACP_STATE_EXPIRED);
   1829 	CLR(lacpp->lp_partner.lpi_state, LACP_STATE_AGGREGATION);
   1830 }
   1831 
   1832 static void
   1833 lacp_port_enable(struct lacp_softc *lsc __LACPDEBUGUSED,
   1834     struct lacp_port *lacpp)
   1835 {
   1836 
   1837 	KASSERT(LACP_LOCKED(lsc));
   1838 
   1839 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
   1840 		LACP_DPRINTF((lsc, lacpp, "disable -> enable\n"));
   1841 
   1842 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION);
   1843 	lacp_sm_rx_set_expired(lacpp);
   1844 }
   1845 
   1846 static void
   1847 lacp_sm_rx_timer(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1848 {
   1849 
   1850 	KASSERT(LACP_LOCKED(lsc));
   1851 
   1852 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED)) {
   1853 		/* CURRENT -> EXPIRED */
   1854 		LACP_DPRINTF((lsc, lacpp, "CURRENT -> EXPIRED\n"));
   1855 		lacp_sm_rx_set_expired(lacpp);
   1856 	} else {
   1857 		LACP_DPRINTF((lsc, lacpp, "EXPIRED -> DEFAULTED\n"));
   1858 		lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
   1859 		lacp_sm_rx_record_default(lsc, lacpp);
   1860 	}
   1861 }
   1862 
   1863 static void
   1864 lacp_sm_ptx_timer(struct lacp_softc *lsc __unused, struct lacp_port *lacpp)
   1865 {
   1866 
   1867 	KASSERT(LACP_LOCKED(lsc));
   1868 	lacp_sm_assert_ntt(lacpp);
   1869 }
   1870 
   1871 static void
   1872 lacp_sm_ptx_schedule(struct lacp_port *lacpp)
   1873 {
   1874 	int timeout;
   1875 
   1876 	/* no periodic */
   1877 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY) &&
   1878 	    !ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_ACTIVITY)) {
   1879 		LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
   1880 		return;
   1881 	}
   1882 
   1883 	if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_PERIODIC))
   1884 		return;
   1885 
   1886 	timeout = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT) ?
   1887 		LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
   1888 
   1889 	LACP_TIMER_ARM(lacpp, LACP_TIMER_PERIODIC, timeout);
   1890 }
   1891 
   1892 static void
   1893 lacp_sm_ptx_update_timeout(struct lacp_port *lacpp, uint8_t oldpstate)
   1894 {
   1895 
   1896 	if (LACP_STATE_EQ(oldpstate, lacpp->lp_partner.lpi_state,
   1897 	    LACP_STATE_TIMEOUT))
   1898 		return;
   1899 
   1900 	LACP_DPRINTF((NULL, lacpp, "partner timeout changed\n"));
   1901 
   1902 	LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
   1903 
   1904 	/* if timeout has been shorted, assert NTT */
   1905 	if (ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT))
   1906 		lacp_sm_assert_ntt(lacpp);
   1907 }
   1908 
   1909 static void
   1910 lacp_sm_mux_timer(struct lacp_softc *lsc __LACPDEBUGUSED,
   1911     struct lacp_port *lacpp)
   1912 {
   1913 	char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   1914 
   1915 	KASSERT(LACP_LOCKED(lsc));
   1916 	KASSERT(lacpp->lp_pending > 0);
   1917 
   1918 	LACP_AGGREGATOR_STR(lacpp->lp_aggregator, buf, sizeof(buf));
   1919 	LACP_DPRINTF((lsc, lacpp, "aggregator %s, pending %d -> %d\n",
   1920 	    buf, lacpp->lp_pending, lacpp->lp_pending -1));
   1921 
   1922 	lacpp->lp_pending--;
   1923 }
   1924 
   1925 static void
   1926 lacp_sm_rx_update_selected(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1927     struct lacpdu_peerinfo *peer_pi)
   1928 {
   1929 	struct lacpdu_peerinfo partner;
   1930 	char str0[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   1931 	char str1[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   1932 
   1933 	if (lacpp->lp_aggregator == NULL)
   1934 		return;
   1935 
   1936 	lacp_peerinfo_partner(lacpp, &partner);
   1937 	if (lacp_compare_peerinfo(peer_pi, &partner) != 0) {
   1938 		LACP_PEERINFO_IDSTR(&partner, str0, sizeof(str0));
   1939 		LACP_PEERINFO_IDSTR(peer_pi, str1, sizeof(str1));
   1940 		LACP_DPRINTF((lsc, lacpp,
   1941 		    "different peerinfo, %s vs %s\n", str0, str1));
   1942 		goto do_unselect;
   1943 	}
   1944 
   1945 	if (!LACP_STATE_EQ(lacpp->lp_partner.lpi_state,
   1946 	    peer_pi->lpi_state, LACP_STATE_AGGREGATION)) {
   1947 		LACP_DPRINTF((lsc, lacpp,
   1948 		    "STATE_AGGREGATION changed %d -> %d\n",
   1949 		    ISSET(lacpp->lp_partner.lpi_state,
   1950 		    LACP_STATE_AGGREGATION) != 0,
   1951 		    ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION) != 0));
   1952 		goto do_unselect;
   1953 	}
   1954 
   1955 	return;
   1956 
   1957 do_unselect:
   1958 	lacpp->lp_selected = LACP_UNSELECTED;
   1959 	/* lacpp->lp_aggregator will be released at lacp_set_mux() */
   1960 }
   1961 
   1962 static void
   1963 lacp_sm_rx_update_ntt(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1964     struct lacpdu_peerinfo *my_pi)
   1965 {
   1966 	struct lacpdu_peerinfo actor;
   1967 
   1968 	lacp_peerinfo_actor(lsc, lacpp, &actor);
   1969 
   1970 	if (lacp_compare_peerinfo(&actor, my_pi) != 0 ||
   1971 	    !LACP_STATE_EQ(lacpp->lp_actor.lpi_state, my_pi->lpi_state,
   1972 	    LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
   1973 		LACP_DPRINTF((lsc, lacpp, "assert ntt\n"));
   1974 		lacp_sm_assert_ntt(lacpp);
   1975 	}
   1976 }
   1977 
   1978 static void
   1979 lacp_sm_rx(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1980     struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
   1981 {
   1982 	int timeout;
   1983 
   1984 	KASSERT(LACP_LOCKED(lsc));
   1985 
   1986 	/* check LACP disabled first */
   1987 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
   1988 		return;
   1989 
   1990 	/* check loopback condition */
   1991 	if (memcmp(lsc->lsc_system_mac, peer_pi->lpi_system_mac,
   1992 	    LACP_MAC_LEN) == 0 &&
   1993 	    lsc->lsc_system_prio == peer_pi->lpi_system_prio)
   1994 		return;
   1995 
   1996 	lacp_sm_rx_update_selected(lsc, lacpp, peer_pi);
   1997 	lacp_sm_rx_update_ntt(lsc, lacpp, my_pi);
   1998 	lacp_sm_rx_record_peerinfo(lsc, lacpp, my_pi, peer_pi);
   1999 
   2000 	timeout = ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_TIMEOUT) ?
   2001 	    LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
   2002 	LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE, timeout);
   2003 
   2004 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
   2005 
   2006 	/* kick transmit machine without timeout. */
   2007 	lacp_sm_tx(lsc, lacpp);
   2008 }
   2009 
   2010 static void
   2011 lacp_disable_collecting(struct lacp_port *lacpp)
   2012 {
   2013 
   2014 	LACP_DPRINTF((NULL, lacpp, "collecting disabled\n"));
   2015 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
   2016 	atomic_store_relaxed(&lacpp->lp_collector, false);
   2017 }
   2018 
   2019 static void
   2020 lacp_enable_collecting(struct lacp_port *lacpp)
   2021 {
   2022 	LACP_DPRINTF((NULL, lacpp, "collecting enabled\n"));
   2023 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
   2024 	atomic_store_relaxed(&lacpp->lp_collector, true);
   2025 }
   2026 
   2027 static void
   2028 lacp_update_portmap(struct lacp_softc *lsc)
   2029 {
   2030 	struct lagg_softc *sc;
   2031 	struct lacp_aggregator *la;
   2032 	struct lacp_portmap *pm_act, *pm_next;
   2033 	struct lacp_port *lacpp;
   2034 	size_t pmap, n;
   2035 	u_int link;
   2036 
   2037 	KASSERT(LACP_LOCKED(lsc));
   2038 
   2039 	la = lsc->lsc_aggregator;
   2040 
   2041 	pmap = LACP_PORTMAP_ACTIVE(lsc);
   2042 	pm_act = &lsc->lsc_portmaps[pmap];
   2043 
   2044 	pmap = LACP_PORTMAP_NEXT(lsc);
   2045 	pm_next = &lsc->lsc_portmaps[pmap];
   2046 
   2047 	n = 0;
   2048 	if (la != NULL) {
   2049 		LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
   2050 			if (!ISSET(lacpp->lp_actor.lpi_state,
   2051 			    LACP_STATE_DISTRIBUTING)) {
   2052 				continue;
   2053 			}
   2054 
   2055 			pm_next->pm_ports[n] = lacpp->lp_laggport;
   2056 			n++;
   2057 
   2058 			if (n >= LACP_MAX_PORTS)
   2059 				break;
   2060 		}
   2061 	}
   2062 	pm_next->pm_count = n;
   2063 
   2064 	atomic_store_release(&lsc->lsc_activemap, pmap);
   2065 	pserialize_perform(lsc->lsc_psz);
   2066 
   2067 	LACP_DPRINTF((lsc, NULL, "portmap count updated (%zu -> %zu)\n",
   2068 	    pm_act->pm_count, pm_next->pm_count));
   2069 
   2070 	link = lacp_portmap_linkstate(pm_next);
   2071 	if (link != lacp_portmap_linkstate(pm_act)) {
   2072 		sc = lsc->lsc_softc;
   2073 		if_link_state_change(&sc->sc_if, link);
   2074 	}
   2075 
   2076 	lagg_workq_add(lsc->lsc_workq,  &lsc->lsc_work_linkspeed);
   2077 
   2078 	/* cleanup */
   2079 	pm_act->pm_count = 0;
   2080 	memset(pm_act->pm_ports, 0, sizeof(pm_act->pm_ports));
   2081 }
   2082 
   2083 static void
   2084 lacp_disable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2085 {
   2086 	struct lacp_portmap *pm;
   2087 	bool do_update;
   2088 	size_t act, i;
   2089 	int s;
   2090 
   2091 	KASSERT(LACP_LOCKED(lsc));
   2092 
   2093 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING)) {
   2094 		LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2095 		    "disable distributing on %s\n", LACP_PORT_XNAME(lacpp));
   2096 		CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
   2097 	}
   2098 
   2099 	s = pserialize_read_enter();
   2100 	act = LACP_PORTMAP_ACTIVE(lsc);
   2101 	pm = &lsc->lsc_portmaps[act];
   2102 
   2103 	do_update = false;
   2104 	for (i = 0; i < pm->pm_count; i++) {
   2105 		if (pm->pm_ports[i] == lacpp->lp_laggport) {
   2106 			do_update = true;
   2107 			break;
   2108 		}
   2109 	}
   2110 	pserialize_read_exit(s);
   2111 
   2112 	if (do_update)
   2113 		lacp_update_portmap(lsc);
   2114 }
   2115 
   2116 static void
   2117 lacp_enable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2118 {
   2119 
   2120 	KASSERT(LACP_LOCKED(lsc));
   2121 
   2122 	KASSERT(lacp_isactive(lsc, lacpp));
   2123 
   2124 	LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2125 	    "enable distributing on %s\n", LACP_PORT_XNAME(lacpp));
   2126 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
   2127 	lacp_suppress_distributing(lsc);
   2128 	lacp_update_portmap(lsc);
   2129 }
   2130 
   2131 static void
   2132 lacp_select_active_aggregator(struct lacp_softc *lsc)
   2133 {
   2134 	struct lacp_aggregator *la, *best_la;
   2135 	char str[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   2136 
   2137 	KASSERT(LACP_LOCKED(lsc));
   2138 
   2139 	la = lsc->lsc_aggregator;
   2140 	if (la != NULL && la->la_attached_port > 0) {
   2141 		best_la = la;
   2142 	} else {
   2143 		best_la = NULL;
   2144 	}
   2145 
   2146 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
   2147 		if (la->la_attached_port <= 0)
   2148 			continue;
   2149 
   2150 		if (best_la == NULL ||
   2151 		    LACP_SYS_PRI(la) < LACP_SYS_PRI(best_la))
   2152 			best_la = la;
   2153 	}
   2154 
   2155 	if (best_la != lsc->lsc_aggregator) {
   2156 		LACP_DPRINTF((lsc, NULL, "active aggregator changed\n"));
   2157 
   2158 		if (lsc->lsc_aggregator != NULL) {
   2159 			LACP_AGGREGATOR_STR(lsc->lsc_aggregator,
   2160 			    str, sizeof(str));
   2161 		} else {
   2162 			snprintf(str, sizeof(str), "(null)");
   2163 		}
   2164 		LACP_DPRINTF((lsc, NULL, "old aggregator=%s\n", str));
   2165 
   2166 		if (best_la != NULL) {
   2167 			LACP_AGGREGATOR_STR(best_la, str, sizeof(str));
   2168 		} else {
   2169 			snprintf(str, sizeof(str), "(null)");
   2170 		}
   2171 		LACP_DPRINTF((lsc, NULL, "new aggregator=%s\n", str));
   2172 
   2173 		lsc->lsc_aggregator = best_la;
   2174 	}
   2175 }
   2176 
   2177 static void
   2178 lacp_port_attached(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2179 {
   2180 	struct lacp_aggregator *la;
   2181 
   2182 	KASSERT(LACP_LOCKED(lsc));
   2183 
   2184 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
   2185 		return;
   2186 
   2187 	la = lacpp->lp_aggregator;
   2188 	KASSERT(la != NULL);
   2189 	KASSERT(la->la_attached_port >= 0);
   2190 
   2191 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
   2192 	la->la_attached_port++;
   2193 	lacp_select_active_aggregator(lsc);
   2194 }
   2195 
   2196 static void
   2197 lacp_port_detached(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2198 {
   2199 	struct lacp_aggregator *la;
   2200 
   2201 	KASSERT(LACP_LOCKED(lsc));
   2202 
   2203 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
   2204 		return;
   2205 
   2206 	la = lacpp->lp_aggregator;
   2207 	KASSERT(la != NULL);
   2208 	KASSERT(la->la_attached_port > 0);
   2209 
   2210 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
   2211 	la->la_attached_port--;
   2212 	lacp_select_active_aggregator(lsc);
   2213 }
   2214 
   2215 static int
   2216 lacp_set_mux(struct lacp_softc *lsc, struct lacp_port *lacpp,
   2217     enum lacp_mux_state new_state)
   2218 {
   2219 	struct lagg_softc *sc;
   2220 	struct ifnet *ifp;
   2221 
   2222 	KASSERT(LACP_LOCKED(lsc));
   2223 
   2224 	sc = lacpp->lp_laggport->lp_softc;
   2225 	ifp = &sc->sc_if;
   2226 
   2227 	if (lacpp->lp_mux_state == new_state)
   2228 		return -1;
   2229 
   2230 	switch (new_state) {
   2231 	case LACP_MUX_DETACHED:
   2232 		lacp_port_detached(lsc, lacpp);
   2233 		lacp_disable_distributing(lsc, lacpp);
   2234 		lacp_disable_collecting(lacpp);
   2235 		lacp_sm_assert_ntt(lacpp);
   2236 		/* cancel timer */
   2237 		if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)) {
   2238 			KASSERT(lacpp->lp_pending > 0);
   2239 			lacpp->lp_pending--;
   2240 			LACP_TIMER_DISARM(lacpp, LACP_TIMER_WAIT_WHILE);
   2241 		}
   2242 		lacp_unselect(lsc, lacpp);
   2243 		break;
   2244 	case LACP_MUX_WAITING:
   2245 		LACP_TIMER_ARM(lacpp, LACP_TIMER_WAIT_WHILE,
   2246 		    LACP_AGGREGATE_WAIT_TIME);
   2247 		lacpp->lp_pending++;
   2248 		break;
   2249 	case LACP_MUX_STANDBY:
   2250 #ifdef LACP_STANDBY_SYNCED
   2251 		lacp_port_attached(lsc, lacpp);
   2252 		lacp_disable_collecting(lacpp);
   2253 		lacp_sm_assert_ntt(lacpp);
   2254 #endif
   2255 		break;
   2256 	case LACP_MUX_ATTACHED:
   2257 		lacp_port_attached(lsc, lacpp);
   2258 		lacp_disable_collecting(lacpp);
   2259 		lacp_sm_assert_ntt(lacpp);
   2260 		break;
   2261 	case LACP_MUX_COLLECTING:
   2262 		lacp_enable_collecting(lacpp);
   2263 		lacp_disable_distributing(lsc, lacpp);
   2264 		lacp_sm_assert_ntt(lacpp);
   2265 		break;
   2266 	case LACP_MUX_DISTRIBUTING:
   2267 		lacp_enable_distributing(lsc, lacpp);
   2268 		break;
   2269 	case LACP_MUX_INIT:
   2270 	default:
   2271 		panic("%s: unknown state", ifp->if_xname);
   2272 	}
   2273 
   2274 	LACP_DPRINTF((lsc, lacpp, "mux_state %d -> %d\n",
   2275 	    lacpp->lp_mux_state, new_state));
   2276 
   2277 	lacpp->lp_mux_state = new_state;
   2278 	return 0;
   2279 }
   2280 
   2281 static void
   2282 lacp_sm_mux(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2283 {
   2284 	struct lacp_aggregator *la __diagused;
   2285 	enum lacp_mux_state  next_state;
   2286 	enum lacp_selected selected;
   2287 	bool p_sync, p_collecting;
   2288 
   2289 	p_sync = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   2290 	p_collecting = ISSET(lacpp->lp_partner.lpi_state,
   2291 	    LACP_STATE_COLLECTING);
   2292 
   2293 	do {
   2294 		next_state = lacpp->lp_mux_state;
   2295 		la = lacpp->lp_aggregator;
   2296 		selected = lacpp->lp_selected;
   2297 		KASSERT(la != NULL ||
   2298 		    lacpp->lp_mux_state == LACP_MUX_DETACHED);
   2299 
   2300 		switch (lacpp->lp_mux_state) {
   2301 		case LACP_MUX_DETACHED:
   2302 			if (selected != LACP_UNSELECTED)
   2303 				next_state = LACP_MUX_WAITING;
   2304 			break;
   2305 		case LACP_MUX_WAITING:
   2306 			KASSERTMSG((lacpp->lp_pending > 0 ||
   2307 			    !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)),
   2308 			    "lp_pending=%d, timer=%d", lacpp->lp_pending,
   2309 			    !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
   2310 
   2311 			if (selected == LACP_UNSELECTED) {
   2312 				next_state = LACP_MUX_DETACHED;
   2313 			} else if (lacpp->lp_pending == 0) {
   2314 				if (selected == LACP_SELECTED) {
   2315 					next_state = LACP_MUX_ATTACHED;
   2316 				} else if (selected == LACP_STANDBY) {
   2317 					next_state = LACP_MUX_STANDBY;
   2318 				} else {
   2319 					next_state = LACP_MUX_DETACHED;
   2320 				}
   2321 			}
   2322 			break;
   2323 		case LACP_MUX_STANDBY:
   2324 			if (selected == LACP_SELECTED) {
   2325 				next_state = LACP_MUX_ATTACHED;
   2326 			} else if (selected != LACP_STANDBY) {
   2327 				next_state = LACP_MUX_DETACHED;
   2328 			}
   2329 			break;
   2330 		case LACP_MUX_ATTACHED:
   2331 			if (selected != LACP_SELECTED) {
   2332 				if (selected == LACP_STANDBY)
   2333 					LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2334 					    "detaching %s\n",
   2335 					    LACP_PORT_XNAME(lacpp));
   2336 				next_state = LACP_MUX_DETACHED;
   2337 			} else if (lacp_isactive(lsc, lacpp) && p_sync) {
   2338 				next_state = LACP_MUX_COLLECTING;
   2339 			}
   2340 			break;
   2341 		case LACP_MUX_COLLECTING:
   2342 			if (selected != LACP_SELECTED ||
   2343 			    !lacp_isactive(lsc, lacpp)
   2344 			    || !p_sync) {
   2345 				next_state = LACP_MUX_ATTACHED;
   2346 			} else if (p_collecting) {
   2347 				next_state = LACP_MUX_DISTRIBUTING;
   2348 			}
   2349 			break;
   2350 		case LACP_MUX_DISTRIBUTING:
   2351 			if (selected != LACP_SELECTED ||
   2352 			    !lacp_isactive(lsc, lacpp)
   2353 			    || !p_sync || !p_collecting) {
   2354 				next_state = LACP_MUX_COLLECTING;
   2355 				LACP_DPRINTF((lsc, lacpp,
   2356 				    "Interface stopped DISTRIBUTING,"
   2357 				    " possible flapping\n"));
   2358 			}
   2359 			break;
   2360 		case LACP_MUX_INIT:
   2361 		default:
   2362 			panic("%s: unknown state",
   2363 			    lsc->lsc_softc->sc_if.if_xname);
   2364 		}
   2365 	} while (lacp_set_mux(lsc, lacpp, next_state) == 0);
   2366 }
   2367 
   2368 static bool
   2369 lacp_aggregator_is_match(struct lacp_aggregator_systemid *a,
   2370 struct lacp_aggregator_systemid *b)
   2371 {
   2372 
   2373 	if (a->sid_prio != b->sid_prio)
   2374 		return false;
   2375 
   2376 	if (a->sid_key != b->sid_key)
   2377 		return false;
   2378 
   2379 	if (memcmp(a->sid_mac, b->sid_mac, sizeof(a->sid_mac)) != 0)
   2380 		return false;
   2381 
   2382 	return true;
   2383 }
   2384 
   2385 static void
   2386 lacp_selected_update(struct lacp_softc *lsc, struct lacp_aggregator *la)
   2387 {
   2388 	struct lacp_port *lacpp;
   2389 	enum lacp_selected next_selected;
   2390 	const char *msg;
   2391 	size_t nselected;
   2392 	uint64_t linkspeed;
   2393 
   2394 	KASSERT(LACP_LOCKED(lsc));
   2395 
   2396 	lacpp = LIST_FIRST(&la->la_ports);
   2397 	if (lacpp == NULL)
   2398 		return;
   2399 
   2400 	linkspeed = lacpp->lp_linkspeed;
   2401 	nselected = 0;
   2402 	LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
   2403 		if (lacpp->lp_selected == LACP_UNSELECTED)
   2404 			continue;
   2405 
   2406 		next_selected = LACP_SELECTED;
   2407 		msg = " is selected";
   2408 
   2409 		if (nselected >= lsc->lsc_max_ports) {
   2410 			next_selected = LACP_STANDBY;
   2411 			msg = " is standby because of too many active ports";
   2412 		}
   2413 
   2414 		if (!lsc->lsc_multi_linkspeed &&
   2415 		    linkspeed != lacpp->lp_linkspeed) {
   2416 			next_selected = LACP_STANDBY;
   2417 			msg = " is standby because of link speed mismatch";
   2418 		}
   2419 
   2420 		if (lacpp->lp_selected != next_selected) {
   2421 			lacpp->lp_selected = next_selected;
   2422 			LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2423 			    "%s%s\n", LACP_PORT_XNAME(lacpp), msg);
   2424 		}
   2425 
   2426 		if (lacpp->lp_selected == LACP_SELECTED)
   2427 			nselected++;
   2428 	}
   2429 }
   2430 
   2431 static void
   2432 lacp_select(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2433 {
   2434 	struct lacp_aggregator *la;
   2435 	struct lacp_aggregator_systemid *sid;
   2436 	struct lacp_port *lacpp0;
   2437 	char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   2438 
   2439 	if (lacpp->lp_aggregator != NULL)
   2440 		return;
   2441 
   2442 	/* If we haven't heard from our peer, skip this step. */
   2443 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED))
   2444 		return
   2445 
   2446 	KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
   2447 
   2448 	sid = &lacpp->lp_aggregator_sidbuf;
   2449 
   2450 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
   2451 		if (lacp_aggregator_is_match(&la->la_sid, sid))
   2452 			break;
   2453 	}
   2454 
   2455 	if (la == NULL) {
   2456 		la = kmem_zalloc(sizeof(*la), KM_NOSLEEP);
   2457 		if (la == NULL) {
   2458 			LACP_DPRINTF((lsc, lacpp,
   2459 			    "couldn't allocate aggregator\n"));
   2460 			/* will retry the next tick. */
   2461 			return;
   2462 		}
   2463 		LIST_INIT(&la->la_ports);
   2464 
   2465 		la->la_sid = *sid;
   2466 		TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
   2467 		LACP_DPRINTF((lsc, lacpp, "a new aggregator created\n"));
   2468 	} else {
   2469 		LACP_DPRINTF((lsc, lacpp, "aggregator found\n"));
   2470 	}
   2471 
   2472 	KASSERT(la != NULL);
   2473 	LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
   2474 	LACP_DPRINTF((lsc, lacpp, "aggregator lagid=%s\n", buf));
   2475 
   2476 	lacpp->lp_aggregator = la;
   2477 	lacpp->lp_selected = LACP_READY;
   2478 
   2479 	LIST_FOREACH(lacpp0, &la->la_ports, lp_entry_la) {
   2480 		if (lacp_port_priority_max(lacpp0, lacpp) == lacpp) {
   2481 			LIST_INSERT_BEFORE(lacpp0, lacpp, lp_entry_la);
   2482 			break;
   2483 		}
   2484 
   2485 		if (LIST_NEXT(lacpp0, lp_entry_la) == NULL) {
   2486 			LIST_INSERT_AFTER(lacpp0, lacpp, lp_entry_la);
   2487 			break;
   2488 		}
   2489 	}
   2490 
   2491 	if (lacpp0 == NULL)
   2492 		LIST_INSERT_HEAD(&la->la_ports, lacpp, lp_entry_la);
   2493 
   2494 	lacp_selected_update(lsc, la);
   2495 }
   2496 
   2497 static void
   2498 lacp_unselect(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2499 {
   2500 	struct lacp_aggregator *la;
   2501 	char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   2502 	bool remove_actaggr;
   2503 
   2504 	KASSERT(LACP_LOCKED(lsc));
   2505 	KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
   2506 
   2507 	la = lacpp->lp_aggregator;
   2508 	lacpp->lp_selected = LACP_UNSELECTED;
   2509 
   2510 	if (la == NULL)
   2511 		return;
   2512 
   2513 	KASSERT(!LIST_EMPTY(&la->la_ports));
   2514 
   2515 	LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
   2516 	LACP_DPRINTF((lsc, lacpp, "unselect aggregator lagid=%s\n", buf));
   2517 
   2518 	LIST_REMOVE(lacpp, lp_entry_la);
   2519 	lacpp->lp_aggregator = NULL;
   2520 
   2521 	if (LIST_EMPTY(&la->la_ports)) {
   2522 		remove_actaggr = false;
   2523 
   2524 		if (la == lsc->lsc_aggregator) {
   2525 			LACP_DPRINTF((lsc, NULL, "remove active aggregator\n"));
   2526 			lsc->lsc_aggregator = NULL;
   2527 			remove_actaggr = true;
   2528 		}
   2529 
   2530 		TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
   2531 		kmem_free(la, sizeof(*la));
   2532 
   2533 		if (remove_actaggr)
   2534 			lacp_select_active_aggregator(lsc);
   2535 	} else {
   2536 		lacp_selected_update(lsc, la);
   2537 	}
   2538 }
   2539 
   2540 static void
   2541 lacp_suppress_distributing(struct lacp_softc *lsc)
   2542 {
   2543 	struct lacp_aggregator *la;
   2544 	struct lacp_port *lacpp;
   2545 
   2546 	KASSERT(LACP_LOCKED(lsc));
   2547 
   2548 	la = lsc->lsc_aggregator;
   2549 
   2550 	LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
   2551 		if (ISSET(lacpp->lp_actor.lpi_state,
   2552 		    LACP_STATE_DISTRIBUTING)) {
   2553 			lagg_workq_add(lsc->lsc_workq,
   2554 			    &lacpp->lp_work_marker);
   2555 		}
   2556 	}
   2557 
   2558 	LACP_PTIMER_ARM(lsc, LACP_PTIMER_DISTRIBUTING,
   2559 	    LACP_TRANSIT_DELAY);
   2560 }
   2561 
   2562 static void
   2563 lacp_distributing_timer(struct lacp_softc *lsc)
   2564 {
   2565 
   2566 	KASSERT(LACP_LOCKED(lsc));
   2567 
   2568 	if (lsc->lsc_suppress_distributing) {
   2569 		LACP_DPRINTF((lsc, NULL,
   2570 		    "disable suppress distributing\n"));
   2571 		lsc->lsc_suppress_distributing = false;
   2572 	}
   2573 }
   2574 
   2575 static struct mbuf *
   2576 lacp_markerdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2577 {
   2578 	struct ifnet *ifp_port;
   2579 	struct mbuf *m;
   2580 	struct markerdu *mdu;
   2581 	struct markerdu_info *mi;
   2582 
   2583 	KASSERT(LACP_LOCKED(lsc));
   2584 
   2585 	ifp_port = lacpp->lp_laggport->lp_ifp;
   2586 
   2587 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2588 	if (m == NULL) {
   2589 		lsc->lsc_mgethdr_failed.ev_count++;
   2590 		return NULL;
   2591 	}
   2592 
   2593 	m->m_pkthdr.len = m->m_len = sizeof(*mdu);
   2594 	m_reset_rcvif(m);
   2595 
   2596 	mdu = mtod(m, struct markerdu *);
   2597 
   2598 	memset(mdu, 0, sizeof(*mdu));
   2599 
   2600 	m->m_flags |= M_MCAST;
   2601 	memcpy(mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
   2602 	    ETHER_ADDR_LEN);
   2603 	memcpy(mdu->mdu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
   2604 	    ETHER_ADDR_LEN);
   2605 	mdu->mdu_eh.ether_type = ntohs(ETHERTYPE_SLOWPROTOCOLS);
   2606 	mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
   2607 	mdu->mdu_sph.sph_version = 1;
   2608 
   2609 	mi = &mdu->mdu_info;
   2610 	tlv_set(&mdu->mdu_tlv_info, MARKER_TYPE_INFO,
   2611 	    sizeof(*mi));
   2612 	mi->mi_rq_port = lacpp->lp_actor.lpi_portno;
   2613 	mi->mi_rq_xid = htonl(lacpp->lp_marker_xid);
   2614 	memcpy(mi->mi_rq_system, lsc->lsc_system_mac, LACP_MAC_LEN);
   2615 
   2616 	mdu->mdu_tlv_term.tlv_type = MARKER_TYPE_TERMINATE;
   2617 	mdu->mdu_tlv_term.tlv_length = 0;
   2618 
   2619 	return m;
   2620 }
   2621 
   2622 static void
   2623 lacp_marker_work(struct lagg_work *lw, void *xlsc)
   2624 {
   2625 	struct lacp_softc *lsc;
   2626 	struct lacp_port *lacpp;
   2627 	struct lagg_port *lp;
   2628 	struct markerdu *mdu;
   2629 	struct mbuf *m;
   2630 	struct psref psref;
   2631 	int bound;
   2632 
   2633 	lsc = xlsc;
   2634 	lacpp = container_of(lw, struct lacp_port, lp_work_marker);
   2635 
   2636 	LACP_LOCK(lsc);
   2637 	lacpp->lp_marker_xid++;
   2638 	m = lacp_markerdu_mbuf(lsc, lacpp);
   2639 	if (m == NULL) {
   2640 		LACP_UNLOCK(lsc);
   2641 		return;
   2642 	}
   2643 	SET(lacpp->lp_flags, LACP_PORT_MARK);
   2644 	lsc->lsc_suppress_distributing = true;
   2645 	lp = lacpp->lp_laggport;
   2646 	bound = curlwp_bind();
   2647 	lagg_port_getref(lp, &psref);
   2648 	LACP_UNLOCK(lsc);
   2649 
   2650 	if (LACP_ISDUMPING(lsc)) {
   2651 		lacp_dprintf(lsc, lacpp, "markerdu transmit\n");
   2652 		mdu = mtod(m, struct markerdu *);
   2653 		lacp_dump_markertlv(&mdu->mdu_info, NULL);
   2654 	}
   2655 
   2656 	lagg_port_xmit(lp, m);
   2657 	lagg_port_putref(lp, &psref);
   2658 	curlwp_bindx(bound);
   2659 }
   2660 
   2661 static void
   2662 lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *pi_actor,
   2663     const struct lacpdu_peerinfo *pi_partner,
   2664     const struct lacpdu_collectorinfo *lci)
   2665 {
   2666 	char str[LACP_STATESTR_LEN];
   2667 
   2668 	if (pi_actor != NULL) {
   2669 		lacp_peerinfo_idstr(pi_actor, str, sizeof(str));
   2670 		printf("actor=%s\n", str);
   2671 		lacp_state_str(pi_actor->lpi_state,
   2672 		    str, sizeof(str));
   2673 		printf("actor.state=%s portno=%d portprio=0x%04x\n",
   2674 		    str,
   2675 		    ntohs(pi_actor->lpi_port_no),
   2676 		    ntohs(pi_actor->lpi_port_prio));
   2677 	} else {
   2678 		printf("no actor info\n");
   2679 	}
   2680 
   2681 	if (pi_partner != NULL) {
   2682 		lacp_peerinfo_idstr(pi_partner, str, sizeof(str));
   2683 		printf("partner=%s\n", str);
   2684 		lacp_state_str(pi_partner->lpi_state,
   2685 		    str, sizeof(str));
   2686 		printf("partner.state=%s portno=%d portprio=0x%04x\n",
   2687 		    str,
   2688 		    ntohs(pi_partner->lpi_port_no),
   2689 		    ntohs(pi_partner->lpi_port_prio));
   2690 	} else {
   2691 		printf("no partner info\n");
   2692 	}
   2693 
   2694 	if (lci != NULL) {
   2695 		printf("maxdelay=%d\n", ntohs(lci->lci_maxdelay));
   2696 	} else {
   2697 		printf("no collector info\n");
   2698 	}
   2699 }
   2700 
   2701 static void
   2702 lacp_dump_markertlv(const struct markerdu_info *mi_info,
   2703     const struct markerdu_info *mi_res)
   2704 {
   2705 
   2706 	if (mi_info != NULL) {
   2707 		printf("marker info: port=%d, sys=%s, id=%u\n",
   2708 		    ntohs(mi_info->mi_rq_port),
   2709 		    ether_sprintf(mi_info->mi_rq_system),
   2710 		    ntohl(mi_info->mi_rq_xid));
   2711 	}
   2712 
   2713 	if (mi_res != NULL) {
   2714 		printf("marker resp: port=%d, sys=%s, id=%u\n",
   2715 		    ntohs(mi_res->mi_rq_port),
   2716 		    ether_sprintf(mi_res->mi_rq_system),
   2717 		    ntohl(mi_res->mi_rq_xid));
   2718 	}
   2719 }
   2720 
   2721 /*
   2722  * lacp_linkstate:
   2723  *   callback on link state changed.
   2724  *   enable, disable or reset LACP processing on the physical port.
   2725  */
   2726 
   2727 static void
   2728 lacp_linkstate(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
   2729 {
   2730 
   2731 	IFNET_ASSERT_UNLOCKED(lp->lp_ifp);
   2732 
   2733 	IFNET_LOCK(lp->lp_ifp);
   2734 	lacp_linkstate_ifnet_locked(xlsc, lp);
   2735 	IFNET_UNLOCK(lp->lp_ifp);
   2736 }
   2737 
   2738 static void
   2739 lacp_linkspeed_work(struct lagg_work *lw __unused, void *xlsc)
   2740 {
   2741 	struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
   2742 	struct lagg_softc *sc = lsc->lsc_softc;
   2743 	struct lacp_portmap *pm;
   2744 	struct lagg_port *lp;
   2745 	struct lacp_port *lacpp;
   2746 	uint64_t linkspeed;
   2747 	size_t act, i;
   2748 
   2749 	linkspeed = 0;
   2750 
   2751 	LACP_LOCK(lsc);
   2752 	act = LACP_PORTMAP_ACTIVE(lsc);
   2753 	pm = &lsc->lsc_portmaps[act];
   2754 	for (i = 0; i < pm->pm_count; i++) {
   2755 		lp = pm->pm_ports[i];
   2756 		lacpp = lp->lp_proto_ctx;
   2757 		linkspeed = MAX(linkspeed, lacpp->lp_linkspeed);
   2758 	}
   2759 	LACP_UNLOCK(lsc);
   2760 
   2761 	LAGG_LOCK(sc);
   2762 	lagg_set_linkspeed(sc, linkspeed);
   2763 	LAGG_UNLOCK(sc);
   2764 }
   2765