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