1 /* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $ */ 2 3 /*- 4 * Copyright (c)2005 YAMAMOTO Takashi, 5 * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/callout.h> 35 #include <sys/eventhandler.h> 36 #include <sys/mbuf.h> 37 #include <sys/systm.h> 38 #include <sys/fnv_hash.h> 39 #include <sys/malloc.h> 40 #include <sys/kernel.h> /* hz */ 41 #include <sys/socket.h> /* for net/if.h */ 42 #include <sys/sockio.h> 43 #include <sys/sysctl.h> 44 #include <machine/stdarg.h> 45 #include <sys/lock.h> 46 #include <sys/rwlock.h> 47 #include <sys/taskqueue.h> 48 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/if_dl.h> 52 #include <net/ethernet.h> 53 #include <net/if_media.h> 54 #include <net/if_types.h> 55 56 #include <net/if_lagg.h> 57 #include <net/ieee8023ad_lacp.h> 58 59 /* 60 * actor system priority and port priority. 61 * XXX should be configurable. 62 */ 63 64 #define LACP_SYSTEM_PRIO 0x8000 65 #define LACP_PORT_PRIO 0x8000 66 67 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] = 68 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 }; 69 70 static const struct tlv_template lacp_info_tlv_template[] = { 71 { LACP_TYPE_ACTORINFO, 72 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) }, 73 { LACP_TYPE_PARTNERINFO, 74 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) }, 75 { LACP_TYPE_COLLECTORINFO, 76 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) }, 77 { 0, 0 }, 78 }; 79 80 static const struct tlv_template marker_info_tlv_template[] = { 81 { MARKER_TYPE_INFO, 82 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) }, 83 { 0, 0 }, 84 }; 85 86 static const struct tlv_template marker_response_tlv_template[] = { 87 { MARKER_TYPE_RESPONSE, 88 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) }, 89 { 0, 0 }, 90 }; 91 92 typedef void (*lacp_timer_func_t)(struct lacp_port *); 93 94 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *); 95 static void lacp_fill_markerinfo(struct lacp_port *, 96 struct lacp_markerinfo *); 97 98 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *); 99 static void lacp_suppress_distributing(struct lacp_softc *, 100 struct lacp_aggregator *); 101 static void lacp_transit_expire(void *); 102 static void lacp_update_portmap(struct lacp_softc *); 103 static void lacp_select_active_aggregator(struct lacp_softc *); 104 static uint16_t lacp_compose_key(struct lacp_port *); 105 static int tlv_check(const void *, size_t, const struct tlvhdr *, 106 const struct tlv_template *, boolean_t); 107 static void lacp_tick(void *); 108 109 static void lacp_fill_aggregator_id(struct lacp_aggregator *, 110 const struct lacp_port *); 111 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *, 112 const struct lacp_peerinfo *); 113 static int lacp_aggregator_is_compatible(const struct lacp_aggregator *, 114 const struct lacp_port *); 115 static int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *, 116 const struct lacp_peerinfo *); 117 118 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *, 119 struct lacp_port *); 120 static void lacp_aggregator_addref(struct lacp_softc *, 121 struct lacp_aggregator *); 122 static void lacp_aggregator_delref(struct lacp_softc *, 123 struct lacp_aggregator *); 124 125 /* receive machine */ 126 127 static int lacp_pdu_input(struct lacp_port *, struct mbuf *); 128 static int lacp_marker_input(struct lacp_port *, struct mbuf *); 129 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *); 130 static void lacp_sm_rx_timer(struct lacp_port *); 131 static void lacp_sm_rx_set_expired(struct lacp_port *); 132 static void lacp_sm_rx_update_ntt(struct lacp_port *, 133 const struct lacpdu *); 134 static void lacp_sm_rx_record_pdu(struct lacp_port *, 135 const struct lacpdu *); 136 static void lacp_sm_rx_update_selected(struct lacp_port *, 137 const struct lacpdu *); 138 static void lacp_sm_rx_record_default(struct lacp_port *); 139 static void lacp_sm_rx_update_default_selected(struct lacp_port *); 140 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *, 141 const struct lacp_peerinfo *); 142 143 /* mux machine */ 144 145 static void lacp_sm_mux(struct lacp_port *); 146 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state); 147 static void lacp_sm_mux_timer(struct lacp_port *); 148 149 /* periodic transmit machine */ 150 151 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t); 152 static void lacp_sm_ptx_tx_schedule(struct lacp_port *); 153 static void lacp_sm_ptx_timer(struct lacp_port *); 154 155 /* transmit machine */ 156 157 static void lacp_sm_tx(struct lacp_port *); 158 static void lacp_sm_assert_ntt(struct lacp_port *); 159 160 static void lacp_run_timers(struct lacp_port *); 161 static int lacp_compare_peerinfo(const struct lacp_peerinfo *, 162 const struct lacp_peerinfo *); 163 static int lacp_compare_systemid(const struct lacp_systemid *, 164 const struct lacp_systemid *); 165 static void lacp_port_enable(struct lacp_port *); 166 static void lacp_port_disable(struct lacp_port *); 167 static void lacp_select(struct lacp_port *); 168 static void lacp_unselect(struct lacp_port *); 169 static void lacp_disable_collecting(struct lacp_port *); 170 static void lacp_enable_collecting(struct lacp_port *); 171 static void lacp_disable_distributing(struct lacp_port *); 172 static void lacp_enable_distributing(struct lacp_port *); 173 static int lacp_xmit_lacpdu(struct lacp_port *); 174 static int lacp_xmit_marker(struct lacp_port *); 175 176 /* Debugging */ 177 178 static void lacp_dump_lacpdu(const struct lacpdu *); 179 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *, 180 size_t); 181 static const char *lacp_format_lagid(const struct lacp_peerinfo *, 182 const struct lacp_peerinfo *, char *, size_t); 183 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *, 184 char *, size_t); 185 static const char *lacp_format_state(uint8_t, char *, size_t); 186 static const char *lacp_format_mac(const uint8_t *, char *, size_t); 187 static const char *lacp_format_systemid(const struct lacp_systemid *, char *, 188 size_t); 189 static const char *lacp_format_portid(const struct lacp_portid *, char *, 190 size_t); 191 static void lacp_dprintf(const struct lacp_port *, const char *, ...) 192 __attribute__((__format__(__printf__, 2, 3))); 193 194 static VNET_DEFINE(int, lacp_debug); 195 #define V_lacp_debug VNET(lacp_debug) 196 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD, 0, "ieee802.3ad"); 197 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET, 198 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)"); 199 200 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; } 201 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); } 202 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; } 203 204 /* 205 * partner administration variables. 206 * XXX should be configurable. 207 */ 208 209 static const struct lacp_peerinfo lacp_partner_admin_optimistic = { 210 .lip_systemid = { .lsi_prio = 0xffff }, 211 .lip_portid = { .lpi_prio = 0xffff }, 212 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION | 213 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING, 214 }; 215 216 static const struct lacp_peerinfo lacp_partner_admin_strict = { 217 .lip_systemid = { .lsi_prio = 0xffff }, 218 .lip_portid = { .lpi_prio = 0xffff }, 219 .lip_state = 0, 220 }; 221 222 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = { 223 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer, 224 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer, 225 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer, 226 }; 227 228 struct mbuf * 229 lacp_input(struct lagg_port *lgp, struct mbuf *m) 230 { 231 struct lacp_port *lp = LACP_PORT(lgp); 232 uint8_t subtype; 233 234 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) { 235 m_freem(m); 236 return (NULL); 237 } 238 239 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype); 240 switch (subtype) { 241 case SLOWPROTOCOLS_SUBTYPE_LACP: 242 lacp_pdu_input(lp, m); 243 return (NULL); 244 245 case SLOWPROTOCOLS_SUBTYPE_MARKER: 246 lacp_marker_input(lp, m); 247 return (NULL); 248 } 249 250 /* Not a subtype we are interested in */ 251 return (m); 252 } 253 254 /* 255 * lacp_pdu_input: process lacpdu 256 */ 257 static int 258 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m) 259 { 260 struct lacp_softc *lsc = lp->lp_lsc; 261 struct lacpdu *du; 262 int error = 0; 263 264 if (m->m_pkthdr.len != sizeof(*du)) { 265 goto bad; 266 } 267 268 if ((m->m_flags & M_MCAST) == 0) { 269 goto bad; 270 } 271 272 if (m->m_len < sizeof(*du)) { 273 m = m_pullup(m, sizeof(*du)); 274 if (m == NULL) { 275 return (ENOMEM); 276 } 277 } 278 279 du = mtod(m, struct lacpdu *); 280 281 if (memcmp(&du->ldu_eh.ether_dhost, 282 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) { 283 goto bad; 284 } 285 286 /* 287 * ignore the version for compatibility with 288 * the future protocol revisions. 289 */ 290 #if 0 291 if (du->ldu_sph.sph_version != 1) { 292 goto bad; 293 } 294 #endif 295 296 /* 297 * ignore tlv types for compatibility with 298 * the future protocol revisions. 299 */ 300 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor, 301 lacp_info_tlv_template, FALSE)) { 302 goto bad; 303 } 304 305 if (V_lacp_debug > 0) { 306 lacp_dprintf(lp, "lacpdu receive\n"); 307 lacp_dump_lacpdu(du); 308 } 309 310 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) { 311 LACP_TPRINTF((lp, "Dropping RX PDU\n")); 312 goto bad; 313 } 314 315 LACP_LOCK(lsc); 316 lacp_sm_rx(lp, du); 317 LACP_UNLOCK(lsc); 318 319 m_freem(m); 320 return (error); 321 322 bad: 323 m_freem(m); 324 return (EINVAL); 325 } 326 327 static void 328 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info) 329 { 330 struct lagg_port *lgp = lp->lp_lagg; 331 struct lagg_softc *sc = lgp->lp_softc; 332 333 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO); 334 memcpy(&info->lip_systemid.lsi_mac, 335 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 336 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO); 337 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index); 338 info->lip_state = lp->lp_state; 339 } 340 341 static void 342 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info) 343 { 344 struct ifnet *ifp = lp->lp_ifp; 345 346 /* Fill in the port index and system id (encoded as the MAC) */ 347 info->mi_rq_port = htons(ifp->if_index); 348 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN); 349 info->mi_rq_xid = htonl(0); 350 } 351 352 static int 353 lacp_xmit_lacpdu(struct lacp_port *lp) 354 { 355 struct lagg_port *lgp = lp->lp_lagg; 356 struct mbuf *m; 357 struct lacpdu *du; 358 int error; 359 360 LACP_LOCK_ASSERT(lp->lp_lsc); 361 362 m = m_gethdr(M_NOWAIT, MT_DATA); 363 if (m == NULL) { 364 return (ENOMEM); 365 } 366 m->m_len = m->m_pkthdr.len = sizeof(*du); 367 368 du = mtod(m, struct lacpdu *); 369 memset(du, 0, sizeof(*du)); 370 371 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols, 372 ETHER_ADDR_LEN); 373 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN); 374 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW); 375 376 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP; 377 du->ldu_sph.sph_version = 1; 378 379 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor)); 380 du->ldu_actor = lp->lp_actor; 381 382 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO, 383 sizeof(du->ldu_partner)); 384 du->ldu_partner = lp->lp_partner; 385 386 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO, 387 sizeof(du->ldu_collector)); 388 du->ldu_collector.lci_maxdelay = 0; 389 390 if (V_lacp_debug > 0) { 391 lacp_dprintf(lp, "lacpdu transmit\n"); 392 lacp_dump_lacpdu(du); 393 } 394 395 m->m_flags |= M_MCAST; 396 397 /* 398 * XXX should use higher priority queue. 399 * otherwise network congestion can break aggregation. 400 */ 401 402 error = lagg_enqueue(lp->lp_ifp, m); 403 return (error); 404 } 405 406 static int 407 lacp_xmit_marker(struct lacp_port *lp) 408 { 409 struct lagg_port *lgp = lp->lp_lagg; 410 struct mbuf *m; 411 struct markerdu *mdu; 412 int error; 413 414 LACP_LOCK_ASSERT(lp->lp_lsc); 415 416 m = m_gethdr(M_NOWAIT, MT_DATA); 417 if (m == NULL) { 418 return (ENOMEM); 419 } 420 m->m_len = m->m_pkthdr.len = sizeof(*mdu); 421 422 mdu = mtod(m, struct markerdu *); 423 memset(mdu, 0, sizeof(*mdu)); 424 425 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols, 426 ETHER_ADDR_LEN); 427 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN); 428 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW); 429 430 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER; 431 mdu->mdu_sph.sph_version = 1; 432 433 /* Bump the transaction id and copy over the marker info */ 434 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1); 435 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info)); 436 mdu->mdu_info = lp->lp_marker; 437 438 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n", 439 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":", 440 ntohl(mdu->mdu_info.mi_rq_xid))); 441 442 m->m_flags |= M_MCAST; 443 error = lagg_enqueue(lp->lp_ifp, m); 444 return (error); 445 } 446 447 void 448 lacp_linkstate(struct lagg_port *lgp) 449 { 450 struct lacp_port *lp = LACP_PORT(lgp); 451 struct lacp_softc *lsc = lp->lp_lsc; 452 struct ifnet *ifp = lgp->lp_ifp; 453 struct ifmediareq ifmr; 454 int error = 0; 455 u_int media; 456 uint8_t old_state; 457 uint16_t old_key; 458 459 bzero((char *)&ifmr, sizeof(ifmr)); 460 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr); 461 if (error != 0) 462 return; 463 464 LACP_LOCK(lsc); 465 media = ifmr.ifm_active; 466 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, " 467 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER, 468 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP)); 469 old_state = lp->lp_state; 470 old_key = lp->lp_key; 471 472 lp->lp_media = media; 473 /* 474 * If the port is not an active full duplex Ethernet link then it can 475 * not be aggregated. 476 */ 477 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 || 478 ifp->if_link_state != LINK_STATE_UP) { 479 lacp_port_disable(lp); 480 } else { 481 lacp_port_enable(lp); 482 } 483 lp->lp_key = lacp_compose_key(lp); 484 485 if (old_state != lp->lp_state || old_key != lp->lp_key) { 486 LACP_DPRINTF((lp, "-> UNSELECTED\n")); 487 lp->lp_selected = LACP_UNSELECTED; 488 } 489 LACP_UNLOCK(lsc); 490 } 491 492 static void 493 lacp_tick(void *arg) 494 { 495 struct lacp_softc *lsc = arg; 496 struct lacp_port *lp; 497 498 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) { 499 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) 500 continue; 501 502 CURVNET_SET(lp->lp_ifp->if_vnet); 503 lacp_run_timers(lp); 504 505 lacp_select(lp); 506 lacp_sm_mux(lp); 507 lacp_sm_tx(lp); 508 lacp_sm_ptx_tx_schedule(lp); 509 CURVNET_RESTORE(); 510 } 511 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc); 512 } 513 514 int 515 lacp_port_create(struct lagg_port *lgp) 516 { 517 struct lagg_softc *sc = lgp->lp_softc; 518 struct lacp_softc *lsc = LACP_SOFTC(sc); 519 struct lacp_port *lp; 520 struct ifnet *ifp = lgp->lp_ifp; 521 struct sockaddr_dl sdl; 522 struct ifmultiaddr *rifma = NULL; 523 int error; 524 525 boolean_t active = TRUE; /* XXX should be configurable */ 526 boolean_t fast = FALSE; /* XXX should be configurable */ 527 528 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER); 529 sdl.sdl_alen = ETHER_ADDR_LEN; 530 531 bcopy(ðermulticastaddr_slowprotocols, 532 LLADDR(&sdl), ETHER_ADDR_LEN); 533 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma); 534 if (error) { 535 printf("%s: ADDMULTI failed on %s\n", __func__, 536 lgp->lp_ifp->if_xname); 537 return (error); 538 } 539 540 lp = malloc(sizeof(struct lacp_port), 541 M_DEVBUF, M_NOWAIT|M_ZERO); 542 if (lp == NULL) 543 return (ENOMEM); 544 545 LACP_LOCK(lsc); 546 lgp->lp_psc = lp; 547 lp->lp_ifp = ifp; 548 lp->lp_lagg = lgp; 549 lp->lp_lsc = lsc; 550 lp->lp_ifma = rifma; 551 552 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next); 553 554 lacp_fill_actorinfo(lp, &lp->lp_actor); 555 lacp_fill_markerinfo(lp, &lp->lp_marker); 556 lp->lp_state = 557 (active ? LACP_STATE_ACTIVITY : 0) | 558 (fast ? LACP_STATE_TIMEOUT : 0); 559 lp->lp_aggregator = NULL; 560 lacp_sm_rx_set_expired(lp); 561 LACP_UNLOCK(lsc); 562 lacp_linkstate(lgp); 563 564 return (0); 565 } 566 567 void 568 lacp_port_destroy(struct lagg_port *lgp) 569 { 570 struct lacp_port *lp = LACP_PORT(lgp); 571 struct lacp_softc *lsc = lp->lp_lsc; 572 int i; 573 574 LACP_LOCK(lsc); 575 for (i = 0; i < LACP_NTIMER; i++) { 576 LACP_TIMER_DISARM(lp, i); 577 } 578 579 lacp_disable_collecting(lp); 580 lacp_disable_distributing(lp); 581 lacp_unselect(lp); 582 583 LIST_REMOVE(lp, lp_next); 584 LACP_UNLOCK(lsc); 585 586 /* The address may have already been removed by if_purgemaddrs() */ 587 if (!lgp->lp_detaching) 588 if_delmulti_ifma(lp->lp_ifma); 589 590 free(lp, M_DEVBUF); 591 } 592 593 void 594 lacp_req(struct lagg_softc *sc, void *data) 595 { 596 struct lacp_opreq *req = (struct lacp_opreq *)data; 597 struct lacp_softc *lsc = LACP_SOFTC(sc); 598 struct lacp_aggregator *la; 599 600 bzero(req, sizeof(struct lacp_opreq)); 601 602 /* 603 * If the LACP softc is NULL, return with the opreq structure full of 604 * zeros. It is normal for the softc to be NULL while the lagg is 605 * being destroyed. 606 */ 607 if (NULL == lsc) 608 return; 609 610 la = lsc->lsc_active_aggregator; 611 LACP_LOCK(lsc); 612 if (la != NULL) { 613 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio); 614 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac, 615 ETHER_ADDR_LEN); 616 req->actor_key = ntohs(la->la_actor.lip_key); 617 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio); 618 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno); 619 req->actor_state = la->la_actor.lip_state; 620 621 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio); 622 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac, 623 ETHER_ADDR_LEN); 624 req->partner_key = ntohs(la->la_partner.lip_key); 625 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio); 626 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno); 627 req->partner_state = la->la_partner.lip_state; 628 } 629 LACP_UNLOCK(lsc); 630 } 631 632 void 633 lacp_portreq(struct lagg_port *lgp, void *data) 634 { 635 struct lacp_opreq *req = (struct lacp_opreq *)data; 636 struct lacp_port *lp = LACP_PORT(lgp); 637 struct lacp_softc *lsc = lp->lp_lsc; 638 639 LACP_LOCK(lsc); 640 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio); 641 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac, 642 ETHER_ADDR_LEN); 643 req->actor_key = ntohs(lp->lp_actor.lip_key); 644 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio); 645 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno); 646 req->actor_state = lp->lp_actor.lip_state; 647 648 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio); 649 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac, 650 ETHER_ADDR_LEN); 651 req->partner_key = ntohs(lp->lp_partner.lip_key); 652 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio); 653 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno); 654 req->partner_state = lp->lp_partner.lip_state; 655 LACP_UNLOCK(lsc); 656 } 657 658 static void 659 lacp_disable_collecting(struct lacp_port *lp) 660 { 661 LACP_DPRINTF((lp, "collecting disabled\n")); 662 lp->lp_state &= ~LACP_STATE_COLLECTING; 663 } 664 665 static void 666 lacp_enable_collecting(struct lacp_port *lp) 667 { 668 LACP_DPRINTF((lp, "collecting enabled\n")); 669 lp->lp_state |= LACP_STATE_COLLECTING; 670 } 671 672 static void 673 lacp_disable_distributing(struct lacp_port *lp) 674 { 675 struct lacp_aggregator *la = lp->lp_aggregator; 676 struct lacp_softc *lsc = lp->lp_lsc; 677 struct lagg_softc *sc = lsc->lsc_softc; 678 char buf[LACP_LAGIDSTR_MAX+1]; 679 680 LACP_LOCK_ASSERT(lsc); 681 682 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) { 683 return; 684 } 685 686 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports")); 687 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports)); 688 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid")); 689 690 LACP_DPRINTF((lp, "disable distributing on aggregator %s, " 691 "nports %d -> %d\n", 692 lacp_format_lagid_aggregator(la, buf, sizeof(buf)), 693 la->la_nports, la->la_nports - 1)); 694 695 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q); 696 la->la_nports--; 697 sc->sc_active = la->la_nports; 698 699 if (lsc->lsc_active_aggregator == la) { 700 lacp_suppress_distributing(lsc, la); 701 lacp_select_active_aggregator(lsc); 702 /* regenerate the port map, the active aggregator has changed */ 703 lacp_update_portmap(lsc); 704 } 705 706 lp->lp_state &= ~LACP_STATE_DISTRIBUTING; 707 } 708 709 static void 710 lacp_enable_distributing(struct lacp_port *lp) 711 { 712 struct lacp_aggregator *la = lp->lp_aggregator; 713 struct lacp_softc *lsc = lp->lp_lsc; 714 struct lagg_softc *sc = lsc->lsc_softc; 715 char buf[LACP_LAGIDSTR_MAX+1]; 716 717 LACP_LOCK_ASSERT(lsc); 718 719 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) { 720 return; 721 } 722 723 LACP_DPRINTF((lp, "enable distributing on aggregator %s, " 724 "nports %d -> %d\n", 725 lacp_format_lagid_aggregator(la, buf, sizeof(buf)), 726 la->la_nports, la->la_nports + 1)); 727 728 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid")); 729 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q); 730 la->la_nports++; 731 sc->sc_active = la->la_nports; 732 733 lp->lp_state |= LACP_STATE_DISTRIBUTING; 734 735 if (lsc->lsc_active_aggregator == la) { 736 lacp_suppress_distributing(lsc, la); 737 lacp_update_portmap(lsc); 738 } else 739 /* try to become the active aggregator */ 740 lacp_select_active_aggregator(lsc); 741 } 742 743 static void 744 lacp_transit_expire(void *vp) 745 { 746 struct lacp_softc *lsc = vp; 747 748 LACP_LOCK_ASSERT(lsc); 749 750 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet); 751 LACP_TRACE(NULL); 752 CURVNET_RESTORE(); 753 754 lsc->lsc_suppress_distributing = FALSE; 755 } 756 757 void 758 lacp_attach(struct lagg_softc *sc) 759 { 760 struct lacp_softc *lsc; 761 uint32_t seed; 762 763 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO); 764 765 sc->sc_psc = lsc; 766 lsc->lsc_softc = sc; 767 768 seed = arc4random(); 769 lsc->lsc_hashkey = FNV1_32_INIT; 770 lsc->lsc_hashkey = fnv_32_buf(&seed, sizeof(seed), lsc->lsc_hashkey); 771 lsc->lsc_active_aggregator = NULL; 772 lsc->lsc_strict_mode = 1; 773 LACP_LOCK_INIT(lsc); 774 TAILQ_INIT(&lsc->lsc_aggregators); 775 LIST_INIT(&lsc->lsc_ports); 776 777 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0); 778 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0); 779 780 /* if the lagg is already up then do the same */ 781 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) 782 lacp_init(sc); 783 } 784 785 void 786 lacp_detach(void *psc) 787 { 788 struct lacp_softc *lsc = (struct lacp_softc *)psc; 789 790 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators), 791 ("aggregators still active")); 792 KASSERT(lsc->lsc_active_aggregator == NULL, 793 ("aggregator still attached")); 794 795 callout_drain(&lsc->lsc_transit_callout); 796 callout_drain(&lsc->lsc_callout); 797 798 LACP_LOCK_DESTROY(lsc); 799 free(lsc, M_DEVBUF); 800 } 801 802 void 803 lacp_init(struct lagg_softc *sc) 804 { 805 struct lacp_softc *lsc = LACP_SOFTC(sc); 806 807 LACP_LOCK(lsc); 808 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc); 809 LACP_UNLOCK(lsc); 810 } 811 812 void 813 lacp_stop(struct lagg_softc *sc) 814 { 815 struct lacp_softc *lsc = LACP_SOFTC(sc); 816 817 LACP_LOCK(lsc); 818 callout_stop(&lsc->lsc_transit_callout); 819 callout_stop(&lsc->lsc_callout); 820 LACP_UNLOCK(lsc); 821 } 822 823 struct lagg_port * 824 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m) 825 { 826 struct lacp_softc *lsc = LACP_SOFTC(sc); 827 struct lacp_portmap *pm; 828 struct lacp_port *lp; 829 uint32_t hash; 830 831 if (__predict_false(lsc->lsc_suppress_distributing)) { 832 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__)); 833 return (NULL); 834 } 835 836 pm = &lsc->lsc_pmap[lsc->lsc_activemap]; 837 if (pm->pm_count == 0) { 838 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__)); 839 return (NULL); 840 } 841 842 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) && 843 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 844 hash = m->m_pkthdr.flowid >> sc->flowid_shift; 845 else 846 hash = lagg_hashmbuf(sc, m, lsc->lsc_hashkey); 847 hash %= pm->pm_count; 848 lp = pm->pm_map[hash]; 849 850 KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0, 851 ("aggregated port is not distributing")); 852 853 return (lp->lp_lagg); 854 } 855 /* 856 * lacp_suppress_distributing: drop transmit packets for a while 857 * to preserve packet ordering. 858 */ 859 860 static void 861 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la) 862 { 863 struct lacp_port *lp; 864 865 if (lsc->lsc_active_aggregator != la) { 866 return; 867 } 868 869 LACP_TRACE(NULL); 870 871 lsc->lsc_suppress_distributing = TRUE; 872 873 /* send a marker frame down each port to verify the queues are empty */ 874 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) { 875 lp->lp_flags |= LACP_PORT_MARK; 876 lacp_xmit_marker(lp); 877 } 878 879 /* set a timeout for the marker frames */ 880 callout_reset(&lsc->lsc_transit_callout, 881 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc); 882 } 883 884 static int 885 lacp_compare_peerinfo(const struct lacp_peerinfo *a, 886 const struct lacp_peerinfo *b) 887 { 888 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state))); 889 } 890 891 static int 892 lacp_compare_systemid(const struct lacp_systemid *a, 893 const struct lacp_systemid *b) 894 { 895 return (memcmp(a, b, sizeof(*a))); 896 } 897 898 #if 0 /* unused */ 899 static int 900 lacp_compare_portid(const struct lacp_portid *a, 901 const struct lacp_portid *b) 902 { 903 return (memcmp(a, b, sizeof(*a))); 904 } 905 #endif 906 907 static uint64_t 908 lacp_aggregator_bandwidth(struct lacp_aggregator *la) 909 { 910 struct lacp_port *lp; 911 uint64_t speed; 912 913 lp = TAILQ_FIRST(&la->la_ports); 914 if (lp == NULL) { 915 return (0); 916 } 917 918 speed = ifmedia_baudrate(lp->lp_media); 919 speed *= la->la_nports; 920 if (speed == 0) { 921 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n", 922 lp->lp_media, la->la_nports)); 923 } 924 925 return (speed); 926 } 927 928 /* 929 * lacp_select_active_aggregator: select an aggregator to be used to transmit 930 * packets from lagg(4) interface. 931 */ 932 933 static void 934 lacp_select_active_aggregator(struct lacp_softc *lsc) 935 { 936 struct lacp_aggregator *la; 937 struct lacp_aggregator *best_la = NULL; 938 uint64_t best_speed = 0; 939 char buf[LACP_LAGIDSTR_MAX+1]; 940 941 LACP_TRACE(NULL); 942 943 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) { 944 uint64_t speed; 945 946 if (la->la_nports == 0) { 947 continue; 948 } 949 950 speed = lacp_aggregator_bandwidth(la); 951 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n", 952 lacp_format_lagid_aggregator(la, buf, sizeof(buf)), 953 speed, la->la_nports)); 954 955 /* 956 * This aggregator is chosen if the partner has a better 957 * system priority or, the total aggregated speed is higher 958 * or, it is already the chosen aggregator 959 */ 960 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) < 961 LACP_SYS_PRI(best_la->la_partner)) || 962 speed > best_speed || 963 (speed == best_speed && 964 la == lsc->lsc_active_aggregator)) { 965 best_la = la; 966 best_speed = speed; 967 } 968 } 969 970 KASSERT(best_la == NULL || best_la->la_nports > 0, 971 ("invalid aggregator refcnt")); 972 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports), 973 ("invalid aggregator list")); 974 975 if (lsc->lsc_active_aggregator != best_la) { 976 LACP_DPRINTF((NULL, "active aggregator changed\n")); 977 LACP_DPRINTF((NULL, "old %s\n", 978 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator, 979 buf, sizeof(buf)))); 980 } else { 981 LACP_DPRINTF((NULL, "active aggregator not changed\n")); 982 } 983 LACP_DPRINTF((NULL, "new %s\n", 984 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf)))); 985 986 if (lsc->lsc_active_aggregator != best_la) { 987 lsc->lsc_active_aggregator = best_la; 988 lacp_update_portmap(lsc); 989 if (best_la) { 990 lacp_suppress_distributing(lsc, best_la); 991 } 992 } 993 } 994 995 /* 996 * Updated the inactive portmap array with the new list of ports and 997 * make it live. 998 */ 999 static void 1000 lacp_update_portmap(struct lacp_softc *lsc) 1001 { 1002 struct lagg_softc *sc = lsc->lsc_softc; 1003 struct lacp_aggregator *la; 1004 struct lacp_portmap *p; 1005 struct lacp_port *lp; 1006 uint64_t speed; 1007 u_int newmap; 1008 int i; 1009 1010 newmap = lsc->lsc_activemap == 0 ? 1 : 0; 1011 p = &lsc->lsc_pmap[newmap]; 1012 la = lsc->lsc_active_aggregator; 1013 speed = 0; 1014 bzero(p, sizeof(struct lacp_portmap)); 1015 1016 if (la != NULL && la->la_nports > 0) { 1017 p->pm_count = la->la_nports; 1018 i = 0; 1019 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) 1020 p->pm_map[i++] = lp; 1021 KASSERT(i == p->pm_count, ("Invalid port count")); 1022 speed = lacp_aggregator_bandwidth(la); 1023 } 1024 sc->sc_ifp->if_baudrate = speed; 1025 1026 /* switch the active portmap over */ 1027 atomic_store_rel_int(&lsc->lsc_activemap, newmap); 1028 LACP_DPRINTF((NULL, "Set table %d with %d ports\n", 1029 lsc->lsc_activemap, 1030 lsc->lsc_pmap[lsc->lsc_activemap].pm_count)); 1031 } 1032 1033 static uint16_t 1034 lacp_compose_key(struct lacp_port *lp) 1035 { 1036 struct lagg_port *lgp = lp->lp_lagg; 1037 struct lagg_softc *sc = lgp->lp_softc; 1038 u_int media = lp->lp_media; 1039 uint16_t key; 1040 1041 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) { 1042 1043 /* 1044 * non-aggregatable links should have unique keys. 1045 * 1046 * XXX this isn't really unique as if_index is 16 bit. 1047 */ 1048 1049 /* bit 0..14: (some bits of) if_index of this port */ 1050 key = lp->lp_ifp->if_index; 1051 /* bit 15: 1 */ 1052 key |= 0x8000; 1053 } else { 1054 u_int subtype = IFM_SUBTYPE(media); 1055 1056 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type")); 1057 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface")); 1058 1059 /* bit 0..4: IFM_SUBTYPE modulo speed */ 1060 switch (subtype) { 1061 case IFM_10_T: 1062 case IFM_10_2: 1063 case IFM_10_5: 1064 case IFM_10_STP: 1065 case IFM_10_FL: 1066 key = IFM_10_T; 1067 break; 1068 case IFM_100_TX: 1069 case IFM_100_FX: 1070 case IFM_100_T4: 1071 case IFM_100_VG: 1072 case IFM_100_T2: 1073 key = IFM_100_TX; 1074 break; 1075 case IFM_1000_SX: 1076 case IFM_1000_LX: 1077 case IFM_1000_CX: 1078 case IFM_1000_T: 1079 key = IFM_1000_SX; 1080 break; 1081 case IFM_10G_LR: 1082 case IFM_10G_SR: 1083 case IFM_10G_CX4: 1084 case IFM_10G_TWINAX: 1085 case IFM_10G_TWINAX_LONG: 1086 case IFM_10G_LRM: 1087 case IFM_10G_T: 1088 key = IFM_10G_LR; 1089 break; 1090 case IFM_40G_CR4: 1091 case IFM_40G_SR4: 1092 case IFM_40G_LR4: 1093 key = IFM_40G_CR4; 1094 break; 1095 default: 1096 key = subtype; 1097 } 1098 /* bit 5..14: (some bits of) if_index of lagg device */ 1099 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5); 1100 /* bit 15: 0 */ 1101 } 1102 return (htons(key)); 1103 } 1104 1105 static void 1106 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la) 1107 { 1108 char buf[LACP_LAGIDSTR_MAX+1]; 1109 1110 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n", 1111 __func__, 1112 lacp_format_lagid(&la->la_actor, &la->la_partner, 1113 buf, sizeof(buf)), 1114 la->la_refcnt, la->la_refcnt + 1)); 1115 1116 KASSERT(la->la_refcnt > 0, ("refcount <= 0")); 1117 la->la_refcnt++; 1118 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount")); 1119 } 1120 1121 static void 1122 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la) 1123 { 1124 char buf[LACP_LAGIDSTR_MAX+1]; 1125 1126 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n", 1127 __func__, 1128 lacp_format_lagid(&la->la_actor, &la->la_partner, 1129 buf, sizeof(buf)), 1130 la->la_refcnt, la->la_refcnt - 1)); 1131 1132 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt")); 1133 la->la_refcnt--; 1134 if (la->la_refcnt > 0) { 1135 return; 1136 } 1137 1138 KASSERT(la->la_refcnt == 0, ("refcount not zero")); 1139 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active")); 1140 1141 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q); 1142 1143 free(la, M_DEVBUF); 1144 } 1145 1146 /* 1147 * lacp_aggregator_get: allocate an aggregator. 1148 */ 1149 1150 static struct lacp_aggregator * 1151 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp) 1152 { 1153 struct lacp_aggregator *la; 1154 1155 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT); 1156 if (la) { 1157 la->la_refcnt = 1; 1158 la->la_nports = 0; 1159 TAILQ_INIT(&la->la_ports); 1160 la->la_pending = 0; 1161 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q); 1162 } 1163 1164 return (la); 1165 } 1166 1167 /* 1168 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port. 1169 */ 1170 1171 static void 1172 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp) 1173 { 1174 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner); 1175 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor); 1176 1177 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION; 1178 } 1179 1180 static void 1181 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr, 1182 const struct lacp_peerinfo *lpi_port) 1183 { 1184 memset(lpi_aggr, 0, sizeof(*lpi_aggr)); 1185 lpi_aggr->lip_systemid = lpi_port->lip_systemid; 1186 lpi_aggr->lip_key = lpi_port->lip_key; 1187 } 1188 1189 /* 1190 * lacp_aggregator_is_compatible: check if a port can join to an aggregator. 1191 */ 1192 1193 static int 1194 lacp_aggregator_is_compatible(const struct lacp_aggregator *la, 1195 const struct lacp_port *lp) 1196 { 1197 if (!(lp->lp_state & LACP_STATE_AGGREGATION) || 1198 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) { 1199 return (0); 1200 } 1201 1202 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) { 1203 return (0); 1204 } 1205 1206 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) { 1207 return (0); 1208 } 1209 1210 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) { 1211 return (0); 1212 } 1213 1214 return (1); 1215 } 1216 1217 static int 1218 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a, 1219 const struct lacp_peerinfo *b) 1220 { 1221 if (memcmp(&a->lip_systemid, &b->lip_systemid, 1222 sizeof(a->lip_systemid))) { 1223 return (0); 1224 } 1225 1226 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) { 1227 return (0); 1228 } 1229 1230 return (1); 1231 } 1232 1233 static void 1234 lacp_port_enable(struct lacp_port *lp) 1235 { 1236 lp->lp_state |= LACP_STATE_AGGREGATION; 1237 } 1238 1239 static void 1240 lacp_port_disable(struct lacp_port *lp) 1241 { 1242 lacp_set_mux(lp, LACP_MUX_DETACHED); 1243 1244 lp->lp_state &= ~LACP_STATE_AGGREGATION; 1245 lp->lp_selected = LACP_UNSELECTED; 1246 lacp_sm_rx_record_default(lp); 1247 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION; 1248 lp->lp_state &= ~LACP_STATE_EXPIRED; 1249 } 1250 1251 /* 1252 * lacp_select: select an aggregator. create one if necessary. 1253 */ 1254 static void 1255 lacp_select(struct lacp_port *lp) 1256 { 1257 struct lacp_softc *lsc = lp->lp_lsc; 1258 struct lacp_aggregator *la; 1259 char buf[LACP_LAGIDSTR_MAX+1]; 1260 1261 if (lp->lp_aggregator) { 1262 return; 1263 } 1264 1265 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE), 1266 ("timer_wait_while still active")); 1267 1268 LACP_DPRINTF((lp, "port lagid=%s\n", 1269 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner, 1270 buf, sizeof(buf)))); 1271 1272 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) { 1273 if (lacp_aggregator_is_compatible(la, lp)) { 1274 break; 1275 } 1276 } 1277 1278 if (la == NULL) { 1279 la = lacp_aggregator_get(lsc, lp); 1280 if (la == NULL) { 1281 LACP_DPRINTF((lp, "aggregator creation failed\n")); 1282 1283 /* 1284 * will retry on the next tick. 1285 */ 1286 1287 return; 1288 } 1289 lacp_fill_aggregator_id(la, lp); 1290 LACP_DPRINTF((lp, "aggregator created\n")); 1291 } else { 1292 LACP_DPRINTF((lp, "compatible aggregator found\n")); 1293 if (la->la_refcnt == LACP_MAX_PORTS) 1294 return; 1295 lacp_aggregator_addref(lsc, la); 1296 } 1297 1298 LACP_DPRINTF((lp, "aggregator lagid=%s\n", 1299 lacp_format_lagid(&la->la_actor, &la->la_partner, 1300 buf, sizeof(buf)))); 1301 1302 lp->lp_aggregator = la; 1303 lp->lp_selected = LACP_SELECTED; 1304 } 1305 1306 /* 1307 * lacp_unselect: finish unselect/detach process. 1308 */ 1309 1310 static void 1311 lacp_unselect(struct lacp_port *lp) 1312 { 1313 struct lacp_softc *lsc = lp->lp_lsc; 1314 struct lacp_aggregator *la = lp->lp_aggregator; 1315 1316 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE), 1317 ("timer_wait_while still active")); 1318 1319 if (la == NULL) { 1320 return; 1321 } 1322 1323 lp->lp_aggregator = NULL; 1324 lacp_aggregator_delref(lsc, la); 1325 } 1326 1327 /* mux machine */ 1328 1329 static void 1330 lacp_sm_mux(struct lacp_port *lp) 1331 { 1332 struct lagg_port *lgp = lp->lp_lagg; 1333 struct lagg_softc *sc = lgp->lp_softc; 1334 enum lacp_mux_state new_state; 1335 boolean_t p_sync = 1336 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0; 1337 boolean_t p_collecting = 1338 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0; 1339 enum lacp_selected selected = lp->lp_selected; 1340 struct lacp_aggregator *la; 1341 1342 if (V_lacp_debug > 1) 1343 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, " 1344 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__, 1345 lp->lp_mux_state, selected, p_sync, p_collecting); 1346 1347 re_eval: 1348 la = lp->lp_aggregator; 1349 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL, 1350 ("MUX not detached")); 1351 new_state = lp->lp_mux_state; 1352 switch (lp->lp_mux_state) { 1353 case LACP_MUX_DETACHED: 1354 if (selected != LACP_UNSELECTED) { 1355 new_state = LACP_MUX_WAITING; 1356 } 1357 break; 1358 case LACP_MUX_WAITING: 1359 KASSERT(la->la_pending > 0 || 1360 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE), 1361 ("timer_wait_while still active")); 1362 if (selected == LACP_SELECTED && la->la_pending == 0) { 1363 new_state = LACP_MUX_ATTACHED; 1364 } else if (selected == LACP_UNSELECTED) { 1365 new_state = LACP_MUX_DETACHED; 1366 } 1367 break; 1368 case LACP_MUX_ATTACHED: 1369 if (selected == LACP_SELECTED && p_sync) { 1370 new_state = LACP_MUX_COLLECTING; 1371 } else if (selected != LACP_SELECTED) { 1372 new_state = LACP_MUX_DETACHED; 1373 } 1374 break; 1375 case LACP_MUX_COLLECTING: 1376 if (selected == LACP_SELECTED && p_sync && p_collecting) { 1377 new_state = LACP_MUX_DISTRIBUTING; 1378 } else if (selected != LACP_SELECTED || !p_sync) { 1379 new_state = LACP_MUX_ATTACHED; 1380 } 1381 break; 1382 case LACP_MUX_DISTRIBUTING: 1383 if (selected != LACP_SELECTED || !p_sync || !p_collecting) { 1384 new_state = LACP_MUX_COLLECTING; 1385 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n"); 1386 sc->sc_flapping++; 1387 } 1388 break; 1389 default: 1390 panic("%s: unknown state", __func__); 1391 } 1392 1393 if (lp->lp_mux_state == new_state) { 1394 return; 1395 } 1396 1397 lacp_set_mux(lp, new_state); 1398 goto re_eval; 1399 } 1400 1401 static void 1402 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state) 1403 { 1404 struct lacp_aggregator *la = lp->lp_aggregator; 1405 1406 if (lp->lp_mux_state == new_state) { 1407 return; 1408 } 1409 1410 switch (new_state) { 1411 case LACP_MUX_DETACHED: 1412 lp->lp_state &= ~LACP_STATE_SYNC; 1413 lacp_disable_distributing(lp); 1414 lacp_disable_collecting(lp); 1415 lacp_sm_assert_ntt(lp); 1416 /* cancel timer */ 1417 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) { 1418 KASSERT(la->la_pending > 0, 1419 ("timer_wait_while not active")); 1420 la->la_pending--; 1421 } 1422 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE); 1423 lacp_unselect(lp); 1424 break; 1425 case LACP_MUX_WAITING: 1426 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE, 1427 LACP_AGGREGATE_WAIT_TIME); 1428 la->la_pending++; 1429 break; 1430 case LACP_MUX_ATTACHED: 1431 lp->lp_state |= LACP_STATE_SYNC; 1432 lacp_disable_collecting(lp); 1433 lacp_sm_assert_ntt(lp); 1434 break; 1435 case LACP_MUX_COLLECTING: 1436 lacp_enable_collecting(lp); 1437 lacp_disable_distributing(lp); 1438 lacp_sm_assert_ntt(lp); 1439 break; 1440 case LACP_MUX_DISTRIBUTING: 1441 lacp_enable_distributing(lp); 1442 break; 1443 default: 1444 panic("%s: unknown state", __func__); 1445 } 1446 1447 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state)); 1448 1449 lp->lp_mux_state = new_state; 1450 } 1451 1452 static void 1453 lacp_sm_mux_timer(struct lacp_port *lp) 1454 { 1455 struct lacp_aggregator *la = lp->lp_aggregator; 1456 char buf[LACP_LAGIDSTR_MAX+1]; 1457 1458 KASSERT(la->la_pending > 0, ("no pending event")); 1459 1460 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__, 1461 lacp_format_lagid(&la->la_actor, &la->la_partner, 1462 buf, sizeof(buf)), 1463 la->la_pending, la->la_pending - 1)); 1464 1465 la->la_pending--; 1466 } 1467 1468 /* periodic transmit machine */ 1469 1470 static void 1471 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate) 1472 { 1473 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state, 1474 LACP_STATE_TIMEOUT)) { 1475 return; 1476 } 1477 1478 LACP_DPRINTF((lp, "partner timeout changed\n")); 1479 1480 /* 1481 * FAST_PERIODIC -> SLOW_PERIODIC 1482 * or 1483 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC 1484 * 1485 * let lacp_sm_ptx_tx_schedule to update timeout. 1486 */ 1487 1488 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC); 1489 1490 /* 1491 * if timeout has been shortened, assert NTT. 1492 */ 1493 1494 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) { 1495 lacp_sm_assert_ntt(lp); 1496 } 1497 } 1498 1499 static void 1500 lacp_sm_ptx_tx_schedule(struct lacp_port *lp) 1501 { 1502 int timeout; 1503 1504 if (!(lp->lp_state & LACP_STATE_ACTIVITY) && 1505 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) { 1506 1507 /* 1508 * NO_PERIODIC 1509 */ 1510 1511 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC); 1512 return; 1513 } 1514 1515 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) { 1516 return; 1517 } 1518 1519 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ? 1520 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME; 1521 1522 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout); 1523 } 1524 1525 static void 1526 lacp_sm_ptx_timer(struct lacp_port *lp) 1527 { 1528 lacp_sm_assert_ntt(lp); 1529 } 1530 1531 static void 1532 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du) 1533 { 1534 int timeout; 1535 1536 /* 1537 * check LACP_DISABLED first 1538 */ 1539 1540 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) { 1541 return; 1542 } 1543 1544 /* 1545 * check loopback condition. 1546 */ 1547 1548 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid, 1549 &lp->lp_actor.lip_systemid)) { 1550 return; 1551 } 1552 1553 /* 1554 * EXPIRED, DEFAULTED, CURRENT -> CURRENT 1555 */ 1556 1557 lacp_sm_rx_update_selected(lp, du); 1558 lacp_sm_rx_update_ntt(lp, du); 1559 lacp_sm_rx_record_pdu(lp, du); 1560 1561 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ? 1562 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME; 1563 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout); 1564 1565 lp->lp_state &= ~LACP_STATE_EXPIRED; 1566 1567 /* 1568 * kick transmit machine without waiting the next tick. 1569 */ 1570 1571 lacp_sm_tx(lp); 1572 } 1573 1574 static void 1575 lacp_sm_rx_set_expired(struct lacp_port *lp) 1576 { 1577 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC; 1578 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT; 1579 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME); 1580 lp->lp_state |= LACP_STATE_EXPIRED; 1581 } 1582 1583 static void 1584 lacp_sm_rx_timer(struct lacp_port *lp) 1585 { 1586 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) { 1587 /* CURRENT -> EXPIRED */ 1588 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__)); 1589 lacp_sm_rx_set_expired(lp); 1590 } else { 1591 /* EXPIRED -> DEFAULTED */ 1592 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__)); 1593 lacp_sm_rx_update_default_selected(lp); 1594 lacp_sm_rx_record_default(lp); 1595 lp->lp_state &= ~LACP_STATE_EXPIRED; 1596 } 1597 } 1598 1599 static void 1600 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du) 1601 { 1602 boolean_t active; 1603 uint8_t oldpstate; 1604 char buf[LACP_STATESTR_MAX+1]; 1605 1606 LACP_TRACE(lp); 1607 1608 oldpstate = lp->lp_partner.lip_state; 1609 1610 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY) 1611 || ((lp->lp_state & LACP_STATE_ACTIVITY) && 1612 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY)); 1613 1614 lp->lp_partner = du->ldu_actor; 1615 if (active && 1616 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state, 1617 LACP_STATE_AGGREGATION) && 1618 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner)) 1619 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) { 1620 /* XXX nothing? */ 1621 } else { 1622 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC; 1623 } 1624 1625 lp->lp_state &= ~LACP_STATE_DEFAULTED; 1626 1627 if (oldpstate != lp->lp_partner.lip_state) { 1628 LACP_DPRINTF((lp, "old pstate %s\n", 1629 lacp_format_state(oldpstate, buf, sizeof(buf)))); 1630 LACP_DPRINTF((lp, "new pstate %s\n", 1631 lacp_format_state(lp->lp_partner.lip_state, buf, 1632 sizeof(buf)))); 1633 } 1634 1635 /* XXX Hack, still need to implement 5.4.9 para 2,3,4 */ 1636 if (lp->lp_lsc->lsc_strict_mode) 1637 lp->lp_partner.lip_state |= LACP_STATE_SYNC; 1638 1639 lacp_sm_ptx_update_timeout(lp, oldpstate); 1640 } 1641 1642 static void 1643 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du) 1644 { 1645 1646 LACP_TRACE(lp); 1647 1648 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) || 1649 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state, 1650 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) { 1651 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__)); 1652 lacp_sm_assert_ntt(lp); 1653 } 1654 } 1655 1656 static void 1657 lacp_sm_rx_record_default(struct lacp_port *lp) 1658 { 1659 uint8_t oldpstate; 1660 1661 LACP_TRACE(lp); 1662 1663 oldpstate = lp->lp_partner.lip_state; 1664 if (lp->lp_lsc->lsc_strict_mode) 1665 lp->lp_partner = lacp_partner_admin_strict; 1666 else 1667 lp->lp_partner = lacp_partner_admin_optimistic;; 1668 lp->lp_state |= LACP_STATE_DEFAULTED; 1669 lacp_sm_ptx_update_timeout(lp, oldpstate); 1670 } 1671 1672 static void 1673 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp, 1674 const struct lacp_peerinfo *info) 1675 { 1676 1677 LACP_TRACE(lp); 1678 1679 if (lacp_compare_peerinfo(&lp->lp_partner, info) || 1680 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state, 1681 LACP_STATE_AGGREGATION)) { 1682 lp->lp_selected = LACP_UNSELECTED; 1683 /* mux machine will clean up lp->lp_aggregator */ 1684 } 1685 } 1686 1687 static void 1688 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du) 1689 { 1690 1691 LACP_TRACE(lp); 1692 1693 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor); 1694 } 1695 1696 static void 1697 lacp_sm_rx_update_default_selected(struct lacp_port *lp) 1698 { 1699 1700 LACP_TRACE(lp); 1701 1702 if (lp->lp_lsc->lsc_strict_mode) 1703 lacp_sm_rx_update_selected_from_peerinfo(lp, 1704 &lacp_partner_admin_strict); 1705 else 1706 lacp_sm_rx_update_selected_from_peerinfo(lp, 1707 &lacp_partner_admin_optimistic); 1708 } 1709 1710 /* transmit machine */ 1711 1712 static void 1713 lacp_sm_tx(struct lacp_port *lp) 1714 { 1715 int error = 0; 1716 1717 if (!(lp->lp_state & LACP_STATE_AGGREGATION) 1718 #if 1 1719 || (!(lp->lp_state & LACP_STATE_ACTIVITY) 1720 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) 1721 #endif 1722 ) { 1723 lp->lp_flags &= ~LACP_PORT_NTT; 1724 } 1725 1726 if (!(lp->lp_flags & LACP_PORT_NTT)) { 1727 return; 1728 } 1729 1730 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */ 1731 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent, 1732 (3 / LACP_FAST_PERIODIC_TIME)) == 0) { 1733 LACP_DPRINTF((lp, "rate limited pdu\n")); 1734 return; 1735 } 1736 1737 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) { 1738 error = lacp_xmit_lacpdu(lp); 1739 } else { 1740 LACP_TPRINTF((lp, "Dropping TX PDU\n")); 1741 } 1742 1743 if (error == 0) { 1744 lp->lp_flags &= ~LACP_PORT_NTT; 1745 } else { 1746 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n", 1747 error)); 1748 } 1749 } 1750 1751 static void 1752 lacp_sm_assert_ntt(struct lacp_port *lp) 1753 { 1754 1755 lp->lp_flags |= LACP_PORT_NTT; 1756 } 1757 1758 static void 1759 lacp_run_timers(struct lacp_port *lp) 1760 { 1761 int i; 1762 1763 for (i = 0; i < LACP_NTIMER; i++) { 1764 KASSERT(lp->lp_timer[i] >= 0, 1765 ("invalid timer value %d", lp->lp_timer[i])); 1766 if (lp->lp_timer[i] == 0) { 1767 continue; 1768 } else if (--lp->lp_timer[i] <= 0) { 1769 if (lacp_timer_funcs[i]) { 1770 (*lacp_timer_funcs[i])(lp); 1771 } 1772 } 1773 } 1774 } 1775 1776 int 1777 lacp_marker_input(struct lacp_port *lp, struct mbuf *m) 1778 { 1779 struct lacp_softc *lsc = lp->lp_lsc; 1780 struct lagg_port *lgp = lp->lp_lagg; 1781 struct lacp_port *lp2; 1782 struct markerdu *mdu; 1783 int error = 0; 1784 int pending = 0; 1785 1786 if (m->m_pkthdr.len != sizeof(*mdu)) { 1787 goto bad; 1788 } 1789 1790 if ((m->m_flags & M_MCAST) == 0) { 1791 goto bad; 1792 } 1793 1794 if (m->m_len < sizeof(*mdu)) { 1795 m = m_pullup(m, sizeof(*mdu)); 1796 if (m == NULL) { 1797 return (ENOMEM); 1798 } 1799 } 1800 1801 mdu = mtod(m, struct markerdu *); 1802 1803 if (memcmp(&mdu->mdu_eh.ether_dhost, 1804 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) { 1805 goto bad; 1806 } 1807 1808 if (mdu->mdu_sph.sph_version != 1) { 1809 goto bad; 1810 } 1811 1812 switch (mdu->mdu_tlv.tlv_type) { 1813 case MARKER_TYPE_INFO: 1814 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv, 1815 marker_info_tlv_template, TRUE)) { 1816 goto bad; 1817 } 1818 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE; 1819 memcpy(&mdu->mdu_eh.ether_dhost, 1820 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN); 1821 memcpy(&mdu->mdu_eh.ether_shost, 1822 lgp->lp_lladdr, ETHER_ADDR_LEN); 1823 error = lagg_enqueue(lp->lp_ifp, m); 1824 break; 1825 1826 case MARKER_TYPE_RESPONSE: 1827 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv, 1828 marker_response_tlv_template, TRUE)) { 1829 goto bad; 1830 } 1831 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n", 1832 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, 1833 ":", ntohl(mdu->mdu_info.mi_rq_xid))); 1834 1835 /* Verify that it is the last marker we sent out */ 1836 if (memcmp(&mdu->mdu_info, &lp->lp_marker, 1837 sizeof(struct lacp_markerinfo))) 1838 goto bad; 1839 1840 LACP_LOCK(lsc); 1841 lp->lp_flags &= ~LACP_PORT_MARK; 1842 1843 if (lsc->lsc_suppress_distributing) { 1844 /* Check if any ports are waiting for a response */ 1845 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) { 1846 if (lp2->lp_flags & LACP_PORT_MARK) { 1847 pending = 1; 1848 break; 1849 } 1850 } 1851 1852 if (pending == 0) { 1853 /* All interface queues are clear */ 1854 LACP_DPRINTF((NULL, "queue flush complete\n")); 1855 lsc->lsc_suppress_distributing = FALSE; 1856 } 1857 } 1858 LACP_UNLOCK(lsc); 1859 m_freem(m); 1860 break; 1861 1862 default: 1863 goto bad; 1864 } 1865 1866 return (error); 1867 1868 bad: 1869 LACP_DPRINTF((lp, "bad marker frame\n")); 1870 m_freem(m); 1871 return (EINVAL); 1872 } 1873 1874 static int 1875 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv, 1876 const struct tlv_template *tmpl, boolean_t check_type) 1877 { 1878 while (/* CONSTCOND */ 1) { 1879 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) { 1880 return (EINVAL); 1881 } 1882 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) || 1883 tlv->tlv_length != tmpl->tmpl_length) { 1884 return (EINVAL); 1885 } 1886 if (tmpl->tmpl_type == 0) { 1887 break; 1888 } 1889 tlv = (const struct tlvhdr *) 1890 ((const char *)tlv + tlv->tlv_length); 1891 tmpl++; 1892 } 1893 1894 return (0); 1895 } 1896 1897 /* Debugging */ 1898 const char * 1899 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen) 1900 { 1901 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X", 1902 (int)mac[0], 1903 (int)mac[1], 1904 (int)mac[2], 1905 (int)mac[3], 1906 (int)mac[4], 1907 (int)mac[5]); 1908 1909 return (buf); 1910 } 1911 1912 const char * 1913 lacp_format_systemid(const struct lacp_systemid *sysid, 1914 char *buf, size_t buflen) 1915 { 1916 char macbuf[LACP_MACSTR_MAX+1]; 1917 1918 snprintf(buf, buflen, "%04X,%s", 1919 ntohs(sysid->lsi_prio), 1920 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf))); 1921 1922 return (buf); 1923 } 1924 1925 const char * 1926 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen) 1927 { 1928 snprintf(buf, buflen, "%04X,%04X", 1929 ntohs(portid->lpi_prio), 1930 ntohs(portid->lpi_portno)); 1931 1932 return (buf); 1933 } 1934 1935 const char * 1936 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen) 1937 { 1938 char sysid[LACP_SYSTEMIDSTR_MAX+1]; 1939 char portid[LACP_PORTIDSTR_MAX+1]; 1940 1941 snprintf(buf, buflen, "(%s,%04X,%s)", 1942 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)), 1943 ntohs(peer->lip_key), 1944 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid))); 1945 1946 return (buf); 1947 } 1948 1949 const char * 1950 lacp_format_lagid(const struct lacp_peerinfo *a, 1951 const struct lacp_peerinfo *b, char *buf, size_t buflen) 1952 { 1953 char astr[LACP_PARTNERSTR_MAX+1]; 1954 char bstr[LACP_PARTNERSTR_MAX+1]; 1955 1956 #if 0 1957 /* 1958 * there's a convention to display small numbered peer 1959 * in the left. 1960 */ 1961 1962 if (lacp_compare_peerinfo(a, b) > 0) { 1963 const struct lacp_peerinfo *t; 1964 1965 t = a; 1966 a = b; 1967 b = t; 1968 } 1969 #endif 1970 1971 snprintf(buf, buflen, "[%s,%s]", 1972 lacp_format_partner(a, astr, sizeof(astr)), 1973 lacp_format_partner(b, bstr, sizeof(bstr))); 1974 1975 return (buf); 1976 } 1977 1978 const char * 1979 lacp_format_lagid_aggregator(const struct lacp_aggregator *la, 1980 char *buf, size_t buflen) 1981 { 1982 if (la == NULL) { 1983 return ("(none)"); 1984 } 1985 1986 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen)); 1987 } 1988 1989 const char * 1990 lacp_format_state(uint8_t state, char *buf, size_t buflen) 1991 { 1992 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS); 1993 return (buf); 1994 } 1995 1996 static void 1997 lacp_dump_lacpdu(const struct lacpdu *du) 1998 { 1999 char buf[LACP_PARTNERSTR_MAX+1]; 2000 char buf2[LACP_STATESTR_MAX+1]; 2001 2002 printf("actor=%s\n", 2003 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf))); 2004 printf("actor.state=%s\n", 2005 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2))); 2006 printf("partner=%s\n", 2007 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf))); 2008 printf("partner.state=%s\n", 2009 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2))); 2010 2011 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay)); 2012 } 2013 2014 static void 2015 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...) 2016 { 2017 va_list va; 2018 2019 if (lp) { 2020 printf("%s: ", lp->lp_ifp->if_xname); 2021 } 2022 2023 va_start(va, fmt); 2024 vprintf(fmt, va); 2025 va_end(va); 2026 } 2027