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