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