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