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/rmlock.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_media.h>
50 #include <net/if_types.h>
51 #include <net/if_var.h>
54 #if defined(INET) || defined(INET6)
55 #include <netinet/in.h>
56 #include <netinet/ip.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/if_ether.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/in6_ifattach.h>
69 #include <net/if_vlan_var.h>
70 #include <net/if_lagg.h>
71 #include <net/ieee8023ad_lacp.h>
73 /* Special flags we should propagate to the lagg ports. */
76 int (*func)(struct ifnet *, int);
78 {IFF_PROMISC, ifpromisc},
79 {IFF_ALLMULTI, if_allmulti},
83 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
84 static struct mtx lagg_list_mtx;
85 eventhandler_tag lagg_detach_cookie = NULL;
87 static int lagg_clone_create(struct if_clone *, int, caddr_t);
88 static void lagg_clone_destroy(struct ifnet *);
89 static struct if_clone *lagg_cloner;
90 static const char laggname[] = "lagg";
92 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
93 static void lagg_capabilities(struct lagg_softc *);
94 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
95 static void lagg_port_setlladdr(void *, int);
96 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
97 static int lagg_port_destroy(struct lagg_port *, int);
98 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
99 static void lagg_linkstate(struct lagg_softc *);
100 static void lagg_port_state(struct ifnet *, int);
101 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
102 static int lagg_port_output(struct ifnet *, struct mbuf *,
103 const struct sockaddr *, struct route *);
104 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
105 #ifdef LAGG_PORT_STACKING
106 static int lagg_port_checkstacking(struct lagg_softc *);
108 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
109 static void lagg_init(void *);
110 static void lagg_stop(struct lagg_softc *);
111 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
112 static int lagg_ether_setmulti(struct lagg_softc *);
113 static int lagg_ether_cmdmulti(struct lagg_port *, int);
114 static int lagg_setflag(struct lagg_port *, int, int,
115 int (*func)(struct ifnet *, int));
116 static int lagg_setflags(struct lagg_port *, int status);
117 static int lagg_transmit(struct ifnet *, struct mbuf *);
118 static void lagg_qflush(struct ifnet *);
119 static int lagg_media_change(struct ifnet *);
120 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
121 static struct lagg_port *lagg_link_active(struct lagg_softc *,
123 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
124 static int lagg_sysctl_active(SYSCTL_HANDLER_ARGS);
126 /* Simple round robin */
127 static int lagg_rr_attach(struct lagg_softc *);
128 static int lagg_rr_detach(struct lagg_softc *);
129 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
130 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
133 /* Active failover */
134 static int lagg_fail_attach(struct lagg_softc *);
135 static int lagg_fail_detach(struct lagg_softc *);
136 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
137 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
141 static int lagg_lb_attach(struct lagg_softc *);
142 static int lagg_lb_detach(struct lagg_softc *);
143 static int lagg_lb_port_create(struct lagg_port *);
144 static void lagg_lb_port_destroy(struct lagg_port *);
145 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
146 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
148 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
151 static int lagg_lacp_attach(struct lagg_softc *);
152 static int lagg_lacp_detach(struct lagg_softc *);
153 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
154 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
156 static void lagg_lacp_lladdr(struct lagg_softc *);
158 static void lagg_callout(void *);
160 /* lagg protocol table */
161 static const struct {
163 int (*ti_attach)(struct lagg_softc *);
165 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
166 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
167 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
168 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
169 { LAGG_PROTO_LACP, lagg_lacp_attach },
170 { LAGG_PROTO_NONE, NULL }
173 SYSCTL_DECL(_net_link);
174 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
177 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
178 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
179 &lagg_failover_rx_all, 0,
180 "Accept input from any interface in a failover lagg");
181 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
182 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
184 "Default setting for using flow id for load sharing");
185 static int def_flowid_shift = 16; /* Default value for using M_FLOWID */
186 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
187 &def_flowid_shift, 0,
188 "Default setting for flowid shift for load sharing");
191 lagg_modevent(module_t mod, int type, void *data)
196 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
197 SLIST_INIT(&lagg_list);
198 lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
199 lagg_clone_destroy, 0);
200 lagg_input_p = lagg_input;
201 lagg_linkstate_p = lagg_port_state;
202 lagg_detach_cookie = EVENTHANDLER_REGISTER(
203 ifnet_departure_event, lagg_port_ifdetach, NULL,
204 EVENTHANDLER_PRI_ANY);
207 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
209 if_clone_detach(lagg_cloner);
211 lagg_linkstate_p = NULL;
212 mtx_destroy(&lagg_list_mtx);
220 static moduledata_t lagg_mod = {
226 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
227 MODULE_VERSION(if_lagg, 1);
230 * This routine is run via an vlan
234 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
236 struct lagg_softc *sc = ifp->if_softc;
237 struct lagg_port *lp;
238 struct rm_priotracker tracker;
240 if (ifp->if_softc != arg) /* Not our event */
243 LAGG_RLOCK(sc, &tracker);
244 if (!SLIST_EMPTY(&sc->sc_ports)) {
245 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
246 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
248 LAGG_RUNLOCK(sc, &tracker);
252 * This routine is run via an vlan
256 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
258 struct lagg_softc *sc = ifp->if_softc;
259 struct lagg_port *lp;
260 struct rm_priotracker tracker;
262 if (ifp->if_softc != arg) /* Not our event */
265 LAGG_RLOCK(sc, &tracker);
266 if (!SLIST_EMPTY(&sc->sc_ports)) {
267 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
268 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
270 LAGG_RUNLOCK(sc, &tracker);
274 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
276 struct lagg_softc *sc;
279 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
280 struct sysctl_oid *oid;
281 char num[14]; /* sufficient for 32 bits */
283 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
284 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
290 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
291 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
292 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
293 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
295 sysctl_ctx_init(&sc->ctx);
296 snprintf(num, sizeof(num), "%u", unit);
297 sc->use_flowid = def_use_flowid;
298 sc->flowid_shift = def_flowid_shift;
299 sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
300 &SYSCTL_NODE_CHILDREN(_net_link, lagg),
301 OID_AUTO, num, CTLFLAG_RD, NULL, "");
302 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
303 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid,
304 sc->use_flowid, "Use flow id for load sharing");
305 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
306 "flowid_shift", CTLTYPE_INT|CTLFLAG_RW, &sc->flowid_shift,
308 "Shift flowid bits to prevent multiqueue collisions");
309 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
310 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
311 "Total number of ports");
312 SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
313 "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
314 "I", "Total number of active ports");
315 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
316 "flapping", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_flapping,
317 sc->sc_flapping, "Total number of port change events");
318 /* Hash all layers by default */
319 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
321 sc->sc_proto = LAGG_PROTO_NONE;
322 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
323 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
324 sc->sc_proto = lagg_protos[i].ti_proto;
325 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
334 LAGG_CALLOUT_LOCK_INIT(sc);
335 SLIST_INIT(&sc->sc_ports);
336 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
339 * This uses the callout lock rather than the rmlock; one can't
340 * hold said rmlock during SWI.
342 callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0);
344 /* Initialise pseudo media types */
345 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
347 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
348 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
350 if_initname(ifp, laggname, unit);
352 ifp->if_transmit = lagg_transmit;
353 ifp->if_qflush = lagg_qflush;
354 ifp->if_init = lagg_init;
355 ifp->if_ioctl = lagg_ioctl;
356 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
357 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
360 * Attach as an ordinary ethernet device, children will be attached
361 * as special device IFT_IEEE8023ADLAG.
363 ether_ifattach(ifp, eaddr);
365 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
366 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
367 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
368 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
370 /* Insert into the global list of laggs */
371 mtx_lock(&lagg_list_mtx);
372 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
373 mtx_unlock(&lagg_list_mtx);
375 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
381 lagg_clone_destroy(struct ifnet *ifp)
383 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
384 struct lagg_port *lp;
389 ifp->if_flags &= ~IFF_UP;
391 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
392 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
394 /* Shutdown and remove lagg ports */
395 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
396 lagg_port_destroy(lp, 1);
397 /* Unhook the aggregation protocol */
398 if (sc->sc_detach != NULL)
399 (*sc->sc_detach)(sc);
403 sysctl_ctx_free(&sc->ctx);
404 ifmedia_removeall(&sc->sc_media);
408 /* This grabs sc_callout_mtx, serialising it correctly */
409 callout_drain(&sc->sc_callout);
411 /* At this point it's drained; we can free this */
412 counter_u64_free(sc->sc_ipackets);
413 counter_u64_free(sc->sc_opackets);
414 counter_u64_free(sc->sc_ibytes);
415 counter_u64_free(sc->sc_obytes);
417 mtx_lock(&lagg_list_mtx);
418 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
419 mtx_unlock(&lagg_list_mtx);
421 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
422 LAGG_LOCK_DESTROY(sc);
423 LAGG_CALLOUT_LOCK_DESTROY(sc);
428 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
430 struct ifnet *ifp = sc->sc_ifp;
432 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
435 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
436 /* Let the protocol know the MAC has changed */
437 if (sc->sc_lladdr != NULL)
438 (*sc->sc_lladdr)(sc);
439 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
443 lagg_capabilities(struct lagg_softc *sc)
445 struct lagg_port *lp;
446 int cap = ~0, ena = ~0;
448 #if defined(INET) || defined(INET6)
449 u_int hw_tsomax = IP_MAXPACKET; /* Initialize to the maximum value. */
451 u_int hw_tsomax = ~0; /* if_hw_tsomax is only for INET/INET6, but.. */
454 LAGG_WLOCK_ASSERT(sc);
456 /* Get capabilities from the lagg ports */
457 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
458 cap &= lp->lp_ifp->if_capabilities;
459 ena &= lp->lp_ifp->if_capenable;
460 hwa &= lp->lp_ifp->if_hwassist;
461 /* Set to the minimum value of the lagg ports. */
462 if (lp->lp_ifp->if_hw_tsomax < hw_tsomax &&
463 lp->lp_ifp->if_hw_tsomax > 0)
464 hw_tsomax = lp->lp_ifp->if_hw_tsomax;
466 cap = (cap == ~0 ? 0 : cap);
467 ena = (ena == ~0 ? 0 : ena);
468 hwa = (hwa == ~0 ? 0 : hwa);
470 if (sc->sc_ifp->if_capabilities != cap ||
471 sc->sc_ifp->if_capenable != ena ||
472 sc->sc_ifp->if_hwassist != hwa ||
473 sc->sc_ifp->if_hw_tsomax != hw_tsomax) {
474 sc->sc_ifp->if_capabilities = cap;
475 sc->sc_ifp->if_capenable = ena;
476 sc->sc_ifp->if_hwassist = hwa;
477 sc->sc_ifp->if_hw_tsomax = hw_tsomax;
478 getmicrotime(&sc->sc_ifp->if_lastchange);
480 if (sc->sc_ifflags & IFF_DEBUG)
481 if_printf(sc->sc_ifp,
482 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
487 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
489 struct lagg_softc *sc = lp->lp_softc;
490 struct ifnet *ifp = lp->lp_ifp;
491 struct lagg_llq *llq;
494 LAGG_WLOCK_ASSERT(sc);
496 if (lp->lp_detaching ||
497 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
500 /* Check to make sure its not already queued to be changed */
501 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
502 if (llq->llq_ifp == ifp) {
509 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
510 if (llq == NULL) /* XXX what to do */
514 /* Update the lladdr even if pending, it may have changed */
516 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
519 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
521 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
525 * Set the interface MAC address from a taskqueue to avoid a LOR.
528 lagg_port_setlladdr(void *arg, int pending)
530 struct lagg_softc *sc = (struct lagg_softc *)arg;
531 struct lagg_llq *llq, *head;
535 /* Grab a local reference of the queue and remove it from the softc */
537 head = SLIST_FIRST(&sc->sc_llq_head);
538 SLIST_FIRST(&sc->sc_llq_head) = NULL;
542 * Traverse the queue and set the lladdr on each ifp. It is safe to do
543 * unlocked as we have the only reference to it.
545 for (llq = head; llq != NULL; llq = head) {
548 /* Set the link layer address */
549 CURVNET_SET(ifp->if_vnet);
550 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
553 printf("%s: setlladdr failed on %s\n", __func__,
556 head = SLIST_NEXT(llq, llq_entries);
562 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
564 struct lagg_softc *sc_ptr;
565 struct lagg_port *lp;
568 LAGG_WLOCK_ASSERT(sc);
570 /* Limit the maximal number of lagg ports */
571 if (sc->sc_count >= LAGG_MAX_PORTS)
574 /* Check if port has already been associated to a lagg */
575 if (ifp->if_lagg != NULL) {
576 /* Port is already in the current lagg? */
577 lp = (struct lagg_port *)ifp->if_lagg;
578 if (lp->lp_softc == sc)
583 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
584 if (ifp->if_type != IFT_ETHER)
585 return (EPROTONOSUPPORT);
589 * The member interface should not have inet6 address because
590 * two interfaces with a valid link-local scope zone must not be
591 * merged in any form. This restriction is needed to
592 * prevent violation of link-local scope zone. Attempts to
593 * add a member interface which has inet6 addresses triggers
594 * removal of all inet6 addresses on the member interface.
596 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
597 if (in6ifa_llaonifp(lp->lp_ifp)) {
598 in6_ifdetach(lp->lp_ifp);
599 if_printf(sc->sc_ifp,
600 "IPv6 addresses on %s have been removed "
601 "before adding it as a member to prevent "
602 "IPv6 address scope violation.\n",
603 lp->lp_ifp->if_xname);
606 if (in6ifa_llaonifp(ifp)) {
608 if_printf(sc->sc_ifp,
609 "IPv6 addresses on %s have been removed "
610 "before adding it as a member to prevent "
611 "IPv6 address scope violation.\n",
615 /* Allow the first Ethernet member to define the MTU */
616 if (SLIST_EMPTY(&sc->sc_ports))
617 sc->sc_ifp->if_mtu = ifp->if_mtu;
618 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
619 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
624 if ((lp = malloc(sizeof(struct lagg_port),
625 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
628 /* Check if port is a stacked lagg */
629 mtx_lock(&lagg_list_mtx);
630 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
631 if (ifp == sc_ptr->sc_ifp) {
632 mtx_unlock(&lagg_list_mtx);
635 /* XXX disable stacking for the moment, its untested */
636 #ifdef LAGG_PORT_STACKING
637 lp->lp_flags |= LAGG_PORT_STACK;
638 if (lagg_port_checkstacking(sc_ptr) >=
640 mtx_unlock(&lagg_list_mtx);
647 mtx_unlock(&lagg_list_mtx);
649 /* Change the interface type */
650 lp->lp_iftype = ifp->if_type;
651 ifp->if_type = IFT_IEEE8023ADLAG;
653 lp->lp_ioctl = ifp->if_ioctl;
654 ifp->if_ioctl = lagg_port_ioctl;
655 lp->lp_output = ifp->if_output;
656 ifp->if_output = lagg_port_output;
661 /* Save port link layer address */
662 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
664 if (SLIST_EMPTY(&sc->sc_ports)) {
666 lagg_lladdr(sc, IF_LLADDR(ifp));
668 /* Update link layer address for this port */
669 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
672 /* Insert into the list of ports */
673 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
676 /* Update lagg capabilities */
677 lagg_capabilities(sc);
680 /* Add multicast addresses and interface flags to this port */
681 lagg_ether_cmdmulti(lp, 1);
682 lagg_setflags(lp, 1);
684 if (sc->sc_port_create != NULL)
685 error = (*sc->sc_port_create)(lp);
687 /* remove the port again, without calling sc_port_destroy */
688 lagg_port_destroy(lp, 0);
695 #ifdef LAGG_PORT_STACKING
697 lagg_port_checkstacking(struct lagg_softc *sc)
699 struct lagg_softc *sc_ptr;
700 struct lagg_port *lp;
703 LAGG_WLOCK_ASSERT(sc);
705 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
706 if (lp->lp_flags & LAGG_PORT_STACK) {
707 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
708 m = MAX(m, lagg_port_checkstacking(sc_ptr));
717 lagg_port_destroy(struct lagg_port *lp, int runpd)
719 struct lagg_softc *sc = lp->lp_softc;
720 struct lagg_port *lp_ptr;
721 struct lagg_llq *llq;
722 struct ifnet *ifp = lp->lp_ifp;
724 LAGG_WLOCK_ASSERT(sc);
726 if (runpd && sc->sc_port_destroy != NULL)
727 (*sc->sc_port_destroy)(lp);
730 * Remove multicast addresses and interface flags from this port and
731 * reset the MAC address, skip if the interface is being detached.
733 if (!lp->lp_detaching) {
734 lagg_ether_cmdmulti(lp, 0);
735 lagg_setflags(lp, 0);
736 lagg_port_lladdr(lp, lp->lp_lladdr);
739 /* Restore interface */
740 ifp->if_type = lp->lp_iftype;
741 ifp->if_ioctl = lp->lp_ioctl;
742 ifp->if_output = lp->lp_output;
745 /* Finally, remove the port from the lagg */
746 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
749 /* Update the primary interface */
750 if (lp == sc->sc_primary) {
751 uint8_t lladdr[ETHER_ADDR_LEN];
753 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
754 bzero(&lladdr, ETHER_ADDR_LEN);
756 bcopy(lp_ptr->lp_lladdr,
757 lladdr, ETHER_ADDR_LEN);
759 lagg_lladdr(sc, lladdr);
760 sc->sc_primary = lp_ptr;
762 /* Update link layer address for each port */
763 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
764 lagg_port_lladdr(lp_ptr, lladdr);
767 /* Remove any pending lladdr changes from the queue */
768 if (lp->lp_detaching) {
769 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
770 if (llq->llq_ifp == ifp) {
771 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
774 break; /* Only appears once */
780 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
784 /* Update lagg capabilities */
785 lagg_capabilities(sc);
792 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
794 struct lagg_reqport *rp = (struct lagg_reqport *)data;
795 struct lagg_softc *sc;
796 struct lagg_port *lp = NULL;
798 struct rm_priotracker tracker;
800 /* Should be checked by the caller */
801 if (ifp->if_type != IFT_IEEE8023ADLAG ||
802 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
807 if (rp->rp_portname[0] == '\0' ||
808 ifunit(rp->rp_portname) != ifp) {
813 LAGG_RLOCK(sc, &tracker);
814 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
816 LAGG_RUNLOCK(sc, &tracker);
820 lagg_port2req(lp, rp);
821 LAGG_RUNLOCK(sc, &tracker);
825 if (lp->lp_ioctl == NULL) {
829 error = (*lp->lp_ioctl)(ifp, cmd, data);
833 /* Update lagg interface capabilities */
835 lagg_capabilities(sc);
840 /* Do not allow the MTU to be changed once joined */
851 if (lp->lp_ioctl != NULL)
852 return ((*lp->lp_ioctl)(ifp, cmd, data));
858 * For direct output to child ports.
861 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
862 const struct sockaddr *dst, struct route *ro)
864 struct lagg_port *lp = ifp->if_lagg;
866 switch (dst->sa_family) {
867 case pseudo_AF_HDRCMPLT:
869 return ((*lp->lp_output)(ifp, m, dst, ro));
872 /* drop any other frames */
878 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
880 struct lagg_port *lp;
881 struct lagg_softc *sc;
883 if ((lp = ifp->if_lagg) == NULL)
885 /* If the ifnet is just being renamed, don't do anything. */
886 if (ifp->if_flags & IFF_RENAMING)
892 lp->lp_detaching = 1;
893 lagg_port_destroy(lp, 1);
898 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
900 struct lagg_softc *sc = lp->lp_softc;
902 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
903 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
904 rp->rp_prio = lp->lp_prio;
905 rp->rp_flags = lp->lp_flags;
906 if (sc->sc_portreq != NULL)
907 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
909 /* Add protocol specific flags */
910 switch (sc->sc_proto) {
911 case LAGG_PROTO_FAILOVER:
912 if (lp == sc->sc_primary)
913 rp->rp_flags |= LAGG_PORT_MASTER;
914 if (lp == lagg_link_active(sc, sc->sc_primary))
915 rp->rp_flags |= LAGG_PORT_ACTIVE;
918 case LAGG_PROTO_ROUNDROBIN:
919 case LAGG_PROTO_LOADBALANCE:
920 case LAGG_PROTO_ETHERCHANNEL:
921 if (LAGG_PORTACTIVE(lp))
922 rp->rp_flags |= LAGG_PORT_ACTIVE;
925 case LAGG_PROTO_LACP:
926 /* LACP has a different definition of active */
927 if (lacp_isactive(lp))
928 rp->rp_flags |= LAGG_PORT_ACTIVE;
929 if (lacp_iscollecting(lp))
930 rp->rp_flags |= LAGG_PORT_COLLECTING;
931 if (lacp_isdistributing(lp))
932 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
941 struct lagg_softc *sc = (struct lagg_softc *)xsc;
942 struct lagg_port *lp;
943 struct ifnet *ifp = sc->sc_ifp;
945 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
950 ifp->if_drv_flags |= IFF_DRV_RUNNING;
951 /* Update the port lladdrs */
952 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
953 lagg_port_lladdr(lp, IF_LLADDR(ifp));
955 if (sc->sc_init != NULL)
962 lagg_stop(struct lagg_softc *sc)
964 struct ifnet *ifp = sc->sc_ifp;
966 LAGG_WLOCK_ASSERT(sc);
968 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
971 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
973 if (sc->sc_stop != NULL)
978 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
980 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
981 struct lagg_reqall *ra = (struct lagg_reqall *)data;
982 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
983 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
984 struct ifreq *ifr = (struct ifreq *)data;
985 struct lagg_port *lp;
987 struct thread *td = curthread;
989 int count, buflen, len, error = 0;
990 struct rm_priotracker tracker;
992 bzero(&rpbuf, sizeof(rpbuf));
996 LAGG_RLOCK(sc, &tracker);
998 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1000 buflen = count * sizeof(struct lagg_reqport);
1001 LAGG_RUNLOCK(sc, &tracker);
1003 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1005 LAGG_RLOCK(sc, &tracker);
1006 ra->ra_proto = sc->sc_proto;
1007 if (sc->sc_req != NULL)
1008 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
1012 len = min(ra->ra_size, buflen);
1013 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1014 if (len < sizeof(rpbuf))
1017 lagg_port2req(lp, &rpbuf);
1018 memcpy(buf, &rpbuf, sizeof(rpbuf));
1020 buf += sizeof(rpbuf);
1021 len -= sizeof(rpbuf);
1023 LAGG_RUNLOCK(sc, &tracker);
1024 ra->ra_ports = count;
1025 ra->ra_size = count * sizeof(rpbuf);
1026 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1027 free(outbuf, M_TEMP);
1030 error = priv_check(td, PRIV_NET_LAGG);
1033 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1034 error = EPROTONOSUPPORT;
1038 if (sc->sc_proto != LAGG_PROTO_NONE) {
1039 /* Reset protocol first in case detach unlocks */
1040 sc->sc_proto = LAGG_PROTO_NONE;
1041 error = sc->sc_detach(sc);
1042 sc->sc_detach = NULL;
1043 sc->sc_start = NULL;
1044 sc->sc_input = NULL;
1045 sc->sc_port_create = NULL;
1046 sc->sc_port_destroy = NULL;
1047 sc->sc_linkstate = NULL;
1050 sc->sc_lladdr = NULL;
1052 sc->sc_portreq = NULL;
1053 } else if (sc->sc_input != NULL) {
1054 /* Still detaching */
1061 for (int i = 0; i < (sizeof(lagg_protos) /
1062 sizeof(lagg_protos[0])); i++) {
1063 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1064 if (sc->sc_ifflags & IFF_DEBUG)
1065 printf("%s: using proto %u\n",
1067 lagg_protos[i].ti_proto);
1068 sc->sc_proto = lagg_protos[i].ti_proto;
1069 if (sc->sc_proto != LAGG_PROTO_NONE)
1070 error = lagg_protos[i].ti_attach(sc);
1076 error = EPROTONOSUPPORT;
1078 case SIOCGLAGGFLAGS:
1079 rf->rf_flags = sc->sc_flags;
1082 error = priv_check(td, PRIV_NET_LAGG);
1085 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1090 sc->sc_flags &= ~LAGG_F_HASHMASK;
1091 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1095 if (rp->rp_portname[0] == '\0' ||
1096 (tpif = ifunit(rp->rp_portname)) == NULL) {
1101 LAGG_RLOCK(sc, &tracker);
1102 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1103 lp->lp_softc != sc) {
1105 LAGG_RUNLOCK(sc, &tracker);
1109 lagg_port2req(lp, rp);
1110 LAGG_RUNLOCK(sc, &tracker);
1113 error = priv_check(td, PRIV_NET_LAGG);
1116 if (rp->rp_portname[0] == '\0' ||
1117 (tpif = ifunit(rp->rp_portname)) == NULL) {
1122 error = lagg_port_create(sc, tpif);
1125 case SIOCSLAGGDELPORT:
1126 error = priv_check(td, PRIV_NET_LAGG);
1129 if (rp->rp_portname[0] == '\0' ||
1130 (tpif = ifunit(rp->rp_portname)) == NULL) {
1136 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1137 lp->lp_softc != sc) {
1143 error = lagg_port_destroy(lp, 1);
1147 /* Set flags on ports too */
1149 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1150 lagg_setflags(lp, 1);
1154 if (!(ifp->if_flags & IFF_UP) &&
1155 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1157 * If interface is marked down and it is running,
1158 * then stop and disable it.
1163 } else if ((ifp->if_flags & IFF_UP) &&
1164 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1166 * If interface is marked up and it is stopped, then
1169 (*ifp->if_init)(sc);
1175 error = lagg_ether_setmulti(sc);
1180 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1185 /* Do not allow the MTU or caps to be directly changed */
1190 error = ether_ioctl(ifp, cmd, data);
1197 lagg_ether_setmulti(struct lagg_softc *sc)
1199 struct lagg_port *lp;
1201 LAGG_WLOCK_ASSERT(sc);
1203 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1204 /* First, remove any existing filter entries. */
1205 lagg_ether_cmdmulti(lp, 0);
1206 /* copy all addresses from the lagg interface to the port */
1207 lagg_ether_cmdmulti(lp, 1);
1213 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1215 struct lagg_softc *sc = lp->lp_softc;
1216 struct ifnet *ifp = lp->lp_ifp;
1217 struct ifnet *scifp = sc->sc_ifp;
1219 struct ifmultiaddr *ifma;
1222 LAGG_WLOCK_ASSERT(sc);
1225 IF_ADDR_WLOCK(scifp);
1226 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1227 if (ifma->ifma_addr->sa_family != AF_LINK)
1229 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1231 IF_ADDR_WUNLOCK(scifp);
1234 bcopy(ifma->ifma_addr, &mc->mc_addr,
1235 ifma->ifma_addr->sa_len);
1236 mc->mc_addr.sdl_index = ifp->if_index;
1238 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1240 IF_ADDR_WUNLOCK(scifp);
1241 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1242 error = if_addmulti(ifp,
1243 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1248 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1249 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1250 if (mc->mc_ifma && !lp->lp_detaching)
1251 if_delmulti_ifma(mc->mc_ifma);
1258 /* Handle a ref counted flag that should be set on the lagg port as well */
1260 lagg_setflag(struct lagg_port *lp, int flag, int status,
1261 int (*func)(struct ifnet *, int))
1263 struct lagg_softc *sc = lp->lp_softc;
1264 struct ifnet *scifp = sc->sc_ifp;
1265 struct ifnet *ifp = lp->lp_ifp;
1268 LAGG_WLOCK_ASSERT(sc);
1270 status = status ? (scifp->if_flags & flag) : 0;
1271 /* Now "status" contains the flag value or 0 */
1274 * See if recorded ports status is different from what
1275 * we want it to be. If it is, flip it. We record ports
1276 * status in lp_ifflags so that we won't clear ports flag
1277 * we haven't set. In fact, we don't clear or set ports
1278 * flags directly, but get or release references to them.
1279 * That's why we can be sure that recorded flags still are
1280 * in accord with actual ports flags.
1282 if (status != (lp->lp_ifflags & flag)) {
1283 error = (*func)(ifp, status);
1286 lp->lp_ifflags &= ~flag;
1287 lp->lp_ifflags |= status;
1293 * Handle IFF_* flags that require certain changes on the lagg port
1294 * if "status" is true, update ports flags respective to the lagg
1295 * if "status" is false, forcedly clear the flags set on port.
1298 lagg_setflags(struct lagg_port *lp, int status)
1302 for (i = 0; lagg_pflags[i].flag; i++) {
1303 error = lagg_setflag(lp, lagg_pflags[i].flag,
1304 status, lagg_pflags[i].func);
1312 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1314 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1315 int error, len, mcast;
1316 struct rm_priotracker tracker;
1318 len = m->m_pkthdr.len;
1319 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1321 LAGG_RLOCK(sc, &tracker);
1322 /* We need a Tx algorithm and at least one port */
1323 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1324 LAGG_RUNLOCK(sc, &tracker);
1330 ETHER_BPF_MTAP(ifp, m);
1332 error = (*sc->sc_start)(sc, m);
1333 LAGG_RUNLOCK(sc, &tracker);
1336 counter_u64_add(sc->sc_opackets, 1);
1337 counter_u64_add(sc->sc_obytes, len);
1338 ifp->if_omcasts += mcast;
1346 * The ifp->if_qflush entry point for lagg(4) is no-op.
1349 lagg_qflush(struct ifnet *ifp __unused)
1353 static struct mbuf *
1354 lagg_input(struct ifnet *ifp, struct mbuf *m)
1356 struct lagg_port *lp = ifp->if_lagg;
1357 struct lagg_softc *sc = lp->lp_softc;
1358 struct ifnet *scifp = sc->sc_ifp;
1359 struct rm_priotracker tracker;
1361 LAGG_RLOCK(sc, &tracker);
1362 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1363 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1364 sc->sc_proto == LAGG_PROTO_NONE) {
1365 LAGG_RUNLOCK(sc, &tracker);
1370 ETHER_BPF_MTAP(scifp, m);
1372 m = (*sc->sc_input)(sc, lp, m);
1375 counter_u64_add(sc->sc_ipackets, 1);
1376 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1378 if (scifp->if_flags & IFF_MONITOR) {
1384 LAGG_RUNLOCK(sc, &tracker);
1389 lagg_media_change(struct ifnet *ifp)
1391 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1393 if (sc->sc_ifflags & IFF_DEBUG)
1394 printf("%s\n", __func__);
1401 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1403 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1404 struct lagg_port *lp;
1405 struct rm_priotracker tracker;
1407 imr->ifm_status = IFM_AVALID;
1408 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1410 LAGG_RLOCK(sc, &tracker);
1411 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1412 if (LAGG_PORTACTIVE(lp))
1413 imr->ifm_status |= IFM_ACTIVE;
1415 LAGG_RUNLOCK(sc, &tracker);
1419 lagg_linkstate(struct lagg_softc *sc)
1421 struct lagg_port *lp;
1422 int new_link = LINK_STATE_DOWN;
1425 /* Our link is considered up if at least one of our ports is active */
1426 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1427 if (lp->lp_link_state == LINK_STATE_UP) {
1428 new_link = LINK_STATE_UP;
1432 if_link_state_change(sc->sc_ifp, new_link);
1434 /* Update if_baudrate to reflect the max possible speed */
1435 switch (sc->sc_proto) {
1436 case LAGG_PROTO_FAILOVER:
1437 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1438 sc->sc_primary->lp_ifp->if_baudrate : 0;
1440 case LAGG_PROTO_ROUNDROBIN:
1441 case LAGG_PROTO_LOADBALANCE:
1442 case LAGG_PROTO_ETHERCHANNEL:
1444 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1445 speed += lp->lp_ifp->if_baudrate;
1446 sc->sc_ifp->if_baudrate = speed;
1448 case LAGG_PROTO_LACP:
1449 /* LACP updates if_baudrate itself */
1455 lagg_port_state(struct ifnet *ifp, int state)
1457 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1458 struct lagg_softc *sc = NULL;
1467 if (sc->sc_linkstate != NULL)
1468 (*sc->sc_linkstate)(lp);
1473 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1475 struct lagg_port *lp_next, *rval = NULL;
1476 // int new_link = LINK_STATE_DOWN;
1478 LAGG_RLOCK_ASSERT(sc);
1480 * Search a port which reports an active link state.
1485 if (LAGG_PORTACTIVE(lp)) {
1489 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1490 LAGG_PORTACTIVE(lp_next)) {
1496 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1497 if (LAGG_PORTACTIVE(lp_next)) {
1506 * The IEEE 802.1D standard assumes that a lagg with
1507 * multiple ports is always full duplex. This is valid
1508 * for load sharing laggs and if at least two links
1509 * are active. Unfortunately, checking the latter would
1510 * be too expensive at this point.
1512 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1514 new_link = LINK_STATE_FULL_DUPLEX;
1516 new_link = rval->lp_link_state;
1524 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1526 if (m->m_pkthdr.len < (off + len)) {
1528 } else if (m->m_len < (off + len)) {
1529 m_copydata(m, off, len, buf);
1532 return (mtod(m, char *) + off);
1536 lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1538 struct lagg_softc *sc = (struct lagg_softc *)arg1;
1539 struct lagg_port *lp;
1542 /* LACP tracks active links automatically, the others do not */
1543 if (sc->sc_proto != LAGG_PROTO_LACP) {
1545 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1546 sc->sc_active += LAGG_PORTACTIVE(lp);
1549 error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1550 if ((error) || (req->newptr == NULL))
1557 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1562 struct ether_header *eh;
1563 const struct ether_vlan_header *vlan;
1565 const struct ip *ip;
1566 const uint32_t *ports;
1570 const struct ip6_hdr *ip6;
1580 struct ether_vlan_header vlan;
1588 eh = mtod(m, struct ether_header *);
1589 etype = ntohs(eh->ether_type);
1590 if (sc->sc_flags & LAGG_F_HASHL2) {
1591 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1592 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1595 /* Special handling for encapsulating VLAN frames */
1596 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1597 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1598 sizeof(m->m_pkthdr.ether_vtag), p);
1599 } else if (etype == ETHERTYPE_VLAN) {
1600 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1604 if (sc->sc_flags & LAGG_F_HASHL2)
1605 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1606 etype = ntohs(vlan->evl_proto);
1607 off += sizeof(*vlan) - sizeof(*eh);
1613 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1617 if (sc->sc_flags & LAGG_F_HASHL3) {
1618 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1619 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1621 if (!(sc->sc_flags & LAGG_F_HASHL4))
1627 iphlen = ip->ip_hl << 2;
1628 if (iphlen < sizeof(*ip))
1631 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1634 p = hash32_buf(ports, sizeof(*ports), p);
1640 case ETHERTYPE_IPV6:
1641 if (!(sc->sc_flags & LAGG_F_HASHL3))
1643 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1647 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1648 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1649 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1650 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1659 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1662 return (ifp->if_transmit)(ifp, m);
1666 * Simple round robin aggregation
1670 lagg_rr_attach(struct lagg_softc *sc)
1672 sc->sc_detach = lagg_rr_detach;
1673 sc->sc_start = lagg_rr_start;
1674 sc->sc_input = lagg_rr_input;
1675 sc->sc_port_create = NULL;
1676 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1683 lagg_rr_detach(struct lagg_softc *sc)
1689 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1691 struct lagg_port *lp;
1694 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1696 lp = SLIST_FIRST(&sc->sc_ports);
1698 lp = SLIST_NEXT(lp, lp_entries);
1701 * Check the port's link state. This will return the next active
1702 * port if the link is down or the port is NULL.
1704 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1710 return (lagg_enqueue(lp->lp_ifp, m));
1713 static struct mbuf *
1714 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1716 struct ifnet *ifp = sc->sc_ifp;
1718 /* Just pass in the packet to our lagg device */
1719 m->m_pkthdr.rcvif = ifp;
1729 lagg_fail_attach(struct lagg_softc *sc)
1731 sc->sc_detach = lagg_fail_detach;
1732 sc->sc_start = lagg_fail_start;
1733 sc->sc_input = lagg_fail_input;
1734 sc->sc_port_create = NULL;
1735 sc->sc_port_destroy = NULL;
1741 lagg_fail_detach(struct lagg_softc *sc)
1747 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1749 struct lagg_port *lp;
1751 /* Use the master port if active or the next available port */
1752 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1758 return (lagg_enqueue(lp->lp_ifp, m));
1761 static struct mbuf *
1762 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1764 struct ifnet *ifp = sc->sc_ifp;
1765 struct lagg_port *tmp_tp;
1767 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1768 m->m_pkthdr.rcvif = ifp;
1772 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1773 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1775 * If tmp_tp is null, we've recieved a packet when all
1776 * our links are down. Weird, but process it anyways.
1778 if ((tmp_tp == NULL || tmp_tp == lp)) {
1779 m->m_pkthdr.rcvif = ifp;
1793 lagg_lb_attach(struct lagg_softc *sc)
1795 struct lagg_port *lp;
1798 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1799 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1802 sc->sc_detach = lagg_lb_detach;
1803 sc->sc_start = lagg_lb_start;
1804 sc->sc_input = lagg_lb_input;
1805 sc->sc_port_create = lagg_lb_port_create;
1806 sc->sc_port_destroy = lagg_lb_port_destroy;
1807 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1809 lb->lb_key = arc4random();
1810 sc->sc_psc = (caddr_t)lb;
1812 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1813 lagg_lb_port_create(lp);
1819 lagg_lb_detach(struct lagg_softc *sc)
1821 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1828 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1830 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1831 struct lagg_port *lp_next;
1834 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1835 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1838 if (i >= LAGG_MAX_PORTS)
1840 if (sc->sc_ifflags & IFF_DEBUG)
1841 printf("%s: port %s at index %d\n",
1842 sc->sc_ifname, lp_next->lp_ifname, i);
1843 lb->lb_ports[i++] = lp_next;
1850 lagg_lb_port_create(struct lagg_port *lp)
1852 struct lagg_softc *sc = lp->lp_softc;
1853 return (lagg_lb_porttable(sc, NULL));
1857 lagg_lb_port_destroy(struct lagg_port *lp)
1859 struct lagg_softc *sc = lp->lp_softc;
1860 lagg_lb_porttable(sc, lp);
1864 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1866 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1867 struct lagg_port *lp = NULL;
1870 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1871 p = m->m_pkthdr.flowid >> sc->flowid_shift;
1873 p = lagg_hashmbuf(sc, m, lb->lb_key);
1875 lp = lb->lb_ports[p];
1878 * Check the port's link state. This will return the next active
1879 * port if the link is down or the port is NULL.
1881 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1887 return (lagg_enqueue(lp->lp_ifp, m));
1890 static struct mbuf *
1891 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1893 struct ifnet *ifp = sc->sc_ifp;
1895 /* Just pass in the packet to our lagg device */
1896 m->m_pkthdr.rcvif = ifp;
1906 lagg_lacp_attach(struct lagg_softc *sc)
1908 struct lagg_port *lp;
1911 sc->sc_detach = lagg_lacp_detach;
1912 sc->sc_port_create = lacp_port_create;
1913 sc->sc_port_destroy = lacp_port_destroy;
1914 sc->sc_linkstate = lacp_linkstate;
1915 sc->sc_start = lagg_lacp_start;
1916 sc->sc_input = lagg_lacp_input;
1917 sc->sc_init = lacp_init;
1918 sc->sc_stop = lacp_stop;
1919 sc->sc_lladdr = lagg_lacp_lladdr;
1920 sc->sc_req = lacp_req;
1921 sc->sc_portreq = lacp_portreq;
1923 error = lacp_attach(sc);
1927 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1928 lacp_port_create(lp);
1934 lagg_lacp_detach(struct lagg_softc *sc)
1936 struct lagg_port *lp;
1939 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1940 lacp_port_destroy(lp);
1942 /* unlocking is safe here */
1944 error = lacp_detach(sc);
1951 lagg_lacp_lladdr(struct lagg_softc *sc)
1953 struct lagg_port *lp;
1955 /* purge all the lacp ports */
1956 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1957 lacp_port_destroy(lp);
1959 /* add them back in */
1960 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1961 lacp_port_create(lp);
1965 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1967 struct lagg_port *lp;
1969 lp = lacp_select_tx_port(sc, m);
1976 return (lagg_enqueue(lp->lp_ifp, m));
1979 static struct mbuf *
1980 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1982 struct ifnet *ifp = sc->sc_ifp;
1983 struct ether_header *eh;
1986 eh = mtod(m, struct ether_header *);
1987 etype = ntohs(eh->ether_type);
1989 /* Tap off LACP control messages */
1990 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1991 m = lacp_input(lp, m);
1997 * If the port is not collecting or not in the active aggregator then
2000 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2005 m->m_pkthdr.rcvif = ifp;
2010 lagg_callout(void *arg)
2012 struct lagg_softc *sc = (struct lagg_softc *)arg;
2013 struct ifnet *ifp = sc->sc_ifp;
2015 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
2016 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
2017 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
2018 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2020 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);