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_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
55 #if defined(INET) || defined(INET6)
56 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_ifattach.h>
70 #include <net/if_vlan_var.h>
71 #include <net/if_lagg.h>
72 #include <net/ieee8023ad_lacp.h>
74 /* Special flags we should propagate to the lagg ports. */
77 int (*func)(struct ifnet *, int);
79 {IFF_PROMISC, ifpromisc},
80 {IFF_ALLMULTI, if_allmulti},
84 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
85 static struct mtx lagg_list_mtx;
86 eventhandler_tag lagg_detach_cookie = NULL;
88 static int lagg_clone_create(struct if_clone *, int, caddr_t);
89 static void lagg_clone_destroy(struct ifnet *);
90 static struct if_clone *lagg_cloner;
91 static const char laggname[] = "lagg";
93 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
94 static void lagg_capabilities(struct lagg_softc *);
95 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
96 static void lagg_port_setlladdr(void *, int);
97 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
98 static int lagg_port_destroy(struct lagg_port *, int);
99 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
100 static void lagg_linkstate(struct lagg_softc *);
101 static void lagg_port_state(struct ifnet *, int);
102 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
103 static int lagg_port_output(struct ifnet *, struct mbuf *,
104 const struct sockaddr *, struct route *);
105 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
106 #ifdef LAGG_PORT_STACKING
107 static int lagg_port_checkstacking(struct lagg_softc *);
109 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
110 static void lagg_init(void *);
111 static void lagg_stop(struct lagg_softc *);
112 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
113 static int lagg_ether_setmulti(struct lagg_softc *);
114 static int lagg_ether_cmdmulti(struct lagg_port *, int);
115 static int lagg_setflag(struct lagg_port *, int, int,
116 int (*func)(struct ifnet *, int));
117 static int lagg_setflags(struct lagg_port *, int status);
118 static int lagg_transmit(struct ifnet *, struct mbuf *);
119 static void lagg_qflush(struct ifnet *);
120 static int lagg_media_change(struct ifnet *);
121 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
122 static struct lagg_port *lagg_link_active(struct lagg_softc *,
124 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
125 static int lagg_sysctl_active(SYSCTL_HANDLER_ARGS);
127 /* Simple round robin */
128 static int lagg_rr_attach(struct lagg_softc *);
129 static int lagg_rr_detach(struct lagg_softc *);
130 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
131 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
134 /* Active failover */
135 static int lagg_fail_attach(struct lagg_softc *);
136 static int lagg_fail_detach(struct lagg_softc *);
137 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
138 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
142 static int lagg_lb_attach(struct lagg_softc *);
143 static int lagg_lb_detach(struct lagg_softc *);
144 static int lagg_lb_port_create(struct lagg_port *);
145 static void lagg_lb_port_destroy(struct lagg_port *);
146 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
147 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
149 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
152 static int lagg_lacp_attach(struct lagg_softc *);
153 static int lagg_lacp_detach(struct lagg_softc *);
154 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
155 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
157 static void lagg_lacp_lladdr(struct lagg_softc *);
159 static void lagg_callout(void *);
161 /* lagg protocol table */
162 static const struct {
164 int (*ti_attach)(struct lagg_softc *);
166 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
167 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
168 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
169 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
170 { LAGG_PROTO_LACP, lagg_lacp_attach },
171 { LAGG_PROTO_NONE, NULL }
174 SYSCTL_DECL(_net_link);
175 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
178 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
179 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
180 &lagg_failover_rx_all, 0,
181 "Accept input from any interface in a failover lagg");
182 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
183 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
184 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
186 "Default setting for using flow id for load sharing");
189 lagg_modevent(module_t mod, int type, void *data)
194 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
195 SLIST_INIT(&lagg_list);
196 lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
197 lagg_clone_destroy, 0);
198 lagg_input_p = lagg_input;
199 lagg_linkstate_p = lagg_port_state;
200 lagg_detach_cookie = EVENTHANDLER_REGISTER(
201 ifnet_departure_event, lagg_port_ifdetach, NULL,
202 EVENTHANDLER_PRI_ANY);
205 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
207 if_clone_detach(lagg_cloner);
209 lagg_linkstate_p = NULL;
210 mtx_destroy(&lagg_list_mtx);
218 static moduledata_t lagg_mod = {
224 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
225 MODULE_VERSION(if_lagg, 1);
228 * This routine is run via an vlan
232 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
234 struct lagg_softc *sc = ifp->if_softc;
235 struct lagg_port *lp;
236 struct rm_priotracker tracker;
238 if (ifp->if_softc != arg) /* Not our event */
241 LAGG_RLOCK(sc, &tracker);
242 if (!SLIST_EMPTY(&sc->sc_ports)) {
243 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
244 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
246 LAGG_RUNLOCK(sc, &tracker);
250 * This routine is run via an vlan
254 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
256 struct lagg_softc *sc = ifp->if_softc;
257 struct lagg_port *lp;
258 struct rm_priotracker tracker;
260 if (ifp->if_softc != arg) /* Not our event */
263 LAGG_RLOCK(sc, &tracker);
264 if (!SLIST_EMPTY(&sc->sc_ports)) {
265 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
266 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
268 LAGG_RUNLOCK(sc, &tracker);
272 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
274 struct lagg_softc *sc;
277 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
278 struct sysctl_oid *oid;
279 char num[14]; /* sufficient for 32 bits */
281 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
282 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
288 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
289 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
290 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
291 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
293 sysctl_ctx_init(&sc->ctx);
294 snprintf(num, sizeof(num), "%u", unit);
295 sc->use_flowid = def_use_flowid;
296 sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
297 &SYSCTL_NODE_CHILDREN(_net_link, lagg),
298 OID_AUTO, num, CTLFLAG_RD, NULL, "");
299 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
300 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
301 "Use flow id for load sharing");
302 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
303 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
304 "Total number of ports");
305 SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
306 "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
307 "I", "Total number of active ports");
308 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
309 "flapping", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_flapping,
310 sc->sc_flapping, "Total number of port change events");
311 /* Hash all layers by default */
312 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
314 sc->sc_proto = LAGG_PROTO_NONE;
315 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
316 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
317 sc->sc_proto = lagg_protos[i].ti_proto;
318 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
327 LAGG_CALLOUT_LOCK_INIT(sc);
328 SLIST_INIT(&sc->sc_ports);
329 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
332 * This uses the callout lock rather than the rmlock; one can't
333 * hold said rmlock during SWI.
335 callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0);
337 /* Initialise pseudo media types */
338 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
340 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
341 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
343 if_initname(ifp, laggname, unit);
345 ifp->if_transmit = lagg_transmit;
346 ifp->if_qflush = lagg_qflush;
347 ifp->if_init = lagg_init;
348 ifp->if_ioctl = lagg_ioctl;
349 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
352 * Attach as an ordinary ethernet device, children will be attached
353 * as special device IFT_IEEE8023ADLAG.
355 ether_ifattach(ifp, eaddr);
357 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
358 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
359 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
360 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
362 /* Insert into the global list of laggs */
363 mtx_lock(&lagg_list_mtx);
364 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
365 mtx_unlock(&lagg_list_mtx);
367 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
373 lagg_clone_destroy(struct ifnet *ifp)
375 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
376 struct lagg_port *lp;
381 ifp->if_flags &= ~IFF_UP;
383 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
384 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
386 /* Shutdown and remove lagg ports */
387 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
388 lagg_port_destroy(lp, 1);
389 /* Unhook the aggregation protocol */
390 if (sc->sc_detach != NULL)
391 (*sc->sc_detach)(sc);
395 sysctl_ctx_free(&sc->ctx);
396 ifmedia_removeall(&sc->sc_media);
400 /* This grabs sc_callout_mtx, serialising it correctly */
401 callout_drain(&sc->sc_callout);
403 /* At this point it's drained; we can free this */
404 counter_u64_free(sc->sc_ipackets);
405 counter_u64_free(sc->sc_opackets);
406 counter_u64_free(sc->sc_ibytes);
407 counter_u64_free(sc->sc_obytes);
409 mtx_lock(&lagg_list_mtx);
410 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
411 mtx_unlock(&lagg_list_mtx);
413 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
414 LAGG_LOCK_DESTROY(sc);
415 LAGG_CALLOUT_LOCK_DESTROY(sc);
420 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
422 struct ifnet *ifp = sc->sc_ifp;
424 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
427 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
428 /* Let the protocol know the MAC has changed */
429 if (sc->sc_lladdr != NULL)
430 (*sc->sc_lladdr)(sc);
431 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
435 lagg_capabilities(struct lagg_softc *sc)
437 struct lagg_port *lp;
438 int cap = ~0, ena = ~0;
441 LAGG_WLOCK_ASSERT(sc);
443 /* Get capabilities from the lagg ports */
444 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
445 cap &= lp->lp_ifp->if_capabilities;
446 ena &= lp->lp_ifp->if_capenable;
447 hwa &= lp->lp_ifp->if_hwassist;
449 cap = (cap == ~0 ? 0 : cap);
450 ena = (ena == ~0 ? 0 : ena);
451 hwa = (hwa == ~0 ? 0 : hwa);
453 if (sc->sc_ifp->if_capabilities != cap ||
454 sc->sc_ifp->if_capenable != ena ||
455 sc->sc_ifp->if_hwassist != hwa) {
456 sc->sc_ifp->if_capabilities = cap;
457 sc->sc_ifp->if_capenable = ena;
458 sc->sc_ifp->if_hwassist = hwa;
459 getmicrotime(&sc->sc_ifp->if_lastchange);
461 if (sc->sc_ifflags & IFF_DEBUG)
462 if_printf(sc->sc_ifp,
463 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
468 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
470 struct lagg_softc *sc = lp->lp_softc;
471 struct ifnet *ifp = lp->lp_ifp;
472 struct lagg_llq *llq;
475 LAGG_WLOCK_ASSERT(sc);
477 if (lp->lp_detaching ||
478 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
481 /* Check to make sure its not already queued to be changed */
482 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
483 if (llq->llq_ifp == ifp) {
490 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
491 if (llq == NULL) /* XXX what to do */
495 /* Update the lladdr even if pending, it may have changed */
497 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
500 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
502 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
506 * Set the interface MAC address from a taskqueue to avoid a LOR.
509 lagg_port_setlladdr(void *arg, int pending)
511 struct lagg_softc *sc = (struct lagg_softc *)arg;
512 struct lagg_llq *llq, *head;
516 /* Grab a local reference of the queue and remove it from the softc */
518 head = SLIST_FIRST(&sc->sc_llq_head);
519 SLIST_FIRST(&sc->sc_llq_head) = NULL;
523 * Traverse the queue and set the lladdr on each ifp. It is safe to do
524 * unlocked as we have the only reference to it.
526 for (llq = head; llq != NULL; llq = head) {
529 /* Set the link layer address */
530 CURVNET_SET(ifp->if_vnet);
531 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
534 printf("%s: setlladdr failed on %s\n", __func__,
537 head = SLIST_NEXT(llq, llq_entries);
543 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
545 struct lagg_softc *sc_ptr;
546 struct lagg_port *lp;
549 LAGG_WLOCK_ASSERT(sc);
551 /* Limit the maximal number of lagg ports */
552 if (sc->sc_count >= LAGG_MAX_PORTS)
555 /* Check if port has already been associated to a lagg */
556 if (ifp->if_lagg != NULL) {
557 /* Port is already in the current lagg? */
558 lp = (struct lagg_port *)ifp->if_lagg;
559 if (lp->lp_softc == sc)
564 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
565 if (ifp->if_type != IFT_ETHER)
566 return (EPROTONOSUPPORT);
570 * The member interface should not have inet6 address because
571 * two interfaces with a valid link-local scope zone must not be
572 * merged in any form. This restriction is needed to
573 * prevent violation of link-local scope zone. Attempts to
574 * add a member interface which has inet6 addresses triggers
575 * removal of all inet6 addresses on the member interface.
577 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
578 if (in6ifa_llaonifp(lp->lp_ifp)) {
579 in6_ifdetach(lp->lp_ifp);
580 if_printf(sc->sc_ifp,
581 "IPv6 addresses on %s have been removed "
582 "before adding it as a member to prevent "
583 "IPv6 address scope violation.\n",
584 lp->lp_ifp->if_xname);
587 if (in6ifa_llaonifp(ifp)) {
589 if_printf(sc->sc_ifp,
590 "IPv6 addresses on %s have been removed "
591 "before adding it as a member to prevent "
592 "IPv6 address scope violation.\n",
596 /* Allow the first Ethernet member to define the MTU */
597 if (SLIST_EMPTY(&sc->sc_ports))
598 sc->sc_ifp->if_mtu = ifp->if_mtu;
599 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
600 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
605 if ((lp = malloc(sizeof(struct lagg_port),
606 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
609 /* Check if port is a stacked lagg */
610 mtx_lock(&lagg_list_mtx);
611 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
612 if (ifp == sc_ptr->sc_ifp) {
613 mtx_unlock(&lagg_list_mtx);
616 /* XXX disable stacking for the moment, its untested */
617 #ifdef LAGG_PORT_STACKING
618 lp->lp_flags |= LAGG_PORT_STACK;
619 if (lagg_port_checkstacking(sc_ptr) >=
621 mtx_unlock(&lagg_list_mtx);
628 mtx_unlock(&lagg_list_mtx);
630 /* Change the interface type */
631 lp->lp_iftype = ifp->if_type;
632 ifp->if_type = IFT_IEEE8023ADLAG;
634 lp->lp_ioctl = ifp->if_ioctl;
635 ifp->if_ioctl = lagg_port_ioctl;
636 lp->lp_output = ifp->if_output;
637 ifp->if_output = lagg_port_output;
642 /* Save port link layer address */
643 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
645 if (SLIST_EMPTY(&sc->sc_ports)) {
647 lagg_lladdr(sc, IF_LLADDR(ifp));
649 /* Update link layer address for this port */
650 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
653 /* Insert into the list of ports */
654 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
657 /* Update lagg capabilities */
658 lagg_capabilities(sc);
661 /* Add multicast addresses and interface flags to this port */
662 lagg_ether_cmdmulti(lp, 1);
663 lagg_setflags(lp, 1);
665 if (sc->sc_port_create != NULL)
666 error = (*sc->sc_port_create)(lp);
668 /* remove the port again, without calling sc_port_destroy */
669 lagg_port_destroy(lp, 0);
676 #ifdef LAGG_PORT_STACKING
678 lagg_port_checkstacking(struct lagg_softc *sc)
680 struct lagg_softc *sc_ptr;
681 struct lagg_port *lp;
684 LAGG_WLOCK_ASSERT(sc);
686 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
687 if (lp->lp_flags & LAGG_PORT_STACK) {
688 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
689 m = MAX(m, lagg_port_checkstacking(sc_ptr));
698 lagg_port_destroy(struct lagg_port *lp, int runpd)
700 struct lagg_softc *sc = lp->lp_softc;
701 struct lagg_port *lp_ptr;
702 struct lagg_llq *llq;
703 struct ifnet *ifp = lp->lp_ifp;
705 LAGG_WLOCK_ASSERT(sc);
707 if (runpd && sc->sc_port_destroy != NULL)
708 (*sc->sc_port_destroy)(lp);
711 * Remove multicast addresses and interface flags from this port and
712 * reset the MAC address, skip if the interface is being detached.
714 if (!lp->lp_detaching) {
715 lagg_ether_cmdmulti(lp, 0);
716 lagg_setflags(lp, 0);
717 lagg_port_lladdr(lp, lp->lp_lladdr);
720 /* Restore interface */
721 ifp->if_type = lp->lp_iftype;
722 ifp->if_ioctl = lp->lp_ioctl;
723 ifp->if_output = lp->lp_output;
726 /* Finally, remove the port from the lagg */
727 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
730 /* Update the primary interface */
731 if (lp == sc->sc_primary) {
732 uint8_t lladdr[ETHER_ADDR_LEN];
734 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
735 bzero(&lladdr, ETHER_ADDR_LEN);
737 bcopy(lp_ptr->lp_lladdr,
738 lladdr, ETHER_ADDR_LEN);
740 lagg_lladdr(sc, lladdr);
741 sc->sc_primary = lp_ptr;
743 /* Update link layer address for each port */
744 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
745 lagg_port_lladdr(lp_ptr, lladdr);
748 /* Remove any pending lladdr changes from the queue */
749 if (lp->lp_detaching) {
750 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
751 if (llq->llq_ifp == ifp) {
752 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
755 break; /* Only appears once */
761 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
765 /* Update lagg capabilities */
766 lagg_capabilities(sc);
773 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
775 struct lagg_reqport *rp = (struct lagg_reqport *)data;
776 struct lagg_softc *sc;
777 struct lagg_port *lp = NULL;
779 struct rm_priotracker tracker;
781 /* Should be checked by the caller */
782 if (ifp->if_type != IFT_IEEE8023ADLAG ||
783 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
788 if (rp->rp_portname[0] == '\0' ||
789 ifunit(rp->rp_portname) != ifp) {
794 LAGG_RLOCK(sc, &tracker);
795 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
797 LAGG_RUNLOCK(sc, &tracker);
801 lagg_port2req(lp, rp);
802 LAGG_RUNLOCK(sc, &tracker);
806 if (lp->lp_ioctl == NULL) {
810 error = (*lp->lp_ioctl)(ifp, cmd, data);
814 /* Update lagg interface capabilities */
816 lagg_capabilities(sc);
821 /* Do not allow the MTU to be changed once joined */
832 if (lp->lp_ioctl != NULL)
833 return ((*lp->lp_ioctl)(ifp, cmd, data));
839 * For direct output to child ports.
842 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
843 const struct sockaddr *dst, struct route *ro)
845 struct lagg_port *lp = ifp->if_lagg;
847 switch (dst->sa_family) {
848 case pseudo_AF_HDRCMPLT:
850 return ((*lp->lp_output)(ifp, m, dst, ro));
853 /* drop any other frames */
859 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
861 struct lagg_port *lp;
862 struct lagg_softc *sc;
864 if ((lp = ifp->if_lagg) == NULL)
866 /* If the ifnet is just being renamed, don't do anything. */
867 if (ifp->if_flags & IFF_RENAMING)
873 lp->lp_detaching = 1;
874 lagg_port_destroy(lp, 1);
879 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
881 struct lagg_softc *sc = lp->lp_softc;
883 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
884 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
885 rp->rp_prio = lp->lp_prio;
886 rp->rp_flags = lp->lp_flags;
887 if (sc->sc_portreq != NULL)
888 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
890 /* Add protocol specific flags */
891 switch (sc->sc_proto) {
892 case LAGG_PROTO_FAILOVER:
893 if (lp == sc->sc_primary)
894 rp->rp_flags |= LAGG_PORT_MASTER;
895 if (lp == lagg_link_active(sc, sc->sc_primary))
896 rp->rp_flags |= LAGG_PORT_ACTIVE;
899 case LAGG_PROTO_ROUNDROBIN:
900 case LAGG_PROTO_LOADBALANCE:
901 case LAGG_PROTO_ETHERCHANNEL:
902 if (LAGG_PORTACTIVE(lp))
903 rp->rp_flags |= LAGG_PORT_ACTIVE;
906 case LAGG_PROTO_LACP:
907 /* LACP has a different definition of active */
908 if (lacp_isactive(lp))
909 rp->rp_flags |= LAGG_PORT_ACTIVE;
910 if (lacp_iscollecting(lp))
911 rp->rp_flags |= LAGG_PORT_COLLECTING;
912 if (lacp_isdistributing(lp))
913 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
922 struct lagg_softc *sc = (struct lagg_softc *)xsc;
923 struct lagg_port *lp;
924 struct ifnet *ifp = sc->sc_ifp;
926 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
931 ifp->if_drv_flags |= IFF_DRV_RUNNING;
932 /* Update the port lladdrs */
933 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
934 lagg_port_lladdr(lp, IF_LLADDR(ifp));
936 if (sc->sc_init != NULL)
943 lagg_stop(struct lagg_softc *sc)
945 struct ifnet *ifp = sc->sc_ifp;
947 LAGG_WLOCK_ASSERT(sc);
949 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
952 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
954 if (sc->sc_stop != NULL)
959 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
961 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
962 struct lagg_reqall *ra = (struct lagg_reqall *)data;
963 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
964 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
965 struct ifreq *ifr = (struct ifreq *)data;
966 struct lagg_port *lp;
968 struct thread *td = curthread;
970 int count, buflen, len, error = 0;
971 struct rm_priotracker tracker;
973 bzero(&rpbuf, sizeof(rpbuf));
977 LAGG_RLOCK(sc, &tracker);
979 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
981 buflen = count * sizeof(struct lagg_reqport);
982 LAGG_RUNLOCK(sc, &tracker);
984 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
986 LAGG_RLOCK(sc, &tracker);
987 ra->ra_proto = sc->sc_proto;
988 if (sc->sc_req != NULL)
989 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
993 len = min(ra->ra_size, buflen);
994 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
995 if (len < sizeof(rpbuf))
998 lagg_port2req(lp, &rpbuf);
999 memcpy(buf, &rpbuf, sizeof(rpbuf));
1001 buf += sizeof(rpbuf);
1002 len -= sizeof(rpbuf);
1004 LAGG_RUNLOCK(sc, &tracker);
1005 ra->ra_ports = count;
1006 ra->ra_size = count * sizeof(rpbuf);
1007 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1008 free(outbuf, M_TEMP);
1011 error = priv_check(td, PRIV_NET_LAGG);
1014 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1015 error = EPROTONOSUPPORT;
1019 if (sc->sc_proto != LAGG_PROTO_NONE) {
1020 /* Reset protocol first in case detach unlocks */
1021 sc->sc_proto = LAGG_PROTO_NONE;
1022 error = sc->sc_detach(sc);
1023 sc->sc_detach = NULL;
1024 sc->sc_start = NULL;
1025 sc->sc_input = NULL;
1026 sc->sc_port_create = NULL;
1027 sc->sc_port_destroy = NULL;
1028 sc->sc_linkstate = NULL;
1031 sc->sc_lladdr = NULL;
1033 sc->sc_portreq = NULL;
1034 } else if (sc->sc_input != NULL) {
1035 /* Still detaching */
1042 for (int i = 0; i < (sizeof(lagg_protos) /
1043 sizeof(lagg_protos[0])); i++) {
1044 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1045 if (sc->sc_ifflags & IFF_DEBUG)
1046 printf("%s: using proto %u\n",
1048 lagg_protos[i].ti_proto);
1049 sc->sc_proto = lagg_protos[i].ti_proto;
1050 if (sc->sc_proto != LAGG_PROTO_NONE)
1051 error = lagg_protos[i].ti_attach(sc);
1057 error = EPROTONOSUPPORT;
1059 case SIOCGLAGGFLAGS:
1060 rf->rf_flags = sc->sc_flags;
1063 error = priv_check(td, PRIV_NET_LAGG);
1066 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1071 sc->sc_flags &= ~LAGG_F_HASHMASK;
1072 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1076 if (rp->rp_portname[0] == '\0' ||
1077 (tpif = ifunit(rp->rp_portname)) == NULL) {
1082 LAGG_RLOCK(sc, &tracker);
1083 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1084 lp->lp_softc != sc) {
1086 LAGG_RUNLOCK(sc, &tracker);
1090 lagg_port2req(lp, rp);
1091 LAGG_RUNLOCK(sc, &tracker);
1094 error = priv_check(td, PRIV_NET_LAGG);
1097 if (rp->rp_portname[0] == '\0' ||
1098 (tpif = ifunit(rp->rp_portname)) == NULL) {
1103 error = lagg_port_create(sc, tpif);
1106 case SIOCSLAGGDELPORT:
1107 error = priv_check(td, PRIV_NET_LAGG);
1110 if (rp->rp_portname[0] == '\0' ||
1111 (tpif = ifunit(rp->rp_portname)) == NULL) {
1117 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1118 lp->lp_softc != sc) {
1124 error = lagg_port_destroy(lp, 1);
1128 /* Set flags on ports too */
1130 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1131 lagg_setflags(lp, 1);
1135 if (!(ifp->if_flags & IFF_UP) &&
1136 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1138 * If interface is marked down and it is running,
1139 * then stop and disable it.
1144 } else if ((ifp->if_flags & IFF_UP) &&
1145 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1147 * If interface is marked up and it is stopped, then
1150 (*ifp->if_init)(sc);
1156 error = lagg_ether_setmulti(sc);
1161 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1166 /* Do not allow the MTU or caps to be directly changed */
1171 error = ether_ioctl(ifp, cmd, data);
1178 lagg_ether_setmulti(struct lagg_softc *sc)
1180 struct lagg_port *lp;
1182 LAGG_WLOCK_ASSERT(sc);
1184 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1185 /* First, remove any existing filter entries. */
1186 lagg_ether_cmdmulti(lp, 0);
1187 /* copy all addresses from the lagg interface to the port */
1188 lagg_ether_cmdmulti(lp, 1);
1194 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1196 struct lagg_softc *sc = lp->lp_softc;
1197 struct ifnet *ifp = lp->lp_ifp;
1198 struct ifnet *scifp = sc->sc_ifp;
1200 struct ifmultiaddr *ifma, *rifma = NULL;
1201 struct sockaddr_dl sdl;
1204 LAGG_WLOCK_ASSERT(sc);
1206 bzero((char *)&sdl, sizeof(sdl));
1207 sdl.sdl_len = sizeof(sdl);
1208 sdl.sdl_family = AF_LINK;
1209 sdl.sdl_type = IFT_ETHER;
1210 sdl.sdl_alen = ETHER_ADDR_LEN;
1211 sdl.sdl_index = ifp->if_index;
1214 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1215 if (ifma->ifma_addr->sa_family != AF_LINK)
1217 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1218 LLADDR(&sdl), ETHER_ADDR_LEN);
1220 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1223 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1226 mc->mc_ifma = rifma;
1227 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1230 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1231 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1232 if_delmulti_ifma(mc->mc_ifma);
1239 /* Handle a ref counted flag that should be set on the lagg port as well */
1241 lagg_setflag(struct lagg_port *lp, int flag, int status,
1242 int (*func)(struct ifnet *, int))
1244 struct lagg_softc *sc = lp->lp_softc;
1245 struct ifnet *scifp = sc->sc_ifp;
1246 struct ifnet *ifp = lp->lp_ifp;
1249 LAGG_WLOCK_ASSERT(sc);
1251 status = status ? (scifp->if_flags & flag) : 0;
1252 /* Now "status" contains the flag value or 0 */
1255 * See if recorded ports status is different from what
1256 * we want it to be. If it is, flip it. We record ports
1257 * status in lp_ifflags so that we won't clear ports flag
1258 * we haven't set. In fact, we don't clear or set ports
1259 * flags directly, but get or release references to them.
1260 * That's why we can be sure that recorded flags still are
1261 * in accord with actual ports flags.
1263 if (status != (lp->lp_ifflags & flag)) {
1264 error = (*func)(ifp, status);
1267 lp->lp_ifflags &= ~flag;
1268 lp->lp_ifflags |= status;
1274 * Handle IFF_* flags that require certain changes on the lagg port
1275 * if "status" is true, update ports flags respective to the lagg
1276 * if "status" is false, forcedly clear the flags set on port.
1279 lagg_setflags(struct lagg_port *lp, int status)
1283 for (i = 0; lagg_pflags[i].flag; i++) {
1284 error = lagg_setflag(lp, lagg_pflags[i].flag,
1285 status, lagg_pflags[i].func);
1293 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1295 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1296 int error, len, mcast;
1297 struct rm_priotracker tracker;
1299 len = m->m_pkthdr.len;
1300 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1302 LAGG_RLOCK(sc, &tracker);
1303 /* We need a Tx algorithm and at least one port */
1304 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1305 LAGG_RUNLOCK(sc, &tracker);
1311 ETHER_BPF_MTAP(ifp, m);
1313 error = (*sc->sc_start)(sc, m);
1314 LAGG_RUNLOCK(sc, &tracker);
1317 counter_u64_add(sc->sc_opackets, 1);
1318 counter_u64_add(sc->sc_obytes, len);
1319 ifp->if_omcasts += mcast;
1327 * The ifp->if_qflush entry point for lagg(4) is no-op.
1330 lagg_qflush(struct ifnet *ifp __unused)
1334 static struct mbuf *
1335 lagg_input(struct ifnet *ifp, struct mbuf *m)
1337 struct lagg_port *lp = ifp->if_lagg;
1338 struct lagg_softc *sc = lp->lp_softc;
1339 struct ifnet *scifp = sc->sc_ifp;
1340 struct rm_priotracker tracker;
1342 LAGG_RLOCK(sc, &tracker);
1343 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1344 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1345 sc->sc_proto == LAGG_PROTO_NONE) {
1346 LAGG_RUNLOCK(sc, &tracker);
1351 ETHER_BPF_MTAP(scifp, m);
1353 m = (*sc->sc_input)(sc, lp, m);
1356 counter_u64_add(sc->sc_ipackets, 1);
1357 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1359 if (scifp->if_flags & IFF_MONITOR) {
1365 LAGG_RUNLOCK(sc, &tracker);
1370 lagg_media_change(struct ifnet *ifp)
1372 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1374 if (sc->sc_ifflags & IFF_DEBUG)
1375 printf("%s\n", __func__);
1382 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1384 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1385 struct lagg_port *lp;
1386 struct rm_priotracker tracker;
1388 imr->ifm_status = IFM_AVALID;
1389 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1391 LAGG_RLOCK(sc, &tracker);
1392 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1393 if (LAGG_PORTACTIVE(lp))
1394 imr->ifm_status |= IFM_ACTIVE;
1396 LAGG_RUNLOCK(sc, &tracker);
1400 lagg_linkstate(struct lagg_softc *sc)
1402 struct lagg_port *lp;
1403 int new_link = LINK_STATE_DOWN;
1406 /* Our link is considered up if at least one of our ports is active */
1407 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1408 if (lp->lp_link_state == LINK_STATE_UP) {
1409 new_link = LINK_STATE_UP;
1413 if_link_state_change(sc->sc_ifp, new_link);
1415 /* Update if_baudrate to reflect the max possible speed */
1416 switch (sc->sc_proto) {
1417 case LAGG_PROTO_FAILOVER:
1418 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1419 sc->sc_primary->lp_ifp->if_baudrate : 0;
1421 case LAGG_PROTO_ROUNDROBIN:
1422 case LAGG_PROTO_LOADBALANCE:
1423 case LAGG_PROTO_ETHERCHANNEL:
1425 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1426 speed += lp->lp_ifp->if_baudrate;
1427 sc->sc_ifp->if_baudrate = speed;
1429 case LAGG_PROTO_LACP:
1430 /* LACP updates if_baudrate itself */
1436 lagg_port_state(struct ifnet *ifp, int state)
1438 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1439 struct lagg_softc *sc = NULL;
1448 if (sc->sc_linkstate != NULL)
1449 (*sc->sc_linkstate)(lp);
1454 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1456 struct lagg_port *lp_next, *rval = NULL;
1457 // int new_link = LINK_STATE_DOWN;
1459 LAGG_RLOCK_ASSERT(sc);
1461 * Search a port which reports an active link state.
1466 if (LAGG_PORTACTIVE(lp)) {
1470 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1471 LAGG_PORTACTIVE(lp_next)) {
1477 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1478 if (LAGG_PORTACTIVE(lp_next)) {
1487 * The IEEE 802.1D standard assumes that a lagg with
1488 * multiple ports is always full duplex. This is valid
1489 * for load sharing laggs and if at least two links
1490 * are active. Unfortunately, checking the latter would
1491 * be too expensive at this point.
1493 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1495 new_link = LINK_STATE_FULL_DUPLEX;
1497 new_link = rval->lp_link_state;
1505 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1507 if (m->m_pkthdr.len < (off + len)) {
1509 } else if (m->m_len < (off + len)) {
1510 m_copydata(m, off, len, buf);
1513 return (mtod(m, char *) + off);
1517 lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1519 struct lagg_softc *sc = (struct lagg_softc *)arg1;
1520 struct lagg_port *lp;
1523 /* LACP tracks active links automatically, the others do not */
1524 if (sc->sc_proto != LAGG_PROTO_LACP) {
1526 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1527 sc->sc_active += LAGG_PORTACTIVE(lp);
1530 error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1531 if ((error) || (req->newptr == NULL))
1538 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1543 struct ether_header *eh;
1544 const struct ether_vlan_header *vlan;
1546 const struct ip *ip;
1547 const uint32_t *ports;
1551 const struct ip6_hdr *ip6;
1561 struct ether_vlan_header vlan;
1569 eh = mtod(m, struct ether_header *);
1570 etype = ntohs(eh->ether_type);
1571 if (sc->sc_flags & LAGG_F_HASHL2) {
1572 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1573 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1576 /* Special handling for encapsulating VLAN frames */
1577 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1578 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1579 sizeof(m->m_pkthdr.ether_vtag), p);
1580 } else if (etype == ETHERTYPE_VLAN) {
1581 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1585 if (sc->sc_flags & LAGG_F_HASHL2)
1586 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1587 etype = ntohs(vlan->evl_proto);
1588 off += sizeof(*vlan) - sizeof(*eh);
1594 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1598 if (sc->sc_flags & LAGG_F_HASHL3) {
1599 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1600 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1602 if (!(sc->sc_flags & LAGG_F_HASHL4))
1608 iphlen = ip->ip_hl << 2;
1609 if (iphlen < sizeof(*ip))
1612 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1615 p = hash32_buf(ports, sizeof(*ports), p);
1621 case ETHERTYPE_IPV6:
1622 if (!(sc->sc_flags & LAGG_F_HASHL3))
1624 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1628 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1629 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1630 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1631 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1640 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1643 return (ifp->if_transmit)(ifp, m);
1647 * Simple round robin aggregation
1651 lagg_rr_attach(struct lagg_softc *sc)
1653 sc->sc_detach = lagg_rr_detach;
1654 sc->sc_start = lagg_rr_start;
1655 sc->sc_input = lagg_rr_input;
1656 sc->sc_port_create = NULL;
1657 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1664 lagg_rr_detach(struct lagg_softc *sc)
1670 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1672 struct lagg_port *lp;
1675 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1677 lp = SLIST_FIRST(&sc->sc_ports);
1679 lp = SLIST_NEXT(lp, lp_entries);
1682 * Check the port's link state. This will return the next active
1683 * port if the link is down or the port is NULL.
1685 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1691 return (lagg_enqueue(lp->lp_ifp, m));
1694 static struct mbuf *
1695 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1697 struct ifnet *ifp = sc->sc_ifp;
1699 /* Just pass in the packet to our lagg device */
1700 m->m_pkthdr.rcvif = ifp;
1710 lagg_fail_attach(struct lagg_softc *sc)
1712 sc->sc_detach = lagg_fail_detach;
1713 sc->sc_start = lagg_fail_start;
1714 sc->sc_input = lagg_fail_input;
1715 sc->sc_port_create = NULL;
1716 sc->sc_port_destroy = NULL;
1722 lagg_fail_detach(struct lagg_softc *sc)
1728 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1730 struct lagg_port *lp;
1732 /* Use the master port if active or the next available port */
1733 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1739 return (lagg_enqueue(lp->lp_ifp, m));
1742 static struct mbuf *
1743 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1745 struct ifnet *ifp = sc->sc_ifp;
1746 struct lagg_port *tmp_tp;
1748 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1749 m->m_pkthdr.rcvif = ifp;
1753 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1754 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1756 * If tmp_tp is null, we've recieved a packet when all
1757 * our links are down. Weird, but process it anyways.
1759 if ((tmp_tp == NULL || tmp_tp == lp)) {
1760 m->m_pkthdr.rcvif = ifp;
1774 lagg_lb_attach(struct lagg_softc *sc)
1776 struct lagg_port *lp;
1779 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1780 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1783 sc->sc_detach = lagg_lb_detach;
1784 sc->sc_start = lagg_lb_start;
1785 sc->sc_input = lagg_lb_input;
1786 sc->sc_port_create = lagg_lb_port_create;
1787 sc->sc_port_destroy = lagg_lb_port_destroy;
1788 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1790 lb->lb_key = arc4random();
1791 sc->sc_psc = (caddr_t)lb;
1793 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1794 lagg_lb_port_create(lp);
1800 lagg_lb_detach(struct lagg_softc *sc)
1802 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1809 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1811 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1812 struct lagg_port *lp_next;
1815 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1816 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1819 if (i >= LAGG_MAX_PORTS)
1821 if (sc->sc_ifflags & IFF_DEBUG)
1822 printf("%s: port %s at index %d\n",
1823 sc->sc_ifname, lp_next->lp_ifname, i);
1824 lb->lb_ports[i++] = lp_next;
1831 lagg_lb_port_create(struct lagg_port *lp)
1833 struct lagg_softc *sc = lp->lp_softc;
1834 return (lagg_lb_porttable(sc, NULL));
1838 lagg_lb_port_destroy(struct lagg_port *lp)
1840 struct lagg_softc *sc = lp->lp_softc;
1841 lagg_lb_porttable(sc, lp);
1845 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1847 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1848 struct lagg_port *lp = NULL;
1851 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1852 p = m->m_pkthdr.flowid;
1854 p = lagg_hashmbuf(sc, m, lb->lb_key);
1856 lp = lb->lb_ports[p];
1859 * Check the port's link state. This will return the next active
1860 * port if the link is down or the port is NULL.
1862 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1868 return (lagg_enqueue(lp->lp_ifp, m));
1871 static struct mbuf *
1872 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1874 struct ifnet *ifp = sc->sc_ifp;
1876 /* Just pass in the packet to our lagg device */
1877 m->m_pkthdr.rcvif = ifp;
1887 lagg_lacp_attach(struct lagg_softc *sc)
1889 struct lagg_port *lp;
1892 sc->sc_detach = lagg_lacp_detach;
1893 sc->sc_port_create = lacp_port_create;
1894 sc->sc_port_destroy = lacp_port_destroy;
1895 sc->sc_linkstate = lacp_linkstate;
1896 sc->sc_start = lagg_lacp_start;
1897 sc->sc_input = lagg_lacp_input;
1898 sc->sc_init = lacp_init;
1899 sc->sc_stop = lacp_stop;
1900 sc->sc_lladdr = lagg_lacp_lladdr;
1901 sc->sc_req = lacp_req;
1902 sc->sc_portreq = lacp_portreq;
1904 error = lacp_attach(sc);
1908 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1909 lacp_port_create(lp);
1915 lagg_lacp_detach(struct lagg_softc *sc)
1917 struct lagg_port *lp;
1920 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1921 lacp_port_destroy(lp);
1923 /* unlocking is safe here */
1925 error = lacp_detach(sc);
1932 lagg_lacp_lladdr(struct lagg_softc *sc)
1934 struct lagg_port *lp;
1936 /* purge all the lacp ports */
1937 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1938 lacp_port_destroy(lp);
1940 /* add them back in */
1941 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1942 lacp_port_create(lp);
1946 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1948 struct lagg_port *lp;
1950 lp = lacp_select_tx_port(sc, m);
1957 return (lagg_enqueue(lp->lp_ifp, m));
1960 static struct mbuf *
1961 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1963 struct ifnet *ifp = sc->sc_ifp;
1964 struct ether_header *eh;
1967 eh = mtod(m, struct ether_header *);
1968 etype = ntohs(eh->ether_type);
1970 /* Tap off LACP control messages */
1971 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1972 m = lacp_input(lp, m);
1978 * If the port is not collecting or not in the active aggregator then
1981 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1986 m->m_pkthdr.rcvif = ifp;
1991 lagg_callout(void *arg)
1993 struct lagg_softc *sc = (struct lagg_softc *)arg;
1994 struct ifnet *ifp = sc->sc_ifp;
1996 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
1997 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
1998 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
1999 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2001 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);