xref: /freebsd/sys/net/if_lagg.h (revision b5e094cf)
118242d3bSAndrew Thompson /*	$OpenBSD: if_trunk.h,v 1.11 2007/01/31 06:20:19 reyk Exp $	*/
218242d3bSAndrew Thompson 
318242d3bSAndrew Thompson /*
418242d3bSAndrew Thompson  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
518242d3bSAndrew Thompson  *
618242d3bSAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
718242d3bSAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
818242d3bSAndrew Thompson  * copyright notice and this permission notice appear in all copies.
918242d3bSAndrew Thompson  *
1018242d3bSAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1118242d3bSAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1218242d3bSAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1318242d3bSAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1418242d3bSAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1518242d3bSAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1618242d3bSAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1718242d3bSAndrew Thompson  *
1818242d3bSAndrew Thompson  * $FreeBSD$
1918242d3bSAndrew Thompson  */
2018242d3bSAndrew Thompson 
2118242d3bSAndrew Thompson #ifndef _NET_LAGG_H
2218242d3bSAndrew Thompson #define _NET_LAGG_H
2318242d3bSAndrew Thompson 
2418242d3bSAndrew Thompson /*
2518242d3bSAndrew Thompson  * Global definitions
2618242d3bSAndrew Thompson  */
2718242d3bSAndrew Thompson 
2818242d3bSAndrew Thompson #define	LAGG_MAX_PORTS		32	/* logically */
2918242d3bSAndrew Thompson #define	LAGG_MAX_NAMESIZE	32	/* name of a protocol */
3018242d3bSAndrew Thompson #define	LAGG_MAX_STACKING	4	/* maximum number of stacked laggs */
3118242d3bSAndrew Thompson 
3286f67641SAndrew Thompson /* Lagg flags */
3386f67641SAndrew Thompson #define	LAGG_F_HASHL2		0x00000001	/* hash layer 2 */
3486f67641SAndrew Thompson #define	LAGG_F_HASHL3		0x00000002	/* hash layer 3 */
3586f67641SAndrew Thompson #define	LAGG_F_HASHL4		0x00000004	/* hash layer 4 */
3686f67641SAndrew Thompson #define	LAGG_F_HASHMASK		0x00000007
3786f67641SAndrew Thompson 
3818242d3bSAndrew Thompson /* Port flags */
3918242d3bSAndrew Thompson #define	LAGG_PORT_SLAVE		0x00000000	/* normal enslaved port */
4018242d3bSAndrew Thompson #define	LAGG_PORT_MASTER	0x00000001	/* primary port */
4118242d3bSAndrew Thompson #define	LAGG_PORT_STACK		0x00000002	/* stacked lagg port */
4218242d3bSAndrew Thompson #define	LAGG_PORT_ACTIVE	0x00000004	/* port is active */
43ff6c5cf6SAndrew Thompson #define	LAGG_PORT_COLLECTING	0x00000008	/* port is receiving frames */
44ff6c5cf6SAndrew Thompson #define	LAGG_PORT_DISTRIBUTING	0x00000010	/* port is sending frames */
45e3163ef6SAndrew Thompson #define	LAGG_PORT_DISABLED	0x00000020	/* port is disabled */
4618242d3bSAndrew Thompson #define	LAGG_PORT_BITS		"\20\01MASTER\02STACK\03ACTIVE\04COLLECTING" \
47e3163ef6SAndrew Thompson 				  "\05DISTRIBUTING\06DISABLED"
4818242d3bSAndrew Thompson 
4918242d3bSAndrew Thompson /* Supported lagg PROTOs */
50b5e094cfSGleb Smirnoff typedef enum {
51b5e094cfSGleb Smirnoff 	LAGG_PROTO_NONE = 0,	/* no lagg protocol defined */
52b5e094cfSGleb Smirnoff 	LAGG_PROTO_ROUNDROBIN,	/* simple round robin */
53b5e094cfSGleb Smirnoff 	LAGG_PROTO_FAILOVER,	/* active failover */
54b5e094cfSGleb Smirnoff 	LAGG_PROTO_LOADBALANCE,	/* loadbalance */
55b5e094cfSGleb Smirnoff 	LAGG_PROTO_LACP,	/* 802.3ad lacp */
56b5e094cfSGleb Smirnoff 	LAGG_PROTO_ETHERCHANNEL,/* Cisco FEC */
57b5e094cfSGleb Smirnoff 	LAGG_PROTO_BROADCAST,	/* broadcast */
58b5e094cfSGleb Smirnoff 	LAGG_PROTO_MAX,
59b5e094cfSGleb Smirnoff } lagg_proto;
6018242d3bSAndrew Thompson 
6118242d3bSAndrew Thompson struct lagg_protos {
62ec32b37eSAndrew Thompson 	const char		*lpr_name;
63b5e094cfSGleb Smirnoff 	lagg_proto		lpr_proto;
6418242d3bSAndrew Thompson };
6518242d3bSAndrew Thompson 
6618242d3bSAndrew Thompson #define	LAGG_PROTO_DEFAULT	LAGG_PROTO_FAILOVER
6718242d3bSAndrew Thompson #define LAGG_PROTOS	{						\
6818242d3bSAndrew Thompson 	{ "failover",		LAGG_PROTO_FAILOVER },		\
6918242d3bSAndrew Thompson 	{ "fec",		LAGG_PROTO_ETHERCHANNEL },		\
7018242d3bSAndrew Thompson 	{ "lacp",		LAGG_PROTO_LACP },			\
7118242d3bSAndrew Thompson 	{ "loadbalance",	LAGG_PROTO_LOADBALANCE },		\
7218242d3bSAndrew Thompson 	{ "roundrobin",	LAGG_PROTO_ROUNDROBIN },		\
7399cdd961SMarcelo Araujo 	{ "broadcast",	LAGG_PROTO_BROADCAST },		\
7418242d3bSAndrew Thompson 	{ "none",		LAGG_PROTO_NONE },			\
7518242d3bSAndrew Thompson 	{ "default",		LAGG_PROTO_DEFAULT }			\
7618242d3bSAndrew Thompson }
7718242d3bSAndrew Thompson 
7818242d3bSAndrew Thompson /*
7918242d3bSAndrew Thompson  * lagg ioctls.
8018242d3bSAndrew Thompson  */
8118242d3bSAndrew Thompson 
82b3d37ca5SAndrew Thompson /*
83b3d37ca5SAndrew Thompson  * LACP current operational parameters structure.
84b3d37ca5SAndrew Thompson  */
85b3d37ca5SAndrew Thompson struct lacp_opreq {
86b3d37ca5SAndrew Thompson 	uint16_t		actor_prio;
87b3d37ca5SAndrew Thompson 	uint8_t			actor_mac[ETHER_ADDR_LEN];
88b3d37ca5SAndrew Thompson 	uint16_t		actor_key;
89b3d37ca5SAndrew Thompson 	uint16_t		actor_portprio;
90b3d37ca5SAndrew Thompson 	uint16_t		actor_portno;
91b3d37ca5SAndrew Thompson 	uint8_t			actor_state;
92b3d37ca5SAndrew Thompson 	uint16_t		partner_prio;
93b3d37ca5SAndrew Thompson 	uint8_t			partner_mac[ETHER_ADDR_LEN];
94b3d37ca5SAndrew Thompson 	uint16_t		partner_key;
95b3d37ca5SAndrew Thompson 	uint16_t		partner_portprio;
96b3d37ca5SAndrew Thompson 	uint16_t		partner_portno;
97b3d37ca5SAndrew Thompson 	uint8_t			partner_state;
98b3d37ca5SAndrew Thompson };
99b3d37ca5SAndrew Thompson 
10018242d3bSAndrew Thompson /* lagg port settings */
10118242d3bSAndrew Thompson struct lagg_reqport {
10218242d3bSAndrew Thompson 	char			rp_ifname[IFNAMSIZ];	/* name of the lagg */
10318242d3bSAndrew Thompson 	char			rp_portname[IFNAMSIZ];	/* name of the port */
10418242d3bSAndrew Thompson 	u_int32_t		rp_prio;		/* port priority */
10518242d3bSAndrew Thompson 	u_int32_t		rp_flags;		/* port flags */
106b3d37ca5SAndrew Thompson 	union {
107b3d37ca5SAndrew Thompson 		struct lacp_opreq rpsc_lacp;
108b3d37ca5SAndrew Thompson 	} rp_psc;
109b3d37ca5SAndrew Thompson #define rp_lacpreq	rp_psc.rpsc_lacp
11018242d3bSAndrew Thompson };
11118242d3bSAndrew Thompson 
11218242d3bSAndrew Thompson #define	SIOCGLAGGPORT		_IOWR('i', 140, struct lagg_reqport)
11318242d3bSAndrew Thompson #define	SIOCSLAGGPORT		 _IOW('i', 141, struct lagg_reqport)
11418242d3bSAndrew Thompson #define	SIOCSLAGGDELPORT	 _IOW('i', 142, struct lagg_reqport)
11518242d3bSAndrew Thompson 
11618242d3bSAndrew Thompson /* lagg, ports and options */
11718242d3bSAndrew Thompson struct lagg_reqall {
11818242d3bSAndrew Thompson 	char			ra_ifname[IFNAMSIZ];	/* name of the lagg */
11918242d3bSAndrew Thompson 	u_int			ra_proto;		/* lagg protocol */
12018242d3bSAndrew Thompson 
12118242d3bSAndrew Thompson 	size_t			ra_size;		/* size of buffer */
12218242d3bSAndrew Thompson 	struct lagg_reqport	*ra_port;		/* allocated buffer */
12318242d3bSAndrew Thompson 	int			ra_ports;		/* total port count */
124b3d37ca5SAndrew Thompson 	union {
125b3d37ca5SAndrew Thompson 		struct lacp_opreq rpsc_lacp;
126b3d37ca5SAndrew Thompson 	} ra_psc;
127b3d37ca5SAndrew Thompson #define ra_lacpreq	ra_psc.rpsc_lacp
12818242d3bSAndrew Thompson };
12918242d3bSAndrew Thompson 
13018242d3bSAndrew Thompson #define	SIOCGLAGG		_IOWR('i', 143, struct lagg_reqall)
13118242d3bSAndrew Thompson #define	SIOCSLAGG		 _IOW('i', 144, struct lagg_reqall)
13218242d3bSAndrew Thompson 
13386f67641SAndrew Thompson struct lagg_reqflags {
13486f67641SAndrew Thompson 	char			rf_ifname[IFNAMSIZ];	/* name of the lagg */
13586f67641SAndrew Thompson 	uint32_t		rf_flags;		/* lagg protocol */
13686f67641SAndrew Thompson };
13786f67641SAndrew Thompson 
13886f67641SAndrew Thompson #define	SIOCGLAGGFLAGS		_IOWR('i', 145, struct lagg_reqflags)
13986f67641SAndrew Thompson #define	SIOCSLAGGHASH		 _IOW('i', 146, struct lagg_reqflags)
14086f67641SAndrew Thompson 
14118242d3bSAndrew Thompson #ifdef _KERNEL
142b64478a1SGleb Smirnoff 
143b64478a1SGleb Smirnoff #include <sys/counter.h>
144b64478a1SGleb Smirnoff 
14518242d3bSAndrew Thompson /*
14618242d3bSAndrew Thompson  * Internal kernel part
14718242d3bSAndrew Thompson  */
14818242d3bSAndrew Thompson 
14918242d3bSAndrew Thompson #define	lp_ifname		lp_ifp->if_xname	/* interface name */
15018242d3bSAndrew Thompson #define	lp_link_state		lp_ifp->if_link_state	/* link state */
15118242d3bSAndrew Thompson 
15218242d3bSAndrew Thompson #define	LAGG_PORTACTIVE(_tp)	(					\
15318242d3bSAndrew Thompson 	((_tp)->lp_link_state == LINK_STATE_UP) &&			\
15418242d3bSAndrew Thompson 	((_tp)->lp_ifp->if_flags & IFF_UP)				\
15518242d3bSAndrew Thompson )
15618242d3bSAndrew Thompson 
15718242d3bSAndrew Thompson struct lagg_ifreq {
15818242d3bSAndrew Thompson 	union {
15918242d3bSAndrew Thompson 		struct ifreq ifreq;
16018242d3bSAndrew Thompson 		struct {
16118242d3bSAndrew Thompson 			char ifr_name[IFNAMSIZ];
16218242d3bSAndrew Thompson 			struct sockaddr_storage ifr_ss;
16318242d3bSAndrew Thompson 		} ifreq_storage;
16418242d3bSAndrew Thompson 	} ifreq;
16518242d3bSAndrew Thompson };
16618242d3bSAndrew Thompson 
16718242d3bSAndrew Thompson #define	sc_ifflags		sc_ifp->if_flags		/* flags */
16818242d3bSAndrew Thompson #define	sc_ifname		sc_ifp->if_xname		/* name */
16918242d3bSAndrew Thompson #define	sc_capabilities		sc_ifp->if_capabilities	/* capabilities */
17018242d3bSAndrew Thompson 
17118242d3bSAndrew Thompson #define	IFCAP_LAGG_MASK		0xffff0000	/* private capabilities */
17218242d3bSAndrew Thompson #define	IFCAP_LAGG_FULLDUPLEX	0x00010000	/* full duplex with >1 ports */
17318242d3bSAndrew Thompson 
17418242d3bSAndrew Thompson /* Private data used by the loadbalancing protocol */
17518242d3bSAndrew Thompson struct lagg_lb {
17618242d3bSAndrew Thompson 	u_int32_t		lb_key;
17718242d3bSAndrew Thompson 	struct lagg_port	*lb_ports[LAGG_MAX_PORTS];
17818242d3bSAndrew Thompson };
17918242d3bSAndrew Thompson 
18018242d3bSAndrew Thompson struct lagg_mc {
1812d222cb7SAlexander Motin 	struct sockaddr_dl	mc_addr;
182d74fd345SAndrew Thompson 	struct ifmultiaddr      *mc_ifma;
18318242d3bSAndrew Thompson 	SLIST_ENTRY(lagg_mc)	mc_entries;
18418242d3bSAndrew Thompson };
18518242d3bSAndrew Thompson 
186cdc6f95fSAndrew Thompson /* List of interfaces to have the MAC address modified */
187cdc6f95fSAndrew Thompson struct lagg_llq {
188cdc6f95fSAndrew Thompson 	struct ifnet		*llq_ifp;
189cdc6f95fSAndrew Thompson 	uint8_t			llq_lladdr[ETHER_ADDR_LEN];
190cdc6f95fSAndrew Thompson 	SLIST_ENTRY(lagg_llq)	llq_entries;
191cdc6f95fSAndrew Thompson };
192cdc6f95fSAndrew Thompson 
19318242d3bSAndrew Thompson struct lagg_softc {
19418242d3bSAndrew Thompson 	struct ifnet			*sc_ifp;	/* virtual interface */
195310915a4SAdrian Chadd 	struct rmlock			sc_mtx;
196310915a4SAdrian Chadd 	struct mtx			sc_call_mtx;
19718242d3bSAndrew Thompson 	int				sc_proto;	/* lagg protocol */
19818242d3bSAndrew Thompson 	u_int				sc_count;	/* number of ports */
19931402c27SAdrian Chadd 	u_int				sc_active;	/* active port count */
20031402c27SAdrian Chadd 	u_int				sc_flapping;	/* number of flapping
20131402c27SAdrian Chadd 							 * events */
20218242d3bSAndrew Thompson 	struct lagg_port		*sc_primary;	/* primary port */
20318242d3bSAndrew Thompson 	struct ifmedia			sc_media;	/* media config */
20418242d3bSAndrew Thompson 	caddr_t				sc_psc;		/* protocol data */
205960dab09SAndrew Thompson 	uint32_t			sc_seq;		/* sequence counter */
20686f67641SAndrew Thompson 	uint32_t			sc_flags;
20718242d3bSAndrew Thompson 
208b64478a1SGleb Smirnoff 	counter_u64_t			sc_ipackets;
209b64478a1SGleb Smirnoff 	counter_u64_t			sc_opackets;
210b64478a1SGleb Smirnoff 	counter_u64_t			sc_ibytes;
211b64478a1SGleb Smirnoff 	counter_u64_t			sc_obytes;
212b64478a1SGleb Smirnoff 
21318242d3bSAndrew Thompson 	SLIST_HEAD(__tplhd, lagg_port)	sc_ports;	/* list of interfaces */
21418242d3bSAndrew Thompson 	SLIST_ENTRY(lagg_softc)	sc_entries;
21518242d3bSAndrew Thompson 
216cdc6f95fSAndrew Thompson 	struct task			sc_lladdr_task;
217cdc6f95fSAndrew Thompson 	SLIST_HEAD(__llqhd, lagg_llq)	sc_llq_head;	/* interfaces to program
218cdc6f95fSAndrew Thompson 							   the lladdr on */
219cdc6f95fSAndrew Thompson 
22018242d3bSAndrew Thompson 	/* lagg protocol callbacks */
221b1bbc5b3SGleb Smirnoff 	void	(*sc_detach)(struct lagg_softc *);
22218242d3bSAndrew Thompson 	int	(*sc_start)(struct lagg_softc *, struct mbuf *);
22318242d3bSAndrew Thompson 	struct mbuf *(*sc_input)(struct lagg_softc *, struct lagg_port *,
22418242d3bSAndrew Thompson 		    struct mbuf *);
22518242d3bSAndrew Thompson 	int	(*sc_port_create)(struct lagg_port *);
22618242d3bSAndrew Thompson 	void	(*sc_port_destroy)(struct lagg_port *);
22718242d3bSAndrew Thompson 	void	(*sc_linkstate)(struct lagg_port *);
22818242d3bSAndrew Thompson 	void	(*sc_init)(struct lagg_softc *);
22918242d3bSAndrew Thompson 	void	(*sc_stop)(struct lagg_softc *);
23018242d3bSAndrew Thompson 	void	(*sc_lladdr)(struct lagg_softc *);
231b3d37ca5SAndrew Thompson 	void	(*sc_req)(struct lagg_softc *, caddr_t);
232b3d37ca5SAndrew Thompson 	void	(*sc_portreq)(struct lagg_port *, caddr_t);
233644da90dSErmal Luçi 	eventhandler_tag vlan_attach;
234644da90dSErmal Luçi 	eventhandler_tag vlan_detach;
235b64478a1SGleb Smirnoff 	struct callout			sc_callout;
2360bf97ae2SAndrew Thompson 	struct sysctl_ctx_list		ctx;		/* sysctl variables */
23749de4f22SAdrian Chadd 	struct sysctl_oid		*sc_oid;	/* sysctl tree oid */
2380bf97ae2SAndrew Thompson 	int				use_flowid;	/* use M_FLOWID */
2391a8959daSScott Long 	int				flowid_shift;	/* shift the flowid */
24018242d3bSAndrew Thompson };
24118242d3bSAndrew Thompson 
24218242d3bSAndrew Thompson struct lagg_port {
24318242d3bSAndrew Thompson 	struct ifnet			*lp_ifp;	/* physical interface */
244ec32b37eSAndrew Thompson 	struct lagg_softc		*lp_softc;	/* parent lagg */
24518242d3bSAndrew Thompson 	uint8_t				lp_lladdr[ETHER_ADDR_LEN];
24618242d3bSAndrew Thompson 
24718242d3bSAndrew Thompson 	u_char				lp_iftype;	/* interface type */
24818242d3bSAndrew Thompson 	uint32_t			lp_prio;	/* port priority */
24918242d3bSAndrew Thompson 	uint32_t			lp_flags;	/* port flags */
25018242d3bSAndrew Thompson 	int				lp_ifflags;	/* saved ifp flags */
25118242d3bSAndrew Thompson 	void				*lh_cookie;	/* if state hook */
25218242d3bSAndrew Thompson 	caddr_t				lp_psc;		/* protocol data */
253108fe96aSAndrew Thompson 	int				lp_detaching;	/* ifnet is detaching */
25418242d3bSAndrew Thompson 
255d74fd345SAndrew Thompson 	SLIST_HEAD(__mclhd, lagg_mc)	lp_mc_head;	/* multicast addresses */
256d74fd345SAndrew Thompson 
25718242d3bSAndrew Thompson 	/* Redirected callbacks */
25818242d3bSAndrew Thompson 	int	(*lp_ioctl)(struct ifnet *, u_long, caddr_t);
25947e8d432SGleb Smirnoff 	int	(*lp_output)(struct ifnet *, struct mbuf *,
26047e8d432SGleb Smirnoff 		     const struct sockaddr *, struct route *);
26118242d3bSAndrew Thompson 
26218242d3bSAndrew Thompson 	SLIST_ENTRY(lagg_port)		lp_entries;
26318242d3bSAndrew Thompson };
26418242d3bSAndrew Thompson 
265310915a4SAdrian Chadd #define	LAGG_LOCK_INIT(_sc)	rm_init(&(_sc)->sc_mtx, "if_lagg rmlock")
266310915a4SAdrian Chadd #define	LAGG_LOCK_DESTROY(_sc)	rm_destroy(&(_sc)->sc_mtx)
267310915a4SAdrian Chadd #define	LAGG_RLOCK(_sc, _p)	rm_rlock(&(_sc)->sc_mtx, (_p))
268310915a4SAdrian Chadd #define	LAGG_WLOCK(_sc)		rm_wlock(&(_sc)->sc_mtx)
269310915a4SAdrian Chadd #define	LAGG_RUNLOCK(_sc, _p)	rm_runlock(&(_sc)->sc_mtx, (_p))
270310915a4SAdrian Chadd #define	LAGG_WUNLOCK(_sc)	rm_wunlock(&(_sc)->sc_mtx)
271310915a4SAdrian Chadd #define	LAGG_RLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_RLOCKED)
272310915a4SAdrian Chadd #define	LAGG_WLOCK_ASSERT(_sc)	rm_assert(&(_sc)->sc_mtx, RA_WLOCKED)
273310915a4SAdrian Chadd 
274310915a4SAdrian Chadd #define	LAGG_CALLOUT_LOCK_INIT(_sc)					\
275310915a4SAdrian Chadd 	    mtx_init(&(_sc)->sc_call_mtx, "if_lagg callout mutex", NULL,\
276310915a4SAdrian Chadd 	    MTX_DEF)
277310915a4SAdrian Chadd #define	LAGG_CALLOUT_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_call_mtx)
27818242d3bSAndrew Thompson 
27918242d3bSAndrew Thompson extern struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
28018242d3bSAndrew Thompson extern void	(*lagg_linkstate_p)(struct ifnet *, int );
28118242d3bSAndrew Thompson 
28218242d3bSAndrew Thompson int		lagg_enqueue(struct ifnet *, struct mbuf *);
28386f67641SAndrew Thompson uint32_t	lagg_hashmbuf(struct lagg_softc *, struct mbuf *, uint32_t);
28418242d3bSAndrew Thompson 
28531402c27SAdrian Chadd SYSCTL_DECL(_net_link_lagg);
28631402c27SAdrian Chadd 
28718242d3bSAndrew Thompson #endif /* _KERNEL */
28818242d3bSAndrew Thompson 
28918242d3bSAndrew Thompson #endif /* _NET_LAGG_H */
290