xref: /dpdk/doc/guides/prog_guide/vhost_lib.rst (revision 5644a1f6924a2d833d098358762e2409f181d35f)
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_DEQUEUE_ZERO_COPY``
55
56    Dequeue zero copy will be enabled when this flag is set. It is disabled by
57    default.
58
59    There are some truths (including limitations) you might want to know while
60    setting this flag:
61
62    * zero copy is not good for small packets (typically for packet size below
63      512).
64
65    * zero copy is really good for VM2VM case. For iperf between two VMs, the
66      boost could be above 70% (when TSO is enableld).
67
68    * for VM2NIC case, the ``nb_tx_desc`` has to be small enough: <= 64 if virtio
69      indirect feature is not enabled and <= 128 if it is enabled.
70
71      This is because when dequeue zero copy is enabled, guest Tx used vring will
72      be updated only when corresponding mbuf is freed. Thus, the nb_tx_desc
73      has to be small enough so that the PMD driver will run out of available
74      Tx descriptors and free mbufs timely. Otherwise, guest Tx vring would be
75      starved.
76
77    * Guest memory should be backended with huge pages to achieve better
78      performance. Using 1G page size is the best.
79
80      When dequeue zero copy is enabled, the guest phys address and host phys
81      address mapping has to be established. Using non-huge pages means far
82      more page segments. To make it simple, DPDK vhost does a linear search
83      of those segments, thus the fewer the segments, the quicker we will get
84      the mapping. NOTE: we may speed it by using tree searching in future.
85
86  - ``RTE_VHOST_USER_IOMMU_SUPPORT``
87
88    IOMMU support will be enabled when this flag is set. It is disabled by
89    default.
90
91    Enabling this flag makes possible to use guest vIOMMU to protect vhost
92    from accessing memory the virtio device isn't allowed to, when the feature
93    is negotiated and an IOMMU device is declared.
94
95    However, this feature enables vhost-user's reply-ack protocol feature,
96    which implementation is buggy in Qemu v2.7.0-v2.9.0 when doing multiqueue.
97    Enabling this flag with these Qemu version results in Qemu being blocked
98    when multiple queue pairs are declared.
99
100* ``rte_vhost_driver_set_features(path, features)``
101
102  This function sets the feature bits the vhost-user driver supports. The
103  vhost-user driver could be vhost-user net, yet it could be something else,
104  say, vhost-user SCSI.
105
106* ``rte_vhost_driver_callback_register(path, vhost_device_ops)``
107
108  This function registers a set of callbacks, to let DPDK applications take
109  the appropriate action when some events happen. The following events are
110  currently supported:
111
112  * ``new_device(int vid)``
113
114    This callback is invoked when a virtio device becomes ready. ``vid``
115    is the vhost device ID.
116
117  * ``destroy_device(int vid)``
118
119    This callback is invoked when a virtio device is paused or shut down.
120
121  * ``vring_state_changed(int vid, uint16_t queue_id, int enable)``
122
123    This callback is invoked when a specific queue's state is changed, for
124    example to enabled or disabled.
125
126  * ``features_changed(int vid, uint64_t features)``
127
128    This callback is invoked when the features is changed. For example,
129    ``VHOST_F_LOG_ALL`` will be set/cleared at the start/end of live
130    migration, respectively.
131
132  * ``new_connection(int vid)``
133
134    This callback is invoked on new vhost-user socket connection. If DPDK
135    acts as the server the device should not be deleted before
136    ``destroy_connection`` callback is received.
137
138  * ``destroy_connection(int vid)``
139
140    This callback is invoked when vhost-user socket connection is closed.
141    It indicates that device with id ``vid`` is no longer in use and can be
142    safely deleted.
143
144* ``rte_vhost_driver_disable/enable_features(path, features))``
145
146  This function disables/enables some features. For example, it can be used to
147  disable mergeable buffers and TSO features, which both are enabled by
148  default.
149
150* ``rte_vhost_driver_start(path)``
151
152  This function triggers the vhost-user negotiation. It should be invoked at
153  the end of initializing a vhost-user driver.
154
155* ``rte_vhost_enqueue_burst(vid, queue_id, pkts, count)``
156
157  Transmits (enqueues) ``count`` packets from host to guest.
158
159* ``rte_vhost_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count)``
160
161  Receives (dequeues) ``count`` packets from guest, and stored them at ``pkts``.
162
163Vhost-user Implementations
164--------------------------
165
166Vhost-user uses Unix domain sockets for passing messages. This means the DPDK
167vhost-user implementation has two options:
168
169* DPDK vhost-user acts as the server.
170
171  DPDK will create a Unix domain socket server file and listen for
172  connections from the frontend.
173
174  Note, this is the default mode, and the only mode before DPDK v16.07.
175
176
177* DPDK vhost-user acts as the client.
178
179  Unlike the server mode, this mode doesn't create the socket file;
180  it just tries to connect to the server (which responses to create the
181  file instead).
182
183  When the DPDK vhost-user application restarts, DPDK vhost-user will try to
184  connect to the server again. This is how the "reconnect" feature works.
185
186  .. Note::
187     * The "reconnect" feature requires **QEMU v2.7** (or above).
188
189     * The vhost supported features must be exactly the same before and
190       after the restart. For example, if TSO is disabled and then enabled,
191       nothing will work and issues undefined might happen.
192
193No matter which mode is used, once a connection is established, DPDK
194vhost-user will start receiving and processing vhost messages from QEMU.
195
196For messages with a file descriptor, the file descriptor can be used directly
197in the vhost process as it is already installed by the Unix domain socket.
198
199The supported vhost messages are:
200
201* ``VHOST_SET_MEM_TABLE``
202* ``VHOST_SET_VRING_KICK``
203* ``VHOST_SET_VRING_CALL``
204* ``VHOST_SET_LOG_FD``
205* ``VHOST_SET_VRING_ERR``
206
207For ``VHOST_SET_MEM_TABLE`` message, QEMU will send information for each
208memory region and its file descriptor in the ancillary data of the message.
209The file descriptor is used to map that region.
210
211``VHOST_SET_VRING_KICK`` is used as the signal to put the vhost device into
212the data plane, and ``VHOST_GET_VRING_BASE`` is used as the signal to remove
213the vhost device from the data plane.
214
215When the socket connection is closed, vhost will destroy the device.
216
217Guest memory requirement
218------------------------
219
220* Memory pre-allocation
221
222  For non-zerocopy, guest memory pre-allocation is not a must. This can help
223  save of memory. If users really want the guest memory to be pre-allocated
224  (e.g., for performance reason), we can add option ``-mem-prealloc`` when
225  starting QEMU. Or, we can lock all memory at vhost side which will force
226  memory to be allocated when mmap at vhost side; option --mlockall in
227  ovs-dpdk is an example in hand.
228
229  For zerocopy, we force the VM memory to be pre-allocated at vhost lib when
230  mapping the guest memory; and also we need to lock the memory to prevent
231  pages being swapped out to disk.
232
233* Memory sharing
234
235  Make sure ``share=on`` QEMU option is given. vhost-user will not work with
236  a QEMU version without shared memory mapping.
237
238Vhost supported vSwitch reference
239---------------------------------
240
241For more vhost details and how to support vhost in vSwitch, please refer to
242the vhost example in the DPDK Sample Applications Guide.
243