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