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