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