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