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";
115 static void lagg_capabilities(struct lagg_softc *);
116 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
117 static int lagg_port_destroy(struct lagg_port *, int);
118 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
119 static void lagg_linkstate(struct lagg_softc *);
120 static void lagg_port_state(struct ifnet *, int);
121 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
122 static int lagg_port_output(struct ifnet *, struct mbuf *,
123 const struct sockaddr *, struct route *);
124 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
125 #ifdef LAGG_PORT_STACKING
126 static int lagg_port_checkstacking(struct lagg_softc *);
128 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
129 static void lagg_init(void *);
130 static void lagg_stop(struct lagg_softc *);
131 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
133 static int lagg_snd_tag_alloc(struct ifnet *,
134 union if_snd_tag_alloc_params *,
135 struct m_snd_tag **);
137 static int lagg_setmulti(struct lagg_port *);
138 static int lagg_clrmulti(struct lagg_port *);
139 static int lagg_setcaps(struct lagg_port *, int cap);
140 static int lagg_setflag(struct lagg_port *, int, int,
141 int (*func)(struct ifnet *, int));
142 static int lagg_setflags(struct lagg_port *, int status);
143 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
144 static int lagg_transmit(struct ifnet *, struct mbuf *);
145 static void lagg_qflush(struct ifnet *);
146 static int lagg_media_change(struct ifnet *);
147 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
148 static struct lagg_port *lagg_link_active(struct lagg_softc *,
151 /* Simple round robin */
152 static void lagg_rr_attach(struct lagg_softc *);
153 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
154 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
157 /* Active failover */
158 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
159 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
163 static void lagg_lb_attach(struct lagg_softc *);
164 static void lagg_lb_detach(struct lagg_softc *);
165 static int lagg_lb_port_create(struct lagg_port *);
166 static void lagg_lb_port_destroy(struct lagg_port *);
167 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
168 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
170 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
173 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
174 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
178 static void lagg_lacp_attach(struct lagg_softc *);
179 static void lagg_lacp_detach(struct lagg_softc *);
180 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
181 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
183 static void lagg_lacp_lladdr(struct lagg_softc *);
185 /* lagg protocol table */
186 static const struct lagg_proto {
188 void (*pr_attach)(struct lagg_softc *);
189 void (*pr_detach)(struct lagg_softc *);
190 int (*pr_start)(struct lagg_softc *, struct mbuf *);
191 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
193 int (*pr_addport)(struct lagg_port *);
194 void (*pr_delport)(struct lagg_port *);
195 void (*pr_linkstate)(struct lagg_port *);
196 void (*pr_init)(struct lagg_softc *);
197 void (*pr_stop)(struct lagg_softc *);
198 void (*pr_lladdr)(struct lagg_softc *);
199 void (*pr_request)(struct lagg_softc *, void *);
200 void (*pr_portreq)(struct lagg_port *, void *);
203 .pr_num = LAGG_PROTO_NONE
206 .pr_num = LAGG_PROTO_ROUNDROBIN,
207 .pr_attach = lagg_rr_attach,
208 .pr_start = lagg_rr_start,
209 .pr_input = lagg_rr_input,
212 .pr_num = LAGG_PROTO_FAILOVER,
213 .pr_start = lagg_fail_start,
214 .pr_input = lagg_fail_input,
217 .pr_num = LAGG_PROTO_LOADBALANCE,
218 .pr_attach = lagg_lb_attach,
219 .pr_detach = lagg_lb_detach,
220 .pr_start = lagg_lb_start,
221 .pr_input = lagg_lb_input,
222 .pr_addport = lagg_lb_port_create,
223 .pr_delport = lagg_lb_port_destroy,
226 .pr_num = LAGG_PROTO_LACP,
227 .pr_attach = lagg_lacp_attach,
228 .pr_detach = lagg_lacp_detach,
229 .pr_start = lagg_lacp_start,
230 .pr_input = lagg_lacp_input,
231 .pr_addport = lacp_port_create,
232 .pr_delport = lacp_port_destroy,
233 .pr_linkstate = lacp_linkstate,
234 .pr_init = lacp_init,
235 .pr_stop = lacp_stop,
236 .pr_lladdr = lagg_lacp_lladdr,
237 .pr_request = lacp_req,
238 .pr_portreq = lacp_portreq,
241 .pr_num = LAGG_PROTO_BROADCAST,
242 .pr_start = lagg_bcast_start,
243 .pr_input = lagg_bcast_input,
247 SYSCTL_DECL(_net_link);
248 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
251 /* Allow input on any failover links */
252 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
253 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
254 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
255 &VNET_NAME(lagg_failover_rx_all), 0,
256 "Accept input from any interface in a failover lagg");
258 /* Default value for using flowid */
259 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
260 #define V_def_use_flowid VNET(def_use_flowid)
261 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
262 &VNET_NAME(def_use_flowid), 0,
263 "Default setting for using flow id for load sharing");
265 /* Default value for flowid shift */
266 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
267 #define V_def_flowid_shift VNET(def_flowid_shift)
268 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
269 &VNET_NAME(def_flowid_shift), 0,
270 "Default setting for flowid shift for load sharing");
273 vnet_lagg_init(const void *unused __unused)
276 LAGG_LIST_LOCK_INIT();
277 SLIST_INIT(&V_lagg_list);
278 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
279 lagg_clone_destroy, 0);
281 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
282 vnet_lagg_init, NULL);
285 vnet_lagg_uninit(const void *unused __unused)
288 if_clone_detach(V_lagg_cloner);
289 LAGG_LIST_LOCK_DESTROY();
291 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
292 vnet_lagg_uninit, NULL);
295 lagg_modevent(module_t mod, int type, void *data)
300 lagg_input_p = lagg_input;
301 lagg_linkstate_p = lagg_port_state;
302 lagg_detach_cookie = EVENTHANDLER_REGISTER(
303 ifnet_departure_event, lagg_port_ifdetach, NULL,
304 EVENTHANDLER_PRI_ANY);
307 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
310 lagg_linkstate_p = NULL;
318 static moduledata_t lagg_mod = {
324 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
325 MODULE_VERSION(if_lagg, 1);
328 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
331 LAGG_XLOCK_ASSERT(sc);
332 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
335 if (sc->sc_ifflags & IFF_DEBUG)
336 if_printf(sc->sc_ifp, "using proto %u\n", pr);
338 if (lagg_protos[pr].pr_attach != NULL)
339 lagg_protos[pr].pr_attach(sc);
344 lagg_proto_detach(struct lagg_softc *sc)
348 LAGG_XLOCK_ASSERT(sc);
350 sc->sc_proto = LAGG_PROTO_NONE;
352 if (lagg_protos[pr].pr_detach != NULL)
353 lagg_protos[pr].pr_detach(sc);
357 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
360 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
364 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
367 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
371 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
374 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
377 return (lagg_protos[sc->sc_proto].pr_addport(lp));
381 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
384 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
385 lagg_protos[sc->sc_proto].pr_delport(lp);
389 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
392 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
393 lagg_protos[sc->sc_proto].pr_linkstate(lp);
397 lagg_proto_init(struct lagg_softc *sc)
400 if (lagg_protos[sc->sc_proto].pr_init != NULL)
401 lagg_protos[sc->sc_proto].pr_init(sc);
405 lagg_proto_stop(struct lagg_softc *sc)
408 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
409 lagg_protos[sc->sc_proto].pr_stop(sc);
413 lagg_proto_lladdr(struct lagg_softc *sc)
416 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
417 lagg_protos[sc->sc_proto].pr_lladdr(sc);
421 lagg_proto_request(struct lagg_softc *sc, void *v)
424 if (lagg_protos[sc->sc_proto].pr_request != NULL)
425 lagg_protos[sc->sc_proto].pr_request(sc, v);
429 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
432 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
433 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
437 * This routine is run via an vlan
441 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
443 struct lagg_softc *sc = ifp->if_softc;
444 struct lagg_port *lp;
446 if (ifp->if_softc != arg) /* Not our event */
450 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
451 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
456 * This routine is run via an vlan
460 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
462 struct lagg_softc *sc = ifp->if_softc;
463 struct lagg_port *lp;
465 if (ifp->if_softc != arg) /* Not our event */
469 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
470 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
475 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
477 struct lagg_softc *sc;
479 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
481 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
482 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
490 if (V_def_use_flowid)
491 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
492 sc->flowid_shift = V_def_flowid_shift;
494 /* Hash all layers by default */
495 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
497 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
499 CK_SLIST_INIT(&sc->sc_ports);
501 /* Initialise pseudo media types */
502 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
504 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
505 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
507 if_initname(ifp, laggname, unit);
509 ifp->if_transmit = lagg_transmit;
510 ifp->if_qflush = lagg_qflush;
511 ifp->if_init = lagg_init;
512 ifp->if_ioctl = lagg_ioctl;
513 ifp->if_get_counter = lagg_get_counter;
514 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
516 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
517 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS | IFCAP_TXRTLMT;
519 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
523 * Attach as an ordinary ethernet device, children will be attached
524 * as special device IFT_IEEE8023ADLAG.
526 ether_ifattach(ifp, eaddr);
528 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
529 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
530 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
531 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
533 /* Insert into the global list of laggs */
535 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
543 lagg_clone_destroy(struct ifnet *ifp)
545 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
546 struct lagg_port *lp;
549 sc->sc_destroying = 1;
551 ifp->if_flags &= ~IFF_UP;
553 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
554 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
556 /* Shutdown and remove lagg ports */
557 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
558 lagg_port_destroy(lp, 1);
560 /* Unhook the aggregation protocol */
561 lagg_proto_detach(sc);
564 ifmedia_removeall(&sc->sc_media);
569 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
577 lagg_capabilities(struct lagg_softc *sc)
579 struct lagg_port *lp;
582 struct ifnet_hw_tsomax hw_tsomax;
584 LAGG_XLOCK_ASSERT(sc);
586 /* Get common enabled capabilities for the lagg ports */
588 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
589 ena &= lp->lp_ifp->if_capenable;
590 ena = (ena == ~0 ? 0 : ena);
593 * Apply common enabled capabilities back to the lagg ports.
594 * May require several iterations if they are dependent.
598 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
599 lagg_setcaps(lp, ena);
600 ena &= lp->lp_ifp->if_capenable;
602 } while (pena != ena);
604 /* Get other capabilities from the lagg ports */
607 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
608 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
609 cap &= lp->lp_ifp->if_capabilities;
610 hwa &= lp->lp_ifp->if_hwassist;
611 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
613 cap = (cap == ~0 ? 0 : cap);
614 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
616 if (sc->sc_ifp->if_capabilities != cap ||
617 sc->sc_ifp->if_capenable != ena ||
618 sc->sc_ifp->if_hwassist != hwa ||
619 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
620 sc->sc_ifp->if_capabilities = cap;
621 sc->sc_ifp->if_capenable = ena;
622 sc->sc_ifp->if_hwassist = hwa;
623 getmicrotime(&sc->sc_ifp->if_lastchange);
625 if (sc->sc_ifflags & IFF_DEBUG)
626 if_printf(sc->sc_ifp,
627 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
632 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
634 struct lagg_softc *sc_ptr;
635 struct lagg_port *lp, *tlp;
637 int error, i, oldmtu;
640 LAGG_XLOCK_ASSERT(sc);
642 if (sc->sc_ifp == ifp) {
643 if_printf(sc->sc_ifp,
644 "cannot add a lagg to itself as a port\n");
648 /* Limit the maximal number of lagg ports */
649 if (sc->sc_count >= LAGG_MAX_PORTS)
652 /* Check if port has already been associated to a lagg */
653 if (ifp->if_lagg != NULL) {
654 /* Port is already in the current lagg? */
655 lp = (struct lagg_port *)ifp->if_lagg;
656 if (lp->lp_softc == sc)
661 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
662 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
663 return (EPROTONOSUPPORT);
665 /* Allow the first Ethernet member to define the MTU */
667 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
668 sc->sc_ifp->if_mtu = ifp->if_mtu;
669 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
670 if (ifp->if_ioctl == NULL) {
671 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
675 oldmtu = ifp->if_mtu;
676 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
677 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
678 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
680 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
684 ifr.ifr_mtu = oldmtu;
687 lp = malloc(sizeof(struct lagg_port), M_DEVBUF, M_WAITOK|M_ZERO);
690 /* Check if port is a stacked lagg */
692 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
693 if (ifp == sc_ptr->sc_ifp) {
697 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
700 /* XXX disable stacking for the moment, its untested */
701 #ifdef LAGG_PORT_STACKING
702 lp->lp_flags |= LAGG_PORT_STACK;
703 if (lagg_port_checkstacking(sc_ptr) >=
708 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
720 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
721 lp->lp_ifcapenable = ifp->if_capenable;
722 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
723 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
724 lagg_proto_lladdr(sc);
725 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
727 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
729 lagg_setflags(lp, 1);
731 if (CK_SLIST_EMPTY(&sc->sc_ports))
734 /* Change the interface type */
735 lp->lp_iftype = ifp->if_type;
736 ifp->if_type = IFT_IEEE8023ADLAG;
738 lp->lp_ioctl = ifp->if_ioctl;
739 ifp->if_ioctl = lagg_port_ioctl;
740 lp->lp_output = ifp->if_output;
741 ifp->if_output = lagg_port_output;
743 /* Read port counters */
744 pval = lp->port_counters.val;
745 for (i = 0; i < IFCOUNTERS; i++, pval++)
746 *pval = ifp->if_get_counter(ifp, i);
749 * Insert into the list of ports.
750 * Keep ports sorted by if_index. It is handy, when configuration
751 * is predictable and `ifconfig laggN create ...` command
752 * 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 >
764 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
766 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
772 if ((error = lagg_proto_addport(sc, lp)) != 0) {
773 /* Remove the port, without calling pr_delport. */
774 lagg_port_destroy(lp, 0);
776 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
780 /* Update lagg capabilities */
781 lagg_capabilities(sc);
787 #ifdef LAGG_PORT_STACKING
789 lagg_port_checkstacking(struct lagg_softc *sc)
791 struct lagg_softc *sc_ptr;
792 struct lagg_port *lp;
795 LAGG_SXLOCK_ASSERT(sc);
796 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
797 if (lp->lp_flags & LAGG_PORT_STACK) {
798 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
799 m = MAX(m, lagg_port_checkstacking(sc_ptr));
808 lagg_port_destroy_cb(epoch_context_t ec)
810 struct lagg_port *lp;
813 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
821 lagg_port_destroy(struct lagg_port *lp, int rundelport)
823 struct lagg_softc *sc = lp->lp_softc;
824 struct lagg_port *lp_ptr, *lp0;
825 struct ifnet *ifp = lp->lp_ifp;
826 uint64_t *pval, vdiff;
829 LAGG_XLOCK_ASSERT(sc);
832 lagg_proto_delport(sc, lp);
834 if (lp->lp_detaching == 0)
837 /* Restore interface */
838 ifp->if_type = lp->lp_iftype;
839 ifp->if_ioctl = lp->lp_ioctl;
840 ifp->if_output = lp->lp_output;
843 /* Update detached port counters */
844 pval = lp->port_counters.val;
845 for (i = 0; i < IFCOUNTERS; i++, pval++) {
846 vdiff = ifp->if_get_counter(ifp, i) - *pval;
847 sc->detached_counters.val[i] += vdiff;
850 /* Finally, remove the port from the lagg */
851 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
854 /* Update the primary interface */
855 if (lp == sc->sc_primary) {
856 uint8_t lladdr[ETHER_ADDR_LEN];
858 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
859 bzero(&lladdr, ETHER_ADDR_LEN);
861 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
862 sc->sc_primary = lp0;
863 if (sc->sc_destroying == 0) {
864 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
865 lagg_proto_lladdr(sc);
866 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
870 * Update lladdr for each port (new primary needs update
871 * as well, to switch from old lladdr to its 'real' one)
873 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
874 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
878 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
880 if (lp->lp_detaching == 0) {
881 lagg_setflags(lp, 0);
882 lagg_setcaps(lp, lp->lp_ifcapenable);
883 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
887 * free port and release it's ifnet reference after a grace period has
890 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
891 /* Update lagg capabilities */
892 lagg_capabilities(sc);
899 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
901 struct lagg_reqport *rp = (struct lagg_reqport *)data;
902 struct lagg_softc *sc;
903 struct lagg_port *lp = NULL;
906 /* Should be checked by the caller */
907 if (ifp->if_type != IFT_IEEE8023ADLAG ||
908 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
913 if (rp->rp_portname[0] == '\0' ||
914 ifunit(rp->rp_portname) != ifp) {
920 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
926 lagg_port2req(lp, rp);
931 if (lp->lp_ioctl == NULL) {
935 error = (*lp->lp_ioctl)(ifp, cmd, data);
939 /* Update lagg interface capabilities */
941 lagg_capabilities(sc);
943 VLAN_CAPABILITIES(sc->sc_ifp);
947 /* Do not allow the MTU to be changed once joined */
958 if (lp != NULL && lp->lp_ioctl != NULL)
959 return ((*lp->lp_ioctl)(ifp, cmd, data));
965 * Requests counter @cnt data.
967 * Counter value is calculated the following way:
968 * 1) for each port, sum difference between current and "initial" measurements.
969 * 2) add lagg logical interface counters.
970 * 3) add data from detached_counters array.
972 * We also do the following things on ports attach/detach:
973 * 1) On port attach we store all counters it has into port_counter array.
974 * 2) On port detach we add the different between "initial" and
975 * current counters data to detached_counters array.
978 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
980 struct lagg_softc *sc;
981 struct lagg_port *lp;
983 uint64_t newval, oldval, vsum;
985 /* Revise this when we've got non-generic counters. */
986 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
988 sc = (struct lagg_softc *)ifp->if_softc;
992 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
993 /* Saved attached value */
994 oldval = lp->port_counters.val[cnt];
997 newval = lpifp->if_get_counter(lpifp, cnt);
998 /* Calculate diff and save new */
999 vsum += newval - oldval;
1004 * Add counter data which might be added by upper
1005 * layer protocols operating on logical interface.
1007 vsum += if_get_counter_default(ifp, cnt);
1010 * Add counter data from detached ports counters
1012 vsum += sc->detached_counters.val[cnt];
1019 * For direct output to child ports.
1022 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1023 const struct sockaddr *dst, struct route *ro)
1025 struct lagg_port *lp = ifp->if_lagg;
1027 switch (dst->sa_family) {
1028 case pseudo_AF_HDRCMPLT:
1030 return ((*lp->lp_output)(ifp, m, dst, ro));
1033 /* drop any other frames */
1039 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1041 struct lagg_port *lp;
1042 struct lagg_softc *sc;
1044 if ((lp = ifp->if_lagg) == NULL)
1046 /* If the ifnet is just being renamed, don't do anything. */
1047 if (ifp->if_flags & IFF_RENAMING)
1053 lp->lp_detaching = 1;
1054 lagg_port_destroy(lp, 1);
1056 VLAN_CAPABILITIES(sc->sc_ifp);
1060 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1062 struct lagg_softc *sc = lp->lp_softc;
1064 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1065 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1066 rp->rp_prio = lp->lp_prio;
1067 rp->rp_flags = lp->lp_flags;
1068 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1070 /* Add protocol specific flags */
1071 switch (sc->sc_proto) {
1072 case LAGG_PROTO_FAILOVER:
1073 if (lp == sc->sc_primary)
1074 rp->rp_flags |= LAGG_PORT_MASTER;
1075 if (lp == lagg_link_active(sc, sc->sc_primary))
1076 rp->rp_flags |= LAGG_PORT_ACTIVE;
1079 case LAGG_PROTO_ROUNDROBIN:
1080 case LAGG_PROTO_LOADBALANCE:
1081 case LAGG_PROTO_BROADCAST:
1082 if (LAGG_PORTACTIVE(lp))
1083 rp->rp_flags |= LAGG_PORT_ACTIVE;
1086 case LAGG_PROTO_LACP:
1087 /* LACP has a different definition of active */
1088 if (lacp_isactive(lp))
1089 rp->rp_flags |= LAGG_PORT_ACTIVE;
1090 if (lacp_iscollecting(lp))
1091 rp->rp_flags |= LAGG_PORT_COLLECTING;
1092 if (lacp_isdistributing(lp))
1093 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1100 lagg_init(void *xsc)
1102 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1103 struct ifnet *ifp = sc->sc_ifp;
1104 struct lagg_port *lp;
1107 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1112 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1115 * Update the port lladdrs if needed.
1116 * This might be if_setlladdr() notification
1117 * that lladdr has been changed.
1119 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1120 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1121 ETHER_ADDR_LEN) != 0)
1122 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1125 lagg_proto_init(sc);
1131 lagg_stop(struct lagg_softc *sc)
1133 struct ifnet *ifp = sc->sc_ifp;
1135 LAGG_XLOCK_ASSERT(sc);
1137 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1140 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1142 lagg_proto_stop(sc);
1146 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1148 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1149 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1150 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1151 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1152 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1153 struct ifreq *ifr = (struct ifreq *)data;
1154 struct lagg_port *lp;
1156 struct thread *td = curthread;
1158 int count, buflen, len, error = 0;
1160 bzero(&rpbuf, sizeof(rpbuf));
1165 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1166 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1167 ra->ra_proto = sc->sc_proto;
1168 lagg_proto_request(sc, &ra->ra_psc);
1171 len = min(ra->ra_size, buflen);
1172 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1173 if (len < sizeof(rpbuf))
1176 lagg_port2req(lp, &rpbuf);
1177 memcpy(buf, &rpbuf, sizeof(rpbuf));
1179 buf += sizeof(rpbuf);
1180 len -= sizeof(rpbuf);
1183 ra->ra_ports = count;
1184 ra->ra_size = count * sizeof(rpbuf);
1185 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1186 free(outbuf, M_TEMP);
1189 error = priv_check(td, PRIV_NET_LAGG);
1192 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1193 error = EPROTONOSUPPORT;
1198 lagg_proto_detach(sc);
1199 LAGG_UNLOCK_ASSERT();
1200 lagg_proto_attach(sc, ra->ra_proto);
1205 ro->ro_opts = sc->sc_opts;
1206 if (sc->sc_proto == LAGG_PROTO_LACP) {
1207 struct lacp_softc *lsc;
1209 lsc = (struct lacp_softc *)sc->sc_psc;
1210 if (lsc->lsc_debug.lsc_tx_test != 0)
1211 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1212 if (lsc->lsc_debug.lsc_rx_test != 0)
1213 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1214 if (lsc->lsc_strict_mode != 0)
1215 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1216 if (lsc->lsc_fast_timeout != 0)
1217 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1219 ro->ro_active = sc->sc_active;
1222 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1223 ro->ro_active += LAGG_PORTACTIVE(lp);
1225 ro->ro_bkt = sc->sc_bkt;
1226 ro->ro_flapping = sc->sc_flapping;
1227 ro->ro_flowid_shift = sc->flowid_shift;
1231 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1232 if (ro->ro_bkt == 0)
1233 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1235 sc->sc_bkt = ro->ro_bkt;
1237 error = priv_check(td, PRIV_NET_LAGG);
1240 if (ro->ro_opts == 0)
1243 * Set options. LACP options are stored in sc->sc_psc,
1248 switch (ro->ro_opts) {
1249 case LAGG_OPT_USE_FLOWID:
1250 case -LAGG_OPT_USE_FLOWID:
1251 case LAGG_OPT_FLOWIDSHIFT:
1255 case LAGG_OPT_LACP_TXTEST:
1256 case -LAGG_OPT_LACP_TXTEST:
1257 case LAGG_OPT_LACP_RXTEST:
1258 case -LAGG_OPT_LACP_RXTEST:
1259 case LAGG_OPT_LACP_STRICT:
1260 case -LAGG_OPT_LACP_STRICT:
1261 case LAGG_OPT_LACP_TIMEOUT:
1262 case -LAGG_OPT_LACP_TIMEOUT:
1273 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1274 /* Invalid combination of options specified. */
1277 break; /* Return from SIOCSLAGGOPTS. */
1280 * Store new options into sc->sc_opts except for
1281 * FLOWIDSHIFT and LACP options.
1284 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1285 sc->flowid_shift = ro->ro_flowid_shift;
1286 else if (ro->ro_opts > 0)
1287 sc->sc_opts |= ro->ro_opts;
1289 sc->sc_opts &= ~ro->ro_opts;
1291 struct lacp_softc *lsc;
1292 struct lacp_port *lp;
1294 lsc = (struct lacp_softc *)sc->sc_psc;
1296 switch (ro->ro_opts) {
1297 case LAGG_OPT_LACP_TXTEST:
1298 lsc->lsc_debug.lsc_tx_test = 1;
1300 case -LAGG_OPT_LACP_TXTEST:
1301 lsc->lsc_debug.lsc_tx_test = 0;
1303 case LAGG_OPT_LACP_RXTEST:
1304 lsc->lsc_debug.lsc_rx_test = 1;
1306 case -LAGG_OPT_LACP_RXTEST:
1307 lsc->lsc_debug.lsc_rx_test = 0;
1309 case LAGG_OPT_LACP_STRICT:
1310 lsc->lsc_strict_mode = 1;
1312 case -LAGG_OPT_LACP_STRICT:
1313 lsc->lsc_strict_mode = 0;
1315 case LAGG_OPT_LACP_TIMEOUT:
1317 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1318 lp->lp_state |= LACP_STATE_TIMEOUT;
1320 lsc->lsc_fast_timeout = 1;
1322 case -LAGG_OPT_LACP_TIMEOUT:
1324 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1325 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1327 lsc->lsc_fast_timeout = 0;
1333 case SIOCGLAGGFLAGS:
1336 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1337 rf->rf_flags |= LAGG_F_HASHL2;
1338 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1339 rf->rf_flags |= LAGG_F_HASHL3;
1340 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1341 rf->rf_flags |= LAGG_F_HASHL4;
1345 error = priv_check(td, PRIV_NET_LAGG);
1348 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1354 if (rf->rf_flags & LAGG_F_HASHL2)
1355 sc->sc_flags |= MBUF_HASHFLAG_L2;
1356 if (rf->rf_flags & LAGG_F_HASHL3)
1357 sc->sc_flags |= MBUF_HASHFLAG_L3;
1358 if (rf->rf_flags & LAGG_F_HASHL4)
1359 sc->sc_flags |= MBUF_HASHFLAG_L4;
1363 if (rp->rp_portname[0] == '\0' ||
1364 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1370 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1371 lp->lp_softc != sc) {
1378 lagg_port2req(lp, rp);
1383 error = priv_check(td, PRIV_NET_LAGG);
1386 if (rp->rp_portname[0] == '\0' ||
1387 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1393 * A laggport interface should not have inet6 address
1394 * because two interfaces with a valid link-local
1395 * scope zone must not be merged in any form. This
1396 * restriction is needed to prevent violation of
1397 * link-local scope zone. Attempts to add a laggport
1398 * interface which has inet6 addresses triggers
1399 * removal of all inet6 addresses on the member
1402 if (in6ifa_llaonifp(tpif)) {
1404 if_printf(sc->sc_ifp,
1405 "IPv6 addresses on %s have been removed "
1406 "before adding it as a member to prevent "
1407 "IPv6 address scope violation.\n",
1412 error = lagg_port_create(sc, tpif);
1415 VLAN_CAPABILITIES(ifp);
1417 case SIOCSLAGGDELPORT:
1418 error = priv_check(td, PRIV_NET_LAGG);
1421 if (rp->rp_portname[0] == '\0' ||
1422 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1428 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1429 lp->lp_softc != sc) {
1436 error = lagg_port_destroy(lp, 1);
1439 VLAN_CAPABILITIES(ifp);
1442 /* Set flags on ports too */
1444 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1445 lagg_setflags(lp, 1);
1448 if (!(ifp->if_flags & IFF_UP) &&
1449 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1451 * If interface is marked down and it is running,
1452 * then stop and disable it.
1456 } else if ((ifp->if_flags & IFF_UP) &&
1457 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1459 * If interface is marked up and it is stopped, then
1463 (*ifp->if_init)(sc);
1470 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1479 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1484 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1485 if (lp->lp_ioctl != NULL)
1486 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1488 lagg_capabilities(sc);
1490 VLAN_CAPABILITIES(ifp);
1496 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1497 if (lp->lp_ioctl != NULL)
1498 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1503 "failed to change MTU to %d on port %s, "
1504 "reverting all ports to original MTU (%d)\n",
1505 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1510 ifp->if_mtu = ifr->ifr_mtu;
1512 /* set every port back to the original MTU */
1513 ifr->ifr_mtu = ifp->if_mtu;
1514 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1515 if (lp->lp_ioctl != NULL)
1516 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1523 error = ether_ioctl(ifp, cmd, data);
1531 lagg_snd_tag_alloc(struct ifnet *ifp,
1532 union if_snd_tag_alloc_params *params,
1533 struct m_snd_tag **ppmt)
1535 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1536 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)
1547 return (EOPNOTSUPP);
1548 p = params->hdr.flowid >> sc->flowid_shift;
1550 lb = (struct lagg_lb *)sc->sc_psc;
1551 lp = lb->lb_ports[p];
1552 lp = lagg_link_active(sc, lp);
1554 case LAGG_PROTO_LACP:
1555 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1556 params->hdr.flowtype == M_HASHTYPE_NONE)
1557 return (EOPNOTSUPP);
1558 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1561 return (EOPNOTSUPP);
1564 return (EOPNOTSUPP);
1566 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1567 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1568 return (EOPNOTSUPP);
1570 /* forward allocation request */
1571 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1576 lagg_setmulti(struct lagg_port *lp)
1578 struct lagg_softc *sc = lp->lp_softc;
1579 struct ifnet *ifp = lp->lp_ifp;
1580 struct ifnet *scifp = sc->sc_ifp;
1582 struct ifmultiaddr *ifma;
1585 IF_ADDR_WLOCK(scifp);
1586 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1587 if (ifma->ifma_addr->sa_family != AF_LINK)
1589 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1591 IF_ADDR_WUNLOCK(scifp);
1594 bcopy(ifma->ifma_addr, &mc->mc_addr,
1595 ifma->ifma_addr->sa_len);
1596 mc->mc_addr.sdl_index = ifp->if_index;
1598 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1600 IF_ADDR_WUNLOCK(scifp);
1601 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1602 error = if_addmulti(ifp,
1603 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1611 lagg_clrmulti(struct lagg_port *lp)
1615 LAGG_XLOCK_ASSERT(lp->lp_softc);
1616 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1617 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1618 if (mc->mc_ifma && lp->lp_detaching == 0)
1619 if_delmulti_ifma(mc->mc_ifma);
1626 lagg_setcaps(struct lagg_port *lp, int cap)
1630 if (lp->lp_ifp->if_capenable == cap)
1632 if (lp->lp_ioctl == NULL)
1634 ifr.ifr_reqcap = cap;
1635 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1638 /* Handle a ref counted flag that should be set on the lagg port as well */
1640 lagg_setflag(struct lagg_port *lp, int flag, int status,
1641 int (*func)(struct ifnet *, int))
1643 struct lagg_softc *sc = lp->lp_softc;
1644 struct ifnet *scifp = sc->sc_ifp;
1645 struct ifnet *ifp = lp->lp_ifp;
1648 LAGG_XLOCK_ASSERT(sc);
1650 status = status ? (scifp->if_flags & flag) : 0;
1651 /* Now "status" contains the flag value or 0 */
1654 * See if recorded ports status is different from what
1655 * we want it to be. If it is, flip it. We record ports
1656 * status in lp_ifflags so that we won't clear ports flag
1657 * we haven't set. In fact, we don't clear or set ports
1658 * flags directly, but get or release references to them.
1659 * That's why we can be sure that recorded flags still are
1660 * in accord with actual ports flags.
1662 if (status != (lp->lp_ifflags & flag)) {
1663 error = (*func)(ifp, status);
1666 lp->lp_ifflags &= ~flag;
1667 lp->lp_ifflags |= status;
1673 * Handle IFF_* flags that require certain changes on the lagg port
1674 * if "status" is true, update ports flags respective to the lagg
1675 * if "status" is false, forcedly clear the flags set on port.
1678 lagg_setflags(struct lagg_port *lp, int status)
1682 for (i = 0; lagg_pflags[i].flag; i++) {
1683 error = lagg_setflag(lp, lagg_pflags[i].flag,
1684 status, lagg_pflags[i].func);
1692 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1694 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1698 /* We need a Tx algorithm and at least one port */
1699 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1702 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1706 ETHER_BPF_MTAP(ifp, m);
1708 error = lagg_proto_start(sc, m);
1712 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1718 * The ifp->if_qflush entry point for lagg(4) is no-op.
1721 lagg_qflush(struct ifnet *ifp __unused)
1725 static struct mbuf *
1726 lagg_input(struct ifnet *ifp, struct mbuf *m)
1728 struct lagg_port *lp = ifp->if_lagg;
1729 struct lagg_softc *sc = lp->lp_softc;
1730 struct ifnet *scifp = sc->sc_ifp;
1733 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1734 lp->lp_detaching != 0 ||
1735 sc->sc_proto == LAGG_PROTO_NONE) {
1741 ETHER_BPF_MTAP(scifp, m);
1743 m = lagg_proto_input(sc, lp, m);
1744 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1754 lagg_media_change(struct ifnet *ifp)
1756 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1758 if (sc->sc_ifflags & IFF_DEBUG)
1759 printf("%s\n", __func__);
1766 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1768 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1769 struct lagg_port *lp;
1771 imr->ifm_status = IFM_AVALID;
1772 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1775 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1776 if (LAGG_PORTACTIVE(lp))
1777 imr->ifm_status |= IFM_ACTIVE;
1783 lagg_linkstate(struct lagg_softc *sc)
1785 struct lagg_port *lp;
1786 int new_link = LINK_STATE_DOWN;
1789 LAGG_XLOCK_ASSERT(sc);
1791 /* LACP handles link state itself */
1792 if (sc->sc_proto == LAGG_PROTO_LACP)
1795 /* Our link is considered up if at least one of our ports is active */
1797 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1798 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1799 new_link = LINK_STATE_UP;
1804 if_link_state_change(sc->sc_ifp, new_link);
1806 /* Update if_baudrate to reflect the max possible speed */
1807 switch (sc->sc_proto) {
1808 case LAGG_PROTO_FAILOVER:
1809 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1810 sc->sc_primary->lp_ifp->if_baudrate : 0;
1812 case LAGG_PROTO_ROUNDROBIN:
1813 case LAGG_PROTO_LOADBALANCE:
1814 case LAGG_PROTO_BROADCAST:
1817 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1818 speed += lp->lp_ifp->if_baudrate;
1820 sc->sc_ifp->if_baudrate = speed;
1822 case LAGG_PROTO_LACP:
1823 /* LACP updates if_baudrate itself */
1829 lagg_port_state(struct ifnet *ifp, int state)
1831 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1832 struct lagg_softc *sc = NULL;
1841 lagg_proto_linkstate(sc, lp);
1846 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1848 struct lagg_port *lp_next, *rval = NULL;
1849 struct epoch_tracker net_et;
1852 * Search a port which reports an active link state.
1857 if (LAGG_PORTACTIVE(lp)) {
1861 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
1862 LAGG_PORTACTIVE(lp_next)) {
1868 epoch_enter_preempt(net_epoch_preempt, &net_et);
1869 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1870 if (LAGG_PORTACTIVE(lp_next)) {
1871 epoch_exit_preempt(net_epoch_preempt, &net_et);
1875 epoch_exit_preempt(net_epoch_preempt, &net_et);
1881 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1884 return (ifp->if_transmit)(ifp, m);
1888 * Simple round robin aggregation
1891 lagg_rr_attach(struct lagg_softc *sc)
1894 sc->sc_bkt_count = sc->sc_bkt;
1898 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1900 struct lagg_port *lp;
1903 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1904 sc->sc_bkt_count = sc->sc_bkt;
1906 if (sc->sc_bkt > 0) {
1907 atomic_subtract_int(&sc->sc_bkt_count, 1);
1908 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1909 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1913 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1916 lp = CK_SLIST_FIRST(&sc->sc_ports);
1919 lp = CK_SLIST_NEXT(lp, lp_entries);
1922 * Check the port's link state. This will return the next active
1923 * port if the link is down or the port is NULL.
1925 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1931 return (lagg_enqueue(lp->lp_ifp, m));
1934 static struct mbuf *
1935 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1937 struct ifnet *ifp = sc->sc_ifp;
1939 /* Just pass in the packet to our lagg device */
1940 m->m_pkthdr.rcvif = ifp;
1949 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1951 int active_ports = 0;
1954 struct lagg_port *lp, *last = NULL;
1958 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1959 if (!LAGG_PORTACTIVE(lp))
1965 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1972 ret = lagg_enqueue(last->lp_ifp, m0);
1984 if ((last = lagg_link_active(sc, last)) == NULL) {
1989 ret = lagg_enqueue(last->lp_ifp, m);
2000 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2002 struct ifnet *ifp = sc->sc_ifp;
2004 /* Just pass in the packet to our lagg device */
2005 m->m_pkthdr.rcvif = ifp;
2013 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2015 struct lagg_port *lp;
2017 /* Use the master port if active or the next available port */
2018 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2024 return (lagg_enqueue(lp->lp_ifp, m));
2027 static struct mbuf *
2028 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2030 struct ifnet *ifp = sc->sc_ifp;
2031 struct lagg_port *tmp_tp;
2033 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2034 m->m_pkthdr.rcvif = ifp;
2038 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2039 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2041 * If tmp_tp is null, we've received a packet when all
2042 * our links are down. Weird, but process it anyways.
2044 if ((tmp_tp == NULL || tmp_tp == lp)) {
2045 m->m_pkthdr.rcvif = ifp;
2058 lagg_lb_attach(struct lagg_softc *sc)
2060 struct lagg_port *lp;
2063 LAGG_XLOCK_ASSERT(sc);
2064 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2065 lb->lb_key = m_ether_tcpip_hash_init();
2068 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2069 lagg_lb_port_create(lp);
2073 lagg_lb_detach(struct lagg_softc *sc)
2077 lb = (struct lagg_lb *)sc->sc_psc;
2083 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2085 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2086 struct lagg_port *lp_next;
2090 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2092 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2095 if (i >= LAGG_MAX_PORTS) {
2099 if (sc->sc_ifflags & IFF_DEBUG)
2100 printf("%s: port %s at index %d\n",
2101 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2102 lb->lb_ports[i++] = lp_next;
2110 lagg_lb_port_create(struct lagg_port *lp)
2112 struct lagg_softc *sc = lp->lp_softc;
2113 return (lagg_lb_porttable(sc, NULL));
2117 lagg_lb_port_destroy(struct lagg_port *lp)
2119 struct lagg_softc *sc = lp->lp_softc;
2120 lagg_lb_porttable(sc, lp);
2124 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2126 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2127 struct lagg_port *lp = NULL;
2130 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2131 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2132 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2134 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2136 lp = lb->lb_ports[p];
2139 * Check the port's link state. This will return the next active
2140 * port if the link is down or the port is NULL.
2142 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2148 return (lagg_enqueue(lp->lp_ifp, m));
2151 static struct mbuf *
2152 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2154 struct ifnet *ifp = sc->sc_ifp;
2156 /* Just pass in the packet to our lagg device */
2157 m->m_pkthdr.rcvif = ifp;
2166 lagg_lacp_attach(struct lagg_softc *sc)
2168 struct lagg_port *lp;
2171 LAGG_XLOCK_ASSERT(sc);
2172 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2173 lacp_port_create(lp);
2177 lagg_lacp_detach(struct lagg_softc *sc)
2179 struct lagg_port *lp;
2182 LAGG_XLOCK_ASSERT(sc);
2183 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2184 lacp_port_destroy(lp);
2192 lagg_lacp_lladdr(struct lagg_softc *sc)
2194 struct lagg_port *lp;
2196 LAGG_SXLOCK_ASSERT(sc);
2198 /* purge all the lacp ports */
2199 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2200 lacp_port_destroy(lp);
2202 /* add them back in */
2203 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2204 lacp_port_create(lp);
2208 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2210 struct lagg_port *lp;
2212 lp = lacp_select_tx_port(sc, m);
2219 return (lagg_enqueue(lp->lp_ifp, m));
2222 static struct mbuf *
2223 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2225 struct ifnet *ifp = sc->sc_ifp;
2226 struct ether_header *eh;
2229 eh = mtod(m, struct ether_header *);
2230 etype = ntohs(eh->ether_type);
2232 /* Tap off LACP control messages */
2233 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2234 m = lacp_input(lp, m);
2240 * If the port is not collecting or not in the active aggregator then
2243 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2248 m->m_pkthdr.rcvif = ifp;