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