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>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_ifattach.h>
70 #include <net/if_vlan_var.h>
71 #include <net/if_lagg.h>
72 #include <net/ieee8023ad_lacp.h>
74 /* Special flags we should propagate to the lagg ports. */
77 int (*func)(struct ifnet *, int);
79 {IFF_PROMISC, ifpromisc},
80 {IFF_ALLMULTI, if_allmulti},
84 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
85 static struct mtx lagg_list_mtx;
86 eventhandler_tag lagg_detach_cookie = NULL;
88 static int lagg_clone_create(struct if_clone *, int, caddr_t);
89 static void lagg_clone_destroy(struct ifnet *);
90 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
91 static void lagg_capabilities(struct lagg_softc *);
92 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
93 static void lagg_port_setlladdr(void *, int);
94 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
95 static int lagg_port_destroy(struct lagg_port *, int);
96 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
97 static void lagg_linkstate(struct lagg_softc *);
98 static void lagg_port_state(struct ifnet *, int);
99 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
100 static int lagg_port_output(struct ifnet *, struct mbuf *,
101 struct sockaddr *, struct route *);
102 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
103 #ifdef LAGG_PORT_STACKING
104 static int lagg_port_checkstacking(struct lagg_softc *);
106 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
107 static void lagg_init(void *);
108 static void lagg_stop(struct lagg_softc *);
109 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
110 static int lagg_ether_setmulti(struct lagg_softc *);
111 static int lagg_ether_cmdmulti(struct lagg_port *, int);
112 static int lagg_setflag(struct lagg_port *, int, int,
113 int (*func)(struct ifnet *, int));
114 static int lagg_setflags(struct lagg_port *, int status);
115 static int lagg_transmit(struct ifnet *, struct mbuf *);
116 static void lagg_qflush(struct ifnet *);
117 static int lagg_media_change(struct ifnet *);
118 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
119 static struct lagg_port *lagg_link_active(struct lagg_softc *,
121 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
123 IFC_SIMPLE_DECLARE(lagg, 0);
125 /* Simple round robin */
126 static int lagg_rr_attach(struct lagg_softc *);
127 static int lagg_rr_detach(struct lagg_softc *);
128 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
129 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
132 /* Active failover */
133 static int lagg_fail_attach(struct lagg_softc *);
134 static int lagg_fail_detach(struct lagg_softc *);
135 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
136 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
140 static int lagg_lb_attach(struct lagg_softc *);
141 static int lagg_lb_detach(struct lagg_softc *);
142 static int lagg_lb_port_create(struct lagg_port *);
143 static void lagg_lb_port_destroy(struct lagg_port *);
144 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
145 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
147 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
150 static int lagg_lacp_attach(struct lagg_softc *);
151 static int lagg_lacp_detach(struct lagg_softc *);
152 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
153 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
155 static void lagg_lacp_lladdr(struct lagg_softc *);
157 /* lagg protocol table */
158 static const struct {
160 int (*ti_attach)(struct lagg_softc *);
162 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
163 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
164 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
165 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
166 { LAGG_PROTO_LACP, lagg_lacp_attach },
167 { LAGG_PROTO_NONE, NULL }
170 SYSCTL_DECL(_net_link);
171 static SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
174 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
175 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
176 &lagg_failover_rx_all, 0,
177 "Accept input from any interface in a failover lagg");
178 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
179 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
180 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
182 "Default setting for using flow id for load sharing");
185 lagg_modevent(module_t mod, int type, void *data)
190 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
191 SLIST_INIT(&lagg_list);
192 if_clone_attach(&lagg_cloner);
193 lagg_input_p = lagg_input;
194 lagg_linkstate_p = lagg_port_state;
195 lagg_detach_cookie = EVENTHANDLER_REGISTER(
196 ifnet_departure_event, lagg_port_ifdetach, NULL,
197 EVENTHANDLER_PRI_ANY);
200 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
202 if_clone_detach(&lagg_cloner);
204 lagg_linkstate_p = NULL;
205 mtx_destroy(&lagg_list_mtx);
213 static moduledata_t lagg_mod = {
219 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
220 MODULE_VERSION(if_lagg, 1);
222 #if __FreeBSD_version >= 800000
224 * This routine is run via an vlan
228 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
230 struct lagg_softc *sc = ifp->if_softc;
231 struct lagg_port *lp;
233 if (ifp->if_softc != arg) /* Not our event */
237 if (!SLIST_EMPTY(&sc->sc_ports)) {
238 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
239 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
245 * This routine is run via an vlan
249 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
251 struct lagg_softc *sc = ifp->if_softc;
252 struct lagg_port *lp;
254 if (ifp->if_softc != arg) /* Not our event */
258 if (!SLIST_EMPTY(&sc->sc_ports)) {
259 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
260 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
267 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
269 struct lagg_softc *sc;
272 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
273 struct sysctl_oid *oid;
274 char num[14]; /* sufficient for 32 bits */
276 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
277 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
283 sysctl_ctx_init(&sc->ctx);
284 snprintf(num, sizeof(num), "%u", unit);
285 sc->use_flowid = def_use_flowid;
286 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
287 OID_AUTO, num, CTLFLAG_RD, NULL, "");
288 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
289 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
290 "Use flow id for load sharing");
291 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
292 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
293 "Total number of ports");
294 /* Hash all layers by default */
295 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
297 sc->sc_proto = LAGG_PROTO_NONE;
298 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
299 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
300 sc->sc_proto = lagg_protos[i].ti_proto;
301 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
302 if_free_type(ifp, IFT_ETHER);
310 SLIST_INIT(&sc->sc_ports);
311 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
313 /* Initialise pseudo media types */
314 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
316 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
317 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
319 if_initname(ifp, ifc->ifc_name, unit);
320 ifp->if_type = IFT_ETHER;
322 ifp->if_transmit = lagg_transmit;
323 ifp->if_qflush = lagg_qflush;
324 ifp->if_init = lagg_init;
325 ifp->if_ioctl = lagg_ioctl;
326 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
329 * Attach as an ordinary ethernet device, childs will be attached
330 * as special device IFT_IEEE8023ADLAG.
332 ether_ifattach(ifp, eaddr);
334 #if __FreeBSD_version >= 800000
335 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
336 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
337 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
338 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
341 /* Insert into the global list of laggs */
342 mtx_lock(&lagg_list_mtx);
343 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
344 mtx_unlock(&lagg_list_mtx);
350 lagg_clone_destroy(struct ifnet *ifp)
352 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
353 struct lagg_port *lp;
358 ifp->if_flags &= ~IFF_UP;
360 #if __FreeBSD_version >= 800000
361 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
362 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
365 /* Shutdown and remove lagg ports */
366 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
367 lagg_port_destroy(lp, 1);
368 /* Unhook the aggregation protocol */
369 if (sc->sc_detach != NULL)
370 (*sc->sc_detach)(sc);
374 sysctl_ctx_free(&sc->ctx);
375 ifmedia_removeall(&sc->sc_media);
377 if_free_type(ifp, IFT_ETHER);
379 mtx_lock(&lagg_list_mtx);
380 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
381 mtx_unlock(&lagg_list_mtx);
383 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
384 LAGG_LOCK_DESTROY(sc);
389 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
391 struct ifnet *ifp = sc->sc_ifp;
393 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
396 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
397 /* Let the protocol know the MAC has changed */
398 if (sc->sc_lladdr != NULL)
399 (*sc->sc_lladdr)(sc);
400 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
404 lagg_capabilities(struct lagg_softc *sc)
406 struct lagg_port *lp;
407 int cap = ~0, ena = ~0;
410 LAGG_WLOCK_ASSERT(sc);
412 /* Get capabilities from the lagg ports */
413 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
414 cap &= lp->lp_ifp->if_capabilities;
415 ena &= lp->lp_ifp->if_capenable;
416 hwa &= lp->lp_ifp->if_hwassist;
418 cap = (cap == ~0 ? 0 : cap);
419 ena = (ena == ~0 ? 0 : ena);
420 hwa = (hwa == ~0 ? 0 : hwa);
422 if (sc->sc_ifp->if_capabilities != cap ||
423 sc->sc_ifp->if_capenable != ena ||
424 sc->sc_ifp->if_hwassist != hwa) {
425 sc->sc_ifp->if_capabilities = cap;
426 sc->sc_ifp->if_capenable = ena;
427 sc->sc_ifp->if_hwassist = hwa;
428 getmicrotime(&sc->sc_ifp->if_lastchange);
430 if (sc->sc_ifflags & IFF_DEBUG)
431 if_printf(sc->sc_ifp,
432 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
437 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
439 struct lagg_softc *sc = lp->lp_softc;
440 struct ifnet *ifp = lp->lp_ifp;
441 struct lagg_llq *llq;
444 LAGG_WLOCK_ASSERT(sc);
446 if (lp->lp_detaching ||
447 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
450 /* Check to make sure its not already queued to be changed */
451 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
452 if (llq->llq_ifp == ifp) {
459 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
460 if (llq == NULL) /* XXX what to do */
464 /* Update the lladdr even if pending, it may have changed */
466 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
469 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
471 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
475 * Set the interface MAC address from a taskqueue to avoid a LOR.
478 lagg_port_setlladdr(void *arg, int pending)
480 struct lagg_softc *sc = (struct lagg_softc *)arg;
481 struct lagg_llq *llq, *head;
485 /* Grab a local reference of the queue and remove it from the softc */
487 head = SLIST_FIRST(&sc->sc_llq_head);
488 SLIST_FIRST(&sc->sc_llq_head) = NULL;
492 * Traverse the queue and set the lladdr on each ifp. It is safe to do
493 * unlocked as we have the only reference to it.
495 for (llq = head; llq != NULL; llq = head) {
498 /* Set the link layer address */
499 CURVNET_SET(ifp->if_vnet);
500 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
503 printf("%s: setlladdr failed on %s\n", __func__,
506 head = SLIST_NEXT(llq, llq_entries);
512 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
514 struct lagg_softc *sc_ptr;
515 struct lagg_port *lp;
518 LAGG_WLOCK_ASSERT(sc);
520 /* Limit the maximal number of lagg ports */
521 if (sc->sc_count >= LAGG_MAX_PORTS)
524 /* Check if port has already been associated to a lagg */
525 if (ifp->if_lagg != NULL) {
526 /* Port is already in the current lagg? */
527 lp = (struct lagg_port *)ifp->if_lagg;
528 if (lp->lp_softc == sc)
533 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
534 if (ifp->if_type != IFT_ETHER)
535 return (EPROTONOSUPPORT);
539 * The member interface should not have inet6 address because
540 * two interfaces with a valid link-local scope zone must not be
541 * merged in any form. This restriction is needed to
542 * prevent violation of link-local scope zone. Attempts to
543 * add a member interface which has inet6 addresses triggers
544 * removal of all inet6 addresses on the member interface.
546 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
547 if (in6ifa_llaonifp(lp->lp_ifp)) {
548 in6_ifdetach(lp->lp_ifp);
549 if_printf(sc->sc_ifp,
550 "IPv6 addresses on %s have been removed "
551 "before adding it as a member to prevent "
552 "IPv6 address scope violation.\n",
553 lp->lp_ifp->if_xname);
556 if (in6ifa_llaonifp(ifp)) {
558 if_printf(sc->sc_ifp,
559 "IPv6 addresses on %s have been removed "
560 "before adding it as a member to prevent "
561 "IPv6 address scope violation.\n",
565 /* Allow the first Ethernet member to define the MTU */
566 if (SLIST_EMPTY(&sc->sc_ports))
567 sc->sc_ifp->if_mtu = ifp->if_mtu;
568 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
569 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
574 if ((lp = malloc(sizeof(struct lagg_port),
575 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
578 /* Check if port is a stacked lagg */
579 mtx_lock(&lagg_list_mtx);
580 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
581 if (ifp == sc_ptr->sc_ifp) {
582 mtx_unlock(&lagg_list_mtx);
585 /* XXX disable stacking for the moment, its untested */
586 #ifdef LAGG_PORT_STACKING
587 lp->lp_flags |= LAGG_PORT_STACK;
588 if (lagg_port_checkstacking(sc_ptr) >=
590 mtx_unlock(&lagg_list_mtx);
597 mtx_unlock(&lagg_list_mtx);
599 /* Change the interface type */
600 lp->lp_iftype = ifp->if_type;
601 ifp->if_type = IFT_IEEE8023ADLAG;
603 lp->lp_ioctl = ifp->if_ioctl;
604 ifp->if_ioctl = lagg_port_ioctl;
605 lp->lp_output = ifp->if_output;
606 ifp->if_output = lagg_port_output;
611 /* Save port link layer address */
612 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
614 if (SLIST_EMPTY(&sc->sc_ports)) {
616 lagg_lladdr(sc, IF_LLADDR(ifp));
618 /* Update link layer address for this port */
619 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
622 /* Insert into the list of ports */
623 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
626 /* Update lagg capabilities */
627 lagg_capabilities(sc);
630 /* Add multicast addresses and interface flags to this port */
631 lagg_ether_cmdmulti(lp, 1);
632 lagg_setflags(lp, 1);
634 if (sc->sc_port_create != NULL)
635 error = (*sc->sc_port_create)(lp);
637 /* remove the port again, without calling sc_port_destroy */
638 lagg_port_destroy(lp, 0);
645 #ifdef LAGG_PORT_STACKING
647 lagg_port_checkstacking(struct lagg_softc *sc)
649 struct lagg_softc *sc_ptr;
650 struct lagg_port *lp;
653 LAGG_WLOCK_ASSERT(sc);
655 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
656 if (lp->lp_flags & LAGG_PORT_STACK) {
657 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
658 m = MAX(m, lagg_port_checkstacking(sc_ptr));
667 lagg_port_destroy(struct lagg_port *lp, int runpd)
669 struct lagg_softc *sc = lp->lp_softc;
670 struct lagg_port *lp_ptr;
671 struct lagg_llq *llq;
672 struct ifnet *ifp = lp->lp_ifp;
674 LAGG_WLOCK_ASSERT(sc);
676 if (runpd && sc->sc_port_destroy != NULL)
677 (*sc->sc_port_destroy)(lp);
680 * Remove multicast addresses and interface flags from this port and
681 * reset the MAC address, skip if the interface is being detached.
683 if (!lp->lp_detaching) {
684 lagg_ether_cmdmulti(lp, 0);
685 lagg_setflags(lp, 0);
686 lagg_port_lladdr(lp, lp->lp_lladdr);
689 /* Restore interface */
690 ifp->if_type = lp->lp_iftype;
691 ifp->if_ioctl = lp->lp_ioctl;
692 ifp->if_output = lp->lp_output;
695 /* Finally, remove the port from the lagg */
696 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
699 /* Update the primary interface */
700 if (lp == sc->sc_primary) {
701 uint8_t lladdr[ETHER_ADDR_LEN];
703 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
704 bzero(&lladdr, ETHER_ADDR_LEN);
706 bcopy(lp_ptr->lp_lladdr,
707 lladdr, ETHER_ADDR_LEN);
709 lagg_lladdr(sc, lladdr);
710 sc->sc_primary = lp_ptr;
712 /* Update link layer address for each port */
713 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
714 lagg_port_lladdr(lp_ptr, lladdr);
717 /* Remove any pending lladdr changes from the queue */
718 if (lp->lp_detaching) {
719 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
720 if (llq->llq_ifp == ifp) {
721 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
724 break; /* Only appears once */
730 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
734 /* Update lagg capabilities */
735 lagg_capabilities(sc);
742 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
744 struct lagg_reqport *rp = (struct lagg_reqport *)data;
745 struct lagg_softc *sc;
746 struct lagg_port *lp = NULL;
749 /* Should be checked by the caller */
750 if (ifp->if_type != IFT_IEEE8023ADLAG ||
751 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
756 if (rp->rp_portname[0] == '\0' ||
757 ifunit(rp->rp_portname) != ifp) {
763 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
769 lagg_port2req(lp, rp);
774 if (lp->lp_ioctl == NULL) {
778 error = (*lp->lp_ioctl)(ifp, cmd, data);
782 /* Update lagg interface capabilities */
784 lagg_capabilities(sc);
789 /* Do not allow the MTU to be changed once joined */
800 if (lp->lp_ioctl != NULL)
801 return ((*lp->lp_ioctl)(ifp, cmd, data));
807 * For direct output to child ports.
810 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
811 struct sockaddr *dst, struct route *ro)
813 struct lagg_port *lp = ifp->if_lagg;
815 switch (dst->sa_family) {
816 case pseudo_AF_HDRCMPLT:
818 return ((*lp->lp_output)(ifp, m, dst, ro));
821 /* drop any other frames */
827 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
829 struct lagg_port *lp;
830 struct lagg_softc *sc;
832 if ((lp = ifp->if_lagg) == NULL)
834 /* If the ifnet is just being renamed, don't do anything. */
835 if (ifp->if_flags & IFF_RENAMING)
841 lp->lp_detaching = 1;
842 lagg_port_destroy(lp, 1);
847 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
849 struct lagg_softc *sc = lp->lp_softc;
851 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
852 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
853 rp->rp_prio = lp->lp_prio;
854 rp->rp_flags = lp->lp_flags;
855 if (sc->sc_portreq != NULL)
856 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
858 /* Add protocol specific flags */
859 switch (sc->sc_proto) {
860 case LAGG_PROTO_FAILOVER:
861 if (lp == sc->sc_primary)
862 rp->rp_flags |= LAGG_PORT_MASTER;
863 if (lp == lagg_link_active(sc, sc->sc_primary))
864 rp->rp_flags |= LAGG_PORT_ACTIVE;
867 case LAGG_PROTO_ROUNDROBIN:
868 case LAGG_PROTO_LOADBALANCE:
869 case LAGG_PROTO_ETHERCHANNEL:
870 if (LAGG_PORTACTIVE(lp))
871 rp->rp_flags |= LAGG_PORT_ACTIVE;
874 case LAGG_PROTO_LACP:
875 /* LACP has a different definition of active */
876 if (lacp_isactive(lp))
877 rp->rp_flags |= LAGG_PORT_ACTIVE;
878 if (lacp_iscollecting(lp))
879 rp->rp_flags |= LAGG_PORT_COLLECTING;
880 if (lacp_isdistributing(lp))
881 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
890 struct lagg_softc *sc = (struct lagg_softc *)xsc;
891 struct lagg_port *lp;
892 struct ifnet *ifp = sc->sc_ifp;
894 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
899 ifp->if_drv_flags |= IFF_DRV_RUNNING;
900 /* Update the port lladdrs */
901 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
902 lagg_port_lladdr(lp, IF_LLADDR(ifp));
904 if (sc->sc_init != NULL)
911 lagg_stop(struct lagg_softc *sc)
913 struct ifnet *ifp = sc->sc_ifp;
915 LAGG_WLOCK_ASSERT(sc);
917 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
920 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
922 if (sc->sc_stop != NULL)
927 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
929 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
930 struct lagg_reqall *ra = (struct lagg_reqall *)data;
931 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
932 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
933 struct ifreq *ifr = (struct ifreq *)data;
934 struct lagg_port *lp;
936 struct thread *td = curthread;
938 int count, buflen, len, error = 0;
940 bzero(&rpbuf, sizeof(rpbuf));
946 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
948 buflen = count * sizeof(struct lagg_reqport);
951 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
954 ra->ra_proto = sc->sc_proto;
955 if (sc->sc_req != NULL)
956 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
960 len = min(ra->ra_size, buflen);
961 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
962 if (len < sizeof(rpbuf))
965 lagg_port2req(lp, &rpbuf);
966 memcpy(buf, &rpbuf, sizeof(rpbuf));
968 buf += sizeof(rpbuf);
969 len -= sizeof(rpbuf);
972 ra->ra_ports = count;
973 ra->ra_size = count * sizeof(rpbuf);
974 error = copyout(outbuf, ra->ra_port, ra->ra_size);
975 free(outbuf, M_TEMP);
978 error = priv_check(td, PRIV_NET_LAGG);
981 if (ra->ra_proto >= LAGG_PROTO_MAX) {
982 error = EPROTONOSUPPORT;
986 if (sc->sc_proto != LAGG_PROTO_NONE) {
987 /* Reset protocol first in case detach unlocks */
988 sc->sc_proto = LAGG_PROTO_NONE;
989 error = sc->sc_detach(sc);
990 sc->sc_detach = NULL;
993 sc->sc_port_create = NULL;
994 sc->sc_port_destroy = NULL;
995 sc->sc_linkstate = NULL;
998 sc->sc_lladdr = NULL;
1000 sc->sc_portreq = NULL;
1001 } else if (sc->sc_input != NULL) {
1002 /* Still detaching */
1009 for (int i = 0; i < (sizeof(lagg_protos) /
1010 sizeof(lagg_protos[0])); i++) {
1011 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1012 if (sc->sc_ifflags & IFF_DEBUG)
1013 printf("%s: using proto %u\n",
1015 lagg_protos[i].ti_proto);
1016 sc->sc_proto = lagg_protos[i].ti_proto;
1017 if (sc->sc_proto != LAGG_PROTO_NONE)
1018 error = lagg_protos[i].ti_attach(sc);
1024 error = EPROTONOSUPPORT;
1026 case SIOCGLAGGFLAGS:
1027 rf->rf_flags = sc->sc_flags;
1030 error = priv_check(td, PRIV_NET_LAGG);
1033 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1038 sc->sc_flags &= ~LAGG_F_HASHMASK;
1039 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1043 if (rp->rp_portname[0] == '\0' ||
1044 (tpif = ifunit(rp->rp_portname)) == NULL) {
1050 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1051 lp->lp_softc != sc) {
1057 lagg_port2req(lp, rp);
1061 error = priv_check(td, PRIV_NET_LAGG);
1064 if (rp->rp_portname[0] == '\0' ||
1065 (tpif = ifunit(rp->rp_portname)) == NULL) {
1070 error = lagg_port_create(sc, tpif);
1073 case SIOCSLAGGDELPORT:
1074 error = priv_check(td, PRIV_NET_LAGG);
1077 if (rp->rp_portname[0] == '\0' ||
1078 (tpif = ifunit(rp->rp_portname)) == NULL) {
1084 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1085 lp->lp_softc != sc) {
1091 error = lagg_port_destroy(lp, 1);
1095 /* Set flags on ports too */
1097 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1098 lagg_setflags(lp, 1);
1102 if (!(ifp->if_flags & IFF_UP) &&
1103 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1105 * If interface is marked down and it is running,
1106 * then stop and disable it.
1111 } else if ((ifp->if_flags & IFF_UP) &&
1112 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1114 * If interface is marked up and it is stopped, then
1117 (*ifp->if_init)(sc);
1123 error = lagg_ether_setmulti(sc);
1128 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1133 /* Do not allow the MTU or caps to be directly changed */
1138 error = ether_ioctl(ifp, cmd, data);
1145 lagg_ether_setmulti(struct lagg_softc *sc)
1147 struct lagg_port *lp;
1149 LAGG_WLOCK_ASSERT(sc);
1151 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1152 /* First, remove any existing filter entries. */
1153 lagg_ether_cmdmulti(lp, 0);
1154 /* copy all addresses from the lagg interface to the port */
1155 lagg_ether_cmdmulti(lp, 1);
1161 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1163 struct lagg_softc *sc = lp->lp_softc;
1164 struct ifnet *ifp = lp->lp_ifp;
1165 struct ifnet *scifp = sc->sc_ifp;
1167 struct ifmultiaddr *ifma, *rifma = NULL;
1168 struct sockaddr_dl sdl;
1171 LAGG_WLOCK_ASSERT(sc);
1173 bzero((char *)&sdl, sizeof(sdl));
1174 sdl.sdl_len = sizeof(sdl);
1175 sdl.sdl_family = AF_LINK;
1176 sdl.sdl_type = IFT_ETHER;
1177 sdl.sdl_alen = ETHER_ADDR_LEN;
1178 sdl.sdl_index = ifp->if_index;
1181 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1182 if (ifma->ifma_addr->sa_family != AF_LINK)
1184 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1185 LLADDR(&sdl), ETHER_ADDR_LEN);
1187 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1190 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1193 mc->mc_ifma = rifma;
1194 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1197 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1198 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1199 if_delmulti_ifma(mc->mc_ifma);
1206 /* Handle a ref counted flag that should be set on the lagg port as well */
1208 lagg_setflag(struct lagg_port *lp, int flag, int status,
1209 int (*func)(struct ifnet *, int))
1211 struct lagg_softc *sc = lp->lp_softc;
1212 struct ifnet *scifp = sc->sc_ifp;
1213 struct ifnet *ifp = lp->lp_ifp;
1216 LAGG_WLOCK_ASSERT(sc);
1218 status = status ? (scifp->if_flags & flag) : 0;
1219 /* Now "status" contains the flag value or 0 */
1222 * See if recorded ports status is different from what
1223 * we want it to be. If it is, flip it. We record ports
1224 * status in lp_ifflags so that we won't clear ports flag
1225 * we haven't set. In fact, we don't clear or set ports
1226 * flags directly, but get or release references to them.
1227 * That's why we can be sure that recorded flags still are
1228 * in accord with actual ports flags.
1230 if (status != (lp->lp_ifflags & flag)) {
1231 error = (*func)(ifp, status);
1234 lp->lp_ifflags &= ~flag;
1235 lp->lp_ifflags |= status;
1241 * Handle IFF_* flags that require certain changes on the lagg port
1242 * if "status" is true, update ports flags respective to the lagg
1243 * if "status" is false, forcedly clear the flags set on port.
1246 lagg_setflags(struct lagg_port *lp, int status)
1250 for (i = 0; lagg_pflags[i].flag; i++) {
1251 error = lagg_setflag(lp, lagg_pflags[i].flag,
1252 status, lagg_pflags[i].func);
1260 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1262 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1263 int error, len, mcast;
1265 len = m->m_pkthdr.len;
1266 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1269 /* We need a Tx algorithm and at least one port */
1270 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1277 ETHER_BPF_MTAP(ifp, m);
1279 error = (*sc->sc_start)(sc, m);
1284 ifp->if_omcasts += mcast;
1285 ifp->if_obytes += len;
1293 * The ifp->if_qflush entry point for lagg(4) is no-op.
1296 lagg_qflush(struct ifnet *ifp __unused)
1300 static struct mbuf *
1301 lagg_input(struct ifnet *ifp, struct mbuf *m)
1303 struct lagg_port *lp = ifp->if_lagg;
1304 struct lagg_softc *sc = lp->lp_softc;
1305 struct ifnet *scifp = sc->sc_ifp;
1308 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1309 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1310 sc->sc_proto == LAGG_PROTO_NONE) {
1316 ETHER_BPF_MTAP(scifp, m);
1318 m = (*sc->sc_input)(sc, lp, m);
1321 scifp->if_ipackets++;
1322 scifp->if_ibytes += m->m_pkthdr.len;
1324 if (scifp->if_flags & IFF_MONITOR) {
1335 lagg_media_change(struct ifnet *ifp)
1337 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1339 if (sc->sc_ifflags & IFF_DEBUG)
1340 printf("%s\n", __func__);
1347 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1349 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1350 struct lagg_port *lp;
1352 imr->ifm_status = IFM_AVALID;
1353 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1356 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1357 if (LAGG_PORTACTIVE(lp))
1358 imr->ifm_status |= IFM_ACTIVE;
1364 lagg_linkstate(struct lagg_softc *sc)
1366 struct lagg_port *lp;
1367 int new_link = LINK_STATE_DOWN;
1370 /* Our link is considered up if at least one of our ports is active */
1371 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1372 if (lp->lp_link_state == LINK_STATE_UP) {
1373 new_link = LINK_STATE_UP;
1377 if_link_state_change(sc->sc_ifp, new_link);
1379 /* Update if_baudrate to reflect the max possible speed */
1380 switch (sc->sc_proto) {
1381 case LAGG_PROTO_FAILOVER:
1382 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1383 sc->sc_primary->lp_ifp->if_baudrate : 0;
1385 case LAGG_PROTO_ROUNDROBIN:
1386 case LAGG_PROTO_LOADBALANCE:
1387 case LAGG_PROTO_ETHERCHANNEL:
1389 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1390 speed += lp->lp_ifp->if_baudrate;
1391 sc->sc_ifp->if_baudrate = speed;
1393 case LAGG_PROTO_LACP:
1394 /* LACP updates if_baudrate itself */
1400 lagg_port_state(struct ifnet *ifp, int state)
1402 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1403 struct lagg_softc *sc = NULL;
1412 if (sc->sc_linkstate != NULL)
1413 (*sc->sc_linkstate)(lp);
1418 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1420 struct lagg_port *lp_next, *rval = NULL;
1421 // int new_link = LINK_STATE_DOWN;
1423 LAGG_RLOCK_ASSERT(sc);
1425 * Search a port which reports an active link state.
1430 if (LAGG_PORTACTIVE(lp)) {
1434 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1435 LAGG_PORTACTIVE(lp_next)) {
1441 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1442 if (LAGG_PORTACTIVE(lp_next)) {
1451 * The IEEE 802.1D standard assumes that a lagg with
1452 * multiple ports is always full duplex. This is valid
1453 * for load sharing laggs and if at least two links
1454 * are active. Unfortunately, checking the latter would
1455 * be too expensive at this point.
1457 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1459 new_link = LINK_STATE_FULL_DUPLEX;
1461 new_link = rval->lp_link_state;
1469 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1471 if (m->m_pkthdr.len < (off + len)) {
1473 } else if (m->m_len < (off + len)) {
1474 m_copydata(m, off, len, buf);
1477 return (mtod(m, char *) + off);
1481 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1486 struct ether_header *eh;
1487 const struct ether_vlan_header *vlan;
1489 const struct ip *ip;
1490 const uint32_t *ports;
1494 const struct ip6_hdr *ip6;
1504 struct ether_vlan_header vlan;
1512 eh = mtod(m, struct ether_header *);
1513 etype = ntohs(eh->ether_type);
1514 if (sc->sc_flags & LAGG_F_HASHL2) {
1515 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1516 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1519 /* Special handling for encapsulating VLAN frames */
1520 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1521 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1522 sizeof(m->m_pkthdr.ether_vtag), p);
1523 } else if (etype == ETHERTYPE_VLAN) {
1524 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1528 if (sc->sc_flags & LAGG_F_HASHL2)
1529 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1530 etype = ntohs(vlan->evl_proto);
1531 off += sizeof(*vlan) - sizeof(*eh);
1537 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1541 if (sc->sc_flags & LAGG_F_HASHL3) {
1542 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1543 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1545 if (!(sc->sc_flags & LAGG_F_HASHL4))
1551 iphlen = ip->ip_hl << 2;
1552 if (iphlen < sizeof(*ip))
1555 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1558 p = hash32_buf(ports, sizeof(*ports), p);
1564 case ETHERTYPE_IPV6:
1565 if (!(sc->sc_flags & LAGG_F_HASHL3))
1567 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1571 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1572 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1573 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1574 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1583 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1586 return (ifp->if_transmit)(ifp, m);
1590 * Simple round robin aggregation
1594 lagg_rr_attach(struct lagg_softc *sc)
1596 sc->sc_detach = lagg_rr_detach;
1597 sc->sc_start = lagg_rr_start;
1598 sc->sc_input = lagg_rr_input;
1599 sc->sc_port_create = NULL;
1600 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1607 lagg_rr_detach(struct lagg_softc *sc)
1613 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1615 struct lagg_port *lp;
1618 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1620 lp = SLIST_FIRST(&sc->sc_ports);
1622 lp = SLIST_NEXT(lp, lp_entries);
1625 * Check the port's link state. This will return the next active
1626 * port if the link is down or the port is NULL.
1628 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1634 return (lagg_enqueue(lp->lp_ifp, m));
1637 static struct mbuf *
1638 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1640 struct ifnet *ifp = sc->sc_ifp;
1642 /* Just pass in the packet to our lagg device */
1643 m->m_pkthdr.rcvif = ifp;
1653 lagg_fail_attach(struct lagg_softc *sc)
1655 sc->sc_detach = lagg_fail_detach;
1656 sc->sc_start = lagg_fail_start;
1657 sc->sc_input = lagg_fail_input;
1658 sc->sc_port_create = NULL;
1659 sc->sc_port_destroy = NULL;
1665 lagg_fail_detach(struct lagg_softc *sc)
1671 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1673 struct lagg_port *lp;
1675 /* Use the master port if active or the next available port */
1676 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1682 return (lagg_enqueue(lp->lp_ifp, m));
1685 static struct mbuf *
1686 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1688 struct ifnet *ifp = sc->sc_ifp;
1689 struct lagg_port *tmp_tp;
1691 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1692 m->m_pkthdr.rcvif = ifp;
1696 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1697 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1699 * If tmp_tp is null, we've recieved a packet when all
1700 * our links are down. Weird, but process it anyways.
1702 if ((tmp_tp == NULL || tmp_tp == lp)) {
1703 m->m_pkthdr.rcvif = ifp;
1717 lagg_lb_attach(struct lagg_softc *sc)
1719 struct lagg_port *lp;
1722 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1723 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1726 sc->sc_detach = lagg_lb_detach;
1727 sc->sc_start = lagg_lb_start;
1728 sc->sc_input = lagg_lb_input;
1729 sc->sc_port_create = lagg_lb_port_create;
1730 sc->sc_port_destroy = lagg_lb_port_destroy;
1731 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1733 lb->lb_key = arc4random();
1734 sc->sc_psc = (caddr_t)lb;
1736 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1737 lagg_lb_port_create(lp);
1743 lagg_lb_detach(struct lagg_softc *sc)
1745 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1752 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1754 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1755 struct lagg_port *lp_next;
1758 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1759 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1762 if (i >= LAGG_MAX_PORTS)
1764 if (sc->sc_ifflags & IFF_DEBUG)
1765 printf("%s: port %s at index %d\n",
1766 sc->sc_ifname, lp_next->lp_ifname, i);
1767 lb->lb_ports[i++] = lp_next;
1774 lagg_lb_port_create(struct lagg_port *lp)
1776 struct lagg_softc *sc = lp->lp_softc;
1777 return (lagg_lb_porttable(sc, NULL));
1781 lagg_lb_port_destroy(struct lagg_port *lp)
1783 struct lagg_softc *sc = lp->lp_softc;
1784 lagg_lb_porttable(sc, lp);
1788 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1790 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1791 struct lagg_port *lp = NULL;
1794 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1795 p = m->m_pkthdr.flowid;
1797 p = lagg_hashmbuf(sc, m, lb->lb_key);
1799 lp = lb->lb_ports[p];
1802 * Check the port's link state. This will return the next active
1803 * port if the link is down or the port is NULL.
1805 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1811 return (lagg_enqueue(lp->lp_ifp, m));
1814 static struct mbuf *
1815 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1817 struct ifnet *ifp = sc->sc_ifp;
1819 /* Just pass in the packet to our lagg device */
1820 m->m_pkthdr.rcvif = ifp;
1830 lagg_lacp_attach(struct lagg_softc *sc)
1832 struct lagg_port *lp;
1835 sc->sc_detach = lagg_lacp_detach;
1836 sc->sc_port_create = lacp_port_create;
1837 sc->sc_port_destroy = lacp_port_destroy;
1838 sc->sc_linkstate = lacp_linkstate;
1839 sc->sc_start = lagg_lacp_start;
1840 sc->sc_input = lagg_lacp_input;
1841 sc->sc_init = lacp_init;
1842 sc->sc_stop = lacp_stop;
1843 sc->sc_lladdr = lagg_lacp_lladdr;
1844 sc->sc_req = lacp_req;
1845 sc->sc_portreq = lacp_portreq;
1847 error = lacp_attach(sc);
1851 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1852 lacp_port_create(lp);
1858 lagg_lacp_detach(struct lagg_softc *sc)
1860 struct lagg_port *lp;
1863 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1864 lacp_port_destroy(lp);
1866 /* unlocking is safe here */
1868 error = lacp_detach(sc);
1875 lagg_lacp_lladdr(struct lagg_softc *sc)
1877 struct lagg_port *lp;
1879 /* purge all the lacp ports */
1880 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1881 lacp_port_destroy(lp);
1883 /* add them back in */
1884 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1885 lacp_port_create(lp);
1889 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1891 struct lagg_port *lp;
1893 lp = lacp_select_tx_port(sc, m);
1900 return (lagg_enqueue(lp->lp_ifp, m));
1903 static struct mbuf *
1904 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1906 struct ifnet *ifp = sc->sc_ifp;
1907 struct ether_header *eh;
1910 eh = mtod(m, struct ether_header *);
1911 etype = ntohs(eh->ether_type);
1913 /* Tap off LACP control messages */
1914 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1915 m = lacp_input(lp, m);
1921 * If the port is not collecting or not in the active aggregator then
1924 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1929 m->m_pkthdr.rcvif = ifp;