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