10ee5e7fbSSiobhan Butler.. BSD LICENSE 22bfaec90SYuanhan Liu Copyright(c) 2010-2016 Intel Corporation. All rights reserved. 30ee5e7fbSSiobhan Butler All rights reserved. 40ee5e7fbSSiobhan Butler 50ee5e7fbSSiobhan Butler Redistribution and use in source and binary forms, with or without 60ee5e7fbSSiobhan Butler modification, are permitted provided that the following conditions 70ee5e7fbSSiobhan Butler are met: 80ee5e7fbSSiobhan Butler 90ee5e7fbSSiobhan Butler * Redistributions of source code must retain the above copyright 100ee5e7fbSSiobhan Butler notice, this list of conditions and the following disclaimer. 110ee5e7fbSSiobhan Butler * Redistributions in binary form must reproduce the above copyright 120ee5e7fbSSiobhan Butler notice, this list of conditions and the following disclaimer in 130ee5e7fbSSiobhan Butler the documentation and/or other materials provided with the 140ee5e7fbSSiobhan Butler distribution. 150ee5e7fbSSiobhan Butler * Neither the name of Intel Corporation nor the names of its 160ee5e7fbSSiobhan Butler contributors may be used to endorse or promote products derived 170ee5e7fbSSiobhan Butler from this software without specific prior written permission. 180ee5e7fbSSiobhan Butler 190ee5e7fbSSiobhan Butler THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 200ee5e7fbSSiobhan Butler "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 210ee5e7fbSSiobhan Butler LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 220ee5e7fbSSiobhan Butler A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 230ee5e7fbSSiobhan Butler OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 240ee5e7fbSSiobhan Butler SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 250ee5e7fbSSiobhan Butler LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 260ee5e7fbSSiobhan Butler DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 270ee5e7fbSSiobhan Butler THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 280ee5e7fbSSiobhan Butler (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 290ee5e7fbSSiobhan Butler OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 300ee5e7fbSSiobhan Butler 310ee5e7fbSSiobhan ButlerVhost Library 320ee5e7fbSSiobhan Butler============= 330ee5e7fbSSiobhan Butler 342bfaec90SYuanhan LiuThe vhost library implements a user space virtio net server allowing the user 352bfaec90SYuanhan Liuto manipulate the virtio ring directly. In another words, it allows the user 362bfaec90SYuanhan Liuto fetch/put packets from/to the VM virtio net device. To achieve this, a 372bfaec90SYuanhan Liuvhost library should be able to: 382bfaec90SYuanhan Liu 392bfaec90SYuanhan Liu* Access the guest memory: 402bfaec90SYuanhan Liu 412bfaec90SYuanhan Liu For QEMU, this is done by using the ``-object memory-backend-file,share=on,...`` 422bfaec90SYuanhan Liu option. Which means QEMU will create a file to serve as the guest RAM. 432bfaec90SYuanhan Liu The ``share=on`` option allows another process to map that file, which 442bfaec90SYuanhan Liu means it can access the guest RAM. 452bfaec90SYuanhan Liu 462bfaec90SYuanhan Liu* Know all the necessary information about the vring: 472bfaec90SYuanhan Liu 482bfaec90SYuanhan Liu Information such as where the available ring is stored. Vhost defines some 49647e191bSYuanhan Liu messages (passed through a Unix domain socket file) to tell the backend all 50647e191bSYuanhan Liu the information it needs to know how to manipulate the vring. 512bfaec90SYuanhan Liu 520ee5e7fbSSiobhan Butler 530ee5e7fbSSiobhan ButlerVhost API Overview 540ee5e7fbSSiobhan Butler------------------ 550ee5e7fbSSiobhan Butler 565fbb3941SYuanhan LiuThe following is an overview of some key Vhost API functions: 570ee5e7fbSSiobhan Butler 582bfaec90SYuanhan Liu* ``rte_vhost_driver_register(path, flags)`` 590ee5e7fbSSiobhan Butler 60647e191bSYuanhan Liu This function registers a vhost driver into the system. ``path`` specifies 61647e191bSYuanhan Liu the Unix domain socket file path. 620ee5e7fbSSiobhan Butler 63647e191bSYuanhan Liu Currently supported flags are: 640ee5e7fbSSiobhan Butler 652bfaec90SYuanhan Liu - ``RTE_VHOST_USER_CLIENT`` 660ee5e7fbSSiobhan Butler 672bfaec90SYuanhan Liu DPDK vhost-user will act as the client when this flag is given. See below 682bfaec90SYuanhan Liu for an explanation. 690ee5e7fbSSiobhan Butler 702bfaec90SYuanhan Liu - ``RTE_VHOST_USER_NO_RECONNECT`` 710ee5e7fbSSiobhan Butler 722bfaec90SYuanhan Liu When DPDK vhost-user acts as the client it will keep trying to reconnect 732bfaec90SYuanhan Liu to the server (QEMU) until it succeeds. This is useful in two cases: 740ee5e7fbSSiobhan Butler 752bfaec90SYuanhan Liu * When QEMU is not started yet. 762bfaec90SYuanhan Liu * When QEMU restarts (for example due to a guest OS reboot). 770ee5e7fbSSiobhan Butler 782bfaec90SYuanhan Liu This reconnect option is enabled by default. However, it can be turned off 792bfaec90SYuanhan Liu by setting this flag. 800ee5e7fbSSiobhan Butler 819ba1e744SYuanhan Liu - ``RTE_VHOST_USER_DEQUEUE_ZERO_COPY`` 829ba1e744SYuanhan Liu 839ba1e744SYuanhan Liu Dequeue zero copy will be enabled when this flag is set. It is disabled by 849ba1e744SYuanhan Liu default. 859ba1e744SYuanhan Liu 869ba1e744SYuanhan Liu There are some truths (including limitations) you might want to know while 879ba1e744SYuanhan Liu setting this flag: 889ba1e744SYuanhan Liu 899ba1e744SYuanhan Liu * zero copy is not good for small packets (typically for packet size below 909ba1e744SYuanhan Liu 512). 919ba1e744SYuanhan Liu 929ba1e744SYuanhan Liu * zero copy is really good for VM2VM case. For iperf between two VMs, the 939ba1e744SYuanhan Liu boost could be above 70% (when TSO is enableld). 949ba1e744SYuanhan Liu 959ba1e744SYuanhan Liu * for VM2NIC case, the ``nb_tx_desc`` has to be small enough: <= 64 if virtio 969ba1e744SYuanhan Liu indirect feature is not enabled and <= 128 if it is enabled. 979ba1e744SYuanhan Liu 98580f8e36SYong Wang This is because when dequeue zero copy is enabled, guest Tx used vring will 999ba1e744SYuanhan Liu be updated only when corresponding mbuf is freed. Thus, the nb_tx_desc 1009ba1e744SYuanhan Liu has to be small enough so that the PMD driver will run out of available 1019ba1e744SYuanhan Liu Tx descriptors and free mbufs timely. Otherwise, guest Tx vring would be 1029ba1e744SYuanhan Liu starved. 1039ba1e744SYuanhan Liu 1049ba1e744SYuanhan Liu * Guest memory should be backended with huge pages to achieve better 1059ba1e744SYuanhan Liu performance. Using 1G page size is the best. 1069ba1e744SYuanhan Liu 1079ba1e744SYuanhan Liu When dequeue zero copy is enabled, the guest phys address and host phys 1089ba1e744SYuanhan Liu address mapping has to be established. Using non-huge pages means far 1099ba1e744SYuanhan Liu more page segments. To make it simple, DPDK vhost does a linear search 1109ba1e744SYuanhan Liu of those segments, thus the fewer the segments, the quicker we will get 1119ba1e744SYuanhan Liu the mapping. NOTE: we may speed it by using tree searching in future. 1129ba1e744SYuanhan Liu 1135fbb3941SYuanhan Liu* ``rte_vhost_driver_set_features(path, features)`` 1145fbb3941SYuanhan Liu 1155fbb3941SYuanhan Liu This function sets the feature bits the vhost-user driver supports. The 1165fbb3941SYuanhan Liu vhost-user driver could be vhost-user net, yet it could be something else, 1175fbb3941SYuanhan Liu say, vhost-user SCSI. 1185fbb3941SYuanhan Liu 1192bfaec90SYuanhan Liu* ``rte_vhost_driver_session_start()`` 1200ee5e7fbSSiobhan Butler 1212bfaec90SYuanhan Liu This function starts the vhost session loop to handle vhost messages. It 1222bfaec90SYuanhan Liu starts an infinite loop, therefore it should be called in a dedicated 1232bfaec90SYuanhan Liu thread. 1242bfaec90SYuanhan Liu 1257c129037SYuanhan Liu* ``rte_vhost_driver_callback_register(path, vhost_device_ops)`` 1262bfaec90SYuanhan Liu 1272bfaec90SYuanhan Liu This function registers a set of callbacks, to let DPDK applications take 1282bfaec90SYuanhan Liu the appropriate action when some events happen. The following events are 1292bfaec90SYuanhan Liu currently supported: 1302bfaec90SYuanhan Liu 1312bfaec90SYuanhan Liu * ``new_device(int vid)`` 1322bfaec90SYuanhan Liu 133cb043557SYuanhan Liu This callback is invoked when a virtio device becomes ready. ``vid`` 134cb043557SYuanhan Liu is the vhost device ID. 1352bfaec90SYuanhan Liu 1362bfaec90SYuanhan Liu * ``destroy_device(int vid)`` 1372bfaec90SYuanhan Liu 138cb043557SYuanhan Liu This callback is invoked when a virtio device shuts down (or when the 1392bfaec90SYuanhan Liu vhost connection is broken). 1402bfaec90SYuanhan Liu 1412bfaec90SYuanhan Liu * ``vring_state_changed(int vid, uint16_t queue_id, int enable)`` 1422bfaec90SYuanhan Liu 1432bfaec90SYuanhan Liu This callback is invoked when a specific queue's state is changed, for 1442bfaec90SYuanhan Liu example to enabled or disabled. 1452bfaec90SYuanhan Liu 146*abd53c16SYuanhan Liu * ``features_changed(int vid, uint64_t features)`` 147*abd53c16SYuanhan Liu 148*abd53c16SYuanhan Liu This callback is invoked when the features is changed. For example, 149*abd53c16SYuanhan Liu ``VHOST_F_LOG_ALL`` will be set/cleared at the start/end of live 150*abd53c16SYuanhan Liu migration, respectively. 151*abd53c16SYuanhan Liu 1522bfaec90SYuanhan Liu* ``rte_vhost_enqueue_burst(vid, queue_id, pkts, count)`` 1532bfaec90SYuanhan Liu 1542bfaec90SYuanhan Liu Transmits (enqueues) ``count`` packets from host to guest. 1552bfaec90SYuanhan Liu 1562bfaec90SYuanhan Liu* ``rte_vhost_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count)`` 1572bfaec90SYuanhan Liu 1582bfaec90SYuanhan Liu Receives (dequeues) ``count`` packets from guest, and stored them at ``pkts``. 1592bfaec90SYuanhan Liu 1605fbb3941SYuanhan Liu* ``rte_vhost_driver_disable/enable_features(path, features))`` 1612bfaec90SYuanhan Liu 1622bfaec90SYuanhan Liu This function disables/enables some features. For example, it can be used to 1632bfaec90SYuanhan Liu disable mergeable buffers and TSO features, which both are enabled by 1642bfaec90SYuanhan Liu default. 1652bfaec90SYuanhan Liu 1662bfaec90SYuanhan Liu 167647e191bSYuanhan LiuVhost-user Implementations 168647e191bSYuanhan Liu-------------------------- 16942683a7dSHuawei Xie 1702bfaec90SYuanhan LiuVhost-user uses Unix domain sockets for passing messages. This means the DPDK 1712bfaec90SYuanhan Liuvhost-user implementation has two options: 17242683a7dSHuawei Xie 1732bfaec90SYuanhan Liu* DPDK vhost-user acts as the server. 17442683a7dSHuawei Xie 1752bfaec90SYuanhan Liu DPDK will create a Unix domain socket server file and listen for 1762bfaec90SYuanhan Liu connections from the frontend. 17742683a7dSHuawei Xie 1782bfaec90SYuanhan Liu Note, this is the default mode, and the only mode before DPDK v16.07. 17942683a7dSHuawei Xie 1802bfaec90SYuanhan Liu 1812bfaec90SYuanhan Liu* DPDK vhost-user acts as the client. 1822bfaec90SYuanhan Liu 1832bfaec90SYuanhan Liu Unlike the server mode, this mode doesn't create the socket file; 1842bfaec90SYuanhan Liu it just tries to connect to the server (which responses to create the 1852bfaec90SYuanhan Liu file instead). 1862bfaec90SYuanhan Liu 1872bfaec90SYuanhan Liu When the DPDK vhost-user application restarts, DPDK vhost-user will try to 1882bfaec90SYuanhan Liu connect to the server again. This is how the "reconnect" feature works. 1892bfaec90SYuanhan Liu 190f6ee75b5SYuanhan Liu .. Note:: 191f6ee75b5SYuanhan Liu * The "reconnect" feature requires **QEMU v2.7** (or above). 192f6ee75b5SYuanhan Liu 193f6ee75b5SYuanhan Liu * The vhost supported features must be exactly the same before and 194f6ee75b5SYuanhan Liu after the restart. For example, if TSO is disabled and then enabled, 195f6ee75b5SYuanhan Liu nothing will work and issues undefined might happen. 1962bfaec90SYuanhan Liu 1972bfaec90SYuanhan LiuNo matter which mode is used, once a connection is established, DPDK 1982bfaec90SYuanhan Liuvhost-user will start receiving and processing vhost messages from QEMU. 1992bfaec90SYuanhan Liu 2002bfaec90SYuanhan LiuFor messages with a file descriptor, the file descriptor can be used directly 2012bfaec90SYuanhan Liuin the vhost process as it is already installed by the Unix domain socket. 2022bfaec90SYuanhan Liu 2032bfaec90SYuanhan LiuThe supported vhost messages are: 2042bfaec90SYuanhan Liu 2052bfaec90SYuanhan Liu* ``VHOST_SET_MEM_TABLE`` 2062bfaec90SYuanhan Liu* ``VHOST_SET_VRING_KICK`` 2072bfaec90SYuanhan Liu* ``VHOST_SET_VRING_CALL`` 2082bfaec90SYuanhan Liu* ``VHOST_SET_LOG_FD`` 2092bfaec90SYuanhan Liu* ``VHOST_SET_VRING_ERR`` 2102bfaec90SYuanhan Liu 2112bfaec90SYuanhan LiuFor ``VHOST_SET_MEM_TABLE`` message, QEMU will send information for each 2122bfaec90SYuanhan Liumemory region and its file descriptor in the ancillary data of the message. 2132bfaec90SYuanhan LiuThe file descriptor is used to map that region. 2142bfaec90SYuanhan Liu 2152bfaec90SYuanhan Liu``VHOST_SET_VRING_KICK`` is used as the signal to put the vhost device into 2162bfaec90SYuanhan Liuthe data plane, and ``VHOST_GET_VRING_BASE`` is used as the signal to remove 2172bfaec90SYuanhan Liuthe vhost device from the data plane. 21842683a7dSHuawei Xie 21942683a7dSHuawei XieWhen the socket connection is closed, vhost will destroy the device. 22042683a7dSHuawei Xie 2210ee5e7fbSSiobhan ButlerVhost supported vSwitch reference 2220ee5e7fbSSiobhan Butler--------------------------------- 2230ee5e7fbSSiobhan Butler 2242bfaec90SYuanhan LiuFor more vhost details and how to support vhost in vSwitch, please refer to 2252bfaec90SYuanhan Liuthe vhost example in the DPDK Sample Applications Guide. 226