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