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