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 route *);
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);
306 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
310 lagg_capabilities(struct lagg_softc *sc)
312 struct lagg_port *lp;
313 int cap = ~0, ena = ~0;
316 LAGG_WLOCK_ASSERT(sc);
318 /* Get capabilities from the lagg ports */
319 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
320 cap &= lp->lp_ifp->if_capabilities;
321 ena &= lp->lp_ifp->if_capenable;
322 hwa &= lp->lp_ifp->if_hwassist;
324 cap = (cap == ~0 ? 0 : cap);
325 ena = (ena == ~0 ? 0 : ena);
326 hwa = (hwa == ~0 ? 0 : hwa);
328 if (sc->sc_ifp->if_capabilities != cap ||
329 sc->sc_ifp->if_capenable != ena ||
330 sc->sc_ifp->if_hwassist != hwa) {
331 sc->sc_ifp->if_capabilities = cap;
332 sc->sc_ifp->if_capenable = ena;
333 sc->sc_ifp->if_hwassist = hwa;
334 getmicrotime(&sc->sc_ifp->if_lastchange);
336 if (sc->sc_ifflags & IFF_DEBUG)
337 if_printf(sc->sc_ifp,
338 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
343 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
345 struct lagg_softc *sc = lp->lp_softc;
346 struct ifnet *ifp = lp->lp_ifp;
347 struct lagg_llq *llq;
350 LAGG_WLOCK_ASSERT(sc);
352 if (lp->lp_detaching ||
353 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
356 /* Check to make sure its not already queued to be changed */
357 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
358 if (llq->llq_ifp == ifp) {
365 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
366 if (llq == NULL) /* XXX what to do */
370 /* Update the lladdr even if pending, it may have changed */
372 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
375 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
377 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
381 * Set the interface MAC address from a taskqueue to avoid a LOR.
384 lagg_port_setlladdr(void *arg, int pending)
386 struct lagg_softc *sc = (struct lagg_softc *)arg;
387 struct lagg_llq *llq, *head;
391 /* Grab a local reference of the queue and remove it from the softc */
393 head = SLIST_FIRST(&sc->sc_llq_head);
394 SLIST_FIRST(&sc->sc_llq_head) = NULL;
398 * Traverse the queue and set the lladdr on each ifp. It is safe to do
399 * unlocked as we have the only reference to it.
401 for (llq = head; llq != NULL; llq = head) {
404 /* Set the link layer address */
405 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
407 printf("%s: setlladdr failed on %s\n", __func__,
410 head = SLIST_NEXT(llq, llq_entries);
416 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
418 struct lagg_softc *sc_ptr;
419 struct lagg_port *lp;
422 LAGG_WLOCK_ASSERT(sc);
424 /* Limit the maximal number of lagg ports */
425 if (sc->sc_count >= LAGG_MAX_PORTS)
428 /* Check if port has already been associated to a lagg */
429 if (ifp->if_lagg != NULL)
432 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
433 if (ifp->if_type != IFT_ETHER)
434 return (EPROTONOSUPPORT);
436 /* Allow the first Ethernet member to define the MTU */
437 if (SLIST_EMPTY(&sc->sc_ports))
438 sc->sc_ifp->if_mtu = ifp->if_mtu;
439 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
440 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
445 if ((lp = malloc(sizeof(struct lagg_port),
446 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
449 /* Check if port is a stacked lagg */
450 mtx_lock(&lagg_list_mtx);
451 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
452 if (ifp == sc_ptr->sc_ifp) {
453 mtx_unlock(&lagg_list_mtx);
456 /* XXX disable stacking for the moment, its untested
457 lp->lp_flags |= LAGG_PORT_STACK;
458 if (lagg_port_checkstacking(sc_ptr) >=
460 mtx_unlock(&lagg_list_mtx);
467 mtx_unlock(&lagg_list_mtx);
469 /* Change the interface type */
470 lp->lp_iftype = ifp->if_type;
471 ifp->if_type = IFT_IEEE8023ADLAG;
473 lp->lp_ioctl = ifp->if_ioctl;
474 ifp->if_ioctl = lagg_port_ioctl;
475 lp->lp_output = ifp->if_output;
476 ifp->if_output = lagg_port_output;
481 /* Save port link layer address */
482 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
484 if (SLIST_EMPTY(&sc->sc_ports)) {
486 lagg_lladdr(sc, IF_LLADDR(ifp));
488 /* Update link layer address for this port */
489 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
492 /* Insert into the list of ports */
493 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
496 /* Update lagg capabilities */
497 lagg_capabilities(sc);
500 /* Add multicast addresses and interface flags to this port */
501 lagg_ether_cmdmulti(lp, 1);
502 lagg_setflags(lp, 1);
504 if (sc->sc_port_create != NULL)
505 error = (*sc->sc_port_create)(lp);
507 /* remove the port again, without calling sc_port_destroy */
508 lagg_port_destroy(lp, 0);
516 lagg_port_checkstacking(struct lagg_softc *sc)
518 struct lagg_softc *sc_ptr;
519 struct lagg_port *lp;
522 LAGG_WLOCK_ASSERT(sc);
524 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
525 if (lp->lp_flags & LAGG_PORT_STACK) {
526 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
527 m = MAX(m, lagg_port_checkstacking(sc_ptr));
535 lagg_port_destroy(struct lagg_port *lp, int runpd)
537 struct lagg_softc *sc = lp->lp_softc;
538 struct lagg_port *lp_ptr;
539 struct lagg_llq *llq;
540 struct ifnet *ifp = lp->lp_ifp;
542 LAGG_WLOCK_ASSERT(sc);
544 if (runpd && sc->sc_port_destroy != NULL)
545 (*sc->sc_port_destroy)(lp);
548 * Remove multicast addresses and interface flags from this port and
549 * reset the MAC address, skip if the interface is being detached.
551 if (!lp->lp_detaching) {
552 lagg_ether_cmdmulti(lp, 0);
553 lagg_setflags(lp, 0);
554 lagg_port_lladdr(lp, lp->lp_lladdr);
557 /* Restore interface */
558 ifp->if_type = lp->lp_iftype;
559 ifp->if_ioctl = lp->lp_ioctl;
560 ifp->if_output = lp->lp_output;
563 /* Finally, remove the port from the lagg */
564 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
567 /* Update the primary interface */
568 if (lp == sc->sc_primary) {
569 uint8_t lladdr[ETHER_ADDR_LEN];
571 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
572 bzero(&lladdr, ETHER_ADDR_LEN);
574 bcopy(lp_ptr->lp_lladdr,
575 lladdr, ETHER_ADDR_LEN);
577 lagg_lladdr(sc, lladdr);
578 sc->sc_primary = lp_ptr;
580 /* Update link layer address for each port */
581 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
582 lagg_port_lladdr(lp_ptr, lladdr);
585 /* Remove any pending lladdr changes from the queue */
586 if (lp->lp_detaching) {
587 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
588 if (llq->llq_ifp == ifp) {
589 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
592 break; /* Only appears once */
598 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
602 /* Update lagg capabilities */
603 lagg_capabilities(sc);
610 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
612 struct lagg_reqport *rp = (struct lagg_reqport *)data;
613 struct lagg_softc *sc;
614 struct lagg_port *lp = NULL;
617 /* Should be checked by the caller */
618 if (ifp->if_type != IFT_IEEE8023ADLAG ||
619 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
624 if (rp->rp_portname[0] == '\0' ||
625 ifunit(rp->rp_portname) != ifp) {
631 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
637 lagg_port2req(lp, rp);
642 if (lp->lp_ioctl == NULL) {
646 error = (*lp->lp_ioctl)(ifp, cmd, data);
650 /* Update lagg interface capabilities */
652 lagg_capabilities(sc);
657 /* Do not allow the MTU to be changed once joined */
668 if (lp->lp_ioctl != NULL)
669 return ((*lp->lp_ioctl)(ifp, cmd, data));
675 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
676 struct sockaddr *dst, struct route *ro)
678 struct lagg_port *lp = ifp->if_lagg;
679 struct ether_header *eh;
682 switch (dst->sa_family) {
683 case pseudo_AF_HDRCMPLT:
685 eh = (struct ether_header *)dst->sa_data;
686 type = eh->ether_type;
691 * Only allow ethernet types required to initiate or maintain the link,
692 * aggregated frames take a different path.
694 switch (ntohs(type)) {
695 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
696 return ((*lp->lp_output)(ifp, m, dst, ro));
699 /* drop any other frames */
705 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
707 struct lagg_port *lp;
708 struct lagg_softc *sc;
710 if ((lp = ifp->if_lagg) == NULL)
716 lp->lp_detaching = 1;
717 lagg_port_destroy(lp, 1);
722 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
724 struct lagg_softc *sc = lp->lp_softc;
726 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
727 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
728 rp->rp_prio = lp->lp_prio;
729 rp->rp_flags = lp->lp_flags;
730 if (sc->sc_portreq != NULL)
731 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
733 /* Add protocol specific flags */
734 switch (sc->sc_proto) {
735 case LAGG_PROTO_FAILOVER:
736 if (lp == sc->sc_primary)
737 rp->rp_flags |= LAGG_PORT_MASTER;
738 if (lp == lagg_link_active(sc, sc->sc_primary))
739 rp->rp_flags |= LAGG_PORT_ACTIVE;
742 case LAGG_PROTO_ROUNDROBIN:
743 case LAGG_PROTO_LOADBALANCE:
744 case LAGG_PROTO_ETHERCHANNEL:
745 if (LAGG_PORTACTIVE(lp))
746 rp->rp_flags |= LAGG_PORT_ACTIVE;
749 case LAGG_PROTO_LACP:
750 /* LACP has a different definition of active */
751 if (lacp_isactive(lp))
752 rp->rp_flags |= LAGG_PORT_ACTIVE;
753 if (lacp_iscollecting(lp))
754 rp->rp_flags |= LAGG_PORT_COLLECTING;
755 if (lacp_isdistributing(lp))
756 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
765 struct lagg_softc *sc = (struct lagg_softc *)xsc;
766 struct lagg_port *lp;
767 struct ifnet *ifp = sc->sc_ifp;
769 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
774 ifp->if_drv_flags |= IFF_DRV_RUNNING;
775 /* Update the port lladdrs */
776 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
777 lagg_port_lladdr(lp, IF_LLADDR(ifp));
779 if (sc->sc_init != NULL)
786 lagg_stop(struct lagg_softc *sc)
788 struct ifnet *ifp = sc->sc_ifp;
790 LAGG_WLOCK_ASSERT(sc);
792 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
795 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
797 if (sc->sc_stop != NULL)
802 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
804 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
805 struct lagg_reqall *ra = (struct lagg_reqall *)data;
806 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
807 struct ifreq *ifr = (struct ifreq *)data;
808 struct lagg_port *lp;
810 struct thread *td = curthread;
812 int count, buflen, len, error = 0;
814 bzero(&rpbuf, sizeof(rpbuf));
820 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
822 buflen = count * sizeof(struct lagg_reqport);
825 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
828 ra->ra_proto = sc->sc_proto;
829 if (sc->sc_req != NULL)
830 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
834 len = min(ra->ra_size, buflen);
835 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
836 if (len < sizeof(rpbuf))
839 lagg_port2req(lp, &rpbuf);
840 memcpy(buf, &rpbuf, sizeof(rpbuf));
842 buf += sizeof(rpbuf);
843 len -= sizeof(rpbuf);
846 ra->ra_ports = count;
847 ra->ra_size = count * sizeof(rpbuf);
848 error = copyout(outbuf, ra->ra_port, ra->ra_size);
849 free(outbuf, M_TEMP);
852 error = priv_check(td, PRIV_NET_LAGG);
855 if (ra->ra_proto >= LAGG_PROTO_MAX) {
856 error = EPROTONOSUPPORT;
859 if (sc->sc_proto != LAGG_PROTO_NONE) {
861 error = sc->sc_detach(sc);
862 /* Reset protocol and pointers */
863 sc->sc_proto = LAGG_PROTO_NONE;
864 sc->sc_detach = NULL;
867 sc->sc_port_create = NULL;
868 sc->sc_port_destroy = NULL;
869 sc->sc_linkstate = NULL;
872 sc->sc_lladdr = NULL;
874 sc->sc_portreq = NULL;
879 for (int i = 0; i < (sizeof(lagg_protos) /
880 sizeof(lagg_protos[0])); i++) {
881 if (lagg_protos[i].ti_proto == ra->ra_proto) {
882 if (sc->sc_ifflags & IFF_DEBUG)
883 printf("%s: using proto %u\n",
885 lagg_protos[i].ti_proto);
887 sc->sc_proto = lagg_protos[i].ti_proto;
888 if (sc->sc_proto != LAGG_PROTO_NONE)
889 error = lagg_protos[i].ti_attach(sc);
894 error = EPROTONOSUPPORT;
897 if (rp->rp_portname[0] == '\0' ||
898 (tpif = ifunit(rp->rp_portname)) == NULL) {
904 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
905 lp->lp_softc != sc) {
911 lagg_port2req(lp, rp);
915 error = priv_check(td, PRIV_NET_LAGG);
918 if (rp->rp_portname[0] == '\0' ||
919 (tpif = ifunit(rp->rp_portname)) == NULL) {
924 error = lagg_port_create(sc, tpif);
927 case SIOCSLAGGDELPORT:
928 error = priv_check(td, PRIV_NET_LAGG);
931 if (rp->rp_portname[0] == '\0' ||
932 (tpif = ifunit(rp->rp_portname)) == NULL) {
938 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
939 lp->lp_softc != sc) {
945 error = lagg_port_destroy(lp, 1);
949 /* Set flags on ports too */
951 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
952 lagg_setflags(lp, 1);
956 if (!(ifp->if_flags & IFF_UP) &&
957 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
959 * If interface is marked down and it is running,
960 * then stop and disable it.
965 } else if ((ifp->if_flags & IFF_UP) &&
966 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
968 * If interface is marked up and it is stopped, then
977 error = lagg_ether_setmulti(sc);
982 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
987 /* Do not allow the MTU or caps to be directly changed */
992 error = ether_ioctl(ifp, cmd, data);
999 lagg_ether_setmulti(struct lagg_softc *sc)
1001 struct lagg_port *lp;
1003 LAGG_WLOCK_ASSERT(sc);
1005 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1006 /* First, remove any existing filter entries. */
1007 lagg_ether_cmdmulti(lp, 0);
1008 /* copy all addresses from the lagg interface to the port */
1009 lagg_ether_cmdmulti(lp, 1);
1015 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1017 struct lagg_softc *sc = lp->lp_softc;
1018 struct ifnet *ifp = lp->lp_ifp;
1019 struct ifnet *scifp = sc->sc_ifp;
1021 struct ifmultiaddr *ifma, *rifma = NULL;
1022 struct sockaddr_dl sdl;
1025 LAGG_WLOCK_ASSERT(sc);
1027 bzero((char *)&sdl, sizeof(sdl));
1028 sdl.sdl_len = sizeof(sdl);
1029 sdl.sdl_family = AF_LINK;
1030 sdl.sdl_type = IFT_ETHER;
1031 sdl.sdl_alen = ETHER_ADDR_LEN;
1032 sdl.sdl_index = ifp->if_index;
1035 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1036 if (ifma->ifma_addr->sa_family != AF_LINK)
1038 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1039 LLADDR(&sdl), ETHER_ADDR_LEN);
1041 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1044 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1047 mc->mc_ifma = rifma;
1048 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1051 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1052 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1053 if_delmulti_ifma(mc->mc_ifma);
1060 /* Handle a ref counted flag that should be set on the lagg port as well */
1062 lagg_setflag(struct lagg_port *lp, int flag, int status,
1063 int (*func)(struct ifnet *, int))
1065 struct lagg_softc *sc = lp->lp_softc;
1066 struct ifnet *scifp = sc->sc_ifp;
1067 struct ifnet *ifp = lp->lp_ifp;
1070 LAGG_WLOCK_ASSERT(sc);
1072 status = status ? (scifp->if_flags & flag) : 0;
1073 /* Now "status" contains the flag value or 0 */
1076 * See if recorded ports status is different from what
1077 * we want it to be. If it is, flip it. We record ports
1078 * status in lp_ifflags so that we won't clear ports flag
1079 * we haven't set. In fact, we don't clear or set ports
1080 * flags directly, but get or release references to them.
1081 * That's why we can be sure that recorded flags still are
1082 * in accord with actual ports flags.
1084 if (status != (lp->lp_ifflags & flag)) {
1085 error = (*func)(ifp, status);
1088 lp->lp_ifflags &= ~flag;
1089 lp->lp_ifflags |= status;
1095 * Handle IFF_* flags that require certain changes on the lagg port
1096 * if "status" is true, update ports flags respective to the lagg
1097 * if "status" is false, forcedly clear the flags set on port.
1100 lagg_setflags(struct lagg_port *lp, int status)
1104 for (i = 0; lagg_pflags[i].flag; i++) {
1105 error = lagg_setflag(lp, lagg_pflags[i].flag,
1106 status, lagg_pflags[i].func);
1114 lagg_start(struct ifnet *ifp)
1116 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1121 /* We need a Tx algorithm and at least one port */
1122 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1123 IF_DRAIN(&ifp->if_snd);
1128 for (;; error = 0) {
1129 IFQ_DEQUEUE(&ifp->if_snd, m);
1133 ETHER_BPF_MTAP(ifp, m);
1135 error = (*sc->sc_start)(sc, m);
1144 static struct mbuf *
1145 lagg_input(struct ifnet *ifp, struct mbuf *m)
1147 struct lagg_port *lp = ifp->if_lagg;
1148 struct lagg_softc *sc = lp->lp_softc;
1149 struct ifnet *scifp = sc->sc_ifp;
1151 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1152 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1153 sc->sc_proto == LAGG_PROTO_NONE) {
1159 ETHER_BPF_MTAP(scifp, m);
1161 m = (*sc->sc_input)(sc, lp, m);
1164 scifp->if_ipackets++;
1165 scifp->if_ibytes += m->m_pkthdr.len;
1167 if (scifp->if_flags & IFF_MONITOR) {
1178 lagg_media_change(struct ifnet *ifp)
1180 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1182 if (sc->sc_ifflags & IFF_DEBUG)
1183 printf("%s\n", __func__);
1190 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1192 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1193 struct lagg_port *lp;
1195 imr->ifm_status = IFM_AVALID;
1196 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1199 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1200 if (LAGG_PORTACTIVE(lp))
1201 imr->ifm_status |= IFM_ACTIVE;
1207 lagg_linkstate(struct lagg_softc *sc)
1209 struct lagg_port *lp;
1210 int new_link = LINK_STATE_DOWN;
1213 /* Our link is considered up if at least one of our ports is active */
1214 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1215 if (lp->lp_link_state == LINK_STATE_UP) {
1216 new_link = LINK_STATE_UP;
1220 if_link_state_change(sc->sc_ifp, new_link);
1222 /* Update if_baudrate to reflect the max possible speed */
1223 switch (sc->sc_proto) {
1224 case LAGG_PROTO_FAILOVER:
1225 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1226 sc->sc_primary->lp_ifp->if_baudrate : 0;
1228 case LAGG_PROTO_ROUNDROBIN:
1229 case LAGG_PROTO_LOADBALANCE:
1230 case LAGG_PROTO_ETHERCHANNEL:
1232 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1233 speed += lp->lp_ifp->if_baudrate;
1234 sc->sc_ifp->if_baudrate = speed;
1236 case LAGG_PROTO_LACP:
1237 /* LACP updates if_baudrate itself */
1243 lagg_port_state(struct ifnet *ifp, int state)
1245 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1246 struct lagg_softc *sc = NULL;
1255 if (sc->sc_linkstate != NULL)
1256 (*sc->sc_linkstate)(lp);
1261 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1263 struct lagg_port *lp_next, *rval = NULL;
1264 // int new_link = LINK_STATE_DOWN;
1266 LAGG_RLOCK_ASSERT(sc);
1268 * Search a port which reports an active link state.
1273 if (LAGG_PORTACTIVE(lp)) {
1277 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1278 LAGG_PORTACTIVE(lp_next)) {
1284 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1285 if (LAGG_PORTACTIVE(lp_next)) {
1294 * The IEEE 802.1D standard assumes that a lagg with
1295 * multiple ports is always full duplex. This is valid
1296 * for load sharing laggs and if at least two links
1297 * are active. Unfortunately, checking the latter would
1298 * be too expensive at this point.
1300 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1302 new_link = LINK_STATE_FULL_DUPLEX;
1304 new_link = rval->lp_link_state;
1312 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1314 if (m->m_pkthdr.len < (off + len)) {
1316 } else if (m->m_len < (off + len)) {
1317 m_copydata(m, off, len, buf);
1320 return (mtod(m, char *) + off);
1324 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1329 struct ether_header *eh;
1330 struct ether_vlan_header vlanbuf;
1331 const struct ether_vlan_header *vlan;
1333 const struct ip *ip;
1337 const struct ip6_hdr *ip6;
1338 struct ip6_hdr ip6buf;
1345 eh = mtod(m, struct ether_header *);
1346 etype = ntohs(eh->ether_type);
1347 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1348 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1350 /* Special handling for encapsulating VLAN frames */
1351 if (m->m_flags & M_VLANTAG) {
1352 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1353 sizeof(m->m_pkthdr.ether_vtag), p);
1354 } else if (etype == ETHERTYPE_VLAN) {
1355 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1359 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1360 etype = ntohs(vlan->evl_proto);
1361 off += sizeof(*vlan) - sizeof(*eh);
1367 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1371 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1372 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1376 case ETHERTYPE_IPV6:
1377 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1381 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1382 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1383 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1384 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1393 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1396 return (ifp->if_transmit)(ifp, m);
1400 * Simple round robin aggregation
1404 lagg_rr_attach(struct lagg_softc *sc)
1406 sc->sc_detach = lagg_rr_detach;
1407 sc->sc_start = lagg_rr_start;
1408 sc->sc_input = lagg_rr_input;
1409 sc->sc_port_create = NULL;
1410 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1417 lagg_rr_detach(struct lagg_softc *sc)
1423 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1425 struct lagg_port *lp;
1428 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1430 lp = SLIST_FIRST(&sc->sc_ports);
1432 lp = SLIST_NEXT(lp, lp_entries);
1435 * Check the port's link state. This will return the next active
1436 * port if the link is down or the port is NULL.
1438 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1444 return (lagg_enqueue(lp->lp_ifp, m));
1447 static struct mbuf *
1448 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1450 struct ifnet *ifp = sc->sc_ifp;
1452 /* Just pass in the packet to our lagg device */
1453 m->m_pkthdr.rcvif = ifp;
1463 lagg_fail_attach(struct lagg_softc *sc)
1465 sc->sc_detach = lagg_fail_detach;
1466 sc->sc_start = lagg_fail_start;
1467 sc->sc_input = lagg_fail_input;
1468 sc->sc_port_create = NULL;
1469 sc->sc_port_destroy = NULL;
1475 lagg_fail_detach(struct lagg_softc *sc)
1481 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1483 struct lagg_port *lp;
1485 /* Use the master port if active or the next available port */
1486 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1492 return (lagg_enqueue(lp->lp_ifp, m));
1495 static struct mbuf *
1496 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1498 struct ifnet *ifp = sc->sc_ifp;
1499 struct lagg_port *tmp_tp;
1501 if (lp == sc->sc_primary) {
1502 m->m_pkthdr.rcvif = ifp;
1506 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1507 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1509 * If tmp_tp is null, we've recieved a packet when all
1510 * our links are down. Weird, but process it anyways.
1512 if ((tmp_tp == NULL || tmp_tp == lp)) {
1513 m->m_pkthdr.rcvif = ifp;
1527 lagg_lb_attach(struct lagg_softc *sc)
1529 struct lagg_port *lp;
1532 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1533 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1536 sc->sc_detach = lagg_lb_detach;
1537 sc->sc_start = lagg_lb_start;
1538 sc->sc_input = lagg_lb_input;
1539 sc->sc_port_create = lagg_lb_port_create;
1540 sc->sc_port_destroy = lagg_lb_port_destroy;
1541 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1543 lb->lb_key = arc4random();
1544 sc->sc_psc = (caddr_t)lb;
1546 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1547 lagg_lb_port_create(lp);
1553 lagg_lb_detach(struct lagg_softc *sc)
1555 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1562 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1564 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1565 struct lagg_port *lp_next;
1568 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1569 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1572 if (i >= LAGG_MAX_PORTS)
1574 if (sc->sc_ifflags & IFF_DEBUG)
1575 printf("%s: port %s at index %d\n",
1576 sc->sc_ifname, lp_next->lp_ifname, i);
1577 lb->lb_ports[i++] = lp_next;
1584 lagg_lb_port_create(struct lagg_port *lp)
1586 struct lagg_softc *sc = lp->lp_softc;
1587 return (lagg_lb_porttable(sc, NULL));
1591 lagg_lb_port_destroy(struct lagg_port *lp)
1593 struct lagg_softc *sc = lp->lp_softc;
1594 lagg_lb_porttable(sc, lp);
1598 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1600 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1601 struct lagg_port *lp = NULL;
1604 if (m->m_flags & M_FLOWID)
1605 p = m->m_pkthdr.flowid;
1607 p = lagg_hashmbuf(m, lb->lb_key);
1609 lp = lb->lb_ports[p];
1612 * Check the port's link state. This will return the next active
1613 * port if the link is down or the port is NULL.
1615 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1621 return (lagg_enqueue(lp->lp_ifp, m));
1624 static struct mbuf *
1625 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1627 struct ifnet *ifp = sc->sc_ifp;
1629 /* Just pass in the packet to our lagg device */
1630 m->m_pkthdr.rcvif = ifp;
1640 lagg_lacp_attach(struct lagg_softc *sc)
1642 struct lagg_port *lp;
1645 sc->sc_detach = lagg_lacp_detach;
1646 sc->sc_port_create = lacp_port_create;
1647 sc->sc_port_destroy = lacp_port_destroy;
1648 sc->sc_linkstate = lacp_linkstate;
1649 sc->sc_start = lagg_lacp_start;
1650 sc->sc_input = lagg_lacp_input;
1651 sc->sc_init = lacp_init;
1652 sc->sc_stop = lacp_stop;
1653 sc->sc_lladdr = lagg_lacp_lladdr;
1654 sc->sc_req = lacp_req;
1655 sc->sc_portreq = lacp_portreq;
1657 error = lacp_attach(sc);
1661 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1662 lacp_port_create(lp);
1668 lagg_lacp_detach(struct lagg_softc *sc)
1670 struct lagg_port *lp;
1673 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1674 lacp_port_destroy(lp);
1676 /* unlocking is safe here */
1678 error = lacp_detach(sc);
1685 lagg_lacp_lladdr(struct lagg_softc *sc)
1687 struct lagg_port *lp;
1689 /* purge all the lacp ports */
1690 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1691 lacp_port_destroy(lp);
1693 /* add them back in */
1694 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1695 lacp_port_create(lp);
1699 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1701 struct lagg_port *lp;
1703 lp = lacp_select_tx_port(sc, m);
1710 return (lagg_enqueue(lp->lp_ifp, m));
1713 static struct mbuf *
1714 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1716 struct ifnet *ifp = sc->sc_ifp;
1717 struct ether_header *eh;
1720 eh = mtod(m, struct ether_header *);
1721 etype = ntohs(eh->ether_type);
1723 /* Tap off LACP control messages */
1724 if (etype == ETHERTYPE_SLOW) {
1725 m = lacp_input(lp, m);
1731 * If the port is not collecting or not in the active aggregator then
1734 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1739 m->m_pkthdr.rcvif = ifp;