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