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