15630257fSFerruh Yigit.. SPDX-License-Identifier: BSD-3-Clause 25630257fSFerruh Yigit Copyright(c) 2010-2016 Intel Corporation. 30ee5e7fbSSiobhan Butler 40ee5e7fbSSiobhan ButlerVhost Library 50ee5e7fbSSiobhan Butler============= 60ee5e7fbSSiobhan Butler 72bfaec90SYuanhan LiuThe vhost library implements a user space virtio net server allowing the user 82bfaec90SYuanhan Liuto manipulate the virtio ring directly. In another words, it allows the user 92bfaec90SYuanhan Liuto fetch/put packets from/to the VM virtio net device. To achieve this, a 102bfaec90SYuanhan Liuvhost library should be able to: 112bfaec90SYuanhan Liu 122bfaec90SYuanhan Liu* Access the guest memory: 132bfaec90SYuanhan Liu 142bfaec90SYuanhan Liu For QEMU, this is done by using the ``-object memory-backend-file,share=on,...`` 152bfaec90SYuanhan Liu option. Which means QEMU will create a file to serve as the guest RAM. 162bfaec90SYuanhan Liu The ``share=on`` option allows another process to map that file, which 172bfaec90SYuanhan Liu means it can access the guest RAM. 182bfaec90SYuanhan Liu 192bfaec90SYuanhan Liu* Know all the necessary information about the vring: 202bfaec90SYuanhan Liu 212bfaec90SYuanhan Liu Information such as where the available ring is stored. Vhost defines some 22647e191bSYuanhan Liu messages (passed through a Unix domain socket file) to tell the backend all 23647e191bSYuanhan Liu the information it needs to know how to manipulate the vring. 242bfaec90SYuanhan Liu 250ee5e7fbSSiobhan Butler 260ee5e7fbSSiobhan ButlerVhost API Overview 270ee5e7fbSSiobhan Butler------------------ 280ee5e7fbSSiobhan Butler 295fbb3941SYuanhan LiuThe following is an overview of some key Vhost API functions: 300ee5e7fbSSiobhan Butler 312bfaec90SYuanhan Liu* ``rte_vhost_driver_register(path, flags)`` 320ee5e7fbSSiobhan Butler 33647e191bSYuanhan Liu This function registers a vhost driver into the system. ``path`` specifies 34647e191bSYuanhan Liu the Unix domain socket file path. 350ee5e7fbSSiobhan Butler 36647e191bSYuanhan Liu Currently supported flags are: 370ee5e7fbSSiobhan Butler 382bfaec90SYuanhan Liu - ``RTE_VHOST_USER_CLIENT`` 390ee5e7fbSSiobhan Butler 402bfaec90SYuanhan Liu DPDK vhost-user will act as the client when this flag is given. See below 412bfaec90SYuanhan Liu for an explanation. 420ee5e7fbSSiobhan Butler 432bfaec90SYuanhan Liu - ``RTE_VHOST_USER_NO_RECONNECT`` 440ee5e7fbSSiobhan Butler 452bfaec90SYuanhan Liu When DPDK vhost-user acts as the client it will keep trying to reconnect 462bfaec90SYuanhan Liu to the server (QEMU) until it succeeds. This is useful in two cases: 470ee5e7fbSSiobhan Butler 482bfaec90SYuanhan Liu * When QEMU is not started yet. 492bfaec90SYuanhan Liu * When QEMU restarts (for example due to a guest OS reboot). 500ee5e7fbSSiobhan Butler 512bfaec90SYuanhan Liu This reconnect option is enabled by default. However, it can be turned off 522bfaec90SYuanhan Liu by setting this flag. 530ee5e7fbSSiobhan Butler 549ba1e744SYuanhan Liu - ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` 559ba1e744SYuanhan Liu 569ba1e744SYuanhan Liu Dequeue zero copy will be enabled when this flag is set. It is disabled by 579ba1e744SYuanhan Liu default. 589ba1e744SYuanhan Liu 599ba1e744SYuanhan Liu There are some truths (including limitations) you might want to know while 609ba1e744SYuanhan Liu setting this flag: 619ba1e744SYuanhan Liu 629ba1e744SYuanhan Liu * zero copy is not good for small packets (typically for packet size below 639ba1e744SYuanhan Liu 512). 649ba1e744SYuanhan Liu 659ba1e744SYuanhan Liu * zero copy is really good for VM2VM case. For iperf between two VMs, the 669ba1e744SYuanhan Liu boost could be above 70% (when TSO is enableld). 679ba1e744SYuanhan Liu 689ba1e744SYuanhan Liu * for VM2NIC case, the ``nb_tx_desc`` has to be small enough: <= 64 if virtio 699ba1e744SYuanhan Liu indirect feature is not enabled and <= 128 if it is enabled. 709ba1e744SYuanhan Liu 71580f8e36SYong Wang This is because when dequeue zero copy is enabled, guest Tx used vring will 729ba1e744SYuanhan Liu be updated only when corresponding mbuf is freed. Thus, the nb_tx_desc 739ba1e744SYuanhan Liu has to be small enough so that the PMD driver will run out of available 749ba1e744SYuanhan Liu Tx descriptors and free mbufs timely. Otherwise, guest Tx vring would be 759ba1e744SYuanhan Liu starved. 769ba1e744SYuanhan Liu 779ba1e744SYuanhan Liu * Guest memory should be backended with huge pages to achieve better 789ba1e744SYuanhan Liu performance. Using 1G page size is the best. 799ba1e744SYuanhan Liu 809ba1e744SYuanhan Liu When dequeue zero copy is enabled, the guest phys address and host phys 819ba1e744SYuanhan Liu address mapping has to be established. Using non-huge pages means far 829ba1e744SYuanhan Liu more page segments. To make it simple, DPDK vhost does a linear search 839ba1e744SYuanhan Liu of those segments, thus the fewer the segments, the quicker we will get 849ba1e744SYuanhan Liu the mapping. NOTE: we may speed it by using tree searching in future. 859ba1e744SYuanhan Liu 86*e3075e96SJunjie Chen * zero copy can not work when using vfio-pci with iommu mode currently, this 87*e3075e96SJunjie Chen is because we don't setup iommu dma mapping for guest memory. If you have 88*e3075e96SJunjie Chen to use vfio-pci driver, please insert vfio-pci kernel module in noiommu 89*e3075e96SJunjie Chen mode. 90*e3075e96SJunjie Chen 91002d6a7eSMaxime Coquelin - ``RTE_VHOST_USER_IOMMU_SUPPORT`` 92002d6a7eSMaxime Coquelin 93002d6a7eSMaxime Coquelin IOMMU support will be enabled when this flag is set. It is disabled by 94002d6a7eSMaxime Coquelin default. 95002d6a7eSMaxime Coquelin 96002d6a7eSMaxime Coquelin Enabling this flag makes possible to use guest vIOMMU to protect vhost 97002d6a7eSMaxime Coquelin from accessing memory the virtio device isn't allowed to, when the feature 98002d6a7eSMaxime Coquelin is negotiated and an IOMMU device is declared. 99002d6a7eSMaxime Coquelin 100002d6a7eSMaxime Coquelin However, this feature enables vhost-user's reply-ack protocol feature, 101002d6a7eSMaxime Coquelin which implementation is buggy in Qemu v2.7.0-v2.9.0 when doing multiqueue. 102002d6a7eSMaxime Coquelin Enabling this flag with these Qemu version results in Qemu being blocked 103002d6a7eSMaxime Coquelin when multiple queue pairs are declared. 104002d6a7eSMaxime Coquelin 1055fbb3941SYuanhan Liu* ``rte_vhost_driver_set_features(path, features)`` 1065fbb3941SYuanhan Liu 1075fbb3941SYuanhan Liu This function sets the feature bits the vhost-user driver supports. The 1085fbb3941SYuanhan Liu vhost-user driver could be vhost-user net, yet it could be something else, 1095fbb3941SYuanhan Liu say, vhost-user SCSI. 1105fbb3941SYuanhan Liu 1117c129037SYuanhan Liu* ``rte_vhost_driver_callback_register(path, vhost_device_ops)`` 1122bfaec90SYuanhan Liu 1132bfaec90SYuanhan Liu This function registers a set of callbacks, to let DPDK applications take 1142bfaec90SYuanhan Liu the appropriate action when some events happen. The following events are 1152bfaec90SYuanhan Liu currently supported: 1162bfaec90SYuanhan Liu 1172bfaec90SYuanhan Liu * ``new_device(int vid)`` 1182bfaec90SYuanhan Liu 119cb043557SYuanhan Liu This callback is invoked when a virtio device becomes ready. ``vid`` 120cb043557SYuanhan Liu is the vhost device ID. 1212bfaec90SYuanhan Liu 1222bfaec90SYuanhan Liu * ``destroy_device(int vid)`` 1232bfaec90SYuanhan Liu 124efba12a7SDariusz Stojaczyk This callback is invoked when a virtio device is paused or shut down. 1252bfaec90SYuanhan Liu 1262bfaec90SYuanhan Liu * ``vring_state_changed(int vid, uint16_t queue_id, int enable)`` 1272bfaec90SYuanhan Liu 1282bfaec90SYuanhan Liu This callback is invoked when a specific queue's state is changed, for 1292bfaec90SYuanhan Liu example to enabled or disabled. 1302bfaec90SYuanhan Liu 131abd53c16SYuanhan Liu * ``features_changed(int vid, uint64_t features)`` 132abd53c16SYuanhan Liu 133abd53c16SYuanhan Liu This callback is invoked when the features is changed. For example, 134abd53c16SYuanhan Liu ``VHOST_F_LOG_ALL`` will be set/cleared at the start/end of live 135abd53c16SYuanhan Liu migration, respectively. 136abd53c16SYuanhan Liu 137efba12a7SDariusz Stojaczyk * ``new_connection(int vid)`` 138efba12a7SDariusz Stojaczyk 139efba12a7SDariusz Stojaczyk This callback is invoked on new vhost-user socket connection. If DPDK 140efba12a7SDariusz Stojaczyk acts as the server the device should not be deleted before 141efba12a7SDariusz Stojaczyk ``destroy_connection`` callback is received. 142efba12a7SDariusz Stojaczyk 143efba12a7SDariusz Stojaczyk * ``destroy_connection(int vid)`` 144efba12a7SDariusz Stojaczyk 145efba12a7SDariusz Stojaczyk This callback is invoked when vhost-user socket connection is closed. 146efba12a7SDariusz Stojaczyk It indicates that device with id ``vid`` is no longer in use and can be 147efba12a7SDariusz Stojaczyk safely deleted. 148efba12a7SDariusz Stojaczyk 149af147591SYuanhan Liu* ``rte_vhost_driver_disable/enable_features(path, features))`` 150af147591SYuanhan Liu 151af147591SYuanhan Liu This function disables/enables some features. For example, it can be used to 152af147591SYuanhan Liu disable mergeable buffers and TSO features, which both are enabled by 153af147591SYuanhan Liu default. 154af147591SYuanhan Liu 155af147591SYuanhan Liu* ``rte_vhost_driver_start(path)`` 156af147591SYuanhan Liu 157af147591SYuanhan Liu This function triggers the vhost-user negotiation. It should be invoked at 158af147591SYuanhan Liu the end of initializing a vhost-user driver. 159af147591SYuanhan Liu 1602bfaec90SYuanhan Liu* ``rte_vhost_enqueue_burst(vid, queue_id, pkts, count)`` 1612bfaec90SYuanhan Liu 1622bfaec90SYuanhan Liu Transmits (enqueues) ``count`` packets from host to guest. 1632bfaec90SYuanhan Liu 1642bfaec90SYuanhan Liu* ``rte_vhost_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count)`` 1652bfaec90SYuanhan Liu 1662bfaec90SYuanhan Liu Receives (dequeues) ``count`` packets from guest, and stored them at ``pkts``. 1672bfaec90SYuanhan Liu 168939066d9SFan Zhang* ``rte_vhost_crypto_create(vid, cryptodev_id, sess_mempool, socket_id)`` 169939066d9SFan Zhang 170939066d9SFan Zhang As an extension of new_device(), this function adds virtio-crypto workload 171939066d9SFan Zhang acceleration capability to the device. All crypto workload is processed by 172939066d9SFan Zhang DPDK cryptodev with the device ID of ``cryptodev_id``. 173939066d9SFan Zhang 174939066d9SFan Zhang* ``rte_vhost_crypto_free(vid)`` 175939066d9SFan Zhang 176939066d9SFan Zhang Frees the memory and vhost-user message handlers created in 177939066d9SFan Zhang rte_vhost_crypto_create(). 178939066d9SFan Zhang 179939066d9SFan Zhang* ``rte_vhost_crypto_fetch_requests(vid, queue_id, ops, nb_ops)`` 180939066d9SFan Zhang 181939066d9SFan Zhang Receives (dequeues) ``nb_ops`` virtio-crypto requests from guest, parses 182939066d9SFan Zhang them to DPDK Crypto Operations, and fills the ``ops`` with parsing results. 183939066d9SFan Zhang 184939066d9SFan Zhang* ``rte_vhost_crypto_finalize_requests(queue_id, ops, nb_ops)`` 185939066d9SFan Zhang 186939066d9SFan Zhang After the ``ops`` are dequeued from Cryptodev, finalizes the jobs and 187939066d9SFan Zhang notifies the guest(s). 188939066d9SFan Zhang 189939066d9SFan Zhang* ``rte_vhost_crypto_set_zero_copy(vid, option)`` 190939066d9SFan Zhang 191939066d9SFan Zhang Enable or disable zero copy feature of the vhost crypto backend. 192939066d9SFan Zhang 193647e191bSYuanhan LiuVhost-user Implementations 194647e191bSYuanhan Liu-------------------------- 19542683a7dSHuawei Xie 1962bfaec90SYuanhan LiuVhost-user uses Unix domain sockets for passing messages. This means the DPDK 1972bfaec90SYuanhan Liuvhost-user implementation has two options: 19842683a7dSHuawei Xie 1992bfaec90SYuanhan Liu* DPDK vhost-user acts as the server. 20042683a7dSHuawei Xie 2012bfaec90SYuanhan Liu DPDK will create a Unix domain socket server file and listen for 2022bfaec90SYuanhan Liu connections from the frontend. 20342683a7dSHuawei Xie 2042bfaec90SYuanhan Liu Note, this is the default mode, and the only mode before DPDK v16.07. 20542683a7dSHuawei Xie 2062bfaec90SYuanhan Liu 2072bfaec90SYuanhan Liu* DPDK vhost-user acts as the client. 2082bfaec90SYuanhan Liu 2092bfaec90SYuanhan Liu Unlike the server mode, this mode doesn't create the socket file; 2102bfaec90SYuanhan Liu it just tries to connect to the server (which responses to create the 2112bfaec90SYuanhan Liu file instead). 2122bfaec90SYuanhan Liu 2132bfaec90SYuanhan Liu When the DPDK vhost-user application restarts, DPDK vhost-user will try to 2142bfaec90SYuanhan Liu connect to the server again. This is how the "reconnect" feature works. 2152bfaec90SYuanhan Liu 216f6ee75b5SYuanhan Liu .. Note:: 217f6ee75b5SYuanhan Liu * The "reconnect" feature requires **QEMU v2.7** (or above). 218f6ee75b5SYuanhan Liu 219f6ee75b5SYuanhan Liu * The vhost supported features must be exactly the same before and 220f6ee75b5SYuanhan Liu after the restart. For example, if TSO is disabled and then enabled, 221f6ee75b5SYuanhan Liu nothing will work and issues undefined might happen. 2222bfaec90SYuanhan Liu 2232bfaec90SYuanhan LiuNo matter which mode is used, once a connection is established, DPDK 2242bfaec90SYuanhan Liuvhost-user will start receiving and processing vhost messages from QEMU. 2252bfaec90SYuanhan Liu 2262bfaec90SYuanhan LiuFor messages with a file descriptor, the file descriptor can be used directly 2272bfaec90SYuanhan Liuin the vhost process as it is already installed by the Unix domain socket. 2282bfaec90SYuanhan Liu 2292bfaec90SYuanhan LiuThe supported vhost messages are: 2302bfaec90SYuanhan Liu 2312bfaec90SYuanhan Liu* ``VHOST_SET_MEM_TABLE`` 2322bfaec90SYuanhan Liu* ``VHOST_SET_VRING_KICK`` 2332bfaec90SYuanhan Liu* ``VHOST_SET_VRING_CALL`` 2342bfaec90SYuanhan Liu* ``VHOST_SET_LOG_FD`` 2352bfaec90SYuanhan Liu* ``VHOST_SET_VRING_ERR`` 2362bfaec90SYuanhan Liu 2372bfaec90SYuanhan LiuFor ``VHOST_SET_MEM_TABLE`` message, QEMU will send information for each 2382bfaec90SYuanhan Liumemory region and its file descriptor in the ancillary data of the message. 2392bfaec90SYuanhan LiuThe file descriptor is used to map that region. 2402bfaec90SYuanhan Liu 2412bfaec90SYuanhan Liu``VHOST_SET_VRING_KICK`` is used as the signal to put the vhost device into 2422bfaec90SYuanhan Liuthe data plane, and ``VHOST_GET_VRING_BASE`` is used as the signal to remove 2432bfaec90SYuanhan Liuthe vhost device from the data plane. 24442683a7dSHuawei Xie 24542683a7dSHuawei XieWhen the socket connection is closed, vhost will destroy the device. 24642683a7dSHuawei Xie 247768274ebSJianfeng TanGuest memory requirement 248768274ebSJianfeng Tan------------------------ 249768274ebSJianfeng Tan 250768274ebSJianfeng Tan* Memory pre-allocation 251768274ebSJianfeng Tan 252768274ebSJianfeng Tan For non-zerocopy, guest memory pre-allocation is not a must. This can help 253768274ebSJianfeng Tan save of memory. If users really want the guest memory to be pre-allocated 254768274ebSJianfeng Tan (e.g., for performance reason), we can add option ``-mem-prealloc`` when 255768274ebSJianfeng Tan starting QEMU. Or, we can lock all memory at vhost side which will force 256768274ebSJianfeng Tan memory to be allocated when mmap at vhost side; option --mlockall in 257768274ebSJianfeng Tan ovs-dpdk is an example in hand. 258768274ebSJianfeng Tan 259768274ebSJianfeng Tan For zerocopy, we force the VM memory to be pre-allocated at vhost lib when 260768274ebSJianfeng Tan mapping the guest memory; and also we need to lock the memory to prevent 261768274ebSJianfeng Tan pages being swapped out to disk. 262768274ebSJianfeng Tan 263768274ebSJianfeng Tan* Memory sharing 264768274ebSJianfeng Tan 265768274ebSJianfeng Tan Make sure ``share=on`` QEMU option is given. vhost-user will not work with 266768274ebSJianfeng Tan a QEMU version without shared memory mapping. 267768274ebSJianfeng Tan 2680ee5e7fbSSiobhan ButlerVhost supported vSwitch reference 2690ee5e7fbSSiobhan Butler--------------------------------- 2700ee5e7fbSSiobhan Butler 2712bfaec90SYuanhan LiuFor more vhost details and how to support vhost in vSwitch, please refer to 2722bfaec90SYuanhan Liuthe vhost example in the DPDK Sample Applications Guide. 273