if_lagg.c revision 273736
1/*	$OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $	*/
2
3/*
4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/cdefs.h>
21__FBSDID("$FreeBSD: stable/10/sys/net/if_lagg.c 273736 2014-10-27 14:38:00Z hselasky $");
22
23#include "opt_inet.h"
24#include "opt_inet6.h"
25
26#include <sys/param.h>
27#include <sys/kernel.h>
28#include <sys/malloc.h>
29#include <sys/mbuf.h>
30#include <sys/queue.h>
31#include <sys/socket.h>
32#include <sys/sockio.h>
33#include <sys/sysctl.h>
34#include <sys/module.h>
35#include <sys/priv.h>
36#include <sys/systm.h>
37#include <sys/proc.h>
38#include <sys/hash.h>
39#include <sys/lock.h>
40#include <sys/rmlock.h>
41#include <sys/taskqueue.h>
42#include <sys/eventhandler.h>
43
44#include <net/ethernet.h>
45#include <net/if.h>
46#include <net/if_clone.h>
47#include <net/if_arp.h>
48#include <net/if_dl.h>
49#include <net/if_llc.h>
50#include <net/if_media.h>
51#include <net/if_types.h>
52#include <net/if_var.h>
53#include <net/bpf.h>
54
55#if defined(INET) || defined(INET6)
56#include <netinet/in.h>
57#include <netinet/ip.h>
58#endif
59#ifdef INET
60#include <netinet/in_systm.h>
61#include <netinet/if_ether.h>
62#endif
63
64#ifdef INET6
65#include <netinet/ip6.h>
66#include <netinet6/in6_var.h>
67#include <netinet6/in6_ifattach.h>
68#endif
69
70#include <net/if_vlan_var.h>
71#include <net/if_lagg.h>
72#include <net/ieee8023ad_lacp.h>
73
74/* Special flags we should propagate to the lagg ports. */
75static struct {
76	int flag;
77	int (*func)(struct ifnet *, int);
78} lagg_pflags[] = {
79	{IFF_PROMISC, ifpromisc},
80	{IFF_ALLMULTI, if_allmulti},
81	{0, NULL}
82};
83
84SLIST_HEAD(__trhead, lagg_softc) lagg_list;	/* list of laggs */
85static struct mtx	lagg_list_mtx;
86eventhandler_tag	lagg_detach_cookie = NULL;
87
88static int	lagg_clone_create(struct if_clone *, int, caddr_t);
89static void	lagg_clone_destroy(struct ifnet *);
90static struct if_clone *lagg_cloner;
91static const char laggname[] = "lagg";
92
93static void	lagg_lladdr(struct lagg_softc *, uint8_t *);
94static void	lagg_capabilities(struct lagg_softc *);
95static void	lagg_port_lladdr(struct lagg_port *, uint8_t *);
96static void	lagg_port_setlladdr(void *, int);
97static int	lagg_port_create(struct lagg_softc *, struct ifnet *);
98static int	lagg_port_destroy(struct lagg_port *, int);
99static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
100static void	lagg_linkstate(struct lagg_softc *);
101static void	lagg_port_state(struct ifnet *, int);
102static int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
103static int	lagg_port_output(struct ifnet *, struct mbuf *,
104		    const struct sockaddr *, struct route *);
105static void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
106#ifdef LAGG_PORT_STACKING
107static int	lagg_port_checkstacking(struct lagg_softc *);
108#endif
109static void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
110static void	lagg_init(void *);
111static void	lagg_stop(struct lagg_softc *);
112static int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
113static int	lagg_ether_setmulti(struct lagg_softc *);
114static int	lagg_ether_cmdmulti(struct lagg_port *, int);
115static	int	lagg_setflag(struct lagg_port *, int, int,
116		    int (*func)(struct ifnet *, int));
117static	int	lagg_setflags(struct lagg_port *, int status);
118static int	lagg_transmit(struct ifnet *, struct mbuf *);
119static void	lagg_qflush(struct ifnet *);
120static int	lagg_media_change(struct ifnet *);
121static void	lagg_media_status(struct ifnet *, struct ifmediareq *);
122static struct lagg_port *lagg_link_active(struct lagg_softc *,
123	    struct lagg_port *);
124static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
125static int	lagg_sysctl_active(SYSCTL_HANDLER_ARGS);
126
127/* Simple round robin */
128static int	lagg_rr_attach(struct lagg_softc *);
129static int	lagg_rr_detach(struct lagg_softc *);
130static int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
131static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
132		    struct mbuf *);
133
134/* Active failover */
135static int	lagg_fail_attach(struct lagg_softc *);
136static int	lagg_fail_detach(struct lagg_softc *);
137static int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
138static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
139		    struct mbuf *);
140
141/* Loadbalancing */
142static int	lagg_lb_attach(struct lagg_softc *);
143static int	lagg_lb_detach(struct lagg_softc *);
144static int	lagg_lb_port_create(struct lagg_port *);
145static void	lagg_lb_port_destroy(struct lagg_port *);
146static int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
147static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
148		    struct mbuf *);
149static int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
150
151/* 802.3ad LACP */
152static int	lagg_lacp_attach(struct lagg_softc *);
153static int	lagg_lacp_detach(struct lagg_softc *);
154static int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
155static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
156		    struct mbuf *);
157static void	lagg_lacp_lladdr(struct lagg_softc *);
158
159static void	lagg_callout(void *);
160
161/* lagg protocol table */
162static const struct {
163	int			ti_proto;
164	int			(*ti_attach)(struct lagg_softc *);
165} lagg_protos[] = {
166	{ LAGG_PROTO_ROUNDROBIN,	lagg_rr_attach },
167	{ LAGG_PROTO_FAILOVER,		lagg_fail_attach },
168	{ LAGG_PROTO_LOADBALANCE,	lagg_lb_attach },
169	{ LAGG_PROTO_ETHERCHANNEL,	lagg_lb_attach },
170	{ LAGG_PROTO_LACP,		lagg_lacp_attach },
171	{ LAGG_PROTO_NONE,		NULL }
172};
173
174SYSCTL_DECL(_net_link);
175SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
176    "Link Aggregation");
177
178static int lagg_failover_rx_all = 0; /* Allow input on any failover links */
179SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW,
180    &lagg_failover_rx_all, 0,
181    "Accept input from any interface in a failover lagg");
182static int def_use_flowid = 1; /* Default value for using M_FLOWID */
183TUNABLE_INT("net.link.lagg.default_use_flowid", &def_use_flowid);
184SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RW,
185    &def_use_flowid, 0,
186    "Default setting for using flow id for load sharing");
187static int def_flowid_shift = 16; /* Default value for using M_FLOWID */
188TUNABLE_INT("net.link.lagg.default_flowid_shift", &def_flowid_shift);
189SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RW,
190    &def_flowid_shift, 0,
191    "Default setting for flowid shift for load sharing");
192
193static int
194lagg_modevent(module_t mod, int type, void *data)
195{
196
197	switch (type) {
198	case MOD_LOAD:
199		mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
200		SLIST_INIT(&lagg_list);
201		lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
202		    lagg_clone_destroy, 0);
203		lagg_input_p = lagg_input;
204		lagg_linkstate_p = lagg_port_state;
205		lagg_detach_cookie = EVENTHANDLER_REGISTER(
206		    ifnet_departure_event, lagg_port_ifdetach, NULL,
207		    EVENTHANDLER_PRI_ANY);
208		break;
209	case MOD_UNLOAD:
210		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
211		    lagg_detach_cookie);
212		if_clone_detach(lagg_cloner);
213		lagg_input_p = NULL;
214		lagg_linkstate_p = NULL;
215		mtx_destroy(&lagg_list_mtx);
216		break;
217	default:
218		return (EOPNOTSUPP);
219	}
220	return (0);
221}
222
223static moduledata_t lagg_mod = {
224	"if_lagg",
225	lagg_modevent,
226	0
227};
228
229DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
230MODULE_VERSION(if_lagg, 1);
231
232/*
233 * This routine is run via an vlan
234 * config EVENT
235 */
236static void
237lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
238{
239        struct lagg_softc       *sc = ifp->if_softc;
240        struct lagg_port        *lp;
241        struct rm_priotracker   tracker;
242
243        if (ifp->if_softc !=  arg)   /* Not our event */
244                return;
245
246        LAGG_RLOCK(sc, &tracker);
247        if (!SLIST_EMPTY(&sc->sc_ports)) {
248                SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
249                        EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
250        }
251        LAGG_RUNLOCK(sc, &tracker);
252}
253
254/*
255 * This routine is run via an vlan
256 * unconfig EVENT
257 */
258static void
259lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
260{
261        struct lagg_softc       *sc = ifp->if_softc;
262        struct lagg_port        *lp;
263        struct rm_priotracker   tracker;
264
265        if (ifp->if_softc !=  arg)   /* Not our event */
266                return;
267
268        LAGG_RLOCK(sc, &tracker);
269        if (!SLIST_EMPTY(&sc->sc_ports)) {
270                SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
271                        EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
272        }
273        LAGG_RUNLOCK(sc, &tracker);
274}
275
276static int
277lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
278{
279	struct lagg_softc *sc;
280	struct ifnet *ifp;
281	int i, error = 0;
282	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
283	struct sysctl_oid *oid;
284	char num[14];			/* sufficient for 32 bits */
285
286	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
287	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
288	if (ifp == NULL) {
289		free(sc, M_DEVBUF);
290		return (ENOSPC);
291	}
292
293	sc->sc_ipackets = counter_u64_alloc(M_WAITOK);
294	sc->sc_opackets = counter_u64_alloc(M_WAITOK);
295	sc->sc_ibytes = counter_u64_alloc(M_WAITOK);
296	sc->sc_obytes = counter_u64_alloc(M_WAITOK);
297
298	sysctl_ctx_init(&sc->ctx);
299	snprintf(num, sizeof(num), "%u", unit);
300	sc->use_flowid = def_use_flowid;
301	sc->flowid_shift = def_flowid_shift;
302	sc->sc_oid = oid = SYSCTL_ADD_NODE(&sc->ctx,
303		&SYSCTL_NODE_CHILDREN(_net_link, lagg),
304		OID_AUTO, num, CTLFLAG_RD, NULL, "");
305	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
306		"use_flowid", CTLFLAG_RW, &sc->use_flowid,
307		sc->use_flowid, "Use flow id for load sharing");
308	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
309		"flowid_shift", CTLFLAG_RW, &sc->flowid_shift,
310		sc->flowid_shift,
311		"Shift flowid bits to prevent multiqueue collisions");
312	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
313		"count", CTLFLAG_RD, &sc->sc_count, sc->sc_count,
314		"Total number of ports");
315	SYSCTL_ADD_PROC(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
316		"active", CTLTYPE_INT|CTLFLAG_RD, sc, 0, lagg_sysctl_active,
317		"I", "Total number of active ports");
318	SYSCTL_ADD_INT(&sc->ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
319		"flapping", CTLFLAG_RD, &sc->sc_flapping,
320		sc->sc_flapping, "Total number of port change events");
321	/* Hash all layers by default */
322	sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
323
324	sc->sc_proto = LAGG_PROTO_NONE;
325	for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
326		if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
327			sc->sc_proto = lagg_protos[i].ti_proto;
328			if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
329				if_free(ifp);
330				free(sc, M_DEVBUF);
331				return (error);
332			}
333			break;
334		}
335	}
336	LAGG_LOCK_INIT(sc);
337	LAGG_CALLOUT_LOCK_INIT(sc);
338	SLIST_INIT(&sc->sc_ports);
339	TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
340
341	/*
342	 * This uses the callout lock rather than the rmlock; one can't
343	 * hold said rmlock during SWI.
344	 */
345	callout_init_mtx(&sc->sc_callout, &sc->sc_call_mtx, 0);
346
347	/* Initialise pseudo media types */
348	ifmedia_init(&sc->sc_media, 0, lagg_media_change,
349	    lagg_media_status);
350	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
351	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
352
353	if_initname(ifp, laggname, unit);
354	ifp->if_softc = sc;
355	ifp->if_transmit = lagg_transmit;
356	ifp->if_qflush = lagg_qflush;
357	ifp->if_init = lagg_init;
358	ifp->if_ioctl = lagg_ioctl;
359	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
360	ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
361
362	/*
363	 * Attach as an ordinary ethernet device, children will be attached
364	 * as special device IFT_IEEE8023ADLAG.
365	 */
366	ether_ifattach(ifp, eaddr);
367
368	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
369		lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
370	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
371		lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
372
373	/* Insert into the global list of laggs */
374	mtx_lock(&lagg_list_mtx);
375	SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
376	mtx_unlock(&lagg_list_mtx);
377
378	callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
379
380	return (0);
381}
382
383static void
384lagg_clone_destroy(struct ifnet *ifp)
385{
386	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
387	struct lagg_port *lp;
388
389	LAGG_WLOCK(sc);
390
391	lagg_stop(sc);
392	ifp->if_flags &= ~IFF_UP;
393
394	EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
395	EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
396
397	/* Shutdown and remove lagg ports */
398	while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
399		lagg_port_destroy(lp, 1);
400	/* Unhook the aggregation protocol */
401	if (sc->sc_detach != NULL)
402		(*sc->sc_detach)(sc);
403
404	LAGG_WUNLOCK(sc);
405
406	sysctl_ctx_free(&sc->ctx);
407	ifmedia_removeall(&sc->sc_media);
408	ether_ifdetach(ifp);
409	if_free(ifp);
410
411	/* This grabs sc_callout_mtx, serialising it correctly */
412	callout_drain(&sc->sc_callout);
413
414	/* At this point it's drained; we can free this */
415	counter_u64_free(sc->sc_ipackets);
416	counter_u64_free(sc->sc_opackets);
417	counter_u64_free(sc->sc_ibytes);
418	counter_u64_free(sc->sc_obytes);
419
420	mtx_lock(&lagg_list_mtx);
421	SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
422	mtx_unlock(&lagg_list_mtx);
423
424	taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
425	LAGG_LOCK_DESTROY(sc);
426	LAGG_CALLOUT_LOCK_DESTROY(sc);
427	free(sc, M_DEVBUF);
428}
429
430static void
431lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
432{
433	struct ifnet *ifp = sc->sc_ifp;
434
435	if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
436		return;
437
438	bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
439	/* Let the protocol know the MAC has changed */
440	if (sc->sc_lladdr != NULL)
441		(*sc->sc_lladdr)(sc);
442	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
443}
444
445static void
446lagg_capabilities(struct lagg_softc *sc)
447{
448	struct lagg_port *lp;
449	int cap = ~0, ena = ~0;
450	u_long hwa = ~0UL;
451#if defined(INET) || defined(INET6)
452	u_int hw_tsomax = IP_MAXPACKET;	/* Initialize to the maximum value. */
453#else
454	u_int hw_tsomax = ~0;	/* if_hw_tsomax is only for INET/INET6, but.. */
455#endif
456
457	LAGG_WLOCK_ASSERT(sc);
458
459	/* Get capabilities from the lagg ports */
460	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
461		cap &= lp->lp_ifp->if_capabilities;
462		ena &= lp->lp_ifp->if_capenable;
463		hwa &= lp->lp_ifp->if_hwassist;
464		/* Set to the minimum value of the lagg ports. */
465		if (lp->lp_ifp->if_hw_tsomax < hw_tsomax &&
466		    lp->lp_ifp->if_hw_tsomax > 0)
467			hw_tsomax = lp->lp_ifp->if_hw_tsomax;
468	}
469	cap = (cap == ~0 ? 0 : cap);
470	ena = (ena == ~0 ? 0 : ena);
471	hwa = (hwa == ~0 ? 0 : hwa);
472
473	if (sc->sc_ifp->if_capabilities != cap ||
474	    sc->sc_ifp->if_capenable != ena ||
475	    sc->sc_ifp->if_hwassist != hwa ||
476	    sc->sc_ifp->if_hw_tsomax != hw_tsomax) {
477		sc->sc_ifp->if_capabilities = cap;
478		sc->sc_ifp->if_capenable = ena;
479		sc->sc_ifp->if_hwassist = hwa;
480		sc->sc_ifp->if_hw_tsomax = hw_tsomax;
481		getmicrotime(&sc->sc_ifp->if_lastchange);
482
483		if (sc->sc_ifflags & IFF_DEBUG)
484			if_printf(sc->sc_ifp,
485			    "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
486	}
487}
488
489static void
490lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
491{
492	struct lagg_softc *sc = lp->lp_softc;
493	struct ifnet *ifp = lp->lp_ifp;
494	struct lagg_llq *llq;
495	int pending = 0;
496
497	LAGG_WLOCK_ASSERT(sc);
498
499	if (lp->lp_detaching ||
500	    memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
501		return;
502
503	/* Check to make sure its not already queued to be changed */
504	SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
505		if (llq->llq_ifp == ifp) {
506			pending = 1;
507			break;
508		}
509	}
510
511	if (!pending) {
512		llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
513		if (llq == NULL)	/* XXX what to do */
514			return;
515	}
516
517	/* Update the lladdr even if pending, it may have changed */
518	llq->llq_ifp = ifp;
519	bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
520
521	if (!pending)
522		SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
523
524	taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
525}
526
527/*
528 * Set the interface MAC address from a taskqueue to avoid a LOR.
529 */
530static void
531lagg_port_setlladdr(void *arg, int pending)
532{
533	struct lagg_softc *sc = (struct lagg_softc *)arg;
534	struct lagg_llq *llq, *head;
535	struct ifnet *ifp;
536	int error;
537
538	/* Grab a local reference of the queue and remove it from the softc */
539	LAGG_WLOCK(sc);
540	head = SLIST_FIRST(&sc->sc_llq_head);
541	SLIST_FIRST(&sc->sc_llq_head) = NULL;
542	LAGG_WUNLOCK(sc);
543
544	/*
545	 * Traverse the queue and set the lladdr on each ifp. It is safe to do
546	 * unlocked as we have the only reference to it.
547	 */
548	for (llq = head; llq != NULL; llq = head) {
549		ifp = llq->llq_ifp;
550
551		/* Set the link layer address */
552		CURVNET_SET(ifp->if_vnet);
553		error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
554		CURVNET_RESTORE();
555		if (error)
556			printf("%s: setlladdr failed on %s\n", __func__,
557			    ifp->if_xname);
558
559		head = SLIST_NEXT(llq, llq_entries);
560		free(llq, M_DEVBUF);
561	}
562}
563
564static int
565lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
566{
567	struct lagg_softc *sc_ptr;
568	struct lagg_port *lp, *tlp;
569	int error = 0;
570
571	LAGG_WLOCK_ASSERT(sc);
572
573	/* Limit the maximal number of lagg ports */
574	if (sc->sc_count >= LAGG_MAX_PORTS)
575		return (ENOSPC);
576
577	/* Check if port has already been associated to a lagg */
578	if (ifp->if_lagg != NULL) {
579		/* Port is already in the current lagg? */
580		lp = (struct lagg_port *)ifp->if_lagg;
581		if (lp->lp_softc == sc)
582			return (EEXIST);
583		return (EBUSY);
584	}
585
586	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
587	if (ifp->if_type != IFT_ETHER)
588		return (EPROTONOSUPPORT);
589
590#ifdef INET6
591	/*
592	 * The member interface should not have inet6 address because
593	 * two interfaces with a valid link-local scope zone must not be
594	 * merged in any form.  This restriction is needed to
595	 * prevent violation of link-local scope zone.  Attempts to
596	 * add a member interface which has inet6 addresses triggers
597	 * removal of all inet6 addresses on the member interface.
598	 */
599	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
600		if (in6ifa_llaonifp(lp->lp_ifp)) {
601			in6_ifdetach(lp->lp_ifp);
602			if_printf(sc->sc_ifp,
603			    "IPv6 addresses on %s have been removed "
604			    "before adding it as a member to prevent "
605			    "IPv6 address scope violation.\n",
606			    lp->lp_ifp->if_xname);
607		}
608	}
609	if (in6ifa_llaonifp(ifp)) {
610		in6_ifdetach(ifp);
611		if_printf(sc->sc_ifp,
612		    "IPv6 addresses on %s have been removed "
613		    "before adding it as a member to prevent "
614		    "IPv6 address scope violation.\n",
615		    ifp->if_xname);
616	}
617#endif
618	/* Allow the first Ethernet member to define the MTU */
619	if (SLIST_EMPTY(&sc->sc_ports))
620		sc->sc_ifp->if_mtu = ifp->if_mtu;
621	else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
622		if_printf(sc->sc_ifp, "invalid MTU for %s\n",
623		    ifp->if_xname);
624		return (EINVAL);
625	}
626
627	if ((lp = malloc(sizeof(struct lagg_port),
628	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
629		return (ENOMEM);
630
631	/* Check if port is a stacked lagg */
632	mtx_lock(&lagg_list_mtx);
633	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
634		if (ifp == sc_ptr->sc_ifp) {
635			mtx_unlock(&lagg_list_mtx);
636			free(lp, M_DEVBUF);
637			return (EINVAL);
638			/* XXX disable stacking for the moment, its untested */
639#ifdef LAGG_PORT_STACKING
640			lp->lp_flags |= LAGG_PORT_STACK;
641			if (lagg_port_checkstacking(sc_ptr) >=
642			    LAGG_MAX_STACKING) {
643				mtx_unlock(&lagg_list_mtx);
644				free(lp, M_DEVBUF);
645				return (E2BIG);
646			}
647#endif
648		}
649	}
650	mtx_unlock(&lagg_list_mtx);
651
652	/* Change the interface type */
653	lp->lp_iftype = ifp->if_type;
654	ifp->if_type = IFT_IEEE8023ADLAG;
655	ifp->if_lagg = lp;
656	lp->lp_ioctl = ifp->if_ioctl;
657	ifp->if_ioctl = lagg_port_ioctl;
658	lp->lp_output = ifp->if_output;
659	ifp->if_output = lagg_port_output;
660
661	lp->lp_ifp = ifp;
662	lp->lp_softc = sc;
663
664	/* Save port link layer address */
665	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
666
667	if (SLIST_EMPTY(&sc->sc_ports)) {
668		sc->sc_primary = lp;
669		lagg_lladdr(sc, IF_LLADDR(ifp));
670	} else {
671		/* Update link layer address for this port */
672		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
673	}
674
675	/* Insert into the list of ports. Keep ports sorted by if_index. */
676	SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
677		if (tlp->lp_ifp->if_index < ifp->if_index && (
678		    SLIST_NEXT(tlp, lp_entries) == NULL ||
679		    SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index <
680		    ifp->if_index))
681			break;
682	}
683	if (tlp != NULL)
684		SLIST_INSERT_AFTER(tlp, lp, lp_entries);
685	else
686		SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
687	sc->sc_count++;
688
689	/* Update lagg capabilities */
690	lagg_capabilities(sc);
691	lagg_linkstate(sc);
692
693	/* Add multicast addresses and interface flags to this port */
694	lagg_ether_cmdmulti(lp, 1);
695	lagg_setflags(lp, 1);
696
697	if (sc->sc_port_create != NULL)
698		error = (*sc->sc_port_create)(lp);
699	if (error) {
700		/* remove the port again, without calling sc_port_destroy */
701		lagg_port_destroy(lp, 0);
702		return (error);
703	}
704
705	return (error);
706}
707
708#ifdef LAGG_PORT_STACKING
709static int
710lagg_port_checkstacking(struct lagg_softc *sc)
711{
712	struct lagg_softc *sc_ptr;
713	struct lagg_port *lp;
714	int m = 0;
715
716	LAGG_WLOCK_ASSERT(sc);
717
718	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
719		if (lp->lp_flags & LAGG_PORT_STACK) {
720			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
721			m = MAX(m, lagg_port_checkstacking(sc_ptr));
722		}
723	}
724
725	return (m + 1);
726}
727#endif
728
729static int
730lagg_port_destroy(struct lagg_port *lp, int runpd)
731{
732	struct lagg_softc *sc = lp->lp_softc;
733	struct lagg_port *lp_ptr;
734	struct lagg_llq *llq;
735	struct ifnet *ifp = lp->lp_ifp;
736
737	LAGG_WLOCK_ASSERT(sc);
738
739	if (runpd && sc->sc_port_destroy != NULL)
740		(*sc->sc_port_destroy)(lp);
741
742	/*
743	 * Remove multicast addresses and interface flags from this port and
744	 * reset the MAC address, skip if the interface is being detached.
745	 */
746	if (!lp->lp_detaching) {
747		lagg_ether_cmdmulti(lp, 0);
748		lagg_setflags(lp, 0);
749		lagg_port_lladdr(lp, lp->lp_lladdr);
750	}
751
752	/* Restore interface */
753	ifp->if_type = lp->lp_iftype;
754	ifp->if_ioctl = lp->lp_ioctl;
755	ifp->if_output = lp->lp_output;
756	ifp->if_lagg = NULL;
757
758	/* Finally, remove the port from the lagg */
759	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
760	sc->sc_count--;
761
762	/* Update the primary interface */
763	if (lp == sc->sc_primary) {
764		uint8_t lladdr[ETHER_ADDR_LEN];
765
766		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
767			bzero(&lladdr, ETHER_ADDR_LEN);
768		} else {
769			bcopy(lp_ptr->lp_lladdr,
770			    lladdr, ETHER_ADDR_LEN);
771		}
772		lagg_lladdr(sc, lladdr);
773		sc->sc_primary = lp_ptr;
774
775		/* Update link layer address for each port */
776		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
777			lagg_port_lladdr(lp_ptr, lladdr);
778	}
779
780	/* Remove any pending lladdr changes from the queue */
781	if (lp->lp_detaching) {
782		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
783			if (llq->llq_ifp == ifp) {
784				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
785				    llq_entries);
786				free(llq, M_DEVBUF);
787				break;	/* Only appears once */
788			}
789		}
790	}
791
792	if (lp->lp_ifflags)
793		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
794
795	free(lp, M_DEVBUF);
796
797	/* Update lagg capabilities */
798	lagg_capabilities(sc);
799	lagg_linkstate(sc);
800
801	return (0);
802}
803
804static int
805lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
806{
807	struct lagg_reqport *rp = (struct lagg_reqport *)data;
808	struct lagg_softc *sc;
809	struct lagg_port *lp = NULL;
810	int error = 0;
811	struct rm_priotracker tracker;
812
813	/* Should be checked by the caller */
814	if (ifp->if_type != IFT_IEEE8023ADLAG ||
815	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
816		goto fallback;
817
818	switch (cmd) {
819	case SIOCGLAGGPORT:
820		if (rp->rp_portname[0] == '\0' ||
821		    ifunit(rp->rp_portname) != ifp) {
822			error = EINVAL;
823			break;
824		}
825
826		LAGG_RLOCK(sc, &tracker);
827		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
828			error = ENOENT;
829			LAGG_RUNLOCK(sc, &tracker);
830			break;
831		}
832
833		lagg_port2req(lp, rp);
834		LAGG_RUNLOCK(sc, &tracker);
835		break;
836
837	case SIOCSIFCAP:
838		if (lp->lp_ioctl == NULL) {
839			error = EINVAL;
840			break;
841		}
842		error = (*lp->lp_ioctl)(ifp, cmd, data);
843		if (error)
844			break;
845
846		/* Update lagg interface capabilities */
847		LAGG_WLOCK(sc);
848		lagg_capabilities(sc);
849		LAGG_WUNLOCK(sc);
850		break;
851
852	case SIOCSIFMTU:
853		/* Do not allow the MTU to be changed once joined */
854		error = EINVAL;
855		break;
856
857	default:
858		goto fallback;
859	}
860
861	return (error);
862
863fallback:
864	if (lp->lp_ioctl != NULL)
865		return ((*lp->lp_ioctl)(ifp, cmd, data));
866
867	return (EINVAL);
868}
869
870/*
871 * For direct output to child ports.
872 */
873static int
874lagg_port_output(struct ifnet *ifp, struct mbuf *m,
875	const struct sockaddr *dst, struct route *ro)
876{
877	struct lagg_port *lp = ifp->if_lagg;
878
879	switch (dst->sa_family) {
880		case pseudo_AF_HDRCMPLT:
881		case AF_UNSPEC:
882			return ((*lp->lp_output)(ifp, m, dst, ro));
883	}
884
885	/* drop any other frames */
886	m_freem(m);
887	return (ENETDOWN);
888}
889
890static void
891lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
892{
893	struct lagg_port *lp;
894	struct lagg_softc *sc;
895
896	if ((lp = ifp->if_lagg) == NULL)
897		return;
898	/* If the ifnet is just being renamed, don't do anything. */
899	if (ifp->if_flags & IFF_RENAMING)
900		return;
901
902	sc = lp->lp_softc;
903
904	LAGG_WLOCK(sc);
905	lp->lp_detaching = 1;
906	lagg_port_destroy(lp, 1);
907	LAGG_WUNLOCK(sc);
908}
909
910static void
911lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
912{
913	struct lagg_softc *sc = lp->lp_softc;
914
915	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
916	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
917	rp->rp_prio = lp->lp_prio;
918	rp->rp_flags = lp->lp_flags;
919	if (sc->sc_portreq != NULL)
920		(*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
921
922	/* Add protocol specific flags */
923	switch (sc->sc_proto) {
924		case LAGG_PROTO_FAILOVER:
925			if (lp == sc->sc_primary)
926				rp->rp_flags |= LAGG_PORT_MASTER;
927			if (lp == lagg_link_active(sc, sc->sc_primary))
928				rp->rp_flags |= LAGG_PORT_ACTIVE;
929			break;
930
931		case LAGG_PROTO_ROUNDROBIN:
932		case LAGG_PROTO_LOADBALANCE:
933		case LAGG_PROTO_ETHERCHANNEL:
934			if (LAGG_PORTACTIVE(lp))
935				rp->rp_flags |= LAGG_PORT_ACTIVE;
936			break;
937
938		case LAGG_PROTO_LACP:
939			/* LACP has a different definition of active */
940			if (lacp_isactive(lp))
941				rp->rp_flags |= LAGG_PORT_ACTIVE;
942			if (lacp_iscollecting(lp))
943				rp->rp_flags |= LAGG_PORT_COLLECTING;
944			if (lacp_isdistributing(lp))
945				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
946			break;
947	}
948
949}
950
951static void
952lagg_init(void *xsc)
953{
954	struct lagg_softc *sc = (struct lagg_softc *)xsc;
955	struct lagg_port *lp;
956	struct ifnet *ifp = sc->sc_ifp;
957
958	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
959		return;
960
961	LAGG_WLOCK(sc);
962
963	ifp->if_drv_flags |= IFF_DRV_RUNNING;
964	/* Update the port lladdrs */
965	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
966		lagg_port_lladdr(lp, IF_LLADDR(ifp));
967
968	if (sc->sc_init != NULL)
969		(*sc->sc_init)(sc);
970
971	LAGG_WUNLOCK(sc);
972}
973
974static void
975lagg_stop(struct lagg_softc *sc)
976{
977	struct ifnet *ifp = sc->sc_ifp;
978
979	LAGG_WLOCK_ASSERT(sc);
980
981	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
982		return;
983
984	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
985
986	if (sc->sc_stop != NULL)
987		(*sc->sc_stop)(sc);
988}
989
990static int
991lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
992{
993	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
994	struct lagg_reqall *ra = (struct lagg_reqall *)data;
995	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
996	struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
997	struct ifreq *ifr = (struct ifreq *)data;
998	struct lagg_port *lp;
999	struct ifnet *tpif;
1000	struct thread *td = curthread;
1001	char *buf, *outbuf;
1002	int count, buflen, len, error = 0;
1003	struct rm_priotracker tracker;
1004
1005	bzero(&rpbuf, sizeof(rpbuf));
1006
1007	switch (cmd) {
1008	case SIOCGLAGG:
1009		LAGG_RLOCK(sc, &tracker);
1010		count = 0;
1011		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1012			count++;
1013		buflen = count * sizeof(struct lagg_reqport);
1014		LAGG_RUNLOCK(sc, &tracker);
1015
1016		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1017
1018		LAGG_RLOCK(sc, &tracker);
1019		ra->ra_proto = sc->sc_proto;
1020		if (sc->sc_req != NULL)
1021			(*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
1022
1023		count = 0;
1024		buf = outbuf;
1025		len = min(ra->ra_size, buflen);
1026		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1027			if (len < sizeof(rpbuf))
1028				break;
1029
1030			lagg_port2req(lp, &rpbuf);
1031			memcpy(buf, &rpbuf, sizeof(rpbuf));
1032			count++;
1033			buf += sizeof(rpbuf);
1034			len -= sizeof(rpbuf);
1035		}
1036		LAGG_RUNLOCK(sc, &tracker);
1037		ra->ra_ports = count;
1038		ra->ra_size = count * sizeof(rpbuf);
1039		error = copyout(outbuf, ra->ra_port, ra->ra_size);
1040		free(outbuf, M_TEMP);
1041		break;
1042	case SIOCSLAGG:
1043		error = priv_check(td, PRIV_NET_LAGG);
1044		if (error)
1045			break;
1046		if (ra->ra_proto >= LAGG_PROTO_MAX) {
1047			error = EPROTONOSUPPORT;
1048			break;
1049		}
1050		LAGG_WLOCK(sc);
1051		if (sc->sc_proto != LAGG_PROTO_NONE) {
1052			/* Reset protocol first in case detach unlocks */
1053			sc->sc_proto = LAGG_PROTO_NONE;
1054			error = sc->sc_detach(sc);
1055			sc->sc_detach = NULL;
1056			sc->sc_start = NULL;
1057			sc->sc_input = NULL;
1058			sc->sc_port_create = NULL;
1059			sc->sc_port_destroy = NULL;
1060			sc->sc_linkstate = NULL;
1061			sc->sc_init = NULL;
1062			sc->sc_stop = NULL;
1063			sc->sc_lladdr = NULL;
1064			sc->sc_req = NULL;
1065			sc->sc_portreq = NULL;
1066		} else if (sc->sc_input != NULL) {
1067			/* Still detaching */
1068			error = EBUSY;
1069		}
1070		if (error != 0) {
1071			LAGG_WUNLOCK(sc);
1072			break;
1073		}
1074		for (int i = 0; i < (sizeof(lagg_protos) /
1075		    sizeof(lagg_protos[0])); i++) {
1076			if (lagg_protos[i].ti_proto == ra->ra_proto) {
1077				if (sc->sc_ifflags & IFF_DEBUG)
1078					printf("%s: using proto %u\n",
1079					    sc->sc_ifname,
1080					    lagg_protos[i].ti_proto);
1081				sc->sc_proto = lagg_protos[i].ti_proto;
1082				if (sc->sc_proto != LAGG_PROTO_NONE)
1083					error = lagg_protos[i].ti_attach(sc);
1084				LAGG_WUNLOCK(sc);
1085				return (error);
1086			}
1087		}
1088		LAGG_WUNLOCK(sc);
1089		error = EPROTONOSUPPORT;
1090		break;
1091	case SIOCGLAGGFLAGS:
1092		rf->rf_flags = sc->sc_flags;
1093		break;
1094	case SIOCSLAGGHASH:
1095		error = priv_check(td, PRIV_NET_LAGG);
1096		if (error)
1097			break;
1098		if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1099			error = EINVAL;
1100			break;
1101		}
1102		LAGG_WLOCK(sc);
1103		sc->sc_flags &= ~LAGG_F_HASHMASK;
1104		sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1105		LAGG_WUNLOCK(sc);
1106		break;
1107	case SIOCGLAGGPORT:
1108		if (rp->rp_portname[0] == '\0' ||
1109		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1110			error = EINVAL;
1111			break;
1112		}
1113
1114		LAGG_RLOCK(sc, &tracker);
1115		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1116		    lp->lp_softc != sc) {
1117			error = ENOENT;
1118			LAGG_RUNLOCK(sc, &tracker);
1119			break;
1120		}
1121
1122		lagg_port2req(lp, rp);
1123		LAGG_RUNLOCK(sc, &tracker);
1124		break;
1125	case SIOCSLAGGPORT:
1126		error = priv_check(td, PRIV_NET_LAGG);
1127		if (error)
1128			break;
1129		if (rp->rp_portname[0] == '\0' ||
1130		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1131			error = EINVAL;
1132			break;
1133		}
1134		LAGG_WLOCK(sc);
1135		error = lagg_port_create(sc, tpif);
1136		LAGG_WUNLOCK(sc);
1137		break;
1138	case SIOCSLAGGDELPORT:
1139		error = priv_check(td, PRIV_NET_LAGG);
1140		if (error)
1141			break;
1142		if (rp->rp_portname[0] == '\0' ||
1143		    (tpif = ifunit(rp->rp_portname)) == NULL) {
1144			error = EINVAL;
1145			break;
1146		}
1147
1148		LAGG_WLOCK(sc);
1149		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1150		    lp->lp_softc != sc) {
1151			error = ENOENT;
1152			LAGG_WUNLOCK(sc);
1153			break;
1154		}
1155
1156		error = lagg_port_destroy(lp, 1);
1157		LAGG_WUNLOCK(sc);
1158		break;
1159	case SIOCSIFFLAGS:
1160		/* Set flags on ports too */
1161		LAGG_WLOCK(sc);
1162		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1163			lagg_setflags(lp, 1);
1164		}
1165		LAGG_WUNLOCK(sc);
1166
1167		if (!(ifp->if_flags & IFF_UP) &&
1168		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1169			/*
1170			 * If interface is marked down and it is running,
1171			 * then stop and disable it.
1172			 */
1173			LAGG_WLOCK(sc);
1174			lagg_stop(sc);
1175			LAGG_WUNLOCK(sc);
1176		} else if ((ifp->if_flags & IFF_UP) &&
1177		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1178			/*
1179			 * If interface is marked up and it is stopped, then
1180			 * start it.
1181			 */
1182			(*ifp->if_init)(sc);
1183		}
1184		break;
1185	case SIOCADDMULTI:
1186	case SIOCDELMULTI:
1187		LAGG_WLOCK(sc);
1188		error = lagg_ether_setmulti(sc);
1189		LAGG_WUNLOCK(sc);
1190		break;
1191	case SIOCSIFMEDIA:
1192	case SIOCGIFMEDIA:
1193		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1194		break;
1195
1196	case SIOCSIFCAP:
1197	case SIOCSIFMTU:
1198		/* Do not allow the MTU or caps to be directly changed */
1199		error = EINVAL;
1200		break;
1201
1202	default:
1203		error = ether_ioctl(ifp, cmd, data);
1204		break;
1205	}
1206	return (error);
1207}
1208
1209static int
1210lagg_ether_setmulti(struct lagg_softc *sc)
1211{
1212	struct lagg_port *lp;
1213
1214	LAGG_WLOCK_ASSERT(sc);
1215
1216	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1217		/* First, remove any existing filter entries. */
1218		lagg_ether_cmdmulti(lp, 0);
1219		/* copy all addresses from the lagg interface to the port */
1220		lagg_ether_cmdmulti(lp, 1);
1221	}
1222	return (0);
1223}
1224
1225static int
1226lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1227{
1228	struct lagg_softc *sc = lp->lp_softc;
1229	struct ifnet *ifp = lp->lp_ifp;
1230	struct ifnet *scifp = sc->sc_ifp;
1231	struct lagg_mc *mc;
1232	struct ifmultiaddr *ifma;
1233	int error;
1234
1235	LAGG_WLOCK_ASSERT(sc);
1236
1237	if (set) {
1238		IF_ADDR_WLOCK(scifp);
1239		TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1240			if (ifma->ifma_addr->sa_family != AF_LINK)
1241				continue;
1242			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1243			if (mc == NULL) {
1244				IF_ADDR_WUNLOCK(scifp);
1245				return (ENOMEM);
1246			}
1247			bcopy(ifma->ifma_addr, &mc->mc_addr,
1248			    ifma->ifma_addr->sa_len);
1249			mc->mc_addr.sdl_index = ifp->if_index;
1250			mc->mc_ifma = NULL;
1251			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1252		}
1253		IF_ADDR_WUNLOCK(scifp);
1254		SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1255			error = if_addmulti(ifp,
1256			    (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1257			if (error)
1258				return (error);
1259		}
1260	} else {
1261		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1262			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1263			if (mc->mc_ifma && !lp->lp_detaching)
1264				if_delmulti_ifma(mc->mc_ifma);
1265			free(mc, M_DEVBUF);
1266		}
1267	}
1268	return (0);
1269}
1270
1271/* Handle a ref counted flag that should be set on the lagg port as well */
1272static int
1273lagg_setflag(struct lagg_port *lp, int flag, int status,
1274	     int (*func)(struct ifnet *, int))
1275{
1276	struct lagg_softc *sc = lp->lp_softc;
1277	struct ifnet *scifp = sc->sc_ifp;
1278	struct ifnet *ifp = lp->lp_ifp;
1279	int error;
1280
1281	LAGG_WLOCK_ASSERT(sc);
1282
1283	status = status ? (scifp->if_flags & flag) : 0;
1284	/* Now "status" contains the flag value or 0 */
1285
1286	/*
1287	 * See if recorded ports status is different from what
1288	 * we want it to be.  If it is, flip it.  We record ports
1289	 * status in lp_ifflags so that we won't clear ports flag
1290	 * we haven't set.  In fact, we don't clear or set ports
1291	 * flags directly, but get or release references to them.
1292	 * That's why we can be sure that recorded flags still are
1293	 * in accord with actual ports flags.
1294	 */
1295	if (status != (lp->lp_ifflags & flag)) {
1296		error = (*func)(ifp, status);
1297		if (error)
1298			return (error);
1299		lp->lp_ifflags &= ~flag;
1300		lp->lp_ifflags |= status;
1301	}
1302	return (0);
1303}
1304
1305/*
1306 * Handle IFF_* flags that require certain changes on the lagg port
1307 * if "status" is true, update ports flags respective to the lagg
1308 * if "status" is false, forcedly clear the flags set on port.
1309 */
1310static int
1311lagg_setflags(struct lagg_port *lp, int status)
1312{
1313	int error, i;
1314
1315	for (i = 0; lagg_pflags[i].flag; i++) {
1316		error = lagg_setflag(lp, lagg_pflags[i].flag,
1317		    status, lagg_pflags[i].func);
1318		if (error)
1319			return (error);
1320	}
1321	return (0);
1322}
1323
1324static int
1325lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1326{
1327	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1328	int error, len, mcast;
1329	struct rm_priotracker tracker;
1330
1331	len = m->m_pkthdr.len;
1332	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1333
1334	LAGG_RLOCK(sc, &tracker);
1335	/* We need a Tx algorithm and at least one port */
1336	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1337		LAGG_RUNLOCK(sc, &tracker);
1338		m_freem(m);
1339		ifp->if_oerrors++;
1340		return (ENXIO);
1341	}
1342
1343	ETHER_BPF_MTAP(ifp, m);
1344
1345	error = (*sc->sc_start)(sc, m);
1346	LAGG_RUNLOCK(sc, &tracker);
1347
1348	if (error == 0) {
1349		counter_u64_add(sc->sc_opackets, 1);
1350		counter_u64_add(sc->sc_obytes, len);
1351		ifp->if_omcasts += mcast;
1352	} else
1353		ifp->if_oerrors++;
1354
1355	return (error);
1356}
1357
1358/*
1359 * The ifp->if_qflush entry point for lagg(4) is no-op.
1360 */
1361static void
1362lagg_qflush(struct ifnet *ifp __unused)
1363{
1364}
1365
1366static struct mbuf *
1367lagg_input(struct ifnet *ifp, struct mbuf *m)
1368{
1369	struct lagg_port *lp = ifp->if_lagg;
1370	struct lagg_softc *sc = lp->lp_softc;
1371	struct ifnet *scifp = sc->sc_ifp;
1372	struct rm_priotracker tracker;
1373
1374	LAGG_RLOCK(sc, &tracker);
1375	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1376	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1377	    sc->sc_proto == LAGG_PROTO_NONE) {
1378		LAGG_RUNLOCK(sc, &tracker);
1379		m_freem(m);
1380		return (NULL);
1381	}
1382
1383	ETHER_BPF_MTAP(scifp, m);
1384
1385	m = (*sc->sc_input)(sc, lp, m);
1386
1387	if (m != NULL) {
1388		counter_u64_add(sc->sc_ipackets, 1);
1389		counter_u64_add(sc->sc_ibytes, m->m_pkthdr.len);
1390
1391		if (scifp->if_flags & IFF_MONITOR) {
1392			m_freem(m);
1393			m = NULL;
1394		}
1395	}
1396
1397	LAGG_RUNLOCK(sc, &tracker);
1398	return (m);
1399}
1400
1401static int
1402lagg_media_change(struct ifnet *ifp)
1403{
1404	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1405
1406	if (sc->sc_ifflags & IFF_DEBUG)
1407		printf("%s\n", __func__);
1408
1409	/* Ignore */
1410	return (0);
1411}
1412
1413static void
1414lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1415{
1416	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1417	struct lagg_port *lp;
1418	struct rm_priotracker tracker;
1419
1420	imr->ifm_status = IFM_AVALID;
1421	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1422
1423	LAGG_RLOCK(sc, &tracker);
1424	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1425		if (LAGG_PORTACTIVE(lp))
1426			imr->ifm_status |= IFM_ACTIVE;
1427	}
1428	LAGG_RUNLOCK(sc, &tracker);
1429}
1430
1431static void
1432lagg_linkstate(struct lagg_softc *sc)
1433{
1434	struct lagg_port *lp;
1435	int new_link = LINK_STATE_DOWN;
1436	uint64_t speed;
1437
1438	/* Our link is considered up if at least one of our ports is active */
1439	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1440		if (lp->lp_link_state == LINK_STATE_UP) {
1441			new_link = LINK_STATE_UP;
1442			break;
1443		}
1444	}
1445	if_link_state_change(sc->sc_ifp, new_link);
1446
1447	/* Update if_baudrate to reflect the max possible speed */
1448	switch (sc->sc_proto) {
1449		case LAGG_PROTO_FAILOVER:
1450			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1451			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1452			break;
1453		case LAGG_PROTO_ROUNDROBIN:
1454		case LAGG_PROTO_LOADBALANCE:
1455		case LAGG_PROTO_ETHERCHANNEL:
1456			speed = 0;
1457			SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1458				speed += lp->lp_ifp->if_baudrate;
1459			sc->sc_ifp->if_baudrate = speed;
1460			break;
1461		case LAGG_PROTO_LACP:
1462			/* LACP updates if_baudrate itself */
1463			break;
1464	}
1465}
1466
1467static void
1468lagg_port_state(struct ifnet *ifp, int state)
1469{
1470	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1471	struct lagg_softc *sc = NULL;
1472
1473	if (lp != NULL)
1474		sc = lp->lp_softc;
1475	if (sc == NULL)
1476		return;
1477
1478	LAGG_WLOCK(sc);
1479	lagg_linkstate(sc);
1480	if (sc->sc_linkstate != NULL)
1481		(*sc->sc_linkstate)(lp);
1482	LAGG_WUNLOCK(sc);
1483}
1484
1485struct lagg_port *
1486lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1487{
1488	struct lagg_port *lp_next, *rval = NULL;
1489	// int new_link = LINK_STATE_DOWN;
1490
1491	LAGG_RLOCK_ASSERT(sc);
1492	/*
1493	 * Search a port which reports an active link state.
1494	 */
1495
1496	if (lp == NULL)
1497		goto search;
1498	if (LAGG_PORTACTIVE(lp)) {
1499		rval = lp;
1500		goto found;
1501	}
1502	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1503	    LAGG_PORTACTIVE(lp_next)) {
1504		rval = lp_next;
1505		goto found;
1506	}
1507
1508search:
1509	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1510		if (LAGG_PORTACTIVE(lp_next)) {
1511			rval = lp_next;
1512			goto found;
1513		}
1514	}
1515
1516found:
1517	if (rval != NULL) {
1518		/*
1519		 * The IEEE 802.1D standard assumes that a lagg with
1520		 * multiple ports is always full duplex. This is valid
1521		 * for load sharing laggs and if at least two links
1522		 * are active. Unfortunately, checking the latter would
1523		 * be too expensive at this point.
1524		 XXX
1525		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1526		    (sc->sc_count > 1))
1527			new_link = LINK_STATE_FULL_DUPLEX;
1528		else
1529			new_link = rval->lp_link_state;
1530		 */
1531	}
1532
1533	return (rval);
1534}
1535
1536static const void *
1537lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1538{
1539	if (m->m_pkthdr.len < (off + len)) {
1540		return (NULL);
1541	} else if (m->m_len < (off + len)) {
1542		m_copydata(m, off, len, buf);
1543		return (buf);
1544	}
1545	return (mtod(m, char *) + off);
1546}
1547
1548static int
1549lagg_sysctl_active(SYSCTL_HANDLER_ARGS)
1550{
1551	struct lagg_softc *sc = (struct lagg_softc *)arg1;
1552	struct lagg_port *lp;
1553	int error;
1554
1555	/* LACP tracks active links automatically, the others do not */
1556	if (sc->sc_proto != LAGG_PROTO_LACP) {
1557		sc->sc_active = 0;
1558		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1559			sc->sc_active += LAGG_PORTACTIVE(lp);
1560	}
1561
1562	error = sysctl_handle_int(oidp, &sc->sc_active, 0, req);
1563	if ((error) || (req->newptr == NULL))
1564		return (error);
1565
1566	return (0);
1567}
1568
1569uint32_t
1570lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1571{
1572	uint16_t etype;
1573	uint32_t p = key;
1574	int off;
1575	struct ether_header *eh;
1576	const struct ether_vlan_header *vlan;
1577#ifdef INET
1578	const struct ip *ip;
1579	const uint32_t *ports;
1580	int iphlen;
1581#endif
1582#ifdef INET6
1583	const struct ip6_hdr *ip6;
1584	uint32_t flow;
1585#endif
1586	union {
1587#ifdef INET
1588		struct ip ip;
1589#endif
1590#ifdef INET6
1591		struct ip6_hdr ip6;
1592#endif
1593		struct ether_vlan_header vlan;
1594		uint32_t port;
1595	} buf;
1596
1597
1598	off = sizeof(*eh);
1599	if (m->m_len < off)
1600		goto out;
1601	eh = mtod(m, struct ether_header *);
1602	etype = ntohs(eh->ether_type);
1603	if (sc->sc_flags & LAGG_F_HASHL2) {
1604		p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1605		p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1606	}
1607
1608	/* Special handling for encapsulating VLAN frames */
1609	if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1610		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1611		    sizeof(m->m_pkthdr.ether_vtag), p);
1612	} else if (etype == ETHERTYPE_VLAN) {
1613		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &buf);
1614		if (vlan == NULL)
1615			goto out;
1616
1617		if (sc->sc_flags & LAGG_F_HASHL2)
1618			p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1619		etype = ntohs(vlan->evl_proto);
1620		off += sizeof(*vlan) - sizeof(*eh);
1621	}
1622
1623	switch (etype) {
1624#ifdef INET
1625	case ETHERTYPE_IP:
1626		ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1627		if (ip == NULL)
1628			goto out;
1629
1630		if (sc->sc_flags & LAGG_F_HASHL3) {
1631			p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1632			p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1633		}
1634		if (!(sc->sc_flags & LAGG_F_HASHL4))
1635			break;
1636		switch (ip->ip_p) {
1637			case IPPROTO_TCP:
1638			case IPPROTO_UDP:
1639			case IPPROTO_SCTP:
1640				iphlen = ip->ip_hl << 2;
1641				if (iphlen < sizeof(*ip))
1642					break;
1643				off += iphlen;
1644				ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1645				if (ports == NULL)
1646					break;
1647				p = hash32_buf(ports, sizeof(*ports), p);
1648				break;
1649		}
1650		break;
1651#endif
1652#ifdef INET6
1653	case ETHERTYPE_IPV6:
1654		if (!(sc->sc_flags & LAGG_F_HASHL3))
1655			break;
1656		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1657		if (ip6 == NULL)
1658			goto out;
1659
1660		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1661		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1662		flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1663		p = hash32_buf(&flow, sizeof(flow), p);	/* IPv6 flow label */
1664		break;
1665#endif
1666	}
1667out:
1668	return (p);
1669}
1670
1671int
1672lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1673{
1674
1675	return (ifp->if_transmit)(ifp, m);
1676}
1677
1678/*
1679 * Simple round robin aggregation
1680 */
1681
1682static int
1683lagg_rr_attach(struct lagg_softc *sc)
1684{
1685	sc->sc_detach = lagg_rr_detach;
1686	sc->sc_start = lagg_rr_start;
1687	sc->sc_input = lagg_rr_input;
1688	sc->sc_port_create = NULL;
1689	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1690	sc->sc_seq = 0;
1691
1692	return (0);
1693}
1694
1695static int
1696lagg_rr_detach(struct lagg_softc *sc)
1697{
1698	return (0);
1699}
1700
1701static int
1702lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1703{
1704	struct lagg_port *lp;
1705	uint32_t p;
1706
1707	p = atomic_fetchadd_32(&sc->sc_seq, 1);
1708	p %= sc->sc_count;
1709	lp = SLIST_FIRST(&sc->sc_ports);
1710	while (p--)
1711		lp = SLIST_NEXT(lp, lp_entries);
1712
1713	/*
1714	 * Check the port's link state. This will return the next active
1715	 * port if the link is down or the port is NULL.
1716	 */
1717	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1718		m_freem(m);
1719		return (ENETDOWN);
1720	}
1721
1722	/* Send mbuf */
1723	return (lagg_enqueue(lp->lp_ifp, m));
1724}
1725
1726static struct mbuf *
1727lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1728{
1729	struct ifnet *ifp = sc->sc_ifp;
1730
1731	/* Just pass in the packet to our lagg device */
1732	m->m_pkthdr.rcvif = ifp;
1733
1734	return (m);
1735}
1736
1737/*
1738 * Active failover
1739 */
1740
1741static int
1742lagg_fail_attach(struct lagg_softc *sc)
1743{
1744	sc->sc_detach = lagg_fail_detach;
1745	sc->sc_start = lagg_fail_start;
1746	sc->sc_input = lagg_fail_input;
1747	sc->sc_port_create = NULL;
1748	sc->sc_port_destroy = NULL;
1749
1750	return (0);
1751}
1752
1753static int
1754lagg_fail_detach(struct lagg_softc *sc)
1755{
1756	return (0);
1757}
1758
1759static int
1760lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1761{
1762	struct lagg_port *lp;
1763
1764	/* Use the master port if active or the next available port */
1765	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1766		m_freem(m);
1767		return (ENETDOWN);
1768	}
1769
1770	/* Send mbuf */
1771	return (lagg_enqueue(lp->lp_ifp, m));
1772}
1773
1774static struct mbuf *
1775lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1776{
1777	struct ifnet *ifp = sc->sc_ifp;
1778	struct lagg_port *tmp_tp;
1779
1780	if (lp == sc->sc_primary || lagg_failover_rx_all) {
1781		m->m_pkthdr.rcvif = ifp;
1782		return (m);
1783	}
1784
1785	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1786		tmp_tp = lagg_link_active(sc, sc->sc_primary);
1787		/*
1788		 * If tmp_tp is null, we've recieved a packet when all
1789		 * our links are down. Weird, but process it anyways.
1790		 */
1791		if ((tmp_tp == NULL || tmp_tp == lp)) {
1792			m->m_pkthdr.rcvif = ifp;
1793			return (m);
1794		}
1795	}
1796
1797	m_freem(m);
1798	return (NULL);
1799}
1800
1801/*
1802 * Loadbalancing
1803 */
1804
1805static int
1806lagg_lb_attach(struct lagg_softc *sc)
1807{
1808	struct lagg_port *lp;
1809	struct lagg_lb *lb;
1810
1811	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1812	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1813		return (ENOMEM);
1814
1815	sc->sc_detach = lagg_lb_detach;
1816	sc->sc_start = lagg_lb_start;
1817	sc->sc_input = lagg_lb_input;
1818	sc->sc_port_create = lagg_lb_port_create;
1819	sc->sc_port_destroy = lagg_lb_port_destroy;
1820	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1821
1822	lb->lb_key = arc4random();
1823	sc->sc_psc = (caddr_t)lb;
1824
1825	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1826		lagg_lb_port_create(lp);
1827
1828	return (0);
1829}
1830
1831static int
1832lagg_lb_detach(struct lagg_softc *sc)
1833{
1834	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1835	if (lb != NULL)
1836		free(lb, M_DEVBUF);
1837	return (0);
1838}
1839
1840static int
1841lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1842{
1843	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1844	struct lagg_port *lp_next;
1845	int i = 0;
1846
1847	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1848	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1849		if (lp_next == lp)
1850			continue;
1851		if (i >= LAGG_MAX_PORTS)
1852			return (EINVAL);
1853		if (sc->sc_ifflags & IFF_DEBUG)
1854			printf("%s: port %s at index %d\n",
1855			    sc->sc_ifname, lp_next->lp_ifname, i);
1856		lb->lb_ports[i++] = lp_next;
1857	}
1858
1859	return (0);
1860}
1861
1862static int
1863lagg_lb_port_create(struct lagg_port *lp)
1864{
1865	struct lagg_softc *sc = lp->lp_softc;
1866	return (lagg_lb_porttable(sc, NULL));
1867}
1868
1869static void
1870lagg_lb_port_destroy(struct lagg_port *lp)
1871{
1872	struct lagg_softc *sc = lp->lp_softc;
1873	lagg_lb_porttable(sc, lp);
1874}
1875
1876static int
1877lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1878{
1879	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1880	struct lagg_port *lp = NULL;
1881	uint32_t p = 0;
1882
1883	if (sc->use_flowid && (m->m_flags & M_FLOWID))
1884		p = m->m_pkthdr.flowid >> sc->flowid_shift;
1885	else
1886		p = lagg_hashmbuf(sc, m, lb->lb_key);
1887	p %= sc->sc_count;
1888	lp = lb->lb_ports[p];
1889
1890	/*
1891	 * Check the port's link state. This will return the next active
1892	 * port if the link is down or the port is NULL.
1893	 */
1894	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1895		m_freem(m);
1896		return (ENETDOWN);
1897	}
1898
1899	/* Send mbuf */
1900	return (lagg_enqueue(lp->lp_ifp, m));
1901}
1902
1903static struct mbuf *
1904lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1905{
1906	struct ifnet *ifp = sc->sc_ifp;
1907
1908	/* Just pass in the packet to our lagg device */
1909	m->m_pkthdr.rcvif = ifp;
1910
1911	return (m);
1912}
1913
1914/*
1915 * 802.3ad LACP
1916 */
1917
1918static int
1919lagg_lacp_attach(struct lagg_softc *sc)
1920{
1921	struct lagg_port *lp;
1922	int error;
1923
1924	sc->sc_detach = lagg_lacp_detach;
1925	sc->sc_port_create = lacp_port_create;
1926	sc->sc_port_destroy = lacp_port_destroy;
1927	sc->sc_linkstate = lacp_linkstate;
1928	sc->sc_start = lagg_lacp_start;
1929	sc->sc_input = lagg_lacp_input;
1930	sc->sc_init = lacp_init;
1931	sc->sc_stop = lacp_stop;
1932	sc->sc_lladdr = lagg_lacp_lladdr;
1933	sc->sc_req = lacp_req;
1934	sc->sc_portreq = lacp_portreq;
1935
1936	error = lacp_attach(sc);
1937	if (error)
1938		return (error);
1939
1940	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1941		lacp_port_create(lp);
1942
1943	return (error);
1944}
1945
1946static int
1947lagg_lacp_detach(struct lagg_softc *sc)
1948{
1949	struct lagg_port *lp;
1950	int error;
1951
1952	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1953		lacp_port_destroy(lp);
1954
1955	/* unlocking is safe here */
1956	LAGG_WUNLOCK(sc);
1957	error = lacp_detach(sc);
1958	LAGG_WLOCK(sc);
1959
1960	return (error);
1961}
1962
1963static void
1964lagg_lacp_lladdr(struct lagg_softc *sc)
1965{
1966	struct lagg_port *lp;
1967
1968	/* purge all the lacp ports */
1969	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1970		lacp_port_destroy(lp);
1971
1972	/* add them back in */
1973	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1974		lacp_port_create(lp);
1975}
1976
1977static int
1978lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1979{
1980	struct lagg_port *lp;
1981
1982	lp = lacp_select_tx_port(sc, m);
1983	if (lp == NULL) {
1984		m_freem(m);
1985		return (ENETDOWN);
1986	}
1987
1988	/* Send mbuf */
1989	return (lagg_enqueue(lp->lp_ifp, m));
1990}
1991
1992static struct mbuf *
1993lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1994{
1995	struct ifnet *ifp = sc->sc_ifp;
1996	struct ether_header *eh;
1997	u_short etype;
1998
1999	eh = mtod(m, struct ether_header *);
2000	etype = ntohs(eh->ether_type);
2001
2002	/* Tap off LACP control messages */
2003	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2004		m = lacp_input(lp, m);
2005		if (m == NULL)
2006			return (NULL);
2007	}
2008
2009	/*
2010	 * If the port is not collecting or not in the active aggregator then
2011	 * free and return.
2012	 */
2013	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2014		m_freem(m);
2015		return (NULL);
2016	}
2017
2018	m->m_pkthdr.rcvif = ifp;
2019	return (m);
2020}
2021
2022static void
2023lagg_callout(void *arg)
2024{
2025	struct lagg_softc *sc = (struct lagg_softc *)arg;
2026	struct ifnet *ifp = sc->sc_ifp;
2027
2028	ifp->if_ipackets = counter_u64_fetch(sc->sc_ipackets);
2029	ifp->if_opackets = counter_u64_fetch(sc->sc_opackets);
2030	ifp->if_ibytes = counter_u64_fetch(sc->sc_ibytes);
2031	ifp->if_obytes = counter_u64_fetch(sc->sc_obytes);
2032
2033	callout_reset(&sc->sc_callout, hz, lagg_callout, sc);
2034}
2035