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);
309 lagg_capabilities(struct lagg_softc *sc)
311 struct lagg_port *lp;
312 int cap = ~0, ena = ~0;
315 LAGG_WLOCK_ASSERT(sc);
317 /* Get capabilities from the lagg ports */
318 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
319 cap &= lp->lp_ifp->if_capabilities;
320 ena &= lp->lp_ifp->if_capenable;
321 hwa &= lp->lp_ifp->if_hwassist;
323 cap = (cap == ~0 ? 0 : cap);
324 ena = (ena == ~0 ? 0 : ena);
325 hwa = (hwa == ~0 ? 0 : hwa);
327 if (sc->sc_ifp->if_capabilities != cap ||
328 sc->sc_ifp->if_capenable != ena ||
329 sc->sc_ifp->if_hwassist != hwa) {
330 sc->sc_ifp->if_capabilities = cap;
331 sc->sc_ifp->if_capenable = ena;
332 sc->sc_ifp->if_hwassist = hwa;
333 getmicrotime(&sc->sc_ifp->if_lastchange);
335 if (sc->sc_ifflags & IFF_DEBUG)
336 if_printf(sc->sc_ifp,
337 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
342 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
344 struct lagg_softc *sc = lp->lp_softc;
345 struct ifnet *ifp = lp->lp_ifp;
346 struct lagg_llq *llq;
349 LAGG_WLOCK_ASSERT(sc);
351 if (lp->lp_detaching ||
352 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
355 /* Check to make sure its not already queued to be changed */
356 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
357 if (llq->llq_ifp == ifp) {
364 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
365 if (llq == NULL) /* XXX what to do */
369 /* Update the lladdr even if pending, it may have changed */
371 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
374 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
376 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
380 * Set the interface MAC address from a taskqueue to avoid a LOR.
383 lagg_port_setlladdr(void *arg, int pending)
385 struct lagg_softc *sc = (struct lagg_softc *)arg;
386 struct lagg_llq *llq, *head;
390 /* Grab a local reference of the queue and remove it from the softc */
392 head = SLIST_FIRST(&sc->sc_llq_head);
393 SLIST_FIRST(&sc->sc_llq_head) = NULL;
397 * Traverse the queue and set the lladdr on each ifp. It is safe to do
398 * unlocked as we have the only reference to it.
400 for (llq = head; llq != NULL; llq = head) {
403 /* Set the link layer address */
404 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
406 printf("%s: setlladdr failed on %s\n", __func__,
409 head = SLIST_NEXT(llq, llq_entries);
415 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
417 struct lagg_softc *sc_ptr;
418 struct lagg_port *lp;
421 LAGG_WLOCK_ASSERT(sc);
423 /* Limit the maximal number of lagg ports */
424 if (sc->sc_count >= LAGG_MAX_PORTS)
427 /* New lagg port has to be in an idle state */
428 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
431 /* Check if port has already been associated to a lagg */
432 if (ifp->if_lagg != NULL)
435 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
436 if (ifp->if_type != IFT_ETHER)
437 return (EPROTONOSUPPORT);
439 /* Allow the first Ethernet member to define the MTU */
440 if (SLIST_EMPTY(&sc->sc_ports))
441 sc->sc_ifp->if_mtu = ifp->if_mtu;
442 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
443 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
448 if ((lp = malloc(sizeof(struct lagg_port),
449 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
452 /* Check if port is a stacked lagg */
453 mtx_lock(&lagg_list_mtx);
454 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
455 if (ifp == sc_ptr->sc_ifp) {
456 mtx_unlock(&lagg_list_mtx);
459 /* XXX disable stacking for the moment, its untested
460 lp->lp_flags |= LAGG_PORT_STACK;
461 if (lagg_port_checkstacking(sc_ptr) >=
463 mtx_unlock(&lagg_list_mtx);
470 mtx_unlock(&lagg_list_mtx);
472 /* Change the interface type */
473 lp->lp_iftype = ifp->if_type;
474 ifp->if_type = IFT_IEEE8023ADLAG;
476 lp->lp_ioctl = ifp->if_ioctl;
477 ifp->if_ioctl = lagg_port_ioctl;
478 lp->lp_output = ifp->if_output;
479 ifp->if_output = lagg_port_output;
484 /* Save port link layer address */
485 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
487 if (SLIST_EMPTY(&sc->sc_ports)) {
489 lagg_lladdr(sc, IF_LLADDR(ifp));
491 /* Update link layer address for this port */
492 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
495 /* Insert into the list of ports */
496 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
499 /* Update lagg capabilities */
500 lagg_capabilities(sc);
503 /* Add multicast addresses and interface flags to this port */
504 lagg_ether_cmdmulti(lp, 1);
505 lagg_setflags(lp, 1);
507 if (sc->sc_port_create != NULL)
508 error = (*sc->sc_port_create)(lp);
510 /* remove the port again, without calling sc_port_destroy */
511 lagg_port_destroy(lp, 0);
519 lagg_port_checkstacking(struct lagg_softc *sc)
521 struct lagg_softc *sc_ptr;
522 struct lagg_port *lp;
525 LAGG_WLOCK_ASSERT(sc);
527 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
528 if (lp->lp_flags & LAGG_PORT_STACK) {
529 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
530 m = MAX(m, lagg_port_checkstacking(sc_ptr));
538 lagg_port_destroy(struct lagg_port *lp, int runpd)
540 struct lagg_softc *sc = lp->lp_softc;
541 struct lagg_port *lp_ptr;
542 struct lagg_llq *llq;
543 struct ifnet *ifp = lp->lp_ifp;
545 LAGG_WLOCK_ASSERT(sc);
547 if (runpd && sc->sc_port_destroy != NULL)
548 (*sc->sc_port_destroy)(lp);
551 * Remove multicast addresses and interface flags from this port and
552 * reset the MAC address, skip if the interface is being detached.
554 if (!lp->lp_detaching) {
555 lagg_ether_cmdmulti(lp, 0);
556 lagg_setflags(lp, 0);
557 lagg_port_lladdr(lp, lp->lp_lladdr);
560 /* Restore interface */
561 ifp->if_type = lp->lp_iftype;
562 ifp->if_ioctl = lp->lp_ioctl;
563 ifp->if_output = lp->lp_output;
566 /* Finally, remove the port from the lagg */
567 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
570 /* Update the primary interface */
571 if (lp == sc->sc_primary) {
572 uint8_t lladdr[ETHER_ADDR_LEN];
574 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
575 bzero(&lladdr, ETHER_ADDR_LEN);
577 bcopy(lp_ptr->lp_lladdr,
578 lladdr, ETHER_ADDR_LEN);
580 lagg_lladdr(sc, lladdr);
581 sc->sc_primary = lp_ptr;
583 /* Update link layer address for each port */
584 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
585 lagg_port_lladdr(lp_ptr, lladdr);
588 /* Remove any pending lladdr changes from the queue */
589 if (lp->lp_detaching) {
590 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
591 if (llq->llq_ifp == ifp) {
592 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
595 break; /* Only appears once */
601 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
605 /* Update lagg capabilities */
606 lagg_capabilities(sc);
613 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
615 struct lagg_reqport *rp = (struct lagg_reqport *)data;
616 struct lagg_softc *sc;
617 struct lagg_port *lp = NULL;
620 /* Should be checked by the caller */
621 if (ifp->if_type != IFT_IEEE8023ADLAG ||
622 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
627 if (rp->rp_portname[0] == '\0' ||
628 ifunit(rp->rp_portname) != ifp) {
634 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
640 lagg_port2req(lp, rp);
645 if (lp->lp_ioctl == NULL) {
649 error = (*lp->lp_ioctl)(ifp, cmd, data);
653 /* Update lagg interface capabilities */
655 lagg_capabilities(sc);
660 /* Do not allow the MTU to be changed once joined */
671 if (lp->lp_ioctl != NULL)
672 return ((*lp->lp_ioctl)(ifp, cmd, data));
678 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
679 struct sockaddr *dst, struct route *ro)
681 struct lagg_port *lp = ifp->if_lagg;
682 struct ether_header *eh;
685 switch (dst->sa_family) {
686 case pseudo_AF_HDRCMPLT:
688 eh = (struct ether_header *)dst->sa_data;
689 type = eh->ether_type;
694 * Only allow ethernet types required to initiate or maintain the link,
695 * aggregated frames take a different path.
697 switch (ntohs(type)) {
698 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
699 return ((*lp->lp_output)(ifp, m, dst, ro));
702 /* drop any other frames */
708 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
710 struct lagg_port *lp;
711 struct lagg_softc *sc;
713 if ((lp = ifp->if_lagg) == NULL)
719 lp->lp_detaching = 1;
720 lagg_port_destroy(lp, 1);
725 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
727 struct lagg_softc *sc = lp->lp_softc;
729 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
730 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
731 rp->rp_prio = lp->lp_prio;
732 rp->rp_flags = lp->lp_flags;
733 if (sc->sc_portreq != NULL)
734 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
736 /* Add protocol specific flags */
737 switch (sc->sc_proto) {
738 case LAGG_PROTO_FAILOVER:
739 if (lp == sc->sc_primary)
740 rp->rp_flags |= LAGG_PORT_MASTER;
741 if (lp == lagg_link_active(sc, sc->sc_primary))
742 rp->rp_flags |= LAGG_PORT_ACTIVE;
745 case LAGG_PROTO_ROUNDROBIN:
746 case LAGG_PROTO_LOADBALANCE:
747 case LAGG_PROTO_ETHERCHANNEL:
748 if (LAGG_PORTACTIVE(lp))
749 rp->rp_flags |= LAGG_PORT_ACTIVE;
752 case LAGG_PROTO_LACP:
753 /* LACP has a different definition of active */
754 if (lacp_isactive(lp))
755 rp->rp_flags |= LAGG_PORT_ACTIVE;
756 if (lacp_iscollecting(lp))
757 rp->rp_flags |= LAGG_PORT_COLLECTING;
758 if (lacp_isdistributing(lp))
759 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
768 struct lagg_softc *sc = (struct lagg_softc *)xsc;
769 struct lagg_port *lp;
770 struct ifnet *ifp = sc->sc_ifp;
772 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
777 ifp->if_drv_flags |= IFF_DRV_RUNNING;
778 /* Update the port lladdrs */
779 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
780 lagg_port_lladdr(lp, IF_LLADDR(ifp));
782 if (sc->sc_init != NULL)
789 lagg_stop(struct lagg_softc *sc)
791 struct ifnet *ifp = sc->sc_ifp;
793 LAGG_WLOCK_ASSERT(sc);
795 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
798 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
800 if (sc->sc_stop != NULL)
805 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
807 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
808 struct lagg_reqall *ra = (struct lagg_reqall *)data;
809 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
810 struct ifreq *ifr = (struct ifreq *)data;
811 struct lagg_port *lp;
813 struct thread *td = curthread;
815 int count, buflen, len, error = 0;
817 bzero(&rpbuf, sizeof(rpbuf));
823 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
825 buflen = count * sizeof(struct lagg_reqport);
828 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
831 ra->ra_proto = sc->sc_proto;
832 if (sc->sc_req != NULL)
833 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
837 len = min(ra->ra_size, buflen);
838 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
839 if (len < sizeof(rpbuf))
842 lagg_port2req(lp, &rpbuf);
843 memcpy(buf, &rpbuf, sizeof(rpbuf));
845 buf += sizeof(rpbuf);
846 len -= sizeof(rpbuf);
849 ra->ra_ports = count;
850 ra->ra_size = count * sizeof(rpbuf);
851 error = copyout(outbuf, ra->ra_port, ra->ra_size);
852 free(outbuf, M_TEMP);
855 error = priv_check(td, PRIV_NET_LAGG);
858 if (ra->ra_proto >= LAGG_PROTO_MAX) {
859 error = EPROTONOSUPPORT;
862 if (sc->sc_proto != LAGG_PROTO_NONE) {
864 error = sc->sc_detach(sc);
865 /* Reset protocol and pointers */
866 sc->sc_proto = LAGG_PROTO_NONE;
867 sc->sc_detach = NULL;
870 sc->sc_port_create = NULL;
871 sc->sc_port_destroy = NULL;
872 sc->sc_linkstate = NULL;
875 sc->sc_lladdr = NULL;
877 sc->sc_portreq = NULL;
882 for (int i = 0; i < (sizeof(lagg_protos) /
883 sizeof(lagg_protos[0])); i++) {
884 if (lagg_protos[i].ti_proto == ra->ra_proto) {
885 if (sc->sc_ifflags & IFF_DEBUG)
886 printf("%s: using proto %u\n",
888 lagg_protos[i].ti_proto);
890 sc->sc_proto = lagg_protos[i].ti_proto;
891 if (sc->sc_proto != LAGG_PROTO_NONE)
892 error = lagg_protos[i].ti_attach(sc);
897 error = EPROTONOSUPPORT;
900 if (rp->rp_portname[0] == '\0' ||
901 (tpif = ifunit(rp->rp_portname)) == NULL) {
907 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
908 lp->lp_softc != sc) {
914 lagg_port2req(lp, rp);
918 error = priv_check(td, PRIV_NET_LAGG);
921 if (rp->rp_portname[0] == '\0' ||
922 (tpif = ifunit(rp->rp_portname)) == NULL) {
927 error = lagg_port_create(sc, tpif);
930 case SIOCSLAGGDELPORT:
931 error = priv_check(td, PRIV_NET_LAGG);
934 if (rp->rp_portname[0] == '\0' ||
935 (tpif = ifunit(rp->rp_portname)) == NULL) {
941 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
942 lp->lp_softc != sc) {
948 error = lagg_port_destroy(lp, 1);
952 /* Set flags on ports too */
954 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
955 lagg_setflags(lp, 1);
959 if (!(ifp->if_flags & IFF_UP) &&
960 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
962 * If interface is marked down and it is running,
963 * then stop and disable it.
968 } else if ((ifp->if_flags & IFF_UP) &&
969 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
971 * If interface is marked up and it is stopped, then
980 error = lagg_ether_setmulti(sc);
985 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
990 /* Do not allow the MTU or caps to be directly changed */
995 error = ether_ioctl(ifp, cmd, data);
1002 lagg_ether_setmulti(struct lagg_softc *sc)
1004 struct lagg_port *lp;
1006 LAGG_WLOCK_ASSERT(sc);
1008 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1009 /* First, remove any existing filter entries. */
1010 lagg_ether_cmdmulti(lp, 0);
1011 /* copy all addresses from the lagg interface to the port */
1012 lagg_ether_cmdmulti(lp, 1);
1018 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1020 struct lagg_softc *sc = lp->lp_softc;
1021 struct ifnet *ifp = lp->lp_ifp;
1022 struct ifnet *scifp = sc->sc_ifp;
1024 struct ifmultiaddr *ifma, *rifma = NULL;
1025 struct sockaddr_dl sdl;
1028 LAGG_WLOCK_ASSERT(sc);
1030 bzero((char *)&sdl, sizeof(sdl));
1031 sdl.sdl_len = sizeof(sdl);
1032 sdl.sdl_family = AF_LINK;
1033 sdl.sdl_type = IFT_ETHER;
1034 sdl.sdl_alen = ETHER_ADDR_LEN;
1035 sdl.sdl_index = ifp->if_index;
1038 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1039 if (ifma->ifma_addr->sa_family != AF_LINK)
1041 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1042 LLADDR(&sdl), ETHER_ADDR_LEN);
1044 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1047 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1050 mc->mc_ifma = rifma;
1051 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1054 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1055 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1056 if_delmulti_ifma(mc->mc_ifma);
1063 /* Handle a ref counted flag that should be set on the lagg port as well */
1065 lagg_setflag(struct lagg_port *lp, int flag, int status,
1066 int (*func)(struct ifnet *, int))
1068 struct lagg_softc *sc = lp->lp_softc;
1069 struct ifnet *scifp = sc->sc_ifp;
1070 struct ifnet *ifp = lp->lp_ifp;
1073 LAGG_WLOCK_ASSERT(sc);
1075 status = status ? (scifp->if_flags & flag) : 0;
1076 /* Now "status" contains the flag value or 0 */
1079 * See if recorded ports status is different from what
1080 * we want it to be. If it is, flip it. We record ports
1081 * status in lp_ifflags so that we won't clear ports flag
1082 * we haven't set. In fact, we don't clear or set ports
1083 * flags directly, but get or release references to them.
1084 * That's why we can be sure that recorded flags still are
1085 * in accord with actual ports flags.
1087 if (status != (lp->lp_ifflags & flag)) {
1088 error = (*func)(ifp, status);
1091 lp->lp_ifflags &= ~flag;
1092 lp->lp_ifflags |= status;
1098 * Handle IFF_* flags that require certain changes on the lagg port
1099 * if "status" is true, update ports flags respective to the lagg
1100 * if "status" is false, forcedly clear the flags set on port.
1103 lagg_setflags(struct lagg_port *lp, int status)
1107 for (i = 0; lagg_pflags[i].flag; i++) {
1108 error = lagg_setflag(lp, lagg_pflags[i].flag,
1109 status, lagg_pflags[i].func);
1117 lagg_start(struct ifnet *ifp)
1119 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1124 /* We need a Tx algorithm and at least one port */
1125 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1126 IF_DRAIN(&ifp->if_snd);
1131 for (;; error = 0) {
1132 IFQ_DEQUEUE(&ifp->if_snd, m);
1136 ETHER_BPF_MTAP(ifp, m);
1138 error = (*sc->sc_start)(sc, m);
1147 static struct mbuf *
1148 lagg_input(struct ifnet *ifp, struct mbuf *m)
1150 struct lagg_port *lp = ifp->if_lagg;
1151 struct lagg_softc *sc = lp->lp_softc;
1152 struct ifnet *scifp = sc->sc_ifp;
1154 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1155 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1156 sc->sc_proto == LAGG_PROTO_NONE) {
1162 ETHER_BPF_MTAP(scifp, m);
1164 m = (*sc->sc_input)(sc, lp, m);
1167 scifp->if_ipackets++;
1168 scifp->if_ibytes += m->m_pkthdr.len;
1170 if (scifp->if_flags & IFF_MONITOR) {
1181 lagg_media_change(struct ifnet *ifp)
1183 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1185 if (sc->sc_ifflags & IFF_DEBUG)
1186 printf("%s\n", __func__);
1193 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1195 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1196 struct lagg_port *lp;
1198 imr->ifm_status = IFM_AVALID;
1199 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1202 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1203 if (LAGG_PORTACTIVE(lp))
1204 imr->ifm_status |= IFM_ACTIVE;
1210 lagg_linkstate(struct lagg_softc *sc)
1212 struct lagg_port *lp;
1213 int new_link = LINK_STATE_DOWN;
1216 /* Our link is considered up if at least one of our ports is active */
1217 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1218 if (lp->lp_link_state == LINK_STATE_UP) {
1219 new_link = LINK_STATE_UP;
1223 if_link_state_change(sc->sc_ifp, new_link);
1225 /* Update if_baudrate to reflect the max possible speed */
1226 switch (sc->sc_proto) {
1227 case LAGG_PROTO_FAILOVER:
1228 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1229 sc->sc_primary->lp_ifp->if_baudrate : 0;
1231 case LAGG_PROTO_ROUNDROBIN:
1232 case LAGG_PROTO_LOADBALANCE:
1233 case LAGG_PROTO_ETHERCHANNEL:
1235 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1236 speed += lp->lp_ifp->if_baudrate;
1237 sc->sc_ifp->if_baudrate = speed;
1239 case LAGG_PROTO_LACP:
1240 /* LACP updates if_baudrate itself */
1246 lagg_port_state(struct ifnet *ifp, int state)
1248 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1249 struct lagg_softc *sc = NULL;
1258 if (sc->sc_linkstate != NULL)
1259 (*sc->sc_linkstate)(lp);
1264 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1266 struct lagg_port *lp_next, *rval = NULL;
1267 // int new_link = LINK_STATE_DOWN;
1269 LAGG_RLOCK_ASSERT(sc);
1271 * Search a port which reports an active link state.
1276 if (LAGG_PORTACTIVE(lp)) {
1280 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1281 LAGG_PORTACTIVE(lp_next)) {
1287 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1288 if (LAGG_PORTACTIVE(lp_next)) {
1297 * The IEEE 802.1D standard assumes that a lagg with
1298 * multiple ports is always full duplex. This is valid
1299 * for load sharing laggs and if at least two links
1300 * are active. Unfortunately, checking the latter would
1301 * be too expensive at this point.
1303 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1305 new_link = LINK_STATE_FULL_DUPLEX;
1307 new_link = rval->lp_link_state;
1315 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1317 if (m->m_pkthdr.len < (off + len)) {
1319 } else if (m->m_len < (off + len)) {
1320 m_copydata(m, off, len, buf);
1323 return (mtod(m, char *) + off);
1327 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1332 struct ether_header *eh;
1333 struct ether_vlan_header vlanbuf;
1334 const struct ether_vlan_header *vlan;
1336 const struct ip *ip;
1340 const struct ip6_hdr *ip6;
1341 struct ip6_hdr ip6buf;
1348 eh = mtod(m, struct ether_header *);
1349 etype = ntohs(eh->ether_type);
1350 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1351 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1353 /* Special handling for encapsulating VLAN frames */
1354 if (m->m_flags & M_VLANTAG) {
1355 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1356 sizeof(m->m_pkthdr.ether_vtag), p);
1357 } else if (etype == ETHERTYPE_VLAN) {
1358 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1362 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1363 etype = ntohs(vlan->evl_proto);
1364 off += sizeof(*vlan) - sizeof(*eh);
1370 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1374 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1375 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1379 case ETHERTYPE_IPV6:
1380 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1384 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1385 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1386 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1387 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1396 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1399 return (ifp->if_transmit)(ifp, m);
1403 * Simple round robin aggregation
1407 lagg_rr_attach(struct lagg_softc *sc)
1409 sc->sc_detach = lagg_rr_detach;
1410 sc->sc_start = lagg_rr_start;
1411 sc->sc_input = lagg_rr_input;
1412 sc->sc_port_create = NULL;
1413 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1420 lagg_rr_detach(struct lagg_softc *sc)
1426 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1428 struct lagg_port *lp;
1431 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1433 lp = SLIST_FIRST(&sc->sc_ports);
1435 lp = SLIST_NEXT(lp, lp_entries);
1438 * Check the port's link state. This will return the next active
1439 * port if the link is down or the port is NULL.
1441 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1447 return (lagg_enqueue(lp->lp_ifp, m));
1450 static struct mbuf *
1451 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1453 struct ifnet *ifp = sc->sc_ifp;
1455 /* Just pass in the packet to our lagg device */
1456 m->m_pkthdr.rcvif = ifp;
1466 lagg_fail_attach(struct lagg_softc *sc)
1468 sc->sc_detach = lagg_fail_detach;
1469 sc->sc_start = lagg_fail_start;
1470 sc->sc_input = lagg_fail_input;
1471 sc->sc_port_create = NULL;
1472 sc->sc_port_destroy = NULL;
1478 lagg_fail_detach(struct lagg_softc *sc)
1484 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1486 struct lagg_port *lp;
1488 /* Use the master port if active or the next available port */
1489 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1495 return (lagg_enqueue(lp->lp_ifp, m));
1498 static struct mbuf *
1499 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1501 struct ifnet *ifp = sc->sc_ifp;
1502 struct lagg_port *tmp_tp;
1504 if (lp == sc->sc_primary) {
1505 m->m_pkthdr.rcvif = ifp;
1509 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1510 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1512 * If tmp_tp is null, we've recieved a packet when all
1513 * our links are down. Weird, but process it anyways.
1515 if ((tmp_tp == NULL || tmp_tp == lp)) {
1516 m->m_pkthdr.rcvif = ifp;
1530 lagg_lb_attach(struct lagg_softc *sc)
1532 struct lagg_port *lp;
1535 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1536 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1539 sc->sc_detach = lagg_lb_detach;
1540 sc->sc_start = lagg_lb_start;
1541 sc->sc_input = lagg_lb_input;
1542 sc->sc_port_create = lagg_lb_port_create;
1543 sc->sc_port_destroy = lagg_lb_port_destroy;
1544 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1546 lb->lb_key = arc4random();
1547 sc->sc_psc = (caddr_t)lb;
1549 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1550 lagg_lb_port_create(lp);
1556 lagg_lb_detach(struct lagg_softc *sc)
1558 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1565 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1567 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1568 struct lagg_port *lp_next;
1571 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1572 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1575 if (i >= LAGG_MAX_PORTS)
1577 if (sc->sc_ifflags & IFF_DEBUG)
1578 printf("%s: port %s at index %d\n",
1579 sc->sc_ifname, lp_next->lp_ifname, i);
1580 lb->lb_ports[i++] = lp_next;
1587 lagg_lb_port_create(struct lagg_port *lp)
1589 struct lagg_softc *sc = lp->lp_softc;
1590 return (lagg_lb_porttable(sc, NULL));
1594 lagg_lb_port_destroy(struct lagg_port *lp)
1596 struct lagg_softc *sc = lp->lp_softc;
1597 lagg_lb_porttable(sc, lp);
1601 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1603 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1604 struct lagg_port *lp = NULL;
1607 if (m->m_flags & M_FLOWID)
1608 p = m->m_pkthdr.flowid;
1610 p = lagg_hashmbuf(m, lb->lb_key);
1612 lp = lb->lb_ports[p];
1615 * Check the port's link state. This will return the next active
1616 * port if the link is down or the port is NULL.
1618 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1624 return (lagg_enqueue(lp->lp_ifp, m));
1627 static struct mbuf *
1628 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1630 struct ifnet *ifp = sc->sc_ifp;
1632 /* Just pass in the packet to our lagg device */
1633 m->m_pkthdr.rcvif = ifp;
1643 lagg_lacp_attach(struct lagg_softc *sc)
1645 struct lagg_port *lp;
1648 sc->sc_detach = lagg_lacp_detach;
1649 sc->sc_port_create = lacp_port_create;
1650 sc->sc_port_destroy = lacp_port_destroy;
1651 sc->sc_linkstate = lacp_linkstate;
1652 sc->sc_start = lagg_lacp_start;
1653 sc->sc_input = lagg_lacp_input;
1654 sc->sc_init = lacp_init;
1655 sc->sc_stop = lacp_stop;
1656 sc->sc_lladdr = lagg_lacp_lladdr;
1657 sc->sc_req = lacp_req;
1658 sc->sc_portreq = lacp_portreq;
1660 error = lacp_attach(sc);
1664 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1665 lacp_port_create(lp);
1671 lagg_lacp_detach(struct lagg_softc *sc)
1673 struct lagg_port *lp;
1676 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1677 lacp_port_destroy(lp);
1679 /* unlocking is safe here */
1681 error = lacp_detach(sc);
1688 lagg_lacp_lladdr(struct lagg_softc *sc)
1690 struct lagg_port *lp;
1692 /* purge all the lacp ports */
1693 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1694 lacp_port_destroy(lp);
1696 /* add them back in */
1697 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1698 lacp_port_create(lp);
1702 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1704 struct lagg_port *lp;
1706 lp = lacp_select_tx_port(sc, m);
1713 return (lagg_enqueue(lp->lp_ifp, m));
1716 static struct mbuf *
1717 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1719 struct ifnet *ifp = sc->sc_ifp;
1720 struct ether_header *eh;
1723 eh = mtod(m, struct ether_header *);
1724 etype = ntohs(eh->ether_type);
1726 /* Tap off LACP control messages */
1727 if (etype == ETHERTYPE_SLOW) {
1728 m = lacp_input(lp, m);
1734 * If the port is not collecting or not in the active aggregator then
1737 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1742 m->m_pkthdr.rcvif = ifp;