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