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 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 *); 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 static void 256 vnet_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 } 264 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 265 vnet_lagg_init, NULL); 266 267 static void 268 vnet_lagg_uninit(const void *unused __unused) 269 { 270 271 if_clone_detach(V_lagg_cloner); 272 LAGG_LIST_LOCK_DESTROY(); 273 } 274 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 275 vnet_lagg_uninit, NULL); 276 277 static int 278 lagg_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 301 static moduledata_t lagg_mod = { 302 "if_lagg", 303 lagg_modevent, 304 0 305 }; 306 307 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 308 MODULE_VERSION(if_lagg, 1); 309 310 static void 311 lagg_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 325 static void 326 lagg_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 341 static int 342 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m) 343 { 344 345 return (lagg_protos[sc->sc_proto].pr_start(sc, m)); 346 } 347 348 static struct mbuf * 349 lagg_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 355 static int 356 lagg_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 365 static void 366 lagg_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 373 static void 374 lagg_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 381 static void 382 lagg_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 389 static void 390 lagg_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 397 static void 398 lagg_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 405 static void 406 lagg_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 413 static void 414 lagg_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 */ 425 static void 426 lagg_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 */ 447 static void 448 lagg_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 465 static int 466 lagg_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 527 static void 528 lagg_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 547 ifmedia_removeall(&sc->sc_media); 548 ether_ifdetach(ifp); 549 if_free(ifp); 550 551 LAGG_LIST_LOCK(); 552 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries); 553 LAGG_LIST_UNLOCK(); 554 555 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task); 556 LAGG_LOCK_DESTROY(sc); 557 free(sc, M_DEVBUF); 558 } 559 560 static void 561 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr) 562 { 563 struct ifnet *ifp = sc->sc_ifp; 564 struct lagg_port lp; 565 566 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 567 return; 568 569 LAGG_WLOCK_ASSERT(sc); 570 /* 571 * Set the link layer address on the lagg interface. 572 * lagg_proto_lladdr() notifies the MAC change to 573 * the aggregation protocol. iflladdr_event handler which 574 * may trigger gratuitous ARPs for INET will be handled in 575 * a taskqueue. 576 */ 577 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 578 lagg_proto_lladdr(sc); 579 580 bzero(&lp, sizeof(lp)); 581 lp.lp_ifp = sc->sc_ifp; 582 lp.lp_softc = sc; 583 584 lagg_port_lladdr(&lp, lladdr); 585 } 586 587 static void 588 lagg_capabilities(struct lagg_softc *sc) 589 { 590 struct lagg_port *lp; 591 int cap = ~0, ena = ~0; 592 u_long hwa = ~0UL; 593 struct ifnet_hw_tsomax hw_tsomax; 594 595 LAGG_WLOCK_ASSERT(sc); 596 597 memset(&hw_tsomax, 0, sizeof(hw_tsomax)); 598 599 /* Get capabilities from the lagg ports */ 600 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 601 cap &= lp->lp_ifp->if_capabilities; 602 ena &= lp->lp_ifp->if_capenable; 603 hwa &= lp->lp_ifp->if_hwassist; 604 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax); 605 } 606 cap = (cap == ~0 ? 0 : cap); 607 ena = (ena == ~0 ? 0 : ena); 608 hwa = (hwa == ~0 ? 0 : hwa); 609 610 if (sc->sc_ifp->if_capabilities != cap || 611 sc->sc_ifp->if_capenable != ena || 612 sc->sc_ifp->if_hwassist != hwa || 613 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) { 614 sc->sc_ifp->if_capabilities = cap; 615 sc->sc_ifp->if_capenable = ena; 616 sc->sc_ifp->if_hwassist = hwa; 617 getmicrotime(&sc->sc_ifp->if_lastchange); 618 619 if (sc->sc_ifflags & IFF_DEBUG) 620 if_printf(sc->sc_ifp, 621 "capabilities 0x%08x enabled 0x%08x\n", cap, ena); 622 } 623 } 624 625 static void 626 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr) 627 { 628 struct lagg_softc *sc = lp->lp_softc; 629 struct ifnet *ifp = lp->lp_ifp; 630 struct lagg_llq *llq; 631 int pending = 0; 632 int primary; 633 634 LAGG_WLOCK_ASSERT(sc); 635 636 primary = (sc->sc_primary->lp_ifp == ifp) ? 1 : 0; 637 if (primary == 0 && (lp->lp_detaching || 638 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)) 639 return; 640 641 /* Check to make sure its not already queued to be changed */ 642 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 643 if (llq->llq_ifp == ifp) { 644 pending = 1; 645 break; 646 } 647 } 648 649 if (!pending) { 650 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT); 651 if (llq == NULL) /* XXX what to do */ 652 return; 653 } 654 655 /* Update the lladdr even if pending, it may have changed */ 656 llq->llq_ifp = ifp; 657 llq->llq_primary = primary; 658 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN); 659 660 if (!pending) 661 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries); 662 663 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task); 664 } 665 666 /* 667 * Set the interface MAC address from a taskqueue to avoid a LOR. 668 */ 669 static void 670 lagg_port_setlladdr(void *arg, int pending) 671 { 672 struct lagg_softc *sc = (struct lagg_softc *)arg; 673 struct lagg_llq *llq, *head; 674 struct ifnet *ifp; 675 int error; 676 677 /* Grab a local reference of the queue and remove it from the softc */ 678 LAGG_WLOCK(sc); 679 head = SLIST_FIRST(&sc->sc_llq_head); 680 SLIST_FIRST(&sc->sc_llq_head) = NULL; 681 LAGG_WUNLOCK(sc); 682 683 /* 684 * Traverse the queue and set the lladdr on each ifp. It is safe to do 685 * unlocked as we have the only reference to it. 686 */ 687 for (llq = head; llq != NULL; llq = head) { 688 ifp = llq->llq_ifp; 689 690 CURVNET_SET(ifp->if_vnet); 691 if (llq->llq_primary == 0) { 692 /* 693 * Set the link layer address on the laggport interface. 694 * if_setlladdr() triggers gratuitous ARPs for INET. 695 */ 696 error = if_setlladdr(ifp, llq->llq_lladdr, 697 ETHER_ADDR_LEN); 698 if (error) 699 printf("%s: setlladdr failed on %s\n", __func__, 700 ifp->if_xname); 701 } else 702 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 703 CURVNET_RESTORE(); 704 head = SLIST_NEXT(llq, llq_entries); 705 free(llq, M_DEVBUF); 706 } 707 } 708 709 static int 710 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp) 711 { 712 struct lagg_softc *sc_ptr; 713 struct lagg_port *lp, *tlp; 714 int error, i; 715 uint64_t *pval; 716 717 LAGG_WLOCK_ASSERT(sc); 718 719 /* Limit the maximal number of lagg ports */ 720 if (sc->sc_count >= LAGG_MAX_PORTS) 721 return (ENOSPC); 722 723 /* Check if port has already been associated to a lagg */ 724 if (ifp->if_lagg != NULL) { 725 /* Port is already in the current lagg? */ 726 lp = (struct lagg_port *)ifp->if_lagg; 727 if (lp->lp_softc == sc) 728 return (EEXIST); 729 return (EBUSY); 730 } 731 732 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */ 733 if (ifp->if_type != IFT_ETHER) 734 return (EPROTONOSUPPORT); 735 736 /* Allow the first Ethernet member to define the MTU */ 737 if (SLIST_EMPTY(&sc->sc_ports)) 738 sc->sc_ifp->if_mtu = ifp->if_mtu; 739 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) { 740 if_printf(sc->sc_ifp, "invalid MTU for %s\n", 741 ifp->if_xname); 742 return (EINVAL); 743 } 744 745 if ((lp = malloc(sizeof(struct lagg_port), 746 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 747 return (ENOMEM); 748 749 /* Check if port is a stacked lagg */ 750 LAGG_LIST_LOCK(); 751 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) { 752 if (ifp == sc_ptr->sc_ifp) { 753 LAGG_LIST_UNLOCK(); 754 free(lp, M_DEVBUF); 755 return (EINVAL); 756 /* XXX disable stacking for the moment, its untested */ 757 #ifdef LAGG_PORT_STACKING 758 lp->lp_flags |= LAGG_PORT_STACK; 759 if (lagg_port_checkstacking(sc_ptr) >= 760 LAGG_MAX_STACKING) { 761 LAGG_LIST_UNLOCK(); 762 free(lp, M_DEVBUF); 763 return (E2BIG); 764 } 765 #endif 766 } 767 } 768 LAGG_LIST_UNLOCK(); 769 770 /* Change the interface type */ 771 lp->lp_iftype = ifp->if_type; 772 ifp->if_type = IFT_IEEE8023ADLAG; 773 ifp->if_lagg = lp; 774 lp->lp_ioctl = ifp->if_ioctl; 775 ifp->if_ioctl = lagg_port_ioctl; 776 lp->lp_output = ifp->if_output; 777 ifp->if_output = lagg_port_output; 778 779 lp->lp_ifp = ifp; 780 lp->lp_softc = sc; 781 782 /* Save port link layer address */ 783 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN); 784 785 if (SLIST_EMPTY(&sc->sc_ports)) { 786 sc->sc_primary = lp; 787 lagg_lladdr(sc, IF_LLADDR(ifp)); 788 } else { 789 /* Update link layer address for this port */ 790 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp)); 791 } 792 793 /* 794 * Insert into the list of ports. 795 * Keep ports sorted by if_index. It is handy, when configuration 796 * is predictable and `ifconfig laggN create ...` command 797 * will lead to the same result each time. 798 */ 799 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) { 800 if (tlp->lp_ifp->if_index < ifp->if_index && ( 801 SLIST_NEXT(tlp, lp_entries) == NULL || 802 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index > 803 ifp->if_index)) 804 break; 805 } 806 if (tlp != NULL) 807 SLIST_INSERT_AFTER(tlp, lp, lp_entries); 808 else 809 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries); 810 sc->sc_count++; 811 812 /* Update lagg capabilities */ 813 lagg_capabilities(sc); 814 lagg_linkstate(sc); 815 816 /* Read port counters */ 817 pval = lp->port_counters.val; 818 for (i = 0; i < IFCOUNTERS; i++, pval++) 819 *pval = ifp->if_get_counter(ifp, i); 820 /* Add multicast addresses and interface flags to this port */ 821 lagg_ether_cmdmulti(lp, 1); 822 lagg_setflags(lp, 1); 823 824 if ((error = lagg_proto_addport(sc, lp)) != 0) { 825 /* Remove the port, without calling pr_delport. */ 826 lagg_port_destroy(lp, 0); 827 return (error); 828 } 829 830 return (0); 831 } 832 833 #ifdef LAGG_PORT_STACKING 834 static int 835 lagg_port_checkstacking(struct lagg_softc *sc) 836 { 837 struct lagg_softc *sc_ptr; 838 struct lagg_port *lp; 839 int m = 0; 840 841 LAGG_WLOCK_ASSERT(sc); 842 843 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 844 if (lp->lp_flags & LAGG_PORT_STACK) { 845 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc; 846 m = MAX(m, lagg_port_checkstacking(sc_ptr)); 847 } 848 } 849 850 return (m + 1); 851 } 852 #endif 853 854 static int 855 lagg_port_destroy(struct lagg_port *lp, int rundelport) 856 { 857 struct lagg_softc *sc = lp->lp_softc; 858 struct lagg_port *lp_ptr; 859 struct lagg_llq *llq; 860 struct ifnet *ifp = lp->lp_ifp; 861 uint64_t *pval, vdiff; 862 int i; 863 864 LAGG_WLOCK_ASSERT(sc); 865 866 if (rundelport) 867 lagg_proto_delport(sc, lp); 868 869 /* 870 * Remove multicast addresses and interface flags from this port and 871 * reset the MAC address, skip if the interface is being detached. 872 */ 873 if (!lp->lp_detaching) { 874 lagg_ether_cmdmulti(lp, 0); 875 lagg_setflags(lp, 0); 876 lagg_port_lladdr(lp, lp->lp_lladdr); 877 } 878 879 /* Restore interface */ 880 ifp->if_type = lp->lp_iftype; 881 ifp->if_ioctl = lp->lp_ioctl; 882 ifp->if_output = lp->lp_output; 883 ifp->if_lagg = NULL; 884 885 /* Update detached port counters */ 886 pval = lp->port_counters.val; 887 for (i = 0; i < IFCOUNTERS; i++, pval++) { 888 vdiff = ifp->if_get_counter(ifp, i) - *pval; 889 sc->detached_counters.val[i] += vdiff; 890 } 891 892 /* Finally, remove the port from the lagg */ 893 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries); 894 sc->sc_count--; 895 896 /* Update the primary interface */ 897 if (lp == sc->sc_primary) { 898 uint8_t lladdr[ETHER_ADDR_LEN]; 899 900 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) { 901 bzero(&lladdr, ETHER_ADDR_LEN); 902 } else { 903 bcopy(lp_ptr->lp_lladdr, 904 lladdr, ETHER_ADDR_LEN); 905 } 906 lagg_lladdr(sc, lladdr); 907 sc->sc_primary = lp_ptr; 908 909 /* Update link layer address for each port */ 910 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries) 911 lagg_port_lladdr(lp_ptr, lladdr); 912 } 913 914 /* Remove any pending lladdr changes from the queue */ 915 if (lp->lp_detaching) { 916 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 917 if (llq->llq_ifp == ifp) { 918 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq, 919 llq_entries); 920 free(llq, M_DEVBUF); 921 break; /* Only appears once */ 922 } 923 } 924 } 925 926 if (lp->lp_ifflags) 927 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__); 928 929 free(lp, M_DEVBUF); 930 931 /* Update lagg capabilities */ 932 lagg_capabilities(sc); 933 lagg_linkstate(sc); 934 935 return (0); 936 } 937 938 static int 939 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 940 { 941 struct lagg_reqport *rp = (struct lagg_reqport *)data; 942 struct lagg_softc *sc; 943 struct lagg_port *lp = NULL; 944 int error = 0; 945 struct rm_priotracker tracker; 946 947 /* Should be checked by the caller */ 948 if (ifp->if_type != IFT_IEEE8023ADLAG || 949 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL) 950 goto fallback; 951 952 switch (cmd) { 953 case SIOCGLAGGPORT: 954 if (rp->rp_portname[0] == '\0' || 955 ifunit(rp->rp_portname) != ifp) { 956 error = EINVAL; 957 break; 958 } 959 960 LAGG_RLOCK(sc, &tracker); 961 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) { 962 error = ENOENT; 963 LAGG_RUNLOCK(sc, &tracker); 964 break; 965 } 966 967 lagg_port2req(lp, rp); 968 LAGG_RUNLOCK(sc, &tracker); 969 break; 970 971 case SIOCSIFCAP: 972 if (lp->lp_ioctl == NULL) { 973 error = EINVAL; 974 break; 975 } 976 error = (*lp->lp_ioctl)(ifp, cmd, data); 977 if (error) 978 break; 979 980 /* Update lagg interface capabilities */ 981 LAGG_WLOCK(sc); 982 lagg_capabilities(sc); 983 LAGG_WUNLOCK(sc); 984 break; 985 986 case SIOCSIFMTU: 987 /* Do not allow the MTU to be changed once joined */ 988 error = EINVAL; 989 break; 990 991 default: 992 goto fallback; 993 } 994 995 return (error); 996 997 fallback: 998 if (lp->lp_ioctl != NULL) 999 return ((*lp->lp_ioctl)(ifp, cmd, data)); 1000 1001 return (EINVAL); 1002 } 1003 1004 /* 1005 * Requests counter @cnt data. 1006 * 1007 * Counter value is calculated the following way: 1008 * 1) for each port, sum difference between current and "initial" measurements. 1009 * 2) add lagg logical interface counters. 1010 * 3) add data from detached_counters array. 1011 * 1012 * We also do the following things on ports attach/detach: 1013 * 1) On port attach we store all counters it has into port_counter array. 1014 * 2) On port detach we add the different between "initial" and 1015 * current counters data to detached_counters array. 1016 */ 1017 static uint64_t 1018 lagg_get_counter(struct ifnet *ifp, ift_counter cnt) 1019 { 1020 struct lagg_softc *sc; 1021 struct lagg_port *lp; 1022 struct ifnet *lpifp; 1023 struct rm_priotracker tracker; 1024 uint64_t newval, oldval, vsum; 1025 1026 /* Revise this when we've got non-generic counters. */ 1027 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1028 1029 sc = (struct lagg_softc *)ifp->if_softc; 1030 LAGG_RLOCK(sc, &tracker); 1031 1032 vsum = 0; 1033 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1034 /* Saved attached value */ 1035 oldval = lp->port_counters.val[cnt]; 1036 /* current value */ 1037 lpifp = lp->lp_ifp; 1038 newval = lpifp->if_get_counter(lpifp, cnt); 1039 /* Calculate diff and save new */ 1040 vsum += newval - oldval; 1041 } 1042 1043 /* 1044 * Add counter data which might be added by upper 1045 * layer protocols operating on logical interface. 1046 */ 1047 vsum += if_get_counter_default(ifp, cnt); 1048 1049 /* 1050 * Add counter data from detached ports counters 1051 */ 1052 vsum += sc->detached_counters.val[cnt]; 1053 1054 LAGG_RUNLOCK(sc, &tracker); 1055 1056 return (vsum); 1057 } 1058 1059 /* 1060 * For direct output to child ports. 1061 */ 1062 static int 1063 lagg_port_output(struct ifnet *ifp, struct mbuf *m, 1064 const struct sockaddr *dst, struct route *ro) 1065 { 1066 struct lagg_port *lp = ifp->if_lagg; 1067 1068 switch (dst->sa_family) { 1069 case pseudo_AF_HDRCMPLT: 1070 case AF_UNSPEC: 1071 return ((*lp->lp_output)(ifp, m, dst, ro)); 1072 } 1073 1074 /* drop any other frames */ 1075 m_freem(m); 1076 return (ENETDOWN); 1077 } 1078 1079 static void 1080 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp) 1081 { 1082 struct lagg_port *lp; 1083 struct lagg_softc *sc; 1084 1085 if ((lp = ifp->if_lagg) == NULL) 1086 return; 1087 /* If the ifnet is just being renamed, don't do anything. */ 1088 if (ifp->if_flags & IFF_RENAMING) 1089 return; 1090 1091 sc = lp->lp_softc; 1092 1093 LAGG_WLOCK(sc); 1094 lp->lp_detaching = 1; 1095 lagg_port_destroy(lp, 1); 1096 LAGG_WUNLOCK(sc); 1097 } 1098 1099 static void 1100 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp) 1101 { 1102 struct lagg_softc *sc = lp->lp_softc; 1103 1104 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname)); 1105 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname)); 1106 rp->rp_prio = lp->lp_prio; 1107 rp->rp_flags = lp->lp_flags; 1108 lagg_proto_portreq(sc, lp, &rp->rp_psc); 1109 1110 /* Add protocol specific flags */ 1111 switch (sc->sc_proto) { 1112 case LAGG_PROTO_FAILOVER: 1113 if (lp == sc->sc_primary) 1114 rp->rp_flags |= LAGG_PORT_MASTER; 1115 if (lp == lagg_link_active(sc, sc->sc_primary)) 1116 rp->rp_flags |= LAGG_PORT_ACTIVE; 1117 break; 1118 1119 case LAGG_PROTO_ROUNDROBIN: 1120 case LAGG_PROTO_LOADBALANCE: 1121 case LAGG_PROTO_BROADCAST: 1122 if (LAGG_PORTACTIVE(lp)) 1123 rp->rp_flags |= LAGG_PORT_ACTIVE; 1124 break; 1125 1126 case LAGG_PROTO_LACP: 1127 /* LACP has a different definition of active */ 1128 if (lacp_isactive(lp)) 1129 rp->rp_flags |= LAGG_PORT_ACTIVE; 1130 if (lacp_iscollecting(lp)) 1131 rp->rp_flags |= LAGG_PORT_COLLECTING; 1132 if (lacp_isdistributing(lp)) 1133 rp->rp_flags |= LAGG_PORT_DISTRIBUTING; 1134 break; 1135 } 1136 1137 } 1138 1139 static void 1140 lagg_init(void *xsc) 1141 { 1142 struct lagg_softc *sc = (struct lagg_softc *)xsc; 1143 struct lagg_port *lp; 1144 struct ifnet *ifp = sc->sc_ifp; 1145 1146 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1147 return; 1148 1149 LAGG_WLOCK(sc); 1150 1151 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1152 /* Update the port lladdrs */ 1153 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1154 lagg_port_lladdr(lp, IF_LLADDR(ifp)); 1155 1156 lagg_proto_init(sc); 1157 1158 LAGG_WUNLOCK(sc); 1159 } 1160 1161 static void 1162 lagg_stop(struct lagg_softc *sc) 1163 { 1164 struct ifnet *ifp = sc->sc_ifp; 1165 1166 LAGG_WLOCK_ASSERT(sc); 1167 1168 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1169 return; 1170 1171 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1172 1173 lagg_proto_stop(sc); 1174 } 1175 1176 static int 1177 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1178 { 1179 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1180 struct lagg_reqall *ra = (struct lagg_reqall *)data; 1181 struct lagg_reqopts *ro = (struct lagg_reqopts *)data; 1182 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf; 1183 struct lagg_reqflags *rf = (struct lagg_reqflags *)data; 1184 struct ifreq *ifr = (struct ifreq *)data; 1185 struct lagg_port *lp; 1186 struct ifnet *tpif; 1187 struct thread *td = curthread; 1188 char *buf, *outbuf; 1189 int count, buflen, len, error = 0; 1190 struct rm_priotracker tracker; 1191 1192 bzero(&rpbuf, sizeof(rpbuf)); 1193 1194 switch (cmd) { 1195 case SIOCGLAGG: 1196 LAGG_RLOCK(sc, &tracker); 1197 count = 0; 1198 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1199 count++; 1200 buflen = count * sizeof(struct lagg_reqport); 1201 LAGG_RUNLOCK(sc, &tracker); 1202 1203 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 1204 1205 LAGG_RLOCK(sc, &tracker); 1206 ra->ra_proto = sc->sc_proto; 1207 lagg_proto_request(sc, &ra->ra_psc); 1208 count = 0; 1209 buf = outbuf; 1210 len = min(ra->ra_size, buflen); 1211 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1212 if (len < sizeof(rpbuf)) 1213 break; 1214 1215 lagg_port2req(lp, &rpbuf); 1216 memcpy(buf, &rpbuf, sizeof(rpbuf)); 1217 count++; 1218 buf += sizeof(rpbuf); 1219 len -= sizeof(rpbuf); 1220 } 1221 LAGG_RUNLOCK(sc, &tracker); 1222 ra->ra_ports = count; 1223 ra->ra_size = count * sizeof(rpbuf); 1224 error = copyout(outbuf, ra->ra_port, ra->ra_size); 1225 free(outbuf, M_TEMP); 1226 break; 1227 case SIOCSLAGG: 1228 error = priv_check(td, PRIV_NET_LAGG); 1229 if (error) 1230 break; 1231 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) { 1232 error = EPROTONOSUPPORT; 1233 break; 1234 } 1235 1236 LAGG_WLOCK(sc); 1237 lagg_proto_detach(sc); 1238 lagg_proto_attach(sc, ra->ra_proto); 1239 break; 1240 case SIOCGLAGGOPTS: 1241 ro->ro_opts = sc->sc_opts; 1242 if (sc->sc_proto == LAGG_PROTO_LACP) { 1243 struct lacp_softc *lsc; 1244 1245 lsc = (struct lacp_softc *)sc->sc_psc; 1246 if (lsc->lsc_debug.lsc_tx_test != 0) 1247 ro->ro_opts |= LAGG_OPT_LACP_TXTEST; 1248 if (lsc->lsc_debug.lsc_rx_test != 0) 1249 ro->ro_opts |= LAGG_OPT_LACP_RXTEST; 1250 if (lsc->lsc_strict_mode != 0) 1251 ro->ro_opts |= LAGG_OPT_LACP_STRICT; 1252 if (lsc->lsc_fast_timeout != 0) 1253 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT; 1254 1255 ro->ro_active = sc->sc_active; 1256 } else { 1257 ro->ro_active = 0; 1258 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1259 ro->ro_active += LAGG_PORTACTIVE(lp); 1260 } 1261 ro->ro_flapping = sc->sc_flapping; 1262 ro->ro_flowid_shift = sc->flowid_shift; 1263 break; 1264 case SIOCSLAGGOPTS: 1265 error = priv_check(td, PRIV_NET_LAGG); 1266 if (error) 1267 break; 1268 if (ro->ro_opts == 0) 1269 break; 1270 /* 1271 * Set options. LACP options are stored in sc->sc_psc, 1272 * not in sc_opts. 1273 */ 1274 int valid, lacp; 1275 1276 switch (ro->ro_opts) { 1277 case LAGG_OPT_USE_FLOWID: 1278 case -LAGG_OPT_USE_FLOWID: 1279 case LAGG_OPT_FLOWIDSHIFT: 1280 valid = 1; 1281 lacp = 0; 1282 break; 1283 case LAGG_OPT_LACP_TXTEST: 1284 case -LAGG_OPT_LACP_TXTEST: 1285 case LAGG_OPT_LACP_RXTEST: 1286 case -LAGG_OPT_LACP_RXTEST: 1287 case LAGG_OPT_LACP_STRICT: 1288 case -LAGG_OPT_LACP_STRICT: 1289 case LAGG_OPT_LACP_TIMEOUT: 1290 case -LAGG_OPT_LACP_TIMEOUT: 1291 valid = lacp = 1; 1292 break; 1293 default: 1294 valid = lacp = 0; 1295 break; 1296 } 1297 1298 LAGG_WLOCK(sc); 1299 if (valid == 0 || 1300 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) { 1301 /* Invalid combination of options specified. */ 1302 error = EINVAL; 1303 LAGG_WUNLOCK(sc); 1304 break; /* Return from SIOCSLAGGOPTS. */ 1305 } 1306 /* 1307 * Store new options into sc->sc_opts except for 1308 * FLOWIDSHIFT and LACP options. 1309 */ 1310 if (lacp == 0) { 1311 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT) 1312 sc->flowid_shift = ro->ro_flowid_shift; 1313 else if (ro->ro_opts > 0) 1314 sc->sc_opts |= ro->ro_opts; 1315 else 1316 sc->sc_opts &= ~ro->ro_opts; 1317 } else { 1318 struct lacp_softc *lsc; 1319 struct lacp_port *lp; 1320 1321 lsc = (struct lacp_softc *)sc->sc_psc; 1322 1323 switch (ro->ro_opts) { 1324 case LAGG_OPT_LACP_TXTEST: 1325 lsc->lsc_debug.lsc_tx_test = 1; 1326 break; 1327 case -LAGG_OPT_LACP_TXTEST: 1328 lsc->lsc_debug.lsc_tx_test = 0; 1329 break; 1330 case LAGG_OPT_LACP_RXTEST: 1331 lsc->lsc_debug.lsc_rx_test = 1; 1332 break; 1333 case -LAGG_OPT_LACP_RXTEST: 1334 lsc->lsc_debug.lsc_rx_test = 0; 1335 break; 1336 case LAGG_OPT_LACP_STRICT: 1337 lsc->lsc_strict_mode = 1; 1338 break; 1339 case -LAGG_OPT_LACP_STRICT: 1340 lsc->lsc_strict_mode = 0; 1341 break; 1342 case LAGG_OPT_LACP_TIMEOUT: 1343 LACP_LOCK(lsc); 1344 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) 1345 lp->lp_state |= LACP_STATE_TIMEOUT; 1346 LACP_UNLOCK(lsc); 1347 lsc->lsc_fast_timeout = 1; 1348 break; 1349 case -LAGG_OPT_LACP_TIMEOUT: 1350 LACP_LOCK(lsc); 1351 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) 1352 lp->lp_state &= ~LACP_STATE_TIMEOUT; 1353 LACP_UNLOCK(lsc); 1354 lsc->lsc_fast_timeout = 0; 1355 break; 1356 } 1357 } 1358 LAGG_WUNLOCK(sc); 1359 break; 1360 case SIOCGLAGGFLAGS: 1361 rf->rf_flags = 0; 1362 LAGG_RLOCK(sc, &tracker); 1363 if (sc->sc_flags & MBUF_HASHFLAG_L2) 1364 rf->rf_flags |= LAGG_F_HASHL2; 1365 if (sc->sc_flags & MBUF_HASHFLAG_L3) 1366 rf->rf_flags |= LAGG_F_HASHL3; 1367 if (sc->sc_flags & MBUF_HASHFLAG_L4) 1368 rf->rf_flags |= LAGG_F_HASHL4; 1369 LAGG_RUNLOCK(sc, &tracker); 1370 break; 1371 case SIOCSLAGGHASH: 1372 error = priv_check(td, PRIV_NET_LAGG); 1373 if (error) 1374 break; 1375 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) { 1376 error = EINVAL; 1377 break; 1378 } 1379 LAGG_WLOCK(sc); 1380 sc->sc_flags = 0; 1381 if (rf->rf_flags & LAGG_F_HASHL2) 1382 sc->sc_flags |= MBUF_HASHFLAG_L2; 1383 if (rf->rf_flags & LAGG_F_HASHL3) 1384 sc->sc_flags |= MBUF_HASHFLAG_L3; 1385 if (rf->rf_flags & LAGG_F_HASHL4) 1386 sc->sc_flags |= MBUF_HASHFLAG_L4; 1387 LAGG_WUNLOCK(sc); 1388 break; 1389 case SIOCGLAGGPORT: 1390 if (rp->rp_portname[0] == '\0' || 1391 (tpif = ifunit(rp->rp_portname)) == NULL) { 1392 error = EINVAL; 1393 break; 1394 } 1395 1396 LAGG_RLOCK(sc, &tracker); 1397 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1398 lp->lp_softc != sc) { 1399 error = ENOENT; 1400 LAGG_RUNLOCK(sc, &tracker); 1401 break; 1402 } 1403 1404 lagg_port2req(lp, rp); 1405 LAGG_RUNLOCK(sc, &tracker); 1406 break; 1407 case SIOCSLAGGPORT: 1408 error = priv_check(td, PRIV_NET_LAGG); 1409 if (error) 1410 break; 1411 if (rp->rp_portname[0] == '\0' || 1412 (tpif = ifunit(rp->rp_portname)) == NULL) { 1413 error = EINVAL; 1414 break; 1415 } 1416 #ifdef INET6 1417 /* 1418 * A laggport interface should not have inet6 address 1419 * because two interfaces with a valid link-local 1420 * scope zone must not be merged in any form. This 1421 * restriction is needed to prevent violation of 1422 * link-local scope zone. Attempts to add a laggport 1423 * interface which has inet6 addresses triggers 1424 * removal of all inet6 addresses on the member 1425 * interface. 1426 */ 1427 if (in6ifa_llaonifp(tpif)) { 1428 in6_ifdetach(tpif); 1429 if_printf(sc->sc_ifp, 1430 "IPv6 addresses on %s have been removed " 1431 "before adding it as a member to prevent " 1432 "IPv6 address scope violation.\n", 1433 tpif->if_xname); 1434 } 1435 #endif 1436 LAGG_WLOCK(sc); 1437 error = lagg_port_create(sc, tpif); 1438 LAGG_WUNLOCK(sc); 1439 break; 1440 case SIOCSLAGGDELPORT: 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 1450 LAGG_WLOCK(sc); 1451 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1452 lp->lp_softc != sc) { 1453 error = ENOENT; 1454 LAGG_WUNLOCK(sc); 1455 break; 1456 } 1457 1458 error = lagg_port_destroy(lp, 1); 1459 LAGG_WUNLOCK(sc); 1460 break; 1461 case SIOCSIFFLAGS: 1462 /* Set flags on ports too */ 1463 LAGG_WLOCK(sc); 1464 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1465 lagg_setflags(lp, 1); 1466 } 1467 LAGG_WUNLOCK(sc); 1468 1469 if (!(ifp->if_flags & IFF_UP) && 1470 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1471 /* 1472 * If interface is marked down and it is running, 1473 * then stop and disable it. 1474 */ 1475 LAGG_WLOCK(sc); 1476 lagg_stop(sc); 1477 LAGG_WUNLOCK(sc); 1478 } else if ((ifp->if_flags & IFF_UP) && 1479 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1480 /* 1481 * If interface is marked up and it is stopped, then 1482 * start it. 1483 */ 1484 (*ifp->if_init)(sc); 1485 } 1486 break; 1487 case SIOCADDMULTI: 1488 case SIOCDELMULTI: 1489 LAGG_WLOCK(sc); 1490 error = lagg_ether_setmulti(sc); 1491 LAGG_WUNLOCK(sc); 1492 break; 1493 case SIOCSIFMEDIA: 1494 case SIOCGIFMEDIA: 1495 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1496 break; 1497 1498 case SIOCSIFCAP: 1499 case SIOCSIFMTU: 1500 /* Do not allow the MTU or caps to be directly changed */ 1501 error = EINVAL; 1502 break; 1503 1504 default: 1505 error = ether_ioctl(ifp, cmd, data); 1506 break; 1507 } 1508 return (error); 1509 } 1510 1511 static int 1512 lagg_ether_setmulti(struct lagg_softc *sc) 1513 { 1514 struct lagg_port *lp; 1515 1516 LAGG_WLOCK_ASSERT(sc); 1517 1518 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1519 /* First, remove any existing filter entries. */ 1520 lagg_ether_cmdmulti(lp, 0); 1521 /* copy all addresses from the lagg interface to the port */ 1522 lagg_ether_cmdmulti(lp, 1); 1523 } 1524 return (0); 1525 } 1526 1527 static int 1528 lagg_ether_cmdmulti(struct lagg_port *lp, int set) 1529 { 1530 struct lagg_softc *sc = lp->lp_softc; 1531 struct ifnet *ifp = lp->lp_ifp; 1532 struct ifnet *scifp = sc->sc_ifp; 1533 struct lagg_mc *mc; 1534 struct ifmultiaddr *ifma; 1535 int error; 1536 1537 LAGG_WLOCK_ASSERT(sc); 1538 1539 if (set) { 1540 IF_ADDR_WLOCK(scifp); 1541 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) { 1542 if (ifma->ifma_addr->sa_family != AF_LINK) 1543 continue; 1544 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT); 1545 if (mc == NULL) { 1546 IF_ADDR_WUNLOCK(scifp); 1547 return (ENOMEM); 1548 } 1549 bcopy(ifma->ifma_addr, &mc->mc_addr, 1550 ifma->ifma_addr->sa_len); 1551 mc->mc_addr.sdl_index = ifp->if_index; 1552 mc->mc_ifma = NULL; 1553 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries); 1554 } 1555 IF_ADDR_WUNLOCK(scifp); 1556 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) { 1557 error = if_addmulti(ifp, 1558 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma); 1559 if (error) 1560 return (error); 1561 } 1562 } else { 1563 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) { 1564 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries); 1565 if (mc->mc_ifma && !lp->lp_detaching) 1566 if_delmulti_ifma(mc->mc_ifma); 1567 free(mc, M_DEVBUF); 1568 } 1569 } 1570 return (0); 1571 } 1572 1573 /* Handle a ref counted flag that should be set on the lagg port as well */ 1574 static int 1575 lagg_setflag(struct lagg_port *lp, int flag, int status, 1576 int (*func)(struct ifnet *, int)) 1577 { 1578 struct lagg_softc *sc = lp->lp_softc; 1579 struct ifnet *scifp = sc->sc_ifp; 1580 struct ifnet *ifp = lp->lp_ifp; 1581 int error; 1582 1583 LAGG_WLOCK_ASSERT(sc); 1584 1585 status = status ? (scifp->if_flags & flag) : 0; 1586 /* Now "status" contains the flag value or 0 */ 1587 1588 /* 1589 * See if recorded ports status is different from what 1590 * we want it to be. If it is, flip it. We record ports 1591 * status in lp_ifflags so that we won't clear ports flag 1592 * we haven't set. In fact, we don't clear or set ports 1593 * flags directly, but get or release references to them. 1594 * That's why we can be sure that recorded flags still are 1595 * in accord with actual ports flags. 1596 */ 1597 if (status != (lp->lp_ifflags & flag)) { 1598 error = (*func)(ifp, status); 1599 if (error) 1600 return (error); 1601 lp->lp_ifflags &= ~flag; 1602 lp->lp_ifflags |= status; 1603 } 1604 return (0); 1605 } 1606 1607 /* 1608 * Handle IFF_* flags that require certain changes on the lagg port 1609 * if "status" is true, update ports flags respective to the lagg 1610 * if "status" is false, forcedly clear the flags set on port. 1611 */ 1612 static int 1613 lagg_setflags(struct lagg_port *lp, int status) 1614 { 1615 int error, i; 1616 1617 for (i = 0; lagg_pflags[i].flag; i++) { 1618 error = lagg_setflag(lp, lagg_pflags[i].flag, 1619 status, lagg_pflags[i].func); 1620 if (error) 1621 return (error); 1622 } 1623 return (0); 1624 } 1625 1626 static int 1627 lagg_transmit(struct ifnet *ifp, struct mbuf *m) 1628 { 1629 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1630 int error, len, mcast; 1631 struct rm_priotracker tracker; 1632 1633 len = m->m_pkthdr.len; 1634 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 1635 1636 LAGG_RLOCK(sc, &tracker); 1637 /* We need a Tx algorithm and at least one port */ 1638 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) { 1639 LAGG_RUNLOCK(sc, &tracker); 1640 m_freem(m); 1641 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1642 return (ENXIO); 1643 } 1644 1645 ETHER_BPF_MTAP(ifp, m); 1646 1647 error = lagg_proto_start(sc, m); 1648 LAGG_RUNLOCK(sc, &tracker); 1649 1650 if (error != 0) 1651 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1652 1653 return (error); 1654 } 1655 1656 /* 1657 * The ifp->if_qflush entry point for lagg(4) is no-op. 1658 */ 1659 static void 1660 lagg_qflush(struct ifnet *ifp __unused) 1661 { 1662 } 1663 1664 static struct mbuf * 1665 lagg_input(struct ifnet *ifp, struct mbuf *m) 1666 { 1667 struct lagg_port *lp = ifp->if_lagg; 1668 struct lagg_softc *sc = lp->lp_softc; 1669 struct ifnet *scifp = sc->sc_ifp; 1670 struct rm_priotracker tracker; 1671 1672 LAGG_RLOCK(sc, &tracker); 1673 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1674 (lp->lp_flags & LAGG_PORT_DISABLED) || 1675 sc->sc_proto == LAGG_PROTO_NONE) { 1676 LAGG_RUNLOCK(sc, &tracker); 1677 m_freem(m); 1678 return (NULL); 1679 } 1680 1681 ETHER_BPF_MTAP(scifp, m); 1682 1683 if (lp->lp_detaching != 0) { 1684 m_freem(m); 1685 m = NULL; 1686 } else 1687 m = lagg_proto_input(sc, lp, m); 1688 1689 if (m != NULL) { 1690 if (scifp->if_flags & IFF_MONITOR) { 1691 m_freem(m); 1692 m = NULL; 1693 } 1694 } 1695 1696 LAGG_RUNLOCK(sc, &tracker); 1697 return (m); 1698 } 1699 1700 static int 1701 lagg_media_change(struct ifnet *ifp) 1702 { 1703 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1704 1705 if (sc->sc_ifflags & IFF_DEBUG) 1706 printf("%s\n", __func__); 1707 1708 /* Ignore */ 1709 return (0); 1710 } 1711 1712 static void 1713 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1714 { 1715 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1716 struct lagg_port *lp; 1717 struct rm_priotracker tracker; 1718 1719 imr->ifm_status = IFM_AVALID; 1720 imr->ifm_active = IFM_ETHER | IFM_AUTO; 1721 1722 LAGG_RLOCK(sc, &tracker); 1723 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1724 if (LAGG_PORTACTIVE(lp)) 1725 imr->ifm_status |= IFM_ACTIVE; 1726 } 1727 LAGG_RUNLOCK(sc, &tracker); 1728 } 1729 1730 static void 1731 lagg_linkstate(struct lagg_softc *sc) 1732 { 1733 struct lagg_port *lp; 1734 int new_link = LINK_STATE_DOWN; 1735 uint64_t speed; 1736 1737 /* Our link is considered up if at least one of our ports is active */ 1738 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1739 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) { 1740 new_link = LINK_STATE_UP; 1741 break; 1742 } 1743 } 1744 if_link_state_change(sc->sc_ifp, new_link); 1745 1746 /* Update if_baudrate to reflect the max possible speed */ 1747 switch (sc->sc_proto) { 1748 case LAGG_PROTO_FAILOVER: 1749 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ? 1750 sc->sc_primary->lp_ifp->if_baudrate : 0; 1751 break; 1752 case LAGG_PROTO_ROUNDROBIN: 1753 case LAGG_PROTO_LOADBALANCE: 1754 case LAGG_PROTO_BROADCAST: 1755 speed = 0; 1756 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1757 speed += lp->lp_ifp->if_baudrate; 1758 sc->sc_ifp->if_baudrate = speed; 1759 break; 1760 case LAGG_PROTO_LACP: 1761 /* LACP updates if_baudrate itself */ 1762 break; 1763 } 1764 } 1765 1766 static void 1767 lagg_port_state(struct ifnet *ifp, int state) 1768 { 1769 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg; 1770 struct lagg_softc *sc = NULL; 1771 1772 if (lp != NULL) 1773 sc = lp->lp_softc; 1774 if (sc == NULL) 1775 return; 1776 1777 LAGG_WLOCK(sc); 1778 lagg_linkstate(sc); 1779 lagg_proto_linkstate(sc, lp); 1780 LAGG_WUNLOCK(sc); 1781 } 1782 1783 struct lagg_port * 1784 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp) 1785 { 1786 struct lagg_port *lp_next, *rval = NULL; 1787 // int new_link = LINK_STATE_DOWN; 1788 1789 LAGG_RLOCK_ASSERT(sc); 1790 /* 1791 * Search a port which reports an active link state. 1792 */ 1793 1794 if (lp == NULL) 1795 goto search; 1796 if (LAGG_PORTACTIVE(lp)) { 1797 rval = lp; 1798 goto found; 1799 } 1800 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL && 1801 LAGG_PORTACTIVE(lp_next)) { 1802 rval = lp_next; 1803 goto found; 1804 } 1805 1806 search: 1807 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1808 if (LAGG_PORTACTIVE(lp_next)) { 1809 rval = lp_next; 1810 goto found; 1811 } 1812 } 1813 1814 found: 1815 if (rval != NULL) { 1816 /* 1817 * The IEEE 802.1D standard assumes that a lagg with 1818 * multiple ports is always full duplex. This is valid 1819 * for load sharing laggs and if at least two links 1820 * are active. Unfortunately, checking the latter would 1821 * be too expensive at this point. 1822 XXX 1823 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) && 1824 (sc->sc_count > 1)) 1825 new_link = LINK_STATE_FULL_DUPLEX; 1826 else 1827 new_link = rval->lp_link_state; 1828 */ 1829 } 1830 1831 return (rval); 1832 } 1833 1834 int 1835 lagg_enqueue(struct ifnet *ifp, struct mbuf *m) 1836 { 1837 1838 return (ifp->if_transmit)(ifp, m); 1839 } 1840 1841 /* 1842 * Simple round robin aggregation 1843 */ 1844 static void 1845 lagg_rr_attach(struct lagg_softc *sc) 1846 { 1847 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1848 sc->sc_seq = 0; 1849 } 1850 1851 static int 1852 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m) 1853 { 1854 struct lagg_port *lp; 1855 uint32_t p; 1856 1857 p = atomic_fetchadd_32(&sc->sc_seq, 1); 1858 p %= sc->sc_count; 1859 lp = SLIST_FIRST(&sc->sc_ports); 1860 while (p--) 1861 lp = SLIST_NEXT(lp, lp_entries); 1862 1863 /* 1864 * Check the port's link state. This will return the next active 1865 * port if the link is down or the port is NULL. 1866 */ 1867 if ((lp = lagg_link_active(sc, lp)) == NULL) { 1868 m_freem(m); 1869 return (ENETDOWN); 1870 } 1871 1872 /* Send mbuf */ 1873 return (lagg_enqueue(lp->lp_ifp, m)); 1874 } 1875 1876 static struct mbuf * 1877 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1878 { 1879 struct ifnet *ifp = sc->sc_ifp; 1880 1881 /* Just pass in the packet to our lagg device */ 1882 m->m_pkthdr.rcvif = ifp; 1883 1884 return (m); 1885 } 1886 1887 /* 1888 * Broadcast mode 1889 */ 1890 static int 1891 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m) 1892 { 1893 int active_ports = 0; 1894 int errors = 0; 1895 int ret; 1896 struct lagg_port *lp, *last = NULL; 1897 struct mbuf *m0; 1898 1899 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1900 if (!LAGG_PORTACTIVE(lp)) 1901 continue; 1902 1903 active_ports++; 1904 1905 if (last != NULL) { 1906 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT); 1907 if (m0 == NULL) { 1908 ret = ENOBUFS; 1909 errors++; 1910 break; 1911 } 1912 1913 ret = lagg_enqueue(last->lp_ifp, m0); 1914 if (ret != 0) 1915 errors++; 1916 } 1917 last = lp; 1918 } 1919 if (last == NULL) { 1920 m_freem(m); 1921 return (ENOENT); 1922 } 1923 if ((last = lagg_link_active(sc, last)) == NULL) { 1924 m_freem(m); 1925 return (ENETDOWN); 1926 } 1927 1928 ret = lagg_enqueue(last->lp_ifp, m); 1929 if (ret != 0) 1930 errors++; 1931 1932 if (errors == 0) 1933 return (ret); 1934 1935 return (0); 1936 } 1937 1938 static struct mbuf* 1939 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1940 { 1941 struct ifnet *ifp = sc->sc_ifp; 1942 1943 /* Just pass in the packet to our lagg device */ 1944 m->m_pkthdr.rcvif = ifp; 1945 return (m); 1946 } 1947 1948 /* 1949 * Active failover 1950 */ 1951 static int 1952 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m) 1953 { 1954 struct lagg_port *lp; 1955 1956 /* Use the master port if active or the next available port */ 1957 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) { 1958 m_freem(m); 1959 return (ENETDOWN); 1960 } 1961 1962 /* Send mbuf */ 1963 return (lagg_enqueue(lp->lp_ifp, m)); 1964 } 1965 1966 static struct mbuf * 1967 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1968 { 1969 struct ifnet *ifp = sc->sc_ifp; 1970 struct lagg_port *tmp_tp; 1971 1972 if (lp == sc->sc_primary || V_lagg_failover_rx_all) { 1973 m->m_pkthdr.rcvif = ifp; 1974 return (m); 1975 } 1976 1977 if (!LAGG_PORTACTIVE(sc->sc_primary)) { 1978 tmp_tp = lagg_link_active(sc, sc->sc_primary); 1979 /* 1980 * If tmp_tp is null, we've recieved a packet when all 1981 * our links are down. Weird, but process it anyways. 1982 */ 1983 if ((tmp_tp == NULL || tmp_tp == lp)) { 1984 m->m_pkthdr.rcvif = ifp; 1985 return (m); 1986 } 1987 } 1988 1989 m_freem(m); 1990 return (NULL); 1991 } 1992 1993 /* 1994 * Loadbalancing 1995 */ 1996 static void 1997 lagg_lb_attach(struct lagg_softc *sc) 1998 { 1999 struct lagg_port *lp; 2000 struct lagg_lb *lb; 2001 2002 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO); 2003 2004 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 2005 2006 lb->lb_key = m_ether_tcpip_hash_init(); 2007 sc->sc_psc = lb; 2008 2009 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2010 lagg_lb_port_create(lp); 2011 } 2012 2013 static void 2014 lagg_lb_detach(struct lagg_softc *sc) 2015 { 2016 struct lagg_lb *lb; 2017 2018 lb = (struct lagg_lb *)sc->sc_psc; 2019 LAGG_WUNLOCK(sc); 2020 if (lb != NULL) 2021 free(lb, M_DEVBUF); 2022 } 2023 2024 static int 2025 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp) 2026 { 2027 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 2028 struct lagg_port *lp_next; 2029 int i = 0; 2030 2031 bzero(&lb->lb_ports, sizeof(lb->lb_ports)); 2032 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 2033 if (lp_next == lp) 2034 continue; 2035 if (i >= LAGG_MAX_PORTS) 2036 return (EINVAL); 2037 if (sc->sc_ifflags & IFF_DEBUG) 2038 printf("%s: port %s at index %d\n", 2039 sc->sc_ifname, lp_next->lp_ifp->if_xname, i); 2040 lb->lb_ports[i++] = lp_next; 2041 } 2042 2043 return (0); 2044 } 2045 2046 static int 2047 lagg_lb_port_create(struct lagg_port *lp) 2048 { 2049 struct lagg_softc *sc = lp->lp_softc; 2050 return (lagg_lb_porttable(sc, NULL)); 2051 } 2052 2053 static void 2054 lagg_lb_port_destroy(struct lagg_port *lp) 2055 { 2056 struct lagg_softc *sc = lp->lp_softc; 2057 lagg_lb_porttable(sc, lp); 2058 } 2059 2060 static int 2061 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m) 2062 { 2063 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 2064 struct lagg_port *lp = NULL; 2065 uint32_t p = 0; 2066 2067 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) && 2068 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 2069 p = m->m_pkthdr.flowid >> sc->flowid_shift; 2070 else 2071 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key); 2072 p %= sc->sc_count; 2073 lp = lb->lb_ports[p]; 2074 2075 /* 2076 * Check the port's link state. This will return the next active 2077 * port if the link is down or the port is NULL. 2078 */ 2079 if ((lp = lagg_link_active(sc, lp)) == NULL) { 2080 m_freem(m); 2081 return (ENETDOWN); 2082 } 2083 2084 /* Send mbuf */ 2085 return (lagg_enqueue(lp->lp_ifp, m)); 2086 } 2087 2088 static struct mbuf * 2089 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 2090 { 2091 struct ifnet *ifp = sc->sc_ifp; 2092 2093 /* Just pass in the packet to our lagg device */ 2094 m->m_pkthdr.rcvif = ifp; 2095 2096 return (m); 2097 } 2098 2099 /* 2100 * 802.3ad LACP 2101 */ 2102 static void 2103 lagg_lacp_attach(struct lagg_softc *sc) 2104 { 2105 struct lagg_port *lp; 2106 2107 lacp_attach(sc); 2108 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2109 lacp_port_create(lp); 2110 } 2111 2112 static void 2113 lagg_lacp_detach(struct lagg_softc *sc) 2114 { 2115 struct lagg_port *lp; 2116 void *psc; 2117 2118 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2119 lacp_port_destroy(lp); 2120 2121 psc = sc->sc_psc; 2122 sc->sc_psc = NULL; 2123 LAGG_WUNLOCK(sc); 2124 2125 lacp_detach(psc); 2126 } 2127 2128 static void 2129 lagg_lacp_lladdr(struct lagg_softc *sc) 2130 { 2131 struct lagg_port *lp; 2132 2133 /* purge all the lacp ports */ 2134 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2135 lacp_port_destroy(lp); 2136 2137 /* add them back in */ 2138 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 2139 lacp_port_create(lp); 2140 } 2141 2142 static int 2143 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m) 2144 { 2145 struct lagg_port *lp; 2146 2147 lp = lacp_select_tx_port(sc, m); 2148 if (lp == NULL) { 2149 m_freem(m); 2150 return (ENETDOWN); 2151 } 2152 2153 /* Send mbuf */ 2154 return (lagg_enqueue(lp->lp_ifp, m)); 2155 } 2156 2157 static struct mbuf * 2158 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 2159 { 2160 struct ifnet *ifp = sc->sc_ifp; 2161 struct ether_header *eh; 2162 u_short etype; 2163 2164 eh = mtod(m, struct ether_header *); 2165 etype = ntohs(eh->ether_type); 2166 2167 /* Tap off LACP control messages */ 2168 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) { 2169 m = lacp_input(lp, m); 2170 if (m == NULL) 2171 return (NULL); 2172 } 2173 2174 /* 2175 * If the port is not collecting or not in the active aggregator then 2176 * free and return. 2177 */ 2178 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) { 2179 m_freem(m); 2180 return (NULL); 2181 } 2182 2183 m->m_pkthdr.rcvif = ifp; 2184 return (m); 2185 } 2186 2187