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