1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2016 Intel Corporation. 3 4Vhost Library 5============= 6 7The vhost library implements a user space virtio net server allowing the user 8to manipulate the virtio ring directly. In another words, it allows the user 9to fetch/put packets from/to the VM virtio net device. To achieve this, a 10vhost library should be able to: 11 12* Access the guest memory: 13 14 For QEMU, this is done by using the ``-object memory-backend-file,share=on,...`` 15 option. Which means QEMU will create a file to serve as the guest RAM. 16 The ``share=on`` option allows another process to map that file, which 17 means it can access the guest RAM. 18 19* Know all the necessary information about the vring: 20 21 Information such as where the available ring is stored. Vhost defines some 22 messages (passed through a Unix domain socket file) to tell the backend all 23 the information it needs to know how to manipulate the vring. 24 25 26Vhost API Overview 27------------------ 28 29The following is an overview of some key Vhost API functions: 30 31* ``rte_vhost_driver_register(path, flags)`` 32 33 This function registers a vhost driver into the system. ``path`` specifies 34 the Unix domain socket file path. 35 36 Currently supported flags are: 37 38 - ``RTE_VHOST_USER_CLIENT`` 39 40 DPDK vhost-user will act as the client when this flag is given. See below 41 for an explanation. 42 43 - ``RTE_VHOST_USER_NO_RECONNECT`` 44 45 When DPDK vhost-user acts as the client it will keep trying to reconnect 46 to the server (QEMU) until it succeeds. This is useful in two cases: 47 48 * When QEMU is not started yet. 49 * When QEMU restarts (for example due to a guest OS reboot). 50 51 This reconnect option is enabled by default. However, it can be turned off 52 by setting this flag. 53 54 - ``RTE_VHOST_USER_IOMMU_SUPPORT`` 55 56 IOMMU support will be enabled when this flag is set. It is disabled by 57 default. 58 59 Enabling this flag makes possible to use guest vIOMMU to protect vhost 60 from accessing memory the virtio device isn't allowed to, when the feature 61 is negotiated and an IOMMU device is declared. 62 63 - ``RTE_VHOST_USER_POSTCOPY_SUPPORT`` 64 65 Postcopy live-migration support will be enabled when this flag is set. 66 It is disabled by default. 67 68 Enabling this flag should only be done when the calling application does 69 not pre-fault the guest shared memory, otherwise migration would fail. 70 71 - ``RTE_VHOST_USER_LINEARBUF_SUPPORT`` 72 73 Enabling this flag forces vhost dequeue function to only provide linear 74 pktmbuf (no multi-segmented pktmbuf). 75 76 The vhost library by default provides a single pktmbuf for given a 77 packet, but if for some reason the data doesn't fit into a single 78 pktmbuf (e.g., TSO is enabled), the library will allocate additional 79 pktmbufs from the same mempool and chain them together to create a 80 multi-segmented pktmbuf. 81 82 However, the vhost application needs to support multi-segmented format. 83 If the vhost application does not support that format and requires large 84 buffers to be dequeue, this flag should be enabled to force only linear 85 buffers (see RTE_VHOST_USER_EXTBUF_SUPPORT) or drop the packet. 86 87 It is disabled by default. 88 89 - ``RTE_VHOST_USER_EXTBUF_SUPPORT`` 90 91 Enabling this flag allows vhost dequeue function to allocate and attach 92 an external buffer to a pktmbuf if the pkmbuf doesn't provide enough 93 space to store all data. 94 95 This is useful when the vhost application wants to support large packets 96 but doesn't want to increase the default mempool object size nor to 97 support multi-segmented mbufs (non-linear). In this case, a fresh buffer 98 is allocated using rte_malloc() which gets attached to a pktmbuf using 99 rte_pktmbuf_attach_extbuf(). 100 101 See RTE_VHOST_USER_LINEARBUF_SUPPORT as well to disable multi-segmented 102 mbufs for application that doesn't support chained mbufs. 103 104 It is disabled by default. 105 106 - ``RTE_VHOST_USER_ASYNC_COPY`` 107 108 Asynchronous data path will be enabled when this flag is set. Async 109 data path allows applications to enable DMA acceleration for vhost 110 queues. Vhost leverages the registered DMA channels to free CPU from 111 memory copy operations in data path. A set of async data path APIs are 112 defined for DPDK applications to make use of the async capability. Only 113 packets enqueued/dequeued by async APIs are processed through the async 114 data path. 115 116 Currently this feature is only implemented on split ring enqueue data 117 path. 118 119 It is disabled by default. 120 121 - ``RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS`` 122 123 Since v16.04, the vhost library forwards checksum and gso requests for 124 packets received from a virtio driver by filling Tx offload metadata in 125 the mbuf. This behavior is inconsistent with other drivers but it is left 126 untouched for existing applications that might rely on it. 127 128 This flag disables the legacy behavior and instead ask vhost to simply 129 populate Rx offload metadata in the mbuf. 130 131 It is disabled by default. 132 133 - ``RTE_VHOST_USER_NET_STATS_ENABLE`` 134 135 Per-virtqueue statistics collection will be enabled when this flag is set. 136 When enabled, the application may use rte_vhost_stats_get_names() and 137 rte_vhost_stats_get() to collect statistics, and rte_vhost_stats_reset() to 138 reset them. 139 140 It is disabled by default 141 142* ``rte_vhost_driver_set_features(path, features)`` 143 144 This function sets the feature bits the vhost-user driver supports. The 145 vhost-user driver could be vhost-user net, yet it could be something else, 146 say, vhost-user SCSI. 147 148* ``rte_vhost_driver_callback_register(path, vhost_device_ops)`` 149 150 This function registers a set of callbacks, to let DPDK applications take 151 the appropriate action when some events happen. The following events are 152 currently supported: 153 154 * ``new_device(int vid)`` 155 156 This callback is invoked when a virtio device becomes ready. ``vid`` 157 is the vhost device ID. 158 159 * ``destroy_device(int vid)`` 160 161 This callback is invoked when a virtio device is paused or shut down. 162 163 * ``vring_state_changed(int vid, uint16_t queue_id, int enable)`` 164 165 This callback is invoked when a specific queue's state is changed, for 166 example to enabled or disabled. 167 168 * ``features_changed(int vid, uint64_t features)`` 169 170 This callback is invoked when the features is changed. For example, 171 ``VHOST_F_LOG_ALL`` will be set/cleared at the start/end of live 172 migration, respectively. 173 174 * ``new_connection(int vid)`` 175 176 This callback is invoked on new vhost-user socket connection. If DPDK 177 acts as the server the device should not be deleted before 178 ``destroy_connection`` callback is received. 179 180 * ``destroy_connection(int vid)`` 181 182 This callback is invoked when vhost-user socket connection is closed. 183 It indicates that device with id ``vid`` is no longer in use and can be 184 safely deleted. 185 186* ``rte_vhost_driver_disable/enable_features(path, features))`` 187 188 This function disables/enables some features. For example, it can be used to 189 disable mergeable buffers and TSO features, which both are enabled by 190 default. 191 192* ``rte_vhost_driver_start(path)`` 193 194 This function triggers the vhost-user negotiation. It should be invoked at 195 the end of initializing a vhost-user driver. 196 197* ``rte_vhost_enqueue_burst(vid, queue_id, pkts, count)`` 198 199 Transmits (enqueues) ``count`` packets from host to guest. 200 201* ``rte_vhost_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count)`` 202 203 Receives (dequeues) ``count`` packets from guest, and stored them at ``pkts``. 204 205* ``rte_vhost_crypto_create(vid, cryptodev_id, sess_mempool, socket_id)`` 206 207 As an extension of new_device(), this function adds virtio-crypto workload 208 acceleration capability to the device. All crypto workload is processed by 209 DPDK cryptodev with the device ID of ``cryptodev_id``. 210 211* ``rte_vhost_crypto_free(vid)`` 212 213 Frees the memory and vhost-user message handlers created in 214 rte_vhost_crypto_create(). 215 216* ``rte_vhost_crypto_fetch_requests(vid, queue_id, ops, nb_ops)`` 217 218 Receives (dequeues) ``nb_ops`` virtio-crypto requests from guest, parses 219 them to DPDK Crypto Operations, and fills the ``ops`` with parsing results. 220 221* ``rte_vhost_crypto_finalize_requests(queue_id, ops, nb_ops)`` 222 223 After the ``ops`` are dequeued from Cryptodev, finalizes the jobs and 224 notifies the guest(s). 225 226* ``rte_vhost_crypto_set_zero_copy(vid, option)`` 227 228 Enable or disable zero copy feature of the vhost crypto backend. 229 230* ``rte_vhost_async_dma_configure(dma_id, vchan_id)`` 231 232 Tell vhost which DMA vChannel is going to use. This function needs to 233 be called before register async data-path for vring. 234 235* ``rte_vhost_async_channel_register(vid, queue_id)`` 236 237 Register async DMA acceleration for a vhost queue after vring is enabled. 238 239* ``rte_vhost_async_channel_register_thread_unsafe(vid, queue_id)`` 240 241 Register async DMA acceleration for a vhost queue without performing 242 any locking. 243 244 This function is only safe to call in vhost callback functions 245 (i.e., struct rte_vhost_device_ops). 246 247* ``rte_vhost_async_channel_unregister(vid, queue_id)`` 248 249 Unregister the async DMA acceleration from a vhost queue. 250 Unregistration will fail, if the vhost queue has in-flight 251 packets that are not completed. 252 253 Unregister async DMA acceleration in vring_state_changed() may 254 fail, as this API tries to acquire the spinlock of vhost 255 queue. The recommended way is to unregister async copy 256 devices for all vhost queues in destroy_device(), when a 257 virtio device is paused or shut down. 258 259* ``rte_vhost_async_channel_unregister_thread_unsafe(vid, queue_id)`` 260 261 Unregister async DMA acceleration for a vhost queue without performing 262 any locking. 263 264 This function is only safe to call in vhost callback functions 265 (i.e., struct rte_vhost_device_ops). 266 267* ``rte_vhost_submit_enqueue_burst(vid, queue_id, pkts, count, dma_id, vchan_id)`` 268 269 Submit an enqueue request to transmit ``count`` packets from host to guest 270 by async data path. Applications must not free the packets submitted for 271 enqueue until the packets are completed. 272 273* ``rte_vhost_poll_enqueue_completed(vid, queue_id, pkts, count, dma_id, vchan_id)`` 274 275 Poll enqueue completion status from async data path. Completed packets 276 are returned to applications through ``pkts``. 277 278* ``rte_vhost_async_get_inflight(vid, queue_id)`` 279 280 This function returns the amount of in-flight packets for the vhost 281 queue using async acceleration. 282 283 * ``rte_vhost_async_get_inflight_thread_unsafe(vid, queue_id)`` 284 285 Get the number of inflight packets for a vhost queue without performing 286 any locking. It should only be used within the vhost ops, which already 287 holds the lock. 288 289* ``rte_vhost_clear_queue_thread_unsafe(vid, queue_id, **pkts, count, dma_id, vchan_id)`` 290 291 Clear in-flight packets which are submitted to async channel in vhost 292 async data path without performing locking on virtqueue. Completed 293 packets are returned to applications through ``pkts``. 294 295* ``rte_vhost_clear_queue(vid, queue_id, **pkts, count, dma_id, vchan_id)`` 296 297 Clear in-flight packets which are submitted to async channel in vhost async data 298 path. Completed packets are returned to applications through ``pkts``. 299 300* ``rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx)`` 301 302 Notify the guest that used descriptors have been added to the vring. This function 303 will return -EAGAIN when vq's access lock is held by other thread, user should try 304 again later. 305 306* ``rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id, struct rte_vhost_stat_name *names, unsigned int size)`` 307 308 This function returns the names of the queue statistics. It requires 309 statistics collection to be enabled at registration time. 310 311* ``rte_vhost_vring_stats_get(int vid, uint16_t queue_id, struct rte_vhost_stat *stats, unsigned int n)`` 312 313 This function returns the queue statistics. It requires statistics 314 collection to be enabled at registration time. 315 316* ``rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)`` 317 318 This function resets the queue statistics. It requires statistics 319 collection to be enabled at registration time. 320 321* ``rte_vhost_async_try_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count, 322 nr_inflight, dma_id, vchan_id)`` 323 324 Receive ``count`` packets from guest to host in async data path, 325 and store them at ``pkts``. 326 327* ``rte_vhost_driver_get_vdpa_dev_type(path, type)`` 328 329 Get device type of vDPA device, such as VDPA_DEVICE_TYPE_NET, 330 VDPA_DEVICE_TYPE_BLK. 331 332* ``rte_vhost_async_dma_unconfigure(dma_id, vchan_id)`` 333 334 Clean DMA vChannel finished to use. After this function is called, 335 the specified DMA vChannel should no longer be used by the Vhost library. 336 337* ``rte_vhost_notify_guest(int vid, uint16_t queue_id)`` 338 339 Inject the offloaded interrupt received by the 'guest_notify' callback, 340 into the vhost device's queue. 341 342* ``rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs)`` 343 344 Set the maximum number of queue pairs supported by the device. 345 346Vhost-user Implementations 347-------------------------- 348 349Vhost-user uses Unix domain sockets for passing messages. This means the DPDK 350vhost-user implementation has two options: 351 352* DPDK vhost-user acts as the server. 353 354 DPDK will create a Unix domain socket server file and listen for 355 connections from the frontend. 356 357 Note, this is the default mode, and the only mode before DPDK v16.07. 358 359 360* DPDK vhost-user acts as the client. 361 362 Unlike the server mode, this mode doesn't create the socket file; 363 it just tries to connect to the server (which responses to create the 364 file instead). 365 366 When the DPDK vhost-user application restarts, DPDK vhost-user will try to 367 connect to the server again. This is how the "reconnect" feature works. 368 369 .. Note:: 370 * The "reconnect" feature requires **QEMU v2.7** (or above). 371 372 * The vhost supported features must be exactly the same before and 373 after the restart. For example, if TSO is disabled and then enabled, 374 nothing will work and undefined issues might happen. 375 376No matter which mode is used, once a connection is established, DPDK 377vhost-user will start receiving and processing vhost messages from QEMU. 378 379For messages with a file descriptor, the file descriptor can be used directly 380in the vhost process as it is already installed by the Unix domain socket. 381 382The supported vhost messages are: 383 384* ``VHOST_SET_MEM_TABLE`` 385* ``VHOST_SET_VRING_KICK`` 386* ``VHOST_SET_VRING_CALL`` 387* ``VHOST_SET_LOG_FD`` 388* ``VHOST_SET_VRING_ERR`` 389 390For ``VHOST_SET_MEM_TABLE`` message, QEMU will send information for each 391memory region and its file descriptor in the ancillary data of the message. 392The file descriptor is used to map that region. 393 394``VHOST_SET_VRING_KICK`` is used as the signal to put the vhost device into 395the data plane, and ``VHOST_GET_VRING_BASE`` is used as the signal to remove 396the vhost device from the data plane. 397 398When the socket connection is closed, vhost will destroy the device. 399 400Guest memory requirement 401------------------------ 402 403* Memory pre-allocation 404 405 For non-async data path guest memory pre-allocation is not a 406 must but can help save memory. To do this we can add option 407 ``-mem-prealloc`` when starting QEMU, or we can lock all memory at vhost 408 side which will force memory to be allocated when it calls mmap 409 (option --mlockall in ovs-dpdk is an example in hand). 410 411 412 For async data path, we force the VM memory to be pre-allocated at vhost 413 lib when mapping the guest memory; and also we need to lock the memory to 414 prevent pages being swapped out to disk. 415 416* Memory sharing 417 418 Make sure ``share=on`` QEMU option is given. The vhost-user will not work with 419 a QEMU instance without shared memory mapping. 420 421Vhost supported vSwitch reference 422--------------------------------- 423 424For more vhost details and how to support vhost in vSwitch, please refer to 425the vhost example in the DPDK Sample Applications Guide. 426 427Vhost data path acceleration (vDPA) 428----------------------------------- 429 430vDPA supports selective datapath in vhost-user lib by enabling virtio ring 431compatible devices to serve virtio driver directly for datapath acceleration. 432 433``rte_vhost_driver_attach_vdpa_device`` is used to configure the vhost device 434with accelerated backend. 435 436Also vhost device capabilities are made configurable to adopt various devices. 437Such capabilities include supported features, protocol features, queue number. 438 439Finally, a set of device ops is defined for device specific operations: 440 441* ``get_queue_num`` 442 443 Called to get supported queue number of the device. 444 445* ``get_features`` 446 447 Called to get supported features of the device. 448 449* ``get_protocol_features`` 450 451 Called to get supported protocol features of the device. 452 453* ``dev_conf`` 454 455 Called to configure the actual device when the virtio device becomes ready. 456 457* ``dev_close`` 458 459 Called to close the actual device when the virtio device is stopped. 460 461* ``set_vring_state`` 462 463 Called to change the state of the vring in the actual device when vring state 464 changes. 465 466* ``set_features`` 467 468 Called to set the negotiated features to device. 469 470* ``migration_done`` 471 472 Called to allow the device to response to RARP sending. 473 474* ``get_vfio_group_fd`` 475 476 Called to get the VFIO group fd of the device. 477 478* ``get_vfio_device_fd`` 479 480 Called to get the VFIO device fd of the device. 481 482* ``get_notify_area`` 483 484 Called to get the notify area info of the queue. 485 486Vhost asynchronous data path 487---------------------------- 488 489Vhost asynchronous data path leverages DMA devices to offload memory 490copies from the CPU and it is implemented in an asynchronous way. It 491enables applications, like OVS, to save CPU cycles and hide memory copy 492overhead, thus achieving higher throughput. 493 494Vhost doesn't manage DMA devices and applications, like OVS, need to 495manage and configure DMA devices. Applications need to tell vhost what 496DMA devices to use in every data path function call. This design enables 497the flexibility for applications to dynamically use DMA channels in 498different function modules, not limited in vhost. 499 500In addition, vhost supports M:N mapping between vrings and DMA virtual 501channels. Specifically, one vring can use multiple different DMA channels 502and one DMA channel can be shared by multiple vrings at the same time. 503The reason of enabling one vring to use multiple DMA channels is that 504it's possible that more than one dataplane threads enqueue packets to 505the same vring with their own DMA virtual channels. Besides, the number 506of DMA devices is limited. For the purpose of scaling, it's necessary to 507support sharing DMA channels among vrings. 508 509* Async enqueue API usage 510 511 In async enqueue path, rte_vhost_poll_enqueue_completed() needs to be 512 called in time to notify the guest of DMA copy completed packets. 513 Moreover, calling rte_vhost_submit_enqueue_burst() all the time but 514 not poll completed will cause the DMA ring to be full, which will 515 result in packet loss eventually. 516 517* Recommended IOVA mode in async datapath 518 519 When DMA devices are bound to VFIO driver, VA mode is recommended. 520 For PA mode, page by page mapping may exceed IOMMU's max capability, 521 better to use 1G guest hugepage. 522 523 For UIO driver or kernel driver, any VFIO related error messages 524 can be ignored. 525