Home | History | Annotate | Line # | Download | only in lagg
if_lagg_lacp.c revision 1.39
      1 /*	$NetBSD: if_lagg_lacp.c,v 1.39 2024/04/05 06:11:16 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.39 2024/04/05 06:11:16 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, s0, s1;
   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, &s0);
   1303 		if (ifp == NULL) {
   1304 			m_freem(m);
   1305 			lsc->lsc_norcvif.ev_count++;
   1306 			continue;
   1307 		}
   1308 
   1309 		s1 = pserialize_read_enter();
   1310 		lp = atomic_load_consume(&ifp->if_lagg);
   1311 		if (lp == NULL) {
   1312 			pserialize_read_exit(s1);
   1313 			m_put_rcvif(ifp, &s0);
   1314 			m_freem(m);
   1315 			lsc->lsc_norcvif.ev_count++;
   1316 			continue;
   1317 		}
   1318 
   1319 		lagg_port_getref(lp, &psref_lp);
   1320 		pserialize_read_exit(s1);
   1321 		m_put_rcvif(ifp, &s0);
   1322 
   1323 		m_copydata(m, sizeof(struct ether_header),
   1324 		    sizeof(subtype), &subtype);
   1325 
   1326 		switch (subtype) {
   1327 		case SLOWPROTOCOLS_SUBTYPE_LACP:
   1328 			(void)lacp_pdu_input(lsc,
   1329 			    lp->lp_proto_ctx, m);
   1330 			break;
   1331 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
   1332 			(void)lacp_marker_input(lsc,
   1333 			    lp->lp_proto_ctx, m);
   1334 			break;
   1335 		}
   1336 
   1337 		lagg_port_putref(lp, &psref_lp);
   1338 	}
   1339 
   1340 	curlwp_bindx(bound);
   1341 }
   1342 
   1343 static bool
   1344 lacp_port_need_to_tell(struct lacp_port *lacpp)
   1345 {
   1346 
   1347 	if (!ISSET(lacpp->lp_actor.lpi_state,
   1348 	    LACP_STATE_AGGREGATION)) {
   1349 		return false;
   1350 	}
   1351 
   1352 	if (!ISSET(lacpp->lp_actor.lpi_state,
   1353 	    LACP_STATE_ACTIVITY)
   1354 	    && !ISSET(lacpp->lp_partner.lpi_state,
   1355 	    LACP_STATE_ACTIVITY)) {
   1356 		return false;
   1357 	}
   1358 
   1359 	if (!ISSET(lacpp->lp_flags, LACP_PORT_NTT))
   1360 		return false;
   1361 
   1362 	if (ppsratecheck(&lacpp->lp_last_lacpdu, &lacpp->lp_lacpdu_sent,
   1363 	    (LACP_SENDDU_PPS / LACP_FAST_PERIODIC_TIME)) == 0)
   1364 		return false;
   1365 
   1366 	return true;
   1367 }
   1368 
   1369 static void
   1370 lacp_sm_assert_ntt(struct lacp_port *lacpp)
   1371 {
   1372 
   1373 	SET(lacpp->lp_flags, LACP_PORT_NTT);
   1374 }
   1375 
   1376 static void
   1377 lacp_sm_negate_ntt(struct lacp_port *lacpp)
   1378 {
   1379 
   1380 	CLR(lacpp->lp_flags, LACP_PORT_NTT);
   1381 }
   1382 
   1383 static struct mbuf *
   1384 lacp_lacpdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1385 {
   1386 	struct ifnet *ifp_port;
   1387 	struct mbuf *m;
   1388 	struct lacpdu *du;
   1389 
   1390 	KASSERT(LACP_LOCKED(lsc));
   1391 
   1392 	ifp_port = lacpp->lp_laggport->lp_ifp;
   1393 
   1394 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1395 	if (m == NULL) {
   1396 		lsc->lsc_mgethdr_failed.ev_count++;
   1397 		return NULL;
   1398 	}
   1399 
   1400 	m->m_pkthdr.len = m->m_len = sizeof(*du);
   1401 	m_reset_rcvif(m);
   1402 
   1403 	du = mtod(m, struct lacpdu *);
   1404 	memset(du, 0, sizeof(*du));
   1405 
   1406 	m->m_flags |= M_MCAST;
   1407 	memcpy(du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
   1408 	    ETHER_ADDR_LEN);
   1409 	memcpy(du->ldu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
   1410 	    ETHER_ADDR_LEN);
   1411 	du->ldu_eh.ether_type = htons(ETHERTYPE_SLOWPROTOCOLS);
   1412 	du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
   1413 	du->ldu_sph.sph_version = 1;
   1414 
   1415 	tlv_set(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO,
   1416 	    sizeof(du->ldu_actor));
   1417 	lacp_peerinfo_actor(lsc, lacpp, &du->ldu_actor);
   1418 
   1419 	tlv_set(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
   1420 	    sizeof(du->ldu_partner));
   1421 	lacp_peerinfo_partner(lacpp, &du->ldu_partner);
   1422 
   1423 	tlv_set(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
   1424 	    sizeof(du->ldu_collector));
   1425 	du->ldu_collector.lci_maxdelay = 0;
   1426 
   1427 	du->ldu_tlv_term.tlv_type = LACP_TYPE_TERMINATE;
   1428 	du->ldu_tlv_term.tlv_length = 0;
   1429 
   1430 	return m;
   1431 }
   1432 
   1433 static void
   1434 lacp_sm_tx_work(struct lagg_work *lw, void *xlsc)
   1435 {
   1436 	struct lacp_softc *lsc;
   1437 	struct lacp_port *lacpp;
   1438 	struct lagg_port *lp;
   1439 	struct lacpdu *du;
   1440 	struct mbuf *m;
   1441 	struct psref psref;
   1442 	int bound;
   1443 
   1444 	lsc = xlsc;
   1445 	lacpp = container_of(lw, struct lacp_port, lp_work_smtx);
   1446 
   1447 	LACP_LOCK(lsc);
   1448 
   1449 	if (lsc->lsc_stop_lacpdu) {
   1450 		LACP_UNLOCK(lsc);
   1451 		return;
   1452 	}
   1453 
   1454 	m = lacp_lacpdu_mbuf(lsc, lacpp);
   1455 	if (m == NULL) {
   1456 		LACP_UNLOCK(lsc);
   1457 		return;
   1458 	}
   1459 	lacp_sm_negate_ntt(lacpp);
   1460 	lp = lacpp->lp_laggport;
   1461 	bound = curlwp_bind();
   1462 	lagg_port_getref(lp, &psref);
   1463 	LACP_UNLOCK(lsc);
   1464 
   1465 	if (LACP_ISDUMPING(lsc)) {
   1466 		lacp_dprintf(lsc, lacpp, "lacpdu transmit\n");
   1467 		du = mtod(m, struct lacpdu *);
   1468 		lacp_dump_lacpdutlv(&du->ldu_actor,
   1469 		    &du->ldu_partner, &du->ldu_collector);
   1470 	}
   1471 
   1472 	lagg_port_xmit(lp, m);
   1473 	lagg_port_putref(lp, &psref);
   1474 	curlwp_bindx(bound);
   1475 }
   1476 
   1477 static void
   1478 lacp_sm_tx(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1479 {
   1480 
   1481 	if (!lacp_port_need_to_tell(lacpp))
   1482 		return;
   1483 
   1484 	lagg_workq_add(lsc->lsc_workq, &lacpp->lp_work_smtx);
   1485 }
   1486 
   1487 static void
   1488 lacp_tick(void *xlsc)
   1489 {
   1490 	struct lacp_softc *lsc;
   1491 
   1492 	lsc = xlsc;
   1493 
   1494 	lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_tick);
   1495 
   1496 	LACP_LOCK(lsc);
   1497 	callout_schedule(&lsc->lsc_tick, hz);
   1498 	LACP_UNLOCK(lsc);
   1499 }
   1500 
   1501 static void
   1502 lacp_run_timers(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1503 {
   1504 	size_t i;
   1505 
   1506 	KASSERT(LACP_LOCKED(lsc));
   1507 
   1508 	for (i = 0; i < LACP_NTIMER; i++) {
   1509 		KASSERT(lacpp->lp_timer[i] >= 0);
   1510 
   1511 		if (lacpp->lp_timer[i] == 0)
   1512 			continue;
   1513 		if (--lacpp->lp_timer[i] > 0)
   1514 			continue;
   1515 
   1516 		KASSERT(lacp_timer_funcs[i] != NULL);
   1517 		lacp_timer_funcs[i](lsc, lacpp);
   1518 	}
   1519 }
   1520 
   1521 static void
   1522 lacp_run_prototimers(struct lacp_softc *lsc)
   1523 {
   1524 	size_t i;
   1525 
   1526 	for (i = 0; i < LACP_NPTIMER; i++) {
   1527 		KASSERT(lsc->lsc_timer[i] >= 0);
   1528 
   1529 		if (lsc->lsc_timer[i] == 0)
   1530 			continue;
   1531 		if (--lsc->lsc_timer[i] > 0)
   1532 			continue;
   1533 
   1534 		KASSERT(lacp_ptimer_funcs[i] != NULL);
   1535 		lacp_ptimer_funcs[i](lsc);
   1536 	}
   1537 }
   1538 
   1539 static void
   1540 lacp_tick_work(struct lagg_work *lw __unused, void *xlsc)
   1541 {
   1542 	struct lacp_softc *lsc;
   1543 	struct lacp_port *lacpp;
   1544 	struct lagg_softc *sc;
   1545 	struct lagg_port *lp;
   1546 
   1547 	lsc = xlsc;
   1548 	sc = lsc->lsc_softc;
   1549 
   1550 	LACP_LOCK(lsc);
   1551 	lacp_run_prototimers(lsc);
   1552 	LACP_UNLOCK(lsc);
   1553 
   1554 	LAGG_LOCK(sc);
   1555 	LACP_LOCK(lsc);
   1556 	LAGG_PORTS_FOREACH(sc, lp) {
   1557 		lacpp = lp->lp_proto_ctx;
   1558 		if (!ISSET(lacpp->lp_actor.lpi_state,
   1559 		    LACP_STATE_AGGREGATION)) {
   1560 			continue;
   1561 		}
   1562 
   1563 		lacp_run_timers(lsc, lacpp);
   1564 		lacp_select(lsc, lacpp);
   1565 		lacp_sm_mux(lsc, lacpp);
   1566 		lacp_sm_tx(lsc, lacpp);
   1567 		lacp_sm_ptx_schedule(lacpp);
   1568 	}
   1569 
   1570 	LACP_UNLOCK(lsc);
   1571 	LAGG_UNLOCK(sc);
   1572 }
   1573 
   1574 static void
   1575 lacp_systemid_str(char *buf, size_t buflen,
   1576     uint16_t prio, const uint8_t *mac, uint16_t key)
   1577 {
   1578 
   1579 	snprintf(buf, buflen,
   1580 	    "%04X,"
   1581 	    "%02X-%02X-%02X-%02X-%02X-%02X,"
   1582 	    "%04X",
   1583 	    (unsigned int)ntohs(prio),
   1584 	    (int)mac[0], (int)mac[1], (int)mac[2],
   1585 	    (int)mac[3], (int)mac[4], (int)mac[5],
   1586 	    (unsigned int)htons(key));
   1587 
   1588 }
   1589 
   1590 __LACPDEBUGUSED static void
   1591 lacp_aggregator_str(struct lacp_aggregator *la, char *buf, size_t buflen)
   1592 {
   1593 
   1594 	lacp_systemid_str(buf, buflen, la->la_sid.sid_prio,
   1595 	    la->la_sid.sid_mac, la->la_sid.sid_key);
   1596 }
   1597 
   1598 static void
   1599 lacp_peerinfo_idstr(const struct lacpdu_peerinfo *pi,
   1600     char *buf, size_t buflen)
   1601 {
   1602 
   1603 	lacp_systemid_str(buf, buflen, pi->lpi_system_prio,
   1604 	    pi->lpi_system_mac, pi->lpi_key);
   1605 }
   1606 
   1607 static void
   1608 lacp_state_str(uint8_t state, char *buf, size_t buflen)
   1609 {
   1610 
   1611 	snprintb(buf, buflen, LACP_STATE_BITS, state);
   1612 }
   1613 
   1614 static struct lagg_port *
   1615 lacp_select_tx_port(struct lacp_softc *lsc, struct mbuf *m,
   1616     struct psref *psref)
   1617 {
   1618 	struct lacp_portmap *pm;
   1619 	struct lagg_port *lp;
   1620 	uint32_t hash;
   1621 	size_t act;
   1622 	int s;
   1623 
   1624 	hash = lagg_hashmbuf(lsc->lsc_softc, m);
   1625 
   1626 	s = pserialize_read_enter();
   1627 	act = LACP_PORTMAP_ACTIVE(lsc);
   1628 	pm = &lsc->lsc_portmaps[act];
   1629 
   1630 	if (pm->pm_count == 0) {
   1631 		pserialize_read_exit(s);
   1632 		return NULL;
   1633 	}
   1634 
   1635 	hash %= pm->pm_count;
   1636 	lp = pm->pm_ports[hash];
   1637 	lagg_port_getref(lp, psref);
   1638 
   1639 	pserialize_read_exit(s);
   1640 
   1641 	return lp;
   1642 }
   1643 
   1644 static void
   1645 lacp_peerinfo_actor(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1646     struct lacpdu_peerinfo *dst)
   1647 {
   1648 
   1649 	memcpy(dst->lpi_system_mac, lsc->lsc_system_mac, LACP_MAC_LEN);
   1650 	dst->lpi_system_prio = lsc->lsc_system_prio;
   1651 	dst->lpi_key = lsc->lsc_key;
   1652 	dst->lpi_port_no = lacpp->lp_actor.lpi_portno;
   1653 	dst->lpi_port_prio = lacpp->lp_actor.lpi_portprio;
   1654 	dst->lpi_state = lacpp->lp_actor.lpi_state;
   1655 }
   1656 
   1657 static void
   1658 lacp_peerinfo_partner(struct lacp_port *lacpp, struct lacpdu_peerinfo *dst)
   1659 {
   1660 	struct lacp_aggregator *la;
   1661 
   1662 	la = lacpp->lp_aggregator;
   1663 
   1664 	if (la != NULL) {
   1665 		memcpy(dst->lpi_system_mac, la->la_sid.sid_mac, LACP_MAC_LEN);
   1666 		dst->lpi_system_prio = la->la_sid.sid_prio;
   1667 		dst->lpi_key = la->la_sid.sid_key;
   1668 	} else {
   1669 		memset(dst->lpi_system_mac, 0, LACP_MAC_LEN);
   1670 		dst->lpi_system_prio = 0;
   1671 		dst->lpi_key = 0;
   1672 	}
   1673 	dst->lpi_port_no = lacpp->lp_partner.lpi_portno;
   1674 	dst->lpi_port_prio = lacpp->lp_partner.lpi_portprio;
   1675 	dst->lpi_state = lacpp->lp_partner.lpi_state;
   1676 }
   1677 
   1678 static int
   1679 lacp_compare_peerinfo(struct lacpdu_peerinfo *a, struct lacpdu_peerinfo *b)
   1680 {
   1681 
   1682 	return memcmp(a, b, offsetof(struct lacpdu_peerinfo, lpi_state));
   1683 }
   1684 
   1685 static void
   1686 lacp_sm_rx_record_default(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1687 {
   1688 	uint8_t oldpstate;
   1689 	struct lacp_portinfo *pi;
   1690 	char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
   1691 
   1692 	pi = &lacpp->lp_partner;
   1693 
   1694 	oldpstate = pi->lpi_state;
   1695 	pi->lpi_portno = htons(LACP_PORTNO_NONE);
   1696 	pi->lpi_portprio = htons(0xffff);
   1697 
   1698 	if (lsc->lsc_optimistic)
   1699 		pi->lpi_state = LACP_PARTNER_ADMIN_OPTIMISTIC;
   1700 	else
   1701 		pi->lpi_state = LACP_PARTNER_ADMIN_STRICT;
   1702 
   1703 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
   1704 
   1705 	if (oldpstate != pi->lpi_state) {
   1706 		LACP_STATE_STR(oldpstate, buf, sizeof(buf));
   1707 		LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
   1708 
   1709 		LACP_STATE_STR(pi->lpi_state, buf, sizeof(buf));
   1710 		LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
   1711 	}
   1712 
   1713 	lacp_sm_ptx_update_timeout(lacpp, oldpstate);
   1714 }
   1715 
   1716 static inline bool
   1717 lacp_port_is_synced(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1718     struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
   1719 {
   1720 	struct lacpdu_peerinfo actor;
   1721 
   1722 	if (!ISSET(peer_pi->lpi_state, LACP_STATE_ACTIVITY) &&
   1723 	    (!ISSET(my_pi->lpi_state, LACP_STATE_ACTIVITY) ||
   1724 	    !ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY)))
   1725 		return false;
   1726 
   1727 	if (!ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION))
   1728 		return false;
   1729 
   1730 	lacp_peerinfo_actor(lsc, lacpp, &actor);
   1731 	if (lacp_compare_peerinfo(&actor, my_pi) != 0)
   1732 		return false;
   1733 
   1734 	if (!LACP_STATE_EQ(actor.lpi_state, my_pi->lpi_state,
   1735 	    LACP_STATE_AGGREGATION)) {
   1736 		return false;
   1737 	}
   1738 
   1739 	return true;
   1740 }
   1741 
   1742 static void
   1743 lacp_sm_rx_record_peerinfo(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1744     struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
   1745 {
   1746 	char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
   1747 	uint8_t oldpstate;
   1748 	struct lacp_portinfo *pi;
   1749 	struct lacp_aggregator_systemid *sid;
   1750 
   1751 	pi = &lacpp->lp_partner;
   1752 	sid = &lacpp->lp_aggregator_sidbuf;
   1753 
   1754 	oldpstate = lacpp->lp_partner.lpi_state;
   1755 
   1756 	sid->sid_prio = peer_pi->lpi_system_prio;
   1757 	sid->sid_key = peer_pi->lpi_key;
   1758 	memcpy(sid->sid_mac, peer_pi->lpi_system_mac,
   1759 	    sizeof(sid->sid_mac));
   1760 
   1761 	pi->lpi_portno = peer_pi->lpi_port_no;
   1762 	pi->lpi_portprio = peer_pi->lpi_port_prio;
   1763 	pi->lpi_state = peer_pi->lpi_state;
   1764 
   1765 	if (lacp_port_is_synced(lsc, lacpp, my_pi, peer_pi)) {
   1766 		if (lsc->lsc_optimistic)
   1767 			SET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   1768 	} else {
   1769 		CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   1770 	}
   1771 
   1772 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
   1773 
   1774 	if (oldpstate != lacpp->lp_partner.lpi_state) {
   1775 		LACP_STATE_STR(oldpstate, buf, sizeof(buf));
   1776 		LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
   1777 
   1778 		LACP_STATE_STR(lacpp->lp_partner.lpi_state,
   1779 		    buf, sizeof(buf));
   1780 		LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
   1781 	}
   1782 
   1783 	lacp_sm_ptx_update_timeout(lacpp, oldpstate);
   1784 }
   1785 
   1786 static void
   1787 lacp_sm_rx_set_expired(struct lacp_port *lacpp)
   1788 {
   1789 	uint8_t oldpstate;
   1790 
   1791 	oldpstate = lacpp->lp_partner.lpi_state;
   1792 
   1793 	CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   1794 	SET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT);
   1795 	LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE,
   1796 	    LACP_SHORT_TIMEOUT_TIME);
   1797 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
   1798 
   1799 	lacp_sm_ptx_update_timeout(lacpp, oldpstate);
   1800 }
   1801 
   1802 static void
   1803 lacp_sm_port_init(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1804     struct lagg_port *lp)
   1805 {
   1806 
   1807 	KASSERT(LACP_LOCKED(lsc));
   1808 
   1809 	lacpp->lp_laggport = lp;
   1810 	lacpp->lp_actor.lpi_state = LACP_STATE_ACTIVITY;
   1811 	lacpp->lp_actor.lpi_portno = htons(if_get_index(lp->lp_ifp));
   1812 	lacpp->lp_actor.lpi_portprio = htons(LACP_PORT_PRIO);
   1813 	lacpp->lp_partner.lpi_state = LACP_STATE_TIMEOUT;
   1814 	lacpp->lp_aggregator = NULL;
   1815 	lacpp->lp_marker_xid = 0;
   1816 	lacpp->lp_mux_state = LACP_MUX_INIT;
   1817 
   1818 	lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
   1819 	lacp_sm_rx_record_default(lsc, lacpp);
   1820 }
   1821 
   1822 static void
   1823 lacp_port_disable(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1824 {
   1825 
   1826 	KASSERT(LACP_LOCKED(lsc));
   1827 
   1828 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
   1829 		LACP_DPRINTF((lsc, lacpp, "enable -> disable\n"));
   1830 
   1831 	lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
   1832 	lacp_sm_rx_record_default(lsc, lacpp);
   1833 	CLR(lacpp->lp_actor.lpi_state,
   1834 	    LACP_STATE_AGGREGATION | LACP_STATE_EXPIRED);
   1835 	CLR(lacpp->lp_partner.lpi_state, LACP_STATE_AGGREGATION);
   1836 }
   1837 
   1838 static void
   1839 lacp_port_enable(struct lacp_softc *lsc __LACPDEBUGUSED,
   1840     struct lacp_port *lacpp)
   1841 {
   1842 
   1843 	KASSERT(LACP_LOCKED(lsc));
   1844 
   1845 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
   1846 		LACP_DPRINTF((lsc, lacpp, "disable -> enable\n"));
   1847 
   1848 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION);
   1849 	lacp_sm_rx_set_expired(lacpp);
   1850 }
   1851 
   1852 static void
   1853 lacp_sm_rx_timer(struct lacp_softc *lsc, struct lacp_port *lacpp)
   1854 {
   1855 
   1856 	KASSERT(LACP_LOCKED(lsc));
   1857 
   1858 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED)) {
   1859 		/* CURRENT -> EXPIRED */
   1860 		LACP_DPRINTF((lsc, lacpp, "CURRENT -> EXPIRED\n"));
   1861 		lacp_sm_rx_set_expired(lacpp);
   1862 	} else {
   1863 		LACP_DPRINTF((lsc, lacpp, "EXPIRED -> DEFAULTED\n"));
   1864 		lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
   1865 		lacp_sm_rx_record_default(lsc, lacpp);
   1866 	}
   1867 }
   1868 
   1869 static void
   1870 lacp_sm_ptx_timer(struct lacp_softc *lsc __unused, struct lacp_port *lacpp)
   1871 {
   1872 
   1873 	KASSERT(LACP_LOCKED(lsc));
   1874 	lacp_sm_assert_ntt(lacpp);
   1875 }
   1876 
   1877 static void
   1878 lacp_sm_ptx_schedule(struct lacp_port *lacpp)
   1879 {
   1880 	int timeout;
   1881 
   1882 	/* no periodic */
   1883 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY) &&
   1884 	    !ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_ACTIVITY)) {
   1885 		LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
   1886 		return;
   1887 	}
   1888 
   1889 	if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_PERIODIC))
   1890 		return;
   1891 
   1892 	timeout = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT) ?
   1893 		LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
   1894 
   1895 	LACP_TIMER_ARM(lacpp, LACP_TIMER_PERIODIC, timeout);
   1896 }
   1897 
   1898 static void
   1899 lacp_sm_ptx_update_timeout(struct lacp_port *lacpp, uint8_t oldpstate)
   1900 {
   1901 
   1902 	if (LACP_STATE_EQ(oldpstate, lacpp->lp_partner.lpi_state,
   1903 	    LACP_STATE_TIMEOUT))
   1904 		return;
   1905 
   1906 	LACP_DPRINTF((NULL, lacpp, "partner timeout changed\n"));
   1907 
   1908 	LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
   1909 
   1910 	/* if timeout has been shorted, assert NTT */
   1911 	if (ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT))
   1912 		lacp_sm_assert_ntt(lacpp);
   1913 }
   1914 
   1915 static void
   1916 lacp_sm_mux_timer(struct lacp_softc *lsc __LACPDEBUGUSED,
   1917     struct lacp_port *lacpp)
   1918 {
   1919 	char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   1920 
   1921 	KASSERT(LACP_LOCKED(lsc));
   1922 	KASSERT(lacpp->lp_pending > 0);
   1923 
   1924 	LACP_AGGREGATOR_STR(lacpp->lp_aggregator, buf, sizeof(buf));
   1925 	LACP_DPRINTF((lsc, lacpp, "aggregator %s, pending %d -> %d\n",
   1926 	    buf, lacpp->lp_pending, lacpp->lp_pending -1));
   1927 
   1928 	lacpp->lp_pending--;
   1929 }
   1930 
   1931 static void
   1932 lacp_sm_rx_update_selected(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1933     struct lacpdu_peerinfo *peer_pi)
   1934 {
   1935 	struct lacpdu_peerinfo partner;
   1936 	char str0[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   1937 	char str1[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   1938 
   1939 	if (lacpp->lp_aggregator == NULL)
   1940 		return;
   1941 
   1942 	lacp_peerinfo_partner(lacpp, &partner);
   1943 	if (lacp_compare_peerinfo(peer_pi, &partner) != 0) {
   1944 		LACP_PEERINFO_IDSTR(&partner, str0, sizeof(str0));
   1945 		LACP_PEERINFO_IDSTR(peer_pi, str1, sizeof(str1));
   1946 		LACP_DPRINTF((lsc, lacpp,
   1947 		    "different peerinfo, %s vs %s\n", str0, str1));
   1948 		goto do_unselect;
   1949 	}
   1950 
   1951 	if (!LACP_STATE_EQ(lacpp->lp_partner.lpi_state,
   1952 	    peer_pi->lpi_state, LACP_STATE_AGGREGATION)) {
   1953 		LACP_DPRINTF((lsc, lacpp,
   1954 		    "STATE_AGGREGATION changed %d -> %d\n",
   1955 		    ISSET(lacpp->lp_partner.lpi_state,
   1956 		    LACP_STATE_AGGREGATION) != 0,
   1957 		    ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION) != 0));
   1958 		goto do_unselect;
   1959 	}
   1960 
   1961 	return;
   1962 
   1963 do_unselect:
   1964 	lacpp->lp_selected = LACP_UNSELECTED;
   1965 	/* lacpp->lp_aggregator will be released at lacp_set_mux() */
   1966 }
   1967 
   1968 static void
   1969 lacp_sm_rx_update_ntt(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1970     struct lacpdu_peerinfo *my_pi)
   1971 {
   1972 	struct lacpdu_peerinfo actor;
   1973 
   1974 	lacp_peerinfo_actor(lsc, lacpp, &actor);
   1975 
   1976 	if (lacp_compare_peerinfo(&actor, my_pi) != 0 ||
   1977 	    !LACP_STATE_EQ(lacpp->lp_actor.lpi_state, my_pi->lpi_state,
   1978 	    LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
   1979 		LACP_DPRINTF((lsc, lacpp, "assert ntt\n"));
   1980 		lacp_sm_assert_ntt(lacpp);
   1981 	}
   1982 }
   1983 
   1984 static void
   1985 lacp_sm_rx(struct lacp_softc *lsc, struct lacp_port *lacpp,
   1986     struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
   1987 {
   1988 	int timeout;
   1989 
   1990 	KASSERT(LACP_LOCKED(lsc));
   1991 
   1992 	/* check LACP disabled first */
   1993 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
   1994 		return;
   1995 
   1996 	/* check loopback condition */
   1997 	if (memcmp(lsc->lsc_system_mac, peer_pi->lpi_system_mac,
   1998 	    LACP_MAC_LEN) == 0 &&
   1999 	    lsc->lsc_system_prio == peer_pi->lpi_system_prio)
   2000 		return;
   2001 
   2002 	lacp_sm_rx_update_selected(lsc, lacpp, peer_pi);
   2003 	lacp_sm_rx_update_ntt(lsc, lacpp, my_pi);
   2004 	lacp_sm_rx_record_peerinfo(lsc, lacpp, my_pi, peer_pi);
   2005 
   2006 	timeout = ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_TIMEOUT) ?
   2007 	    LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
   2008 	LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE, timeout);
   2009 
   2010 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
   2011 
   2012 	/* kick transmit machine without timeout. */
   2013 	lacp_sm_tx(lsc, lacpp);
   2014 }
   2015 
   2016 static void
   2017 lacp_disable_collecting(struct lacp_port *lacpp)
   2018 {
   2019 
   2020 	LACP_DPRINTF((NULL, lacpp, "collecting disabled\n"));
   2021 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
   2022 	atomic_store_relaxed(&lacpp->lp_collector, false);
   2023 }
   2024 
   2025 static void
   2026 lacp_enable_collecting(struct lacp_port *lacpp)
   2027 {
   2028 	LACP_DPRINTF((NULL, lacpp, "collecting enabled\n"));
   2029 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
   2030 	atomic_store_relaxed(&lacpp->lp_collector, true);
   2031 }
   2032 
   2033 static void
   2034 lacp_update_portmap(struct lacp_softc *lsc)
   2035 {
   2036 	struct lagg_softc *sc;
   2037 	struct lacp_aggregator *la;
   2038 	struct lacp_portmap *pm_act, *pm_next;
   2039 	struct lacp_port *lacpp;
   2040 	size_t pmap, n;
   2041 	u_int link;
   2042 
   2043 	KASSERT(LACP_LOCKED(lsc));
   2044 
   2045 	la = lsc->lsc_aggregator;
   2046 
   2047 	pmap = LACP_PORTMAP_ACTIVE(lsc);
   2048 	pm_act = &lsc->lsc_portmaps[pmap];
   2049 
   2050 	pmap = LACP_PORTMAP_NEXT(lsc);
   2051 	pm_next = &lsc->lsc_portmaps[pmap];
   2052 
   2053 	n = 0;
   2054 	if (la != NULL) {
   2055 		LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
   2056 			if (!ISSET(lacpp->lp_actor.lpi_state,
   2057 			    LACP_STATE_DISTRIBUTING)) {
   2058 				continue;
   2059 			}
   2060 
   2061 			pm_next->pm_ports[n] = lacpp->lp_laggport;
   2062 			n++;
   2063 
   2064 			if (n >= LACP_MAX_PORTS)
   2065 				break;
   2066 		}
   2067 	}
   2068 	pm_next->pm_count = n;
   2069 
   2070 	atomic_store_release(&lsc->lsc_activemap, pmap);
   2071 	pserialize_perform(lsc->lsc_psz);
   2072 
   2073 	LACP_DPRINTF((lsc, NULL, "portmap count updated (%zu -> %zu)\n",
   2074 	    pm_act->pm_count, pm_next->pm_count));
   2075 
   2076 	link = lacp_portmap_linkstate(pm_next);
   2077 	if (link != lacp_portmap_linkstate(pm_act)) {
   2078 		sc = lsc->lsc_softc;
   2079 		if_link_state_change(&sc->sc_if, link);
   2080 	}
   2081 
   2082 	lagg_workq_add(lsc->lsc_workq,  &lsc->lsc_work_linkspeed);
   2083 
   2084 	/* cleanup */
   2085 	pm_act->pm_count = 0;
   2086 	memset(pm_act->pm_ports, 0, sizeof(pm_act->pm_ports));
   2087 }
   2088 
   2089 static void
   2090 lacp_disable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2091 {
   2092 	struct lacp_portmap *pm;
   2093 	bool do_update;
   2094 	size_t act, i;
   2095 	int s;
   2096 
   2097 	KASSERT(LACP_LOCKED(lsc));
   2098 
   2099 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING)) {
   2100 		LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2101 		    "disable distributing on %s\n", LACP_PORT_XNAME(lacpp));
   2102 		CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
   2103 	}
   2104 
   2105 	s = pserialize_read_enter();
   2106 	act = LACP_PORTMAP_ACTIVE(lsc);
   2107 	pm = &lsc->lsc_portmaps[act];
   2108 
   2109 	do_update = false;
   2110 	for (i = 0; i < pm->pm_count; i++) {
   2111 		if (pm->pm_ports[i] == lacpp->lp_laggport) {
   2112 			do_update = true;
   2113 			break;
   2114 		}
   2115 	}
   2116 	pserialize_read_exit(s);
   2117 
   2118 	if (do_update)
   2119 		lacp_update_portmap(lsc);
   2120 }
   2121 
   2122 static void
   2123 lacp_enable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2124 {
   2125 
   2126 	KASSERT(LACP_LOCKED(lsc));
   2127 
   2128 	KASSERT(lacp_isactive(lsc, lacpp));
   2129 
   2130 	LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2131 	    "enable distributing on %s\n", LACP_PORT_XNAME(lacpp));
   2132 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
   2133 	lacp_suppress_distributing(lsc);
   2134 	lacp_update_portmap(lsc);
   2135 }
   2136 
   2137 static void
   2138 lacp_select_active_aggregator(struct lacp_softc *lsc)
   2139 {
   2140 	struct lacp_aggregator *la, *best_la;
   2141 	char str[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   2142 
   2143 	KASSERT(LACP_LOCKED(lsc));
   2144 
   2145 	la = lsc->lsc_aggregator;
   2146 	if (la != NULL && la->la_attached_port > 0) {
   2147 		best_la = la;
   2148 	} else {
   2149 		best_la = NULL;
   2150 	}
   2151 
   2152 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
   2153 		if (la->la_attached_port <= 0)
   2154 			continue;
   2155 
   2156 		if (best_la == NULL ||
   2157 		    LACP_SYS_PRI(la) < LACP_SYS_PRI(best_la))
   2158 			best_la = la;
   2159 	}
   2160 
   2161 	if (best_la != lsc->lsc_aggregator) {
   2162 		LACP_DPRINTF((lsc, NULL, "active aggregator changed\n"));
   2163 
   2164 		if (lsc->lsc_aggregator != NULL) {
   2165 			LACP_AGGREGATOR_STR(lsc->lsc_aggregator,
   2166 			    str, sizeof(str));
   2167 		} else {
   2168 			snprintf(str, sizeof(str), "(null)");
   2169 		}
   2170 		LACP_DPRINTF((lsc, NULL, "old aggregator=%s\n", str));
   2171 
   2172 		if (best_la != NULL) {
   2173 			LACP_AGGREGATOR_STR(best_la, str, sizeof(str));
   2174 		} else {
   2175 			snprintf(str, sizeof(str), "(null)");
   2176 		}
   2177 		LACP_DPRINTF((lsc, NULL, "new aggregator=%s\n", str));
   2178 
   2179 		lsc->lsc_aggregator = best_la;
   2180 	}
   2181 }
   2182 
   2183 static void
   2184 lacp_port_attached(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2185 {
   2186 	struct lacp_aggregator *la;
   2187 
   2188 	KASSERT(LACP_LOCKED(lsc));
   2189 
   2190 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
   2191 		return;
   2192 
   2193 	la = lacpp->lp_aggregator;
   2194 	KASSERT(la != NULL);
   2195 	KASSERT(la->la_attached_port >= 0);
   2196 
   2197 	SET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
   2198 	la->la_attached_port++;
   2199 	lacp_select_active_aggregator(lsc);
   2200 }
   2201 
   2202 static void
   2203 lacp_port_detached(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2204 {
   2205 	struct lacp_aggregator *la;
   2206 
   2207 	KASSERT(LACP_LOCKED(lsc));
   2208 
   2209 	if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
   2210 		return;
   2211 
   2212 	la = lacpp->lp_aggregator;
   2213 	KASSERT(la != NULL);
   2214 	KASSERT(la->la_attached_port > 0);
   2215 
   2216 	CLR(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
   2217 	la->la_attached_port--;
   2218 	lacp_select_active_aggregator(lsc);
   2219 }
   2220 
   2221 static int
   2222 lacp_set_mux(struct lacp_softc *lsc, struct lacp_port *lacpp,
   2223     enum lacp_mux_state new_state)
   2224 {
   2225 	struct lagg_softc *sc;
   2226 	struct ifnet *ifp;
   2227 
   2228 	KASSERT(LACP_LOCKED(lsc));
   2229 
   2230 	sc = lacpp->lp_laggport->lp_softc;
   2231 	ifp = &sc->sc_if;
   2232 
   2233 	if (lacpp->lp_mux_state == new_state)
   2234 		return -1;
   2235 
   2236 	switch (new_state) {
   2237 	case LACP_MUX_DETACHED:
   2238 		lacp_port_detached(lsc, lacpp);
   2239 		lacp_disable_distributing(lsc, lacpp);
   2240 		lacp_disable_collecting(lacpp);
   2241 		lacp_sm_assert_ntt(lacpp);
   2242 		/* cancel timer */
   2243 		if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)) {
   2244 			KASSERT(lacpp->lp_pending > 0);
   2245 			lacpp->lp_pending--;
   2246 			LACP_TIMER_DISARM(lacpp, LACP_TIMER_WAIT_WHILE);
   2247 		}
   2248 		lacp_unselect(lsc, lacpp);
   2249 		break;
   2250 	case LACP_MUX_WAITING:
   2251 		LACP_TIMER_ARM(lacpp, LACP_TIMER_WAIT_WHILE,
   2252 		    LACP_AGGREGATE_WAIT_TIME);
   2253 		lacpp->lp_pending++;
   2254 		break;
   2255 	case LACP_MUX_STANDBY:
   2256 #ifdef LACP_STANDBY_SYNCED
   2257 		lacp_port_attached(lsc, lacpp);
   2258 		lacp_disable_collecting(lacpp);
   2259 		lacp_sm_assert_ntt(lacpp);
   2260 #endif
   2261 		break;
   2262 	case LACP_MUX_ATTACHED:
   2263 		lacp_port_attached(lsc, lacpp);
   2264 		lacp_disable_collecting(lacpp);
   2265 		lacp_sm_assert_ntt(lacpp);
   2266 		break;
   2267 	case LACP_MUX_COLLECTING:
   2268 		lacp_enable_collecting(lacpp);
   2269 		lacp_disable_distributing(lsc, lacpp);
   2270 		lacp_sm_assert_ntt(lacpp);
   2271 		break;
   2272 	case LACP_MUX_DISTRIBUTING:
   2273 		lacp_enable_distributing(lsc, lacpp);
   2274 		break;
   2275 	case LACP_MUX_INIT:
   2276 	default:
   2277 		panic("%s: unknown state", ifp->if_xname);
   2278 	}
   2279 
   2280 	LACP_DPRINTF((lsc, lacpp, "mux_state %d -> %d\n",
   2281 	    lacpp->lp_mux_state, new_state));
   2282 
   2283 	lacpp->lp_mux_state = new_state;
   2284 	return 0;
   2285 }
   2286 
   2287 static void
   2288 lacp_sm_mux(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2289 {
   2290 	struct lacp_aggregator *la __diagused;
   2291 	enum lacp_mux_state  next_state;
   2292 	enum lacp_selected selected;
   2293 	bool p_sync, p_collecting;
   2294 
   2295 	p_sync = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
   2296 	p_collecting = ISSET(lacpp->lp_partner.lpi_state,
   2297 	    LACP_STATE_COLLECTING);
   2298 
   2299 	do {
   2300 		next_state = lacpp->lp_mux_state;
   2301 		la = lacpp->lp_aggregator;
   2302 		selected = lacpp->lp_selected;
   2303 		KASSERT(la != NULL ||
   2304 		    lacpp->lp_mux_state == LACP_MUX_DETACHED);
   2305 
   2306 		switch (lacpp->lp_mux_state) {
   2307 		case LACP_MUX_DETACHED:
   2308 			if (selected != LACP_UNSELECTED)
   2309 				next_state = LACP_MUX_WAITING;
   2310 			break;
   2311 		case LACP_MUX_WAITING:
   2312 			KASSERTMSG((lacpp->lp_pending > 0 ||
   2313 			    !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)),
   2314 			    "lp_pending=%d, timer=%d", lacpp->lp_pending,
   2315 			    !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
   2316 
   2317 			if (selected == LACP_UNSELECTED) {
   2318 				next_state = LACP_MUX_DETACHED;
   2319 			} else if (lacpp->lp_pending == 0) {
   2320 				if (selected == LACP_SELECTED) {
   2321 					next_state = LACP_MUX_ATTACHED;
   2322 				} else if (selected == LACP_STANDBY) {
   2323 					next_state = LACP_MUX_STANDBY;
   2324 				} else {
   2325 					next_state = LACP_MUX_DETACHED;
   2326 				}
   2327 			}
   2328 			break;
   2329 		case LACP_MUX_STANDBY:
   2330 			if (selected == LACP_SELECTED) {
   2331 				next_state = LACP_MUX_ATTACHED;
   2332 			} else if (selected != LACP_STANDBY) {
   2333 				next_state = LACP_MUX_DETACHED;
   2334 			}
   2335 			break;
   2336 		case LACP_MUX_ATTACHED:
   2337 			if (selected != LACP_SELECTED) {
   2338 				if (selected == LACP_STANDBY)
   2339 					LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2340 					    "detaching %s\n",
   2341 					    LACP_PORT_XNAME(lacpp));
   2342 				next_state = LACP_MUX_DETACHED;
   2343 			} else if (lacp_isactive(lsc, lacpp) && p_sync) {
   2344 				next_state = LACP_MUX_COLLECTING;
   2345 			}
   2346 			break;
   2347 		case LACP_MUX_COLLECTING:
   2348 			if (selected != LACP_SELECTED ||
   2349 			    !lacp_isactive(lsc, lacpp)
   2350 			    || !p_sync) {
   2351 				next_state = LACP_MUX_ATTACHED;
   2352 			} else if (p_collecting) {
   2353 				next_state = LACP_MUX_DISTRIBUTING;
   2354 			}
   2355 			break;
   2356 		case LACP_MUX_DISTRIBUTING:
   2357 			if (selected != LACP_SELECTED ||
   2358 			    !lacp_isactive(lsc, lacpp)
   2359 			    || !p_sync || !p_collecting) {
   2360 				next_state = LACP_MUX_COLLECTING;
   2361 				LACP_DPRINTF((lsc, lacpp,
   2362 				    "Interface stopped DISTRIBUTING,"
   2363 				    " possible flapping\n"));
   2364 			}
   2365 			break;
   2366 		case LACP_MUX_INIT:
   2367 		default:
   2368 			panic("%s: unknown state",
   2369 			    lsc->lsc_softc->sc_if.if_xname);
   2370 		}
   2371 	} while (lacp_set_mux(lsc, lacpp, next_state) == 0);
   2372 }
   2373 
   2374 static bool
   2375 lacp_aggregator_is_match(struct lacp_aggregator_systemid *a,
   2376 struct lacp_aggregator_systemid *b)
   2377 {
   2378 
   2379 	if (a->sid_prio != b->sid_prio)
   2380 		return false;
   2381 
   2382 	if (a->sid_key != b->sid_key)
   2383 		return false;
   2384 
   2385 	if (memcmp(a->sid_mac, b->sid_mac, sizeof(a->sid_mac)) != 0)
   2386 		return false;
   2387 
   2388 	return true;
   2389 }
   2390 
   2391 static void
   2392 lacp_selected_update(struct lacp_softc *lsc, struct lacp_aggregator *la)
   2393 {
   2394 	struct lacp_port *lacpp;
   2395 	enum lacp_selected next_selected;
   2396 	const char *msg;
   2397 	size_t nselected;
   2398 	uint64_t linkspeed;
   2399 
   2400 	KASSERT(LACP_LOCKED(lsc));
   2401 
   2402 	lacpp = LIST_FIRST(&la->la_ports);
   2403 	if (lacpp == NULL)
   2404 		return;
   2405 
   2406 	linkspeed = lacpp->lp_linkspeed;
   2407 	nselected = 0;
   2408 	LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
   2409 		if (lacpp->lp_selected == LACP_UNSELECTED)
   2410 			continue;
   2411 
   2412 		next_selected = LACP_SELECTED;
   2413 		msg = " is selected";
   2414 
   2415 		if (nselected >= lsc->lsc_max_ports) {
   2416 			next_selected = LACP_STANDBY;
   2417 			msg = " is standby because of too many active ports";
   2418 		}
   2419 
   2420 		if (!lsc->lsc_multi_linkspeed &&
   2421 		    linkspeed != lacpp->lp_linkspeed) {
   2422 			next_selected = LACP_STANDBY;
   2423 			msg = " is standby because of link speed mismatch";
   2424 		}
   2425 
   2426 		if (lacpp->lp_selected != next_selected) {
   2427 			lacpp->lp_selected = next_selected;
   2428 			LAGG_LOG(lsc->lsc_softc, LOG_INFO,
   2429 			    "%s%s\n", LACP_PORT_XNAME(lacpp), msg);
   2430 		}
   2431 
   2432 		if (lacpp->lp_selected == LACP_SELECTED)
   2433 			nselected++;
   2434 	}
   2435 }
   2436 
   2437 static void
   2438 lacp_select(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2439 {
   2440 	struct lacp_aggregator *la;
   2441 	struct lacp_aggregator_systemid *sid;
   2442 	struct lacp_port *lacpp0;
   2443 	char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   2444 
   2445 	if (lacpp->lp_aggregator != NULL)
   2446 		return;
   2447 
   2448 	/* If we haven't heard from our peer, skip this step. */
   2449 	if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED))
   2450 		return
   2451 
   2452 	KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
   2453 
   2454 	sid = &lacpp->lp_aggregator_sidbuf;
   2455 
   2456 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
   2457 		if (lacp_aggregator_is_match(&la->la_sid, sid))
   2458 			break;
   2459 	}
   2460 
   2461 	if (la == NULL) {
   2462 		la = kmem_zalloc(sizeof(*la), KM_NOSLEEP);
   2463 		if (la == NULL) {
   2464 			LACP_DPRINTF((lsc, lacpp,
   2465 			    "couldn't allocate aggregator\n"));
   2466 			/* will retry the next tick. */
   2467 			return;
   2468 		}
   2469 		LIST_INIT(&la->la_ports);
   2470 
   2471 		la->la_sid = *sid;
   2472 		TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
   2473 		LACP_DPRINTF((lsc, lacpp, "a new aggregator created\n"));
   2474 	} else {
   2475 		LACP_DPRINTF((lsc, lacpp, "aggregator found\n"));
   2476 	}
   2477 
   2478 	KASSERT(la != NULL);
   2479 	LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
   2480 	LACP_DPRINTF((lsc, lacpp, "aggregator lagid=%s\n", buf));
   2481 
   2482 	lacpp->lp_aggregator = la;
   2483 	lacpp->lp_selected = LACP_READY;
   2484 
   2485 	LIST_FOREACH(lacpp0, &la->la_ports, lp_entry_la) {
   2486 		if (lacp_port_priority_max(lacpp0, lacpp) == lacpp) {
   2487 			LIST_INSERT_BEFORE(lacpp0, lacpp, lp_entry_la);
   2488 			break;
   2489 		}
   2490 
   2491 		if (LIST_NEXT(lacpp0, lp_entry_la) == NULL) {
   2492 			LIST_INSERT_AFTER(lacpp0, lacpp, lp_entry_la);
   2493 			break;
   2494 		}
   2495 	}
   2496 
   2497 	if (lacpp0 == NULL)
   2498 		LIST_INSERT_HEAD(&la->la_ports, lacpp, lp_entry_la);
   2499 
   2500 	lacp_selected_update(lsc, la);
   2501 }
   2502 
   2503 static void
   2504 lacp_unselect(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2505 {
   2506 	struct lacp_aggregator *la;
   2507 	char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
   2508 	bool remove_actaggr;
   2509 
   2510 	KASSERT(LACP_LOCKED(lsc));
   2511 	KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
   2512 
   2513 	la = lacpp->lp_aggregator;
   2514 	lacpp->lp_selected = LACP_UNSELECTED;
   2515 
   2516 	if (la == NULL)
   2517 		return;
   2518 
   2519 	KASSERT(!LIST_EMPTY(&la->la_ports));
   2520 
   2521 	LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
   2522 	LACP_DPRINTF((lsc, lacpp, "unselect aggregator lagid=%s\n", buf));
   2523 
   2524 	LIST_REMOVE(lacpp, lp_entry_la);
   2525 	lacpp->lp_aggregator = NULL;
   2526 
   2527 	if (LIST_EMPTY(&la->la_ports)) {
   2528 		remove_actaggr = false;
   2529 
   2530 		if (la == lsc->lsc_aggregator) {
   2531 			LACP_DPRINTF((lsc, NULL, "remove active aggregator\n"));
   2532 			lsc->lsc_aggregator = NULL;
   2533 			remove_actaggr = true;
   2534 		}
   2535 
   2536 		TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
   2537 		kmem_free(la, sizeof(*la));
   2538 
   2539 		if (remove_actaggr)
   2540 			lacp_select_active_aggregator(lsc);
   2541 	} else {
   2542 		lacp_selected_update(lsc, la);
   2543 	}
   2544 }
   2545 
   2546 static void
   2547 lacp_suppress_distributing(struct lacp_softc *lsc)
   2548 {
   2549 	struct lacp_aggregator *la;
   2550 	struct lacp_port *lacpp;
   2551 
   2552 	KASSERT(LACP_LOCKED(lsc));
   2553 
   2554 	la = lsc->lsc_aggregator;
   2555 
   2556 	LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
   2557 		if (ISSET(lacpp->lp_actor.lpi_state,
   2558 		    LACP_STATE_DISTRIBUTING)) {
   2559 			lagg_workq_add(lsc->lsc_workq,
   2560 			    &lacpp->lp_work_marker);
   2561 		}
   2562 	}
   2563 
   2564 	LACP_PTIMER_ARM(lsc, LACP_PTIMER_DISTRIBUTING,
   2565 	    LACP_TRANSIT_DELAY);
   2566 }
   2567 
   2568 static void
   2569 lacp_distributing_timer(struct lacp_softc *lsc)
   2570 {
   2571 
   2572 	KASSERT(LACP_LOCKED(lsc));
   2573 
   2574 	if (lsc->lsc_suppress_distributing) {
   2575 		LACP_DPRINTF((lsc, NULL,
   2576 		    "disable suppress distributing\n"));
   2577 		lsc->lsc_suppress_distributing = false;
   2578 	}
   2579 }
   2580 
   2581 static struct mbuf *
   2582 lacp_markerdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
   2583 {
   2584 	struct ifnet *ifp_port;
   2585 	struct mbuf *m;
   2586 	struct markerdu *mdu;
   2587 	struct markerdu_info *mi;
   2588 
   2589 	KASSERT(LACP_LOCKED(lsc));
   2590 
   2591 	ifp_port = lacpp->lp_laggport->lp_ifp;
   2592 
   2593 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2594 	if (m == NULL) {
   2595 		lsc->lsc_mgethdr_failed.ev_count++;
   2596 		return NULL;
   2597 	}
   2598 
   2599 	m->m_pkthdr.len = m->m_len = sizeof(*mdu);
   2600 	m_reset_rcvif(m);
   2601 
   2602 	mdu = mtod(m, struct markerdu *);
   2603 
   2604 	memset(mdu, 0, sizeof(*mdu));
   2605 
   2606 	m->m_flags |= M_MCAST;
   2607 	memcpy(mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
   2608 	    ETHER_ADDR_LEN);
   2609 	memcpy(mdu->mdu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
   2610 	    ETHER_ADDR_LEN);
   2611 	mdu->mdu_eh.ether_type = ntohs(ETHERTYPE_SLOWPROTOCOLS);
   2612 	mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
   2613 	mdu->mdu_sph.sph_version = 1;
   2614 
   2615 	mi = &mdu->mdu_info;
   2616 	tlv_set(&mdu->mdu_tlv_info, MARKER_TYPE_INFO,
   2617 	    sizeof(*mi));
   2618 	mi->mi_rq_port = lacpp->lp_actor.lpi_portno;
   2619 	mi->mi_rq_xid = htonl(lacpp->lp_marker_xid);
   2620 	memcpy(mi->mi_rq_system, lsc->lsc_system_mac, LACP_MAC_LEN);
   2621 
   2622 	mdu->mdu_tlv_term.tlv_type = MARKER_TYPE_TERMINATE;
   2623 	mdu->mdu_tlv_term.tlv_length = 0;
   2624 
   2625 	return m;
   2626 }
   2627 
   2628 static void
   2629 lacp_marker_work(struct lagg_work *lw, void *xlsc)
   2630 {
   2631 	struct lacp_softc *lsc;
   2632 	struct lacp_port *lacpp;
   2633 	struct lagg_port *lp;
   2634 	struct markerdu *mdu;
   2635 	struct mbuf *m;
   2636 	struct psref psref;
   2637 	int bound;
   2638 
   2639 	lsc = xlsc;
   2640 	lacpp = container_of(lw, struct lacp_port, lp_work_marker);
   2641 
   2642 	LACP_LOCK(lsc);
   2643 	lacpp->lp_marker_xid++;
   2644 	m = lacp_markerdu_mbuf(lsc, lacpp);
   2645 	if (m == NULL) {
   2646 		LACP_UNLOCK(lsc);
   2647 		return;
   2648 	}
   2649 	SET(lacpp->lp_flags, LACP_PORT_MARK);
   2650 	lsc->lsc_suppress_distributing = true;
   2651 	lp = lacpp->lp_laggport;
   2652 	bound = curlwp_bind();
   2653 	lagg_port_getref(lp, &psref);
   2654 	LACP_UNLOCK(lsc);
   2655 
   2656 	if (LACP_ISDUMPING(lsc)) {
   2657 		lacp_dprintf(lsc, lacpp, "markerdu transmit\n");
   2658 		mdu = mtod(m, struct markerdu *);
   2659 		lacp_dump_markertlv(&mdu->mdu_info, NULL);
   2660 	}
   2661 
   2662 	lagg_port_xmit(lp, m);
   2663 	lagg_port_putref(lp, &psref);
   2664 	curlwp_bindx(bound);
   2665 }
   2666 
   2667 static void
   2668 lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *pi_actor,
   2669     const struct lacpdu_peerinfo *pi_partner,
   2670     const struct lacpdu_collectorinfo *lci)
   2671 {
   2672 	char str[LACP_STATESTR_LEN];
   2673 
   2674 	if (pi_actor != NULL) {
   2675 		lacp_peerinfo_idstr(pi_actor, str, sizeof(str));
   2676 		printf("actor=%s\n", str);
   2677 		lacp_state_str(pi_actor->lpi_state,
   2678 		    str, sizeof(str));
   2679 		printf("actor.state=%s portno=%d portprio=0x%04x\n",
   2680 		    str,
   2681 		    ntohs(pi_actor->lpi_port_no),
   2682 		    ntohs(pi_actor->lpi_port_prio));
   2683 	} else {
   2684 		printf("no actor info\n");
   2685 	}
   2686 
   2687 	if (pi_partner != NULL) {
   2688 		lacp_peerinfo_idstr(pi_partner, str, sizeof(str));
   2689 		printf("partner=%s\n", str);
   2690 		lacp_state_str(pi_partner->lpi_state,
   2691 		    str, sizeof(str));
   2692 		printf("partner.state=%s portno=%d portprio=0x%04x\n",
   2693 		    str,
   2694 		    ntohs(pi_partner->lpi_port_no),
   2695 		    ntohs(pi_partner->lpi_port_prio));
   2696 	} else {
   2697 		printf("no partner info\n");
   2698 	}
   2699 
   2700 	if (lci != NULL) {
   2701 		printf("maxdelay=%d\n", ntohs(lci->lci_maxdelay));
   2702 	} else {
   2703 		printf("no collector info\n");
   2704 	}
   2705 }
   2706 
   2707 static void
   2708 lacp_dump_markertlv(const struct markerdu_info *mi_info,
   2709     const struct markerdu_info *mi_res)
   2710 {
   2711 
   2712 	if (mi_info != NULL) {
   2713 		printf("marker info: port=%d, sys=%s, id=%u\n",
   2714 		    ntohs(mi_info->mi_rq_port),
   2715 		    ether_sprintf(mi_info->mi_rq_system),
   2716 		    ntohl(mi_info->mi_rq_xid));
   2717 	}
   2718 
   2719 	if (mi_res != NULL) {
   2720 		printf("marker resp: port=%d, sys=%s, id=%u\n",
   2721 		    ntohs(mi_res->mi_rq_port),
   2722 		    ether_sprintf(mi_res->mi_rq_system),
   2723 		    ntohl(mi_res->mi_rq_xid));
   2724 	}
   2725 }
   2726 
   2727 /*
   2728  * lacp_linkstate:
   2729  *   callback on link state changed.
   2730  *   enable, disable or reset LACP processing on the physical port.
   2731  */
   2732 
   2733 static void
   2734 lacp_linkstate(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
   2735 {
   2736 
   2737 	IFNET_ASSERT_UNLOCKED(lp->lp_ifp);
   2738 
   2739 	IFNET_LOCK(lp->lp_ifp);
   2740 	lacp_linkstate_ifnet_locked(xlsc, lp);
   2741 	IFNET_UNLOCK(lp->lp_ifp);
   2742 }
   2743 
   2744 static void
   2745 lacp_linkspeed_work(struct lagg_work *lw __unused, void *xlsc)
   2746 {
   2747 	struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
   2748 	struct lagg_softc *sc = lsc->lsc_softc;
   2749 	struct lacp_portmap *pm;
   2750 	struct lagg_port *lp;
   2751 	struct lacp_port *lacpp;
   2752 	uint64_t linkspeed;
   2753 	size_t act, i;
   2754 
   2755 	linkspeed = 0;
   2756 
   2757 	LACP_LOCK(lsc);
   2758 	act = LACP_PORTMAP_ACTIVE(lsc);
   2759 	pm = &lsc->lsc_portmaps[act];
   2760 	for (i = 0; i < pm->pm_count; i++) {
   2761 		lp = pm->pm_ports[i];
   2762 		lacpp = lp->lp_proto_ctx;
   2763 		linkspeed = MAX(linkspeed, lacpp->lp_linkspeed);
   2764 	}
   2765 	LACP_UNLOCK(lsc);
   2766 
   2767 	LAGG_LOCK(sc);
   2768 	lagg_set_linkspeed(sc, linkspeed);
   2769 	LAGG_UNLOCK(sc);
   2770 }
   2771