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