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 int 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;
314 LAGG_WLOCK_ASSERT(sc);
316 /* Preserve private capabilities */
317 priv = sc->sc_capabilities & IFCAP_LAGG_MASK;
319 /* Get capabilities from the lagg ports */
320 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
321 cap &= lp->lp_capabilities;
323 if (sc->sc_ifflags & IFF_DEBUG) {
324 printf("%s: capabilities 0x%08x\n",
325 sc->sc_ifname, cap == ~0 ? priv : (cap | priv));
328 return (cap == ~0 ? priv : (cap | priv));
332 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
334 struct lagg_softc *sc = lp->lp_softc;
335 struct ifnet *ifp = lp->lp_ifp;
336 struct lagg_llq *llq;
339 LAGG_WLOCK_ASSERT(sc);
341 if (lp->lp_detaching ||
342 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
345 /* Check to make sure its not already queued to be changed */
346 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
347 if (llq->llq_ifp == ifp) {
354 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
355 if (llq == NULL) /* XXX what to do */
359 /* Update the lladdr even if pending, it may have changed */
361 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
364 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
366 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
370 * Set the interface MAC address from a taskqueue to avoid a LOR.
373 lagg_port_setlladdr(void *arg, int pending)
375 struct lagg_softc *sc = (struct lagg_softc *)arg;
376 struct lagg_llq *llq, *head;
380 /* Grab a local reference of the queue and remove it from the softc */
382 head = SLIST_FIRST(&sc->sc_llq_head);
383 SLIST_FIRST(&sc->sc_llq_head) = NULL;
387 * Traverse the queue and set the lladdr on each ifp. It is safe to do
388 * unlocked as we have the only reference to it.
390 for (llq = head; llq != NULL; llq = head) {
393 /* Set the link layer address */
394 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
396 printf("%s: setlladdr failed on %s\n", __func__,
399 head = SLIST_NEXT(llq, llq_entries);
405 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
407 struct lagg_softc *sc_ptr;
408 struct lagg_port *lp;
411 LAGG_WLOCK_ASSERT(sc);
413 /* Limit the maximal number of lagg ports */
414 if (sc->sc_count >= LAGG_MAX_PORTS)
417 /* New lagg port has to be in an idle state */
418 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
421 /* Check if port has already been associated to a lagg */
422 if (ifp->if_lagg != NULL)
425 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
426 if (ifp->if_type != IFT_ETHER)
427 return (EPROTONOSUPPORT);
429 if ((lp = malloc(sizeof(struct lagg_port),
430 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
433 /* Check if port is a stacked lagg */
434 mtx_lock(&lagg_list_mtx);
435 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
436 if (ifp == sc_ptr->sc_ifp) {
437 mtx_unlock(&lagg_list_mtx);
440 /* XXX disable stacking for the moment, its untested
441 lp->lp_flags |= LAGG_PORT_STACK;
442 if (lagg_port_checkstacking(sc_ptr) >=
444 mtx_unlock(&lagg_list_mtx);
451 mtx_unlock(&lagg_list_mtx);
453 /* Change the interface type */
454 lp->lp_iftype = ifp->if_type;
455 ifp->if_type = IFT_IEEE8023ADLAG;
457 lp->lp_ioctl = ifp->if_ioctl;
458 ifp->if_ioctl = lagg_port_ioctl;
459 lp->lp_output = ifp->if_output;
460 ifp->if_output = lagg_port_output;
465 /* Save port link layer address */
466 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
468 if (SLIST_EMPTY(&sc->sc_ports)) {
470 lagg_lladdr(sc, IF_LLADDR(ifp));
472 /* Update link layer address for this port */
473 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
476 /* Insert into the list of ports */
477 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
480 /* Update lagg capabilities */
481 sc->sc_capabilities = lagg_capabilities(sc);
483 /* Add multicast addresses and interface flags to this port */
484 lagg_ether_cmdmulti(lp, 1);
485 lagg_setflags(lp, 1);
487 if (sc->sc_port_create != NULL)
488 error = (*sc->sc_port_create)(lp);
490 /* remove the port again, without calling sc_port_destroy */
491 lagg_port_destroy(lp, 0);
499 lagg_port_checkstacking(struct lagg_softc *sc)
501 struct lagg_softc *sc_ptr;
502 struct lagg_port *lp;
505 LAGG_WLOCK_ASSERT(sc);
507 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
508 if (lp->lp_flags & LAGG_PORT_STACK) {
509 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
510 m = MAX(m, lagg_port_checkstacking(sc_ptr));
518 lagg_port_destroy(struct lagg_port *lp, int runpd)
520 struct lagg_softc *sc = lp->lp_softc;
521 struct lagg_port *lp_ptr;
522 struct lagg_llq *llq;
523 struct ifnet *ifp = lp->lp_ifp;
525 LAGG_WLOCK_ASSERT(sc);
527 if (runpd && sc->sc_port_destroy != NULL)
528 (*sc->sc_port_destroy)(lp);
531 * Remove multicast addresses and interface flags from this port and
532 * reset the MAC address, skip if the interface is being detached.
534 if (!lp->lp_detaching) {
535 lagg_ether_cmdmulti(lp, 0);
536 lagg_setflags(lp, 0);
537 lagg_port_lladdr(lp, lp->lp_lladdr);
540 /* Restore interface */
541 ifp->if_type = lp->lp_iftype;
542 ifp->if_ioctl = lp->lp_ioctl;
543 ifp->if_output = lp->lp_output;
546 /* Finally, remove the port from the lagg */
547 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
550 /* Update the primary interface */
551 if (lp == sc->sc_primary) {
552 uint8_t lladdr[ETHER_ADDR_LEN];
554 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
555 bzero(&lladdr, ETHER_ADDR_LEN);
557 bcopy(lp_ptr->lp_lladdr,
558 lladdr, ETHER_ADDR_LEN);
560 lagg_lladdr(sc, lladdr);
561 sc->sc_primary = lp_ptr;
563 /* Update link layer address for each port */
564 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
565 lagg_port_lladdr(lp_ptr, lladdr);
568 /* Remove any pending lladdr changes from the queue */
569 if (lp->lp_detaching) {
570 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
571 if (llq->llq_ifp == ifp) {
572 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
575 break; /* Only appears once */
581 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
585 /* Update lagg capabilities */
586 sc->sc_capabilities = lagg_capabilities(sc);
592 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
594 struct lagg_reqport *rp = (struct lagg_reqport *)data;
595 struct lagg_softc *sc;
596 struct lagg_port *lp = NULL;
599 /* Should be checked by the caller */
600 if (ifp->if_type != IFT_IEEE8023ADLAG ||
601 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
606 if (rp->rp_portname[0] == '\0' ||
607 ifunit(rp->rp_portname) != ifp) {
613 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
619 lagg_port2req(lp, rp);
629 if (lp->lp_ioctl != NULL)
630 return ((*lp->lp_ioctl)(ifp, cmd, data));
636 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
637 struct sockaddr *dst, struct rtentry *rt0)
639 struct lagg_port *lp = ifp->if_lagg;
640 struct ether_header *eh;
643 switch (dst->sa_family) {
644 case pseudo_AF_HDRCMPLT:
646 eh = (struct ether_header *)dst->sa_data;
647 type = eh->ether_type;
652 * Only allow ethernet types required to initiate or maintain the link,
653 * aggregated frames take a different path.
655 switch (ntohs(type)) {
656 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
657 return ((*lp->lp_output)(ifp, m, dst, rt0));
660 /* drop any other frames */
666 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
668 struct lagg_port *lp;
669 struct lagg_softc *sc;
671 if ((lp = ifp->if_lagg) == NULL)
677 lp->lp_detaching = 1;
678 lagg_port_destroy(lp, 1);
683 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
685 struct lagg_softc *sc = lp->lp_softc;
686 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
687 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
688 rp->rp_prio = lp->lp_prio;
689 rp->rp_flags = lp->lp_flags;
690 if (sc->sc_portreq != NULL)
691 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
693 /* Add protocol specific flags */
694 switch (sc->sc_proto) {
695 case LAGG_PROTO_FAILOVER:
696 if (lp == sc->sc_primary)
697 rp->rp_flags |= LAGG_PORT_MASTER;
699 case LAGG_PROTO_ROUNDROBIN:
700 case LAGG_PROTO_LOADBALANCE:
701 case LAGG_PROTO_ETHERCHANNEL:
702 if (LAGG_PORTACTIVE(lp))
703 rp->rp_flags |= LAGG_PORT_ACTIVE;
706 case LAGG_PROTO_LACP:
707 /* LACP has a different definition of active */
708 if (lacp_port_isactive(lp))
709 rp->rp_flags |= LAGG_PORT_ACTIVE;
718 struct lagg_softc *sc = (struct lagg_softc *)xsc;
719 struct lagg_port *lp;
720 struct ifnet *ifp = sc->sc_ifp;
722 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
727 ifp->if_drv_flags |= IFF_DRV_RUNNING;
728 /* Update the port lladdrs */
729 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
730 lagg_port_lladdr(lp, IF_LLADDR(ifp));
732 if (sc->sc_init != NULL)
739 lagg_stop(struct lagg_softc *sc)
741 struct ifnet *ifp = sc->sc_ifp;
743 LAGG_WLOCK_ASSERT(sc);
745 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
748 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
750 if (sc->sc_stop != NULL)
755 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
757 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
758 struct lagg_reqall *ra = (struct lagg_reqall *)data;
759 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
760 struct ifreq *ifr = (struct ifreq *)data;
761 struct lagg_port *lp;
763 struct thread *td = curthread;
765 int count, buflen, len, error = 0;
767 bzero(&rpbuf, sizeof(rpbuf));
773 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
775 buflen = count * sizeof(struct lagg_reqport);
778 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
781 ra->ra_proto = sc->sc_proto;
782 if (sc->sc_req != NULL)
783 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
787 len = min(ra->ra_size, buflen);
788 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
789 if (len < sizeof(rpbuf))
792 lagg_port2req(lp, &rpbuf);
793 memcpy(buf, &rpbuf, sizeof(rpbuf));
795 buf += sizeof(rpbuf);
796 len -= sizeof(rpbuf);
799 ra->ra_ports = count;
800 ra->ra_size = count * sizeof(rpbuf);
801 error = copyout(outbuf, ra->ra_port, ra->ra_size);
802 free(outbuf, M_TEMP);
805 error = priv_check(td, PRIV_NET_LAGG);
808 if (ra->ra_proto >= LAGG_PROTO_MAX) {
809 error = EPROTONOSUPPORT;
812 if (sc->sc_proto != LAGG_PROTO_NONE) {
814 error = sc->sc_detach(sc);
815 /* Reset protocol and pointers */
816 sc->sc_proto = LAGG_PROTO_NONE;
817 sc->sc_detach = NULL;
820 sc->sc_port_create = NULL;
821 sc->sc_port_destroy = NULL;
822 sc->sc_linkstate = NULL;
825 sc->sc_lladdr = NULL;
827 sc->sc_portreq = NULL;
832 for (int i = 0; i < (sizeof(lagg_protos) /
833 sizeof(lagg_protos[0])); i++) {
834 if (lagg_protos[i].ti_proto == ra->ra_proto) {
835 if (sc->sc_ifflags & IFF_DEBUG)
836 printf("%s: using proto %u\n",
838 lagg_protos[i].ti_proto);
840 sc->sc_proto = lagg_protos[i].ti_proto;
841 if (sc->sc_proto != LAGG_PROTO_NONE)
842 error = lagg_protos[i].ti_attach(sc);
847 error = EPROTONOSUPPORT;
850 if (rp->rp_portname[0] == '\0' ||
851 (tpif = ifunit(rp->rp_portname)) == NULL) {
857 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
858 lp->lp_softc != sc) {
864 lagg_port2req(lp, rp);
868 error = priv_check(td, PRIV_NET_LAGG);
871 if (rp->rp_portname[0] == '\0' ||
872 (tpif = ifunit(rp->rp_portname)) == NULL) {
877 error = lagg_port_create(sc, tpif);
880 case SIOCSLAGGDELPORT:
881 error = priv_check(td, PRIV_NET_LAGG);
884 if (rp->rp_portname[0] == '\0' ||
885 (tpif = ifunit(rp->rp_portname)) == NULL) {
891 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
892 lp->lp_softc != sc) {
898 error = lagg_port_destroy(lp, 1);
902 /* Set flags on ports too */
904 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
905 lagg_setflags(lp, 1);
909 if (!(ifp->if_flags & IFF_UP) &&
910 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
912 * If interface is marked down and it is running,
913 * then stop and disable it.
918 } else if ((ifp->if_flags & IFF_UP) &&
919 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
921 * If interface is marked up and it is stopped, then
930 error = lagg_ether_setmulti(sc);
935 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
938 error = ether_ioctl(ifp, cmd, data);
945 lagg_ether_setmulti(struct lagg_softc *sc)
947 struct lagg_port *lp;
949 LAGG_WLOCK_ASSERT(sc);
951 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
952 /* First, remove any existing filter entries. */
953 lagg_ether_cmdmulti(lp, 0);
954 /* copy all addresses from the lagg interface to the port */
955 lagg_ether_cmdmulti(lp, 1);
961 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
963 struct lagg_softc *sc = lp->lp_softc;
964 struct ifnet *ifp = lp->lp_ifp;
965 struct ifnet *scifp = sc->sc_ifp;
967 struct ifmultiaddr *ifma, *rifma = NULL;
968 struct sockaddr_dl sdl;
971 LAGG_WLOCK_ASSERT(sc);
973 bzero((char *)&sdl, sizeof(sdl));
974 sdl.sdl_len = sizeof(sdl);
975 sdl.sdl_family = AF_LINK;
976 sdl.sdl_type = IFT_ETHER;
977 sdl.sdl_alen = ETHER_ADDR_LEN;
978 sdl.sdl_index = ifp->if_index;
981 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
982 if (ifma->ifma_addr->sa_family != AF_LINK)
984 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
985 LLADDR(&sdl), ETHER_ADDR_LEN);
987 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
990 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
994 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
997 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
998 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
999 if_delmulti_ifma(mc->mc_ifma);
1006 /* Handle a ref counted flag that should be set on the lagg port as well */
1008 lagg_setflag(struct lagg_port *lp, int flag, int status,
1009 int (*func)(struct ifnet *, int))
1011 struct lagg_softc *sc = lp->lp_softc;
1012 struct ifnet *scifp = sc->sc_ifp;
1013 struct ifnet *ifp = lp->lp_ifp;
1016 LAGG_WLOCK_ASSERT(sc);
1018 status = status ? (scifp->if_flags & flag) : 0;
1019 /* Now "status" contains the flag value or 0 */
1022 * See if recorded ports status is different from what
1023 * we want it to be. If it is, flip it. We record ports
1024 * status in lp_ifflags so that we won't clear ports flag
1025 * we haven't set. In fact, we don't clear or set ports
1026 * flags directly, but get or release references to them.
1027 * That's why we can be sure that recorded flags still are
1028 * in accord with actual ports flags.
1030 if (status != (lp->lp_ifflags & flag)) {
1031 error = (*func)(ifp, status);
1034 lp->lp_ifflags &= ~flag;
1035 lp->lp_ifflags |= status;
1041 * Handle IFF_* flags that require certain changes on the lagg port
1042 * if "status" is true, update ports flags respective to the lagg
1043 * if "status" is false, forcedly clear the flags set on port.
1046 lagg_setflags(struct lagg_port *lp, int status)
1050 for (i = 0; lagg_pflags[i].flag; i++) {
1051 error = lagg_setflag(lp, lagg_pflags[i].flag,
1052 status, lagg_pflags[i].func);
1060 lagg_start(struct ifnet *ifp)
1062 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1067 for (;; error = 0) {
1068 IFQ_DEQUEUE(&ifp->if_snd, m);
1074 if (sc->sc_proto != LAGG_PROTO_NONE)
1075 error = (*sc->sc_start)(sc, m);
1082 m_freem(m); /* sc_start failed */
1091 static struct mbuf *
1092 lagg_input(struct ifnet *ifp, struct mbuf *m)
1094 struct lagg_port *lp = ifp->if_lagg;
1095 struct lagg_softc *sc = lp->lp_softc;
1096 struct ifnet *scifp = sc->sc_ifp;
1098 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1099 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1100 sc->sc_proto == LAGG_PROTO_NONE) {
1108 m = (*sc->sc_input)(sc, lp, m);
1111 scifp->if_ipackets++;
1112 scifp->if_ibytes += m->m_pkthdr.len;
1120 lagg_media_change(struct ifnet *ifp)
1122 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1124 if (sc->sc_ifflags & IFF_DEBUG)
1125 printf("%s\n", __func__);
1132 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1134 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1135 struct lagg_port *lp;
1137 imr->ifm_status = IFM_AVALID;
1138 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1141 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1142 if (LAGG_PORTACTIVE(lp))
1143 imr->ifm_status |= IFM_ACTIVE;
1149 lagg_port_state(struct ifnet *ifp, int state)
1151 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1152 struct lagg_softc *sc = NULL;
1160 if (sc->sc_linkstate != NULL)
1161 (*sc->sc_linkstate)(lp);
1166 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1168 struct lagg_port *lp_next, *rval = NULL;
1169 // int new_link = LINK_STATE_DOWN;
1171 LAGG_RLOCK_ASSERT(sc);
1173 * Search a port which reports an active link state.
1178 if (LAGG_PORTACTIVE(lp)) {
1182 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1183 LAGG_PORTACTIVE(lp_next)) {
1189 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1190 if (LAGG_PORTACTIVE(lp_next)) {
1199 * The IEEE 802.1D standard assumes that a lagg with
1200 * multiple ports is always full duplex. This is valid
1201 * for load sharing laggs and if at least two links
1202 * are active. Unfortunately, checking the latter would
1203 * be too expensive at this point.
1205 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1207 new_link = LINK_STATE_FULL_DUPLEX;
1209 new_link = rval->lp_link_state;
1217 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1219 if (m->m_pkthdr.len < (off + len)) {
1221 } else if (m->m_len < (off + len)) {
1222 m_copydata(m, off, len, buf);
1225 return (mtod(m, char *) + off);
1229 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1234 struct ether_header *eh;
1235 struct ether_vlan_header vlanbuf;
1236 const struct ether_vlan_header *vlan;
1238 const struct ip *ip;
1242 const struct ip6_hdr *ip6;
1243 struct ip6_hdr ip6buf;
1250 eh = mtod(m, struct ether_header *);
1251 etype = ntohs(eh->ether_type);
1252 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1253 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1255 /* Special handling for encapsulating VLAN frames */
1256 if (m->m_flags & M_VLANTAG) {
1257 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1258 sizeof(m->m_pkthdr.ether_vtag), p);
1259 } else if (etype == ETHERTYPE_VLAN) {
1260 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1264 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1265 etype = ntohs(vlan->evl_proto);
1266 off += sizeof(*vlan) - sizeof(*eh);
1272 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1276 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1277 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1281 case ETHERTYPE_IPV6:
1282 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1286 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1287 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1288 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1289 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1298 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1302 IFQ_HANDOFF(ifp, m, error);
1307 * Simple round robin aggregation
1311 lagg_rr_attach(struct lagg_softc *sc)
1313 struct lagg_port *lp;
1315 sc->sc_detach = lagg_rr_detach;
1316 sc->sc_start = lagg_rr_start;
1317 sc->sc_input = lagg_rr_input;
1318 sc->sc_port_create = NULL;
1319 sc->sc_port_destroy = lagg_rr_port_destroy;
1320 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1322 lp = SLIST_FIRST(&sc->sc_ports);
1323 sc->sc_psc = (caddr_t)lp;
1329 lagg_rr_detach(struct lagg_softc *sc)
1336 lagg_rr_port_destroy(struct lagg_port *lp)
1338 struct lagg_softc *sc = lp->lp_softc;
1340 if (lp == (struct lagg_port *)sc->sc_psc)
1345 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1347 struct lagg_port *lp = (struct lagg_port *)sc->sc_psc, *lp_next;
1350 if (lp == NULL && (lp = lagg_link_active(sc, NULL)) == NULL)
1354 error = lagg_enqueue(lp->lp_ifp, m);
1356 /* Get next active port */
1357 lp_next = lagg_link_active(sc, SLIST_NEXT(lp, lp_entries));
1358 sc->sc_psc = (caddr_t)lp_next;
1363 static struct mbuf *
1364 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1366 struct ifnet *ifp = sc->sc_ifp;
1368 /* Just pass in the packet to our lagg device */
1369 m->m_pkthdr.rcvif = ifp;
1379 lagg_fail_attach(struct lagg_softc *sc)
1381 sc->sc_detach = lagg_fail_detach;
1382 sc->sc_start = lagg_fail_start;
1383 sc->sc_input = lagg_fail_input;
1384 sc->sc_port_create = NULL;
1385 sc->sc_port_destroy = NULL;
1391 lagg_fail_detach(struct lagg_softc *sc)
1397 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1399 struct lagg_port *lp;
1401 /* Use the master port if active or the next available port */
1402 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL)
1406 return (lagg_enqueue(lp->lp_ifp, m));
1409 static struct mbuf *
1410 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1412 struct ifnet *ifp = sc->sc_ifp;
1413 struct lagg_port *tmp_tp;
1415 if (lp == sc->sc_primary) {
1416 m->m_pkthdr.rcvif = ifp;
1420 if (sc->sc_primary->lp_link_state == LINK_STATE_DOWN) {
1421 tmp_tp = lagg_link_active(sc, NULL);
1423 * If tmp_tp is null, we've recieved a packet when all
1424 * our links are down. Weird, but process it anyways.
1426 if ((tmp_tp == NULL || tmp_tp == lp)) {
1427 m->m_pkthdr.rcvif = ifp;
1441 lagg_lb_attach(struct lagg_softc *sc)
1443 struct lagg_port *lp;
1446 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1447 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1450 sc->sc_detach = lagg_lb_detach;
1451 sc->sc_start = lagg_lb_start;
1452 sc->sc_input = lagg_lb_input;
1453 sc->sc_port_create = lagg_lb_port_create;
1454 sc->sc_port_destroy = lagg_lb_port_destroy;
1455 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1457 lb->lb_key = arc4random();
1458 sc->sc_psc = (caddr_t)lb;
1460 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1461 lagg_lb_port_create(lp);
1467 lagg_lb_detach(struct lagg_softc *sc)
1469 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1476 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1478 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1479 struct lagg_port *lp_next;
1482 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1483 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1486 if (i >= LAGG_MAX_PORTS)
1488 if (sc->sc_ifflags & IFF_DEBUG)
1489 printf("%s: port %s at index %d\n",
1490 sc->sc_ifname, lp_next->lp_ifname, i);
1491 lb->lb_ports[i++] = lp_next;
1498 lagg_lb_port_create(struct lagg_port *lp)
1500 struct lagg_softc *sc = lp->lp_softc;
1501 return (lagg_lb_porttable(sc, NULL));
1505 lagg_lb_port_destroy(struct lagg_port *lp)
1507 struct lagg_softc *sc = lp->lp_softc;
1508 lagg_lb_porttable(sc, lp);
1512 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1514 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1515 struct lagg_port *lp = NULL;
1519 p = lagg_hashmbuf(m, lb->lb_key);
1520 if ((idx = p % sc->sc_count) >= LAGG_MAX_PORTS)
1522 lp = lb->lb_ports[idx];
1525 * Check the port's link state. This will return the next active
1526 * port if the link is down or the port is NULL.
1528 if ((lp = lagg_link_active(sc, lp)) == NULL)
1532 return (lagg_enqueue(lp->lp_ifp, m));
1535 static struct mbuf *
1536 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1538 struct ifnet *ifp = sc->sc_ifp;
1540 /* Just pass in the packet to our lagg device */
1541 m->m_pkthdr.rcvif = ifp;
1551 lagg_lacp_attach(struct lagg_softc *sc)
1553 struct lagg_port *lp;
1556 sc->sc_detach = lagg_lacp_detach;
1557 sc->sc_port_create = lacp_port_create;
1558 sc->sc_port_destroy = lacp_port_destroy;
1559 sc->sc_linkstate = lacp_linkstate;
1560 sc->sc_start = lagg_lacp_start;
1561 sc->sc_input = lagg_lacp_input;
1562 sc->sc_init = lacp_init;
1563 sc->sc_stop = lacp_stop;
1564 sc->sc_lladdr = lagg_lacp_lladdr;
1565 sc->sc_req = lacp_req;
1566 sc->sc_portreq = lacp_portreq;
1568 error = lacp_attach(sc);
1572 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1573 lacp_port_create(lp);
1579 lagg_lacp_detach(struct lagg_softc *sc)
1581 struct lagg_port *lp;
1584 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1585 lacp_port_destroy(lp);
1587 /* unlocking is safe here */
1589 error = lacp_detach(sc);
1596 lagg_lacp_lladdr(struct lagg_softc *sc)
1598 struct lagg_port *lp;
1600 /* purge all the lacp ports */
1601 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1602 lacp_port_destroy(lp);
1604 /* add them back in */
1605 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1606 lacp_port_create(lp);
1610 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1612 struct lagg_port *lp;
1614 lp = lacp_select_tx_port(sc, m);
1619 return (lagg_enqueue(lp->lp_ifp, m));
1622 static struct mbuf *
1623 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1625 struct ifnet *ifp = sc->sc_ifp;
1626 struct ether_header *eh;
1629 eh = mtod(m, struct ether_header *);
1630 etype = ntohs(eh->ether_type);
1632 /* Tap off LACP control messages */
1633 if (etype == ETHERTYPE_SLOW) {
1639 * If the port is not collecting or not in the active aggregator then
1642 if ((lp->lp_flags & LAGG_PORT_COLLECTING) == 0 ||
1643 lacp_port_isactive(lp) == 0) {
1648 m->m_pkthdr.rcvif = ifp;