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