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 int 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_port_state(struct ifnet *, int); 92 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t); 93 static int lagg_port_output(struct ifnet *, struct mbuf *, 94 struct sockaddr *, struct rtentry *); 95 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *); 96 static int lagg_port_checkstacking(struct lagg_softc *); 97 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *); 98 static void lagg_init(void *); 99 static void lagg_stop(struct lagg_softc *); 100 static int lagg_ioctl(struct ifnet *, u_long, caddr_t); 101 static int lagg_ether_setmulti(struct lagg_softc *); 102 static int lagg_ether_cmdmulti(struct lagg_port *, int); 103 static int lagg_setflag(struct lagg_port *, int, int, 104 int (*func)(struct ifnet *, int)); 105 static int lagg_setflags(struct lagg_port *, int status); 106 static void lagg_start(struct ifnet *); 107 static int lagg_media_change(struct ifnet *); 108 static void lagg_media_status(struct ifnet *, struct ifmediareq *); 109 static struct lagg_port *lagg_link_active(struct lagg_softc *, 110 struct lagg_port *); 111 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *); 112 113 IFC_SIMPLE_DECLARE(lagg, 0); 114 115 /* Simple round robin */ 116 static int lagg_rr_attach(struct lagg_softc *); 117 static int lagg_rr_detach(struct lagg_softc *); 118 static void lagg_rr_port_destroy(struct lagg_port *); 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 if (sc->sc_detach != NULL) 277 (*sc->sc_detach)(sc); 278 279 LAGG_WUNLOCK(sc); 280 281 ifmedia_removeall(&sc->sc_media); 282 ether_ifdetach(ifp); 283 if_free_type(ifp, IFT_ETHER); 284 285 mtx_lock(&lagg_list_mtx); 286 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries); 287 mtx_unlock(&lagg_list_mtx); 288 289 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task); 290 LAGG_LOCK_DESTROY(sc); 291 free(sc, M_DEVBUF); 292 } 293 294 static void 295 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr) 296 { 297 struct ifnet *ifp = sc->sc_ifp; 298 299 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 300 return; 301 302 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 303 /* Let the protocol know the MAC has changed */ 304 if (sc->sc_lladdr != NULL) 305 (*sc->sc_lladdr)(sc); 306 } 307 308 static int 309 lagg_capabilities(struct lagg_softc *sc) 310 { 311 struct lagg_port *lp; 312 int cap = ~0, priv; 313 314 LAGG_WLOCK_ASSERT(sc); 315 316 /* Preserve private capabilities */ 317 priv = sc->sc_capabilities & IFCAP_LAGG_MASK; 318 319 /* Get capabilities from the lagg ports */ 320 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 321 cap &= lp->lp_capabilities; 322 323 if (sc->sc_ifflags & IFF_DEBUG) { 324 printf("%s: capabilities 0x%08x\n", 325 sc->sc_ifname, cap == ~0 ? priv : (cap | priv)); 326 } 327 328 return (cap == ~0 ? priv : (cap | priv)); 329 } 330 331 static void 332 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr) 333 { 334 struct lagg_softc *sc = lp->lp_softc; 335 struct ifnet *ifp = lp->lp_ifp; 336 struct lagg_llq *llq; 337 int pending = 0; 338 339 LAGG_WLOCK_ASSERT(sc); 340 341 if (lp->lp_detaching || 342 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 343 return; 344 345 /* Check to make sure its not already queued to be changed */ 346 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 347 if (llq->llq_ifp == ifp) { 348 pending = 1; 349 break; 350 } 351 } 352 353 if (!pending) { 354 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT); 355 if (llq == NULL) /* XXX what to do */ 356 return; 357 } 358 359 /* Update the lladdr even if pending, it may have changed */ 360 llq->llq_ifp = ifp; 361 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN); 362 363 if (!pending) 364 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries); 365 366 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task); 367 } 368 369 /* 370 * Set the interface MAC address from a taskqueue to avoid a LOR. 371 */ 372 static void 373 lagg_port_setlladdr(void *arg, int pending) 374 { 375 struct lagg_softc *sc = (struct lagg_softc *)arg; 376 struct lagg_llq *llq, *head; 377 struct ifnet *ifp; 378 int error; 379 380 /* Grab a local reference of the queue and remove it from the softc */ 381 LAGG_WLOCK(sc); 382 head = SLIST_FIRST(&sc->sc_llq_head); 383 SLIST_FIRST(&sc->sc_llq_head) = NULL; 384 LAGG_WUNLOCK(sc); 385 386 /* 387 * Traverse the queue and set the lladdr on each ifp. It is safe to do 388 * unlocked as we have the only reference to it. 389 */ 390 for (llq = head; llq != NULL; llq = head) { 391 ifp = llq->llq_ifp; 392 393 /* Set the link layer address */ 394 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN); 395 if (error) 396 printf("%s: setlladdr failed on %s\n", __func__, 397 ifp->if_xname); 398 399 head = SLIST_NEXT(llq, llq_entries); 400 free(llq, M_DEVBUF); 401 } 402 } 403 404 static int 405 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp) 406 { 407 struct lagg_softc *sc_ptr; 408 struct lagg_port *lp; 409 int error = 0; 410 411 LAGG_WLOCK_ASSERT(sc); 412 413 /* Limit the maximal number of lagg ports */ 414 if (sc->sc_count >= LAGG_MAX_PORTS) 415 return (ENOSPC); 416 417 /* New lagg port has to be in an idle state */ 418 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 419 return (EBUSY); 420 421 /* Check if port has already been associated to a lagg */ 422 if (ifp->if_lagg != NULL) 423 return (EBUSY); 424 425 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */ 426 if (ifp->if_type != IFT_ETHER) 427 return (EPROTONOSUPPORT); 428 429 if ((lp = malloc(sizeof(struct lagg_port), 430 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 431 return (ENOMEM); 432 433 /* Check if port is a stacked lagg */ 434 mtx_lock(&lagg_list_mtx); 435 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) { 436 if (ifp == sc_ptr->sc_ifp) { 437 mtx_unlock(&lagg_list_mtx); 438 free(lp, M_DEVBUF); 439 return (EINVAL); 440 /* XXX disable stacking for the moment, its untested 441 lp->lp_flags |= LAGG_PORT_STACK; 442 if (lagg_port_checkstacking(sc_ptr) >= 443 LAGG_MAX_STACKING) { 444 mtx_unlock(&lagg_list_mtx); 445 free(lp, M_DEVBUF); 446 return (E2BIG); 447 } 448 */ 449 } 450 } 451 mtx_unlock(&lagg_list_mtx); 452 453 /* Change the interface type */ 454 lp->lp_iftype = ifp->if_type; 455 ifp->if_type = IFT_IEEE8023ADLAG; 456 ifp->if_lagg = lp; 457 lp->lp_ioctl = ifp->if_ioctl; 458 ifp->if_ioctl = lagg_port_ioctl; 459 lp->lp_output = ifp->if_output; 460 ifp->if_output = lagg_port_output; 461 462 lp->lp_ifp = ifp; 463 lp->lp_softc = sc; 464 465 /* Save port link layer address */ 466 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN); 467 468 if (SLIST_EMPTY(&sc->sc_ports)) { 469 sc->sc_primary = lp; 470 lagg_lladdr(sc, IF_LLADDR(ifp)); 471 } else { 472 /* Update link layer address for this port */ 473 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp)); 474 } 475 476 /* Insert into the list of ports */ 477 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries); 478 sc->sc_count++; 479 480 /* Update lagg capabilities */ 481 sc->sc_capabilities = lagg_capabilities(sc); 482 483 /* Add multicast addresses and interface flags to this port */ 484 lagg_ether_cmdmulti(lp, 1); 485 lagg_setflags(lp, 1); 486 487 if (sc->sc_port_create != NULL) 488 error = (*sc->sc_port_create)(lp); 489 if (error) { 490 /* remove the port again, without calling sc_port_destroy */ 491 lagg_port_destroy(lp, 0); 492 return (error); 493 } 494 495 return (error); 496 } 497 498 static int 499 lagg_port_checkstacking(struct lagg_softc *sc) 500 { 501 struct lagg_softc *sc_ptr; 502 struct lagg_port *lp; 503 int m = 0; 504 505 LAGG_WLOCK_ASSERT(sc); 506 507 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 508 if (lp->lp_flags & LAGG_PORT_STACK) { 509 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc; 510 m = MAX(m, lagg_port_checkstacking(sc_ptr)); 511 } 512 } 513 514 return (m + 1); 515 } 516 517 static int 518 lagg_port_destroy(struct lagg_port *lp, int runpd) 519 { 520 struct lagg_softc *sc = lp->lp_softc; 521 struct lagg_port *lp_ptr; 522 struct lagg_llq *llq; 523 struct ifnet *ifp = lp->lp_ifp; 524 525 LAGG_WLOCK_ASSERT(sc); 526 527 if (runpd && sc->sc_port_destroy != NULL) 528 (*sc->sc_port_destroy)(lp); 529 530 /* 531 * Remove multicast addresses and interface flags from this port and 532 * reset the MAC address, skip if the interface is being detached. 533 */ 534 if (!lp->lp_detaching) { 535 lagg_ether_cmdmulti(lp, 0); 536 lagg_setflags(lp, 0); 537 lagg_port_lladdr(lp, lp->lp_lladdr); 538 } 539 540 /* Restore interface */ 541 ifp->if_type = lp->lp_iftype; 542 ifp->if_ioctl = lp->lp_ioctl; 543 ifp->if_output = lp->lp_output; 544 ifp->if_lagg = NULL; 545 546 /* Finally, remove the port from the lagg */ 547 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries); 548 sc->sc_count--; 549 550 /* Update the primary interface */ 551 if (lp == sc->sc_primary) { 552 uint8_t lladdr[ETHER_ADDR_LEN]; 553 554 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) { 555 bzero(&lladdr, ETHER_ADDR_LEN); 556 } else { 557 bcopy(lp_ptr->lp_lladdr, 558 lladdr, ETHER_ADDR_LEN); 559 } 560 lagg_lladdr(sc, lladdr); 561 sc->sc_primary = lp_ptr; 562 563 /* Update link layer address for each port */ 564 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries) 565 lagg_port_lladdr(lp_ptr, lladdr); 566 } 567 568 /* Remove any pending lladdr changes from the queue */ 569 if (lp->lp_detaching) { 570 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 571 if (llq->llq_ifp == ifp) { 572 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq, 573 llq_entries); 574 free(llq, M_DEVBUF); 575 break; /* Only appears once */ 576 } 577 } 578 } 579 580 if (lp->lp_ifflags) 581 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__); 582 583 free(lp, M_DEVBUF); 584 585 /* Update lagg capabilities */ 586 sc->sc_capabilities = lagg_capabilities(sc); 587 588 return (0); 589 } 590 591 static int 592 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 593 { 594 struct lagg_reqport *rp = (struct lagg_reqport *)data; 595 struct lagg_softc *sc; 596 struct lagg_port *lp = NULL; 597 int error = 0; 598 599 /* Should be checked by the caller */ 600 if (ifp->if_type != IFT_IEEE8023ADLAG || 601 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL) 602 goto fallback; 603 604 switch (cmd) { 605 case SIOCGLAGGPORT: 606 LAGG_RLOCK(sc); 607 if (rp->rp_portname[0] == '\0' || 608 ifunit(rp->rp_portname) != ifp) { 609 error = EINVAL; 610 break; 611 } 612 613 if (lp->lp_softc != sc) { 614 error = ENOENT; 615 break; 616 } 617 618 lagg_port2req(lp, rp); 619 LAGG_RUNLOCK(sc); 620 break; 621 default: 622 goto fallback; 623 } 624 625 return (error); 626 627 fallback: 628 if (lp->lp_ioctl != NULL) 629 return ((*lp->lp_ioctl)(ifp, cmd, data)); 630 631 return (EINVAL); 632 } 633 634 static int 635 lagg_port_output(struct ifnet *ifp, struct mbuf *m, 636 struct sockaddr *dst, struct rtentry *rt0) 637 { 638 struct lagg_port *lp = ifp->if_lagg; 639 struct ether_header *eh; 640 short type = 0; 641 642 switch (dst->sa_family) { 643 case pseudo_AF_HDRCMPLT: 644 case AF_UNSPEC: 645 eh = (struct ether_header *)dst->sa_data; 646 type = eh->ether_type; 647 break; 648 } 649 650 /* 651 * Only allow ethernet types required to initiate or maintain the link, 652 * aggregated frames take a different path. 653 */ 654 switch (ntohs(type)) { 655 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */ 656 return ((*lp->lp_output)(ifp, m, dst, rt0)); 657 } 658 659 /* drop any other frames */ 660 m_freem(m); 661 return (EBUSY); 662 } 663 664 static void 665 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp) 666 { 667 struct lagg_port *lp; 668 struct lagg_softc *sc; 669 670 if ((lp = ifp->if_lagg) == NULL) 671 return; 672 673 sc = lp->lp_softc; 674 675 LAGG_WLOCK(sc); 676 lp->lp_detaching = 1; 677 lagg_port_destroy(lp, 1); 678 LAGG_WUNLOCK(sc); 679 } 680 681 static void 682 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp) 683 { 684 struct lagg_softc *sc = lp->lp_softc; 685 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname)); 686 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname)); 687 rp->rp_prio = lp->lp_prio; 688 rp->rp_flags = lp->lp_flags; 689 if (sc->sc_portreq != NULL) 690 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc); 691 692 /* Add protocol specific flags */ 693 switch (sc->sc_proto) { 694 case LAGG_PROTO_FAILOVER: 695 if (lp == sc->sc_primary) 696 rp->rp_flags |= LAGG_PORT_MASTER; 697 /* FALLTHROUGH */ 698 case LAGG_PROTO_ROUNDROBIN: 699 case LAGG_PROTO_LOADBALANCE: 700 case LAGG_PROTO_ETHERCHANNEL: 701 if (LAGG_PORTACTIVE(lp)) 702 rp->rp_flags |= LAGG_PORT_ACTIVE; 703 break; 704 705 case LAGG_PROTO_LACP: 706 /* LACP has a different definition of active */ 707 if (lacp_port_isactive(lp)) 708 rp->rp_flags |= LAGG_PORT_ACTIVE; 709 break; 710 } 711 712 } 713 714 static void 715 lagg_init(void *xsc) 716 { 717 struct lagg_softc *sc = (struct lagg_softc *)xsc; 718 struct lagg_port *lp; 719 struct ifnet *ifp = sc->sc_ifp; 720 721 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 722 return; 723 724 LAGG_WLOCK(sc); 725 726 ifp->if_drv_flags |= IFF_DRV_RUNNING; 727 /* Update the port lladdrs */ 728 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 729 lagg_port_lladdr(lp, IF_LLADDR(ifp)); 730 731 if (sc->sc_init != NULL) 732 (*sc->sc_init)(sc); 733 734 LAGG_WUNLOCK(sc); 735 } 736 737 static void 738 lagg_stop(struct lagg_softc *sc) 739 { 740 struct ifnet *ifp = sc->sc_ifp; 741 742 LAGG_WLOCK_ASSERT(sc); 743 744 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 745 return; 746 747 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 748 749 if (sc->sc_stop != NULL) 750 (*sc->sc_stop)(sc); 751 } 752 753 static int 754 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 755 { 756 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 757 struct lagg_reqall *ra = (struct lagg_reqall *)data; 758 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf; 759 struct ifreq *ifr = (struct ifreq *)data; 760 struct lagg_port *lp; 761 struct ifnet *tpif; 762 struct thread *td = curthread; 763 int i, error = 0, unlock = 1; 764 765 LAGG_WLOCK(sc); 766 767 bzero(&rpbuf, sizeof(rpbuf)); 768 769 switch (cmd) { 770 case SIOCGLAGG: 771 ra->ra_proto = sc->sc_proto; 772 ra->ra_ports = i = 0; 773 if (sc->sc_req != NULL) 774 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc); 775 lp = SLIST_FIRST(&sc->sc_ports); 776 while (lp && ra->ra_size >= 777 i + sizeof(struct lagg_reqport)) { 778 lagg_port2req(lp, &rpbuf); 779 error = copyout(&rpbuf, (caddr_t)ra->ra_port + i, 780 sizeof(struct lagg_reqport)); 781 if (error) 782 break; 783 i += sizeof(struct lagg_reqport); 784 ra->ra_ports++; 785 lp = SLIST_NEXT(lp, lp_entries); 786 } 787 break; 788 case SIOCSLAGG: 789 error = priv_check(td, PRIV_NET_LAGG); 790 if (error) 791 break; 792 if (ra->ra_proto >= LAGG_PROTO_MAX) { 793 error = EPROTONOSUPPORT; 794 break; 795 } 796 if (sc->sc_proto != LAGG_PROTO_NONE) { 797 error = sc->sc_detach(sc); 798 /* Reset protocol and pointers */ 799 sc->sc_proto = LAGG_PROTO_NONE; 800 sc->sc_detach = NULL; 801 sc->sc_start = NULL; 802 sc->sc_input = NULL; 803 sc->sc_port_create = NULL; 804 sc->sc_port_destroy = NULL; 805 sc->sc_linkstate = NULL; 806 sc->sc_init = NULL; 807 sc->sc_stop = NULL; 808 sc->sc_lladdr = NULL; 809 sc->sc_req = NULL; 810 sc->sc_portreq = NULL; 811 } 812 if (error != 0) 813 break; 814 for (i = 0; i < (sizeof(lagg_protos) / 815 sizeof(lagg_protos[0])); i++) { 816 if (lagg_protos[i].ti_proto == ra->ra_proto) { 817 if (sc->sc_ifflags & IFF_DEBUG) 818 printf("%s: using proto %u\n", 819 sc->sc_ifname, 820 lagg_protos[i].ti_proto); 821 sc->sc_proto = lagg_protos[i].ti_proto; 822 if (sc->sc_proto != LAGG_PROTO_NONE) 823 error = lagg_protos[i].ti_attach(sc); 824 goto out; 825 } 826 } 827 error = EPROTONOSUPPORT; 828 break; 829 case SIOCGLAGGPORT: 830 if (rp->rp_portname[0] == '\0' || 831 (tpif = ifunit(rp->rp_portname)) == NULL) { 832 error = EINVAL; 833 break; 834 } 835 836 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 837 lp->lp_softc != sc) { 838 error = ENOENT; 839 break; 840 } 841 842 lagg_port2req(lp, rp); 843 break; 844 case SIOCSLAGGPORT: 845 error = priv_check(td, PRIV_NET_LAGG); 846 if (error) 847 break; 848 if (rp->rp_portname[0] == '\0' || 849 (tpif = ifunit(rp->rp_portname)) == NULL) { 850 error = EINVAL; 851 break; 852 } 853 error = lagg_port_create(sc, tpif); 854 break; 855 case SIOCSLAGGDELPORT: 856 error = priv_check(td, PRIV_NET_LAGG); 857 if (error) 858 break; 859 if (rp->rp_portname[0] == '\0' || 860 (tpif = ifunit(rp->rp_portname)) == NULL) { 861 error = EINVAL; 862 break; 863 } 864 865 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 866 lp->lp_softc != sc) { 867 error = ENOENT; 868 break; 869 } 870 871 error = lagg_port_destroy(lp, 1); 872 break; 873 case SIOCSIFFLAGS: 874 /* Set flags on ports too */ 875 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 876 lagg_setflags(lp, 1); 877 } 878 879 if (!(ifp->if_flags & IFF_UP) && 880 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 881 /* 882 * If interface is marked down and it is running, 883 * then stop and disable it. 884 */ 885 lagg_stop(sc); 886 } else if ((ifp->if_flags & IFF_UP) && 887 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 888 /* 889 * If interface is marked up and it is stopped, then 890 * start it. 891 */ 892 LAGG_WUNLOCK(sc); 893 unlock = 0; 894 (*ifp->if_init)(sc); 895 } 896 break; 897 case SIOCADDMULTI: 898 case SIOCDELMULTI: 899 error = lagg_ether_setmulti(sc); 900 break; 901 case SIOCSIFMEDIA: 902 case SIOCGIFMEDIA: 903 LAGG_WUNLOCK(sc); 904 unlock = 0; 905 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 906 break; 907 default: 908 LAGG_WUNLOCK(sc); 909 unlock = 0; 910 error = ether_ioctl(ifp, cmd, data); 911 break; 912 } 913 914 out: 915 if (unlock) 916 LAGG_WUNLOCK(sc); 917 return (error); 918 } 919 920 static int 921 lagg_ether_setmulti(struct lagg_softc *sc) 922 { 923 struct lagg_port *lp; 924 925 LAGG_WLOCK_ASSERT(sc); 926 927 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 928 /* First, remove any existing filter entries. */ 929 lagg_ether_cmdmulti(lp, 0); 930 /* copy all addresses from the lagg interface to the port */ 931 lagg_ether_cmdmulti(lp, 1); 932 } 933 return (0); 934 } 935 936 static int 937 lagg_ether_cmdmulti(struct lagg_port *lp, int set) 938 { 939 struct lagg_softc *sc = lp->lp_softc; 940 struct ifnet *ifp = lp->lp_ifp; 941 struct ifnet *scifp = sc->sc_ifp; 942 struct lagg_mc *mc; 943 struct ifmultiaddr *ifma, *rifma = NULL; 944 struct sockaddr_dl sdl; 945 int error; 946 947 LAGG_WLOCK_ASSERT(sc); 948 949 bzero((char *)&sdl, sizeof(sdl)); 950 sdl.sdl_len = sizeof(sdl); 951 sdl.sdl_family = AF_LINK; 952 sdl.sdl_type = IFT_ETHER; 953 sdl.sdl_alen = ETHER_ADDR_LEN; 954 sdl.sdl_index = ifp->if_index; 955 956 if (set) { 957 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) { 958 if (ifma->ifma_addr->sa_family != AF_LINK) 959 continue; 960 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 961 LLADDR(&sdl), ETHER_ADDR_LEN); 962 963 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma); 964 if (error) 965 return (error); 966 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT); 967 if (mc == NULL) 968 return (ENOMEM); 969 mc->mc_ifma = rifma; 970 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries); 971 } 972 } else { 973 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) { 974 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries); 975 if_delmulti_ifma(mc->mc_ifma); 976 free(mc, M_DEVBUF); 977 } 978 } 979 return (0); 980 } 981 982 /* Handle a ref counted flag that should be set on the lagg port as well */ 983 static int 984 lagg_setflag(struct lagg_port *lp, int flag, int status, 985 int (*func)(struct ifnet *, int)) 986 { 987 struct lagg_softc *sc = lp->lp_softc; 988 struct ifnet *scifp = sc->sc_ifp; 989 struct ifnet *ifp = lp->lp_ifp; 990 int error; 991 992 LAGG_WLOCK_ASSERT(sc); 993 994 status = status ? (scifp->if_flags & flag) : 0; 995 /* Now "status" contains the flag value or 0 */ 996 997 /* 998 * See if recorded ports status is different from what 999 * we want it to be. If it is, flip it. We record ports 1000 * status in lp_ifflags so that we won't clear ports flag 1001 * we haven't set. In fact, we don't clear or set ports 1002 * flags directly, but get or release references to them. 1003 * That's why we can be sure that recorded flags still are 1004 * in accord with actual ports flags. 1005 */ 1006 if (status != (lp->lp_ifflags & flag)) { 1007 error = (*func)(ifp, status); 1008 if (error) 1009 return (error); 1010 lp->lp_ifflags &= ~flag; 1011 lp->lp_ifflags |= status; 1012 } 1013 return (0); 1014 } 1015 1016 /* 1017 * Handle IFF_* flags that require certain changes on the lagg port 1018 * if "status" is true, update ports flags respective to the lagg 1019 * if "status" is false, forcedly clear the flags set on port. 1020 */ 1021 static int 1022 lagg_setflags(struct lagg_port *lp, int status) 1023 { 1024 int error, i; 1025 1026 for (i = 0; lagg_pflags[i].flag; i++) { 1027 error = lagg_setflag(lp, lagg_pflags[i].flag, 1028 status, lagg_pflags[i].func); 1029 if (error) 1030 return (error); 1031 } 1032 return (0); 1033 } 1034 1035 static void 1036 lagg_start(struct ifnet *ifp) 1037 { 1038 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1039 struct mbuf *m; 1040 int error = 0; 1041 1042 LAGG_RLOCK(sc); 1043 for (;; error = 0) { 1044 IFQ_DEQUEUE(&ifp->if_snd, m); 1045 if (m == NULL) 1046 break; 1047 1048 BPF_MTAP(ifp, m); 1049 1050 if (sc->sc_proto != LAGG_PROTO_NONE) 1051 error = (*sc->sc_start)(sc, m); 1052 else 1053 m_freem(m); 1054 1055 if (error == 0) 1056 ifp->if_opackets++; 1057 else { 1058 m_freem(m); /* sc_start failed */ 1059 ifp->if_oerrors++; 1060 } 1061 } 1062 LAGG_RUNLOCK(sc); 1063 1064 return; 1065 } 1066 1067 static struct mbuf * 1068 lagg_input(struct ifnet *ifp, struct mbuf *m) 1069 { 1070 struct lagg_port *lp = ifp->if_lagg; 1071 struct lagg_softc *sc = lp->lp_softc; 1072 struct ifnet *scifp = sc->sc_ifp; 1073 1074 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1075 (lp->lp_flags & LAGG_PORT_DISABLED) || 1076 sc->sc_proto == LAGG_PROTO_NONE) { 1077 m_freem(m); 1078 return (NULL); 1079 } 1080 1081 LAGG_RLOCK(sc); 1082 BPF_MTAP(scifp, m); 1083 1084 m = (*sc->sc_input)(sc, lp, m); 1085 1086 if (m != NULL) { 1087 scifp->if_ipackets++; 1088 scifp->if_ibytes += m->m_pkthdr.len; 1089 } 1090 1091 LAGG_RUNLOCK(sc); 1092 return (m); 1093 } 1094 1095 static int 1096 lagg_media_change(struct ifnet *ifp) 1097 { 1098 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1099 1100 if (sc->sc_ifflags & IFF_DEBUG) 1101 printf("%s\n", __func__); 1102 1103 /* Ignore */ 1104 return (0); 1105 } 1106 1107 static void 1108 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1109 { 1110 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1111 struct lagg_port *lp; 1112 1113 imr->ifm_status = IFM_AVALID; 1114 imr->ifm_active = IFM_ETHER | IFM_AUTO; 1115 1116 LAGG_RLOCK(sc); 1117 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1118 if (LAGG_PORTACTIVE(lp)) 1119 imr->ifm_status |= IFM_ACTIVE; 1120 } 1121 LAGG_RUNLOCK(sc); 1122 } 1123 1124 static void 1125 lagg_port_state(struct ifnet *ifp, int state) 1126 { 1127 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg; 1128 struct lagg_softc *sc = NULL; 1129 1130 if (lp != NULL) 1131 sc = lp->lp_softc; 1132 if (sc == NULL) 1133 return; 1134 1135 LAGG_WLOCK(sc); 1136 if (sc->sc_linkstate != NULL) 1137 (*sc->sc_linkstate)(lp); 1138 LAGG_WUNLOCK(sc); 1139 } 1140 1141 struct lagg_port * 1142 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp) 1143 { 1144 struct lagg_port *lp_next, *rval = NULL; 1145 // int new_link = LINK_STATE_DOWN; 1146 1147 LAGG_RLOCK_ASSERT(sc); 1148 /* 1149 * Search a port which reports an active link state. 1150 */ 1151 1152 if (lp == NULL) 1153 goto search; 1154 if (LAGG_PORTACTIVE(lp)) { 1155 rval = lp; 1156 goto found; 1157 } 1158 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL && 1159 LAGG_PORTACTIVE(lp_next)) { 1160 rval = lp_next; 1161 goto found; 1162 } 1163 1164 search: 1165 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1166 if (LAGG_PORTACTIVE(lp_next)) { 1167 rval = lp_next; 1168 goto found; 1169 } 1170 } 1171 1172 found: 1173 if (rval != NULL) { 1174 /* 1175 * The IEEE 802.1D standard assumes that a lagg with 1176 * multiple ports is always full duplex. This is valid 1177 * for load sharing laggs and if at least two links 1178 * are active. Unfortunately, checking the latter would 1179 * be too expensive at this point. 1180 XXX 1181 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) && 1182 (sc->sc_count > 1)) 1183 new_link = LINK_STATE_FULL_DUPLEX; 1184 else 1185 new_link = rval->lp_link_state; 1186 */ 1187 } 1188 1189 return (rval); 1190 } 1191 1192 static const void * 1193 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf) 1194 { 1195 if (m->m_pkthdr.len < (off + len)) { 1196 return (NULL); 1197 } else if (m->m_len < (off + len)) { 1198 m_copydata(m, off, len, buf); 1199 return (buf); 1200 } 1201 return (mtod(m, char *) + off); 1202 } 1203 1204 uint32_t 1205 lagg_hashmbuf(struct mbuf *m, uint32_t key) 1206 { 1207 uint16_t etype; 1208 uint32_t p = 0; 1209 int off; 1210 struct ether_header *eh; 1211 struct ether_vlan_header vlanbuf; 1212 const struct ether_vlan_header *vlan; 1213 #ifdef INET 1214 const struct ip *ip; 1215 struct ip ipbuf; 1216 #endif 1217 #ifdef INET6 1218 const struct ip6_hdr *ip6; 1219 struct ip6_hdr ip6buf; 1220 uint32_t flow; 1221 #endif 1222 1223 off = sizeof(*eh); 1224 if (m->m_len < off) 1225 goto out; 1226 eh = mtod(m, struct ether_header *); 1227 etype = ntohs(eh->ether_type); 1228 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key); 1229 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p); 1230 1231 /* Special handling for encapsulating VLAN frames */ 1232 if (m->m_flags & M_VLANTAG) { 1233 p = hash32_buf(&m->m_pkthdr.ether_vtag, 1234 sizeof(m->m_pkthdr.ether_vtag), p); 1235 } else if (etype == ETHERTYPE_VLAN) { 1236 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf); 1237 if (vlan == NULL) 1238 goto out; 1239 1240 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p); 1241 etype = ntohs(vlan->evl_proto); 1242 off += sizeof(*vlan) - sizeof(*eh); 1243 } 1244 1245 switch (etype) { 1246 #ifdef INET 1247 case ETHERTYPE_IP: 1248 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf); 1249 if (ip == NULL) 1250 goto out; 1251 1252 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p); 1253 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p); 1254 break; 1255 #endif 1256 #ifdef INET6 1257 case ETHERTYPE_IPV6: 1258 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf); 1259 if (ip6 == NULL) 1260 goto out; 1261 1262 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p); 1263 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p); 1264 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK; 1265 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */ 1266 break; 1267 #endif 1268 } 1269 out: 1270 return (p); 1271 } 1272 1273 int 1274 lagg_enqueue(struct ifnet *ifp, struct mbuf *m) 1275 { 1276 int error = 0; 1277 1278 IFQ_HANDOFF(ifp, m, error); 1279 return (error); 1280 } 1281 1282 /* 1283 * Simple round robin aggregation 1284 */ 1285 1286 static int 1287 lagg_rr_attach(struct lagg_softc *sc) 1288 { 1289 struct lagg_port *lp; 1290 1291 sc->sc_detach = lagg_rr_detach; 1292 sc->sc_start = lagg_rr_start; 1293 sc->sc_input = lagg_rr_input; 1294 sc->sc_port_create = NULL; 1295 sc->sc_port_destroy = lagg_rr_port_destroy; 1296 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1297 1298 lp = SLIST_FIRST(&sc->sc_ports); 1299 sc->sc_psc = (caddr_t)lp; 1300 1301 return (0); 1302 } 1303 1304 static int 1305 lagg_rr_detach(struct lagg_softc *sc) 1306 { 1307 sc->sc_psc = NULL; 1308 return (0); 1309 } 1310 1311 static void 1312 lagg_rr_port_destroy(struct lagg_port *lp) 1313 { 1314 struct lagg_softc *sc = lp->lp_softc; 1315 1316 if (lp == (struct lagg_port *)sc->sc_psc) 1317 sc->sc_psc = NULL; 1318 } 1319 1320 static int 1321 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m) 1322 { 1323 struct lagg_port *lp = (struct lagg_port *)sc->sc_psc, *lp_next; 1324 int error = 0; 1325 1326 if (lp == NULL && (lp = lagg_link_active(sc, NULL)) == NULL) 1327 return (ENOENT); 1328 1329 /* Send mbuf */ 1330 error = lagg_enqueue(lp->lp_ifp, m); 1331 1332 /* Get next active port */ 1333 lp_next = lagg_link_active(sc, SLIST_NEXT(lp, lp_entries)); 1334 sc->sc_psc = (caddr_t)lp_next; 1335 1336 return (error); 1337 } 1338 1339 static struct mbuf * 1340 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1341 { 1342 struct ifnet *ifp = sc->sc_ifp; 1343 1344 /* Just pass in the packet to our lagg device */ 1345 m->m_pkthdr.rcvif = ifp; 1346 1347 return (m); 1348 } 1349 1350 /* 1351 * Active failover 1352 */ 1353 1354 static int 1355 lagg_fail_attach(struct lagg_softc *sc) 1356 { 1357 sc->sc_detach = lagg_fail_detach; 1358 sc->sc_start = lagg_fail_start; 1359 sc->sc_input = lagg_fail_input; 1360 sc->sc_port_create = NULL; 1361 sc->sc_port_destroy = NULL; 1362 1363 return (0); 1364 } 1365 1366 static int 1367 lagg_fail_detach(struct lagg_softc *sc) 1368 { 1369 return (0); 1370 } 1371 1372 static int 1373 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m) 1374 { 1375 struct lagg_port *lp; 1376 1377 /* Use the master port if active or the next available port */ 1378 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) 1379 return (ENOENT); 1380 1381 /* Send mbuf */ 1382 return (lagg_enqueue(lp->lp_ifp, m)); 1383 } 1384 1385 static struct mbuf * 1386 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1387 { 1388 struct ifnet *ifp = sc->sc_ifp; 1389 struct lagg_port *tmp_tp; 1390 1391 if (lp == sc->sc_primary) { 1392 m->m_pkthdr.rcvif = ifp; 1393 return (m); 1394 } 1395 1396 if (sc->sc_primary->lp_link_state == LINK_STATE_DOWN) { 1397 tmp_tp = lagg_link_active(sc, NULL); 1398 /* 1399 * If tmp_tp is null, we've recieved a packet when all 1400 * our links are down. Weird, but process it anyways. 1401 */ 1402 if ((tmp_tp == NULL || tmp_tp == lp)) { 1403 m->m_pkthdr.rcvif = ifp; 1404 return (m); 1405 } 1406 } 1407 1408 m_freem(m); 1409 return (NULL); 1410 } 1411 1412 /* 1413 * Loadbalancing 1414 */ 1415 1416 static int 1417 lagg_lb_attach(struct lagg_softc *sc) 1418 { 1419 struct lagg_port *lp; 1420 struct lagg_lb *lb; 1421 1422 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb), 1423 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 1424 return (ENOMEM); 1425 1426 sc->sc_detach = lagg_lb_detach; 1427 sc->sc_start = lagg_lb_start; 1428 sc->sc_input = lagg_lb_input; 1429 sc->sc_port_create = lagg_lb_port_create; 1430 sc->sc_port_destroy = lagg_lb_port_destroy; 1431 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1432 1433 lb->lb_key = arc4random(); 1434 sc->sc_psc = (caddr_t)lb; 1435 1436 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1437 lagg_lb_port_create(lp); 1438 1439 return (0); 1440 } 1441 1442 static int 1443 lagg_lb_detach(struct lagg_softc *sc) 1444 { 1445 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1446 if (lb != NULL) 1447 free(lb, M_DEVBUF); 1448 return (0); 1449 } 1450 1451 static int 1452 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp) 1453 { 1454 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1455 struct lagg_port *lp_next; 1456 int i = 0; 1457 1458 bzero(&lb->lb_ports, sizeof(lb->lb_ports)); 1459 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1460 if (lp_next == lp) 1461 continue; 1462 if (i >= LAGG_MAX_PORTS) 1463 return (EINVAL); 1464 if (sc->sc_ifflags & IFF_DEBUG) 1465 printf("%s: port %s at index %d\n", 1466 sc->sc_ifname, lp_next->lp_ifname, i); 1467 lb->lb_ports[i++] = lp_next; 1468 } 1469 1470 return (0); 1471 } 1472 1473 static int 1474 lagg_lb_port_create(struct lagg_port *lp) 1475 { 1476 struct lagg_softc *sc = lp->lp_softc; 1477 return (lagg_lb_porttable(sc, NULL)); 1478 } 1479 1480 static void 1481 lagg_lb_port_destroy(struct lagg_port *lp) 1482 { 1483 struct lagg_softc *sc = lp->lp_softc; 1484 lagg_lb_porttable(sc, lp); 1485 } 1486 1487 static int 1488 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m) 1489 { 1490 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1491 struct lagg_port *lp = NULL; 1492 uint32_t p = 0; 1493 int idx; 1494 1495 p = lagg_hashmbuf(m, lb->lb_key); 1496 if ((idx = p % sc->sc_count) >= LAGG_MAX_PORTS) 1497 return (EINVAL); 1498 lp = lb->lb_ports[idx]; 1499 1500 /* 1501 * Check the port's link state. This will return the next active 1502 * port if the link is down or the port is NULL. 1503 */ 1504 if ((lp = lagg_link_active(sc, lp)) == NULL) 1505 return (ENOENT); 1506 1507 /* Send mbuf */ 1508 return (lagg_enqueue(lp->lp_ifp, m)); 1509 } 1510 1511 static struct mbuf * 1512 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1513 { 1514 struct ifnet *ifp = sc->sc_ifp; 1515 1516 /* Just pass in the packet to our lagg device */ 1517 m->m_pkthdr.rcvif = ifp; 1518 1519 return (m); 1520 } 1521 1522 /* 1523 * 802.3ad LACP 1524 */ 1525 1526 static int 1527 lagg_lacp_attach(struct lagg_softc *sc) 1528 { 1529 struct lagg_port *lp; 1530 int error; 1531 1532 sc->sc_detach = lagg_lacp_detach; 1533 sc->sc_port_create = lacp_port_create; 1534 sc->sc_port_destroy = lacp_port_destroy; 1535 sc->sc_linkstate = lacp_linkstate; 1536 sc->sc_start = lagg_lacp_start; 1537 sc->sc_input = lagg_lacp_input; 1538 sc->sc_init = lacp_init; 1539 sc->sc_stop = lacp_stop; 1540 sc->sc_lladdr = lagg_lacp_lladdr; 1541 sc->sc_req = lacp_req; 1542 sc->sc_portreq = lacp_portreq; 1543 1544 error = lacp_attach(sc); 1545 if (error) 1546 return (error); 1547 1548 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1549 lacp_port_create(lp); 1550 1551 return (error); 1552 } 1553 1554 static int 1555 lagg_lacp_detach(struct lagg_softc *sc) 1556 { 1557 struct lagg_port *lp; 1558 int error; 1559 1560 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1561 lacp_port_destroy(lp); 1562 1563 /* unlocking is safe here */ 1564 LAGG_WUNLOCK(sc); 1565 error = lacp_detach(sc); 1566 LAGG_WLOCK(sc); 1567 1568 return (error); 1569 } 1570 1571 static void 1572 lagg_lacp_lladdr(struct lagg_softc *sc) 1573 { 1574 struct lagg_port *lp; 1575 1576 /* purge all the lacp ports */ 1577 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1578 lacp_port_destroy(lp); 1579 1580 /* add them back in */ 1581 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1582 lacp_port_create(lp); 1583 } 1584 1585 static int 1586 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m) 1587 { 1588 struct lagg_port *lp; 1589 1590 lp = lacp_select_tx_port(sc, m); 1591 if (lp == NULL) 1592 return (EBUSY); 1593 1594 /* Send mbuf */ 1595 return (lagg_enqueue(lp->lp_ifp, m)); 1596 } 1597 1598 static struct mbuf * 1599 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1600 { 1601 struct ifnet *ifp = sc->sc_ifp; 1602 struct ether_header *eh; 1603 u_short etype; 1604 1605 eh = mtod(m, struct ether_header *); 1606 etype = ntohs(eh->ether_type); 1607 1608 /* Tap off LACP control messages */ 1609 if (etype == ETHERTYPE_SLOW) { 1610 lacp_input(lp, m); 1611 return (NULL); 1612 } 1613 1614 /* 1615 * If the port is not collecting or not in the active aggregator then 1616 * free and return. 1617 */ 1618 if ((lp->lp_flags & LAGG_PORT_COLLECTING) == 0 || 1619 lacp_port_isactive(lp) == 0) { 1620 m_freem(m); 1621 return (NULL); 1622 } 1623 1624 m->m_pkthdr.rcvif = ifp; 1625 return (m); 1626 } 1627