xref: /titanic_50/usr/src/uts/sun4v/sys/vnet.h (revision c242ec1b4cd260e90178d81575297bb3b3648766)
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 	uint32_t		flags;		/* interface flags */
180 	vnet_res_t		*hio_fp;	/* Hybrid IO resource */
181 	vnet_res_t		*vres_list;	/* Resource list */
182 	vnet_dds_info_t		vdds_info;	/* DDS related info */
183 	krwlock_t		vrwlock;	/* Resource list lock */
184 	ddi_taskq_t		*taskqp;	/* Resource taskq */
185 } vnet_t;
186 
187 #define	VNET_STARTED	0x01
188 
189 #ifdef DEBUG
190 /*
191  * debug levels:
192  * DBG_LEVEL1:	Function entry/exit tracing
193  * DBG_LEVEL2:	Info messages
194  * DBG_LEVEL3:	Warning messages
195  * DBG_LEVEL4:	Error messages
196  */
197 
198 enum	{ DBG_LEVEL1 = 0x01, DBG_LEVEL2 = 0x02, DBG_WARN = 0x04,
199 	    DBG_ERR = 0x08 };
200 
201 #define	DBG1(...)	do {						\
202 			    if ((vnet_dbglevel & DBG_LEVEL1) != 0) {	\
203 				debug_printf(__func__, __VA_ARGS__);	\
204 			    }						\
205 			_NOTE(CONSTCOND) } while (0)
206 
207 #define	DBG2(...)	do {						\
208 			    if ((vnet_dbglevel & DBG_LEVEL2) != 0) {	\
209 				debug_printf(__func__, __VA_ARGS__);	\
210 			    }						\
211 			_NOTE(CONSTCOND) } while (0)
212 
213 #define	DWARN(...)	do {						\
214 			    if ((vnet_dbglevel & DBG_WARN) != 0) {	\
215 				debug_printf(__func__, __VA_ARGS__);	\
216 			    }						\
217 			_NOTE(CONSTCOND) } while (0)
218 
219 #define	DERR(...)	do {						\
220 			    if ((vnet_dbglevel & DBG_ERR) != 0) {	\
221 				debug_printf(__func__, __VA_ARGS__);	\
222 			    }						\
223 			_NOTE(CONSTCOND) } while (0)
224 
225 #else
226 
227 #define	DBG1(...)	if (0)	do { } while (0)
228 #define	DBG2(...)	if (0)	do { } while (0)
229 #define	DWARN(...)	if (0)	do { } while (0)
230 #define	DERR(...)	if (0)	do { } while (0)
231 
232 #endif
233 
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 #endif	/* _VNET_H */
239