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 static SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
172 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
173 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
174 &lagg_failover_rx_all, 0,
175 "Accept input from any interface in a failover lagg");
176 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
177 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
178 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
180 "Default setting for using flow id for load sharing");
183 lagg_modevent(module_t mod, int type, void *data)
188 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
189 SLIST_INIT(&lagg_list);
190 if_clone_attach(&lagg_cloner);
191 lagg_input_p = lagg_input;
192 lagg_linkstate_p = lagg_port_state;
193 lagg_detach_cookie = EVENTHANDLER_REGISTER(
194 ifnet_departure_event, lagg_port_ifdetach, NULL,
195 EVENTHANDLER_PRI_ANY);
198 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
200 if_clone_detach(&lagg_cloner);
202 lagg_linkstate_p = NULL;
203 mtx_destroy(&lagg_list_mtx);
211 static moduledata_t lagg_mod = {
217 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
218 MODULE_VERSION(if_lagg, 1);
220 #if __FreeBSD_version >= 800000
222 * This routine is run via an vlan
226 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
228 struct lagg_softc *sc = ifp->if_softc;
229 struct lagg_port *lp;
231 if (ifp->if_softc != arg) /* Not our event */
235 if (!SLIST_EMPTY(&sc->sc_ports)) {
236 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
237 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
243 * This routine is run via an vlan
247 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
249 struct lagg_softc *sc = ifp->if_softc;
250 struct lagg_port *lp;
252 if (ifp->if_softc != arg) /* Not our event */
256 if (!SLIST_EMPTY(&sc->sc_ports)) {
257 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
258 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
265 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
267 struct lagg_softc *sc;
270 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
271 struct sysctl_oid *oid;
272 char num[14]; /* sufficient for 32 bits */
274 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
275 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
281 sysctl_ctx_init(&sc->ctx);
282 snprintf(num, sizeof(num), "%u", unit);
283 sc->use_flowid = def_use_flowid;
284 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
285 OID_AUTO, num, CTLFLAG_RD, NULL, "");
286 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
287 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
288 "Use flow id for load sharing");
289 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
290 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
291 "Total number of ports");
292 /* Hash all layers by default */
293 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
295 sc->sc_proto = LAGG_PROTO_NONE;
296 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
297 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
298 sc->sc_proto = lagg_protos[i].ti_proto;
299 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
300 if_free_type(ifp, IFT_ETHER);
308 SLIST_INIT(&sc->sc_ports);
309 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
311 /* Initialise pseudo media types */
312 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
314 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
315 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
317 if_initname(ifp, ifc->ifc_name, unit);
318 ifp->if_type = IFT_ETHER;
320 ifp->if_transmit = lagg_transmit;
321 ifp->if_qflush = lagg_qflush;
322 ifp->if_init = lagg_init;
323 ifp->if_ioctl = lagg_ioctl;
324 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
327 * Attach as an ordinary ethernet device, childs will be attached
328 * as special device IFT_IEEE8023ADLAG.
330 ether_ifattach(ifp, eaddr);
332 #if __FreeBSD_version >= 800000
333 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
334 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
335 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
336 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
339 /* Insert into the global list of laggs */
340 mtx_lock(&lagg_list_mtx);
341 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
342 mtx_unlock(&lagg_list_mtx);
348 lagg_clone_destroy(struct ifnet *ifp)
350 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
351 struct lagg_port *lp;
356 ifp->if_flags &= ~IFF_UP;
358 #if __FreeBSD_version >= 800000
359 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
360 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
363 /* Shutdown and remove lagg ports */
364 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
365 lagg_port_destroy(lp, 1);
366 /* Unhook the aggregation protocol */
367 if (sc->sc_detach != NULL)
368 (*sc->sc_detach)(sc);
372 sysctl_ctx_free(&sc->ctx);
373 ifmedia_removeall(&sc->sc_media);
375 if_free_type(ifp, IFT_ETHER);
377 mtx_lock(&lagg_list_mtx);
378 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
379 mtx_unlock(&lagg_list_mtx);
381 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
382 LAGG_LOCK_DESTROY(sc);
387 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
389 struct ifnet *ifp = sc->sc_ifp;
391 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
394 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
395 /* Let the protocol know the MAC has changed */
396 if (sc->sc_lladdr != NULL)
397 (*sc->sc_lladdr)(sc);
398 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
402 lagg_capabilities(struct lagg_softc *sc)
404 struct lagg_port *lp;
405 int cap = ~0, ena = ~0;
408 LAGG_WLOCK_ASSERT(sc);
410 /* Get capabilities from the lagg ports */
411 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
412 cap &= lp->lp_ifp->if_capabilities;
413 ena &= lp->lp_ifp->if_capenable;
414 hwa &= lp->lp_ifp->if_hwassist;
416 cap = (cap == ~0 ? 0 : cap);
417 ena = (ena == ~0 ? 0 : ena);
418 hwa = (hwa == ~0 ? 0 : hwa);
420 if (sc->sc_ifp->if_capabilities != cap ||
421 sc->sc_ifp->if_capenable != ena ||
422 sc->sc_ifp->if_hwassist != hwa) {
423 sc->sc_ifp->if_capabilities = cap;
424 sc->sc_ifp->if_capenable = ena;
425 sc->sc_ifp->if_hwassist = hwa;
426 getmicrotime(&sc->sc_ifp->if_lastchange);
428 if (sc->sc_ifflags & IFF_DEBUG)
429 if_printf(sc->sc_ifp,
430 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
435 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
437 struct lagg_softc *sc = lp->lp_softc;
438 struct ifnet *ifp = lp->lp_ifp;
439 struct lagg_llq *llq;
442 LAGG_WLOCK_ASSERT(sc);
444 if (lp->lp_detaching ||
445 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
448 /* Check to make sure its not already queued to be changed */
449 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
450 if (llq->llq_ifp == ifp) {
457 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
458 if (llq == NULL) /* XXX what to do */
462 /* Update the lladdr even if pending, it may have changed */
464 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
467 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
469 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
473 * Set the interface MAC address from a taskqueue to avoid a LOR.
476 lagg_port_setlladdr(void *arg, int pending)
478 struct lagg_softc *sc = (struct lagg_softc *)arg;
479 struct lagg_llq *llq, *head;
483 /* Grab a local reference of the queue and remove it from the softc */
485 head = SLIST_FIRST(&sc->sc_llq_head);
486 SLIST_FIRST(&sc->sc_llq_head) = NULL;
490 * Traverse the queue and set the lladdr on each ifp. It is safe to do
491 * unlocked as we have the only reference to it.
493 for (llq = head; llq != NULL; llq = head) {
496 /* Set the link layer address */
497 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
499 printf("%s: setlladdr failed on %s\n", __func__,
502 head = SLIST_NEXT(llq, llq_entries);
508 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
510 struct lagg_softc *sc_ptr;
511 struct lagg_port *lp;
514 LAGG_WLOCK_ASSERT(sc);
516 /* Limit the maximal number of lagg ports */
517 if (sc->sc_count >= LAGG_MAX_PORTS)
520 /* Check if port has already been associated to a lagg */
521 if (ifp->if_lagg != NULL) {
522 /* Port is already in the current lagg? */
523 lp = (struct lagg_port *)ifp->if_lagg;
524 if (lp->lp_softc == sc)
529 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
530 if (ifp->if_type != IFT_ETHER)
531 return (EPROTONOSUPPORT);
533 /* Allow the first Ethernet member to define the MTU */
534 if (SLIST_EMPTY(&sc->sc_ports))
535 sc->sc_ifp->if_mtu = ifp->if_mtu;
536 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
537 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
542 if ((lp = malloc(sizeof(struct lagg_port),
543 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
546 /* Check if port is a stacked lagg */
547 mtx_lock(&lagg_list_mtx);
548 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
549 if (ifp == sc_ptr->sc_ifp) {
550 mtx_unlock(&lagg_list_mtx);
553 /* XXX disable stacking for the moment, its untested */
554 #ifdef LAGG_PORT_STACKING
555 lp->lp_flags |= LAGG_PORT_STACK;
556 if (lagg_port_checkstacking(sc_ptr) >=
558 mtx_unlock(&lagg_list_mtx);
565 mtx_unlock(&lagg_list_mtx);
567 /* Change the interface type */
568 lp->lp_iftype = ifp->if_type;
569 ifp->if_type = IFT_IEEE8023ADLAG;
571 lp->lp_ioctl = ifp->if_ioctl;
572 ifp->if_ioctl = lagg_port_ioctl;
573 lp->lp_output = ifp->if_output;
574 ifp->if_output = lagg_port_output;
579 /* Save port link layer address */
580 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
582 if (SLIST_EMPTY(&sc->sc_ports)) {
584 lagg_lladdr(sc, IF_LLADDR(ifp));
586 /* Update link layer address for this port */
587 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
590 /* Insert into the list of ports */
591 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
594 /* Update lagg capabilities */
595 lagg_capabilities(sc);
598 /* Add multicast addresses and interface flags to this port */
599 lagg_ether_cmdmulti(lp, 1);
600 lagg_setflags(lp, 1);
602 if (sc->sc_port_create != NULL)
603 error = (*sc->sc_port_create)(lp);
605 /* remove the port again, without calling sc_port_destroy */
606 lagg_port_destroy(lp, 0);
613 #ifdef LAGG_PORT_STACKING
615 lagg_port_checkstacking(struct lagg_softc *sc)
617 struct lagg_softc *sc_ptr;
618 struct lagg_port *lp;
621 LAGG_WLOCK_ASSERT(sc);
623 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
624 if (lp->lp_flags & LAGG_PORT_STACK) {
625 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
626 m = MAX(m, lagg_port_checkstacking(sc_ptr));
635 lagg_port_destroy(struct lagg_port *lp, int runpd)
637 struct lagg_softc *sc = lp->lp_softc;
638 struct lagg_port *lp_ptr;
639 struct lagg_llq *llq;
640 struct ifnet *ifp = lp->lp_ifp;
642 LAGG_WLOCK_ASSERT(sc);
644 if (runpd && sc->sc_port_destroy != NULL)
645 (*sc->sc_port_destroy)(lp);
648 * Remove multicast addresses and interface flags from this port and
649 * reset the MAC address, skip if the interface is being detached.
651 if (!lp->lp_detaching) {
652 lagg_ether_cmdmulti(lp, 0);
653 lagg_setflags(lp, 0);
654 lagg_port_lladdr(lp, lp->lp_lladdr);
657 /* Restore interface */
658 ifp->if_type = lp->lp_iftype;
659 ifp->if_ioctl = lp->lp_ioctl;
660 ifp->if_output = lp->lp_output;
663 /* Finally, remove the port from the lagg */
664 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
667 /* Update the primary interface */
668 if (lp == sc->sc_primary) {
669 uint8_t lladdr[ETHER_ADDR_LEN];
671 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
672 bzero(&lladdr, ETHER_ADDR_LEN);
674 bcopy(lp_ptr->lp_lladdr,
675 lladdr, ETHER_ADDR_LEN);
677 lagg_lladdr(sc, lladdr);
678 sc->sc_primary = lp_ptr;
680 /* Update link layer address for each port */
681 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
682 lagg_port_lladdr(lp_ptr, lladdr);
685 /* Remove any pending lladdr changes from the queue */
686 if (lp->lp_detaching) {
687 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
688 if (llq->llq_ifp == ifp) {
689 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
692 break; /* Only appears once */
698 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
702 /* Update lagg capabilities */
703 lagg_capabilities(sc);
710 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
712 struct lagg_reqport *rp = (struct lagg_reqport *)data;
713 struct lagg_softc *sc;
714 struct lagg_port *lp = NULL;
717 /* Should be checked by the caller */
718 if (ifp->if_type != IFT_IEEE8023ADLAG ||
719 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
724 if (rp->rp_portname[0] == '\0' ||
725 ifunit(rp->rp_portname) != ifp) {
731 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
737 lagg_port2req(lp, rp);
742 if (lp->lp_ioctl == NULL) {
746 error = (*lp->lp_ioctl)(ifp, cmd, data);
750 /* Update lagg interface capabilities */
752 lagg_capabilities(sc);
757 /* Do not allow the MTU to be changed once joined */
768 if (lp->lp_ioctl != NULL)
769 return ((*lp->lp_ioctl)(ifp, cmd, data));
775 * For direct output to child ports.
778 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
779 struct sockaddr *dst, struct route *ro)
781 struct lagg_port *lp = ifp->if_lagg;
783 switch (dst->sa_family) {
784 case pseudo_AF_HDRCMPLT:
786 return ((*lp->lp_output)(ifp, m, dst, ro));
789 /* drop any other frames */
795 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
797 struct lagg_port *lp;
798 struct lagg_softc *sc;
800 if ((lp = ifp->if_lagg) == NULL)
802 /* If the ifnet is just being renamed, don't do anything. */
803 if (ifp->if_flags & IFF_RENAMING)
809 lp->lp_detaching = 1;
810 lagg_port_destroy(lp, 1);
815 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
817 struct lagg_softc *sc = lp->lp_softc;
819 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
820 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
821 rp->rp_prio = lp->lp_prio;
822 rp->rp_flags = lp->lp_flags;
823 if (sc->sc_portreq != NULL)
824 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
826 /* Add protocol specific flags */
827 switch (sc->sc_proto) {
828 case LAGG_PROTO_FAILOVER:
829 if (lp == sc->sc_primary)
830 rp->rp_flags |= LAGG_PORT_MASTER;
831 if (lp == lagg_link_active(sc, sc->sc_primary))
832 rp->rp_flags |= LAGG_PORT_ACTIVE;
835 case LAGG_PROTO_ROUNDROBIN:
836 case LAGG_PROTO_LOADBALANCE:
837 case LAGG_PROTO_ETHERCHANNEL:
838 if (LAGG_PORTACTIVE(lp))
839 rp->rp_flags |= LAGG_PORT_ACTIVE;
842 case LAGG_PROTO_LACP:
843 /* LACP has a different definition of active */
844 if (lacp_isactive(lp))
845 rp->rp_flags |= LAGG_PORT_ACTIVE;
846 if (lacp_iscollecting(lp))
847 rp->rp_flags |= LAGG_PORT_COLLECTING;
848 if (lacp_isdistributing(lp))
849 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
858 struct lagg_softc *sc = (struct lagg_softc *)xsc;
859 struct lagg_port *lp;
860 struct ifnet *ifp = sc->sc_ifp;
862 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
867 ifp->if_drv_flags |= IFF_DRV_RUNNING;
868 /* Update the port lladdrs */
869 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
870 lagg_port_lladdr(lp, IF_LLADDR(ifp));
872 if (sc->sc_init != NULL)
879 lagg_stop(struct lagg_softc *sc)
881 struct ifnet *ifp = sc->sc_ifp;
883 LAGG_WLOCK_ASSERT(sc);
885 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
888 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
890 if (sc->sc_stop != NULL)
895 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
897 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
898 struct lagg_reqall *ra = (struct lagg_reqall *)data;
899 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
900 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
901 struct ifreq *ifr = (struct ifreq *)data;
902 struct lagg_port *lp;
904 struct thread *td = curthread;
906 int count, buflen, len, error = 0;
908 bzero(&rpbuf, sizeof(rpbuf));
914 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
916 buflen = count * sizeof(struct lagg_reqport);
919 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
922 ra->ra_proto = sc->sc_proto;
923 if (sc->sc_req != NULL)
924 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
928 len = min(ra->ra_size, buflen);
929 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
930 if (len < sizeof(rpbuf))
933 lagg_port2req(lp, &rpbuf);
934 memcpy(buf, &rpbuf, sizeof(rpbuf));
936 buf += sizeof(rpbuf);
937 len -= sizeof(rpbuf);
940 ra->ra_ports = count;
941 ra->ra_size = count * sizeof(rpbuf);
942 error = copyout(outbuf, ra->ra_port, ra->ra_size);
943 free(outbuf, M_TEMP);
946 error = priv_check(td, PRIV_NET_LAGG);
949 if (ra->ra_proto >= LAGG_PROTO_MAX) {
950 error = EPROTONOSUPPORT;
954 if (sc->sc_proto != LAGG_PROTO_NONE) {
955 /* Reset protocol first in case detach unlocks */
956 sc->sc_proto = LAGG_PROTO_NONE;
957 error = sc->sc_detach(sc);
958 sc->sc_detach = NULL;
961 sc->sc_port_create = NULL;
962 sc->sc_port_destroy = NULL;
963 sc->sc_linkstate = NULL;
966 sc->sc_lladdr = NULL;
968 sc->sc_portreq = NULL;
969 } else if (sc->sc_input != NULL) {
970 /* Still detaching */
977 for (int i = 0; i < (sizeof(lagg_protos) /
978 sizeof(lagg_protos[0])); i++) {
979 if (lagg_protos[i].ti_proto == ra->ra_proto) {
980 if (sc->sc_ifflags & IFF_DEBUG)
981 printf("%s: using proto %u\n",
983 lagg_protos[i].ti_proto);
984 sc->sc_proto = lagg_protos[i].ti_proto;
985 if (sc->sc_proto != LAGG_PROTO_NONE)
986 error = lagg_protos[i].ti_attach(sc);
992 error = EPROTONOSUPPORT;
995 rf->rf_flags = sc->sc_flags;
998 error = priv_check(td, PRIV_NET_LAGG);
1001 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1006 sc->sc_flags &= ~LAGG_F_HASHMASK;
1007 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1011 if (rp->rp_portname[0] == '\0' ||
1012 (tpif = ifunit(rp->rp_portname)) == NULL) {
1018 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1019 lp->lp_softc != sc) {
1025 lagg_port2req(lp, rp);
1029 error = priv_check(td, PRIV_NET_LAGG);
1032 if (rp->rp_portname[0] == '\0' ||
1033 (tpif = ifunit(rp->rp_portname)) == NULL) {
1038 error = lagg_port_create(sc, tpif);
1041 case SIOCSLAGGDELPORT:
1042 error = priv_check(td, PRIV_NET_LAGG);
1045 if (rp->rp_portname[0] == '\0' ||
1046 (tpif = ifunit(rp->rp_portname)) == NULL) {
1052 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1053 lp->lp_softc != sc) {
1059 error = lagg_port_destroy(lp, 1);
1063 /* Set flags on ports too */
1065 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1066 lagg_setflags(lp, 1);
1070 if (!(ifp->if_flags & IFF_UP) &&
1071 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1073 * If interface is marked down and it is running,
1074 * then stop and disable it.
1079 } else if ((ifp->if_flags & IFF_UP) &&
1080 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1082 * If interface is marked up and it is stopped, then
1085 (*ifp->if_init)(sc);
1091 error = lagg_ether_setmulti(sc);
1096 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1101 /* Do not allow the MTU or caps to be directly changed */
1106 error = ether_ioctl(ifp, cmd, data);
1113 lagg_ether_setmulti(struct lagg_softc *sc)
1115 struct lagg_port *lp;
1117 LAGG_WLOCK_ASSERT(sc);
1119 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1120 /* First, remove any existing filter entries. */
1121 lagg_ether_cmdmulti(lp, 0);
1122 /* copy all addresses from the lagg interface to the port */
1123 lagg_ether_cmdmulti(lp, 1);
1129 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1131 struct lagg_softc *sc = lp->lp_softc;
1132 struct ifnet *ifp = lp->lp_ifp;
1133 struct ifnet *scifp = sc->sc_ifp;
1135 struct ifmultiaddr *ifma, *rifma = NULL;
1136 struct sockaddr_dl sdl;
1139 LAGG_WLOCK_ASSERT(sc);
1141 bzero((char *)&sdl, sizeof(sdl));
1142 sdl.sdl_len = sizeof(sdl);
1143 sdl.sdl_family = AF_LINK;
1144 sdl.sdl_type = IFT_ETHER;
1145 sdl.sdl_alen = ETHER_ADDR_LEN;
1146 sdl.sdl_index = ifp->if_index;
1149 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1150 if (ifma->ifma_addr->sa_family != AF_LINK)
1152 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1153 LLADDR(&sdl), ETHER_ADDR_LEN);
1155 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1158 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1161 mc->mc_ifma = rifma;
1162 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1165 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1166 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1167 if_delmulti_ifma(mc->mc_ifma);
1174 /* Handle a ref counted flag that should be set on the lagg port as well */
1176 lagg_setflag(struct lagg_port *lp, int flag, int status,
1177 int (*func)(struct ifnet *, int))
1179 struct lagg_softc *sc = lp->lp_softc;
1180 struct ifnet *scifp = sc->sc_ifp;
1181 struct ifnet *ifp = lp->lp_ifp;
1184 LAGG_WLOCK_ASSERT(sc);
1186 status = status ? (scifp->if_flags & flag) : 0;
1187 /* Now "status" contains the flag value or 0 */
1190 * See if recorded ports status is different from what
1191 * we want it to be. If it is, flip it. We record ports
1192 * status in lp_ifflags so that we won't clear ports flag
1193 * we haven't set. In fact, we don't clear or set ports
1194 * flags directly, but get or release references to them.
1195 * That's why we can be sure that recorded flags still are
1196 * in accord with actual ports flags.
1198 if (status != (lp->lp_ifflags & flag)) {
1199 error = (*func)(ifp, status);
1202 lp->lp_ifflags &= ~flag;
1203 lp->lp_ifflags |= status;
1209 * Handle IFF_* flags that require certain changes on the lagg port
1210 * if "status" is true, update ports flags respective to the lagg
1211 * if "status" is false, forcedly clear the flags set on port.
1214 lagg_setflags(struct lagg_port *lp, int status)
1218 for (i = 0; lagg_pflags[i].flag; i++) {
1219 error = lagg_setflag(lp, lagg_pflags[i].flag,
1220 status, lagg_pflags[i].func);
1228 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1230 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1231 int error, len, mcast;
1233 len = m->m_pkthdr.len;
1234 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1237 /* We need a Tx algorithm and at least one port */
1238 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1245 ETHER_BPF_MTAP(ifp, m);
1247 error = (*sc->sc_start)(sc, m);
1252 ifp->if_omcasts += mcast;
1253 ifp->if_obytes += len;
1261 * The ifp->if_qflush entry point for lagg(4) is no-op.
1264 lagg_qflush(struct ifnet *ifp __unused)
1268 static struct mbuf *
1269 lagg_input(struct ifnet *ifp, struct mbuf *m)
1271 struct lagg_port *lp = ifp->if_lagg;
1272 struct lagg_softc *sc = lp->lp_softc;
1273 struct ifnet *scifp = sc->sc_ifp;
1276 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1277 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1278 sc->sc_proto == LAGG_PROTO_NONE) {
1284 ETHER_BPF_MTAP(scifp, m);
1286 m = (*sc->sc_input)(sc, lp, m);
1289 scifp->if_ipackets++;
1290 scifp->if_ibytes += m->m_pkthdr.len;
1292 if (scifp->if_flags & IFF_MONITOR) {
1303 lagg_media_change(struct ifnet *ifp)
1305 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1307 if (sc->sc_ifflags & IFF_DEBUG)
1308 printf("%s\n", __func__);
1315 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1317 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1318 struct lagg_port *lp;
1320 imr->ifm_status = IFM_AVALID;
1321 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1324 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1325 if (LAGG_PORTACTIVE(lp))
1326 imr->ifm_status |= IFM_ACTIVE;
1332 lagg_linkstate(struct lagg_softc *sc)
1334 struct lagg_port *lp;
1335 int new_link = LINK_STATE_DOWN;
1338 /* Our link is considered up if at least one of our ports is active */
1339 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1340 if (lp->lp_link_state == LINK_STATE_UP) {
1341 new_link = LINK_STATE_UP;
1345 if_link_state_change(sc->sc_ifp, new_link);
1347 /* Update if_baudrate to reflect the max possible speed */
1348 switch (sc->sc_proto) {
1349 case LAGG_PROTO_FAILOVER:
1350 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1351 sc->sc_primary->lp_ifp->if_baudrate : 0;
1353 case LAGG_PROTO_ROUNDROBIN:
1354 case LAGG_PROTO_LOADBALANCE:
1355 case LAGG_PROTO_ETHERCHANNEL:
1357 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1358 speed += lp->lp_ifp->if_baudrate;
1359 sc->sc_ifp->if_baudrate = speed;
1361 case LAGG_PROTO_LACP:
1362 /* LACP updates if_baudrate itself */
1368 lagg_port_state(struct ifnet *ifp, int state)
1370 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1371 struct lagg_softc *sc = NULL;
1380 if (sc->sc_linkstate != NULL)
1381 (*sc->sc_linkstate)(lp);
1386 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1388 struct lagg_port *lp_next, *rval = NULL;
1389 // int new_link = LINK_STATE_DOWN;
1391 LAGG_RLOCK_ASSERT(sc);
1393 * Search a port which reports an active link state.
1398 if (LAGG_PORTACTIVE(lp)) {
1402 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1403 LAGG_PORTACTIVE(lp_next)) {
1409 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1410 if (LAGG_PORTACTIVE(lp_next)) {
1419 * The IEEE 802.1D standard assumes that a lagg with
1420 * multiple ports is always full duplex. This is valid
1421 * for load sharing laggs and if at least two links
1422 * are active. Unfortunately, checking the latter would
1423 * be too expensive at this point.
1425 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1427 new_link = LINK_STATE_FULL_DUPLEX;
1429 new_link = rval->lp_link_state;
1437 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1439 if (m->m_pkthdr.len < (off + len)) {
1441 } else if (m->m_len < (off + len)) {
1442 m_copydata(m, off, len, buf);
1445 return (mtod(m, char *) + off);
1449 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1454 struct ether_header *eh;
1455 const struct ether_vlan_header *vlan;
1457 const struct ip *ip;
1458 const uint32_t *ports;
1462 const struct ip6_hdr *ip6;
1472 struct ether_vlan_header vlan;
1480 eh = mtod(m, struct ether_header *);
1481 etype = ntohs(eh->ether_type);
1482 if (sc->sc_flags & LAGG_F_HASHL2) {
1483 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1484 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1487 /* Special handling for encapsulating VLAN frames */
1488 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1489 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1490 sizeof(m->m_pkthdr.ether_vtag), p);
1491 } else if (etype == ETHERTYPE_VLAN) {
1492 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1496 if (sc->sc_flags & LAGG_F_HASHL2)
1497 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1498 etype = ntohs(vlan->evl_proto);
1499 off += sizeof(*vlan) - sizeof(*eh);
1505 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1509 if (sc->sc_flags & LAGG_F_HASHL3) {
1510 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1511 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1513 if (!(sc->sc_flags & LAGG_F_HASHL4))
1519 iphlen = ip->ip_hl << 2;
1520 if (iphlen < sizeof(*ip))
1523 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1526 p = hash32_buf(ports, sizeof(*ports), p);
1532 case ETHERTYPE_IPV6:
1533 if (!(sc->sc_flags & LAGG_F_HASHL3))
1535 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1539 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1540 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1541 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1542 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1551 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1554 return (ifp->if_transmit)(ifp, m);
1558 * Simple round robin aggregation
1562 lagg_rr_attach(struct lagg_softc *sc)
1564 sc->sc_detach = lagg_rr_detach;
1565 sc->sc_start = lagg_rr_start;
1566 sc->sc_input = lagg_rr_input;
1567 sc->sc_port_create = NULL;
1568 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1575 lagg_rr_detach(struct lagg_softc *sc)
1581 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1583 struct lagg_port *lp;
1586 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1588 lp = SLIST_FIRST(&sc->sc_ports);
1590 lp = SLIST_NEXT(lp, lp_entries);
1593 * Check the port's link state. This will return the next active
1594 * port if the link is down or the port is NULL.
1596 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1602 return (lagg_enqueue(lp->lp_ifp, m));
1605 static struct mbuf *
1606 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1608 struct ifnet *ifp = sc->sc_ifp;
1610 /* Just pass in the packet to our lagg device */
1611 m->m_pkthdr.rcvif = ifp;
1621 lagg_fail_attach(struct lagg_softc *sc)
1623 sc->sc_detach = lagg_fail_detach;
1624 sc->sc_start = lagg_fail_start;
1625 sc->sc_input = lagg_fail_input;
1626 sc->sc_port_create = NULL;
1627 sc->sc_port_destroy = NULL;
1633 lagg_fail_detach(struct lagg_softc *sc)
1639 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1641 struct lagg_port *lp;
1643 /* Use the master port if active or the next available port */
1644 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1650 return (lagg_enqueue(lp->lp_ifp, m));
1653 static struct mbuf *
1654 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1656 struct ifnet *ifp = sc->sc_ifp;
1657 struct lagg_port *tmp_tp;
1659 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1660 m->m_pkthdr.rcvif = ifp;
1664 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1665 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1667 * If tmp_tp is null, we've recieved a packet when all
1668 * our links are down. Weird, but process it anyways.
1670 if ((tmp_tp == NULL || tmp_tp == lp)) {
1671 m->m_pkthdr.rcvif = ifp;
1685 lagg_lb_attach(struct lagg_softc *sc)
1687 struct lagg_port *lp;
1690 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1691 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1694 sc->sc_detach = lagg_lb_detach;
1695 sc->sc_start = lagg_lb_start;
1696 sc->sc_input = lagg_lb_input;
1697 sc->sc_port_create = lagg_lb_port_create;
1698 sc->sc_port_destroy = lagg_lb_port_destroy;
1699 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1701 lb->lb_key = arc4random();
1702 sc->sc_psc = (caddr_t)lb;
1704 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1705 lagg_lb_port_create(lp);
1711 lagg_lb_detach(struct lagg_softc *sc)
1713 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1720 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1722 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1723 struct lagg_port *lp_next;
1726 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1727 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1730 if (i >= LAGG_MAX_PORTS)
1732 if (sc->sc_ifflags & IFF_DEBUG)
1733 printf("%s: port %s at index %d\n",
1734 sc->sc_ifname, lp_next->lp_ifname, i);
1735 lb->lb_ports[i++] = lp_next;
1742 lagg_lb_port_create(struct lagg_port *lp)
1744 struct lagg_softc *sc = lp->lp_softc;
1745 return (lagg_lb_porttable(sc, NULL));
1749 lagg_lb_port_destroy(struct lagg_port *lp)
1751 struct lagg_softc *sc = lp->lp_softc;
1752 lagg_lb_porttable(sc, lp);
1756 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1758 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1759 struct lagg_port *lp = NULL;
1762 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1763 p = m->m_pkthdr.flowid;
1765 p = lagg_hashmbuf(sc, m, lb->lb_key);
1767 lp = lb->lb_ports[p];
1770 * Check the port's link state. This will return the next active
1771 * port if the link is down or the port is NULL.
1773 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1779 return (lagg_enqueue(lp->lp_ifp, m));
1782 static struct mbuf *
1783 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1785 struct ifnet *ifp = sc->sc_ifp;
1787 /* Just pass in the packet to our lagg device */
1788 m->m_pkthdr.rcvif = ifp;
1798 lagg_lacp_attach(struct lagg_softc *sc)
1800 struct lagg_port *lp;
1803 sc->sc_detach = lagg_lacp_detach;
1804 sc->sc_port_create = lacp_port_create;
1805 sc->sc_port_destroy = lacp_port_destroy;
1806 sc->sc_linkstate = lacp_linkstate;
1807 sc->sc_start = lagg_lacp_start;
1808 sc->sc_input = lagg_lacp_input;
1809 sc->sc_init = lacp_init;
1810 sc->sc_stop = lacp_stop;
1811 sc->sc_lladdr = lagg_lacp_lladdr;
1812 sc->sc_req = lacp_req;
1813 sc->sc_portreq = lacp_portreq;
1815 error = lacp_attach(sc);
1819 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1820 lacp_port_create(lp);
1826 lagg_lacp_detach(struct lagg_softc *sc)
1828 struct lagg_port *lp;
1831 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1832 lacp_port_destroy(lp);
1834 /* unlocking is safe here */
1836 error = lacp_detach(sc);
1843 lagg_lacp_lladdr(struct lagg_softc *sc)
1845 struct lagg_port *lp;
1847 /* purge all the lacp ports */
1848 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1849 lacp_port_destroy(lp);
1851 /* add them back in */
1852 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1853 lacp_port_create(lp);
1857 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1859 struct lagg_port *lp;
1861 lp = lacp_select_tx_port(sc, m);
1868 return (lagg_enqueue(lp->lp_ifp, m));
1871 static struct mbuf *
1872 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1874 struct ifnet *ifp = sc->sc_ifp;
1875 struct ether_header *eh;
1878 eh = mtod(m, struct ether_header *);
1879 etype = ntohs(eh->ether_type);
1881 /* Tap off LACP control messages */
1882 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1883 m = lacp_input(lp, m);
1889 * If the port is not collecting or not in the active aggregator then
1892 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1897 m->m_pkthdr.rcvif = ifp;