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>
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_media.h>
50 #include <net/if_types.h>
51 #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 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
85 #define V_lagg_list VNET(lagg_list)
86 static VNET_DEFINE(struct mtx, lagg_list_mtx);
87 #define V_lagg_list_mtx VNET(lagg_list_mtx)
88 #define LAGG_LIST_LOCK_INIT(x) mtx_init(&V_lagg_list_mtx, \
89 "if_lagg list", NULL, MTX_DEF)
90 #define LAGG_LIST_LOCK_DESTROY(x) mtx_destroy(&V_lagg_list_mtx)
91 #define LAGG_LIST_LOCK(x) mtx_lock(&V_lagg_list_mtx)
92 #define LAGG_LIST_UNLOCK(x) mtx_unlock(&V_lagg_list_mtx)
93 eventhandler_tag lagg_detach_cookie = NULL;
95 static int lagg_clone_create(struct if_clone *, int, caddr_t);
96 static void lagg_clone_destroy(struct ifnet *);
97 static VNET_DEFINE(struct if_clone *, lagg_cloner);
98 #define V_lagg_cloner VNET(lagg_cloner)
99 static const char laggname[] = "lagg";
101 static void lagg_lladdr(struct lagg_softc *, uint8_t *);
102 static void lagg_capabilities(struct lagg_softc *);
103 static void lagg_port_lladdr(struct lagg_port *, uint8_t *);
104 static void lagg_port_setlladdr(void *, int);
105 static int lagg_port_create(struct lagg_softc *, struct ifnet *);
106 static int lagg_port_destroy(struct lagg_port *, int);
107 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
108 static void lagg_linkstate(struct lagg_softc *);
109 static void lagg_port_state(struct ifnet *, int);
110 static int lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
111 static int lagg_port_output(struct ifnet *, struct mbuf *,
112 const struct sockaddr *, struct route *);
113 static void lagg_port_ifdetach(void *arg __unused, struct ifnet *);
114 #ifdef LAGG_PORT_STACKING
115 static int lagg_port_checkstacking(struct lagg_softc *);
117 static void lagg_port2req(struct lagg_port *, struct lagg_reqport *);
118 static void lagg_init(void *);
119 static void lagg_stop(struct lagg_softc *);
120 static int lagg_ioctl(struct ifnet *, u_long, caddr_t);
121 static int lagg_ether_setmulti(struct lagg_softc *);
122 static int lagg_ether_cmdmulti(struct lagg_port *, int);
123 static int lagg_setflag(struct lagg_port *, int, int,
124 int (*func)(struct ifnet *, int));
125 static int lagg_setflags(struct lagg_port *, int status);
126 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
127 static int lagg_transmit(struct ifnet *, struct mbuf *);
128 static void lagg_qflush(struct ifnet *);
129 static int lagg_media_change(struct ifnet *);
130 static void lagg_media_status(struct ifnet *, struct ifmediareq *);
131 static struct lagg_port *lagg_link_active(struct lagg_softc *,
134 /* Simple round robin */
135 static void lagg_rr_attach(struct lagg_softc *);
136 static int lagg_rr_start(struct lagg_softc *, struct mbuf *);
137 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
140 /* Active failover */
141 static int lagg_fail_start(struct lagg_softc *, struct mbuf *);
142 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
146 static void lagg_lb_attach(struct lagg_softc *);
147 static void lagg_lb_detach(struct lagg_softc *);
148 static int lagg_lb_port_create(struct lagg_port *);
149 static void lagg_lb_port_destroy(struct lagg_port *);
150 static int lagg_lb_start(struct lagg_softc *, struct mbuf *);
151 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
153 static int lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
156 static int lagg_bcast_start(struct lagg_softc *, struct mbuf *);
157 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
161 static void lagg_lacp_attach(struct lagg_softc *);
162 static void lagg_lacp_detach(struct lagg_softc *);
163 static int lagg_lacp_start(struct lagg_softc *, struct mbuf *);
164 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
166 static void lagg_lacp_lladdr(struct lagg_softc *);
168 /* lagg protocol table */
169 static const struct lagg_proto {
171 void (*pr_attach)(struct lagg_softc *);
172 void (*pr_detach)(struct lagg_softc *);
173 int (*pr_start)(struct lagg_softc *, struct mbuf *);
174 struct mbuf * (*pr_input)(struct lagg_softc *, struct lagg_port *,
176 int (*pr_addport)(struct lagg_port *);
177 void (*pr_delport)(struct lagg_port *);
178 void (*pr_linkstate)(struct lagg_port *);
179 void (*pr_init)(struct lagg_softc *);
180 void (*pr_stop)(struct lagg_softc *);
181 void (*pr_lladdr)(struct lagg_softc *);
182 void (*pr_request)(struct lagg_softc *, void *);
183 void (*pr_portreq)(struct lagg_port *, void *);
186 .pr_num = LAGG_PROTO_NONE
189 .pr_num = LAGG_PROTO_ROUNDROBIN,
190 .pr_attach = lagg_rr_attach,
191 .pr_start = lagg_rr_start,
192 .pr_input = lagg_rr_input,
195 .pr_num = LAGG_PROTO_FAILOVER,
196 .pr_start = lagg_fail_start,
197 .pr_input = lagg_fail_input,
200 .pr_num = LAGG_PROTO_LOADBALANCE,
201 .pr_attach = lagg_lb_attach,
202 .pr_detach = lagg_lb_detach,
203 .pr_start = lagg_lb_start,
204 .pr_input = lagg_lb_input,
205 .pr_addport = lagg_lb_port_create,
206 .pr_delport = lagg_lb_port_destroy,
209 .pr_num = LAGG_PROTO_LACP,
210 .pr_attach = lagg_lacp_attach,
211 .pr_detach = lagg_lacp_detach,
212 .pr_start = lagg_lacp_start,
213 .pr_input = lagg_lacp_input,
214 .pr_addport = lacp_port_create,
215 .pr_delport = lacp_port_destroy,
216 .pr_linkstate = lacp_linkstate,
217 .pr_init = lacp_init,
218 .pr_stop = lacp_stop,
219 .pr_lladdr = lagg_lacp_lladdr,
220 .pr_request = lacp_req,
221 .pr_portreq = lacp_portreq,
224 .pr_num = LAGG_PROTO_BROADCAST,
225 .pr_start = lagg_bcast_start,
226 .pr_input = lagg_bcast_input,
230 SYSCTL_DECL(_net_link);
231 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
234 /* Allow input on any failover links */
235 static VNET_DEFINE(int, lagg_failover_rx_all);
236 #define V_lagg_failover_rx_all VNET(lagg_failover_rx_all)
237 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
238 &VNET_NAME(lagg_failover_rx_all), 0,
239 "Accept input from any interface in a failover lagg");
241 /* Default value for using flowid */
242 static VNET_DEFINE(int, def_use_flowid) = 1;
243 #define V_def_use_flowid VNET(def_use_flowid)
244 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
245 &VNET_NAME(def_use_flowid), 0,
246 "Default setting for using flow id for load sharing");
248 /* Default value for flowid shift */
249 static VNET_DEFINE(int, def_flowid_shift) = 16;
250 #define V_def_flowid_shift VNET(def_flowid_shift)
251 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
252 &VNET_NAME(def_flowid_shift), 0,
253 "Default setting for flowid shift for load sharing");
256 vnet_lagg_init(const void *unused __unused)
259 LAGG_LIST_LOCK_INIT();
260 SLIST_INIT(&V_lagg_list);
261 V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
262 lagg_clone_destroy, 0);
264 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
265 vnet_lagg_init, NULL);
268 vnet_lagg_uninit(const void *unused __unused)
271 if_clone_detach(V_lagg_cloner);
272 LAGG_LIST_LOCK_DESTROY();
274 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
275 vnet_lagg_uninit, NULL);
278 lagg_modevent(module_t mod, int type, void *data)
283 lagg_input_p = lagg_input;
284 lagg_linkstate_p = lagg_port_state;
285 lagg_detach_cookie = EVENTHANDLER_REGISTER(
286 ifnet_departure_event, lagg_port_ifdetach, NULL,
287 EVENTHANDLER_PRI_ANY);
290 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
293 lagg_linkstate_p = NULL;
301 static moduledata_t lagg_mod = {
307 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
308 MODULE_VERSION(if_lagg, 1);
311 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
314 KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
317 if (sc->sc_ifflags & IFF_DEBUG)
318 if_printf(sc->sc_ifp, "using proto %u\n", pr);
320 if (lagg_protos[pr].pr_attach != NULL)
321 lagg_protos[pr].pr_attach(sc);
326 lagg_proto_detach(struct lagg_softc *sc)
330 LAGG_WLOCK_ASSERT(sc);
333 sc->sc_proto = LAGG_PROTO_NONE;
335 if (lagg_protos[pr].pr_detach != NULL)
336 lagg_protos[pr].pr_detach(sc);
342 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
345 return (lagg_protos[sc->sc_proto].pr_start(sc, m));
349 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
352 return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
356 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
359 if (lagg_protos[sc->sc_proto].pr_addport == NULL)
362 return (lagg_protos[sc->sc_proto].pr_addport(lp));
366 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
369 if (lagg_protos[sc->sc_proto].pr_delport != NULL)
370 lagg_protos[sc->sc_proto].pr_delport(lp);
374 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
377 if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
378 lagg_protos[sc->sc_proto].pr_linkstate(lp);
382 lagg_proto_init(struct lagg_softc *sc)
385 if (lagg_protos[sc->sc_proto].pr_init != NULL)
386 lagg_protos[sc->sc_proto].pr_init(sc);
390 lagg_proto_stop(struct lagg_softc *sc)
393 if (lagg_protos[sc->sc_proto].pr_stop != NULL)
394 lagg_protos[sc->sc_proto].pr_stop(sc);
398 lagg_proto_lladdr(struct lagg_softc *sc)
401 if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
402 lagg_protos[sc->sc_proto].pr_lladdr(sc);
406 lagg_proto_request(struct lagg_softc *sc, void *v)
409 if (lagg_protos[sc->sc_proto].pr_request != NULL)
410 lagg_protos[sc->sc_proto].pr_request(sc, v);
414 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
417 if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
418 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
422 * This routine is run via an vlan
426 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
428 struct lagg_softc *sc = ifp->if_softc;
429 struct lagg_port *lp;
430 struct rm_priotracker tracker;
432 if (ifp->if_softc != arg) /* Not our event */
435 LAGG_RLOCK(sc, &tracker);
436 if (!SLIST_EMPTY(&sc->sc_ports)) {
437 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
438 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
440 LAGG_RUNLOCK(sc, &tracker);
444 * This routine is run via an vlan
448 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
450 struct lagg_softc *sc = ifp->if_softc;
451 struct lagg_port *lp;
452 struct rm_priotracker tracker;
454 if (ifp->if_softc != arg) /* Not our event */
457 LAGG_RLOCK(sc, &tracker);
458 if (!SLIST_EMPTY(&sc->sc_ports)) {
459 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
460 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
462 LAGG_RUNLOCK(sc, &tracker);
466 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
468 struct lagg_softc *sc;
470 static const u_char eaddr[6]; /* 00:00:00:00:00:00 */
472 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
473 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
479 if (V_def_use_flowid)
480 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
481 sc->flowid_shift = V_def_flowid_shift;
483 /* Hash all layers by default */
484 sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
486 lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
489 SLIST_INIT(&sc->sc_ports);
490 TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
492 /* Initialise pseudo media types */
493 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
495 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
496 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
498 if_initname(ifp, laggname, unit);
500 ifp->if_transmit = lagg_transmit;
501 ifp->if_qflush = lagg_qflush;
502 ifp->if_init = lagg_init;
503 ifp->if_ioctl = lagg_ioctl;
504 ifp->if_get_counter = lagg_get_counter;
505 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
506 ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
509 * Attach as an ordinary ethernet device, children will be attached
510 * as special device IFT_IEEE8023ADLAG.
512 ether_ifattach(ifp, eaddr);
514 sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
515 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
516 sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
517 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
519 /* Insert into the global list of laggs */
521 SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
528 lagg_clone_destroy(struct ifnet *ifp)
530 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
531 struct lagg_port *lp;
536 ifp->if_flags &= ~IFF_UP;
538 EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
539 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
541 /* Shutdown and remove lagg ports */
542 while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
543 lagg_port_destroy(lp, 1);
544 /* Unhook the aggregation protocol */
545 lagg_proto_detach(sc);
547 ifmedia_removeall(&sc->sc_media);
552 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
555 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
556 LAGG_LOCK_DESTROY(sc);
561 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
563 struct ifnet *ifp = sc->sc_ifp;
566 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
569 LAGG_WLOCK_ASSERT(sc);
571 * Set the link layer address on the lagg interface.
572 * lagg_proto_lladdr() notifies the MAC change to
573 * the aggregation protocol. iflladdr_event handler which
574 * may trigger gratuitous ARPs for INET will be handled in
577 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
578 lagg_proto_lladdr(sc);
580 bzero(&lp, sizeof(lp));
581 lp.lp_ifp = sc->sc_ifp;
584 lagg_port_lladdr(&lp, lladdr);
588 lagg_capabilities(struct lagg_softc *sc)
590 struct lagg_port *lp;
591 int cap = ~0, ena = ~0;
593 struct ifnet_hw_tsomax hw_tsomax;
595 LAGG_WLOCK_ASSERT(sc);
597 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
599 /* Get capabilities from the lagg ports */
600 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
601 cap &= lp->lp_ifp->if_capabilities;
602 ena &= lp->lp_ifp->if_capenable;
603 hwa &= lp->lp_ifp->if_hwassist;
604 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
606 cap = (cap == ~0 ? 0 : cap);
607 ena = (ena == ~0 ? 0 : ena);
608 hwa = (hwa == ~0 ? 0 : hwa);
610 if (sc->sc_ifp->if_capabilities != cap ||
611 sc->sc_ifp->if_capenable != ena ||
612 sc->sc_ifp->if_hwassist != hwa ||
613 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
614 sc->sc_ifp->if_capabilities = cap;
615 sc->sc_ifp->if_capenable = ena;
616 sc->sc_ifp->if_hwassist = hwa;
617 getmicrotime(&sc->sc_ifp->if_lastchange);
619 if (sc->sc_ifflags & IFF_DEBUG)
620 if_printf(sc->sc_ifp,
621 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
626 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
628 struct lagg_softc *sc = lp->lp_softc;
629 struct ifnet *ifp = lp->lp_ifp;
630 struct lagg_llq *llq;
634 LAGG_WLOCK_ASSERT(sc);
636 primary = (sc->sc_primary->lp_ifp == ifp) ? 1 : 0;
637 if (primary == 0 && (lp->lp_detaching ||
638 memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0))
641 /* Check to make sure its not already queued to be changed */
642 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
643 if (llq->llq_ifp == ifp && llq->llq_primary == primary) {
650 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
651 if (llq == NULL) /* XXX what to do */
655 /* Update the lladdr even if pending, it may have changed */
657 llq->llq_primary = primary;
658 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
661 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
663 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
667 * Set the interface MAC address from a taskqueue to avoid a LOR.
670 lagg_port_setlladdr(void *arg, int pending)
672 struct lagg_softc *sc = (struct lagg_softc *)arg;
673 struct lagg_llq *llq, *head;
677 /* Grab a local reference of the queue and remove it from the softc */
679 head = SLIST_FIRST(&sc->sc_llq_head);
680 SLIST_FIRST(&sc->sc_llq_head) = NULL;
684 * Traverse the queue and set the lladdr on each ifp. It is safe to do
685 * unlocked as we have the only reference to it.
687 for (llq = head; llq != NULL; llq = head) {
690 CURVNET_SET(ifp->if_vnet);
691 if (llq->llq_primary == 0) {
693 * Set the link layer address on the laggport interface.
694 * if_setlladdr() triggers gratuitous ARPs for INET.
696 error = if_setlladdr(ifp, llq->llq_lladdr,
699 printf("%s: setlladdr failed on %s\n", __func__,
702 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
704 head = SLIST_NEXT(llq, llq_entries);
710 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
712 struct lagg_softc *sc_ptr;
713 struct lagg_port *lp, *tlp;
717 LAGG_WLOCK_ASSERT(sc);
719 /* Limit the maximal number of lagg ports */
720 if (sc->sc_count >= LAGG_MAX_PORTS)
723 /* Check if port has already been associated to a lagg */
724 if (ifp->if_lagg != NULL) {
725 /* Port is already in the current lagg? */
726 lp = (struct lagg_port *)ifp->if_lagg;
727 if (lp->lp_softc == sc)
732 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
733 if (ifp->if_type != IFT_ETHER)
734 return (EPROTONOSUPPORT);
736 /* Allow the first Ethernet member to define the MTU */
737 if (SLIST_EMPTY(&sc->sc_ports))
738 sc->sc_ifp->if_mtu = ifp->if_mtu;
739 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
740 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
745 if ((lp = malloc(sizeof(struct lagg_port),
746 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
749 /* Check if port is a stacked lagg */
751 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
752 if (ifp == sc_ptr->sc_ifp) {
756 /* XXX disable stacking for the moment, its untested */
757 #ifdef LAGG_PORT_STACKING
758 lp->lp_flags |= LAGG_PORT_STACK;
759 if (lagg_port_checkstacking(sc_ptr) >=
770 /* Change the interface type */
771 lp->lp_iftype = ifp->if_type;
772 ifp->if_type = IFT_IEEE8023ADLAG;
774 lp->lp_ioctl = ifp->if_ioctl;
775 ifp->if_ioctl = lagg_port_ioctl;
776 lp->lp_output = ifp->if_output;
777 ifp->if_output = lagg_port_output;
782 /* Save port link layer address */
783 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
785 if (SLIST_EMPTY(&sc->sc_ports)) {
787 lagg_lladdr(sc, IF_LLADDR(ifp));
789 /* Update link layer address for this port */
790 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
794 * Insert into the list of ports.
795 * Keep ports sorted by if_index. It is handy, when configuration
796 * is predictable and `ifconfig laggN create ...` command
797 * will lead to the same result each time.
799 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
800 if (tlp->lp_ifp->if_index < ifp->if_index && (
801 SLIST_NEXT(tlp, lp_entries) == NULL ||
802 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
807 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
809 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
812 /* Update lagg capabilities */
813 lagg_capabilities(sc);
816 /* Read port counters */
817 pval = lp->port_counters.val;
818 for (i = 0; i < IFCOUNTERS; i++, pval++)
819 *pval = ifp->if_get_counter(ifp, i);
820 /* Add multicast addresses and interface flags to this port */
821 lagg_ether_cmdmulti(lp, 1);
822 lagg_setflags(lp, 1);
824 if ((error = lagg_proto_addport(sc, lp)) != 0) {
825 /* Remove the port, without calling pr_delport. */
826 lagg_port_destroy(lp, 0);
833 #ifdef LAGG_PORT_STACKING
835 lagg_port_checkstacking(struct lagg_softc *sc)
837 struct lagg_softc *sc_ptr;
838 struct lagg_port *lp;
841 LAGG_WLOCK_ASSERT(sc);
843 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
844 if (lp->lp_flags & LAGG_PORT_STACK) {
845 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
846 m = MAX(m, lagg_port_checkstacking(sc_ptr));
855 lagg_port_destroy(struct lagg_port *lp, int rundelport)
857 struct lagg_softc *sc = lp->lp_softc;
858 struct lagg_port *lp_ptr, *lp0;
859 struct lagg_llq *llq;
860 struct ifnet *ifp = lp->lp_ifp;
861 uint64_t *pval, vdiff;
864 LAGG_WLOCK_ASSERT(sc);
867 lagg_proto_delport(sc, lp);
870 * Remove multicast addresses and interface flags from this port and
871 * reset the MAC address, skip if the interface is being detached.
873 if (!lp->lp_detaching) {
874 lagg_ether_cmdmulti(lp, 0);
875 lagg_setflags(lp, 0);
876 lagg_port_lladdr(lp, lp->lp_lladdr);
879 /* Restore interface */
880 ifp->if_type = lp->lp_iftype;
881 ifp->if_ioctl = lp->lp_ioctl;
882 ifp->if_output = lp->lp_output;
885 /* Update detached port counters */
886 pval = lp->port_counters.val;
887 for (i = 0; i < IFCOUNTERS; i++, pval++) {
888 vdiff = ifp->if_get_counter(ifp, i) - *pval;
889 sc->detached_counters.val[i] += vdiff;
892 /* Finally, remove the port from the lagg */
893 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
896 /* Update the primary interface */
897 if (lp == sc->sc_primary) {
898 uint8_t lladdr[ETHER_ADDR_LEN];
900 if ((lp0 = SLIST_FIRST(&sc->sc_ports)) == NULL) {
901 bzero(&lladdr, ETHER_ADDR_LEN);
903 bcopy(lp0->lp_lladdr,
904 lladdr, ETHER_ADDR_LEN);
906 lagg_lladdr(sc, lladdr);
909 * Update link layer address for each port. No port is
910 * marked as primary at this moment.
912 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
913 lagg_port_lladdr(lp_ptr, lladdr);
915 * Mark lp0 as the new primary. This invokes an
918 sc->sc_primary = lp0;
920 lagg_port_lladdr(lp0, lladdr);
923 /* Remove any pending lladdr changes from the queue */
924 if (lp->lp_detaching) {
925 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
926 if (llq->llq_ifp == ifp) {
927 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
930 break; /* Only appears once */
936 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
940 /* Update lagg capabilities */
941 lagg_capabilities(sc);
948 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
950 struct lagg_reqport *rp = (struct lagg_reqport *)data;
951 struct lagg_softc *sc;
952 struct lagg_port *lp = NULL;
954 struct rm_priotracker tracker;
956 /* Should be checked by the caller */
957 if (ifp->if_type != IFT_IEEE8023ADLAG ||
958 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
963 if (rp->rp_portname[0] == '\0' ||
964 ifunit(rp->rp_portname) != ifp) {
969 LAGG_RLOCK(sc, &tracker);
970 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
972 LAGG_RUNLOCK(sc, &tracker);
976 lagg_port2req(lp, rp);
977 LAGG_RUNLOCK(sc, &tracker);
981 if (lp->lp_ioctl == NULL) {
985 error = (*lp->lp_ioctl)(ifp, cmd, data);
989 /* Update lagg interface capabilities */
991 lagg_capabilities(sc);
996 /* Do not allow the MTU to be changed once joined */
1007 if (lp->lp_ioctl != NULL)
1008 return ((*lp->lp_ioctl)(ifp, cmd, data));
1014 * Requests counter @cnt data.
1016 * Counter value is calculated the following way:
1017 * 1) for each port, sum difference between current and "initial" measurements.
1018 * 2) add lagg logical interface counters.
1019 * 3) add data from detached_counters array.
1021 * We also do the following things on ports attach/detach:
1022 * 1) On port attach we store all counters it has into port_counter array.
1023 * 2) On port detach we add the different between "initial" and
1024 * current counters data to detached_counters array.
1027 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1029 struct lagg_softc *sc;
1030 struct lagg_port *lp;
1031 struct ifnet *lpifp;
1032 struct rm_priotracker tracker;
1033 uint64_t newval, oldval, vsum;
1035 /* Revise this when we've got non-generic counters. */
1036 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1038 sc = (struct lagg_softc *)ifp->if_softc;
1039 LAGG_RLOCK(sc, &tracker);
1042 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1043 /* Saved attached value */
1044 oldval = lp->port_counters.val[cnt];
1047 newval = lpifp->if_get_counter(lpifp, cnt);
1048 /* Calculate diff and save new */
1049 vsum += newval - oldval;
1053 * Add counter data which might be added by upper
1054 * layer protocols operating on logical interface.
1056 vsum += if_get_counter_default(ifp, cnt);
1059 * Add counter data from detached ports counters
1061 vsum += sc->detached_counters.val[cnt];
1063 LAGG_RUNLOCK(sc, &tracker);
1069 * For direct output to child ports.
1072 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1073 const struct sockaddr *dst, struct route *ro)
1075 struct lagg_port *lp = ifp->if_lagg;
1077 switch (dst->sa_family) {
1078 case pseudo_AF_HDRCMPLT:
1080 return ((*lp->lp_output)(ifp, m, dst, ro));
1083 /* drop any other frames */
1089 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1091 struct lagg_port *lp;
1092 struct lagg_softc *sc;
1094 if ((lp = ifp->if_lagg) == NULL)
1096 /* If the ifnet is just being renamed, don't do anything. */
1097 if (ifp->if_flags & IFF_RENAMING)
1103 lp->lp_detaching = 1;
1104 lagg_port_destroy(lp, 1);
1109 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1111 struct lagg_softc *sc = lp->lp_softc;
1113 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1114 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1115 rp->rp_prio = lp->lp_prio;
1116 rp->rp_flags = lp->lp_flags;
1117 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1119 /* Add protocol specific flags */
1120 switch (sc->sc_proto) {
1121 case LAGG_PROTO_FAILOVER:
1122 if (lp == sc->sc_primary)
1123 rp->rp_flags |= LAGG_PORT_MASTER;
1124 if (lp == lagg_link_active(sc, sc->sc_primary))
1125 rp->rp_flags |= LAGG_PORT_ACTIVE;
1128 case LAGG_PROTO_ROUNDROBIN:
1129 case LAGG_PROTO_LOADBALANCE:
1130 case LAGG_PROTO_BROADCAST:
1131 if (LAGG_PORTACTIVE(lp))
1132 rp->rp_flags |= LAGG_PORT_ACTIVE;
1135 case LAGG_PROTO_LACP:
1136 /* LACP has a different definition of active */
1137 if (lacp_isactive(lp))
1138 rp->rp_flags |= LAGG_PORT_ACTIVE;
1139 if (lacp_iscollecting(lp))
1140 rp->rp_flags |= LAGG_PORT_COLLECTING;
1141 if (lacp_isdistributing(lp))
1142 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1149 lagg_init(void *xsc)
1151 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1152 struct lagg_port *lp;
1153 struct ifnet *ifp = sc->sc_ifp;
1155 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1160 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1161 /* Update the port lladdrs */
1162 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1163 lagg_port_lladdr(lp, IF_LLADDR(ifp));
1165 lagg_proto_init(sc);
1171 lagg_stop(struct lagg_softc *sc)
1173 struct ifnet *ifp = sc->sc_ifp;
1175 LAGG_WLOCK_ASSERT(sc);
1177 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1180 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1182 lagg_proto_stop(sc);
1186 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1188 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1189 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1190 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1191 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1192 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1193 struct ifreq *ifr = (struct ifreq *)data;
1194 struct lagg_port *lp;
1196 struct thread *td = curthread;
1198 int count, buflen, len, error = 0;
1199 struct rm_priotracker tracker;
1201 bzero(&rpbuf, sizeof(rpbuf));
1205 LAGG_RLOCK(sc, &tracker);
1207 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1209 buflen = count * sizeof(struct lagg_reqport);
1210 LAGG_RUNLOCK(sc, &tracker);
1212 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1214 LAGG_RLOCK(sc, &tracker);
1215 ra->ra_proto = sc->sc_proto;
1216 lagg_proto_request(sc, &ra->ra_psc);
1219 len = min(ra->ra_size, buflen);
1220 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1221 if (len < sizeof(rpbuf))
1224 lagg_port2req(lp, &rpbuf);
1225 memcpy(buf, &rpbuf, sizeof(rpbuf));
1227 buf += sizeof(rpbuf);
1228 len -= sizeof(rpbuf);
1230 LAGG_RUNLOCK(sc, &tracker);
1231 ra->ra_ports = count;
1232 ra->ra_size = count * sizeof(rpbuf);
1233 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1234 free(outbuf, M_TEMP);
1237 error = priv_check(td, PRIV_NET_LAGG);
1240 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) {
1241 error = EPROTONOSUPPORT;
1246 lagg_proto_detach(sc);
1247 lagg_proto_attach(sc, ra->ra_proto);
1250 ro->ro_opts = sc->sc_opts;
1251 if (sc->sc_proto == LAGG_PROTO_LACP) {
1252 struct lacp_softc *lsc;
1254 lsc = (struct lacp_softc *)sc->sc_psc;
1255 if (lsc->lsc_debug.lsc_tx_test != 0)
1256 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1257 if (lsc->lsc_debug.lsc_rx_test != 0)
1258 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1259 if (lsc->lsc_strict_mode != 0)
1260 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1261 if (lsc->lsc_fast_timeout != 0)
1262 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1264 ro->ro_active = sc->sc_active;
1267 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1268 ro->ro_active += LAGG_PORTACTIVE(lp);
1270 ro->ro_flapping = sc->sc_flapping;
1271 ro->ro_flowid_shift = sc->flowid_shift;
1274 error = priv_check(td, PRIV_NET_LAGG);
1277 if (ro->ro_opts == 0)
1280 * Set options. LACP options are stored in sc->sc_psc,
1285 switch (ro->ro_opts) {
1286 case LAGG_OPT_USE_FLOWID:
1287 case -LAGG_OPT_USE_FLOWID:
1288 case LAGG_OPT_FLOWIDSHIFT:
1292 case LAGG_OPT_LACP_TXTEST:
1293 case -LAGG_OPT_LACP_TXTEST:
1294 case LAGG_OPT_LACP_RXTEST:
1295 case -LAGG_OPT_LACP_RXTEST:
1296 case LAGG_OPT_LACP_STRICT:
1297 case -LAGG_OPT_LACP_STRICT:
1298 case LAGG_OPT_LACP_TIMEOUT:
1299 case -LAGG_OPT_LACP_TIMEOUT:
1309 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1310 /* Invalid combination of options specified. */
1313 break; /* Return from SIOCSLAGGOPTS. */
1316 * Store new options into sc->sc_opts except for
1317 * FLOWIDSHIFT and LACP options.
1320 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1321 sc->flowid_shift = ro->ro_flowid_shift;
1322 else if (ro->ro_opts > 0)
1323 sc->sc_opts |= ro->ro_opts;
1325 sc->sc_opts &= ~ro->ro_opts;
1327 struct lacp_softc *lsc;
1328 struct lacp_port *lp;
1330 lsc = (struct lacp_softc *)sc->sc_psc;
1332 switch (ro->ro_opts) {
1333 case LAGG_OPT_LACP_TXTEST:
1334 lsc->lsc_debug.lsc_tx_test = 1;
1336 case -LAGG_OPT_LACP_TXTEST:
1337 lsc->lsc_debug.lsc_tx_test = 0;
1339 case LAGG_OPT_LACP_RXTEST:
1340 lsc->lsc_debug.lsc_rx_test = 1;
1342 case -LAGG_OPT_LACP_RXTEST:
1343 lsc->lsc_debug.lsc_rx_test = 0;
1345 case LAGG_OPT_LACP_STRICT:
1346 lsc->lsc_strict_mode = 1;
1348 case -LAGG_OPT_LACP_STRICT:
1349 lsc->lsc_strict_mode = 0;
1351 case LAGG_OPT_LACP_TIMEOUT:
1353 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1354 lp->lp_state |= LACP_STATE_TIMEOUT;
1356 lsc->lsc_fast_timeout = 1;
1358 case -LAGG_OPT_LACP_TIMEOUT:
1360 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1361 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1363 lsc->lsc_fast_timeout = 0;
1369 case SIOCGLAGGFLAGS:
1371 LAGG_RLOCK(sc, &tracker);
1372 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1373 rf->rf_flags |= LAGG_F_HASHL2;
1374 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1375 rf->rf_flags |= LAGG_F_HASHL3;
1376 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1377 rf->rf_flags |= LAGG_F_HASHL4;
1378 LAGG_RUNLOCK(sc, &tracker);
1381 error = priv_check(td, PRIV_NET_LAGG);
1384 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1390 if (rf->rf_flags & LAGG_F_HASHL2)
1391 sc->sc_flags |= MBUF_HASHFLAG_L2;
1392 if (rf->rf_flags & LAGG_F_HASHL3)
1393 sc->sc_flags |= MBUF_HASHFLAG_L3;
1394 if (rf->rf_flags & LAGG_F_HASHL4)
1395 sc->sc_flags |= MBUF_HASHFLAG_L4;
1399 if (rp->rp_portname[0] == '\0' ||
1400 (tpif = ifunit(rp->rp_portname)) == NULL) {
1405 LAGG_RLOCK(sc, &tracker);
1406 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1407 lp->lp_softc != sc) {
1409 LAGG_RUNLOCK(sc, &tracker);
1413 lagg_port2req(lp, rp);
1414 LAGG_RUNLOCK(sc, &tracker);
1417 error = priv_check(td, PRIV_NET_LAGG);
1420 if (rp->rp_portname[0] == '\0' ||
1421 (tpif = ifunit(rp->rp_portname)) == NULL) {
1427 * A laggport interface should not have inet6 address
1428 * because two interfaces with a valid link-local
1429 * scope zone must not be merged in any form. This
1430 * restriction is needed to prevent violation of
1431 * link-local scope zone. Attempts to add a laggport
1432 * interface which has inet6 addresses triggers
1433 * removal of all inet6 addresses on the member
1436 if (in6ifa_llaonifp(tpif)) {
1438 if_printf(sc->sc_ifp,
1439 "IPv6 addresses on %s have been removed "
1440 "before adding it as a member to prevent "
1441 "IPv6 address scope violation.\n",
1446 error = lagg_port_create(sc, tpif);
1449 case SIOCSLAGGDELPORT:
1450 error = priv_check(td, PRIV_NET_LAGG);
1453 if (rp->rp_portname[0] == '\0' ||
1454 (tpif = ifunit(rp->rp_portname)) == NULL) {
1460 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1461 lp->lp_softc != sc) {
1467 error = lagg_port_destroy(lp, 1);
1471 /* Set flags on ports too */
1473 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1474 lagg_setflags(lp, 1);
1478 if (!(ifp->if_flags & IFF_UP) &&
1479 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1481 * If interface is marked down and it is running,
1482 * then stop and disable it.
1487 } else if ((ifp->if_flags & IFF_UP) &&
1488 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1490 * If interface is marked up and it is stopped, then
1493 (*ifp->if_init)(sc);
1499 error = lagg_ether_setmulti(sc);
1504 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1509 /* Do not allow the MTU or caps to be directly changed */
1514 error = ether_ioctl(ifp, cmd, data);
1521 lagg_ether_setmulti(struct lagg_softc *sc)
1523 struct lagg_port *lp;
1525 LAGG_WLOCK_ASSERT(sc);
1527 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1528 /* First, remove any existing filter entries. */
1529 lagg_ether_cmdmulti(lp, 0);
1530 /* copy all addresses from the lagg interface to the port */
1531 lagg_ether_cmdmulti(lp, 1);
1537 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1539 struct lagg_softc *sc = lp->lp_softc;
1540 struct ifnet *ifp = lp->lp_ifp;
1541 struct ifnet *scifp = sc->sc_ifp;
1543 struct ifmultiaddr *ifma;
1546 LAGG_WLOCK_ASSERT(sc);
1549 IF_ADDR_WLOCK(scifp);
1550 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1551 if (ifma->ifma_addr->sa_family != AF_LINK)
1553 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1555 IF_ADDR_WUNLOCK(scifp);
1558 bcopy(ifma->ifma_addr, &mc->mc_addr,
1559 ifma->ifma_addr->sa_len);
1560 mc->mc_addr.sdl_index = ifp->if_index;
1562 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1564 IF_ADDR_WUNLOCK(scifp);
1565 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1566 error = if_addmulti(ifp,
1567 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1572 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1573 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1574 if (mc->mc_ifma && !lp->lp_detaching)
1575 if_delmulti_ifma(mc->mc_ifma);
1582 /* Handle a ref counted flag that should be set on the lagg port as well */
1584 lagg_setflag(struct lagg_port *lp, int flag, int status,
1585 int (*func)(struct ifnet *, int))
1587 struct lagg_softc *sc = lp->lp_softc;
1588 struct ifnet *scifp = sc->sc_ifp;
1589 struct ifnet *ifp = lp->lp_ifp;
1592 LAGG_WLOCK_ASSERT(sc);
1594 status = status ? (scifp->if_flags & flag) : 0;
1595 /* Now "status" contains the flag value or 0 */
1598 * See if recorded ports status is different from what
1599 * we want it to be. If it is, flip it. We record ports
1600 * status in lp_ifflags so that we won't clear ports flag
1601 * we haven't set. In fact, we don't clear or set ports
1602 * flags directly, but get or release references to them.
1603 * That's why we can be sure that recorded flags still are
1604 * in accord with actual ports flags.
1606 if (status != (lp->lp_ifflags & flag)) {
1607 error = (*func)(ifp, status);
1610 lp->lp_ifflags &= ~flag;
1611 lp->lp_ifflags |= status;
1617 * Handle IFF_* flags that require certain changes on the lagg port
1618 * if "status" is true, update ports flags respective to the lagg
1619 * if "status" is false, forcedly clear the flags set on port.
1622 lagg_setflags(struct lagg_port *lp, int status)
1626 for (i = 0; lagg_pflags[i].flag; i++) {
1627 error = lagg_setflag(lp, lagg_pflags[i].flag,
1628 status, lagg_pflags[i].func);
1636 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1638 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1639 int error, len, mcast;
1640 struct rm_priotracker tracker;
1642 len = m->m_pkthdr.len;
1643 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1645 LAGG_RLOCK(sc, &tracker);
1646 /* We need a Tx algorithm and at least one port */
1647 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1648 LAGG_RUNLOCK(sc, &tracker);
1650 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1654 ETHER_BPF_MTAP(ifp, m);
1656 error = lagg_proto_start(sc, m);
1657 LAGG_RUNLOCK(sc, &tracker);
1660 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1666 * The ifp->if_qflush entry point for lagg(4) is no-op.
1669 lagg_qflush(struct ifnet *ifp __unused)
1673 static struct mbuf *
1674 lagg_input(struct ifnet *ifp, struct mbuf *m)
1676 struct lagg_port *lp = ifp->if_lagg;
1677 struct lagg_softc *sc = lp->lp_softc;
1678 struct ifnet *scifp = sc->sc_ifp;
1679 struct rm_priotracker tracker;
1681 LAGG_RLOCK(sc, &tracker);
1682 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1683 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1684 sc->sc_proto == LAGG_PROTO_NONE) {
1685 LAGG_RUNLOCK(sc, &tracker);
1690 ETHER_BPF_MTAP(scifp, m);
1692 if (lp->lp_detaching != 0) {
1696 m = lagg_proto_input(sc, lp, m);
1699 if (scifp->if_flags & IFF_MONITOR) {
1705 LAGG_RUNLOCK(sc, &tracker);
1710 lagg_media_change(struct ifnet *ifp)
1712 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1714 if (sc->sc_ifflags & IFF_DEBUG)
1715 printf("%s\n", __func__);
1722 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1724 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1725 struct lagg_port *lp;
1726 struct rm_priotracker tracker;
1728 imr->ifm_status = IFM_AVALID;
1729 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1731 LAGG_RLOCK(sc, &tracker);
1732 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1733 if (LAGG_PORTACTIVE(lp))
1734 imr->ifm_status |= IFM_ACTIVE;
1736 LAGG_RUNLOCK(sc, &tracker);
1740 lagg_linkstate(struct lagg_softc *sc)
1742 struct lagg_port *lp;
1743 int new_link = LINK_STATE_DOWN;
1746 /* Our link is considered up if at least one of our ports is active */
1747 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1748 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1749 new_link = LINK_STATE_UP;
1753 if_link_state_change(sc->sc_ifp, new_link);
1755 /* Update if_baudrate to reflect the max possible speed */
1756 switch (sc->sc_proto) {
1757 case LAGG_PROTO_FAILOVER:
1758 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1759 sc->sc_primary->lp_ifp->if_baudrate : 0;
1761 case LAGG_PROTO_ROUNDROBIN:
1762 case LAGG_PROTO_LOADBALANCE:
1763 case LAGG_PROTO_BROADCAST:
1765 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1766 speed += lp->lp_ifp->if_baudrate;
1767 sc->sc_ifp->if_baudrate = speed;
1769 case LAGG_PROTO_LACP:
1770 /* LACP updates if_baudrate itself */
1776 lagg_port_state(struct ifnet *ifp, int state)
1778 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1779 struct lagg_softc *sc = NULL;
1788 lagg_proto_linkstate(sc, lp);
1793 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1795 struct lagg_port *lp_next, *rval = NULL;
1796 // int new_link = LINK_STATE_DOWN;
1798 LAGG_RLOCK_ASSERT(sc);
1800 * Search a port which reports an active link state.
1805 if (LAGG_PORTACTIVE(lp)) {
1809 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1810 LAGG_PORTACTIVE(lp_next)) {
1816 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1817 if (LAGG_PORTACTIVE(lp_next)) {
1826 * The IEEE 802.1D standard assumes that a lagg with
1827 * multiple ports is always full duplex. This is valid
1828 * for load sharing laggs and if at least two links
1829 * are active. Unfortunately, checking the latter would
1830 * be too expensive at this point.
1832 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1834 new_link = LINK_STATE_FULL_DUPLEX;
1836 new_link = rval->lp_link_state;
1844 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1847 return (ifp->if_transmit)(ifp, m);
1851 * Simple round robin aggregation
1854 lagg_rr_attach(struct lagg_softc *sc)
1856 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1861 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1863 struct lagg_port *lp;
1866 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1868 lp = SLIST_FIRST(&sc->sc_ports);
1870 lp = SLIST_NEXT(lp, lp_entries);
1873 * Check the port's link state. This will return the next active
1874 * port if the link is down or the port is NULL.
1876 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1882 return (lagg_enqueue(lp->lp_ifp, m));
1885 static struct mbuf *
1886 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1888 struct ifnet *ifp = sc->sc_ifp;
1890 /* Just pass in the packet to our lagg device */
1891 m->m_pkthdr.rcvif = ifp;
1900 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1902 int active_ports = 0;
1905 struct lagg_port *lp, *last = NULL;
1908 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1909 if (!LAGG_PORTACTIVE(lp))
1915 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1922 ret = lagg_enqueue(last->lp_ifp, m0);
1932 if ((last = lagg_link_active(sc, last)) == NULL) {
1937 ret = lagg_enqueue(last->lp_ifp, m);
1948 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1950 struct ifnet *ifp = sc->sc_ifp;
1952 /* Just pass in the packet to our lagg device */
1953 m->m_pkthdr.rcvif = ifp;
1961 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1963 struct lagg_port *lp;
1965 /* Use the master port if active or the next available port */
1966 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1972 return (lagg_enqueue(lp->lp_ifp, m));
1975 static struct mbuf *
1976 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1978 struct ifnet *ifp = sc->sc_ifp;
1979 struct lagg_port *tmp_tp;
1981 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
1982 m->m_pkthdr.rcvif = ifp;
1986 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1987 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1989 * If tmp_tp is null, we've recieved a packet when all
1990 * our links are down. Weird, but process it anyways.
1992 if ((tmp_tp == NULL || tmp_tp == lp)) {
1993 m->m_pkthdr.rcvif = ifp;
2006 lagg_lb_attach(struct lagg_softc *sc)
2008 struct lagg_port *lp;
2011 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2013 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2015 lb->lb_key = m_ether_tcpip_hash_init();
2018 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2019 lagg_lb_port_create(lp);
2023 lagg_lb_detach(struct lagg_softc *sc)
2027 lb = (struct lagg_lb *)sc->sc_psc;
2034 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2036 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2037 struct lagg_port *lp_next;
2040 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2041 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2044 if (i >= LAGG_MAX_PORTS)
2046 if (sc->sc_ifflags & IFF_DEBUG)
2047 printf("%s: port %s at index %d\n",
2048 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2049 lb->lb_ports[i++] = lp_next;
2056 lagg_lb_port_create(struct lagg_port *lp)
2058 struct lagg_softc *sc = lp->lp_softc;
2059 return (lagg_lb_porttable(sc, NULL));
2063 lagg_lb_port_destroy(struct lagg_port *lp)
2065 struct lagg_softc *sc = lp->lp_softc;
2066 lagg_lb_porttable(sc, lp);
2070 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2072 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2073 struct lagg_port *lp = NULL;
2076 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2077 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2078 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2080 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2082 lp = lb->lb_ports[p];
2085 * Check the port's link state. This will return the next active
2086 * port if the link is down or the port is NULL.
2088 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2094 return (lagg_enqueue(lp->lp_ifp, m));
2097 static struct mbuf *
2098 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2100 struct ifnet *ifp = sc->sc_ifp;
2102 /* Just pass in the packet to our lagg device */
2103 m->m_pkthdr.rcvif = ifp;
2112 lagg_lacp_attach(struct lagg_softc *sc)
2114 struct lagg_port *lp;
2117 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2118 lacp_port_create(lp);
2122 lagg_lacp_detach(struct lagg_softc *sc)
2124 struct lagg_port *lp;
2127 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2128 lacp_port_destroy(lp);
2138 lagg_lacp_lladdr(struct lagg_softc *sc)
2140 struct lagg_port *lp;
2142 /* purge all the lacp ports */
2143 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2144 lacp_port_destroy(lp);
2146 /* add them back in */
2147 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2148 lacp_port_create(lp);
2152 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2154 struct lagg_port *lp;
2156 lp = lacp_select_tx_port(sc, m);
2163 return (lagg_enqueue(lp->lp_ifp, m));
2166 static struct mbuf *
2167 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2169 struct ifnet *ifp = sc->sc_ifp;
2170 struct ether_header *eh;
2173 eh = mtod(m, struct ether_header *);
2174 etype = ntohs(eh->ether_type);
2176 /* Tap off LACP control messages */
2177 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2178 m = lacp_input(lp, m);
2184 * If the port is not collecting or not in the active aggregator then
2187 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2192 m->m_pkthdr.rcvif = ifp;