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},
100 struct m_snd_tag *tag;
103 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
104 #define V_lagg_list VNET(lagg_list)
105 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
106 #define V_lagg_list_mtx VNET(lagg_list_mtx)
107 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
108 "if_lagg list", NULL, MTX_DEF)
109 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
110 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
111 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
112 eventhandler_tag lagg_detach_cookie = NULL;
114 static int lagg_clone_create(struct if_clone *, int, caddr_t);
115 static void lagg_clone_destroy(struct ifnet *);
116 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
117 #define V_lagg_cloner VNET(lagg_cloner)
118 static const char laggname[] = "lagg";
119 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
121 static void lagg_capabilities(struct lagg_softc *);
122 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
123 static int lagg_port_destroy(struct lagg_port *, int);
124 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
125 static void lagg_linkstate(struct lagg_softc *);
126 static void lagg_port_state(struct ifnet *, int);
127 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
128 static int lagg_port_output(struct ifnet *, struct mbuf *,
129 const struct sockaddr *, struct route *);
130 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
131 #ifdef LAGG_PORT_STACKING
132 static int lagg_port_checkstacking(struct lagg_softc *);
134 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
135 static void lagg_init(void *);
136 static void lagg_stop(struct lagg_softc *);
137 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
139 static int lagg_snd_tag_alloc(struct ifnet *,
140 union if_snd_tag_alloc_params *,
141 struct m_snd_tag **);
142 static int lagg_snd_tag_modify(struct m_snd_tag *,
143 union if_snd_tag_modify_params *);
144 static int lagg_snd_tag_query(struct m_snd_tag *,
145 union if_snd_tag_query_params *);
146 static void lagg_snd_tag_free(struct m_snd_tag *);
147 static void lagg_ratelimit_query(struct ifnet *,
148 struct if_ratelimit_query_results *);
150 static int lagg_setmulti(struct lagg_port *);
151 static int lagg_clrmulti(struct lagg_port *);
152 static int lagg_setcaps(struct lagg_port *, int cap);
153 static int lagg_setflag(struct lagg_port *, int, int,
154 int (*func)(struct ifnet *, int));
155 static int lagg_setflags(struct lagg_port *, int status);
156 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
157 static int lagg_transmit(struct ifnet *, struct mbuf *);
158 static void lagg_qflush(struct ifnet *);
159 static int lagg_media_change(struct ifnet *);
160 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
161 static struct lagg_port *lagg_link_active(struct lagg_softc *,
164 /* Simple round robin */
165 static void lagg_rr_attach(struct lagg_softc *);
166 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
167 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
170 /* Active failover */
171 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
172 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
176 static void lagg_lb_attach(struct lagg_softc *);
177 static void lagg_lb_detach(struct lagg_softc *);
178 static int lagg_lb_port_create(struct lagg_port *);
179 static void lagg_lb_port_destroy(struct lagg_port *);
180 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
181 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
183 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
186 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
187 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
191 static void lagg_lacp_attach(struct lagg_softc *);
192 static void lagg_lacp_detach(struct lagg_softc *);
193 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
194 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
196 static void lagg_lacp_lladdr(struct lagg_softc *);
198 /* lagg protocol table */
199 static const struct lagg_proto {
201 void (*pr_attach)(struct lagg_softc *);
202 void (*pr_detach)(struct lagg_softc *);
203 int (*pr_start)(struct lagg_softc *, struct mbuf *);
204 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
206 int (*pr_addport)(struct lagg_port *);
207 void (*pr_delport)(struct lagg_port *);
208 void (*pr_linkstate)(struct lagg_port *);
209 void (*pr_init)(struct lagg_softc *);
210 void (*pr_stop)(struct lagg_softc *);
211 void (*pr_lladdr)(struct lagg_softc *);
212 void (*pr_request)(struct lagg_softc *, void *);
213 void (*pr_portreq)(struct lagg_port *, void *);
216 .pr_num = LAGG_PROTO_NONE
219 .pr_num = LAGG_PROTO_ROUNDROBIN,
220 .pr_attach = lagg_rr_attach,
221 .pr_start = lagg_rr_start,
222 .pr_input = lagg_rr_input,
225 .pr_num = LAGG_PROTO_FAILOVER,
226 .pr_start = lagg_fail_start,
227 .pr_input = lagg_fail_input,
230 .pr_num = LAGG_PROTO_LOADBALANCE,
231 .pr_attach = lagg_lb_attach,
232 .pr_detach = lagg_lb_detach,
233 .pr_start = lagg_lb_start,
234 .pr_input = lagg_lb_input,
235 .pr_addport = lagg_lb_port_create,
236 .pr_delport = lagg_lb_port_destroy,
239 .pr_num = LAGG_PROTO_LACP,
240 .pr_attach = lagg_lacp_attach,
241 .pr_detach = lagg_lacp_detach,
242 .pr_start = lagg_lacp_start,
243 .pr_input = lagg_lacp_input,
244 .pr_addport = lacp_port_create,
245 .pr_delport = lacp_port_destroy,
246 .pr_linkstate = lacp_linkstate,
247 .pr_init = lacp_init,
248 .pr_stop = lacp_stop,
249 .pr_lladdr = lagg_lacp_lladdr,
250 .pr_request = lacp_req,
251 .pr_portreq = lacp_portreq,
254 .pr_num = LAGG_PROTO_BROADCAST,
255 .pr_start = lagg_bcast_start,
256 .pr_input = lagg_bcast_input,
260 SYSCTL_DECL(_net_link);
261 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
264 /* Allow input on any failover links */
265 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
266 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
267 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
268 &VNET_NAME(lagg_failover_rx_all), 0,
269 "Accept input from any interface in a failover lagg");
271 /* Default value for using flowid */
272 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
273 #define V_def_use_flowid VNET(def_use_flowid)
274 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
275 &VNET_NAME(def_use_flowid), 0,
276 "Default setting for using flow id for load sharing");
278 /* Default value for using numa */
279 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
280 #define V_def_use_numa VNET(def_use_numa)
281 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa, CTLFLAG_RWTUN,
282 &VNET_NAME(def_use_numa), 0,
283 "Use numa to steer flows");
285 /* Default value for flowid shift */
286 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
287 #define V_def_flowid_shift VNET(def_flowid_shift)
288 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
289 &VNET_NAME(def_flowid_shift), 0,
290 "Default setting for flowid shift for load sharing");
293 vnet_lagg_init(const void *unused __unused)
296 LAGG_LIST_LOCK_INIT();
297 SLIST_INIT(&V_lagg_list);
298 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
299 lagg_clone_destroy, 0);
301 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
302 vnet_lagg_init, NULL);
305 vnet_lagg_uninit(const void *unused __unused)
308 if_clone_detach(V_lagg_cloner);
309 LAGG_LIST_LOCK_DESTROY();
311 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
312 vnet_lagg_uninit, NULL);
315 lagg_modevent(module_t mod, int type, void *data)
320 lagg_input_p = lagg_input;
321 lagg_linkstate_p = lagg_port_state;
322 lagg_detach_cookie = EVENTHANDLER_REGISTER(
323 ifnet_departure_event, lagg_port_ifdetach, NULL,
324 EVENTHANDLER_PRI_ANY);
327 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
330 lagg_linkstate_p = NULL;
338 static moduledata_t lagg_mod = {
344 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
345 MODULE_VERSION(if_lagg, 1);
348 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
351 LAGG_XLOCK_ASSERT(sc);
352 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
355 if (sc->sc_ifflags & IFF_DEBUG)
356 if_printf(sc->sc_ifp, "using proto %u\n", pr);
358 if (lagg_protos[pr].pr_attach != NULL)
359 lagg_protos[pr].pr_attach(sc);
364 lagg_proto_detach(struct lagg_softc *sc)
368 LAGG_XLOCK_ASSERT(sc);
370 sc->sc_proto = LAGG_PROTO_NONE;
372 if (lagg_protos[pr].pr_detach != NULL)
373 lagg_protos[pr].pr_detach(sc);
377 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
380 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
384 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
387 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
391 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
394 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
397 return (lagg_protos[sc->sc_proto].pr_addport(lp));
401 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
404 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
405 lagg_protos[sc->sc_proto].pr_delport(lp);
409 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
412 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
413 lagg_protos[sc->sc_proto].pr_linkstate(lp);
417 lagg_proto_init(struct lagg_softc *sc)
420 if (lagg_protos[sc->sc_proto].pr_init != NULL)
421 lagg_protos[sc->sc_proto].pr_init(sc);
425 lagg_proto_stop(struct lagg_softc *sc)
428 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
429 lagg_protos[sc->sc_proto].pr_stop(sc);
433 lagg_proto_lladdr(struct lagg_softc *sc)
436 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
437 lagg_protos[sc->sc_proto].pr_lladdr(sc);
441 lagg_proto_request(struct lagg_softc *sc, void *v)
444 if (lagg_protos[sc->sc_proto].pr_request != NULL)
445 lagg_protos[sc->sc_proto].pr_request(sc, v);
449 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
452 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
453 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
457 * This routine is run via an vlan
461 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
463 struct lagg_softc *sc = ifp->if_softc;
464 struct lagg_port *lp;
466 if (ifp->if_softc != arg) /* Not our event */
470 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
471 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
476 * This routine is run via an vlan
480 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
482 struct lagg_softc *sc = ifp->if_softc;
483 struct lagg_port *lp;
485 if (ifp->if_softc != arg) /* Not our event */
489 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
490 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
495 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
497 struct lagg_softc *sc;
499 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
501 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
502 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
510 if (V_def_use_flowid)
511 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
513 sc->sc_opts |= LAGG_OPT_USE_NUMA;
514 sc->flowid_shift = V_def_flowid_shift;
516 /* Hash all layers by default */
517 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
519 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
521 CK_SLIST_INIT(&sc->sc_ports);
523 /* Initialise pseudo media types */
524 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
526 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
527 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
529 if_initname(ifp, laggname, unit);
531 ifp->if_transmit = lagg_transmit;
532 ifp->if_qflush = lagg_qflush;
533 ifp->if_init = lagg_init;
534 ifp->if_ioctl = lagg_ioctl;
535 ifp->if_get_counter = lagg_get_counter;
536 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
538 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
539 ifp->if_snd_tag_modify = lagg_snd_tag_modify;
540 ifp->if_snd_tag_query = lagg_snd_tag_query;
541 ifp->if_snd_tag_free = lagg_snd_tag_free;
542 ifp->if_ratelimit_query = lagg_ratelimit_query;
544 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
547 * Attach as an ordinary ethernet device, children will be attached
548 * as special device IFT_IEEE8023ADLAG.
550 ether_ifattach(ifp, eaddr);
552 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
553 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
554 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
555 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
557 /* Insert into the global list of laggs */
559 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
567 lagg_clone_destroy(struct ifnet *ifp)
569 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
570 struct lagg_port *lp;
573 sc->sc_destroying = 1;
575 ifp->if_flags &= ~IFF_UP;
577 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
578 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
580 /* Shutdown and remove lagg ports */
581 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
582 lagg_port_destroy(lp, 1);
584 /* Unhook the aggregation protocol */
585 lagg_proto_detach(sc);
588 ifmedia_removeall(&sc->sc_media);
593 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
601 lagg_capabilities(struct lagg_softc *sc)
603 struct lagg_port *lp;
606 struct ifnet_hw_tsomax hw_tsomax;
608 LAGG_XLOCK_ASSERT(sc);
610 /* Get common enabled capabilities for the lagg ports */
612 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
613 ena &= lp->lp_ifp->if_capenable;
614 ena = (ena == ~0 ? 0 : ena);
617 * Apply common enabled capabilities back to the lagg ports.
618 * May require several iterations if they are dependent.
622 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
623 lagg_setcaps(lp, ena);
624 ena &= lp->lp_ifp->if_capenable;
626 } while (pena != ena);
628 /* Get other capabilities from the lagg ports */
631 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
632 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
633 cap &= lp->lp_ifp->if_capabilities;
634 hwa &= lp->lp_ifp->if_hwassist;
635 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
637 cap = (cap == ~0 ? 0 : cap);
638 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
640 if (sc->sc_ifp->if_capabilities != cap ||
641 sc->sc_ifp->if_capenable != ena ||
642 sc->sc_ifp->if_hwassist != hwa ||
643 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
644 sc->sc_ifp->if_capabilities = cap;
645 sc->sc_ifp->if_capenable = ena;
646 sc->sc_ifp->if_hwassist = hwa;
647 getmicrotime(&sc->sc_ifp->if_lastchange);
649 if (sc->sc_ifflags & IFF_DEBUG)
650 if_printf(sc->sc_ifp,
651 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
656 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
658 struct lagg_softc *sc_ptr;
659 struct lagg_port *lp, *tlp;
661 int error, i, oldmtu;
664 LAGG_XLOCK_ASSERT(sc);
666 if (sc->sc_ifp == ifp) {
667 if_printf(sc->sc_ifp,
668 "cannot add a lagg to itself as a port\n");
672 /* Limit the maximal number of lagg ports */
673 if (sc->sc_count >= LAGG_MAX_PORTS)
676 /* Check if port has already been associated to a lagg */
677 if (ifp->if_lagg != NULL) {
678 /* Port is already in the current lagg? */
679 lp = (struct lagg_port *)ifp->if_lagg;
680 if (lp->lp_softc == sc)
685 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
686 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
687 return (EPROTONOSUPPORT);
689 /* Allow the first Ethernet member to define the MTU */
691 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
692 sc->sc_ifp->if_mtu = ifp->if_mtu;
693 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
694 if (ifp->if_ioctl == NULL) {
695 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
699 oldmtu = ifp->if_mtu;
700 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
701 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
702 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
704 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
708 ifr.ifr_mtu = oldmtu;
711 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
714 /* Check if port is a stacked lagg */
716 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
717 if (ifp == sc_ptr->sc_ifp) {
721 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
724 /* XXX disable stacking for the moment, its untested */
725 #ifdef LAGG_PORT_STACKING
726 lp->lp_flags |= LAGG_PORT_STACK;
727 if (lagg_port_checkstacking(sc_ptr) >=
732 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
744 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
745 lp->lp_ifcapenable = ifp->if_capenable;
746 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
747 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
748 lagg_proto_lladdr(sc);
749 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
751 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
753 lagg_setflags(lp, 1);
755 if (CK_SLIST_EMPTY(&sc->sc_ports))
758 /* Change the interface type */
759 lp->lp_iftype = ifp->if_type;
760 ifp->if_type = IFT_IEEE8023ADLAG;
762 lp->lp_ioctl = ifp->if_ioctl;
763 ifp->if_ioctl = lagg_port_ioctl;
764 lp->lp_output = ifp->if_output;
765 ifp->if_output = lagg_port_output;
767 /* Read port counters */
768 pval = lp->port_counters.val;
769 for (i = 0; i < IFCOUNTERS; i++, pval++)
770 *pval = ifp->if_get_counter(ifp, i);
773 * Insert into the list of ports.
774 * Keep ports sorted by if_index. It is handy, when configuration
775 * is predictable and `ifconfig laggN create ...` command
776 * will lead to the same result each time.
778 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
779 if (tlp->lp_ifp->if_index < ifp->if_index && (
780 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
781 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
786 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
788 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
794 if ((error = lagg_proto_addport(sc, lp)) != 0) {
795 /* Remove the port, without calling pr_delport. */
796 lagg_port_destroy(lp, 0);
798 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
802 /* Update lagg capabilities */
803 lagg_capabilities(sc);
809 #ifdef LAGG_PORT_STACKING
811 lagg_port_checkstacking(struct lagg_softc *sc)
813 struct lagg_softc *sc_ptr;
814 struct lagg_port *lp;
817 LAGG_SXLOCK_ASSERT(sc);
818 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
819 if (lp->lp_flags & LAGG_PORT_STACK) {
820 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
821 m = MAX(m, lagg_port_checkstacking(sc_ptr));
830 lagg_port_destroy_cb(epoch_context_t ec)
832 struct lagg_port *lp;
835 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
843 lagg_port_destroy(struct lagg_port *lp, int rundelport)
845 struct lagg_softc *sc = lp->lp_softc;
846 struct lagg_port *lp_ptr, *lp0;
847 struct ifnet *ifp = lp->lp_ifp;
848 uint64_t *pval, vdiff;
851 LAGG_XLOCK_ASSERT(sc);
854 lagg_proto_delport(sc, lp);
856 if (lp->lp_detaching == 0)
859 /* Restore interface */
860 ifp->if_type = lp->lp_iftype;
861 ifp->if_ioctl = lp->lp_ioctl;
862 ifp->if_output = lp->lp_output;
865 /* Update detached port counters */
866 pval = lp->port_counters.val;
867 for (i = 0; i < IFCOUNTERS; i++, pval++) {
868 vdiff = ifp->if_get_counter(ifp, i) - *pval;
869 sc->detached_counters.val[i] += vdiff;
872 /* Finally, remove the port from the lagg */
873 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
876 /* Update the primary interface */
877 if (lp == sc->sc_primary) {
878 uint8_t lladdr[ETHER_ADDR_LEN];
880 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
881 bzero(&lladdr, ETHER_ADDR_LEN);
883 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
884 sc->sc_primary = lp0;
885 if (sc->sc_destroying == 0) {
886 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
887 lagg_proto_lladdr(sc);
888 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
892 * Update lladdr for each port (new primary needs update
893 * as well, to switch from old lladdr to its 'real' one)
895 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
896 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
900 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
902 if (lp->lp_detaching == 0) {
903 lagg_setflags(lp, 0);
904 lagg_setcaps(lp, lp->lp_ifcapenable);
905 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
909 * free port and release it's ifnet reference after a grace period has
912 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
913 /* Update lagg capabilities */
914 lagg_capabilities(sc);
921 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
923 struct lagg_reqport *rp = (struct lagg_reqport *)data;
924 struct lagg_softc *sc;
925 struct lagg_port *lp = NULL;
928 /* Should be checked by the caller */
929 if (ifp->if_type != IFT_IEEE8023ADLAG ||
930 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
935 if (rp->rp_portname[0] == '\0' ||
936 ifunit(rp->rp_portname) != ifp) {
942 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
948 lagg_port2req(lp, rp);
953 if (lp->lp_ioctl == NULL) {
957 error = (*lp->lp_ioctl)(ifp, cmd, data);
961 /* Update lagg interface capabilities */
963 lagg_capabilities(sc);
965 VLAN_CAPABILITIES(sc->sc_ifp);
969 /* Do not allow the MTU to be changed once joined */
980 if (lp != NULL && lp->lp_ioctl != NULL)
981 return ((*lp->lp_ioctl)(ifp, cmd, data));
987 * Requests counter @cnt data.
989 * Counter value is calculated the following way:
990 * 1) for each port, sum difference between current and "initial" measurements.
991 * 2) add lagg logical interface counters.
992 * 3) add data from detached_counters array.
994 * We also do the following things on ports attach/detach:
995 * 1) On port attach we store all counters it has into port_counter array.
996 * 2) On port detach we add the different between "initial" and
997 * current counters data to detached_counters array.
1000 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1002 struct lagg_softc *sc;
1003 struct lagg_port *lp;
1004 struct ifnet *lpifp;
1005 uint64_t newval, oldval, vsum;
1007 /* Revise this when we've got non-generic counters. */
1008 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1010 sc = (struct lagg_softc *)ifp->if_softc;
1014 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1015 /* Saved attached value */
1016 oldval = lp->port_counters.val[cnt];
1019 newval = lpifp->if_get_counter(lpifp, cnt);
1020 /* Calculate diff and save new */
1021 vsum += newval - oldval;
1026 * Add counter data which might be added by upper
1027 * layer protocols operating on logical interface.
1029 vsum += if_get_counter_default(ifp, cnt);
1032 * Add counter data from detached ports counters
1034 vsum += sc->detached_counters.val[cnt];
1041 * For direct output to child ports.
1044 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1045 const struct sockaddr *dst, struct route *ro)
1047 struct lagg_port *lp = ifp->if_lagg;
1049 switch (dst->sa_family) {
1050 case pseudo_AF_HDRCMPLT:
1052 return ((*lp->lp_output)(ifp, m, dst, ro));
1055 /* drop any other frames */
1061 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1063 struct lagg_port *lp;
1064 struct lagg_softc *sc;
1066 if ((lp = ifp->if_lagg) == NULL)
1068 /* If the ifnet is just being renamed, don't do anything. */
1069 if (ifp->if_flags & IFF_RENAMING)
1075 lp->lp_detaching = 1;
1076 lagg_port_destroy(lp, 1);
1078 VLAN_CAPABILITIES(sc->sc_ifp);
1082 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1084 struct lagg_softc *sc = lp->lp_softc;
1086 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1087 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1088 rp->rp_prio = lp->lp_prio;
1089 rp->rp_flags = lp->lp_flags;
1090 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1092 /* Add protocol specific flags */
1093 switch (sc->sc_proto) {
1094 case LAGG_PROTO_FAILOVER:
1095 if (lp == sc->sc_primary)
1096 rp->rp_flags |= LAGG_PORT_MASTER;
1097 if (lp == lagg_link_active(sc, sc->sc_primary))
1098 rp->rp_flags |= LAGG_PORT_ACTIVE;
1101 case LAGG_PROTO_ROUNDROBIN:
1102 case LAGG_PROTO_LOADBALANCE:
1103 case LAGG_PROTO_BROADCAST:
1104 if (LAGG_PORTACTIVE(lp))
1105 rp->rp_flags |= LAGG_PORT_ACTIVE;
1108 case LAGG_PROTO_LACP:
1109 /* LACP has a different definition of active */
1110 if (lacp_isactive(lp))
1111 rp->rp_flags |= LAGG_PORT_ACTIVE;
1112 if (lacp_iscollecting(lp))
1113 rp->rp_flags |= LAGG_PORT_COLLECTING;
1114 if (lacp_isdistributing(lp))
1115 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1122 lagg_init(void *xsc)
1124 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1125 struct ifnet *ifp = sc->sc_ifp;
1126 struct lagg_port *lp;
1129 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1134 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1137 * Update the port lladdrs if needed.
1138 * This might be if_setlladdr() notification
1139 * that lladdr has been changed.
1141 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1142 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1143 ETHER_ADDR_LEN) != 0)
1144 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1147 lagg_proto_init(sc);
1153 lagg_stop(struct lagg_softc *sc)
1155 struct ifnet *ifp = sc->sc_ifp;
1157 LAGG_XLOCK_ASSERT(sc);
1159 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1162 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1164 lagg_proto_stop(sc);
1168 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1170 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1171 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1172 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1173 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1174 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1175 struct ifreq *ifr = (struct ifreq *)data;
1176 struct lagg_port *lp;
1178 struct thread *td = curthread;
1180 int count, buflen, len, error = 0;
1182 bzero(&rpbuf, sizeof(rpbuf));
1187 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1188 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1189 ra->ra_proto = sc->sc_proto;
1190 lagg_proto_request(sc, &ra->ra_psc);
1193 len = min(ra->ra_size, buflen);
1194 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1195 if (len < sizeof(rpbuf))
1198 lagg_port2req(lp, &rpbuf);
1199 memcpy(buf, &rpbuf, sizeof(rpbuf));
1201 buf += sizeof(rpbuf);
1202 len -= sizeof(rpbuf);
1205 ra->ra_ports = count;
1206 ra->ra_size = count * sizeof(rpbuf);
1207 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1208 free(outbuf, M_TEMP);
1211 error = priv_check(td, PRIV_NET_LAGG);
1214 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1215 error = EPROTONOSUPPORT;
1220 lagg_proto_detach(sc);
1221 LAGG_UNLOCK_ASSERT();
1222 lagg_proto_attach(sc, ra->ra_proto);
1227 ro->ro_opts = sc->sc_opts;
1228 if (sc->sc_proto == LAGG_PROTO_LACP) {
1229 struct lacp_softc *lsc;
1231 lsc = (struct lacp_softc *)sc->sc_psc;
1232 if (lsc->lsc_debug.lsc_tx_test != 0)
1233 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1234 if (lsc->lsc_debug.lsc_rx_test != 0)
1235 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1236 if (lsc->lsc_strict_mode != 0)
1237 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1238 if (lsc->lsc_fast_timeout != 0)
1239 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1241 ro->ro_active = sc->sc_active;
1244 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1245 ro->ro_active += LAGG_PORTACTIVE(lp);
1247 ro->ro_bkt = sc->sc_bkt;
1248 ro->ro_flapping = sc->sc_flapping;
1249 ro->ro_flowid_shift = sc->flowid_shift;
1253 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1254 if (ro->ro_bkt == 0)
1255 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1257 sc->sc_bkt = ro->ro_bkt;
1259 error = priv_check(td, PRIV_NET_LAGG);
1262 if (ro->ro_opts == 0)
1265 * Set options. LACP options are stored in sc->sc_psc,
1270 switch (ro->ro_opts) {
1271 case LAGG_OPT_USE_FLOWID:
1272 case -LAGG_OPT_USE_FLOWID:
1273 case LAGG_OPT_USE_NUMA:
1274 case -LAGG_OPT_USE_NUMA:
1275 case LAGG_OPT_FLOWIDSHIFT:
1279 case LAGG_OPT_LACP_TXTEST:
1280 case -LAGG_OPT_LACP_TXTEST:
1281 case LAGG_OPT_LACP_RXTEST:
1282 case -LAGG_OPT_LACP_RXTEST:
1283 case LAGG_OPT_LACP_STRICT:
1284 case -LAGG_OPT_LACP_STRICT:
1285 case LAGG_OPT_LACP_TIMEOUT:
1286 case -LAGG_OPT_LACP_TIMEOUT:
1297 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1298 /* Invalid combination of options specified. */
1301 break; /* Return from SIOCSLAGGOPTS. */
1304 * Store new options into sc->sc_opts except for
1305 * FLOWIDSHIFT and LACP options.
1308 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1309 sc->flowid_shift = ro->ro_flowid_shift;
1310 else if (ro->ro_opts > 0)
1311 sc->sc_opts |= ro->ro_opts;
1313 sc->sc_opts &= ~ro->ro_opts;
1315 struct lacp_softc *lsc;
1316 struct lacp_port *lp;
1318 lsc = (struct lacp_softc *)sc->sc_psc;
1320 switch (ro->ro_opts) {
1321 case LAGG_OPT_LACP_TXTEST:
1322 lsc->lsc_debug.lsc_tx_test = 1;
1324 case -LAGG_OPT_LACP_TXTEST:
1325 lsc->lsc_debug.lsc_tx_test = 0;
1327 case LAGG_OPT_LACP_RXTEST:
1328 lsc->lsc_debug.lsc_rx_test = 1;
1330 case -LAGG_OPT_LACP_RXTEST:
1331 lsc->lsc_debug.lsc_rx_test = 0;
1333 case LAGG_OPT_LACP_STRICT:
1334 lsc->lsc_strict_mode = 1;
1336 case -LAGG_OPT_LACP_STRICT:
1337 lsc->lsc_strict_mode = 0;
1339 case LAGG_OPT_LACP_TIMEOUT:
1341 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1342 lp->lp_state |= LACP_STATE_TIMEOUT;
1344 lsc->lsc_fast_timeout = 1;
1346 case -LAGG_OPT_LACP_TIMEOUT:
1348 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1349 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1351 lsc->lsc_fast_timeout = 0;
1357 case SIOCGLAGGFLAGS:
1360 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1361 rf->rf_flags |= LAGG_F_HASHL2;
1362 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1363 rf->rf_flags |= LAGG_F_HASHL3;
1364 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1365 rf->rf_flags |= LAGG_F_HASHL4;
1369 error = priv_check(td, PRIV_NET_LAGG);
1372 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1378 if (rf->rf_flags & LAGG_F_HASHL2)
1379 sc->sc_flags |= MBUF_HASHFLAG_L2;
1380 if (rf->rf_flags & LAGG_F_HASHL3)
1381 sc->sc_flags |= MBUF_HASHFLAG_L3;
1382 if (rf->rf_flags & LAGG_F_HASHL4)
1383 sc->sc_flags |= MBUF_HASHFLAG_L4;
1387 if (rp->rp_portname[0] == '\0' ||
1388 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1394 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1395 lp->lp_softc != sc) {
1402 lagg_port2req(lp, rp);
1407 error = priv_check(td, PRIV_NET_LAGG);
1410 if (rp->rp_portname[0] == '\0' ||
1411 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1417 * A laggport interface should not have inet6 address
1418 * because two interfaces with a valid link-local
1419 * scope zone must not be merged in any form. This
1420 * restriction is needed to prevent violation of
1421 * link-local scope zone. Attempts to add a laggport
1422 * interface which has inet6 addresses triggers
1423 * removal of all inet6 addresses on the member
1426 if (in6ifa_llaonifp(tpif)) {
1428 if_printf(sc->sc_ifp,
1429 "IPv6 addresses on %s have been removed "
1430 "before adding it as a member to prevent "
1431 "IPv6 address scope violation.\n",
1436 error = lagg_port_create(sc, tpif);
1439 VLAN_CAPABILITIES(ifp);
1441 case SIOCSLAGGDELPORT:
1442 error = priv_check(td, PRIV_NET_LAGG);
1445 if (rp->rp_portname[0] == '\0' ||
1446 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1452 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1453 lp->lp_softc != sc) {
1460 error = lagg_port_destroy(lp, 1);
1463 VLAN_CAPABILITIES(ifp);
1466 /* Set flags on ports too */
1468 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1469 lagg_setflags(lp, 1);
1472 if (!(ifp->if_flags & IFF_UP) &&
1473 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1475 * If interface is marked down and it is running,
1476 * then stop and disable it.
1480 } else if ((ifp->if_flags & IFF_UP) &&
1481 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1483 * If interface is marked up and it is stopped, then
1487 (*ifp->if_init)(sc);
1494 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1503 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1508 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1509 if (lp->lp_ioctl != NULL)
1510 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1512 lagg_capabilities(sc);
1514 VLAN_CAPABILITIES(ifp);
1520 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1521 if (lp->lp_ioctl != NULL)
1522 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1527 "failed to change MTU to %d on port %s, "
1528 "reverting all ports to original MTU (%d)\n",
1529 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1534 ifp->if_mtu = ifr->ifr_mtu;
1536 /* set every port back to the original MTU */
1537 ifr->ifr_mtu = ifp->if_mtu;
1538 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1539 if (lp->lp_ioctl != NULL)
1540 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1547 error = ether_ioctl(ifp, cmd, data);
1554 static inline struct lagg_snd_tag *
1555 mst_to_lst(struct m_snd_tag *mst)
1558 return (__containerof(mst, struct lagg_snd_tag, com));
1562 * Look up the port used by a specific flow. This only works for lagg
1563 * protocols with deterministic port mappings (e.g. not roundrobin).
1564 * In addition protocols which use a hash to map flows to ports must
1565 * be configured to use the mbuf flowid rather than hashing packet
1568 static struct lagg_port *
1569 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype)
1571 struct lagg_softc *sc;
1572 struct lagg_port *lp;
1578 switch (sc->sc_proto) {
1579 case LAGG_PROTO_FAILOVER:
1580 return (lagg_link_active(sc, sc->sc_primary));
1581 case LAGG_PROTO_LOADBALANCE:
1582 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1583 flowtype == M_HASHTYPE_NONE)
1585 p = flowid >> sc->flowid_shift;
1587 lb = (struct lagg_lb *)sc->sc_psc;
1588 lp = lb->lb_ports[p];
1589 return (lagg_link_active(sc, lp));
1590 case LAGG_PROTO_LACP:
1591 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1592 flowtype == M_HASHTYPE_NONE)
1594 return (lacp_select_tx_port_by_hash(sc, flowid));
1601 lagg_snd_tag_alloc(struct ifnet *ifp,
1602 union if_snd_tag_alloc_params *params,
1603 struct m_snd_tag **ppmt)
1605 struct lagg_snd_tag *lst;
1606 struct lagg_softc *sc;
1607 struct lagg_port *lp;
1608 struct ifnet *lp_ifp;
1614 lp = lookup_snd_tag_port(ifp, params->hdr.flowid, params->hdr.flowtype);
1617 return (EOPNOTSUPP);
1619 if (lp->lp_ifp == NULL || lp->lp_ifp->if_snd_tag_alloc == NULL) {
1621 return (EOPNOTSUPP);
1623 lp_ifp = lp->lp_ifp;
1627 lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1633 error = lp_ifp->if_snd_tag_alloc(lp_ifp, params, &lst->tag);
1640 m_snd_tag_init(&lst->com, ifp);
1647 lagg_snd_tag_modify(struct m_snd_tag *mst,
1648 union if_snd_tag_modify_params *params)
1650 struct lagg_snd_tag *lst;
1652 lst = mst_to_lst(mst);
1653 return (lst->tag->ifp->if_snd_tag_modify(lst->tag, params));
1657 lagg_snd_tag_query(struct m_snd_tag *mst,
1658 union if_snd_tag_query_params *params)
1660 struct lagg_snd_tag *lst;
1662 lst = mst_to_lst(mst);
1663 return (lst->tag->ifp->if_snd_tag_query(lst->tag, params));
1667 lagg_snd_tag_free(struct m_snd_tag *mst)
1669 struct lagg_snd_tag *lst;
1671 lst = mst_to_lst(mst);
1672 m_snd_tag_rele(lst->tag);
1677 lagg_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
1680 * For lagg, we have an indirect
1681 * interface. The caller needs to
1682 * get a ratelimit tag on the actual
1683 * interface the flow will go on.
1685 q->rate_table = NULL;
1686 q->flags = RT_IS_INDIRECT;
1688 q->number_of_rates = 0;
1693 lagg_setmulti(struct lagg_port *lp)
1695 struct lagg_softc *sc = lp->lp_softc;
1696 struct ifnet *ifp = lp->lp_ifp;
1697 struct ifnet *scifp = sc->sc_ifp;
1699 struct ifmultiaddr *ifma;
1702 IF_ADDR_WLOCK(scifp);
1703 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1704 if (ifma->ifma_addr->sa_family != AF_LINK)
1706 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1708 IF_ADDR_WUNLOCK(scifp);
1711 bcopy(ifma->ifma_addr, &mc->mc_addr,
1712 ifma->ifma_addr->sa_len);
1713 mc->mc_addr.sdl_index = ifp->if_index;
1715 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1717 IF_ADDR_WUNLOCK(scifp);
1718 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1719 error = if_addmulti(ifp,
1720 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1728 lagg_clrmulti(struct lagg_port *lp)
1732 LAGG_XLOCK_ASSERT(lp->lp_softc);
1733 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1734 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1735 if (mc->mc_ifma && lp->lp_detaching == 0)
1736 if_delmulti_ifma(mc->mc_ifma);
1743 lagg_setcaps(struct lagg_port *lp, int cap)
1747 if (lp->lp_ifp->if_capenable == cap)
1749 if (lp->lp_ioctl == NULL)
1751 ifr.ifr_reqcap = cap;
1752 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1755 /* Handle a ref counted flag that should be set on the lagg port as well */
1757 lagg_setflag(struct lagg_port *lp, int flag, int status,
1758 int (*func)(struct ifnet *, int))
1760 struct lagg_softc *sc = lp->lp_softc;
1761 struct ifnet *scifp = sc->sc_ifp;
1762 struct ifnet *ifp = lp->lp_ifp;
1765 LAGG_XLOCK_ASSERT(sc);
1767 status = status ? (scifp->if_flags & flag) : 0;
1768 /* Now "status" contains the flag value or 0 */
1771 * See if recorded ports status is different from what
1772 * we want it to be. If it is, flip it. We record ports
1773 * status in lp_ifflags so that we won't clear ports flag
1774 * we haven't set. In fact, we don't clear or set ports
1775 * flags directly, but get or release references to them.
1776 * That's why we can be sure that recorded flags still are
1777 * in accord with actual ports flags.
1779 if (status != (lp->lp_ifflags & flag)) {
1780 error = (*func)(ifp, status);
1783 lp->lp_ifflags &= ~flag;
1784 lp->lp_ifflags |= status;
1790 * Handle IFF_* flags that require certain changes on the lagg port
1791 * if "status" is true, update ports flags respective to the lagg
1792 * if "status" is false, forcedly clear the flags set on port.
1795 lagg_setflags(struct lagg_port *lp, int status)
1799 for (i = 0; lagg_pflags[i].flag; i++) {
1800 error = lagg_setflag(lp, lagg_pflags[i].flag,
1801 status, lagg_pflags[i].func);
1809 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1811 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1815 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
1816 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1819 /* We need a Tx algorithm and at least one port */
1820 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1823 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1827 ETHER_BPF_MTAP(ifp, m);
1829 error = lagg_proto_start(sc, m);
1833 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1839 * The ifp->if_qflush entry point for lagg(4) is no-op.
1842 lagg_qflush(struct ifnet *ifp __unused)
1846 static struct mbuf *
1847 lagg_input(struct ifnet *ifp, struct mbuf *m)
1849 struct lagg_port *lp = ifp->if_lagg;
1850 struct lagg_softc *sc = lp->lp_softc;
1851 struct ifnet *scifp = sc->sc_ifp;
1854 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1855 lp->lp_detaching != 0 ||
1856 sc->sc_proto == LAGG_PROTO_NONE) {
1862 ETHER_BPF_MTAP(scifp, m);
1864 m = lagg_proto_input(sc, lp, m);
1865 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1875 lagg_media_change(struct ifnet *ifp)
1877 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1879 if (sc->sc_ifflags & IFF_DEBUG)
1880 printf("%s\n", __func__);
1887 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1889 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1890 struct lagg_port *lp;
1892 imr->ifm_status = IFM_AVALID;
1893 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1896 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1897 if (LAGG_PORTACTIVE(lp))
1898 imr->ifm_status |= IFM_ACTIVE;
1904 lagg_linkstate(struct lagg_softc *sc)
1906 struct lagg_port *lp;
1907 int new_link = LINK_STATE_DOWN;
1910 LAGG_XLOCK_ASSERT(sc);
1912 /* LACP handles link state itself */
1913 if (sc->sc_proto == LAGG_PROTO_LACP)
1916 /* Our link is considered up if at least one of our ports is active */
1918 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1919 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1920 new_link = LINK_STATE_UP;
1925 if_link_state_change(sc->sc_ifp, new_link);
1927 /* Update if_baudrate to reflect the max possible speed */
1928 switch (sc->sc_proto) {
1929 case LAGG_PROTO_FAILOVER:
1930 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1931 sc->sc_primary->lp_ifp->if_baudrate : 0;
1933 case LAGG_PROTO_ROUNDROBIN:
1934 case LAGG_PROTO_LOADBALANCE:
1935 case LAGG_PROTO_BROADCAST:
1938 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1939 speed += lp->lp_ifp->if_baudrate;
1941 sc->sc_ifp->if_baudrate = speed;
1943 case LAGG_PROTO_LACP:
1944 /* LACP updates if_baudrate itself */
1950 lagg_port_state(struct ifnet *ifp, int state)
1952 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1953 struct lagg_softc *sc = NULL;
1962 lagg_proto_linkstate(sc, lp);
1967 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1969 struct lagg_port *lp_next, *rval = NULL;
1972 * Search a port which reports an active link state.
1977 * This is called with either LAGG_RLOCK() held or
1978 * LAGG_XLOCK(sc) held.
1980 if (!in_epoch(net_epoch_preempt))
1981 LAGG_XLOCK_ASSERT(sc);
1986 if (LAGG_PORTACTIVE(lp)) {
1990 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
1991 LAGG_PORTACTIVE(lp_next)) {
1997 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1998 if (LAGG_PORTACTIVE(lp_next)) {
2007 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2011 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2012 struct lagg_snd_tag *lst;
2013 struct m_snd_tag *mst;
2015 mst = m->m_pkthdr.snd_tag;
2016 lst = mst_to_lst(mst);
2017 if (lst->tag->ifp != ifp) {
2021 m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2022 m_snd_tag_rele(mst);
2025 return (ifp->if_transmit)(ifp, m);
2029 * Simple round robin aggregation
2032 lagg_rr_attach(struct lagg_softc *sc)
2035 sc->sc_bkt_count = sc->sc_bkt;
2039 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2041 struct lagg_port *lp;
2044 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
2045 sc->sc_bkt_count = sc->sc_bkt;
2047 if (sc->sc_bkt > 0) {
2048 atomic_subtract_int(&sc->sc_bkt_count, 1);
2049 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
2050 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2054 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2057 lp = CK_SLIST_FIRST(&sc->sc_ports);
2060 lp = CK_SLIST_NEXT(lp, lp_entries);
2063 * Check the port's link state. This will return the next active
2064 * port if the link is down or the port is NULL.
2066 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2072 return (lagg_enqueue(lp->lp_ifp, m));
2075 static struct mbuf *
2076 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2078 struct ifnet *ifp = sc->sc_ifp;
2080 /* Just pass in the packet to our lagg device */
2081 m->m_pkthdr.rcvif = ifp;
2090 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2092 int active_ports = 0;
2095 struct lagg_port *lp, *last = NULL;
2098 LAGG_RLOCK_ASSERT();
2099 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2100 if (!LAGG_PORTACTIVE(lp))
2106 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2113 ret = lagg_enqueue(last->lp_ifp, m0);
2124 if ((last = lagg_link_active(sc, last)) == NULL) {
2129 ret = lagg_enqueue(last->lp_ifp, m);
2140 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2142 struct ifnet *ifp = sc->sc_ifp;
2144 /* Just pass in the packet to our lagg device */
2145 m->m_pkthdr.rcvif = ifp;
2153 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2155 struct lagg_port *lp;
2157 /* Use the master port if active or the next available port */
2158 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2164 return (lagg_enqueue(lp->lp_ifp, m));
2167 static struct mbuf *
2168 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2170 struct ifnet *ifp = sc->sc_ifp;
2171 struct lagg_port *tmp_tp;
2173 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2174 m->m_pkthdr.rcvif = ifp;
2178 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2179 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2181 * If tmp_tp is null, we've received a packet when all
2182 * our links are down. Weird, but process it anyways.
2184 if ((tmp_tp == NULL || tmp_tp == lp)) {
2185 m->m_pkthdr.rcvif = ifp;
2198 lagg_lb_attach(struct lagg_softc *sc)
2200 struct lagg_port *lp;
2203 LAGG_XLOCK_ASSERT(sc);
2204 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2205 lb->lb_key = m_ether_tcpip_hash_init();
2208 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2209 lagg_lb_port_create(lp);
2213 lagg_lb_detach(struct lagg_softc *sc)
2217 lb = (struct lagg_lb *)sc->sc_psc;
2223 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2225 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2226 struct lagg_port *lp_next;
2230 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2231 LAGG_XLOCK_ASSERT(sc);
2232 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2235 if (i >= LAGG_MAX_PORTS) {
2239 if (sc->sc_ifflags & IFF_DEBUG)
2240 printf("%s: port %s at index %d\n",
2241 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2242 lb->lb_ports[i++] = lp_next;
2249 lagg_lb_port_create(struct lagg_port *lp)
2251 struct lagg_softc *sc = lp->lp_softc;
2252 return (lagg_lb_porttable(sc, NULL));
2256 lagg_lb_port_destroy(struct lagg_port *lp)
2258 struct lagg_softc *sc = lp->lp_softc;
2259 lagg_lb_porttable(sc, lp);
2263 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2265 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2266 struct lagg_port *lp = NULL;
2269 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2270 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2271 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2273 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2275 lp = lb->lb_ports[p];
2278 * Check the port's link state. This will return the next active
2279 * port if the link is down or the port is NULL.
2281 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2287 return (lagg_enqueue(lp->lp_ifp, m));
2290 static struct mbuf *
2291 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2293 struct ifnet *ifp = sc->sc_ifp;
2295 /* Just pass in the packet to our lagg device */
2296 m->m_pkthdr.rcvif = ifp;
2305 lagg_lacp_attach(struct lagg_softc *sc)
2307 struct lagg_port *lp;
2310 LAGG_XLOCK_ASSERT(sc);
2311 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2312 lacp_port_create(lp);
2316 lagg_lacp_detach(struct lagg_softc *sc)
2318 struct lagg_port *lp;
2321 LAGG_XLOCK_ASSERT(sc);
2322 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2323 lacp_port_destroy(lp);
2331 lagg_lacp_lladdr(struct lagg_softc *sc)
2333 struct lagg_port *lp;
2335 LAGG_SXLOCK_ASSERT(sc);
2337 /* purge all the lacp ports */
2338 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2339 lacp_port_destroy(lp);
2341 /* add them back in */
2342 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2343 lacp_port_create(lp);
2347 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2349 struct lagg_port *lp;
2351 lp = lacp_select_tx_port(sc, m);
2358 return (lagg_enqueue(lp->lp_ifp, m));
2361 static struct mbuf *
2362 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2364 struct ifnet *ifp = sc->sc_ifp;
2365 struct ether_header *eh;
2368 eh = mtod(m, struct ether_header *);
2369 etype = ntohs(eh->ether_type);
2371 /* Tap off LACP control messages */
2372 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2373 m = lacp_input(lp, m);
2379 * If the port is not collecting or not in the active aggregator then
2382 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2387 m->m_pkthdr.rcvif = ifp;