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