xref: /illumos-gate/usr/src/uts/sun4v/sys/vnet.h (revision 3c573fcc51430b02603f62713f3f5d1b0b1aed1c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _VNET_H
28 #define	_VNET_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/vnet_res.h>
35 #include <sys/vnet_mailbox.h>
36 #include <sys/modhash.h>
37 #include <net/if.h>
38 #include <sys/mac_client.h>
39 
40 #define	VNET_SUCCESS		(0)	/* successful return */
41 #define	VNET_FAILURE		(-1)	/* unsuccessful return */
42 
43 #define	KMEM_FREE(_p)		kmem_free((_p), sizeof (*(_p)))
44 
45 #define	VNET_NTXDS		512		/* power of 2 tx descriptors */
46 #define	VNET_LDCWD_INTERVAL	1000		/* watchdog freq in msec */
47 #define	VNET_LDCWD_TXTIMEOUT	1000		/* tx timeout in msec */
48 #define	VNET_LDC_MTU		64		/* ldc mtu */
49 
50 
51 #define	IS_BROADCAST(ehp) \
52 		(ether_cmp(&ehp->ether_dhost, &etherbroadcastaddr) == 0)
53 #define	IS_MULTICAST(ehp) \
54 		((ehp->ether_dhost.ether_addr_octet[0] & 01) == 1)
55 
56 #define	VNET_MATCH_RES(vresp, vnetp)	\
57 	(ether_cmp(vresp->local_macaddr, vnetp->curr_macaddr) == 0)
58 
59 /*
60  * Flags used to indicate the state of the vnet device and its associated
61  * resources.
62  */
63 typedef enum vnet_flags {
64 	VNET_STOPPED = 0x0,
65 	VNET_STARTED = 0x1,
66 	VNET_STOPPING = 0x2
67 } vnet_flags_t;
68 
69 typedef struct vnet_hio_stats {
70 	/* Link Input/Output stats */
71 	uint64_t	ipackets;	/* # rx packets */
72 	uint64_t	ierrors;	/* # rx error */
73 	uint64_t	opackets;	/* # tx packets */
74 	uint64_t	oerrors;	/* # tx error */
75 
76 	/* MIB II variables */
77 	uint64_t	rbytes;		/* # bytes received */
78 	uint64_t	obytes;		/* # bytes transmitted */
79 	uint32_t	multircv;	/* # multicast packets received */
80 	uint32_t	multixmt;	/* # multicast packets for xmit */
81 	uint32_t	brdcstrcv;	/* # broadcast packets received */
82 	uint32_t	brdcstxmt;	/* # broadcast packets for xmit */
83 	uint32_t	norcvbuf;	/* # rcv packets discarded */
84 	uint32_t	noxmtbuf;	/* # xmit packets discarded */
85 } vnet_hio_stats_t;
86 
87 typedef struct vnet_hio_kstats {
88 	/* Link Input/Output stats */
89 	kstat_named_t	ipackets;
90 	kstat_named_t	ipackets64;
91 	kstat_named_t	ierrors;
92 	kstat_named_t	opackets;
93 	kstat_named_t	opackets64;
94 	kstat_named_t	oerrors;
95 
96 	/* required by kstat for MIB II objects(RFC 1213) */
97 	kstat_named_t	rbytes; 	/* MIB - ifInOctets */
98 	kstat_named_t	rbytes64;
99 	kstat_named_t	obytes; 	/* MIB - ifOutOctets */
100 	kstat_named_t	obytes64;
101 	kstat_named_t	multircv; 	/* MIB - ifInNUcastPkts */
102 	kstat_named_t	multixmt; 	/* MIB - ifOutNUcastPkts */
103 	kstat_named_t	brdcstrcv;	/* MIB - ifInNUcastPkts */
104 	kstat_named_t	brdcstxmt;	/* MIB - ifOutNUcastPkts */
105 	kstat_named_t	norcvbuf; 	/* MIB - ifInDiscards */
106 	kstat_named_t	noxmtbuf; 	/* MIB - ifOutDiscards */
107 } vnet_hio_kstats_t;
108 
109 /*
110  * A vnet resource structure.
111  */
112 typedef struct vnet_res {
113 	struct vnet_res		*nextp;		/* next resource in the list */
114 	mac_register_t		macreg;		/* resource's mac_reg */
115 	vio_net_res_type_t	type;		/* resource type */
116 	ether_addr_t		local_macaddr;	/* resource's macaddr */
117 	ether_addr_t		rem_macaddr;	/* resource's remote macaddr */
118 	vnet_flags_t		flags;		/* resource flags */
119 	uint32_t		refcnt;		/* reference count */
120 	struct	vnet		*vnetp;		/* back pointer to vnet */
121 	kstat_t			*ksp;		/* hio kstats */
122 	void			*rx_ringp;	/* assoc pseudo rx ring */
123 } vnet_res_t;
124 
125 #define	VNET_DDS_TASK_ADD_SHARE		0x01
126 #define	VNET_DDS_TASK_DEL_SHARE		0x02
127 #define	VNET_DDS_TASK_REL_SHARE		0x04
128 
129 /* An instance specific DDS structure */
130 typedef struct vnet_dds_info {
131 	kmutex_t	lock;		/* lock for this structure */
132 	uint8_t		task_flags;	/* flags for taskq */
133 	uint8_t		dds_req_id;	/* DDS message request id */
134 	vio_dds_msg_t	dmsg;		/* Pending DDS message */
135 	dev_info_t	*hio_dip;	/* Hybrid device's dip */
136 	uint64_t	hio_cookie;	/* Hybrid device's cookie */
137 	char		hio_ifname[LIFNAMSIZ];  /* Hybrid interface name */
138 	ddi_taskq_t	*dds_taskqp;	/* Taskq's used for DDS */
139 	struct vnet	*vnetp;		/* Back pointer to vnetp */
140 } vnet_dds_info_t;
141 
142 #define	VNET_NFDB_HASH	64
143 
144 #define	KEY_HASH(key, addr) \
145 	(key = (((uint64_t)(addr[0])) << 40) | \
146 	(((uint64_t)(addr[1])) << 32) | \
147 	(((uint64_t)(addr[2])) << 24) | \
148 	(((uint64_t)(addr[3])) << 16) | \
149 	(((uint64_t)(addr[4])) << 8) | \
150 	((uint64_t)(addr[5])));
151 
152 
153 /* rwlock macros */
154 #define	READ_ENTER(x)	rw_enter(x, RW_READER)
155 #define	WRITE_ENTER(x)	rw_enter(x, RW_WRITER)
156 #define	RW_EXIT(x)	rw_exit(x)
157 
158 #define	VLAN_ID_KEY(key)	((mod_hash_key_t)(uintptr_t)(key))
159 
160 typedef enum {
161 		AST_init = 0x0, AST_vnet_alloc = 0x1,
162 		AST_ring_init = 0x2, AST_vdds_init = 0x4,
163 		AST_read_macaddr = 0x8, AST_fdbh_alloc = 0x10,
164 		AST_taskq_create = 0x20, AST_vnet_list = 0x40,
165 		AST_vgen_init = 0x80, AST_macreg = 0x100,
166 		AST_init_mdeg = 0x200
167 } vnet_attach_progress_t;
168 
169 #define	VNET_NUM_PSEUDO_GROUPS		1	/* # of pseudo ring grps */
170 #define	VNET_NUM_HYBRID_RINGS		2	/* # of Hybrid tx/rx rings */
171 #define	VNET_HYBRID_RXRING_INDEX	1	/* Hybrid rx ring start index */
172 
173 /*
174  * # of Pseudo TX Rings is defined based on the possible
175  * # of TX Hardware Rings from a Hybrid resource.
176  */
177 #define	VNET_NUM_PSEUDO_TXRINGS		VNET_NUM_HYBRID_RINGS
178 
179 /*
180  * # of Pseudo RX Rings that are reserved and exposed by default.
181  * 1 for LDC resource to vsw + 2 for RX rings of Hybrid resource.
182  */
183 #define	VNET_NUM_PSEUDO_RXRINGS_DEFAULT	(VNET_NUM_HYBRID_RINGS + 1)
184 
185 /* Pseudo RX Ring States */
186 typedef enum {
187 	VNET_RXRING_FREE = 0x0,		/* Free */
188 	VNET_RXRING_INUSE = 0x1,	/* In use */
189 	VNET_RXRING_LDC_SERVICE = 0x2,	/* Mapped to vswitch */
190 	VNET_RXRING_LDC_GUEST = 0x4,	/* Mapped to a peer vnet */
191 	VNET_RXRING_HYBRID = 0x8,	/* Mapped to Hybrid resource */
192 	VNET_RXRING_STARTED = 0x10	/* Started */
193 } vnet_rxring_state_t;
194 
195 /* Pseudo TX Ring States */
196 typedef enum {
197 	VNET_TXRING_FREE = 0x0,		/* Free */
198 	VNET_TXRING_INUSE = 0x1,	/* In use */
199 	VNET_TXRING_SHARED = 0x2,	/* Shared among LDCs */
200 	VNET_TXRING_HYBRID = 0x4,	/* Shared among LDCs, Hybrid resource */
201 	VNET_TXRING_STARTED = 0x8	/* Started */
202 } vnet_txring_state_t;
203 
204 /*
205  * Psuedo TX Ring
206  */
207 typedef struct vnet_pseudo_tx_ring {
208 	uint_t			index;		/* ring index */
209 	vnet_txring_state_t	state;		/* ring state */
210 	void			*grp;		/* grp associated */
211 	void			*vnetp;		/* vnet associated */
212 	mac_ring_handle_t	handle;		/* ring handle in mac layer */
213 	mac_ring_handle_t	hw_rh;	/* Resource type dependent, internal */
214 					/* ring handle. Hybrid res: ring hdl */
215 					/* of hardware rx ring; LDC res: hdl */
216 					/* to the res itself (vnet_res_t)    */
217 } vnet_pseudo_tx_ring_t;
218 
219 /*
220  * Psuedo RX Ring
221  */
222 typedef struct vnet_pseudo_rx_ring {
223 	uint_t			index;		/* ring index */
224 	vnet_rxring_state_t	state;		/* ring state */
225 	void			*grp;		/* grp associated */
226 	void			*vnetp;		/* vnet associated */
227 	mac_ring_handle_t	handle;		/* ring handle in mac layer */
228 	mac_ring_handle_t	hw_rh;	/* Resource type dependent, internal */
229 					/* ring handle. Hybrid res: ring hdl */
230 					/* of hardware tx ring; otherwise    */
231 					/* NULL */
232 	uint64_t		gen_num;	/* Mac layer gen_num */
233 } vnet_pseudo_rx_ring_t;
234 
235 /*
236  * Psuedo TX Ring Group
237  */
238 typedef struct vnet_pseudo_tx_group {
239 	uint_t			index;		/* group index */
240 	void			*vnetp;		/* vnet associated */
241 	mac_group_handle_t	handle;		/* grp handle in mac layer */
242 	uint_t			ring_cnt;	/* total # of rings in grp */
243 	vnet_pseudo_tx_ring_t	*rings;		/* array of rings */
244 } vnet_pseudo_tx_group_t;
245 
246 /*
247  * Psuedo RX Ring Group
248  */
249 typedef struct vnet_pseudo_rx_group {
250 	krwlock_t		lock;		/* sync rings access in grp */
251 	int			index;		/* group index */
252 	void			*vnetp;		/* vnet this grp belongs to */
253 	mac_group_handle_t	handle;		/* grp handle in mac layer */
254 	uint_t			max_ring_cnt;	/* total # of rings in grp */
255 	uint_t			ring_cnt;	/* # of rings in use */
256 	vnet_pseudo_rx_ring_t	*rings;		/* array of rings */
257 } vnet_pseudo_rx_group_t;
258 
259 /*
260  * vnet instance state information
261  */
262 typedef struct vnet {
263 	int			instance;	/* instance # */
264 	dev_info_t		*dip;		/* dev_info */
265 	uint64_t		reg;		/* reg prop value */
266 	vnet_attach_progress_t	attach_progress; /* attach progress flags */
267 	struct vnet		*nextp;		/* next in list */
268 	mac_handle_t		mh;		/* handle to GLDv3 mac module */
269 	uchar_t			vendor_addr[ETHERADDRL]; /* orig macadr */
270 	uchar_t			curr_macaddr[ETHERADDRL]; /* current macadr */
271 	void			*vgenhdl;	/* Handle for vgen */
272 
273 	uint32_t		fdb_nchains;	/* # of hash chains in fdbtbl */
274 	mod_hash_t		*fdb_hashp;	/* forwarding database */
275 	vnet_res_t		*vsw_fp;	/* cached fdb entry of vsw */
276 	krwlock_t		vsw_fp_rw;	/* lock to protect vsw_fp */
277 	uint32_t		mtu;		/* mtu of the device */
278 
279 	uint16_t		default_vlan_id; /* default vlan id */
280 	uint16_t		pvid;		/* port vlan id (untagged) */
281 	uint16_t		*vids;		/* vlan ids (tagged) */
282 	uint16_t		nvids;		/* # of vids */
283 
284 	link_state_t		link_state;	/* link status */
285 	boolean_t		pls_update;	/* phys link state update ? */
286 	vnet_flags_t		flags;		/* interface flags */
287 	vnet_res_t		*hio_fp;	/* Hybrid IO resource */
288 	vnet_res_t		*vres_list;	/* Resource list */
289 	vnet_dds_info_t		vdds_info;	/* DDS related info */
290 	krwlock_t		vrwlock;	/* Resource list lock */
291 	ddi_taskq_t		*taskqp;	/* Resource taskq */
292 
293 	/* pseudo ring groups */
294 	vnet_pseudo_rx_group_t	rx_grp[VNET_NUM_PSEUDO_GROUPS];
295 	vnet_pseudo_tx_group_t	tx_grp[VNET_NUM_PSEUDO_GROUPS];
296 
297 	vio_net_handle_t	hio_vhp;	/* HIO resource hdl */
298 	mac_handle_t		hio_mh;		/* HIO mac hdl */
299 	mac_client_handle_t	hio_mch;	/* HIO mac client hdl */
300 	mac_unicast_handle_t	hio_muh;	/* HIO mac unicst hdl */
301 	mac_notify_handle_t	hio_mnh;	/* HIO notify cb hdl */
302 	mac_group_handle_t	rx_hwgh;	/* HIO rx ring-group hdl */
303 	mac_group_handle_t	tx_hwgh;	/* HIO tx ring-group hdl */
304 } vnet_t;
305 
306 #ifdef DEBUG
307 /*
308  * debug levels:
309  * DBG_LEVEL1:	Function entry/exit tracing
310  * DBG_LEVEL2:	Info messages
311  * DBG_LEVEL3:	Warning messages
312  * DBG_LEVEL4:	Error messages
313  */
314 
315 enum	{ DBG_LEVEL1 = 0x01, DBG_LEVEL2 = 0x02, DBG_WARN = 0x04,
316 	    DBG_ERR = 0x08 };
317 
318 #define	DBG1(...)	do {						\
319 			    if ((vnet_dbglevel & DBG_LEVEL1) != 0) {	\
320 				debug_printf(__func__, __VA_ARGS__);	\
321 			    }						\
322 			_NOTE(CONSTCOND) } while (0)
323 
324 #define	DBG2(...)	do {						\
325 			    if ((vnet_dbglevel & DBG_LEVEL2) != 0) {	\
326 				debug_printf(__func__, __VA_ARGS__);	\
327 			    }						\
328 			_NOTE(CONSTCOND) } while (0)
329 
330 #define	DWARN(...)	do {						\
331 			    if ((vnet_dbglevel & DBG_WARN) != 0) {	\
332 				debug_printf(__func__, __VA_ARGS__);	\
333 			    }						\
334 			_NOTE(CONSTCOND) } while (0)
335 
336 #define	DERR(...)	do {						\
337 			    if ((vnet_dbglevel & DBG_ERR) != 0) {	\
338 				debug_printf(__func__, __VA_ARGS__);	\
339 			    }						\
340 			_NOTE(CONSTCOND) } while (0)
341 
342 #else
343 
344 #define	DBG1(...)	if (0)	do { } while (0)
345 #define	DBG2(...)	if (0)	do { } while (0)
346 #define	DWARN(...)	if (0)	do { } while (0)
347 #define	DERR(...)	if (0)	do { } while (0)
348 
349 #endif
350 
351 #ifdef	VNET_IOC_DEBUG	/* Debug ioctls */
352 
353 #define	VNET_FORCE_LINK_DOWN	0x1
354 #define	VNET_FORCE_LINK_UP	0x2
355 
356 #endif
357 
358 #ifdef __cplusplus
359 }
360 #endif
361 
362 #endif	/* _VNET_H */
363