xref: /freebsd/sys/net/if_lagg.c (revision f2d48b5e2c3b45850585e4d7aee324fe148afbf2)
1 /*	$OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6  * Copyright (c) 2014, 2016 Marcelo Araujo <araujo@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_inet.h"
25 #include "opt_inet6.h"
26 #include "opt_kern_tls.h"
27 #include "opt_ratelimit.h"
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/mbuf.h>
33 #include <sys/queue.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 #include <sys/sysctl.h>
37 #include <sys/module.h>
38 #include <sys/priv.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/lock.h>
42 #include <sys/rmlock.h>
43 #include <sys/sx.h>
44 #include <sys/taskqueue.h>
45 #include <sys/eventhandler.h>
46 
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_clone.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
54 #include <net/if_var.h>
55 #include <net/bpf.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58 #include <net/infiniband.h>
59 
60 #if defined(INET) || defined(INET6)
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
63 #endif
64 #ifdef INET
65 #include <netinet/in_systm.h>
66 #include <netinet/if_ether.h>
67 #endif
68 
69 #ifdef INET6
70 #include <netinet/ip6.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet6/in6_ifattach.h>
73 #endif
74 
75 #include <net/if_vlan_var.h>
76 #include <net/if_lagg.h>
77 #include <net/ieee8023ad_lacp.h>
78 
79 #ifdef INET6
80 /*
81  * XXX: declare here to avoid to include many inet6 related files..
82  * should be more generalized?
83  */
84 extern void	nd6_setmtu(struct ifnet *);
85 #endif
86 
87 #define	LAGG_SX_INIT(_sc)	sx_init(&(_sc)->sc_sx, "if_lagg sx")
88 #define	LAGG_SX_DESTROY(_sc)	sx_destroy(&(_sc)->sc_sx)
89 #define	LAGG_XLOCK(_sc)		sx_xlock(&(_sc)->sc_sx)
90 #define	LAGG_XUNLOCK(_sc)	sx_xunlock(&(_sc)->sc_sx)
91 #define	LAGG_SXLOCK_ASSERT(_sc)	sx_assert(&(_sc)->sc_sx, SA_LOCKED)
92 #define	LAGG_XLOCK_ASSERT(_sc)	sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
93 
94 /* Special flags we should propagate to the lagg ports. */
95 static struct {
96 	int flag;
97 	int (*func)(struct ifnet *, int);
98 } lagg_pflags[] = {
99 	{IFF_PROMISC, ifpromisc},
100 	{IFF_ALLMULTI, if_allmulti},
101 	{0, NULL}
102 };
103 
104 struct lagg_snd_tag {
105 	struct m_snd_tag com;
106 	struct m_snd_tag *tag;
107 };
108 
109 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
110 #define	V_lagg_list	VNET(lagg_list)
111 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
112 #define	V_lagg_list_mtx	VNET(lagg_list_mtx)
113 #define	LAGG_LIST_LOCK_INIT(x)		mtx_init(&V_lagg_list_mtx, \
114 					"if_lagg list", NULL, MTX_DEF)
115 #define	LAGG_LIST_LOCK_DESTROY(x)	mtx_destroy(&V_lagg_list_mtx)
116 #define	LAGG_LIST_LOCK(x)		mtx_lock(&V_lagg_list_mtx)
117 #define	LAGG_LIST_UNLOCK(x)		mtx_unlock(&V_lagg_list_mtx)
118 eventhandler_tag	lagg_detach_cookie = NULL;
119 
120 static int	lagg_clone_create(struct if_clone *, int, caddr_t);
121 static void	lagg_clone_destroy(struct ifnet *);
122 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
123 #define	V_lagg_cloner	VNET(lagg_cloner)
124 static const char laggname[] = "lagg";
125 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
126 
127 static void	lagg_capabilities(struct lagg_softc *);
128 static int	lagg_port_create(struct lagg_softc *, struct ifnet *);
129 static int	lagg_port_destroy(struct lagg_port *, int);
130 static struct mbuf *lagg_input_ethernet(struct ifnet *, struct mbuf *);
131 static struct mbuf *lagg_input_infiniband(struct ifnet *, struct mbuf *);
132 static void	lagg_linkstate(struct lagg_softc *);
133 static void	lagg_port_state(struct ifnet *, int);
134 static int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
135 static int	lagg_port_output(struct ifnet *, struct mbuf *,
136 		    const struct sockaddr *, struct route *);
137 static void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
138 #ifdef LAGG_PORT_STACKING
139 static int	lagg_port_checkstacking(struct lagg_softc *);
140 #endif
141 static void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
142 static void	lagg_init(void *);
143 static void	lagg_stop(struct lagg_softc *);
144 static int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
145 #if defined(KERN_TLS) || defined(RATELIMIT)
146 static int	lagg_snd_tag_alloc(struct ifnet *,
147 		    union if_snd_tag_alloc_params *,
148 		    struct m_snd_tag **);
149 static int	lagg_snd_tag_modify(struct m_snd_tag *,
150 		    union if_snd_tag_modify_params *);
151 static int	lagg_snd_tag_query(struct m_snd_tag *,
152 		    union if_snd_tag_query_params *);
153 static void	lagg_snd_tag_free(struct m_snd_tag *);
154 static void     lagg_ratelimit_query(struct ifnet *,
155 		    struct if_ratelimit_query_results *);
156 #endif
157 static int	lagg_setmulti(struct lagg_port *);
158 static int	lagg_clrmulti(struct lagg_port *);
159 static	int	lagg_setcaps(struct lagg_port *, int cap);
160 static	int	lagg_setflag(struct lagg_port *, int, int,
161 		    int (*func)(struct ifnet *, int));
162 static	int	lagg_setflags(struct lagg_port *, int status);
163 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
164 static int	lagg_transmit_ethernet(struct ifnet *, struct mbuf *);
165 static int	lagg_transmit_infiniband(struct ifnet *, struct mbuf *);
166 static void	lagg_qflush(struct ifnet *);
167 static int	lagg_media_change(struct ifnet *);
168 static void	lagg_media_status(struct ifnet *, struct ifmediareq *);
169 static struct lagg_port *lagg_link_active(struct lagg_softc *,
170 	    struct lagg_port *);
171 
172 /* Simple round robin */
173 static void	lagg_rr_attach(struct lagg_softc *);
174 static int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
175 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
176 		    struct mbuf *);
177 
178 /* Active failover */
179 static int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
180 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
181 		    struct mbuf *);
182 
183 /* Loadbalancing */
184 static void	lagg_lb_attach(struct lagg_softc *);
185 static void	lagg_lb_detach(struct lagg_softc *);
186 static int	lagg_lb_port_create(struct lagg_port *);
187 static void	lagg_lb_port_destroy(struct lagg_port *);
188 static int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
189 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
190 		    struct mbuf *);
191 static int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
192 
193 /* Broadcast */
194 static int    lagg_bcast_start(struct lagg_softc *, struct mbuf *);
195 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
196 		    struct mbuf *);
197 
198 /* 802.3ad LACP */
199 static void	lagg_lacp_attach(struct lagg_softc *);
200 static void	lagg_lacp_detach(struct lagg_softc *);
201 static int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
202 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
203 		    struct mbuf *);
204 static void	lagg_lacp_lladdr(struct lagg_softc *);
205 
206 /* lagg protocol table */
207 static const struct lagg_proto {
208 	lagg_proto	pr_num;
209 	void		(*pr_attach)(struct lagg_softc *);
210 	void		(*pr_detach)(struct lagg_softc *);
211 	int		(*pr_start)(struct lagg_softc *, struct mbuf *);
212 	struct mbuf *	(*pr_input)(struct lagg_softc *, struct lagg_port *,
213 			    struct mbuf *);
214 	int		(*pr_addport)(struct lagg_port *);
215 	void		(*pr_delport)(struct lagg_port *);
216 	void		(*pr_linkstate)(struct lagg_port *);
217 	void 		(*pr_init)(struct lagg_softc *);
218 	void 		(*pr_stop)(struct lagg_softc *);
219 	void 		(*pr_lladdr)(struct lagg_softc *);
220 	void		(*pr_request)(struct lagg_softc *, void *);
221 	void		(*pr_portreq)(struct lagg_port *, void *);
222 } lagg_protos[] = {
223     {
224 	.pr_num = LAGG_PROTO_NONE
225     },
226     {
227 	.pr_num = LAGG_PROTO_ROUNDROBIN,
228 	.pr_attach = lagg_rr_attach,
229 	.pr_start = lagg_rr_start,
230 	.pr_input = lagg_rr_input,
231     },
232     {
233 	.pr_num = LAGG_PROTO_FAILOVER,
234 	.pr_start = lagg_fail_start,
235 	.pr_input = lagg_fail_input,
236     },
237     {
238 	.pr_num = LAGG_PROTO_LOADBALANCE,
239 	.pr_attach = lagg_lb_attach,
240 	.pr_detach = lagg_lb_detach,
241 	.pr_start = lagg_lb_start,
242 	.pr_input = lagg_lb_input,
243 	.pr_addport = lagg_lb_port_create,
244 	.pr_delport = lagg_lb_port_destroy,
245     },
246     {
247 	.pr_num = LAGG_PROTO_LACP,
248 	.pr_attach = lagg_lacp_attach,
249 	.pr_detach = lagg_lacp_detach,
250 	.pr_start = lagg_lacp_start,
251 	.pr_input = lagg_lacp_input,
252 	.pr_addport = lacp_port_create,
253 	.pr_delport = lacp_port_destroy,
254 	.pr_linkstate = lacp_linkstate,
255 	.pr_init = lacp_init,
256 	.pr_stop = lacp_stop,
257 	.pr_lladdr = lagg_lacp_lladdr,
258 	.pr_request = lacp_req,
259 	.pr_portreq = lacp_portreq,
260     },
261     {
262 	.pr_num = LAGG_PROTO_BROADCAST,
263 	.pr_start = lagg_bcast_start,
264 	.pr_input = lagg_bcast_input,
265     },
266 };
267 
268 SYSCTL_DECL(_net_link);
269 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
270     "Link Aggregation");
271 
272 /* Allow input on any failover links */
273 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
274 #define	V_lagg_failover_rx_all	VNET(lagg_failover_rx_all)
275 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
276     &VNET_NAME(lagg_failover_rx_all), 0,
277     "Accept input from any interface in a failover lagg");
278 
279 /* Default value for using flowid */
280 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
281 #define	V_def_use_flowid	VNET(def_use_flowid)
282 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
283     &VNET_NAME(def_use_flowid), 0,
284     "Default setting for using flow id for load sharing");
285 
286 /* Default value for using numa */
287 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
288 #define	V_def_use_numa	VNET(def_use_numa)
289 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa, CTLFLAG_RWTUN,
290     &VNET_NAME(def_use_numa), 0,
291     "Use numa to steer flows");
292 
293 /* Default value for flowid shift */
294 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
295 #define	V_def_flowid_shift	VNET(def_flowid_shift)
296 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
297     &VNET_NAME(def_flowid_shift), 0,
298     "Default setting for flowid shift for load sharing");
299 
300 static void
301 vnet_lagg_init(const void *unused __unused)
302 {
303 
304 	LAGG_LIST_LOCK_INIT();
305 	SLIST_INIT(&V_lagg_list);
306 	V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
307 	    lagg_clone_destroy, 0);
308 }
309 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
310     vnet_lagg_init, NULL);
311 
312 static void
313 vnet_lagg_uninit(const void *unused __unused)
314 {
315 
316 	if_clone_detach(V_lagg_cloner);
317 	LAGG_LIST_LOCK_DESTROY();
318 }
319 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
320     vnet_lagg_uninit, NULL);
321 
322 static int
323 lagg_modevent(module_t mod, int type, void *data)
324 {
325 
326 	switch (type) {
327 	case MOD_LOAD:
328 		lagg_input_ethernet_p = lagg_input_ethernet;
329 		lagg_input_infiniband_p = lagg_input_infiniband;
330 		lagg_linkstate_p = lagg_port_state;
331 		lagg_detach_cookie = EVENTHANDLER_REGISTER(
332 		    ifnet_departure_event, lagg_port_ifdetach, NULL,
333 		    EVENTHANDLER_PRI_ANY);
334 		break;
335 	case MOD_UNLOAD:
336 		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
337 		    lagg_detach_cookie);
338 		lagg_input_ethernet_p = NULL;
339 		lagg_input_infiniband_p = NULL;
340 		lagg_linkstate_p = NULL;
341 		break;
342 	default:
343 		return (EOPNOTSUPP);
344 	}
345 	return (0);
346 }
347 
348 static moduledata_t lagg_mod = {
349 	"if_lagg",
350 	lagg_modevent,
351 	0
352 };
353 
354 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
355 MODULE_VERSION(if_lagg, 1);
356 MODULE_DEPEND(if_lagg, if_infiniband, 1, 1, 1);
357 
358 static void
359 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
360 {
361 
362 	LAGG_XLOCK_ASSERT(sc);
363 	KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
364 	    __func__, sc));
365 
366 	if (sc->sc_ifflags & IFF_DEBUG)
367 		if_printf(sc->sc_ifp, "using proto %u\n", pr);
368 
369 	if (lagg_protos[pr].pr_attach != NULL)
370 		lagg_protos[pr].pr_attach(sc);
371 	sc->sc_proto = pr;
372 }
373 
374 static void
375 lagg_proto_detach(struct lagg_softc *sc)
376 {
377 	lagg_proto pr;
378 
379 	LAGG_XLOCK_ASSERT(sc);
380 	pr = sc->sc_proto;
381 	sc->sc_proto = LAGG_PROTO_NONE;
382 
383 	if (lagg_protos[pr].pr_detach != NULL)
384 		lagg_protos[pr].pr_detach(sc);
385 }
386 
387 static int
388 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
389 {
390 
391 	return (lagg_protos[sc->sc_proto].pr_start(sc, m));
392 }
393 
394 static struct mbuf *
395 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
396 {
397 
398 	return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
399 }
400 
401 static int
402 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
403 {
404 
405 	if (lagg_protos[sc->sc_proto].pr_addport == NULL)
406 		return (0);
407 	else
408 		return (lagg_protos[sc->sc_proto].pr_addport(lp));
409 }
410 
411 static void
412 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
413 {
414 
415 	if (lagg_protos[sc->sc_proto].pr_delport != NULL)
416 		lagg_protos[sc->sc_proto].pr_delport(lp);
417 }
418 
419 static void
420 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
421 {
422 
423 	if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
424 		lagg_protos[sc->sc_proto].pr_linkstate(lp);
425 }
426 
427 static void
428 lagg_proto_init(struct lagg_softc *sc)
429 {
430 
431 	if (lagg_protos[sc->sc_proto].pr_init != NULL)
432 		lagg_protos[sc->sc_proto].pr_init(sc);
433 }
434 
435 static void
436 lagg_proto_stop(struct lagg_softc *sc)
437 {
438 
439 	if (lagg_protos[sc->sc_proto].pr_stop != NULL)
440 		lagg_protos[sc->sc_proto].pr_stop(sc);
441 }
442 
443 static void
444 lagg_proto_lladdr(struct lagg_softc *sc)
445 {
446 
447 	if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
448 		lagg_protos[sc->sc_proto].pr_lladdr(sc);
449 }
450 
451 static void
452 lagg_proto_request(struct lagg_softc *sc, void *v)
453 {
454 
455 	if (lagg_protos[sc->sc_proto].pr_request != NULL)
456 		lagg_protos[sc->sc_proto].pr_request(sc, v);
457 }
458 
459 static void
460 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
461 {
462 
463 	if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
464 		lagg_protos[sc->sc_proto].pr_portreq(lp, v);
465 }
466 
467 /*
468  * This routine is run via an vlan
469  * config EVENT
470  */
471 static void
472 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
473 {
474 	struct lagg_softc *sc = ifp->if_softc;
475 	struct lagg_port *lp;
476 
477 	if (ifp->if_softc !=  arg)   /* Not our event */
478 		return;
479 
480 	LAGG_XLOCK(sc);
481 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
482 		EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
483 	LAGG_XUNLOCK(sc);
484 }
485 
486 /*
487  * This routine is run via an vlan
488  * unconfig EVENT
489  */
490 static void
491 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
492 {
493 	struct lagg_softc *sc = ifp->if_softc;
494 	struct lagg_port *lp;
495 
496 	if (ifp->if_softc !=  arg)   /* Not our event */
497 		return;
498 
499 	LAGG_XLOCK(sc);
500 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
501 		EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
502 	LAGG_XUNLOCK(sc);
503 }
504 
505 static int
506 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
507 {
508 	struct iflaggparam iflp;
509 	struct lagg_softc *sc;
510 	struct ifnet *ifp;
511 	int if_type;
512 	int error;
513 	static const uint8_t eaddr[LAGG_ADDR_LEN];
514 	static const uint8_t ib_bcast_addr[INFINIBAND_ADDR_LEN] = {
515 		0x00, 0xff, 0xff, 0xff,
516 		0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
517 		0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
518 	};
519 
520 	if (params != NULL) {
521 		error = copyin(params, &iflp, sizeof(iflp));
522 		if (error)
523 			return (error);
524 
525 		switch (iflp.lagg_type) {
526 		case LAGG_TYPE_ETHERNET:
527 			if_type = IFT_ETHER;
528 			break;
529 		case LAGG_TYPE_INFINIBAND:
530 			if_type = IFT_INFINIBAND;
531 			break;
532 		default:
533 			return (EINVAL);
534 		}
535 	} else {
536 		if_type = IFT_ETHER;
537 	}
538 
539 	sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK|M_ZERO);
540 	ifp = sc->sc_ifp = if_alloc(if_type);
541 	if (ifp == NULL) {
542 		free(sc, M_LAGG);
543 		return (ENOSPC);
544 	}
545 	LAGG_SX_INIT(sc);
546 
547 	mtx_init(&sc->sc_mtx, "lagg-mtx", NULL, MTX_DEF);
548 	callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
549 
550 	LAGG_XLOCK(sc);
551 	if (V_def_use_flowid)
552 		sc->sc_opts |= LAGG_OPT_USE_FLOWID;
553 	if (V_def_use_numa)
554 		sc->sc_opts |= LAGG_OPT_USE_NUMA;
555 	sc->flowid_shift = V_def_flowid_shift;
556 
557 	/* Hash all layers by default */
558 	sc->sc_flags = MBUF_HASHFLAG_L2|MBUF_HASHFLAG_L3|MBUF_HASHFLAG_L4;
559 
560 	lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
561 
562 	CK_SLIST_INIT(&sc->sc_ports);
563 
564 	switch (if_type) {
565 	case IFT_ETHER:
566 		/* Initialise pseudo media types */
567 		ifmedia_init(&sc->sc_media, 0, lagg_media_change,
568 		    lagg_media_status);
569 		ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
570 		ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
571 
572 		if_initname(ifp, laggname, unit);
573 		ifp->if_transmit = lagg_transmit_ethernet;
574 		break;
575 	case IFT_INFINIBAND:
576 		if_initname(ifp, laggname, unit);
577 		ifp->if_transmit = lagg_transmit_infiniband;
578 		break;
579 	default:
580 		break;
581 	}
582 	ifp->if_softc = sc;
583 	ifp->if_qflush = lagg_qflush;
584 	ifp->if_init = lagg_init;
585 	ifp->if_ioctl = lagg_ioctl;
586 	ifp->if_get_counter = lagg_get_counter;
587 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
588 #if defined(KERN_TLS) || defined(RATELIMIT)
589 	ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
590 	ifp->if_snd_tag_modify = lagg_snd_tag_modify;
591 	ifp->if_snd_tag_query = lagg_snd_tag_query;
592 	ifp->if_snd_tag_free = lagg_snd_tag_free;
593 	ifp->if_ratelimit_query = lagg_ratelimit_query;
594 #endif
595 	ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
596 
597 	/*
598 	 * Attach as an ordinary ethernet device, children will be attached
599 	 * as special device IFT_IEEE8023ADLAG or IFT_INFINIBANDLAG.
600 	 */
601 	switch (if_type) {
602 	case IFT_ETHER:
603 		ether_ifattach(ifp, eaddr);
604 		break;
605 	case IFT_INFINIBAND:
606 		infiniband_ifattach(ifp, eaddr, ib_bcast_addr);
607 		break;
608 	default:
609 		break;
610 	}
611 
612 	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
613 		lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
614 	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
615 		lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
616 
617 	/* Insert into the global list of laggs */
618 	LAGG_LIST_LOCK();
619 	SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
620 	LAGG_LIST_UNLOCK();
621 	LAGG_XUNLOCK(sc);
622 
623 	return (0);
624 }
625 
626 static void
627 lagg_clone_destroy(struct ifnet *ifp)
628 {
629 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
630 	struct lagg_port *lp;
631 
632 	LAGG_XLOCK(sc);
633 	sc->sc_destroying = 1;
634 	lagg_stop(sc);
635 	ifp->if_flags &= ~IFF_UP;
636 
637 	EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
638 	EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
639 
640 	/* Shutdown and remove lagg ports */
641 	while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
642 		lagg_port_destroy(lp, 1);
643 
644 	/* Unhook the aggregation protocol */
645 	lagg_proto_detach(sc);
646 	LAGG_XUNLOCK(sc);
647 
648 	switch (ifp->if_type) {
649 	case IFT_ETHER:
650 		ifmedia_removeall(&sc->sc_media);
651 		ether_ifdetach(ifp);
652 		break;
653 	case IFT_INFINIBAND:
654 		infiniband_ifdetach(ifp);
655 		break;
656 	default:
657 		break;
658 	}
659 	if_free(ifp);
660 
661 	LAGG_LIST_LOCK();
662 	SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
663 	LAGG_LIST_UNLOCK();
664 
665 	mtx_destroy(&sc->sc_mtx);
666 	LAGG_SX_DESTROY(sc);
667 	free(sc, M_LAGG);
668 }
669 
670 static void
671 lagg_capabilities(struct lagg_softc *sc)
672 {
673 	struct lagg_port *lp;
674 	int cap, ena, pena;
675 	uint64_t hwa;
676 	struct ifnet_hw_tsomax hw_tsomax;
677 
678 	LAGG_XLOCK_ASSERT(sc);
679 
680 	/* Get common enabled capabilities for the lagg ports */
681 	ena = ~0;
682 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
683 		ena &= lp->lp_ifp->if_capenable;
684 	ena = (ena == ~0 ? 0 : ena);
685 
686 	/*
687 	 * Apply common enabled capabilities back to the lagg ports.
688 	 * May require several iterations if they are dependent.
689 	 */
690 	do {
691 		pena = ena;
692 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
693 			lagg_setcaps(lp, ena);
694 			ena &= lp->lp_ifp->if_capenable;
695 		}
696 	} while (pena != ena);
697 
698 	/* Get other capabilities from the lagg ports */
699 	cap = ~0;
700 	hwa = ~(uint64_t)0;
701 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
702 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
703 		cap &= lp->lp_ifp->if_capabilities;
704 		hwa &= lp->lp_ifp->if_hwassist;
705 		if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
706 	}
707 	cap = (cap == ~0 ? 0 : cap);
708 	hwa = (hwa == ~(uint64_t)0 ? 0 : hwa);
709 
710 	if (sc->sc_ifp->if_capabilities != cap ||
711 	    sc->sc_ifp->if_capenable != ena ||
712 	    sc->sc_ifp->if_hwassist != hwa ||
713 	    if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
714 		sc->sc_ifp->if_capabilities = cap;
715 		sc->sc_ifp->if_capenable = ena;
716 		sc->sc_ifp->if_hwassist = hwa;
717 		getmicrotime(&sc->sc_ifp->if_lastchange);
718 
719 		if (sc->sc_ifflags & IFF_DEBUG)
720 			if_printf(sc->sc_ifp,
721 			    "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
722 	}
723 }
724 
725 static int
726 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
727 {
728 	struct lagg_softc *sc_ptr;
729 	struct lagg_port *lp, *tlp;
730 	struct ifreq ifr;
731 	int error, i, oldmtu;
732 	int if_type;
733 	uint64_t *pval;
734 
735 	LAGG_XLOCK_ASSERT(sc);
736 
737 	if (sc->sc_ifp == ifp) {
738 		if_printf(sc->sc_ifp,
739 		    "cannot add a lagg to itself as a port\n");
740 		return (EINVAL);
741 	}
742 
743 	if (sc->sc_destroying == 1)
744 		return (ENXIO);
745 
746 	/* Limit the maximal number of lagg ports */
747 	if (sc->sc_count >= LAGG_MAX_PORTS)
748 		return (ENOSPC);
749 
750 	/* Check if port has already been associated to a lagg */
751 	if (ifp->if_lagg != NULL) {
752 		/* Port is already in the current lagg? */
753 		lp = (struct lagg_port *)ifp->if_lagg;
754 		if (lp->lp_softc == sc)
755 			return (EEXIST);
756 		return (EBUSY);
757 	}
758 
759 	switch (sc->sc_ifp->if_type) {
760 	case IFT_ETHER:
761 		/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
762 		if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
763 			return (EPROTONOSUPPORT);
764 		if_type = IFT_IEEE8023ADLAG;
765 		break;
766 	case IFT_INFINIBAND:
767 		/* XXX Disallow non-infiniband interfaces */
768 		if (ifp->if_type != IFT_INFINIBAND)
769 			return (EPROTONOSUPPORT);
770 		if_type = IFT_INFINIBANDLAG;
771 		break;
772 	default:
773 		break;
774 	}
775 
776 	/* Allow the first Ethernet member to define the MTU */
777 	oldmtu = -1;
778 	if (CK_SLIST_EMPTY(&sc->sc_ports)) {
779 		sc->sc_ifp->if_mtu = ifp->if_mtu;
780 	} else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
781 		if (ifp->if_ioctl == NULL) {
782 			if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
783 			    ifp->if_xname);
784 			return (EINVAL);
785 		}
786 		oldmtu = ifp->if_mtu;
787 		strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
788 		ifr.ifr_mtu = sc->sc_ifp->if_mtu;
789 		error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
790 		if (error != 0) {
791 			if_printf(sc->sc_ifp, "invalid MTU for %s\n",
792 			    ifp->if_xname);
793 			return (error);
794 		}
795 		ifr.ifr_mtu = oldmtu;
796 	}
797 
798 	lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK|M_ZERO);
799 	lp->lp_softc = sc;
800 
801 	/* Check if port is a stacked lagg */
802 	LAGG_LIST_LOCK();
803 	SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
804 		if (ifp == sc_ptr->sc_ifp) {
805 			LAGG_LIST_UNLOCK();
806 			free(lp, M_LAGG);
807 			if (oldmtu != -1)
808 				(*ifp->if_ioctl)(ifp, SIOCSIFMTU,
809 				    (caddr_t)&ifr);
810 			return (EINVAL);
811 			/* XXX disable stacking for the moment, its untested */
812 #ifdef LAGG_PORT_STACKING
813 			lp->lp_flags |= LAGG_PORT_STACK;
814 			if (lagg_port_checkstacking(sc_ptr) >=
815 			    LAGG_MAX_STACKING) {
816 				LAGG_LIST_UNLOCK();
817 				free(lp, M_LAGG);
818 				if (oldmtu != -1)
819 					(*ifp->if_ioctl)(ifp, SIOCSIFMTU,
820 					    (caddr_t)&ifr);
821 				return (E2BIG);
822 			}
823 #endif
824 		}
825 	}
826 	LAGG_LIST_UNLOCK();
827 
828 	if_ref(ifp);
829 	lp->lp_ifp = ifp;
830 
831 	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ifp->if_addrlen);
832 	lp->lp_ifcapenable = ifp->if_capenable;
833 	if (CK_SLIST_EMPTY(&sc->sc_ports)) {
834 		bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
835 		lagg_proto_lladdr(sc);
836 		EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
837 	} else {
838 		if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
839 	}
840 	lagg_setflags(lp, 1);
841 
842 	if (CK_SLIST_EMPTY(&sc->sc_ports))
843 		sc->sc_primary = lp;
844 
845 	/* Change the interface type */
846 	lp->lp_iftype = ifp->if_type;
847 	ifp->if_type = if_type;
848 	ifp->if_lagg = lp;
849 	lp->lp_ioctl = ifp->if_ioctl;
850 	ifp->if_ioctl = lagg_port_ioctl;
851 	lp->lp_output = ifp->if_output;
852 	ifp->if_output = lagg_port_output;
853 
854 	/* Read port counters */
855 	pval = lp->port_counters.val;
856 	for (i = 0; i < IFCOUNTERS; i++, pval++)
857 		*pval = ifp->if_get_counter(ifp, i);
858 
859 	/*
860 	 * Insert into the list of ports.
861 	 * Keep ports sorted by if_index. It is handy, when configuration
862 	 * is predictable and `ifconfig laggN create ...` command
863 	 * will lead to the same result each time.
864 	 */
865 	CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
866 		if (tlp->lp_ifp->if_index < ifp->if_index && (
867 		    CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
868 		    ((struct  lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
869 		    ifp->if_index))
870 			break;
871 	}
872 	if (tlp != NULL)
873 		CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
874 	else
875 		CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
876 	sc->sc_count++;
877 
878 	lagg_setmulti(lp);
879 
880 	if ((error = lagg_proto_addport(sc, lp)) != 0) {
881 		/* Remove the port, without calling pr_delport. */
882 		lagg_port_destroy(lp, 0);
883 		if (oldmtu != -1)
884 			(*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
885 		return (error);
886 	}
887 
888 	/* Update lagg capabilities */
889 	lagg_capabilities(sc);
890 	lagg_linkstate(sc);
891 
892 	return (0);
893 }
894 
895 #ifdef LAGG_PORT_STACKING
896 static int
897 lagg_port_checkstacking(struct lagg_softc *sc)
898 {
899 	struct lagg_softc *sc_ptr;
900 	struct lagg_port *lp;
901 	int m = 0;
902 
903 	LAGG_SXLOCK_ASSERT(sc);
904 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
905 		if (lp->lp_flags & LAGG_PORT_STACK) {
906 			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
907 			m = MAX(m, lagg_port_checkstacking(sc_ptr));
908 		}
909 	}
910 
911 	return (m + 1);
912 }
913 #endif
914 
915 static void
916 lagg_port_destroy_cb(epoch_context_t ec)
917 {
918 	struct lagg_port *lp;
919 	struct ifnet *ifp;
920 
921 	lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
922 	ifp = lp->lp_ifp;
923 
924 	if_rele(ifp);
925 	free(lp, M_LAGG);
926 }
927 
928 static int
929 lagg_port_destroy(struct lagg_port *lp, int rundelport)
930 {
931 	struct lagg_softc *sc = lp->lp_softc;
932 	struct lagg_port *lp_ptr, *lp0;
933 	struct ifnet *ifp = lp->lp_ifp;
934 	uint64_t *pval, vdiff;
935 	int i;
936 
937 	LAGG_XLOCK_ASSERT(sc);
938 
939 	if (rundelport)
940 		lagg_proto_delport(sc, lp);
941 
942 	if (lp->lp_detaching == 0)
943 		lagg_clrmulti(lp);
944 
945 	/* Restore interface */
946 	ifp->if_type = lp->lp_iftype;
947 	ifp->if_ioctl = lp->lp_ioctl;
948 	ifp->if_output = lp->lp_output;
949 	ifp->if_lagg = NULL;
950 
951 	/* Update detached port counters */
952 	pval = lp->port_counters.val;
953 	for (i = 0; i < IFCOUNTERS; i++, pval++) {
954 		vdiff = ifp->if_get_counter(ifp, i) - *pval;
955 		sc->detached_counters.val[i] += vdiff;
956 	}
957 
958 	/* Finally, remove the port from the lagg */
959 	CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
960 	sc->sc_count--;
961 
962 	/* Update the primary interface */
963 	if (lp == sc->sc_primary) {
964 		uint8_t lladdr[LAGG_ADDR_LEN];
965 
966 		if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
967 			bzero(&lladdr, LAGG_ADDR_LEN);
968 		else
969 			bcopy(lp0->lp_lladdr, lladdr, LAGG_ADDR_LEN);
970 		sc->sc_primary = lp0;
971 		if (sc->sc_destroying == 0) {
972 			bcopy(lladdr, IF_LLADDR(sc->sc_ifp), sc->sc_ifp->if_addrlen);
973 			lagg_proto_lladdr(sc);
974 			EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
975 		}
976 
977 		/*
978 		 * Update lladdr for each port (new primary needs update
979 		 * as well, to switch from old lladdr to its 'real' one)
980 		 */
981 		CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
982 			if_setlladdr(lp_ptr->lp_ifp, lladdr, lp_ptr->lp_ifp->if_addrlen);
983 	}
984 
985 	if (lp->lp_ifflags)
986 		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
987 
988 	if (lp->lp_detaching == 0) {
989 		lagg_setflags(lp, 0);
990 		lagg_setcaps(lp, lp->lp_ifcapenable);
991 		if_setlladdr(ifp, lp->lp_lladdr, ifp->if_addrlen);
992 	}
993 
994 	/*
995 	 * free port and release it's ifnet reference after a grace period has
996 	 * elapsed.
997 	 */
998 	NET_EPOCH_CALL(lagg_port_destroy_cb, &lp->lp_epoch_ctx);
999 	/* Update lagg capabilities */
1000 	lagg_capabilities(sc);
1001 	lagg_linkstate(sc);
1002 
1003 	return (0);
1004 }
1005 
1006 static int
1007 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1008 {
1009 	struct epoch_tracker et;
1010 	struct lagg_reqport *rp = (struct lagg_reqport *)data;
1011 	struct lagg_softc *sc;
1012 	struct lagg_port *lp = NULL;
1013 	int error = 0;
1014 
1015 	/* Should be checked by the caller */
1016 	switch (ifp->if_type) {
1017 	case IFT_IEEE8023ADLAG:
1018 	case IFT_INFINIBANDLAG:
1019 		if ((lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
1020 			goto fallback;
1021 		break;
1022 	default:
1023 		goto fallback;
1024 	}
1025 
1026 	switch (cmd) {
1027 	case SIOCGLAGGPORT:
1028 		if (rp->rp_portname[0] == '\0' ||
1029 		    ifunit(rp->rp_portname) != ifp) {
1030 			error = EINVAL;
1031 			break;
1032 		}
1033 
1034 		NET_EPOCH_ENTER(et);
1035 		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
1036 			error = ENOENT;
1037 			NET_EPOCH_EXIT(et);
1038 			break;
1039 		}
1040 
1041 		lagg_port2req(lp, rp);
1042 		NET_EPOCH_EXIT(et);
1043 		break;
1044 
1045 	case SIOCSIFCAP:
1046 		if (lp->lp_ioctl == NULL) {
1047 			error = EINVAL;
1048 			break;
1049 		}
1050 		error = (*lp->lp_ioctl)(ifp, cmd, data);
1051 		if (error)
1052 			break;
1053 
1054 		/* Update lagg interface capabilities */
1055 		LAGG_XLOCK(sc);
1056 		lagg_capabilities(sc);
1057 		LAGG_XUNLOCK(sc);
1058 		VLAN_CAPABILITIES(sc->sc_ifp);
1059 		break;
1060 
1061 	case SIOCSIFMTU:
1062 		/* Do not allow the MTU to be changed once joined */
1063 		error = EINVAL;
1064 		break;
1065 
1066 	default:
1067 		goto fallback;
1068 	}
1069 
1070 	return (error);
1071 
1072 fallback:
1073 	if (lp != NULL && lp->lp_ioctl != NULL)
1074 		return ((*lp->lp_ioctl)(ifp, cmd, data));
1075 
1076 	return (EINVAL);
1077 }
1078 
1079 /*
1080  * Requests counter @cnt data.
1081  *
1082  * Counter value is calculated the following way:
1083  * 1) for each port, sum  difference between current and "initial" measurements.
1084  * 2) add lagg logical interface counters.
1085  * 3) add data from detached_counters array.
1086  *
1087  * We also do the following things on ports attach/detach:
1088  * 1) On port attach we store all counters it has into port_counter array.
1089  * 2) On port detach we add the different between "initial" and
1090  *   current counters data to detached_counters array.
1091  */
1092 static uint64_t
1093 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1094 {
1095 	struct epoch_tracker et;
1096 	struct lagg_softc *sc;
1097 	struct lagg_port *lp;
1098 	struct ifnet *lpifp;
1099 	uint64_t newval, oldval, vsum;
1100 
1101 	/* Revise this when we've got non-generic counters. */
1102 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1103 
1104 	sc = (struct lagg_softc *)ifp->if_softc;
1105 
1106 	vsum = 0;
1107 	NET_EPOCH_ENTER(et);
1108 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1109 		/* Saved attached value */
1110 		oldval = lp->port_counters.val[cnt];
1111 		/* current value */
1112 		lpifp = lp->lp_ifp;
1113 		newval = lpifp->if_get_counter(lpifp, cnt);
1114 		/* Calculate diff and save new */
1115 		vsum += newval - oldval;
1116 	}
1117 	NET_EPOCH_EXIT(et);
1118 
1119 	/*
1120 	 * Add counter data which might be added by upper
1121 	 * layer protocols operating on logical interface.
1122 	 */
1123 	vsum += if_get_counter_default(ifp, cnt);
1124 
1125 	/*
1126 	 * Add counter data from detached ports counters
1127 	 */
1128 	vsum += sc->detached_counters.val[cnt];
1129 
1130 	return (vsum);
1131 }
1132 
1133 /*
1134  * For direct output to child ports.
1135  */
1136 static int
1137 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1138 	const struct sockaddr *dst, struct route *ro)
1139 {
1140 	struct lagg_port *lp = ifp->if_lagg;
1141 
1142 	switch (dst->sa_family) {
1143 		case pseudo_AF_HDRCMPLT:
1144 		case AF_UNSPEC:
1145 			if (lp != NULL)
1146 				return ((*lp->lp_output)(ifp, m, dst, ro));
1147 	}
1148 
1149 	/* drop any other frames */
1150 	m_freem(m);
1151 	return (ENETDOWN);
1152 }
1153 
1154 static void
1155 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1156 {
1157 	struct lagg_port *lp;
1158 	struct lagg_softc *sc;
1159 
1160 	if ((lp = ifp->if_lagg) == NULL)
1161 		return;
1162 	/* If the ifnet is just being renamed, don't do anything. */
1163 	if (ifp->if_flags & IFF_RENAMING)
1164 		return;
1165 
1166 	sc = lp->lp_softc;
1167 
1168 	LAGG_XLOCK(sc);
1169 	lp->lp_detaching = 1;
1170 	lagg_port_destroy(lp, 1);
1171 	LAGG_XUNLOCK(sc);
1172 	VLAN_CAPABILITIES(sc->sc_ifp);
1173 }
1174 
1175 static void
1176 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1177 {
1178 	struct lagg_softc *sc = lp->lp_softc;
1179 
1180 	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1181 	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1182 	rp->rp_prio = lp->lp_prio;
1183 	rp->rp_flags = lp->lp_flags;
1184 	lagg_proto_portreq(sc, lp, &rp->rp_psc);
1185 
1186 	/* Add protocol specific flags */
1187 	switch (sc->sc_proto) {
1188 		case LAGG_PROTO_FAILOVER:
1189 			if (lp == sc->sc_primary)
1190 				rp->rp_flags |= LAGG_PORT_MASTER;
1191 			if (lp == lagg_link_active(sc, sc->sc_primary))
1192 				rp->rp_flags |= LAGG_PORT_ACTIVE;
1193 			break;
1194 
1195 		case LAGG_PROTO_ROUNDROBIN:
1196 		case LAGG_PROTO_LOADBALANCE:
1197 		case LAGG_PROTO_BROADCAST:
1198 			if (LAGG_PORTACTIVE(lp))
1199 				rp->rp_flags |= LAGG_PORT_ACTIVE;
1200 			break;
1201 
1202 		case LAGG_PROTO_LACP:
1203 			/* LACP has a different definition of active */
1204 			if (lacp_isactive(lp))
1205 				rp->rp_flags |= LAGG_PORT_ACTIVE;
1206 			if (lacp_iscollecting(lp))
1207 				rp->rp_flags |= LAGG_PORT_COLLECTING;
1208 			if (lacp_isdistributing(lp))
1209 				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1210 			break;
1211 	}
1212 
1213 }
1214 
1215 static void
1216 lagg_watchdog_infiniband(void *arg)
1217 {
1218 	struct epoch_tracker et;
1219 	struct lagg_softc *sc;
1220 	struct lagg_port *lp;
1221 	struct ifnet *ifp;
1222 	struct ifnet *lp_ifp;
1223 
1224 	sc = arg;
1225 
1226 	/*
1227 	 * Because infiniband nodes have a fixed MAC address, which is
1228 	 * generated by the so-called GID, we need to regularly update
1229 	 * the link level address of the parent lagg<N> device when
1230 	 * the active port changes. Possibly we could piggy-back on
1231 	 * link up/down events aswell, but using a timer also provides
1232 	 * a guarantee against too frequent events. This operation
1233 	 * does not have to be atomic.
1234 	 */
1235 	NET_EPOCH_ENTER(et);
1236 	lp = lagg_link_active(sc, sc->sc_primary);
1237 	if (lp != NULL) {
1238 		ifp = sc->sc_ifp;
1239 		lp_ifp = lp->lp_ifp;
1240 
1241 		if (ifp != NULL && lp_ifp != NULL &&
1242 		    memcmp(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen) != 0) {
1243 			memcpy(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen);
1244 			CURVNET_SET(ifp->if_vnet);
1245 			EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1246 			CURVNET_RESTORE();
1247 		}
1248 	}
1249 	NET_EPOCH_EXIT(et);
1250 
1251 	callout_reset(&sc->sc_watchdog, hz, &lagg_watchdog_infiniband, arg);
1252 }
1253 
1254 static void
1255 lagg_init(void *xsc)
1256 {
1257 	struct lagg_softc *sc = (struct lagg_softc *)xsc;
1258 	struct ifnet *ifp = sc->sc_ifp;
1259 	struct lagg_port *lp;
1260 
1261 	LAGG_XLOCK(sc);
1262 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1263 		LAGG_XUNLOCK(sc);
1264 		return;
1265 	}
1266 
1267 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1268 
1269 	/*
1270 	 * Update the port lladdrs if needed.
1271 	 * This might be if_setlladdr() notification
1272 	 * that lladdr has been changed.
1273 	 */
1274 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1275 		if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1276 		    ifp->if_addrlen) != 0)
1277 			if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1278 	}
1279 
1280 	lagg_proto_init(sc);
1281 
1282 	if (ifp->if_type == IFT_INFINIBAND) {
1283 		mtx_lock(&sc->sc_mtx);
1284 		lagg_watchdog_infiniband(sc);
1285 		mtx_unlock(&sc->sc_mtx);
1286 	}
1287 
1288 	LAGG_XUNLOCK(sc);
1289 }
1290 
1291 static void
1292 lagg_stop(struct lagg_softc *sc)
1293 {
1294 	struct ifnet *ifp = sc->sc_ifp;
1295 
1296 	LAGG_XLOCK_ASSERT(sc);
1297 
1298 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1299 		return;
1300 
1301 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1302 
1303 	lagg_proto_stop(sc);
1304 
1305 	mtx_lock(&sc->sc_mtx);
1306 	callout_stop(&sc->sc_watchdog);
1307 	mtx_unlock(&sc->sc_mtx);
1308 
1309 	callout_drain(&sc->sc_watchdog);
1310 }
1311 
1312 static int
1313 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1314 {
1315 	struct epoch_tracker et;
1316 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1317 	struct lagg_reqall *ra = (struct lagg_reqall *)data;
1318 	struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1319 	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1320 	struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1321 	struct ifreq *ifr = (struct ifreq *)data;
1322 	struct lagg_port *lp;
1323 	struct ifnet *tpif;
1324 	struct thread *td = curthread;
1325 	char *buf, *outbuf;
1326 	int count, buflen, len, error = 0, oldmtu;
1327 
1328 	bzero(&rpbuf, sizeof(rpbuf));
1329 
1330 	/* XXX: This can race with lagg_clone_destroy. */
1331 
1332 	switch (cmd) {
1333 	case SIOCGLAGG:
1334 		LAGG_XLOCK(sc);
1335 		buflen = sc->sc_count * sizeof(struct lagg_reqport);
1336 		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1337 		ra->ra_proto = sc->sc_proto;
1338 		lagg_proto_request(sc, &ra->ra_psc);
1339 		count = 0;
1340 		buf = outbuf;
1341 		len = min(ra->ra_size, buflen);
1342 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1343 			if (len < sizeof(rpbuf))
1344 				break;
1345 
1346 			lagg_port2req(lp, &rpbuf);
1347 			memcpy(buf, &rpbuf, sizeof(rpbuf));
1348 			count++;
1349 			buf += sizeof(rpbuf);
1350 			len -= sizeof(rpbuf);
1351 		}
1352 		LAGG_XUNLOCK(sc);
1353 		ra->ra_ports = count;
1354 		ra->ra_size = count * sizeof(rpbuf);
1355 		error = copyout(outbuf, ra->ra_port, ra->ra_size);
1356 		free(outbuf, M_TEMP);
1357 		break;
1358 	case SIOCSLAGG:
1359 		error = priv_check(td, PRIV_NET_LAGG);
1360 		if (error)
1361 			break;
1362 		if (ra->ra_proto >= LAGG_PROTO_MAX) {
1363 			error = EPROTONOSUPPORT;
1364 			break;
1365 		}
1366 		/* Infiniband only supports the failover protocol. */
1367 		if (ra->ra_proto != LAGG_PROTO_FAILOVER &&
1368 		    ifp->if_type == IFT_INFINIBAND) {
1369 			error = EPROTONOSUPPORT;
1370 			break;
1371 		}
1372 		LAGG_XLOCK(sc);
1373 		lagg_proto_detach(sc);
1374 		lagg_proto_attach(sc, ra->ra_proto);
1375 		LAGG_XUNLOCK(sc);
1376 		break;
1377 	case SIOCGLAGGOPTS:
1378 		LAGG_XLOCK(sc);
1379 		ro->ro_opts = sc->sc_opts;
1380 		if (sc->sc_proto == LAGG_PROTO_LACP) {
1381 			struct lacp_softc *lsc;
1382 
1383 			lsc = (struct lacp_softc *)sc->sc_psc;
1384 			if (lsc->lsc_debug.lsc_tx_test != 0)
1385 				ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1386 			if (lsc->lsc_debug.lsc_rx_test != 0)
1387 				ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1388 			if (lsc->lsc_strict_mode != 0)
1389 				ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1390 			if (lsc->lsc_fast_timeout != 0)
1391 				ro->ro_opts |= LAGG_OPT_LACP_FAST_TIMO;
1392 
1393 			ro->ro_active = sc->sc_active;
1394 		} else {
1395 			ro->ro_active = 0;
1396 			CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1397 				ro->ro_active += LAGG_PORTACTIVE(lp);
1398 		}
1399 		ro->ro_bkt = sc->sc_stride;
1400 		ro->ro_flapping = sc->sc_flapping;
1401 		ro->ro_flowid_shift = sc->flowid_shift;
1402 		LAGG_XUNLOCK(sc);
1403 		break;
1404 	case SIOCSLAGGOPTS:
1405 		error = priv_check(td, PRIV_NET_LAGG);
1406 		if (error)
1407 			break;
1408 
1409 		/*
1410 		 * The stride option was added without defining a corresponding
1411 		 * LAGG_OPT flag, so handle a non-zero value before checking
1412 		 * anything else to preserve compatibility.
1413 		 */
1414 		LAGG_XLOCK(sc);
1415 		if (ro->ro_opts == 0 && ro->ro_bkt != 0) {
1416 			if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN) {
1417 				LAGG_XUNLOCK(sc);
1418 				error = EINVAL;
1419 				break;
1420 			}
1421 			sc->sc_stride = ro->ro_bkt;
1422 		}
1423 		if (ro->ro_opts == 0) {
1424 			LAGG_XUNLOCK(sc);
1425 			break;
1426 		}
1427 
1428 		/*
1429 		 * Set options.  LACP options are stored in sc->sc_psc,
1430 		 * not in sc_opts.
1431 		 */
1432 		int valid, lacp;
1433 
1434 		switch (ro->ro_opts) {
1435 		case LAGG_OPT_USE_FLOWID:
1436 		case -LAGG_OPT_USE_FLOWID:
1437 		case LAGG_OPT_USE_NUMA:
1438 		case -LAGG_OPT_USE_NUMA:
1439 		case LAGG_OPT_FLOWIDSHIFT:
1440 		case LAGG_OPT_RR_LIMIT:
1441 			valid = 1;
1442 			lacp = 0;
1443 			break;
1444 		case LAGG_OPT_LACP_TXTEST:
1445 		case -LAGG_OPT_LACP_TXTEST:
1446 		case LAGG_OPT_LACP_RXTEST:
1447 		case -LAGG_OPT_LACP_RXTEST:
1448 		case LAGG_OPT_LACP_STRICT:
1449 		case -LAGG_OPT_LACP_STRICT:
1450 		case LAGG_OPT_LACP_FAST_TIMO:
1451 		case -LAGG_OPT_LACP_FAST_TIMO:
1452 			valid = lacp = 1;
1453 			break;
1454 		default:
1455 			valid = lacp = 0;
1456 			break;
1457 		}
1458 
1459 		if (valid == 0 ||
1460 		    (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1461 			/* Invalid combination of options specified. */
1462 			error = EINVAL;
1463 			LAGG_XUNLOCK(sc);
1464 			break;	/* Return from SIOCSLAGGOPTS. */
1465 		}
1466 
1467 		/*
1468 		 * Store new options into sc->sc_opts except for
1469 		 * FLOWIDSHIFT, RR and LACP options.
1470 		 */
1471 		if (lacp == 0) {
1472 			if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1473 				sc->flowid_shift = ro->ro_flowid_shift;
1474 			else if (ro->ro_opts == LAGG_OPT_RR_LIMIT) {
1475 				if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN ||
1476 				    ro->ro_bkt == 0) {
1477 					error = EINVAL;
1478 					LAGG_XUNLOCK(sc);
1479 					break;
1480 				}
1481 				sc->sc_stride = ro->ro_bkt;
1482 			} else if (ro->ro_opts > 0)
1483 				sc->sc_opts |= ro->ro_opts;
1484 			else
1485 				sc->sc_opts &= ~ro->ro_opts;
1486 		} else {
1487 			struct lacp_softc *lsc;
1488 			struct lacp_port *lp;
1489 
1490 			lsc = (struct lacp_softc *)sc->sc_psc;
1491 
1492 			switch (ro->ro_opts) {
1493 			case LAGG_OPT_LACP_TXTEST:
1494 				lsc->lsc_debug.lsc_tx_test = 1;
1495 				break;
1496 			case -LAGG_OPT_LACP_TXTEST:
1497 				lsc->lsc_debug.lsc_tx_test = 0;
1498 				break;
1499 			case LAGG_OPT_LACP_RXTEST:
1500 				lsc->lsc_debug.lsc_rx_test = 1;
1501 				break;
1502 			case -LAGG_OPT_LACP_RXTEST:
1503 				lsc->lsc_debug.lsc_rx_test = 0;
1504 				break;
1505 			case LAGG_OPT_LACP_STRICT:
1506 				lsc->lsc_strict_mode = 1;
1507 				break;
1508 			case -LAGG_OPT_LACP_STRICT:
1509 				lsc->lsc_strict_mode = 0;
1510 				break;
1511 			case LAGG_OPT_LACP_FAST_TIMO:
1512 				LACP_LOCK(lsc);
1513         			LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1514                         		lp->lp_state |= LACP_STATE_TIMEOUT;
1515 				LACP_UNLOCK(lsc);
1516 				lsc->lsc_fast_timeout = 1;
1517 				break;
1518 			case -LAGG_OPT_LACP_FAST_TIMO:
1519 				LACP_LOCK(lsc);
1520         			LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1521                         		lp->lp_state &= ~LACP_STATE_TIMEOUT;
1522 				LACP_UNLOCK(lsc);
1523 				lsc->lsc_fast_timeout = 0;
1524 				break;
1525 			}
1526 		}
1527 		LAGG_XUNLOCK(sc);
1528 		break;
1529 	case SIOCGLAGGFLAGS:
1530 		rf->rf_flags = 0;
1531 		LAGG_XLOCK(sc);
1532 		if (sc->sc_flags & MBUF_HASHFLAG_L2)
1533 			rf->rf_flags |= LAGG_F_HASHL2;
1534 		if (sc->sc_flags & MBUF_HASHFLAG_L3)
1535 			rf->rf_flags |= LAGG_F_HASHL3;
1536 		if (sc->sc_flags & MBUF_HASHFLAG_L4)
1537 			rf->rf_flags |= LAGG_F_HASHL4;
1538 		LAGG_XUNLOCK(sc);
1539 		break;
1540 	case SIOCSLAGGHASH:
1541 		error = priv_check(td, PRIV_NET_LAGG);
1542 		if (error)
1543 			break;
1544 		if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1545 			error = EINVAL;
1546 			break;
1547 		}
1548 		LAGG_XLOCK(sc);
1549 		sc->sc_flags = 0;
1550 		if (rf->rf_flags & LAGG_F_HASHL2)
1551 			sc->sc_flags |= MBUF_HASHFLAG_L2;
1552 		if (rf->rf_flags & LAGG_F_HASHL3)
1553 			sc->sc_flags |= MBUF_HASHFLAG_L3;
1554 		if (rf->rf_flags & LAGG_F_HASHL4)
1555 			sc->sc_flags |= MBUF_HASHFLAG_L4;
1556 		LAGG_XUNLOCK(sc);
1557 		break;
1558 	case SIOCGLAGGPORT:
1559 		if (rp->rp_portname[0] == '\0' ||
1560 		    (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1561 			error = EINVAL;
1562 			break;
1563 		}
1564 
1565 		NET_EPOCH_ENTER(et);
1566 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1567 		    lp->lp_softc != sc) {
1568 			error = ENOENT;
1569 			NET_EPOCH_EXIT(et);
1570 			if_rele(tpif);
1571 			break;
1572 		}
1573 
1574 		lagg_port2req(lp, rp);
1575 		NET_EPOCH_EXIT(et);
1576 		if_rele(tpif);
1577 		break;
1578 	case SIOCSLAGGPORT:
1579 		error = priv_check(td, PRIV_NET_LAGG);
1580 		if (error)
1581 			break;
1582 		if (rp->rp_portname[0] == '\0' ||
1583 		    (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1584 			error = EINVAL;
1585 			break;
1586 		}
1587 #ifdef INET6
1588 		/*
1589 		 * A laggport interface should not have inet6 address
1590 		 * because two interfaces with a valid link-local
1591 		 * scope zone must not be merged in any form.  This
1592 		 * restriction is needed to prevent violation of
1593 		 * link-local scope zone.  Attempts to add a laggport
1594 		 * interface which has inet6 addresses triggers
1595 		 * removal of all inet6 addresses on the member
1596 		 * interface.
1597 		 */
1598 		if (in6ifa_llaonifp(tpif)) {
1599 			in6_ifdetach(tpif);
1600 				if_printf(sc->sc_ifp,
1601 				    "IPv6 addresses on %s have been removed "
1602 				    "before adding it as a member to prevent "
1603 				    "IPv6 address scope violation.\n",
1604 				    tpif->if_xname);
1605 		}
1606 #endif
1607 		oldmtu = ifp->if_mtu;
1608 		LAGG_XLOCK(sc);
1609 		error = lagg_port_create(sc, tpif);
1610 		LAGG_XUNLOCK(sc);
1611 		if_rele(tpif);
1612 
1613 		/*
1614 		 * LAGG MTU may change during addition of the first port.
1615 		 * If it did, do network layer specific procedure.
1616 		 */
1617 		if (ifp->if_mtu != oldmtu) {
1618 #ifdef INET6
1619 			nd6_setmtu(ifp);
1620 #endif
1621 			rt_updatemtu(ifp);
1622 		}
1623 
1624 		VLAN_CAPABILITIES(ifp);
1625 		break;
1626 	case SIOCSLAGGDELPORT:
1627 		error = priv_check(td, PRIV_NET_LAGG);
1628 		if (error)
1629 			break;
1630 		if (rp->rp_portname[0] == '\0' ||
1631 		    (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1632 			error = EINVAL;
1633 			break;
1634 		}
1635 
1636 		LAGG_XLOCK(sc);
1637 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1638 		    lp->lp_softc != sc) {
1639 			error = ENOENT;
1640 			LAGG_XUNLOCK(sc);
1641 			if_rele(tpif);
1642 			break;
1643 		}
1644 
1645 		error = lagg_port_destroy(lp, 1);
1646 		LAGG_XUNLOCK(sc);
1647 		if_rele(tpif);
1648 		VLAN_CAPABILITIES(ifp);
1649 		break;
1650 	case SIOCSIFFLAGS:
1651 		/* Set flags on ports too */
1652 		LAGG_XLOCK(sc);
1653 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1654 			lagg_setflags(lp, 1);
1655 		}
1656 
1657 		if (!(ifp->if_flags & IFF_UP) &&
1658 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1659 			/*
1660 			 * If interface is marked down and it is running,
1661 			 * then stop and disable it.
1662 			 */
1663 			lagg_stop(sc);
1664 			LAGG_XUNLOCK(sc);
1665 		} else if ((ifp->if_flags & IFF_UP) &&
1666 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1667 			/*
1668 			 * If interface is marked up and it is stopped, then
1669 			 * start it.
1670 			 */
1671 			LAGG_XUNLOCK(sc);
1672 			(*ifp->if_init)(sc);
1673 		} else
1674 			LAGG_XUNLOCK(sc);
1675 		break;
1676 	case SIOCADDMULTI:
1677 	case SIOCDELMULTI:
1678 		LAGG_XLOCK(sc);
1679 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1680 			lagg_clrmulti(lp);
1681 			lagg_setmulti(lp);
1682 		}
1683 		LAGG_XUNLOCK(sc);
1684 		error = 0;
1685 		break;
1686 	case SIOCSIFMEDIA:
1687 	case SIOCGIFMEDIA:
1688 		if (ifp->if_type == IFT_INFINIBAND)
1689 			error = EINVAL;
1690 		else
1691 			error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1692 		break;
1693 
1694 	case SIOCSIFCAP:
1695 		LAGG_XLOCK(sc);
1696 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1697 			if (lp->lp_ioctl != NULL)
1698 				(*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1699 		}
1700 		lagg_capabilities(sc);
1701 		LAGG_XUNLOCK(sc);
1702 		VLAN_CAPABILITIES(ifp);
1703 		error = 0;
1704 		break;
1705 
1706 	case SIOCSIFMTU:
1707 		LAGG_XLOCK(sc);
1708 		CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1709 			if (lp->lp_ioctl != NULL)
1710 				error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1711 			else
1712 				error = EINVAL;
1713 			if (error != 0) {
1714 				if_printf(ifp,
1715 				    "failed to change MTU to %d on port %s, "
1716 				    "reverting all ports to original MTU (%d)\n",
1717 				    ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1718 				break;
1719 			}
1720 		}
1721 		if (error == 0) {
1722 			ifp->if_mtu = ifr->ifr_mtu;
1723 		} else {
1724 			/* set every port back to the original MTU */
1725 			ifr->ifr_mtu = ifp->if_mtu;
1726 			CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1727 				if (lp->lp_ioctl != NULL)
1728 					(*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1729 			}
1730 		}
1731 		LAGG_XUNLOCK(sc);
1732 		break;
1733 
1734 	default:
1735 		error = ether_ioctl(ifp, cmd, data);
1736 		break;
1737 	}
1738 	return (error);
1739 }
1740 
1741 #if defined(KERN_TLS) || defined(RATELIMIT)
1742 static inline struct lagg_snd_tag *
1743 mst_to_lst(struct m_snd_tag *mst)
1744 {
1745 
1746 	return (__containerof(mst, struct lagg_snd_tag, com));
1747 }
1748 
1749 /*
1750  * Look up the port used by a specific flow.  This only works for lagg
1751  * protocols with deterministic port mappings (e.g. not roundrobin).
1752  * In addition protocols which use a hash to map flows to ports must
1753  * be configured to use the mbuf flowid rather than hashing packet
1754  * contents.
1755  */
1756 static struct lagg_port *
1757 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype,
1758     uint8_t numa_domain)
1759 {
1760 	struct lagg_softc *sc;
1761 	struct lagg_port *lp;
1762 	struct lagg_lb *lb;
1763 	uint32_t hash, p;
1764 	int err;
1765 
1766 	sc = ifp->if_softc;
1767 
1768 	switch (sc->sc_proto) {
1769 	case LAGG_PROTO_FAILOVER:
1770 		return (lagg_link_active(sc, sc->sc_primary));
1771 	case LAGG_PROTO_LOADBALANCE:
1772 		if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1773 		    flowtype == M_HASHTYPE_NONE)
1774 			return (NULL);
1775 		p = flowid >> sc->flowid_shift;
1776 		p %= sc->sc_count;
1777 		lb = (struct lagg_lb *)sc->sc_psc;
1778 		lp = lb->lb_ports[p];
1779 		return (lagg_link_active(sc, lp));
1780 	case LAGG_PROTO_LACP:
1781 		if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1782 		    flowtype == M_HASHTYPE_NONE)
1783 			return (NULL);
1784 		hash = flowid >> sc->flowid_shift;
1785 		return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, &err));
1786 	default:
1787 		return (NULL);
1788 	}
1789 }
1790 
1791 static int
1792 lagg_snd_tag_alloc(struct ifnet *ifp,
1793     union if_snd_tag_alloc_params *params,
1794     struct m_snd_tag **ppmt)
1795 {
1796 	struct epoch_tracker et;
1797 	struct lagg_snd_tag *lst;
1798 	struct lagg_softc *sc;
1799 	struct lagg_port *lp;
1800 	struct ifnet *lp_ifp;
1801 	int error;
1802 
1803 	sc = ifp->if_softc;
1804 
1805 	NET_EPOCH_ENTER(et);
1806 	lp = lookup_snd_tag_port(ifp, params->hdr.flowid,
1807 	    params->hdr.flowtype, params->hdr.numa_domain);
1808 	if (lp == NULL) {
1809 		NET_EPOCH_EXIT(et);
1810 		return (EOPNOTSUPP);
1811 	}
1812 	if (lp->lp_ifp == NULL) {
1813 		NET_EPOCH_EXIT(et);
1814 		return (EOPNOTSUPP);
1815 	}
1816 	lp_ifp = lp->lp_ifp;
1817 	if_ref(lp_ifp);
1818 	NET_EPOCH_EXIT(et);
1819 
1820 	lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1821 	if (lst == NULL) {
1822 		if_rele(lp_ifp);
1823 		return (ENOMEM);
1824 	}
1825 
1826 	error = m_snd_tag_alloc(lp_ifp, params, &lst->tag);
1827 	if_rele(lp_ifp);
1828 	if (error) {
1829 		free(lst, M_LAGG);
1830 		return (error);
1831 	}
1832 
1833 	m_snd_tag_init(&lst->com, ifp, lst->tag->type);
1834 
1835 	*ppmt = &lst->com;
1836 	return (0);
1837 }
1838 
1839 static int
1840 lagg_snd_tag_modify(struct m_snd_tag *mst,
1841     union if_snd_tag_modify_params *params)
1842 {
1843 	struct lagg_snd_tag *lst;
1844 
1845 	lst = mst_to_lst(mst);
1846 	return (lst->tag->ifp->if_snd_tag_modify(lst->tag, params));
1847 }
1848 
1849 static int
1850 lagg_snd_tag_query(struct m_snd_tag *mst,
1851     union if_snd_tag_query_params *params)
1852 {
1853 	struct lagg_snd_tag *lst;
1854 
1855 	lst = mst_to_lst(mst);
1856 	return (lst->tag->ifp->if_snd_tag_query(lst->tag, params));
1857 }
1858 
1859 static void
1860 lagg_snd_tag_free(struct m_snd_tag *mst)
1861 {
1862 	struct lagg_snd_tag *lst;
1863 
1864 	lst = mst_to_lst(mst);
1865 	m_snd_tag_rele(lst->tag);
1866 	free(lst, M_LAGG);
1867 }
1868 
1869 static void
1870 lagg_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
1871 {
1872 	/*
1873 	 * For lagg, we have an indirect
1874 	 * interface. The caller needs to
1875 	 * get a ratelimit tag on the actual
1876 	 * interface the flow will go on.
1877 	 */
1878 	q->rate_table = NULL;
1879 	q->flags = RT_IS_INDIRECT;
1880 	q->max_flows = 0;
1881 	q->number_of_rates = 0;
1882 }
1883 #endif
1884 
1885 static int
1886 lagg_setmulti(struct lagg_port *lp)
1887 {
1888 	struct lagg_softc *sc = lp->lp_softc;
1889 	struct ifnet *ifp = lp->lp_ifp;
1890 	struct ifnet *scifp = sc->sc_ifp;
1891 	struct lagg_mc *mc;
1892 	struct ifmultiaddr *ifma;
1893 	int error;
1894 
1895 	IF_ADDR_WLOCK(scifp);
1896 	CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1897 		if (ifma->ifma_addr->sa_family != AF_LINK)
1898 			continue;
1899 		mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
1900 		if (mc == NULL) {
1901 			IF_ADDR_WUNLOCK(scifp);
1902 			return (ENOMEM);
1903 		}
1904 		bcopy(ifma->ifma_addr, &mc->mc_addr,
1905 		    ifma->ifma_addr->sa_len);
1906 		mc->mc_addr.sdl_index = ifp->if_index;
1907 		mc->mc_ifma = NULL;
1908 		SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1909 	}
1910 	IF_ADDR_WUNLOCK(scifp);
1911 	SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1912 		error = if_addmulti(ifp,
1913 		    (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1914 		if (error)
1915 			return (error);
1916 	}
1917 	return (0);
1918 }
1919 
1920 static int
1921 lagg_clrmulti(struct lagg_port *lp)
1922 {
1923 	struct lagg_mc *mc;
1924 
1925 	LAGG_XLOCK_ASSERT(lp->lp_softc);
1926 	while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1927 		SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1928 		if (mc->mc_ifma && lp->lp_detaching == 0)
1929 			if_delmulti_ifma(mc->mc_ifma);
1930 		free(mc, M_LAGG);
1931 	}
1932 	return (0);
1933 }
1934 
1935 static int
1936 lagg_setcaps(struct lagg_port *lp, int cap)
1937 {
1938 	struct ifreq ifr;
1939 
1940 	if (lp->lp_ifp->if_capenable == cap)
1941 		return (0);
1942 	if (lp->lp_ioctl == NULL)
1943 		return (ENXIO);
1944 	ifr.ifr_reqcap = cap;
1945 	return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1946 }
1947 
1948 /* Handle a ref counted flag that should be set on the lagg port as well */
1949 static int
1950 lagg_setflag(struct lagg_port *lp, int flag, int status,
1951     int (*func)(struct ifnet *, int))
1952 {
1953 	struct lagg_softc *sc = lp->lp_softc;
1954 	struct ifnet *scifp = sc->sc_ifp;
1955 	struct ifnet *ifp = lp->lp_ifp;
1956 	int error;
1957 
1958 	LAGG_XLOCK_ASSERT(sc);
1959 
1960 	status = status ? (scifp->if_flags & flag) : 0;
1961 	/* Now "status" contains the flag value or 0 */
1962 
1963 	/*
1964 	 * See if recorded ports status is different from what
1965 	 * we want it to be.  If it is, flip it.  We record ports
1966 	 * status in lp_ifflags so that we won't clear ports flag
1967 	 * we haven't set.  In fact, we don't clear or set ports
1968 	 * flags directly, but get or release references to them.
1969 	 * That's why we can be sure that recorded flags still are
1970 	 * in accord with actual ports flags.
1971 	 */
1972 	if (status != (lp->lp_ifflags & flag)) {
1973 		error = (*func)(ifp, status);
1974 		if (error)
1975 			return (error);
1976 		lp->lp_ifflags &= ~flag;
1977 		lp->lp_ifflags |= status;
1978 	}
1979 	return (0);
1980 }
1981 
1982 /*
1983  * Handle IFF_* flags that require certain changes on the lagg port
1984  * if "status" is true, update ports flags respective to the lagg
1985  * if "status" is false, forcedly clear the flags set on port.
1986  */
1987 static int
1988 lagg_setflags(struct lagg_port *lp, int status)
1989 {
1990 	int error, i;
1991 
1992 	for (i = 0; lagg_pflags[i].flag; i++) {
1993 		error = lagg_setflag(lp, lagg_pflags[i].flag,
1994 		    status, lagg_pflags[i].func);
1995 		if (error)
1996 			return (error);
1997 	}
1998 	return (0);
1999 }
2000 
2001 static int
2002 lagg_transmit_ethernet(struct ifnet *ifp, struct mbuf *m)
2003 {
2004 	struct epoch_tracker et;
2005 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2006 	int error;
2007 
2008 #if defined(KERN_TLS) || defined(RATELIMIT)
2009 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2010 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2011 #endif
2012 	NET_EPOCH_ENTER(et);
2013 	/* We need a Tx algorithm and at least one port */
2014 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2015 		NET_EPOCH_EXIT(et);
2016 		m_freem(m);
2017 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2018 		return (ENXIO);
2019 	}
2020 
2021 	ETHER_BPF_MTAP(ifp, m);
2022 
2023 	error = lagg_proto_start(sc, m);
2024 	NET_EPOCH_EXIT(et);
2025 	return (error);
2026 }
2027 
2028 static int
2029 lagg_transmit_infiniband(struct ifnet *ifp, struct mbuf *m)
2030 {
2031 	struct epoch_tracker et;
2032 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2033 	int error;
2034 
2035 #if defined(KERN_TLS) || defined(RATELIMIT)
2036 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2037 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2038 #endif
2039 	NET_EPOCH_ENTER(et);
2040 	/* We need a Tx algorithm and at least one port */
2041 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2042 		NET_EPOCH_EXIT(et);
2043 		m_freem(m);
2044 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2045 		return (ENXIO);
2046 	}
2047 
2048 	INFINIBAND_BPF_MTAP(ifp, m);
2049 
2050 	error = lagg_proto_start(sc, m);
2051 	NET_EPOCH_EXIT(et);
2052 	return (error);
2053 }
2054 
2055 /*
2056  * The ifp->if_qflush entry point for lagg(4) is no-op.
2057  */
2058 static void
2059 lagg_qflush(struct ifnet *ifp __unused)
2060 {
2061 }
2062 
2063 static struct mbuf *
2064 lagg_input_ethernet(struct ifnet *ifp, struct mbuf *m)
2065 {
2066 	struct epoch_tracker et;
2067 	struct lagg_port *lp = ifp->if_lagg;
2068 	struct lagg_softc *sc = lp->lp_softc;
2069 	struct ifnet *scifp = sc->sc_ifp;
2070 
2071 	NET_EPOCH_ENTER(et);
2072 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2073 	    lp->lp_detaching != 0 ||
2074 	    sc->sc_proto == LAGG_PROTO_NONE) {
2075 		NET_EPOCH_EXIT(et);
2076 		m_freem(m);
2077 		return (NULL);
2078 	}
2079 
2080 	ETHER_BPF_MTAP(scifp, m);
2081 
2082 	m = lagg_proto_input(sc, lp, m);
2083 	if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
2084 		m_freem(m);
2085 		m = NULL;
2086 	}
2087 
2088 	NET_EPOCH_EXIT(et);
2089 	return (m);
2090 }
2091 
2092 static struct mbuf *
2093 lagg_input_infiniband(struct ifnet *ifp, struct mbuf *m)
2094 {
2095 	struct epoch_tracker et;
2096 	struct lagg_port *lp = ifp->if_lagg;
2097 	struct lagg_softc *sc = lp->lp_softc;
2098 	struct ifnet *scifp = sc->sc_ifp;
2099 
2100 	NET_EPOCH_ENTER(et);
2101 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2102 	    lp->lp_detaching != 0 ||
2103 	    sc->sc_proto == LAGG_PROTO_NONE) {
2104 		NET_EPOCH_EXIT(et);
2105 		m_freem(m);
2106 		return (NULL);
2107 	}
2108 
2109 	INFINIBAND_BPF_MTAP(scifp, m);
2110 
2111 	m = lagg_proto_input(sc, lp, m);
2112 	if (m != NULL && (scifp->if_flags & IFF_MONITOR) != 0) {
2113 		m_freem(m);
2114 		m = NULL;
2115 	}
2116 
2117 	NET_EPOCH_EXIT(et);
2118 	return (m);
2119 }
2120 
2121 static int
2122 lagg_media_change(struct ifnet *ifp)
2123 {
2124 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2125 
2126 	if (sc->sc_ifflags & IFF_DEBUG)
2127 		printf("%s\n", __func__);
2128 
2129 	/* Ignore */
2130 	return (0);
2131 }
2132 
2133 static void
2134 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2135 {
2136 	struct epoch_tracker et;
2137 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2138 	struct lagg_port *lp;
2139 
2140 	imr->ifm_status = IFM_AVALID;
2141 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
2142 
2143 	NET_EPOCH_ENTER(et);
2144 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2145 		if (LAGG_PORTACTIVE(lp))
2146 			imr->ifm_status |= IFM_ACTIVE;
2147 	}
2148 	NET_EPOCH_EXIT(et);
2149 }
2150 
2151 static void
2152 lagg_linkstate(struct lagg_softc *sc)
2153 {
2154 	struct epoch_tracker et;
2155 	struct lagg_port *lp;
2156 	int new_link = LINK_STATE_DOWN;
2157 	uint64_t speed;
2158 
2159 	LAGG_XLOCK_ASSERT(sc);
2160 
2161 	/* LACP handles link state itself */
2162 	if (sc->sc_proto == LAGG_PROTO_LACP)
2163 		return;
2164 
2165 	/* Our link is considered up if at least one of our ports is active */
2166 	NET_EPOCH_ENTER(et);
2167 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2168 		if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
2169 			new_link = LINK_STATE_UP;
2170 			break;
2171 		}
2172 	}
2173 	NET_EPOCH_EXIT(et);
2174 	if_link_state_change(sc->sc_ifp, new_link);
2175 
2176 	/* Update if_baudrate to reflect the max possible speed */
2177 	switch (sc->sc_proto) {
2178 		case LAGG_PROTO_FAILOVER:
2179 			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
2180 			    sc->sc_primary->lp_ifp->if_baudrate : 0;
2181 			break;
2182 		case LAGG_PROTO_ROUNDROBIN:
2183 		case LAGG_PROTO_LOADBALANCE:
2184 		case LAGG_PROTO_BROADCAST:
2185 			speed = 0;
2186 			NET_EPOCH_ENTER(et);
2187 			CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2188 				speed += lp->lp_ifp->if_baudrate;
2189 			NET_EPOCH_EXIT(et);
2190 			sc->sc_ifp->if_baudrate = speed;
2191 			break;
2192 		case LAGG_PROTO_LACP:
2193 			/* LACP updates if_baudrate itself */
2194 			break;
2195 	}
2196 }
2197 
2198 static void
2199 lagg_port_state(struct ifnet *ifp, int state)
2200 {
2201 	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
2202 	struct lagg_softc *sc = NULL;
2203 
2204 	if (lp != NULL)
2205 		sc = lp->lp_softc;
2206 	if (sc == NULL)
2207 		return;
2208 
2209 	LAGG_XLOCK(sc);
2210 	lagg_linkstate(sc);
2211 	lagg_proto_linkstate(sc, lp);
2212 	LAGG_XUNLOCK(sc);
2213 }
2214 
2215 struct lagg_port *
2216 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
2217 {
2218 	struct lagg_port *lp_next, *rval = NULL;
2219 
2220 	/*
2221 	 * Search a port which reports an active link state.
2222 	 */
2223 
2224 #ifdef INVARIANTS
2225 	/*
2226 	 * This is called with either in the network epoch
2227 	 * or with LAGG_XLOCK(sc) held.
2228 	 */
2229 	if (!in_epoch(net_epoch_preempt))
2230 		LAGG_XLOCK_ASSERT(sc);
2231 #endif
2232 
2233 	if (lp == NULL)
2234 		goto search;
2235 	if (LAGG_PORTACTIVE(lp)) {
2236 		rval = lp;
2237 		goto found;
2238 	}
2239 	if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
2240 	    LAGG_PORTACTIVE(lp_next)) {
2241 		rval = lp_next;
2242 		goto found;
2243 	}
2244 
2245 search:
2246 	CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2247 		if (LAGG_PORTACTIVE(lp_next)) {
2248 			return (lp_next);
2249 		}
2250 	}
2251 found:
2252 	return (rval);
2253 }
2254 
2255 int
2256 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2257 {
2258 
2259 #if defined(KERN_TLS) || defined(RATELIMIT)
2260 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2261 		struct lagg_snd_tag *lst;
2262 		struct m_snd_tag *mst;
2263 
2264 		mst = m->m_pkthdr.snd_tag;
2265 		lst = mst_to_lst(mst);
2266 		if (lst->tag->ifp != ifp) {
2267 			m_freem(m);
2268 			return (EAGAIN);
2269 		}
2270 		m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2271 		m_snd_tag_rele(mst);
2272 	}
2273 #endif
2274 	return (ifp->if_transmit)(ifp, m);
2275 }
2276 
2277 /*
2278  * Simple round robin aggregation
2279  */
2280 static void
2281 lagg_rr_attach(struct lagg_softc *sc)
2282 {
2283 	sc->sc_seq = 0;
2284 	sc->sc_stride = 1;
2285 }
2286 
2287 static int
2288 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2289 {
2290 	struct lagg_port *lp;
2291 	uint32_t p;
2292 
2293 	p = atomic_fetchadd_32(&sc->sc_seq, 1);
2294 	p /= sc->sc_stride;
2295 	p %= sc->sc_count;
2296 	lp = CK_SLIST_FIRST(&sc->sc_ports);
2297 
2298 	while (p--)
2299 		lp = CK_SLIST_NEXT(lp, lp_entries);
2300 
2301 	/*
2302 	 * Check the port's link state. This will return the next active
2303 	 * port if the link is down or the port is NULL.
2304 	 */
2305 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
2306 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2307 		m_freem(m);
2308 		return (ENETDOWN);
2309 	}
2310 
2311 	/* Send mbuf */
2312 	return (lagg_enqueue(lp->lp_ifp, m));
2313 }
2314 
2315 static struct mbuf *
2316 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2317 {
2318 	struct ifnet *ifp = sc->sc_ifp;
2319 
2320 	/* Just pass in the packet to our lagg device */
2321 	m->m_pkthdr.rcvif = ifp;
2322 
2323 	return (m);
2324 }
2325 
2326 /*
2327  * Broadcast mode
2328  */
2329 static int
2330 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2331 {
2332 	int active_ports = 0;
2333 	int errors = 0;
2334 	int ret;
2335 	struct lagg_port *lp, *last = NULL;
2336 	struct mbuf *m0;
2337 
2338 	NET_EPOCH_ASSERT();
2339 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2340 		if (!LAGG_PORTACTIVE(lp))
2341 			continue;
2342 
2343 		active_ports++;
2344 
2345 		if (last != NULL) {
2346 			m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2347 			if (m0 == NULL) {
2348 				ret = ENOBUFS;
2349 				errors++;
2350 				break;
2351 			}
2352 			lagg_enqueue(last->lp_ifp, m0);
2353 		}
2354 		last = lp;
2355 	}
2356 
2357 	if (last == NULL) {
2358 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2359 		m_freem(m);
2360 		return (ENOENT);
2361 	}
2362 	if ((last = lagg_link_active(sc, last)) == NULL) {
2363 		errors++;
2364 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2365 		m_freem(m);
2366 		return (ENETDOWN);
2367 	}
2368 
2369 	ret = lagg_enqueue(last->lp_ifp, m);
2370 	if (errors != 0)
2371 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2372 
2373 	return (ret);
2374 }
2375 
2376 static struct mbuf*
2377 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2378 {
2379 	struct ifnet *ifp = sc->sc_ifp;
2380 
2381 	/* Just pass in the packet to our lagg device */
2382 	m->m_pkthdr.rcvif = ifp;
2383 	return (m);
2384 }
2385 
2386 /*
2387  * Active failover
2388  */
2389 static int
2390 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2391 {
2392 	struct lagg_port *lp;
2393 
2394 	/* Use the master port if active or the next available port */
2395 	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2396 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2397 		m_freem(m);
2398 		return (ENETDOWN);
2399 	}
2400 
2401 	/* Send mbuf */
2402 	return (lagg_enqueue(lp->lp_ifp, m));
2403 }
2404 
2405 static struct mbuf *
2406 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2407 {
2408 	struct ifnet *ifp = sc->sc_ifp;
2409 	struct lagg_port *tmp_tp;
2410 
2411 	if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2412 		m->m_pkthdr.rcvif = ifp;
2413 		return (m);
2414 	}
2415 
2416 	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2417 		tmp_tp = lagg_link_active(sc, sc->sc_primary);
2418 		/*
2419 		 * If tmp_tp is null, we've received a packet when all
2420 		 * our links are down. Weird, but process it anyways.
2421 		 */
2422 		if ((tmp_tp == NULL || tmp_tp == lp)) {
2423 			m->m_pkthdr.rcvif = ifp;
2424 			return (m);
2425 		}
2426 	}
2427 
2428 	m_freem(m);
2429 	return (NULL);
2430 }
2431 
2432 /*
2433  * Loadbalancing
2434  */
2435 static void
2436 lagg_lb_attach(struct lagg_softc *sc)
2437 {
2438 	struct lagg_port *lp;
2439 	struct lagg_lb *lb;
2440 
2441 	LAGG_XLOCK_ASSERT(sc);
2442 	lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2443 	lb->lb_key = m_ether_tcpip_hash_init();
2444 	sc->sc_psc = lb;
2445 
2446 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2447 		lagg_lb_port_create(lp);
2448 }
2449 
2450 static void
2451 lagg_lb_detach(struct lagg_softc *sc)
2452 {
2453 	struct lagg_lb *lb;
2454 
2455 	lb = (struct lagg_lb *)sc->sc_psc;
2456 	if (lb != NULL)
2457 		free(lb, M_LAGG);
2458 }
2459 
2460 static int
2461 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2462 {
2463 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2464 	struct lagg_port *lp_next;
2465 	int i = 0, rv;
2466 
2467 	rv = 0;
2468 	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2469 	LAGG_XLOCK_ASSERT(sc);
2470 	CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2471 		if (lp_next == lp)
2472 			continue;
2473 		if (i >= LAGG_MAX_PORTS) {
2474 			rv = EINVAL;
2475 			break;
2476 		}
2477 		if (sc->sc_ifflags & IFF_DEBUG)
2478 			printf("%s: port %s at index %d\n",
2479 			    sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2480 		lb->lb_ports[i++] = lp_next;
2481 	}
2482 
2483 	return (rv);
2484 }
2485 
2486 static int
2487 lagg_lb_port_create(struct lagg_port *lp)
2488 {
2489 	struct lagg_softc *sc = lp->lp_softc;
2490 	return (lagg_lb_porttable(sc, NULL));
2491 }
2492 
2493 static void
2494 lagg_lb_port_destroy(struct lagg_port *lp)
2495 {
2496 	struct lagg_softc *sc = lp->lp_softc;
2497 	lagg_lb_porttable(sc, lp);
2498 }
2499 
2500 static int
2501 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2502 {
2503 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2504 	struct lagg_port *lp = NULL;
2505 	uint32_t p = 0;
2506 
2507 	if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2508 	    M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2509 		p = m->m_pkthdr.flowid >> sc->flowid_shift;
2510 	else
2511 		p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2512 	p %= sc->sc_count;
2513 	lp = lb->lb_ports[p];
2514 
2515 	/*
2516 	 * Check the port's link state. This will return the next active
2517 	 * port if the link is down or the port is NULL.
2518 	 */
2519 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
2520 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2521 		m_freem(m);
2522 		return (ENETDOWN);
2523 	}
2524 
2525 	/* Send mbuf */
2526 	return (lagg_enqueue(lp->lp_ifp, m));
2527 }
2528 
2529 static struct mbuf *
2530 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2531 {
2532 	struct ifnet *ifp = sc->sc_ifp;
2533 
2534 	/* Just pass in the packet to our lagg device */
2535 	m->m_pkthdr.rcvif = ifp;
2536 
2537 	return (m);
2538 }
2539 
2540 /*
2541  * 802.3ad LACP
2542  */
2543 static void
2544 lagg_lacp_attach(struct lagg_softc *sc)
2545 {
2546 	struct lagg_port *lp;
2547 
2548 	lacp_attach(sc);
2549 	LAGG_XLOCK_ASSERT(sc);
2550 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2551 		lacp_port_create(lp);
2552 }
2553 
2554 static void
2555 lagg_lacp_detach(struct lagg_softc *sc)
2556 {
2557 	struct lagg_port *lp;
2558 	void *psc;
2559 
2560 	LAGG_XLOCK_ASSERT(sc);
2561 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2562 		lacp_port_destroy(lp);
2563 
2564 	psc = sc->sc_psc;
2565 	sc->sc_psc = NULL;
2566 	lacp_detach(psc);
2567 }
2568 
2569 static void
2570 lagg_lacp_lladdr(struct lagg_softc *sc)
2571 {
2572 	struct lagg_port *lp;
2573 
2574 	LAGG_SXLOCK_ASSERT(sc);
2575 
2576 	/* purge all the lacp ports */
2577 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2578 		lacp_port_destroy(lp);
2579 
2580 	/* add them back in */
2581 	CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2582 		lacp_port_create(lp);
2583 }
2584 
2585 static int
2586 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2587 {
2588 	struct lagg_port *lp;
2589 	int err;
2590 
2591 	lp = lacp_select_tx_port(sc, m, &err);
2592 	if (lp == NULL) {
2593 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2594 		m_freem(m);
2595 		return (err);
2596 	}
2597 
2598 	/* Send mbuf */
2599 	return (lagg_enqueue(lp->lp_ifp, m));
2600 }
2601 
2602 static struct mbuf *
2603 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2604 {
2605 	struct ifnet *ifp = sc->sc_ifp;
2606 	struct ether_header *eh;
2607 	u_short etype;
2608 
2609 	eh = mtod(m, struct ether_header *);
2610 	etype = ntohs(eh->ether_type);
2611 
2612 	/* Tap off LACP control messages */
2613 	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2614 		m = lacp_input(lp, m);
2615 		if (m == NULL)
2616 			return (NULL);
2617 	}
2618 
2619 	/*
2620 	 * If the port is not collecting or not in the active aggregator then
2621 	 * free and return.
2622 	 */
2623 	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2624 		m_freem(m);
2625 		return (NULL);
2626 	}
2627 
2628 	m->m_pkthdr.rcvif = ifp;
2629 	return (m);
2630 }
2631