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