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