1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */
4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 #include "opt_inet6.h"
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/socket.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/module.h>
36 #include <sys/systm.h>
40 #include <sys/rwlock.h>
41 #include <sys/taskqueue.h>
42 #include <sys/eventhandler.h>
44 #include <net/ethernet.h>
46 #include <net/if_clone.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
55 #if defined(INET) || defined(INET6)
56 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
65 #include <netinet/ip6.h>
68 #include <net/if_vlan_var.h>
69 #include <net/if_lagg.h>
70 #include <net/ieee8023ad_lacp.h>
72 /* Special flags we should propagate to the lagg ports. */
75 int (*func)(struct ifnet *, int);
77 {IFF_PROMISC, ifpromisc},
78 {IFF_ALLMULTI, if_allmulti},
82 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
83 static struct mtx lagg_list_mtx;
84 eventhandler_tag lagg_detach_cookie = NULL;
86 static int lagg_clone_create(struct if_clone *, int, caddr_t);
87 static void lagg_clone_destroy(struct ifnet *);
88 static struct if_clone *lagg_cloner;
89 static const char laggname[] = "lagg";
91 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
92 static void lagg_capabilities(struct lagg_softc *);
93 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
94 static void lagg_port_setlladdr(void *, int);
95 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
96 static int lagg_port_destroy(struct lagg_port *, int);
97 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
98 static void lagg_linkstate(struct lagg_softc *);
99 static void lagg_port_state(struct ifnet *, int);
100 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
101 static int lagg_port_output(struct ifnet *, struct mbuf *,
102 const struct sockaddr *, struct route *);
103 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
104 #ifdef LAGG_PORT_STACKING
105 static int lagg_port_checkstacking(struct lagg_softc *);
107 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
108 static void lagg_init(void *);
109 static void lagg_stop(struct lagg_softc *);
110 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
111 static int lagg_ether_setmulti(struct lagg_softc *);
112 static int lagg_ether_cmdmulti(struct lagg_port *, int);
113 static int lagg_setflag(struct lagg_port *, int, int,
114 int (*func)(struct ifnet *, int));
115 static int lagg_setflags(struct lagg_port *, int status);
116 static int lagg_transmit(struct ifnet *, struct mbuf *);
117 static void lagg_qflush(struct ifnet *);
118 static int lagg_media_change(struct ifnet *);
119 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
120 static struct lagg_port *lagg_link_active(struct lagg_softc *,
122 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
124 /* Simple round robin */
125 static int lagg_rr_attach(struct lagg_softc *);
126 static int lagg_rr_detach(struct lagg_softc *);
127 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
128 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
131 /* Active failover */
132 static int lagg_fail_attach(struct lagg_softc *);
133 static int lagg_fail_detach(struct lagg_softc *);
134 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
135 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
139 static int lagg_lb_attach(struct lagg_softc *);
140 static int lagg_lb_detach(struct lagg_softc *);
141 static int lagg_lb_port_create(struct lagg_port *);
142 static void lagg_lb_port_destroy(struct lagg_port *);
143 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
144 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
146 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
149 static int lagg_lacp_attach(struct lagg_softc *);
150 static int lagg_lacp_detach(struct lagg_softc *);
151 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
152 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
154 static void lagg_lacp_lladdr(struct lagg_softc *);
156 static void lagg_callout(void *);
158 /* lagg protocol table */
159 static const struct {
161 int (*ti_attach)(struct lagg_softc *);
163 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
164 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
165 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
166 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
167 { LAGG_PROTO_LACP, lagg_lacp_attach },
168 { LAGG_PROTO_NONE, NULL }
171 SYSCTL_DECL(_net_link);
172 static SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
175 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
176 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
177 &lagg_failover_rx_all, 0,
178 "Accept input from any interface in a failover lagg");
179 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
180 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
181 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
183 "Default setting for using flow id for load sharing");
186 lagg_modevent(module_t mod, int type, void *data)
191 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
192 SLIST_INIT(&lagg_list);
193 lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
194 lagg_clone_destroy, 0);
195 lagg_input_p = lagg_input;
196 lagg_linkstate_p = lagg_port_state;
197 lagg_detach_cookie = EVENTHANDLER_REGISTER(
198 ifnet_departure_event, lagg_port_ifdetach, NULL,
199 EVENTHANDLER_PRI_ANY);
202 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
204 if_clone_detach(lagg_cloner);
206 lagg_linkstate_p = NULL;
207 mtx_destroy(&lagg_list_mtx);
215 static moduledata_t lagg_mod = {
221 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
222 MODULE_VERSION(if_lagg, 1);
225 * This routine is run via an vlan
229 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
231 struct lagg_softc *sc = ifp->if_softc;
232 struct lagg_port *lp;
234 if (ifp->if_softc != arg) /* Not our event */
238 if (!SLIST_EMPTY(&sc->sc_ports)) {
239 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
240 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
246 * This routine is run via an vlan
250 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
252 struct lagg_softc *sc = ifp->if_softc;
253 struct lagg_port *lp;
255 if (ifp->if_softc != arg) /* Not our event */
259 if (!SLIST_EMPTY(&sc->sc_ports)) {
260 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
261 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
267 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
269 struct lagg_softc *sc;
272 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
273 struct sysctl_oid *oid;
274 char num[14]; /* sufficient for 32 bits */
276 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
277 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
283 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
284 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
285 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
286 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
288 sysctl_ctx_init(&sc->ctx);
289 snprintf(num, sizeof(num), "%u", unit);
290 sc->use_flowid = def_use_flowid;
291 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
292 OID_AUTO, num, CTLFLAG_RD, NULL, "");
293 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
294 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
295 "Use flow id for load sharing");
296 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
297 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
298 "Total number of ports");
299 /* Hash all layers by default */
300 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
302 sc->sc_proto = LAGG_PROTO_NONE;
303 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
304 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
305 sc->sc_proto = lagg_protos[i].ti_proto;
306 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
315 SLIST_INIT(&sc->sc_ports);
316 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
317 callout_init_rw(&sc->sc_callout, &sc->sc_mtx, CALLOUT_SHAREDLOCK);
319 /* Initialise pseudo media types */
320 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
322 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
323 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
325 if_initname(ifp, laggname, unit);
327 ifp->if_transmit = lagg_transmit;
328 ifp->if_qflush = lagg_qflush;
329 ifp->if_init = lagg_init;
330 ifp->if_ioctl = lagg_ioctl;
331 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
334 * Attach as an ordinary ethernet device, children will be attached
335 * as special device IFT_IEEE8023ADLAG.
337 ether_ifattach(ifp, eaddr);
339 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
340 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
341 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
342 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
344 /* Insert into the global list of laggs */
345 mtx_lock(&lagg_list_mtx);
346 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
347 mtx_unlock(&lagg_list_mtx);
349 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
355 lagg_clone_destroy(struct ifnet *ifp)
357 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
358 struct lagg_port *lp;
363 ifp->if_flags &= ~IFF_UP;
365 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
366 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
368 /* Shutdown and remove lagg ports */
369 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
370 lagg_port_destroy(lp, 1);
371 /* Unhook the aggregation protocol */
372 if (sc->sc_detach != NULL)
373 (*sc->sc_detach)(sc);
377 sysctl_ctx_free(&sc->ctx);
378 ifmedia_removeall(&sc->sc_media);
382 callout_drain(&sc->sc_callout);
383 counter_u64_free(sc->sc_ipackets);
384 counter_u64_free(sc->sc_opackets);
385 counter_u64_free(sc->sc_ibytes);
386 counter_u64_free(sc->sc_obytes);
388 mtx_lock(&lagg_list_mtx);
389 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
390 mtx_unlock(&lagg_list_mtx);
392 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
393 LAGG_LOCK_DESTROY(sc);
398 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
400 struct ifnet *ifp = sc->sc_ifp;
402 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
405 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
406 /* Let the protocol know the MAC has changed */
407 if (sc->sc_lladdr != NULL)
408 (*sc->sc_lladdr)(sc);
409 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
413 lagg_capabilities(struct lagg_softc *sc)
415 struct lagg_port *lp;
416 int cap = ~0, ena = ~0;
419 LAGG_WLOCK_ASSERT(sc);
421 /* Get capabilities from the lagg ports */
422 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
423 cap &= lp->lp_ifp->if_capabilities;
424 ena &= lp->lp_ifp->if_capenable;
425 hwa &= lp->lp_ifp->if_hwassist;
427 cap = (cap == ~0 ? 0 : cap);
428 ena = (ena == ~0 ? 0 : ena);
429 hwa = (hwa == ~0 ? 0 : hwa);
431 if (sc->sc_ifp->if_capabilities != cap ||
432 sc->sc_ifp->if_capenable != ena ||
433 sc->sc_ifp->if_hwassist != hwa) {
434 sc->sc_ifp->if_capabilities = cap;
435 sc->sc_ifp->if_capenable = ena;
436 sc->sc_ifp->if_hwassist = hwa;
437 getmicrotime(&sc->sc_ifp->if_lastchange);
439 if (sc->sc_ifflags & IFF_DEBUG)
440 if_printf(sc->sc_ifp,
441 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
446 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
448 struct lagg_softc *sc = lp->lp_softc;
449 struct ifnet *ifp = lp->lp_ifp;
450 struct lagg_llq *llq;
453 LAGG_WLOCK_ASSERT(sc);
455 if (lp->lp_detaching ||
456 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
459 /* Check to make sure its not already queued to be changed */
460 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
461 if (llq->llq_ifp == ifp) {
468 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
469 if (llq == NULL) /* XXX what to do */
473 /* Update the lladdr even if pending, it may have changed */
475 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
478 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
480 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
484 * Set the interface MAC address from a taskqueue to avoid a LOR.
487 lagg_port_setlladdr(void *arg, int pending)
489 struct lagg_softc *sc = (struct lagg_softc *)arg;
490 struct lagg_llq *llq, *head;
494 /* Grab a local reference of the queue and remove it from the softc */
496 head = SLIST_FIRST(&sc->sc_llq_head);
497 SLIST_FIRST(&sc->sc_llq_head) = NULL;
501 * Traverse the queue and set the lladdr on each ifp. It is safe to do
502 * unlocked as we have the only reference to it.
504 for (llq = head; llq != NULL; llq = head) {
507 /* Set the link layer address */
508 CURVNET_SET(ifp->if_vnet);
509 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
512 printf("%s: setlladdr failed on %s\n", __func__,
515 head = SLIST_NEXT(llq, llq_entries);
521 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
523 struct lagg_softc *sc_ptr;
524 struct lagg_port *lp;
527 LAGG_WLOCK_ASSERT(sc);
529 /* Limit the maximal number of lagg ports */
530 if (sc->sc_count >= LAGG_MAX_PORTS)
533 /* Check if port has already been associated to a lagg */
534 if (ifp->if_lagg != NULL) {
535 /* Port is already in the current lagg? */
536 lp = (struct lagg_port *)ifp->if_lagg;
537 if (lp->lp_softc == sc)
542 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
543 if (ifp->if_type != IFT_ETHER)
544 return (EPROTONOSUPPORT);
546 /* Allow the first Ethernet member to define the MTU */
547 if (SLIST_EMPTY(&sc->sc_ports))
548 sc->sc_ifp->if_mtu = ifp->if_mtu;
549 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
550 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
555 if ((lp = malloc(sizeof(struct lagg_port),
556 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
559 /* Check if port is a stacked lagg */
560 mtx_lock(&lagg_list_mtx);
561 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
562 if (ifp == sc_ptr->sc_ifp) {
563 mtx_unlock(&lagg_list_mtx);
566 /* XXX disable stacking for the moment, its untested */
567 #ifdef LAGG_PORT_STACKING
568 lp->lp_flags |= LAGG_PORT_STACK;
569 if (lagg_port_checkstacking(sc_ptr) >=
571 mtx_unlock(&lagg_list_mtx);
578 mtx_unlock(&lagg_list_mtx);
580 /* Change the interface type */
581 lp->lp_iftype = ifp->if_type;
582 ifp->if_type = IFT_IEEE8023ADLAG;
584 lp->lp_ioctl = ifp->if_ioctl;
585 ifp->if_ioctl = lagg_port_ioctl;
586 lp->lp_output = ifp->if_output;
587 ifp->if_output = lagg_port_output;
592 /* Save port link layer address */
593 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
595 if (SLIST_EMPTY(&sc->sc_ports)) {
597 lagg_lladdr(sc, IF_LLADDR(ifp));
599 /* Update link layer address for this port */
600 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
603 /* Insert into the list of ports */
604 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
607 /* Update lagg capabilities */
608 lagg_capabilities(sc);
611 /* Add multicast addresses and interface flags to this port */
612 lagg_ether_cmdmulti(lp, 1);
613 lagg_setflags(lp, 1);
615 if (sc->sc_port_create != NULL)
616 error = (*sc->sc_port_create)(lp);
618 /* remove the port again, without calling sc_port_destroy */
619 lagg_port_destroy(lp, 0);
626 #ifdef LAGG_PORT_STACKING
628 lagg_port_checkstacking(struct lagg_softc *sc)
630 struct lagg_softc *sc_ptr;
631 struct lagg_port *lp;
634 LAGG_WLOCK_ASSERT(sc);
636 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
637 if (lp->lp_flags & LAGG_PORT_STACK) {
638 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
639 m = MAX(m, lagg_port_checkstacking(sc_ptr));
648 lagg_port_destroy(struct lagg_port *lp, int runpd)
650 struct lagg_softc *sc = lp->lp_softc;
651 struct lagg_port *lp_ptr;
652 struct lagg_llq *llq;
653 struct ifnet *ifp = lp->lp_ifp;
655 LAGG_WLOCK_ASSERT(sc);
657 if (runpd && sc->sc_port_destroy != NULL)
658 (*sc->sc_port_destroy)(lp);
661 * Remove multicast addresses and interface flags from this port and
662 * reset the MAC address, skip if the interface is being detached.
664 if (!lp->lp_detaching) {
665 lagg_ether_cmdmulti(lp, 0);
666 lagg_setflags(lp, 0);
667 lagg_port_lladdr(lp, lp->lp_lladdr);
670 /* Restore interface */
671 ifp->if_type = lp->lp_iftype;
672 ifp->if_ioctl = lp->lp_ioctl;
673 ifp->if_output = lp->lp_output;
676 /* Finally, remove the port from the lagg */
677 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
680 /* Update the primary interface */
681 if (lp == sc->sc_primary) {
682 uint8_t lladdr[ETHER_ADDR_LEN];
684 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
685 bzero(&lladdr, ETHER_ADDR_LEN);
687 bcopy(lp_ptr->lp_lladdr,
688 lladdr, ETHER_ADDR_LEN);
690 lagg_lladdr(sc, lladdr);
691 sc->sc_primary = lp_ptr;
693 /* Update link layer address for each port */
694 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
695 lagg_port_lladdr(lp_ptr, lladdr);
698 /* Remove any pending lladdr changes from the queue */
699 if (lp->lp_detaching) {
700 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
701 if (llq->llq_ifp == ifp) {
702 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
705 break; /* Only appears once */
711 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
715 /* Update lagg capabilities */
716 lagg_capabilities(sc);
723 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
725 struct lagg_reqport *rp = (struct lagg_reqport *)data;
726 struct lagg_softc *sc;
727 struct lagg_port *lp = NULL;
730 /* Should be checked by the caller */
731 if (ifp->if_type != IFT_IEEE8023ADLAG ||
732 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
737 if (rp->rp_portname[0] == '\0' ||
738 ifunit(rp->rp_portname) != ifp) {
744 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
750 lagg_port2req(lp, rp);
755 if (lp->lp_ioctl == NULL) {
759 error = (*lp->lp_ioctl)(ifp, cmd, data);
763 /* Update lagg interface capabilities */
765 lagg_capabilities(sc);
770 /* Do not allow the MTU to be changed once joined */
781 if (lp->lp_ioctl != NULL)
782 return ((*lp->lp_ioctl)(ifp, cmd, data));
788 * For direct output to child ports.
791 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
792 const struct sockaddr *dst, struct route *ro)
794 struct lagg_port *lp = ifp->if_lagg;
796 switch (dst->sa_family) {
797 case pseudo_AF_HDRCMPLT:
799 return ((*lp->lp_output)(ifp, m, dst, ro));
802 /* drop any other frames */
808 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
810 struct lagg_port *lp;
811 struct lagg_softc *sc;
813 if ((lp = ifp->if_lagg) == NULL)
815 /* If the ifnet is just being renamed, don't do anything. */
816 if (ifp->if_flags & IFF_RENAMING)
822 lp->lp_detaching = 1;
823 lagg_port_destroy(lp, 1);
828 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
830 struct lagg_softc *sc = lp->lp_softc;
832 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
833 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
834 rp->rp_prio = lp->lp_prio;
835 rp->rp_flags = lp->lp_flags;
836 if (sc->sc_portreq != NULL)
837 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
839 /* Add protocol specific flags */
840 switch (sc->sc_proto) {
841 case LAGG_PROTO_FAILOVER:
842 if (lp == sc->sc_primary)
843 rp->rp_flags |= LAGG_PORT_MASTER;
844 if (lp == lagg_link_active(sc, sc->sc_primary))
845 rp->rp_flags |= LAGG_PORT_ACTIVE;
848 case LAGG_PROTO_ROUNDROBIN:
849 case LAGG_PROTO_LOADBALANCE:
850 case LAGG_PROTO_ETHERCHANNEL:
851 if (LAGG_PORTACTIVE(lp))
852 rp->rp_flags |= LAGG_PORT_ACTIVE;
855 case LAGG_PROTO_LACP:
856 /* LACP has a different definition of active */
857 if (lacp_isactive(lp))
858 rp->rp_flags |= LAGG_PORT_ACTIVE;
859 if (lacp_iscollecting(lp))
860 rp->rp_flags |= LAGG_PORT_COLLECTING;
861 if (lacp_isdistributing(lp))
862 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
871 struct lagg_softc *sc = (struct lagg_softc *)xsc;
872 struct lagg_port *lp;
873 struct ifnet *ifp = sc->sc_ifp;
875 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
880 ifp->if_drv_flags |= IFF_DRV_RUNNING;
881 /* Update the port lladdrs */
882 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
883 lagg_port_lladdr(lp, IF_LLADDR(ifp));
885 if (sc->sc_init != NULL)
892 lagg_stop(struct lagg_softc *sc)
894 struct ifnet *ifp = sc->sc_ifp;
896 LAGG_WLOCK_ASSERT(sc);
898 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
901 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
903 if (sc->sc_stop != NULL)
908 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
910 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
911 struct lagg_reqall *ra = (struct lagg_reqall *)data;
912 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
913 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
914 struct ifreq *ifr = (struct ifreq *)data;
915 struct lagg_port *lp;
917 struct thread *td = curthread;
919 int count, buflen, len, error = 0;
921 bzero(&rpbuf, sizeof(rpbuf));
927 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
929 buflen = count * sizeof(struct lagg_reqport);
932 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
935 ra->ra_proto = sc->sc_proto;
936 if (sc->sc_req != NULL)
937 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
941 len = min(ra->ra_size, buflen);
942 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
943 if (len < sizeof(rpbuf))
946 lagg_port2req(lp, &rpbuf);
947 memcpy(buf, &rpbuf, sizeof(rpbuf));
949 buf += sizeof(rpbuf);
950 len -= sizeof(rpbuf);
953 ra->ra_ports = count;
954 ra->ra_size = count * sizeof(rpbuf);
955 error = copyout(outbuf, ra->ra_port, ra->ra_size);
956 free(outbuf, M_TEMP);
959 error = priv_check(td, PRIV_NET_LAGG);
962 if (ra->ra_proto >= LAGG_PROTO_MAX) {
963 error = EPROTONOSUPPORT;
967 if (sc->sc_proto != LAGG_PROTO_NONE) {
968 /* Reset protocol first in case detach unlocks */
969 sc->sc_proto = LAGG_PROTO_NONE;
970 error = sc->sc_detach(sc);
971 sc->sc_detach = NULL;
974 sc->sc_port_create = NULL;
975 sc->sc_port_destroy = NULL;
976 sc->sc_linkstate = NULL;
979 sc->sc_lladdr = NULL;
981 sc->sc_portreq = NULL;
982 } else if (sc->sc_input != NULL) {
983 /* Still detaching */
990 for (int i = 0; i < (sizeof(lagg_protos) /
991 sizeof(lagg_protos[0])); i++) {
992 if (lagg_protos[i].ti_proto == ra->ra_proto) {
993 if (sc->sc_ifflags & IFF_DEBUG)
994 printf("%s: using proto %u\n",
996 lagg_protos[i].ti_proto);
997 sc->sc_proto = lagg_protos[i].ti_proto;
998 if (sc->sc_proto != LAGG_PROTO_NONE)
999 error = lagg_protos[i].ti_attach(sc);
1005 error = EPROTONOSUPPORT;
1007 case SIOCGLAGGFLAGS:
1008 rf->rf_flags = sc->sc_flags;
1011 error = priv_check(td, PRIV_NET_LAGG);
1014 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1019 sc->sc_flags &= ~LAGG_F_HASHMASK;
1020 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1024 if (rp->rp_portname[0] == '\0' ||
1025 (tpif = ifunit(rp->rp_portname)) == NULL) {
1031 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1032 lp->lp_softc != sc) {
1038 lagg_port2req(lp, rp);
1042 error = priv_check(td, PRIV_NET_LAGG);
1045 if (rp->rp_portname[0] == '\0' ||
1046 (tpif = ifunit(rp->rp_portname)) == NULL) {
1051 error = lagg_port_create(sc, tpif);
1054 case SIOCSLAGGDELPORT:
1055 error = priv_check(td, PRIV_NET_LAGG);
1058 if (rp->rp_portname[0] == '\0' ||
1059 (tpif = ifunit(rp->rp_portname)) == NULL) {
1065 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1066 lp->lp_softc != sc) {
1072 error = lagg_port_destroy(lp, 1);
1076 /* Set flags on ports too */
1078 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1079 lagg_setflags(lp, 1);
1083 if (!(ifp->if_flags & IFF_UP) &&
1084 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1086 * If interface is marked down and it is running,
1087 * then stop and disable it.
1092 } else if ((ifp->if_flags & IFF_UP) &&
1093 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1095 * If interface is marked up and it is stopped, then
1098 (*ifp->if_init)(sc);
1104 error = lagg_ether_setmulti(sc);
1109 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1114 /* Do not allow the MTU or caps to be directly changed */
1119 error = ether_ioctl(ifp, cmd, data);
1126 lagg_ether_setmulti(struct lagg_softc *sc)
1128 struct lagg_port *lp;
1130 LAGG_WLOCK_ASSERT(sc);
1132 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1133 /* First, remove any existing filter entries. */
1134 lagg_ether_cmdmulti(lp, 0);
1135 /* copy all addresses from the lagg interface to the port */
1136 lagg_ether_cmdmulti(lp, 1);
1142 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1144 struct lagg_softc *sc = lp->lp_softc;
1145 struct ifnet *ifp = lp->lp_ifp;
1146 struct ifnet *scifp = sc->sc_ifp;
1148 struct ifmultiaddr *ifma, *rifma = NULL;
1149 struct sockaddr_dl sdl;
1152 LAGG_WLOCK_ASSERT(sc);
1154 bzero((char *)&sdl, sizeof(sdl));
1155 sdl.sdl_len = sizeof(sdl);
1156 sdl.sdl_family = AF_LINK;
1157 sdl.sdl_type = IFT_ETHER;
1158 sdl.sdl_alen = ETHER_ADDR_LEN;
1159 sdl.sdl_index = ifp->if_index;
1162 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1163 if (ifma->ifma_addr->sa_family != AF_LINK)
1165 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1166 LLADDR(&sdl), ETHER_ADDR_LEN);
1168 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1171 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1174 mc->mc_ifma = rifma;
1175 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1178 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1179 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1180 if_delmulti_ifma(mc->mc_ifma);
1187 /* Handle a ref counted flag that should be set on the lagg port as well */
1189 lagg_setflag(struct lagg_port *lp, int flag, int status,
1190 int (*func)(struct ifnet *, int))
1192 struct lagg_softc *sc = lp->lp_softc;
1193 struct ifnet *scifp = sc->sc_ifp;
1194 struct ifnet *ifp = lp->lp_ifp;
1197 LAGG_WLOCK_ASSERT(sc);
1199 status = status ? (scifp->if_flags & flag) : 0;
1200 /* Now "status" contains the flag value or 0 */
1203 * See if recorded ports status is different from what
1204 * we want it to be. If it is, flip it. We record ports
1205 * status in lp_ifflags so that we won't clear ports flag
1206 * we haven't set. In fact, we don't clear or set ports
1207 * flags directly, but get or release references to them.
1208 * That's why we can be sure that recorded flags still are
1209 * in accord with actual ports flags.
1211 if (status != (lp->lp_ifflags & flag)) {
1212 error = (*func)(ifp, status);
1215 lp->lp_ifflags &= ~flag;
1216 lp->lp_ifflags |= status;
1222 * Handle IFF_* flags that require certain changes on the lagg port
1223 * if "status" is true, update ports flags respective to the lagg
1224 * if "status" is false, forcedly clear the flags set on port.
1227 lagg_setflags(struct lagg_port *lp, int status)
1231 for (i = 0; lagg_pflags[i].flag; i++) {
1232 error = lagg_setflag(lp, lagg_pflags[i].flag,
1233 status, lagg_pflags[i].func);
1241 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1243 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1244 int error, len, mcast;
1246 len = m->m_pkthdr.len;
1247 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1250 /* We need a Tx algorithm and at least one port */
1251 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1258 ETHER_BPF_MTAP(ifp, m);
1260 error = (*sc->sc_start)(sc, m);
1264 counter_u64_add(sc->sc_opackets, 1);
1265 counter_u64_add(sc->sc_obytes, len);
1266 ifp->if_omcasts += mcast;
1274 * The ifp->if_qflush entry point for lagg(4) is no-op.
1277 lagg_qflush(struct ifnet *ifp __unused)
1281 static struct mbuf *
1282 lagg_input(struct ifnet *ifp, struct mbuf *m)
1284 struct lagg_port *lp = ifp->if_lagg;
1285 struct lagg_softc *sc = lp->lp_softc;
1286 struct ifnet *scifp = sc->sc_ifp;
1289 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1290 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1291 sc->sc_proto == LAGG_PROTO_NONE) {
1297 ETHER_BPF_MTAP(scifp, m);
1299 m = (*sc->sc_input)(sc, lp, m);
1302 counter_u64_add(sc->sc_ipackets, 1);
1303 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1305 if (scifp->if_flags & IFF_MONITOR) {
1316 lagg_media_change(struct ifnet *ifp)
1318 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1320 if (sc->sc_ifflags & IFF_DEBUG)
1321 printf("%s\n", __func__);
1328 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1330 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1331 struct lagg_port *lp;
1333 imr->ifm_status = IFM_AVALID;
1334 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1337 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1338 if (LAGG_PORTACTIVE(lp))
1339 imr->ifm_status |= IFM_ACTIVE;
1345 lagg_linkstate(struct lagg_softc *sc)
1347 struct lagg_port *lp;
1348 int new_link = LINK_STATE_DOWN;
1351 /* Our link is considered up if at least one of our ports is active */
1352 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1353 if (lp->lp_link_state == LINK_STATE_UP) {
1354 new_link = LINK_STATE_UP;
1358 if_link_state_change(sc->sc_ifp, new_link);
1360 /* Update if_baudrate to reflect the max possible speed */
1361 switch (sc->sc_proto) {
1362 case LAGG_PROTO_FAILOVER:
1363 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1364 sc->sc_primary->lp_ifp->if_baudrate : 0;
1366 case LAGG_PROTO_ROUNDROBIN:
1367 case LAGG_PROTO_LOADBALANCE:
1368 case LAGG_PROTO_ETHERCHANNEL:
1370 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1371 speed += lp->lp_ifp->if_baudrate;
1372 sc->sc_ifp->if_baudrate = speed;
1374 case LAGG_PROTO_LACP:
1375 /* LACP updates if_baudrate itself */
1381 lagg_port_state(struct ifnet *ifp, int state)
1383 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1384 struct lagg_softc *sc = NULL;
1393 if (sc->sc_linkstate != NULL)
1394 (*sc->sc_linkstate)(lp);
1399 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1401 struct lagg_port *lp_next, *rval = NULL;
1402 // int new_link = LINK_STATE_DOWN;
1404 LAGG_RLOCK_ASSERT(sc);
1406 * Search a port which reports an active link state.
1411 if (LAGG_PORTACTIVE(lp)) {
1415 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1416 LAGG_PORTACTIVE(lp_next)) {
1422 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1423 if (LAGG_PORTACTIVE(lp_next)) {
1432 * The IEEE 802.1D standard assumes that a lagg with
1433 * multiple ports is always full duplex. This is valid
1434 * for load sharing laggs and if at least two links
1435 * are active. Unfortunately, checking the latter would
1436 * be too expensive at this point.
1438 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1440 new_link = LINK_STATE_FULL_DUPLEX;
1442 new_link = rval->lp_link_state;
1450 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1452 if (m->m_pkthdr.len < (off + len)) {
1454 } else if (m->m_len < (off + len)) {
1455 m_copydata(m, off, len, buf);
1458 return (mtod(m, char *) + off);
1462 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1467 struct ether_header *eh;
1468 const struct ether_vlan_header *vlan;
1470 const struct ip *ip;
1471 const uint32_t *ports;
1475 const struct ip6_hdr *ip6;
1485 struct ether_vlan_header vlan;
1493 eh = mtod(m, struct ether_header *);
1494 etype = ntohs(eh->ether_type);
1495 if (sc->sc_flags & LAGG_F_HASHL2) {
1496 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1497 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1500 /* Special handling for encapsulating VLAN frames */
1501 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1502 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1503 sizeof(m->m_pkthdr.ether_vtag), p);
1504 } else if (etype == ETHERTYPE_VLAN) {
1505 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1509 if (sc->sc_flags & LAGG_F_HASHL2)
1510 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1511 etype = ntohs(vlan->evl_proto);
1512 off += sizeof(*vlan) - sizeof(*eh);
1518 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1522 if (sc->sc_flags & LAGG_F_HASHL3) {
1523 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1524 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1526 if (!(sc->sc_flags & LAGG_F_HASHL4))
1532 iphlen = ip->ip_hl << 2;
1533 if (iphlen < sizeof(*ip))
1536 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1539 p = hash32_buf(ports, sizeof(*ports), p);
1545 case ETHERTYPE_IPV6:
1546 if (!(sc->sc_flags & LAGG_F_HASHL3))
1548 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1552 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1553 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1554 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1555 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1564 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1567 return (ifp->if_transmit)(ifp, m);
1571 * Simple round robin aggregation
1575 lagg_rr_attach(struct lagg_softc *sc)
1577 sc->sc_detach = lagg_rr_detach;
1578 sc->sc_start = lagg_rr_start;
1579 sc->sc_input = lagg_rr_input;
1580 sc->sc_port_create = NULL;
1581 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1588 lagg_rr_detach(struct lagg_softc *sc)
1594 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1596 struct lagg_port *lp;
1599 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1601 lp = SLIST_FIRST(&sc->sc_ports);
1603 lp = SLIST_NEXT(lp, lp_entries);
1606 * Check the port's link state. This will return the next active
1607 * port if the link is down or the port is NULL.
1609 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1615 return (lagg_enqueue(lp->lp_ifp, m));
1618 static struct mbuf *
1619 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1621 struct ifnet *ifp = sc->sc_ifp;
1623 /* Just pass in the packet to our lagg device */
1624 m->m_pkthdr.rcvif = ifp;
1634 lagg_fail_attach(struct lagg_softc *sc)
1636 sc->sc_detach = lagg_fail_detach;
1637 sc->sc_start = lagg_fail_start;
1638 sc->sc_input = lagg_fail_input;
1639 sc->sc_port_create = NULL;
1640 sc->sc_port_destroy = NULL;
1646 lagg_fail_detach(struct lagg_softc *sc)
1652 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1654 struct lagg_port *lp;
1656 /* Use the master port if active or the next available port */
1657 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1663 return (lagg_enqueue(lp->lp_ifp, m));
1666 static struct mbuf *
1667 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1669 struct ifnet *ifp = sc->sc_ifp;
1670 struct lagg_port *tmp_tp;
1672 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1673 m->m_pkthdr.rcvif = ifp;
1677 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1678 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1680 * If tmp_tp is null, we've recieved a packet when all
1681 * our links are down. Weird, but process it anyways.
1683 if ((tmp_tp == NULL || tmp_tp == lp)) {
1684 m->m_pkthdr.rcvif = ifp;
1698 lagg_lb_attach(struct lagg_softc *sc)
1700 struct lagg_port *lp;
1703 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1704 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1707 sc->sc_detach = lagg_lb_detach;
1708 sc->sc_start = lagg_lb_start;
1709 sc->sc_input = lagg_lb_input;
1710 sc->sc_port_create = lagg_lb_port_create;
1711 sc->sc_port_destroy = lagg_lb_port_destroy;
1712 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1714 lb->lb_key = arc4random();
1715 sc->sc_psc = (caddr_t)lb;
1717 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1718 lagg_lb_port_create(lp);
1724 lagg_lb_detach(struct lagg_softc *sc)
1726 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1733 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1735 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1736 struct lagg_port *lp_next;
1739 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1740 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1743 if (i >= LAGG_MAX_PORTS)
1745 if (sc->sc_ifflags & IFF_DEBUG)
1746 printf("%s: port %s at index %d\n",
1747 sc->sc_ifname, lp_next->lp_ifname, i);
1748 lb->lb_ports[i++] = lp_next;
1755 lagg_lb_port_create(struct lagg_port *lp)
1757 struct lagg_softc *sc = lp->lp_softc;
1758 return (lagg_lb_porttable(sc, NULL));
1762 lagg_lb_port_destroy(struct lagg_port *lp)
1764 struct lagg_softc *sc = lp->lp_softc;
1765 lagg_lb_porttable(sc, lp);
1769 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1771 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1772 struct lagg_port *lp = NULL;
1775 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1776 p = m->m_pkthdr.flowid;
1778 p = lagg_hashmbuf(sc, m, lb->lb_key);
1780 lp = lb->lb_ports[p];
1783 * Check the port's link state. This will return the next active
1784 * port if the link is down or the port is NULL.
1786 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1792 return (lagg_enqueue(lp->lp_ifp, m));
1795 static struct mbuf *
1796 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1798 struct ifnet *ifp = sc->sc_ifp;
1800 /* Just pass in the packet to our lagg device */
1801 m->m_pkthdr.rcvif = ifp;
1811 lagg_lacp_attach(struct lagg_softc *sc)
1813 struct lagg_port *lp;
1816 sc->sc_detach = lagg_lacp_detach;
1817 sc->sc_port_create = lacp_port_create;
1818 sc->sc_port_destroy = lacp_port_destroy;
1819 sc->sc_linkstate = lacp_linkstate;
1820 sc->sc_start = lagg_lacp_start;
1821 sc->sc_input = lagg_lacp_input;
1822 sc->sc_init = lacp_init;
1823 sc->sc_stop = lacp_stop;
1824 sc->sc_lladdr = lagg_lacp_lladdr;
1825 sc->sc_req = lacp_req;
1826 sc->sc_portreq = lacp_portreq;
1828 error = lacp_attach(sc);
1832 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1833 lacp_port_create(lp);
1839 lagg_lacp_detach(struct lagg_softc *sc)
1841 struct lagg_port *lp;
1844 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1845 lacp_port_destroy(lp);
1847 /* unlocking is safe here */
1849 error = lacp_detach(sc);
1856 lagg_lacp_lladdr(struct lagg_softc *sc)
1858 struct lagg_port *lp;
1860 /* purge all the lacp ports */
1861 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1862 lacp_port_destroy(lp);
1864 /* add them back in */
1865 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1866 lacp_port_create(lp);
1870 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1872 struct lagg_port *lp;
1874 lp = lacp_select_tx_port(sc, m);
1881 return (lagg_enqueue(lp->lp_ifp, m));
1884 static struct mbuf *
1885 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1887 struct ifnet *ifp = sc->sc_ifp;
1888 struct ether_header *eh;
1891 eh = mtod(m, struct ether_header *);
1892 etype = ntohs(eh->ether_type);
1894 /* Tap off LACP control messages */
1895 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1896 m = lacp_input(lp, m);
1902 * If the port is not collecting or not in the active aggregator then
1905 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1910 m->m_pkthdr.rcvif = ifp;
1915 lagg_callout(void *arg)
1917 struct lagg_softc *sc = (struct lagg_softc *)arg;
1918 struct ifnet *ifp = sc->sc_ifp;
1920 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
1921 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
1922 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
1923 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
1925 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);