zfs: merge openzfs/zfs@90ba19eb7
[freebsd.git] / sys / net / if_lagg.c
1 /*      $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $      */
2
3 /*
4  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6  * Copyright (c) 2014, 2016 Marcelo Araujo <araujo@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 #include <sys/cdefs.h>
22 #include "opt_inet.h"
23 #include "opt_inet6.h"
24 #include "opt_kern_tls.h"
25 #include "opt_ratelimit.h"
26
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
30 #include <sys/mbuf.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
36 #include <sys/priv.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/lock.h>
40 #include <sys/rmlock.h>
41 #include <sys/sx.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/if_private.h>
54 #include <net/bpf.h>
55 #include <net/route.h>
56 #include <net/vnet.h>
57 #include <net/infiniband.h>
58
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
61 #include <netinet/ip.h>
62 #endif
63 #ifdef INET
64 #include <netinet/in_systm.h>
65 #include <netinet/if_ether.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/if_vlan_var.h>
75 #include <net/if_lagg.h>
76 #include <net/ieee8023ad_lacp.h>
77
78 #ifdef DEV_NETMAP
79 MODULE_DEPEND(if_lagg, netmap, 1, 1, 1);
80 #endif
81
82 #define LAGG_SX_INIT(_sc)       sx_init(&(_sc)->sc_sx, "if_lagg sx")
83 #define LAGG_SX_DESTROY(_sc)    sx_destroy(&(_sc)->sc_sx)
84 #define LAGG_XLOCK(_sc)         sx_xlock(&(_sc)->sc_sx)
85 #define LAGG_XUNLOCK(_sc)       sx_xunlock(&(_sc)->sc_sx)
86 #define LAGG_XLOCK_ASSERT(_sc)  sx_assert(&(_sc)->sc_sx, SA_XLOCKED)
87 #define LAGG_SLOCK(_sc)         sx_slock(&(_sc)->sc_sx)
88 #define LAGG_SUNLOCK(_sc)       sx_sunlock(&(_sc)->sc_sx)
89 #define LAGG_SXLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SA_LOCKED)
90
91 /* Special flags we should propagate to the lagg ports. */
92 static struct {
93         int flag;
94         int (*func)(struct ifnet *, int);
95 } lagg_pflags[] = {
96         {IFF_PROMISC, ifpromisc},
97         {IFF_ALLMULTI, if_allmulti},
98         {0, NULL}
99 };
100
101 struct lagg_snd_tag {
102         struct m_snd_tag com;
103         struct m_snd_tag *tag;
104 };
105
106 VNET_DEFINE_STATIC(SLIST_HEAD(__trhead, lagg_softc), lagg_list); /* list of laggs */
107 #define V_lagg_list     VNET(lagg_list)
108 VNET_DEFINE_STATIC(struct mtx, lagg_list_mtx);
109 #define V_lagg_list_mtx VNET(lagg_list_mtx)
110 #define LAGG_LIST_LOCK_INIT(x)          mtx_init(&V_lagg_list_mtx, \
111                                         "if_lagg list", NULL, MTX_DEF)
112 #define LAGG_LIST_LOCK_DESTROY(x)       mtx_destroy(&V_lagg_list_mtx)
113 #define LAGG_LIST_LOCK(x)               mtx_lock(&V_lagg_list_mtx)
114 #define LAGG_LIST_UNLOCK(x)             mtx_unlock(&V_lagg_list_mtx)
115 static eventhandler_tag lagg_detach_cookie = NULL;
116
117 static int      lagg_clone_create(struct if_clone *, char *, size_t,
118                     struct ifc_data *, struct ifnet **);
119 static int      lagg_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
120 VNET_DEFINE_STATIC(struct if_clone *, lagg_cloner);
121 #define V_lagg_cloner   VNET(lagg_cloner)
122 static const char laggname[] = "lagg";
123 static MALLOC_DEFINE(M_LAGG, laggname, "802.3AD Link Aggregation Interface");
124
125 static void     lagg_capabilities(struct lagg_softc *);
126 static int      lagg_port_create(struct lagg_softc *, struct ifnet *);
127 static int      lagg_port_destroy(struct lagg_port *, int);
128 static struct mbuf *lagg_input_ethernet(struct ifnet *, struct mbuf *);
129 static struct mbuf *lagg_input_infiniband(struct ifnet *, struct mbuf *);
130 static void     lagg_linkstate(struct lagg_softc *);
131 static void     lagg_port_state(struct ifnet *, int);
132 static int      lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
133 static int      lagg_port_output(struct ifnet *, struct mbuf *,
134                     const struct sockaddr *, struct route *);
135 static void     lagg_port_ifdetach(void *arg __unused, struct ifnet *);
136 #ifdef LAGG_PORT_STACKING
137 static int      lagg_port_checkstacking(struct lagg_softc *);
138 #endif
139 static void     lagg_port2req(struct lagg_port *, struct lagg_reqport *);
140 static void     lagg_init(void *);
141 static void     lagg_stop(struct lagg_softc *);
142 static int      lagg_ioctl(struct ifnet *, u_long, caddr_t);
143 #if defined(KERN_TLS) || defined(RATELIMIT)
144 static int      lagg_snd_tag_alloc(struct ifnet *,
145                     union if_snd_tag_alloc_params *,
146                     struct m_snd_tag **);
147 static int      lagg_snd_tag_modify(struct m_snd_tag *,
148                     union if_snd_tag_modify_params *);
149 static int      lagg_snd_tag_query(struct m_snd_tag *,
150                     union if_snd_tag_query_params *);
151 static void     lagg_snd_tag_free(struct m_snd_tag *);
152 static struct m_snd_tag *lagg_next_snd_tag(struct m_snd_tag *);
153 static void     lagg_ratelimit_query(struct ifnet *,
154                     struct if_ratelimit_query_results *);
155 #endif
156 static int      lagg_setmulti(struct lagg_port *);
157 static int      lagg_clrmulti(struct lagg_port *);
158 static void     lagg_setcaps(struct lagg_port *, int cap, int cap2);
159 static int      lagg_setflag(struct lagg_port *, int, int,
160                     int (*func)(struct ifnet *, int));
161 static int      lagg_setflags(struct lagg_port *, int status);
162 static uint64_t lagg_get_counter(struct ifnet *ifp, ift_counter cnt);
163 static int      lagg_transmit_ethernet(struct ifnet *, struct mbuf *);
164 static int      lagg_transmit_infiniband(struct ifnet *, struct mbuf *);
165 static void     lagg_qflush(struct ifnet *);
166 static int      lagg_media_change(struct ifnet *);
167 static void     lagg_media_status(struct ifnet *, struct ifmediareq *);
168 static struct lagg_port *lagg_link_active(struct lagg_softc *,
169                     struct lagg_port *);
170
171 /* Simple round robin */
172 static void     lagg_rr_attach(struct lagg_softc *);
173 static int      lagg_rr_start(struct lagg_softc *, struct mbuf *);
174
175 /* Active failover */
176 static int      lagg_fail_start(struct lagg_softc *, struct mbuf *);
177 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
178                     struct mbuf *);
179
180 /* Loadbalancing */
181 static void     lagg_lb_attach(struct lagg_softc *);
182 static void     lagg_lb_detach(struct lagg_softc *);
183 static int      lagg_lb_port_create(struct lagg_port *);
184 static void     lagg_lb_port_destroy(struct lagg_port *);
185 static int      lagg_lb_start(struct lagg_softc *, struct mbuf *);
186 static int      lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
187
188 /* Broadcast */
189 static int      lagg_bcast_start(struct lagg_softc *, struct mbuf *);
190
191 /* 802.3ad LACP */
192 static void     lagg_lacp_attach(struct lagg_softc *);
193 static void     lagg_lacp_detach(struct lagg_softc *);
194 static int      lagg_lacp_start(struct lagg_softc *, struct mbuf *);
195 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
196                     struct mbuf *);
197 static void     lagg_lacp_lladdr(struct lagg_softc *);
198
199 /* Default input */
200 static struct mbuf *lagg_default_input(struct lagg_softc *, struct lagg_port *,
201                     struct mbuf *);
202
203 /* lagg protocol table */
204 static const struct lagg_proto {
205         lagg_proto      pr_num;
206         void            (*pr_attach)(struct lagg_softc *);
207         void            (*pr_detach)(struct lagg_softc *);
208         int             (*pr_start)(struct lagg_softc *, struct mbuf *);
209         struct mbuf *   (*pr_input)(struct lagg_softc *, struct lagg_port *,
210                             struct mbuf *);
211         int             (*pr_addport)(struct lagg_port *);
212         void            (*pr_delport)(struct lagg_port *);
213         void            (*pr_linkstate)(struct lagg_port *);
214         void            (*pr_init)(struct lagg_softc *);
215         void            (*pr_stop)(struct lagg_softc *);
216         void            (*pr_lladdr)(struct lagg_softc *);
217         void            (*pr_request)(struct lagg_softc *, void *);
218         void            (*pr_portreq)(struct lagg_port *, void *);
219 } lagg_protos[] = {
220     {
221         .pr_num = LAGG_PROTO_NONE
222     },
223     {
224         .pr_num = LAGG_PROTO_ROUNDROBIN,
225         .pr_attach = lagg_rr_attach,
226         .pr_start = lagg_rr_start,
227         .pr_input = lagg_default_input,
228     },
229     {
230         .pr_num = LAGG_PROTO_FAILOVER,
231         .pr_start = lagg_fail_start,
232         .pr_input = lagg_fail_input,
233     },
234     {
235         .pr_num = LAGG_PROTO_LOADBALANCE,
236         .pr_attach = lagg_lb_attach,
237         .pr_detach = lagg_lb_detach,
238         .pr_start = lagg_lb_start,
239         .pr_input = lagg_default_input,
240         .pr_addport = lagg_lb_port_create,
241         .pr_delport = lagg_lb_port_destroy,
242     },
243     {
244         .pr_num = LAGG_PROTO_LACP,
245         .pr_attach = lagg_lacp_attach,
246         .pr_detach = lagg_lacp_detach,
247         .pr_start = lagg_lacp_start,
248         .pr_input = lagg_lacp_input,
249         .pr_addport = lacp_port_create,
250         .pr_delport = lacp_port_destroy,
251         .pr_linkstate = lacp_linkstate,
252         .pr_init = lacp_init,
253         .pr_stop = lacp_stop,
254         .pr_lladdr = lagg_lacp_lladdr,
255         .pr_request = lacp_req,
256         .pr_portreq = lacp_portreq,
257     },
258     {
259         .pr_num = LAGG_PROTO_BROADCAST,
260         .pr_start = lagg_bcast_start,
261         .pr_input = lagg_default_input,
262     },
263 };
264
265 SYSCTL_DECL(_net_link);
266 SYSCTL_NODE(_net_link, OID_AUTO, lagg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
267     "Link Aggregation");
268
269 /* Allow input on any failover links */
270 VNET_DEFINE_STATIC(int, lagg_failover_rx_all);
271 #define V_lagg_failover_rx_all  VNET(lagg_failover_rx_all)
272 SYSCTL_INT(_net_link_lagg, OID_AUTO, failover_rx_all, CTLFLAG_RW | CTLFLAG_VNET,
273     &VNET_NAME(lagg_failover_rx_all), 0,
274     "Accept input from any interface in a failover lagg");
275
276 /* Default value for using flowid */
277 VNET_DEFINE_STATIC(int, def_use_flowid) = 0;
278 #define V_def_use_flowid        VNET(def_use_flowid)
279 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_flowid,
280     CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(def_use_flowid), 0,
281     "Default setting for using flow id for load sharing");
282
283 /* Default value for using numa */
284 VNET_DEFINE_STATIC(int, def_use_numa) = 1;
285 #define V_def_use_numa  VNET(def_use_numa)
286 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_use_numa,
287     CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(def_use_numa), 0,
288     "Use numa to steer flows");
289
290 /* Default value for flowid shift */
291 VNET_DEFINE_STATIC(int, def_flowid_shift) = 16;
292 #define V_def_flowid_shift      VNET(def_flowid_shift)
293 SYSCTL_INT(_net_link_lagg, OID_AUTO, default_flowid_shift,
294     CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(def_flowid_shift), 0,
295     "Default setting for flowid shift for load sharing");
296
297 static void
298 vnet_lagg_init(const void *unused __unused)
299 {
300
301         LAGG_LIST_LOCK_INIT();
302         SLIST_INIT(&V_lagg_list);
303         struct if_clone_addreq req = {
304                 .create_f = lagg_clone_create,
305                 .destroy_f = lagg_clone_destroy,
306                 .flags = IFC_F_AUTOUNIT,
307         };
308         V_lagg_cloner = ifc_attach_cloner(laggname, &req);
309 }
310 VNET_SYSINIT(vnet_lagg_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
311     vnet_lagg_init, NULL);
312
313 static void
314 vnet_lagg_uninit(const void *unused __unused)
315 {
316
317         ifc_detach_cloner(V_lagg_cloner);
318         LAGG_LIST_LOCK_DESTROY();
319 }
320 VNET_SYSUNINIT(vnet_lagg_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
321     vnet_lagg_uninit, NULL);
322
323 static int
324 lagg_modevent(module_t mod, int type, void *data)
325 {
326
327         switch (type) {
328         case MOD_LOAD:
329                 lagg_input_ethernet_p = lagg_input_ethernet;
330                 lagg_input_infiniband_p = lagg_input_infiniband;
331                 lagg_linkstate_p = lagg_port_state;
332                 lagg_detach_cookie = EVENTHANDLER_REGISTER(
333                     ifnet_departure_event, lagg_port_ifdetach, NULL,
334                     EVENTHANDLER_PRI_ANY);
335                 break;
336         case MOD_UNLOAD:
337                 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
338                     lagg_detach_cookie);
339                 lagg_input_ethernet_p = NULL;
340                 lagg_input_infiniband_p = NULL;
341                 lagg_linkstate_p = NULL;
342                 break;
343         default:
344                 return (EOPNOTSUPP);
345         }
346         return (0);
347 }
348
349 static moduledata_t lagg_mod = {
350         "if_lagg",
351         lagg_modevent,
352         0
353 };
354
355 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
356 MODULE_VERSION(if_lagg, 1);
357 MODULE_DEPEND(if_lagg, if_infiniband, 1, 1, 1);
358
359 static void
360 lagg_proto_attach(struct lagg_softc *sc, lagg_proto pr)
361 {
362
363         LAGG_XLOCK_ASSERT(sc);
364         KASSERT(sc->sc_proto == LAGG_PROTO_NONE, ("%s: sc %p has proto",
365             __func__, sc));
366
367         if (sc->sc_ifflags & IFF_DEBUG)
368                 if_printf(sc->sc_ifp, "using proto %u\n", pr);
369
370         if (lagg_protos[pr].pr_attach != NULL)
371                 lagg_protos[pr].pr_attach(sc);
372         sc->sc_proto = pr;
373 }
374
375 static void
376 lagg_proto_detach(struct lagg_softc *sc)
377 {
378         lagg_proto pr;
379
380         LAGG_XLOCK_ASSERT(sc);
381         pr = sc->sc_proto;
382         sc->sc_proto = LAGG_PROTO_NONE;
383
384         if (lagg_protos[pr].pr_detach != NULL)
385                 lagg_protos[pr].pr_detach(sc);
386 }
387
388 static inline int
389 lagg_proto_start(struct lagg_softc *sc, struct mbuf *m)
390 {
391
392         return (lagg_protos[sc->sc_proto].pr_start(sc, m));
393 }
394
395 static inline struct mbuf *
396 lagg_proto_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
397 {
398
399         return (lagg_protos[sc->sc_proto].pr_input(sc, lp, m));
400 }
401
402 static int
403 lagg_proto_addport(struct lagg_softc *sc, struct lagg_port *lp)
404 {
405
406         if (lagg_protos[sc->sc_proto].pr_addport == NULL)
407                 return (0);
408         else
409                 return (lagg_protos[sc->sc_proto].pr_addport(lp));
410 }
411
412 static void
413 lagg_proto_delport(struct lagg_softc *sc, struct lagg_port *lp)
414 {
415
416         if (lagg_protos[sc->sc_proto].pr_delport != NULL)
417                 lagg_protos[sc->sc_proto].pr_delport(lp);
418 }
419
420 static void
421 lagg_proto_linkstate(struct lagg_softc *sc, struct lagg_port *lp)
422 {
423
424         if (lagg_protos[sc->sc_proto].pr_linkstate != NULL)
425                 lagg_protos[sc->sc_proto].pr_linkstate(lp);
426 }
427
428 static void
429 lagg_proto_init(struct lagg_softc *sc)
430 {
431
432         if (lagg_protos[sc->sc_proto].pr_init != NULL)
433                 lagg_protos[sc->sc_proto].pr_init(sc);
434 }
435
436 static void
437 lagg_proto_stop(struct lagg_softc *sc)
438 {
439
440         if (lagg_protos[sc->sc_proto].pr_stop != NULL)
441                 lagg_protos[sc->sc_proto].pr_stop(sc);
442 }
443
444 static void
445 lagg_proto_lladdr(struct lagg_softc *sc)
446 {
447
448         if (lagg_protos[sc->sc_proto].pr_lladdr != NULL)
449                 lagg_protos[sc->sc_proto].pr_lladdr(sc);
450 }
451
452 static void
453 lagg_proto_request(struct lagg_softc *sc, void *v)
454 {
455
456         if (lagg_protos[sc->sc_proto].pr_request != NULL)
457                 lagg_protos[sc->sc_proto].pr_request(sc, v);
458 }
459
460 static void
461 lagg_proto_portreq(struct lagg_softc *sc, struct lagg_port *lp, void *v)
462 {
463
464         if (lagg_protos[sc->sc_proto].pr_portreq != NULL)
465                 lagg_protos[sc->sc_proto].pr_portreq(lp, v);
466 }
467
468 /*
469  * This routine is run via an vlan
470  * config EVENT
471  */
472 static void
473 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
474 {
475         struct lagg_softc *sc = ifp->if_softc;
476         struct lagg_port *lp;
477
478         if (ifp->if_softc != arg) /* Not our event */
479                 return;
480
481         LAGG_XLOCK(sc);
482         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
483                 EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
484         LAGG_XUNLOCK(sc);
485 }
486
487 /*
488  * This routine is run via an vlan
489  * unconfig EVENT
490  */
491 static void
492 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
493 {
494         struct lagg_softc *sc = ifp->if_softc;
495         struct lagg_port *lp;
496
497         if (ifp->if_softc != arg) /* Not our event */
498                 return;
499
500         LAGG_XLOCK(sc);
501         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
502                 EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
503         LAGG_XUNLOCK(sc);
504 }
505
506 static int
507 lagg_clone_create(struct if_clone *ifc, char *name, size_t len,
508     struct ifc_data *ifd, struct ifnet **ifpp)
509 {
510         struct iflaggparam iflp;
511         struct lagg_softc *sc;
512         struct ifnet *ifp;
513         int if_type;
514         int error;
515         static const uint8_t eaddr[LAGG_ADDR_LEN];
516
517         if (ifd->params != NULL) {
518                 error = ifc_copyin(ifd, &iflp, sizeof(iflp));
519                 if (error)
520                         return (error);
521
522                 switch (iflp.lagg_type) {
523                 case LAGG_TYPE_ETHERNET:
524                         if_type = IFT_ETHER;
525                         break;
526                 case LAGG_TYPE_INFINIBAND:
527                         if_type = IFT_INFINIBAND;
528                         break;
529                 default:
530                         return (EINVAL);
531                 }
532         } else {
533                 if_type = IFT_ETHER;
534         }
535
536         sc = malloc(sizeof(*sc), M_LAGG, M_WAITOK | M_ZERO);
537         ifp = sc->sc_ifp = if_alloc(if_type);
538         if (ifp == NULL) {
539                 free(sc, M_LAGG);
540                 return (ENOSPC);
541         }
542         LAGG_SX_INIT(sc);
543
544         mtx_init(&sc->sc_mtx, "lagg-mtx", NULL, MTX_DEF);
545         callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
546
547         LAGG_XLOCK(sc);
548         if (V_def_use_flowid)
549                 sc->sc_opts |= LAGG_OPT_USE_FLOWID;
550         if (V_def_use_numa)
551                 sc->sc_opts |= LAGG_OPT_USE_NUMA;
552         sc->flowid_shift = V_def_flowid_shift;
553
554         /* Hash all layers by default */
555         sc->sc_flags = MBUF_HASHFLAG_L2 | MBUF_HASHFLAG_L3 | MBUF_HASHFLAG_L4;
556
557         lagg_proto_attach(sc, LAGG_PROTO_DEFAULT);
558
559         CK_SLIST_INIT(&sc->sc_ports);
560
561         switch (if_type) {
562         case IFT_ETHER:
563                 /* Initialise pseudo media types */
564                 ifmedia_init(&sc->sc_media, 0, lagg_media_change,
565                     lagg_media_status);
566                 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
567                 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
568
569                 if_initname(ifp, laggname, ifd->unit);
570                 ifp->if_transmit = lagg_transmit_ethernet;
571                 break;
572         case IFT_INFINIBAND:
573                 if_initname(ifp, laggname, ifd->unit);
574                 ifp->if_transmit = lagg_transmit_infiniband;
575                 break;
576         default:
577                 break;
578         }
579         ifp->if_softc = sc;
580         ifp->if_qflush = lagg_qflush;
581         ifp->if_init = lagg_init;
582         ifp->if_ioctl = lagg_ioctl;
583         ifp->if_get_counter = lagg_get_counter;
584         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
585 #if defined(KERN_TLS) || defined(RATELIMIT)
586         ifp->if_snd_tag_alloc = lagg_snd_tag_alloc;
587         ifp->if_ratelimit_query = lagg_ratelimit_query;
588 #endif
589         ifp->if_capenable = ifp->if_capabilities = IFCAP_HWSTATS;
590
591         /*
592          * Attach as an ordinary ethernet device, children will be attached
593          * as special device IFT_IEEE8023ADLAG or IFT_INFINIBANDLAG.
594          */
595         switch (if_type) {
596         case IFT_ETHER:
597                 ether_ifattach(ifp, eaddr);
598                 break;
599         case IFT_INFINIBAND:
600                 infiniband_ifattach(ifp, eaddr, sc->sc_bcast_addr);
601                 break;
602         default:
603                 break;
604         }
605
606         sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
607                 lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
608         sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
609                 lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
610
611         /* Insert into the global list of laggs */
612         LAGG_LIST_LOCK();
613         SLIST_INSERT_HEAD(&V_lagg_list, sc, sc_entries);
614         LAGG_LIST_UNLOCK();
615         LAGG_XUNLOCK(sc);
616         *ifpp = ifp;
617
618         return (0);
619 }
620
621 static int
622 lagg_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
623 {
624         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
625         struct lagg_port *lp;
626
627         LAGG_XLOCK(sc);
628         sc->sc_destroying = 1;
629         lagg_stop(sc);
630         ifp->if_flags &= ~IFF_UP;
631
632         EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
633         EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
634
635         /* Shutdown and remove lagg ports */
636         while ((lp = CK_SLIST_FIRST(&sc->sc_ports)) != NULL)
637                 lagg_port_destroy(lp, 1);
638
639         /* Unhook the aggregation protocol */
640         lagg_proto_detach(sc);
641         LAGG_XUNLOCK(sc);
642
643         switch (ifp->if_type) {
644         case IFT_ETHER:
645                 ifmedia_removeall(&sc->sc_media);
646                 ether_ifdetach(ifp);
647                 break;
648         case IFT_INFINIBAND:
649                 infiniband_ifdetach(ifp);
650                 break;
651         default:
652                 break;
653         }
654         if_free(ifp);
655
656         LAGG_LIST_LOCK();
657         SLIST_REMOVE(&V_lagg_list, sc, lagg_softc, sc_entries);
658         LAGG_LIST_UNLOCK();
659
660         mtx_destroy(&sc->sc_mtx);
661         LAGG_SX_DESTROY(sc);
662         free(sc, M_LAGG);
663
664         return (0);
665 }
666
667 static void
668 lagg_capabilities(struct lagg_softc *sc)
669 {
670         struct lagg_port *lp;
671         int cap, cap2, ena, ena2, pena, pena2;
672         uint64_t hwa;
673         struct ifnet_hw_tsomax hw_tsomax;
674
675         LAGG_XLOCK_ASSERT(sc);
676
677         /* Get common enabled capabilities for the lagg ports */
678         ena = ena2 = ~0;
679         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
680                 ena &= lp->lp_ifp->if_capenable;
681                 ena2 &= lp->lp_ifp->if_capenable2;
682         }
683         if (CK_SLIST_FIRST(&sc->sc_ports) == NULL)
684                 ena = ena2 = 0;
685
686         /*
687          * Apply common enabled capabilities back to the lagg ports.
688          * May require several iterations if they are dependent.
689          */
690         do {
691                 pena = ena;
692                 pena2 = ena2;
693                 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
694                         lagg_setcaps(lp, ena, ena2);
695                         ena &= lp->lp_ifp->if_capenable;
696                         ena2 &= lp->lp_ifp->if_capenable2;
697                 }
698         } while (pena != ena || pena2 != ena2);
699
700         /* Get other capabilities from the lagg ports */
701         cap = cap2 = ~0;
702         hwa = ~(uint64_t)0;
703         memset(&hw_tsomax, 0, sizeof(hw_tsomax));
704         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
705                 cap &= lp->lp_ifp->if_capabilities;
706                 cap2 &= lp->lp_ifp->if_capabilities2;
707                 hwa &= lp->lp_ifp->if_hwassist;
708                 if_hw_tsomax_common(lp->lp_ifp, &hw_tsomax);
709         }
710         if (CK_SLIST_FIRST(&sc->sc_ports) == NULL)
711                 cap = cap2 = hwa = 0;
712
713         if (sc->sc_ifp->if_capabilities != cap ||
714             sc->sc_ifp->if_capenable != ena ||
715             sc->sc_ifp->if_capenable2 != ena2 ||
716             sc->sc_ifp->if_hwassist != hwa ||
717             if_hw_tsomax_update(sc->sc_ifp, &hw_tsomax) != 0) {
718                 sc->sc_ifp->if_capabilities = cap;
719                 sc->sc_ifp->if_capabilities2 = cap2;
720                 sc->sc_ifp->if_capenable = ena;
721                 sc->sc_ifp->if_capenable2 = ena2;
722                 sc->sc_ifp->if_hwassist = hwa;
723                 getmicrotime(&sc->sc_ifp->if_lastchange);
724
725                 if (sc->sc_ifflags & IFF_DEBUG)
726                         if_printf(sc->sc_ifp,
727                             "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
728         }
729 }
730
731 static int
732 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
733 {
734         struct lagg_softc *sc_ptr;
735         struct lagg_port *lp, *tlp;
736         struct ifreq ifr;
737         int error, i, oldmtu;
738         int if_type;
739         uint64_t *pval;
740
741         LAGG_XLOCK_ASSERT(sc);
742
743         if (sc->sc_ifp == ifp) {
744                 if_printf(sc->sc_ifp,
745                     "cannot add a lagg to itself as a port\n");
746                 return (EINVAL);
747         }
748
749         if (sc->sc_destroying == 1)
750                 return (ENXIO);
751
752         /* Limit the maximal number of lagg ports */
753         if (sc->sc_count >= LAGG_MAX_PORTS)
754                 return (ENOSPC);
755
756         /* Check if port has already been associated to a lagg */
757         if (ifp->if_lagg != NULL) {
758                 /* Port is already in the current lagg? */
759                 lp = (struct lagg_port *)ifp->if_lagg;
760                 if (lp->lp_softc == sc)
761                         return (EEXIST);
762                 return (EBUSY);
763         }
764
765         switch (sc->sc_ifp->if_type) {
766         case IFT_ETHER:
767                 /* XXX Disallow non-ethernet interfaces (this should be any of 802) */
768                 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN)
769                         return (EPROTONOSUPPORT);
770                 if_type = IFT_IEEE8023ADLAG;
771                 break;
772         case IFT_INFINIBAND:
773                 /* XXX Disallow non-infiniband interfaces */
774                 if (ifp->if_type != IFT_INFINIBAND)
775                         return (EPROTONOSUPPORT);
776                 if_type = IFT_INFINIBANDLAG;
777                 break;
778         default:
779                 break;
780         }
781
782         /* Allow the first Ethernet member to define the MTU */
783         oldmtu = -1;
784         if (CK_SLIST_EMPTY(&sc->sc_ports)) {
785                 sc->sc_ifp->if_mtu = ifp->if_mtu;
786         } else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
787                 if (ifp->if_ioctl == NULL) {
788                         if_printf(sc->sc_ifp, "cannot change MTU for %s\n",
789                             ifp->if_xname);
790                         return (EINVAL);
791                 }
792                 oldmtu = ifp->if_mtu;
793                 strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
794                 ifr.ifr_mtu = sc->sc_ifp->if_mtu;
795                 error = (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
796                 if (error != 0) {
797                         if_printf(sc->sc_ifp, "invalid MTU for %s\n",
798                             ifp->if_xname);
799                         return (error);
800                 }
801                 ifr.ifr_mtu = oldmtu;
802         }
803
804         lp = malloc(sizeof(struct lagg_port), M_LAGG, M_WAITOK | M_ZERO);
805         lp->lp_softc = sc;
806
807         /* Check if port is a stacked lagg */
808         LAGG_LIST_LOCK();
809         SLIST_FOREACH(sc_ptr, &V_lagg_list, sc_entries) {
810                 if (ifp == sc_ptr->sc_ifp) {
811                         LAGG_LIST_UNLOCK();
812                         free(lp, M_LAGG);
813                         if (oldmtu != -1)
814                                 (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
815                                     (caddr_t)&ifr);
816                         return (EINVAL);
817                         /* XXX disable stacking for the moment, its untested */
818 #ifdef LAGG_PORT_STACKING
819                         lp->lp_flags |= LAGG_PORT_STACK;
820                         if (lagg_port_checkstacking(sc_ptr) >=
821                             LAGG_MAX_STACKING) {
822                                 LAGG_LIST_UNLOCK();
823                                 free(lp, M_LAGG);
824                                 if (oldmtu != -1)
825                                         (*ifp->if_ioctl)(ifp, SIOCSIFMTU,
826                                             (caddr_t)&ifr);
827                                 return (E2BIG);
828                         }
829 #endif
830                 }
831         }
832         LAGG_LIST_UNLOCK();
833
834         if_ref(ifp);
835         lp->lp_ifp = ifp;
836
837         bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ifp->if_addrlen);
838         lp->lp_ifcapenable = ifp->if_capenable;
839         if (CK_SLIST_EMPTY(&sc->sc_ports)) {
840                 bcopy(IF_LLADDR(ifp), IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
841                 lagg_proto_lladdr(sc);
842                 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
843         } else {
844                 if_setlladdr(ifp, IF_LLADDR(sc->sc_ifp), ifp->if_addrlen);
845         }
846         lagg_setflags(lp, 1);
847
848         if (CK_SLIST_EMPTY(&sc->sc_ports))
849                 sc->sc_primary = lp;
850
851         /* Change the interface type */
852         lp->lp_iftype = ifp->if_type;
853         ifp->if_type = if_type;
854         ifp->if_lagg = lp;
855         lp->lp_ioctl = ifp->if_ioctl;
856         ifp->if_ioctl = lagg_port_ioctl;
857         lp->lp_output = ifp->if_output;
858         ifp->if_output = lagg_port_output;
859
860         /* Read port counters */
861         pval = lp->port_counters.val;
862         for (i = 0; i < IFCOUNTERS; i++, pval++)
863                 *pval = ifp->if_get_counter(ifp, i);
864
865         /*
866          * Insert into the list of ports.
867          * Keep ports sorted by if_index. It is handy, when configuration
868          * is predictable and `ifconfig laggN create ...` command
869          * will lead to the same result each time.
870          */
871         CK_SLIST_FOREACH(tlp, &sc->sc_ports, lp_entries) {
872                 if (tlp->lp_ifp->if_index < ifp->if_index && (
873                     CK_SLIST_NEXT(tlp, lp_entries) == NULL ||
874                     ((struct lagg_port*)CK_SLIST_NEXT(tlp, lp_entries))->lp_ifp->if_index >
875                     ifp->if_index))
876                         break;
877         }
878         if (tlp != NULL)
879                 CK_SLIST_INSERT_AFTER(tlp, lp, lp_entries);
880         else
881                 CK_SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
882         sc->sc_count++;
883
884         lagg_setmulti(lp);
885
886         if ((error = lagg_proto_addport(sc, lp)) != 0) {
887                 /* Remove the port, without calling pr_delport. */
888                 lagg_port_destroy(lp, 0);
889                 if (oldmtu != -1)
890                         (*ifp->if_ioctl)(ifp, SIOCSIFMTU, (caddr_t)&ifr);
891                 return (error);
892         }
893
894         /* Update lagg capabilities */
895         lagg_capabilities(sc);
896         lagg_linkstate(sc);
897
898         return (0);
899 }
900
901 #ifdef LAGG_PORT_STACKING
902 static int
903 lagg_port_checkstacking(struct lagg_softc *sc)
904 {
905         struct lagg_softc *sc_ptr;
906         struct lagg_port *lp;
907         int m = 0;
908
909         LAGG_SXLOCK_ASSERT(sc);
910         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
911                 if (lp->lp_flags & LAGG_PORT_STACK) {
912                         sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
913                         m = MAX(m, lagg_port_checkstacking(sc_ptr));
914                 }
915         }
916
917         return (m + 1);
918 }
919 #endif
920
921 static void
922 lagg_port_destroy_cb(epoch_context_t ec)
923 {
924         struct lagg_port *lp;
925         struct ifnet *ifp;
926
927         lp = __containerof(ec, struct lagg_port, lp_epoch_ctx);
928         ifp = lp->lp_ifp;
929
930         if_rele(ifp);
931         free(lp, M_LAGG);
932 }
933
934 static int
935 lagg_port_destroy(struct lagg_port *lp, int rundelport)
936 {
937         struct lagg_softc *sc = lp->lp_softc;
938         struct lagg_port *lp_ptr, *lp0;
939         struct ifnet *ifp = lp->lp_ifp;
940         uint64_t *pval, vdiff;
941         int i;
942
943         LAGG_XLOCK_ASSERT(sc);
944
945         if (rundelport)
946                 lagg_proto_delport(sc, lp);
947
948         if (lp->lp_detaching == 0)
949                 lagg_clrmulti(lp);
950
951         /* Restore interface */
952         ifp->if_type = lp->lp_iftype;
953         ifp->if_ioctl = lp->lp_ioctl;
954         ifp->if_output = lp->lp_output;
955         ifp->if_lagg = NULL;
956
957         /* Update detached port counters */
958         pval = lp->port_counters.val;
959         for (i = 0; i < IFCOUNTERS; i++, pval++) {
960                 vdiff = ifp->if_get_counter(ifp, i) - *pval;
961                 sc->detached_counters.val[i] += vdiff;
962         }
963
964         /* Finally, remove the port from the lagg */
965         CK_SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
966         sc->sc_count--;
967
968         /* Update the primary interface */
969         if (lp == sc->sc_primary) {
970                 uint8_t lladdr[LAGG_ADDR_LEN];
971
972                 if ((lp0 = CK_SLIST_FIRST(&sc->sc_ports)) == NULL)
973                         bzero(&lladdr, LAGG_ADDR_LEN);
974                 else
975                         bcopy(lp0->lp_lladdr, lladdr, LAGG_ADDR_LEN);
976                 sc->sc_primary = lp0;
977                 if (sc->sc_destroying == 0) {
978                         bcopy(lladdr, IF_LLADDR(sc->sc_ifp), sc->sc_ifp->if_addrlen);
979                         lagg_proto_lladdr(sc);
980                         EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
981
982                         /*
983                          * Update lladdr for each port (new primary needs update
984                          * as well, to switch from old lladdr to its 'real' one).
985                          * We can skip this if the lagg is being destroyed.
986                          */
987                         CK_SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
988                                 if_setlladdr(lp_ptr->lp_ifp, lladdr,
989                                     lp_ptr->lp_ifp->if_addrlen);
990                 }
991         }
992
993         if (lp->lp_ifflags)
994                 if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
995
996         if (lp->lp_detaching == 0) {
997                 lagg_setflags(lp, 0);
998                 lagg_setcaps(lp, lp->lp_ifcapenable, lp->lp_ifcapenable2);
999                 if_setlladdr(ifp, lp->lp_lladdr, ifp->if_addrlen);
1000         }
1001
1002         /*
1003          * free port and release it's ifnet reference after a grace period has
1004          * elapsed.
1005          */
1006         NET_EPOCH_CALL(lagg_port_destroy_cb, &lp->lp_epoch_ctx);
1007         /* Update lagg capabilities */
1008         lagg_capabilities(sc);
1009         lagg_linkstate(sc);
1010
1011         return (0);
1012 }
1013
1014 static int
1015 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1016 {
1017         struct epoch_tracker et;
1018         struct lagg_reqport *rp = (struct lagg_reqport *)data;
1019         struct lagg_softc *sc;
1020         struct lagg_port *lp = NULL;
1021         int error = 0;
1022
1023         /* Should be checked by the caller */
1024         switch (ifp->if_type) {
1025         case IFT_IEEE8023ADLAG:
1026         case IFT_INFINIBANDLAG:
1027                 if ((lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
1028                         goto fallback;
1029                 break;
1030         default:
1031                 goto fallback;
1032         }
1033
1034         switch (cmd) {
1035         case SIOCGLAGGPORT:
1036                 if (rp->rp_portname[0] == '\0' ||
1037                     ifunit(rp->rp_portname) != ifp) {
1038                         error = EINVAL;
1039                         break;
1040                 }
1041
1042                 NET_EPOCH_ENTER(et);
1043                 if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
1044                         error = ENOENT;
1045                         NET_EPOCH_EXIT(et);
1046                         break;
1047                 }
1048
1049                 LAGG_SLOCK(sc);
1050                 lagg_port2req(lp, rp);
1051                 LAGG_SUNLOCK(sc);
1052                 NET_EPOCH_EXIT(et);
1053                 break;
1054
1055         case SIOCSIFCAP:
1056         case SIOCSIFCAPNV:
1057                 if (lp->lp_ioctl == NULL) {
1058                         error = EINVAL;
1059                         break;
1060                 }
1061                 error = (*lp->lp_ioctl)(ifp, cmd, data);
1062                 if (error)
1063                         break;
1064
1065                 /* Update lagg interface capabilities */
1066                 LAGG_XLOCK(sc);
1067                 lagg_capabilities(sc);
1068                 LAGG_XUNLOCK(sc);
1069                 VLAN_CAPABILITIES(sc->sc_ifp);
1070                 break;
1071
1072         case SIOCSIFMTU:
1073                 /* Do not allow the MTU to be changed once joined */
1074                 error = EINVAL;
1075                 break;
1076
1077         default:
1078                 goto fallback;
1079         }
1080
1081         return (error);
1082
1083 fallback:
1084         if (lp != NULL && lp->lp_ioctl != NULL)
1085                 return ((*lp->lp_ioctl)(ifp, cmd, data));
1086
1087         return (EINVAL);
1088 }
1089
1090 /*
1091  * Requests counter @cnt data.
1092  *
1093  * Counter value is calculated the following way:
1094  * 1) for each port, sum difference between current and "initial" measurements.
1095  * 2) add lagg logical interface counters.
1096  * 3) add data from detached_counters array.
1097  *
1098  * We also do the following things on ports attach/detach:
1099  * 1) On port attach we store all counters it has into port_counter array.
1100  * 2) On port detach we add the different between "initial" and
1101  *   current counters data to detached_counters array.
1102  */
1103 static uint64_t
1104 lagg_get_counter(struct ifnet *ifp, ift_counter cnt)
1105 {
1106         struct epoch_tracker et;
1107         struct lagg_softc *sc;
1108         struct lagg_port *lp;
1109         struct ifnet *lpifp;
1110         uint64_t newval, oldval, vsum;
1111
1112         /* Revise this when we've got non-generic counters. */
1113         KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1114
1115         sc = (struct lagg_softc *)ifp->if_softc;
1116
1117         vsum = 0;
1118         NET_EPOCH_ENTER(et);
1119         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1120                 /* Saved attached value */
1121                 oldval = lp->port_counters.val[cnt];
1122                 /* current value */
1123                 lpifp = lp->lp_ifp;
1124                 newval = lpifp->if_get_counter(lpifp, cnt);
1125                 /* Calculate diff and save new */
1126                 vsum += newval - oldval;
1127         }
1128         NET_EPOCH_EXIT(et);
1129
1130         /*
1131          * Add counter data which might be added by upper
1132          * layer protocols operating on logical interface.
1133          */
1134         vsum += if_get_counter_default(ifp, cnt);
1135
1136         /*
1137          * Add counter data from detached ports counters
1138          */
1139         vsum += sc->detached_counters.val[cnt];
1140
1141         return (vsum);
1142 }
1143
1144 /*
1145  * For direct output to child ports.
1146  */
1147 static int
1148 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
1149         const struct sockaddr *dst, struct route *ro)
1150 {
1151         struct lagg_port *lp = ifp->if_lagg;
1152
1153         switch (dst->sa_family) {
1154                 case pseudo_AF_HDRCMPLT:
1155                 case AF_UNSPEC:
1156                         if (lp != NULL)
1157                                 return ((*lp->lp_output)(ifp, m, dst, ro));
1158         }
1159
1160         /* drop any other frames */
1161         m_freem(m);
1162         return (ENETDOWN);
1163 }
1164
1165 static void
1166 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
1167 {
1168         struct lagg_port *lp;
1169         struct lagg_softc *sc;
1170
1171         if ((lp = ifp->if_lagg) == NULL)
1172                 return;
1173         /* If the ifnet is just being renamed, don't do anything. */
1174         if (ifp->if_flags & IFF_RENAMING)
1175                 return;
1176
1177         sc = lp->lp_softc;
1178
1179         LAGG_XLOCK(sc);
1180         lp->lp_detaching = 1;
1181         lagg_port_destroy(lp, 1);
1182         LAGG_XUNLOCK(sc);
1183         VLAN_CAPABILITIES(sc->sc_ifp);
1184 }
1185
1186 static void
1187 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
1188 {
1189         struct lagg_softc *sc = lp->lp_softc;
1190
1191         strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
1192         strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
1193         rp->rp_prio = lp->lp_prio;
1194         rp->rp_flags = lp->lp_flags;
1195         lagg_proto_portreq(sc, lp, &rp->rp_psc);
1196
1197         /* Add protocol specific flags */
1198         switch (sc->sc_proto) {
1199                 case LAGG_PROTO_FAILOVER:
1200                         if (lp == sc->sc_primary)
1201                                 rp->rp_flags |= LAGG_PORT_MASTER;
1202                         if (lp == lagg_link_active(sc, sc->sc_primary))
1203                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1204                         break;
1205
1206                 case LAGG_PROTO_ROUNDROBIN:
1207                 case LAGG_PROTO_LOADBALANCE:
1208                 case LAGG_PROTO_BROADCAST:
1209                         if (LAGG_PORTACTIVE(lp))
1210                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1211                         break;
1212
1213                 case LAGG_PROTO_LACP:
1214                         /* LACP has a different definition of active */
1215                         if (lacp_isactive(lp))
1216                                 rp->rp_flags |= LAGG_PORT_ACTIVE;
1217                         if (lacp_iscollecting(lp))
1218                                 rp->rp_flags |= LAGG_PORT_COLLECTING;
1219                         if (lacp_isdistributing(lp))
1220                                 rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
1221                         break;
1222         }
1223
1224 }
1225
1226 static void
1227 lagg_watchdog_infiniband(void *arg)
1228 {
1229         struct epoch_tracker et;
1230         struct lagg_softc *sc;
1231         struct lagg_port *lp;
1232         struct ifnet *ifp;
1233         struct ifnet *lp_ifp;
1234
1235         sc = arg;
1236
1237         /*
1238          * Because infiniband nodes have a fixed MAC address, which is
1239          * generated by the so-called GID, we need to regularly update
1240          * the link level address of the parent lagg<N> device when
1241          * the active port changes. Possibly we could piggy-back on
1242          * link up/down events aswell, but using a timer also provides
1243          * a guarantee against too frequent events. This operation
1244          * does not have to be atomic.
1245          */
1246         NET_EPOCH_ENTER(et);
1247         lp = lagg_link_active(sc, sc->sc_primary);
1248         if (lp != NULL) {
1249                 ifp = sc->sc_ifp;
1250                 lp_ifp = lp->lp_ifp;
1251
1252                 if (ifp != NULL && lp_ifp != NULL &&
1253                     (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen) != 0 ||
1254                      memcmp(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen) != 0)) {
1255                         memcpy(IF_LLADDR(ifp), IF_LLADDR(lp_ifp), ifp->if_addrlen);
1256                         memcpy(sc->sc_bcast_addr, lp_ifp->if_broadcastaddr, ifp->if_addrlen);
1257
1258                         CURVNET_SET(ifp->if_vnet);
1259                         EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1260                         CURVNET_RESTORE();
1261                 }
1262         }
1263         NET_EPOCH_EXIT(et);
1264
1265         callout_reset(&sc->sc_watchdog, hz, &lagg_watchdog_infiniband, arg);
1266 }
1267
1268 static void
1269 lagg_init(void *xsc)
1270 {
1271         struct lagg_softc *sc = (struct lagg_softc *)xsc;
1272         struct ifnet *ifp = sc->sc_ifp;
1273         struct lagg_port *lp;
1274
1275         LAGG_XLOCK(sc);
1276         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1277                 LAGG_XUNLOCK(sc);
1278                 return;
1279         }
1280
1281         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1282
1283         /*
1284          * Update the port lladdrs if needed.
1285          * This might be if_setlladdr() notification
1286          * that lladdr has been changed.
1287          */
1288         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1289                 if (memcmp(IF_LLADDR(ifp), IF_LLADDR(lp->lp_ifp),
1290                     ifp->if_addrlen) != 0)
1291                         if_setlladdr(lp->lp_ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1292         }
1293
1294         lagg_proto_init(sc);
1295
1296         if (ifp->if_type == IFT_INFINIBAND) {
1297                 mtx_lock(&sc->sc_mtx);
1298                 lagg_watchdog_infiniband(sc);
1299                 mtx_unlock(&sc->sc_mtx);
1300         }
1301
1302         LAGG_XUNLOCK(sc);
1303 }
1304
1305 static void
1306 lagg_stop(struct lagg_softc *sc)
1307 {
1308         struct ifnet *ifp = sc->sc_ifp;
1309
1310         LAGG_XLOCK_ASSERT(sc);
1311
1312         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1313                 return;
1314
1315         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1316
1317         lagg_proto_stop(sc);
1318
1319         mtx_lock(&sc->sc_mtx);
1320         callout_stop(&sc->sc_watchdog);
1321         mtx_unlock(&sc->sc_mtx);
1322
1323         callout_drain(&sc->sc_watchdog);
1324 }
1325
1326 static int
1327 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1328 {
1329         struct epoch_tracker et;
1330         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1331         struct lagg_reqall *ra = (struct lagg_reqall *)data;
1332         struct lagg_reqopts *ro = (struct lagg_reqopts *)data;
1333         struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
1334         struct lagg_reqflags *rf = (struct lagg_reqflags *)data;
1335         struct ifreq *ifr = (struct ifreq *)data;
1336         struct lagg_port *lp;
1337         struct ifnet *tpif;
1338         struct thread *td = curthread;
1339         char *buf, *outbuf;
1340         int count, buflen, len, error = 0, oldmtu;
1341
1342         bzero(&rpbuf, sizeof(rpbuf));
1343
1344         /* XXX: This can race with lagg_clone_destroy. */
1345
1346         switch (cmd) {
1347         case SIOCGLAGG:
1348                 LAGG_XLOCK(sc);
1349                 buflen = sc->sc_count * sizeof(struct lagg_reqport);
1350                 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
1351                 ra->ra_proto = sc->sc_proto;
1352                 lagg_proto_request(sc, &ra->ra_psc);
1353                 count = 0;
1354                 buf = outbuf;
1355                 len = min(ra->ra_size, buflen);
1356                 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1357                         if (len < sizeof(rpbuf))
1358                                 break;
1359
1360                         lagg_port2req(lp, &rpbuf);
1361                         memcpy(buf, &rpbuf, sizeof(rpbuf));
1362                         count++;
1363                         buf += sizeof(rpbuf);
1364                         len -= sizeof(rpbuf);
1365                 }
1366                 LAGG_XUNLOCK(sc);
1367                 ra->ra_ports = count;
1368                 ra->ra_size = count * sizeof(rpbuf);
1369                 error = copyout(outbuf, ra->ra_port, ra->ra_size);
1370                 free(outbuf, M_TEMP);
1371                 break;
1372         case SIOCSLAGG:
1373                 error = priv_check(td, PRIV_NET_LAGG);
1374                 if (error)
1375                         break;
1376                 if (ra->ra_proto >= LAGG_PROTO_MAX) {
1377                         error = EPROTONOSUPPORT;
1378                         break;
1379                 }
1380                 /* Infiniband only supports the failover protocol. */
1381                 if (ra->ra_proto != LAGG_PROTO_FAILOVER &&
1382                     ifp->if_type == IFT_INFINIBAND) {
1383                         error = EPROTONOSUPPORT;
1384                         break;
1385                 }
1386                 LAGG_XLOCK(sc);
1387                 lagg_proto_detach(sc);
1388                 lagg_proto_attach(sc, ra->ra_proto);
1389                 LAGG_XUNLOCK(sc);
1390                 break;
1391         case SIOCGLAGGOPTS:
1392                 LAGG_XLOCK(sc);
1393                 ro->ro_opts = sc->sc_opts;
1394                 if (sc->sc_proto == LAGG_PROTO_LACP) {
1395                         struct lacp_softc *lsc;
1396
1397                         lsc = (struct lacp_softc *)sc->sc_psc;
1398                         if (lsc->lsc_debug.lsc_tx_test != 0)
1399                                 ro->ro_opts |= LAGG_OPT_LACP_TXTEST;
1400                         if (lsc->lsc_debug.lsc_rx_test != 0)
1401                                 ro->ro_opts |= LAGG_OPT_LACP_RXTEST;
1402                         if (lsc->lsc_strict_mode != 0)
1403                                 ro->ro_opts |= LAGG_OPT_LACP_STRICT;
1404                         if (lsc->lsc_fast_timeout != 0)
1405                                 ro->ro_opts |= LAGG_OPT_LACP_FAST_TIMO;
1406
1407                         ro->ro_active = sc->sc_active;
1408                 } else {
1409                         ro->ro_active = 0;
1410                         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1411                                 ro->ro_active += LAGG_PORTACTIVE(lp);
1412                 }
1413                 ro->ro_bkt = sc->sc_stride;
1414                 ro->ro_flapping = sc->sc_flapping;
1415                 ro->ro_flowid_shift = sc->flowid_shift;
1416                 LAGG_XUNLOCK(sc);
1417                 break;
1418         case SIOCSLAGGOPTS:
1419                 error = priv_check(td, PRIV_NET_LAGG);
1420                 if (error)
1421                         break;
1422
1423                 /*
1424                  * The stride option was added without defining a corresponding
1425                  * LAGG_OPT flag, so handle a non-zero value before checking
1426                  * anything else to preserve compatibility.
1427                  */
1428                 LAGG_XLOCK(sc);
1429                 if (ro->ro_opts == 0 && ro->ro_bkt != 0) {
1430                         if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN) {
1431                                 LAGG_XUNLOCK(sc);
1432                                 error = EINVAL;
1433                                 break;
1434                         }
1435                         sc->sc_stride = ro->ro_bkt;
1436                 }
1437                 if (ro->ro_opts == 0) {
1438                         LAGG_XUNLOCK(sc);
1439                         break;
1440                 }
1441
1442                 /*
1443                  * Set options.  LACP options are stored in sc->sc_psc,
1444                  * not in sc_opts.
1445                  */
1446                 int valid, lacp;
1447
1448                 switch (ro->ro_opts) {
1449                 case LAGG_OPT_USE_FLOWID:
1450                 case -LAGG_OPT_USE_FLOWID:
1451                 case LAGG_OPT_USE_NUMA:
1452                 case -LAGG_OPT_USE_NUMA:
1453                 case LAGG_OPT_FLOWIDSHIFT:
1454                 case LAGG_OPT_RR_LIMIT:
1455                         valid = 1;
1456                         lacp = 0;
1457                         break;
1458                 case LAGG_OPT_LACP_TXTEST:
1459                 case -LAGG_OPT_LACP_TXTEST:
1460                 case LAGG_OPT_LACP_RXTEST:
1461                 case -LAGG_OPT_LACP_RXTEST:
1462                 case LAGG_OPT_LACP_STRICT:
1463                 case -LAGG_OPT_LACP_STRICT:
1464                 case LAGG_OPT_LACP_FAST_TIMO:
1465                 case -LAGG_OPT_LACP_FAST_TIMO:
1466                         valid = lacp = 1;
1467                         break;
1468                 default:
1469                         valid = lacp = 0;
1470                         break;
1471                 }
1472
1473                 if (valid == 0 ||
1474                     (lacp == 1 && sc->sc_proto != LAGG_PROTO_LACP)) {
1475                         /* Invalid combination of options specified. */
1476                         error = EINVAL;
1477                         LAGG_XUNLOCK(sc);
1478                         break;  /* Return from SIOCSLAGGOPTS. */
1479                 }
1480
1481                 /*
1482                  * Store new options into sc->sc_opts except for
1483                  * FLOWIDSHIFT, RR and LACP options.
1484                  */
1485                 if (lacp == 0) {
1486                         if (ro->ro_opts == LAGG_OPT_FLOWIDSHIFT)
1487                                 sc->flowid_shift = ro->ro_flowid_shift;
1488                         else if (ro->ro_opts == LAGG_OPT_RR_LIMIT) {
1489                                 if (sc->sc_proto != LAGG_PROTO_ROUNDROBIN ||
1490                                     ro->ro_bkt == 0) {
1491                                         error = EINVAL;
1492                                         LAGG_XUNLOCK(sc);
1493                                         break;
1494                                 }
1495                                 sc->sc_stride = ro->ro_bkt;
1496                         } else if (ro->ro_opts > 0)
1497                                 sc->sc_opts |= ro->ro_opts;
1498                         else
1499                                 sc->sc_opts &= ~ro->ro_opts;
1500                 } else {
1501                         struct lacp_softc *lsc;
1502                         struct lacp_port *lp;
1503
1504                         lsc = (struct lacp_softc *)sc->sc_psc;
1505
1506                         switch (ro->ro_opts) {
1507                         case LAGG_OPT_LACP_TXTEST:
1508                                 lsc->lsc_debug.lsc_tx_test = 1;
1509                                 break;
1510                         case -LAGG_OPT_LACP_TXTEST:
1511                                 lsc->lsc_debug.lsc_tx_test = 0;
1512                                 break;
1513                         case LAGG_OPT_LACP_RXTEST:
1514                                 lsc->lsc_debug.lsc_rx_test = 1;
1515                                 break;
1516                         case -LAGG_OPT_LACP_RXTEST:
1517                                 lsc->lsc_debug.lsc_rx_test = 0;
1518                                 break;
1519                         case LAGG_OPT_LACP_STRICT:
1520                                 lsc->lsc_strict_mode = 1;
1521                                 break;
1522                         case -LAGG_OPT_LACP_STRICT:
1523                                 lsc->lsc_strict_mode = 0;
1524                                 break;
1525                         case LAGG_OPT_LACP_FAST_TIMO:
1526                                 LACP_LOCK(lsc);
1527                                 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1528                                         lp->lp_state |= LACP_STATE_TIMEOUT;
1529                                 LACP_UNLOCK(lsc);
1530                                 lsc->lsc_fast_timeout = 1;
1531                                 break;
1532                         case -LAGG_OPT_LACP_FAST_TIMO:
1533                                 LACP_LOCK(lsc);
1534                                 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next)
1535                                         lp->lp_state &= ~LACP_STATE_TIMEOUT;
1536                                 LACP_UNLOCK(lsc);
1537                                 lsc->lsc_fast_timeout = 0;
1538                                 break;
1539                         }
1540                 }
1541                 LAGG_XUNLOCK(sc);
1542                 break;
1543         case SIOCGLAGGFLAGS:
1544                 rf->rf_flags = 0;
1545                 LAGG_XLOCK(sc);
1546                 if (sc->sc_flags & MBUF_HASHFLAG_L2)
1547                         rf->rf_flags |= LAGG_F_HASHL2;
1548                 if (sc->sc_flags & MBUF_HASHFLAG_L3)
1549                         rf->rf_flags |= LAGG_F_HASHL3;
1550                 if (sc->sc_flags & MBUF_HASHFLAG_L4)
1551                         rf->rf_flags |= LAGG_F_HASHL4;
1552                 LAGG_XUNLOCK(sc);
1553                 break;
1554         case SIOCSLAGGHASH:
1555                 error = priv_check(td, PRIV_NET_LAGG);
1556                 if (error)
1557                         break;
1558                 if ((rf->rf_flags & LAGG_F_HASHMASK) == 0) {
1559                         error = EINVAL;
1560                         break;
1561                 }
1562                 LAGG_XLOCK(sc);
1563                 sc->sc_flags = 0;
1564                 if (rf->rf_flags & LAGG_F_HASHL2)
1565                         sc->sc_flags |= MBUF_HASHFLAG_L2;
1566                 if (rf->rf_flags & LAGG_F_HASHL3)
1567                         sc->sc_flags |= MBUF_HASHFLAG_L3;
1568                 if (rf->rf_flags & LAGG_F_HASHL4)
1569                         sc->sc_flags |= MBUF_HASHFLAG_L4;
1570                 LAGG_XUNLOCK(sc);
1571                 break;
1572         case SIOCGLAGGPORT:
1573                 if (rp->rp_portname[0] == '\0' ||
1574                     (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1575                         error = EINVAL;
1576                         break;
1577                 }
1578
1579                 NET_EPOCH_ENTER(et);
1580                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1581                     lp->lp_softc != sc) {
1582                         error = ENOENT;
1583                         NET_EPOCH_EXIT(et);
1584                         if_rele(tpif);
1585                         break;
1586                 }
1587
1588                 LAGG_SLOCK(sc);
1589                 lagg_port2req(lp, rp);
1590                 LAGG_SUNLOCK(sc);
1591                 NET_EPOCH_EXIT(et);
1592                 if_rele(tpif);
1593                 break;
1594         case SIOCSLAGGPORT:
1595                 error = priv_check(td, PRIV_NET_LAGG);
1596                 if (error)
1597                         break;
1598                 if (rp->rp_portname[0] == '\0' ||
1599                     (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1600                         error = EINVAL;
1601                         break;
1602                 }
1603 #ifdef INET6
1604                 /*
1605                  * A laggport interface should not have inet6 address
1606                  * because two interfaces with a valid link-local
1607                  * scope zone must not be merged in any form.  This
1608                  * restriction is needed to prevent violation of
1609                  * link-local scope zone.  Attempts to add a laggport
1610                  * interface which has inet6 addresses triggers
1611                  * removal of all inet6 addresses on the member
1612                  * interface.
1613                  */
1614                 if (in6ifa_llaonifp(tpif)) {
1615                         in6_ifdetach(tpif);
1616                                 if_printf(sc->sc_ifp,
1617                                     "IPv6 addresses on %s have been removed "
1618                                     "before adding it as a member to prevent "
1619                                     "IPv6 address scope violation.\n",
1620                                     tpif->if_xname);
1621                 }
1622 #endif
1623                 oldmtu = ifp->if_mtu;
1624                 LAGG_XLOCK(sc);
1625                 error = lagg_port_create(sc, tpif);
1626                 LAGG_XUNLOCK(sc);
1627                 if_rele(tpif);
1628
1629                 /*
1630                  * LAGG MTU may change during addition of the first port.
1631                  * If it did, do network layer specific procedure.
1632                  */
1633                 if (ifp->if_mtu != oldmtu)
1634                         if_notifymtu(ifp);
1635
1636                 VLAN_CAPABILITIES(ifp);
1637                 break;
1638         case SIOCSLAGGDELPORT:
1639                 error = priv_check(td, PRIV_NET_LAGG);
1640                 if (error)
1641                         break;
1642                 if (rp->rp_portname[0] == '\0' ||
1643                     (tpif = ifunit_ref(rp->rp_portname)) == NULL) {
1644                         error = EINVAL;
1645                         break;
1646                 }
1647
1648                 LAGG_XLOCK(sc);
1649                 if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1650                     lp->lp_softc != sc) {
1651                         error = ENOENT;
1652                         LAGG_XUNLOCK(sc);
1653                         if_rele(tpif);
1654                         break;
1655                 }
1656
1657                 error = lagg_port_destroy(lp, 1);
1658                 LAGG_XUNLOCK(sc);
1659                 if_rele(tpif);
1660                 VLAN_CAPABILITIES(ifp);
1661                 break;
1662         case SIOCSIFFLAGS:
1663                 /* Set flags on ports too */
1664                 LAGG_XLOCK(sc);
1665                 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1666                         lagg_setflags(lp, 1);
1667                 }
1668
1669                 if (!(ifp->if_flags & IFF_UP) &&
1670                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1671                         /*
1672                          * If interface is marked down and it is running,
1673                          * then stop and disable it.
1674                          */
1675                         lagg_stop(sc);
1676                         LAGG_XUNLOCK(sc);
1677                 } else if ((ifp->if_flags & IFF_UP) &&
1678                     !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1679                         /*
1680                          * If interface is marked up and it is stopped, then
1681                          * start it.
1682                          */
1683                         LAGG_XUNLOCK(sc);
1684                         (*ifp->if_init)(sc);
1685                 } else
1686                         LAGG_XUNLOCK(sc);
1687                 break;
1688         case SIOCADDMULTI:
1689         case SIOCDELMULTI:
1690                 LAGG_XLOCK(sc);
1691                 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1692                         lagg_clrmulti(lp);
1693                         lagg_setmulti(lp);
1694                 }
1695                 LAGG_XUNLOCK(sc);
1696                 error = 0;
1697                 break;
1698         case SIOCSIFMEDIA:
1699         case SIOCGIFMEDIA:
1700                 if (ifp->if_type == IFT_INFINIBAND)
1701                         error = EINVAL;
1702                 else
1703                         error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1704                 break;
1705
1706         case SIOCSIFCAP:
1707         case SIOCSIFCAPNV:
1708                 LAGG_XLOCK(sc);
1709                 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1710                         if (lp->lp_ioctl != NULL)
1711                                 (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1712                 }
1713                 lagg_capabilities(sc);
1714                 LAGG_XUNLOCK(sc);
1715                 VLAN_CAPABILITIES(ifp);
1716                 error = 0;
1717                 break;
1718
1719         case SIOCGIFCAPNV:
1720                 error = 0;
1721                 break;
1722
1723         case SIOCSIFMTU:
1724                 LAGG_XLOCK(sc);
1725                 CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1726                         if (lp->lp_ioctl != NULL)
1727                                 error = (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1728                         else
1729                                 error = EINVAL;
1730                         if (error != 0) {
1731                                 if_printf(ifp,
1732                                     "failed to change MTU to %d on port %s, "
1733                                     "reverting all ports to original MTU (%d)\n",
1734                                     ifr->ifr_mtu, lp->lp_ifp->if_xname, ifp->if_mtu);
1735                                 break;
1736                         }
1737                 }
1738                 if (error == 0) {
1739                         ifp->if_mtu = ifr->ifr_mtu;
1740                 } else {
1741                         /* set every port back to the original MTU */
1742                         ifr->ifr_mtu = ifp->if_mtu;
1743                         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1744                                 if (lp->lp_ioctl != NULL)
1745                                         (*lp->lp_ioctl)(lp->lp_ifp, cmd, data);
1746                         }
1747                 }
1748                 lagg_capabilities(sc);
1749                 LAGG_XUNLOCK(sc);
1750                 VLAN_CAPABILITIES(ifp);
1751                 break;
1752
1753         default:
1754                 error = ether_ioctl(ifp, cmd, data);
1755                 break;
1756         }
1757         return (error);
1758 }
1759
1760 #if defined(KERN_TLS) || defined(RATELIMIT)
1761 #ifdef RATELIMIT
1762 static const struct if_snd_tag_sw lagg_snd_tag_ul_sw = {
1763         .snd_tag_modify = lagg_snd_tag_modify,
1764         .snd_tag_query = lagg_snd_tag_query,
1765         .snd_tag_free = lagg_snd_tag_free,
1766         .next_snd_tag = lagg_next_snd_tag,
1767         .type = IF_SND_TAG_TYPE_UNLIMITED
1768 };
1769
1770 static const struct if_snd_tag_sw lagg_snd_tag_rl_sw = {
1771         .snd_tag_modify = lagg_snd_tag_modify,
1772         .snd_tag_query = lagg_snd_tag_query,
1773         .snd_tag_free = lagg_snd_tag_free,
1774         .next_snd_tag = lagg_next_snd_tag,
1775         .type = IF_SND_TAG_TYPE_RATE_LIMIT
1776 };
1777 #endif
1778
1779 #ifdef KERN_TLS
1780 static const struct if_snd_tag_sw lagg_snd_tag_tls_sw = {
1781         .snd_tag_modify = lagg_snd_tag_modify,
1782         .snd_tag_query = lagg_snd_tag_query,
1783         .snd_tag_free = lagg_snd_tag_free,
1784         .next_snd_tag = lagg_next_snd_tag,
1785         .type = IF_SND_TAG_TYPE_TLS
1786 };
1787
1788 #ifdef RATELIMIT
1789 static const struct if_snd_tag_sw lagg_snd_tag_tls_rl_sw = {
1790         .snd_tag_modify = lagg_snd_tag_modify,
1791         .snd_tag_query = lagg_snd_tag_query,
1792         .snd_tag_free = lagg_snd_tag_free,
1793         .next_snd_tag = lagg_next_snd_tag,
1794         .type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
1795 };
1796 #endif
1797 #endif
1798
1799 static inline struct lagg_snd_tag *
1800 mst_to_lst(struct m_snd_tag *mst)
1801 {
1802
1803         return (__containerof(mst, struct lagg_snd_tag, com));
1804 }
1805
1806 /*
1807  * Look up the port used by a specific flow.  This only works for lagg
1808  * protocols with deterministic port mappings (e.g. not roundrobin).
1809  * In addition protocols which use a hash to map flows to ports must
1810  * be configured to use the mbuf flowid rather than hashing packet
1811  * contents.
1812  */
1813 static struct lagg_port *
1814 lookup_snd_tag_port(struct ifnet *ifp, uint32_t flowid, uint32_t flowtype,
1815     uint8_t numa_domain)
1816 {
1817         struct lagg_softc *sc;
1818         struct lagg_port *lp;
1819         struct lagg_lb *lb;
1820         uint32_t hash, p;
1821         int err;
1822
1823         sc = ifp->if_softc;
1824
1825         switch (sc->sc_proto) {
1826         case LAGG_PROTO_FAILOVER:
1827                 return (lagg_link_active(sc, sc->sc_primary));
1828         case LAGG_PROTO_LOADBALANCE:
1829                 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1830                     flowtype == M_HASHTYPE_NONE)
1831                         return (NULL);
1832                 p = flowid >> sc->flowid_shift;
1833                 p %= sc->sc_count;
1834                 lb = (struct lagg_lb *)sc->sc_psc;
1835                 lp = lb->lb_ports[p];
1836                 return (lagg_link_active(sc, lp));
1837         case LAGG_PROTO_LACP:
1838                 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) == 0 ||
1839                     flowtype == M_HASHTYPE_NONE)
1840                         return (NULL);
1841                 hash = flowid >> sc->flowid_shift;
1842                 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, &err));
1843         default:
1844                 return (NULL);
1845         }
1846 }
1847
1848 static int
1849 lagg_snd_tag_alloc(struct ifnet *ifp,
1850     union if_snd_tag_alloc_params *params,
1851     struct m_snd_tag **ppmt)
1852 {
1853         struct epoch_tracker et;
1854         const struct if_snd_tag_sw *sw;
1855         struct lagg_snd_tag *lst;
1856         struct lagg_port *lp;
1857         struct ifnet *lp_ifp;
1858         struct m_snd_tag *mst;
1859         int error;
1860
1861         switch (params->hdr.type) {
1862 #ifdef RATELIMIT
1863         case IF_SND_TAG_TYPE_UNLIMITED:
1864                 sw = &lagg_snd_tag_ul_sw;
1865                 break;
1866         case IF_SND_TAG_TYPE_RATE_LIMIT:
1867                 sw = &lagg_snd_tag_rl_sw;
1868                 break;
1869 #endif
1870 #ifdef KERN_TLS
1871         case IF_SND_TAG_TYPE_TLS:
1872                 sw = &lagg_snd_tag_tls_sw;
1873                 break;
1874         case IF_SND_TAG_TYPE_TLS_RX:
1875                 /* Return tag from port interface directly. */
1876                 sw = NULL;
1877                 break;
1878 #ifdef RATELIMIT
1879         case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
1880                 sw = &lagg_snd_tag_tls_rl_sw;
1881                 break;
1882 #endif
1883 #endif
1884         default:
1885                 return (EOPNOTSUPP);
1886         }
1887
1888         NET_EPOCH_ENTER(et);
1889         lp = lookup_snd_tag_port(ifp, params->hdr.flowid,
1890             params->hdr.flowtype, params->hdr.numa_domain);
1891         if (lp == NULL) {
1892                 NET_EPOCH_EXIT(et);
1893                 return (EOPNOTSUPP);
1894         }
1895         if (lp->lp_ifp == NULL) {
1896                 NET_EPOCH_EXIT(et);
1897                 return (EOPNOTSUPP);
1898         }
1899         lp_ifp = lp->lp_ifp;
1900         if_ref(lp_ifp);
1901         NET_EPOCH_EXIT(et);
1902
1903         if (sw != NULL) {
1904                 lst = malloc(sizeof(*lst), M_LAGG, M_NOWAIT);
1905                 if (lst == NULL) {
1906                         if_rele(lp_ifp);
1907                         return (ENOMEM);
1908                 }
1909         } else
1910                 lst = NULL;
1911
1912         error = m_snd_tag_alloc(lp_ifp, params, &mst);
1913         if_rele(lp_ifp);
1914         if (error) {
1915                 free(lst, M_LAGG);
1916                 return (error);
1917         }
1918
1919         if (sw != NULL) {
1920                 m_snd_tag_init(&lst->com, ifp, sw);
1921                 lst->tag = mst;
1922
1923                 *ppmt = &lst->com;
1924         } else
1925                 *ppmt = mst;
1926
1927         return (0);
1928 }
1929
1930 static struct m_snd_tag *
1931 lagg_next_snd_tag(struct m_snd_tag *mst)
1932 {
1933         struct lagg_snd_tag *lst;
1934
1935         lst = mst_to_lst(mst);
1936         return (lst->tag);
1937 }
1938
1939 static int
1940 lagg_snd_tag_modify(struct m_snd_tag *mst,
1941     union if_snd_tag_modify_params *params)
1942 {
1943         struct lagg_snd_tag *lst;
1944
1945         lst = mst_to_lst(mst);
1946         return (lst->tag->sw->snd_tag_modify(lst->tag, params));
1947 }
1948
1949 static int
1950 lagg_snd_tag_query(struct m_snd_tag *mst,
1951     union if_snd_tag_query_params *params)
1952 {
1953         struct lagg_snd_tag *lst;
1954
1955         lst = mst_to_lst(mst);
1956         return (lst->tag->sw->snd_tag_query(lst->tag, params));
1957 }
1958
1959 static void
1960 lagg_snd_tag_free(struct m_snd_tag *mst)
1961 {
1962         struct lagg_snd_tag *lst;
1963
1964         lst = mst_to_lst(mst);
1965         m_snd_tag_rele(lst->tag);
1966         free(lst, M_LAGG);
1967 }
1968
1969 static void
1970 lagg_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
1971 {
1972         /*
1973          * For lagg, we have an indirect
1974          * interface. The caller needs to
1975          * get a ratelimit tag on the actual
1976          * interface the flow will go on.
1977          */
1978         q->rate_table = NULL;
1979         q->flags = RT_IS_INDIRECT;
1980         q->max_flows = 0;
1981         q->number_of_rates = 0;
1982 }
1983 #endif
1984
1985 static int
1986 lagg_setmulti(struct lagg_port *lp)
1987 {
1988         struct lagg_softc *sc = lp->lp_softc;
1989         struct ifnet *ifp = lp->lp_ifp;
1990         struct ifnet *scifp = sc->sc_ifp;
1991         struct lagg_mc *mc;
1992         struct ifmultiaddr *ifma;
1993         int error;
1994
1995         IF_ADDR_WLOCK(scifp);
1996         CK_STAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1997                 if (ifma->ifma_addr->sa_family != AF_LINK)
1998                         continue;
1999                 mc = malloc(sizeof(struct lagg_mc), M_LAGG, M_NOWAIT);
2000                 if (mc == NULL) {
2001                         IF_ADDR_WUNLOCK(scifp);
2002                         return (ENOMEM);
2003                 }
2004                 bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
2005                 mc->mc_addr.sdl_index = ifp->if_index;
2006                 mc->mc_ifma = NULL;
2007                 SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
2008         }
2009         IF_ADDR_WUNLOCK(scifp);
2010         SLIST_FOREACH (mc, &lp->lp_mc_head, mc_entries) {
2011                 error = if_addmulti(ifp,
2012                     (struct sockaddr *)&mc->mc_addr, &mc->mc_ifma);
2013                 if (error)
2014                         return (error);
2015         }
2016         return (0);
2017 }
2018
2019 static int
2020 lagg_clrmulti(struct lagg_port *lp)
2021 {
2022         struct lagg_mc *mc;
2023
2024         LAGG_XLOCK_ASSERT(lp->lp_softc);
2025         while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
2026                 SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
2027                 if (mc->mc_ifma && lp->lp_detaching == 0)
2028                         if_delmulti_ifma(mc->mc_ifma);
2029                 free(mc, M_LAGG);
2030         }
2031         return (0);
2032 }
2033
2034 static void
2035 lagg_setcaps(struct lagg_port *lp, int cap, int cap2)
2036 {
2037         struct ifreq ifr;
2038         struct siocsifcapnv_driver_data drv_ioctl_data;
2039
2040         if (lp->lp_ifp->if_capenable == cap &&
2041             lp->lp_ifp->if_capenable2 == cap2)
2042                 return;
2043         if (lp->lp_ioctl == NULL)
2044                 return;
2045         /* XXX */
2046         if ((lp->lp_ifp->if_capabilities & IFCAP_NV) != 0) {
2047                 drv_ioctl_data.reqcap = cap;
2048                 drv_ioctl_data.reqcap2 = cap2;
2049                 drv_ioctl_data.nvcap = NULL;
2050                 (*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAPNV,
2051                     (caddr_t)&drv_ioctl_data);
2052         } else {
2053                 ifr.ifr_reqcap = cap;
2054                 (*lp->lp_ioctl)(lp->lp_ifp, SIOCSIFCAP, (caddr_t)&ifr);
2055         }
2056 }
2057
2058 /* Handle a ref counted flag that should be set on the lagg port as well */
2059 static int
2060 lagg_setflag(struct lagg_port *lp, int flag, int status,
2061     int (*func)(struct ifnet *, int))
2062 {
2063         struct lagg_softc *sc = lp->lp_softc;
2064         struct ifnet *scifp = sc->sc_ifp;
2065         struct ifnet *ifp = lp->lp_ifp;
2066         int error;
2067
2068         LAGG_XLOCK_ASSERT(sc);
2069
2070         status = status ? (scifp->if_flags & flag) : 0;
2071         /* Now "status" contains the flag value or 0 */
2072
2073         /*
2074          * See if recorded ports status is different from what
2075          * we want it to be.  If it is, flip it.  We record ports
2076          * status in lp_ifflags so that we won't clear ports flag
2077          * we haven't set.  In fact, we don't clear or set ports
2078          * flags directly, but get or release references to them.
2079          * That's why we can be sure that recorded flags still are
2080          * in accord with actual ports flags.
2081          */
2082         if (status != (lp->lp_ifflags & flag)) {
2083                 error = (*func)(ifp, status);
2084                 if (error)
2085                         return (error);
2086                 lp->lp_ifflags &= ~flag;
2087                 lp->lp_ifflags |= status;
2088         }
2089         return (0);
2090 }
2091
2092 /*
2093  * Handle IFF_* flags that require certain changes on the lagg port
2094  * if "status" is true, update ports flags respective to the lagg
2095  * if "status" is false, forcedly clear the flags set on port.
2096  */
2097 static int
2098 lagg_setflags(struct lagg_port *lp, int status)
2099 {
2100         int error, i;
2101
2102         for (i = 0; lagg_pflags[i].flag; i++) {
2103                 error = lagg_setflag(lp, lagg_pflags[i].flag,
2104                     status, lagg_pflags[i].func);
2105                 if (error)
2106                         return (error);
2107         }
2108         return (0);
2109 }
2110
2111 static int
2112 lagg_transmit_ethernet(struct ifnet *ifp, struct mbuf *m)
2113 {
2114         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2115
2116         NET_EPOCH_ASSERT();
2117 #if defined(KERN_TLS) || defined(RATELIMIT)
2118         if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2119                 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2120 #endif
2121         /* We need a Tx algorithm and at least one port */
2122         if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2123                 m_freem(m);
2124                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2125                 return (ENXIO);
2126         }
2127
2128         ETHER_BPF_MTAP(ifp, m);
2129
2130         return (lagg_proto_start(sc, m));
2131 }
2132
2133 static int
2134 lagg_transmit_infiniband(struct ifnet *ifp, struct mbuf *m)
2135 {
2136         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2137
2138         NET_EPOCH_ASSERT();
2139 #if defined(KERN_TLS) || defined(RATELIMIT)
2140         if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
2141                 MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
2142 #endif
2143         /* We need a Tx algorithm and at least one port */
2144         if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
2145                 m_freem(m);
2146                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2147                 return (ENXIO);
2148         }
2149
2150         infiniband_bpf_mtap(ifp, m);
2151
2152         return (lagg_proto_start(sc, m));
2153 }
2154
2155 /*
2156  * The ifp->if_qflush entry point for lagg(4) is no-op.
2157  */
2158 static void
2159 lagg_qflush(struct ifnet *ifp __unused)
2160 {
2161 }
2162
2163 static struct mbuf *
2164 lagg_input_ethernet(struct ifnet *ifp, struct mbuf *m)
2165 {
2166         struct lagg_port *lp = ifp->if_lagg;
2167         struct lagg_softc *sc = lp->lp_softc;
2168         struct ifnet *scifp = sc->sc_ifp;
2169
2170         NET_EPOCH_ASSERT();
2171         if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2172             lp->lp_detaching != 0 ||
2173             sc->sc_proto == LAGG_PROTO_NONE) {
2174                 m_freem(m);
2175                 return (NULL);
2176         }
2177
2178         m = lagg_proto_input(sc, lp, m);
2179         if (m != NULL) {
2180                 ETHER_BPF_MTAP(scifp, m);
2181
2182                 if ((scifp->if_flags & IFF_MONITOR) != 0) {
2183                         m_freem(m);
2184                         m = NULL;
2185                 }
2186         }
2187
2188 #ifdef DEV_NETMAP
2189         if (m != NULL && scifp->if_capenable & IFCAP_NETMAP) {
2190                 scifp->if_input(scifp, m);
2191                 m = NULL;
2192         }
2193 #endif  /* DEV_NETMAP */
2194
2195         return (m);
2196 }
2197
2198 static struct mbuf *
2199 lagg_input_infiniband(struct ifnet *ifp, struct mbuf *m)
2200 {
2201         struct lagg_port *lp = ifp->if_lagg;
2202         struct lagg_softc *sc = lp->lp_softc;
2203         struct ifnet *scifp = sc->sc_ifp;
2204
2205         NET_EPOCH_ASSERT();
2206         if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2207             lp->lp_detaching != 0 ||
2208             sc->sc_proto == LAGG_PROTO_NONE) {
2209                 m_freem(m);
2210                 return (NULL);
2211         }
2212
2213         m = lagg_proto_input(sc, lp, m);
2214         if (m != NULL) {
2215                 infiniband_bpf_mtap(scifp, m);
2216
2217                 if ((scifp->if_flags & IFF_MONITOR) != 0) {
2218                         m_freem(m);
2219                         m = NULL;
2220                 }
2221         }
2222
2223         return (m);
2224 }
2225
2226 static int
2227 lagg_media_change(struct ifnet *ifp)
2228 {
2229         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2230
2231         if (sc->sc_ifflags & IFF_DEBUG)
2232                 printf("%s\n", __func__);
2233
2234         /* Ignore */
2235         return (0);
2236 }
2237
2238 static void
2239 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
2240 {
2241         struct epoch_tracker et;
2242         struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
2243         struct lagg_port *lp;
2244
2245         imr->ifm_status = IFM_AVALID;
2246         imr->ifm_active = IFM_ETHER | IFM_AUTO;
2247
2248         NET_EPOCH_ENTER(et);
2249         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2250                 if (LAGG_PORTACTIVE(lp))
2251                         imr->ifm_status |= IFM_ACTIVE;
2252         }
2253         NET_EPOCH_EXIT(et);
2254 }
2255
2256 static void
2257 lagg_linkstate(struct lagg_softc *sc)
2258 {
2259         struct epoch_tracker et;
2260         struct lagg_port *lp;
2261         int new_link = LINK_STATE_DOWN;
2262         uint64_t speed;
2263
2264         LAGG_XLOCK_ASSERT(sc);
2265
2266         /* LACP handles link state itself */
2267         if (sc->sc_proto == LAGG_PROTO_LACP)
2268                 return;
2269
2270         /* Our link is considered up if at least one of our ports is active */
2271         NET_EPOCH_ENTER(et);
2272         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2273                 if (lp->lp_ifp->if_link_state == LINK_STATE_UP) {
2274                         new_link = LINK_STATE_UP;
2275                         break;
2276                 }
2277         }
2278         NET_EPOCH_EXIT(et);
2279         if_link_state_change(sc->sc_ifp, new_link);
2280
2281         /* Update if_baudrate to reflect the max possible speed */
2282         switch (sc->sc_proto) {
2283                 case LAGG_PROTO_FAILOVER:
2284                         sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
2285                             sc->sc_primary->lp_ifp->if_baudrate : 0;
2286                         break;
2287                 case LAGG_PROTO_ROUNDROBIN:
2288                 case LAGG_PROTO_LOADBALANCE:
2289                 case LAGG_PROTO_BROADCAST:
2290                         speed = 0;
2291                         NET_EPOCH_ENTER(et);
2292                         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2293                                 speed += lp->lp_ifp->if_baudrate;
2294                         NET_EPOCH_EXIT(et);
2295                         sc->sc_ifp->if_baudrate = speed;
2296                         break;
2297                 case LAGG_PROTO_LACP:
2298                         /* LACP updates if_baudrate itself */
2299                         break;
2300         }
2301 }
2302
2303 static void
2304 lagg_port_state(struct ifnet *ifp, int state)
2305 {
2306         struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
2307         struct lagg_softc *sc = NULL;
2308
2309         if (lp != NULL)
2310                 sc = lp->lp_softc;
2311         if (sc == NULL)
2312                 return;
2313
2314         LAGG_XLOCK(sc);
2315         lagg_linkstate(sc);
2316         lagg_proto_linkstate(sc, lp);
2317         LAGG_XUNLOCK(sc);
2318 }
2319
2320 struct lagg_port *
2321 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
2322 {
2323         struct lagg_port *lp_next, *rval = NULL;
2324
2325         /*
2326          * Search a port which reports an active link state.
2327          */
2328
2329 #ifdef INVARIANTS
2330         /*
2331          * This is called with either in the network epoch
2332          * or with LAGG_XLOCK(sc) held.
2333          */
2334         if (!in_epoch(net_epoch_preempt))
2335                 LAGG_XLOCK_ASSERT(sc);
2336 #endif
2337
2338         if (lp == NULL)
2339                 goto search;
2340         if (LAGG_PORTACTIVE(lp)) {
2341                 rval = lp;
2342                 goto found;
2343         }
2344         if ((lp_next = CK_SLIST_NEXT(lp, lp_entries)) != NULL &&
2345             LAGG_PORTACTIVE(lp_next)) {
2346                 rval = lp_next;
2347                 goto found;
2348         }
2349
2350 search:
2351         CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2352                 if (LAGG_PORTACTIVE(lp_next)) {
2353                         return (lp_next);
2354                 }
2355         }
2356 found:
2357         return (rval);
2358 }
2359
2360 int
2361 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
2362 {
2363
2364 #if defined(KERN_TLS) || defined(RATELIMIT)
2365         if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
2366                 struct lagg_snd_tag *lst;
2367                 struct m_snd_tag *mst;
2368
2369                 mst = m->m_pkthdr.snd_tag;
2370                 lst = mst_to_lst(mst);
2371                 if (lst->tag->ifp != ifp) {
2372                         m_freem(m);
2373                         return (EAGAIN);
2374                 }
2375                 m->m_pkthdr.snd_tag = m_snd_tag_ref(lst->tag);
2376                 m_snd_tag_rele(mst);
2377         }
2378 #endif
2379         return (ifp->if_transmit)(ifp, m);
2380 }
2381
2382 /*
2383  * Simple round robin aggregation
2384  */
2385 static void
2386 lagg_rr_attach(struct lagg_softc *sc)
2387 {
2388         sc->sc_seq = 0;
2389         sc->sc_stride = 1;
2390 }
2391
2392 static int
2393 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
2394 {
2395         struct lagg_port *lp;
2396         uint32_t p;
2397
2398         p = atomic_fetchadd_32(&sc->sc_seq, 1);
2399         p /= sc->sc_stride;
2400         p %= sc->sc_count;
2401         lp = CK_SLIST_FIRST(&sc->sc_ports);
2402
2403         while (p--)
2404                 lp = CK_SLIST_NEXT(lp, lp_entries);
2405
2406         /*
2407          * Check the port's link state. This will return the next active
2408          * port if the link is down or the port is NULL.
2409          */
2410         if ((lp = lagg_link_active(sc, lp)) == NULL) {
2411                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2412                 m_freem(m);
2413                 return (ENETDOWN);
2414         }
2415
2416         /* Send mbuf */
2417         return (lagg_enqueue(lp->lp_ifp, m));
2418 }
2419
2420 /*
2421  * Broadcast mode
2422  */
2423 static int
2424 lagg_bcast_start(struct lagg_softc *sc, struct mbuf *m)
2425 {
2426         int errors = 0;
2427         int ret;
2428         struct lagg_port *lp, *last = NULL;
2429         struct mbuf *m0;
2430
2431         NET_EPOCH_ASSERT();
2432         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
2433                 if (!LAGG_PORTACTIVE(lp))
2434                         continue;
2435
2436                 if (last != NULL) {
2437                         m0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2438                         if (m0 == NULL) {
2439                                 ret = ENOBUFS;
2440                                 errors++;
2441                                 break;
2442                         }
2443                         lagg_enqueue(last->lp_ifp, m0);
2444                 }
2445                 last = lp;
2446         }
2447
2448         if (last == NULL) {
2449                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2450                 m_freem(m);
2451                 return (ENOENT);
2452         }
2453         if ((last = lagg_link_active(sc, last)) == NULL) {
2454                 errors++;
2455                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2456                 m_freem(m);
2457                 return (ENETDOWN);
2458         }
2459
2460         ret = lagg_enqueue(last->lp_ifp, m);
2461         if (errors != 0)
2462                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, errors);
2463
2464         return (ret);
2465 }
2466
2467 /*
2468  * Active failover
2469  */
2470 static int
2471 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
2472 {
2473         struct lagg_port *lp;
2474
2475         /* Use the master port if active or the next available port */
2476         if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
2477                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2478                 m_freem(m);
2479                 return (ENETDOWN);
2480         }
2481
2482         /* Send mbuf */
2483         return (lagg_enqueue(lp->lp_ifp, m));
2484 }
2485
2486 static struct mbuf *
2487 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2488 {
2489         struct ifnet *ifp = sc->sc_ifp;
2490         struct lagg_port *tmp_tp;
2491
2492         if (lp == sc->sc_primary || V_lagg_failover_rx_all) {
2493                 m->m_pkthdr.rcvif = ifp;
2494                 return (m);
2495         }
2496
2497         if (!LAGG_PORTACTIVE(sc->sc_primary)) {
2498                 tmp_tp = lagg_link_active(sc, sc->sc_primary);
2499                 /*
2500                  * If tmp_tp is null, we've received a packet when all
2501                  * our links are down. Weird, but process it anyways.
2502                  */
2503                 if (tmp_tp == NULL || tmp_tp == lp) {
2504                         m->m_pkthdr.rcvif = ifp;
2505                         return (m);
2506                 }
2507         }
2508
2509         m_freem(m);
2510         return (NULL);
2511 }
2512
2513 /*
2514  * Loadbalancing
2515  */
2516 static void
2517 lagg_lb_attach(struct lagg_softc *sc)
2518 {
2519         struct lagg_port *lp;
2520         struct lagg_lb *lb;
2521
2522         LAGG_XLOCK_ASSERT(sc);
2523         lb = malloc(sizeof(struct lagg_lb), M_LAGG, M_WAITOK | M_ZERO);
2524         lb->lb_key = m_ether_tcpip_hash_init();
2525         sc->sc_psc = lb;
2526
2527         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2528                 lagg_lb_port_create(lp);
2529 }
2530
2531 static void
2532 lagg_lb_detach(struct lagg_softc *sc)
2533 {
2534         struct lagg_lb *lb;
2535
2536         lb = (struct lagg_lb *)sc->sc_psc;
2537         if (lb != NULL)
2538                 free(lb, M_LAGG);
2539 }
2540
2541 static int
2542 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
2543 {
2544         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2545         struct lagg_port *lp_next;
2546         int i = 0, rv;
2547
2548         rv = 0;
2549         bzero(&lb->lb_ports, sizeof(lb->lb_ports));
2550         LAGG_XLOCK_ASSERT(sc);
2551         CK_SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
2552                 if (lp_next == lp)
2553                         continue;
2554                 if (i >= LAGG_MAX_PORTS) {
2555                         rv = EINVAL;
2556                         break;
2557                 }
2558                 if (sc->sc_ifflags & IFF_DEBUG)
2559                         printf("%s: port %s at index %d\n",
2560                             sc->sc_ifname, lp_next->lp_ifp->if_xname, i);
2561                 lb->lb_ports[i++] = lp_next;
2562         }
2563
2564         return (rv);
2565 }
2566
2567 static int
2568 lagg_lb_port_create(struct lagg_port *lp)
2569 {
2570         struct lagg_softc *sc = lp->lp_softc;
2571         return (lagg_lb_porttable(sc, NULL));
2572 }
2573
2574 static void
2575 lagg_lb_port_destroy(struct lagg_port *lp)
2576 {
2577         struct lagg_softc *sc = lp->lp_softc;
2578         lagg_lb_porttable(sc, lp);
2579 }
2580
2581 static int
2582 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
2583 {
2584         struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
2585         struct lagg_port *lp = NULL;
2586         uint32_t p = 0;
2587
2588         if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
2589             M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2590                 p = m->m_pkthdr.flowid >> sc->flowid_shift;
2591         else
2592                 p = m_ether_tcpip_hash(sc->sc_flags, m, lb->lb_key);
2593         p %= sc->sc_count;
2594         lp = lb->lb_ports[p];
2595
2596         /*
2597          * Check the port's link state. This will return the next active
2598          * port if the link is down or the port is NULL.
2599          */
2600         if ((lp = lagg_link_active(sc, lp)) == NULL) {
2601                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2602                 m_freem(m);
2603                 return (ENETDOWN);
2604         }
2605
2606         /* Send mbuf */
2607         return (lagg_enqueue(lp->lp_ifp, m));
2608 }
2609
2610 /*
2611  * 802.3ad LACP
2612  */
2613 static void
2614 lagg_lacp_attach(struct lagg_softc *sc)
2615 {
2616         struct lagg_port *lp;
2617
2618         lacp_attach(sc);
2619         LAGG_XLOCK_ASSERT(sc);
2620         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2621                 lacp_port_create(lp);
2622 }
2623
2624 static void
2625 lagg_lacp_detach(struct lagg_softc *sc)
2626 {
2627         struct lagg_port *lp;
2628         void *psc;
2629
2630         LAGG_XLOCK_ASSERT(sc);
2631         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2632                 lacp_port_destroy(lp);
2633
2634         psc = sc->sc_psc;
2635         sc->sc_psc = NULL;
2636         lacp_detach(psc);
2637 }
2638
2639 static void
2640 lagg_lacp_lladdr(struct lagg_softc *sc)
2641 {
2642         struct lagg_port *lp;
2643
2644         LAGG_SXLOCK_ASSERT(sc);
2645
2646         /* purge all the lacp ports */
2647         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2648                 lacp_port_destroy(lp);
2649
2650         /* add them back in */
2651         CK_SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
2652                 lacp_port_create(lp);
2653 }
2654
2655 static int
2656 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
2657 {
2658         struct lagg_port *lp;
2659         int err;
2660
2661         lp = lacp_select_tx_port(sc, m, &err);
2662         if (lp == NULL) {
2663                 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
2664                 m_freem(m);
2665                 return (err);
2666         }
2667
2668         /* Send mbuf */
2669         return (lagg_enqueue(lp->lp_ifp, m));
2670 }
2671
2672 static struct mbuf *
2673 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2674 {
2675         struct ifnet *ifp = sc->sc_ifp;
2676         struct ether_header *eh;
2677         u_short etype;
2678
2679         eh = mtod(m, struct ether_header *);
2680         etype = ntohs(eh->ether_type);
2681
2682         /* Tap off LACP control messages */
2683         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_SLOW) {
2684                 m = lacp_input(lp, m);
2685                 if (m == NULL)
2686                         return (NULL);
2687         }
2688
2689         /*
2690          * If the port is not collecting or not in the active aggregator then
2691          * free and return.
2692          */
2693         if (!lacp_iscollecting(lp) || !lacp_isactive(lp)) {
2694                 m_freem(m);
2695                 return (NULL);
2696         }
2697
2698         m->m_pkthdr.rcvif = ifp;
2699         return (m);
2700 }
2701
2702 /* Default input */
2703 static struct mbuf *
2704 lagg_default_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
2705 {
2706         struct ifnet *ifp = sc->sc_ifp;
2707
2708         /* Just pass in the packet to our lagg device */
2709         m->m_pkthdr.rcvif = ifp;
2710
2711         return (m);
2712 }