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 void lagg_start(struct ifnet *);
114 static int lagg_media_change(struct ifnet *);
115 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
116 static struct lagg_port *lagg_link_active(struct lagg_softc *,
118 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
120 IFC_SIMPLE_DECLARE(lagg, 0);
122 /* Simple round robin */
123 static int lagg_rr_attach(struct lagg_softc *);
124 static int lagg_rr_detach(struct lagg_softc *);
125 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
126 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
129 /* Active failover */
130 static int lagg_fail_attach(struct lagg_softc *);
131 static int lagg_fail_detach(struct lagg_softc *);
132 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
133 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
137 static int lagg_lb_attach(struct lagg_softc *);
138 static int lagg_lb_detach(struct lagg_softc *);
139 static int lagg_lb_port_create(struct lagg_port *);
140 static void lagg_lb_port_destroy(struct lagg_port *);
141 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
142 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
144 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
147 static int lagg_lacp_attach(struct lagg_softc *);
148 static int lagg_lacp_detach(struct lagg_softc *);
149 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
150 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
152 static void lagg_lacp_lladdr(struct lagg_softc *);
154 /* lagg protocol table */
155 static const struct {
157 int (*ti_attach)(struct lagg_softc *);
159 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
160 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
161 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
162 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
163 { LAGG_PROTO_LACP, lagg_lacp_attach },
164 { LAGG_PROTO_NONE, NULL }
167 SYSCTL_DECL(_net_link);
168 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0, "Link Aggregation");
170 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
171 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
172 &lagg_failover_rx_all, 0,
173 "Accept input from any interface in a failover lagg");
174 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
175 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
176 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
178 "Default setting for using flow id for load sharing");
181 lagg_modevent(module_t mod, int type, void *data)
186 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
187 SLIST_INIT(&lagg_list);
188 if_clone_attach(&lagg_cloner);
189 lagg_input_p = lagg_input;
190 lagg_linkstate_p = lagg_port_state;
191 lagg_detach_cookie = EVENTHANDLER_REGISTER(
192 ifnet_departure_event, lagg_port_ifdetach, NULL,
193 EVENTHANDLER_PRI_ANY);
196 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
198 if_clone_detach(&lagg_cloner);
200 lagg_linkstate_p = NULL;
201 mtx_destroy(&lagg_list_mtx);
209 static moduledata_t lagg_mod = {
215 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
216 MODULE_VERSION(if_lagg, 1);
218 #if __FreeBSD_version >= 800000
220 * This routine is run via an vlan
224 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
226 struct lagg_softc *sc = ifp->if_softc;
227 struct lagg_port *lp;
229 if (ifp->if_softc != arg) /* Not our event */
233 if (!SLIST_EMPTY(&sc->sc_ports)) {
234 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
235 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
241 * This routine is run via an vlan
245 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
247 struct lagg_softc *sc = ifp->if_softc;
248 struct lagg_port *lp;
250 if (ifp->if_softc != arg) /* Not our event */
254 if (!SLIST_EMPTY(&sc->sc_ports)) {
255 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
256 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
263 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
265 struct lagg_softc *sc;
268 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
269 struct sysctl_oid *oid;
270 char num[14]; /* sufficient for 32 bits */
272 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
273 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
279 sysctl_ctx_init(&sc->ctx);
280 snprintf(num, sizeof(num), "%u", unit);
281 sc->use_flowid = def_use_flowid;
282 oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
283 OID_AUTO, num, CTLFLAG_RD, NULL, "");
284 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
285 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
286 "Use flow id for load sharing");
287 /* Hash all layers by default */
288 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
290 sc->sc_proto = LAGG_PROTO_NONE;
291 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
292 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
293 sc->sc_proto = lagg_protos[i].ti_proto;
294 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
295 if_free_type(ifp, IFT_ETHER);
303 SLIST_INIT(&sc->sc_ports);
304 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
306 /* Initialise pseudo media types */
307 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
309 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
310 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
312 if_initname(ifp, ifc->ifc_name, unit);
313 ifp->if_type = IFT_ETHER;
315 ifp->if_start = lagg_start;
316 ifp->if_init = lagg_init;
317 ifp->if_ioctl = lagg_ioctl;
318 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
320 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
321 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
322 IFQ_SET_READY(&ifp->if_snd);
325 * Attach as an ordinary ethernet device, childs will be attached
326 * as special device IFT_IEEE8023ADLAG.
328 ether_ifattach(ifp, eaddr);
330 #if __FreeBSD_version >= 800000
331 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
332 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
333 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
334 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
337 /* Insert into the global list of laggs */
338 mtx_lock(&lagg_list_mtx);
339 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
340 mtx_unlock(&lagg_list_mtx);
346 lagg_clone_destroy(struct ifnet *ifp)
348 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
349 struct lagg_port *lp;
354 ifp->if_flags &= ~IFF_UP;
356 #if __FreeBSD_version >= 800000
357 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
358 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
361 /* Shutdown and remove lagg ports */
362 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
363 lagg_port_destroy(lp, 1);
364 /* Unhook the aggregation protocol */
365 if (sc->sc_detach != NULL)
366 (*sc->sc_detach)(sc);
370 sysctl_ctx_free(&sc->ctx);
371 ifmedia_removeall(&sc->sc_media);
373 if_free_type(ifp, IFT_ETHER);
375 mtx_lock(&lagg_list_mtx);
376 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
377 mtx_unlock(&lagg_list_mtx);
379 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
380 LAGG_LOCK_DESTROY(sc);
385 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
387 struct ifnet *ifp = sc->sc_ifp;
389 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
392 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
393 /* Let the protocol know the MAC has changed */
394 if (sc->sc_lladdr != NULL)
395 (*sc->sc_lladdr)(sc);
396 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
400 lagg_capabilities(struct lagg_softc *sc)
402 struct lagg_port *lp;
403 int cap = ~0, ena = ~0;
406 LAGG_WLOCK_ASSERT(sc);
408 /* Get capabilities from the lagg ports */
409 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
410 cap &= lp->lp_ifp->if_capabilities;
411 ena &= lp->lp_ifp->if_capenable;
412 hwa &= lp->lp_ifp->if_hwassist;
414 cap = (cap == ~0 ? 0 : cap);
415 ena = (ena == ~0 ? 0 : ena);
416 hwa = (hwa == ~0 ? 0 : hwa);
418 if (sc->sc_ifp->if_capabilities != cap ||
419 sc->sc_ifp->if_capenable != ena ||
420 sc->sc_ifp->if_hwassist != hwa) {
421 sc->sc_ifp->if_capabilities = cap;
422 sc->sc_ifp->if_capenable = ena;
423 sc->sc_ifp->if_hwassist = hwa;
424 getmicrotime(&sc->sc_ifp->if_lastchange);
426 if (sc->sc_ifflags & IFF_DEBUG)
427 if_printf(sc->sc_ifp,
428 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
433 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
435 struct lagg_softc *sc = lp->lp_softc;
436 struct ifnet *ifp = lp->lp_ifp;
437 struct lagg_llq *llq;
440 LAGG_WLOCK_ASSERT(sc);
442 if (lp->lp_detaching ||
443 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
446 /* Check to make sure its not already queued to be changed */
447 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
448 if (llq->llq_ifp == ifp) {
455 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
456 if (llq == NULL) /* XXX what to do */
460 /* Update the lladdr even if pending, it may have changed */
462 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
465 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
467 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
471 * Set the interface MAC address from a taskqueue to avoid a LOR.
474 lagg_port_setlladdr(void *arg, int pending)
476 struct lagg_softc *sc = (struct lagg_softc *)arg;
477 struct lagg_llq *llq, *head;
481 /* Grab a local reference of the queue and remove it from the softc */
483 head = SLIST_FIRST(&sc->sc_llq_head);
484 SLIST_FIRST(&sc->sc_llq_head) = NULL;
488 * Traverse the queue and set the lladdr on each ifp. It is safe to do
489 * unlocked as we have the only reference to it.
491 for (llq = head; llq != NULL; llq = head) {
494 /* Set the link layer address */
495 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
497 printf("%s: setlladdr failed on %s\n", __func__,
500 head = SLIST_NEXT(llq, llq_entries);
506 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
508 struct lagg_softc *sc_ptr;
509 struct lagg_port *lp;
512 LAGG_WLOCK_ASSERT(sc);
514 /* Limit the maximal number of lagg ports */
515 if (sc->sc_count >= LAGG_MAX_PORTS)
518 /* Check if port has already been associated to a lagg */
519 if (ifp->if_lagg != NULL)
522 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
523 if (ifp->if_type != IFT_ETHER)
524 return (EPROTONOSUPPORT);
526 /* Allow the first Ethernet member to define the MTU */
527 if (SLIST_EMPTY(&sc->sc_ports))
528 sc->sc_ifp->if_mtu = ifp->if_mtu;
529 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
530 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
535 if ((lp = malloc(sizeof(struct lagg_port),
536 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
539 /* Check if port is a stacked lagg */
540 mtx_lock(&lagg_list_mtx);
541 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
542 if (ifp == sc_ptr->sc_ifp) {
543 mtx_unlock(&lagg_list_mtx);
546 /* XXX disable stacking for the moment, its untested */
547 #ifdef LAGG_PORT_STACKING
548 lp->lp_flags |= LAGG_PORT_STACK;
549 if (lagg_port_checkstacking(sc_ptr) >=
551 mtx_unlock(&lagg_list_mtx);
558 mtx_unlock(&lagg_list_mtx);
560 /* Change the interface type */
561 lp->lp_iftype = ifp->if_type;
562 ifp->if_type = IFT_IEEE8023ADLAG;
564 lp->lp_ioctl = ifp->if_ioctl;
565 ifp->if_ioctl = lagg_port_ioctl;
566 lp->lp_output = ifp->if_output;
567 ifp->if_output = lagg_port_output;
572 /* Save port link layer address */
573 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
575 if (SLIST_EMPTY(&sc->sc_ports)) {
577 lagg_lladdr(sc, IF_LLADDR(ifp));
579 /* Update link layer address for this port */
580 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
583 /* Insert into the list of ports */
584 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
587 /* Update lagg capabilities */
588 lagg_capabilities(sc);
591 /* Add multicast addresses and interface flags to this port */
592 lagg_ether_cmdmulti(lp, 1);
593 lagg_setflags(lp, 1);
595 if (sc->sc_port_create != NULL)
596 error = (*sc->sc_port_create)(lp);
598 /* remove the port again, without calling sc_port_destroy */
599 lagg_port_destroy(lp, 0);
606 #ifdef LAGG_PORT_STACKING
608 lagg_port_checkstacking(struct lagg_softc *sc)
610 struct lagg_softc *sc_ptr;
611 struct lagg_port *lp;
614 LAGG_WLOCK_ASSERT(sc);
616 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
617 if (lp->lp_flags & LAGG_PORT_STACK) {
618 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
619 m = MAX(m, lagg_port_checkstacking(sc_ptr));
628 lagg_port_destroy(struct lagg_port *lp, int runpd)
630 struct lagg_softc *sc = lp->lp_softc;
631 struct lagg_port *lp_ptr;
632 struct lagg_llq *llq;
633 struct ifnet *ifp = lp->lp_ifp;
635 LAGG_WLOCK_ASSERT(sc);
637 if (runpd && sc->sc_port_destroy != NULL)
638 (*sc->sc_port_destroy)(lp);
641 * Remove multicast addresses and interface flags from this port and
642 * reset the MAC address, skip if the interface is being detached.
644 if (!lp->lp_detaching) {
645 lagg_ether_cmdmulti(lp, 0);
646 lagg_setflags(lp, 0);
647 lagg_port_lladdr(lp, lp->lp_lladdr);
650 /* Restore interface */
651 ifp->if_type = lp->lp_iftype;
652 ifp->if_ioctl = lp->lp_ioctl;
653 ifp->if_output = lp->lp_output;
656 /* Finally, remove the port from the lagg */
657 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
660 /* Update the primary interface */
661 if (lp == sc->sc_primary) {
662 uint8_t lladdr[ETHER_ADDR_LEN];
664 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
665 bzero(&lladdr, ETHER_ADDR_LEN);
667 bcopy(lp_ptr->lp_lladdr,
668 lladdr, ETHER_ADDR_LEN);
670 lagg_lladdr(sc, lladdr);
671 sc->sc_primary = lp_ptr;
673 /* Update link layer address for each port */
674 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
675 lagg_port_lladdr(lp_ptr, lladdr);
678 /* Remove any pending lladdr changes from the queue */
679 if (lp->lp_detaching) {
680 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
681 if (llq->llq_ifp == ifp) {
682 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
685 break; /* Only appears once */
691 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
695 /* Update lagg capabilities */
696 lagg_capabilities(sc);
703 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
705 struct lagg_reqport *rp = (struct lagg_reqport *)data;
706 struct lagg_softc *sc;
707 struct lagg_port *lp = NULL;
710 /* Should be checked by the caller */
711 if (ifp->if_type != IFT_IEEE8023ADLAG ||
712 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
717 if (rp->rp_portname[0] == '\0' ||
718 ifunit(rp->rp_portname) != ifp) {
724 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
730 lagg_port2req(lp, rp);
735 if (lp->lp_ioctl == NULL) {
739 error = (*lp->lp_ioctl)(ifp, cmd, data);
743 /* Update lagg interface capabilities */
745 lagg_capabilities(sc);
750 /* Do not allow the MTU to be changed once joined */
761 if (lp->lp_ioctl != NULL)
762 return ((*lp->lp_ioctl)(ifp, cmd, data));
768 * For direct output to child ports.
771 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
772 struct sockaddr *dst, struct route *ro)
774 struct lagg_port *lp = ifp->if_lagg;
776 switch (dst->sa_family) {
777 case pseudo_AF_HDRCMPLT:
779 return ((*lp->lp_output)(ifp, m, dst, ro));
782 /* drop any other frames */
788 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
790 struct lagg_port *lp;
791 struct lagg_softc *sc;
793 if ((lp = ifp->if_lagg) == NULL)
799 lp->lp_detaching = 1;
800 lagg_port_destroy(lp, 1);
805 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
807 struct lagg_softc *sc = lp->lp_softc;
809 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
810 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
811 rp->rp_prio = lp->lp_prio;
812 rp->rp_flags = lp->lp_flags;
813 if (sc->sc_portreq != NULL)
814 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
816 /* Add protocol specific flags */
817 switch (sc->sc_proto) {
818 case LAGG_PROTO_FAILOVER:
819 if (lp == sc->sc_primary)
820 rp->rp_flags |= LAGG_PORT_MASTER;
821 if (lp == lagg_link_active(sc, sc->sc_primary))
822 rp->rp_flags |= LAGG_PORT_ACTIVE;
825 case LAGG_PROTO_ROUNDROBIN:
826 case LAGG_PROTO_LOADBALANCE:
827 case LAGG_PROTO_ETHERCHANNEL:
828 if (LAGG_PORTACTIVE(lp))
829 rp->rp_flags |= LAGG_PORT_ACTIVE;
832 case LAGG_PROTO_LACP:
833 /* LACP has a different definition of active */
834 if (lacp_isactive(lp))
835 rp->rp_flags |= LAGG_PORT_ACTIVE;
836 if (lacp_iscollecting(lp))
837 rp->rp_flags |= LAGG_PORT_COLLECTING;
838 if (lacp_isdistributing(lp))
839 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
848 struct lagg_softc *sc = (struct lagg_softc *)xsc;
849 struct lagg_port *lp;
850 struct ifnet *ifp = sc->sc_ifp;
852 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
857 ifp->if_drv_flags |= IFF_DRV_RUNNING;
858 /* Update the port lladdrs */
859 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
860 lagg_port_lladdr(lp, IF_LLADDR(ifp));
862 if (sc->sc_init != NULL)
869 lagg_stop(struct lagg_softc *sc)
871 struct ifnet *ifp = sc->sc_ifp;
873 LAGG_WLOCK_ASSERT(sc);
875 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
878 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
880 if (sc->sc_stop != NULL)
885 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
887 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
888 struct lagg_reqall *ra = (struct lagg_reqall *)data;
889 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
890 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
891 struct ifreq *ifr = (struct ifreq *)data;
892 struct lagg_port *lp;
894 struct thread *td = curthread;
896 int count, buflen, len, error = 0;
898 bzero(&rpbuf, sizeof(rpbuf));
904 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
906 buflen = count * sizeof(struct lagg_reqport);
909 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
912 ra->ra_proto = sc->sc_proto;
913 if (sc->sc_req != NULL)
914 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
918 len = min(ra->ra_size, buflen);
919 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
920 if (len < sizeof(rpbuf))
923 lagg_port2req(lp, &rpbuf);
924 memcpy(buf, &rpbuf, sizeof(rpbuf));
926 buf += sizeof(rpbuf);
927 len -= sizeof(rpbuf);
930 ra->ra_ports = count;
931 ra->ra_size = count * sizeof(rpbuf);
932 error = copyout(outbuf, ra->ra_port, ra->ra_size);
933 free(outbuf, M_TEMP);
936 error = priv_check(td, PRIV_NET_LAGG);
939 if (ra->ra_proto >= LAGG_PROTO_MAX) {
940 error = EPROTONOSUPPORT;
944 if (sc->sc_proto != LAGG_PROTO_NONE) {
945 /* Reset protocol first in case detach unlocks */
946 sc->sc_proto = LAGG_PROTO_NONE;
947 error = sc->sc_detach(sc);
948 sc->sc_detach = NULL;
951 sc->sc_port_create = NULL;
952 sc->sc_port_destroy = NULL;
953 sc->sc_linkstate = NULL;
956 sc->sc_lladdr = NULL;
958 sc->sc_portreq = NULL;
959 } else if (sc->sc_input != NULL) {
960 /* Still detaching */
967 for (int i = 0; i < (sizeof(lagg_protos) /
968 sizeof(lagg_protos[0])); i++) {
969 if (lagg_protos[i].ti_proto == ra->ra_proto) {
970 if (sc->sc_ifflags & IFF_DEBUG)
971 printf("%s: using proto %u\n",
973 lagg_protos[i].ti_proto);
974 sc->sc_proto = lagg_protos[i].ti_proto;
975 if (sc->sc_proto != LAGG_PROTO_NONE)
976 error = lagg_protos[i].ti_attach(sc);
982 error = EPROTONOSUPPORT;
985 rf->rf_flags = sc->sc_flags;
988 error = priv_check(td, PRIV_NET_LAGG);
991 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
996 sc->sc_flags &= ~LAGG_F_HASHMASK;
997 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1001 if (rp->rp_portname[0] == '\0' ||
1002 (tpif = ifunit(rp->rp_portname)) == NULL) {
1008 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1009 lp->lp_softc != sc) {
1015 lagg_port2req(lp, rp);
1019 error = priv_check(td, PRIV_NET_LAGG);
1022 if (rp->rp_portname[0] == '\0' ||
1023 (tpif = ifunit(rp->rp_portname)) == NULL) {
1028 error = lagg_port_create(sc, tpif);
1031 case SIOCSLAGGDELPORT:
1032 error = priv_check(td, PRIV_NET_LAGG);
1035 if (rp->rp_portname[0] == '\0' ||
1036 (tpif = ifunit(rp->rp_portname)) == NULL) {
1042 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1043 lp->lp_softc != sc) {
1049 error = lagg_port_destroy(lp, 1);
1053 /* Set flags on ports too */
1055 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1056 lagg_setflags(lp, 1);
1060 if (!(ifp->if_flags & IFF_UP) &&
1061 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1063 * If interface is marked down and it is running,
1064 * then stop and disable it.
1069 } else if ((ifp->if_flags & IFF_UP) &&
1070 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1072 * If interface is marked up and it is stopped, then
1075 (*ifp->if_init)(sc);
1081 error = lagg_ether_setmulti(sc);
1086 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1091 /* Do not allow the MTU or caps to be directly changed */
1096 error = ether_ioctl(ifp, cmd, data);
1103 lagg_ether_setmulti(struct lagg_softc *sc)
1105 struct lagg_port *lp;
1107 LAGG_WLOCK_ASSERT(sc);
1109 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1110 /* First, remove any existing filter entries. */
1111 lagg_ether_cmdmulti(lp, 0);
1112 /* copy all addresses from the lagg interface to the port */
1113 lagg_ether_cmdmulti(lp, 1);
1119 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1121 struct lagg_softc *sc = lp->lp_softc;
1122 struct ifnet *ifp = lp->lp_ifp;
1123 struct ifnet *scifp = sc->sc_ifp;
1125 struct ifmultiaddr *ifma, *rifma = NULL;
1126 struct sockaddr_dl sdl;
1129 LAGG_WLOCK_ASSERT(sc);
1131 bzero((char *)&sdl, sizeof(sdl));
1132 sdl.sdl_len = sizeof(sdl);
1133 sdl.sdl_family = AF_LINK;
1134 sdl.sdl_type = IFT_ETHER;
1135 sdl.sdl_alen = ETHER_ADDR_LEN;
1136 sdl.sdl_index = ifp->if_index;
1139 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1140 if (ifma->ifma_addr->sa_family != AF_LINK)
1142 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1143 LLADDR(&sdl), ETHER_ADDR_LEN);
1145 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1148 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1151 mc->mc_ifma = rifma;
1152 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1155 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1156 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1157 if_delmulti_ifma(mc->mc_ifma);
1164 /* Handle a ref counted flag that should be set on the lagg port as well */
1166 lagg_setflag(struct lagg_port *lp, int flag, int status,
1167 int (*func)(struct ifnet *, int))
1169 struct lagg_softc *sc = lp->lp_softc;
1170 struct ifnet *scifp = sc->sc_ifp;
1171 struct ifnet *ifp = lp->lp_ifp;
1174 LAGG_WLOCK_ASSERT(sc);
1176 status = status ? (scifp->if_flags & flag) : 0;
1177 /* Now "status" contains the flag value or 0 */
1180 * See if recorded ports status is different from what
1181 * we want it to be. If it is, flip it. We record ports
1182 * status in lp_ifflags so that we won't clear ports flag
1183 * we haven't set. In fact, we don't clear or set ports
1184 * flags directly, but get or release references to them.
1185 * That's why we can be sure that recorded flags still are
1186 * in accord with actual ports flags.
1188 if (status != (lp->lp_ifflags & flag)) {
1189 error = (*func)(ifp, status);
1192 lp->lp_ifflags &= ~flag;
1193 lp->lp_ifflags |= status;
1199 * Handle IFF_* flags that require certain changes on the lagg port
1200 * if "status" is true, update ports flags respective to the lagg
1201 * if "status" is false, forcedly clear the flags set on port.
1204 lagg_setflags(struct lagg_port *lp, int status)
1208 for (i = 0; lagg_pflags[i].flag; i++) {
1209 error = lagg_setflag(lp, lagg_pflags[i].flag,
1210 status, lagg_pflags[i].func);
1218 lagg_start(struct ifnet *ifp)
1220 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1225 /* We need a Tx algorithm and at least one port */
1226 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1227 IF_DRAIN(&ifp->if_snd);
1232 for (;; error = 0) {
1233 IFQ_DEQUEUE(&ifp->if_snd, m);
1237 ETHER_BPF_MTAP(ifp, m);
1239 error = (*sc->sc_start)(sc, m);
1248 static struct mbuf *
1249 lagg_input(struct ifnet *ifp, struct mbuf *m)
1251 struct lagg_port *lp = ifp->if_lagg;
1252 struct lagg_softc *sc = lp->lp_softc;
1253 struct ifnet *scifp = sc->sc_ifp;
1256 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1257 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1258 sc->sc_proto == LAGG_PROTO_NONE) {
1264 ETHER_BPF_MTAP(scifp, m);
1266 m = (*sc->sc_input)(sc, lp, m);
1269 scifp->if_ipackets++;
1270 scifp->if_ibytes += m->m_pkthdr.len;
1272 if (scifp->if_flags & IFF_MONITOR) {
1283 lagg_media_change(struct ifnet *ifp)
1285 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1287 if (sc->sc_ifflags & IFF_DEBUG)
1288 printf("%s\n", __func__);
1295 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1297 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1298 struct lagg_port *lp;
1300 imr->ifm_status = IFM_AVALID;
1301 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1304 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1305 if (LAGG_PORTACTIVE(lp))
1306 imr->ifm_status |= IFM_ACTIVE;
1312 lagg_linkstate(struct lagg_softc *sc)
1314 struct lagg_port *lp;
1315 int new_link = LINK_STATE_DOWN;
1318 /* Our link is considered up if at least one of our ports is active */
1319 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1320 if (lp->lp_link_state == LINK_STATE_UP) {
1321 new_link = LINK_STATE_UP;
1325 if_link_state_change(sc->sc_ifp, new_link);
1327 /* Update if_baudrate to reflect the max possible speed */
1328 switch (sc->sc_proto) {
1329 case LAGG_PROTO_FAILOVER:
1330 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1331 sc->sc_primary->lp_ifp->if_baudrate : 0;
1333 case LAGG_PROTO_ROUNDROBIN:
1334 case LAGG_PROTO_LOADBALANCE:
1335 case LAGG_PROTO_ETHERCHANNEL:
1337 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1338 speed += lp->lp_ifp->if_baudrate;
1339 sc->sc_ifp->if_baudrate = speed;
1341 case LAGG_PROTO_LACP:
1342 /* LACP updates if_baudrate itself */
1348 lagg_port_state(struct ifnet *ifp, int state)
1350 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1351 struct lagg_softc *sc = NULL;
1360 if (sc->sc_linkstate != NULL)
1361 (*sc->sc_linkstate)(lp);
1366 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1368 struct lagg_port *lp_next, *rval = NULL;
1369 // int new_link = LINK_STATE_DOWN;
1371 LAGG_RLOCK_ASSERT(sc);
1373 * Search a port which reports an active link state.
1378 if (LAGG_PORTACTIVE(lp)) {
1382 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1383 LAGG_PORTACTIVE(lp_next)) {
1389 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1390 if (LAGG_PORTACTIVE(lp_next)) {
1399 * The IEEE 802.1D standard assumes that a lagg with
1400 * multiple ports is always full duplex. This is valid
1401 * for load sharing laggs and if at least two links
1402 * are active. Unfortunately, checking the latter would
1403 * be too expensive at this point.
1405 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1407 new_link = LINK_STATE_FULL_DUPLEX;
1409 new_link = rval->lp_link_state;
1417 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1419 if (m->m_pkthdr.len < (off + len)) {
1421 } else if (m->m_len < (off + len)) {
1422 m_copydata(m, off, len, buf);
1425 return (mtod(m, char *) + off);
1429 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1434 struct ether_header *eh;
1435 const struct ether_vlan_header *vlan;
1437 const struct ip *ip;
1438 const uint32_t *ports;
1442 const struct ip6_hdr *ip6;
1452 struct ether_vlan_header vlan;
1460 eh = mtod(m, struct ether_header *);
1461 etype = ntohs(eh->ether_type);
1462 if (sc->sc_flags & LAGG_F_HASHL2) {
1463 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1464 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1467 /* Special handling for encapsulating VLAN frames */
1468 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1469 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1470 sizeof(m->m_pkthdr.ether_vtag), p);
1471 } else if (etype == ETHERTYPE_VLAN) {
1472 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1476 if (sc->sc_flags & LAGG_F_HASHL2)
1477 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1478 etype = ntohs(vlan->evl_proto);
1479 off += sizeof(*vlan) - sizeof(*eh);
1485 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1489 if (sc->sc_flags & LAGG_F_HASHL3) {
1490 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1491 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1493 if (!(sc->sc_flags & LAGG_F_HASHL4))
1499 iphlen = ip->ip_hl << 2;
1500 if (iphlen < sizeof(*ip))
1503 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1506 p = hash32_buf(ports, sizeof(*ports), p);
1512 case ETHERTYPE_IPV6:
1513 if (!(sc->sc_flags & LAGG_F_HASHL3))
1515 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1519 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1520 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1521 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1522 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1531 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1534 return (ifp->if_transmit)(ifp, m);
1538 * Simple round robin aggregation
1542 lagg_rr_attach(struct lagg_softc *sc)
1544 sc->sc_detach = lagg_rr_detach;
1545 sc->sc_start = lagg_rr_start;
1546 sc->sc_input = lagg_rr_input;
1547 sc->sc_port_create = NULL;
1548 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1555 lagg_rr_detach(struct lagg_softc *sc)
1561 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1563 struct lagg_port *lp;
1566 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1568 lp = SLIST_FIRST(&sc->sc_ports);
1570 lp = SLIST_NEXT(lp, lp_entries);
1573 * Check the port's link state. This will return the next active
1574 * port if the link is down or the port is NULL.
1576 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1582 return (lagg_enqueue(lp->lp_ifp, m));
1585 static struct mbuf *
1586 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1588 struct ifnet *ifp = sc->sc_ifp;
1590 /* Just pass in the packet to our lagg device */
1591 m->m_pkthdr.rcvif = ifp;
1601 lagg_fail_attach(struct lagg_softc *sc)
1603 sc->sc_detach = lagg_fail_detach;
1604 sc->sc_start = lagg_fail_start;
1605 sc->sc_input = lagg_fail_input;
1606 sc->sc_port_create = NULL;
1607 sc->sc_port_destroy = NULL;
1613 lagg_fail_detach(struct lagg_softc *sc)
1619 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1621 struct lagg_port *lp;
1623 /* Use the master port if active or the next available port */
1624 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1630 return (lagg_enqueue(lp->lp_ifp, m));
1633 static struct mbuf *
1634 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1636 struct ifnet *ifp = sc->sc_ifp;
1637 struct lagg_port *tmp_tp;
1639 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1640 m->m_pkthdr.rcvif = ifp;
1644 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1645 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1647 * If tmp_tp is null, we've recieved a packet when all
1648 * our links are down. Weird, but process it anyways.
1650 if ((tmp_tp == NULL || tmp_tp == lp)) {
1651 m->m_pkthdr.rcvif = ifp;
1665 lagg_lb_attach(struct lagg_softc *sc)
1667 struct lagg_port *lp;
1670 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1671 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1674 sc->sc_detach = lagg_lb_detach;
1675 sc->sc_start = lagg_lb_start;
1676 sc->sc_input = lagg_lb_input;
1677 sc->sc_port_create = lagg_lb_port_create;
1678 sc->sc_port_destroy = lagg_lb_port_destroy;
1679 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1681 lb->lb_key = arc4random();
1682 sc->sc_psc = (caddr_t)lb;
1684 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1685 lagg_lb_port_create(lp);
1691 lagg_lb_detach(struct lagg_softc *sc)
1693 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1700 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1702 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1703 struct lagg_port *lp_next;
1706 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1707 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1710 if (i >= LAGG_MAX_PORTS)
1712 if (sc->sc_ifflags & IFF_DEBUG)
1713 printf("%s: port %s at index %d\n",
1714 sc->sc_ifname, lp_next->lp_ifname, i);
1715 lb->lb_ports[i++] = lp_next;
1722 lagg_lb_port_create(struct lagg_port *lp)
1724 struct lagg_softc *sc = lp->lp_softc;
1725 return (lagg_lb_porttable(sc, NULL));
1729 lagg_lb_port_destroy(struct lagg_port *lp)
1731 struct lagg_softc *sc = lp->lp_softc;
1732 lagg_lb_porttable(sc, lp);
1736 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1738 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1739 struct lagg_port *lp = NULL;
1742 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1743 p = m->m_pkthdr.flowid;
1745 p = lagg_hashmbuf(sc, m, lb->lb_key);
1747 lp = lb->lb_ports[p];
1750 * Check the port's link state. This will return the next active
1751 * port if the link is down or the port is NULL.
1753 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1759 return (lagg_enqueue(lp->lp_ifp, m));
1762 static struct mbuf *
1763 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1765 struct ifnet *ifp = sc->sc_ifp;
1767 /* Just pass in the packet to our lagg device */
1768 m->m_pkthdr.rcvif = ifp;
1778 lagg_lacp_attach(struct lagg_softc *sc)
1780 struct lagg_port *lp;
1783 sc->sc_detach = lagg_lacp_detach;
1784 sc->sc_port_create = lacp_port_create;
1785 sc->sc_port_destroy = lacp_port_destroy;
1786 sc->sc_linkstate = lacp_linkstate;
1787 sc->sc_start = lagg_lacp_start;
1788 sc->sc_input = lagg_lacp_input;
1789 sc->sc_init = lacp_init;
1790 sc->sc_stop = lacp_stop;
1791 sc->sc_lladdr = lagg_lacp_lladdr;
1792 sc->sc_req = lacp_req;
1793 sc->sc_portreq = lacp_portreq;
1795 error = lacp_attach(sc);
1799 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1800 lacp_port_create(lp);
1806 lagg_lacp_detach(struct lagg_softc *sc)
1808 struct lagg_port *lp;
1811 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1812 lacp_port_destroy(lp);
1814 /* unlocking is safe here */
1816 error = lacp_detach(sc);
1823 lagg_lacp_lladdr(struct lagg_softc *sc)
1825 struct lagg_port *lp;
1827 /* purge all the lacp ports */
1828 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1829 lacp_port_destroy(lp);
1831 /* add them back in */
1832 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1833 lacp_port_create(lp);
1837 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1839 struct lagg_port *lp;
1841 lp = lacp_select_tx_port(sc, m);
1848 return (lagg_enqueue(lp->lp_ifp, m));
1851 static struct mbuf *
1852 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1854 struct ifnet *ifp = sc->sc_ifp;
1855 struct ether_header *eh;
1858 eh = mtod(m, struct ether_header *);
1859 etype = ntohs(eh->ether_type);
1861 /* Tap off LACP control messages */
1862 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1863 m = lacp_input(lp, m);
1869 * If the port is not collecting or not in the active aggregator then
1872 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1877 m->m_pkthdr.rcvif = ifp;