1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org> 5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org> 6 * Copyright (c) 2014, 2016 Marcelo Araujo <araujo@FreeBSD.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 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. */ 75 static 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 84 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */ 85 #define V_lagg_list VNET(lagg_list) 86 static 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) 93 eventhandler_tag lagg_detach_cookie = NULL; 94 95 static int lagg_clone_create(struct if_clone *, int, caddr_t); 96 static void lagg_clone_destroy(struct ifnet *); 97 static VNET_DEFINE(struct if_clone *, lagg_cloner); 98 #define V_lagg_cloner VNET(lagg_cloner) 99 static const char laggname[] = "lagg"; 100 101 static void lagg_lladdr(struct lagg_softc *, uint8_t *); 102 static void lagg_capabilities(struct lagg_softc *); 103 static void lagg_port_lladdr(struct lagg_port *, uint8_t *, lagg_llqtype); 104 static void lagg_port_setlladdr(void *, int); 105 static int lagg_port_create(struct lagg_softc *, struct ifnet *); 106 static int lagg_port_destroy(struct lagg_port *, int); 107 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *); 108 static void lagg_linkstate(struct lagg_softc *); 109 static void lagg_port_state(struct ifnet *, int); 110 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t); 111 static int lagg_port_output(struct ifnet *, struct mbuf *, 112 const struct sockaddr *, struct route *); 113 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *); 114 #ifdef LAGG_PORT_STACKING 115 static int lagg_port_checkstacking(struct lagg_softc *); 116 #endif 117 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *); 118 static void lagg_init(void *); 119 static void lagg_stop(struct lagg_softc *); 120 static int lagg_ioctl(struct ifnet *, u_long, caddr_t); 121 static int lagg_ether_setmulti(struct lagg_softc *); 122 static int lagg_ether_cmdmulti(struct lagg_port *, int); 123 static int lagg_setflag(struct lagg_port *, int, int, 124 int (*func)(struct ifnet *, int)); 125 static int lagg_setflags(struct lagg_port *, int status); 126 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt); 127 static int lagg_transmit(struct ifnet *, struct mbuf *); 128 static void lagg_qflush(struct ifnet *); 129 static int lagg_media_change(struct ifnet *); 130 static void lagg_media_status(struct ifnet *, struct ifmediareq *); 131 static struct lagg_port *lagg_link_active(struct lagg_softc *, 132 struct lagg_port *); 133 134 /* Simple round robin */ 135 static void lagg_rr_attach(struct lagg_softc *); 136 static int lagg_rr_start(struct lagg_softc *, struct mbuf *); 137 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *, 138 struct mbuf *); 139 140 /* Active failover */ 141 static int lagg_fail_start(struct lagg_softc *, struct mbuf *); 142 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *, 143 struct mbuf *); 144 145 /* Loadbalancing */ 146 static void lagg_lb_attach(struct lagg_softc *); 147 static void lagg_lb_detach(struct lagg_softc *); 148 static int lagg_lb_port_create(struct lagg_port *); 149 static void lagg_lb_port_destroy(struct lagg_port *); 150 static int lagg_lb_start(struct lagg_softc *, struct mbuf *); 151 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *, 152 struct mbuf *); 153 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *); 154 155 /* Broadcast */ 156 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *); 157 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *, 158 struct mbuf *); 159 160 /* 802.3ad LACP */ 161 static void lagg_lacp_attach(struct lagg_softc *); 162 static void lagg_lacp_detach(struct lagg_softc *); 163 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *); 164 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *, 165 struct mbuf *); 166 static void lagg_lacp_lladdr(struct lagg_softc *); 167 168 /* lagg protocol table */ 169 static 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 230 SYSCTL_DECL(_net_link); 231 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, 232 "Link Aggregation"); 233 234 /* Allow input on any failover links */ 235 static VNET_DEFINE(int, lagg_failover_rx_all); 236 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all) 237 SYSCTL_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 */ 242 static VNET_DEFINE(int, def_use_flowid) = 1; 243 #define V_def_use_flowid VNET(def_use_flowid) 244 SYSCTL_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 */ 249 static VNET_DEFINE(int, def_flowid_shift) = 16; 250 #define V_def_flowid_shift VNET(def_flowid_shift) 251 SYSCTL_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 255 #pragma clang optimize off 256 static void 257 vnet_lagg_init(const void *unused __unused) 258 { 259 260 LAGG_LIST_LOCK_INIT(); 261 SLIST_INIT(&V_lagg_list); 262 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create, 263 lagg_clone_destroy, 0); 264 } 265 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 266 vnet_lagg_init, NULL); 267 268 static void 269 vnet_lagg_uninit(const void *unused __unused) 270 { 271 272 if_clone_detach(V_lagg_cloner); 273 LAGG_LIST_LOCK_DESTROY(); 274 } 275 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY, 276 vnet_lagg_uninit, NULL); 277 278 static int 279 lagg_modevent(module_t mod, int type, void *data) 280 { 281 282 switch (type) { 283 case MOD_LOAD: 284 lagg_input_p = lagg_input; 285 lagg_linkstate_p = lagg_port_state; 286 lagg_detach_cookie = EVENTHANDLER_REGISTER( 287 ifnet_departure_event, lagg_port_ifdetach, NULL, 288 EVENTHANDLER_PRI_ANY); 289 break; 290 case MOD_UNLOAD: 291 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 292 lagg_detach_cookie); 293 lagg_input_p = NULL; 294 lagg_linkstate_p = NULL; 295 break; 296 default: 297 return (EOPNOTSUPP); 298 } 299 return (0); 300 } 301 302 static moduledata_t lagg_mod = { 303 "if_lagg", 304 lagg_modevent, 305 0 306 }; 307 308 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 309 MODULE_VERSION(if_lagg, 1); 310 311 static void 312 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr) 313 { 314 315 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto", 316 __func__, sc)); 317 318 if (sc->sc_ifflags & IFF_DEBUG) 319 if_printf(sc->sc_ifp, "using proto %u\n", pr); 320 321 if (lagg_protos[pr].pr_attach != NULL) 322 lagg_protos[pr].pr_attach(sc); 323 sc->sc_proto = pr; 324 } 325 326 static void 327 lagg_proto_detach(struct lagg_softc *sc) 328 { 329 lagg_proto pr; 330 331 LAGG_WLOCK_ASSERT(sc); 332 333 pr = sc->sc_proto; 334 sc->sc_proto = LAGG_PROTO_NONE; 335 336 if (lagg_protos[pr].pr_detach != NULL) 337 lagg_protos[pr].pr_detach(sc); 338 else 339 LAGG_WUNLOCK(sc); 340 } 341 342 static int 343 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m) 344 { 345 346 return (lagg_protos[sc->sc_proto].pr_start(sc, m)); 347 } 348 349 static struct mbuf * 350 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 351 { 352 353 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m)); 354 } 355 356 static int 357 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp) 358 { 359 360 if (lagg_protos[sc->sc_proto].pr_addport == NULL) 361 return (0); 362 else 363 return (lagg_protos[sc->sc_proto].pr_addport(lp)); 364 } 365 366 static void 367 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp) 368 { 369 370 if (lagg_protos[sc->sc_proto].pr_delport != NULL) 371 lagg_protos[sc->sc_proto].pr_delport(lp); 372 } 373 374 static void 375 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp) 376 { 377 378 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL) 379 lagg_protos[sc->sc_proto].pr_linkstate(lp); 380 } 381 382 static void 383 lagg_proto_init(struct lagg_softc *sc) 384 { 385 386 if (lagg_protos[sc->sc_proto].pr_init != NULL) 387 lagg_protos[sc->sc_proto].pr_init(sc); 388 } 389 390 static void 391 lagg_proto_stop(struct lagg_softc *sc) 392 { 393 394 if (lagg_protos[sc->sc_proto].pr_stop != NULL) 395 lagg_protos[sc->sc_proto].pr_stop(sc); 396 } 397 398 static void 399 lagg_proto_lladdr(struct lagg_softc *sc) 400 { 401 402 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL) 403 lagg_protos[sc->sc_proto].pr_lladdr(sc); 404 } 405 406 static void 407 lagg_proto_request(struct lagg_softc *sc, void *v) 408 { 409 410 if (lagg_protos[sc->sc_proto].pr_request != NULL) 411 lagg_protos[sc->sc_proto].pr_request(sc, v); 412 } 413 414 static void 415 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v) 416 { 417 418 if (lagg_protos[sc->sc_proto].pr_portreq != NULL) 419 lagg_protos[sc->sc_proto].pr_portreq(lp, v); 420 } 421 422 /* 423 * This routine is run via an vlan 424 * config EVENT 425 */ 426 static void 427 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 428 { 429 struct lagg_softc *sc = ifp->if_softc; 430 struct lagg_port *lp; 431 struct rm_priotracker tracker; 432 433 if (ifp->if_softc != arg) /* Not our event */ 434 return; 435 436 LAGG_RLOCK(sc, &tracker); 437 if (!SLIST_EMPTY(&sc->sc_ports)) { 438 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 439 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag); 440 } 441 LAGG_RUNLOCK(sc, &tracker); 442 } 443 444 /* 445 * This routine is run via an vlan 446 * unconfig EVENT 447 */ 448 static void 449 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 450 { 451 struct lagg_softc *sc = ifp->if_softc; 452 struct lagg_port *lp; 453 struct rm_priotracker tracker; 454 455 if (ifp->if_softc != arg) /* Not our event */ 456 return; 457 458 LAGG_RLOCK(sc, &tracker); 459 if (!SLIST_EMPTY(&sc->sc_ports)) { 460 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 461 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag); 462 } 463 LAGG_RUNLOCK(sc, &tracker); 464 } 465 466 static int 467 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params) 468 { 469 struct lagg_softc *sc; 470 struct ifnet *ifp; 471 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */ 472 473 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); 474 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 475 if (ifp == NULL) { 476 free(sc, M_DEVBUF); 477 return (ENOSPC); 478 } 479 480 if (V_def_use_flowid) 481 sc->sc_opts |= LAGG_OPT_USE_FLOWID; 482 sc->flowid_shift = V_def_flowid_shift; 483 484 /* Hash all layers by default */ 485 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4; 486 487 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT); 488 489 LAGG_LOCK_INIT(sc); 490 SLIST_INIT(&sc->sc_ports); 491 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc); 492 493 /* Initialise pseudo media types */ 494 ifmedia_init(&sc->sc_media, 0, lagg_media_change, 495 lagg_media_status); 496 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL); 497 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO); 498 499 if_initname(ifp, laggname, unit); 500 ifp->if_softc = sc; 501 ifp->if_transmit = lagg_transmit; 502 ifp->if_qflush = lagg_qflush; 503 ifp->if_init = lagg_init; 504 ifp->if_ioctl = lagg_ioctl; 505 ifp->if_get_counter = lagg_get_counter; 506 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 507 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS; 508 509 /* 510 * Attach as an ordinary ethernet device, children will be attached 511 * as special device IFT_IEEE8023ADLAG. 512 */ 513 ether_ifattach(ifp, eaddr); 514 515 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config, 516 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST); 517 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig, 518 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST); 519 520 /* Insert into the global list of laggs */ 521 LAGG_LIST_LOCK(); 522 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries); 523 LAGG_LIST_UNLOCK(); 524 525 return (0); 526 } 527 528 static void 529 lagg_clone_destroy(struct ifnet *ifp) 530 { 531 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 532 struct lagg_port *lp; 533 534 LAGG_WLOCK(sc); 535 536 lagg_stop(sc); 537 ifp->if_flags &= ~IFF_UP; 538 539 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach); 540 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach); 541 542 /* Shutdown and remove lagg ports */ 543 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL) 544 lagg_port_destroy(lp, 1); 545 /* Unhook the aggregation protocol */ 546 lagg_proto_detach(sc); 547 LAGG_UNLOCK_ASSERT(sc); 548 549 ifmedia_removeall(&sc->sc_media); 550 ether_ifdetach(ifp); 551 if_free(ifp); 552 553 LAGG_LIST_LOCK(); 554 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries); 555 LAGG_LIST_UNLOCK(); 556 557 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task); 558 LAGG_LOCK_DESTROY(sc); 559 free(sc, M_DEVBUF); 560 } 561 562 /* 563 * Set link-layer address on the lagg interface itself. 564 * 565 * Set noinline to be dtrace-friendly 566 */ 567 static __noinline void 568 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr) 569 { 570 struct ifnet *ifp = sc->sc_ifp; 571 struct lagg_port lp; 572 573 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 574 return; 575 576 LAGG_WLOCK_ASSERT(sc); 577 /* 578 * Set the link layer address on the lagg interface. 579 * lagg_proto_lladdr() notifies the MAC change to 580 * the aggregation protocol. iflladdr_event handler which 581 * may trigger gratuitous ARPs for INET will be handled in 582 * a taskqueue. 583 */ 584 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 585 lagg_proto_lladdr(sc); 586 587 /* 588 * Send notification request for lagg interface 589 * itself. Note that new lladdr is already set. 590 */ 591 bzero(&lp, sizeof(lp)); 592 lp.lp_ifp = sc->sc_ifp; 593 lp.lp_softc = sc; 594 595 /* Do not request lladdr change */ 596 lagg_port_lladdr(&lp, lladdr, LAGG_LLQTYPE_VIRT); 597 } 598 599 static void 600 lagg_capabilities(struct lagg_softc *sc) 601 { 602 struct lagg_port *lp; 603 int cap = ~0, ena = ~0; 604 u_long hwa = ~0UL; 605 struct ifnet_hw_tsomax hw_tsomax; 606 607 LAGG_WLOCK_ASSERT(sc); 608 609 memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 610 611 /* Get capabilities from the lagg ports */ 612 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 613 cap &= lp->lp_ifp->if_capabilities; 614 ena &= lp->lp_ifp->if_capenable; 615 hwa &= lp->lp_ifp->if_hwassist; 616 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax); 617 } 618 cap = (cap == ~0 ? 0 : cap); 619 ena = (ena == ~0 ? 0 : ena); 620 hwa = (hwa == ~0 ? 0 : hwa); 621 622 if (sc->sc_ifp->if_capabilities != cap || 623 sc->sc_ifp->if_capenable != ena || 624 sc->sc_ifp->if_hwassist != hwa || 625 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) { 626 sc->sc_ifp->if_capabilities = cap; 627 sc->sc_ifp->if_capenable = ena; 628 sc->sc_ifp->if_hwassist = hwa; 629 getmicrotime(&sc->sc_ifp->if_lastchange); 630 631 if (sc->sc_ifflags & IFF_DEBUG) 632 if_printf(sc->sc_ifp, 633 "capabilities 0x%08x enabled 0x%08x\n", cap, ena); 634 } 635 } 636 637 /* 638 * Enqueue interface lladdr notification. 639 * If request is already queued, it is updated. 640 * If setting lladdr is also desired, @do_change has to be set to 1. 641 * 642 * Set noinline to be dtrace-friendly 643 */ 644 static __noinline void 645 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr, lagg_llqtype llq_type) 646 { 647 struct lagg_softc *sc = lp->lp_softc; 648 struct ifnet *ifp = lp->lp_ifp; 649 struct lagg_llq *llq; 650 651 LAGG_WLOCK_ASSERT(sc); 652 653 /* 654 * Do not enqueue requests where lladdr is the same for 655 * "physical" interfaces (e.g. ports in lagg) 656 */ 657 if (llq_type == LAGG_LLQTYPE_PHYS && 658 memcmp(IF_LLADDR(ifp), lladdr, ETHER_ADDR_LEN) == 0) 659 return; 660 661 /* Check to make sure its not already queued to be changed */ 662 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 663 if (llq->llq_ifp == ifp) { 664 /* Update lladdr, it may have changed */ 665 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN); 666 return; 667 } 668 } 669 670 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT | M_ZERO); 671 if (llq == NULL) /* XXX what to do */ 672 return; 673 674 llq->llq_ifp = ifp; 675 llq->llq_type = llq_type; 676 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN); 677 /* XXX: We should insert to tail */ 678 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries); 679 680 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task); 681 } 682 683 /* 684 * Set the interface MAC address from a taskqueue to avoid a LOR. 685 * 686 * Set noinline to be dtrace-friendly 687 */ 688 static __noinline void 689 lagg_port_setlladdr(void *arg, int pending) 690 { 691 struct lagg_softc *sc = (struct lagg_softc *)arg; 692 struct lagg_llq *llq, *head; 693 struct ifnet *ifp; 694 695 /* Grab a local reference of the queue and remove it from the softc */ 696 LAGG_WLOCK(sc); 697 head = SLIST_FIRST(&sc->sc_llq_head); 698 SLIST_FIRST(&sc->sc_llq_head) = NULL; 699 LAGG_WUNLOCK(sc); 700 701 /* 702 * Traverse the queue and set the lladdr on each ifp. It is safe to do 703 * unlocked as we have the only reference to it. 704 */ 705 for (llq = head; llq != NULL; llq = head) { 706 ifp = llq->llq_ifp; 707 708 CURVNET_SET(ifp->if_vnet); 709 710 /* 711 * Set the link layer address on the laggport interface. 712 * Note that if_setlladdr() or iflladdr_event handler 713 * may result in arp transmission / lltable updates. 714 */ 715 if (llq->llq_type == LAGG_LLQTYPE_PHYS) 716 if_setlladdr(ifp, llq->llq_lladdr, 717 ETHER_ADDR_LEN); 718 else 719 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 720 CURVNET_RESTORE(); 721 head = SLIST_NEXT(llq, llq_entries); 722 free(llq, M_DEVBUF); 723 } 724 } 725 726 static int 727 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp) 728 { 729 struct lagg_softc *sc_ptr; 730 struct lagg_port *lp, *tlp; 731 int error, i; 732 uint64_t *pval; 733 734 LAGG_WLOCK_ASSERT(sc); 735 736 /* Limit the maximal number of lagg ports */ 737 if (sc->sc_count >= LAGG_MAX_PORTS) 738 return (ENOSPC); 739 740 /* Check if port has already been associated to a lagg */ 741 if (ifp->if_lagg != NULL) { 742 /* Port is already in the current lagg? */ 743 lp = (struct lagg_port *)ifp->if_lagg; 744 if (lp->lp_softc == sc) 745 return (EEXIST); 746 return (EBUSY); 747 } 748 749 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */ 750 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN) 751 return (EPROTONOSUPPORT); 752 753 /* Allow the first Ethernet member to define the MTU */ 754 if (SLIST_EMPTY(&sc->sc_ports)) 755 sc->sc_ifp->if_mtu = ifp->if_mtu; 756 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) { 757 if_printf(sc->sc_ifp, "invalid MTU for %s\n", 758 ifp->if_xname); 759 return (EINVAL); 760 } 761 762 if ((lp = malloc(sizeof(struct lagg_port), 763 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 764 return (ENOMEM); 765 766 /* Check if port is a stacked lagg */ 767 LAGG_LIST_LOCK(); 768 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) { 769 if (ifp == sc_ptr->sc_ifp) { 770 LAGG_LIST_UNLOCK(); 771 free(lp, M_DEVBUF); 772 return (EINVAL); 773 /* XXX disable stacking for the moment, its untested */ 774 #ifdef LAGG_PORT_STACKING 775 lp->lp_flags |= LAGG_PORT_STACK; 776 if (lagg_port_checkstacking(sc_ptr) >= 777 LAGG_MAX_STACKING) { 778 LAGG_LIST_UNLOCK(); 779 free(lp, M_DEVBUF); 780 return (E2BIG); 781 } 782 #endif 783 } 784 } 785 LAGG_LIST_UNLOCK(); 786 787 /* Change the interface type */ 788 lp->lp_iftype = ifp->if_type; 789 ifp->if_type = IFT_IEEE8023ADLAG; 790 ifp->if_lagg = lp; 791 lp->lp_ioctl = ifp->if_ioctl; 792 ifp->if_ioctl = lagg_port_ioctl; 793 lp->lp_output = ifp->if_output; 794 ifp->if_output = lagg_port_output; 795 796 lp->lp_ifp = ifp; 797 lp->lp_softc = sc; 798 799 /* Save port link layer address */ 800 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN); 801 802 if (SLIST_EMPTY(&sc->sc_ports)) { 803 sc->sc_primary = lp; 804 /* First port in lagg. Update/notify lagg lladdress */ 805 lagg_lladdr(sc, IF_LLADDR(ifp)); 806 } else { 807 808 /* 809 * Update link layer address for this port and 810 * send notifications to other subsystems. 811 */ 812 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp), LAGG_LLQTYPE_PHYS); 813 } 814 815 /* 816 * Insert into the list of ports. 817 * Keep ports sorted by if_index. It is handy, when configuration 818 * is predictable and `ifconfig laggN create ...` command 819 * will lead to the same result each time. 820 */ 821 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) { 822 if (tlp->lp_ifp->if_index < ifp->if_index && ( 823 SLIST_NEXT(tlp, lp_entries) == NULL || 824 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index > 825 ifp->if_index)) 826 break; 827 } 828 if (tlp != NULL) 829 SLIST_INSERT_AFTER(tlp, lp, lp_entries); 830 else 831 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries); 832 sc->sc_count++; 833 834 /* Update lagg capabilities */ 835 lagg_capabilities(sc); 836 lagg_linkstate(sc); 837 838 /* Read port counters */ 839 pval = lp->port_counters.val; 840 for (i = 0; i < IFCOUNTERS; i++, pval++) 841 *pval = ifp->if_get_counter(ifp, i); 842 /* Add multicast addresses and interface flags to this port */ 843 lagg_ether_cmdmulti(lp, 1); 844 lagg_setflags(lp, 1); 845 846 if ((error = lagg_proto_addport(sc, lp)) != 0) { 847 /* Remove the port, without calling pr_delport. */ 848 lagg_port_destroy(lp, 0); 849 return (error); 850 } 851 852 return (0); 853 } 854 855 #ifdef LAGG_PORT_STACKING 856 static int 857 lagg_port_checkstacking(struct lagg_softc *sc) 858 { 859 struct lagg_softc *sc_ptr; 860 struct lagg_port *lp; 861 int m = 0; 862 863 LAGG_WLOCK_ASSERT(sc); 864 865 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 866 if (lp->lp_flags & LAGG_PORT_STACK) { 867 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc; 868 m = MAX(m, lagg_port_checkstacking(sc_ptr)); 869 } 870 } 871 872 return (m + 1); 873 } 874 #endif 875 876 static int 877 lagg_port_destroy(struct lagg_port *lp, int rundelport) 878 { 879 struct lagg_softc *sc = lp->lp_softc; 880 struct lagg_port *lp_ptr, *lp0; 881 struct lagg_llq *llq; 882 struct ifnet *ifp = lp->lp_ifp; 883 uint64_t *pval, vdiff; 884 int i; 885 886 LAGG_WLOCK_ASSERT(sc); 887 888 if (rundelport) 889 lagg_proto_delport(sc, lp); 890 891 /* 892 * Remove multicast addresses and interface flags from this port and 893 * reset the MAC address, skip if the interface is being detached. 894 */ 895 if (!lp->lp_detaching) { 896 lagg_ether_cmdmulti(lp, 0); 897 lagg_setflags(lp, 0); 898 lagg_port_lladdr(lp, lp->lp_lladdr, LAGG_LLQTYPE_PHYS); 899 } 900 901 /* Restore interface */ 902 ifp->if_type = lp->lp_iftype; 903 ifp->if_ioctl = lp->lp_ioctl; 904 ifp->if_output = lp->lp_output; 905 ifp->if_lagg = NULL; 906 907 /* Update detached port counters */ 908 pval = lp->port_counters.val; 909 for (i = 0; i < IFCOUNTERS; i++, pval++) { 910 vdiff = ifp->if_get_counter(ifp, i) - *pval; 911 sc->detached_counters.val[i] += vdiff; 912 } 913 914 /* Finally, remove the port from the lagg */ 915 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries); 916 sc->sc_count--; 917 918 /* Update the primary interface */ 919 if (lp == sc->sc_primary) { 920 uint8_t lladdr[ETHER_ADDR_LEN]; 921 922 if ((lp0 = SLIST_FIRST(&sc->sc_ports)) == NULL) { 923 bzero(&lladdr, ETHER_ADDR_LEN); 924 } else { 925 bcopy(lp0->lp_lladdr, 926 lladdr, ETHER_ADDR_LEN); 927 } 928 lagg_lladdr(sc, lladdr); 929 930 /* Mark lp0 as new primary */ 931 sc->sc_primary = lp0; 932 933 /* 934 * Enqueue lladdr update/notification for each port 935 * (new primary needs update as well, to switch from 936 * old lladdr to its 'real' one). 937 */ 938 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries) 939 lagg_port_lladdr(lp_ptr, lladdr, LAGG_LLQTYPE_PHYS); 940 } 941 942 /* Remove any pending lladdr changes from the queue */ 943 if (lp->lp_detaching) { 944 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 945 if (llq->llq_ifp == ifp) { 946 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq, 947 llq_entries); 948 free(llq, M_DEVBUF); 949 break; /* Only appears once */ 950 } 951 } 952 } 953 954 if (lp->lp_ifflags) 955 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__); 956 957 free(lp, M_DEVBUF); 958 959 /* Update lagg capabilities */ 960 lagg_capabilities(sc); 961 lagg_linkstate(sc); 962 963 return (0); 964 } 965 966 static int 967 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 968 { 969 struct lagg_reqport *rp = (struct lagg_reqport *)data; 970 struct lagg_softc *sc; 971 struct lagg_port *lp = NULL; 972 int error = 0; 973 struct rm_priotracker tracker; 974 975 /* Should be checked by the caller */ 976 if (ifp->if_type != IFT_IEEE8023ADLAG || 977 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL) 978 goto fallback; 979 980 switch (cmd) { 981 case SIOCGLAGGPORT: 982 if (rp->rp_portname[0] == '\0' || 983 ifunit(rp->rp_portname) != ifp) { 984 error = EINVAL; 985 break; 986 } 987 988 LAGG_RLOCK(sc, &tracker); 989 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) { 990 error = ENOENT; 991 LAGG_RUNLOCK(sc, &tracker); 992 break; 993 } 994 995 lagg_port2req(lp, rp); 996 LAGG_RUNLOCK(sc, &tracker); 997 break; 998 999 case SIOCSIFCAP: 1000 if (lp->lp_ioctl == NULL) { 1001 error = EINVAL; 1002 break; 1003 } 1004 error = (*lp->lp_ioctl)(ifp, cmd, data); 1005 if (error) 1006 break; 1007 1008 /* Update lagg interface capabilities */ 1009 LAGG_WLOCK(sc); 1010 lagg_capabilities(sc); 1011 LAGG_WUNLOCK(sc); 1012 break; 1013 1014 case SIOCSIFMTU: 1015 /* Do not allow the MTU to be changed once joined */ 1016 error = EINVAL; 1017 break; 1018 1019 default: 1020 goto fallback; 1021 } 1022 1023 return (error); 1024 1025 fallback: 1026 if (lp != NULL && lp->lp_ioctl != NULL) 1027 return ((*lp->lp_ioctl)(ifp, cmd, data)); 1028 1029 return (EINVAL); 1030 } 1031 1032 /* 1033 * Requests counter @cnt data. 1034 * 1035 * Counter value is calculated the following way: 1036 * 1) for each port, sum difference between current and "initial" measurements. 1037 * 2) add lagg logical interface counters. 1038 * 3) add data from detached_counters array. 1039 * 1040 * We also do the following things on ports attach/detach: 1041 * 1) On port attach we store all counters it has into port_counter array. 1042 * 2) On port detach we add the different between "initial" and 1043 * current counters data to detached_counters array. 1044 */ 1045 static uint64_t 1046 lagg_get_counter(struct ifnet *ifp, ift_counter cnt) 1047 { 1048 struct lagg_softc *sc; 1049 struct lagg_port *lp; 1050 struct ifnet *lpifp; 1051 struct rm_priotracker tracker; 1052 uint64_t newval, oldval, vsum; 1053 1054 /* Revise this when we've got non-generic counters. */ 1055 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1056 1057 sc = (struct lagg_softc *)ifp->if_softc; 1058 LAGG_RLOCK(sc, &tracker); 1059 1060 vsum = 0; 1061 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1062 /* Saved attached value */ 1063 oldval = lp->port_counters.val[cnt]; 1064 /* current value */ 1065 lpifp = lp->lp_ifp; 1066 newval = lpifp->if_get_counter(lpifp, cnt); 1067 /* Calculate diff and save new */ 1068 vsum += newval - oldval; 1069 } 1070 1071 /* 1072 * Add counter data which might be added by upper 1073 * layer protocols operating on logical interface. 1074 */ 1075 vsum += if_get_counter_default(ifp, cnt); 1076 1077 /* 1078 * Add counter data from detached ports counters 1079 */ 1080 vsum += sc->detached_counters.val[cnt]; 1081 1082 LAGG_RUNLOCK(sc, &tracker); 1083 1084 return (vsum); 1085 } 1086 1087 /* 1088 * For direct output to child ports. 1089 */ 1090 static int 1091 lagg_port_output(struct ifnet *ifp, struct mbuf *m, 1092 const struct sockaddr *dst, struct route *ro) 1093 { 1094 struct lagg_port *lp = ifp->if_lagg; 1095 1096 switch (dst->sa_family) { 1097 case pseudo_AF_HDRCMPLT: 1098 case AF_UNSPEC: 1099 return ((*lp->lp_output)(ifp, m, dst, ro)); 1100 } 1101 1102 /* drop any other frames */ 1103 m_freem(m); 1104 return (ENETDOWN); 1105 } 1106 1107 static void 1108 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp) 1109 { 1110 struct lagg_port *lp; 1111 struct lagg_softc *sc; 1112 1113 if ((lp = ifp->if_lagg) == NULL) 1114 return; 1115 /* If the ifnet is just being renamed, don't do anything. */ 1116 if (ifp->if_flags & IFF_RENAMING) 1117 return; 1118 1119 sc = lp->lp_softc; 1120 1121 LAGG_WLOCK(sc); 1122 lp->lp_detaching = 1; 1123 lagg_port_destroy(lp, 1); 1124 LAGG_WUNLOCK(sc); 1125 } 1126 1127 static void 1128 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp) 1129 { 1130 struct lagg_softc *sc = lp->lp_softc; 1131 1132 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname)); 1133 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname)); 1134 rp->rp_prio = lp->lp_prio; 1135 rp->rp_flags = lp->lp_flags; 1136 lagg_proto_portreq(sc, lp, &rp->rp_psc); 1137 1138 /* Add protocol specific flags */ 1139 switch (sc->sc_proto) { 1140 case LAGG_PROTO_FAILOVER: 1141 if (lp == sc->sc_primary) 1142 rp->rp_flags |= LAGG_PORT_MASTER; 1143 if (lp == lagg_link_active(sc, sc->sc_primary)) 1144 rp->rp_flags |= LAGG_PORT_ACTIVE; 1145 break; 1146 1147 case LAGG_PROTO_ROUNDROBIN: 1148 case LAGG_PROTO_LOADBALANCE: 1149 case LAGG_PROTO_BROADCAST: 1150 if (LAGG_PORTACTIVE(lp)) 1151 rp->rp_flags |= LAGG_PORT_ACTIVE; 1152 break; 1153 1154 case LAGG_PROTO_LACP: 1155 /* LACP has a different definition of active */ 1156 if (lacp_isactive(lp)) 1157 rp->rp_flags |= LAGG_PORT_ACTIVE; 1158 if (lacp_iscollecting(lp)) 1159 rp->rp_flags |= LAGG_PORT_COLLECTING; 1160 if (lacp_isdistributing(lp)) 1161 rp->rp_flags |= LAGG_PORT_DISTRIBUTING; 1162 break; 1163 } 1164 1165 } 1166 1167 static void 1168 lagg_init(void *xsc) 1169 { 1170 struct lagg_softc *sc = (struct lagg_softc *)xsc; 1171 struct ifnet *ifp = sc->sc_ifp; 1172 struct lagg_port *lp; 1173 1174 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1175 return; 1176 1177 LAGG_WLOCK(sc); 1178 1179 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1180 1181 /* 1182 * Update the port lladdrs if needed. 1183 * This might be if_setlladdr() notification 1184 * that lladdr has been changed. 1185 */ 1186 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1187 lagg_port_lladdr(lp, IF_LLADDR(ifp), LAGG_LLQTYPE_PHYS); 1188 1189 lagg_proto_init(sc); 1190 1191 LAGG_WUNLOCK(sc); 1192 } 1193 1194 static void 1195 lagg_stop(struct lagg_softc *sc) 1196 { 1197 struct ifnet *ifp = sc->sc_ifp; 1198 1199 LAGG_WLOCK_ASSERT(sc); 1200 1201 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1202 return; 1203 1204 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1205 1206 lagg_proto_stop(sc); 1207 } 1208 1209 static int 1210 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1211 { 1212 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1213 struct lagg_reqall *ra = (struct lagg_reqall *)data; 1214 struct lagg_reqopts *ro = (struct lagg_reqopts *)data; 1215 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf; 1216 struct lagg_reqflags *rf = (struct lagg_reqflags *)data; 1217 struct ifreq *ifr = (struct ifreq *)data; 1218 struct lagg_port *lp; 1219 struct ifnet *tpif; 1220 struct thread *td = curthread; 1221 char *buf, *outbuf; 1222 int count, buflen, len, error = 0; 1223 struct rm_priotracker tracker; 1224 1225 bzero(&rpbuf, sizeof(rpbuf)); 1226 1227 switch (cmd) { 1228 case SIOCGLAGG: 1229 LAGG_RLOCK(sc, &tracker); 1230 count = 0; 1231 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1232 count++; 1233 buflen = count * sizeof(struct lagg_reqport); 1234 LAGG_RUNLOCK(sc, &tracker); 1235 1236 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 1237 1238 LAGG_RLOCK(sc, &tracker); 1239 ra->ra_proto = sc->sc_proto; 1240 lagg_proto_request(sc, &ra->ra_psc); 1241 count = 0; 1242 buf = outbuf; 1243 len = min(ra->ra_size, buflen); 1244 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1245 if (len < sizeof(rpbuf)) 1246 break; 1247 1248 lagg_port2req(lp, &rpbuf); 1249 memcpy(buf, &rpbuf, sizeof(rpbuf)); 1250 count++; 1251 buf += sizeof(rpbuf); 1252 len -= sizeof(rpbuf); 1253 } 1254 LAGG_RUNLOCK(sc, &tracker); 1255 ra->ra_ports = count; 1256 ra->ra_size = count * sizeof(rpbuf); 1257 error = copyout(outbuf, ra->ra_port, ra->ra_size); 1258 free(outbuf, M_TEMP); 1259 break; 1260 case SIOCSLAGG: 1261 error = priv_check(td, PRIV_NET_LAGG); 1262 if (error) 1263 break; 1264 if (ra->ra_proto >= LAGG_PROTO_MAX) { 1265 error = EPROTONOSUPPORT; 1266 break; 1267 } 1268 1269 LAGG_WLOCK(sc); 1270 lagg_proto_detach(sc); 1271 LAGG_UNLOCK_ASSERT(sc); 1272 lagg_proto_attach(sc, ra->ra_proto); 1273 break; 1274 case SIOCGLAGGOPTS: 1275 ro->ro_opts = sc->sc_opts; 1276 if (sc->sc_proto == LAGG_PROTO_LACP) { 1277 struct lacp_softc *lsc; 1278 1279 lsc = (struct lacp_softc *)sc->sc_psc; 1280 if (lsc->lsc_debug.lsc_tx_test != 0) 1281 ro->ro_opts |= LAGG_OPT_LACP_TXTEST; 1282 if (lsc->lsc_debug.lsc_rx_test != 0) 1283 ro->ro_opts |= LAGG_OPT_LACP_RXTEST; 1284 if (lsc->lsc_strict_mode != 0) 1285 ro->ro_opts |= LAGG_OPT_LACP_STRICT; 1286 if (lsc->lsc_fast_timeout != 0) 1287 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT; 1288 1289 ro->ro_active = sc->sc_active; 1290 } else { 1291 ro->ro_active = 0; 1292 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1293 ro->ro_active += LAGG_PORTACTIVE(lp); 1294 } 1295 ro->ro_bkt = sc->sc_bkt; 1296 ro->ro_flapping = sc->sc_flapping; 1297 ro->ro_flowid_shift = sc->flowid_shift; 1298 break; 1299 case SIOCSLAGGOPTS: 1300 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) { 1301 if (ro->ro_bkt == 0) 1302 sc->sc_bkt = 1; // Minimum 1 packet per iface. 1303 else 1304 sc->sc_bkt = ro->ro_bkt; 1305 } 1306 error = priv_check(td, PRIV_NET_LAGG); 1307 if (error) 1308 break; 1309 if (ro->ro_opts == 0) 1310 break; 1311 /* 1312 * Set options. LACP options are stored in sc->sc_psc, 1313 * not in sc_opts. 1314 */ 1315 int valid, lacp; 1316 1317 switch (ro->ro_opts) { 1318 case LAGG_OPT_USE_FLOWID: 1319 case -LAGG_OPT_USE_FLOWID: 1320 case LAGG_OPT_FLOWIDSHIFT: 1321 valid = 1; 1322 lacp = 0; 1323 break; 1324 case LAGG_OPT_LACP_TXTEST: 1325 case -LAGG_OPT_LACP_TXTEST: 1326 case LAGG_OPT_LACP_RXTEST: 1327 case -LAGG_OPT_LACP_RXTEST: 1328 case LAGG_OPT_LACP_STRICT: 1329 case -LAGG_OPT_LACP_STRICT: 1330 case LAGG_OPT_LACP_TIMEOUT: 1331 case -LAGG_OPT_LACP_TIMEOUT: 1332 valid = lacp = 1; 1333 break; 1334 default: 1335 valid = lacp = 0; 1336 break; 1337 } 1338 1339 LAGG_WLOCK(sc); 1340 1341 if (valid == 0 || 1342 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) { 1343 /* Invalid combination of options specified. */ 1344 error = EINVAL; 1345 LAGG_WUNLOCK(sc); 1346 break; /* Return from SIOCSLAGGOPTS. */ 1347 } 1348 /* 1349 * Store new options into sc->sc_opts except for 1350 * FLOWIDSHIFT and LACP options. 1351 */ 1352 if (lacp == 0) { 1353 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT) 1354 sc->flowid_shift = ro->ro_flowid_shift; 1355 else if (ro->ro_opts > 0) 1356 sc->sc_opts |= ro->ro_opts; 1357 else 1358 sc->sc_opts &= ~ro->ro_opts; 1359 } else { 1360 struct lacp_softc *lsc; 1361 struct lacp_port *lp; 1362 1363 lsc = (struct lacp_softc *)sc->sc_psc; 1364 1365 switch (ro->ro_opts) { 1366 case LAGG_OPT_LACP_TXTEST: 1367 lsc->lsc_debug.lsc_tx_test = 1; 1368 break; 1369 case -LAGG_OPT_LACP_TXTEST: 1370 lsc->lsc_debug.lsc_tx_test = 0; 1371 break; 1372 case LAGG_OPT_LACP_RXTEST: 1373 lsc->lsc_debug.lsc_rx_test = 1; 1374 break; 1375 case -LAGG_OPT_LACP_RXTEST: 1376 lsc->lsc_debug.lsc_rx_test = 0; 1377 break; 1378 case LAGG_OPT_LACP_STRICT: 1379 lsc->lsc_strict_mode = 1; 1380 break; 1381 case -LAGG_OPT_LACP_STRICT: 1382 lsc->lsc_strict_mode = 0; 1383 break; 1384 case LAGG_OPT_LACP_TIMEOUT: 1385 LACP_LOCK(lsc); 1386 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) 1387 lp->lp_state |= LACP_STATE_TIMEOUT; 1388 LACP_UNLOCK(lsc); 1389 lsc->lsc_fast_timeout = 1; 1390 break; 1391 case -LAGG_OPT_LACP_TIMEOUT: 1392 LACP_LOCK(lsc); 1393 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) 1394 lp->lp_state &= ~LACP_STATE_TIMEOUT; 1395 LACP_UNLOCK(lsc); 1396 lsc->lsc_fast_timeout = 0; 1397 break; 1398 } 1399 } 1400 LAGG_WUNLOCK(sc); 1401 break; 1402 case SIOCGLAGGFLAGS: 1403 rf->rf_flags = 0; 1404 LAGG_RLOCK(sc, &tracker); 1405 if (sc->sc_flags & MBUF_HASHFLAG_L2) 1406 rf->rf_flags |= LAGG_F_HASHL2; 1407 if (sc->sc_flags & MBUF_HASHFLAG_L3) 1408 rf->rf_flags |= LAGG_F_HASHL3; 1409 if (sc->sc_flags & MBUF_HASHFLAG_L4) 1410 rf->rf_flags |= LAGG_F_HASHL4; 1411 LAGG_RUNLOCK(sc, &tracker); 1412 break; 1413 case SIOCSLAGGHASH: 1414 error = priv_check(td, PRIV_NET_LAGG); 1415 if (error) 1416 break; 1417 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) { 1418 error = EINVAL; 1419 break; 1420 } 1421 LAGG_WLOCK(sc); 1422 sc->sc_flags = 0; 1423 if (rf->rf_flags & LAGG_F_HASHL2) 1424 sc->sc_flags |= MBUF_HASHFLAG_L2; 1425 if (rf->rf_flags & LAGG_F_HASHL3) 1426 sc->sc_flags |= MBUF_HASHFLAG_L3; 1427 if (rf->rf_flags & LAGG_F_HASHL4) 1428 sc->sc_flags |= MBUF_HASHFLAG_L4; 1429 LAGG_WUNLOCK(sc); 1430 break; 1431 case SIOCGLAGGPORT: 1432 if (rp->rp_portname[0] == '\0' || 1433 (tpif = ifunit(rp->rp_portname)) == NULL) { 1434 error = EINVAL; 1435 break; 1436 } 1437 1438 LAGG_RLOCK(sc, &tracker); 1439 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1440 lp->lp_softc != sc) { 1441 error = ENOENT; 1442 LAGG_RUNLOCK(sc, &tracker); 1443 break; 1444 } 1445 1446 lagg_port2req(lp, rp); 1447 LAGG_RUNLOCK(sc, &tracker); 1448 break; 1449 case SIOCSLAGGPORT: 1450 error = priv_check(td, PRIV_NET_LAGG); 1451 if (error) 1452 break; 1453 if (rp->rp_portname[0] == '\0' || 1454 (tpif = ifunit(rp->rp_portname)) == NULL) { 1455 error = EINVAL; 1456 break; 1457 } 1458 #ifdef INET6 1459 /* 1460 * A laggport interface should not have inet6 address 1461 * because two interfaces with a valid link-local 1462 * scope zone must not be merged in any form. This 1463 * restriction is needed to prevent violation of 1464 * link-local scope zone. Attempts to add a laggport 1465 * interface which has inet6 addresses triggers 1466 * removal of all inet6 addresses on the member 1467 * interface. 1468 */ 1469 if (in6ifa_llaonifp(tpif)) { 1470 in6_ifdetach(tpif); 1471 if_printf(sc->sc_ifp, 1472 "IPv6 addresses on %s have been removed " 1473 "before adding it as a member to prevent " 1474 "IPv6 address scope violation.\n", 1475 tpif->if_xname); 1476 } 1477 #endif 1478 LAGG_WLOCK(sc); 1479 error = lagg_port_create(sc, tpif); 1480 LAGG_WUNLOCK(sc); 1481 break; 1482 case SIOCSLAGGDELPORT: 1483 error = priv_check(td, PRIV_NET_LAGG); 1484 if (error) 1485 break; 1486 if (rp->rp_portname[0] == '\0' || 1487 (tpif = ifunit(rp->rp_portname)) == NULL) { 1488 error = EINVAL; 1489 break; 1490 } 1491 1492 LAGG_WLOCK(sc); 1493 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1494 lp->lp_softc != sc) { 1495 error = ENOENT; 1496 LAGG_WUNLOCK(sc); 1497 break; 1498 } 1499 1500 error = lagg_port_destroy(lp, 1); 1501 LAGG_WUNLOCK(sc); 1502 break; 1503 case SIOCSIFFLAGS: 1504 /* Set flags on ports too */ 1505 LAGG_WLOCK(sc); 1506 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1507 lagg_setflags(lp, 1); 1508 } 1509 LAGG_WUNLOCK(sc); 1510 1511 if (!(ifp->if_flags & IFF_UP) && 1512 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1513 /* 1514 * If interface is marked down and it is running, 1515 * then stop and disable it. 1516 */ 1517 LAGG_WLOCK(sc); 1518 lagg_stop(sc); 1519 LAGG_WUNLOCK(sc); 1520 } else if ((ifp->if_flags & IFF_UP) && 1521 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1522 /* 1523 * If interface is marked up and it is stopped, then 1524 * start it. 1525 */ 1526 (*ifp->if_init)(sc); 1527 } 1528 break; 1529 case SIOCADDMULTI: 1530 case SIOCDELMULTI: 1531 LAGG_WLOCK(sc); 1532 error = lagg_ether_setmulti(sc); 1533 LAGG_WUNLOCK(sc); 1534 break; 1535 case SIOCSIFMEDIA: 1536 case SIOCGIFMEDIA: 1537 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1538 break; 1539 1540 case SIOCSIFCAP: 1541 case SIOCSIFMTU: 1542 /* Do not allow the MTU or caps to be directly changed */ 1543 error = EINVAL; 1544 break; 1545 1546 default: 1547 error = ether_ioctl(ifp, cmd, data); 1548 break; 1549 } 1550 return (error); 1551 } 1552 1553 static int 1554 lagg_ether_setmulti(struct lagg_softc *sc) 1555 { 1556 struct lagg_port *lp; 1557 1558 LAGG_WLOCK_ASSERT(sc); 1559 1560 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1561 /* First, remove any existing filter entries. */ 1562 lagg_ether_cmdmulti(lp, 0); 1563 /* copy all addresses from the lagg interface to the port */ 1564 lagg_ether_cmdmulti(lp, 1); 1565 } 1566 return (0); 1567 } 1568 1569 static int 1570 lagg_ether_cmdmulti(struct lagg_port *lp, int set) 1571 { 1572 struct lagg_softc *sc = lp->lp_softc; 1573 struct ifnet *ifp = lp->lp_ifp; 1574 struct ifnet *scifp = sc->sc_ifp; 1575 struct lagg_mc *mc; 1576 struct ifmultiaddr *ifma; 1577 int error; 1578 1579 LAGG_WLOCK_ASSERT(sc); 1580 1581 if (set) { 1582 IF_ADDR_WLOCK(scifp); 1583 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) { 1584 if (ifma->ifma_addr->sa_family != AF_LINK) 1585 continue; 1586 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT); 1587 if (mc == NULL) { 1588 IF_ADDR_WUNLOCK(scifp); 1589 return (ENOMEM); 1590 } 1591 bcopy(ifma->ifma_addr, &mc->mc_addr, 1592 ifma->ifma_addr->sa_len); 1593 mc->mc_addr.sdl_index = ifp->if_index; 1594 mc->mc_ifma = NULL; 1595 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries); 1596 } 1597 IF_ADDR_WUNLOCK(scifp); 1598 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) { 1599 error = if_addmulti(ifp, 1600 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma); 1601 if (error) 1602 return (error); 1603 } 1604 } else { 1605 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) { 1606 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries); 1607 if (mc->mc_ifma && !lp->lp_detaching) 1608 if_delmulti_ifma(mc->mc_ifma); 1609 free(mc, M_DEVBUF); 1610 } 1611 } 1612 return (0); 1613 } 1614 1615 /* Handle a ref counted flag that should be set on the lagg port as well */ 1616 static int 1617 lagg_setflag(struct lagg_port *lp, int flag, int status, 1618 int (*func)(struct ifnet *, int)) 1619 { 1620 struct lagg_softc *sc = lp->lp_softc; 1621 struct ifnet *scifp = sc->sc_ifp; 1622 struct ifnet *ifp = lp->lp_ifp; 1623 int error; 1624 1625 LAGG_WLOCK_ASSERT(sc); 1626 1627 status = status ? (scifp->if_flags & flag) : 0; 1628 /* Now "status" contains the flag value or 0 */ 1629 1630 /* 1631 * See if recorded ports status is different from what 1632 * we want it to be. If it is, flip it. We record ports 1633 * status in lp_ifflags so that we won't clear ports flag 1634 * we haven't set. In fact, we don't clear or set ports 1635 * flags directly, but get or release references to them. 1636 * That's why we can be sure that recorded flags still are 1637 * in accord with actual ports flags. 1638 */ 1639 if (status != (lp->lp_ifflags & flag)) { 1640 error = (*func)(ifp, status); 1641 if (error) 1642 return (error); 1643 lp->lp_ifflags &= ~flag; 1644 lp->lp_ifflags |= status; 1645 } 1646 return (0); 1647 } 1648 1649 /* 1650 * Handle IFF_* flags that require certain changes on the lagg port 1651 * if "status" is true, update ports flags respective to the lagg 1652 * if "status" is false, forcedly clear the flags set on port. 1653 */ 1654 static int 1655 lagg_setflags(struct lagg_port *lp, int status) 1656 { 1657 int error, i; 1658 1659 for (i = 0; lagg_pflags[i].flag; i++) { 1660 error = lagg_setflag(lp, lagg_pflags[i].flag, 1661 status, lagg_pflags[i].func); 1662 if (error) 1663 return (error); 1664 } 1665 return (0); 1666 } 1667 1668 static int 1669 lagg_transmit(struct ifnet *ifp, struct mbuf *m) 1670 { 1671 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1672 int error, len, mcast; 1673 struct rm_priotracker tracker; 1674 1675 len = m->m_pkthdr.len; 1676 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 1677 1678 LAGG_RLOCK(sc, &tracker); 1679 /* We need a Tx algorithm and at least one port */ 1680 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) { 1681 LAGG_RUNLOCK(sc, &tracker); 1682 m_freem(m); 1683 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1684 return (ENXIO); 1685 } 1686 1687 ETHER_BPF_MTAP(ifp, m); 1688 1689 error = lagg_proto_start(sc, m); 1690 LAGG_RUNLOCK(sc, &tracker); 1691 1692 if (error != 0) 1693 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1694 1695 return (error); 1696 } 1697 1698 /* 1699 * The ifp->if_qflush entry point for lagg(4) is no-op. 1700 */ 1701 static void 1702 lagg_qflush(struct ifnet *ifp __unused) 1703 { 1704 } 1705 1706 static struct mbuf * 1707 lagg_input(struct ifnet *ifp, struct mbuf *m) 1708 { 1709 struct lagg_port *lp = ifp->if_lagg; 1710 struct lagg_softc *sc = lp->lp_softc; 1711 struct ifnet *scifp = sc->sc_ifp; 1712 struct rm_priotracker tracker; 1713 1714 LAGG_RLOCK(sc, &tracker); 1715 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1716 (lp->lp_flags & LAGG_PORT_DISABLED) || 1717 sc->sc_proto == LAGG_PROTO_NONE) { 1718 LAGG_RUNLOCK(sc, &tracker); 1719 m_freem(m); 1720 return (NULL); 1721 } 1722 1723 ETHER_BPF_MTAP(scifp, m); 1724 1725 if (lp->lp_detaching != 0) { 1726 m_freem(m); 1727 m = NULL; 1728 } else 1729 m = lagg_proto_input(sc, lp, m); 1730 1731 if (m != NULL) { 1732 if (scifp->if_flags & IFF_MONITOR) { 1733 m_freem(m); 1734 m = NULL; 1735 } 1736 } 1737 1738 LAGG_RUNLOCK(sc, &tracker); 1739 return (m); 1740 } 1741 1742 static int 1743 lagg_media_change(struct ifnet *ifp) 1744 { 1745 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1746 1747 if (sc->sc_ifflags & IFF_DEBUG) 1748 printf("%s\n", __func__); 1749 1750 /* Ignore */ 1751 return (0); 1752 } 1753 1754 static void 1755 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1756 { 1757 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1758 struct lagg_port *lp; 1759 struct rm_priotracker tracker; 1760 1761 imr->ifm_status = IFM_AVALID; 1762 imr->ifm_active = IFM_ETHER | IFM_AUTO; 1763 1764 LAGG_RLOCK(sc, &tracker); 1765 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1766 if (LAGG_PORTACTIVE(lp)) 1767 imr->ifm_status |= IFM_ACTIVE; 1768 } 1769 LAGG_RUNLOCK(sc, &tracker); 1770 } 1771 1772 static void 1773 lagg_linkstate(struct lagg_softc *sc) 1774 { 1775 struct lagg_port *lp; 1776 int new_link = LINK_STATE_DOWN; 1777 uint64_t speed; 1778 1779 /* Our link is considered up if at least one of our ports is active */ 1780 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1781 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) { 1782 new_link = LINK_STATE_UP; 1783 break; 1784 } 1785 } 1786 if_link_state_change(sc->sc_ifp, new_link); 1787 1788 /* Update if_baudrate to reflect the max possible speed */ 1789 switch (sc->sc_proto) { 1790 case LAGG_PROTO_FAILOVER: 1791 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ? 1792 sc->sc_primary->lp_ifp->if_baudrate : 0; 1793 break; 1794 case LAGG_PROTO_ROUNDROBIN: 1795 case LAGG_PROTO_LOADBALANCE: 1796 case LAGG_PROTO_BROADCAST: 1797 speed = 0; 1798 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1799 speed += lp->lp_ifp->if_baudrate; 1800 sc->sc_ifp->if_baudrate = speed; 1801 break; 1802 case LAGG_PROTO_LACP: 1803 /* LACP updates if_baudrate itself */ 1804 break; 1805 } 1806 } 1807 1808 static void 1809 lagg_port_state(struct ifnet *ifp, int state) 1810 { 1811 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg; 1812 struct lagg_softc *sc = NULL; 1813 1814 if (lp != NULL) 1815 sc = lp->lp_softc; 1816 if (sc == NULL) 1817 return; 1818 1819 LAGG_WLOCK(sc); 1820 lagg_linkstate(sc); 1821 lagg_proto_linkstate(sc, lp); 1822 LAGG_WUNLOCK(sc); 1823 } 1824 1825 struct lagg_port * 1826 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp) 1827 { 1828 struct lagg_port *lp_next, *rval = NULL; 1829 // int new_link = LINK_STATE_DOWN; 1830 1831 LAGG_RLOCK_ASSERT(sc); 1832 /* 1833 * Search a port which reports an active link state. 1834 */ 1835 1836 if (lp == NULL) 1837 goto search; 1838 if (LAGG_PORTACTIVE(lp)) { 1839 rval = lp; 1840 goto found; 1841 } 1842 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL && 1843 LAGG_PORTACTIVE(lp_next)) { 1844 rval = lp_next; 1845 goto found; 1846 } 1847 1848 search: 1849 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1850 if (LAGG_PORTACTIVE(lp_next)) { 1851 rval = lp_next; 1852 goto found; 1853 } 1854 } 1855 1856 found: 1857 if (rval != NULL) { 1858 /* 1859 * The IEEE 802.1D standard assumes that a lagg with 1860 * multiple ports is always full duplex. This is valid 1861 * for load sharing laggs and if at least two links 1862 * are active. Unfortunately, checking the latter would 1863 * be too expensive at this point. 1864 XXX 1865 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) && 1866 (sc->sc_count > 1)) 1867 new_link = LINK_STATE_FULL_DUPLEX; 1868 else 1869 new_link = rval->lp_link_state; 1870 */ 1871 } 1872 1873 return (rval); 1874 } 1875 1876 int 1877 lagg_enqueue(struct ifnet *ifp, struct mbuf *m) 1878 { 1879 1880 return (ifp->if_transmit)(ifp, m); 1881 } 1882 1883 /* 1884 * Simple round robin aggregation 1885 */ 1886 static void 1887 lagg_rr_attach(struct lagg_softc *sc) 1888 { 1889 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1890 sc->sc_seq = 0; 1891 sc->sc_bkt_count = sc->sc_bkt; 1892 } 1893 1894 static int 1895 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m) 1896 { 1897 struct lagg_port *lp; 1898 uint32_t p; 1899 1900 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0) 1901 sc->sc_bkt_count = sc->sc_bkt; 1902 1903 if (sc->sc_bkt > 0) { 1904 atomic_subtract_int(&sc->sc_bkt_count, 1); 1905 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt)) 1906 p = atomic_fetchadd_32(&sc->sc_seq, 1); 1907 else 1908 p = sc->sc_seq; 1909 } else 1910 p = atomic_fetchadd_32(&sc->sc_seq, 1); 1911 1912 p %= sc->sc_count; 1913 lp = SLIST_FIRST(&sc->sc_ports); 1914 1915 while (p--) 1916 lp = SLIST_NEXT(lp, lp_entries); 1917 1918 /* 1919 * Check the port's link state. This will return the next active 1920 * port if the link is down or the port is NULL. 1921 */ 1922 if ((lp = lagg_link_active(sc, lp)) == NULL) { 1923 m_freem(m); 1924 return (ENETDOWN); 1925 } 1926 1927 /* Send mbuf */ 1928 return (lagg_enqueue(lp->lp_ifp, m)); 1929 } 1930 1931 static struct mbuf * 1932 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1933 { 1934 struct ifnet *ifp = sc->sc_ifp; 1935 1936 /* Just pass in the packet to our lagg device */ 1937 m->m_pkthdr.rcvif = ifp; 1938 1939 return (m); 1940 } 1941 1942 /* 1943 * Broadcast mode 1944 */ 1945 static int 1946 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m) 1947 { 1948 int active_ports = 0; 1949 int errors = 0; 1950 int ret; 1951 struct lagg_port *lp, *last = NULL; 1952 struct mbuf *m0; 1953 1954 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1955 if (!LAGG_PORTACTIVE(lp)) 1956 continue; 1957 1958 active_ports++; 1959 1960 if (last != NULL) { 1961 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT); 1962 if (m0 == NULL) { 1963 ret = ENOBUFS; 1964 errors++; 1965 break; 1966 } 1967 1968 ret = lagg_enqueue(last->lp_ifp, m0); 1969 if (ret != 0) 1970 errors++; 1971 } 1972 last = lp; 1973 } 1974 if (last == NULL) { 1975 m_freem(m); 1976 return (ENOENT); 1977 } 1978 if ((last = lagg_link_active(sc, last)) == NULL) { 1979 m_freem(m); 1980 return (ENETDOWN); 1981 } 1982 1983 ret = lagg_enqueue(last->lp_ifp, m); 1984 if (ret != 0) 1985 errors++; 1986 1987 if (errors == 0) 1988 return (ret); 1989 1990 return (0); 1991 } 1992 1993 static struct mbuf* 1994 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1995 { 1996 struct ifnet *ifp = sc->sc_ifp; 1997 1998 /* Just pass in the packet to our lagg device */ 1999 m->m_pkthdr.rcvif = ifp; 2000 return (m); 2001 } 2002 2003 /* 2004 * Active failover 2005 */ 2006 static int 2007 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m) 2008 { 2009 struct lagg_port *lp; 2010 2011 /* Use the master port if active or the next available port */ 2012 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) { 2013 m_freem(m); 2014 return (ENETDOWN); 2015 } 2016 2017 /* Send mbuf */ 2018 return (lagg_enqueue(lp->lp_ifp, m)); 2019 } 2020 2021 static struct mbuf * 2022 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 2023 { 2024 struct ifnet *ifp = sc->sc_ifp; 2025 struct lagg_port *tmp_tp; 2026 2027 if (lp == sc->sc_primary || V_lagg_failover_rx_all) { 2028 m->m_pkthdr.rcvif = ifp; 2029 return (m); 2030 } 2031 2032 if (!LAGG_PORTACTIVE(sc->sc_primary)) { 2033 tmp_tp = lagg_link_active(sc, sc->sc_primary); 2034 /* 2035 * If tmp_tp is null, we've received a packet when all 2036 * our links are down. Weird, but process it anyways. 2037 */ 2038 if ((tmp_tp == NULL || tmp_tp == lp)) { 2039 m->m_pkthdr.rcvif = ifp; 2040 return (m); 2041 } 2042 } 2043 2044 m_freem(m); 2045 return (NULL); 2046 } 2047 2048 /* 2049 * Loadbalancing 2050 */ 2051 static void 2052 lagg_lb_attach(struct lagg_softc *sc) 2053 { 2054 struct lagg_port *lp; 2055 struct lagg_lb *lb; 2056 2057 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO); 2058 2059 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 2060 2061 lb->lb_key = m_ether_tcpip_hash_init(); 2062 sc->sc_psc = lb; 2063 2064 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2065 lagg_lb_port_create(lp); 2066 } 2067 2068 static void 2069 lagg_lb_detach(struct lagg_softc *sc) 2070 { 2071 struct lagg_lb *lb; 2072 2073 lb = (struct lagg_lb *)sc->sc_psc; 2074 LAGG_WUNLOCK(sc); 2075 if (lb != NULL) 2076 free(lb, M_DEVBUF); 2077 } 2078 2079 static int 2080 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp) 2081 { 2082 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 2083 struct lagg_port *lp_next; 2084 int i = 0; 2085 2086 bzero(&lb->lb_ports, sizeof(lb->lb_ports)); 2087 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 2088 if (lp_next == lp) 2089 continue; 2090 if (i >= LAGG_MAX_PORTS) 2091 return (EINVAL); 2092 if (sc->sc_ifflags & IFF_DEBUG) 2093 printf("%s: port %s at index %d\n", 2094 sc->sc_ifname, lp_next->lp_ifp->if_xname, i); 2095 lb->lb_ports[i++] = lp_next; 2096 } 2097 2098 return (0); 2099 } 2100 2101 static int 2102 lagg_lb_port_create(struct lagg_port *lp) 2103 { 2104 struct lagg_softc *sc = lp->lp_softc; 2105 return (lagg_lb_porttable(sc, NULL)); 2106 } 2107 2108 static void 2109 lagg_lb_port_destroy(struct lagg_port *lp) 2110 { 2111 struct lagg_softc *sc = lp->lp_softc; 2112 lagg_lb_porttable(sc, lp); 2113 } 2114 2115 static int 2116 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m) 2117 { 2118 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 2119 struct lagg_port *lp = NULL; 2120 uint32_t p = 0; 2121 2122 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) && 2123 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 2124 p = m->m_pkthdr.flowid >> sc->flowid_shift; 2125 else 2126 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key); 2127 p %= sc->sc_count; 2128 lp = lb->lb_ports[p]; 2129 2130 /* 2131 * Check the port's link state. This will return the next active 2132 * port if the link is down or the port is NULL. 2133 */ 2134 if ((lp = lagg_link_active(sc, lp)) == NULL) { 2135 m_freem(m); 2136 return (ENETDOWN); 2137 } 2138 2139 /* Send mbuf */ 2140 return (lagg_enqueue(lp->lp_ifp, m)); 2141 } 2142 2143 static struct mbuf * 2144 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 2145 { 2146 struct ifnet *ifp = sc->sc_ifp; 2147 2148 /* Just pass in the packet to our lagg device */ 2149 m->m_pkthdr.rcvif = ifp; 2150 2151 return (m); 2152 } 2153 2154 /* 2155 * 802.3ad LACP 2156 */ 2157 static void 2158 lagg_lacp_attach(struct lagg_softc *sc) 2159 { 2160 struct lagg_port *lp; 2161 2162 lacp_attach(sc); 2163 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2164 lacp_port_create(lp); 2165 } 2166 2167 static void 2168 lagg_lacp_detach(struct lagg_softc *sc) 2169 { 2170 struct lagg_port *lp; 2171 void *psc; 2172 2173 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2174 lacp_port_destroy(lp); 2175 2176 psc = sc->sc_psc; 2177 sc->sc_psc = NULL; 2178 LAGG_WUNLOCK(sc); 2179 2180 lacp_detach(psc); 2181 } 2182 2183 static void 2184 lagg_lacp_lladdr(struct lagg_softc *sc) 2185 { 2186 struct lagg_port *lp; 2187 2188 /* purge all the lacp ports */ 2189 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2190 lacp_port_destroy(lp); 2191 2192 /* add them back in */ 2193 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2194 lacp_port_create(lp); 2195 } 2196 2197 static int 2198 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m) 2199 { 2200 struct lagg_port *lp; 2201 2202 lp = lacp_select_tx_port(sc, m); 2203 if (lp == NULL) { 2204 m_freem(m); 2205 return (ENETDOWN); 2206 } 2207 2208 /* Send mbuf */ 2209 return (lagg_enqueue(lp->lp_ifp, m)); 2210 } 2211 2212 static struct mbuf * 2213 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 2214 { 2215 struct ifnet *ifp = sc->sc_ifp; 2216 struct ether_header *eh; 2217 u_short etype; 2218 2219 eh = mtod(m, struct ether_header *); 2220 etype = ntohs(eh->ether_type); 2221 2222 /* Tap off LACP control messages */ 2223 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) { 2224 m = lacp_input(lp, m); 2225 if (m == NULL) 2226 return (NULL); 2227 } 2228 2229 /* 2230 * If the port is not collecting or not in the active aggregator then 2231 * free and return. 2232 */ 2233 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) { 2234 m_freem(m); 2235 return (NULL); 2236 } 2237 2238 m->m_pkthdr.rcvif = ifp; 2239 return (m); 2240 } 2241 2242