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