xref: /illumos-gate/usr/src/uts/sun4v/sys/vnet.h (revision 129b3e6c5b0ac55b5021a4c38db6387b6acdaaf1)
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 
38 #define	VNET_SUCCESS		(0)	/* successful return */
39 #define	VNET_FAILURE		(-1)	/* unsuccessful return */
40 
41 #define	KMEM_FREE(_p)		kmem_free((_p), sizeof (*(_p)))
42 
43 #define	VNET_NTXDS		512		/* power of 2 tx descriptors */
44 #define	VNET_LDCWD_INTERVAL	1000		/* watchdog freq in msec */
45 #define	VNET_LDCWD_TXTIMEOUT	1000		/* tx timeout in msec */
46 #define	VNET_LDC_MTU		64		/* ldc mtu */
47 
48 
49 #define	IS_BROADCAST(ehp) \
50 		(ether_cmp(&ehp->ether_dhost, &etherbroadcastaddr) == 0)
51 #define	IS_MULTICAST(ehp) \
52 		((ehp->ether_dhost.ether_addr_octet[0] & 01) == 1)
53 
54 #define	VNET_MATCH_RES(vresp, vnetp)	\
55 	(ether_cmp(vresp->local_macaddr, vnetp->curr_macaddr) == 0)
56 
57 /*
58  * Flags used to indicate the state of the vnet device and its associated
59  * resources.
60  */
61 typedef enum vnet_flags {
62 	VNET_STOPPED = 0x0,
63 	VNET_STARTED = 0x1,
64 	VNET_STOPPING = 0x2
65 } vnet_flags_t;
66 
67 typedef struct vnet_hio_stats {
68 	/* Link Input/Output stats */
69 	uint64_t	ipackets;	/* # rx packets */
70 	uint64_t	ierrors;	/* # rx error */
71 	uint64_t	opackets;	/* # tx packets */
72 	uint64_t	oerrors;	/* # tx error */
73 
74 	/* MIB II variables */
75 	uint64_t	rbytes;		/* # bytes received */
76 	uint64_t	obytes;		/* # bytes transmitted */
77 	uint32_t	multircv;	/* # multicast packets received */
78 	uint32_t	multixmt;	/* # multicast packets for xmit */
79 	uint32_t	brdcstrcv;	/* # broadcast packets received */
80 	uint32_t	brdcstxmt;	/* # broadcast packets for xmit */
81 	uint32_t	norcvbuf;	/* # rcv packets discarded */
82 	uint32_t	noxmtbuf;	/* # xmit packets discarded */
83 } vnet_hio_stats_t;
84 
85 typedef struct vnet_hio_kstats {
86 	/* Link Input/Output stats */
87 	kstat_named_t	ipackets;
88 	kstat_named_t	ipackets64;
89 	kstat_named_t	ierrors;
90 	kstat_named_t	opackets;
91 	kstat_named_t	opackets64;
92 	kstat_named_t	oerrors;
93 
94 	/* required by kstat for MIB II objects(RFC 1213) */
95 	kstat_named_t	rbytes; 	/* MIB - ifInOctets */
96 	kstat_named_t	rbytes64;
97 	kstat_named_t	obytes; 	/* MIB - ifOutOctets */
98 	kstat_named_t	obytes64;
99 	kstat_named_t	multircv; 	/* MIB - ifInNUcastPkts */
100 	kstat_named_t	multixmt; 	/* MIB - ifOutNUcastPkts */
101 	kstat_named_t	brdcstrcv;	/* MIB - ifInNUcastPkts */
102 	kstat_named_t	brdcstxmt;	/* MIB - ifOutNUcastPkts */
103 	kstat_named_t	norcvbuf; 	/* MIB - ifInDiscards */
104 	kstat_named_t	noxmtbuf; 	/* MIB - ifOutDiscards */
105 } vnet_hio_kstats_t;
106 
107 /*
108  * A vnet resource structure.
109  */
110 typedef struct vnet_res {
111 	struct vnet_res		*nextp;		/* next resource in the list */
112 	mac_register_t		macreg;		/* resource's mac_reg */
113 	vio_net_res_type_t	type;		/* resource type */
114 	ether_addr_t		local_macaddr;	/* resource's macaddr */
115 	ether_addr_t		rem_macaddr;	/* resource's remote macaddr */
116 	vnet_flags_t		flags;		/* resource flags */
117 	uint32_t		refcnt;		/* reference count */
118 	struct	vnet		*vnetp;		/* back pointer to vnet */
119 	kstat_t			*ksp;		/* hio kstats */
120 } vnet_res_t;
121 
122 #define	VNET_DDS_TASK_ADD_SHARE		0x01
123 #define	VNET_DDS_TASK_DEL_SHARE		0x02
124 #define	VNET_DDS_TASK_REL_SHARE		0x04
125 
126 /* An instance specific DDS structure */
127 typedef struct vnet_dds_info {
128 	kmutex_t	lock;		/* lock for this structure */
129 	uint8_t		task_flags;	/* flags for taskq */
130 	uint8_t		dds_req_id;	/* DDS message request id */
131 	vio_dds_msg_t	dmsg;		/* Pending DDS message */
132 	dev_info_t	*hio_dip;	/* Hybrid device's dip */
133 	uint64_t	hio_cookie;	/* Hybrid device's cookie */
134 	ddi_taskq_t	*dds_taskqp;	/* Taskq's used for DDS */
135 	struct vnet	*vnetp;		/* Back pointer to vnetp */
136 } vnet_dds_info_t;
137 
138 #define	VNET_NFDB_HASH	64
139 
140 #define	KEY_HASH(key, addr) \
141 	(key = (((uint64_t)(addr[0])) << 40) | \
142 	(((uint64_t)(addr[1])) << 32) | \
143 	(((uint64_t)(addr[2])) << 24) | \
144 	(((uint64_t)(addr[3])) << 16) | \
145 	(((uint64_t)(addr[4])) << 8) | \
146 	((uint64_t)(addr[5])));
147 
148 
149 /* rwlock macros */
150 #define	READ_ENTER(x)	rw_enter(x, RW_READER)
151 #define	WRITE_ENTER(x)	rw_enter(x, RW_WRITER)
152 #define	RW_EXIT(x)	rw_exit(x)
153 
154 #define	VLAN_ID_KEY(key)	((mod_hash_key_t)(uintptr_t)(key))
155 
156 typedef enum {
157 		AST_init = 0x0, AST_vnet_alloc = 0x1,
158 		AST_mac_alloc = 0x2, AST_read_macaddr = 0x4,
159 		AST_vgen_init = 0x8, AST_fdbh_alloc = 0x10,
160 		AST_vdds_init = 0x20, AST_taskq_create = 0x40,
161 		AST_vnet_list = 0x80, AST_macreg = 0x100
162 } vnet_attach_progress_t;
163 
164 /*
165  * vnet instance state information
166  */
167 typedef struct vnet {
168 	int			instance;	/* instance # */
169 	dev_info_t		*dip;		/* dev_info */
170 	uint64_t		reg;		/* reg prop value */
171 	vnet_attach_progress_t	attach_progress; /* attach progress flags */
172 	struct vnet		*nextp;		/* next in list */
173 	mac_handle_t		mh;		/* handle to GLDv3 mac module */
174 	uchar_t			vendor_addr[ETHERADDRL]; /* orig macadr */
175 	uchar_t			curr_macaddr[ETHERADDRL]; /* current macadr */
176 	void			*vgenhdl;	/* Handle for vgen */
177 
178 	uint32_t		fdb_nchains;	/* # of hash chains in fdbtbl */
179 	mod_hash_t		*fdb_hashp;	/* forwarding database */
180 	vnet_res_t		*vsw_fp;	/* cached fdb entry of vsw */
181 	krwlock_t		vsw_fp_rw;	/* lock to protect vsw_fp */
182 	uint32_t		mtu;		/* mtu of the device */
183 
184 	uint16_t		default_vlan_id; /* default vlan id */
185 	uint16_t		pvid;		/* port vlan id (untagged) */
186 	uint16_t		*vids;		/* vlan ids (tagged) */
187 	uint16_t		nvids;		/* # of vids */
188 
189 	link_state_t		link_state;	/* link status */
190 	boolean_t		pls_update;	/* phys link state update ? */
191 	vnet_flags_t		flags;		/* interface flags */
192 	vnet_res_t		*hio_fp;	/* Hybrid IO resource */
193 	vnet_res_t		*vres_list;	/* Resource list */
194 	vnet_dds_info_t		vdds_info;	/* DDS related info */
195 	krwlock_t		vrwlock;	/* Resource list lock */
196 	ddi_taskq_t		*taskqp;	/* Resource taskq */
197 } vnet_t;
198 
199 #ifdef DEBUG
200 /*
201  * debug levels:
202  * DBG_LEVEL1:	Function entry/exit tracing
203  * DBG_LEVEL2:	Info messages
204  * DBG_LEVEL3:	Warning messages
205  * DBG_LEVEL4:	Error messages
206  */
207 
208 enum	{ DBG_LEVEL1 = 0x01, DBG_LEVEL2 = 0x02, DBG_WARN = 0x04,
209 	    DBG_ERR = 0x08 };
210 
211 #define	DBG1(...)	do {						\
212 			    if ((vnet_dbglevel & DBG_LEVEL1) != 0) {	\
213 				debug_printf(__func__, __VA_ARGS__);	\
214 			    }						\
215 			_NOTE(CONSTCOND) } while (0)
216 
217 #define	DBG2(...)	do {						\
218 			    if ((vnet_dbglevel & DBG_LEVEL2) != 0) {	\
219 				debug_printf(__func__, __VA_ARGS__);	\
220 			    }						\
221 			_NOTE(CONSTCOND) } while (0)
222 
223 #define	DWARN(...)	do {						\
224 			    if ((vnet_dbglevel & DBG_WARN) != 0) {	\
225 				debug_printf(__func__, __VA_ARGS__);	\
226 			    }						\
227 			_NOTE(CONSTCOND) } while (0)
228 
229 #define	DERR(...)	do {						\
230 			    if ((vnet_dbglevel & DBG_ERR) != 0) {	\
231 				debug_printf(__func__, __VA_ARGS__);	\
232 			    }						\
233 			_NOTE(CONSTCOND) } while (0)
234 
235 #else
236 
237 #define	DBG1(...)	if (0)	do { } while (0)
238 #define	DBG2(...)	if (0)	do { } while (0)
239 #define	DWARN(...)	if (0)	do { } while (0)
240 #define	DERR(...)	if (0)	do { } while (0)
241 
242 #endif
243 
244 #ifdef	VNET_IOC_DEBUG	/* Debug ioctls */
245 
246 #define	VNET_FORCE_LINK_DOWN	0x1
247 #define	VNET_FORCE_LINK_UP	0x2
248 
249 #endif
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
255 #endif	/* _VNET_H */
256