xref: /freebsd/sys/net/if_lagg.h (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*	$OpenBSD: if_trunk.h,v 1.11 2007/01/31 06:20:19 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * $FreeBSD$
19  */
20 
21 #ifndef _NET_LAGG_H
22 #define _NET_LAGG_H
23 
24 /*
25  * Global definitions
26  */
27 
28 #define	LAGG_MAX_PORTS		32	/* logically */
29 #define	LAGG_MAX_NAMESIZE	32	/* name of a protocol */
30 #define	LAGG_MAX_STACKING	4	/* maximum number of stacked laggs */
31 
32 /* Lagg flags */
33 #define	LAGG_F_HASHL2		0x00000001	/* hash layer 2 */
34 #define	LAGG_F_HASHL3		0x00000002	/* hash layer 3 */
35 #define	LAGG_F_HASHL4		0x00000004	/* hash layer 4 */
36 #define	LAGG_F_HASHMASK		0x00000007
37 
38 /* Port flags */
39 #define	LAGG_PORT_SLAVE		0x00000000	/* normal enslaved port */
40 #define	LAGG_PORT_MASTER	0x00000001	/* primary port */
41 #define	LAGG_PORT_STACK		0x00000002	/* stacked lagg port */
42 #define	LAGG_PORT_ACTIVE	0x00000004	/* port is active */
43 #define	LAGG_PORT_COLLECTING	0x00000008	/* port is receiving frames */
44 #define	LAGG_PORT_DISTRIBUTING	0x00000010	/* port is sending frames */
45 #define	LAGG_PORT_BITS		"\20\01MASTER\02STACK\03ACTIVE\04COLLECTING" \
46 				  "\05DISTRIBUTING"
47 
48 /* Supported lagg PROTOs */
49 typedef enum {
50 	LAGG_PROTO_NONE = 0,	/* no lagg protocol defined */
51 	LAGG_PROTO_ROUNDROBIN,	/* simple round robin */
52 	LAGG_PROTO_FAILOVER,	/* active failover */
53 	LAGG_PROTO_LOADBALANCE,	/* loadbalance */
54 	LAGG_PROTO_LACP,	/* 802.3ad lacp */
55 	LAGG_PROTO_BROADCAST,	/* broadcast */
56 	LAGG_PROTO_MAX,
57 } lagg_proto;
58 
59 struct lagg_protos {
60 	const char		*lpr_name;
61 	lagg_proto		lpr_proto;
62 };
63 
64 #define	LAGG_PROTO_DEFAULT	LAGG_PROTO_FAILOVER
65 #define LAGG_PROTOS	{						\
66 	{ "failover",		LAGG_PROTO_FAILOVER },			\
67 	{ "lacp",		LAGG_PROTO_LACP },			\
68 	{ "loadbalance",	LAGG_PROTO_LOADBALANCE },		\
69 	{ "roundrobin",		LAGG_PROTO_ROUNDROBIN },		\
70 	{ "broadcast",		LAGG_PROTO_BROADCAST },			\
71 	{ "none",		LAGG_PROTO_NONE },			\
72 	{ "default",		LAGG_PROTO_DEFAULT }			\
73 }
74 
75 /* Supported lagg TYPEs */
76 typedef enum {
77 	LAGG_TYPE_ETHERNET = 0, /* ethernet (default) */
78 	LAGG_TYPE_INFINIBAND,	/* infiniband */
79 	LAGG_TYPE_MAX,
80 } lagg_type;
81 
82 struct lagg_types {
83 	const char		*lt_name;
84 	lagg_type		lt_value;
85 };
86 
87 #define	LAGG_TYPE_DEFAULT	LAGG_TYPE_ETHERNET
88 #define LAGG_TYPES	{						\
89 	{ "ethernet",		LAGG_TYPE_ETHERNET },			\
90 	{ "infiniband",		LAGG_TYPE_INFINIBAND },			\
91 }
92 
93 /*
94  * lagg create clone params
95  */
96 struct iflaggparam {
97 	uint8_t lagg_type;	/* see LAGG_TYPE_XXX */
98 	uint8_t reserved_8[3];
99 	uint32_t reserved_32[3];
100 };
101 
102 /*
103  * lagg ioctls.
104  */
105 
106 /*
107  * LACP current operational parameters structure.
108  */
109 struct lacp_opreq {
110 	uint16_t		actor_prio;
111 	uint8_t			actor_mac[ETHER_ADDR_LEN];
112 	uint16_t		actor_key;
113 	uint16_t		actor_portprio;
114 	uint16_t		actor_portno;
115 	uint8_t			actor_state;
116 	uint16_t		partner_prio;
117 	uint8_t			partner_mac[ETHER_ADDR_LEN];
118 	uint16_t		partner_key;
119 	uint16_t		partner_portprio;
120 	uint16_t		partner_portno;
121 	uint8_t			partner_state;
122 };
123 
124 /* lagg port settings */
125 struct lagg_reqport {
126 	char			rp_ifname[IFNAMSIZ];	/* name of the lagg */
127 	char			rp_portname[IFNAMSIZ];	/* name of the port */
128 	u_int32_t		rp_prio;		/* port priority */
129 	u_int32_t		rp_flags;		/* port flags */
130 	union {
131 		struct lacp_opreq rpsc_lacp;
132 	} rp_psc;
133 #define rp_lacpreq	rp_psc.rpsc_lacp
134 };
135 
136 #define	SIOCGLAGGPORT		_IOWR('i', 140, struct lagg_reqport)
137 #define	SIOCSLAGGPORT		 _IOW('i', 141, struct lagg_reqport)
138 #define	SIOCSLAGGDELPORT	 _IOW('i', 142, struct lagg_reqport)
139 
140 /* lagg, ports and options */
141 struct lagg_reqall {
142 	char			ra_ifname[IFNAMSIZ];	/* name of the lagg */
143 	u_int			ra_proto;		/* lagg protocol */
144 
145 	size_t			ra_size;		/* size of buffer */
146 	struct lagg_reqport	*ra_port;		/* allocated buffer */
147 	int			ra_ports;		/* total port count */
148 	union {
149 		struct lacp_opreq rpsc_lacp;
150 	} ra_psc;
151 #define ra_lacpreq	ra_psc.rpsc_lacp
152 };
153 
154 #define	SIOCGLAGG		_IOWR('i', 143, struct lagg_reqall)
155 #define	SIOCSLAGG		 _IOW('i', 144, struct lagg_reqall)
156 
157 struct lagg_reqflags {
158 	char			rf_ifname[IFNAMSIZ];	/* name of the lagg */
159 	uint32_t		rf_flags;		/* lagg protocol */
160 };
161 
162 #define	SIOCGLAGGFLAGS		_IOWR('i', 145, struct lagg_reqflags)
163 #define	SIOCSLAGGHASH		 _IOW('i', 146, struct lagg_reqflags)
164 
165 struct lagg_reqopts {
166 	char			ro_ifname[IFNAMSIZ];	/* name of the lagg */
167 
168 	int			ro_opts;		/* Option bitmap */
169 #define	LAGG_OPT_NONE			0x00
170 #define	LAGG_OPT_USE_FLOWID		0x01		/* enable use of flowid */
171 /* Pseudo flags which are used in ro_opts but not stored into sc_opts. */
172 #define	LAGG_OPT_FLOWIDSHIFT		0x02		/* set flowid shift */
173 #define	LAGG_OPT_USE_NUMA		0x04		/* enable use of numa */
174 #define	LAGG_OPT_FLOWIDSHIFT_MASK	0x1f		/* flowid is uint32_t */
175 #define	LAGG_OPT_LACP_STRICT		0x10		/* LACP strict mode */
176 #define	LAGG_OPT_LACP_TXTEST		0x20		/* LACP debug: txtest */
177 #define	LAGG_OPT_LACP_RXTEST		0x40		/* LACP debug: rxtest */
178 #define	LAGG_OPT_LACP_FAST_TIMO		0x80		/* LACP fast timeout */
179 #define	LAGG_OPT_RR_LIMIT		0x100		/* RR stride */
180 	u_int			ro_count;		/* number of ports */
181 	u_int			ro_active;		/* active port count */
182 	u_int			ro_flapping;		/* number of flapping */
183 	int			ro_flowid_shift;	/* shift the flowid */
184 	uint32_t		ro_bkt;			/* stride for RR */
185 };
186 
187 #define	SIOCGLAGGOPTS		_IOWR('i', 152, struct lagg_reqopts)
188 #define	SIOCSLAGGOPTS		 _IOW('i', 153, struct lagg_reqopts)
189 
190 #define	LAGG_OPT_BITS		"\020\001USE_FLOWID\003USE_NUMA" \
191 				"\005LACP_STRICT\006LACP_TXTEST" \
192 				"\007LACP_RXTEST\010LACP_FAST_TIMO"
193 
194 #ifdef _KERNEL
195 
196 /*
197  * Internal kernel part
198  */
199 
200 #define	LAGG_PORTACTIVE(_tp)	(					\
201 	((_tp)->lp_ifp->if_link_state == LINK_STATE_UP) &&		\
202 	((_tp)->lp_ifp->if_flags & IFF_UP)				\
203 )
204 
205 struct lagg_ifreq {
206 	union {
207 		struct ifreq ifreq;
208 		struct {
209 			char ifr_name[IFNAMSIZ];
210 			struct sockaddr_storage ifr_ss;
211 		} ifreq_storage;
212 	} ifreq;
213 };
214 
215 #define	sc_ifflags		sc_ifp->if_flags		/* flags */
216 #define	sc_ifname		sc_ifp->if_xname		/* name */
217 
218 /* Private data used by the loadbalancing protocol */
219 struct lagg_lb {
220 	u_int32_t		lb_key;
221 	struct lagg_port	*lb_ports[LAGG_MAX_PORTS];
222 };
223 
224 struct lagg_mc {
225 	struct sockaddr_dl	mc_addr;
226 	struct ifmultiaddr      *mc_ifma;
227 	SLIST_ENTRY(lagg_mc)	mc_entries;
228 };
229 
230 struct lagg_counters {
231 	uint64_t	val[IFCOUNTERS];
232 };
233 
234 struct lagg_softc {
235 	struct ifnet			*sc_ifp;	/* virtual interface */
236 	struct mtx			sc_mtx;		/* watchdog mutex */
237 	struct sx			sc_sx;
238 	int				sc_proto;	/* lagg protocol */
239 	u_int				sc_count;	/* number of ports */
240 	u_int				sc_active;	/* active port count */
241 	u_int				sc_flapping;	/* number of flapping
242 							 * events */
243 	struct lagg_port		*sc_primary;	/* primary port */
244 	struct ifmedia			sc_media;	/* media config */
245 	void				*sc_psc;	/* protocol data */
246 	uint32_t			sc_seq;		/* sequence counter */
247 	uint32_t			sc_stride;	/* stride for RR */
248 	uint32_t			sc_flags;
249 	int				sc_destroying;	/* destroying lagg */
250 
251 	CK_SLIST_HEAD(__tplhd, lagg_port)	sc_ports;	/* list of interfaces */
252 	SLIST_ENTRY(lagg_softc)	sc_entries;
253 
254 	eventhandler_tag vlan_attach;
255 	eventhandler_tag vlan_detach;
256 	struct callout			sc_callout;
257 	u_int				sc_opts;
258 	int				flowid_shift;	/* shift the flowid */
259 	struct lagg_counters		detached_counters; /* detached ports sum */
260 	struct callout			sc_watchdog;	/* watchdog timer */
261 #define	LAGG_ADDR_LEN \
262 	MAX(INFINIBAND_ADDR_LEN, ETHER_ADDR_LEN)
263 	uint8_t				sc_bcast_addr[LAGG_ADDR_LEN];
264 };
265 
266 struct lagg_port {
267 	struct ifnet			*lp_ifp;	/* physical interface */
268 	struct lagg_softc		*lp_softc;	/* parent lagg */
269 	uint8_t				lp_lladdr[LAGG_ADDR_LEN];
270 
271 	u_char				lp_iftype;	/* interface type */
272 	uint32_t			lp_prio;	/* port priority */
273 	uint32_t			lp_flags;	/* port flags */
274 	int				lp_ifflags;	/* saved ifp flags */
275 	int				lp_ifcapenable;	/* saved ifp capenable */
276 	void				*lh_cookie;	/* if state hook */
277 	void				*lp_psc;	/* protocol data */
278 	int				lp_detaching;	/* ifnet is detaching */
279 	SLIST_HEAD(__mclhd, lagg_mc)	lp_mc_head;	/* multicast addresses */
280 
281 	/* Redirected callbacks */
282 	int	(*lp_ioctl)(struct ifnet *, u_long, caddr_t);
283 	int	(*lp_output)(struct ifnet *, struct mbuf *,
284 		     const struct sockaddr *, struct route *);
285 	struct lagg_counters		port_counters;	/* ifp counters copy */
286 
287 	CK_SLIST_ENTRY(lagg_port)		lp_entries;
288 	struct epoch_context	lp_epoch_ctx;
289 };
290 
291 extern struct mbuf *(*lagg_input_ethernet_p)(struct ifnet *, struct mbuf *);
292 extern struct mbuf *(*lagg_input_infiniband_p)(struct ifnet *, struct mbuf *);
293 extern void	(*lagg_linkstate_p)(struct ifnet *, int );
294 
295 int		lagg_enqueue(struct ifnet *, struct mbuf *);
296 
297 SYSCTL_DECL(_net_link_lagg);
298 
299 #endif /* _KERNEL */
300 
301 #endif /* _NET_LAGG_H */
302