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