]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_lagg.c
IFC @r273206
[FreeBSD/FreeBSD.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  * Copyright (c) 2014 Marcelo Araujo <araujo@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "opt_inet.h"
25 #include "opt_inet6.h"
26
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
30 #include <sys/mbuf.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
36 #include <sys/priv.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/hash.h>
40 #include <sys/lock.h>
41 #include <sys/rmlock.h>
42 #include <sys/taskqueue.h>
43 #include <sys/eventhandler.h>
44
45 #include <net/ethernet.h>
46 #include <net/if.h>
47 #include <net/if_clone.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.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 #include <net/vnet.h>
55
56 #if defined(INET) || defined(INET6)
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
59 #endif
60 #ifdef INET
61 #include <netinet/in_systm.h>
62 #include <netinet/if_ether.h>
63 #endif
64
65 #ifdef INET6
66 #include <netinet/ip6.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_ifattach.h>
69 #endif
70
71 #include <net/if_vlan_var.h>
72 #include <net/if_lagg.h>
73 #include <net/ieee8023ad_lacp.h>
74
75 /* Special flags we should propagate to the lagg ports. */
76 static struct {
77         int flag;
78         int (*func)(struct ifnet *, int);
79 } lagg_pflags[] = {
80         {IFF_PROMISC, ifpromisc},
81         {IFF_ALLMULTI, if_allmulti},
82         {0, NULL}
83 };
84
85 VNET_DEFINE(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
86 #define V_lagg_list     VNET(lagg_list)
87 static VNET_DEFINE(struct mtx, lagg_list_mtx);
88 #define V_lagg_list_mtx VNET(lagg_list_mtx)
89 #define LAGG_LIST_LOCK_INIT(x)          mtx_init(&V_lagg_list_mtx, \
90                                         "if_lagg list", NULL, MTX_DEF)
91 #define LAGG_LIST_LOCK_DESTROY(x)       mtx_destroy(&V_lagg_list_mtx)
92 #define LAGG_LIST_LOCK(x)               mtx_lock(&V_lagg_list_mtx)
93 #define LAGG_LIST_UNLOCK(x)             mtx_unlock(&V_lagg_list_mtx)
94 eventhandler_tag        lagg_detach_cookie = NULL;
95
96 static int      lagg_clone_create(struct if_clone *, int, caddr_t);
97 static void     lagg_clone_destroy(struct ifnet *);
98 static VNET_DEFINE(struct if_clone *, lagg_cloner);
99 #define V_lagg_cloner   VNET(lagg_cloner)
100 static const char laggname[] = "lagg";
101
102 static void     lagg_lladdr(struct lagg_softc *, uint8_t *);
103 static void     lagg_capabilities(struct lagg_softc *);
104 static void     lagg_port_lladdr(struct lagg_port *, uint8_t *);
105 static void     lagg_port_setlladdr(void *, int);
106 static int      lagg_port_create(struct lagg_softc *, struct ifnet *);
107 static int      lagg_port_destroy(struct lagg_port *, int);
108 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
109 static void     lagg_linkstate(struct lagg_softc *);
110 static void     lagg_port_state(struct ifnet *, int);
111 static int      lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
112 static int      lagg_port_output(struct ifnet *, struct mbuf *,
113                     const struct sockaddr *, struct route *);
114 static void     lagg_port_ifdetach(void *arg __unused, struct ifnet *);
115 #ifdef LAGG_PORT_STACKING
116 static int      lagg_port_checkstacking(struct lagg_softc *);
117 #endif
118 static void     lagg_port2req(struct lagg_port *, struct lagg_reqport *);
119 static void     lagg_init(void *);
120 static void     lagg_stop(struct lagg_softc *);
121 static int      lagg_ioctl(struct ifnet *, u_long, caddr_t);
122 static int      lagg_ether_setmulti(struct lagg_softc *);
123 static int      lagg_ether_cmdmulti(struct lagg_port *, int);
124 static  int     lagg_setflag(struct lagg_port *, int, int,
125                     int (*func)(struct ifnet *, int));
126 static  int     lagg_setflags(struct lagg_port *, int status);
127 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
128 static int      lagg_transmit(struct ifnet *, struct mbuf *);
129 static void     lagg_qflush(struct ifnet *);
130 static int      lagg_media_change(struct ifnet *);
131 static void     lagg_media_status(struct ifnet *, struct ifmediareq *);
132 static struct lagg_port *lagg_link_active(struct lagg_softc *,
133             struct lagg_port *);
134 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
135
136 /* Simple round robin */
137 static void     lagg_rr_attach(struct lagg_softc *);
138 static int      lagg_rr_start(struct lagg_softc *, struct mbuf *);
139 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
140                     struct mbuf *);
141
142 /* Active failover */
143 static int      lagg_fail_start(struct lagg_softc *, struct mbuf *);
144 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
145                     struct mbuf *);
146
147 /* Loadbalancing */
148 static void     lagg_lb_attach(struct lagg_softc *);
149 static void     lagg_lb_detach(struct lagg_softc *);
150 static int      lagg_lb_port_create(struct lagg_port *);
151 static void     lagg_lb_port_destroy(struct lagg_port *);
152 static int      lagg_lb_start(struct lagg_softc *, struct mbuf *);
153 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
154                     struct mbuf *);
155 static int      lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
156
157 /* Broadcast */
158 static int    lagg_bcast_start(struct lagg_softc *, struct mbuf *);
159 static struct mbuf *lagg_bcast_input(struct lagg_softc *, struct lagg_port *,
160                     struct mbuf *);
161
162 /* 802.3ad LACP */
163 static void     lagg_lacp_attach(struct lagg_softc *);
164 static void     lagg_lacp_detach(struct lagg_softc *);
165 static int      lagg_lacp_start(struct lagg_softc *, struct mbuf *);
166 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
167                     struct mbuf *);
168 static void     lagg_lacp_lladdr(struct lagg_softc *);
169
170 /* lagg protocol table */
171 static const struct lagg_proto {
172         lagg_proto      pr_num;
173         void            (*pr_attach)(struct lagg_softc *);
174         void            (*pr_detach)(struct lagg_softc *);
175         int             (*pr_start)(struct lagg_softc *, struct mbuf *);
176         struct mbuf *   (*pr_input)(struct lagg_softc *, struct lagg_port *,
177                             struct mbuf *);
178         int             (*pr_addport)(struct lagg_port *);
179         void            (*pr_delport)(struct lagg_port *);
180         void            (*pr_linkstate)(struct lagg_port *);
181         void            (*pr_init)(struct lagg_softc *);
182         void            (*pr_stop)(struct lagg_softc *);
183         void            (*pr_lladdr)(struct lagg_softc *);
184         void            (*pr_request)(struct lagg_softc *, void *);
185         void            (*pr_portreq)(struct lagg_port *, void *);
186 } lagg_protos[] = {
187     {
188         .pr_num = LAGG_PROTO_NONE
189     },
190     {
191         .pr_num = LAGG_PROTO_ROUNDROBIN,
192         .pr_attach = lagg_rr_attach,
193         .pr_start = lagg_rr_start,
194         .pr_input = lagg_rr_input,
195     },
196     {
197         .pr_num = LAGG_PROTO_FAILOVER,
198         .pr_start = lagg_fail_start,
199         .pr_input = lagg_fail_input,
200     },
201     {
202         .pr_num = LAGG_PROTO_LOADBALANCE,
203         .pr_attach = lagg_lb_attach,
204         .pr_detach = lagg_lb_detach,
205         .pr_start = lagg_lb_start,
206         .pr_input = lagg_lb_input,
207         .pr_addport = lagg_lb_port_create,
208         .pr_delport = lagg_lb_port_destroy,
209     },
210     {
211         .pr_num = LAGG_PROTO_LACP,
212         .pr_attach = lagg_lacp_attach,
213         .pr_detach = lagg_lacp_detach,
214         .pr_start = lagg_lacp_start,
215         .pr_input = lagg_lacp_input,
216         .pr_addport = lacp_port_create,
217         .pr_delport = lacp_port_destroy,
218         .pr_linkstate = lacp_linkstate,
219         .pr_init = lacp_init,
220         .pr_stop = lacp_stop,
221         .pr_lladdr = lagg_lacp_lladdr,
222         .pr_request = lacp_req,
223         .pr_portreq = lacp_portreq,
224     },
225     {
226         .pr_num = LAGG_PROTO_ETHERCHANNEL,
227         .pr_attach = lagg_lb_attach,
228         .pr_detach = lagg_lb_detach,
229         .pr_start = lagg_lb_start,
230         .pr_input = lagg_lb_input,
231     },
232     {
233         .pr_num = LAGG_PROTO_BROADCAST,
234         .pr_start = lagg_bcast_start,
235         .pr_input = lagg_bcast_input,
236     },
237 };
238
239 SYSCTL_DECL(_net_link);
240 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW, 0,
241     "Link Aggregation");
242
243 /* Allow input on any failover links */
244 static VNET_DEFINE(int, lagg_failover_rx_all);
245 #define V_lagg_failover_rx_all  VNET(lagg_failover_rx_all)
246 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
247     &VNET_NAME(lagg_failover_rx_all), 0,
248     "Accept input from any interface in a failover lagg");
249
250 /* Default value for using M_FLOWID */
251 static VNET_DEFINE(int, def_use_flowid) = 1;
252 #define V_def_use_flowid        VNET(def_use_flowid)
253 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid, CTLFLAG_RWTUN,
254     &VNET_NAME(def_use_flowid), 0,
255     "Default setting for using flow id for load sharing");
256
257 /* Default value for using M_FLOWID */
258 static VNET_DEFINE(int, def_flowid_shift) = 16;
259 #define V_def_flowid_shift      VNET(def_flowid_shift)
260 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift, CTLFLAG_RWTUN,
261     &VNET_NAME(def_flowid_shift), 0,
262     "Default setting for flowid shift for load sharing");
263
264 static void
265 vnet_lagg_init(const void *unused __unused)
266 {
267
268         LAGG_LIST_LOCK_INIT();
269         SLIST_INIT(&V_lagg_list);
270         V_lagg_cloner = if_clone_simple(laggname, lagg_clone_create,
271             lagg_clone_destroy, 0);
272 }
273 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
274     vnet_lagg_init, NULL);
275
276 static void
277 vnet_lagg_uninit(const void *unused __unused)
278 {
279
280         if_clone_detach(V_lagg_cloner);
281         LAGG_LIST_LOCK_DESTROY();
282 }
283 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
284     vnet_lagg_uninit, NULL);
285
286 static int
287 lagg_modevent(module_t mod, int type, void *data)
288 {
289
290         switch (type) {
291         case MOD_LOAD:
292                 lagg_input_p = lagg_input;
293                 lagg_linkstate_p = lagg_port_state;
294                 lagg_detach_cookie = EVENTHANDLER_REGISTER(
295                     ifnet_departure_event, lagg_port_ifdetach, NULL,
296                     EVENTHANDLER_PRI_ANY);
297                 break;
298         case MOD_UNLOAD:
299                 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
300                     lagg_detach_cookie);
301                 lagg_input_p = NULL;
302                 lagg_linkstate_p = NULL;
303                 break;
304         default:
305                 return (EOPNOTSUPP);
306         }
307         return (0);
308 }
309
310 static moduledata_t lagg_mod = {
311         "if_lagg",
312         lagg_modevent,
313         0
314 };
315
316 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
317 MODULE_VERSION(if_lagg, 1);
318
319 static void
320 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
321 {
322
323         KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
324             __func__, sc));
325
326         if (sc->sc_ifflags & IFF_DEBUG)
327                 if_printf(sc->sc_ifp, "using proto %u\n", pr);
328
329         if (lagg_protos[pr].pr_attach != NULL)
330                 lagg_protos[pr].pr_attach(sc);
331         sc->sc_proto = pr;
332 }
333
334 static void
335 lagg_proto_detach(struct lagg_softc *sc)
336 {
337         lagg_proto pr;
338
339         LAGG_WLOCK_ASSERT(sc);
340
341         pr = sc->sc_proto;
342         sc->sc_proto = LAGG_PROTO_NONE;
343
344         if (lagg_protos[pr].pr_detach != NULL)
345                 lagg_protos[pr].pr_detach(sc);
346         else
347                 LAGG_WUNLOCK(sc);
348 }
349
350 static int
351 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
352 {
353
354         return (lagg_protos[sc->sc_proto].pr_start(sc, m));
355 }
356
357 static struct mbuf *
358 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
359 {
360
361         return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
362 }
363
364 static int
365 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
366 {
367
368         if (lagg_protos[sc->sc_proto].pr_addport == NULL)
369                 return (0);
370         else
371                 return (lagg_protos[sc->sc_proto].pr_addport(lp));
372 }
373
374 static void
375 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
376 {
377
378         if (lagg_protos[sc->sc_proto].pr_delport != NULL)
379                 lagg_protos[sc->sc_proto].pr_delport(lp);
380 }
381
382 static void
383 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
384 {
385
386         if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
387                 lagg_protos[sc->sc_proto].pr_linkstate(lp);
388 }
389
390 static void
391 lagg_proto_init(struct lagg_softc *sc)
392 {
393
394         if (lagg_protos[sc->sc_proto].pr_init != NULL)
395                 lagg_protos[sc->sc_proto].pr_init(sc);
396 }
397
398 static void
399 lagg_proto_stop(struct lagg_softc *sc)
400 {
401
402         if (lagg_protos[sc->sc_proto].pr_stop != NULL)
403                 lagg_protos[sc->sc_proto].pr_stop(sc);
404 }
405
406 static void
407 lagg_proto_lladdr(struct lagg_softc *sc)
408 {
409
410         if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
411                 lagg_protos[sc->sc_proto].pr_lladdr(sc);
412 }
413
414 static void
415 lagg_proto_request(struct lagg_softc *sc, void *v)
416 {
417
418         if (lagg_protos[sc->sc_proto].pr_request != NULL)
419                 lagg_protos[sc->sc_proto].pr_request(sc, v);
420 }
421
422 static void
423 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
424 {
425
426         if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
427                 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
428 }
429
430 /*
431  * This routine is run via an vlan
432  * config EVENT
433  */
434 static void
435 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
436 {
437         struct lagg_softc *sc = ifp->if_softc;
438         struct lagg_port *lp;
439         struct rm_priotracker tracker;
440
441         if (ifp->if_softc !=  arg)   /* Not our event */
442                 return;
443
444         LAGG_RLOCK(sc, &tracker);
445         if (!SLIST_EMPTY(&sc->sc_ports)) {
446                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
447                         EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
448         }
449         LAGG_RUNLOCK(sc, &tracker);
450 }
451
452 /*
453  * This routine is run via an vlan
454  * unconfig EVENT
455  */
456 static void
457 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
458 {
459         struct lagg_softc *sc = ifp->if_softc;
460         struct lagg_port *lp;
461         struct rm_priotracker tracker;
462
463         if (ifp->if_softc !=  arg)   /* Not our event */
464                 return;
465
466         LAGG_RLOCK(sc, &tracker);
467         if (!SLIST_EMPTY(&sc->sc_ports)) {
468                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
469                         EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
470         }
471         LAGG_RUNLOCK(sc, &tracker);
472 }
473
474 static int
475 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
476 {
477         struct lagg_softc *sc;
478         struct ifnet *ifp;
479         static const u_char eaddr[6];   /* 00:00:00:00:00:00 */
480
481         sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
482         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
483         if (ifp == NULL) {
484                 free(sc, M_DEVBUF);
485                 return (ENOSPC);
486         }
487
488         if (V_def_use_flowid)
489                 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
490         sc->flowid_shift = V_def_flowid_shift;
491
492         /* Hash all layers by default */
493         sc->sc_flags = LAGG_F_HASHL2|LAGG_F_HASHL3|LAGG_F_HASHL4;
494
495         lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
496
497         LAGG_LOCK_INIT(sc);
498         SLIST_INIT(&sc->sc_ports);
499         TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
500
501         /* Initialise pseudo media types */
502         ifmedia_init(&sc->sc_media, 0, lagg_media_change,
503             lagg_media_status);
504         ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
505         ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
506
507         if_initname(ifp, laggname, unit);
508         ifp->if_softc = sc;
509         ifp->if_transmit = lagg_transmit;
510         ifp->if_qflush = lagg_qflush;
511         ifp->if_init = lagg_init;
512         ifp->if_ioctl = lagg_ioctl;
513         ifp->if_get_counter = lagg_get_counter;
514         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
515         ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
516
517         /*
518          * Attach as an ordinary ethernet device, children will be attached
519          * as special device IFT_IEEE8023ADLAG.
520          */
521         ether_ifattach(ifp, eaddr);
522
523         sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
524                 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
525         sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
526                 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
527
528         /* Insert into the global list of laggs */
529         LAGG_LIST_LOCK();
530         SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
531         LAGG_LIST_UNLOCK();
532
533         return (0);
534 }
535
536 static void
537 lagg_clone_destroy(struct ifnet *ifp)
538 {
539         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
540         struct lagg_port *lp;
541
542         LAGG_WLOCK(sc);
543
544         lagg_stop(sc);
545         ifp->if_flags &= ~IFF_UP;
546
547         EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
548         EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
549
550         /* Shutdown and remove lagg ports */
551         while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
552                 lagg_port_destroy(lp, 1);
553         /* Unhook the aggregation protocol */
554         lagg_proto_detach(sc);
555
556         ifmedia_removeall(&sc->sc_media);
557         ether_ifdetach(ifp);
558         if_free(ifp);
559
560         LAGG_LIST_LOCK();
561         SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
562         LAGG_LIST_UNLOCK();
563
564         taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
565         LAGG_LOCK_DESTROY(sc);
566         free(sc, M_DEVBUF);
567 }
568
569 static void
570 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
571 {
572         struct lagg_port lp;
573
574         LAGG_WLOCK_ASSERT(sc);
575
576         bzero(&lp, sizeof(lp));
577         lp.lp_ifp = sc->sc_ifp;
578         lp.lp_softc = sc;
579
580         lagg_port_lladdr(&lp, lladdr);
581 }
582
583 static void
584 lagg_capabilities(struct lagg_softc *sc)
585 {
586         struct lagg_port *lp;
587         int cap = ~0, ena = ~0;
588         u_long hwa = ~0UL;
589         struct ifnet_hw_tsomax hw_tsomax;
590
591         LAGG_WLOCK_ASSERT(sc);
592
593         memset(&hw_tsomax, 0, sizeof(hw_tsomax));
594
595         /* Get capabilities from the lagg ports */
596         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
597                 cap &= lp->lp_ifp->if_capabilities;
598                 ena &= lp->lp_ifp->if_capenable;
599                 hwa &= lp->lp_ifp->if_hwassist;
600                 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
601         }
602         cap = (cap == ~0 ? 0 : cap);
603         ena = (ena == ~0 ? 0 : ena);
604         hwa = (hwa == ~0 ? 0 : hwa);
605
606         if (sc->sc_ifp->if_capabilities != cap ||
607             sc->sc_ifp->if_capenable != ena ||
608             sc->sc_ifp->if_hwassist != hwa ||
609             if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
610                 sc->sc_ifp->if_capabilities = cap;
611                 sc->sc_ifp->if_capenable = ena;
612                 sc->sc_ifp->if_hwassist = hwa;
613                 getmicrotime(&sc->sc_ifp->if_lastchange);
614
615                 if (sc->sc_ifflags & IFF_DEBUG)
616                         if_printf(sc->sc_ifp,
617                             "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
618         }
619 }
620
621 static void
622 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
623 {
624         struct lagg_softc *sc = lp->lp_softc;
625         struct ifnet *ifp = lp->lp_ifp;
626         struct lagg_llq *llq;
627         int pending = 0;
628
629         LAGG_WLOCK_ASSERT(sc);
630
631         if (lp->lp_detaching ||
632             memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
633                 return;
634
635         /* Check to make sure its not already queued to be changed */
636         SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
637                 if (llq->llq_ifp == ifp) {
638                         pending = 1;
639                         break;
640                 }
641         }
642
643         if (!pending) {
644                 llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
645                 if (llq == NULL)        /* XXX what to do */
646                         return;
647         }
648
649         /* Update the lladdr even if pending, it may have changed */
650         llq->llq_ifp = ifp;
651         llq->llq_primary = (sc->sc_primary->lp_ifp == ifp) ? 1 : 0;
652         bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
653
654         if (!pending)
655                 SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
656
657         taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
658 }
659
660 /*
661  * Set the interface MAC address from a taskqueue to avoid a LOR.
662  */
663 static void
664 lagg_port_setlladdr(void *arg, int pending)
665 {
666         struct lagg_softc *sc = (struct lagg_softc *)arg;
667         struct lagg_llq *llq, *head;
668         struct ifnet *ifp;
669         int error;
670
671         /* Grab a local reference of the queue and remove it from the softc */
672         LAGG_WLOCK(sc);
673         head = SLIST_FIRST(&sc->sc_llq_head);
674         SLIST_FIRST(&sc->sc_llq_head) = NULL;
675         LAGG_WUNLOCK(sc);
676
677         /*
678          * Traverse the queue and set the lladdr on each ifp. It is safe to do
679          * unlocked as we have the only reference to it.
680          */
681         for (llq = head; llq != NULL; llq = head) {
682                 ifp = llq->llq_ifp;
683
684                 CURVNET_SET(ifp->if_vnet);
685                 if (llq->llq_primary == 0) {
686                         /*
687                          * Set the link layer address on the laggport interface.
688                          * if_setlladdr() triggers gratuitous ARPs for INET.
689                          */
690                         error = if_setlladdr(ifp, llq->llq_lladdr,
691                             ETHER_ADDR_LEN);
692                         if (error)
693                                 printf("%s: setlladdr failed on %s\n", __func__,
694                                     ifp->if_xname);
695                 } else {
696                         /*
697                          * Set the link layer address on the lagg interface.
698                          * lagg_proto_lladdr() notifies the MAC change to
699                          * the aggregation protocol.  iflladdr_event handler
700                          * may trigger gratuitous ARPs for INET.
701                          */
702                         if (memcmp(llq->llq_lladdr, IF_LLADDR(ifp),
703                             ETHER_ADDR_LEN) != 0) {
704                                 bcopy(llq->llq_lladdr, IF_LLADDR(ifp),
705                                     ETHER_ADDR_LEN);
706                                 LAGG_WLOCK(sc);
707                                 lagg_proto_lladdr(sc);
708                                 LAGG_WUNLOCK(sc);
709                                 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
710                         }
711                 }
712                 CURVNET_RESTORE();
713                 head = SLIST_NEXT(llq, llq_entries);
714                 free(llq, M_DEVBUF);
715         }
716 }
717
718 static int
719 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
720 {
721         struct lagg_softc *sc_ptr;
722         struct lagg_port *lp, *tlp;
723         int error, i;
724         uint64_t *pval;
725
726         LAGG_WLOCK_ASSERT(sc);
727
728         /* Limit the maximal number of lagg ports */
729         if (sc->sc_count >= LAGG_MAX_PORTS)
730                 return (ENOSPC);
731
732         /* Check if port has already been associated to a lagg */
733         if (ifp->if_lagg != NULL) {
734                 /* Port is already in the current lagg? */
735                 lp = (struct lagg_port *)ifp->if_lagg;
736                 if (lp->lp_softc == sc)
737                         return (EEXIST);
738                 return (EBUSY);
739         }
740
741         /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
742         if (ifp->if_type != IFT_ETHER)
743                 return (EPROTONOSUPPORT);
744
745 #ifdef INET6
746         /*
747          * The member interface should not have inet6 address because
748          * two interfaces with a valid link-local scope zone must not be
749          * merged in any form.  This restriction is needed to
750          * prevent violation of link-local scope zone.  Attempts to
751          * add a member interface which has inet6 addresses triggers
752          * removal of all inet6 addresses on the member interface.
753          */
754         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
755                 if (in6ifa_llaonifp(lp->lp_ifp)) {
756                         in6_ifdetach(lp->lp_ifp);
757                         if_printf(sc->sc_ifp,
758                             "IPv6 addresses on %s have been removed "
759                             "before adding it as a member to prevent "
760                             "IPv6 address scope violation.\n",
761                             lp->lp_ifp->if_xname);
762                 }
763         }
764         if (in6ifa_llaonifp(ifp)) {
765                 in6_ifdetach(ifp);
766                 if_printf(sc->sc_ifp,
767                     "IPv6 addresses on %s have been removed "
768                     "before adding it as a member to prevent "
769                     "IPv6 address scope violation.\n",
770                     ifp->if_xname);
771         }
772 #endif
773         /* Allow the first Ethernet member to define the MTU */
774         if (SLIST_EMPTY(&sc->sc_ports))
775                 sc->sc_ifp->if_mtu = ifp->if_mtu;
776         else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
777                 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
778                     ifp->if_xname);
779                 return (EINVAL);
780         }
781
782         if ((lp = malloc(sizeof(struct lagg_port),
783             M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
784                 return (ENOMEM);
785
786         /* Check if port is a stacked lagg */
787         LAGG_LIST_LOCK();
788         SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
789                 if (ifp == sc_ptr->sc_ifp) {
790                         LAGG_LIST_UNLOCK();
791                         free(lp, M_DEVBUF);
792                         return (EINVAL);
793                         /* XXX disable stacking for the moment, its untested */
794 #ifdef LAGG_PORT_STACKING
795                         lp->lp_flags |= LAGG_PORT_STACK;
796                         if (lagg_port_checkstacking(sc_ptr) >=
797                             LAGG_MAX_STACKING) {
798                                 LAGG_LIST_UNLOCK();
799                                 free(lp, M_DEVBUF);
800                                 return (E2BIG);
801                         }
802 #endif
803                 }
804         }
805         LAGG_LIST_UNLOCK();
806
807         /* Change the interface type */
808         lp->lp_iftype = ifp->if_type;
809         ifp->if_type = IFT_IEEE8023ADLAG;
810         ifp->if_lagg = lp;
811         lp->lp_ioctl = ifp->if_ioctl;
812         ifp->if_ioctl = lagg_port_ioctl;
813         lp->lp_output = ifp->if_output;
814         ifp->if_output = lagg_port_output;
815
816         lp->lp_ifp = ifp;
817         lp->lp_softc = sc;
818
819         /* Save port link layer address */
820         bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
821
822         if (SLIST_EMPTY(&sc->sc_ports)) {
823                 sc->sc_primary = lp;
824                 lagg_lladdr(sc, IF_LLADDR(ifp));
825         } else {
826                 /* Update link layer address for this port */
827                 lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
828         }
829
830         /* Insert into the list of ports. Keep ports sorted by if_index. */
831         SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
832                 if (tlp->lp_ifp->if_index < ifp->if_index && (
833                     SLIST_NEXT(tlp, lp_entries) == NULL ||
834                     SLIST_NEXT(tlp, lp_entries)->lp_ifp->if_index <
835                     ifp->if_index))
836                         break;
837         }
838         if (tlp != NULL)
839                 SLIST_INSERT_AFTER(tlp, lp, lp_entries);
840         else
841                 SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
842         sc->sc_count++;
843
844         /* Update lagg capabilities */
845         lagg_capabilities(sc);
846         lagg_linkstate(sc);
847
848         /* Read port counters */
849         pval = lp->port_counters.val;
850         for (i = 0; i < IFCOUNTERS; i++, pval++)
851                 *pval = ifp->if_get_counter(ifp, i);
852         /* Add multicast addresses and interface flags to this port */
853         lagg_ether_cmdmulti(lp, 1);
854         lagg_setflags(lp, 1);
855
856         if ((error = lagg_proto_addport(sc, lp)) != 0) {
857                 /* Remove the port, without calling pr_delport. */
858                 lagg_port_destroy(lp, 0);
859                 return (error);
860         }
861
862         return (0);
863 }
864
865 #ifdef LAGG_PORT_STACKING
866 static int
867 lagg_port_checkstacking(struct lagg_softc *sc)
868 {
869         struct lagg_softc *sc_ptr;
870         struct lagg_port *lp;
871         int m = 0;
872
873         LAGG_WLOCK_ASSERT(sc);
874
875         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
876                 if (lp->lp_flags & LAGG_PORT_STACK) {
877                         sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
878                         m = MAX(m, lagg_port_checkstacking(sc_ptr));
879                 }
880         }
881
882         return (m + 1);
883 }
884 #endif
885
886 static int
887 lagg_port_destroy(struct lagg_port *lp, int rundelport)
888 {
889         struct lagg_softc *sc = lp->lp_softc;
890         struct lagg_port *lp_ptr;
891         struct lagg_llq *llq;
892         struct ifnet *ifp = lp->lp_ifp;
893         uint64_t *pval, vdiff;
894         int i;
895
896         LAGG_WLOCK_ASSERT(sc);
897
898         if (rundelport)
899                 lagg_proto_delport(sc, lp);
900
901         /*
902          * Remove multicast addresses and interface flags from this port and
903          * reset the MAC address, skip if the interface is being detached.
904          */
905         if (!lp->lp_detaching) {
906                 lagg_ether_cmdmulti(lp, 0);
907                 lagg_setflags(lp, 0);
908                 lagg_port_lladdr(lp, lp->lp_lladdr);
909         }
910
911         /* Restore interface */
912         ifp->if_type = lp->lp_iftype;
913         ifp->if_ioctl = lp->lp_ioctl;
914         ifp->if_output = lp->lp_output;
915         ifp->if_lagg = NULL;
916
917         /* Update detached port counters */
918         pval = lp->port_counters.val;
919         for (i = 0; i < IFCOUNTERS; i++, pval++) {
920                 vdiff = ifp->if_get_counter(ifp, i) - *pval;
921                 sc->detached_counters.val[i] += vdiff;
922         }
923
924         /* Finally, remove the port from the lagg */
925         SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
926         sc->sc_count--;
927
928         /* Update the primary interface */
929         if (lp == sc->sc_primary) {
930                 uint8_t lladdr[ETHER_ADDR_LEN];
931
932                 if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
933                         bzero(&lladdr, ETHER_ADDR_LEN);
934                 } else {
935                         bcopy(lp_ptr->lp_lladdr,
936                             lladdr, ETHER_ADDR_LEN);
937                 }
938                 lagg_lladdr(sc, lladdr);
939                 sc->sc_primary = lp_ptr;
940
941                 /* Update link layer address for each port */
942                 SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
943                         lagg_port_lladdr(lp_ptr, lladdr);
944         }
945
946         /* Remove any pending lladdr changes from the queue */
947         if (lp->lp_detaching) {
948                 SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
949                         if (llq->llq_ifp == ifp) {
950                                 SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
951                                     llq_entries);
952                                 free(llq, M_DEVBUF);
953                                 break;  /* Only appears once */
954                         }
955                 }
956         }
957
958         if (lp->lp_ifflags)
959                 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
960
961         free(lp, M_DEVBUF);
962
963         /* Update lagg capabilities */
964         lagg_capabilities(sc);
965         lagg_linkstate(sc);
966
967         return (0);
968 }
969
970 static int
971 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
972 {
973         struct lagg_reqport *rp = (struct lagg_reqport *)data;
974         struct lagg_softc *sc;
975         struct lagg_port *lp = NULL;
976         int error = 0;
977         struct rm_priotracker tracker;
978
979         /* Should be checked by the caller */
980         if (ifp->if_type != IFT_IEEE8023ADLAG ||
981             (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
982                 goto fallback;
983
984         switch (cmd) {
985         case SIOCGLAGGPORT:
986                 if (rp->rp_portname[0] == '\0' ||
987                     ifunit(rp->rp_portname) != ifp) {
988                         error = EINVAL;
989                         break;
990                 }
991
992                 LAGG_RLOCK(sc, &tracker);
993                 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
994                         error = ENOENT;
995                         LAGG_RUNLOCK(sc, &tracker);
996                         break;
997                 }
998
999                 lagg_port2req(lp, rp);
1000                 LAGG_RUNLOCK(sc, &tracker);
1001                 break;
1002
1003         case SIOCSIFCAP:
1004                 if (lp->lp_ioctl == NULL) {
1005                         error = EINVAL;
1006                         break;
1007                 }
1008                 error = (*lp->lp_ioctl)(ifp, cmd, data);
1009                 if (error)
1010                         break;
1011
1012                 /* Update lagg interface capabilities */
1013                 LAGG_WLOCK(sc);
1014                 lagg_capabilities(sc);
1015                 LAGG_WUNLOCK(sc);
1016                 break;
1017
1018         case SIOCSIFMTU:
1019                 /* Do not allow the MTU to be changed once joined */
1020                 error = EINVAL;
1021                 break;
1022
1023         default:
1024                 goto fallback;
1025         }
1026
1027         return (error);
1028
1029 fallback:
1030         if (lp->lp_ioctl != NULL)
1031                 return ((*lp->lp_ioctl)(ifp, cmd, data));
1032
1033         return (EINVAL);
1034 }
1035
1036 /*
1037  * Requests counter @cnt data. 
1038  *
1039  * Counter value is calculated the following way:
1040  * 1) for each port, sum  difference between current and "initial" measurements.
1041  * 2) add lagg logical interface counters.
1042  * 3) add data from detached_counters array.
1043  *
1044  * We also do the following things on ports attach/detach:
1045  * 1) On port attach we store all counters it has into port_counter array. 
1046  * 2) On port detach we add the different between "initial" and
1047  *   current counters data to detached_counters array.
1048  */
1049 static uint64_t
1050 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1051 {
1052         struct lagg_softc *sc;
1053         struct lagg_port *lp;
1054         struct ifnet *lpifp;
1055         struct rm_priotracker tracker;
1056         uint64_t newval, oldval, vsum;
1057
1058         /* Revise this when we've got non-generic counters. */
1059         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1060
1061         sc = (struct lagg_softc *)ifp->if_softc;
1062         LAGG_RLOCK(sc, &tracker);
1063
1064         vsum = 0;
1065         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1066                 /* Saved attached value */
1067                 oldval = lp->port_counters.val[cnt];
1068                 /* current value */
1069                 lpifp = lp->lp_ifp;
1070                 newval = lpifp->if_get_counter(lpifp, cnt);
1071                 /* Calculate diff and save new */
1072                 vsum += newval - oldval;
1073         }
1074
1075         /*
1076          * Add counter data which might be added by upper
1077          * layer protocols operating on logical interface.
1078          */
1079         vsum += if_get_counter_default(ifp, cnt);
1080
1081         /*
1082          * Add counter data from detached ports counters
1083          */
1084         vsum += sc->detached_counters.val[cnt];
1085
1086         LAGG_RUNLOCK(sc, &tracker);
1087
1088         return (vsum);
1089 }
1090
1091 /*
1092  * For direct output to child ports.
1093  */
1094 static int
1095 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1096         const struct sockaddr *dst, struct route *ro)
1097 {
1098         struct lagg_port *lp = ifp->if_lagg;
1099
1100         switch (dst->sa_family) {
1101                 case pseudo_AF_HDRCMPLT:
1102                 case AF_UNSPEC:
1103                         return ((*lp->lp_output)(ifp, m, dst, ro));
1104         }
1105
1106         /* drop any other frames */
1107         m_freem(m);
1108         return (ENETDOWN);
1109 }
1110
1111 static void
1112 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1113 {
1114         struct lagg_port *lp;
1115         struct lagg_softc *sc;
1116
1117         if ((lp = ifp->if_lagg) == NULL)
1118                 return;
1119         /* If the ifnet is just being renamed, don't do anything. */
1120         if (ifp->if_flags & IFF_RENAMING)
1121                 return;
1122
1123         sc = lp->lp_softc;
1124
1125         LAGG_WLOCK(sc);
1126         lp->lp_detaching = 1;
1127         lagg_port_destroy(lp, 1);
1128         LAGG_WUNLOCK(sc);
1129 }
1130
1131 static void
1132 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1133 {
1134         struct lagg_softc *sc = lp->lp_softc;
1135
1136         strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1137         strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1138         rp->rp_prio = lp->lp_prio;
1139         rp->rp_flags = lp->lp_flags;
1140         lagg_proto_portreq(sc, lp, &rp->rp_psc);
1141
1142         /* Add protocol specific flags */
1143         switch (sc->sc_proto) {
1144                 case LAGG_PROTO_FAILOVER:
1145                         if (lp == sc->sc_primary)
1146                                 rp->rp_flags |= LAGG_PORT_MASTER;
1147                         if (lp == lagg_link_active(sc, sc->sc_primary))
1148                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1149                         break;
1150
1151                 case LAGG_PROTO_ROUNDROBIN:
1152                 case LAGG_PROTO_LOADBALANCE:
1153                 case LAGG_PROTO_ETHERCHANNEL:
1154                 case LAGG_PROTO_BROADCAST:
1155                         if (LAGG_PORTACTIVE(lp))
1156                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1157                         break;
1158
1159                 case LAGG_PROTO_LACP:
1160                         /* LACP has a different definition of active */
1161                         if (lacp_isactive(lp))
1162                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1163                         if (lacp_iscollecting(lp))
1164                                 rp->rp_flags |= LAGG_PORT_COLLECTING;
1165                         if (lacp_isdistributing(lp))
1166                                 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1167                         break;
1168         }
1169
1170 }
1171
1172 static void
1173 lagg_init(void *xsc)
1174 {
1175         struct lagg_softc *sc = (struct lagg_softc *)xsc;
1176         struct lagg_port *lp;
1177         struct ifnet *ifp = sc->sc_ifp;
1178
1179         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1180                 return;
1181
1182         LAGG_WLOCK(sc);
1183
1184         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1185         /* Update the port lladdrs */
1186         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1187                 lagg_port_lladdr(lp, IF_LLADDR(ifp));
1188
1189         lagg_proto_init(sc);
1190
1191         LAGG_WUNLOCK(sc);
1192 }
1193
1194 static void
1195 lagg_stop(struct lagg_softc *sc)
1196 {
1197         struct ifnet *ifp = sc->sc_ifp;
1198
1199         LAGG_WLOCK_ASSERT(sc);
1200
1201         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1202                 return;
1203
1204         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1205
1206         lagg_proto_stop(sc);
1207 }
1208
1209 static int
1210 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1211 {
1212         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1213         struct lagg_reqall *ra = (struct lagg_reqall *)data;
1214         struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1215         struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1216         struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1217         struct ifreq *ifr = (struct ifreq *)data;
1218         struct lagg_port *lp;
1219         struct ifnet *tpif;
1220         struct thread *td = curthread;
1221         char *buf, *outbuf;
1222         int count, buflen, len, error = 0;
1223         struct rm_priotracker tracker;
1224
1225         bzero(&rpbuf, sizeof(rpbuf));
1226
1227         switch (cmd) {
1228         case SIOCGLAGG:
1229                 LAGG_RLOCK(sc, &tracker);
1230                 count = 0;
1231                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1232                         count++;
1233                 buflen = count * sizeof(struct lagg_reqport);
1234                 LAGG_RUNLOCK(sc, &tracker);
1235
1236                 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1237
1238                 LAGG_RLOCK(sc, &tracker);
1239                 ra->ra_proto = sc->sc_proto;
1240                 lagg_proto_request(sc, &ra->ra_psc);
1241                 count = 0;
1242                 buf = outbuf;
1243                 len = min(ra->ra_size, buflen);
1244                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1245                         if (len < sizeof(rpbuf))
1246                                 break;
1247
1248                         lagg_port2req(lp, &rpbuf);
1249                         memcpy(buf, &rpbuf, sizeof(rpbuf));
1250                         count++;
1251                         buf += sizeof(rpbuf);
1252                         len -= sizeof(rpbuf);
1253                 }
1254                 LAGG_RUNLOCK(sc, &tracker);
1255                 ra->ra_ports = count;
1256                 ra->ra_size = count * sizeof(rpbuf);
1257                 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1258                 free(outbuf, M_TEMP);
1259                 break;
1260         case SIOCSLAGG:
1261                 error = priv_check(td, PRIV_NET_LAGG);
1262                 if (error)
1263                         break;
1264                 if (ra->ra_proto < 1 || ra->ra_proto >= LAGG_PROTO_MAX) {
1265                         error = EPROTONOSUPPORT;
1266                         break;
1267                 }
1268
1269                 LAGG_WLOCK(sc);
1270                 lagg_proto_detach(sc);
1271                 lagg_proto_attach(sc, ra->ra_proto);
1272                 break;
1273         case SIOCGLAGGOPTS:
1274                 ro->ro_opts = sc->sc_opts;
1275                 if (sc->sc_proto == LAGG_PROTO_LACP) {
1276                         struct lacp_softc *lsc;
1277
1278                         lsc = (struct lacp_softc *)sc->sc_psc;
1279                         if (lsc->lsc_debug.lsc_tx_test != 0)
1280                                 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1281                         if (lsc->lsc_debug.lsc_rx_test != 0)
1282                                 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1283                         if (lsc->lsc_strict_mode != 0)
1284                                 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1285
1286                         ro->ro_active = sc->sc_active;
1287                 } else {
1288                         ro->ro_active = 0;
1289                         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1290                                 ro->ro_active += LAGG_PORTACTIVE(lp);
1291                 }
1292                 ro->ro_flapping = sc->sc_flapping;
1293                 ro->ro_flowid_shift = sc->flowid_shift;
1294                 break;
1295         case SIOCSLAGGOPTS:
1296                 error = priv_check(td, PRIV_NET_LAGG);
1297                 if (error)
1298                         break;
1299                 if (ro->ro_opts == 0)
1300                         break;
1301                 /*
1302                  * Set options.  LACP options are stored in sc->sc_psc,
1303                  * not in sc_opts.
1304                  */
1305                 int valid, lacp;
1306
1307                 switch (ro->ro_opts) {
1308                 case LAGG_OPT_USE_FLOWID:
1309                 case -LAGG_OPT_USE_FLOWID:
1310                 case LAGG_OPT_FLOWIDSHIFT:
1311                         valid = 1;
1312                         lacp = 0;
1313                         break;
1314                 case LAGG_OPT_LACP_TXTEST:
1315                 case -LAGG_OPT_LACP_TXTEST:
1316                 case LAGG_OPT_LACP_RXTEST:
1317                 case -LAGG_OPT_LACP_RXTEST:
1318                 case LAGG_OPT_LACP_STRICT:
1319                 case -LAGG_OPT_LACP_STRICT:
1320                         valid = lacp = 1;
1321                         break;
1322                 default:
1323                         valid = lacp = 0;
1324                         break;
1325                 }
1326
1327                 LAGG_WLOCK(sc);
1328                 if (valid == 0 ||
1329                     (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1330                         /* Invalid combination of options specified. */
1331                         error = EINVAL;
1332                         LAGG_WUNLOCK(sc);
1333                         break;  /* Return from SIOCSLAGGOPTS. */ 
1334                 }
1335                 /*
1336                  * Store new options into sc->sc_opts except for
1337                  * FLOWIDSHIFT and LACP options.
1338                  */
1339                 if (lacp == 0) {
1340                         if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1341                                 sc->flowid_shift = ro->ro_flowid_shift;
1342                         else if (ro->ro_opts > 0)
1343                                 sc->sc_opts |= ro->ro_opts;
1344                         else
1345                                 sc->sc_opts &= ~ro->ro_opts;
1346                 } else {
1347                         struct lacp_softc *lsc;
1348
1349                         lsc = (struct lacp_softc *)sc->sc_psc;
1350
1351                         switch (ro->ro_opts) {
1352                         case LAGG_OPT_LACP_TXTEST:
1353                                 lsc->lsc_debug.lsc_tx_test = 1;
1354                                 break;
1355                         case -LAGG_OPT_LACP_TXTEST:
1356                                 lsc->lsc_debug.lsc_tx_test = 0;
1357                                 break;
1358                         case LAGG_OPT_LACP_RXTEST:
1359                                 lsc->lsc_debug.lsc_rx_test = 1;
1360                                 break;
1361                         case -LAGG_OPT_LACP_RXTEST:
1362                                 lsc->lsc_debug.lsc_rx_test = 0;
1363                                 break;
1364                         case LAGG_OPT_LACP_STRICT:
1365                                 lsc->lsc_strict_mode = 1;
1366                                 break;
1367                         case -LAGG_OPT_LACP_STRICT:
1368                                 lsc->lsc_strict_mode = 0;
1369                                 break;
1370                         }
1371                 }
1372                 LAGG_WUNLOCK(sc);
1373                 break;
1374         case SIOCGLAGGFLAGS:
1375                 rf->rf_flags = sc->sc_flags;
1376                 break;
1377         case SIOCSLAGGHASH:
1378                 error = priv_check(td, PRIV_NET_LAGG);
1379                 if (error)
1380                         break;
1381                 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1382                         error = EINVAL;
1383                         break;
1384                 }
1385                 LAGG_WLOCK(sc);
1386                 sc->sc_flags &= ~LAGG_F_HASHMASK;
1387                 sc->sc_flags |= rf->rf_flags & LAGG_F_HASHMASK;
1388                 LAGG_WUNLOCK(sc);
1389                 break;
1390         case SIOCGLAGGPORT:
1391                 if (rp->rp_portname[0] == '\0' ||
1392                     (tpif = ifunit(rp->rp_portname)) == NULL) {
1393                         error = EINVAL;
1394                         break;
1395                 }
1396
1397                 LAGG_RLOCK(sc, &tracker);
1398                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1399                     lp->lp_softc != sc) {
1400                         error = ENOENT;
1401                         LAGG_RUNLOCK(sc, &tracker);
1402                         break;
1403                 }
1404
1405                 lagg_port2req(lp, rp);
1406                 LAGG_RUNLOCK(sc, &tracker);
1407                 break;
1408         case SIOCSLAGGPORT:
1409                 error = priv_check(td, PRIV_NET_LAGG);
1410                 if (error)
1411                         break;
1412                 if (rp->rp_portname[0] == '\0' ||
1413                     (tpif = ifunit(rp->rp_portname)) == NULL) {
1414                         error = EINVAL;
1415                         break;
1416                 }
1417                 LAGG_WLOCK(sc);
1418                 error = lagg_port_create(sc, tpif);
1419                 LAGG_WUNLOCK(sc);
1420                 break;
1421         case SIOCSLAGGDELPORT:
1422                 error = priv_check(td, PRIV_NET_LAGG);
1423                 if (error)
1424                         break;
1425                 if (rp->rp_portname[0] == '\0' ||
1426                     (tpif = ifunit(rp->rp_portname)) == NULL) {
1427                         error = EINVAL;
1428                         break;
1429                 }
1430
1431                 LAGG_WLOCK(sc);
1432                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1433                     lp->lp_softc != sc) {
1434                         error = ENOENT;
1435                         LAGG_WUNLOCK(sc);
1436                         break;
1437                 }
1438
1439                 error = lagg_port_destroy(lp, 1);
1440                 LAGG_WUNLOCK(sc);
1441                 break;
1442         case SIOCSIFFLAGS:
1443                 /* Set flags on ports too */
1444                 LAGG_WLOCK(sc);
1445                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1446                         lagg_setflags(lp, 1);
1447                 }
1448                 LAGG_WUNLOCK(sc);
1449
1450                 if (!(ifp->if_flags & IFF_UP) &&
1451                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1452                         /*
1453                          * If interface is marked down and it is running,
1454                          * then stop and disable it.
1455                          */
1456                         LAGG_WLOCK(sc);
1457                         lagg_stop(sc);
1458                         LAGG_WUNLOCK(sc);
1459                 } else if ((ifp->if_flags & IFF_UP) &&
1460                     !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1461                         /*
1462                          * If interface is marked up and it is stopped, then
1463                          * start it.
1464                          */
1465                         (*ifp->if_init)(sc);
1466                 }
1467                 break;
1468         case SIOCADDMULTI:
1469         case SIOCDELMULTI:
1470                 LAGG_WLOCK(sc);
1471                 error = lagg_ether_setmulti(sc);
1472                 LAGG_WUNLOCK(sc);
1473                 break;
1474         case SIOCSIFMEDIA:
1475         case SIOCGIFMEDIA:
1476                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1477                 break;
1478
1479         case SIOCSIFCAP:
1480         case SIOCSIFMTU:
1481                 /* Do not allow the MTU or caps to be directly changed */
1482                 error = EINVAL;
1483                 break;
1484
1485         default:
1486                 error = ether_ioctl(ifp, cmd, data);
1487                 break;
1488         }
1489         return (error);
1490 }
1491
1492 static int
1493 lagg_ether_setmulti(struct lagg_softc *sc)
1494 {
1495         struct lagg_port *lp;
1496
1497         LAGG_WLOCK_ASSERT(sc);
1498
1499         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1500                 /* First, remove any existing filter entries. */
1501                 lagg_ether_cmdmulti(lp, 0);
1502                 /* copy all addresses from the lagg interface to the port */
1503                 lagg_ether_cmdmulti(lp, 1);
1504         }
1505         return (0);
1506 }
1507
1508 static int
1509 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1510 {
1511         struct lagg_softc *sc = lp->lp_softc;
1512         struct ifnet *ifp = lp->lp_ifp;
1513         struct ifnet *scifp = sc->sc_ifp;
1514         struct lagg_mc *mc;
1515         struct ifmultiaddr *ifma;
1516         int error;
1517
1518         LAGG_WLOCK_ASSERT(sc);
1519
1520         if (set) {
1521                 IF_ADDR_WLOCK(scifp);
1522                 TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1523                         if (ifma->ifma_addr->sa_family != AF_LINK)
1524                                 continue;
1525                         mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1526                         if (mc == NULL) {
1527                                 IF_ADDR_WUNLOCK(scifp);
1528                                 return (ENOMEM);
1529                         }
1530                         bcopy(ifma->ifma_addr, &mc->mc_addr,
1531                             ifma->ifma_addr->sa_len);
1532                         mc->mc_addr.sdl_index = ifp->if_index;
1533                         mc->mc_ifma = NULL;
1534                         SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1535                 }
1536                 IF_ADDR_WUNLOCK(scifp);
1537                 SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
1538                         error = if_addmulti(ifp,
1539                             (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
1540                         if (error)
1541                                 return (error);
1542                 }
1543         } else {
1544                 while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1545                         SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1546                         if (mc->mc_ifma && !lp->lp_detaching)
1547                                 if_delmulti_ifma(mc->mc_ifma);
1548                         free(mc, M_DEVBUF);
1549                 }
1550         }
1551         return (0);
1552 }
1553
1554 /* Handle a ref counted flag that should be set on the lagg port as well */
1555 static int
1556 lagg_setflag(struct lagg_port *lp, int flag, int status,
1557     int (*func)(struct ifnet *, int))
1558 {
1559         struct lagg_softc *sc = lp->lp_softc;
1560         struct ifnet *scifp = sc->sc_ifp;
1561         struct ifnet *ifp = lp->lp_ifp;
1562         int error;
1563
1564         LAGG_WLOCK_ASSERT(sc);
1565
1566         status = status ? (scifp->if_flags & flag) : 0;
1567         /* Now "status" contains the flag value or 0 */
1568
1569         /*
1570          * See if recorded ports status is different from what
1571          * we want it to be.  If it is, flip it.  We record ports
1572          * status in lp_ifflags so that we won't clear ports flag
1573          * we haven't set.  In fact, we don't clear or set ports
1574          * flags directly, but get or release references to them.
1575          * That's why we can be sure that recorded flags still are
1576          * in accord with actual ports flags.
1577          */
1578         if (status != (lp->lp_ifflags & flag)) {
1579                 error = (*func)(ifp, status);
1580                 if (error)
1581                         return (error);
1582                 lp->lp_ifflags &= ~flag;
1583                 lp->lp_ifflags |= status;
1584         }
1585         return (0);
1586 }
1587
1588 /*
1589  * Handle IFF_* flags that require certain changes on the lagg port
1590  * if "status" is true, update ports flags respective to the lagg
1591  * if "status" is false, forcedly clear the flags set on port.
1592  */
1593 static int
1594 lagg_setflags(struct lagg_port *lp, int status)
1595 {
1596         int error, i;
1597
1598         for (i = 0; lagg_pflags[i].flag; i++) {
1599                 error = lagg_setflag(lp, lagg_pflags[i].flag,
1600                     status, lagg_pflags[i].func);
1601                 if (error)
1602                         return (error);
1603         }
1604         return (0);
1605 }
1606
1607 static int
1608 lagg_transmit(struct ifnet *ifp, struct mbuf *m)
1609 {
1610         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1611         int error, len, mcast;
1612         struct rm_priotracker tracker;
1613
1614         len = m->m_pkthdr.len;
1615         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1616
1617         LAGG_RLOCK(sc, &tracker);
1618         /* We need a Tx algorithm and at least one port */
1619         if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1620                 LAGG_RUNLOCK(sc, &tracker);
1621                 m_freem(m);
1622                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1623                 return (ENXIO);
1624         }
1625
1626         ETHER_BPF_MTAP(ifp, m);
1627
1628         error = lagg_proto_start(sc, m);
1629         LAGG_RUNLOCK(sc, &tracker);
1630
1631         if (error != 0)
1632                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1633
1634         return (error);
1635 }
1636
1637 /*
1638  * The ifp->if_qflush entry point for lagg(4) is no-op.
1639  */
1640 static void
1641 lagg_qflush(struct ifnet *ifp __unused)
1642 {
1643 }
1644
1645 static struct mbuf *
1646 lagg_input(struct ifnet *ifp, struct mbuf *m)
1647 {
1648         struct lagg_port *lp = ifp->if_lagg;
1649         struct lagg_softc *sc = lp->lp_softc;
1650         struct ifnet *scifp = sc->sc_ifp;
1651         struct rm_priotracker tracker;
1652
1653         LAGG_RLOCK(sc, &tracker);
1654         if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1655             (lp->lp_flags & LAGG_PORT_DISABLED) ||
1656             sc->sc_proto == LAGG_PROTO_NONE) {
1657                 LAGG_RUNLOCK(sc, &tracker);
1658                 m_freem(m);
1659                 return (NULL);
1660         }
1661
1662         ETHER_BPF_MTAP(scifp, m);
1663
1664         m = (lp->lp_detaching == 0) ? lagg_proto_input(sc, lp, m) : NULL;
1665
1666         if (m != NULL) {
1667                 if (scifp->if_flags & IFF_MONITOR) {
1668                         m_freem(m);
1669                         m = NULL;
1670                 }
1671         }
1672
1673         LAGG_RUNLOCK(sc, &tracker);
1674         return (m);
1675 }
1676
1677 static int
1678 lagg_media_change(struct ifnet *ifp)
1679 {
1680         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1681
1682         if (sc->sc_ifflags & IFF_DEBUG)
1683                 printf("%s\n", __func__);
1684
1685         /* Ignore */
1686         return (0);
1687 }
1688
1689 static void
1690 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1691 {
1692         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1693         struct lagg_port *lp;
1694         struct rm_priotracker tracker;
1695
1696         imr->ifm_status = IFM_AVALID;
1697         imr->ifm_active = IFM_ETHER | IFM_AUTO;
1698
1699         LAGG_RLOCK(sc, &tracker);
1700         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1701                 if (LAGG_PORTACTIVE(lp))
1702                         imr->ifm_status |= IFM_ACTIVE;
1703         }
1704         LAGG_RUNLOCK(sc, &tracker);
1705 }
1706
1707 static void
1708 lagg_linkstate(struct lagg_softc *sc)
1709 {
1710         struct lagg_port *lp;
1711         int new_link = LINK_STATE_DOWN;
1712         uint64_t speed;
1713
1714         /* Our link is considered up if at least one of our ports is active */
1715         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1716                 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
1717                         new_link = LINK_STATE_UP;
1718                         break;
1719                 }
1720         }
1721         if_link_state_change(sc->sc_ifp, new_link);
1722
1723         /* Update if_baudrate to reflect the max possible speed */
1724         switch (sc->sc_proto) {
1725                 case LAGG_PROTO_FAILOVER:
1726                         sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1727                             sc->sc_primary->lp_ifp->if_baudrate : 0;
1728                         break;
1729                 case LAGG_PROTO_ROUNDROBIN:
1730                 case LAGG_PROTO_LOADBALANCE:
1731                 case LAGG_PROTO_ETHERCHANNEL:
1732                 case LAGG_PROTO_BROADCAST:
1733                         speed = 0;
1734                         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1735                                 speed += lp->lp_ifp->if_baudrate;
1736                         sc->sc_ifp->if_baudrate = speed;
1737                         break;
1738                 case LAGG_PROTO_LACP:
1739                         /* LACP updates if_baudrate itself */
1740                         break;
1741         }
1742 }
1743
1744 static void
1745 lagg_port_state(struct ifnet *ifp, int state)
1746 {
1747         struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1748         struct lagg_softc *sc = NULL;
1749
1750         if (lp != NULL)
1751                 sc = lp->lp_softc;
1752         if (sc == NULL)
1753                 return;
1754
1755         LAGG_WLOCK(sc);
1756         lagg_linkstate(sc);
1757         lagg_proto_linkstate(sc, lp);
1758         LAGG_WUNLOCK(sc);
1759 }
1760
1761 struct lagg_port *
1762 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1763 {
1764         struct lagg_port *lp_next, *rval = NULL;
1765         // int new_link = LINK_STATE_DOWN;
1766
1767         LAGG_RLOCK_ASSERT(sc);
1768         /*
1769          * Search a port which reports an active link state.
1770          */
1771
1772         if (lp == NULL)
1773                 goto search;
1774         if (LAGG_PORTACTIVE(lp)) {
1775                 rval = lp;
1776                 goto found;
1777         }
1778         if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1779             LAGG_PORTACTIVE(lp_next)) {
1780                 rval = lp_next;
1781                 goto found;
1782         }
1783
1784 search:
1785         SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1786                 if (LAGG_PORTACTIVE(lp_next)) {
1787                         rval = lp_next;
1788                         goto found;
1789                 }
1790         }
1791
1792 found:
1793         if (rval != NULL) {
1794                 /*
1795                  * The IEEE 802.1D standard assumes that a lagg with
1796                  * multiple ports is always full duplex. This is valid
1797                  * for load sharing laggs and if at least two links
1798                  * are active. Unfortunately, checking the latter would
1799                  * be too expensive at this point.
1800                  XXX
1801                 if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1802                     (sc->sc_count > 1))
1803                         new_link = LINK_STATE_FULL_DUPLEX;
1804                 else
1805                         new_link = rval->lp_link_state;
1806                  */
1807         }
1808
1809         return (rval);
1810 }
1811
1812 static const void *
1813 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1814 {
1815         if (m->m_pkthdr.len < (off + len)) {
1816                 return (NULL);
1817         } else if (m->m_len < (off + len)) {
1818                 m_copydata(m, off, len, buf);
1819                 return (buf);
1820         }
1821         return (mtod(m, char *) + off);
1822 }
1823
1824 uint32_t
1825 lagg_hashmbuf(struct lagg_softc *sc, struct mbuf *m, uint32_t key)
1826 {
1827         uint16_t etype;
1828         uint32_t p = key;
1829         int off;
1830         struct ether_header *eh;
1831         const struct ether_vlan_header *vlan;
1832 #ifdef INET
1833         const struct ip *ip;
1834         const uint32_t *ports;
1835         int iphlen;
1836 #endif
1837 #ifdef INET6
1838         const struct ip6_hdr *ip6;
1839         uint32_t flow;
1840 #endif
1841         union {
1842 #ifdef INET
1843                 struct ip ip;
1844 #endif
1845 #ifdef INET6
1846                 struct ip6_hdr ip6;
1847 #endif
1848                 struct ether_vlan_header vlan;
1849                 uint32_t port;
1850         } buf;
1851
1852
1853         off = sizeof(*eh);
1854         if (m->m_len < off)
1855                 goto out;
1856         eh = mtod(m, struct ether_header *);
1857         etype = ntohs(eh->ether_type);
1858         if (sc->sc_flags & LAGG_F_HASHL2) {
1859                 p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
1860                 p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1861         }
1862
1863         /* Special handling for encapsulating VLAN frames */
1864         if ((m->m_flags & M_VLANTAG) && (sc->sc_flags & LAGG_F_HASHL2)) {
1865                 p = hash32_buf(&m->m_pkthdr.ether_vtag,
1866                     sizeof(m->m_pkthdr.ether_vtag), p);
1867         } else if (etype == ETHERTYPE_VLAN) {
1868                 vlan = lagg_gethdr(m, off,  sizeof(*vlan), &buf);
1869                 if (vlan == NULL)
1870                         goto out;
1871
1872                 if (sc->sc_flags & LAGG_F_HASHL2)
1873                         p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1874                 etype = ntohs(vlan->evl_proto);
1875                 off += sizeof(*vlan) - sizeof(*eh);
1876         }
1877
1878         switch (etype) {
1879 #ifdef INET
1880         case ETHERTYPE_IP:
1881                 ip = lagg_gethdr(m, off, sizeof(*ip), &buf);
1882                 if (ip == NULL)
1883                         goto out;
1884
1885                 if (sc->sc_flags & LAGG_F_HASHL3) {
1886                         p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1887                         p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1888                 }
1889                 if (!(sc->sc_flags & LAGG_F_HASHL4))
1890                         break;
1891                 switch (ip->ip_p) {
1892                         case IPPROTO_TCP:
1893                         case IPPROTO_UDP:
1894                         case IPPROTO_SCTP:
1895                                 iphlen = ip->ip_hl << 2;
1896                                 if (iphlen < sizeof(*ip))
1897                                         break;
1898                                 off += iphlen;
1899                                 ports = lagg_gethdr(m, off, sizeof(*ports), &buf);
1900                                 if (ports == NULL)
1901                                         break;
1902                                 p = hash32_buf(ports, sizeof(*ports), p);
1903                                 break;
1904                 }
1905                 break;
1906 #endif
1907 #ifdef INET6
1908         case ETHERTYPE_IPV6:
1909                 if (!(sc->sc_flags & LAGG_F_HASHL3))
1910                         break;
1911                 ip6 = lagg_gethdr(m, off, sizeof(*ip6), &buf);
1912                 if (ip6 == NULL)
1913                         goto out;
1914
1915                 p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1916                 p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1917                 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1918                 p = hash32_buf(&flow, sizeof(flow), p); /* IPv6 flow label */
1919                 break;
1920 #endif
1921         }
1922 out:
1923         return (p);
1924 }
1925
1926 int
1927 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1928 {
1929
1930         return (ifp->if_transmit)(ifp, m);
1931 }
1932
1933 /*
1934  * Simple round robin aggregation
1935  */
1936 static void
1937 lagg_rr_attach(struct lagg_softc *sc)
1938 {
1939         sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1940         sc->sc_seq = 0;
1941 }
1942
1943 static int
1944 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1945 {
1946         struct lagg_port *lp;
1947         uint32_t p;
1948
1949         p = atomic_fetchadd_32(&sc->sc_seq, 1);
1950         p %= sc->sc_count;
1951         lp = SLIST_FIRST(&sc->sc_ports);
1952         while (p--)
1953                 lp = SLIST_NEXT(lp, lp_entries);
1954
1955         /*
1956          * Check the port's link state. This will return the next active
1957          * port if the link is down or the port is NULL.
1958          */
1959         if ((lp = lagg_link_active(sc, lp)) == NULL) {
1960                 m_freem(m);
1961                 return (ENETDOWN);
1962         }
1963
1964         /* Send mbuf */
1965         return (lagg_enqueue(lp->lp_ifp, m));
1966 }
1967
1968 static struct mbuf *
1969 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1970 {
1971         struct ifnet *ifp = sc->sc_ifp;
1972
1973         /* Just pass in the packet to our lagg device */
1974         m->m_pkthdr.rcvif = ifp;
1975
1976         return (m);
1977 }
1978
1979 /*
1980  * Broadcast mode
1981  */
1982 static int
1983 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
1984 {
1985         int active_ports = 0;
1986         int errors = 0;
1987         int ret;
1988         struct lagg_port *lp, *last = NULL;
1989         struct mbuf *m0;
1990
1991         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1992                 if (!LAGG_PORTACTIVE(lp))
1993                         continue;
1994
1995                 active_ports++;
1996
1997                 if (last != NULL) {
1998                         m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1999                         if (m0 == NULL) {
2000                                 ret = ENOBUFS;
2001                                 errors++;
2002                                 break;
2003                         }
2004
2005                         ret = lagg_enqueue(last->lp_ifp, m0);
2006                         if (ret != 0)
2007                                 errors++;
2008                 }
2009                 last = lp;
2010         }
2011         if (last == NULL) {
2012                 m_freem(m);
2013                 return (ENOENT);
2014         }
2015         if ((last = lagg_link_active(sc, last)) == NULL) {
2016                 m_freem(m);
2017                 return (ENETDOWN);
2018         }
2019
2020         ret = lagg_enqueue(last->lp_ifp, m);
2021         if (ret != 0)
2022                 errors++;
2023
2024         if (errors == 0)
2025                 return (ret);
2026
2027         return (0);
2028 }
2029
2030 static struct mbuf*
2031 lagg_bcast_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2032 {
2033         struct ifnet *ifp = sc->sc_ifp;
2034
2035         /* Just pass in the packet to our lagg device */
2036         m->m_pkthdr.rcvif = ifp;
2037         return (m);
2038 }
2039
2040 /*
2041  * Active failover
2042  */
2043 static int
2044 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2045 {
2046         struct lagg_port *lp;
2047
2048         /* Use the master port if active or the next available port */
2049         if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2050                 m_freem(m);
2051                 return (ENETDOWN);
2052         }
2053
2054         /* Send mbuf */
2055         return (lagg_enqueue(lp->lp_ifp, m));
2056 }
2057
2058 static struct mbuf *
2059 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2060 {
2061         struct ifnet *ifp = sc->sc_ifp;
2062         struct lagg_port *tmp_tp;
2063
2064         if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2065                 m->m_pkthdr.rcvif = ifp;
2066                 return (m);
2067         }
2068
2069         if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2070                 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2071                 /*
2072                  * If tmp_tp is null, we've recieved a packet when all
2073                  * our links are down. Weird, but process it anyways.
2074                  */
2075                 if ((tmp_tp == NULL || tmp_tp == lp)) {
2076                         m->m_pkthdr.rcvif = ifp;
2077                         return (m);
2078                 }
2079         }
2080
2081         m_freem(m);
2082         return (NULL);
2083 }
2084
2085 /*
2086  * Loadbalancing
2087  */
2088 static void
2089 lagg_lb_attach(struct lagg_softc *sc)
2090 {
2091         struct lagg_port *lp;
2092         struct lagg_lb *lb;
2093
2094         lb = malloc(sizeof(struct lagg_lb), M_DEVBUF, M_WAITOK | M_ZERO);
2095
2096         sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
2097
2098         lb->lb_key = arc4random();
2099         sc->sc_psc = lb;
2100
2101         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2102                 lagg_lb_port_create(lp);
2103 }
2104
2105 static void
2106 lagg_lb_detach(struct lagg_softc *sc)
2107 {
2108         struct lagg_lb *lb;
2109
2110         lb = (struct lagg_lb *)sc->sc_psc;
2111         LAGG_WUNLOCK(sc);
2112         if (lb != NULL)
2113                 free(lb, M_DEVBUF);
2114 }
2115
2116 static int
2117 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2118 {
2119         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2120         struct lagg_port *lp_next;
2121         int i = 0;
2122
2123         bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2124         SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2125                 if (lp_next == lp)
2126                         continue;
2127                 if (i >= LAGG_MAX_PORTS)
2128                         return (EINVAL);
2129                 if (sc->sc_ifflags & IFF_DEBUG)
2130                         printf("%s: port %s at index %d\n",
2131                             sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2132                 lb->lb_ports[i++] = lp_next;
2133         }
2134
2135         return (0);
2136 }
2137
2138 static int
2139 lagg_lb_port_create(struct lagg_port *lp)
2140 {
2141         struct lagg_softc *sc = lp->lp_softc;
2142         return (lagg_lb_porttable(sc, NULL));
2143 }
2144
2145 static void
2146 lagg_lb_port_destroy(struct lagg_port *lp)
2147 {
2148         struct lagg_softc *sc = lp->lp_softc;
2149         lagg_lb_porttable(sc, lp);
2150 }
2151
2152 static int
2153 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2154 {
2155         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2156         struct lagg_port *lp = NULL;
2157         uint32_t p = 0;
2158
2159         if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) && (m->m_flags & M_FLOWID))
2160                 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2161         else
2162                 p = lagg_hashmbuf(sc, m, lb->lb_key);
2163         p %= sc->sc_count;
2164         lp = lb->lb_ports[p];
2165
2166         /*
2167          * Check the port's link state. This will return the next active
2168          * port if the link is down or the port is NULL.
2169          */
2170         if ((lp = lagg_link_active(sc, lp)) == NULL) {
2171                 m_freem(m);
2172                 return (ENETDOWN);
2173         }
2174
2175         /* Send mbuf */
2176         return (lagg_enqueue(lp->lp_ifp, m));
2177 }
2178
2179 static struct mbuf *
2180 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2181 {
2182         struct ifnet *ifp = sc->sc_ifp;
2183
2184         /* Just pass in the packet to our lagg device */
2185         m->m_pkthdr.rcvif = ifp;
2186
2187         return (m);
2188 }
2189
2190 /*
2191  * 802.3ad LACP
2192  */
2193 static void
2194 lagg_lacp_attach(struct lagg_softc *sc)
2195 {
2196         struct lagg_port *lp;
2197
2198         lacp_attach(sc);
2199         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2200                 lacp_port_create(lp);
2201 }
2202
2203 static void
2204 lagg_lacp_detach(struct lagg_softc *sc)
2205 {
2206         struct lagg_port *lp;
2207         void *psc;
2208
2209         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2210                 lacp_port_destroy(lp);
2211
2212         psc = sc->sc_psc;
2213         sc->sc_psc = NULL;
2214         LAGG_WUNLOCK(sc);
2215
2216         lacp_detach(psc);
2217 }
2218
2219 static void
2220 lagg_lacp_lladdr(struct lagg_softc *sc)
2221 {
2222         struct lagg_port *lp;
2223
2224         /* purge all the lacp ports */
2225         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2226                 lacp_port_destroy(lp);
2227
2228         /* add them back in */
2229         SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2230                 lacp_port_create(lp);
2231 }
2232
2233 static int
2234 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2235 {
2236         struct lagg_port *lp;
2237
2238         lp = lacp_select_tx_port(sc, m);
2239         if (lp == NULL) {
2240                 m_freem(m);
2241                 return (ENETDOWN);
2242         }
2243
2244         /* Send mbuf */
2245         return (lagg_enqueue(lp->lp_ifp, m));
2246 }
2247
2248 static struct mbuf *
2249 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2250 {
2251         struct ifnet *ifp = sc->sc_ifp;
2252         struct ether_header *eh;
2253         u_short etype;
2254
2255         eh = mtod(m, struct ether_header *);
2256         etype = ntohs(eh->ether_type);
2257
2258         /* Tap off LACP control messages */
2259         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2260                 m = lacp_input(lp, m);
2261                 if (m == NULL)
2262                         return (NULL);
2263         }
2264
2265         /*
2266          * If the port is not collecting or not in the active aggregator then
2267          * free and return.
2268          */
2269         if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
2270                 m_freem(m);
2271                 return (NULL);
2272         }
2273
2274         m->m_pkthdr.rcvif = ifp;
2275         return (m);
2276 }
2277