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