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