xref: /illumos-gate/usr/src/uts/intel/io/viona/viona_impl.h (revision 38aced4fb3d60e34a44207b22d0b0dd4909bf4d1)
1 /*
2  * Copyright (c) 2013  Chris Torek <torek @ torek net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /*
27  * This file and its contents are supplied under the terms of the
28  * Common Development and Distribution License ("CDDL"), version 1.0.
29  * You may only use this file in accordance with the terms of version
30  * 1.0 of the CDDL.
31  *
32  * A full copy of the text of the CDDL should have accompanied this
33  * source.  A copy of the CDDL is also available via the Internet at
34  * http://www.illumos.org/license/CDDL.
35  *
36  * Copyright 2015 Pluribus Networks Inc.
37  * Copyright 2019 Joyent, Inc.
38  * Copyright 2021 Oxide Computer Company
39  */
40 
41 #ifndef	_VIONA_IMPL_H
42 #define	_VIONA_IMPL_H
43 
44 #include <sys/ddi.h>
45 #include <sys/list.h>
46 #include <sys/sunddi.h>
47 #include <sys/sunndi.h>
48 #include <sys/strsun.h>
49 #include <sys/sysmacros.h>
50 #include <sys/uio.h>
51 
52 #include <sys/mac_client.h>
53 #include <sys/mac_provider.h>
54 #include <sys/mac_client_priv.h>
55 #include <sys/neti.h>
56 #include <inet/ip.h>
57 #include <inet/tcp.h>
58 
59 #include <sys/vmm_drv.h>
60 #include <sys/viona_io.h>
61 
62 struct viona_link;
63 typedef struct viona_link viona_link_t;
64 struct viona_desb;
65 typedef struct viona_desb viona_desb_t;
66 struct viona_net;
67 typedef struct viona_neti viona_neti_t;
68 
69 enum viona_ring_state {
70 	VRS_RESET	= 0x0,	/* just allocated or reset */
71 	VRS_SETUP	= 0x1,	/* addrs setup and starting worker thread */
72 	VRS_INIT	= 0x2,	/* worker thread started & waiting to run */
73 	VRS_RUN		= 0x3,	/* running work routine */
74 	VRS_STOP	= 0x4,	/* worker is exiting */
75 };
76 enum viona_ring_state_flags {
77 	VRSF_REQ_START	= 0x1,	/* start running from INIT state */
78 	VRSF_REQ_STOP	= 0x2,	/* stop running, clean up, goto RESET state */
79 	VRSF_RENEW	= 0x4,	/* ring renewing lease */
80 };
81 
82 typedef struct viona_vring {
83 	viona_link_t	*vr_link;
84 
85 	kmutex_t	vr_lock;
86 	kcondvar_t	vr_cv;
87 	uint16_t	vr_state;
88 	uint16_t	vr_state_flags;
89 	uint_t		vr_xfer_outstanding;
90 	kthread_t	*vr_worker_thread;
91 	vmm_lease_t	*vr_lease;
92 
93 	/* ring-sized resources for TX activity */
94 	viona_desb_t	*vr_txdesb;
95 	struct iovec	*vr_txiov;
96 
97 	uint_t		vr_intr_enabled;
98 	uint64_t	vr_msi_addr;
99 	uint64_t	vr_msi_msg;
100 
101 	/* Internal ring-related state */
102 	kmutex_t	vr_a_mutex;	/* sync consumers of 'avail' */
103 	kmutex_t	vr_u_mutex;	/* sync consumers of 'used' */
104 	uint64_t	vr_pa;
105 	uint16_t	vr_size;
106 	uint16_t	vr_mask;	/* cached from vr_size */
107 	uint16_t	vr_cur_aidx;	/* trails behind 'avail_idx' */
108 	uint16_t	vr_cur_uidx;	/* drives 'used_idx' */
109 
110 	/* Reference to guest pages holding virtqueue */
111 	void		**vr_map_pages;
112 	vmm_page_t	*vr_map_hold;
113 
114 	/* Per-ring error condition statistics */
115 	struct viona_ring_stats {
116 		uint64_t	rs_ndesc_too_high;
117 		uint64_t	rs_bad_idx;
118 		uint64_t	rs_indir_bad_len;
119 		uint64_t	rs_indir_bad_nest;
120 		uint64_t	rs_indir_bad_next;
121 		uint64_t	rs_no_space;
122 		uint64_t	rs_too_many_desc;
123 		uint64_t	rs_desc_bad_len;
124 
125 		uint64_t	rs_bad_ring_addr;
126 
127 		uint64_t	rs_fail_hcksum;
128 		uint64_t	rs_fail_hcksum6;
129 		uint64_t	rs_fail_hcksum_proto;
130 
131 		uint64_t	rs_bad_rx_frame;
132 		uint64_t	rs_rx_merge_overrun;
133 		uint64_t	rs_rx_merge_underrun;
134 		uint64_t	rs_rx_pad_short;
135 		uint64_t	rs_rx_mcast_check;
136 		uint64_t	rs_too_short;
137 		uint64_t	rs_tx_absent;
138 
139 		uint64_t	rs_rx_hookdrop;
140 		uint64_t	rs_tx_hookdrop;
141 	} vr_stats;
142 } viona_vring_t;
143 
144 struct viona_link {
145 	vmm_hold_t		*l_vm_hold;
146 	boolean_t		l_destroyed;
147 
148 	viona_vring_t		l_vrings[VIONA_VQ_MAX];
149 
150 	uint32_t		l_features;
151 	uint32_t		l_features_hw;
152 	uint32_t		l_cap_csum;
153 
154 	uint16_t		l_notify_ioport;
155 	void			*l_notify_cookie;
156 
157 	datalink_id_t		l_linkid;
158 	mac_handle_t		l_mh;
159 	mac_client_handle_t	l_mch;
160 	mac_promisc_handle_t	l_mph;
161 
162 	pollhead_t		l_pollhead;
163 
164 	viona_neti_t		*l_neti;
165 };
166 
167 typedef struct viona_nethook {
168 	net_handle_t		vnh_neti;
169 	hook_family_t		vnh_family;
170 	hook_event_t		vnh_event_in;
171 	hook_event_t		vnh_event_out;
172 	hook_event_token_t	vnh_token_in;
173 	hook_event_token_t	vnh_token_out;
174 	boolean_t		vnh_hooked;
175 } viona_nethook_t;
176 
177 struct viona_neti {
178 	list_node_t		vni_node;
179 
180 	netid_t			vni_netid;
181 	zoneid_t		vni_zid;
182 
183 	viona_nethook_t		vni_nethook;
184 
185 	kmutex_t		vni_lock;	/* Protects remaining members */
186 	kcondvar_t		vni_ref_change; /* Protected by vni_lock */
187 	uint_t			vni_ref;	/* Protected by vni_lock */
188 	list_t			vni_dev_list;	/* Protected by vni_lock */
189 };
190 
191 typedef struct used_elem {
192 	uint16_t	id;
193 	uint32_t	len;
194 } used_elem_t;
195 
196 typedef struct viona_soft_state {
197 	kmutex_t		ss_lock;
198 	viona_link_t		*ss_link;
199 	list_node_t		ss_node;
200 } viona_soft_state_t;
201 
202 #pragma pack(1)
203 struct virtio_desc {
204 	uint64_t	vd_addr;
205 	uint32_t	vd_len;
206 	uint16_t	vd_flags;
207 	uint16_t	vd_next;
208 };
209 
210 struct virtio_used {
211 	uint32_t	vu_idx;
212 	uint32_t	vu_tlen;
213 };
214 
215 struct virtio_net_mrgrxhdr {
216 	uint8_t		vrh_flags;
217 	uint8_t		vrh_gso_type;
218 	uint16_t	vrh_hdr_len;
219 	uint16_t	vrh_gso_size;
220 	uint16_t	vrh_csum_start;
221 	uint16_t	vrh_csum_offset;
222 	uint16_t	vrh_bufs;
223 };
224 
225 struct virtio_net_hdr {
226 	uint8_t		vrh_flags;
227 	uint8_t		vrh_gso_type;
228 	uint16_t	vrh_hdr_len;
229 	uint16_t	vrh_gso_size;
230 	uint16_t	vrh_csum_start;
231 	uint16_t	vrh_csum_offset;
232 };
233 #pragma pack()
234 
235 #define	VRING_NEED_BAIL(ring, proc)					\
236 		(((ring)->vr_state_flags & VRSF_REQ_STOP) != 0 ||	\
237 		((proc)->p_flag & SEXITING) != 0)
238 
239 
240 #define	VNETHOOK_INTERESTED_IN(neti) \
241 	(neti)->vni_nethook.vnh_event_in.he_interested
242 #define	VNETHOOK_INTERESTED_OUT(neti) \
243 	(neti)->vni_nethook.vnh_event_out.he_interested
244 
245 
246 #define	VIONA_PROBE(name)	DTRACE_PROBE(viona__##name)
247 #define	VIONA_PROBE1(name, arg1, arg2)	\
248 	DTRACE_PROBE1(viona__##name, arg1, arg2)
249 #define	VIONA_PROBE2(name, arg1, arg2, arg3, arg4)	\
250 	DTRACE_PROBE2(viona__##name, arg1, arg2, arg3, arg4)
251 #define	VIONA_PROBE3(name, arg1, arg2, arg3, arg4, arg5, arg6)	\
252 	DTRACE_PROBE3(viona__##name, arg1, arg2, arg3, arg4, arg5, arg6)
253 #define	VIONA_PROBE5(name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, \
254 	arg9, arg10) \
255 	DTRACE_PROBE5(viona__##name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, \
256 	arg8, arg9, arg10)
257 #define	VIONA_PROBE_BAD_RING_ADDR(r, a)		\
258 	VIONA_PROBE2(bad_ring_addr, viona_vring_t *, r, void *, (void *)(a))
259 
260 #define	VIONA_RING_STAT_INCR(r, name)	\
261 	(((r)->vr_stats.rs_ ## name)++)
262 
263 
264 #define	VIONA_MAX_HDRS_LEN	(sizeof (struct ether_vlan_header) + \
265 	IP_MAX_HDR_LENGTH + TCP_MAX_HDR_LENGTH)
266 
267 #define	VRING_AVAIL_F_NO_INTERRUPT	1
268 #define	VRING_USED_F_NO_NOTIFY		1
269 
270 #define	VRING_DESC_F_NEXT	(1 << 0)
271 #define	VRING_DESC_F_WRITE	(1 << 1)
272 #define	VRING_DESC_F_INDIRECT	(1 << 2)
273 
274 #define	VIRTIO_NET_HDR_F_NEEDS_CSUM	(1 << 0)
275 #define	VIRTIO_NET_HDR_F_DATA_VALID	(1 << 1)
276 
277 #define	VIRTIO_NET_HDR_GSO_NONE		0
278 #define	VIRTIO_NET_HDR_GSO_TCPV4	1
279 
280 #define	VIRTIO_NET_F_CSUM		(1 << 0)
281 #define	VIRTIO_NET_F_GUEST_CSUM		(1 << 1)
282 #define	VIRTIO_NET_F_MAC		(1 << 5) /* host supplies MAC */
283 #define	VIRTIO_NET_F_GUEST_TSO4		(1 << 7) /* guest can accept TSO */
284 #define	VIRTIO_NET_F_HOST_TSO4		(1 << 11) /* host can accept TSO */
285 #define	VIRTIO_NET_F_MRG_RXBUF		(1 << 15) /* host can merge RX bufs */
286 #define	VIRTIO_NET_F_STATUS		(1 << 16) /* cfg status field present */
287 #define	VIRTIO_F_RING_NOTIFY_ON_EMPTY	(1 << 24)
288 #define	VIRTIO_F_RING_INDIRECT_DESC	(1 << 28)
289 #define	VIRTIO_F_RING_EVENT_IDX		(1 << 29)
290 
291 
292 void viona_ring_alloc(viona_link_t *, viona_vring_t *);
293 void viona_ring_free(viona_vring_t *);
294 int viona_ring_reset(viona_vring_t *, boolean_t);
295 int viona_ring_init(viona_link_t *, uint16_t, uint16_t, uint64_t);
296 boolean_t viona_ring_lease_renew(viona_vring_t *);
297 
298 int vq_popchain(viona_vring_t *, struct iovec *, uint_t, uint16_t *,
299     vmm_page_t **);
300 void vq_pushchain(viona_vring_t *, uint32_t, uint16_t);
301 void vq_pushchain_many(viona_vring_t *, uint_t, used_elem_t *);
302 
303 void viona_intr_ring(viona_vring_t *ring, boolean_t);
304 void viona_ring_set_no_notify(viona_vring_t *, boolean_t);
305 void viona_ring_disable_notify(viona_vring_t *);
306 void viona_ring_enable_notify(viona_vring_t *);
307 uint16_t viona_ring_num_avail(viona_vring_t *);
308 
309 
310 void viona_rx_init(void);
311 void viona_rx_fini(void);
312 int viona_rx_set(viona_link_t *);
313 void viona_rx_clear(viona_link_t *);
314 void viona_worker_rx(viona_vring_t *, viona_link_t *);
315 
316 extern kmutex_t viona_force_copy_lock;
317 void viona_worker_tx(viona_vring_t *, viona_link_t *);
318 void viona_tx_ring_alloc(viona_vring_t *, const uint16_t);
319 void viona_tx_ring_free(viona_vring_t *, const uint16_t);
320 
321 void viona_neti_attach(void);
322 void viona_neti_detach(void);
323 viona_neti_t *viona_neti_lookup_by_zid(zoneid_t);
324 void viona_neti_rele(viona_neti_t *);
325 int viona_hook(viona_link_t *, viona_vring_t *, mblk_t **, boolean_t);
326 
327 #endif	/* _VIONA_IMPL_H */
328