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>
23 #include "opt_inet6.h"
24 #include "opt_kern_tls.h"
25 #include "opt_ratelimit.h"
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
37 #include <sys/systm.h>
40 #include <sys/rmlock.h>
42 #include <sys/taskqueue.h>
43 #include <sys/eventhandler.h>
45 #include <net/ethernet.h>
47 #include <net/if_clone.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
53 #include <net/if_private.h>
55 #include <net/route.h>
57 #include <net/infiniband.h>
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
61 #include <netinet/ip.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/if_ether.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/in6_ifattach.h>
74 #include <net/if_vlan_var.h>
75 #include <net/if_lagg.h>
76 #include <net/ieee8023ad_lacp.h>
79 MODULE_DEPEND(if_lagg, netmap, 1, 1, 1);
82 #define LAGG_SX_INIT(_sc) sx_init(&(_sc)->sc_sx, "if_lagg sx")
83 #define LAGG_SX_DESTROY(_sc) sx_destroy(&(_sc)->sc_sx)
84 #define LAGG_XLOCK(_sc) sx_xlock(&(_sc)->sc_sx)
85 #define LAGG_XUNLOCK(_sc) sx_xunlock(&(_sc)->sc_sx)
86 #define LAGG_SXLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_LOCKED)
87 #define LAGG_XLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
89 /* Special flags we should propagate to the lagg ports. */
92 int (*func)(struct ifnet *, int);
94 {IFF_PROMISC, ifpromisc},
95 {IFF_ALLMULTI, if_allmulti},
100 struct m_snd_tag com;
101 struct m_snd_tag *tag;
104 VNET_DEFINE_STATIC(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
105 #define V_lagg_list VNET(lagg_list)
106 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
107 #define V_lagg_list_mtx VNET(lagg_list_mtx)
108 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
109 "if_lagg list", NULL, MTX_DEF)
110 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
111 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
112 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
113 static eventhandler_tag lagg_detach_cookie = NULL;
115 static int lagg_clone_create(struct if_clone *, char *, size_t,
116 struct ifc_data *, struct ifnet **);
117 static int lagg_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
118 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
119 #define V_lagg_cloner VNET(lagg_cloner)
120 static const char laggname[] = "lagg";
121 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
123 static void lagg_capabilities(struct lagg_softc *);
124 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
125 static int lagg_port_destroy(struct lagg_port *, int);
126 static struct mbuf *lagg_input_ethernet(struct ifnet *, struct mbuf *);
127 static struct mbuf *lagg_input_infiniband(struct ifnet *, struct mbuf *);
128 static void lagg_linkstate(struct lagg_softc *);
129 static void lagg_port_state(struct ifnet *, int);
130 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
131 static int lagg_port_output(struct ifnet *, struct mbuf *,
132 const struct sockaddr *, struct route *);
133 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
134 #ifdef LAGG_PORT_STACKING
135 static int lagg_port_checkstacking(struct lagg_softc *);
137 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
138 static void lagg_init(void *);
139 static void lagg_stop(struct lagg_softc *);
140 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
141 #if defined(KERN_TLS) || defined(RATELIMIT)
142 static int lagg_snd_tag_alloc(struct ifnet *,
143 union if_snd_tag_alloc_params *,
144 struct m_snd_tag **);
145 static int lagg_snd_tag_modify(struct m_snd_tag *,
146 union if_snd_tag_modify_params *);
147 static int lagg_snd_tag_query(struct m_snd_tag *,
148 union if_snd_tag_query_params *);
149 static void lagg_snd_tag_free(struct m_snd_tag *);
150 static struct m_snd_tag *lagg_next_snd_tag(struct m_snd_tag *);
151 static void lagg_ratelimit_query(struct ifnet *,
152 struct if_ratelimit_query_results *);
154 static int lagg_setmulti(struct lagg_port *);
155 static int lagg_clrmulti(struct lagg_port *);
156 static void lagg_setcaps(struct lagg_port *, int cap, int cap2);
157 static int lagg_setflag(struct lagg_port *, int, int,
158 int (*func)(struct ifnet *, int));
159 static int lagg_setflags(struct lagg_port *, int status);
160 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
161 static int lagg_transmit_ethernet(struct ifnet *, struct mbuf *);
162 static int lagg_transmit_infiniband(struct ifnet *, struct mbuf *);
163 static void lagg_qflush(struct ifnet *);
164 static int lagg_media_change(struct ifnet *);
165 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
166 static struct lagg_port *lagg_link_active(struct lagg_softc *,
169 /* Simple round robin */
170 static void lagg_rr_attach(struct lagg_softc *);
171 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
173 /* Active failover */
174 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
175 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
179 static void lagg_lb_attach(struct lagg_softc *);
180 static void lagg_lb_detach(struct lagg_softc *);
181 static int lagg_lb_port_create(struct lagg_port *);
182 static void lagg_lb_port_destroy(struct lagg_port *);
183 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
184 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
187 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
190 static void lagg_lacp_attach(struct lagg_softc *);
191 static void lagg_lacp_detach(struct lagg_softc *);
192 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
193 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
195 static void lagg_lacp_lladdr(struct lagg_softc *);
198 static struct mbuf *lagg_default_input(struct lagg_softc *, struct lagg_port *,
201 /* lagg protocol table */
202 static const struct lagg_proto {
204 void (*pr_attach)(struct lagg_softc *);
205 void (*pr_detach)(struct lagg_softc *);
206 int (*pr_start)(struct lagg_softc *, struct mbuf *);
207 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
209 int (*pr_addport)(struct lagg_port *);
210 void (*pr_delport)(struct lagg_port *);
211 void (*pr_linkstate)(struct lagg_port *);
212 void (*pr_init)(struct lagg_softc *);
213 void (*pr_stop)(struct lagg_softc *);
214 void (*pr_lladdr)(struct lagg_softc *);
215 void (*pr_request)(struct lagg_softc *, void *);
216 void (*pr_portreq)(struct lagg_port *, void *);
219 .pr_num = LAGG_PROTO_NONE
222 .pr_num = LAGG_PROTO_ROUNDROBIN,
223 .pr_attach = lagg_rr_attach,
224 .pr_start = lagg_rr_start,
225 .pr_input = lagg_default_input,
228 .pr_num = LAGG_PROTO_FAILOVER,
229 .pr_start = lagg_fail_start,
230 .pr_input = lagg_fail_input,
233 .pr_num = LAGG_PROTO_LOADBALANCE,
234 .pr_attach = lagg_lb_attach,
235 .pr_detach = lagg_lb_detach,
236 .pr_start = lagg_lb_start,
237 .pr_input = lagg_default_input,
238 .pr_addport = lagg_lb_port_create,
239 .pr_delport = lagg_lb_port_destroy,
242 .pr_num = LAGG_PROTO_LACP,
243 .pr_attach = lagg_lacp_attach,
244 .pr_detach = lagg_lacp_detach,
245 .pr_start = lagg_lacp_start,
246 .pr_input = lagg_lacp_input,
247 .pr_addport = lacp_port_create,
248 .pr_delport = lacp_port_destroy,
249 .pr_linkstate = lacp_linkstate,
250 .pr_init = lacp_init,
251 .pr_stop = lacp_stop,
252 .pr_lladdr = lagg_lacp_lladdr,
253 .pr_request = lacp_req,
254 .pr_portreq = lacp_portreq,
257 .pr_num = LAGG_PROTO_BROADCAST,
258 .pr_start = lagg_bcast_start,
259 .pr_input = lagg_default_input,
263 SYSCTL_DECL(_net_link);
264 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
267 /* Allow input on any failover links */
268 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
269 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
270 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
271 &VNET_NAME(lagg_failover_rx_all), 0,
272 "Accept input from any interface in a failover lagg");
274 /* Default value for using flowid */
275 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
276 #define V_def_use_flowid VNET(def_use_flowid)
277 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid,
278 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(def_use_flowid), 0,
279 "Default setting for using flow id for load sharing");
281 /* Default value for using numa */
282 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
283 #define V_def_use_numa VNET(def_use_numa)
284 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa,
285 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(def_use_numa), 0,
286 "Use numa to steer flows");
288 /* Default value for flowid shift */
289 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
290 #define V_def_flowid_shift VNET(def_flowid_shift)
291 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift,
292 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(def_flowid_shift), 0,
293 "Default setting for flowid shift for load sharing");
296 vnet_lagg_init(const void *unused __unused)
299 LAGG_LIST_LOCK_INIT();
300 SLIST_INIT(&V_lagg_list);
301 struct if_clone_addreq req = {
302 .create_f = lagg_clone_create,
303 .destroy_f = lagg_clone_destroy,
304 .flags = IFC_F_AUTOUNIT,
306 V_lagg_cloner = ifc_attach_cloner(laggname, &req);
308 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
309 vnet_lagg_init, NULL);
312 vnet_lagg_uninit(const void *unused __unused)
315 ifc_detach_cloner(V_lagg_cloner);
316 LAGG_LIST_LOCK_DESTROY();
318 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
319 vnet_lagg_uninit, NULL);
322 lagg_modevent(module_t mod, int type, void *data)
327 lagg_input_ethernet_p = lagg_input_ethernet;
328 lagg_input_infiniband_p = lagg_input_infiniband;
329 lagg_linkstate_p = lagg_port_state;
330 lagg_detach_cookie = EVENTHANDLER_REGISTER(
331 ifnet_departure_event, lagg_port_ifdetach, NULL,
332 EVENTHANDLER_PRI_ANY);
335 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
337 lagg_input_ethernet_p = NULL;
338 lagg_input_infiniband_p = NULL;
339 lagg_linkstate_p = NULL;
347 static moduledata_t lagg_mod = {
353 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
354 MODULE_VERSION(if_lagg, 1);
355 MODULE_DEPEND(if_lagg, if_infiniband, 1, 1, 1);
358 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
361 LAGG_XLOCK_ASSERT(sc);
362 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
365 if (sc->sc_ifflags & IFF_DEBUG)
366 if_printf(sc->sc_ifp, "using proto %u\n", pr);
368 if (lagg_protos[pr].pr_attach != NULL)
369 lagg_protos[pr].pr_attach(sc);
374 lagg_proto_detach(struct lagg_softc *sc)
378 LAGG_XLOCK_ASSERT(sc);
380 sc->sc_proto = LAGG_PROTO_NONE;
382 if (lagg_protos[pr].pr_detach != NULL)
383 lagg_protos[pr].pr_detach(sc);
387 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
390 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
393 static inline struct mbuf *
394 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
397 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
401 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
404 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
407 return (lagg_protos[sc->sc_proto].pr_addport(lp));
411 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
414 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
415 lagg_protos[sc->sc_proto].pr_delport(lp);
419 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
422 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
423 lagg_protos[sc->sc_proto].pr_linkstate(lp);
427 lagg_proto_init(struct lagg_softc *sc)
430 if (lagg_protos[sc->sc_proto].pr_init != NULL)
431 lagg_protos[sc->sc_proto].pr_init(sc);
435 lagg_proto_stop(struct lagg_softc *sc)
438 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
439 lagg_protos[sc->sc_proto].pr_stop(sc);
443 lagg_proto_lladdr(struct lagg_softc *sc)
446 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
447 lagg_protos[sc->sc_proto].pr_lladdr(sc);
451 lagg_proto_request(struct lagg_softc *sc, void *v)
454 if (lagg_protos[sc->sc_proto].pr_request != NULL)
455 lagg_protos[sc->sc_proto].pr_request(sc, v);
459 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
462 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
463 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
467 * This routine is run via an vlan
471 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
473 struct lagg_softc *sc = ifp->if_softc;
474 struct lagg_port *lp;
476 if (ifp->if_softc != arg) /* Not our event */
480 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
481 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
486 * This routine is run via an vlan
490 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
492 struct lagg_softc *sc = ifp->if_softc;
493 struct lagg_port *lp;
495 if (ifp->if_softc != arg) /* Not our event */
499 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
500 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
505 lagg_clone_create(struct if_clone *ifc, char *name, size_t len,
506 struct ifc_data *ifd, struct ifnet **ifpp)
508 struct iflaggparam iflp;
509 struct lagg_softc *sc;
513 static const uint8_t eaddr[LAGG_ADDR_LEN];
515 if (ifd->params != NULL) {
516 error = ifc_copyin(ifd, &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, ifd->unit);
568 ifp->if_transmit = lagg_transmit_ethernet;
571 if_initname(ifp, laggname, ifd->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_ratelimit_query = lagg_ratelimit_query;
587 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
590 * Attach as an ordinary ethernet device, children will be attached
591 * as special device IFT_IEEE8023ADLAG or IFT_INFINIBANDLAG.
595 ether_ifattach(ifp, eaddr);
598 infiniband_ifattach(ifp, eaddr, sc->sc_bcast_addr);
604 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
605 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
606 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
607 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
609 /* Insert into the global list of laggs */
611 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
620 lagg_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
622 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
623 struct lagg_port *lp;
626 sc->sc_destroying = 1;
628 ifp->if_flags &= ~IFF_UP;
630 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
631 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
633 /* Shutdown and remove lagg ports */
634 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
635 lagg_port_destroy(lp, 1);
637 /* Unhook the aggregation protocol */
638 lagg_proto_detach(sc);
641 switch (ifp->if_type) {
643 ifmedia_removeall(&sc->sc_media);
647 infiniband_ifdetach(ifp);
655 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
658 mtx_destroy(&sc->sc_mtx);
666 lagg_capabilities(struct lagg_softc *sc)
668 struct lagg_port *lp;
669 int cap, cap2, ena, ena2, pena, pena2;
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 ena2 &= lp->lp_ifp->if_capenable2;
681 if (CK_SLIST_FIRST(&sc->sc_ports) == NULL)
685 * Apply common enabled capabilities back to the lagg ports.
686 * May require several iterations if they are dependent.
691 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
692 lagg_setcaps(lp, ena, ena2);
693 ena &= lp->lp_ifp->if_capenable;
694 ena2 &= lp->lp_ifp->if_capenable2;
696 } while (pena != ena || pena2 != ena2);
698 /* Get other capabilities from the lagg ports */
701 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
702 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
703 cap &= lp->lp_ifp->if_capabilities;
704 cap2 &= lp->lp_ifp->if_capabilities2;
705 hwa &= lp->lp_ifp->if_hwassist;
706 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
708 if (CK_SLIST_FIRST(&sc->sc_ports) == NULL)
709 cap = cap2 = hwa = 0;
711 if (sc->sc_ifp->if_capabilities != cap ||
712 sc->sc_ifp->if_capenable != ena ||
713 sc->sc_ifp->if_capenable2 != ena2 ||
714 sc->sc_ifp->if_hwassist != hwa ||
715 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
716 sc->sc_ifp->if_capabilities = cap;
717 sc->sc_ifp->if_capabilities2 = cap2;
718 sc->sc_ifp->if_capenable = ena;
719 sc->sc_ifp->if_capenable2 = ena2;
720 sc->sc_ifp->if_hwassist = hwa;
721 getmicrotime(&sc->sc_ifp->if_lastchange);
723 if (sc->sc_ifflags & IFF_DEBUG)
724 if_printf(sc->sc_ifp,
725 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
730 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
732 struct lagg_softc *sc_ptr;
733 struct lagg_port *lp, *tlp;
735 int error, i, oldmtu;
739 LAGG_XLOCK_ASSERT(sc);
741 if (sc->sc_ifp == ifp) {
742 if_printf(sc->sc_ifp,
743 "cannot add a lagg to itself as a port\n");
747 if (sc->sc_destroying == 1)
750 /* Limit the maximal number of lagg ports */
751 if (sc->sc_count >= LAGG_MAX_PORTS)
754 /* Check if port has already been associated to a lagg */
755 if (ifp->if_lagg != NULL) {
756 /* Port is already in the current lagg? */
757 lp = (struct lagg_port *)ifp->if_lagg;
758 if (lp->lp_softc == sc)
763 switch (sc->sc_ifp->if_type) {
765 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
766 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
767 return (EPROTONOSUPPORT);
768 if_type = IFT_IEEE8023ADLAG;
771 /* XXX Disallow non-infiniband interfaces */
772 if (ifp->if_type != IFT_INFINIBAND)
773 return (EPROTONOSUPPORT);
774 if_type = IFT_INFINIBANDLAG;
780 /* Allow the first Ethernet member to define the MTU */
782 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
783 sc->sc_ifp->if_mtu = ifp->if_mtu;
784 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
785 if (ifp->if_ioctl == NULL) {
786 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
790 oldmtu = ifp->if_mtu;
791 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
792 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
793 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
795 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
799 ifr.ifr_mtu = oldmtu;
802 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK | M_ZERO);
805 /* Check if port is a stacked lagg */
807 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
808 if (ifp == sc_ptr->sc_ifp) {
812 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
815 /* XXX disable stacking for the moment, its untested */
816 #ifdef LAGG_PORT_STACKING
817 lp->lp_flags |= LAGG_PORT_STACK;
818 if (lagg_port_checkstacking(sc_ptr) >=
823 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
835 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ifp->if_addrlen);
836 lp->lp_ifcapenable = ifp->if_capenable;
837 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
838 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
839 lagg_proto_lladdr(sc);
840 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
842 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
844 lagg_setflags(lp, 1);
846 if (CK_SLIST_EMPTY(&sc->sc_ports))
849 /* Change the interface type */
850 lp->lp_iftype = ifp->if_type;
851 ifp->if_type = if_type;
853 lp->lp_ioctl = ifp->if_ioctl;
854 ifp->if_ioctl = lagg_port_ioctl;
855 lp->lp_output = ifp->if_output;
856 ifp->if_output = lagg_port_output;
858 /* Read port counters */
859 pval = lp->port_counters.val;
860 for (i = 0; i < IFCOUNTERS; i++, pval++)
861 *pval = ifp->if_get_counter(ifp, i);
864 * Insert into the list of ports.
865 * Keep ports sorted by if_index. It is handy, when configuration
866 * is predictable and `ifconfig laggN create ...` command
867 * will lead to the same result each time.
869 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
870 if (tlp->lp_ifp->if_index < ifp->if_index && (
871 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
872 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
877 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
879 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
884 if ((error = lagg_proto_addport(sc, lp)) != 0) {
885 /* Remove the port, without calling pr_delport. */
886 lagg_port_destroy(lp, 0);
888 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
892 /* Update lagg capabilities */
893 lagg_capabilities(sc);
899 #ifdef LAGG_PORT_STACKING
901 lagg_port_checkstacking(struct lagg_softc *sc)
903 struct lagg_softc *sc_ptr;
904 struct lagg_port *lp;
907 LAGG_SXLOCK_ASSERT(sc);
908 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
909 if (lp->lp_flags & LAGG_PORT_STACK) {
910 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
911 m = MAX(m, lagg_port_checkstacking(sc_ptr));
920 lagg_port_destroy_cb(epoch_context_t ec)
922 struct lagg_port *lp;
925 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
933 lagg_port_destroy(struct lagg_port *lp, int rundelport)
935 struct lagg_softc *sc = lp->lp_softc;
936 struct lagg_port *lp_ptr, *lp0;
937 struct ifnet *ifp = lp->lp_ifp;
938 uint64_t *pval, vdiff;
941 LAGG_XLOCK_ASSERT(sc);
944 lagg_proto_delport(sc, lp);
946 if (lp->lp_detaching == 0)
949 /* Restore interface */
950 ifp->if_type = lp->lp_iftype;
951 ifp->if_ioctl = lp->lp_ioctl;
952 ifp->if_output = lp->lp_output;
955 /* Update detached port counters */
956 pval = lp->port_counters.val;
957 for (i = 0; i < IFCOUNTERS; i++, pval++) {
958 vdiff = ifp->if_get_counter(ifp, i) - *pval;
959 sc->detached_counters.val[i] += vdiff;
962 /* Finally, remove the port from the lagg */
963 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
966 /* Update the primary interface */
967 if (lp == sc->sc_primary) {
968 uint8_t lladdr[LAGG_ADDR_LEN];
970 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
971 bzero(&lladdr, LAGG_ADDR_LEN);
973 bcopy(lp0->lp_lladdr, lladdr, LAGG_ADDR_LEN);
974 sc->sc_primary = lp0;
975 if (sc->sc_destroying == 0) {
976 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), sc->sc_ifp->if_addrlen);
977 lagg_proto_lladdr(sc);
978 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
981 * Update lladdr for each port (new primary needs update
982 * as well, to switch from old lladdr to its 'real' one).
983 * We can skip this if the lagg is being destroyed.
985 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
986 if_setlladdr(lp_ptr->lp_ifp, lladdr,
987 lp_ptr->lp_ifp->if_addrlen);
992 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
994 if (lp->lp_detaching == 0) {
995 lagg_setflags(lp, 0);
996 lagg_setcaps(lp, lp->lp_ifcapenable, lp->lp_ifcapenable2);
997 if_setlladdr(ifp, lp->lp_lladdr, ifp->if_addrlen);
1001 * free port and release it's ifnet reference after a grace period has
1004 NET_EPOCH_CALL(lagg_port_destroy_cb, &lp->lp_epoch_ctx);
1005 /* Update lagg capabilities */
1006 lagg_capabilities(sc);
1013 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1015 struct epoch_tracker et;
1016 struct lagg_reqport *rp = (struct lagg_reqport *)data;
1017 struct lagg_softc *sc;
1018 struct lagg_port *lp = NULL;
1021 /* Should be checked by the caller */
1022 switch (ifp->if_type) {
1023 case IFT_IEEE8023ADLAG:
1024 case IFT_INFINIBANDLAG:
1025 if ((lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
1034 if (rp->rp_portname[0] == '\0' ||
1035 ifunit(rp->rp_portname) != ifp) {
1040 NET_EPOCH_ENTER(et);
1041 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
1047 lagg_port2req(lp, rp);
1053 if (lp->lp_ioctl == NULL) {
1057 error = (*lp->lp_ioctl)(ifp, cmd, data);
1061 /* Update lagg interface capabilities */
1063 lagg_capabilities(sc);
1065 VLAN_CAPABILITIES(sc->sc_ifp);
1069 /* Do not allow the MTU to be changed once joined */
1080 if (lp != NULL && lp->lp_ioctl != NULL)
1081 return ((*lp->lp_ioctl)(ifp, cmd, data));
1087 * Requests counter @cnt data.
1089 * Counter value is calculated the following way:
1090 * 1) for each port, sum difference between current and "initial" measurements.
1091 * 2) add lagg logical interface counters.
1092 * 3) add data from detached_counters array.
1094 * We also do the following things on ports attach/detach:
1095 * 1) On port attach we store all counters it has into port_counter array.
1096 * 2) On port detach we add the different between "initial" and
1097 * current counters data to detached_counters array.
1100 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1102 struct epoch_tracker et;
1103 struct lagg_softc *sc;
1104 struct lagg_port *lp;
1105 struct ifnet *lpifp;
1106 uint64_t newval, oldval, vsum;
1108 /* Revise this when we've got non-generic counters. */
1109 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1111 sc = (struct lagg_softc *)ifp->if_softc;
1114 NET_EPOCH_ENTER(et);
1115 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1116 /* Saved attached value */
1117 oldval = lp->port_counters.val[cnt];
1120 newval = lpifp->if_get_counter(lpifp, cnt);
1121 /* Calculate diff and save new */
1122 vsum += newval - oldval;
1127 * Add counter data which might be added by upper
1128 * layer protocols operating on logical interface.
1130 vsum += if_get_counter_default(ifp, cnt);
1133 * Add counter data from detached ports counters
1135 vsum += sc->detached_counters.val[cnt];
1141 * For direct output to child ports.
1144 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1145 const struct sockaddr *dst, struct route *ro)
1147 struct lagg_port *lp = ifp->if_lagg;
1149 switch (dst->sa_family) {
1150 case pseudo_AF_HDRCMPLT:
1153 return ((*lp->lp_output)(ifp, m, dst, ro));
1156 /* drop any other frames */
1162 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1164 struct lagg_port *lp;
1165 struct lagg_softc *sc;
1167 if ((lp = ifp->if_lagg) == NULL)
1169 /* If the ifnet is just being renamed, don't do anything. */
1170 if (ifp->if_flags & IFF_RENAMING)
1176 lp->lp_detaching = 1;
1177 lagg_port_destroy(lp, 1);
1179 VLAN_CAPABILITIES(sc->sc_ifp);
1183 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1185 struct lagg_softc *sc = lp->lp_softc;
1187 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1188 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1189 rp->rp_prio = lp->lp_prio;
1190 rp->rp_flags = lp->lp_flags;
1191 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1193 /* Add protocol specific flags */
1194 switch (sc->sc_proto) {
1195 case LAGG_PROTO_FAILOVER:
1196 if (lp == sc->sc_primary)
1197 rp->rp_flags |= LAGG_PORT_MASTER;
1198 if (lp == lagg_link_active(sc, sc->sc_primary))
1199 rp->rp_flags |= LAGG_PORT_ACTIVE;
1202 case LAGG_PROTO_ROUNDROBIN:
1203 case LAGG_PROTO_LOADBALANCE:
1204 case LAGG_PROTO_BROADCAST:
1205 if (LAGG_PORTACTIVE(lp))
1206 rp->rp_flags |= LAGG_PORT_ACTIVE;
1209 case LAGG_PROTO_LACP:
1210 /* LACP has a different definition of active */
1211 if (lacp_isactive(lp))
1212 rp->rp_flags |= LAGG_PORT_ACTIVE;
1213 if (lacp_iscollecting(lp))
1214 rp->rp_flags |= LAGG_PORT_COLLECTING;
1215 if (lacp_isdistributing(lp))
1216 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1223 lagg_watchdog_infiniband(void *arg)
1225 struct epoch_tracker et;
1226 struct lagg_softc *sc;
1227 struct lagg_port *lp;
1229 struct ifnet *lp_ifp;
1234 * Because infiniband nodes have a fixed MAC address, which is
1235 * generated by the so-called GID, we need to regularly update
1236 * the link level address of the parent lagg<N> device when
1237 * the active port changes. Possibly we could piggy-back on
1238 * link up/down events aswell, but using a timer also provides
1239 * a guarantee against too frequent events. This operation
1240 * does not have to be atomic.
1242 NET_EPOCH_ENTER(et);
1243 lp = lagg_link_active(sc, sc->sc_primary);
1246 lp_ifp = lp->lp_ifp;
1248 if (ifp != NULL && lp_ifp != NULL &&
1249 (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen) != 0 ||
1250 memcmp(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen) != 0)) {
1251 memcpy(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen);
1252 memcpy(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen);
1254 CURVNET_SET(ifp->if_vnet);
1255 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1261 callout_reset(&sc->sc_watchdog, hz, &lagg_watchdog_infiniband, arg);
1265 lagg_init(void *xsc)
1267 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1268 struct ifnet *ifp = sc->sc_ifp;
1269 struct lagg_port *lp;
1272 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1277 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1280 * Update the port lladdrs if needed.
1281 * This might be if_setlladdr() notification
1282 * that lladdr has been changed.
1284 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1285 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1286 ifp->if_addrlen) != 0)
1287 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1290 lagg_proto_init(sc);
1292 if (ifp->if_type == IFT_INFINIBAND) {
1293 mtx_lock(&sc->sc_mtx);
1294 lagg_watchdog_infiniband(sc);
1295 mtx_unlock(&sc->sc_mtx);
1302 lagg_stop(struct lagg_softc *sc)
1304 struct ifnet *ifp = sc->sc_ifp;
1306 LAGG_XLOCK_ASSERT(sc);
1308 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1311 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1313 lagg_proto_stop(sc);
1315 mtx_lock(&sc->sc_mtx);
1316 callout_stop(&sc->sc_watchdog);
1317 mtx_unlock(&sc->sc_mtx);
1319 callout_drain(&sc->sc_watchdog);
1323 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1325 struct epoch_tracker et;
1326 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1327 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1328 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1329 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1330 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1331 struct ifreq *ifr = (struct ifreq *)data;
1332 struct lagg_port *lp;
1334 struct thread *td = curthread;
1336 int count, buflen, len, error = 0, oldmtu;
1338 bzero(&rpbuf, sizeof(rpbuf));
1340 /* XXX: This can race with lagg_clone_destroy. */
1345 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1346 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1347 ra->ra_proto = sc->sc_proto;
1348 lagg_proto_request(sc, &ra->ra_psc);
1351 len = min(ra->ra_size, buflen);
1352 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1353 if (len < sizeof(rpbuf))
1356 lagg_port2req(lp, &rpbuf);
1357 memcpy(buf, &rpbuf, sizeof(rpbuf));
1359 buf += sizeof(rpbuf);
1360 len -= sizeof(rpbuf);
1363 ra->ra_ports = count;
1364 ra->ra_size = count * sizeof(rpbuf);
1365 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1366 free(outbuf, M_TEMP);
1369 error = priv_check(td, PRIV_NET_LAGG);
1372 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1373 error = EPROTONOSUPPORT;
1376 /* Infiniband only supports the failover protocol. */
1377 if (ra->ra_proto != LAGG_PROTO_FAILOVER &&
1378 ifp->if_type == IFT_INFINIBAND) {
1379 error = EPROTONOSUPPORT;
1383 lagg_proto_detach(sc);
1384 lagg_proto_attach(sc, ra->ra_proto);
1389 ro->ro_opts = sc->sc_opts;
1390 if (sc->sc_proto == LAGG_PROTO_LACP) {
1391 struct lacp_softc *lsc;
1393 lsc = (struct lacp_softc *)sc->sc_psc;
1394 if (lsc->lsc_debug.lsc_tx_test != 0)
1395 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1396 if (lsc->lsc_debug.lsc_rx_test != 0)
1397 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1398 if (lsc->lsc_strict_mode != 0)
1399 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1400 if (lsc->lsc_fast_timeout != 0)
1401 ro->ro_opts |= LAGG_OPT_LACP_FAST_TIMO;
1403 ro->ro_active = sc->sc_active;
1406 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1407 ro->ro_active += LAGG_PORTACTIVE(lp);
1409 ro->ro_bkt = sc->sc_stride;
1410 ro->ro_flapping = sc->sc_flapping;
1411 ro->ro_flowid_shift = sc->flowid_shift;
1415 error = priv_check(td, PRIV_NET_LAGG);
1420 * The stride option was added without defining a corresponding
1421 * LAGG_OPT flag, so handle a non-zero value before checking
1422 * anything else to preserve compatibility.
1425 if (ro->ro_opts == 0 && ro->ro_bkt != 0) {
1426 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN) {
1431 sc->sc_stride = ro->ro_bkt;
1433 if (ro->ro_opts == 0) {
1439 * Set options. LACP options are stored in sc->sc_psc,
1444 switch (ro->ro_opts) {
1445 case LAGG_OPT_USE_FLOWID:
1446 case -LAGG_OPT_USE_FLOWID:
1447 case LAGG_OPT_USE_NUMA:
1448 case -LAGG_OPT_USE_NUMA:
1449 case LAGG_OPT_FLOWIDSHIFT:
1450 case LAGG_OPT_RR_LIMIT:
1454 case LAGG_OPT_LACP_TXTEST:
1455 case -LAGG_OPT_LACP_TXTEST:
1456 case LAGG_OPT_LACP_RXTEST:
1457 case -LAGG_OPT_LACP_RXTEST:
1458 case LAGG_OPT_LACP_STRICT:
1459 case -LAGG_OPT_LACP_STRICT:
1460 case LAGG_OPT_LACP_FAST_TIMO:
1461 case -LAGG_OPT_LACP_FAST_TIMO:
1470 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1471 /* Invalid combination of options specified. */
1474 break; /* Return from SIOCSLAGGOPTS. */
1478 * Store new options into sc->sc_opts except for
1479 * FLOWIDSHIFT, RR and LACP options.
1482 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1483 sc->flowid_shift = ro->ro_flowid_shift;
1484 else if (ro->ro_opts == LAGG_OPT_RR_LIMIT) {
1485 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN ||
1491 sc->sc_stride = ro->ro_bkt;
1492 } else if (ro->ro_opts > 0)
1493 sc->sc_opts |= ro->ro_opts;
1495 sc->sc_opts &= ~ro->ro_opts;
1497 struct lacp_softc *lsc;
1498 struct lacp_port *lp;
1500 lsc = (struct lacp_softc *)sc->sc_psc;
1502 switch (ro->ro_opts) {
1503 case LAGG_OPT_LACP_TXTEST:
1504 lsc->lsc_debug.lsc_tx_test = 1;
1506 case -LAGG_OPT_LACP_TXTEST:
1507 lsc->lsc_debug.lsc_tx_test = 0;
1509 case LAGG_OPT_LACP_RXTEST:
1510 lsc->lsc_debug.lsc_rx_test = 1;
1512 case -LAGG_OPT_LACP_RXTEST:
1513 lsc->lsc_debug.lsc_rx_test = 0;
1515 case LAGG_OPT_LACP_STRICT:
1516 lsc->lsc_strict_mode = 1;
1518 case -LAGG_OPT_LACP_STRICT:
1519 lsc->lsc_strict_mode = 0;
1521 case LAGG_OPT_LACP_FAST_TIMO:
1523 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1524 lp->lp_state |= LACP_STATE_TIMEOUT;
1526 lsc->lsc_fast_timeout = 1;
1528 case -LAGG_OPT_LACP_FAST_TIMO:
1530 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1531 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1533 lsc->lsc_fast_timeout = 0;
1539 case SIOCGLAGGFLAGS:
1542 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1543 rf->rf_flags |= LAGG_F_HASHL2;
1544 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1545 rf->rf_flags |= LAGG_F_HASHL3;
1546 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1547 rf->rf_flags |= LAGG_F_HASHL4;
1551 error = priv_check(td, PRIV_NET_LAGG);
1554 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1560 if (rf->rf_flags & LAGG_F_HASHL2)
1561 sc->sc_flags |= MBUF_HASHFLAG_L2;
1562 if (rf->rf_flags & LAGG_F_HASHL3)
1563 sc->sc_flags |= MBUF_HASHFLAG_L3;
1564 if (rf->rf_flags & LAGG_F_HASHL4)
1565 sc->sc_flags |= MBUF_HASHFLAG_L4;
1569 if (rp->rp_portname[0] == '\0' ||
1570 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1575 NET_EPOCH_ENTER(et);
1576 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1577 lp->lp_softc != sc) {
1584 lagg_port2req(lp, rp);
1589 error = priv_check(td, PRIV_NET_LAGG);
1592 if (rp->rp_portname[0] == '\0' ||
1593 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1599 * A laggport interface should not have inet6 address
1600 * because two interfaces with a valid link-local
1601 * scope zone must not be merged in any form. This
1602 * restriction is needed to prevent violation of
1603 * link-local scope zone. Attempts to add a laggport
1604 * interface which has inet6 addresses triggers
1605 * removal of all inet6 addresses on the member
1608 if (in6ifa_llaonifp(tpif)) {
1610 if_printf(sc->sc_ifp,
1611 "IPv6 addresses on %s have been removed "
1612 "before adding it as a member to prevent "
1613 "IPv6 address scope violation.\n",
1617 oldmtu = ifp->if_mtu;
1619 error = lagg_port_create(sc, tpif);
1624 * LAGG MTU may change during addition of the first port.
1625 * If it did, do network layer specific procedure.
1627 if (ifp->if_mtu != oldmtu)
1630 VLAN_CAPABILITIES(ifp);
1632 case SIOCSLAGGDELPORT:
1633 error = priv_check(td, PRIV_NET_LAGG);
1636 if (rp->rp_portname[0] == '\0' ||
1637 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1643 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1644 lp->lp_softc != sc) {
1651 error = lagg_port_destroy(lp, 1);
1654 VLAN_CAPABILITIES(ifp);
1657 /* Set flags on ports too */
1659 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1660 lagg_setflags(lp, 1);
1663 if (!(ifp->if_flags & IFF_UP) &&
1664 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1666 * If interface is marked down and it is running,
1667 * then stop and disable it.
1671 } else if ((ifp->if_flags & IFF_UP) &&
1672 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1674 * If interface is marked up and it is stopped, then
1678 (*ifp->if_init)(sc);
1685 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1694 if (ifp->if_type == IFT_INFINIBAND)
1697 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1703 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1704 if (lp->lp_ioctl != NULL)
1705 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1707 lagg_capabilities(sc);
1709 VLAN_CAPABILITIES(ifp);
1719 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1720 if (lp->lp_ioctl != NULL)
1721 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1726 "failed to change MTU to %d on port %s, "
1727 "reverting all ports to original MTU (%d)\n",
1728 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1733 ifp->if_mtu = ifr->ifr_mtu;
1735 /* set every port back to the original MTU */
1736 ifr->ifr_mtu = ifp->if_mtu;
1737 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1738 if (lp->lp_ioctl != NULL)
1739 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1742 lagg_capabilities(sc);
1744 VLAN_CAPABILITIES(ifp);
1748 error = ether_ioctl(ifp, cmd, data);
1754 #if defined(KERN_TLS) || defined(RATELIMIT)
1756 static const struct if_snd_tag_sw lagg_snd_tag_ul_sw = {
1757 .snd_tag_modify = lagg_snd_tag_modify,
1758 .snd_tag_query = lagg_snd_tag_query,
1759 .snd_tag_free = lagg_snd_tag_free,
1760 .next_snd_tag = lagg_next_snd_tag,
1761 .type = IF_SND_TAG_TYPE_UNLIMITED
1764 static const struct if_snd_tag_sw lagg_snd_tag_rl_sw = {
1765 .snd_tag_modify = lagg_snd_tag_modify,
1766 .snd_tag_query = lagg_snd_tag_query,
1767 .snd_tag_free = lagg_snd_tag_free,
1768 .next_snd_tag = lagg_next_snd_tag,
1769 .type = IF_SND_TAG_TYPE_RATE_LIMIT
1774 static const struct if_snd_tag_sw lagg_snd_tag_tls_sw = {
1775 .snd_tag_modify = lagg_snd_tag_modify,
1776 .snd_tag_query = lagg_snd_tag_query,
1777 .snd_tag_free = lagg_snd_tag_free,
1778 .next_snd_tag = lagg_next_snd_tag,
1779 .type = IF_SND_TAG_TYPE_TLS
1783 static const struct if_snd_tag_sw lagg_snd_tag_tls_rl_sw = {
1784 .snd_tag_modify = lagg_snd_tag_modify,
1785 .snd_tag_query = lagg_snd_tag_query,
1786 .snd_tag_free = lagg_snd_tag_free,
1787 .next_snd_tag = lagg_next_snd_tag,
1788 .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
1793 static inline struct lagg_snd_tag *
1794 mst_to_lst(struct m_snd_tag *mst)
1797 return (__containerof(mst, struct lagg_snd_tag, com));
1801 * Look up the port used by a specific flow. This only works for lagg
1802 * protocols with deterministic port mappings (e.g. not roundrobin).
1803 * In addition protocols which use a hash to map flows to ports must
1804 * be configured to use the mbuf flowid rather than hashing packet
1807 static struct lagg_port *
1808 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype,
1809 uint8_t numa_domain)
1811 struct lagg_softc *sc;
1812 struct lagg_port *lp;
1819 switch (sc->sc_proto) {
1820 case LAGG_PROTO_FAILOVER:
1821 return (lagg_link_active(sc, sc->sc_primary));
1822 case LAGG_PROTO_LOADBALANCE:
1823 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1824 flowtype == M_HASHTYPE_NONE)
1826 p = flowid >> sc->flowid_shift;
1828 lb = (struct lagg_lb *)sc->sc_psc;
1829 lp = lb->lb_ports[p];
1830 return (lagg_link_active(sc, lp));
1831 case LAGG_PROTO_LACP:
1832 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1833 flowtype == M_HASHTYPE_NONE)
1835 hash = flowid >> sc->flowid_shift;
1836 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, &err));
1843 lagg_snd_tag_alloc(struct ifnet *ifp,
1844 union if_snd_tag_alloc_params *params,
1845 struct m_snd_tag **ppmt)
1847 struct epoch_tracker et;
1848 const struct if_snd_tag_sw *sw;
1849 struct lagg_snd_tag *lst;
1850 struct lagg_port *lp;
1851 struct ifnet *lp_ifp;
1852 struct m_snd_tag *mst;
1855 switch (params->hdr.type) {
1857 case IF_SND_TAG_TYPE_UNLIMITED:
1858 sw = &lagg_snd_tag_ul_sw;
1860 case IF_SND_TAG_TYPE_RATE_LIMIT:
1861 sw = &lagg_snd_tag_rl_sw;
1865 case IF_SND_TAG_TYPE_TLS:
1866 sw = &lagg_snd_tag_tls_sw;
1868 case IF_SND_TAG_TYPE_TLS_RX:
1869 /* Return tag from port interface directly. */
1873 case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
1874 sw = &lagg_snd_tag_tls_rl_sw;
1879 return (EOPNOTSUPP);
1882 NET_EPOCH_ENTER(et);
1883 lp = lookup_snd_tag_port(ifp, params->hdr.flowid,
1884 params->hdr.flowtype, params->hdr.numa_domain);
1887 return (EOPNOTSUPP);
1889 if (lp->lp_ifp == NULL) {
1891 return (EOPNOTSUPP);
1893 lp_ifp = lp->lp_ifp;
1898 lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1906 error = m_snd_tag_alloc(lp_ifp, params, &mst);
1914 m_snd_tag_init(&lst->com, ifp, sw);
1924 static struct m_snd_tag *
1925 lagg_next_snd_tag(struct m_snd_tag *mst)
1927 struct lagg_snd_tag *lst;
1929 lst = mst_to_lst(mst);
1934 lagg_snd_tag_modify(struct m_snd_tag *mst,
1935 union if_snd_tag_modify_params *params)
1937 struct lagg_snd_tag *lst;
1939 lst = mst_to_lst(mst);
1940 return (lst->tag->sw->snd_tag_modify(lst->tag, params));
1944 lagg_snd_tag_query(struct m_snd_tag *mst,
1945 union if_snd_tag_query_params *params)
1947 struct lagg_snd_tag *lst;
1949 lst = mst_to_lst(mst);
1950 return (lst->tag->sw->snd_tag_query(lst->tag, params));
1954 lagg_snd_tag_free(struct m_snd_tag *mst)
1956 struct lagg_snd_tag *lst;
1958 lst = mst_to_lst(mst);
1959 m_snd_tag_rele(lst->tag);
1964 lagg_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
1967 * For lagg, we have an indirect
1968 * interface. The caller needs to
1969 * get a ratelimit tag on the actual
1970 * interface the flow will go on.
1972 q->rate_table = NULL;
1973 q->flags = RT_IS_INDIRECT;
1975 q->number_of_rates = 0;
1980 lagg_setmulti(struct lagg_port *lp)
1982 struct lagg_softc *sc = lp->lp_softc;
1983 struct ifnet *ifp = lp->lp_ifp;
1984 struct ifnet *scifp = sc->sc_ifp;
1986 struct ifmultiaddr *ifma;
1989 IF_ADDR_WLOCK(scifp);
1990 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1991 if (ifma->ifma_addr->sa_family != AF_LINK)
1993 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1995 IF_ADDR_WUNLOCK(scifp);
1998 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
1999 mc->mc_addr.sdl_index = ifp->if_index;
2001 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
2003 IF_ADDR_WUNLOCK(scifp);
2004 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
2005 error = if_addmulti(ifp,
2006 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
2014 lagg_clrmulti(struct lagg_port *lp)
2018 LAGG_XLOCK_ASSERT(lp->lp_softc);
2019 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
2020 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
2021 if (mc->mc_ifma && lp->lp_detaching == 0)
2022 if_delmulti_ifma(mc->mc_ifma);
2029 lagg_setcaps(struct lagg_port *lp, int cap, int cap2)
2032 struct siocsifcapnv_driver_data drv_ioctl_data;
2034 if (lp->lp_ifp->if_capenable == cap &&
2035 lp->lp_ifp->if_capenable2 == cap2)
2037 if (lp->lp_ioctl == NULL)
2040 if ((lp->lp_ifp->if_capabilities & IFCAP_NV) != 0) {
2041 drv_ioctl_data.reqcap = cap;
2042 drv_ioctl_data.reqcap2 = cap2;
2043 drv_ioctl_data.nvcap = NULL;
2044 (*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAPNV,
2045 (caddr_t)&drv_ioctl_data);
2047 ifr.ifr_reqcap = cap;
2048 (*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr);
2052 /* Handle a ref counted flag that should be set on the lagg port as well */
2054 lagg_setflag(struct lagg_port *lp, int flag, int status,
2055 int (*func)(struct ifnet *, int))
2057 struct lagg_softc *sc = lp->lp_softc;
2058 struct ifnet *scifp = sc->sc_ifp;
2059 struct ifnet *ifp = lp->lp_ifp;
2062 LAGG_XLOCK_ASSERT(sc);
2064 status = status ? (scifp->if_flags & flag) : 0;
2065 /* Now "status" contains the flag value or 0 */
2068 * See if recorded ports status is different from what
2069 * we want it to be. If it is, flip it. We record ports
2070 * status in lp_ifflags so that we won't clear ports flag
2071 * we haven't set. In fact, we don't clear or set ports
2072 * flags directly, but get or release references to them.
2073 * That's why we can be sure that recorded flags still are
2074 * in accord with actual ports flags.
2076 if (status != (lp->lp_ifflags & flag)) {
2077 error = (*func)(ifp, status);
2080 lp->lp_ifflags &= ~flag;
2081 lp->lp_ifflags |= status;
2087 * Handle IFF_* flags that require certain changes on the lagg port
2088 * if "status" is true, update ports flags respective to the lagg
2089 * if "status" is false, forcedly clear the flags set on port.
2092 lagg_setflags(struct lagg_port *lp, int status)
2096 for (i = 0; lagg_pflags[i].flag; i++) {
2097 error = lagg_setflag(lp, lagg_pflags[i].flag,
2098 status, lagg_pflags[i].func);
2106 lagg_transmit_ethernet(struct ifnet *ifp, struct mbuf *m)
2108 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2111 #if defined(KERN_TLS) || defined(RATELIMIT)
2112 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2113 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2115 /* We need a Tx algorithm and at least one port */
2116 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2118 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2122 ETHER_BPF_MTAP(ifp, m);
2124 return (lagg_proto_start(sc, m));
2128 lagg_transmit_infiniband(struct ifnet *ifp, struct mbuf *m)
2130 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2133 #if defined(KERN_TLS) || defined(RATELIMIT)
2134 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2135 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2137 /* We need a Tx algorithm and at least one port */
2138 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2140 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2144 infiniband_bpf_mtap(ifp, m);
2146 return (lagg_proto_start(sc, m));
2150 * The ifp->if_qflush entry point for lagg(4) is no-op.
2153 lagg_qflush(struct ifnet *ifp __unused)
2157 static struct mbuf *
2158 lagg_input_ethernet(struct ifnet *ifp, struct mbuf *m)
2160 struct lagg_port *lp = ifp->if_lagg;
2161 struct lagg_softc *sc = lp->lp_softc;
2162 struct ifnet *scifp = sc->sc_ifp;
2165 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2166 lp->lp_detaching != 0 ||
2167 sc->sc_proto == LAGG_PROTO_NONE) {
2172 m = lagg_proto_input(sc, lp, m);
2174 ETHER_BPF_MTAP(scifp, m);
2176 if ((scifp->if_flags & IFF_MONITOR) != 0) {
2183 if (m != NULL && scifp->if_capenable & IFCAP_NETMAP) {
2184 scifp->if_input(scifp, m);
2187 #endif /* DEV_NETMAP */
2192 static struct mbuf *
2193 lagg_input_infiniband(struct ifnet *ifp, struct mbuf *m)
2195 struct lagg_port *lp = ifp->if_lagg;
2196 struct lagg_softc *sc = lp->lp_softc;
2197 struct ifnet *scifp = sc->sc_ifp;
2200 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2201 lp->lp_detaching != 0 ||
2202 sc->sc_proto == LAGG_PROTO_NONE) {
2207 m = lagg_proto_input(sc, lp, m);
2209 infiniband_bpf_mtap(scifp, m);
2211 if ((scifp->if_flags & IFF_MONITOR) != 0) {
2221 lagg_media_change(struct ifnet *ifp)
2223 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2225 if (sc->sc_ifflags & IFF_DEBUG)
2226 printf("%s\n", __func__);
2233 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2235 struct epoch_tracker et;
2236 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2237 struct lagg_port *lp;
2239 imr->ifm_status = IFM_AVALID;
2240 imr->ifm_active = IFM_ETHER | IFM_AUTO;
2242 NET_EPOCH_ENTER(et);
2243 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2244 if (LAGG_PORTACTIVE(lp))
2245 imr->ifm_status |= IFM_ACTIVE;
2251 lagg_linkstate(struct lagg_softc *sc)
2253 struct epoch_tracker et;
2254 struct lagg_port *lp;
2255 int new_link = LINK_STATE_DOWN;
2258 LAGG_XLOCK_ASSERT(sc);
2260 /* LACP handles link state itself */
2261 if (sc->sc_proto == LAGG_PROTO_LACP)
2264 /* Our link is considered up if at least one of our ports is active */
2265 NET_EPOCH_ENTER(et);
2266 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2267 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
2268 new_link = LINK_STATE_UP;
2273 if_link_state_change(sc->sc_ifp, new_link);
2275 /* Update if_baudrate to reflect the max possible speed */
2276 switch (sc->sc_proto) {
2277 case LAGG_PROTO_FAILOVER:
2278 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
2279 sc->sc_primary->lp_ifp->if_baudrate : 0;
2281 case LAGG_PROTO_ROUNDROBIN:
2282 case LAGG_PROTO_LOADBALANCE:
2283 case LAGG_PROTO_BROADCAST:
2285 NET_EPOCH_ENTER(et);
2286 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2287 speed += lp->lp_ifp->if_baudrate;
2289 sc->sc_ifp->if_baudrate = speed;
2291 case LAGG_PROTO_LACP:
2292 /* LACP updates if_baudrate itself */
2298 lagg_port_state(struct ifnet *ifp, int state)
2300 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
2301 struct lagg_softc *sc = NULL;
2310 lagg_proto_linkstate(sc, lp);
2315 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
2317 struct lagg_port *lp_next, *rval = NULL;
2320 * Search a port which reports an active link state.
2325 * This is called with either in the network epoch
2326 * or with LAGG_XLOCK(sc) held.
2328 if (!in_epoch(net_epoch_preempt))
2329 LAGG_XLOCK_ASSERT(sc);
2334 if (LAGG_PORTACTIVE(lp)) {
2338 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
2339 LAGG_PORTACTIVE(lp_next)) {
2345 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2346 if (LAGG_PORTACTIVE(lp_next)) {
2355 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2358 #if defined(KERN_TLS) || defined(RATELIMIT)
2359 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2360 struct lagg_snd_tag *lst;
2361 struct m_snd_tag *mst;
2363 mst = m->m_pkthdr.snd_tag;
2364 lst = mst_to_lst(mst);
2365 if (lst->tag->ifp != ifp) {
2369 m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2370 m_snd_tag_rele(mst);
2373 return (ifp->if_transmit)(ifp, m);
2377 * Simple round robin aggregation
2380 lagg_rr_attach(struct lagg_softc *sc)
2387 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2389 struct lagg_port *lp;
2392 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2395 lp = CK_SLIST_FIRST(&sc->sc_ports);
2398 lp = CK_SLIST_NEXT(lp, lp_entries);
2401 * Check the port's link state. This will return the next active
2402 * port if the link is down or the port is NULL.
2404 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2405 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2411 return (lagg_enqueue(lp->lp_ifp, m));
2418 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2422 struct lagg_port *lp, *last = NULL;
2426 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2427 if (!LAGG_PORTACTIVE(lp))
2431 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2437 lagg_enqueue(last->lp_ifp, m0);
2443 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2447 if ((last = lagg_link_active(sc, last)) == NULL) {
2449 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2454 ret = lagg_enqueue(last->lp_ifp, m);
2456 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2465 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2467 struct lagg_port *lp;
2469 /* Use the master port if active or the next available port */
2470 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2471 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2477 return (lagg_enqueue(lp->lp_ifp, m));
2480 static struct mbuf *
2481 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2483 struct ifnet *ifp = sc->sc_ifp;
2484 struct lagg_port *tmp_tp;
2486 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2487 m->m_pkthdr.rcvif = ifp;
2491 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2492 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2494 * If tmp_tp is null, we've received a packet when all
2495 * our links are down. Weird, but process it anyways.
2497 if (tmp_tp == NULL || tmp_tp == lp) {
2498 m->m_pkthdr.rcvif = ifp;
2511 lagg_lb_attach(struct lagg_softc *sc)
2513 struct lagg_port *lp;
2516 LAGG_XLOCK_ASSERT(sc);
2517 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2518 lb->lb_key = m_ether_tcpip_hash_init();
2521 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2522 lagg_lb_port_create(lp);
2526 lagg_lb_detach(struct lagg_softc *sc)
2530 lb = (struct lagg_lb *)sc->sc_psc;
2536 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2538 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2539 struct lagg_port *lp_next;
2543 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2544 LAGG_XLOCK_ASSERT(sc);
2545 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2548 if (i >= LAGG_MAX_PORTS) {
2552 if (sc->sc_ifflags & IFF_DEBUG)
2553 printf("%s: port %s at index %d\n",
2554 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2555 lb->lb_ports[i++] = lp_next;
2562 lagg_lb_port_create(struct lagg_port *lp)
2564 struct lagg_softc *sc = lp->lp_softc;
2565 return (lagg_lb_porttable(sc, NULL));
2569 lagg_lb_port_destroy(struct lagg_port *lp)
2571 struct lagg_softc *sc = lp->lp_softc;
2572 lagg_lb_porttable(sc, lp);
2576 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2578 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2579 struct lagg_port *lp = NULL;
2582 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2583 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2584 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2586 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2588 lp = lb->lb_ports[p];
2591 * Check the port's link state. This will return the next active
2592 * port if the link is down or the port is NULL.
2594 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2595 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2601 return (lagg_enqueue(lp->lp_ifp, m));
2608 lagg_lacp_attach(struct lagg_softc *sc)
2610 struct lagg_port *lp;
2613 LAGG_XLOCK_ASSERT(sc);
2614 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2615 lacp_port_create(lp);
2619 lagg_lacp_detach(struct lagg_softc *sc)
2621 struct lagg_port *lp;
2624 LAGG_XLOCK_ASSERT(sc);
2625 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2626 lacp_port_destroy(lp);
2634 lagg_lacp_lladdr(struct lagg_softc *sc)
2636 struct lagg_port *lp;
2638 LAGG_SXLOCK_ASSERT(sc);
2640 /* purge all the lacp ports */
2641 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2642 lacp_port_destroy(lp);
2644 /* add them back in */
2645 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2646 lacp_port_create(lp);
2650 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2652 struct lagg_port *lp;
2655 lp = lacp_select_tx_port(sc, m, &err);
2657 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2663 return (lagg_enqueue(lp->lp_ifp, m));
2666 static struct mbuf *
2667 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2669 struct ifnet *ifp = sc->sc_ifp;
2670 struct ether_header *eh;
2673 eh = mtod(m, struct ether_header *);
2674 etype = ntohs(eh->ether_type);
2676 /* Tap off LACP control messages */
2677 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2678 m = lacp_input(lp, m);
2684 * If the port is not collecting or not in the active aggregator then
2687 if (!lacp_iscollecting(lp) || !lacp_isactive(lp)) {
2692 m->m_pkthdr.rcvif = ifp;
2697 static struct mbuf *
2698 lagg_default_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2700 struct ifnet *ifp = sc->sc_ifp;
2702 /* Just pass in the packet to our lagg device */
2703 m->m_pkthdr.rcvif = ifp;