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