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 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"
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
37 #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 *);
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);
122 static int lagg_ether_setmulti(struct lagg_softc *);
123 static int lagg_ether_cmdmulti(struct lagg_port *, int);
124 static int lagg_setflag(struct lagg_port *, int, int,
125 int (*func)(struct ifnet *, int));
126 static int lagg_setflags(struct lagg_port *, int status);
127 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
128 static int lagg_transmit(struct ifnet *, struct mbuf *);
129 static void lagg_qflush(struct ifnet *);
130 static int lagg_media_change(struct ifnet *);
131 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
132 static struct lagg_port *lagg_link_active(struct lagg_softc *,
134 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
136 /* Simple round robin */
137 static void lagg_rr_attach(struct lagg_softc *);
138 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
139 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
142 /* Active failover */
143 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
144 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
148 static void lagg_lb_attach(struct lagg_softc *);
149 static void lagg_lb_detach(struct lagg_softc *);
150 static int lagg_lb_port_create(struct lagg_port *);
151 static void lagg_lb_port_destroy(struct lagg_port *);
152 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
153 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
155 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
158 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
159 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
163 static void lagg_lacp_attach(struct lagg_softc *);
164 static void lagg_lacp_detach(struct lagg_softc *);
165 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
166 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
168 static void lagg_lacp_lladdr(struct lagg_softc *);
170 /* lagg protocol table */
171 static const struct lagg_proto {
173 void (*pr_attach)(struct lagg_softc *);
174 void (*pr_detach)(struct lagg_softc *);
175 int (*pr_start)(struct lagg_softc *, struct mbuf *);
176 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
178 int (*pr_addport)(struct lagg_port *);
179 void (*pr_delport)(struct lagg_port *);
180 void (*pr_linkstate)(struct lagg_port *);
181 void (*pr_init)(struct lagg_softc *);
182 void (*pr_stop)(struct lagg_softc *);
183 void (*pr_lladdr)(struct lagg_softc *);
184 void (*pr_request)(struct lagg_softc *, void *);
185 void (*pr_portreq)(struct lagg_port *, void *);
188 .pr_num = LAGG_PROTO_NONE
191 .pr_num = LAGG_PROTO_ROUNDROBIN,
192 .pr_attach = lagg_rr_attach,
193 .pr_start = lagg_rr_start,
194 .pr_input = lagg_rr_input,
197 .pr_num = LAGG_PROTO_FAILOVER,
198 .pr_start = lagg_fail_start,
199 .pr_input = lagg_fail_input,
202 .pr_num = LAGG_PROTO_LOADBALANCE,
203 .pr_attach = lagg_lb_attach,
204 .pr_detach = lagg_lb_detach,
205 .pr_start = lagg_lb_start,
206 .pr_input = lagg_lb_input,
207 .pr_addport = lagg_lb_port_create,
208 .pr_delport = lagg_lb_port_destroy,
211 .pr_num = LAGG_PROTO_LACP,
212 .pr_attach = lagg_lacp_attach,
213 .pr_detach = lagg_lacp_detach,
214 .pr_start = lagg_lacp_start,
215 .pr_input = lagg_lacp_input,
216 .pr_addport = lacp_port_create,
217 .pr_delport = lacp_port_destroy,
218 .pr_linkstate = lacp_linkstate,
219 .pr_init = lacp_init,
220 .pr_stop = lacp_stop,
221 .pr_lladdr = lagg_lacp_lladdr,
222 .pr_request = lacp_req,
223 .pr_portreq = lacp_portreq,
226 .pr_num = LAGG_PROTO_ETHERCHANNEL,
227 .pr_attach = lagg_lb_attach,
228 .pr_detach = lagg_lb_detach,
229 .pr_start = lagg_lb_start,
230 .pr_input = lagg_lb_input,
233 .pr_num = LAGG_PROTO_BROADCAST,
234 .pr_start = lagg_bcast_start,
235 .pr_input = lagg_bcast_input,
239 SYSCTL_DECL(_net_link);
240 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
243 /* Allow input on any failover links */
244 static VNET_DEFINE(int, lagg_failover_rx_all);
245 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
246 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
247 &VNET_NAME(lagg_failover_rx_all), 0,
248 "Accept input from any interface in a failover lagg");
250 /* Default value for using M_FLOWID */
251 static VNET_DEFINE(int, def_use_flowid) = 1;
252 #define V_def_use_flowid VNET(def_use_flowid)
253 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
254 &VNET_NAME(def_use_flowid), 0,
255 "Default setting for using flow id for load sharing");
257 /* Default value for using M_FLOWID */
258 static VNET_DEFINE(int, def_flowid_shift) = 16;
259 #define V_def_flowid_shift VNET(def_flowid_shift)
260 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
261 &VNET_NAME(def_flowid_shift), 0,
262 "Default setting for flowid shift for load sharing");
265 vnet_lagg_init(const void *unused __unused)
268 LAGG_LIST_LOCK_INIT();
269 SLIST_INIT(&V_lagg_list);
270 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
271 lagg_clone_destroy, 0);
273 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
274 vnet_lagg_init, NULL);
277 vnet_lagg_uninit(const void *unused __unused)
280 if_clone_detach(V_lagg_cloner);
281 LAGG_LIST_LOCK_DESTROY();
283 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
284 vnet_lagg_uninit, NULL);
287 lagg_modevent(module_t mod, int type, void *data)
292 lagg_input_p = lagg_input;
293 lagg_linkstate_p = lagg_port_state;
294 lagg_detach_cookie = EVENTHANDLER_REGISTER(
295 ifnet_departure_event, lagg_port_ifdetach, NULL,
296 EVENTHANDLER_PRI_ANY);
299 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
302 lagg_linkstate_p = NULL;
310 static moduledata_t lagg_mod = {
316 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
317 MODULE_VERSION(if_lagg, 1);
320 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
323 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
326 if (sc->sc_ifflags & IFF_DEBUG)
327 if_printf(sc->sc_ifp, "using proto %u\n", pr);
329 if (lagg_protos[pr].pr_attach != NULL)
330 lagg_protos[pr].pr_attach(sc);
335 lagg_proto_detach(struct lagg_softc *sc)
339 LAGG_WLOCK_ASSERT(sc);
342 sc->sc_proto = LAGG_PROTO_NONE;
344 if (lagg_protos[pr].pr_detach != NULL)
345 lagg_protos[pr].pr_detach(sc);
351 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
354 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
358 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
361 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
365 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
368 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
371 return (lagg_protos[sc->sc_proto].pr_addport(lp));
375 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
378 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
379 lagg_protos[sc->sc_proto].pr_delport(lp);
383 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
386 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
387 lagg_protos[sc->sc_proto].pr_linkstate(lp);
391 lagg_proto_init(struct lagg_softc *sc)
394 if (lagg_protos[sc->sc_proto].pr_init != NULL)
395 lagg_protos[sc->sc_proto].pr_init(sc);
399 lagg_proto_stop(struct lagg_softc *sc)
402 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
403 lagg_protos[sc->sc_proto].pr_stop(sc);
407 lagg_proto_lladdr(struct lagg_softc *sc)
410 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
411 lagg_protos[sc->sc_proto].pr_lladdr(sc);
415 lagg_proto_request(struct lagg_softc *sc, void *v)
418 if (lagg_protos[sc->sc_proto].pr_request != NULL)
419 lagg_protos[sc->sc_proto].pr_request(sc, v);
423 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
426 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
427 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
431 * This routine is run via an vlan
435 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
437 struct lagg_softc *sc = ifp->if_softc;
438 struct lagg_port *lp;
439 struct rm_priotracker tracker;
441 if (ifp->if_softc != arg) /* Not our event */
444 LAGG_RLOCK(sc, &tracker);
445 if (!SLIST_EMPTY(&sc->sc_ports)) {
446 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
447 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
449 LAGG_RUNLOCK(sc, &tracker);
453 * This routine is run via an vlan
457 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
459 struct lagg_softc *sc = ifp->if_softc;
460 struct lagg_port *lp;
461 struct rm_priotracker tracker;
463 if (ifp->if_softc != arg) /* Not our event */
466 LAGG_RLOCK(sc, &tracker);
467 if (!SLIST_EMPTY(&sc->sc_ports)) {
468 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
469 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
471 LAGG_RUNLOCK(sc, &tracker);
475 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
477 struct lagg_softc *sc;
479 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
481 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
482 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
488 if (V_def_use_flowid)
489 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
490 sc->flowid_shift = V_def_flowid_shift;
492 /* Hash all layers by default */
493 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
495 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
498 SLIST_INIT(&sc->sc_ports);
499 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
501 /* Initialise pseudo media types */
502 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
504 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
505 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
507 if_initname(ifp, laggname, unit);
509 ifp->if_transmit = lagg_transmit;
510 ifp->if_qflush = lagg_qflush;
511 ifp->if_init = lagg_init;
512 ifp->if_ioctl = lagg_ioctl;
513 ifp->if_get_counter = lagg_get_counter;
514 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
515 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
518 * Attach as an ordinary ethernet device, children will be attached
519 * as special device IFT_IEEE8023ADLAG.
521 ether_ifattach(ifp, eaddr);
523 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
524 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
525 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
526 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
528 /* Insert into the global list of laggs */
530 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
537 lagg_clone_destroy(struct ifnet *ifp)
539 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
540 struct lagg_port *lp;
545 ifp->if_flags &= ~IFF_UP;
547 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
548 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
550 /* Shutdown and remove lagg ports */
551 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
552 lagg_port_destroy(lp, 1);
553 /* Unhook the aggregation protocol */
554 lagg_proto_detach(sc);
556 ifmedia_removeall(&sc->sc_media);
561 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
564 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
565 LAGG_LOCK_DESTROY(sc);
570 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
574 LAGG_WLOCK_ASSERT(sc);
576 bzero(&lp, sizeof(lp));
577 lp.lp_ifp = sc->sc_ifp;
580 lagg_port_lladdr(&lp, lladdr);
584 lagg_capabilities(struct lagg_softc *sc)
586 struct lagg_port *lp;
587 int cap = ~0, ena = ~0;
589 struct ifnet_hw_tsomax hw_tsomax;
591 LAGG_WLOCK_ASSERT(sc);
593 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
595 /* Get capabilities from the lagg ports */
596 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
597 cap &= lp->lp_ifp->if_capabilities;
598 ena &= lp->lp_ifp->if_capenable;
599 hwa &= lp->lp_ifp->if_hwassist;
600 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
602 cap = (cap == ~0 ? 0 : cap);
603 ena = (ena == ~0 ? 0 : ena);
604 hwa = (hwa == ~0 ? 0 : hwa);
606 if (sc->sc_ifp->if_capabilities != cap ||
607 sc->sc_ifp->if_capenable != ena ||
608 sc->sc_ifp->if_hwassist != hwa ||
609 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
610 sc->sc_ifp->if_capabilities = cap;
611 sc->sc_ifp->if_capenable = ena;
612 sc->sc_ifp->if_hwassist = hwa;
613 getmicrotime(&sc->sc_ifp->if_lastchange);
615 if (sc->sc_ifflags & IFF_DEBUG)
616 if_printf(sc->sc_ifp,
617 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
622 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
624 struct lagg_softc *sc = lp->lp_softc;
625 struct ifnet *ifp = lp->lp_ifp;
626 struct lagg_llq *llq;
629 LAGG_WLOCK_ASSERT(sc);
631 if (lp->lp_detaching ||
632 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
635 /* Check to make sure its not already queued to be changed */
636 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
637 if (llq->llq_ifp == ifp) {
644 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
645 if (llq == NULL) /* XXX what to do */
649 /* Update the lladdr even if pending, it may have changed */
651 llq->llq_primary = (sc->sc_primary->lp_ifp == ifp) ? 1 : 0;
652 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
655 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
657 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
661 * Set the interface MAC address from a taskqueue to avoid a LOR.
664 lagg_port_setlladdr(void *arg, int pending)
666 struct lagg_softc *sc = (struct lagg_softc *)arg;
667 struct lagg_llq *llq, *head;
671 /* Grab a local reference of the queue and remove it from the softc */
673 head = SLIST_FIRST(&sc->sc_llq_head);
674 SLIST_FIRST(&sc->sc_llq_head) = NULL;
678 * Traverse the queue and set the lladdr on each ifp. It is safe to do
679 * unlocked as we have the only reference to it.
681 for (llq = head; llq != NULL; llq = head) {
684 CURVNET_SET(ifp->if_vnet);
685 if (llq->llq_primary == 0) {
687 * Set the link layer address on the laggport interface.
688 * if_setlladdr() triggers gratuitous ARPs for INET.
690 error = if_setlladdr(ifp, llq->llq_lladdr,
693 printf("%s: setlladdr failed on %s\n", __func__,
697 * Set the link layer address on the lagg interface.
698 * lagg_proto_lladdr() notifies the MAC change to
699 * the aggregation protocol. iflladdr_event handler
700 * may trigger gratuitous ARPs for INET.
702 if (memcmp(llq->llq_lladdr, IF_LLADDR(ifp),
703 ETHER_ADDR_LEN) != 0) {
704 bcopy(llq->llq_lladdr, IF_LLADDR(ifp),
707 lagg_proto_lladdr(sc);
709 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
713 head = SLIST_NEXT(llq, llq_entries);
719 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
721 struct lagg_softc *sc_ptr;
722 struct lagg_port *lp, *tlp;
726 LAGG_WLOCK_ASSERT(sc);
728 /* Limit the maximal number of lagg ports */
729 if (sc->sc_count >= LAGG_MAX_PORTS)
732 /* Check if port has already been associated to a lagg */
733 if (ifp->if_lagg != NULL) {
734 /* Port is already in the current lagg? */
735 lp = (struct lagg_port *)ifp->if_lagg;
736 if (lp->lp_softc == sc)
741 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
742 if (ifp->if_type != IFT_ETHER)
743 return (EPROTONOSUPPORT);
747 * The member interface should not have inet6 address because
748 * two interfaces with a valid link-local scope zone must not be
749 * merged in any form. This restriction is needed to
750 * prevent violation of link-local scope zone. Attempts to
751 * add a member interface which has inet6 addresses triggers
752 * removal of all inet6 addresses on the member interface.
754 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
755 if (in6ifa_llaonifp(lp->lp_ifp)) {
756 in6_ifdetach(lp->lp_ifp);
757 if_printf(sc->sc_ifp,
758 "IPv6 addresses on %s have been removed "
759 "before adding it as a member to prevent "
760 "IPv6 address scope violation.\n",
761 lp->lp_ifp->if_xname);
764 if (in6ifa_llaonifp(ifp)) {
766 if_printf(sc->sc_ifp,
767 "IPv6 addresses on %s have been removed "
768 "before adding it as a member to prevent "
769 "IPv6 address scope violation.\n",
773 /* Allow the first Ethernet member to define the MTU */
774 if (SLIST_EMPTY(&sc->sc_ports))
775 sc->sc_ifp->if_mtu = ifp->if_mtu;
776 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
777 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
782 if ((lp = malloc(sizeof(struct lagg_port),
783 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
786 /* Check if port is a stacked lagg */
788 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
789 if (ifp == sc_ptr->sc_ifp) {
793 /* XXX disable stacking for the moment, its untested */
794 #ifdef LAGG_PORT_STACKING
795 lp->lp_flags |= LAGG_PORT_STACK;
796 if (lagg_port_checkstacking(sc_ptr) >=
807 /* Change the interface type */
808 lp->lp_iftype = ifp->if_type;
809 ifp->if_type = IFT_IEEE8023ADLAG;
811 lp->lp_ioctl = ifp->if_ioctl;
812 ifp->if_ioctl = lagg_port_ioctl;
813 lp->lp_output = ifp->if_output;
814 ifp->if_output = lagg_port_output;
819 /* Save port link layer address */
820 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
822 if (SLIST_EMPTY(&sc->sc_ports)) {
824 lagg_lladdr(sc, IF_LLADDR(ifp));
826 /* Update link layer address for this port */
827 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
830 /* Insert into the list of ports. Keep ports sorted by if_index. */
831 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
832 if (tlp->lp_ifp->if_index < ifp->if_index && (
833 SLIST_NEXT(tlp, lp_entries) == NULL ||
834 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index <
839 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
841 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
844 /* Update lagg capabilities */
845 lagg_capabilities(sc);
848 /* Read port counters */
849 pval = lp->port_counters.val;
850 for (i = 0; i < IFCOUNTERS; i++, pval++)
851 *pval = ifp->if_get_counter(ifp, i);
852 /* Add multicast addresses and interface flags to this port */
853 lagg_ether_cmdmulti(lp, 1);
854 lagg_setflags(lp, 1);
856 if ((error = lagg_proto_addport(sc, lp)) != 0) {
857 /* Remove the port, without calling pr_delport. */
858 lagg_port_destroy(lp, 0);
865 #ifdef LAGG_PORT_STACKING
867 lagg_port_checkstacking(struct lagg_softc *sc)
869 struct lagg_softc *sc_ptr;
870 struct lagg_port *lp;
873 LAGG_WLOCK_ASSERT(sc);
875 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
876 if (lp->lp_flags & LAGG_PORT_STACK) {
877 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
878 m = MAX(m, lagg_port_checkstacking(sc_ptr));
887 lagg_port_destroy(struct lagg_port *lp, int rundelport)
889 struct lagg_softc *sc = lp->lp_softc;
890 struct lagg_port *lp_ptr;
891 struct lagg_llq *llq;
892 struct ifnet *ifp = lp->lp_ifp;
893 uint64_t *pval, vdiff;
896 LAGG_WLOCK_ASSERT(sc);
899 lagg_proto_delport(sc, lp);
902 * Remove multicast addresses and interface flags from this port and
903 * reset the MAC address, skip if the interface is being detached.
905 if (!lp->lp_detaching) {
906 lagg_ether_cmdmulti(lp, 0);
907 lagg_setflags(lp, 0);
908 lagg_port_lladdr(lp, lp->lp_lladdr);
911 /* Restore interface */
912 ifp->if_type = lp->lp_iftype;
913 ifp->if_ioctl = lp->lp_ioctl;
914 ifp->if_output = lp->lp_output;
917 /* Update detached port counters */
918 pval = lp->port_counters.val;
919 for (i = 0; i < IFCOUNTERS; i++, pval++) {
920 vdiff = ifp->if_get_counter(ifp, i) - *pval;
921 sc->detached_counters.val[i] += vdiff;
924 /* Finally, remove the port from the lagg */
925 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
928 /* Update the primary interface */
929 if (lp == sc->sc_primary) {
930 uint8_t lladdr[ETHER_ADDR_LEN];
932 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
933 bzero(&lladdr, ETHER_ADDR_LEN);
935 bcopy(lp_ptr->lp_lladdr,
936 lladdr, ETHER_ADDR_LEN);
938 lagg_lladdr(sc, lladdr);
939 sc->sc_primary = lp_ptr;
941 /* Update link layer address for each port */
942 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
943 lagg_port_lladdr(lp_ptr, lladdr);
946 /* Remove any pending lladdr changes from the queue */
947 if (lp->lp_detaching) {
948 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
949 if (llq->llq_ifp == ifp) {
950 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
953 break; /* Only appears once */
959 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
963 /* Update lagg capabilities */
964 lagg_capabilities(sc);
971 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
973 struct lagg_reqport *rp = (struct lagg_reqport *)data;
974 struct lagg_softc *sc;
975 struct lagg_port *lp = NULL;
977 struct rm_priotracker tracker;
979 /* Should be checked by the caller */
980 if (ifp->if_type != IFT_IEEE8023ADLAG ||
981 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
986 if (rp->rp_portname[0] == '\0' ||
987 ifunit(rp->rp_portname) != ifp) {
992 LAGG_RLOCK(sc, &tracker);
993 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
995 LAGG_RUNLOCK(sc, &tracker);
999 lagg_port2req(lp, rp);
1000 LAGG_RUNLOCK(sc, &tracker);
1004 if (lp->lp_ioctl == NULL) {
1008 error = (*lp->lp_ioctl)(ifp, cmd, data);
1012 /* Update lagg interface capabilities */
1014 lagg_capabilities(sc);
1019 /* Do not allow the MTU to be changed once joined */
1030 if (lp->lp_ioctl != NULL)
1031 return ((*lp->lp_ioctl)(ifp, cmd, data));
1037 * Requests counter @cnt data.
1039 * Counter value is calculated the following way:
1040 * 1) for each port, sum difference between current and "initial" measurements.
1041 * 2) add lagg logical interface counters.
1042 * 3) add data from detached_counters array.
1044 * We also do the following things on ports attach/detach:
1045 * 1) On port attach we store all counters it has into port_counter array.
1046 * 2) On port detach we add the different between "initial" and
1047 * current counters data to detached_counters array.
1050 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1052 struct lagg_softc *sc;
1053 struct lagg_port *lp;
1054 struct ifnet *lpifp;
1055 struct rm_priotracker tracker;
1056 uint64_t newval, oldval, vsum;
1058 /* Revise this when we've got non-generic counters. */
1059 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1061 sc = (struct lagg_softc *)ifp->if_softc;
1062 LAGG_RLOCK(sc, &tracker);
1065 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1066 /* Saved attached value */
1067 oldval = lp->port_counters.val[cnt];
1070 newval = lpifp->if_get_counter(lpifp, cnt);
1071 /* Calculate diff and save new */
1072 vsum += newval - oldval;
1076 * Add counter data which might be added by upper
1077 * layer protocols operating on logical interface.
1079 vsum += if_get_counter_default(ifp, cnt);
1082 * Add counter data from detached ports counters
1084 vsum += sc->detached_counters.val[cnt];
1086 LAGG_RUNLOCK(sc, &tracker);
1092 * For direct output to child ports.
1095 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1096 const struct sockaddr *dst, struct route *ro)
1098 struct lagg_port *lp = ifp->if_lagg;
1100 switch (dst->sa_family) {
1101 case pseudo_AF_HDRCMPLT:
1103 return ((*lp->lp_output)(ifp, m, dst, ro));
1106 /* drop any other frames */
1112 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1114 struct lagg_port *lp;
1115 struct lagg_softc *sc;
1117 if ((lp = ifp->if_lagg) == NULL)
1119 /* If the ifnet is just being renamed, don't do anything. */
1120 if (ifp->if_flags & IFF_RENAMING)
1126 lp->lp_detaching = 1;
1127 lagg_port_destroy(lp, 1);
1132 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1134 struct lagg_softc *sc = lp->lp_softc;
1136 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1137 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1138 rp->rp_prio = lp->lp_prio;
1139 rp->rp_flags = lp->lp_flags;
1140 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1142 /* Add protocol specific flags */
1143 switch (sc->sc_proto) {
1144 case LAGG_PROTO_FAILOVER:
1145 if (lp == sc->sc_primary)
1146 rp->rp_flags |= LAGG_PORT_MASTER;
1147 if (lp == lagg_link_active(sc, sc->sc_primary))
1148 rp->rp_flags |= LAGG_PORT_ACTIVE;
1151 case LAGG_PROTO_ROUNDROBIN:
1152 case LAGG_PROTO_LOADBALANCE:
1153 case LAGG_PROTO_ETHERCHANNEL:
1154 case LAGG_PROTO_BROADCAST:
1155 if (LAGG_PORTACTIVE(lp))
1156 rp->rp_flags |= LAGG_PORT_ACTIVE;
1159 case LAGG_PROTO_LACP:
1160 /* LACP has a different definition of active */
1161 if (lacp_isactive(lp))
1162 rp->rp_flags |= LAGG_PORT_ACTIVE;
1163 if (lacp_iscollecting(lp))
1164 rp->rp_flags |= LAGG_PORT_COLLECTING;
1165 if (lacp_isdistributing(lp))
1166 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1173 lagg_init(void *xsc)
1175 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1176 struct lagg_port *lp;
1177 struct ifnet *ifp = sc->sc_ifp;
1179 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1184 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1185 /* Update the port lladdrs */
1186 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1187 lagg_port_lladdr(lp, IF_LLADDR(ifp));
1189 lagg_proto_init(sc);
1195 lagg_stop(struct lagg_softc *sc)
1197 struct ifnet *ifp = sc->sc_ifp;
1199 LAGG_WLOCK_ASSERT(sc);
1201 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1204 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1206 lagg_proto_stop(sc);
1210 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1212 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1213 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1214 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1215 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1216 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1217 struct ifreq *ifr = (struct ifreq *)data;
1218 struct lagg_port *lp;
1220 struct thread *td = curthread;
1222 int count, buflen, len, error = 0;
1223 struct rm_priotracker tracker;
1225 bzero(&rpbuf, sizeof(rpbuf));
1229 LAGG_RLOCK(sc, &tracker);
1231 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1233 buflen = count * sizeof(struct lagg_reqport);
1234 LAGG_RUNLOCK(sc, &tracker);
1236 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1238 LAGG_RLOCK(sc, &tracker);
1239 ra->ra_proto = sc->sc_proto;
1240 lagg_proto_request(sc, &ra->ra_psc);
1243 len = min(ra->ra_size, buflen);
1244 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1245 if (len < sizeof(rpbuf))
1248 lagg_port2req(lp, &rpbuf);
1249 memcpy(buf, &rpbuf, sizeof(rpbuf));
1251 buf += sizeof(rpbuf);
1252 len -= sizeof(rpbuf);
1254 LAGG_RUNLOCK(sc, &tracker);
1255 ra->ra_ports = count;
1256 ra->ra_size = count * sizeof(rpbuf);
1257 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1258 free(outbuf, M_TEMP);
1261 error = priv_check(td, PRIV_NET_LAGG);
1264 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) {
1265 error = EPROTONOSUPPORT;
1270 lagg_proto_detach(sc);
1271 lagg_proto_attach(sc, ra->ra_proto);
1274 ro->ro_opts = sc->sc_opts;
1275 if (sc->sc_proto == LAGG_PROTO_LACP) {
1276 struct lacp_softc *lsc;
1278 lsc = (struct lacp_softc *)sc->sc_psc;
1279 if (lsc->lsc_debug.lsc_tx_test != 0)
1280 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1281 if (lsc->lsc_debug.lsc_rx_test != 0)
1282 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1283 if (lsc->lsc_strict_mode != 0)
1284 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1286 ro->ro_active = sc->sc_active;
1289 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1290 ro->ro_active += LAGG_PORTACTIVE(lp);
1292 ro->ro_flapping = sc->sc_flapping;
1293 ro->ro_flowid_shift = sc->flowid_shift;
1296 error = priv_check(td, PRIV_NET_LAGG);
1299 if (ro->ro_opts == 0)
1302 * Set options. LACP options are stored in sc->sc_psc,
1307 switch (ro->ro_opts) {
1308 case LAGG_OPT_USE_FLOWID:
1309 case -LAGG_OPT_USE_FLOWID:
1310 case LAGG_OPT_FLOWIDSHIFT:
1314 case LAGG_OPT_LACP_TXTEST:
1315 case -LAGG_OPT_LACP_TXTEST:
1316 case LAGG_OPT_LACP_RXTEST:
1317 case -LAGG_OPT_LACP_RXTEST:
1318 case LAGG_OPT_LACP_STRICT:
1319 case -LAGG_OPT_LACP_STRICT:
1329 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1330 /* Invalid combination of options specified. */
1333 break; /* Return from SIOCSLAGGOPTS. */
1336 * Store new options into sc->sc_opts except for
1337 * FLOWIDSHIFT and LACP options.
1340 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1341 sc->flowid_shift = ro->ro_flowid_shift;
1342 else if (ro->ro_opts > 0)
1343 sc->sc_opts |= ro->ro_opts;
1345 sc->sc_opts &= ~ro->ro_opts;
1347 struct lacp_softc *lsc;
1349 lsc = (struct lacp_softc *)sc->sc_psc;
1351 switch (ro->ro_opts) {
1352 case LAGG_OPT_LACP_TXTEST:
1353 lsc->lsc_debug.lsc_tx_test = 1;
1355 case -LAGG_OPT_LACP_TXTEST:
1356 lsc->lsc_debug.lsc_tx_test = 0;
1358 case LAGG_OPT_LACP_RXTEST:
1359 lsc->lsc_debug.lsc_rx_test = 1;
1361 case -LAGG_OPT_LACP_RXTEST:
1362 lsc->lsc_debug.lsc_rx_test = 0;
1364 case LAGG_OPT_LACP_STRICT:
1365 lsc->lsc_strict_mode = 1;
1367 case -LAGG_OPT_LACP_STRICT:
1368 lsc->lsc_strict_mode = 0;
1374 case SIOCGLAGGFLAGS:
1375 rf->rf_flags = sc->sc_flags;
1378 error = priv_check(td, PRIV_NET_LAGG);
1381 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1386 sc->sc_flags &= ~LAGG_F_HASHMASK;
1387 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1391 if (rp->rp_portname[0] == '\0' ||
1392 (tpif = ifunit(rp->rp_portname)) == NULL) {
1397 LAGG_RLOCK(sc, &tracker);
1398 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1399 lp->lp_softc != sc) {
1401 LAGG_RUNLOCK(sc, &tracker);
1405 lagg_port2req(lp, rp);
1406 LAGG_RUNLOCK(sc, &tracker);
1409 error = priv_check(td, PRIV_NET_LAGG);
1412 if (rp->rp_portname[0] == '\0' ||
1413 (tpif = ifunit(rp->rp_portname)) == NULL) {
1418 error = lagg_port_create(sc, tpif);
1421 case SIOCSLAGGDELPORT:
1422 error = priv_check(td, PRIV_NET_LAGG);
1425 if (rp->rp_portname[0] == '\0' ||
1426 (tpif = ifunit(rp->rp_portname)) == NULL) {
1432 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1433 lp->lp_softc != sc) {
1439 error = lagg_port_destroy(lp, 1);
1443 /* Set flags on ports too */
1445 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1446 lagg_setflags(lp, 1);
1450 if (!(ifp->if_flags & IFF_UP) &&
1451 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1453 * If interface is marked down and it is running,
1454 * then stop and disable it.
1459 } else if ((ifp->if_flags & IFF_UP) &&
1460 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1462 * If interface is marked up and it is stopped, then
1465 (*ifp->if_init)(sc);
1471 error = lagg_ether_setmulti(sc);
1476 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1481 /* Do not allow the MTU or caps to be directly changed */
1486 error = ether_ioctl(ifp, cmd, data);
1493 lagg_ether_setmulti(struct lagg_softc *sc)
1495 struct lagg_port *lp;
1497 LAGG_WLOCK_ASSERT(sc);
1499 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1500 /* First, remove any existing filter entries. */
1501 lagg_ether_cmdmulti(lp, 0);
1502 /* copy all addresses from the lagg interface to the port */
1503 lagg_ether_cmdmulti(lp, 1);
1509 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1511 struct lagg_softc *sc = lp->lp_softc;
1512 struct ifnet *ifp = lp->lp_ifp;
1513 struct ifnet *scifp = sc->sc_ifp;
1515 struct ifmultiaddr *ifma;
1518 LAGG_WLOCK_ASSERT(sc);
1521 IF_ADDR_WLOCK(scifp);
1522 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1523 if (ifma->ifma_addr->sa_family != AF_LINK)
1525 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1527 IF_ADDR_WUNLOCK(scifp);
1530 bcopy(ifma->ifma_addr, &mc->mc_addr,
1531 ifma->ifma_addr->sa_len);
1532 mc->mc_addr.sdl_index = ifp->if_index;
1534 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1536 IF_ADDR_WUNLOCK(scifp);
1537 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1538 error = if_addmulti(ifp,
1539 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1544 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1545 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1546 if (mc->mc_ifma && !lp->lp_detaching)
1547 if_delmulti_ifma(mc->mc_ifma);
1554 /* Handle a ref counted flag that should be set on the lagg port as well */
1556 lagg_setflag(struct lagg_port *lp, int flag, int status,
1557 int (*func)(struct ifnet *, int))
1559 struct lagg_softc *sc = lp->lp_softc;
1560 struct ifnet *scifp = sc->sc_ifp;
1561 struct ifnet *ifp = lp->lp_ifp;
1564 LAGG_WLOCK_ASSERT(sc);
1566 status = status ? (scifp->if_flags & flag) : 0;
1567 /* Now "status" contains the flag value or 0 */
1570 * See if recorded ports status is different from what
1571 * we want it to be. If it is, flip it. We record ports
1572 * status in lp_ifflags so that we won't clear ports flag
1573 * we haven't set. In fact, we don't clear or set ports
1574 * flags directly, but get or release references to them.
1575 * That's why we can be sure that recorded flags still are
1576 * in accord with actual ports flags.
1578 if (status != (lp->lp_ifflags & flag)) {
1579 error = (*func)(ifp, status);
1582 lp->lp_ifflags &= ~flag;
1583 lp->lp_ifflags |= status;
1589 * Handle IFF_* flags that require certain changes on the lagg port
1590 * if "status" is true, update ports flags respective to the lagg
1591 * if "status" is false, forcedly clear the flags set on port.
1594 lagg_setflags(struct lagg_port *lp, int status)
1598 for (i = 0; lagg_pflags[i].flag; i++) {
1599 error = lagg_setflag(lp, lagg_pflags[i].flag,
1600 status, lagg_pflags[i].func);
1608 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1610 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1611 int error, len, mcast;
1612 struct rm_priotracker tracker;
1614 len = m->m_pkthdr.len;
1615 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1617 LAGG_RLOCK(sc, &tracker);
1618 /* We need a Tx algorithm and at least one port */
1619 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1620 LAGG_RUNLOCK(sc, &tracker);
1622 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1626 ETHER_BPF_MTAP(ifp, m);
1628 error = lagg_proto_start(sc, m);
1629 LAGG_RUNLOCK(sc, &tracker);
1632 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1638 * The ifp->if_qflush entry point for lagg(4) is no-op.
1641 lagg_qflush(struct ifnet *ifp __unused)
1645 static struct mbuf *
1646 lagg_input(struct ifnet *ifp, struct mbuf *m)
1648 struct lagg_port *lp = ifp->if_lagg;
1649 struct lagg_softc *sc = lp->lp_softc;
1650 struct ifnet *scifp = sc->sc_ifp;
1651 struct rm_priotracker tracker;
1653 LAGG_RLOCK(sc, &tracker);
1654 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1655 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1656 sc->sc_proto == LAGG_PROTO_NONE) {
1657 LAGG_RUNLOCK(sc, &tracker);
1662 ETHER_BPF_MTAP(scifp, m);
1664 m = (lp->lp_detaching == 0) ? lagg_proto_input(sc, lp, m) : NULL;
1667 if (scifp->if_flags & IFF_MONITOR) {
1673 LAGG_RUNLOCK(sc, &tracker);
1678 lagg_media_change(struct ifnet *ifp)
1680 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1682 if (sc->sc_ifflags & IFF_DEBUG)
1683 printf("%s\n", __func__);
1690 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1692 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1693 struct lagg_port *lp;
1694 struct rm_priotracker tracker;
1696 imr->ifm_status = IFM_AVALID;
1697 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1699 LAGG_RLOCK(sc, &tracker);
1700 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1701 if (LAGG_PORTACTIVE(lp))
1702 imr->ifm_status |= IFM_ACTIVE;
1704 LAGG_RUNLOCK(sc, &tracker);
1708 lagg_linkstate(struct lagg_softc *sc)
1710 struct lagg_port *lp;
1711 int new_link = LINK_STATE_DOWN;
1714 /* Our link is considered up if at least one of our ports is active */
1715 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1716 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1717 new_link = LINK_STATE_UP;
1721 if_link_state_change(sc->sc_ifp, new_link);
1723 /* Update if_baudrate to reflect the max possible speed */
1724 switch (sc->sc_proto) {
1725 case LAGG_PROTO_FAILOVER:
1726 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1727 sc->sc_primary->lp_ifp->if_baudrate : 0;
1729 case LAGG_PROTO_ROUNDROBIN:
1730 case LAGG_PROTO_LOADBALANCE:
1731 case LAGG_PROTO_ETHERCHANNEL:
1732 case LAGG_PROTO_BROADCAST:
1734 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1735 speed += lp->lp_ifp->if_baudrate;
1736 sc->sc_ifp->if_baudrate = speed;
1738 case LAGG_PROTO_LACP:
1739 /* LACP updates if_baudrate itself */
1745 lagg_port_state(struct ifnet *ifp, int state)
1747 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1748 struct lagg_softc *sc = NULL;
1757 lagg_proto_linkstate(sc, lp);
1762 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1764 struct lagg_port *lp_next, *rval = NULL;
1765 // int new_link = LINK_STATE_DOWN;
1767 LAGG_RLOCK_ASSERT(sc);
1769 * Search a port which reports an active link state.
1774 if (LAGG_PORTACTIVE(lp)) {
1778 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1779 LAGG_PORTACTIVE(lp_next)) {
1785 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1786 if (LAGG_PORTACTIVE(lp_next)) {
1795 * The IEEE 802.1D standard assumes that a lagg with
1796 * multiple ports is always full duplex. This is valid
1797 * for load sharing laggs and if at least two links
1798 * are active. Unfortunately, checking the latter would
1799 * be too expensive at this point.
1801 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1803 new_link = LINK_STATE_FULL_DUPLEX;
1805 new_link = rval->lp_link_state;
1813 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1815 if (m->m_pkthdr.len < (off + len)) {
1817 } else if (m->m_len < (off + len)) {
1818 m_copydata(m, off, len, buf);
1821 return (mtod(m, char *) + off);
1825 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1830 struct ether_header *eh;
1831 const struct ether_vlan_header *vlan;
1833 const struct ip *ip;
1834 const uint32_t *ports;
1838 const struct ip6_hdr *ip6;
1848 struct ether_vlan_header vlan;
1856 eh = mtod(m, struct ether_header *);
1857 etype = ntohs(eh->ether_type);
1858 if (sc->sc_flags & LAGG_F_HASHL2) {
1859 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1860 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1863 /* Special handling for encapsulating VLAN frames */
1864 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1865 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1866 sizeof(m->m_pkthdr.ether_vtag), p);
1867 } else if (etype == ETHERTYPE_VLAN) {
1868 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1872 if (sc->sc_flags & LAGG_F_HASHL2)
1873 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1874 etype = ntohs(vlan->evl_proto);
1875 off += sizeof(*vlan) - sizeof(*eh);
1881 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1885 if (sc->sc_flags & LAGG_F_HASHL3) {
1886 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1887 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1889 if (!(sc->sc_flags & LAGG_F_HASHL4))
1895 iphlen = ip->ip_hl << 2;
1896 if (iphlen < sizeof(*ip))
1899 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1902 p = hash32_buf(ports, sizeof(*ports), p);
1908 case ETHERTYPE_IPV6:
1909 if (!(sc->sc_flags & LAGG_F_HASHL3))
1911 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1915 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1916 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1917 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1918 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1927 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1930 return (ifp->if_transmit)(ifp, m);
1934 * Simple round robin aggregation
1937 lagg_rr_attach(struct lagg_softc *sc)
1939 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1944 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1946 struct lagg_port *lp;
1949 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1951 lp = SLIST_FIRST(&sc->sc_ports);
1953 lp = SLIST_NEXT(lp, lp_entries);
1956 * Check the port's link state. This will return the next active
1957 * port if the link is down or the port is NULL.
1959 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1965 return (lagg_enqueue(lp->lp_ifp, m));
1968 static struct mbuf *
1969 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1971 struct ifnet *ifp = sc->sc_ifp;
1973 /* Just pass in the packet to our lagg device */
1974 m->m_pkthdr.rcvif = ifp;
1983 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1985 int active_ports = 0;
1988 struct lagg_port *lp, *last = NULL;
1991 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1992 if (!LAGG_PORTACTIVE(lp))
1998 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2005 ret = lagg_enqueue(last->lp_ifp, m0);
2015 if ((last = lagg_link_active(sc, last)) == NULL) {
2020 ret = lagg_enqueue(last->lp_ifp, m);
2031 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2033 struct ifnet *ifp = sc->sc_ifp;
2035 /* Just pass in the packet to our lagg device */
2036 m->m_pkthdr.rcvif = ifp;
2044 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2046 struct lagg_port *lp;
2048 /* Use the master port if active or the next available port */
2049 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2055 return (lagg_enqueue(lp->lp_ifp, m));
2058 static struct mbuf *
2059 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2061 struct ifnet *ifp = sc->sc_ifp;
2062 struct lagg_port *tmp_tp;
2064 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2065 m->m_pkthdr.rcvif = ifp;
2069 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2070 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2072 * If tmp_tp is null, we've recieved a packet when all
2073 * our links are down. Weird, but process it anyways.
2075 if ((tmp_tp == NULL || tmp_tp == lp)) {
2076 m->m_pkthdr.rcvif = ifp;
2089 lagg_lb_attach(struct lagg_softc *sc)
2091 struct lagg_port *lp;
2094 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2096 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2098 lb->lb_key = arc4random();
2101 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2102 lagg_lb_port_create(lp);
2106 lagg_lb_detach(struct lagg_softc *sc)
2110 lb = (struct lagg_lb *)sc->sc_psc;
2117 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2119 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2120 struct lagg_port *lp_next;
2123 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2124 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2127 if (i >= LAGG_MAX_PORTS)
2129 if (sc->sc_ifflags & IFF_DEBUG)
2130 printf("%s: port %s at index %d\n",
2131 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2132 lb->lb_ports[i++] = lp_next;
2139 lagg_lb_port_create(struct lagg_port *lp)
2141 struct lagg_softc *sc = lp->lp_softc;
2142 return (lagg_lb_porttable(sc, NULL));
2146 lagg_lb_port_destroy(struct lagg_port *lp)
2148 struct lagg_softc *sc = lp->lp_softc;
2149 lagg_lb_porttable(sc, lp);
2153 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2155 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2156 struct lagg_port *lp = NULL;
2159 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) && (m->m_flags & M_FLOWID))
2160 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2162 p = lagg_hashmbuf(sc, m, lb->lb_key);
2164 lp = lb->lb_ports[p];
2167 * Check the port's link state. This will return the next active
2168 * port if the link is down or the port is NULL.
2170 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2176 return (lagg_enqueue(lp->lp_ifp, m));
2179 static struct mbuf *
2180 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2182 struct ifnet *ifp = sc->sc_ifp;
2184 /* Just pass in the packet to our lagg device */
2185 m->m_pkthdr.rcvif = ifp;
2194 lagg_lacp_attach(struct lagg_softc *sc)
2196 struct lagg_port *lp;
2199 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2200 lacp_port_create(lp);
2204 lagg_lacp_detach(struct lagg_softc *sc)
2206 struct lagg_port *lp;
2209 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2210 lacp_port_destroy(lp);
2220 lagg_lacp_lladdr(struct lagg_softc *sc)
2222 struct lagg_port *lp;
2224 /* purge all the lacp ports */
2225 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2226 lacp_port_destroy(lp);
2228 /* add them back in */
2229 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2230 lacp_port_create(lp);
2234 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2236 struct lagg_port *lp;
2238 lp = lacp_select_tx_port(sc, m);
2245 return (lagg_enqueue(lp->lp_ifp, m));
2248 static struct mbuf *
2249 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2251 struct ifnet *ifp = sc->sc_ifp;
2252 struct ether_header *eh;
2255 eh = mtod(m, struct ether_header *);
2256 etype = ntohs(eh->ether_type);
2258 /* Tap off LACP control messages */
2259 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2260 m = lacp_input(lp, m);
2266 * If the port is not collecting or not in the active aggregator then
2269 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2274 m->m_pkthdr.rcvif = ifp;