1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2010-2016 Intel Corporation.
7 The vhost library implements a user space virtio net server allowing the user
8 to manipulate the virtio ring directly. In another words, it allows the user
9 to fetch/put packets from/to the VM virtio net device. To achieve this, a
10 vhost library should be able to:
12 * Access the guest memory:
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.
19 * Know all the necessary information about the vring:
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.
29 The following is an overview of some key Vhost API functions:
31 * ``rte_vhost_driver_register(path, flags)``
33 This function registers a vhost driver into the system. ``path`` specifies
34 the Unix domain socket file path.
36 Currently supported flags are:
38 - ``RTE_VHOST_USER_CLIENT``
40 DPDK vhost-user will act as the client when this flag is given. See below
43 - ``RTE_VHOST_USER_NO_RECONNECT``
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:
48 * When QEMU is not started yet.
49 * When QEMU restarts (for example due to a guest OS reboot).
51 This reconnect option is enabled by default. However, it can be turned off
54 - ``RTE_VHOST_USER_IOMMU_SUPPORT``
56 IOMMU support will be enabled when this flag is set. It is disabled by
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.
63 - ``RTE_VHOST_USER_POSTCOPY_SUPPORT``
65 Postcopy live-migration support will be enabled when this flag is set.
66 It is disabled by default.
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.
71 - ``RTE_VHOST_USER_LINEARBUF_SUPPORT``
73 Enabling this flag forces vhost dequeue function to only provide linear
74 pktmbuf (no multi-segmented pktmbuf).
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.
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.
87 It is disabled by default.
89 - ``RTE_VHOST_USER_EXTBUF_SUPPORT``
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.
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().
101 See RTE_VHOST_USER_LINEARBUF_SUPPORT as well to disable multi-segmented
102 mbufs for application that doesn't support chained mbufs.
104 It is disabled by default.
106 - ``RTE_VHOST_USER_ASYNC_COPY``
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
116 Currently this feature is only implemented on split ring enqueue data
119 It is disabled by default.
121 - ``RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS``
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.
128 This flag disables the legacy behavior and instead ask vhost to simply
129 populate Rx offload metadata in the mbuf.
131 It is disabled by default.
133 * ``rte_vhost_driver_set_features(path, features)``
135 This function sets the feature bits the vhost-user driver supports. The
136 vhost-user driver could be vhost-user net, yet it could be something else,
137 say, vhost-user SCSI.
139 * ``rte_vhost_driver_callback_register(path, vhost_device_ops)``
141 This function registers a set of callbacks, to let DPDK applications take
142 the appropriate action when some events happen. The following events are
145 * ``new_device(int vid)``
147 This callback is invoked when a virtio device becomes ready. ``vid``
148 is the vhost device ID.
150 * ``destroy_device(int vid)``
152 This callback is invoked when a virtio device is paused or shut down.
154 * ``vring_state_changed(int vid, uint16_t queue_id, int enable)``
156 This callback is invoked when a specific queue's state is changed, for
157 example to enabled or disabled.
159 * ``features_changed(int vid, uint64_t features)``
161 This callback is invoked when the features is changed. For example,
162 ``VHOST_F_LOG_ALL`` will be set/cleared at the start/end of live
163 migration, respectively.
165 * ``new_connection(int vid)``
167 This callback is invoked on new vhost-user socket connection. If DPDK
168 acts as the server the device should not be deleted before
169 ``destroy_connection`` callback is received.
171 * ``destroy_connection(int vid)``
173 This callback is invoked when vhost-user socket connection is closed.
174 It indicates that device with id ``vid`` is no longer in use and can be
177 * ``rte_vhost_driver_disable/enable_features(path, features))``
179 This function disables/enables some features. For example, it can be used to
180 disable mergeable buffers and TSO features, which both are enabled by
183 * ``rte_vhost_driver_start(path)``
185 This function triggers the vhost-user negotiation. It should be invoked at
186 the end of initializing a vhost-user driver.
188 * ``rte_vhost_enqueue_burst(vid, queue_id, pkts, count)``
190 Transmits (enqueues) ``count`` packets from host to guest.
192 * ``rte_vhost_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count)``
194 Receives (dequeues) ``count`` packets from guest, and stored them at ``pkts``.
196 * ``rte_vhost_crypto_create(vid, cryptodev_id, sess_mempool, socket_id)``
198 As an extension of new_device(), this function adds virtio-crypto workload
199 acceleration capability to the device. All crypto workload is processed by
200 DPDK cryptodev with the device ID of ``cryptodev_id``.
202 * ``rte_vhost_crypto_free(vid)``
204 Frees the memory and vhost-user message handlers created in
205 rte_vhost_crypto_create().
207 * ``rte_vhost_crypto_fetch_requests(vid, queue_id, ops, nb_ops)``
209 Receives (dequeues) ``nb_ops`` virtio-crypto requests from guest, parses
210 them to DPDK Crypto Operations, and fills the ``ops`` with parsing results.
212 * ``rte_vhost_crypto_finalize_requests(queue_id, ops, nb_ops)``
214 After the ``ops`` are dequeued from Cryptodev, finalizes the jobs and
215 notifies the guest(s).
217 * ``rte_vhost_crypto_set_zero_copy(vid, option)``
219 Enable or disable zero copy feature of the vhost crypto backend.
221 * ``rte_vhost_async_dma_configure(dma_id, vchan_id)``
223 Tell vhost which DMA vChannel is going to use. This function needs to
224 be called before register async data-path for vring.
226 * ``rte_vhost_async_channel_register(vid, queue_id)``
228 Register async DMA acceleration for a vhost queue after vring is enabled.
230 * ``rte_vhost_async_channel_register_thread_unsafe(vid, queue_id)``
232 Register async DMA acceleration for a vhost queue without performing
235 This function is only safe to call in vhost callback functions
236 (i.e., struct rte_vhost_device_ops).
238 * ``rte_vhost_async_channel_unregister(vid, queue_id)``
240 Unregister the async DMA acceleration from a vhost queue.
241 Unregistration will fail, if the vhost queue has in-flight
242 packets that are not completed.
244 Unregister async DMA acceleration in vring_state_changed() may
245 fail, as this API tries to acquire the spinlock of vhost
246 queue. The recommended way is to unregister async copy
247 devices for all vhost queues in destroy_device(), when a
248 virtio device is paused or shut down.
250 * ``rte_vhost_async_channel_unregister_thread_unsafe(vid, queue_id)``
252 Unregister async DMA acceleration for a vhost queue without performing
255 This function is only safe to call in vhost callback functions
256 (i.e., struct rte_vhost_device_ops).
258 * ``rte_vhost_submit_enqueue_burst(vid, queue_id, pkts, count, dma_id, vchan_id)``
260 Submit an enqueue request to transmit ``count`` packets from host to guest
261 by async data path. Applications must not free the packets submitted for
262 enqueue until the packets are completed.
264 * ``rte_vhost_poll_enqueue_completed(vid, queue_id, pkts, count, dma_id, vchan_id)``
266 Poll enqueue completion status from async data path. Completed packets
267 are returned to applications through ``pkts``.
269 * ``rte_vhost_async_get_inflight(vid, queue_id)``
271 This function returns the amount of in-flight packets for the vhost
272 queue using async acceleration.
274 * ``rte_vhost_clear_queue_thread_unsafe(vid, queue_id, **pkts, count, dma_id, vchan_id)``
276 Clear inflight packets which are submitted to DMA engine in vhost async data
277 path. Completed packets are returned to applications through ``pkts``.
279 Vhost-user Implementations
280 --------------------------
282 Vhost-user uses Unix domain sockets for passing messages. This means the DPDK
283 vhost-user implementation has two options:
285 * DPDK vhost-user acts as the server.
287 DPDK will create a Unix domain socket server file and listen for
288 connections from the frontend.
290 Note, this is the default mode, and the only mode before DPDK v16.07.
293 * DPDK vhost-user acts as the client.
295 Unlike the server mode, this mode doesn't create the socket file;
296 it just tries to connect to the server (which responses to create the
299 When the DPDK vhost-user application restarts, DPDK vhost-user will try to
300 connect to the server again. This is how the "reconnect" feature works.
303 * The "reconnect" feature requires **QEMU v2.7** (or above).
305 * The vhost supported features must be exactly the same before and
306 after the restart. For example, if TSO is disabled and then enabled,
307 nothing will work and issues undefined might happen.
309 No matter which mode is used, once a connection is established, DPDK
310 vhost-user will start receiving and processing vhost messages from QEMU.
312 For messages with a file descriptor, the file descriptor can be used directly
313 in the vhost process as it is already installed by the Unix domain socket.
315 The supported vhost messages are:
317 * ``VHOST_SET_MEM_TABLE``
318 * ``VHOST_SET_VRING_KICK``
319 * ``VHOST_SET_VRING_CALL``
320 * ``VHOST_SET_LOG_FD``
321 * ``VHOST_SET_VRING_ERR``
323 For ``VHOST_SET_MEM_TABLE`` message, QEMU will send information for each
324 memory region and its file descriptor in the ancillary data of the message.
325 The file descriptor is used to map that region.
327 ``VHOST_SET_VRING_KICK`` is used as the signal to put the vhost device into
328 the data plane, and ``VHOST_GET_VRING_BASE`` is used as the signal to remove
329 the vhost device from the data plane.
331 When the socket connection is closed, vhost will destroy the device.
333 Guest memory requirement
334 ------------------------
336 * Memory pre-allocation
338 For non-async data path, guest memory pre-allocation is not a
339 must. This can help save of memory. If users really want the guest memory
340 to be pre-allocated (e.g., for performance reason), we can add option
341 ``-mem-prealloc`` when starting QEMU. Or, we can lock all memory at vhost
342 side which will force memory to be allocated when mmap at vhost side;
343 option --mlockall in ovs-dpdk is an example in hand.
345 For async data path, we force the VM memory to be pre-allocated at vhost
346 lib when mapping the guest memory; and also we need to lock the memory to
347 prevent pages being swapped out to disk.
351 Make sure ``share=on`` QEMU option is given. vhost-user will not work with
352 a QEMU version without shared memory mapping.
354 Vhost supported vSwitch reference
355 ---------------------------------
357 For more vhost details and how to support vhost in vSwitch, please refer to
358 the vhost example in the DPDK Sample Applications Guide.
360 Vhost data path acceleration (vDPA)
361 -----------------------------------
363 vDPA supports selective datapath in vhost-user lib by enabling virtio ring
364 compatible devices to serve virtio driver directly for datapath acceleration.
366 ``rte_vhost_driver_attach_vdpa_device`` is used to configure the vhost device
367 with accelerated backend.
369 Also vhost device capabilities are made configurable to adopt various devices.
370 Such capabilities include supported features, protocol features, queue number.
372 Finally, a set of device ops is defined for device specific operations:
376 Called to get supported queue number of the device.
380 Called to get supported features of the device.
382 * ``get_protocol_features``
384 Called to get supported protocol features of the device.
388 Called to configure the actual device when the virtio device becomes ready.
392 Called to close the actual device when the virtio device is stopped.
394 * ``set_vring_state``
396 Called to change the state of the vring in the actual device when vring state
401 Called to set the negotiated features to device.
405 Called to allow the device to response to RARP sending.
407 * ``get_vfio_group_fd``
409 Called to get the VFIO group fd of the device.
411 * ``get_vfio_device_fd``
413 Called to get the VFIO device fd of the device.
415 * ``get_notify_area``
417 Called to get the notify area info of the queue.
419 Vhost asynchronous data path
420 ----------------------------
422 Vhost asynchronous data path leverages DMA devices to offload memory
423 copies from the CPU and it is implemented in an asynchronous way. It
424 enables applications, like OVS, to save CPU cycles and hide memory copy
425 overhead, thus achieving higher throughput.
427 Vhost doesn't manage DMA devices and applications, like OVS, need to
428 manage and configure DMA devices. Applications need to tell vhost what
429 DMA devices to use in every data path function call. This design enables
430 the flexibility for applications to dynamically use DMA channels in
431 different function modules, not limited in vhost.
433 In addition, vhost supports M:N mapping between vrings and DMA virtual
434 channels. Specifically, one vring can use multiple different DMA channels
435 and one DMA channel can be shared by multiple vrings at the same time.
436 The reason of enabling one vring to use multiple DMA channels is that
437 it's possible that more than one dataplane threads enqueue packets to
438 the same vring with their own DMA virtual channels. Besides, the number
439 of DMA devices is limited. For the purpose of scaling, it's necessary to
440 support sharing DMA channels among vrings.
442 Recommended IOVA mode in async datapath
443 ---------------------------------------
445 When DMA devices are bound to VFIO driver, VA mode is recommended.
446 For PA mode, page by page mapping may exceed IOMMU's max capability,
447 better to use 1G guest hugepage.
449 For UIO driver, any VFIO related error message can be ignored.