1168561Sthompsa/* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $ */ 2168561Sthompsa 3168561Sthompsa/*- 4168561Sthompsa * Copyright (c)2005 YAMAMOTO Takashi, 5177274Sthompsa * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org> 6168561Sthompsa * All rights reserved. 7168561Sthompsa * 8168561Sthompsa * Redistribution and use in source and binary forms, with or without 9168561Sthompsa * modification, are permitted provided that the following conditions 10168561Sthompsa * are met: 11168561Sthompsa * 1. Redistributions of source code must retain the above copyright 12168561Sthompsa * notice, this list of conditions and the following disclaimer. 13168561Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 14168561Sthompsa * notice, this list of conditions and the following disclaimer in the 15168561Sthompsa * documentation and/or other materials provided with the distribution. 16168561Sthompsa * 17168561Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18168561Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19168561Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20168561Sthompsa * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21168561Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22168561Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23168561Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24168561Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25168561Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26168561Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27168561Sthompsa * SUCH DAMAGE. 28168561Sthompsa */ 29168561Sthompsa 30168561Sthompsa#include <sys/cdefs.h> 31168561Sthompsa__FBSDID("$FreeBSD: stable/10/sys/net/ieee8023ad_lacp.c 353414 2019-10-10 20:28:34Z np $"); 32168561Sthompsa 33168561Sthompsa#include <sys/param.h> 34168561Sthompsa#include <sys/callout.h> 35168561Sthompsa#include <sys/mbuf.h> 36168561Sthompsa#include <sys/systm.h> 37168561Sthompsa#include <sys/malloc.h> 38168561Sthompsa#include <sys/kernel.h> /* hz */ 39168561Sthompsa#include <sys/socket.h> /* for net/if.h */ 40168561Sthompsa#include <sys/sockio.h> 41236062Sthompsa#include <sys/sysctl.h> 42168561Sthompsa#include <machine/stdarg.h> 43169569Sthompsa#include <sys/lock.h> 44169569Sthompsa#include <sys/rwlock.h> 45168561Sthompsa 46168561Sthompsa#include <net/if.h> 47168561Sthompsa#include <net/if_dl.h> 48168561Sthompsa#include <net/ethernet.h> 49168561Sthompsa#include <net/if_media.h> 50168561Sthompsa#include <net/if_types.h> 51168561Sthompsa 52168793Sthompsa#include <net/if_lagg.h> 53168561Sthompsa#include <net/ieee8023ad_lacp.h> 54168561Sthompsa 55168561Sthompsa/* 56168561Sthompsa * actor system priority and port priority. 57168561Sthompsa * XXX should be configurable. 58168561Sthompsa */ 59168561Sthompsa 60168561Sthompsa#define LACP_SYSTEM_PRIO 0x8000 61168561Sthompsa#define LACP_PORT_PRIO 0x8000 62168561Sthompsa 63168561Sthompsaconst uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] = 64168561Sthompsa { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 }; 65168561Sthompsa 66168561Sthompsastatic const struct tlv_template lacp_info_tlv_template[] = { 67168561Sthompsa { LACP_TYPE_ACTORINFO, 68168561Sthompsa sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) }, 69168561Sthompsa { LACP_TYPE_PARTNERINFO, 70168561Sthompsa sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) }, 71168561Sthompsa { LACP_TYPE_COLLECTORINFO, 72168561Sthompsa sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) }, 73168561Sthompsa { 0, 0 }, 74168561Sthompsa}; 75168561Sthompsa 76168561Sthompsastatic const struct tlv_template marker_info_tlv_template[] = { 77169739Sthompsa { MARKER_TYPE_INFO, 78169739Sthompsa sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) }, 79168561Sthompsa { 0, 0 }, 80168561Sthompsa}; 81168561Sthompsa 82168561Sthompsastatic const struct tlv_template marker_response_tlv_template[] = { 83169739Sthompsa { MARKER_TYPE_RESPONSE, 84169739Sthompsa sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) }, 85168561Sthompsa { 0, 0 }, 86168561Sthompsa}; 87168561Sthompsa 88177274Sthompsatypedef void (*lacp_timer_func_t)(struct lacp_port *); 89177274Sthompsa 90168561Sthompsastatic void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *); 91169739Sthompsastatic void lacp_fill_markerinfo(struct lacp_port *, 92169739Sthompsa struct lacp_markerinfo *); 93168561Sthompsa 94168561Sthompsastatic uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *); 95168561Sthompsastatic void lacp_suppress_distributing(struct lacp_softc *, 96168561Sthompsa struct lacp_aggregator *); 97168561Sthompsastatic void lacp_transit_expire(void *); 98177274Sthompsastatic void lacp_update_portmap(struct lacp_softc *); 99168561Sthompsastatic void lacp_select_active_aggregator(struct lacp_softc *); 100168561Sthompsastatic uint16_t lacp_compose_key(struct lacp_port *); 101168561Sthompsastatic int tlv_check(const void *, size_t, const struct tlvhdr *, 102168561Sthompsa const struct tlv_template *, boolean_t); 103168561Sthompsastatic void lacp_tick(void *); 104168561Sthompsa 105168561Sthompsastatic void lacp_fill_aggregator_id(struct lacp_aggregator *, 106168561Sthompsa const struct lacp_port *); 107168561Sthompsastatic void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *, 108168561Sthompsa const struct lacp_peerinfo *); 109168561Sthompsastatic int lacp_aggregator_is_compatible(const struct lacp_aggregator *, 110168561Sthompsa const struct lacp_port *); 111168561Sthompsastatic int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *, 112168561Sthompsa const struct lacp_peerinfo *); 113168561Sthompsa 114168561Sthompsastatic struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *, 115168561Sthompsa struct lacp_port *); 116168561Sthompsastatic void lacp_aggregator_addref(struct lacp_softc *, 117168561Sthompsa struct lacp_aggregator *); 118168561Sthompsastatic void lacp_aggregator_delref(struct lacp_softc *, 119168561Sthompsa struct lacp_aggregator *); 120168561Sthompsa 121168561Sthompsa/* receive machine */ 122168561Sthompsa 123177274Sthompsastatic int lacp_pdu_input(struct lacp_port *, struct mbuf *); 124177274Sthompsastatic int lacp_marker_input(struct lacp_port *, struct mbuf *); 125168561Sthompsastatic void lacp_sm_rx(struct lacp_port *, const struct lacpdu *); 126168561Sthompsastatic void lacp_sm_rx_timer(struct lacp_port *); 127168561Sthompsastatic void lacp_sm_rx_set_expired(struct lacp_port *); 128168561Sthompsastatic void lacp_sm_rx_update_ntt(struct lacp_port *, 129168561Sthompsa const struct lacpdu *); 130168561Sthompsastatic void lacp_sm_rx_record_pdu(struct lacp_port *, 131168561Sthompsa const struct lacpdu *); 132168561Sthompsastatic void lacp_sm_rx_update_selected(struct lacp_port *, 133168561Sthompsa const struct lacpdu *); 134168561Sthompsastatic void lacp_sm_rx_record_default(struct lacp_port *); 135168561Sthompsastatic void lacp_sm_rx_update_default_selected(struct lacp_port *); 136168561Sthompsastatic void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *, 137168561Sthompsa const struct lacp_peerinfo *); 138168561Sthompsa 139168561Sthompsa/* mux machine */ 140168561Sthompsa 141168561Sthompsastatic void lacp_sm_mux(struct lacp_port *); 142168561Sthompsastatic void lacp_set_mux(struct lacp_port *, enum lacp_mux_state); 143168561Sthompsastatic void lacp_sm_mux_timer(struct lacp_port *); 144168561Sthompsa 145168561Sthompsa/* periodic transmit machine */ 146168561Sthompsa 147168561Sthompsastatic void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t); 148168561Sthompsastatic void lacp_sm_ptx_tx_schedule(struct lacp_port *); 149168561Sthompsastatic void lacp_sm_ptx_timer(struct lacp_port *); 150168561Sthompsa 151168561Sthompsa/* transmit machine */ 152168561Sthompsa 153168561Sthompsastatic void lacp_sm_tx(struct lacp_port *); 154168561Sthompsastatic void lacp_sm_assert_ntt(struct lacp_port *); 155168561Sthompsa 156168561Sthompsastatic void lacp_run_timers(struct lacp_port *); 157168561Sthompsastatic int lacp_compare_peerinfo(const struct lacp_peerinfo *, 158168561Sthompsa const struct lacp_peerinfo *); 159168561Sthompsastatic int lacp_compare_systemid(const struct lacp_systemid *, 160168561Sthompsa const struct lacp_systemid *); 161168561Sthompsastatic void lacp_port_enable(struct lacp_port *); 162168561Sthompsastatic void lacp_port_disable(struct lacp_port *); 163168561Sthompsastatic void lacp_select(struct lacp_port *); 164168561Sthompsastatic void lacp_unselect(struct lacp_port *); 165168561Sthompsastatic void lacp_disable_collecting(struct lacp_port *); 166168561Sthompsastatic void lacp_enable_collecting(struct lacp_port *); 167168561Sthompsastatic void lacp_disable_distributing(struct lacp_port *); 168168561Sthompsastatic void lacp_enable_distributing(struct lacp_port *); 169168561Sthompsastatic int lacp_xmit_lacpdu(struct lacp_port *); 170169739Sthompsastatic int lacp_xmit_marker(struct lacp_port *); 171168561Sthompsa 172236062Sthompsa/* Debugging */ 173236062Sthompsa 174168561Sthompsastatic void lacp_dump_lacpdu(const struct lacpdu *); 175168561Sthompsastatic const char *lacp_format_partner(const struct lacp_peerinfo *, char *, 176168561Sthompsa size_t); 177168561Sthompsastatic const char *lacp_format_lagid(const struct lacp_peerinfo *, 178168561Sthompsa const struct lacp_peerinfo *, char *, size_t); 179168561Sthompsastatic const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *, 180168561Sthompsa char *, size_t); 181168561Sthompsastatic const char *lacp_format_state(uint8_t, char *, size_t); 182168561Sthompsastatic const char *lacp_format_mac(const uint8_t *, char *, size_t); 183168561Sthompsastatic const char *lacp_format_systemid(const struct lacp_systemid *, char *, 184168561Sthompsa size_t); 185168561Sthompsastatic const char *lacp_format_portid(const struct lacp_portid *, char *, 186168561Sthompsa size_t); 187168561Sthompsastatic void lacp_dprintf(const struct lacp_port *, const char *, ...) 188168561Sthompsa __attribute__((__format__(__printf__, 2, 3))); 189168561Sthompsa 190287723Shrsstatic VNET_DEFINE(int, lacp_debug); 191287723Shrs#define V_lacp_debug VNET(lacp_debug) 192253314SadrianSYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad"); 193287723ShrsSYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET, 194287723Shrs &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)"); 195236062Sthompsa 196290736Ssmhstatic VNET_DEFINE(int, lacp_default_strict_mode) = 1; 197290736SsmhSYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode, CTLFLAG_RWTUN, 198290736Ssmh &VNET_NAME(lacp_default_strict_mode), 0, 199290736Ssmh "LACP strict protocol compliance default"); 200290736Ssmh 201287723Shrs#define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; } 202287723Shrs#define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); } 203287723Shrs#define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; } 204253314Sadrian 205168561Sthompsa/* 206168561Sthompsa * partner administration variables. 207168561Sthompsa * XXX should be configurable. 208168561Sthompsa */ 209168561Sthompsa 210253314Sadrianstatic const struct lacp_peerinfo lacp_partner_admin_optimistic = { 211168561Sthompsa .lip_systemid = { .lsi_prio = 0xffff }, 212168561Sthompsa .lip_portid = { .lpi_prio = 0xffff }, 213168561Sthompsa .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION | 214168561Sthompsa LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING, 215253314Sadrian}; 216253314Sadrian 217253314Sadrianstatic const struct lacp_peerinfo lacp_partner_admin_strict = { 218253314Sadrian .lip_systemid = { .lsi_prio = 0xffff }, 219253314Sadrian .lip_portid = { .lpi_prio = 0xffff }, 220168561Sthompsa .lip_state = 0, 221168561Sthompsa}; 222168561Sthompsa 223168561Sthompsastatic const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = { 224168561Sthompsa [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer, 225168561Sthompsa [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer, 226168561Sthompsa [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer, 227168561Sthompsa}; 228168561Sthompsa 229175005Sthompsastruct mbuf * 230169569Sthompsalacp_input(struct lagg_port *lgp, struct mbuf *m) 231169569Sthompsa{ 232177274Sthompsa struct lacp_port *lp = LACP_PORT(lgp); 233169569Sthompsa uint8_t subtype; 234169569Sthompsa 235169569Sthompsa if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) { 236169569Sthompsa m_freem(m); 237175005Sthompsa return (NULL); 238169569Sthompsa } 239169569Sthompsa 240169569Sthompsa m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype); 241169569Sthompsa switch (subtype) { 242169569Sthompsa case SLOWPROTOCOLS_SUBTYPE_LACP: 243177274Sthompsa lacp_pdu_input(lp, m); 244177274Sthompsa return (NULL); 245169569Sthompsa 246169569Sthompsa case SLOWPROTOCOLS_SUBTYPE_MARKER: 247177274Sthompsa lacp_marker_input(lp, m); 248177274Sthompsa return (NULL); 249169569Sthompsa } 250169569Sthompsa 251177274Sthompsa /* Not a subtype we are interested in */ 252177274Sthompsa return (m); 253169569Sthompsa} 254169569Sthompsa 255168561Sthompsa/* 256169569Sthompsa * lacp_pdu_input: process lacpdu 257168561Sthompsa */ 258169569Sthompsastatic int 259177274Sthompsalacp_pdu_input(struct lacp_port *lp, struct mbuf *m) 260168561Sthompsa{ 261177274Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 262168561Sthompsa struct lacpdu *du; 263168561Sthompsa int error = 0; 264168561Sthompsa 265168561Sthompsa if (m->m_pkthdr.len != sizeof(*du)) { 266168561Sthompsa goto bad; 267168561Sthompsa } 268168561Sthompsa 269168561Sthompsa if ((m->m_flags & M_MCAST) == 0) { 270168561Sthompsa goto bad; 271168561Sthompsa } 272168561Sthompsa 273168561Sthompsa if (m->m_len < sizeof(*du)) { 274168561Sthompsa m = m_pullup(m, sizeof(*du)); 275168561Sthompsa if (m == NULL) { 276168561Sthompsa return (ENOMEM); 277168561Sthompsa } 278168561Sthompsa } 279168561Sthompsa 280168561Sthompsa du = mtod(m, struct lacpdu *); 281168561Sthompsa 282168561Sthompsa if (memcmp(&du->ldu_eh.ether_dhost, 283168561Sthompsa ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) { 284168561Sthompsa goto bad; 285168561Sthompsa } 286168561Sthompsa 287168561Sthompsa /* 288168561Sthompsa * ignore the version for compatibility with 289168561Sthompsa * the future protocol revisions. 290168561Sthompsa */ 291168561Sthompsa#if 0 292168561Sthompsa if (du->ldu_sph.sph_version != 1) { 293168561Sthompsa goto bad; 294168561Sthompsa } 295168561Sthompsa#endif 296168561Sthompsa 297168561Sthompsa /* 298168561Sthompsa * ignore tlv types for compatibility with 299168561Sthompsa * the future protocol revisions. 300168561Sthompsa */ 301168561Sthompsa if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor, 302168561Sthompsa lacp_info_tlv_template, FALSE)) { 303168561Sthompsa goto bad; 304168561Sthompsa } 305168561Sthompsa 306287723Shrs if (V_lacp_debug > 0) { 307236062Sthompsa lacp_dprintf(lp, "lacpdu receive\n"); 308236062Sthompsa lacp_dump_lacpdu(du); 309236062Sthompsa } 310177274Sthompsa 311253687Sadrian if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) { 312253314Sadrian LACP_TPRINTF((lp, "Dropping RX PDU\n")); 313253314Sadrian goto bad; 314253314Sadrian } 315253314Sadrian 316177274Sthompsa LACP_LOCK(lsc); 317168561Sthompsa lacp_sm_rx(lp, du); 318177274Sthompsa LACP_UNLOCK(lsc); 319168561Sthompsa 320168561Sthompsa m_freem(m); 321168561Sthompsa return (error); 322168561Sthompsa 323168561Sthompsabad: 324168561Sthompsa m_freem(m); 325168561Sthompsa return (EINVAL); 326168561Sthompsa} 327168561Sthompsa 328168561Sthompsastatic void 329168561Sthompsalacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info) 330168561Sthompsa{ 331168793Sthompsa struct lagg_port *lgp = lp->lp_lagg; 332170599Sthompsa struct lagg_softc *sc = lgp->lp_softc; 333168561Sthompsa 334168561Sthompsa info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO); 335168561Sthompsa memcpy(&info->lip_systemid.lsi_mac, 336170599Sthompsa IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 337168561Sthompsa info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO); 338168561Sthompsa info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index); 339168561Sthompsa info->lip_state = lp->lp_state; 340168561Sthompsa} 341168561Sthompsa 342169739Sthompsastatic void 343169739Sthompsalacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info) 344169739Sthompsa{ 345169739Sthompsa struct ifnet *ifp = lp->lp_ifp; 346169739Sthompsa 347169739Sthompsa /* Fill in the port index and system id (encoded as the MAC) */ 348169739Sthompsa info->mi_rq_port = htons(ifp->if_index); 349169739Sthompsa memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN); 350169739Sthompsa info->mi_rq_xid = htonl(0); 351169739Sthompsa} 352169739Sthompsa 353168561Sthompsastatic int 354168561Sthompsalacp_xmit_lacpdu(struct lacp_port *lp) 355168561Sthompsa{ 356168793Sthompsa struct lagg_port *lgp = lp->lp_lagg; 357168561Sthompsa struct mbuf *m; 358168561Sthompsa struct lacpdu *du; 359168561Sthompsa int error; 360168561Sthompsa 361177274Sthompsa LACP_LOCK_ASSERT(lp->lp_lsc); 362168561Sthompsa 363243882Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 364168561Sthompsa if (m == NULL) { 365168561Sthompsa return (ENOMEM); 366168561Sthompsa } 367168561Sthompsa m->m_len = m->m_pkthdr.len = sizeof(*du); 368168561Sthompsa 369168561Sthompsa du = mtod(m, struct lacpdu *); 370168561Sthompsa memset(du, 0, sizeof(*du)); 371168561Sthompsa 372168561Sthompsa memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols, 373168561Sthompsa ETHER_ADDR_LEN); 374168793Sthompsa memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN); 375168561Sthompsa du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW); 376168561Sthompsa 377168561Sthompsa du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP; 378168561Sthompsa du->ldu_sph.sph_version = 1; 379168561Sthompsa 380168561Sthompsa TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor)); 381168561Sthompsa du->ldu_actor = lp->lp_actor; 382168561Sthompsa 383168561Sthompsa TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO, 384168561Sthompsa sizeof(du->ldu_partner)); 385168561Sthompsa du->ldu_partner = lp->lp_partner; 386168561Sthompsa 387168561Sthompsa TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO, 388168561Sthompsa sizeof(du->ldu_collector)); 389168561Sthompsa du->ldu_collector.lci_maxdelay = 0; 390168561Sthompsa 391287723Shrs if (V_lacp_debug > 0) { 392236062Sthompsa lacp_dprintf(lp, "lacpdu transmit\n"); 393236062Sthompsa lacp_dump_lacpdu(du); 394236062Sthompsa } 395168561Sthompsa 396168561Sthompsa m->m_flags |= M_MCAST; 397168561Sthompsa 398168561Sthompsa /* 399168561Sthompsa * XXX should use higher priority queue. 400168561Sthompsa * otherwise network congestion can break aggregation. 401168561Sthompsa */ 402168561Sthompsa 403168793Sthompsa error = lagg_enqueue(lp->lp_ifp, m); 404168561Sthompsa return (error); 405168561Sthompsa} 406168561Sthompsa 407169739Sthompsastatic int 408169739Sthompsalacp_xmit_marker(struct lacp_port *lp) 409169739Sthompsa{ 410169739Sthompsa struct lagg_port *lgp = lp->lp_lagg; 411169739Sthompsa struct mbuf *m; 412169739Sthompsa struct markerdu *mdu; 413169739Sthompsa int error; 414169739Sthompsa 415177274Sthompsa LACP_LOCK_ASSERT(lp->lp_lsc); 416169739Sthompsa 417243882Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 418169739Sthompsa if (m == NULL) { 419169739Sthompsa return (ENOMEM); 420169739Sthompsa } 421169739Sthompsa m->m_len = m->m_pkthdr.len = sizeof(*mdu); 422169739Sthompsa 423169739Sthompsa mdu = mtod(m, struct markerdu *); 424169739Sthompsa memset(mdu, 0, sizeof(*mdu)); 425169739Sthompsa 426169739Sthompsa memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols, 427169739Sthompsa ETHER_ADDR_LEN); 428169739Sthompsa memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN); 429169739Sthompsa mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW); 430169739Sthompsa 431169739Sthompsa mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER; 432169739Sthompsa mdu->mdu_sph.sph_version = 1; 433169739Sthompsa 434169739Sthompsa /* Bump the transaction id and copy over the marker info */ 435169739Sthompsa lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1); 436169739Sthompsa TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info)); 437169739Sthompsa mdu->mdu_info = lp->lp_marker; 438169739Sthompsa 439169739Sthompsa LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n", 440169739Sthompsa ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":", 441169739Sthompsa ntohl(mdu->mdu_info.mi_rq_xid))); 442169739Sthompsa 443169739Sthompsa m->m_flags |= M_MCAST; 444169739Sthompsa error = lagg_enqueue(lp->lp_ifp, m); 445169739Sthompsa return (error); 446169739Sthompsa} 447177274Sthompsa 448168561Sthompsavoid 449168793Sthompsalacp_linkstate(struct lagg_port *lgp) 450168561Sthompsa{ 451168793Sthompsa struct lacp_port *lp = LACP_PORT(lgp); 452177274Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 453168793Sthompsa struct ifnet *ifp = lgp->lp_ifp; 454168561Sthompsa struct ifmediareq ifmr; 455168561Sthompsa int error = 0; 456168561Sthompsa u_int media; 457168561Sthompsa uint8_t old_state; 458168561Sthompsa uint16_t old_key; 459168561Sthompsa 460168561Sthompsa bzero((char *)&ifmr, sizeof(ifmr)); 461168561Sthompsa error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr); 462168561Sthompsa if (error != 0) 463168561Sthompsa return; 464168561Sthompsa 465177274Sthompsa LACP_LOCK(lsc); 466168561Sthompsa media = ifmr.ifm_active; 467169227Sthompsa LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, " 468169227Sthompsa "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER, 469169227Sthompsa (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP)); 470168561Sthompsa old_state = lp->lp_state; 471168561Sthompsa old_key = lp->lp_key; 472168561Sthompsa 473168561Sthompsa lp->lp_media = media; 474170599Sthompsa /* 475169227Sthompsa * If the port is not an active full duplex Ethernet link then it can 476169227Sthompsa * not be aggregated. 477169227Sthompsa */ 478169227Sthompsa if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 || 479169227Sthompsa ifp->if_link_state != LINK_STATE_UP) { 480168561Sthompsa lacp_port_disable(lp); 481168561Sthompsa } else { 482168561Sthompsa lacp_port_enable(lp); 483168561Sthompsa } 484168561Sthompsa lp->lp_key = lacp_compose_key(lp); 485168561Sthompsa 486168561Sthompsa if (old_state != lp->lp_state || old_key != lp->lp_key) { 487168561Sthompsa LACP_DPRINTF((lp, "-> UNSELECTED\n")); 488168561Sthompsa lp->lp_selected = LACP_UNSELECTED; 489168561Sthompsa } 490177274Sthompsa LACP_UNLOCK(lsc); 491168561Sthompsa} 492168561Sthompsa 493168561Sthompsastatic void 494168561Sthompsalacp_tick(void *arg) 495168561Sthompsa{ 496168561Sthompsa struct lacp_softc *lsc = arg; 497168561Sthompsa struct lacp_port *lp; 498168561Sthompsa 499168561Sthompsa LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) { 500168561Sthompsa if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) 501168561Sthompsa continue; 502168561Sthompsa 503287723Shrs CURVNET_SET(lp->lp_ifp->if_vnet); 504168561Sthompsa lacp_run_timers(lp); 505168561Sthompsa 506168561Sthompsa lacp_select(lp); 507168561Sthompsa lacp_sm_mux(lp); 508168561Sthompsa lacp_sm_tx(lp); 509168561Sthompsa lacp_sm_ptx_tx_schedule(lp); 510287723Shrs CURVNET_RESTORE(); 511168561Sthompsa } 512168561Sthompsa callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc); 513168561Sthompsa} 514168561Sthompsa 515168561Sthompsaint 516168793Sthompsalacp_port_create(struct lagg_port *lgp) 517168561Sthompsa{ 518170599Sthompsa struct lagg_softc *sc = lgp->lp_softc; 519170599Sthompsa struct lacp_softc *lsc = LACP_SOFTC(sc); 520168561Sthompsa struct lacp_port *lp; 521168793Sthompsa struct ifnet *ifp = lgp->lp_ifp; 522168561Sthompsa struct sockaddr_dl sdl; 523168561Sthompsa struct ifmultiaddr *rifma = NULL; 524168561Sthompsa int error; 525168561Sthompsa 526168561Sthompsa bzero((char *)&sdl, sizeof(sdl)); 527168561Sthompsa sdl.sdl_len = sizeof(sdl); 528168561Sthompsa sdl.sdl_family = AF_LINK; 529168561Sthompsa sdl.sdl_index = ifp->if_index; 530168561Sthompsa sdl.sdl_type = IFT_ETHER; 531168561Sthompsa sdl.sdl_alen = ETHER_ADDR_LEN; 532168561Sthompsa 533168561Sthompsa bcopy(ðermulticastaddr_slowprotocols, 534168561Sthompsa LLADDR(&sdl), ETHER_ADDR_LEN); 535168561Sthompsa error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma); 536168561Sthompsa if (error) { 537168793Sthompsa printf("%s: ADDMULTI failed on %s\n", __func__, lgp->lp_ifname); 538168561Sthompsa return (error); 539168561Sthompsa } 540168561Sthompsa 541168561Sthompsa lp = malloc(sizeof(struct lacp_port), 542168561Sthompsa M_DEVBUF, M_NOWAIT|M_ZERO); 543168561Sthompsa if (lp == NULL) 544168561Sthompsa return (ENOMEM); 545168561Sthompsa 546177274Sthompsa LACP_LOCK(lsc); 547168793Sthompsa lgp->lp_psc = (caddr_t)lp; 548168561Sthompsa lp->lp_ifp = ifp; 549168793Sthompsa lp->lp_lagg = lgp; 550168561Sthompsa lp->lp_lsc = lsc; 551169327Sthompsa lp->lp_ifma = rifma; 552168561Sthompsa 553168561Sthompsa LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next); 554168561Sthompsa 555168561Sthompsa lacp_fill_actorinfo(lp, &lp->lp_actor); 556169739Sthompsa lacp_fill_markerinfo(lp, &lp->lp_marker); 557313039Srpokala lp->lp_state = LACP_STATE_ACTIVITY; 558168561Sthompsa lp->lp_aggregator = NULL; 559177274Sthompsa lacp_sm_rx_set_expired(lp); 560177274Sthompsa LACP_UNLOCK(lsc); 561168793Sthompsa lacp_linkstate(lgp); 562168561Sthompsa 563168561Sthompsa return (0); 564168561Sthompsa} 565168561Sthompsa 566168561Sthompsavoid 567168793Sthompsalacp_port_destroy(struct lagg_port *lgp) 568168561Sthompsa{ 569168793Sthompsa struct lacp_port *lp = LACP_PORT(lgp); 570177274Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 571169327Sthompsa int i; 572168561Sthompsa 573177274Sthompsa LACP_LOCK(lsc); 574168561Sthompsa for (i = 0; i < LACP_NTIMER; i++) { 575168561Sthompsa LACP_TIMER_DISARM(lp, i); 576168561Sthompsa } 577168561Sthompsa 578168561Sthompsa lacp_disable_collecting(lp); 579168561Sthompsa lacp_disable_distributing(lp); 580168561Sthompsa lacp_unselect(lp); 581168561Sthompsa 582287723Shrs LIST_REMOVE(lp, lp_next); 583287723Shrs LACP_UNLOCK(lsc); 584287723Shrs 585169328Sthompsa /* The address may have already been removed by if_purgemaddrs() */ 586169328Sthompsa if (!lgp->lp_detaching) 587169328Sthompsa if_delmulti_ifma(lp->lp_ifma); 588168561Sthompsa 589168561Sthompsa free(lp, M_DEVBUF); 590168561Sthompsa} 591168561Sthompsa 592171247Sthompsavoid 593171247Sthompsalacp_req(struct lagg_softc *sc, caddr_t data) 594171247Sthompsa{ 595171247Sthompsa struct lacp_opreq *req = (struct lacp_opreq *)data; 596171247Sthompsa struct lacp_softc *lsc = LACP_SOFTC(sc); 597272672Sasomers struct lacp_aggregator *la; 598171247Sthompsa 599272672Sasomers bzero(req, sizeof(struct lacp_opreq)); 600272672Sasomers 601272672Sasomers /* 602272672Sasomers * If the LACP softc is NULL, return with the opreq structure full of 603272672Sasomers * zeros. It is normal for the softc to be NULL while the lagg is 604272672Sasomers * being destroyed. 605272672Sasomers */ 606272672Sasomers if (NULL == lsc) 607272672Sasomers return; 608272672Sasomers 609272672Sasomers la = lsc->lsc_active_aggregator; 610177274Sthompsa LACP_LOCK(lsc); 611171247Sthompsa if (la != NULL) { 612171247Sthompsa req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio); 613171247Sthompsa memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac, 614171247Sthompsa ETHER_ADDR_LEN); 615171247Sthompsa req->actor_key = ntohs(la->la_actor.lip_key); 616171247Sthompsa req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio); 617171247Sthompsa req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno); 618171247Sthompsa req->actor_state = la->la_actor.lip_state; 619171247Sthompsa 620171247Sthompsa req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio); 621171247Sthompsa memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac, 622171247Sthompsa ETHER_ADDR_LEN); 623171247Sthompsa req->partner_key = ntohs(la->la_partner.lip_key); 624171247Sthompsa req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio); 625171247Sthompsa req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno); 626171247Sthompsa req->partner_state = la->la_partner.lip_state; 627171247Sthompsa } 628177274Sthompsa LACP_UNLOCK(lsc); 629171247Sthompsa} 630171247Sthompsa 631171247Sthompsavoid 632171247Sthompsalacp_portreq(struct lagg_port *lgp, caddr_t data) 633171247Sthompsa{ 634171247Sthompsa struct lacp_opreq *req = (struct lacp_opreq *)data; 635171247Sthompsa struct lacp_port *lp = LACP_PORT(lgp); 636177274Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 637171247Sthompsa 638177274Sthompsa LACP_LOCK(lsc); 639171247Sthompsa req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio); 640171247Sthompsa memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac, 641171247Sthompsa ETHER_ADDR_LEN); 642171247Sthompsa req->actor_key = ntohs(lp->lp_actor.lip_key); 643171247Sthompsa req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio); 644171247Sthompsa req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno); 645171247Sthompsa req->actor_state = lp->lp_actor.lip_state; 646171247Sthompsa 647171247Sthompsa req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio); 648171247Sthompsa memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac, 649171247Sthompsa ETHER_ADDR_LEN); 650171247Sthompsa req->partner_key = ntohs(lp->lp_partner.lip_key); 651171247Sthompsa req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio); 652171247Sthompsa req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno); 653171247Sthompsa req->partner_state = lp->lp_partner.lip_state; 654177274Sthompsa LACP_UNLOCK(lsc); 655171247Sthompsa} 656171247Sthompsa 657168561Sthompsastatic void 658168561Sthompsalacp_disable_collecting(struct lacp_port *lp) 659168561Sthompsa{ 660168561Sthompsa LACP_DPRINTF((lp, "collecting disabled\n")); 661168561Sthompsa lp->lp_state &= ~LACP_STATE_COLLECTING; 662168561Sthompsa} 663168561Sthompsa 664168561Sthompsastatic void 665168561Sthompsalacp_enable_collecting(struct lacp_port *lp) 666168561Sthompsa{ 667168561Sthompsa LACP_DPRINTF((lp, "collecting enabled\n")); 668168561Sthompsa lp->lp_state |= LACP_STATE_COLLECTING; 669168561Sthompsa} 670168561Sthompsa 671168561Sthompsastatic void 672168561Sthompsalacp_disable_distributing(struct lacp_port *lp) 673168561Sthompsa{ 674168561Sthompsa struct lacp_aggregator *la = lp->lp_aggregator; 675168561Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 676253314Sadrian struct lagg_softc *sc = lsc->lsc_softc; 677168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 678168561Sthompsa 679177274Sthompsa LACP_LOCK_ASSERT(lsc); 680168561Sthompsa 681168561Sthompsa if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) { 682168561Sthompsa return; 683168561Sthompsa } 684168561Sthompsa 685168561Sthompsa KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports")); 686168561Sthompsa KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports)); 687168561Sthompsa KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid")); 688168561Sthompsa 689168561Sthompsa LACP_DPRINTF((lp, "disable distributing on aggregator %s, " 690168561Sthompsa "nports %d -> %d\n", 691168561Sthompsa lacp_format_lagid_aggregator(la, buf, sizeof(buf)), 692168561Sthompsa la->la_nports, la->la_nports - 1)); 693168561Sthompsa 694168561Sthompsa TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q); 695168561Sthompsa la->la_nports--; 696253314Sadrian sc->sc_active = la->la_nports; 697168561Sthompsa 698168561Sthompsa if (lsc->lsc_active_aggregator == la) { 699177274Sthompsa lacp_suppress_distributing(lsc, la); 700168561Sthompsa lacp_select_active_aggregator(lsc); 701177274Sthompsa /* regenerate the port map, the active aggregator has changed */ 702177274Sthompsa lacp_update_portmap(lsc); 703168561Sthompsa } 704177274Sthompsa 705177274Sthompsa lp->lp_state &= ~LACP_STATE_DISTRIBUTING; 706168561Sthompsa} 707168561Sthompsa 708168561Sthompsastatic void 709168561Sthompsalacp_enable_distributing(struct lacp_port *lp) 710168561Sthompsa{ 711168561Sthompsa struct lacp_aggregator *la = lp->lp_aggregator; 712168561Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 713253314Sadrian struct lagg_softc *sc = lsc->lsc_softc; 714168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 715168561Sthompsa 716177274Sthompsa LACP_LOCK_ASSERT(lsc); 717168561Sthompsa 718168561Sthompsa if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) { 719168561Sthompsa return; 720168561Sthompsa } 721168561Sthompsa 722168561Sthompsa LACP_DPRINTF((lp, "enable distributing on aggregator %s, " 723168561Sthompsa "nports %d -> %d\n", 724168561Sthompsa lacp_format_lagid_aggregator(la, buf, sizeof(buf)), 725168561Sthompsa la->la_nports, la->la_nports + 1)); 726168561Sthompsa 727168561Sthompsa KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid")); 728168561Sthompsa TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q); 729168561Sthompsa la->la_nports++; 730253314Sadrian sc->sc_active = la->la_nports; 731168561Sthompsa 732168561Sthompsa lp->lp_state |= LACP_STATE_DISTRIBUTING; 733168561Sthompsa 734177274Sthompsa if (lsc->lsc_active_aggregator == la) { 735177274Sthompsa lacp_suppress_distributing(lsc, la); 736177274Sthompsa lacp_update_portmap(lsc); 737177274Sthompsa } else 738177274Sthompsa /* try to become the active aggregator */ 739168561Sthompsa lacp_select_active_aggregator(lsc); 740168561Sthompsa} 741168561Sthompsa 742168561Sthompsastatic void 743168561Sthompsalacp_transit_expire(void *vp) 744168561Sthompsa{ 745168561Sthompsa struct lacp_softc *lsc = vp; 746168561Sthompsa 747177274Sthompsa LACP_LOCK_ASSERT(lsc); 748177274Sthompsa 749287723Shrs CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet); 750236062Sthompsa LACP_TRACE(NULL); 751287723Shrs CURVNET_RESTORE(); 752236062Sthompsa 753168561Sthompsa lsc->lsc_suppress_distributing = FALSE; 754168561Sthompsa} 755168561Sthompsa 756287723Shrsvoid 757170599Sthompsalacp_attach(struct lagg_softc *sc) 758168561Sthompsa{ 759168561Sthompsa struct lacp_softc *lsc; 760168561Sthompsa 761287723Shrs lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO); 762168561Sthompsa 763170599Sthompsa sc->sc_psc = (caddr_t)lsc; 764170599Sthompsa lsc->lsc_softc = sc; 765168561Sthompsa 766168561Sthompsa lsc->lsc_hashkey = arc4random(); 767168561Sthompsa lsc->lsc_active_aggregator = NULL; 768290736Ssmh lsc->lsc_strict_mode = VNET(lacp_default_strict_mode); 769177274Sthompsa LACP_LOCK_INIT(lsc); 770168561Sthompsa TAILQ_INIT(&lsc->lsc_aggregators); 771168561Sthompsa LIST_INIT(&lsc->lsc_ports); 772168561Sthompsa 773177274Sthompsa callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0); 774177274Sthompsa callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0); 775168561Sthompsa 776168793Sthompsa /* if the lagg is already up then do the same */ 777170599Sthompsa if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) 778170599Sthompsa lacp_init(sc); 779168561Sthompsa} 780168561Sthompsa 781168561Sthompsaint 782287723Shrslacp_detach(void *psc) 783168561Sthompsa{ 784287723Shrs struct lacp_softc *lsc = (struct lacp_softc *)psc; 785168561Sthompsa 786168561Sthompsa KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators), 787168561Sthompsa ("aggregators still active")); 788168561Sthompsa KASSERT(lsc->lsc_active_aggregator == NULL, 789168561Sthompsa ("aggregator still attached")); 790168561Sthompsa 791168561Sthompsa callout_drain(&lsc->lsc_transit_callout); 792168561Sthompsa callout_drain(&lsc->lsc_callout); 793168561Sthompsa 794177274Sthompsa LACP_LOCK_DESTROY(lsc); 795168561Sthompsa free(lsc, M_DEVBUF); 796168561Sthompsa return (0); 797168561Sthompsa} 798168561Sthompsa 799168561Sthompsavoid 800170599Sthompsalacp_init(struct lagg_softc *sc) 801168561Sthompsa{ 802170599Sthompsa struct lacp_softc *lsc = LACP_SOFTC(sc); 803168561Sthompsa 804177274Sthompsa LACP_LOCK(lsc); 805168561Sthompsa callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc); 806177274Sthompsa LACP_UNLOCK(lsc); 807168561Sthompsa} 808168561Sthompsa 809168561Sthompsavoid 810170599Sthompsalacp_stop(struct lagg_softc *sc) 811168561Sthompsa{ 812170599Sthompsa struct lacp_softc *lsc = LACP_SOFTC(sc); 813168561Sthompsa 814177274Sthompsa LACP_LOCK(lsc); 815168561Sthompsa callout_stop(&lsc->lsc_transit_callout); 816168561Sthompsa callout_stop(&lsc->lsc_callout); 817177274Sthompsa LACP_UNLOCK(lsc); 818168561Sthompsa} 819168561Sthompsa 820168793Sthompsastruct lagg_port * 821170599Sthompsalacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m) 822168561Sthompsa{ 823170599Sthompsa struct lacp_softc *lsc = LACP_SOFTC(sc); 824177274Sthompsa struct lacp_portmap *pm; 825168561Sthompsa struct lacp_port *lp; 826168561Sthompsa uint32_t hash; 827168561Sthompsa 828168561Sthompsa if (__predict_false(lsc->lsc_suppress_distributing)) { 829168561Sthompsa LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__)); 830168561Sthompsa return (NULL); 831168561Sthompsa } 832168561Sthompsa 833177274Sthompsa pm = &lsc->lsc_pmap[lsc->lsc_activemap]; 834177274Sthompsa if (pm->pm_count == 0) { 835168561Sthompsa LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__)); 836168561Sthompsa return (NULL); 837168561Sthompsa } 838168561Sthompsa 839287723Shrs if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) && 840281955Shiren M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 841260179Sscottl hash = m->m_pkthdr.flowid >> sc->flowid_shift; 842191692Sthompsa else 843232629Sthompsa hash = lagg_hashmbuf(sc, m, lsc->lsc_hashkey); 844177274Sthompsa hash %= pm->pm_count; 845177274Sthompsa lp = pm->pm_map[hash]; 846168561Sthompsa 847168561Sthompsa KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0, 848168561Sthompsa ("aggregated port is not distributing")); 849168561Sthompsa 850168793Sthompsa return (lp->lp_lagg); 851168561Sthompsa} 852168561Sthompsa/* 853168561Sthompsa * lacp_suppress_distributing: drop transmit packets for a while 854168561Sthompsa * to preserve packet ordering. 855168561Sthompsa */ 856168561Sthompsa 857168561Sthompsastatic void 858168561Sthompsalacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la) 859168561Sthompsa{ 860169739Sthompsa struct lacp_port *lp; 861169739Sthompsa 862168561Sthompsa if (lsc->lsc_active_aggregator != la) { 863168561Sthompsa return; 864168561Sthompsa } 865168561Sthompsa 866236062Sthompsa LACP_TRACE(NULL); 867236062Sthompsa 868168561Sthompsa lsc->lsc_suppress_distributing = TRUE; 869169739Sthompsa 870169739Sthompsa /* send a marker frame down each port to verify the queues are empty */ 871169739Sthompsa LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) { 872169739Sthompsa lp->lp_flags |= LACP_PORT_MARK; 873169739Sthompsa lacp_xmit_marker(lp); 874169739Sthompsa } 875169739Sthompsa 876169739Sthompsa /* set a timeout for the marker frames */ 877168561Sthompsa callout_reset(&lsc->lsc_transit_callout, 878168561Sthompsa LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc); 879168561Sthompsa} 880168561Sthompsa 881168561Sthompsastatic int 882168561Sthompsalacp_compare_peerinfo(const struct lacp_peerinfo *a, 883168561Sthompsa const struct lacp_peerinfo *b) 884168561Sthompsa{ 885168561Sthompsa return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state))); 886168561Sthompsa} 887168561Sthompsa 888168561Sthompsastatic int 889168561Sthompsalacp_compare_systemid(const struct lacp_systemid *a, 890168561Sthompsa const struct lacp_systemid *b) 891168561Sthompsa{ 892168561Sthompsa return (memcmp(a, b, sizeof(*a))); 893168561Sthompsa} 894168561Sthompsa 895168561Sthompsa#if 0 /* unused */ 896168561Sthompsastatic int 897168561Sthompsalacp_compare_portid(const struct lacp_portid *a, 898168561Sthompsa const struct lacp_portid *b) 899168561Sthompsa{ 900168561Sthompsa return (memcmp(a, b, sizeof(*a))); 901168561Sthompsa} 902168561Sthompsa#endif 903168561Sthompsa 904168561Sthompsastatic uint64_t 905168561Sthompsalacp_aggregator_bandwidth(struct lacp_aggregator *la) 906168561Sthompsa{ 907168561Sthompsa struct lacp_port *lp; 908168561Sthompsa uint64_t speed; 909168561Sthompsa 910168561Sthompsa lp = TAILQ_FIRST(&la->la_ports); 911168561Sthompsa if (lp == NULL) { 912168561Sthompsa return (0); 913168561Sthompsa } 914168561Sthompsa 915168561Sthompsa speed = ifmedia_baudrate(lp->lp_media); 916168561Sthompsa speed *= la->la_nports; 917168561Sthompsa if (speed == 0) { 918168561Sthompsa LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n", 919168561Sthompsa lp->lp_media, la->la_nports)); 920168561Sthompsa } 921168561Sthompsa 922168561Sthompsa return (speed); 923168561Sthompsa} 924168561Sthompsa 925168561Sthompsa/* 926168561Sthompsa * lacp_select_active_aggregator: select an aggregator to be used to transmit 927168793Sthompsa * packets from lagg(4) interface. 928168561Sthompsa */ 929168561Sthompsa 930168561Sthompsastatic void 931168561Sthompsalacp_select_active_aggregator(struct lacp_softc *lsc) 932168561Sthompsa{ 933168561Sthompsa struct lacp_aggregator *la; 934168561Sthompsa struct lacp_aggregator *best_la = NULL; 935168561Sthompsa uint64_t best_speed = 0; 936168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 937168561Sthompsa 938236062Sthompsa LACP_TRACE(NULL); 939168561Sthompsa 940168561Sthompsa TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) { 941168561Sthompsa uint64_t speed; 942168561Sthompsa 943168561Sthompsa if (la->la_nports == 0) { 944168561Sthompsa continue; 945168561Sthompsa } 946168561Sthompsa 947168561Sthompsa speed = lacp_aggregator_bandwidth(la); 948168561Sthompsa LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n", 949168561Sthompsa lacp_format_lagid_aggregator(la, buf, sizeof(buf)), 950168561Sthompsa speed, la->la_nports)); 951169741Sthompsa 952169741Sthompsa /* This aggregator is chosen if 953169741Sthompsa * the partner has a better system priority 954169741Sthompsa * or, the total aggregated speed is higher 955169741Sthompsa * or, it is already the chosen aggregator 956169741Sthompsa */ 957169741Sthompsa if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) < 958169741Sthompsa LACP_SYS_PRI(best_la->la_partner)) || 959169741Sthompsa speed > best_speed || 960168561Sthompsa (speed == best_speed && 961168561Sthompsa la == lsc->lsc_active_aggregator)) { 962168561Sthompsa best_la = la; 963168561Sthompsa best_speed = speed; 964168561Sthompsa } 965168561Sthompsa } 966168561Sthompsa 967168561Sthompsa KASSERT(best_la == NULL || best_la->la_nports > 0, 968168561Sthompsa ("invalid aggregator refcnt")); 969168561Sthompsa KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports), 970168561Sthompsa ("invalid aggregator list")); 971168561Sthompsa 972168561Sthompsa if (lsc->lsc_active_aggregator != best_la) { 973168561Sthompsa LACP_DPRINTF((NULL, "active aggregator changed\n")); 974168561Sthompsa LACP_DPRINTF((NULL, "old %s\n", 975168561Sthompsa lacp_format_lagid_aggregator(lsc->lsc_active_aggregator, 976168561Sthompsa buf, sizeof(buf)))); 977168561Sthompsa } else { 978168561Sthompsa LACP_DPRINTF((NULL, "active aggregator not changed\n")); 979168561Sthompsa } 980168561Sthompsa LACP_DPRINTF((NULL, "new %s\n", 981168561Sthompsa lacp_format_lagid_aggregator(best_la, buf, sizeof(buf)))); 982168561Sthompsa 983168561Sthompsa if (lsc->lsc_active_aggregator != best_la) { 984168561Sthompsa lsc->lsc_active_aggregator = best_la; 985177274Sthompsa lacp_update_portmap(lsc); 986168561Sthompsa if (best_la) { 987168561Sthompsa lacp_suppress_distributing(lsc, best_la); 988168561Sthompsa } 989168561Sthompsa } 990168561Sthompsa} 991168561Sthompsa 992177274Sthompsa/* 993177274Sthompsa * Updated the inactive portmap array with the new list of ports and 994177274Sthompsa * make it live. 995177274Sthompsa */ 996177274Sthompsastatic void 997177274Sthompsalacp_update_portmap(struct lacp_softc *lsc) 998177274Sthompsa{ 999253314Sadrian struct lagg_softc *sc = lsc->lsc_softc; 1000177274Sthompsa struct lacp_aggregator *la; 1001177274Sthompsa struct lacp_portmap *p; 1002177274Sthompsa struct lacp_port *lp; 1003253314Sadrian uint64_t speed; 1004177274Sthompsa u_int newmap; 1005177274Sthompsa int i; 1006177274Sthompsa 1007177274Sthompsa newmap = lsc->lsc_activemap == 0 ? 1 : 0; 1008177274Sthompsa p = &lsc->lsc_pmap[newmap]; 1009177274Sthompsa la = lsc->lsc_active_aggregator; 1010253314Sadrian speed = 0; 1011177274Sthompsa bzero(p, sizeof(struct lacp_portmap)); 1012177274Sthompsa 1013177274Sthompsa if (la != NULL && la->la_nports > 0) { 1014177274Sthompsa p->pm_count = la->la_nports; 1015177274Sthompsa i = 0; 1016177274Sthompsa TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) 1017177274Sthompsa p->pm_map[i++] = lp; 1018177274Sthompsa KASSERT(i == p->pm_count, ("Invalid port count")); 1019253314Sadrian speed = lacp_aggregator_bandwidth(la); 1020177274Sthompsa } 1021253314Sadrian sc->sc_ifp->if_baudrate = speed; 1022177274Sthompsa 1023177274Sthompsa /* switch the active portmap over */ 1024177274Sthompsa atomic_store_rel_int(&lsc->lsc_activemap, newmap); 1025177274Sthompsa LACP_DPRINTF((NULL, "Set table %d with %d ports\n", 1026177274Sthompsa lsc->lsc_activemap, 1027177274Sthompsa lsc->lsc_pmap[lsc->lsc_activemap].pm_count)); 1028177274Sthompsa} 1029177274Sthompsa 1030168561Sthompsastatic uint16_t 1031168561Sthompsalacp_compose_key(struct lacp_port *lp) 1032168561Sthompsa{ 1033168793Sthompsa struct lagg_port *lgp = lp->lp_lagg; 1034170599Sthompsa struct lagg_softc *sc = lgp->lp_softc; 1035168561Sthompsa u_int media = lp->lp_media; 1036168561Sthompsa uint16_t key; 1037168561Sthompsa 1038168561Sthompsa if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) { 1039168561Sthompsa 1040168561Sthompsa /* 1041168561Sthompsa * non-aggregatable links should have unique keys. 1042168561Sthompsa * 1043168561Sthompsa * XXX this isn't really unique as if_index is 16 bit. 1044168561Sthompsa */ 1045168561Sthompsa 1046168561Sthompsa /* bit 0..14: (some bits of) if_index of this port */ 1047168561Sthompsa key = lp->lp_ifp->if_index; 1048168561Sthompsa /* bit 15: 1 */ 1049168561Sthompsa key |= 0x8000; 1050168561Sthompsa } else { 1051168561Sthompsa u_int subtype = IFM_SUBTYPE(media); 1052168561Sthompsa 1053169227Sthompsa KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type")); 1054169227Sthompsa KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface")); 1055168561Sthompsa 1056257956Sae /* bit 0..4: IFM_SUBTYPE modulo speed */ 1057257956Sae switch (subtype) { 1058257956Sae case IFM_10_T: 1059257956Sae case IFM_10_2: 1060257956Sae case IFM_10_5: 1061257956Sae case IFM_10_STP: 1062257956Sae case IFM_10_FL: 1063257956Sae key = IFM_10_T; 1064257956Sae break; 1065257956Sae case IFM_100_TX: 1066257956Sae case IFM_100_FX: 1067257956Sae case IFM_100_T4: 1068257956Sae case IFM_100_VG: 1069257956Sae case IFM_100_T2: 1070283758Serj case IFM_100_T: 1071257956Sae key = IFM_100_TX; 1072257956Sae break; 1073257956Sae case IFM_1000_SX: 1074257956Sae case IFM_1000_LX: 1075257956Sae case IFM_1000_CX: 1076257956Sae case IFM_1000_T: 1077283758Serj case IFM_1000_KX: 1078283758Serj case IFM_1000_SGMII: 1079283758Serj case IFM_1000_CX_SGMII: 1080257956Sae key = IFM_1000_SX; 1081257956Sae break; 1082257956Sae case IFM_10G_LR: 1083257956Sae case IFM_10G_SR: 1084257956Sae case IFM_10G_CX4: 1085257956Sae case IFM_10G_TWINAX: 1086257956Sae case IFM_10G_TWINAX_LONG: 1087257956Sae case IFM_10G_LRM: 1088257956Sae case IFM_10G_T: 1089283758Serj case IFM_10G_KX4: 1090283758Serj case IFM_10G_KR: 1091283758Serj case IFM_10G_CR1: 1092283758Serj case IFM_10G_ER: 1093283758Serj case IFM_10G_SFI: 1094353414Snp case IFM_10G_AOC: 1095257956Sae key = IFM_10G_LR; 1096257956Sae break; 1097283758Serj case IFM_20G_KR2: 1098283758Serj key = IFM_20G_KR2; 1099283758Serj break; 1100283758Serj case IFM_2500_KX: 1101283758Serj case IFM_2500_T: 1102283758Serj key = IFM_2500_KX; 1103283758Serj break; 1104283758Serj case IFM_5000_T: 1105283758Serj key = IFM_5000_T; 1106283758Serj break; 1107283758Serj case IFM_50G_PCIE: 1108283758Serj case IFM_50G_CR2: 1109283758Serj case IFM_50G_KR2: 1110283758Serj key = IFM_50G_PCIE; 1111283758Serj break; 1112283758Serj case IFM_56G_R4: 1113283758Serj key = IFM_56G_R4; 1114283758Serj break; 1115283758Serj case IFM_25G_PCIE: 1116283758Serj case IFM_25G_CR: 1117283758Serj case IFM_25G_KR: 1118283758Serj case IFM_25G_SR: 1119353414Snp case IFM_25G_LR: 1120353414Snp case IFM_25G_ACC: 1121353414Snp case IFM_25G_AOC: 1122283758Serj key = IFM_25G_PCIE; 1123283758Serj break; 1124257956Sae case IFM_40G_CR4: 1125257956Sae case IFM_40G_SR4: 1126257956Sae case IFM_40G_LR4: 1127283758Serj case IFM_40G_XLPPI: 1128283758Serj case IFM_40G_KR4: 1129257956Sae key = IFM_40G_CR4; 1130257956Sae break; 1131283758Serj case IFM_100G_CR4: 1132283758Serj case IFM_100G_SR4: 1133283758Serj case IFM_100G_KR4: 1134283758Serj case IFM_100G_LR4: 1135283758Serj key = IFM_100G_CR4; 1136283758Serj break; 1137257956Sae default: 1138257956Sae key = subtype; 1139283758Serj break; 1140257956Sae } 1141168793Sthompsa /* bit 5..14: (some bits of) if_index of lagg device */ 1142170599Sthompsa key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5); 1143168561Sthompsa /* bit 15: 0 */ 1144168561Sthompsa } 1145168561Sthompsa return (htons(key)); 1146168561Sthompsa} 1147168561Sthompsa 1148168561Sthompsastatic void 1149168561Sthompsalacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la) 1150168561Sthompsa{ 1151168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 1152168561Sthompsa 1153168561Sthompsa LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n", 1154168561Sthompsa __func__, 1155168561Sthompsa lacp_format_lagid(&la->la_actor, &la->la_partner, 1156168561Sthompsa buf, sizeof(buf)), 1157168561Sthompsa la->la_refcnt, la->la_refcnt + 1)); 1158168561Sthompsa 1159168561Sthompsa KASSERT(la->la_refcnt > 0, ("refcount <= 0")); 1160168561Sthompsa la->la_refcnt++; 1161168561Sthompsa KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount")); 1162168561Sthompsa} 1163168561Sthompsa 1164168561Sthompsastatic void 1165168561Sthompsalacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la) 1166168561Sthompsa{ 1167168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 1168168561Sthompsa 1169168561Sthompsa LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n", 1170168561Sthompsa __func__, 1171168561Sthompsa lacp_format_lagid(&la->la_actor, &la->la_partner, 1172168561Sthompsa buf, sizeof(buf)), 1173168561Sthompsa la->la_refcnt, la->la_refcnt - 1)); 1174168561Sthompsa 1175168561Sthompsa KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt")); 1176168561Sthompsa la->la_refcnt--; 1177168561Sthompsa if (la->la_refcnt > 0) { 1178168561Sthompsa return; 1179168561Sthompsa } 1180168561Sthompsa 1181168561Sthompsa KASSERT(la->la_refcnt == 0, ("refcount not zero")); 1182168561Sthompsa KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active")); 1183168561Sthompsa 1184168561Sthompsa TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q); 1185168561Sthompsa 1186168561Sthompsa free(la, M_DEVBUF); 1187168561Sthompsa} 1188168561Sthompsa 1189168561Sthompsa/* 1190168561Sthompsa * lacp_aggregator_get: allocate an aggregator. 1191168561Sthompsa */ 1192168561Sthompsa 1193168561Sthompsastatic struct lacp_aggregator * 1194168561Sthompsalacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp) 1195168561Sthompsa{ 1196168561Sthompsa struct lacp_aggregator *la; 1197168561Sthompsa 1198168561Sthompsa la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT); 1199168561Sthompsa if (la) { 1200168561Sthompsa la->la_refcnt = 1; 1201168561Sthompsa la->la_nports = 0; 1202168561Sthompsa TAILQ_INIT(&la->la_ports); 1203168561Sthompsa la->la_pending = 0; 1204168561Sthompsa TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q); 1205168561Sthompsa } 1206168561Sthompsa 1207168561Sthompsa return (la); 1208168561Sthompsa} 1209168561Sthompsa 1210168561Sthompsa/* 1211168561Sthompsa * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port. 1212168561Sthompsa */ 1213168561Sthompsa 1214168561Sthompsastatic void 1215168561Sthompsalacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp) 1216168561Sthompsa{ 1217168561Sthompsa lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner); 1218168561Sthompsa lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor); 1219168561Sthompsa 1220168561Sthompsa la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION; 1221168561Sthompsa} 1222168561Sthompsa 1223168561Sthompsastatic void 1224168561Sthompsalacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr, 1225168561Sthompsa const struct lacp_peerinfo *lpi_port) 1226168561Sthompsa{ 1227168561Sthompsa memset(lpi_aggr, 0, sizeof(*lpi_aggr)); 1228168561Sthompsa lpi_aggr->lip_systemid = lpi_port->lip_systemid; 1229168561Sthompsa lpi_aggr->lip_key = lpi_port->lip_key; 1230168561Sthompsa} 1231168561Sthompsa 1232168561Sthompsa/* 1233168561Sthompsa * lacp_aggregator_is_compatible: check if a port can join to an aggregator. 1234168561Sthompsa */ 1235168561Sthompsa 1236168561Sthompsastatic int 1237168561Sthompsalacp_aggregator_is_compatible(const struct lacp_aggregator *la, 1238168561Sthompsa const struct lacp_port *lp) 1239168561Sthompsa{ 1240168561Sthompsa if (!(lp->lp_state & LACP_STATE_AGGREGATION) || 1241168561Sthompsa !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) { 1242168561Sthompsa return (0); 1243168561Sthompsa } 1244168561Sthompsa 1245168561Sthompsa if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) { 1246168561Sthompsa return (0); 1247168561Sthompsa } 1248168561Sthompsa 1249168561Sthompsa if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) { 1250168561Sthompsa return (0); 1251168561Sthompsa } 1252168561Sthompsa 1253168561Sthompsa if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) { 1254168561Sthompsa return (0); 1255168561Sthompsa } 1256168561Sthompsa 1257168561Sthompsa return (1); 1258168561Sthompsa} 1259168561Sthompsa 1260168561Sthompsastatic int 1261168561Sthompsalacp_peerinfo_is_compatible(const struct lacp_peerinfo *a, 1262168561Sthompsa const struct lacp_peerinfo *b) 1263168561Sthompsa{ 1264168561Sthompsa if (memcmp(&a->lip_systemid, &b->lip_systemid, 1265168561Sthompsa sizeof(a->lip_systemid))) { 1266168561Sthompsa return (0); 1267168561Sthompsa } 1268168561Sthompsa 1269168561Sthompsa if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) { 1270168561Sthompsa return (0); 1271168561Sthompsa } 1272168561Sthompsa 1273168561Sthompsa return (1); 1274168561Sthompsa} 1275168561Sthompsa 1276168561Sthompsastatic void 1277168561Sthompsalacp_port_enable(struct lacp_port *lp) 1278168561Sthompsa{ 1279168561Sthompsa lp->lp_state |= LACP_STATE_AGGREGATION; 1280168561Sthompsa} 1281168561Sthompsa 1282168561Sthompsastatic void 1283168561Sthompsalacp_port_disable(struct lacp_port *lp) 1284168561Sthompsa{ 1285168561Sthompsa lacp_set_mux(lp, LACP_MUX_DETACHED); 1286168561Sthompsa 1287168561Sthompsa lp->lp_state &= ~LACP_STATE_AGGREGATION; 1288168561Sthompsa lp->lp_selected = LACP_UNSELECTED; 1289168561Sthompsa lacp_sm_rx_record_default(lp); 1290168561Sthompsa lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION; 1291168561Sthompsa lp->lp_state &= ~LACP_STATE_EXPIRED; 1292168561Sthompsa} 1293168561Sthompsa 1294168561Sthompsa/* 1295168561Sthompsa * lacp_select: select an aggregator. create one if necessary. 1296168561Sthompsa */ 1297168561Sthompsastatic void 1298168561Sthompsalacp_select(struct lacp_port *lp) 1299168561Sthompsa{ 1300168561Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 1301168561Sthompsa struct lacp_aggregator *la; 1302168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 1303168561Sthompsa 1304168561Sthompsa if (lp->lp_aggregator) { 1305168561Sthompsa return; 1306168561Sthompsa } 1307168561Sthompsa 1308168561Sthompsa KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE), 1309168561Sthompsa ("timer_wait_while still active")); 1310168561Sthompsa 1311168561Sthompsa LACP_DPRINTF((lp, "port lagid=%s\n", 1312168561Sthompsa lacp_format_lagid(&lp->lp_actor, &lp->lp_partner, 1313168561Sthompsa buf, sizeof(buf)))); 1314168561Sthompsa 1315168561Sthompsa TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) { 1316168561Sthompsa if (lacp_aggregator_is_compatible(la, lp)) { 1317168561Sthompsa break; 1318168561Sthompsa } 1319168561Sthompsa } 1320168561Sthompsa 1321168561Sthompsa if (la == NULL) { 1322168561Sthompsa la = lacp_aggregator_get(lsc, lp); 1323168561Sthompsa if (la == NULL) { 1324168561Sthompsa LACP_DPRINTF((lp, "aggregator creation failed\n")); 1325168561Sthompsa 1326168561Sthompsa /* 1327168561Sthompsa * will retry on the next tick. 1328168561Sthompsa */ 1329168561Sthompsa 1330168561Sthompsa return; 1331168561Sthompsa } 1332168561Sthompsa lacp_fill_aggregator_id(la, lp); 1333168561Sthompsa LACP_DPRINTF((lp, "aggregator created\n")); 1334168561Sthompsa } else { 1335168561Sthompsa LACP_DPRINTF((lp, "compatible aggregator found\n")); 1336177274Sthompsa if (la->la_refcnt == LACP_MAX_PORTS) 1337177274Sthompsa return; 1338168561Sthompsa lacp_aggregator_addref(lsc, la); 1339168561Sthompsa } 1340168561Sthompsa 1341168561Sthompsa LACP_DPRINTF((lp, "aggregator lagid=%s\n", 1342168561Sthompsa lacp_format_lagid(&la->la_actor, &la->la_partner, 1343168561Sthompsa buf, sizeof(buf)))); 1344168561Sthompsa 1345168561Sthompsa lp->lp_aggregator = la; 1346168561Sthompsa lp->lp_selected = LACP_SELECTED; 1347168561Sthompsa} 1348168561Sthompsa 1349168561Sthompsa/* 1350168561Sthompsa * lacp_unselect: finish unselect/detach process. 1351168561Sthompsa */ 1352168561Sthompsa 1353168561Sthompsastatic void 1354168561Sthompsalacp_unselect(struct lacp_port *lp) 1355168561Sthompsa{ 1356168561Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 1357168561Sthompsa struct lacp_aggregator *la = lp->lp_aggregator; 1358168561Sthompsa 1359168561Sthompsa KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE), 1360168561Sthompsa ("timer_wait_while still active")); 1361168561Sthompsa 1362168561Sthompsa if (la == NULL) { 1363168561Sthompsa return; 1364168561Sthompsa } 1365168561Sthompsa 1366168561Sthompsa lp->lp_aggregator = NULL; 1367168561Sthompsa lacp_aggregator_delref(lsc, la); 1368168561Sthompsa} 1369168561Sthompsa 1370168561Sthompsa/* mux machine */ 1371168561Sthompsa 1372168561Sthompsastatic void 1373168561Sthompsalacp_sm_mux(struct lacp_port *lp) 1374168561Sthompsa{ 1375253314Sadrian struct lagg_port *lgp = lp->lp_lagg; 1376253314Sadrian struct lagg_softc *sc = lgp->lp_softc; 1377168561Sthompsa enum lacp_mux_state new_state; 1378168561Sthompsa boolean_t p_sync = 1379168561Sthompsa (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0; 1380168561Sthompsa boolean_t p_collecting = 1381168561Sthompsa (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0; 1382168561Sthompsa enum lacp_selected selected = lp->lp_selected; 1383168561Sthompsa struct lacp_aggregator *la; 1384168561Sthompsa 1385287723Shrs if (V_lacp_debug > 1) 1386253314Sadrian lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, " 1387253314Sadrian "p_sync= 0x%x, p_collecting= 0x%x\n", __func__, 1388253314Sadrian lp->lp_mux_state, selected, p_sync, p_collecting); 1389168561Sthompsa 1390168561Sthompsare_eval: 1391168561Sthompsa la = lp->lp_aggregator; 1392168561Sthompsa KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL, 1393168561Sthompsa ("MUX not detached")); 1394168561Sthompsa new_state = lp->lp_mux_state; 1395168561Sthompsa switch (lp->lp_mux_state) { 1396168561Sthompsa case LACP_MUX_DETACHED: 1397168561Sthompsa if (selected != LACP_UNSELECTED) { 1398168561Sthompsa new_state = LACP_MUX_WAITING; 1399168561Sthompsa } 1400168561Sthompsa break; 1401168561Sthompsa case LACP_MUX_WAITING: 1402168561Sthompsa KASSERT(la->la_pending > 0 || 1403168561Sthompsa !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE), 1404168561Sthompsa ("timer_wait_while still active")); 1405168561Sthompsa if (selected == LACP_SELECTED && la->la_pending == 0) { 1406168561Sthompsa new_state = LACP_MUX_ATTACHED; 1407168561Sthompsa } else if (selected == LACP_UNSELECTED) { 1408168561Sthompsa new_state = LACP_MUX_DETACHED; 1409168561Sthompsa } 1410168561Sthompsa break; 1411168561Sthompsa case LACP_MUX_ATTACHED: 1412168561Sthompsa if (selected == LACP_SELECTED && p_sync) { 1413168561Sthompsa new_state = LACP_MUX_COLLECTING; 1414168561Sthompsa } else if (selected != LACP_SELECTED) { 1415168561Sthompsa new_state = LACP_MUX_DETACHED; 1416168561Sthompsa } 1417168561Sthompsa break; 1418168561Sthompsa case LACP_MUX_COLLECTING: 1419168561Sthompsa if (selected == LACP_SELECTED && p_sync && p_collecting) { 1420168561Sthompsa new_state = LACP_MUX_DISTRIBUTING; 1421168561Sthompsa } else if (selected != LACP_SELECTED || !p_sync) { 1422168561Sthompsa new_state = LACP_MUX_ATTACHED; 1423168561Sthompsa } 1424168561Sthompsa break; 1425168561Sthompsa case LACP_MUX_DISTRIBUTING: 1426168561Sthompsa if (selected != LACP_SELECTED || !p_sync || !p_collecting) { 1427168561Sthompsa new_state = LACP_MUX_COLLECTING; 1428253655Sadrian lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n"); 1429253314Sadrian sc->sc_flapping++; 1430168561Sthompsa } 1431168561Sthompsa break; 1432168561Sthompsa default: 1433168561Sthompsa panic("%s: unknown state", __func__); 1434168561Sthompsa } 1435168561Sthompsa 1436168561Sthompsa if (lp->lp_mux_state == new_state) { 1437168561Sthompsa return; 1438168561Sthompsa } 1439168561Sthompsa 1440168561Sthompsa lacp_set_mux(lp, new_state); 1441168561Sthompsa goto re_eval; 1442168561Sthompsa} 1443168561Sthompsa 1444168561Sthompsastatic void 1445168561Sthompsalacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state) 1446168561Sthompsa{ 1447168561Sthompsa struct lacp_aggregator *la = lp->lp_aggregator; 1448168561Sthompsa 1449168561Sthompsa if (lp->lp_mux_state == new_state) { 1450168561Sthompsa return; 1451168561Sthompsa } 1452168561Sthompsa 1453168561Sthompsa switch (new_state) { 1454168561Sthompsa case LACP_MUX_DETACHED: 1455168561Sthompsa lp->lp_state &= ~LACP_STATE_SYNC; 1456168561Sthompsa lacp_disable_distributing(lp); 1457168561Sthompsa lacp_disable_collecting(lp); 1458168561Sthompsa lacp_sm_assert_ntt(lp); 1459168561Sthompsa /* cancel timer */ 1460168561Sthompsa if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) { 1461168561Sthompsa KASSERT(la->la_pending > 0, 1462168561Sthompsa ("timer_wait_while not active")); 1463168561Sthompsa la->la_pending--; 1464168561Sthompsa } 1465168561Sthompsa LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE); 1466168561Sthompsa lacp_unselect(lp); 1467168561Sthompsa break; 1468168561Sthompsa case LACP_MUX_WAITING: 1469168561Sthompsa LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE, 1470168561Sthompsa LACP_AGGREGATE_WAIT_TIME); 1471168561Sthompsa la->la_pending++; 1472168561Sthompsa break; 1473168561Sthompsa case LACP_MUX_ATTACHED: 1474168561Sthompsa lp->lp_state |= LACP_STATE_SYNC; 1475168561Sthompsa lacp_disable_collecting(lp); 1476168561Sthompsa lacp_sm_assert_ntt(lp); 1477168561Sthompsa break; 1478168561Sthompsa case LACP_MUX_COLLECTING: 1479168561Sthompsa lacp_enable_collecting(lp); 1480168561Sthompsa lacp_disable_distributing(lp); 1481168561Sthompsa lacp_sm_assert_ntt(lp); 1482168561Sthompsa break; 1483168561Sthompsa case LACP_MUX_DISTRIBUTING: 1484168561Sthompsa lacp_enable_distributing(lp); 1485168561Sthompsa break; 1486168561Sthompsa default: 1487168561Sthompsa panic("%s: unknown state", __func__); 1488168561Sthompsa } 1489168561Sthompsa 1490168561Sthompsa LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state)); 1491168561Sthompsa 1492168561Sthompsa lp->lp_mux_state = new_state; 1493168561Sthompsa} 1494168561Sthompsa 1495168561Sthompsastatic void 1496168561Sthompsalacp_sm_mux_timer(struct lacp_port *lp) 1497168561Sthompsa{ 1498168561Sthompsa struct lacp_aggregator *la = lp->lp_aggregator; 1499168561Sthompsa char buf[LACP_LAGIDSTR_MAX+1]; 1500168561Sthompsa 1501168561Sthompsa KASSERT(la->la_pending > 0, ("no pending event")); 1502168561Sthompsa 1503168561Sthompsa LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__, 1504168561Sthompsa lacp_format_lagid(&la->la_actor, &la->la_partner, 1505168561Sthompsa buf, sizeof(buf)), 1506168561Sthompsa la->la_pending, la->la_pending - 1)); 1507168561Sthompsa 1508168561Sthompsa la->la_pending--; 1509168561Sthompsa} 1510168561Sthompsa 1511168561Sthompsa/* periodic transmit machine */ 1512168561Sthompsa 1513168561Sthompsastatic void 1514168561Sthompsalacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate) 1515168561Sthompsa{ 1516168561Sthompsa if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state, 1517168561Sthompsa LACP_STATE_TIMEOUT)) { 1518168561Sthompsa return; 1519168561Sthompsa } 1520168561Sthompsa 1521168561Sthompsa LACP_DPRINTF((lp, "partner timeout changed\n")); 1522168561Sthompsa 1523168561Sthompsa /* 1524168561Sthompsa * FAST_PERIODIC -> SLOW_PERIODIC 1525168561Sthompsa * or 1526168561Sthompsa * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC 1527168561Sthompsa * 1528168561Sthompsa * let lacp_sm_ptx_tx_schedule to update timeout. 1529168561Sthompsa */ 1530168561Sthompsa 1531168561Sthompsa LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC); 1532168561Sthompsa 1533168561Sthompsa /* 1534168561Sthompsa * if timeout has been shortened, assert NTT. 1535168561Sthompsa */ 1536168561Sthompsa 1537168561Sthompsa if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) { 1538168561Sthompsa lacp_sm_assert_ntt(lp); 1539168561Sthompsa } 1540168561Sthompsa} 1541168561Sthompsa 1542168561Sthompsastatic void 1543168561Sthompsalacp_sm_ptx_tx_schedule(struct lacp_port *lp) 1544168561Sthompsa{ 1545168561Sthompsa int timeout; 1546168561Sthompsa 1547168561Sthompsa if (!(lp->lp_state & LACP_STATE_ACTIVITY) && 1548168561Sthompsa !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) { 1549168561Sthompsa 1550168561Sthompsa /* 1551168561Sthompsa * NO_PERIODIC 1552168561Sthompsa */ 1553168561Sthompsa 1554168561Sthompsa LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC); 1555168561Sthompsa return; 1556168561Sthompsa } 1557168561Sthompsa 1558168561Sthompsa if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) { 1559168561Sthompsa return; 1560168561Sthompsa } 1561168561Sthompsa 1562168561Sthompsa timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ? 1563168561Sthompsa LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME; 1564168561Sthompsa 1565168561Sthompsa LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout); 1566168561Sthompsa} 1567168561Sthompsa 1568168561Sthompsastatic void 1569168561Sthompsalacp_sm_ptx_timer(struct lacp_port *lp) 1570168561Sthompsa{ 1571168561Sthompsa lacp_sm_assert_ntt(lp); 1572168561Sthompsa} 1573168561Sthompsa 1574168561Sthompsastatic void 1575168561Sthompsalacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du) 1576168561Sthompsa{ 1577168561Sthompsa int timeout; 1578168561Sthompsa 1579168561Sthompsa /* 1580168561Sthompsa * check LACP_DISABLED first 1581168561Sthompsa */ 1582168561Sthompsa 1583168561Sthompsa if (!(lp->lp_state & LACP_STATE_AGGREGATION)) { 1584168561Sthompsa return; 1585168561Sthompsa } 1586168561Sthompsa 1587168561Sthompsa /* 1588168561Sthompsa * check loopback condition. 1589168561Sthompsa */ 1590168561Sthompsa 1591168561Sthompsa if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid, 1592168561Sthompsa &lp->lp_actor.lip_systemid)) { 1593168561Sthompsa return; 1594168561Sthompsa } 1595168561Sthompsa 1596168561Sthompsa /* 1597168561Sthompsa * EXPIRED, DEFAULTED, CURRENT -> CURRENT 1598168561Sthompsa */ 1599168561Sthompsa 1600168561Sthompsa lacp_sm_rx_update_selected(lp, du); 1601168561Sthompsa lacp_sm_rx_update_ntt(lp, du); 1602168561Sthompsa lacp_sm_rx_record_pdu(lp, du); 1603168561Sthompsa 1604168561Sthompsa timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ? 1605168561Sthompsa LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME; 1606168561Sthompsa LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout); 1607168561Sthompsa 1608168561Sthompsa lp->lp_state &= ~LACP_STATE_EXPIRED; 1609168561Sthompsa 1610168561Sthompsa /* 1611168561Sthompsa * kick transmit machine without waiting the next tick. 1612168561Sthompsa */ 1613168561Sthompsa 1614168561Sthompsa lacp_sm_tx(lp); 1615168561Sthompsa} 1616168561Sthompsa 1617168561Sthompsastatic void 1618168561Sthompsalacp_sm_rx_set_expired(struct lacp_port *lp) 1619168561Sthompsa{ 1620168561Sthompsa lp->lp_partner.lip_state &= ~LACP_STATE_SYNC; 1621168561Sthompsa lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT; 1622168561Sthompsa LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME); 1623168561Sthompsa lp->lp_state |= LACP_STATE_EXPIRED; 1624168561Sthompsa} 1625168561Sthompsa 1626168561Sthompsastatic void 1627168561Sthompsalacp_sm_rx_timer(struct lacp_port *lp) 1628168561Sthompsa{ 1629168561Sthompsa if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) { 1630168561Sthompsa /* CURRENT -> EXPIRED */ 1631168561Sthompsa LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__)); 1632168561Sthompsa lacp_sm_rx_set_expired(lp); 1633168561Sthompsa } else { 1634168561Sthompsa /* EXPIRED -> DEFAULTED */ 1635168561Sthompsa LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__)); 1636168561Sthompsa lacp_sm_rx_update_default_selected(lp); 1637168561Sthompsa lacp_sm_rx_record_default(lp); 1638168561Sthompsa lp->lp_state &= ~LACP_STATE_EXPIRED; 1639168561Sthompsa } 1640168561Sthompsa} 1641168561Sthompsa 1642168561Sthompsastatic void 1643168561Sthompsalacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du) 1644168561Sthompsa{ 1645168561Sthompsa boolean_t active; 1646168561Sthompsa uint8_t oldpstate; 1647168561Sthompsa char buf[LACP_STATESTR_MAX+1]; 1648168561Sthompsa 1649236062Sthompsa LACP_TRACE(lp); 1650168561Sthompsa 1651168561Sthompsa oldpstate = lp->lp_partner.lip_state; 1652168561Sthompsa 1653168561Sthompsa active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY) 1654168561Sthompsa || ((lp->lp_state & LACP_STATE_ACTIVITY) && 1655168561Sthompsa (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY)); 1656168561Sthompsa 1657168561Sthompsa lp->lp_partner = du->ldu_actor; 1658168561Sthompsa if (active && 1659168561Sthompsa ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state, 1660168561Sthompsa LACP_STATE_AGGREGATION) && 1661168561Sthompsa !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner)) 1662168561Sthompsa || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) { 1663168561Sthompsa /* XXX nothing? */ 1664168561Sthompsa } else { 1665168561Sthompsa lp->lp_partner.lip_state &= ~LACP_STATE_SYNC; 1666168561Sthompsa } 1667168561Sthompsa 1668168561Sthompsa lp->lp_state &= ~LACP_STATE_DEFAULTED; 1669168561Sthompsa 1670168561Sthompsa if (oldpstate != lp->lp_partner.lip_state) { 1671168561Sthompsa LACP_DPRINTF((lp, "old pstate %s\n", 1672168561Sthompsa lacp_format_state(oldpstate, buf, sizeof(buf)))); 1673168561Sthompsa LACP_DPRINTF((lp, "new pstate %s\n", 1674168561Sthompsa lacp_format_state(lp->lp_partner.lip_state, buf, 1675168561Sthompsa sizeof(buf)))); 1676168561Sthompsa } 1677168561Sthompsa 1678253314Sadrian /* XXX Hack, still need to implement 5.4.9 para 2,3,4 */ 1679253687Sadrian if (lp->lp_lsc->lsc_strict_mode) 1680253314Sadrian lp->lp_partner.lip_state |= LACP_STATE_SYNC; 1681253314Sadrian 1682168561Sthompsa lacp_sm_ptx_update_timeout(lp, oldpstate); 1683168561Sthompsa} 1684168561Sthompsa 1685168561Sthompsastatic void 1686168561Sthompsalacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du) 1687168561Sthompsa{ 1688168561Sthompsa 1689236062Sthompsa LACP_TRACE(lp); 1690236062Sthompsa 1691168561Sthompsa if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) || 1692168561Sthompsa !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state, 1693168561Sthompsa LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) { 1694168561Sthompsa LACP_DPRINTF((lp, "%s: assert ntt\n", __func__)); 1695168561Sthompsa lacp_sm_assert_ntt(lp); 1696168561Sthompsa } 1697168561Sthompsa} 1698168561Sthompsa 1699168561Sthompsastatic void 1700168561Sthompsalacp_sm_rx_record_default(struct lacp_port *lp) 1701168561Sthompsa{ 1702168561Sthompsa uint8_t oldpstate; 1703168561Sthompsa 1704236062Sthompsa LACP_TRACE(lp); 1705168561Sthompsa 1706168561Sthompsa oldpstate = lp->lp_partner.lip_state; 1707253687Sadrian if (lp->lp_lsc->lsc_strict_mode) 1708253314Sadrian lp->lp_partner = lacp_partner_admin_strict; 1709253314Sadrian else 1710290736Ssmh lp->lp_partner = lacp_partner_admin_optimistic; 1711168561Sthompsa lp->lp_state |= LACP_STATE_DEFAULTED; 1712168561Sthompsa lacp_sm_ptx_update_timeout(lp, oldpstate); 1713168561Sthompsa} 1714168561Sthompsa 1715168561Sthompsastatic void 1716168561Sthompsalacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp, 1717168561Sthompsa const struct lacp_peerinfo *info) 1718168561Sthompsa{ 1719168561Sthompsa 1720236062Sthompsa LACP_TRACE(lp); 1721236062Sthompsa 1722168561Sthompsa if (lacp_compare_peerinfo(&lp->lp_partner, info) || 1723168561Sthompsa !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state, 1724168561Sthompsa LACP_STATE_AGGREGATION)) { 1725168561Sthompsa lp->lp_selected = LACP_UNSELECTED; 1726168561Sthompsa /* mux machine will clean up lp->lp_aggregator */ 1727168561Sthompsa } 1728168561Sthompsa} 1729168561Sthompsa 1730168561Sthompsastatic void 1731168561Sthompsalacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du) 1732168561Sthompsa{ 1733168561Sthompsa 1734236062Sthompsa LACP_TRACE(lp); 1735236062Sthompsa 1736168561Sthompsa lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor); 1737168561Sthompsa} 1738168561Sthompsa 1739168561Sthompsastatic void 1740168561Sthompsalacp_sm_rx_update_default_selected(struct lacp_port *lp) 1741168561Sthompsa{ 1742168561Sthompsa 1743236062Sthompsa LACP_TRACE(lp); 1744236062Sthompsa 1745253687Sadrian if (lp->lp_lsc->lsc_strict_mode) 1746253314Sadrian lacp_sm_rx_update_selected_from_peerinfo(lp, 1747253314Sadrian &lacp_partner_admin_strict); 1748253314Sadrian else 1749253314Sadrian lacp_sm_rx_update_selected_from_peerinfo(lp, 1750253314Sadrian &lacp_partner_admin_optimistic); 1751168561Sthompsa} 1752168561Sthompsa 1753168561Sthompsa/* transmit machine */ 1754168561Sthompsa 1755168561Sthompsastatic void 1756168561Sthompsalacp_sm_tx(struct lacp_port *lp) 1757168561Sthompsa{ 1758253314Sadrian int error = 0; 1759168561Sthompsa 1760168561Sthompsa if (!(lp->lp_state & LACP_STATE_AGGREGATION) 1761168561Sthompsa#if 1 1762168561Sthompsa || (!(lp->lp_state & LACP_STATE_ACTIVITY) 1763168561Sthompsa && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) 1764168561Sthompsa#endif 1765168561Sthompsa ) { 1766168561Sthompsa lp->lp_flags &= ~LACP_PORT_NTT; 1767168561Sthompsa } 1768168561Sthompsa 1769168561Sthompsa if (!(lp->lp_flags & LACP_PORT_NTT)) { 1770168561Sthompsa return; 1771168561Sthompsa } 1772168561Sthompsa 1773168561Sthompsa /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */ 1774168561Sthompsa if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent, 1775168561Sthompsa (3 / LACP_FAST_PERIODIC_TIME)) == 0) { 1776168561Sthompsa LACP_DPRINTF((lp, "rate limited pdu\n")); 1777168561Sthompsa return; 1778168561Sthompsa } 1779168561Sthompsa 1780253687Sadrian if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) { 1781253314Sadrian error = lacp_xmit_lacpdu(lp); 1782253687Sadrian } else { 1783253314Sadrian LACP_TPRINTF((lp, "Dropping TX PDU\n")); 1784253687Sadrian } 1785168561Sthompsa 1786168561Sthompsa if (error == 0) { 1787168561Sthompsa lp->lp_flags &= ~LACP_PORT_NTT; 1788168561Sthompsa } else { 1789168561Sthompsa LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n", 1790168561Sthompsa error)); 1791168561Sthompsa } 1792168561Sthompsa} 1793168561Sthompsa 1794168561Sthompsastatic void 1795168561Sthompsalacp_sm_assert_ntt(struct lacp_port *lp) 1796168561Sthompsa{ 1797168561Sthompsa 1798168561Sthompsa lp->lp_flags |= LACP_PORT_NTT; 1799168561Sthompsa} 1800168561Sthompsa 1801168561Sthompsastatic void 1802168561Sthompsalacp_run_timers(struct lacp_port *lp) 1803168561Sthompsa{ 1804168561Sthompsa int i; 1805168561Sthompsa 1806168561Sthompsa for (i = 0; i < LACP_NTIMER; i++) { 1807168561Sthompsa KASSERT(lp->lp_timer[i] >= 0, 1808168561Sthompsa ("invalid timer value %d", lp->lp_timer[i])); 1809168561Sthompsa if (lp->lp_timer[i] == 0) { 1810168561Sthompsa continue; 1811168561Sthompsa } else if (--lp->lp_timer[i] <= 0) { 1812168561Sthompsa if (lacp_timer_funcs[i]) { 1813168561Sthompsa (*lacp_timer_funcs[i])(lp); 1814168561Sthompsa } 1815168561Sthompsa } 1816168561Sthompsa } 1817168561Sthompsa} 1818168561Sthompsa 1819168561Sthompsaint 1820177274Sthompsalacp_marker_input(struct lacp_port *lp, struct mbuf *m) 1821168561Sthompsa{ 1822177274Sthompsa struct lacp_softc *lsc = lp->lp_lsc; 1823177274Sthompsa struct lagg_port *lgp = lp->lp_lagg; 1824169739Sthompsa struct lacp_port *lp2; 1825168561Sthompsa struct markerdu *mdu; 1826168561Sthompsa int error = 0; 1827169739Sthompsa int pending = 0; 1828168561Sthompsa 1829168561Sthompsa if (m->m_pkthdr.len != sizeof(*mdu)) { 1830168561Sthompsa goto bad; 1831168561Sthompsa } 1832168561Sthompsa 1833168561Sthompsa if ((m->m_flags & M_MCAST) == 0) { 1834168561Sthompsa goto bad; 1835168561Sthompsa } 1836168561Sthompsa 1837168561Sthompsa if (m->m_len < sizeof(*mdu)) { 1838168561Sthompsa m = m_pullup(m, sizeof(*mdu)); 1839168561Sthompsa if (m == NULL) { 1840168561Sthompsa return (ENOMEM); 1841168561Sthompsa } 1842168561Sthompsa } 1843168561Sthompsa 1844168561Sthompsa mdu = mtod(m, struct markerdu *); 1845168561Sthompsa 1846168561Sthompsa if (memcmp(&mdu->mdu_eh.ether_dhost, 1847168561Sthompsa ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) { 1848168561Sthompsa goto bad; 1849168561Sthompsa } 1850168561Sthompsa 1851168561Sthompsa if (mdu->mdu_sph.sph_version != 1) { 1852168561Sthompsa goto bad; 1853168561Sthompsa } 1854168561Sthompsa 1855168561Sthompsa switch (mdu->mdu_tlv.tlv_type) { 1856168561Sthompsa case MARKER_TYPE_INFO: 1857168561Sthompsa if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv, 1858168561Sthompsa marker_info_tlv_template, TRUE)) { 1859168561Sthompsa goto bad; 1860168561Sthompsa } 1861168561Sthompsa mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE; 1862168561Sthompsa memcpy(&mdu->mdu_eh.ether_dhost, 1863168561Sthompsa ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN); 1864168561Sthompsa memcpy(&mdu->mdu_eh.ether_shost, 1865168793Sthompsa lgp->lp_lladdr, ETHER_ADDR_LEN); 1866168793Sthompsa error = lagg_enqueue(lp->lp_ifp, m); 1867168561Sthompsa break; 1868168561Sthompsa 1869168561Sthompsa case MARKER_TYPE_RESPONSE: 1870168561Sthompsa if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv, 1871168561Sthompsa marker_response_tlv_template, TRUE)) { 1872168561Sthompsa goto bad; 1873168561Sthompsa } 1874169739Sthompsa LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n", 1875169739Sthompsa ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, 1876169739Sthompsa ":", ntohl(mdu->mdu_info.mi_rq_xid))); 1877169739Sthompsa 1878169739Sthompsa /* Verify that it is the last marker we sent out */ 1879169739Sthompsa if (memcmp(&mdu->mdu_info, &lp->lp_marker, 1880169739Sthompsa sizeof(struct lacp_markerinfo))) 1881169739Sthompsa goto bad; 1882169739Sthompsa 1883177274Sthompsa LACP_LOCK(lsc); 1884169739Sthompsa lp->lp_flags &= ~LACP_PORT_MARK; 1885169739Sthompsa 1886169739Sthompsa if (lsc->lsc_suppress_distributing) { 1887169739Sthompsa /* Check if any ports are waiting for a response */ 1888169739Sthompsa LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) { 1889169739Sthompsa if (lp2->lp_flags & LACP_PORT_MARK) { 1890169739Sthompsa pending = 1; 1891169739Sthompsa break; 1892169739Sthompsa } 1893169739Sthompsa } 1894169739Sthompsa 1895169739Sthompsa if (pending == 0) { 1896169739Sthompsa /* All interface queues are clear */ 1897169739Sthompsa LACP_DPRINTF((NULL, "queue flush complete\n")); 1898169739Sthompsa lsc->lsc_suppress_distributing = FALSE; 1899169739Sthompsa } 1900169739Sthompsa } 1901177274Sthompsa LACP_UNLOCK(lsc); 1902169739Sthompsa m_freem(m); 1903169739Sthompsa break; 1904169739Sthompsa 1905168561Sthompsa default: 1906168561Sthompsa goto bad; 1907168561Sthompsa } 1908168561Sthompsa 1909168561Sthompsa return (error); 1910168561Sthompsa 1911168561Sthompsabad: 1912169739Sthompsa LACP_DPRINTF((lp, "bad marker frame\n")); 1913168561Sthompsa m_freem(m); 1914168561Sthompsa return (EINVAL); 1915168561Sthompsa} 1916168561Sthompsa 1917168561Sthompsastatic int 1918168561Sthompsatlv_check(const void *p, size_t size, const struct tlvhdr *tlv, 1919168561Sthompsa const struct tlv_template *tmpl, boolean_t check_type) 1920168561Sthompsa{ 1921168561Sthompsa while (/* CONSTCOND */ 1) { 1922168561Sthompsa if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) { 1923168561Sthompsa return (EINVAL); 1924168561Sthompsa } 1925168561Sthompsa if ((check_type && tlv->tlv_type != tmpl->tmpl_type) || 1926168561Sthompsa tlv->tlv_length != tmpl->tmpl_length) { 1927168561Sthompsa return (EINVAL); 1928168561Sthompsa } 1929168561Sthompsa if (tmpl->tmpl_type == 0) { 1930168561Sthompsa break; 1931168561Sthompsa } 1932168561Sthompsa tlv = (const struct tlvhdr *) 1933168561Sthompsa ((const char *)tlv + tlv->tlv_length); 1934168561Sthompsa tmpl++; 1935168561Sthompsa } 1936168561Sthompsa 1937168561Sthompsa return (0); 1938168561Sthompsa} 1939168561Sthompsa 1940236062Sthompsa/* Debugging */ 1941168561Sthompsaconst char * 1942168561Sthompsalacp_format_mac(const uint8_t *mac, char *buf, size_t buflen) 1943168561Sthompsa{ 1944168561Sthompsa snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X", 1945168561Sthompsa (int)mac[0], 1946168561Sthompsa (int)mac[1], 1947168561Sthompsa (int)mac[2], 1948168561Sthompsa (int)mac[3], 1949168561Sthompsa (int)mac[4], 1950168561Sthompsa (int)mac[5]); 1951168561Sthompsa 1952168561Sthompsa return (buf); 1953168561Sthompsa} 1954168561Sthompsa 1955168561Sthompsaconst char * 1956168561Sthompsalacp_format_systemid(const struct lacp_systemid *sysid, 1957168561Sthompsa char *buf, size_t buflen) 1958168561Sthompsa{ 1959168561Sthompsa char macbuf[LACP_MACSTR_MAX+1]; 1960168561Sthompsa 1961168561Sthompsa snprintf(buf, buflen, "%04X,%s", 1962168561Sthompsa ntohs(sysid->lsi_prio), 1963168561Sthompsa lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf))); 1964168561Sthompsa 1965168561Sthompsa return (buf); 1966168561Sthompsa} 1967168561Sthompsa 1968168561Sthompsaconst char * 1969168561Sthompsalacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen) 1970168561Sthompsa{ 1971168561Sthompsa snprintf(buf, buflen, "%04X,%04X", 1972168561Sthompsa ntohs(portid->lpi_prio), 1973168561Sthompsa ntohs(portid->lpi_portno)); 1974168561Sthompsa 1975168561Sthompsa return (buf); 1976168561Sthompsa} 1977168561Sthompsa 1978168561Sthompsaconst char * 1979168561Sthompsalacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen) 1980168561Sthompsa{ 1981168561Sthompsa char sysid[LACP_SYSTEMIDSTR_MAX+1]; 1982168561Sthompsa char portid[LACP_PORTIDSTR_MAX+1]; 1983168561Sthompsa 1984168561Sthompsa snprintf(buf, buflen, "(%s,%04X,%s)", 1985168561Sthompsa lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)), 1986168561Sthompsa ntohs(peer->lip_key), 1987168561Sthompsa lacp_format_portid(&peer->lip_portid, portid, sizeof(portid))); 1988168561Sthompsa 1989168561Sthompsa return (buf); 1990168561Sthompsa} 1991168561Sthompsa 1992168561Sthompsaconst char * 1993168561Sthompsalacp_format_lagid(const struct lacp_peerinfo *a, 1994168561Sthompsa const struct lacp_peerinfo *b, char *buf, size_t buflen) 1995168561Sthompsa{ 1996168561Sthompsa char astr[LACP_PARTNERSTR_MAX+1]; 1997168561Sthompsa char bstr[LACP_PARTNERSTR_MAX+1]; 1998168561Sthompsa 1999168561Sthompsa#if 0 2000168561Sthompsa /* 2001168561Sthompsa * there's a convention to display small numbered peer 2002168561Sthompsa * in the left. 2003168561Sthompsa */ 2004168561Sthompsa 2005168561Sthompsa if (lacp_compare_peerinfo(a, b) > 0) { 2006168561Sthompsa const struct lacp_peerinfo *t; 2007168561Sthompsa 2008168561Sthompsa t = a; 2009168561Sthompsa a = b; 2010168561Sthompsa b = t; 2011168561Sthompsa } 2012168561Sthompsa#endif 2013168561Sthompsa 2014168561Sthompsa snprintf(buf, buflen, "[%s,%s]", 2015168561Sthompsa lacp_format_partner(a, astr, sizeof(astr)), 2016168561Sthompsa lacp_format_partner(b, bstr, sizeof(bstr))); 2017168561Sthompsa 2018168561Sthompsa return (buf); 2019168561Sthompsa} 2020168561Sthompsa 2021168561Sthompsaconst char * 2022168561Sthompsalacp_format_lagid_aggregator(const struct lacp_aggregator *la, 2023168561Sthompsa char *buf, size_t buflen) 2024168561Sthompsa{ 2025168561Sthompsa if (la == NULL) { 2026168561Sthompsa return ("(none)"); 2027168561Sthompsa } 2028168561Sthompsa 2029168561Sthompsa return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen)); 2030168561Sthompsa} 2031168561Sthompsa 2032168561Sthompsaconst char * 2033168561Sthompsalacp_format_state(uint8_t state, char *buf, size_t buflen) 2034168561Sthompsa{ 2035168561Sthompsa snprintf(buf, buflen, "%b", state, LACP_STATE_BITS); 2036168561Sthompsa return (buf); 2037168561Sthompsa} 2038168561Sthompsa 2039168561Sthompsastatic void 2040168561Sthompsalacp_dump_lacpdu(const struct lacpdu *du) 2041168561Sthompsa{ 2042168561Sthompsa char buf[LACP_PARTNERSTR_MAX+1]; 2043168561Sthompsa char buf2[LACP_STATESTR_MAX+1]; 2044168561Sthompsa 2045168561Sthompsa printf("actor=%s\n", 2046168561Sthompsa lacp_format_partner(&du->ldu_actor, buf, sizeof(buf))); 2047168561Sthompsa printf("actor.state=%s\n", 2048168561Sthompsa lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2))); 2049168561Sthompsa printf("partner=%s\n", 2050168561Sthompsa lacp_format_partner(&du->ldu_partner, buf, sizeof(buf))); 2051168561Sthompsa printf("partner.state=%s\n", 2052168561Sthompsa lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2))); 2053168561Sthompsa 2054168561Sthompsa printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay)); 2055168561Sthompsa} 2056168561Sthompsa 2057168561Sthompsastatic void 2058168561Sthompsalacp_dprintf(const struct lacp_port *lp, const char *fmt, ...) 2059168561Sthompsa{ 2060168561Sthompsa va_list va; 2061168561Sthompsa 2062168561Sthompsa if (lp) { 2063168561Sthompsa printf("%s: ", lp->lp_ifp->if_xname); 2064168561Sthompsa } 2065168561Sthompsa 2066168561Sthompsa va_start(va, fmt); 2067168561Sthompsa vprintf(fmt, va); 2068168561Sthompsa va_end(va); 2069168561Sthompsa} 2070