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