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/rmlock.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>
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_lacp_attach(struct lagg_softc *);
153 static int lagg_lacp_detach(struct lagg_softc *);
154 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
155 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
157 static void lagg_lacp_lladdr(struct lagg_softc *);
159 static void lagg_callout(void *);
161 /* lagg protocol table */
162 static const struct {
164 int (*ti_attach)(struct lagg_softc *);
166 { LAGG_PROTO_ROUNDROBIN, lagg_rr_attach },
167 { LAGG_PROTO_FAILOVER, lagg_fail_attach },
168 { LAGG_PROTO_LOADBALANCE, lagg_lb_attach },
169 { LAGG_PROTO_ETHERCHANNEL, lagg_lb_attach },
170 { LAGG_PROTO_LACP, lagg_lacp_attach },
171 { LAGG_PROTO_NONE, NULL }
174 SYSCTL_DECL(_net_link);
175 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
178 static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
179 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
180 &lagg_failover_rx_all, 0,
181 "Accept input from any interface in a failover lagg");
182 static int def_use_flowid = 1; /* Default value for using flowid */
183 TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
184 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
186 "Default setting for using flow id for load sharing");
187 static int def_flowid_shift = 16; /* Default value for using flow shift */
188 TUNABLE_INT("net.link.lagg.default_flowid_shift", &def_flowid_shift);
189 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RW,
190 &def_flowid_shift, 0,
191 "Default setting for flowid shift for load sharing");
194 lagg_modevent(module_t mod, int type, void *data)
199 mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
200 SLIST_INIT(&lagg_list);
201 lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
202 lagg_clone_destroy, 0);
203 lagg_input_p = lagg_input;
204 lagg_linkstate_p = lagg_port_state;
205 lagg_detach_cookie = EVENTHANDLER_REGISTER(
206 ifnet_departure_event, lagg_port_ifdetach, NULL,
207 EVENTHANDLER_PRI_ANY);
210 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
212 if_clone_detach(lagg_cloner);
214 lagg_linkstate_p = NULL;
215 mtx_destroy(&lagg_list_mtx);
223 static moduledata_t lagg_mod = {
229 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
230 MODULE_VERSION(if_lagg, 1);
233 * This routine is run via an vlan
237 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
239 struct lagg_softc *sc = ifp->if_softc;
240 struct lagg_port *lp;
241 struct rm_priotracker tracker;
243 if (ifp->if_softc != arg) /* Not our event */
246 LAGG_RLOCK(sc, &tracker);
247 if (!SLIST_EMPTY(&sc->sc_ports)) {
248 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
249 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
251 LAGG_RUNLOCK(sc, &tracker);
255 * This routine is run via an vlan
259 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
261 struct lagg_softc *sc = ifp->if_softc;
262 struct lagg_port *lp;
263 struct rm_priotracker tracker;
265 if (ifp->if_softc != arg) /* Not our event */
268 LAGG_RLOCK(sc, &tracker);
269 if (!SLIST_EMPTY(&sc->sc_ports)) {
270 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
271 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
273 LAGG_RUNLOCK(sc, &tracker);
277 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
279 struct lagg_softc *sc;
282 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
283 struct sysctl_oid *oid;
284 char num[14]; /* sufficient for 32 bits */
286 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
287 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
293 sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
294 sc->sc_opackets = counter_u64_alloc(M_WAITOK);
295 sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
296 sc->sc_obytes = counter_u64_alloc(M_WAITOK);
298 sysctl_ctx_init(&sc->ctx);
299 snprintf(num, sizeof(num), "%u", unit);
300 sc->use_flowid = def_use_flowid;
301 sc->flowid_shift = def_flowid_shift;
302 sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
303 &SYSCTL_NODE_CHILDREN(_net_link, lagg),
304 OID_AUTO, num, CTLFLAG_RD, NULL, "");
305 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
306 "use_flowid", CTLFLAG_RW, &sc->use_flowid,
307 sc->use_flowid, "Use flow id for load sharing");
308 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
309 "flowid_shift", CTLFLAG_RW, &sc->flowid_shift,
311 "Shift flowid bits to prevent multiqueue collisions");
312 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
313 "count", CTLFLAG_RD, &sc->sc_count, sc->sc_count,
314 "Total number of ports");
315 SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
316 "active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
317 "I", "Total number of active ports");
318 SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
319 "flapping", CTLFLAG_RD, &sc->sc_flapping,
320 sc->sc_flapping, "Total number of port change events");
321 /* Hash all layers by default */
322 sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
324 sc->sc_proto = LAGG_PROTO_NONE;
325 for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
326 if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
327 sc->sc_proto = lagg_protos[i].ti_proto;
328 if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
337 LAGG_CALLOUT_LOCK_INIT(sc);
338 SLIST_INIT(&sc->sc_ports);
339 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
342 * This uses the callout lock rather than the rmlock; one can't
343 * hold said rmlock during SWI.
345 callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0);
347 /* Initialise pseudo media types */
348 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
350 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
351 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
353 if_initname(ifp, laggname, unit);
355 ifp->if_transmit = lagg_transmit;
356 ifp->if_qflush = lagg_qflush;
357 ifp->if_init = lagg_init;
358 ifp->if_ioctl = lagg_ioctl;
359 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
360 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
363 * Attach as an ordinary ethernet device, children will be attached
364 * as special device IFT_IEEE8023ADLAG.
366 ether_ifattach(ifp, eaddr);
368 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
369 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
370 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
371 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
373 /* Insert into the global list of laggs */
374 mtx_lock(&lagg_list_mtx);
375 SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
376 mtx_unlock(&lagg_list_mtx);
378 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
384 lagg_clone_destroy(struct ifnet *ifp)
386 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
387 struct lagg_port *lp;
392 ifp->if_flags &= ~IFF_UP;
394 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
395 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
397 /* Shutdown and remove lagg ports */
398 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
399 lagg_port_destroy(lp, 1);
400 /* Unhook the aggregation protocol */
401 if (sc->sc_detach != NULL)
402 (*sc->sc_detach)(sc);
406 sysctl_ctx_free(&sc->ctx);
407 ifmedia_removeall(&sc->sc_media);
411 /* This grabs sc_callout_mtx, serialising it correctly */
412 callout_drain(&sc->sc_callout);
414 /* At this point it's drained; we can free this */
415 counter_u64_free(sc->sc_ipackets);
416 counter_u64_free(sc->sc_opackets);
417 counter_u64_free(sc->sc_ibytes);
418 counter_u64_free(sc->sc_obytes);
420 mtx_lock(&lagg_list_mtx);
421 SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
422 mtx_unlock(&lagg_list_mtx);
424 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
425 LAGG_LOCK_DESTROY(sc);
426 LAGG_CALLOUT_LOCK_DESTROY(sc);
431 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
433 struct ifnet *ifp = sc->sc_ifp;
435 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
438 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
439 /* Let the protocol know the MAC has changed */
440 if (sc->sc_lladdr != NULL)
441 (*sc->sc_lladdr)(sc);
442 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
446 lagg_capabilities(struct lagg_softc *sc)
448 struct lagg_port *lp;
449 int cap = ~0, ena = ~0;
451 struct ifnet_hw_tsomax hw_tsomax;
453 LAGG_WLOCK_ASSERT(sc);
455 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
457 /* Get capabilities from the lagg ports */
458 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
459 cap &= lp->lp_ifp->if_capabilities;
460 ena &= lp->lp_ifp->if_capenable;
461 hwa &= lp->lp_ifp->if_hwassist;
462 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
464 cap = (cap == ~0 ? 0 : cap);
465 ena = (ena == ~0 ? 0 : ena);
466 hwa = (hwa == ~0 ? 0 : hwa);
468 if (sc->sc_ifp->if_capabilities != cap ||
469 sc->sc_ifp->if_capenable != ena ||
470 sc->sc_ifp->if_hwassist != hwa ||
471 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
472 sc->sc_ifp->if_capabilities = cap;
473 sc->sc_ifp->if_capenable = ena;
474 sc->sc_ifp->if_hwassist = hwa;
475 getmicrotime(&sc->sc_ifp->if_lastchange);
477 if (sc->sc_ifflags & IFF_DEBUG)
478 if_printf(sc->sc_ifp,
479 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
484 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
486 struct lagg_softc *sc = lp->lp_softc;
487 struct ifnet *ifp = lp->lp_ifp;
488 struct lagg_llq *llq;
491 LAGG_WLOCK_ASSERT(sc);
493 if (lp->lp_detaching ||
494 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
497 /* Check to make sure its not already queued to be changed */
498 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
499 if (llq->llq_ifp == ifp) {
506 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
507 if (llq == NULL) /* XXX what to do */
511 /* Update the lladdr even if pending, it may have changed */
513 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
516 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
518 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
522 * Set the interface MAC address from a taskqueue to avoid a LOR.
525 lagg_port_setlladdr(void *arg, int pending)
527 struct lagg_softc *sc = (struct lagg_softc *)arg;
528 struct lagg_llq *llq, *head;
532 /* Grab a local reference of the queue and remove it from the softc */
534 head = SLIST_FIRST(&sc->sc_llq_head);
535 SLIST_FIRST(&sc->sc_llq_head) = NULL;
539 * Traverse the queue and set the lladdr on each ifp. It is safe to do
540 * unlocked as we have the only reference to it.
542 for (llq = head; llq != NULL; llq = head) {
545 /* Set the link layer address */
546 CURVNET_SET(ifp->if_vnet);
547 error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
550 printf("%s: setlladdr failed on %s\n", __func__,
553 head = SLIST_NEXT(llq, llq_entries);
559 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
561 struct lagg_softc *sc_ptr;
562 struct lagg_port *lp, *tlp;
565 LAGG_WLOCK_ASSERT(sc);
567 /* Limit the maximal number of lagg ports */
568 if (sc->sc_count >= LAGG_MAX_PORTS)
571 /* Check if port has already been associated to a lagg */
572 if (ifp->if_lagg != NULL) {
573 /* Port is already in the current lagg? */
574 lp = (struct lagg_port *)ifp->if_lagg;
575 if (lp->lp_softc == sc)
580 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
581 if (ifp->if_type != IFT_ETHER)
582 return (EPROTONOSUPPORT);
586 * The member interface should not have inet6 address because
587 * two interfaces with a valid link-local scope zone must not be
588 * merged in any form. This restriction is needed to
589 * prevent violation of link-local scope zone. Attempts to
590 * add a member interface which has inet6 addresses triggers
591 * removal of all inet6 addresses on the member interface.
593 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
594 if (in6ifa_llaonifp(lp->lp_ifp)) {
595 in6_ifdetach(lp->lp_ifp);
596 if_printf(sc->sc_ifp,
597 "IPv6 addresses on %s have been removed "
598 "before adding it as a member to prevent "
599 "IPv6 address scope violation.\n",
600 lp->lp_ifp->if_xname);
603 if (in6ifa_llaonifp(ifp)) {
605 if_printf(sc->sc_ifp,
606 "IPv6 addresses on %s have been removed "
607 "before adding it as a member to prevent "
608 "IPv6 address scope violation.\n",
612 /* Allow the first Ethernet member to define the MTU */
613 if (SLIST_EMPTY(&sc->sc_ports))
614 sc->sc_ifp->if_mtu = ifp->if_mtu;
615 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
616 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
621 if ((lp = malloc(sizeof(struct lagg_port),
622 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
625 /* Check if port is a stacked lagg */
626 mtx_lock(&lagg_list_mtx);
627 SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
628 if (ifp == sc_ptr->sc_ifp) {
629 mtx_unlock(&lagg_list_mtx);
632 /* XXX disable stacking for the moment, its untested */
633 #ifdef LAGG_PORT_STACKING
634 lp->lp_flags |= LAGG_PORT_STACK;
635 if (lagg_port_checkstacking(sc_ptr) >=
637 mtx_unlock(&lagg_list_mtx);
644 mtx_unlock(&lagg_list_mtx);
646 /* Change the interface type */
647 lp->lp_iftype = ifp->if_type;
648 ifp->if_type = IFT_IEEE8023ADLAG;
650 lp->lp_ioctl = ifp->if_ioctl;
651 ifp->if_ioctl = lagg_port_ioctl;
652 lp->lp_output = ifp->if_output;
653 ifp->if_output = lagg_port_output;
658 /* Save port link layer address */
659 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
661 if (SLIST_EMPTY(&sc->sc_ports)) {
663 lagg_lladdr(sc, IF_LLADDR(ifp));
665 /* Update link layer address for this port */
666 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
670 * Insert into the list of ports.
671 * Keep ports sorted by if_index. It is handy, when configuration
672 * is predictable and `ifconfig laggN create ...` command
673 * will lead to the same result each time.
675 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
676 if (tlp->lp_ifp->if_index < ifp->if_index && (
677 SLIST_NEXT(tlp, lp_entries) == NULL ||
678 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
683 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
685 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
688 /* Update lagg capabilities */
689 lagg_capabilities(sc);
692 /* Add multicast addresses and interface flags to this port */
693 lagg_ether_cmdmulti(lp, 1);
694 lagg_setflags(lp, 1);
696 if (sc->sc_port_create != NULL)
697 error = (*sc->sc_port_create)(lp);
699 /* remove the port again, without calling sc_port_destroy */
700 lagg_port_destroy(lp, 0);
707 #ifdef LAGG_PORT_STACKING
709 lagg_port_checkstacking(struct lagg_softc *sc)
711 struct lagg_softc *sc_ptr;
712 struct lagg_port *lp;
715 LAGG_WLOCK_ASSERT(sc);
717 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
718 if (lp->lp_flags & LAGG_PORT_STACK) {
719 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
720 m = MAX(m, lagg_port_checkstacking(sc_ptr));
729 lagg_port_destroy(struct lagg_port *lp, int runpd)
731 struct lagg_softc *sc = lp->lp_softc;
732 struct lagg_port *lp_ptr;
733 struct lagg_llq *llq;
734 struct ifnet *ifp = lp->lp_ifp;
736 LAGG_WLOCK_ASSERT(sc);
738 if (runpd && sc->sc_port_destroy != NULL)
739 (*sc->sc_port_destroy)(lp);
742 * Remove multicast addresses and interface flags from this port and
743 * reset the MAC address, skip if the interface is being detached.
745 if (!lp->lp_detaching) {
746 lagg_ether_cmdmulti(lp, 0);
747 lagg_setflags(lp, 0);
748 lagg_port_lladdr(lp, lp->lp_lladdr);
751 /* Restore interface */
752 ifp->if_type = lp->lp_iftype;
753 ifp->if_ioctl = lp->lp_ioctl;
754 ifp->if_output = lp->lp_output;
757 /* Finally, remove the port from the lagg */
758 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
761 /* Update the primary interface */
762 if (lp == sc->sc_primary) {
763 uint8_t lladdr[ETHER_ADDR_LEN];
765 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
766 bzero(&lladdr, ETHER_ADDR_LEN);
768 bcopy(lp_ptr->lp_lladdr,
769 lladdr, ETHER_ADDR_LEN);
771 lagg_lladdr(sc, lladdr);
772 sc->sc_primary = lp_ptr;
774 /* Update link layer address for each port */
775 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
776 lagg_port_lladdr(lp_ptr, lladdr);
779 /* Remove any pending lladdr changes from the queue */
780 if (lp->lp_detaching) {
781 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
782 if (llq->llq_ifp == ifp) {
783 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
786 break; /* Only appears once */
792 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
796 /* Update lagg capabilities */
797 lagg_capabilities(sc);
804 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
806 struct lagg_reqport *rp = (struct lagg_reqport *)data;
807 struct lagg_softc *sc;
808 struct lagg_port *lp = NULL;
810 struct rm_priotracker tracker;
812 /* Should be checked by the caller */
813 if (ifp->if_type != IFT_IEEE8023ADLAG ||
814 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
819 if (rp->rp_portname[0] == '\0' ||
820 ifunit(rp->rp_portname) != ifp) {
825 LAGG_RLOCK(sc, &tracker);
826 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
828 LAGG_RUNLOCK(sc, &tracker);
832 lagg_port2req(lp, rp);
833 LAGG_RUNLOCK(sc, &tracker);
837 if (lp->lp_ioctl == NULL) {
841 error = (*lp->lp_ioctl)(ifp, cmd, data);
845 /* Update lagg interface capabilities */
847 lagg_capabilities(sc);
852 /* Do not allow the MTU to be changed once joined */
863 if (lp->lp_ioctl != NULL)
864 return ((*lp->lp_ioctl)(ifp, cmd, data));
870 * For direct output to child ports.
873 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
874 const struct sockaddr *dst, struct route *ro)
876 struct lagg_port *lp = ifp->if_lagg;
878 switch (dst->sa_family) {
879 case pseudo_AF_HDRCMPLT:
881 return ((*lp->lp_output)(ifp, m, dst, ro));
884 /* drop any other frames */
890 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
892 struct lagg_port *lp;
893 struct lagg_softc *sc;
895 if ((lp = ifp->if_lagg) == NULL)
897 /* If the ifnet is just being renamed, don't do anything. */
898 if (ifp->if_flags & IFF_RENAMING)
904 lp->lp_detaching = 1;
905 lagg_port_destroy(lp, 1);
910 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
912 struct lagg_softc *sc = lp->lp_softc;
914 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
915 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
916 rp->rp_prio = lp->lp_prio;
917 rp->rp_flags = lp->lp_flags;
918 if (sc->sc_portreq != NULL)
919 (*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
921 /* Add protocol specific flags */
922 switch (sc->sc_proto) {
923 case LAGG_PROTO_FAILOVER:
924 if (lp == sc->sc_primary)
925 rp->rp_flags |= LAGG_PORT_MASTER;
926 if (lp == lagg_link_active(sc, sc->sc_primary))
927 rp->rp_flags |= LAGG_PORT_ACTIVE;
930 case LAGG_PROTO_ROUNDROBIN:
931 case LAGG_PROTO_LOADBALANCE:
932 case LAGG_PROTO_ETHERCHANNEL:
933 if (LAGG_PORTACTIVE(lp))
934 rp->rp_flags |= LAGG_PORT_ACTIVE;
937 case LAGG_PROTO_LACP:
938 /* LACP has a different definition of active */
939 if (lacp_isactive(lp))
940 rp->rp_flags |= LAGG_PORT_ACTIVE;
941 if (lacp_iscollecting(lp))
942 rp->rp_flags |= LAGG_PORT_COLLECTING;
943 if (lacp_isdistributing(lp))
944 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
953 struct lagg_softc *sc = (struct lagg_softc *)xsc;
954 struct lagg_port *lp;
955 struct ifnet *ifp = sc->sc_ifp;
957 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
962 ifp->if_drv_flags |= IFF_DRV_RUNNING;
963 /* Update the port lladdrs */
964 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
965 lagg_port_lladdr(lp, IF_LLADDR(ifp));
967 if (sc->sc_init != NULL)
974 lagg_stop(struct lagg_softc *sc)
976 struct ifnet *ifp = sc->sc_ifp;
978 LAGG_WLOCK_ASSERT(sc);
980 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
983 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
985 if (sc->sc_stop != NULL)
990 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
992 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
993 struct lagg_reqall *ra = (struct lagg_reqall *)data;
994 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
995 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
996 struct ifreq *ifr = (struct ifreq *)data;
997 struct lagg_port *lp;
999 struct thread *td = curthread;
1001 int count, buflen, len, error = 0;
1002 struct rm_priotracker tracker;
1004 bzero(&rpbuf, sizeof(rpbuf));
1008 LAGG_RLOCK(sc, &tracker);
1010 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1012 buflen = count * sizeof(struct lagg_reqport);
1013 LAGG_RUNLOCK(sc, &tracker);
1015 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1017 LAGG_RLOCK(sc, &tracker);
1018 ra->ra_proto = sc->sc_proto;
1019 if (sc->sc_req != NULL)
1020 (*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
1024 len = min(ra->ra_size, buflen);
1025 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1026 if (len < sizeof(rpbuf))
1029 lagg_port2req(lp, &rpbuf);
1030 memcpy(buf, &rpbuf, sizeof(rpbuf));
1032 buf += sizeof(rpbuf);
1033 len -= sizeof(rpbuf);
1035 LAGG_RUNLOCK(sc, &tracker);
1036 ra->ra_ports = count;
1037 ra->ra_size = count * sizeof(rpbuf);
1038 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1039 free(outbuf, M_TEMP);
1042 error = priv_check(td, PRIV_NET_LAGG);
1045 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1046 error = EPROTONOSUPPORT;
1050 if (sc->sc_proto != LAGG_PROTO_NONE) {
1051 /* Reset protocol first in case detach unlocks */
1052 sc->sc_proto = LAGG_PROTO_NONE;
1053 error = sc->sc_detach(sc);
1054 sc->sc_detach = NULL;
1055 sc->sc_start = NULL;
1056 sc->sc_input = NULL;
1057 sc->sc_port_create = NULL;
1058 sc->sc_port_destroy = NULL;
1059 sc->sc_linkstate = NULL;
1062 sc->sc_lladdr = NULL;
1064 sc->sc_portreq = NULL;
1065 } else if (sc->sc_input != NULL) {
1066 /* Still detaching */
1073 for (int i = 0; i < (sizeof(lagg_protos) /
1074 sizeof(lagg_protos[0])); i++) {
1075 if (lagg_protos[i].ti_proto == ra->ra_proto) {
1076 if (sc->sc_ifflags & IFF_DEBUG)
1077 printf("%s: using proto %u\n",
1079 lagg_protos[i].ti_proto);
1080 sc->sc_proto = lagg_protos[i].ti_proto;
1081 if (sc->sc_proto != LAGG_PROTO_NONE)
1082 error = lagg_protos[i].ti_attach(sc);
1088 error = EPROTONOSUPPORT;
1090 case SIOCGLAGGFLAGS:
1091 rf->rf_flags = sc->sc_flags;
1094 error = priv_check(td, PRIV_NET_LAGG);
1097 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1102 sc->sc_flags &= ~LAGG_F_HASHMASK;
1103 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1107 if (rp->rp_portname[0] == '\0' ||
1108 (tpif = ifunit(rp->rp_portname)) == NULL) {
1113 LAGG_RLOCK(sc, &tracker);
1114 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1115 lp->lp_softc != sc) {
1117 LAGG_RUNLOCK(sc, &tracker);
1121 lagg_port2req(lp, rp);
1122 LAGG_RUNLOCK(sc, &tracker);
1125 error = priv_check(td, PRIV_NET_LAGG);
1128 if (rp->rp_portname[0] == '\0' ||
1129 (tpif = ifunit(rp->rp_portname)) == NULL) {
1134 error = lagg_port_create(sc, tpif);
1137 case SIOCSLAGGDELPORT:
1138 error = priv_check(td, PRIV_NET_LAGG);
1141 if (rp->rp_portname[0] == '\0' ||
1142 (tpif = ifunit(rp->rp_portname)) == NULL) {
1148 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1149 lp->lp_softc != sc) {
1155 error = lagg_port_destroy(lp, 1);
1159 /* Set flags on ports too */
1161 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1162 lagg_setflags(lp, 1);
1166 if (!(ifp->if_flags & IFF_UP) &&
1167 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1169 * If interface is marked down and it is running,
1170 * then stop and disable it.
1175 } else if ((ifp->if_flags & IFF_UP) &&
1176 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1178 * If interface is marked up and it is stopped, then
1181 (*ifp->if_init)(sc);
1187 error = lagg_ether_setmulti(sc);
1192 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1197 /* Do not allow the MTU or caps to be directly changed */
1202 error = ether_ioctl(ifp, cmd, data);
1209 lagg_ether_setmulti(struct lagg_softc *sc)
1211 struct lagg_port *lp;
1213 LAGG_WLOCK_ASSERT(sc);
1215 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1216 /* First, remove any existing filter entries. */
1217 lagg_ether_cmdmulti(lp, 0);
1218 /* copy all addresses from the lagg interface to the port */
1219 lagg_ether_cmdmulti(lp, 1);
1225 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1227 struct lagg_softc *sc = lp->lp_softc;
1228 struct ifnet *ifp = lp->lp_ifp;
1229 struct ifnet *scifp = sc->sc_ifp;
1231 struct ifmultiaddr *ifma;
1234 LAGG_WLOCK_ASSERT(sc);
1237 IF_ADDR_WLOCK(scifp);
1238 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1239 if (ifma->ifma_addr->sa_family != AF_LINK)
1241 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1243 IF_ADDR_WUNLOCK(scifp);
1246 bcopy(ifma->ifma_addr, &mc->mc_addr,
1247 ifma->ifma_addr->sa_len);
1248 mc->mc_addr.sdl_index = ifp->if_index;
1250 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1252 IF_ADDR_WUNLOCK(scifp);
1253 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1254 error = if_addmulti(ifp,
1255 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1260 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1261 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1262 if (mc->mc_ifma && !lp->lp_detaching)
1263 if_delmulti_ifma(mc->mc_ifma);
1270 /* Handle a ref counted flag that should be set on the lagg port as well */
1272 lagg_setflag(struct lagg_port *lp, int flag, int status,
1273 int (*func)(struct ifnet *, int))
1275 struct lagg_softc *sc = lp->lp_softc;
1276 struct ifnet *scifp = sc->sc_ifp;
1277 struct ifnet *ifp = lp->lp_ifp;
1280 LAGG_WLOCK_ASSERT(sc);
1282 status = status ? (scifp->if_flags & flag) : 0;
1283 /* Now "status" contains the flag value or 0 */
1286 * See if recorded ports status is different from what
1287 * we want it to be. If it is, flip it. We record ports
1288 * status in lp_ifflags so that we won't clear ports flag
1289 * we haven't set. In fact, we don't clear or set ports
1290 * flags directly, but get or release references to them.
1291 * That's why we can be sure that recorded flags still are
1292 * in accord with actual ports flags.
1294 if (status != (lp->lp_ifflags & flag)) {
1295 error = (*func)(ifp, status);
1298 lp->lp_ifflags &= ~flag;
1299 lp->lp_ifflags |= status;
1305 * Handle IFF_* flags that require certain changes on the lagg port
1306 * if "status" is true, update ports flags respective to the lagg
1307 * if "status" is false, forcedly clear the flags set on port.
1310 lagg_setflags(struct lagg_port *lp, int status)
1314 for (i = 0; lagg_pflags[i].flag; i++) {
1315 error = lagg_setflag(lp, lagg_pflags[i].flag,
1316 status, lagg_pflags[i].func);
1324 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1326 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1327 int error, len, mcast;
1328 struct rm_priotracker tracker;
1330 len = m->m_pkthdr.len;
1331 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1333 LAGG_RLOCK(sc, &tracker);
1334 /* We need a Tx algorithm and at least one port */
1335 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1336 LAGG_RUNLOCK(sc, &tracker);
1342 ETHER_BPF_MTAP(ifp, m);
1344 error = (*sc->sc_start)(sc, m);
1345 LAGG_RUNLOCK(sc, &tracker);
1348 counter_u64_add(sc->sc_opackets, 1);
1349 counter_u64_add(sc->sc_obytes, len);
1350 ifp->if_omcasts += mcast;
1358 * The ifp->if_qflush entry point for lagg(4) is no-op.
1361 lagg_qflush(struct ifnet *ifp __unused)
1365 static struct mbuf *
1366 lagg_input(struct ifnet *ifp, struct mbuf *m)
1368 struct lagg_port *lp = ifp->if_lagg;
1369 struct lagg_softc *sc = lp->lp_softc;
1370 struct ifnet *scifp = sc->sc_ifp;
1371 struct rm_priotracker tracker;
1373 LAGG_RLOCK(sc, &tracker);
1374 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1375 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1376 sc->sc_proto == LAGG_PROTO_NONE) {
1377 LAGG_RUNLOCK(sc, &tracker);
1382 ETHER_BPF_MTAP(scifp, m);
1384 m = (*sc->sc_input)(sc, lp, m);
1387 counter_u64_add(sc->sc_ipackets, 1);
1388 counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1390 if (scifp->if_flags & IFF_MONITOR) {
1396 LAGG_RUNLOCK(sc, &tracker);
1401 lagg_media_change(struct ifnet *ifp)
1403 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1405 if (sc->sc_ifflags & IFF_DEBUG)
1406 printf("%s\n", __func__);
1413 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1415 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1416 struct lagg_port *lp;
1417 struct rm_priotracker tracker;
1419 imr->ifm_status = IFM_AVALID;
1420 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1422 LAGG_RLOCK(sc, &tracker);
1423 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1424 if (LAGG_PORTACTIVE(lp))
1425 imr->ifm_status |= IFM_ACTIVE;
1427 LAGG_RUNLOCK(sc, &tracker);
1431 lagg_linkstate(struct lagg_softc *sc)
1433 struct lagg_port *lp;
1434 int new_link = LINK_STATE_DOWN;
1437 /* Our link is considered up if at least one of our ports is active */
1438 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1439 if (lp->lp_link_state == LINK_STATE_UP) {
1440 new_link = LINK_STATE_UP;
1444 if_link_state_change(sc->sc_ifp, new_link);
1446 /* Update if_baudrate to reflect the max possible speed */
1447 switch (sc->sc_proto) {
1448 case LAGG_PROTO_FAILOVER:
1449 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1450 sc->sc_primary->lp_ifp->if_baudrate : 0;
1452 case LAGG_PROTO_ROUNDROBIN:
1453 case LAGG_PROTO_LOADBALANCE:
1454 case LAGG_PROTO_ETHERCHANNEL:
1456 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1457 speed += lp->lp_ifp->if_baudrate;
1458 sc->sc_ifp->if_baudrate = speed;
1460 case LAGG_PROTO_LACP:
1461 /* LACP updates if_baudrate itself */
1467 lagg_port_state(struct ifnet *ifp, int state)
1469 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1470 struct lagg_softc *sc = NULL;
1479 if (sc->sc_linkstate != NULL)
1480 (*sc->sc_linkstate)(lp);
1485 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1487 struct lagg_port *lp_next, *rval = NULL;
1488 // int new_link = LINK_STATE_DOWN;
1490 LAGG_RLOCK_ASSERT(sc);
1492 * Search a port which reports an active link state.
1497 if (LAGG_PORTACTIVE(lp)) {
1501 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1502 LAGG_PORTACTIVE(lp_next)) {
1508 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1509 if (LAGG_PORTACTIVE(lp_next)) {
1518 * The IEEE 802.1D standard assumes that a lagg with
1519 * multiple ports is always full duplex. This is valid
1520 * for load sharing laggs and if at least two links
1521 * are active. Unfortunately, checking the latter would
1522 * be too expensive at this point.
1524 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1526 new_link = LINK_STATE_FULL_DUPLEX;
1528 new_link = rval->lp_link_state;
1536 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1538 if (m->m_pkthdr.len < (off + len)) {
1540 } else if (m->m_len < (off + len)) {
1541 m_copydata(m, off, len, buf);
1544 return (mtod(m, char *) + off);
1548 lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1550 struct lagg_softc *sc = (struct lagg_softc *)arg1;
1551 struct lagg_port *lp;
1554 /* LACP tracks active links automatically, the others do not */
1555 if (sc->sc_proto != LAGG_PROTO_LACP) {
1557 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1558 sc->sc_active += LAGG_PORTACTIVE(lp);
1561 error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1562 if ((error) || (req->newptr == NULL))
1569 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1574 struct ether_header *eh;
1575 const struct ether_vlan_header *vlan;
1577 const struct ip *ip;
1578 const uint32_t *ports;
1582 const struct ip6_hdr *ip6;
1592 struct ether_vlan_header vlan;
1600 eh = mtod(m, struct ether_header *);
1601 etype = ntohs(eh->ether_type);
1602 if (sc->sc_flags & LAGG_F_HASHL2) {
1603 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1604 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1607 /* Special handling for encapsulating VLAN frames */
1608 if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1609 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1610 sizeof(m->m_pkthdr.ether_vtag), p);
1611 } else if (etype == ETHERTYPE_VLAN) {
1612 vlan = lagg_gethdr(m, off, sizeof(*vlan), &buf);
1616 if (sc->sc_flags & LAGG_F_HASHL2)
1617 p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1618 etype = ntohs(vlan->evl_proto);
1619 off += sizeof(*vlan) - sizeof(*eh);
1625 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1629 if (sc->sc_flags & LAGG_F_HASHL3) {
1630 p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1631 p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1633 if (!(sc->sc_flags & LAGG_F_HASHL4))
1639 iphlen = ip->ip_hl << 2;
1640 if (iphlen < sizeof(*ip))
1643 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1646 p = hash32_buf(ports, sizeof(*ports), p);
1652 case ETHERTYPE_IPV6:
1653 if (!(sc->sc_flags & LAGG_F_HASHL3))
1655 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1659 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1660 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1661 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1662 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1671 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1674 return (ifp->if_transmit)(ifp, m);
1678 * Simple round robin aggregation
1682 lagg_rr_attach(struct lagg_softc *sc)
1684 sc->sc_detach = lagg_rr_detach;
1685 sc->sc_start = lagg_rr_start;
1686 sc->sc_input = lagg_rr_input;
1687 sc->sc_port_create = NULL;
1688 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1695 lagg_rr_detach(struct lagg_softc *sc)
1701 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1703 struct lagg_port *lp;
1706 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1708 lp = SLIST_FIRST(&sc->sc_ports);
1710 lp = SLIST_NEXT(lp, lp_entries);
1713 * Check the port's link state. This will return the next active
1714 * port if the link is down or the port is NULL.
1716 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1722 return (lagg_enqueue(lp->lp_ifp, m));
1725 static struct mbuf *
1726 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1728 struct ifnet *ifp = sc->sc_ifp;
1730 /* Just pass in the packet to our lagg device */
1731 m->m_pkthdr.rcvif = ifp;
1741 lagg_fail_attach(struct lagg_softc *sc)
1743 sc->sc_detach = lagg_fail_detach;
1744 sc->sc_start = lagg_fail_start;
1745 sc->sc_input = lagg_fail_input;
1746 sc->sc_port_create = NULL;
1747 sc->sc_port_destroy = NULL;
1753 lagg_fail_detach(struct lagg_softc *sc)
1759 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1761 struct lagg_port *lp;
1763 /* Use the master port if active or the next available port */
1764 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1770 return (lagg_enqueue(lp->lp_ifp, m));
1773 static struct mbuf *
1774 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1776 struct ifnet *ifp = sc->sc_ifp;
1777 struct lagg_port *tmp_tp;
1779 if (lp == sc->sc_primary || lagg_failover_rx_all) {
1780 m->m_pkthdr.rcvif = ifp;
1784 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1785 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1787 * If tmp_tp is null, we've recieved a packet when all
1788 * our links are down. Weird, but process it anyways.
1790 if ((tmp_tp == NULL || tmp_tp == lp)) {
1791 m->m_pkthdr.rcvif = ifp;
1805 lagg_lb_attach(struct lagg_softc *sc)
1807 struct lagg_port *lp;
1810 if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1811 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1814 sc->sc_detach = lagg_lb_detach;
1815 sc->sc_start = lagg_lb_start;
1816 sc->sc_input = lagg_lb_input;
1817 sc->sc_port_create = lagg_lb_port_create;
1818 sc->sc_port_destroy = lagg_lb_port_destroy;
1819 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1821 lb->lb_key = arc4random();
1822 sc->sc_psc = (caddr_t)lb;
1824 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1825 lagg_lb_port_create(lp);
1831 lagg_lb_detach(struct lagg_softc *sc)
1833 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1840 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1842 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1843 struct lagg_port *lp_next;
1846 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1847 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1850 if (i >= LAGG_MAX_PORTS)
1852 if (sc->sc_ifflags & IFF_DEBUG)
1853 printf("%s: port %s at index %d\n",
1854 sc->sc_ifname, lp_next->lp_ifname, i);
1855 lb->lb_ports[i++] = lp_next;
1862 lagg_lb_port_create(struct lagg_port *lp)
1864 struct lagg_softc *sc = lp->lp_softc;
1865 return (lagg_lb_porttable(sc, NULL));
1869 lagg_lb_port_destroy(struct lagg_port *lp)
1871 struct lagg_softc *sc = lp->lp_softc;
1872 lagg_lb_porttable(sc, lp);
1876 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1878 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1879 struct lagg_port *lp = NULL;
1882 if (sc->use_flowid &&
1883 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
1884 p = m->m_pkthdr.flowid >> sc->flowid_shift;
1886 p = lagg_hashmbuf(sc, m, lb->lb_key);
1888 lp = lb->lb_ports[p];
1891 * Check the port's link state. This will return the next active
1892 * port if the link is down or the port is NULL.
1894 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1900 return (lagg_enqueue(lp->lp_ifp, m));
1903 static struct mbuf *
1904 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1906 struct ifnet *ifp = sc->sc_ifp;
1908 /* Just pass in the packet to our lagg device */
1909 m->m_pkthdr.rcvif = ifp;
1919 lagg_lacp_attach(struct lagg_softc *sc)
1921 struct lagg_port *lp;
1924 sc->sc_detach = lagg_lacp_detach;
1925 sc->sc_port_create = lacp_port_create;
1926 sc->sc_port_destroy = lacp_port_destroy;
1927 sc->sc_linkstate = lacp_linkstate;
1928 sc->sc_start = lagg_lacp_start;
1929 sc->sc_input = lagg_lacp_input;
1930 sc->sc_init = lacp_init;
1931 sc->sc_stop = lacp_stop;
1932 sc->sc_lladdr = lagg_lacp_lladdr;
1933 sc->sc_req = lacp_req;
1934 sc->sc_portreq = lacp_portreq;
1936 error = lacp_attach(sc);
1940 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1941 lacp_port_create(lp);
1947 lagg_lacp_detach(struct lagg_softc *sc)
1949 struct lagg_port *lp;
1952 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1953 lacp_port_destroy(lp);
1955 /* unlocking is safe here */
1957 error = lacp_detach(sc);
1964 lagg_lacp_lladdr(struct lagg_softc *sc)
1966 struct lagg_port *lp;
1968 /* purge all the lacp ports */
1969 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1970 lacp_port_destroy(lp);
1972 /* add them back in */
1973 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1974 lacp_port_create(lp);
1978 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1980 struct lagg_port *lp;
1982 lp = lacp_select_tx_port(sc, m);
1989 return (lagg_enqueue(lp->lp_ifp, m));
1992 static struct mbuf *
1993 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1995 struct ifnet *ifp = sc->sc_ifp;
1996 struct ether_header *eh;
1999 eh = mtod(m, struct ether_header *);
2000 etype = ntohs(eh->ether_type);
2002 /* Tap off LACP control messages */
2003 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2004 m = lacp_input(lp, m);
2010 * If the port is not collecting or not in the active aggregator then
2013 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2018 m->m_pkthdr.rcvif = ifp;
2023 lagg_callout(void *arg)
2025 struct lagg_softc *sc = (struct lagg_softc *)arg;
2026 struct ifnet *ifp = sc->sc_ifp;
2028 ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
2029 ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
2030 ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
2031 ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2033 callout_reset(&sc->sc_callout, hz, lagg_callout, sc);