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