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