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>
56 #if defined(INET) || defined(INET6)
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/if_ether.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_ifattach.h>
71 #include <net/if_vlan_var.h>
72 #include <net/if_lagg.h>
73 #include <net/ieee8023ad_lacp.h>
75 /* Special flags we should propagate to the lagg ports. */
78 int (*func)(struct ifnet *, int);
80 {IFF_PROMISC, ifpromisc},
81 {IFF_ALLMULTI, if_allmulti},
85 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
86 #define V_lagg_list VNET(lagg_list)
87 static VNET_DEFINE(struct mtx, lagg_list_mtx);
88 #define V_lagg_list_mtx VNET(lagg_list_mtx)
89 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
90 "if_lagg list", NULL, MTX_DEF)
91 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
92 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
93 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
94 eventhandler_tag lagg_detach_cookie = NULL;
96 static int lagg_clone_create(struct if_clone *, int, caddr_t);
97 static void lagg_clone_destroy(struct ifnet *);
98 static VNET_DEFINE(struct if_clone *, lagg_cloner);
99 #define V_lagg_cloner VNET(lagg_cloner)
100 static const char laggname[] = "lagg";
102 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
103 static void lagg_capabilities(struct lagg_softc *);
104 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
105 static void lagg_port_setlladdr(void *, int);
106 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
107 static int lagg_port_destroy(struct lagg_port *, int);
108 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
109 static void lagg_linkstate(struct lagg_softc *);
110 static void lagg_port_state(struct ifnet *, int);
111 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
112 static int lagg_port_output(struct ifnet *, struct mbuf *,
113 const struct sockaddr *, struct route *);
114 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
115 #ifdef LAGG_PORT_STACKING
116 static int lagg_port_checkstacking(struct lagg_softc *);
118 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
119 static void lagg_init(void *);
120 static void lagg_stop(struct lagg_softc *);
121 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
122 static int lagg_ether_setmulti(struct lagg_softc *);
123 static int lagg_ether_cmdmulti(struct lagg_port *, int);
124 static int lagg_setflag(struct lagg_port *, int, int,
125 int (*func)(struct ifnet *, int));
126 static int lagg_setflags(struct lagg_port *, int status);
127 static int lagg_transmit(struct ifnet *, struct mbuf *);
128 static void lagg_qflush(struct ifnet *);
129 static int lagg_media_change(struct ifnet *);
130 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
131 static struct lagg_port *lagg_link_active(struct lagg_softc *,
133 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
135 /* Simple round robin */
136 static void lagg_rr_attach(struct lagg_softc *);
137 static int lagg_rr_detach(struct lagg_softc *);
138 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
139 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
142 /* Active failover */
143 static void lagg_fail_attach(struct lagg_softc *);
144 static int lagg_fail_detach(struct lagg_softc *);
145 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
146 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
150 static void lagg_lb_attach(struct lagg_softc *);
151 static int lagg_lb_detach(struct lagg_softc *);
152 static int lagg_lb_port_create(struct lagg_port *);
153 static void lagg_lb_port_destroy(struct lagg_port *);
154 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
155 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
157 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
160 static void lagg_lacp_attach(struct lagg_softc *);
161 static int lagg_lacp_detach(struct lagg_softc *);
162 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
163 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
165 static void lagg_lacp_lladdr(struct lagg_softc *);
167 static void lagg_callout(void *);
169 /* lagg protocol table */
170 static const struct lagg_proto {
172 void (*ti_attach)(struct lagg_softc *);
174 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
175 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
176 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
177 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
178 { LAGG_PROTO_LACP, lagg_lacp_attach },
179 { LAGG_PROTO_NONE, NULL }
182 SYSCTL_DECL(_net_link);
183 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
186 /* Allow input on any failover links */
187 static VNET_DEFINE(int, lagg_failover_rx_all);
188 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
189 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
190 &VNET_NAME(lagg_failover_rx_all), 0,
191 "Accept input from any interface in a failover lagg");
193 /* Default value for using M_FLOWID */
194 static VNET_DEFINE(int, def_use_flowid) = 1;
195 #define V_def_use_flowid VNET(def_use_flowid)
196 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
197 &VNET_NAME(def_use_flowid), 0,
198 "Default setting for using flow id for load sharing");
200 /* Default value for using M_FLOWID */
201 static VNET_DEFINE(int, def_flowid_shift) = 16;
202 #define V_def_flowid_shift VNET(def_flowid_shift)
203 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
204 &VNET_NAME(def_flowid_shift), 0,
205 "Default setting for flowid shift for load sharing");
208 vnet_lagg_init(const void *unused __unused)
211 LAGG_LIST_LOCK_INIT();
212 SLIST_INIT(&V_lagg_list);
213 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
214 lagg_clone_destroy, 0);
216 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
217 vnet_lagg_init, NULL);
220 vnet_lagg_uninit(const void *unused __unused)
223 if_clone_detach(V_lagg_cloner);
224 LAGG_LIST_LOCK_DESTROY();
226 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
227 vnet_lagg_uninit, NULL);
230 lagg_modevent(module_t mod, int type, void *data)
235 lagg_input_p = lagg_input;
236 lagg_linkstate_p = lagg_port_state;
237 lagg_detach_cookie = EVENTHANDLER_REGISTER(
238 ifnet_departure_event, lagg_port_ifdetach, NULL,
239 EVENTHANDLER_PRI_ANY);
242 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
245 lagg_linkstate_p = NULL;
253 static moduledata_t lagg_mod = {
259 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
260 MODULE_VERSION(if_lagg, 1);
263 * This routine is run via an vlan
267 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
269 struct lagg_softc *sc = ifp->if_softc;
270 struct lagg_port *lp;
271 struct rm_priotracker tracker;
273 if (ifp->if_softc != arg) /* Not our event */
276 LAGG_RLOCK(sc, &tracker);
277 if (!SLIST_EMPTY(&sc->sc_ports)) {
278 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
279 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
281 LAGG_RUNLOCK(sc, &tracker);
285 * This routine is run via an vlan
289 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
291 struct lagg_softc *sc = ifp->if_softc;
292 struct lagg_port *lp;
293 struct rm_priotracker tracker;
295 if (ifp->if_softc != arg) /* Not our event */
298 LAGG_RLOCK(sc, &tracker);
299 if (!SLIST_EMPTY(&sc->sc_ports)) {
300 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
301 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
303 LAGG_RUNLOCK(sc, &tracker);
307 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
309 struct lagg_softc *sc;
311 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
314 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
315 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
321 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
322 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
323 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
324 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
326 if (V_def_use_flowid)
327 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
328 sc->flowid_shift = V_def_flowid_shift;
330 /* Hash all layers by default */
331 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
333 sc->sc_proto = LAGG_PROTO_NONE;
334 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
335 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
336 sc->sc_proto = lagg_protos[i].ti_proto;
337 lagg_protos[i].ti_attach(sc);
342 LAGG_CALLOUT_LOCK_INIT(sc);
343 SLIST_INIT(&sc->sc_ports);
344 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
347 * This uses the callout lock rather than the rmlock; one can't
348 * hold said rmlock during SWI.
350 callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0);
352 /* Initialise pseudo media types */
353 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
355 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
356 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
358 if_initname(ifp, laggname, unit);
360 ifp->if_transmit = lagg_transmit;
361 ifp->if_qflush = lagg_qflush;
362 ifp->if_init = lagg_init;
363 ifp->if_ioctl = lagg_ioctl;
364 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
365 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
368 * Attach as an ordinary ethernet device, children will be attached
369 * as special device IFT_IEEE8023ADLAG.
371 ether_ifattach(ifp, eaddr);
373 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
374 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
375 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
376 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
378 /* Insert into the global list of laggs */
380 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
383 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
389 lagg_clone_destroy(struct ifnet *ifp)
391 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
392 struct lagg_port *lp;
397 ifp->if_flags &= ~IFF_UP;
399 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
400 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
402 /* Shutdown and remove lagg ports */
403 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
404 lagg_port_destroy(lp, 1);
405 /* Unhook the aggregation protocol */
406 if (sc->sc_detach != NULL)
407 (*sc->sc_detach)(sc);
411 ifmedia_removeall(&sc->sc_media);
415 /* This grabs sc_callout_mtx, serialising it correctly */
416 callout_drain(&sc->sc_callout);
418 /* At this point it's drained; we can free this */
419 counter_u64_free(sc->sc_ipackets);
420 counter_u64_free(sc->sc_opackets);
421 counter_u64_free(sc->sc_ibytes);
422 counter_u64_free(sc->sc_obytes);
425 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
428 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
429 LAGG_LOCK_DESTROY(sc);
430 LAGG_CALLOUT_LOCK_DESTROY(sc);
435 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
437 struct ifnet *ifp = sc->sc_ifp;
440 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
443 LAGG_WLOCK_ASSERT(sc);
445 * Set the link layer address on the lagg interface.
446 * sc_lladdr() notifies the MAC change to
447 * the aggregation protocol. iflladdr_event handler which
448 * may trigger gratuitous ARPs for INET will be handled in
451 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
452 if (sc->sc_lladdr != NULL)
453 (*sc->sc_lladdr)(sc);
455 bzero(&lp, sizeof(lp));
456 lp.lp_ifp = sc->sc_ifp;
459 lagg_port_lladdr(&lp, lladdr);
463 lagg_capabilities(struct lagg_softc *sc)
465 struct lagg_port *lp;
466 int cap = ~0, ena = ~0;
468 struct ifnet_hw_tsomax hw_tsomax;
470 LAGG_WLOCK_ASSERT(sc);
472 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
474 /* Get capabilities from the lagg ports */
475 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
476 cap &= lp->lp_ifp->if_capabilities;
477 ena &= lp->lp_ifp->if_capenable;
478 hwa &= lp->lp_ifp->if_hwassist;
479 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
481 cap = (cap == ~0 ? 0 : cap);
482 ena = (ena == ~0 ? 0 : ena);
483 hwa = (hwa == ~0 ? 0 : hwa);
485 if (sc->sc_ifp->if_capabilities != cap ||
486 sc->sc_ifp->if_capenable != ena ||
487 sc->sc_ifp->if_hwassist != hwa ||
488 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
489 sc->sc_ifp->if_capabilities = cap;
490 sc->sc_ifp->if_capenable = ena;
491 sc->sc_ifp->if_hwassist = hwa;
492 getmicrotime(&sc->sc_ifp->if_lastchange);
494 if (sc->sc_ifflags & IFF_DEBUG)
495 if_printf(sc->sc_ifp,
496 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
501 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
503 struct lagg_softc *sc = lp->lp_softc;
504 struct ifnet *ifp = lp->lp_ifp;
505 struct lagg_llq *llq;
509 LAGG_WLOCK_ASSERT(sc);
511 primary = (sc->sc_primary->lp_ifp == ifp) ? 1 : 0;
512 if (primary == 0 && (lp->lp_detaching ||
513 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0))
516 /* Check to make sure its not already queued to be changed */
517 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
518 if (llq->llq_ifp == ifp) {
525 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
526 if (llq == NULL) /* XXX what to do */
530 /* Update the lladdr even if pending, it may have changed */
532 llq->llq_primary = primary;
533 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
536 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
538 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
542 * Set the interface MAC address from a taskqueue to avoid a LOR.
545 lagg_port_setlladdr(void *arg, int pending)
547 struct lagg_softc *sc = (struct lagg_softc *)arg;
548 struct lagg_llq *llq, *head;
552 /* Grab a local reference of the queue and remove it from the softc */
554 head = SLIST_FIRST(&sc->sc_llq_head);
555 SLIST_FIRST(&sc->sc_llq_head) = NULL;
559 * Traverse the queue and set the lladdr on each ifp. It is safe to do
560 * unlocked as we have the only reference to it.
562 for (llq = head; llq != NULL; llq = head) {
565 CURVNET_SET(ifp->if_vnet);
566 if (llq->llq_primary == 0) {
568 * Set the link layer address on the laggport interface.
569 * if_setlladdr() triggers gratuitous ARPs for INET.
571 error = if_setlladdr(ifp, llq->llq_lladdr,
574 printf("%s: setlladdr failed on %s\n", __func__,
577 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
579 head = SLIST_NEXT(llq, llq_entries);
585 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
587 struct lagg_softc *sc_ptr;
588 struct lagg_port *lp, *tlp;
591 LAGG_WLOCK_ASSERT(sc);
593 /* Limit the maximal number of lagg ports */
594 if (sc->sc_count >= LAGG_MAX_PORTS)
597 /* Check if port has already been associated to a lagg */
598 if (ifp->if_lagg != NULL) {
599 /* Port is already in the current lagg? */
600 lp = (struct lagg_port *)ifp->if_lagg;
601 if (lp->lp_softc == sc)
606 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
607 if (ifp->if_type != IFT_ETHER)
608 return (EPROTONOSUPPORT);
610 /* Allow the first Ethernet member to define the MTU */
611 if (SLIST_EMPTY(&sc->sc_ports))
612 sc->sc_ifp->if_mtu = ifp->if_mtu;
613 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
614 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
619 if ((lp = malloc(sizeof(struct lagg_port),
620 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
623 /* Check if port is a stacked lagg */
625 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
626 if (ifp == sc_ptr->sc_ifp) {
630 /* XXX disable stacking for the moment, its untested */
631 #ifdef LAGG_PORT_STACKING
632 lp->lp_flags |= LAGG_PORT_STACK;
633 if (lagg_port_checkstacking(sc_ptr) >=
644 /* Change the interface type */
645 lp->lp_iftype = ifp->if_type;
646 ifp->if_type = IFT_IEEE8023ADLAG;
648 lp->lp_ioctl = ifp->if_ioctl;
649 ifp->if_ioctl = lagg_port_ioctl;
650 lp->lp_output = ifp->if_output;
651 ifp->if_output = lagg_port_output;
656 /* Save port link layer address */
657 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
659 if (SLIST_EMPTY(&sc->sc_ports)) {
661 lagg_lladdr(sc, IF_LLADDR(ifp));
663 /* Update link layer address for this port */
664 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
668 * Insert into the list of ports.
669 * Keep ports sorted by if_index. It is handy, when configuration
670 * is predictable and `ifconfig laggN create ...` command
671 * will lead to the same result each time.
673 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
674 if (tlp->lp_ifp->if_index < ifp->if_index && (
675 SLIST_NEXT(tlp, lp_entries) == NULL ||
676 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
681 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
683 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
686 /* Update lagg capabilities */
687 lagg_capabilities(sc);
690 /* Add multicast addresses and interface flags to this port */
691 lagg_ether_cmdmulti(lp, 1);
692 lagg_setflags(lp, 1);
694 if (sc->sc_port_create != NULL)
695 error = (*sc->sc_port_create)(lp);
697 /* remove the port again, without calling sc_port_destroy */
698 lagg_port_destroy(lp, 0);
705 #ifdef LAGG_PORT_STACKING
707 lagg_port_checkstacking(struct lagg_softc *sc)
709 struct lagg_softc *sc_ptr;
710 struct lagg_port *lp;
713 LAGG_WLOCK_ASSERT(sc);
715 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
716 if (lp->lp_flags & LAGG_PORT_STACK) {
717 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
718 m = MAX(m, lagg_port_checkstacking(sc_ptr));
727 lagg_port_destroy(struct lagg_port *lp, int runpd)
729 struct lagg_softc *sc = lp->lp_softc;
730 struct lagg_port *lp_ptr;
731 struct lagg_llq *llq;
732 struct ifnet *ifp = lp->lp_ifp;
734 LAGG_WLOCK_ASSERT(sc);
736 if (runpd && sc->sc_port_destroy != NULL)
737 (*sc->sc_port_destroy)(lp);
740 * Remove multicast addresses and interface flags from this port and
741 * reset the MAC address, skip if the interface is being detached.
743 if (!lp->lp_detaching) {
744 lagg_ether_cmdmulti(lp, 0);
745 lagg_setflags(lp, 0);
746 lagg_port_lladdr(lp, lp->lp_lladdr);
749 /* Restore interface */
750 ifp->if_type = lp->lp_iftype;
751 ifp->if_ioctl = lp->lp_ioctl;
752 ifp->if_output = lp->lp_output;
755 /* Finally, remove the port from the lagg */
756 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
759 /* Update the primary interface */
760 if (lp == sc->sc_primary) {
761 uint8_t lladdr[ETHER_ADDR_LEN];
763 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
764 bzero(&lladdr, ETHER_ADDR_LEN);
766 bcopy(lp_ptr->lp_lladdr,
767 lladdr, ETHER_ADDR_LEN);
769 lagg_lladdr(sc, lladdr);
770 sc->sc_primary = lp_ptr;
772 /* Update link layer address for each port */
773 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
774 lagg_port_lladdr(lp_ptr, lladdr);
777 /* Remove any pending lladdr changes from the queue */
778 if (lp->lp_detaching) {
779 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
780 if (llq->llq_ifp == ifp) {
781 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
784 break; /* Only appears once */
790 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
794 /* Update lagg capabilities */
795 lagg_capabilities(sc);
802 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
804 struct lagg_reqport *rp = (struct lagg_reqport *)data;
805 struct lagg_softc *sc;
806 struct lagg_port *lp = NULL;
808 struct rm_priotracker tracker;
810 /* Should be checked by the caller */
811 if (ifp->if_type != IFT_IEEE8023ADLAG ||
812 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
817 if (rp->rp_portname[0] == '\0' ||
818 ifunit(rp->rp_portname) != ifp) {
823 LAGG_RLOCK(sc, &tracker);
824 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
826 LAGG_RUNLOCK(sc, &tracker);
830 lagg_port2req(lp, rp);
831 LAGG_RUNLOCK(sc, &tracker);
835 if (lp->lp_ioctl == NULL) {
839 error = (*lp->lp_ioctl)(ifp, cmd, data);
843 /* Update lagg interface capabilities */
845 lagg_capabilities(sc);
850 /* Do not allow the MTU to be changed once joined */
861 if (lp->lp_ioctl != NULL)
862 return ((*lp->lp_ioctl)(ifp, cmd, data));
868 * For direct output to child ports.
871 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
872 const struct sockaddr *dst, struct route *ro)
874 struct lagg_port *lp = ifp->if_lagg;
876 switch (dst->sa_family) {
877 case pseudo_AF_HDRCMPLT:
879 return ((*lp->lp_output)(ifp, m, dst, ro));
882 /* drop any other frames */
888 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
890 struct lagg_port *lp;
891 struct lagg_softc *sc;
893 if ((lp = ifp->if_lagg) == NULL)
895 /* If the ifnet is just being renamed, don't do anything. */
896 if (ifp->if_flags & IFF_RENAMING)
902 lp->lp_detaching = 1;
903 lagg_port_destroy(lp, 1);
908 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
910 struct lagg_softc *sc = lp->lp_softc;
912 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
913 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
914 rp->rp_prio = lp->lp_prio;
915 rp->rp_flags = lp->lp_flags;
916 if (sc->sc_portreq != NULL)
917 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
919 /* Add protocol specific flags */
920 switch (sc->sc_proto) {
921 case LAGG_PROTO_FAILOVER:
922 if (lp == sc->sc_primary)
923 rp->rp_flags |= LAGG_PORT_MASTER;
924 if (lp == lagg_link_active(sc, sc->sc_primary))
925 rp->rp_flags |= LAGG_PORT_ACTIVE;
928 case LAGG_PROTO_ROUNDROBIN:
929 case LAGG_PROTO_LOADBALANCE:
930 case LAGG_PROTO_ETHERCHANNEL:
931 if (LAGG_PORTACTIVE(lp))
932 rp->rp_flags |= LAGG_PORT_ACTIVE;
935 case LAGG_PROTO_LACP:
936 /* LACP has a different definition of active */
937 if (lacp_isactive(lp))
938 rp->rp_flags |= LAGG_PORT_ACTIVE;
939 if (lacp_iscollecting(lp))
940 rp->rp_flags |= LAGG_PORT_COLLECTING;
941 if (lacp_isdistributing(lp))
942 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
951 struct lagg_softc *sc = (struct lagg_softc *)xsc;
952 struct lagg_port *lp;
953 struct ifnet *ifp = sc->sc_ifp;
955 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
960 ifp->if_drv_flags |= IFF_DRV_RUNNING;
961 /* Update the port lladdrs */
962 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
963 lagg_port_lladdr(lp, IF_LLADDR(ifp));
965 if (sc->sc_init != NULL)
972 lagg_stop(struct lagg_softc *sc)
974 struct ifnet *ifp = sc->sc_ifp;
976 LAGG_WLOCK_ASSERT(sc);
978 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
981 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
983 if (sc->sc_stop != NULL)
988 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
990 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
991 struct lagg_reqall *ra = (struct lagg_reqall *)data;
992 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
993 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
994 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
995 struct ifreq *ifr = (struct ifreq *)data;
996 struct lagg_port *lp;
997 const struct lagg_proto *proto = NULL;
999 struct thread *td = curthread;
1001 int count, buflen, len, error = 0;
1002 struct rm_priotracker tracker;
1004 bzero(&rpbuf, sizeof(rpbuf));
1008 LAGG_RLOCK(sc, &tracker);
1010 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1012 buflen = count * sizeof(struct lagg_reqport);
1013 LAGG_RUNLOCK(sc, &tracker);
1015 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1017 LAGG_RLOCK(sc, &tracker);
1018 ra->ra_proto = sc->sc_proto;
1019 if (sc->sc_req != NULL)
1020 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
1024 len = min(ra->ra_size, buflen);
1025 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1026 if (len < sizeof(rpbuf))
1029 lagg_port2req(lp, &rpbuf);
1030 memcpy(buf, &rpbuf, sizeof(rpbuf));
1032 buf += sizeof(rpbuf);
1033 len -= sizeof(rpbuf);
1035 LAGG_RUNLOCK(sc, &tracker);
1036 ra->ra_ports = count;
1037 ra->ra_size = count * sizeof(rpbuf);
1038 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1039 free(outbuf, M_TEMP);
1042 error = priv_check(td, PRIV_NET_LAGG);
1045 for (proto = lagg_protos; proto->ti_proto != LAGG_PROTO_NONE;
1047 if (proto->ti_proto == ra->ra_proto) {
1048 if (sc->sc_ifflags & IFF_DEBUG)
1049 printf("%s: using proto %u\n",
1050 sc->sc_ifname, proto->ti_proto);
1054 if (proto->ti_proto == LAGG_PROTO_NONE) {
1055 error = EPROTONOSUPPORT;
1058 /* Set to LAGG_PROTO_NONE during the attach. */
1060 if (sc->sc_proto != LAGG_PROTO_NONE) {
1061 int (*sc_detach)(struct lagg_softc *sc);
1063 /* Reset protocol and pointers */
1064 sc->sc_proto = LAGG_PROTO_NONE;
1065 sc_detach = sc->sc_detach;
1066 sc->sc_detach = NULL;
1067 sc->sc_start = NULL;
1068 sc->sc_input = NULL;
1069 sc->sc_port_create = NULL;
1070 sc->sc_port_destroy = NULL;
1071 sc->sc_linkstate = NULL;
1074 sc->sc_lladdr = NULL;
1076 sc->sc_portreq = NULL;
1078 if (sc_detach != NULL)
1084 proto->ti_attach(sc);
1086 sc->sc_proto = proto->ti_proto;
1090 ro->ro_opts = sc->sc_opts;
1091 if (sc->sc_proto == LAGG_PROTO_LACP) {
1092 struct lacp_softc *lsc;
1094 lsc = (struct lacp_softc *)sc->sc_psc;
1095 if (lsc->lsc_debug.lsc_tx_test != 0)
1096 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1097 if (lsc->lsc_debug.lsc_rx_test != 0)
1098 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1099 if (lsc->lsc_strict_mode != 0)
1100 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1101 if (lsc->lsc_fast_timeout != 0)
1102 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1104 ro->ro_active = sc->sc_active;
1107 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1108 ro->ro_active += LAGG_PORTACTIVE(lp);
1110 ro->ro_flapping = sc->sc_flapping;
1111 ro->ro_flowid_shift = sc->flowid_shift;
1114 error = priv_check(td, PRIV_NET_LAGG);
1117 if (ro->ro_opts == 0)
1120 * Set options. LACP options are stored in sc->sc_psc,
1125 switch (ro->ro_opts) {
1126 case LAGG_OPT_USE_FLOWID:
1127 case -LAGG_OPT_USE_FLOWID:
1128 case LAGG_OPT_FLOWIDSHIFT:
1132 case LAGG_OPT_LACP_TXTEST:
1133 case -LAGG_OPT_LACP_TXTEST:
1134 case LAGG_OPT_LACP_RXTEST:
1135 case -LAGG_OPT_LACP_RXTEST:
1136 case LAGG_OPT_LACP_STRICT:
1137 case -LAGG_OPT_LACP_STRICT:
1138 case LAGG_OPT_LACP_TIMEOUT:
1139 case -LAGG_OPT_LACP_TIMEOUT:
1149 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1150 /* Invalid combination of options specified. */
1153 break; /* Return from SIOCSLAGGOPTS. */
1156 * Store new options into sc->sc_opts except for
1157 * FLOWIDSHIFT and LACP options.
1160 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1161 sc->flowid_shift = ro->ro_flowid_shift;
1162 else if (ro->ro_opts > 0)
1163 sc->sc_opts |= ro->ro_opts;
1165 sc->sc_opts &= ~ro->ro_opts;
1167 struct lacp_softc *lsc;
1168 struct lacp_port *lp;
1170 lsc = (struct lacp_softc *)sc->sc_psc;
1172 switch (ro->ro_opts) {
1173 case LAGG_OPT_LACP_TXTEST:
1174 lsc->lsc_debug.lsc_tx_test = 1;
1176 case -LAGG_OPT_LACP_TXTEST:
1177 lsc->lsc_debug.lsc_tx_test = 0;
1179 case LAGG_OPT_LACP_RXTEST:
1180 lsc->lsc_debug.lsc_rx_test = 1;
1182 case -LAGG_OPT_LACP_RXTEST:
1183 lsc->lsc_debug.lsc_rx_test = 0;
1185 case LAGG_OPT_LACP_STRICT:
1186 lsc->lsc_strict_mode = 1;
1188 case -LAGG_OPT_LACP_STRICT:
1189 lsc->lsc_strict_mode = 0;
1191 case LAGG_OPT_LACP_TIMEOUT:
1193 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1194 lp->lp_state |= LACP_STATE_TIMEOUT;
1196 lsc->lsc_fast_timeout = 1;
1198 case -LAGG_OPT_LACP_TIMEOUT:
1200 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1201 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1203 lsc->lsc_fast_timeout = 0;
1209 case SIOCGLAGGFLAGS:
1210 rf->rf_flags = sc->sc_flags;
1213 error = priv_check(td, PRIV_NET_LAGG);
1216 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1221 sc->sc_flags &= ~LAGG_F_HASHMASK;
1222 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1226 if (rp->rp_portname[0] == '\0' ||
1227 (tpif = ifunit(rp->rp_portname)) == NULL) {
1232 LAGG_RLOCK(sc, &tracker);
1233 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1234 lp->lp_softc != sc) {
1236 LAGG_RUNLOCK(sc, &tracker);
1240 lagg_port2req(lp, rp);
1241 LAGG_RUNLOCK(sc, &tracker);
1244 error = priv_check(td, PRIV_NET_LAGG);
1247 if (rp->rp_portname[0] == '\0' ||
1248 (tpif = ifunit(rp->rp_portname)) == NULL) {
1254 * A laggport interface should not have inet6 address
1255 * because two interfaces with a valid link-local
1256 * scope zone must not be merged in any form. This
1257 * restriction is needed to prevent violation of
1258 * link-local scope zone. Attempts to add a laggport
1259 * interface which has inet6 addresses triggers
1260 * removal of all inet6 addresses on the member
1263 if (in6ifa_llaonifp(tpif)) {
1265 if_printf(sc->sc_ifp,
1266 "IPv6 addresses on %s have been removed "
1267 "before adding it as a member to prevent "
1268 "IPv6 address scope violation.\n",
1273 error = lagg_port_create(sc, tpif);
1276 case SIOCSLAGGDELPORT:
1277 error = priv_check(td, PRIV_NET_LAGG);
1280 if (rp->rp_portname[0] == '\0' ||
1281 (tpif = ifunit(rp->rp_portname)) == NULL) {
1287 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1288 lp->lp_softc != sc) {
1294 error = lagg_port_destroy(lp, 1);
1298 /* Set flags on ports too */
1300 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1301 lagg_setflags(lp, 1);
1305 if (!(ifp->if_flags & IFF_UP) &&
1306 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1308 * If interface is marked down and it is running,
1309 * then stop and disable it.
1314 } else if ((ifp->if_flags & IFF_UP) &&
1315 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1317 * If interface is marked up and it is stopped, then
1320 (*ifp->if_init)(sc);
1326 error = lagg_ether_setmulti(sc);
1331 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1336 /* Do not allow the MTU or caps to be directly changed */
1341 error = ether_ioctl(ifp, cmd, data);
1348 lagg_ether_setmulti(struct lagg_softc *sc)
1350 struct lagg_port *lp;
1352 LAGG_WLOCK_ASSERT(sc);
1354 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1355 /* First, remove any existing filter entries. */
1356 lagg_ether_cmdmulti(lp, 0);
1357 /* copy all addresses from the lagg interface to the port */
1358 lagg_ether_cmdmulti(lp, 1);
1364 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1366 struct lagg_softc *sc = lp->lp_softc;
1367 struct ifnet *ifp = lp->lp_ifp;
1368 struct ifnet *scifp = sc->sc_ifp;
1370 struct ifmultiaddr *ifma;
1373 LAGG_WLOCK_ASSERT(sc);
1376 IF_ADDR_WLOCK(scifp);
1377 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1378 if (ifma->ifma_addr->sa_family != AF_LINK)
1380 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1382 IF_ADDR_WUNLOCK(scifp);
1385 bcopy(ifma->ifma_addr, &mc->mc_addr,
1386 ifma->ifma_addr->sa_len);
1387 mc->mc_addr.sdl_index = ifp->if_index;
1389 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1391 IF_ADDR_WUNLOCK(scifp);
1392 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1393 error = if_addmulti(ifp,
1394 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1399 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1400 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1401 if (mc->mc_ifma && !lp->lp_detaching)
1402 if_delmulti_ifma(mc->mc_ifma);
1409 /* Handle a ref counted flag that should be set on the lagg port as well */
1411 lagg_setflag(struct lagg_port *lp, int flag, int status,
1412 int (*func)(struct ifnet *, int))
1414 struct lagg_softc *sc = lp->lp_softc;
1415 struct ifnet *scifp = sc->sc_ifp;
1416 struct ifnet *ifp = lp->lp_ifp;
1419 LAGG_WLOCK_ASSERT(sc);
1421 status = status ? (scifp->if_flags & flag) : 0;
1422 /* Now "status" contains the flag value or 0 */
1425 * See if recorded ports status is different from what
1426 * we want it to be. If it is, flip it. We record ports
1427 * status in lp_ifflags so that we won't clear ports flag
1428 * we haven't set. In fact, we don't clear or set ports
1429 * flags directly, but get or release references to them.
1430 * That's why we can be sure that recorded flags still are
1431 * in accord with actual ports flags.
1433 if (status != (lp->lp_ifflags & flag)) {
1434 error = (*func)(ifp, status);
1437 lp->lp_ifflags &= ~flag;
1438 lp->lp_ifflags |= status;
1444 * Handle IFF_* flags that require certain changes on the lagg port
1445 * if "status" is true, update ports flags respective to the lagg
1446 * if "status" is false, forcedly clear the flags set on port.
1449 lagg_setflags(struct lagg_port *lp, int status)
1453 for (i = 0; lagg_pflags[i].flag; i++) {
1454 error = lagg_setflag(lp, lagg_pflags[i].flag,
1455 status, lagg_pflags[i].func);
1463 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1465 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1466 int error, len, mcast;
1467 struct rm_priotracker tracker;
1469 len = m->m_pkthdr.len;
1470 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1472 LAGG_RLOCK(sc, &tracker);
1473 /* We need a Tx algorithm and at least one port */
1474 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1475 LAGG_RUNLOCK(sc, &tracker);
1481 ETHER_BPF_MTAP(ifp, m);
1483 error = (*sc->sc_start)(sc, m);
1484 LAGG_RUNLOCK(sc, &tracker);
1487 counter_u64_add(sc->sc_opackets, 1);
1488 counter_u64_add(sc->sc_obytes, len);
1489 ifp->if_omcasts += mcast;
1497 * The ifp->if_qflush entry point for lagg(4) is no-op.
1500 lagg_qflush(struct ifnet *ifp __unused)
1504 static struct mbuf *
1505 lagg_input(struct ifnet *ifp, struct mbuf *m)
1507 struct lagg_port *lp = ifp->if_lagg;
1508 struct lagg_softc *sc = lp->lp_softc;
1509 struct ifnet *scifp = sc->sc_ifp;
1510 struct rm_priotracker tracker;
1512 LAGG_RLOCK(sc, &tracker);
1513 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1514 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1515 sc->sc_proto == LAGG_PROTO_NONE) {
1516 LAGG_RUNLOCK(sc, &tracker);
1521 ETHER_BPF_MTAP(scifp, m);
1523 m = (lp->lp_detaching == 0) ? (*sc->sc_input)(sc, lp, m) : NULL;
1526 counter_u64_add(sc->sc_ipackets, 1);
1527 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1529 if (scifp->if_flags & IFF_MONITOR) {
1535 LAGG_RUNLOCK(sc, &tracker);
1540 lagg_media_change(struct ifnet *ifp)
1542 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1544 if (sc->sc_ifflags & IFF_DEBUG)
1545 printf("%s\n", __func__);
1552 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1554 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1555 struct lagg_port *lp;
1556 struct rm_priotracker tracker;
1558 imr->ifm_status = IFM_AVALID;
1559 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1561 LAGG_RLOCK(sc, &tracker);
1562 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1563 if (LAGG_PORTACTIVE(lp))
1564 imr->ifm_status |= IFM_ACTIVE;
1566 LAGG_RUNLOCK(sc, &tracker);
1570 lagg_linkstate(struct lagg_softc *sc)
1572 struct lagg_port *lp;
1573 int new_link = LINK_STATE_DOWN;
1576 /* Our link is considered up if at least one of our ports is active */
1577 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1578 if (lp->lp_link_state == LINK_STATE_UP) {
1579 new_link = LINK_STATE_UP;
1583 if_link_state_change(sc->sc_ifp, new_link);
1585 /* Update if_baudrate to reflect the max possible speed */
1586 switch (sc->sc_proto) {
1587 case LAGG_PROTO_FAILOVER:
1588 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1589 sc->sc_primary->lp_ifp->if_baudrate : 0;
1591 case LAGG_PROTO_ROUNDROBIN:
1592 case LAGG_PROTO_LOADBALANCE:
1593 case LAGG_PROTO_ETHERCHANNEL:
1595 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1596 speed += lp->lp_ifp->if_baudrate;
1597 sc->sc_ifp->if_baudrate = speed;
1599 case LAGG_PROTO_LACP:
1600 /* LACP updates if_baudrate itself */
1606 lagg_port_state(struct ifnet *ifp, int state)
1608 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1609 struct lagg_softc *sc = NULL;
1618 if (sc->sc_linkstate != NULL)
1619 (*sc->sc_linkstate)(lp);
1624 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1626 struct lagg_port *lp_next, *rval = NULL;
1627 // int new_link = LINK_STATE_DOWN;
1629 LAGG_RLOCK_ASSERT(sc);
1631 * Search a port which reports an active link state.
1636 if (LAGG_PORTACTIVE(lp)) {
1640 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1641 LAGG_PORTACTIVE(lp_next)) {
1647 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1648 if (LAGG_PORTACTIVE(lp_next)) {
1657 * The IEEE 802.1D standard assumes that a lagg with
1658 * multiple ports is always full duplex. This is valid
1659 * for load sharing laggs and if at least two links
1660 * are active. Unfortunately, checking the latter would
1661 * be too expensive at this point.
1663 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1665 new_link = LINK_STATE_FULL_DUPLEX;
1667 new_link = rval->lp_link_state;
1675 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1677 if (m->m_pkthdr.len < (off + len)) {
1679 } else if (m->m_len < (off + len)) {
1680 m_copydata(m, off, len, buf);
1683 return (mtod(m, char *) + off);
1687 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1692 struct ether_header *eh;
1693 const struct ether_vlan_header *vlan;
1695 const struct ip *ip;
1696 const uint32_t *ports;
1700 const struct ip6_hdr *ip6;
1710 struct ether_vlan_header vlan;
1718 eh = mtod(m, struct ether_header *);
1719 etype = ntohs(eh->ether_type);
1720 if (sc->sc_flags & LAGG_F_HASHL2) {
1721 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1722 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1725 /* Special handling for encapsulating VLAN frames */
1726 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1727 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1728 sizeof(m->m_pkthdr.ether_vtag), p);
1729 } else if (etype == ETHERTYPE_VLAN) {
1730 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1734 if (sc->sc_flags & LAGG_F_HASHL2)
1735 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1736 etype = ntohs(vlan->evl_proto);
1737 off += sizeof(*vlan) - sizeof(*eh);
1743 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1747 if (sc->sc_flags & LAGG_F_HASHL3) {
1748 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1749 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1751 if (!(sc->sc_flags & LAGG_F_HASHL4))
1757 iphlen = ip->ip_hl << 2;
1758 if (iphlen < sizeof(*ip))
1761 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1764 p = hash32_buf(ports, sizeof(*ports), p);
1770 case ETHERTYPE_IPV6:
1771 if (!(sc->sc_flags & LAGG_F_HASHL3))
1773 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1777 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1778 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1779 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1780 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1789 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1792 return (ifp->if_transmit)(ifp, m);
1796 * Simple round robin aggregation
1799 lagg_rr_attach(struct lagg_softc *sc)
1801 sc->sc_detach = lagg_rr_detach;
1802 sc->sc_start = lagg_rr_start;
1803 sc->sc_input = lagg_rr_input;
1804 sc->sc_detach = NULL;
1805 sc->sc_port_create = NULL;
1806 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1811 lagg_rr_detach(struct lagg_softc *sc)
1817 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1819 struct lagg_port *lp;
1822 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1824 lp = SLIST_FIRST(&sc->sc_ports);
1826 lp = SLIST_NEXT(lp, lp_entries);
1829 * Check the port's link state. This will return the next active
1830 * port if the link is down or the port is NULL.
1832 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1838 return (lagg_enqueue(lp->lp_ifp, m));
1841 static struct mbuf *
1842 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1844 struct ifnet *ifp = sc->sc_ifp;
1846 /* Just pass in the packet to our lagg device */
1847 m->m_pkthdr.rcvif = ifp;
1856 lagg_fail_attach(struct lagg_softc *sc)
1858 sc->sc_detach = lagg_fail_detach;
1859 sc->sc_start = lagg_fail_start;
1860 sc->sc_input = lagg_fail_input;
1861 sc->sc_port_create = NULL;
1862 sc->sc_port_destroy = NULL;
1863 sc->sc_detach = NULL;
1867 lagg_fail_detach(struct lagg_softc *sc)
1873 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1875 struct lagg_port *lp;
1877 /* Use the master port if active or the next available port */
1878 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1884 return (lagg_enqueue(lp->lp_ifp, m));
1887 static struct mbuf *
1888 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1890 struct ifnet *ifp = sc->sc_ifp;
1891 struct lagg_port *tmp_tp;
1893 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
1894 m->m_pkthdr.rcvif = ifp;
1898 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1899 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1901 * If tmp_tp is null, we've recieved a packet when all
1902 * our links are down. Weird, but process it anyways.
1904 if ((tmp_tp == NULL || tmp_tp == lp)) {
1905 m->m_pkthdr.rcvif = ifp;
1918 lagg_lb_attach(struct lagg_softc *sc)
1920 struct lagg_port *lp;
1923 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
1925 sc->sc_detach = lagg_lb_detach;
1926 sc->sc_start = lagg_lb_start;
1927 sc->sc_input = lagg_lb_input;
1928 sc->sc_port_create = lagg_lb_port_create;
1929 sc->sc_port_destroy = lagg_lb_port_destroy;
1930 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1932 lb->lb_key = arc4random();
1933 sc->sc_psc = (caddr_t)lb;
1935 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1936 lagg_lb_port_create(lp);
1940 lagg_lb_detach(struct lagg_softc *sc)
1942 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1950 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1952 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1953 struct lagg_port *lp_next;
1956 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1957 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1960 if (i >= LAGG_MAX_PORTS)
1962 if (sc->sc_ifflags & IFF_DEBUG)
1963 printf("%s: port %s at index %d\n",
1964 sc->sc_ifname, lp_next->lp_ifname, i);
1965 lb->lb_ports[i++] = lp_next;
1972 lagg_lb_port_create(struct lagg_port *lp)
1974 struct lagg_softc *sc = lp->lp_softc;
1975 return (lagg_lb_porttable(sc, NULL));
1979 lagg_lb_port_destroy(struct lagg_port *lp)
1981 struct lagg_softc *sc = lp->lp_softc;
1982 lagg_lb_porttable(sc, lp);
1986 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1988 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1989 struct lagg_port *lp = NULL;
1992 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
1993 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
1994 p = m->m_pkthdr.flowid >> sc->flowid_shift;
1996 p = lagg_hashmbuf(sc, m, lb->lb_key);
1998 lp = lb->lb_ports[p];
2001 * Check the port's link state. This will return the next active
2002 * port if the link is down or the port is NULL.
2004 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2010 return (lagg_enqueue(lp->lp_ifp, m));
2013 static struct mbuf *
2014 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2016 struct ifnet *ifp = sc->sc_ifp;
2018 /* Just pass in the packet to our lagg device */
2019 m->m_pkthdr.rcvif = ifp;
2028 lagg_lacp_attach(struct lagg_softc *sc)
2030 struct lagg_port *lp;
2032 sc->sc_detach = lagg_lacp_detach;
2033 sc->sc_port_create = lacp_port_create;
2034 sc->sc_port_destroy = lacp_port_destroy;
2035 sc->sc_linkstate = lacp_linkstate;
2036 sc->sc_start = lagg_lacp_start;
2037 sc->sc_input = lagg_lacp_input;
2038 sc->sc_init = lacp_init;
2039 sc->sc_stop = lacp_stop;
2040 sc->sc_lladdr = lagg_lacp_lladdr;
2041 sc->sc_req = lacp_req;
2042 sc->sc_portreq = lacp_portreq;
2046 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2047 lacp_port_create(lp);
2051 lagg_lacp_detach(struct lagg_softc *sc)
2053 struct lagg_port *lp;
2056 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2057 lacp_port_destroy(lp);
2069 lagg_lacp_lladdr(struct lagg_softc *sc)
2071 struct lagg_port *lp;
2073 /* purge all the lacp ports */
2074 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2075 lacp_port_destroy(lp);
2077 /* add them back in */
2078 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2079 lacp_port_create(lp);
2083 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2085 struct lagg_port *lp;
2087 lp = lacp_select_tx_port(sc, m);
2094 return (lagg_enqueue(lp->lp_ifp, m));
2097 static struct mbuf *
2098 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2100 struct ifnet *ifp = sc->sc_ifp;
2101 struct ether_header *eh;
2104 eh = mtod(m, struct ether_header *);
2105 etype = ntohs(eh->ether_type);
2107 /* Tap off LACP control messages */
2108 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2109 m = lacp_input(lp, m);
2115 * If the port is not collecting or not in the active aggregator then
2118 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2123 m->m_pkthdr.rcvif = ifp;
2128 lagg_callout(void *arg)
2130 struct lagg_softc *sc = (struct lagg_softc *)arg;
2131 struct ifnet *ifp = sc->sc_ifp;
2133 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
2134 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
2135 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
2136 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2138 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);