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