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