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