1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org> 5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/cdefs.h> 21 __FBSDID("$FreeBSD$"); 22 23 #include "opt_inet.h" 24 #include "opt_inet6.h" 25 26 #include <sys/param.h> 27 #include <sys/kernel.h> 28 #include <sys/malloc.h> 29 #include <sys/mbuf.h> 30 #include <sys/queue.h> 31 #include <sys/socket.h> 32 #include <sys/sockio.h> 33 #include <sys/sysctl.h> 34 #include <sys/module.h> 35 #include <sys/priv.h> 36 #include <sys/systm.h> 37 #include <sys/proc.h> 38 #include <sys/hash.h> 39 #include <sys/lock.h> 40 #include <sys/rwlock.h> 41 #include <sys/taskqueue.h> 42 #include <sys/eventhandler.h> 43 44 #include <net/ethernet.h> 45 #include <net/if.h> 46 #include <net/if_clone.h> 47 #include <net/if_arp.h> 48 #include <net/if_dl.h> 49 #include <net/if_llc.h> 50 #include <net/if_media.h> 51 #include <net/if_types.h> 52 #include <net/if_var.h> 53 #include <net/bpf.h> 54 55 #ifdef INET 56 #include <netinet/in.h> 57 #include <netinet/in_systm.h> 58 #include <netinet/if_ether.h> 59 #include <netinet/ip.h> 60 #endif 61 62 #ifdef INET6 63 #include <netinet/ip6.h> 64 #endif 65 66 #include <net/if_vlan_var.h> 67 #include <net/if_lagg.h> 68 #include <net/ieee8023ad_lacp.h> 69 70 /* Special flags we should propagate to the lagg ports. */ 71 static struct { 72 int flag; 73 int (*func)(struct ifnet *, int); 74 } lagg_pflags[] = { 75 {IFF_PROMISC, ifpromisc}, 76 {IFF_ALLMULTI, if_allmulti}, 77 {0, NULL} 78 }; 79 80 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */ 81 static struct mtx lagg_list_mtx; 82 eventhandler_tag lagg_detach_cookie = NULL; 83 84 static int lagg_clone_create(struct if_clone *, int, caddr_t); 85 static void lagg_clone_destroy(struct ifnet *); 86 static void lagg_lladdr(struct lagg_softc *, uint8_t *); 87 static void lagg_capabilities(struct lagg_softc *); 88 static void lagg_port_lladdr(struct lagg_port *, uint8_t *); 89 static void lagg_port_setlladdr(void *, int); 90 static int lagg_port_create(struct lagg_softc *, struct ifnet *); 91 static int lagg_port_destroy(struct lagg_port *, int); 92 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *); 93 static void lagg_linkstate(struct lagg_softc *); 94 static void lagg_port_state(struct ifnet *, int); 95 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t); 96 static int lagg_port_output(struct ifnet *, struct mbuf *, 97 struct sockaddr *, struct route *); 98 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *); 99 #ifdef LAGG_PORT_STACKING 100 static int lagg_port_checkstacking(struct lagg_softc *); 101 #endif 102 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *); 103 static void lagg_init(void *); 104 static void lagg_stop(struct lagg_softc *); 105 static int lagg_ioctl(struct ifnet *, u_long, caddr_t); 106 static int lagg_ether_setmulti(struct lagg_softc *); 107 static int lagg_ether_cmdmulti(struct lagg_port *, int); 108 static int lagg_setflag(struct lagg_port *, int, int, 109 int (*func)(struct ifnet *, int)); 110 static int lagg_setflags(struct lagg_port *, int status); 111 static void lagg_start(struct ifnet *); 112 static int lagg_media_change(struct ifnet *); 113 static void lagg_media_status(struct ifnet *, struct ifmediareq *); 114 static struct lagg_port *lagg_link_active(struct lagg_softc *, 115 struct lagg_port *); 116 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *); 117 118 IFC_SIMPLE_DECLARE(lagg, 0); 119 120 /* Simple round robin */ 121 static int lagg_rr_attach(struct lagg_softc *); 122 static int lagg_rr_detach(struct lagg_softc *); 123 static int lagg_rr_start(struct lagg_softc *, struct mbuf *); 124 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *, 125 struct mbuf *); 126 127 /* Active failover */ 128 static int lagg_fail_attach(struct lagg_softc *); 129 static int lagg_fail_detach(struct lagg_softc *); 130 static int lagg_fail_start(struct lagg_softc *, struct mbuf *); 131 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *, 132 struct mbuf *); 133 134 /* Loadbalancing */ 135 static int lagg_lb_attach(struct lagg_softc *); 136 static int lagg_lb_detach(struct lagg_softc *); 137 static int lagg_lb_port_create(struct lagg_port *); 138 static void lagg_lb_port_destroy(struct lagg_port *); 139 static int lagg_lb_start(struct lagg_softc *, struct mbuf *); 140 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *, 141 struct mbuf *); 142 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *); 143 144 /* 802.3ad LACP */ 145 static int lagg_lacp_attach(struct lagg_softc *); 146 static int lagg_lacp_detach(struct lagg_softc *); 147 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *); 148 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *, 149 struct mbuf *); 150 static void lagg_lacp_lladdr(struct lagg_softc *); 151 152 /* lagg protocol table */ 153 static const struct { 154 int ti_proto; 155 int (*ti_attach)(struct lagg_softc *); 156 } lagg_protos[] = { 157 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach }, 158 { LAGG_PROTO_FAILOVER, lagg_fail_attach }, 159 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach }, 160 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach }, 161 { LAGG_PROTO_LACP, lagg_lacp_attach }, 162 { LAGG_PROTO_NONE, NULL } 163 }; 164 165 SYSCTL_DECL(_net_link); 166 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, "Link Aggregation"); 167 168 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */ 169 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW, 170 &lagg_failover_rx_all, 0, 171 "Accept input from any interface in a failover lagg"); 172 173 static int 174 lagg_modevent(module_t mod, int type, void *data) 175 { 176 177 switch (type) { 178 case MOD_LOAD: 179 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF); 180 SLIST_INIT(&lagg_list); 181 if_clone_attach(&lagg_cloner); 182 lagg_input_p = lagg_input; 183 lagg_linkstate_p = lagg_port_state; 184 lagg_detach_cookie = EVENTHANDLER_REGISTER( 185 ifnet_departure_event, lagg_port_ifdetach, NULL, 186 EVENTHANDLER_PRI_ANY); 187 break; 188 case MOD_UNLOAD: 189 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 190 lagg_detach_cookie); 191 if_clone_detach(&lagg_cloner); 192 lagg_input_p = NULL; 193 lagg_linkstate_p = NULL; 194 mtx_destroy(&lagg_list_mtx); 195 break; 196 default: 197 return (EOPNOTSUPP); 198 } 199 return (0); 200 } 201 202 static moduledata_t lagg_mod = { 203 "if_lagg", 204 lagg_modevent, 205 0 206 }; 207 208 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 209 210 #if __FreeBSD_version >= 800000 211 /* 212 * This routine is run via an vlan 213 * config EVENT 214 */ 215 static void 216 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 217 { 218 struct lagg_softc *sc = ifp->if_softc; 219 struct lagg_port *lp; 220 221 if (ifp->if_softc != arg) /* Not our event */ 222 return; 223 224 LAGG_RLOCK(sc); 225 if (!SLIST_EMPTY(&sc->sc_ports)) { 226 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 227 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag); 228 } 229 LAGG_RUNLOCK(sc); 230 } 231 232 /* 233 * This routine is run via an vlan 234 * unconfig EVENT 235 */ 236 static void 237 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 238 { 239 struct lagg_softc *sc = ifp->if_softc; 240 struct lagg_port *lp; 241 242 if (ifp->if_softc != arg) /* Not our event */ 243 return; 244 245 LAGG_RLOCK(sc); 246 if (!SLIST_EMPTY(&sc->sc_ports)) { 247 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 248 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag); 249 } 250 LAGG_RUNLOCK(sc); 251 } 252 #endif 253 254 static int 255 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params) 256 { 257 struct lagg_softc *sc; 258 struct ifnet *ifp; 259 int i, error = 0; 260 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */ 261 262 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); 263 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 264 if (ifp == NULL) { 265 free(sc, M_DEVBUF); 266 return (ENOSPC); 267 } 268 269 sc->sc_proto = LAGG_PROTO_NONE; 270 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) { 271 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) { 272 sc->sc_proto = lagg_protos[i].ti_proto; 273 if ((error = lagg_protos[i].ti_attach(sc)) != 0) { 274 if_free_type(ifp, IFT_ETHER); 275 free(sc, M_DEVBUF); 276 return (error); 277 } 278 break; 279 } 280 } 281 LAGG_LOCK_INIT(sc); 282 SLIST_INIT(&sc->sc_ports); 283 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc); 284 285 /* Initialise pseudo media types */ 286 ifmedia_init(&sc->sc_media, 0, lagg_media_change, 287 lagg_media_status); 288 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL); 289 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO); 290 291 if_initname(ifp, ifc->ifc_name, unit); 292 ifp->if_type = IFT_ETHER; 293 ifp->if_softc = sc; 294 ifp->if_start = lagg_start; 295 ifp->if_init = lagg_init; 296 ifp->if_ioctl = lagg_ioctl; 297 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 298 299 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 300 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 301 IFQ_SET_READY(&ifp->if_snd); 302 303 /* 304 * Attach as an ordinary ethernet device, childs will be attached 305 * as special device IFT_IEEE8023ADLAG. 306 */ 307 ether_ifattach(ifp, eaddr); 308 309 #if __FreeBSD_version >= 800000 310 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config, 311 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST); 312 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig, 313 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST); 314 #endif 315 316 /* Insert into the global list of laggs */ 317 mtx_lock(&lagg_list_mtx); 318 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries); 319 mtx_unlock(&lagg_list_mtx); 320 321 return (0); 322 } 323 324 static void 325 lagg_clone_destroy(struct ifnet *ifp) 326 { 327 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 328 struct lagg_port *lp; 329 330 LAGG_WLOCK(sc); 331 332 lagg_stop(sc); 333 ifp->if_flags &= ~IFF_UP; 334 335 #if __FreeBSD_version >= 800000 336 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach); 337 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach); 338 #endif 339 340 /* Shutdown and remove lagg ports */ 341 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL) 342 lagg_port_destroy(lp, 1); 343 /* Unhook the aggregation protocol */ 344 if (sc->sc_detach != NULL) 345 (*sc->sc_detach)(sc); 346 347 LAGG_WUNLOCK(sc); 348 349 ifmedia_removeall(&sc->sc_media); 350 ether_ifdetach(ifp); 351 if_free_type(ifp, IFT_ETHER); 352 353 mtx_lock(&lagg_list_mtx); 354 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries); 355 mtx_unlock(&lagg_list_mtx); 356 357 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task); 358 LAGG_LOCK_DESTROY(sc); 359 free(sc, M_DEVBUF); 360 } 361 362 static void 363 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr) 364 { 365 struct ifnet *ifp = sc->sc_ifp; 366 367 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 368 return; 369 370 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 371 /* Let the protocol know the MAC has changed */ 372 if (sc->sc_lladdr != NULL) 373 (*sc->sc_lladdr)(sc); 374 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 375 } 376 377 static void 378 lagg_capabilities(struct lagg_softc *sc) 379 { 380 struct lagg_port *lp; 381 int cap = ~0, ena = ~0; 382 u_long hwa = ~0UL; 383 384 LAGG_WLOCK_ASSERT(sc); 385 386 /* Get capabilities from the lagg ports */ 387 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 388 cap &= lp->lp_ifp->if_capabilities; 389 ena &= lp->lp_ifp->if_capenable; 390 hwa &= lp->lp_ifp->if_hwassist; 391 } 392 cap = (cap == ~0 ? 0 : cap); 393 ena = (ena == ~0 ? 0 : ena); 394 hwa = (hwa == ~0 ? 0 : hwa); 395 396 if (sc->sc_ifp->if_capabilities != cap || 397 sc->sc_ifp->if_capenable != ena || 398 sc->sc_ifp->if_hwassist != hwa) { 399 sc->sc_ifp->if_capabilities = cap; 400 sc->sc_ifp->if_capenable = ena; 401 sc->sc_ifp->if_hwassist = hwa; 402 getmicrotime(&sc->sc_ifp->if_lastchange); 403 404 if (sc->sc_ifflags & IFF_DEBUG) 405 if_printf(sc->sc_ifp, 406 "capabilities 0x%08x enabled 0x%08x\n", cap, ena); 407 } 408 } 409 410 static void 411 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr) 412 { 413 struct lagg_softc *sc = lp->lp_softc; 414 struct ifnet *ifp = lp->lp_ifp; 415 struct lagg_llq *llq; 416 int pending = 0; 417 418 LAGG_WLOCK_ASSERT(sc); 419 420 if (lp->lp_detaching || 421 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 422 return; 423 424 /* Check to make sure its not already queued to be changed */ 425 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 426 if (llq->llq_ifp == ifp) { 427 pending = 1; 428 break; 429 } 430 } 431 432 if (!pending) { 433 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT); 434 if (llq == NULL) /* XXX what to do */ 435 return; 436 } 437 438 /* Update the lladdr even if pending, it may have changed */ 439 llq->llq_ifp = ifp; 440 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN); 441 442 if (!pending) 443 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries); 444 445 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task); 446 } 447 448 /* 449 * Set the interface MAC address from a taskqueue to avoid a LOR. 450 */ 451 static void 452 lagg_port_setlladdr(void *arg, int pending) 453 { 454 struct lagg_softc *sc = (struct lagg_softc *)arg; 455 struct lagg_llq *llq, *head; 456 struct ifnet *ifp; 457 int error; 458 459 /* Grab a local reference of the queue and remove it from the softc */ 460 LAGG_WLOCK(sc); 461 head = SLIST_FIRST(&sc->sc_llq_head); 462 SLIST_FIRST(&sc->sc_llq_head) = NULL; 463 LAGG_WUNLOCK(sc); 464 465 /* 466 * Traverse the queue and set the lladdr on each ifp. It is safe to do 467 * unlocked as we have the only reference to it. 468 */ 469 for (llq = head; llq != NULL; llq = head) { 470 ifp = llq->llq_ifp; 471 472 /* Set the link layer address */ 473 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN); 474 if (error) 475 printf("%s: setlladdr failed on %s\n", __func__, 476 ifp->if_xname); 477 478 head = SLIST_NEXT(llq, llq_entries); 479 free(llq, M_DEVBUF); 480 } 481 } 482 483 static int 484 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp) 485 { 486 struct lagg_softc *sc_ptr; 487 struct lagg_port *lp; 488 int error = 0; 489 490 LAGG_WLOCK_ASSERT(sc); 491 492 /* Limit the maximal number of lagg ports */ 493 if (sc->sc_count >= LAGG_MAX_PORTS) 494 return (ENOSPC); 495 496 /* Check if port has already been associated to a lagg */ 497 if (ifp->if_lagg != NULL) 498 return (EBUSY); 499 500 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */ 501 if (ifp->if_type != IFT_ETHER) 502 return (EPROTONOSUPPORT); 503 504 /* Allow the first Ethernet member to define the MTU */ 505 if (SLIST_EMPTY(&sc->sc_ports)) 506 sc->sc_ifp->if_mtu = ifp->if_mtu; 507 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) { 508 if_printf(sc->sc_ifp, "invalid MTU for %s\n", 509 ifp->if_xname); 510 return (EINVAL); 511 } 512 513 if ((lp = malloc(sizeof(struct lagg_port), 514 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 515 return (ENOMEM); 516 517 /* Check if port is a stacked lagg */ 518 mtx_lock(&lagg_list_mtx); 519 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) { 520 if (ifp == sc_ptr->sc_ifp) { 521 mtx_unlock(&lagg_list_mtx); 522 free(lp, M_DEVBUF); 523 return (EINVAL); 524 /* XXX disable stacking for the moment, its untested */ 525 #ifdef LAGG_PORT_STACKING 526 lp->lp_flags |= LAGG_PORT_STACK; 527 if (lagg_port_checkstacking(sc_ptr) >= 528 LAGG_MAX_STACKING) { 529 mtx_unlock(&lagg_list_mtx); 530 free(lp, M_DEVBUF); 531 return (E2BIG); 532 } 533 #endif 534 } 535 } 536 mtx_unlock(&lagg_list_mtx); 537 538 /* Change the interface type */ 539 lp->lp_iftype = ifp->if_type; 540 ifp->if_type = IFT_IEEE8023ADLAG; 541 ifp->if_lagg = lp; 542 lp->lp_ioctl = ifp->if_ioctl; 543 ifp->if_ioctl = lagg_port_ioctl; 544 lp->lp_output = ifp->if_output; 545 ifp->if_output = lagg_port_output; 546 547 lp->lp_ifp = ifp; 548 lp->lp_softc = sc; 549 550 /* Save port link layer address */ 551 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN); 552 553 if (SLIST_EMPTY(&sc->sc_ports)) { 554 sc->sc_primary = lp; 555 lagg_lladdr(sc, IF_LLADDR(ifp)); 556 } else { 557 /* Update link layer address for this port */ 558 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp)); 559 } 560 561 /* Insert into the list of ports */ 562 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries); 563 sc->sc_count++; 564 565 /* Update lagg capabilities */ 566 lagg_capabilities(sc); 567 lagg_linkstate(sc); 568 569 /* Add multicast addresses and interface flags to this port */ 570 lagg_ether_cmdmulti(lp, 1); 571 lagg_setflags(lp, 1); 572 573 if (sc->sc_port_create != NULL) 574 error = (*sc->sc_port_create)(lp); 575 if (error) { 576 /* remove the port again, without calling sc_port_destroy */ 577 lagg_port_destroy(lp, 0); 578 return (error); 579 } 580 581 return (error); 582 } 583 584 #ifdef LAGG_PORT_STACKING 585 static int 586 lagg_port_checkstacking(struct lagg_softc *sc) 587 { 588 struct lagg_softc *sc_ptr; 589 struct lagg_port *lp; 590 int m = 0; 591 592 LAGG_WLOCK_ASSERT(sc); 593 594 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 595 if (lp->lp_flags & LAGG_PORT_STACK) { 596 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc; 597 m = MAX(m, lagg_port_checkstacking(sc_ptr)); 598 } 599 } 600 601 return (m + 1); 602 } 603 #endif 604 605 static int 606 lagg_port_destroy(struct lagg_port *lp, int runpd) 607 { 608 struct lagg_softc *sc = lp->lp_softc; 609 struct lagg_port *lp_ptr; 610 struct lagg_llq *llq; 611 struct ifnet *ifp = lp->lp_ifp; 612 613 LAGG_WLOCK_ASSERT(sc); 614 615 if (runpd && sc->sc_port_destroy != NULL) 616 (*sc->sc_port_destroy)(lp); 617 618 /* 619 * Remove multicast addresses and interface flags from this port and 620 * reset the MAC address, skip if the interface is being detached. 621 */ 622 if (!lp->lp_detaching) { 623 lagg_ether_cmdmulti(lp, 0); 624 lagg_setflags(lp, 0); 625 lagg_port_lladdr(lp, lp->lp_lladdr); 626 } 627 628 /* Restore interface */ 629 ifp->if_type = lp->lp_iftype; 630 ifp->if_ioctl = lp->lp_ioctl; 631 ifp->if_output = lp->lp_output; 632 ifp->if_lagg = NULL; 633 634 /* Finally, remove the port from the lagg */ 635 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries); 636 sc->sc_count--; 637 638 /* Update the primary interface */ 639 if (lp == sc->sc_primary) { 640 uint8_t lladdr[ETHER_ADDR_LEN]; 641 642 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) { 643 bzero(&lladdr, ETHER_ADDR_LEN); 644 } else { 645 bcopy(lp_ptr->lp_lladdr, 646 lladdr, ETHER_ADDR_LEN); 647 } 648 lagg_lladdr(sc, lladdr); 649 sc->sc_primary = lp_ptr; 650 651 /* Update link layer address for each port */ 652 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries) 653 lagg_port_lladdr(lp_ptr, lladdr); 654 } 655 656 /* Remove any pending lladdr changes from the queue */ 657 if (lp->lp_detaching) { 658 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 659 if (llq->llq_ifp == ifp) { 660 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq, 661 llq_entries); 662 free(llq, M_DEVBUF); 663 break; /* Only appears once */ 664 } 665 } 666 } 667 668 if (lp->lp_ifflags) 669 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__); 670 671 free(lp, M_DEVBUF); 672 673 /* Update lagg capabilities */ 674 lagg_capabilities(sc); 675 lagg_linkstate(sc); 676 677 return (0); 678 } 679 680 static int 681 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 682 { 683 struct lagg_reqport *rp = (struct lagg_reqport *)data; 684 struct lagg_softc *sc; 685 struct lagg_port *lp = NULL; 686 int error = 0; 687 688 /* Should be checked by the caller */ 689 if (ifp->if_type != IFT_IEEE8023ADLAG || 690 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL) 691 goto fallback; 692 693 switch (cmd) { 694 case SIOCGLAGGPORT: 695 if (rp->rp_portname[0] == '\0' || 696 ifunit(rp->rp_portname) != ifp) { 697 error = EINVAL; 698 break; 699 } 700 701 LAGG_RLOCK(sc); 702 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) { 703 error = ENOENT; 704 LAGG_RUNLOCK(sc); 705 break; 706 } 707 708 lagg_port2req(lp, rp); 709 LAGG_RUNLOCK(sc); 710 break; 711 712 case SIOCSIFCAP: 713 if (lp->lp_ioctl == NULL) { 714 error = EINVAL; 715 break; 716 } 717 error = (*lp->lp_ioctl)(ifp, cmd, data); 718 if (error) 719 break; 720 721 /* Update lagg interface capabilities */ 722 LAGG_WLOCK(sc); 723 lagg_capabilities(sc); 724 LAGG_WUNLOCK(sc); 725 break; 726 727 case SIOCSIFMTU: 728 /* Do not allow the MTU to be changed once joined */ 729 error = EINVAL; 730 break; 731 732 default: 733 goto fallback; 734 } 735 736 return (error); 737 738 fallback: 739 if (lp->lp_ioctl != NULL) 740 return ((*lp->lp_ioctl)(ifp, cmd, data)); 741 742 return (EINVAL); 743 } 744 745 static int 746 lagg_port_output(struct ifnet *ifp, struct mbuf *m, 747 struct sockaddr *dst, struct route *ro) 748 { 749 struct lagg_port *lp = ifp->if_lagg; 750 struct ether_header *eh; 751 short type = 0; 752 753 switch (dst->sa_family) { 754 case pseudo_AF_HDRCMPLT: 755 case AF_UNSPEC: 756 eh = (struct ether_header *)dst->sa_data; 757 type = eh->ether_type; 758 break; 759 } 760 761 /* 762 * Only allow ethernet types required to initiate or maintain the link, 763 * aggregated frames take a different path. 764 */ 765 switch (ntohs(type)) { 766 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */ 767 return ((*lp->lp_output)(ifp, m, dst, ro)); 768 } 769 770 /* drop any other frames */ 771 m_freem(m); 772 return (EBUSY); 773 } 774 775 static void 776 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp) 777 { 778 struct lagg_port *lp; 779 struct lagg_softc *sc; 780 781 if ((lp = ifp->if_lagg) == NULL) 782 return; 783 784 sc = lp->lp_softc; 785 786 LAGG_WLOCK(sc); 787 lp->lp_detaching = 1; 788 lagg_port_destroy(lp, 1); 789 LAGG_WUNLOCK(sc); 790 } 791 792 static void 793 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp) 794 { 795 struct lagg_softc *sc = lp->lp_softc; 796 797 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname)); 798 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname)); 799 rp->rp_prio = lp->lp_prio; 800 rp->rp_flags = lp->lp_flags; 801 if (sc->sc_portreq != NULL) 802 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc); 803 804 /* Add protocol specific flags */ 805 switch (sc->sc_proto) { 806 case LAGG_PROTO_FAILOVER: 807 if (lp == sc->sc_primary) 808 rp->rp_flags |= LAGG_PORT_MASTER; 809 if (lp == lagg_link_active(sc, sc->sc_primary)) 810 rp->rp_flags |= LAGG_PORT_ACTIVE; 811 break; 812 813 case LAGG_PROTO_ROUNDROBIN: 814 case LAGG_PROTO_LOADBALANCE: 815 case LAGG_PROTO_ETHERCHANNEL: 816 if (LAGG_PORTACTIVE(lp)) 817 rp->rp_flags |= LAGG_PORT_ACTIVE; 818 break; 819 820 case LAGG_PROTO_LACP: 821 /* LACP has a different definition of active */ 822 if (lacp_isactive(lp)) 823 rp->rp_flags |= LAGG_PORT_ACTIVE; 824 if (lacp_iscollecting(lp)) 825 rp->rp_flags |= LAGG_PORT_COLLECTING; 826 if (lacp_isdistributing(lp)) 827 rp->rp_flags |= LAGG_PORT_DISTRIBUTING; 828 break; 829 } 830 831 } 832 833 static void 834 lagg_init(void *xsc) 835 { 836 struct lagg_softc *sc = (struct lagg_softc *)xsc; 837 struct lagg_port *lp; 838 struct ifnet *ifp = sc->sc_ifp; 839 840 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 841 return; 842 843 LAGG_WLOCK(sc); 844 845 ifp->if_drv_flags |= IFF_DRV_RUNNING; 846 /* Update the port lladdrs */ 847 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 848 lagg_port_lladdr(lp, IF_LLADDR(ifp)); 849 850 if (sc->sc_init != NULL) 851 (*sc->sc_init)(sc); 852 853 LAGG_WUNLOCK(sc); 854 } 855 856 static void 857 lagg_stop(struct lagg_softc *sc) 858 { 859 struct ifnet *ifp = sc->sc_ifp; 860 861 LAGG_WLOCK_ASSERT(sc); 862 863 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 864 return; 865 866 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 867 868 if (sc->sc_stop != NULL) 869 (*sc->sc_stop)(sc); 870 } 871 872 static int 873 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 874 { 875 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 876 struct lagg_reqall *ra = (struct lagg_reqall *)data; 877 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf; 878 struct ifreq *ifr = (struct ifreq *)data; 879 struct lagg_port *lp; 880 struct ifnet *tpif; 881 struct thread *td = curthread; 882 char *buf, *outbuf; 883 int count, buflen, len, error = 0; 884 885 bzero(&rpbuf, sizeof(rpbuf)); 886 887 switch (cmd) { 888 case SIOCGLAGG: 889 LAGG_RLOCK(sc); 890 count = 0; 891 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 892 count++; 893 buflen = count * sizeof(struct lagg_reqport); 894 LAGG_RUNLOCK(sc); 895 896 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 897 898 LAGG_RLOCK(sc); 899 ra->ra_proto = sc->sc_proto; 900 if (sc->sc_req != NULL) 901 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc); 902 903 count = 0; 904 buf = outbuf; 905 len = min(ra->ra_size, buflen); 906 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 907 if (len < sizeof(rpbuf)) 908 break; 909 910 lagg_port2req(lp, &rpbuf); 911 memcpy(buf, &rpbuf, sizeof(rpbuf)); 912 count++; 913 buf += sizeof(rpbuf); 914 len -= sizeof(rpbuf); 915 } 916 LAGG_RUNLOCK(sc); 917 ra->ra_ports = count; 918 ra->ra_size = count * sizeof(rpbuf); 919 error = copyout(outbuf, ra->ra_port, ra->ra_size); 920 free(outbuf, M_TEMP); 921 break; 922 case SIOCSLAGG: 923 error = priv_check(td, PRIV_NET_LAGG); 924 if (error) 925 break; 926 if (ra->ra_proto >= LAGG_PROTO_MAX) { 927 error = EPROTONOSUPPORT; 928 break; 929 } 930 if (sc->sc_proto != LAGG_PROTO_NONE) { 931 LAGG_WLOCK(sc); 932 error = sc->sc_detach(sc); 933 /* Reset protocol and pointers */ 934 sc->sc_proto = LAGG_PROTO_NONE; 935 sc->sc_detach = NULL; 936 sc->sc_start = NULL; 937 sc->sc_input = NULL; 938 sc->sc_port_create = NULL; 939 sc->sc_port_destroy = NULL; 940 sc->sc_linkstate = NULL; 941 sc->sc_init = NULL; 942 sc->sc_stop = NULL; 943 sc->sc_lladdr = NULL; 944 sc->sc_req = NULL; 945 sc->sc_portreq = NULL; 946 LAGG_WUNLOCK(sc); 947 } 948 if (error != 0) 949 break; 950 for (int i = 0; i < (sizeof(lagg_protos) / 951 sizeof(lagg_protos[0])); i++) { 952 if (lagg_protos[i].ti_proto == ra->ra_proto) { 953 if (sc->sc_ifflags & IFF_DEBUG) 954 printf("%s: using proto %u\n", 955 sc->sc_ifname, 956 lagg_protos[i].ti_proto); 957 LAGG_WLOCK(sc); 958 sc->sc_proto = lagg_protos[i].ti_proto; 959 if (sc->sc_proto != LAGG_PROTO_NONE) 960 error = lagg_protos[i].ti_attach(sc); 961 LAGG_WUNLOCK(sc); 962 return (error); 963 } 964 } 965 error = EPROTONOSUPPORT; 966 break; 967 case SIOCGLAGGPORT: 968 if (rp->rp_portname[0] == '\0' || 969 (tpif = ifunit(rp->rp_portname)) == NULL) { 970 error = EINVAL; 971 break; 972 } 973 974 LAGG_RLOCK(sc); 975 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 976 lp->lp_softc != sc) { 977 error = ENOENT; 978 LAGG_RUNLOCK(sc); 979 break; 980 } 981 982 lagg_port2req(lp, rp); 983 LAGG_RUNLOCK(sc); 984 break; 985 case SIOCSLAGGPORT: 986 error = priv_check(td, PRIV_NET_LAGG); 987 if (error) 988 break; 989 if (rp->rp_portname[0] == '\0' || 990 (tpif = ifunit(rp->rp_portname)) == NULL) { 991 error = EINVAL; 992 break; 993 } 994 LAGG_WLOCK(sc); 995 error = lagg_port_create(sc, tpif); 996 LAGG_WUNLOCK(sc); 997 break; 998 case SIOCSLAGGDELPORT: 999 error = priv_check(td, PRIV_NET_LAGG); 1000 if (error) 1001 break; 1002 if (rp->rp_portname[0] == '\0' || 1003 (tpif = ifunit(rp->rp_portname)) == NULL) { 1004 error = EINVAL; 1005 break; 1006 } 1007 1008 LAGG_WLOCK(sc); 1009 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1010 lp->lp_softc != sc) { 1011 error = ENOENT; 1012 LAGG_WUNLOCK(sc); 1013 break; 1014 } 1015 1016 error = lagg_port_destroy(lp, 1); 1017 LAGG_WUNLOCK(sc); 1018 break; 1019 case SIOCSIFFLAGS: 1020 /* Set flags on ports too */ 1021 LAGG_WLOCK(sc); 1022 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1023 lagg_setflags(lp, 1); 1024 } 1025 LAGG_WUNLOCK(sc); 1026 1027 if (!(ifp->if_flags & IFF_UP) && 1028 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1029 /* 1030 * If interface is marked down and it is running, 1031 * then stop and disable it. 1032 */ 1033 LAGG_WLOCK(sc); 1034 lagg_stop(sc); 1035 LAGG_WUNLOCK(sc); 1036 } else if ((ifp->if_flags & IFF_UP) && 1037 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1038 /* 1039 * If interface is marked up and it is stopped, then 1040 * start it. 1041 */ 1042 (*ifp->if_init)(sc); 1043 } 1044 break; 1045 case SIOCADDMULTI: 1046 case SIOCDELMULTI: 1047 LAGG_WLOCK(sc); 1048 error = lagg_ether_setmulti(sc); 1049 LAGG_WUNLOCK(sc); 1050 break; 1051 case SIOCSIFMEDIA: 1052 case SIOCGIFMEDIA: 1053 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1054 break; 1055 1056 case SIOCSIFCAP: 1057 case SIOCSIFMTU: 1058 /* Do not allow the MTU or caps to be directly changed */ 1059 error = EINVAL; 1060 break; 1061 1062 default: 1063 error = ether_ioctl(ifp, cmd, data); 1064 break; 1065 } 1066 return (error); 1067 } 1068 1069 static int 1070 lagg_ether_setmulti(struct lagg_softc *sc) 1071 { 1072 struct lagg_port *lp; 1073 1074 LAGG_WLOCK_ASSERT(sc); 1075 1076 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1077 /* First, remove any existing filter entries. */ 1078 lagg_ether_cmdmulti(lp, 0); 1079 /* copy all addresses from the lagg interface to the port */ 1080 lagg_ether_cmdmulti(lp, 1); 1081 } 1082 return (0); 1083 } 1084 1085 static int 1086 lagg_ether_cmdmulti(struct lagg_port *lp, int set) 1087 { 1088 struct lagg_softc *sc = lp->lp_softc; 1089 struct ifnet *ifp = lp->lp_ifp; 1090 struct ifnet *scifp = sc->sc_ifp; 1091 struct lagg_mc *mc; 1092 struct ifmultiaddr *ifma, *rifma = NULL; 1093 struct sockaddr_dl sdl; 1094 int error; 1095 1096 LAGG_WLOCK_ASSERT(sc); 1097 1098 bzero((char *)&sdl, sizeof(sdl)); 1099 sdl.sdl_len = sizeof(sdl); 1100 sdl.sdl_family = AF_LINK; 1101 sdl.sdl_type = IFT_ETHER; 1102 sdl.sdl_alen = ETHER_ADDR_LEN; 1103 sdl.sdl_index = ifp->if_index; 1104 1105 if (set) { 1106 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) { 1107 if (ifma->ifma_addr->sa_family != AF_LINK) 1108 continue; 1109 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 1110 LLADDR(&sdl), ETHER_ADDR_LEN); 1111 1112 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma); 1113 if (error) 1114 return (error); 1115 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT); 1116 if (mc == NULL) 1117 return (ENOMEM); 1118 mc->mc_ifma = rifma; 1119 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries); 1120 } 1121 } else { 1122 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) { 1123 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries); 1124 if_delmulti_ifma(mc->mc_ifma); 1125 free(mc, M_DEVBUF); 1126 } 1127 } 1128 return (0); 1129 } 1130 1131 /* Handle a ref counted flag that should be set on the lagg port as well */ 1132 static int 1133 lagg_setflag(struct lagg_port *lp, int flag, int status, 1134 int (*func)(struct ifnet *, int)) 1135 { 1136 struct lagg_softc *sc = lp->lp_softc; 1137 struct ifnet *scifp = sc->sc_ifp; 1138 struct ifnet *ifp = lp->lp_ifp; 1139 int error; 1140 1141 LAGG_WLOCK_ASSERT(sc); 1142 1143 status = status ? (scifp->if_flags & flag) : 0; 1144 /* Now "status" contains the flag value or 0 */ 1145 1146 /* 1147 * See if recorded ports status is different from what 1148 * we want it to be. If it is, flip it. We record ports 1149 * status in lp_ifflags so that we won't clear ports flag 1150 * we haven't set. In fact, we don't clear or set ports 1151 * flags directly, but get or release references to them. 1152 * That's why we can be sure that recorded flags still are 1153 * in accord with actual ports flags. 1154 */ 1155 if (status != (lp->lp_ifflags & flag)) { 1156 error = (*func)(ifp, status); 1157 if (error) 1158 return (error); 1159 lp->lp_ifflags &= ~flag; 1160 lp->lp_ifflags |= status; 1161 } 1162 return (0); 1163 } 1164 1165 /* 1166 * Handle IFF_* flags that require certain changes on the lagg port 1167 * if "status" is true, update ports flags respective to the lagg 1168 * if "status" is false, forcedly clear the flags set on port. 1169 */ 1170 static int 1171 lagg_setflags(struct lagg_port *lp, int status) 1172 { 1173 int error, i; 1174 1175 for (i = 0; lagg_pflags[i].flag; i++) { 1176 error = lagg_setflag(lp, lagg_pflags[i].flag, 1177 status, lagg_pflags[i].func); 1178 if (error) 1179 return (error); 1180 } 1181 return (0); 1182 } 1183 1184 static void 1185 lagg_start(struct ifnet *ifp) 1186 { 1187 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1188 struct mbuf *m; 1189 int error = 0; 1190 1191 LAGG_RLOCK(sc); 1192 /* We need a Tx algorithm and at least one port */ 1193 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) { 1194 IF_DRAIN(&ifp->if_snd); 1195 LAGG_RUNLOCK(sc); 1196 return; 1197 } 1198 1199 for (;; error = 0) { 1200 IFQ_DEQUEUE(&ifp->if_snd, m); 1201 if (m == NULL) 1202 break; 1203 1204 ETHER_BPF_MTAP(ifp, m); 1205 1206 error = (*sc->sc_start)(sc, m); 1207 if (error == 0) 1208 ifp->if_opackets++; 1209 else 1210 ifp->if_oerrors++; 1211 } 1212 LAGG_RUNLOCK(sc); 1213 } 1214 1215 static struct mbuf * 1216 lagg_input(struct ifnet *ifp, struct mbuf *m) 1217 { 1218 struct lagg_port *lp = ifp->if_lagg; 1219 struct lagg_softc *sc = lp->lp_softc; 1220 struct ifnet *scifp = sc->sc_ifp; 1221 1222 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1223 (lp->lp_flags & LAGG_PORT_DISABLED) || 1224 sc->sc_proto == LAGG_PROTO_NONE) { 1225 m_freem(m); 1226 return (NULL); 1227 } 1228 1229 LAGG_RLOCK(sc); 1230 ETHER_BPF_MTAP(scifp, m); 1231 1232 m = (*sc->sc_input)(sc, lp, m); 1233 1234 if (m != NULL) { 1235 scifp->if_ipackets++; 1236 scifp->if_ibytes += m->m_pkthdr.len; 1237 1238 if (scifp->if_flags & IFF_MONITOR) { 1239 m_freem(m); 1240 m = NULL; 1241 } 1242 } 1243 1244 LAGG_RUNLOCK(sc); 1245 return (m); 1246 } 1247 1248 static int 1249 lagg_media_change(struct ifnet *ifp) 1250 { 1251 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1252 1253 if (sc->sc_ifflags & IFF_DEBUG) 1254 printf("%s\n", __func__); 1255 1256 /* Ignore */ 1257 return (0); 1258 } 1259 1260 static void 1261 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1262 { 1263 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1264 struct lagg_port *lp; 1265 1266 imr->ifm_status = IFM_AVALID; 1267 imr->ifm_active = IFM_ETHER | IFM_AUTO; 1268 1269 LAGG_RLOCK(sc); 1270 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1271 if (LAGG_PORTACTIVE(lp)) 1272 imr->ifm_status |= IFM_ACTIVE; 1273 } 1274 LAGG_RUNLOCK(sc); 1275 } 1276 1277 static void 1278 lagg_linkstate(struct lagg_softc *sc) 1279 { 1280 struct lagg_port *lp; 1281 int new_link = LINK_STATE_DOWN; 1282 uint64_t speed; 1283 1284 /* Our link is considered up if at least one of our ports is active */ 1285 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1286 if (lp->lp_link_state == LINK_STATE_UP) { 1287 new_link = LINK_STATE_UP; 1288 break; 1289 } 1290 } 1291 if_link_state_change(sc->sc_ifp, new_link); 1292 1293 /* Update if_baudrate to reflect the max possible speed */ 1294 switch (sc->sc_proto) { 1295 case LAGG_PROTO_FAILOVER: 1296 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ? 1297 sc->sc_primary->lp_ifp->if_baudrate : 0; 1298 break; 1299 case LAGG_PROTO_ROUNDROBIN: 1300 case LAGG_PROTO_LOADBALANCE: 1301 case LAGG_PROTO_ETHERCHANNEL: 1302 speed = 0; 1303 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1304 speed += lp->lp_ifp->if_baudrate; 1305 sc->sc_ifp->if_baudrate = speed; 1306 break; 1307 case LAGG_PROTO_LACP: 1308 /* LACP updates if_baudrate itself */ 1309 break; 1310 } 1311 } 1312 1313 static void 1314 lagg_port_state(struct ifnet *ifp, int state) 1315 { 1316 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg; 1317 struct lagg_softc *sc = NULL; 1318 1319 if (lp != NULL) 1320 sc = lp->lp_softc; 1321 if (sc == NULL) 1322 return; 1323 1324 LAGG_WLOCK(sc); 1325 lagg_linkstate(sc); 1326 if (sc->sc_linkstate != NULL) 1327 (*sc->sc_linkstate)(lp); 1328 LAGG_WUNLOCK(sc); 1329 } 1330 1331 struct lagg_port * 1332 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp) 1333 { 1334 struct lagg_port *lp_next, *rval = NULL; 1335 // int new_link = LINK_STATE_DOWN; 1336 1337 LAGG_RLOCK_ASSERT(sc); 1338 /* 1339 * Search a port which reports an active link state. 1340 */ 1341 1342 if (lp == NULL) 1343 goto search; 1344 if (LAGG_PORTACTIVE(lp)) { 1345 rval = lp; 1346 goto found; 1347 } 1348 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL && 1349 LAGG_PORTACTIVE(lp_next)) { 1350 rval = lp_next; 1351 goto found; 1352 } 1353 1354 search: 1355 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1356 if (LAGG_PORTACTIVE(lp_next)) { 1357 rval = lp_next; 1358 goto found; 1359 } 1360 } 1361 1362 found: 1363 if (rval != NULL) { 1364 /* 1365 * The IEEE 802.1D standard assumes that a lagg with 1366 * multiple ports is always full duplex. This is valid 1367 * for load sharing laggs and if at least two links 1368 * are active. Unfortunately, checking the latter would 1369 * be too expensive at this point. 1370 XXX 1371 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) && 1372 (sc->sc_count > 1)) 1373 new_link = LINK_STATE_FULL_DUPLEX; 1374 else 1375 new_link = rval->lp_link_state; 1376 */ 1377 } 1378 1379 return (rval); 1380 } 1381 1382 static const void * 1383 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf) 1384 { 1385 if (m->m_pkthdr.len < (off + len)) { 1386 return (NULL); 1387 } else if (m->m_len < (off + len)) { 1388 m_copydata(m, off, len, buf); 1389 return (buf); 1390 } 1391 return (mtod(m, char *) + off); 1392 } 1393 1394 uint32_t 1395 lagg_hashmbuf(struct mbuf *m, uint32_t key) 1396 { 1397 uint16_t etype; 1398 uint32_t p = 0; 1399 int off; 1400 struct ether_header *eh; 1401 struct ether_vlan_header vlanbuf; 1402 const struct ether_vlan_header *vlan; 1403 #ifdef INET 1404 const struct ip *ip; 1405 struct ip ipbuf; 1406 #endif 1407 #ifdef INET6 1408 const struct ip6_hdr *ip6; 1409 struct ip6_hdr ip6buf; 1410 uint32_t flow; 1411 #endif 1412 1413 off = sizeof(*eh); 1414 if (m->m_len < off) 1415 goto out; 1416 eh = mtod(m, struct ether_header *); 1417 etype = ntohs(eh->ether_type); 1418 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key); 1419 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p); 1420 1421 /* Special handling for encapsulating VLAN frames */ 1422 if (m->m_flags & M_VLANTAG) { 1423 p = hash32_buf(&m->m_pkthdr.ether_vtag, 1424 sizeof(m->m_pkthdr.ether_vtag), p); 1425 } else if (etype == ETHERTYPE_VLAN) { 1426 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf); 1427 if (vlan == NULL) 1428 goto out; 1429 1430 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p); 1431 etype = ntohs(vlan->evl_proto); 1432 off += sizeof(*vlan) - sizeof(*eh); 1433 } 1434 1435 switch (etype) { 1436 #ifdef INET 1437 case ETHERTYPE_IP: 1438 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf); 1439 if (ip == NULL) 1440 goto out; 1441 1442 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p); 1443 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p); 1444 break; 1445 #endif 1446 #ifdef INET6 1447 case ETHERTYPE_IPV6: 1448 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf); 1449 if (ip6 == NULL) 1450 goto out; 1451 1452 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p); 1453 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p); 1454 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK; 1455 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */ 1456 break; 1457 #endif 1458 } 1459 out: 1460 return (p); 1461 } 1462 1463 int 1464 lagg_enqueue(struct ifnet *ifp, struct mbuf *m) 1465 { 1466 1467 return (ifp->if_transmit)(ifp, m); 1468 } 1469 1470 /* 1471 * Simple round robin aggregation 1472 */ 1473 1474 static int 1475 lagg_rr_attach(struct lagg_softc *sc) 1476 { 1477 sc->sc_detach = lagg_rr_detach; 1478 sc->sc_start = lagg_rr_start; 1479 sc->sc_input = lagg_rr_input; 1480 sc->sc_port_create = NULL; 1481 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1482 sc->sc_seq = 0; 1483 1484 return (0); 1485 } 1486 1487 static int 1488 lagg_rr_detach(struct lagg_softc *sc) 1489 { 1490 return (0); 1491 } 1492 1493 static int 1494 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m) 1495 { 1496 struct lagg_port *lp; 1497 uint32_t p; 1498 1499 p = atomic_fetchadd_32(&sc->sc_seq, 1); 1500 p %= sc->sc_count; 1501 lp = SLIST_FIRST(&sc->sc_ports); 1502 while (p--) 1503 lp = SLIST_NEXT(lp, lp_entries); 1504 1505 /* 1506 * Check the port's link state. This will return the next active 1507 * port if the link is down or the port is NULL. 1508 */ 1509 if ((lp = lagg_link_active(sc, lp)) == NULL) { 1510 m_freem(m); 1511 return (ENOENT); 1512 } 1513 1514 /* Send mbuf */ 1515 return (lagg_enqueue(lp->lp_ifp, m)); 1516 } 1517 1518 static struct mbuf * 1519 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1520 { 1521 struct ifnet *ifp = sc->sc_ifp; 1522 1523 /* Just pass in the packet to our lagg device */ 1524 m->m_pkthdr.rcvif = ifp; 1525 1526 return (m); 1527 } 1528 1529 /* 1530 * Active failover 1531 */ 1532 1533 static int 1534 lagg_fail_attach(struct lagg_softc *sc) 1535 { 1536 sc->sc_detach = lagg_fail_detach; 1537 sc->sc_start = lagg_fail_start; 1538 sc->sc_input = lagg_fail_input; 1539 sc->sc_port_create = NULL; 1540 sc->sc_port_destroy = NULL; 1541 1542 return (0); 1543 } 1544 1545 static int 1546 lagg_fail_detach(struct lagg_softc *sc) 1547 { 1548 return (0); 1549 } 1550 1551 static int 1552 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m) 1553 { 1554 struct lagg_port *lp; 1555 1556 /* Use the master port if active or the next available port */ 1557 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) { 1558 m_freem(m); 1559 return (ENOENT); 1560 } 1561 1562 /* Send mbuf */ 1563 return (lagg_enqueue(lp->lp_ifp, m)); 1564 } 1565 1566 static struct mbuf * 1567 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1568 { 1569 struct ifnet *ifp = sc->sc_ifp; 1570 struct lagg_port *tmp_tp; 1571 1572 if (lp == sc->sc_primary || lagg_failover_rx_all) { 1573 m->m_pkthdr.rcvif = ifp; 1574 return (m); 1575 } 1576 1577 if (!LAGG_PORTACTIVE(sc->sc_primary)) { 1578 tmp_tp = lagg_link_active(sc, sc->sc_primary); 1579 /* 1580 * If tmp_tp is null, we've recieved a packet when all 1581 * our links are down. Weird, but process it anyways. 1582 */ 1583 if ((tmp_tp == NULL || tmp_tp == lp)) { 1584 m->m_pkthdr.rcvif = ifp; 1585 return (m); 1586 } 1587 } 1588 1589 m_freem(m); 1590 return (NULL); 1591 } 1592 1593 /* 1594 * Loadbalancing 1595 */ 1596 1597 static int 1598 lagg_lb_attach(struct lagg_softc *sc) 1599 { 1600 struct lagg_port *lp; 1601 struct lagg_lb *lb; 1602 1603 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb), 1604 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 1605 return (ENOMEM); 1606 1607 sc->sc_detach = lagg_lb_detach; 1608 sc->sc_start = lagg_lb_start; 1609 sc->sc_input = lagg_lb_input; 1610 sc->sc_port_create = lagg_lb_port_create; 1611 sc->sc_port_destroy = lagg_lb_port_destroy; 1612 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1613 1614 lb->lb_key = arc4random(); 1615 sc->sc_psc = (caddr_t)lb; 1616 1617 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1618 lagg_lb_port_create(lp); 1619 1620 return (0); 1621 } 1622 1623 static int 1624 lagg_lb_detach(struct lagg_softc *sc) 1625 { 1626 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1627 if (lb != NULL) 1628 free(lb, M_DEVBUF); 1629 return (0); 1630 } 1631 1632 static int 1633 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp) 1634 { 1635 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1636 struct lagg_port *lp_next; 1637 int i = 0; 1638 1639 bzero(&lb->lb_ports, sizeof(lb->lb_ports)); 1640 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1641 if (lp_next == lp) 1642 continue; 1643 if (i >= LAGG_MAX_PORTS) 1644 return (EINVAL); 1645 if (sc->sc_ifflags & IFF_DEBUG) 1646 printf("%s: port %s at index %d\n", 1647 sc->sc_ifname, lp_next->lp_ifname, i); 1648 lb->lb_ports[i++] = lp_next; 1649 } 1650 1651 return (0); 1652 } 1653 1654 static int 1655 lagg_lb_port_create(struct lagg_port *lp) 1656 { 1657 struct lagg_softc *sc = lp->lp_softc; 1658 return (lagg_lb_porttable(sc, NULL)); 1659 } 1660 1661 static void 1662 lagg_lb_port_destroy(struct lagg_port *lp) 1663 { 1664 struct lagg_softc *sc = lp->lp_softc; 1665 lagg_lb_porttable(sc, lp); 1666 } 1667 1668 static int 1669 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m) 1670 { 1671 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1672 struct lagg_port *lp = NULL; 1673 uint32_t p = 0; 1674 1675 if (m->m_flags & M_FLOWID) 1676 p = m->m_pkthdr.flowid; 1677 else 1678 p = lagg_hashmbuf(m, lb->lb_key); 1679 p %= sc->sc_count; 1680 lp = lb->lb_ports[p]; 1681 1682 /* 1683 * Check the port's link state. This will return the next active 1684 * port if the link is down or the port is NULL. 1685 */ 1686 if ((lp = lagg_link_active(sc, lp)) == NULL) { 1687 m_freem(m); 1688 return (ENOENT); 1689 } 1690 1691 /* Send mbuf */ 1692 return (lagg_enqueue(lp->lp_ifp, m)); 1693 } 1694 1695 static struct mbuf * 1696 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1697 { 1698 struct ifnet *ifp = sc->sc_ifp; 1699 1700 /* Just pass in the packet to our lagg device */ 1701 m->m_pkthdr.rcvif = ifp; 1702 1703 return (m); 1704 } 1705 1706 /* 1707 * 802.3ad LACP 1708 */ 1709 1710 static int 1711 lagg_lacp_attach(struct lagg_softc *sc) 1712 { 1713 struct lagg_port *lp; 1714 int error; 1715 1716 sc->sc_detach = lagg_lacp_detach; 1717 sc->sc_port_create = lacp_port_create; 1718 sc->sc_port_destroy = lacp_port_destroy; 1719 sc->sc_linkstate = lacp_linkstate; 1720 sc->sc_start = lagg_lacp_start; 1721 sc->sc_input = lagg_lacp_input; 1722 sc->sc_init = lacp_init; 1723 sc->sc_stop = lacp_stop; 1724 sc->sc_lladdr = lagg_lacp_lladdr; 1725 sc->sc_req = lacp_req; 1726 sc->sc_portreq = lacp_portreq; 1727 1728 error = lacp_attach(sc); 1729 if (error) 1730 return (error); 1731 1732 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1733 lacp_port_create(lp); 1734 1735 return (error); 1736 } 1737 1738 static int 1739 lagg_lacp_detach(struct lagg_softc *sc) 1740 { 1741 struct lagg_port *lp; 1742 int error; 1743 1744 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1745 lacp_port_destroy(lp); 1746 1747 /* unlocking is safe here */ 1748 LAGG_WUNLOCK(sc); 1749 error = lacp_detach(sc); 1750 LAGG_WLOCK(sc); 1751 1752 return (error); 1753 } 1754 1755 static void 1756 lagg_lacp_lladdr(struct lagg_softc *sc) 1757 { 1758 struct lagg_port *lp; 1759 1760 /* purge all the lacp ports */ 1761 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1762 lacp_port_destroy(lp); 1763 1764 /* add them back in */ 1765 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1766 lacp_port_create(lp); 1767 } 1768 1769 static int 1770 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m) 1771 { 1772 struct lagg_port *lp; 1773 1774 lp = lacp_select_tx_port(sc, m); 1775 if (lp == NULL) { 1776 m_freem(m); 1777 return (EBUSY); 1778 } 1779 1780 /* Send mbuf */ 1781 return (lagg_enqueue(lp->lp_ifp, m)); 1782 } 1783 1784 static struct mbuf * 1785 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1786 { 1787 struct ifnet *ifp = sc->sc_ifp; 1788 struct ether_header *eh; 1789 u_short etype; 1790 1791 eh = mtod(m, struct ether_header *); 1792 etype = ntohs(eh->ether_type); 1793 1794 /* Tap off LACP control messages */ 1795 if (etype == ETHERTYPE_SLOW) { 1796 m = lacp_input(lp, m); 1797 if (m == NULL) 1798 return (NULL); 1799 } 1800 1801 /* 1802 * If the port is not collecting or not in the active aggregator then 1803 * free and return. 1804 */ 1805 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) { 1806 m_freem(m); 1807 return (NULL); 1808 } 1809 1810 m->m_pkthdr.rcvif = ifp; 1811 return (m); 1812 } 1813