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>
39 #include <sys/fnv_hash.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 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 flowid shift */
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)
572 struct ifnet *ifp = sc->sc_ifp;
575 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
578 LAGG_WLOCK_ASSERT(sc);
580 * Set the link layer address on the lagg interface.
581 * lagg_proto_lladdr() notifies the MAC change to
582 * the aggregation protocol. iflladdr_event handler which
583 * may trigger gratuitous ARPs for INET will be handled in
586 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
587 lagg_proto_lladdr(sc);
589 bzero(&lp, sizeof(lp));
590 lp.lp_ifp = sc->sc_ifp;
593 lagg_port_lladdr(&lp, lladdr);
597 lagg_capabilities(struct lagg_softc *sc)
599 struct lagg_port *lp;
600 int cap = ~0, ena = ~0;
602 struct ifnet_hw_tsomax hw_tsomax;
604 LAGG_WLOCK_ASSERT(sc);
606 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
608 /* Get capabilities from the lagg ports */
609 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
610 cap &= lp->lp_ifp->if_capabilities;
611 ena &= lp->lp_ifp->if_capenable;
612 hwa &= lp->lp_ifp->if_hwassist;
613 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
615 cap = (cap == ~0 ? 0 : cap);
616 ena = (ena == ~0 ? 0 : ena);
617 hwa = (hwa == ~0 ? 0 : hwa);
619 if (sc->sc_ifp->if_capabilities != cap ||
620 sc->sc_ifp->if_capenable != ena ||
621 sc->sc_ifp->if_hwassist != hwa ||
622 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
623 sc->sc_ifp->if_capabilities = cap;
624 sc->sc_ifp->if_capenable = ena;
625 sc->sc_ifp->if_hwassist = hwa;
626 getmicrotime(&sc->sc_ifp->if_lastchange);
628 if (sc->sc_ifflags & IFF_DEBUG)
629 if_printf(sc->sc_ifp,
630 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
635 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
637 struct lagg_softc *sc = lp->lp_softc;
638 struct ifnet *ifp = lp->lp_ifp;
639 struct lagg_llq *llq;
643 LAGG_WLOCK_ASSERT(sc);
645 primary = (sc->sc_primary->lp_ifp == ifp) ? 1 : 0;
646 if (primary == 0 && (lp->lp_detaching ||
647 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0))
650 /* Check to make sure its not already queued to be changed */
651 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
652 if (llq->llq_ifp == ifp) {
659 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
660 if (llq == NULL) /* XXX what to do */
664 /* Update the lladdr even if pending, it may have changed */
666 llq->llq_primary = primary;
667 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
670 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
672 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
676 * Set the interface MAC address from a taskqueue to avoid a LOR.
679 lagg_port_setlladdr(void *arg, int pending)
681 struct lagg_softc *sc = (struct lagg_softc *)arg;
682 struct lagg_llq *llq, *head;
686 /* Grab a local reference of the queue and remove it from the softc */
688 head = SLIST_FIRST(&sc->sc_llq_head);
689 SLIST_FIRST(&sc->sc_llq_head) = NULL;
693 * Traverse the queue and set the lladdr on each ifp. It is safe to do
694 * unlocked as we have the only reference to it.
696 for (llq = head; llq != NULL; llq = head) {
699 CURVNET_SET(ifp->if_vnet);
700 if (llq->llq_primary == 0) {
702 * Set the link layer address on the laggport interface.
703 * if_setlladdr() triggers gratuitous ARPs for INET.
705 error = if_setlladdr(ifp, llq->llq_lladdr,
708 printf("%s: setlladdr failed on %s\n", __func__,
711 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);
745 /* Allow the first Ethernet member to define the MTU */
746 if (SLIST_EMPTY(&sc->sc_ports))
747 sc->sc_ifp->if_mtu = ifp->if_mtu;
748 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
749 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
754 if ((lp = malloc(sizeof(struct lagg_port),
755 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
758 /* Check if port is a stacked lagg */
760 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
761 if (ifp == sc_ptr->sc_ifp) {
765 /* XXX disable stacking for the moment, its untested */
766 #ifdef LAGG_PORT_STACKING
767 lp->lp_flags |= LAGG_PORT_STACK;
768 if (lagg_port_checkstacking(sc_ptr) >=
779 /* Change the interface type */
780 lp->lp_iftype = ifp->if_type;
781 ifp->if_type = IFT_IEEE8023ADLAG;
783 lp->lp_ioctl = ifp->if_ioctl;
784 ifp->if_ioctl = lagg_port_ioctl;
785 lp->lp_output = ifp->if_output;
786 ifp->if_output = lagg_port_output;
791 /* Save port link layer address */
792 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
794 if (SLIST_EMPTY(&sc->sc_ports)) {
796 lagg_lladdr(sc, IF_LLADDR(ifp));
798 /* Update link layer address for this port */
799 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
803 * Insert into the list of ports.
804 * Keep ports sorted by if_index. It is handy, when configuration
805 * is predictable and `ifconfig laggN create ...` command
806 * will lead to the same result each time.
808 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
809 if (tlp->lp_ifp->if_index < ifp->if_index && (
810 SLIST_NEXT(tlp, lp_entries) == NULL ||
811 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
816 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
818 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
821 /* Update lagg capabilities */
822 lagg_capabilities(sc);
825 /* Read port counters */
826 pval = lp->port_counters.val;
827 for (i = 0; i < IFCOUNTERS; i++, pval++)
828 *pval = ifp->if_get_counter(ifp, i);
829 /* Add multicast addresses and interface flags to this port */
830 lagg_ether_cmdmulti(lp, 1);
831 lagg_setflags(lp, 1);
833 if ((error = lagg_proto_addport(sc, lp)) != 0) {
834 /* Remove the port, without calling pr_delport. */
835 lagg_port_destroy(lp, 0);
842 #ifdef LAGG_PORT_STACKING
844 lagg_port_checkstacking(struct lagg_softc *sc)
846 struct lagg_softc *sc_ptr;
847 struct lagg_port *lp;
850 LAGG_WLOCK_ASSERT(sc);
852 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
853 if (lp->lp_flags & LAGG_PORT_STACK) {
854 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
855 m = MAX(m, lagg_port_checkstacking(sc_ptr));
864 lagg_port_destroy(struct lagg_port *lp, int rundelport)
866 struct lagg_softc *sc = lp->lp_softc;
867 struct lagg_port *lp_ptr;
868 struct lagg_llq *llq;
869 struct ifnet *ifp = lp->lp_ifp;
870 uint64_t *pval, vdiff;
873 LAGG_WLOCK_ASSERT(sc);
876 lagg_proto_delport(sc, lp);
879 * Remove multicast addresses and interface flags from this port and
880 * reset the MAC address, skip if the interface is being detached.
882 if (!lp->lp_detaching) {
883 lagg_ether_cmdmulti(lp, 0);
884 lagg_setflags(lp, 0);
885 lagg_port_lladdr(lp, lp->lp_lladdr);
888 /* Restore interface */
889 ifp->if_type = lp->lp_iftype;
890 ifp->if_ioctl = lp->lp_ioctl;
891 ifp->if_output = lp->lp_output;
894 /* Update detached port counters */
895 pval = lp->port_counters.val;
896 for (i = 0; i < IFCOUNTERS; i++, pval++) {
897 vdiff = ifp->if_get_counter(ifp, i) - *pval;
898 sc->detached_counters.val[i] += vdiff;
901 /* Finally, remove the port from the lagg */
902 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
905 /* Update the primary interface */
906 if (lp == sc->sc_primary) {
907 uint8_t lladdr[ETHER_ADDR_LEN];
909 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
910 bzero(&lladdr, ETHER_ADDR_LEN);
912 bcopy(lp_ptr->lp_lladdr,
913 lladdr, ETHER_ADDR_LEN);
915 lagg_lladdr(sc, lladdr);
916 sc->sc_primary = lp_ptr;
918 /* Update link layer address for each port */
919 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
920 lagg_port_lladdr(lp_ptr, lladdr);
923 /* Remove any pending lladdr changes from the queue */
924 if (lp->lp_detaching) {
925 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
926 if (llq->llq_ifp == ifp) {
927 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
930 break; /* Only appears once */
936 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
940 /* Update lagg capabilities */
941 lagg_capabilities(sc);
948 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
950 struct lagg_reqport *rp = (struct lagg_reqport *)data;
951 struct lagg_softc *sc;
952 struct lagg_port *lp = NULL;
954 struct rm_priotracker tracker;
956 /* Should be checked by the caller */
957 if (ifp->if_type != IFT_IEEE8023ADLAG ||
958 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
963 if (rp->rp_portname[0] == '\0' ||
964 ifunit(rp->rp_portname) != ifp) {
969 LAGG_RLOCK(sc, &tracker);
970 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
972 LAGG_RUNLOCK(sc, &tracker);
976 lagg_port2req(lp, rp);
977 LAGG_RUNLOCK(sc, &tracker);
981 if (lp->lp_ioctl == NULL) {
985 error = (*lp->lp_ioctl)(ifp, cmd, data);
989 /* Update lagg interface capabilities */
991 lagg_capabilities(sc);
996 /* Do not allow the MTU to be changed once joined */
1007 if (lp->lp_ioctl != NULL)
1008 return ((*lp->lp_ioctl)(ifp, cmd, data));
1014 * Requests counter @cnt data.
1016 * Counter value is calculated the following way:
1017 * 1) for each port, sum difference between current and "initial" measurements.
1018 * 2) add lagg logical interface counters.
1019 * 3) add data from detached_counters array.
1021 * We also do the following things on ports attach/detach:
1022 * 1) On port attach we store all counters it has into port_counter array.
1023 * 2) On port detach we add the different between "initial" and
1024 * current counters data to detached_counters array.
1027 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1029 struct lagg_softc *sc;
1030 struct lagg_port *lp;
1031 struct ifnet *lpifp;
1032 struct rm_priotracker tracker;
1033 uint64_t newval, oldval, vsum;
1035 /* Revise this when we've got non-generic counters. */
1036 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1038 sc = (struct lagg_softc *)ifp->if_softc;
1039 LAGG_RLOCK(sc, &tracker);
1042 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1043 /* Saved attached value */
1044 oldval = lp->port_counters.val[cnt];
1047 newval = lpifp->if_get_counter(lpifp, cnt);
1048 /* Calculate diff and save new */
1049 vsum += newval - oldval;
1053 * Add counter data which might be added by upper
1054 * layer protocols operating on logical interface.
1056 vsum += if_get_counter_default(ifp, cnt);
1059 * Add counter data from detached ports counters
1061 vsum += sc->detached_counters.val[cnt];
1063 LAGG_RUNLOCK(sc, &tracker);
1069 * For direct output to child ports.
1072 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1073 const struct sockaddr *dst, struct route *ro)
1075 struct lagg_port *lp = ifp->if_lagg;
1077 switch (dst->sa_family) {
1078 case pseudo_AF_HDRCMPLT:
1080 return ((*lp->lp_output)(ifp, m, dst, ro));
1083 /* drop any other frames */
1089 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1091 struct lagg_port *lp;
1092 struct lagg_softc *sc;
1094 if ((lp = ifp->if_lagg) == NULL)
1096 /* If the ifnet is just being renamed, don't do anything. */
1097 if (ifp->if_flags & IFF_RENAMING)
1103 lp->lp_detaching = 1;
1104 lagg_port_destroy(lp, 1);
1109 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1111 struct lagg_softc *sc = lp->lp_softc;
1113 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1114 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1115 rp->rp_prio = lp->lp_prio;
1116 rp->rp_flags = lp->lp_flags;
1117 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1119 /* Add protocol specific flags */
1120 switch (sc->sc_proto) {
1121 case LAGG_PROTO_FAILOVER:
1122 if (lp == sc->sc_primary)
1123 rp->rp_flags |= LAGG_PORT_MASTER;
1124 if (lp == lagg_link_active(sc, sc->sc_primary))
1125 rp->rp_flags |= LAGG_PORT_ACTIVE;
1128 case LAGG_PROTO_ROUNDROBIN:
1129 case LAGG_PROTO_LOADBALANCE:
1130 case LAGG_PROTO_ETHERCHANNEL:
1131 case LAGG_PROTO_BROADCAST:
1132 if (LAGG_PORTACTIVE(lp))
1133 rp->rp_flags |= LAGG_PORT_ACTIVE;
1136 case LAGG_PROTO_LACP:
1137 /* LACP has a different definition of active */
1138 if (lacp_isactive(lp))
1139 rp->rp_flags |= LAGG_PORT_ACTIVE;
1140 if (lacp_iscollecting(lp))
1141 rp->rp_flags |= LAGG_PORT_COLLECTING;
1142 if (lacp_isdistributing(lp))
1143 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1150 lagg_init(void *xsc)
1152 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1153 struct lagg_port *lp;
1154 struct ifnet *ifp = sc->sc_ifp;
1156 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1161 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1162 /* Update the port lladdrs */
1163 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1164 lagg_port_lladdr(lp, IF_LLADDR(ifp));
1166 lagg_proto_init(sc);
1172 lagg_stop(struct lagg_softc *sc)
1174 struct ifnet *ifp = sc->sc_ifp;
1176 LAGG_WLOCK_ASSERT(sc);
1178 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1181 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1183 lagg_proto_stop(sc);
1187 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1189 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1190 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1191 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1192 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1193 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1194 struct ifreq *ifr = (struct ifreq *)data;
1195 struct lagg_port *lp;
1197 struct thread *td = curthread;
1199 int count, buflen, len, error = 0;
1200 struct rm_priotracker tracker;
1202 bzero(&rpbuf, sizeof(rpbuf));
1206 LAGG_RLOCK(sc, &tracker);
1208 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1210 buflen = count * sizeof(struct lagg_reqport);
1211 LAGG_RUNLOCK(sc, &tracker);
1213 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1215 LAGG_RLOCK(sc, &tracker);
1216 ra->ra_proto = sc->sc_proto;
1217 lagg_proto_request(sc, &ra->ra_psc);
1220 len = min(ra->ra_size, buflen);
1221 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1222 if (len < sizeof(rpbuf))
1225 lagg_port2req(lp, &rpbuf);
1226 memcpy(buf, &rpbuf, sizeof(rpbuf));
1228 buf += sizeof(rpbuf);
1229 len -= sizeof(rpbuf);
1231 LAGG_RUNLOCK(sc, &tracker);
1232 ra->ra_ports = count;
1233 ra->ra_size = count * sizeof(rpbuf);
1234 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1235 free(outbuf, M_TEMP);
1238 error = priv_check(td, PRIV_NET_LAGG);
1241 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) {
1242 error = EPROTONOSUPPORT;
1247 lagg_proto_detach(sc);
1248 lagg_proto_attach(sc, ra->ra_proto);
1251 ro->ro_opts = sc->sc_opts;
1252 if (sc->sc_proto == LAGG_PROTO_LACP) {
1253 struct lacp_softc *lsc;
1255 lsc = (struct lacp_softc *)sc->sc_psc;
1256 if (lsc->lsc_debug.lsc_tx_test != 0)
1257 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1258 if (lsc->lsc_debug.lsc_rx_test != 0)
1259 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1260 if (lsc->lsc_strict_mode != 0)
1261 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1263 ro->ro_active = sc->sc_active;
1266 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1267 ro->ro_active += LAGG_PORTACTIVE(lp);
1269 ro->ro_flapping = sc->sc_flapping;
1270 ro->ro_flowid_shift = sc->flowid_shift;
1273 error = priv_check(td, PRIV_NET_LAGG);
1276 if (ro->ro_opts == 0)
1279 * Set options. LACP options are stored in sc->sc_psc,
1284 switch (ro->ro_opts) {
1285 case LAGG_OPT_USE_FLOWID:
1286 case -LAGG_OPT_USE_FLOWID:
1287 case LAGG_OPT_FLOWIDSHIFT:
1291 case LAGG_OPT_LACP_TXTEST:
1292 case -LAGG_OPT_LACP_TXTEST:
1293 case LAGG_OPT_LACP_RXTEST:
1294 case -LAGG_OPT_LACP_RXTEST:
1295 case LAGG_OPT_LACP_STRICT:
1296 case -LAGG_OPT_LACP_STRICT:
1306 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1307 /* Invalid combination of options specified. */
1310 break; /* Return from SIOCSLAGGOPTS. */
1313 * Store new options into sc->sc_opts except for
1314 * FLOWIDSHIFT and LACP options.
1317 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1318 sc->flowid_shift = ro->ro_flowid_shift;
1319 else if (ro->ro_opts > 0)
1320 sc->sc_opts |= ro->ro_opts;
1322 sc->sc_opts &= ~ro->ro_opts;
1324 struct lacp_softc *lsc;
1326 lsc = (struct lacp_softc *)sc->sc_psc;
1328 switch (ro->ro_opts) {
1329 case LAGG_OPT_LACP_TXTEST:
1330 lsc->lsc_debug.lsc_tx_test = 1;
1332 case -LAGG_OPT_LACP_TXTEST:
1333 lsc->lsc_debug.lsc_tx_test = 0;
1335 case LAGG_OPT_LACP_RXTEST:
1336 lsc->lsc_debug.lsc_rx_test = 1;
1338 case -LAGG_OPT_LACP_RXTEST:
1339 lsc->lsc_debug.lsc_rx_test = 0;
1341 case LAGG_OPT_LACP_STRICT:
1342 lsc->lsc_strict_mode = 1;
1344 case -LAGG_OPT_LACP_STRICT:
1345 lsc->lsc_strict_mode = 0;
1351 case SIOCGLAGGFLAGS:
1352 rf->rf_flags = sc->sc_flags;
1355 error = priv_check(td, PRIV_NET_LAGG);
1358 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1363 sc->sc_flags &= ~LAGG_F_HASHMASK;
1364 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1368 if (rp->rp_portname[0] == '\0' ||
1369 (tpif = ifunit(rp->rp_portname)) == NULL) {
1374 LAGG_RLOCK(sc, &tracker);
1375 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1376 lp->lp_softc != sc) {
1378 LAGG_RUNLOCK(sc, &tracker);
1382 lagg_port2req(lp, rp);
1383 LAGG_RUNLOCK(sc, &tracker);
1386 error = priv_check(td, PRIV_NET_LAGG);
1389 if (rp->rp_portname[0] == '\0' ||
1390 (tpif = ifunit(rp->rp_portname)) == NULL) {
1396 * A laggport interface should not have inet6 address
1397 * because two interfaces with a valid link-local
1398 * scope zone must not be merged in any form. This
1399 * restriction is needed to prevent violation of
1400 * link-local scope zone. Attempts to add a laggport
1401 * interface which has inet6 addresses triggers
1402 * removal of all inet6 addresses on the member
1405 if (in6ifa_llaonifp(tpif)) {
1407 if_printf(sc->sc_ifp,
1408 "IPv6 addresses on %s have been removed "
1409 "before adding it as a member to prevent "
1410 "IPv6 address scope violation.\n",
1415 error = lagg_port_create(sc, tpif);
1418 case SIOCSLAGGDELPORT:
1419 error = priv_check(td, PRIV_NET_LAGG);
1422 if (rp->rp_portname[0] == '\0' ||
1423 (tpif = ifunit(rp->rp_portname)) == NULL) {
1429 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1430 lp->lp_softc != sc) {
1436 error = lagg_port_destroy(lp, 1);
1440 /* Set flags on ports too */
1442 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1443 lagg_setflags(lp, 1);
1447 if (!(ifp->if_flags & IFF_UP) &&
1448 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1450 * If interface is marked down and it is running,
1451 * then stop and disable it.
1456 } else if ((ifp->if_flags & IFF_UP) &&
1457 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1459 * If interface is marked up and it is stopped, then
1462 (*ifp->if_init)(sc);
1468 error = lagg_ether_setmulti(sc);
1473 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1478 /* Do not allow the MTU or caps to be directly changed */
1483 error = ether_ioctl(ifp, cmd, data);
1490 lagg_ether_setmulti(struct lagg_softc *sc)
1492 struct lagg_port *lp;
1494 LAGG_WLOCK_ASSERT(sc);
1496 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1497 /* First, remove any existing filter entries. */
1498 lagg_ether_cmdmulti(lp, 0);
1499 /* copy all addresses from the lagg interface to the port */
1500 lagg_ether_cmdmulti(lp, 1);
1506 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1508 struct lagg_softc *sc = lp->lp_softc;
1509 struct ifnet *ifp = lp->lp_ifp;
1510 struct ifnet *scifp = sc->sc_ifp;
1512 struct ifmultiaddr *ifma;
1515 LAGG_WLOCK_ASSERT(sc);
1518 IF_ADDR_WLOCK(scifp);
1519 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1520 if (ifma->ifma_addr->sa_family != AF_LINK)
1522 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1524 IF_ADDR_WUNLOCK(scifp);
1527 bcopy(ifma->ifma_addr, &mc->mc_addr,
1528 ifma->ifma_addr->sa_len);
1529 mc->mc_addr.sdl_index = ifp->if_index;
1531 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1533 IF_ADDR_WUNLOCK(scifp);
1534 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1535 error = if_addmulti(ifp,
1536 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1541 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1542 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1543 if (mc->mc_ifma && !lp->lp_detaching)
1544 if_delmulti_ifma(mc->mc_ifma);
1551 /* Handle a ref counted flag that should be set on the lagg port as well */
1553 lagg_setflag(struct lagg_port *lp, int flag, int status,
1554 int (*func)(struct ifnet *, int))
1556 struct lagg_softc *sc = lp->lp_softc;
1557 struct ifnet *scifp = sc->sc_ifp;
1558 struct ifnet *ifp = lp->lp_ifp;
1561 LAGG_WLOCK_ASSERT(sc);
1563 status = status ? (scifp->if_flags & flag) : 0;
1564 /* Now "status" contains the flag value or 0 */
1567 * See if recorded ports status is different from what
1568 * we want it to be. If it is, flip it. We record ports
1569 * status in lp_ifflags so that we won't clear ports flag
1570 * we haven't set. In fact, we don't clear or set ports
1571 * flags directly, but get or release references to them.
1572 * That's why we can be sure that recorded flags still are
1573 * in accord with actual ports flags.
1575 if (status != (lp->lp_ifflags & flag)) {
1576 error = (*func)(ifp, status);
1579 lp->lp_ifflags &= ~flag;
1580 lp->lp_ifflags |= status;
1586 * Handle IFF_* flags that require certain changes on the lagg port
1587 * if "status" is true, update ports flags respective to the lagg
1588 * if "status" is false, forcedly clear the flags set on port.
1591 lagg_setflags(struct lagg_port *lp, int status)
1595 for (i = 0; lagg_pflags[i].flag; i++) {
1596 error = lagg_setflag(lp, lagg_pflags[i].flag,
1597 status, lagg_pflags[i].func);
1605 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1607 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1608 int error, len, mcast;
1609 struct rm_priotracker tracker;
1611 len = m->m_pkthdr.len;
1612 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1614 LAGG_RLOCK(sc, &tracker);
1615 /* We need a Tx algorithm and at least one port */
1616 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1617 LAGG_RUNLOCK(sc, &tracker);
1619 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1623 ETHER_BPF_MTAP(ifp, m);
1625 error = lagg_proto_start(sc, m);
1626 LAGG_RUNLOCK(sc, &tracker);
1629 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1635 * The ifp->if_qflush entry point for lagg(4) is no-op.
1638 lagg_qflush(struct ifnet *ifp __unused)
1642 static struct mbuf *
1643 lagg_input(struct ifnet *ifp, struct mbuf *m)
1645 struct lagg_port *lp = ifp->if_lagg;
1646 struct lagg_softc *sc = lp->lp_softc;
1647 struct ifnet *scifp = sc->sc_ifp;
1648 struct rm_priotracker tracker;
1650 LAGG_RLOCK(sc, &tracker);
1651 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1652 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1653 sc->sc_proto == LAGG_PROTO_NONE) {
1654 LAGG_RUNLOCK(sc, &tracker);
1659 ETHER_BPF_MTAP(scifp, m);
1661 m = (lp->lp_detaching == 0) ? lagg_proto_input(sc, lp, m) : NULL;
1664 if (scifp->if_flags & IFF_MONITOR) {
1670 LAGG_RUNLOCK(sc, &tracker);
1675 lagg_media_change(struct ifnet *ifp)
1677 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1679 if (sc->sc_ifflags & IFF_DEBUG)
1680 printf("%s\n", __func__);
1687 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1689 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1690 struct lagg_port *lp;
1691 struct rm_priotracker tracker;
1693 imr->ifm_status = IFM_AVALID;
1694 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1696 LAGG_RLOCK(sc, &tracker);
1697 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1698 if (LAGG_PORTACTIVE(lp))
1699 imr->ifm_status |= IFM_ACTIVE;
1701 LAGG_RUNLOCK(sc, &tracker);
1705 lagg_linkstate(struct lagg_softc *sc)
1707 struct lagg_port *lp;
1708 int new_link = LINK_STATE_DOWN;
1711 /* Our link is considered up if at least one of our ports is active */
1712 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1713 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1714 new_link = LINK_STATE_UP;
1718 if_link_state_change(sc->sc_ifp, new_link);
1720 /* Update if_baudrate to reflect the max possible speed */
1721 switch (sc->sc_proto) {
1722 case LAGG_PROTO_FAILOVER:
1723 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1724 sc->sc_primary->lp_ifp->if_baudrate : 0;
1726 case LAGG_PROTO_ROUNDROBIN:
1727 case LAGG_PROTO_LOADBALANCE:
1728 case LAGG_PROTO_ETHERCHANNEL:
1729 case LAGG_PROTO_BROADCAST:
1731 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1732 speed += lp->lp_ifp->if_baudrate;
1733 sc->sc_ifp->if_baudrate = speed;
1735 case LAGG_PROTO_LACP:
1736 /* LACP updates if_baudrate itself */
1742 lagg_port_state(struct ifnet *ifp, int state)
1744 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1745 struct lagg_softc *sc = NULL;
1754 lagg_proto_linkstate(sc, lp);
1759 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1761 struct lagg_port *lp_next, *rval = NULL;
1762 // int new_link = LINK_STATE_DOWN;
1764 LAGG_RLOCK_ASSERT(sc);
1766 * Search a port which reports an active link state.
1771 if (LAGG_PORTACTIVE(lp)) {
1775 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1776 LAGG_PORTACTIVE(lp_next)) {
1782 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1783 if (LAGG_PORTACTIVE(lp_next)) {
1792 * The IEEE 802.1D standard assumes that a lagg with
1793 * multiple ports is always full duplex. This is valid
1794 * for load sharing laggs and if at least two links
1795 * are active. Unfortunately, checking the latter would
1796 * be too expensive at this point.
1798 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1800 new_link = LINK_STATE_FULL_DUPLEX;
1802 new_link = rval->lp_link_state;
1810 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1812 if (m->m_pkthdr.len < (off + len)) {
1814 } else if (m->m_len < (off + len)) {
1815 m_copydata(m, off, len, buf);
1818 return (mtod(m, char *) + off);
1822 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1827 struct ether_header *eh;
1828 const struct ether_vlan_header *vlan;
1830 const struct ip *ip;
1831 const uint32_t *ports;
1835 const struct ip6_hdr *ip6;
1845 struct ether_vlan_header vlan;
1853 eh = mtod(m, struct ether_header *);
1854 etype = ntohs(eh->ether_type);
1855 if (sc->sc_flags & LAGG_F_HASHL2) {
1856 p = fnv_32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1857 p = fnv_32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1860 /* Special handling for encapsulating VLAN frames */
1861 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1862 p = fnv_32_buf(&m->m_pkthdr.ether_vtag,
1863 sizeof(m->m_pkthdr.ether_vtag), p);
1864 } else if (etype == ETHERTYPE_VLAN) {
1865 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1869 if (sc->sc_flags & LAGG_F_HASHL2)
1870 p = fnv_32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1871 etype = ntohs(vlan->evl_proto);
1872 off += sizeof(*vlan) - sizeof(*eh);
1878 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1882 if (sc->sc_flags & LAGG_F_HASHL3) {
1883 p = fnv_32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1884 p = fnv_32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1886 if (!(sc->sc_flags & LAGG_F_HASHL4))
1892 iphlen = ip->ip_hl << 2;
1893 if (iphlen < sizeof(*ip))
1896 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1899 p = fnv_32_buf(ports, sizeof(*ports), p);
1905 case ETHERTYPE_IPV6:
1906 if (!(sc->sc_flags & LAGG_F_HASHL3))
1908 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1912 p = fnv_32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1913 p = fnv_32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1914 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1915 p = fnv_32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1924 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1927 return (ifp->if_transmit)(ifp, m);
1931 * Simple round robin aggregation
1934 lagg_rr_attach(struct lagg_softc *sc)
1936 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1941 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1943 struct lagg_port *lp;
1946 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1948 lp = SLIST_FIRST(&sc->sc_ports);
1950 lp = SLIST_NEXT(lp, lp_entries);
1953 * Check the port's link state. This will return the next active
1954 * port if the link is down or the port is NULL.
1956 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1962 return (lagg_enqueue(lp->lp_ifp, m));
1965 static struct mbuf *
1966 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1968 struct ifnet *ifp = sc->sc_ifp;
1970 /* Just pass in the packet to our lagg device */
1971 m->m_pkthdr.rcvif = ifp;
1980 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1982 int active_ports = 0;
1985 struct lagg_port *lp, *last = NULL;
1988 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1989 if (!LAGG_PORTACTIVE(lp))
1995 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2002 ret = lagg_enqueue(last->lp_ifp, m0);
2012 if ((last = lagg_link_active(sc, last)) == NULL) {
2017 ret = lagg_enqueue(last->lp_ifp, m);
2028 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2030 struct ifnet *ifp = sc->sc_ifp;
2032 /* Just pass in the packet to our lagg device */
2033 m->m_pkthdr.rcvif = ifp;
2041 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2043 struct lagg_port *lp;
2045 /* Use the master port if active or the next available port */
2046 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2052 return (lagg_enqueue(lp->lp_ifp, m));
2055 static struct mbuf *
2056 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2058 struct ifnet *ifp = sc->sc_ifp;
2059 struct lagg_port *tmp_tp;
2061 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2062 m->m_pkthdr.rcvif = ifp;
2066 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2067 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2069 * If tmp_tp is null, we've recieved a packet when all
2070 * our links are down. Weird, but process it anyways.
2072 if ((tmp_tp == NULL || tmp_tp == lp)) {
2073 m->m_pkthdr.rcvif = ifp;
2086 lagg_lb_attach(struct lagg_softc *sc)
2088 struct lagg_port *lp;
2092 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2094 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2096 seed = arc4random();
2097 lb->lb_key = FNV1_32_INIT;
2098 lb->lb_key = fnv_32_buf(&seed, sizeof(seed), lb->lb_key);
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) &&
2160 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2161 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2163 p = lagg_hashmbuf(sc, m, lb->lb_key);
2165 lp = lb->lb_ports[p];
2168 * Check the port's link state. This will return the next active
2169 * port if the link is down or the port is NULL.
2171 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2177 return (lagg_enqueue(lp->lp_ifp, m));
2180 static struct mbuf *
2181 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2183 struct ifnet *ifp = sc->sc_ifp;
2185 /* Just pass in the packet to our lagg device */
2186 m->m_pkthdr.rcvif = ifp;
2195 lagg_lacp_attach(struct lagg_softc *sc)
2197 struct lagg_port *lp;
2200 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2201 lacp_port_create(lp);
2205 lagg_lacp_detach(struct lagg_softc *sc)
2207 struct lagg_port *lp;
2210 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2211 lacp_port_destroy(lp);
2221 lagg_lacp_lladdr(struct lagg_softc *sc)
2223 struct lagg_port *lp;
2225 /* purge all the lacp ports */
2226 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2227 lacp_port_destroy(lp);
2229 /* add them back in */
2230 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2231 lacp_port_create(lp);
2235 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2237 struct lagg_port *lp;
2239 lp = lacp_select_tx_port(sc, m);
2246 return (lagg_enqueue(lp->lp_ifp, m));
2249 static struct mbuf *
2250 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2252 struct ifnet *ifp = sc->sc_ifp;
2253 struct ether_header *eh;
2256 eh = mtod(m, struct ether_header *);
2257 etype = ntohs(eh->ether_type);
2259 /* Tap off LACP control messages */
2260 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2261 m = lacp_input(lp, m);
2267 * If the port is not collecting or not in the active aggregator then
2270 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2275 m->m_pkthdr.rcvif = ifp;