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