1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */
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>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 #include "opt_inet6.h"
26 #include "opt_kern_tls.h"
27 #include "opt_ratelimit.h"
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.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>
39 #include <sys/systm.h>
42 #include <sys/rmlock.h>
44 #include <sys/taskqueue.h>
45 #include <sys/eventhandler.h>
47 #include <net/ethernet.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>
56 #include <net/route.h>
58 #include <net/infiniband.h>
60 #if defined(INET) || defined(INET6)
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/if_ether.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet6/in6_ifattach.h>
75 #include <net/if_vlan_var.h>
76 #include <net/if_lagg.h>
77 #include <net/ieee8023ad_lacp.h>
81 * XXX: declare here to avoid to include many inet6 related files..
82 * should be more generalized?
84 extern void nd6_setmtu(struct ifnet *);
87 #define LAGG_SX_INIT(_sc) sx_init(&(_sc)->sc_sx, "if_lagg sx")
88 #define LAGG_SX_DESTROY(_sc) sx_destroy(&(_sc)->sc_sx)
89 #define LAGG_XLOCK(_sc) sx_xlock(&(_sc)->sc_sx)
90 #define LAGG_XUNLOCK(_sc) sx_xunlock(&(_sc)->sc_sx)
91 #define LAGG_SXLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_LOCKED)
92 #define LAGG_XLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
94 /* Special flags we should propagate to the lagg ports. */
97 int (*func)(struct ifnet *, int);
99 {IFF_PROMISC, ifpromisc},
100 {IFF_ALLMULTI, if_allmulti},
104 struct lagg_snd_tag {
105 struct m_snd_tag com;
106 struct m_snd_tag *tag;
109 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
110 #define V_lagg_list VNET(lagg_list)
111 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
112 #define V_lagg_list_mtx VNET(lagg_list_mtx)
113 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
114 "if_lagg list", NULL, MTX_DEF)
115 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
116 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
117 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
118 eventhandler_tag lagg_detach_cookie = NULL;
120 static int lagg_clone_create(struct if_clone *, int, caddr_t);
121 static void lagg_clone_destroy(struct ifnet *);
122 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
123 #define V_lagg_cloner VNET(lagg_cloner)
124 static const char laggname[] = "lagg";
125 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
127 static void lagg_capabilities(struct lagg_softc *);
128 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
129 static int lagg_port_destroy(struct lagg_port *, int);
130 static struct mbuf *lagg_input_ethernet(struct ifnet *, struct mbuf *);
131 static struct mbuf *lagg_input_infiniband(struct ifnet *, struct mbuf *);
132 static void lagg_linkstate(struct lagg_softc *);
133 static void lagg_port_state(struct ifnet *, int);
134 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
135 static int lagg_port_output(struct ifnet *, struct mbuf *,
136 const struct sockaddr *, struct route *);
137 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
138 #ifdef LAGG_PORT_STACKING
139 static int lagg_port_checkstacking(struct lagg_softc *);
141 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
142 static void lagg_init(void *);
143 static void lagg_stop(struct lagg_softc *);
144 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
145 #if defined(KERN_TLS) || defined(RATELIMIT)
146 static int lagg_snd_tag_alloc(struct ifnet *,
147 union if_snd_tag_alloc_params *,
148 struct m_snd_tag **);
149 static int lagg_snd_tag_modify(struct m_snd_tag *,
150 union if_snd_tag_modify_params *);
151 static int lagg_snd_tag_query(struct m_snd_tag *,
152 union if_snd_tag_query_params *);
153 static void lagg_snd_tag_free(struct m_snd_tag *);
154 static void lagg_ratelimit_query(struct ifnet *,
155 struct if_ratelimit_query_results *);
157 static int lagg_setmulti(struct lagg_port *);
158 static int lagg_clrmulti(struct lagg_port *);
159 static int lagg_setcaps(struct lagg_port *, int cap);
160 static int lagg_setflag(struct lagg_port *, int, int,
161 int (*func)(struct ifnet *, int));
162 static int lagg_setflags(struct lagg_port *, int status);
163 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
164 static int lagg_transmit_ethernet(struct ifnet *, struct mbuf *);
165 static int lagg_transmit_infiniband(struct ifnet *, struct mbuf *);
166 static void lagg_qflush(struct ifnet *);
167 static int lagg_media_change(struct ifnet *);
168 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
169 static struct lagg_port *lagg_link_active(struct lagg_softc *,
172 /* Simple round robin */
173 static void lagg_rr_attach(struct lagg_softc *);
174 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
175 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
178 /* Active failover */
179 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
180 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
184 static void lagg_lb_attach(struct lagg_softc *);
185 static void lagg_lb_detach(struct lagg_softc *);
186 static int lagg_lb_port_create(struct lagg_port *);
187 static void lagg_lb_port_destroy(struct lagg_port *);
188 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
189 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
191 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
194 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
195 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
199 static void lagg_lacp_attach(struct lagg_softc *);
200 static void lagg_lacp_detach(struct lagg_softc *);
201 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
202 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
204 static void lagg_lacp_lladdr(struct lagg_softc *);
206 /* lagg protocol table */
207 static const struct lagg_proto {
209 void (*pr_attach)(struct lagg_softc *);
210 void (*pr_detach)(struct lagg_softc *);
211 int (*pr_start)(struct lagg_softc *, struct mbuf *);
212 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
214 int (*pr_addport)(struct lagg_port *);
215 void (*pr_delport)(struct lagg_port *);
216 void (*pr_linkstate)(struct lagg_port *);
217 void (*pr_init)(struct lagg_softc *);
218 void (*pr_stop)(struct lagg_softc *);
219 void (*pr_lladdr)(struct lagg_softc *);
220 void (*pr_request)(struct lagg_softc *, void *);
221 void (*pr_portreq)(struct lagg_port *, void *);
224 .pr_num = LAGG_PROTO_NONE
227 .pr_num = LAGG_PROTO_ROUNDROBIN,
228 .pr_attach = lagg_rr_attach,
229 .pr_start = lagg_rr_start,
230 .pr_input = lagg_rr_input,
233 .pr_num = LAGG_PROTO_FAILOVER,
234 .pr_start = lagg_fail_start,
235 .pr_input = lagg_fail_input,
238 .pr_num = LAGG_PROTO_LOADBALANCE,
239 .pr_attach = lagg_lb_attach,
240 .pr_detach = lagg_lb_detach,
241 .pr_start = lagg_lb_start,
242 .pr_input = lagg_lb_input,
243 .pr_addport = lagg_lb_port_create,
244 .pr_delport = lagg_lb_port_destroy,
247 .pr_num = LAGG_PROTO_LACP,
248 .pr_attach = lagg_lacp_attach,
249 .pr_detach = lagg_lacp_detach,
250 .pr_start = lagg_lacp_start,
251 .pr_input = lagg_lacp_input,
252 .pr_addport = lacp_port_create,
253 .pr_delport = lacp_port_destroy,
254 .pr_linkstate = lacp_linkstate,
255 .pr_init = lacp_init,
256 .pr_stop = lacp_stop,
257 .pr_lladdr = lagg_lacp_lladdr,
258 .pr_request = lacp_req,
259 .pr_portreq = lacp_portreq,
262 .pr_num = LAGG_PROTO_BROADCAST,
263 .pr_start = lagg_bcast_start,
264 .pr_input = lagg_bcast_input,
268 SYSCTL_DECL(_net_link);
269 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
272 /* Allow input on any failover links */
273 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
274 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
275 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
276 &VNET_NAME(lagg_failover_rx_all), 0,
277 "Accept input from any interface in a failover lagg");
279 /* Default value for using flowid */
280 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
281 #define V_def_use_flowid VNET(def_use_flowid)
282 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
283 &VNET_NAME(def_use_flowid), 0,
284 "Default setting for using flow id for load sharing");
286 /* Default value for using numa */
287 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
288 #define V_def_use_numa VNET(def_use_numa)
289 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa, CTLFLAG_RWTUN,
290 &VNET_NAME(def_use_numa), 0,
291 "Use numa to steer flows");
293 /* Default value for flowid shift */
294 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
295 #define V_def_flowid_shift VNET(def_flowid_shift)
296 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
297 &VNET_NAME(def_flowid_shift), 0,
298 "Default setting for flowid shift for load sharing");
301 vnet_lagg_init(const void *unused __unused)
304 LAGG_LIST_LOCK_INIT();
305 SLIST_INIT(&V_lagg_list);
306 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
307 lagg_clone_destroy, 0);
309 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
310 vnet_lagg_init, NULL);
313 vnet_lagg_uninit(const void *unused __unused)
316 if_clone_detach(V_lagg_cloner);
317 LAGG_LIST_LOCK_DESTROY();
319 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
320 vnet_lagg_uninit, NULL);
323 lagg_modevent(module_t mod, int type, void *data)
328 lagg_input_ethernet_p = lagg_input_ethernet;
329 lagg_input_infiniband_p = lagg_input_infiniband;
330 lagg_linkstate_p = lagg_port_state;
331 lagg_detach_cookie = EVENTHANDLER_REGISTER(
332 ifnet_departure_event, lagg_port_ifdetach, NULL,
333 EVENTHANDLER_PRI_ANY);
336 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
338 lagg_input_ethernet_p = NULL;
339 lagg_input_infiniband_p = NULL;
340 lagg_linkstate_p = NULL;
348 static moduledata_t lagg_mod = {
354 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
355 MODULE_VERSION(if_lagg, 1);
356 MODULE_DEPEND(if_lagg, if_infiniband, 1, 1, 1);
359 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
362 LAGG_XLOCK_ASSERT(sc);
363 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
366 if (sc->sc_ifflags & IFF_DEBUG)
367 if_printf(sc->sc_ifp, "using proto %u\n", pr);
369 if (lagg_protos[pr].pr_attach != NULL)
370 lagg_protos[pr].pr_attach(sc);
375 lagg_proto_detach(struct lagg_softc *sc)
379 LAGG_XLOCK_ASSERT(sc);
381 sc->sc_proto = LAGG_PROTO_NONE;
383 if (lagg_protos[pr].pr_detach != NULL)
384 lagg_protos[pr].pr_detach(sc);
388 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
391 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
395 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
398 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
402 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
405 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
408 return (lagg_protos[sc->sc_proto].pr_addport(lp));
412 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
415 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
416 lagg_protos[sc->sc_proto].pr_delport(lp);
420 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
423 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
424 lagg_protos[sc->sc_proto].pr_linkstate(lp);
428 lagg_proto_init(struct lagg_softc *sc)
431 if (lagg_protos[sc->sc_proto].pr_init != NULL)
432 lagg_protos[sc->sc_proto].pr_init(sc);
436 lagg_proto_stop(struct lagg_softc *sc)
439 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
440 lagg_protos[sc->sc_proto].pr_stop(sc);
444 lagg_proto_lladdr(struct lagg_softc *sc)
447 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
448 lagg_protos[sc->sc_proto].pr_lladdr(sc);
452 lagg_proto_request(struct lagg_softc *sc, void *v)
455 if (lagg_protos[sc->sc_proto].pr_request != NULL)
456 lagg_protos[sc->sc_proto].pr_request(sc, v);
460 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
463 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
464 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
468 * This routine is run via an vlan
472 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
474 struct lagg_softc *sc = ifp->if_softc;
475 struct lagg_port *lp;
477 if (ifp->if_softc != arg) /* Not our event */
481 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
482 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
487 * This routine is run via an vlan
491 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
493 struct lagg_softc *sc = ifp->if_softc;
494 struct lagg_port *lp;
496 if (ifp->if_softc != arg) /* Not our event */
500 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
501 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
506 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
508 struct iflaggparam iflp;
509 struct lagg_softc *sc;
513 static const uint8_t eaddr[LAGG_ADDR_LEN];
515 if (params != NULL) {
516 error = copyin(params, &iflp, sizeof(iflp));
520 switch (iflp.lagg_type) {
521 case LAGG_TYPE_ETHERNET:
524 case LAGG_TYPE_INFINIBAND:
525 if_type = IFT_INFINIBAND;
534 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
535 ifp = sc->sc_ifp = if_alloc(if_type);
542 mtx_init(&sc->sc_mtx, "lagg-mtx", NULL, MTX_DEF);
543 callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
546 if (V_def_use_flowid)
547 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
549 sc->sc_opts |= LAGG_OPT_USE_NUMA;
550 sc->flowid_shift = V_def_flowid_shift;
552 /* Hash all layers by default */
553 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
555 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
557 CK_SLIST_INIT(&sc->sc_ports);
561 /* Initialise pseudo media types */
562 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
564 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
565 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
567 if_initname(ifp, laggname, unit);
568 ifp->if_transmit = lagg_transmit_ethernet;
571 if_initname(ifp, laggname, unit);
572 ifp->if_transmit = lagg_transmit_infiniband;
578 ifp->if_qflush = lagg_qflush;
579 ifp->if_init = lagg_init;
580 ifp->if_ioctl = lagg_ioctl;
581 ifp->if_get_counter = lagg_get_counter;
582 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
583 #if defined(KERN_TLS) || defined(RATELIMIT)
584 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
585 ifp->if_snd_tag_modify = lagg_snd_tag_modify;
586 ifp->if_snd_tag_query = lagg_snd_tag_query;
587 ifp->if_snd_tag_free = lagg_snd_tag_free;
588 ifp->if_ratelimit_query = lagg_ratelimit_query;
590 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
593 * Attach as an ordinary ethernet device, children will be attached
594 * as special device IFT_IEEE8023ADLAG or IFT_INFINIBANDLAG.
598 ether_ifattach(ifp, eaddr);
601 infiniband_ifattach(ifp, eaddr, sc->sc_bcast_addr);
607 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
608 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
609 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
610 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
612 /* Insert into the global list of laggs */
614 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
622 lagg_clone_destroy(struct ifnet *ifp)
624 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
625 struct lagg_port *lp;
628 sc->sc_destroying = 1;
630 ifp->if_flags &= ~IFF_UP;
632 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
633 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
635 /* Shutdown and remove lagg ports */
636 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
637 lagg_port_destroy(lp, 1);
639 /* Unhook the aggregation protocol */
640 lagg_proto_detach(sc);
643 switch (ifp->if_type) {
645 ifmedia_removeall(&sc->sc_media);
649 infiniband_ifdetach(ifp);
657 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
660 mtx_destroy(&sc->sc_mtx);
666 lagg_capabilities(struct lagg_softc *sc)
668 struct lagg_port *lp;
671 struct ifnet_hw_tsomax hw_tsomax;
673 LAGG_XLOCK_ASSERT(sc);
675 /* Get common enabled capabilities for the lagg ports */
677 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
678 ena &= lp->lp_ifp->if_capenable;
679 ena = (ena == ~0 ? 0 : ena);
682 * Apply common enabled capabilities back to the lagg ports.
683 * May require several iterations if they are dependent.
687 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
688 lagg_setcaps(lp, ena);
689 ena &= lp->lp_ifp->if_capenable;
691 } while (pena != ena);
693 /* Get other capabilities from the lagg ports */
696 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
697 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
698 cap &= lp->lp_ifp->if_capabilities;
699 hwa &= lp->lp_ifp->if_hwassist;
700 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
702 cap = (cap == ~0 ? 0 : cap);
703 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
705 if (sc->sc_ifp->if_capabilities != cap ||
706 sc->sc_ifp->if_capenable != ena ||
707 sc->sc_ifp->if_hwassist != hwa ||
708 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
709 sc->sc_ifp->if_capabilities = cap;
710 sc->sc_ifp->if_capenable = ena;
711 sc->sc_ifp->if_hwassist = hwa;
712 getmicrotime(&sc->sc_ifp->if_lastchange);
714 if (sc->sc_ifflags & IFF_DEBUG)
715 if_printf(sc->sc_ifp,
716 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
721 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
723 struct lagg_softc *sc_ptr;
724 struct lagg_port *lp, *tlp;
726 int error, i, oldmtu;
730 LAGG_XLOCK_ASSERT(sc);
732 if (sc->sc_ifp == ifp) {
733 if_printf(sc->sc_ifp,
734 "cannot add a lagg to itself as a port\n");
738 if (sc->sc_destroying == 1)
741 /* Limit the maximal number of lagg ports */
742 if (sc->sc_count >= LAGG_MAX_PORTS)
745 /* Check if port has already been associated to a lagg */
746 if (ifp->if_lagg != NULL) {
747 /* Port is already in the current lagg? */
748 lp = (struct lagg_port *)ifp->if_lagg;
749 if (lp->lp_softc == sc)
754 switch (sc->sc_ifp->if_type) {
756 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
757 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
758 return (EPROTONOSUPPORT);
759 if_type = IFT_IEEE8023ADLAG;
762 /* XXX Disallow non-infiniband interfaces */
763 if (ifp->if_type != IFT_INFINIBAND)
764 return (EPROTONOSUPPORT);
765 if_type = IFT_INFINIBANDLAG;
771 /* Allow the first Ethernet member to define the MTU */
773 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
774 sc->sc_ifp->if_mtu = ifp->if_mtu;
775 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
776 if (ifp->if_ioctl == NULL) {
777 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
781 oldmtu = ifp->if_mtu;
782 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
783 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
784 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
786 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
790 ifr.ifr_mtu = oldmtu;
793 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
796 /* Check if port is a stacked lagg */
798 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
799 if (ifp == sc_ptr->sc_ifp) {
803 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
806 /* XXX disable stacking for the moment, its untested */
807 #ifdef LAGG_PORT_STACKING
808 lp->lp_flags |= LAGG_PORT_STACK;
809 if (lagg_port_checkstacking(sc_ptr) >=
814 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
826 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ifp->if_addrlen);
827 lp->lp_ifcapenable = ifp->if_capenable;
828 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
829 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
830 lagg_proto_lladdr(sc);
831 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
833 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
835 lagg_setflags(lp, 1);
837 if (CK_SLIST_EMPTY(&sc->sc_ports))
840 /* Change the interface type */
841 lp->lp_iftype = ifp->if_type;
842 ifp->if_type = if_type;
844 lp->lp_ioctl = ifp->if_ioctl;
845 ifp->if_ioctl = lagg_port_ioctl;
846 lp->lp_output = ifp->if_output;
847 ifp->if_output = lagg_port_output;
849 /* Read port counters */
850 pval = lp->port_counters.val;
851 for (i = 0; i < IFCOUNTERS; i++, pval++)
852 *pval = ifp->if_get_counter(ifp, i);
855 * Insert into the list of ports.
856 * Keep ports sorted by if_index. It is handy, when configuration
857 * is predictable and `ifconfig laggN create ...` command
858 * will lead to the same result each time.
860 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
861 if (tlp->lp_ifp->if_index < ifp->if_index && (
862 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
863 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
868 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
870 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
875 if ((error = lagg_proto_addport(sc, lp)) != 0) {
876 /* Remove the port, without calling pr_delport. */
877 lagg_port_destroy(lp, 0);
879 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
883 /* Update lagg capabilities */
884 lagg_capabilities(sc);
890 #ifdef LAGG_PORT_STACKING
892 lagg_port_checkstacking(struct lagg_softc *sc)
894 struct lagg_softc *sc_ptr;
895 struct lagg_port *lp;
898 LAGG_SXLOCK_ASSERT(sc);
899 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
900 if (lp->lp_flags & LAGG_PORT_STACK) {
901 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
902 m = MAX(m, lagg_port_checkstacking(sc_ptr));
911 lagg_port_destroy_cb(epoch_context_t ec)
913 struct lagg_port *lp;
916 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
924 lagg_port_destroy(struct lagg_port *lp, int rundelport)
926 struct lagg_softc *sc = lp->lp_softc;
927 struct lagg_port *lp_ptr, *lp0;
928 struct ifnet *ifp = lp->lp_ifp;
929 uint64_t *pval, vdiff;
932 LAGG_XLOCK_ASSERT(sc);
935 lagg_proto_delport(sc, lp);
937 if (lp->lp_detaching == 0)
940 /* Restore interface */
941 ifp->if_type = lp->lp_iftype;
942 ifp->if_ioctl = lp->lp_ioctl;
943 ifp->if_output = lp->lp_output;
946 /* Update detached port counters */
947 pval = lp->port_counters.val;
948 for (i = 0; i < IFCOUNTERS; i++, pval++) {
949 vdiff = ifp->if_get_counter(ifp, i) - *pval;
950 sc->detached_counters.val[i] += vdiff;
953 /* Finally, remove the port from the lagg */
954 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
957 /* Update the primary interface */
958 if (lp == sc->sc_primary) {
959 uint8_t lladdr[LAGG_ADDR_LEN];
961 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
962 bzero(&lladdr, LAGG_ADDR_LEN);
964 bcopy(lp0->lp_lladdr, lladdr, LAGG_ADDR_LEN);
965 sc->sc_primary = lp0;
966 if (sc->sc_destroying == 0) {
967 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), sc->sc_ifp->if_addrlen);
968 lagg_proto_lladdr(sc);
969 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
973 * Update lladdr for each port (new primary needs update
974 * as well, to switch from old lladdr to its 'real' one)
976 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
977 if_setlladdr(lp_ptr->lp_ifp, lladdr, lp_ptr->lp_ifp->if_addrlen);
981 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
983 if (lp->lp_detaching == 0) {
984 lagg_setflags(lp, 0);
985 lagg_setcaps(lp, lp->lp_ifcapenable);
986 if_setlladdr(ifp, lp->lp_lladdr, ifp->if_addrlen);
990 * free port and release it's ifnet reference after a grace period has
993 NET_EPOCH_CALL(lagg_port_destroy_cb, &lp->lp_epoch_ctx);
994 /* Update lagg capabilities */
995 lagg_capabilities(sc);
1002 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1004 struct epoch_tracker et;
1005 struct lagg_reqport *rp = (struct lagg_reqport *)data;
1006 struct lagg_softc *sc;
1007 struct lagg_port *lp = NULL;
1010 /* Should be checked by the caller */
1011 switch (ifp->if_type) {
1012 case IFT_IEEE8023ADLAG:
1013 case IFT_INFINIBANDLAG:
1014 if ((lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
1023 if (rp->rp_portname[0] == '\0' ||
1024 ifunit(rp->rp_portname) != ifp) {
1029 NET_EPOCH_ENTER(et);
1030 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
1036 lagg_port2req(lp, rp);
1041 if (lp->lp_ioctl == NULL) {
1045 error = (*lp->lp_ioctl)(ifp, cmd, data);
1049 /* Update lagg interface capabilities */
1051 lagg_capabilities(sc);
1053 VLAN_CAPABILITIES(sc->sc_ifp);
1057 /* Do not allow the MTU to be changed once joined */
1068 if (lp != NULL && lp->lp_ioctl != NULL)
1069 return ((*lp->lp_ioctl)(ifp, cmd, data));
1075 * Requests counter @cnt data.
1077 * Counter value is calculated the following way:
1078 * 1) for each port, sum difference between current and "initial" measurements.
1079 * 2) add lagg logical interface counters.
1080 * 3) add data from detached_counters array.
1082 * We also do the following things on ports attach/detach:
1083 * 1) On port attach we store all counters it has into port_counter array.
1084 * 2) On port detach we add the different between "initial" and
1085 * current counters data to detached_counters array.
1088 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1090 struct epoch_tracker et;
1091 struct lagg_softc *sc;
1092 struct lagg_port *lp;
1093 struct ifnet *lpifp;
1094 uint64_t newval, oldval, vsum;
1096 /* Revise this when we've got non-generic counters. */
1097 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1099 sc = (struct lagg_softc *)ifp->if_softc;
1102 NET_EPOCH_ENTER(et);
1103 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1104 /* Saved attached value */
1105 oldval = lp->port_counters.val[cnt];
1108 newval = lpifp->if_get_counter(lpifp, cnt);
1109 /* Calculate diff and save new */
1110 vsum += newval - oldval;
1115 * Add counter data which might be added by upper
1116 * layer protocols operating on logical interface.
1118 vsum += if_get_counter_default(ifp, cnt);
1121 * Add counter data from detached ports counters
1123 vsum += sc->detached_counters.val[cnt];
1129 * For direct output to child ports.
1132 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1133 const struct sockaddr *dst, struct route *ro)
1135 struct lagg_port *lp = ifp->if_lagg;
1137 switch (dst->sa_family) {
1138 case pseudo_AF_HDRCMPLT:
1141 return ((*lp->lp_output)(ifp, m, dst, ro));
1144 /* drop any other frames */
1150 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1152 struct lagg_port *lp;
1153 struct lagg_softc *sc;
1155 if ((lp = ifp->if_lagg) == NULL)
1157 /* If the ifnet is just being renamed, don't do anything. */
1158 if (ifp->if_flags & IFF_RENAMING)
1164 lp->lp_detaching = 1;
1165 lagg_port_destroy(lp, 1);
1167 VLAN_CAPABILITIES(sc->sc_ifp);
1171 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1173 struct lagg_softc *sc = lp->lp_softc;
1175 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1176 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1177 rp->rp_prio = lp->lp_prio;
1178 rp->rp_flags = lp->lp_flags;
1179 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1181 /* Add protocol specific flags */
1182 switch (sc->sc_proto) {
1183 case LAGG_PROTO_FAILOVER:
1184 if (lp == sc->sc_primary)
1185 rp->rp_flags |= LAGG_PORT_MASTER;
1186 if (lp == lagg_link_active(sc, sc->sc_primary))
1187 rp->rp_flags |= LAGG_PORT_ACTIVE;
1190 case LAGG_PROTO_ROUNDROBIN:
1191 case LAGG_PROTO_LOADBALANCE:
1192 case LAGG_PROTO_BROADCAST:
1193 if (LAGG_PORTACTIVE(lp))
1194 rp->rp_flags |= LAGG_PORT_ACTIVE;
1197 case LAGG_PROTO_LACP:
1198 /* LACP has a different definition of active */
1199 if (lacp_isactive(lp))
1200 rp->rp_flags |= LAGG_PORT_ACTIVE;
1201 if (lacp_iscollecting(lp))
1202 rp->rp_flags |= LAGG_PORT_COLLECTING;
1203 if (lacp_isdistributing(lp))
1204 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1211 lagg_watchdog_infiniband(void *arg)
1213 struct epoch_tracker et;
1214 struct lagg_softc *sc;
1215 struct lagg_port *lp;
1217 struct ifnet *lp_ifp;
1222 * Because infiniband nodes have a fixed MAC address, which is
1223 * generated by the so-called GID, we need to regularly update
1224 * the link level address of the parent lagg<N> device when
1225 * the active port changes. Possibly we could piggy-back on
1226 * link up/down events aswell, but using a timer also provides
1227 * a guarantee against too frequent events. This operation
1228 * does not have to be atomic.
1230 NET_EPOCH_ENTER(et);
1231 lp = lagg_link_active(sc, sc->sc_primary);
1234 lp_ifp = lp->lp_ifp;
1236 if (ifp != NULL && lp_ifp != NULL &&
1237 (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen) != 0 ||
1238 memcmp(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen) != 0)) {
1239 memcpy(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen);
1240 memcpy(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen);
1242 CURVNET_SET(ifp->if_vnet);
1243 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1249 callout_reset(&sc->sc_watchdog, hz, &lagg_watchdog_infiniband, arg);
1253 lagg_init(void *xsc)
1255 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1256 struct ifnet *ifp = sc->sc_ifp;
1257 struct lagg_port *lp;
1260 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1265 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1268 * Update the port lladdrs if needed.
1269 * This might be if_setlladdr() notification
1270 * that lladdr has been changed.
1272 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1273 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1274 ifp->if_addrlen) != 0)
1275 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1278 lagg_proto_init(sc);
1280 if (ifp->if_type == IFT_INFINIBAND) {
1281 mtx_lock(&sc->sc_mtx);
1282 lagg_watchdog_infiniband(sc);
1283 mtx_unlock(&sc->sc_mtx);
1290 lagg_stop(struct lagg_softc *sc)
1292 struct ifnet *ifp = sc->sc_ifp;
1294 LAGG_XLOCK_ASSERT(sc);
1296 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1299 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1301 lagg_proto_stop(sc);
1303 mtx_lock(&sc->sc_mtx);
1304 callout_stop(&sc->sc_watchdog);
1305 mtx_unlock(&sc->sc_mtx);
1307 callout_drain(&sc->sc_watchdog);
1311 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1313 struct epoch_tracker et;
1314 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1315 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1316 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1317 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1318 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1319 struct ifreq *ifr = (struct ifreq *)data;
1320 struct lagg_port *lp;
1322 struct thread *td = curthread;
1324 int count, buflen, len, error = 0, oldmtu;
1326 bzero(&rpbuf, sizeof(rpbuf));
1328 /* XXX: This can race with lagg_clone_destroy. */
1333 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1334 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1335 ra->ra_proto = sc->sc_proto;
1336 lagg_proto_request(sc, &ra->ra_psc);
1339 len = min(ra->ra_size, buflen);
1340 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1341 if (len < sizeof(rpbuf))
1344 lagg_port2req(lp, &rpbuf);
1345 memcpy(buf, &rpbuf, sizeof(rpbuf));
1347 buf += sizeof(rpbuf);
1348 len -= sizeof(rpbuf);
1351 ra->ra_ports = count;
1352 ra->ra_size = count * sizeof(rpbuf);
1353 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1354 free(outbuf, M_TEMP);
1357 error = priv_check(td, PRIV_NET_LAGG);
1360 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1361 error = EPROTONOSUPPORT;
1364 /* Infiniband only supports the failover protocol. */
1365 if (ra->ra_proto != LAGG_PROTO_FAILOVER &&
1366 ifp->if_type == IFT_INFINIBAND) {
1367 error = EPROTONOSUPPORT;
1371 lagg_proto_detach(sc);
1372 lagg_proto_attach(sc, ra->ra_proto);
1377 ro->ro_opts = sc->sc_opts;
1378 if (sc->sc_proto == LAGG_PROTO_LACP) {
1379 struct lacp_softc *lsc;
1381 lsc = (struct lacp_softc *)sc->sc_psc;
1382 if (lsc->lsc_debug.lsc_tx_test != 0)
1383 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1384 if (lsc->lsc_debug.lsc_rx_test != 0)
1385 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1386 if (lsc->lsc_strict_mode != 0)
1387 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1388 if (lsc->lsc_fast_timeout != 0)
1389 ro->ro_opts |= LAGG_OPT_LACP_FAST_TIMO;
1391 ro->ro_active = sc->sc_active;
1394 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1395 ro->ro_active += LAGG_PORTACTIVE(lp);
1397 ro->ro_bkt = sc->sc_stride;
1398 ro->ro_flapping = sc->sc_flapping;
1399 ro->ro_flowid_shift = sc->flowid_shift;
1403 error = priv_check(td, PRIV_NET_LAGG);
1408 * The stride option was added without defining a corresponding
1409 * LAGG_OPT flag, so handle a non-zero value before checking
1410 * anything else to preserve compatibility.
1413 if (ro->ro_opts == 0 && ro->ro_bkt != 0) {
1414 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN) {
1419 sc->sc_stride = ro->ro_bkt;
1421 if (ro->ro_opts == 0) {
1427 * Set options. LACP options are stored in sc->sc_psc,
1432 switch (ro->ro_opts) {
1433 case LAGG_OPT_USE_FLOWID:
1434 case -LAGG_OPT_USE_FLOWID:
1435 case LAGG_OPT_USE_NUMA:
1436 case -LAGG_OPT_USE_NUMA:
1437 case LAGG_OPT_FLOWIDSHIFT:
1438 case LAGG_OPT_RR_LIMIT:
1442 case LAGG_OPT_LACP_TXTEST:
1443 case -LAGG_OPT_LACP_TXTEST:
1444 case LAGG_OPT_LACP_RXTEST:
1445 case -LAGG_OPT_LACP_RXTEST:
1446 case LAGG_OPT_LACP_STRICT:
1447 case -LAGG_OPT_LACP_STRICT:
1448 case LAGG_OPT_LACP_FAST_TIMO:
1449 case -LAGG_OPT_LACP_FAST_TIMO:
1458 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1459 /* Invalid combination of options specified. */
1462 break; /* Return from SIOCSLAGGOPTS. */
1466 * Store new options into sc->sc_opts except for
1467 * FLOWIDSHIFT, RR and LACP options.
1470 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1471 sc->flowid_shift = ro->ro_flowid_shift;
1472 else if (ro->ro_opts == LAGG_OPT_RR_LIMIT) {
1473 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN ||
1479 sc->sc_stride = ro->ro_bkt;
1480 } else if (ro->ro_opts > 0)
1481 sc->sc_opts |= ro->ro_opts;
1483 sc->sc_opts &= ~ro->ro_opts;
1485 struct lacp_softc *lsc;
1486 struct lacp_port *lp;
1488 lsc = (struct lacp_softc *)sc->sc_psc;
1490 switch (ro->ro_opts) {
1491 case LAGG_OPT_LACP_TXTEST:
1492 lsc->lsc_debug.lsc_tx_test = 1;
1494 case -LAGG_OPT_LACP_TXTEST:
1495 lsc->lsc_debug.lsc_tx_test = 0;
1497 case LAGG_OPT_LACP_RXTEST:
1498 lsc->lsc_debug.lsc_rx_test = 1;
1500 case -LAGG_OPT_LACP_RXTEST:
1501 lsc->lsc_debug.lsc_rx_test = 0;
1503 case LAGG_OPT_LACP_STRICT:
1504 lsc->lsc_strict_mode = 1;
1506 case -LAGG_OPT_LACP_STRICT:
1507 lsc->lsc_strict_mode = 0;
1509 case LAGG_OPT_LACP_FAST_TIMO:
1511 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1512 lp->lp_state |= LACP_STATE_TIMEOUT;
1514 lsc->lsc_fast_timeout = 1;
1516 case -LAGG_OPT_LACP_FAST_TIMO:
1518 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1519 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1521 lsc->lsc_fast_timeout = 0;
1527 case SIOCGLAGGFLAGS:
1530 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1531 rf->rf_flags |= LAGG_F_HASHL2;
1532 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1533 rf->rf_flags |= LAGG_F_HASHL3;
1534 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1535 rf->rf_flags |= LAGG_F_HASHL4;
1539 error = priv_check(td, PRIV_NET_LAGG);
1542 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1548 if (rf->rf_flags & LAGG_F_HASHL2)
1549 sc->sc_flags |= MBUF_HASHFLAG_L2;
1550 if (rf->rf_flags & LAGG_F_HASHL3)
1551 sc->sc_flags |= MBUF_HASHFLAG_L3;
1552 if (rf->rf_flags & LAGG_F_HASHL4)
1553 sc->sc_flags |= MBUF_HASHFLAG_L4;
1557 if (rp->rp_portname[0] == '\0' ||
1558 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1563 NET_EPOCH_ENTER(et);
1564 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1565 lp->lp_softc != sc) {
1572 lagg_port2req(lp, rp);
1577 error = priv_check(td, PRIV_NET_LAGG);
1580 if (rp->rp_portname[0] == '\0' ||
1581 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1587 * A laggport interface should not have inet6 address
1588 * because two interfaces with a valid link-local
1589 * scope zone must not be merged in any form. This
1590 * restriction is needed to prevent violation of
1591 * link-local scope zone. Attempts to add a laggport
1592 * interface which has inet6 addresses triggers
1593 * removal of all inet6 addresses on the member
1596 if (in6ifa_llaonifp(tpif)) {
1598 if_printf(sc->sc_ifp,
1599 "IPv6 addresses on %s have been removed "
1600 "before adding it as a member to prevent "
1601 "IPv6 address scope violation.\n",
1605 oldmtu = ifp->if_mtu;
1607 error = lagg_port_create(sc, tpif);
1612 * LAGG MTU may change during addition of the first port.
1613 * If it did, do network layer specific procedure.
1615 if (ifp->if_mtu != oldmtu) {
1622 VLAN_CAPABILITIES(ifp);
1624 case SIOCSLAGGDELPORT:
1625 error = priv_check(td, PRIV_NET_LAGG);
1628 if (rp->rp_portname[0] == '\0' ||
1629 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1635 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1636 lp->lp_softc != sc) {
1643 error = lagg_port_destroy(lp, 1);
1646 VLAN_CAPABILITIES(ifp);
1649 /* Set flags on ports too */
1651 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1652 lagg_setflags(lp, 1);
1655 if (!(ifp->if_flags & IFF_UP) &&
1656 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1658 * If interface is marked down and it is running,
1659 * then stop and disable it.
1663 } else if ((ifp->if_flags & IFF_UP) &&
1664 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1666 * If interface is marked up and it is stopped, then
1670 (*ifp->if_init)(sc);
1677 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1686 if (ifp->if_type == IFT_INFINIBAND)
1689 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1694 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1695 if (lp->lp_ioctl != NULL)
1696 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1698 lagg_capabilities(sc);
1700 VLAN_CAPABILITIES(ifp);
1706 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1707 if (lp->lp_ioctl != NULL)
1708 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1713 "failed to change MTU to %d on port %s, "
1714 "reverting all ports to original MTU (%d)\n",
1715 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1720 ifp->if_mtu = ifr->ifr_mtu;
1722 /* set every port back to the original MTU */
1723 ifr->ifr_mtu = ifp->if_mtu;
1724 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1725 if (lp->lp_ioctl != NULL)
1726 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1733 error = ether_ioctl(ifp, cmd, data);
1739 #if defined(KERN_TLS) || defined(RATELIMIT)
1740 static inline struct lagg_snd_tag *
1741 mst_to_lst(struct m_snd_tag *mst)
1744 return (__containerof(mst, struct lagg_snd_tag, com));
1748 * Look up the port used by a specific flow. This only works for lagg
1749 * protocols with deterministic port mappings (e.g. not roundrobin).
1750 * In addition protocols which use a hash to map flows to ports must
1751 * be configured to use the mbuf flowid rather than hashing packet
1754 static struct lagg_port *
1755 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype,
1756 uint8_t numa_domain)
1758 struct lagg_softc *sc;
1759 struct lagg_port *lp;
1766 switch (sc->sc_proto) {
1767 case LAGG_PROTO_FAILOVER:
1768 return (lagg_link_active(sc, sc->sc_primary));
1769 case LAGG_PROTO_LOADBALANCE:
1770 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1771 flowtype == M_HASHTYPE_NONE)
1773 p = flowid >> sc->flowid_shift;
1775 lb = (struct lagg_lb *)sc->sc_psc;
1776 lp = lb->lb_ports[p];
1777 return (lagg_link_active(sc, lp));
1778 case LAGG_PROTO_LACP:
1779 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1780 flowtype == M_HASHTYPE_NONE)
1782 hash = flowid >> sc->flowid_shift;
1783 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, &err));
1790 lagg_snd_tag_alloc(struct ifnet *ifp,
1791 union if_snd_tag_alloc_params *params,
1792 struct m_snd_tag **ppmt)
1794 struct epoch_tracker et;
1795 struct lagg_snd_tag *lst;
1796 struct lagg_softc *sc;
1797 struct lagg_port *lp;
1798 struct ifnet *lp_ifp;
1803 NET_EPOCH_ENTER(et);
1804 lp = lookup_snd_tag_port(ifp, params->hdr.flowid,
1805 params->hdr.flowtype, params->hdr.numa_domain);
1808 return (EOPNOTSUPP);
1810 if (lp->lp_ifp == NULL) {
1812 return (EOPNOTSUPP);
1814 lp_ifp = lp->lp_ifp;
1818 lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1824 error = m_snd_tag_alloc(lp_ifp, params, &lst->tag);
1831 m_snd_tag_init(&lst->com, ifp, lst->tag->type);
1838 lagg_snd_tag_modify(struct m_snd_tag *mst,
1839 union if_snd_tag_modify_params *params)
1841 struct lagg_snd_tag *lst;
1843 lst = mst_to_lst(mst);
1844 return (lst->tag->ifp->if_snd_tag_modify(lst->tag, params));
1848 lagg_snd_tag_query(struct m_snd_tag *mst,
1849 union if_snd_tag_query_params *params)
1851 struct lagg_snd_tag *lst;
1853 lst = mst_to_lst(mst);
1854 return (lst->tag->ifp->if_snd_tag_query(lst->tag, params));
1858 lagg_snd_tag_free(struct m_snd_tag *mst)
1860 struct lagg_snd_tag *lst;
1862 lst = mst_to_lst(mst);
1863 m_snd_tag_rele(lst->tag);
1868 lagg_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
1871 * For lagg, we have an indirect
1872 * interface. The caller needs to
1873 * get a ratelimit tag on the actual
1874 * interface the flow will go on.
1876 q->rate_table = NULL;
1877 q->flags = RT_IS_INDIRECT;
1879 q->number_of_rates = 0;
1884 lagg_setmulti(struct lagg_port *lp)
1886 struct lagg_softc *sc = lp->lp_softc;
1887 struct ifnet *ifp = lp->lp_ifp;
1888 struct ifnet *scifp = sc->sc_ifp;
1890 struct ifmultiaddr *ifma;
1893 IF_ADDR_WLOCK(scifp);
1894 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1895 if (ifma->ifma_addr->sa_family != AF_LINK)
1897 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1899 IF_ADDR_WUNLOCK(scifp);
1902 bcopy(ifma->ifma_addr, &mc->mc_addr,
1903 ifma->ifma_addr->sa_len);
1904 mc->mc_addr.sdl_index = ifp->if_index;
1906 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1908 IF_ADDR_WUNLOCK(scifp);
1909 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1910 error = if_addmulti(ifp,
1911 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1919 lagg_clrmulti(struct lagg_port *lp)
1923 LAGG_XLOCK_ASSERT(lp->lp_softc);
1924 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1925 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1926 if (mc->mc_ifma && lp->lp_detaching == 0)
1927 if_delmulti_ifma(mc->mc_ifma);
1934 lagg_setcaps(struct lagg_port *lp, int cap)
1938 if (lp->lp_ifp->if_capenable == cap)
1940 if (lp->lp_ioctl == NULL)
1942 ifr.ifr_reqcap = cap;
1943 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1946 /* Handle a ref counted flag that should be set on the lagg port as well */
1948 lagg_setflag(struct lagg_port *lp, int flag, int status,
1949 int (*func)(struct ifnet *, int))
1951 struct lagg_softc *sc = lp->lp_softc;
1952 struct ifnet *scifp = sc->sc_ifp;
1953 struct ifnet *ifp = lp->lp_ifp;
1956 LAGG_XLOCK_ASSERT(sc);
1958 status = status ? (scifp->if_flags & flag) : 0;
1959 /* Now "status" contains the flag value or 0 */
1962 * See if recorded ports status is different from what
1963 * we want it to be. If it is, flip it. We record ports
1964 * status in lp_ifflags so that we won't clear ports flag
1965 * we haven't set. In fact, we don't clear or set ports
1966 * flags directly, but get or release references to them.
1967 * That's why we can be sure that recorded flags still are
1968 * in accord with actual ports flags.
1970 if (status != (lp->lp_ifflags & flag)) {
1971 error = (*func)(ifp, status);
1974 lp->lp_ifflags &= ~flag;
1975 lp->lp_ifflags |= status;
1981 * Handle IFF_* flags that require certain changes on the lagg port
1982 * if "status" is true, update ports flags respective to the lagg
1983 * if "status" is false, forcedly clear the flags set on port.
1986 lagg_setflags(struct lagg_port *lp, int status)
1990 for (i = 0; lagg_pflags[i].flag; i++) {
1991 error = lagg_setflag(lp, lagg_pflags[i].flag,
1992 status, lagg_pflags[i].func);
2000 lagg_transmit_ethernet(struct ifnet *ifp, struct mbuf *m)
2002 struct epoch_tracker et;
2003 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2006 #if defined(KERN_TLS) || defined(RATELIMIT)
2007 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2008 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2010 NET_EPOCH_ENTER(et);
2011 /* We need a Tx algorithm and at least one port */
2012 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2015 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2019 ETHER_BPF_MTAP(ifp, m);
2021 error = lagg_proto_start(sc, m);
2027 lagg_transmit_infiniband(struct ifnet *ifp, struct mbuf *m)
2029 struct epoch_tracker et;
2030 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2033 #if defined(KERN_TLS) || defined(RATELIMIT)
2034 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2035 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2037 NET_EPOCH_ENTER(et);
2038 /* We need a Tx algorithm and at least one port */
2039 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2042 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2046 INFINIBAND_BPF_MTAP(ifp, m);
2048 error = lagg_proto_start(sc, m);
2054 * The ifp->if_qflush entry point for lagg(4) is no-op.
2057 lagg_qflush(struct ifnet *ifp __unused)
2061 static struct mbuf *
2062 lagg_input_ethernet(struct ifnet *ifp, struct mbuf *m)
2064 struct epoch_tracker et;
2065 struct lagg_port *lp = ifp->if_lagg;
2066 struct lagg_softc *sc = lp->lp_softc;
2067 struct ifnet *scifp = sc->sc_ifp;
2069 NET_EPOCH_ENTER(et);
2070 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2071 lp->lp_detaching != 0 ||
2072 sc->sc_proto == LAGG_PROTO_NONE) {
2078 ETHER_BPF_MTAP(scifp, m);
2080 m = lagg_proto_input(sc, lp, m);
2081 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
2090 static struct mbuf *
2091 lagg_input_infiniband(struct ifnet *ifp, struct mbuf *m)
2093 struct epoch_tracker et;
2094 struct lagg_port *lp = ifp->if_lagg;
2095 struct lagg_softc *sc = lp->lp_softc;
2096 struct ifnet *scifp = sc->sc_ifp;
2098 NET_EPOCH_ENTER(et);
2099 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2100 lp->lp_detaching != 0 ||
2101 sc->sc_proto == LAGG_PROTO_NONE) {
2107 INFINIBAND_BPF_MTAP(scifp, m);
2109 m = lagg_proto_input(sc, lp, m);
2110 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
2120 lagg_media_change(struct ifnet *ifp)
2122 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2124 if (sc->sc_ifflags & IFF_DEBUG)
2125 printf("%s\n", __func__);
2132 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2134 struct epoch_tracker et;
2135 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2136 struct lagg_port *lp;
2138 imr->ifm_status = IFM_AVALID;
2139 imr->ifm_active = IFM_ETHER | IFM_AUTO;
2141 NET_EPOCH_ENTER(et);
2142 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2143 if (LAGG_PORTACTIVE(lp))
2144 imr->ifm_status |= IFM_ACTIVE;
2150 lagg_linkstate(struct lagg_softc *sc)
2152 struct epoch_tracker et;
2153 struct lagg_port *lp;
2154 int new_link = LINK_STATE_DOWN;
2157 LAGG_XLOCK_ASSERT(sc);
2159 /* LACP handles link state itself */
2160 if (sc->sc_proto == LAGG_PROTO_LACP)
2163 /* Our link is considered up if at least one of our ports is active */
2164 NET_EPOCH_ENTER(et);
2165 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2166 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
2167 new_link = LINK_STATE_UP;
2172 if_link_state_change(sc->sc_ifp, new_link);
2174 /* Update if_baudrate to reflect the max possible speed */
2175 switch (sc->sc_proto) {
2176 case LAGG_PROTO_FAILOVER:
2177 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
2178 sc->sc_primary->lp_ifp->if_baudrate : 0;
2180 case LAGG_PROTO_ROUNDROBIN:
2181 case LAGG_PROTO_LOADBALANCE:
2182 case LAGG_PROTO_BROADCAST:
2184 NET_EPOCH_ENTER(et);
2185 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2186 speed += lp->lp_ifp->if_baudrate;
2188 sc->sc_ifp->if_baudrate = speed;
2190 case LAGG_PROTO_LACP:
2191 /* LACP updates if_baudrate itself */
2197 lagg_port_state(struct ifnet *ifp, int state)
2199 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
2200 struct lagg_softc *sc = NULL;
2209 lagg_proto_linkstate(sc, lp);
2214 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
2216 struct lagg_port *lp_next, *rval = NULL;
2219 * Search a port which reports an active link state.
2224 * This is called with either in the network epoch
2225 * or with LAGG_XLOCK(sc) held.
2227 if (!in_epoch(net_epoch_preempt))
2228 LAGG_XLOCK_ASSERT(sc);
2233 if (LAGG_PORTACTIVE(lp)) {
2237 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
2238 LAGG_PORTACTIVE(lp_next)) {
2244 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2245 if (LAGG_PORTACTIVE(lp_next)) {
2254 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2257 #if defined(KERN_TLS) || defined(RATELIMIT)
2258 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2259 struct lagg_snd_tag *lst;
2260 struct m_snd_tag *mst;
2262 mst = m->m_pkthdr.snd_tag;
2263 lst = mst_to_lst(mst);
2264 if (lst->tag->ifp != ifp) {
2268 m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2269 m_snd_tag_rele(mst);
2272 return (ifp->if_transmit)(ifp, m);
2276 * Simple round robin aggregation
2279 lagg_rr_attach(struct lagg_softc *sc)
2286 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2288 struct lagg_port *lp;
2291 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2294 lp = CK_SLIST_FIRST(&sc->sc_ports);
2297 lp = CK_SLIST_NEXT(lp, lp_entries);
2300 * Check the port's link state. This will return the next active
2301 * port if the link is down or the port is NULL.
2303 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2304 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2310 return (lagg_enqueue(lp->lp_ifp, m));
2313 static struct mbuf *
2314 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2316 struct ifnet *ifp = sc->sc_ifp;
2318 /* Just pass in the packet to our lagg device */
2319 m->m_pkthdr.rcvif = ifp;
2328 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2330 int active_ports = 0;
2333 struct lagg_port *lp, *last = NULL;
2337 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2338 if (!LAGG_PORTACTIVE(lp))
2344 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2350 lagg_enqueue(last->lp_ifp, m0);
2356 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2360 if ((last = lagg_link_active(sc, last)) == NULL) {
2362 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2367 ret = lagg_enqueue(last->lp_ifp, m);
2369 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2375 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2377 struct ifnet *ifp = sc->sc_ifp;
2379 /* Just pass in the packet to our lagg device */
2380 m->m_pkthdr.rcvif = ifp;
2388 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2390 struct lagg_port *lp;
2392 /* Use the master port if active or the next available port */
2393 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2394 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2400 return (lagg_enqueue(lp->lp_ifp, m));
2403 static struct mbuf *
2404 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2406 struct ifnet *ifp = sc->sc_ifp;
2407 struct lagg_port *tmp_tp;
2409 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2410 m->m_pkthdr.rcvif = ifp;
2414 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2415 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2417 * If tmp_tp is null, we've received a packet when all
2418 * our links are down. Weird, but process it anyways.
2420 if ((tmp_tp == NULL || tmp_tp == lp)) {
2421 m->m_pkthdr.rcvif = ifp;
2434 lagg_lb_attach(struct lagg_softc *sc)
2436 struct lagg_port *lp;
2439 LAGG_XLOCK_ASSERT(sc);
2440 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2441 lb->lb_key = m_ether_tcpip_hash_init();
2444 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2445 lagg_lb_port_create(lp);
2449 lagg_lb_detach(struct lagg_softc *sc)
2453 lb = (struct lagg_lb *)sc->sc_psc;
2459 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2461 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2462 struct lagg_port *lp_next;
2466 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2467 LAGG_XLOCK_ASSERT(sc);
2468 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2471 if (i >= LAGG_MAX_PORTS) {
2475 if (sc->sc_ifflags & IFF_DEBUG)
2476 printf("%s: port %s at index %d\n",
2477 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2478 lb->lb_ports[i++] = lp_next;
2485 lagg_lb_port_create(struct lagg_port *lp)
2487 struct lagg_softc *sc = lp->lp_softc;
2488 return (lagg_lb_porttable(sc, NULL));
2492 lagg_lb_port_destroy(struct lagg_port *lp)
2494 struct lagg_softc *sc = lp->lp_softc;
2495 lagg_lb_porttable(sc, lp);
2499 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2501 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2502 struct lagg_port *lp = NULL;
2505 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2506 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2507 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2509 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2511 lp = lb->lb_ports[p];
2514 * Check the port's link state. This will return the next active
2515 * port if the link is down or the port is NULL.
2517 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2518 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2524 return (lagg_enqueue(lp->lp_ifp, m));
2527 static struct mbuf *
2528 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2530 struct ifnet *ifp = sc->sc_ifp;
2532 /* Just pass in the packet to our lagg device */
2533 m->m_pkthdr.rcvif = ifp;
2542 lagg_lacp_attach(struct lagg_softc *sc)
2544 struct lagg_port *lp;
2547 LAGG_XLOCK_ASSERT(sc);
2548 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2549 lacp_port_create(lp);
2553 lagg_lacp_detach(struct lagg_softc *sc)
2555 struct lagg_port *lp;
2558 LAGG_XLOCK_ASSERT(sc);
2559 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2560 lacp_port_destroy(lp);
2568 lagg_lacp_lladdr(struct lagg_softc *sc)
2570 struct lagg_port *lp;
2572 LAGG_SXLOCK_ASSERT(sc);
2574 /* purge all the lacp ports */
2575 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2576 lacp_port_destroy(lp);
2578 /* add them back in */
2579 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2580 lacp_port_create(lp);
2584 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2586 struct lagg_port *lp;
2589 lp = lacp_select_tx_port(sc, m, &err);
2591 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2597 return (lagg_enqueue(lp->lp_ifp, m));
2600 static struct mbuf *
2601 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2603 struct ifnet *ifp = sc->sc_ifp;
2604 struct ether_header *eh;
2607 eh = mtod(m, struct ether_header *);
2608 etype = ntohs(eh->ether_type);
2610 /* Tap off LACP control messages */
2611 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2612 m = lacp_input(lp, m);
2618 * If the port is not collecting or not in the active aggregator then
2621 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2626 m->m_pkthdr.rcvif = ifp;