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