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 /* Special flags we should propagate to the lagg ports. */
79 int (*func)(struct ifnet *, int);
81 {IFF_PROMISC, ifpromisc},
82 {IFF_ALLMULTI, if_allmulti},
86 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
87 #define V_lagg_list VNET(lagg_list)
88 static VNET_DEFINE(struct mtx, lagg_list_mtx);
89 #define V_lagg_list_mtx VNET(lagg_list_mtx)
90 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
91 "if_lagg list", NULL, MTX_DEF)
92 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
93 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
94 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
95 eventhandler_tag lagg_detach_cookie = NULL;
97 static int lagg_clone_create(struct if_clone *, int, caddr_t);
98 static void lagg_clone_destroy(struct ifnet *);
99 static VNET_DEFINE(struct if_clone *, lagg_cloner);
100 #define V_lagg_cloner VNET(lagg_cloner)
101 static const char laggname[] = "lagg";
103 static void lagg_capabilities(struct lagg_softc *);
104 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
105 static int lagg_port_destroy(struct lagg_port *, int);
106 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
107 static void lagg_linkstate(struct lagg_softc *);
108 static void lagg_port_state(struct ifnet *, int);
109 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
110 static int lagg_port_output(struct ifnet *, struct mbuf *,
111 const struct sockaddr *, struct route *);
112 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
113 #ifdef LAGG_PORT_STACKING
114 static int lagg_port_checkstacking(struct lagg_softc *);
116 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
117 static void lagg_init(void *);
118 static void lagg_stop(struct lagg_softc *);
119 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
121 static int lagg_snd_tag_alloc(struct ifnet *,
122 union if_snd_tag_alloc_params *,
123 struct m_snd_tag **);
125 static int lagg_setmulti(struct lagg_port *);
126 static int lagg_clrmulti(struct lagg_port *);
127 static int lagg_setcaps(struct lagg_port *, int cap);
128 static int lagg_setflag(struct lagg_port *, int, int,
129 int (*func)(struct ifnet *, int));
130 static int lagg_setflags(struct lagg_port *, int status);
131 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
132 static int lagg_transmit(struct ifnet *, struct mbuf *);
133 static void lagg_qflush(struct ifnet *);
134 static int lagg_media_change(struct ifnet *);
135 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
136 static struct lagg_port *lagg_link_active(struct lagg_softc *,
139 /* Simple round robin */
140 static void lagg_rr_attach(struct lagg_softc *);
141 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
142 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
145 /* Active failover */
146 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
147 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
151 static void lagg_lb_attach(struct lagg_softc *);
152 static void lagg_lb_detach(struct lagg_softc *);
153 static int lagg_lb_port_create(struct lagg_port *);
154 static void lagg_lb_port_destroy(struct lagg_port *);
155 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
156 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
158 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
161 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
162 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
166 static void lagg_lacp_attach(struct lagg_softc *);
167 static void lagg_lacp_detach(struct lagg_softc *);
168 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
169 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
171 static void lagg_lacp_lladdr(struct lagg_softc *);
173 /* lagg protocol table */
174 static const struct lagg_proto {
176 void (*pr_attach)(struct lagg_softc *);
177 void (*pr_detach)(struct lagg_softc *);
178 int (*pr_start)(struct lagg_softc *, struct mbuf *);
179 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
181 int (*pr_addport)(struct lagg_port *);
182 void (*pr_delport)(struct lagg_port *);
183 void (*pr_linkstate)(struct lagg_port *);
184 void (*pr_init)(struct lagg_softc *);
185 void (*pr_stop)(struct lagg_softc *);
186 void (*pr_lladdr)(struct lagg_softc *);
187 void (*pr_request)(struct lagg_softc *, void *);
188 void (*pr_portreq)(struct lagg_port *, void *);
191 .pr_num = LAGG_PROTO_NONE
194 .pr_num = LAGG_PROTO_ROUNDROBIN,
195 .pr_attach = lagg_rr_attach,
196 .pr_start = lagg_rr_start,
197 .pr_input = lagg_rr_input,
200 .pr_num = LAGG_PROTO_FAILOVER,
201 .pr_start = lagg_fail_start,
202 .pr_input = lagg_fail_input,
205 .pr_num = LAGG_PROTO_LOADBALANCE,
206 .pr_attach = lagg_lb_attach,
207 .pr_detach = lagg_lb_detach,
208 .pr_start = lagg_lb_start,
209 .pr_input = lagg_lb_input,
210 .pr_addport = lagg_lb_port_create,
211 .pr_delport = lagg_lb_port_destroy,
214 .pr_num = LAGG_PROTO_LACP,
215 .pr_attach = lagg_lacp_attach,
216 .pr_detach = lagg_lacp_detach,
217 .pr_start = lagg_lacp_start,
218 .pr_input = lagg_lacp_input,
219 .pr_addport = lacp_port_create,
220 .pr_delport = lacp_port_destroy,
221 .pr_linkstate = lacp_linkstate,
222 .pr_init = lacp_init,
223 .pr_stop = lacp_stop,
224 .pr_lladdr = lagg_lacp_lladdr,
225 .pr_request = lacp_req,
226 .pr_portreq = lacp_portreq,
229 .pr_num = LAGG_PROTO_BROADCAST,
230 .pr_start = lagg_bcast_start,
231 .pr_input = lagg_bcast_input,
235 SYSCTL_DECL(_net_link);
236 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
239 /* Allow input on any failover links */
240 static VNET_DEFINE(int, lagg_failover_rx_all);
241 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
242 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
243 &VNET_NAME(lagg_failover_rx_all), 0,
244 "Accept input from any interface in a failover lagg");
246 /* Default value for using flowid */
247 static VNET_DEFINE(int, def_use_flowid) = 1;
248 #define V_def_use_flowid VNET(def_use_flowid)
249 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
250 &VNET_NAME(def_use_flowid), 0,
251 "Default setting for using flow id for load sharing");
253 /* Default value for flowid shift */
254 static VNET_DEFINE(int, def_flowid_shift) = 16;
255 #define V_def_flowid_shift VNET(def_flowid_shift)
256 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
257 &VNET_NAME(def_flowid_shift), 0,
258 "Default setting for flowid shift for load sharing");
261 vnet_lagg_init(const void *unused __unused)
264 LAGG_LIST_LOCK_INIT();
265 SLIST_INIT(&V_lagg_list);
266 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
267 lagg_clone_destroy, 0);
269 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
270 vnet_lagg_init, NULL);
273 vnet_lagg_uninit(const void *unused __unused)
276 if_clone_detach(V_lagg_cloner);
277 LAGG_LIST_LOCK_DESTROY();
279 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
280 vnet_lagg_uninit, NULL);
283 lagg_modevent(module_t mod, int type, void *data)
288 lagg_input_p = lagg_input;
289 lagg_linkstate_p = lagg_port_state;
290 lagg_detach_cookie = EVENTHANDLER_REGISTER(
291 ifnet_departure_event, lagg_port_ifdetach, NULL,
292 EVENTHANDLER_PRI_ANY);
295 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
298 lagg_linkstate_p = NULL;
306 static moduledata_t lagg_mod = {
312 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
313 MODULE_VERSION(if_lagg, 1);
316 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
319 LAGG_XLOCK_ASSERT(sc);
320 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
323 if (sc->sc_ifflags & IFF_DEBUG)
324 if_printf(sc->sc_ifp, "using proto %u\n", pr);
326 if (lagg_protos[pr].pr_attach != NULL)
327 lagg_protos[pr].pr_attach(sc);
332 lagg_proto_detach(struct lagg_softc *sc)
336 LAGG_XLOCK_ASSERT(sc);
337 LAGG_WLOCK_ASSERT(sc);
339 sc->sc_proto = LAGG_PROTO_NONE;
341 if (lagg_protos[pr].pr_detach != NULL)
342 lagg_protos[pr].pr_detach(sc);
348 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
351 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
355 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
358 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
362 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
365 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
368 return (lagg_protos[sc->sc_proto].pr_addport(lp));
372 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
375 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
376 lagg_protos[sc->sc_proto].pr_delport(lp);
380 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
383 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
384 lagg_protos[sc->sc_proto].pr_linkstate(lp);
388 lagg_proto_init(struct lagg_softc *sc)
391 if (lagg_protos[sc->sc_proto].pr_init != NULL)
392 lagg_protos[sc->sc_proto].pr_init(sc);
396 lagg_proto_stop(struct lagg_softc *sc)
399 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
400 lagg_protos[sc->sc_proto].pr_stop(sc);
404 lagg_proto_lladdr(struct lagg_softc *sc)
407 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
408 lagg_protos[sc->sc_proto].pr_lladdr(sc);
412 lagg_proto_request(struct lagg_softc *sc, void *v)
415 if (lagg_protos[sc->sc_proto].pr_request != NULL)
416 lagg_protos[sc->sc_proto].pr_request(sc, v);
420 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
423 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
424 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
428 * This routine is run via an vlan
432 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
434 struct lagg_softc *sc = ifp->if_softc;
435 struct lagg_port *lp;
437 if (ifp->if_softc != arg) /* Not our event */
441 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
442 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
447 * This routine is run via an vlan
451 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
453 struct lagg_softc *sc = ifp->if_softc;
454 struct lagg_port *lp;
456 if (ifp->if_softc != arg) /* Not our event */
460 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
461 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
466 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
468 struct lagg_softc *sc;
470 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
472 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
473 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
482 if (V_def_use_flowid)
483 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
484 sc->flowid_shift = V_def_flowid_shift;
486 /* Hash all layers by default */
487 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
489 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
491 SLIST_INIT(&sc->sc_ports);
493 /* Initialise pseudo media types */
494 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
496 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
497 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
499 if_initname(ifp, laggname, unit);
501 ifp->if_transmit = lagg_transmit;
502 ifp->if_qflush = lagg_qflush;
503 ifp->if_init = lagg_init;
504 ifp->if_ioctl = lagg_ioctl;
505 ifp->if_get_counter = lagg_get_counter;
506 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
508 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
509 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS | IFCAP_TXRTLMT;
511 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
515 * Attach as an ordinary ethernet device, children will be attached
516 * as special device IFT_IEEE8023ADLAG.
518 ether_ifattach(ifp, eaddr);
520 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
521 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
522 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
523 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
525 /* Insert into the global list of laggs */
527 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
535 lagg_clone_destroy(struct ifnet *ifp)
537 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
538 struct lagg_port *lp;
541 sc->sc_destroying = 1;
543 ifp->if_flags &= ~IFF_UP;
545 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
546 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
548 /* Shutdown and remove lagg ports */
549 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
550 lagg_port_destroy(lp, 1);
552 /* Unhook the aggregation protocol */
554 lagg_proto_detach(sc);
555 LAGG_UNLOCK_ASSERT(sc);
558 ifmedia_removeall(&sc->sc_media);
563 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
567 LAGG_LOCK_DESTROY(sc);
572 lagg_capabilities(struct lagg_softc *sc)
574 struct lagg_port *lp;
577 struct ifnet_hw_tsomax hw_tsomax;
579 LAGG_XLOCK_ASSERT(sc);
581 /* Get common enabled capabilities for the lagg ports */
583 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
584 ena &= lp->lp_ifp->if_capenable;
585 ena = (ena == ~0 ? 0 : ena);
588 * Apply common enabled capabilities back to the lagg ports.
589 * May require several iterations if they are dependent.
593 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
594 lagg_setcaps(lp, ena);
595 ena &= lp->lp_ifp->if_capenable;
597 } while (pena != ena);
599 /* Get other capabilities from the lagg ports */
602 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
603 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
604 cap &= lp->lp_ifp->if_capabilities;
605 hwa &= lp->lp_ifp->if_hwassist;
606 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
608 cap = (cap == ~0 ? 0 : cap);
609 hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
611 if (sc->sc_ifp->if_capabilities != cap ||
612 sc->sc_ifp->if_capenable != ena ||
613 sc->sc_ifp->if_hwassist != hwa ||
614 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
615 sc->sc_ifp->if_capabilities = cap;
616 sc->sc_ifp->if_capenable = ena;
617 sc->sc_ifp->if_hwassist = hwa;
618 getmicrotime(&sc->sc_ifp->if_lastchange);
620 if (sc->sc_ifflags & IFF_DEBUG)
621 if_printf(sc->sc_ifp,
622 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
627 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
629 struct lagg_softc *sc_ptr;
630 struct lagg_port *lp, *tlp;
634 LAGG_XLOCK_ASSERT(sc);
636 /* Limit the maximal number of lagg ports */
637 if (sc->sc_count >= LAGG_MAX_PORTS)
640 /* Check if port has already been associated to a lagg */
641 if (ifp->if_lagg != NULL) {
642 /* Port is already in the current lagg? */
643 lp = (struct lagg_port *)ifp->if_lagg;
644 if (lp->lp_softc == sc)
649 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
650 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
651 return (EPROTONOSUPPORT);
653 /* Allow the first Ethernet member to define the MTU */
654 if (SLIST_EMPTY(&sc->sc_ports))
655 sc->sc_ifp->if_mtu = ifp->if_mtu;
656 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
657 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
662 lp = malloc(sizeof(struct lagg_port), M_DEVBUF, M_WAITOK|M_ZERO);
665 /* Check if port is a stacked lagg */
667 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
668 if (ifp == sc_ptr->sc_ifp) {
672 /* XXX disable stacking for the moment, its untested */
673 #ifdef LAGG_PORT_STACKING
674 lp->lp_flags |= LAGG_PORT_STACK;
675 if (lagg_port_checkstacking(sc_ptr) >=
689 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
690 lp->lp_ifcapenable = ifp->if_capenable;
691 if (SLIST_EMPTY(&sc->sc_ports)) {
693 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
694 lagg_proto_lladdr(sc);
696 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
698 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
700 lagg_setflags(lp, 1);
703 if (SLIST_EMPTY(&sc->sc_ports))
706 /* Change the interface type */
707 lp->lp_iftype = ifp->if_type;
708 ifp->if_type = IFT_IEEE8023ADLAG;
710 lp->lp_ioctl = ifp->if_ioctl;
711 ifp->if_ioctl = lagg_port_ioctl;
712 lp->lp_output = ifp->if_output;
713 ifp->if_output = lagg_port_output;
715 /* Read port counters */
716 pval = lp->port_counters.val;
717 for (i = 0; i < IFCOUNTERS; i++, pval++)
718 *pval = ifp->if_get_counter(ifp, i);
721 * Insert into the list of ports.
722 * Keep ports sorted by if_index. It is handy, when configuration
723 * is predictable and `ifconfig laggN create ...` command
724 * will lead to the same result each time.
726 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
727 if (tlp->lp_ifp->if_index < ifp->if_index && (
728 SLIST_NEXT(tlp, lp_entries) == NULL ||
729 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
734 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
736 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
741 if ((error = lagg_proto_addport(sc, lp)) != 0) {
742 /* Remove the port, without calling pr_delport. */
743 lagg_port_destroy(lp, 0);
744 LAGG_UNLOCK_ASSERT(sc);
750 /* Update lagg capabilities */
751 lagg_capabilities(sc);
757 #ifdef LAGG_PORT_STACKING
759 lagg_port_checkstacking(struct lagg_softc *sc)
761 struct lagg_softc *sc_ptr;
762 struct lagg_port *lp;
765 LAGG_SXLOCK_ASSERT(sc);
766 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
767 if (lp->lp_flags & LAGG_PORT_STACK) {
768 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
769 m = MAX(m, lagg_port_checkstacking(sc_ptr));
778 lagg_port_destroy(struct lagg_port *lp, int rundelport)
780 struct lagg_softc *sc = lp->lp_softc;
781 struct lagg_port *lp_ptr, *lp0;
782 struct ifnet *ifp = lp->lp_ifp;
783 uint64_t *pval, vdiff;
786 LAGG_XLOCK_ASSERT(sc);
790 lagg_proto_delport(sc, lp);
792 LAGG_WLOCK_ASSERT(sc);
794 if (lp->lp_detaching == 0)
797 /* Restore interface */
798 ifp->if_type = lp->lp_iftype;
799 ifp->if_ioctl = lp->lp_ioctl;
800 ifp->if_output = lp->lp_output;
803 /* Update detached port counters */
804 pval = lp->port_counters.val;
805 for (i = 0; i < IFCOUNTERS; i++, pval++) {
806 vdiff = ifp->if_get_counter(ifp, i) - *pval;
807 sc->detached_counters.val[i] += vdiff;
810 /* Finally, remove the port from the lagg */
811 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
814 /* Update the primary interface */
815 if (lp == sc->sc_primary) {
816 uint8_t lladdr[ETHER_ADDR_LEN];
818 if ((lp0 = SLIST_FIRST(&sc->sc_ports)) == NULL)
819 bzero(&lladdr, ETHER_ADDR_LEN);
821 bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
822 sc->sc_primary = lp0;
823 if (sc->sc_destroying == 0) {
824 bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
825 lagg_proto_lladdr(sc);
827 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
832 * Update lladdr for each port (new primary needs update
833 * as well, to switch from old lladdr to its 'real' one)
835 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
836 if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
841 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
843 if (lp->lp_detaching == 0) {
844 lagg_setflags(lp, 0);
845 lagg_setcaps(lp, lp->lp_ifcapenable);
846 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
852 /* Update lagg capabilities */
853 lagg_capabilities(sc);
860 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
862 struct lagg_reqport *rp = (struct lagg_reqport *)data;
863 struct lagg_softc *sc;
864 struct lagg_port *lp = NULL;
867 /* Should be checked by the caller */
868 if (ifp->if_type != IFT_IEEE8023ADLAG ||
869 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
874 if (rp->rp_portname[0] == '\0' ||
875 ifunit(rp->rp_portname) != ifp) {
881 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
887 lagg_port2req(lp, rp);
892 if (lp->lp_ioctl == NULL) {
896 error = (*lp->lp_ioctl)(ifp, cmd, data);
900 /* Update lagg interface capabilities */
902 lagg_capabilities(sc);
904 VLAN_CAPABILITIES(sc->sc_ifp);
908 /* Do not allow the MTU to be changed once joined */
919 if (lp != NULL && lp->lp_ioctl != NULL)
920 return ((*lp->lp_ioctl)(ifp, cmd, data));
926 * Requests counter @cnt data.
928 * Counter value is calculated the following way:
929 * 1) for each port, sum difference between current and "initial" measurements.
930 * 2) add lagg logical interface counters.
931 * 3) add data from detached_counters array.
933 * We also do the following things on ports attach/detach:
934 * 1) On port attach we store all counters it has into port_counter array.
935 * 2) On port detach we add the different between "initial" and
936 * current counters data to detached_counters array.
939 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
941 struct lagg_softc *sc;
942 struct lagg_port *lp;
944 struct rm_priotracker tracker;
945 uint64_t newval, oldval, vsum;
947 /* Revise this when we've got non-generic counters. */
948 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
950 sc = (struct lagg_softc *)ifp->if_softc;
951 LAGG_RLOCK(sc, &tracker);
954 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
955 /* Saved attached value */
956 oldval = lp->port_counters.val[cnt];
959 newval = lpifp->if_get_counter(lpifp, cnt);
960 /* Calculate diff and save new */
961 vsum += newval - oldval;
965 * Add counter data which might be added by upper
966 * layer protocols operating on logical interface.
968 vsum += if_get_counter_default(ifp, cnt);
971 * Add counter data from detached ports counters
973 vsum += sc->detached_counters.val[cnt];
975 LAGG_RUNLOCK(sc, &tracker);
981 * For direct output to child ports.
984 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
985 const struct sockaddr *dst, struct route *ro)
987 struct lagg_port *lp = ifp->if_lagg;
989 switch (dst->sa_family) {
990 case pseudo_AF_HDRCMPLT:
992 return ((*lp->lp_output)(ifp, m, dst, ro));
995 /* drop any other frames */
1001 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1003 struct lagg_port *lp;
1004 struct lagg_softc *sc;
1006 if ((lp = ifp->if_lagg) == NULL)
1008 /* If the ifnet is just being renamed, don't do anything. */
1009 if (ifp->if_flags & IFF_RENAMING)
1015 lp->lp_detaching = 1;
1016 lagg_port_destroy(lp, 1);
1018 VLAN_CAPABILITIES(sc->sc_ifp);
1022 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1024 struct lagg_softc *sc = lp->lp_softc;
1026 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1027 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1028 rp->rp_prio = lp->lp_prio;
1029 rp->rp_flags = lp->lp_flags;
1030 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1032 /* Add protocol specific flags */
1033 switch (sc->sc_proto) {
1034 case LAGG_PROTO_FAILOVER:
1035 if (lp == sc->sc_primary)
1036 rp->rp_flags |= LAGG_PORT_MASTER;
1037 if (lp == lagg_link_active(sc, sc->sc_primary))
1038 rp->rp_flags |= LAGG_PORT_ACTIVE;
1041 case LAGG_PROTO_ROUNDROBIN:
1042 case LAGG_PROTO_LOADBALANCE:
1043 case LAGG_PROTO_BROADCAST:
1044 if (LAGG_PORTACTIVE(lp))
1045 rp->rp_flags |= LAGG_PORT_ACTIVE;
1048 case LAGG_PROTO_LACP:
1049 /* LACP has a different definition of active */
1050 if (lacp_isactive(lp))
1051 rp->rp_flags |= LAGG_PORT_ACTIVE;
1052 if (lacp_iscollecting(lp))
1053 rp->rp_flags |= LAGG_PORT_COLLECTING;
1054 if (lacp_isdistributing(lp))
1055 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1062 lagg_init(void *xsc)
1064 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1065 struct ifnet *ifp = sc->sc_ifp;
1066 struct lagg_port *lp;
1069 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1074 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1077 * Update the port lladdrs if needed.
1078 * This might be if_setlladdr() notification
1079 * that lladdr has been changed.
1081 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1082 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1083 ETHER_ADDR_LEN) != 0)
1084 if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1087 lagg_proto_init(sc);
1093 lagg_stop(struct lagg_softc *sc)
1095 struct ifnet *ifp = sc->sc_ifp;
1097 LAGG_XLOCK_ASSERT(sc);
1099 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1102 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1104 lagg_proto_stop(sc);
1108 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1110 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1111 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1112 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1113 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1114 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1115 struct ifreq *ifr = (struct ifreq *)data;
1116 struct lagg_port *lp;
1118 struct thread *td = curthread;
1120 int count, buflen, len, error = 0;
1122 bzero(&rpbuf, sizeof(rpbuf));
1127 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1128 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1129 ra->ra_proto = sc->sc_proto;
1130 lagg_proto_request(sc, &ra->ra_psc);
1133 len = min(ra->ra_size, buflen);
1134 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1135 if (len < sizeof(rpbuf))
1138 lagg_port2req(lp, &rpbuf);
1139 memcpy(buf, &rpbuf, sizeof(rpbuf));
1141 buf += sizeof(rpbuf);
1142 len -= sizeof(rpbuf);
1145 ra->ra_ports = count;
1146 ra->ra_size = count * sizeof(rpbuf);
1147 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1148 free(outbuf, M_TEMP);
1151 error = priv_check(td, PRIV_NET_LAGG);
1154 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1155 error = EPROTONOSUPPORT;
1161 lagg_proto_detach(sc);
1162 LAGG_UNLOCK_ASSERT(sc);
1163 lagg_proto_attach(sc, ra->ra_proto);
1168 ro->ro_opts = sc->sc_opts;
1169 if (sc->sc_proto == LAGG_PROTO_LACP) {
1170 struct lacp_softc *lsc;
1172 lsc = (struct lacp_softc *)sc->sc_psc;
1173 if (lsc->lsc_debug.lsc_tx_test != 0)
1174 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1175 if (lsc->lsc_debug.lsc_rx_test != 0)
1176 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1177 if (lsc->lsc_strict_mode != 0)
1178 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1179 if (lsc->lsc_fast_timeout != 0)
1180 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1182 ro->ro_active = sc->sc_active;
1185 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1186 ro->ro_active += LAGG_PORTACTIVE(lp);
1188 ro->ro_bkt = sc->sc_bkt;
1189 ro->ro_flapping = sc->sc_flapping;
1190 ro->ro_flowid_shift = sc->flowid_shift;
1194 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1195 if (ro->ro_bkt == 0)
1196 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1198 sc->sc_bkt = ro->ro_bkt;
1200 error = priv_check(td, PRIV_NET_LAGG);
1203 if (ro->ro_opts == 0)
1206 * Set options. LACP options are stored in sc->sc_psc,
1211 switch (ro->ro_opts) {
1212 case LAGG_OPT_USE_FLOWID:
1213 case -LAGG_OPT_USE_FLOWID:
1214 case LAGG_OPT_FLOWIDSHIFT:
1218 case LAGG_OPT_LACP_TXTEST:
1219 case -LAGG_OPT_LACP_TXTEST:
1220 case LAGG_OPT_LACP_RXTEST:
1221 case -LAGG_OPT_LACP_RXTEST:
1222 case LAGG_OPT_LACP_STRICT:
1223 case -LAGG_OPT_LACP_STRICT:
1224 case LAGG_OPT_LACP_TIMEOUT:
1225 case -LAGG_OPT_LACP_TIMEOUT:
1236 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1237 /* Invalid combination of options specified. */
1240 break; /* Return from SIOCSLAGGOPTS. */
1243 * Store new options into sc->sc_opts except for
1244 * FLOWIDSHIFT and LACP options.
1247 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1248 sc->flowid_shift = ro->ro_flowid_shift;
1249 else if (ro->ro_opts > 0)
1250 sc->sc_opts |= ro->ro_opts;
1252 sc->sc_opts &= ~ro->ro_opts;
1254 struct lacp_softc *lsc;
1255 struct lacp_port *lp;
1257 lsc = (struct lacp_softc *)sc->sc_psc;
1259 switch (ro->ro_opts) {
1260 case LAGG_OPT_LACP_TXTEST:
1261 lsc->lsc_debug.lsc_tx_test = 1;
1263 case -LAGG_OPT_LACP_TXTEST:
1264 lsc->lsc_debug.lsc_tx_test = 0;
1266 case LAGG_OPT_LACP_RXTEST:
1267 lsc->lsc_debug.lsc_rx_test = 1;
1269 case -LAGG_OPT_LACP_RXTEST:
1270 lsc->lsc_debug.lsc_rx_test = 0;
1272 case LAGG_OPT_LACP_STRICT:
1273 lsc->lsc_strict_mode = 1;
1275 case -LAGG_OPT_LACP_STRICT:
1276 lsc->lsc_strict_mode = 0;
1278 case LAGG_OPT_LACP_TIMEOUT:
1280 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1281 lp->lp_state |= LACP_STATE_TIMEOUT;
1283 lsc->lsc_fast_timeout = 1;
1285 case -LAGG_OPT_LACP_TIMEOUT:
1287 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1288 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1290 lsc->lsc_fast_timeout = 0;
1296 case SIOCGLAGGFLAGS:
1299 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1300 rf->rf_flags |= LAGG_F_HASHL2;
1301 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1302 rf->rf_flags |= LAGG_F_HASHL3;
1303 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1304 rf->rf_flags |= LAGG_F_HASHL4;
1308 error = priv_check(td, PRIV_NET_LAGG);
1311 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1317 if (rf->rf_flags & LAGG_F_HASHL2)
1318 sc->sc_flags |= MBUF_HASHFLAG_L2;
1319 if (rf->rf_flags & LAGG_F_HASHL3)
1320 sc->sc_flags |= MBUF_HASHFLAG_L3;
1321 if (rf->rf_flags & LAGG_F_HASHL4)
1322 sc->sc_flags |= MBUF_HASHFLAG_L4;
1326 if (rp->rp_portname[0] == '\0' ||
1327 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1333 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1334 lp->lp_softc != sc) {
1341 lagg_port2req(lp, rp);
1346 error = priv_check(td, PRIV_NET_LAGG);
1349 if (rp->rp_portname[0] == '\0' ||
1350 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1356 * A laggport interface should not have inet6 address
1357 * because two interfaces with a valid link-local
1358 * scope zone must not be merged in any form. This
1359 * restriction is needed to prevent violation of
1360 * link-local scope zone. Attempts to add a laggport
1361 * interface which has inet6 addresses triggers
1362 * removal of all inet6 addresses on the member
1365 if (in6ifa_llaonifp(tpif)) {
1367 if_printf(sc->sc_ifp,
1368 "IPv6 addresses on %s have been removed "
1369 "before adding it as a member to prevent "
1370 "IPv6 address scope violation.\n",
1375 error = lagg_port_create(sc, tpif);
1378 VLAN_CAPABILITIES(ifp);
1380 case SIOCSLAGGDELPORT:
1381 error = priv_check(td, PRIV_NET_LAGG);
1384 if (rp->rp_portname[0] == '\0' ||
1385 (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1391 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1392 lp->lp_softc != sc) {
1399 error = lagg_port_destroy(lp, 1);
1402 VLAN_CAPABILITIES(ifp);
1405 /* Set flags on ports too */
1407 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1408 lagg_setflags(lp, 1);
1411 if (!(ifp->if_flags & IFF_UP) &&
1412 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1414 * If interface is marked down and it is running,
1415 * then stop and disable it.
1419 } else if ((ifp->if_flags & IFF_UP) &&
1420 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1422 * If interface is marked up and it is stopped, then
1426 (*ifp->if_init)(sc);
1433 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1442 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1447 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1448 if (lp->lp_ioctl != NULL)
1449 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1451 lagg_capabilities(sc);
1453 VLAN_CAPABILITIES(ifp);
1458 /* Do not allow the MTU to be directly changed */
1463 error = ether_ioctl(ifp, cmd, data);
1471 lagg_snd_tag_alloc(struct ifnet *ifp,
1472 union if_snd_tag_alloc_params *params,
1473 struct m_snd_tag **ppmt)
1475 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1476 struct lagg_port *lp;
1480 switch (sc->sc_proto) {
1481 case LAGG_PROTO_FAILOVER:
1482 lp = lagg_link_active(sc, sc->sc_primary);
1484 case LAGG_PROTO_LOADBALANCE:
1485 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1486 params->hdr.flowtype == M_HASHTYPE_NONE)
1487 return (EOPNOTSUPP);
1488 p = params->hdr.flowid >> sc->flowid_shift;
1490 lb = (struct lagg_lb *)sc->sc_psc;
1491 lp = lb->lb_ports[p];
1492 lp = lagg_link_active(sc, lp);
1494 case LAGG_PROTO_LACP:
1495 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1496 params->hdr.flowtype == M_HASHTYPE_NONE)
1497 return (EOPNOTSUPP);
1498 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1501 return (EOPNOTSUPP);
1504 return (EOPNOTSUPP);
1506 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1507 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1508 return (EOPNOTSUPP);
1510 /* forward allocation request */
1511 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1516 lagg_setmulti(struct lagg_port *lp)
1518 struct lagg_softc *sc = lp->lp_softc;
1519 struct ifnet *ifp = lp->lp_ifp;
1520 struct ifnet *scifp = sc->sc_ifp;
1522 struct ifmultiaddr *ifma;
1525 LAGG_WLOCK_ASSERT(sc);
1526 IF_ADDR_WLOCK(scifp);
1527 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1528 if (ifma->ifma_addr->sa_family != AF_LINK)
1530 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1532 IF_ADDR_WUNLOCK(scifp);
1535 bcopy(ifma->ifma_addr, &mc->mc_addr,
1536 ifma->ifma_addr->sa_len);
1537 mc->mc_addr.sdl_index = ifp->if_index;
1539 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1541 IF_ADDR_WUNLOCK(scifp);
1542 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1543 error = if_addmulti(ifp,
1544 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1552 lagg_clrmulti(struct lagg_port *lp)
1556 LAGG_WLOCK_ASSERT(lp->lp_softc);
1557 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1558 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1559 if (mc->mc_ifma && lp->lp_detaching == 0)
1560 if_delmulti_ifma(mc->mc_ifma);
1567 lagg_setcaps(struct lagg_port *lp, int cap)
1571 if (lp->lp_ifp->if_capenable == cap)
1573 if (lp->lp_ioctl == NULL)
1575 ifr.ifr_reqcap = cap;
1576 return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1579 /* Handle a ref counted flag that should be set on the lagg port as well */
1581 lagg_setflag(struct lagg_port *lp, int flag, int status,
1582 int (*func)(struct ifnet *, int))
1584 struct lagg_softc *sc = lp->lp_softc;
1585 struct ifnet *scifp = sc->sc_ifp;
1586 struct ifnet *ifp = lp->lp_ifp;
1589 LAGG_XLOCK_ASSERT(sc);
1591 status = status ? (scifp->if_flags & flag) : 0;
1592 /* Now "status" contains the flag value or 0 */
1595 * See if recorded ports status is different from what
1596 * we want it to be. If it is, flip it. We record ports
1597 * status in lp_ifflags so that we won't clear ports flag
1598 * we haven't set. In fact, we don't clear or set ports
1599 * flags directly, but get or release references to them.
1600 * That's why we can be sure that recorded flags still are
1601 * in accord with actual ports flags.
1603 if (status != (lp->lp_ifflags & flag)) {
1604 error = (*func)(ifp, status);
1607 lp->lp_ifflags &= ~flag;
1608 lp->lp_ifflags |= status;
1614 * Handle IFF_* flags that require certain changes on the lagg port
1615 * if "status" is true, update ports flags respective to the lagg
1616 * if "status" is false, forcedly clear the flags set on port.
1619 lagg_setflags(struct lagg_port *lp, int status)
1623 for (i = 0; lagg_pflags[i].flag; i++) {
1624 error = lagg_setflag(lp, lagg_pflags[i].flag,
1625 status, lagg_pflags[i].func);
1633 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1635 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1636 int error, len, mcast;
1637 struct rm_priotracker tracker;
1639 len = m->m_pkthdr.len;
1640 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1642 LAGG_RLOCK(sc, &tracker);
1643 /* We need a Tx algorithm and at least one port */
1644 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1645 LAGG_RUNLOCK(sc, &tracker);
1647 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1651 ETHER_BPF_MTAP(ifp, m);
1653 error = lagg_proto_start(sc, m);
1654 LAGG_RUNLOCK(sc, &tracker);
1657 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1663 * The ifp->if_qflush entry point for lagg(4) is no-op.
1666 lagg_qflush(struct ifnet *ifp __unused)
1670 static struct mbuf *
1671 lagg_input(struct ifnet *ifp, struct mbuf *m)
1673 struct lagg_port *lp = ifp->if_lagg;
1674 struct lagg_softc *sc = lp->lp_softc;
1675 struct ifnet *scifp = sc->sc_ifp;
1676 struct rm_priotracker tracker;
1678 LAGG_RLOCK(sc, &tracker);
1679 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1680 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1681 sc->sc_proto == LAGG_PROTO_NONE) {
1682 LAGG_RUNLOCK(sc, &tracker);
1687 ETHER_BPF_MTAP(scifp, m);
1689 if (lp->lp_detaching != 0) {
1693 m = lagg_proto_input(sc, lp, m);
1696 if (scifp->if_flags & IFF_MONITOR) {
1702 LAGG_RUNLOCK(sc, &tracker);
1707 lagg_media_change(struct ifnet *ifp)
1709 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1711 if (sc->sc_ifflags & IFF_DEBUG)
1712 printf("%s\n", __func__);
1719 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1721 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1722 struct lagg_port *lp;
1724 imr->ifm_status = IFM_AVALID;
1725 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1728 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1729 if (LAGG_PORTACTIVE(lp))
1730 imr->ifm_status |= IFM_ACTIVE;
1736 lagg_linkstate(struct lagg_softc *sc)
1738 struct lagg_port *lp;
1739 int new_link = LINK_STATE_DOWN;
1742 LAGG_XLOCK_ASSERT(sc);
1744 /* Our link is considered up if at least one of our ports is active */
1745 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1746 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1747 new_link = LINK_STATE_UP;
1751 if_link_state_change(sc->sc_ifp, new_link);
1753 /* Update if_baudrate to reflect the max possible speed */
1754 switch (sc->sc_proto) {
1755 case LAGG_PROTO_FAILOVER:
1756 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1757 sc->sc_primary->lp_ifp->if_baudrate : 0;
1759 case LAGG_PROTO_ROUNDROBIN:
1760 case LAGG_PROTO_LOADBALANCE:
1761 case LAGG_PROTO_BROADCAST:
1763 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1764 speed += lp->lp_ifp->if_baudrate;
1765 sc->sc_ifp->if_baudrate = speed;
1767 case LAGG_PROTO_LACP:
1768 /* LACP updates if_baudrate itself */
1774 lagg_port_state(struct ifnet *ifp, int state)
1776 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1777 struct lagg_softc *sc = NULL;
1786 lagg_proto_linkstate(sc, lp);
1791 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1793 struct lagg_port *lp_next, *rval = NULL;
1796 * Search a port which reports an active link state.
1801 if (LAGG_PORTACTIVE(lp)) {
1805 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1806 LAGG_PORTACTIVE(lp_next)) {
1812 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1813 if (LAGG_PORTACTIVE(lp_next)) {
1824 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1827 return (ifp->if_transmit)(ifp, m);
1831 * Simple round robin aggregation
1834 lagg_rr_attach(struct lagg_softc *sc)
1837 sc->sc_bkt_count = sc->sc_bkt;
1841 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1843 struct lagg_port *lp;
1846 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1847 sc->sc_bkt_count = sc->sc_bkt;
1849 if (sc->sc_bkt > 0) {
1850 atomic_subtract_int(&sc->sc_bkt_count, 1);
1851 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1852 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1856 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1859 lp = SLIST_FIRST(&sc->sc_ports);
1862 lp = SLIST_NEXT(lp, lp_entries);
1865 * Check the port's link state. This will return the next active
1866 * port if the link is down or the port is NULL.
1868 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1874 return (lagg_enqueue(lp->lp_ifp, m));
1877 static struct mbuf *
1878 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1880 struct ifnet *ifp = sc->sc_ifp;
1882 /* Just pass in the packet to our lagg device */
1883 m->m_pkthdr.rcvif = ifp;
1892 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1894 int active_ports = 0;
1897 struct lagg_port *lp, *last = NULL;
1900 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1901 if (!LAGG_PORTACTIVE(lp))
1907 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1914 ret = lagg_enqueue(last->lp_ifp, m0);
1924 if ((last = lagg_link_active(sc, last)) == NULL) {
1929 ret = lagg_enqueue(last->lp_ifp, m);
1940 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1942 struct ifnet *ifp = sc->sc_ifp;
1944 /* Just pass in the packet to our lagg device */
1945 m->m_pkthdr.rcvif = ifp;
1953 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1955 struct lagg_port *lp;
1957 /* Use the master port if active or the next available port */
1958 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1964 return (lagg_enqueue(lp->lp_ifp, m));
1967 static struct mbuf *
1968 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1970 struct ifnet *ifp = sc->sc_ifp;
1971 struct lagg_port *tmp_tp;
1973 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
1974 m->m_pkthdr.rcvif = ifp;
1978 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1979 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1981 * If tmp_tp is null, we've received a packet when all
1982 * our links are down. Weird, but process it anyways.
1984 if ((tmp_tp == NULL || tmp_tp == lp)) {
1985 m->m_pkthdr.rcvif = ifp;
1998 lagg_lb_attach(struct lagg_softc *sc)
2000 struct lagg_port *lp;
2003 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2004 lb->lb_key = m_ether_tcpip_hash_init();
2007 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2008 lagg_lb_port_create(lp);
2012 lagg_lb_detach(struct lagg_softc *sc)
2016 lb = (struct lagg_lb *)sc->sc_psc;
2023 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2025 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2026 struct lagg_port *lp_next;
2029 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2030 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2033 if (i >= LAGG_MAX_PORTS)
2035 if (sc->sc_ifflags & IFF_DEBUG)
2036 printf("%s: port %s at index %d\n",
2037 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2038 lb->lb_ports[i++] = lp_next;
2045 lagg_lb_port_create(struct lagg_port *lp)
2047 struct lagg_softc *sc = lp->lp_softc;
2048 return (lagg_lb_porttable(sc, NULL));
2052 lagg_lb_port_destroy(struct lagg_port *lp)
2054 struct lagg_softc *sc = lp->lp_softc;
2055 lagg_lb_porttable(sc, lp);
2059 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2061 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2062 struct lagg_port *lp = NULL;
2065 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2066 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2067 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2069 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2071 lp = lb->lb_ports[p];
2074 * Check the port's link state. This will return the next active
2075 * port if the link is down or the port is NULL.
2077 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2083 return (lagg_enqueue(lp->lp_ifp, m));
2086 static struct mbuf *
2087 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2089 struct ifnet *ifp = sc->sc_ifp;
2091 /* Just pass in the packet to our lagg device */
2092 m->m_pkthdr.rcvif = ifp;
2101 lagg_lacp_attach(struct lagg_softc *sc)
2103 struct lagg_port *lp;
2106 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2107 lacp_port_create(lp);
2111 lagg_lacp_detach(struct lagg_softc *sc)
2113 struct lagg_port *lp;
2116 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2117 lacp_port_destroy(lp);
2127 lagg_lacp_lladdr(struct lagg_softc *sc)
2129 struct lagg_port *lp;
2131 LAGG_SXLOCK_ASSERT(sc);
2133 /* purge all the lacp ports */
2134 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2135 lacp_port_destroy(lp);
2137 /* add them back in */
2138 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2139 lacp_port_create(lp);
2143 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2145 struct lagg_port *lp;
2147 lp = lacp_select_tx_port(sc, m);
2154 return (lagg_enqueue(lp->lp_ifp, m));
2157 static struct mbuf *
2158 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2160 struct ifnet *ifp = sc->sc_ifp;
2161 struct ether_header *eh;
2164 eh = mtod(m, struct ether_header *);
2165 etype = ntohs(eh->ether_type);
2167 /* Tap off LACP control messages */
2168 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2169 m = lacp_input(lp, m);
2175 * If the port is not collecting or not in the active aggregator then
2178 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2183 m->m_pkthdr.rcvif = ifp;