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