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 vhost_poll_group { 98 struct spdk_thread *thread; 99 unsigned ref; 100 TAILQ_ENTRY(vhost_poll_group) tailq; 101 }; 102 103 struct spdk_vhost_virtqueue { 104 struct rte_vhost_vring vring; 105 uint16_t last_avail_idx; 106 uint16_t last_used_idx; 107 108 void *tasks; 109 110 /* Request count from last stats check */ 111 uint32_t req_cnt; 112 113 /* Request count from last event */ 114 uint16_t used_req_cnt; 115 116 /* How long interrupt is delayed */ 117 uint32_t irq_delay_time; 118 119 /* Next time when we need to send event */ 120 uint64_t next_event_time; 121 122 /* Associated vhost_virtqueue in the virtio device's virtqueue list */ 123 uint32_t vring_idx; 124 } __attribute((aligned(SPDK_CACHE_LINE_SIZE))); 125 126 struct spdk_vhost_session { 127 struct spdk_vhost_dev *vdev; 128 129 /* rte_vhost connection ID. */ 130 int vid; 131 132 /* Unique session ID. */ 133 uint64_t id; 134 /* Unique session name. */ 135 char *name; 136 137 struct vhost_poll_group *poll_group; 138 139 bool initialized; 140 bool started; 141 bool needs_restart; 142 bool forced_polling; 143 144 struct rte_vhost_memory *mem; 145 146 int task_cnt; 147 148 uint16_t max_queues; 149 150 uint64_t negotiated_features; 151 152 /* Local copy of device coalescing settings. */ 153 uint32_t coalescing_delay_time_base; 154 uint32_t coalescing_io_rate_threshold; 155 156 /* Next time when stats for event coalescing will be checked. */ 157 uint64_t next_stats_check_time; 158 159 /* Interval used for event coalescing checking. */ 160 uint64_t stats_check_interval; 161 162 struct spdk_vhost_virtqueue virtqueue[SPDK_VHOST_MAX_VQUEUES]; 163 164 TAILQ_ENTRY(spdk_vhost_session) tailq; 165 }; 166 167 struct spdk_vhost_dev { 168 char *name; 169 char *path; 170 171 struct spdk_cpuset *cpumask; 172 bool registered; 173 174 const struct spdk_vhost_dev_backend *backend; 175 176 /* Saved orginal values used to setup coalescing to avoid integer 177 * rounding issues during save/load config. 178 */ 179 uint32_t coalescing_delay_us; 180 uint32_t coalescing_iops_threshold; 181 182 /* Current connections to the device */ 183 TAILQ_HEAD(, spdk_vhost_session) vsessions; 184 185 /* Increment-only session counter */ 186 uint64_t vsessions_num; 187 188 /* Number of started and actively polled sessions */ 189 uint32_t active_session_num; 190 191 /* Number of pending asynchronous operations */ 192 uint32_t pending_async_op_num; 193 194 TAILQ_ENTRY(spdk_vhost_dev) tailq; 195 }; 196 197 /** 198 * \param vdev vhost device. 199 * \param vsession vhost session. 200 * \param arg user-provided parameter. 201 * 202 * \return negative values will break the foreach call, meaning 203 * the function won't be called again. Return codes zero and 204 * positive don't have any effect. 205 */ 206 typedef int (*spdk_vhost_session_fn)(struct spdk_vhost_dev *vdev, 207 struct spdk_vhost_session *vsession, 208 void *arg); 209 210 /** 211 * \param vdev vhost device. 212 * \param arg user-provided parameter. 213 */ 214 typedef void (*spdk_vhost_dev_fn)(struct spdk_vhost_dev *vdev, void *arg); 215 216 struct spdk_vhost_dev_backend { 217 uint64_t virtio_features; 218 uint64_t disabled_features; 219 220 /** 221 * Size of additional per-session context data 222 * allocated whenever a new client connects. 223 */ 224 size_t session_ctx_size; 225 226 int (*start_session)(struct spdk_vhost_session *vsession); 227 int (*stop_session)(struct spdk_vhost_session *vsession); 228 229 int (*vhost_get_config)(struct spdk_vhost_dev *vdev, uint8_t *config, uint32_t len); 230 int (*vhost_set_config)(struct spdk_vhost_dev *vdev, uint8_t *config, 231 uint32_t offset, uint32_t size, uint32_t flags); 232 233 void (*dump_info_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 234 void (*write_config_json)(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 235 int (*remove_device)(struct spdk_vhost_dev *vdev); 236 }; 237 238 void *vhost_gpa_to_vva(struct spdk_vhost_session *vsession, uint64_t addr, uint64_t len); 239 240 uint16_t vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *vq, uint16_t *reqs, 241 uint16_t reqs_len); 242 243 /** 244 * Get a virtio descriptor at given index in given virtqueue. 245 * The descriptor will provide access to the entire descriptor 246 * chain. The subsequent descriptors are accesible via 247 * \c spdk_vhost_vring_desc_get_next. 248 * \param vsession vhost session 249 * \param vq virtqueue 250 * \param req_idx descriptor index 251 * \param desc pointer to be set to the descriptor 252 * \param desc_table descriptor table to be used with 253 * \c spdk_vhost_vring_desc_get_next. This might be either 254 * default virtqueue descriptor table or per-chain indirect 255 * table. 256 * \param desc_table_size size of the *desc_table* 257 * \return 0 on success, -1 if given index is invalid. 258 * If -1 is returned, the content of params is undefined. 259 */ 260 int vhost_vq_get_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq, 261 uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table, 262 uint32_t *desc_table_size); 263 264 /** 265 * Send IRQ/call client (if pending) for \c vq. 266 * \param vsession vhost session 267 * \param vq virtqueue 268 * \return 269 * 0 - if no interrupt was signalled 270 * 1 - if interrupt was signalled 271 */ 272 int vhost_vq_used_signal(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *vq); 273 274 275 /** 276 * Send IRQs for all queues that need to be signaled. 277 * \param vsession vhost session 278 * \param vq virtqueue 279 */ 280 void vhost_session_used_signal(struct spdk_vhost_session *vsession); 281 282 void vhost_vq_used_ring_enqueue(struct spdk_vhost_session *vsession, 283 struct spdk_vhost_virtqueue *vq, 284 uint16_t id, uint32_t len); 285 286 /** 287 * Get subsequent descriptor from given table. 288 * \param desc current descriptor, will be set to the 289 * next descriptor (NULL in case this is the last 290 * descriptor in the chain or the next desc is invalid) 291 * \param desc_table descriptor table 292 * \param desc_table_size size of the *desc_table* 293 * \return 0 on success, -1 if given index is invalid 294 * The *desc* param will be set regardless of the 295 * return value. 296 */ 297 int vhost_vring_desc_get_next(struct vring_desc **desc, 298 struct vring_desc *desc_table, uint32_t desc_table_size); 299 bool vhost_vring_desc_is_wr(struct vring_desc *cur_desc); 300 301 int vhost_vring_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov, 302 uint16_t *iov_index, const struct vring_desc *desc); 303 304 static inline bool __attribute__((always_inline)) 305 vhost_dev_has_feature(struct spdk_vhost_session *vsession, unsigned feature_id) 306 { 307 return vsession->negotiated_features & (1ULL << feature_id); 308 } 309 310 int vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, 311 const struct spdk_vhost_dev_backend *backend); 312 int vhost_dev_unregister(struct spdk_vhost_dev *vdev); 313 314 int vhost_scsi_controller_construct(void); 315 int vhost_blk_controller_construct(void); 316 void vhost_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); 317 318 /* 319 * Call a function for each session of the provided vhost device. 320 * The function will be called one-by-one on each session's thread. 321 * 322 * \param vdev vhost device 323 * \param fn function to call on each session's thread 324 * \param cpl_fn function to be called at the end of the iteration on 325 * the vhost management thread. 326 * Optional, can be NULL. 327 * \param arg additional argument to the both callbacks 328 */ 329 void vhost_dev_foreach_session(struct spdk_vhost_dev *dev, 330 spdk_vhost_session_fn fn, 331 spdk_vhost_dev_fn cpl_fn, 332 void *arg); 333 334 /** 335 * Call a function on the provided lcore and block until either 336 * spdk_vhost_session_start_done() or spdk_vhost_session_stop_done() 337 * is called. 338 * 339 * This must be called under the global vhost mutex, which this function 340 * will unlock for the time it's waiting. It's meant to be called only 341 * from start/stop session callbacks. 342 * 343 * \param pg designated session's poll group 344 * \param vsession vhost session 345 * \param cb_fn the function to call. The void *arg parameter in cb_fn 346 * is always NULL. 347 * \param timeout_sec timeout in seconds. This function will still 348 * block after the timeout expires, but will print the provided errmsg. 349 * \param errmsg error message to print once the timeout expires 350 * \return return the code passed to spdk_vhost_session_event_done(). 351 */ 352 int vhost_session_send_event(struct vhost_poll_group *pg, 353 struct spdk_vhost_session *vsession, 354 spdk_vhost_session_fn cb_fn, unsigned timeout_sec, 355 const char *errmsg); 356 357 /** 358 * Finish a blocking spdk_vhost_session_send_event() call and finally 359 * start the session. This must be called on the target lcore, which 360 * will now receive all session-related messages (e.g. from 361 * spdk_vhost_dev_foreach_session()). 362 * 363 * Must be called under the global vhost lock. 364 * 365 * \param vsession vhost session 366 * \param response return code 367 */ 368 void vhost_session_start_done(struct spdk_vhost_session *vsession, int response); 369 370 /** 371 * Finish a blocking spdk_vhost_session_send_event() call and finally 372 * stop the session. This must be called on the session's lcore which 373 * used to receive all session-related messages (e.g. from 374 * spdk_vhost_dev_foreach_session()). After this call, the session- 375 * related messages will be once again processed by any arbitrary thread. 376 * 377 * Must be called under the global vhost lock. 378 * 379 * Must be called under the global vhost mutex. 380 * 381 * \param vsession vhost session 382 * \param response return code 383 */ 384 void vhost_session_stop_done(struct spdk_vhost_session *vsession, int response); 385 386 struct spdk_vhost_session *vhost_session_find_by_vid(int vid); 387 void vhost_session_install_rte_compat_hooks(struct spdk_vhost_session *vsession); 388 void vhost_dev_install_rte_compat_hooks(struct spdk_vhost_dev *vdev); 389 390 struct vhost_poll_group *vhost_get_poll_group(struct spdk_cpuset *cpumask); 391 void vhost_put_poll_group(struct vhost_poll_group *pg); 392 393 int remove_vhost_controller(struct spdk_vhost_dev *vdev); 394 395 #ifdef SPDK_CONFIG_VHOST_INTERNAL_LIB 396 int vhost_nvme_admin_passthrough(int vid, void *cmd, void *cqe, void *buf); 397 int vhost_nvme_set_cq_call(int vid, uint16_t qid, int fd); 398 int vhost_nvme_set_bar_mr(int vid, void *bar_addr, uint64_t bar_size); 399 int vhost_nvme_get_cap(int vid, uint64_t *cap); 400 int vhost_nvme_controller_construct(void); 401 int vhost_nvme_dev_construct(const char *name, const char *cpumask, uint32_t io_queues); 402 int vhost_nvme_dev_remove(struct spdk_vhost_dev *vdev); 403 int vhost_nvme_dev_add_ns(struct spdk_vhost_dev *vdev, 404 const char *bdev_name); 405 #endif 406 407 #endif /* SPDK_VHOST_INTERNAL_H */ 408