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>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
23 #include "opt_inet6.h"
25 #include <sys/param.h>
26 #include <sys/kernel.h>
27 #include <sys/malloc.h>
29 #include <sys/queue.h>
30 #include <sys/socket.h>
31 #include <sys/sockio.h>
32 #include <sys/sysctl.h>
33 #include <sys/module.h>
35 #include <sys/systm.h>
39 #include <sys/rwlock.h>
40 #include <sys/taskqueue.h>
42 #include <net/ethernet.h>
44 #include <net/if_clone.h>
45 #include <net/if_arp.h>
46 #include <net/if_dl.h>
47 #include <net/if_llc.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
50 #include <net/if_var.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip.h>
61 #include <netinet/ip6.h>
64 #include <net/if_vlan_var.h>
65 #include <net/if_lagg.h>
66 #include <net/ieee8023ad_lacp.h>
68 /* Special flags we should propagate to the lagg ports. */
71 int (*func)(struct ifnet *, int);
73 {IFF_PROMISC, ifpromisc},
74 {IFF_ALLMULTI, if_allmulti},
78 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
79 static struct mtx lagg_list_mtx;
80 eventhandler_tag lagg_detach_cookie = NULL;
82 static int lagg_clone_create(struct if_clone *, int, caddr_t);
83 static void lagg_clone_destroy(struct ifnet *);
84 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
85 static void lagg_capabilities(struct lagg_softc *);
86 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
87 static void lagg_port_setlladdr(void *, int);
88 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
89 static int lagg_port_destroy(struct lagg_port *, int);
90 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
91 static void lagg_port_state(struct ifnet *, int);
92 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
93 static int lagg_port_output(struct ifnet *, struct mbuf *,
94 struct sockaddr *, struct rtentry *);
95 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
96 static int lagg_port_checkstacking(struct lagg_softc *);
97 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
98 static void lagg_init(void *);
99 static void lagg_stop(struct lagg_softc *);
100 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
101 static int lagg_ether_setmulti(struct lagg_softc *);
102 static int lagg_ether_cmdmulti(struct lagg_port *, int);
103 static int lagg_setflag(struct lagg_port *, int, int,
104 int (*func)(struct ifnet *, int));
105 static int lagg_setflags(struct lagg_port *, int status);
106 static void lagg_start(struct ifnet *);
107 static int lagg_media_change(struct ifnet *);
108 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
109 static struct lagg_port *lagg_link_active(struct lagg_softc *,
111 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
113 IFC_SIMPLE_DECLARE(lagg, 0);
115 /* Simple round robin */
116 static int lagg_rr_attach(struct lagg_softc *);
117 static int lagg_rr_detach(struct lagg_softc *);
118 static void lagg_rr_port_destroy(struct lagg_port *);
119 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
120 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
123 /* Active failover */
124 static int lagg_fail_attach(struct lagg_softc *);
125 static int lagg_fail_detach(struct lagg_softc *);
126 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
127 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
131 static int lagg_lb_attach(struct lagg_softc *);
132 static int lagg_lb_detach(struct lagg_softc *);
133 static int lagg_lb_port_create(struct lagg_port *);
134 static void lagg_lb_port_destroy(struct lagg_port *);
135 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
136 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
138 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
141 static int lagg_lacp_attach(struct lagg_softc *);
142 static int lagg_lacp_detach(struct lagg_softc *);
143 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
144 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
146 static void lagg_lacp_lladdr(struct lagg_softc *);
148 /* lagg protocol table */
149 static const struct {
151 int (*ti_attach)(struct lagg_softc *);
153 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
154 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
155 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
156 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
157 { LAGG_PROTO_LACP, lagg_lacp_attach },
158 { LAGG_PROTO_NONE, NULL }
162 lagg_modevent(module_t mod, int type, void *data)
167 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
168 SLIST_INIT(&lagg_list);
169 if_clone_attach(&lagg_cloner);
170 lagg_input_p = lagg_input;
171 lagg_linkstate_p = lagg_port_state;
172 lagg_detach_cookie = EVENTHANDLER_REGISTER(
173 ifnet_departure_event, lagg_port_ifdetach, NULL,
174 EVENTHANDLER_PRI_ANY);
177 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
179 if_clone_detach(&lagg_cloner);
181 lagg_linkstate_p = NULL;
182 mtx_destroy(&lagg_list_mtx);
190 static moduledata_t lagg_mod = {
196 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
199 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
201 struct lagg_softc *sc;
204 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
206 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
207 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
213 sc->sc_proto = LAGG_PROTO_NONE;
214 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
215 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
216 sc->sc_proto = lagg_protos[i].ti_proto;
217 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
218 if_free_type(ifp, IFT_ETHER);
226 SLIST_INIT(&sc->sc_ports);
227 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
229 /* Initialise pseudo media types */
230 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
232 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
233 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
235 if_initname(ifp, ifc->ifc_name, unit);
236 ifp->if_type = IFT_ETHER;
238 ifp->if_start = lagg_start;
239 ifp->if_init = lagg_init;
240 ifp->if_ioctl = lagg_ioctl;
241 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
243 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
244 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
245 IFQ_SET_READY(&ifp->if_snd);
248 * Attach as an ordinary ethernet device, childs will be attached
249 * as special device IFT_IEEE8023ADLAG.
251 ether_ifattach(ifp, eaddr);
253 /* Insert into the global list of laggs */
254 mtx_lock(&lagg_list_mtx);
255 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
256 mtx_unlock(&lagg_list_mtx);
262 lagg_clone_destroy(struct ifnet *ifp)
264 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
265 struct lagg_port *lp;
270 ifp->if_flags &= ~IFF_UP;
272 /* Shutdown and remove lagg ports */
273 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
274 lagg_port_destroy(lp, 1);
275 /* Unhook the aggregation protocol */
276 if (sc->sc_detach != NULL)
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);
497 /* Add multicast addresses and interface flags to this port */
498 lagg_ether_cmdmulti(lp, 1);
499 lagg_setflags(lp, 1);
501 if (sc->sc_port_create != NULL)
502 error = (*sc->sc_port_create)(lp);
504 /* remove the port again, without calling sc_port_destroy */
505 lagg_port_destroy(lp, 0);
513 lagg_port_checkstacking(struct lagg_softc *sc)
515 struct lagg_softc *sc_ptr;
516 struct lagg_port *lp;
519 LAGG_WLOCK_ASSERT(sc);
521 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
522 if (lp->lp_flags & LAGG_PORT_STACK) {
523 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
524 m = MAX(m, lagg_port_checkstacking(sc_ptr));
532 lagg_port_destroy(struct lagg_port *lp, int runpd)
534 struct lagg_softc *sc = lp->lp_softc;
535 struct lagg_port *lp_ptr;
536 struct lagg_llq *llq;
537 struct ifnet *ifp = lp->lp_ifp;
539 LAGG_WLOCK_ASSERT(sc);
541 if (runpd && sc->sc_port_destroy != NULL)
542 (*sc->sc_port_destroy)(lp);
545 * Remove multicast addresses and interface flags from this port and
546 * reset the MAC address, skip if the interface is being detached.
548 if (!lp->lp_detaching) {
549 lagg_ether_cmdmulti(lp, 0);
550 lagg_setflags(lp, 0);
551 lagg_port_lladdr(lp, lp->lp_lladdr);
554 /* Restore interface */
555 ifp->if_type = lp->lp_iftype;
556 ifp->if_ioctl = lp->lp_ioctl;
557 ifp->if_output = lp->lp_output;
560 /* Finally, remove the port from the lagg */
561 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
564 /* Update the primary interface */
565 if (lp == sc->sc_primary) {
566 uint8_t lladdr[ETHER_ADDR_LEN];
568 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
569 bzero(&lladdr, ETHER_ADDR_LEN);
571 bcopy(lp_ptr->lp_lladdr,
572 lladdr, ETHER_ADDR_LEN);
574 lagg_lladdr(sc, lladdr);
575 sc->sc_primary = lp_ptr;
577 /* Update link layer address for each port */
578 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
579 lagg_port_lladdr(lp_ptr, lladdr);
582 /* Remove any pending lladdr changes from the queue */
583 if (lp->lp_detaching) {
584 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
585 if (llq->llq_ifp == ifp) {
586 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
589 break; /* Only appears once */
595 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
599 /* Update lagg capabilities */
600 lagg_capabilities(sc);
606 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
608 struct lagg_reqport *rp = (struct lagg_reqport *)data;
609 struct lagg_softc *sc;
610 struct lagg_port *lp = NULL;
613 /* Should be checked by the caller */
614 if (ifp->if_type != IFT_IEEE8023ADLAG ||
615 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
620 if (rp->rp_portname[0] == '\0' ||
621 ifunit(rp->rp_portname) != ifp) {
627 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
633 lagg_port2req(lp, rp);
638 if (lp->lp_ioctl == NULL) {
642 error = (*lp->lp_ioctl)(ifp, cmd, data);
646 /* Update lagg interface capabilities */
648 lagg_capabilities(sc);
653 /* Do not allow the MTU to be changed once joined */
664 if (lp->lp_ioctl != NULL)
665 return ((*lp->lp_ioctl)(ifp, cmd, data));
671 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
672 struct sockaddr *dst, struct rtentry *rt0)
674 struct lagg_port *lp = ifp->if_lagg;
675 struct ether_header *eh;
678 switch (dst->sa_family) {
679 case pseudo_AF_HDRCMPLT:
681 eh = (struct ether_header *)dst->sa_data;
682 type = eh->ether_type;
687 * Only allow ethernet types required to initiate or maintain the link,
688 * aggregated frames take a different path.
690 switch (ntohs(type)) {
691 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
692 return ((*lp->lp_output)(ifp, m, dst, rt0));
695 /* drop any other frames */
701 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
703 struct lagg_port *lp;
704 struct lagg_softc *sc;
706 if ((lp = ifp->if_lagg) == NULL)
712 lp->lp_detaching = 1;
713 lagg_port_destroy(lp, 1);
718 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
720 struct lagg_softc *sc = lp->lp_softc;
721 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
722 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
723 rp->rp_prio = lp->lp_prio;
724 rp->rp_flags = lp->lp_flags;
725 if (sc->sc_portreq != NULL)
726 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
728 /* Add protocol specific flags */
729 switch (sc->sc_proto) {
730 case LAGG_PROTO_FAILOVER:
731 if (lp == sc->sc_primary)
732 rp->rp_flags |= LAGG_PORT_MASTER;
734 case LAGG_PROTO_ROUNDROBIN:
735 case LAGG_PROTO_LOADBALANCE:
736 case LAGG_PROTO_ETHERCHANNEL:
737 if (LAGG_PORTACTIVE(lp))
738 rp->rp_flags |= LAGG_PORT_ACTIVE;
741 case LAGG_PROTO_LACP:
742 /* LACP has a different definition of active */
743 if (lacp_port_isactive(lp))
744 rp->rp_flags |= LAGG_PORT_ACTIVE;
753 struct lagg_softc *sc = (struct lagg_softc *)xsc;
754 struct lagg_port *lp;
755 struct ifnet *ifp = sc->sc_ifp;
757 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
762 ifp->if_drv_flags |= IFF_DRV_RUNNING;
763 /* Update the port lladdrs */
764 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
765 lagg_port_lladdr(lp, IF_LLADDR(ifp));
767 if (sc->sc_init != NULL)
774 lagg_stop(struct lagg_softc *sc)
776 struct ifnet *ifp = sc->sc_ifp;
778 LAGG_WLOCK_ASSERT(sc);
780 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
783 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
785 if (sc->sc_stop != NULL)
790 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
792 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
793 struct lagg_reqall *ra = (struct lagg_reqall *)data;
794 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
795 struct ifreq *ifr = (struct ifreq *)data;
796 struct lagg_port *lp;
798 struct thread *td = curthread;
800 int count, buflen, len, error = 0;
802 bzero(&rpbuf, sizeof(rpbuf));
808 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
810 buflen = count * sizeof(struct lagg_reqport);
813 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
816 ra->ra_proto = sc->sc_proto;
817 if (sc->sc_req != NULL)
818 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
822 len = min(ra->ra_size, buflen);
823 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
824 if (len < sizeof(rpbuf))
827 lagg_port2req(lp, &rpbuf);
828 memcpy(buf, &rpbuf, sizeof(rpbuf));
830 buf += sizeof(rpbuf);
831 len -= sizeof(rpbuf);
834 ra->ra_ports = count;
835 ra->ra_size = count * sizeof(rpbuf);
836 error = copyout(outbuf, ra->ra_port, ra->ra_size);
837 free(outbuf, M_TEMP);
840 error = priv_check(td, PRIV_NET_LAGG);
843 if (ra->ra_proto >= LAGG_PROTO_MAX) {
844 error = EPROTONOSUPPORT;
847 if (sc->sc_proto != LAGG_PROTO_NONE) {
849 error = sc->sc_detach(sc);
850 /* Reset protocol and pointers */
851 sc->sc_proto = LAGG_PROTO_NONE;
852 sc->sc_detach = NULL;
855 sc->sc_port_create = NULL;
856 sc->sc_port_destroy = NULL;
857 sc->sc_linkstate = NULL;
860 sc->sc_lladdr = NULL;
862 sc->sc_portreq = NULL;
867 for (int i = 0; i < (sizeof(lagg_protos) /
868 sizeof(lagg_protos[0])); i++) {
869 if (lagg_protos[i].ti_proto == ra->ra_proto) {
870 if (sc->sc_ifflags & IFF_DEBUG)
871 printf("%s: using proto %u\n",
873 lagg_protos[i].ti_proto);
875 sc->sc_proto = lagg_protos[i].ti_proto;
876 if (sc->sc_proto != LAGG_PROTO_NONE)
877 error = lagg_protos[i].ti_attach(sc);
882 error = EPROTONOSUPPORT;
885 if (rp->rp_portname[0] == '\0' ||
886 (tpif = ifunit(rp->rp_portname)) == NULL) {
892 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
893 lp->lp_softc != sc) {
899 lagg_port2req(lp, rp);
903 error = priv_check(td, PRIV_NET_LAGG);
906 if (rp->rp_portname[0] == '\0' ||
907 (tpif = ifunit(rp->rp_portname)) == NULL) {
912 error = lagg_port_create(sc, tpif);
915 case SIOCSLAGGDELPORT:
916 error = priv_check(td, PRIV_NET_LAGG);
919 if (rp->rp_portname[0] == '\0' ||
920 (tpif = ifunit(rp->rp_portname)) == NULL) {
926 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
927 lp->lp_softc != sc) {
933 error = lagg_port_destroy(lp, 1);
937 /* Set flags on ports too */
939 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
940 lagg_setflags(lp, 1);
944 if (!(ifp->if_flags & IFF_UP) &&
945 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
947 * If interface is marked down and it is running,
948 * then stop and disable it.
953 } else if ((ifp->if_flags & IFF_UP) &&
954 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
956 * If interface is marked up and it is stopped, then
965 error = lagg_ether_setmulti(sc);
970 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
975 /* Do not allow the MTU or caps to be directly changed */
980 error = ether_ioctl(ifp, cmd, data);
987 lagg_ether_setmulti(struct lagg_softc *sc)
989 struct lagg_port *lp;
991 LAGG_WLOCK_ASSERT(sc);
993 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
994 /* First, remove any existing filter entries. */
995 lagg_ether_cmdmulti(lp, 0);
996 /* copy all addresses from the lagg interface to the port */
997 lagg_ether_cmdmulti(lp, 1);
1003 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1005 struct lagg_softc *sc = lp->lp_softc;
1006 struct ifnet *ifp = lp->lp_ifp;
1007 struct ifnet *scifp = sc->sc_ifp;
1009 struct ifmultiaddr *ifma, *rifma = NULL;
1010 struct sockaddr_dl sdl;
1013 LAGG_WLOCK_ASSERT(sc);
1015 bzero((char *)&sdl, sizeof(sdl));
1016 sdl.sdl_len = sizeof(sdl);
1017 sdl.sdl_family = AF_LINK;
1018 sdl.sdl_type = IFT_ETHER;
1019 sdl.sdl_alen = ETHER_ADDR_LEN;
1020 sdl.sdl_index = ifp->if_index;
1023 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1024 if (ifma->ifma_addr->sa_family != AF_LINK)
1026 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1027 LLADDR(&sdl), ETHER_ADDR_LEN);
1029 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1032 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1035 mc->mc_ifma = rifma;
1036 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1039 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1040 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1041 if_delmulti_ifma(mc->mc_ifma);
1048 /* Handle a ref counted flag that should be set on the lagg port as well */
1050 lagg_setflag(struct lagg_port *lp, int flag, int status,
1051 int (*func)(struct ifnet *, int))
1053 struct lagg_softc *sc = lp->lp_softc;
1054 struct ifnet *scifp = sc->sc_ifp;
1055 struct ifnet *ifp = lp->lp_ifp;
1058 LAGG_WLOCK_ASSERT(sc);
1060 status = status ? (scifp->if_flags & flag) : 0;
1061 /* Now "status" contains the flag value or 0 */
1064 * See if recorded ports status is different from what
1065 * we want it to be. If it is, flip it. We record ports
1066 * status in lp_ifflags so that we won't clear ports flag
1067 * we haven't set. In fact, we don't clear or set ports
1068 * flags directly, but get or release references to them.
1069 * That's why we can be sure that recorded flags still are
1070 * in accord with actual ports flags.
1072 if (status != (lp->lp_ifflags & flag)) {
1073 error = (*func)(ifp, status);
1076 lp->lp_ifflags &= ~flag;
1077 lp->lp_ifflags |= status;
1083 * Handle IFF_* flags that require certain changes on the lagg port
1084 * if "status" is true, update ports flags respective to the lagg
1085 * if "status" is false, forcedly clear the flags set on port.
1088 lagg_setflags(struct lagg_port *lp, int status)
1092 for (i = 0; lagg_pflags[i].flag; i++) {
1093 error = lagg_setflag(lp, lagg_pflags[i].flag,
1094 status, lagg_pflags[i].func);
1102 lagg_start(struct ifnet *ifp)
1104 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1109 for (;; error = 0) {
1110 IFQ_DEQUEUE(&ifp->if_snd, m);
1116 if (sc->sc_proto != LAGG_PROTO_NONE)
1117 error = (*sc->sc_start)(sc, m);
1124 m_freem(m); /* sc_start failed */
1133 static struct mbuf *
1134 lagg_input(struct ifnet *ifp, struct mbuf *m)
1136 struct lagg_port *lp = ifp->if_lagg;
1137 struct lagg_softc *sc = lp->lp_softc;
1138 struct ifnet *scifp = sc->sc_ifp;
1140 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1141 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1142 sc->sc_proto == LAGG_PROTO_NONE) {
1150 m = (*sc->sc_input)(sc, lp, m);
1153 scifp->if_ipackets++;
1154 scifp->if_ibytes += m->m_pkthdr.len;
1162 lagg_media_change(struct ifnet *ifp)
1164 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1166 if (sc->sc_ifflags & IFF_DEBUG)
1167 printf("%s\n", __func__);
1174 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1176 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1177 struct lagg_port *lp;
1179 imr->ifm_status = IFM_AVALID;
1180 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1183 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1184 if (LAGG_PORTACTIVE(lp))
1185 imr->ifm_status |= IFM_ACTIVE;
1191 lagg_port_state(struct ifnet *ifp, int state)
1193 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1194 struct lagg_softc *sc = NULL;
1202 if (sc->sc_linkstate != NULL)
1203 (*sc->sc_linkstate)(lp);
1208 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1210 struct lagg_port *lp_next, *rval = NULL;
1211 // int new_link = LINK_STATE_DOWN;
1213 LAGG_RLOCK_ASSERT(sc);
1215 * Search a port which reports an active link state.
1220 if (LAGG_PORTACTIVE(lp)) {
1224 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1225 LAGG_PORTACTIVE(lp_next)) {
1231 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1232 if (LAGG_PORTACTIVE(lp_next)) {
1241 * The IEEE 802.1D standard assumes that a lagg with
1242 * multiple ports is always full duplex. This is valid
1243 * for load sharing laggs and if at least two links
1244 * are active. Unfortunately, checking the latter would
1245 * be too expensive at this point.
1247 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1249 new_link = LINK_STATE_FULL_DUPLEX;
1251 new_link = rval->lp_link_state;
1259 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1261 if (m->m_pkthdr.len < (off + len)) {
1263 } else if (m->m_len < (off + len)) {
1264 m_copydata(m, off, len, buf);
1267 return (mtod(m, char *) + off);
1271 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1276 struct ether_header *eh;
1277 struct ether_vlan_header vlanbuf;
1278 const struct ether_vlan_header *vlan;
1280 const struct ip *ip;
1284 const struct ip6_hdr *ip6;
1285 struct ip6_hdr ip6buf;
1292 eh = mtod(m, struct ether_header *);
1293 etype = ntohs(eh->ether_type);
1294 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1295 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1297 /* Special handling for encapsulating VLAN frames */
1298 if (m->m_flags & M_VLANTAG) {
1299 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1300 sizeof(m->m_pkthdr.ether_vtag), p);
1301 } else if (etype == ETHERTYPE_VLAN) {
1302 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1306 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1307 etype = ntohs(vlan->evl_proto);
1308 off += sizeof(*vlan) - sizeof(*eh);
1314 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1318 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1319 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1323 case ETHERTYPE_IPV6:
1324 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1328 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1329 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1330 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1331 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1340 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1344 IFQ_HANDOFF(ifp, m, error);
1349 * Simple round robin aggregation
1353 lagg_rr_attach(struct lagg_softc *sc)
1355 struct lagg_port *lp;
1357 sc->sc_detach = lagg_rr_detach;
1358 sc->sc_start = lagg_rr_start;
1359 sc->sc_input = lagg_rr_input;
1360 sc->sc_port_create = NULL;
1361 sc->sc_port_destroy = lagg_rr_port_destroy;
1362 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1364 lp = SLIST_FIRST(&sc->sc_ports);
1365 sc->sc_psc = (caddr_t)lp;
1371 lagg_rr_detach(struct lagg_softc *sc)
1378 lagg_rr_port_destroy(struct lagg_port *lp)
1380 struct lagg_softc *sc = lp->lp_softc;
1382 if (lp == (struct lagg_port *)sc->sc_psc)
1387 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1389 struct lagg_port *lp = (struct lagg_port *)sc->sc_psc, *lp_next;
1392 if (lp == NULL && (lp = lagg_link_active(sc, NULL)) == NULL)
1396 error = lagg_enqueue(lp->lp_ifp, m);
1398 /* Get next active port */
1399 lp_next = lagg_link_active(sc, SLIST_NEXT(lp, lp_entries));
1400 sc->sc_psc = (caddr_t)lp_next;
1405 static struct mbuf *
1406 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1408 struct ifnet *ifp = sc->sc_ifp;
1410 /* Just pass in the packet to our lagg device */
1411 m->m_pkthdr.rcvif = ifp;
1421 lagg_fail_attach(struct lagg_softc *sc)
1423 sc->sc_detach = lagg_fail_detach;
1424 sc->sc_start = lagg_fail_start;
1425 sc->sc_input = lagg_fail_input;
1426 sc->sc_port_create = NULL;
1427 sc->sc_port_destroy = NULL;
1433 lagg_fail_detach(struct lagg_softc *sc)
1439 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1441 struct lagg_port *lp;
1443 /* Use the master port if active or the next available port */
1444 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL)
1448 return (lagg_enqueue(lp->lp_ifp, m));
1451 static struct mbuf *
1452 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1454 struct ifnet *ifp = sc->sc_ifp;
1455 struct lagg_port *tmp_tp;
1457 if (lp == sc->sc_primary) {
1458 m->m_pkthdr.rcvif = ifp;
1462 if (sc->sc_primary->lp_link_state == LINK_STATE_DOWN) {
1463 tmp_tp = lagg_link_active(sc, NULL);
1465 * If tmp_tp is null, we've recieved a packet when all
1466 * our links are down. Weird, but process it anyways.
1468 if ((tmp_tp == NULL || tmp_tp == lp)) {
1469 m->m_pkthdr.rcvif = ifp;
1483 lagg_lb_attach(struct lagg_softc *sc)
1485 struct lagg_port *lp;
1488 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1489 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1492 sc->sc_detach = lagg_lb_detach;
1493 sc->sc_start = lagg_lb_start;
1494 sc->sc_input = lagg_lb_input;
1495 sc->sc_port_create = lagg_lb_port_create;
1496 sc->sc_port_destroy = lagg_lb_port_destroy;
1497 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1499 lb->lb_key = arc4random();
1500 sc->sc_psc = (caddr_t)lb;
1502 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1503 lagg_lb_port_create(lp);
1509 lagg_lb_detach(struct lagg_softc *sc)
1511 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1518 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1520 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1521 struct lagg_port *lp_next;
1524 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1525 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1528 if (i >= LAGG_MAX_PORTS)
1530 if (sc->sc_ifflags & IFF_DEBUG)
1531 printf("%s: port %s at index %d\n",
1532 sc->sc_ifname, lp_next->lp_ifname, i);
1533 lb->lb_ports[i++] = lp_next;
1540 lagg_lb_port_create(struct lagg_port *lp)
1542 struct lagg_softc *sc = lp->lp_softc;
1543 return (lagg_lb_porttable(sc, NULL));
1547 lagg_lb_port_destroy(struct lagg_port *lp)
1549 struct lagg_softc *sc = lp->lp_softc;
1550 lagg_lb_porttable(sc, lp);
1554 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1556 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1557 struct lagg_port *lp = NULL;
1561 p = lagg_hashmbuf(m, lb->lb_key);
1562 if ((idx = p % sc->sc_count) >= LAGG_MAX_PORTS)
1564 lp = lb->lb_ports[idx];
1567 * Check the port's link state. This will return the next active
1568 * port if the link is down or the port is NULL.
1570 if ((lp = lagg_link_active(sc, lp)) == NULL)
1574 return (lagg_enqueue(lp->lp_ifp, m));
1577 static struct mbuf *
1578 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1580 struct ifnet *ifp = sc->sc_ifp;
1582 /* Just pass in the packet to our lagg device */
1583 m->m_pkthdr.rcvif = ifp;
1593 lagg_lacp_attach(struct lagg_softc *sc)
1595 struct lagg_port *lp;
1598 sc->sc_detach = lagg_lacp_detach;
1599 sc->sc_port_create = lacp_port_create;
1600 sc->sc_port_destroy = lacp_port_destroy;
1601 sc->sc_linkstate = lacp_linkstate;
1602 sc->sc_start = lagg_lacp_start;
1603 sc->sc_input = lagg_lacp_input;
1604 sc->sc_init = lacp_init;
1605 sc->sc_stop = lacp_stop;
1606 sc->sc_lladdr = lagg_lacp_lladdr;
1607 sc->sc_req = lacp_req;
1608 sc->sc_portreq = lacp_portreq;
1610 error = lacp_attach(sc);
1614 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1615 lacp_port_create(lp);
1621 lagg_lacp_detach(struct lagg_softc *sc)
1623 struct lagg_port *lp;
1626 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1627 lacp_port_destroy(lp);
1629 /* unlocking is safe here */
1631 error = lacp_detach(sc);
1638 lagg_lacp_lladdr(struct lagg_softc *sc)
1640 struct lagg_port *lp;
1642 /* purge all the lacp ports */
1643 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1644 lacp_port_destroy(lp);
1646 /* add them back in */
1647 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1648 lacp_port_create(lp);
1652 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1654 struct lagg_port *lp;
1656 lp = lacp_select_tx_port(sc, m);
1661 return (lagg_enqueue(lp->lp_ifp, m));
1664 static struct mbuf *
1665 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1667 struct ifnet *ifp = sc->sc_ifp;
1668 struct ether_header *eh;
1671 eh = mtod(m, struct ether_header *);
1672 etype = ntohs(eh->ether_type);
1674 /* Tap off LACP control messages */
1675 if (etype == ETHERTYPE_SLOW) {
1681 * If the port is not collecting or not in the active aggregator then
1684 if ((lp->lp_flags & LAGG_PORT_COLLECTING) == 0 ||
1685 lacp_port_isactive(lp) == 0) {
1690 m->m_pkthdr.rcvif = ifp;