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