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