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 using numa */
268 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
269 #define V_def_use_numa VNET(def_use_numa)
270 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa, CTLFLAG_RWTUN,
271 &VNET_NAME(def_use_numa), 0,
272 "Use numa to steer flows");
274 /* Default value for flowid shift */
275 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
276 #define V_def_flowid_shift VNET(def_flowid_shift)
277 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
278 &VNET_NAME(def_flowid_shift), 0,
279 "Default setting for flowid shift for load sharing");
282 vnet_lagg_init(const void *unused __unused)
285 LAGG_LIST_LOCK_INIT();
286 SLIST_INIT(&V_lagg_list);
287 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
288 lagg_clone_destroy, 0);
290 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
291 vnet_lagg_init, NULL);
294 vnet_lagg_uninit(const void *unused __unused)
297 if_clone_detach(V_lagg_cloner);
298 LAGG_LIST_LOCK_DESTROY();
300 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
301 vnet_lagg_uninit, NULL);
304 lagg_modevent(module_t mod, int type, void *data)
309 lagg_input_p = lagg_input;
310 lagg_linkstate_p = lagg_port_state;
311 lagg_detach_cookie = EVENTHANDLER_REGISTER(
312 ifnet_departure_event, lagg_port_ifdetach, NULL,
313 EVENTHANDLER_PRI_ANY);
316 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
319 lagg_linkstate_p = NULL;
327 static moduledata_t lagg_mod = {
333 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
334 MODULE_VERSION(if_lagg, 1);
337 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
340 LAGG_XLOCK_ASSERT(sc);
341 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
344 if (sc->sc_ifflags & IFF_DEBUG)
345 if_printf(sc->sc_ifp, "using proto %u\n", pr);
347 if (lagg_protos[pr].pr_attach != NULL)
348 lagg_protos[pr].pr_attach(sc);
353 lagg_proto_detach(struct lagg_softc *sc)
357 LAGG_XLOCK_ASSERT(sc);
359 sc->sc_proto = LAGG_PROTO_NONE;
361 if (lagg_protos[pr].pr_detach != NULL)
362 lagg_protos[pr].pr_detach(sc);
366 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
369 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
373 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
376 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
380 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
383 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
386 return (lagg_protos[sc->sc_proto].pr_addport(lp));
390 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
393 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
394 lagg_protos[sc->sc_proto].pr_delport(lp);
398 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
401 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
402 lagg_protos[sc->sc_proto].pr_linkstate(lp);
406 lagg_proto_init(struct lagg_softc *sc)
409 if (lagg_protos[sc->sc_proto].pr_init != NULL)
410 lagg_protos[sc->sc_proto].pr_init(sc);
414 lagg_proto_stop(struct lagg_softc *sc)
417 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
418 lagg_protos[sc->sc_proto].pr_stop(sc);
422 lagg_proto_lladdr(struct lagg_softc *sc)
425 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
426 lagg_protos[sc->sc_proto].pr_lladdr(sc);
430 lagg_proto_request(struct lagg_softc *sc, void *v)
433 if (lagg_protos[sc->sc_proto].pr_request != NULL)
434 lagg_protos[sc->sc_proto].pr_request(sc, v);
438 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
441 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
442 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
446 * This routine is run via an vlan
450 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
452 struct lagg_softc *sc = ifp->if_softc;
453 struct lagg_port *lp;
455 if (ifp->if_softc != arg) /* Not our event */
459 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
460 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
465 * This routine is run via an vlan
469 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
471 struct lagg_softc *sc = ifp->if_softc;
472 struct lagg_port *lp;
474 if (ifp->if_softc != arg) /* Not our event */
478 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
479 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
484 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
486 struct lagg_softc *sc;
488 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
490 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
491 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
499 if (V_def_use_flowid)
500 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
502 sc->sc_opts |= LAGG_OPT_USE_NUMA;
503 sc->flowid_shift = V_def_flowid_shift;
505 /* Hash all layers by default */
506 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
508 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
510 CK_SLIST_INIT(&sc->sc_ports);
512 /* Initialise pseudo media types */
513 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
515 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
516 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
518 if_initname(ifp, laggname, unit);
520 ifp->if_transmit = lagg_transmit;
521 ifp->if_qflush = lagg_qflush;
522 ifp->if_init = lagg_init;
523 ifp->if_ioctl = lagg_ioctl;
524 ifp->if_get_counter = lagg_get_counter;
525 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
527 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
528 ifp->if_snd_tag_free = lagg_snd_tag_free;
530 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
533 * Attach as an ordinary ethernet device, children will be attached
534 * as special device IFT_IEEE8023ADLAG.
536 ether_ifattach(ifp, eaddr);
538 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
539 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
540 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
541 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
543 /* Insert into the global list of laggs */
545 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
553 lagg_clone_destroy(struct ifnet *ifp)
555 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
556 struct lagg_port *lp;
559 sc->sc_destroying = 1;
561 ifp->if_flags &= ~IFF_UP;
563 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
564 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
566 /* Shutdown and remove lagg ports */
567 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
568 lagg_port_destroy(lp, 1);
570 /* Unhook the aggregation protocol */
571 lagg_proto_detach(sc);
574 ifmedia_removeall(&sc->sc_media);
579 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
587 lagg_capabilities(struct lagg_softc *sc)
589 struct lagg_port *lp;
592 struct ifnet_hw_tsomax hw_tsomax;
594 LAGG_XLOCK_ASSERT(sc);
596 /* Get common enabled capabilities for the lagg ports */
598 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
599 ena &= lp->lp_ifp->if_capenable;
600 ena = (ena == ~0 ? 0 : ena);
603 * Apply common enabled capabilities back to the lagg ports.
604 * May require several iterations if they are dependent.
608 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
609 lagg_setcaps(lp, ena);
610 ena &= lp->lp_ifp->if_capenable;
612 } while (pena != ena);
614 /* Get other capabilities from the lagg ports */
617 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
618 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
619 cap &= lp->lp_ifp->if_capabilities;
620 hwa &= lp->lp_ifp->if_hwassist;
621 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
623 cap = (cap == ~0 ? 0 : cap);
624 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
626 if (sc->sc_ifp->if_capabilities != cap ||
627 sc->sc_ifp->if_capenable != ena ||
628 sc->sc_ifp->if_hwassist != hwa ||
629 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
630 sc->sc_ifp->if_capabilities = cap;
631 sc->sc_ifp->if_capenable = ena;
632 sc->sc_ifp->if_hwassist = hwa;
633 getmicrotime(&sc->sc_ifp->if_lastchange);
635 if (sc->sc_ifflags & IFF_DEBUG)
636 if_printf(sc->sc_ifp,
637 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
642 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
644 struct lagg_softc *sc_ptr;
645 struct lagg_port *lp, *tlp;
647 int error, i, oldmtu;
650 LAGG_XLOCK_ASSERT(sc);
652 if (sc->sc_ifp == ifp) {
653 if_printf(sc->sc_ifp,
654 "cannot add a lagg to itself as a port\n");
658 /* Limit the maximal number of lagg ports */
659 if (sc->sc_count >= LAGG_MAX_PORTS)
662 /* Check if port has already been associated to a lagg */
663 if (ifp->if_lagg != NULL) {
664 /* Port is already in the current lagg? */
665 lp = (struct lagg_port *)ifp->if_lagg;
666 if (lp->lp_softc == sc)
671 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
672 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
673 return (EPROTONOSUPPORT);
675 /* Allow the first Ethernet member to define the MTU */
677 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
678 sc->sc_ifp->if_mtu = ifp->if_mtu;
679 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
680 if (ifp->if_ioctl == NULL) {
681 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
685 oldmtu = ifp->if_mtu;
686 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
687 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
688 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
690 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
694 ifr.ifr_mtu = oldmtu;
697 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
700 /* Check if port is a stacked lagg */
702 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
703 if (ifp == sc_ptr->sc_ifp) {
707 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
710 /* XXX disable stacking for the moment, its untested */
711 #ifdef LAGG_PORT_STACKING
712 lp->lp_flags |= LAGG_PORT_STACK;
713 if (lagg_port_checkstacking(sc_ptr) >=
718 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
730 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
731 lp->lp_ifcapenable = ifp->if_capenable;
732 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
733 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
734 lagg_proto_lladdr(sc);
735 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
737 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
739 lagg_setflags(lp, 1);
741 if (CK_SLIST_EMPTY(&sc->sc_ports))
744 /* Change the interface type */
745 lp->lp_iftype = ifp->if_type;
746 ifp->if_type = IFT_IEEE8023ADLAG;
748 lp->lp_ioctl = ifp->if_ioctl;
749 ifp->if_ioctl = lagg_port_ioctl;
750 lp->lp_output = ifp->if_output;
751 ifp->if_output = lagg_port_output;
753 /* Read port counters */
754 pval = lp->port_counters.val;
755 for (i = 0; i < IFCOUNTERS; i++, pval++)
756 *pval = ifp->if_get_counter(ifp, i);
759 * Insert into the list of ports.
760 * Keep ports sorted by if_index. It is handy, when configuration
761 * is predictable and `ifconfig laggN create ...` command
762 * will lead to the same result each time.
764 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
765 if (tlp->lp_ifp->if_index < ifp->if_index && (
766 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
767 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
772 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
774 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
780 if ((error = lagg_proto_addport(sc, lp)) != 0) {
781 /* Remove the port, without calling pr_delport. */
782 lagg_port_destroy(lp, 0);
784 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
788 /* Update lagg capabilities */
789 lagg_capabilities(sc);
795 #ifdef LAGG_PORT_STACKING
797 lagg_port_checkstacking(struct lagg_softc *sc)
799 struct lagg_softc *sc_ptr;
800 struct lagg_port *lp;
803 LAGG_SXLOCK_ASSERT(sc);
804 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
805 if (lp->lp_flags & LAGG_PORT_STACK) {
806 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
807 m = MAX(m, lagg_port_checkstacking(sc_ptr));
816 lagg_port_destroy_cb(epoch_context_t ec)
818 struct lagg_port *lp;
821 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
829 lagg_port_destroy(struct lagg_port *lp, int rundelport)
831 struct lagg_softc *sc = lp->lp_softc;
832 struct lagg_port *lp_ptr, *lp0;
833 struct ifnet *ifp = lp->lp_ifp;
834 uint64_t *pval, vdiff;
837 LAGG_XLOCK_ASSERT(sc);
840 lagg_proto_delport(sc, lp);
842 if (lp->lp_detaching == 0)
845 /* Restore interface */
846 ifp->if_type = lp->lp_iftype;
847 ifp->if_ioctl = lp->lp_ioctl;
848 ifp->if_output = lp->lp_output;
851 /* Update detached port counters */
852 pval = lp->port_counters.val;
853 for (i = 0; i < IFCOUNTERS; i++, pval++) {
854 vdiff = ifp->if_get_counter(ifp, i) - *pval;
855 sc->detached_counters.val[i] += vdiff;
858 /* Finally, remove the port from the lagg */
859 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
862 /* Update the primary interface */
863 if (lp == sc->sc_primary) {
864 uint8_t lladdr[ETHER_ADDR_LEN];
866 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
867 bzero(&lladdr, ETHER_ADDR_LEN);
869 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
870 sc->sc_primary = lp0;
871 if (sc->sc_destroying == 0) {
872 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
873 lagg_proto_lladdr(sc);
874 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
878 * Update lladdr for each port (new primary needs update
879 * as well, to switch from old lladdr to its 'real' one)
881 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
882 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
886 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
888 if (lp->lp_detaching == 0) {
889 lagg_setflags(lp, 0);
890 lagg_setcaps(lp, lp->lp_ifcapenable);
891 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
895 * free port and release it's ifnet reference after a grace period has
898 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
899 /* Update lagg capabilities */
900 lagg_capabilities(sc);
907 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
909 struct lagg_reqport *rp = (struct lagg_reqport *)data;
910 struct lagg_softc *sc;
911 struct lagg_port *lp = NULL;
914 /* Should be checked by the caller */
915 if (ifp->if_type != IFT_IEEE8023ADLAG ||
916 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
921 if (rp->rp_portname[0] == '\0' ||
922 ifunit(rp->rp_portname) != ifp) {
928 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
934 lagg_port2req(lp, rp);
939 if (lp->lp_ioctl == NULL) {
943 error = (*lp->lp_ioctl)(ifp, cmd, data);
947 /* Update lagg interface capabilities */
949 lagg_capabilities(sc);
951 VLAN_CAPABILITIES(sc->sc_ifp);
955 /* Do not allow the MTU to be changed once joined */
966 if (lp != NULL && lp->lp_ioctl != NULL)
967 return ((*lp->lp_ioctl)(ifp, cmd, data));
973 * Requests counter @cnt data.
975 * Counter value is calculated the following way:
976 * 1) for each port, sum difference between current and "initial" measurements.
977 * 2) add lagg logical interface counters.
978 * 3) add data from detached_counters array.
980 * We also do the following things on ports attach/detach:
981 * 1) On port attach we store all counters it has into port_counter array.
982 * 2) On port detach we add the different between "initial" and
983 * current counters data to detached_counters array.
986 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
988 struct lagg_softc *sc;
989 struct lagg_port *lp;
991 uint64_t newval, oldval, vsum;
993 /* Revise this when we've got non-generic counters. */
994 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
996 sc = (struct lagg_softc *)ifp->if_softc;
1000 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1001 /* Saved attached value */
1002 oldval = lp->port_counters.val[cnt];
1005 newval = lpifp->if_get_counter(lpifp, cnt);
1006 /* Calculate diff and save new */
1007 vsum += newval - oldval;
1012 * Add counter data which might be added by upper
1013 * layer protocols operating on logical interface.
1015 vsum += if_get_counter_default(ifp, cnt);
1018 * Add counter data from detached ports counters
1020 vsum += sc->detached_counters.val[cnt];
1027 * For direct output to child ports.
1030 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1031 const struct sockaddr *dst, struct route *ro)
1033 struct lagg_port *lp = ifp->if_lagg;
1035 switch (dst->sa_family) {
1036 case pseudo_AF_HDRCMPLT:
1038 return ((*lp->lp_output)(ifp, m, dst, ro));
1041 /* drop any other frames */
1047 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1049 struct lagg_port *lp;
1050 struct lagg_softc *sc;
1052 if ((lp = ifp->if_lagg) == NULL)
1054 /* If the ifnet is just being renamed, don't do anything. */
1055 if (ifp->if_flags & IFF_RENAMING)
1061 lp->lp_detaching = 1;
1062 lagg_port_destroy(lp, 1);
1064 VLAN_CAPABILITIES(sc->sc_ifp);
1068 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1070 struct lagg_softc *sc = lp->lp_softc;
1072 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1073 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1074 rp->rp_prio = lp->lp_prio;
1075 rp->rp_flags = lp->lp_flags;
1076 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1078 /* Add protocol specific flags */
1079 switch (sc->sc_proto) {
1080 case LAGG_PROTO_FAILOVER:
1081 if (lp == sc->sc_primary)
1082 rp->rp_flags |= LAGG_PORT_MASTER;
1083 if (lp == lagg_link_active(sc, sc->sc_primary))
1084 rp->rp_flags |= LAGG_PORT_ACTIVE;
1087 case LAGG_PROTO_ROUNDROBIN:
1088 case LAGG_PROTO_LOADBALANCE:
1089 case LAGG_PROTO_BROADCAST:
1090 if (LAGG_PORTACTIVE(lp))
1091 rp->rp_flags |= LAGG_PORT_ACTIVE;
1094 case LAGG_PROTO_LACP:
1095 /* LACP has a different definition of active */
1096 if (lacp_isactive(lp))
1097 rp->rp_flags |= LAGG_PORT_ACTIVE;
1098 if (lacp_iscollecting(lp))
1099 rp->rp_flags |= LAGG_PORT_COLLECTING;
1100 if (lacp_isdistributing(lp))
1101 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1108 lagg_init(void *xsc)
1110 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1111 struct ifnet *ifp = sc->sc_ifp;
1112 struct lagg_port *lp;
1115 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1120 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1123 * Update the port lladdrs if needed.
1124 * This might be if_setlladdr() notification
1125 * that lladdr has been changed.
1127 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1128 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1129 ETHER_ADDR_LEN) != 0)
1130 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1133 lagg_proto_init(sc);
1139 lagg_stop(struct lagg_softc *sc)
1141 struct ifnet *ifp = sc->sc_ifp;
1143 LAGG_XLOCK_ASSERT(sc);
1145 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1148 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1150 lagg_proto_stop(sc);
1154 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1156 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1157 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1158 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1159 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1160 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1161 struct ifreq *ifr = (struct ifreq *)data;
1162 struct lagg_port *lp;
1164 struct thread *td = curthread;
1166 int count, buflen, len, error = 0;
1168 bzero(&rpbuf, sizeof(rpbuf));
1173 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1174 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1175 ra->ra_proto = sc->sc_proto;
1176 lagg_proto_request(sc, &ra->ra_psc);
1179 len = min(ra->ra_size, buflen);
1180 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1181 if (len < sizeof(rpbuf))
1184 lagg_port2req(lp, &rpbuf);
1185 memcpy(buf, &rpbuf, sizeof(rpbuf));
1187 buf += sizeof(rpbuf);
1188 len -= sizeof(rpbuf);
1191 ra->ra_ports = count;
1192 ra->ra_size = count * sizeof(rpbuf);
1193 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1194 free(outbuf, M_TEMP);
1197 error = priv_check(td, PRIV_NET_LAGG);
1200 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1201 error = EPROTONOSUPPORT;
1206 lagg_proto_detach(sc);
1207 LAGG_UNLOCK_ASSERT();
1208 lagg_proto_attach(sc, ra->ra_proto);
1213 ro->ro_opts = sc->sc_opts;
1214 if (sc->sc_proto == LAGG_PROTO_LACP) {
1215 struct lacp_softc *lsc;
1217 lsc = (struct lacp_softc *)sc->sc_psc;
1218 if (lsc->lsc_debug.lsc_tx_test != 0)
1219 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1220 if (lsc->lsc_debug.lsc_rx_test != 0)
1221 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1222 if (lsc->lsc_strict_mode != 0)
1223 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1224 if (lsc->lsc_fast_timeout != 0)
1225 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1227 ro->ro_active = sc->sc_active;
1230 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1231 ro->ro_active += LAGG_PORTACTIVE(lp);
1233 ro->ro_bkt = sc->sc_bkt;
1234 ro->ro_flapping = sc->sc_flapping;
1235 ro->ro_flowid_shift = sc->flowid_shift;
1239 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1240 if (ro->ro_bkt == 0)
1241 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1243 sc->sc_bkt = ro->ro_bkt;
1245 error = priv_check(td, PRIV_NET_LAGG);
1248 if (ro->ro_opts == 0)
1251 * Set options. LACP options are stored in sc->sc_psc,
1256 switch (ro->ro_opts) {
1257 case LAGG_OPT_USE_FLOWID:
1258 case -LAGG_OPT_USE_FLOWID:
1259 case LAGG_OPT_USE_NUMA:
1260 case -LAGG_OPT_USE_NUMA:
1261 case LAGG_OPT_FLOWIDSHIFT:
1265 case LAGG_OPT_LACP_TXTEST:
1266 case -LAGG_OPT_LACP_TXTEST:
1267 case LAGG_OPT_LACP_RXTEST:
1268 case -LAGG_OPT_LACP_RXTEST:
1269 case LAGG_OPT_LACP_STRICT:
1270 case -LAGG_OPT_LACP_STRICT:
1271 case LAGG_OPT_LACP_TIMEOUT:
1272 case -LAGG_OPT_LACP_TIMEOUT:
1283 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1284 /* Invalid combination of options specified. */
1287 break; /* Return from SIOCSLAGGOPTS. */
1290 * Store new options into sc->sc_opts except for
1291 * FLOWIDSHIFT and LACP options.
1294 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1295 sc->flowid_shift = ro->ro_flowid_shift;
1296 else if (ro->ro_opts > 0)
1297 sc->sc_opts |= ro->ro_opts;
1299 sc->sc_opts &= ~ro->ro_opts;
1301 struct lacp_softc *lsc;
1302 struct lacp_port *lp;
1304 lsc = (struct lacp_softc *)sc->sc_psc;
1306 switch (ro->ro_opts) {
1307 case LAGG_OPT_LACP_TXTEST:
1308 lsc->lsc_debug.lsc_tx_test = 1;
1310 case -LAGG_OPT_LACP_TXTEST:
1311 lsc->lsc_debug.lsc_tx_test = 0;
1313 case LAGG_OPT_LACP_RXTEST:
1314 lsc->lsc_debug.lsc_rx_test = 1;
1316 case -LAGG_OPT_LACP_RXTEST:
1317 lsc->lsc_debug.lsc_rx_test = 0;
1319 case LAGG_OPT_LACP_STRICT:
1320 lsc->lsc_strict_mode = 1;
1322 case -LAGG_OPT_LACP_STRICT:
1323 lsc->lsc_strict_mode = 0;
1325 case LAGG_OPT_LACP_TIMEOUT:
1327 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1328 lp->lp_state |= LACP_STATE_TIMEOUT;
1330 lsc->lsc_fast_timeout = 1;
1332 case -LAGG_OPT_LACP_TIMEOUT:
1334 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1335 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1337 lsc->lsc_fast_timeout = 0;
1343 case SIOCGLAGGFLAGS:
1346 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1347 rf->rf_flags |= LAGG_F_HASHL2;
1348 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1349 rf->rf_flags |= LAGG_F_HASHL3;
1350 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1351 rf->rf_flags |= LAGG_F_HASHL4;
1355 error = priv_check(td, PRIV_NET_LAGG);
1358 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1364 if (rf->rf_flags & LAGG_F_HASHL2)
1365 sc->sc_flags |= MBUF_HASHFLAG_L2;
1366 if (rf->rf_flags & LAGG_F_HASHL3)
1367 sc->sc_flags |= MBUF_HASHFLAG_L3;
1368 if (rf->rf_flags & LAGG_F_HASHL4)
1369 sc->sc_flags |= MBUF_HASHFLAG_L4;
1373 if (rp->rp_portname[0] == '\0' ||
1374 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1380 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1381 lp->lp_softc != sc) {
1388 lagg_port2req(lp, rp);
1393 error = priv_check(td, PRIV_NET_LAGG);
1396 if (rp->rp_portname[0] == '\0' ||
1397 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1403 * A laggport interface should not have inet6 address
1404 * because two interfaces with a valid link-local
1405 * scope zone must not be merged in any form. This
1406 * restriction is needed to prevent violation of
1407 * link-local scope zone. Attempts to add a laggport
1408 * interface which has inet6 addresses triggers
1409 * removal of all inet6 addresses on the member
1412 if (in6ifa_llaonifp(tpif)) {
1414 if_printf(sc->sc_ifp,
1415 "IPv6 addresses on %s have been removed "
1416 "before adding it as a member to prevent "
1417 "IPv6 address scope violation.\n",
1422 error = lagg_port_create(sc, tpif);
1425 VLAN_CAPABILITIES(ifp);
1427 case SIOCSLAGGDELPORT:
1428 error = priv_check(td, PRIV_NET_LAGG);
1431 if (rp->rp_portname[0] == '\0' ||
1432 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1438 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1439 lp->lp_softc != sc) {
1446 error = lagg_port_destroy(lp, 1);
1449 VLAN_CAPABILITIES(ifp);
1452 /* Set flags on ports too */
1454 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1455 lagg_setflags(lp, 1);
1458 if (!(ifp->if_flags & IFF_UP) &&
1459 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1461 * If interface is marked down and it is running,
1462 * then stop and disable it.
1466 } else if ((ifp->if_flags & IFF_UP) &&
1467 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1469 * If interface is marked up and it is stopped, then
1473 (*ifp->if_init)(sc);
1480 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1489 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1494 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1495 if (lp->lp_ioctl != NULL)
1496 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1498 lagg_capabilities(sc);
1500 VLAN_CAPABILITIES(ifp);
1506 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1507 if (lp->lp_ioctl != NULL)
1508 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1513 "failed to change MTU to %d on port %s, "
1514 "reverting all ports to original MTU (%d)\n",
1515 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1520 ifp->if_mtu = ifr->ifr_mtu;
1522 /* set every port back to the original MTU */
1523 ifr->ifr_mtu = ifp->if_mtu;
1524 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1525 if (lp->lp_ioctl != NULL)
1526 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1533 error = ether_ioctl(ifp, cmd, data);
1541 lagg_snd_tag_alloc(struct ifnet *ifp,
1542 union if_snd_tag_alloc_params *params,
1543 struct m_snd_tag **ppmt)
1545 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1546 struct lagg_port *lp;
1551 switch (sc->sc_proto) {
1552 case LAGG_PROTO_FAILOVER:
1553 lp = lagg_link_active(sc, sc->sc_primary);
1555 case LAGG_PROTO_LOADBALANCE:
1556 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1557 params->hdr.flowtype == M_HASHTYPE_NONE) {
1559 return (EOPNOTSUPP);
1561 p = params->hdr.flowid >> sc->flowid_shift;
1563 lb = (struct lagg_lb *)sc->sc_psc;
1564 lp = lb->lb_ports[p];
1565 lp = lagg_link_active(sc, lp);
1567 case LAGG_PROTO_LACP:
1568 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1569 params->hdr.flowtype == M_HASHTYPE_NONE) {
1571 return (EOPNOTSUPP);
1573 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1577 return (EOPNOTSUPP);
1581 return (EOPNOTSUPP);
1585 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1586 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1587 return (EOPNOTSUPP);
1589 /* forward allocation request */
1590 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1594 lagg_snd_tag_free(struct m_snd_tag *tag)
1596 tag->ifp->if_snd_tag_free(tag);
1602 lagg_setmulti(struct lagg_port *lp)
1604 struct lagg_softc *sc = lp->lp_softc;
1605 struct ifnet *ifp = lp->lp_ifp;
1606 struct ifnet *scifp = sc->sc_ifp;
1608 struct ifmultiaddr *ifma;
1611 IF_ADDR_WLOCK(scifp);
1612 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1613 if (ifma->ifma_addr->sa_family != AF_LINK)
1615 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1617 IF_ADDR_WUNLOCK(scifp);
1620 bcopy(ifma->ifma_addr, &mc->mc_addr,
1621 ifma->ifma_addr->sa_len);
1622 mc->mc_addr.sdl_index = ifp->if_index;
1624 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1626 IF_ADDR_WUNLOCK(scifp);
1627 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1628 error = if_addmulti(ifp,
1629 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1637 lagg_clrmulti(struct lagg_port *lp)
1641 LAGG_XLOCK_ASSERT(lp->lp_softc);
1642 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1643 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1644 if (mc->mc_ifma && lp->lp_detaching == 0)
1645 if_delmulti_ifma(mc->mc_ifma);
1652 lagg_setcaps(struct lagg_port *lp, int cap)
1656 if (lp->lp_ifp->if_capenable == cap)
1658 if (lp->lp_ioctl == NULL)
1660 ifr.ifr_reqcap = cap;
1661 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1664 /* Handle a ref counted flag that should be set on the lagg port as well */
1666 lagg_setflag(struct lagg_port *lp, int flag, int status,
1667 int (*func)(struct ifnet *, int))
1669 struct lagg_softc *sc = lp->lp_softc;
1670 struct ifnet *scifp = sc->sc_ifp;
1671 struct ifnet *ifp = lp->lp_ifp;
1674 LAGG_XLOCK_ASSERT(sc);
1676 status = status ? (scifp->if_flags & flag) : 0;
1677 /* Now "status" contains the flag value or 0 */
1680 * See if recorded ports status is different from what
1681 * we want it to be. If it is, flip it. We record ports
1682 * status in lp_ifflags so that we won't clear ports flag
1683 * we haven't set. In fact, we don't clear or set ports
1684 * flags directly, but get or release references to them.
1685 * That's why we can be sure that recorded flags still are
1686 * in accord with actual ports flags.
1688 if (status != (lp->lp_ifflags & flag)) {
1689 error = (*func)(ifp, status);
1692 lp->lp_ifflags &= ~flag;
1693 lp->lp_ifflags |= status;
1699 * Handle IFF_* flags that require certain changes on the lagg port
1700 * if "status" is true, update ports flags respective to the lagg
1701 * if "status" is false, forcedly clear the flags set on port.
1704 lagg_setflags(struct lagg_port *lp, int status)
1708 for (i = 0; lagg_pflags[i].flag; i++) {
1709 error = lagg_setflag(lp, lagg_pflags[i].flag,
1710 status, lagg_pflags[i].func);
1718 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1720 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1724 /* We need a Tx algorithm and at least one port */
1725 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1728 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1732 ETHER_BPF_MTAP(ifp, m);
1734 error = lagg_proto_start(sc, m);
1738 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1744 * The ifp->if_qflush entry point for lagg(4) is no-op.
1747 lagg_qflush(struct ifnet *ifp __unused)
1751 static struct mbuf *
1752 lagg_input(struct ifnet *ifp, struct mbuf *m)
1754 struct lagg_port *lp = ifp->if_lagg;
1755 struct lagg_softc *sc = lp->lp_softc;
1756 struct ifnet *scifp = sc->sc_ifp;
1759 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1760 lp->lp_detaching != 0 ||
1761 sc->sc_proto == LAGG_PROTO_NONE) {
1767 ETHER_BPF_MTAP(scifp, m);
1769 m = lagg_proto_input(sc, lp, m);
1770 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1780 lagg_media_change(struct ifnet *ifp)
1782 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1784 if (sc->sc_ifflags & IFF_DEBUG)
1785 printf("%s\n", __func__);
1792 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1794 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1795 struct lagg_port *lp;
1797 imr->ifm_status = IFM_AVALID;
1798 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1801 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1802 if (LAGG_PORTACTIVE(lp))
1803 imr->ifm_status |= IFM_ACTIVE;
1809 lagg_linkstate(struct lagg_softc *sc)
1811 struct lagg_port *lp;
1812 int new_link = LINK_STATE_DOWN;
1815 LAGG_XLOCK_ASSERT(sc);
1817 /* LACP handles link state itself */
1818 if (sc->sc_proto == LAGG_PROTO_LACP)
1821 /* Our link is considered up if at least one of our ports is active */
1823 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1824 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1825 new_link = LINK_STATE_UP;
1830 if_link_state_change(sc->sc_ifp, new_link);
1832 /* Update if_baudrate to reflect the max possible speed */
1833 switch (sc->sc_proto) {
1834 case LAGG_PROTO_FAILOVER:
1835 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1836 sc->sc_primary->lp_ifp->if_baudrate : 0;
1838 case LAGG_PROTO_ROUNDROBIN:
1839 case LAGG_PROTO_LOADBALANCE:
1840 case LAGG_PROTO_BROADCAST:
1843 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1844 speed += lp->lp_ifp->if_baudrate;
1846 sc->sc_ifp->if_baudrate = speed;
1848 case LAGG_PROTO_LACP:
1849 /* LACP updates if_baudrate itself */
1855 lagg_port_state(struct ifnet *ifp, int state)
1857 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1858 struct lagg_softc *sc = NULL;
1867 lagg_proto_linkstate(sc, lp);
1872 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1874 struct lagg_port *lp_next, *rval = NULL;
1877 * Search a port which reports an active link state.
1881 * This is called with either LAGG_RLOCK() held or
1882 * LAGG_XLOCK(sc) held.
1884 if (!in_epoch(net_epoch_preempt))
1885 LAGG_XLOCK_ASSERT(sc);
1889 if (LAGG_PORTACTIVE(lp)) {
1893 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
1894 LAGG_PORTACTIVE(lp_next)) {
1900 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1901 if (LAGG_PORTACTIVE(lp_next)) {
1910 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1913 return (ifp->if_transmit)(ifp, m);
1917 * Simple round robin aggregation
1920 lagg_rr_attach(struct lagg_softc *sc)
1923 sc->sc_bkt_count = sc->sc_bkt;
1927 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1929 struct lagg_port *lp;
1932 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1933 sc->sc_bkt_count = sc->sc_bkt;
1935 if (sc->sc_bkt > 0) {
1936 atomic_subtract_int(&sc->sc_bkt_count, 1);
1937 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1938 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1942 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1945 lp = CK_SLIST_FIRST(&sc->sc_ports);
1948 lp = CK_SLIST_NEXT(lp, lp_entries);
1951 * Check the port's link state. This will return the next active
1952 * port if the link is down or the port is NULL.
1954 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1960 return (lagg_enqueue(lp->lp_ifp, m));
1963 static struct mbuf *
1964 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1966 struct ifnet *ifp = sc->sc_ifp;
1968 /* Just pass in the packet to our lagg device */
1969 m->m_pkthdr.rcvif = ifp;
1978 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1980 int active_ports = 0;
1983 struct lagg_port *lp, *last = NULL;
1986 LAGG_RLOCK_ASSERT();
1987 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1988 if (!LAGG_PORTACTIVE(lp))
1994 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2001 ret = lagg_enqueue(last->lp_ifp, m0);
2012 if ((last = lagg_link_active(sc, last)) == NULL) {
2017 ret = lagg_enqueue(last->lp_ifp, m);
2028 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2030 struct ifnet *ifp = sc->sc_ifp;
2032 /* Just pass in the packet to our lagg device */
2033 m->m_pkthdr.rcvif = ifp;
2041 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2043 struct lagg_port *lp;
2045 /* Use the master port if active or the next available port */
2046 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2052 return (lagg_enqueue(lp->lp_ifp, m));
2055 static struct mbuf *
2056 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2058 struct ifnet *ifp = sc->sc_ifp;
2059 struct lagg_port *tmp_tp;
2061 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2062 m->m_pkthdr.rcvif = ifp;
2066 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2067 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2069 * If tmp_tp is null, we've received a packet when all
2070 * our links are down. Weird, but process it anyways.
2072 if ((tmp_tp == NULL || tmp_tp == lp)) {
2073 m->m_pkthdr.rcvif = ifp;
2086 lagg_lb_attach(struct lagg_softc *sc)
2088 struct lagg_port *lp;
2091 LAGG_XLOCK_ASSERT(sc);
2092 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2093 lb->lb_key = m_ether_tcpip_hash_init();
2096 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2097 lagg_lb_port_create(lp);
2101 lagg_lb_detach(struct lagg_softc *sc)
2105 lb = (struct lagg_lb *)sc->sc_psc;
2111 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2113 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2114 struct lagg_port *lp_next;
2118 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2119 LAGG_XLOCK_ASSERT(sc);
2120 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2123 if (i >= LAGG_MAX_PORTS) {
2127 if (sc->sc_ifflags & IFF_DEBUG)
2128 printf("%s: port %s at index %d\n",
2129 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2130 lb->lb_ports[i++] = lp_next;
2137 lagg_lb_port_create(struct lagg_port *lp)
2139 struct lagg_softc *sc = lp->lp_softc;
2140 return (lagg_lb_porttable(sc, NULL));
2144 lagg_lb_port_destroy(struct lagg_port *lp)
2146 struct lagg_softc *sc = lp->lp_softc;
2147 lagg_lb_porttable(sc, lp);
2151 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2153 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2154 struct lagg_port *lp = NULL;
2157 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2158 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2159 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2161 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2163 lp = lb->lb_ports[p];
2166 * Check the port's link state. This will return the next active
2167 * port if the link is down or the port is NULL.
2169 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2175 return (lagg_enqueue(lp->lp_ifp, m));
2178 static struct mbuf *
2179 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2181 struct ifnet *ifp = sc->sc_ifp;
2183 /* Just pass in the packet to our lagg device */
2184 m->m_pkthdr.rcvif = ifp;
2193 lagg_lacp_attach(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_create(lp);
2204 lagg_lacp_detach(struct lagg_softc *sc)
2206 struct lagg_port *lp;
2209 LAGG_XLOCK_ASSERT(sc);
2210 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2211 lacp_port_destroy(lp);
2219 lagg_lacp_lladdr(struct lagg_softc *sc)
2221 struct lagg_port *lp;
2223 LAGG_SXLOCK_ASSERT(sc);
2225 /* purge all the lacp ports */
2226 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2227 lacp_port_destroy(lp);
2229 /* add them back in */
2230 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2231 lacp_port_create(lp);
2235 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2237 struct lagg_port *lp;
2239 lp = lacp_select_tx_port(sc, m);
2246 return (lagg_enqueue(lp->lp_ifp, m));
2249 static struct mbuf *
2250 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2252 struct ifnet *ifp = sc->sc_ifp;
2253 struct ether_header *eh;
2256 eh = mtod(m, struct ether_header *);
2257 etype = ntohs(eh->ether_type);
2259 /* Tap off LACP control messages */
2260 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2261 m = lacp_input(lp, m);
2267 * If the port is not collecting or not in the active aggregator then
2270 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2275 m->m_pkthdr.rcvif = ifp;