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