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