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", 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", 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 struct ifnet_hw_tsomax hw_tsomax;
411 LAGG_WLOCK_ASSERT(sc);
413 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
415 /* Get capabilities from the lagg ports */
416 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
417 cap &= lp->lp_ifp->if_capabilities;
418 ena &= lp->lp_ifp->if_capenable;
419 hwa &= lp->lp_ifp->if_hwassist;
420 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
422 cap = (cap == ~0 ? 0 : cap);
423 ena = (ena == ~0 ? 0 : ena);
424 hwa = (hwa == ~0 ? 0 : hwa);
426 if (sc->sc_ifp->if_capabilities != cap ||
427 sc->sc_ifp->if_capenable != ena ||
428 sc->sc_ifp->if_hwassist != hwa ||
429 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
430 sc->sc_ifp->if_capabilities = cap;
431 sc->sc_ifp->if_capenable = ena;
432 sc->sc_ifp->if_hwassist = hwa;
433 getmicrotime(&sc->sc_ifp->if_lastchange);
435 if (sc->sc_ifflags & IFF_DEBUG)
436 if_printf(sc->sc_ifp,
437 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
442 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
444 struct lagg_softc *sc = lp->lp_softc;
445 struct ifnet *ifp = lp->lp_ifp;
446 struct lagg_llq *llq;
449 LAGG_WLOCK_ASSERT(sc);
451 if (lp->lp_detaching ||
452 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
455 /* Check to make sure its not already queued to be changed */
456 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
457 if (llq->llq_ifp == ifp) {
464 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
465 if (llq == NULL) /* XXX what to do */
469 /* Update the lladdr even if pending, it may have changed */
471 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
474 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
476 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
480 * Set the interface MAC address from a taskqueue to avoid a LOR.
483 lagg_port_setlladdr(void *arg, int pending)
485 struct lagg_softc *sc = (struct lagg_softc *)arg;
486 struct lagg_llq *llq, *head;
490 /* Grab a local reference of the queue and remove it from the softc */
492 head = SLIST_FIRST(&sc->sc_llq_head);
493 SLIST_FIRST(&sc->sc_llq_head) = NULL;
497 * Traverse the queue and set the lladdr on each ifp. It is safe to do
498 * unlocked as we have the only reference to it.
500 for (llq = head; llq != NULL; llq = head) {
503 /* Set the link layer address */
504 CURVNET_SET(ifp->if_vnet);
505 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
508 printf("%s: setlladdr failed on %s\n", __func__,
511 head = SLIST_NEXT(llq, llq_entries);
517 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
519 struct lagg_softc *sc_ptr;
520 struct lagg_port *lp, *tlp;
523 LAGG_WLOCK_ASSERT(sc);
525 /* Limit the maximal number of lagg ports */
526 if (sc->sc_count >= LAGG_MAX_PORTS)
529 /* Check if port has already been associated to a lagg */
530 if (ifp->if_lagg != NULL) {
531 /* Port is already in the current lagg? */
532 lp = (struct lagg_port *)ifp->if_lagg;
533 if (lp->lp_softc == sc)
538 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
539 if (ifp->if_type != IFT_ETHER)
540 return (EPROTONOSUPPORT);
544 * The member interface should not have inet6 address because
545 * two interfaces with a valid link-local scope zone must not be
546 * merged in any form. This restriction is needed to
547 * prevent violation of link-local scope zone. Attempts to
548 * add a member interface which has inet6 addresses triggers
549 * removal of all inet6 addresses on the member interface.
551 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
552 if (in6ifa_llaonifp(lp->lp_ifp)) {
553 in6_ifdetach(lp->lp_ifp);
554 if_printf(sc->sc_ifp,
555 "IPv6 addresses on %s have been removed "
556 "before adding it as a member to prevent "
557 "IPv6 address scope violation.\n",
558 lp->lp_ifp->if_xname);
561 if (in6ifa_llaonifp(ifp)) {
563 if_printf(sc->sc_ifp,
564 "IPv6 addresses on %s have been removed "
565 "before adding it as a member to prevent "
566 "IPv6 address scope violation.\n",
570 /* Allow the first Ethernet member to define the MTU */
571 if (SLIST_EMPTY(&sc->sc_ports))
572 sc->sc_ifp->if_mtu = ifp->if_mtu;
573 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
574 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
579 if ((lp = malloc(sizeof(struct lagg_port),
580 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
583 /* Check if port is a stacked lagg */
584 mtx_lock(&lagg_list_mtx);
585 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
586 if (ifp == sc_ptr->sc_ifp) {
587 mtx_unlock(&lagg_list_mtx);
590 /* XXX disable stacking for the moment, its untested */
591 #ifdef LAGG_PORT_STACKING
592 lp->lp_flags |= LAGG_PORT_STACK;
593 if (lagg_port_checkstacking(sc_ptr) >=
595 mtx_unlock(&lagg_list_mtx);
602 mtx_unlock(&lagg_list_mtx);
604 /* Change the interface type */
605 lp->lp_iftype = ifp->if_type;
606 ifp->if_type = IFT_IEEE8023ADLAG;
608 lp->lp_ioctl = ifp->if_ioctl;
609 ifp->if_ioctl = lagg_port_ioctl;
610 lp->lp_output = ifp->if_output;
611 ifp->if_output = lagg_port_output;
616 /* Save port link layer address */
617 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
619 if (SLIST_EMPTY(&sc->sc_ports)) {
621 lagg_lladdr(sc, IF_LLADDR(ifp));
623 /* Update link layer address for this port */
624 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
628 * Insert into the list of ports.
629 * Keep ports sorted by if_index. It is handy, when configuration
630 * is predictable and `ifconfig laggN create ...` command
631 * will lead to the same result each time.
633 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
634 if (tlp->lp_ifp->if_index < ifp->if_index && (
635 SLIST_NEXT(tlp, lp_entries) == NULL ||
636 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
641 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
643 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
646 /* Update lagg capabilities */
647 lagg_capabilities(sc);
650 /* Add multicast addresses and interface flags to this port */
651 lagg_ether_cmdmulti(lp, 1);
652 lagg_setflags(lp, 1);
654 if (sc->sc_port_create != NULL)
655 error = (*sc->sc_port_create)(lp);
657 /* remove the port again, without calling sc_port_destroy */
658 lagg_port_destroy(lp, 0);
665 #ifdef LAGG_PORT_STACKING
667 lagg_port_checkstacking(struct lagg_softc *sc)
669 struct lagg_softc *sc_ptr;
670 struct lagg_port *lp;
673 LAGG_WLOCK_ASSERT(sc);
675 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
676 if (lp->lp_flags & LAGG_PORT_STACK) {
677 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
678 m = MAX(m, lagg_port_checkstacking(sc_ptr));
687 lagg_port_destroy(struct lagg_port *lp, int runpd)
689 struct lagg_softc *sc = lp->lp_softc;
690 struct lagg_port *lp_ptr;
691 struct lagg_llq *llq;
692 struct ifnet *ifp = lp->lp_ifp;
694 LAGG_WLOCK_ASSERT(sc);
696 if (runpd && sc->sc_port_destroy != NULL)
697 (*sc->sc_port_destroy)(lp);
700 * Remove multicast addresses and interface flags from this port and
701 * reset the MAC address, skip if the interface is being detached.
703 if (!lp->lp_detaching) {
704 lagg_ether_cmdmulti(lp, 0);
705 lagg_setflags(lp, 0);
706 lagg_port_lladdr(lp, lp->lp_lladdr);
709 /* Restore interface */
710 ifp->if_type = lp->lp_iftype;
711 ifp->if_ioctl = lp->lp_ioctl;
712 ifp->if_output = lp->lp_output;
715 /* Finally, remove the port from the lagg */
716 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
719 /* Update the primary interface */
720 if (lp == sc->sc_primary) {
721 uint8_t lladdr[ETHER_ADDR_LEN];
723 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
724 bzero(&lladdr, ETHER_ADDR_LEN);
726 bcopy(lp_ptr->lp_lladdr,
727 lladdr, ETHER_ADDR_LEN);
729 lagg_lladdr(sc, lladdr);
730 sc->sc_primary = lp_ptr;
732 /* Update link layer address for each port */
733 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
734 lagg_port_lladdr(lp_ptr, lladdr);
737 /* Remove any pending lladdr changes from the queue */
738 if (lp->lp_detaching) {
739 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
740 if (llq->llq_ifp == ifp) {
741 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
744 break; /* Only appears once */
750 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
754 /* Update lagg capabilities */
755 lagg_capabilities(sc);
762 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
764 struct lagg_reqport *rp = (struct lagg_reqport *)data;
765 struct lagg_softc *sc;
766 struct lagg_port *lp = NULL;
769 /* Should be checked by the caller */
770 if (ifp->if_type != IFT_IEEE8023ADLAG ||
771 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
776 if (rp->rp_portname[0] == '\0' ||
777 ifunit(rp->rp_portname) != ifp) {
783 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
789 lagg_port2req(lp, rp);
794 if (lp->lp_ioctl == NULL) {
798 error = (*lp->lp_ioctl)(ifp, cmd, data);
802 /* Update lagg interface capabilities */
804 lagg_capabilities(sc);
809 /* Do not allow the MTU to be changed once joined */
820 if (lp->lp_ioctl != NULL)
821 return ((*lp->lp_ioctl)(ifp, cmd, data));
827 * For direct output to child ports.
830 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
831 struct sockaddr *dst, struct route *ro)
833 struct lagg_port *lp = ifp->if_lagg;
835 switch (dst->sa_family) {
836 case pseudo_AF_HDRCMPLT:
838 return ((*lp->lp_output)(ifp, m, dst, ro));
841 /* drop any other frames */
847 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
849 struct lagg_port *lp;
850 struct lagg_softc *sc;
852 if ((lp = ifp->if_lagg) == NULL)
854 /* If the ifnet is just being renamed, don't do anything. */
855 if (ifp->if_flags & IFF_RENAMING)
861 lp->lp_detaching = 1;
862 lagg_port_destroy(lp, 1);
867 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
869 struct lagg_softc *sc = lp->lp_softc;
871 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
872 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
873 rp->rp_prio = lp->lp_prio;
874 rp->rp_flags = lp->lp_flags;
875 if (sc->sc_portreq != NULL)
876 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
878 /* Add protocol specific flags */
879 switch (sc->sc_proto) {
880 case LAGG_PROTO_FAILOVER:
881 if (lp == sc->sc_primary)
882 rp->rp_flags |= LAGG_PORT_MASTER;
883 if (lp == lagg_link_active(sc, sc->sc_primary))
884 rp->rp_flags |= LAGG_PORT_ACTIVE;
887 case LAGG_PROTO_ROUNDROBIN:
888 case LAGG_PROTO_LOADBALANCE:
889 case LAGG_PROTO_ETHERCHANNEL:
890 if (LAGG_PORTACTIVE(lp))
891 rp->rp_flags |= LAGG_PORT_ACTIVE;
894 case LAGG_PROTO_LACP:
895 /* LACP has a different definition of active */
896 if (lacp_isactive(lp))
897 rp->rp_flags |= LAGG_PORT_ACTIVE;
898 if (lacp_iscollecting(lp))
899 rp->rp_flags |= LAGG_PORT_COLLECTING;
900 if (lacp_isdistributing(lp))
901 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
910 struct lagg_softc *sc = (struct lagg_softc *)xsc;
911 struct lagg_port *lp;
912 struct ifnet *ifp = sc->sc_ifp;
914 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
919 ifp->if_drv_flags |= IFF_DRV_RUNNING;
920 /* Update the port lladdrs */
921 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
922 lagg_port_lladdr(lp, IF_LLADDR(ifp));
924 if (sc->sc_init != NULL)
931 lagg_stop(struct lagg_softc *sc)
933 struct ifnet *ifp = sc->sc_ifp;
935 LAGG_WLOCK_ASSERT(sc);
937 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
940 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
942 if (sc->sc_stop != NULL)
947 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
949 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
950 struct lagg_reqall *ra = (struct lagg_reqall *)data;
951 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
952 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
953 struct ifreq *ifr = (struct ifreq *)data;
954 struct lagg_port *lp;
956 struct thread *td = curthread;
958 int count, buflen, len, error = 0;
960 bzero(&rpbuf, sizeof(rpbuf));
966 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
968 buflen = count * sizeof(struct lagg_reqport);
971 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
974 ra->ra_proto = sc->sc_proto;
975 if (sc->sc_req != NULL)
976 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
980 len = min(ra->ra_size, buflen);
981 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
982 if (len < sizeof(rpbuf))
985 lagg_port2req(lp, &rpbuf);
986 memcpy(buf, &rpbuf, sizeof(rpbuf));
988 buf += sizeof(rpbuf);
989 len -= sizeof(rpbuf);
992 ra->ra_ports = count;
993 ra->ra_size = count * sizeof(rpbuf);
994 error = copyout(outbuf, ra->ra_port, ra->ra_size);
995 free(outbuf, M_TEMP);
998 error = priv_check(td, PRIV_NET_LAGG);
1001 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1002 error = EPROTONOSUPPORT;
1006 if (sc->sc_proto != LAGG_PROTO_NONE) {
1007 /* Reset protocol first in case detach unlocks */
1008 sc->sc_proto = LAGG_PROTO_NONE;
1009 error = sc->sc_detach(sc);
1010 sc->sc_detach = NULL;
1011 sc->sc_start = NULL;
1012 sc->sc_input = NULL;
1013 sc->sc_port_create = NULL;
1014 sc->sc_port_destroy = NULL;
1015 sc->sc_linkstate = NULL;
1018 sc->sc_lladdr = NULL;
1020 sc->sc_portreq = NULL;
1021 } else if (sc->sc_input != NULL) {
1022 /* Still detaching */
1029 for (int i = 0; i < (sizeof(lagg_protos) /
1030 sizeof(lagg_protos[0])); i++) {
1031 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1032 if (sc->sc_ifflags & IFF_DEBUG)
1033 printf("%s: using proto %u\n",
1035 lagg_protos[i].ti_proto);
1036 sc->sc_proto = lagg_protos[i].ti_proto;
1037 if (sc->sc_proto != LAGG_PROTO_NONE)
1038 error = lagg_protos[i].ti_attach(sc);
1044 error = EPROTONOSUPPORT;
1046 case SIOCGLAGGFLAGS:
1047 rf->rf_flags = sc->sc_flags;
1050 error = priv_check(td, PRIV_NET_LAGG);
1053 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1058 sc->sc_flags &= ~LAGG_F_HASHMASK;
1059 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1063 if (rp->rp_portname[0] == '\0' ||
1064 (tpif = ifunit(rp->rp_portname)) == NULL) {
1070 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1071 lp->lp_softc != sc) {
1077 lagg_port2req(lp, rp);
1081 error = priv_check(td, PRIV_NET_LAGG);
1084 if (rp->rp_portname[0] == '\0' ||
1085 (tpif = ifunit(rp->rp_portname)) == NULL) {
1090 error = lagg_port_create(sc, tpif);
1093 case SIOCSLAGGDELPORT:
1094 error = priv_check(td, PRIV_NET_LAGG);
1097 if (rp->rp_portname[0] == '\0' ||
1098 (tpif = ifunit(rp->rp_portname)) == NULL) {
1104 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1105 lp->lp_softc != sc) {
1111 error = lagg_port_destroy(lp, 1);
1115 /* Set flags on ports too */
1117 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1118 lagg_setflags(lp, 1);
1122 if (!(ifp->if_flags & IFF_UP) &&
1123 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1125 * If interface is marked down and it is running,
1126 * then stop and disable it.
1131 } else if ((ifp->if_flags & IFF_UP) &&
1132 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1134 * If interface is marked up and it is stopped, then
1137 (*ifp->if_init)(sc);
1143 error = lagg_ether_setmulti(sc);
1148 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1153 /* Do not allow the MTU or caps to be directly changed */
1158 error = ether_ioctl(ifp, cmd, data);
1165 lagg_ether_setmulti(struct lagg_softc *sc)
1167 struct lagg_port *lp;
1169 LAGG_WLOCK_ASSERT(sc);
1171 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1172 /* First, remove any existing filter entries. */
1173 lagg_ether_cmdmulti(lp, 0);
1174 /* copy all addresses from the lagg interface to the port */
1175 lagg_ether_cmdmulti(lp, 1);
1181 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1183 struct lagg_softc *sc = lp->lp_softc;
1184 struct ifnet *ifp = lp->lp_ifp;
1185 struct ifnet *scifp = sc->sc_ifp;
1187 struct ifmultiaddr *ifma, *rifma = NULL;
1188 struct sockaddr_dl sdl;
1191 LAGG_WLOCK_ASSERT(sc);
1193 bzero((char *)&sdl, sizeof(sdl));
1194 sdl.sdl_len = sizeof(sdl);
1195 sdl.sdl_family = AF_LINK;
1196 sdl.sdl_type = IFT_ETHER;
1197 sdl.sdl_alen = ETHER_ADDR_LEN;
1198 sdl.sdl_index = ifp->if_index;
1201 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1202 if (ifma->ifma_addr->sa_family != AF_LINK)
1204 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1205 LLADDR(&sdl), ETHER_ADDR_LEN);
1207 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1210 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1213 mc->mc_ifma = rifma;
1214 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1217 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1218 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1219 if_delmulti_ifma(mc->mc_ifma);
1226 /* Handle a ref counted flag that should be set on the lagg port as well */
1228 lagg_setflag(struct lagg_port *lp, int flag, int status,
1229 int (*func)(struct ifnet *, int))
1231 struct lagg_softc *sc = lp->lp_softc;
1232 struct ifnet *scifp = sc->sc_ifp;
1233 struct ifnet *ifp = lp->lp_ifp;
1236 LAGG_WLOCK_ASSERT(sc);
1238 status = status ? (scifp->if_flags & flag) : 0;
1239 /* Now "status" contains the flag value or 0 */
1242 * See if recorded ports status is different from what
1243 * we want it to be. If it is, flip it. We record ports
1244 * status in lp_ifflags so that we won't clear ports flag
1245 * we haven't set. In fact, we don't clear or set ports
1246 * flags directly, but get or release references to them.
1247 * That's why we can be sure that recorded flags still are
1248 * in accord with actual ports flags.
1250 if (status != (lp->lp_ifflags & flag)) {
1251 error = (*func)(ifp, status);
1254 lp->lp_ifflags &= ~flag;
1255 lp->lp_ifflags |= status;
1261 * Handle IFF_* flags that require certain changes on the lagg port
1262 * if "status" is true, update ports flags respective to the lagg
1263 * if "status" is false, forcedly clear the flags set on port.
1266 lagg_setflags(struct lagg_port *lp, int status)
1270 for (i = 0; lagg_pflags[i].flag; i++) {
1271 error = lagg_setflag(lp, lagg_pflags[i].flag,
1272 status, lagg_pflags[i].func);
1280 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1282 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1283 int error, len, mcast;
1285 len = m->m_pkthdr.len;
1286 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1289 /* We need a Tx algorithm and at least one port */
1290 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1297 ETHER_BPF_MTAP(ifp, m);
1299 error = (*sc->sc_start)(sc, m);
1304 ifp->if_omcasts += mcast;
1305 ifp->if_obytes += len;
1313 * The ifp->if_qflush entry point for lagg(4) is no-op.
1316 lagg_qflush(struct ifnet *ifp __unused)
1320 static struct mbuf *
1321 lagg_input(struct ifnet *ifp, struct mbuf *m)
1323 struct lagg_port *lp = ifp->if_lagg;
1324 struct lagg_softc *sc = lp->lp_softc;
1325 struct ifnet *scifp = sc->sc_ifp;
1328 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1329 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1330 sc->sc_proto == LAGG_PROTO_NONE) {
1336 ETHER_BPF_MTAP(scifp, m);
1338 m = (*sc->sc_input)(sc, lp, m);
1341 scifp->if_ipackets++;
1342 scifp->if_ibytes += m->m_pkthdr.len;
1344 if (scifp->if_flags & IFF_MONITOR) {
1355 lagg_media_change(struct ifnet *ifp)
1357 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1359 if (sc->sc_ifflags & IFF_DEBUG)
1360 printf("%s\n", __func__);
1367 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1369 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1370 struct lagg_port *lp;
1372 imr->ifm_status = IFM_AVALID;
1373 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1376 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1377 if (LAGG_PORTACTIVE(lp))
1378 imr->ifm_status |= IFM_ACTIVE;
1384 lagg_linkstate(struct lagg_softc *sc)
1386 struct lagg_port *lp;
1387 int new_link = LINK_STATE_DOWN;
1390 /* Our link is considered up if at least one of our ports is active */
1391 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1392 if (lp->lp_link_state == LINK_STATE_UP) {
1393 new_link = LINK_STATE_UP;
1397 if_link_state_change(sc->sc_ifp, new_link);
1399 /* Update if_baudrate to reflect the max possible speed */
1400 switch (sc->sc_proto) {
1401 case LAGG_PROTO_FAILOVER:
1402 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1403 sc->sc_primary->lp_ifp->if_baudrate : 0;
1405 case LAGG_PROTO_ROUNDROBIN:
1406 case LAGG_PROTO_LOADBALANCE:
1407 case LAGG_PROTO_ETHERCHANNEL:
1409 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1410 speed += lp->lp_ifp->if_baudrate;
1411 sc->sc_ifp->if_baudrate = speed;
1413 case LAGG_PROTO_LACP:
1414 /* LACP updates if_baudrate itself */
1420 lagg_port_state(struct ifnet *ifp, int state)
1422 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1423 struct lagg_softc *sc = NULL;
1432 if (sc->sc_linkstate != NULL)
1433 (*sc->sc_linkstate)(lp);
1438 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1440 struct lagg_port *lp_next, *rval = NULL;
1441 // int new_link = LINK_STATE_DOWN;
1443 LAGG_RLOCK_ASSERT(sc);
1445 * Search a port which reports an active link state.
1450 if (LAGG_PORTACTIVE(lp)) {
1454 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1455 LAGG_PORTACTIVE(lp_next)) {
1461 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1462 if (LAGG_PORTACTIVE(lp_next)) {
1471 * The IEEE 802.1D standard assumes that a lagg with
1472 * multiple ports is always full duplex. This is valid
1473 * for load sharing laggs and if at least two links
1474 * are active. Unfortunately, checking the latter would
1475 * be too expensive at this point.
1477 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1479 new_link = LINK_STATE_FULL_DUPLEX;
1481 new_link = rval->lp_link_state;
1489 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1491 if (m->m_pkthdr.len < (off + len)) {
1493 } else if (m->m_len < (off + len)) {
1494 m_copydata(m, off, len, buf);
1497 return (mtod(m, char *) + off);
1501 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1506 struct ether_header *eh;
1507 const struct ether_vlan_header *vlan;
1509 const struct ip *ip;
1510 const uint32_t *ports;
1514 const struct ip6_hdr *ip6;
1524 struct ether_vlan_header vlan;
1532 eh = mtod(m, struct ether_header *);
1533 etype = ntohs(eh->ether_type);
1534 if (sc->sc_flags & LAGG_F_HASHL2) {
1535 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1536 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1539 /* Special handling for encapsulating VLAN frames */
1540 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1541 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1542 sizeof(m->m_pkthdr.ether_vtag), p);
1543 } else if (etype == ETHERTYPE_VLAN) {
1544 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1548 if (sc->sc_flags & LAGG_F_HASHL2)
1549 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1550 etype = ntohs(vlan->evl_proto);
1551 off += sizeof(*vlan) - sizeof(*eh);
1557 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1561 if (sc->sc_flags & LAGG_F_HASHL3) {
1562 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1563 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1565 if (!(sc->sc_flags & LAGG_F_HASHL4))
1571 iphlen = ip->ip_hl << 2;
1572 if (iphlen < sizeof(*ip))
1575 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1578 p = hash32_buf(ports, sizeof(*ports), p);
1584 case ETHERTYPE_IPV6:
1585 if (!(sc->sc_flags & LAGG_F_HASHL3))
1587 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1591 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1592 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1593 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1594 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1603 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1606 return (ifp->if_transmit)(ifp, m);
1610 * Simple round robin aggregation
1614 lagg_rr_attach(struct lagg_softc *sc)
1616 sc->sc_detach = lagg_rr_detach;
1617 sc->sc_start = lagg_rr_start;
1618 sc->sc_input = lagg_rr_input;
1619 sc->sc_port_create = NULL;
1620 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1627 lagg_rr_detach(struct lagg_softc *sc)
1633 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1635 struct lagg_port *lp;
1638 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1640 lp = SLIST_FIRST(&sc->sc_ports);
1642 lp = SLIST_NEXT(lp, lp_entries);
1645 * Check the port's link state. This will return the next active
1646 * port if the link is down or the port is NULL.
1648 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1654 return (lagg_enqueue(lp->lp_ifp, m));
1657 static struct mbuf *
1658 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1660 struct ifnet *ifp = sc->sc_ifp;
1662 /* Just pass in the packet to our lagg device */
1663 m->m_pkthdr.rcvif = ifp;
1673 lagg_fail_attach(struct lagg_softc *sc)
1675 sc->sc_detach = lagg_fail_detach;
1676 sc->sc_start = lagg_fail_start;
1677 sc->sc_input = lagg_fail_input;
1678 sc->sc_port_create = NULL;
1679 sc->sc_port_destroy = NULL;
1685 lagg_fail_detach(struct lagg_softc *sc)
1691 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1693 struct lagg_port *lp;
1695 /* Use the master port if active or the next available port */
1696 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1702 return (lagg_enqueue(lp->lp_ifp, m));
1705 static struct mbuf *
1706 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1708 struct ifnet *ifp = sc->sc_ifp;
1709 struct lagg_port *tmp_tp;
1711 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1712 m->m_pkthdr.rcvif = ifp;
1716 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1717 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1719 * If tmp_tp is null, we've recieved a packet when all
1720 * our links are down. Weird, but process it anyways.
1722 if ((tmp_tp == NULL || tmp_tp == lp)) {
1723 m->m_pkthdr.rcvif = ifp;
1737 lagg_lb_attach(struct lagg_softc *sc)
1739 struct lagg_port *lp;
1742 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1743 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1746 sc->sc_detach = lagg_lb_detach;
1747 sc->sc_start = lagg_lb_start;
1748 sc->sc_input = lagg_lb_input;
1749 sc->sc_port_create = lagg_lb_port_create;
1750 sc->sc_port_destroy = lagg_lb_port_destroy;
1751 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1753 lb->lb_key = arc4random();
1754 sc->sc_psc = (caddr_t)lb;
1756 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1757 lagg_lb_port_create(lp);
1763 lagg_lb_detach(struct lagg_softc *sc)
1765 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1772 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1774 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1775 struct lagg_port *lp_next;
1778 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1779 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1782 if (i >= LAGG_MAX_PORTS)
1784 if (sc->sc_ifflags & IFF_DEBUG)
1785 printf("%s: port %s at index %d\n",
1786 sc->sc_ifname, lp_next->lp_ifname, i);
1787 lb->lb_ports[i++] = lp_next;
1794 lagg_lb_port_create(struct lagg_port *lp)
1796 struct lagg_softc *sc = lp->lp_softc;
1797 return (lagg_lb_porttable(sc, NULL));
1801 lagg_lb_port_destroy(struct lagg_port *lp)
1803 struct lagg_softc *sc = lp->lp_softc;
1804 lagg_lb_porttable(sc, lp);
1808 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1810 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1811 struct lagg_port *lp = NULL;
1814 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1815 p = m->m_pkthdr.flowid;
1817 p = lagg_hashmbuf(sc, m, lb->lb_key);
1819 lp = lb->lb_ports[p];
1822 * Check the port's link state. This will return the next active
1823 * port if the link is down or the port is NULL.
1825 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1831 return (lagg_enqueue(lp->lp_ifp, m));
1834 static struct mbuf *
1835 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1837 struct ifnet *ifp = sc->sc_ifp;
1839 /* Just pass in the packet to our lagg device */
1840 m->m_pkthdr.rcvif = ifp;
1850 lagg_lacp_attach(struct lagg_softc *sc)
1852 struct lagg_port *lp;
1855 sc->sc_detach = lagg_lacp_detach;
1856 sc->sc_port_create = lacp_port_create;
1857 sc->sc_port_destroy = lacp_port_destroy;
1858 sc->sc_linkstate = lacp_linkstate;
1859 sc->sc_start = lagg_lacp_start;
1860 sc->sc_input = lagg_lacp_input;
1861 sc->sc_init = lacp_init;
1862 sc->sc_stop = lacp_stop;
1863 sc->sc_lladdr = lagg_lacp_lladdr;
1864 sc->sc_req = lacp_req;
1865 sc->sc_portreq = lacp_portreq;
1867 error = lacp_attach(sc);
1871 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1872 lacp_port_create(lp);
1878 lagg_lacp_detach(struct lagg_softc *sc)
1880 struct lagg_port *lp;
1883 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1884 lacp_port_destroy(lp);
1886 /* unlocking is safe here */
1888 error = lacp_detach(sc);
1895 lagg_lacp_lladdr(struct lagg_softc *sc)
1897 struct lagg_port *lp;
1899 /* purge all the lacp ports */
1900 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1901 lacp_port_destroy(lp);
1903 /* add them back in */
1904 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1905 lacp_port_create(lp);
1909 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1911 struct lagg_port *lp;
1913 lp = lacp_select_tx_port(sc, m);
1920 return (lagg_enqueue(lp->lp_ifp, m));
1923 static struct mbuf *
1924 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1926 struct ifnet *ifp = sc->sc_ifp;
1927 struct ether_header *eh;
1930 eh = mtod(m, struct ether_header *);
1931 etype = ntohs(eh->ether_type);
1933 /* Tap off LACP control messages */
1934 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1935 m = lacp_input(lp, m);
1941 * If the port is not collecting or not in the active aggregator then
1944 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1949 m->m_pkthdr.rcvif = ifp;