1168793Sthompsa/* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */ 2168793Sthompsa 3168793Sthompsa/* 4168793Sthompsa * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org> 5174721Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org> 6168793Sthompsa * 7168793Sthompsa * Permission to use, copy, modify, and distribute this software for any 8168793Sthompsa * purpose with or without fee is hereby granted, provided that the above 9168793Sthompsa * copyright notice and this permission notice appear in all copies. 10168793Sthompsa * 11168793Sthompsa * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12168793Sthompsa * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13168793Sthompsa * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14168793Sthompsa * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15168793Sthompsa * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16168793Sthompsa * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17168793Sthompsa * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18168793Sthompsa */ 19168793Sthompsa 20168793Sthompsa#include <sys/cdefs.h> 21168793Sthompsa__FBSDID("$FreeBSD$"); 22168793Sthompsa 23168793Sthompsa#include "opt_inet.h" 24168793Sthompsa#include "opt_inet6.h" 25168793Sthompsa 26168793Sthompsa#include <sys/param.h> 27168793Sthompsa#include <sys/kernel.h> 28168793Sthompsa#include <sys/malloc.h> 29168793Sthompsa#include <sys/mbuf.h> 30168793Sthompsa#include <sys/queue.h> 31168793Sthompsa#include <sys/socket.h> 32168793Sthompsa#include <sys/sockio.h> 33168793Sthompsa#include <sys/sysctl.h> 34168793Sthompsa#include <sys/module.h> 35168793Sthompsa#include <sys/priv.h> 36168793Sthompsa#include <sys/systm.h> 37168793Sthompsa#include <sys/proc.h> 38168793Sthompsa#include <sys/hash.h> 39169569Sthompsa#include <sys/lock.h> 40255038Sadrian#include <sys/rmlock.h> 41169329Sthompsa#include <sys/taskqueue.h> 42203548Seri#include <sys/eventhandler.h> 43168793Sthompsa 44168793Sthompsa#include <net/ethernet.h> 45168793Sthompsa#include <net/if.h> 46168793Sthompsa#include <net/if_clone.h> 47168793Sthompsa#include <net/if_arp.h> 48168793Sthompsa#include <net/if_dl.h> 49168793Sthompsa#include <net/if_llc.h> 50168793Sthompsa#include <net/if_media.h> 51168793Sthompsa#include <net/if_types.h> 52168793Sthompsa#include <net/if_var.h> 53168793Sthompsa#include <net/bpf.h> 54168793Sthompsa 55221130Sbz#if defined(INET) || defined(INET6) 56221130Sbz#include <netinet/in.h> 57265412Srmacklem#include <netinet/ip.h> 58221130Sbz#endif 59168793Sthompsa#ifdef INET 60168793Sthompsa#include <netinet/in_systm.h> 61168793Sthompsa#include <netinet/if_ether.h> 62168793Sthompsa#endif 63168793Sthompsa 64168793Sthompsa#ifdef INET6 65168793Sthompsa#include <netinet/ip6.h> 66252511Shrs#include <netinet6/in6_var.h> 67252511Shrs#include <netinet6/in6_ifattach.h> 68168793Sthompsa#endif 69168793Sthompsa 70168793Sthompsa#include <net/if_vlan_var.h> 71168793Sthompsa#include <net/if_lagg.h> 72168793Sthompsa#include <net/ieee8023ad_lacp.h> 73168793Sthompsa 74168793Sthompsa/* Special flags we should propagate to the lagg ports. */ 75168793Sthompsastatic struct { 76168793Sthompsa int flag; 77168793Sthompsa int (*func)(struct ifnet *, int); 78168793Sthompsa} lagg_pflags[] = { 79168793Sthompsa {IFF_PROMISC, ifpromisc}, 80168793Sthompsa {IFF_ALLMULTI, if_allmulti}, 81168793Sthompsa {0, NULL} 82168793Sthompsa}; 83168793Sthompsa 84168793SthompsaSLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */ 85170599Sthompsastatic struct mtx lagg_list_mtx; 86168793Sthompsaeventhandler_tag lagg_detach_cookie = NULL; 87168793Sthompsa 88168793Sthompsastatic int lagg_clone_create(struct if_clone *, int, caddr_t); 89168793Sthompsastatic void lagg_clone_destroy(struct ifnet *); 90241610Sglebiusstatic struct if_clone *lagg_cloner; 91241610Sglebiusstatic const char laggname[] = "lagg"; 92241610Sglebius 93168793Sthompsastatic void lagg_lladdr(struct lagg_softc *, uint8_t *); 94171661Sthompsastatic void lagg_capabilities(struct lagg_softc *); 95168793Sthompsastatic void lagg_port_lladdr(struct lagg_port *, uint8_t *); 96169329Sthompsastatic void lagg_port_setlladdr(void *, int); 97168793Sthompsastatic int lagg_port_create(struct lagg_softc *, struct ifnet *); 98168793Sthompsastatic int lagg_port_destroy(struct lagg_port *, int); 99168793Sthompsastatic struct mbuf *lagg_input(struct ifnet *, struct mbuf *); 100173895Sthompsastatic void lagg_linkstate(struct lagg_softc *); 101168793Sthompsastatic void lagg_port_state(struct ifnet *, int); 102168793Sthompsastatic int lagg_port_ioctl(struct ifnet *, u_long, caddr_t); 103168793Sthompsastatic int lagg_port_output(struct ifnet *, struct mbuf *, 104249925Sglebius const struct sockaddr *, struct route *); 105168793Sthompsastatic void lagg_port_ifdetach(void *arg __unused, struct ifnet *); 106201803Strasz#ifdef LAGG_PORT_STACKING 107168793Sthompsastatic int lagg_port_checkstacking(struct lagg_softc *); 108201803Strasz#endif 109168793Sthompsastatic void lagg_port2req(struct lagg_port *, struct lagg_reqport *); 110168793Sthompsastatic void lagg_init(void *); 111168793Sthompsastatic void lagg_stop(struct lagg_softc *); 112168793Sthompsastatic int lagg_ioctl(struct ifnet *, u_long, caddr_t); 113168793Sthompsastatic int lagg_ether_setmulti(struct lagg_softc *); 114168793Sthompsastatic int lagg_ether_cmdmulti(struct lagg_port *, int); 115168793Sthompsastatic int lagg_setflag(struct lagg_port *, int, int, 116168793Sthompsa int (*func)(struct ifnet *, int)); 117168793Sthompsastatic int lagg_setflags(struct lagg_port *, int status); 118240742Sglebiusstatic int lagg_transmit(struct ifnet *, struct mbuf *); 119240742Sglebiusstatic void lagg_qflush(struct ifnet *); 120168793Sthompsastatic int lagg_media_change(struct ifnet *); 121168793Sthompsastatic void lagg_media_status(struct ifnet *, struct ifmediareq *); 122168793Sthompsastatic struct lagg_port *lagg_link_active(struct lagg_softc *, 123168793Sthompsa struct lagg_port *); 124168793Sthompsastatic const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *); 125253314Sadrianstatic int lagg_sysctl_active(SYSCTL_HANDLER_ARGS); 126168793Sthompsa 127168793Sthompsa/* Simple round robin */ 128168793Sthompsastatic int lagg_rr_attach(struct lagg_softc *); 129168793Sthompsastatic int lagg_rr_detach(struct lagg_softc *); 130168793Sthompsastatic int lagg_rr_start(struct lagg_softc *, struct mbuf *); 131168793Sthompsastatic struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *, 132168793Sthompsa struct mbuf *); 133168793Sthompsa 134168793Sthompsa/* Active failover */ 135168793Sthompsastatic int lagg_fail_attach(struct lagg_softc *); 136168793Sthompsastatic int lagg_fail_detach(struct lagg_softc *); 137168793Sthompsastatic int lagg_fail_start(struct lagg_softc *, struct mbuf *); 138168793Sthompsastatic struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *, 139168793Sthompsa struct mbuf *); 140168793Sthompsa 141168793Sthompsa/* Loadbalancing */ 142168793Sthompsastatic int lagg_lb_attach(struct lagg_softc *); 143168793Sthompsastatic int lagg_lb_detach(struct lagg_softc *); 144168793Sthompsastatic int lagg_lb_port_create(struct lagg_port *); 145168793Sthompsastatic void lagg_lb_port_destroy(struct lagg_port *); 146168793Sthompsastatic int lagg_lb_start(struct lagg_softc *, struct mbuf *); 147168793Sthompsastatic struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *, 148168793Sthompsa struct mbuf *); 149168793Sthompsastatic int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *); 150168793Sthompsa 151168793Sthompsa/* 802.3ad LACP */ 152168793Sthompsastatic int lagg_lacp_attach(struct lagg_softc *); 153168793Sthompsastatic int lagg_lacp_detach(struct lagg_softc *); 154168793Sthompsastatic int lagg_lacp_start(struct lagg_softc *, struct mbuf *); 155168793Sthompsastatic struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *, 156168793Sthompsa struct mbuf *); 157168793Sthompsastatic void lagg_lacp_lladdr(struct lagg_softc *); 158168793Sthompsa 159249506Sglebiusstatic void lagg_callout(void *); 160249506Sglebius 161168793Sthompsa/* lagg protocol table */ 162168793Sthompsastatic const struct { 163168793Sthompsa int ti_proto; 164168793Sthompsa int (*ti_attach)(struct lagg_softc *); 165168793Sthompsa} lagg_protos[] = { 166168793Sthompsa { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach }, 167168793Sthompsa { LAGG_PROTO_FAILOVER, lagg_fail_attach }, 168168793Sthompsa { LAGG_PROTO_LOADBALANCE, lagg_lb_attach }, 169168793Sthompsa { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach }, 170168793Sthompsa { LAGG_PROTO_LACP, lagg_lacp_attach }, 171168793Sthompsa { LAGG_PROTO_NONE, NULL } 172168793Sthompsa}; 173168793Sthompsa 174212100SemasteSYSCTL_DECL(_net_link); 175253314SadrianSYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, 176227309Sed "Link Aggregation"); 177212100Semaste 178212100Semastestatic int lagg_failover_rx_all = 0; /* Allow input on any failover links */ 179212100SemasteSYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW, 180212100Semaste &lagg_failover_rx_all, 0, 181212100Semaste "Accept input from any interface in a failover lagg"); 182232080Sthompsastatic int def_use_flowid = 1; /* Default value for using M_FLOWID */ 183232080SthompsaTUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid); 184232080SthompsaSYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW, 185232080Sthompsa &def_use_flowid, 0, 186232080Sthompsa "Default setting for using flow id for load sharing"); 187260179Sscottlstatic int def_flowid_shift = 16; /* Default value for using M_FLOWID */ 188260179SscottlTUNABLE_INT("net.link.lagg.default_flowid_shift", &def_flowid_shift); 189260179SscottlSYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RW, 190260179Sscottl &def_flowid_shift, 0, 191260179Sscottl "Default setting for flowid shift for load sharing"); 192212100Semaste 193168793Sthompsastatic int 194168793Sthompsalagg_modevent(module_t mod, int type, void *data) 195168793Sthompsa{ 196168793Sthompsa 197168793Sthompsa switch (type) { 198168793Sthompsa case MOD_LOAD: 199168793Sthompsa mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF); 200168793Sthompsa SLIST_INIT(&lagg_list); 201241610Sglebius lagg_cloner = if_clone_simple(laggname, lagg_clone_create, 202241610Sglebius lagg_clone_destroy, 0); 203168793Sthompsa lagg_input_p = lagg_input; 204168793Sthompsa lagg_linkstate_p = lagg_port_state; 205168793Sthompsa lagg_detach_cookie = EVENTHANDLER_REGISTER( 206168793Sthompsa ifnet_departure_event, lagg_port_ifdetach, NULL, 207168793Sthompsa EVENTHANDLER_PRI_ANY); 208168793Sthompsa break; 209168793Sthompsa case MOD_UNLOAD: 210168793Sthompsa EVENTHANDLER_DEREGISTER(ifnet_departure_event, 211168793Sthompsa lagg_detach_cookie); 212241610Sglebius if_clone_detach(lagg_cloner); 213168793Sthompsa lagg_input_p = NULL; 214168793Sthompsa lagg_linkstate_p = NULL; 215168793Sthompsa mtx_destroy(&lagg_list_mtx); 216168793Sthompsa break; 217168793Sthompsa default: 218168793Sthompsa return (EOPNOTSUPP); 219168793Sthompsa } 220168793Sthompsa return (0); 221168793Sthompsa} 222168793Sthompsa 223168793Sthompsastatic moduledata_t lagg_mod = { 224168793Sthompsa "if_lagg", 225168793Sthompsa lagg_modevent, 226241394Skevlo 0 227168793Sthompsa}; 228168793Sthompsa 229168793SthompsaDECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 230224571SpluknetMODULE_VERSION(if_lagg, 1); 231168793Sthompsa 232203548Seri/* 233203548Seri * This routine is run via an vlan 234203548Seri * config EVENT 235203548Seri */ 236203548Seristatic void 237203548Serilagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 238203548Seri{ 239203548Seri struct lagg_softc *sc = ifp->if_softc; 240203548Seri struct lagg_port *lp; 241255038Sadrian struct rm_priotracker tracker; 242203548Seri 243203548Seri if (ifp->if_softc != arg) /* Not our event */ 244203548Seri return; 245203548Seri 246255038Sadrian LAGG_RLOCK(sc, &tracker); 247203548Seri if (!SLIST_EMPTY(&sc->sc_ports)) { 248203548Seri SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 249203548Seri EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag); 250203548Seri } 251255038Sadrian LAGG_RUNLOCK(sc, &tracker); 252203548Seri} 253203548Seri 254203548Seri/* 255203548Seri * This routine is run via an vlan 256203548Seri * unconfig EVENT 257203548Seri */ 258203548Seristatic void 259203548Serilagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 260203548Seri{ 261203548Seri struct lagg_softc *sc = ifp->if_softc; 262203548Seri struct lagg_port *lp; 263255038Sadrian struct rm_priotracker tracker; 264203548Seri 265203548Seri if (ifp->if_softc != arg) /* Not our event */ 266203548Seri return; 267203548Seri 268255038Sadrian LAGG_RLOCK(sc, &tracker); 269203548Seri if (!SLIST_EMPTY(&sc->sc_ports)) { 270203548Seri SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 271203548Seri EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag); 272203548Seri } 273255038Sadrian LAGG_RUNLOCK(sc, &tracker); 274203548Seri} 275203548Seri 276168793Sthompsastatic int 277168793Sthompsalagg_clone_create(struct if_clone *ifc, int unit, caddr_t params) 278168793Sthompsa{ 279168793Sthompsa struct lagg_softc *sc; 280168793Sthompsa struct ifnet *ifp; 281168793Sthompsa int i, error = 0; 282168793Sthompsa static const u_char eaddr[6]; /* 00:00:00:00:00:00 */ 283232008Sthompsa struct sysctl_oid *oid; 284232008Sthompsa char num[14]; /* sufficient for 32 bits */ 285168793Sthompsa 286168793Sthompsa sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); 287168793Sthompsa ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 288168793Sthompsa if (ifp == NULL) { 289168793Sthompsa free(sc, M_DEVBUF); 290168793Sthompsa return (ENOSPC); 291168793Sthompsa } 292168793Sthompsa 293249506Sglebius sc->sc_ipackets = counter_u64_alloc(M_WAITOK); 294249506Sglebius sc->sc_opackets = counter_u64_alloc(M_WAITOK); 295249506Sglebius sc->sc_ibytes = counter_u64_alloc(M_WAITOK); 296249506Sglebius sc->sc_obytes = counter_u64_alloc(M_WAITOK); 297249506Sglebius 298232008Sthompsa sysctl_ctx_init(&sc->ctx); 299232008Sthompsa snprintf(num, sizeof(num), "%u", unit); 300232080Sthompsa sc->use_flowid = def_use_flowid; 301260179Sscottl sc->flowid_shift = def_flowid_shift; 302253687Sadrian sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx, 303253687Sadrian &SYSCTL_NODE_CHILDREN(_net_link, lagg), 304232008Sthompsa OID_AUTO, num, CTLFLAG_RD, NULL, ""); 305232008Sthompsa SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 306260179Sscottl "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, 307260179Sscottl sc->use_flowid, "Use flow id for load sharing"); 308241619Semax SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 309260179Sscottl "flowid_shift", CTLTYPE_INT|CTLFLAG_RW, &sc->flowid_shift, 310260179Sscottl sc->flowid_shift, 311260179Sscottl "Shift flowid bits to prevent multiqueue collisions"); 312260179Sscottl SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 313241627Sdelphij "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count, 314241619Semax "Total number of ports"); 315253314Sadrian SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 316253314Sadrian "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active, 317253314Sadrian "I", "Total number of active ports"); 318253314Sadrian SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 319253314Sadrian "flapping", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_flapping, 320253314Sadrian sc->sc_flapping, "Total number of port change events"); 321232629Sthompsa /* Hash all layers by default */ 322232629Sthompsa sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4; 323232008Sthompsa 324168793Sthompsa sc->sc_proto = LAGG_PROTO_NONE; 325168793Sthompsa for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) { 326168793Sthompsa if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) { 327168793Sthompsa sc->sc_proto = lagg_protos[i].ti_proto; 328168793Sthompsa if ((error = lagg_protos[i].ti_attach(sc)) != 0) { 329227459Sbrooks if_free(ifp); 330168793Sthompsa free(sc, M_DEVBUF); 331168793Sthompsa return (error); 332168793Sthompsa } 333168793Sthompsa break; 334168793Sthompsa } 335168793Sthompsa } 336168793Sthompsa LAGG_LOCK_INIT(sc); 337255038Sadrian LAGG_CALLOUT_LOCK_INIT(sc); 338168793Sthompsa SLIST_INIT(&sc->sc_ports); 339169329Sthompsa TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc); 340168793Sthompsa 341255038Sadrian /* 342255038Sadrian * This uses the callout lock rather than the rmlock; one can't 343255038Sadrian * hold said rmlock during SWI. 344255038Sadrian */ 345255038Sadrian callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0); 346255038Sadrian 347168793Sthompsa /* Initialise pseudo media types */ 348168793Sthompsa ifmedia_init(&sc->sc_media, 0, lagg_media_change, 349168793Sthompsa lagg_media_status); 350168793Sthompsa ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL); 351168793Sthompsa ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO); 352168793Sthompsa 353241610Sglebius if_initname(ifp, laggname, unit); 354168793Sthompsa ifp->if_softc = sc; 355240742Sglebius ifp->if_transmit = lagg_transmit; 356240742Sglebius ifp->if_qflush = lagg_qflush; 357168793Sthompsa ifp->if_init = lagg_init; 358168793Sthompsa ifp->if_ioctl = lagg_ioctl; 359168793Sthompsa ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 360256218Sglebius ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS; 361168793Sthompsa 362168793Sthompsa /* 363227459Sbrooks * Attach as an ordinary ethernet device, children will be attached 364168793Sthompsa * as special device IFT_IEEE8023ADLAG. 365168793Sthompsa */ 366168793Sthompsa ether_ifattach(ifp, eaddr); 367168793Sthompsa 368203548Seri sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config, 369203548Seri lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST); 370203548Seri sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig, 371203548Seri lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST); 372203548Seri 373168793Sthompsa /* Insert into the global list of laggs */ 374168793Sthompsa mtx_lock(&lagg_list_mtx); 375168793Sthompsa SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries); 376168793Sthompsa mtx_unlock(&lagg_list_mtx); 377168793Sthompsa 378249506Sglebius callout_reset(&sc->sc_callout, hz, lagg_callout, sc); 379249506Sglebius 380168793Sthompsa return (0); 381168793Sthompsa} 382168793Sthompsa 383168793Sthompsastatic void 384168793Sthompsalagg_clone_destroy(struct ifnet *ifp) 385168793Sthompsa{ 386168793Sthompsa struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 387168793Sthompsa struct lagg_port *lp; 388168793Sthompsa 389169569Sthompsa LAGG_WLOCK(sc); 390168793Sthompsa 391168793Sthompsa lagg_stop(sc); 392168793Sthompsa ifp->if_flags &= ~IFF_UP; 393168793Sthompsa 394203548Seri EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach); 395203548Seri EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach); 396203548Seri 397168793Sthompsa /* Shutdown and remove lagg ports */ 398168793Sthompsa while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL) 399168793Sthompsa lagg_port_destroy(lp, 1); 400168793Sthompsa /* Unhook the aggregation protocol */ 401219275Seri if (sc->sc_detach != NULL) 402219275Seri (*sc->sc_detach)(sc); 403168793Sthompsa 404169569Sthompsa LAGG_WUNLOCK(sc); 405168793Sthompsa 406232008Sthompsa sysctl_ctx_free(&sc->ctx); 407168793Sthompsa ifmedia_removeall(&sc->sc_media); 408168793Sthompsa ether_ifdetach(ifp); 409227459Sbrooks if_free(ifp); 410168793Sthompsa 411255038Sadrian /* This grabs sc_callout_mtx, serialising it correctly */ 412249506Sglebius callout_drain(&sc->sc_callout); 413255038Sadrian 414255038Sadrian /* At this point it's drained; we can free this */ 415249506Sglebius counter_u64_free(sc->sc_ipackets); 416249506Sglebius counter_u64_free(sc->sc_opackets); 417249506Sglebius counter_u64_free(sc->sc_ibytes); 418249506Sglebius counter_u64_free(sc->sc_obytes); 419249506Sglebius 420168793Sthompsa mtx_lock(&lagg_list_mtx); 421168793Sthompsa SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries); 422168793Sthompsa mtx_unlock(&lagg_list_mtx); 423168793Sthompsa 424169329Sthompsa taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task); 425168793Sthompsa LAGG_LOCK_DESTROY(sc); 426255038Sadrian LAGG_CALLOUT_LOCK_DESTROY(sc); 427168793Sthompsa free(sc, M_DEVBUF); 428168793Sthompsa} 429168793Sthompsa 430168793Sthompsastatic void 431168793Sthompsalagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr) 432168793Sthompsa{ 433168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 434168793Sthompsa 435168793Sthompsa if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 436168793Sthompsa return; 437168793Sthompsa 438168793Sthompsa bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 439168793Sthompsa /* Let the protocol know the MAC has changed */ 440168793Sthompsa if (sc->sc_lladdr != NULL) 441168793Sthompsa (*sc->sc_lladdr)(sc); 442202588Sthompsa EVENTHANDLER_INVOKE(iflladdr_event, ifp); 443168793Sthompsa} 444168793Sthompsa 445171661Sthompsastatic void 446168793Sthompsalagg_capabilities(struct lagg_softc *sc) 447168793Sthompsa{ 448168793Sthompsa struct lagg_port *lp; 449171661Sthompsa int cap = ~0, ena = ~0; 450186195Sthompsa u_long hwa = ~0UL; 451265412Srmacklem#if defined(INET) || defined(INET6) 452265412Srmacklem u_int hw_tsomax = IP_MAXPACKET; /* Initialize to the maximum value. */ 453265412Srmacklem#else 454265412Srmacklem u_int hw_tsomax = ~0; /* if_hw_tsomax is only for INET/INET6, but.. */ 455265412Srmacklem#endif 456168793Sthompsa 457169569Sthompsa LAGG_WLOCK_ASSERT(sc); 458168793Sthompsa 459168793Sthompsa /* Get capabilities from the lagg ports */ 460171661Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 461171661Sthompsa cap &= lp->lp_ifp->if_capabilities; 462171661Sthompsa ena &= lp->lp_ifp->if_capenable; 463186195Sthompsa hwa &= lp->lp_ifp->if_hwassist; 464265412Srmacklem /* Set to the minimum value of the lagg ports. */ 465265412Srmacklem if (lp->lp_ifp->if_hw_tsomax < hw_tsomax && 466265412Srmacklem lp->lp_ifp->if_hw_tsomax > 0) 467265412Srmacklem hw_tsomax = lp->lp_ifp->if_hw_tsomax; 468171661Sthompsa } 469171661Sthompsa cap = (cap == ~0 ? 0 : cap); 470171661Sthompsa ena = (ena == ~0 ? 0 : ena); 471186195Sthompsa hwa = (hwa == ~0 ? 0 : hwa); 472168793Sthompsa 473171661Sthompsa if (sc->sc_ifp->if_capabilities != cap || 474186195Sthompsa sc->sc_ifp->if_capenable != ena || 475265412Srmacklem sc->sc_ifp->if_hwassist != hwa || 476265412Srmacklem sc->sc_ifp->if_hw_tsomax != hw_tsomax) { 477171661Sthompsa sc->sc_ifp->if_capabilities = cap; 478171661Sthompsa sc->sc_ifp->if_capenable = ena; 479186195Sthompsa sc->sc_ifp->if_hwassist = hwa; 480265412Srmacklem sc->sc_ifp->if_hw_tsomax = hw_tsomax; 481171661Sthompsa getmicrotime(&sc->sc_ifp->if_lastchange); 482171661Sthompsa 483171661Sthompsa if (sc->sc_ifflags & IFF_DEBUG) 484171661Sthompsa if_printf(sc->sc_ifp, 485171661Sthompsa "capabilities 0x%08x enabled 0x%08x\n", cap, ena); 486168793Sthompsa } 487168793Sthompsa} 488168793Sthompsa 489168793Sthompsastatic void 490168793Sthompsalagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr) 491168793Sthompsa{ 492170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 493168793Sthompsa struct ifnet *ifp = lp->lp_ifp; 494169329Sthompsa struct lagg_llq *llq; 495169329Sthompsa int pending = 0; 496168793Sthompsa 497169569Sthompsa LAGG_WLOCK_ASSERT(sc); 498169329Sthompsa 499169328Sthompsa if (lp->lp_detaching || 500169328Sthompsa memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0) 501168793Sthompsa return; 502168793Sthompsa 503169329Sthompsa /* Check to make sure its not already queued to be changed */ 504169329Sthompsa SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 505169329Sthompsa if (llq->llq_ifp == ifp) { 506169329Sthompsa pending = 1; 507169329Sthompsa break; 508169329Sthompsa } 509169329Sthompsa } 510168793Sthompsa 511169329Sthompsa if (!pending) { 512169329Sthompsa llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT); 513169329Sthompsa if (llq == NULL) /* XXX what to do */ 514169329Sthompsa return; 515169329Sthompsa } 516169329Sthompsa 517169329Sthompsa /* Update the lladdr even if pending, it may have changed */ 518169329Sthompsa llq->llq_ifp = ifp; 519169329Sthompsa bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN); 520169329Sthompsa 521169329Sthompsa if (!pending) 522169329Sthompsa SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries); 523169329Sthompsa 524169329Sthompsa taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task); 525168793Sthompsa} 526168793Sthompsa 527169329Sthompsa/* 528169329Sthompsa * Set the interface MAC address from a taskqueue to avoid a LOR. 529169329Sthompsa */ 530169329Sthompsastatic void 531169329Sthompsalagg_port_setlladdr(void *arg, int pending) 532169329Sthompsa{ 533169329Sthompsa struct lagg_softc *sc = (struct lagg_softc *)arg; 534169329Sthompsa struct lagg_llq *llq, *head; 535169329Sthompsa struct ifnet *ifp; 536169329Sthompsa int error; 537169329Sthompsa 538169329Sthompsa /* Grab a local reference of the queue and remove it from the softc */ 539169569Sthompsa LAGG_WLOCK(sc); 540169329Sthompsa head = SLIST_FIRST(&sc->sc_llq_head); 541169329Sthompsa SLIST_FIRST(&sc->sc_llq_head) = NULL; 542169569Sthompsa LAGG_WUNLOCK(sc); 543169329Sthompsa 544169329Sthompsa /* 545169329Sthompsa * Traverse the queue and set the lladdr on each ifp. It is safe to do 546169329Sthompsa * unlocked as we have the only reference to it. 547169329Sthompsa */ 548169329Sthompsa for (llq = head; llq != NULL; llq = head) { 549169329Sthompsa ifp = llq->llq_ifp; 550169329Sthompsa 551169329Sthompsa /* Set the link layer address */ 552251490Strociny CURVNET_SET(ifp->if_vnet); 553169329Sthompsa error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN); 554251490Strociny CURVNET_RESTORE(); 555169329Sthompsa if (error) 556169329Sthompsa printf("%s: setlladdr failed on %s\n", __func__, 557169329Sthompsa ifp->if_xname); 558169329Sthompsa 559169329Sthompsa head = SLIST_NEXT(llq, llq_entries); 560169329Sthompsa free(llq, M_DEVBUF); 561169329Sthompsa } 562169329Sthompsa} 563169329Sthompsa 564168793Sthompsastatic int 565168793Sthompsalagg_port_create(struct lagg_softc *sc, struct ifnet *ifp) 566168793Sthompsa{ 567168793Sthompsa struct lagg_softc *sc_ptr; 568168793Sthompsa struct lagg_port *lp; 569168793Sthompsa int error = 0; 570168793Sthompsa 571169569Sthompsa LAGG_WLOCK_ASSERT(sc); 572168793Sthompsa 573168793Sthompsa /* Limit the maximal number of lagg ports */ 574168793Sthompsa if (sc->sc_count >= LAGG_MAX_PORTS) 575168793Sthompsa return (ENOSPC); 576168793Sthompsa 577168793Sthompsa /* Check if port has already been associated to a lagg */ 578236178Srea if (ifp->if_lagg != NULL) { 579236178Srea /* Port is already in the current lagg? */ 580236178Srea lp = (struct lagg_port *)ifp->if_lagg; 581236178Srea if (lp->lp_softc == sc) 582236178Srea return (EEXIST); 583168793Sthompsa return (EBUSY); 584236178Srea } 585168793Sthompsa 586168793Sthompsa /* XXX Disallow non-ethernet interfaces (this should be any of 802) */ 587168793Sthompsa if (ifp->if_type != IFT_ETHER) 588168793Sthompsa return (EPROTONOSUPPORT); 589168793Sthompsa 590252511Shrs#ifdef INET6 591252511Shrs /* 592252511Shrs * The member interface should not have inet6 address because 593252511Shrs * two interfaces with a valid link-local scope zone must not be 594252511Shrs * merged in any form. This restriction is needed to 595252511Shrs * prevent violation of link-local scope zone. Attempts to 596252511Shrs * add a member interface which has inet6 addresses triggers 597252511Shrs * removal of all inet6 addresses on the member interface. 598252511Shrs */ 599252511Shrs SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 600252511Shrs if (in6ifa_llaonifp(lp->lp_ifp)) { 601252511Shrs in6_ifdetach(lp->lp_ifp); 602252511Shrs if_printf(sc->sc_ifp, 603252511Shrs "IPv6 addresses on %s have been removed " 604252511Shrs "before adding it as a member to prevent " 605252511Shrs "IPv6 address scope violation.\n", 606252511Shrs lp->lp_ifp->if_xname); 607252511Shrs } 608252511Shrs } 609252511Shrs if (in6ifa_llaonifp(ifp)) { 610252511Shrs in6_ifdetach(ifp); 611252511Shrs if_printf(sc->sc_ifp, 612252511Shrs "IPv6 addresses on %s have been removed " 613252511Shrs "before adding it as a member to prevent " 614252511Shrs "IPv6 address scope violation.\n", 615252511Shrs ifp->if_xname); 616252511Shrs } 617252511Shrs#endif 618171661Sthompsa /* Allow the first Ethernet member to define the MTU */ 619171661Sthompsa if (SLIST_EMPTY(&sc->sc_ports)) 620171661Sthompsa sc->sc_ifp->if_mtu = ifp->if_mtu; 621171661Sthompsa else if (sc->sc_ifp->if_mtu != ifp->if_mtu) { 622171661Sthompsa if_printf(sc->sc_ifp, "invalid MTU for %s\n", 623171661Sthompsa ifp->if_xname); 624171661Sthompsa return (EINVAL); 625171661Sthompsa } 626171661Sthompsa 627168793Sthompsa if ((lp = malloc(sizeof(struct lagg_port), 628168793Sthompsa M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 629168793Sthompsa return (ENOMEM); 630168793Sthompsa 631168793Sthompsa /* Check if port is a stacked lagg */ 632168793Sthompsa mtx_lock(&lagg_list_mtx); 633168793Sthompsa SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) { 634168793Sthompsa if (ifp == sc_ptr->sc_ifp) { 635168793Sthompsa mtx_unlock(&lagg_list_mtx); 636168793Sthompsa free(lp, M_DEVBUF); 637168793Sthompsa return (EINVAL); 638201803Strasz /* XXX disable stacking for the moment, its untested */ 639201803Strasz#ifdef LAGG_PORT_STACKING 640168793Sthompsa lp->lp_flags |= LAGG_PORT_STACK; 641168793Sthompsa if (lagg_port_checkstacking(sc_ptr) >= 642168793Sthompsa LAGG_MAX_STACKING) { 643168793Sthompsa mtx_unlock(&lagg_list_mtx); 644168793Sthompsa free(lp, M_DEVBUF); 645168793Sthompsa return (E2BIG); 646168793Sthompsa } 647201803Strasz#endif 648168793Sthompsa } 649168793Sthompsa } 650168793Sthompsa mtx_unlock(&lagg_list_mtx); 651168793Sthompsa 652168793Sthompsa /* Change the interface type */ 653168793Sthompsa lp->lp_iftype = ifp->if_type; 654168793Sthompsa ifp->if_type = IFT_IEEE8023ADLAG; 655168793Sthompsa ifp->if_lagg = lp; 656168793Sthompsa lp->lp_ioctl = ifp->if_ioctl; 657168793Sthompsa ifp->if_ioctl = lagg_port_ioctl; 658168793Sthompsa lp->lp_output = ifp->if_output; 659168793Sthompsa ifp->if_output = lagg_port_output; 660168793Sthompsa 661168793Sthompsa lp->lp_ifp = ifp; 662170599Sthompsa lp->lp_softc = sc; 663168793Sthompsa 664168793Sthompsa /* Save port link layer address */ 665168793Sthompsa bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN); 666168793Sthompsa 667168793Sthompsa if (SLIST_EMPTY(&sc->sc_ports)) { 668168793Sthompsa sc->sc_primary = lp; 669168793Sthompsa lagg_lladdr(sc, IF_LLADDR(ifp)); 670168793Sthompsa } else { 671168793Sthompsa /* Update link layer address for this port */ 672168793Sthompsa lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp)); 673168793Sthompsa } 674168793Sthompsa 675168793Sthompsa /* Insert into the list of ports */ 676168793Sthompsa SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries); 677168793Sthompsa sc->sc_count++; 678168793Sthompsa 679168793Sthompsa /* Update lagg capabilities */ 680171661Sthompsa lagg_capabilities(sc); 681173895Sthompsa lagg_linkstate(sc); 682168793Sthompsa 683168793Sthompsa /* Add multicast addresses and interface flags to this port */ 684168793Sthompsa lagg_ether_cmdmulti(lp, 1); 685168793Sthompsa lagg_setflags(lp, 1); 686168793Sthompsa 687168793Sthompsa if (sc->sc_port_create != NULL) 688168793Sthompsa error = (*sc->sc_port_create)(lp); 689168793Sthompsa if (error) { 690170599Sthompsa /* remove the port again, without calling sc_port_destroy */ 691168793Sthompsa lagg_port_destroy(lp, 0); 692168793Sthompsa return (error); 693168793Sthompsa } 694168793Sthompsa 695168793Sthompsa return (error); 696168793Sthompsa} 697168793Sthompsa 698201803Strasz#ifdef LAGG_PORT_STACKING 699168793Sthompsastatic int 700168793Sthompsalagg_port_checkstacking(struct lagg_softc *sc) 701168793Sthompsa{ 702168793Sthompsa struct lagg_softc *sc_ptr; 703168793Sthompsa struct lagg_port *lp; 704168793Sthompsa int m = 0; 705168793Sthompsa 706169569Sthompsa LAGG_WLOCK_ASSERT(sc); 707168793Sthompsa 708168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 709168793Sthompsa if (lp->lp_flags & LAGG_PORT_STACK) { 710168793Sthompsa sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc; 711168793Sthompsa m = MAX(m, lagg_port_checkstacking(sc_ptr)); 712168793Sthompsa } 713168793Sthompsa } 714168793Sthompsa 715168793Sthompsa return (m + 1); 716168793Sthompsa} 717201803Strasz#endif 718168793Sthompsa 719168793Sthompsastatic int 720168793Sthompsalagg_port_destroy(struct lagg_port *lp, int runpd) 721168793Sthompsa{ 722170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 723168793Sthompsa struct lagg_port *lp_ptr; 724169329Sthompsa struct lagg_llq *llq; 725168793Sthompsa struct ifnet *ifp = lp->lp_ifp; 726168793Sthompsa 727169569Sthompsa LAGG_WLOCK_ASSERT(sc); 728168793Sthompsa 729168793Sthompsa if (runpd && sc->sc_port_destroy != NULL) 730168793Sthompsa (*sc->sc_port_destroy)(lp); 731168793Sthompsa 732169328Sthompsa /* 733169328Sthompsa * Remove multicast addresses and interface flags from this port and 734169328Sthompsa * reset the MAC address, skip if the interface is being detached. 735169328Sthompsa */ 736169328Sthompsa if (!lp->lp_detaching) { 737169328Sthompsa lagg_ether_cmdmulti(lp, 0); 738169328Sthompsa lagg_setflags(lp, 0); 739169328Sthompsa lagg_port_lladdr(lp, lp->lp_lladdr); 740169328Sthompsa } 741168793Sthompsa 742168793Sthompsa /* Restore interface */ 743168793Sthompsa ifp->if_type = lp->lp_iftype; 744168793Sthompsa ifp->if_ioctl = lp->lp_ioctl; 745168793Sthompsa ifp->if_output = lp->lp_output; 746168793Sthompsa ifp->if_lagg = NULL; 747168793Sthompsa 748168793Sthompsa /* Finally, remove the port from the lagg */ 749168793Sthompsa SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries); 750168793Sthompsa sc->sc_count--; 751168793Sthompsa 752168793Sthompsa /* Update the primary interface */ 753168793Sthompsa if (lp == sc->sc_primary) { 754168793Sthompsa uint8_t lladdr[ETHER_ADDR_LEN]; 755168793Sthompsa 756168793Sthompsa if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) { 757168793Sthompsa bzero(&lladdr, ETHER_ADDR_LEN); 758168793Sthompsa } else { 759168793Sthompsa bcopy(lp_ptr->lp_lladdr, 760168793Sthompsa lladdr, ETHER_ADDR_LEN); 761168793Sthompsa } 762168793Sthompsa lagg_lladdr(sc, lladdr); 763168793Sthompsa sc->sc_primary = lp_ptr; 764168793Sthompsa 765168793Sthompsa /* Update link layer address for each port */ 766168793Sthompsa SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries) 767168793Sthompsa lagg_port_lladdr(lp_ptr, lladdr); 768168793Sthompsa } 769168793Sthompsa 770169329Sthompsa /* Remove any pending lladdr changes from the queue */ 771169329Sthompsa if (lp->lp_detaching) { 772169329Sthompsa SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) { 773169329Sthompsa if (llq->llq_ifp == ifp) { 774169329Sthompsa SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq, 775169329Sthompsa llq_entries); 776169329Sthompsa free(llq, M_DEVBUF); 777169329Sthompsa break; /* Only appears once */ 778169329Sthompsa } 779169329Sthompsa } 780169329Sthompsa } 781169329Sthompsa 782168793Sthompsa if (lp->lp_ifflags) 783168793Sthompsa if_printf(ifp, "%s: lp_ifflags unclean\n", __func__); 784168793Sthompsa 785168793Sthompsa free(lp, M_DEVBUF); 786168793Sthompsa 787168793Sthompsa /* Update lagg capabilities */ 788171661Sthompsa lagg_capabilities(sc); 789173895Sthompsa lagg_linkstate(sc); 790168793Sthompsa 791168793Sthompsa return (0); 792168793Sthompsa} 793168793Sthompsa 794168793Sthompsastatic int 795168793Sthompsalagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 796168793Sthompsa{ 797168793Sthompsa struct lagg_reqport *rp = (struct lagg_reqport *)data; 798168793Sthompsa struct lagg_softc *sc; 799168793Sthompsa struct lagg_port *lp = NULL; 800168793Sthompsa int error = 0; 801255038Sadrian struct rm_priotracker tracker; 802168793Sthompsa 803168793Sthompsa /* Should be checked by the caller */ 804168793Sthompsa if (ifp->if_type != IFT_IEEE8023ADLAG || 805170599Sthompsa (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL) 806168793Sthompsa goto fallback; 807168793Sthompsa 808168793Sthompsa switch (cmd) { 809168793Sthompsa case SIOCGLAGGPORT: 810168793Sthompsa if (rp->rp_portname[0] == '\0' || 811168793Sthompsa ifunit(rp->rp_portname) != ifp) { 812168793Sthompsa error = EINVAL; 813168793Sthompsa break; 814168793Sthompsa } 815168793Sthompsa 816255038Sadrian LAGG_RLOCK(sc, &tracker); 817171603Sthompsa if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) { 818168793Sthompsa error = ENOENT; 819255038Sadrian LAGG_RUNLOCK(sc, &tracker); 820168793Sthompsa break; 821168793Sthompsa } 822168793Sthompsa 823168793Sthompsa lagg_port2req(lp, rp); 824255038Sadrian LAGG_RUNLOCK(sc, &tracker); 825168793Sthompsa break; 826171661Sthompsa 827171661Sthompsa case SIOCSIFCAP: 828171661Sthompsa if (lp->lp_ioctl == NULL) { 829171661Sthompsa error = EINVAL; 830171661Sthompsa break; 831171661Sthompsa } 832171661Sthompsa error = (*lp->lp_ioctl)(ifp, cmd, data); 833171661Sthompsa if (error) 834171661Sthompsa break; 835171661Sthompsa 836171661Sthompsa /* Update lagg interface capabilities */ 837171661Sthompsa LAGG_WLOCK(sc); 838171661Sthompsa lagg_capabilities(sc); 839171661Sthompsa LAGG_WUNLOCK(sc); 840171661Sthompsa break; 841171661Sthompsa 842171661Sthompsa case SIOCSIFMTU: 843171661Sthompsa /* Do not allow the MTU to be changed once joined */ 844171661Sthompsa error = EINVAL; 845171661Sthompsa break; 846171661Sthompsa 847168793Sthompsa default: 848168793Sthompsa goto fallback; 849168793Sthompsa } 850168793Sthompsa 851168793Sthompsa return (error); 852168793Sthompsa 853168793Sthompsafallback: 854169340Sthompsa if (lp->lp_ioctl != NULL) 855168793Sthompsa return ((*lp->lp_ioctl)(ifp, cmd, data)); 856168793Sthompsa 857168793Sthompsa return (EINVAL); 858168793Sthompsa} 859168793Sthompsa 860234936Semaste/* 861234936Semaste * For direct output to child ports. 862234936Semaste */ 863168793Sthompsastatic int 864168793Sthompsalagg_port_output(struct ifnet *ifp, struct mbuf *m, 865249925Sglebius const struct sockaddr *dst, struct route *ro) 866168793Sthompsa{ 867168793Sthompsa struct lagg_port *lp = ifp->if_lagg; 868168793Sthompsa 869168793Sthompsa switch (dst->sa_family) { 870168793Sthompsa case pseudo_AF_HDRCMPLT: 871168793Sthompsa case AF_UNSPEC: 872191148Skmacy return ((*lp->lp_output)(ifp, m, dst, ro)); 873168793Sthompsa } 874168793Sthompsa 875168793Sthompsa /* drop any other frames */ 876168793Sthompsa m_freem(m); 877245741Sglebius return (ENETDOWN); 878168793Sthompsa} 879168793Sthompsa 880168793Sthompsastatic void 881168793Sthompsalagg_port_ifdetach(void *arg __unused, struct ifnet *ifp) 882168793Sthompsa{ 883168793Sthompsa struct lagg_port *lp; 884168793Sthompsa struct lagg_softc *sc; 885168793Sthompsa 886168793Sthompsa if ((lp = ifp->if_lagg) == NULL) 887168793Sthompsa return; 888237852Sthompsa /* If the ifnet is just being renamed, don't do anything. */ 889237852Sthompsa if (ifp->if_flags & IFF_RENAMING) 890237852Sthompsa return; 891168793Sthompsa 892170599Sthompsa sc = lp->lp_softc; 893168793Sthompsa 894169569Sthompsa LAGG_WLOCK(sc); 895169328Sthompsa lp->lp_detaching = 1; 896168793Sthompsa lagg_port_destroy(lp, 1); 897169569Sthompsa LAGG_WUNLOCK(sc); 898168793Sthompsa} 899168793Sthompsa 900168793Sthompsastatic void 901168793Sthompsalagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp) 902168793Sthompsa{ 903170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 904172020Sthompsa 905168793Sthompsa strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname)); 906168793Sthompsa strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname)); 907168793Sthompsa rp->rp_prio = lp->lp_prio; 908168793Sthompsa rp->rp_flags = lp->lp_flags; 909171247Sthompsa if (sc->sc_portreq != NULL) 910171247Sthompsa (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc); 911168793Sthompsa 912168793Sthompsa /* Add protocol specific flags */ 913168793Sthompsa switch (sc->sc_proto) { 914168793Sthompsa case LAGG_PROTO_FAILOVER: 915168793Sthompsa if (lp == sc->sc_primary) 916169204Sthompsa rp->rp_flags |= LAGG_PORT_MASTER; 917172020Sthompsa if (lp == lagg_link_active(sc, sc->sc_primary)) 918172020Sthompsa rp->rp_flags |= LAGG_PORT_ACTIVE; 919172020Sthompsa break; 920172020Sthompsa 921168793Sthompsa case LAGG_PROTO_ROUNDROBIN: 922168793Sthompsa case LAGG_PROTO_LOADBALANCE: 923168793Sthompsa case LAGG_PROTO_ETHERCHANNEL: 924168793Sthompsa if (LAGG_PORTACTIVE(lp)) 925168793Sthompsa rp->rp_flags |= LAGG_PORT_ACTIVE; 926168793Sthompsa break; 927168793Sthompsa 928168793Sthompsa case LAGG_PROTO_LACP: 929168793Sthompsa /* LACP has a different definition of active */ 930177274Sthompsa if (lacp_isactive(lp)) 931168793Sthompsa rp->rp_flags |= LAGG_PORT_ACTIVE; 932177274Sthompsa if (lacp_iscollecting(lp)) 933177274Sthompsa rp->rp_flags |= LAGG_PORT_COLLECTING; 934177274Sthompsa if (lacp_isdistributing(lp)) 935177274Sthompsa rp->rp_flags |= LAGG_PORT_DISTRIBUTING; 936168793Sthompsa break; 937168793Sthompsa } 938168793Sthompsa 939168793Sthompsa} 940168793Sthompsa 941168793Sthompsastatic void 942168793Sthompsalagg_init(void *xsc) 943168793Sthompsa{ 944168793Sthompsa struct lagg_softc *sc = (struct lagg_softc *)xsc; 945168793Sthompsa struct lagg_port *lp; 946168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 947168793Sthompsa 948168793Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 949168793Sthompsa return; 950168793Sthompsa 951169569Sthompsa LAGG_WLOCK(sc); 952168793Sthompsa 953168793Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 954168793Sthompsa /* Update the port lladdrs */ 955168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 956168793Sthompsa lagg_port_lladdr(lp, IF_LLADDR(ifp)); 957168793Sthompsa 958168793Sthompsa if (sc->sc_init != NULL) 959168793Sthompsa (*sc->sc_init)(sc); 960168793Sthompsa 961169569Sthompsa LAGG_WUNLOCK(sc); 962168793Sthompsa} 963168793Sthompsa 964168793Sthompsastatic void 965168793Sthompsalagg_stop(struct lagg_softc *sc) 966168793Sthompsa{ 967168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 968168793Sthompsa 969169569Sthompsa LAGG_WLOCK_ASSERT(sc); 970168793Sthompsa 971168793Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 972168793Sthompsa return; 973168793Sthompsa 974168793Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 975168793Sthompsa 976168793Sthompsa if (sc->sc_stop != NULL) 977168793Sthompsa (*sc->sc_stop)(sc); 978168793Sthompsa} 979168793Sthompsa 980168793Sthompsastatic int 981168793Sthompsalagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 982168793Sthompsa{ 983168793Sthompsa struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 984168793Sthompsa struct lagg_reqall *ra = (struct lagg_reqall *)data; 985168793Sthompsa struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf; 986232629Sthompsa struct lagg_reqflags *rf = (struct lagg_reqflags *)data; 987168793Sthompsa struct ifreq *ifr = (struct ifreq *)data; 988168793Sthompsa struct lagg_port *lp; 989168793Sthompsa struct ifnet *tpif; 990168793Sthompsa struct thread *td = curthread; 991171603Sthompsa char *buf, *outbuf; 992171603Sthompsa int count, buflen, len, error = 0; 993255038Sadrian struct rm_priotracker tracker; 994168793Sthompsa 995168793Sthompsa bzero(&rpbuf, sizeof(rpbuf)); 996168793Sthompsa 997168793Sthompsa switch (cmd) { 998168793Sthompsa case SIOCGLAGG: 999255038Sadrian LAGG_RLOCK(sc, &tracker); 1000171603Sthompsa count = 0; 1001171603Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1002171603Sthompsa count++; 1003171603Sthompsa buflen = count * sizeof(struct lagg_reqport); 1004255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1005171603Sthompsa 1006171603Sthompsa outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 1007171603Sthompsa 1008255038Sadrian LAGG_RLOCK(sc, &tracker); 1009168793Sthompsa ra->ra_proto = sc->sc_proto; 1010171247Sthompsa if (sc->sc_req != NULL) 1011171247Sthompsa (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc); 1012171603Sthompsa 1013171603Sthompsa count = 0; 1014171603Sthompsa buf = outbuf; 1015171603Sthompsa len = min(ra->ra_size, buflen); 1016171603Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1017171603Sthompsa if (len < sizeof(rpbuf)) 1018171603Sthompsa break; 1019171603Sthompsa 1020168793Sthompsa lagg_port2req(lp, &rpbuf); 1021171603Sthompsa memcpy(buf, &rpbuf, sizeof(rpbuf)); 1022171603Sthompsa count++; 1023171603Sthompsa buf += sizeof(rpbuf); 1024171603Sthompsa len -= sizeof(rpbuf); 1025168793Sthompsa } 1026255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1027171603Sthompsa ra->ra_ports = count; 1028171603Sthompsa ra->ra_size = count * sizeof(rpbuf); 1029171603Sthompsa error = copyout(outbuf, ra->ra_port, ra->ra_size); 1030171603Sthompsa free(outbuf, M_TEMP); 1031168793Sthompsa break; 1032168793Sthompsa case SIOCSLAGG: 1033168793Sthompsa error = priv_check(td, PRIV_NET_LAGG); 1034168793Sthompsa if (error) 1035168793Sthompsa break; 1036168793Sthompsa if (ra->ra_proto >= LAGG_PROTO_MAX) { 1037168793Sthompsa error = EPROTONOSUPPORT; 1038168793Sthompsa break; 1039168793Sthompsa } 1040234163Sthompsa LAGG_WLOCK(sc); 1041168793Sthompsa if (sc->sc_proto != LAGG_PROTO_NONE) { 1042234163Sthompsa /* Reset protocol first in case detach unlocks */ 1043234163Sthompsa sc->sc_proto = LAGG_PROTO_NONE; 1044168793Sthompsa error = sc->sc_detach(sc); 1045168793Sthompsa sc->sc_detach = NULL; 1046168793Sthompsa sc->sc_start = NULL; 1047168793Sthompsa sc->sc_input = NULL; 1048168793Sthompsa sc->sc_port_create = NULL; 1049168793Sthompsa sc->sc_port_destroy = NULL; 1050168793Sthompsa sc->sc_linkstate = NULL; 1051168793Sthompsa sc->sc_init = NULL; 1052168793Sthompsa sc->sc_stop = NULL; 1053168793Sthompsa sc->sc_lladdr = NULL; 1054171247Sthompsa sc->sc_req = NULL; 1055171247Sthompsa sc->sc_portreq = NULL; 1056234163Sthompsa } else if (sc->sc_input != NULL) { 1057234163Sthompsa /* Still detaching */ 1058234163Sthompsa error = EBUSY; 1059234163Sthompsa } 1060234163Sthompsa if (error != 0) { 1061171603Sthompsa LAGG_WUNLOCK(sc); 1062234163Sthompsa break; 1063168793Sthompsa } 1064171603Sthompsa for (int i = 0; i < (sizeof(lagg_protos) / 1065168793Sthompsa sizeof(lagg_protos[0])); i++) { 1066168793Sthompsa if (lagg_protos[i].ti_proto == ra->ra_proto) { 1067168793Sthompsa if (sc->sc_ifflags & IFF_DEBUG) 1068168793Sthompsa printf("%s: using proto %u\n", 1069168793Sthompsa sc->sc_ifname, 1070168793Sthompsa lagg_protos[i].ti_proto); 1071168793Sthompsa sc->sc_proto = lagg_protos[i].ti_proto; 1072168793Sthompsa if (sc->sc_proto != LAGG_PROTO_NONE) 1073168793Sthompsa error = lagg_protos[i].ti_attach(sc); 1074171603Sthompsa LAGG_WUNLOCK(sc); 1075171603Sthompsa return (error); 1076168793Sthompsa } 1077168793Sthompsa } 1078234163Sthompsa LAGG_WUNLOCK(sc); 1079168793Sthompsa error = EPROTONOSUPPORT; 1080168793Sthompsa break; 1081232629Sthompsa case SIOCGLAGGFLAGS: 1082232629Sthompsa rf->rf_flags = sc->sc_flags; 1083232629Sthompsa break; 1084232629Sthompsa case SIOCSLAGGHASH: 1085232629Sthompsa error = priv_check(td, PRIV_NET_LAGG); 1086232629Sthompsa if (error) 1087232629Sthompsa break; 1088232629Sthompsa if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) { 1089232629Sthompsa error = EINVAL; 1090232629Sthompsa break; 1091232629Sthompsa } 1092232629Sthompsa LAGG_WLOCK(sc); 1093232629Sthompsa sc->sc_flags &= ~LAGG_F_HASHMASK; 1094232629Sthompsa sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK; 1095232629Sthompsa LAGG_WUNLOCK(sc); 1096232629Sthompsa break; 1097168793Sthompsa case SIOCGLAGGPORT: 1098168793Sthompsa if (rp->rp_portname[0] == '\0' || 1099168793Sthompsa (tpif = ifunit(rp->rp_portname)) == NULL) { 1100168793Sthompsa error = EINVAL; 1101168793Sthompsa break; 1102168793Sthompsa } 1103168793Sthompsa 1104255038Sadrian LAGG_RLOCK(sc, &tracker); 1105168793Sthompsa if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1106170599Sthompsa lp->lp_softc != sc) { 1107168793Sthompsa error = ENOENT; 1108255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1109168793Sthompsa break; 1110168793Sthompsa } 1111168793Sthompsa 1112168793Sthompsa lagg_port2req(lp, rp); 1113255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1114168793Sthompsa break; 1115168793Sthompsa case SIOCSLAGGPORT: 1116168793Sthompsa error = priv_check(td, PRIV_NET_LAGG); 1117168793Sthompsa if (error) 1118168793Sthompsa break; 1119168793Sthompsa if (rp->rp_portname[0] == '\0' || 1120168793Sthompsa (tpif = ifunit(rp->rp_portname)) == NULL) { 1121168793Sthompsa error = EINVAL; 1122168793Sthompsa break; 1123168793Sthompsa } 1124171603Sthompsa LAGG_WLOCK(sc); 1125168793Sthompsa error = lagg_port_create(sc, tpif); 1126171603Sthompsa LAGG_WUNLOCK(sc); 1127168793Sthompsa break; 1128168793Sthompsa case SIOCSLAGGDELPORT: 1129168793Sthompsa error = priv_check(td, PRIV_NET_LAGG); 1130168793Sthompsa if (error) 1131168793Sthompsa break; 1132168793Sthompsa if (rp->rp_portname[0] == '\0' || 1133168793Sthompsa (tpif = ifunit(rp->rp_portname)) == NULL) { 1134168793Sthompsa error = EINVAL; 1135168793Sthompsa break; 1136168793Sthompsa } 1137168793Sthompsa 1138171603Sthompsa LAGG_WLOCK(sc); 1139168793Sthompsa if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL || 1140170599Sthompsa lp->lp_softc != sc) { 1141168793Sthompsa error = ENOENT; 1142171603Sthompsa LAGG_WUNLOCK(sc); 1143168793Sthompsa break; 1144168793Sthompsa } 1145168793Sthompsa 1146168793Sthompsa error = lagg_port_destroy(lp, 1); 1147171603Sthompsa LAGG_WUNLOCK(sc); 1148168793Sthompsa break; 1149168793Sthompsa case SIOCSIFFLAGS: 1150168793Sthompsa /* Set flags on ports too */ 1151171603Sthompsa LAGG_WLOCK(sc); 1152168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1153168793Sthompsa lagg_setflags(lp, 1); 1154168793Sthompsa } 1155171603Sthompsa LAGG_WUNLOCK(sc); 1156168793Sthompsa 1157168793Sthompsa if (!(ifp->if_flags & IFF_UP) && 1158168793Sthompsa (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1159168793Sthompsa /* 1160168793Sthompsa * If interface is marked down and it is running, 1161168793Sthompsa * then stop and disable it. 1162168793Sthompsa */ 1163171603Sthompsa LAGG_WLOCK(sc); 1164168793Sthompsa lagg_stop(sc); 1165171603Sthompsa LAGG_WUNLOCK(sc); 1166168793Sthompsa } else if ((ifp->if_flags & IFF_UP) && 1167168793Sthompsa !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1168168793Sthompsa /* 1169168793Sthompsa * If interface is marked up and it is stopped, then 1170168793Sthompsa * start it. 1171168793Sthompsa */ 1172168793Sthompsa (*ifp->if_init)(sc); 1173168793Sthompsa } 1174168793Sthompsa break; 1175168793Sthompsa case SIOCADDMULTI: 1176168793Sthompsa case SIOCDELMULTI: 1177171603Sthompsa LAGG_WLOCK(sc); 1178168793Sthompsa error = lagg_ether_setmulti(sc); 1179171603Sthompsa LAGG_WUNLOCK(sc); 1180168793Sthompsa break; 1181168793Sthompsa case SIOCSIFMEDIA: 1182168793Sthompsa case SIOCGIFMEDIA: 1183168793Sthompsa error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1184168793Sthompsa break; 1185171661Sthompsa 1186171661Sthompsa case SIOCSIFCAP: 1187171661Sthompsa case SIOCSIFMTU: 1188171661Sthompsa /* Do not allow the MTU or caps to be directly changed */ 1189171661Sthompsa error = EINVAL; 1190171661Sthompsa break; 1191171661Sthompsa 1192168793Sthompsa default: 1193168793Sthompsa error = ether_ioctl(ifp, cmd, data); 1194168793Sthompsa break; 1195168793Sthompsa } 1196168793Sthompsa return (error); 1197168793Sthompsa} 1198168793Sthompsa 1199168793Sthompsastatic int 1200168793Sthompsalagg_ether_setmulti(struct lagg_softc *sc) 1201168793Sthompsa{ 1202169327Sthompsa struct lagg_port *lp; 1203168793Sthompsa 1204169569Sthompsa LAGG_WLOCK_ASSERT(sc); 1205168793Sthompsa 1206169327Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1207169340Sthompsa /* First, remove any existing filter entries. */ 1208169340Sthompsa lagg_ether_cmdmulti(lp, 0); 1209169340Sthompsa /* copy all addresses from the lagg interface to the port */ 1210169327Sthompsa lagg_ether_cmdmulti(lp, 1); 1211169340Sthompsa } 1212168793Sthompsa return (0); 1213168793Sthompsa} 1214168793Sthompsa 1215168793Sthompsastatic int 1216168793Sthompsalagg_ether_cmdmulti(struct lagg_port *lp, int set) 1217168793Sthompsa{ 1218170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 1219169327Sthompsa struct ifnet *ifp = lp->lp_ifp; 1220170599Sthompsa struct ifnet *scifp = sc->sc_ifp; 1221169327Sthompsa struct lagg_mc *mc; 1222270136Smav struct ifmultiaddr *ifma; 1223169327Sthompsa int error; 1224168793Sthompsa 1225169569Sthompsa LAGG_WLOCK_ASSERT(sc); 1226168793Sthompsa 1227169327Sthompsa if (set) { 1228270136Smav IF_ADDR_WLOCK(scifp); 1229170599Sthompsa TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) { 1230169327Sthompsa if (ifma->ifma_addr->sa_family != AF_LINK) 1231169327Sthompsa continue; 1232169327Sthompsa mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT); 1233270136Smav if (mc == NULL) { 1234270136Smav IF_ADDR_WUNLOCK(scifp); 1235169327Sthompsa return (ENOMEM); 1236270136Smav } 1237270136Smav bcopy(ifma->ifma_addr, &mc->mc_addr, 1238270136Smav ifma->ifma_addr->sa_len); 1239270136Smav mc->mc_addr.sdl_index = ifp->if_index; 1240270136Smav mc->mc_ifma = NULL; 1241169327Sthompsa SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries); 1242168793Sthompsa } 1243270136Smav IF_ADDR_WUNLOCK(scifp); 1244270136Smav SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) { 1245270136Smav error = if_addmulti(ifp, 1246270136Smav (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma); 1247270136Smav if (error) 1248270136Smav return (error); 1249270136Smav } 1250169327Sthompsa } else { 1251169327Sthompsa while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) { 1252169327Sthompsa SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries); 1253270136Smav if (mc->mc_ifma && !lp->lp_detaching) 1254270136Smav if_delmulti_ifma(mc->mc_ifma); 1255169327Sthompsa free(mc, M_DEVBUF); 1256169327Sthompsa } 1257168793Sthompsa } 1258168793Sthompsa return (0); 1259168793Sthompsa} 1260168793Sthompsa 1261168793Sthompsa/* Handle a ref counted flag that should be set on the lagg port as well */ 1262168793Sthompsastatic int 1263168793Sthompsalagg_setflag(struct lagg_port *lp, int flag, int status, 1264168793Sthompsa int (*func)(struct ifnet *, int)) 1265168793Sthompsa{ 1266170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 1267170599Sthompsa struct ifnet *scifp = sc->sc_ifp; 1268168793Sthompsa struct ifnet *ifp = lp->lp_ifp; 1269168793Sthompsa int error; 1270168793Sthompsa 1271169569Sthompsa LAGG_WLOCK_ASSERT(sc); 1272168793Sthompsa 1273170599Sthompsa status = status ? (scifp->if_flags & flag) : 0; 1274168793Sthompsa /* Now "status" contains the flag value or 0 */ 1275168793Sthompsa 1276168793Sthompsa /* 1277168793Sthompsa * See if recorded ports status is different from what 1278168793Sthompsa * we want it to be. If it is, flip it. We record ports 1279168793Sthompsa * status in lp_ifflags so that we won't clear ports flag 1280168793Sthompsa * we haven't set. In fact, we don't clear or set ports 1281168793Sthompsa * flags directly, but get or release references to them. 1282168793Sthompsa * That's why we can be sure that recorded flags still are 1283168793Sthompsa * in accord with actual ports flags. 1284168793Sthompsa */ 1285168793Sthompsa if (status != (lp->lp_ifflags & flag)) { 1286168793Sthompsa error = (*func)(ifp, status); 1287168793Sthompsa if (error) 1288168793Sthompsa return (error); 1289168793Sthompsa lp->lp_ifflags &= ~flag; 1290168793Sthompsa lp->lp_ifflags |= status; 1291168793Sthompsa } 1292168793Sthompsa return (0); 1293168793Sthompsa} 1294168793Sthompsa 1295168793Sthompsa/* 1296168793Sthompsa * Handle IFF_* flags that require certain changes on the lagg port 1297168793Sthompsa * if "status" is true, update ports flags respective to the lagg 1298168793Sthompsa * if "status" is false, forcedly clear the flags set on port. 1299168793Sthompsa */ 1300168793Sthompsastatic int 1301168793Sthompsalagg_setflags(struct lagg_port *lp, int status) 1302168793Sthompsa{ 1303168793Sthompsa int error, i; 1304170599Sthompsa 1305168793Sthompsa for (i = 0; lagg_pflags[i].flag; i++) { 1306168793Sthompsa error = lagg_setflag(lp, lagg_pflags[i].flag, 1307168793Sthompsa status, lagg_pflags[i].func); 1308168793Sthompsa if (error) 1309168793Sthompsa return (error); 1310168793Sthompsa } 1311168793Sthompsa return (0); 1312168793Sthompsa} 1313168793Sthompsa 1314240742Sglebiusstatic int 1315240742Sglebiuslagg_transmit(struct ifnet *ifp, struct mbuf *m) 1316168793Sthompsa{ 1317168793Sthompsa struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1318240742Sglebius int error, len, mcast; 1319255038Sadrian struct rm_priotracker tracker; 1320168793Sthompsa 1321240742Sglebius len = m->m_pkthdr.len; 1322240742Sglebius mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0; 1323240742Sglebius 1324255038Sadrian LAGG_RLOCK(sc, &tracker); 1325183160Sthompsa /* We need a Tx algorithm and at least one port */ 1326183160Sthompsa if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) { 1327255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1328240742Sglebius m_freem(m); 1329240742Sglebius ifp->if_oerrors++; 1330240742Sglebius return (ENXIO); 1331183160Sthompsa } 1332183160Sthompsa 1333240742Sglebius ETHER_BPF_MTAP(ifp, m); 1334168793Sthompsa 1335240742Sglebius error = (*sc->sc_start)(sc, m); 1336255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1337168793Sthompsa 1338240742Sglebius if (error == 0) { 1339249506Sglebius counter_u64_add(sc->sc_opackets, 1); 1340249506Sglebius counter_u64_add(sc->sc_obytes, len); 1341240742Sglebius ifp->if_omcasts += mcast; 1342240742Sglebius } else 1343240742Sglebius ifp->if_oerrors++; 1344240742Sglebius 1345240742Sglebius return (error); 1346168793Sthompsa} 1347168793Sthompsa 1348240742Sglebius/* 1349240742Sglebius * The ifp->if_qflush entry point for lagg(4) is no-op. 1350240742Sglebius */ 1351240742Sglebiusstatic void 1352240742Sglebiuslagg_qflush(struct ifnet *ifp __unused) 1353240742Sglebius{ 1354240742Sglebius} 1355240742Sglebius 1356168793Sthompsastatic struct mbuf * 1357168793Sthompsalagg_input(struct ifnet *ifp, struct mbuf *m) 1358168793Sthompsa{ 1359168793Sthompsa struct lagg_port *lp = ifp->if_lagg; 1360170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 1361170599Sthompsa struct ifnet *scifp = sc->sc_ifp; 1362255038Sadrian struct rm_priotracker tracker; 1363168793Sthompsa 1364255038Sadrian LAGG_RLOCK(sc, &tracker); 1365170599Sthompsa if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 1366169227Sthompsa (lp->lp_flags & LAGG_PORT_DISABLED) || 1367168793Sthompsa sc->sc_proto == LAGG_PROTO_NONE) { 1368255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1369168793Sthompsa m_freem(m); 1370168793Sthompsa return (NULL); 1371168793Sthompsa } 1372168793Sthompsa 1373172825Sthompsa ETHER_BPF_MTAP(scifp, m); 1374168793Sthompsa 1375168793Sthompsa m = (*sc->sc_input)(sc, lp, m); 1376168793Sthompsa 1377168793Sthompsa if (m != NULL) { 1378249506Sglebius counter_u64_add(sc->sc_ipackets, 1); 1379249506Sglebius counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len); 1380174278Sthompsa 1381174278Sthompsa if (scifp->if_flags & IFF_MONITOR) { 1382174278Sthompsa m_freem(m); 1383174278Sthompsa m = NULL; 1384174278Sthompsa } 1385168793Sthompsa } 1386168793Sthompsa 1387255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1388168793Sthompsa return (m); 1389168793Sthompsa} 1390168793Sthompsa 1391168793Sthompsastatic int 1392168793Sthompsalagg_media_change(struct ifnet *ifp) 1393168793Sthompsa{ 1394168793Sthompsa struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1395168793Sthompsa 1396168793Sthompsa if (sc->sc_ifflags & IFF_DEBUG) 1397168793Sthompsa printf("%s\n", __func__); 1398168793Sthompsa 1399168793Sthompsa /* Ignore */ 1400168793Sthompsa return (0); 1401168793Sthompsa} 1402168793Sthompsa 1403168793Sthompsastatic void 1404168793Sthompsalagg_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1405168793Sthompsa{ 1406168793Sthompsa struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc; 1407168793Sthompsa struct lagg_port *lp; 1408255038Sadrian struct rm_priotracker tracker; 1409168793Sthompsa 1410168793Sthompsa imr->ifm_status = IFM_AVALID; 1411168793Sthompsa imr->ifm_active = IFM_ETHER | IFM_AUTO; 1412168793Sthompsa 1413255038Sadrian LAGG_RLOCK(sc, &tracker); 1414169340Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1415169340Sthompsa if (LAGG_PORTACTIVE(lp)) 1416169340Sthompsa imr->ifm_status |= IFM_ACTIVE; 1417169340Sthompsa } 1418255038Sadrian LAGG_RUNLOCK(sc, &tracker); 1419168793Sthompsa} 1420168793Sthompsa 1421168793Sthompsastatic void 1422173895Sthompsalagg_linkstate(struct lagg_softc *sc) 1423173895Sthompsa{ 1424173895Sthompsa struct lagg_port *lp; 1425173895Sthompsa int new_link = LINK_STATE_DOWN; 1426186255Sthompsa uint64_t speed; 1427173895Sthompsa 1428173895Sthompsa /* Our link is considered up if at least one of our ports is active */ 1429173895Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) { 1430173895Sthompsa if (lp->lp_link_state == LINK_STATE_UP) { 1431173895Sthompsa new_link = LINK_STATE_UP; 1432173895Sthompsa break; 1433173895Sthompsa } 1434173895Sthompsa } 1435173895Sthompsa if_link_state_change(sc->sc_ifp, new_link); 1436186254Sthompsa 1437186254Sthompsa /* Update if_baudrate to reflect the max possible speed */ 1438186254Sthompsa switch (sc->sc_proto) { 1439186254Sthompsa case LAGG_PROTO_FAILOVER: 1440186255Sthompsa sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ? 1441186255Sthompsa sc->sc_primary->lp_ifp->if_baudrate : 0; 1442186254Sthompsa break; 1443186254Sthompsa case LAGG_PROTO_ROUNDROBIN: 1444186254Sthompsa case LAGG_PROTO_LOADBALANCE: 1445186254Sthompsa case LAGG_PROTO_ETHERCHANNEL: 1446186255Sthompsa speed = 0; 1447186254Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1448186254Sthompsa speed += lp->lp_ifp->if_baudrate; 1449186254Sthompsa sc->sc_ifp->if_baudrate = speed; 1450186254Sthompsa break; 1451186254Sthompsa case LAGG_PROTO_LACP: 1452186254Sthompsa /* LACP updates if_baudrate itself */ 1453186254Sthompsa break; 1454186254Sthompsa } 1455173895Sthompsa} 1456173895Sthompsa 1457173895Sthompsastatic void 1458168793Sthompsalagg_port_state(struct ifnet *ifp, int state) 1459168793Sthompsa{ 1460168793Sthompsa struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg; 1461168793Sthompsa struct lagg_softc *sc = NULL; 1462168793Sthompsa 1463168793Sthompsa if (lp != NULL) 1464170599Sthompsa sc = lp->lp_softc; 1465168793Sthompsa if (sc == NULL) 1466168793Sthompsa return; 1467168793Sthompsa 1468169569Sthompsa LAGG_WLOCK(sc); 1469173895Sthompsa lagg_linkstate(sc); 1470168793Sthompsa if (sc->sc_linkstate != NULL) 1471168793Sthompsa (*sc->sc_linkstate)(lp); 1472169569Sthompsa LAGG_WUNLOCK(sc); 1473168793Sthompsa} 1474168793Sthompsa 1475168793Sthompsastruct lagg_port * 1476168793Sthompsalagg_link_active(struct lagg_softc *sc, struct lagg_port *lp) 1477168793Sthompsa{ 1478168793Sthompsa struct lagg_port *lp_next, *rval = NULL; 1479168793Sthompsa // int new_link = LINK_STATE_DOWN; 1480168793Sthompsa 1481169688Sthompsa LAGG_RLOCK_ASSERT(sc); 1482168793Sthompsa /* 1483168793Sthompsa * Search a port which reports an active link state. 1484168793Sthompsa */ 1485168793Sthompsa 1486168793Sthompsa if (lp == NULL) 1487168793Sthompsa goto search; 1488168793Sthompsa if (LAGG_PORTACTIVE(lp)) { 1489168793Sthompsa rval = lp; 1490168793Sthompsa goto found; 1491168793Sthompsa } 1492168793Sthompsa if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL && 1493168793Sthompsa LAGG_PORTACTIVE(lp_next)) { 1494168793Sthompsa rval = lp_next; 1495168793Sthompsa goto found; 1496168793Sthompsa } 1497168793Sthompsa 1498168793Sthompsasearch: 1499168793Sthompsa SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1500168793Sthompsa if (LAGG_PORTACTIVE(lp_next)) { 1501168793Sthompsa rval = lp_next; 1502168793Sthompsa goto found; 1503168793Sthompsa } 1504168793Sthompsa } 1505168793Sthompsa 1506168793Sthompsafound: 1507168793Sthompsa if (rval != NULL) { 1508168793Sthompsa /* 1509168793Sthompsa * The IEEE 802.1D standard assumes that a lagg with 1510168793Sthompsa * multiple ports is always full duplex. This is valid 1511168793Sthompsa * for load sharing laggs and if at least two links 1512168793Sthompsa * are active. Unfortunately, checking the latter would 1513168793Sthompsa * be too expensive at this point. 1514168793Sthompsa XXX 1515168793Sthompsa if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) && 1516168793Sthompsa (sc->sc_count > 1)) 1517168793Sthompsa new_link = LINK_STATE_FULL_DUPLEX; 1518168793Sthompsa else 1519168793Sthompsa new_link = rval->lp_link_state; 1520168793Sthompsa */ 1521168793Sthompsa } 1522168793Sthompsa 1523168793Sthompsa return (rval); 1524168793Sthompsa} 1525168793Sthompsa 1526168793Sthompsastatic const void * 1527168793Sthompsalagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf) 1528168793Sthompsa{ 1529168793Sthompsa if (m->m_pkthdr.len < (off + len)) { 1530168793Sthompsa return (NULL); 1531168793Sthompsa } else if (m->m_len < (off + len)) { 1532168793Sthompsa m_copydata(m, off, len, buf); 1533168793Sthompsa return (buf); 1534168793Sthompsa } 1535168793Sthompsa return (mtod(m, char *) + off); 1536168793Sthompsa} 1537168793Sthompsa 1538253314Sadrianstatic int 1539253314Sadrianlagg_sysctl_active(SYSCTL_HANDLER_ARGS) 1540253314Sadrian{ 1541253314Sadrian struct lagg_softc *sc = (struct lagg_softc *)arg1; 1542253314Sadrian struct lagg_port *lp; 1543253314Sadrian int error; 1544253314Sadrian 1545253314Sadrian /* LACP tracks active links automatically, the others do not */ 1546253314Sadrian if (sc->sc_proto != LAGG_PROTO_LACP) { 1547253314Sadrian sc->sc_active = 0; 1548253314Sadrian SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1549253314Sadrian sc->sc_active += LAGG_PORTACTIVE(lp); 1550253314Sadrian } 1551253314Sadrian 1552253314Sadrian error = sysctl_handle_int(oidp, &sc->sc_active, 0, req); 1553253314Sadrian if ((error) || (req->newptr == NULL)) 1554253314Sadrian return (error); 1555253314Sadrian 1556253314Sadrian return (0); 1557253314Sadrian} 1558253314Sadrian 1559168793Sthompsauint32_t 1560232629Sthompsalagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key) 1561168793Sthompsa{ 1562168793Sthompsa uint16_t etype; 1563232629Sthompsa uint32_t p = key; 1564168793Sthompsa int off; 1565168793Sthompsa struct ether_header *eh; 1566168793Sthompsa const struct ether_vlan_header *vlan; 1567168793Sthompsa#ifdef INET 1568168793Sthompsa const struct ip *ip; 1569232629Sthompsa const uint32_t *ports; 1570232629Sthompsa int iphlen; 1571168793Sthompsa#endif 1572168793Sthompsa#ifdef INET6 1573168793Sthompsa const struct ip6_hdr *ip6; 1574169583Sthompsa uint32_t flow; 1575168793Sthompsa#endif 1576232629Sthompsa union { 1577232629Sthompsa#ifdef INET 1578232629Sthompsa struct ip ip; 1579232629Sthompsa#endif 1580232629Sthompsa#ifdef INET6 1581232629Sthompsa struct ip6_hdr ip6; 1582232629Sthompsa#endif 1583232640Sthompsa struct ether_vlan_header vlan; 1584232629Sthompsa uint32_t port; 1585232629Sthompsa } buf; 1586168793Sthompsa 1587232629Sthompsa 1588168793Sthompsa off = sizeof(*eh); 1589168793Sthompsa if (m->m_len < off) 1590168793Sthompsa goto out; 1591168793Sthompsa eh = mtod(m, struct ether_header *); 1592168793Sthompsa etype = ntohs(eh->ether_type); 1593232629Sthompsa if (sc->sc_flags & LAGG_F_HASHL2) { 1594232629Sthompsa p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p); 1595232629Sthompsa p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p); 1596232629Sthompsa } 1597168793Sthompsa 1598168793Sthompsa /* Special handling for encapsulating VLAN frames */ 1599232629Sthompsa if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) { 1600168793Sthompsa p = hash32_buf(&m->m_pkthdr.ether_vtag, 1601168793Sthompsa sizeof(m->m_pkthdr.ether_vtag), p); 1602168793Sthompsa } else if (etype == ETHERTYPE_VLAN) { 1603232640Sthompsa vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf); 1604170599Sthompsa if (vlan == NULL) 1605168793Sthompsa goto out; 1606168793Sthompsa 1607232629Sthompsa if (sc->sc_flags & LAGG_F_HASHL2) 1608232629Sthompsa p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p); 1609168793Sthompsa etype = ntohs(vlan->evl_proto); 1610168793Sthompsa off += sizeof(*vlan) - sizeof(*eh); 1611168793Sthompsa } 1612168793Sthompsa 1613168793Sthompsa switch (etype) { 1614168793Sthompsa#ifdef INET 1615168793Sthompsa case ETHERTYPE_IP: 1616232629Sthompsa ip = lagg_gethdr(m, off, sizeof(*ip), &buf); 1617168793Sthompsa if (ip == NULL) 1618168793Sthompsa goto out; 1619168793Sthompsa 1620232629Sthompsa if (sc->sc_flags & LAGG_F_HASHL3) { 1621232629Sthompsa p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p); 1622232629Sthompsa p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p); 1623232629Sthompsa } 1624232629Sthompsa if (!(sc->sc_flags & LAGG_F_HASHL4)) 1625232629Sthompsa break; 1626232629Sthompsa switch (ip->ip_p) { 1627232629Sthompsa case IPPROTO_TCP: 1628232629Sthompsa case IPPROTO_UDP: 1629232629Sthompsa case IPPROTO_SCTP: 1630232629Sthompsa iphlen = ip->ip_hl << 2; 1631232629Sthompsa if (iphlen < sizeof(*ip)) 1632232629Sthompsa break; 1633232629Sthompsa off += iphlen; 1634232629Sthompsa ports = lagg_gethdr(m, off, sizeof(*ports), &buf); 1635232629Sthompsa if (ports == NULL) 1636232629Sthompsa break; 1637232629Sthompsa p = hash32_buf(ports, sizeof(*ports), p); 1638232629Sthompsa break; 1639232629Sthompsa } 1640168793Sthompsa break; 1641168793Sthompsa#endif 1642168793Sthompsa#ifdef INET6 1643168793Sthompsa case ETHERTYPE_IPV6: 1644232629Sthompsa if (!(sc->sc_flags & LAGG_F_HASHL3)) 1645232629Sthompsa break; 1646232629Sthompsa ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf); 1647168793Sthompsa if (ip6 == NULL) 1648168793Sthompsa goto out; 1649168793Sthompsa 1650168793Sthompsa p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p); 1651168793Sthompsa p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p); 1652169570Sthompsa flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK; 1653169570Sthompsa p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */ 1654168793Sthompsa break; 1655168793Sthompsa#endif 1656168793Sthompsa } 1657168793Sthompsaout: 1658168793Sthompsa return (p); 1659168793Sthompsa} 1660168793Sthompsa 1661168793Sthompsaint 1662168793Sthompsalagg_enqueue(struct ifnet *ifp, struct mbuf *m) 1663168793Sthompsa{ 1664168793Sthompsa 1665185164Skmacy return (ifp->if_transmit)(ifp, m); 1666168793Sthompsa} 1667168793Sthompsa 1668168793Sthompsa/* 1669168793Sthompsa * Simple round robin aggregation 1670168793Sthompsa */ 1671168793Sthompsa 1672168793Sthompsastatic int 1673168793Sthompsalagg_rr_attach(struct lagg_softc *sc) 1674168793Sthompsa{ 1675168793Sthompsa sc->sc_detach = lagg_rr_detach; 1676168793Sthompsa sc->sc_start = lagg_rr_start; 1677168793Sthompsa sc->sc_input = lagg_rr_input; 1678168793Sthompsa sc->sc_port_create = NULL; 1679168793Sthompsa sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1680172554Sthompsa sc->sc_seq = 0; 1681168793Sthompsa 1682168793Sthompsa return (0); 1683168793Sthompsa} 1684168793Sthompsa 1685168793Sthompsastatic int 1686168793Sthompsalagg_rr_detach(struct lagg_softc *sc) 1687168793Sthompsa{ 1688168793Sthompsa return (0); 1689168793Sthompsa} 1690168793Sthompsa 1691168793Sthompsastatic int 1692168793Sthompsalagg_rr_start(struct lagg_softc *sc, struct mbuf *m) 1693168793Sthompsa{ 1694172554Sthompsa struct lagg_port *lp; 1695172554Sthompsa uint32_t p; 1696168793Sthompsa 1697172554Sthompsa p = atomic_fetchadd_32(&sc->sc_seq, 1); 1698172554Sthompsa p %= sc->sc_count; 1699172554Sthompsa lp = SLIST_FIRST(&sc->sc_ports); 1700172554Sthompsa while (p--) 1701172554Sthompsa lp = SLIST_NEXT(lp, lp_entries); 1702172554Sthompsa 1703172554Sthompsa /* 1704172554Sthompsa * Check the port's link state. This will return the next active 1705172554Sthompsa * port if the link is down or the port is NULL. 1706172554Sthompsa */ 1707172554Sthompsa if ((lp = lagg_link_active(sc, lp)) == NULL) { 1708172554Sthompsa m_freem(m); 1709251859Sdelphij return (ENETDOWN); 1710172554Sthompsa } 1711168793Sthompsa 1712168793Sthompsa /* Send mbuf */ 1713172554Sthompsa return (lagg_enqueue(lp->lp_ifp, m)); 1714168793Sthompsa} 1715168793Sthompsa 1716168793Sthompsastatic struct mbuf * 1717168793Sthompsalagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1718168793Sthompsa{ 1719168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 1720168793Sthompsa 1721168793Sthompsa /* Just pass in the packet to our lagg device */ 1722168793Sthompsa m->m_pkthdr.rcvif = ifp; 1723168793Sthompsa 1724168793Sthompsa return (m); 1725168793Sthompsa} 1726168793Sthompsa 1727168793Sthompsa/* 1728168793Sthompsa * Active failover 1729168793Sthompsa */ 1730168793Sthompsa 1731168793Sthompsastatic int 1732168793Sthompsalagg_fail_attach(struct lagg_softc *sc) 1733168793Sthompsa{ 1734168793Sthompsa sc->sc_detach = lagg_fail_detach; 1735168793Sthompsa sc->sc_start = lagg_fail_start; 1736168793Sthompsa sc->sc_input = lagg_fail_input; 1737168793Sthompsa sc->sc_port_create = NULL; 1738168793Sthompsa sc->sc_port_destroy = NULL; 1739168793Sthompsa 1740168793Sthompsa return (0); 1741168793Sthompsa} 1742168793Sthompsa 1743168793Sthompsastatic int 1744168793Sthompsalagg_fail_detach(struct lagg_softc *sc) 1745168793Sthompsa{ 1746168793Sthompsa return (0); 1747168793Sthompsa} 1748168793Sthompsa 1749168793Sthompsastatic int 1750168793Sthompsalagg_fail_start(struct lagg_softc *sc, struct mbuf *m) 1751168793Sthompsa{ 1752168793Sthompsa struct lagg_port *lp; 1753168793Sthompsa 1754168793Sthompsa /* Use the master port if active or the next available port */ 1755172554Sthompsa if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) { 1756172554Sthompsa m_freem(m); 1757251859Sdelphij return (ENETDOWN); 1758172554Sthompsa } 1759168793Sthompsa 1760168793Sthompsa /* Send mbuf */ 1761168793Sthompsa return (lagg_enqueue(lp->lp_ifp, m)); 1762168793Sthompsa} 1763168793Sthompsa 1764168793Sthompsastatic struct mbuf * 1765168793Sthompsalagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1766168793Sthompsa{ 1767168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 1768168793Sthompsa struct lagg_port *tmp_tp; 1769168793Sthompsa 1770212100Semaste if (lp == sc->sc_primary || lagg_failover_rx_all) { 1771168793Sthompsa m->m_pkthdr.rcvif = ifp; 1772168793Sthompsa return (m); 1773168793Sthompsa } 1774168793Sthompsa 1775174742Sthompsa if (!LAGG_PORTACTIVE(sc->sc_primary)) { 1776174742Sthompsa tmp_tp = lagg_link_active(sc, sc->sc_primary); 1777168793Sthompsa /* 1778168793Sthompsa * If tmp_tp is null, we've recieved a packet when all 1779168793Sthompsa * our links are down. Weird, but process it anyways. 1780168793Sthompsa */ 1781168793Sthompsa if ((tmp_tp == NULL || tmp_tp == lp)) { 1782168793Sthompsa m->m_pkthdr.rcvif = ifp; 1783168793Sthompsa return (m); 1784168793Sthompsa } 1785168793Sthompsa } 1786168793Sthompsa 1787168793Sthompsa m_freem(m); 1788168793Sthompsa return (NULL); 1789168793Sthompsa} 1790168793Sthompsa 1791168793Sthompsa/* 1792168793Sthompsa * Loadbalancing 1793168793Sthompsa */ 1794168793Sthompsa 1795168793Sthompsastatic int 1796168793Sthompsalagg_lb_attach(struct lagg_softc *sc) 1797168793Sthompsa{ 1798168793Sthompsa struct lagg_port *lp; 1799168793Sthompsa struct lagg_lb *lb; 1800168793Sthompsa 1801168793Sthompsa if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb), 1802168793Sthompsa M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) 1803168793Sthompsa return (ENOMEM); 1804168793Sthompsa 1805168793Sthompsa sc->sc_detach = lagg_lb_detach; 1806168793Sthompsa sc->sc_start = lagg_lb_start; 1807168793Sthompsa sc->sc_input = lagg_lb_input; 1808168793Sthompsa sc->sc_port_create = lagg_lb_port_create; 1809168793Sthompsa sc->sc_port_destroy = lagg_lb_port_destroy; 1810168793Sthompsa sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX; 1811168793Sthompsa 1812168793Sthompsa lb->lb_key = arc4random(); 1813168793Sthompsa sc->sc_psc = (caddr_t)lb; 1814168793Sthompsa 1815168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1816168793Sthompsa lagg_lb_port_create(lp); 1817168793Sthompsa 1818168793Sthompsa return (0); 1819168793Sthompsa} 1820168793Sthompsa 1821168793Sthompsastatic int 1822168793Sthompsalagg_lb_detach(struct lagg_softc *sc) 1823168793Sthompsa{ 1824168793Sthompsa struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1825168793Sthompsa if (lb != NULL) 1826168793Sthompsa free(lb, M_DEVBUF); 1827168793Sthompsa return (0); 1828168793Sthompsa} 1829168793Sthompsa 1830168793Sthompsastatic int 1831168793Sthompsalagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp) 1832168793Sthompsa{ 1833168793Sthompsa struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1834168793Sthompsa struct lagg_port *lp_next; 1835168793Sthompsa int i = 0; 1836168793Sthompsa 1837168793Sthompsa bzero(&lb->lb_ports, sizeof(lb->lb_ports)); 1838168793Sthompsa SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) { 1839168793Sthompsa if (lp_next == lp) 1840168793Sthompsa continue; 1841168793Sthompsa if (i >= LAGG_MAX_PORTS) 1842168793Sthompsa return (EINVAL); 1843168793Sthompsa if (sc->sc_ifflags & IFF_DEBUG) 1844168793Sthompsa printf("%s: port %s at index %d\n", 1845168793Sthompsa sc->sc_ifname, lp_next->lp_ifname, i); 1846168793Sthompsa lb->lb_ports[i++] = lp_next; 1847168793Sthompsa } 1848168793Sthompsa 1849168793Sthompsa return (0); 1850168793Sthompsa} 1851168793Sthompsa 1852168793Sthompsastatic int 1853168793Sthompsalagg_lb_port_create(struct lagg_port *lp) 1854168793Sthompsa{ 1855170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 1856168793Sthompsa return (lagg_lb_porttable(sc, NULL)); 1857168793Sthompsa} 1858168793Sthompsa 1859168793Sthompsastatic void 1860168793Sthompsalagg_lb_port_destroy(struct lagg_port *lp) 1861168793Sthompsa{ 1862170599Sthompsa struct lagg_softc *sc = lp->lp_softc; 1863168793Sthompsa lagg_lb_porttable(sc, lp); 1864168793Sthompsa} 1865168793Sthompsa 1866168793Sthompsastatic int 1867168793Sthompsalagg_lb_start(struct lagg_softc *sc, struct mbuf *m) 1868168793Sthompsa{ 1869168793Sthompsa struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc; 1870168793Sthompsa struct lagg_port *lp = NULL; 1871168793Sthompsa uint32_t p = 0; 1872168793Sthompsa 1873232008Sthompsa if (sc->use_flowid && (m->m_flags & M_FLOWID)) 1874260179Sscottl p = m->m_pkthdr.flowid >> sc->flowid_shift; 1875191692Sthompsa else 1876232629Sthompsa p = lagg_hashmbuf(sc, m, lb->lb_key); 1877180249Sthompsa p %= sc->sc_count; 1878180249Sthompsa lp = lb->lb_ports[p]; 1879168793Sthompsa 1880168793Sthompsa /* 1881168793Sthompsa * Check the port's link state. This will return the next active 1882168793Sthompsa * port if the link is down or the port is NULL. 1883168793Sthompsa */ 1884172554Sthompsa if ((lp = lagg_link_active(sc, lp)) == NULL) { 1885172554Sthompsa m_freem(m); 1886251859Sdelphij return (ENETDOWN); 1887172554Sthompsa } 1888168793Sthompsa 1889168793Sthompsa /* Send mbuf */ 1890168793Sthompsa return (lagg_enqueue(lp->lp_ifp, m)); 1891168793Sthompsa} 1892168793Sthompsa 1893168793Sthompsastatic struct mbuf * 1894168793Sthompsalagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1895168793Sthompsa{ 1896168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 1897168793Sthompsa 1898168793Sthompsa /* Just pass in the packet to our lagg device */ 1899168793Sthompsa m->m_pkthdr.rcvif = ifp; 1900168793Sthompsa 1901168793Sthompsa return (m); 1902168793Sthompsa} 1903168793Sthompsa 1904168793Sthompsa/* 1905168793Sthompsa * 802.3ad LACP 1906168793Sthompsa */ 1907168793Sthompsa 1908168793Sthompsastatic int 1909168793Sthompsalagg_lacp_attach(struct lagg_softc *sc) 1910168793Sthompsa{ 1911168793Sthompsa struct lagg_port *lp; 1912168793Sthompsa int error; 1913168793Sthompsa 1914168793Sthompsa sc->sc_detach = lagg_lacp_detach; 1915168793Sthompsa sc->sc_port_create = lacp_port_create; 1916168793Sthompsa sc->sc_port_destroy = lacp_port_destroy; 1917168793Sthompsa sc->sc_linkstate = lacp_linkstate; 1918168793Sthompsa sc->sc_start = lagg_lacp_start; 1919168793Sthompsa sc->sc_input = lagg_lacp_input; 1920168793Sthompsa sc->sc_init = lacp_init; 1921168793Sthompsa sc->sc_stop = lacp_stop; 1922168793Sthompsa sc->sc_lladdr = lagg_lacp_lladdr; 1923171247Sthompsa sc->sc_req = lacp_req; 1924171247Sthompsa sc->sc_portreq = lacp_portreq; 1925168793Sthompsa 1926168793Sthompsa error = lacp_attach(sc); 1927168793Sthompsa if (error) 1928168793Sthompsa return (error); 1929168793Sthompsa 1930168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1931168793Sthompsa lacp_port_create(lp); 1932168793Sthompsa 1933168793Sthompsa return (error); 1934168793Sthompsa} 1935168793Sthompsa 1936168793Sthompsastatic int 1937168793Sthompsalagg_lacp_detach(struct lagg_softc *sc) 1938168793Sthompsa{ 1939168793Sthompsa struct lagg_port *lp; 1940168793Sthompsa int error; 1941168793Sthompsa 1942168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1943168793Sthompsa lacp_port_destroy(lp); 1944168793Sthompsa 1945168793Sthompsa /* unlocking is safe here */ 1946169569Sthompsa LAGG_WUNLOCK(sc); 1947168793Sthompsa error = lacp_detach(sc); 1948169569Sthompsa LAGG_WLOCK(sc); 1949168793Sthompsa 1950168793Sthompsa return (error); 1951168793Sthompsa} 1952168793Sthompsa 1953168793Sthompsastatic void 1954168793Sthompsalagg_lacp_lladdr(struct lagg_softc *sc) 1955168793Sthompsa{ 1956168793Sthompsa struct lagg_port *lp; 1957168793Sthompsa 1958168793Sthompsa /* purge all the lacp ports */ 1959168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1960168793Sthompsa lacp_port_destroy(lp); 1961168793Sthompsa 1962168793Sthompsa /* add them back in */ 1963168793Sthompsa SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) 1964168793Sthompsa lacp_port_create(lp); 1965168793Sthompsa} 1966168793Sthompsa 1967168793Sthompsastatic int 1968168793Sthompsalagg_lacp_start(struct lagg_softc *sc, struct mbuf *m) 1969168793Sthompsa{ 1970168793Sthompsa struct lagg_port *lp; 1971168793Sthompsa 1972168793Sthompsa lp = lacp_select_tx_port(sc, m); 1973172554Sthompsa if (lp == NULL) { 1974172554Sthompsa m_freem(m); 1975245741Sglebius return (ENETDOWN); 1976172554Sthompsa } 1977168793Sthompsa 1978168793Sthompsa /* Send mbuf */ 1979168793Sthompsa return (lagg_enqueue(lp->lp_ifp, m)); 1980168793Sthompsa} 1981168793Sthompsa 1982168793Sthompsastatic struct mbuf * 1983168793Sthompsalagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m) 1984168793Sthompsa{ 1985168793Sthompsa struct ifnet *ifp = sc->sc_ifp; 1986168793Sthompsa struct ether_header *eh; 1987168793Sthompsa u_short etype; 1988168793Sthompsa 1989168793Sthompsa eh = mtod(m, struct ether_header *); 1990168793Sthompsa etype = ntohs(eh->ether_type); 1991168793Sthompsa 1992168793Sthompsa /* Tap off LACP control messages */ 1993221270Sthompsa if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) { 1994175005Sthompsa m = lacp_input(lp, m); 1995175005Sthompsa if (m == NULL) 1996175005Sthompsa return (NULL); 1997168793Sthompsa } 1998168793Sthompsa 1999168793Sthompsa /* 2000168793Sthompsa * If the port is not collecting or not in the active aggregator then 2001168793Sthompsa * free and return. 2002168793Sthompsa */ 2003177274Sthompsa if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) { 2004168793Sthompsa m_freem(m); 2005168793Sthompsa return (NULL); 2006168793Sthompsa } 2007168793Sthompsa 2008168793Sthompsa m->m_pkthdr.rcvif = ifp; 2009168793Sthompsa return (m); 2010168793Sthompsa} 2011249506Sglebius 2012249506Sglebiusstatic void 2013249506Sglebiuslagg_callout(void *arg) 2014249506Sglebius{ 2015249506Sglebius struct lagg_softc *sc = (struct lagg_softc *)arg; 2016249506Sglebius struct ifnet *ifp = sc->sc_ifp; 2017249506Sglebius 2018249506Sglebius ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets); 2019249506Sglebius ifp->if_opackets = counter_u64_fetch(sc->sc_opackets); 2020249506Sglebius ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes); 2021249506Sglebius ifp->if_obytes = counter_u64_fetch(sc->sc_obytes); 2022249506Sglebius 2023249506Sglebius callout_reset(&sc->sc_callout, hz, lagg_callout, sc); 2024249506Sglebius} 2025