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