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