Home | History | Annotate | Line # | Download | only in agr
ieee8023ad_lacp.c revision 1.1
      1 /*	$NetBSD: ieee8023ad_lacp.c,v 1.1 2005/03/18 11:11:50 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2005 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ieee8023ad_lacp.c,v 1.1 2005/03/18 11:11:50 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/mbuf.h>
     34 #include <sys/systm.h>
     35 #include <sys/malloc.h>
     36 #include <sys/kernel.h> /* hz */
     37 
     38 #include <net/if.h>
     39 #include <net/if_dl.h>
     40 #include <net/if_ether.h>
     41 #include <net/if_media.h>
     42 
     43 #include <net/agr/if_agrvar_impl.h>
     44 #include <net/agr/if_agrsubr.h>
     45 #include <net/agr/ieee8023_slowprotocols.h>
     46 #include <net/agr/ieee8023_tlv.h>
     47 #include <net/agr/ieee8023ad.h>
     48 #include <net/agr/ieee8023ad_lacp.h>
     49 #include <net/agr/ieee8023ad_lacp_impl.h>
     50 #include <net/agr/ieee8023ad_impl.h>
     51 #include <net/agr/ieee8023ad_lacp_sm.h>
     52 #include <net/agr/ieee8023ad_lacp_debug.h>
     53 
     54 static void lacp_fill_actorinfo(struct agr_port *, struct lacp_peerinfo *);
     55 
     56 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
     57 static void lacp_suppress_distributing(struct lacp_softc *,
     58     struct lacp_aggregator *);
     59 static void lacp_transit_expire(void *);
     60 static void lacp_select_active_aggregator(struct lacp_softc *);
     61 static uint16_t lacp_compose_key(struct lacp_port *);
     62 
     63 /*
     64  * actor system priority and port priority.
     65  * XXX should be configurable.
     66  */
     67 
     68 #define	LACP_SYSTEM_PRIO	0x8000
     69 #define	LACP_PORT_PRIO		0x8000
     70 
     71 static const struct tlv_template lacp_info_tlv_template[] = {
     72 	{ LACP_TYPE_ACTORINFO,
     73 	    sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
     74 	{ LACP_TYPE_PARTNERINFO,
     75 	    sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
     76 	{ LACP_TYPE_COLLECTORINFO,
     77 	    sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
     78 	{ 0, 0 },
     79 };
     80 
     81 /*
     82  * ieee8023ad_lacp_input: process lacpdu
     83  *
     84  * => called from ether_input.  (ie. at IPL_NET)
     85  *
     86  * XXX is it better to defer processing to lower IPL?
     87  * XXX anyway input rate should be very low...
     88  */
     89 
     90 int
     91 ieee8023ad_lacp_input(struct ifnet *ifp, struct mbuf *m)
     92 {
     93 	struct lacpdu *du;
     94 	struct agr_softc *sc;
     95 	struct agr_port *port;
     96 	struct lacp_port *lp;
     97 	int error = 0;
     98 	int s;
     99 
    100 	port = ifp->if_agrprivate; /* XXX race with agr_remport. */
    101 	if (__predict_false(port->port_flags & AGRPORT_DETACHING)) {
    102 		goto bad;
    103 	}
    104 	sc = AGR_SC_FROM_PORT(port);
    105 	KASSERT(port);
    106 
    107 	if (m->m_pkthdr.len != sizeof(*du)) {
    108 		goto bad;
    109 	}
    110 
    111 	if ((m->m_flags & M_MCAST) == 0) {
    112 		goto bad;
    113 	}
    114 
    115 	if (m->m_len < sizeof(*du)) {
    116 		m = m_pullup(m, sizeof(*du));
    117 		if (m == NULL) {
    118 			return ENOMEM;
    119 		}
    120 	}
    121 
    122 	du = mtod(m, struct lacpdu *);
    123 
    124 	if (memcmp(&du->ldu_eh.ether_dhost,
    125 	    &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
    126 		goto bad;
    127 	}
    128 
    129 	KASSERT(du->ldu_sph.sph_subtype == SLOWPROTOCOLS_SUBTYPE_LACP);
    130 
    131 	/*
    132 	 * ignore the version for compatibility with
    133 	 * the future protocol revisions.
    134 	 */
    135 
    136 #if 0
    137 	if (du->ldu_sph.sph_version != 1) {
    138 		goto bad;
    139 	}
    140 #endif
    141 
    142 	/*
    143 	 * ignore tlv types for compatibility with
    144 	 * the future protocol revisions.
    145 	 */
    146 
    147 	if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
    148 	    lacp_info_tlv_template, FALSE)) {
    149 		goto bad;
    150 	}
    151 
    152 	s = AGR_LOCK(sc);
    153 	lp = LACP_PORT(port);
    154 
    155 #if defined(LACP_DEBUG)
    156 	if (lacpdebug) {
    157 		LACP_DPRINTF((lp, "lacpdu receive\n"));
    158 		lacp_dump_lacpdu(du);
    159 	}
    160 #endif /* defined(LACP_DEBUG) */
    161 	lacp_sm_rx(lp, du);
    162 
    163 	AGR_UNLOCK(sc, s);
    164 
    165 	m_freem(m);
    166 
    167 	return error;
    168 
    169 bad:
    170 	m_freem(m);
    171 	return EINVAL;
    172 }
    173 
    174 static void
    175 lacp_fill_actorinfo(struct agr_port *port, struct lacp_peerinfo *info)
    176 {
    177 	struct lacp_port *lp = LACP_PORT(port);
    178 
    179 	info->lip_systemid.lsi_prio = htobe16(LACP_SYSTEM_PRIO);
    180 	memcpy(&info->lip_systemid.lsi_mac,
    181 	    LLADDR(port->port_ifp->if_sadl), ETHER_ADDR_LEN);
    182 	info->lip_portid.lpi_prio = htobe16(LACP_PORT_PRIO);
    183 	info->lip_portid.lpi_portno = htobe16(port->port_ifp->if_index);
    184 	info->lip_state = lp->lp_state;
    185 }
    186 
    187 int
    188 lacp_xmit_lacpdu(struct lacp_port *lp)
    189 {
    190 	struct agr_port *port = lp->lp_agrport;
    191 	struct mbuf *m;
    192 	struct lacpdu *du;
    193 	int error;
    194 
    195 	KDASSERT(MHLEN >= sizeof(*du));
    196 
    197 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    198 	if (m == NULL) {
    199 		return ENOMEM;
    200 	}
    201 	m->m_len = m->m_pkthdr.len = sizeof(*du);
    202 
    203 	du = mtod(m, struct lacpdu *);
    204 	memset(du, 0, sizeof(*du));
    205 
    206 	memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
    207 	    ETHER_ADDR_LEN);
    208 	memcpy(&du->ldu_eh.ether_shost, &port->port_origlladdr, ETHER_ADDR_LEN);
    209 	du->ldu_eh.ether_type = htobe16(ETHERTYPE_SLOWPROTOCOLS);
    210 
    211 	du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
    212 	du->ldu_sph.sph_version = 1;
    213 
    214 	TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
    215 	du->ldu_actor = lp->lp_actor;
    216 
    217 	TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
    218 	    sizeof(du->ldu_partner));
    219 	du->ldu_partner = lp->lp_partner;
    220 
    221 	TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
    222 	    sizeof(du->ldu_collector));
    223 	du->ldu_collector.lci_maxdelay = 0;
    224 
    225 #if defined(LACP_DEBUG)
    226 	if (lacpdebug) {
    227 		LACP_DPRINTF((lp, "lacpdu transmit\n"));
    228 		lacp_dump_lacpdu(du);
    229 	}
    230 #endif /* defined(LACP_DEBUG) */
    231 
    232 	m->m_flags |= M_MCAST;
    233 
    234 	/*
    235 	 * XXX should use higher priority queue.
    236 	 * otherwise network congestion can break aggregation.
    237 	 */
    238 
    239 	error = agr_xmit_frame(port->port_ifp, m);
    240 	return error;
    241 }
    242 
    243 void
    244 ieee8023ad_lacp_portstate(struct agr_port *port)
    245 {
    246 	struct lacp_port *lp = LACP_PORT(port);
    247 	u_int media = port->port_media;
    248 	uint8_t old_state;
    249 	uint16_t old_key;
    250 
    251 	AGR_ASSERT_LOCKED(AGR_SC_FROM_PORT(port));
    252 
    253 	LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x\n", lp->lp_media, media));
    254 
    255 	old_state = lp->lp_state;
    256 	old_key = lp->lp_key;
    257 
    258 	lp->lp_media = media;
    259 	if ((media & IFM_HDX) != 0) {
    260 		lp->lp_state &= ~LACP_STATE_AGGREGATION;
    261 	} else {
    262 		lp->lp_state |= LACP_STATE_AGGREGATION;
    263 	}
    264 	lp->lp_key = lacp_compose_key(lp);
    265 
    266 	if (old_state != lp->lp_state || old_key != lp->lp_key) {
    267 		LACP_DPRINTF((lp, "-> UNSELECTED\n"));
    268 		lp->lp_selected = LACP_UNSELECTED;
    269 	}
    270 }
    271 
    272 void
    273 ieee8023ad_lacp_porttick(struct agr_softc *sc, struct agr_port *port)
    274 {
    275 	struct lacp_port *lp = LACP_PORT(port);
    276 
    277 	AGR_ASSERT_LOCKED(sc);
    278 
    279 	lacp_run_timers(lp);
    280 
    281 	lacp_select(lp);
    282 	lacp_sm_mux(lp);
    283 	lacp_sm_tx(lp);
    284 	lacp_sm_ptx_tx_schedule(lp);
    285 }
    286 
    287 void
    288 lacp_portinit(struct agr_port *port)
    289 {
    290 	struct lacp_port *lp = LACP_PORT(port);
    291 	boolean_t active = TRUE; /* XXX should be configurable */
    292 	boolean_t fast = FALSE; /* XXX should be configurable */
    293 
    294 	lp->lp_agrport = port;
    295 	lacp_fill_actorinfo(port, &lp->lp_actor);
    296 	lp->lp_state =
    297 	    (active ? LACP_STATE_ACTIVITY : 0) |
    298 	    (fast ? LACP_STATE_TIMEOUT : 0);
    299 	lp->lp_aggregator = NULL;
    300 	lp->lp_media = port->port_media; /* XXX */
    301 	lp->lp_key = lacp_compose_key(lp);
    302 	lacp_sm_rx_set_expired(lp);
    303 }
    304 
    305 void
    306 lacp_portfini(struct agr_port *port)
    307 {
    308 	struct lacp_port *lp = LACP_PORT(port);
    309 	struct lacp_aggregator *la = lp->lp_aggregator;
    310 	int i;
    311 
    312 	LACP_DPRINTF((lp, "portfini\n"));
    313 
    314 	for (i = 0; i < LACP_NTIMER; i++) {
    315 		LACP_TIMER_DISARM(lp, i);
    316 	}
    317 
    318 	if (la == NULL) {
    319 		return;
    320 	}
    321 
    322 	lacp_disable_distributing(lp);
    323 	lacp_unselect(lp);
    324 }
    325 
    326 /* -------------------- */
    327 void
    328 lacp_disable_collecting(struct lacp_port *lp)
    329 {
    330 	struct agr_port *port = lp->lp_agrport;
    331 
    332 	lp->lp_state &= ~LACP_STATE_COLLECTING;
    333 	port->port_flags &= ~AGRPORT_COLLECTING;
    334 }
    335 
    336 void
    337 lacp_enable_collecting(struct lacp_port *lp)
    338 {
    339 	struct agr_port *port = lp->lp_agrport;
    340 
    341 	lp->lp_state |= LACP_STATE_COLLECTING;
    342 	port->port_flags |= AGRPORT_COLLECTING;
    343 }
    344 
    345 void
    346 lacp_disable_distributing(struct lacp_port *lp)
    347 {
    348 	struct agr_port *port = lp->lp_agrport;
    349 	struct lacp_aggregator *la = lp->lp_aggregator;
    350 	struct lacp_softc *lsc = LACP_SOFTC(AGR_SC_FROM_PORT(port));
    351 #if defined(LACP_DEBUG)
    352 	char buf[LACP_LAGIDSTR_MAX+1];
    353 #endif /* defined(LACP_DEBUG) */
    354 
    355 	if ((lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
    356 		return;
    357 	}
    358 
    359 	KASSERT(la);
    360 	KASSERT(!TAILQ_EMPTY(&la->la_ports));
    361 	KASSERT(la->la_nports > 0);
    362 	KASSERT(la->la_refcnt >= la->la_nports);
    363 
    364 	LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
    365 	    "nports %d -> %d\n",
    366 	    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
    367 	    la->la_nports, la->la_nports - 1));
    368 
    369 	TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
    370 	la->la_nports--;
    371 
    372 	lacp_suppress_distributing(lsc, la);
    373 
    374 	lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
    375 	port->port_flags &= ~AGRPORT_DISTRIBUTING;
    376 
    377 	if (lsc->lsc_active_aggregator == la) {
    378 		lacp_select_active_aggregator(lsc);
    379 	}
    380 }
    381 
    382 void
    383 lacp_enable_distributing(struct lacp_port *lp)
    384 {
    385 	struct agr_port *port = lp->lp_agrport;
    386 	struct lacp_aggregator *la = lp->lp_aggregator;
    387 	struct lacp_softc *lsc = LACP_SOFTC(AGR_SC_FROM_PORT(port));
    388 #if defined(LACP_DEBUG)
    389 	char buf[LACP_LAGIDSTR_MAX+1];
    390 #endif /* defined(LACP_DEBUG) */
    391 
    392 	if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
    393 		return;
    394 	}
    395 
    396 	KASSERT(la);
    397 
    398 	LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
    399 	    "nports %d -> %d\n",
    400 	    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
    401 	    la->la_nports, la->la_nports + 1));
    402 
    403 	KASSERT(la->la_refcnt > la->la_nports);
    404 	TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
    405 	la->la_nports++;
    406 
    407 	lacp_suppress_distributing(lsc, la);
    408 
    409 	lp->lp_state |= LACP_STATE_DISTRIBUTING;
    410 	port->port_flags |= AGRPORT_DISTRIBUTING;
    411 
    412 	if (lsc->lsc_active_aggregator != la) {
    413 		lacp_select_active_aggregator(lsc);
    414 	}
    415 }
    416 
    417 static void
    418 lacp_transit_expire(void *vp)
    419 {
    420 	struct agr_softc *sc = vp;
    421 	struct lacp_softc *lsc = LACP_SOFTC(sc);
    422 	int s;
    423 
    424 	s = AGR_LOCK(sc);
    425 	LACP_DPRINTF((NULL, "%s\n", __func__));
    426 	lsc->lsc_suppress_distributing = FALSE;
    427 	AGR_UNLOCK(sc, s);
    428 }
    429 
    430 /* -------------------- */
    431 /* XXX */
    432 void
    433 ieee8023ad_portinit(struct agr_port *port)
    434 {
    435 	struct ieee8023ad_port *iport = IEEE8023AD_PORT(port);
    436 
    437 	memset(iport, 0, sizeof(iport));
    438 
    439 	lacp_portinit(port);
    440 }
    441 
    442 void
    443 ieee8023ad_portfini(struct agr_port *port)
    444 {
    445 	struct agr_softc *sc = AGR_SC_FROM_PORT(port);
    446 	int s;
    447 
    448 	s = AGR_LOCK(sc);
    449 
    450 	lacp_portfini(port);
    451 
    452 	AGR_UNLOCK(sc, s);
    453 }
    454 
    455 void
    456 ieee8023ad_ctor(struct agr_softc *sc)
    457 {
    458 	struct ieee8023ad_softc *isc = IEEE8023AD_SOFTC(sc);
    459 	struct lacp_softc *lsc = &isc->isc_lacpsc;
    460 
    461 	lsc->lsc_active_aggregator = NULL;
    462 	TAILQ_INIT(&lsc->lsc_aggregators);
    463 	callout_init(&lsc->lsc_transit_callout);
    464 	callout_setfunc(&lsc->lsc_transit_callout, lacp_transit_expire, sc);
    465 }
    466 
    467 void
    468 ieee8023ad_dtor(struct agr_softc *sc)
    469 {
    470 	struct ieee8023ad_softc *isc = IEEE8023AD_SOFTC(sc);
    471 	struct lacp_softc *lsc = &isc->isc_lacpsc;
    472 
    473 	LACP_DPRINTF((NULL, "%s\n", __func__));
    474 
    475 	callout_stop(&lsc->lsc_transit_callout);
    476 	KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators));
    477 	KASSERT(lsc->lsc_active_aggregator == NULL);
    478 }
    479 
    480 /* -------------------- */
    481 
    482 struct agr_port *
    483 ieee8023ad_select_tx_port(struct agr_softc *sc, struct mbuf *m)
    484 {
    485 	const struct lacp_softc *lsc = LACP_SOFTC(sc);
    486 	const struct lacp_aggregator *la;
    487 	const struct lacp_port *lp;
    488 	uint32_t hash;
    489 	int nports;
    490 
    491 	if (__predict_false(lsc->lsc_suppress_distributing &&
    492 	    !AGR_ROUNDROBIN(sc))) {
    493 		LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
    494 		sc->sc_if.if_collisions++; /* XXX abuse */
    495 		return NULL;
    496 	}
    497 
    498 	la = lsc->lsc_active_aggregator;
    499 	if (__predict_false(la == NULL)) {
    500 		LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
    501 		return NULL;
    502 	}
    503 
    504 	nports = la->la_nports;
    505 	KASSERT(nports > 0);
    506 
    507 	if (AGR_ROUNDROBIN(sc)) {
    508 		/* packet ordering rule violation */
    509 		hash = sc->sc_rr_counter++;
    510 	} else {
    511 		hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
    512 	}
    513 	hash %= nports;
    514 	lp = TAILQ_FIRST(&la->la_ports);
    515 	KASSERT(lp != NULL);
    516 	while (hash--) {
    517 		lp = TAILQ_NEXT(lp, lp_dist_q);
    518 		KASSERT(lp != NULL);
    519 	}
    520 
    521 	KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0);
    522 
    523 	return lp->lp_agrport;
    524 }
    525 
    526 /*
    527  * lacp_suppress_distributing: drop transmit packets for a while
    528  * to preserve packet ordering.
    529  */
    530 
    531 static void
    532 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
    533 {
    534 
    535 	if (lsc->lsc_active_aggregator != la) {
    536 		return;
    537 	}
    538 
    539 	LACP_DPRINTF((NULL, "%s\n", __func__));
    540 	lsc->lsc_suppress_distributing = TRUE;
    541 	/* XXX should consider collector max delay */
    542 	callout_schedule(&lsc->lsc_transit_callout,
    543 	    LACP_TRANSIT_DELAY * hz / 1000);
    544 }
    545 
    546 /* -------------------- */
    547 
    548 int
    549 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
    550     const struct lacp_peerinfo *b)
    551 {
    552 
    553 	return memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state));
    554 }
    555 
    556 int
    557 lacp_compare_systemid(const struct lacp_systemid *a,
    558     const struct lacp_systemid *b)
    559 {
    560 
    561 	return memcmp(a, b, sizeof(*a));
    562 }
    563 
    564 int
    565 lacp_compare_portid(const struct lacp_portid *a,
    566     const struct lacp_portid *b)
    567 {
    568 
    569 	return memcmp(a, b, sizeof(*a));
    570 }
    571 
    572 /* -------------------- */
    573 
    574 static uint64_t
    575 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
    576 {
    577 	struct lacp_port *lp;
    578 	uint64_t speed;
    579 
    580 	lp = TAILQ_FIRST(&la->la_ports);
    581 	if (lp == NULL) {
    582 		return 0;
    583 	}
    584 
    585 	speed = ifmedia_baudrate(lp->lp_media);
    586 	speed *= la->la_nports;
    587 	if (speed == 0) {
    588 		LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
    589 		    lp->lp_media, la->la_nports));
    590 	}
    591 
    592 	return speed;
    593 }
    594 
    595 /*
    596  * lacp_select_active_aggregator: select an aggregator to be used to transmit
    597  * packets from agr(4) interface.
    598  */
    599 
    600 static void
    601 lacp_select_active_aggregator(struct lacp_softc *lsc)
    602 {
    603 	struct lacp_aggregator *la;
    604 	struct lacp_aggregator *best_la = NULL;
    605 	uint64_t best_speed = 0;
    606 #if defined(LACP_DEBUG)
    607 	char buf[LACP_LAGIDSTR_MAX+1];
    608 #endif /* defined(LACP_DEBUG) */
    609 
    610 	LACP_DPRINTF((NULL, "%s:\n", __func__));
    611 
    612 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
    613 		uint64_t speed;
    614 
    615 		if (la->la_nports == 0) {
    616 			continue;
    617 		}
    618 
    619 		speed = lacp_aggregator_bandwidth(la);
    620 		LACP_DPRINTF((NULL, "%s, speed=%" PRIu64 ", nports=%d\n",
    621 		    lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
    622 		    speed, la->la_nports));
    623 		if (speed > best_speed ||
    624 		    (speed == best_speed &&
    625 		    la == lsc->lsc_active_aggregator)) {
    626 			best_la = la;
    627 			best_speed = speed;
    628 		}
    629 	}
    630 
    631 	KASSERT(best_la == NULL || best_la->la_nports > 0);
    632 	KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports));
    633 
    634 #if defined(LACP_DEBUG)
    635 	if (lsc->lsc_active_aggregator != best_la) {
    636 		LACP_DPRINTF((NULL, "active aggregator changed\n"));
    637 		LACP_DPRINTF((NULL, "old %s\n",
    638 		    lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
    639 		    buf, sizeof(buf))));
    640 	} else {
    641 		LACP_DPRINTF((NULL, "active aggregator not changed\n"));
    642 	}
    643 	LACP_DPRINTF((NULL, "new %s\n",
    644 	    lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
    645 #endif /* defined(LACP_DEBUG) */
    646 
    647 	if (lsc->lsc_active_aggregator != best_la) {
    648 		lsc->lsc_active_aggregator = best_la;
    649 		if (best_la) {
    650 			lacp_suppress_distributing(lsc, best_la);
    651 		}
    652 	}
    653 }
    654 
    655 uint16_t
    656 lacp_compose_key(struct lacp_port *lp)
    657 {
    658 	u_int media = lp->lp_media;
    659 	uint16_t key;
    660 
    661 	KASSERT(IFM_TYPE(media) == IFM_ETHER);
    662 
    663 	if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
    664 
    665 		/*
    666 		 * non-aggregatable links should have unique keys.
    667 		 *
    668 		 * XXX this isn't really unique as if_index is 16 bit.
    669 		 */
    670 
    671 		/* bit 0..14:	(some bits of) if_index of this port */
    672 		key = lp->lp_agrport->port_ifp->if_index;
    673 		/* bit 15:	1 */
    674 		key |= 0x8000;
    675 	} else {
    676 		u_int subtype = IFM_SUBTYPE(media);
    677 
    678 		KASSERT((media & IFM_HDX) == 0); /* should be handled above */
    679 		KASSERT((subtype & 0x1f) == subtype);
    680 
    681 		/* bit 0..4:	IFM_SUBTYPE */
    682 		key = subtype;
    683 		/* bit 5..14:	(some bits of) if_index of agr device */
    684 		key |= 0x7fe0 & ((lp->lp_agrport->port_agrifp->if_index) << 5);
    685 		/* bit 15:	0 */
    686 	}
    687 
    688 	return htobe16(key);
    689 }
    690