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_ratelimit.h"
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35 #include <sys/sysctl.h>
36 #include <sys/module.h>
38 #include <sys/systm.h>
41 #include <sys/rmlock.h>
43 #include <sys/taskqueue.h>
44 #include <sys/eventhandler.h>
46 #include <net/ethernet.h>
48 #include <net/if_clone.h>
49 #include <net/if_arp.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
53 #include <net/if_var.h>
57 #if defined(INET) || defined(INET6)
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/if_ether.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/in6_ifattach.h>
72 #include <net/if_vlan_var.h>
73 #include <net/if_lagg.h>
74 #include <net/ieee8023ad_lacp.h>
76 #define LAGG_RLOCK() struct epoch_tracker lagg_et; epoch_enter_preempt(net_epoch_preempt, &lagg_et)
77 #define LAGG_RUNLOCK() epoch_exit_preempt(net_epoch_preempt, &lagg_et)
78 #define LAGG_RLOCK_ASSERT() MPASS(in_epoch(net_epoch_preempt))
79 #define LAGG_UNLOCK_ASSERT() MPASS(!in_epoch(net_epoch_preempt))
81 #define LAGG_SX_INIT(_sc) sx_init(&(_sc)->sc_sx, "if_lagg sx")
82 #define LAGG_SX_DESTROY(_sc) sx_destroy(&(_sc)->sc_sx)
83 #define LAGG_XLOCK(_sc) sx_xlock(&(_sc)->sc_sx)
84 #define LAGG_XUNLOCK(_sc) sx_xunlock(&(_sc)->sc_sx)
85 #define LAGG_SXLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_LOCKED)
86 #define LAGG_XLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
88 /* Special flags we should propagate to the lagg ports. */
91 int (*func)(struct ifnet *, int);
93 {IFF_PROMISC, ifpromisc},
94 {IFF_ALLMULTI, if_allmulti},
98 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
99 #define V_lagg_list VNET(lagg_list)
100 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
101 #define V_lagg_list_mtx VNET(lagg_list_mtx)
102 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
103 "if_lagg list", NULL, MTX_DEF)
104 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
105 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
106 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
107 eventhandler_tag lagg_detach_cookie = NULL;
109 static int lagg_clone_create(struct if_clone *, int, caddr_t);
110 static void lagg_clone_destroy(struct ifnet *);
111 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
112 #define V_lagg_cloner VNET(lagg_cloner)
113 static const char laggname[] = "lagg";
114 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
116 static void lagg_capabilities(struct lagg_softc *);
117 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
118 static int lagg_port_destroy(struct lagg_port *, int);
119 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
120 static void lagg_linkstate(struct lagg_softc *);
121 static void lagg_port_state(struct ifnet *, int);
122 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
123 static int lagg_port_output(struct ifnet *, struct mbuf *,
124 const struct sockaddr *, struct route *);
125 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
126 #ifdef LAGG_PORT_STACKING
127 static int lagg_port_checkstacking(struct lagg_softc *);
129 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
130 static void lagg_init(void *);
131 static void lagg_stop(struct lagg_softc *);
132 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
134 static int lagg_snd_tag_alloc(struct ifnet *,
135 union if_snd_tag_alloc_params *,
136 struct m_snd_tag **);
137 static void lagg_snd_tag_free(struct m_snd_tag *);
139 static int lagg_setmulti(struct lagg_port *);
140 static int lagg_clrmulti(struct lagg_port *);
141 static int lagg_setcaps(struct lagg_port *, int cap);
142 static int lagg_setflag(struct lagg_port *, int, int,
143 int (*func)(struct ifnet *, int));
144 static int lagg_setflags(struct lagg_port *, int status);
145 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
146 static int lagg_transmit(struct ifnet *, struct mbuf *);
147 static void lagg_qflush(struct ifnet *);
148 static int lagg_media_change(struct ifnet *);
149 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
150 static struct lagg_port *lagg_link_active(struct lagg_softc *,
153 /* Simple round robin */
154 static void lagg_rr_attach(struct lagg_softc *);
155 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
156 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
159 /* Active failover */
160 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
161 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
165 static void lagg_lb_attach(struct lagg_softc *);
166 static void lagg_lb_detach(struct lagg_softc *);
167 static int lagg_lb_port_create(struct lagg_port *);
168 static void lagg_lb_port_destroy(struct lagg_port *);
169 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
170 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
172 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
175 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
176 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
180 static void lagg_lacp_attach(struct lagg_softc *);
181 static void lagg_lacp_detach(struct lagg_softc *);
182 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
183 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
185 static void lagg_lacp_lladdr(struct lagg_softc *);
187 /* lagg protocol table */
188 static const struct lagg_proto {
190 void (*pr_attach)(struct lagg_softc *);
191 void (*pr_detach)(struct lagg_softc *);
192 int (*pr_start)(struct lagg_softc *, struct mbuf *);
193 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
195 int (*pr_addport)(struct lagg_port *);
196 void (*pr_delport)(struct lagg_port *);
197 void (*pr_linkstate)(struct lagg_port *);
198 void (*pr_init)(struct lagg_softc *);
199 void (*pr_stop)(struct lagg_softc *);
200 void (*pr_lladdr)(struct lagg_softc *);
201 void (*pr_request)(struct lagg_softc *, void *);
202 void (*pr_portreq)(struct lagg_port *, void *);
205 .pr_num = LAGG_PROTO_NONE
208 .pr_num = LAGG_PROTO_ROUNDROBIN,
209 .pr_attach = lagg_rr_attach,
210 .pr_start = lagg_rr_start,
211 .pr_input = lagg_rr_input,
214 .pr_num = LAGG_PROTO_FAILOVER,
215 .pr_start = lagg_fail_start,
216 .pr_input = lagg_fail_input,
219 .pr_num = LAGG_PROTO_LOADBALANCE,
220 .pr_attach = lagg_lb_attach,
221 .pr_detach = lagg_lb_detach,
222 .pr_start = lagg_lb_start,
223 .pr_input = lagg_lb_input,
224 .pr_addport = lagg_lb_port_create,
225 .pr_delport = lagg_lb_port_destroy,
228 .pr_num = LAGG_PROTO_LACP,
229 .pr_attach = lagg_lacp_attach,
230 .pr_detach = lagg_lacp_detach,
231 .pr_start = lagg_lacp_start,
232 .pr_input = lagg_lacp_input,
233 .pr_addport = lacp_port_create,
234 .pr_delport = lacp_port_destroy,
235 .pr_linkstate = lacp_linkstate,
236 .pr_init = lacp_init,
237 .pr_stop = lacp_stop,
238 .pr_lladdr = lagg_lacp_lladdr,
239 .pr_request = lacp_req,
240 .pr_portreq = lacp_portreq,
243 .pr_num = LAGG_PROTO_BROADCAST,
244 .pr_start = lagg_bcast_start,
245 .pr_input = lagg_bcast_input,
249 SYSCTL_DECL(_net_link);
250 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
253 /* Allow input on any failover links */
254 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
255 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
256 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
257 &VNET_NAME(lagg_failover_rx_all), 0,
258 "Accept input from any interface in a failover lagg");
260 /* Default value for using flowid */
261 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
262 #define V_def_use_flowid VNET(def_use_flowid)
263 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
264 &VNET_NAME(def_use_flowid), 0,
265 "Default setting for using flow id for load sharing");
267 /* Default value for flowid shift */
268 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
269 #define V_def_flowid_shift VNET(def_flowid_shift)
270 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
271 &VNET_NAME(def_flowid_shift), 0,
272 "Default setting for flowid shift for load sharing");
275 vnet_lagg_init(const void *unused __unused)
278 LAGG_LIST_LOCK_INIT();
279 SLIST_INIT(&V_lagg_list);
280 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
281 lagg_clone_destroy, 0);
283 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
284 vnet_lagg_init, NULL);
287 vnet_lagg_uninit(const void *unused __unused)
290 if_clone_detach(V_lagg_cloner);
291 LAGG_LIST_LOCK_DESTROY();
293 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
294 vnet_lagg_uninit, NULL);
297 lagg_modevent(module_t mod, int type, void *data)
302 lagg_input_p = lagg_input;
303 lagg_linkstate_p = lagg_port_state;
304 lagg_detach_cookie = EVENTHANDLER_REGISTER(
305 ifnet_departure_event, lagg_port_ifdetach, NULL,
306 EVENTHANDLER_PRI_ANY);
309 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
312 lagg_linkstate_p = NULL;
320 static moduledata_t lagg_mod = {
326 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
327 MODULE_VERSION(if_lagg, 1);
330 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
333 LAGG_XLOCK_ASSERT(sc);
334 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
337 if (sc->sc_ifflags & IFF_DEBUG)
338 if_printf(sc->sc_ifp, "using proto %u\n", pr);
340 if (lagg_protos[pr].pr_attach != NULL)
341 lagg_protos[pr].pr_attach(sc);
346 lagg_proto_detach(struct lagg_softc *sc)
350 LAGG_XLOCK_ASSERT(sc);
352 sc->sc_proto = LAGG_PROTO_NONE;
354 if (lagg_protos[pr].pr_detach != NULL)
355 lagg_protos[pr].pr_detach(sc);
359 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
362 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
366 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
369 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
373 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
376 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
379 return (lagg_protos[sc->sc_proto].pr_addport(lp));
383 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
386 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
387 lagg_protos[sc->sc_proto].pr_delport(lp);
391 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
394 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
395 lagg_protos[sc->sc_proto].pr_linkstate(lp);
399 lagg_proto_init(struct lagg_softc *sc)
402 if (lagg_protos[sc->sc_proto].pr_init != NULL)
403 lagg_protos[sc->sc_proto].pr_init(sc);
407 lagg_proto_stop(struct lagg_softc *sc)
410 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
411 lagg_protos[sc->sc_proto].pr_stop(sc);
415 lagg_proto_lladdr(struct lagg_softc *sc)
418 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
419 lagg_protos[sc->sc_proto].pr_lladdr(sc);
423 lagg_proto_request(struct lagg_softc *sc, void *v)
426 if (lagg_protos[sc->sc_proto].pr_request != NULL)
427 lagg_protos[sc->sc_proto].pr_request(sc, v);
431 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
434 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
435 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
439 * This routine is run via an vlan
443 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
445 struct lagg_softc *sc = ifp->if_softc;
446 struct lagg_port *lp;
448 if (ifp->if_softc != arg) /* Not our event */
452 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
453 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
458 * This routine is run via an vlan
462 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
464 struct lagg_softc *sc = ifp->if_softc;
465 struct lagg_port *lp;
467 if (ifp->if_softc != arg) /* Not our event */
471 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
472 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
477 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
479 struct lagg_softc *sc;
481 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
483 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
484 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
492 if (V_def_use_flowid)
493 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
494 sc->flowid_shift = V_def_flowid_shift;
496 /* Hash all layers by default */
497 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
499 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
501 CK_SLIST_INIT(&sc->sc_ports);
503 /* Initialise pseudo media types */
504 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
506 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
507 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
509 if_initname(ifp, laggname, unit);
511 ifp->if_transmit = lagg_transmit;
512 ifp->if_qflush = lagg_qflush;
513 ifp->if_init = lagg_init;
514 ifp->if_ioctl = lagg_ioctl;
515 ifp->if_get_counter = lagg_get_counter;
516 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
518 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
519 ifp->if_snd_tag_free = lagg_snd_tag_free;
521 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
524 * Attach as an ordinary ethernet device, children will be attached
525 * as special device IFT_IEEE8023ADLAG.
527 ether_ifattach(ifp, eaddr);
529 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
530 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
531 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
532 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
534 /* Insert into the global list of laggs */
536 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
544 lagg_clone_destroy(struct ifnet *ifp)
546 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
547 struct lagg_port *lp;
550 sc->sc_destroying = 1;
552 ifp->if_flags &= ~IFF_UP;
554 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
555 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
557 /* Shutdown and remove lagg ports */
558 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
559 lagg_port_destroy(lp, 1);
561 /* Unhook the aggregation protocol */
562 lagg_proto_detach(sc);
565 ifmedia_removeall(&sc->sc_media);
570 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
578 lagg_capabilities(struct lagg_softc *sc)
580 struct lagg_port *lp;
583 struct ifnet_hw_tsomax hw_tsomax;
585 LAGG_XLOCK_ASSERT(sc);
587 /* Get common enabled capabilities for the lagg ports */
589 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
590 ena &= lp->lp_ifp->if_capenable;
591 ena = (ena == ~0 ? 0 : ena);
594 * Apply common enabled capabilities back to the lagg ports.
595 * May require several iterations if they are dependent.
599 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
600 lagg_setcaps(lp, ena);
601 ena &= lp->lp_ifp->if_capenable;
603 } while (pena != ena);
605 /* Get other capabilities from the lagg ports */
608 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
609 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
610 cap &= lp->lp_ifp->if_capabilities;
611 hwa &= lp->lp_ifp->if_hwassist;
612 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
614 cap = (cap == ~0 ? 0 : cap);
615 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
617 if (sc->sc_ifp->if_capabilities != cap ||
618 sc->sc_ifp->if_capenable != ena ||
619 sc->sc_ifp->if_hwassist != hwa ||
620 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
621 sc->sc_ifp->if_capabilities = cap;
622 sc->sc_ifp->if_capenable = ena;
623 sc->sc_ifp->if_hwassist = hwa;
624 getmicrotime(&sc->sc_ifp->if_lastchange);
626 if (sc->sc_ifflags & IFF_DEBUG)
627 if_printf(sc->sc_ifp,
628 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
633 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
635 struct lagg_softc *sc_ptr;
636 struct lagg_port *lp, *tlp;
638 int error, i, oldmtu;
641 LAGG_XLOCK_ASSERT(sc);
643 if (sc->sc_ifp == ifp) {
644 if_printf(sc->sc_ifp,
645 "cannot add a lagg to itself as a port\n");
649 /* Limit the maximal number of lagg ports */
650 if (sc->sc_count >= LAGG_MAX_PORTS)
653 /* Check if port has already been associated to a lagg */
654 if (ifp->if_lagg != NULL) {
655 /* Port is already in the current lagg? */
656 lp = (struct lagg_port *)ifp->if_lagg;
657 if (lp->lp_softc == sc)
662 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
663 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
664 return (EPROTONOSUPPORT);
666 /* Allow the first Ethernet member to define the MTU */
668 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
669 sc->sc_ifp->if_mtu = ifp->if_mtu;
670 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
671 if (ifp->if_ioctl == NULL) {
672 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
676 oldmtu = ifp->if_mtu;
677 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
678 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
679 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
681 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
685 ifr.ifr_mtu = oldmtu;
688 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
691 /* Check if port is a stacked lagg */
693 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
694 if (ifp == sc_ptr->sc_ifp) {
698 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
701 /* XXX disable stacking for the moment, its untested */
702 #ifdef LAGG_PORT_STACKING
703 lp->lp_flags |= LAGG_PORT_STACK;
704 if (lagg_port_checkstacking(sc_ptr) >=
709 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
721 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
722 lp->lp_ifcapenable = ifp->if_capenable;
723 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
724 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
725 lagg_proto_lladdr(sc);
726 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
728 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
730 lagg_setflags(lp, 1);
732 if (CK_SLIST_EMPTY(&sc->sc_ports))
735 /* Change the interface type */
736 lp->lp_iftype = ifp->if_type;
737 ifp->if_type = IFT_IEEE8023ADLAG;
739 lp->lp_ioctl = ifp->if_ioctl;
740 ifp->if_ioctl = lagg_port_ioctl;
741 lp->lp_output = ifp->if_output;
742 ifp->if_output = lagg_port_output;
744 /* Read port counters */
745 pval = lp->port_counters.val;
746 for (i = 0; i < IFCOUNTERS; i++, pval++)
747 *pval = ifp->if_get_counter(ifp, i);
750 * Insert into the list of ports.
751 * Keep ports sorted by if_index. It is handy, when configuration
752 * is predictable and `ifconfig laggN create ...` command
753 * will lead to the same result each time.
755 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
756 if (tlp->lp_ifp->if_index < ifp->if_index && (
757 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
758 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
763 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
765 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
771 if ((error = lagg_proto_addport(sc, lp)) != 0) {
772 /* Remove the port, without calling pr_delport. */
773 lagg_port_destroy(lp, 0);
775 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
779 /* Update lagg capabilities */
780 lagg_capabilities(sc);
786 #ifdef LAGG_PORT_STACKING
788 lagg_port_checkstacking(struct lagg_softc *sc)
790 struct lagg_softc *sc_ptr;
791 struct lagg_port *lp;
794 LAGG_SXLOCK_ASSERT(sc);
795 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
796 if (lp->lp_flags & LAGG_PORT_STACK) {
797 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
798 m = MAX(m, lagg_port_checkstacking(sc_ptr));
807 lagg_port_destroy_cb(epoch_context_t ec)
809 struct lagg_port *lp;
812 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
820 lagg_port_destroy(struct lagg_port *lp, int rundelport)
822 struct lagg_softc *sc = lp->lp_softc;
823 struct lagg_port *lp_ptr, *lp0;
824 struct ifnet *ifp = lp->lp_ifp;
825 uint64_t *pval, vdiff;
828 LAGG_XLOCK_ASSERT(sc);
831 lagg_proto_delport(sc, lp);
833 if (lp->lp_detaching == 0)
836 /* Restore interface */
837 ifp->if_type = lp->lp_iftype;
838 ifp->if_ioctl = lp->lp_ioctl;
839 ifp->if_output = lp->lp_output;
842 /* Update detached port counters */
843 pval = lp->port_counters.val;
844 for (i = 0; i < IFCOUNTERS; i++, pval++) {
845 vdiff = ifp->if_get_counter(ifp, i) - *pval;
846 sc->detached_counters.val[i] += vdiff;
849 /* Finally, remove the port from the lagg */
850 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
853 /* Update the primary interface */
854 if (lp == sc->sc_primary) {
855 uint8_t lladdr[ETHER_ADDR_LEN];
857 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
858 bzero(&lladdr, ETHER_ADDR_LEN);
860 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
861 sc->sc_primary = lp0;
862 if (sc->sc_destroying == 0) {
863 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
864 lagg_proto_lladdr(sc);
865 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
869 * Update lladdr for each port (new primary needs update
870 * as well, to switch from old lladdr to its 'real' one)
872 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
873 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
877 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
879 if (lp->lp_detaching == 0) {
880 lagg_setflags(lp, 0);
881 lagg_setcaps(lp, lp->lp_ifcapenable);
882 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
886 * free port and release it's ifnet reference after a grace period has
889 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
890 /* Update lagg capabilities */
891 lagg_capabilities(sc);
898 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
900 struct lagg_reqport *rp = (struct lagg_reqport *)data;
901 struct lagg_softc *sc;
902 struct lagg_port *lp = NULL;
905 /* Should be checked by the caller */
906 if (ifp->if_type != IFT_IEEE8023ADLAG ||
907 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
912 if (rp->rp_portname[0] == '\0' ||
913 ifunit(rp->rp_portname) != ifp) {
919 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
925 lagg_port2req(lp, rp);
930 if (lp->lp_ioctl == NULL) {
934 error = (*lp->lp_ioctl)(ifp, cmd, data);
938 /* Update lagg interface capabilities */
940 lagg_capabilities(sc);
942 VLAN_CAPABILITIES(sc->sc_ifp);
946 /* Do not allow the MTU to be changed once joined */
957 if (lp != NULL && lp->lp_ioctl != NULL)
958 return ((*lp->lp_ioctl)(ifp, cmd, data));
964 * Requests counter @cnt data.
966 * Counter value is calculated the following way:
967 * 1) for each port, sum difference between current and "initial" measurements.
968 * 2) add lagg logical interface counters.
969 * 3) add data from detached_counters array.
971 * We also do the following things on ports attach/detach:
972 * 1) On port attach we store all counters it has into port_counter array.
973 * 2) On port detach we add the different between "initial" and
974 * current counters data to detached_counters array.
977 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
979 struct lagg_softc *sc;
980 struct lagg_port *lp;
982 uint64_t newval, oldval, vsum;
984 /* Revise this when we've got non-generic counters. */
985 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
987 sc = (struct lagg_softc *)ifp->if_softc;
991 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
992 /* Saved attached value */
993 oldval = lp->port_counters.val[cnt];
996 newval = lpifp->if_get_counter(lpifp, cnt);
997 /* Calculate diff and save new */
998 vsum += newval - oldval;
1003 * Add counter data which might be added by upper
1004 * layer protocols operating on logical interface.
1006 vsum += if_get_counter_default(ifp, cnt);
1009 * Add counter data from detached ports counters
1011 vsum += sc->detached_counters.val[cnt];
1018 * For direct output to child ports.
1021 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1022 const struct sockaddr *dst, struct route *ro)
1024 struct lagg_port *lp = ifp->if_lagg;
1026 switch (dst->sa_family) {
1027 case pseudo_AF_HDRCMPLT:
1029 return ((*lp->lp_output)(ifp, m, dst, ro));
1032 /* drop any other frames */
1038 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1040 struct lagg_port *lp;
1041 struct lagg_softc *sc;
1043 if ((lp = ifp->if_lagg) == NULL)
1045 /* If the ifnet is just being renamed, don't do anything. */
1046 if (ifp->if_flags & IFF_RENAMING)
1052 lp->lp_detaching = 1;
1053 lagg_port_destroy(lp, 1);
1055 VLAN_CAPABILITIES(sc->sc_ifp);
1059 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1061 struct lagg_softc *sc = lp->lp_softc;
1063 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1064 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1065 rp->rp_prio = lp->lp_prio;
1066 rp->rp_flags = lp->lp_flags;
1067 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1069 /* Add protocol specific flags */
1070 switch (sc->sc_proto) {
1071 case LAGG_PROTO_FAILOVER:
1072 if (lp == sc->sc_primary)
1073 rp->rp_flags |= LAGG_PORT_MASTER;
1074 if (lp == lagg_link_active(sc, sc->sc_primary))
1075 rp->rp_flags |= LAGG_PORT_ACTIVE;
1078 case LAGG_PROTO_ROUNDROBIN:
1079 case LAGG_PROTO_LOADBALANCE:
1080 case LAGG_PROTO_BROADCAST:
1081 if (LAGG_PORTACTIVE(lp))
1082 rp->rp_flags |= LAGG_PORT_ACTIVE;
1085 case LAGG_PROTO_LACP:
1086 /* LACP has a different definition of active */
1087 if (lacp_isactive(lp))
1088 rp->rp_flags |= LAGG_PORT_ACTIVE;
1089 if (lacp_iscollecting(lp))
1090 rp->rp_flags |= LAGG_PORT_COLLECTING;
1091 if (lacp_isdistributing(lp))
1092 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1099 lagg_init(void *xsc)
1101 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1102 struct ifnet *ifp = sc->sc_ifp;
1103 struct lagg_port *lp;
1106 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1111 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1114 * Update the port lladdrs if needed.
1115 * This might be if_setlladdr() notification
1116 * that lladdr has been changed.
1118 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1119 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1120 ETHER_ADDR_LEN) != 0)
1121 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1124 lagg_proto_init(sc);
1130 lagg_stop(struct lagg_softc *sc)
1132 struct ifnet *ifp = sc->sc_ifp;
1134 LAGG_XLOCK_ASSERT(sc);
1136 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1139 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1141 lagg_proto_stop(sc);
1145 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1147 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1148 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1149 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1150 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1151 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1152 struct ifreq *ifr = (struct ifreq *)data;
1153 struct lagg_port *lp;
1155 struct thread *td = curthread;
1157 int count, buflen, len, error = 0;
1159 bzero(&rpbuf, sizeof(rpbuf));
1164 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1165 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1166 ra->ra_proto = sc->sc_proto;
1167 lagg_proto_request(sc, &ra->ra_psc);
1170 len = min(ra->ra_size, buflen);
1171 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1172 if (len < sizeof(rpbuf))
1175 lagg_port2req(lp, &rpbuf);
1176 memcpy(buf, &rpbuf, sizeof(rpbuf));
1178 buf += sizeof(rpbuf);
1179 len -= sizeof(rpbuf);
1182 ra->ra_ports = count;
1183 ra->ra_size = count * sizeof(rpbuf);
1184 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1185 free(outbuf, M_TEMP);
1188 error = priv_check(td, PRIV_NET_LAGG);
1191 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1192 error = EPROTONOSUPPORT;
1197 lagg_proto_detach(sc);
1198 LAGG_UNLOCK_ASSERT();
1199 lagg_proto_attach(sc, ra->ra_proto);
1204 ro->ro_opts = sc->sc_opts;
1205 if (sc->sc_proto == LAGG_PROTO_LACP) {
1206 struct lacp_softc *lsc;
1208 lsc = (struct lacp_softc *)sc->sc_psc;
1209 if (lsc->lsc_debug.lsc_tx_test != 0)
1210 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1211 if (lsc->lsc_debug.lsc_rx_test != 0)
1212 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1213 if (lsc->lsc_strict_mode != 0)
1214 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1215 if (lsc->lsc_fast_timeout != 0)
1216 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1218 ro->ro_active = sc->sc_active;
1221 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1222 ro->ro_active += LAGG_PORTACTIVE(lp);
1224 ro->ro_bkt = sc->sc_bkt;
1225 ro->ro_flapping = sc->sc_flapping;
1226 ro->ro_flowid_shift = sc->flowid_shift;
1230 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1231 if (ro->ro_bkt == 0)
1232 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1234 sc->sc_bkt = ro->ro_bkt;
1236 error = priv_check(td, PRIV_NET_LAGG);
1239 if (ro->ro_opts == 0)
1242 * Set options. LACP options are stored in sc->sc_psc,
1247 switch (ro->ro_opts) {
1248 case LAGG_OPT_USE_FLOWID:
1249 case -LAGG_OPT_USE_FLOWID:
1250 case LAGG_OPT_FLOWIDSHIFT:
1254 case LAGG_OPT_LACP_TXTEST:
1255 case -LAGG_OPT_LACP_TXTEST:
1256 case LAGG_OPT_LACP_RXTEST:
1257 case -LAGG_OPT_LACP_RXTEST:
1258 case LAGG_OPT_LACP_STRICT:
1259 case -LAGG_OPT_LACP_STRICT:
1260 case LAGG_OPT_LACP_TIMEOUT:
1261 case -LAGG_OPT_LACP_TIMEOUT:
1272 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1273 /* Invalid combination of options specified. */
1276 break; /* Return from SIOCSLAGGOPTS. */
1279 * Store new options into sc->sc_opts except for
1280 * FLOWIDSHIFT and LACP options.
1283 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1284 sc->flowid_shift = ro->ro_flowid_shift;
1285 else if (ro->ro_opts > 0)
1286 sc->sc_opts |= ro->ro_opts;
1288 sc->sc_opts &= ~ro->ro_opts;
1290 struct lacp_softc *lsc;
1291 struct lacp_port *lp;
1293 lsc = (struct lacp_softc *)sc->sc_psc;
1295 switch (ro->ro_opts) {
1296 case LAGG_OPT_LACP_TXTEST:
1297 lsc->lsc_debug.lsc_tx_test = 1;
1299 case -LAGG_OPT_LACP_TXTEST:
1300 lsc->lsc_debug.lsc_tx_test = 0;
1302 case LAGG_OPT_LACP_RXTEST:
1303 lsc->lsc_debug.lsc_rx_test = 1;
1305 case -LAGG_OPT_LACP_RXTEST:
1306 lsc->lsc_debug.lsc_rx_test = 0;
1308 case LAGG_OPT_LACP_STRICT:
1309 lsc->lsc_strict_mode = 1;
1311 case -LAGG_OPT_LACP_STRICT:
1312 lsc->lsc_strict_mode = 0;
1314 case LAGG_OPT_LACP_TIMEOUT:
1316 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1317 lp->lp_state |= LACP_STATE_TIMEOUT;
1319 lsc->lsc_fast_timeout = 1;
1321 case -LAGG_OPT_LACP_TIMEOUT:
1323 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1324 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1326 lsc->lsc_fast_timeout = 0;
1332 case SIOCGLAGGFLAGS:
1335 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1336 rf->rf_flags |= LAGG_F_HASHL2;
1337 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1338 rf->rf_flags |= LAGG_F_HASHL3;
1339 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1340 rf->rf_flags |= LAGG_F_HASHL4;
1344 error = priv_check(td, PRIV_NET_LAGG);
1347 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1353 if (rf->rf_flags & LAGG_F_HASHL2)
1354 sc->sc_flags |= MBUF_HASHFLAG_L2;
1355 if (rf->rf_flags & LAGG_F_HASHL3)
1356 sc->sc_flags |= MBUF_HASHFLAG_L3;
1357 if (rf->rf_flags & LAGG_F_HASHL4)
1358 sc->sc_flags |= MBUF_HASHFLAG_L4;
1362 if (rp->rp_portname[0] == '\0' ||
1363 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1369 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1370 lp->lp_softc != sc) {
1377 lagg_port2req(lp, rp);
1382 error = priv_check(td, PRIV_NET_LAGG);
1385 if (rp->rp_portname[0] == '\0' ||
1386 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1392 * A laggport interface should not have inet6 address
1393 * because two interfaces with a valid link-local
1394 * scope zone must not be merged in any form. This
1395 * restriction is needed to prevent violation of
1396 * link-local scope zone. Attempts to add a laggport
1397 * interface which has inet6 addresses triggers
1398 * removal of all inet6 addresses on the member
1401 if (in6ifa_llaonifp(tpif)) {
1403 if_printf(sc->sc_ifp,
1404 "IPv6 addresses on %s have been removed "
1405 "before adding it as a member to prevent "
1406 "IPv6 address scope violation.\n",
1411 error = lagg_port_create(sc, tpif);
1414 VLAN_CAPABILITIES(ifp);
1416 case SIOCSLAGGDELPORT:
1417 error = priv_check(td, PRIV_NET_LAGG);
1420 if (rp->rp_portname[0] == '\0' ||
1421 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1427 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1428 lp->lp_softc != sc) {
1435 error = lagg_port_destroy(lp, 1);
1438 VLAN_CAPABILITIES(ifp);
1441 /* Set flags on ports too */
1443 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1444 lagg_setflags(lp, 1);
1447 if (!(ifp->if_flags & IFF_UP) &&
1448 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1450 * If interface is marked down and it is running,
1451 * then stop and disable it.
1455 } else if ((ifp->if_flags & IFF_UP) &&
1456 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1458 * If interface is marked up and it is stopped, then
1462 (*ifp->if_init)(sc);
1469 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1478 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1483 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1484 if (lp->lp_ioctl != NULL)
1485 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1487 lagg_capabilities(sc);
1489 VLAN_CAPABILITIES(ifp);
1495 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1496 if (lp->lp_ioctl != NULL)
1497 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1502 "failed to change MTU to %d on port %s, "
1503 "reverting all ports to original MTU (%d)\n",
1504 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1509 ifp->if_mtu = ifr->ifr_mtu;
1511 /* set every port back to the original MTU */
1512 ifr->ifr_mtu = ifp->if_mtu;
1513 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1514 if (lp->lp_ioctl != NULL)
1515 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1522 error = ether_ioctl(ifp, cmd, data);
1530 lagg_snd_tag_alloc(struct ifnet *ifp,
1531 union if_snd_tag_alloc_params *params,
1532 struct m_snd_tag **ppmt)
1534 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1535 struct lagg_port *lp;
1540 switch (sc->sc_proto) {
1541 case LAGG_PROTO_FAILOVER:
1542 lp = lagg_link_active(sc, sc->sc_primary);
1544 case LAGG_PROTO_LOADBALANCE:
1545 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1546 params->hdr.flowtype == M_HASHTYPE_NONE) {
1548 return (EOPNOTSUPP);
1550 p = params->hdr.flowid >> sc->flowid_shift;
1552 lb = (struct lagg_lb *)sc->sc_psc;
1553 lp = lb->lb_ports[p];
1554 lp = lagg_link_active(sc, lp);
1556 case LAGG_PROTO_LACP:
1557 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1558 params->hdr.flowtype == M_HASHTYPE_NONE) {
1560 return (EOPNOTSUPP);
1562 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1566 return (EOPNOTSUPP);
1570 return (EOPNOTSUPP);
1574 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1575 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1576 return (EOPNOTSUPP);
1578 /* forward allocation request */
1579 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1583 lagg_snd_tag_free(struct m_snd_tag *tag)
1585 tag->ifp->if_snd_tag_free(tag);
1591 lagg_setmulti(struct lagg_port *lp)
1593 struct lagg_softc *sc = lp->lp_softc;
1594 struct ifnet *ifp = lp->lp_ifp;
1595 struct ifnet *scifp = sc->sc_ifp;
1597 struct ifmultiaddr *ifma;
1600 IF_ADDR_WLOCK(scifp);
1601 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1602 if (ifma->ifma_addr->sa_family != AF_LINK)
1604 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1606 IF_ADDR_WUNLOCK(scifp);
1609 bcopy(ifma->ifma_addr, &mc->mc_addr,
1610 ifma->ifma_addr->sa_len);
1611 mc->mc_addr.sdl_index = ifp->if_index;
1613 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1615 IF_ADDR_WUNLOCK(scifp);
1616 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1617 error = if_addmulti(ifp,
1618 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1626 lagg_clrmulti(struct lagg_port *lp)
1630 LAGG_XLOCK_ASSERT(lp->lp_softc);
1631 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1632 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1633 if (mc->mc_ifma && lp->lp_detaching == 0)
1634 if_delmulti_ifma(mc->mc_ifma);
1641 lagg_setcaps(struct lagg_port *lp, int cap)
1645 if (lp->lp_ifp->if_capenable == cap)
1647 if (lp->lp_ioctl == NULL)
1649 ifr.ifr_reqcap = cap;
1650 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1653 /* Handle a ref counted flag that should be set on the lagg port as well */
1655 lagg_setflag(struct lagg_port *lp, int flag, int status,
1656 int (*func)(struct ifnet *, int))
1658 struct lagg_softc *sc = lp->lp_softc;
1659 struct ifnet *scifp = sc->sc_ifp;
1660 struct ifnet *ifp = lp->lp_ifp;
1663 LAGG_XLOCK_ASSERT(sc);
1665 status = status ? (scifp->if_flags & flag) : 0;
1666 /* Now "status" contains the flag value or 0 */
1669 * See if recorded ports status is different from what
1670 * we want it to be. If it is, flip it. We record ports
1671 * status in lp_ifflags so that we won't clear ports flag
1672 * we haven't set. In fact, we don't clear or set ports
1673 * flags directly, but get or release references to them.
1674 * That's why we can be sure that recorded flags still are
1675 * in accord with actual ports flags.
1677 if (status != (lp->lp_ifflags & flag)) {
1678 error = (*func)(ifp, status);
1681 lp->lp_ifflags &= ~flag;
1682 lp->lp_ifflags |= status;
1688 * Handle IFF_* flags that require certain changes on the lagg port
1689 * if "status" is true, update ports flags respective to the lagg
1690 * if "status" is false, forcedly clear the flags set on port.
1693 lagg_setflags(struct lagg_port *lp, int status)
1697 for (i = 0; lagg_pflags[i].flag; i++) {
1698 error = lagg_setflag(lp, lagg_pflags[i].flag,
1699 status, lagg_pflags[i].func);
1707 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1709 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1713 /* We need a Tx algorithm and at least one port */
1714 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1717 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1721 ETHER_BPF_MTAP(ifp, m);
1723 error = lagg_proto_start(sc, m);
1727 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1733 * The ifp->if_qflush entry point for lagg(4) is no-op.
1736 lagg_qflush(struct ifnet *ifp __unused)
1740 static struct mbuf *
1741 lagg_input(struct ifnet *ifp, struct mbuf *m)
1743 struct lagg_port *lp = ifp->if_lagg;
1744 struct lagg_softc *sc = lp->lp_softc;
1745 struct ifnet *scifp = sc->sc_ifp;
1748 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1749 lp->lp_detaching != 0 ||
1750 sc->sc_proto == LAGG_PROTO_NONE) {
1756 ETHER_BPF_MTAP(scifp, m);
1758 m = lagg_proto_input(sc, lp, m);
1759 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1769 lagg_media_change(struct ifnet *ifp)
1771 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1773 if (sc->sc_ifflags & IFF_DEBUG)
1774 printf("%s\n", __func__);
1781 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1783 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1784 struct lagg_port *lp;
1786 imr->ifm_status = IFM_AVALID;
1787 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1790 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1791 if (LAGG_PORTACTIVE(lp))
1792 imr->ifm_status |= IFM_ACTIVE;
1798 lagg_linkstate(struct lagg_softc *sc)
1800 struct lagg_port *lp;
1801 int new_link = LINK_STATE_DOWN;
1804 LAGG_XLOCK_ASSERT(sc);
1806 /* LACP handles link state itself */
1807 if (sc->sc_proto == LAGG_PROTO_LACP)
1810 /* Our link is considered up if at least one of our ports is active */
1812 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1813 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1814 new_link = LINK_STATE_UP;
1819 if_link_state_change(sc->sc_ifp, new_link);
1821 /* Update if_baudrate to reflect the max possible speed */
1822 switch (sc->sc_proto) {
1823 case LAGG_PROTO_FAILOVER:
1824 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1825 sc->sc_primary->lp_ifp->if_baudrate : 0;
1827 case LAGG_PROTO_ROUNDROBIN:
1828 case LAGG_PROTO_LOADBALANCE:
1829 case LAGG_PROTO_BROADCAST:
1832 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1833 speed += lp->lp_ifp->if_baudrate;
1835 sc->sc_ifp->if_baudrate = speed;
1837 case LAGG_PROTO_LACP:
1838 /* LACP updates if_baudrate itself */
1844 lagg_port_state(struct ifnet *ifp, int state)
1846 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1847 struct lagg_softc *sc = NULL;
1856 lagg_proto_linkstate(sc, lp);
1861 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1863 struct lagg_port *lp_next, *rval = NULL;
1866 * Search a port which reports an active link state.
1870 * This is called with either LAGG_RLOCK() held or
1871 * LAGG_XLOCK(sc) held.
1873 if (!in_epoch(net_epoch_preempt))
1874 LAGG_XLOCK_ASSERT(sc);
1878 if (LAGG_PORTACTIVE(lp)) {
1882 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
1883 LAGG_PORTACTIVE(lp_next)) {
1889 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1890 if (LAGG_PORTACTIVE(lp_next)) {
1899 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1902 return (ifp->if_transmit)(ifp, m);
1906 * Simple round robin aggregation
1909 lagg_rr_attach(struct lagg_softc *sc)
1912 sc->sc_bkt_count = sc->sc_bkt;
1916 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1918 struct lagg_port *lp;
1921 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1922 sc->sc_bkt_count = sc->sc_bkt;
1924 if (sc->sc_bkt > 0) {
1925 atomic_subtract_int(&sc->sc_bkt_count, 1);
1926 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1927 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1931 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1934 lp = CK_SLIST_FIRST(&sc->sc_ports);
1937 lp = CK_SLIST_NEXT(lp, lp_entries);
1940 * Check the port's link state. This will return the next active
1941 * port if the link is down or the port is NULL.
1943 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1949 return (lagg_enqueue(lp->lp_ifp, m));
1952 static struct mbuf *
1953 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1955 struct ifnet *ifp = sc->sc_ifp;
1957 /* Just pass in the packet to our lagg device */
1958 m->m_pkthdr.rcvif = ifp;
1967 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1969 int active_ports = 0;
1972 struct lagg_port *lp, *last = NULL;
1975 LAGG_RLOCK_ASSERT();
1976 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1977 if (!LAGG_PORTACTIVE(lp))
1983 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1990 ret = lagg_enqueue(last->lp_ifp, m0);
2001 if ((last = lagg_link_active(sc, last)) == NULL) {
2006 ret = lagg_enqueue(last->lp_ifp, m);
2017 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2019 struct ifnet *ifp = sc->sc_ifp;
2021 /* Just pass in the packet to our lagg device */
2022 m->m_pkthdr.rcvif = ifp;
2030 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2032 struct lagg_port *lp;
2034 /* Use the master port if active or the next available port */
2035 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2041 return (lagg_enqueue(lp->lp_ifp, m));
2044 static struct mbuf *
2045 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2047 struct ifnet *ifp = sc->sc_ifp;
2048 struct lagg_port *tmp_tp;
2050 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2051 m->m_pkthdr.rcvif = ifp;
2055 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2056 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2058 * If tmp_tp is null, we've received a packet when all
2059 * our links are down. Weird, but process it anyways.
2061 if ((tmp_tp == NULL || tmp_tp == lp)) {
2062 m->m_pkthdr.rcvif = ifp;
2075 lagg_lb_attach(struct lagg_softc *sc)
2077 struct lagg_port *lp;
2080 LAGG_XLOCK_ASSERT(sc);
2081 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2082 lb->lb_key = m_ether_tcpip_hash_init();
2085 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2086 lagg_lb_port_create(lp);
2090 lagg_lb_detach(struct lagg_softc *sc)
2094 lb = (struct lagg_lb *)sc->sc_psc;
2100 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2102 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2103 struct lagg_port *lp_next;
2107 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2108 LAGG_XLOCK_ASSERT(sc);
2109 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2112 if (i >= LAGG_MAX_PORTS) {
2116 if (sc->sc_ifflags & IFF_DEBUG)
2117 printf("%s: port %s at index %d\n",
2118 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2119 lb->lb_ports[i++] = lp_next;
2126 lagg_lb_port_create(struct lagg_port *lp)
2128 struct lagg_softc *sc = lp->lp_softc;
2129 return (lagg_lb_porttable(sc, NULL));
2133 lagg_lb_port_destroy(struct lagg_port *lp)
2135 struct lagg_softc *sc = lp->lp_softc;
2136 lagg_lb_porttable(sc, lp);
2140 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2142 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2143 struct lagg_port *lp = NULL;
2146 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2147 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2148 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2150 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2152 lp = lb->lb_ports[p];
2155 * Check the port's link state. This will return the next active
2156 * port if the link is down or the port is NULL.
2158 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2164 return (lagg_enqueue(lp->lp_ifp, m));
2167 static struct mbuf *
2168 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2170 struct ifnet *ifp = sc->sc_ifp;
2172 /* Just pass in the packet to our lagg device */
2173 m->m_pkthdr.rcvif = ifp;
2182 lagg_lacp_attach(struct lagg_softc *sc)
2184 struct lagg_port *lp;
2187 LAGG_XLOCK_ASSERT(sc);
2188 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2189 lacp_port_create(lp);
2193 lagg_lacp_detach(struct lagg_softc *sc)
2195 struct lagg_port *lp;
2198 LAGG_XLOCK_ASSERT(sc);
2199 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2200 lacp_port_destroy(lp);
2208 lagg_lacp_lladdr(struct lagg_softc *sc)
2210 struct lagg_port *lp;
2212 LAGG_SXLOCK_ASSERT(sc);
2214 /* purge all the lacp ports */
2215 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2216 lacp_port_destroy(lp);
2218 /* add them back in */
2219 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2220 lacp_port_create(lp);
2224 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2226 struct lagg_port *lp;
2228 lp = lacp_select_tx_port(sc, m);
2235 return (lagg_enqueue(lp->lp_ifp, m));
2238 static struct mbuf *
2239 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2241 struct ifnet *ifp = sc->sc_ifp;
2242 struct ether_header *eh;
2245 eh = mtod(m, struct ether_header *);
2246 etype = ntohs(eh->ether_type);
2248 /* Tap off LACP control messages */
2249 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2250 m = lacp_input(lp, m);
2256 * If the port is not collecting or not in the active aggregator then
2259 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2264 m->m_pkthdr.rcvif = ifp;