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 *);
148 static int lagg_setmulti(struct lagg_port *);
149 static int lagg_clrmulti(struct lagg_port *);
150 static int lagg_setcaps(struct lagg_port *, int cap);
151 static int lagg_setflag(struct lagg_port *, int, int,
152 int (*func)(struct ifnet *, int));
153 static int lagg_setflags(struct lagg_port *, int status);
154 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
155 static int lagg_transmit(struct ifnet *, struct mbuf *);
156 static void lagg_qflush(struct ifnet *);
157 static int lagg_media_change(struct ifnet *);
158 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
159 static struct lagg_port *lagg_link_active(struct lagg_softc *,
162 /* Simple round robin */
163 static void lagg_rr_attach(struct lagg_softc *);
164 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
165 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
168 /* Active failover */
169 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
170 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
174 static void lagg_lb_attach(struct lagg_softc *);
175 static void lagg_lb_detach(struct lagg_softc *);
176 static int lagg_lb_port_create(struct lagg_port *);
177 static void lagg_lb_port_destroy(struct lagg_port *);
178 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
179 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
181 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
184 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
185 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
189 static void lagg_lacp_attach(struct lagg_softc *);
190 static void lagg_lacp_detach(struct lagg_softc *);
191 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
192 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
194 static void lagg_lacp_lladdr(struct lagg_softc *);
196 /* lagg protocol table */
197 static const struct lagg_proto {
199 void (*pr_attach)(struct lagg_softc *);
200 void (*pr_detach)(struct lagg_softc *);
201 int (*pr_start)(struct lagg_softc *, struct mbuf *);
202 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
204 int (*pr_addport)(struct lagg_port *);
205 void (*pr_delport)(struct lagg_port *);
206 void (*pr_linkstate)(struct lagg_port *);
207 void (*pr_init)(struct lagg_softc *);
208 void (*pr_stop)(struct lagg_softc *);
209 void (*pr_lladdr)(struct lagg_softc *);
210 void (*pr_request)(struct lagg_softc *, void *);
211 void (*pr_portreq)(struct lagg_port *, void *);
214 .pr_num = LAGG_PROTO_NONE
217 .pr_num = LAGG_PROTO_ROUNDROBIN,
218 .pr_attach = lagg_rr_attach,
219 .pr_start = lagg_rr_start,
220 .pr_input = lagg_rr_input,
223 .pr_num = LAGG_PROTO_FAILOVER,
224 .pr_start = lagg_fail_start,
225 .pr_input = lagg_fail_input,
228 .pr_num = LAGG_PROTO_LOADBALANCE,
229 .pr_attach = lagg_lb_attach,
230 .pr_detach = lagg_lb_detach,
231 .pr_start = lagg_lb_start,
232 .pr_input = lagg_lb_input,
233 .pr_addport = lagg_lb_port_create,
234 .pr_delport = lagg_lb_port_destroy,
237 .pr_num = LAGG_PROTO_LACP,
238 .pr_attach = lagg_lacp_attach,
239 .pr_detach = lagg_lacp_detach,
240 .pr_start = lagg_lacp_start,
241 .pr_input = lagg_lacp_input,
242 .pr_addport = lacp_port_create,
243 .pr_delport = lacp_port_destroy,
244 .pr_linkstate = lacp_linkstate,
245 .pr_init = lacp_init,
246 .pr_stop = lacp_stop,
247 .pr_lladdr = lagg_lacp_lladdr,
248 .pr_request = lacp_req,
249 .pr_portreq = lacp_portreq,
252 .pr_num = LAGG_PROTO_BROADCAST,
253 .pr_start = lagg_bcast_start,
254 .pr_input = lagg_bcast_input,
258 SYSCTL_DECL(_net_link);
259 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
262 /* Allow input on any failover links */
263 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
264 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
265 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
266 &VNET_NAME(lagg_failover_rx_all), 0,
267 "Accept input from any interface in a failover lagg");
269 /* Default value for using flowid */
270 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
271 #define V_def_use_flowid VNET(def_use_flowid)
272 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
273 &VNET_NAME(def_use_flowid), 0,
274 "Default setting for using flow id for load sharing");
276 /* Default value for using numa */
277 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
278 #define V_def_use_numa VNET(def_use_numa)
279 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa, CTLFLAG_RWTUN,
280 &VNET_NAME(def_use_numa), 0,
281 "Use numa to steer flows");
283 /* Default value for flowid shift */
284 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
285 #define V_def_flowid_shift VNET(def_flowid_shift)
286 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
287 &VNET_NAME(def_flowid_shift), 0,
288 "Default setting for flowid shift for load sharing");
291 vnet_lagg_init(const void *unused __unused)
294 LAGG_LIST_LOCK_INIT();
295 SLIST_INIT(&V_lagg_list);
296 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
297 lagg_clone_destroy, 0);
299 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
300 vnet_lagg_init, NULL);
303 vnet_lagg_uninit(const void *unused __unused)
306 if_clone_detach(V_lagg_cloner);
307 LAGG_LIST_LOCK_DESTROY();
309 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
310 vnet_lagg_uninit, NULL);
313 lagg_modevent(module_t mod, int type, void *data)
318 lagg_input_p = lagg_input;
319 lagg_linkstate_p = lagg_port_state;
320 lagg_detach_cookie = EVENTHANDLER_REGISTER(
321 ifnet_departure_event, lagg_port_ifdetach, NULL,
322 EVENTHANDLER_PRI_ANY);
325 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
328 lagg_linkstate_p = NULL;
336 static moduledata_t lagg_mod = {
342 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
343 MODULE_VERSION(if_lagg, 1);
346 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
349 LAGG_XLOCK_ASSERT(sc);
350 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
353 if (sc->sc_ifflags & IFF_DEBUG)
354 if_printf(sc->sc_ifp, "using proto %u\n", pr);
356 if (lagg_protos[pr].pr_attach != NULL)
357 lagg_protos[pr].pr_attach(sc);
362 lagg_proto_detach(struct lagg_softc *sc)
366 LAGG_XLOCK_ASSERT(sc);
368 sc->sc_proto = LAGG_PROTO_NONE;
370 if (lagg_protos[pr].pr_detach != NULL)
371 lagg_protos[pr].pr_detach(sc);
375 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
378 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
382 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
385 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
389 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
392 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
395 return (lagg_protos[sc->sc_proto].pr_addport(lp));
399 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
402 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
403 lagg_protos[sc->sc_proto].pr_delport(lp);
407 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
410 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
411 lagg_protos[sc->sc_proto].pr_linkstate(lp);
415 lagg_proto_init(struct lagg_softc *sc)
418 if (lagg_protos[sc->sc_proto].pr_init != NULL)
419 lagg_protos[sc->sc_proto].pr_init(sc);
423 lagg_proto_stop(struct lagg_softc *sc)
426 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
427 lagg_protos[sc->sc_proto].pr_stop(sc);
431 lagg_proto_lladdr(struct lagg_softc *sc)
434 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
435 lagg_protos[sc->sc_proto].pr_lladdr(sc);
439 lagg_proto_request(struct lagg_softc *sc, void *v)
442 if (lagg_protos[sc->sc_proto].pr_request != NULL)
443 lagg_protos[sc->sc_proto].pr_request(sc, v);
447 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
450 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
451 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
455 * This routine is run via an vlan
459 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
461 struct lagg_softc *sc = ifp->if_softc;
462 struct lagg_port *lp;
464 if (ifp->if_softc != arg) /* Not our event */
468 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
469 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
474 * This routine is run via an vlan
478 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
480 struct lagg_softc *sc = ifp->if_softc;
481 struct lagg_port *lp;
483 if (ifp->if_softc != arg) /* Not our event */
487 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
488 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
493 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
495 struct lagg_softc *sc;
497 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
499 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
500 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
508 if (V_def_use_flowid)
509 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
511 sc->sc_opts |= LAGG_OPT_USE_NUMA;
512 sc->flowid_shift = V_def_flowid_shift;
514 /* Hash all layers by default */
515 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
517 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
519 CK_SLIST_INIT(&sc->sc_ports);
521 /* Initialise pseudo media types */
522 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
524 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
525 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
527 if_initname(ifp, laggname, unit);
529 ifp->if_transmit = lagg_transmit;
530 ifp->if_qflush = lagg_qflush;
531 ifp->if_init = lagg_init;
532 ifp->if_ioctl = lagg_ioctl;
533 ifp->if_get_counter = lagg_get_counter;
534 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
536 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
537 ifp->if_snd_tag_modify = lagg_snd_tag_modify;
538 ifp->if_snd_tag_query = lagg_snd_tag_query;
539 ifp->if_snd_tag_free = lagg_snd_tag_free;
541 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
544 * Attach as an ordinary ethernet device, children will be attached
545 * as special device IFT_IEEE8023ADLAG.
547 ether_ifattach(ifp, eaddr);
549 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
550 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
551 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
552 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
554 /* Insert into the global list of laggs */
556 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
564 lagg_clone_destroy(struct ifnet *ifp)
566 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
567 struct lagg_port *lp;
570 sc->sc_destroying = 1;
572 ifp->if_flags &= ~IFF_UP;
574 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
575 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
577 /* Shutdown and remove lagg ports */
578 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
579 lagg_port_destroy(lp, 1);
581 /* Unhook the aggregation protocol */
582 lagg_proto_detach(sc);
585 ifmedia_removeall(&sc->sc_media);
590 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
598 lagg_capabilities(struct lagg_softc *sc)
600 struct lagg_port *lp;
603 struct ifnet_hw_tsomax hw_tsomax;
605 LAGG_XLOCK_ASSERT(sc);
607 /* Get common enabled capabilities for the lagg ports */
609 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
610 ena &= lp->lp_ifp->if_capenable;
611 ena = (ena == ~0 ? 0 : ena);
614 * Apply common enabled capabilities back to the lagg ports.
615 * May require several iterations if they are dependent.
619 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
620 lagg_setcaps(lp, ena);
621 ena &= lp->lp_ifp->if_capenable;
623 } while (pena != ena);
625 /* Get other capabilities from the lagg ports */
628 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
629 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
630 cap &= lp->lp_ifp->if_capabilities;
631 hwa &= lp->lp_ifp->if_hwassist;
632 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
634 cap = (cap == ~0 ? 0 : cap);
635 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
637 if (sc->sc_ifp->if_capabilities != cap ||
638 sc->sc_ifp->if_capenable != ena ||
639 sc->sc_ifp->if_hwassist != hwa ||
640 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
641 sc->sc_ifp->if_capabilities = cap;
642 sc->sc_ifp->if_capenable = ena;
643 sc->sc_ifp->if_hwassist = hwa;
644 getmicrotime(&sc->sc_ifp->if_lastchange);
646 if (sc->sc_ifflags & IFF_DEBUG)
647 if_printf(sc->sc_ifp,
648 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
653 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
655 struct lagg_softc *sc_ptr;
656 struct lagg_port *lp, *tlp;
658 int error, i, oldmtu;
661 LAGG_XLOCK_ASSERT(sc);
663 if (sc->sc_ifp == ifp) {
664 if_printf(sc->sc_ifp,
665 "cannot add a lagg to itself as a port\n");
669 /* Limit the maximal number of lagg ports */
670 if (sc->sc_count >= LAGG_MAX_PORTS)
673 /* Check if port has already been associated to a lagg */
674 if (ifp->if_lagg != NULL) {
675 /* Port is already in the current lagg? */
676 lp = (struct lagg_port *)ifp->if_lagg;
677 if (lp->lp_softc == sc)
682 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
683 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
684 return (EPROTONOSUPPORT);
686 /* Allow the first Ethernet member to define the MTU */
688 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
689 sc->sc_ifp->if_mtu = ifp->if_mtu;
690 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
691 if (ifp->if_ioctl == NULL) {
692 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
696 oldmtu = ifp->if_mtu;
697 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
698 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
699 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
701 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
705 ifr.ifr_mtu = oldmtu;
708 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
711 /* Check if port is a stacked lagg */
713 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
714 if (ifp == sc_ptr->sc_ifp) {
718 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
721 /* XXX disable stacking for the moment, its untested */
722 #ifdef LAGG_PORT_STACKING
723 lp->lp_flags |= LAGG_PORT_STACK;
724 if (lagg_port_checkstacking(sc_ptr) >=
729 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
741 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
742 lp->lp_ifcapenable = ifp->if_capenable;
743 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
744 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
745 lagg_proto_lladdr(sc);
746 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
748 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
750 lagg_setflags(lp, 1);
752 if (CK_SLIST_EMPTY(&sc->sc_ports))
755 /* Change the interface type */
756 lp->lp_iftype = ifp->if_type;
757 ifp->if_type = IFT_IEEE8023ADLAG;
759 lp->lp_ioctl = ifp->if_ioctl;
760 ifp->if_ioctl = lagg_port_ioctl;
761 lp->lp_output = ifp->if_output;
762 ifp->if_output = lagg_port_output;
764 /* Read port counters */
765 pval = lp->port_counters.val;
766 for (i = 0; i < IFCOUNTERS; i++, pval++)
767 *pval = ifp->if_get_counter(ifp, i);
770 * Insert into the list of ports.
771 * Keep ports sorted by if_index. It is handy, when configuration
772 * is predictable and `ifconfig laggN create ...` command
773 * will lead to the same result each time.
775 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
776 if (tlp->lp_ifp->if_index < ifp->if_index && (
777 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
778 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
783 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
785 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
791 if ((error = lagg_proto_addport(sc, lp)) != 0) {
792 /* Remove the port, without calling pr_delport. */
793 lagg_port_destroy(lp, 0);
795 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
799 /* Update lagg capabilities */
800 lagg_capabilities(sc);
806 #ifdef LAGG_PORT_STACKING
808 lagg_port_checkstacking(struct lagg_softc *sc)
810 struct lagg_softc *sc_ptr;
811 struct lagg_port *lp;
814 LAGG_SXLOCK_ASSERT(sc);
815 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
816 if (lp->lp_flags & LAGG_PORT_STACK) {
817 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
818 m = MAX(m, lagg_port_checkstacking(sc_ptr));
827 lagg_port_destroy_cb(epoch_context_t ec)
829 struct lagg_port *lp;
832 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
840 lagg_port_destroy(struct lagg_port *lp, int rundelport)
842 struct lagg_softc *sc = lp->lp_softc;
843 struct lagg_port *lp_ptr, *lp0;
844 struct ifnet *ifp = lp->lp_ifp;
845 uint64_t *pval, vdiff;
848 LAGG_XLOCK_ASSERT(sc);
851 lagg_proto_delport(sc, lp);
853 if (lp->lp_detaching == 0)
856 /* Restore interface */
857 ifp->if_type = lp->lp_iftype;
858 ifp->if_ioctl = lp->lp_ioctl;
859 ifp->if_output = lp->lp_output;
862 /* Update detached port counters */
863 pval = lp->port_counters.val;
864 for (i = 0; i < IFCOUNTERS; i++, pval++) {
865 vdiff = ifp->if_get_counter(ifp, i) - *pval;
866 sc->detached_counters.val[i] += vdiff;
869 /* Finally, remove the port from the lagg */
870 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
873 /* Update the primary interface */
874 if (lp == sc->sc_primary) {
875 uint8_t lladdr[ETHER_ADDR_LEN];
877 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
878 bzero(&lladdr, ETHER_ADDR_LEN);
880 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
881 sc->sc_primary = lp0;
882 if (sc->sc_destroying == 0) {
883 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
884 lagg_proto_lladdr(sc);
885 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
889 * Update lladdr for each port (new primary needs update
890 * as well, to switch from old lladdr to its 'real' one)
892 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
893 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
897 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
899 if (lp->lp_detaching == 0) {
900 lagg_setflags(lp, 0);
901 lagg_setcaps(lp, lp->lp_ifcapenable);
902 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
906 * free port and release it's ifnet reference after a grace period has
909 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
910 /* Update lagg capabilities */
911 lagg_capabilities(sc);
918 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
920 struct lagg_reqport *rp = (struct lagg_reqport *)data;
921 struct lagg_softc *sc;
922 struct lagg_port *lp = NULL;
925 /* Should be checked by the caller */
926 if (ifp->if_type != IFT_IEEE8023ADLAG ||
927 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
932 if (rp->rp_portname[0] == '\0' ||
933 ifunit(rp->rp_portname) != ifp) {
939 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
945 lagg_port2req(lp, rp);
950 if (lp->lp_ioctl == NULL) {
954 error = (*lp->lp_ioctl)(ifp, cmd, data);
958 /* Update lagg interface capabilities */
960 lagg_capabilities(sc);
962 VLAN_CAPABILITIES(sc->sc_ifp);
966 /* Do not allow the MTU to be changed once joined */
977 if (lp != NULL && lp->lp_ioctl != NULL)
978 return ((*lp->lp_ioctl)(ifp, cmd, data));
984 * Requests counter @cnt data.
986 * Counter value is calculated the following way:
987 * 1) for each port, sum difference between current and "initial" measurements.
988 * 2) add lagg logical interface counters.
989 * 3) add data from detached_counters array.
991 * We also do the following things on ports attach/detach:
992 * 1) On port attach we store all counters it has into port_counter array.
993 * 2) On port detach we add the different between "initial" and
994 * current counters data to detached_counters array.
997 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
999 struct lagg_softc *sc;
1000 struct lagg_port *lp;
1001 struct ifnet *lpifp;
1002 uint64_t newval, oldval, vsum;
1004 /* Revise this when we've got non-generic counters. */
1005 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1007 sc = (struct lagg_softc *)ifp->if_softc;
1011 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1012 /* Saved attached value */
1013 oldval = lp->port_counters.val[cnt];
1016 newval = lpifp->if_get_counter(lpifp, cnt);
1017 /* Calculate diff and save new */
1018 vsum += newval - oldval;
1023 * Add counter data which might be added by upper
1024 * layer protocols operating on logical interface.
1026 vsum += if_get_counter_default(ifp, cnt);
1029 * Add counter data from detached ports counters
1031 vsum += sc->detached_counters.val[cnt];
1038 * For direct output to child ports.
1041 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1042 const struct sockaddr *dst, struct route *ro)
1044 struct lagg_port *lp = ifp->if_lagg;
1046 switch (dst->sa_family) {
1047 case pseudo_AF_HDRCMPLT:
1049 return ((*lp->lp_output)(ifp, m, dst, ro));
1052 /* drop any other frames */
1058 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1060 struct lagg_port *lp;
1061 struct lagg_softc *sc;
1063 if ((lp = ifp->if_lagg) == NULL)
1065 /* If the ifnet is just being renamed, don't do anything. */
1066 if (ifp->if_flags & IFF_RENAMING)
1072 lp->lp_detaching = 1;
1073 lagg_port_destroy(lp, 1);
1075 VLAN_CAPABILITIES(sc->sc_ifp);
1079 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1081 struct lagg_softc *sc = lp->lp_softc;
1083 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1084 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1085 rp->rp_prio = lp->lp_prio;
1086 rp->rp_flags = lp->lp_flags;
1087 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1089 /* Add protocol specific flags */
1090 switch (sc->sc_proto) {
1091 case LAGG_PROTO_FAILOVER:
1092 if (lp == sc->sc_primary)
1093 rp->rp_flags |= LAGG_PORT_MASTER;
1094 if (lp == lagg_link_active(sc, sc->sc_primary))
1095 rp->rp_flags |= LAGG_PORT_ACTIVE;
1098 case LAGG_PROTO_ROUNDROBIN:
1099 case LAGG_PROTO_LOADBALANCE:
1100 case LAGG_PROTO_BROADCAST:
1101 if (LAGG_PORTACTIVE(lp))
1102 rp->rp_flags |= LAGG_PORT_ACTIVE;
1105 case LAGG_PROTO_LACP:
1106 /* LACP has a different definition of active */
1107 if (lacp_isactive(lp))
1108 rp->rp_flags |= LAGG_PORT_ACTIVE;
1109 if (lacp_iscollecting(lp))
1110 rp->rp_flags |= LAGG_PORT_COLLECTING;
1111 if (lacp_isdistributing(lp))
1112 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1119 lagg_init(void *xsc)
1121 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1122 struct ifnet *ifp = sc->sc_ifp;
1123 struct lagg_port *lp;
1126 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1131 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1134 * Update the port lladdrs if needed.
1135 * This might be if_setlladdr() notification
1136 * that lladdr has been changed.
1138 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1139 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1140 ETHER_ADDR_LEN) != 0)
1141 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1144 lagg_proto_init(sc);
1150 lagg_stop(struct lagg_softc *sc)
1152 struct ifnet *ifp = sc->sc_ifp;
1154 LAGG_XLOCK_ASSERT(sc);
1156 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1159 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1161 lagg_proto_stop(sc);
1165 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1167 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1168 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1169 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1170 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1171 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1172 struct ifreq *ifr = (struct ifreq *)data;
1173 struct lagg_port *lp;
1175 struct thread *td = curthread;
1177 int count, buflen, len, error = 0;
1179 bzero(&rpbuf, sizeof(rpbuf));
1184 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1185 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1186 ra->ra_proto = sc->sc_proto;
1187 lagg_proto_request(sc, &ra->ra_psc);
1190 len = min(ra->ra_size, buflen);
1191 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1192 if (len < sizeof(rpbuf))
1195 lagg_port2req(lp, &rpbuf);
1196 memcpy(buf, &rpbuf, sizeof(rpbuf));
1198 buf += sizeof(rpbuf);
1199 len -= sizeof(rpbuf);
1202 ra->ra_ports = count;
1203 ra->ra_size = count * sizeof(rpbuf);
1204 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1205 free(outbuf, M_TEMP);
1208 error = priv_check(td, PRIV_NET_LAGG);
1211 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1212 error = EPROTONOSUPPORT;
1217 lagg_proto_detach(sc);
1218 LAGG_UNLOCK_ASSERT();
1219 lagg_proto_attach(sc, ra->ra_proto);
1224 ro->ro_opts = sc->sc_opts;
1225 if (sc->sc_proto == LAGG_PROTO_LACP) {
1226 struct lacp_softc *lsc;
1228 lsc = (struct lacp_softc *)sc->sc_psc;
1229 if (lsc->lsc_debug.lsc_tx_test != 0)
1230 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1231 if (lsc->lsc_debug.lsc_rx_test != 0)
1232 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1233 if (lsc->lsc_strict_mode != 0)
1234 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1235 if (lsc->lsc_fast_timeout != 0)
1236 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1238 ro->ro_active = sc->sc_active;
1241 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1242 ro->ro_active += LAGG_PORTACTIVE(lp);
1244 ro->ro_bkt = sc->sc_bkt;
1245 ro->ro_flapping = sc->sc_flapping;
1246 ro->ro_flowid_shift = sc->flowid_shift;
1250 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1251 if (ro->ro_bkt == 0)
1252 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1254 sc->sc_bkt = ro->ro_bkt;
1256 error = priv_check(td, PRIV_NET_LAGG);
1259 if (ro->ro_opts == 0)
1262 * Set options. LACP options are stored in sc->sc_psc,
1267 switch (ro->ro_opts) {
1268 case LAGG_OPT_USE_FLOWID:
1269 case -LAGG_OPT_USE_FLOWID:
1270 case LAGG_OPT_USE_NUMA:
1271 case -LAGG_OPT_USE_NUMA:
1272 case LAGG_OPT_FLOWIDSHIFT:
1276 case LAGG_OPT_LACP_TXTEST:
1277 case -LAGG_OPT_LACP_TXTEST:
1278 case LAGG_OPT_LACP_RXTEST:
1279 case -LAGG_OPT_LACP_RXTEST:
1280 case LAGG_OPT_LACP_STRICT:
1281 case -LAGG_OPT_LACP_STRICT:
1282 case LAGG_OPT_LACP_TIMEOUT:
1283 case -LAGG_OPT_LACP_TIMEOUT:
1294 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1295 /* Invalid combination of options specified. */
1298 break; /* Return from SIOCSLAGGOPTS. */
1301 * Store new options into sc->sc_opts except for
1302 * FLOWIDSHIFT and LACP options.
1305 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1306 sc->flowid_shift = ro->ro_flowid_shift;
1307 else if (ro->ro_opts > 0)
1308 sc->sc_opts |= ro->ro_opts;
1310 sc->sc_opts &= ~ro->ro_opts;
1312 struct lacp_softc *lsc;
1313 struct lacp_port *lp;
1315 lsc = (struct lacp_softc *)sc->sc_psc;
1317 switch (ro->ro_opts) {
1318 case LAGG_OPT_LACP_TXTEST:
1319 lsc->lsc_debug.lsc_tx_test = 1;
1321 case -LAGG_OPT_LACP_TXTEST:
1322 lsc->lsc_debug.lsc_tx_test = 0;
1324 case LAGG_OPT_LACP_RXTEST:
1325 lsc->lsc_debug.lsc_rx_test = 1;
1327 case -LAGG_OPT_LACP_RXTEST:
1328 lsc->lsc_debug.lsc_rx_test = 0;
1330 case LAGG_OPT_LACP_STRICT:
1331 lsc->lsc_strict_mode = 1;
1333 case -LAGG_OPT_LACP_STRICT:
1334 lsc->lsc_strict_mode = 0;
1336 case LAGG_OPT_LACP_TIMEOUT:
1338 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1339 lp->lp_state |= LACP_STATE_TIMEOUT;
1341 lsc->lsc_fast_timeout = 1;
1343 case -LAGG_OPT_LACP_TIMEOUT:
1345 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1346 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1348 lsc->lsc_fast_timeout = 0;
1354 case SIOCGLAGGFLAGS:
1357 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1358 rf->rf_flags |= LAGG_F_HASHL2;
1359 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1360 rf->rf_flags |= LAGG_F_HASHL3;
1361 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1362 rf->rf_flags |= LAGG_F_HASHL4;
1366 error = priv_check(td, PRIV_NET_LAGG);
1369 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1375 if (rf->rf_flags & LAGG_F_HASHL2)
1376 sc->sc_flags |= MBUF_HASHFLAG_L2;
1377 if (rf->rf_flags & LAGG_F_HASHL3)
1378 sc->sc_flags |= MBUF_HASHFLAG_L3;
1379 if (rf->rf_flags & LAGG_F_HASHL4)
1380 sc->sc_flags |= MBUF_HASHFLAG_L4;
1384 if (rp->rp_portname[0] == '\0' ||
1385 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1391 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1392 lp->lp_softc != sc) {
1399 lagg_port2req(lp, rp);
1404 error = priv_check(td, PRIV_NET_LAGG);
1407 if (rp->rp_portname[0] == '\0' ||
1408 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1414 * A laggport interface should not have inet6 address
1415 * because two interfaces with a valid link-local
1416 * scope zone must not be merged in any form. This
1417 * restriction is needed to prevent violation of
1418 * link-local scope zone. Attempts to add a laggport
1419 * interface which has inet6 addresses triggers
1420 * removal of all inet6 addresses on the member
1423 if (in6ifa_llaonifp(tpif)) {
1425 if_printf(sc->sc_ifp,
1426 "IPv6 addresses on %s have been removed "
1427 "before adding it as a member to prevent "
1428 "IPv6 address scope violation.\n",
1433 error = lagg_port_create(sc, tpif);
1436 VLAN_CAPABILITIES(ifp);
1438 case SIOCSLAGGDELPORT:
1439 error = priv_check(td, PRIV_NET_LAGG);
1442 if (rp->rp_portname[0] == '\0' ||
1443 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1449 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1450 lp->lp_softc != sc) {
1457 error = lagg_port_destroy(lp, 1);
1460 VLAN_CAPABILITIES(ifp);
1463 /* Set flags on ports too */
1465 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1466 lagg_setflags(lp, 1);
1469 if (!(ifp->if_flags & IFF_UP) &&
1470 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1472 * If interface is marked down and it is running,
1473 * then stop and disable it.
1477 } else if ((ifp->if_flags & IFF_UP) &&
1478 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1480 * If interface is marked up and it is stopped, then
1484 (*ifp->if_init)(sc);
1491 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1500 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1505 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1506 if (lp->lp_ioctl != NULL)
1507 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1509 lagg_capabilities(sc);
1511 VLAN_CAPABILITIES(ifp);
1517 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1518 if (lp->lp_ioctl != NULL)
1519 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1524 "failed to change MTU to %d on port %s, "
1525 "reverting all ports to original MTU (%d)\n",
1526 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1531 ifp->if_mtu = ifr->ifr_mtu;
1533 /* set every port back to the original MTU */
1534 ifr->ifr_mtu = ifp->if_mtu;
1535 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1536 if (lp->lp_ioctl != NULL)
1537 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1544 error = ether_ioctl(ifp, cmd, data);
1551 static inline struct lagg_snd_tag *
1552 mst_to_lst(struct m_snd_tag *mst)
1555 return (__containerof(mst, struct lagg_snd_tag, com));
1559 * Look up the port used by a specific flow. This only works for lagg
1560 * protocols with deterministic port mappings (e.g. not roundrobin).
1561 * In addition protocols which use a hash to map flows to ports must
1562 * be configured to use the mbuf flowid rather than hashing packet
1565 static struct lagg_port *
1566 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype)
1568 struct lagg_softc *sc;
1569 struct lagg_port *lp;
1575 switch (sc->sc_proto) {
1576 case LAGG_PROTO_FAILOVER:
1577 return (lagg_link_active(sc, sc->sc_primary));
1578 case LAGG_PROTO_LOADBALANCE:
1579 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1580 flowtype == M_HASHTYPE_NONE)
1582 p = flowid >> sc->flowid_shift;
1584 lb = (struct lagg_lb *)sc->sc_psc;
1585 lp = lb->lb_ports[p];
1586 return (lagg_link_active(sc, lp));
1587 case LAGG_PROTO_LACP:
1588 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1589 flowtype == M_HASHTYPE_NONE)
1591 return (lacp_select_tx_port_by_hash(sc, flowid));
1598 lagg_snd_tag_alloc(struct ifnet *ifp,
1599 union if_snd_tag_alloc_params *params,
1600 struct m_snd_tag **ppmt)
1602 struct lagg_snd_tag *lst;
1603 struct lagg_softc *sc;
1604 struct lagg_port *lp;
1605 struct ifnet *lp_ifp;
1611 lp = lookup_snd_tag_port(ifp, params->hdr.flowid, params->hdr.flowtype);
1614 return (EOPNOTSUPP);
1616 if (lp->lp_ifp == NULL || lp->lp_ifp->if_snd_tag_alloc == NULL) {
1618 return (EOPNOTSUPP);
1620 lp_ifp = lp->lp_ifp;
1624 lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1630 error = lp_ifp->if_snd_tag_alloc(lp_ifp, params, &lst->tag);
1637 m_snd_tag_init(&lst->com, ifp);
1644 lagg_snd_tag_modify(struct m_snd_tag *mst,
1645 union if_snd_tag_modify_params *params)
1647 struct lagg_snd_tag *lst;
1649 lst = mst_to_lst(mst);
1650 return (lst->tag->ifp->if_snd_tag_modify(lst->tag, params));
1654 lagg_snd_tag_query(struct m_snd_tag *mst,
1655 union if_snd_tag_query_params *params)
1657 struct lagg_snd_tag *lst;
1659 lst = mst_to_lst(mst);
1660 return (lst->tag->ifp->if_snd_tag_query(lst->tag, params));
1664 lagg_snd_tag_free(struct m_snd_tag *mst)
1666 struct lagg_snd_tag *lst;
1668 lst = mst_to_lst(mst);
1669 m_snd_tag_rele(lst->tag);
1676 lagg_setmulti(struct lagg_port *lp)
1678 struct lagg_softc *sc = lp->lp_softc;
1679 struct ifnet *ifp = lp->lp_ifp;
1680 struct ifnet *scifp = sc->sc_ifp;
1682 struct ifmultiaddr *ifma;
1685 IF_ADDR_WLOCK(scifp);
1686 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1687 if (ifma->ifma_addr->sa_family != AF_LINK)
1689 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1691 IF_ADDR_WUNLOCK(scifp);
1694 bcopy(ifma->ifma_addr, &mc->mc_addr,
1695 ifma->ifma_addr->sa_len);
1696 mc->mc_addr.sdl_index = ifp->if_index;
1698 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1700 IF_ADDR_WUNLOCK(scifp);
1701 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1702 error = if_addmulti(ifp,
1703 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1711 lagg_clrmulti(struct lagg_port *lp)
1715 LAGG_XLOCK_ASSERT(lp->lp_softc);
1716 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1717 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1718 if (mc->mc_ifma && lp->lp_detaching == 0)
1719 if_delmulti_ifma(mc->mc_ifma);
1726 lagg_setcaps(struct lagg_port *lp, int cap)
1730 if (lp->lp_ifp->if_capenable == cap)
1732 if (lp->lp_ioctl == NULL)
1734 ifr.ifr_reqcap = cap;
1735 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1738 /* Handle a ref counted flag that should be set on the lagg port as well */
1740 lagg_setflag(struct lagg_port *lp, int flag, int status,
1741 int (*func)(struct ifnet *, int))
1743 struct lagg_softc *sc = lp->lp_softc;
1744 struct ifnet *scifp = sc->sc_ifp;
1745 struct ifnet *ifp = lp->lp_ifp;
1748 LAGG_XLOCK_ASSERT(sc);
1750 status = status ? (scifp->if_flags & flag) : 0;
1751 /* Now "status" contains the flag value or 0 */
1754 * See if recorded ports status is different from what
1755 * we want it to be. If it is, flip it. We record ports
1756 * status in lp_ifflags so that we won't clear ports flag
1757 * we haven't set. In fact, we don't clear or set ports
1758 * flags directly, but get or release references to them.
1759 * That's why we can be sure that recorded flags still are
1760 * in accord with actual ports flags.
1762 if (status != (lp->lp_ifflags & flag)) {
1763 error = (*func)(ifp, status);
1766 lp->lp_ifflags &= ~flag;
1767 lp->lp_ifflags |= status;
1773 * Handle IFF_* flags that require certain changes on the lagg port
1774 * if "status" is true, update ports flags respective to the lagg
1775 * if "status" is false, forcedly clear the flags set on port.
1778 lagg_setflags(struct lagg_port *lp, int status)
1782 for (i = 0; lagg_pflags[i].flag; i++) {
1783 error = lagg_setflag(lp, lagg_pflags[i].flag,
1784 status, lagg_pflags[i].func);
1792 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1794 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1798 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
1799 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1802 /* We need a Tx algorithm and at least one port */
1803 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1806 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1810 ETHER_BPF_MTAP(ifp, m);
1812 error = lagg_proto_start(sc, m);
1816 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1822 * The ifp->if_qflush entry point for lagg(4) is no-op.
1825 lagg_qflush(struct ifnet *ifp __unused)
1829 static struct mbuf *
1830 lagg_input(struct ifnet *ifp, struct mbuf *m)
1832 struct lagg_port *lp = ifp->if_lagg;
1833 struct lagg_softc *sc = lp->lp_softc;
1834 struct ifnet *scifp = sc->sc_ifp;
1837 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1838 lp->lp_detaching != 0 ||
1839 sc->sc_proto == LAGG_PROTO_NONE) {
1845 ETHER_BPF_MTAP(scifp, m);
1847 m = lagg_proto_input(sc, lp, m);
1848 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1858 lagg_media_change(struct ifnet *ifp)
1860 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1862 if (sc->sc_ifflags & IFF_DEBUG)
1863 printf("%s\n", __func__);
1870 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1872 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1873 struct lagg_port *lp;
1875 imr->ifm_status = IFM_AVALID;
1876 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1879 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1880 if (LAGG_PORTACTIVE(lp))
1881 imr->ifm_status |= IFM_ACTIVE;
1887 lagg_linkstate(struct lagg_softc *sc)
1889 struct lagg_port *lp;
1890 int new_link = LINK_STATE_DOWN;
1893 LAGG_XLOCK_ASSERT(sc);
1895 /* LACP handles link state itself */
1896 if (sc->sc_proto == LAGG_PROTO_LACP)
1899 /* Our link is considered up if at least one of our ports is active */
1901 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1902 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1903 new_link = LINK_STATE_UP;
1908 if_link_state_change(sc->sc_ifp, new_link);
1910 /* Update if_baudrate to reflect the max possible speed */
1911 switch (sc->sc_proto) {
1912 case LAGG_PROTO_FAILOVER:
1913 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1914 sc->sc_primary->lp_ifp->if_baudrate : 0;
1916 case LAGG_PROTO_ROUNDROBIN:
1917 case LAGG_PROTO_LOADBALANCE:
1918 case LAGG_PROTO_BROADCAST:
1921 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1922 speed += lp->lp_ifp->if_baudrate;
1924 sc->sc_ifp->if_baudrate = speed;
1926 case LAGG_PROTO_LACP:
1927 /* LACP updates if_baudrate itself */
1933 lagg_port_state(struct ifnet *ifp, int state)
1935 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1936 struct lagg_softc *sc = NULL;
1945 lagg_proto_linkstate(sc, lp);
1950 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1952 struct lagg_port *lp_next, *rval = NULL;
1955 * Search a port which reports an active link state.
1959 * This is called with either LAGG_RLOCK() held or
1960 * LAGG_XLOCK(sc) held.
1962 if (!in_epoch(net_epoch_preempt))
1963 LAGG_XLOCK_ASSERT(sc);
1967 if (LAGG_PORTACTIVE(lp)) {
1971 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
1972 LAGG_PORTACTIVE(lp_next)) {
1978 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1979 if (LAGG_PORTACTIVE(lp_next)) {
1988 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1992 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1993 struct lagg_snd_tag *lst;
1994 struct m_snd_tag *mst;
1996 mst = m->m_pkthdr.snd_tag;
1997 lst = mst_to_lst(mst);
1998 if (lst->tag->ifp != ifp) {
2002 m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2003 m_snd_tag_rele(mst);
2006 return (ifp->if_transmit)(ifp, m);
2010 * Simple round robin aggregation
2013 lagg_rr_attach(struct lagg_softc *sc)
2016 sc->sc_bkt_count = sc->sc_bkt;
2020 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2022 struct lagg_port *lp;
2025 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
2026 sc->sc_bkt_count = sc->sc_bkt;
2028 if (sc->sc_bkt > 0) {
2029 atomic_subtract_int(&sc->sc_bkt_count, 1);
2030 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
2031 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2035 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2038 lp = CK_SLIST_FIRST(&sc->sc_ports);
2041 lp = CK_SLIST_NEXT(lp, lp_entries);
2044 * Check the port's link state. This will return the next active
2045 * port if the link is down or the port is NULL.
2047 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2053 return (lagg_enqueue(lp->lp_ifp, m));
2056 static struct mbuf *
2057 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2059 struct ifnet *ifp = sc->sc_ifp;
2061 /* Just pass in the packet to our lagg device */
2062 m->m_pkthdr.rcvif = ifp;
2071 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2073 int active_ports = 0;
2076 struct lagg_port *lp, *last = NULL;
2079 LAGG_RLOCK_ASSERT();
2080 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2081 if (!LAGG_PORTACTIVE(lp))
2087 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2094 ret = lagg_enqueue(last->lp_ifp, m0);
2105 if ((last = lagg_link_active(sc, last)) == NULL) {
2110 ret = lagg_enqueue(last->lp_ifp, m);
2121 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2123 struct ifnet *ifp = sc->sc_ifp;
2125 /* Just pass in the packet to our lagg device */
2126 m->m_pkthdr.rcvif = ifp;
2134 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2136 struct lagg_port *lp;
2138 /* Use the master port if active or the next available port */
2139 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2145 return (lagg_enqueue(lp->lp_ifp, m));
2148 static struct mbuf *
2149 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2151 struct ifnet *ifp = sc->sc_ifp;
2152 struct lagg_port *tmp_tp;
2154 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2155 m->m_pkthdr.rcvif = ifp;
2159 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2160 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2162 * If tmp_tp is null, we've received a packet when all
2163 * our links are down. Weird, but process it anyways.
2165 if ((tmp_tp == NULL || tmp_tp == lp)) {
2166 m->m_pkthdr.rcvif = ifp;
2179 lagg_lb_attach(struct lagg_softc *sc)
2181 struct lagg_port *lp;
2184 LAGG_XLOCK_ASSERT(sc);
2185 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2186 lb->lb_key = m_ether_tcpip_hash_init();
2189 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2190 lagg_lb_port_create(lp);
2194 lagg_lb_detach(struct lagg_softc *sc)
2198 lb = (struct lagg_lb *)sc->sc_psc;
2204 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2206 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2207 struct lagg_port *lp_next;
2211 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2212 LAGG_XLOCK_ASSERT(sc);
2213 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2216 if (i >= LAGG_MAX_PORTS) {
2220 if (sc->sc_ifflags & IFF_DEBUG)
2221 printf("%s: port %s at index %d\n",
2222 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2223 lb->lb_ports[i++] = lp_next;
2230 lagg_lb_port_create(struct lagg_port *lp)
2232 struct lagg_softc *sc = lp->lp_softc;
2233 return (lagg_lb_porttable(sc, NULL));
2237 lagg_lb_port_destroy(struct lagg_port *lp)
2239 struct lagg_softc *sc = lp->lp_softc;
2240 lagg_lb_porttable(sc, lp);
2244 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2246 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2247 struct lagg_port *lp = NULL;
2250 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2251 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2252 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2254 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2256 lp = lb->lb_ports[p];
2259 * Check the port's link state. This will return the next active
2260 * port if the link is down or the port is NULL.
2262 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2268 return (lagg_enqueue(lp->lp_ifp, m));
2271 static struct mbuf *
2272 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2274 struct ifnet *ifp = sc->sc_ifp;
2276 /* Just pass in the packet to our lagg device */
2277 m->m_pkthdr.rcvif = ifp;
2286 lagg_lacp_attach(struct lagg_softc *sc)
2288 struct lagg_port *lp;
2291 LAGG_XLOCK_ASSERT(sc);
2292 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2293 lacp_port_create(lp);
2297 lagg_lacp_detach(struct lagg_softc *sc)
2299 struct lagg_port *lp;
2302 LAGG_XLOCK_ASSERT(sc);
2303 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2304 lacp_port_destroy(lp);
2312 lagg_lacp_lladdr(struct lagg_softc *sc)
2314 struct lagg_port *lp;
2316 LAGG_SXLOCK_ASSERT(sc);
2318 /* purge all the lacp ports */
2319 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2320 lacp_port_destroy(lp);
2322 /* add them back in */
2323 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2324 lacp_port_create(lp);
2328 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2330 struct lagg_port *lp;
2332 lp = lacp_select_tx_port(sc, m);
2339 return (lagg_enqueue(lp->lp_ifp, m));
2342 static struct mbuf *
2343 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2345 struct ifnet *ifp = sc->sc_ifp;
2346 struct ether_header *eh;
2349 eh = mtod(m, struct ether_header *);
2350 etype = ntohs(eh->ether_type);
2352 /* Tap off LACP control messages */
2353 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2354 m = lacp_input(lp, m);
2360 * If the port is not collecting or not in the active aggregator then
2363 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2368 m->m_pkthdr.rcvif = ifp;