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_linkstate(struct lagg_softc *);
92 static void lagg_port_state(struct ifnet *, int);
93 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
94 static int lagg_port_output(struct ifnet *, struct mbuf *,
95 struct sockaddr *, struct rtentry *);
96 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
97 static int lagg_port_checkstacking(struct lagg_softc *);
98 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
99 static void lagg_init(void *);
100 static void lagg_stop(struct lagg_softc *);
101 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
102 static int lagg_ether_setmulti(struct lagg_softc *);
103 static int lagg_ether_cmdmulti(struct lagg_port *, int);
104 static int lagg_setflag(struct lagg_port *, int, int,
105 int (*func)(struct ifnet *, int));
106 static int lagg_setflags(struct lagg_port *, int status);
107 static void lagg_start(struct ifnet *);
108 static int lagg_media_change(struct ifnet *);
109 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
110 static struct lagg_port *lagg_link_active(struct lagg_softc *,
112 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
114 IFC_SIMPLE_DECLARE(lagg, 0);
116 /* Simple round robin */
117 static int lagg_rr_attach(struct lagg_softc *);
118 static int lagg_rr_detach(struct lagg_softc *);
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 (*sc->sc_detach)(sc);
280 ifmedia_removeall(&sc->sc_media);
282 if_free_type(ifp, IFT_ETHER);
284 mtx_lock(&lagg_list_mtx);
285 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
286 mtx_unlock(&lagg_list_mtx);
288 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
289 LAGG_LOCK_DESTROY(sc);
294 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
296 struct ifnet *ifp = sc->sc_ifp;
298 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
301 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
302 /* Let the protocol know the MAC has changed */
303 if (sc->sc_lladdr != NULL)
304 (*sc->sc_lladdr)(sc);
308 lagg_capabilities(struct lagg_softc *sc)
310 struct lagg_port *lp;
311 int cap = ~0, ena = ~0;
313 LAGG_WLOCK_ASSERT(sc);
315 /* Get capabilities from the lagg ports */
316 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
317 cap &= lp->lp_ifp->if_capabilities;
318 ena &= lp->lp_ifp->if_capenable;
320 cap = (cap == ~0 ? 0 : cap);
321 ena = (ena == ~0 ? 0 : ena);
323 if (sc->sc_ifp->if_capabilities != cap ||
324 sc->sc_ifp->if_capenable != ena) {
325 sc->sc_ifp->if_capabilities = cap;
326 sc->sc_ifp->if_capenable = ena;
327 getmicrotime(&sc->sc_ifp->if_lastchange);
329 if (sc->sc_ifflags & IFF_DEBUG)
330 if_printf(sc->sc_ifp,
331 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
336 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
338 struct lagg_softc *sc = lp->lp_softc;
339 struct ifnet *ifp = lp->lp_ifp;
340 struct lagg_llq *llq;
343 LAGG_WLOCK_ASSERT(sc);
345 if (lp->lp_detaching ||
346 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
349 /* Check to make sure its not already queued to be changed */
350 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
351 if (llq->llq_ifp == ifp) {
358 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
359 if (llq == NULL) /* XXX what to do */
363 /* Update the lladdr even if pending, it may have changed */
365 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
368 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
370 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
374 * Set the interface MAC address from a taskqueue to avoid a LOR.
377 lagg_port_setlladdr(void *arg, int pending)
379 struct lagg_softc *sc = (struct lagg_softc *)arg;
380 struct lagg_llq *llq, *head;
384 /* Grab a local reference of the queue and remove it from the softc */
386 head = SLIST_FIRST(&sc->sc_llq_head);
387 SLIST_FIRST(&sc->sc_llq_head) = NULL;
391 * Traverse the queue and set the lladdr on each ifp. It is safe to do
392 * unlocked as we have the only reference to it.
394 for (llq = head; llq != NULL; llq = head) {
397 /* Set the link layer address */
398 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
400 printf("%s: setlladdr failed on %s\n", __func__,
403 head = SLIST_NEXT(llq, llq_entries);
409 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
411 struct lagg_softc *sc_ptr;
412 struct lagg_port *lp;
415 LAGG_WLOCK_ASSERT(sc);
417 /* Limit the maximal number of lagg ports */
418 if (sc->sc_count >= LAGG_MAX_PORTS)
421 /* New lagg port has to be in an idle state */
422 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
425 /* Check if port has already been associated to a lagg */
426 if (ifp->if_lagg != NULL)
429 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
430 if (ifp->if_type != IFT_ETHER)
431 return (EPROTONOSUPPORT);
433 /* Allow the first Ethernet member to define the MTU */
434 if (SLIST_EMPTY(&sc->sc_ports))
435 sc->sc_ifp->if_mtu = ifp->if_mtu;
436 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
437 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
442 if ((lp = malloc(sizeof(struct lagg_port),
443 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
446 /* Check if port is a stacked lagg */
447 mtx_lock(&lagg_list_mtx);
448 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
449 if (ifp == sc_ptr->sc_ifp) {
450 mtx_unlock(&lagg_list_mtx);
453 /* XXX disable stacking for the moment, its untested
454 lp->lp_flags |= LAGG_PORT_STACK;
455 if (lagg_port_checkstacking(sc_ptr) >=
457 mtx_unlock(&lagg_list_mtx);
464 mtx_unlock(&lagg_list_mtx);
466 /* Change the interface type */
467 lp->lp_iftype = ifp->if_type;
468 ifp->if_type = IFT_IEEE8023ADLAG;
470 lp->lp_ioctl = ifp->if_ioctl;
471 ifp->if_ioctl = lagg_port_ioctl;
472 lp->lp_output = ifp->if_output;
473 ifp->if_output = lagg_port_output;
478 /* Save port link layer address */
479 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
481 if (SLIST_EMPTY(&sc->sc_ports)) {
483 lagg_lladdr(sc, IF_LLADDR(ifp));
485 /* Update link layer address for this port */
486 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
489 /* Insert into the list of ports */
490 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
493 /* Update lagg capabilities */
494 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);
607 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
609 struct lagg_reqport *rp = (struct lagg_reqport *)data;
610 struct lagg_softc *sc;
611 struct lagg_port *lp = NULL;
614 /* Should be checked by the caller */
615 if (ifp->if_type != IFT_IEEE8023ADLAG ||
616 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
621 if (rp->rp_portname[0] == '\0' ||
622 ifunit(rp->rp_portname) != ifp) {
628 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
634 lagg_port2req(lp, rp);
639 if (lp->lp_ioctl == NULL) {
643 error = (*lp->lp_ioctl)(ifp, cmd, data);
647 /* Update lagg interface capabilities */
649 lagg_capabilities(sc);
654 /* Do not allow the MTU to be changed once joined */
665 if (lp->lp_ioctl != NULL)
666 return ((*lp->lp_ioctl)(ifp, cmd, data));
672 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
673 struct sockaddr *dst, struct rtentry *rt0)
675 struct lagg_port *lp = ifp->if_lagg;
676 struct ether_header *eh;
679 switch (dst->sa_family) {
680 case pseudo_AF_HDRCMPLT:
682 eh = (struct ether_header *)dst->sa_data;
683 type = eh->ether_type;
688 * Only allow ethernet types required to initiate or maintain the link,
689 * aggregated frames take a different path.
691 switch (ntohs(type)) {
692 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
693 return ((*lp->lp_output)(ifp, m, dst, rt0));
696 /* drop any other frames */
702 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
704 struct lagg_port *lp;
705 struct lagg_softc *sc;
707 if ((lp = ifp->if_lagg) == NULL)
713 lp->lp_detaching = 1;
714 lagg_port_destroy(lp, 1);
719 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
721 struct lagg_softc *sc = lp->lp_softc;
723 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
724 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
725 rp->rp_prio = lp->lp_prio;
726 rp->rp_flags = lp->lp_flags;
727 if (sc->sc_portreq != NULL)
728 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
730 /* Add protocol specific flags */
731 switch (sc->sc_proto) {
732 case LAGG_PROTO_FAILOVER:
733 if (lp == sc->sc_primary)
734 rp->rp_flags |= LAGG_PORT_MASTER;
735 if (lp == lagg_link_active(sc, sc->sc_primary))
736 rp->rp_flags |= LAGG_PORT_ACTIVE;
739 case LAGG_PROTO_ROUNDROBIN:
740 case LAGG_PROTO_LOADBALANCE:
741 case LAGG_PROTO_ETHERCHANNEL:
742 if (LAGG_PORTACTIVE(lp))
743 rp->rp_flags |= LAGG_PORT_ACTIVE;
746 case LAGG_PROTO_LACP:
747 /* LACP has a different definition of active */
748 if (lacp_port_isactive(lp))
749 rp->rp_flags |= LAGG_PORT_ACTIVE;
758 struct lagg_softc *sc = (struct lagg_softc *)xsc;
759 struct lagg_port *lp;
760 struct ifnet *ifp = sc->sc_ifp;
762 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
767 ifp->if_drv_flags |= IFF_DRV_RUNNING;
768 /* Update the port lladdrs */
769 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
770 lagg_port_lladdr(lp, IF_LLADDR(ifp));
772 if (sc->sc_init != NULL)
779 lagg_stop(struct lagg_softc *sc)
781 struct ifnet *ifp = sc->sc_ifp;
783 LAGG_WLOCK_ASSERT(sc);
785 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
788 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
790 if (sc->sc_stop != NULL)
795 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
797 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
798 struct lagg_reqall *ra = (struct lagg_reqall *)data;
799 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
800 struct ifreq *ifr = (struct ifreq *)data;
801 struct lagg_port *lp;
803 struct thread *td = curthread;
805 int count, buflen, len, error = 0;
807 bzero(&rpbuf, sizeof(rpbuf));
813 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
815 buflen = count * sizeof(struct lagg_reqport);
818 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
821 ra->ra_proto = sc->sc_proto;
822 if (sc->sc_req != NULL)
823 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
827 len = min(ra->ra_size, buflen);
828 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
829 if (len < sizeof(rpbuf))
832 lagg_port2req(lp, &rpbuf);
833 memcpy(buf, &rpbuf, sizeof(rpbuf));
835 buf += sizeof(rpbuf);
836 len -= sizeof(rpbuf);
839 ra->ra_ports = count;
840 ra->ra_size = count * sizeof(rpbuf);
841 error = copyout(outbuf, ra->ra_port, ra->ra_size);
842 free(outbuf, M_TEMP);
845 error = priv_check(td, PRIV_NET_LAGG);
848 if (ra->ra_proto >= LAGG_PROTO_MAX) {
849 error = EPROTONOSUPPORT;
852 if (sc->sc_proto != LAGG_PROTO_NONE) {
854 error = sc->sc_detach(sc);
855 /* Reset protocol and pointers */
856 sc->sc_proto = LAGG_PROTO_NONE;
857 sc->sc_detach = NULL;
860 sc->sc_port_create = NULL;
861 sc->sc_port_destroy = NULL;
862 sc->sc_linkstate = NULL;
865 sc->sc_lladdr = NULL;
867 sc->sc_portreq = NULL;
872 for (int i = 0; i < (sizeof(lagg_protos) /
873 sizeof(lagg_protos[0])); i++) {
874 if (lagg_protos[i].ti_proto == ra->ra_proto) {
875 if (sc->sc_ifflags & IFF_DEBUG)
876 printf("%s: using proto %u\n",
878 lagg_protos[i].ti_proto);
880 sc->sc_proto = lagg_protos[i].ti_proto;
881 if (sc->sc_proto != LAGG_PROTO_NONE)
882 error = lagg_protos[i].ti_attach(sc);
887 error = EPROTONOSUPPORT;
890 if (rp->rp_portname[0] == '\0' ||
891 (tpif = ifunit(rp->rp_portname)) == NULL) {
897 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
898 lp->lp_softc != sc) {
904 lagg_port2req(lp, rp);
908 error = priv_check(td, PRIV_NET_LAGG);
911 if (rp->rp_portname[0] == '\0' ||
912 (tpif = ifunit(rp->rp_portname)) == NULL) {
917 error = lagg_port_create(sc, tpif);
920 case SIOCSLAGGDELPORT:
921 error = priv_check(td, PRIV_NET_LAGG);
924 if (rp->rp_portname[0] == '\0' ||
925 (tpif = ifunit(rp->rp_portname)) == NULL) {
931 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
932 lp->lp_softc != sc) {
938 error = lagg_port_destroy(lp, 1);
942 /* Set flags on ports too */
944 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
945 lagg_setflags(lp, 1);
949 if (!(ifp->if_flags & IFF_UP) &&
950 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
952 * If interface is marked down and it is running,
953 * then stop and disable it.
958 } else if ((ifp->if_flags & IFF_UP) &&
959 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
961 * If interface is marked up and it is stopped, then
970 error = lagg_ether_setmulti(sc);
975 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
980 /* Do not allow the MTU or caps to be directly changed */
985 error = ether_ioctl(ifp, cmd, data);
992 lagg_ether_setmulti(struct lagg_softc *sc)
994 struct lagg_port *lp;
996 LAGG_WLOCK_ASSERT(sc);
998 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
999 /* First, remove any existing filter entries. */
1000 lagg_ether_cmdmulti(lp, 0);
1001 /* copy all addresses from the lagg interface to the port */
1002 lagg_ether_cmdmulti(lp, 1);
1008 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1010 struct lagg_softc *sc = lp->lp_softc;
1011 struct ifnet *ifp = lp->lp_ifp;
1012 struct ifnet *scifp = sc->sc_ifp;
1014 struct ifmultiaddr *ifma, *rifma = NULL;
1015 struct sockaddr_dl sdl;
1018 LAGG_WLOCK_ASSERT(sc);
1020 bzero((char *)&sdl, sizeof(sdl));
1021 sdl.sdl_len = sizeof(sdl);
1022 sdl.sdl_family = AF_LINK;
1023 sdl.sdl_type = IFT_ETHER;
1024 sdl.sdl_alen = ETHER_ADDR_LEN;
1025 sdl.sdl_index = ifp->if_index;
1028 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1029 if (ifma->ifma_addr->sa_family != AF_LINK)
1031 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1032 LLADDR(&sdl), ETHER_ADDR_LEN);
1034 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1037 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1040 mc->mc_ifma = rifma;
1041 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1044 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1045 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1046 if_delmulti_ifma(mc->mc_ifma);
1053 /* Handle a ref counted flag that should be set on the lagg port as well */
1055 lagg_setflag(struct lagg_port *lp, int flag, int status,
1056 int (*func)(struct ifnet *, int))
1058 struct lagg_softc *sc = lp->lp_softc;
1059 struct ifnet *scifp = sc->sc_ifp;
1060 struct ifnet *ifp = lp->lp_ifp;
1063 LAGG_WLOCK_ASSERT(sc);
1065 status = status ? (scifp->if_flags & flag) : 0;
1066 /* Now "status" contains the flag value or 0 */
1069 * See if recorded ports status is different from what
1070 * we want it to be. If it is, flip it. We record ports
1071 * status in lp_ifflags so that we won't clear ports flag
1072 * we haven't set. In fact, we don't clear or set ports
1073 * flags directly, but get or release references to them.
1074 * That's why we can be sure that recorded flags still are
1075 * in accord with actual ports flags.
1077 if (status != (lp->lp_ifflags & flag)) {
1078 error = (*func)(ifp, status);
1081 lp->lp_ifflags &= ~flag;
1082 lp->lp_ifflags |= status;
1088 * Handle IFF_* flags that require certain changes on the lagg port
1089 * if "status" is true, update ports flags respective to the lagg
1090 * if "status" is false, forcedly clear the flags set on port.
1093 lagg_setflags(struct lagg_port *lp, int status)
1097 for (i = 0; lagg_pflags[i].flag; i++) {
1098 error = lagg_setflag(lp, lagg_pflags[i].flag,
1099 status, lagg_pflags[i].func);
1107 lagg_start(struct ifnet *ifp)
1109 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1114 for (;; error = 0) {
1115 IFQ_DEQUEUE(&ifp->if_snd, m);
1119 ETHER_BPF_MTAP(ifp, m);
1121 if (sc->sc_proto != LAGG_PROTO_NONE)
1122 error = (*sc->sc_start)(sc, m);
1136 static struct mbuf *
1137 lagg_input(struct ifnet *ifp, struct mbuf *m)
1139 struct lagg_port *lp = ifp->if_lagg;
1140 struct lagg_softc *sc = lp->lp_softc;
1141 struct ifnet *scifp = sc->sc_ifp;
1143 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1144 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1145 sc->sc_proto == LAGG_PROTO_NONE) {
1151 ETHER_BPF_MTAP(scifp, m);
1153 m = (*sc->sc_input)(sc, lp, m);
1156 scifp->if_ipackets++;
1157 scifp->if_ibytes += m->m_pkthdr.len;
1159 if (scifp->if_flags & IFF_MONITOR) {
1170 lagg_media_change(struct ifnet *ifp)
1172 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1174 if (sc->sc_ifflags & IFF_DEBUG)
1175 printf("%s\n", __func__);
1182 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1184 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1185 struct lagg_port *lp;
1187 imr->ifm_status = IFM_AVALID;
1188 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1191 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1192 if (LAGG_PORTACTIVE(lp))
1193 imr->ifm_status |= IFM_ACTIVE;
1199 lagg_linkstate(struct lagg_softc *sc)
1201 struct lagg_port *lp;
1202 int new_link = LINK_STATE_DOWN;
1204 /* Our link is considered up if at least one of our ports is active */
1205 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1206 if (lp->lp_link_state == LINK_STATE_UP) {
1207 new_link = LINK_STATE_UP;
1211 if_link_state_change(sc->sc_ifp, new_link);
1215 lagg_port_state(struct ifnet *ifp, int state)
1217 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1218 struct lagg_softc *sc = NULL;
1227 if (sc->sc_linkstate != NULL)
1228 (*sc->sc_linkstate)(lp);
1233 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1235 struct lagg_port *lp_next, *rval = NULL;
1236 // int new_link = LINK_STATE_DOWN;
1238 LAGG_RLOCK_ASSERT(sc);
1240 * Search a port which reports an active link state.
1245 if (LAGG_PORTACTIVE(lp)) {
1249 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1250 LAGG_PORTACTIVE(lp_next)) {
1256 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1257 if (LAGG_PORTACTIVE(lp_next)) {
1266 * The IEEE 802.1D standard assumes that a lagg with
1267 * multiple ports is always full duplex. This is valid
1268 * for load sharing laggs and if at least two links
1269 * are active. Unfortunately, checking the latter would
1270 * be too expensive at this point.
1272 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1274 new_link = LINK_STATE_FULL_DUPLEX;
1276 new_link = rval->lp_link_state;
1284 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1286 if (m->m_pkthdr.len < (off + len)) {
1288 } else if (m->m_len < (off + len)) {
1289 m_copydata(m, off, len, buf);
1292 return (mtod(m, char *) + off);
1296 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1301 struct ether_header *eh;
1302 struct ether_vlan_header vlanbuf;
1303 const struct ether_vlan_header *vlan;
1305 const struct ip *ip;
1309 const struct ip6_hdr *ip6;
1310 struct ip6_hdr ip6buf;
1317 eh = mtod(m, struct ether_header *);
1318 etype = ntohs(eh->ether_type);
1319 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1320 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1322 /* Special handling for encapsulating VLAN frames */
1323 if (m->m_flags & M_VLANTAG) {
1324 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1325 sizeof(m->m_pkthdr.ether_vtag), p);
1326 } else if (etype == ETHERTYPE_VLAN) {
1327 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1331 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1332 etype = ntohs(vlan->evl_proto);
1333 off += sizeof(*vlan) - sizeof(*eh);
1339 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1343 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1344 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1348 case ETHERTYPE_IPV6:
1349 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1353 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1354 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1355 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1356 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1365 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1369 IFQ_HANDOFF(ifp, m, error);
1376 * Simple round robin aggregation
1380 lagg_rr_attach(struct lagg_softc *sc)
1382 sc->sc_detach = lagg_rr_detach;
1383 sc->sc_start = lagg_rr_start;
1384 sc->sc_input = lagg_rr_input;
1385 sc->sc_port_create = NULL;
1386 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1393 lagg_rr_detach(struct lagg_softc *sc)
1399 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1401 struct lagg_port *lp;
1404 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1406 lp = SLIST_FIRST(&sc->sc_ports);
1408 lp = SLIST_NEXT(lp, lp_entries);
1411 * Check the port's link state. This will return the next active
1412 * port if the link is down or the port is NULL.
1414 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1420 return (lagg_enqueue(lp->lp_ifp, m));
1423 static struct mbuf *
1424 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1426 struct ifnet *ifp = sc->sc_ifp;
1428 /* Just pass in the packet to our lagg device */
1429 m->m_pkthdr.rcvif = ifp;
1439 lagg_fail_attach(struct lagg_softc *sc)
1441 sc->sc_detach = lagg_fail_detach;
1442 sc->sc_start = lagg_fail_start;
1443 sc->sc_input = lagg_fail_input;
1444 sc->sc_port_create = NULL;
1445 sc->sc_port_destroy = NULL;
1451 lagg_fail_detach(struct lagg_softc *sc)
1457 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1459 struct lagg_port *lp;
1461 /* Use the master port if active or the next available port */
1462 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1468 return (lagg_enqueue(lp->lp_ifp, m));
1471 static struct mbuf *
1472 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1474 struct ifnet *ifp = sc->sc_ifp;
1475 struct lagg_port *tmp_tp;
1477 if (lp == sc->sc_primary) {
1478 m->m_pkthdr.rcvif = ifp;
1482 if (sc->sc_primary->lp_link_state == LINK_STATE_DOWN) {
1483 tmp_tp = lagg_link_active(sc, NULL);
1485 * If tmp_tp is null, we've recieved a packet when all
1486 * our links are down. Weird, but process it anyways.
1488 if ((tmp_tp == NULL || tmp_tp == lp)) {
1489 m->m_pkthdr.rcvif = ifp;
1503 lagg_lb_attach(struct lagg_softc *sc)
1505 struct lagg_port *lp;
1508 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1509 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1512 sc->sc_detach = lagg_lb_detach;
1513 sc->sc_start = lagg_lb_start;
1514 sc->sc_input = lagg_lb_input;
1515 sc->sc_port_create = lagg_lb_port_create;
1516 sc->sc_port_destroy = lagg_lb_port_destroy;
1517 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1519 lb->lb_key = arc4random();
1520 sc->sc_psc = (caddr_t)lb;
1522 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1523 lagg_lb_port_create(lp);
1529 lagg_lb_detach(struct lagg_softc *sc)
1531 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1538 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1540 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1541 struct lagg_port *lp_next;
1544 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1545 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1548 if (i >= LAGG_MAX_PORTS)
1550 if (sc->sc_ifflags & IFF_DEBUG)
1551 printf("%s: port %s at index %d\n",
1552 sc->sc_ifname, lp_next->lp_ifname, i);
1553 lb->lb_ports[i++] = lp_next;
1560 lagg_lb_port_create(struct lagg_port *lp)
1562 struct lagg_softc *sc = lp->lp_softc;
1563 return (lagg_lb_porttable(sc, NULL));
1567 lagg_lb_port_destroy(struct lagg_port *lp)
1569 struct lagg_softc *sc = lp->lp_softc;
1570 lagg_lb_porttable(sc, lp);
1574 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1576 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1577 struct lagg_port *lp = NULL;
1581 p = lagg_hashmbuf(m, lb->lb_key);
1582 if ((idx = p % sc->sc_count) >= LAGG_MAX_PORTS) {
1586 lp = lb->lb_ports[idx];
1589 * Check the port's link state. This will return the next active
1590 * port if the link is down or the port is NULL.
1592 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1598 return (lagg_enqueue(lp->lp_ifp, m));
1601 static struct mbuf *
1602 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1604 struct ifnet *ifp = sc->sc_ifp;
1606 /* Just pass in the packet to our lagg device */
1607 m->m_pkthdr.rcvif = ifp;
1617 lagg_lacp_attach(struct lagg_softc *sc)
1619 struct lagg_port *lp;
1622 sc->sc_detach = lagg_lacp_detach;
1623 sc->sc_port_create = lacp_port_create;
1624 sc->sc_port_destroy = lacp_port_destroy;
1625 sc->sc_linkstate = lacp_linkstate;
1626 sc->sc_start = lagg_lacp_start;
1627 sc->sc_input = lagg_lacp_input;
1628 sc->sc_init = lacp_init;
1629 sc->sc_stop = lacp_stop;
1630 sc->sc_lladdr = lagg_lacp_lladdr;
1631 sc->sc_req = lacp_req;
1632 sc->sc_portreq = lacp_portreq;
1634 error = lacp_attach(sc);
1638 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1639 lacp_port_create(lp);
1645 lagg_lacp_detach(struct lagg_softc *sc)
1647 struct lagg_port *lp;
1650 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1651 lacp_port_destroy(lp);
1653 /* unlocking is safe here */
1655 error = lacp_detach(sc);
1662 lagg_lacp_lladdr(struct lagg_softc *sc)
1664 struct lagg_port *lp;
1666 /* purge all the lacp ports */
1667 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1668 lacp_port_destroy(lp);
1670 /* add them back in */
1671 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1672 lacp_port_create(lp);
1676 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1678 struct lagg_port *lp;
1680 lp = lacp_select_tx_port(sc, m);
1687 return (lagg_enqueue(lp->lp_ifp, m));
1690 static struct mbuf *
1691 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1693 struct ifnet *ifp = sc->sc_ifp;
1694 struct ether_header *eh;
1697 eh = mtod(m, struct ether_header *);
1698 etype = ntohs(eh->ether_type);
1700 /* Tap off LACP control messages */
1701 if (etype == ETHERTYPE_SLOW) {
1707 * If the port is not collecting or not in the active aggregator then
1710 if ((lp->lp_flags & LAGG_PORT_COLLECTING) == 0 ||
1711 lacp_port_isactive(lp) == 0) {
1716 m->m_pkthdr.rcvif = ifp;