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 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 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
510 printf("%s: setlladdr failed on %s\n", __func__,
513 head = SLIST_NEXT(llq, llq_entries);
519 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
521 struct lagg_softc *sc_ptr;
522 struct lagg_port *lp;
525 LAGG_WLOCK_ASSERT(sc);
527 /* Limit the maximal number of lagg ports */
528 if (sc->sc_count >= LAGG_MAX_PORTS)
531 /* Check if port has already been associated to a lagg */
532 if (ifp->if_lagg != NULL) {
533 /* Port is already in the current lagg? */
534 lp = (struct lagg_port *)ifp->if_lagg;
535 if (lp->lp_softc == sc)
540 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
541 if (ifp->if_type != IFT_ETHER)
542 return (EPROTONOSUPPORT);
544 /* Allow the first Ethernet member to define the MTU */
545 if (SLIST_EMPTY(&sc->sc_ports))
546 sc->sc_ifp->if_mtu = ifp->if_mtu;
547 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
548 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
553 if ((lp = malloc(sizeof(struct lagg_port),
554 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
557 /* Check if port is a stacked lagg */
558 mtx_lock(&lagg_list_mtx);
559 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
560 if (ifp == sc_ptr->sc_ifp) {
561 mtx_unlock(&lagg_list_mtx);
564 /* XXX disable stacking for the moment, its untested */
565 #ifdef LAGG_PORT_STACKING
566 lp->lp_flags |= LAGG_PORT_STACK;
567 if (lagg_port_checkstacking(sc_ptr) >=
569 mtx_unlock(&lagg_list_mtx);
576 mtx_unlock(&lagg_list_mtx);
578 /* Change the interface type */
579 lp->lp_iftype = ifp->if_type;
580 ifp->if_type = IFT_IEEE8023ADLAG;
582 lp->lp_ioctl = ifp->if_ioctl;
583 ifp->if_ioctl = lagg_port_ioctl;
584 lp->lp_output = ifp->if_output;
585 ifp->if_output = lagg_port_output;
590 /* Save port link layer address */
591 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
593 if (SLIST_EMPTY(&sc->sc_ports)) {
595 lagg_lladdr(sc, IF_LLADDR(ifp));
597 /* Update link layer address for this port */
598 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
601 /* Insert into the list of ports */
602 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
605 /* Update lagg capabilities */
606 lagg_capabilities(sc);
609 /* Add multicast addresses and interface flags to this port */
610 lagg_ether_cmdmulti(lp, 1);
611 lagg_setflags(lp, 1);
613 if (sc->sc_port_create != NULL)
614 error = (*sc->sc_port_create)(lp);
616 /* remove the port again, without calling sc_port_destroy */
617 lagg_port_destroy(lp, 0);
624 #ifdef LAGG_PORT_STACKING
626 lagg_port_checkstacking(struct lagg_softc *sc)
628 struct lagg_softc *sc_ptr;
629 struct lagg_port *lp;
632 LAGG_WLOCK_ASSERT(sc);
634 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
635 if (lp->lp_flags & LAGG_PORT_STACK) {
636 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
637 m = MAX(m, lagg_port_checkstacking(sc_ptr));
646 lagg_port_destroy(struct lagg_port *lp, int runpd)
648 struct lagg_softc *sc = lp->lp_softc;
649 struct lagg_port *lp_ptr;
650 struct lagg_llq *llq;
651 struct ifnet *ifp = lp->lp_ifp;
653 LAGG_WLOCK_ASSERT(sc);
655 if (runpd && sc->sc_port_destroy != NULL)
656 (*sc->sc_port_destroy)(lp);
659 * Remove multicast addresses and interface flags from this port and
660 * reset the MAC address, skip if the interface is being detached.
662 if (!lp->lp_detaching) {
663 lagg_ether_cmdmulti(lp, 0);
664 lagg_setflags(lp, 0);
665 lagg_port_lladdr(lp, lp->lp_lladdr);
668 /* Restore interface */
669 ifp->if_type = lp->lp_iftype;
670 ifp->if_ioctl = lp->lp_ioctl;
671 ifp->if_output = lp->lp_output;
674 /* Finally, remove the port from the lagg */
675 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
678 /* Update the primary interface */
679 if (lp == sc->sc_primary) {
680 uint8_t lladdr[ETHER_ADDR_LEN];
682 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
683 bzero(&lladdr, ETHER_ADDR_LEN);
685 bcopy(lp_ptr->lp_lladdr,
686 lladdr, ETHER_ADDR_LEN);
688 lagg_lladdr(sc, lladdr);
689 sc->sc_primary = lp_ptr;
691 /* Update link layer address for each port */
692 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
693 lagg_port_lladdr(lp_ptr, lladdr);
696 /* Remove any pending lladdr changes from the queue */
697 if (lp->lp_detaching) {
698 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
699 if (llq->llq_ifp == ifp) {
700 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
703 break; /* Only appears once */
709 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
713 /* Update lagg capabilities */
714 lagg_capabilities(sc);
721 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
723 struct lagg_reqport *rp = (struct lagg_reqport *)data;
724 struct lagg_softc *sc;
725 struct lagg_port *lp = NULL;
728 /* Should be checked by the caller */
729 if (ifp->if_type != IFT_IEEE8023ADLAG ||
730 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
735 if (rp->rp_portname[0] == '\0' ||
736 ifunit(rp->rp_portname) != ifp) {
742 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
748 lagg_port2req(lp, rp);
753 if (lp->lp_ioctl == NULL) {
757 error = (*lp->lp_ioctl)(ifp, cmd, data);
761 /* Update lagg interface capabilities */
763 lagg_capabilities(sc);
768 /* Do not allow the MTU to be changed once joined */
779 if (lp->lp_ioctl != NULL)
780 return ((*lp->lp_ioctl)(ifp, cmd, data));
786 * For direct output to child ports.
789 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
790 struct sockaddr *dst, struct route *ro)
792 struct lagg_port *lp = ifp->if_lagg;
794 switch (dst->sa_family) {
795 case pseudo_AF_HDRCMPLT:
797 return ((*lp->lp_output)(ifp, m, dst, ro));
800 /* drop any other frames */
806 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
808 struct lagg_port *lp;
809 struct lagg_softc *sc;
811 if ((lp = ifp->if_lagg) == NULL)
813 /* If the ifnet is just being renamed, don't do anything. */
814 if (ifp->if_flags & IFF_RENAMING)
820 lp->lp_detaching = 1;
821 lagg_port_destroy(lp, 1);
826 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
828 struct lagg_softc *sc = lp->lp_softc;
830 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
831 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
832 rp->rp_prio = lp->lp_prio;
833 rp->rp_flags = lp->lp_flags;
834 if (sc->sc_portreq != NULL)
835 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
837 /* Add protocol specific flags */
838 switch (sc->sc_proto) {
839 case LAGG_PROTO_FAILOVER:
840 if (lp == sc->sc_primary)
841 rp->rp_flags |= LAGG_PORT_MASTER;
842 if (lp == lagg_link_active(sc, sc->sc_primary))
843 rp->rp_flags |= LAGG_PORT_ACTIVE;
846 case LAGG_PROTO_ROUNDROBIN:
847 case LAGG_PROTO_LOADBALANCE:
848 case LAGG_PROTO_ETHERCHANNEL:
849 if (LAGG_PORTACTIVE(lp))
850 rp->rp_flags |= LAGG_PORT_ACTIVE;
853 case LAGG_PROTO_LACP:
854 /* LACP has a different definition of active */
855 if (lacp_isactive(lp))
856 rp->rp_flags |= LAGG_PORT_ACTIVE;
857 if (lacp_iscollecting(lp))
858 rp->rp_flags |= LAGG_PORT_COLLECTING;
859 if (lacp_isdistributing(lp))
860 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
869 struct lagg_softc *sc = (struct lagg_softc *)xsc;
870 struct lagg_port *lp;
871 struct ifnet *ifp = sc->sc_ifp;
873 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
878 ifp->if_drv_flags |= IFF_DRV_RUNNING;
879 /* Update the port lladdrs */
880 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
881 lagg_port_lladdr(lp, IF_LLADDR(ifp));
883 if (sc->sc_init != NULL)
890 lagg_stop(struct lagg_softc *sc)
892 struct ifnet *ifp = sc->sc_ifp;
894 LAGG_WLOCK_ASSERT(sc);
896 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
899 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
901 if (sc->sc_stop != NULL)
906 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
908 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
909 struct lagg_reqall *ra = (struct lagg_reqall *)data;
910 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
911 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
912 struct ifreq *ifr = (struct ifreq *)data;
913 struct lagg_port *lp;
915 struct thread *td = curthread;
917 int count, buflen, len, error = 0;
919 bzero(&rpbuf, sizeof(rpbuf));
925 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
927 buflen = count * sizeof(struct lagg_reqport);
930 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
933 ra->ra_proto = sc->sc_proto;
934 if (sc->sc_req != NULL)
935 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
939 len = min(ra->ra_size, buflen);
940 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
941 if (len < sizeof(rpbuf))
944 lagg_port2req(lp, &rpbuf);
945 memcpy(buf, &rpbuf, sizeof(rpbuf));
947 buf += sizeof(rpbuf);
948 len -= sizeof(rpbuf);
951 ra->ra_ports = count;
952 ra->ra_size = count * sizeof(rpbuf);
953 error = copyout(outbuf, ra->ra_port, ra->ra_size);
954 free(outbuf, M_TEMP);
957 error = priv_check(td, PRIV_NET_LAGG);
960 if (ra->ra_proto >= LAGG_PROTO_MAX) {
961 error = EPROTONOSUPPORT;
965 if (sc->sc_proto != LAGG_PROTO_NONE) {
966 /* Reset protocol first in case detach unlocks */
967 sc->sc_proto = LAGG_PROTO_NONE;
968 error = sc->sc_detach(sc);
969 sc->sc_detach = NULL;
972 sc->sc_port_create = NULL;
973 sc->sc_port_destroy = NULL;
974 sc->sc_linkstate = NULL;
977 sc->sc_lladdr = NULL;
979 sc->sc_portreq = NULL;
980 } else if (sc->sc_input != NULL) {
981 /* Still detaching */
988 for (int i = 0; i < (sizeof(lagg_protos) /
989 sizeof(lagg_protos[0])); i++) {
990 if (lagg_protos[i].ti_proto == ra->ra_proto) {
991 if (sc->sc_ifflags & IFF_DEBUG)
992 printf("%s: using proto %u\n",
994 lagg_protos[i].ti_proto);
995 sc->sc_proto = lagg_protos[i].ti_proto;
996 if (sc->sc_proto != LAGG_PROTO_NONE)
997 error = lagg_protos[i].ti_attach(sc);
1003 error = EPROTONOSUPPORT;
1005 case SIOCGLAGGFLAGS:
1006 rf->rf_flags = sc->sc_flags;
1009 error = priv_check(td, PRIV_NET_LAGG);
1012 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1017 sc->sc_flags &= ~LAGG_F_HASHMASK;
1018 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1022 if (rp->rp_portname[0] == '\0' ||
1023 (tpif = ifunit(rp->rp_portname)) == NULL) {
1029 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1030 lp->lp_softc != sc) {
1036 lagg_port2req(lp, rp);
1040 error = priv_check(td, PRIV_NET_LAGG);
1043 if (rp->rp_portname[0] == '\0' ||
1044 (tpif = ifunit(rp->rp_portname)) == NULL) {
1049 error = lagg_port_create(sc, tpif);
1052 case SIOCSLAGGDELPORT:
1053 error = priv_check(td, PRIV_NET_LAGG);
1056 if (rp->rp_portname[0] == '\0' ||
1057 (tpif = ifunit(rp->rp_portname)) == NULL) {
1063 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1064 lp->lp_softc != sc) {
1070 error = lagg_port_destroy(lp, 1);
1074 /* Set flags on ports too */
1076 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1077 lagg_setflags(lp, 1);
1081 if (!(ifp->if_flags & IFF_UP) &&
1082 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1084 * If interface is marked down and it is running,
1085 * then stop and disable it.
1090 } else if ((ifp->if_flags & IFF_UP) &&
1091 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1093 * If interface is marked up and it is stopped, then
1096 (*ifp->if_init)(sc);
1102 error = lagg_ether_setmulti(sc);
1107 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1112 /* Do not allow the MTU or caps to be directly changed */
1117 error = ether_ioctl(ifp, cmd, data);
1124 lagg_ether_setmulti(struct lagg_softc *sc)
1126 struct lagg_port *lp;
1128 LAGG_WLOCK_ASSERT(sc);
1130 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1131 /* First, remove any existing filter entries. */
1132 lagg_ether_cmdmulti(lp, 0);
1133 /* copy all addresses from the lagg interface to the port */
1134 lagg_ether_cmdmulti(lp, 1);
1140 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1142 struct lagg_softc *sc = lp->lp_softc;
1143 struct ifnet *ifp = lp->lp_ifp;
1144 struct ifnet *scifp = sc->sc_ifp;
1146 struct ifmultiaddr *ifma, *rifma = NULL;
1147 struct sockaddr_dl sdl;
1150 LAGG_WLOCK_ASSERT(sc);
1152 bzero((char *)&sdl, sizeof(sdl));
1153 sdl.sdl_len = sizeof(sdl);
1154 sdl.sdl_family = AF_LINK;
1155 sdl.sdl_type = IFT_ETHER;
1156 sdl.sdl_alen = ETHER_ADDR_LEN;
1157 sdl.sdl_index = ifp->if_index;
1160 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1161 if (ifma->ifma_addr->sa_family != AF_LINK)
1163 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1164 LLADDR(&sdl), ETHER_ADDR_LEN);
1166 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1169 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1172 mc->mc_ifma = rifma;
1173 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1176 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1177 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1178 if_delmulti_ifma(mc->mc_ifma);
1185 /* Handle a ref counted flag that should be set on the lagg port as well */
1187 lagg_setflag(struct lagg_port *lp, int flag, int status,
1188 int (*func)(struct ifnet *, int))
1190 struct lagg_softc *sc = lp->lp_softc;
1191 struct ifnet *scifp = sc->sc_ifp;
1192 struct ifnet *ifp = lp->lp_ifp;
1195 LAGG_WLOCK_ASSERT(sc);
1197 status = status ? (scifp->if_flags & flag) : 0;
1198 /* Now "status" contains the flag value or 0 */
1201 * See if recorded ports status is different from what
1202 * we want it to be. If it is, flip it. We record ports
1203 * status in lp_ifflags so that we won't clear ports flag
1204 * we haven't set. In fact, we don't clear or set ports
1205 * flags directly, but get or release references to them.
1206 * That's why we can be sure that recorded flags still are
1207 * in accord with actual ports flags.
1209 if (status != (lp->lp_ifflags & flag)) {
1210 error = (*func)(ifp, status);
1213 lp->lp_ifflags &= ~flag;
1214 lp->lp_ifflags |= status;
1220 * Handle IFF_* flags that require certain changes on the lagg port
1221 * if "status" is true, update ports flags respective to the lagg
1222 * if "status" is false, forcedly clear the flags set on port.
1225 lagg_setflags(struct lagg_port *lp, int status)
1229 for (i = 0; lagg_pflags[i].flag; i++) {
1230 error = lagg_setflag(lp, lagg_pflags[i].flag,
1231 status, lagg_pflags[i].func);
1239 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1241 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1242 int error, len, mcast;
1244 len = m->m_pkthdr.len;
1245 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1248 /* We need a Tx algorithm and at least one port */
1249 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1256 ETHER_BPF_MTAP(ifp, m);
1258 error = (*sc->sc_start)(sc, m);
1262 counter_u64_add(sc->sc_opackets, 1);
1263 counter_u64_add(sc->sc_obytes, len);
1264 ifp->if_omcasts += mcast;
1272 * The ifp->if_qflush entry point for lagg(4) is no-op.
1275 lagg_qflush(struct ifnet *ifp __unused)
1279 static struct mbuf *
1280 lagg_input(struct ifnet *ifp, struct mbuf *m)
1282 struct lagg_port *lp = ifp->if_lagg;
1283 struct lagg_softc *sc = lp->lp_softc;
1284 struct ifnet *scifp = sc->sc_ifp;
1287 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1288 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1289 sc->sc_proto == LAGG_PROTO_NONE) {
1295 ETHER_BPF_MTAP(scifp, m);
1297 m = (*sc->sc_input)(sc, lp, m);
1300 counter_u64_add(sc->sc_ipackets, 1);
1301 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1303 if (scifp->if_flags & IFF_MONITOR) {
1314 lagg_media_change(struct ifnet *ifp)
1316 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1318 if (sc->sc_ifflags & IFF_DEBUG)
1319 printf("%s\n", __func__);
1326 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1328 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1329 struct lagg_port *lp;
1331 imr->ifm_status = IFM_AVALID;
1332 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1335 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1336 if (LAGG_PORTACTIVE(lp))
1337 imr->ifm_status |= IFM_ACTIVE;
1343 lagg_linkstate(struct lagg_softc *sc)
1345 struct lagg_port *lp;
1346 int new_link = LINK_STATE_DOWN;
1349 /* Our link is considered up if at least one of our ports is active */
1350 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1351 if (lp->lp_link_state == LINK_STATE_UP) {
1352 new_link = LINK_STATE_UP;
1356 if_link_state_change(sc->sc_ifp, new_link);
1358 /* Update if_baudrate to reflect the max possible speed */
1359 switch (sc->sc_proto) {
1360 case LAGG_PROTO_FAILOVER:
1361 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1362 sc->sc_primary->lp_ifp->if_baudrate : 0;
1364 case LAGG_PROTO_ROUNDROBIN:
1365 case LAGG_PROTO_LOADBALANCE:
1366 case LAGG_PROTO_ETHERCHANNEL:
1368 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1369 speed += lp->lp_ifp->if_baudrate;
1370 sc->sc_ifp->if_baudrate = speed;
1372 case LAGG_PROTO_LACP:
1373 /* LACP updates if_baudrate itself */
1379 lagg_port_state(struct ifnet *ifp, int state)
1381 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1382 struct lagg_softc *sc = NULL;
1391 if (sc->sc_linkstate != NULL)
1392 (*sc->sc_linkstate)(lp);
1397 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1399 struct lagg_port *lp_next, *rval = NULL;
1400 // int new_link = LINK_STATE_DOWN;
1402 LAGG_RLOCK_ASSERT(sc);
1404 * Search a port which reports an active link state.
1409 if (LAGG_PORTACTIVE(lp)) {
1413 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1414 LAGG_PORTACTIVE(lp_next)) {
1420 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1421 if (LAGG_PORTACTIVE(lp_next)) {
1430 * The IEEE 802.1D standard assumes that a lagg with
1431 * multiple ports is always full duplex. This is valid
1432 * for load sharing laggs and if at least two links
1433 * are active. Unfortunately, checking the latter would
1434 * be too expensive at this point.
1436 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1438 new_link = LINK_STATE_FULL_DUPLEX;
1440 new_link = rval->lp_link_state;
1448 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1450 if (m->m_pkthdr.len < (off + len)) {
1452 } else if (m->m_len < (off + len)) {
1453 m_copydata(m, off, len, buf);
1456 return (mtod(m, char *) + off);
1460 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1465 struct ether_header *eh;
1466 const struct ether_vlan_header *vlan;
1468 const struct ip *ip;
1469 const uint32_t *ports;
1473 const struct ip6_hdr *ip6;
1483 struct ether_vlan_header vlan;
1491 eh = mtod(m, struct ether_header *);
1492 etype = ntohs(eh->ether_type);
1493 if (sc->sc_flags & LAGG_F_HASHL2) {
1494 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1495 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1498 /* Special handling for encapsulating VLAN frames */
1499 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1500 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1501 sizeof(m->m_pkthdr.ether_vtag), p);
1502 } else if (etype == ETHERTYPE_VLAN) {
1503 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1507 if (sc->sc_flags & LAGG_F_HASHL2)
1508 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1509 etype = ntohs(vlan->evl_proto);
1510 off += sizeof(*vlan) - sizeof(*eh);
1516 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1520 if (sc->sc_flags & LAGG_F_HASHL3) {
1521 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1522 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1524 if (!(sc->sc_flags & LAGG_F_HASHL4))
1530 iphlen = ip->ip_hl << 2;
1531 if (iphlen < sizeof(*ip))
1534 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1537 p = hash32_buf(ports, sizeof(*ports), p);
1543 case ETHERTYPE_IPV6:
1544 if (!(sc->sc_flags & LAGG_F_HASHL3))
1546 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1550 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1551 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1552 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1553 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1562 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1565 return (ifp->if_transmit)(ifp, m);
1569 * Simple round robin aggregation
1573 lagg_rr_attach(struct lagg_softc *sc)
1575 sc->sc_detach = lagg_rr_detach;
1576 sc->sc_start = lagg_rr_start;
1577 sc->sc_input = lagg_rr_input;
1578 sc->sc_port_create = NULL;
1579 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1586 lagg_rr_detach(struct lagg_softc *sc)
1592 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1594 struct lagg_port *lp;
1597 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1599 lp = SLIST_FIRST(&sc->sc_ports);
1601 lp = SLIST_NEXT(lp, lp_entries);
1604 * Check the port's link state. This will return the next active
1605 * port if the link is down or the port is NULL.
1607 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1613 return (lagg_enqueue(lp->lp_ifp, m));
1616 static struct mbuf *
1617 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1619 struct ifnet *ifp = sc->sc_ifp;
1621 /* Just pass in the packet to our lagg device */
1622 m->m_pkthdr.rcvif = ifp;
1632 lagg_fail_attach(struct lagg_softc *sc)
1634 sc->sc_detach = lagg_fail_detach;
1635 sc->sc_start = lagg_fail_start;
1636 sc->sc_input = lagg_fail_input;
1637 sc->sc_port_create = NULL;
1638 sc->sc_port_destroy = NULL;
1644 lagg_fail_detach(struct lagg_softc *sc)
1650 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1652 struct lagg_port *lp;
1654 /* Use the master port if active or the next available port */
1655 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1661 return (lagg_enqueue(lp->lp_ifp, m));
1664 static struct mbuf *
1665 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1667 struct ifnet *ifp = sc->sc_ifp;
1668 struct lagg_port *tmp_tp;
1670 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1671 m->m_pkthdr.rcvif = ifp;
1675 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1676 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1678 * If tmp_tp is null, we've recieved a packet when all
1679 * our links are down. Weird, but process it anyways.
1681 if ((tmp_tp == NULL || tmp_tp == lp)) {
1682 m->m_pkthdr.rcvif = ifp;
1696 lagg_lb_attach(struct lagg_softc *sc)
1698 struct lagg_port *lp;
1701 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1702 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1705 sc->sc_detach = lagg_lb_detach;
1706 sc->sc_start = lagg_lb_start;
1707 sc->sc_input = lagg_lb_input;
1708 sc->sc_port_create = lagg_lb_port_create;
1709 sc->sc_port_destroy = lagg_lb_port_destroy;
1710 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1712 lb->lb_key = arc4random();
1713 sc->sc_psc = (caddr_t)lb;
1715 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1716 lagg_lb_port_create(lp);
1722 lagg_lb_detach(struct lagg_softc *sc)
1724 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1731 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1733 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1734 struct lagg_port *lp_next;
1737 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1738 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1741 if (i >= LAGG_MAX_PORTS)
1743 if (sc->sc_ifflags & IFF_DEBUG)
1744 printf("%s: port %s at index %d\n",
1745 sc->sc_ifname, lp_next->lp_ifname, i);
1746 lb->lb_ports[i++] = lp_next;
1753 lagg_lb_port_create(struct lagg_port *lp)
1755 struct lagg_softc *sc = lp->lp_softc;
1756 return (lagg_lb_porttable(sc, NULL));
1760 lagg_lb_port_destroy(struct lagg_port *lp)
1762 struct lagg_softc *sc = lp->lp_softc;
1763 lagg_lb_porttable(sc, lp);
1767 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1769 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1770 struct lagg_port *lp = NULL;
1773 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1774 p = m->m_pkthdr.flowid;
1776 p = lagg_hashmbuf(sc, m, lb->lb_key);
1778 lp = lb->lb_ports[p];
1781 * Check the port's link state. This will return the next active
1782 * port if the link is down or the port is NULL.
1784 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1790 return (lagg_enqueue(lp->lp_ifp, m));
1793 static struct mbuf *
1794 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1796 struct ifnet *ifp = sc->sc_ifp;
1798 /* Just pass in the packet to our lagg device */
1799 m->m_pkthdr.rcvif = ifp;
1809 lagg_lacp_attach(struct lagg_softc *sc)
1811 struct lagg_port *lp;
1814 sc->sc_detach = lagg_lacp_detach;
1815 sc->sc_port_create = lacp_port_create;
1816 sc->sc_port_destroy = lacp_port_destroy;
1817 sc->sc_linkstate = lacp_linkstate;
1818 sc->sc_start = lagg_lacp_start;
1819 sc->sc_input = lagg_lacp_input;
1820 sc->sc_init = lacp_init;
1821 sc->sc_stop = lacp_stop;
1822 sc->sc_lladdr = lagg_lacp_lladdr;
1823 sc->sc_req = lacp_req;
1824 sc->sc_portreq = lacp_portreq;
1826 error = lacp_attach(sc);
1830 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1831 lacp_port_create(lp);
1837 lagg_lacp_detach(struct lagg_softc *sc)
1839 struct lagg_port *lp;
1842 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1843 lacp_port_destroy(lp);
1845 /* unlocking is safe here */
1847 error = lacp_detach(sc);
1854 lagg_lacp_lladdr(struct lagg_softc *sc)
1856 struct lagg_port *lp;
1858 /* purge all the lacp ports */
1859 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1860 lacp_port_destroy(lp);
1862 /* add them back in */
1863 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1864 lacp_port_create(lp);
1868 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1870 struct lagg_port *lp;
1872 lp = lacp_select_tx_port(sc, m);
1879 return (lagg_enqueue(lp->lp_ifp, m));
1882 static struct mbuf *
1883 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1885 struct ifnet *ifp = sc->sc_ifp;
1886 struct ether_header *eh;
1889 eh = mtod(m, struct ether_header *);
1890 etype = ntohs(eh->ether_type);
1892 /* Tap off LACP control messages */
1893 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1894 m = lacp_input(lp, m);
1900 * If the port is not collecting or not in the active aggregator then
1903 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1908 m->m_pkthdr.rcvif = ifp;
1913 lagg_callout(void *arg)
1915 struct lagg_softc *sc = (struct lagg_softc *)arg;
1916 struct ifnet *ifp = sc->sc_ifp;
1918 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
1919 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
1920 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
1921 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
1923 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);