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>
42 #include <sys/eventhandler.h>
44 #include <net/ethernet.h>
46 #include <net/if_clone.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
55 #if defined(INET) || defined(INET6)
56 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
65 #include <netinet/ip6.h>
68 #include <net/if_vlan_var.h>
69 #include <net/if_lagg.h>
70 #include <net/ieee8023ad_lacp.h>
72 /* Special flags we should propagate to the lagg ports. */
75 int (*func)(struct ifnet *, int);
77 {IFF_PROMISC, ifpromisc},
78 {IFF_ALLMULTI, if_allmulti},
82 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
83 static struct mtx lagg_list_mtx;
84 eventhandler_tag lagg_detach_cookie = NULL;
86 static int lagg_clone_create(struct if_clone *, int, caddr_t);
87 static void lagg_clone_destroy(struct ifnet *);
88 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
89 static void lagg_capabilities(struct lagg_softc *);
90 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
91 static void lagg_port_setlladdr(void *, int);
92 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
93 static int lagg_port_destroy(struct lagg_port *, int);
94 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
95 static void lagg_linkstate(struct lagg_softc *);
96 static void lagg_port_state(struct ifnet *, int);
97 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
98 static int lagg_port_output(struct ifnet *, struct mbuf *,
99 struct sockaddr *, struct route *);
100 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
101 #ifdef LAGG_PORT_STACKING
102 static int lagg_port_checkstacking(struct lagg_softc *);
104 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
105 static void lagg_init(void *);
106 static void lagg_stop(struct lagg_softc *);
107 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
108 static int lagg_ether_setmulti(struct lagg_softc *);
109 static int lagg_ether_cmdmulti(struct lagg_port *, int);
110 static int lagg_setflag(struct lagg_port *, int, int,
111 int (*func)(struct ifnet *, int));
112 static int lagg_setflags(struct lagg_port *, int status);
113 static void lagg_start(struct ifnet *);
114 static int lagg_media_change(struct ifnet *);
115 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
116 static struct lagg_port *lagg_link_active(struct lagg_softc *,
118 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
120 IFC_SIMPLE_DECLARE(lagg, 0);
122 /* Simple round robin */
123 static int lagg_rr_attach(struct lagg_softc *);
124 static int lagg_rr_detach(struct lagg_softc *);
125 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
126 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
129 /* Active failover */
130 static int lagg_fail_attach(struct lagg_softc *);
131 static int lagg_fail_detach(struct lagg_softc *);
132 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
133 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
137 static int lagg_lb_attach(struct lagg_softc *);
138 static int lagg_lb_detach(struct lagg_softc *);
139 static int lagg_lb_port_create(struct lagg_port *);
140 static void lagg_lb_port_destroy(struct lagg_port *);
141 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
142 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
144 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
147 static int lagg_lacp_attach(struct lagg_softc *);
148 static int lagg_lacp_detach(struct lagg_softc *);
149 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
150 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
152 static void lagg_lacp_lladdr(struct lagg_softc *);
154 /* lagg protocol table */
155 static const struct {
157 int (*ti_attach)(struct lagg_softc *);
159 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
160 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
161 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
162 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
163 { LAGG_PROTO_LACP, lagg_lacp_attach },
164 { LAGG_PROTO_NONE, NULL }
167 SYSCTL_DECL(_net_link);
168 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, "Link Aggregation");
170 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
171 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
172 &lagg_failover_rx_all, 0,
173 "Accept input from any interface in a failover lagg");
176 lagg_modevent(module_t mod, int type, void *data)
181 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
182 SLIST_INIT(&lagg_list);
183 if_clone_attach(&lagg_cloner);
184 lagg_input_p = lagg_input;
185 lagg_linkstate_p = lagg_port_state;
186 lagg_detach_cookie = EVENTHANDLER_REGISTER(
187 ifnet_departure_event, lagg_port_ifdetach, NULL,
188 EVENTHANDLER_PRI_ANY);
191 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
193 if_clone_detach(&lagg_cloner);
195 lagg_linkstate_p = NULL;
196 mtx_destroy(&lagg_list_mtx);
204 static moduledata_t lagg_mod = {
210 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
211 MODULE_VERSION(if_lagg, 1);
213 #if __FreeBSD_version >= 800000
215 * This routine is run via an vlan
219 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
221 struct lagg_softc *sc = ifp->if_softc;
222 struct lagg_port *lp;
224 if (ifp->if_softc != arg) /* Not our event */
228 if (!SLIST_EMPTY(&sc->sc_ports)) {
229 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
230 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
236 * This routine is run via an vlan
240 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
242 struct lagg_softc *sc = ifp->if_softc;
243 struct lagg_port *lp;
245 if (ifp->if_softc != arg) /* Not our event */
249 if (!SLIST_EMPTY(&sc->sc_ports)) {
250 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
251 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
258 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
260 struct lagg_softc *sc;
263 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
265 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
266 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
272 sc->sc_proto = LAGG_PROTO_NONE;
273 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
274 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
275 sc->sc_proto = lagg_protos[i].ti_proto;
276 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
277 if_free_type(ifp, IFT_ETHER);
285 SLIST_INIT(&sc->sc_ports);
286 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
288 /* Initialise pseudo media types */
289 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
291 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
292 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
294 if_initname(ifp, ifc->ifc_name, unit);
295 ifp->if_type = IFT_ETHER;
297 ifp->if_start = lagg_start;
298 ifp->if_init = lagg_init;
299 ifp->if_ioctl = lagg_ioctl;
300 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
302 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
303 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
304 IFQ_SET_READY(&ifp->if_snd);
307 * Attach as an ordinary ethernet device, childs will be attached
308 * as special device IFT_IEEE8023ADLAG.
310 ether_ifattach(ifp, eaddr);
312 #if __FreeBSD_version >= 800000
313 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
314 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
315 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
316 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
319 /* Insert into the global list of laggs */
320 mtx_lock(&lagg_list_mtx);
321 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
322 mtx_unlock(&lagg_list_mtx);
328 lagg_clone_destroy(struct ifnet *ifp)
330 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
331 struct lagg_port *lp;
336 ifp->if_flags &= ~IFF_UP;
338 #if __FreeBSD_version >= 800000
339 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
340 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
343 /* Shutdown and remove lagg ports */
344 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
345 lagg_port_destroy(lp, 1);
346 /* Unhook the aggregation protocol */
347 if (sc->sc_detach != NULL)
348 (*sc->sc_detach)(sc);
352 ifmedia_removeall(&sc->sc_media);
354 if_free_type(ifp, IFT_ETHER);
356 mtx_lock(&lagg_list_mtx);
357 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
358 mtx_unlock(&lagg_list_mtx);
360 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
361 LAGG_LOCK_DESTROY(sc);
366 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
368 struct ifnet *ifp = sc->sc_ifp;
370 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
373 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
374 /* Let the protocol know the MAC has changed */
375 if (sc->sc_lladdr != NULL)
376 (*sc->sc_lladdr)(sc);
377 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
381 lagg_capabilities(struct lagg_softc *sc)
383 struct lagg_port *lp;
384 int cap = ~0, ena = ~0;
387 LAGG_WLOCK_ASSERT(sc);
389 /* Get capabilities from the lagg ports */
390 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
391 cap &= lp->lp_ifp->if_capabilities;
392 ena &= lp->lp_ifp->if_capenable;
393 hwa &= lp->lp_ifp->if_hwassist;
395 cap = (cap == ~0 ? 0 : cap);
396 ena = (ena == ~0 ? 0 : ena);
397 hwa = (hwa == ~0 ? 0 : hwa);
399 if (sc->sc_ifp->if_capabilities != cap ||
400 sc->sc_ifp->if_capenable != ena ||
401 sc->sc_ifp->if_hwassist != hwa) {
402 sc->sc_ifp->if_capabilities = cap;
403 sc->sc_ifp->if_capenable = ena;
404 sc->sc_ifp->if_hwassist = hwa;
405 getmicrotime(&sc->sc_ifp->if_lastchange);
407 if (sc->sc_ifflags & IFF_DEBUG)
408 if_printf(sc->sc_ifp,
409 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
414 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
416 struct lagg_softc *sc = lp->lp_softc;
417 struct ifnet *ifp = lp->lp_ifp;
418 struct lagg_llq *llq;
421 LAGG_WLOCK_ASSERT(sc);
423 if (lp->lp_detaching ||
424 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
427 /* Check to make sure its not already queued to be changed */
428 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
429 if (llq->llq_ifp == ifp) {
436 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
437 if (llq == NULL) /* XXX what to do */
441 /* Update the lladdr even if pending, it may have changed */
443 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
446 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
448 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
452 * Set the interface MAC address from a taskqueue to avoid a LOR.
455 lagg_port_setlladdr(void *arg, int pending)
457 struct lagg_softc *sc = (struct lagg_softc *)arg;
458 struct lagg_llq *llq, *head;
462 /* Grab a local reference of the queue and remove it from the softc */
464 head = SLIST_FIRST(&sc->sc_llq_head);
465 SLIST_FIRST(&sc->sc_llq_head) = NULL;
469 * Traverse the queue and set the lladdr on each ifp. It is safe to do
470 * unlocked as we have the only reference to it.
472 for (llq = head; llq != NULL; llq = head) {
475 /* Set the link layer address */
476 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
478 printf("%s: setlladdr failed on %s\n", __func__,
481 head = SLIST_NEXT(llq, llq_entries);
487 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
489 struct lagg_softc *sc_ptr;
490 struct lagg_port *lp;
493 LAGG_WLOCK_ASSERT(sc);
495 /* Limit the maximal number of lagg ports */
496 if (sc->sc_count >= LAGG_MAX_PORTS)
499 /* Check if port has already been associated to a lagg */
500 if (ifp->if_lagg != NULL)
503 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
504 if (ifp->if_type != IFT_ETHER)
505 return (EPROTONOSUPPORT);
507 /* Allow the first Ethernet member to define the MTU */
508 if (SLIST_EMPTY(&sc->sc_ports))
509 sc->sc_ifp->if_mtu = ifp->if_mtu;
510 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
511 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
516 if ((lp = malloc(sizeof(struct lagg_port),
517 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
520 /* Check if port is a stacked lagg */
521 mtx_lock(&lagg_list_mtx);
522 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
523 if (ifp == sc_ptr->sc_ifp) {
524 mtx_unlock(&lagg_list_mtx);
527 /* XXX disable stacking for the moment, its untested */
528 #ifdef LAGG_PORT_STACKING
529 lp->lp_flags |= LAGG_PORT_STACK;
530 if (lagg_port_checkstacking(sc_ptr) >=
532 mtx_unlock(&lagg_list_mtx);
539 mtx_unlock(&lagg_list_mtx);
541 /* Change the interface type */
542 lp->lp_iftype = ifp->if_type;
543 ifp->if_type = IFT_IEEE8023ADLAG;
545 lp->lp_ioctl = ifp->if_ioctl;
546 ifp->if_ioctl = lagg_port_ioctl;
547 lp->lp_output = ifp->if_output;
548 ifp->if_output = lagg_port_output;
553 /* Save port link layer address */
554 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
556 if (SLIST_EMPTY(&sc->sc_ports)) {
558 lagg_lladdr(sc, IF_LLADDR(ifp));
560 /* Update link layer address for this port */
561 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
564 /* Insert into the list of ports */
565 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
568 /* Update lagg capabilities */
569 lagg_capabilities(sc);
572 /* Add multicast addresses and interface flags to this port */
573 lagg_ether_cmdmulti(lp, 1);
574 lagg_setflags(lp, 1);
576 if (sc->sc_port_create != NULL)
577 error = (*sc->sc_port_create)(lp);
579 /* remove the port again, without calling sc_port_destroy */
580 lagg_port_destroy(lp, 0);
587 #ifdef LAGG_PORT_STACKING
589 lagg_port_checkstacking(struct lagg_softc *sc)
591 struct lagg_softc *sc_ptr;
592 struct lagg_port *lp;
595 LAGG_WLOCK_ASSERT(sc);
597 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
598 if (lp->lp_flags & LAGG_PORT_STACK) {
599 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
600 m = MAX(m, lagg_port_checkstacking(sc_ptr));
609 lagg_port_destroy(struct lagg_port *lp, int runpd)
611 struct lagg_softc *sc = lp->lp_softc;
612 struct lagg_port *lp_ptr;
613 struct lagg_llq *llq;
614 struct ifnet *ifp = lp->lp_ifp;
616 LAGG_WLOCK_ASSERT(sc);
618 if (runpd && sc->sc_port_destroy != NULL)
619 (*sc->sc_port_destroy)(lp);
622 * Remove multicast addresses and interface flags from this port and
623 * reset the MAC address, skip if the interface is being detached.
625 if (!lp->lp_detaching) {
626 lagg_ether_cmdmulti(lp, 0);
627 lagg_setflags(lp, 0);
628 lagg_port_lladdr(lp, lp->lp_lladdr);
631 /* Restore interface */
632 ifp->if_type = lp->lp_iftype;
633 ifp->if_ioctl = lp->lp_ioctl;
634 ifp->if_output = lp->lp_output;
637 /* Finally, remove the port from the lagg */
638 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
641 /* Update the primary interface */
642 if (lp == sc->sc_primary) {
643 uint8_t lladdr[ETHER_ADDR_LEN];
645 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
646 bzero(&lladdr, ETHER_ADDR_LEN);
648 bcopy(lp_ptr->lp_lladdr,
649 lladdr, ETHER_ADDR_LEN);
651 lagg_lladdr(sc, lladdr);
652 sc->sc_primary = lp_ptr;
654 /* Update link layer address for each port */
655 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
656 lagg_port_lladdr(lp_ptr, lladdr);
659 /* Remove any pending lladdr changes from the queue */
660 if (lp->lp_detaching) {
661 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
662 if (llq->llq_ifp == ifp) {
663 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
666 break; /* Only appears once */
672 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
676 /* Update lagg capabilities */
677 lagg_capabilities(sc);
684 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
686 struct lagg_reqport *rp = (struct lagg_reqport *)data;
687 struct lagg_softc *sc;
688 struct lagg_port *lp = NULL;
691 /* Should be checked by the caller */
692 if (ifp->if_type != IFT_IEEE8023ADLAG ||
693 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
698 if (rp->rp_portname[0] == '\0' ||
699 ifunit(rp->rp_portname) != ifp) {
705 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
711 lagg_port2req(lp, rp);
716 if (lp->lp_ioctl == NULL) {
720 error = (*lp->lp_ioctl)(ifp, cmd, data);
724 /* Update lagg interface capabilities */
726 lagg_capabilities(sc);
731 /* Do not allow the MTU to be changed once joined */
742 if (lp->lp_ioctl != NULL)
743 return ((*lp->lp_ioctl)(ifp, cmd, data));
749 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
750 struct sockaddr *dst, struct route *ro)
752 struct lagg_port *lp = ifp->if_lagg;
753 struct ether_header *eh;
756 switch (dst->sa_family) {
757 case pseudo_AF_HDRCMPLT:
759 eh = (struct ether_header *)dst->sa_data;
760 type = eh->ether_type;
765 * Only allow ethernet types required to initiate or maintain the link,
766 * aggregated frames take a different path.
768 switch (ntohs(type)) {
769 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
770 return ((*lp->lp_output)(ifp, m, dst, ro));
773 /* drop any other frames */
779 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
781 struct lagg_port *lp;
782 struct lagg_softc *sc;
784 if ((lp = ifp->if_lagg) == NULL)
790 lp->lp_detaching = 1;
791 lagg_port_destroy(lp, 1);
796 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
798 struct lagg_softc *sc = lp->lp_softc;
800 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
801 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
802 rp->rp_prio = lp->lp_prio;
803 rp->rp_flags = lp->lp_flags;
804 if (sc->sc_portreq != NULL)
805 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
807 /* Add protocol specific flags */
808 switch (sc->sc_proto) {
809 case LAGG_PROTO_FAILOVER:
810 if (lp == sc->sc_primary)
811 rp->rp_flags |= LAGG_PORT_MASTER;
812 if (lp == lagg_link_active(sc, sc->sc_primary))
813 rp->rp_flags |= LAGG_PORT_ACTIVE;
816 case LAGG_PROTO_ROUNDROBIN:
817 case LAGG_PROTO_LOADBALANCE:
818 case LAGG_PROTO_ETHERCHANNEL:
819 if (LAGG_PORTACTIVE(lp))
820 rp->rp_flags |= LAGG_PORT_ACTIVE;
823 case LAGG_PROTO_LACP:
824 /* LACP has a different definition of active */
825 if (lacp_isactive(lp))
826 rp->rp_flags |= LAGG_PORT_ACTIVE;
827 if (lacp_iscollecting(lp))
828 rp->rp_flags |= LAGG_PORT_COLLECTING;
829 if (lacp_isdistributing(lp))
830 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
839 struct lagg_softc *sc = (struct lagg_softc *)xsc;
840 struct lagg_port *lp;
841 struct ifnet *ifp = sc->sc_ifp;
843 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
848 ifp->if_drv_flags |= IFF_DRV_RUNNING;
849 /* Update the port lladdrs */
850 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
851 lagg_port_lladdr(lp, IF_LLADDR(ifp));
853 if (sc->sc_init != NULL)
860 lagg_stop(struct lagg_softc *sc)
862 struct ifnet *ifp = sc->sc_ifp;
864 LAGG_WLOCK_ASSERT(sc);
866 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
869 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
871 if (sc->sc_stop != NULL)
876 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
878 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
879 struct lagg_reqall *ra = (struct lagg_reqall *)data;
880 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
881 struct ifreq *ifr = (struct ifreq *)data;
882 struct lagg_port *lp;
884 struct thread *td = curthread;
886 int count, buflen, len, error = 0;
888 bzero(&rpbuf, sizeof(rpbuf));
894 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
896 buflen = count * sizeof(struct lagg_reqport);
899 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
902 ra->ra_proto = sc->sc_proto;
903 if (sc->sc_req != NULL)
904 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
908 len = min(ra->ra_size, buflen);
909 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
910 if (len < sizeof(rpbuf))
913 lagg_port2req(lp, &rpbuf);
914 memcpy(buf, &rpbuf, sizeof(rpbuf));
916 buf += sizeof(rpbuf);
917 len -= sizeof(rpbuf);
920 ra->ra_ports = count;
921 ra->ra_size = count * sizeof(rpbuf);
922 error = copyout(outbuf, ra->ra_port, ra->ra_size);
923 free(outbuf, M_TEMP);
926 error = priv_check(td, PRIV_NET_LAGG);
929 if (ra->ra_proto >= LAGG_PROTO_MAX) {
930 error = EPROTONOSUPPORT;
933 if (sc->sc_proto != LAGG_PROTO_NONE) {
935 error = sc->sc_detach(sc);
936 /* Reset protocol and pointers */
937 sc->sc_proto = LAGG_PROTO_NONE;
938 sc->sc_detach = NULL;
941 sc->sc_port_create = NULL;
942 sc->sc_port_destroy = NULL;
943 sc->sc_linkstate = NULL;
946 sc->sc_lladdr = NULL;
948 sc->sc_portreq = NULL;
953 for (int i = 0; i < (sizeof(lagg_protos) /
954 sizeof(lagg_protos[0])); i++) {
955 if (lagg_protos[i].ti_proto == ra->ra_proto) {
956 if (sc->sc_ifflags & IFF_DEBUG)
957 printf("%s: using proto %u\n",
959 lagg_protos[i].ti_proto);
961 sc->sc_proto = lagg_protos[i].ti_proto;
962 if (sc->sc_proto != LAGG_PROTO_NONE)
963 error = lagg_protos[i].ti_attach(sc);
968 error = EPROTONOSUPPORT;
971 if (rp->rp_portname[0] == '\0' ||
972 (tpif = ifunit(rp->rp_portname)) == NULL) {
978 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
979 lp->lp_softc != sc) {
985 lagg_port2req(lp, rp);
989 error = priv_check(td, PRIV_NET_LAGG);
992 if (rp->rp_portname[0] == '\0' ||
993 (tpif = ifunit(rp->rp_portname)) == NULL) {
998 error = lagg_port_create(sc, tpif);
1001 case SIOCSLAGGDELPORT:
1002 error = priv_check(td, PRIV_NET_LAGG);
1005 if (rp->rp_portname[0] == '\0' ||
1006 (tpif = ifunit(rp->rp_portname)) == NULL) {
1012 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1013 lp->lp_softc != sc) {
1019 error = lagg_port_destroy(lp, 1);
1023 /* Set flags on ports too */
1025 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1026 lagg_setflags(lp, 1);
1030 if (!(ifp->if_flags & IFF_UP) &&
1031 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1033 * If interface is marked down and it is running,
1034 * then stop and disable it.
1039 } else if ((ifp->if_flags & IFF_UP) &&
1040 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1042 * If interface is marked up and it is stopped, then
1045 (*ifp->if_init)(sc);
1051 error = lagg_ether_setmulti(sc);
1056 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1061 /* Do not allow the MTU or caps to be directly changed */
1066 error = ether_ioctl(ifp, cmd, data);
1073 lagg_ether_setmulti(struct lagg_softc *sc)
1075 struct lagg_port *lp;
1077 LAGG_WLOCK_ASSERT(sc);
1079 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1080 /* First, remove any existing filter entries. */
1081 lagg_ether_cmdmulti(lp, 0);
1082 /* copy all addresses from the lagg interface to the port */
1083 lagg_ether_cmdmulti(lp, 1);
1089 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1091 struct lagg_softc *sc = lp->lp_softc;
1092 struct ifnet *ifp = lp->lp_ifp;
1093 struct ifnet *scifp = sc->sc_ifp;
1095 struct ifmultiaddr *ifma, *rifma = NULL;
1096 struct sockaddr_dl sdl;
1099 LAGG_WLOCK_ASSERT(sc);
1101 bzero((char *)&sdl, sizeof(sdl));
1102 sdl.sdl_len = sizeof(sdl);
1103 sdl.sdl_family = AF_LINK;
1104 sdl.sdl_type = IFT_ETHER;
1105 sdl.sdl_alen = ETHER_ADDR_LEN;
1106 sdl.sdl_index = ifp->if_index;
1109 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1110 if (ifma->ifma_addr->sa_family != AF_LINK)
1112 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1113 LLADDR(&sdl), ETHER_ADDR_LEN);
1115 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1118 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1121 mc->mc_ifma = rifma;
1122 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1125 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1126 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1127 if_delmulti_ifma(mc->mc_ifma);
1134 /* Handle a ref counted flag that should be set on the lagg port as well */
1136 lagg_setflag(struct lagg_port *lp, int flag, int status,
1137 int (*func)(struct ifnet *, int))
1139 struct lagg_softc *sc = lp->lp_softc;
1140 struct ifnet *scifp = sc->sc_ifp;
1141 struct ifnet *ifp = lp->lp_ifp;
1144 LAGG_WLOCK_ASSERT(sc);
1146 status = status ? (scifp->if_flags & flag) : 0;
1147 /* Now "status" contains the flag value or 0 */
1150 * See if recorded ports status is different from what
1151 * we want it to be. If it is, flip it. We record ports
1152 * status in lp_ifflags so that we won't clear ports flag
1153 * we haven't set. In fact, we don't clear or set ports
1154 * flags directly, but get or release references to them.
1155 * That's why we can be sure that recorded flags still are
1156 * in accord with actual ports flags.
1158 if (status != (lp->lp_ifflags & flag)) {
1159 error = (*func)(ifp, status);
1162 lp->lp_ifflags &= ~flag;
1163 lp->lp_ifflags |= status;
1169 * Handle IFF_* flags that require certain changes on the lagg port
1170 * if "status" is true, update ports flags respective to the lagg
1171 * if "status" is false, forcedly clear the flags set on port.
1174 lagg_setflags(struct lagg_port *lp, int status)
1178 for (i = 0; lagg_pflags[i].flag; i++) {
1179 error = lagg_setflag(lp, lagg_pflags[i].flag,
1180 status, lagg_pflags[i].func);
1188 lagg_start(struct ifnet *ifp)
1190 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1195 /* We need a Tx algorithm and at least one port */
1196 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1197 IF_DRAIN(&ifp->if_snd);
1202 for (;; error = 0) {
1203 IFQ_DEQUEUE(&ifp->if_snd, m);
1207 ETHER_BPF_MTAP(ifp, m);
1209 error = (*sc->sc_start)(sc, m);
1218 static struct mbuf *
1219 lagg_input(struct ifnet *ifp, struct mbuf *m)
1221 struct lagg_port *lp = ifp->if_lagg;
1222 struct lagg_softc *sc = lp->lp_softc;
1223 struct ifnet *scifp = sc->sc_ifp;
1226 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1227 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1228 sc->sc_proto == LAGG_PROTO_NONE) {
1234 ETHER_BPF_MTAP(scifp, m);
1236 m = (*sc->sc_input)(sc, lp, m);
1239 scifp->if_ipackets++;
1240 scifp->if_ibytes += m->m_pkthdr.len;
1242 if (scifp->if_flags & IFF_MONITOR) {
1253 lagg_media_change(struct ifnet *ifp)
1255 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1257 if (sc->sc_ifflags & IFF_DEBUG)
1258 printf("%s\n", __func__);
1265 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1267 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1268 struct lagg_port *lp;
1270 imr->ifm_status = IFM_AVALID;
1271 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1274 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1275 if (LAGG_PORTACTIVE(lp))
1276 imr->ifm_status |= IFM_ACTIVE;
1282 lagg_linkstate(struct lagg_softc *sc)
1284 struct lagg_port *lp;
1285 int new_link = LINK_STATE_DOWN;
1288 /* Our link is considered up if at least one of our ports is active */
1289 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1290 if (lp->lp_link_state == LINK_STATE_UP) {
1291 new_link = LINK_STATE_UP;
1295 if_link_state_change(sc->sc_ifp, new_link);
1297 /* Update if_baudrate to reflect the max possible speed */
1298 switch (sc->sc_proto) {
1299 case LAGG_PROTO_FAILOVER:
1300 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1301 sc->sc_primary->lp_ifp->if_baudrate : 0;
1303 case LAGG_PROTO_ROUNDROBIN:
1304 case LAGG_PROTO_LOADBALANCE:
1305 case LAGG_PROTO_ETHERCHANNEL:
1307 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1308 speed += lp->lp_ifp->if_baudrate;
1309 sc->sc_ifp->if_baudrate = speed;
1311 case LAGG_PROTO_LACP:
1312 /* LACP updates if_baudrate itself */
1318 lagg_port_state(struct ifnet *ifp, int state)
1320 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1321 struct lagg_softc *sc = NULL;
1330 if (sc->sc_linkstate != NULL)
1331 (*sc->sc_linkstate)(lp);
1336 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1338 struct lagg_port *lp_next, *rval = NULL;
1339 // int new_link = LINK_STATE_DOWN;
1341 LAGG_RLOCK_ASSERT(sc);
1343 * Search a port which reports an active link state.
1348 if (LAGG_PORTACTIVE(lp)) {
1352 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1353 LAGG_PORTACTIVE(lp_next)) {
1359 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1360 if (LAGG_PORTACTIVE(lp_next)) {
1369 * The IEEE 802.1D standard assumes that a lagg with
1370 * multiple ports is always full duplex. This is valid
1371 * for load sharing laggs and if at least two links
1372 * are active. Unfortunately, checking the latter would
1373 * be too expensive at this point.
1375 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1377 new_link = LINK_STATE_FULL_DUPLEX;
1379 new_link = rval->lp_link_state;
1387 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1389 if (m->m_pkthdr.len < (off + len)) {
1391 } else if (m->m_len < (off + len)) {
1392 m_copydata(m, off, len, buf);
1395 return (mtod(m, char *) + off);
1399 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1404 struct ether_header *eh;
1405 struct ether_vlan_header vlanbuf;
1406 const struct ether_vlan_header *vlan;
1408 const struct ip *ip;
1412 const struct ip6_hdr *ip6;
1413 struct ip6_hdr ip6buf;
1420 eh = mtod(m, struct ether_header *);
1421 etype = ntohs(eh->ether_type);
1422 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1423 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1425 /* Special handling for encapsulating VLAN frames */
1426 if (m->m_flags & M_VLANTAG) {
1427 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1428 sizeof(m->m_pkthdr.ether_vtag), p);
1429 } else if (etype == ETHERTYPE_VLAN) {
1430 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1434 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1435 etype = ntohs(vlan->evl_proto);
1436 off += sizeof(*vlan) - sizeof(*eh);
1442 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1446 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1447 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1451 case ETHERTYPE_IPV6:
1452 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1456 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1457 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1458 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1459 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1468 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1471 return (ifp->if_transmit)(ifp, m);
1475 * Simple round robin aggregation
1479 lagg_rr_attach(struct lagg_softc *sc)
1481 sc->sc_detach = lagg_rr_detach;
1482 sc->sc_start = lagg_rr_start;
1483 sc->sc_input = lagg_rr_input;
1484 sc->sc_port_create = NULL;
1485 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1492 lagg_rr_detach(struct lagg_softc *sc)
1498 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1500 struct lagg_port *lp;
1503 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1505 lp = SLIST_FIRST(&sc->sc_ports);
1507 lp = SLIST_NEXT(lp, lp_entries);
1510 * Check the port's link state. This will return the next active
1511 * port if the link is down or the port is NULL.
1513 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1519 return (lagg_enqueue(lp->lp_ifp, m));
1522 static struct mbuf *
1523 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1525 struct ifnet *ifp = sc->sc_ifp;
1527 /* Just pass in the packet to our lagg device */
1528 m->m_pkthdr.rcvif = ifp;
1538 lagg_fail_attach(struct lagg_softc *sc)
1540 sc->sc_detach = lagg_fail_detach;
1541 sc->sc_start = lagg_fail_start;
1542 sc->sc_input = lagg_fail_input;
1543 sc->sc_port_create = NULL;
1544 sc->sc_port_destroy = NULL;
1550 lagg_fail_detach(struct lagg_softc *sc)
1556 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1558 struct lagg_port *lp;
1560 /* Use the master port if active or the next available port */
1561 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1567 return (lagg_enqueue(lp->lp_ifp, m));
1570 static struct mbuf *
1571 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1573 struct ifnet *ifp = sc->sc_ifp;
1574 struct lagg_port *tmp_tp;
1576 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1577 m->m_pkthdr.rcvif = ifp;
1581 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1582 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1584 * If tmp_tp is null, we've recieved a packet when all
1585 * our links are down. Weird, but process it anyways.
1587 if ((tmp_tp == NULL || tmp_tp == lp)) {
1588 m->m_pkthdr.rcvif = ifp;
1602 lagg_lb_attach(struct lagg_softc *sc)
1604 struct lagg_port *lp;
1607 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1608 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1611 sc->sc_detach = lagg_lb_detach;
1612 sc->sc_start = lagg_lb_start;
1613 sc->sc_input = lagg_lb_input;
1614 sc->sc_port_create = lagg_lb_port_create;
1615 sc->sc_port_destroy = lagg_lb_port_destroy;
1616 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1618 lb->lb_key = arc4random();
1619 sc->sc_psc = (caddr_t)lb;
1621 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1622 lagg_lb_port_create(lp);
1628 lagg_lb_detach(struct lagg_softc *sc)
1630 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1637 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1639 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1640 struct lagg_port *lp_next;
1643 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1644 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1647 if (i >= LAGG_MAX_PORTS)
1649 if (sc->sc_ifflags & IFF_DEBUG)
1650 printf("%s: port %s at index %d\n",
1651 sc->sc_ifname, lp_next->lp_ifname, i);
1652 lb->lb_ports[i++] = lp_next;
1659 lagg_lb_port_create(struct lagg_port *lp)
1661 struct lagg_softc *sc = lp->lp_softc;
1662 return (lagg_lb_porttable(sc, NULL));
1666 lagg_lb_port_destroy(struct lagg_port *lp)
1668 struct lagg_softc *sc = lp->lp_softc;
1669 lagg_lb_porttable(sc, lp);
1673 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1675 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1676 struct lagg_port *lp = NULL;
1679 if (m->m_flags & M_FLOWID)
1680 p = m->m_pkthdr.flowid;
1682 p = lagg_hashmbuf(m, lb->lb_key);
1684 lp = lb->lb_ports[p];
1687 * Check the port's link state. This will return the next active
1688 * port if the link is down or the port is NULL.
1690 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1696 return (lagg_enqueue(lp->lp_ifp, m));
1699 static struct mbuf *
1700 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1702 struct ifnet *ifp = sc->sc_ifp;
1704 /* Just pass in the packet to our lagg device */
1705 m->m_pkthdr.rcvif = ifp;
1715 lagg_lacp_attach(struct lagg_softc *sc)
1717 struct lagg_port *lp;
1720 sc->sc_detach = lagg_lacp_detach;
1721 sc->sc_port_create = lacp_port_create;
1722 sc->sc_port_destroy = lacp_port_destroy;
1723 sc->sc_linkstate = lacp_linkstate;
1724 sc->sc_start = lagg_lacp_start;
1725 sc->sc_input = lagg_lacp_input;
1726 sc->sc_init = lacp_init;
1727 sc->sc_stop = lacp_stop;
1728 sc->sc_lladdr = lagg_lacp_lladdr;
1729 sc->sc_req = lacp_req;
1730 sc->sc_portreq = lacp_portreq;
1732 error = lacp_attach(sc);
1736 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1737 lacp_port_create(lp);
1743 lagg_lacp_detach(struct lagg_softc *sc)
1745 struct lagg_port *lp;
1748 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1749 lacp_port_destroy(lp);
1751 /* unlocking is safe here */
1753 error = lacp_detach(sc);
1760 lagg_lacp_lladdr(struct lagg_softc *sc)
1762 struct lagg_port *lp;
1764 /* purge all the lacp ports */
1765 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1766 lacp_port_destroy(lp);
1768 /* add them back in */
1769 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1770 lacp_port_create(lp);
1774 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1776 struct lagg_port *lp;
1778 lp = lacp_select_tx_port(sc, m);
1785 return (lagg_enqueue(lp->lp_ifp, m));
1788 static struct mbuf *
1789 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1791 struct ifnet *ifp = sc->sc_ifp;
1792 struct ether_header *eh;
1795 eh = mtod(m, struct ether_header *);
1796 etype = ntohs(eh->ether_type);
1798 /* Tap off LACP control messages */
1799 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1800 m = lacp_input(lp, m);
1806 * If the port is not collecting or not in the active aggregator then
1809 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1814 m->m_pkthdr.rcvif = ifp;