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