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>
57 #include <netinet/ip.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/if_ether.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;
409 #if defined(INET) || defined(INET6)
410 u_int hw_tsomax = IP_MAXPACKET; /* Initialize to the maximum value. */
412 u_int hw_tsomax = ~0; /* if_hw_tsomax is only for INET/INET6, but.. */
415 LAGG_WLOCK_ASSERT(sc);
417 /* Get capabilities from the lagg ports */
418 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
419 cap &= lp->lp_ifp->if_capabilities;
420 ena &= lp->lp_ifp->if_capenable;
421 hwa &= lp->lp_ifp->if_hwassist;
422 /* Set to the minimum value of the lagg ports. */
423 if (lp->lp_ifp->if_hw_tsomax < hw_tsomax &&
424 lp->lp_ifp->if_hw_tsomax > 0)
425 hw_tsomax = lp->lp_ifp->if_hw_tsomax;
427 cap = (cap == ~0 ? 0 : cap);
428 ena = (ena == ~0 ? 0 : ena);
429 hwa = (hwa == ~0 ? 0 : hwa);
431 if (sc->sc_ifp->if_capabilities != cap ||
432 sc->sc_ifp->if_capenable != ena ||
433 sc->sc_ifp->if_hwassist != hwa ||
434 sc->sc_ifp->if_hw_tsomax != hw_tsomax) {
435 sc->sc_ifp->if_capabilities = cap;
436 sc->sc_ifp->if_capenable = ena;
437 sc->sc_ifp->if_hwassist = hwa;
438 sc->sc_ifp->if_hw_tsomax = hw_tsomax;
439 getmicrotime(&sc->sc_ifp->if_lastchange);
441 if (sc->sc_ifflags & IFF_DEBUG)
442 if_printf(sc->sc_ifp,
443 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
448 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
450 struct lagg_softc *sc = lp->lp_softc;
451 struct ifnet *ifp = lp->lp_ifp;
452 struct lagg_llq *llq;
455 LAGG_WLOCK_ASSERT(sc);
457 if (lp->lp_detaching ||
458 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
461 /* Check to make sure its not already queued to be changed */
462 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
463 if (llq->llq_ifp == ifp) {
470 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
471 if (llq == NULL) /* XXX what to do */
475 /* Update the lladdr even if pending, it may have changed */
477 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
480 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
482 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
486 * Set the interface MAC address from a taskqueue to avoid a LOR.
489 lagg_port_setlladdr(void *arg, int pending)
491 struct lagg_softc *sc = (struct lagg_softc *)arg;
492 struct lagg_llq *llq, *head;
496 /* Grab a local reference of the queue and remove it from the softc */
498 head = SLIST_FIRST(&sc->sc_llq_head);
499 SLIST_FIRST(&sc->sc_llq_head) = NULL;
503 * Traverse the queue and set the lladdr on each ifp. It is safe to do
504 * unlocked as we have the only reference to it.
506 for (llq = head; llq != NULL; llq = head) {
509 /* Set the link layer address */
510 CURVNET_SET(ifp->if_vnet);
511 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
514 printf("%s: setlladdr failed on %s\n", __func__,
517 head = SLIST_NEXT(llq, llq_entries);
523 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
525 struct lagg_softc *sc_ptr;
526 struct lagg_port *lp;
529 LAGG_WLOCK_ASSERT(sc);
531 /* Limit the maximal number of lagg ports */
532 if (sc->sc_count >= LAGG_MAX_PORTS)
535 /* Check if port has already been associated to a lagg */
536 if (ifp->if_lagg != NULL) {
537 /* Port is already in the current lagg? */
538 lp = (struct lagg_port *)ifp->if_lagg;
539 if (lp->lp_softc == sc)
544 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
545 if (ifp->if_type != IFT_ETHER)
546 return (EPROTONOSUPPORT);
550 * The member interface should not have inet6 address because
551 * two interfaces with a valid link-local scope zone must not be
552 * merged in any form. This restriction is needed to
553 * prevent violation of link-local scope zone. Attempts to
554 * add a member interface which has inet6 addresses triggers
555 * removal of all inet6 addresses on the member interface.
557 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
558 if (in6ifa_llaonifp(lp->lp_ifp)) {
559 in6_ifdetach(lp->lp_ifp);
560 if_printf(sc->sc_ifp,
561 "IPv6 addresses on %s have been removed "
562 "before adding it as a member to prevent "
563 "IPv6 address scope violation.\n",
564 lp->lp_ifp->if_xname);
567 if (in6ifa_llaonifp(ifp)) {
569 if_printf(sc->sc_ifp,
570 "IPv6 addresses on %s have been removed "
571 "before adding it as a member to prevent "
572 "IPv6 address scope violation.\n",
576 /* Allow the first Ethernet member to define the MTU */
577 if (SLIST_EMPTY(&sc->sc_ports))
578 sc->sc_ifp->if_mtu = ifp->if_mtu;
579 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
580 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
585 if ((lp = malloc(sizeof(struct lagg_port),
586 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
589 /* Check if port is a stacked lagg */
590 mtx_lock(&lagg_list_mtx);
591 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
592 if (ifp == sc_ptr->sc_ifp) {
593 mtx_unlock(&lagg_list_mtx);
596 /* XXX disable stacking for the moment, its untested */
597 #ifdef LAGG_PORT_STACKING
598 lp->lp_flags |= LAGG_PORT_STACK;
599 if (lagg_port_checkstacking(sc_ptr) >=
601 mtx_unlock(&lagg_list_mtx);
608 mtx_unlock(&lagg_list_mtx);
610 /* Change the interface type */
611 lp->lp_iftype = ifp->if_type;
612 ifp->if_type = IFT_IEEE8023ADLAG;
614 lp->lp_ioctl = ifp->if_ioctl;
615 ifp->if_ioctl = lagg_port_ioctl;
616 lp->lp_output = ifp->if_output;
617 ifp->if_output = lagg_port_output;
622 /* Save port link layer address */
623 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
625 if (SLIST_EMPTY(&sc->sc_ports)) {
627 lagg_lladdr(sc, IF_LLADDR(ifp));
629 /* Update link layer address for this port */
630 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
633 /* Insert into the list of ports */
634 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
637 /* Update lagg capabilities */
638 lagg_capabilities(sc);
641 /* Add multicast addresses and interface flags to this port */
642 lagg_ether_cmdmulti(lp, 1);
643 lagg_setflags(lp, 1);
645 if (sc->sc_port_create != NULL)
646 error = (*sc->sc_port_create)(lp);
648 /* remove the port again, without calling sc_port_destroy */
649 lagg_port_destroy(lp, 0);
656 #ifdef LAGG_PORT_STACKING
658 lagg_port_checkstacking(struct lagg_softc *sc)
660 struct lagg_softc *sc_ptr;
661 struct lagg_port *lp;
664 LAGG_WLOCK_ASSERT(sc);
666 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
667 if (lp->lp_flags & LAGG_PORT_STACK) {
668 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
669 m = MAX(m, lagg_port_checkstacking(sc_ptr));
678 lagg_port_destroy(struct lagg_port *lp, int runpd)
680 struct lagg_softc *sc = lp->lp_softc;
681 struct lagg_port *lp_ptr;
682 struct lagg_llq *llq;
683 struct ifnet *ifp = lp->lp_ifp;
685 LAGG_WLOCK_ASSERT(sc);
687 if (runpd && sc->sc_port_destroy != NULL)
688 (*sc->sc_port_destroy)(lp);
691 * Remove multicast addresses and interface flags from this port and
692 * reset the MAC address, skip if the interface is being detached.
694 if (!lp->lp_detaching) {
695 lagg_ether_cmdmulti(lp, 0);
696 lagg_setflags(lp, 0);
697 lagg_port_lladdr(lp, lp->lp_lladdr);
700 /* Restore interface */
701 ifp->if_type = lp->lp_iftype;
702 ifp->if_ioctl = lp->lp_ioctl;
703 ifp->if_output = lp->lp_output;
706 /* Finally, remove the port from the lagg */
707 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
710 /* Update the primary interface */
711 if (lp == sc->sc_primary) {
712 uint8_t lladdr[ETHER_ADDR_LEN];
714 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
715 bzero(&lladdr, ETHER_ADDR_LEN);
717 bcopy(lp_ptr->lp_lladdr,
718 lladdr, ETHER_ADDR_LEN);
720 lagg_lladdr(sc, lladdr);
721 sc->sc_primary = lp_ptr;
723 /* Update link layer address for each port */
724 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
725 lagg_port_lladdr(lp_ptr, lladdr);
728 /* Remove any pending lladdr changes from the queue */
729 if (lp->lp_detaching) {
730 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
731 if (llq->llq_ifp == ifp) {
732 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
735 break; /* Only appears once */
741 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
745 /* Update lagg capabilities */
746 lagg_capabilities(sc);
753 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
755 struct lagg_reqport *rp = (struct lagg_reqport *)data;
756 struct lagg_softc *sc;
757 struct lagg_port *lp = NULL;
760 /* Should be checked by the caller */
761 if (ifp->if_type != IFT_IEEE8023ADLAG ||
762 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
767 if (rp->rp_portname[0] == '\0' ||
768 ifunit(rp->rp_portname) != ifp) {
774 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
780 lagg_port2req(lp, rp);
785 if (lp->lp_ioctl == NULL) {
789 error = (*lp->lp_ioctl)(ifp, cmd, data);
793 /* Update lagg interface capabilities */
795 lagg_capabilities(sc);
800 /* Do not allow the MTU to be changed once joined */
811 if (lp->lp_ioctl != NULL)
812 return ((*lp->lp_ioctl)(ifp, cmd, data));
818 * For direct output to child ports.
821 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
822 struct sockaddr *dst, struct route *ro)
824 struct lagg_port *lp = ifp->if_lagg;
826 switch (dst->sa_family) {
827 case pseudo_AF_HDRCMPLT:
829 return ((*lp->lp_output)(ifp, m, dst, ro));
832 /* drop any other frames */
838 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
840 struct lagg_port *lp;
841 struct lagg_softc *sc;
843 if ((lp = ifp->if_lagg) == NULL)
845 /* If the ifnet is just being renamed, don't do anything. */
846 if (ifp->if_flags & IFF_RENAMING)
852 lp->lp_detaching = 1;
853 lagg_port_destroy(lp, 1);
858 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
860 struct lagg_softc *sc = lp->lp_softc;
862 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
863 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
864 rp->rp_prio = lp->lp_prio;
865 rp->rp_flags = lp->lp_flags;
866 if (sc->sc_portreq != NULL)
867 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
869 /* Add protocol specific flags */
870 switch (sc->sc_proto) {
871 case LAGG_PROTO_FAILOVER:
872 if (lp == sc->sc_primary)
873 rp->rp_flags |= LAGG_PORT_MASTER;
874 if (lp == lagg_link_active(sc, sc->sc_primary))
875 rp->rp_flags |= LAGG_PORT_ACTIVE;
878 case LAGG_PROTO_ROUNDROBIN:
879 case LAGG_PROTO_LOADBALANCE:
880 case LAGG_PROTO_ETHERCHANNEL:
881 if (LAGG_PORTACTIVE(lp))
882 rp->rp_flags |= LAGG_PORT_ACTIVE;
885 case LAGG_PROTO_LACP:
886 /* LACP has a different definition of active */
887 if (lacp_isactive(lp))
888 rp->rp_flags |= LAGG_PORT_ACTIVE;
889 if (lacp_iscollecting(lp))
890 rp->rp_flags |= LAGG_PORT_COLLECTING;
891 if (lacp_isdistributing(lp))
892 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
901 struct lagg_softc *sc = (struct lagg_softc *)xsc;
902 struct lagg_port *lp;
903 struct ifnet *ifp = sc->sc_ifp;
905 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
910 ifp->if_drv_flags |= IFF_DRV_RUNNING;
911 /* Update the port lladdrs */
912 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
913 lagg_port_lladdr(lp, IF_LLADDR(ifp));
915 if (sc->sc_init != NULL)
922 lagg_stop(struct lagg_softc *sc)
924 struct ifnet *ifp = sc->sc_ifp;
926 LAGG_WLOCK_ASSERT(sc);
928 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
931 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
933 if (sc->sc_stop != NULL)
938 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
940 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
941 struct lagg_reqall *ra = (struct lagg_reqall *)data;
942 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
943 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
944 struct ifreq *ifr = (struct ifreq *)data;
945 struct lagg_port *lp;
947 struct thread *td = curthread;
949 int count, buflen, len, error = 0;
951 bzero(&rpbuf, sizeof(rpbuf));
957 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
959 buflen = count * sizeof(struct lagg_reqport);
962 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
965 ra->ra_proto = sc->sc_proto;
966 if (sc->sc_req != NULL)
967 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
971 len = min(ra->ra_size, buflen);
972 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
973 if (len < sizeof(rpbuf))
976 lagg_port2req(lp, &rpbuf);
977 memcpy(buf, &rpbuf, sizeof(rpbuf));
979 buf += sizeof(rpbuf);
980 len -= sizeof(rpbuf);
983 ra->ra_ports = count;
984 ra->ra_size = count * sizeof(rpbuf);
985 error = copyout(outbuf, ra->ra_port, ra->ra_size);
986 free(outbuf, M_TEMP);
989 error = priv_check(td, PRIV_NET_LAGG);
992 if (ra->ra_proto >= LAGG_PROTO_MAX) {
993 error = EPROTONOSUPPORT;
997 if (sc->sc_proto != LAGG_PROTO_NONE) {
998 /* Reset protocol first in case detach unlocks */
999 sc->sc_proto = LAGG_PROTO_NONE;
1000 error = sc->sc_detach(sc);
1001 sc->sc_detach = NULL;
1002 sc->sc_start = NULL;
1003 sc->sc_input = NULL;
1004 sc->sc_port_create = NULL;
1005 sc->sc_port_destroy = NULL;
1006 sc->sc_linkstate = NULL;
1009 sc->sc_lladdr = NULL;
1011 sc->sc_portreq = NULL;
1012 } else if (sc->sc_input != NULL) {
1013 /* Still detaching */
1020 for (int i = 0; i < (sizeof(lagg_protos) /
1021 sizeof(lagg_protos[0])); i++) {
1022 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1023 if (sc->sc_ifflags & IFF_DEBUG)
1024 printf("%s: using proto %u\n",
1026 lagg_protos[i].ti_proto);
1027 sc->sc_proto = lagg_protos[i].ti_proto;
1028 if (sc->sc_proto != LAGG_PROTO_NONE)
1029 error = lagg_protos[i].ti_attach(sc);
1035 error = EPROTONOSUPPORT;
1037 case SIOCGLAGGFLAGS:
1038 rf->rf_flags = sc->sc_flags;
1041 error = priv_check(td, PRIV_NET_LAGG);
1044 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1049 sc->sc_flags &= ~LAGG_F_HASHMASK;
1050 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1054 if (rp->rp_portname[0] == '\0' ||
1055 (tpif = ifunit(rp->rp_portname)) == NULL) {
1061 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1062 lp->lp_softc != sc) {
1068 lagg_port2req(lp, rp);
1072 error = priv_check(td, PRIV_NET_LAGG);
1075 if (rp->rp_portname[0] == '\0' ||
1076 (tpif = ifunit(rp->rp_portname)) == NULL) {
1081 error = lagg_port_create(sc, tpif);
1084 case SIOCSLAGGDELPORT:
1085 error = priv_check(td, PRIV_NET_LAGG);
1088 if (rp->rp_portname[0] == '\0' ||
1089 (tpif = ifunit(rp->rp_portname)) == NULL) {
1095 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1096 lp->lp_softc != sc) {
1102 error = lagg_port_destroy(lp, 1);
1106 /* Set flags on ports too */
1108 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1109 lagg_setflags(lp, 1);
1113 if (!(ifp->if_flags & IFF_UP) &&
1114 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1116 * If interface is marked down and it is running,
1117 * then stop and disable it.
1122 } else if ((ifp->if_flags & IFF_UP) &&
1123 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1125 * If interface is marked up and it is stopped, then
1128 (*ifp->if_init)(sc);
1134 error = lagg_ether_setmulti(sc);
1139 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1144 /* Do not allow the MTU or caps to be directly changed */
1149 error = ether_ioctl(ifp, cmd, data);
1156 lagg_ether_setmulti(struct lagg_softc *sc)
1158 struct lagg_port *lp;
1160 LAGG_WLOCK_ASSERT(sc);
1162 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1163 /* First, remove any existing filter entries. */
1164 lagg_ether_cmdmulti(lp, 0);
1165 /* copy all addresses from the lagg interface to the port */
1166 lagg_ether_cmdmulti(lp, 1);
1172 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1174 struct lagg_softc *sc = lp->lp_softc;
1175 struct ifnet *ifp = lp->lp_ifp;
1176 struct ifnet *scifp = sc->sc_ifp;
1178 struct ifmultiaddr *ifma, *rifma = NULL;
1179 struct sockaddr_dl sdl;
1182 LAGG_WLOCK_ASSERT(sc);
1184 bzero((char *)&sdl, sizeof(sdl));
1185 sdl.sdl_len = sizeof(sdl);
1186 sdl.sdl_family = AF_LINK;
1187 sdl.sdl_type = IFT_ETHER;
1188 sdl.sdl_alen = ETHER_ADDR_LEN;
1189 sdl.sdl_index = ifp->if_index;
1192 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1193 if (ifma->ifma_addr->sa_family != AF_LINK)
1195 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1196 LLADDR(&sdl), ETHER_ADDR_LEN);
1198 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1201 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1204 mc->mc_ifma = rifma;
1205 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1208 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1209 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1210 if_delmulti_ifma(mc->mc_ifma);
1217 /* Handle a ref counted flag that should be set on the lagg port as well */
1219 lagg_setflag(struct lagg_port *lp, int flag, int status,
1220 int (*func)(struct ifnet *, int))
1222 struct lagg_softc *sc = lp->lp_softc;
1223 struct ifnet *scifp = sc->sc_ifp;
1224 struct ifnet *ifp = lp->lp_ifp;
1227 LAGG_WLOCK_ASSERT(sc);
1229 status = status ? (scifp->if_flags & flag) : 0;
1230 /* Now "status" contains the flag value or 0 */
1233 * See if recorded ports status is different from what
1234 * we want it to be. If it is, flip it. We record ports
1235 * status in lp_ifflags so that we won't clear ports flag
1236 * we haven't set. In fact, we don't clear or set ports
1237 * flags directly, but get or release references to them.
1238 * That's why we can be sure that recorded flags still are
1239 * in accord with actual ports flags.
1241 if (status != (lp->lp_ifflags & flag)) {
1242 error = (*func)(ifp, status);
1245 lp->lp_ifflags &= ~flag;
1246 lp->lp_ifflags |= status;
1252 * Handle IFF_* flags that require certain changes on the lagg port
1253 * if "status" is true, update ports flags respective to the lagg
1254 * if "status" is false, forcedly clear the flags set on port.
1257 lagg_setflags(struct lagg_port *lp, int status)
1261 for (i = 0; lagg_pflags[i].flag; i++) {
1262 error = lagg_setflag(lp, lagg_pflags[i].flag,
1263 status, lagg_pflags[i].func);
1271 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1273 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1274 int error, len, mcast;
1276 len = m->m_pkthdr.len;
1277 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1280 /* We need a Tx algorithm and at least one port */
1281 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1288 ETHER_BPF_MTAP(ifp, m);
1290 error = (*sc->sc_start)(sc, m);
1295 ifp->if_omcasts += mcast;
1296 ifp->if_obytes += len;
1304 * The ifp->if_qflush entry point for lagg(4) is no-op.
1307 lagg_qflush(struct ifnet *ifp __unused)
1311 static struct mbuf *
1312 lagg_input(struct ifnet *ifp, struct mbuf *m)
1314 struct lagg_port *lp = ifp->if_lagg;
1315 struct lagg_softc *sc = lp->lp_softc;
1316 struct ifnet *scifp = sc->sc_ifp;
1319 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1320 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1321 sc->sc_proto == LAGG_PROTO_NONE) {
1327 ETHER_BPF_MTAP(scifp, m);
1329 m = (*sc->sc_input)(sc, lp, m);
1332 scifp->if_ipackets++;
1333 scifp->if_ibytes += m->m_pkthdr.len;
1335 if (scifp->if_flags & IFF_MONITOR) {
1346 lagg_media_change(struct ifnet *ifp)
1348 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1350 if (sc->sc_ifflags & IFF_DEBUG)
1351 printf("%s\n", __func__);
1358 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1360 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1361 struct lagg_port *lp;
1363 imr->ifm_status = IFM_AVALID;
1364 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1367 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1368 if (LAGG_PORTACTIVE(lp))
1369 imr->ifm_status |= IFM_ACTIVE;
1375 lagg_linkstate(struct lagg_softc *sc)
1377 struct lagg_port *lp;
1378 int new_link = LINK_STATE_DOWN;
1381 /* Our link is considered up if at least one of our ports is active */
1382 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1383 if (lp->lp_link_state == LINK_STATE_UP) {
1384 new_link = LINK_STATE_UP;
1388 if_link_state_change(sc->sc_ifp, new_link);
1390 /* Update if_baudrate to reflect the max possible speed */
1391 switch (sc->sc_proto) {
1392 case LAGG_PROTO_FAILOVER:
1393 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1394 sc->sc_primary->lp_ifp->if_baudrate : 0;
1396 case LAGG_PROTO_ROUNDROBIN:
1397 case LAGG_PROTO_LOADBALANCE:
1398 case LAGG_PROTO_ETHERCHANNEL:
1400 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1401 speed += lp->lp_ifp->if_baudrate;
1402 sc->sc_ifp->if_baudrate = speed;
1404 case LAGG_PROTO_LACP:
1405 /* LACP updates if_baudrate itself */
1411 lagg_port_state(struct ifnet *ifp, int state)
1413 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1414 struct lagg_softc *sc = NULL;
1423 if (sc->sc_linkstate != NULL)
1424 (*sc->sc_linkstate)(lp);
1429 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1431 struct lagg_port *lp_next, *rval = NULL;
1432 // int new_link = LINK_STATE_DOWN;
1434 LAGG_RLOCK_ASSERT(sc);
1436 * Search a port which reports an active link state.
1441 if (LAGG_PORTACTIVE(lp)) {
1445 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1446 LAGG_PORTACTIVE(lp_next)) {
1452 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1453 if (LAGG_PORTACTIVE(lp_next)) {
1462 * The IEEE 802.1D standard assumes that a lagg with
1463 * multiple ports is always full duplex. This is valid
1464 * for load sharing laggs and if at least two links
1465 * are active. Unfortunately, checking the latter would
1466 * be too expensive at this point.
1468 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1470 new_link = LINK_STATE_FULL_DUPLEX;
1472 new_link = rval->lp_link_state;
1480 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1482 if (m->m_pkthdr.len < (off + len)) {
1484 } else if (m->m_len < (off + len)) {
1485 m_copydata(m, off, len, buf);
1488 return (mtod(m, char *) + off);
1492 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1497 struct ether_header *eh;
1498 const struct ether_vlan_header *vlan;
1500 const struct ip *ip;
1501 const uint32_t *ports;
1505 const struct ip6_hdr *ip6;
1515 struct ether_vlan_header vlan;
1523 eh = mtod(m, struct ether_header *);
1524 etype = ntohs(eh->ether_type);
1525 if (sc->sc_flags & LAGG_F_HASHL2) {
1526 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1527 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1530 /* Special handling for encapsulating VLAN frames */
1531 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1532 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1533 sizeof(m->m_pkthdr.ether_vtag), p);
1534 } else if (etype == ETHERTYPE_VLAN) {
1535 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1539 if (sc->sc_flags & LAGG_F_HASHL2)
1540 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1541 etype = ntohs(vlan->evl_proto);
1542 off += sizeof(*vlan) - sizeof(*eh);
1548 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1552 if (sc->sc_flags & LAGG_F_HASHL3) {
1553 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1554 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1556 if (!(sc->sc_flags & LAGG_F_HASHL4))
1562 iphlen = ip->ip_hl << 2;
1563 if (iphlen < sizeof(*ip))
1566 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1569 p = hash32_buf(ports, sizeof(*ports), p);
1575 case ETHERTYPE_IPV6:
1576 if (!(sc->sc_flags & LAGG_F_HASHL3))
1578 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1582 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1583 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1584 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1585 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1594 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1597 return (ifp->if_transmit)(ifp, m);
1601 * Simple round robin aggregation
1605 lagg_rr_attach(struct lagg_softc *sc)
1607 sc->sc_detach = lagg_rr_detach;
1608 sc->sc_start = lagg_rr_start;
1609 sc->sc_input = lagg_rr_input;
1610 sc->sc_port_create = NULL;
1611 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1618 lagg_rr_detach(struct lagg_softc *sc)
1624 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1626 struct lagg_port *lp;
1629 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1631 lp = SLIST_FIRST(&sc->sc_ports);
1633 lp = SLIST_NEXT(lp, lp_entries);
1636 * Check the port's link state. This will return the next active
1637 * port if the link is down or the port is NULL.
1639 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1645 return (lagg_enqueue(lp->lp_ifp, m));
1648 static struct mbuf *
1649 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1651 struct ifnet *ifp = sc->sc_ifp;
1653 /* Just pass in the packet to our lagg device */
1654 m->m_pkthdr.rcvif = ifp;
1664 lagg_fail_attach(struct lagg_softc *sc)
1666 sc->sc_detach = lagg_fail_detach;
1667 sc->sc_start = lagg_fail_start;
1668 sc->sc_input = lagg_fail_input;
1669 sc->sc_port_create = NULL;
1670 sc->sc_port_destroy = NULL;
1676 lagg_fail_detach(struct lagg_softc *sc)
1682 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1684 struct lagg_port *lp;
1686 /* Use the master port if active or the next available port */
1687 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1693 return (lagg_enqueue(lp->lp_ifp, m));
1696 static struct mbuf *
1697 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1699 struct ifnet *ifp = sc->sc_ifp;
1700 struct lagg_port *tmp_tp;
1702 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1703 m->m_pkthdr.rcvif = ifp;
1707 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1708 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1710 * If tmp_tp is null, we've recieved a packet when all
1711 * our links are down. Weird, but process it anyways.
1713 if ((tmp_tp == NULL || tmp_tp == lp)) {
1714 m->m_pkthdr.rcvif = ifp;
1728 lagg_lb_attach(struct lagg_softc *sc)
1730 struct lagg_port *lp;
1733 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1734 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1737 sc->sc_detach = lagg_lb_detach;
1738 sc->sc_start = lagg_lb_start;
1739 sc->sc_input = lagg_lb_input;
1740 sc->sc_port_create = lagg_lb_port_create;
1741 sc->sc_port_destroy = lagg_lb_port_destroy;
1742 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1744 lb->lb_key = arc4random();
1745 sc->sc_psc = (caddr_t)lb;
1747 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1748 lagg_lb_port_create(lp);
1754 lagg_lb_detach(struct lagg_softc *sc)
1756 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1763 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1765 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1766 struct lagg_port *lp_next;
1769 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1770 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1773 if (i >= LAGG_MAX_PORTS)
1775 if (sc->sc_ifflags & IFF_DEBUG)
1776 printf("%s: port %s at index %d\n",
1777 sc->sc_ifname, lp_next->lp_ifname, i);
1778 lb->lb_ports[i++] = lp_next;
1785 lagg_lb_port_create(struct lagg_port *lp)
1787 struct lagg_softc *sc = lp->lp_softc;
1788 return (lagg_lb_porttable(sc, NULL));
1792 lagg_lb_port_destroy(struct lagg_port *lp)
1794 struct lagg_softc *sc = lp->lp_softc;
1795 lagg_lb_porttable(sc, lp);
1799 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1801 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1802 struct lagg_port *lp = NULL;
1805 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1806 p = m->m_pkthdr.flowid;
1808 p = lagg_hashmbuf(sc, m, lb->lb_key);
1810 lp = lb->lb_ports[p];
1813 * Check the port's link state. This will return the next active
1814 * port if the link is down or the port is NULL.
1816 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1822 return (lagg_enqueue(lp->lp_ifp, m));
1825 static struct mbuf *
1826 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1828 struct ifnet *ifp = sc->sc_ifp;
1830 /* Just pass in the packet to our lagg device */
1831 m->m_pkthdr.rcvif = ifp;
1841 lagg_lacp_attach(struct lagg_softc *sc)
1843 struct lagg_port *lp;
1846 sc->sc_detach = lagg_lacp_detach;
1847 sc->sc_port_create = lacp_port_create;
1848 sc->sc_port_destroy = lacp_port_destroy;
1849 sc->sc_linkstate = lacp_linkstate;
1850 sc->sc_start = lagg_lacp_start;
1851 sc->sc_input = lagg_lacp_input;
1852 sc->sc_init = lacp_init;
1853 sc->sc_stop = lacp_stop;
1854 sc->sc_lladdr = lagg_lacp_lladdr;
1855 sc->sc_req = lacp_req;
1856 sc->sc_portreq = lacp_portreq;
1858 error = lacp_attach(sc);
1862 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1863 lacp_port_create(lp);
1869 lagg_lacp_detach(struct lagg_softc *sc)
1871 struct lagg_port *lp;
1874 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1875 lacp_port_destroy(lp);
1877 /* unlocking is safe here */
1879 error = lacp_detach(sc);
1886 lagg_lacp_lladdr(struct lagg_softc *sc)
1888 struct lagg_port *lp;
1890 /* purge all the lacp ports */
1891 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1892 lacp_port_destroy(lp);
1894 /* add them back in */
1895 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1896 lacp_port_create(lp);
1900 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1902 struct lagg_port *lp;
1904 lp = lacp_select_tx_port(sc, m);
1911 return (lagg_enqueue(lp->lp_ifp, m));
1914 static struct mbuf *
1915 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1917 struct ifnet *ifp = sc->sc_ifp;
1918 struct ether_header *eh;
1921 eh = mtod(m, struct ether_header *);
1922 etype = ntohs(eh->ether_type);
1924 /* Tap off LACP control messages */
1925 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1926 m = lacp_input(lp, m);
1932 * If the port is not collecting or not in the active aggregator then
1935 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1940 m->m_pkthdr.rcvif = ifp;