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