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