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>
42 #include <sys/taskqueue.h>
43 #include <sys/eventhandler.h>
45 #include <net/ethernet.h>
47 #include <net/if_clone.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
56 #if defined(INET) || defined(INET6)
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/if_ether.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_ifattach.h>
71 #include <net/if_vlan_var.h>
72 #include <net/if_lagg.h>
73 #include <net/ieee8023ad_lacp.h>
75 /* Special flags we should propagate to the lagg ports. */
78 int (*func)(struct ifnet *, int);
80 {IFF_PROMISC, ifpromisc},
81 {IFF_ALLMULTI, if_allmulti},
85 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
86 #define V_lagg_list VNET(lagg_list)
87 static VNET_DEFINE(struct mtx, lagg_list_mtx);
88 #define V_lagg_list_mtx VNET(lagg_list_mtx)
89 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
90 "if_lagg list", NULL, MTX_DEF)
91 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
92 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
93 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
94 eventhandler_tag lagg_detach_cookie = NULL;
96 static int lagg_clone_create(struct if_clone *, int, caddr_t);
97 static void lagg_clone_destroy(struct ifnet *);
98 static VNET_DEFINE(struct if_clone *, lagg_cloner);
99 #define V_lagg_cloner VNET(lagg_cloner)
100 static const char laggname[] = "lagg";
102 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
103 static void lagg_capabilities(struct lagg_softc *);
104 static void lagg_port_lladdr(struct lagg_port *, uint8_t *, lagg_llqtype);
105 static void lagg_port_setlladdr(void *, int);
106 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
107 static int lagg_port_destroy(struct lagg_port *, int);
108 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
109 static void lagg_linkstate(struct lagg_softc *);
110 static void lagg_port_state(struct ifnet *, int);
111 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
112 static int lagg_port_output(struct ifnet *, struct mbuf *,
113 const struct sockaddr *, struct route *);
114 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
115 #ifdef LAGG_PORT_STACKING
116 static int lagg_port_checkstacking(struct lagg_softc *);
118 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
119 static void lagg_init(void *);
120 static void lagg_stop(struct lagg_softc *);
121 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
123 static int lagg_snd_tag_alloc(struct ifnet *,
124 union if_snd_tag_alloc_params *,
125 struct m_snd_tag **);
127 static int lagg_ether_setmulti(struct lagg_softc *);
128 static int lagg_ether_cmdmulti(struct lagg_port *, int);
129 static int lagg_setflag(struct lagg_port *, int, int,
130 int (*func)(struct ifnet *, int));
131 static int lagg_setflags(struct lagg_port *, int status);
132 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
133 static int lagg_transmit(struct ifnet *, struct mbuf *);
134 static void lagg_qflush(struct ifnet *);
135 static int lagg_media_change(struct ifnet *);
136 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
137 static struct lagg_port *lagg_link_active(struct lagg_softc *,
140 /* Simple round robin */
141 static void lagg_rr_attach(struct lagg_softc *);
142 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
143 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
146 /* Active failover */
147 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
148 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
152 static void lagg_lb_attach(struct lagg_softc *);
153 static void lagg_lb_detach(struct lagg_softc *);
154 static int lagg_lb_port_create(struct lagg_port *);
155 static void lagg_lb_port_destroy(struct lagg_port *);
156 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
157 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
159 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
162 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
163 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
167 static void lagg_lacp_attach(struct lagg_softc *);
168 static void lagg_lacp_detach(struct lagg_softc *);
169 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
170 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
172 static void lagg_lacp_lladdr(struct lagg_softc *);
174 /* lagg protocol table */
175 static const struct lagg_proto {
177 void (*pr_attach)(struct lagg_softc *);
178 void (*pr_detach)(struct lagg_softc *);
179 int (*pr_start)(struct lagg_softc *, struct mbuf *);
180 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
182 int (*pr_addport)(struct lagg_port *);
183 void (*pr_delport)(struct lagg_port *);
184 void (*pr_linkstate)(struct lagg_port *);
185 void (*pr_init)(struct lagg_softc *);
186 void (*pr_stop)(struct lagg_softc *);
187 void (*pr_lladdr)(struct lagg_softc *);
188 void (*pr_request)(struct lagg_softc *, void *);
189 void (*pr_portreq)(struct lagg_port *, void *);
192 .pr_num = LAGG_PROTO_NONE
195 .pr_num = LAGG_PROTO_ROUNDROBIN,
196 .pr_attach = lagg_rr_attach,
197 .pr_start = lagg_rr_start,
198 .pr_input = lagg_rr_input,
201 .pr_num = LAGG_PROTO_FAILOVER,
202 .pr_start = lagg_fail_start,
203 .pr_input = lagg_fail_input,
206 .pr_num = LAGG_PROTO_LOADBALANCE,
207 .pr_attach = lagg_lb_attach,
208 .pr_detach = lagg_lb_detach,
209 .pr_start = lagg_lb_start,
210 .pr_input = lagg_lb_input,
211 .pr_addport = lagg_lb_port_create,
212 .pr_delport = lagg_lb_port_destroy,
215 .pr_num = LAGG_PROTO_LACP,
216 .pr_attach = lagg_lacp_attach,
217 .pr_detach = lagg_lacp_detach,
218 .pr_start = lagg_lacp_start,
219 .pr_input = lagg_lacp_input,
220 .pr_addport = lacp_port_create,
221 .pr_delport = lacp_port_destroy,
222 .pr_linkstate = lacp_linkstate,
223 .pr_init = lacp_init,
224 .pr_stop = lacp_stop,
225 .pr_lladdr = lagg_lacp_lladdr,
226 .pr_request = lacp_req,
227 .pr_portreq = lacp_portreq,
230 .pr_num = LAGG_PROTO_BROADCAST,
231 .pr_start = lagg_bcast_start,
232 .pr_input = lagg_bcast_input,
236 SYSCTL_DECL(_net_link);
237 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
240 /* Allow input on any failover links */
241 static VNET_DEFINE(int, lagg_failover_rx_all);
242 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
243 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
244 &VNET_NAME(lagg_failover_rx_all), 0,
245 "Accept input from any interface in a failover lagg");
247 /* Default value for using flowid */
248 static VNET_DEFINE(int, def_use_flowid) = 1;
249 #define V_def_use_flowid VNET(def_use_flowid)
250 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
251 &VNET_NAME(def_use_flowid), 0,
252 "Default setting for using flow id for load sharing");
254 /* Default value for flowid shift */
255 static VNET_DEFINE(int, def_flowid_shift) = 16;
256 #define V_def_flowid_shift VNET(def_flowid_shift)
257 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
258 &VNET_NAME(def_flowid_shift), 0,
259 "Default setting for flowid shift for load sharing");
262 vnet_lagg_init(const void *unused __unused)
265 LAGG_LIST_LOCK_INIT();
266 SLIST_INIT(&V_lagg_list);
267 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
268 lagg_clone_destroy, 0);
270 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
271 vnet_lagg_init, NULL);
274 vnet_lagg_uninit(const void *unused __unused)
277 if_clone_detach(V_lagg_cloner);
278 LAGG_LIST_LOCK_DESTROY();
280 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
281 vnet_lagg_uninit, NULL);
284 lagg_modevent(module_t mod, int type, void *data)
289 lagg_input_p = lagg_input;
290 lagg_linkstate_p = lagg_port_state;
291 lagg_detach_cookie = EVENTHANDLER_REGISTER(
292 ifnet_departure_event, lagg_port_ifdetach, NULL,
293 EVENTHANDLER_PRI_ANY);
296 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
299 lagg_linkstate_p = NULL;
307 static moduledata_t lagg_mod = {
313 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
314 MODULE_VERSION(if_lagg, 1);
317 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
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_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;
436 struct rm_priotracker tracker;
438 if (ifp->if_softc != arg) /* Not our event */
441 LAGG_RLOCK(sc, &tracker);
442 if (!SLIST_EMPTY(&sc->sc_ports)) {
443 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
444 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
446 LAGG_RUNLOCK(sc, &tracker);
450 * This routine is run via an vlan
454 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
456 struct lagg_softc *sc = ifp->if_softc;
457 struct lagg_port *lp;
458 struct rm_priotracker tracker;
460 if (ifp->if_softc != arg) /* Not our event */
463 LAGG_RLOCK(sc, &tracker);
464 if (!SLIST_EMPTY(&sc->sc_ports)) {
465 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
466 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
468 LAGG_RUNLOCK(sc, &tracker);
472 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
474 struct lagg_softc *sc;
476 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
478 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
479 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
485 if (V_def_use_flowid)
486 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
487 sc->flowid_shift = V_def_flowid_shift;
489 /* Hash all layers by default */
490 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
492 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
495 SLIST_INIT(&sc->sc_ports);
496 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
498 /* Initialise pseudo media types */
499 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
501 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
502 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
504 if_initname(ifp, laggname, unit);
506 ifp->if_transmit = lagg_transmit;
507 ifp->if_qflush = lagg_qflush;
508 ifp->if_init = lagg_init;
509 ifp->if_ioctl = lagg_ioctl;
510 ifp->if_get_counter = lagg_get_counter;
511 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
513 ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
514 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS | IFCAP_TXRTLMT;
516 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
520 * Attach as an ordinary ethernet device, children will be attached
521 * as special device IFT_IEEE8023ADLAG.
523 ether_ifattach(ifp, eaddr);
525 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
526 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
527 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
528 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
530 /* Insert into the global list of laggs */
532 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
539 lagg_clone_destroy(struct ifnet *ifp)
541 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
542 struct lagg_port *lp;
547 ifp->if_flags &= ~IFF_UP;
549 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
550 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
552 /* Shutdown and remove lagg ports */
553 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL) {
554 lp->lp_detaching = LAGG_CLONE_DESTROY;
555 lagg_port_destroy(lp, 1);
557 /* Unhook the aggregation protocol */
558 lagg_proto_detach(sc);
559 LAGG_UNLOCK_ASSERT(sc);
561 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
562 ifmedia_removeall(&sc->sc_media);
567 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
570 LAGG_LOCK_DESTROY(sc);
575 * Set link-layer address on the lagg interface itself.
577 * Set noinline to be dtrace-friendly
579 static __noinline void
580 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
582 struct ifnet *ifp = sc->sc_ifp;
585 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
588 LAGG_WLOCK_ASSERT(sc);
590 * Set the link layer address on the lagg interface.
591 * lagg_proto_lladdr() notifies the MAC change to
592 * the aggregation protocol. iflladdr_event handler which
593 * may trigger gratuitous ARPs for INET will be handled in
596 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
597 lagg_proto_lladdr(sc);
600 * Send notification request for lagg interface
601 * itself. Note that new lladdr is already set.
603 bzero(&lp, sizeof(lp));
604 lp.lp_ifp = sc->sc_ifp;
607 /* Do not request lladdr change */
608 lagg_port_lladdr(&lp, lladdr, LAGG_LLQTYPE_VIRT);
612 lagg_capabilities(struct lagg_softc *sc)
614 struct lagg_port *lp;
615 int cap = ~0, ena = ~0;
617 struct ifnet_hw_tsomax hw_tsomax;
619 LAGG_WLOCK_ASSERT(sc);
621 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
623 /* Get capabilities from the lagg ports */
624 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
625 cap &= lp->lp_ifp->if_capabilities;
626 ena &= lp->lp_ifp->if_capenable;
627 hwa &= lp->lp_ifp->if_hwassist;
628 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
630 cap = (cap == ~0 ? 0 : cap);
631 ena = (ena == ~0 ? 0 : ena);
632 hwa = (hwa == ~0 ? 0 : hwa);
634 if (sc->sc_ifp->if_capabilities != cap ||
635 sc->sc_ifp->if_capenable != ena ||
636 sc->sc_ifp->if_hwassist != hwa ||
637 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
638 sc->sc_ifp->if_capabilities = cap;
639 sc->sc_ifp->if_capenable = ena;
640 sc->sc_ifp->if_hwassist = hwa;
641 getmicrotime(&sc->sc_ifp->if_lastchange);
643 if (sc->sc_ifflags & IFF_DEBUG)
644 if_printf(sc->sc_ifp,
645 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
650 * Enqueue interface lladdr notification.
651 * If request is already queued, it is updated.
652 * If setting lladdr is also desired, @do_change has to be set to 1.
654 * Set noinline to be dtrace-friendly
656 static __noinline void
657 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr, lagg_llqtype llq_type)
659 struct lagg_softc *sc = lp->lp_softc;
660 struct ifnet *ifp = lp->lp_ifp;
661 struct lagg_llq *llq;
663 LAGG_WLOCK_ASSERT(sc);
666 * Do not enqueue requests where lladdr is the same for
667 * "physical" interfaces (e.g. ports in lagg)
669 if (llq_type == LAGG_LLQTYPE_PHYS &&
670 memcmp(IF_LLADDR(ifp), lladdr, ETHER_ADDR_LEN) == 0)
673 /* Check to make sure its not already queued to be changed */
674 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
675 if (llq->llq_ifp == ifp) {
676 /* Update lladdr, it may have changed */
677 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
682 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT | M_ZERO);
683 if (llq == NULL) /* XXX what to do */
687 llq->llq_type = llq_type;
688 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
689 /* XXX: We should insert to tail */
690 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
692 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
696 * Set the interface MAC address from a taskqueue to avoid a LOR.
698 * Set noinline to be dtrace-friendly
700 static __noinline void
701 lagg_port_setlladdr(void *arg, int pending)
703 struct lagg_softc *sc = (struct lagg_softc *)arg;
704 struct lagg_llq *llq, *head;
707 /* Grab a local reference of the queue and remove it from the softc */
709 head = SLIST_FIRST(&sc->sc_llq_head);
710 SLIST_FIRST(&sc->sc_llq_head) = NULL;
714 * Traverse the queue and set the lladdr on each ifp. It is safe to do
715 * unlocked as we have the only reference to it.
717 for (llq = head; llq != NULL; llq = head) {
720 CURVNET_SET(ifp->if_vnet);
723 * Set the link layer address on the laggport interface.
724 * Note that if_setlladdr() or iflladdr_event handler
725 * may result in arp transmission / lltable updates.
727 if (llq->llq_type == LAGG_LLQTYPE_PHYS)
728 if_setlladdr(ifp, llq->llq_lladdr,
731 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
733 head = SLIST_NEXT(llq, llq_entries);
739 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
741 struct lagg_softc *sc_ptr;
742 struct lagg_port *lp, *tlp;
746 LAGG_WLOCK_ASSERT(sc);
748 /* Limit the maximal number of lagg ports */
749 if (sc->sc_count >= LAGG_MAX_PORTS)
752 /* Check if port has already been associated to a lagg */
753 if (ifp->if_lagg != NULL) {
754 /* Port is already in the current lagg? */
755 lp = (struct lagg_port *)ifp->if_lagg;
756 if (lp->lp_softc == sc)
761 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
762 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
763 return (EPROTONOSUPPORT);
765 /* Allow the first Ethernet member to define the MTU */
766 if (SLIST_EMPTY(&sc->sc_ports))
767 sc->sc_ifp->if_mtu = ifp->if_mtu;
768 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
769 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
774 if ((lp = malloc(sizeof(struct lagg_port),
775 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
778 /* Check if port is a stacked lagg */
780 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
781 if (ifp == sc_ptr->sc_ifp) {
785 /* XXX disable stacking for the moment, its untested */
786 #ifdef LAGG_PORT_STACKING
787 lp->lp_flags |= LAGG_PORT_STACK;
788 if (lagg_port_checkstacking(sc_ptr) >=
799 /* Change the interface type */
800 lp->lp_iftype = ifp->if_type;
801 ifp->if_type = IFT_IEEE8023ADLAG;
803 lp->lp_ioctl = ifp->if_ioctl;
804 ifp->if_ioctl = lagg_port_ioctl;
805 lp->lp_output = ifp->if_output;
806 ifp->if_output = lagg_port_output;
811 /* Save port link layer address */
812 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
814 if (SLIST_EMPTY(&sc->sc_ports)) {
816 /* First port in lagg. Update/notify lagg lladdress */
817 lagg_lladdr(sc, IF_LLADDR(ifp));
821 * Update link layer address for this port and
822 * send notifications to other subsystems.
824 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp), LAGG_LLQTYPE_PHYS);
828 * Insert into the list of ports.
829 * Keep ports sorted by if_index. It is handy, when configuration
830 * is predictable and `ifconfig laggN create ...` command
831 * will lead to the same result each time.
833 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
834 if (tlp->lp_ifp->if_index < ifp->if_index && (
835 SLIST_NEXT(tlp, lp_entries) == NULL ||
836 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
841 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
843 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
846 /* Update lagg capabilities */
847 lagg_capabilities(sc);
850 /* Read port counters */
851 pval = lp->port_counters.val;
852 for (i = 0; i < IFCOUNTERS; i++, pval++)
853 *pval = ifp->if_get_counter(ifp, i);
854 /* Add multicast addresses and interface flags to this port */
855 lagg_ether_cmdmulti(lp, 1);
856 lagg_setflags(lp, 1);
858 if ((error = lagg_proto_addport(sc, lp)) != 0) {
859 /* Remove the port, without calling pr_delport. */
860 lagg_port_destroy(lp, 0);
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_WLOCK_ASSERT(sc);
877 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
878 if (lp->lp_flags & LAGG_PORT_STACK) {
879 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
880 m = MAX(m, lagg_port_checkstacking(sc_ptr));
889 lagg_port_destroy(struct lagg_port *lp, int rundelport)
891 struct lagg_softc *sc = lp->lp_softc;
892 struct lagg_port *lp_ptr, *lp0;
893 struct lagg_llq *llq;
894 struct ifnet *ifp = lp->lp_ifp;
895 uint64_t *pval, vdiff;
898 LAGG_WLOCK_ASSERT(sc);
901 lagg_proto_delport(sc, lp);
904 * Remove multicast addresses and interface flags from this port and
905 * reset the MAC address, skip if the interface is being detached.
907 if (lp->lp_detaching == 0) {
908 lagg_ether_cmdmulti(lp, 0);
909 lagg_setflags(lp, 0);
910 lagg_port_lladdr(lp, lp->lp_lladdr, LAGG_LLQTYPE_PHYS);
913 /* Restore interface */
914 ifp->if_type = lp->lp_iftype;
915 ifp->if_ioctl = lp->lp_ioctl;
916 ifp->if_output = lp->lp_output;
919 /* Update detached port counters */
920 pval = lp->port_counters.val;
921 for (i = 0; i < IFCOUNTERS; i++, pval++) {
922 vdiff = ifp->if_get_counter(ifp, i) - *pval;
923 sc->detached_counters.val[i] += vdiff;
926 /* Finally, remove the port from the lagg */
927 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
930 /* Update the primary interface */
931 if (lp == sc->sc_primary) {
932 uint8_t lladdr[ETHER_ADDR_LEN];
934 if ((lp0 = SLIST_FIRST(&sc->sc_ports)) == NULL) {
935 bzero(&lladdr, ETHER_ADDR_LEN);
937 bcopy(lp0->lp_lladdr,
938 lladdr, ETHER_ADDR_LEN);
940 if (lp->lp_detaching != LAGG_CLONE_DESTROY)
941 lagg_lladdr(sc, lladdr);
943 /* Mark lp0 as new primary */
944 sc->sc_primary = lp0;
947 * Enqueue lladdr update/notification for each port
948 * (new primary needs update as well, to switch from
949 * old lladdr to its 'real' one).
951 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
952 lagg_port_lladdr(lp_ptr, lladdr, LAGG_LLQTYPE_PHYS);
955 /* Remove any pending lladdr changes from the queue */
956 if (lp->lp_detaching != 0) {
957 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
958 if (llq->llq_ifp == ifp) {
959 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
962 break; /* Only appears once */
968 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
972 /* Update lagg capabilities */
973 lagg_capabilities(sc);
980 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
982 struct lagg_reqport *rp = (struct lagg_reqport *)data;
983 struct lagg_softc *sc;
984 struct lagg_port *lp = NULL;
986 struct rm_priotracker tracker;
988 /* Should be checked by the caller */
989 if (ifp->if_type != IFT_IEEE8023ADLAG ||
990 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
995 if (rp->rp_portname[0] == '\0' ||
996 ifunit(rp->rp_portname) != ifp) {
1001 LAGG_RLOCK(sc, &tracker);
1002 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
1004 LAGG_RUNLOCK(sc, &tracker);
1008 lagg_port2req(lp, rp);
1009 LAGG_RUNLOCK(sc, &tracker);
1013 if (lp->lp_ioctl == NULL) {
1017 error = (*lp->lp_ioctl)(ifp, cmd, data);
1021 /* Update lagg interface capabilities */
1023 lagg_capabilities(sc);
1028 /* Do not allow the MTU to be changed once joined */
1039 if (lp != NULL && lp->lp_ioctl != NULL)
1040 return ((*lp->lp_ioctl)(ifp, cmd, data));
1046 * Requests counter @cnt data.
1048 * Counter value is calculated the following way:
1049 * 1) for each port, sum difference between current and "initial" measurements.
1050 * 2) add lagg logical interface counters.
1051 * 3) add data from detached_counters array.
1053 * We also do the following things on ports attach/detach:
1054 * 1) On port attach we store all counters it has into port_counter array.
1055 * 2) On port detach we add the different between "initial" and
1056 * current counters data to detached_counters array.
1059 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1061 struct lagg_softc *sc;
1062 struct lagg_port *lp;
1063 struct ifnet *lpifp;
1064 struct rm_priotracker tracker;
1065 uint64_t newval, oldval, vsum;
1067 /* Revise this when we've got non-generic counters. */
1068 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1070 sc = (struct lagg_softc *)ifp->if_softc;
1071 LAGG_RLOCK(sc, &tracker);
1074 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1075 /* Saved attached value */
1076 oldval = lp->port_counters.val[cnt];
1079 newval = lpifp->if_get_counter(lpifp, cnt);
1080 /* Calculate diff and save new */
1081 vsum += newval - oldval;
1085 * Add counter data which might be added by upper
1086 * layer protocols operating on logical interface.
1088 vsum += if_get_counter_default(ifp, cnt);
1091 * Add counter data from detached ports counters
1093 vsum += sc->detached_counters.val[cnt];
1095 LAGG_RUNLOCK(sc, &tracker);
1101 * For direct output to child ports.
1104 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1105 const struct sockaddr *dst, struct route *ro)
1107 struct lagg_port *lp = ifp->if_lagg;
1109 switch (dst->sa_family) {
1110 case pseudo_AF_HDRCMPLT:
1112 return ((*lp->lp_output)(ifp, m, dst, ro));
1115 /* drop any other frames */
1121 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1123 struct lagg_port *lp;
1124 struct lagg_softc *sc;
1126 if ((lp = ifp->if_lagg) == NULL)
1128 /* If the ifnet is just being renamed, don't do anything. */
1129 if (ifp->if_flags & IFF_RENAMING)
1135 lp->lp_detaching = LAGG_PORT_DETACH;
1136 lagg_port_destroy(lp, 1);
1141 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1143 struct lagg_softc *sc = lp->lp_softc;
1145 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1146 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1147 rp->rp_prio = lp->lp_prio;
1148 rp->rp_flags = lp->lp_flags;
1149 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1151 /* Add protocol specific flags */
1152 switch (sc->sc_proto) {
1153 case LAGG_PROTO_FAILOVER:
1154 if (lp == sc->sc_primary)
1155 rp->rp_flags |= LAGG_PORT_MASTER;
1156 if (lp == lagg_link_active(sc, sc->sc_primary))
1157 rp->rp_flags |= LAGG_PORT_ACTIVE;
1160 case LAGG_PROTO_ROUNDROBIN:
1161 case LAGG_PROTO_LOADBALANCE:
1162 case LAGG_PROTO_BROADCAST:
1163 if (LAGG_PORTACTIVE(lp))
1164 rp->rp_flags |= LAGG_PORT_ACTIVE;
1167 case LAGG_PROTO_LACP:
1168 /* LACP has a different definition of active */
1169 if (lacp_isactive(lp))
1170 rp->rp_flags |= LAGG_PORT_ACTIVE;
1171 if (lacp_iscollecting(lp))
1172 rp->rp_flags |= LAGG_PORT_COLLECTING;
1173 if (lacp_isdistributing(lp))
1174 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1181 lagg_init(void *xsc)
1183 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1184 struct ifnet *ifp = sc->sc_ifp;
1185 struct lagg_port *lp;
1187 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1192 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1195 * Update the port lladdrs if needed.
1196 * This might be if_setlladdr() notification
1197 * that lladdr has been changed.
1199 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1200 lagg_port_lladdr(lp, IF_LLADDR(ifp), LAGG_LLQTYPE_PHYS);
1202 lagg_proto_init(sc);
1208 lagg_stop(struct lagg_softc *sc)
1210 struct ifnet *ifp = sc->sc_ifp;
1212 LAGG_WLOCK_ASSERT(sc);
1214 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1217 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1219 lagg_proto_stop(sc);
1223 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1225 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1226 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1227 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1228 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1229 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1230 struct ifreq *ifr = (struct ifreq *)data;
1231 struct lagg_port *lp;
1233 struct thread *td = curthread;
1235 int count, buflen, len, error = 0;
1236 struct rm_priotracker tracker;
1238 bzero(&rpbuf, sizeof(rpbuf));
1242 LAGG_RLOCK(sc, &tracker);
1244 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1246 buflen = count * sizeof(struct lagg_reqport);
1247 LAGG_RUNLOCK(sc, &tracker);
1249 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1251 LAGG_RLOCK(sc, &tracker);
1252 ra->ra_proto = sc->sc_proto;
1253 lagg_proto_request(sc, &ra->ra_psc);
1256 len = min(ra->ra_size, buflen);
1257 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1258 if (len < sizeof(rpbuf))
1261 lagg_port2req(lp, &rpbuf);
1262 memcpy(buf, &rpbuf, sizeof(rpbuf));
1264 buf += sizeof(rpbuf);
1265 len -= sizeof(rpbuf);
1267 LAGG_RUNLOCK(sc, &tracker);
1268 ra->ra_ports = count;
1269 ra->ra_size = count * sizeof(rpbuf);
1270 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1271 free(outbuf, M_TEMP);
1274 error = priv_check(td, PRIV_NET_LAGG);
1277 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1278 error = EPROTONOSUPPORT;
1283 lagg_proto_detach(sc);
1284 LAGG_UNLOCK_ASSERT(sc);
1285 lagg_proto_attach(sc, ra->ra_proto);
1288 ro->ro_opts = sc->sc_opts;
1289 if (sc->sc_proto == LAGG_PROTO_LACP) {
1290 struct lacp_softc *lsc;
1292 lsc = (struct lacp_softc *)sc->sc_psc;
1293 if (lsc->lsc_debug.lsc_tx_test != 0)
1294 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1295 if (lsc->lsc_debug.lsc_rx_test != 0)
1296 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1297 if (lsc->lsc_strict_mode != 0)
1298 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1299 if (lsc->lsc_fast_timeout != 0)
1300 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1302 ro->ro_active = sc->sc_active;
1305 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1306 ro->ro_active += LAGG_PORTACTIVE(lp);
1308 ro->ro_bkt = sc->sc_bkt;
1309 ro->ro_flapping = sc->sc_flapping;
1310 ro->ro_flowid_shift = sc->flowid_shift;
1313 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1314 if (ro->ro_bkt == 0)
1315 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1317 sc->sc_bkt = ro->ro_bkt;
1319 error = priv_check(td, PRIV_NET_LAGG);
1322 if (ro->ro_opts == 0)
1325 * Set options. LACP options are stored in sc->sc_psc,
1330 switch (ro->ro_opts) {
1331 case LAGG_OPT_USE_FLOWID:
1332 case -LAGG_OPT_USE_FLOWID:
1333 case LAGG_OPT_FLOWIDSHIFT:
1337 case LAGG_OPT_LACP_TXTEST:
1338 case -LAGG_OPT_LACP_TXTEST:
1339 case LAGG_OPT_LACP_RXTEST:
1340 case -LAGG_OPT_LACP_RXTEST:
1341 case LAGG_OPT_LACP_STRICT:
1342 case -LAGG_OPT_LACP_STRICT:
1343 case LAGG_OPT_LACP_TIMEOUT:
1344 case -LAGG_OPT_LACP_TIMEOUT:
1355 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1356 /* Invalid combination of options specified. */
1359 break; /* Return from SIOCSLAGGOPTS. */
1362 * Store new options into sc->sc_opts except for
1363 * FLOWIDSHIFT and LACP options.
1366 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1367 sc->flowid_shift = ro->ro_flowid_shift;
1368 else if (ro->ro_opts > 0)
1369 sc->sc_opts |= ro->ro_opts;
1371 sc->sc_opts &= ~ro->ro_opts;
1373 struct lacp_softc *lsc;
1374 struct lacp_port *lp;
1376 lsc = (struct lacp_softc *)sc->sc_psc;
1378 switch (ro->ro_opts) {
1379 case LAGG_OPT_LACP_TXTEST:
1380 lsc->lsc_debug.lsc_tx_test = 1;
1382 case -LAGG_OPT_LACP_TXTEST:
1383 lsc->lsc_debug.lsc_tx_test = 0;
1385 case LAGG_OPT_LACP_RXTEST:
1386 lsc->lsc_debug.lsc_rx_test = 1;
1388 case -LAGG_OPT_LACP_RXTEST:
1389 lsc->lsc_debug.lsc_rx_test = 0;
1391 case LAGG_OPT_LACP_STRICT:
1392 lsc->lsc_strict_mode = 1;
1394 case -LAGG_OPT_LACP_STRICT:
1395 lsc->lsc_strict_mode = 0;
1397 case LAGG_OPT_LACP_TIMEOUT:
1399 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1400 lp->lp_state |= LACP_STATE_TIMEOUT;
1402 lsc->lsc_fast_timeout = 1;
1404 case -LAGG_OPT_LACP_TIMEOUT:
1406 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1407 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1409 lsc->lsc_fast_timeout = 0;
1415 case SIOCGLAGGFLAGS:
1417 LAGG_RLOCK(sc, &tracker);
1418 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1419 rf->rf_flags |= LAGG_F_HASHL2;
1420 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1421 rf->rf_flags |= LAGG_F_HASHL3;
1422 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1423 rf->rf_flags |= LAGG_F_HASHL4;
1424 LAGG_RUNLOCK(sc, &tracker);
1427 error = priv_check(td, PRIV_NET_LAGG);
1430 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1436 if (rf->rf_flags & LAGG_F_HASHL2)
1437 sc->sc_flags |= MBUF_HASHFLAG_L2;
1438 if (rf->rf_flags & LAGG_F_HASHL3)
1439 sc->sc_flags |= MBUF_HASHFLAG_L3;
1440 if (rf->rf_flags & LAGG_F_HASHL4)
1441 sc->sc_flags |= MBUF_HASHFLAG_L4;
1445 if (rp->rp_portname[0] == '\0' ||
1446 (tpif = ifunit(rp->rp_portname)) == NULL) {
1451 LAGG_RLOCK(sc, &tracker);
1452 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1453 lp->lp_softc != sc) {
1455 LAGG_RUNLOCK(sc, &tracker);
1459 lagg_port2req(lp, rp);
1460 LAGG_RUNLOCK(sc, &tracker);
1463 error = priv_check(td, PRIV_NET_LAGG);
1466 if (rp->rp_portname[0] == '\0' ||
1467 (tpif = ifunit(rp->rp_portname)) == NULL) {
1473 * A laggport interface should not have inet6 address
1474 * because two interfaces with a valid link-local
1475 * scope zone must not be merged in any form. This
1476 * restriction is needed to prevent violation of
1477 * link-local scope zone. Attempts to add a laggport
1478 * interface which has inet6 addresses triggers
1479 * removal of all inet6 addresses on the member
1482 if (in6ifa_llaonifp(tpif)) {
1484 if_printf(sc->sc_ifp,
1485 "IPv6 addresses on %s have been removed "
1486 "before adding it as a member to prevent "
1487 "IPv6 address scope violation.\n",
1492 error = lagg_port_create(sc, tpif);
1495 case SIOCSLAGGDELPORT:
1496 error = priv_check(td, PRIV_NET_LAGG);
1499 if (rp->rp_portname[0] == '\0' ||
1500 (tpif = ifunit(rp->rp_portname)) == NULL) {
1506 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1507 lp->lp_softc != sc) {
1513 error = lagg_port_destroy(lp, 1);
1517 /* Set flags on ports too */
1519 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1520 lagg_setflags(lp, 1);
1524 if (!(ifp->if_flags & IFF_UP) &&
1525 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1527 * If interface is marked down and it is running,
1528 * then stop and disable it.
1533 } else if ((ifp->if_flags & IFF_UP) &&
1534 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1536 * If interface is marked up and it is stopped, then
1539 (*ifp->if_init)(sc);
1545 error = lagg_ether_setmulti(sc);
1550 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1555 /* Do not allow the MTU or caps to be directly changed */
1560 error = ether_ioctl(ifp, cmd, data);
1568 lagg_snd_tag_alloc(struct ifnet *ifp,
1569 union if_snd_tag_alloc_params *params,
1570 struct m_snd_tag **ppmt)
1572 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1573 struct lagg_port *lp;
1577 switch (sc->sc_proto) {
1578 case LAGG_PROTO_FAILOVER:
1579 lp = lagg_link_active(sc, sc->sc_primary);
1581 case LAGG_PROTO_LOADBALANCE:
1582 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1583 params->hdr.flowtype == M_HASHTYPE_NONE)
1584 return (EOPNOTSUPP);
1585 p = params->hdr.flowid >> sc->flowid_shift;
1587 lb = (struct lagg_lb *)sc->sc_psc;
1588 lp = lb->lb_ports[p];
1589 lp = lagg_link_active(sc, lp);
1591 case LAGG_PROTO_LACP:
1592 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1593 params->hdr.flowtype == M_HASHTYPE_NONE)
1594 return (EOPNOTSUPP);
1595 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1598 return (EOPNOTSUPP);
1601 return (EOPNOTSUPP);
1603 if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1604 (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1605 return (EOPNOTSUPP);
1607 /* forward allocation request */
1608 return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1613 lagg_ether_setmulti(struct lagg_softc *sc)
1615 struct lagg_port *lp;
1617 LAGG_WLOCK_ASSERT(sc);
1619 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1620 /* First, remove any existing filter entries. */
1621 lagg_ether_cmdmulti(lp, 0);
1622 /* copy all addresses from the lagg interface to the port */
1623 lagg_ether_cmdmulti(lp, 1);
1629 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1631 struct lagg_softc *sc = lp->lp_softc;
1632 struct ifnet *ifp = lp->lp_ifp;
1633 struct ifnet *scifp = sc->sc_ifp;
1635 struct ifmultiaddr *ifma;
1638 LAGG_WLOCK_ASSERT(sc);
1641 IF_ADDR_WLOCK(scifp);
1642 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1643 if (ifma->ifma_addr->sa_family != AF_LINK)
1645 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1647 IF_ADDR_WUNLOCK(scifp);
1650 bcopy(ifma->ifma_addr, &mc->mc_addr,
1651 ifma->ifma_addr->sa_len);
1652 mc->mc_addr.sdl_index = ifp->if_index;
1654 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1656 IF_ADDR_WUNLOCK(scifp);
1657 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1658 error = if_addmulti(ifp,
1659 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1664 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1665 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1666 if (mc->mc_ifma && lp->lp_detaching == 0)
1667 if_delmulti_ifma(mc->mc_ifma);
1674 /* Handle a ref counted flag that should be set on the lagg port as well */
1676 lagg_setflag(struct lagg_port *lp, int flag, int status,
1677 int (*func)(struct ifnet *, int))
1679 struct lagg_softc *sc = lp->lp_softc;
1680 struct ifnet *scifp = sc->sc_ifp;
1681 struct ifnet *ifp = lp->lp_ifp;
1684 LAGG_WLOCK_ASSERT(sc);
1686 status = status ? (scifp->if_flags & flag) : 0;
1687 /* Now "status" contains the flag value or 0 */
1690 * See if recorded ports status is different from what
1691 * we want it to be. If it is, flip it. We record ports
1692 * status in lp_ifflags so that we won't clear ports flag
1693 * we haven't set. In fact, we don't clear or set ports
1694 * flags directly, but get or release references to them.
1695 * That's why we can be sure that recorded flags still are
1696 * in accord with actual ports flags.
1698 if (status != (lp->lp_ifflags & flag)) {
1699 error = (*func)(ifp, status);
1702 lp->lp_ifflags &= ~flag;
1703 lp->lp_ifflags |= status;
1709 * Handle IFF_* flags that require certain changes on the lagg port
1710 * if "status" is true, update ports flags respective to the lagg
1711 * if "status" is false, forcedly clear the flags set on port.
1714 lagg_setflags(struct lagg_port *lp, int status)
1718 for (i = 0; lagg_pflags[i].flag; i++) {
1719 error = lagg_setflag(lp, lagg_pflags[i].flag,
1720 status, lagg_pflags[i].func);
1728 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1730 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1731 int error, len, mcast;
1732 struct rm_priotracker tracker;
1734 len = m->m_pkthdr.len;
1735 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1737 LAGG_RLOCK(sc, &tracker);
1738 /* We need a Tx algorithm and at least one port */
1739 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1740 LAGG_RUNLOCK(sc, &tracker);
1742 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1746 ETHER_BPF_MTAP(ifp, m);
1748 error = lagg_proto_start(sc, m);
1749 LAGG_RUNLOCK(sc, &tracker);
1752 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1758 * The ifp->if_qflush entry point for lagg(4) is no-op.
1761 lagg_qflush(struct ifnet *ifp __unused)
1765 static struct mbuf *
1766 lagg_input(struct ifnet *ifp, struct mbuf *m)
1768 struct lagg_port *lp = ifp->if_lagg;
1769 struct lagg_softc *sc = lp->lp_softc;
1770 struct ifnet *scifp = sc->sc_ifp;
1771 struct rm_priotracker tracker;
1773 LAGG_RLOCK(sc, &tracker);
1774 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1775 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1776 sc->sc_proto == LAGG_PROTO_NONE) {
1777 LAGG_RUNLOCK(sc, &tracker);
1782 ETHER_BPF_MTAP(scifp, m);
1784 if (lp->lp_detaching != 0) {
1788 m = lagg_proto_input(sc, lp, m);
1791 if (scifp->if_flags & IFF_MONITOR) {
1797 LAGG_RUNLOCK(sc, &tracker);
1802 lagg_media_change(struct ifnet *ifp)
1804 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1806 if (sc->sc_ifflags & IFF_DEBUG)
1807 printf("%s\n", __func__);
1814 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1816 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1817 struct lagg_port *lp;
1818 struct rm_priotracker tracker;
1820 imr->ifm_status = IFM_AVALID;
1821 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1823 LAGG_RLOCK(sc, &tracker);
1824 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1825 if (LAGG_PORTACTIVE(lp))
1826 imr->ifm_status |= IFM_ACTIVE;
1828 LAGG_RUNLOCK(sc, &tracker);
1832 lagg_linkstate(struct lagg_softc *sc)
1834 struct lagg_port *lp;
1835 int new_link = LINK_STATE_DOWN;
1838 /* Our link is considered up if at least one of our ports is active */
1839 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1840 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1841 new_link = LINK_STATE_UP;
1845 if_link_state_change(sc->sc_ifp, new_link);
1847 /* Update if_baudrate to reflect the max possible speed */
1848 switch (sc->sc_proto) {
1849 case LAGG_PROTO_FAILOVER:
1850 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1851 sc->sc_primary->lp_ifp->if_baudrate : 0;
1853 case LAGG_PROTO_ROUNDROBIN:
1854 case LAGG_PROTO_LOADBALANCE:
1855 case LAGG_PROTO_BROADCAST:
1857 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1858 speed += lp->lp_ifp->if_baudrate;
1859 sc->sc_ifp->if_baudrate = speed;
1861 case LAGG_PROTO_LACP:
1862 /* LACP updates if_baudrate itself */
1868 lagg_port_state(struct ifnet *ifp, int state)
1870 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1871 struct lagg_softc *sc = NULL;
1880 lagg_proto_linkstate(sc, lp);
1885 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1887 struct lagg_port *lp_next, *rval = NULL;
1888 // int new_link = LINK_STATE_DOWN;
1890 LAGG_RLOCK_ASSERT(sc);
1892 * Search a port which reports an active link state.
1897 if (LAGG_PORTACTIVE(lp)) {
1901 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1902 LAGG_PORTACTIVE(lp_next)) {
1908 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1909 if (LAGG_PORTACTIVE(lp_next)) {
1918 * The IEEE 802.1D standard assumes that a lagg with
1919 * multiple ports is always full duplex. This is valid
1920 * for load sharing laggs and if at least two links
1921 * are active. Unfortunately, checking the latter would
1922 * be too expensive at this point.
1924 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1926 new_link = LINK_STATE_FULL_DUPLEX;
1928 new_link = rval->lp_link_state;
1936 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1939 return (ifp->if_transmit)(ifp, m);
1943 * Simple round robin aggregation
1946 lagg_rr_attach(struct lagg_softc *sc)
1948 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1950 sc->sc_bkt_count = sc->sc_bkt;
1954 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1956 struct lagg_port *lp;
1959 if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1960 sc->sc_bkt_count = sc->sc_bkt;
1962 if (sc->sc_bkt > 0) {
1963 atomic_subtract_int(&sc->sc_bkt_count, 1);
1964 if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1965 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1969 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1972 lp = SLIST_FIRST(&sc->sc_ports);
1975 lp = SLIST_NEXT(lp, lp_entries);
1978 * Check the port's link state. This will return the next active
1979 * port if the link is down or the port is NULL.
1981 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1987 return (lagg_enqueue(lp->lp_ifp, m));
1990 static struct mbuf *
1991 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1993 struct ifnet *ifp = sc->sc_ifp;
1995 /* Just pass in the packet to our lagg device */
1996 m->m_pkthdr.rcvif = ifp;
2005 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2007 int active_ports = 0;
2010 struct lagg_port *lp, *last = NULL;
2013 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2014 if (!LAGG_PORTACTIVE(lp))
2020 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2027 ret = lagg_enqueue(last->lp_ifp, m0);
2037 if ((last = lagg_link_active(sc, last)) == NULL) {
2042 ret = lagg_enqueue(last->lp_ifp, m);
2053 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2055 struct ifnet *ifp = sc->sc_ifp;
2057 /* Just pass in the packet to our lagg device */
2058 m->m_pkthdr.rcvif = ifp;
2066 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2068 struct lagg_port *lp;
2070 /* Use the master port if active or the next available port */
2071 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2077 return (lagg_enqueue(lp->lp_ifp, m));
2080 static struct mbuf *
2081 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2083 struct ifnet *ifp = sc->sc_ifp;
2084 struct lagg_port *tmp_tp;
2086 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2087 m->m_pkthdr.rcvif = ifp;
2091 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2092 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2094 * If tmp_tp is null, we've received a packet when all
2095 * our links are down. Weird, but process it anyways.
2097 if ((tmp_tp == NULL || tmp_tp == lp)) {
2098 m->m_pkthdr.rcvif = ifp;
2111 lagg_lb_attach(struct lagg_softc *sc)
2113 struct lagg_port *lp;
2116 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2118 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2120 lb->lb_key = m_ether_tcpip_hash_init();
2123 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2124 lagg_lb_port_create(lp);
2128 lagg_lb_detach(struct lagg_softc *sc)
2132 lb = (struct lagg_lb *)sc->sc_psc;
2139 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2141 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2142 struct lagg_port *lp_next;
2145 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2146 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2149 if (i >= LAGG_MAX_PORTS)
2151 if (sc->sc_ifflags & IFF_DEBUG)
2152 printf("%s: port %s at index %d\n",
2153 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2154 lb->lb_ports[i++] = lp_next;
2161 lagg_lb_port_create(struct lagg_port *lp)
2163 struct lagg_softc *sc = lp->lp_softc;
2164 return (lagg_lb_porttable(sc, NULL));
2168 lagg_lb_port_destroy(struct lagg_port *lp)
2170 struct lagg_softc *sc = lp->lp_softc;
2171 lagg_lb_porttable(sc, lp);
2175 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2177 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2178 struct lagg_port *lp = NULL;
2181 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2182 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2183 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2185 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2187 lp = lb->lb_ports[p];
2190 * Check the port's link state. This will return the next active
2191 * port if the link is down or the port is NULL.
2193 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2199 return (lagg_enqueue(lp->lp_ifp, m));
2202 static struct mbuf *
2203 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2205 struct ifnet *ifp = sc->sc_ifp;
2207 /* Just pass in the packet to our lagg device */
2208 m->m_pkthdr.rcvif = ifp;
2217 lagg_lacp_attach(struct lagg_softc *sc)
2219 struct lagg_port *lp;
2222 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2223 lacp_port_create(lp);
2227 lagg_lacp_detach(struct lagg_softc *sc)
2229 struct lagg_port *lp;
2232 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2233 lacp_port_destroy(lp);
2243 lagg_lacp_lladdr(struct lagg_softc *sc)
2245 struct lagg_port *lp;
2247 /* purge all the lacp ports */
2248 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2249 lacp_port_destroy(lp);
2251 /* add them back in */
2252 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2253 lacp_port_create(lp);
2257 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2259 struct lagg_port *lp;
2261 lp = lacp_select_tx_port(sc, m);
2268 return (lagg_enqueue(lp->lp_ifp, m));
2271 static struct mbuf *
2272 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2274 struct ifnet *ifp = sc->sc_ifp;
2275 struct ether_header *eh;
2278 eh = mtod(m, struct ether_header *);
2279 etype = ntohs(eh->ether_type);
2281 /* Tap off LACP control messages */
2282 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2283 m = lacp_input(lp, m);
2289 * If the port is not collecting or not in the active aggregator then
2292 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2297 m->m_pkthdr.rcvif = ifp;