1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $
2 * $FreeBSD: head/sys/net/if_lagg.c 256218 2013-10-09 19:04:40Z glebius $
5 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
6 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
7 * Copyright (c) 2014 Marcelo Araujo <araujo@FreeBSD.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/cdefs.h>
25 #include "opt_inet6.h"
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
37 #include <sys/systm.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>
53 #include <net/ifq_var.h>
56 #include <net/netmsg2.h>
57 #include <net/netisr2.h>
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/if_ether.h>
65 #include <netinet/ip.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/in6_ifattach.h>
74 #include <net/vlan/if_vlan_var.h>
75 #include <net/lagg/if_lagg.h>
76 #include <net/lagg/ieee8023ad_lacp.h>
78 /* Special flags we should propagate to the lagg ports. */
81 int (*func)(struct ifnet *, int);
83 {IFF_PROMISC, ifpromisc},
84 {IFF_ALLMULTI, if_allmulti},
88 SLIST_HEAD(, lagg_softc) lagg_list; /* list of laggs */
89 static struct lock lagg_list_lock;
90 eventhandler_tag lagg_detach_cookie = NULL;
92 static int lagg_clone_create(struct if_clone *, int, caddr_t);
93 static int lagg_clone_destroy(struct ifnet *);
94 static const char * laggname = "lagg";
95 struct if_clone lagg_cloner = IF_CLONE_INITIALIZER("lagg",
100 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
101 static void lagg_capabilities(struct lagg_softc *);
102 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
103 static void lagg_port_setlladdr(void *, int);
104 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
105 static int lagg_port_destroy(struct lagg_port *, int);
106 static void lagg_input(struct ifnet *, struct mbuf *);
107 static void lagg_linkstate(struct lagg_softc *);
109 static void lagg_port_state(struct ifnet *, int);
111 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *cr);
112 static int lagg_port_output(struct ifnet *, struct mbuf *,
113 struct sockaddr *, struct rtentry *);
114 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
115 #ifdef LAGG_PORT_STACKING
116 static int lagg_port_checkstacking(struct lagg_softc *);
118 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
119 static void lagg_init(void *);
120 static void lagg_stop(struct lagg_softc *);
121 static int lagg_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *cr);
122 static int lagg_ether_setmulti(struct lagg_softc *);
123 static int lagg_ether_cmdmulti(struct lagg_port *, int);
124 static int lagg_setflag(struct lagg_port *, int, int,
125 int (*func)(struct ifnet *, int));
126 static int lagg_setflags(struct lagg_port *, int status);
127 static void lagg_start(struct ifnet *, struct ifaltq_subque *ifsq);
128 static void lagg_start_dispatch(netmsg_t msg);
130 static int lagg_output(struct ifnet *ifp, struct mbuf *m);
133 static int lagg_transmit(struct ifnet *, struct mbuf *);
134 static void lagg_qflush(struct ifnet *);
136 static int lagg_media_change(struct ifnet *);
137 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
138 static struct lagg_port *lagg_link_active(struct lagg_softc *,
140 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
141 static int lagg_sysctl_active(SYSCTL_HANDLER_ARGS);
143 /* Simple round robin */
144 static int lagg_rr_attach(struct lagg_softc *);
145 static int lagg_rr_detach(struct lagg_softc *);
146 static struct ifnet *lagg_rr_select_tx_port(struct lagg_softc *sc,
148 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
151 /* Active failover */
152 static int lagg_fail_attach(struct lagg_softc *);
153 static int lagg_fail_detach(struct lagg_softc *);
154 static struct ifnet *lagg_fail_select_tx_port(struct lagg_softc *,
156 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
160 static int lagg_lb_attach(struct lagg_softc *);
161 static int lagg_lb_detach(struct lagg_softc *);
162 static int lagg_lb_port_create(struct lagg_port *);
163 static void lagg_lb_port_destroy(struct lagg_port *);
164 static struct ifnet *lagg_lb_select_tx_port(struct lagg_softc *,
166 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
168 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
171 static int lagg_lacp_attach(struct lagg_softc *);
172 static int lagg_lacp_detach(struct lagg_softc *);
173 static struct ifnet *lagg_lacp_select_tx_port(struct lagg_softc *,
175 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
177 static void lagg_lacp_lladdr(struct lagg_softc *);
179 static void lagg_callout(void *);
181 /* lagg protocol table */
182 static const struct {
184 int (*ti_attach)(struct lagg_softc *);
186 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
187 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
188 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
189 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
190 { LAGG_PROTO_LACP, lagg_lacp_attach },
191 { LAGG_PROTO_NONE, NULL }
194 SYSCTL_DECL(_net_link);
195 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
198 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
199 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
200 &lagg_failover_rx_all, 0,
201 "Accept input from any interface in a failover lagg");
202 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
203 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
204 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
206 "Default setting for using flow id for load sharing");
209 lagg_modevent(module_t mod, int type, void *data)
214 lockinit(&lagg_list_lock, "if_lagg list", 0, 0);
215 SLIST_INIT(&lagg_list);
216 if_clone_attach(&lagg_cloner);
217 lagg_input_p = lagg_input;
218 lagg_detach_cookie = EVENTHANDLER_REGISTER(
219 ifnet_departure_event, lagg_port_ifdetach, NULL,
220 EVENTHANDLER_PRI_ANY);
223 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
225 if_clone_detach(&lagg_cloner);
227 if_clone_detach(&lagg_cloner);
228 lockuninit(&lagg_list_lock);
236 static moduledata_t lagg_mod = {
242 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
243 MODULE_VERSION(if_lagg, 1);
246 * This routine is run via an vlan
250 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
253 struct lagg_softc *sc = ifp->if_softc;
254 struct lagg_port *lp;
256 if (ifp->if_softc != arg) /* Not our event */
260 if (!SLIST_EMPTY(&sc->sc_ports)) {
261 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
262 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp);
270 * This routine is run via an vlan
274 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
276 struct lagg_softc *sc = ifp->if_softc;
278 struct lagg_port *lp;
281 if (ifp->if_softc != arg) /* Not our event */
285 if (!SLIST_EMPTY(&sc->sc_ports)) {
287 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
288 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp);
295 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params __unused)
297 struct lagg_softc *sc;
300 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
301 struct sysctl_oid *oid;
302 char num[14]; /* sufficient for 32 bits */
304 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
305 ifp = sc->sc_ifp = &sc->sc_if; // XXX if_alloc(IFT_ETHER);
313 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
314 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
315 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
316 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
318 sysctl_ctx_init(&sc->ctx);
319 ksnprintf(num, sizeof(num), "%u", unit);
320 sc->use_flowid = def_use_flowid;
321 sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
322 &SYSCTL_NODE_CHILDREN(_net_link, lagg),
323 OID_AUTO, num, CTLFLAG_RD, NULL, "");
324 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
325 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
326 "Use flow id for load sharing");
327 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
328 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
329 "Total number of ports");
330 SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
331 "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
332 "I", "Total number of active ports");
333 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
334 "flapping", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_flapping,
335 sc->sc_flapping, "Total number of port change events");
336 /* Hash all layers by default */
337 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
339 sc->sc_proto = LAGG_PROTO_NONE;
340 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
341 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
342 sc->sc_proto = lagg_protos[i].ti_proto;
343 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
352 LAGG_CALLOUT_LOCK_INIT(sc);
353 SLIST_INIT(&sc->sc_ports);
354 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
357 * This uses the callout lock rather than the rmlock; one can't
358 * hold said rmlock during SWI.
360 callout_init(&sc->sc_callout);
361 /*, &sc->sc_call_lock, 0); */
363 /* Initialise pseudo media types */
364 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
366 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
367 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
369 if_initname(ifp, laggname, unit);
372 ifp->if_transmit = lagg_transmit;
373 ifp->if_qflush = lagg_qflush;
375 ifp->if_mtu = ETHERMTU;
376 ifp->if_init = lagg_init;
377 ifp->if_ioctl = lagg_ioctl;
378 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
379 ifp->if_start = lagg_start;
380 ifp->if_type = IFT_ETHER;
381 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
382 ifq_set_ready(&ifp->if_snd);
383 ifp->if_hdrlen = ETHER_HDR_LEN;
386 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
389 * Attach as an ordinary ethernet device, children will be attached
390 * as special device IFT_IEEE8023ADLAG.
393 ether_ifattach(ifp, eaddr, NULL);
395 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
396 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
397 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
398 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
400 /* Insert into the global list of laggs */
401 lockmgr(&lagg_list_lock, LK_EXCLUSIVE);
402 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
403 lockmgr(&lagg_list_lock, LK_RELEASE);
405 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
411 lagg_clone_destroy(struct ifnet *ifp)
413 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
414 struct lagg_port *lp;
419 ifp->if_flags &= ~IFF_UP;
421 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
422 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
424 /* Shutdown and remove lagg ports */
425 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
426 lagg_port_destroy(lp, 1);
427 /* Unhook the aggregation protocol */
428 if (sc->sc_detach != NULL)
429 (*sc->sc_detach)(sc);
433 sysctl_ctx_free(&sc->ctx);
434 ifmedia_removeall(&sc->sc_media);
438 /* This grabs sc_callout_mtx, serialising it correctly */
439 callout_drain(&sc->sc_callout);
442 /* At this point it's drained; we can free this */
443 counter_u64_free(sc->sc_ipackets);
444 counter_u64_free(sc->sc_opackets);
445 counter_u64_free(sc->sc_ibytes);
446 counter_u64_free(sc->sc_obytes);
449 lockmgr(&lagg_list_lock, LK_EXCLUSIVE);
450 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
451 lockmgr(&lagg_list_lock, LK_RELEASE);
453 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
454 LAGG_LOCK_DESTROY(sc);
455 LAGG_CALLOUT_LOCK_DESTROY(sc);
462 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
464 struct ifnet *ifp = sc->sc_ifp;
466 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
469 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
470 /* Let the protocol know the MAC has changed */
471 if (sc->sc_lladdr != NULL)
472 (*sc->sc_lladdr)(sc);
473 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
477 lagg_capabilities(struct lagg_softc *sc)
479 struct lagg_port *lp;
480 int cap = ~0, ena = ~0;
483 LAGG_WLOCK_ASSERT(sc);
485 /* Get capabilities from the lagg ports */
486 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
487 cap &= lp->lp_ifp->if_capabilities;
488 ena &= lp->lp_ifp->if_capenable;
489 hwa &= lp->lp_ifp->if_hwassist;
491 cap = (cap == ~0 ? 0 : cap);
492 ena = (ena == ~0 ? 0 : ena);
493 hwa = (hwa == ~0 ? 0 : hwa);
495 if (sc->sc_ifp->if_capabilities != cap ||
496 sc->sc_ifp->if_capenable != ena ||
497 sc->sc_ifp->if_hwassist != hwa) {
498 sc->sc_ifp->if_capabilities = cap;
499 sc->sc_ifp->if_capenable = ena;
500 sc->sc_ifp->if_hwassist = hwa;
501 getmicrotime(&sc->sc_ifp->if_lastchange);
503 if (sc->sc_ifflags & IFF_DEBUG)
504 if_printf(sc->sc_ifp,
505 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
510 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
512 struct lagg_softc *sc = lp->lp_softc;
513 struct ifnet *ifp = lp->lp_ifp;
514 struct lagg_llq *llq;
517 LAGG_WLOCK_ASSERT(sc);
519 if (lp->lp_detaching ||
520 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
523 /* Check to make sure its not already queued to be changed */
524 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
525 if (llq->llq_ifp == ifp) {
532 llq = kmalloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
533 if (llq == NULL) /* XXX what to do */
537 /* Update the lladdr even if pending, it may have changed */
539 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
542 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
544 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
548 * Set the interface MAC address from a taskqueue to avoid a LOR.
551 lagg_port_setlladdr(void *arg, int pending)
553 struct lagg_softc *sc = (struct lagg_softc *)arg;
554 struct lagg_llq *llq, *head;
558 /* Grab a local reference of the queue and remove it from the softc */
560 head = SLIST_FIRST(&sc->sc_llq_head);
561 SLIST_FIRST(&sc->sc_llq_head) = NULL;
565 * Traverse the queue and set the lladdr on each ifp. It is safe to do
566 * unlocked as we have the only reference to it.
568 for (llq = head; llq != NULL; llq = head) {
571 /* Set the link layer address */
572 /* CURVNET_SET(ifp->if_vnet); */
573 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
574 /* CURVNET_RESTORE(); */
576 kprintf("%s: setlladdr failed on %s\n", __func__,
579 head = SLIST_NEXT(llq, llq_entries);
580 kfree(llq, M_DEVBUF);
585 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
587 struct lagg_softc *sc_ptr;
588 struct lagg_port *lp;
591 LAGG_WLOCK_ASSERT(sc);
593 /* Limit the maximal number of lagg ports */
594 if (sc->sc_count >= LAGG_MAX_PORTS)
597 /* Check if port has already been associated to a lagg */
598 if (ifp->if_lagg != NULL) {
599 /* Port is already in the current lagg? */
600 lp = (struct lagg_port *)ifp->if_lagg;
601 if (lp->lp_softc == sc)
606 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
607 if (ifp->if_type != IFT_ETHER)
608 return (EPROTONOSUPPORT);
612 * The member interface should not have inet6 address because
613 * two interfaces with a valid link-local scope zone must not be
614 * merged in any form. This restriction is needed to
615 * prevent violation of link-local scope zone. Attempts to
616 * add a member interface which has inet6 addresses triggers
617 * removal of all inet6 addresses on the member interface.
619 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
620 if (in6ifa_llaonifp(lp->lp_ifp)) {
621 in6_ifdetach(lp->lp_ifp);
622 if_printf(sc->sc_ifp,
623 "IPv6 addresses on %s have been removed "
624 "before adding it as a member to prevent "
625 "IPv6 address scope violation.\n",
626 lp->lp_ifp->if_xname);
629 if (in6ifa_llaonifp(ifp)) {
631 if_printf(sc->sc_ifp,
632 "IPv6 addresses on %s have been removed "
633 "before adding it as a member to prevent "
634 "IPv6 address scope violation.\n",
638 /* Allow the first Ethernet member to define the MTU */
639 if (SLIST_EMPTY(&sc->sc_ports))
640 sc->sc_ifp->if_mtu = ifp->if_mtu;
641 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
642 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
647 if ((lp = kmalloc(sizeof(struct lagg_port),
648 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
651 /* Check if port is a stacked lagg */
652 lockmgr(&lagg_list_lock, LK_EXCLUSIVE);
653 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
654 if (ifp == sc_ptr->sc_ifp) {
655 lockmgr(&lagg_list_lock, LK_RELEASE);
658 /* XXX disable stacking for the moment, its untested */
659 #ifdef LAGG_PORT_STACKING
660 lp->lp_flags |= LAGG_PORT_STACK;
661 if (lagg_port_checkstacking(sc_ptr) >=
663 lockmgr(&lagg_list_lock, LK_RELEASE);
670 lockmgr(&lagg_list_lock, LK_RELEASE);
672 /* Change the interface type */
673 lp->lp_iftype = ifp->if_type;
674 ifp->if_type = IFT_IEEE8023ADLAG;
676 lp->lp_ioctl = ifp->if_ioctl;
677 ifp->if_ioctl = lagg_port_ioctl;
678 lp->lp_output = ifp->if_output;
679 ifp->if_output = lagg_port_output;
684 /* Save port link layer address */
685 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
687 if (SLIST_EMPTY(&sc->sc_ports)) {
689 lagg_lladdr(sc, IF_LLADDR(ifp));
691 /* Update link layer address for this port */
692 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
695 /* Insert into the list of ports */
696 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
699 /* Update lagg capabilities */
700 lagg_capabilities(sc);
703 /* Add multicast addresses and interface flags to this port */
704 lagg_ether_cmdmulti(lp, 1);
705 lagg_setflags(lp, 1);
707 if (sc->sc_port_create != NULL)
708 error = (*sc->sc_port_create)(lp);
710 /* remove the port again, without calling sc_port_destroy */
711 lagg_port_destroy(lp, 0);
718 #ifdef LAGG_PORT_STACKING
720 lagg_port_checkstacking(struct lagg_softc *sc)
722 struct lagg_softc *sc_ptr;
723 struct lagg_port *lp;
726 LAGG_WLOCK_ASSERT(sc);
728 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
729 if (lp->lp_flags & LAGG_PORT_STACK) {
730 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
731 m = MAX(m, lagg_port_checkstacking(sc_ptr));
740 lagg_port_destroy(struct lagg_port *lp, int runpd)
742 struct lagg_softc *sc = lp->lp_softc;
743 struct lagg_port *lp_ptr;
744 struct lagg_llq *llq;
745 struct ifnet *ifp = lp->lp_ifp;
747 LAGG_WLOCK_ASSERT(sc);
749 if (runpd && sc->sc_port_destroy != NULL)
750 (*sc->sc_port_destroy)(lp);
753 * Remove multicast addresses and interface flags from this port and
754 * reset the MAC address, skip if the interface is being detached.
756 if (!lp->lp_detaching) {
757 lagg_ether_cmdmulti(lp, 0);
758 lagg_setflags(lp, 0);
759 lagg_port_lladdr(lp, lp->lp_lladdr);
762 /* Restore interface */
763 ifp->if_type = lp->lp_iftype;
764 ifp->if_ioctl = lp->lp_ioctl;
765 ifp->if_output = lp->lp_output;
768 /* Finally, remove the port from the lagg */
769 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
772 /* Update the primary interface */
773 if (lp == sc->sc_primary) {
774 uint8_t lladdr[ETHER_ADDR_LEN];
776 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
777 bzero(&lladdr, ETHER_ADDR_LEN);
779 bcopy(lp_ptr->lp_lladdr,
780 lladdr, ETHER_ADDR_LEN);
782 lagg_lladdr(sc, lladdr);
783 sc->sc_primary = lp_ptr;
785 /* Update link layer address for each port */
786 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
787 lagg_port_lladdr(lp_ptr, lladdr);
790 /* Remove any pending lladdr changes from the queue */
791 if (lp->lp_detaching) {
792 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
793 if (llq->llq_ifp == ifp) {
794 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
796 kfree(llq, M_DEVBUF);
797 break; /* Only appears once */
803 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
807 /* Update lagg capabilities */
808 lagg_capabilities(sc);
815 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
817 struct lagg_reqport *rp = (struct lagg_reqport *)data;
818 struct lagg_softc *sc;
819 struct lagg_port *lp = NULL;
822 ASSERT_IFNET_SERIALIZED_ALL(ifp);
824 /* Should be checked by the caller */
825 if (ifp->if_type != IFT_IEEE8023ADLAG ||
826 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
831 if (rp->rp_portname[0] == '\0') {
837 * Release ifp serializers before ifnet_lock
838 * to prevent lock order reversal.
840 ifnet_deserialize_all(ifp);
842 if (ifunit(rp->rp_portname) != ifp) {
844 ifnet_serialize_all(ifp);
849 ifnet_serialize_all(ifp);
852 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
858 lagg_port2req(lp, rp);
863 if (lp->lp_ioctl == NULL) {
868 error = (*lp->lp_ioctl)(ifp, cmd, data, cr);
872 /* Update lagg interface capabilities */
874 lagg_capabilities(sc);
878 if (lp->lp_ioctl == NULL) {
883 error = (*lp->lp_ioctl)(ifp, cmd, data, cr);
886 /* Do not allow the MTU to be changed once joined */
897 if (lp->lp_ioctl != NULL) {
899 result = ((*lp->lp_ioctl)(ifp, cmd, data, cr));
905 * For direct output to child ports.
908 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
909 struct sockaddr *dst, struct rtentry *ro)
911 struct lagg_port *lp = ifp->if_lagg;
913 switch (dst->sa_family) {
914 case pseudo_AF_HDRCMPLT:
916 return ((*lp->lp_output)(ifp, m, dst, ro));
919 /* drop any other frames */
925 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
927 struct lagg_port *lp;
928 struct lagg_softc *sc;
930 if ((lp = ifp->if_lagg) == NULL)
933 /* If the ifnet is just being renamed, don't do anything. */
934 if (ifp->if_flags & IFF_RENAMING)
940 lp->lp_detaching = 1;
941 lagg_port_destroy(lp, 1);
946 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
948 struct lagg_softc *sc = lp->lp_softc;
950 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
951 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
952 rp->rp_prio = lp->lp_prio;
953 rp->rp_flags = lp->lp_flags;
954 if (sc->sc_portreq != NULL)
955 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
957 /* Add protocol specific flags */
958 switch (sc->sc_proto) {
959 case LAGG_PROTO_FAILOVER:
960 if (lp == sc->sc_primary)
961 rp->rp_flags |= LAGG_PORT_MASTER;
962 if (lp == lagg_link_active(sc, sc->sc_primary))
963 rp->rp_flags |= LAGG_PORT_ACTIVE;
966 case LAGG_PROTO_ROUNDROBIN:
967 case LAGG_PROTO_LOADBALANCE:
968 case LAGG_PROTO_ETHERCHANNEL:
969 if (LAGG_PORTACTIVE(lp))
970 rp->rp_flags |= LAGG_PORT_ACTIVE;
973 case LAGG_PROTO_LACP:
974 /* LACP has a different definition of active */
975 if (lacp_isactive(lp))
976 rp->rp_flags |= LAGG_PORT_ACTIVE;
977 if (lacp_iscollecting(lp))
978 rp->rp_flags |= LAGG_PORT_COLLECTING;
979 if (lacp_isdistributing(lp))
980 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
989 struct lagg_softc *sc = (struct lagg_softc *)xsc;
990 struct lagg_port *lp;
991 struct ifnet *ifp = sc->sc_ifp;
993 if (ifp->if_flags & IFF_RUNNING)
998 ifp->if_flags |= IFF_RUNNING;
999 /* Update the port lladdrs */
1000 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1001 lagg_port_lladdr(lp, IF_LLADDR(ifp));
1003 if (sc->sc_init != NULL)
1010 lagg_stop(struct lagg_softc *sc)
1012 struct ifnet *ifp = sc->sc_ifp;
1014 LAGG_WLOCK_ASSERT(sc);
1016 if ((ifp->if_flags & IFF_RUNNING) == 0)
1019 ifp->if_flags &= ~IFF_RUNNING;
1021 if (sc->sc_stop != NULL)
1026 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1028 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1029 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1030 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1031 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1032 struct ifreq *ifr = (struct ifreq *)data;
1033 struct lagg_port *lp;
1035 struct thread *td = curthread;
1037 int count, buflen, len, error = 0;
1039 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1041 bzero(&rpbuf, sizeof(rpbuf));
1047 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1049 buflen = count * sizeof(struct lagg_reqport);
1052 outbuf = kmalloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1055 ra->ra_proto = sc->sc_proto;
1056 if (sc->sc_req != NULL)
1057 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
1061 len = min(ra->ra_size, buflen);
1062 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1063 if (len < sizeof(rpbuf))
1066 lagg_port2req(lp, &rpbuf);
1067 memcpy(buf, &rpbuf, sizeof(rpbuf));
1069 buf += sizeof(rpbuf);
1070 len -= sizeof(rpbuf);
1073 ra->ra_ports = count;
1074 ra->ra_size = count * sizeof(rpbuf);
1075 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1076 kfree(outbuf, M_TEMP);
1079 error = priv_check(td, PRIV_NET_LAGG);
1082 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1083 error = EPROTONOSUPPORT;
1087 if (sc->sc_proto != LAGG_PROTO_NONE) {
1088 /* Reset protocol first in case detach unlocks */
1089 sc->sc_proto = LAGG_PROTO_NONE;
1090 error = sc->sc_detach(sc);
1091 sc->sc_detach = NULL;
1092 sc->sc_start = NULL;
1093 sc->sc_input = NULL;
1094 sc->sc_port_create = NULL;
1095 sc->sc_port_destroy = NULL;
1096 sc->sc_linkstate = NULL;
1099 sc->sc_lladdr = NULL;
1101 sc->sc_portreq = NULL;
1102 } else if (sc->sc_input != NULL) {
1103 /* Still detaching */
1110 for (int i = 0; i < (sizeof(lagg_protos) /
1111 sizeof(lagg_protos[0])); i++) {
1112 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1113 if (sc->sc_ifflags & IFF_DEBUG)
1114 kprintf("%s: using proto %u\n",
1116 lagg_protos[i].ti_proto);
1117 sc->sc_proto = lagg_protos[i].ti_proto;
1118 if (sc->sc_proto != LAGG_PROTO_NONE)
1119 error = lagg_protos[i].ti_attach(sc);
1125 error = EPROTONOSUPPORT;
1127 case SIOCGLAGGFLAGS:
1128 rf->rf_flags = sc->sc_flags;
1131 error = priv_check(td, PRIV_NET_LAGG);
1134 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1139 sc->sc_flags &= ~LAGG_F_HASHMASK;
1140 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1145 * Release ifp serializers before ifnet_lock
1146 * to prevent lock order reversal.
1148 ifnet_deserialize_all(ifp);
1150 if (rp->rp_portname[0] == '\0' ||
1151 (tpif = ifunit(rp->rp_portname)) == NULL) {
1153 ifnet_serialize_all(ifp);
1159 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1160 lp->lp_softc != sc) {
1164 ifnet_serialize_all(ifp);
1168 lagg_port2req(lp, rp);
1171 ifnet_serialize_all(ifp);
1174 error = priv_check(td, PRIV_NET_LAGG);
1178 * Release ifp serializers before ifnet_lock
1179 * to prevent lock order reversal.
1181 ifnet_deserialize_all(ifp);
1183 if (rp->rp_portname[0] == '\0' ||
1184 (tpif = ifunit(rp->rp_portname)) == NULL) {
1186 ifnet_serialize_all(ifp);
1191 error = lagg_port_create(sc, tpif);
1194 ifnet_serialize_all(ifp);
1196 case SIOCSLAGGDELPORT:
1197 error = priv_check(td, PRIV_NET_LAGG);
1201 * Release ifp serializers before ifnet_lock
1202 * to prevent lock order reversal.
1204 ifnet_deserialize_all(ifp);
1206 if (rp->rp_portname[0] == '\0' ||
1207 (tpif = ifunit(rp->rp_portname)) == NULL) {
1209 ifnet_serialize_all(ifp);
1215 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1216 lp->lp_softc != sc) {
1220 ifnet_serialize_all(ifp);
1224 error = lagg_port_destroy(lp, 1);
1227 ifnet_serialize_all(ifp);
1230 /* Set flags on ports too */
1232 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1233 lagg_setflags(lp, 1);
1237 if (!(ifp->if_flags & IFF_UP) &&
1238 (ifp->if_flags & IFF_RUNNING)) {
1240 * If interface is marked down and it is running,
1241 * then stop and disable it.
1246 } else if ((ifp->if_flags & IFF_UP) &&
1247 !(ifp->if_flags & IFF_RUNNING)) {
1249 * If interface is marked up and it is stopped, then
1252 (*ifp->if_init)(sc);
1258 error = lagg_ether_setmulti(sc);
1263 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1268 /* Do not allow the MTU or caps to be directly changed */
1273 error = ether_ioctl(ifp, cmd, data);
1280 lagg_ether_setmulti(struct lagg_softc *sc)
1282 struct lagg_port *lp;
1284 LAGG_WLOCK_ASSERT(sc);
1286 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1287 /* First, remove any existing filter entries. */
1288 lagg_ether_cmdmulti(lp, 0);
1289 /* copy all addresses from the lagg interface to the port */
1290 lagg_ether_cmdmulti(lp, 1);
1296 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1298 struct lagg_softc *sc = lp->lp_softc;
1299 struct ifnet *ifp = lp->lp_ifp;
1300 struct ifnet *scifp = sc->sc_ifp;
1302 struct ifmultiaddr *ifma, *rifma = NULL;
1303 struct sockaddr_dl sdl;
1306 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
1307 LAGG_WLOCK_ASSERT(sc);
1309 bzero((char *)&sdl, sizeof(sdl));
1310 sdl.sdl_len = sizeof(sdl);
1311 sdl.sdl_family = AF_LINK;
1312 sdl.sdl_type = IFT_ETHER;
1313 sdl.sdl_alen = ETHER_ADDR_LEN;
1314 sdl.sdl_index = ifp->if_index;
1317 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1318 if (ifma->ifma_addr->sa_family != AF_LINK)
1320 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1321 LLADDR(&sdl), ETHER_ADDR_LEN);
1323 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1326 mc = kmalloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1329 mc->mc_ifma = rifma;
1330 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1333 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1334 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1335 if_delmulti(ifp, (struct sockaddr *)mc->mc_ifma);
1336 kfree(mc, M_DEVBUF);
1342 /* Handle a ref counted flag that should be set on the lagg port as well */
1344 lagg_setflag(struct lagg_port *lp, int flag, int status,
1345 int (*func)(struct ifnet *, int))
1347 struct lagg_softc *sc = lp->lp_softc;
1348 struct ifnet *scifp = sc->sc_ifp;
1349 struct ifnet *ifp = lp->lp_ifp;
1352 LAGG_WLOCK_ASSERT(sc);
1353 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
1355 status = status ? (scifp->if_flags & flag) : 0;
1356 /* Now "status" contains the flag value or 0 */
1359 * See if recorded ports status is different from what
1360 * we want it to be. If it is, flip it. We record ports
1361 * status in lp_ifflags so that we won't clear ports flag
1362 * we haven't set. In fact, we don't clear or set ports
1363 * flags directly, but get or release references to them.
1364 * That's why we can be sure that recorded flags still are
1365 * in accord with actual ports flags.
1367 if (status != (lp->lp_ifflags & flag)) {
1368 error = (*func)(ifp, status);
1371 lp->lp_ifflags &= ~flag;
1372 lp->lp_ifflags |= status;
1378 * Handle IFF_* flags that require certain changes on the lagg port
1379 * if "status" is true, update ports flags respective to the lagg
1380 * if "status" is false, forcedly clear the flags set on port.
1383 lagg_setflags(struct lagg_port *lp, int status)
1387 ASSERT_IFNET_NOT_SERIALIZED_ALL(lp->lp_ifp);
1389 for (i = 0; lagg_pflags[i].flag; i++) {
1390 error = lagg_setflag(lp, lagg_pflags[i].flag,
1391 status, lagg_pflags[i].func);
1399 #ifdef XXX /* not needed? */
1401 lagg_output(struct ifnet *ifp, struct mbuf *m)
1403 struct lagg_softc *sc = ifp->if_softc;
1404 int error, len, mcast;
1406 len = m->m_pkthdr.len;
1407 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1410 /* We need a Tx algorithm and at least one port */
1411 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1420 error = (*sc->sc_start)(sc, m);
1424 IFNET_STAT_INC(ifp, opackets, 1);
1425 IFNET_STAT_INC(ifp, obytes, len);
1426 ifp->if_omcasts += mcast;
1436 * The ifp->if_qflush entry point for lagg(4) is no-op.
1439 lagg_qflush(struct ifnet *ifp __unused)
1444 lagg_input(struct ifnet *ifp, struct mbuf *m)
1446 struct lagg_port *lp = ifp->if_lagg;
1447 struct lagg_softc *sc = lp->lp_softc;
1448 struct ifnet *scifp = sc->sc_ifp;
1451 if ((scifp->if_flags & IFF_RUNNING) == 0 ||
1452 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1453 sc->sc_proto == LAGG_PROTO_NONE) {
1460 m = (*sc->sc_input)(sc, lp, m);
1465 IFNET_STAT_INC(ifp, ipackets, 1);
1466 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1468 if (scifp->if_flags & IFF_MONITOR) {
1472 ether_reinput_oncpu(scifp, m, REINPUT_RUNBPF);
1477 lagg_media_change(struct ifnet *ifp)
1479 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1481 if (sc->sc_ifflags & IFF_DEBUG)
1482 kprintf("%s\n", __func__);
1488 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1490 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1491 struct lagg_port *lp;
1493 imr->ifm_status = IFM_AVALID;
1494 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1497 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1498 if (LAGG_PORTACTIVE(lp))
1499 imr->ifm_status |= IFM_ACTIVE;
1505 lagg_linkstate(struct lagg_softc *sc)
1507 struct lagg_port *lp;
1508 int new_link = LINK_STATE_DOWN;
1511 /* Our link is considered up if at least one of our ports is active */
1512 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1513 if (lp->lp_link_state == LINK_STATE_UP) {
1514 new_link = LINK_STATE_UP;
1518 if_link_state_change(sc->sc_ifp);
1520 /* Update if_baudrate to reflect the max possible speed */
1521 switch (sc->sc_proto) {
1522 case LAGG_PROTO_FAILOVER:
1523 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1524 sc->sc_primary->lp_ifp->if_baudrate : 0;
1526 case LAGG_PROTO_ROUNDROBIN:
1527 case LAGG_PROTO_LOADBALANCE:
1528 case LAGG_PROTO_ETHERCHANNEL:
1530 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1531 speed += lp->lp_ifp->if_baudrate;
1532 sc->sc_ifp->if_baudrate = speed;
1534 case LAGG_PROTO_LACP:
1535 /* LACP updates if_baudrate itself */
1542 lagg_port_state(struct ifnet *ifp, int state)
1544 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1545 struct lagg_softc *sc = NULL;
1554 if (sc->sc_linkstate != NULL)
1555 (*sc->sc_linkstate)(lp);
1561 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1563 struct lagg_port *lp_next, *rval = NULL;
1564 // int new_link = LINK_STATE_DOWN;
1566 LAGG_RLOCK_ASSERT(sc);
1568 * Search a port which reports an active link state.
1573 if (LAGG_PORTACTIVE(lp)) {
1577 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1578 LAGG_PORTACTIVE(lp_next)) {
1584 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1585 if (LAGG_PORTACTIVE(lp_next)) {
1594 * The IEEE 802.1D standard assumes that a lagg with
1595 * multiple ports is always full duplex. This is valid
1596 * for load sharing laggs and if at least two links
1597 * are active. Unfortunately, checking the latter would
1598 * be too expensive at this point.
1600 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1602 new_link = LINK_STATE_FULL_DUPLEX;
1604 new_link = rval->lp_link_state;
1612 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1614 if (m->m_pkthdr.len < (off + len)) {
1616 } else if (m->m_len < (off + len)) {
1617 m_copydata(m, off, len, buf);
1620 return (mtod(m, char *) + off);
1624 lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1626 struct lagg_softc *sc = (struct lagg_softc *)arg1;
1627 struct lagg_port *lp;
1630 /* LACP tracks active links automatically, the others do not */
1631 if (sc->sc_proto != LAGG_PROTO_LACP) {
1633 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1634 sc->sc_active += LAGG_PORTACTIVE(lp);
1637 error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1638 if ((error) || (req->newptr == NULL))
1645 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1650 struct ether_header *eh;
1651 const struct ether_vlan_header *vlan;
1653 const struct ip *ip;
1654 const uint32_t *ports;
1658 const struct ip6_hdr *ip6;
1668 struct ether_vlan_header vlan;
1676 eh = mtod(m, struct ether_header *);
1677 etype = ntohs(eh->ether_type);
1678 if (sc->sc_flags & LAGG_F_HASHL2) {
1679 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1680 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1683 /* Special handling for encapsulating VLAN frames */
1685 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1686 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1687 sizeof(m->m_pkthdr.ether_vtag), p);
1690 if (etype == ETHERTYPE_VLAN) {
1691 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1695 if (sc->sc_flags & LAGG_F_HASHL2)
1696 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1697 etype = ntohs(vlan->evl_proto);
1698 off += sizeof(*vlan) - sizeof(*eh);
1704 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1708 if (sc->sc_flags & LAGG_F_HASHL3) {
1709 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1710 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1712 if (!(sc->sc_flags & LAGG_F_HASHL4))
1717 iphlen = ip->ip_hl << 2;
1718 if (iphlen < sizeof(*ip))
1721 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1724 p = hash32_buf(ports, sizeof(*ports), p);
1730 case ETHERTYPE_IPV6:
1731 if (!(sc->sc_flags & LAGG_F_HASHL3))
1733 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1737 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1738 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1739 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1740 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1749 lagg_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1751 struct lagg_softc *sc = ifp->if_softc;
1753 struct ifnet *ifp_p;
1754 struct netmsg_packet *nmp;
1757 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1758 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
1760 if (((ifp->if_flags & IFF_RUNNING) == 0)
1761 || (sc->sc_proto == LAGG_PROTO_NONE)
1762 || (sc->sc_count == 0)) {
1770 m = ifsq_dequeue(ifsq);
1774 // Choose output port
1775 ifp_p = (*sc->sc_select_tx_port)(sc, m);
1777 if (ifp_p == NULL) {
1781 p_port = netisr_cpuport(
1782 ifsq_get_cpuid(ifq_get_subq_default(&ifp_p->if_snd)));
1786 nmp = &m->m_hdr.mh_netmsg;
1788 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
1789 0, lagg_start_dispatch);
1791 nmp->base.lmsg.u.ms_resultp = ifp_p;
1793 lwkt_sendmsg(p_port, &nmp->base.lmsg);
1794 IFNET_STAT_INC(ifp, opackets, 1);
1801 lagg_start_dispatch(netmsg_t msg)
1803 struct netmsg_packet *nmp = &msg->packet;
1806 struct altq_pktattr pktattr;
1809 ifp = msg->lmsg.u.ms_resultp;
1813 /* Does altq mix with lacp? */
1814 if (ifq_is_enabled(&ifp->if_snd))
1815 altq_etherclassify(&ifp->if_snd, m, &pktattr);
1817 ifq_dispatch(ifp, m, &pktattr);
1822 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1824 struct altq_pktattr pktattr;
1826 if (ifq_is_enabled(&ifp->if_snd))
1827 altq_etherclassify(&ifp->if_snd, m, &pktattr);
1829 ifq_dispatch(ifp, m, &pktattr);
1834 * Simple round robin aggregation
1837 lagg_rr_attach(struct lagg_softc *sc)
1839 sc->sc_detach = lagg_rr_detach;
1840 sc->sc_input = lagg_rr_input;
1841 sc->sc_select_tx_port = lagg_rr_select_tx_port;
1842 sc->sc_port_create = NULL;
1843 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1850 lagg_rr_detach(struct lagg_softc *sc)
1855 static struct ifnet *
1856 lagg_rr_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
1858 struct lagg_port *lp;
1861 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1863 lp = SLIST_FIRST(&sc->sc_ports);
1865 lp = SLIST_NEXT(lp, lp_entries);
1868 * Check the port's link state. This will return the next active
1869 * port if the link is down or the port is NULL.
1871 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1875 return (lp->lp_ifp);
1878 static struct mbuf *
1879 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1881 struct ifnet *ifp = sc->sc_ifp;
1883 /* Just pass in the packet to our lagg device */
1884 m->m_pkthdr.rcvif = ifp;
1894 lagg_fail_attach(struct lagg_softc *sc)
1896 sc->sc_detach = lagg_fail_detach;
1897 sc->sc_select_tx_port = lagg_fail_select_tx_port;
1898 sc->sc_input = lagg_fail_input;
1899 sc->sc_port_create = NULL;
1900 sc->sc_port_destroy = NULL;
1906 lagg_fail_detach(struct lagg_softc *sc)
1912 lagg_fail_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
1914 struct lagg_port *lp;
1916 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL)
1922 static struct mbuf *
1923 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1925 struct ifnet *ifp = sc->sc_ifp;
1926 struct lagg_port *tmp_tp;
1928 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1929 m->m_pkthdr.rcvif = ifp;
1933 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1934 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1936 * If tmp_tp is null, we've recieved a packet when all
1937 * our links are down. Weird, but process it anyways.
1939 if ((tmp_tp == NULL || tmp_tp == lp)) {
1940 m->m_pkthdr.rcvif = ifp;
1954 lagg_lb_attach(struct lagg_softc *sc)
1956 struct lagg_port *lp;
1959 if ((lb = (struct lagg_lb *)kmalloc(sizeof(struct lagg_lb),
1960 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1963 sc->sc_detach = lagg_lb_detach;
1964 sc->sc_select_tx_port = lagg_lb_select_tx_port;
1965 sc->sc_input = lagg_lb_input;
1966 sc->sc_port_create = lagg_lb_port_create;
1967 sc->sc_port_destroy = lagg_lb_port_destroy;
1968 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1970 lb->lb_key = karc4random();
1971 sc->sc_psc = (caddr_t)lb;
1973 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1974 lagg_lb_port_create(lp);
1980 lagg_lb_detach(struct lagg_softc *sc)
1982 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1984 kfree(lb, M_DEVBUF);
1989 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1991 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1992 struct lagg_port *lp_next;
1995 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1996 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1999 if (i >= LAGG_MAX_PORTS)
2001 if (sc->sc_ifflags & IFF_DEBUG)
2002 kprintf("%s: port %s at index %d\n",
2003 sc->sc_ifname, lp_next->lp_ifname, i);
2004 lb->lb_ports[i++] = lp_next;
2011 lagg_lb_port_create(struct lagg_port *lp)
2013 struct lagg_softc *sc = lp->lp_softc;
2014 return (lagg_lb_porttable(sc, NULL));
2018 lagg_lb_port_destroy(struct lagg_port *lp)
2020 struct lagg_softc *sc = lp->lp_softc;
2021 lagg_lb_porttable(sc, lp);
2026 lagg_lb_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
2028 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2029 struct lagg_port *lp = NULL;
2033 if (sc->use_flowid && (m->m_flags & M_FLOWID))
2034 p = m->m_pkthdr.flowid;
2037 p = lagg_hashmbuf(sc, m, lb->lb_key);
2039 lp = lb->lb_ports[p];
2042 * Check the port's link state. This will return the next active
2043 * port if the link is down or the port is NULL.
2045 if ((lp = lagg_link_active(sc, lp)) == NULL)
2051 static struct mbuf *
2052 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2054 struct ifnet *ifp = sc->sc_ifp;
2056 /* Just pass in the packet to our lagg device */
2057 m->m_pkthdr.rcvif = ifp;
2066 lagg_lacp_attach(struct lagg_softc *sc)
2068 struct lagg_port *lp;
2071 sc->sc_detach = lagg_lacp_detach;
2072 sc->sc_port_create = lacp_port_create;
2073 sc->sc_port_destroy = lacp_port_destroy;
2074 sc->sc_linkstate = lacp_linkstate;
2075 sc->sc_select_tx_port = lagg_lacp_select_tx_port;
2076 sc->sc_input = lagg_lacp_input;
2077 sc->sc_init = lacp_init;
2078 sc->sc_stop = lacp_stop;
2079 sc->sc_lladdr = lagg_lacp_lladdr;
2080 sc->sc_req = lacp_req;
2081 sc->sc_portreq = lacp_portreq;
2083 error = lacp_attach(sc);
2087 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2088 lacp_port_create(lp);
2094 lagg_lacp_detach(struct lagg_softc *sc)
2096 struct lagg_port *lp;
2099 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2100 lacp_port_destroy(lp);
2102 /* unlocking is safe here */
2104 error = lacp_detach(sc);
2111 lagg_lacp_lladdr(struct lagg_softc *sc)
2113 struct lagg_port *lp;
2115 /* purge all the lacp ports */
2116 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2117 lacp_port_destroy(lp);
2119 /* add them back in */
2120 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2121 lacp_port_create(lp);
2125 lagg_lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m)
2127 struct lagg_port *lp;
2128 lp = lacp_select_tx_port(sc, m);
2135 static struct mbuf *
2136 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2138 struct ifnet *ifp = sc->sc_ifp;
2139 struct ether_header *eh;
2142 eh = mtod(m, struct ether_header *);
2143 etype = ntohs(eh->ether_type);
2145 /* Tap off LACP control messages */
2146 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2147 m = lacp_input(lp, m);
2153 * If the port is not collecting or not in the active aggregator then
2156 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2161 m->m_pkthdr.rcvif = ifp;
2166 lagg_callout(void *arg)
2168 struct lagg_softc *sc = (struct lagg_softc *)arg;
2170 struct ifnet *ifp = sc->sc_ifp;
2172 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
2173 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
2174 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
2175 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2178 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);