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>
6 * Copyright (c) 2014 Marcelo Araujo <araujo@FreeBSD.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 #include "opt_inet6.h"
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
37 #include <sys/systm.h>
41 #include <sys/rmlock.h>
42 #include <sys/taskqueue.h>
43 #include <sys/eventhandler.h>
45 #include <net/ethernet.h>
47 #include <net/if_clone.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
55 #if defined(INET) || defined(INET6)
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/if_ether.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_ifattach.h>
70 #include <net/if_vlan_var.h>
71 #include <net/if_lagg.h>
72 #include <net/ieee8023ad_lacp.h>
74 /* Special flags we should propagate to the lagg ports. */
77 int (*func)(struct ifnet *, int);
79 {IFF_PROMISC, ifpromisc},
80 {IFF_ALLMULTI, if_allmulti},
84 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
85 static struct mtx lagg_list_mtx;
86 eventhandler_tag lagg_detach_cookie = NULL;
88 static int lagg_clone_create(struct if_clone *, int, caddr_t);
89 static void lagg_clone_destroy(struct ifnet *);
90 static 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 uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
119 static int lagg_transmit(struct ifnet *, struct mbuf *);
120 static void lagg_qflush(struct ifnet *);
121 static int lagg_media_change(struct ifnet *);
122 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
123 static struct lagg_port *lagg_link_active(struct lagg_softc *,
125 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
126 static int lagg_sysctl_active(SYSCTL_HANDLER_ARGS);
128 /* Simple round robin */
129 static void lagg_rr_attach(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_start(struct lagg_softc *, struct mbuf *);
136 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
140 static void lagg_lb_attach(struct lagg_softc *);
141 static void lagg_lb_detach(struct lagg_softc *);
142 static int lagg_lb_port_create(struct lagg_port *);
143 static void lagg_lb_port_destroy(struct lagg_port *);
144 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
145 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
147 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
150 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
151 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
155 static void lagg_lacp_attach(struct lagg_softc *);
156 static void lagg_lacp_detach(struct lagg_softc *);
157 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
158 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
160 static void lagg_lacp_lladdr(struct lagg_softc *);
162 /* lagg protocol table */
163 static const struct lagg_proto {
165 void (*pr_attach)(struct lagg_softc *);
166 void (*pr_detach)(struct lagg_softc *);
167 int (*pr_start)(struct lagg_softc *, struct mbuf *);
168 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
170 int (*pr_addport)(struct lagg_port *);
171 void (*pr_delport)(struct lagg_port *);
172 void (*pr_linkstate)(struct lagg_port *);
173 void (*pr_init)(struct lagg_softc *);
174 void (*pr_stop)(struct lagg_softc *);
175 void (*pr_lladdr)(struct lagg_softc *);
176 void (*pr_request)(struct lagg_softc *, void *);
177 void (*pr_portreq)(struct lagg_port *, void *);
180 .pr_num = LAGG_PROTO_NONE
183 .pr_num = LAGG_PROTO_ROUNDROBIN,
184 .pr_attach = lagg_rr_attach,
185 .pr_start = lagg_rr_start,
186 .pr_input = lagg_rr_input,
189 .pr_num = LAGG_PROTO_FAILOVER,
190 .pr_start = lagg_fail_start,
191 .pr_input = lagg_fail_input,
194 .pr_num = LAGG_PROTO_LOADBALANCE,
195 .pr_attach = lagg_lb_attach,
196 .pr_detach = lagg_lb_detach,
197 .pr_start = lagg_lb_start,
198 .pr_input = lagg_lb_input,
199 .pr_addport = lagg_lb_port_create,
200 .pr_delport = lagg_lb_port_destroy,
203 .pr_num = LAGG_PROTO_LACP,
204 .pr_attach = lagg_lacp_attach,
205 .pr_detach = lagg_lacp_detach,
206 .pr_start = lagg_lacp_start,
207 .pr_input = lagg_lacp_input,
208 .pr_addport = lacp_port_create,
209 .pr_delport = lacp_port_destroy,
210 .pr_linkstate = lacp_linkstate,
211 .pr_init = lacp_init,
212 .pr_stop = lacp_stop,
213 .pr_lladdr = lagg_lacp_lladdr,
214 .pr_request = lacp_req,
215 .pr_portreq = lacp_portreq,
218 .pr_num = LAGG_PROTO_ETHERCHANNEL,
219 .pr_attach = lagg_lb_attach,
220 .pr_detach = lagg_lb_detach,
221 .pr_start = lagg_lb_start,
222 .pr_input = lagg_lb_input,
225 .pr_num = LAGG_PROTO_BROADCAST,
226 .pr_start = lagg_bcast_start,
227 .pr_input = lagg_bcast_input,
231 SYSCTL_DECL(_net_link);
232 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
235 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
236 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
237 &lagg_failover_rx_all, 0,
238 "Accept input from any interface in a failover lagg");
239 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
240 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
242 "Default setting for using flow id for load sharing");
243 static int def_flowid_shift = 16; /* Default value for using M_FLOWID */
244 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
245 &def_flowid_shift, 0,
246 "Default setting for flowid shift for load sharing");
249 lagg_modevent(module_t mod, int type, void *data)
254 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
255 SLIST_INIT(&lagg_list);
256 lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
257 lagg_clone_destroy, 0);
258 lagg_input_p = lagg_input;
259 lagg_linkstate_p = lagg_port_state;
260 lagg_detach_cookie = EVENTHANDLER_REGISTER(
261 ifnet_departure_event, lagg_port_ifdetach, NULL,
262 EVENTHANDLER_PRI_ANY);
265 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
267 if_clone_detach(lagg_cloner);
269 lagg_linkstate_p = NULL;
270 mtx_destroy(&lagg_list_mtx);
278 static moduledata_t lagg_mod = {
284 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
285 MODULE_VERSION(if_lagg, 1);
288 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
291 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
294 if (sc->sc_ifflags & IFF_DEBUG)
295 if_printf(sc->sc_ifp, "using proto %u\n", pr);
297 if (lagg_protos[pr].pr_attach != NULL)
298 lagg_protos[pr].pr_attach(sc);
303 lagg_proto_detach(struct lagg_softc *sc)
307 LAGG_WLOCK_ASSERT(sc);
310 sc->sc_proto = LAGG_PROTO_NONE;
312 if (lagg_protos[pr].pr_detach != NULL)
313 lagg_protos[pr].pr_detach(sc);
319 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
322 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
326 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
329 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
333 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
336 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
339 return (lagg_protos[sc->sc_proto].pr_addport(lp));
343 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
346 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
347 lagg_protos[sc->sc_proto].pr_delport(lp);
351 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
354 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
355 lagg_protos[sc->sc_proto].pr_linkstate(lp);
359 lagg_proto_init(struct lagg_softc *sc)
362 if (lagg_protos[sc->sc_proto].pr_init != NULL)
363 lagg_protos[sc->sc_proto].pr_init(sc);
367 lagg_proto_stop(struct lagg_softc *sc)
370 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
371 lagg_protos[sc->sc_proto].pr_stop(sc);
375 lagg_proto_lladdr(struct lagg_softc *sc)
378 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
379 lagg_protos[sc->sc_proto].pr_lladdr(sc);
383 lagg_proto_request(struct lagg_softc *sc, void *v)
386 if (lagg_protos[sc->sc_proto].pr_request != NULL)
387 lagg_protos[sc->sc_proto].pr_request(sc, v);
391 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
394 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
395 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
399 * This routine is run via an vlan
403 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
405 struct lagg_softc *sc = ifp->if_softc;
406 struct lagg_port *lp;
407 struct rm_priotracker tracker;
409 if (ifp->if_softc != arg) /* Not our event */
412 LAGG_RLOCK(sc, &tracker);
413 if (!SLIST_EMPTY(&sc->sc_ports)) {
414 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
415 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
417 LAGG_RUNLOCK(sc, &tracker);
421 * This routine is run via an vlan
425 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
427 struct lagg_softc *sc = ifp->if_softc;
428 struct lagg_port *lp;
429 struct rm_priotracker tracker;
431 if (ifp->if_softc != arg) /* Not our event */
434 LAGG_RLOCK(sc, &tracker);
435 if (!SLIST_EMPTY(&sc->sc_ports)) {
436 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
437 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
439 LAGG_RUNLOCK(sc, &tracker);
443 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
445 struct lagg_softc *sc;
447 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
448 struct sysctl_oid *oid;
449 char num[14]; /* sufficient for 32 bits */
451 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
452 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
458 sysctl_ctx_init(&sc->ctx);
459 snprintf(num, sizeof(num), "%u", unit);
460 sc->use_flowid = def_use_flowid;
461 sc->flowid_shift = def_flowid_shift;
462 sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
463 &SYSCTL_NODE_CHILDREN(_net_link, lagg),
464 OID_AUTO, num, CTLFLAG_RD, NULL, "");
465 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
466 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid,
467 sc->use_flowid, "Use flow id for load sharing");
468 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
469 "flowid_shift", CTLTYPE_INT|CTLFLAG_RW, &sc->flowid_shift,
471 "Shift flowid bits to prevent multiqueue collisions");
472 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
473 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
474 "Total number of ports");
475 SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
476 "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
477 "I", "Total number of active ports");
478 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
479 "flapping", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_flapping,
480 sc->sc_flapping, "Total number of port change events");
481 /* Hash all layers by default */
482 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
484 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
487 SLIST_INIT(&sc->sc_ports);
488 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
490 /* Initialise pseudo media types */
491 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
493 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
494 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
496 if_initname(ifp, laggname, unit);
498 ifp->if_transmit = lagg_transmit;
499 ifp->if_qflush = lagg_qflush;
500 ifp->if_init = lagg_init;
501 ifp->if_ioctl = lagg_ioctl;
502 ifp->if_get_counter = lagg_get_counter;
503 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
504 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
507 * Attach as an ordinary ethernet device, children will be attached
508 * as special device IFT_IEEE8023ADLAG.
510 ether_ifattach(ifp, eaddr);
512 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
513 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
514 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
515 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
517 /* Insert into the global list of laggs */
518 mtx_lock(&lagg_list_mtx);
519 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
520 mtx_unlock(&lagg_list_mtx);
526 lagg_clone_destroy(struct ifnet *ifp)
528 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
529 struct lagg_port *lp;
534 ifp->if_flags &= ~IFF_UP;
536 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
537 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
539 /* Shutdown and remove lagg ports */
540 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
541 lagg_port_destroy(lp, 1);
542 /* Unhook the aggregation protocol */
543 lagg_proto_detach(sc);
545 sysctl_ctx_free(&sc->ctx);
546 ifmedia_removeall(&sc->sc_media);
550 mtx_lock(&lagg_list_mtx);
551 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
552 mtx_unlock(&lagg_list_mtx);
554 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
555 LAGG_LOCK_DESTROY(sc);
560 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
562 struct ifnet *ifp = sc->sc_ifp;
564 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
567 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
568 /* Let the protocol know the MAC has changed */
569 lagg_proto_lladdr(sc);
570 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
574 lagg_capabilities(struct lagg_softc *sc)
576 struct lagg_port *lp;
577 int cap = ~0, ena = ~0;
579 struct ifnet_hw_tsomax hw_tsomax;
581 LAGG_WLOCK_ASSERT(sc);
583 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
585 /* Get capabilities from the lagg ports */
586 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
587 cap &= lp->lp_ifp->if_capabilities;
588 ena &= lp->lp_ifp->if_capenable;
589 hwa &= lp->lp_ifp->if_hwassist;
590 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
592 cap = (cap == ~0 ? 0 : cap);
593 ena = (ena == ~0 ? 0 : ena);
594 hwa = (hwa == ~0 ? 0 : hwa);
596 if (sc->sc_ifp->if_capabilities != cap ||
597 sc->sc_ifp->if_capenable != ena ||
598 sc->sc_ifp->if_hwassist != hwa ||
599 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
600 sc->sc_ifp->if_capabilities = cap;
601 sc->sc_ifp->if_capenable = ena;
602 sc->sc_ifp->if_hwassist = hwa;
603 getmicrotime(&sc->sc_ifp->if_lastchange);
605 if (sc->sc_ifflags & IFF_DEBUG)
606 if_printf(sc->sc_ifp,
607 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
612 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
614 struct lagg_softc *sc = lp->lp_softc;
615 struct ifnet *ifp = lp->lp_ifp;
616 struct lagg_llq *llq;
619 LAGG_WLOCK_ASSERT(sc);
621 if (lp->lp_detaching ||
622 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
625 /* Check to make sure its not already queued to be changed */
626 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
627 if (llq->llq_ifp == ifp) {
634 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
635 if (llq == NULL) /* XXX what to do */
639 /* Update the lladdr even if pending, it may have changed */
641 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
644 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
646 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
650 * Set the interface MAC address from a taskqueue to avoid a LOR.
653 lagg_port_setlladdr(void *arg, int pending)
655 struct lagg_softc *sc = (struct lagg_softc *)arg;
656 struct lagg_llq *llq, *head;
660 /* Grab a local reference of the queue and remove it from the softc */
662 head = SLIST_FIRST(&sc->sc_llq_head);
663 SLIST_FIRST(&sc->sc_llq_head) = NULL;
667 * Traverse the queue and set the lladdr on each ifp. It is safe to do
668 * unlocked as we have the only reference to it.
670 for (llq = head; llq != NULL; llq = head) {
673 /* Set the link layer address */
674 CURVNET_SET(ifp->if_vnet);
675 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
678 printf("%s: setlladdr failed on %s\n", __func__,
681 head = SLIST_NEXT(llq, llq_entries);
687 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
689 struct lagg_softc *sc_ptr;
690 struct lagg_port *lp, *tlp;
694 LAGG_WLOCK_ASSERT(sc);
696 /* Limit the maximal number of lagg ports */
697 if (sc->sc_count >= LAGG_MAX_PORTS)
700 /* Check if port has already been associated to a lagg */
701 if (ifp->if_lagg != NULL) {
702 /* Port is already in the current lagg? */
703 lp = (struct lagg_port *)ifp->if_lagg;
704 if (lp->lp_softc == sc)
709 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
710 if (ifp->if_type != IFT_ETHER)
711 return (EPROTONOSUPPORT);
715 * The member interface should not have inet6 address because
716 * two interfaces with a valid link-local scope zone must not be
717 * merged in any form. This restriction is needed to
718 * prevent violation of link-local scope zone. Attempts to
719 * add a member interface which has inet6 addresses triggers
720 * removal of all inet6 addresses on the member interface.
722 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
723 if (in6ifa_llaonifp(lp->lp_ifp)) {
724 in6_ifdetach(lp->lp_ifp);
725 if_printf(sc->sc_ifp,
726 "IPv6 addresses on %s have been removed "
727 "before adding it as a member to prevent "
728 "IPv6 address scope violation.\n",
729 lp->lp_ifp->if_xname);
732 if (in6ifa_llaonifp(ifp)) {
734 if_printf(sc->sc_ifp,
735 "IPv6 addresses on %s have been removed "
736 "before adding it as a member to prevent "
737 "IPv6 address scope violation.\n",
741 /* Allow the first Ethernet member to define the MTU */
742 if (SLIST_EMPTY(&sc->sc_ports))
743 sc->sc_ifp->if_mtu = ifp->if_mtu;
744 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
745 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
750 if ((lp = malloc(sizeof(struct lagg_port),
751 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
754 /* Check if port is a stacked lagg */
755 mtx_lock(&lagg_list_mtx);
756 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
757 if (ifp == sc_ptr->sc_ifp) {
758 mtx_unlock(&lagg_list_mtx);
761 /* XXX disable stacking for the moment, its untested */
762 #ifdef LAGG_PORT_STACKING
763 lp->lp_flags |= LAGG_PORT_STACK;
764 if (lagg_port_checkstacking(sc_ptr) >=
766 mtx_unlock(&lagg_list_mtx);
773 mtx_unlock(&lagg_list_mtx);
775 /* Change the interface type */
776 lp->lp_iftype = ifp->if_type;
777 ifp->if_type = IFT_IEEE8023ADLAG;
779 lp->lp_ioctl = ifp->if_ioctl;
780 ifp->if_ioctl = lagg_port_ioctl;
781 lp->lp_output = ifp->if_output;
782 ifp->if_output = lagg_port_output;
787 /* Save port link layer address */
788 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
790 if (SLIST_EMPTY(&sc->sc_ports)) {
792 lagg_lladdr(sc, IF_LLADDR(ifp));
794 /* Update link layer address for this port */
795 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
798 /* Insert into the list of ports. Keep ports sorted by if_index. */
799 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
800 if (tlp->lp_ifp->if_index < ifp->if_index && (
801 SLIST_NEXT(tlp, lp_entries) == NULL ||
802 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index <
807 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
809 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
812 /* Update lagg capabilities */
813 lagg_capabilities(sc);
816 /* Read port counters */
817 pval = lp->port_counters.val;
818 for (i = 0; i < IFCOUNTERS; i++, pval++)
819 *pval = ifp->if_get_counter(ifp, i);
820 /* Add multicast addresses and interface flags to this port */
821 lagg_ether_cmdmulti(lp, 1);
822 lagg_setflags(lp, 1);
824 if ((error = lagg_proto_addport(sc, lp)) != 0) {
825 /* Remove the port, without calling pr_delport. */
826 lagg_port_destroy(lp, 0);
833 #ifdef LAGG_PORT_STACKING
835 lagg_port_checkstacking(struct lagg_softc *sc)
837 struct lagg_softc *sc_ptr;
838 struct lagg_port *lp;
841 LAGG_WLOCK_ASSERT(sc);
843 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
844 if (lp->lp_flags & LAGG_PORT_STACK) {
845 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
846 m = MAX(m, lagg_port_checkstacking(sc_ptr));
855 lagg_port_destroy(struct lagg_port *lp, int rundelport)
857 struct lagg_softc *sc = lp->lp_softc;
858 struct lagg_port *lp_ptr;
859 struct lagg_llq *llq;
860 struct ifnet *ifp = lp->lp_ifp;
861 uint64_t *pval, vdiff;
864 LAGG_WLOCK_ASSERT(sc);
867 lagg_proto_delport(sc, lp);
870 * Remove multicast addresses and interface flags from this port and
871 * reset the MAC address, skip if the interface is being detached.
873 if (!lp->lp_detaching) {
874 lagg_ether_cmdmulti(lp, 0);
875 lagg_setflags(lp, 0);
876 lagg_port_lladdr(lp, lp->lp_lladdr);
879 /* Restore interface */
880 ifp->if_type = lp->lp_iftype;
881 ifp->if_ioctl = lp->lp_ioctl;
882 ifp->if_output = lp->lp_output;
885 /* Update detached port counters */
886 pval = lp->port_counters.val;
887 for (i = 0; i < IFCOUNTERS; i++, pval++) {
888 vdiff = ifp->if_get_counter(ifp, i) - *pval;
889 sc->detached_counters.val[i] += vdiff;
892 /* Finally, remove the port from the lagg */
893 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
896 /* Update the primary interface */
897 if (lp == sc->sc_primary) {
898 uint8_t lladdr[ETHER_ADDR_LEN];
900 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
901 bzero(&lladdr, ETHER_ADDR_LEN);
903 bcopy(lp_ptr->lp_lladdr,
904 lladdr, ETHER_ADDR_LEN);
906 lagg_lladdr(sc, lladdr);
907 sc->sc_primary = lp_ptr;
909 /* Update link layer address for each port */
910 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
911 lagg_port_lladdr(lp_ptr, lladdr);
914 /* Remove any pending lladdr changes from the queue */
915 if (lp->lp_detaching) {
916 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
917 if (llq->llq_ifp == ifp) {
918 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
921 break; /* Only appears once */
927 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
931 /* Update lagg capabilities */
932 lagg_capabilities(sc);
939 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
941 struct lagg_reqport *rp = (struct lagg_reqport *)data;
942 struct lagg_softc *sc;
943 struct lagg_port *lp = NULL;
945 struct rm_priotracker tracker;
947 /* Should be checked by the caller */
948 if (ifp->if_type != IFT_IEEE8023ADLAG ||
949 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
954 if (rp->rp_portname[0] == '\0' ||
955 ifunit(rp->rp_portname) != ifp) {
960 LAGG_RLOCK(sc, &tracker);
961 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
963 LAGG_RUNLOCK(sc, &tracker);
967 lagg_port2req(lp, rp);
968 LAGG_RUNLOCK(sc, &tracker);
972 if (lp->lp_ioctl == NULL) {
976 error = (*lp->lp_ioctl)(ifp, cmd, data);
980 /* Update lagg interface capabilities */
982 lagg_capabilities(sc);
987 /* Do not allow the MTU to be changed once joined */
998 if (lp->lp_ioctl != NULL)
999 return ((*lp->lp_ioctl)(ifp, cmd, data));
1005 * Requests counter @cnt data.
1007 * Counter value is calculated the following way:
1008 * 1) for each port, sum difference between current and "initial" measurements.
1009 * 2) add lagg logical interface counters.
1010 * 3) add data from detached_counters array.
1012 * We also do the following things on ports attach/detach:
1013 * 1) On port attach we store all counters it has into port_counter array.
1014 * 2) On port detach we add the different between "initial" and
1015 * current counters data to detached_counters array.
1018 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1020 struct lagg_softc *sc;
1021 struct lagg_port *lp;
1022 struct ifnet *lpifp;
1023 struct rm_priotracker tracker;
1024 uint64_t newval, oldval, vsum;
1026 /* Revise this when we've got non-generic counters. */
1027 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1029 sc = (struct lagg_softc *)ifp->if_softc;
1030 LAGG_RLOCK(sc, &tracker);
1033 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1034 /* Saved attached value */
1035 oldval = lp->port_counters.val[cnt];
1038 newval = lpifp->if_get_counter(lpifp, cnt);
1039 /* Calculate diff and save new */
1040 vsum += newval - oldval;
1044 * Add counter data which might be added by upper
1045 * layer protocols operating on logical interface.
1047 vsum += if_get_counter_default(ifp, cnt);
1050 * Add counter data from detached ports counters
1052 vsum += sc->detached_counters.val[cnt];
1054 LAGG_RUNLOCK(sc, &tracker);
1060 * For direct output to child ports.
1063 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1064 const struct sockaddr *dst, struct route *ro)
1066 struct lagg_port *lp = ifp->if_lagg;
1068 switch (dst->sa_family) {
1069 case pseudo_AF_HDRCMPLT:
1071 return ((*lp->lp_output)(ifp, m, dst, ro));
1074 /* drop any other frames */
1080 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1082 struct lagg_port *lp;
1083 struct lagg_softc *sc;
1085 if ((lp = ifp->if_lagg) == NULL)
1087 /* If the ifnet is just being renamed, don't do anything. */
1088 if (ifp->if_flags & IFF_RENAMING)
1094 lp->lp_detaching = 1;
1095 lagg_port_destroy(lp, 1);
1100 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1102 struct lagg_softc *sc = lp->lp_softc;
1104 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1105 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1106 rp->rp_prio = lp->lp_prio;
1107 rp->rp_flags = lp->lp_flags;
1108 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1110 /* Add protocol specific flags */
1111 switch (sc->sc_proto) {
1112 case LAGG_PROTO_FAILOVER:
1113 if (lp == sc->sc_primary)
1114 rp->rp_flags |= LAGG_PORT_MASTER;
1115 if (lp == lagg_link_active(sc, sc->sc_primary))
1116 rp->rp_flags |= LAGG_PORT_ACTIVE;
1119 case LAGG_PROTO_ROUNDROBIN:
1120 case LAGG_PROTO_LOADBALANCE:
1121 case LAGG_PROTO_ETHERCHANNEL:
1122 case LAGG_PROTO_BROADCAST:
1123 if (LAGG_PORTACTIVE(lp))
1124 rp->rp_flags |= LAGG_PORT_ACTIVE;
1127 case LAGG_PROTO_LACP:
1128 /* LACP has a different definition of active */
1129 if (lacp_isactive(lp))
1130 rp->rp_flags |= LAGG_PORT_ACTIVE;
1131 if (lacp_iscollecting(lp))
1132 rp->rp_flags |= LAGG_PORT_COLLECTING;
1133 if (lacp_isdistributing(lp))
1134 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1141 lagg_init(void *xsc)
1143 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1144 struct lagg_port *lp;
1145 struct ifnet *ifp = sc->sc_ifp;
1147 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1152 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1153 /* Update the port lladdrs */
1154 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1155 lagg_port_lladdr(lp, IF_LLADDR(ifp));
1157 lagg_proto_init(sc);
1163 lagg_stop(struct lagg_softc *sc)
1165 struct ifnet *ifp = sc->sc_ifp;
1167 LAGG_WLOCK_ASSERT(sc);
1169 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1172 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1174 lagg_proto_stop(sc);
1178 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1180 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1181 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1182 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1183 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1184 struct ifreq *ifr = (struct ifreq *)data;
1185 struct lagg_port *lp;
1187 struct thread *td = curthread;
1189 int count, buflen, len, error = 0;
1190 struct rm_priotracker tracker;
1192 bzero(&rpbuf, sizeof(rpbuf));
1196 LAGG_RLOCK(sc, &tracker);
1198 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1200 buflen = count * sizeof(struct lagg_reqport);
1201 LAGG_RUNLOCK(sc, &tracker);
1203 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1205 LAGG_RLOCK(sc, &tracker);
1206 ra->ra_proto = sc->sc_proto;
1207 lagg_proto_request(sc, &ra->ra_psc);
1210 len = min(ra->ra_size, buflen);
1211 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1212 if (len < sizeof(rpbuf))
1215 lagg_port2req(lp, &rpbuf);
1216 memcpy(buf, &rpbuf, sizeof(rpbuf));
1218 buf += sizeof(rpbuf);
1219 len -= sizeof(rpbuf);
1221 LAGG_RUNLOCK(sc, &tracker);
1222 ra->ra_ports = count;
1223 ra->ra_size = count * sizeof(rpbuf);
1224 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1225 free(outbuf, M_TEMP);
1228 error = priv_check(td, PRIV_NET_LAGG);
1231 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) {
1232 error = EPROTONOSUPPORT;
1237 lagg_proto_detach(sc);
1238 lagg_proto_attach(sc, ra->ra_proto);
1240 case SIOCGLAGGFLAGS:
1241 rf->rf_flags = sc->sc_flags;
1244 error = priv_check(td, PRIV_NET_LAGG);
1247 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1252 sc->sc_flags &= ~LAGG_F_HASHMASK;
1253 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1257 if (rp->rp_portname[0] == '\0' ||
1258 (tpif = ifunit(rp->rp_portname)) == NULL) {
1263 LAGG_RLOCK(sc, &tracker);
1264 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1265 lp->lp_softc != sc) {
1267 LAGG_RUNLOCK(sc, &tracker);
1271 lagg_port2req(lp, rp);
1272 LAGG_RUNLOCK(sc, &tracker);
1275 error = priv_check(td, PRIV_NET_LAGG);
1278 if (rp->rp_portname[0] == '\0' ||
1279 (tpif = ifunit(rp->rp_portname)) == NULL) {
1284 error = lagg_port_create(sc, tpif);
1287 case SIOCSLAGGDELPORT:
1288 error = priv_check(td, PRIV_NET_LAGG);
1291 if (rp->rp_portname[0] == '\0' ||
1292 (tpif = ifunit(rp->rp_portname)) == NULL) {
1298 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1299 lp->lp_softc != sc) {
1305 error = lagg_port_destroy(lp, 1);
1309 /* Set flags on ports too */
1311 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1312 lagg_setflags(lp, 1);
1316 if (!(ifp->if_flags & IFF_UP) &&
1317 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1319 * If interface is marked down and it is running,
1320 * then stop and disable it.
1325 } else if ((ifp->if_flags & IFF_UP) &&
1326 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1328 * If interface is marked up and it is stopped, then
1331 (*ifp->if_init)(sc);
1337 error = lagg_ether_setmulti(sc);
1342 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1347 /* Do not allow the MTU or caps to be directly changed */
1352 error = ether_ioctl(ifp, cmd, data);
1359 lagg_ether_setmulti(struct lagg_softc *sc)
1361 struct lagg_port *lp;
1363 LAGG_WLOCK_ASSERT(sc);
1365 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1366 /* First, remove any existing filter entries. */
1367 lagg_ether_cmdmulti(lp, 0);
1368 /* copy all addresses from the lagg interface to the port */
1369 lagg_ether_cmdmulti(lp, 1);
1375 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1377 struct lagg_softc *sc = lp->lp_softc;
1378 struct ifnet *ifp = lp->lp_ifp;
1379 struct ifnet *scifp = sc->sc_ifp;
1381 struct ifmultiaddr *ifma;
1384 LAGG_WLOCK_ASSERT(sc);
1387 IF_ADDR_WLOCK(scifp);
1388 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1389 if (ifma->ifma_addr->sa_family != AF_LINK)
1391 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1393 IF_ADDR_WUNLOCK(scifp);
1396 bcopy(ifma->ifma_addr, &mc->mc_addr,
1397 ifma->ifma_addr->sa_len);
1398 mc->mc_addr.sdl_index = ifp->if_index;
1400 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1402 IF_ADDR_WUNLOCK(scifp);
1403 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1404 error = if_addmulti(ifp,
1405 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1410 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1411 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1412 if (mc->mc_ifma && !lp->lp_detaching)
1413 if_delmulti_ifma(mc->mc_ifma);
1420 /* Handle a ref counted flag that should be set on the lagg port as well */
1422 lagg_setflag(struct lagg_port *lp, int flag, int status,
1423 int (*func)(struct ifnet *, int))
1425 struct lagg_softc *sc = lp->lp_softc;
1426 struct ifnet *scifp = sc->sc_ifp;
1427 struct ifnet *ifp = lp->lp_ifp;
1430 LAGG_WLOCK_ASSERT(sc);
1432 status = status ? (scifp->if_flags & flag) : 0;
1433 /* Now "status" contains the flag value or 0 */
1436 * See if recorded ports status is different from what
1437 * we want it to be. If it is, flip it. We record ports
1438 * status in lp_ifflags so that we won't clear ports flag
1439 * we haven't set. In fact, we don't clear or set ports
1440 * flags directly, but get or release references to them.
1441 * That's why we can be sure that recorded flags still are
1442 * in accord with actual ports flags.
1444 if (status != (lp->lp_ifflags & flag)) {
1445 error = (*func)(ifp, status);
1448 lp->lp_ifflags &= ~flag;
1449 lp->lp_ifflags |= status;
1455 * Handle IFF_* flags that require certain changes on the lagg port
1456 * if "status" is true, update ports flags respective to the lagg
1457 * if "status" is false, forcedly clear the flags set on port.
1460 lagg_setflags(struct lagg_port *lp, int status)
1464 for (i = 0; lagg_pflags[i].flag; i++) {
1465 error = lagg_setflag(lp, lagg_pflags[i].flag,
1466 status, lagg_pflags[i].func);
1474 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1476 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1477 int error, len, mcast;
1478 struct rm_priotracker tracker;
1480 len = m->m_pkthdr.len;
1481 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1483 LAGG_RLOCK(sc, &tracker);
1484 /* We need a Tx algorithm and at least one port */
1485 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1486 LAGG_RUNLOCK(sc, &tracker);
1488 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1492 ETHER_BPF_MTAP(ifp, m);
1494 error = lagg_proto_start(sc, m);
1495 LAGG_RUNLOCK(sc, &tracker);
1498 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1504 * The ifp->if_qflush entry point for lagg(4) is no-op.
1507 lagg_qflush(struct ifnet *ifp __unused)
1511 static struct mbuf *
1512 lagg_input(struct ifnet *ifp, struct mbuf *m)
1514 struct lagg_port *lp = ifp->if_lagg;
1515 struct lagg_softc *sc = lp->lp_softc;
1516 struct ifnet *scifp = sc->sc_ifp;
1517 struct rm_priotracker tracker;
1519 LAGG_RLOCK(sc, &tracker);
1520 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1521 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1522 sc->sc_proto == LAGG_PROTO_NONE) {
1523 LAGG_RUNLOCK(sc, &tracker);
1528 ETHER_BPF_MTAP(scifp, m);
1530 m = lagg_proto_input(sc, lp, m);
1533 if (scifp->if_flags & IFF_MONITOR) {
1539 LAGG_RUNLOCK(sc, &tracker);
1544 lagg_media_change(struct ifnet *ifp)
1546 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1548 if (sc->sc_ifflags & IFF_DEBUG)
1549 printf("%s\n", __func__);
1556 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1558 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1559 struct lagg_port *lp;
1560 struct rm_priotracker tracker;
1562 imr->ifm_status = IFM_AVALID;
1563 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1565 LAGG_RLOCK(sc, &tracker);
1566 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1567 if (LAGG_PORTACTIVE(lp))
1568 imr->ifm_status |= IFM_ACTIVE;
1570 LAGG_RUNLOCK(sc, &tracker);
1574 lagg_linkstate(struct lagg_softc *sc)
1576 struct lagg_port *lp;
1577 int new_link = LINK_STATE_DOWN;
1580 /* Our link is considered up if at least one of our ports is active */
1581 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1582 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1583 new_link = LINK_STATE_UP;
1587 if_link_state_change(sc->sc_ifp, new_link);
1589 /* Update if_baudrate to reflect the max possible speed */
1590 switch (sc->sc_proto) {
1591 case LAGG_PROTO_FAILOVER:
1592 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1593 sc->sc_primary->lp_ifp->if_baudrate : 0;
1595 case LAGG_PROTO_ROUNDROBIN:
1596 case LAGG_PROTO_LOADBALANCE:
1597 case LAGG_PROTO_ETHERCHANNEL:
1598 case LAGG_PROTO_BROADCAST:
1600 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1601 speed += lp->lp_ifp->if_baudrate;
1602 sc->sc_ifp->if_baudrate = speed;
1604 case LAGG_PROTO_LACP:
1605 /* LACP updates if_baudrate itself */
1611 lagg_port_state(struct ifnet *ifp, int state)
1613 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1614 struct lagg_softc *sc = NULL;
1623 lagg_proto_linkstate(sc, lp);
1628 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1630 struct lagg_port *lp_next, *rval = NULL;
1631 // int new_link = LINK_STATE_DOWN;
1633 LAGG_RLOCK_ASSERT(sc);
1635 * Search a port which reports an active link state.
1640 if (LAGG_PORTACTIVE(lp)) {
1644 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1645 LAGG_PORTACTIVE(lp_next)) {
1651 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1652 if (LAGG_PORTACTIVE(lp_next)) {
1661 * The IEEE 802.1D standard assumes that a lagg with
1662 * multiple ports is always full duplex. This is valid
1663 * for load sharing laggs and if at least two links
1664 * are active. Unfortunately, checking the latter would
1665 * be too expensive at this point.
1667 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1669 new_link = LINK_STATE_FULL_DUPLEX;
1671 new_link = rval->lp_link_state;
1679 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1681 if (m->m_pkthdr.len < (off + len)) {
1683 } else if (m->m_len < (off + len)) {
1684 m_copydata(m, off, len, buf);
1687 return (mtod(m, char *) + off);
1691 lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1693 struct lagg_softc *sc = (struct lagg_softc *)arg1;
1694 struct lagg_port *lp;
1697 /* LACP tracks active links automatically, the others do not */
1698 if (sc->sc_proto != LAGG_PROTO_LACP) {
1700 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1701 sc->sc_active += LAGG_PORTACTIVE(lp);
1704 error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1705 if ((error) || (req->newptr == NULL))
1712 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1717 struct ether_header *eh;
1718 const struct ether_vlan_header *vlan;
1720 const struct ip *ip;
1721 const uint32_t *ports;
1725 const struct ip6_hdr *ip6;
1735 struct ether_vlan_header vlan;
1743 eh = mtod(m, struct ether_header *);
1744 etype = ntohs(eh->ether_type);
1745 if (sc->sc_flags & LAGG_F_HASHL2) {
1746 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1747 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1750 /* Special handling for encapsulating VLAN frames */
1751 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1752 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1753 sizeof(m->m_pkthdr.ether_vtag), p);
1754 } else if (etype == ETHERTYPE_VLAN) {
1755 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1759 if (sc->sc_flags & LAGG_F_HASHL2)
1760 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1761 etype = ntohs(vlan->evl_proto);
1762 off += sizeof(*vlan) - sizeof(*eh);
1768 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1772 if (sc->sc_flags & LAGG_F_HASHL3) {
1773 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1774 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1776 if (!(sc->sc_flags & LAGG_F_HASHL4))
1782 iphlen = ip->ip_hl << 2;
1783 if (iphlen < sizeof(*ip))
1786 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1789 p = hash32_buf(ports, sizeof(*ports), p);
1795 case ETHERTYPE_IPV6:
1796 if (!(sc->sc_flags & LAGG_F_HASHL3))
1798 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1802 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1803 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1804 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1805 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1814 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1817 return (ifp->if_transmit)(ifp, m);
1821 * Simple round robin aggregation
1824 lagg_rr_attach(struct lagg_softc *sc)
1826 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1831 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1833 struct lagg_port *lp;
1836 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1838 lp = SLIST_FIRST(&sc->sc_ports);
1840 lp = SLIST_NEXT(lp, lp_entries);
1843 * Check the port's link state. This will return the next active
1844 * port if the link is down or the port is NULL.
1846 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1852 return (lagg_enqueue(lp->lp_ifp, m));
1855 static struct mbuf *
1856 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1858 struct ifnet *ifp = sc->sc_ifp;
1860 /* Just pass in the packet to our lagg device */
1861 m->m_pkthdr.rcvif = ifp;
1870 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1872 int active_ports = 0;
1875 struct lagg_port *lp, *last = NULL;
1878 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1879 if (!LAGG_PORTACTIVE(lp))
1885 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1892 ret = lagg_enqueue(last->lp_ifp, m0);
1902 if ((last = lagg_link_active(sc, last)) == NULL) {
1907 ret = lagg_enqueue(last->lp_ifp, m);
1918 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1920 struct ifnet *ifp = sc->sc_ifp;
1922 /* Just pass in the packet to our lagg device */
1923 m->m_pkthdr.rcvif = ifp;
1931 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1933 struct lagg_port *lp;
1935 /* Use the master port if active or the next available port */
1936 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1942 return (lagg_enqueue(lp->lp_ifp, m));
1945 static struct mbuf *
1946 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1948 struct ifnet *ifp = sc->sc_ifp;
1949 struct lagg_port *tmp_tp;
1951 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1952 m->m_pkthdr.rcvif = ifp;
1956 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1957 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1959 * If tmp_tp is null, we've recieved a packet when all
1960 * our links are down. Weird, but process it anyways.
1962 if ((tmp_tp == NULL || tmp_tp == lp)) {
1963 m->m_pkthdr.rcvif = ifp;
1976 lagg_lb_attach(struct lagg_softc *sc)
1978 struct lagg_port *lp;
1981 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
1983 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1985 lb->lb_key = arc4random();
1988 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1989 lagg_lb_port_create(lp);
1993 lagg_lb_detach(struct lagg_softc *sc)
1997 lb = (struct lagg_lb *)sc->sc_psc;
2004 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2006 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2007 struct lagg_port *lp_next;
2010 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2011 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2014 if (i >= LAGG_MAX_PORTS)
2016 if (sc->sc_ifflags & IFF_DEBUG)
2017 printf("%s: port %s at index %d\n",
2018 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2019 lb->lb_ports[i++] = lp_next;
2026 lagg_lb_port_create(struct lagg_port *lp)
2028 struct lagg_softc *sc = lp->lp_softc;
2029 return (lagg_lb_porttable(sc, NULL));
2033 lagg_lb_port_destroy(struct lagg_port *lp)
2035 struct lagg_softc *sc = lp->lp_softc;
2036 lagg_lb_porttable(sc, lp);
2040 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2042 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2043 struct lagg_port *lp = NULL;
2046 if (sc->use_flowid && (m->m_flags & M_FLOWID))
2047 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2049 p = lagg_hashmbuf(sc, m, lb->lb_key);
2051 lp = lb->lb_ports[p];
2054 * Check the port's link state. This will return the next active
2055 * port if the link is down or the port is NULL.
2057 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2063 return (lagg_enqueue(lp->lp_ifp, m));
2066 static struct mbuf *
2067 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2069 struct ifnet *ifp = sc->sc_ifp;
2071 /* Just pass in the packet to our lagg device */
2072 m->m_pkthdr.rcvif = ifp;
2081 lagg_lacp_attach(struct lagg_softc *sc)
2083 struct lagg_port *lp;
2086 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2087 lacp_port_create(lp);
2091 lagg_lacp_detach(struct lagg_softc *sc)
2093 struct lagg_port *lp;
2096 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2097 lacp_port_destroy(lp);
2107 lagg_lacp_lladdr(struct lagg_softc *sc)
2109 struct lagg_port *lp;
2111 /* purge all the lacp ports */
2112 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2113 lacp_port_destroy(lp);
2115 /* add them back in */
2116 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2117 lacp_port_create(lp);
2121 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2123 struct lagg_port *lp;
2125 lp = lacp_select_tx_port(sc, m);
2132 return (lagg_enqueue(lp->lp_ifp, m));
2135 static struct mbuf *
2136 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2138 struct ifnet *ifp = sc->sc_ifp;
2139 struct ether_header *eh;
2142 eh = mtod(m, struct ether_header *);
2143 etype = ntohs(eh->ether_type);
2145 /* Tap off LACP control messages */
2146 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2147 m = lacp_input(lp, m);
2153 * If the port is not collecting or not in the active aggregator then
2156 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2161 m->m_pkthdr.rcvif = ifp;