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