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