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 *, lagg_llqtype);
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);
546 LAGG_UNLOCK_ASSERT(sc);
548 ifmedia_removeall(&sc->sc_media);
553 SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
556 taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
557 LAGG_LOCK_DESTROY(sc);
562 * Set link-layer address on the lagg interface itself.
564 * Set noinline to be dtrace-friendly
566 static __noinline void
567 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
569 struct ifnet *ifp = sc->sc_ifp;
572 if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
575 LAGG_WLOCK_ASSERT(sc);
577 * Set the link layer address on the lagg interface.
578 * lagg_proto_lladdr() notifies the MAC change to
579 * the aggregation protocol. iflladdr_event handler which
580 * may trigger gratuitous ARPs for INET will be handled in
583 bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
584 lagg_proto_lladdr(sc);
587 * Send notification request for lagg interface
588 * itself. Note that new lladdr is already set.
590 bzero(&lp, sizeof(lp));
591 lp.lp_ifp = sc->sc_ifp;
594 /* Do not request lladdr change */
595 lagg_port_lladdr(&lp, lladdr, LAGG_LLQTYPE_VIRT);
599 lagg_capabilities(struct lagg_softc *sc)
601 struct lagg_port *lp;
602 int cap = ~0, ena = ~0;
604 struct ifnet_hw_tsomax hw_tsomax;
606 LAGG_WLOCK_ASSERT(sc);
608 memset(&hw_tsomax, 0, sizeof(hw_tsomax));
610 /* Get capabilities from the lagg ports */
611 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
612 cap &= lp->lp_ifp->if_capabilities;
613 ena &= lp->lp_ifp->if_capenable;
614 hwa &= lp->lp_ifp->if_hwassist;
615 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
617 cap = (cap == ~0 ? 0 : cap);
618 ena = (ena == ~0 ? 0 : ena);
619 hwa = (hwa == ~0 ? 0 : hwa);
621 if (sc->sc_ifp->if_capabilities != cap ||
622 sc->sc_ifp->if_capenable != ena ||
623 sc->sc_ifp->if_hwassist != hwa ||
624 if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
625 sc->sc_ifp->if_capabilities = cap;
626 sc->sc_ifp->if_capenable = ena;
627 sc->sc_ifp->if_hwassist = hwa;
628 getmicrotime(&sc->sc_ifp->if_lastchange);
630 if (sc->sc_ifflags & IFF_DEBUG)
631 if_printf(sc->sc_ifp,
632 "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
637 * Enqueue interface lladdr notification.
638 * If request is already queued, it is updated.
639 * If setting lladdr is also desired, @do_change has to be set to 1.
641 * Set noinline to be dtrace-friendly
643 static __noinline void
644 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr, lagg_llqtype llq_type)
646 struct lagg_softc *sc = lp->lp_softc;
647 struct ifnet *ifp = lp->lp_ifp;
648 struct lagg_llq *llq;
650 LAGG_WLOCK_ASSERT(sc);
653 * Do not enqueue requests where lladdr is the same for
654 * "physical" interfaces (e.g. ports in lagg)
656 if (llq_type == LAGG_LLQTYPE_PHYS &&
657 memcmp(IF_LLADDR(ifp), lladdr, ETHER_ADDR_LEN) == 0)
660 /* Check to make sure its not already queued to be changed */
661 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
662 if (llq->llq_ifp == ifp) {
663 /* Update lladdr, it may have changed */
664 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
669 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT | M_ZERO);
670 if (llq == NULL) /* XXX what to do */
674 llq->llq_type = llq_type;
675 bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
676 /* XXX: We should insert to tail */
677 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
679 taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
683 * Set the interface MAC address from a taskqueue to avoid a LOR.
685 * Set noinline to be dtrace-friendly
687 static __noinline void
688 lagg_port_setlladdr(void *arg, int pending)
690 struct lagg_softc *sc = (struct lagg_softc *)arg;
691 struct lagg_llq *llq, *head;
694 /* Grab a local reference of the queue and remove it from the softc */
696 head = SLIST_FIRST(&sc->sc_llq_head);
697 SLIST_FIRST(&sc->sc_llq_head) = NULL;
701 * Traverse the queue and set the lladdr on each ifp. It is safe to do
702 * unlocked as we have the only reference to it.
704 for (llq = head; llq != NULL; llq = head) {
707 CURVNET_SET(ifp->if_vnet);
710 * Set the link layer address on the laggport interface.
711 * Note that if_setlladdr() or iflladdr_event handler
712 * may result in arp transmission / lltable updates.
714 if (llq->llq_type == LAGG_LLQTYPE_PHYS)
715 if_setlladdr(ifp, llq->llq_lladdr,
718 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
720 head = SLIST_NEXT(llq, llq_entries);
726 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
728 struct lagg_softc *sc_ptr;
729 struct lagg_port *lp, *tlp;
733 LAGG_WLOCK_ASSERT(sc);
735 /* Limit the maximal number of lagg ports */
736 if (sc->sc_count >= LAGG_MAX_PORTS)
739 /* Check if port has already been associated to a lagg */
740 if (ifp->if_lagg != NULL) {
741 /* Port is already in the current lagg? */
742 lp = (struct lagg_port *)ifp->if_lagg;
743 if (lp->lp_softc == sc)
748 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
749 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
750 return (EPROTONOSUPPORT);
752 /* Allow the first Ethernet member to define the MTU */
753 if (SLIST_EMPTY(&sc->sc_ports))
754 sc->sc_ifp->if_mtu = ifp->if_mtu;
755 else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
756 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
761 if ((lp = malloc(sizeof(struct lagg_port),
762 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
765 /* Check if port is a stacked lagg */
767 SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
768 if (ifp == sc_ptr->sc_ifp) {
772 /* XXX disable stacking for the moment, its untested */
773 #ifdef LAGG_PORT_STACKING
774 lp->lp_flags |= LAGG_PORT_STACK;
775 if (lagg_port_checkstacking(sc_ptr) >=
786 /* Change the interface type */
787 lp->lp_iftype = ifp->if_type;
788 ifp->if_type = IFT_IEEE8023ADLAG;
790 lp->lp_ioctl = ifp->if_ioctl;
791 ifp->if_ioctl = lagg_port_ioctl;
792 lp->lp_output = ifp->if_output;
793 ifp->if_output = lagg_port_output;
798 /* Save port link layer address */
799 bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
801 if (SLIST_EMPTY(&sc->sc_ports)) {
803 /* First port in lagg. Update/notify lagg lladdress */
804 lagg_lladdr(sc, IF_LLADDR(ifp));
808 * Update link layer address for this port and
809 * send notifications to other subsystems.
811 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp), LAGG_LLQTYPE_PHYS);
815 * Insert into the list of ports.
816 * Keep ports sorted by if_index. It is handy, when configuration
817 * is predictable and `ifconfig laggN create ...` command
818 * will lead to the same result each time.
820 SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
821 if (tlp->lp_ifp->if_index < ifp->if_index && (
822 SLIST_NEXT(tlp, lp_entries) == NULL ||
823 SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
828 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
830 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
833 /* Update lagg capabilities */
834 lagg_capabilities(sc);
837 /* Read port counters */
838 pval = lp->port_counters.val;
839 for (i = 0; i < IFCOUNTERS; i++, pval++)
840 *pval = ifp->if_get_counter(ifp, i);
841 /* Add multicast addresses and interface flags to this port */
842 lagg_ether_cmdmulti(lp, 1);
843 lagg_setflags(lp, 1);
845 if ((error = lagg_proto_addport(sc, lp)) != 0) {
846 /* Remove the port, without calling pr_delport. */
847 lagg_port_destroy(lp, 0);
854 #ifdef LAGG_PORT_STACKING
856 lagg_port_checkstacking(struct lagg_softc *sc)
858 struct lagg_softc *sc_ptr;
859 struct lagg_port *lp;
862 LAGG_WLOCK_ASSERT(sc);
864 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
865 if (lp->lp_flags & LAGG_PORT_STACK) {
866 sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
867 m = MAX(m, lagg_port_checkstacking(sc_ptr));
876 lagg_port_destroy(struct lagg_port *lp, int rundelport)
878 struct lagg_softc *sc = lp->lp_softc;
879 struct lagg_port *lp_ptr, *lp0;
880 struct lagg_llq *llq;
881 struct ifnet *ifp = lp->lp_ifp;
882 uint64_t *pval, vdiff;
885 LAGG_WLOCK_ASSERT(sc);
888 lagg_proto_delport(sc, lp);
891 * Remove multicast addresses and interface flags from this port and
892 * reset the MAC address, skip if the interface is being detached.
894 if (!lp->lp_detaching) {
895 lagg_ether_cmdmulti(lp, 0);
896 lagg_setflags(lp, 0);
897 lagg_port_lladdr(lp, lp->lp_lladdr, LAGG_LLQTYPE_PHYS);
900 /* Restore interface */
901 ifp->if_type = lp->lp_iftype;
902 ifp->if_ioctl = lp->lp_ioctl;
903 ifp->if_output = lp->lp_output;
906 /* Update detached port counters */
907 pval = lp->port_counters.val;
908 for (i = 0; i < IFCOUNTERS; i++, pval++) {
909 vdiff = ifp->if_get_counter(ifp, i) - *pval;
910 sc->detached_counters.val[i] += vdiff;
913 /* Finally, remove the port from the lagg */
914 SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
917 /* Update the primary interface */
918 if (lp == sc->sc_primary) {
919 uint8_t lladdr[ETHER_ADDR_LEN];
921 if ((lp0 = SLIST_FIRST(&sc->sc_ports)) == NULL) {
922 bzero(&lladdr, ETHER_ADDR_LEN);
924 bcopy(lp0->lp_lladdr,
925 lladdr, ETHER_ADDR_LEN);
927 lagg_lladdr(sc, lladdr);
929 /* Mark lp0 as new primary */
930 sc->sc_primary = lp0;
933 * Enqueue lladdr update/notification for each port
934 * (new primary needs update as well, to switch from
935 * old lladdr to its 'real' one).
937 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
938 lagg_port_lladdr(lp_ptr, lladdr, LAGG_LLQTYPE_PHYS);
941 /* Remove any pending lladdr changes from the queue */
942 if (lp->lp_detaching) {
943 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
944 if (llq->llq_ifp == ifp) {
945 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
948 break; /* Only appears once */
954 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
958 /* Update lagg capabilities */
959 lagg_capabilities(sc);
966 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
968 struct lagg_reqport *rp = (struct lagg_reqport *)data;
969 struct lagg_softc *sc;
970 struct lagg_port *lp = NULL;
972 struct rm_priotracker tracker;
974 /* Should be checked by the caller */
975 if (ifp->if_type != IFT_IEEE8023ADLAG ||
976 (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
981 if (rp->rp_portname[0] == '\0' ||
982 ifunit(rp->rp_portname) != ifp) {
987 LAGG_RLOCK(sc, &tracker);
988 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
990 LAGG_RUNLOCK(sc, &tracker);
994 lagg_port2req(lp, rp);
995 LAGG_RUNLOCK(sc, &tracker);
999 if (lp->lp_ioctl == NULL) {
1003 error = (*lp->lp_ioctl)(ifp, cmd, data);
1007 /* Update lagg interface capabilities */
1009 lagg_capabilities(sc);
1014 /* Do not allow the MTU to be changed once joined */
1025 if (lp->lp_ioctl != NULL)
1026 return ((*lp->lp_ioctl)(ifp, cmd, data));
1032 * Requests counter @cnt data.
1034 * Counter value is calculated the following way:
1035 * 1) for each port, sum difference between current and "initial" measurements.
1036 * 2) add lagg logical interface counters.
1037 * 3) add data from detached_counters array.
1039 * We also do the following things on ports attach/detach:
1040 * 1) On port attach we store all counters it has into port_counter array.
1041 * 2) On port detach we add the different between "initial" and
1042 * current counters data to detached_counters array.
1045 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1047 struct lagg_softc *sc;
1048 struct lagg_port *lp;
1049 struct ifnet *lpifp;
1050 struct rm_priotracker tracker;
1051 uint64_t newval, oldval, vsum;
1053 /* Revise this when we've got non-generic counters. */
1054 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1056 sc = (struct lagg_softc *)ifp->if_softc;
1057 LAGG_RLOCK(sc, &tracker);
1060 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1061 /* Saved attached value */
1062 oldval = lp->port_counters.val[cnt];
1065 newval = lpifp->if_get_counter(lpifp, cnt);
1066 /* Calculate diff and save new */
1067 vsum += newval - oldval;
1071 * Add counter data which might be added by upper
1072 * layer protocols operating on logical interface.
1074 vsum += if_get_counter_default(ifp, cnt);
1077 * Add counter data from detached ports counters
1079 vsum += sc->detached_counters.val[cnt];
1081 LAGG_RUNLOCK(sc, &tracker);
1087 * For direct output to child ports.
1090 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1091 const struct sockaddr *dst, struct route *ro)
1093 struct lagg_port *lp = ifp->if_lagg;
1095 switch (dst->sa_family) {
1096 case pseudo_AF_HDRCMPLT:
1098 return ((*lp->lp_output)(ifp, m, dst, ro));
1101 /* drop any other frames */
1107 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1109 struct lagg_port *lp;
1110 struct lagg_softc *sc;
1112 if ((lp = ifp->if_lagg) == NULL)
1114 /* If the ifnet is just being renamed, don't do anything. */
1115 if (ifp->if_flags & IFF_RENAMING)
1121 lp->lp_detaching = 1;
1122 lagg_port_destroy(lp, 1);
1127 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1129 struct lagg_softc *sc = lp->lp_softc;
1131 strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1132 strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1133 rp->rp_prio = lp->lp_prio;
1134 rp->rp_flags = lp->lp_flags;
1135 lagg_proto_portreq(sc, lp, &rp->rp_psc);
1137 /* Add protocol specific flags */
1138 switch (sc->sc_proto) {
1139 case LAGG_PROTO_FAILOVER:
1140 if (lp == sc->sc_primary)
1141 rp->rp_flags |= LAGG_PORT_MASTER;
1142 if (lp == lagg_link_active(sc, sc->sc_primary))
1143 rp->rp_flags |= LAGG_PORT_ACTIVE;
1146 case LAGG_PROTO_ROUNDROBIN:
1147 case LAGG_PROTO_LOADBALANCE:
1148 case LAGG_PROTO_BROADCAST:
1149 if (LAGG_PORTACTIVE(lp))
1150 rp->rp_flags |= LAGG_PORT_ACTIVE;
1153 case LAGG_PROTO_LACP:
1154 /* LACP has a different definition of active */
1155 if (lacp_isactive(lp))
1156 rp->rp_flags |= LAGG_PORT_ACTIVE;
1157 if (lacp_iscollecting(lp))
1158 rp->rp_flags |= LAGG_PORT_COLLECTING;
1159 if (lacp_isdistributing(lp))
1160 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1167 lagg_init(void *xsc)
1169 struct lagg_softc *sc = (struct lagg_softc *)xsc;
1170 struct ifnet *ifp = sc->sc_ifp;
1171 struct lagg_port *lp;
1173 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1178 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1181 * Update the port lladdrs if needed.
1182 * This might be if_setlladdr() notification
1183 * that lladdr has been changed.
1185 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1186 lagg_port_lladdr(lp, IF_LLADDR(ifp), LAGG_LLQTYPE_PHYS);
1188 lagg_proto_init(sc);
1194 lagg_stop(struct lagg_softc *sc)
1196 struct ifnet *ifp = sc->sc_ifp;
1198 LAGG_WLOCK_ASSERT(sc);
1200 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1203 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1205 lagg_proto_stop(sc);
1209 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1211 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1212 struct lagg_reqall *ra = (struct lagg_reqall *)data;
1213 struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1214 struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1215 struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1216 struct ifreq *ifr = (struct ifreq *)data;
1217 struct lagg_port *lp;
1219 struct thread *td = curthread;
1221 int count, buflen, len, error = 0;
1222 struct rm_priotracker tracker;
1224 bzero(&rpbuf, sizeof(rpbuf));
1228 LAGG_RLOCK(sc, &tracker);
1230 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1232 buflen = count * sizeof(struct lagg_reqport);
1233 LAGG_RUNLOCK(sc, &tracker);
1235 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1237 LAGG_RLOCK(sc, &tracker);
1238 ra->ra_proto = sc->sc_proto;
1239 lagg_proto_request(sc, &ra->ra_psc);
1242 len = min(ra->ra_size, buflen);
1243 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1244 if (len < sizeof(rpbuf))
1247 lagg_port2req(lp, &rpbuf);
1248 memcpy(buf, &rpbuf, sizeof(rpbuf));
1250 buf += sizeof(rpbuf);
1251 len -= sizeof(rpbuf);
1253 LAGG_RUNLOCK(sc, &tracker);
1254 ra->ra_ports = count;
1255 ra->ra_size = count * sizeof(rpbuf);
1256 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1257 free(outbuf, M_TEMP);
1260 error = priv_check(td, PRIV_NET_LAGG);
1263 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) {
1264 error = EPROTONOSUPPORT;
1269 lagg_proto_detach(sc);
1270 LAGG_UNLOCK_ASSERT(sc);
1271 lagg_proto_attach(sc, ra->ra_proto);
1274 ro->ro_opts = sc->sc_opts;
1275 if (sc->sc_proto == LAGG_PROTO_LACP) {
1276 struct lacp_softc *lsc;
1278 lsc = (struct lacp_softc *)sc->sc_psc;
1279 if (lsc->lsc_debug.lsc_tx_test != 0)
1280 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1281 if (lsc->lsc_debug.lsc_rx_test != 0)
1282 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1283 if (lsc->lsc_strict_mode != 0)
1284 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1285 if (lsc->lsc_fast_timeout != 0)
1286 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1288 ro->ro_active = sc->sc_active;
1291 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1292 ro->ro_active += LAGG_PORTACTIVE(lp);
1294 ro->ro_flapping = sc->sc_flapping;
1295 ro->ro_flowid_shift = sc->flowid_shift;
1298 error = priv_check(td, PRIV_NET_LAGG);
1301 if (ro->ro_opts == 0)
1304 * Set options. LACP options are stored in sc->sc_psc,
1309 switch (ro->ro_opts) {
1310 case LAGG_OPT_USE_FLOWID:
1311 case -LAGG_OPT_USE_FLOWID:
1312 case LAGG_OPT_FLOWIDSHIFT:
1316 case LAGG_OPT_LACP_TXTEST:
1317 case -LAGG_OPT_LACP_TXTEST:
1318 case LAGG_OPT_LACP_RXTEST:
1319 case -LAGG_OPT_LACP_RXTEST:
1320 case LAGG_OPT_LACP_STRICT:
1321 case -LAGG_OPT_LACP_STRICT:
1322 case LAGG_OPT_LACP_TIMEOUT:
1323 case -LAGG_OPT_LACP_TIMEOUT:
1333 (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1334 /* Invalid combination of options specified. */
1337 break; /* Return from SIOCSLAGGOPTS. */
1340 * Store new options into sc->sc_opts except for
1341 * FLOWIDSHIFT and LACP options.
1344 if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1345 sc->flowid_shift = ro->ro_flowid_shift;
1346 else if (ro->ro_opts > 0)
1347 sc->sc_opts |= ro->ro_opts;
1349 sc->sc_opts &= ~ro->ro_opts;
1351 struct lacp_softc *lsc;
1352 struct lacp_port *lp;
1354 lsc = (struct lacp_softc *)sc->sc_psc;
1356 switch (ro->ro_opts) {
1357 case LAGG_OPT_LACP_TXTEST:
1358 lsc->lsc_debug.lsc_tx_test = 1;
1360 case -LAGG_OPT_LACP_TXTEST:
1361 lsc->lsc_debug.lsc_tx_test = 0;
1363 case LAGG_OPT_LACP_RXTEST:
1364 lsc->lsc_debug.lsc_rx_test = 1;
1366 case -LAGG_OPT_LACP_RXTEST:
1367 lsc->lsc_debug.lsc_rx_test = 0;
1369 case LAGG_OPT_LACP_STRICT:
1370 lsc->lsc_strict_mode = 1;
1372 case -LAGG_OPT_LACP_STRICT:
1373 lsc->lsc_strict_mode = 0;
1375 case LAGG_OPT_LACP_TIMEOUT:
1377 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1378 lp->lp_state |= LACP_STATE_TIMEOUT;
1380 lsc->lsc_fast_timeout = 1;
1382 case -LAGG_OPT_LACP_TIMEOUT:
1384 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1385 lp->lp_state &= ~LACP_STATE_TIMEOUT;
1387 lsc->lsc_fast_timeout = 0;
1393 case SIOCGLAGGFLAGS:
1395 LAGG_RLOCK(sc, &tracker);
1396 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1397 rf->rf_flags |= LAGG_F_HASHL2;
1398 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1399 rf->rf_flags |= LAGG_F_HASHL3;
1400 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1401 rf->rf_flags |= LAGG_F_HASHL4;
1402 LAGG_RUNLOCK(sc, &tracker);
1405 error = priv_check(td, PRIV_NET_LAGG);
1408 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1414 if (rf->rf_flags & LAGG_F_HASHL2)
1415 sc->sc_flags |= MBUF_HASHFLAG_L2;
1416 if (rf->rf_flags & LAGG_F_HASHL3)
1417 sc->sc_flags |= MBUF_HASHFLAG_L3;
1418 if (rf->rf_flags & LAGG_F_HASHL4)
1419 sc->sc_flags |= MBUF_HASHFLAG_L4;
1423 if (rp->rp_portname[0] == '\0' ||
1424 (tpif = ifunit(rp->rp_portname)) == NULL) {
1429 LAGG_RLOCK(sc, &tracker);
1430 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1431 lp->lp_softc != sc) {
1433 LAGG_RUNLOCK(sc, &tracker);
1437 lagg_port2req(lp, rp);
1438 LAGG_RUNLOCK(sc, &tracker);
1441 error = priv_check(td, PRIV_NET_LAGG);
1444 if (rp->rp_portname[0] == '\0' ||
1445 (tpif = ifunit(rp->rp_portname)) == NULL) {
1451 * A laggport interface should not have inet6 address
1452 * because two interfaces with a valid link-local
1453 * scope zone must not be merged in any form. This
1454 * restriction is needed to prevent violation of
1455 * link-local scope zone. Attempts to add a laggport
1456 * interface which has inet6 addresses triggers
1457 * removal of all inet6 addresses on the member
1460 if (in6ifa_llaonifp(tpif)) {
1462 if_printf(sc->sc_ifp,
1463 "IPv6 addresses on %s have been removed "
1464 "before adding it as a member to prevent "
1465 "IPv6 address scope violation.\n",
1470 error = lagg_port_create(sc, tpif);
1473 case SIOCSLAGGDELPORT:
1474 error = priv_check(td, PRIV_NET_LAGG);
1477 if (rp->rp_portname[0] == '\0' ||
1478 (tpif = ifunit(rp->rp_portname)) == NULL) {
1484 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1485 lp->lp_softc != sc) {
1491 error = lagg_port_destroy(lp, 1);
1495 /* Set flags on ports too */
1497 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1498 lagg_setflags(lp, 1);
1502 if (!(ifp->if_flags & IFF_UP) &&
1503 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1505 * If interface is marked down and it is running,
1506 * then stop and disable it.
1511 } else if ((ifp->if_flags & IFF_UP) &&
1512 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1514 * If interface is marked up and it is stopped, then
1517 (*ifp->if_init)(sc);
1523 error = lagg_ether_setmulti(sc);
1528 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1533 /* Do not allow the MTU or caps to be directly changed */
1538 error = ether_ioctl(ifp, cmd, data);
1545 lagg_ether_setmulti(struct lagg_softc *sc)
1547 struct lagg_port *lp;
1549 LAGG_WLOCK_ASSERT(sc);
1551 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1552 /* First, remove any existing filter entries. */
1553 lagg_ether_cmdmulti(lp, 0);
1554 /* copy all addresses from the lagg interface to the port */
1555 lagg_ether_cmdmulti(lp, 1);
1561 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1563 struct lagg_softc *sc = lp->lp_softc;
1564 struct ifnet *ifp = lp->lp_ifp;
1565 struct ifnet *scifp = sc->sc_ifp;
1567 struct ifmultiaddr *ifma;
1570 LAGG_WLOCK_ASSERT(sc);
1573 IF_ADDR_WLOCK(scifp);
1574 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1575 if (ifma->ifma_addr->sa_family != AF_LINK)
1577 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1579 IF_ADDR_WUNLOCK(scifp);
1582 bcopy(ifma->ifma_addr, &mc->mc_addr,
1583 ifma->ifma_addr->sa_len);
1584 mc->mc_addr.sdl_index = ifp->if_index;
1586 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1588 IF_ADDR_WUNLOCK(scifp);
1589 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1590 error = if_addmulti(ifp,
1591 (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1596 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1597 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1598 if (mc->mc_ifma && !lp->lp_detaching)
1599 if_delmulti_ifma(mc->mc_ifma);
1606 /* Handle a ref counted flag that should be set on the lagg port as well */
1608 lagg_setflag(struct lagg_port *lp, int flag, int status,
1609 int (*func)(struct ifnet *, int))
1611 struct lagg_softc *sc = lp->lp_softc;
1612 struct ifnet *scifp = sc->sc_ifp;
1613 struct ifnet *ifp = lp->lp_ifp;
1616 LAGG_WLOCK_ASSERT(sc);
1618 status = status ? (scifp->if_flags & flag) : 0;
1619 /* Now "status" contains the flag value or 0 */
1622 * See if recorded ports status is different from what
1623 * we want it to be. If it is, flip it. We record ports
1624 * status in lp_ifflags so that we won't clear ports flag
1625 * we haven't set. In fact, we don't clear or set ports
1626 * flags directly, but get or release references to them.
1627 * That's why we can be sure that recorded flags still are
1628 * in accord with actual ports flags.
1630 if (status != (lp->lp_ifflags & flag)) {
1631 error = (*func)(ifp, status);
1634 lp->lp_ifflags &= ~flag;
1635 lp->lp_ifflags |= status;
1641 * Handle IFF_* flags that require certain changes on the lagg port
1642 * if "status" is true, update ports flags respective to the lagg
1643 * if "status" is false, forcedly clear the flags set on port.
1646 lagg_setflags(struct lagg_port *lp, int status)
1650 for (i = 0; lagg_pflags[i].flag; i++) {
1651 error = lagg_setflag(lp, lagg_pflags[i].flag,
1652 status, lagg_pflags[i].func);
1660 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1662 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1663 int error, len, mcast;
1664 struct rm_priotracker tracker;
1666 len = m->m_pkthdr.len;
1667 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1669 LAGG_RLOCK(sc, &tracker);
1670 /* We need a Tx algorithm and at least one port */
1671 if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1672 LAGG_RUNLOCK(sc, &tracker);
1674 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1678 ETHER_BPF_MTAP(ifp, m);
1680 error = lagg_proto_start(sc, m);
1681 LAGG_RUNLOCK(sc, &tracker);
1684 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1690 * The ifp->if_qflush entry point for lagg(4) is no-op.
1693 lagg_qflush(struct ifnet *ifp __unused)
1697 static struct mbuf *
1698 lagg_input(struct ifnet *ifp, struct mbuf *m)
1700 struct lagg_port *lp = ifp->if_lagg;
1701 struct lagg_softc *sc = lp->lp_softc;
1702 struct ifnet *scifp = sc->sc_ifp;
1703 struct rm_priotracker tracker;
1705 LAGG_RLOCK(sc, &tracker);
1706 if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1707 (lp->lp_flags & LAGG_PORT_DISABLED) ||
1708 sc->sc_proto == LAGG_PROTO_NONE) {
1709 LAGG_RUNLOCK(sc, &tracker);
1714 ETHER_BPF_MTAP(scifp, m);
1716 if (lp->lp_detaching != 0) {
1720 m = lagg_proto_input(sc, lp, m);
1723 if (scifp->if_flags & IFF_MONITOR) {
1729 LAGG_RUNLOCK(sc, &tracker);
1734 lagg_media_change(struct ifnet *ifp)
1736 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1738 if (sc->sc_ifflags & IFF_DEBUG)
1739 printf("%s\n", __func__);
1746 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1748 struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1749 struct lagg_port *lp;
1750 struct rm_priotracker tracker;
1752 imr->ifm_status = IFM_AVALID;
1753 imr->ifm_active = IFM_ETHER | IFM_AUTO;
1755 LAGG_RLOCK(sc, &tracker);
1756 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1757 if (LAGG_PORTACTIVE(lp))
1758 imr->ifm_status |= IFM_ACTIVE;
1760 LAGG_RUNLOCK(sc, &tracker);
1764 lagg_linkstate(struct lagg_softc *sc)
1766 struct lagg_port *lp;
1767 int new_link = LINK_STATE_DOWN;
1770 /* Our link is considered up if at least one of our ports is active */
1771 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1772 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1773 new_link = LINK_STATE_UP;
1777 if_link_state_change(sc->sc_ifp, new_link);
1779 /* Update if_baudrate to reflect the max possible speed */
1780 switch (sc->sc_proto) {
1781 case LAGG_PROTO_FAILOVER:
1782 sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1783 sc->sc_primary->lp_ifp->if_baudrate : 0;
1785 case LAGG_PROTO_ROUNDROBIN:
1786 case LAGG_PROTO_LOADBALANCE:
1787 case LAGG_PROTO_BROADCAST:
1789 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1790 speed += lp->lp_ifp->if_baudrate;
1791 sc->sc_ifp->if_baudrate = speed;
1793 case LAGG_PROTO_LACP:
1794 /* LACP updates if_baudrate itself */
1800 lagg_port_state(struct ifnet *ifp, int state)
1802 struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1803 struct lagg_softc *sc = NULL;
1812 lagg_proto_linkstate(sc, lp);
1817 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1819 struct lagg_port *lp_next, *rval = NULL;
1820 // int new_link = LINK_STATE_DOWN;
1822 LAGG_RLOCK_ASSERT(sc);
1824 * Search a port which reports an active link state.
1829 if (LAGG_PORTACTIVE(lp)) {
1833 if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1834 LAGG_PORTACTIVE(lp_next)) {
1840 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1841 if (LAGG_PORTACTIVE(lp_next)) {
1850 * The IEEE 802.1D standard assumes that a lagg with
1851 * multiple ports is always full duplex. This is valid
1852 * for load sharing laggs and if at least two links
1853 * are active. Unfortunately, checking the latter would
1854 * be too expensive at this point.
1856 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1858 new_link = LINK_STATE_FULL_DUPLEX;
1860 new_link = rval->lp_link_state;
1868 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1871 return (ifp->if_transmit)(ifp, m);
1875 * Simple round robin aggregation
1878 lagg_rr_attach(struct lagg_softc *sc)
1880 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1885 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1887 struct lagg_port *lp;
1890 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1892 lp = SLIST_FIRST(&sc->sc_ports);
1894 lp = SLIST_NEXT(lp, lp_entries);
1897 * Check the port's link state. This will return the next active
1898 * port if the link is down or the port is NULL.
1900 if ((lp = lagg_link_active(sc, lp)) == NULL) {
1906 return (lagg_enqueue(lp->lp_ifp, m));
1909 static struct mbuf *
1910 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1912 struct ifnet *ifp = sc->sc_ifp;
1914 /* Just pass in the packet to our lagg device */
1915 m->m_pkthdr.rcvif = ifp;
1924 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1926 int active_ports = 0;
1929 struct lagg_port *lp, *last = NULL;
1932 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1933 if (!LAGG_PORTACTIVE(lp))
1939 m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1946 ret = lagg_enqueue(last->lp_ifp, m0);
1956 if ((last = lagg_link_active(sc, last)) == NULL) {
1961 ret = lagg_enqueue(last->lp_ifp, m);
1972 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1974 struct ifnet *ifp = sc->sc_ifp;
1976 /* Just pass in the packet to our lagg device */
1977 m->m_pkthdr.rcvif = ifp;
1985 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1987 struct lagg_port *lp;
1989 /* Use the master port if active or the next available port */
1990 if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1996 return (lagg_enqueue(lp->lp_ifp, m));
1999 static struct mbuf *
2000 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2002 struct ifnet *ifp = sc->sc_ifp;
2003 struct lagg_port *tmp_tp;
2005 if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2006 m->m_pkthdr.rcvif = ifp;
2010 if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2011 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2013 * If tmp_tp is null, we've recieved a packet when all
2014 * our links are down. Weird, but process it anyways.
2016 if ((tmp_tp == NULL || tmp_tp == lp)) {
2017 m->m_pkthdr.rcvif = ifp;
2030 lagg_lb_attach(struct lagg_softc *sc)
2032 struct lagg_port *lp;
2035 lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2037 sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2039 lb->lb_key = m_ether_tcpip_hash_init();
2042 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2043 lagg_lb_port_create(lp);
2047 lagg_lb_detach(struct lagg_softc *sc)
2051 lb = (struct lagg_lb *)sc->sc_psc;
2058 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2060 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2061 struct lagg_port *lp_next;
2064 bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2065 SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2068 if (i >= LAGG_MAX_PORTS)
2070 if (sc->sc_ifflags & IFF_DEBUG)
2071 printf("%s: port %s at index %d\n",
2072 sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2073 lb->lb_ports[i++] = lp_next;
2080 lagg_lb_port_create(struct lagg_port *lp)
2082 struct lagg_softc *sc = lp->lp_softc;
2083 return (lagg_lb_porttable(sc, NULL));
2087 lagg_lb_port_destroy(struct lagg_port *lp)
2089 struct lagg_softc *sc = lp->lp_softc;
2090 lagg_lb_porttable(sc, lp);
2094 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2096 struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2097 struct lagg_port *lp = NULL;
2100 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2101 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2102 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2104 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2106 lp = lb->lb_ports[p];
2109 * Check the port's link state. This will return the next active
2110 * port if the link is down or the port is NULL.
2112 if ((lp = lagg_link_active(sc, lp)) == NULL) {
2118 return (lagg_enqueue(lp->lp_ifp, m));
2121 static struct mbuf *
2122 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2124 struct ifnet *ifp = sc->sc_ifp;
2126 /* Just pass in the packet to our lagg device */
2127 m->m_pkthdr.rcvif = ifp;
2136 lagg_lacp_attach(struct lagg_softc *sc)
2138 struct lagg_port *lp;
2141 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2142 lacp_port_create(lp);
2146 lagg_lacp_detach(struct lagg_softc *sc)
2148 struct lagg_port *lp;
2151 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2152 lacp_port_destroy(lp);
2162 lagg_lacp_lladdr(struct lagg_softc *sc)
2164 struct lagg_port *lp;
2166 /* purge all the lacp ports */
2167 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2168 lacp_port_destroy(lp);
2170 /* add them back in */
2171 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2172 lacp_port_create(lp);
2176 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2178 struct lagg_port *lp;
2180 lp = lacp_select_tx_port(sc, m);
2187 return (lagg_enqueue(lp->lp_ifp, m));
2190 static struct mbuf *
2191 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2193 struct ifnet *ifp = sc->sc_ifp;
2194 struct ether_header *eh;
2197 eh = mtod(m, struct ether_header *);
2198 etype = ntohs(eh->ether_type);
2200 /* Tap off LACP control messages */
2201 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2202 m = lacp_input(lp, m);
2208 * If the port is not collecting or not in the active aggregator then
2211 if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2216 m->m_pkthdr.rcvif = ifp;