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