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