]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_lagg.c
Persistently store NIC's hardware MAC address, and add a way to retrive it
[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) = 1;
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 = ~0, ena = ~0;
576         u_long hwa = ~0UL;
577         struct ifnet_hw_tsomax hw_tsomax;
578
579         LAGG_XLOCK_ASSERT(sc);
580
581         memset(&hw_tsomax, 0, sizeof(hw_tsomax));
582
583         /* Get capabilities from the lagg ports */
584         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
585                 cap &= lp->lp_ifp->if_capabilities;
586                 ena &= lp->lp_ifp->if_capenable;
587                 hwa &= lp->lp_ifp->if_hwassist;
588                 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
589         }
590         cap = (cap == ~0 ? 0 : cap);
591         ena = (ena == ~0 ? 0 : ena);
592         hwa = (hwa == ~0 ? 0 : hwa);
593
594         if (sc->sc_ifp->if_capabilities != cap ||
595             sc->sc_ifp->if_capenable != ena ||
596             sc->sc_ifp->if_hwassist != hwa ||
597             if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
598                 sc->sc_ifp->if_capabilities = cap;
599                 sc->sc_ifp->if_capenable = ena;
600                 sc->sc_ifp->if_hwassist = hwa;
601                 getmicrotime(&sc->sc_ifp->if_lastchange);
602
603                 if (sc->sc_ifflags & IFF_DEBUG)
604                         if_printf(sc->sc_ifp,
605                             "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
606         }
607
608         /* Apply unified capabilities back to the lagg ports. */
609         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
610                 lagg_setcaps(lp, ena);
611 }
612
613 static int
614 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
615 {
616         struct lagg_softc *sc_ptr;
617         struct lagg_port *lp, *tlp;
618         int error, i;
619         uint64_t *pval;
620
621         LAGG_XLOCK_ASSERT(sc);
622
623         /* Limit the maximal number of lagg ports */
624         if (sc->sc_count >= LAGG_MAX_PORTS)
625                 return (ENOSPC);
626
627         /* Check if port has already been associated to a lagg */
628         if (ifp->if_lagg != NULL) {
629                 /* Port is already in the current lagg? */
630                 lp = (struct lagg_port *)ifp->if_lagg;
631                 if (lp->lp_softc == sc)
632                         return (EEXIST);
633                 return (EBUSY);
634         }
635
636         /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
637         if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
638                 return (EPROTONOSUPPORT);
639
640         /* Allow the first Ethernet member to define the MTU */
641         if (SLIST_EMPTY(&sc->sc_ports))
642                 sc->sc_ifp->if_mtu = ifp->if_mtu;
643         else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
644                 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
645                     ifp->if_xname);
646                 return (EINVAL);
647         }
648
649         lp = malloc(sizeof(struct lagg_port), M_DEVBUF, M_WAITOK|M_ZERO);
650         lp->lp_softc = sc;
651
652         /* Check if port is a stacked lagg */
653         LAGG_LIST_LOCK();
654         SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
655                 if (ifp == sc_ptr->sc_ifp) {
656                         LAGG_LIST_UNLOCK();
657                         free(lp, M_DEVBUF);
658                         return (EINVAL);
659                         /* XXX disable stacking for the moment, its untested */
660 #ifdef LAGG_PORT_STACKING
661                         lp->lp_flags |= LAGG_PORT_STACK;
662                         if (lagg_port_checkstacking(sc_ptr) >=
663                             LAGG_MAX_STACKING) {
664                                 LAGG_LIST_UNLOCK();
665                                 free(lp, M_DEVBUF);
666                                 return (E2BIG);
667                         }
668 #endif
669                 }
670         }
671         LAGG_LIST_UNLOCK();
672
673         if_ref(ifp);
674         lp->lp_ifp = ifp;
675
676         bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
677         lp->lp_ifcapenable = ifp->if_capenable;
678         if (SLIST_EMPTY(&sc->sc_ports)) {
679                 LAGG_WLOCK(sc);
680                 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
681                 lagg_proto_lladdr(sc);
682                 LAGG_WUNLOCK(sc);
683                 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
684         } else {
685                 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
686         }
687         lagg_setflags(lp, 1);
688
689         LAGG_WLOCK(sc);
690         if (SLIST_EMPTY(&sc->sc_ports))
691                 sc->sc_primary = lp;
692
693         /* Change the interface type */
694         lp->lp_iftype = ifp->if_type;
695         ifp->if_type = IFT_IEEE8023ADLAG;
696         ifp->if_lagg = lp;
697         lp->lp_ioctl = ifp->if_ioctl;
698         ifp->if_ioctl = lagg_port_ioctl;
699         lp->lp_output = ifp->if_output;
700         ifp->if_output = lagg_port_output;
701
702         /* Read port counters */
703         pval = lp->port_counters.val;
704         for (i = 0; i < IFCOUNTERS; i++, pval++)
705                 *pval = ifp->if_get_counter(ifp, i);
706
707         /*
708          * Insert into the list of ports.
709          * Keep ports sorted by if_index. It is handy, when configuration
710          * is predictable and `ifconfig laggN create ...` command
711          * will lead to the same result each time.
712          */
713         SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
714                 if (tlp->lp_ifp->if_index < ifp->if_index && (
715                     SLIST_NEXT(tlp, lp_entries) == NULL ||
716                     SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index >
717                     ifp->if_index))
718                         break;
719         }
720         if (tlp != NULL)
721                 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
722         else
723                 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
724         sc->sc_count++;
725
726         lagg_setmulti(lp);
727
728         if ((error = lagg_proto_addport(sc, lp)) != 0) {
729                 /* Remove the port, without calling pr_delport. */
730                 lagg_port_destroy(lp, 0);
731                 LAGG_UNLOCK_ASSERT(sc);
732                 return (error);
733         }
734
735         LAGG_WUNLOCK(sc);
736
737         /* Update lagg capabilities */
738         lagg_capabilities(sc);
739         lagg_linkstate(sc);
740
741         return (0);
742 }
743
744 #ifdef LAGG_PORT_STACKING
745 static int
746 lagg_port_checkstacking(struct lagg_softc *sc)
747 {
748         struct lagg_softc *sc_ptr;
749         struct lagg_port *lp;
750         int m = 0;
751
752         LAGG_SXLOCK_ASSERT(sc);
753         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
754                 if (lp->lp_flags & LAGG_PORT_STACK) {
755                         sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
756                         m = MAX(m, lagg_port_checkstacking(sc_ptr));
757                 }
758         }
759
760         return (m + 1);
761 }
762 #endif
763
764 static int
765 lagg_port_destroy(struct lagg_port *lp, int rundelport)
766 {
767         struct lagg_softc *sc = lp->lp_softc;
768         struct lagg_port *lp_ptr, *lp0;
769         struct ifnet *ifp = lp->lp_ifp;
770         uint64_t *pval, vdiff;
771         int i;
772
773         LAGG_XLOCK_ASSERT(sc);
774
775         if (rundelport) {
776                 LAGG_WLOCK(sc);
777                 lagg_proto_delport(sc, lp);
778         } else
779                 LAGG_WLOCK_ASSERT(sc);
780
781         if (lp->lp_detaching == 0)
782                 lagg_clrmulti(lp);
783
784         /* Restore interface */
785         ifp->if_type = lp->lp_iftype;
786         ifp->if_ioctl = lp->lp_ioctl;
787         ifp->if_output = lp->lp_output;
788         ifp->if_lagg = NULL;
789
790         /* Update detached port counters */
791         pval = lp->port_counters.val;
792         for (i = 0; i < IFCOUNTERS; i++, pval++) {
793                 vdiff = ifp->if_get_counter(ifp, i) - *pval;
794                 sc->detached_counters.val[i] += vdiff;
795         }
796
797         /* Finally, remove the port from the lagg */
798         SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
799         sc->sc_count--;
800
801         /* Update the primary interface */
802         if (lp == sc->sc_primary) {
803                 uint8_t lladdr[ETHER_ADDR_LEN];
804
805                 if ((lp0 = SLIST_FIRST(&sc->sc_ports)) == NULL)
806                         bzero(&lladdr, ETHER_ADDR_LEN);
807                 else
808                         bcopy(lp0->lp_lladdr, lladdr, ETHER_ADDR_LEN);
809                 sc->sc_primary = lp0;
810                 if (sc->sc_destroying == 0) {
811                         bcopy(lladdr, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
812                         lagg_proto_lladdr(sc);
813                         LAGG_WUNLOCK(sc);
814                         EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
815                 } else
816                         LAGG_WUNLOCK(sc);
817
818                 /*
819                  * Update lladdr for each port (new primary needs update
820                  * as well, to switch from old lladdr to its 'real' one)
821                  */
822                 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
823                         if_setlladdr(lp_ptr->lp_ifp, lladdr, ETHER_ADDR_LEN);
824         } else
825                 LAGG_WUNLOCK(sc);
826
827         if (lp->lp_ifflags)
828                 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
829
830         if (lp->lp_detaching == 0) {
831                 lagg_setflags(lp, 0);
832                 lagg_setcaps(lp, lp->lp_ifcapenable);
833                 if_setlladdr(ifp, lp->lp_lladdr, ETHER_ADDR_LEN);
834         }
835
836         if_rele(ifp);
837         free(lp, M_DEVBUF);
838
839         /* Update lagg capabilities */
840         lagg_capabilities(sc);
841         lagg_linkstate(sc);
842
843         return (0);
844 }
845
846 static int
847 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
848 {
849         struct lagg_reqport *rp = (struct lagg_reqport *)data;
850         struct lagg_softc *sc;
851         struct lagg_port *lp = NULL;
852         int error = 0;
853
854         /* Should be checked by the caller */
855         if (ifp->if_type != IFT_IEEE8023ADLAG ||
856             (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
857                 goto fallback;
858
859         switch (cmd) {
860         case SIOCGLAGGPORT:
861                 if (rp->rp_portname[0] == '\0' ||
862                     ifunit(rp->rp_portname) != ifp) {
863                         error = EINVAL;
864                         break;
865                 }
866
867                 LAGG_SLOCK(sc);
868                 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
869                         error = ENOENT;
870                         LAGG_SUNLOCK(sc);
871                         break;
872                 }
873
874                 lagg_port2req(lp, rp);
875                 LAGG_SUNLOCK(sc);
876                 break;
877
878         case SIOCSIFCAP:
879                 if (lp->lp_ioctl == NULL) {
880                         error = EINVAL;
881                         break;
882                 }
883                 error = (*lp->lp_ioctl)(ifp, cmd, data);
884                 if (error)
885                         break;
886
887                 /* Update lagg interface capabilities */
888                 LAGG_XLOCK(sc);
889                 lagg_capabilities(sc);
890                 LAGG_XUNLOCK(sc);
891                 break;
892
893         case SIOCSIFMTU:
894                 /* Do not allow the MTU to be changed once joined */
895                 error = EINVAL;
896                 break;
897
898         default:
899                 goto fallback;
900         }
901
902         return (error);
903
904 fallback:
905         if (lp != NULL && lp->lp_ioctl != NULL)
906                 return ((*lp->lp_ioctl)(ifp, cmd, data));
907
908         return (EINVAL);
909 }
910
911 /*
912  * Requests counter @cnt data. 
913  *
914  * Counter value is calculated the following way:
915  * 1) for each port, sum  difference between current and "initial" measurements.
916  * 2) add lagg logical interface counters.
917  * 3) add data from detached_counters array.
918  *
919  * We also do the following things on ports attach/detach:
920  * 1) On port attach we store all counters it has into port_counter array. 
921  * 2) On port detach we add the different between "initial" and
922  *   current counters data to detached_counters array.
923  */
924 static uint64_t
925 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
926 {
927         struct lagg_softc *sc;
928         struct lagg_port *lp;
929         struct ifnet *lpifp;
930         struct rm_priotracker tracker;
931         uint64_t newval, oldval, vsum;
932
933         /* Revise this when we've got non-generic counters. */
934         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
935
936         sc = (struct lagg_softc *)ifp->if_softc;
937         LAGG_RLOCK(sc, &tracker);
938
939         vsum = 0;
940         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
941                 /* Saved attached value */
942                 oldval = lp->port_counters.val[cnt];
943                 /* current value */
944                 lpifp = lp->lp_ifp;
945                 newval = lpifp->if_get_counter(lpifp, cnt);
946                 /* Calculate diff and save new */
947                 vsum += newval - oldval;
948         }
949
950         /*
951          * Add counter data which might be added by upper
952          * layer protocols operating on logical interface.
953          */
954         vsum += if_get_counter_default(ifp, cnt);
955
956         /*
957          * Add counter data from detached ports counters
958          */
959         vsum += sc->detached_counters.val[cnt];
960
961         LAGG_RUNLOCK(sc, &tracker);
962
963         return (vsum);
964 }
965
966 /*
967  * For direct output to child ports.
968  */
969 static int
970 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
971         const struct sockaddr *dst, struct route *ro)
972 {
973         struct lagg_port *lp = ifp->if_lagg;
974
975         switch (dst->sa_family) {
976                 case pseudo_AF_HDRCMPLT:
977                 case AF_UNSPEC:
978                         return ((*lp->lp_output)(ifp, m, dst, ro));
979         }
980
981         /* drop any other frames */
982         m_freem(m);
983         return (ENETDOWN);
984 }
985
986 static void
987 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
988 {
989         struct lagg_port *lp;
990         struct lagg_softc *sc;
991
992         if ((lp = ifp->if_lagg) == NULL)
993                 return;
994         /* If the ifnet is just being renamed, don't do anything. */
995         if (ifp->if_flags & IFF_RENAMING)
996                 return;
997
998         sc = lp->lp_softc;
999
1000         LAGG_XLOCK(sc);
1001         lp->lp_detaching = 1;
1002         lagg_port_destroy(lp, 1);
1003         LAGG_XUNLOCK(sc);
1004 }
1005
1006 static void
1007 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1008 {
1009         struct lagg_softc *sc = lp->lp_softc;
1010
1011         strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1012         strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1013         rp->rp_prio = lp->lp_prio;
1014         rp->rp_flags = lp->lp_flags;
1015         lagg_proto_portreq(sc, lp, &rp->rp_psc);
1016
1017         /* Add protocol specific flags */
1018         switch (sc->sc_proto) {
1019                 case LAGG_PROTO_FAILOVER:
1020                         if (lp == sc->sc_primary)
1021                                 rp->rp_flags |= LAGG_PORT_MASTER;
1022                         if (lp == lagg_link_active(sc, sc->sc_primary))
1023                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1024                         break;
1025
1026                 case LAGG_PROTO_ROUNDROBIN:
1027                 case LAGG_PROTO_LOADBALANCE:
1028                 case LAGG_PROTO_BROADCAST:
1029                         if (LAGG_PORTACTIVE(lp))
1030                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1031                         break;
1032
1033                 case LAGG_PROTO_LACP:
1034                         /* LACP has a different definition of active */
1035                         if (lacp_isactive(lp))
1036                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1037                         if (lacp_iscollecting(lp))
1038                                 rp->rp_flags |= LAGG_PORT_COLLECTING;
1039                         if (lacp_isdistributing(lp))
1040                                 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1041                         break;
1042         }
1043
1044 }
1045
1046 static void
1047 lagg_init(void *xsc)
1048 {
1049         struct lagg_softc *sc = (struct lagg_softc *)xsc;
1050         struct ifnet *ifp = sc->sc_ifp;
1051         struct lagg_port *lp;
1052
1053         LAGG_XLOCK(sc);
1054         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1055                 LAGG_XUNLOCK(sc);
1056                 return;
1057         }
1058
1059         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1060
1061         /*
1062          * Update the port lladdrs if needed.
1063          * This might be if_setlladdr() notification
1064          * that lladdr has been changed.
1065          */
1066         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1067                 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1068                     ETHER_ADDR_LEN) != 0)
1069                         if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1070         }
1071
1072         lagg_proto_init(sc);
1073
1074         LAGG_XUNLOCK(sc);
1075 }
1076
1077 static void
1078 lagg_stop(struct lagg_softc *sc)
1079 {
1080         struct ifnet *ifp = sc->sc_ifp;
1081
1082         LAGG_XLOCK_ASSERT(sc);
1083
1084         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1085                 return;
1086
1087         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1088
1089         lagg_proto_stop(sc);
1090 }
1091
1092 static int
1093 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1094 {
1095         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1096         struct lagg_reqall *ra = (struct lagg_reqall *)data;
1097         struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1098         struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1099         struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1100         struct ifreq *ifr = (struct ifreq *)data;
1101         struct lagg_port *lp;
1102         struct ifnet *tpif;
1103         struct thread *td = curthread;
1104         char *buf, *outbuf;
1105         int count, buflen, len, error = 0;
1106
1107         bzero(&rpbuf, sizeof(rpbuf));
1108
1109         switch (cmd) {
1110         case SIOCGLAGG:
1111                 LAGG_SLOCK(sc);
1112                 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1113                 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1114                 ra->ra_proto = sc->sc_proto;
1115                 lagg_proto_request(sc, &ra->ra_psc);
1116                 count = 0;
1117                 buf = outbuf;
1118                 len = min(ra->ra_size, buflen);
1119                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1120                         if (len < sizeof(rpbuf))
1121                                 break;
1122
1123                         lagg_port2req(lp, &rpbuf);
1124                         memcpy(buf, &rpbuf, sizeof(rpbuf));
1125                         count++;
1126                         buf += sizeof(rpbuf);
1127                         len -= sizeof(rpbuf);
1128                 }
1129                 LAGG_SUNLOCK(sc);
1130                 ra->ra_ports = count;
1131                 ra->ra_size = count * sizeof(rpbuf);
1132                 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1133                 free(outbuf, M_TEMP);
1134                 break;
1135         case SIOCSLAGG:
1136                 error = priv_check(td, PRIV_NET_LAGG);
1137                 if (error)
1138                         break;
1139                 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1140                         error = EPROTONOSUPPORT;
1141                         break;
1142                 }
1143
1144                 LAGG_XLOCK(sc);
1145                 LAGG_WLOCK(sc);
1146                 lagg_proto_detach(sc);
1147                 LAGG_UNLOCK_ASSERT(sc);
1148                 lagg_proto_attach(sc, ra->ra_proto);
1149                 LAGG_XUNLOCK(sc);
1150                 break;
1151         case SIOCGLAGGOPTS:
1152                 LAGG_SLOCK(sc);
1153                 ro->ro_opts = sc->sc_opts;
1154                 if (sc->sc_proto == LAGG_PROTO_LACP) {
1155                         struct lacp_softc *lsc;
1156
1157                         lsc = (struct lacp_softc *)sc->sc_psc;
1158                         if (lsc->lsc_debug.lsc_tx_test != 0)
1159                                 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1160                         if (lsc->lsc_debug.lsc_rx_test != 0)
1161                                 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1162                         if (lsc->lsc_strict_mode != 0)
1163                                 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1164                         if (lsc->lsc_fast_timeout != 0)
1165                                 ro->ro_opts |= LAGG_OPT_LACP_TIMEOUT;
1166
1167                         ro->ro_active = sc->sc_active;
1168                 } else {
1169                         ro->ro_active = 0;
1170                         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1171                                 ro->ro_active += LAGG_PORTACTIVE(lp);
1172                 }
1173                 ro->ro_bkt = sc->sc_bkt;
1174                 ro->ro_flapping = sc->sc_flapping;
1175                 ro->ro_flowid_shift = sc->flowid_shift;
1176                 LAGG_SUNLOCK(sc);
1177                 break;
1178         case SIOCSLAGGOPTS:
1179                 if (sc->sc_proto == LAGG_PROTO_ROUNDROBIN) {
1180                         if (ro->ro_bkt == 0)
1181                                 sc->sc_bkt = 1; // Minimum 1 packet per iface.
1182                         else
1183                                 sc->sc_bkt = ro->ro_bkt;
1184                 }
1185                 error = priv_check(td, PRIV_NET_LAGG);
1186                 if (error)
1187                         break;
1188                 if (ro->ro_opts == 0)
1189                         break;
1190                 /*
1191                  * Set options.  LACP options are stored in sc->sc_psc,
1192                  * not in sc_opts.
1193                  */
1194                 int valid, lacp;
1195
1196                 switch (ro->ro_opts) {
1197                 case LAGG_OPT_USE_FLOWID:
1198                 case -LAGG_OPT_USE_FLOWID:
1199                 case LAGG_OPT_FLOWIDSHIFT:
1200                         valid = 1;
1201                         lacp = 0;
1202                         break;
1203                 case LAGG_OPT_LACP_TXTEST:
1204                 case -LAGG_OPT_LACP_TXTEST:
1205                 case LAGG_OPT_LACP_RXTEST:
1206                 case -LAGG_OPT_LACP_RXTEST:
1207                 case LAGG_OPT_LACP_STRICT:
1208                 case -LAGG_OPT_LACP_STRICT:
1209                 case LAGG_OPT_LACP_TIMEOUT:
1210                 case -LAGG_OPT_LACP_TIMEOUT:
1211                         valid = lacp = 1;
1212                         break;
1213                 default:
1214                         valid = lacp = 0;
1215                         break;
1216                 }
1217
1218                 LAGG_XLOCK(sc);
1219
1220                 if (valid == 0 ||
1221                     (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1222                         /* Invalid combination of options specified. */
1223                         error = EINVAL;
1224                         LAGG_XUNLOCK(sc);
1225                         break;  /* Return from SIOCSLAGGOPTS. */ 
1226                 }
1227                 /*
1228                  * Store new options into sc->sc_opts except for
1229                  * FLOWIDSHIFT and LACP options.
1230                  */
1231                 if (lacp == 0) {
1232                         if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1233                                 sc->flowid_shift = ro->ro_flowid_shift;
1234                         else if (ro->ro_opts > 0)
1235                                 sc->sc_opts |= ro->ro_opts;
1236                         else
1237                                 sc->sc_opts &= ~ro->ro_opts;
1238                 } else {
1239                         struct lacp_softc *lsc;
1240                         struct lacp_port *lp;
1241
1242                         lsc = (struct lacp_softc *)sc->sc_psc;
1243
1244                         switch (ro->ro_opts) {
1245                         case LAGG_OPT_LACP_TXTEST:
1246                                 lsc->lsc_debug.lsc_tx_test = 1;
1247                                 break;
1248                         case -LAGG_OPT_LACP_TXTEST:
1249                                 lsc->lsc_debug.lsc_tx_test = 0;
1250                                 break;
1251                         case LAGG_OPT_LACP_RXTEST:
1252                                 lsc->lsc_debug.lsc_rx_test = 1;
1253                                 break;
1254                         case -LAGG_OPT_LACP_RXTEST:
1255                                 lsc->lsc_debug.lsc_rx_test = 0;
1256                                 break;
1257                         case LAGG_OPT_LACP_STRICT:
1258                                 lsc->lsc_strict_mode = 1;
1259                                 break;
1260                         case -LAGG_OPT_LACP_STRICT:
1261                                 lsc->lsc_strict_mode = 0;
1262                                 break;
1263                         case LAGG_OPT_LACP_TIMEOUT:
1264                                 LACP_LOCK(lsc);
1265                                 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1266                                         lp->lp_state |= LACP_STATE_TIMEOUT;
1267                                 LACP_UNLOCK(lsc);
1268                                 lsc->lsc_fast_timeout = 1;
1269                                 break;
1270                         case -LAGG_OPT_LACP_TIMEOUT:
1271                                 LACP_LOCK(lsc);
1272                                 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1273                                         lp->lp_state &= ~LACP_STATE_TIMEOUT;
1274                                 LACP_UNLOCK(lsc);
1275                                 lsc->lsc_fast_timeout = 0;
1276                                 break;
1277                         }
1278                 }
1279                 LAGG_XUNLOCK(sc);
1280                 break;
1281         case SIOCGLAGGFLAGS:
1282                 rf->rf_flags = 0;
1283                 LAGG_SLOCK(sc);
1284                 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1285                         rf->rf_flags |= LAGG_F_HASHL2;
1286                 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1287                         rf->rf_flags |= LAGG_F_HASHL3;
1288                 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1289                         rf->rf_flags |= LAGG_F_HASHL4;
1290                 LAGG_SUNLOCK(sc);
1291                 break;
1292         case SIOCSLAGGHASH:
1293                 error = priv_check(td, PRIV_NET_LAGG);
1294                 if (error)
1295                         break;
1296                 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1297                         error = EINVAL;
1298                         break;
1299                 }
1300                 LAGG_XLOCK(sc);
1301                 sc->sc_flags = 0;
1302                 if (rf->rf_flags & LAGG_F_HASHL2)
1303                         sc->sc_flags |= MBUF_HASHFLAG_L2;
1304                 if (rf->rf_flags & LAGG_F_HASHL3)
1305                         sc->sc_flags |= MBUF_HASHFLAG_L3;
1306                 if (rf->rf_flags & LAGG_F_HASHL4)
1307                         sc->sc_flags |= MBUF_HASHFLAG_L4;
1308                 LAGG_XUNLOCK(sc);
1309                 break;
1310         case SIOCGLAGGPORT:
1311                 if (rp->rp_portname[0] == '\0' ||
1312                     (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1313                         error = EINVAL;
1314                         break;
1315                 }
1316
1317                 LAGG_SLOCK(sc);
1318                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1319                     lp->lp_softc != sc) {
1320                         error = ENOENT;
1321                         LAGG_SUNLOCK(sc);
1322                         if_rele(tpif);
1323                         break;
1324                 }
1325
1326                 lagg_port2req(lp, rp);
1327                 LAGG_SUNLOCK(sc);
1328                 if_rele(tpif);
1329                 break;
1330         case SIOCSLAGGPORT:
1331                 error = priv_check(td, PRIV_NET_LAGG);
1332                 if (error)
1333                         break;
1334                 if (rp->rp_portname[0] == '\0' ||
1335                     (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1336                         error = EINVAL;
1337                         break;
1338                 }
1339 #ifdef INET6
1340                 /*
1341                  * A laggport interface should not have inet6 address
1342                  * because two interfaces with a valid link-local
1343                  * scope zone must not be merged in any form.  This
1344                  * restriction is needed to prevent violation of
1345                  * link-local scope zone.  Attempts to add a laggport
1346                  * interface which has inet6 addresses triggers
1347                  * removal of all inet6 addresses on the member
1348                  * interface.
1349                  */
1350                 if (in6ifa_llaonifp(tpif)) {
1351                         in6_ifdetach(tpif);
1352                                 if_printf(sc->sc_ifp,
1353                                     "IPv6 addresses on %s have been removed "
1354                                     "before adding it as a member to prevent "
1355                                     "IPv6 address scope violation.\n",
1356                                     tpif->if_xname);
1357                 }
1358 #endif
1359                 LAGG_XLOCK(sc);
1360                 error = lagg_port_create(sc, tpif);
1361                 LAGG_XUNLOCK(sc);
1362                 if_rele(tpif);
1363                 break;
1364         case SIOCSLAGGDELPORT:
1365                 error = priv_check(td, PRIV_NET_LAGG);
1366                 if (error)
1367                         break;
1368                 if (rp->rp_portname[0] == '\0' ||
1369                     (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1370                         error = EINVAL;
1371                         break;
1372                 }
1373
1374                 LAGG_XLOCK(sc);
1375                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1376                     lp->lp_softc != sc) {
1377                         error = ENOENT;
1378                         LAGG_XUNLOCK(sc);
1379                         if_rele(tpif);
1380                         break;
1381                 }
1382
1383                 error = lagg_port_destroy(lp, 1);
1384                 LAGG_XUNLOCK(sc);
1385                 if_rele(tpif);
1386                 break;
1387         case SIOCSIFFLAGS:
1388                 /* Set flags on ports too */
1389                 LAGG_XLOCK(sc);
1390                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1391                         lagg_setflags(lp, 1);
1392                 }
1393
1394                 if (!(ifp->if_flags & IFF_UP) &&
1395                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1396                         /*
1397                          * If interface is marked down and it is running,
1398                          * then stop and disable it.
1399                          */
1400                         lagg_stop(sc);
1401                         LAGG_XUNLOCK(sc);
1402                 } else if ((ifp->if_flags & IFF_UP) &&
1403                     !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1404                         /*
1405                          * If interface is marked up and it is stopped, then
1406                          * start it.
1407                          */
1408                         LAGG_XUNLOCK(sc);
1409                         (*ifp->if_init)(sc);
1410                 } else
1411                         LAGG_XUNLOCK(sc);
1412                 break;
1413         case SIOCADDMULTI:
1414         case SIOCDELMULTI:
1415                 LAGG_WLOCK(sc);
1416                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1417                         lagg_clrmulti(lp);
1418                         lagg_setmulti(lp);
1419                 }
1420                 LAGG_WUNLOCK(sc);
1421                 error = 0;
1422                 break;
1423         case SIOCSIFMEDIA:
1424         case SIOCGIFMEDIA:
1425                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1426                 break;
1427
1428         case SIOCSIFCAP:
1429                 LAGG_XLOCK(sc);
1430                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1431                         if (lp->lp_ioctl != NULL)
1432                                 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1433                 }
1434                 lagg_capabilities(sc);
1435                 LAGG_XUNLOCK(sc);
1436                 error = 0;
1437                 break;
1438
1439         case SIOCSIFMTU:
1440                 /* Do not allow the MTU to be directly changed */
1441                 error = EINVAL;
1442                 break;
1443
1444         default:
1445                 error = ether_ioctl(ifp, cmd, data);
1446                 break;
1447         }
1448         return (error);
1449 }
1450
1451 #ifdef RATELIMIT
1452 static int
1453 lagg_snd_tag_alloc(struct ifnet *ifp,
1454     union if_snd_tag_alloc_params *params,
1455     struct m_snd_tag **ppmt)
1456 {
1457         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1458         struct lagg_port *lp;
1459         struct lagg_lb *lb;
1460         uint32_t p;
1461
1462         switch (sc->sc_proto) {
1463         case LAGG_PROTO_FAILOVER:
1464                 lp = lagg_link_active(sc, sc->sc_primary);
1465                 break;
1466         case LAGG_PROTO_LOADBALANCE:
1467                 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1468                     params->hdr.flowtype == M_HASHTYPE_NONE)
1469                         return (EOPNOTSUPP);
1470                 p = params->hdr.flowid >> sc->flowid_shift;
1471                 p %= sc->sc_count;
1472                 lb = (struct lagg_lb *)sc->sc_psc;
1473                 lp = lb->lb_ports[p];
1474                 lp = lagg_link_active(sc, lp);
1475                 break;
1476         case LAGG_PROTO_LACP:
1477                 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1478                     params->hdr.flowtype == M_HASHTYPE_NONE)
1479                         return (EOPNOTSUPP);
1480                 lp = lacp_select_tx_port_by_hash(sc, params->hdr.flowid);
1481                 break;
1482         default:
1483                 return (EOPNOTSUPP);
1484         }
1485         if (lp == NULL)
1486                 return (EOPNOTSUPP);
1487         ifp = lp->lp_ifp;
1488         if (ifp == NULL || ifp->if_snd_tag_alloc == NULL ||
1489             (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1490                 return (EOPNOTSUPP);
1491
1492         /* forward allocation request */
1493         return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1494 }
1495 #endif
1496
1497 static int
1498 lagg_setmulti(struct lagg_port *lp)
1499 {
1500         struct lagg_softc *sc = lp->lp_softc;
1501         struct ifnet *ifp = lp->lp_ifp;
1502         struct ifnet *scifp = sc->sc_ifp;
1503         struct lagg_mc *mc;
1504         struct ifmultiaddr *ifma;
1505         int error;
1506
1507         LAGG_WLOCK_ASSERT(sc);
1508         IF_ADDR_WLOCK(scifp);
1509         TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1510                 if (ifma->ifma_addr->sa_family != AF_LINK)
1511                         continue;
1512                 mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1513                 if (mc == NULL) {
1514                         IF_ADDR_WUNLOCK(scifp);
1515                         return (ENOMEM);
1516                 }
1517                 bcopy(ifma->ifma_addr, &mc->mc_addr,
1518                     ifma->ifma_addr->sa_len);
1519                 mc->mc_addr.sdl_index = ifp->if_index;
1520                 mc->mc_ifma = NULL;
1521                 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1522         }
1523         IF_ADDR_WUNLOCK(scifp);
1524         SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1525                 error = if_addmulti(ifp,
1526                     (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1527                 if (error)
1528                         return (error);
1529         }
1530         return (0);
1531 }
1532
1533 static int
1534 lagg_clrmulti(struct lagg_port *lp)
1535 {
1536         struct lagg_mc *mc;
1537
1538         LAGG_WLOCK_ASSERT(lp->lp_softc);
1539         while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1540                 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1541                 if (mc->mc_ifma && lp->lp_detaching == 0)
1542                         if_delmulti_ifma(mc->mc_ifma);
1543                 free(mc, M_DEVBUF);
1544         }
1545         return (0);
1546 }
1547
1548 static int
1549 lagg_setcaps(struct lagg_port *lp, int cap)
1550 {
1551         struct ifreq ifr;
1552
1553         if (lp->lp_ifp->if_capenable == cap)
1554                 return (0);
1555         if (lp->lp_ioctl == NULL)
1556                 return (ENXIO);
1557         ifr.ifr_reqcap = cap;
1558         return ((*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr));
1559 }
1560
1561 /* Handle a ref counted flag that should be set on the lagg port as well */
1562 static int
1563 lagg_setflag(struct lagg_port *lp, int flag, int status,
1564     int (*func)(struct ifnet *, int))
1565 {
1566         struct lagg_softc *sc = lp->lp_softc;
1567         struct ifnet *scifp = sc->sc_ifp;
1568         struct ifnet *ifp = lp->lp_ifp;
1569         int error;
1570
1571         LAGG_XLOCK_ASSERT(sc);
1572
1573         status = status ? (scifp->if_flags & flag) : 0;
1574         /* Now "status" contains the flag value or 0 */
1575
1576         /*
1577          * See if recorded ports status is different from what
1578          * we want it to be.  If it is, flip it.  We record ports
1579          * status in lp_ifflags so that we won't clear ports flag
1580          * we haven't set.  In fact, we don't clear or set ports
1581          * flags directly, but get or release references to them.
1582          * That's why we can be sure that recorded flags still are
1583          * in accord with actual ports flags.
1584          */
1585         if (status != (lp->lp_ifflags & flag)) {
1586                 error = (*func)(ifp, status);
1587                 if (error)
1588                         return (error);
1589                 lp->lp_ifflags &= ~flag;
1590                 lp->lp_ifflags |= status;
1591         }
1592         return (0);
1593 }
1594
1595 /*
1596  * Handle IFF_* flags that require certain changes on the lagg port
1597  * if "status" is true, update ports flags respective to the lagg
1598  * if "status" is false, forcedly clear the flags set on port.
1599  */
1600 static int
1601 lagg_setflags(struct lagg_port *lp, int status)
1602 {
1603         int error, i;
1604
1605         for (i = 0; lagg_pflags[i].flag; i++) {
1606                 error = lagg_setflag(lp, lagg_pflags[i].flag,
1607                     status, lagg_pflags[i].func);
1608                 if (error)
1609                         return (error);
1610         }
1611         return (0);
1612 }
1613
1614 static int
1615 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1616 {
1617         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1618         int error, len, mcast;
1619         struct rm_priotracker tracker;
1620
1621         len = m->m_pkthdr.len;
1622         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1623
1624         LAGG_RLOCK(sc, &tracker);
1625         /* We need a Tx algorithm and at least one port */
1626         if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1627                 LAGG_RUNLOCK(sc, &tracker);
1628                 m_freem(m);
1629                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1630                 return (ENXIO);
1631         }
1632
1633         ETHER_BPF_MTAP(ifp, m);
1634
1635         error = lagg_proto_start(sc, m);
1636         LAGG_RUNLOCK(sc, &tracker);
1637
1638         if (error != 0)
1639                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1640
1641         return (error);
1642 }
1643
1644 /*
1645  * The ifp->if_qflush entry point for lagg(4) is no-op.
1646  */
1647 static void
1648 lagg_qflush(struct ifnet *ifp __unused)
1649 {
1650 }
1651
1652 static struct mbuf *
1653 lagg_input(struct ifnet *ifp, struct mbuf *m)
1654 {
1655         struct lagg_port *lp = ifp->if_lagg;
1656         struct lagg_softc *sc = lp->lp_softc;
1657         struct ifnet *scifp = sc->sc_ifp;
1658         struct rm_priotracker tracker;
1659
1660         LAGG_RLOCK(sc, &tracker);
1661         if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1662             (lp->lp_flags & LAGG_PORT_DISABLED) ||
1663             sc->sc_proto == LAGG_PROTO_NONE) {
1664                 LAGG_RUNLOCK(sc, &tracker);
1665                 m_freem(m);
1666                 return (NULL);
1667         }
1668
1669         ETHER_BPF_MTAP(scifp, m);
1670
1671         if (lp->lp_detaching != 0) {
1672                 m_freem(m);
1673                 m = NULL;
1674         } else
1675                 m = lagg_proto_input(sc, lp, m);
1676
1677         if (m != NULL) {
1678                 if (scifp->if_flags & IFF_MONITOR) {
1679                         m_freem(m);
1680                         m = NULL;
1681                 }
1682         }
1683
1684         LAGG_RUNLOCK(sc, &tracker);
1685         return (m);
1686 }
1687
1688 static int
1689 lagg_media_change(struct ifnet *ifp)
1690 {
1691         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1692
1693         if (sc->sc_ifflags & IFF_DEBUG)
1694                 printf("%s\n", __func__);
1695
1696         /* Ignore */
1697         return (0);
1698 }
1699
1700 static void
1701 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1702 {
1703         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1704         struct lagg_port *lp;
1705
1706         imr->ifm_status = IFM_AVALID;
1707         imr->ifm_active = IFM_ETHER | IFM_AUTO;
1708
1709         LAGG_SLOCK(sc);
1710         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1711                 if (LAGG_PORTACTIVE(lp))
1712                         imr->ifm_status |= IFM_ACTIVE;
1713         }
1714         LAGG_SUNLOCK(sc);
1715 }
1716
1717 static void
1718 lagg_linkstate(struct lagg_softc *sc)
1719 {
1720         struct lagg_port *lp;
1721         int new_link = LINK_STATE_DOWN;
1722         uint64_t speed;
1723
1724         LAGG_XLOCK_ASSERT(sc);
1725
1726         /* Our link is considered up if at least one of our ports is active */
1727         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1728                 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1729                         new_link = LINK_STATE_UP;
1730                         break;
1731                 }
1732         }
1733         if_link_state_change(sc->sc_ifp, new_link);
1734
1735         /* Update if_baudrate to reflect the max possible speed */
1736         switch (sc->sc_proto) {
1737                 case LAGG_PROTO_FAILOVER:
1738                         sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1739                             sc->sc_primary->lp_ifp->if_baudrate : 0;
1740                         break;
1741                 case LAGG_PROTO_ROUNDROBIN:
1742                 case LAGG_PROTO_LOADBALANCE:
1743                 case LAGG_PROTO_BROADCAST:
1744                         speed = 0;
1745                         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1746                                 speed += lp->lp_ifp->if_baudrate;
1747                         sc->sc_ifp->if_baudrate = speed;
1748                         break;
1749                 case LAGG_PROTO_LACP:
1750                         /* LACP updates if_baudrate itself */
1751                         break;
1752         }
1753 }
1754
1755 static void
1756 lagg_port_state(struct ifnet *ifp, int state)
1757 {
1758         struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1759         struct lagg_softc *sc = NULL;
1760
1761         if (lp != NULL)
1762                 sc = lp->lp_softc;
1763         if (sc == NULL)
1764                 return;
1765
1766         LAGG_XLOCK(sc);
1767         lagg_linkstate(sc);
1768         lagg_proto_linkstate(sc, lp);
1769         LAGG_XUNLOCK(sc);
1770 }
1771
1772 struct lagg_port *
1773 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1774 {
1775         struct lagg_port *lp_next, *rval = NULL;
1776         // int new_link = LINK_STATE_DOWN;
1777
1778         /*
1779          * Search a port which reports an active link state.
1780          */
1781
1782         if (lp == NULL)
1783                 goto search;
1784         if (LAGG_PORTACTIVE(lp)) {
1785                 rval = lp;
1786                 goto found;
1787         }
1788         if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1789             LAGG_PORTACTIVE(lp_next)) {
1790                 rval = lp_next;
1791                 goto found;
1792         }
1793
1794 search:
1795         SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1796                 if (LAGG_PORTACTIVE(lp_next)) {
1797                         rval = lp_next;
1798                         goto found;
1799                 }
1800         }
1801
1802 found:
1803         if (rval != NULL) {
1804                 /*
1805                  * The IEEE 802.1D standard assumes that a lagg with
1806                  * multiple ports is always full duplex. This is valid
1807                  * for load sharing laggs and if at least two links
1808                  * are active. Unfortunately, checking the latter would
1809                  * be too expensive at this point.
1810                  XXX
1811                 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1812                     (sc->sc_count > 1))
1813                         new_link = LINK_STATE_FULL_DUPLEX;
1814                 else
1815                         new_link = rval->lp_link_state;
1816                  */
1817         }
1818
1819         return (rval);
1820 }
1821
1822 int
1823 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1824 {
1825
1826         return (ifp->if_transmit)(ifp, m);
1827 }
1828
1829 /*
1830  * Simple round robin aggregation
1831  */
1832 static void
1833 lagg_rr_attach(struct lagg_softc *sc)
1834 {
1835         sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1836         sc->sc_seq = 0;
1837         sc->sc_bkt_count = sc->sc_bkt;
1838 }
1839
1840 static int
1841 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1842 {
1843         struct lagg_port *lp;
1844         uint32_t p;
1845
1846         if (sc->sc_bkt_count == 0 && sc->sc_bkt > 0)
1847                 sc->sc_bkt_count = sc->sc_bkt;
1848
1849         if (sc->sc_bkt > 0) {
1850                 atomic_subtract_int(&sc->sc_bkt_count, 1);
1851         if (atomic_cmpset_int(&sc->sc_bkt_count, 0, sc->sc_bkt))
1852                 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1853         else
1854                 p = sc->sc_seq; 
1855         } else
1856                 p = atomic_fetchadd_32(&sc->sc_seq, 1);
1857
1858         p %= sc->sc_count;
1859         lp = SLIST_FIRST(&sc->sc_ports);
1860
1861         while (p--)
1862                 lp = SLIST_NEXT(lp, lp_entries);
1863
1864         /*
1865          * Check the port's link state. This will return the next active
1866          * port if the link is down or the port is NULL.
1867          */
1868         if ((lp = lagg_link_active(sc, lp)) == NULL) {
1869                 m_freem(m);
1870                 return (ENETDOWN);
1871         }
1872
1873         /* Send mbuf */
1874         return (lagg_enqueue(lp->lp_ifp, m));
1875 }
1876
1877 static struct mbuf *
1878 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1879 {
1880         struct ifnet *ifp = sc->sc_ifp;
1881
1882         /* Just pass in the packet to our lagg device */
1883         m->m_pkthdr.rcvif = ifp;
1884
1885         return (m);
1886 }
1887
1888 /*
1889  * Broadcast mode
1890  */
1891 static int
1892 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1893 {
1894         int active_ports = 0;
1895         int errors = 0;
1896         int ret;
1897         struct lagg_port *lp, *last = NULL;
1898         struct mbuf *m0;
1899
1900         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1901                 if (!LAGG_PORTACTIVE(lp))
1902                         continue;
1903
1904                 active_ports++;
1905
1906                 if (last != NULL) {
1907                         m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1908                         if (m0 == NULL) {
1909                                 ret = ENOBUFS;
1910                                 errors++;
1911                                 break;
1912                         }
1913
1914                         ret = lagg_enqueue(last->lp_ifp, m0);
1915                         if (ret != 0)
1916                                 errors++;
1917                 }
1918                 last = lp;
1919         }
1920         if (last == NULL) {
1921                 m_freem(m);
1922                 return (ENOENT);
1923         }
1924         if ((last = lagg_link_active(sc, last)) == NULL) {
1925                 m_freem(m);
1926                 return (ENETDOWN);
1927         }
1928
1929         ret = lagg_enqueue(last->lp_ifp, m);
1930         if (ret != 0)
1931                 errors++;
1932
1933         if (errors == 0)
1934                 return (ret);
1935
1936         return (0);
1937 }
1938
1939 static struct mbuf*
1940 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1941 {
1942         struct ifnet *ifp = sc->sc_ifp;
1943
1944         /* Just pass in the packet to our lagg device */
1945         m->m_pkthdr.rcvif = ifp;
1946         return (m);
1947 }
1948
1949 /*
1950  * Active failover
1951  */
1952 static int
1953 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1954 {
1955         struct lagg_port *lp;
1956
1957         /* Use the master port if active or the next available port */
1958         if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1959                 m_freem(m);
1960                 return (ENETDOWN);
1961         }
1962
1963         /* Send mbuf */
1964         return (lagg_enqueue(lp->lp_ifp, m));
1965 }
1966
1967 static struct mbuf *
1968 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1969 {
1970         struct ifnet *ifp = sc->sc_ifp;
1971         struct lagg_port *tmp_tp;
1972
1973         if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
1974                 m->m_pkthdr.rcvif = ifp;
1975                 return (m);
1976         }
1977
1978         if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1979                 tmp_tp = lagg_link_active(sc, sc->sc_primary);
1980                 /*
1981                  * If tmp_tp is null, we've received a packet when all
1982                  * our links are down. Weird, but process it anyways.
1983                  */
1984                 if ((tmp_tp == NULL || tmp_tp == lp)) {
1985                         m->m_pkthdr.rcvif = ifp;
1986                         return (m);
1987                 }
1988         }
1989
1990         m_freem(m);
1991         return (NULL);
1992 }
1993
1994 /*
1995  * Loadbalancing
1996  */
1997 static void
1998 lagg_lb_attach(struct lagg_softc *sc)
1999 {
2000         struct lagg_port *lp;
2001         struct lagg_lb *lb;
2002
2003         lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2004
2005         sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2006
2007         lb->lb_key = m_ether_tcpip_hash_init();
2008         sc->sc_psc = lb;
2009
2010         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2011                 lagg_lb_port_create(lp);
2012 }
2013
2014 static void
2015 lagg_lb_detach(struct lagg_softc *sc)
2016 {
2017         struct lagg_lb *lb;
2018
2019         lb = (struct lagg_lb *)sc->sc_psc;
2020         LAGG_WUNLOCK(sc);
2021         if (lb != NULL)
2022                 free(lb, M_DEVBUF);
2023 }
2024
2025 static int
2026 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2027 {
2028         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2029         struct lagg_port *lp_next;
2030         int i = 0;
2031
2032         bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2033         SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2034                 if (lp_next == lp)
2035                         continue;
2036                 if (i >= LAGG_MAX_PORTS)
2037                         return (EINVAL);
2038                 if (sc->sc_ifflags & IFF_DEBUG)
2039                         printf("%s: port %s at index %d\n",
2040                             sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2041                 lb->lb_ports[i++] = lp_next;
2042         }
2043
2044         return (0);
2045 }
2046
2047 static int
2048 lagg_lb_port_create(struct lagg_port *lp)
2049 {
2050         struct lagg_softc *sc = lp->lp_softc;
2051         return (lagg_lb_porttable(sc, NULL));
2052 }
2053
2054 static void
2055 lagg_lb_port_destroy(struct lagg_port *lp)
2056 {
2057         struct lagg_softc *sc = lp->lp_softc;
2058         lagg_lb_porttable(sc, lp);
2059 }
2060
2061 static int
2062 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2063 {
2064         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2065         struct lagg_port *lp = NULL;
2066         uint32_t p = 0;
2067
2068         if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2069             M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2070                 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2071         else
2072                 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2073         p %= sc->sc_count;
2074         lp = lb->lb_ports[p];
2075
2076         /*
2077          * Check the port's link state. This will return the next active
2078          * port if the link is down or the port is NULL.
2079          */
2080         if ((lp = lagg_link_active(sc, lp)) == NULL) {
2081                 m_freem(m);
2082                 return (ENETDOWN);
2083         }
2084
2085         /* Send mbuf */
2086         return (lagg_enqueue(lp->lp_ifp, m));
2087 }
2088
2089 static struct mbuf *
2090 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2091 {
2092         struct ifnet *ifp = sc->sc_ifp;
2093
2094         /* Just pass in the packet to our lagg device */
2095         m->m_pkthdr.rcvif = ifp;
2096
2097         return (m);
2098 }
2099
2100 /*
2101  * 802.3ad LACP
2102  */
2103 static void
2104 lagg_lacp_attach(struct lagg_softc *sc)
2105 {
2106         struct lagg_port *lp;
2107
2108         lacp_attach(sc);
2109         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2110                 lacp_port_create(lp);
2111 }
2112
2113 static void
2114 lagg_lacp_detach(struct lagg_softc *sc)
2115 {
2116         struct lagg_port *lp;
2117         void *psc;
2118
2119         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2120                 lacp_port_destroy(lp);
2121
2122         psc = sc->sc_psc;
2123         sc->sc_psc = NULL;
2124         LAGG_WUNLOCK(sc);
2125
2126         lacp_detach(psc);
2127 }
2128
2129 static void
2130 lagg_lacp_lladdr(struct lagg_softc *sc)
2131 {
2132         struct lagg_port *lp;
2133
2134         LAGG_SXLOCK_ASSERT(sc);
2135
2136         /* purge all the lacp ports */
2137         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2138                 lacp_port_destroy(lp);
2139
2140         /* add them back in */
2141         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2142                 lacp_port_create(lp);
2143 }
2144
2145 static int
2146 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2147 {
2148         struct lagg_port *lp;
2149
2150         lp = lacp_select_tx_port(sc, m);
2151         if (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 *
2161 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2162 {
2163         struct ifnet *ifp = sc->sc_ifp;
2164         struct ether_header *eh;
2165         u_short etype;
2166
2167         eh = mtod(m, struct ether_header *);
2168         etype = ntohs(eh->ether_type);
2169
2170         /* Tap off LACP control messages */
2171         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2172                 m = lacp_input(lp, m);
2173                 if (m == NULL)
2174                         return (NULL);
2175         }
2176
2177         /*
2178          * If the port is not collecting or not in the active aggregator then
2179          * free and return.
2180          */
2181         if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2182                 m_freem(m);
2183                 return (NULL);
2184         }
2185
2186         m->m_pkthdr.rcvif = ifp;
2187         return (m);
2188 }
2189