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