xref: /openbsd/sys/net/if_trunk.h (revision 91f110e0)
1 /*	$OpenBSD: if_trunk.h,v 1.18 2013/11/18 09:16:30 mpi Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006, 2007 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_TRUNK_H
20 #define _NET_TRUNK_H
21 
22 /*
23  * Global definitions
24  */
25 
26 #define TRUNK_MAX_PORTS		32	/* logically */
27 #define TRUNK_MAX_NAMESIZE	32	/* name of a protocol */
28 #define TRUNK_MAX_STACKING	4	/* maximum number of stacked trunks */
29 
30 /* Port flags */
31 #define TRUNK_PORT_SLAVE		0x00000000 /* normal enslaved port */
32 #define TRUNK_PORT_MASTER		0x00000001 /* primary port */
33 #define TRUNK_PORT_STACK		0x00000002 /* stacked trunk port */
34 #define TRUNK_PORT_ACTIVE		0x00000004 /* port is active */
35 #define TRUNK_PORT_COLLECTING		0x00000008 /* port is receiving frames */
36 #define TRUNK_PORT_DISTRIBUTING	0x00000010 /* port is sending frames */
37 #define TRUNK_PORT_DISABLED		0x00000020 /* port is disabled */
38 #define TRUNK_PORT_GLOBAL		0x80000000 /* IOCTL: global flag */
39 #define TRUNK_PORT_BITS		"\20\01MASTER\02STACK\03ACTIVE" \
40 					 "\04COLLECTING\05DISTRIBUTING\06DISABLED"
41 
42 /* Supported trunk PROTOs */
43 enum trunk_proto {
44 	TRUNK_PROTO_NONE	= 0,		/* no trunk protocol defined */
45 	TRUNK_PROTO_ROUNDROBIN	= 1,		/* simple round robin */
46 	TRUNK_PROTO_FAILOVER	= 2,		/* active failover */
47 	TRUNK_PROTO_LOADBALANCE	= 3,		/* loadbalance */
48 	TRUNK_PROTO_BROADCAST	= 4,		/* broadcast */
49 	TRUNK_PROTO_LACP	= 5,		/* 802.3ad LACP */
50 	TRUNK_PROTO_MAX		= 6
51 };
52 
53 struct trunk_protos {
54 	const char		*tpr_name;
55 	enum trunk_proto	tpr_proto;
56 };
57 
58 #define	TRUNK_PROTO_DEFAULT	TRUNK_PROTO_ROUNDROBIN
59 #define TRUNK_PROTOS	{						\
60 	{ "roundrobin",		TRUNK_PROTO_ROUNDROBIN },		\
61 	{ "failover",		TRUNK_PROTO_FAILOVER },			\
62 	{ "lacp",		TRUNK_PROTO_LACP },			\
63 	{ "loadbalance",	TRUNK_PROTO_LOADBALANCE },		\
64 	{ "broadcast",		TRUNK_PROTO_BROADCAST },		\
65 	{ "none",		TRUNK_PROTO_NONE },			\
66 	{ "default",		TRUNK_PROTO_DEFAULT }			\
67 }
68 
69 /*
70  * Trunk ioctls.
71  */
72 
73 /*
74  * LACP current operational parameters structure.
75  */
76 struct lacp_opreq {
77 	u_int16_t		actor_prio;
78 	u_int8_t		actor_mac[ETHER_ADDR_LEN];
79 	u_int16_t		actor_key;
80 	u_int16_t		actor_portprio;
81 	u_int16_t		actor_portno;
82 	u_int8_t		actor_state;
83 	u_int16_t		partner_prio;
84 	u_int8_t		partner_mac[ETHER_ADDR_LEN];
85 	u_int16_t		partner_key;
86 	u_int16_t		partner_portprio;
87 	u_int16_t		partner_portno;
88 	u_int8_t		partner_state;
89 };
90 
91 /* Trunk port settings */
92 struct trunk_reqport {
93 	char			rp_ifname[IFNAMSIZ];	/* name of the trunk */
94 	char			rp_portname[IFNAMSIZ];	/* name of the port */
95 	u_int32_t		rp_prio;		/* port priority */
96 	u_int32_t		rp_flags;		/* port flags */
97 	union {
98 		struct lacp_opreq rpsc_lacp;
99 	} rp_psc;
100 #define rp_lacpreq	rp_psc.rpsc_lacp
101 };
102 
103 #define SIOCGTRUNKPORT		_IOWR('i', 140, struct trunk_reqport)
104 #define SIOCSTRUNKPORT		 _IOW('i', 141, struct trunk_reqport)
105 #define SIOCSTRUNKDELPORT	 _IOW('i', 142, struct trunk_reqport)
106 
107 /* Trunk, ports and options */
108 struct trunk_reqall {
109 	char			ra_ifname[IFNAMSIZ];	/* name of the trunk */
110 	u_int			ra_proto;		/* trunk protocol */
111 
112 	size_t			ra_size;		/* size of buffer */
113 	struct trunk_reqport	*ra_port;		/* allocated buffer */
114 	int			ra_ports;		/* total port count */
115 	union {
116 		struct lacp_opreq rpsc_lacp;
117 	} ra_psc;
118 #define ra_lacpreq	ra_psc.rpsc_lacp
119 };
120 
121 #define SIOCGTRUNK		_IOWR('i', 143, struct trunk_reqall)
122 #define SIOCSTRUNK		 _IOW('i', 144, struct trunk_reqall)
123 
124 #ifdef _KERNEL
125 /*
126  * Internal kernel part
127  */
128 struct trunk_softc;
129 struct trunk_port {
130 	struct ifnet			*tp_if;		/* physical interface */
131 	struct trunk_softc		*tp_trunk;	/* parent trunk */
132 	u_int8_t			tp_lladdr[ETHER_ADDR_LEN];
133 	caddr_t				tp_psc;		/* protocol data */
134 
135 	u_char				tp_iftype;	/* interface type */
136 	u_int32_t			tp_prio;	/* port priority */
137 	u_int32_t			tp_flags;	/* port flags */
138 	void				*lh_cookie;	/* if state hook */
139 	void				*dh_cookie;	/* if detach hook */
140 
141 	/* Redirected callbacks */
142 	void	(*tp_watchdog)(struct ifnet *);
143 	int	(*tp_ioctl)(struct ifnet *, u_long, caddr_t);
144 
145 	SLIST_ENTRY(trunk_port)		tp_entries;
146 };
147 
148 #define tp_ifname		tp_if->if_xname		/* interface name */
149 #define tp_ifflags		tp_if->if_flags		/* interface flags */
150 #define tp_link_state		tp_if->if_link_state	/* link state */
151 #define tp_capabilities		tp_if->if_capabilities	/* capabilities */
152 
153 #define TRUNK_PORTACTIVE(_tp)	(					\
154 	LINK_STATE_IS_UP((_tp)->tp_link_state) &&			\
155 	(_tp)->tp_ifflags & IFF_UP)
156 
157 struct trunk_mc {
158 	union {
159 		struct ether_multi	*mcu_enm;
160 	} mc_u;
161 	struct sockaddr_storage		mc_addr;
162 
163 	SLIST_ENTRY(trunk_mc)		mc_entries;
164 };
165 
166 #define mc_enm	mc_u.mcu_enm
167 
168 struct trunk_ifreq {
169 	union {
170 		struct ifreq ifreq;
171 		struct {
172 			char ifr_name[IFNAMSIZ];
173 			struct sockaddr_storage ifr_ss;
174 		} ifreq_storage;
175 	} ifreq;
176 };
177 
178 struct trunk_softc {
179 	struct arpcom			tr_ac;		/* virtual interface */
180 	int				tr_unit;	/* trunk unit */
181 	enum trunk_proto		tr_proto;	/* trunk protocol */
182 	u_int				tr_count;	/* number of ports */
183 	struct trunk_port		*tr_primary;	/* primary port */
184 	struct ifmedia			tr_media;	/* media config */
185 	caddr_t				tr_psc;		/* protocol data */
186 
187 	SLIST_HEAD(__tplhd, trunk_port)	tr_ports;	/* list of interfaces */
188 	SLIST_ENTRY(trunk_softc)	tr_entries;
189 
190 	SLIST_HEAD(__mclhd, trunk_mc)	tr_mc_head;	/* multicast addresses */
191 
192 	/* Trunk protocol callbacks */
193 	int	(*tr_detach)(struct trunk_softc *);
194 	int	(*tr_start)(struct trunk_softc *, struct mbuf *);
195 	int	(*tr_watchdog)(struct trunk_softc *);
196 	int	(*tr_input)(struct trunk_softc *, struct trunk_port *,
197 		    struct ether_header *, struct mbuf *);
198 	int	(*tr_port_create)(struct trunk_port *);
199 	void	(*tr_port_destroy)(struct trunk_port *);
200 	void	(*tr_linkstate)(struct trunk_port *);
201 	void	(*tr_init)(struct trunk_softc *);
202 	void	(*tr_stop)(struct trunk_softc *);
203 	void	(*tr_req)(struct trunk_softc *, caddr_t);
204 	void	(*tr_portreq)(struct trunk_port *, caddr_t);
205 };
206 
207 #define tr_ifflags		tr_ac.ac_if.if_flags		/* flags */
208 #define tr_ifname		tr_ac.ac_if.if_xname		/* name */
209 #define tr_capabilities		tr_ac.ac_if.if_capabilities	/* capabilities */
210 #define tr_ifindex		tr_ac.ac_if.if_index		/* int index */
211 #define tr_lladdr		tr_ac.ac_enaddr			/* lladdr */
212 
213 #define IFCAP_TRUNK_MASK	0xffff0000	/* private capabilities */
214 #define IFCAP_TRUNK_FULLDUPLEX	0x00010000	/* full duplex with >1 ports */
215 
216 /* Private data used by the loadbalancing protocol */
217 #define TRUNK_LB_MAXKEYS	8
218 struct trunk_lb {
219 	u_int32_t		lb_key;
220 	struct trunk_port	*lb_ports[TRUNK_MAX_PORTS];
221 };
222 
223 int	 	trunk_input(struct ifnet *, struct ether_header *,
224 		    struct mbuf *);
225 int		trunk_enqueue(struct ifnet *, struct mbuf *);
226 u_int32_t	trunk_hashmbuf(struct mbuf *, u_int32_t);
227 #endif /* _KERNEL */
228 
229 #endif /* _NET_TRUNK_H */
230