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