doc: update async vhost register/unregister
[dpdk.git] / doc / guides / prog_guide / vhost_lib.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2010-2016 Intel Corporation.
3
4 Vhost Library
5 =============
6
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:
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
26 Vhost API Overview
27 ------------------
28
29 The 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 data
109     path allows applications to register async copy devices (typically
110     hardware DMA channels) to the vhost queues. Vhost leverages the copy
111     device registered to free CPU from memory copy operations. A set of
112     async data path APIs are defined for DPDK applications to make use of
113     the async capability. Only packets enqueued/dequeued by async APIs are
114     processed through the async 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_driver_set_features(path, features)``
122
123   This function sets the feature bits the vhost-user driver supports. The
124   vhost-user driver could be vhost-user net, yet it could be something else,
125   say, vhost-user SCSI.
126
127 * ``rte_vhost_driver_callback_register(path, vhost_device_ops)``
128
129   This function registers a set of callbacks, to let DPDK applications take
130   the appropriate action when some events happen. The following events are
131   currently supported:
132
133   * ``new_device(int vid)``
134
135     This callback is invoked when a virtio device becomes ready. ``vid``
136     is the vhost device ID.
137
138   * ``destroy_device(int vid)``
139
140     This callback is invoked when a virtio device is paused or shut down.
141
142   * ``vring_state_changed(int vid, uint16_t queue_id, int enable)``
143
144     This callback is invoked when a specific queue's state is changed, for
145     example to enabled or disabled.
146
147   * ``features_changed(int vid, uint64_t features)``
148
149     This callback is invoked when the features is changed. For example,
150     ``VHOST_F_LOG_ALL`` will be set/cleared at the start/end of live
151     migration, respectively.
152
153   * ``new_connection(int vid)``
154
155     This callback is invoked on new vhost-user socket connection. If DPDK
156     acts as the server the device should not be deleted before
157     ``destroy_connection`` callback is received.
158
159   * ``destroy_connection(int vid)``
160
161     This callback is invoked when vhost-user socket connection is closed.
162     It indicates that device with id ``vid`` is no longer in use and can be
163     safely deleted.
164
165 * ``rte_vhost_driver_disable/enable_features(path, features))``
166
167   This function disables/enables some features. For example, it can be used to
168   disable mergeable buffers and TSO features, which both are enabled by
169   default.
170
171 * ``rte_vhost_driver_start(path)``
172
173   This function triggers the vhost-user negotiation. It should be invoked at
174   the end of initializing a vhost-user driver.
175
176 * ``rte_vhost_enqueue_burst(vid, queue_id, pkts, count)``
177
178   Transmits (enqueues) ``count`` packets from host to guest.
179
180 * ``rte_vhost_dequeue_burst(vid, queue_id, mbuf_pool, pkts, count)``
181
182   Receives (dequeues) ``count`` packets from guest, and stored them at ``pkts``.
183
184 * ``rte_vhost_crypto_create(vid, cryptodev_id, sess_mempool, socket_id)``
185
186   As an extension of new_device(), this function adds virtio-crypto workload
187   acceleration capability to the device. All crypto workload is processed by
188   DPDK cryptodev with the device ID of ``cryptodev_id``.
189
190 * ``rte_vhost_crypto_free(vid)``
191
192   Frees the memory and vhost-user message handlers created in
193   rte_vhost_crypto_create().
194
195 * ``rte_vhost_crypto_fetch_requests(vid, queue_id, ops, nb_ops)``
196
197   Receives (dequeues) ``nb_ops`` virtio-crypto requests from guest, parses
198   them to DPDK Crypto Operations, and fills the ``ops`` with parsing results.
199
200 * ``rte_vhost_crypto_finalize_requests(queue_id, ops, nb_ops)``
201
202   After the ``ops`` are dequeued from Cryptodev, finalizes the jobs and
203   notifies the guest(s).
204
205 * ``rte_vhost_crypto_set_zero_copy(vid, option)``
206
207   Enable or disable zero copy feature of the vhost crypto backend.
208
209 * ``rte_vhost_async_channel_register(vid, queue_id, features, ops)``
210
211   Register a vhost queue with async copy device channel after vring
212   is enabled. Following device ``features`` must be specified together
213   with the registration:
214
215   * ``async_inorder``
216
217     Async copy device can guarantee the ordering of copy completion
218     sequence. Copies are completed in the same order with that at
219     the submission time.
220
221     Currently, only ``async_inorder`` capable device is supported by vhost.
222
223   * ``async_threshold``
224
225     The copy length (in bytes) below which CPU copy will be used even if
226     applications call async vhost APIs to enqueue/dequeue data.
227
228     Typical value is 512~1024 depending on the async device capability.
229
230   Applications must provide following ``ops`` callbacks for vhost lib to
231   work with the async copy devices:
232
233   * ``transfer_data(vid, queue_id, descs, opaque_data, count)``
234
235     vhost invokes this function to submit copy data to the async devices.
236     For non-async_inorder capable devices, ``opaque_data`` could be used
237     for identifying the completed packets.
238
239   * ``check_completed_copies(vid, queue_id, opaque_data, max_packets)``
240
241     vhost invokes this function to get the copy data completed by async
242     devices.
243
244 * ``rte_vhost_async_channel_unregister(vid, queue_id)``
245
246   Unregister the async copy device channel from a vhost queue.
247   Unregistration will fail, if the vhost queue has in-flight
248   packets that are not completed.
249
250   Unregister async copy devices in vring_state_changed() may
251   fail, as this API tries to acquire the spinlock of vhost
252   queue. The recommended way is to unregister async copy
253   devices for all vhost queues in destroy_device(), when a
254   virtio device is paused or shut down.
255
256 * ``rte_vhost_submit_enqueue_burst(vid, queue_id, pkts, count, comp_pkts, comp_count)``
257
258   Submit an enqueue request to transmit ``count`` packets from host to guest
259   by async data path. Successfully enqueued packets can be transfer completed
260   or being occupied by DMA engines; transfer completed packets are returned in
261   ``comp_pkts``, but others are not guaranteed to finish, when this API
262   call returns.
263
264   Applications must not free the packets submitted for enqueue until the
265   packets are completed.
266
267 * ``rte_vhost_poll_enqueue_completed(vid, queue_id, pkts, count)``
268
269   Poll enqueue completion status from async data path. Completed packets
270   are returned to applications through ``pkts``.
271
272 Vhost-user Implementations
273 --------------------------
274
275 Vhost-user uses Unix domain sockets for passing messages. This means the DPDK
276 vhost-user implementation has two options:
277
278 * DPDK vhost-user acts as the server.
279
280   DPDK will create a Unix domain socket server file and listen for
281   connections from the frontend.
282
283   Note, this is the default mode, and the only mode before DPDK v16.07.
284
285
286 * DPDK vhost-user acts as the client.
287
288   Unlike the server mode, this mode doesn't create the socket file;
289   it just tries to connect to the server (which responses to create the
290   file instead).
291
292   When the DPDK vhost-user application restarts, DPDK vhost-user will try to
293   connect to the server again. This is how the "reconnect" feature works.
294
295   .. Note::
296      * The "reconnect" feature requires **QEMU v2.7** (or above).
297
298      * The vhost supported features must be exactly the same before and
299        after the restart. For example, if TSO is disabled and then enabled,
300        nothing will work and issues undefined might happen.
301
302 No matter which mode is used, once a connection is established, DPDK
303 vhost-user will start receiving and processing vhost messages from QEMU.
304
305 For messages with a file descriptor, the file descriptor can be used directly
306 in the vhost process as it is already installed by the Unix domain socket.
307
308 The supported vhost messages are:
309
310 * ``VHOST_SET_MEM_TABLE``
311 * ``VHOST_SET_VRING_KICK``
312 * ``VHOST_SET_VRING_CALL``
313 * ``VHOST_SET_LOG_FD``
314 * ``VHOST_SET_VRING_ERR``
315
316 For ``VHOST_SET_MEM_TABLE`` message, QEMU will send information for each
317 memory region and its file descriptor in the ancillary data of the message.
318 The file descriptor is used to map that region.
319
320 ``VHOST_SET_VRING_KICK`` is used as the signal to put the vhost device into
321 the data plane, and ``VHOST_GET_VRING_BASE`` is used as the signal to remove
322 the vhost device from the data plane.
323
324 When the socket connection is closed, vhost will destroy the device.
325
326 Guest memory requirement
327 ------------------------
328
329 * Memory pre-allocation
330
331   For non-async data path, guest memory pre-allocation is not a
332   must. This can help save of memory. If users really want the guest memory
333   to be pre-allocated (e.g., for performance reason), we can add option
334   ``-mem-prealloc`` when starting QEMU. Or, we can lock all memory at vhost
335   side which will force memory to be allocated when mmap at vhost side;
336   option --mlockall in ovs-dpdk is an example in hand.
337
338   For async data path, we force the VM memory to be pre-allocated at vhost
339   lib when mapping the guest memory; and also we need to lock the memory to
340   prevent pages being swapped out to disk.
341
342 * Memory sharing
343
344   Make sure ``share=on`` QEMU option is given. vhost-user will not work with
345   a QEMU version without shared memory mapping.
346
347 Vhost supported vSwitch reference
348 ---------------------------------
349
350 For more vhost details and how to support vhost in vSwitch, please refer to
351 the vhost example in the DPDK Sample Applications Guide.
352
353 Vhost data path acceleration (vDPA)
354 -----------------------------------
355
356 vDPA supports selective datapath in vhost-user lib by enabling virtio ring
357 compatible devices to serve virtio driver directly for datapath acceleration.
358
359 ``rte_vhost_driver_attach_vdpa_device`` is used to configure the vhost device
360 with accelerated backend.
361
362 Also vhost device capabilities are made configurable to adopt various devices.
363 Such capabilities include supported features, protocol features, queue number.
364
365 Finally, a set of device ops is defined for device specific operations:
366
367 * ``get_queue_num``
368
369   Called to get supported queue number of the device.
370
371 * ``get_features``
372
373   Called to get supported features of the device.
374
375 * ``get_protocol_features``
376
377   Called to get supported protocol features of the device.
378
379 * ``dev_conf``
380
381   Called to configure the actual device when the virtio device becomes ready.
382
383 * ``dev_close``
384
385   Called to close the actual device when the virtio device is stopped.
386
387 * ``set_vring_state``
388
389   Called to change the state of the vring in the actual device when vring state
390   changes.
391
392 * ``set_features``
393
394   Called to set the negotiated features to device.
395
396 * ``migration_done``
397
398   Called to allow the device to response to RARP sending.
399
400 * ``get_vfio_group_fd``
401
402    Called to get the VFIO group fd of the device.
403
404 * ``get_vfio_device_fd``
405
406   Called to get the VFIO device fd of the device.
407
408 * ``get_notify_area``
409
410   Called to get the notify area info of the queue.