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