xref: /spdk/lib/vhost/vhost_internal.h (revision 9e2eb8cb51a16839a54771eb842c4e3aa99b25d6)
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 
37 #include "spdk/stdinc.h"
38 
39 #include <rte_vhost.h>
40 
41 #include "spdk_internal/log.h"
42 #include "spdk/event.h"
43 #include "spdk/rpc.h"
44 #include "spdk/config.h"
45 
46 #define SPDK_CACHE_LINE_SIZE RTE_CACHE_LINE_SIZE
47 
48 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
49 #define VHOST_USER_F_PROTOCOL_FEATURES	30
50 #endif
51 
52 #ifndef VIRTIO_F_VERSION_1
53 #define VIRTIO_F_VERSION_1 32
54 #endif
55 
56 #ifndef VIRTIO_BLK_F_MQ
57 #define VIRTIO_BLK_F_MQ		12	/* support more than one vq */
58 #endif
59 
60 #ifndef VIRTIO_BLK_F_CONFIG_WCE
61 #define VIRTIO_BLK_F_CONFIG_WCE	11
62 #endif
63 
64 #define SPDK_VHOST_MAX_VQUEUES	256
65 #define SPDK_VHOST_MAX_VQ_SIZE	1024
66 
67 #define SPDK_VHOST_SCSI_CTRLR_MAX_DEVS 8
68 
69 #define SPDK_VHOST_IOVS_MAX 129
70 
71 /*
72  * Rate at which stats are checked for interrupt coalescing.
73  */
74 #define SPDK_VHOST_STATS_CHECK_INTERVAL_MS 10
75 /*
76  * Default threshold at which interrupts start to be coalesced.
77  */
78 #define SPDK_VHOST_VQ_IOPS_COALESCING_THRESHOLD 60000
79 
80 /*
81  * Currently coalescing is not used by default.
82  * Setting this to value > 0 here or by RPC will enable coalescing.
83  */
84 #define SPDK_VHOST_COALESCING_DELAY_BASE_US 0
85 
86 
87 #define SPDK_VHOST_FEATURES ((1ULL << VHOST_F_LOG_ALL) | \
88 	(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
89 	(1ULL << VIRTIO_F_VERSION_1) | \
90 	(1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
91 	(1ULL << VIRTIO_RING_F_EVENT_IDX) | \
92 	(1ULL << VIRTIO_RING_F_INDIRECT_DESC))
93 
94 #define SPDK_VHOST_DISABLED_FEATURES ((1ULL << VIRTIO_RING_F_EVENT_IDX) | \
95 	(1ULL << VIRTIO_F_NOTIFY_ON_EMPTY))
96 
97 struct spdk_vhost_virtqueue {
98 	struct rte_vhost_vring vring;
99 	uint16_t last_avail_idx;
100 	uint16_t last_used_idx;
101 
102 	void *tasks;
103 
104 	/* Request count from last stats check */
105 	uint32_t req_cnt;
106 
107 	/* Request count from last event */
108 	uint16_t used_req_cnt;
109 
110 	/* How long interrupt is delayed */
111 	uint32_t irq_delay_time;
112 
113 	/* Next time when we need to send event */
114 	uint64_t next_event_time;
115 
116 } __attribute((aligned(SPDK_CACHE_LINE_SIZE)));
117 
118 struct spdk_vhost_session {
119 	struct spdk_vhost_dev *vdev;
120 
121 	/* rte_vhost connection ID. */
122 	int vid;
123 
124 	/* Unique session ID. */
125 	unsigned id;
126 
127 	int32_t lcore;
128 
129 	bool initialized;
130 	bool needs_restart;
131 	bool forced_polling;
132 
133 	struct rte_vhost_memory *mem;
134 
135 	int task_cnt;
136 
137 	uint16_t max_queues;
138 
139 	uint64_t negotiated_features;
140 
141 	/* Local copy of device coalescing settings. */
142 	uint32_t coalescing_delay_time_base;
143 	uint32_t coalescing_io_rate_threshold;
144 
145 	/* Next time when stats for event coalescing will be checked. */
146 	uint64_t next_stats_check_time;
147 
148 	/* Interval used for event coalescing checking. */
149 	uint64_t stats_check_interval;
150 
151 	struct spdk_vhost_virtqueue virtqueue[SPDK_VHOST_MAX_VQUEUES];
152 
153 	TAILQ_ENTRY(spdk_vhost_session) tailq;
154 
155 	struct spdk_vhost_session_fn_ctx *event_ctx;
156 };
157 
158 struct spdk_vhost_dev {
159 	char *name;
160 	char *path;
161 
162 	struct spdk_cpuset *cpumask;
163 	bool registered;
164 
165 	const struct spdk_vhost_dev_backend *backend;
166 
167 	/* Saved orginal values used to setup coalescing to avoid integer
168 	 * rounding issues during save/load config.
169 	 */
170 	uint32_t coalescing_delay_us;
171 	uint32_t coalescing_iops_threshold;
172 
173 	/* Current connections to the device */
174 	TAILQ_HEAD(, spdk_vhost_session) vsessions;
175 
176 	/* Increment-only session counter */
177 	uint64_t vsessions_num;
178 
179 	/* Number of started and actively polled sessions */
180 	uint32_t active_session_num;
181 
182 	/* Number of pending asynchronous operations */
183 	uint32_t pending_async_op_num;
184 
185 	TAILQ_ENTRY(spdk_vhost_dev) tailq;
186 };
187 
188 /**
189  * Synchronized vhost session event used for backend callbacks.
190  *
191  * \param vdev vhost device. If the device has been deleted
192  * in the meantime, this function will be called one last
193  * time with vdev == NULL.
194  * \param vsession vhost session. If all sessions have been
195  * iterated through, this function will be called one last
196  * time with vsession == NULL.
197  * \param arg user-provided parameter.
198  *
199  * \return negative values will break the foreach call, meaning
200  * the function won't be called again. Return codes zero and
201  * positive don't have any effect.
202  */
203 typedef int (*spdk_vhost_session_fn)(struct spdk_vhost_dev *vdev,
204 				     struct spdk_vhost_session *vsession,
205 				     void *arg);
206 
207 struct spdk_vhost_dev_backend {
208 	uint64_t virtio_features;
209 	uint64_t disabled_features;
210 
211 	/**
212 	 * Size of additional per-session context data
213 	 * allocated whenever a new client connects.
214 	 */
215 	size_t session_ctx_size;
216 
217 	int (*start_session)(struct spdk_vhost_session *vsession);
218 	int (*stop_session)(struct spdk_vhost_session *vsession);
219 
220 	int (*vhost_get_config)(struct spdk_vhost_dev *vdev, uint8_t *config, uint32_t len);
221 	int (*vhost_set_config)(struct spdk_vhost_dev *vdev, uint8_t *config,
222 				uint32_t offset, uint32_t size, uint32_t flags);
223 
224 	void (*dump_info_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
225 	void (*write_config_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
226 	int (*remove_device)(struct spdk_vhost_dev *vdev);
227 };
228 
229 void *spdk_vhost_gpa_to_vva(struct spdk_vhost_session *vsession, uint64_t addr, uint64_t len);
230 
231 uint16_t spdk_vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *vq, uint16_t *reqs,
232 				      uint16_t reqs_len);
233 
234 /**
235  * Get a virtio descriptor at given index in given virtqueue.
236  * The descriptor will provide access to the entire descriptor
237  * chain. The subsequent descriptors are accesible via
238  * \c spdk_vhost_vring_desc_get_next.
239  * \param vsession vhost session
240  * \param vq virtqueue
241  * \param req_idx descriptor index
242  * \param desc pointer to be set to the descriptor
243  * \param desc_table descriptor table to be used with
244  * \c spdk_vhost_vring_desc_get_next. This might be either
245  * default virtqueue descriptor table or per-chain indirect
246  * table.
247  * \param desc_table_size size of the *desc_table*
248  * \return 0 on success, -1 if given index is invalid.
249  * If -1 is returned, the content of params is undefined.
250  */
251 int spdk_vhost_vq_get_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq,
252 			   uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table,
253 			   uint32_t *desc_table_size);
254 
255 /**
256  * Send IRQ/call client (if pending) for \c vq.
257  * \param vsession vhost session
258  * \param vq virtqueue
259  * \return
260  *   0 - if no interrupt was signalled
261  *   1 - if interrupt was signalled
262  */
263 int spdk_vhost_vq_used_signal(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq);
264 
265 
266 /**
267  * Send IRQs for all queues that need to be signaled.
268  * \param vsession vhost session
269  * \param vq virtqueue
270  */
271 void spdk_vhost_session_used_signal(struct spdk_vhost_session *vsession);
272 
273 void spdk_vhost_vq_used_ring_enqueue(struct spdk_vhost_session *vsession,
274 				     struct spdk_vhost_virtqueue *vq,
275 				     uint16_t id, uint32_t len);
276 
277 /**
278  * Get subsequent descriptor from given table.
279  * \param desc current descriptor, will be set to the
280  * next descriptor (NULL in case this is the last
281  * descriptor in the chain or the next desc is invalid)
282  * \param desc_table descriptor table
283  * \param desc_table_size size of the *desc_table*
284  * \return 0 on success, -1 if given index is invalid
285  * The *desc* param will be set regardless of the
286  * return value.
287  */
288 int spdk_vhost_vring_desc_get_next(struct vring_desc **desc,
289 				   struct vring_desc *desc_table, uint32_t desc_table_size);
290 bool spdk_vhost_vring_desc_is_wr(struct vring_desc *cur_desc);
291 
292 int spdk_vhost_vring_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov,
293 				 uint16_t *iov_index, const struct vring_desc *desc);
294 
295 static inline bool __attribute__((always_inline))
296 spdk_vhost_dev_has_feature(struct spdk_vhost_session *vsession, unsigned feature_id)
297 {
298 	return vsession->negotiated_features & (1ULL << feature_id);
299 }
300 
301 int spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str,
302 			    const struct spdk_vhost_dev_backend *backend);
303 int spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev);
304 
305 int spdk_vhost_scsi_controller_construct(void);
306 int spdk_vhost_blk_controller_construct(void);
307 void spdk_vhost_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
308 
309 /*
310  * Call function for each active session on the provided
311  * vhost device. The function will be called one-by-one
312  * on each session's thread.
313  *
314  * \param vdev vhost device
315  * \param fn function to call
316  * \param arg additional argument to \c fn
317  */
318 void spdk_vhost_dev_foreach_session(struct spdk_vhost_dev *dev,
319 				    spdk_vhost_session_fn fn, void *arg);
320 
321 /**
322  * Call a function on the provided lcore and block until either
323  * spdk_vhost_session_start_done() or spdk_vhost_session_stop_done()
324  * is called.
325  *
326  * This must be called under the global vhost mutex, which this function
327  * will unlock for the time it's waiting. It's meant to be called only
328  * from start/stop session callbacks.
329  *
330  * \param lcore target session's lcore
331  * \param vsession vhost session
332  * \param cb_fn the function to call. The void *arg parameter in cb_fn
333  * is always NULL.
334  * \param timeout_sec timeout in seconds. This function will still
335  * block after the timeout expires, but will print the provided errmsg.
336  * \param errmsg error message to print once the timeout expires
337  * \return return the code passed to spdk_vhost_session_event_done().
338  */
339 int spdk_vhost_session_send_event(int32_t lcore, struct spdk_vhost_session *vsession,
340 				  spdk_vhost_session_fn cb_fn, unsigned timeout_sec,
341 				  const char *errmsg);
342 
343 /**
344  * Finish a blocking spdk_vhost_session_send_event() call and finally
345  * start the session. This must be called on the target lcore, which
346  * will now receive all session-related messages (e.g. from
347  * spdk_vhost_dev_foreach_session()).
348  *
349  * Must be called under the global vhost lock.
350  *
351  * \param vsession vhost session
352  * \param response return code
353  */
354 void spdk_vhost_session_start_done(struct spdk_vhost_session *vsession, int response);
355 
356 /**
357  * Finish a blocking spdk_vhost_session_send_event() call and finally
358  * stop the session. This must be called on the session's lcore which
359  * used to receive all session-related messages (e.g. from
360  * spdk_vhost_dev_foreach_session()). After this call, the session-
361  * related messages will be once again processed by any arbitrary thread.
362  *
363  * Must be called under the global vhost lock.
364  *
365  * Must be called under the global vhost mutex.
366  *
367  * \param vsession vhost session
368  * \param response return code
369  */
370 void spdk_vhost_session_stop_done(struct spdk_vhost_session *vsession, int response);
371 
372 struct spdk_vhost_session *spdk_vhost_session_find_by_vid(int vid);
373 void spdk_vhost_session_install_rte_compat_hooks(struct spdk_vhost_session *vsession);
374 void spdk_vhost_dev_install_rte_compat_hooks(struct spdk_vhost_dev *vdev);
375 
376 void spdk_vhost_free_reactor(uint32_t lcore);
377 uint32_t spdk_vhost_allocate_reactor(struct spdk_cpuset *cpumask);
378 
379 int spdk_remove_vhost_controller(struct spdk_vhost_dev *vdev);
380 
381 #ifdef SPDK_CONFIG_VHOST_INTERNAL_LIB
382 int spdk_vhost_nvme_admin_passthrough(int vid, void *cmd, void *cqe, void *buf);
383 int spdk_vhost_nvme_set_cq_call(int vid, uint16_t qid, int fd);
384 int spdk_vhost_nvme_set_bar_mr(int vid, void *bar_addr, uint64_t bar_size);
385 int spdk_vhost_nvme_get_cap(int vid, uint64_t *cap);
386 int spdk_vhost_nvme_controller_construct(void);
387 int spdk_vhost_nvme_dev_construct(const char *name, const char *cpumask, uint32_t io_queues);
388 int spdk_vhost_nvme_dev_remove(struct spdk_vhost_dev *vdev);
389 int spdk_vhost_nvme_dev_add_ns(struct spdk_vhost_dev *vdev,
390 			       const char *bdev_name);
391 #endif
392 
393 #endif /* SPDK_VHOST_INTERNAL_H */
394