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>
6 * Copyright (c) 2014 Marcelo Araujo <araujo@FreeBSD.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
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/rmlock.h>
42 #include <sys/taskqueue.h>
43 #include <sys/eventhandler.h>
45 #include <net/ethernet.h>
47 #include <net/if_clone.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.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>
57 #include <netinet/ip.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/if_ether.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_ifattach.h>
70 #include <net/if_vlan_var.h>
71 #include <net/if_lagg.h>
72 #include <net/ieee8023ad_lacp.h>
74 /* Special flags we should propagate to the lagg ports. */
77 int (*func)(struct ifnet *, int);
79 {IFF_PROMISC, ifpromisc},
80 {IFF_ALLMULTI, if_allmulti},
84 SLIST_HEAD(__trhead, lagg_softc) lagg_list; /* list of laggs */
85 static struct mtx lagg_list_mtx;
86 eventhandler_tag lagg_detach_cookie = NULL;
88 static int lagg_clone_create(struct if_clone *, int, caddr_t);
89 static void lagg_clone_destroy(struct ifnet *);
90 static struct if_clone *lagg_cloner;
91 static const char laggname[] = "lagg";
93 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
94 static void lagg_capabilities(struct lagg_softc *);
95 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
96 static void lagg_port_setlladdr(void *, int);
97 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
98 static int lagg_port_destroy(struct lagg_port *, int);
99 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
100 static void lagg_linkstate(struct lagg_softc *);
101 static void lagg_port_state(struct ifnet *, int);
102 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
103 static int lagg_port_output(struct ifnet *, struct mbuf *,
104 const struct sockaddr *, struct route *);
105 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
106 #ifdef LAGG_PORT_STACKING
107 static int lagg_port_checkstacking(struct lagg_softc *);
109 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
110 static void lagg_init(void *);
111 static void lagg_stop(struct lagg_softc *);
112 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
113 static int lagg_ether_setmulti(struct lagg_softc *);
114 static int lagg_ether_cmdmulti(struct lagg_port *, int);
115 static int lagg_setflag(struct lagg_port *, int, int,
116 int (*func)(struct ifnet *, int));
117 static int lagg_setflags(struct lagg_port *, int status);
118 static int lagg_transmit(struct ifnet *, struct mbuf *);
119 static void lagg_qflush(struct ifnet *);
120 static int lagg_media_change(struct ifnet *);
121 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
122 static struct lagg_port *lagg_link_active(struct lagg_softc *,
124 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
125 static int lagg_sysctl_active(SYSCTL_HANDLER_ARGS);
127 /* Simple round robin */
128 static int lagg_rr_attach(struct lagg_softc *);
129 static int lagg_rr_detach(struct lagg_softc *);
130 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
131 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
134 /* Active failover */
135 static int lagg_fail_attach(struct lagg_softc *);
136 static int lagg_fail_detach(struct lagg_softc *);
137 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
138 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
142 static int lagg_lb_attach(struct lagg_softc *);
143 static int lagg_lb_detach(struct lagg_softc *);
144 static int lagg_lb_port_create(struct lagg_port *);
145 static void lagg_lb_port_destroy(struct lagg_port *);
146 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
147 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
149 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
152 static int lagg_bcast_attach(struct lagg_softc *);
153 static int lagg_bcast_detach(struct lagg_softc *);
154 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
155 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
159 static int lagg_lacp_attach(struct lagg_softc *);
160 static int lagg_lacp_detach(struct lagg_softc *);
161 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
162 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
164 static void lagg_lacp_lladdr(struct lagg_softc *);
166 static void lagg_callout(void *);
168 /* lagg protocol table */
169 static const struct {
171 int (*ti_attach)(struct lagg_softc *);
173 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
174 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
175 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
176 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
177 { LAGG_PROTO_LACP, lagg_lacp_attach },
178 { LAGG_PROTO_BROADCAST, lagg_bcast_attach },
179 { LAGG_PROTO_NONE, NULL }
182 SYSCTL_DECL(_net_link);
183 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
186 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
187 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
188 &lagg_failover_rx_all, 0,
189 "Accept input from any interface in a failover lagg");
190 static int def_use_flowid = 1; /* Default value for using M_FLOWID */
191 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
193 "Default setting for using flow id for load sharing");
194 static int def_flowid_shift = 16; /* Default value for using M_FLOWID */
195 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
196 &def_flowid_shift, 0,
197 "Default setting for flowid shift for load sharing");
200 lagg_modevent(module_t mod, int type, void *data)
205 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
206 SLIST_INIT(&lagg_list);
207 lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
208 lagg_clone_destroy, 0);
209 lagg_input_p = lagg_input;
210 lagg_linkstate_p = lagg_port_state;
211 lagg_detach_cookie = EVENTHANDLER_REGISTER(
212 ifnet_departure_event, lagg_port_ifdetach, NULL,
213 EVENTHANDLER_PRI_ANY);
216 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
218 if_clone_detach(lagg_cloner);
220 lagg_linkstate_p = NULL;
221 mtx_destroy(&lagg_list_mtx);
229 static moduledata_t lagg_mod = {
235 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
236 MODULE_VERSION(if_lagg, 1);
239 * This routine is run via an vlan
243 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
245 struct lagg_softc *sc = ifp->if_softc;
246 struct lagg_port *lp;
247 struct rm_priotracker tracker;
249 if (ifp->if_softc != arg) /* Not our event */
252 LAGG_RLOCK(sc, &tracker);
253 if (!SLIST_EMPTY(&sc->sc_ports)) {
254 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
255 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
257 LAGG_RUNLOCK(sc, &tracker);
261 * This routine is run via an vlan
265 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
267 struct lagg_softc *sc = ifp->if_softc;
268 struct lagg_port *lp;
269 struct rm_priotracker tracker;
271 if (ifp->if_softc != arg) /* Not our event */
274 LAGG_RLOCK(sc, &tracker);
275 if (!SLIST_EMPTY(&sc->sc_ports)) {
276 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
277 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
279 LAGG_RUNLOCK(sc, &tracker);
283 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
285 struct lagg_softc *sc;
288 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
289 struct sysctl_oid *oid;
290 char num[14]; /* sufficient for 32 bits */
292 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
293 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
299 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
300 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
301 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
302 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
304 sysctl_ctx_init(&sc->ctx);
305 snprintf(num, sizeof(num), "%u", unit);
306 sc->use_flowid = def_use_flowid;
307 sc->flowid_shift = def_flowid_shift;
308 sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
309 &SYSCTL_NODE_CHILDREN(_net_link, lagg),
310 OID_AUTO, num, CTLFLAG_RD, NULL, "");
311 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
312 "use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid,
313 sc->use_flowid, "Use flow id for load sharing");
314 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
315 "flowid_shift", CTLTYPE_INT|CTLFLAG_RW, &sc->flowid_shift,
317 "Shift flowid bits to prevent multiqueue collisions");
318 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
319 "count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
320 "Total number of ports");
321 SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
322 "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
323 "I", "Total number of active ports");
324 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
325 "flapping", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_flapping,
326 sc->sc_flapping, "Total number of port change events");
327 /* Hash all layers by default */
328 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
330 sc->sc_proto = LAGG_PROTO_NONE;
331 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
332 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
333 sc->sc_proto = lagg_protos[i].ti_proto;
334 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
343 LAGG_CALLOUT_LOCK_INIT(sc);
344 SLIST_INIT(&sc->sc_ports);
345 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
348 * This uses the callout lock rather than the rmlock; one can't
349 * hold said rmlock during SWI.
351 callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0);
353 /* Initialise pseudo media types */
354 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
356 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
357 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
359 if_initname(ifp, laggname, unit);
361 ifp->if_transmit = lagg_transmit;
362 ifp->if_qflush = lagg_qflush;
363 ifp->if_init = lagg_init;
364 ifp->if_ioctl = lagg_ioctl;
365 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
366 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
369 * Attach as an ordinary ethernet device, children will be attached
370 * as special device IFT_IEEE8023ADLAG.
372 ether_ifattach(ifp, eaddr);
374 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
375 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
376 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
377 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
379 /* Insert into the global list of laggs */
380 mtx_lock(&lagg_list_mtx);
381 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
382 mtx_unlock(&lagg_list_mtx);
384 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
390 lagg_clone_destroy(struct ifnet *ifp)
392 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
393 struct lagg_port *lp;
398 ifp->if_flags &= ~IFF_UP;
400 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
401 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
403 /* Shutdown and remove lagg ports */
404 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
405 lagg_port_destroy(lp, 1);
406 /* Unhook the aggregation protocol */
407 if (sc->sc_detach != NULL)
408 (*sc->sc_detach)(sc);
412 sysctl_ctx_free(&sc->ctx);
413 ifmedia_removeall(&sc->sc_media);
417 /* This grabs sc_callout_mtx, serialising it correctly */
418 callout_drain(&sc->sc_callout);
420 /* At this point it's drained; we can free this */
421 counter_u64_free(sc->sc_ipackets);
422 counter_u64_free(sc->sc_opackets);
423 counter_u64_free(sc->sc_ibytes);
424 counter_u64_free(sc->sc_obytes);
426 mtx_lock(&lagg_list_mtx);
427 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
428 mtx_unlock(&lagg_list_mtx);
430 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
431 LAGG_LOCK_DESTROY(sc);
432 LAGG_CALLOUT_LOCK_DESTROY(sc);
437 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
439 struct ifnet *ifp = sc->sc_ifp;
441 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
444 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
445 /* Let the protocol know the MAC has changed */
446 if (sc->sc_lladdr != NULL)
447 (*sc->sc_lladdr)(sc);
448 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
452 lagg_capabilities(struct lagg_softc *sc)
454 struct lagg_port *lp;
455 int cap = ~0, ena = ~0;
457 struct ifnet_hw_tsomax hw_tsomax;
459 LAGG_WLOCK_ASSERT(sc);
461 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
463 /* Get capabilities from the lagg ports */
464 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
465 cap &= lp->lp_ifp->if_capabilities;
466 ena &= lp->lp_ifp->if_capenable;
467 hwa &= lp->lp_ifp->if_hwassist;
468 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
470 cap = (cap == ~0 ? 0 : cap);
471 ena = (ena == ~0 ? 0 : ena);
472 hwa = (hwa == ~0 ? 0 : hwa);
474 if (sc->sc_ifp->if_capabilities != cap ||
475 sc->sc_ifp->if_capenable != ena ||
476 sc->sc_ifp->if_hwassist != hwa ||
477 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
478 sc->sc_ifp->if_capabilities = cap;
479 sc->sc_ifp->if_capenable = ena;
480 sc->sc_ifp->if_hwassist = hwa;
481 getmicrotime(&sc->sc_ifp->if_lastchange);
483 if (sc->sc_ifflags & IFF_DEBUG)
484 if_printf(sc->sc_ifp,
485 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
490 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
492 struct lagg_softc *sc = lp->lp_softc;
493 struct ifnet *ifp = lp->lp_ifp;
494 struct lagg_llq *llq;
497 LAGG_WLOCK_ASSERT(sc);
499 if (lp->lp_detaching ||
500 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
503 /* Check to make sure its not already queued to be changed */
504 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
505 if (llq->llq_ifp == ifp) {
512 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
513 if (llq == NULL) /* XXX what to do */
517 /* Update the lladdr even if pending, it may have changed */
519 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
522 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
524 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
528 * Set the interface MAC address from a taskqueue to avoid a LOR.
531 lagg_port_setlladdr(void *arg, int pending)
533 struct lagg_softc *sc = (struct lagg_softc *)arg;
534 struct lagg_llq *llq, *head;
538 /* Grab a local reference of the queue and remove it from the softc */
540 head = SLIST_FIRST(&sc->sc_llq_head);
541 SLIST_FIRST(&sc->sc_llq_head) = NULL;
545 * Traverse the queue and set the lladdr on each ifp. It is safe to do
546 * unlocked as we have the only reference to it.
548 for (llq = head; llq != NULL; llq = head) {
551 /* Set the link layer address */
552 CURVNET_SET(ifp->if_vnet);
553 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
556 printf("%s: setlladdr failed on %s\n", __func__,
559 head = SLIST_NEXT(llq, llq_entries);
565 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
567 struct lagg_softc *sc_ptr;
568 struct lagg_port *lp;
571 LAGG_WLOCK_ASSERT(sc);
573 /* Limit the maximal number of lagg ports */
574 if (sc->sc_count >= LAGG_MAX_PORTS)
577 /* Check if port has already been associated to a lagg */
578 if (ifp->if_lagg != NULL) {
579 /* Port is already in the current lagg? */
580 lp = (struct lagg_port *)ifp->if_lagg;
581 if (lp->lp_softc == sc)
586 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
587 if (ifp->if_type != IFT_ETHER)
588 return (EPROTONOSUPPORT);
592 * The member interface should not have inet6 address because
593 * two interfaces with a valid link-local scope zone must not be
594 * merged in any form. This restriction is needed to
595 * prevent violation of link-local scope zone. Attempts to
596 * add a member interface which has inet6 addresses triggers
597 * removal of all inet6 addresses on the member interface.
599 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
600 if (in6ifa_llaonifp(lp->lp_ifp)) {
601 in6_ifdetach(lp->lp_ifp);
602 if_printf(sc->sc_ifp,
603 "IPv6 addresses on %s have been removed "
604 "before adding it as a member to prevent "
605 "IPv6 address scope violation.\n",
606 lp->lp_ifp->if_xname);
609 if (in6ifa_llaonifp(ifp)) {
611 if_printf(sc->sc_ifp,
612 "IPv6 addresses on %s have been removed "
613 "before adding it as a member to prevent "
614 "IPv6 address scope violation.\n",
618 /* Allow the first Ethernet member to define the MTU */
619 if (SLIST_EMPTY(&sc->sc_ports))
620 sc->sc_ifp->if_mtu = ifp->if_mtu;
621 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
622 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
627 if ((lp = malloc(sizeof(struct lagg_port),
628 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
631 /* Check if port is a stacked lagg */
632 mtx_lock(&lagg_list_mtx);
633 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
634 if (ifp == sc_ptr->sc_ifp) {
635 mtx_unlock(&lagg_list_mtx);
638 /* XXX disable stacking for the moment, its untested */
639 #ifdef LAGG_PORT_STACKING
640 lp->lp_flags |= LAGG_PORT_STACK;
641 if (lagg_port_checkstacking(sc_ptr) >=
643 mtx_unlock(&lagg_list_mtx);
650 mtx_unlock(&lagg_list_mtx);
652 /* Change the interface type */
653 lp->lp_iftype = ifp->if_type;
654 ifp->if_type = IFT_IEEE8023ADLAG;
656 lp->lp_ioctl = ifp->if_ioctl;
657 ifp->if_ioctl = lagg_port_ioctl;
658 lp->lp_output = ifp->if_output;
659 ifp->if_output = lagg_port_output;
664 /* Save port link layer address */
665 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
667 if (SLIST_EMPTY(&sc->sc_ports)) {
669 lagg_lladdr(sc, IF_LLADDR(ifp));
671 /* Update link layer address for this port */
672 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
675 /* Insert into the list of ports */
676 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
679 /* Update lagg capabilities */
680 lagg_capabilities(sc);
683 /* Add multicast addresses and interface flags to this port */
684 lagg_ether_cmdmulti(lp, 1);
685 lagg_setflags(lp, 1);
687 if (sc->sc_port_create != NULL)
688 error = (*sc->sc_port_create)(lp);
690 /* remove the port again, without calling sc_port_destroy */
691 lagg_port_destroy(lp, 0);
698 #ifdef LAGG_PORT_STACKING
700 lagg_port_checkstacking(struct lagg_softc *sc)
702 struct lagg_softc *sc_ptr;
703 struct lagg_port *lp;
706 LAGG_WLOCK_ASSERT(sc);
708 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
709 if (lp->lp_flags & LAGG_PORT_STACK) {
710 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
711 m = MAX(m, lagg_port_checkstacking(sc_ptr));
720 lagg_port_destroy(struct lagg_port *lp, int runpd)
722 struct lagg_softc *sc = lp->lp_softc;
723 struct lagg_port *lp_ptr;
724 struct lagg_llq *llq;
725 struct ifnet *ifp = lp->lp_ifp;
727 LAGG_WLOCK_ASSERT(sc);
729 if (runpd && sc->sc_port_destroy != NULL)
730 (*sc->sc_port_destroy)(lp);
733 * Remove multicast addresses and interface flags from this port and
734 * reset the MAC address, skip if the interface is being detached.
736 if (!lp->lp_detaching) {
737 lagg_ether_cmdmulti(lp, 0);
738 lagg_setflags(lp, 0);
739 lagg_port_lladdr(lp, lp->lp_lladdr);
742 /* Restore interface */
743 ifp->if_type = lp->lp_iftype;
744 ifp->if_ioctl = lp->lp_ioctl;
745 ifp->if_output = lp->lp_output;
748 /* Finally, remove the port from the lagg */
749 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
752 /* Update the primary interface */
753 if (lp == sc->sc_primary) {
754 uint8_t lladdr[ETHER_ADDR_LEN];
756 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
757 bzero(&lladdr, ETHER_ADDR_LEN);
759 bcopy(lp_ptr->lp_lladdr,
760 lladdr, ETHER_ADDR_LEN);
762 lagg_lladdr(sc, lladdr);
763 sc->sc_primary = lp_ptr;
765 /* Update link layer address for each port */
766 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
767 lagg_port_lladdr(lp_ptr, lladdr);
770 /* Remove any pending lladdr changes from the queue */
771 if (lp->lp_detaching) {
772 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
773 if (llq->llq_ifp == ifp) {
774 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
777 break; /* Only appears once */
783 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
787 /* Update lagg capabilities */
788 lagg_capabilities(sc);
795 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
797 struct lagg_reqport *rp = (struct lagg_reqport *)data;
798 struct lagg_softc *sc;
799 struct lagg_port *lp = NULL;
801 struct rm_priotracker tracker;
803 /* Should be checked by the caller */
804 if (ifp->if_type != IFT_IEEE8023ADLAG ||
805 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
810 if (rp->rp_portname[0] == '\0' ||
811 ifunit(rp->rp_portname) != ifp) {
816 LAGG_RLOCK(sc, &tracker);
817 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
819 LAGG_RUNLOCK(sc, &tracker);
823 lagg_port2req(lp, rp);
824 LAGG_RUNLOCK(sc, &tracker);
828 if (lp->lp_ioctl == NULL) {
832 error = (*lp->lp_ioctl)(ifp, cmd, data);
836 /* Update lagg interface capabilities */
838 lagg_capabilities(sc);
843 /* Do not allow the MTU to be changed once joined */
854 if (lp->lp_ioctl != NULL)
855 return ((*lp->lp_ioctl)(ifp, cmd, data));
861 * For direct output to child ports.
864 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
865 const struct sockaddr *dst, struct route *ro)
867 struct lagg_port *lp = ifp->if_lagg;
869 switch (dst->sa_family) {
870 case pseudo_AF_HDRCMPLT:
872 return ((*lp->lp_output)(ifp, m, dst, ro));
875 /* drop any other frames */
881 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
883 struct lagg_port *lp;
884 struct lagg_softc *sc;
886 if ((lp = ifp->if_lagg) == NULL)
888 /* If the ifnet is just being renamed, don't do anything. */
889 if (ifp->if_flags & IFF_RENAMING)
895 lp->lp_detaching = 1;
896 lagg_port_destroy(lp, 1);
901 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
903 struct lagg_softc *sc = lp->lp_softc;
905 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
906 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
907 rp->rp_prio = lp->lp_prio;
908 rp->rp_flags = lp->lp_flags;
909 if (sc->sc_portreq != NULL)
910 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
912 /* Add protocol specific flags */
913 switch (sc->sc_proto) {
914 case LAGG_PROTO_FAILOVER:
915 if (lp == sc->sc_primary)
916 rp->rp_flags |= LAGG_PORT_MASTER;
917 if (lp == lagg_link_active(sc, sc->sc_primary))
918 rp->rp_flags |= LAGG_PORT_ACTIVE;
921 case LAGG_PROTO_ROUNDROBIN:
922 case LAGG_PROTO_LOADBALANCE:
923 case LAGG_PROTO_ETHERCHANNEL:
924 case LAGG_PROTO_BROADCAST:
925 if (LAGG_PORTACTIVE(lp))
926 rp->rp_flags |= LAGG_PORT_ACTIVE;
929 case LAGG_PROTO_LACP:
930 /* LACP has a different definition of active */
931 if (lacp_isactive(lp))
932 rp->rp_flags |= LAGG_PORT_ACTIVE;
933 if (lacp_iscollecting(lp))
934 rp->rp_flags |= LAGG_PORT_COLLECTING;
935 if (lacp_isdistributing(lp))
936 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
945 struct lagg_softc *sc = (struct lagg_softc *)xsc;
946 struct lagg_port *lp;
947 struct ifnet *ifp = sc->sc_ifp;
949 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
954 ifp->if_drv_flags |= IFF_DRV_RUNNING;
955 /* Update the port lladdrs */
956 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
957 lagg_port_lladdr(lp, IF_LLADDR(ifp));
959 if (sc->sc_init != NULL)
966 lagg_stop(struct lagg_softc *sc)
968 struct ifnet *ifp = sc->sc_ifp;
970 LAGG_WLOCK_ASSERT(sc);
972 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
975 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
977 if (sc->sc_stop != NULL)
982 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
984 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
985 struct lagg_reqall *ra = (struct lagg_reqall *)data;
986 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
987 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
988 struct ifreq *ifr = (struct ifreq *)data;
989 struct lagg_port *lp;
991 struct thread *td = curthread;
993 int count, buflen, len, error = 0;
994 struct rm_priotracker tracker;
996 bzero(&rpbuf, sizeof(rpbuf));
1000 LAGG_RLOCK(sc, &tracker);
1002 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1004 buflen = count * sizeof(struct lagg_reqport);
1005 LAGG_RUNLOCK(sc, &tracker);
1007 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1009 LAGG_RLOCK(sc, &tracker);
1010 ra->ra_proto = sc->sc_proto;
1011 if (sc->sc_req != NULL)
1012 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
1016 len = min(ra->ra_size, buflen);
1017 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1018 if (len < sizeof(rpbuf))
1021 lagg_port2req(lp, &rpbuf);
1022 memcpy(buf, &rpbuf, sizeof(rpbuf));
1024 buf += sizeof(rpbuf);
1025 len -= sizeof(rpbuf);
1027 LAGG_RUNLOCK(sc, &tracker);
1028 ra->ra_ports = count;
1029 ra->ra_size = count * sizeof(rpbuf);
1030 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1031 free(outbuf, M_TEMP);
1034 error = priv_check(td, PRIV_NET_LAGG);
1037 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1038 error = EPROTONOSUPPORT;
1042 if (sc->sc_proto != LAGG_PROTO_NONE) {
1043 /* Reset protocol first in case detach unlocks */
1044 sc->sc_proto = LAGG_PROTO_NONE;
1045 error = sc->sc_detach(sc);
1046 sc->sc_detach = NULL;
1047 sc->sc_start = NULL;
1048 sc->sc_input = NULL;
1049 sc->sc_port_create = NULL;
1050 sc->sc_port_destroy = NULL;
1051 sc->sc_linkstate = NULL;
1054 sc->sc_lladdr = NULL;
1056 sc->sc_portreq = NULL;
1057 } else if (sc->sc_input != NULL) {
1058 /* Still detaching */
1065 for (int i = 0; i < (sizeof(lagg_protos) /
1066 sizeof(lagg_protos[0])); i++) {
1067 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1068 if (sc->sc_ifflags & IFF_DEBUG)
1069 printf("%s: using proto %u\n",
1071 lagg_protos[i].ti_proto);
1072 sc->sc_proto = lagg_protos[i].ti_proto;
1073 if (sc->sc_proto != LAGG_PROTO_NONE)
1074 error = lagg_protos[i].ti_attach(sc);
1080 error = EPROTONOSUPPORT;
1082 case SIOCGLAGGFLAGS:
1083 rf->rf_flags = sc->sc_flags;
1086 error = priv_check(td, PRIV_NET_LAGG);
1089 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1094 sc->sc_flags &= ~LAGG_F_HASHMASK;
1095 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1099 if (rp->rp_portname[0] == '\0' ||
1100 (tpif = ifunit(rp->rp_portname)) == NULL) {
1105 LAGG_RLOCK(sc, &tracker);
1106 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1107 lp->lp_softc != sc) {
1109 LAGG_RUNLOCK(sc, &tracker);
1113 lagg_port2req(lp, rp);
1114 LAGG_RUNLOCK(sc, &tracker);
1117 error = priv_check(td, PRIV_NET_LAGG);
1120 if (rp->rp_portname[0] == '\0' ||
1121 (tpif = ifunit(rp->rp_portname)) == NULL) {
1126 error = lagg_port_create(sc, tpif);
1129 case SIOCSLAGGDELPORT:
1130 error = priv_check(td, PRIV_NET_LAGG);
1133 if (rp->rp_portname[0] == '\0' ||
1134 (tpif = ifunit(rp->rp_portname)) == NULL) {
1140 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1141 lp->lp_softc != sc) {
1147 error = lagg_port_destroy(lp, 1);
1151 /* Set flags on ports too */
1153 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1154 lagg_setflags(lp, 1);
1158 if (!(ifp->if_flags & IFF_UP) &&
1159 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1161 * If interface is marked down and it is running,
1162 * then stop and disable it.
1167 } else if ((ifp->if_flags & IFF_UP) &&
1168 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1170 * If interface is marked up and it is stopped, then
1173 (*ifp->if_init)(sc);
1179 error = lagg_ether_setmulti(sc);
1184 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1189 /* Do not allow the MTU or caps to be directly changed */
1194 error = ether_ioctl(ifp, cmd, data);
1201 lagg_ether_setmulti(struct lagg_softc *sc)
1203 struct lagg_port *lp;
1205 LAGG_WLOCK_ASSERT(sc);
1207 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1208 /* First, remove any existing filter entries. */
1209 lagg_ether_cmdmulti(lp, 0);
1210 /* copy all addresses from the lagg interface to the port */
1211 lagg_ether_cmdmulti(lp, 1);
1217 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1219 struct lagg_softc *sc = lp->lp_softc;
1220 struct ifnet *ifp = lp->lp_ifp;
1221 struct ifnet *scifp = sc->sc_ifp;
1223 struct ifmultiaddr *ifma;
1226 LAGG_WLOCK_ASSERT(sc);
1229 IF_ADDR_WLOCK(scifp);
1230 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1231 if (ifma->ifma_addr->sa_family != AF_LINK)
1233 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1235 IF_ADDR_WUNLOCK(scifp);
1238 bcopy(ifma->ifma_addr, &mc->mc_addr,
1239 ifma->ifma_addr->sa_len);
1240 mc->mc_addr.sdl_index = ifp->if_index;
1242 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1244 IF_ADDR_WUNLOCK(scifp);
1245 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1246 error = if_addmulti(ifp,
1247 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1252 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1253 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1254 if (mc->mc_ifma && !lp->lp_detaching)
1255 if_delmulti_ifma(mc->mc_ifma);
1262 /* Handle a ref counted flag that should be set on the lagg port as well */
1264 lagg_setflag(struct lagg_port *lp, int flag, int status,
1265 int (*func)(struct ifnet *, int))
1267 struct lagg_softc *sc = lp->lp_softc;
1268 struct ifnet *scifp = sc->sc_ifp;
1269 struct ifnet *ifp = lp->lp_ifp;
1272 LAGG_WLOCK_ASSERT(sc);
1274 status = status ? (scifp->if_flags & flag) : 0;
1275 /* Now "status" contains the flag value or 0 */
1278 * See if recorded ports status is different from what
1279 * we want it to be. If it is, flip it. We record ports
1280 * status in lp_ifflags so that we won't clear ports flag
1281 * we haven't set. In fact, we don't clear or set ports
1282 * flags directly, but get or release references to them.
1283 * That's why we can be sure that recorded flags still are
1284 * in accord with actual ports flags.
1286 if (status != (lp->lp_ifflags & flag)) {
1287 error = (*func)(ifp, status);
1290 lp->lp_ifflags &= ~flag;
1291 lp->lp_ifflags |= status;
1297 * Handle IFF_* flags that require certain changes on the lagg port
1298 * if "status" is true, update ports flags respective to the lagg
1299 * if "status" is false, forcedly clear the flags set on port.
1302 lagg_setflags(struct lagg_port *lp, int status)
1306 for (i = 0; lagg_pflags[i].flag; i++) {
1307 error = lagg_setflag(lp, lagg_pflags[i].flag,
1308 status, lagg_pflags[i].func);
1316 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1318 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1319 int error, len, mcast;
1320 struct rm_priotracker tracker;
1322 len = m->m_pkthdr.len;
1323 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1325 LAGG_RLOCK(sc, &tracker);
1326 /* We need a Tx algorithm and at least one port */
1327 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1328 LAGG_RUNLOCK(sc, &tracker);
1334 ETHER_BPF_MTAP(ifp, m);
1336 error = (*sc->sc_start)(sc, m);
1337 LAGG_RUNLOCK(sc, &tracker);
1340 counter_u64_add(sc->sc_opackets, 1);
1341 counter_u64_add(sc->sc_obytes, len);
1342 ifp->if_omcasts += mcast;
1350 * The ifp->if_qflush entry point for lagg(4) is no-op.
1353 lagg_qflush(struct ifnet *ifp __unused)
1357 static struct mbuf *
1358 lagg_input(struct ifnet *ifp, struct mbuf *m)
1360 struct lagg_port *lp = ifp->if_lagg;
1361 struct lagg_softc *sc = lp->lp_softc;
1362 struct ifnet *scifp = sc->sc_ifp;
1363 struct rm_priotracker tracker;
1365 LAGG_RLOCK(sc, &tracker);
1366 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1367 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1368 sc->sc_proto == LAGG_PROTO_NONE) {
1369 LAGG_RUNLOCK(sc, &tracker);
1374 ETHER_BPF_MTAP(scifp, m);
1376 m = (*sc->sc_input)(sc, lp, m);
1379 counter_u64_add(sc->sc_ipackets, 1);
1380 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1382 if (scifp->if_flags & IFF_MONITOR) {
1388 LAGG_RUNLOCK(sc, &tracker);
1393 lagg_media_change(struct ifnet *ifp)
1395 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1397 if (sc->sc_ifflags & IFF_DEBUG)
1398 printf("%s\n", __func__);
1405 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1407 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1408 struct lagg_port *lp;
1409 struct rm_priotracker tracker;
1411 imr->ifm_status = IFM_AVALID;
1412 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1414 LAGG_RLOCK(sc, &tracker);
1415 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1416 if (LAGG_PORTACTIVE(lp))
1417 imr->ifm_status |= IFM_ACTIVE;
1419 LAGG_RUNLOCK(sc, &tracker);
1423 lagg_linkstate(struct lagg_softc *sc)
1425 struct lagg_port *lp;
1426 int new_link = LINK_STATE_DOWN;
1429 /* Our link is considered up if at least one of our ports is active */
1430 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1431 if (lp->lp_link_state == LINK_STATE_UP) {
1432 new_link = LINK_STATE_UP;
1436 if_link_state_change(sc->sc_ifp, new_link);
1438 /* Update if_baudrate to reflect the max possible speed */
1439 switch (sc->sc_proto) {
1440 case LAGG_PROTO_FAILOVER:
1441 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1442 sc->sc_primary->lp_ifp->if_baudrate : 0;
1444 case LAGG_PROTO_ROUNDROBIN:
1445 case LAGG_PROTO_LOADBALANCE:
1446 case LAGG_PROTO_ETHERCHANNEL:
1447 case LAGG_PROTO_BROADCAST:
1449 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1450 speed += lp->lp_ifp->if_baudrate;
1451 sc->sc_ifp->if_baudrate = speed;
1453 case LAGG_PROTO_LACP:
1454 /* LACP updates if_baudrate itself */
1460 lagg_port_state(struct ifnet *ifp, int state)
1462 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1463 struct lagg_softc *sc = NULL;
1472 if (sc->sc_linkstate != NULL)
1473 (*sc->sc_linkstate)(lp);
1478 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1480 struct lagg_port *lp_next, *rval = NULL;
1481 // int new_link = LINK_STATE_DOWN;
1483 LAGG_RLOCK_ASSERT(sc);
1485 * Search a port which reports an active link state.
1490 if (LAGG_PORTACTIVE(lp)) {
1494 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1495 LAGG_PORTACTIVE(lp_next)) {
1501 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1502 if (LAGG_PORTACTIVE(lp_next)) {
1511 * The IEEE 802.1D standard assumes that a lagg with
1512 * multiple ports is always full duplex. This is valid
1513 * for load sharing laggs and if at least two links
1514 * are active. Unfortunately, checking the latter would
1515 * be too expensive at this point.
1517 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1519 new_link = LINK_STATE_FULL_DUPLEX;
1521 new_link = rval->lp_link_state;
1529 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1531 if (m->m_pkthdr.len < (off + len)) {
1533 } else if (m->m_len < (off + len)) {
1534 m_copydata(m, off, len, buf);
1537 return (mtod(m, char *) + off);
1541 lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1543 struct lagg_softc *sc = (struct lagg_softc *)arg1;
1544 struct lagg_port *lp;
1547 /* LACP tracks active links automatically, the others do not */
1548 if (sc->sc_proto != LAGG_PROTO_LACP) {
1550 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1551 sc->sc_active += LAGG_PORTACTIVE(lp);
1554 error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1555 if ((error) || (req->newptr == NULL))
1562 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1567 struct ether_header *eh;
1568 const struct ether_vlan_header *vlan;
1570 const struct ip *ip;
1571 const uint32_t *ports;
1575 const struct ip6_hdr *ip6;
1585 struct ether_vlan_header vlan;
1593 eh = mtod(m, struct ether_header *);
1594 etype = ntohs(eh->ether_type);
1595 if (sc->sc_flags & LAGG_F_HASHL2) {
1596 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1597 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1600 /* Special handling for encapsulating VLAN frames */
1601 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1602 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1603 sizeof(m->m_pkthdr.ether_vtag), p);
1604 } else if (etype == ETHERTYPE_VLAN) {
1605 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1609 if (sc->sc_flags & LAGG_F_HASHL2)
1610 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1611 etype = ntohs(vlan->evl_proto);
1612 off += sizeof(*vlan) - sizeof(*eh);
1618 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1622 if (sc->sc_flags & LAGG_F_HASHL3) {
1623 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1624 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1626 if (!(sc->sc_flags & LAGG_F_HASHL4))
1632 iphlen = ip->ip_hl << 2;
1633 if (iphlen < sizeof(*ip))
1636 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1639 p = hash32_buf(ports, sizeof(*ports), p);
1645 case ETHERTYPE_IPV6:
1646 if (!(sc->sc_flags & LAGG_F_HASHL3))
1648 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1652 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1653 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1654 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1655 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1664 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1667 return (ifp->if_transmit)(ifp, m);
1671 * Simple round robin aggregation
1675 lagg_rr_attach(struct lagg_softc *sc)
1677 sc->sc_detach = lagg_rr_detach;
1678 sc->sc_start = lagg_rr_start;
1679 sc->sc_input = lagg_rr_input;
1680 sc->sc_port_create = NULL;
1681 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1688 lagg_rr_detach(struct lagg_softc *sc)
1694 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1696 struct lagg_port *lp;
1699 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1701 lp = SLIST_FIRST(&sc->sc_ports);
1703 lp = SLIST_NEXT(lp, lp_entries);
1706 * Check the port's link state. This will return the next active
1707 * port if the link is down or the port is NULL.
1709 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1715 return (lagg_enqueue(lp->lp_ifp, m));
1718 static struct mbuf *
1719 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1721 struct ifnet *ifp = sc->sc_ifp;
1723 /* Just pass in the packet to our lagg device */
1724 m->m_pkthdr.rcvif = ifp;
1734 lagg_bcast_attach(struct lagg_softc *sc)
1736 sc->sc_detach = lagg_bcast_detach;
1737 sc->sc_start = lagg_bcast_start;
1738 sc->sc_input = lagg_bcast_input;
1739 sc->sc_port_create = NULL;
1740 sc->sc_port_destroy = NULL;
1741 sc->sc_linkstate = NULL;
1743 sc->sc_portreq = NULL;
1749 lagg_bcast_detach(struct lagg_softc *sc)
1755 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1757 int active_ports = 0;
1760 struct lagg_port *lp, *last = NULL;
1763 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1764 if (!LAGG_PORTACTIVE(lp))
1770 m0 = m_copym(m, 0, M_COPYALL,
1778 ret = lagg_enqueue(last->lp_ifp, m0);
1788 if ((last = lagg_link_active(sc, last)) == NULL) {
1793 ret = lagg_enqueue(last->lp_ifp, m);
1804 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp,
1807 struct ifnet *ifp = sc->sc_ifp;
1809 /* Just pass in the packet to our lagg device */
1810 m->m_pkthdr.rcvif = ifp;
1819 lagg_fail_attach(struct lagg_softc *sc)
1821 sc->sc_detach = lagg_fail_detach;
1822 sc->sc_start = lagg_fail_start;
1823 sc->sc_input = lagg_fail_input;
1824 sc->sc_port_create = NULL;
1825 sc->sc_port_destroy = NULL;
1831 lagg_fail_detach(struct lagg_softc *sc)
1837 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1839 struct lagg_port *lp;
1841 /* Use the master port if active or the next available port */
1842 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1848 return (lagg_enqueue(lp->lp_ifp, m));
1851 static struct mbuf *
1852 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1854 struct ifnet *ifp = sc->sc_ifp;
1855 struct lagg_port *tmp_tp;
1857 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1858 m->m_pkthdr.rcvif = ifp;
1862 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1863 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1865 * If tmp_tp is null, we've recieved a packet when all
1866 * our links are down. Weird, but process it anyways.
1868 if ((tmp_tp == NULL || tmp_tp == lp)) {
1869 m->m_pkthdr.rcvif = ifp;
1883 lagg_lb_attach(struct lagg_softc *sc)
1885 struct lagg_port *lp;
1888 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1889 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1892 sc->sc_detach = lagg_lb_detach;
1893 sc->sc_start = lagg_lb_start;
1894 sc->sc_input = lagg_lb_input;
1895 sc->sc_port_create = lagg_lb_port_create;
1896 sc->sc_port_destroy = lagg_lb_port_destroy;
1897 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1899 lb->lb_key = arc4random();
1900 sc->sc_psc = (caddr_t)lb;
1902 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1903 lagg_lb_port_create(lp);
1909 lagg_lb_detach(struct lagg_softc *sc)
1911 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1918 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1920 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1921 struct lagg_port *lp_next;
1924 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1925 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1928 if (i >= LAGG_MAX_PORTS)
1930 if (sc->sc_ifflags & IFF_DEBUG)
1931 printf("%s: port %s at index %d\n",
1932 sc->sc_ifname, lp_next->lp_ifname, i);
1933 lb->lb_ports[i++] = lp_next;
1940 lagg_lb_port_create(struct lagg_port *lp)
1942 struct lagg_softc *sc = lp->lp_softc;
1943 return (lagg_lb_porttable(sc, NULL));
1947 lagg_lb_port_destroy(struct lagg_port *lp)
1949 struct lagg_softc *sc = lp->lp_softc;
1950 lagg_lb_porttable(sc, lp);
1954 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1956 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1957 struct lagg_port *lp = NULL;
1960 if (sc->use_flowid && (m->m_flags & M_FLOWID))
1961 p = m->m_pkthdr.flowid >> sc->flowid_shift;
1963 p = lagg_hashmbuf(sc, m, lb->lb_key);
1965 lp = lb->lb_ports[p];
1968 * Check the port's link state. This will return the next active
1969 * port if the link is down or the port is NULL.
1971 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1977 return (lagg_enqueue(lp->lp_ifp, m));
1980 static struct mbuf *
1981 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1983 struct ifnet *ifp = sc->sc_ifp;
1985 /* Just pass in the packet to our lagg device */
1986 m->m_pkthdr.rcvif = ifp;
1996 lagg_lacp_attach(struct lagg_softc *sc)
1998 struct lagg_port *lp;
2001 sc->sc_detach = lagg_lacp_detach;
2002 sc->sc_port_create = lacp_port_create;
2003 sc->sc_port_destroy = lacp_port_destroy;
2004 sc->sc_linkstate = lacp_linkstate;
2005 sc->sc_start = lagg_lacp_start;
2006 sc->sc_input = lagg_lacp_input;
2007 sc->sc_init = lacp_init;
2008 sc->sc_stop = lacp_stop;
2009 sc->sc_lladdr = lagg_lacp_lladdr;
2010 sc->sc_req = lacp_req;
2011 sc->sc_portreq = lacp_portreq;
2013 error = lacp_attach(sc);
2017 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2018 lacp_port_create(lp);
2024 lagg_lacp_detach(struct lagg_softc *sc)
2026 struct lagg_port *lp;
2029 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2030 lacp_port_destroy(lp);
2032 /* unlocking is safe here */
2034 error = lacp_detach(sc);
2041 lagg_lacp_lladdr(struct lagg_softc *sc)
2043 struct lagg_port *lp;
2045 /* purge all the lacp ports */
2046 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2047 lacp_port_destroy(lp);
2049 /* add them back in */
2050 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2051 lacp_port_create(lp);
2055 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2057 struct lagg_port *lp;
2059 lp = lacp_select_tx_port(sc, m);
2066 return (lagg_enqueue(lp->lp_ifp, m));
2069 static struct mbuf *
2070 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2072 struct ifnet *ifp = sc->sc_ifp;
2073 struct ether_header *eh;
2076 eh = mtod(m, struct ether_header *);
2077 etype = ntohs(eh->ether_type);
2079 /* Tap off LACP control messages */
2080 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2081 m = lacp_input(lp, m);
2087 * If the port is not collecting or not in the active aggregator then
2090 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2095 m->m_pkthdr.rcvif = ifp;
2100 lagg_callout(void *arg)
2102 struct lagg_softc *sc = (struct lagg_softc *)arg;
2103 struct ifnet *ifp = sc->sc_ifp;
2105 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
2106 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
2107 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
2108 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2110 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);