xref: /illumos-gate/usr/src/uts/intel/io/viona/viona_impl.h (revision e6ec7b0973d9b6269f1c06a33a5e924ac17aec55)
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 2022 OmniOS Community Edition (OmniOSce) Association.
39  * Copyright 2024 Oxide Computer Company
40  */
41 
42 #ifndef	_VIONA_IMPL_H
43 #define	_VIONA_IMPL_H
44 
45 #include <sys/ddi.h>
46 #include <sys/list.h>
47 #include <sys/sunddi.h>
48 #include <sys/sunndi.h>
49 #include <sys/strsun.h>
50 #include <sys/sysmacros.h>
51 #include <sys/uio.h>
52 
53 #include <sys/mac_client.h>
54 #include <sys/mac_provider.h>
55 #include <sys/mac_client_priv.h>
56 #include <sys/neti.h>
57 #include <inet/ip.h>
58 #include <inet/tcp.h>
59 
60 #include <sys/vmm_drv.h>
61 #include <sys/viona_io.h>
62 
63 struct viona_link;
64 typedef struct viona_link viona_link_t;
65 struct viona_desb;
66 typedef struct viona_desb viona_desb_t;
67 struct viona_net;
68 typedef struct viona_neti viona_neti_t;
69 
70 typedef struct viona_transfer_stats {
71 	/* Packets transferred successfully */
72 	uint64_t vts_packets;
73 	/* Bytes transferred successfully */
74 	uint64_t vts_bytes;
75 	/*
76 	 * Count of transfers which encountered errors, not including
77 	 * insufficient space in ring.
78 	 */
79 	uint64_t vts_errors;
80 	/*
81 	 * Count of packets dropped due to insufficient space in the ring or by
82 	 * order of associated hook.
83 	 */
84 	uint64_t vts_drops;
85 } viona_transfer_stats_t;
86 
87 enum viona_ring_state {
88 	VRS_RESET	= 0x0,	/* just allocated or reset */
89 	VRS_SETUP	= 0x1,	/* addrs setup and starting worker thread */
90 	VRS_INIT	= 0x2,	/* worker thread started & waiting to run */
91 	VRS_RUN		= 0x3,	/* running work routine */
92 	VRS_STOP	= 0x4,	/* worker is exiting */
93 };
94 enum viona_ring_state_flags {
95 	VRSF_REQ_START	= 0x1,	/* start running from INIT state */
96 	VRSF_REQ_STOP	= 0x2,	/* stop running, clean up, goto RESET state */
97 	VRSF_REQ_PAUSE	= 0x4,	/* stop running, goto INIT state */
98 	VRSF_RENEW	= 0x8,	/* ring renewing lease */
99 };
100 
101 typedef struct viona_vring {
102 	viona_link_t	*vr_link;
103 
104 	kmutex_t	vr_lock;
105 	kcondvar_t	vr_cv;
106 	uint16_t	vr_state;
107 	uint16_t	vr_state_flags;
108 	uint_t		vr_xfer_outstanding;
109 	kthread_t	*vr_worker_thread;
110 	vmm_lease_t	*vr_lease;
111 
112 	/* ring-sized resources for TX activity */
113 	viona_desb_t	*vr_txdesb;
114 	struct iovec	*vr_txiov;
115 
116 	uint_t		vr_intr_enabled;
117 	uint64_t	vr_msi_addr;
118 	uint64_t	vr_msi_msg;
119 
120 	/* Internal ring-related state */
121 	kmutex_t	vr_a_mutex;	/* sync consumers of 'avail' */
122 	kmutex_t	vr_u_mutex;	/* sync consumers of 'used' */
123 	uint64_t	vr_pa;
124 	uint16_t	vr_size;
125 	uint16_t	vr_mask;	/* cached from vr_size */
126 	uint16_t	vr_cur_aidx;	/* trails behind 'avail_idx' */
127 	uint16_t	vr_cur_uidx;	/* drives 'used_idx' */
128 
129 	/* Reference to guest pages holding virtqueue */
130 	void		**vr_map_pages;
131 	vmm_page_t	*vr_map_hold;
132 
133 	/* Per-ring general statistics */
134 	struct viona_transfer_stats vr_stats;
135 
136 	/* Per-ring error condition statistics */
137 	struct viona_ring_err_stats {
138 		uint64_t	rs_ndesc_too_high;
139 		uint64_t	rs_bad_idx;
140 		uint64_t	rs_indir_bad_len;
141 		uint64_t	rs_indir_bad_nest;
142 		uint64_t	rs_indir_bad_next;
143 		uint64_t	rs_no_space;
144 		uint64_t	rs_too_many_desc;
145 		uint64_t	rs_desc_bad_len;
146 
147 		uint64_t	rs_bad_ring_addr;
148 
149 		uint64_t	rs_fail_hcksum;
150 		uint64_t	rs_fail_hcksum6;
151 		uint64_t	rs_fail_hcksum_proto;
152 
153 		uint64_t	rs_bad_rx_frame;
154 		uint64_t	rs_rx_merge_overrun;
155 		uint64_t	rs_rx_merge_underrun;
156 		uint64_t	rs_rx_pad_short;
157 		uint64_t	rs_rx_mcast_check;
158 		uint64_t	rs_too_short;
159 		uint64_t	rs_tx_absent;
160 
161 		uint64_t	rs_rx_hookdrop;
162 		uint64_t	rs_tx_hookdrop;
163 	} vr_err_stats;
164 } viona_vring_t;
165 
166 struct viona_link {
167 	vmm_hold_t		*l_vm_hold;
168 	boolean_t		l_destroyed;
169 
170 	viona_vring_t		l_vrings[VIONA_VQ_MAX];
171 
172 	uint32_t		l_features;
173 	uint32_t		l_features_hw;
174 	uint32_t		l_cap_csum;
175 
176 	uint16_t		l_notify_ioport;
177 	void			*l_notify_cookie;
178 
179 	datalink_id_t		l_linkid;
180 	mac_handle_t		l_mh;
181 	mac_client_handle_t	l_mch;
182 	mac_promisc_handle_t	l_mph;
183 	mac_unicast_handle_t	l_muh;
184 	viona_promisc_t		l_promisc;
185 
186 	pollhead_t		l_pollhead;
187 
188 	viona_neti_t		*l_neti;
189 
190 	kmutex_t		l_stats_lock;
191 	struct viona_link_stats {
192 		struct viona_transfer_stats vls_rx;
193 		struct viona_transfer_stats vls_tx;
194 	} l_stats;
195 };
196 
197 typedef struct viona_nethook {
198 	net_handle_t		vnh_neti;
199 	hook_family_t		vnh_family;
200 	hook_event_t		vnh_event_in;
201 	hook_event_t		vnh_event_out;
202 	hook_event_token_t	vnh_token_in;
203 	hook_event_token_t	vnh_token_out;
204 	boolean_t		vnh_hooked;
205 } viona_nethook_t;
206 
207 struct viona_neti {
208 	list_node_t		vni_node;
209 
210 	netid_t			vni_netid;
211 	zoneid_t		vni_zid;
212 
213 	viona_nethook_t		vni_nethook;
214 
215 	kmutex_t		vni_lock;	/* Protects remaining members */
216 	kcondvar_t		vni_ref_change; /* Protected by vni_lock */
217 	uint_t			vni_ref;	/* Protected by vni_lock */
218 	list_t			vni_dev_list;	/* Protected by vni_lock */
219 };
220 
221 typedef struct viona_kstats {
222 	kstat_named_t	vk_rx_packets;
223 	kstat_named_t	vk_rx_bytes;
224 	kstat_named_t	vk_rx_errors;
225 	kstat_named_t	vk_rx_drops;
226 	kstat_named_t	vk_tx_packets;
227 	kstat_named_t	vk_tx_bytes;
228 	kstat_named_t	vk_tx_errors;
229 	kstat_named_t	vk_tx_drops;
230 } viona_kstats_t;
231 
232 typedef struct used_elem {
233 	uint16_t	id;
234 	uint32_t	len;
235 } used_elem_t;
236 
237 typedef struct viona_soft_state {
238 	kmutex_t		ss_lock;
239 	viona_link_t		*ss_link;
240 	list_node_t		ss_node;
241 	kstat_t			*ss_kstat;
242 	minor_t			ss_minor;
243 } viona_soft_state_t;
244 
245 #pragma pack(1)
246 struct virtio_desc {
247 	uint64_t	vd_addr;
248 	uint32_t	vd_len;
249 	uint16_t	vd_flags;
250 	uint16_t	vd_next;
251 };
252 
253 struct virtio_used {
254 	uint32_t	vu_idx;
255 	uint32_t	vu_tlen;
256 };
257 
258 struct virtio_net_mrgrxhdr {
259 	uint8_t		vrh_flags;
260 	uint8_t		vrh_gso_type;
261 	uint16_t	vrh_hdr_len;
262 	uint16_t	vrh_gso_size;
263 	uint16_t	vrh_csum_start;
264 	uint16_t	vrh_csum_offset;
265 	uint16_t	vrh_bufs;
266 };
267 
268 struct virtio_net_hdr {
269 	uint8_t		vrh_flags;
270 	uint8_t		vrh_gso_type;
271 	uint16_t	vrh_hdr_len;
272 	uint16_t	vrh_gso_size;
273 	uint16_t	vrh_csum_start;
274 	uint16_t	vrh_csum_offset;
275 };
276 #pragma pack()
277 
278 #define	VNETHOOK_INTERESTED_IN(neti) \
279 	(neti)->vni_nethook.vnh_event_in.he_interested
280 #define	VNETHOOK_INTERESTED_OUT(neti) \
281 	(neti)->vni_nethook.vnh_event_out.he_interested
282 
283 
284 #define	VIONA_PROBE(name)	DTRACE_PROBE(viona__##name)
285 #define	VIONA_PROBE1(name, arg1, arg2)	\
286 	DTRACE_PROBE1(viona__##name, arg1, arg2)
287 #define	VIONA_PROBE2(name, arg1, arg2, arg3, arg4)	\
288 	DTRACE_PROBE2(viona__##name, arg1, arg2, arg3, arg4)
289 #define	VIONA_PROBE3(name, arg1, arg2, arg3, arg4, arg5, arg6)	\
290 	DTRACE_PROBE3(viona__##name, arg1, arg2, arg3, arg4, arg5, arg6)
291 #define	VIONA_PROBE4(name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
292 	DTRACE_PROBE4(viona__##name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, \
293 	arg8)
294 #define	VIONA_PROBE5(name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, \
295 	arg9, arg10) \
296 	DTRACE_PROBE5(viona__##name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, \
297 	arg8, arg9, arg10)
298 #define	VIONA_PROBE_BAD_RING_ADDR(r, a)		\
299 	VIONA_PROBE2(bad_ring_addr, viona_vring_t *, r, void *, (void *)(a))
300 
301 /* Increment one of the named ring error stats */
302 #define	VIONA_RING_STAT_INCR(r, name)	\
303 	(((r)->vr_err_stats.rs_ ## name)++)
304 
305 #define	VIONA_MAX_HDRS_LEN	(sizeof (struct ether_vlan_header) + \
306 	IP_MAX_HDR_LENGTH + TCP_MAX_HDR_LENGTH)
307 
308 #define	VRING_AVAIL_F_NO_INTERRUPT	1
309 #define	VRING_USED_F_NO_NOTIFY		1
310 
311 #define	VRING_DESC_F_NEXT	(1 << 0)
312 #define	VRING_DESC_F_WRITE	(1 << 1)
313 #define	VRING_DESC_F_INDIRECT	(1 << 2)
314 
315 #define	VIRTIO_NET_HDR_F_NEEDS_CSUM	(1 << 0)
316 #define	VIRTIO_NET_HDR_F_DATA_VALID	(1 << 1)
317 
318 #define	VIRTIO_NET_HDR_GSO_NONE		0
319 #define	VIRTIO_NET_HDR_GSO_TCPV4	1
320 
321 #define	VIRTIO_NET_F_CSUM		(1 << 0)
322 #define	VIRTIO_NET_F_GUEST_CSUM		(1 << 1)
323 #define	VIRTIO_NET_F_MAC		(1 << 5) /* host supplies MAC */
324 #define	VIRTIO_NET_F_GUEST_TSO4		(1 << 7) /* guest can accept TSO */
325 #define	VIRTIO_NET_F_HOST_TSO4		(1 << 11) /* host can accept TSO */
326 #define	VIRTIO_NET_F_MRG_RXBUF		(1 << 15) /* host can merge RX bufs */
327 #define	VIRTIO_NET_F_STATUS		(1 << 16) /* cfg status field present */
328 #define	VIRTIO_F_RING_NOTIFY_ON_EMPTY	(1 << 24)
329 #define	VIRTIO_F_RING_INDIRECT_DESC	(1 << 28)
330 #define	VIRTIO_F_RING_EVENT_IDX		(1 << 29)
331 
332 struct viona_ring_params {
333 	uint64_t	vrp_pa;
334 	uint16_t	vrp_size;
335 	uint16_t	vrp_avail_idx;
336 	uint16_t	vrp_used_idx;
337 };
338 
339 void viona_ring_alloc(viona_link_t *, viona_vring_t *);
340 void viona_ring_free(viona_vring_t *);
341 int viona_ring_get_state(viona_link_t *, uint16_t, struct viona_ring_params *);
342 int viona_ring_set_state(viona_link_t *, uint16_t,
343     const struct viona_ring_params *);
344 int viona_ring_reset(viona_vring_t *, boolean_t);
345 int viona_ring_init(viona_link_t *, uint16_t, const struct viona_ring_params *);
346 boolean_t viona_ring_lease_renew(viona_vring_t *);
347 bool vring_need_bail(const viona_vring_t *);
348 int viona_ring_pause(viona_vring_t *);
349 
350 int vq_popchain(viona_vring_t *, struct iovec *, uint_t, uint16_t *,
351     vmm_page_t **);
352 void vq_pushchain(viona_vring_t *, uint32_t, uint16_t);
353 void vq_pushchain_many(viona_vring_t *, uint_t, used_elem_t *);
354 
355 void viona_intr_ring(viona_vring_t *ring, boolean_t);
356 void viona_ring_set_no_notify(viona_vring_t *, boolean_t);
357 void viona_ring_disable_notify(viona_vring_t *);
358 void viona_ring_enable_notify(viona_vring_t *);
359 uint16_t viona_ring_num_avail(viona_vring_t *);
360 
361 void viona_ring_stat_accept(viona_vring_t *, uint32_t);
362 void viona_ring_stat_drop(viona_vring_t *);
363 void viona_ring_stat_error(viona_vring_t *);
364 
365 
366 void viona_rx_init(void);
367 void viona_rx_fini(void);
368 int viona_rx_set(viona_link_t *, viona_promisc_t);
369 void viona_rx_clear(viona_link_t *);
370 void viona_worker_rx(viona_vring_t *, viona_link_t *);
371 
372 extern kmutex_t viona_force_copy_lock;
373 void viona_worker_tx(viona_vring_t *, viona_link_t *);
374 void viona_tx_ring_alloc(viona_vring_t *, const uint16_t);
375 void viona_tx_ring_free(viona_vring_t *, const uint16_t);
376 
377 void viona_neti_attach(void);
378 void viona_neti_detach(void);
379 viona_neti_t *viona_neti_lookup_by_zid(zoneid_t);
380 void viona_neti_rele(viona_neti_t *);
381 int viona_hook(viona_link_t *, viona_vring_t *, mblk_t **, boolean_t);
382 
383 #endif	/* _VIONA_IMPL_H */
384