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>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip.h>
63 #include <netinet/ip6.h>
66 #include <net/if_vlan_var.h>
67 #include <net/if_lagg.h>
68 #include <net/ieee8023ad_lacp.h>
70 /* Special flags we should propagate to the lagg ports. */
73 int (*func)(struct ifnet *, int);
75 {IFF_PROMISC, ifpromisc},
76 {IFF_ALLMULTI, if_allmulti},
80 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
81 static struct mtx lagg_list_mtx;
82 eventhandler_tag lagg_detach_cookie = NULL;
84 static int lagg_clone_create(struct if_clone *, int, caddr_t);
85 static void lagg_clone_destroy(struct ifnet *);
86 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
87 static void lagg_capabilities(struct lagg_softc *);
88 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
89 static void lagg_port_setlladdr(void *, int);
90 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
91 static int lagg_port_destroy(struct lagg_port *, int);
92 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
93 static void lagg_linkstate(struct lagg_softc *);
94 static void lagg_port_state(struct ifnet *, int);
95 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
96 static int lagg_port_output(struct ifnet *, struct mbuf *,
97 struct sockaddr *, struct route *);
98 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
99 static int lagg_port_checkstacking(struct lagg_softc *);
100 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
101 static void lagg_init(void *);
102 static void lagg_stop(struct lagg_softc *);
103 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
104 static int lagg_ether_setmulti(struct lagg_softc *);
105 static int lagg_ether_cmdmulti(struct lagg_port *, int);
106 static int lagg_setflag(struct lagg_port *, int, int,
107 int (*func)(struct ifnet *, int));
108 static int lagg_setflags(struct lagg_port *, int status);
109 static void lagg_start(struct ifnet *);
110 static int lagg_media_change(struct ifnet *);
111 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
112 static struct lagg_port *lagg_link_active(struct lagg_softc *,
114 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
116 IFC_SIMPLE_DECLARE(lagg, 0);
118 /* Simple round robin */
119 static int lagg_rr_attach(struct lagg_softc *);
120 static int lagg_rr_detach(struct lagg_softc *);
121 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
122 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
125 /* Active failover */
126 static int lagg_fail_attach(struct lagg_softc *);
127 static int lagg_fail_detach(struct lagg_softc *);
128 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
129 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
133 static int lagg_lb_attach(struct lagg_softc *);
134 static int lagg_lb_detach(struct lagg_softc *);
135 static int lagg_lb_port_create(struct lagg_port *);
136 static void lagg_lb_port_destroy(struct lagg_port *);
137 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
138 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
140 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
143 static int lagg_lacp_attach(struct lagg_softc *);
144 static int lagg_lacp_detach(struct lagg_softc *);
145 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
146 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
148 static void lagg_lacp_lladdr(struct lagg_softc *);
150 /* lagg protocol table */
151 static const struct {
153 int (*ti_attach)(struct lagg_softc *);
155 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
156 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
157 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
158 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
159 { LAGG_PROTO_LACP, lagg_lacp_attach },
160 { LAGG_PROTO_NONE, NULL }
163 SYSCTL_DECL(_net_link);
164 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, "Link Aggregation");
166 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
167 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
168 &lagg_failover_rx_all, 0,
169 "Accept input from any interface in a failover lagg");
170 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
171 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
172 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
174 "Default setting for using flow id for load sharing");
177 lagg_modevent(module_t mod, int type, void *data)
182 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
183 SLIST_INIT(&lagg_list);
184 if_clone_attach(&lagg_cloner);
185 lagg_input_p = lagg_input;
186 lagg_linkstate_p = lagg_port_state;
187 lagg_detach_cookie = EVENTHANDLER_REGISTER(
188 ifnet_departure_event, lagg_port_ifdetach, NULL,
189 EVENTHANDLER_PRI_ANY);
192 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
194 if_clone_detach(&lagg_cloner);
196 lagg_linkstate_p = NULL;
197 mtx_destroy(&lagg_list_mtx);
205 static moduledata_t lagg_mod = {
211 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
212 MODULE_VERSION(if_lagg, 1);
214 #if __FreeBSD_version >= 800000
216 * This routine is run via an vlan
220 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
222 struct lagg_softc *sc = ifp->if_softc;
223 struct lagg_port *lp;
225 if (ifp->if_softc != arg) /* Not our event */
229 if (!SLIST_EMPTY(&sc->sc_ports)) {
230 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
231 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
237 * This routine is run via an vlan
241 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
243 struct lagg_softc *sc = ifp->if_softc;
244 struct lagg_port *lp;
246 if (ifp->if_softc != arg) /* Not our event */
250 if (!SLIST_EMPTY(&sc->sc_ports)) {
251 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
252 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
259 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
261 struct lagg_softc *sc;
264 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
265 struct sysctl_oid *oid;
266 char num[14]; /* sufficient for 32 bits */
268 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
269 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
275 sysctl_ctx_init(&sc->ctx);
276 snprintf(num, sizeof(num), "%u", unit);
277 sc->use_flowid = def_use_flowid;
278 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
279 OID_AUTO, num, CTLFLAG_RD, NULL, "");
280 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
281 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
282 "Use flow id for load sharing");
284 sc->sc_proto = LAGG_PROTO_NONE;
285 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
286 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
287 sc->sc_proto = lagg_protos[i].ti_proto;
288 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
289 if_free_type(ifp, IFT_ETHER);
297 SLIST_INIT(&sc->sc_ports);
298 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
300 /* Initialise pseudo media types */
301 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
303 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
304 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
306 if_initname(ifp, ifc->ifc_name, unit);
307 ifp->if_type = IFT_ETHER;
309 ifp->if_start = lagg_start;
310 ifp->if_init = lagg_init;
311 ifp->if_ioctl = lagg_ioctl;
312 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
314 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
315 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
316 IFQ_SET_READY(&ifp->if_snd);
319 * Attach as an ordinary ethernet device, childs will be attached
320 * as special device IFT_IEEE8023ADLAG.
322 ether_ifattach(ifp, eaddr);
324 #if __FreeBSD_version >= 800000
325 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
326 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
327 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
328 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
331 /* Insert into the global list of laggs */
332 mtx_lock(&lagg_list_mtx);
333 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
334 mtx_unlock(&lagg_list_mtx);
340 lagg_clone_destroy(struct ifnet *ifp)
342 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
343 struct lagg_port *lp;
348 ifp->if_flags &= ~IFF_UP;
350 #if __FreeBSD_version >= 800000
351 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
352 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
355 /* Shutdown and remove lagg ports */
356 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
357 lagg_port_destroy(lp, 1);
358 /* Unhook the aggregation protocol */
359 (*sc->sc_detach)(sc);
363 sysctl_ctx_free(&sc->ctx);
364 ifmedia_removeall(&sc->sc_media);
366 if_free_type(ifp, IFT_ETHER);
368 mtx_lock(&lagg_list_mtx);
369 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
370 mtx_unlock(&lagg_list_mtx);
372 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
373 LAGG_LOCK_DESTROY(sc);
378 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
380 struct ifnet *ifp = sc->sc_ifp;
382 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
385 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
386 /* Let the protocol know the MAC has changed */
387 if (sc->sc_lladdr != NULL)
388 (*sc->sc_lladdr)(sc);
389 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
393 lagg_capabilities(struct lagg_softc *sc)
395 struct lagg_port *lp;
396 int cap = ~0, ena = ~0;
399 LAGG_WLOCK_ASSERT(sc);
401 /* Get capabilities from the lagg ports */
402 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
403 cap &= lp->lp_ifp->if_capabilities;
404 ena &= lp->lp_ifp->if_capenable;
405 hwa &= lp->lp_ifp->if_hwassist;
407 cap = (cap == ~0 ? 0 : cap);
408 ena = (ena == ~0 ? 0 : ena);
409 hwa = (hwa == ~0 ? 0 : hwa);
411 if (sc->sc_ifp->if_capabilities != cap ||
412 sc->sc_ifp->if_capenable != ena ||
413 sc->sc_ifp->if_hwassist != hwa) {
414 sc->sc_ifp->if_capabilities = cap;
415 sc->sc_ifp->if_capenable = ena;
416 sc->sc_ifp->if_hwassist = hwa;
417 getmicrotime(&sc->sc_ifp->if_lastchange);
419 if (sc->sc_ifflags & IFF_DEBUG)
420 if_printf(sc->sc_ifp,
421 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
426 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
428 struct lagg_softc *sc = lp->lp_softc;
429 struct ifnet *ifp = lp->lp_ifp;
430 struct lagg_llq *llq;
433 LAGG_WLOCK_ASSERT(sc);
435 if (lp->lp_detaching ||
436 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
439 /* Check to make sure its not already queued to be changed */
440 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
441 if (llq->llq_ifp == ifp) {
448 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
449 if (llq == NULL) /* XXX what to do */
453 /* Update the lladdr even if pending, it may have changed */
455 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
458 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
460 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
464 * Set the interface MAC address from a taskqueue to avoid a LOR.
467 lagg_port_setlladdr(void *arg, int pending)
469 struct lagg_softc *sc = (struct lagg_softc *)arg;
470 struct lagg_llq *llq, *head;
474 /* Grab a local reference of the queue and remove it from the softc */
476 head = SLIST_FIRST(&sc->sc_llq_head);
477 SLIST_FIRST(&sc->sc_llq_head) = NULL;
481 * Traverse the queue and set the lladdr on each ifp. It is safe to do
482 * unlocked as we have the only reference to it.
484 for (llq = head; llq != NULL; llq = head) {
487 /* Set the link layer address */
488 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
490 printf("%s: setlladdr failed on %s\n", __func__,
493 head = SLIST_NEXT(llq, llq_entries);
499 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
501 struct lagg_softc *sc_ptr;
502 struct lagg_port *lp;
505 LAGG_WLOCK_ASSERT(sc);
507 /* Limit the maximal number of lagg ports */
508 if (sc->sc_count >= LAGG_MAX_PORTS)
511 /* Check if port has already been associated to a lagg */
512 if (ifp->if_lagg != NULL)
515 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
516 if (ifp->if_type != IFT_ETHER)
517 return (EPROTONOSUPPORT);
519 /* Allow the first Ethernet member to define the MTU */
520 if (SLIST_EMPTY(&sc->sc_ports))
521 sc->sc_ifp->if_mtu = ifp->if_mtu;
522 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
523 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
528 if ((lp = malloc(sizeof(struct lagg_port),
529 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
532 /* Check if port is a stacked lagg */
533 mtx_lock(&lagg_list_mtx);
534 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
535 if (ifp == sc_ptr->sc_ifp) {
536 mtx_unlock(&lagg_list_mtx);
539 /* XXX disable stacking for the moment, its untested
540 lp->lp_flags |= LAGG_PORT_STACK;
541 if (lagg_port_checkstacking(sc_ptr) >=
543 mtx_unlock(&lagg_list_mtx);
550 mtx_unlock(&lagg_list_mtx);
552 /* Change the interface type */
553 lp->lp_iftype = ifp->if_type;
554 ifp->if_type = IFT_IEEE8023ADLAG;
556 lp->lp_ioctl = ifp->if_ioctl;
557 ifp->if_ioctl = lagg_port_ioctl;
558 lp->lp_output = ifp->if_output;
559 ifp->if_output = lagg_port_output;
564 /* Save port link layer address */
565 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
567 if (SLIST_EMPTY(&sc->sc_ports)) {
569 lagg_lladdr(sc, IF_LLADDR(ifp));
571 /* Update link layer address for this port */
572 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
575 /* Insert into the list of ports */
576 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
579 /* Update lagg capabilities */
580 lagg_capabilities(sc);
583 /* Add multicast addresses and interface flags to this port */
584 lagg_ether_cmdmulti(lp, 1);
585 lagg_setflags(lp, 1);
587 if (sc->sc_port_create != NULL)
588 error = (*sc->sc_port_create)(lp);
590 /* remove the port again, without calling sc_port_destroy */
591 lagg_port_destroy(lp, 0);
599 lagg_port_checkstacking(struct lagg_softc *sc)
601 struct lagg_softc *sc_ptr;
602 struct lagg_port *lp;
605 LAGG_WLOCK_ASSERT(sc);
607 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
608 if (lp->lp_flags & LAGG_PORT_STACK) {
609 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
610 m = MAX(m, lagg_port_checkstacking(sc_ptr));
618 lagg_port_destroy(struct lagg_port *lp, int runpd)
620 struct lagg_softc *sc = lp->lp_softc;
621 struct lagg_port *lp_ptr;
622 struct lagg_llq *llq;
623 struct ifnet *ifp = lp->lp_ifp;
625 LAGG_WLOCK_ASSERT(sc);
627 if (runpd && sc->sc_port_destroy != NULL)
628 (*sc->sc_port_destroy)(lp);
631 * Remove multicast addresses and interface flags from this port and
632 * reset the MAC address, skip if the interface is being detached.
634 if (!lp->lp_detaching) {
635 lagg_ether_cmdmulti(lp, 0);
636 lagg_setflags(lp, 0);
637 lagg_port_lladdr(lp, lp->lp_lladdr);
640 /* Restore interface */
641 ifp->if_type = lp->lp_iftype;
642 ifp->if_ioctl = lp->lp_ioctl;
643 ifp->if_output = lp->lp_output;
646 /* Finally, remove the port from the lagg */
647 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
650 /* Update the primary interface */
651 if (lp == sc->sc_primary) {
652 uint8_t lladdr[ETHER_ADDR_LEN];
654 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
655 bzero(&lladdr, ETHER_ADDR_LEN);
657 bcopy(lp_ptr->lp_lladdr,
658 lladdr, ETHER_ADDR_LEN);
660 lagg_lladdr(sc, lladdr);
661 sc->sc_primary = lp_ptr;
663 /* Update link layer address for each port */
664 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
665 lagg_port_lladdr(lp_ptr, lladdr);
668 /* Remove any pending lladdr changes from the queue */
669 if (lp->lp_detaching) {
670 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
671 if (llq->llq_ifp == ifp) {
672 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
675 break; /* Only appears once */
681 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
685 /* Update lagg capabilities */
686 lagg_capabilities(sc);
693 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
695 struct lagg_reqport *rp = (struct lagg_reqport *)data;
696 struct lagg_softc *sc;
697 struct lagg_port *lp = NULL;
700 /* Should be checked by the caller */
701 if (ifp->if_type != IFT_IEEE8023ADLAG ||
702 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
707 if (rp->rp_portname[0] == '\0' ||
708 ifunit(rp->rp_portname) != ifp) {
714 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
720 lagg_port2req(lp, rp);
725 if (lp->lp_ioctl == NULL) {
729 error = (*lp->lp_ioctl)(ifp, cmd, data);
733 /* Update lagg interface capabilities */
735 lagg_capabilities(sc);
740 /* Do not allow the MTU to be changed once joined */
751 if (lp->lp_ioctl != NULL)
752 return ((*lp->lp_ioctl)(ifp, cmd, data));
758 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
759 struct sockaddr *dst, struct route *ro)
761 struct lagg_port *lp = ifp->if_lagg;
762 struct ether_header *eh;
765 switch (dst->sa_family) {
766 case pseudo_AF_HDRCMPLT:
768 eh = (struct ether_header *)dst->sa_data;
769 type = eh->ether_type;
774 * Only allow ethernet types required to initiate or maintain the link,
775 * aggregated frames take a different path.
777 switch (ntohs(type)) {
778 case ETHERTYPE_PAE: /* EAPOL PAE/802.1x */
779 return ((*lp->lp_output)(ifp, m, dst, ro));
782 /* drop any other frames */
788 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
790 struct lagg_port *lp;
791 struct lagg_softc *sc;
793 if ((lp = ifp->if_lagg) == NULL)
799 lp->lp_detaching = 1;
800 lagg_port_destroy(lp, 1);
805 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
807 struct lagg_softc *sc = lp->lp_softc;
809 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
810 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
811 rp->rp_prio = lp->lp_prio;
812 rp->rp_flags = lp->lp_flags;
813 if (sc->sc_portreq != NULL)
814 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
816 /* Add protocol specific flags */
817 switch (sc->sc_proto) {
818 case LAGG_PROTO_FAILOVER:
819 if (lp == sc->sc_primary)
820 rp->rp_flags |= LAGG_PORT_MASTER;
821 if (lp == lagg_link_active(sc, sc->sc_primary))
822 rp->rp_flags |= LAGG_PORT_ACTIVE;
825 case LAGG_PROTO_ROUNDROBIN:
826 case LAGG_PROTO_LOADBALANCE:
827 case LAGG_PROTO_ETHERCHANNEL:
828 if (LAGG_PORTACTIVE(lp))
829 rp->rp_flags |= LAGG_PORT_ACTIVE;
832 case LAGG_PROTO_LACP:
833 /* LACP has a different definition of active */
834 if (lacp_isactive(lp))
835 rp->rp_flags |= LAGG_PORT_ACTIVE;
836 if (lacp_iscollecting(lp))
837 rp->rp_flags |= LAGG_PORT_COLLECTING;
838 if (lacp_isdistributing(lp))
839 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
848 struct lagg_softc *sc = (struct lagg_softc *)xsc;
849 struct lagg_port *lp;
850 struct ifnet *ifp = sc->sc_ifp;
852 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
857 ifp->if_drv_flags |= IFF_DRV_RUNNING;
858 /* Update the port lladdrs */
859 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
860 lagg_port_lladdr(lp, IF_LLADDR(ifp));
862 if (sc->sc_init != NULL)
869 lagg_stop(struct lagg_softc *sc)
871 struct ifnet *ifp = sc->sc_ifp;
873 LAGG_WLOCK_ASSERT(sc);
875 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
878 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
880 if (sc->sc_stop != NULL)
885 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
887 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
888 struct lagg_reqall *ra = (struct lagg_reqall *)data;
889 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
890 struct ifreq *ifr = (struct ifreq *)data;
891 struct lagg_port *lp;
893 struct thread *td = curthread;
895 int count, buflen, len, error = 0;
897 bzero(&rpbuf, sizeof(rpbuf));
903 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
905 buflen = count * sizeof(struct lagg_reqport);
908 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
911 ra->ra_proto = sc->sc_proto;
912 if (sc->sc_req != NULL)
913 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
917 len = min(ra->ra_size, buflen);
918 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
919 if (len < sizeof(rpbuf))
922 lagg_port2req(lp, &rpbuf);
923 memcpy(buf, &rpbuf, sizeof(rpbuf));
925 buf += sizeof(rpbuf);
926 len -= sizeof(rpbuf);
929 ra->ra_ports = count;
930 ra->ra_size = count * sizeof(rpbuf);
931 error = copyout(outbuf, ra->ra_port, ra->ra_size);
932 free(outbuf, M_TEMP);
935 error = priv_check(td, PRIV_NET_LAGG);
938 if (ra->ra_proto >= LAGG_PROTO_MAX) {
939 error = EPROTONOSUPPORT;
942 if (sc->sc_proto != LAGG_PROTO_NONE) {
944 error = sc->sc_detach(sc);
945 /* Reset protocol and pointers */
946 sc->sc_proto = LAGG_PROTO_NONE;
947 sc->sc_detach = NULL;
950 sc->sc_port_create = NULL;
951 sc->sc_port_destroy = NULL;
952 sc->sc_linkstate = NULL;
955 sc->sc_lladdr = NULL;
957 sc->sc_portreq = NULL;
962 for (int i = 0; i < (sizeof(lagg_protos) /
963 sizeof(lagg_protos[0])); i++) {
964 if (lagg_protos[i].ti_proto == ra->ra_proto) {
965 if (sc->sc_ifflags & IFF_DEBUG)
966 printf("%s: using proto %u\n",
968 lagg_protos[i].ti_proto);
970 sc->sc_proto = lagg_protos[i].ti_proto;
971 if (sc->sc_proto != LAGG_PROTO_NONE)
972 error = lagg_protos[i].ti_attach(sc);
977 error = EPROTONOSUPPORT;
980 if (rp->rp_portname[0] == '\0' ||
981 (tpif = ifunit(rp->rp_portname)) == NULL) {
987 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
988 lp->lp_softc != sc) {
994 lagg_port2req(lp, rp);
998 error = priv_check(td, PRIV_NET_LAGG);
1001 if (rp->rp_portname[0] == '\0' ||
1002 (tpif = ifunit(rp->rp_portname)) == NULL) {
1007 error = lagg_port_create(sc, tpif);
1010 case SIOCSLAGGDELPORT:
1011 error = priv_check(td, PRIV_NET_LAGG);
1014 if (rp->rp_portname[0] == '\0' ||
1015 (tpif = ifunit(rp->rp_portname)) == NULL) {
1021 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1022 lp->lp_softc != sc) {
1028 error = lagg_port_destroy(lp, 1);
1032 /* Set flags on ports too */
1034 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1035 lagg_setflags(lp, 1);
1039 if (!(ifp->if_flags & IFF_UP) &&
1040 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1042 * If interface is marked down and it is running,
1043 * then stop and disable it.
1048 } else if ((ifp->if_flags & IFF_UP) &&
1049 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1051 * If interface is marked up and it is stopped, then
1054 (*ifp->if_init)(sc);
1060 error = lagg_ether_setmulti(sc);
1065 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1070 /* Do not allow the MTU or caps to be directly changed */
1075 error = ether_ioctl(ifp, cmd, data);
1082 lagg_ether_setmulti(struct lagg_softc *sc)
1084 struct lagg_port *lp;
1086 LAGG_WLOCK_ASSERT(sc);
1088 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1089 /* First, remove any existing filter entries. */
1090 lagg_ether_cmdmulti(lp, 0);
1091 /* copy all addresses from the lagg interface to the port */
1092 lagg_ether_cmdmulti(lp, 1);
1098 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1100 struct lagg_softc *sc = lp->lp_softc;
1101 struct ifnet *ifp = lp->lp_ifp;
1102 struct ifnet *scifp = sc->sc_ifp;
1104 struct ifmultiaddr *ifma, *rifma = NULL;
1105 struct sockaddr_dl sdl;
1108 LAGG_WLOCK_ASSERT(sc);
1110 bzero((char *)&sdl, sizeof(sdl));
1111 sdl.sdl_len = sizeof(sdl);
1112 sdl.sdl_family = AF_LINK;
1113 sdl.sdl_type = IFT_ETHER;
1114 sdl.sdl_alen = ETHER_ADDR_LEN;
1115 sdl.sdl_index = ifp->if_index;
1118 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1119 if (ifma->ifma_addr->sa_family != AF_LINK)
1121 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1122 LLADDR(&sdl), ETHER_ADDR_LEN);
1124 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1127 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1130 mc->mc_ifma = rifma;
1131 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1134 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1135 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1136 if_delmulti_ifma(mc->mc_ifma);
1143 /* Handle a ref counted flag that should be set on the lagg port as well */
1145 lagg_setflag(struct lagg_port *lp, int flag, int status,
1146 int (*func)(struct ifnet *, int))
1148 struct lagg_softc *sc = lp->lp_softc;
1149 struct ifnet *scifp = sc->sc_ifp;
1150 struct ifnet *ifp = lp->lp_ifp;
1153 LAGG_WLOCK_ASSERT(sc);
1155 status = status ? (scifp->if_flags & flag) : 0;
1156 /* Now "status" contains the flag value or 0 */
1159 * See if recorded ports status is different from what
1160 * we want it to be. If it is, flip it. We record ports
1161 * status in lp_ifflags so that we won't clear ports flag
1162 * we haven't set. In fact, we don't clear or set ports
1163 * flags directly, but get or release references to them.
1164 * That's why we can be sure that recorded flags still are
1165 * in accord with actual ports flags.
1167 if (status != (lp->lp_ifflags & flag)) {
1168 error = (*func)(ifp, status);
1171 lp->lp_ifflags &= ~flag;
1172 lp->lp_ifflags |= status;
1178 * Handle IFF_* flags that require certain changes on the lagg port
1179 * if "status" is true, update ports flags respective to the lagg
1180 * if "status" is false, forcedly clear the flags set on port.
1183 lagg_setflags(struct lagg_port *lp, int status)
1187 for (i = 0; lagg_pflags[i].flag; i++) {
1188 error = lagg_setflag(lp, lagg_pflags[i].flag,
1189 status, lagg_pflags[i].func);
1197 lagg_start(struct ifnet *ifp)
1199 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1204 /* We need a Tx algorithm and at least one port */
1205 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1206 IF_DRAIN(&ifp->if_snd);
1211 for (;; error = 0) {
1212 IFQ_DEQUEUE(&ifp->if_snd, m);
1216 ETHER_BPF_MTAP(ifp, m);
1218 error = (*sc->sc_start)(sc, m);
1227 static struct mbuf *
1228 lagg_input(struct ifnet *ifp, struct mbuf *m)
1230 struct lagg_port *lp = ifp->if_lagg;
1231 struct lagg_softc *sc = lp->lp_softc;
1232 struct ifnet *scifp = sc->sc_ifp;
1235 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1236 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1237 sc->sc_proto == LAGG_PROTO_NONE) {
1243 ETHER_BPF_MTAP(scifp, m);
1245 m = (*sc->sc_input)(sc, lp, m);
1248 scifp->if_ipackets++;
1249 scifp->if_ibytes += m->m_pkthdr.len;
1251 if (scifp->if_flags & IFF_MONITOR) {
1262 lagg_media_change(struct ifnet *ifp)
1264 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1266 if (sc->sc_ifflags & IFF_DEBUG)
1267 printf("%s\n", __func__);
1274 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1276 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1277 struct lagg_port *lp;
1279 imr->ifm_status = IFM_AVALID;
1280 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1283 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1284 if (LAGG_PORTACTIVE(lp))
1285 imr->ifm_status |= IFM_ACTIVE;
1291 lagg_linkstate(struct lagg_softc *sc)
1293 struct lagg_port *lp;
1294 int new_link = LINK_STATE_DOWN;
1297 /* Our link is considered up if at least one of our ports is active */
1298 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1299 if (lp->lp_link_state == LINK_STATE_UP) {
1300 new_link = LINK_STATE_UP;
1304 if_link_state_change(sc->sc_ifp, new_link);
1306 /* Update if_baudrate to reflect the max possible speed */
1307 switch (sc->sc_proto) {
1308 case LAGG_PROTO_FAILOVER:
1309 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1310 sc->sc_primary->lp_ifp->if_baudrate : 0;
1312 case LAGG_PROTO_ROUNDROBIN:
1313 case LAGG_PROTO_LOADBALANCE:
1314 case LAGG_PROTO_ETHERCHANNEL:
1316 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1317 speed += lp->lp_ifp->if_baudrate;
1318 sc->sc_ifp->if_baudrate = speed;
1320 case LAGG_PROTO_LACP:
1321 /* LACP updates if_baudrate itself */
1327 lagg_port_state(struct ifnet *ifp, int state)
1329 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1330 struct lagg_softc *sc = NULL;
1339 if (sc->sc_linkstate != NULL)
1340 (*sc->sc_linkstate)(lp);
1345 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1347 struct lagg_port *lp_next, *rval = NULL;
1348 // int new_link = LINK_STATE_DOWN;
1350 LAGG_RLOCK_ASSERT(sc);
1352 * Search a port which reports an active link state.
1357 if (LAGG_PORTACTIVE(lp)) {
1361 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1362 LAGG_PORTACTIVE(lp_next)) {
1368 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1369 if (LAGG_PORTACTIVE(lp_next)) {
1378 * The IEEE 802.1D standard assumes that a lagg with
1379 * multiple ports is always full duplex. This is valid
1380 * for load sharing laggs and if at least two links
1381 * are active. Unfortunately, checking the latter would
1382 * be too expensive at this point.
1384 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1386 new_link = LINK_STATE_FULL_DUPLEX;
1388 new_link = rval->lp_link_state;
1396 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1398 if (m->m_pkthdr.len < (off + len)) {
1400 } else if (m->m_len < (off + len)) {
1401 m_copydata(m, off, len, buf);
1404 return (mtod(m, char *) + off);
1408 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1413 struct ether_header *eh;
1414 struct ether_vlan_header vlanbuf;
1415 const struct ether_vlan_header *vlan;
1417 const struct ip *ip;
1421 const struct ip6_hdr *ip6;
1422 struct ip6_hdr ip6buf;
1429 eh = mtod(m, struct ether_header *);
1430 etype = ntohs(eh->ether_type);
1431 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1432 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1434 /* Special handling for encapsulating VLAN frames */
1435 if (m->m_flags & M_VLANTAG) {
1436 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1437 sizeof(m->m_pkthdr.ether_vtag), p);
1438 } else if (etype == ETHERTYPE_VLAN) {
1439 vlan = lagg_gethdr(m, off, sizeof(*vlan), &vlanbuf);
1443 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1444 etype = ntohs(vlan->evl_proto);
1445 off += sizeof(*vlan) - sizeof(*eh);
1451 ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1455 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1456 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1460 case ETHERTYPE_IPV6:
1461 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1465 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1466 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1467 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1468 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1477 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1480 return (ifp->if_transmit)(ifp, m);
1484 * Simple round robin aggregation
1488 lagg_rr_attach(struct lagg_softc *sc)
1490 sc->sc_detach = lagg_rr_detach;
1491 sc->sc_start = lagg_rr_start;
1492 sc->sc_input = lagg_rr_input;
1493 sc->sc_port_create = NULL;
1494 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1501 lagg_rr_detach(struct lagg_softc *sc)
1507 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1509 struct lagg_port *lp;
1512 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1514 lp = SLIST_FIRST(&sc->sc_ports);
1516 lp = SLIST_NEXT(lp, lp_entries);
1519 * Check the port's link state. This will return the next active
1520 * port if the link is down or the port is NULL.
1522 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1528 return (lagg_enqueue(lp->lp_ifp, m));
1531 static struct mbuf *
1532 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1534 struct ifnet *ifp = sc->sc_ifp;
1536 /* Just pass in the packet to our lagg device */
1537 m->m_pkthdr.rcvif = ifp;
1547 lagg_fail_attach(struct lagg_softc *sc)
1549 sc->sc_detach = lagg_fail_detach;
1550 sc->sc_start = lagg_fail_start;
1551 sc->sc_input = lagg_fail_input;
1552 sc->sc_port_create = NULL;
1553 sc->sc_port_destroy = NULL;
1559 lagg_fail_detach(struct lagg_softc *sc)
1565 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1567 struct lagg_port *lp;
1569 /* Use the master port if active or the next available port */
1570 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1576 return (lagg_enqueue(lp->lp_ifp, m));
1579 static struct mbuf *
1580 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1582 struct ifnet *ifp = sc->sc_ifp;
1583 struct lagg_port *tmp_tp;
1585 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1586 m->m_pkthdr.rcvif = ifp;
1590 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1591 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1593 * If tmp_tp is null, we've recieved a packet when all
1594 * our links are down. Weird, but process it anyways.
1596 if ((tmp_tp == NULL || tmp_tp == lp)) {
1597 m->m_pkthdr.rcvif = ifp;
1611 lagg_lb_attach(struct lagg_softc *sc)
1613 struct lagg_port *lp;
1616 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1617 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1620 sc->sc_detach = lagg_lb_detach;
1621 sc->sc_start = lagg_lb_start;
1622 sc->sc_input = lagg_lb_input;
1623 sc->sc_port_create = lagg_lb_port_create;
1624 sc->sc_port_destroy = lagg_lb_port_destroy;
1625 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1627 lb->lb_key = arc4random();
1628 sc->sc_psc = (caddr_t)lb;
1630 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1631 lagg_lb_port_create(lp);
1637 lagg_lb_detach(struct lagg_softc *sc)
1639 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1646 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1648 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1649 struct lagg_port *lp_next;
1652 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1653 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1656 if (i >= LAGG_MAX_PORTS)
1658 if (sc->sc_ifflags & IFF_DEBUG)
1659 printf("%s: port %s at index %d\n",
1660 sc->sc_ifname, lp_next->lp_ifname, i);
1661 lb->lb_ports[i++] = lp_next;
1668 lagg_lb_port_create(struct lagg_port *lp)
1670 struct lagg_softc *sc = lp->lp_softc;
1671 return (lagg_lb_porttable(sc, NULL));
1675 lagg_lb_port_destroy(struct lagg_port *lp)
1677 struct lagg_softc *sc = lp->lp_softc;
1678 lagg_lb_porttable(sc, lp);
1682 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1684 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1685 struct lagg_port *lp = NULL;
1688 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1689 p = m->m_pkthdr.flowid;
1691 p = lagg_hashmbuf(m, lb->lb_key);
1693 lp = lb->lb_ports[p];
1696 * Check the port's link state. This will return the next active
1697 * port if the link is down or the port is NULL.
1699 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1705 return (lagg_enqueue(lp->lp_ifp, m));
1708 static struct mbuf *
1709 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1711 struct ifnet *ifp = sc->sc_ifp;
1713 /* Just pass in the packet to our lagg device */
1714 m->m_pkthdr.rcvif = ifp;
1724 lagg_lacp_attach(struct lagg_softc *sc)
1726 struct lagg_port *lp;
1729 sc->sc_detach = lagg_lacp_detach;
1730 sc->sc_port_create = lacp_port_create;
1731 sc->sc_port_destroy = lacp_port_destroy;
1732 sc->sc_linkstate = lacp_linkstate;
1733 sc->sc_start = lagg_lacp_start;
1734 sc->sc_input = lagg_lacp_input;
1735 sc->sc_init = lacp_init;
1736 sc->sc_stop = lacp_stop;
1737 sc->sc_lladdr = lagg_lacp_lladdr;
1738 sc->sc_req = lacp_req;
1739 sc->sc_portreq = lacp_portreq;
1741 error = lacp_attach(sc);
1745 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1746 lacp_port_create(lp);
1752 lagg_lacp_detach(struct lagg_softc *sc)
1754 struct lagg_port *lp;
1757 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1758 lacp_port_destroy(lp);
1760 /* unlocking is safe here */
1762 error = lacp_detach(sc);
1769 lagg_lacp_lladdr(struct lagg_softc *sc)
1771 struct lagg_port *lp;
1773 /* purge all the lacp ports */
1774 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1775 lacp_port_destroy(lp);
1777 /* add them back in */
1778 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1779 lacp_port_create(lp);
1783 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1785 struct lagg_port *lp;
1787 lp = lacp_select_tx_port(sc, m);
1794 return (lagg_enqueue(lp->lp_ifp, m));
1797 static struct mbuf *
1798 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1800 struct ifnet *ifp = sc->sc_ifp;
1801 struct ether_header *eh;
1804 eh = mtod(m, struct ether_header *);
1805 etype = ntohs(eh->ether_type);
1807 /* Tap off LACP control messages */
1808 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1809 m = lacp_input(lp, m);
1815 * If the port is not collecting or not in the active aggregator then
1818 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1823 m->m_pkthdr.rcvif = ifp;