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