1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */
4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6 * Copyright (c) 2014, 2016 Marcelo Araujo <araujo@FreeBSD.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 #include "opt_inet6.h"
26 #include "opt_ratelimit.h"
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35 #include <sys/sysctl.h>
36 #include <sys/module.h>
38 #include <sys/systm.h>
41 #include <sys/rmlock.h>
43 #include <sys/taskqueue.h>
44 #include <sys/eventhandler.h>
46 #include <net/ethernet.h>
48 #include <net/if_clone.h>
49 #include <net/if_arp.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
53 #include <net/if_var.h>
57 #if defined(INET) || defined(INET6)
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/if_ether.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/in6_ifattach.h>
72 #include <net/if_vlan_var.h>
73 #include <net/if_lagg.h>
74 #include <net/ieee8023ad_lacp.h>
76 #define LAGG_RLOCK() struct epoch_tracker lagg_et; epoch_enter_preempt(net_epoch_preempt, &lagg_et)
77 #define LAGG_RUNLOCK() epoch_exit_preempt(net_epoch_preempt, &lagg_et)
78 #define LAGG_RLOCK_ASSERT() MPASS(in_epoch(net_epoch_preempt))
79 #define LAGG_UNLOCK_ASSERT() MPASS(!in_epoch(net_epoch_preempt))
81 #define LAGG_SX_INIT(_sc) sx_init(&(_sc)->sc_sx, "if_lagg sx")
82 #define LAGG_SX_DESTROY(_sc) sx_destroy(&(_sc)->sc_sx)
83 #define LAGG_XLOCK(_sc) sx_xlock(&(_sc)->sc_sx)
84 #define LAGG_XUNLOCK(_sc) sx_xunlock(&(_sc)->sc_sx)
85 #define LAGG_SXLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_LOCKED)
86 #define LAGG_XLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
88 /* Special flags we should propagate to the lagg ports. */
91 int (*func)(struct ifnet *, int);
93 {IFF_PROMISC, ifpromisc},
94 {IFF_ALLMULTI, if_allmulti},
98 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
99 #define V_lagg_list VNET(lagg_list)
100 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
101 #define V_lagg_list_mtx VNET(lagg_list_mtx)
102 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
103 "if_lagg list", NULL, MTX_DEF)
104 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
105 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
106 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
107 eventhandler_tag lagg_detach_cookie = NULL;
109 static int lagg_clone_create(struct if_clone *, int, caddr_t);
110 static void lagg_clone_destroy(struct ifnet *);
111 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
112 #define V_lagg_cloner VNET(lagg_cloner)
113 static const char laggname[] = "lagg";
114 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
116 static void lagg_capabilities(struct lagg_softc *);
117 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
118 static int lagg_port_destroy(struct lagg_port *, int);
119 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
120 static void lagg_linkstate(struct lagg_softc *);
121 static void lagg_port_state(struct ifnet *, int);
122 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
123 static int lagg_port_output(struct ifnet *, struct mbuf *,
124 const struct sockaddr *, struct route *);
125 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
126 #ifdef LAGG_PORT_STACKING
127 static int lagg_port_checkstacking(struct lagg_softc *);
129 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
130 static void lagg_init(void *);
131 static void lagg_stop(struct lagg_softc *);
132 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
134 static int lagg_snd_tag_alloc(struct ifnet *,
135 union if_snd_tag_alloc_params *,
136 struct m_snd_tag **);
138 static int lagg_setmulti(struct lagg_port *);
139 static int lagg_clrmulti(struct lagg_port *);
140 static int lagg_setcaps(struct lagg_port *, int cap);
141 static int lagg_setflag(struct lagg_port *, int, int,
142 int (*func)(struct ifnet *, int));
143 static int lagg_setflags(struct lagg_port *, int status);
144 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
145 static int lagg_transmit(struct ifnet *, struct mbuf *);
146 static void lagg_qflush(struct ifnet *);
147 static int lagg_media_change(struct ifnet *);
148 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
149 static struct lagg_port *lagg_link_active(struct lagg_softc *,
152 /* Simple round robin */
153 static void lagg_rr_attach(struct lagg_softc *);
154 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
155 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
158 /* Active failover */
159 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
160 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
164 static void lagg_lb_attach(struct lagg_softc *);
165 static void lagg_lb_detach(struct lagg_softc *);
166 static int lagg_lb_port_create(struct lagg_port *);
167 static void lagg_lb_port_destroy(struct lagg_port *);
168 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
169 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
171 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
174 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
175 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
179 static void lagg_lacp_attach(struct lagg_softc *);
180 static void lagg_lacp_detach(struct lagg_softc *);
181 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
182 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
184 static void lagg_lacp_lladdr(struct lagg_softc *);
186 /* lagg protocol table */
187 static const struct lagg_proto {
189 void (*pr_attach)(struct lagg_softc *);
190 void (*pr_detach)(struct lagg_softc *);
191 int (*pr_start)(struct lagg_softc *, struct mbuf *);
192 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
194 int (*pr_addport)(struct lagg_port *);
195 void (*pr_delport)(struct lagg_port *);
196 void (*pr_linkstate)(struct lagg_port *);
197 void (*pr_init)(struct lagg_softc *);
198 void (*pr_stop)(struct lagg_softc *);
199 void (*pr_lladdr)(struct lagg_softc *);
200 void (*pr_request)(struct lagg_softc *, void *);
201 void (*pr_portreq)(struct lagg_port *, void *);
204 .pr_num = LAGG_PROTO_NONE
207 .pr_num = LAGG_PROTO_ROUNDROBIN,
208 .pr_attach = lagg_rr_attach,
209 .pr_start = lagg_rr_start,
210 .pr_input = lagg_rr_input,
213 .pr_num = LAGG_PROTO_FAILOVER,
214 .pr_start = lagg_fail_start,
215 .pr_input = lagg_fail_input,
218 .pr_num = LAGG_PROTO_LOADBALANCE,
219 .pr_attach = lagg_lb_attach,
220 .pr_detach = lagg_lb_detach,
221 .pr_start = lagg_lb_start,
222 .pr_input = lagg_lb_input,
223 .pr_addport = lagg_lb_port_create,
224 .pr_delport = lagg_lb_port_destroy,
227 .pr_num = LAGG_PROTO_LACP,
228 .pr_attach = lagg_lacp_attach,
229 .pr_detach = lagg_lacp_detach,
230 .pr_start = lagg_lacp_start,
231 .pr_input = lagg_lacp_input,
232 .pr_addport = lacp_port_create,
233 .pr_delport = lacp_port_destroy,
234 .pr_linkstate = lacp_linkstate,
235 .pr_init = lacp_init,
236 .pr_stop = lacp_stop,
237 .pr_lladdr = lagg_lacp_lladdr,
238 .pr_request = lacp_req,
239 .pr_portreq = lacp_portreq,
242 .pr_num = LAGG_PROTO_BROADCAST,
243 .pr_start = lagg_bcast_start,
244 .pr_input = lagg_bcast_input,
248 SYSCTL_DECL(_net_link);
249 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
252 /* Allow input on any failover links */
253 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
254 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
255 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
256 &VNET_NAME(lagg_failover_rx_all), 0,
257 "Accept input from any interface in a failover lagg");
259 /* Default value for using flowid */
260 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
261 #define V_def_use_flowid VNET(def_use_flowid)
262 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
263 &VNET_NAME(def_use_flowid), 0,
264 "Default setting for using flow id for load sharing");
266 /* Default value for flowid shift */
267 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
268 #define V_def_flowid_shift VNET(def_flowid_shift)
269 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
270 &VNET_NAME(def_flowid_shift), 0,
271 "Default setting for flowid shift for load sharing");
274 vnet_lagg_init(const void *unused __unused)
277 LAGG_LIST_LOCK_INIT();
278 SLIST_INIT(&V_lagg_list);
279 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
280 lagg_clone_destroy, 0);
282 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
283 vnet_lagg_init, NULL);
286 vnet_lagg_uninit(const void *unused __unused)
289 if_clone_detach(V_lagg_cloner);
290 LAGG_LIST_LOCK_DESTROY();
292 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
293 vnet_lagg_uninit, NULL);
296 lagg_modevent(module_t mod, int type, void *data)
301 lagg_input_p = lagg_input;
302 lagg_linkstate_p = lagg_port_state;
303 lagg_detach_cookie = EVENTHANDLER_REGISTER(
304 ifnet_departure_event, lagg_port_ifdetach, NULL,
305 EVENTHANDLER_PRI_ANY);
308 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
311 lagg_linkstate_p = NULL;
319 static moduledata_t lagg_mod = {
325 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
326 MODULE_VERSION(if_lagg, 1);
329 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
332 LAGG_XLOCK_ASSERT(sc);
333 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
336 if (sc->sc_ifflags & IFF_DEBUG)
337 if_printf(sc->sc_ifp, "using proto %u\n", pr);
339 if (lagg_protos[pr].pr_attach != NULL)
340 lagg_protos[pr].pr_attach(sc);
345 lagg_proto_detach(struct lagg_softc *sc)
349 LAGG_XLOCK_ASSERT(sc);
351 sc->sc_proto = LAGG_PROTO_NONE;
353 if (lagg_protos[pr].pr_detach != NULL)
354 lagg_protos[pr].pr_detach(sc);
358 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
361 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
365 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
368 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
372 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
375 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
378 return (lagg_protos[sc->sc_proto].pr_addport(lp));
382 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
385 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
386 lagg_protos[sc->sc_proto].pr_delport(lp);
390 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
393 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
394 lagg_protos[sc->sc_proto].pr_linkstate(lp);
398 lagg_proto_init(struct lagg_softc *sc)
401 if (lagg_protos[sc->sc_proto].pr_init != NULL)
402 lagg_protos[sc->sc_proto].pr_init(sc);
406 lagg_proto_stop(struct lagg_softc *sc)
409 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
410 lagg_protos[sc->sc_proto].pr_stop(sc);
414 lagg_proto_lladdr(struct lagg_softc *sc)
417 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
418 lagg_protos[sc->sc_proto].pr_lladdr(sc);
422 lagg_proto_request(struct lagg_softc *sc, void *v)
425 if (lagg_protos[sc->sc_proto].pr_request != NULL)
426 lagg_protos[sc->sc_proto].pr_request(sc, v);
430 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
433 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
434 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
438 * This routine is run via an vlan
442 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
444 struct lagg_softc *sc = ifp->if_softc;
445 struct lagg_port *lp;
447 if (ifp->if_softc != arg) /* Not our event */
451 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
452 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
457 * This routine is run via an vlan
461 lagg_unregister_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_unconfig, lp->lp_ifp, vtag);
476 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
478 struct lagg_softc *sc;
480 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
482 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
483 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
491 if (V_def_use_flowid)
492 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
493 sc->flowid_shift = V_def_flowid_shift;
495 /* Hash all layers by default */
496 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
498 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
500 CK_SLIST_INIT(&sc->sc_ports);
502 /* Initialise pseudo media types */
503 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
505 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
506 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
508 if_initname(ifp, laggname, unit);
510 ifp->if_transmit = lagg_transmit;
511 ifp->if_qflush = lagg_qflush;
512 ifp->if_init = lagg_init;
513 ifp->if_ioctl = lagg_ioctl;
514 ifp->if_get_counter = lagg_get_counter;
515 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
517 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
519 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
522 * Attach as an ordinary ethernet device, children will be attached
523 * as special device IFT_IEEE8023ADLAG.
525 ether_ifattach(ifp, eaddr);
527 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
528 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
529 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
530 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
532 /* Insert into the global list of laggs */
534 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
542 lagg_clone_destroy(struct ifnet *ifp)
544 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
545 struct lagg_port *lp;
548 sc->sc_destroying = 1;
550 ifp->if_flags &= ~IFF_UP;
552 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
553 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
555 /* Shutdown and remove lagg ports */
556 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
557 lagg_port_destroy(lp, 1);
559 /* Unhook the aggregation protocol */
560 lagg_proto_detach(sc);
563 ifmedia_removeall(&sc->sc_media);
568 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
576 lagg_capabilities(struct lagg_softc *sc)
578 struct lagg_port *lp;
581 struct ifnet_hw_tsomax hw_tsomax;
583 LAGG_XLOCK_ASSERT(sc);
585 /* Get common enabled capabilities for the lagg ports */
587 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
588 ena &= lp->lp_ifp->if_capenable;
589 ena = (ena == ~0 ? 0 : ena);
592 * Apply common enabled capabilities back to the lagg ports.
593 * May require several iterations if they are dependent.
597 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
598 lagg_setcaps(lp, ena);
599 ena &= lp->lp_ifp->if_capenable;
601 } while (pena != ena);
603 /* Get other capabilities from the lagg ports */
606 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
607 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
608 cap &= lp->lp_ifp->if_capabilities;
609 hwa &= lp->lp_ifp->if_hwassist;
610 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
612 cap = (cap == ~0 ? 0 : cap);
613 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
615 if (sc->sc_ifp->if_capabilities != cap ||
616 sc->sc_ifp->if_capenable != ena ||
617 sc->sc_ifp->if_hwassist != hwa ||
618 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
619 sc->sc_ifp->if_capabilities = cap;
620 sc->sc_ifp->if_capenable = ena;
621 sc->sc_ifp->if_hwassist = hwa;
622 getmicrotime(&sc->sc_ifp->if_lastchange);
624 if (sc->sc_ifflags & IFF_DEBUG)
625 if_printf(sc->sc_ifp,
626 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
631 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
633 struct lagg_softc *sc_ptr;
634 struct lagg_port *lp, *tlp;
636 int error, i, oldmtu;
639 LAGG_XLOCK_ASSERT(sc);
641 if (sc->sc_ifp == ifp) {
642 if_printf(sc->sc_ifp,
643 "cannot add a lagg to itself as a port\n");
647 /* Limit the maximal number of lagg ports */
648 if (sc->sc_count >= LAGG_MAX_PORTS)
651 /* Check if port has already been associated to a lagg */
652 if (ifp->if_lagg != NULL) {
653 /* Port is already in the current lagg? */
654 lp = (struct lagg_port *)ifp->if_lagg;
655 if (lp->lp_softc == sc)
660 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
661 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
662 return (EPROTONOSUPPORT);
664 /* Allow the first Ethernet member to define the MTU */
666 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
667 sc->sc_ifp->if_mtu = ifp->if_mtu;
668 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
669 if (ifp->if_ioctl == NULL) {
670 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
674 oldmtu = ifp->if_mtu;
675 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
676 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
677 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
679 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
683 ifr.ifr_mtu = oldmtu;
686 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
689 /* Check if port is a stacked lagg */
691 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
692 if (ifp == sc_ptr->sc_ifp) {
696 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
699 /* XXX disable stacking for the moment, its untested */
700 #ifdef LAGG_PORT_STACKING
701 lp->lp_flags |= LAGG_PORT_STACK;
702 if (lagg_port_checkstacking(sc_ptr) >=
707 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
719 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
720 lp->lp_ifcapenable = ifp->if_capenable;
721 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
722 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
723 lagg_proto_lladdr(sc);
724 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
726 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
728 lagg_setflags(lp, 1);
730 if (CK_SLIST_EMPTY(&sc->sc_ports))
733 /* Change the interface type */
734 lp->lp_iftype = ifp->if_type;
735 ifp->if_type = IFT_IEEE8023ADLAG;
737 lp->lp_ioctl = ifp->if_ioctl;
738 ifp->if_ioctl = lagg_port_ioctl;
739 lp->lp_output = ifp->if_output;
740 ifp->if_output = lagg_port_output;
742 /* Read port counters */
743 pval = lp->port_counters.val;
744 for (i = 0; i < IFCOUNTERS; i++, pval++)
745 *pval = ifp->if_get_counter(ifp, i);
748 * Insert into the list of ports.
749 * Keep ports sorted by if_index. It is handy, when configuration
750 * is predictable and `ifconfig laggN create ...` command
751 * will lead to the same result each time.
753 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
754 if (tlp->lp_ifp->if_index < ifp->if_index && (
755 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
756 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
761 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
763 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
769 if ((error = lagg_proto_addport(sc, lp)) != 0) {
770 /* Remove the port, without calling pr_delport. */
771 lagg_port_destroy(lp, 0);
773 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
777 /* Update lagg capabilities */
778 lagg_capabilities(sc);
784 #ifdef LAGG_PORT_STACKING
786 lagg_port_checkstacking(struct lagg_softc *sc)
788 struct lagg_softc *sc_ptr;
789 struct lagg_port *lp;
792 LAGG_SXLOCK_ASSERT(sc);
793 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
794 if (lp->lp_flags & LAGG_PORT_STACK) {
795 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
796 m = MAX(m, lagg_port_checkstacking(sc_ptr));
805 lagg_port_destroy_cb(epoch_context_t ec)
807 struct lagg_port *lp;
810 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
818 lagg_port_destroy(struct lagg_port *lp, int rundelport)
820 struct lagg_softc *sc = lp->lp_softc;
821 struct lagg_port *lp_ptr, *lp0;
822 struct ifnet *ifp = lp->lp_ifp;
823 uint64_t *pval, vdiff;
826 LAGG_XLOCK_ASSERT(sc);
829 lagg_proto_delport(sc, lp);
831 if (lp->lp_detaching == 0)
834 /* Restore interface */
835 ifp->if_type = lp->lp_iftype;
836 ifp->if_ioctl = lp->lp_ioctl;
837 ifp->if_output = lp->lp_output;
840 /* Update detached port counters */
841 pval = lp->port_counters.val;
842 for (i = 0; i < IFCOUNTERS; i++, pval++) {
843 vdiff = ifp->if_get_counter(ifp, i) - *pval;
844 sc->detached_counters.val[i] += vdiff;
847 /* Finally, remove the port from the lagg */
848 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
851 /* Update the primary interface */
852 if (lp == sc->sc_primary) {
853 uint8_t lladdr[ETHER_ADDR_LEN];
855 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
856 bzero(&lladdr, ETHER_ADDR_LEN);
858 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
859 sc->sc_primary = lp0;
860 if (sc->sc_destroying == 0) {
861 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
862 lagg_proto_lladdr(sc);
863 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
867 * Update lladdr for each port (new primary needs update
868 * as well, to switch from old lladdr to its 'real' one)
870 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
871 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
875 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
877 if (lp->lp_detaching == 0) {
878 lagg_setflags(lp, 0);
879 lagg_setcaps(lp, lp->lp_ifcapenable);
880 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
884 * free port and release it's ifnet reference after a grace period has
887 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
888 /* Update lagg capabilities */
889 lagg_capabilities(sc);
896 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
898 struct lagg_reqport *rp = (struct lagg_reqport *)data;
899 struct lagg_softc *sc;
900 struct lagg_port *lp = NULL;
903 /* Should be checked by the caller */
904 if (ifp->if_type != IFT_IEEE8023ADLAG ||
905 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
910 if (rp->rp_portname[0] == '\0' ||
911 ifunit(rp->rp_portname) != ifp) {
917 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
923 lagg_port2req(lp, rp);
928 if (lp->lp_ioctl == NULL) {
932 error = (*lp->lp_ioctl)(ifp, cmd, data);
936 /* Update lagg interface capabilities */
938 lagg_capabilities(sc);
940 VLAN_CAPABILITIES(sc->sc_ifp);
944 /* Do not allow the MTU to be changed once joined */
955 if (lp != NULL && lp->lp_ioctl != NULL)
956 return ((*lp->lp_ioctl)(ifp, cmd, data));
962 * Requests counter @cnt data.
964 * Counter value is calculated the following way:
965 * 1) for each port, sum difference between current and "initial" measurements.
966 * 2) add lagg logical interface counters.
967 * 3) add data from detached_counters array.
969 * We also do the following things on ports attach/detach:
970 * 1) On port attach we store all counters it has into port_counter array.
971 * 2) On port detach we add the different between "initial" and
972 * current counters data to detached_counters array.
975 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
977 struct lagg_softc *sc;
978 struct lagg_port *lp;
980 uint64_t newval, oldval, vsum;
982 /* Revise this when we've got non-generic counters. */
983 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
985 sc = (struct lagg_softc *)ifp->if_softc;
989 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
990 /* Saved attached value */
991 oldval = lp->port_counters.val[cnt];
994 newval = lpifp->if_get_counter(lpifp, cnt);
995 /* Calculate diff and save new */
996 vsum += newval - oldval;
1001 * Add counter data which might be added by upper
1002 * layer protocols operating on logical interface.
1004 vsum += if_get_counter_default(ifp, cnt);
1007 * Add counter data from detached ports counters
1009 vsum += sc->detached_counters.val[cnt];
1016 * For direct output to child ports.
1019 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1020 const struct sockaddr *dst, struct route *ro)
1022 struct lagg_port *lp = ifp->if_lagg;
1024 switch (dst->sa_family) {
1025 case pseudo_AF_HDRCMPLT:
1027 return ((*lp->lp_output)(ifp, m, dst, ro));
1030 /* drop any other frames */
1036 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1038 struct lagg_port *lp;
1039 struct lagg_softc *sc;
1041 if ((lp = ifp->if_lagg) == NULL)
1043 /* If the ifnet is just being renamed, don't do anything. */
1044 if (ifp->if_flags & IFF_RENAMING)
1050 lp->lp_detaching = 1;
1051 lagg_port_destroy(lp, 1);
1053 VLAN_CAPABILITIES(sc->sc_ifp);
1057 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1059 struct lagg_softc *sc = lp->lp_softc;
1061 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1062 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1063 rp->rp_prio = lp->lp_prio;
1064 rp->rp_flags = lp->lp_flags;
1065 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1067 /* Add protocol specific flags */
1068 switch (sc->sc_proto) {
1069 case LAGG_PROTO_FAILOVER:
1070 if (lp == sc->sc_primary)
1071 rp->rp_flags |= LAGG_PORT_MASTER;
1072 if (lp == lagg_link_active(sc, sc->sc_primary))
1073 rp->rp_flags |= LAGG_PORT_ACTIVE;
1076 case LAGG_PROTO_ROUNDROBIN:
1077 case LAGG_PROTO_LOADBALANCE:
1078 case LAGG_PROTO_BROADCAST:
1079 if (LAGG_PORTACTIVE(lp))
1080 rp->rp_flags |= LAGG_PORT_ACTIVE;
1083 case LAGG_PROTO_LACP:
1084 /* LACP has a different definition of active */
1085 if (lacp_isactive(lp))
1086 rp->rp_flags |= LAGG_PORT_ACTIVE;
1087 if (lacp_iscollecting(lp))
1088 rp->rp_flags |= LAGG_PORT_COLLECTING;
1089 if (lacp_isdistributing(lp))
1090 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1097 lagg_init(void *xsc)
1099 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1100 struct ifnet *ifp = sc->sc_ifp;
1101 struct lagg_port *lp;
1104 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1109 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1112 * Update the port lladdrs if needed.
1113 * This might be if_setlladdr() notification
1114 * that lladdr has been changed.
1116 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1117 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1118 ETHER_ADDR_LEN) != 0)
1119 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1122 lagg_proto_init(sc);
1128 lagg_stop(struct lagg_softc *sc)
1130 struct ifnet *ifp = sc->sc_ifp;
1132 LAGG_XLOCK_ASSERT(sc);
1134 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1137 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1139 lagg_proto_stop(sc);
1143 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1145 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1146 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1147 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1148 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1149 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1150 struct ifreq *ifr = (struct ifreq *)data;
1151 struct lagg_port *lp;
1153 struct thread *td = curthread;
1155 int count, buflen, len, error = 0;
1157 bzero(&rpbuf, sizeof(rpbuf));
1162 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1163 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1164 ra->ra_proto = sc->sc_proto;
1165 lagg_proto_request(sc, &ra->ra_psc);
1168 len = min(ra->ra_size, buflen);
1169 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1170 if (len < sizeof(rpbuf))
1173 lagg_port2req(lp, &rpbuf);
1174 memcpy(buf, &rpbuf, sizeof(rpbuf));
1176 buf += sizeof(rpbuf);
1177 len -= sizeof(rpbuf);
1180 ra->ra_ports = count;
1181 ra->ra_size = count * sizeof(rpbuf);
1182 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1183 free(outbuf, M_TEMP);
1186 error = priv_check(td, PRIV_NET_LAGG);
1189 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1190 error = EPROTONOSUPPORT;
1195 lagg_proto_detach(sc);
1196 LAGG_UNLOCK_ASSERT();
1197 lagg_proto_attach(sc, ra->ra_proto);
1202 ro->ro_opts = sc->sc_opts;
1203 if (sc->sc_proto == LAGG_PROTO_LACP) {
1204 struct lacp_softc *lsc;
1206 lsc = (struct lacp_softc *)sc->sc_psc;
1207 if (lsc->lsc_debug.lsc_tx_test != 0)
1208 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1209 if (lsc->lsc_debug.lsc_rx_test != 0)
1210 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1211 if (lsc->lsc_strict_mode != 0)
1212 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1213 if (lsc->lsc_fast_timeout != 0)
1214 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1216 ro->ro_active = sc->sc_active;
1219 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1220 ro->ro_active += LAGG_PORTACTIVE(lp);
1222 ro->ro_bkt = sc->sc_bkt;
1223 ro->ro_flapping = sc->sc_flapping;
1224 ro->ro_flowid_shift = sc->flowid_shift;
1228 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1229 if (ro->ro_bkt == 0)
1230 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1232 sc->sc_bkt = ro->ro_bkt;
1234 error = priv_check(td, PRIV_NET_LAGG);
1237 if (ro->ro_opts == 0)
1240 * Set options. LACP options are stored in sc->sc_psc,
1245 switch (ro->ro_opts) {
1246 case LAGG_OPT_USE_FLOWID:
1247 case -LAGG_OPT_USE_FLOWID:
1248 case LAGG_OPT_FLOWIDSHIFT:
1252 case LAGG_OPT_LACP_TXTEST:
1253 case -LAGG_OPT_LACP_TXTEST:
1254 case LAGG_OPT_LACP_RXTEST:
1255 case -LAGG_OPT_LACP_RXTEST:
1256 case LAGG_OPT_LACP_STRICT:
1257 case -LAGG_OPT_LACP_STRICT:
1258 case LAGG_OPT_LACP_TIMEOUT:
1259 case -LAGG_OPT_LACP_TIMEOUT:
1270 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1271 /* Invalid combination of options specified. */
1274 break; /* Return from SIOCSLAGGOPTS. */
1277 * Store new options into sc->sc_opts except for
1278 * FLOWIDSHIFT and LACP options.
1281 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1282 sc->flowid_shift = ro->ro_flowid_shift;
1283 else if (ro->ro_opts > 0)
1284 sc->sc_opts |= ro->ro_opts;
1286 sc->sc_opts &= ~ro->ro_opts;
1288 struct lacp_softc *lsc;
1289 struct lacp_port *lp;
1291 lsc = (struct lacp_softc *)sc->sc_psc;
1293 switch (ro->ro_opts) {
1294 case LAGG_OPT_LACP_TXTEST:
1295 lsc->lsc_debug.lsc_tx_test = 1;
1297 case -LAGG_OPT_LACP_TXTEST:
1298 lsc->lsc_debug.lsc_tx_test = 0;
1300 case LAGG_OPT_LACP_RXTEST:
1301 lsc->lsc_debug.lsc_rx_test = 1;
1303 case -LAGG_OPT_LACP_RXTEST:
1304 lsc->lsc_debug.lsc_rx_test = 0;
1306 case LAGG_OPT_LACP_STRICT:
1307 lsc->lsc_strict_mode = 1;
1309 case -LAGG_OPT_LACP_STRICT:
1310 lsc->lsc_strict_mode = 0;
1312 case LAGG_OPT_LACP_TIMEOUT:
1314 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1315 lp->lp_state |= LACP_STATE_TIMEOUT;
1317 lsc->lsc_fast_timeout = 1;
1319 case -LAGG_OPT_LACP_TIMEOUT:
1321 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1322 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1324 lsc->lsc_fast_timeout = 0;
1330 case SIOCGLAGGFLAGS:
1333 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1334 rf->rf_flags |= LAGG_F_HASHL2;
1335 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1336 rf->rf_flags |= LAGG_F_HASHL3;
1337 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1338 rf->rf_flags |= LAGG_F_HASHL4;
1342 error = priv_check(td, PRIV_NET_LAGG);
1345 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1351 if (rf->rf_flags & LAGG_F_HASHL2)
1352 sc->sc_flags |= MBUF_HASHFLAG_L2;
1353 if (rf->rf_flags & LAGG_F_HASHL3)
1354 sc->sc_flags |= MBUF_HASHFLAG_L3;
1355 if (rf->rf_flags & LAGG_F_HASHL4)
1356 sc->sc_flags |= MBUF_HASHFLAG_L4;
1360 if (rp->rp_portname[0] == '\0' ||
1361 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1367 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1368 lp->lp_softc != sc) {
1375 lagg_port2req(lp, rp);
1380 error = priv_check(td, PRIV_NET_LAGG);
1383 if (rp->rp_portname[0] == '\0' ||
1384 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1390 * A laggport interface should not have inet6 address
1391 * because two interfaces with a valid link-local
1392 * scope zone must not be merged in any form. This
1393 * restriction is needed to prevent violation of
1394 * link-local scope zone. Attempts to add a laggport
1395 * interface which has inet6 addresses triggers
1396 * removal of all inet6 addresses on the member
1399 if (in6ifa_llaonifp(tpif)) {
1401 if_printf(sc->sc_ifp,
1402 "IPv6 addresses on %s have been removed "
1403 "before adding it as a member to prevent "
1404 "IPv6 address scope violation.\n",
1409 error = lagg_port_create(sc, tpif);
1412 VLAN_CAPABILITIES(ifp);
1414 case SIOCSLAGGDELPORT:
1415 error = priv_check(td, PRIV_NET_LAGG);
1418 if (rp->rp_portname[0] == '\0' ||
1419 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1425 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1426 lp->lp_softc != sc) {
1433 error = lagg_port_destroy(lp, 1);
1436 VLAN_CAPABILITIES(ifp);
1439 /* Set flags on ports too */
1441 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1442 lagg_setflags(lp, 1);
1445 if (!(ifp->if_flags & IFF_UP) &&
1446 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1448 * If interface is marked down and it is running,
1449 * then stop and disable it.
1453 } else if ((ifp->if_flags & IFF_UP) &&
1454 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1456 * If interface is marked up and it is stopped, then
1460 (*ifp->if_init)(sc);
1467 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1476 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1481 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1482 if (lp->lp_ioctl != NULL)
1483 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1485 lagg_capabilities(sc);
1487 VLAN_CAPABILITIES(ifp);
1493 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1494 if (lp->lp_ioctl != NULL)
1495 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1500 "failed to change MTU to %d on port %s, "
1501 "reverting all ports to original MTU (%d)\n",
1502 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1507 ifp->if_mtu = ifr->ifr_mtu;
1509 /* set every port back to the original MTU */
1510 ifr->ifr_mtu = ifp->if_mtu;
1511 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1512 if (lp->lp_ioctl != NULL)
1513 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1520 error = ether_ioctl(ifp, cmd, data);
1528 lagg_snd_tag_alloc(struct ifnet *ifp,
1529 union if_snd_tag_alloc_params *params,
1530 struct m_snd_tag **ppmt)
1532 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1533 struct lagg_port *lp;
1538 switch (sc->sc_proto) {
1539 case LAGG_PROTO_FAILOVER:
1540 lp = lagg_link_active(sc, sc->sc_primary);
1542 case LAGG_PROTO_LOADBALANCE:
1543 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1544 params->hdr.flowtype == M_HASHTYPE_NONE) {
1546 return (EOPNOTSUPP);
1548 p = params->hdr.flowid >> sc->flowid_shift;
1550 lb = (struct lagg_lb *)sc->sc_psc;
1551 lp = lb->lb_ports[p];
1552 lp = lagg_link_active(sc, lp);
1554 case LAGG_PROTO_LACP:
1555 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1556 params->hdr.flowtype == M_HASHTYPE_NONE) {
1558 return (EOPNOTSUPP);
1560 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1564 return (EOPNOTSUPP);
1568 return (EOPNOTSUPP);
1572 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1573 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1574 return (EOPNOTSUPP);
1576 /* forward allocation request */
1577 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1582 lagg_setmulti(struct lagg_port *lp)
1584 struct lagg_softc *sc = lp->lp_softc;
1585 struct ifnet *ifp = lp->lp_ifp;
1586 struct ifnet *scifp = sc->sc_ifp;
1588 struct ifmultiaddr *ifma;
1591 IF_ADDR_WLOCK(scifp);
1592 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1593 if (ifma->ifma_addr->sa_family != AF_LINK)
1595 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1597 IF_ADDR_WUNLOCK(scifp);
1600 bcopy(ifma->ifma_addr, &mc->mc_addr,
1601 ifma->ifma_addr->sa_len);
1602 mc->mc_addr.sdl_index = ifp->if_index;
1604 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1606 IF_ADDR_WUNLOCK(scifp);
1607 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1608 error = if_addmulti(ifp,
1609 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1617 lagg_clrmulti(struct lagg_port *lp)
1621 LAGG_XLOCK_ASSERT(lp->lp_softc);
1622 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1623 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1624 if (mc->mc_ifma && lp->lp_detaching == 0)
1625 if_delmulti_ifma(mc->mc_ifma);
1632 lagg_setcaps(struct lagg_port *lp, int cap)
1636 if (lp->lp_ifp->if_capenable == cap)
1638 if (lp->lp_ioctl == NULL)
1640 ifr.ifr_reqcap = cap;
1641 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1644 /* Handle a ref counted flag that should be set on the lagg port as well */
1646 lagg_setflag(struct lagg_port *lp, int flag, int status,
1647 int (*func)(struct ifnet *, int))
1649 struct lagg_softc *sc = lp->lp_softc;
1650 struct ifnet *scifp = sc->sc_ifp;
1651 struct ifnet *ifp = lp->lp_ifp;
1654 LAGG_XLOCK_ASSERT(sc);
1656 status = status ? (scifp->if_flags & flag) : 0;
1657 /* Now "status" contains the flag value or 0 */
1660 * See if recorded ports status is different from what
1661 * we want it to be. If it is, flip it. We record ports
1662 * status in lp_ifflags so that we won't clear ports flag
1663 * we haven't set. In fact, we don't clear or set ports
1664 * flags directly, but get or release references to them.
1665 * That's why we can be sure that recorded flags still are
1666 * in accord with actual ports flags.
1668 if (status != (lp->lp_ifflags & flag)) {
1669 error = (*func)(ifp, status);
1672 lp->lp_ifflags &= ~flag;
1673 lp->lp_ifflags |= status;
1679 * Handle IFF_* flags that require certain changes on the lagg port
1680 * if "status" is true, update ports flags respective to the lagg
1681 * if "status" is false, forcedly clear the flags set on port.
1684 lagg_setflags(struct lagg_port *lp, int status)
1688 for (i = 0; lagg_pflags[i].flag; i++) {
1689 error = lagg_setflag(lp, lagg_pflags[i].flag,
1690 status, lagg_pflags[i].func);
1698 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1700 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1704 /* We need a Tx algorithm and at least one port */
1705 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1708 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1712 ETHER_BPF_MTAP(ifp, m);
1714 error = lagg_proto_start(sc, m);
1718 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1724 * The ifp->if_qflush entry point for lagg(4) is no-op.
1727 lagg_qflush(struct ifnet *ifp __unused)
1731 static struct mbuf *
1732 lagg_input(struct ifnet *ifp, struct mbuf *m)
1734 struct lagg_port *lp = ifp->if_lagg;
1735 struct lagg_softc *sc = lp->lp_softc;
1736 struct ifnet *scifp = sc->sc_ifp;
1739 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1740 lp->lp_detaching != 0 ||
1741 sc->sc_proto == LAGG_PROTO_NONE) {
1747 ETHER_BPF_MTAP(scifp, m);
1749 m = lagg_proto_input(sc, lp, m);
1750 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1760 lagg_media_change(struct ifnet *ifp)
1762 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1764 if (sc->sc_ifflags & IFF_DEBUG)
1765 printf("%s\n", __func__);
1772 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1774 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1775 struct lagg_port *lp;
1777 imr->ifm_status = IFM_AVALID;
1778 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1781 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1782 if (LAGG_PORTACTIVE(lp))
1783 imr->ifm_status |= IFM_ACTIVE;
1789 lagg_linkstate(struct lagg_softc *sc)
1791 struct lagg_port *lp;
1792 int new_link = LINK_STATE_DOWN;
1795 LAGG_XLOCK_ASSERT(sc);
1797 /* LACP handles link state itself */
1798 if (sc->sc_proto == LAGG_PROTO_LACP)
1801 /* Our link is considered up if at least one of our ports is active */
1803 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1804 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1805 new_link = LINK_STATE_UP;
1810 if_link_state_change(sc->sc_ifp, new_link);
1812 /* Update if_baudrate to reflect the max possible speed */
1813 switch (sc->sc_proto) {
1814 case LAGG_PROTO_FAILOVER:
1815 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1816 sc->sc_primary->lp_ifp->if_baudrate : 0;
1818 case LAGG_PROTO_ROUNDROBIN:
1819 case LAGG_PROTO_LOADBALANCE:
1820 case LAGG_PROTO_BROADCAST:
1823 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1824 speed += lp->lp_ifp->if_baudrate;
1826 sc->sc_ifp->if_baudrate = speed;
1828 case LAGG_PROTO_LACP:
1829 /* LACP updates if_baudrate itself */
1835 lagg_port_state(struct ifnet *ifp, int state)
1837 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1838 struct lagg_softc *sc = NULL;
1847 lagg_proto_linkstate(sc, lp);
1852 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1854 struct lagg_port *lp_next, *rval = NULL;
1857 * Search a port which reports an active link state.
1862 * This is called with either LAGG_RLOCK() held or
1863 * LAGG_XLOCK(sc) held.
1865 if (!in_epoch(net_epoch_preempt))
1866 LAGG_XLOCK_ASSERT(sc);
1871 if (LAGG_PORTACTIVE(lp)) {
1875 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
1876 LAGG_PORTACTIVE(lp_next)) {
1882 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1883 if (LAGG_PORTACTIVE(lp_next)) {
1892 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1895 return (ifp->if_transmit)(ifp, m);
1899 * Simple round robin aggregation
1902 lagg_rr_attach(struct lagg_softc *sc)
1905 sc->sc_bkt_count = sc->sc_bkt;
1909 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1911 struct lagg_port *lp;
1914 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1915 sc->sc_bkt_count = sc->sc_bkt;
1917 if (sc->sc_bkt > 0) {
1918 atomic_subtract_int(&sc->sc_bkt_count, 1);
1919 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1920 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1924 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1927 lp = CK_SLIST_FIRST(&sc->sc_ports);
1930 lp = CK_SLIST_NEXT(lp, lp_entries);
1933 * Check the port's link state. This will return the next active
1934 * port if the link is down or the port is NULL.
1936 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1942 return (lagg_enqueue(lp->lp_ifp, m));
1945 static struct mbuf *
1946 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1948 struct ifnet *ifp = sc->sc_ifp;
1950 /* Just pass in the packet to our lagg device */
1951 m->m_pkthdr.rcvif = ifp;
1960 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1962 int active_ports = 0;
1965 struct lagg_port *lp, *last = NULL;
1968 LAGG_RLOCK_ASSERT();
1969 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1970 if (!LAGG_PORTACTIVE(lp))
1976 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1983 ret = lagg_enqueue(last->lp_ifp, m0);
1994 if ((last = lagg_link_active(sc, last)) == NULL) {
1999 ret = lagg_enqueue(last->lp_ifp, m);
2010 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2012 struct ifnet *ifp = sc->sc_ifp;
2014 /* Just pass in the packet to our lagg device */
2015 m->m_pkthdr.rcvif = ifp;
2023 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2025 struct lagg_port *lp;
2027 /* Use the master port if active or the next available port */
2028 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2034 return (lagg_enqueue(lp->lp_ifp, m));
2037 static struct mbuf *
2038 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2040 struct ifnet *ifp = sc->sc_ifp;
2041 struct lagg_port *tmp_tp;
2043 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2044 m->m_pkthdr.rcvif = ifp;
2048 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2049 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2051 * If tmp_tp is null, we've received a packet when all
2052 * our links are down. Weird, but process it anyways.
2054 if ((tmp_tp == NULL || tmp_tp == lp)) {
2055 m->m_pkthdr.rcvif = ifp;
2068 lagg_lb_attach(struct lagg_softc *sc)
2070 struct lagg_port *lp;
2073 LAGG_XLOCK_ASSERT(sc);
2074 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2075 lb->lb_key = m_ether_tcpip_hash_init();
2078 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2079 lagg_lb_port_create(lp);
2083 lagg_lb_detach(struct lagg_softc *sc)
2087 lb = (struct lagg_lb *)sc->sc_psc;
2093 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2095 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2096 struct lagg_port *lp_next;
2100 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2101 LAGG_XLOCK_ASSERT(sc);
2102 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2105 if (i >= LAGG_MAX_PORTS) {
2109 if (sc->sc_ifflags & IFF_DEBUG)
2110 printf("%s: port %s at index %d\n",
2111 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2112 lb->lb_ports[i++] = lp_next;
2119 lagg_lb_port_create(struct lagg_port *lp)
2121 struct lagg_softc *sc = lp->lp_softc;
2122 return (lagg_lb_porttable(sc, NULL));
2126 lagg_lb_port_destroy(struct lagg_port *lp)
2128 struct lagg_softc *sc = lp->lp_softc;
2129 lagg_lb_porttable(sc, lp);
2133 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2135 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2136 struct lagg_port *lp = NULL;
2139 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2140 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2141 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2143 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2145 lp = lb->lb_ports[p];
2148 * Check the port's link state. This will return the next active
2149 * port if the link is down or the port is NULL.
2151 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2157 return (lagg_enqueue(lp->lp_ifp, m));
2160 static struct mbuf *
2161 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2163 struct ifnet *ifp = sc->sc_ifp;
2165 /* Just pass in the packet to our lagg device */
2166 m->m_pkthdr.rcvif = ifp;
2175 lagg_lacp_attach(struct lagg_softc *sc)
2177 struct lagg_port *lp;
2180 LAGG_XLOCK_ASSERT(sc);
2181 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2182 lacp_port_create(lp);
2186 lagg_lacp_detach(struct lagg_softc *sc)
2188 struct lagg_port *lp;
2191 LAGG_XLOCK_ASSERT(sc);
2192 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2193 lacp_port_destroy(lp);
2201 lagg_lacp_lladdr(struct lagg_softc *sc)
2203 struct lagg_port *lp;
2205 LAGG_SXLOCK_ASSERT(sc);
2207 /* purge all the lacp ports */
2208 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2209 lacp_port_destroy(lp);
2211 /* add them back in */
2212 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2213 lacp_port_create(lp);
2217 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2219 struct lagg_port *lp;
2221 lp = lacp_select_tx_port(sc, m);
2228 return (lagg_enqueue(lp->lp_ifp, m));
2231 static struct mbuf *
2232 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2234 struct ifnet *ifp = sc->sc_ifp;
2235 struct ether_header *eh;
2238 eh = mtod(m, struct ether_header *);
2239 etype = ntohs(eh->ether_type);
2241 /* Tap off LACP control messages */
2242 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2243 m = lacp_input(lp, m);
2249 * If the port is not collecting or not in the active aggregator then
2252 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2257 m->m_pkthdr.rcvif = ifp;