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