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