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