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