xref: /spdk/lib/vhost/vhost_internal.h (revision c4d9daeb7bf491bc0eb6e8d417b75d44773cb009)
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 started;
131  	bool needs_restart;
132  	bool forced_polling;
133  
134  	struct rte_vhost_memory *mem;
135  
136  	int task_cnt;
137  
138  	uint16_t max_queues;
139  
140  	uint64_t negotiated_features;
141  
142  	/* Local copy of device coalescing settings. */
143  	uint32_t coalescing_delay_time_base;
144  	uint32_t coalescing_io_rate_threshold;
145  
146  	/* Next time when stats for event coalescing will be checked. */
147  	uint64_t next_stats_check_time;
148  
149  	/* Interval used for event coalescing checking. */
150  	uint64_t stats_check_interval;
151  
152  	struct spdk_vhost_virtqueue virtqueue[SPDK_VHOST_MAX_VQUEUES];
153  
154  	TAILQ_ENTRY(spdk_vhost_session) tailq;
155  
156  	struct spdk_vhost_session_fn_ctx *event_ctx;
157  };
158  
159  struct spdk_vhost_dev {
160  	char *name;
161  	char *path;
162  
163  	struct spdk_cpuset *cpumask;
164  	bool registered;
165  
166  	const struct spdk_vhost_dev_backend *backend;
167  
168  	/* Saved orginal values used to setup coalescing to avoid integer
169  	 * rounding issues during save/load config.
170  	 */
171  	uint32_t coalescing_delay_us;
172  	uint32_t coalescing_iops_threshold;
173  
174  	/* Current connections to the device */
175  	TAILQ_HEAD(, spdk_vhost_session) vsessions;
176  
177  	/* Increment-only session counter */
178  	uint64_t vsessions_num;
179  
180  	/* Number of started and actively polled sessions */
181  	uint32_t active_session_num;
182  
183  	/* Number of pending asynchronous operations */
184  	uint32_t pending_async_op_num;
185  
186  	TAILQ_ENTRY(spdk_vhost_dev) tailq;
187  };
188  
189  /**
190   * Synchronized vhost session event used for backend callbacks.
191   *
192   * \param vdev vhost device. If the device has been deleted
193   * in the meantime, this function will be called one last
194   * time with vdev == NULL.
195   * \param vsession vhost session. If all sessions have been
196   * iterated through, this function will be called one last
197   * time with vsession == NULL.
198   * \param arg user-provided parameter.
199   *
200   * \return negative values will break the foreach call, meaning
201   * the function won't be called again. Return codes zero and
202   * positive don't have any effect.
203   */
204  typedef int (*spdk_vhost_session_fn)(struct spdk_vhost_dev *vdev,
205  				     struct spdk_vhost_session *vsession,
206  				     void *arg);
207  
208  struct spdk_vhost_dev_backend {
209  	uint64_t virtio_features;
210  	uint64_t disabled_features;
211  
212  	/**
213  	 * Size of additional per-session context data
214  	 * allocated whenever a new client connects.
215  	 */
216  	size_t session_ctx_size;
217  
218  	int (*start_session)(struct spdk_vhost_session *vsession);
219  	int (*stop_session)(struct spdk_vhost_session *vsession);
220  
221  	int (*vhost_get_config)(struct spdk_vhost_dev *vdev, uint8_t *config, uint32_t len);
222  	int (*vhost_set_config)(struct spdk_vhost_dev *vdev, uint8_t *config,
223  				uint32_t offset, uint32_t size, uint32_t flags);
224  
225  	void (*dump_info_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
226  	void (*write_config_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
227  	int (*remove_device)(struct spdk_vhost_dev *vdev);
228  };
229  
230  void *spdk_vhost_gpa_to_vva(struct spdk_vhost_session *vsession, uint64_t addr, uint64_t len);
231  
232  uint16_t spdk_vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *vq, uint16_t *reqs,
233  				      uint16_t reqs_len);
234  
235  /**
236   * Get a virtio descriptor at given index in given virtqueue.
237   * The descriptor will provide access to the entire descriptor
238   * chain. The subsequent descriptors are accesible via
239   * \c spdk_vhost_vring_desc_get_next.
240   * \param vsession vhost session
241   * \param vq virtqueue
242   * \param req_idx descriptor index
243   * \param desc pointer to be set to the descriptor
244   * \param desc_table descriptor table to be used with
245   * \c spdk_vhost_vring_desc_get_next. This might be either
246   * default virtqueue descriptor table or per-chain indirect
247   * table.
248   * \param desc_table_size size of the *desc_table*
249   * \return 0 on success, -1 if given index is invalid.
250   * If -1 is returned, the content of params is undefined.
251   */
252  int spdk_vhost_vq_get_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq,
253  			   uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table,
254  			   uint32_t *desc_table_size);
255  
256  /**
257   * Send IRQ/call client (if pending) for \c vq.
258   * \param vsession vhost session
259   * \param vq virtqueue
260   * \return
261   *   0 - if no interrupt was signalled
262   *   1 - if interrupt was signalled
263   */
264  int spdk_vhost_vq_used_signal(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq);
265  
266  
267  /**
268   * Send IRQs for all queues that need to be signaled.
269   * \param vsession vhost session
270   * \param vq virtqueue
271   */
272  void spdk_vhost_session_used_signal(struct spdk_vhost_session *vsession);
273  
274  void spdk_vhost_vq_used_ring_enqueue(struct spdk_vhost_session *vsession,
275  				     struct spdk_vhost_virtqueue *vq,
276  				     uint16_t id, uint32_t len);
277  
278  /**
279   * Get subsequent descriptor from given table.
280   * \param desc current descriptor, will be set to the
281   * next descriptor (NULL in case this is the last
282   * descriptor in the chain or the next desc is invalid)
283   * \param desc_table descriptor table
284   * \param desc_table_size size of the *desc_table*
285   * \return 0 on success, -1 if given index is invalid
286   * The *desc* param will be set regardless of the
287   * return value.
288   */
289  int spdk_vhost_vring_desc_get_next(struct vring_desc **desc,
290  				   struct vring_desc *desc_table, uint32_t desc_table_size);
291  bool spdk_vhost_vring_desc_is_wr(struct vring_desc *cur_desc);
292  
293  int spdk_vhost_vring_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov,
294  				 uint16_t *iov_index, const struct vring_desc *desc);
295  
296  static inline bool __attribute__((always_inline))
297  spdk_vhost_dev_has_feature(struct spdk_vhost_session *vsession, unsigned feature_id)
298  {
299  	return vsession->negotiated_features & (1ULL << feature_id);
300  }
301  
302  int spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str,
303  			    const struct spdk_vhost_dev_backend *backend);
304  int spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev);
305  
306  int spdk_vhost_scsi_controller_construct(void);
307  int spdk_vhost_blk_controller_construct(void);
308  void spdk_vhost_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w);
309  
310  /*
311   * Call function for each active session on the provided
312   * vhost device. The function will be called one-by-one
313   * on each session's thread.
314   *
315   * \param vdev vhost device
316   * \param fn function to call
317   * \param arg additional argument to \c fn
318   */
319  void spdk_vhost_dev_foreach_session(struct spdk_vhost_dev *dev,
320  				    spdk_vhost_session_fn fn, void *arg);
321  
322  /**
323   * Call a function on the provided lcore and block until either
324   * spdk_vhost_session_start_done() or spdk_vhost_session_stop_done()
325   * is called.
326   *
327   * This must be called under the global vhost mutex, which this function
328   * will unlock for the time it's waiting. It's meant to be called only
329   * from start/stop session callbacks.
330   *
331   * \param lcore target session's lcore
332   * \param vsession vhost session
333   * \param cb_fn the function to call. The void *arg parameter in cb_fn
334   * is always NULL.
335   * \param timeout_sec timeout in seconds. This function will still
336   * block after the timeout expires, but will print the provided errmsg.
337   * \param errmsg error message to print once the timeout expires
338   * \return return the code passed to spdk_vhost_session_event_done().
339   */
340  int spdk_vhost_session_send_event(int32_t lcore, struct spdk_vhost_session *vsession,
341  				  spdk_vhost_session_fn cb_fn, unsigned timeout_sec,
342  				  const char *errmsg);
343  
344  /**
345   * Finish a blocking spdk_vhost_session_send_event() call and finally
346   * start the session. This must be called on the target lcore, which
347   * will now receive all session-related messages (e.g. from
348   * spdk_vhost_dev_foreach_session()).
349   *
350   * Must be called under the global vhost lock.
351   *
352   * \param vsession vhost session
353   * \param response return code
354   */
355  void spdk_vhost_session_start_done(struct spdk_vhost_session *vsession, int response);
356  
357  /**
358   * Finish a blocking spdk_vhost_session_send_event() call and finally
359   * stop the session. This must be called on the session's lcore which
360   * used to receive all session-related messages (e.g. from
361   * spdk_vhost_dev_foreach_session()). After this call, the session-
362   * related messages will be once again processed by any arbitrary thread.
363   *
364   * Must be called under the global vhost lock.
365   *
366   * Must be called under the global vhost mutex.
367   *
368   * \param vsession vhost session
369   * \param response return code
370   */
371  void spdk_vhost_session_stop_done(struct spdk_vhost_session *vsession, int response);
372  
373  struct spdk_vhost_session *spdk_vhost_session_find_by_vid(int vid);
374  void spdk_vhost_session_install_rte_compat_hooks(struct spdk_vhost_session *vsession);
375  void spdk_vhost_dev_install_rte_compat_hooks(struct spdk_vhost_dev *vdev);
376  
377  void spdk_vhost_free_reactor(uint32_t lcore);
378  uint32_t spdk_vhost_allocate_reactor(struct spdk_cpuset *cpumask);
379  
380  int spdk_remove_vhost_controller(struct spdk_vhost_dev *vdev);
381  
382  #ifdef SPDK_CONFIG_VHOST_INTERNAL_LIB
383  int spdk_vhost_nvme_admin_passthrough(int vid, void *cmd, void *cqe, void *buf);
384  int spdk_vhost_nvme_set_cq_call(int vid, uint16_t qid, int fd);
385  int spdk_vhost_nvme_set_bar_mr(int vid, void *bar_addr, uint64_t bar_size);
386  int spdk_vhost_nvme_get_cap(int vid, uint64_t *cap);
387  int spdk_vhost_nvme_controller_construct(void);
388  int spdk_vhost_nvme_dev_construct(const char *name, const char *cpumask, uint32_t io_queues);
389  int spdk_vhost_nvme_dev_remove(struct spdk_vhost_dev *vdev);
390  int spdk_vhost_nvme_dev_add_ns(struct spdk_vhost_dev *vdev,
391  			       const char *bdev_name);
392  #endif
393  
394  #endif /* SPDK_VHOST_INTERNAL_H */
395