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>
55 #include <net/route.h>
57 #include <net/infiniband.h>
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
61 #include <netinet/ip.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/if_ether.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/in6_ifattach.h>
74 #include <net/if_vlan_var.h>
75 #include <net/if_lagg.h>
76 #include <net/ieee8023ad_lacp.h>
80 * XXX: declare here to avoid to include many inet6 related files..
81 * should be more generalized?
83 extern void nd6_setmtu(struct ifnet *);
86 #define LAGG_RLOCK() struct epoch_tracker lagg_et; epoch_enter_preempt(net_epoch_preempt, &lagg_et)
87 #define LAGG_RUNLOCK() epoch_exit_preempt(net_epoch_preempt, &lagg_et)
88 #define LAGG_RLOCK_ASSERT() MPASS(in_epoch(net_epoch_preempt))
89 #define LAGG_UNLOCK_ASSERT() MPASS(!in_epoch(net_epoch_preempt))
91 #define LAGG_SX_INIT(_sc) sx_init(&(_sc)->sc_sx, "if_lagg sx")
92 #define LAGG_SX_DESTROY(_sc) sx_destroy(&(_sc)->sc_sx)
93 #define LAGG_XLOCK(_sc) sx_xlock(&(_sc)->sc_sx)
94 #define LAGG_XUNLOCK(_sc) sx_xunlock(&(_sc)->sc_sx)
95 #define LAGG_SXLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_LOCKED)
96 #define LAGG_XLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
98 /* Special flags we should propagate to the lagg ports. */
101 int (*func)(struct ifnet *, int);
103 {IFF_PROMISC, ifpromisc},
104 {IFF_ALLMULTI, if_allmulti},
108 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
109 #define V_lagg_list VNET(lagg_list)
110 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
111 #define V_lagg_list_mtx VNET(lagg_list_mtx)
112 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
113 "if_lagg list", NULL, MTX_DEF)
114 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
115 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
116 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
117 eventhandler_tag lagg_detach_cookie = NULL;
119 static int lagg_clone_create(struct if_clone *, int, caddr_t);
120 static void lagg_clone_destroy(struct ifnet *);
121 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
122 #define V_lagg_cloner VNET(lagg_cloner)
123 static const char laggname[] = "lagg";
124 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
126 static void lagg_capabilities(struct lagg_softc *);
127 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
128 static int lagg_port_destroy(struct lagg_port *, int);
129 static struct mbuf *lagg_input_ethernet(struct ifnet *, struct mbuf *);
130 static struct mbuf *lagg_input_infiniband(struct ifnet *, struct mbuf *);
131 static void lagg_linkstate(struct lagg_softc *);
132 static void lagg_port_state(struct ifnet *, int);
133 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
134 static int lagg_port_output(struct ifnet *, struct mbuf *,
135 const struct sockaddr *, struct route *);
136 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
137 #ifdef LAGG_PORT_STACKING
138 static int lagg_port_checkstacking(struct lagg_softc *);
140 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
141 static void lagg_init(void *);
142 static void lagg_stop(struct lagg_softc *);
143 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
145 static int lagg_snd_tag_alloc(struct ifnet *,
146 union if_snd_tag_alloc_params *,
147 struct m_snd_tag **);
149 static int lagg_setmulti(struct lagg_port *);
150 static int lagg_clrmulti(struct lagg_port *);
151 static int lagg_setcaps(struct lagg_port *, int cap);
152 static int lagg_setflag(struct lagg_port *, int, int,
153 int (*func)(struct ifnet *, int));
154 static int lagg_setflags(struct lagg_port *, int status);
155 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
156 static int lagg_transmit_ethernet(struct ifnet *, struct mbuf *);
157 static int lagg_transmit_infiniband(struct ifnet *, struct mbuf *);
158 static void lagg_qflush(struct ifnet *);
159 static int lagg_media_change(struct ifnet *);
160 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
161 static struct lagg_port *lagg_link_active(struct lagg_softc *,
164 /* Simple round robin */
165 static void lagg_rr_attach(struct lagg_softc *);
166 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
167 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
170 /* Active failover */
171 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
172 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
176 static void lagg_lb_attach(struct lagg_softc *);
177 static void lagg_lb_detach(struct lagg_softc *);
178 static int lagg_lb_port_create(struct lagg_port *);
179 static void lagg_lb_port_destroy(struct lagg_port *);
180 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
181 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
183 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
186 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
187 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
191 static void lagg_lacp_attach(struct lagg_softc *);
192 static void lagg_lacp_detach(struct lagg_softc *);
193 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
194 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
196 static void lagg_lacp_lladdr(struct lagg_softc *);
198 /* lagg protocol table */
199 static const struct lagg_proto {
201 void (*pr_attach)(struct lagg_softc *);
202 void (*pr_detach)(struct lagg_softc *);
203 int (*pr_start)(struct lagg_softc *, struct mbuf *);
204 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
206 int (*pr_addport)(struct lagg_port *);
207 void (*pr_delport)(struct lagg_port *);
208 void (*pr_linkstate)(struct lagg_port *);
209 void (*pr_init)(struct lagg_softc *);
210 void (*pr_stop)(struct lagg_softc *);
211 void (*pr_lladdr)(struct lagg_softc *);
212 void (*pr_request)(struct lagg_softc *, void *);
213 void (*pr_portreq)(struct lagg_port *, void *);
216 .pr_num = LAGG_PROTO_NONE
219 .pr_num = LAGG_PROTO_ROUNDROBIN,
220 .pr_attach = lagg_rr_attach,
221 .pr_start = lagg_rr_start,
222 .pr_input = lagg_rr_input,
225 .pr_num = LAGG_PROTO_FAILOVER,
226 .pr_start = lagg_fail_start,
227 .pr_input = lagg_fail_input,
230 .pr_num = LAGG_PROTO_LOADBALANCE,
231 .pr_attach = lagg_lb_attach,
232 .pr_detach = lagg_lb_detach,
233 .pr_start = lagg_lb_start,
234 .pr_input = lagg_lb_input,
235 .pr_addport = lagg_lb_port_create,
236 .pr_delport = lagg_lb_port_destroy,
239 .pr_num = LAGG_PROTO_LACP,
240 .pr_attach = lagg_lacp_attach,
241 .pr_detach = lagg_lacp_detach,
242 .pr_start = lagg_lacp_start,
243 .pr_input = lagg_lacp_input,
244 .pr_addport = lacp_port_create,
245 .pr_delport = lacp_port_destroy,
246 .pr_linkstate = lacp_linkstate,
247 .pr_init = lacp_init,
248 .pr_stop = lacp_stop,
249 .pr_lladdr = lagg_lacp_lladdr,
250 .pr_request = lacp_req,
251 .pr_portreq = lacp_portreq,
254 .pr_num = LAGG_PROTO_BROADCAST,
255 .pr_start = lagg_bcast_start,
256 .pr_input = lagg_bcast_input,
260 SYSCTL_DECL(_net_link);
261 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
264 /* Allow input on any failover links */
265 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
266 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
267 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
268 &VNET_NAME(lagg_failover_rx_all), 0,
269 "Accept input from any interface in a failover lagg");
271 /* Default value for using flowid */
272 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
273 #define V_def_use_flowid VNET(def_use_flowid)
274 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
275 &VNET_NAME(def_use_flowid), 0,
276 "Default setting for using flow id for load sharing");
278 /* Default value for flowid shift */
279 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
280 #define V_def_flowid_shift VNET(def_flowid_shift)
281 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
282 &VNET_NAME(def_flowid_shift), 0,
283 "Default setting for flowid shift for load sharing");
286 vnet_lagg_init(const void *unused __unused)
289 LAGG_LIST_LOCK_INIT();
290 SLIST_INIT(&V_lagg_list);
291 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
292 lagg_clone_destroy, 0);
294 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
295 vnet_lagg_init, NULL);
298 vnet_lagg_uninit(const void *unused __unused)
301 if_clone_detach(V_lagg_cloner);
302 LAGG_LIST_LOCK_DESTROY();
304 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
305 vnet_lagg_uninit, NULL);
308 lagg_modevent(module_t mod, int type, void *data)
313 lagg_input_ethernet_p = lagg_input_ethernet;
314 lagg_input_infiniband_p = lagg_input_infiniband;
315 lagg_linkstate_p = lagg_port_state;
316 lagg_detach_cookie = EVENTHANDLER_REGISTER(
317 ifnet_departure_event, lagg_port_ifdetach, NULL,
318 EVENTHANDLER_PRI_ANY);
321 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
323 lagg_input_ethernet_p = NULL;
324 lagg_input_infiniband_p = NULL;
325 lagg_linkstate_p = NULL;
333 static moduledata_t lagg_mod = {
339 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
340 MODULE_VERSION(if_lagg, 1);
341 MODULE_DEPEND(if_lagg, if_infiniband, 1, 1, 1);
344 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
347 LAGG_XLOCK_ASSERT(sc);
348 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
351 if (sc->sc_ifflags & IFF_DEBUG)
352 if_printf(sc->sc_ifp, "using proto %u\n", pr);
354 if (lagg_protos[pr].pr_attach != NULL)
355 lagg_protos[pr].pr_attach(sc);
360 lagg_proto_detach(struct lagg_softc *sc)
364 LAGG_XLOCK_ASSERT(sc);
366 sc->sc_proto = LAGG_PROTO_NONE;
368 if (lagg_protos[pr].pr_detach != NULL)
369 lagg_protos[pr].pr_detach(sc);
373 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
376 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
380 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
383 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
387 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
390 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
393 return (lagg_protos[sc->sc_proto].pr_addport(lp));
397 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
400 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
401 lagg_protos[sc->sc_proto].pr_delport(lp);
405 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
408 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
409 lagg_protos[sc->sc_proto].pr_linkstate(lp);
413 lagg_proto_init(struct lagg_softc *sc)
416 if (lagg_protos[sc->sc_proto].pr_init != NULL)
417 lagg_protos[sc->sc_proto].pr_init(sc);
421 lagg_proto_stop(struct lagg_softc *sc)
424 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
425 lagg_protos[sc->sc_proto].pr_stop(sc);
429 lagg_proto_lladdr(struct lagg_softc *sc)
432 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
433 lagg_protos[sc->sc_proto].pr_lladdr(sc);
437 lagg_proto_request(struct lagg_softc *sc, void *v)
440 if (lagg_protos[sc->sc_proto].pr_request != NULL)
441 lagg_protos[sc->sc_proto].pr_request(sc, v);
445 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
448 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
449 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
453 * This routine is run via an vlan
457 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
459 struct lagg_softc *sc = ifp->if_softc;
460 struct lagg_port *lp;
462 if (ifp->if_softc != arg) /* Not our event */
466 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
467 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
472 * This routine is run via an vlan
476 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
478 struct lagg_softc *sc = ifp->if_softc;
479 struct lagg_port *lp;
481 if (ifp->if_softc != arg) /* Not our event */
485 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
486 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
491 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
493 struct iflaggparam iflp;
494 struct lagg_softc *sc;
498 static const uint8_t eaddr[LAGG_ADDR_LEN];
500 if (params != NULL) {
501 error = copyin(params, &iflp, sizeof(iflp));
505 switch (iflp.lagg_type) {
506 case LAGG_TYPE_ETHERNET:
509 case LAGG_TYPE_INFINIBAND:
510 if_type = IFT_INFINIBAND;
519 sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
520 ifp = sc->sc_ifp = if_alloc(if_type);
527 mtx_init(&sc->sc_mtx, "lagg-mtx", NULL, MTX_DEF);
528 callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
531 if (V_def_use_flowid)
532 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
533 sc->flowid_shift = V_def_flowid_shift;
535 /* Hash all layers by default */
536 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
538 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
540 CK_SLIST_INIT(&sc->sc_ports);
544 /* Initialise pseudo media types */
545 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
547 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
548 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
550 if_initname(ifp, laggname, unit);
551 ifp->if_transmit = lagg_transmit_ethernet;
554 if_initname(ifp, laggname, unit);
555 ifp->if_transmit = lagg_transmit_infiniband;
561 ifp->if_qflush = lagg_qflush;
562 ifp->if_init = lagg_init;
563 ifp->if_ioctl = lagg_ioctl;
564 ifp->if_get_counter = lagg_get_counter;
565 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
567 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
569 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
572 * Attach as an ordinary ethernet device, children will be attached
573 * as special device IFT_IEEE8023ADLAG or IFT_INFINIBANDLAG.
577 ether_ifattach(ifp, eaddr);
580 infiniband_ifattach(ifp, eaddr, sc->sc_bcast_addr);
586 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
587 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
588 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
589 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
591 /* Insert into the global list of laggs */
593 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
601 lagg_clone_destroy(struct ifnet *ifp)
603 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
604 struct lagg_port *lp;
607 sc->sc_destroying = 1;
609 ifp->if_flags &= ~IFF_UP;
611 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
612 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
614 /* Shutdown and remove lagg ports */
615 while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
616 lagg_port_destroy(lp, 1);
618 /* Unhook the aggregation protocol */
619 lagg_proto_detach(sc);
622 switch (ifp->if_type) {
624 ifmedia_removeall(&sc->sc_media);
628 infiniband_ifdetach(ifp);
636 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
639 mtx_destroy(&sc->sc_mtx);
645 lagg_capabilities(struct lagg_softc *sc)
647 struct lagg_port *lp;
650 struct ifnet_hw_tsomax hw_tsomax;
652 LAGG_XLOCK_ASSERT(sc);
654 /* Get common enabled capabilities for the lagg ports */
656 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
657 ena &= lp->lp_ifp->if_capenable;
658 ena = (ena == ~0 ? 0 : ena);
661 * Apply common enabled capabilities back to the lagg ports.
662 * May require several iterations if they are dependent.
666 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
667 lagg_setcaps(lp, ena);
668 ena &= lp->lp_ifp->if_capenable;
670 } while (pena != ena);
672 /* Get other capabilities from the lagg ports */
675 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
676 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
677 cap &= lp->lp_ifp->if_capabilities;
678 hwa &= lp->lp_ifp->if_hwassist;
679 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
681 cap = (cap == ~0 ? 0 : cap);
682 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
684 if (sc->sc_ifp->if_capabilities != cap ||
685 sc->sc_ifp->if_capenable != ena ||
686 sc->sc_ifp->if_hwassist != hwa ||
687 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
688 sc->sc_ifp->if_capabilities = cap;
689 sc->sc_ifp->if_capenable = ena;
690 sc->sc_ifp->if_hwassist = hwa;
691 getmicrotime(&sc->sc_ifp->if_lastchange);
693 if (sc->sc_ifflags & IFF_DEBUG)
694 if_printf(sc->sc_ifp,
695 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
700 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
702 struct lagg_softc *sc_ptr;
703 struct lagg_port *lp, *tlp;
705 int error, i, oldmtu;
709 LAGG_XLOCK_ASSERT(sc);
711 if (sc->sc_ifp == ifp) {
712 if_printf(sc->sc_ifp,
713 "cannot add a lagg to itself as a port\n");
717 /* Limit the maximal number of lagg ports */
718 if (sc->sc_count >= LAGG_MAX_PORTS)
721 /* Check if port has already been associated to a lagg */
722 if (ifp->if_lagg != NULL) {
723 /* Port is already in the current lagg? */
724 lp = (struct lagg_port *)ifp->if_lagg;
725 if (lp->lp_softc == sc)
730 switch (sc->sc_ifp->if_type) {
732 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
733 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
734 return (EPROTONOSUPPORT);
735 if_type = IFT_IEEE8023ADLAG;
738 /* XXX Disallow non-infiniband interfaces */
739 if (ifp->if_type != IFT_INFINIBAND)
740 return (EPROTONOSUPPORT);
741 if_type = IFT_INFINIBANDLAG;
747 /* Allow the first Ethernet member to define the MTU */
749 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
750 sc->sc_ifp->if_mtu = ifp->if_mtu;
751 } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
752 if (ifp->if_ioctl == NULL) {
753 if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
757 oldmtu = ifp->if_mtu;
758 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
759 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
760 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
762 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
766 ifr.ifr_mtu = oldmtu;
769 lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
772 /* Check if port is a stacked lagg */
774 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
775 if (ifp == sc_ptr->sc_ifp) {
779 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
782 /* XXX disable stacking for the moment, its untested */
783 #ifdef LAGG_PORT_STACKING
784 lp->lp_flags |= LAGG_PORT_STACK;
785 if (lagg_port_checkstacking(sc_ptr) >=
790 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
802 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ifp->if_addrlen);
803 lp->lp_ifcapenable = ifp->if_capenable;
804 if (CK_SLIST_EMPTY(&sc->sc_ports)) {
805 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
806 lagg_proto_lladdr(sc);
807 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
809 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
811 lagg_setflags(lp, 1);
813 if (CK_SLIST_EMPTY(&sc->sc_ports))
816 /* Change the interface type */
817 lp->lp_iftype = ifp->if_type;
818 ifp->if_type = if_type;
820 lp->lp_ioctl = ifp->if_ioctl;
821 ifp->if_ioctl = lagg_port_ioctl;
822 lp->lp_output = ifp->if_output;
823 ifp->if_output = lagg_port_output;
825 /* Read port counters */
826 pval = lp->port_counters.val;
827 for (i = 0; i < IFCOUNTERS; i++, pval++)
828 *pval = ifp->if_get_counter(ifp, i);
831 * Insert into the list of ports.
832 * Keep ports sorted by if_index. It is handy, when configuration
833 * is predictable and `ifconfig laggN create ...` command
834 * will lead to the same result each time.
836 CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
837 if (tlp->lp_ifp->if_index < ifp->if_index && (
838 CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
839 ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
844 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
846 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
852 if ((error = lagg_proto_addport(sc, lp)) != 0) {
853 /* Remove the port, without calling pr_delport. */
854 lagg_port_destroy(lp, 0);
856 (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
860 /* Update lagg capabilities */
861 lagg_capabilities(sc);
867 #ifdef LAGG_PORT_STACKING
869 lagg_port_checkstacking(struct lagg_softc *sc)
871 struct lagg_softc *sc_ptr;
872 struct lagg_port *lp;
875 LAGG_SXLOCK_ASSERT(sc);
876 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
877 if (lp->lp_flags & LAGG_PORT_STACK) {
878 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
879 m = MAX(m, lagg_port_checkstacking(sc_ptr));
888 lagg_port_destroy_cb(epoch_context_t ec)
890 struct lagg_port *lp;
893 lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
901 lagg_port_destroy(struct lagg_port *lp, int rundelport)
903 struct lagg_softc *sc = lp->lp_softc;
904 struct lagg_port *lp_ptr, *lp0;
905 struct ifnet *ifp = lp->lp_ifp;
906 uint64_t *pval, vdiff;
909 LAGG_XLOCK_ASSERT(sc);
912 lagg_proto_delport(sc, lp);
914 if (lp->lp_detaching == 0)
917 /* Restore interface */
918 ifp->if_type = lp->lp_iftype;
919 ifp->if_ioctl = lp->lp_ioctl;
920 ifp->if_output = lp->lp_output;
923 /* Update detached port counters */
924 pval = lp->port_counters.val;
925 for (i = 0; i < IFCOUNTERS; i++, pval++) {
926 vdiff = ifp->if_get_counter(ifp, i) - *pval;
927 sc->detached_counters.val[i] += vdiff;
930 /* Finally, remove the port from the lagg */
931 CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
934 /* Update the primary interface */
935 if (lp == sc->sc_primary) {
936 uint8_t lladdr[LAGG_ADDR_LEN];
938 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
939 bzero(&lladdr, LAGG_ADDR_LEN);
941 bcopy(lp0->lp_lladdr, lladdr, LAGG_ADDR_LEN);
942 sc->sc_primary = lp0;
943 if (sc->sc_destroying == 0) {
944 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), sc->sc_ifp->if_addrlen);
945 lagg_proto_lladdr(sc);
946 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
949 * Update lladdr for each port (new primary needs update
950 * as well, to switch from old lladdr to its 'real' one).
951 * We can skip this if the lagg is being destroyed.
953 CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
954 if_setlladdr(lp_ptr->lp_ifp, lladdr,
955 lp_ptr->lp_ifp->if_addrlen);
960 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
962 if (lp->lp_detaching == 0) {
963 lagg_setflags(lp, 0);
964 lagg_setcaps(lp, lp->lp_ifcapenable);
965 if_setlladdr(ifp, lp->lp_lladdr, ifp->if_addrlen);
969 * free port and release it's ifnet reference after a grace period has
972 epoch_call(net_epoch_preempt, &lp->lp_epoch_ctx, lagg_port_destroy_cb);
973 /* Update lagg capabilities */
974 lagg_capabilities(sc);
981 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
983 struct lagg_reqport *rp = (struct lagg_reqport *)data;
984 struct lagg_softc *sc;
985 struct lagg_port *lp = NULL;
988 /* Should be checked by the caller */
989 switch (ifp->if_type) {
990 case IFT_IEEE8023ADLAG:
991 case IFT_INFINIBANDLAG:
992 if ((lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
1001 if (rp->rp_portname[0] == '\0' ||
1002 ifunit(rp->rp_portname) != ifp) {
1008 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
1014 lagg_port2req(lp, rp);
1019 if (lp->lp_ioctl == NULL) {
1023 error = (*lp->lp_ioctl)(ifp, cmd, data);
1027 /* Update lagg interface capabilities */
1029 lagg_capabilities(sc);
1031 VLAN_CAPABILITIES(sc->sc_ifp);
1035 /* Do not allow the MTU to be changed once joined */
1046 if (lp != NULL && lp->lp_ioctl != NULL)
1047 return ((*lp->lp_ioctl)(ifp, cmd, data));
1053 * Requests counter @cnt data.
1055 * Counter value is calculated the following way:
1056 * 1) for each port, sum difference between current and "initial" measurements.
1057 * 2) add lagg logical interface counters.
1058 * 3) add data from detached_counters array.
1060 * We also do the following things on ports attach/detach:
1061 * 1) On port attach we store all counters it has into port_counter array.
1062 * 2) On port detach we add the different between "initial" and
1063 * current counters data to detached_counters array.
1066 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1068 struct lagg_softc *sc;
1069 struct lagg_port *lp;
1070 struct ifnet *lpifp;
1071 uint64_t newval, oldval, vsum;
1073 /* Revise this when we've got non-generic counters. */
1074 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1076 sc = (struct lagg_softc *)ifp->if_softc;
1080 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1081 /* Saved attached value */
1082 oldval = lp->port_counters.val[cnt];
1085 newval = lpifp->if_get_counter(lpifp, cnt);
1086 /* Calculate diff and save new */
1087 vsum += newval - oldval;
1092 * Add counter data which might be added by upper
1093 * layer protocols operating on logical interface.
1095 vsum += if_get_counter_default(ifp, cnt);
1098 * Add counter data from detached ports counters
1100 vsum += sc->detached_counters.val[cnt];
1107 * For direct output to child ports.
1110 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1111 const struct sockaddr *dst, struct route *ro)
1113 struct lagg_port *lp = ifp->if_lagg;
1115 switch (dst->sa_family) {
1116 case pseudo_AF_HDRCMPLT:
1119 return ((*lp->lp_output)(ifp, m, dst, ro));
1122 /* drop any other frames */
1128 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1130 struct lagg_port *lp;
1131 struct lagg_softc *sc;
1133 if ((lp = ifp->if_lagg) == NULL)
1135 /* If the ifnet is just being renamed, don't do anything. */
1136 if (ifp->if_flags & IFF_RENAMING)
1142 lp->lp_detaching = 1;
1143 lagg_port_destroy(lp, 1);
1145 VLAN_CAPABILITIES(sc->sc_ifp);
1149 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1151 struct lagg_softc *sc = lp->lp_softc;
1153 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1154 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1155 rp->rp_prio = lp->lp_prio;
1156 rp->rp_flags = lp->lp_flags;
1157 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1159 /* Add protocol specific flags */
1160 switch (sc->sc_proto) {
1161 case LAGG_PROTO_FAILOVER:
1162 if (lp == sc->sc_primary)
1163 rp->rp_flags |= LAGG_PORT_MASTER;
1164 if (lp == lagg_link_active(sc, sc->sc_primary))
1165 rp->rp_flags |= LAGG_PORT_ACTIVE;
1168 case LAGG_PROTO_ROUNDROBIN:
1169 case LAGG_PROTO_LOADBALANCE:
1170 case LAGG_PROTO_BROADCAST:
1171 if (LAGG_PORTACTIVE(lp))
1172 rp->rp_flags |= LAGG_PORT_ACTIVE;
1175 case LAGG_PROTO_LACP:
1176 /* LACP has a different definition of active */
1177 if (lacp_isactive(lp))
1178 rp->rp_flags |= LAGG_PORT_ACTIVE;
1179 if (lacp_iscollecting(lp))
1180 rp->rp_flags |= LAGG_PORT_COLLECTING;
1181 if (lacp_isdistributing(lp))
1182 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1189 lagg_watchdog_infiniband(void *arg)
1191 struct lagg_softc *sc;
1192 struct lagg_port *lp;
1194 struct ifnet *lp_ifp;
1199 * Because infiniband nodes have a fixed MAC address, which is
1200 * generated by the so-called GID, we need to regularly update
1201 * the link level address of the parent lagg<N> device when
1202 * the active port changes. Possibly we could piggy-back on
1203 * link up/down events aswell, but using a timer also provides
1204 * a guarantee against too frequent events. This operation
1205 * does not have to be atomic.
1208 lp = lagg_link_active(sc, sc->sc_primary);
1211 lp_ifp = lp->lp_ifp;
1213 if (ifp != NULL && lp_ifp != NULL &&
1214 (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen) != 0 ||
1215 memcmp(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen) != 0)) {
1216 memcpy(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen);
1217 memcpy(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen);
1219 CURVNET_SET(ifp->if_vnet);
1220 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1226 callout_reset(&sc->sc_watchdog, hz, &lagg_watchdog_infiniband, arg);
1230 lagg_init(void *xsc)
1232 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1233 struct ifnet *ifp = sc->sc_ifp;
1234 struct lagg_port *lp;
1237 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1242 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1245 * Update the port lladdrs if needed.
1246 * This might be if_setlladdr() notification
1247 * that lladdr has been changed.
1249 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1250 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1251 ifp->if_addrlen) != 0)
1252 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1255 lagg_proto_init(sc);
1257 if (ifp->if_type == IFT_INFINIBAND) {
1258 mtx_lock(&sc->sc_mtx);
1259 lagg_watchdog_infiniband(sc);
1260 mtx_unlock(&sc->sc_mtx);
1267 lagg_stop(struct lagg_softc *sc)
1269 struct ifnet *ifp = sc->sc_ifp;
1271 LAGG_XLOCK_ASSERT(sc);
1273 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1276 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1278 lagg_proto_stop(sc);
1280 mtx_lock(&sc->sc_mtx);
1281 callout_stop(&sc->sc_watchdog);
1282 mtx_unlock(&sc->sc_mtx);
1284 callout_drain(&sc->sc_watchdog);
1288 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1290 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1291 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1292 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1293 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1294 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1295 struct ifreq *ifr = (struct ifreq *)data;
1296 struct lagg_port *lp;
1298 struct thread *td = curthread;
1300 int count, buflen, len, error = 0, oldmtu;
1302 bzero(&rpbuf, sizeof(rpbuf));
1307 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1308 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1309 ra->ra_proto = sc->sc_proto;
1310 lagg_proto_request(sc, &ra->ra_psc);
1313 len = min(ra->ra_size, buflen);
1314 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1315 if (len < sizeof(rpbuf))
1318 lagg_port2req(lp, &rpbuf);
1319 memcpy(buf, &rpbuf, sizeof(rpbuf));
1321 buf += sizeof(rpbuf);
1322 len -= sizeof(rpbuf);
1325 ra->ra_ports = count;
1326 ra->ra_size = count * sizeof(rpbuf);
1327 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1328 free(outbuf, M_TEMP);
1331 error = priv_check(td, PRIV_NET_LAGG);
1334 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1335 error = EPROTONOSUPPORT;
1338 /* Infiniband only supports the failover protocol. */
1339 if (ra->ra_proto != LAGG_PROTO_FAILOVER &&
1340 ifp->if_type == IFT_INFINIBAND) {
1341 error = EPROTONOSUPPORT;
1345 lagg_proto_detach(sc);
1346 LAGG_UNLOCK_ASSERT();
1347 lagg_proto_attach(sc, ra->ra_proto);
1352 ro->ro_opts = sc->sc_opts;
1353 if (sc->sc_proto == LAGG_PROTO_LACP) {
1354 struct lacp_softc *lsc;
1356 lsc = (struct lacp_softc *)sc->sc_psc;
1357 if (lsc->lsc_debug.lsc_tx_test != 0)
1358 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1359 if (lsc->lsc_debug.lsc_rx_test != 0)
1360 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1361 if (lsc->lsc_strict_mode != 0)
1362 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1363 if (lsc->lsc_fast_timeout != 0)
1364 ro->ro_opts |= LAGG_OPT_LACP_FAST_TIMO;
1366 ro->ro_active = sc->sc_active;
1369 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1370 ro->ro_active += LAGG_PORTACTIVE(lp);
1372 ro->ro_bkt = sc->sc_stride;
1373 ro->ro_flapping = sc->sc_flapping;
1374 ro->ro_flowid_shift = sc->flowid_shift;
1378 error = priv_check(td, PRIV_NET_LAGG);
1383 * The stride option was added without defining a corresponding
1384 * LAGG_OPT flag, so handle a non-zero value before checking
1385 * anything else to preserve compatibility.
1388 if (ro->ro_opts == 0 && ro->ro_bkt != 0) {
1389 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN) {
1394 sc->sc_stride = ro->ro_bkt;
1396 if (ro->ro_opts == 0) {
1402 * Set options. LACP options are stored in sc->sc_psc,
1407 switch (ro->ro_opts) {
1408 case LAGG_OPT_USE_FLOWID:
1409 case -LAGG_OPT_USE_FLOWID:
1410 case LAGG_OPT_FLOWIDSHIFT:
1411 case LAGG_OPT_RR_LIMIT:
1415 case LAGG_OPT_LACP_TXTEST:
1416 case -LAGG_OPT_LACP_TXTEST:
1417 case LAGG_OPT_LACP_RXTEST:
1418 case -LAGG_OPT_LACP_RXTEST:
1419 case LAGG_OPT_LACP_STRICT:
1420 case -LAGG_OPT_LACP_STRICT:
1421 case LAGG_OPT_LACP_FAST_TIMO:
1422 case -LAGG_OPT_LACP_FAST_TIMO:
1431 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1432 /* Invalid combination of options specified. */
1435 break; /* Return from SIOCSLAGGOPTS. */
1439 * Store new options into sc->sc_opts except for
1440 * FLOWIDSHIFT, RR and LACP options.
1443 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1444 sc->flowid_shift = ro->ro_flowid_shift;
1445 else if (ro->ro_opts == LAGG_OPT_RR_LIMIT) {
1446 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN ||
1452 sc->sc_stride = ro->ro_bkt;
1453 } else if (ro->ro_opts > 0)
1454 sc->sc_opts |= ro->ro_opts;
1456 sc->sc_opts &= ~ro->ro_opts;
1458 struct lacp_softc *lsc;
1459 struct lacp_port *lp;
1461 lsc = (struct lacp_softc *)sc->sc_psc;
1463 switch (ro->ro_opts) {
1464 case LAGG_OPT_LACP_TXTEST:
1465 lsc->lsc_debug.lsc_tx_test = 1;
1467 case -LAGG_OPT_LACP_TXTEST:
1468 lsc->lsc_debug.lsc_tx_test = 0;
1470 case LAGG_OPT_LACP_RXTEST:
1471 lsc->lsc_debug.lsc_rx_test = 1;
1473 case -LAGG_OPT_LACP_RXTEST:
1474 lsc->lsc_debug.lsc_rx_test = 0;
1476 case LAGG_OPT_LACP_STRICT:
1477 lsc->lsc_strict_mode = 1;
1479 case -LAGG_OPT_LACP_STRICT:
1480 lsc->lsc_strict_mode = 0;
1482 case LAGG_OPT_LACP_FAST_TIMO:
1484 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1485 lp->lp_state |= LACP_STATE_TIMEOUT;
1487 lsc->lsc_fast_timeout = 1;
1489 case -LAGG_OPT_LACP_FAST_TIMO:
1491 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1492 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1494 lsc->lsc_fast_timeout = 0;
1500 case SIOCGLAGGFLAGS:
1503 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1504 rf->rf_flags |= LAGG_F_HASHL2;
1505 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1506 rf->rf_flags |= LAGG_F_HASHL3;
1507 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1508 rf->rf_flags |= LAGG_F_HASHL4;
1512 error = priv_check(td, PRIV_NET_LAGG);
1515 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1521 if (rf->rf_flags & LAGG_F_HASHL2)
1522 sc->sc_flags |= MBUF_HASHFLAG_L2;
1523 if (rf->rf_flags & LAGG_F_HASHL3)
1524 sc->sc_flags |= MBUF_HASHFLAG_L3;
1525 if (rf->rf_flags & LAGG_F_HASHL4)
1526 sc->sc_flags |= MBUF_HASHFLAG_L4;
1530 if (rp->rp_portname[0] == '\0' ||
1531 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1537 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1538 lp->lp_softc != sc) {
1545 lagg_port2req(lp, rp);
1550 error = priv_check(td, PRIV_NET_LAGG);
1553 if (rp->rp_portname[0] == '\0' ||
1554 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1560 * A laggport interface should not have inet6 address
1561 * because two interfaces with a valid link-local
1562 * scope zone must not be merged in any form. This
1563 * restriction is needed to prevent violation of
1564 * link-local scope zone. Attempts to add a laggport
1565 * interface which has inet6 addresses triggers
1566 * removal of all inet6 addresses on the member
1569 if (in6ifa_llaonifp(tpif)) {
1571 if_printf(sc->sc_ifp,
1572 "IPv6 addresses on %s have been removed "
1573 "before adding it as a member to prevent "
1574 "IPv6 address scope violation.\n",
1578 oldmtu = ifp->if_mtu;
1580 error = lagg_port_create(sc, tpif);
1585 * LAGG MTU may change during addition of the first port.
1586 * If it did, do network layer specific procedure.
1588 if (ifp->if_mtu != oldmtu) {
1595 VLAN_CAPABILITIES(ifp);
1597 case SIOCSLAGGDELPORT:
1598 error = priv_check(td, PRIV_NET_LAGG);
1601 if (rp->rp_portname[0] == '\0' ||
1602 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1608 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1609 lp->lp_softc != sc) {
1616 error = lagg_port_destroy(lp, 1);
1619 VLAN_CAPABILITIES(ifp);
1622 /* Set flags on ports too */
1624 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1625 lagg_setflags(lp, 1);
1628 if (!(ifp->if_flags & IFF_UP) &&
1629 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1631 * If interface is marked down and it is running,
1632 * then stop and disable it.
1636 } else if ((ifp->if_flags & IFF_UP) &&
1637 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1639 * If interface is marked up and it is stopped, then
1643 (*ifp->if_init)(sc);
1650 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1659 if (ifp->if_type == IFT_INFINIBAND)
1662 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1667 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1668 if (lp->lp_ioctl != NULL)
1669 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1671 lagg_capabilities(sc);
1673 VLAN_CAPABILITIES(ifp);
1679 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1680 if (lp->lp_ioctl != NULL)
1681 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1686 "failed to change MTU to %d on port %s, "
1687 "reverting all ports to original MTU (%d)\n",
1688 ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1693 ifp->if_mtu = ifr->ifr_mtu;
1695 /* set every port back to the original MTU */
1696 ifr->ifr_mtu = ifp->if_mtu;
1697 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1698 if (lp->lp_ioctl != NULL)
1699 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1706 error = ether_ioctl(ifp, cmd, data);
1714 lagg_snd_tag_alloc(struct ifnet *ifp,
1715 union if_snd_tag_alloc_params *params,
1716 struct m_snd_tag **ppmt)
1718 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1719 struct lagg_port *lp;
1724 switch (sc->sc_proto) {
1725 case LAGG_PROTO_FAILOVER:
1726 lp = lagg_link_active(sc, sc->sc_primary);
1728 case LAGG_PROTO_LOADBALANCE:
1729 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1730 params->hdr.flowtype == M_HASHTYPE_NONE) {
1732 return (EOPNOTSUPP);
1734 p = params->hdr.flowid >> sc->flowid_shift;
1736 lb = (struct lagg_lb *)sc->sc_psc;
1737 lp = lb->lb_ports[p];
1738 lp = lagg_link_active(sc, lp);
1740 case LAGG_PROTO_LACP:
1741 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1742 params->hdr.flowtype == M_HASHTYPE_NONE) {
1744 return (EOPNOTSUPP);
1746 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1750 return (EOPNOTSUPP);
1754 return (EOPNOTSUPP);
1758 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1759 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1760 return (EOPNOTSUPP);
1762 /* forward allocation request */
1763 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1768 lagg_setmulti(struct lagg_port *lp)
1770 struct lagg_softc *sc = lp->lp_softc;
1771 struct ifnet *ifp = lp->lp_ifp;
1772 struct ifnet *scifp = sc->sc_ifp;
1774 struct ifmultiaddr *ifma;
1777 IF_ADDR_WLOCK(scifp);
1778 CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1779 if (ifma->ifma_addr->sa_family != AF_LINK)
1781 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1783 IF_ADDR_WUNLOCK(scifp);
1786 bcopy(ifma->ifma_addr, &mc->mc_addr,
1787 ifma->ifma_addr->sa_len);
1788 mc->mc_addr.sdl_index = ifp->if_index;
1790 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1792 IF_ADDR_WUNLOCK(scifp);
1793 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1794 error = if_addmulti(ifp,
1795 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1803 lagg_clrmulti(struct lagg_port *lp)
1807 LAGG_XLOCK_ASSERT(lp->lp_softc);
1808 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1809 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1810 if (mc->mc_ifma && lp->lp_detaching == 0)
1811 if_delmulti_ifma(mc->mc_ifma);
1818 lagg_setcaps(struct lagg_port *lp, int cap)
1822 if (lp->lp_ifp->if_capenable == cap)
1824 if (lp->lp_ioctl == NULL)
1826 ifr.ifr_reqcap = cap;
1827 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1830 /* Handle a ref counted flag that should be set on the lagg port as well */
1832 lagg_setflag(struct lagg_port *lp, int flag, int status,
1833 int (*func)(struct ifnet *, int))
1835 struct lagg_softc *sc = lp->lp_softc;
1836 struct ifnet *scifp = sc->sc_ifp;
1837 struct ifnet *ifp = lp->lp_ifp;
1840 LAGG_XLOCK_ASSERT(sc);
1842 status = status ? (scifp->if_flags & flag) : 0;
1843 /* Now "status" contains the flag value or 0 */
1846 * See if recorded ports status is different from what
1847 * we want it to be. If it is, flip it. We record ports
1848 * status in lp_ifflags so that we won't clear ports flag
1849 * we haven't set. In fact, we don't clear or set ports
1850 * flags directly, but get or release references to them.
1851 * That's why we can be sure that recorded flags still are
1852 * in accord with actual ports flags.
1854 if (status != (lp->lp_ifflags & flag)) {
1855 error = (*func)(ifp, status);
1858 lp->lp_ifflags &= ~flag;
1859 lp->lp_ifflags |= status;
1865 * Handle IFF_* flags that require certain changes on the lagg port
1866 * if "status" is true, update ports flags respective to the lagg
1867 * if "status" is false, forcedly clear the flags set on port.
1870 lagg_setflags(struct lagg_port *lp, int status)
1874 for (i = 0; lagg_pflags[i].flag; i++) {
1875 error = lagg_setflag(lp, lagg_pflags[i].flag,
1876 status, lagg_pflags[i].func);
1884 lagg_transmit_ethernet(struct ifnet *ifp, struct mbuf *m)
1886 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1890 /* We need a Tx algorithm and at least one port */
1891 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1894 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1898 ETHER_BPF_MTAP(ifp, m);
1900 error = lagg_proto_start(sc, m);
1904 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1910 lagg_transmit_infiniband(struct ifnet *ifp, struct mbuf *m)
1912 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1915 #if defined(KERN_TLS) || defined(RATELIMIT)
1916 if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
1917 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1920 /* We need a Tx algorithm and at least one port */
1921 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1924 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1928 INFINIBAND_BPF_MTAP(ifp, m);
1930 error = lagg_proto_start(sc, m);
1936 * The ifp->if_qflush entry point for lagg(4) is no-op.
1939 lagg_qflush(struct ifnet *ifp __unused)
1943 static struct mbuf *
1944 lagg_input_ethernet(struct ifnet *ifp, struct mbuf *m)
1946 struct lagg_port *lp = ifp->if_lagg;
1947 struct lagg_softc *sc = lp->lp_softc;
1948 struct ifnet *scifp = sc->sc_ifp;
1951 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1952 lp->lp_detaching != 0 ||
1953 sc->sc_proto == LAGG_PROTO_NONE) {
1959 ETHER_BPF_MTAP(scifp, m);
1961 m = lagg_proto_input(sc, lp, m);
1962 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
1971 static struct mbuf *
1972 lagg_input_infiniband(struct ifnet *ifp, struct mbuf *m)
1974 struct lagg_port *lp = ifp->if_lagg;
1975 struct lagg_softc *sc = lp->lp_softc;
1976 struct ifnet *scifp = sc->sc_ifp;
1979 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1980 lp->lp_detaching != 0 ||
1981 sc->sc_proto == LAGG_PROTO_NONE) {
1987 INFINIBAND_BPF_MTAP(scifp, m);
1989 m = lagg_proto_input(sc, lp, m);
1990 if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
2000 lagg_media_change(struct ifnet *ifp)
2002 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2004 if (sc->sc_ifflags & IFF_DEBUG)
2005 printf("%s\n", __func__);
2012 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2014 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2015 struct lagg_port *lp;
2017 imr->ifm_status = IFM_AVALID;
2018 imr->ifm_active = IFM_ETHER | IFM_AUTO;
2021 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2022 if (LAGG_PORTACTIVE(lp))
2023 imr->ifm_status |= IFM_ACTIVE;
2029 lagg_linkstate(struct lagg_softc *sc)
2031 struct lagg_port *lp;
2032 int new_link = LINK_STATE_DOWN;
2035 LAGG_XLOCK_ASSERT(sc);
2037 /* LACP handles link state itself */
2038 if (sc->sc_proto == LAGG_PROTO_LACP)
2041 /* Our link is considered up if at least one of our ports is active */
2043 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2044 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
2045 new_link = LINK_STATE_UP;
2050 if_link_state_change(sc->sc_ifp, new_link);
2052 /* Update if_baudrate to reflect the max possible speed */
2053 switch (sc->sc_proto) {
2054 case LAGG_PROTO_FAILOVER:
2055 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
2056 sc->sc_primary->lp_ifp->if_baudrate : 0;
2058 case LAGG_PROTO_ROUNDROBIN:
2059 case LAGG_PROTO_LOADBALANCE:
2060 case LAGG_PROTO_BROADCAST:
2063 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2064 speed += lp->lp_ifp->if_baudrate;
2066 sc->sc_ifp->if_baudrate = speed;
2068 case LAGG_PROTO_LACP:
2069 /* LACP updates if_baudrate itself */
2075 lagg_port_state(struct ifnet *ifp, int state)
2077 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
2078 struct lagg_softc *sc = NULL;
2087 lagg_proto_linkstate(sc, lp);
2092 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
2094 struct lagg_port *lp_next, *rval = NULL;
2097 * Search a port which reports an active link state.
2102 * This is called with either LAGG_RLOCK() held or
2103 * LAGG_XLOCK(sc) held.
2105 if (!in_epoch(net_epoch_preempt))
2106 LAGG_XLOCK_ASSERT(sc);
2111 if (LAGG_PORTACTIVE(lp)) {
2115 if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
2116 LAGG_PORTACTIVE(lp_next)) {
2122 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2123 if (LAGG_PORTACTIVE(lp_next)) {
2132 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2135 return (ifp->if_transmit)(ifp, m);
2139 * Simple round robin aggregation
2142 lagg_rr_attach(struct lagg_softc *sc)
2149 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2151 struct lagg_port *lp;
2154 p = atomic_fetchadd_32(&sc->sc_seq, 1);
2157 lp = CK_SLIST_FIRST(&sc->sc_ports);
2160 lp = CK_SLIST_NEXT(lp, lp_entries);
2163 * Check the port's link state. This will return the next active
2164 * port if the link is down or the port is NULL.
2166 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2172 return (lagg_enqueue(lp->lp_ifp, m));
2175 static struct mbuf *
2176 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2178 struct ifnet *ifp = sc->sc_ifp;
2180 /* Just pass in the packet to our lagg device */
2181 m->m_pkthdr.rcvif = ifp;
2190 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2192 int active_ports = 0;
2195 struct lagg_port *lp, *last = NULL;
2198 LAGG_RLOCK_ASSERT();
2199 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2200 if (!LAGG_PORTACTIVE(lp))
2206 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2213 ret = lagg_enqueue(last->lp_ifp, m0);
2224 if ((last = lagg_link_active(sc, last)) == NULL) {
2229 ret = lagg_enqueue(last->lp_ifp, m);
2240 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2242 struct ifnet *ifp = sc->sc_ifp;
2244 /* Just pass in the packet to our lagg device */
2245 m->m_pkthdr.rcvif = ifp;
2253 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2255 struct lagg_port *lp;
2257 /* Use the master port if active or the next available port */
2258 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2264 return (lagg_enqueue(lp->lp_ifp, m));
2267 static struct mbuf *
2268 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2270 struct ifnet *ifp = sc->sc_ifp;
2271 struct lagg_port *tmp_tp;
2273 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2274 m->m_pkthdr.rcvif = ifp;
2278 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2279 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2281 * If tmp_tp is null, we've received a packet when all
2282 * our links are down. Weird, but process it anyways.
2284 if ((tmp_tp == NULL || tmp_tp == lp)) {
2285 m->m_pkthdr.rcvif = ifp;
2298 lagg_lb_attach(struct lagg_softc *sc)
2300 struct lagg_port *lp;
2303 LAGG_XLOCK_ASSERT(sc);
2304 lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2305 lb->lb_key = m_ether_tcpip_hash_init();
2308 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2309 lagg_lb_port_create(lp);
2313 lagg_lb_detach(struct lagg_softc *sc)
2317 lb = (struct lagg_lb *)sc->sc_psc;
2323 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2325 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2326 struct lagg_port *lp_next;
2330 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2331 LAGG_XLOCK_ASSERT(sc);
2332 CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2335 if (i >= LAGG_MAX_PORTS) {
2339 if (sc->sc_ifflags & IFF_DEBUG)
2340 printf("%s: port %s at index %d\n",
2341 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2342 lb->lb_ports[i++] = lp_next;
2349 lagg_lb_port_create(struct lagg_port *lp)
2351 struct lagg_softc *sc = lp->lp_softc;
2352 return (lagg_lb_porttable(sc, NULL));
2356 lagg_lb_port_destroy(struct lagg_port *lp)
2358 struct lagg_softc *sc = lp->lp_softc;
2359 lagg_lb_porttable(sc, lp);
2363 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2365 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2366 struct lagg_port *lp = NULL;
2369 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2370 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2371 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2373 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2375 lp = lb->lb_ports[p];
2378 * Check the port's link state. This will return the next active
2379 * port if the link is down or the port is NULL.
2381 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2387 return (lagg_enqueue(lp->lp_ifp, m));
2390 static struct mbuf *
2391 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2393 struct ifnet *ifp = sc->sc_ifp;
2395 /* Just pass in the packet to our lagg device */
2396 m->m_pkthdr.rcvif = ifp;
2405 lagg_lacp_attach(struct lagg_softc *sc)
2407 struct lagg_port *lp;
2410 LAGG_XLOCK_ASSERT(sc);
2411 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2412 lacp_port_create(lp);
2416 lagg_lacp_detach(struct lagg_softc *sc)
2418 struct lagg_port *lp;
2421 LAGG_XLOCK_ASSERT(sc);
2422 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2423 lacp_port_destroy(lp);
2431 lagg_lacp_lladdr(struct lagg_softc *sc)
2433 struct lagg_port *lp;
2435 LAGG_SXLOCK_ASSERT(sc);
2437 /* purge all the lacp ports */
2438 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2439 lacp_port_destroy(lp);
2441 /* add them back in */
2442 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2443 lacp_port_create(lp);
2447 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2449 struct lagg_port *lp;
2451 lp = lacp_select_tx_port(sc, m);
2458 return (lagg_enqueue(lp->lp_ifp, m));
2461 static struct mbuf *
2462 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2464 struct ifnet *ifp = sc->sc_ifp;
2465 struct ether_header *eh;
2468 eh = mtod(m, struct ether_header *);
2469 etype = ntohs(eh->ether_type);
2471 /* Tap off LACP control messages */
2472 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2473 m = lacp_input(lp, m);
2479 * If the port is not collecting or not in the active aggregator then
2482 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2487 m->m_pkthdr.rcvif = ifp;