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