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");
174 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
175 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
176 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
178 "Default setting for using flow id for load sharing");
181 lagg_modevent(module_t mod, int type, void *data)
186 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
187 SLIST_INIT(&lagg_list);
188 if_clone_attach(&lagg_cloner);
189 lagg_input_p = lagg_input;
190 lagg_linkstate_p = lagg_port_state;
191 lagg_detach_cookie = EVENTHANDLER_REGISTER(
192 ifnet_departure_event, lagg_port_ifdetach, NULL,
193 EVENTHANDLER_PRI_ANY);
196 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
198 if_clone_detach(&lagg_cloner);
200 lagg_linkstate_p = NULL;
201 mtx_destroy(&lagg_list_mtx);
209 static moduledata_t lagg_mod = {
215 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
216 MODULE_VERSION(if_lagg, 1);
218 #if __FreeBSD_version >= 800000
220 * This routine is run via an vlan
224 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
226 struct lagg_softc *sc = ifp->if_softc;
227 struct lagg_port *lp;
229 if (ifp->if_softc != arg) /* Not our event */
233 if (!SLIST_EMPTY(&sc->sc_ports)) {
234 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
235 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
241 * This routine is run via an vlan
245 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
247 struct lagg_softc *sc = ifp->if_softc;
248 struct lagg_port *lp;
250 if (ifp->if_softc != arg) /* Not our event */
254 if (!SLIST_EMPTY(&sc->sc_ports)) {
255 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
256 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
263 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
265 struct lagg_softc *sc;
268 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
269 struct sysctl_oid *oid;
270 char num[14]; /* sufficient for 32 bits */
272 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
273 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
279 sysctl_ctx_init(&sc->ctx);
280 snprintf(num, sizeof(num), "%u", unit);
281 sc->use_flowid = def_use_flowid;
282 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
283 OID_AUTO, num, CTLFLAG_RD, NULL, "");
284 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
285 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
286 "Use flow id for load sharing");
288 sc->sc_proto = LAGG_PROTO_NONE;
289 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
290 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
291 sc->sc_proto = lagg_protos[i].ti_proto;
292 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
293 if_free_type(ifp, IFT_ETHER);
301 SLIST_INIT(&sc->sc_ports);
302 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
304 /* Initialise pseudo media types */
305 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
307 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
308 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
310 if_initname(ifp, ifc->ifc_name, unit);
311 ifp->if_type = IFT_ETHER;
313 ifp->if_start = lagg_start;
314 ifp->if_init = lagg_init;
315 ifp->if_ioctl = lagg_ioctl;
316 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
318 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
319 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
320 IFQ_SET_READY(&ifp->if_snd);
323 * Attach as an ordinary ethernet device, childs will be attached
324 * as special device IFT_IEEE8023ADLAG.
326 ether_ifattach(ifp, eaddr);
328 #if __FreeBSD_version >= 800000
329 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
330 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
331 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
332 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
335 /* Insert into the global list of laggs */
336 mtx_lock(&lagg_list_mtx);
337 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
338 mtx_unlock(&lagg_list_mtx);
344 lagg_clone_destroy(struct ifnet *ifp)
346 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
347 struct lagg_port *lp;
352 ifp->if_flags &= ~IFF_UP;
354 #if __FreeBSD_version >= 800000
355 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
356 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
359 /* Shutdown and remove lagg ports */
360 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
361 lagg_port_destroy(lp, 1);
362 /* Unhook the aggregation protocol */
363 if (sc->sc_detach != NULL)
364 (*sc->sc_detach)(sc);
368 sysctl_ctx_free(&sc->ctx);
369 ifmedia_removeall(&sc->sc_media);
371 if_free_type(ifp, IFT_ETHER);
373 mtx_lock(&lagg_list_mtx);
374 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
375 mtx_unlock(&lagg_list_mtx);
377 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
378 LAGG_LOCK_DESTROY(sc);
383 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
385 struct ifnet *ifp = sc->sc_ifp;
387 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
390 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
391 /* Let the protocol know the MAC has changed */
392 if (sc->sc_lladdr != NULL)
393 (*sc->sc_lladdr)(sc);
394 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
398 lagg_capabilities(struct lagg_softc *sc)
400 struct lagg_port *lp;
401 int cap = ~0, ena = ~0;
404 LAGG_WLOCK_ASSERT(sc);
406 /* Get capabilities from the lagg ports */
407 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
408 cap &= lp->lp_ifp->if_capabilities;
409 ena &= lp->lp_ifp->if_capenable;
410 hwa &= lp->lp_ifp->if_hwassist;
412 cap = (cap == ~0 ? 0 : cap);
413 ena = (ena == ~0 ? 0 : ena);
414 hwa = (hwa == ~0 ? 0 : hwa);
416 if (sc->sc_ifp->if_capabilities != cap ||
417 sc->sc_ifp->if_capenable != ena ||
418 sc->sc_ifp->if_hwassist != hwa) {
419 sc->sc_ifp->if_capabilities = cap;
420 sc->sc_ifp->if_capenable = ena;
421 sc->sc_ifp->if_hwassist = hwa;
422 getmicrotime(&sc->sc_ifp->if_lastchange);
424 if (sc->sc_ifflags & IFF_DEBUG)
425 if_printf(sc->sc_ifp,
426 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
431 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
433 struct lagg_softc *sc = lp->lp_softc;
434 struct ifnet *ifp = lp->lp_ifp;
435 struct lagg_llq *llq;
438 LAGG_WLOCK_ASSERT(sc);
440 if (lp->lp_detaching ||
441 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
444 /* Check to make sure its not already queued to be changed */
445 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
446 if (llq->llq_ifp == ifp) {
453 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
454 if (llq == NULL) /* XXX what to do */
458 /* Update the lladdr even if pending, it may have changed */
460 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
463 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
465 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
469 * Set the interface MAC address from a taskqueue to avoid a LOR.
472 lagg_port_setlladdr(void *arg, int pending)
474 struct lagg_softc *sc = (struct lagg_softc *)arg;
475 struct lagg_llq *llq, *head;
479 /* Grab a local reference of the queue and remove it from the softc */
481 head = SLIST_FIRST(&sc->sc_llq_head);
482 SLIST_FIRST(&sc->sc_llq_head) = NULL;
486 * Traverse the queue and set the lladdr on each ifp. It is safe to do
487 * unlocked as we have the only reference to it.
489 for (llq = head; llq != NULL; llq = head) {
492 /* Set the link layer address */
493 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
495 printf("%s: setlladdr failed on %s\n", __func__,
498 head = SLIST_NEXT(llq, llq_entries);
504 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
506 struct lagg_softc *sc_ptr;
507 struct lagg_port *lp;
510 LAGG_WLOCK_ASSERT(sc);
512 /* Limit the maximal number of lagg ports */
513 if (sc->sc_count >= LAGG_MAX_PORTS)
516 /* Check if port has already been associated to a lagg */
517 if (ifp->if_lagg != NULL)
520 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
521 if (ifp->if_type != IFT_ETHER)
522 return (EPROTONOSUPPORT);
524 /* Allow the first Ethernet member to define the MTU */
525 if (SLIST_EMPTY(&sc->sc_ports))
526 sc->sc_ifp->if_mtu = ifp->if_mtu;
527 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
528 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
533 if ((lp = malloc(sizeof(struct lagg_port),
534 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
537 /* Check if port is a stacked lagg */
538 mtx_lock(&lagg_list_mtx);
539 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
540 if (ifp == sc_ptr->sc_ifp) {
541 mtx_unlock(&lagg_list_mtx);
544 /* XXX disable stacking for the moment, its untested */
545 #ifdef LAGG_PORT_STACKING
546 lp->lp_flags |= LAGG_PORT_STACK;
547 if (lagg_port_checkstacking(sc_ptr) >=
549 mtx_unlock(&lagg_list_mtx);
556 mtx_unlock(&lagg_list_mtx);
558 /* Change the interface type */
559 lp->lp_iftype = ifp->if_type;
560 ifp->if_type = IFT_IEEE8023ADLAG;
562 lp->lp_ioctl = ifp->if_ioctl;
563 ifp->if_ioctl = lagg_port_ioctl;
564 lp->lp_output = ifp->if_output;
565 ifp->if_output = lagg_port_output;
570 /* Save port link layer address */
571 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
573 if (SLIST_EMPTY(&sc->sc_ports)) {
575 lagg_lladdr(sc, IF_LLADDR(ifp));
577 /* Update link layer address for this port */
578 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
581 /* Insert into the list of ports */
582 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
585 /* Update lagg capabilities */
586 lagg_capabilities(sc);
589 /* Add multicast addresses and interface flags to this port */
590 lagg_ether_cmdmulti(lp, 1);
591 lagg_setflags(lp, 1);
593 if (sc->sc_port_create != NULL)
594 error = (*sc->sc_port_create)(lp);
596 /* remove the port again, without calling sc_port_destroy */
597 lagg_port_destroy(lp, 0);
604 #ifdef LAGG_PORT_STACKING
606 lagg_port_checkstacking(struct lagg_softc *sc)
608 struct lagg_softc *sc_ptr;
609 struct lagg_port *lp;
612 LAGG_WLOCK_ASSERT(sc);
614 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
615 if (lp->lp_flags & LAGG_PORT_STACK) {
616 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
617 m = MAX(m, lagg_port_checkstacking(sc_ptr));
626 lagg_port_destroy(struct lagg_port *lp, int runpd)
628 struct lagg_softc *sc = lp->lp_softc;
629 struct lagg_port *lp_ptr;
630 struct lagg_llq *llq;
631 struct ifnet *ifp = lp->lp_ifp;
633 LAGG_WLOCK_ASSERT(sc);
635 if (runpd && sc->sc_port_destroy != NULL)
636 (*sc->sc_port_destroy)(lp);
639 * Remove multicast addresses and interface flags from this port and
640 * reset the MAC address, skip if the interface is being detached.
642 if (!lp->lp_detaching) {
643 lagg_ether_cmdmulti(lp, 0);
644 lagg_setflags(lp, 0);
645 lagg_port_lladdr(lp, lp->lp_lladdr);
648 /* Restore interface */
649 ifp->if_type = lp->lp_iftype;
650 ifp->if_ioctl = lp->lp_ioctl;
651 ifp->if_output = lp->lp_output;
654 /* Finally, remove the port from the lagg */
655 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
658 /* Update the primary interface */
659 if (lp == sc->sc_primary) {
660 uint8_t lladdr[ETHER_ADDR_LEN];
662 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
663 bzero(&lladdr, ETHER_ADDR_LEN);
665 bcopy(lp_ptr->lp_lladdr,
666 lladdr, ETHER_ADDR_LEN);
668 lagg_lladdr(sc, lladdr);
669 sc->sc_primary = lp_ptr;
671 /* Update link layer address for each port */
672 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
673 lagg_port_lladdr(lp_ptr, lladdr);
676 /* Remove any pending lladdr changes from the queue */
677 if (lp->lp_detaching) {
678 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
679 if (llq->llq_ifp == ifp) {
680 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
683 break; /* Only appears once */
689 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
693 /* Update lagg capabilities */
694 lagg_capabilities(sc);
701 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
703 struct lagg_reqport *rp = (struct lagg_reqport *)data;
704 struct lagg_softc *sc;
705 struct lagg_port *lp = NULL;
708 /* Should be checked by the caller */
709 if (ifp->if_type != IFT_IEEE8023ADLAG ||
710 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
715 if (rp->rp_portname[0] == '\0' ||
716 ifunit(rp->rp_portname) != ifp) {
722 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
728 lagg_port2req(lp, rp);
733 if (lp->lp_ioctl == NULL) {
737 error = (*lp->lp_ioctl)(ifp, cmd, data);
741 /* Update lagg interface capabilities */
743 lagg_capabilities(sc);
748 /* Do not allow the MTU to be changed once joined */
759 if (lp->lp_ioctl != NULL)
760 return ((*lp->lp_ioctl)(ifp, cmd, data));
766 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
767 struct sockaddr *dst, struct route *ro)
769 struct lagg_port *lp = ifp->if_lagg;
770 struct ether_header *eh;
773 switch (dst->sa_family) {
774 case pseudo_AF_HDRCMPLT:
776 eh = (struct ether_header *)dst->sa_data;
777 type = eh->ether_type;
782 * Only allow ethernet types required to initiate or maintain the link,
783 * aggregated frames take a different path.
785 switch (ntohs(type)) {
786 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
787 return ((*lp->lp_output)(ifp, m, dst, ro));
790 /* drop any other frames */
796 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
798 struct lagg_port *lp;
799 struct lagg_softc *sc;
801 if ((lp = ifp->if_lagg) == NULL)
807 lp->lp_detaching = 1;
808 lagg_port_destroy(lp, 1);
813 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
815 struct lagg_softc *sc = lp->lp_softc;
817 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
818 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
819 rp->rp_prio = lp->lp_prio;
820 rp->rp_flags = lp->lp_flags;
821 if (sc->sc_portreq != NULL)
822 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
824 /* Add protocol specific flags */
825 switch (sc->sc_proto) {
826 case LAGG_PROTO_FAILOVER:
827 if (lp == sc->sc_primary)
828 rp->rp_flags |= LAGG_PORT_MASTER;
829 if (lp == lagg_link_active(sc, sc->sc_primary))
830 rp->rp_flags |= LAGG_PORT_ACTIVE;
833 case LAGG_PROTO_ROUNDROBIN:
834 case LAGG_PROTO_LOADBALANCE:
835 case LAGG_PROTO_ETHERCHANNEL:
836 if (LAGG_PORTACTIVE(lp))
837 rp->rp_flags |= LAGG_PORT_ACTIVE;
840 case LAGG_PROTO_LACP:
841 /* LACP has a different definition of active */
842 if (lacp_isactive(lp))
843 rp->rp_flags |= LAGG_PORT_ACTIVE;
844 if (lacp_iscollecting(lp))
845 rp->rp_flags |= LAGG_PORT_COLLECTING;
846 if (lacp_isdistributing(lp))
847 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
856 struct lagg_softc *sc = (struct lagg_softc *)xsc;
857 struct lagg_port *lp;
858 struct ifnet *ifp = sc->sc_ifp;
860 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
865 ifp->if_drv_flags |= IFF_DRV_RUNNING;
866 /* Update the port lladdrs */
867 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
868 lagg_port_lladdr(lp, IF_LLADDR(ifp));
870 if (sc->sc_init != NULL)
877 lagg_stop(struct lagg_softc *sc)
879 struct ifnet *ifp = sc->sc_ifp;
881 LAGG_WLOCK_ASSERT(sc);
883 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
886 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
888 if (sc->sc_stop != NULL)
893 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
895 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
896 struct lagg_reqall *ra = (struct lagg_reqall *)data;
897 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
898 struct ifreq *ifr = (struct ifreq *)data;
899 struct lagg_port *lp;
901 struct thread *td = curthread;
903 int count, buflen, len, error = 0;
905 bzero(&rpbuf, sizeof(rpbuf));
911 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
913 buflen = count * sizeof(struct lagg_reqport);
916 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
919 ra->ra_proto = sc->sc_proto;
920 if (sc->sc_req != NULL)
921 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
925 len = min(ra->ra_size, buflen);
926 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
927 if (len < sizeof(rpbuf))
930 lagg_port2req(lp, &rpbuf);
931 memcpy(buf, &rpbuf, sizeof(rpbuf));
933 buf += sizeof(rpbuf);
934 len -= sizeof(rpbuf);
937 ra->ra_ports = count;
938 ra->ra_size = count * sizeof(rpbuf);
939 error = copyout(outbuf, ra->ra_port, ra->ra_size);
940 free(outbuf, M_TEMP);
943 error = priv_check(td, PRIV_NET_LAGG);
946 if (ra->ra_proto >= LAGG_PROTO_MAX) {
947 error = EPROTONOSUPPORT;
950 if (sc->sc_proto != LAGG_PROTO_NONE) {
952 error = sc->sc_detach(sc);
953 /* Reset protocol and pointers */
954 sc->sc_proto = LAGG_PROTO_NONE;
955 sc->sc_detach = NULL;
958 sc->sc_port_create = NULL;
959 sc->sc_port_destroy = NULL;
960 sc->sc_linkstate = NULL;
963 sc->sc_lladdr = NULL;
965 sc->sc_portreq = NULL;
970 for (int i = 0; i < (sizeof(lagg_protos) /
971 sizeof(lagg_protos[0])); i++) {
972 if (lagg_protos[i].ti_proto == ra->ra_proto) {
973 if (sc->sc_ifflags & IFF_DEBUG)
974 printf("%s: using proto %u\n",
976 lagg_protos[i].ti_proto);
978 sc->sc_proto = lagg_protos[i].ti_proto;
979 if (sc->sc_proto != LAGG_PROTO_NONE)
980 error = lagg_protos[i].ti_attach(sc);
985 error = EPROTONOSUPPORT;
988 if (rp->rp_portname[0] == '\0' ||
989 (tpif = ifunit(rp->rp_portname)) == NULL) {
995 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
996 lp->lp_softc != sc) {
1002 lagg_port2req(lp, rp);
1006 error = priv_check(td, PRIV_NET_LAGG);
1009 if (rp->rp_portname[0] == '\0' ||
1010 (tpif = ifunit(rp->rp_portname)) == NULL) {
1015 error = lagg_port_create(sc, tpif);
1018 case SIOCSLAGGDELPORT:
1019 error = priv_check(td, PRIV_NET_LAGG);
1022 if (rp->rp_portname[0] == '\0' ||
1023 (tpif = ifunit(rp->rp_portname)) == NULL) {
1029 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1030 lp->lp_softc != sc) {
1036 error = lagg_port_destroy(lp, 1);
1040 /* Set flags on ports too */
1042 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1043 lagg_setflags(lp, 1);
1047 if (!(ifp->if_flags & IFF_UP) &&
1048 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1050 * If interface is marked down and it is running,
1051 * then stop and disable it.
1056 } else if ((ifp->if_flags & IFF_UP) &&
1057 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1059 * If interface is marked up and it is stopped, then
1062 (*ifp->if_init)(sc);
1068 error = lagg_ether_setmulti(sc);
1073 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1078 /* Do not allow the MTU or caps to be directly changed */
1083 error = ether_ioctl(ifp, cmd, data);
1090 lagg_ether_setmulti(struct lagg_softc *sc)
1092 struct lagg_port *lp;
1094 LAGG_WLOCK_ASSERT(sc);
1096 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1097 /* First, remove any existing filter entries. */
1098 lagg_ether_cmdmulti(lp, 0);
1099 /* copy all addresses from the lagg interface to the port */
1100 lagg_ether_cmdmulti(lp, 1);
1106 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1108 struct lagg_softc *sc = lp->lp_softc;
1109 struct ifnet *ifp = lp->lp_ifp;
1110 struct ifnet *scifp = sc->sc_ifp;
1112 struct ifmultiaddr *ifma, *rifma = NULL;
1113 struct sockaddr_dl sdl;
1116 LAGG_WLOCK_ASSERT(sc);
1118 bzero((char *)&sdl, sizeof(sdl));
1119 sdl.sdl_len = sizeof(sdl);
1120 sdl.sdl_family = AF_LINK;
1121 sdl.sdl_type = IFT_ETHER;
1122 sdl.sdl_alen = ETHER_ADDR_LEN;
1123 sdl.sdl_index = ifp->if_index;
1126 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1127 if (ifma->ifma_addr->sa_family != AF_LINK)
1129 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1130 LLADDR(&sdl), ETHER_ADDR_LEN);
1132 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1135 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1138 mc->mc_ifma = rifma;
1139 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1142 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1143 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1144 if_delmulti_ifma(mc->mc_ifma);
1151 /* Handle a ref counted flag that should be set on the lagg port as well */
1153 lagg_setflag(struct lagg_port *lp, int flag, int status,
1154 int (*func)(struct ifnet *, int))
1156 struct lagg_softc *sc = lp->lp_softc;
1157 struct ifnet *scifp = sc->sc_ifp;
1158 struct ifnet *ifp = lp->lp_ifp;
1161 LAGG_WLOCK_ASSERT(sc);
1163 status = status ? (scifp->if_flags & flag) : 0;
1164 /* Now "status" contains the flag value or 0 */
1167 * See if recorded ports status is different from what
1168 * we want it to be. If it is, flip it. We record ports
1169 * status in lp_ifflags so that we won't clear ports flag
1170 * we haven't set. In fact, we don't clear or set ports
1171 * flags directly, but get or release references to them.
1172 * That's why we can be sure that recorded flags still are
1173 * in accord with actual ports flags.
1175 if (status != (lp->lp_ifflags & flag)) {
1176 error = (*func)(ifp, status);
1179 lp->lp_ifflags &= ~flag;
1180 lp->lp_ifflags |= status;
1186 * Handle IFF_* flags that require certain changes on the lagg port
1187 * if "status" is true, update ports flags respective to the lagg
1188 * if "status" is false, forcedly clear the flags set on port.
1191 lagg_setflags(struct lagg_port *lp, int status)
1195 for (i = 0; lagg_pflags[i].flag; i++) {
1196 error = lagg_setflag(lp, lagg_pflags[i].flag,
1197 status, lagg_pflags[i].func);
1205 lagg_start(struct ifnet *ifp)
1207 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1212 /* We need a Tx algorithm and at least one port */
1213 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1214 IF_DRAIN(&ifp->if_snd);
1219 for (;; error = 0) {
1220 IFQ_DEQUEUE(&ifp->if_snd, m);
1224 ETHER_BPF_MTAP(ifp, m);
1226 error = (*sc->sc_start)(sc, m);
1235 static struct mbuf *
1236 lagg_input(struct ifnet *ifp, struct mbuf *m)
1238 struct lagg_port *lp = ifp->if_lagg;
1239 struct lagg_softc *sc = lp->lp_softc;
1240 struct ifnet *scifp = sc->sc_ifp;
1243 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1244 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1245 sc->sc_proto == LAGG_PROTO_NONE) {
1251 ETHER_BPF_MTAP(scifp, m);
1253 m = (*sc->sc_input)(sc, lp, m);
1256 scifp->if_ipackets++;
1257 scifp->if_ibytes += m->m_pkthdr.len;
1259 if (scifp->if_flags & IFF_MONITOR) {
1270 lagg_media_change(struct ifnet *ifp)
1272 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1274 if (sc->sc_ifflags & IFF_DEBUG)
1275 printf("%s\n", __func__);
1282 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1284 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1285 struct lagg_port *lp;
1287 imr->ifm_status = IFM_AVALID;
1288 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1291 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1292 if (LAGG_PORTACTIVE(lp))
1293 imr->ifm_status |= IFM_ACTIVE;
1299 lagg_linkstate(struct lagg_softc *sc)
1301 struct lagg_port *lp;
1302 int new_link = LINK_STATE_DOWN;
1305 /* Our link is considered up if at least one of our ports is active */
1306 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1307 if (lp->lp_link_state == LINK_STATE_UP) {
1308 new_link = LINK_STATE_UP;
1312 if_link_state_change(sc->sc_ifp, new_link);
1314 /* Update if_baudrate to reflect the max possible speed */
1315 switch (sc->sc_proto) {
1316 case LAGG_PROTO_FAILOVER:
1317 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1318 sc->sc_primary->lp_ifp->if_baudrate : 0;
1320 case LAGG_PROTO_ROUNDROBIN:
1321 case LAGG_PROTO_LOADBALANCE:
1322 case LAGG_PROTO_ETHERCHANNEL:
1324 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1325 speed += lp->lp_ifp->if_baudrate;
1326 sc->sc_ifp->if_baudrate = speed;
1328 case LAGG_PROTO_LACP:
1329 /* LACP updates if_baudrate itself */
1335 lagg_port_state(struct ifnet *ifp, int state)
1337 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1338 struct lagg_softc *sc = NULL;
1347 if (sc->sc_linkstate != NULL)
1348 (*sc->sc_linkstate)(lp);
1353 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1355 struct lagg_port *lp_next, *rval = NULL;
1356 // int new_link = LINK_STATE_DOWN;
1358 LAGG_RLOCK_ASSERT(sc);
1360 * Search a port which reports an active link state.
1365 if (LAGG_PORTACTIVE(lp)) {
1369 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1370 LAGG_PORTACTIVE(lp_next)) {
1376 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1377 if (LAGG_PORTACTIVE(lp_next)) {
1386 * The IEEE 802.1D standard assumes that a lagg with
1387 * multiple ports is always full duplex. This is valid
1388 * for load sharing laggs and if at least two links
1389 * are active. Unfortunately, checking the latter would
1390 * be too expensive at this point.
1392 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1394 new_link = LINK_STATE_FULL_DUPLEX;
1396 new_link = rval->lp_link_state;
1404 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1406 if (m->m_pkthdr.len < (off + len)) {
1408 } else if (m->m_len < (off + len)) {
1409 m_copydata(m, off, len, buf);
1412 return (mtod(m, char *) + off);
1416 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1421 struct ether_header *eh;
1422 struct ether_vlan_header vlanbuf;
1423 const struct ether_vlan_header *vlan;
1425 const struct ip *ip;
1429 const struct ip6_hdr *ip6;
1430 struct ip6_hdr ip6buf;
1437 eh = mtod(m, struct ether_header *);
1438 etype = ntohs(eh->ether_type);
1439 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1440 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1442 /* Special handling for encapsulating VLAN frames */
1443 if (m->m_flags & M_VLANTAG) {
1444 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1445 sizeof(m->m_pkthdr.ether_vtag), p);
1446 } else if (etype == ETHERTYPE_VLAN) {
1447 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1451 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1452 etype = ntohs(vlan->evl_proto);
1453 off += sizeof(*vlan) - sizeof(*eh);
1459 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1463 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1464 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1468 case ETHERTYPE_IPV6:
1469 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1473 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1474 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1475 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1476 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1485 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1488 return (ifp->if_transmit)(ifp, m);
1492 * Simple round robin aggregation
1496 lagg_rr_attach(struct lagg_softc *sc)
1498 sc->sc_detach = lagg_rr_detach;
1499 sc->sc_start = lagg_rr_start;
1500 sc->sc_input = lagg_rr_input;
1501 sc->sc_port_create = NULL;
1502 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1509 lagg_rr_detach(struct lagg_softc *sc)
1515 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1517 struct lagg_port *lp;
1520 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1522 lp = SLIST_FIRST(&sc->sc_ports);
1524 lp = SLIST_NEXT(lp, lp_entries);
1527 * Check the port's link state. This will return the next active
1528 * port if the link is down or the port is NULL.
1530 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1536 return (lagg_enqueue(lp->lp_ifp, m));
1539 static struct mbuf *
1540 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1542 struct ifnet *ifp = sc->sc_ifp;
1544 /* Just pass in the packet to our lagg device */
1545 m->m_pkthdr.rcvif = ifp;
1555 lagg_fail_attach(struct lagg_softc *sc)
1557 sc->sc_detach = lagg_fail_detach;
1558 sc->sc_start = lagg_fail_start;
1559 sc->sc_input = lagg_fail_input;
1560 sc->sc_port_create = NULL;
1561 sc->sc_port_destroy = NULL;
1567 lagg_fail_detach(struct lagg_softc *sc)
1573 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1575 struct lagg_port *lp;
1577 /* Use the master port if active or the next available port */
1578 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1584 return (lagg_enqueue(lp->lp_ifp, m));
1587 static struct mbuf *
1588 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1590 struct ifnet *ifp = sc->sc_ifp;
1591 struct lagg_port *tmp_tp;
1593 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1594 m->m_pkthdr.rcvif = ifp;
1598 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1599 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1601 * If tmp_tp is null, we've recieved a packet when all
1602 * our links are down. Weird, but process it anyways.
1604 if ((tmp_tp == NULL || tmp_tp == lp)) {
1605 m->m_pkthdr.rcvif = ifp;
1619 lagg_lb_attach(struct lagg_softc *sc)
1621 struct lagg_port *lp;
1624 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1625 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1628 sc->sc_detach = lagg_lb_detach;
1629 sc->sc_start = lagg_lb_start;
1630 sc->sc_input = lagg_lb_input;
1631 sc->sc_port_create = lagg_lb_port_create;
1632 sc->sc_port_destroy = lagg_lb_port_destroy;
1633 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1635 lb->lb_key = arc4random();
1636 sc->sc_psc = (caddr_t)lb;
1638 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1639 lagg_lb_port_create(lp);
1645 lagg_lb_detach(struct lagg_softc *sc)
1647 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1654 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1656 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1657 struct lagg_port *lp_next;
1660 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1661 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1664 if (i >= LAGG_MAX_PORTS)
1666 if (sc->sc_ifflags & IFF_DEBUG)
1667 printf("%s: port %s at index %d\n",
1668 sc->sc_ifname, lp_next->lp_ifname, i);
1669 lb->lb_ports[i++] = lp_next;
1676 lagg_lb_port_create(struct lagg_port *lp)
1678 struct lagg_softc *sc = lp->lp_softc;
1679 return (lagg_lb_porttable(sc, NULL));
1683 lagg_lb_port_destroy(struct lagg_port *lp)
1685 struct lagg_softc *sc = lp->lp_softc;
1686 lagg_lb_porttable(sc, lp);
1690 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1692 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1693 struct lagg_port *lp = NULL;
1696 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1697 p = m->m_pkthdr.flowid;
1699 p = lagg_hashmbuf(m, lb->lb_key);
1701 lp = lb->lb_ports[p];
1704 * Check the port's link state. This will return the next active
1705 * port if the link is down or the port is NULL.
1707 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1713 return (lagg_enqueue(lp->lp_ifp, m));
1716 static struct mbuf *
1717 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1719 struct ifnet *ifp = sc->sc_ifp;
1721 /* Just pass in the packet to our lagg device */
1722 m->m_pkthdr.rcvif = ifp;
1732 lagg_lacp_attach(struct lagg_softc *sc)
1734 struct lagg_port *lp;
1737 sc->sc_detach = lagg_lacp_detach;
1738 sc->sc_port_create = lacp_port_create;
1739 sc->sc_port_destroy = lacp_port_destroy;
1740 sc->sc_linkstate = lacp_linkstate;
1741 sc->sc_start = lagg_lacp_start;
1742 sc->sc_input = lagg_lacp_input;
1743 sc->sc_init = lacp_init;
1744 sc->sc_stop = lacp_stop;
1745 sc->sc_lladdr = lagg_lacp_lladdr;
1746 sc->sc_req = lacp_req;
1747 sc->sc_portreq = lacp_portreq;
1749 error = lacp_attach(sc);
1753 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1754 lacp_port_create(lp);
1760 lagg_lacp_detach(struct lagg_softc *sc)
1762 struct lagg_port *lp;
1765 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1766 lacp_port_destroy(lp);
1768 /* unlocking is safe here */
1770 error = lacp_detach(sc);
1777 lagg_lacp_lladdr(struct lagg_softc *sc)
1779 struct lagg_port *lp;
1781 /* purge all the lacp ports */
1782 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1783 lacp_port_destroy(lp);
1785 /* add them back in */
1786 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1787 lacp_port_create(lp);
1791 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1793 struct lagg_port *lp;
1795 lp = lacp_select_tx_port(sc, m);
1802 return (lagg_enqueue(lp->lp_ifp, m));
1805 static struct mbuf *
1806 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1808 struct ifnet *ifp = sc->sc_ifp;
1809 struct ether_header *eh;
1812 eh = mtod(m, struct ether_header *);
1813 etype = ntohs(eh->ether_type);
1815 /* Tap off LACP control messages */
1816 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1817 m = lacp_input(lp, m);
1823 * If the port is not collecting or not in the active aggregator then
1826 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1831 m->m_pkthdr.rcvif = ifp;