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