if_lagg.c revision 169569
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 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/cdefs.h>
20__FBSDID("$FreeBSD: head/sys/net/if_lagg.c 169569 2007-05-15 07:41:46Z thompsa $");
21
22#include "opt_inet.h"
23#include "opt_inet6.h"
24
25#include <sys/param.h>
26#include <sys/kernel.h>
27#include <sys/malloc.h>
28#include <sys/mbuf.h>
29#include <sys/queue.h>
30#include <sys/socket.h>
31#include <sys/sockio.h>
32#include <sys/sysctl.h>
33#include <sys/module.h>
34#include <sys/priv.h>
35#include <sys/systm.h>
36#include <sys/proc.h>
37#include <sys/hash.h>
38#include <sys/lock.h>
39#include <sys/rwlock.h>
40#include <sys/taskqueue.h>
41
42#include <net/ethernet.h>
43#include <net/if.h>
44#include <net/if_clone.h>
45#include <net/if_arp.h>
46#include <net/if_dl.h>
47#include <net/if_llc.h>
48#include <net/if_media.h>
49#include <net/if_types.h>
50#include <net/if_var.h>
51#include <net/bpf.h>
52
53#ifdef INET
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/if_ether.h>
57#include <netinet/ip.h>
58#endif
59
60#ifdef INET6
61#include <netinet/ip6.h>
62#endif
63
64#include <net/if_vlan_var.h>
65#include <net/if_lagg.h>
66#include <net/ieee8023ad_lacp.h>
67
68/* Special flags we should propagate to the lagg ports. */
69static struct {
70	int flag;
71	int (*func)(struct ifnet *, int);
72} lagg_pflags[] = {
73	{IFF_PROMISC, ifpromisc},
74	{IFF_ALLMULTI, if_allmulti},
75	{0, NULL}
76};
77
78SLIST_HEAD(__trhead, lagg_softc) lagg_list;	/* list of laggs */
79static struct mtx 	lagg_list_mtx;
80eventhandler_tag	lagg_detach_cookie = NULL;
81
82static int	lagg_clone_create(struct if_clone *, int, caddr_t);
83static void	lagg_clone_destroy(struct ifnet *);
84static void	lagg_lladdr(struct lagg_softc *, uint8_t *);
85static int	lagg_capabilities(struct lagg_softc *);
86static void	lagg_port_lladdr(struct lagg_port *, uint8_t *);
87static void	lagg_port_setlladdr(void *, int);
88static int	lagg_port_create(struct lagg_softc *, struct ifnet *);
89static int	lagg_port_destroy(struct lagg_port *, int);
90static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
91static void	lagg_port_state(struct ifnet *, int);
92static int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
93static int	lagg_port_output(struct ifnet *, struct mbuf *,
94		    struct sockaddr *, struct rtentry *);
95static void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
96static int	lagg_port_checkstacking(struct lagg_softc *);
97static void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
98static void	lagg_init(void *);
99static void	lagg_stop(struct lagg_softc *);
100static int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
101static int	lagg_ether_setmulti(struct lagg_softc *);
102static int	lagg_ether_cmdmulti(struct lagg_port *, int);
103static	int	lagg_setflag(struct lagg_port *, int, int,
104		    int (*func)(struct ifnet *, int));
105static	int	lagg_setflags(struct lagg_port *, int status);
106static void	lagg_start(struct ifnet *);
107static int	lagg_media_change(struct ifnet *);
108static void	lagg_media_status(struct ifnet *, struct ifmediareq *);
109static struct lagg_port *lagg_link_active(struct lagg_softc *,
110	    struct lagg_port *);
111static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
112
113IFC_SIMPLE_DECLARE(lagg, 0);
114
115/* Simple round robin */
116static int	lagg_rr_attach(struct lagg_softc *);
117static int	lagg_rr_detach(struct lagg_softc *);
118static void	lagg_rr_port_destroy(struct lagg_port *);
119static int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
120static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
121		    struct mbuf *);
122
123/* Active failover */
124static int	lagg_fail_attach(struct lagg_softc *);
125static int	lagg_fail_detach(struct lagg_softc *);
126static int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
127static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
128		    struct mbuf *);
129
130/* Loadbalancing */
131static int	lagg_lb_attach(struct lagg_softc *);
132static int	lagg_lb_detach(struct lagg_softc *);
133static int	lagg_lb_port_create(struct lagg_port *);
134static void	lagg_lb_port_destroy(struct lagg_port *);
135static int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
136static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
137		    struct mbuf *);
138static int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
139
140/* 802.3ad LACP */
141static int	lagg_lacp_attach(struct lagg_softc *);
142static int	lagg_lacp_detach(struct lagg_softc *);
143static int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
144static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
145		    struct mbuf *);
146static void	lagg_lacp_lladdr(struct lagg_softc *);
147
148/* lagg protocol table */
149static const struct {
150	int			ti_proto;
151	int			(*ti_attach)(struct lagg_softc *);
152} lagg_protos[] = {
153	{ LAGG_PROTO_ROUNDROBIN,	lagg_rr_attach },
154	{ LAGG_PROTO_FAILOVER,		lagg_fail_attach },
155	{ LAGG_PROTO_LOADBALANCE,	lagg_lb_attach },
156	{ LAGG_PROTO_ETHERCHANNEL,	lagg_lb_attach },
157	{ LAGG_PROTO_LACP,		lagg_lacp_attach },
158	{ LAGG_PROTO_NONE,		NULL }
159};
160
161static int
162lagg_modevent(module_t mod, int type, void *data)
163{
164
165	switch (type) {
166	case MOD_LOAD:
167		mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
168		SLIST_INIT(&lagg_list);
169		if_clone_attach(&lagg_cloner);
170		lagg_input_p = lagg_input;
171		lagg_linkstate_p = lagg_port_state;
172		lagg_detach_cookie = EVENTHANDLER_REGISTER(
173		    ifnet_departure_event, lagg_port_ifdetach, NULL,
174		    EVENTHANDLER_PRI_ANY);
175		break;
176	case MOD_UNLOAD:
177		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
178		    lagg_detach_cookie);
179		if_clone_detach(&lagg_cloner);
180		lagg_input_p = NULL;
181		lagg_linkstate_p = NULL;
182		mtx_destroy(&lagg_list_mtx);
183		break;
184	default:
185		return (EOPNOTSUPP);
186	}
187	return (0);
188}
189
190static moduledata_t lagg_mod = {
191	"if_lagg",
192	lagg_modevent,
193	0
194};
195
196DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
197
198static int
199lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
200{
201	struct lagg_softc *sc;
202	struct ifnet *ifp;
203	int i, error = 0;
204	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
205
206	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
207	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
208	if (ifp == NULL) {
209		free(sc, M_DEVBUF);
210		return (ENOSPC);
211	}
212
213	sc->sc_proto = LAGG_PROTO_NONE;
214	for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
215		if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
216			sc->sc_proto = lagg_protos[i].ti_proto;
217			if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
218				if_free_type(ifp, IFT_ETHER);
219				free(sc, M_DEVBUF);
220				return (error);
221			}
222			break;
223		}
224	}
225	LAGG_LOCK_INIT(sc);
226	SLIST_INIT(&sc->sc_ports);
227	TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
228
229	/* Initialise pseudo media types */
230	ifmedia_init(&sc->sc_media, 0, lagg_media_change,
231	    lagg_media_status);
232	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
233	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
234
235	if_initname(ifp, ifc->ifc_name, unit);
236	ifp->if_type = IFT_ETHER;
237	ifp->if_softc = sc;
238	ifp->if_start = lagg_start;
239	ifp->if_init = lagg_init;
240	ifp->if_ioctl = lagg_ioctl;
241	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
242
243	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
244	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
245	IFQ_SET_READY(&ifp->if_snd);
246
247	/*
248	 * Attach as an ordinary ethernet device, childs will be attached
249	 * as special device IFT_IEEE8023ADLAG.
250	 */
251	ether_ifattach(ifp, eaddr);
252
253	/* Insert into the global list of laggs */
254	mtx_lock(&lagg_list_mtx);
255	SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
256	mtx_unlock(&lagg_list_mtx);
257
258	return (0);
259}
260
261static void
262lagg_clone_destroy(struct ifnet *ifp)
263{
264	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
265	struct lagg_port *lp;
266
267	LAGG_WLOCK(sc);
268
269	lagg_stop(sc);
270	ifp->if_flags &= ~IFF_UP;
271
272	/* Shutdown and remove lagg ports */
273	while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
274		lagg_port_destroy(lp, 1);
275	/* Unhook the aggregation protocol */
276	if (sc->sc_detach != NULL)
277		(*sc->sc_detach)(sc);
278
279	LAGG_WUNLOCK(sc);
280
281	ifmedia_removeall(&sc->sc_media);
282	ether_ifdetach(ifp);
283	if_free_type(ifp, IFT_ETHER);
284
285	mtx_lock(&lagg_list_mtx);
286	SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
287	mtx_unlock(&lagg_list_mtx);
288
289	taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
290	LAGG_LOCK_DESTROY(sc);
291	free(sc, M_DEVBUF);
292}
293
294static void
295lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
296{
297	struct ifnet *ifp = sc->sc_ifp;
298
299	if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
300		return;
301
302	bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
303	/* Let the protocol know the MAC has changed */
304	if (sc->sc_lladdr != NULL)
305		(*sc->sc_lladdr)(sc);
306}
307
308static int
309lagg_capabilities(struct lagg_softc *sc)
310{
311	struct lagg_port *lp;
312	int cap = ~0, priv;
313
314	LAGG_WLOCK_ASSERT(sc);
315
316	/* Preserve private capabilities */
317	priv = sc->sc_capabilities & IFCAP_LAGG_MASK;
318
319	/* Get capabilities from the lagg ports */
320	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
321		cap &= lp->lp_capabilities;
322
323	if (sc->sc_ifflags & IFF_DEBUG) {
324		printf("%s: capabilities 0x%08x\n",
325		    sc->sc_ifname, cap == ~0 ? priv : (cap | priv));
326	}
327
328	return (cap == ~0 ? priv : (cap | priv));
329}
330
331static void
332lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
333{
334	struct lagg_softc *sc = lp->lp_lagg;
335	struct ifnet *ifp = lp->lp_ifp;
336	struct lagg_llq *llq;
337	int pending = 0;
338
339	LAGG_WLOCK_ASSERT(sc);
340
341	if (lp->lp_detaching ||
342	    memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
343		return;
344
345	/* Check to make sure its not already queued to be changed */
346	SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
347		if (llq->llq_ifp == ifp) {
348			pending = 1;
349			break;
350		}
351	}
352
353	if (!pending) {
354		llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
355		if (llq == NULL)	/* XXX what to do */
356			return;
357	}
358
359	/* Update the lladdr even if pending, it may have changed */
360	llq->llq_ifp = ifp;
361	bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
362
363	if (!pending)
364		SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
365
366	taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
367}
368
369/*
370 * Set the interface MAC address from a taskqueue to avoid a LOR.
371 */
372static void
373lagg_port_setlladdr(void *arg, int pending)
374{
375	struct lagg_softc *sc = (struct lagg_softc *)arg;
376	struct lagg_llq *llq, *head;
377	struct ifnet *ifp;
378	int error;
379
380	/* Grab a local reference of the queue and remove it from the softc */
381	LAGG_WLOCK(sc);
382	head = SLIST_FIRST(&sc->sc_llq_head);
383	SLIST_FIRST(&sc->sc_llq_head) = NULL;
384	LAGG_WUNLOCK(sc);
385
386	/*
387	 * Traverse the queue and set the lladdr on each ifp. It is safe to do
388	 * unlocked as we have the only reference to it.
389	 */
390	for (llq = head; llq != NULL; llq = head) {
391		ifp = llq->llq_ifp;
392
393		/* Set the link layer address */
394		error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
395		if (error)
396			printf("%s: setlladdr failed on %s\n", __func__,
397			    ifp->if_xname);
398
399		head = SLIST_NEXT(llq, llq_entries);
400		free(llq, M_DEVBUF);
401	}
402}
403
404static int
405lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
406{
407	struct lagg_softc *sc_ptr;
408	struct lagg_port *lp;
409	int error = 0;
410
411	LAGG_WLOCK_ASSERT(sc);
412
413	/* Limit the maximal number of lagg ports */
414	if (sc->sc_count >= LAGG_MAX_PORTS)
415		return (ENOSPC);
416
417	/* New lagg port has to be in an idle state */
418	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
419		return (EBUSY);
420
421	/* Check if port has already been associated to a lagg */
422	if (ifp->if_lagg != NULL)
423		return (EBUSY);
424
425	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
426	if (ifp->if_type != IFT_ETHER)
427		return (EPROTONOSUPPORT);
428
429	if ((lp = malloc(sizeof(struct lagg_port),
430	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
431		return (ENOMEM);
432
433	/* Check if port is a stacked lagg */
434	mtx_lock(&lagg_list_mtx);
435	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
436		if (ifp == sc_ptr->sc_ifp) {
437			mtx_unlock(&lagg_list_mtx);
438			free(lp, M_DEVBUF);
439			return (EINVAL);
440			/* XXX disable stacking for the moment, its untested
441			lp->lp_flags |= LAGG_PORT_STACK;
442			if (lagg_port_checkstacking(sc_ptr) >=
443			    LAGG_MAX_STACKING) {
444				mtx_unlock(&lagg_list_mtx);
445				free(lp, M_DEVBUF);
446				return (E2BIG);
447			}
448			*/
449		}
450	}
451	mtx_unlock(&lagg_list_mtx);
452
453	/* Change the interface type */
454	lp->lp_iftype = ifp->if_type;
455	ifp->if_type = IFT_IEEE8023ADLAG;
456	ifp->if_lagg = lp;
457	lp->lp_ioctl = ifp->if_ioctl;
458	ifp->if_ioctl = lagg_port_ioctl;
459	lp->lp_output = ifp->if_output;
460	ifp->if_output = lagg_port_output;
461
462	lp->lp_ifp = ifp;
463	lp->lp_lagg = sc;
464
465	/* Save port link layer address */
466	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
467
468	if (SLIST_EMPTY(&sc->sc_ports)) {
469		sc->sc_primary = lp;
470		lagg_lladdr(sc, IF_LLADDR(ifp));
471	} else {
472		/* Update link layer address for this port */
473		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
474	}
475
476	/* Insert into the list of ports */
477	SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
478	sc->sc_count++;
479
480	/* Update lagg capabilities */
481	sc->sc_capabilities = lagg_capabilities(sc);
482
483	/* Add multicast addresses and interface flags to this port */
484	lagg_ether_cmdmulti(lp, 1);
485	lagg_setflags(lp, 1);
486
487	if (sc->sc_port_create != NULL)
488		error = (*sc->sc_port_create)(lp);
489	if (error) {
490		/* remove the port again, without calling sc_port_destroy */
491		lagg_port_destroy(lp, 0);
492		return (error);
493	}
494
495	return (error);
496}
497
498static int
499lagg_port_checkstacking(struct lagg_softc *sc)
500{
501	struct lagg_softc *sc_ptr;
502	struct lagg_port *lp;
503	int m = 0;
504
505	LAGG_WLOCK_ASSERT(sc);
506
507	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
508		if (lp->lp_flags & LAGG_PORT_STACK) {
509			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
510			m = MAX(m, lagg_port_checkstacking(sc_ptr));
511		}
512	}
513
514	return (m + 1);
515}
516
517static int
518lagg_port_destroy(struct lagg_port *lp, int runpd)
519{
520	struct lagg_softc *sc = lp->lp_lagg;
521	struct lagg_port *lp_ptr;
522	struct lagg_llq *llq;
523	struct ifnet *ifp = lp->lp_ifp;
524
525	LAGG_WLOCK_ASSERT(sc);
526
527	if (runpd && sc->sc_port_destroy != NULL)
528		(*sc->sc_port_destroy)(lp);
529
530	/*
531	 * Remove multicast addresses and interface flags from this port and
532	 * reset the MAC address, skip if the interface is being detached.
533	 */
534	if (!lp->lp_detaching) {
535		lagg_ether_cmdmulti(lp, 0);
536		lagg_setflags(lp, 0);
537		lagg_port_lladdr(lp, lp->lp_lladdr);
538	}
539
540	/* Restore interface */
541	ifp->if_type = lp->lp_iftype;
542	ifp->if_ioctl = lp->lp_ioctl;
543	ifp->if_output = lp->lp_output;
544	ifp->if_lagg = NULL;
545
546	/* Finally, remove the port from the lagg */
547	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
548	sc->sc_count--;
549
550	/* Update the primary interface */
551	if (lp == sc->sc_primary) {
552		uint8_t lladdr[ETHER_ADDR_LEN];
553
554		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
555			bzero(&lladdr, ETHER_ADDR_LEN);
556		} else {
557			bcopy(lp_ptr->lp_lladdr,
558			    lladdr, ETHER_ADDR_LEN);
559		}
560		lagg_lladdr(sc, lladdr);
561		sc->sc_primary = lp_ptr;
562
563		/* Update link layer address for each port */
564		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
565			lagg_port_lladdr(lp_ptr, lladdr);
566	}
567
568	/* Remove any pending lladdr changes from the queue */
569	if (lp->lp_detaching) {
570		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
571			if (llq->llq_ifp == ifp) {
572				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
573				    llq_entries);
574				free(llq, M_DEVBUF);
575				break;	/* Only appears once */
576			}
577		}
578	}
579
580	if (lp->lp_ifflags)
581		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
582
583	free(lp, M_DEVBUF);
584
585	/* Update lagg capabilities */
586	sc->sc_capabilities = lagg_capabilities(sc);
587
588	return (0);
589}
590
591static int
592lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
593{
594	struct lagg_reqport *rp = (struct lagg_reqport *)data;
595	struct lagg_softc *sc;
596	struct lagg_port *lp = NULL;
597	int error = 0;
598
599	/* Should be checked by the caller */
600	if (ifp->if_type != IFT_IEEE8023ADLAG ||
601	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_lagg) == NULL)
602		goto fallback;
603
604	switch (cmd) {
605	case SIOCGLAGGPORT:
606		LAGG_RLOCK(sc);
607		if (rp->rp_portname[0] == '\0' ||
608		    ifunit(rp->rp_portname) != ifp) {
609			error = EINVAL;
610			break;
611		}
612
613		if (lp->lp_lagg != sc) {
614			error = ENOENT;
615			break;
616		}
617
618		lagg_port2req(lp, rp);
619		LAGG_RUNLOCK(sc);
620		break;
621	default:
622		goto fallback;
623	}
624
625	return (error);
626
627fallback:
628	if (lp->lp_ioctl != NULL)
629		return ((*lp->lp_ioctl)(ifp, cmd, data));
630
631	return (EINVAL);
632}
633
634static int
635lagg_port_output(struct ifnet *ifp, struct mbuf *m,
636	struct sockaddr *dst, struct rtentry *rt0)
637{
638	struct lagg_port *lp = ifp->if_lagg;
639	struct ether_header *eh;
640	short type = 0;
641
642	switch (dst->sa_family) {
643		case pseudo_AF_HDRCMPLT:
644		case AF_UNSPEC:
645			eh = (struct ether_header *)dst->sa_data;
646			type = eh->ether_type;
647			break;
648	}
649
650	/*
651	 * Only allow ethernet types required to initiate or maintain the link,
652	 * aggregated frames take a different path.
653	 */
654	switch (ntohs(type)) {
655		case ETHERTYPE_PAE:	/* EAPOL PAE/802.1x */
656			return ((*lp->lp_output)(ifp, m, dst, rt0));
657	}
658
659	/* drop any other frames */
660	m_freem(m);
661	return (EBUSY);
662}
663
664static void
665lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
666{
667	struct lagg_port *lp;
668	struct lagg_softc *sc;
669
670	if ((lp = ifp->if_lagg) == NULL)
671		return;
672
673	sc = lp->lp_lagg;
674
675	LAGG_WLOCK(sc);
676	lp->lp_detaching = 1;
677	lagg_port_destroy(lp, 1);
678	LAGG_WUNLOCK(sc);
679}
680
681static void
682lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
683{
684	struct lagg_softc *sc = lp->lp_lagg;
685	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
686	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
687	rp->rp_prio = lp->lp_prio;
688	rp->rp_flags = lp->lp_flags;
689
690	/* Add protocol specific flags */
691	switch (sc->sc_proto) {
692		case LAGG_PROTO_FAILOVER:
693			if (lp == sc->sc_primary)
694				rp->rp_flags |= LAGG_PORT_MASTER;
695			/* FALLTHROUGH */
696		case LAGG_PROTO_ROUNDROBIN:
697		case LAGG_PROTO_LOADBALANCE:
698		case LAGG_PROTO_ETHERCHANNEL:
699			if (LAGG_PORTACTIVE(lp))
700				rp->rp_flags |= LAGG_PORT_ACTIVE;
701			break;
702
703		case LAGG_PROTO_LACP:
704			/* LACP has a different definition of active */
705			if (lacp_port_isactive(lp))
706				rp->rp_flags |= LAGG_PORT_ACTIVE;
707			break;
708	}
709
710}
711
712static void
713lagg_init(void *xsc)
714{
715	struct lagg_softc *sc = (struct lagg_softc *)xsc;
716	struct lagg_port *lp;
717	struct ifnet *ifp = sc->sc_ifp;
718
719	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
720		return;
721
722	LAGG_WLOCK(sc);
723
724	ifp->if_drv_flags |= IFF_DRV_RUNNING;
725	/* Update the port lladdrs */
726	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
727		lagg_port_lladdr(lp, IF_LLADDR(ifp));
728
729	if (sc->sc_init != NULL)
730		(*sc->sc_init)(sc);
731
732	LAGG_WUNLOCK(sc);
733}
734
735static void
736lagg_stop(struct lagg_softc *sc)
737{
738	struct ifnet *ifp = sc->sc_ifp;
739
740	LAGG_WLOCK_ASSERT(sc);
741
742	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
743		return;
744
745	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
746
747	if (sc->sc_stop != NULL)
748		(*sc->sc_stop)(sc);
749}
750
751static int
752lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
753{
754	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
755	struct lagg_reqall *ra = (struct lagg_reqall *)data;
756	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
757	struct ifreq *ifr = (struct ifreq *)data;
758	struct lagg_port *lp;
759	struct ifnet *tpif;
760	struct thread *td = curthread;
761	int i, error = 0, unlock = 1;
762
763	LAGG_WLOCK(sc);
764
765	bzero(&rpbuf, sizeof(rpbuf));
766
767	switch (cmd) {
768	case SIOCGLAGG:
769		ra->ra_proto = sc->sc_proto;
770		ra->ra_ports = i = 0;
771		lp = SLIST_FIRST(&sc->sc_ports);
772		while (lp && ra->ra_size >=
773		    i + sizeof(struct lagg_reqport)) {
774			lagg_port2req(lp, &rpbuf);
775			error = copyout(&rpbuf, (caddr_t)ra->ra_port + i,
776			    sizeof(struct lagg_reqport));
777			if (error)
778				break;
779			i += sizeof(struct lagg_reqport);
780			ra->ra_ports++;
781			lp = SLIST_NEXT(lp, lp_entries);
782		}
783		break;
784	case SIOCSLAGG:
785		error = priv_check(td, PRIV_NET_LAGG);
786		if (error)
787			break;
788		if (ra->ra_proto >= LAGG_PROTO_MAX) {
789			error = EPROTONOSUPPORT;
790			break;
791		}
792		if (sc->sc_proto != LAGG_PROTO_NONE) {
793			error = sc->sc_detach(sc);
794			/* Reset protocol and pointers */
795			sc->sc_proto = LAGG_PROTO_NONE;
796			sc->sc_detach = NULL;
797			sc->sc_start = NULL;
798			sc->sc_input = NULL;
799			sc->sc_port_create = NULL;
800			sc->sc_port_destroy = NULL;
801			sc->sc_linkstate = NULL;
802			sc->sc_init = NULL;
803			sc->sc_stop = NULL;
804			sc->sc_lladdr = NULL;
805		}
806		if (error != 0)
807			break;
808		for (i = 0; i < (sizeof(lagg_protos) /
809		    sizeof(lagg_protos[0])); i++) {
810			if (lagg_protos[i].ti_proto == ra->ra_proto) {
811				if (sc->sc_ifflags & IFF_DEBUG)
812					printf("%s: using proto %u\n",
813					    sc->sc_ifname,
814					    lagg_protos[i].ti_proto);
815				sc->sc_proto = lagg_protos[i].ti_proto;
816				if (sc->sc_proto != LAGG_PROTO_NONE)
817					error = lagg_protos[i].ti_attach(sc);
818				goto out;
819			}
820		}
821		error = EPROTONOSUPPORT;
822		break;
823	case SIOCGLAGGPORT:
824		if (rp->rp_portname[0] == '\0' ||
825		    (tpif = ifunit(rp->rp_portname)) == NULL) {
826			error = EINVAL;
827			break;
828		}
829
830		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
831		    lp->lp_lagg != sc) {
832			error = ENOENT;
833			break;
834		}
835
836		lagg_port2req(lp, rp);
837		break;
838	case SIOCSLAGGPORT:
839		error = priv_check(td, PRIV_NET_LAGG);
840		if (error)
841			break;
842		if (rp->rp_portname[0] == '\0' ||
843		    (tpif = ifunit(rp->rp_portname)) == NULL) {
844			error = EINVAL;
845			break;
846		}
847		error = lagg_port_create(sc, tpif);
848		break;
849	case SIOCSLAGGDELPORT:
850		error = priv_check(td, PRIV_NET_LAGG);
851		if (error)
852			break;
853		if (rp->rp_portname[0] == '\0' ||
854		    (tpif = ifunit(rp->rp_portname)) == NULL) {
855			error = EINVAL;
856			break;
857		}
858
859		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
860		    lp->lp_lagg != sc) {
861			error = ENOENT;
862			break;
863		}
864
865		error = lagg_port_destroy(lp, 1);
866		break;
867	case SIOCSIFFLAGS:
868		/* Set flags on ports too */
869		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
870			lagg_setflags(lp, 1);
871		}
872
873		if (!(ifp->if_flags & IFF_UP) &&
874		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
875			/*
876			 * If interface is marked down and it is running,
877			 * then stop and disable it.
878			 */
879			lagg_stop(sc);
880		} else if ((ifp->if_flags & IFF_UP) &&
881		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
882			/*
883			 * If interface is marked up and it is stopped, then
884			 * start it.
885			 */
886			LAGG_WUNLOCK(sc);
887			unlock = 0;
888			(*ifp->if_init)(sc);
889		}
890		break;
891	case SIOCADDMULTI:
892	case SIOCDELMULTI:
893		error = lagg_ether_setmulti(sc);
894		break;
895	case SIOCSIFMEDIA:
896	case SIOCGIFMEDIA:
897		LAGG_WUNLOCK(sc);
898		unlock = 0;
899		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
900		break;
901	default:
902		LAGG_WUNLOCK(sc);
903		unlock = 0;
904		error = ether_ioctl(ifp, cmd, data);
905		break;
906	}
907
908out:
909	if (unlock)
910		LAGG_WUNLOCK(sc);
911	return (error);
912}
913
914static int
915lagg_ether_setmulti(struct lagg_softc *sc)
916{
917	struct lagg_port *lp;
918
919	LAGG_WLOCK_ASSERT(sc);
920
921	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
922		/* First, remove any existing filter entries. */
923		lagg_ether_cmdmulti(lp, 0);
924		/* copy all addresses from the lagg interface to the port */
925		lagg_ether_cmdmulti(lp, 1);
926	}
927	return (0);
928}
929
930static int
931lagg_ether_cmdmulti(struct lagg_port *lp, int set)
932{
933	struct lagg_softc *sc = lp->lp_lagg;
934	struct ifnet *ifp = lp->lp_ifp;
935	struct ifnet *trifp = sc->sc_ifp;
936	struct lagg_mc *mc;
937	struct ifmultiaddr *ifma, *rifma = NULL;
938	struct sockaddr_dl sdl;
939	int error;
940
941	LAGG_WLOCK_ASSERT(sc);
942
943	bzero((char *)&sdl, sizeof(sdl));
944	sdl.sdl_len = sizeof(sdl);
945	sdl.sdl_family = AF_LINK;
946	sdl.sdl_type = IFT_ETHER;
947	sdl.sdl_alen = ETHER_ADDR_LEN;
948	sdl.sdl_index = ifp->if_index;
949
950	if (set) {
951		TAILQ_FOREACH(ifma, &trifp->if_multiaddrs, ifma_link) {
952			if (ifma->ifma_addr->sa_family != AF_LINK)
953				continue;
954			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
955			    LLADDR(&sdl), ETHER_ADDR_LEN);
956
957			error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
958			if (error)
959				return (error);
960			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
961			if (mc == NULL)
962				return (ENOMEM);
963			mc->mc_ifma = rifma;
964			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
965		}
966	} else {
967		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
968			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
969			if_delmulti_ifma(mc->mc_ifma);
970			free(mc, M_DEVBUF);
971		}
972	}
973	return (0);
974}
975
976/* Handle a ref counted flag that should be set on the lagg port as well */
977static int
978lagg_setflag(struct lagg_port *lp, int flag, int status,
979	     int (*func)(struct ifnet *, int))
980{
981	struct lagg_softc *sc = lp->lp_lagg;
982	struct ifnet *trifp = sc->sc_ifp;
983	struct ifnet *ifp = lp->lp_ifp;
984	int error;
985
986	LAGG_WLOCK_ASSERT(sc);
987
988	status = status ? (trifp->if_flags & flag) : 0;
989	/* Now "status" contains the flag value or 0 */
990
991	/*
992	 * See if recorded ports status is different from what
993	 * we want it to be.  If it is, flip it.  We record ports
994	 * status in lp_ifflags so that we won't clear ports flag
995	 * we haven't set.  In fact, we don't clear or set ports
996	 * flags directly, but get or release references to them.
997	 * That's why we can be sure that recorded flags still are
998	 * in accord with actual ports flags.
999	 */
1000	if (status != (lp->lp_ifflags & flag)) {
1001		error = (*func)(ifp, status);
1002		if (error)
1003			return (error);
1004		lp->lp_ifflags &= ~flag;
1005		lp->lp_ifflags |= status;
1006	}
1007	return (0);
1008}
1009
1010/*
1011 * Handle IFF_* flags that require certain changes on the lagg port
1012 * if "status" is true, update ports flags respective to the lagg
1013 * if "status" is false, forcedly clear the flags set on port.
1014 */
1015static int
1016lagg_setflags(struct lagg_port *lp, int status)
1017{
1018	int error, i;
1019
1020	for (i = 0; lagg_pflags[i].flag; i++) {
1021		error = lagg_setflag(lp, lagg_pflags[i].flag,
1022		    status, lagg_pflags[i].func);
1023		if (error)
1024			return (error);
1025	}
1026	return (0);
1027}
1028
1029static void
1030lagg_start(struct ifnet *ifp)
1031{
1032	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1033	struct mbuf *m;
1034	int error = 0;
1035
1036	LAGG_RLOCK(sc);
1037	for (;; error = 0) {
1038		IFQ_DEQUEUE(&ifp->if_snd, m);
1039		if (m == NULL)
1040			break;
1041
1042		BPF_MTAP(ifp, m);
1043
1044		if (sc->sc_proto != LAGG_PROTO_NONE)
1045			error = (*sc->sc_start)(sc, m);
1046		else
1047			m_free(m);
1048
1049		if (error == 0)
1050			ifp->if_opackets++;
1051		else
1052			ifp->if_oerrors++;
1053	}
1054	LAGG_RUNLOCK(sc);
1055
1056	return;
1057}
1058
1059static struct mbuf *
1060lagg_input(struct ifnet *ifp, struct mbuf *m)
1061{
1062	struct lagg_port *lp = ifp->if_lagg;
1063	struct lagg_softc *sc = lp->lp_lagg;
1064	struct ifnet *trifp = sc->sc_ifp;
1065
1066	if ((trifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1067	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1068	    sc->sc_proto == LAGG_PROTO_NONE) {
1069		m_freem(m);
1070		return (NULL);
1071	}
1072
1073	LAGG_RLOCK(sc);
1074	BPF_MTAP(trifp, m);
1075
1076	m = (*sc->sc_input)(sc, lp, m);
1077
1078	if (m != NULL) {
1079		ifp->if_ipackets++;
1080		ifp->if_ibytes += m->m_pkthdr.len;
1081		trifp->if_ipackets++;
1082		trifp->if_ibytes += m->m_pkthdr.len;
1083	}
1084
1085	LAGG_RUNLOCK(sc);
1086	return (m);
1087}
1088
1089static int
1090lagg_media_change(struct ifnet *ifp)
1091{
1092	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1093
1094	if (sc->sc_ifflags & IFF_DEBUG)
1095		printf("%s\n", __func__);
1096
1097	/* Ignore */
1098	return (0);
1099}
1100
1101static void
1102lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1103{
1104	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1105	struct lagg_port *lp;
1106
1107	imr->ifm_status = IFM_AVALID;
1108	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1109
1110	LAGG_RLOCK(sc);
1111	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1112		if (LAGG_PORTACTIVE(lp))
1113			imr->ifm_status |= IFM_ACTIVE;
1114	}
1115	LAGG_RUNLOCK(sc);
1116}
1117
1118static void
1119lagg_port_state(struct ifnet *ifp, int state)
1120{
1121	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1122	struct lagg_softc *sc = NULL;
1123
1124	if (lp != NULL)
1125		sc = lp->lp_lagg;
1126	if (sc == NULL)
1127		return;
1128
1129	LAGG_WLOCK(sc);
1130	if (sc->sc_linkstate != NULL)
1131		(*sc->sc_linkstate)(lp);
1132	LAGG_WUNLOCK(sc);
1133}
1134
1135struct lagg_port *
1136lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1137{
1138	struct lagg_port *lp_next, *rval = NULL;
1139	// int new_link = LINK_STATE_DOWN;
1140
1141	LAGG_WLOCK_ASSERT(sc);
1142	/*
1143	 * Search a port which reports an active link state.
1144	 */
1145
1146	if (lp == NULL)
1147		goto search;
1148	if (LAGG_PORTACTIVE(lp)) {
1149		rval = lp;
1150		goto found;
1151	}
1152	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1153	    LAGG_PORTACTIVE(lp_next)) {
1154		rval = lp_next;
1155		goto found;
1156	}
1157
1158search:
1159	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1160		if (LAGG_PORTACTIVE(lp_next)) {
1161			rval = lp_next;
1162			goto found;
1163		}
1164	}
1165
1166found:
1167	if (rval != NULL) {
1168		/*
1169		 * The IEEE 802.1D standard assumes that a lagg with
1170		 * multiple ports is always full duplex. This is valid
1171		 * for load sharing laggs and if at least two links
1172		 * are active. Unfortunately, checking the latter would
1173		 * be too expensive at this point.
1174		 XXX
1175		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1176		    (sc->sc_count > 1))
1177			new_link = LINK_STATE_FULL_DUPLEX;
1178		else
1179			new_link = rval->lp_link_state;
1180		 */
1181	}
1182
1183	return (rval);
1184}
1185
1186static const void *
1187lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1188{
1189	if (m->m_pkthdr.len < (off + len)) {
1190		return (NULL);
1191	} else if (m->m_len < (off + len)) {
1192		m_copydata(m, off, len, buf);
1193		return (buf);
1194	}
1195	return (mtod(m, char *) + off);
1196}
1197
1198uint32_t
1199lagg_hashmbuf(struct mbuf *m, uint32_t key)
1200{
1201	uint16_t etype;
1202	uint32_t p = 0;
1203	int off;
1204	struct ether_header *eh;
1205	struct ether_vlan_header vlanbuf;
1206	const struct ether_vlan_header *vlan;
1207#ifdef INET
1208	const struct ip *ip;
1209	struct ip ipbuf;
1210#endif
1211#ifdef INET6
1212	const struct ip6_hdr *ip6;
1213	struct ip6_hdr ip6buf;
1214#endif
1215
1216	off = sizeof(*eh);
1217	if (m->m_len < off)
1218		goto out;
1219	eh = mtod(m, struct ether_header *);
1220	etype = ntohs(eh->ether_type);
1221	p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1222	p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1223
1224	/* Special handling for encapsulating VLAN frames */
1225	if (m->m_flags & M_VLANTAG) {
1226		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1227		    sizeof(m->m_pkthdr.ether_vtag), p);
1228	} else if (etype == ETHERTYPE_VLAN) {
1229		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &vlanbuf);
1230		if (vlan == NULL)
1231			goto out;
1232
1233		p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1234		etype = ntohs(vlan->evl_proto);
1235		off += sizeof(*vlan) - sizeof(*eh);
1236	}
1237
1238	switch (etype) {
1239#ifdef INET
1240	case ETHERTYPE_IP:
1241		ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1242		if (ip == NULL)
1243			goto out;
1244
1245		p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1246		p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1247		break;
1248#endif
1249#ifdef INET6
1250	case ETHERTYPE_IPV6:
1251		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1252		if (ip6 == NULL)
1253			goto out;
1254
1255		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1256		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1257		break;
1258#endif
1259	}
1260out:
1261	return (p);
1262}
1263
1264int
1265lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1266{
1267	int error = 0;
1268
1269	/* Send mbuf */
1270	IFQ_ENQUEUE(&ifp->if_snd, m, error);
1271	if (error)
1272		return (error);
1273	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
1274		(*ifp->if_start)(ifp);
1275
1276	ifp->if_obytes += m->m_pkthdr.len;
1277	if (m->m_flags & M_MCAST)
1278		ifp->if_omcasts++;
1279
1280	return (error);
1281}
1282
1283/*
1284 * Simple round robin aggregation
1285 */
1286
1287static int
1288lagg_rr_attach(struct lagg_softc *sc)
1289{
1290	struct lagg_port *lp;
1291
1292	sc->sc_detach = lagg_rr_detach;
1293	sc->sc_start = lagg_rr_start;
1294	sc->sc_input = lagg_rr_input;
1295	sc->sc_port_create = NULL;
1296	sc->sc_port_destroy = lagg_rr_port_destroy;
1297	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1298
1299	lp = SLIST_FIRST(&sc->sc_ports);
1300	sc->sc_psc = (caddr_t)lp;
1301
1302	return (0);
1303}
1304
1305static int
1306lagg_rr_detach(struct lagg_softc *sc)
1307{
1308	sc->sc_psc = NULL;
1309	return (0);
1310}
1311
1312static void
1313lagg_rr_port_destroy(struct lagg_port *lp)
1314{
1315	struct lagg_softc *sc = lp->lp_lagg;
1316
1317	if (lp == (struct lagg_port *)sc->sc_psc)
1318		sc->sc_psc = NULL;
1319}
1320
1321static int
1322lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1323{
1324	struct lagg_port *lp = (struct lagg_port *)sc->sc_psc, *lp_next;
1325	int error = 0;
1326
1327	if (lp == NULL && (lp = lagg_link_active(sc, NULL)) == NULL)
1328		return (ENOENT);
1329
1330	/* Send mbuf */
1331	error = lagg_enqueue(lp->lp_ifp, m);
1332
1333	/* Get next active port */
1334	lp_next = lagg_link_active(sc, SLIST_NEXT(lp, lp_entries));
1335	sc->sc_psc = (caddr_t)lp_next;
1336
1337	return (error);
1338}
1339
1340static struct mbuf *
1341lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1342{
1343	struct ifnet *ifp = sc->sc_ifp;
1344
1345	/* Just pass in the packet to our lagg device */
1346	m->m_pkthdr.rcvif = ifp;
1347
1348	return (m);
1349}
1350
1351/*
1352 * Active failover
1353 */
1354
1355static int
1356lagg_fail_attach(struct lagg_softc *sc)
1357{
1358	sc->sc_detach = lagg_fail_detach;
1359	sc->sc_start = lagg_fail_start;
1360	sc->sc_input = lagg_fail_input;
1361	sc->sc_port_create = NULL;
1362	sc->sc_port_destroy = NULL;
1363
1364	return (0);
1365}
1366
1367static int
1368lagg_fail_detach(struct lagg_softc *sc)
1369{
1370	return (0);
1371}
1372
1373static int
1374lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1375{
1376	struct lagg_port *lp;
1377
1378	/* Use the master port if active or the next available port */
1379	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL)
1380		return (ENOENT);
1381
1382	/* Send mbuf */
1383	return (lagg_enqueue(lp->lp_ifp, m));
1384}
1385
1386static struct mbuf *
1387lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1388{
1389	struct ifnet *ifp = sc->sc_ifp;
1390	struct lagg_port *tmp_tp;
1391
1392	if (lp == sc->sc_primary) {
1393		m->m_pkthdr.rcvif = ifp;
1394		return (m);
1395	}
1396
1397	if (sc->sc_primary->lp_link_state == LINK_STATE_DOWN) {
1398		tmp_tp = lagg_link_active(sc, NULL);
1399		/*
1400		 * If tmp_tp is null, we've recieved a packet when all
1401		 * our links are down. Weird, but process it anyways.
1402		 */
1403		if ((tmp_tp == NULL || tmp_tp == lp)) {
1404			m->m_pkthdr.rcvif = ifp;
1405			return (m);
1406		}
1407	}
1408
1409	m_freem(m);
1410	return (NULL);
1411}
1412
1413/*
1414 * Loadbalancing
1415 */
1416
1417static int
1418lagg_lb_attach(struct lagg_softc *sc)
1419{
1420	struct lagg_port *lp;
1421	struct lagg_lb *lb;
1422
1423	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1424	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1425		return (ENOMEM);
1426
1427	sc->sc_detach = lagg_lb_detach;
1428	sc->sc_start = lagg_lb_start;
1429	sc->sc_input = lagg_lb_input;
1430	sc->sc_port_create = lagg_lb_port_create;
1431	sc->sc_port_destroy = lagg_lb_port_destroy;
1432	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1433
1434	lb->lb_key = arc4random();
1435	sc->sc_psc = (caddr_t)lb;
1436
1437	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1438		lagg_lb_port_create(lp);
1439
1440	return (0);
1441}
1442
1443static int
1444lagg_lb_detach(struct lagg_softc *sc)
1445{
1446	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1447	if (lb != NULL)
1448		free(lb, M_DEVBUF);
1449	return (0);
1450}
1451
1452static int
1453lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1454{
1455	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1456	struct lagg_port *lp_next;
1457	int i = 0;
1458
1459	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1460	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1461		if (lp_next == lp)
1462			continue;
1463		if (i >= LAGG_MAX_PORTS)
1464			return (EINVAL);
1465		if (sc->sc_ifflags & IFF_DEBUG)
1466			printf("%s: port %s at index %d\n",
1467			    sc->sc_ifname, lp_next->lp_ifname, i);
1468		lb->lb_ports[i++] = lp_next;
1469	}
1470
1471	return (0);
1472}
1473
1474static int
1475lagg_lb_port_create(struct lagg_port *lp)
1476{
1477	struct lagg_softc *sc = lp->lp_lagg;
1478	return (lagg_lb_porttable(sc, NULL));
1479}
1480
1481static void
1482lagg_lb_port_destroy(struct lagg_port *lp)
1483{
1484	struct lagg_softc *sc = lp->lp_lagg;
1485	lagg_lb_porttable(sc, lp);
1486}
1487
1488static int
1489lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1490{
1491	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1492	struct lagg_port *lp = NULL;
1493	uint32_t p = 0;
1494	int idx;
1495
1496	p = lagg_hashmbuf(m, lb->lb_key);
1497	if ((idx = p % sc->sc_count) >= LAGG_MAX_PORTS)
1498		return (EINVAL);
1499	lp = lb->lb_ports[idx];
1500
1501	/*
1502	 * Check the port's link state. This will return the next active
1503	 * port if the link is down or the port is NULL.
1504	 */
1505	if ((lp = lagg_link_active(sc, lp)) == NULL)
1506		return (ENOENT);
1507
1508	/* Send mbuf */
1509	return (lagg_enqueue(lp->lp_ifp, m));
1510}
1511
1512static struct mbuf *
1513lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1514{
1515	struct ifnet *ifp = sc->sc_ifp;
1516
1517	/* Just pass in the packet to our lagg device */
1518	m->m_pkthdr.rcvif = ifp;
1519
1520	return (m);
1521}
1522
1523/*
1524 * 802.3ad LACP
1525 */
1526
1527static int
1528lagg_lacp_attach(struct lagg_softc *sc)
1529{
1530	struct lagg_port *lp;
1531	int error;
1532
1533	sc->sc_detach = lagg_lacp_detach;
1534	sc->sc_port_create = lacp_port_create;
1535	sc->sc_port_destroy = lacp_port_destroy;
1536	sc->sc_linkstate = lacp_linkstate;
1537	sc->sc_start = lagg_lacp_start;
1538	sc->sc_input = lagg_lacp_input;
1539	sc->sc_init = lacp_init;
1540	sc->sc_stop = lacp_stop;
1541	sc->sc_lladdr = lagg_lacp_lladdr;
1542
1543	error = lacp_attach(sc);
1544	if (error)
1545		return (error);
1546
1547	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1548		lacp_port_create(lp);
1549
1550	return (error);
1551}
1552
1553static int
1554lagg_lacp_detach(struct lagg_softc *sc)
1555{
1556	struct lagg_port *lp;
1557	int error;
1558
1559	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1560		lacp_port_destroy(lp);
1561
1562	/* unlocking is safe here */
1563	LAGG_WUNLOCK(sc);
1564	error = lacp_detach(sc);
1565	LAGG_WLOCK(sc);
1566
1567	return (error);
1568}
1569
1570static void
1571lagg_lacp_lladdr(struct lagg_softc *sc)
1572{
1573	struct lagg_port *lp;
1574
1575	/* purge all the lacp ports */
1576	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1577		lacp_port_destroy(lp);
1578
1579	/* add them back in */
1580	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1581		lacp_port_create(lp);
1582}
1583
1584static int
1585lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1586{
1587	struct lagg_port *lp;
1588
1589	lp = lacp_select_tx_port(sc, m);
1590	if (lp == NULL)
1591		return (EBUSY);
1592
1593	/* Send mbuf */
1594	return (lagg_enqueue(lp->lp_ifp, m));
1595}
1596
1597static struct mbuf *
1598lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1599{
1600	struct ifnet *ifp = sc->sc_ifp;
1601	struct ether_header *eh;
1602	u_short etype;
1603
1604	eh = mtod(m, struct ether_header *);
1605	etype = ntohs(eh->ether_type);
1606
1607	/* Tap off LACP control messages */
1608	if (etype == ETHERTYPE_SLOW) {
1609		lacp_input(lp, m);
1610		return (NULL);
1611	}
1612
1613	/*
1614	 * If the port is not collecting or not in the active aggregator then
1615	 * free and return.
1616	 */
1617	if ((lp->lp_flags & LAGG_PORT_COLLECTING) == 0 ||
1618	    lacp_port_isactive(lp) == 0) {
1619		m_freem(m);
1620		return (NULL);
1621	}
1622
1623	m->m_pkthdr.rcvif = ifp;
1624	return (m);
1625}
1626