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