if_lagg.h revision 297609
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: head/sys/net/if_lagg.h 297609 2016-04-06 04:50:28Z rpokala $
19 */
20
21#ifndef _NET_LAGG_H
22#define _NET_LAGG_H
23
24#include <sys/condvar.h>
25
26/*
27 * Global definitions
28 */
29
30#define	LAGG_MAX_PORTS		32	/* logically */
31#define	LAGG_MAX_NAMESIZE	32	/* name of a protocol */
32#define	LAGG_MAX_STACKING	4	/* maximum number of stacked laggs */
33
34/* Lagg flags */
35#define	LAGG_F_HASHL2		0x00000001	/* hash layer 2 */
36#define	LAGG_F_HASHL3		0x00000002	/* hash layer 3 */
37#define	LAGG_F_HASHL4		0x00000004	/* hash layer 4 */
38#define	LAGG_F_HASHMASK		0x00000007
39
40/* Port flags */
41#define	LAGG_PORT_SLAVE		0x00000000	/* normal enslaved port */
42#define	LAGG_PORT_MASTER	0x00000001	/* primary port */
43#define	LAGG_PORT_STACK		0x00000002	/* stacked lagg port */
44#define	LAGG_PORT_ACTIVE	0x00000004	/* port is active */
45#define	LAGG_PORT_COLLECTING	0x00000008	/* port is receiving frames */
46#define	LAGG_PORT_DISTRIBUTING	0x00000010	/* port is sending frames */
47#define	LAGG_PORT_DISABLED	0x00000020	/* port is disabled */
48#define	LAGG_PORT_BITS		"\20\01MASTER\02STACK\03ACTIVE\04COLLECTING" \
49				  "\05DISTRIBUTING\06DISABLED"
50
51/* Supported lagg PROTOs */
52typedef enum {
53	LAGG_PROTO_NONE = 0,	/* no lagg protocol defined */
54	LAGG_PROTO_ROUNDROBIN,	/* simple round robin */
55	LAGG_PROTO_FAILOVER,	/* active failover */
56	LAGG_PROTO_LOADBALANCE,	/* loadbalance */
57	LAGG_PROTO_LACP,	/* 802.3ad lacp */
58	LAGG_PROTO_BROADCAST,	/* broadcast */
59	LAGG_PROTO_MAX,
60} lagg_proto;
61
62struct lagg_protos {
63	const char		*lpr_name;
64	lagg_proto		lpr_proto;
65};
66
67#define	LAGG_PROTO_DEFAULT	LAGG_PROTO_FAILOVER
68#define LAGG_PROTOS	{						\
69	{ "failover",		LAGG_PROTO_FAILOVER },		\
70	{ "lacp",		LAGG_PROTO_LACP },			\
71	{ "loadbalance",	LAGG_PROTO_LOADBALANCE },		\
72	{ "roundrobin",	LAGG_PROTO_ROUNDROBIN },		\
73	{ "broadcast",	LAGG_PROTO_BROADCAST },		\
74	{ "none",		LAGG_PROTO_NONE },			\
75	{ "default",		LAGG_PROTO_DEFAULT }			\
76}
77
78/*
79 * lagg ioctls.
80 */
81
82/*
83 * LACP current operational parameters structure.
84 */
85struct lacp_opreq {
86	uint16_t		actor_prio;
87	uint8_t			actor_mac[ETHER_ADDR_LEN];
88	uint16_t		actor_key;
89	uint16_t		actor_portprio;
90	uint16_t		actor_portno;
91	uint8_t			actor_state;
92	uint16_t		partner_prio;
93	uint8_t			partner_mac[ETHER_ADDR_LEN];
94	uint16_t		partner_key;
95	uint16_t		partner_portprio;
96	uint16_t		partner_portno;
97	uint8_t			partner_state;
98};
99
100/* lagg port settings */
101struct lagg_reqport {
102	char			rp_ifname[IFNAMSIZ];	/* name of the lagg */
103	char			rp_portname[IFNAMSIZ];	/* name of the port */
104	u_int32_t		rp_prio;		/* port priority */
105	u_int32_t		rp_flags;		/* port flags */
106	union {
107		struct lacp_opreq rpsc_lacp;
108	} rp_psc;
109#define rp_lacpreq	rp_psc.rpsc_lacp
110};
111
112#define	SIOCGLAGGPORT		_IOWR('i', 140, struct lagg_reqport)
113#define	SIOCSLAGGPORT		 _IOW('i', 141, struct lagg_reqport)
114#define	SIOCSLAGGDELPORT	 _IOW('i', 142, struct lagg_reqport)
115
116/* lagg, ports and options */
117struct lagg_reqall {
118	char			ra_ifname[IFNAMSIZ];	/* name of the lagg */
119	u_int			ra_proto;		/* lagg protocol */
120
121	size_t			ra_size;		/* size of buffer */
122	struct lagg_reqport	*ra_port;		/* allocated buffer */
123	int			ra_ports;		/* total port count */
124	union {
125		struct lacp_opreq rpsc_lacp;
126	} ra_psc;
127#define ra_lacpreq	ra_psc.rpsc_lacp
128};
129
130#define	SIOCGLAGG		_IOWR('i', 143, struct lagg_reqall)
131#define	SIOCSLAGG		 _IOW('i', 144, struct lagg_reqall)
132
133struct lagg_reqflags {
134	char			rf_ifname[IFNAMSIZ];	/* name of the lagg */
135	uint32_t		rf_flags;		/* lagg protocol */
136};
137
138#define	SIOCGLAGGFLAGS		_IOWR('i', 145, struct lagg_reqflags)
139#define	SIOCSLAGGHASH		 _IOW('i', 146, struct lagg_reqflags)
140
141struct lagg_reqopts {
142	char			ro_ifname[IFNAMSIZ];	/* name of the lagg */
143
144	int			ro_opts;		/* Option bitmap */
145#define	LAGG_OPT_NONE			0x00
146#define	LAGG_OPT_USE_FLOWID		0x01		/* enable use of flowid */
147/* Pseudo flags which are used in ro_opts but not stored into sc_opts. */
148#define	LAGG_OPT_FLOWIDSHIFT		0x02		/* set flowid shift */
149#define	LAGG_OPT_FLOWIDSHIFT_MASK	0x1f		/* flowid is uint32_t */
150#define	LAGG_OPT_LACP_STRICT		0x10		/* LACP strict mode */
151#define	LAGG_OPT_LACP_TXTEST		0x20		/* LACP debug: txtest */
152#define	LAGG_OPT_LACP_RXTEST		0x40		/* LACP debug: rxtest */
153#define	LAGG_OPT_LACP_TIMEOUT		0x80		/* LACP timeout */
154	u_int			ro_count;		/* number of ports */
155	u_int			ro_active;		/* active port count */
156	u_int			ro_flapping;		/* number of flapping */
157	int			ro_flowid_shift;	/* shift the flowid */
158	uint32_t		ro_bkt;			/* packet bucket for roundrobin */
159};
160
161#define	SIOCGLAGGOPTS		_IOWR('i', 152, struct lagg_reqopts)
162#define	SIOCSLAGGOPTS		 _IOW('i', 153, struct lagg_reqopts)
163
164#define	LAGG_OPT_BITS		"\020\001USE_FLOWID\005LACP_STRICT" \
165				"\006LACP_TXTEST\007LACP_RXTEST"
166
167#ifdef _KERNEL
168
169/*
170 * Internal kernel part
171 */
172
173#define	LAGG_PORTACTIVE(_tp)	(					\
174	((_tp)->lp_ifp->if_link_state == LINK_STATE_UP) &&		\
175	((_tp)->lp_ifp->if_flags & IFF_UP)				\
176)
177
178struct lagg_ifreq {
179	union {
180		struct ifreq ifreq;
181		struct {
182			char ifr_name[IFNAMSIZ];
183			struct sockaddr_storage ifr_ss;
184		} ifreq_storage;
185	} ifreq;
186};
187
188#define	sc_ifflags		sc_ifp->if_flags		/* flags */
189#define	sc_ifname		sc_ifp->if_xname		/* name */
190#define	sc_capabilities		sc_ifp->if_capabilities	/* capabilities */
191
192#define	IFCAP_LAGG_MASK		0xffff0000	/* private capabilities */
193#define	IFCAP_LAGG_FULLDUPLEX	0x00010000	/* full duplex with >1 ports */
194
195/* Private data used by the loadbalancing protocol */
196struct lagg_lb {
197	u_int32_t		lb_key;
198	struct lagg_port	*lb_ports[LAGG_MAX_PORTS];
199};
200
201struct lagg_mc {
202	struct sockaddr_dl	mc_addr;
203	struct ifmultiaddr      *mc_ifma;
204	SLIST_ENTRY(lagg_mc)	mc_entries;
205};
206
207typedef enum {
208	LAGG_LLQTYPE_PHYS = 0,	/* Task related to physical (underlying) port */
209	LAGG_LLQTYPE_VIRT,	/* Task related to lagg interface itself */
210} lagg_llqtype;
211
212/* Adding new entry here, SHOULD also have relevant entry in llq_action */
213typedef enum {
214	LAGG_LLQ_MIN = 0,
215	LAGG_LLQ_LLADDR = LAGG_LLQ_MIN, /* MAC Address index */
216	LAGG_LLQ_MTU, /*  MTU index */
217	LAGG_LLQ_MAX /* This SHOULD be the last entry */
218} lagg_llq_idx;
219
220/* Common list entry definition for each taskq operation */
221struct lagg_llq_slist_entry {
222	SLIST_ENTRY(lagg_llq_slist_entry)	llq_entries;
223};
224
225/* Context for lladdr llq operation part of lagg soft context */
226struct lagg_lladdr_llq_ctxt {
227	struct lagg_llq_slist_entry llq_cmn;	/* This SHOULD be the first
228						member */
229	struct ifnet		*llq_ifp;
230	uint8_t			llq_lladdr[ETHER_ADDR_LEN];
231	lagg_llqtype		llq_type;
232};
233
234/* Context for mtu llq operation part of lagg soft context */
235struct lagg_mtu_llq_ctxt {
236	struct lagg_llq_slist_entry llq_cmn;	/* This SHOULD be the first
237						member */
238    struct ifnet	*llq_ifp;
239    struct ifreq	llq_ifr;
240    uint32_t		llq_old_mtu;
241    int			(*llq_ioctl)(struct ifnet *, u_long, caddr_t);
242};
243
244struct lagg_counters {
245	uint64_t	val[IFCOUNTERS];
246};
247
248/* Conditional variables context for lagg operations */
249struct lagg_signal {
250	struct mtx	lock;
251	struct cv       cv;
252};
253
254/* Lagg MTU context */
255struct lagg_mtu_ctxt {
256	struct  lagg_signal	mtu_sync;	/* Synchronize cmd completion */
257	int			mtu_cmd_ret;
258	bool			busy;
259};
260
261struct lagg_softc {
262	struct ifnet			*sc_ifp;	/* virtual interface */
263	struct rmlock			sc_mtx;
264	int				sc_proto;	/* lagg protocol */
265	u_int				sc_count;	/* number of ports */
266	u_int				sc_active;	/* active port count */
267	u_int				sc_flapping;	/* number of flapping
268							 * events */
269	struct lagg_port		*sc_primary;	/* primary port */
270	struct ifmedia			sc_media;	/* media config */
271	void				*sc_psc;	/* protocol data */
272	uint32_t			sc_seq;		/* sequence counter */
273	uint32_t			sc_flags;
274
275	SLIST_HEAD(__tplhd, lagg_port)	sc_ports;	/* list of interfaces */
276	SLIST_ENTRY(lagg_softc)	sc_entries;
277
278	struct task			sc_llq_task;	/* SYNC & ASYNC ops
279							enqueued here */
280	struct lagg_mtu_ctxt		sc_mtu_ctxt;	/* MTU programming */
281	/*  List of LLQs */
282	SLIST_HEAD(__llqhd, lagg_llq_slist_entry)	sc_llq[LAGG_LLQ_MAX];
283
284	eventhandler_tag vlan_attach;
285	eventhandler_tag vlan_detach;
286	struct callout			sc_callout;
287	u_int				sc_opts;
288	int				flowid_shift;	/* shift the flowid */
289	uint32_t			sc_bkt;		/* packates bucket for roundrobin */
290	uint32_t			sc_bkt_count;	/* packates bucket count for roundrobin */
291	struct lagg_counters		detached_counters; /* detached ports sum */
292};
293
294struct lagg_port {
295	struct ifnet			*lp_ifp;	/* physical interface */
296	struct lagg_softc		*lp_softc;	/* parent lagg */
297	uint8_t				lp_lladdr[ETHER_ADDR_LEN];
298
299	u_char				lp_iftype;	/* interface type */
300	uint32_t			lp_prio;	/* port priority */
301	uint32_t			lp_flags;	/* port flags */
302	int				lp_ifflags;	/* saved ifp flags */
303	void				*lh_cookie;	/* if state hook */
304	void				*lp_psc;	/* protocol data */
305	int				lp_detaching;	/* ifnet is detaching */
306
307	SLIST_HEAD(__mclhd, lagg_mc)	lp_mc_head;	/* multicast addresses */
308
309	/* Redirected callbacks */
310	int	(*lp_ioctl)(struct ifnet *, u_long, caddr_t);
311	int	(*lp_output)(struct ifnet *, struct mbuf *,
312		     const struct sockaddr *, struct route *);
313	struct lagg_counters		port_counters;	/* ifp counters copy */
314
315	SLIST_ENTRY(lagg_port)		lp_entries;
316};
317
318#define	LAGG_LOCK_INIT(_sc)	rm_init(&(_sc)->sc_mtx, "if_lagg rmlock")
319#define	LAGG_LOCK_DESTROY(_sc)	rm_destroy(&(_sc)->sc_mtx)
320#define	LAGG_RLOCK(_sc, _p)	rm_rlock(&(_sc)->sc_mtx, (_p))
321#define	LAGG_WLOCK(_sc)		rm_wlock(&(_sc)->sc_mtx)
322#define	LAGG_RUNLOCK(_sc, _p)	rm_runlock(&(_sc)->sc_mtx, (_p))
323#define	LAGG_WUNLOCK(_sc)	rm_wunlock(&(_sc)->sc_mtx)
324#define	LAGG_RLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_RLOCKED)
325#define	LAGG_WLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_WLOCKED)
326#define	LAGG_UNLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_UNLOCKED)
327
328extern struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
329extern void	(*lagg_linkstate_p)(struct ifnet *, int );
330
331int		lagg_enqueue(struct ifnet *, struct mbuf *);
332
333SYSCTL_DECL(_net_link_lagg);
334
335#endif /* _KERNEL */
336
337#endif /* _NET_LAGG_H */
338