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 void lagg_lladdr(struct lagg_softc *, uint8_t *);
89 static void lagg_capabilities(struct lagg_softc *);
90 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
91 static void lagg_port_setlladdr(void *, int);
92 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
93 static int lagg_port_destroy(struct lagg_port *, int);
94 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
95 static void lagg_linkstate(struct lagg_softc *);
96 static void lagg_port_state(struct ifnet *, int);
97 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
98 static int lagg_port_output(struct ifnet *, struct mbuf *,
99 struct sockaddr *, struct route *);
100 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
101 #ifdef LAGG_PORT_STACKING
102 static int lagg_port_checkstacking(struct lagg_softc *);
104 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
105 static void lagg_init(void *);
106 static void lagg_stop(struct lagg_softc *);
107 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
108 static int lagg_ether_setmulti(struct lagg_softc *);
109 static int lagg_ether_cmdmulti(struct lagg_port *, int);
110 static int lagg_setflag(struct lagg_port *, int, int,
111 int (*func)(struct ifnet *, int));
112 static int lagg_setflags(struct lagg_port *, int status);
113 static int lagg_transmit(struct ifnet *, struct mbuf *);
114 static void lagg_qflush(struct ifnet *);
115 static int lagg_media_change(struct ifnet *);
116 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
117 static struct lagg_port *lagg_link_active(struct lagg_softc *,
119 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
121 IFC_SIMPLE_DECLARE(lagg, 0);
123 /* Simple round robin */
124 static int lagg_rr_attach(struct lagg_softc *);
125 static int lagg_rr_detach(struct lagg_softc *);
126 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
127 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
130 /* Active failover */
131 static int lagg_fail_attach(struct lagg_softc *);
132 static int lagg_fail_detach(struct lagg_softc *);
133 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
134 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
138 static int lagg_lb_attach(struct lagg_softc *);
139 static int lagg_lb_detach(struct lagg_softc *);
140 static int lagg_lb_port_create(struct lagg_port *);
141 static void lagg_lb_port_destroy(struct lagg_port *);
142 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
143 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
145 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
148 static int lagg_lacp_attach(struct lagg_softc *);
149 static int lagg_lacp_detach(struct lagg_softc *);
150 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
151 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
153 static void lagg_lacp_lladdr(struct lagg_softc *);
155 /* lagg protocol table */
156 static const struct {
158 int (*ti_attach)(struct lagg_softc *);
160 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
161 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
162 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
163 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
164 { LAGG_PROTO_LACP, lagg_lacp_attach },
165 { LAGG_PROTO_NONE, NULL }
168 SYSCTL_DECL(_net_link);
169 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, "Link Aggregation");
171 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
172 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
173 &lagg_failover_rx_all, 0,
174 "Accept input from any interface in a failover lagg");
175 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
176 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
177 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
179 "Default setting for using flow id for load sharing");
182 lagg_modevent(module_t mod, int type, void *data)
187 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
188 SLIST_INIT(&lagg_list);
189 if_clone_attach(&lagg_cloner);
190 lagg_input_p = lagg_input;
191 lagg_linkstate_p = lagg_port_state;
192 lagg_detach_cookie = EVENTHANDLER_REGISTER(
193 ifnet_departure_event, lagg_port_ifdetach, NULL,
194 EVENTHANDLER_PRI_ANY);
197 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
199 if_clone_detach(&lagg_cloner);
201 lagg_linkstate_p = NULL;
202 mtx_destroy(&lagg_list_mtx);
210 static moduledata_t lagg_mod = {
216 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
217 MODULE_VERSION(if_lagg, 1);
219 #if __FreeBSD_version >= 800000
221 * This routine is run via an vlan
225 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
227 struct lagg_softc *sc = ifp->if_softc;
228 struct lagg_port *lp;
230 if (ifp->if_softc != arg) /* Not our event */
234 if (!SLIST_EMPTY(&sc->sc_ports)) {
235 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
236 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
242 * This routine is run via an vlan
246 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
248 struct lagg_softc *sc = ifp->if_softc;
249 struct lagg_port *lp;
251 if (ifp->if_softc != arg) /* Not our event */
255 if (!SLIST_EMPTY(&sc->sc_ports)) {
256 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
257 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
264 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
266 struct lagg_softc *sc;
269 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
270 struct sysctl_oid *oid;
271 char num[14]; /* sufficient for 32 bits */
273 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
274 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
280 sysctl_ctx_init(&sc->ctx);
281 snprintf(num, sizeof(num), "%u", unit);
282 sc->use_flowid = def_use_flowid;
283 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
284 OID_AUTO, num, CTLFLAG_RD, NULL, "");
285 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
286 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
287 "Use flow id for load sharing");
288 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
289 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
290 "Total number of ports");
291 /* Hash all layers by default */
292 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
294 sc->sc_proto = LAGG_PROTO_NONE;
295 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
296 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
297 sc->sc_proto = lagg_protos[i].ti_proto;
298 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
299 if_free_type(ifp, IFT_ETHER);
307 SLIST_INIT(&sc->sc_ports);
308 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
310 /* Initialise pseudo media types */
311 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
313 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
314 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
316 if_initname(ifp, ifc->ifc_name, unit);
317 ifp->if_type = IFT_ETHER;
319 ifp->if_transmit = lagg_transmit;
320 ifp->if_qflush = lagg_qflush;
321 ifp->if_init = lagg_init;
322 ifp->if_ioctl = lagg_ioctl;
323 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
326 * Attach as an ordinary ethernet device, childs will be attached
327 * as special device IFT_IEEE8023ADLAG.
329 ether_ifattach(ifp, eaddr);
331 #if __FreeBSD_version >= 800000
332 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
333 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
334 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
335 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
338 /* Insert into the global list of laggs */
339 mtx_lock(&lagg_list_mtx);
340 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
341 mtx_unlock(&lagg_list_mtx);
347 lagg_clone_destroy(struct ifnet *ifp)
349 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
350 struct lagg_port *lp;
355 ifp->if_flags &= ~IFF_UP;
357 #if __FreeBSD_version >= 800000
358 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
359 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
362 /* Shutdown and remove lagg ports */
363 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
364 lagg_port_destroy(lp, 1);
365 /* Unhook the aggregation protocol */
366 if (sc->sc_detach != NULL)
367 (*sc->sc_detach)(sc);
371 sysctl_ctx_free(&sc->ctx);
372 ifmedia_removeall(&sc->sc_media);
374 if_free_type(ifp, IFT_ETHER);
376 mtx_lock(&lagg_list_mtx);
377 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
378 mtx_unlock(&lagg_list_mtx);
380 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
381 LAGG_LOCK_DESTROY(sc);
386 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
388 struct ifnet *ifp = sc->sc_ifp;
390 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
393 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
394 /* Let the protocol know the MAC has changed */
395 if (sc->sc_lladdr != NULL)
396 (*sc->sc_lladdr)(sc);
397 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
401 lagg_capabilities(struct lagg_softc *sc)
403 struct lagg_port *lp;
404 int cap = ~0, ena = ~0;
407 LAGG_WLOCK_ASSERT(sc);
409 /* Get capabilities from the lagg ports */
410 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
411 cap &= lp->lp_ifp->if_capabilities;
412 ena &= lp->lp_ifp->if_capenable;
413 hwa &= lp->lp_ifp->if_hwassist;
415 cap = (cap == ~0 ? 0 : cap);
416 ena = (ena == ~0 ? 0 : ena);
417 hwa = (hwa == ~0 ? 0 : hwa);
419 if (sc->sc_ifp->if_capabilities != cap ||
420 sc->sc_ifp->if_capenable != ena ||
421 sc->sc_ifp->if_hwassist != hwa) {
422 sc->sc_ifp->if_capabilities = cap;
423 sc->sc_ifp->if_capenable = ena;
424 sc->sc_ifp->if_hwassist = hwa;
425 getmicrotime(&sc->sc_ifp->if_lastchange);
427 if (sc->sc_ifflags & IFF_DEBUG)
428 if_printf(sc->sc_ifp,
429 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
434 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
436 struct lagg_softc *sc = lp->lp_softc;
437 struct ifnet *ifp = lp->lp_ifp;
438 struct lagg_llq *llq;
441 LAGG_WLOCK_ASSERT(sc);
443 if (lp->lp_detaching ||
444 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
447 /* Check to make sure its not already queued to be changed */
448 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
449 if (llq->llq_ifp == ifp) {
456 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
457 if (llq == NULL) /* XXX what to do */
461 /* Update the lladdr even if pending, it may have changed */
463 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
466 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
468 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
472 * Set the interface MAC address from a taskqueue to avoid a LOR.
475 lagg_port_setlladdr(void *arg, int pending)
477 struct lagg_softc *sc = (struct lagg_softc *)arg;
478 struct lagg_llq *llq, *head;
482 /* Grab a local reference of the queue and remove it from the softc */
484 head = SLIST_FIRST(&sc->sc_llq_head);
485 SLIST_FIRST(&sc->sc_llq_head) = NULL;
489 * Traverse the queue and set the lladdr on each ifp. It is safe to do
490 * unlocked as we have the only reference to it.
492 for (llq = head; llq != NULL; llq = head) {
495 /* Set the link layer address */
496 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
498 printf("%s: setlladdr failed on %s\n", __func__,
501 head = SLIST_NEXT(llq, llq_entries);
507 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
509 struct lagg_softc *sc_ptr;
510 struct lagg_port *lp;
513 LAGG_WLOCK_ASSERT(sc);
515 /* Limit the maximal number of lagg ports */
516 if (sc->sc_count >= LAGG_MAX_PORTS)
519 /* Check if port has already been associated to a lagg */
520 if (ifp->if_lagg != NULL) {
521 /* Port is already in the current lagg? */
522 lp = (struct lagg_port *)ifp->if_lagg;
523 if (lp->lp_softc == sc)
528 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
529 if (ifp->if_type != IFT_ETHER)
530 return (EPROTONOSUPPORT);
532 /* Allow the first Ethernet member to define the MTU */
533 if (SLIST_EMPTY(&sc->sc_ports))
534 sc->sc_ifp->if_mtu = ifp->if_mtu;
535 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
536 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
541 if ((lp = malloc(sizeof(struct lagg_port),
542 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
545 /* Check if port is a stacked lagg */
546 mtx_lock(&lagg_list_mtx);
547 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
548 if (ifp == sc_ptr->sc_ifp) {
549 mtx_unlock(&lagg_list_mtx);
552 /* XXX disable stacking for the moment, its untested */
553 #ifdef LAGG_PORT_STACKING
554 lp->lp_flags |= LAGG_PORT_STACK;
555 if (lagg_port_checkstacking(sc_ptr) >=
557 mtx_unlock(&lagg_list_mtx);
564 mtx_unlock(&lagg_list_mtx);
566 /* Change the interface type */
567 lp->lp_iftype = ifp->if_type;
568 ifp->if_type = IFT_IEEE8023ADLAG;
570 lp->lp_ioctl = ifp->if_ioctl;
571 ifp->if_ioctl = lagg_port_ioctl;
572 lp->lp_output = ifp->if_output;
573 ifp->if_output = lagg_port_output;
578 /* Save port link layer address */
579 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
581 if (SLIST_EMPTY(&sc->sc_ports)) {
583 lagg_lladdr(sc, IF_LLADDR(ifp));
585 /* Update link layer address for this port */
586 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
589 /* Insert into the list of ports */
590 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
593 /* Update lagg capabilities */
594 lagg_capabilities(sc);
597 /* Add multicast addresses and interface flags to this port */
598 lagg_ether_cmdmulti(lp, 1);
599 lagg_setflags(lp, 1);
601 if (sc->sc_port_create != NULL)
602 error = (*sc->sc_port_create)(lp);
604 /* remove the port again, without calling sc_port_destroy */
605 lagg_port_destroy(lp, 0);
612 #ifdef LAGG_PORT_STACKING
614 lagg_port_checkstacking(struct lagg_softc *sc)
616 struct lagg_softc *sc_ptr;
617 struct lagg_port *lp;
620 LAGG_WLOCK_ASSERT(sc);
622 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
623 if (lp->lp_flags & LAGG_PORT_STACK) {
624 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
625 m = MAX(m, lagg_port_checkstacking(sc_ptr));
634 lagg_port_destroy(struct lagg_port *lp, int runpd)
636 struct lagg_softc *sc = lp->lp_softc;
637 struct lagg_port *lp_ptr;
638 struct lagg_llq *llq;
639 struct ifnet *ifp = lp->lp_ifp;
641 LAGG_WLOCK_ASSERT(sc);
643 if (runpd && sc->sc_port_destroy != NULL)
644 (*sc->sc_port_destroy)(lp);
647 * Remove multicast addresses and interface flags from this port and
648 * reset the MAC address, skip if the interface is being detached.
650 if (!lp->lp_detaching) {
651 lagg_ether_cmdmulti(lp, 0);
652 lagg_setflags(lp, 0);
653 lagg_port_lladdr(lp, lp->lp_lladdr);
656 /* Restore interface */
657 ifp->if_type = lp->lp_iftype;
658 ifp->if_ioctl = lp->lp_ioctl;
659 ifp->if_output = lp->lp_output;
662 /* Finally, remove the port from the lagg */
663 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
666 /* Update the primary interface */
667 if (lp == sc->sc_primary) {
668 uint8_t lladdr[ETHER_ADDR_LEN];
670 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
671 bzero(&lladdr, ETHER_ADDR_LEN);
673 bcopy(lp_ptr->lp_lladdr,
674 lladdr, ETHER_ADDR_LEN);
676 lagg_lladdr(sc, lladdr);
677 sc->sc_primary = lp_ptr;
679 /* Update link layer address for each port */
680 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
681 lagg_port_lladdr(lp_ptr, lladdr);
684 /* Remove any pending lladdr changes from the queue */
685 if (lp->lp_detaching) {
686 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
687 if (llq->llq_ifp == ifp) {
688 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
691 break; /* Only appears once */
697 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
701 /* Update lagg capabilities */
702 lagg_capabilities(sc);
709 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
711 struct lagg_reqport *rp = (struct lagg_reqport *)data;
712 struct lagg_softc *sc;
713 struct lagg_port *lp = NULL;
716 /* Should be checked by the caller */
717 if (ifp->if_type != IFT_IEEE8023ADLAG ||
718 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
723 if (rp->rp_portname[0] == '\0' ||
724 ifunit(rp->rp_portname) != ifp) {
730 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
736 lagg_port2req(lp, rp);
741 if (lp->lp_ioctl == NULL) {
745 error = (*lp->lp_ioctl)(ifp, cmd, data);
749 /* Update lagg interface capabilities */
751 lagg_capabilities(sc);
756 /* Do not allow the MTU to be changed once joined */
767 if (lp->lp_ioctl != NULL)
768 return ((*lp->lp_ioctl)(ifp, cmd, data));
774 * For direct output to child ports.
777 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
778 struct sockaddr *dst, struct route *ro)
780 struct lagg_port *lp = ifp->if_lagg;
782 switch (dst->sa_family) {
783 case pseudo_AF_HDRCMPLT:
785 return ((*lp->lp_output)(ifp, m, dst, ro));
788 /* drop any other frames */
794 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
796 struct lagg_port *lp;
797 struct lagg_softc *sc;
799 if ((lp = ifp->if_lagg) == NULL)
801 /* If the ifnet is just being renamed, don't do anything. */
802 if (ifp->if_flags & IFF_RENAMING)
808 lp->lp_detaching = 1;
809 lagg_port_destroy(lp, 1);
814 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
816 struct lagg_softc *sc = lp->lp_softc;
818 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
819 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
820 rp->rp_prio = lp->lp_prio;
821 rp->rp_flags = lp->lp_flags;
822 if (sc->sc_portreq != NULL)
823 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
825 /* Add protocol specific flags */
826 switch (sc->sc_proto) {
827 case LAGG_PROTO_FAILOVER:
828 if (lp == sc->sc_primary)
829 rp->rp_flags |= LAGG_PORT_MASTER;
830 if (lp == lagg_link_active(sc, sc->sc_primary))
831 rp->rp_flags |= LAGG_PORT_ACTIVE;
834 case LAGG_PROTO_ROUNDROBIN:
835 case LAGG_PROTO_LOADBALANCE:
836 case LAGG_PROTO_ETHERCHANNEL:
837 if (LAGG_PORTACTIVE(lp))
838 rp->rp_flags |= LAGG_PORT_ACTIVE;
841 case LAGG_PROTO_LACP:
842 /* LACP has a different definition of active */
843 if (lacp_isactive(lp))
844 rp->rp_flags |= LAGG_PORT_ACTIVE;
845 if (lacp_iscollecting(lp))
846 rp->rp_flags |= LAGG_PORT_COLLECTING;
847 if (lacp_isdistributing(lp))
848 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
857 struct lagg_softc *sc = (struct lagg_softc *)xsc;
858 struct lagg_port *lp;
859 struct ifnet *ifp = sc->sc_ifp;
861 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
866 ifp->if_drv_flags |= IFF_DRV_RUNNING;
867 /* Update the port lladdrs */
868 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
869 lagg_port_lladdr(lp, IF_LLADDR(ifp));
871 if (sc->sc_init != NULL)
878 lagg_stop(struct lagg_softc *sc)
880 struct ifnet *ifp = sc->sc_ifp;
882 LAGG_WLOCK_ASSERT(sc);
884 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
887 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
889 if (sc->sc_stop != NULL)
894 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
896 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
897 struct lagg_reqall *ra = (struct lagg_reqall *)data;
898 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
899 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
900 struct ifreq *ifr = (struct ifreq *)data;
901 struct lagg_port *lp;
903 struct thread *td = curthread;
905 int count, buflen, len, error = 0;
907 bzero(&rpbuf, sizeof(rpbuf));
913 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
915 buflen = count * sizeof(struct lagg_reqport);
918 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
921 ra->ra_proto = sc->sc_proto;
922 if (sc->sc_req != NULL)
923 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
927 len = min(ra->ra_size, buflen);
928 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
929 if (len < sizeof(rpbuf))
932 lagg_port2req(lp, &rpbuf);
933 memcpy(buf, &rpbuf, sizeof(rpbuf));
935 buf += sizeof(rpbuf);
936 len -= sizeof(rpbuf);
939 ra->ra_ports = count;
940 ra->ra_size = count * sizeof(rpbuf);
941 error = copyout(outbuf, ra->ra_port, ra->ra_size);
942 free(outbuf, M_TEMP);
945 error = priv_check(td, PRIV_NET_LAGG);
948 if (ra->ra_proto >= LAGG_PROTO_MAX) {
949 error = EPROTONOSUPPORT;
953 if (sc->sc_proto != LAGG_PROTO_NONE) {
954 /* Reset protocol first in case detach unlocks */
955 sc->sc_proto = LAGG_PROTO_NONE;
956 error = sc->sc_detach(sc);
957 sc->sc_detach = NULL;
960 sc->sc_port_create = NULL;
961 sc->sc_port_destroy = NULL;
962 sc->sc_linkstate = NULL;
965 sc->sc_lladdr = NULL;
967 sc->sc_portreq = NULL;
968 } else if (sc->sc_input != NULL) {
969 /* Still detaching */
976 for (int i = 0; i < (sizeof(lagg_protos) /
977 sizeof(lagg_protos[0])); i++) {
978 if (lagg_protos[i].ti_proto == ra->ra_proto) {
979 if (sc->sc_ifflags & IFF_DEBUG)
980 printf("%s: using proto %u\n",
982 lagg_protos[i].ti_proto);
983 sc->sc_proto = lagg_protos[i].ti_proto;
984 if (sc->sc_proto != LAGG_PROTO_NONE)
985 error = lagg_protos[i].ti_attach(sc);
991 error = EPROTONOSUPPORT;
994 rf->rf_flags = sc->sc_flags;
997 error = priv_check(td, PRIV_NET_LAGG);
1000 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1005 sc->sc_flags &= ~LAGG_F_HASHMASK;
1006 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1010 if (rp->rp_portname[0] == '\0' ||
1011 (tpif = ifunit(rp->rp_portname)) == NULL) {
1017 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1018 lp->lp_softc != sc) {
1024 lagg_port2req(lp, rp);
1028 error = priv_check(td, PRIV_NET_LAGG);
1031 if (rp->rp_portname[0] == '\0' ||
1032 (tpif = ifunit(rp->rp_portname)) == NULL) {
1037 error = lagg_port_create(sc, tpif);
1040 case SIOCSLAGGDELPORT:
1041 error = priv_check(td, PRIV_NET_LAGG);
1044 if (rp->rp_portname[0] == '\0' ||
1045 (tpif = ifunit(rp->rp_portname)) == NULL) {
1051 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1052 lp->lp_softc != sc) {
1058 error = lagg_port_destroy(lp, 1);
1062 /* Set flags on ports too */
1064 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1065 lagg_setflags(lp, 1);
1069 if (!(ifp->if_flags & IFF_UP) &&
1070 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1072 * If interface is marked down and it is running,
1073 * then stop and disable it.
1078 } else if ((ifp->if_flags & IFF_UP) &&
1079 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1081 * If interface is marked up and it is stopped, then
1084 (*ifp->if_init)(sc);
1090 error = lagg_ether_setmulti(sc);
1095 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1100 /* Do not allow the MTU or caps to be directly changed */
1105 error = ether_ioctl(ifp, cmd, data);
1112 lagg_ether_setmulti(struct lagg_softc *sc)
1114 struct lagg_port *lp;
1116 LAGG_WLOCK_ASSERT(sc);
1118 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1119 /* First, remove any existing filter entries. */
1120 lagg_ether_cmdmulti(lp, 0);
1121 /* copy all addresses from the lagg interface to the port */
1122 lagg_ether_cmdmulti(lp, 1);
1128 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1130 struct lagg_softc *sc = lp->lp_softc;
1131 struct ifnet *ifp = lp->lp_ifp;
1132 struct ifnet *scifp = sc->sc_ifp;
1134 struct ifmultiaddr *ifma, *rifma = NULL;
1135 struct sockaddr_dl sdl;
1138 LAGG_WLOCK_ASSERT(sc);
1140 bzero((char *)&sdl, sizeof(sdl));
1141 sdl.sdl_len = sizeof(sdl);
1142 sdl.sdl_family = AF_LINK;
1143 sdl.sdl_type = IFT_ETHER;
1144 sdl.sdl_alen = ETHER_ADDR_LEN;
1145 sdl.sdl_index = ifp->if_index;
1148 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1149 if (ifma->ifma_addr->sa_family != AF_LINK)
1151 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1152 LLADDR(&sdl), ETHER_ADDR_LEN);
1154 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1157 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1160 mc->mc_ifma = rifma;
1161 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1164 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1165 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1166 if_delmulti_ifma(mc->mc_ifma);
1173 /* Handle a ref counted flag that should be set on the lagg port as well */
1175 lagg_setflag(struct lagg_port *lp, int flag, int status,
1176 int (*func)(struct ifnet *, int))
1178 struct lagg_softc *sc = lp->lp_softc;
1179 struct ifnet *scifp = sc->sc_ifp;
1180 struct ifnet *ifp = lp->lp_ifp;
1183 LAGG_WLOCK_ASSERT(sc);
1185 status = status ? (scifp->if_flags & flag) : 0;
1186 /* Now "status" contains the flag value or 0 */
1189 * See if recorded ports status is different from what
1190 * we want it to be. If it is, flip it. We record ports
1191 * status in lp_ifflags so that we won't clear ports flag
1192 * we haven't set. In fact, we don't clear or set ports
1193 * flags directly, but get or release references to them.
1194 * That's why we can be sure that recorded flags still are
1195 * in accord with actual ports flags.
1197 if (status != (lp->lp_ifflags & flag)) {
1198 error = (*func)(ifp, status);
1201 lp->lp_ifflags &= ~flag;
1202 lp->lp_ifflags |= status;
1208 * Handle IFF_* flags that require certain changes on the lagg port
1209 * if "status" is true, update ports flags respective to the lagg
1210 * if "status" is false, forcedly clear the flags set on port.
1213 lagg_setflags(struct lagg_port *lp, int status)
1217 for (i = 0; lagg_pflags[i].flag; i++) {
1218 error = lagg_setflag(lp, lagg_pflags[i].flag,
1219 status, lagg_pflags[i].func);
1227 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1229 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1230 int error, len, mcast;
1232 len = m->m_pkthdr.len;
1233 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1236 /* We need a Tx algorithm and at least one port */
1237 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1244 ETHER_BPF_MTAP(ifp, m);
1246 error = (*sc->sc_start)(sc, m);
1251 ifp->if_omcasts += mcast;
1252 ifp->if_obytes += len;
1260 * The ifp->if_qflush entry point for lagg(4) is no-op.
1263 lagg_qflush(struct ifnet *ifp __unused)
1267 static struct mbuf *
1268 lagg_input(struct ifnet *ifp, struct mbuf *m)
1270 struct lagg_port *lp = ifp->if_lagg;
1271 struct lagg_softc *sc = lp->lp_softc;
1272 struct ifnet *scifp = sc->sc_ifp;
1275 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1276 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1277 sc->sc_proto == LAGG_PROTO_NONE) {
1283 ETHER_BPF_MTAP(scifp, m);
1285 m = (*sc->sc_input)(sc, lp, m);
1288 scifp->if_ipackets++;
1289 scifp->if_ibytes += m->m_pkthdr.len;
1291 if (scifp->if_flags & IFF_MONITOR) {
1302 lagg_media_change(struct ifnet *ifp)
1304 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1306 if (sc->sc_ifflags & IFF_DEBUG)
1307 printf("%s\n", __func__);
1314 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1316 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1317 struct lagg_port *lp;
1319 imr->ifm_status = IFM_AVALID;
1320 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1323 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1324 if (LAGG_PORTACTIVE(lp))
1325 imr->ifm_status |= IFM_ACTIVE;
1331 lagg_linkstate(struct lagg_softc *sc)
1333 struct lagg_port *lp;
1334 int new_link = LINK_STATE_DOWN;
1337 /* Our link is considered up if at least one of our ports is active */
1338 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1339 if (lp->lp_link_state == LINK_STATE_UP) {
1340 new_link = LINK_STATE_UP;
1344 if_link_state_change(sc->sc_ifp, new_link);
1346 /* Update if_baudrate to reflect the max possible speed */
1347 switch (sc->sc_proto) {
1348 case LAGG_PROTO_FAILOVER:
1349 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1350 sc->sc_primary->lp_ifp->if_baudrate : 0;
1352 case LAGG_PROTO_ROUNDROBIN:
1353 case LAGG_PROTO_LOADBALANCE:
1354 case LAGG_PROTO_ETHERCHANNEL:
1356 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1357 speed += lp->lp_ifp->if_baudrate;
1358 sc->sc_ifp->if_baudrate = speed;
1360 case LAGG_PROTO_LACP:
1361 /* LACP updates if_baudrate itself */
1367 lagg_port_state(struct ifnet *ifp, int state)
1369 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1370 struct lagg_softc *sc = NULL;
1379 if (sc->sc_linkstate != NULL)
1380 (*sc->sc_linkstate)(lp);
1385 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1387 struct lagg_port *lp_next, *rval = NULL;
1388 // int new_link = LINK_STATE_DOWN;
1390 LAGG_RLOCK_ASSERT(sc);
1392 * Search a port which reports an active link state.
1397 if (LAGG_PORTACTIVE(lp)) {
1401 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1402 LAGG_PORTACTIVE(lp_next)) {
1408 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1409 if (LAGG_PORTACTIVE(lp_next)) {
1418 * The IEEE 802.1D standard assumes that a lagg with
1419 * multiple ports is always full duplex. This is valid
1420 * for load sharing laggs and if at least two links
1421 * are active. Unfortunately, checking the latter would
1422 * be too expensive at this point.
1424 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1426 new_link = LINK_STATE_FULL_DUPLEX;
1428 new_link = rval->lp_link_state;
1436 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1438 if (m->m_pkthdr.len < (off + len)) {
1440 } else if (m->m_len < (off + len)) {
1441 m_copydata(m, off, len, buf);
1444 return (mtod(m, char *) + off);
1448 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1453 struct ether_header *eh;
1454 const struct ether_vlan_header *vlan;
1456 const struct ip *ip;
1457 const uint32_t *ports;
1461 const struct ip6_hdr *ip6;
1471 struct ether_vlan_header vlan;
1479 eh = mtod(m, struct ether_header *);
1480 etype = ntohs(eh->ether_type);
1481 if (sc->sc_flags & LAGG_F_HASHL2) {
1482 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1483 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1486 /* Special handling for encapsulating VLAN frames */
1487 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1488 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1489 sizeof(m->m_pkthdr.ether_vtag), p);
1490 } else if (etype == ETHERTYPE_VLAN) {
1491 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1495 if (sc->sc_flags & LAGG_F_HASHL2)
1496 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1497 etype = ntohs(vlan->evl_proto);
1498 off += sizeof(*vlan) - sizeof(*eh);
1504 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1508 if (sc->sc_flags & LAGG_F_HASHL3) {
1509 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1510 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1512 if (!(sc->sc_flags & LAGG_F_HASHL4))
1518 iphlen = ip->ip_hl << 2;
1519 if (iphlen < sizeof(*ip))
1522 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1525 p = hash32_buf(ports, sizeof(*ports), p);
1531 case ETHERTYPE_IPV6:
1532 if (!(sc->sc_flags & LAGG_F_HASHL3))
1534 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1538 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1539 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1540 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1541 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1550 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1553 return (ifp->if_transmit)(ifp, m);
1557 * Simple round robin aggregation
1561 lagg_rr_attach(struct lagg_softc *sc)
1563 sc->sc_detach = lagg_rr_detach;
1564 sc->sc_start = lagg_rr_start;
1565 sc->sc_input = lagg_rr_input;
1566 sc->sc_port_create = NULL;
1567 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1574 lagg_rr_detach(struct lagg_softc *sc)
1580 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1582 struct lagg_port *lp;
1585 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1587 lp = SLIST_FIRST(&sc->sc_ports);
1589 lp = SLIST_NEXT(lp, lp_entries);
1592 * Check the port's link state. This will return the next active
1593 * port if the link is down or the port is NULL.
1595 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1601 return (lagg_enqueue(lp->lp_ifp, m));
1604 static struct mbuf *
1605 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1607 struct ifnet *ifp = sc->sc_ifp;
1609 /* Just pass in the packet to our lagg device */
1610 m->m_pkthdr.rcvif = ifp;
1620 lagg_fail_attach(struct lagg_softc *sc)
1622 sc->sc_detach = lagg_fail_detach;
1623 sc->sc_start = lagg_fail_start;
1624 sc->sc_input = lagg_fail_input;
1625 sc->sc_port_create = NULL;
1626 sc->sc_port_destroy = NULL;
1632 lagg_fail_detach(struct lagg_softc *sc)
1638 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1640 struct lagg_port *lp;
1642 /* Use the master port if active or the next available port */
1643 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1649 return (lagg_enqueue(lp->lp_ifp, m));
1652 static struct mbuf *
1653 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1655 struct ifnet *ifp = sc->sc_ifp;
1656 struct lagg_port *tmp_tp;
1658 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1659 m->m_pkthdr.rcvif = ifp;
1663 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1664 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1666 * If tmp_tp is null, we've recieved a packet when all
1667 * our links are down. Weird, but process it anyways.
1669 if ((tmp_tp == NULL || tmp_tp == lp)) {
1670 m->m_pkthdr.rcvif = ifp;
1684 lagg_lb_attach(struct lagg_softc *sc)
1686 struct lagg_port *lp;
1689 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1690 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1693 sc->sc_detach = lagg_lb_detach;
1694 sc->sc_start = lagg_lb_start;
1695 sc->sc_input = lagg_lb_input;
1696 sc->sc_port_create = lagg_lb_port_create;
1697 sc->sc_port_destroy = lagg_lb_port_destroy;
1698 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1700 lb->lb_key = arc4random();
1701 sc->sc_psc = (caddr_t)lb;
1703 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1704 lagg_lb_port_create(lp);
1710 lagg_lb_detach(struct lagg_softc *sc)
1712 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1719 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1721 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1722 struct lagg_port *lp_next;
1725 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1726 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1729 if (i >= LAGG_MAX_PORTS)
1731 if (sc->sc_ifflags & IFF_DEBUG)
1732 printf("%s: port %s at index %d\n",
1733 sc->sc_ifname, lp_next->lp_ifname, i);
1734 lb->lb_ports[i++] = lp_next;
1741 lagg_lb_port_create(struct lagg_port *lp)
1743 struct lagg_softc *sc = lp->lp_softc;
1744 return (lagg_lb_porttable(sc, NULL));
1748 lagg_lb_port_destroy(struct lagg_port *lp)
1750 struct lagg_softc *sc = lp->lp_softc;
1751 lagg_lb_porttable(sc, lp);
1755 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1757 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1758 struct lagg_port *lp = NULL;
1761 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1762 p = m->m_pkthdr.flowid;
1764 p = lagg_hashmbuf(sc, m, lb->lb_key);
1766 lp = lb->lb_ports[p];
1769 * Check the port's link state. This will return the next active
1770 * port if the link is down or the port is NULL.
1772 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1778 return (lagg_enqueue(lp->lp_ifp, m));
1781 static struct mbuf *
1782 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1784 struct ifnet *ifp = sc->sc_ifp;
1786 /* Just pass in the packet to our lagg device */
1787 m->m_pkthdr.rcvif = ifp;
1797 lagg_lacp_attach(struct lagg_softc *sc)
1799 struct lagg_port *lp;
1802 sc->sc_detach = lagg_lacp_detach;
1803 sc->sc_port_create = lacp_port_create;
1804 sc->sc_port_destroy = lacp_port_destroy;
1805 sc->sc_linkstate = lacp_linkstate;
1806 sc->sc_start = lagg_lacp_start;
1807 sc->sc_input = lagg_lacp_input;
1808 sc->sc_init = lacp_init;
1809 sc->sc_stop = lacp_stop;
1810 sc->sc_lladdr = lagg_lacp_lladdr;
1811 sc->sc_req = lacp_req;
1812 sc->sc_portreq = lacp_portreq;
1814 error = lacp_attach(sc);
1818 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1819 lacp_port_create(lp);
1825 lagg_lacp_detach(struct lagg_softc *sc)
1827 struct lagg_port *lp;
1830 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1831 lacp_port_destroy(lp);
1833 /* unlocking is safe here */
1835 error = lacp_detach(sc);
1842 lagg_lacp_lladdr(struct lagg_softc *sc)
1844 struct lagg_port *lp;
1846 /* purge all the lacp ports */
1847 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1848 lacp_port_destroy(lp);
1850 /* add them back in */
1851 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1852 lacp_port_create(lp);
1856 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1858 struct lagg_port *lp;
1860 lp = lacp_select_tx_port(sc, m);
1867 return (lagg_enqueue(lp->lp_ifp, m));
1870 static struct mbuf *
1871 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1873 struct ifnet *ifp = sc->sc_ifp;
1874 struct ether_header *eh;
1877 eh = mtod(m, struct ether_header *);
1878 etype = ntohs(eh->ether_type);
1880 /* Tap off LACP control messages */
1881 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1882 m = lacp_input(lp, m);
1888 * If the port is not collecting or not in the active aggregator then
1891 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1896 m->m_pkthdr.rcvif = ifp;