1168793Sthompsa/*	$OpenBSD: if_trunk.h,v 1.11 2007/01/31 06:20:19 reyk Exp $	*/
2168793Sthompsa
3168793Sthompsa/*
4168793Sthompsa * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5168793Sthompsa *
6168793Sthompsa * Permission to use, copy, modify, and distribute this software for any
7168793Sthompsa * purpose with or without fee is hereby granted, provided that the above
8168793Sthompsa * copyright notice and this permission notice appear in all copies.
9168793Sthompsa *
10168793Sthompsa * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11168793Sthompsa * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12168793Sthompsa * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13168793Sthompsa * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14168793Sthompsa * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15168793Sthompsa * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16168793Sthompsa * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17168793Sthompsa *
18168793Sthompsa * $FreeBSD: stable/10/sys/net/if_lagg.h 362368 2020-06-19 07:07:10Z rpokala $
19168793Sthompsa */
20168793Sthompsa
21168793Sthompsa#ifndef _NET_LAGG_H
22168793Sthompsa#define _NET_LAGG_H
23168793Sthompsa
24168793Sthompsa/*
25168793Sthompsa * Global definitions
26168793Sthompsa */
27168793Sthompsa
28170599Sthompsa#define	LAGG_MAX_PORTS		32	/* logically */
29170599Sthompsa#define	LAGG_MAX_NAMESIZE	32	/* name of a protocol */
30170599Sthompsa#define	LAGG_MAX_STACKING	4	/* maximum number of stacked laggs */
31168793Sthompsa
32232629Sthompsa/* Lagg flags */
33232629Sthompsa#define	LAGG_F_HASHL2		0x00000001	/* hash layer 2 */
34232629Sthompsa#define	LAGG_F_HASHL3		0x00000002	/* hash layer 3 */
35232629Sthompsa#define	LAGG_F_HASHL4		0x00000004	/* hash layer 4 */
36232629Sthompsa#define	LAGG_F_HASHMASK		0x00000007
37232629Sthompsa
38168793Sthompsa/* Port flags */
39170599Sthompsa#define	LAGG_PORT_SLAVE		0x00000000	/* normal enslaved port */
40170599Sthompsa#define	LAGG_PORT_MASTER	0x00000001	/* primary port */
41170599Sthompsa#define	LAGG_PORT_STACK		0x00000002	/* stacked lagg port */
42170599Sthompsa#define	LAGG_PORT_ACTIVE	0x00000004	/* port is active */
43170599Sthompsa#define	LAGG_PORT_COLLECTING	0x00000008	/* port is receiving frames */
44170599Sthompsa#define	LAGG_PORT_DISTRIBUTING	0x00000010	/* port is sending frames */
45170599Sthompsa#define	LAGG_PORT_DISABLED	0x00000020	/* port is disabled */
46170599Sthompsa#define	LAGG_PORT_BITS		"\20\01MASTER\02STACK\03ACTIVE\04COLLECTING" \
47169227Sthompsa				  "\05DISTRIBUTING\06DISABLED"
48168793Sthompsa
49168793Sthompsa/* Supported lagg PROTOs */
50287723Shrstypedef enum {
51287723Shrs	LAGG_PROTO_NONE = 0,	/* no lagg protocol defined */
52287723Shrs	LAGG_PROTO_ROUNDROBIN,	/* simple round robin */
53287723Shrs	LAGG_PROTO_FAILOVER,	/* active failover */
54287723Shrs	LAGG_PROTO_LOADBALANCE,	/* loadbalance */
55287723Shrs	LAGG_PROTO_LACP,	/* 802.3ad lacp */
56287723Shrs	LAGG_PROTO_ETHERCHANNEL,/* Cisco FEC */
57287723Shrs	LAGG_PROTO_MAX,
58287723Shrs} lagg_proto;
59168793Sthompsa
60168793Sthompsastruct lagg_protos {
61170599Sthompsa	const char		*lpr_name;
62287723Shrs	lagg_proto		lpr_proto;
63168793Sthompsa};
64168793Sthompsa
65168793Sthompsa#define	LAGG_PROTO_DEFAULT	LAGG_PROTO_FAILOVER
66168793Sthompsa#define LAGG_PROTOS	{						\
67168793Sthompsa	{ "failover",		LAGG_PROTO_FAILOVER },			\
68168793Sthompsa	{ "fec",		LAGG_PROTO_ETHERCHANNEL },		\
69168793Sthompsa	{ "lacp",		LAGG_PROTO_LACP },			\
70168793Sthompsa	{ "loadbalance",	LAGG_PROTO_LOADBALANCE },		\
71168793Sthompsa	{ "roundrobin",		LAGG_PROTO_ROUNDROBIN },		\
72168793Sthompsa	{ "none",		LAGG_PROTO_NONE },			\
73168793Sthompsa	{ "default",		LAGG_PROTO_DEFAULT }			\
74168793Sthompsa}
75168793Sthompsa
76168793Sthompsa/*
77168793Sthompsa * lagg ioctls.
78168793Sthompsa */
79168793Sthompsa
80171247Sthompsa/*
81171247Sthompsa * LACP current operational parameters structure.
82171247Sthompsa */
83171247Sthompsastruct lacp_opreq {
84171247Sthompsa	uint16_t		actor_prio;
85171247Sthompsa	uint8_t			actor_mac[ETHER_ADDR_LEN];
86171247Sthompsa	uint16_t		actor_key;
87171247Sthompsa	uint16_t		actor_portprio;
88171247Sthompsa	uint16_t		actor_portno;
89171247Sthompsa	uint8_t			actor_state;
90171247Sthompsa	uint16_t		partner_prio;
91171247Sthompsa	uint8_t			partner_mac[ETHER_ADDR_LEN];
92171247Sthompsa	uint16_t		partner_key;
93171247Sthompsa	uint16_t		partner_portprio;
94171247Sthompsa	uint16_t		partner_portno;
95171247Sthompsa	uint8_t			partner_state;
96171247Sthompsa};
97171247Sthompsa
98168793Sthompsa/* lagg port settings */
99168793Sthompsastruct lagg_reqport {
100168793Sthompsa	char			rp_ifname[IFNAMSIZ];	/* name of the lagg */
101168793Sthompsa	char			rp_portname[IFNAMSIZ];	/* name of the port */
102168793Sthompsa	u_int32_t		rp_prio;		/* port priority */
103168793Sthompsa	u_int32_t		rp_flags;		/* port flags */
104171247Sthompsa	union {
105171247Sthompsa		struct lacp_opreq rpsc_lacp;
106171247Sthompsa	} rp_psc;
107171247Sthompsa#define rp_lacpreq	rp_psc.rpsc_lacp
108168793Sthompsa};
109168793Sthompsa
110170599Sthompsa#define	SIOCGLAGGPORT		_IOWR('i', 140, struct lagg_reqport)
111170599Sthompsa#define	SIOCSLAGGPORT		 _IOW('i', 141, struct lagg_reqport)
112170599Sthompsa#define	SIOCSLAGGDELPORT	 _IOW('i', 142, struct lagg_reqport)
113168793Sthompsa
114168793Sthompsa/* lagg, ports and options */
115168793Sthompsastruct lagg_reqall {
116168793Sthompsa	char			ra_ifname[IFNAMSIZ];	/* name of the lagg */
117168793Sthompsa	u_int			ra_proto;		/* lagg protocol */
118168793Sthompsa
119168793Sthompsa	size_t			ra_size;		/* size of buffer */
120168793Sthompsa	struct lagg_reqport	*ra_port;		/* allocated buffer */
121168793Sthompsa	int			ra_ports;		/* total port count */
122171247Sthompsa	union {
123171247Sthompsa		struct lacp_opreq rpsc_lacp;
124171247Sthompsa	} ra_psc;
125171247Sthompsa#define ra_lacpreq	ra_psc.rpsc_lacp
126168793Sthompsa};
127168793Sthompsa
128170599Sthompsa#define	SIOCGLAGG		_IOWR('i', 143, struct lagg_reqall)
129170599Sthompsa#define	SIOCSLAGG		 _IOW('i', 144, struct lagg_reqall)
130168793Sthompsa
131232629Sthompsastruct lagg_reqflags {
132232629Sthompsa	char			rf_ifname[IFNAMSIZ];	/* name of the lagg */
133232629Sthompsa	uint32_t		rf_flags;		/* lagg protocol */
134232629Sthompsa};
135232629Sthompsa
136232629Sthompsa#define	SIOCGLAGGFLAGS		_IOWR('i', 145, struct lagg_reqflags)
137232629Sthompsa#define	SIOCSLAGGHASH		 _IOW('i', 146, struct lagg_reqflags)
138232629Sthompsa
139287723Shrsstruct lagg_reqopts {
140287723Shrs	char			ro_ifname[IFNAMSIZ];	/* name of the lagg */
141287723Shrs
142287723Shrs	int			ro_opts;		/* Option bitmap */
143287723Shrs#define	LAGG_OPT_NONE			0x00
144287723Shrs#define	LAGG_OPT_USE_FLOWID		0x01		/* use M_FLOWID */
145287723Shrs/* Pseudo flags which are used in ro_opts but not stored into sc_opts. */
146287723Shrs#define	LAGG_OPT_FLOWIDSHIFT		0x02		/* Set flowid */
147287723Shrs#define	LAGG_OPT_FLOWIDSHIFT_MASK	0x1f		/* flowid is uint32_t */
148287723Shrs#define	LAGG_OPT_LACP_STRICT		0x10		/* LACP strict mode */
149287723Shrs#define	LAGG_OPT_LACP_TXTEST		0x20		/* LACP debug: txtest */
150287723Shrs#define	LAGG_OPT_LACP_RXTEST		0x40		/* LACP debug: rxtest */
151362368Srpokala#define	LAGG_OPT_LACP_FAST_TIMO		0x80		/* LACP fast timeout */
152287723Shrs	u_int			ro_count;		/* number of ports */
153287723Shrs	u_int			ro_active;		/* active port count */
154287723Shrs	u_int			ro_flapping;		/* number of flapping */
155287723Shrs	int			ro_flowid_shift;	/* shift the flowid */
156287723Shrs};
157287723Shrs
158287723Shrs#define	SIOCGLAGGOPTS		_IOWR('i', 152, struct lagg_reqopts)
159287723Shrs#define	SIOCSLAGGOPTS		 _IOW('i', 153, struct lagg_reqopts)
160287723Shrs
161287723Shrs#define	LAGG_OPT_BITS		"\020\001USE_FLOWID\005LACP_STRICT" \
162362368Srpokala				"\006LACP_TXTEST\007LACP_RXTEST" \
163362368Srpokala				"\010LACP_FAST_TIMO"
164287723Shrs
165168793Sthompsa#ifdef _KERNEL
166249506Sglebius
167249506Sglebius#include <sys/counter.h>
168249506Sglebius
169168793Sthompsa/*
170168793Sthompsa * Internal kernel part
171168793Sthompsa */
172168793Sthompsa
173170599Sthompsa#define	lp_ifname		lp_ifp->if_xname	/* interface name */
174170599Sthompsa#define	lp_link_state		lp_ifp->if_link_state	/* link state */
175168793Sthompsa
176170599Sthompsa#define	LAGG_PORTACTIVE(_tp)	(					\
177168793Sthompsa	((_tp)->lp_link_state == LINK_STATE_UP) &&			\
178170599Sthompsa	((_tp)->lp_ifp->if_flags & IFF_UP)				\
179168793Sthompsa)
180168793Sthompsa
181168793Sthompsastruct lagg_ifreq {
182168793Sthompsa	union {
183168793Sthompsa		struct ifreq ifreq;
184168793Sthompsa		struct {
185168793Sthompsa			char ifr_name[IFNAMSIZ];
186168793Sthompsa			struct sockaddr_storage ifr_ss;
187168793Sthompsa		} ifreq_storage;
188168793Sthompsa	} ifreq;
189168793Sthompsa};
190168793Sthompsa
191170599Sthompsa#define	sc_ifflags		sc_ifp->if_flags		/* flags */
192170599Sthompsa#define	sc_ifname		sc_ifp->if_xname		/* name */
193170599Sthompsa#define	sc_capabilities		sc_ifp->if_capabilities	/* capabilities */
194168793Sthompsa
195170599Sthompsa#define	IFCAP_LAGG_MASK		0xffff0000	/* private capabilities */
196170599Sthompsa#define	IFCAP_LAGG_FULLDUPLEX	0x00010000	/* full duplex with >1 ports */
197168793Sthompsa
198168793Sthompsa/* Private data used by the loadbalancing protocol */
199168793Sthompsastruct lagg_lb {
200168793Sthompsa	u_int32_t		lb_key;
201168793Sthompsa	struct lagg_port	*lb_ports[LAGG_MAX_PORTS];
202168793Sthompsa};
203168793Sthompsa
204168793Sthompsastruct lagg_mc {
205270136Smav	struct sockaddr_dl	mc_addr;
206169327Sthompsa	struct ifmultiaddr      *mc_ifma;
207169327Sthompsa	SLIST_ENTRY(lagg_mc)	mc_entries;
208168793Sthompsa};
209168793Sthompsa
210169329Sthompsa/* List of interfaces to have the MAC address modified */
211169329Sthompsastruct lagg_llq {
212169329Sthompsa	struct ifnet		*llq_ifp;
213169329Sthompsa	uint8_t			llq_lladdr[ETHER_ADDR_LEN];
214287723Shrs	uint8_t			llq_primary;
215169329Sthompsa	SLIST_ENTRY(lagg_llq)	llq_entries;
216169329Sthompsa};
217169329Sthompsa
218168793Sthompsastruct lagg_softc {
219168793Sthompsa	struct ifnet			*sc_ifp;	/* virtual interface */
220255038Sadrian	struct rmlock			sc_mtx;
221255038Sadrian	struct mtx			sc_call_mtx;
222168793Sthompsa	int				sc_proto;	/* lagg protocol */
223168793Sthompsa	u_int				sc_count;	/* number of ports */
224253314Sadrian	u_int				sc_active;	/* active port count */
225253314Sadrian	u_int				sc_flapping;	/* number of flapping
226253314Sadrian							 * events */
227168793Sthompsa	struct lagg_port		*sc_primary;	/* primary port */
228168793Sthompsa	struct ifmedia			sc_media;	/* media config */
229168793Sthompsa	caddr_t				sc_psc;		/* protocol data */
230172554Sthompsa	uint32_t			sc_seq;		/* sequence counter */
231232629Sthompsa	uint32_t			sc_flags;
232168793Sthompsa
233249506Sglebius	counter_u64_t			sc_ipackets;
234249506Sglebius	counter_u64_t			sc_opackets;
235249506Sglebius	counter_u64_t			sc_ibytes;
236249506Sglebius	counter_u64_t			sc_obytes;
237249506Sglebius
238168793Sthompsa	SLIST_HEAD(__tplhd, lagg_port)	sc_ports;	/* list of interfaces */
239168793Sthompsa	SLIST_ENTRY(lagg_softc)	sc_entries;
240168793Sthompsa
241169329Sthompsa	struct task			sc_lladdr_task;
242169329Sthompsa	SLIST_HEAD(__llqhd, lagg_llq)	sc_llq_head;	/* interfaces to program
243169329Sthompsa							   the lladdr on */
244169329Sthompsa
245168793Sthompsa	/* lagg protocol callbacks */
246168793Sthompsa	int	(*sc_detach)(struct lagg_softc *);
247168793Sthompsa	int	(*sc_start)(struct lagg_softc *, struct mbuf *);
248168793Sthompsa	struct mbuf *(*sc_input)(struct lagg_softc *, struct lagg_port *,
249168793Sthompsa		    struct mbuf *);
250168793Sthompsa	int	(*sc_port_create)(struct lagg_port *);
251168793Sthompsa	void	(*sc_port_destroy)(struct lagg_port *);
252168793Sthompsa	void	(*sc_linkstate)(struct lagg_port *);
253168793Sthompsa	void	(*sc_init)(struct lagg_softc *);
254168793Sthompsa	void	(*sc_stop)(struct lagg_softc *);
255168793Sthompsa	void	(*sc_lladdr)(struct lagg_softc *);
256171247Sthompsa	void	(*sc_req)(struct lagg_softc *, caddr_t);
257171247Sthompsa	void	(*sc_portreq)(struct lagg_port *, caddr_t);
258203548Seri	eventhandler_tag vlan_attach;
259203548Seri	eventhandler_tag vlan_detach;
260249506Sglebius	struct callout			sc_callout;
261287723Shrs	u_int				sc_opts;
262281955Shiren	int				flowid_shift;	/* set flowid shift*/
263168793Sthompsa};
264168793Sthompsa
265168793Sthompsastruct lagg_port {
266168793Sthompsa	struct ifnet			*lp_ifp;	/* physical interface */
267170599Sthompsa	struct lagg_softc		*lp_softc;	/* parent lagg */
268168793Sthompsa	uint8_t				lp_lladdr[ETHER_ADDR_LEN];
269168793Sthompsa
270168793Sthompsa	u_char				lp_iftype;	/* interface type */
271168793Sthompsa	uint32_t			lp_prio;	/* port priority */
272168793Sthompsa	uint32_t			lp_flags;	/* port flags */
273168793Sthompsa	int				lp_ifflags;	/* saved ifp flags */
274168793Sthompsa	void				*lh_cookie;	/* if state hook */
275168793Sthompsa	caddr_t				lp_psc;		/* protocol data */
276169328Sthompsa	int				lp_detaching;	/* ifnet is detaching */
277168793Sthompsa
278169327Sthompsa	SLIST_HEAD(__mclhd, lagg_mc)	lp_mc_head;	/* multicast addresses */
279169327Sthompsa
280168793Sthompsa	/* Redirected callbacks */
281168793Sthompsa	int	(*lp_ioctl)(struct ifnet *, u_long, caddr_t);
282249925Sglebius	int	(*lp_output)(struct ifnet *, struct mbuf *,
283249925Sglebius		     const struct sockaddr *, struct route *);
284168793Sthompsa
285168793Sthompsa	SLIST_ENTRY(lagg_port)		lp_entries;
286168793Sthompsa};
287168793Sthompsa
288255038Sadrian#define	LAGG_LOCK_INIT(_sc)	rm_init(&(_sc)->sc_mtx, "if_lagg rmlock")
289255038Sadrian#define	LAGG_LOCK_DESTROY(_sc)	rm_destroy(&(_sc)->sc_mtx)
290255038Sadrian#define	LAGG_RLOCK(_sc, _p)	rm_rlock(&(_sc)->sc_mtx, (_p))
291255038Sadrian#define	LAGG_WLOCK(_sc)		rm_wlock(&(_sc)->sc_mtx)
292255038Sadrian#define	LAGG_RUNLOCK(_sc, _p)	rm_runlock(&(_sc)->sc_mtx, (_p))
293255038Sadrian#define	LAGG_WUNLOCK(_sc)	rm_wunlock(&(_sc)->sc_mtx)
294255038Sadrian#define	LAGG_RLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_RLOCKED)
295255038Sadrian#define	LAGG_WLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_WLOCKED)
296168793Sthompsa
297255038Sadrian#define	LAGG_CALLOUT_LOCK_INIT(_sc)					\
298255038Sadrian	    mtx_init(&(_sc)->sc_call_mtx, "if_lagg callout mutex", NULL,\
299255038Sadrian	    MTX_DEF)
300255038Sadrian#define	LAGG_CALLOUT_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_call_mtx)
301255038Sadrian
302168793Sthompsaextern struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
303168793Sthompsaextern void	(*lagg_linkstate_p)(struct ifnet *, int );
304168793Sthompsa
305168793Sthompsaint		lagg_enqueue(struct ifnet *, struct mbuf *);
306232629Sthompsauint32_t	lagg_hashmbuf(struct lagg_softc *, struct mbuf *, uint32_t);
307168793Sthompsa
308253314SadrianSYSCTL_DECL(_net_link_lagg);
309253314Sadrian
310168793Sthompsa#endif /* _KERNEL */
311168793Sthompsa
312168793Sthompsa#endif /* _NET_LAGG_H */
313