if_lagg.c revision 251490
1168793Sthompsa/*	$OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $	*/
2168793Sthompsa
3168793Sthompsa/*
4168793Sthompsa * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5174721Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6168793Sthompsa *
7168793Sthompsa * Permission to use, copy, modify, and distribute this software for any
8168793Sthompsa * purpose with or without fee is hereby granted, provided that the above
9168793Sthompsa * copyright notice and this permission notice appear in all copies.
10168793Sthompsa *
11168793Sthompsa * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12168793Sthompsa * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13168793Sthompsa * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14168793Sthompsa * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15168793Sthompsa * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16168793Sthompsa * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17168793Sthompsa * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18168793Sthompsa */
19168793Sthompsa
20168793Sthompsa#include <sys/cdefs.h>
21168793Sthompsa__FBSDID("$FreeBSD: head/sys/net/if_lagg.c 251490 2013-06-07 10:27:50Z trociny $");
22168793Sthompsa
23168793Sthompsa#include "opt_inet.h"
24168793Sthompsa#include "opt_inet6.h"
25168793Sthompsa
26168793Sthompsa#include <sys/param.h>
27168793Sthompsa#include <sys/kernel.h>
28168793Sthompsa#include <sys/malloc.h>
29168793Sthompsa#include <sys/mbuf.h>
30168793Sthompsa#include <sys/queue.h>
31168793Sthompsa#include <sys/socket.h>
32168793Sthompsa#include <sys/sockio.h>
33168793Sthompsa#include <sys/sysctl.h>
34168793Sthompsa#include <sys/module.h>
35168793Sthompsa#include <sys/priv.h>
36168793Sthompsa#include <sys/systm.h>
37168793Sthompsa#include <sys/proc.h>
38168793Sthompsa#include <sys/hash.h>
39169569Sthompsa#include <sys/lock.h>
40169569Sthompsa#include <sys/rwlock.h>
41169329Sthompsa#include <sys/taskqueue.h>
42203548Seri#include <sys/eventhandler.h>
43168793Sthompsa
44168793Sthompsa#include <net/ethernet.h>
45168793Sthompsa#include <net/if.h>
46168793Sthompsa#include <net/if_clone.h>
47168793Sthompsa#include <net/if_arp.h>
48168793Sthompsa#include <net/if_dl.h>
49168793Sthompsa#include <net/if_llc.h>
50168793Sthompsa#include <net/if_media.h>
51168793Sthompsa#include <net/if_types.h>
52168793Sthompsa#include <net/if_var.h>
53168793Sthompsa#include <net/bpf.h>
54168793Sthompsa
55221130Sbz#if defined(INET) || defined(INET6)
56221130Sbz#include <netinet/in.h>
57221130Sbz#endif
58168793Sthompsa#ifdef INET
59168793Sthompsa#include <netinet/in_systm.h>
60168793Sthompsa#include <netinet/if_ether.h>
61168793Sthompsa#include <netinet/ip.h>
62168793Sthompsa#endif
63168793Sthompsa
64168793Sthompsa#ifdef INET6
65168793Sthompsa#include <netinet/ip6.h>
66168793Sthompsa#endif
67168793Sthompsa
68168793Sthompsa#include <net/if_vlan_var.h>
69168793Sthompsa#include <net/if_lagg.h>
70168793Sthompsa#include <net/ieee8023ad_lacp.h>
71168793Sthompsa
72168793Sthompsa/* Special flags we should propagate to the lagg ports. */
73168793Sthompsastatic struct {
74168793Sthompsa	int flag;
75168793Sthompsa	int (*func)(struct ifnet *, int);
76168793Sthompsa} lagg_pflags[] = {
77168793Sthompsa	{IFF_PROMISC, ifpromisc},
78168793Sthompsa	{IFF_ALLMULTI, if_allmulti},
79168793Sthompsa	{0, NULL}
80168793Sthompsa};
81168793Sthompsa
82168793SthompsaSLIST_HEAD(__trhead, lagg_softc) lagg_list;	/* list of laggs */
83170599Sthompsastatic struct mtx	lagg_list_mtx;
84168793Sthompsaeventhandler_tag	lagg_detach_cookie = NULL;
85168793Sthompsa
86168793Sthompsastatic int	lagg_clone_create(struct if_clone *, int, caddr_t);
87168793Sthompsastatic void	lagg_clone_destroy(struct ifnet *);
88241610Sglebiusstatic struct if_clone *lagg_cloner;
89241610Sglebiusstatic const char laggname[] = "lagg";
90241610Sglebius
91168793Sthompsastatic void	lagg_lladdr(struct lagg_softc *, uint8_t *);
92171661Sthompsastatic void	lagg_capabilities(struct lagg_softc *);
93168793Sthompsastatic void	lagg_port_lladdr(struct lagg_port *, uint8_t *);
94169329Sthompsastatic void	lagg_port_setlladdr(void *, int);
95168793Sthompsastatic int	lagg_port_create(struct lagg_softc *, struct ifnet *);
96168793Sthompsastatic int	lagg_port_destroy(struct lagg_port *, int);
97168793Sthompsastatic struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
98173895Sthompsastatic void	lagg_linkstate(struct lagg_softc *);
99168793Sthompsastatic void	lagg_port_state(struct ifnet *, int);
100168793Sthompsastatic int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
101168793Sthompsastatic int	lagg_port_output(struct ifnet *, struct mbuf *,
102249925Sglebius		    const struct sockaddr *, struct route *);
103168793Sthompsastatic void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
104201803Strasz#ifdef LAGG_PORT_STACKING
105168793Sthompsastatic int	lagg_port_checkstacking(struct lagg_softc *);
106201803Strasz#endif
107168793Sthompsastatic void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
108168793Sthompsastatic void	lagg_init(void *);
109168793Sthompsastatic void	lagg_stop(struct lagg_softc *);
110168793Sthompsastatic int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
111168793Sthompsastatic int	lagg_ether_setmulti(struct lagg_softc *);
112168793Sthompsastatic int	lagg_ether_cmdmulti(struct lagg_port *, int);
113168793Sthompsastatic	int	lagg_setflag(struct lagg_port *, int, int,
114168793Sthompsa		    int (*func)(struct ifnet *, int));
115168793Sthompsastatic	int	lagg_setflags(struct lagg_port *, int status);
116240742Sglebiusstatic int	lagg_transmit(struct ifnet *, struct mbuf *);
117240742Sglebiusstatic void	lagg_qflush(struct ifnet *);
118168793Sthompsastatic int	lagg_media_change(struct ifnet *);
119168793Sthompsastatic void	lagg_media_status(struct ifnet *, struct ifmediareq *);
120168793Sthompsastatic struct lagg_port *lagg_link_active(struct lagg_softc *,
121168793Sthompsa	    struct lagg_port *);
122168793Sthompsastatic const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
123168793Sthompsa
124168793Sthompsa/* Simple round robin */
125168793Sthompsastatic int	lagg_rr_attach(struct lagg_softc *);
126168793Sthompsastatic int	lagg_rr_detach(struct lagg_softc *);
127168793Sthompsastatic int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
128168793Sthompsastatic struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
129168793Sthompsa		    struct mbuf *);
130168793Sthompsa
131168793Sthompsa/* Active failover */
132168793Sthompsastatic int	lagg_fail_attach(struct lagg_softc *);
133168793Sthompsastatic int	lagg_fail_detach(struct lagg_softc *);
134168793Sthompsastatic int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
135168793Sthompsastatic struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
136168793Sthompsa		    struct mbuf *);
137168793Sthompsa
138168793Sthompsa/* Loadbalancing */
139168793Sthompsastatic int	lagg_lb_attach(struct lagg_softc *);
140168793Sthompsastatic int	lagg_lb_detach(struct lagg_softc *);
141168793Sthompsastatic int	lagg_lb_port_create(struct lagg_port *);
142168793Sthompsastatic void	lagg_lb_port_destroy(struct lagg_port *);
143168793Sthompsastatic int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
144168793Sthompsastatic struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
145168793Sthompsa		    struct mbuf *);
146168793Sthompsastatic int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
147168793Sthompsa
148168793Sthompsa/* 802.3ad LACP */
149168793Sthompsastatic int	lagg_lacp_attach(struct lagg_softc *);
150168793Sthompsastatic int	lagg_lacp_detach(struct lagg_softc *);
151168793Sthompsastatic int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
152168793Sthompsastatic struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
153168793Sthompsa		    struct mbuf *);
154168793Sthompsastatic void	lagg_lacp_lladdr(struct lagg_softc *);
155168793Sthompsa
156249506Sglebiusstatic void	lagg_callout(void *);
157249506Sglebius
158168793Sthompsa/* lagg protocol table */
159168793Sthompsastatic const struct {
160168793Sthompsa	int			ti_proto;
161168793Sthompsa	int			(*ti_attach)(struct lagg_softc *);
162168793Sthompsa} lagg_protos[] = {
163168793Sthompsa	{ LAGG_PROTO_ROUNDROBIN,	lagg_rr_attach },
164168793Sthompsa	{ LAGG_PROTO_FAILOVER,		lagg_fail_attach },
165168793Sthompsa	{ LAGG_PROTO_LOADBALANCE,	lagg_lb_attach },
166168793Sthompsa	{ LAGG_PROTO_ETHERCHANNEL,	lagg_lb_attach },
167168793Sthompsa	{ LAGG_PROTO_LACP,		lagg_lacp_attach },
168168793Sthompsa	{ LAGG_PROTO_NONE,		NULL }
169168793Sthompsa};
170168793Sthompsa
171212100SemasteSYSCTL_DECL(_net_link);
172227309Sedstatic SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
173227309Sed    "Link Aggregation");
174212100Semaste
175212100Semastestatic int lagg_failover_rx_all = 0; /* Allow input on any failover links */
176212100SemasteSYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
177212100Semaste    &lagg_failover_rx_all, 0,
178212100Semaste    "Accept input from any interface in a failover lagg");
179232080Sthompsastatic int def_use_flowid = 1; /* Default value for using M_FLOWID */
180232080SthompsaTUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
181232080SthompsaSYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
182232080Sthompsa    &def_use_flowid, 0,
183232080Sthompsa    "Default setting for using flow id for load sharing");
184212100Semaste
185168793Sthompsastatic int
186168793Sthompsalagg_modevent(module_t mod, int type, void *data)
187168793Sthompsa{
188168793Sthompsa
189168793Sthompsa	switch (type) {
190168793Sthompsa	case MOD_LOAD:
191168793Sthompsa		mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
192168793Sthompsa		SLIST_INIT(&lagg_list);
193241610Sglebius		lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
194241610Sglebius		    lagg_clone_destroy, 0);
195168793Sthompsa		lagg_input_p = lagg_input;
196168793Sthompsa		lagg_linkstate_p = lagg_port_state;
197168793Sthompsa		lagg_detach_cookie = EVENTHANDLER_REGISTER(
198168793Sthompsa		    ifnet_departure_event, lagg_port_ifdetach, NULL,
199168793Sthompsa		    EVENTHANDLER_PRI_ANY);
200168793Sthompsa		break;
201168793Sthompsa	case MOD_UNLOAD:
202168793Sthompsa		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
203168793Sthompsa		    lagg_detach_cookie);
204241610Sglebius		if_clone_detach(lagg_cloner);
205168793Sthompsa		lagg_input_p = NULL;
206168793Sthompsa		lagg_linkstate_p = NULL;
207168793Sthompsa		mtx_destroy(&lagg_list_mtx);
208168793Sthompsa		break;
209168793Sthompsa	default:
210168793Sthompsa		return (EOPNOTSUPP);
211168793Sthompsa	}
212168793Sthompsa	return (0);
213168793Sthompsa}
214168793Sthompsa
215168793Sthompsastatic moduledata_t lagg_mod = {
216168793Sthompsa	"if_lagg",
217168793Sthompsa	lagg_modevent,
218241394Skevlo	0
219168793Sthompsa};
220168793Sthompsa
221168793SthompsaDECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
222224571SpluknetMODULE_VERSION(if_lagg, 1);
223168793Sthompsa
224203548Seri/*
225203548Seri * This routine is run via an vlan
226203548Seri * config EVENT
227203548Seri */
228203548Seristatic void
229203548Serilagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
230203548Seri{
231203548Seri        struct lagg_softc       *sc = ifp->if_softc;
232203548Seri        struct lagg_port        *lp;
233203548Seri
234203548Seri        if (ifp->if_softc !=  arg)   /* Not our event */
235203548Seri                return;
236203548Seri
237203548Seri        LAGG_RLOCK(sc);
238203548Seri        if (!SLIST_EMPTY(&sc->sc_ports)) {
239203548Seri                SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
240203548Seri                        EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
241203548Seri        }
242203548Seri        LAGG_RUNLOCK(sc);
243203548Seri}
244203548Seri
245203548Seri/*
246203548Seri * This routine is run via an vlan
247203548Seri * unconfig EVENT
248203548Seri */
249203548Seristatic void
250203548Serilagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
251203548Seri{
252203548Seri        struct lagg_softc       *sc = ifp->if_softc;
253203548Seri        struct lagg_port        *lp;
254203548Seri
255203548Seri        if (ifp->if_softc !=  arg)   /* Not our event */
256203548Seri                return;
257203548Seri
258203548Seri        LAGG_RLOCK(sc);
259203548Seri        if (!SLIST_EMPTY(&sc->sc_ports)) {
260203548Seri                SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
261203548Seri                        EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
262203548Seri        }
263203548Seri        LAGG_RUNLOCK(sc);
264203548Seri}
265203548Seri
266168793Sthompsastatic int
267168793Sthompsalagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
268168793Sthompsa{
269168793Sthompsa	struct lagg_softc *sc;
270168793Sthompsa	struct ifnet *ifp;
271168793Sthompsa	int i, error = 0;
272168793Sthompsa	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
273232008Sthompsa	struct sysctl_oid *oid;
274232008Sthompsa	char num[14];			/* sufficient for 32 bits */
275168793Sthompsa
276168793Sthompsa	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
277168793Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
278168793Sthompsa	if (ifp == NULL) {
279168793Sthompsa		free(sc, M_DEVBUF);
280168793Sthompsa		return (ENOSPC);
281168793Sthompsa	}
282168793Sthompsa
283249506Sglebius	sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
284249506Sglebius	sc->sc_opackets = counter_u64_alloc(M_WAITOK);
285249506Sglebius	sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
286249506Sglebius	sc->sc_obytes = counter_u64_alloc(M_WAITOK);
287249506Sglebius
288232008Sthompsa	sysctl_ctx_init(&sc->ctx);
289232008Sthompsa	snprintf(num, sizeof(num), "%u", unit);
290232080Sthompsa	sc->use_flowid = def_use_flowid;
291232008Sthompsa	oid = SYSCTL_ADD_NODE(&sc->ctx, &SYSCTL_NODE_CHILDREN(_net_link, lagg),
292232008Sthompsa		OID_AUTO, num, CTLFLAG_RD, NULL, "");
293232008Sthompsa	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
294232008Sthompsa		"use_flowid", CTLTYPE_INT|CTLFLAG_RW, &sc->use_flowid, sc->use_flowid,
295232008Sthompsa		"Use flow id for load sharing");
296241619Semax	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
297241627Sdelphij		"count", CTLTYPE_INT|CTLFLAG_RD, &sc->sc_count, sc->sc_count,
298241619Semax		"Total number of ports");
299232629Sthompsa	/* Hash all layers by default */
300232629Sthompsa	sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
301232008Sthompsa
302168793Sthompsa	sc->sc_proto = LAGG_PROTO_NONE;
303168793Sthompsa	for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
304168793Sthompsa		if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
305168793Sthompsa			sc->sc_proto = lagg_protos[i].ti_proto;
306168793Sthompsa			if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
307227459Sbrooks				if_free(ifp);
308168793Sthompsa				free(sc, M_DEVBUF);
309168793Sthompsa				return (error);
310168793Sthompsa			}
311168793Sthompsa			break;
312168793Sthompsa		}
313168793Sthompsa	}
314168793Sthompsa	LAGG_LOCK_INIT(sc);
315168793Sthompsa	SLIST_INIT(&sc->sc_ports);
316169329Sthompsa	TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
317249506Sglebius	callout_init_rw(&sc->sc_callout, &sc->sc_mtx, CALLOUT_SHAREDLOCK);
318168793Sthompsa
319168793Sthompsa	/* Initialise pseudo media types */
320168793Sthompsa	ifmedia_init(&sc->sc_media, 0, lagg_media_change,
321168793Sthompsa	    lagg_media_status);
322168793Sthompsa	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
323168793Sthompsa	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
324168793Sthompsa
325241610Sglebius	if_initname(ifp, laggname, unit);
326168793Sthompsa	ifp->if_softc = sc;
327240742Sglebius	ifp->if_transmit = lagg_transmit;
328240742Sglebius	ifp->if_qflush = lagg_qflush;
329168793Sthompsa	ifp->if_init = lagg_init;
330168793Sthompsa	ifp->if_ioctl = lagg_ioctl;
331168793Sthompsa	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
332168793Sthompsa
333168793Sthompsa	/*
334227459Sbrooks	 * Attach as an ordinary ethernet device, children will be attached
335168793Sthompsa	 * as special device IFT_IEEE8023ADLAG.
336168793Sthompsa	 */
337168793Sthompsa	ether_ifattach(ifp, eaddr);
338168793Sthompsa
339203548Seri	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
340203548Seri		lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
341203548Seri	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
342203548Seri		lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
343203548Seri
344168793Sthompsa	/* Insert into the global list of laggs */
345168793Sthompsa	mtx_lock(&lagg_list_mtx);
346168793Sthompsa	SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
347168793Sthompsa	mtx_unlock(&lagg_list_mtx);
348168793Sthompsa
349249506Sglebius	callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
350249506Sglebius
351168793Sthompsa	return (0);
352168793Sthompsa}
353168793Sthompsa
354168793Sthompsastatic void
355168793Sthompsalagg_clone_destroy(struct ifnet *ifp)
356168793Sthompsa{
357168793Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
358168793Sthompsa	struct lagg_port *lp;
359168793Sthompsa
360169569Sthompsa	LAGG_WLOCK(sc);
361168793Sthompsa
362168793Sthompsa	lagg_stop(sc);
363168793Sthompsa	ifp->if_flags &= ~IFF_UP;
364168793Sthompsa
365203548Seri	EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
366203548Seri	EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
367203548Seri
368168793Sthompsa	/* Shutdown and remove lagg ports */
369168793Sthompsa	while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
370168793Sthompsa		lagg_port_destroy(lp, 1);
371168793Sthompsa	/* Unhook the aggregation protocol */
372219275Seri	if (sc->sc_detach != NULL)
373219275Seri		(*sc->sc_detach)(sc);
374168793Sthompsa
375169569Sthompsa	LAGG_WUNLOCK(sc);
376168793Sthompsa
377232008Sthompsa	sysctl_ctx_free(&sc->ctx);
378168793Sthompsa	ifmedia_removeall(&sc->sc_media);
379168793Sthompsa	ether_ifdetach(ifp);
380227459Sbrooks	if_free(ifp);
381168793Sthompsa
382249506Sglebius	callout_drain(&sc->sc_callout);
383249506Sglebius	counter_u64_free(sc->sc_ipackets);
384249506Sglebius	counter_u64_free(sc->sc_opackets);
385249506Sglebius	counter_u64_free(sc->sc_ibytes);
386249506Sglebius	counter_u64_free(sc->sc_obytes);
387249506Sglebius
388168793Sthompsa	mtx_lock(&lagg_list_mtx);
389168793Sthompsa	SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
390168793Sthompsa	mtx_unlock(&lagg_list_mtx);
391168793Sthompsa
392169329Sthompsa	taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
393168793Sthompsa	LAGG_LOCK_DESTROY(sc);
394168793Sthompsa	free(sc, M_DEVBUF);
395168793Sthompsa}
396168793Sthompsa
397168793Sthompsastatic void
398168793Sthompsalagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
399168793Sthompsa{
400168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
401168793Sthompsa
402168793Sthompsa	if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
403168793Sthompsa		return;
404168793Sthompsa
405168793Sthompsa	bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
406168793Sthompsa	/* Let the protocol know the MAC has changed */
407168793Sthompsa	if (sc->sc_lladdr != NULL)
408168793Sthompsa		(*sc->sc_lladdr)(sc);
409202588Sthompsa	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
410168793Sthompsa}
411168793Sthompsa
412171661Sthompsastatic void
413168793Sthompsalagg_capabilities(struct lagg_softc *sc)
414168793Sthompsa{
415168793Sthompsa	struct lagg_port *lp;
416171661Sthompsa	int cap = ~0, ena = ~0;
417186195Sthompsa	u_long hwa = ~0UL;
418168793Sthompsa
419169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
420168793Sthompsa
421168793Sthompsa	/* Get capabilities from the lagg ports */
422171661Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
423171661Sthompsa		cap &= lp->lp_ifp->if_capabilities;
424171661Sthompsa		ena &= lp->lp_ifp->if_capenable;
425186195Sthompsa		hwa &= lp->lp_ifp->if_hwassist;
426171661Sthompsa	}
427171661Sthompsa	cap = (cap == ~0 ? 0 : cap);
428171661Sthompsa	ena = (ena == ~0 ? 0 : ena);
429186195Sthompsa	hwa = (hwa == ~0 ? 0 : hwa);
430168793Sthompsa
431171661Sthompsa	if (sc->sc_ifp->if_capabilities != cap ||
432186195Sthompsa	    sc->sc_ifp->if_capenable != ena ||
433186195Sthompsa	    sc->sc_ifp->if_hwassist != hwa) {
434171661Sthompsa		sc->sc_ifp->if_capabilities = cap;
435171661Sthompsa		sc->sc_ifp->if_capenable = ena;
436186195Sthompsa		sc->sc_ifp->if_hwassist = hwa;
437171661Sthompsa		getmicrotime(&sc->sc_ifp->if_lastchange);
438171661Sthompsa
439171661Sthompsa		if (sc->sc_ifflags & IFF_DEBUG)
440171661Sthompsa			if_printf(sc->sc_ifp,
441171661Sthompsa			    "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
442168793Sthompsa	}
443168793Sthompsa}
444168793Sthompsa
445168793Sthompsastatic void
446168793Sthompsalagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
447168793Sthompsa{
448170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
449168793Sthompsa	struct ifnet *ifp = lp->lp_ifp;
450169329Sthompsa	struct lagg_llq *llq;
451169329Sthompsa	int pending = 0;
452168793Sthompsa
453169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
454169329Sthompsa
455169328Sthompsa	if (lp->lp_detaching ||
456169328Sthompsa	    memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
457168793Sthompsa		return;
458168793Sthompsa
459169329Sthompsa	/* Check to make sure its not already queued to be changed */
460169329Sthompsa	SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
461169329Sthompsa		if (llq->llq_ifp == ifp) {
462169329Sthompsa			pending = 1;
463169329Sthompsa			break;
464169329Sthompsa		}
465169329Sthompsa	}
466168793Sthompsa
467169329Sthompsa	if (!pending) {
468169329Sthompsa		llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
469169329Sthompsa		if (llq == NULL)	/* XXX what to do */
470169329Sthompsa			return;
471169329Sthompsa	}
472169329Sthompsa
473169329Sthompsa	/* Update the lladdr even if pending, it may have changed */
474169329Sthompsa	llq->llq_ifp = ifp;
475169329Sthompsa	bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
476169329Sthompsa
477169329Sthompsa	if (!pending)
478169329Sthompsa		SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
479169329Sthompsa
480169329Sthompsa	taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
481168793Sthompsa}
482168793Sthompsa
483169329Sthompsa/*
484169329Sthompsa * Set the interface MAC address from a taskqueue to avoid a LOR.
485169329Sthompsa */
486169329Sthompsastatic void
487169329Sthompsalagg_port_setlladdr(void *arg, int pending)
488169329Sthompsa{
489169329Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)arg;
490169329Sthompsa	struct lagg_llq *llq, *head;
491169329Sthompsa	struct ifnet *ifp;
492169329Sthompsa	int error;
493169329Sthompsa
494169329Sthompsa	/* Grab a local reference of the queue and remove it from the softc */
495169569Sthompsa	LAGG_WLOCK(sc);
496169329Sthompsa	head = SLIST_FIRST(&sc->sc_llq_head);
497169329Sthompsa	SLIST_FIRST(&sc->sc_llq_head) = NULL;
498169569Sthompsa	LAGG_WUNLOCK(sc);
499169329Sthompsa
500169329Sthompsa	/*
501169329Sthompsa	 * Traverse the queue and set the lladdr on each ifp. It is safe to do
502169329Sthompsa	 * unlocked as we have the only reference to it.
503169329Sthompsa	 */
504169329Sthompsa	for (llq = head; llq != NULL; llq = head) {
505169329Sthompsa		ifp = llq->llq_ifp;
506169329Sthompsa
507169329Sthompsa		/* Set the link layer address */
508251490Strociny		CURVNET_SET(ifp->if_vnet);
509169329Sthompsa		error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
510251490Strociny		CURVNET_RESTORE();
511169329Sthompsa		if (error)
512169329Sthompsa			printf("%s: setlladdr failed on %s\n", __func__,
513169329Sthompsa			    ifp->if_xname);
514169329Sthompsa
515169329Sthompsa		head = SLIST_NEXT(llq, llq_entries);
516169329Sthompsa		free(llq, M_DEVBUF);
517169329Sthompsa	}
518169329Sthompsa}
519169329Sthompsa
520168793Sthompsastatic int
521168793Sthompsalagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
522168793Sthompsa{
523168793Sthompsa	struct lagg_softc *sc_ptr;
524168793Sthompsa	struct lagg_port *lp;
525168793Sthompsa	int error = 0;
526168793Sthompsa
527169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
528168793Sthompsa
529168793Sthompsa	/* Limit the maximal number of lagg ports */
530168793Sthompsa	if (sc->sc_count >= LAGG_MAX_PORTS)
531168793Sthompsa		return (ENOSPC);
532168793Sthompsa
533168793Sthompsa	/* Check if port has already been associated to a lagg */
534236178Srea	if (ifp->if_lagg != NULL) {
535236178Srea		/* Port is already in the current lagg? */
536236178Srea		lp = (struct lagg_port *)ifp->if_lagg;
537236178Srea		if (lp->lp_softc == sc)
538236178Srea			return (EEXIST);
539168793Sthompsa		return (EBUSY);
540236178Srea	}
541168793Sthompsa
542168793Sthompsa	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
543168793Sthompsa	if (ifp->if_type != IFT_ETHER)
544168793Sthompsa		return (EPROTONOSUPPORT);
545168793Sthompsa
546171661Sthompsa	/* Allow the first Ethernet member to define the MTU */
547171661Sthompsa	if (SLIST_EMPTY(&sc->sc_ports))
548171661Sthompsa		sc->sc_ifp->if_mtu = ifp->if_mtu;
549171661Sthompsa	else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
550171661Sthompsa		if_printf(sc->sc_ifp, "invalid MTU for %s\n",
551171661Sthompsa		    ifp->if_xname);
552171661Sthompsa		return (EINVAL);
553171661Sthompsa	}
554171661Sthompsa
555168793Sthompsa	if ((lp = malloc(sizeof(struct lagg_port),
556168793Sthompsa	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
557168793Sthompsa		return (ENOMEM);
558168793Sthompsa
559168793Sthompsa	/* Check if port is a stacked lagg */
560168793Sthompsa	mtx_lock(&lagg_list_mtx);
561168793Sthompsa	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
562168793Sthompsa		if (ifp == sc_ptr->sc_ifp) {
563168793Sthompsa			mtx_unlock(&lagg_list_mtx);
564168793Sthompsa			free(lp, M_DEVBUF);
565168793Sthompsa			return (EINVAL);
566201803Strasz			/* XXX disable stacking for the moment, its untested */
567201803Strasz#ifdef LAGG_PORT_STACKING
568168793Sthompsa			lp->lp_flags |= LAGG_PORT_STACK;
569168793Sthompsa			if (lagg_port_checkstacking(sc_ptr) >=
570168793Sthompsa			    LAGG_MAX_STACKING) {
571168793Sthompsa				mtx_unlock(&lagg_list_mtx);
572168793Sthompsa				free(lp, M_DEVBUF);
573168793Sthompsa				return (E2BIG);
574168793Sthompsa			}
575201803Strasz#endif
576168793Sthompsa		}
577168793Sthompsa	}
578168793Sthompsa	mtx_unlock(&lagg_list_mtx);
579168793Sthompsa
580168793Sthompsa	/* Change the interface type */
581168793Sthompsa	lp->lp_iftype = ifp->if_type;
582168793Sthompsa	ifp->if_type = IFT_IEEE8023ADLAG;
583168793Sthompsa	ifp->if_lagg = lp;
584168793Sthompsa	lp->lp_ioctl = ifp->if_ioctl;
585168793Sthompsa	ifp->if_ioctl = lagg_port_ioctl;
586168793Sthompsa	lp->lp_output = ifp->if_output;
587168793Sthompsa	ifp->if_output = lagg_port_output;
588168793Sthompsa
589168793Sthompsa	lp->lp_ifp = ifp;
590170599Sthompsa	lp->lp_softc = sc;
591168793Sthompsa
592168793Sthompsa	/* Save port link layer address */
593168793Sthompsa	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
594168793Sthompsa
595168793Sthompsa	if (SLIST_EMPTY(&sc->sc_ports)) {
596168793Sthompsa		sc->sc_primary = lp;
597168793Sthompsa		lagg_lladdr(sc, IF_LLADDR(ifp));
598168793Sthompsa	} else {
599168793Sthompsa		/* Update link layer address for this port */
600168793Sthompsa		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
601168793Sthompsa	}
602168793Sthompsa
603168793Sthompsa	/* Insert into the list of ports */
604168793Sthompsa	SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
605168793Sthompsa	sc->sc_count++;
606168793Sthompsa
607168793Sthompsa	/* Update lagg capabilities */
608171661Sthompsa	lagg_capabilities(sc);
609173895Sthompsa	lagg_linkstate(sc);
610168793Sthompsa
611168793Sthompsa	/* Add multicast addresses and interface flags to this port */
612168793Sthompsa	lagg_ether_cmdmulti(lp, 1);
613168793Sthompsa	lagg_setflags(lp, 1);
614168793Sthompsa
615168793Sthompsa	if (sc->sc_port_create != NULL)
616168793Sthompsa		error = (*sc->sc_port_create)(lp);
617168793Sthompsa	if (error) {
618170599Sthompsa		/* remove the port again, without calling sc_port_destroy */
619168793Sthompsa		lagg_port_destroy(lp, 0);
620168793Sthompsa		return (error);
621168793Sthompsa	}
622168793Sthompsa
623168793Sthompsa	return (error);
624168793Sthompsa}
625168793Sthompsa
626201803Strasz#ifdef LAGG_PORT_STACKING
627168793Sthompsastatic int
628168793Sthompsalagg_port_checkstacking(struct lagg_softc *sc)
629168793Sthompsa{
630168793Sthompsa	struct lagg_softc *sc_ptr;
631168793Sthompsa	struct lagg_port *lp;
632168793Sthompsa	int m = 0;
633168793Sthompsa
634169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
635168793Sthompsa
636168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
637168793Sthompsa		if (lp->lp_flags & LAGG_PORT_STACK) {
638168793Sthompsa			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
639168793Sthompsa			m = MAX(m, lagg_port_checkstacking(sc_ptr));
640168793Sthompsa		}
641168793Sthompsa	}
642168793Sthompsa
643168793Sthompsa	return (m + 1);
644168793Sthompsa}
645201803Strasz#endif
646168793Sthompsa
647168793Sthompsastatic int
648168793Sthompsalagg_port_destroy(struct lagg_port *lp, int runpd)
649168793Sthompsa{
650170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
651168793Sthompsa	struct lagg_port *lp_ptr;
652169329Sthompsa	struct lagg_llq *llq;
653168793Sthompsa	struct ifnet *ifp = lp->lp_ifp;
654168793Sthompsa
655169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
656168793Sthompsa
657168793Sthompsa	if (runpd && sc->sc_port_destroy != NULL)
658168793Sthompsa		(*sc->sc_port_destroy)(lp);
659168793Sthompsa
660169328Sthompsa	/*
661169328Sthompsa	 * Remove multicast addresses and interface flags from this port and
662169328Sthompsa	 * reset the MAC address, skip if the interface is being detached.
663169328Sthompsa	 */
664169328Sthompsa	if (!lp->lp_detaching) {
665169328Sthompsa		lagg_ether_cmdmulti(lp, 0);
666169328Sthompsa		lagg_setflags(lp, 0);
667169328Sthompsa		lagg_port_lladdr(lp, lp->lp_lladdr);
668169328Sthompsa	}
669168793Sthompsa
670168793Sthompsa	/* Restore interface */
671168793Sthompsa	ifp->if_type = lp->lp_iftype;
672168793Sthompsa	ifp->if_ioctl = lp->lp_ioctl;
673168793Sthompsa	ifp->if_output = lp->lp_output;
674168793Sthompsa	ifp->if_lagg = NULL;
675168793Sthompsa
676168793Sthompsa	/* Finally, remove the port from the lagg */
677168793Sthompsa	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
678168793Sthompsa	sc->sc_count--;
679168793Sthompsa
680168793Sthompsa	/* Update the primary interface */
681168793Sthompsa	if (lp == sc->sc_primary) {
682168793Sthompsa		uint8_t lladdr[ETHER_ADDR_LEN];
683168793Sthompsa
684168793Sthompsa		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
685168793Sthompsa			bzero(&lladdr, ETHER_ADDR_LEN);
686168793Sthompsa		} else {
687168793Sthompsa			bcopy(lp_ptr->lp_lladdr,
688168793Sthompsa			    lladdr, ETHER_ADDR_LEN);
689168793Sthompsa		}
690168793Sthompsa		lagg_lladdr(sc, lladdr);
691168793Sthompsa		sc->sc_primary = lp_ptr;
692168793Sthompsa
693168793Sthompsa		/* Update link layer address for each port */
694168793Sthompsa		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
695168793Sthompsa			lagg_port_lladdr(lp_ptr, lladdr);
696168793Sthompsa	}
697168793Sthompsa
698169329Sthompsa	/* Remove any pending lladdr changes from the queue */
699169329Sthompsa	if (lp->lp_detaching) {
700169329Sthompsa		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
701169329Sthompsa			if (llq->llq_ifp == ifp) {
702169329Sthompsa				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
703169329Sthompsa				    llq_entries);
704169329Sthompsa				free(llq, M_DEVBUF);
705169329Sthompsa				break;	/* Only appears once */
706169329Sthompsa			}
707169329Sthompsa		}
708169329Sthompsa	}
709169329Sthompsa
710168793Sthompsa	if (lp->lp_ifflags)
711168793Sthompsa		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
712168793Sthompsa
713168793Sthompsa	free(lp, M_DEVBUF);
714168793Sthompsa
715168793Sthompsa	/* Update lagg capabilities */
716171661Sthompsa	lagg_capabilities(sc);
717173895Sthompsa	lagg_linkstate(sc);
718168793Sthompsa
719168793Sthompsa	return (0);
720168793Sthompsa}
721168793Sthompsa
722168793Sthompsastatic int
723168793Sthompsalagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
724168793Sthompsa{
725168793Sthompsa	struct lagg_reqport *rp = (struct lagg_reqport *)data;
726168793Sthompsa	struct lagg_softc *sc;
727168793Sthompsa	struct lagg_port *lp = NULL;
728168793Sthompsa	int error = 0;
729168793Sthompsa
730168793Sthompsa	/* Should be checked by the caller */
731168793Sthompsa	if (ifp->if_type != IFT_IEEE8023ADLAG ||
732170599Sthompsa	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
733168793Sthompsa		goto fallback;
734168793Sthompsa
735168793Sthompsa	switch (cmd) {
736168793Sthompsa	case SIOCGLAGGPORT:
737168793Sthompsa		if (rp->rp_portname[0] == '\0' ||
738168793Sthompsa		    ifunit(rp->rp_portname) != ifp) {
739168793Sthompsa			error = EINVAL;
740168793Sthompsa			break;
741168793Sthompsa		}
742168793Sthompsa
743171603Sthompsa		LAGG_RLOCK(sc);
744171603Sthompsa		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
745168793Sthompsa			error = ENOENT;
746171603Sthompsa			LAGG_RUNLOCK(sc);
747168793Sthompsa			break;
748168793Sthompsa		}
749168793Sthompsa
750168793Sthompsa		lagg_port2req(lp, rp);
751169569Sthompsa		LAGG_RUNLOCK(sc);
752168793Sthompsa		break;
753171661Sthompsa
754171661Sthompsa	case SIOCSIFCAP:
755171661Sthompsa		if (lp->lp_ioctl == NULL) {
756171661Sthompsa			error = EINVAL;
757171661Sthompsa			break;
758171661Sthompsa		}
759171661Sthompsa		error = (*lp->lp_ioctl)(ifp, cmd, data);
760171661Sthompsa		if (error)
761171661Sthompsa			break;
762171661Sthompsa
763171661Sthompsa		/* Update lagg interface capabilities */
764171661Sthompsa		LAGG_WLOCK(sc);
765171661Sthompsa		lagg_capabilities(sc);
766171661Sthompsa		LAGG_WUNLOCK(sc);
767171661Sthompsa		break;
768171661Sthompsa
769171661Sthompsa	case SIOCSIFMTU:
770171661Sthompsa		/* Do not allow the MTU to be changed once joined */
771171661Sthompsa		error = EINVAL;
772171661Sthompsa		break;
773171661Sthompsa
774168793Sthompsa	default:
775168793Sthompsa		goto fallback;
776168793Sthompsa	}
777168793Sthompsa
778168793Sthompsa	return (error);
779168793Sthompsa
780168793Sthompsafallback:
781169340Sthompsa	if (lp->lp_ioctl != NULL)
782168793Sthompsa		return ((*lp->lp_ioctl)(ifp, cmd, data));
783168793Sthompsa
784168793Sthompsa	return (EINVAL);
785168793Sthompsa}
786168793Sthompsa
787234936Semaste/*
788234936Semaste * For direct output to child ports.
789234936Semaste */
790168793Sthompsastatic int
791168793Sthompsalagg_port_output(struct ifnet *ifp, struct mbuf *m,
792249925Sglebius	const struct sockaddr *dst, struct route *ro)
793168793Sthompsa{
794168793Sthompsa	struct lagg_port *lp = ifp->if_lagg;
795168793Sthompsa
796168793Sthompsa	switch (dst->sa_family) {
797168793Sthompsa		case pseudo_AF_HDRCMPLT:
798168793Sthompsa		case AF_UNSPEC:
799191148Skmacy			return ((*lp->lp_output)(ifp, m, dst, ro));
800168793Sthompsa	}
801168793Sthompsa
802168793Sthompsa	/* drop any other frames */
803168793Sthompsa	m_freem(m);
804245741Sglebius	return (ENETDOWN);
805168793Sthompsa}
806168793Sthompsa
807168793Sthompsastatic void
808168793Sthompsalagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
809168793Sthompsa{
810168793Sthompsa	struct lagg_port *lp;
811168793Sthompsa	struct lagg_softc *sc;
812168793Sthompsa
813168793Sthompsa	if ((lp = ifp->if_lagg) == NULL)
814168793Sthompsa		return;
815237852Sthompsa	/* If the ifnet is just being renamed, don't do anything. */
816237852Sthompsa	if (ifp->if_flags & IFF_RENAMING)
817237852Sthompsa		return;
818168793Sthompsa
819170599Sthompsa	sc = lp->lp_softc;
820168793Sthompsa
821169569Sthompsa	LAGG_WLOCK(sc);
822169328Sthompsa	lp->lp_detaching = 1;
823168793Sthompsa	lagg_port_destroy(lp, 1);
824169569Sthompsa	LAGG_WUNLOCK(sc);
825168793Sthompsa}
826168793Sthompsa
827168793Sthompsastatic void
828168793Sthompsalagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
829168793Sthompsa{
830170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
831172020Sthompsa
832168793Sthompsa	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
833168793Sthompsa	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
834168793Sthompsa	rp->rp_prio = lp->lp_prio;
835168793Sthompsa	rp->rp_flags = lp->lp_flags;
836171247Sthompsa	if (sc->sc_portreq != NULL)
837171247Sthompsa		(*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
838168793Sthompsa
839168793Sthompsa	/* Add protocol specific flags */
840168793Sthompsa	switch (sc->sc_proto) {
841168793Sthompsa		case LAGG_PROTO_FAILOVER:
842168793Sthompsa			if (lp == sc->sc_primary)
843169204Sthompsa				rp->rp_flags |= LAGG_PORT_MASTER;
844172020Sthompsa			if (lp == lagg_link_active(sc, sc->sc_primary))
845172020Sthompsa				rp->rp_flags |= LAGG_PORT_ACTIVE;
846172020Sthompsa			break;
847172020Sthompsa
848168793Sthompsa		case LAGG_PROTO_ROUNDROBIN:
849168793Sthompsa		case LAGG_PROTO_LOADBALANCE:
850168793Sthompsa		case LAGG_PROTO_ETHERCHANNEL:
851168793Sthompsa			if (LAGG_PORTACTIVE(lp))
852168793Sthompsa				rp->rp_flags |= LAGG_PORT_ACTIVE;
853168793Sthompsa			break;
854168793Sthompsa
855168793Sthompsa		case LAGG_PROTO_LACP:
856168793Sthompsa			/* LACP has a different definition of active */
857177274Sthompsa			if (lacp_isactive(lp))
858168793Sthompsa				rp->rp_flags |= LAGG_PORT_ACTIVE;
859177274Sthompsa			if (lacp_iscollecting(lp))
860177274Sthompsa				rp->rp_flags |= LAGG_PORT_COLLECTING;
861177274Sthompsa			if (lacp_isdistributing(lp))
862177274Sthompsa				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
863168793Sthompsa			break;
864168793Sthompsa	}
865168793Sthompsa
866168793Sthompsa}
867168793Sthompsa
868168793Sthompsastatic void
869168793Sthompsalagg_init(void *xsc)
870168793Sthompsa{
871168793Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)xsc;
872168793Sthompsa	struct lagg_port *lp;
873168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
874168793Sthompsa
875168793Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
876168793Sthompsa		return;
877168793Sthompsa
878169569Sthompsa	LAGG_WLOCK(sc);
879168793Sthompsa
880168793Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
881168793Sthompsa	/* Update the port lladdrs */
882168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
883168793Sthompsa		lagg_port_lladdr(lp, IF_LLADDR(ifp));
884168793Sthompsa
885168793Sthompsa	if (sc->sc_init != NULL)
886168793Sthompsa		(*sc->sc_init)(sc);
887168793Sthompsa
888169569Sthompsa	LAGG_WUNLOCK(sc);
889168793Sthompsa}
890168793Sthompsa
891168793Sthompsastatic void
892168793Sthompsalagg_stop(struct lagg_softc *sc)
893168793Sthompsa{
894168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
895168793Sthompsa
896169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
897168793Sthompsa
898168793Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
899168793Sthompsa		return;
900168793Sthompsa
901168793Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
902168793Sthompsa
903168793Sthompsa	if (sc->sc_stop != NULL)
904168793Sthompsa		(*sc->sc_stop)(sc);
905168793Sthompsa}
906168793Sthompsa
907168793Sthompsastatic int
908168793Sthompsalagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
909168793Sthompsa{
910168793Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
911168793Sthompsa	struct lagg_reqall *ra = (struct lagg_reqall *)data;
912168793Sthompsa	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
913232629Sthompsa	struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
914168793Sthompsa	struct ifreq *ifr = (struct ifreq *)data;
915168793Sthompsa	struct lagg_port *lp;
916168793Sthompsa	struct ifnet *tpif;
917168793Sthompsa	struct thread *td = curthread;
918171603Sthompsa	char *buf, *outbuf;
919171603Sthompsa	int count, buflen, len, error = 0;
920168793Sthompsa
921168793Sthompsa	bzero(&rpbuf, sizeof(rpbuf));
922168793Sthompsa
923168793Sthompsa	switch (cmd) {
924168793Sthompsa	case SIOCGLAGG:
925171603Sthompsa		LAGG_RLOCK(sc);
926171603Sthompsa		count = 0;
927171603Sthompsa		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
928171603Sthompsa			count++;
929171603Sthompsa		buflen = count * sizeof(struct lagg_reqport);
930171603Sthompsa		LAGG_RUNLOCK(sc);
931171603Sthompsa
932171603Sthompsa		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
933171603Sthompsa
934171603Sthompsa		LAGG_RLOCK(sc);
935168793Sthompsa		ra->ra_proto = sc->sc_proto;
936171247Sthompsa		if (sc->sc_req != NULL)
937171247Sthompsa			(*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
938171603Sthompsa
939171603Sthompsa		count = 0;
940171603Sthompsa		buf = outbuf;
941171603Sthompsa		len = min(ra->ra_size, buflen);
942171603Sthompsa		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
943171603Sthompsa			if (len < sizeof(rpbuf))
944171603Sthompsa				break;
945171603Sthompsa
946168793Sthompsa			lagg_port2req(lp, &rpbuf);
947171603Sthompsa			memcpy(buf, &rpbuf, sizeof(rpbuf));
948171603Sthompsa			count++;
949171603Sthompsa			buf += sizeof(rpbuf);
950171603Sthompsa			len -= sizeof(rpbuf);
951168793Sthompsa		}
952171603Sthompsa		LAGG_RUNLOCK(sc);
953171603Sthompsa		ra->ra_ports = count;
954171603Sthompsa		ra->ra_size = count * sizeof(rpbuf);
955171603Sthompsa		error = copyout(outbuf, ra->ra_port, ra->ra_size);
956171603Sthompsa		free(outbuf, M_TEMP);
957168793Sthompsa		break;
958168793Sthompsa	case SIOCSLAGG:
959168793Sthompsa		error = priv_check(td, PRIV_NET_LAGG);
960168793Sthompsa		if (error)
961168793Sthompsa			break;
962168793Sthompsa		if (ra->ra_proto >= LAGG_PROTO_MAX) {
963168793Sthompsa			error = EPROTONOSUPPORT;
964168793Sthompsa			break;
965168793Sthompsa		}
966234163Sthompsa		LAGG_WLOCK(sc);
967168793Sthompsa		if (sc->sc_proto != LAGG_PROTO_NONE) {
968234163Sthompsa			/* Reset protocol first in case detach unlocks */
969234163Sthompsa			sc->sc_proto = LAGG_PROTO_NONE;
970168793Sthompsa			error = sc->sc_detach(sc);
971168793Sthompsa			sc->sc_detach = NULL;
972168793Sthompsa			sc->sc_start = NULL;
973168793Sthompsa			sc->sc_input = NULL;
974168793Sthompsa			sc->sc_port_create = NULL;
975168793Sthompsa			sc->sc_port_destroy = NULL;
976168793Sthompsa			sc->sc_linkstate = NULL;
977168793Sthompsa			sc->sc_init = NULL;
978168793Sthompsa			sc->sc_stop = NULL;
979168793Sthompsa			sc->sc_lladdr = NULL;
980171247Sthompsa			sc->sc_req = NULL;
981171247Sthompsa			sc->sc_portreq = NULL;
982234163Sthompsa		} else if (sc->sc_input != NULL) {
983234163Sthompsa			/* Still detaching */
984234163Sthompsa			error = EBUSY;
985234163Sthompsa		}
986234163Sthompsa		if (error != 0) {
987171603Sthompsa			LAGG_WUNLOCK(sc);
988234163Sthompsa			break;
989168793Sthompsa		}
990171603Sthompsa		for (int i = 0; i < (sizeof(lagg_protos) /
991168793Sthompsa		    sizeof(lagg_protos[0])); i++) {
992168793Sthompsa			if (lagg_protos[i].ti_proto == ra->ra_proto) {
993168793Sthompsa				if (sc->sc_ifflags & IFF_DEBUG)
994168793Sthompsa					printf("%s: using proto %u\n",
995168793Sthompsa					    sc->sc_ifname,
996168793Sthompsa					    lagg_protos[i].ti_proto);
997168793Sthompsa				sc->sc_proto = lagg_protos[i].ti_proto;
998168793Sthompsa				if (sc->sc_proto != LAGG_PROTO_NONE)
999168793Sthompsa					error = lagg_protos[i].ti_attach(sc);
1000171603Sthompsa				LAGG_WUNLOCK(sc);
1001171603Sthompsa				return (error);
1002168793Sthompsa			}
1003168793Sthompsa		}
1004234163Sthompsa		LAGG_WUNLOCK(sc);
1005168793Sthompsa		error = EPROTONOSUPPORT;
1006168793Sthompsa		break;
1007232629Sthompsa	case SIOCGLAGGFLAGS:
1008232629Sthompsa		rf->rf_flags = sc->sc_flags;
1009232629Sthompsa		break;
1010232629Sthompsa	case SIOCSLAGGHASH:
1011232629Sthompsa		error = priv_check(td, PRIV_NET_LAGG);
1012232629Sthompsa		if (error)
1013232629Sthompsa			break;
1014232629Sthompsa		if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1015232629Sthompsa			error = EINVAL;
1016232629Sthompsa			break;
1017232629Sthompsa		}
1018232629Sthompsa		LAGG_WLOCK(sc);
1019232629Sthompsa		sc->sc_flags &= ~LAGG_F_HASHMASK;
1020232629Sthompsa		sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1021232629Sthompsa		LAGG_WUNLOCK(sc);
1022232629Sthompsa		break;
1023168793Sthompsa	case SIOCGLAGGPORT:
1024168793Sthompsa		if (rp->rp_portname[0] == '\0' ||
1025168793Sthompsa		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1026168793Sthompsa			error = EINVAL;
1027168793Sthompsa			break;
1028168793Sthompsa		}
1029168793Sthompsa
1030171603Sthompsa		LAGG_RLOCK(sc);
1031168793Sthompsa		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1032170599Sthompsa		    lp->lp_softc != sc) {
1033168793Sthompsa			error = ENOENT;
1034171603Sthompsa			LAGG_RUNLOCK(sc);
1035168793Sthompsa			break;
1036168793Sthompsa		}
1037168793Sthompsa
1038168793Sthompsa		lagg_port2req(lp, rp);
1039171603Sthompsa		LAGG_RUNLOCK(sc);
1040168793Sthompsa		break;
1041168793Sthompsa	case SIOCSLAGGPORT:
1042168793Sthompsa		error = priv_check(td, PRIV_NET_LAGG);
1043168793Sthompsa		if (error)
1044168793Sthompsa			break;
1045168793Sthompsa		if (rp->rp_portname[0] == '\0' ||
1046168793Sthompsa		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1047168793Sthompsa			error = EINVAL;
1048168793Sthompsa			break;
1049168793Sthompsa		}
1050171603Sthompsa		LAGG_WLOCK(sc);
1051168793Sthompsa		error = lagg_port_create(sc, tpif);
1052171603Sthompsa		LAGG_WUNLOCK(sc);
1053168793Sthompsa		break;
1054168793Sthompsa	case SIOCSLAGGDELPORT:
1055168793Sthompsa		error = priv_check(td, PRIV_NET_LAGG);
1056168793Sthompsa		if (error)
1057168793Sthompsa			break;
1058168793Sthompsa		if (rp->rp_portname[0] == '\0' ||
1059168793Sthompsa		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1060168793Sthompsa			error = EINVAL;
1061168793Sthompsa			break;
1062168793Sthompsa		}
1063168793Sthompsa
1064171603Sthompsa		LAGG_WLOCK(sc);
1065168793Sthompsa		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1066170599Sthompsa		    lp->lp_softc != sc) {
1067168793Sthompsa			error = ENOENT;
1068171603Sthompsa			LAGG_WUNLOCK(sc);
1069168793Sthompsa			break;
1070168793Sthompsa		}
1071168793Sthompsa
1072168793Sthompsa		error = lagg_port_destroy(lp, 1);
1073171603Sthompsa		LAGG_WUNLOCK(sc);
1074168793Sthompsa		break;
1075168793Sthompsa	case SIOCSIFFLAGS:
1076168793Sthompsa		/* Set flags on ports too */
1077171603Sthompsa		LAGG_WLOCK(sc);
1078168793Sthompsa		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1079168793Sthompsa			lagg_setflags(lp, 1);
1080168793Sthompsa		}
1081171603Sthompsa		LAGG_WUNLOCK(sc);
1082168793Sthompsa
1083168793Sthompsa		if (!(ifp->if_flags & IFF_UP) &&
1084168793Sthompsa		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1085168793Sthompsa			/*
1086168793Sthompsa			 * If interface is marked down and it is running,
1087168793Sthompsa			 * then stop and disable it.
1088168793Sthompsa			 */
1089171603Sthompsa			LAGG_WLOCK(sc);
1090168793Sthompsa			lagg_stop(sc);
1091171603Sthompsa			LAGG_WUNLOCK(sc);
1092168793Sthompsa		} else if ((ifp->if_flags & IFF_UP) &&
1093168793Sthompsa		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1094168793Sthompsa			/*
1095168793Sthompsa			 * If interface is marked up and it is stopped, then
1096168793Sthompsa			 * start it.
1097168793Sthompsa			 */
1098168793Sthompsa			(*ifp->if_init)(sc);
1099168793Sthompsa		}
1100168793Sthompsa		break;
1101168793Sthompsa	case SIOCADDMULTI:
1102168793Sthompsa	case SIOCDELMULTI:
1103171603Sthompsa		LAGG_WLOCK(sc);
1104168793Sthompsa		error = lagg_ether_setmulti(sc);
1105171603Sthompsa		LAGG_WUNLOCK(sc);
1106168793Sthompsa		break;
1107168793Sthompsa	case SIOCSIFMEDIA:
1108168793Sthompsa	case SIOCGIFMEDIA:
1109168793Sthompsa		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1110168793Sthompsa		break;
1111171661Sthompsa
1112171661Sthompsa	case SIOCSIFCAP:
1113171661Sthompsa	case SIOCSIFMTU:
1114171661Sthompsa		/* Do not allow the MTU or caps to be directly changed */
1115171661Sthompsa		error = EINVAL;
1116171661Sthompsa		break;
1117171661Sthompsa
1118168793Sthompsa	default:
1119168793Sthompsa		error = ether_ioctl(ifp, cmd, data);
1120168793Sthompsa		break;
1121168793Sthompsa	}
1122168793Sthompsa	return (error);
1123168793Sthompsa}
1124168793Sthompsa
1125168793Sthompsastatic int
1126168793Sthompsalagg_ether_setmulti(struct lagg_softc *sc)
1127168793Sthompsa{
1128169327Sthompsa	struct lagg_port *lp;
1129168793Sthompsa
1130169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
1131168793Sthompsa
1132169327Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1133169340Sthompsa		/* First, remove any existing filter entries. */
1134169340Sthompsa		lagg_ether_cmdmulti(lp, 0);
1135169340Sthompsa		/* copy all addresses from the lagg interface to the port */
1136169327Sthompsa		lagg_ether_cmdmulti(lp, 1);
1137169340Sthompsa	}
1138168793Sthompsa	return (0);
1139168793Sthompsa}
1140168793Sthompsa
1141168793Sthompsastatic int
1142168793Sthompsalagg_ether_cmdmulti(struct lagg_port *lp, int set)
1143168793Sthompsa{
1144170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
1145169327Sthompsa	struct ifnet *ifp = lp->lp_ifp;
1146170599Sthompsa	struct ifnet *scifp = sc->sc_ifp;
1147169327Sthompsa	struct lagg_mc *mc;
1148169327Sthompsa	struct ifmultiaddr *ifma, *rifma = NULL;
1149169327Sthompsa	struct sockaddr_dl sdl;
1150169327Sthompsa	int error;
1151168793Sthompsa
1152169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
1153168793Sthompsa
1154168793Sthompsa	bzero((char *)&sdl, sizeof(sdl));
1155168793Sthompsa	sdl.sdl_len = sizeof(sdl);
1156168793Sthompsa	sdl.sdl_family = AF_LINK;
1157168793Sthompsa	sdl.sdl_type = IFT_ETHER;
1158168793Sthompsa	sdl.sdl_alen = ETHER_ADDR_LEN;
1159168793Sthompsa	sdl.sdl_index = ifp->if_index;
1160168793Sthompsa
1161169327Sthompsa	if (set) {
1162170599Sthompsa		TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1163169327Sthompsa			if (ifma->ifma_addr->sa_family != AF_LINK)
1164169327Sthompsa				continue;
1165169327Sthompsa			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1166169327Sthompsa			    LLADDR(&sdl), ETHER_ADDR_LEN);
1167168793Sthompsa
1168168793Sthompsa			error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1169169327Sthompsa			if (error)
1170169327Sthompsa				return (error);
1171169327Sthompsa			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1172169327Sthompsa			if (mc == NULL)
1173169327Sthompsa				return (ENOMEM);
1174169327Sthompsa			mc->mc_ifma = rifma;
1175169327Sthompsa			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1176168793Sthompsa		}
1177169327Sthompsa	} else {
1178169327Sthompsa		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1179169327Sthompsa			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1180169327Sthompsa			if_delmulti_ifma(mc->mc_ifma);
1181169327Sthompsa			free(mc, M_DEVBUF);
1182169327Sthompsa		}
1183168793Sthompsa	}
1184168793Sthompsa	return (0);
1185168793Sthompsa}
1186168793Sthompsa
1187168793Sthompsa/* Handle a ref counted flag that should be set on the lagg port as well */
1188168793Sthompsastatic int
1189168793Sthompsalagg_setflag(struct lagg_port *lp, int flag, int status,
1190168793Sthompsa	     int (*func)(struct ifnet *, int))
1191168793Sthompsa{
1192170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
1193170599Sthompsa	struct ifnet *scifp = sc->sc_ifp;
1194168793Sthompsa	struct ifnet *ifp = lp->lp_ifp;
1195168793Sthompsa	int error;
1196168793Sthompsa
1197169569Sthompsa	LAGG_WLOCK_ASSERT(sc);
1198168793Sthompsa
1199170599Sthompsa	status = status ? (scifp->if_flags & flag) : 0;
1200168793Sthompsa	/* Now "status" contains the flag value or 0 */
1201168793Sthompsa
1202168793Sthompsa	/*
1203168793Sthompsa	 * See if recorded ports status is different from what
1204168793Sthompsa	 * we want it to be.  If it is, flip it.  We record ports
1205168793Sthompsa	 * status in lp_ifflags so that we won't clear ports flag
1206168793Sthompsa	 * we haven't set.  In fact, we don't clear or set ports
1207168793Sthompsa	 * flags directly, but get or release references to them.
1208168793Sthompsa	 * That's why we can be sure that recorded flags still are
1209168793Sthompsa	 * in accord with actual ports flags.
1210168793Sthompsa	 */
1211168793Sthompsa	if (status != (lp->lp_ifflags & flag)) {
1212168793Sthompsa		error = (*func)(ifp, status);
1213168793Sthompsa		if (error)
1214168793Sthompsa			return (error);
1215168793Sthompsa		lp->lp_ifflags &= ~flag;
1216168793Sthompsa		lp->lp_ifflags |= status;
1217168793Sthompsa	}
1218168793Sthompsa	return (0);
1219168793Sthompsa}
1220168793Sthompsa
1221168793Sthompsa/*
1222168793Sthompsa * Handle IFF_* flags that require certain changes on the lagg port
1223168793Sthompsa * if "status" is true, update ports flags respective to the lagg
1224168793Sthompsa * if "status" is false, forcedly clear the flags set on port.
1225168793Sthompsa */
1226168793Sthompsastatic int
1227168793Sthompsalagg_setflags(struct lagg_port *lp, int status)
1228168793Sthompsa{
1229168793Sthompsa	int error, i;
1230170599Sthompsa
1231168793Sthompsa	for (i = 0; lagg_pflags[i].flag; i++) {
1232168793Sthompsa		error = lagg_setflag(lp, lagg_pflags[i].flag,
1233168793Sthompsa		    status, lagg_pflags[i].func);
1234168793Sthompsa		if (error)
1235168793Sthompsa			return (error);
1236168793Sthompsa	}
1237168793Sthompsa	return (0);
1238168793Sthompsa}
1239168793Sthompsa
1240240742Sglebiusstatic int
1241240742Sglebiuslagg_transmit(struct ifnet *ifp, struct mbuf *m)
1242168793Sthompsa{
1243168793Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1244240742Sglebius	int error, len, mcast;
1245168793Sthompsa
1246240742Sglebius	len = m->m_pkthdr.len;
1247240742Sglebius	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1248240742Sglebius
1249169569Sthompsa	LAGG_RLOCK(sc);
1250183160Sthompsa	/* We need a Tx algorithm and at least one port */
1251183160Sthompsa	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1252183160Sthompsa		LAGG_RUNLOCK(sc);
1253240742Sglebius		m_freem(m);
1254240742Sglebius		ifp->if_oerrors++;
1255240742Sglebius		return (ENXIO);
1256183160Sthompsa	}
1257183160Sthompsa
1258240742Sglebius	ETHER_BPF_MTAP(ifp, m);
1259168793Sthompsa
1260240742Sglebius	error = (*sc->sc_start)(sc, m);
1261240742Sglebius	LAGG_RUNLOCK(sc);
1262168793Sthompsa
1263240742Sglebius	if (error == 0) {
1264249506Sglebius		counter_u64_add(sc->sc_opackets, 1);
1265249506Sglebius		counter_u64_add(sc->sc_obytes, len);
1266240742Sglebius		ifp->if_omcasts += mcast;
1267240742Sglebius	} else
1268240742Sglebius		ifp->if_oerrors++;
1269240742Sglebius
1270240742Sglebius	return (error);
1271168793Sthompsa}
1272168793Sthompsa
1273240742Sglebius/*
1274240742Sglebius * The ifp->if_qflush entry point for lagg(4) is no-op.
1275240742Sglebius */
1276240742Sglebiusstatic void
1277240742Sglebiuslagg_qflush(struct ifnet *ifp __unused)
1278240742Sglebius{
1279240742Sglebius}
1280240742Sglebius
1281168793Sthompsastatic struct mbuf *
1282168793Sthompsalagg_input(struct ifnet *ifp, struct mbuf *m)
1283168793Sthompsa{
1284168793Sthompsa	struct lagg_port *lp = ifp->if_lagg;
1285170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
1286170599Sthompsa	struct ifnet *scifp = sc->sc_ifp;
1287168793Sthompsa
1288223846Sthompsa	LAGG_RLOCK(sc);
1289170599Sthompsa	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1290169227Sthompsa	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1291168793Sthompsa	    sc->sc_proto == LAGG_PROTO_NONE) {
1292223846Sthompsa		LAGG_RUNLOCK(sc);
1293168793Sthompsa		m_freem(m);
1294168793Sthompsa		return (NULL);
1295168793Sthompsa	}
1296168793Sthompsa
1297172825Sthompsa	ETHER_BPF_MTAP(scifp, m);
1298168793Sthompsa
1299168793Sthompsa	m = (*sc->sc_input)(sc, lp, m);
1300168793Sthompsa
1301168793Sthompsa	if (m != NULL) {
1302249506Sglebius		counter_u64_add(sc->sc_ipackets, 1);
1303249506Sglebius		counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1304174278Sthompsa
1305174278Sthompsa		if (scifp->if_flags & IFF_MONITOR) {
1306174278Sthompsa			m_freem(m);
1307174278Sthompsa			m = NULL;
1308174278Sthompsa		}
1309168793Sthompsa	}
1310168793Sthompsa
1311169569Sthompsa	LAGG_RUNLOCK(sc);
1312168793Sthompsa	return (m);
1313168793Sthompsa}
1314168793Sthompsa
1315168793Sthompsastatic int
1316168793Sthompsalagg_media_change(struct ifnet *ifp)
1317168793Sthompsa{
1318168793Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1319168793Sthompsa
1320168793Sthompsa	if (sc->sc_ifflags & IFF_DEBUG)
1321168793Sthompsa		printf("%s\n", __func__);
1322168793Sthompsa
1323168793Sthompsa	/* Ignore */
1324168793Sthompsa	return (0);
1325168793Sthompsa}
1326168793Sthompsa
1327168793Sthompsastatic void
1328168793Sthompsalagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1329168793Sthompsa{
1330168793Sthompsa	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1331168793Sthompsa	struct lagg_port *lp;
1332168793Sthompsa
1333168793Sthompsa	imr->ifm_status = IFM_AVALID;
1334168793Sthompsa	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1335168793Sthompsa
1336169569Sthompsa	LAGG_RLOCK(sc);
1337169340Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1338169340Sthompsa		if (LAGG_PORTACTIVE(lp))
1339169340Sthompsa			imr->ifm_status |= IFM_ACTIVE;
1340169340Sthompsa	}
1341169569Sthompsa	LAGG_RUNLOCK(sc);
1342168793Sthompsa}
1343168793Sthompsa
1344168793Sthompsastatic void
1345173895Sthompsalagg_linkstate(struct lagg_softc *sc)
1346173895Sthompsa{
1347173895Sthompsa	struct lagg_port *lp;
1348173895Sthompsa	int new_link = LINK_STATE_DOWN;
1349186255Sthompsa	uint64_t speed;
1350173895Sthompsa
1351173895Sthompsa	/* Our link is considered up if at least one of our ports is active */
1352173895Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1353173895Sthompsa		if (lp->lp_link_state == LINK_STATE_UP) {
1354173895Sthompsa			new_link = LINK_STATE_UP;
1355173895Sthompsa			break;
1356173895Sthompsa		}
1357173895Sthompsa	}
1358173895Sthompsa	if_link_state_change(sc->sc_ifp, new_link);
1359186254Sthompsa
1360186254Sthompsa	/* Update if_baudrate to reflect the max possible speed */
1361186254Sthompsa	switch (sc->sc_proto) {
1362186254Sthompsa		case LAGG_PROTO_FAILOVER:
1363186255Sthompsa			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1364186255Sthompsa			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1365186254Sthompsa			break;
1366186254Sthompsa		case LAGG_PROTO_ROUNDROBIN:
1367186254Sthompsa		case LAGG_PROTO_LOADBALANCE:
1368186254Sthompsa		case LAGG_PROTO_ETHERCHANNEL:
1369186255Sthompsa			speed = 0;
1370186254Sthompsa			SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1371186254Sthompsa				speed += lp->lp_ifp->if_baudrate;
1372186254Sthompsa			sc->sc_ifp->if_baudrate = speed;
1373186254Sthompsa			break;
1374186254Sthompsa		case LAGG_PROTO_LACP:
1375186254Sthompsa			/* LACP updates if_baudrate itself */
1376186254Sthompsa			break;
1377186254Sthompsa	}
1378173895Sthompsa}
1379173895Sthompsa
1380173895Sthompsastatic void
1381168793Sthompsalagg_port_state(struct ifnet *ifp, int state)
1382168793Sthompsa{
1383168793Sthompsa	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1384168793Sthompsa	struct lagg_softc *sc = NULL;
1385168793Sthompsa
1386168793Sthompsa	if (lp != NULL)
1387170599Sthompsa		sc = lp->lp_softc;
1388168793Sthompsa	if (sc == NULL)
1389168793Sthompsa		return;
1390168793Sthompsa
1391169569Sthompsa	LAGG_WLOCK(sc);
1392173895Sthompsa	lagg_linkstate(sc);
1393168793Sthompsa	if (sc->sc_linkstate != NULL)
1394168793Sthompsa		(*sc->sc_linkstate)(lp);
1395169569Sthompsa	LAGG_WUNLOCK(sc);
1396168793Sthompsa}
1397168793Sthompsa
1398168793Sthompsastruct lagg_port *
1399168793Sthompsalagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1400168793Sthompsa{
1401168793Sthompsa	struct lagg_port *lp_next, *rval = NULL;
1402168793Sthompsa	// int new_link = LINK_STATE_DOWN;
1403168793Sthompsa
1404169688Sthompsa	LAGG_RLOCK_ASSERT(sc);
1405168793Sthompsa	/*
1406168793Sthompsa	 * Search a port which reports an active link state.
1407168793Sthompsa	 */
1408168793Sthompsa
1409168793Sthompsa	if (lp == NULL)
1410168793Sthompsa		goto search;
1411168793Sthompsa	if (LAGG_PORTACTIVE(lp)) {
1412168793Sthompsa		rval = lp;
1413168793Sthompsa		goto found;
1414168793Sthompsa	}
1415168793Sthompsa	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1416168793Sthompsa	    LAGG_PORTACTIVE(lp_next)) {
1417168793Sthompsa		rval = lp_next;
1418168793Sthompsa		goto found;
1419168793Sthompsa	}
1420168793Sthompsa
1421168793Sthompsasearch:
1422168793Sthompsa	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1423168793Sthompsa		if (LAGG_PORTACTIVE(lp_next)) {
1424168793Sthompsa			rval = lp_next;
1425168793Sthompsa			goto found;
1426168793Sthompsa		}
1427168793Sthompsa	}
1428168793Sthompsa
1429168793Sthompsafound:
1430168793Sthompsa	if (rval != NULL) {
1431168793Sthompsa		/*
1432168793Sthompsa		 * The IEEE 802.1D standard assumes that a lagg with
1433168793Sthompsa		 * multiple ports is always full duplex. This is valid
1434168793Sthompsa		 * for load sharing laggs and if at least two links
1435168793Sthompsa		 * are active. Unfortunately, checking the latter would
1436168793Sthompsa		 * be too expensive at this point.
1437168793Sthompsa		 XXX
1438168793Sthompsa		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1439168793Sthompsa		    (sc->sc_count > 1))
1440168793Sthompsa			new_link = LINK_STATE_FULL_DUPLEX;
1441168793Sthompsa		else
1442168793Sthompsa			new_link = rval->lp_link_state;
1443168793Sthompsa		 */
1444168793Sthompsa	}
1445168793Sthompsa
1446168793Sthompsa	return (rval);
1447168793Sthompsa}
1448168793Sthompsa
1449168793Sthompsastatic const void *
1450168793Sthompsalagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1451168793Sthompsa{
1452168793Sthompsa	if (m->m_pkthdr.len < (off + len)) {
1453168793Sthompsa		return (NULL);
1454168793Sthompsa	} else if (m->m_len < (off + len)) {
1455168793Sthompsa		m_copydata(m, off, len, buf);
1456168793Sthompsa		return (buf);
1457168793Sthompsa	}
1458168793Sthompsa	return (mtod(m, char *) + off);
1459168793Sthompsa}
1460168793Sthompsa
1461168793Sthompsauint32_t
1462232629Sthompsalagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1463168793Sthompsa{
1464168793Sthompsa	uint16_t etype;
1465232629Sthompsa	uint32_t p = key;
1466168793Sthompsa	int off;
1467168793Sthompsa	struct ether_header *eh;
1468168793Sthompsa	const struct ether_vlan_header *vlan;
1469168793Sthompsa#ifdef INET
1470168793Sthompsa	const struct ip *ip;
1471232629Sthompsa	const uint32_t *ports;
1472232629Sthompsa	int iphlen;
1473168793Sthompsa#endif
1474168793Sthompsa#ifdef INET6
1475168793Sthompsa	const struct ip6_hdr *ip6;
1476169583Sthompsa	uint32_t flow;
1477168793Sthompsa#endif
1478232629Sthompsa	union {
1479232629Sthompsa#ifdef INET
1480232629Sthompsa		struct ip ip;
1481232629Sthompsa#endif
1482232629Sthompsa#ifdef INET6
1483232629Sthompsa		struct ip6_hdr ip6;
1484232629Sthompsa#endif
1485232640Sthompsa		struct ether_vlan_header vlan;
1486232629Sthompsa		uint32_t port;
1487232629Sthompsa	} buf;
1488168793Sthompsa
1489232629Sthompsa
1490168793Sthompsa	off = sizeof(*eh);
1491168793Sthompsa	if (m->m_len < off)
1492168793Sthompsa		goto out;
1493168793Sthompsa	eh = mtod(m, struct ether_header *);
1494168793Sthompsa	etype = ntohs(eh->ether_type);
1495232629Sthompsa	if (sc->sc_flags & LAGG_F_HASHL2) {
1496232629Sthompsa		p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1497232629Sthompsa		p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1498232629Sthompsa	}
1499168793Sthompsa
1500168793Sthompsa	/* Special handling for encapsulating VLAN frames */
1501232629Sthompsa	if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1502168793Sthompsa		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1503168793Sthompsa		    sizeof(m->m_pkthdr.ether_vtag), p);
1504168793Sthompsa	} else if (etype == ETHERTYPE_VLAN) {
1505232640Sthompsa		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &buf);
1506170599Sthompsa		if (vlan == NULL)
1507168793Sthompsa			goto out;
1508168793Sthompsa
1509232629Sthompsa		if (sc->sc_flags & LAGG_F_HASHL2)
1510232629Sthompsa			p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1511168793Sthompsa		etype = ntohs(vlan->evl_proto);
1512168793Sthompsa		off += sizeof(*vlan) - sizeof(*eh);
1513168793Sthompsa	}
1514168793Sthompsa
1515168793Sthompsa	switch (etype) {
1516168793Sthompsa#ifdef INET
1517168793Sthompsa	case ETHERTYPE_IP:
1518232629Sthompsa		ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1519168793Sthompsa		if (ip == NULL)
1520168793Sthompsa			goto out;
1521168793Sthompsa
1522232629Sthompsa		if (sc->sc_flags & LAGG_F_HASHL3) {
1523232629Sthompsa			p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1524232629Sthompsa			p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1525232629Sthompsa		}
1526232629Sthompsa		if (!(sc->sc_flags & LAGG_F_HASHL4))
1527232629Sthompsa			break;
1528232629Sthompsa		switch (ip->ip_p) {
1529232629Sthompsa			case IPPROTO_TCP:
1530232629Sthompsa			case IPPROTO_UDP:
1531232629Sthompsa			case IPPROTO_SCTP:
1532232629Sthompsa				iphlen = ip->ip_hl << 2;
1533232629Sthompsa				if (iphlen < sizeof(*ip))
1534232629Sthompsa					break;
1535232629Sthompsa				off += iphlen;
1536232629Sthompsa				ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1537232629Sthompsa				if (ports == NULL)
1538232629Sthompsa					break;
1539232629Sthompsa				p = hash32_buf(ports, sizeof(*ports), p);
1540232629Sthompsa				break;
1541232629Sthompsa		}
1542168793Sthompsa		break;
1543168793Sthompsa#endif
1544168793Sthompsa#ifdef INET6
1545168793Sthompsa	case ETHERTYPE_IPV6:
1546232629Sthompsa		if (!(sc->sc_flags & LAGG_F_HASHL3))
1547232629Sthompsa			break;
1548232629Sthompsa		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1549168793Sthompsa		if (ip6 == NULL)
1550168793Sthompsa			goto out;
1551168793Sthompsa
1552168793Sthompsa		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1553168793Sthompsa		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1554169570Sthompsa		flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1555169570Sthompsa		p = hash32_buf(&flow, sizeof(flow), p);	/* IPv6 flow label */
1556168793Sthompsa		break;
1557168793Sthompsa#endif
1558168793Sthompsa	}
1559168793Sthompsaout:
1560168793Sthompsa	return (p);
1561168793Sthompsa}
1562168793Sthompsa
1563168793Sthompsaint
1564168793Sthompsalagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1565168793Sthompsa{
1566168793Sthompsa
1567185164Skmacy	return (ifp->if_transmit)(ifp, m);
1568168793Sthompsa}
1569168793Sthompsa
1570168793Sthompsa/*
1571168793Sthompsa * Simple round robin aggregation
1572168793Sthompsa */
1573168793Sthompsa
1574168793Sthompsastatic int
1575168793Sthompsalagg_rr_attach(struct lagg_softc *sc)
1576168793Sthompsa{
1577168793Sthompsa	sc->sc_detach = lagg_rr_detach;
1578168793Sthompsa	sc->sc_start = lagg_rr_start;
1579168793Sthompsa	sc->sc_input = lagg_rr_input;
1580168793Sthompsa	sc->sc_port_create = NULL;
1581168793Sthompsa	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1582172554Sthompsa	sc->sc_seq = 0;
1583168793Sthompsa
1584168793Sthompsa	return (0);
1585168793Sthompsa}
1586168793Sthompsa
1587168793Sthompsastatic int
1588168793Sthompsalagg_rr_detach(struct lagg_softc *sc)
1589168793Sthompsa{
1590168793Sthompsa	return (0);
1591168793Sthompsa}
1592168793Sthompsa
1593168793Sthompsastatic int
1594168793Sthompsalagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1595168793Sthompsa{
1596172554Sthompsa	struct lagg_port *lp;
1597172554Sthompsa	uint32_t p;
1598168793Sthompsa
1599172554Sthompsa	p = atomic_fetchadd_32(&sc->sc_seq, 1);
1600172554Sthompsa	p %= sc->sc_count;
1601172554Sthompsa	lp = SLIST_FIRST(&sc->sc_ports);
1602172554Sthompsa	while (p--)
1603172554Sthompsa		lp = SLIST_NEXT(lp, lp_entries);
1604172554Sthompsa
1605172554Sthompsa	/*
1606172554Sthompsa	 * Check the port's link state. This will return the next active
1607172554Sthompsa	 * port if the link is down or the port is NULL.
1608172554Sthompsa	 */
1609172554Sthompsa	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1610172554Sthompsa		m_freem(m);
1611168793Sthompsa		return (ENOENT);
1612172554Sthompsa	}
1613168793Sthompsa
1614168793Sthompsa	/* Send mbuf */
1615172554Sthompsa	return (lagg_enqueue(lp->lp_ifp, m));
1616168793Sthompsa}
1617168793Sthompsa
1618168793Sthompsastatic struct mbuf *
1619168793Sthompsalagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1620168793Sthompsa{
1621168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1622168793Sthompsa
1623168793Sthompsa	/* Just pass in the packet to our lagg device */
1624168793Sthompsa	m->m_pkthdr.rcvif = ifp;
1625168793Sthompsa
1626168793Sthompsa	return (m);
1627168793Sthompsa}
1628168793Sthompsa
1629168793Sthompsa/*
1630168793Sthompsa * Active failover
1631168793Sthompsa */
1632168793Sthompsa
1633168793Sthompsastatic int
1634168793Sthompsalagg_fail_attach(struct lagg_softc *sc)
1635168793Sthompsa{
1636168793Sthompsa	sc->sc_detach = lagg_fail_detach;
1637168793Sthompsa	sc->sc_start = lagg_fail_start;
1638168793Sthompsa	sc->sc_input = lagg_fail_input;
1639168793Sthompsa	sc->sc_port_create = NULL;
1640168793Sthompsa	sc->sc_port_destroy = NULL;
1641168793Sthompsa
1642168793Sthompsa	return (0);
1643168793Sthompsa}
1644168793Sthompsa
1645168793Sthompsastatic int
1646168793Sthompsalagg_fail_detach(struct lagg_softc *sc)
1647168793Sthompsa{
1648168793Sthompsa	return (0);
1649168793Sthompsa}
1650168793Sthompsa
1651168793Sthompsastatic int
1652168793Sthompsalagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1653168793Sthompsa{
1654168793Sthompsa	struct lagg_port *lp;
1655168793Sthompsa
1656168793Sthompsa	/* Use the master port if active or the next available port */
1657172554Sthompsa	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1658172554Sthompsa		m_freem(m);
1659168793Sthompsa		return (ENOENT);
1660172554Sthompsa	}
1661168793Sthompsa
1662168793Sthompsa	/* Send mbuf */
1663168793Sthompsa	return (lagg_enqueue(lp->lp_ifp, m));
1664168793Sthompsa}
1665168793Sthompsa
1666168793Sthompsastatic struct mbuf *
1667168793Sthompsalagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1668168793Sthompsa{
1669168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1670168793Sthompsa	struct lagg_port *tmp_tp;
1671168793Sthompsa
1672212100Semaste	if (lp == sc->sc_primary || lagg_failover_rx_all) {
1673168793Sthompsa		m->m_pkthdr.rcvif = ifp;
1674168793Sthompsa		return (m);
1675168793Sthompsa	}
1676168793Sthompsa
1677174742Sthompsa	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1678174742Sthompsa		tmp_tp = lagg_link_active(sc, sc->sc_primary);
1679168793Sthompsa		/*
1680168793Sthompsa		 * If tmp_tp is null, we've recieved a packet when all
1681168793Sthompsa		 * our links are down. Weird, but process it anyways.
1682168793Sthompsa		 */
1683168793Sthompsa		if ((tmp_tp == NULL || tmp_tp == lp)) {
1684168793Sthompsa			m->m_pkthdr.rcvif = ifp;
1685168793Sthompsa			return (m);
1686168793Sthompsa		}
1687168793Sthompsa	}
1688168793Sthompsa
1689168793Sthompsa	m_freem(m);
1690168793Sthompsa	return (NULL);
1691168793Sthompsa}
1692168793Sthompsa
1693168793Sthompsa/*
1694168793Sthompsa * Loadbalancing
1695168793Sthompsa */
1696168793Sthompsa
1697168793Sthompsastatic int
1698168793Sthompsalagg_lb_attach(struct lagg_softc *sc)
1699168793Sthompsa{
1700168793Sthompsa	struct lagg_port *lp;
1701168793Sthompsa	struct lagg_lb *lb;
1702168793Sthompsa
1703168793Sthompsa	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1704168793Sthompsa	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1705168793Sthompsa		return (ENOMEM);
1706168793Sthompsa
1707168793Sthompsa	sc->sc_detach = lagg_lb_detach;
1708168793Sthompsa	sc->sc_start = lagg_lb_start;
1709168793Sthompsa	sc->sc_input = lagg_lb_input;
1710168793Sthompsa	sc->sc_port_create = lagg_lb_port_create;
1711168793Sthompsa	sc->sc_port_destroy = lagg_lb_port_destroy;
1712168793Sthompsa	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1713168793Sthompsa
1714168793Sthompsa	lb->lb_key = arc4random();
1715168793Sthompsa	sc->sc_psc = (caddr_t)lb;
1716168793Sthompsa
1717168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1718168793Sthompsa		lagg_lb_port_create(lp);
1719168793Sthompsa
1720168793Sthompsa	return (0);
1721168793Sthompsa}
1722168793Sthompsa
1723168793Sthompsastatic int
1724168793Sthompsalagg_lb_detach(struct lagg_softc *sc)
1725168793Sthompsa{
1726168793Sthompsa	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1727168793Sthompsa	if (lb != NULL)
1728168793Sthompsa		free(lb, M_DEVBUF);
1729168793Sthompsa	return (0);
1730168793Sthompsa}
1731168793Sthompsa
1732168793Sthompsastatic int
1733168793Sthompsalagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1734168793Sthompsa{
1735168793Sthompsa	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1736168793Sthompsa	struct lagg_port *lp_next;
1737168793Sthompsa	int i = 0;
1738168793Sthompsa
1739168793Sthompsa	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1740168793Sthompsa	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1741168793Sthompsa		if (lp_next == lp)
1742168793Sthompsa			continue;
1743168793Sthompsa		if (i >= LAGG_MAX_PORTS)
1744168793Sthompsa			return (EINVAL);
1745168793Sthompsa		if (sc->sc_ifflags & IFF_DEBUG)
1746168793Sthompsa			printf("%s: port %s at index %d\n",
1747168793Sthompsa			    sc->sc_ifname, lp_next->lp_ifname, i);
1748168793Sthompsa		lb->lb_ports[i++] = lp_next;
1749168793Sthompsa	}
1750168793Sthompsa
1751168793Sthompsa	return (0);
1752168793Sthompsa}
1753168793Sthompsa
1754168793Sthompsastatic int
1755168793Sthompsalagg_lb_port_create(struct lagg_port *lp)
1756168793Sthompsa{
1757170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
1758168793Sthompsa	return (lagg_lb_porttable(sc, NULL));
1759168793Sthompsa}
1760168793Sthompsa
1761168793Sthompsastatic void
1762168793Sthompsalagg_lb_port_destroy(struct lagg_port *lp)
1763168793Sthompsa{
1764170599Sthompsa	struct lagg_softc *sc = lp->lp_softc;
1765168793Sthompsa	lagg_lb_porttable(sc, lp);
1766168793Sthompsa}
1767168793Sthompsa
1768168793Sthompsastatic int
1769168793Sthompsalagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1770168793Sthompsa{
1771168793Sthompsa	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1772168793Sthompsa	struct lagg_port *lp = NULL;
1773168793Sthompsa	uint32_t p = 0;
1774168793Sthompsa
1775232008Sthompsa	if (sc->use_flowid && (m->m_flags & M_FLOWID))
1776191692Sthompsa		p = m->m_pkthdr.flowid;
1777191692Sthompsa	else
1778232629Sthompsa		p = lagg_hashmbuf(sc, m, lb->lb_key);
1779180249Sthompsa	p %= sc->sc_count;
1780180249Sthompsa	lp = lb->lb_ports[p];
1781168793Sthompsa
1782168793Sthompsa	/*
1783168793Sthompsa	 * Check the port's link state. This will return the next active
1784168793Sthompsa	 * port if the link is down or the port is NULL.
1785168793Sthompsa	 */
1786172554Sthompsa	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1787172554Sthompsa		m_freem(m);
1788168793Sthompsa		return (ENOENT);
1789172554Sthompsa	}
1790168793Sthompsa
1791168793Sthompsa	/* Send mbuf */
1792168793Sthompsa	return (lagg_enqueue(lp->lp_ifp, m));
1793168793Sthompsa}
1794168793Sthompsa
1795168793Sthompsastatic struct mbuf *
1796168793Sthompsalagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1797168793Sthompsa{
1798168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1799168793Sthompsa
1800168793Sthompsa	/* Just pass in the packet to our lagg device */
1801168793Sthompsa	m->m_pkthdr.rcvif = ifp;
1802168793Sthompsa
1803168793Sthompsa	return (m);
1804168793Sthompsa}
1805168793Sthompsa
1806168793Sthompsa/*
1807168793Sthompsa * 802.3ad LACP
1808168793Sthompsa */
1809168793Sthompsa
1810168793Sthompsastatic int
1811168793Sthompsalagg_lacp_attach(struct lagg_softc *sc)
1812168793Sthompsa{
1813168793Sthompsa	struct lagg_port *lp;
1814168793Sthompsa	int error;
1815168793Sthompsa
1816168793Sthompsa	sc->sc_detach = lagg_lacp_detach;
1817168793Sthompsa	sc->sc_port_create = lacp_port_create;
1818168793Sthompsa	sc->sc_port_destroy = lacp_port_destroy;
1819168793Sthompsa	sc->sc_linkstate = lacp_linkstate;
1820168793Sthompsa	sc->sc_start = lagg_lacp_start;
1821168793Sthompsa	sc->sc_input = lagg_lacp_input;
1822168793Sthompsa	sc->sc_init = lacp_init;
1823168793Sthompsa	sc->sc_stop = lacp_stop;
1824168793Sthompsa	sc->sc_lladdr = lagg_lacp_lladdr;
1825171247Sthompsa	sc->sc_req = lacp_req;
1826171247Sthompsa	sc->sc_portreq = lacp_portreq;
1827168793Sthompsa
1828168793Sthompsa	error = lacp_attach(sc);
1829168793Sthompsa	if (error)
1830168793Sthompsa		return (error);
1831168793Sthompsa
1832168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1833168793Sthompsa		lacp_port_create(lp);
1834168793Sthompsa
1835168793Sthompsa	return (error);
1836168793Sthompsa}
1837168793Sthompsa
1838168793Sthompsastatic int
1839168793Sthompsalagg_lacp_detach(struct lagg_softc *sc)
1840168793Sthompsa{
1841168793Sthompsa	struct lagg_port *lp;
1842168793Sthompsa	int error;
1843168793Sthompsa
1844168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1845168793Sthompsa		lacp_port_destroy(lp);
1846168793Sthompsa
1847168793Sthompsa	/* unlocking is safe here */
1848169569Sthompsa	LAGG_WUNLOCK(sc);
1849168793Sthompsa	error = lacp_detach(sc);
1850169569Sthompsa	LAGG_WLOCK(sc);
1851168793Sthompsa
1852168793Sthompsa	return (error);
1853168793Sthompsa}
1854168793Sthompsa
1855168793Sthompsastatic void
1856168793Sthompsalagg_lacp_lladdr(struct lagg_softc *sc)
1857168793Sthompsa{
1858168793Sthompsa	struct lagg_port *lp;
1859168793Sthompsa
1860168793Sthompsa	/* purge all the lacp ports */
1861168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1862168793Sthompsa		lacp_port_destroy(lp);
1863168793Sthompsa
1864168793Sthompsa	/* add them back in */
1865168793Sthompsa	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1866168793Sthompsa		lacp_port_create(lp);
1867168793Sthompsa}
1868168793Sthompsa
1869168793Sthompsastatic int
1870168793Sthompsalagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1871168793Sthompsa{
1872168793Sthompsa	struct lagg_port *lp;
1873168793Sthompsa
1874168793Sthompsa	lp = lacp_select_tx_port(sc, m);
1875172554Sthompsa	if (lp == NULL) {
1876172554Sthompsa		m_freem(m);
1877245741Sglebius		return (ENETDOWN);
1878172554Sthompsa	}
1879168793Sthompsa
1880168793Sthompsa	/* Send mbuf */
1881168793Sthompsa	return (lagg_enqueue(lp->lp_ifp, m));
1882168793Sthompsa}
1883168793Sthompsa
1884168793Sthompsastatic struct mbuf *
1885168793Sthompsalagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1886168793Sthompsa{
1887168793Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1888168793Sthompsa	struct ether_header *eh;
1889168793Sthompsa	u_short etype;
1890168793Sthompsa
1891168793Sthompsa	eh = mtod(m, struct ether_header *);
1892168793Sthompsa	etype = ntohs(eh->ether_type);
1893168793Sthompsa
1894168793Sthompsa	/* Tap off LACP control messages */
1895221270Sthompsa	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
1896175005Sthompsa		m = lacp_input(lp, m);
1897175005Sthompsa		if (m == NULL)
1898175005Sthompsa			return (NULL);
1899168793Sthompsa	}
1900168793Sthompsa
1901168793Sthompsa	/*
1902168793Sthompsa	 * If the port is not collecting or not in the active aggregator then
1903168793Sthompsa	 * free and return.
1904168793Sthompsa	 */
1905177274Sthompsa	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1906168793Sthompsa		m_freem(m);
1907168793Sthompsa		return (NULL);
1908168793Sthompsa	}
1909168793Sthompsa
1910168793Sthompsa	m->m_pkthdr.rcvif = ifp;
1911168793Sthompsa	return (m);
1912168793Sthompsa}
1913249506Sglebius
1914249506Sglebiusstatic void
1915249506Sglebiuslagg_callout(void *arg)
1916249506Sglebius{
1917249506Sglebius	struct lagg_softc *sc = (struct lagg_softc *)arg;
1918249506Sglebius	struct ifnet *ifp = sc->sc_ifp;
1919249506Sglebius
1920249506Sglebius	ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
1921249506Sglebius	ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
1922249506Sglebius	ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
1923249506Sglebius	ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
1924249506Sglebius
1925249506Sglebius	callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
1926249506Sglebius}
1927