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