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>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 #include "opt_inet6.h"
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/socket.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/module.h>
36 #include <sys/systm.h>
40 #include <sys/rwlock.h>
41 #include <sys/taskqueue.h>
43 #include <net/ethernet.h>
45 #include <net/if_clone.h>
46 #include <net/if_arp.h>
47 #include <net/if_dl.h>
48 #include <net/if_llc.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/if_var.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
65 #include <net/if_vlan_var.h>
66 #include <net/if_lagg.h>
67 #include <net/ieee8023ad_lacp.h>
69 /* Special flags we should propagate to the lagg ports. */
72 int (*func)(struct ifnet *, int);
74 {IFF_PROMISC, ifpromisc},
75 {IFF_ALLMULTI, if_allmulti},
79 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
80 static struct mtx lagg_list_mtx;
81 eventhandler_tag lagg_detach_cookie = NULL;
83 static int lagg_clone_create(struct if_clone *, int, caddr_t);
84 static void lagg_clone_destroy(struct ifnet *);
85 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
86 static void lagg_capabilities(struct lagg_softc *);
87 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
88 static void lagg_port_setlladdr(void *, int);
89 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
90 static int lagg_port_destroy(struct lagg_port *, int);
91 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
92 static void lagg_linkstate(struct lagg_softc *);
93 static void lagg_port_state(struct ifnet *, int);
94 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
95 static int lagg_port_output(struct ifnet *, struct mbuf *,
96 struct sockaddr *, struct rtentry *);
97 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
98 static int lagg_port_checkstacking(struct lagg_softc *);
99 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
100 static void lagg_init(void *);
101 static void lagg_stop(struct lagg_softc *);
102 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
103 static int lagg_ether_setmulti(struct lagg_softc *);
104 static int lagg_ether_cmdmulti(struct lagg_port *, int);
105 static int lagg_setflag(struct lagg_port *, int, int,
106 int (*func)(struct ifnet *, int));
107 static int lagg_setflags(struct lagg_port *, int status);
108 static void lagg_start(struct ifnet *);
109 static int lagg_media_change(struct ifnet *);
110 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
111 static struct lagg_port *lagg_link_active(struct lagg_softc *,
113 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
115 IFC_SIMPLE_DECLARE(lagg, 0);
117 /* Simple round robin */
118 static int lagg_rr_attach(struct lagg_softc *);
119 static int lagg_rr_detach(struct lagg_softc *);
120 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
121 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
124 /* Active failover */
125 static int lagg_fail_attach(struct lagg_softc *);
126 static int lagg_fail_detach(struct lagg_softc *);
127 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
128 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
132 static int lagg_lb_attach(struct lagg_softc *);
133 static int lagg_lb_detach(struct lagg_softc *);
134 static int lagg_lb_port_create(struct lagg_port *);
135 static void lagg_lb_port_destroy(struct lagg_port *);
136 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
137 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
139 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
142 static int lagg_lacp_attach(struct lagg_softc *);
143 static int lagg_lacp_detach(struct lagg_softc *);
144 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
145 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
147 static void lagg_lacp_lladdr(struct lagg_softc *);
149 /* lagg protocol table */
150 static const struct {
152 int (*ti_attach)(struct lagg_softc *);
154 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
155 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
156 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
157 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
158 { LAGG_PROTO_LACP, lagg_lacp_attach },
159 { LAGG_PROTO_NONE, NULL }
163 lagg_modevent(module_t mod, int type, void *data)
168 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
169 SLIST_INIT(&lagg_list);
170 if_clone_attach(&lagg_cloner);
171 lagg_input_p = lagg_input;
172 lagg_linkstate_p = lagg_port_state;
173 lagg_detach_cookie = EVENTHANDLER_REGISTER(
174 ifnet_departure_event, lagg_port_ifdetach, NULL,
175 EVENTHANDLER_PRI_ANY);
178 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
180 if_clone_detach(&lagg_cloner);
182 lagg_linkstate_p = NULL;
183 mtx_destroy(&lagg_list_mtx);
191 static moduledata_t lagg_mod = {
197 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
200 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
202 struct lagg_softc *sc;
205 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
207 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
208 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
214 sc->sc_proto = LAGG_PROTO_NONE;
215 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
216 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
217 sc->sc_proto = lagg_protos[i].ti_proto;
218 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
219 if_free_type(ifp, IFT_ETHER);
227 SLIST_INIT(&sc->sc_ports);
228 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
230 /* Initialise pseudo media types */
231 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
233 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
234 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
236 if_initname(ifp, ifc->ifc_name, unit);
237 ifp->if_type = IFT_ETHER;
239 ifp->if_start = lagg_start;
240 ifp->if_init = lagg_init;
241 ifp->if_ioctl = lagg_ioctl;
242 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
244 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
245 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
246 IFQ_SET_READY(&ifp->if_snd);
249 * Attach as an ordinary ethernet device, childs will be attached
250 * as special device IFT_IEEE8023ADLAG.
252 ether_ifattach(ifp, eaddr);
254 /* Insert into the global list of laggs */
255 mtx_lock(&lagg_list_mtx);
256 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
257 mtx_unlock(&lagg_list_mtx);
263 lagg_clone_destroy(struct ifnet *ifp)
265 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
266 struct lagg_port *lp;
271 ifp->if_flags &= ~IFF_UP;
273 /* Shutdown and remove lagg ports */
274 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
275 lagg_port_destroy(lp, 1);
276 /* Unhook the aggregation protocol */
277 (*sc->sc_detach)(sc);
281 ifmedia_removeall(&sc->sc_media);
283 if_free_type(ifp, IFT_ETHER);
285 mtx_lock(&lagg_list_mtx);
286 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
287 mtx_unlock(&lagg_list_mtx);
289 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
290 LAGG_LOCK_DESTROY(sc);
295 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
297 struct ifnet *ifp = sc->sc_ifp;
299 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
302 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
303 /* Let the protocol know the MAC has changed */
304 if (sc->sc_lladdr != NULL)
305 (*sc->sc_lladdr)(sc);
309 lagg_capabilities(struct lagg_softc *sc)
311 struct lagg_port *lp;
312 int cap = ~0, ena = ~0;
314 LAGG_WLOCK_ASSERT(sc);
316 /* Get capabilities from the lagg ports */
317 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
318 cap &= lp->lp_ifp->if_capabilities;
319 ena &= lp->lp_ifp->if_capenable;
321 cap = (cap == ~0 ? 0 : cap);
322 ena = (ena == ~0 ? 0 : ena);
324 if (sc->sc_ifp->if_capabilities != cap ||
325 sc->sc_ifp->if_capenable != ena) {
326 sc->sc_ifp->if_capabilities = cap;
327 sc->sc_ifp->if_capenable = ena;
328 getmicrotime(&sc->sc_ifp->if_lastchange);
330 if (sc->sc_ifflags & IFF_DEBUG)
331 if_printf(sc->sc_ifp,
332 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
337 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
339 struct lagg_softc *sc = lp->lp_softc;
340 struct ifnet *ifp = lp->lp_ifp;
341 struct lagg_llq *llq;
344 LAGG_WLOCK_ASSERT(sc);
346 if (lp->lp_detaching ||
347 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
350 /* Check to make sure its not already queued to be changed */
351 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
352 if (llq->llq_ifp == ifp) {
359 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
360 if (llq == NULL) /* XXX what to do */
364 /* Update the lladdr even if pending, it may have changed */
366 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
369 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
371 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
375 * Set the interface MAC address from a taskqueue to avoid a LOR.
378 lagg_port_setlladdr(void *arg, int pending)
380 struct lagg_softc *sc = (struct lagg_softc *)arg;
381 struct lagg_llq *llq, *head;
385 /* Grab a local reference of the queue and remove it from the softc */
387 head = SLIST_FIRST(&sc->sc_llq_head);
388 SLIST_FIRST(&sc->sc_llq_head) = NULL;
392 * Traverse the queue and set the lladdr on each ifp. It is safe to do
393 * unlocked as we have the only reference to it.
395 for (llq = head; llq != NULL; llq = head) {
398 /* Set the link layer address */
399 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
401 printf("%s: setlladdr failed on %s\n", __func__,
404 head = SLIST_NEXT(llq, llq_entries);
410 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
412 struct lagg_softc *sc_ptr;
413 struct lagg_port *lp;
416 LAGG_WLOCK_ASSERT(sc);
418 /* Limit the maximal number of lagg ports */
419 if (sc->sc_count >= LAGG_MAX_PORTS)
422 /* New lagg port has to be in an idle state */
423 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
426 /* Check if port has already been associated to a lagg */
427 if (ifp->if_lagg != NULL)
430 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
431 if (ifp->if_type != IFT_ETHER)
432 return (EPROTONOSUPPORT);
434 /* Allow the first Ethernet member to define the MTU */
435 if (SLIST_EMPTY(&sc->sc_ports))
436 sc->sc_ifp->if_mtu = ifp->if_mtu;
437 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
438 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
443 if ((lp = malloc(sizeof(struct lagg_port),
444 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
447 /* Check if port is a stacked lagg */
448 mtx_lock(&lagg_list_mtx);
449 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
450 if (ifp == sc_ptr->sc_ifp) {
451 mtx_unlock(&lagg_list_mtx);
454 /* XXX disable stacking for the moment, its untested
455 lp->lp_flags |= LAGG_PORT_STACK;
456 if (lagg_port_checkstacking(sc_ptr) >=
458 mtx_unlock(&lagg_list_mtx);
465 mtx_unlock(&lagg_list_mtx);
467 /* Change the interface type */
468 lp->lp_iftype = ifp->if_type;
469 ifp->if_type = IFT_IEEE8023ADLAG;
471 lp->lp_ioctl = ifp->if_ioctl;
472 ifp->if_ioctl = lagg_port_ioctl;
473 lp->lp_output = ifp->if_output;
474 ifp->if_output = lagg_port_output;
479 /* Save port link layer address */
480 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
482 if (SLIST_EMPTY(&sc->sc_ports)) {
484 lagg_lladdr(sc, IF_LLADDR(ifp));
486 /* Update link layer address for this port */
487 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
490 /* Insert into the list of ports */
491 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
494 /* Update lagg capabilities */
495 lagg_capabilities(sc);
498 /* Add multicast addresses and interface flags to this port */
499 lagg_ether_cmdmulti(lp, 1);
500 lagg_setflags(lp, 1);
502 if (sc->sc_port_create != NULL)
503 error = (*sc->sc_port_create)(lp);
505 /* remove the port again, without calling sc_port_destroy */
506 lagg_port_destroy(lp, 0);
514 lagg_port_checkstacking(struct lagg_softc *sc)
516 struct lagg_softc *sc_ptr;
517 struct lagg_port *lp;
520 LAGG_WLOCK_ASSERT(sc);
522 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
523 if (lp->lp_flags & LAGG_PORT_STACK) {
524 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
525 m = MAX(m, lagg_port_checkstacking(sc_ptr));
533 lagg_port_destroy(struct lagg_port *lp, int runpd)
535 struct lagg_softc *sc = lp->lp_softc;
536 struct lagg_port *lp_ptr;
537 struct lagg_llq *llq;
538 struct ifnet *ifp = lp->lp_ifp;
540 LAGG_WLOCK_ASSERT(sc);
542 if (runpd && sc->sc_port_destroy != NULL)
543 (*sc->sc_port_destroy)(lp);
546 * Remove multicast addresses and interface flags from this port and
547 * reset the MAC address, skip if the interface is being detached.
549 if (!lp->lp_detaching) {
550 lagg_ether_cmdmulti(lp, 0);
551 lagg_setflags(lp, 0);
552 lagg_port_lladdr(lp, lp->lp_lladdr);
555 /* Restore interface */
556 ifp->if_type = lp->lp_iftype;
557 ifp->if_ioctl = lp->lp_ioctl;
558 ifp->if_output = lp->lp_output;
561 /* Finally, remove the port from the lagg */
562 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
565 /* Update the primary interface */
566 if (lp == sc->sc_primary) {
567 uint8_t lladdr[ETHER_ADDR_LEN];
569 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
570 bzero(&lladdr, ETHER_ADDR_LEN);
572 bcopy(lp_ptr->lp_lladdr,
573 lladdr, ETHER_ADDR_LEN);
575 lagg_lladdr(sc, lladdr);
576 sc->sc_primary = lp_ptr;
578 /* Update link layer address for each port */
579 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
580 lagg_port_lladdr(lp_ptr, lladdr);
583 /* Remove any pending lladdr changes from the queue */
584 if (lp->lp_detaching) {
585 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
586 if (llq->llq_ifp == ifp) {
587 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
590 break; /* Only appears once */
596 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
600 /* Update lagg capabilities */
601 lagg_capabilities(sc);
608 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
610 struct lagg_reqport *rp = (struct lagg_reqport *)data;
611 struct lagg_softc *sc;
612 struct lagg_port *lp = NULL;
615 /* Should be checked by the caller */
616 if (ifp->if_type != IFT_IEEE8023ADLAG ||
617 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
622 if (rp->rp_portname[0] == '\0' ||
623 ifunit(rp->rp_portname) != ifp) {
629 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
635 lagg_port2req(lp, rp);
640 if (lp->lp_ioctl == NULL) {
644 error = (*lp->lp_ioctl)(ifp, cmd, data);
648 /* Update lagg interface capabilities */
650 lagg_capabilities(sc);
655 /* Do not allow the MTU to be changed once joined */
666 if (lp->lp_ioctl != NULL)
667 return ((*lp->lp_ioctl)(ifp, cmd, data));
673 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
674 struct sockaddr *dst, struct rtentry *rt0)
676 struct lagg_port *lp = ifp->if_lagg;
677 struct ether_header *eh;
680 switch (dst->sa_family) {
681 case pseudo_AF_HDRCMPLT:
683 eh = (struct ether_header *)dst->sa_data;
684 type = eh->ether_type;
689 * Only allow ethernet types required to initiate or maintain the link,
690 * aggregated frames take a different path.
692 switch (ntohs(type)) {
693 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
694 return ((*lp->lp_output)(ifp, m, dst, rt0));
697 /* drop any other frames */
703 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
705 struct lagg_port *lp;
706 struct lagg_softc *sc;
708 if ((lp = ifp->if_lagg) == NULL)
714 lp->lp_detaching = 1;
715 lagg_port_destroy(lp, 1);
720 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
722 struct lagg_softc *sc = lp->lp_softc;
724 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
725 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
726 rp->rp_prio = lp->lp_prio;
727 rp->rp_flags = lp->lp_flags;
728 if (sc->sc_portreq != NULL)
729 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
731 /* Add protocol specific flags */
732 switch (sc->sc_proto) {
733 case LAGG_PROTO_FAILOVER:
734 if (lp == sc->sc_primary)
735 rp->rp_flags |= LAGG_PORT_MASTER;
736 if (lp == lagg_link_active(sc, sc->sc_primary))
737 rp->rp_flags |= LAGG_PORT_ACTIVE;
740 case LAGG_PROTO_ROUNDROBIN:
741 case LAGG_PROTO_LOADBALANCE:
742 case LAGG_PROTO_ETHERCHANNEL:
743 if (LAGG_PORTACTIVE(lp))
744 rp->rp_flags |= LAGG_PORT_ACTIVE;
747 case LAGG_PROTO_LACP:
748 /* LACP has a different definition of active */
749 if (lacp_isactive(lp))
750 rp->rp_flags |= LAGG_PORT_ACTIVE;
751 if (lacp_iscollecting(lp))
752 rp->rp_flags |= LAGG_PORT_COLLECTING;
753 if (lacp_isdistributing(lp))
754 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
763 struct lagg_softc *sc = (struct lagg_softc *)xsc;
764 struct lagg_port *lp;
765 struct ifnet *ifp = sc->sc_ifp;
767 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
772 ifp->if_drv_flags |= IFF_DRV_RUNNING;
773 /* Update the port lladdrs */
774 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
775 lagg_port_lladdr(lp, IF_LLADDR(ifp));
777 if (sc->sc_init != NULL)
784 lagg_stop(struct lagg_softc *sc)
786 struct ifnet *ifp = sc->sc_ifp;
788 LAGG_WLOCK_ASSERT(sc);
790 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
793 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
795 if (sc->sc_stop != NULL)
800 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
802 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
803 struct lagg_reqall *ra = (struct lagg_reqall *)data;
804 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
805 struct ifreq *ifr = (struct ifreq *)data;
806 struct lagg_port *lp;
808 struct thread *td = curthread;
810 int count, buflen, len, error = 0;
812 bzero(&rpbuf, sizeof(rpbuf));
818 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
820 buflen = count * sizeof(struct lagg_reqport);
823 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
826 ra->ra_proto = sc->sc_proto;
827 if (sc->sc_req != NULL)
828 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
832 len = min(ra->ra_size, buflen);
833 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
834 if (len < sizeof(rpbuf))
837 lagg_port2req(lp, &rpbuf);
838 memcpy(buf, &rpbuf, sizeof(rpbuf));
840 buf += sizeof(rpbuf);
841 len -= sizeof(rpbuf);
844 ra->ra_ports = count;
845 ra->ra_size = count * sizeof(rpbuf);
846 error = copyout(outbuf, ra->ra_port, ra->ra_size);
847 free(outbuf, M_TEMP);
850 error = priv_check(td, PRIV_NET_LAGG);
853 if (ra->ra_proto >= LAGG_PROTO_MAX) {
854 error = EPROTONOSUPPORT;
857 if (sc->sc_proto != LAGG_PROTO_NONE) {
859 error = sc->sc_detach(sc);
860 /* Reset protocol and pointers */
861 sc->sc_proto = LAGG_PROTO_NONE;
862 sc->sc_detach = NULL;
865 sc->sc_port_create = NULL;
866 sc->sc_port_destroy = NULL;
867 sc->sc_linkstate = NULL;
870 sc->sc_lladdr = NULL;
872 sc->sc_portreq = NULL;
877 for (int i = 0; i < (sizeof(lagg_protos) /
878 sizeof(lagg_protos[0])); i++) {
879 if (lagg_protos[i].ti_proto == ra->ra_proto) {
880 if (sc->sc_ifflags & IFF_DEBUG)
881 printf("%s: using proto %u\n",
883 lagg_protos[i].ti_proto);
885 sc->sc_proto = lagg_protos[i].ti_proto;
886 if (sc->sc_proto != LAGG_PROTO_NONE)
887 error = lagg_protos[i].ti_attach(sc);
892 error = EPROTONOSUPPORT;
895 if (rp->rp_portname[0] == '\0' ||
896 (tpif = ifunit(rp->rp_portname)) == NULL) {
902 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
903 lp->lp_softc != sc) {
909 lagg_port2req(lp, rp);
913 error = priv_check(td, PRIV_NET_LAGG);
916 if (rp->rp_portname[0] == '\0' ||
917 (tpif = ifunit(rp->rp_portname)) == NULL) {
922 error = lagg_port_create(sc, tpif);
925 case SIOCSLAGGDELPORT:
926 error = priv_check(td, PRIV_NET_LAGG);
929 if (rp->rp_portname[0] == '\0' ||
930 (tpif = ifunit(rp->rp_portname)) == NULL) {
936 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
937 lp->lp_softc != sc) {
943 error = lagg_port_destroy(lp, 1);
947 /* Set flags on ports too */
949 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
950 lagg_setflags(lp, 1);
954 if (!(ifp->if_flags & IFF_UP) &&
955 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
957 * If interface is marked down and it is running,
958 * then stop and disable it.
963 } else if ((ifp->if_flags & IFF_UP) &&
964 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
966 * If interface is marked up and it is stopped, then
975 error = lagg_ether_setmulti(sc);
980 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
985 /* Do not allow the MTU or caps to be directly changed */
990 error = ether_ioctl(ifp, cmd, data);
997 lagg_ether_setmulti(struct lagg_softc *sc)
999 struct lagg_port *lp;
1001 LAGG_WLOCK_ASSERT(sc);
1003 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1004 /* First, remove any existing filter entries. */
1005 lagg_ether_cmdmulti(lp, 0);
1006 /* copy all addresses from the lagg interface to the port */
1007 lagg_ether_cmdmulti(lp, 1);
1013 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1015 struct lagg_softc *sc = lp->lp_softc;
1016 struct ifnet *ifp = lp->lp_ifp;
1017 struct ifnet *scifp = sc->sc_ifp;
1019 struct ifmultiaddr *ifma, *rifma = NULL;
1020 struct sockaddr_dl sdl;
1023 LAGG_WLOCK_ASSERT(sc);
1025 bzero((char *)&sdl, sizeof(sdl));
1026 sdl.sdl_len = sizeof(sdl);
1027 sdl.sdl_family = AF_LINK;
1028 sdl.sdl_type = IFT_ETHER;
1029 sdl.sdl_alen = ETHER_ADDR_LEN;
1030 sdl.sdl_index = ifp->if_index;
1033 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1034 if (ifma->ifma_addr->sa_family != AF_LINK)
1036 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1037 LLADDR(&sdl), ETHER_ADDR_LEN);
1039 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1042 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1045 mc->mc_ifma = rifma;
1046 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1049 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1050 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1051 if_delmulti_ifma(mc->mc_ifma);
1058 /* Handle a ref counted flag that should be set on the lagg port as well */
1060 lagg_setflag(struct lagg_port *lp, int flag, int status,
1061 int (*func)(struct ifnet *, int))
1063 struct lagg_softc *sc = lp->lp_softc;
1064 struct ifnet *scifp = sc->sc_ifp;
1065 struct ifnet *ifp = lp->lp_ifp;
1068 LAGG_WLOCK_ASSERT(sc);
1070 status = status ? (scifp->if_flags & flag) : 0;
1071 /* Now "status" contains the flag value or 0 */
1074 * See if recorded ports status is different from what
1075 * we want it to be. If it is, flip it. We record ports
1076 * status in lp_ifflags so that we won't clear ports flag
1077 * we haven't set. In fact, we don't clear or set ports
1078 * flags directly, but get or release references to them.
1079 * That's why we can be sure that recorded flags still are
1080 * in accord with actual ports flags.
1082 if (status != (lp->lp_ifflags & flag)) {
1083 error = (*func)(ifp, status);
1086 lp->lp_ifflags &= ~flag;
1087 lp->lp_ifflags |= status;
1093 * Handle IFF_* flags that require certain changes on the lagg port
1094 * if "status" is true, update ports flags respective to the lagg
1095 * if "status" is false, forcedly clear the flags set on port.
1098 lagg_setflags(struct lagg_port *lp, int status)
1102 for (i = 0; lagg_pflags[i].flag; i++) {
1103 error = lagg_setflag(lp, lagg_pflags[i].flag,
1104 status, lagg_pflags[i].func);
1112 lagg_start(struct ifnet *ifp)
1114 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1119 for (;; error = 0) {
1120 IFQ_DEQUEUE(&ifp->if_snd, m);
1124 ETHER_BPF_MTAP(ifp, m);
1126 /* We need a Tx algorithm and at least one port */
1127 if (sc->sc_proto != LAGG_PROTO_NONE && sc->sc_count)
1128 error = (*sc->sc_start)(sc, m);
1142 static struct mbuf *
1143 lagg_input(struct ifnet *ifp, struct mbuf *m)
1145 struct lagg_port *lp = ifp->if_lagg;
1146 struct lagg_softc *sc = lp->lp_softc;
1147 struct ifnet *scifp = sc->sc_ifp;
1149 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1150 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1151 sc->sc_proto == LAGG_PROTO_NONE) {
1157 ETHER_BPF_MTAP(scifp, m);
1159 m = (*sc->sc_input)(sc, lp, m);
1162 scifp->if_ipackets++;
1163 scifp->if_ibytes += m->m_pkthdr.len;
1165 if (scifp->if_flags & IFF_MONITOR) {
1176 lagg_media_change(struct ifnet *ifp)
1178 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1180 if (sc->sc_ifflags & IFF_DEBUG)
1181 printf("%s\n", __func__);
1188 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1190 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1191 struct lagg_port *lp;
1193 imr->ifm_status = IFM_AVALID;
1194 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1197 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1198 if (LAGG_PORTACTIVE(lp))
1199 imr->ifm_status |= IFM_ACTIVE;
1205 lagg_linkstate(struct lagg_softc *sc)
1207 struct lagg_port *lp;
1208 int new_link = LINK_STATE_DOWN;
1210 /* Our link is considered up if at least one of our ports is active */
1211 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1212 if (lp->lp_link_state == LINK_STATE_UP) {
1213 new_link = LINK_STATE_UP;
1217 if_link_state_change(sc->sc_ifp, new_link);
1221 lagg_port_state(struct ifnet *ifp, int state)
1223 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1224 struct lagg_softc *sc = NULL;
1233 if (sc->sc_linkstate != NULL)
1234 (*sc->sc_linkstate)(lp);
1239 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1241 struct lagg_port *lp_next, *rval = NULL;
1242 // int new_link = LINK_STATE_DOWN;
1244 LAGG_RLOCK_ASSERT(sc);
1246 * Search a port which reports an active link state.
1251 if (LAGG_PORTACTIVE(lp)) {
1255 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1256 LAGG_PORTACTIVE(lp_next)) {
1262 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1263 if (LAGG_PORTACTIVE(lp_next)) {
1272 * The IEEE 802.1D standard assumes that a lagg with
1273 * multiple ports is always full duplex. This is valid
1274 * for load sharing laggs and if at least two links
1275 * are active. Unfortunately, checking the latter would
1276 * be too expensive at this point.
1278 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1280 new_link = LINK_STATE_FULL_DUPLEX;
1282 new_link = rval->lp_link_state;
1290 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1292 if (m->m_pkthdr.len < (off + len)) {
1294 } else if (m->m_len < (off + len)) {
1295 m_copydata(m, off, len, buf);
1298 return (mtod(m, char *) + off);
1302 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1307 struct ether_header *eh;
1308 struct ether_vlan_header vlanbuf;
1309 const struct ether_vlan_header *vlan;
1311 const struct ip *ip;
1315 const struct ip6_hdr *ip6;
1316 struct ip6_hdr ip6buf;
1323 eh = mtod(m, struct ether_header *);
1324 etype = ntohs(eh->ether_type);
1325 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1326 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1328 /* Special handling for encapsulating VLAN frames */
1329 if (m->m_flags & M_VLANTAG) {
1330 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1331 sizeof(m->m_pkthdr.ether_vtag), p);
1332 } else if (etype == ETHERTYPE_VLAN) {
1333 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1337 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1338 etype = ntohs(vlan->evl_proto);
1339 off += sizeof(*vlan) - sizeof(*eh);
1345 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1349 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1350 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1354 case ETHERTYPE_IPV6:
1355 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1359 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1360 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1361 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1362 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1371 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1375 IFQ_HANDOFF(ifp, m, error);
1382 * Simple round robin aggregation
1386 lagg_rr_attach(struct lagg_softc *sc)
1388 sc->sc_detach = lagg_rr_detach;
1389 sc->sc_start = lagg_rr_start;
1390 sc->sc_input = lagg_rr_input;
1391 sc->sc_port_create = NULL;
1392 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1399 lagg_rr_detach(struct lagg_softc *sc)
1405 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1407 struct lagg_port *lp;
1410 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1412 lp = SLIST_FIRST(&sc->sc_ports);
1414 lp = SLIST_NEXT(lp, lp_entries);
1417 * Check the port's link state. This will return the next active
1418 * port if the link is down or the port is NULL.
1420 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1426 return (lagg_enqueue(lp->lp_ifp, m));
1429 static struct mbuf *
1430 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1432 struct ifnet *ifp = sc->sc_ifp;
1434 /* Just pass in the packet to our lagg device */
1435 m->m_pkthdr.rcvif = ifp;
1445 lagg_fail_attach(struct lagg_softc *sc)
1447 sc->sc_detach = lagg_fail_detach;
1448 sc->sc_start = lagg_fail_start;
1449 sc->sc_input = lagg_fail_input;
1450 sc->sc_port_create = NULL;
1451 sc->sc_port_destroy = NULL;
1457 lagg_fail_detach(struct lagg_softc *sc)
1463 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1465 struct lagg_port *lp;
1467 /* Use the master port if active or the next available port */
1468 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1474 return (lagg_enqueue(lp->lp_ifp, m));
1477 static struct mbuf *
1478 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1480 struct ifnet *ifp = sc->sc_ifp;
1481 struct lagg_port *tmp_tp;
1483 if (lp == sc->sc_primary) {
1484 m->m_pkthdr.rcvif = ifp;
1488 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1489 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1491 * If tmp_tp is null, we've recieved a packet when all
1492 * our links are down. Weird, but process it anyways.
1494 if ((tmp_tp == NULL || tmp_tp == lp)) {
1495 m->m_pkthdr.rcvif = ifp;
1509 lagg_lb_attach(struct lagg_softc *sc)
1511 struct lagg_port *lp;
1514 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1515 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1518 sc->sc_detach = lagg_lb_detach;
1519 sc->sc_start = lagg_lb_start;
1520 sc->sc_input = lagg_lb_input;
1521 sc->sc_port_create = lagg_lb_port_create;
1522 sc->sc_port_destroy = lagg_lb_port_destroy;
1523 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1525 lb->lb_key = arc4random();
1526 sc->sc_psc = (caddr_t)lb;
1528 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1529 lagg_lb_port_create(lp);
1535 lagg_lb_detach(struct lagg_softc *sc)
1537 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1544 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1546 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1547 struct lagg_port *lp_next;
1550 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1551 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1554 if (i >= LAGG_MAX_PORTS)
1556 if (sc->sc_ifflags & IFF_DEBUG)
1557 printf("%s: port %s at index %d\n",
1558 sc->sc_ifname, lp_next->lp_ifname, i);
1559 lb->lb_ports[i++] = lp_next;
1566 lagg_lb_port_create(struct lagg_port *lp)
1568 struct lagg_softc *sc = lp->lp_softc;
1569 return (lagg_lb_porttable(sc, NULL));
1573 lagg_lb_port_destroy(struct lagg_port *lp)
1575 struct lagg_softc *sc = lp->lp_softc;
1576 lagg_lb_porttable(sc, lp);
1580 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1582 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1583 struct lagg_port *lp = NULL;
1586 p = lagg_hashmbuf(m, lb->lb_key);
1588 lp = lb->lb_ports[p];
1591 * Check the port's link state. This will return the next active
1592 * port if the link is down or the port is NULL.
1594 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1600 return (lagg_enqueue(lp->lp_ifp, m));
1603 static struct mbuf *
1604 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1606 struct ifnet *ifp = sc->sc_ifp;
1608 /* Just pass in the packet to our lagg device */
1609 m->m_pkthdr.rcvif = ifp;
1619 lagg_lacp_attach(struct lagg_softc *sc)
1621 struct lagg_port *lp;
1624 sc->sc_detach = lagg_lacp_detach;
1625 sc->sc_port_create = lacp_port_create;
1626 sc->sc_port_destroy = lacp_port_destroy;
1627 sc->sc_linkstate = lacp_linkstate;
1628 sc->sc_start = lagg_lacp_start;
1629 sc->sc_input = lagg_lacp_input;
1630 sc->sc_init = lacp_init;
1631 sc->sc_stop = lacp_stop;
1632 sc->sc_lladdr = lagg_lacp_lladdr;
1633 sc->sc_req = lacp_req;
1634 sc->sc_portreq = lacp_portreq;
1636 error = lacp_attach(sc);
1640 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1641 lacp_port_create(lp);
1647 lagg_lacp_detach(struct lagg_softc *sc)
1649 struct lagg_port *lp;
1652 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1653 lacp_port_destroy(lp);
1655 /* unlocking is safe here */
1657 error = lacp_detach(sc);
1664 lagg_lacp_lladdr(struct lagg_softc *sc)
1666 struct lagg_port *lp;
1668 /* purge all the lacp ports */
1669 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1670 lacp_port_destroy(lp);
1672 /* add them back in */
1673 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1674 lacp_port_create(lp);
1678 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1680 struct lagg_port *lp;
1682 lp = lacp_select_tx_port(sc, m);
1689 return (lagg_enqueue(lp->lp_ifp, m));
1692 static struct mbuf *
1693 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1695 struct ifnet *ifp = sc->sc_ifp;
1696 struct ether_header *eh;
1699 eh = mtod(m, struct ether_header *);
1700 etype = ntohs(eh->ether_type);
1702 /* Tap off LACP control messages */
1703 if (etype == ETHERTYPE_SLOW) {
1704 m = lacp_input(lp, m);
1710 * If the port is not collecting or not in the active aggregator then
1713 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1718 m->m_pkthdr.rcvif = ifp;