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>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip.h>
63 #include <netinet/ip6.h>
66 #include <net/if_vlan_var.h>
67 #include <net/if_lagg.h>
68 #include <net/ieee8023ad_lacp.h>
70 /* Special flags we should propagate to the lagg ports. */
73 int (*func)(struct ifnet *, int);
75 {IFF_PROMISC, ifpromisc},
76 {IFF_ALLMULTI, if_allmulti},
80 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
81 static struct mtx lagg_list_mtx;
82 eventhandler_tag lagg_detach_cookie = NULL;
84 static int lagg_clone_create(struct if_clone *, int, caddr_t);
85 static void lagg_clone_destroy(struct ifnet *);
86 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
87 static void lagg_capabilities(struct lagg_softc *);
88 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
89 static void lagg_port_setlladdr(void *, int);
90 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
91 static int lagg_port_destroy(struct lagg_port *, int);
92 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
93 static void lagg_linkstate(struct lagg_softc *);
94 static void lagg_port_state(struct ifnet *, int);
95 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
96 static int lagg_port_output(struct ifnet *, struct mbuf *,
97 struct sockaddr *, struct route *);
98 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
99 static int lagg_port_checkstacking(struct lagg_softc *);
100 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
101 static void lagg_init(void *);
102 static void lagg_stop(struct lagg_softc *);
103 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
104 static int lagg_ether_setmulti(struct lagg_softc *);
105 static int lagg_ether_cmdmulti(struct lagg_port *, int);
106 static int lagg_setflag(struct lagg_port *, int, int,
107 int (*func)(struct ifnet *, int));
108 static int lagg_setflags(struct lagg_port *, int status);
109 static void lagg_start(struct ifnet *);
110 static int lagg_media_change(struct ifnet *);
111 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
112 static struct lagg_port *lagg_link_active(struct lagg_softc *,
114 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
116 IFC_SIMPLE_DECLARE(lagg, 0);
118 /* Simple round robin */
119 static int lagg_rr_attach(struct lagg_softc *);
120 static int lagg_rr_detach(struct lagg_softc *);
121 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
122 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
125 /* Active failover */
126 static int lagg_fail_attach(struct lagg_softc *);
127 static int lagg_fail_detach(struct lagg_softc *);
128 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
129 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
133 static int lagg_lb_attach(struct lagg_softc *);
134 static int lagg_lb_detach(struct lagg_softc *);
135 static int lagg_lb_port_create(struct lagg_port *);
136 static void lagg_lb_port_destroy(struct lagg_port *);
137 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
138 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
140 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
143 static int lagg_lacp_attach(struct lagg_softc *);
144 static int lagg_lacp_detach(struct lagg_softc *);
145 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
146 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
148 static void lagg_lacp_lladdr(struct lagg_softc *);
150 /* lagg protocol table */
151 static const struct {
153 int (*ti_attach)(struct lagg_softc *);
155 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
156 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
157 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
158 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
159 { LAGG_PROTO_LACP, lagg_lacp_attach },
160 { LAGG_PROTO_NONE, NULL }
163 SYSCTL_DECL(_net_link);
164 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, "Link Aggregation");
166 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
167 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
168 &lagg_failover_rx_all, 0,
169 "Accept input from any interface in a failover lagg");
170 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
171 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
172 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
174 "Default setting for using flow id for load sharing");
177 lagg_modevent(module_t mod, int type, void *data)
182 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
183 SLIST_INIT(&lagg_list);
184 if_clone_attach(&lagg_cloner);
185 lagg_input_p = lagg_input;
186 lagg_linkstate_p = lagg_port_state;
187 lagg_detach_cookie = EVENTHANDLER_REGISTER(
188 ifnet_departure_event, lagg_port_ifdetach, NULL,
189 EVENTHANDLER_PRI_ANY);
192 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
194 if_clone_detach(&lagg_cloner);
196 lagg_linkstate_p = NULL;
197 mtx_destroy(&lagg_list_mtx);
205 static moduledata_t lagg_mod = {
211 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
212 MODULE_VERSION(if_lagg, 1);
214 #if __FreeBSD_version >= 800000
216 * This routine is run via an vlan
220 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
222 struct lagg_softc *sc = ifp->if_softc;
223 struct lagg_port *lp;
225 if (ifp->if_softc != arg) /* Not our event */
229 if (!SLIST_EMPTY(&sc->sc_ports)) {
230 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
231 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
237 * This routine is run via an vlan
241 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
243 struct lagg_softc *sc = ifp->if_softc;
244 struct lagg_port *lp;
246 if (ifp->if_softc != arg) /* Not our event */
250 if (!SLIST_EMPTY(&sc->sc_ports)) {
251 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
252 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
259 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
261 struct lagg_softc *sc;
264 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
265 struct sysctl_oid *oid;
266 char num[14]; /* sufficient for 32 bits */
268 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
269 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
275 sysctl_ctx_init(&sc->ctx);
276 snprintf(num, sizeof(num), "%u", unit);
277 sc->use_flowid = def_use_flowid;
278 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
279 OID_AUTO, num, CTLFLAG_RD, NULL, "");
280 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
281 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
282 "Use flow id for load sharing");
283 /* Hash all layers by default */
284 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
286 sc->sc_proto = LAGG_PROTO_NONE;
287 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
288 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
289 sc->sc_proto = lagg_protos[i].ti_proto;
290 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
291 if_free_type(ifp, IFT_ETHER);
299 SLIST_INIT(&sc->sc_ports);
300 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
302 /* Initialise pseudo media types */
303 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
305 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
306 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
308 if_initname(ifp, ifc->ifc_name, unit);
309 ifp->if_type = IFT_ETHER;
311 ifp->if_start = lagg_start;
312 ifp->if_init = lagg_init;
313 ifp->if_ioctl = lagg_ioctl;
314 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
316 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
317 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
318 IFQ_SET_READY(&ifp->if_snd);
321 * Attach as an ordinary ethernet device, childs will be attached
322 * as special device IFT_IEEE8023ADLAG.
324 ether_ifattach(ifp, eaddr);
326 #if __FreeBSD_version >= 800000
327 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
328 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
329 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
330 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
333 /* Insert into the global list of laggs */
334 mtx_lock(&lagg_list_mtx);
335 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
336 mtx_unlock(&lagg_list_mtx);
342 lagg_clone_destroy(struct ifnet *ifp)
344 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
345 struct lagg_port *lp;
350 ifp->if_flags &= ~IFF_UP;
352 #if __FreeBSD_version >= 800000
353 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
354 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
357 /* Shutdown and remove lagg ports */
358 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
359 lagg_port_destroy(lp, 1);
360 /* Unhook the aggregation protocol */
361 (*sc->sc_detach)(sc);
365 sysctl_ctx_free(&sc->ctx);
366 ifmedia_removeall(&sc->sc_media);
368 if_free_type(ifp, IFT_ETHER);
370 mtx_lock(&lagg_list_mtx);
371 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
372 mtx_unlock(&lagg_list_mtx);
374 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
375 LAGG_LOCK_DESTROY(sc);
380 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
382 struct ifnet *ifp = sc->sc_ifp;
384 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
387 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
388 /* Let the protocol know the MAC has changed */
389 if (sc->sc_lladdr != NULL)
390 (*sc->sc_lladdr)(sc);
391 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
395 lagg_capabilities(struct lagg_softc *sc)
397 struct lagg_port *lp;
398 int cap = ~0, ena = ~0;
401 LAGG_WLOCK_ASSERT(sc);
403 /* Get capabilities from the lagg ports */
404 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
405 cap &= lp->lp_ifp->if_capabilities;
406 ena &= lp->lp_ifp->if_capenable;
407 hwa &= lp->lp_ifp->if_hwassist;
409 cap = (cap == ~0 ? 0 : cap);
410 ena = (ena == ~0 ? 0 : ena);
411 hwa = (hwa == ~0 ? 0 : hwa);
413 if (sc->sc_ifp->if_capabilities != cap ||
414 sc->sc_ifp->if_capenable != ena ||
415 sc->sc_ifp->if_hwassist != hwa) {
416 sc->sc_ifp->if_capabilities = cap;
417 sc->sc_ifp->if_capenable = ena;
418 sc->sc_ifp->if_hwassist = hwa;
419 getmicrotime(&sc->sc_ifp->if_lastchange);
421 if (sc->sc_ifflags & IFF_DEBUG)
422 if_printf(sc->sc_ifp,
423 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
428 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
430 struct lagg_softc *sc = lp->lp_softc;
431 struct ifnet *ifp = lp->lp_ifp;
432 struct lagg_llq *llq;
435 LAGG_WLOCK_ASSERT(sc);
437 if (lp->lp_detaching ||
438 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
441 /* Check to make sure its not already queued to be changed */
442 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
443 if (llq->llq_ifp == ifp) {
450 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
451 if (llq == NULL) /* XXX what to do */
455 /* Update the lladdr even if pending, it may have changed */
457 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
460 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
462 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
466 * Set the interface MAC address from a taskqueue to avoid a LOR.
469 lagg_port_setlladdr(void *arg, int pending)
471 struct lagg_softc *sc = (struct lagg_softc *)arg;
472 struct lagg_llq *llq, *head;
476 /* Grab a local reference of the queue and remove it from the softc */
478 head = SLIST_FIRST(&sc->sc_llq_head);
479 SLIST_FIRST(&sc->sc_llq_head) = NULL;
483 * Traverse the queue and set the lladdr on each ifp. It is safe to do
484 * unlocked as we have the only reference to it.
486 for (llq = head; llq != NULL; llq = head) {
489 /* Set the link layer address */
490 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
492 printf("%s: setlladdr failed on %s\n", __func__,
495 head = SLIST_NEXT(llq, llq_entries);
501 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
503 struct lagg_softc *sc_ptr;
504 struct lagg_port *lp;
507 LAGG_WLOCK_ASSERT(sc);
509 /* Limit the maximal number of lagg ports */
510 if (sc->sc_count >= LAGG_MAX_PORTS)
513 /* Check if port has already been associated to a lagg */
514 if (ifp->if_lagg != NULL)
517 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
518 if (ifp->if_type != IFT_ETHER)
519 return (EPROTONOSUPPORT);
521 /* Allow the first Ethernet member to define the MTU */
522 if (SLIST_EMPTY(&sc->sc_ports))
523 sc->sc_ifp->if_mtu = ifp->if_mtu;
524 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
525 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
530 if ((lp = malloc(sizeof(struct lagg_port),
531 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
534 /* Check if port is a stacked lagg */
535 mtx_lock(&lagg_list_mtx);
536 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
537 if (ifp == sc_ptr->sc_ifp) {
538 mtx_unlock(&lagg_list_mtx);
541 /* XXX disable stacking for the moment, its untested
542 lp->lp_flags |= LAGG_PORT_STACK;
543 if (lagg_port_checkstacking(sc_ptr) >=
545 mtx_unlock(&lagg_list_mtx);
552 mtx_unlock(&lagg_list_mtx);
554 /* Change the interface type */
555 lp->lp_iftype = ifp->if_type;
556 ifp->if_type = IFT_IEEE8023ADLAG;
558 lp->lp_ioctl = ifp->if_ioctl;
559 ifp->if_ioctl = lagg_port_ioctl;
560 lp->lp_output = ifp->if_output;
561 ifp->if_output = lagg_port_output;
566 /* Save port link layer address */
567 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
569 if (SLIST_EMPTY(&sc->sc_ports)) {
571 lagg_lladdr(sc, IF_LLADDR(ifp));
573 /* Update link layer address for this port */
574 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
577 /* Insert into the list of ports */
578 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
581 /* Update lagg capabilities */
582 lagg_capabilities(sc);
585 /* Add multicast addresses and interface flags to this port */
586 lagg_ether_cmdmulti(lp, 1);
587 lagg_setflags(lp, 1);
589 if (sc->sc_port_create != NULL)
590 error = (*sc->sc_port_create)(lp);
592 /* remove the port again, without calling sc_port_destroy */
593 lagg_port_destroy(lp, 0);
601 lagg_port_checkstacking(struct lagg_softc *sc)
603 struct lagg_softc *sc_ptr;
604 struct lagg_port *lp;
607 LAGG_WLOCK_ASSERT(sc);
609 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
610 if (lp->lp_flags & LAGG_PORT_STACK) {
611 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
612 m = MAX(m, lagg_port_checkstacking(sc_ptr));
620 lagg_port_destroy(struct lagg_port *lp, int runpd)
622 struct lagg_softc *sc = lp->lp_softc;
623 struct lagg_port *lp_ptr;
624 struct lagg_llq *llq;
625 struct ifnet *ifp = lp->lp_ifp;
627 LAGG_WLOCK_ASSERT(sc);
629 if (runpd && sc->sc_port_destroy != NULL)
630 (*sc->sc_port_destroy)(lp);
633 * Remove multicast addresses and interface flags from this port and
634 * reset the MAC address, skip if the interface is being detached.
636 if (!lp->lp_detaching) {
637 lagg_ether_cmdmulti(lp, 0);
638 lagg_setflags(lp, 0);
639 lagg_port_lladdr(lp, lp->lp_lladdr);
642 /* Restore interface */
643 ifp->if_type = lp->lp_iftype;
644 ifp->if_ioctl = lp->lp_ioctl;
645 ifp->if_output = lp->lp_output;
648 /* Finally, remove the port from the lagg */
649 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
652 /* Update the primary interface */
653 if (lp == sc->sc_primary) {
654 uint8_t lladdr[ETHER_ADDR_LEN];
656 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
657 bzero(&lladdr, ETHER_ADDR_LEN);
659 bcopy(lp_ptr->lp_lladdr,
660 lladdr, ETHER_ADDR_LEN);
662 lagg_lladdr(sc, lladdr);
663 sc->sc_primary = lp_ptr;
665 /* Update link layer address for each port */
666 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
667 lagg_port_lladdr(lp_ptr, lladdr);
670 /* Remove any pending lladdr changes from the queue */
671 if (lp->lp_detaching) {
672 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
673 if (llq->llq_ifp == ifp) {
674 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
677 break; /* Only appears once */
683 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
687 /* Update lagg capabilities */
688 lagg_capabilities(sc);
695 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
697 struct lagg_reqport *rp = (struct lagg_reqport *)data;
698 struct lagg_softc *sc;
699 struct lagg_port *lp = NULL;
702 /* Should be checked by the caller */
703 if (ifp->if_type != IFT_IEEE8023ADLAG ||
704 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
709 if (rp->rp_portname[0] == '\0' ||
710 ifunit(rp->rp_portname) != ifp) {
716 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
722 lagg_port2req(lp, rp);
727 if (lp->lp_ioctl == NULL) {
731 error = (*lp->lp_ioctl)(ifp, cmd, data);
735 /* Update lagg interface capabilities */
737 lagg_capabilities(sc);
742 /* Do not allow the MTU to be changed once joined */
753 if (lp->lp_ioctl != NULL)
754 return ((*lp->lp_ioctl)(ifp, cmd, data));
760 * For direct output to child ports.
763 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
764 struct sockaddr *dst, struct route *ro)
766 struct lagg_port *lp = ifp->if_lagg;
768 switch (dst->sa_family) {
769 case pseudo_AF_HDRCMPLT:
771 return ((*lp->lp_output)(ifp, m, dst, ro));
774 /* drop any other frames */
780 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
782 struct lagg_port *lp;
783 struct lagg_softc *sc;
785 if ((lp = ifp->if_lagg) == NULL)
787 /* If the ifnet is just being renamed, don't do anything. */
788 if (ifp->if_flags & IFF_RENAMING)
794 lp->lp_detaching = 1;
795 lagg_port_destroy(lp, 1);
800 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
802 struct lagg_softc *sc = lp->lp_softc;
804 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
805 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
806 rp->rp_prio = lp->lp_prio;
807 rp->rp_flags = lp->lp_flags;
808 if (sc->sc_portreq != NULL)
809 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
811 /* Add protocol specific flags */
812 switch (sc->sc_proto) {
813 case LAGG_PROTO_FAILOVER:
814 if (lp == sc->sc_primary)
815 rp->rp_flags |= LAGG_PORT_MASTER;
816 if (lp == lagg_link_active(sc, sc->sc_primary))
817 rp->rp_flags |= LAGG_PORT_ACTIVE;
820 case LAGG_PROTO_ROUNDROBIN:
821 case LAGG_PROTO_LOADBALANCE:
822 case LAGG_PROTO_ETHERCHANNEL:
823 if (LAGG_PORTACTIVE(lp))
824 rp->rp_flags |= LAGG_PORT_ACTIVE;
827 case LAGG_PROTO_LACP:
828 /* LACP has a different definition of active */
829 if (lacp_isactive(lp))
830 rp->rp_flags |= LAGG_PORT_ACTIVE;
831 if (lacp_iscollecting(lp))
832 rp->rp_flags |= LAGG_PORT_COLLECTING;
833 if (lacp_isdistributing(lp))
834 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
843 struct lagg_softc *sc = (struct lagg_softc *)xsc;
844 struct lagg_port *lp;
845 struct ifnet *ifp = sc->sc_ifp;
847 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
852 ifp->if_drv_flags |= IFF_DRV_RUNNING;
853 /* Update the port lladdrs */
854 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
855 lagg_port_lladdr(lp, IF_LLADDR(ifp));
857 if (sc->sc_init != NULL)
864 lagg_stop(struct lagg_softc *sc)
866 struct ifnet *ifp = sc->sc_ifp;
868 LAGG_WLOCK_ASSERT(sc);
870 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
873 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
875 if (sc->sc_stop != NULL)
880 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
882 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
883 struct lagg_reqall *ra = (struct lagg_reqall *)data;
884 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
885 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
886 struct ifreq *ifr = (struct ifreq *)data;
887 struct lagg_port *lp;
889 struct thread *td = curthread;
891 int count, buflen, len, error = 0;
893 bzero(&rpbuf, sizeof(rpbuf));
899 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
901 buflen = count * sizeof(struct lagg_reqport);
904 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
907 ra->ra_proto = sc->sc_proto;
908 if (sc->sc_req != NULL)
909 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
913 len = min(ra->ra_size, buflen);
914 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
915 if (len < sizeof(rpbuf))
918 lagg_port2req(lp, &rpbuf);
919 memcpy(buf, &rpbuf, sizeof(rpbuf));
921 buf += sizeof(rpbuf);
922 len -= sizeof(rpbuf);
925 ra->ra_ports = count;
926 ra->ra_size = count * sizeof(rpbuf);
927 error = copyout(outbuf, ra->ra_port, ra->ra_size);
928 free(outbuf, M_TEMP);
931 error = priv_check(td, PRIV_NET_LAGG);
934 if (ra->ra_proto >= LAGG_PROTO_MAX) {
935 error = EPROTONOSUPPORT;
939 if (sc->sc_proto != LAGG_PROTO_NONE) {
940 /* Reset protocol first in case detach unlocks */
941 sc->sc_proto = LAGG_PROTO_NONE;
942 error = sc->sc_detach(sc);
943 sc->sc_detach = NULL;
946 sc->sc_port_create = NULL;
947 sc->sc_port_destroy = NULL;
948 sc->sc_linkstate = NULL;
951 sc->sc_lladdr = NULL;
953 sc->sc_portreq = NULL;
954 } else if (sc->sc_input != NULL) {
955 /* Still detaching */
962 for (int i = 0; i < (sizeof(lagg_protos) /
963 sizeof(lagg_protos[0])); i++) {
964 if (lagg_protos[i].ti_proto == ra->ra_proto) {
965 if (sc->sc_ifflags & IFF_DEBUG)
966 printf("%s: using proto %u\n",
968 lagg_protos[i].ti_proto);
969 sc->sc_proto = lagg_protos[i].ti_proto;
970 if (sc->sc_proto != LAGG_PROTO_NONE)
971 error = lagg_protos[i].ti_attach(sc);
977 error = EPROTONOSUPPORT;
980 rf->rf_flags = sc->sc_flags;
983 error = priv_check(td, PRIV_NET_LAGG);
986 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
991 sc->sc_flags &= ~LAGG_F_HASHMASK;
992 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
996 if (rp->rp_portname[0] == '\0' ||
997 (tpif = ifunit(rp->rp_portname)) == NULL) {
1003 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1004 lp->lp_softc != sc) {
1010 lagg_port2req(lp, rp);
1014 error = priv_check(td, PRIV_NET_LAGG);
1017 if (rp->rp_portname[0] == '\0' ||
1018 (tpif = ifunit(rp->rp_portname)) == NULL) {
1023 error = lagg_port_create(sc, tpif);
1026 case SIOCSLAGGDELPORT:
1027 error = priv_check(td, PRIV_NET_LAGG);
1030 if (rp->rp_portname[0] == '\0' ||
1031 (tpif = ifunit(rp->rp_portname)) == NULL) {
1037 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1038 lp->lp_softc != sc) {
1044 error = lagg_port_destroy(lp, 1);
1048 /* Set flags on ports too */
1050 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1051 lagg_setflags(lp, 1);
1055 if (!(ifp->if_flags & IFF_UP) &&
1056 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1058 * If interface is marked down and it is running,
1059 * then stop and disable it.
1064 } else if ((ifp->if_flags & IFF_UP) &&
1065 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1067 * If interface is marked up and it is stopped, then
1070 (*ifp->if_init)(sc);
1076 error = lagg_ether_setmulti(sc);
1081 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1086 /* Do not allow the MTU or caps to be directly changed */
1091 error = ether_ioctl(ifp, cmd, data);
1098 lagg_ether_setmulti(struct lagg_softc *sc)
1100 struct lagg_port *lp;
1102 LAGG_WLOCK_ASSERT(sc);
1104 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1105 /* First, remove any existing filter entries. */
1106 lagg_ether_cmdmulti(lp, 0);
1107 /* copy all addresses from the lagg interface to the port */
1108 lagg_ether_cmdmulti(lp, 1);
1114 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1116 struct lagg_softc *sc = lp->lp_softc;
1117 struct ifnet *ifp = lp->lp_ifp;
1118 struct ifnet *scifp = sc->sc_ifp;
1120 struct ifmultiaddr *ifma, *rifma = NULL;
1121 struct sockaddr_dl sdl;
1124 LAGG_WLOCK_ASSERT(sc);
1126 bzero((char *)&sdl, sizeof(sdl));
1127 sdl.sdl_len = sizeof(sdl);
1128 sdl.sdl_family = AF_LINK;
1129 sdl.sdl_type = IFT_ETHER;
1130 sdl.sdl_alen = ETHER_ADDR_LEN;
1131 sdl.sdl_index = ifp->if_index;
1134 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1135 if (ifma->ifma_addr->sa_family != AF_LINK)
1137 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1138 LLADDR(&sdl), ETHER_ADDR_LEN);
1140 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1143 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1146 mc->mc_ifma = rifma;
1147 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1150 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1151 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1152 if_delmulti_ifma(mc->mc_ifma);
1159 /* Handle a ref counted flag that should be set on the lagg port as well */
1161 lagg_setflag(struct lagg_port *lp, int flag, int status,
1162 int (*func)(struct ifnet *, int))
1164 struct lagg_softc *sc = lp->lp_softc;
1165 struct ifnet *scifp = sc->sc_ifp;
1166 struct ifnet *ifp = lp->lp_ifp;
1169 LAGG_WLOCK_ASSERT(sc);
1171 status = status ? (scifp->if_flags & flag) : 0;
1172 /* Now "status" contains the flag value or 0 */
1175 * See if recorded ports status is different from what
1176 * we want it to be. If it is, flip it. We record ports
1177 * status in lp_ifflags so that we won't clear ports flag
1178 * we haven't set. In fact, we don't clear or set ports
1179 * flags directly, but get or release references to them.
1180 * That's why we can be sure that recorded flags still are
1181 * in accord with actual ports flags.
1183 if (status != (lp->lp_ifflags & flag)) {
1184 error = (*func)(ifp, status);
1187 lp->lp_ifflags &= ~flag;
1188 lp->lp_ifflags |= status;
1194 * Handle IFF_* flags that require certain changes on the lagg port
1195 * if "status" is true, update ports flags respective to the lagg
1196 * if "status" is false, forcedly clear the flags set on port.
1199 lagg_setflags(struct lagg_port *lp, int status)
1203 for (i = 0; lagg_pflags[i].flag; i++) {
1204 error = lagg_setflag(lp, lagg_pflags[i].flag,
1205 status, lagg_pflags[i].func);
1213 lagg_start(struct ifnet *ifp)
1215 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1220 /* We need a Tx algorithm and at least one port */
1221 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1222 IF_DRAIN(&ifp->if_snd);
1227 for (;; error = 0) {
1228 IFQ_DEQUEUE(&ifp->if_snd, m);
1232 ETHER_BPF_MTAP(ifp, m);
1234 error = (*sc->sc_start)(sc, m);
1243 static struct mbuf *
1244 lagg_input(struct ifnet *ifp, struct mbuf *m)
1246 struct lagg_port *lp = ifp->if_lagg;
1247 struct lagg_softc *sc = lp->lp_softc;
1248 struct ifnet *scifp = sc->sc_ifp;
1251 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1252 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1253 sc->sc_proto == LAGG_PROTO_NONE) {
1259 ETHER_BPF_MTAP(scifp, m);
1261 m = (*sc->sc_input)(sc, lp, m);
1264 scifp->if_ipackets++;
1265 scifp->if_ibytes += m->m_pkthdr.len;
1267 if (scifp->if_flags & IFF_MONITOR) {
1278 lagg_media_change(struct ifnet *ifp)
1280 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1282 if (sc->sc_ifflags & IFF_DEBUG)
1283 printf("%s\n", __func__);
1290 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1292 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1293 struct lagg_port *lp;
1295 imr->ifm_status = IFM_AVALID;
1296 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1299 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1300 if (LAGG_PORTACTIVE(lp))
1301 imr->ifm_status |= IFM_ACTIVE;
1307 lagg_linkstate(struct lagg_softc *sc)
1309 struct lagg_port *lp;
1310 int new_link = LINK_STATE_DOWN;
1313 /* Our link is considered up if at least one of our ports is active */
1314 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1315 if (lp->lp_link_state == LINK_STATE_UP) {
1316 new_link = LINK_STATE_UP;
1320 if_link_state_change(sc->sc_ifp, new_link);
1322 /* Update if_baudrate to reflect the max possible speed */
1323 switch (sc->sc_proto) {
1324 case LAGG_PROTO_FAILOVER:
1325 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1326 sc->sc_primary->lp_ifp->if_baudrate : 0;
1328 case LAGG_PROTO_ROUNDROBIN:
1329 case LAGG_PROTO_LOADBALANCE:
1330 case LAGG_PROTO_ETHERCHANNEL:
1332 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1333 speed += lp->lp_ifp->if_baudrate;
1334 sc->sc_ifp->if_baudrate = speed;
1336 case LAGG_PROTO_LACP:
1337 /* LACP updates if_baudrate itself */
1343 lagg_port_state(struct ifnet *ifp, int state)
1345 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1346 struct lagg_softc *sc = NULL;
1355 if (sc->sc_linkstate != NULL)
1356 (*sc->sc_linkstate)(lp);
1361 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1363 struct lagg_port *lp_next, *rval = NULL;
1364 // int new_link = LINK_STATE_DOWN;
1366 LAGG_RLOCK_ASSERT(sc);
1368 * Search a port which reports an active link state.
1373 if (LAGG_PORTACTIVE(lp)) {
1377 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1378 LAGG_PORTACTIVE(lp_next)) {
1384 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1385 if (LAGG_PORTACTIVE(lp_next)) {
1394 * The IEEE 802.1D standard assumes that a lagg with
1395 * multiple ports is always full duplex. This is valid
1396 * for load sharing laggs and if at least two links
1397 * are active. Unfortunately, checking the latter would
1398 * be too expensive at this point.
1400 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1402 new_link = LINK_STATE_FULL_DUPLEX;
1404 new_link = rval->lp_link_state;
1412 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1414 if (m->m_pkthdr.len < (off + len)) {
1416 } else if (m->m_len < (off + len)) {
1417 m_copydata(m, off, len, buf);
1420 return (mtod(m, char *) + off);
1424 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1429 struct ether_header *eh;
1430 const struct ether_vlan_header *vlan;
1432 const struct ip *ip;
1433 const uint32_t *ports;
1437 const struct ip6_hdr *ip6;
1447 struct ether_vlan_header vlan;
1455 eh = mtod(m, struct ether_header *);
1456 etype = ntohs(eh->ether_type);
1457 if (sc->sc_flags & LAGG_F_HASHL2) {
1458 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1459 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1462 /* Special handling for encapsulating VLAN frames */
1463 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1464 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1465 sizeof(m->m_pkthdr.ether_vtag), p);
1466 } else if (etype == ETHERTYPE_VLAN) {
1467 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1471 if (sc->sc_flags & LAGG_F_HASHL2)
1472 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1473 etype = ntohs(vlan->evl_proto);
1474 off += sizeof(*vlan) - sizeof(*eh);
1480 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1484 if (sc->sc_flags & LAGG_F_HASHL3) {
1485 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1486 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1488 if (!(sc->sc_flags & LAGG_F_HASHL4))
1494 iphlen = ip->ip_hl << 2;
1495 if (iphlen < sizeof(*ip))
1498 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1501 p = hash32_buf(ports, sizeof(*ports), p);
1507 case ETHERTYPE_IPV6:
1508 if (!(sc->sc_flags & LAGG_F_HASHL3))
1510 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1514 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1515 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1516 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1517 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1526 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1529 return (ifp->if_transmit)(ifp, m);
1533 * Simple round robin aggregation
1537 lagg_rr_attach(struct lagg_softc *sc)
1539 sc->sc_detach = lagg_rr_detach;
1540 sc->sc_start = lagg_rr_start;
1541 sc->sc_input = lagg_rr_input;
1542 sc->sc_port_create = NULL;
1543 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1550 lagg_rr_detach(struct lagg_softc *sc)
1556 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1558 struct lagg_port *lp;
1561 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1563 lp = SLIST_FIRST(&sc->sc_ports);
1565 lp = SLIST_NEXT(lp, lp_entries);
1568 * Check the port's link state. This will return the next active
1569 * port if the link is down or the port is NULL.
1571 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1577 return (lagg_enqueue(lp->lp_ifp, m));
1580 static struct mbuf *
1581 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1583 struct ifnet *ifp = sc->sc_ifp;
1585 /* Just pass in the packet to our lagg device */
1586 m->m_pkthdr.rcvif = ifp;
1596 lagg_fail_attach(struct lagg_softc *sc)
1598 sc->sc_detach = lagg_fail_detach;
1599 sc->sc_start = lagg_fail_start;
1600 sc->sc_input = lagg_fail_input;
1601 sc->sc_port_create = NULL;
1602 sc->sc_port_destroy = NULL;
1608 lagg_fail_detach(struct lagg_softc *sc)
1614 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1616 struct lagg_port *lp;
1618 /* Use the master port if active or the next available port */
1619 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1625 return (lagg_enqueue(lp->lp_ifp, m));
1628 static struct mbuf *
1629 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1631 struct ifnet *ifp = sc->sc_ifp;
1632 struct lagg_port *tmp_tp;
1634 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1635 m->m_pkthdr.rcvif = ifp;
1639 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1640 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1642 * If tmp_tp is null, we've recieved a packet when all
1643 * our links are down. Weird, but process it anyways.
1645 if ((tmp_tp == NULL || tmp_tp == lp)) {
1646 m->m_pkthdr.rcvif = ifp;
1660 lagg_lb_attach(struct lagg_softc *sc)
1662 struct lagg_port *lp;
1665 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1666 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1669 sc->sc_detach = lagg_lb_detach;
1670 sc->sc_start = lagg_lb_start;
1671 sc->sc_input = lagg_lb_input;
1672 sc->sc_port_create = lagg_lb_port_create;
1673 sc->sc_port_destroy = lagg_lb_port_destroy;
1674 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1676 lb->lb_key = arc4random();
1677 sc->sc_psc = (caddr_t)lb;
1679 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1680 lagg_lb_port_create(lp);
1686 lagg_lb_detach(struct lagg_softc *sc)
1688 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1695 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1697 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1698 struct lagg_port *lp_next;
1701 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1702 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1705 if (i >= LAGG_MAX_PORTS)
1707 if (sc->sc_ifflags & IFF_DEBUG)
1708 printf("%s: port %s at index %d\n",
1709 sc->sc_ifname, lp_next->lp_ifname, i);
1710 lb->lb_ports[i++] = lp_next;
1717 lagg_lb_port_create(struct lagg_port *lp)
1719 struct lagg_softc *sc = lp->lp_softc;
1720 return (lagg_lb_porttable(sc, NULL));
1724 lagg_lb_port_destroy(struct lagg_port *lp)
1726 struct lagg_softc *sc = lp->lp_softc;
1727 lagg_lb_porttable(sc, lp);
1731 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1733 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1734 struct lagg_port *lp = NULL;
1737 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1738 p = m->m_pkthdr.flowid;
1740 p = lagg_hashmbuf(sc, m, lb->lb_key);
1742 lp = lb->lb_ports[p];
1745 * Check the port's link state. This will return the next active
1746 * port if the link is down or the port is NULL.
1748 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1754 return (lagg_enqueue(lp->lp_ifp, m));
1757 static struct mbuf *
1758 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1760 struct ifnet *ifp = sc->sc_ifp;
1762 /* Just pass in the packet to our lagg device */
1763 m->m_pkthdr.rcvif = ifp;
1773 lagg_lacp_attach(struct lagg_softc *sc)
1775 struct lagg_port *lp;
1778 sc->sc_detach = lagg_lacp_detach;
1779 sc->sc_port_create = lacp_port_create;
1780 sc->sc_port_destroy = lacp_port_destroy;
1781 sc->sc_linkstate = lacp_linkstate;
1782 sc->sc_start = lagg_lacp_start;
1783 sc->sc_input = lagg_lacp_input;
1784 sc->sc_init = lacp_init;
1785 sc->sc_stop = lacp_stop;
1786 sc->sc_lladdr = lagg_lacp_lladdr;
1787 sc->sc_req = lacp_req;
1788 sc->sc_portreq = lacp_portreq;
1790 error = lacp_attach(sc);
1794 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1795 lacp_port_create(lp);
1801 lagg_lacp_detach(struct lagg_softc *sc)
1803 struct lagg_port *lp;
1806 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1807 lacp_port_destroy(lp);
1809 /* unlocking is safe here */
1811 error = lacp_detach(sc);
1818 lagg_lacp_lladdr(struct lagg_softc *sc)
1820 struct lagg_port *lp;
1822 /* purge all the lacp ports */
1823 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1824 lacp_port_destroy(lp);
1826 /* add them back in */
1827 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1828 lacp_port_create(lp);
1832 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1834 struct lagg_port *lp;
1836 lp = lacp_select_tx_port(sc, m);
1843 return (lagg_enqueue(lp->lp_ifp, m));
1846 static struct mbuf *
1847 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1849 struct ifnet *ifp = sc->sc_ifp;
1850 struct ether_header *eh;
1853 eh = mtod(m, struct ether_header *);
1854 etype = ntohs(eh->ether_type);
1856 /* Tap off LACP control messages */
1857 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1858 m = lacp_input(lp, m);
1864 * If the port is not collecting or not in the active aggregator then
1867 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1872 m->m_pkthdr.rcvif = ifp;