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