xref: /spdk/lib/vhost/vhost_internal.h (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef SPDK_VHOST_INTERNAL_H
35 #define SPDK_VHOST_INTERNAL_H
36 #include <linux/virtio_config.h>
37 
38 #include "spdk/stdinc.h"
39 
40 #include <rte_vhost.h>
41 
42 #include "spdk_internal/vhost_user.h"
43 #include "spdk/log.h"
44 #include "spdk/util.h"
45 #include "spdk/rpc.h"
46 #include "spdk/config.h"
47 
48 extern bool g_packed_ring_recovery;
49 
50 #define SPDK_VHOST_MAX_VQUEUES	256
51 #define SPDK_VHOST_MAX_VQ_SIZE	1024
52 
53 #define SPDK_VHOST_SCSI_CTRLR_MAX_DEVS 8
54 
55 #define SPDK_VHOST_IOVS_MAX 129
56 
57 #define SPDK_VHOST_VQ_MAX_SUBMISSIONS	32
58 
59 /*
60  * Rate at which stats are checked for interrupt coalescing.
61  */
62 #define SPDK_VHOST_STATS_CHECK_INTERVAL_MS 10
63 /*
64  * Default threshold at which interrupts start to be coalesced.
65  */
66 #define SPDK_VHOST_VQ_IOPS_COALESCING_THRESHOLD 60000
67 
68 /*
69  * Currently coalescing is not used by default.
70  * Setting this to value > 0 here or by RPC will enable coalescing.
71  */
72 #define SPDK_VHOST_COALESCING_DELAY_BASE_US 0
73 
74 #define SPDK_VHOST_FEATURES ((1ULL << VHOST_F_LOG_ALL) | \
75 	(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
76 	(1ULL << VIRTIO_F_VERSION_1) | \
77 	(1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
78 	(1ULL << VIRTIO_RING_F_EVENT_IDX) | \
79 	(1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
80 	(1ULL << VIRTIO_F_RING_PACKED))
81 
82 #define SPDK_VHOST_DISABLED_FEATURES ((1ULL << VIRTIO_RING_F_EVENT_IDX) | \
83 	(1ULL << VIRTIO_F_NOTIFY_ON_EMPTY))
84 
85 #define VRING_DESC_F_AVAIL	(1ULL << VRING_PACKED_DESC_F_AVAIL)
86 #define VRING_DESC_F_USED	(1ULL << VRING_PACKED_DESC_F_USED)
87 #define VRING_DESC_F_AVAIL_USED	(VRING_DESC_F_AVAIL | VRING_DESC_F_USED)
88 
89 typedef struct rte_vhost_resubmit_desc spdk_vhost_resubmit_desc;
90 typedef struct rte_vhost_resubmit_info spdk_vhost_resubmit_info;
91 typedef struct rte_vhost_inflight_desc_packed	spdk_vhost_inflight_desc;
92 
93 struct spdk_vhost_virtqueue {
94 	struct rte_vhost_vring vring;
95 	struct rte_vhost_ring_inflight vring_inflight;
96 	uint16_t last_avail_idx;
97 	uint16_t last_used_idx;
98 
99 	struct {
100 		/* To mark a descriptor as available in packed ring
101 		 * Equal to avail_wrap_counter in spec.
102 		 */
103 		uint8_t avail_phase	: 1;
104 		/* To mark a descriptor as used in packed ring
105 		 * Equal to used_wrap_counter in spec.
106 		 */
107 		uint8_t used_phase	: 1;
108 		uint8_t padding		: 5;
109 		bool packed_ring	: 1;
110 	} packed;
111 
112 	void *tasks;
113 
114 	/* Request count from last stats check */
115 	uint32_t req_cnt;
116 
117 	/* Request count from last event */
118 	uint16_t used_req_cnt;
119 
120 	/* How long interrupt is delayed */
121 	uint32_t irq_delay_time;
122 
123 	/* Next time when we need to send event */
124 	uint64_t next_event_time;
125 
126 	/* Associated vhost_virtqueue in the virtio device's virtqueue list */
127 	uint32_t vring_idx;
128 
129 	struct spdk_vhost_session *vsession;
130 
131 	struct spdk_interrupt *intr;
132 } __attribute((aligned(SPDK_CACHE_LINE_SIZE)));
133 
134 struct spdk_vhost_session {
135 	struct spdk_vhost_dev *vdev;
136 
137 	/* rte_vhost connection ID. */
138 	int vid;
139 
140 	/* Unique session ID. */
141 	uint64_t id;
142 	/* Unique session name. */
143 	char *name;
144 
145 	bool initialized;
146 	bool started;
147 	bool needs_restart;
148 	bool forced_polling;
149 	bool interrupt_mode;
150 
151 	struct rte_vhost_memory *mem;
152 
153 	int task_cnt;
154 
155 	uint16_t max_queues;
156 
157 	uint64_t negotiated_features;
158 
159 	/* Local copy of device coalescing settings. */
160 	uint32_t coalescing_delay_time_base;
161 	uint32_t coalescing_io_rate_threshold;
162 
163 	/* Next time when stats for event coalescing will be checked. */
164 	uint64_t next_stats_check_time;
165 
166 	/* Interval used for event coalescing checking. */
167 	uint64_t stats_check_interval;
168 
169 	struct spdk_vhost_virtqueue virtqueue[SPDK_VHOST_MAX_VQUEUES];
170 
171 	TAILQ_ENTRY(spdk_vhost_session) tailq;
172 };
173 
174 struct spdk_vhost_dev {
175 	char *name;
176 	char *path;
177 
178 	struct spdk_thread *thread;
179 	bool registered;
180 
181 	uint64_t virtio_features;
182 	uint64_t disabled_features;
183 	uint64_t protocol_features;
184 
185 	const struct spdk_vhost_dev_backend *backend;
186 
187 	/* Saved orginal values used to setup coalescing to avoid integer
188 	 * rounding issues during save/load config.
189 	 */
190 	uint32_t coalescing_delay_us;
191 	uint32_t coalescing_iops_threshold;
192 
193 	/* Current connections to the device */
194 	TAILQ_HEAD(, spdk_vhost_session) vsessions;
195 
196 	/* Increment-only session counter */
197 	uint64_t vsessions_num;
198 
199 	/* Number of started and actively polled sessions */
200 	uint32_t active_session_num;
201 
202 	/* Number of pending asynchronous operations */
203 	uint32_t pending_async_op_num;
204 
205 	TAILQ_ENTRY(spdk_vhost_dev) tailq;
206 };
207 
208 /**
209  * \param vdev vhost device.
210  * \param vsession vhost session.
211  * \param arg user-provided parameter.
212  *
213  * \return negative values will break the foreach call, meaning
214  * the function won't be called again. Return codes zero and
215  * positive don't have any effect.
216  */
217 typedef int (*spdk_vhost_session_fn)(struct spdk_vhost_dev *vdev,
218 				     struct spdk_vhost_session *vsession,
219 				     void *arg);
220 
221 /**
222  * \param vdev vhost device.
223  * \param arg user-provided parameter.
224  */
225 typedef void (*spdk_vhost_dev_fn)(struct spdk_vhost_dev *vdev, void *arg);
226 
227 struct spdk_vhost_dev_backend {
228 	/**
229 	 * Size of additional per-session context data
230 	 * allocated whenever a new client connects.
231 	 */
232 	size_t session_ctx_size;
233 
234 	int (*start_session)(struct spdk_vhost_session *vsession);
235 	int (*stop_session)(struct spdk_vhost_session *vsession);
236 
237 	int (*vhost_get_config)(struct spdk_vhost_dev *vdev, uint8_t *config, uint32_t len);
238 	int (*vhost_set_config)(struct spdk_vhost_dev *vdev, uint8_t *config,
239 				uint32_t offset, uint32_t size, uint32_t flags);
240 
241 	void (*dump_info_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
242 	void (*write_config_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
243 	int (*remove_device)(struct spdk_vhost_dev *vdev);
244 };
245 
246 void *vhost_gpa_to_vva(struct spdk_vhost_session *vsession, uint64_t addr, uint64_t len);
247 
248 uint16_t vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *vq, uint16_t *reqs,
249 				 uint16_t reqs_len);
250 
251 /**
252  * Get a virtio split descriptor at given index in given virtqueue.
253  * The descriptor will provide access to the entire descriptor
254  * chain. The subsequent descriptors are accesible via
255  * \c spdk_vhost_vring_desc_get_next.
256  * \param vsession vhost session
257  * \param vq virtqueue
258  * \param req_idx descriptor index
259  * \param desc pointer to be set to the descriptor
260  * \param desc_table descriptor table to be used with
261  * \c spdk_vhost_vring_desc_get_next. This might be either
262  * default virtqueue descriptor table or per-chain indirect
263  * table.
264  * \param desc_table_size size of the *desc_table*
265  * \return 0 on success, -1 if given index is invalid.
266  * If -1 is returned, the content of params is undefined.
267  */
268 int vhost_vq_get_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq,
269 		      uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table,
270 		      uint32_t *desc_table_size);
271 
272 /**
273  * Get a virtio packed descriptor at given index in given virtqueue.
274  * The descriptor will provide access to the entire descriptor
275  * chain. The subsequent descriptors are accesible via
276  * \c vhost_vring_packed_desc_get_next.
277  * \param vsession vhost session
278  * \param vq virtqueue
279  * \param req_idx descriptor index
280  * \param desc pointer to be set to the descriptor
281  * \param desc_table descriptor table to be used with
282  * \c spdk_vhost_vring_desc_get_next. This might be either
283  * \c NULL or per-chain indirect table.
284  * \param desc_table_size size of the *desc_table*
285  * \return 0 on success, -1 if given index is invalid.
286  * If -1 is returned, the content of params is undefined.
287  */
288 int vhost_vq_get_desc_packed(struct spdk_vhost_session *vsession,
289 			     struct spdk_vhost_virtqueue *virtqueue,
290 			     uint16_t req_idx, struct vring_packed_desc **desc,
291 			     struct vring_packed_desc **desc_table, uint32_t *desc_table_size);
292 
293 int vhost_inflight_queue_get_desc(struct spdk_vhost_session *vsession,
294 				  spdk_vhost_inflight_desc *desc_array,
295 				  uint16_t req_idx, spdk_vhost_inflight_desc **desc,
296 				  struct vring_packed_desc  **desc_table, uint32_t *desc_table_size);
297 
298 /**
299  * Send IRQ/call client (if pending) for \c vq.
300  * \param vsession vhost session
301  * \param vq virtqueue
302  * \return
303  *   0 - if no interrupt was signalled
304  *   1 - if interrupt was signalled
305  */
306 int vhost_vq_used_signal(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq);
307 
308 
309 /**
310  * Send IRQs for all queues that need to be signaled.
311  * \param vsession vhost session
312  * \param vq virtqueue
313  */
314 void vhost_session_used_signal(struct spdk_vhost_session *vsession);
315 
316 /**
317  * Send IRQs for the queue that need to be signaled.
318  * \param vq virtqueue
319  */
320 void vhost_session_vq_used_signal(struct spdk_vhost_virtqueue *virtqueue);
321 
322 void vhost_vq_used_ring_enqueue(struct spdk_vhost_session *vsession,
323 				struct spdk_vhost_virtqueue *vq,
324 				uint16_t id, uint32_t len);
325 
326 /**
327  * Enqueue the entry to the used ring when device complete the request.
328  * \param vsession vhost session
329  * \param vq virtqueue
330  * \req_idx descriptor index. It's the first index of this descriptor chain.
331  * \num_descs descriptor count. It's the count of the number of buffers in the chain.
332  * \buffer_id descriptor buffer ID.
333  * \length device write length. Specify the length of the buffer that has been initialized
334  * (written to) by the device
335  * \inflight_head the head idx of this IO inflight desc chain.
336  */
337 void vhost_vq_packed_ring_enqueue(struct spdk_vhost_session *vsession,
338 				  struct spdk_vhost_virtqueue *virtqueue,
339 				  uint16_t num_descs, uint16_t buffer_id,
340 				  uint32_t length, uint16_t inflight_head);
341 
342 /**
343  * Get subsequent descriptor from given table.
344  * \param desc current descriptor, will be set to the
345  * next descriptor (NULL in case this is the last
346  * descriptor in the chain or the next desc is invalid)
347  * \param desc_table descriptor table
348  * \param desc_table_size size of the *desc_table*
349  * \return 0 on success, -1 if given index is invalid
350  * The *desc* param will be set regardless of the
351  * return value.
352  */
353 int vhost_vring_desc_get_next(struct vring_desc **desc,
354 			      struct vring_desc *desc_table, uint32_t desc_table_size);
355 static inline bool
356 vhost_vring_desc_is_wr(struct vring_desc *cur_desc)
357 {
358 	return !!(cur_desc->flags & VRING_DESC_F_WRITE);
359 }
360 
361 int vhost_vring_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov,
362 			    uint16_t *iov_index, const struct vring_desc *desc);
363 
364 bool vhost_vq_packed_ring_is_avail(struct spdk_vhost_virtqueue *virtqueue);
365 
366 /**
367  * Get subsequent descriptor from vq or desc table.
368  * \param desc current descriptor, will be set to the
369  * next descriptor (NULL in case this is the last
370  * descriptor in the chain or the next desc is invalid)
371  * \req_idx index of current desc, will be set to the next
372  * index. If desc_table != NULL the req_idx is the the vring index
373  * or the req_idx is the desc_table index.
374  * \param desc_table descriptor table
375  * \param desc_table_size size of the *desc_table*
376  * \return 0 on success, -1 if given index is invalid
377  * The *desc* param will be set regardless of the
378  * return value.
379  */
380 int vhost_vring_packed_desc_get_next(struct vring_packed_desc **desc, uint16_t *req_idx,
381 				     struct spdk_vhost_virtqueue *vq,
382 				     struct vring_packed_desc *desc_table,
383 				     uint32_t desc_table_size);
384 
385 bool vhost_vring_packed_desc_is_wr(struct vring_packed_desc *cur_desc);
386 
387 int vhost_vring_packed_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov,
388 				   uint16_t *iov_index, const struct vring_packed_desc *desc);
389 
390 bool vhost_vring_inflight_desc_is_wr(spdk_vhost_inflight_desc *cur_desc);
391 
392 int vhost_vring_inflight_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov,
393 				     uint16_t *iov_index, const spdk_vhost_inflight_desc *desc);
394 
395 uint16_t vhost_vring_packed_desc_get_buffer_id(struct spdk_vhost_virtqueue *vq, uint16_t req_idx,
396 		uint16_t *num_descs);
397 
398 static inline bool __attribute__((always_inline))
399 vhost_dev_has_feature(struct spdk_vhost_session *vsession, unsigned feature_id)
400 {
401 	return vsession->negotiated_features & (1ULL << feature_id);
402 }
403 
404 int vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str,
405 		       const struct spdk_vhost_dev_backend *backend);
406 int vhost_dev_unregister(struct spdk_vhost_dev *vdev);
407 
408 void vhost_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
409 
410 /*
411  * Vhost callbacks for vhost_device_ops interface
412  */
413 
414 int vhost_new_connection_cb(int vid, const char *ifname);
415 int vhost_start_device_cb(int vid);
416 int vhost_stop_device_cb(int vid);
417 int vhost_destroy_connection_cb(int vid);
418 
419 /*
420  * Memory registration functions used in start/stop device callbacks
421  */
422 void vhost_session_mem_register(struct rte_vhost_memory *mem);
423 void vhost_session_mem_unregister(struct rte_vhost_memory *mem);
424 
425 /*
426  * Call a function for each session of the provided vhost device.
427  * The function will be called one-by-one on each session's thread.
428  *
429  * \param vdev vhost device
430  * \param fn function to call on each session's thread
431  * \param cpl_fn function to be called at the end of the iteration on
432  * the vhost management thread.
433  * Optional, can be NULL.
434  * \param arg additional argument to the both callbacks
435  */
436 void vhost_dev_foreach_session(struct spdk_vhost_dev *dev,
437 			       spdk_vhost_session_fn fn,
438 			       spdk_vhost_dev_fn cpl_fn,
439 			       void *arg);
440 
441 /**
442  * Call a function on the provided lcore and block until either
443  * spdk_vhost_session_start_done() or spdk_vhost_session_stop_done()
444  * is called.
445  *
446  * This must be called under the global vhost mutex, which this function
447  * will unlock for the time it's waiting. It's meant to be called only
448  * from start/stop session callbacks.
449  *
450  * \param vsession vhost session
451  * \param cb_fn the function to call. The void *arg parameter in cb_fn
452  * is always NULL.
453  * \param timeout_sec timeout in seconds. This function will still
454  * block after the timeout expires, but will print the provided errmsg.
455  * \param errmsg error message to print once the timeout expires
456  * \return return the code passed to spdk_vhost_session_event_done().
457  */
458 int vhost_session_send_event(struct spdk_vhost_session *vsession,
459 			     spdk_vhost_session_fn cb_fn, unsigned timeout_sec,
460 			     const char *errmsg);
461 
462 /**
463  * Finish a blocking spdk_vhost_session_send_event() call and finally
464  * start the session. This must be called on the target lcore, which
465  * will now receive all session-related messages (e.g. from
466  * spdk_vhost_dev_foreach_session()).
467  *
468  * Must be called under the global vhost lock.
469  *
470  * \param vsession vhost session
471  * \param response return code
472  */
473 void vhost_session_start_done(struct spdk_vhost_session *vsession, int response);
474 
475 /**
476  * Finish a blocking spdk_vhost_session_send_event() call and finally
477  * stop the session. This must be called on the session's lcore which
478  * used to receive all session-related messages (e.g. from
479  * spdk_vhost_dev_foreach_session()). After this call, the session-
480  * related messages will be once again processed by any arbitrary thread.
481  *
482  * Must be called under the global vhost lock.
483  *
484  * Must be called under the global vhost mutex.
485  *
486  * \param vsession vhost session
487  * \param response return code
488  */
489 void vhost_session_stop_done(struct spdk_vhost_session *vsession, int response);
490 
491 struct spdk_vhost_session *vhost_session_find_by_vid(int vid);
492 void vhost_session_install_rte_compat_hooks(struct spdk_vhost_session *vsession);
493 int vhost_register_unix_socket(const char *path, const char *ctrl_name,
494 			       uint64_t virtio_features, uint64_t disabled_features, uint64_t protocol_features);
495 int vhost_driver_unregister(const char *path);
496 int vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
497 int vhost_get_negotiated_features(int vid, uint64_t *negotiated_features);
498 
499 int remove_vhost_controller(struct spdk_vhost_dev *vdev);
500 
501 #endif /* SPDK_VHOST_INTERNAL_H */
502