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