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