ppc/pegasos2: Add constants for PCI config addresses
[qemu/rayw.git] / docs / interop / vhost-user.rst
blobedc3ad84a3504a9b4b78029b23828fc4bd08085a
1 .. _vhost_user_proto:
3 ===================
4 Vhost-user Protocol
5 ===================
7 ..
8   Copyright 2014 Virtual Open Systems Sarl.
9   Copyright 2019 Intel Corporation
10   Licence: This work is licensed under the terms of the GNU GPL,
11            version 2 or later. See the COPYING file in the top-level
12            directory.
14 .. contents:: Table of Contents
16 Introduction
17 ============
19 This protocol is aiming to complement the ``ioctl`` interface used to
20 control the vhost implementation in the Linux kernel. It implements
21 the control plane needed to establish virtqueue sharing with a user
22 space process on the same host. It uses communication over a Unix
23 domain socket to share file descriptors in the ancillary data of the
24 message.
26 The protocol defines 2 sides of the communication, *master* and
27 *slave*. *Master* is the application that shares its virtqueues, in
28 our case QEMU. *Slave* is the consumer of the virtqueues.
30 In the current implementation QEMU is the *master*, and the *slave* is
31 the external process consuming the virtio queues, for example a
32 software Ethernet switch running in user space, such as Snabbswitch,
33 or a block device backend processing read & write to a virtual
34 disk. In order to facilitate interoperability between various backend
35 implementations, it is recommended to follow the :ref:`Backend program
36 conventions <backend_conventions>`.
38 *Master* and *slave* can be either a client (i.e. connecting) or
39 server (listening) in the socket communication.
41 Message Specification
42 =====================
44 .. Note:: All numbers are in the machine native byte order.
46 A vhost-user message consists of 3 header fields and a payload.
48 +---------+-------+------+---------+
49 | request | flags | size | payload |
50 +---------+-------+------+---------+
52 Header
53 ------
55 :request: 32-bit type of the request
57 :flags: 32-bit bit field
59 - Lower 2 bits are the version (currently 0x01)
60 - Bit 2 is the reply flag - needs to be sent on each reply from the slave
61 - Bit 3 is the need_reply flag - see :ref:`REPLY_ACK <reply_ack>` for
62   details.
64 :size: 32-bit size of the payload
66 Payload
67 -------
69 Depending on the request type, **payload** can be:
71 A single 64-bit integer
72 ^^^^^^^^^^^^^^^^^^^^^^^
74 +-----+
75 | u64 |
76 +-----+
78 :u64: a 64-bit unsigned integer
80 A vring state description
81 ^^^^^^^^^^^^^^^^^^^^^^^^^
83 +-------+-----+
84 | index | num |
85 +-------+-----+
87 :index: a 32-bit index
89 :num: a 32-bit number
91 A vring address description
92 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
94 +-------+-------+------+------------+------+-----------+-----+
95 | index | flags | size | descriptor | used | available | log |
96 +-------+-------+------+------------+------+-----------+-----+
98 :index: a 32-bit vring index
100 :flags: a 32-bit vring flags
102 :descriptor: a 64-bit ring address of the vring descriptor table
104 :used: a 64-bit ring address of the vring used ring
106 :available: a 64-bit ring address of the vring available ring
108 :log: a 64-bit guest address for logging
110 Note that a ring address is an IOVA if ``VIRTIO_F_IOMMU_PLATFORM`` has
111 been negotiated. Otherwise it is a user address.
113 Memory regions description
114 ^^^^^^^^^^^^^^^^^^^^^^^^^^
116 +-------------+---------+---------+-----+---------+
117 | num regions | padding | region0 | ... | region7 |
118 +-------------+---------+---------+-----+---------+
120 :num regions: a 32-bit number of regions
122 :padding: 32-bit
124 A region is:
126 +---------------+------+--------------+-------------+
127 | guest address | size | user address | mmap offset |
128 +---------------+------+--------------+-------------+
130 :guest address: a 64-bit guest address of the region
132 :size: a 64-bit size
134 :user address: a 64-bit user address
136 :mmap offset: 64-bit offset where region starts in the mapped memory
138 Single memory region description
139 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141 +---------+---------------+------+--------------+-------------+
142 | padding | guest address | size | user address | mmap offset |
143 +---------+---------------+------+--------------+-------------+
145 :padding: 64-bit
147 :guest address: a 64-bit guest address of the region
149 :size: a 64-bit size
151 :user address: a 64-bit user address
153 :mmap offset: 64-bit offset where region starts in the mapped memory
155 Log description
156 ^^^^^^^^^^^^^^^
158 +----------+------------+
159 | log size | log offset |
160 +----------+------------+
162 :log size: size of area used for logging
164 :log offset: offset from start of supplied file descriptor where
165              logging starts (i.e. where guest address 0 would be
166              logged)
168 An IOTLB message
169 ^^^^^^^^^^^^^^^^
171 +------+------+--------------+-------------------+------+
172 | iova | size | user address | permissions flags | type |
173 +------+------+--------------+-------------------+------+
175 :iova: a 64-bit I/O virtual address programmed by the guest
177 :size: a 64-bit size
179 :user address: a 64-bit user address
181 :permissions flags: an 8-bit value:
182   - 0: No access
183   - 1: Read access
184   - 2: Write access
185   - 3: Read/Write access
187 :type: an 8-bit IOTLB message type:
188   - 1: IOTLB miss
189   - 2: IOTLB update
190   - 3: IOTLB invalidate
191   - 4: IOTLB access fail
193 Virtio device config space
194 ^^^^^^^^^^^^^^^^^^^^^^^^^^
196 +--------+------+-------+---------+
197 | offset | size | flags | payload |
198 +--------+------+-------+---------+
200 :offset: a 32-bit offset of virtio device's configuration space
202 :size: a 32-bit configuration space access size in bytes
204 :flags: a 32-bit value:
205   - 0: Vhost master messages used for writeable fields
206   - 1: Vhost master messages used for live migration
208 :payload: Size bytes array holding the contents of the virtio
209           device's configuration space
211 Vring area description
212 ^^^^^^^^^^^^^^^^^^^^^^
214 +-----+------+--------+
215 | u64 | size | offset |
216 +-----+------+--------+
218 :u64: a 64-bit integer contains vring index and flags
220 :size: a 64-bit size of this area
222 :offset: a 64-bit offset of this area from the start of the
223          supplied file descriptor
225 Inflight description
226 ^^^^^^^^^^^^^^^^^^^^
228 +-----------+-------------+------------+------------+
229 | mmap size | mmap offset | num queues | queue size |
230 +-----------+-------------+------------+------------+
232 :mmap size: a 64-bit size of area to track inflight I/O
234 :mmap offset: a 64-bit offset of this area from the start
235               of the supplied file descriptor
237 :num queues: a 16-bit number of virtqueues
239 :queue size: a 16-bit size of virtqueues
241 C structure
242 -----------
244 In QEMU the vhost-user message is implemented with the following struct:
246 .. code:: c
248   typedef struct VhostUserMsg {
249       VhostUserRequest request;
250       uint32_t flags;
251       uint32_t size;
252       union {
253           uint64_t u64;
254           struct vhost_vring_state state;
255           struct vhost_vring_addr addr;
256           VhostUserMemory memory;
257           VhostUserLog log;
258           struct vhost_iotlb_msg iotlb;
259           VhostUserConfig config;
260           VhostUserVringArea area;
261           VhostUserInflight inflight;
262       };
263   } QEMU_PACKED VhostUserMsg;
265 Communication
266 =============
268 The protocol for vhost-user is based on the existing implementation of
269 vhost for the Linux Kernel. Most messages that can be sent via the
270 Unix domain socket implementing vhost-user have an equivalent ioctl to
271 the kernel implementation.
273 The communication consists of *master* sending message requests and
274 *slave* sending message replies. Most of the requests don't require
275 replies. Here is a list of the ones that do:
277 * ``VHOST_USER_GET_FEATURES``
278 * ``VHOST_USER_GET_PROTOCOL_FEATURES``
279 * ``VHOST_USER_GET_VRING_BASE``
280 * ``VHOST_USER_SET_LOG_BASE`` (if ``VHOST_USER_PROTOCOL_F_LOG_SHMFD``)
281 * ``VHOST_USER_GET_INFLIGHT_FD`` (if ``VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD``)
283 .. seealso::
285    :ref:`REPLY_ACK <reply_ack>`
286        The section on ``REPLY_ACK`` protocol extension.
288 There are several messages that the master sends with file descriptors passed
289 in the ancillary data:
291 * ``VHOST_USER_SET_MEM_TABLE``
292 * ``VHOST_USER_SET_LOG_BASE`` (if ``VHOST_USER_PROTOCOL_F_LOG_SHMFD``)
293 * ``VHOST_USER_SET_LOG_FD``
294 * ``VHOST_USER_SET_VRING_KICK``
295 * ``VHOST_USER_SET_VRING_CALL``
296 * ``VHOST_USER_SET_VRING_ERR``
297 * ``VHOST_USER_SET_SLAVE_REQ_FD``
298 * ``VHOST_USER_SET_INFLIGHT_FD`` (if ``VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD``)
300 If *master* is unable to send the full message or receives a wrong
301 reply it will close the connection. An optional reconnection mechanism
302 can be implemented.
304 If *slave* detects some error such as incompatible features, it may also
305 close the connection. This should only happen in exceptional circumstances.
307 Any protocol extensions are gated by protocol feature bits, which
308 allows full backwards compatibility on both master and slave.  As
309 older slaves don't support negotiating protocol features, a feature
310 bit was dedicated for this purpose::
312   #define VHOST_USER_F_PROTOCOL_FEATURES 30
314 Starting and stopping rings
315 ---------------------------
317 Client must only process each ring when it is started.
319 Client must only pass data between the ring and the backend, when the
320 ring is enabled.
322 If ring is started but disabled, client must process the ring without
323 talking to the backend.
325 For example, for a networking device, in the disabled state client
326 must not supply any new RX packets, but must process and discard any
327 TX packets.
329 If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the
330 ring is initialized in an enabled state.
332 If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is
333 initialized in a disabled state. Client must not pass data to/from the
334 backend until ring is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with
335 parameter 1, or after it has been disabled by
336 ``VHOST_USER_SET_VRING_ENABLE`` with parameter 0.
338 Each ring is initialized in a stopped state, client must not process
339 it until ring is started, or after it has been stopped.
341 Client must start ring upon receiving a kick (that is, detecting that
342 file descriptor is readable) on the descriptor specified by
343 ``VHOST_USER_SET_VRING_KICK`` or receiving the in-band message
344 ``VHOST_USER_VRING_KICK`` if negotiated, and stop ring upon receiving
345 ``VHOST_USER_GET_VRING_BASE``.
347 While processing the rings (whether they are enabled or not), client
348 must support changing some configuration aspects on the fly.
350 Multiple queue support
351 ----------------------
353 Many devices have a fixed number of virtqueues.  In this case the master
354 already knows the number of available virtqueues without communicating with the
355 slave.
357 Some devices do not have a fixed number of virtqueues.  Instead the maximum
358 number of virtqueues is chosen by the slave.  The number can depend on host
359 resource availability or slave implementation details.  Such devices are called
360 multiple queue devices.
362 Multiple queue support allows the slave to advertise the maximum number of
363 queues.  This is treated as a protocol extension, hence the slave has to
364 implement protocol features first. The multiple queues feature is supported
365 only when the protocol feature ``VHOST_USER_PROTOCOL_F_MQ`` (bit 0) is set.
367 The max number of queues the slave supports can be queried with message
368 ``VHOST_USER_GET_QUEUE_NUM``. Master should stop when the number of requested
369 queues is bigger than that.
371 As all queues share one connection, the master uses a unique index for each
372 queue in the sent message to identify a specified queue.
374 The master enables queues by sending message ``VHOST_USER_SET_VRING_ENABLE``.
375 vhost-user-net has historically automatically enabled the first queue pair.
377 Slaves should always implement the ``VHOST_USER_PROTOCOL_F_MQ`` protocol
378 feature, even for devices with a fixed number of virtqueues, since it is simple
379 to implement and offers a degree of introspection.
381 Masters must not rely on the ``VHOST_USER_PROTOCOL_F_MQ`` protocol feature for
382 devices with a fixed number of virtqueues.  Only true multiqueue devices
383 require this protocol feature.
385 Migration
386 ---------
388 During live migration, the master may need to track the modifications
389 the slave makes to the memory mapped regions. The client should mark
390 the dirty pages in a log. Once it complies to this logging, it may
391 declare the ``VHOST_F_LOG_ALL`` vhost feature.
393 To start/stop logging of data/used ring writes, server may send
394 messages ``VHOST_USER_SET_FEATURES`` with ``VHOST_F_LOG_ALL`` and
395 ``VHOST_USER_SET_VRING_ADDR`` with ``VHOST_VRING_F_LOG`` in ring's
396 flags set to 1/0, respectively.
398 All the modifications to memory pointed by vring "descriptor" should
399 be marked. Modifications to "used" vring should be marked if
400 ``VHOST_VRING_F_LOG`` is part of ring's flags.
402 Dirty pages are of size::
404   #define VHOST_LOG_PAGE 0x1000
406 The log memory fd is provided in the ancillary data of
407 ``VHOST_USER_SET_LOG_BASE`` message when the slave has
408 ``VHOST_USER_PROTOCOL_F_LOG_SHMFD`` protocol feature.
410 The size of the log is supplied as part of ``VhostUserMsg`` which
411 should be large enough to cover all known guest addresses. Log starts
412 at the supplied offset in the supplied file descriptor.  The log
413 covers from address 0 to the maximum of guest regions. In pseudo-code,
414 to mark page at ``addr`` as dirty::
416   page = addr / VHOST_LOG_PAGE
417   log[page / 8] |= 1 << page % 8
419 Where ``addr`` is the guest physical address.
421 Use atomic operations, as the log may be concurrently manipulated.
423 Note that when logging modifications to the used ring (when
424 ``VHOST_VRING_F_LOG`` is set for this ring), ``log_guest_addr`` should
425 be used to calculate the log offset: the write to first byte of the
426 used ring is logged at this offset from log start. Also note that this
427 value might be outside the legal guest physical address range
428 (i.e. does not have to be covered by the ``VhostUserMemory`` table), but
429 the bit offset of the last byte of the ring must fall within the size
430 supplied by ``VhostUserLog``.
432 ``VHOST_USER_SET_LOG_FD`` is an optional message with an eventfd in
433 ancillary data, it may be used to inform the master that the log has
434 been modified.
436 Once the source has finished migration, rings will be stopped by the
437 source. No further update must be done before rings are restarted.
439 In postcopy migration the slave is started before all the memory has
440 been received from the source host, and care must be taken to avoid
441 accessing pages that have yet to be received.  The slave opens a
442 'userfault'-fd and registers the memory with it; this fd is then
443 passed back over to the master.  The master services requests on the
444 userfaultfd for pages that are accessed and when the page is available
445 it performs WAKE ioctl's on the userfaultfd to wake the stalled
446 slave.  The client indicates support for this via the
447 ``VHOST_USER_PROTOCOL_F_PAGEFAULT`` feature.
449 Memory access
450 -------------
452 The master sends a list of vhost memory regions to the slave using the
453 ``VHOST_USER_SET_MEM_TABLE`` message.  Each region has two base
454 addresses: a guest address and a user address.
456 Messages contain guest addresses and/or user addresses to reference locations
457 within the shared memory.  The mapping of these addresses works as follows.
459 User addresses map to the vhost memory region containing that user address.
461 When the ``VIRTIO_F_IOMMU_PLATFORM`` feature has not been negotiated:
463 * Guest addresses map to the vhost memory region containing that guest
464   address.
466 When the ``VIRTIO_F_IOMMU_PLATFORM`` feature has been negotiated:
468 * Guest addresses are also called I/O virtual addresses (IOVAs).  They are
469   translated to user addresses via the IOTLB.
471 * The vhost memory region guest address is not used.
473 IOMMU support
474 -------------
476 When the ``VIRTIO_F_IOMMU_PLATFORM`` feature has been negotiated, the
477 master sends IOTLB entries update & invalidation by sending
478 ``VHOST_USER_IOTLB_MSG`` requests to the slave with a ``struct
479 vhost_iotlb_msg`` as payload. For update events, the ``iotlb`` payload
480 has to be filled with the update message type (2), the I/O virtual
481 address, the size, the user virtual address, and the permissions
482 flags. Addresses and size must be within vhost memory regions set via
483 the ``VHOST_USER_SET_MEM_TABLE`` request. For invalidation events, the
484 ``iotlb`` payload has to be filled with the invalidation message type
485 (3), the I/O virtual address and the size. On success, the slave is
486 expected to reply with a zero payload, non-zero otherwise.
488 The slave relies on the slave communication channel (see :ref:`Slave
489 communication <slave_communication>` section below) to send IOTLB miss
490 and access failure events, by sending ``VHOST_USER_SLAVE_IOTLB_MSG``
491 requests to the master with a ``struct vhost_iotlb_msg`` as
492 payload. For miss events, the iotlb payload has to be filled with the
493 miss message type (1), the I/O virtual address and the permissions
494 flags. For access failure event, the iotlb payload has to be filled
495 with the access failure message type (4), the I/O virtual address and
496 the permissions flags.  For synchronization purpose, the slave may
497 rely on the reply-ack feature, so the master may send a reply when
498 operation is completed if the reply-ack feature is negotiated and
499 slaves requests a reply. For miss events, completed operation means
500 either master sent an update message containing the IOTLB entry
501 containing requested address and permission, or master sent nothing if
502 the IOTLB miss message is invalid (invalid IOVA or permission).
504 The master isn't expected to take the initiative to send IOTLB update
505 messages, as the slave sends IOTLB miss messages for the guest virtual
506 memory areas it needs to access.
508 .. _slave_communication:
510 Slave communication
511 -------------------
513 An optional communication channel is provided if the slave declares
514 ``VHOST_USER_PROTOCOL_F_SLAVE_REQ`` protocol feature, to allow the
515 slave to make requests to the master.
517 The fd is provided via ``VHOST_USER_SET_SLAVE_REQ_FD`` ancillary data.
519 A slave may then send ``VHOST_USER_SLAVE_*`` messages to the master
520 using this fd communication channel.
522 If ``VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD`` protocol feature is
523 negotiated, slave can send file descriptors (at most 8 descriptors in
524 each message) to master via ancillary data using this fd communication
525 channel.
527 Inflight I/O tracking
528 ---------------------
530 To support reconnecting after restart or crash, slave may need to
531 resubmit inflight I/Os. If virtqueue is processed in order, we can
532 easily achieve that by getting the inflight descriptors from
533 descriptor table (split virtqueue) or descriptor ring (packed
534 virtqueue). However, it can't work when we process descriptors
535 out-of-order because some entries which store the information of
536 inflight descriptors in available ring (split virtqueue) or descriptor
537 ring (packed virtqueue) might be overridden by new entries. To solve
538 this problem, slave need to allocate an extra buffer to store this
539 information of inflight descriptors and share it with master for
540 persistent. ``VHOST_USER_GET_INFLIGHT_FD`` and
541 ``VHOST_USER_SET_INFLIGHT_FD`` are used to transfer this buffer
542 between master and slave. And the format of this buffer is described
543 below:
545 +---------------+---------------+-----+---------------+
546 | queue0 region | queue1 region | ... | queueN region |
547 +---------------+---------------+-----+---------------+
549 N is the number of available virtqueues. Slave could get it from num
550 queues field of ``VhostUserInflight``.
552 For split virtqueue, queue region can be implemented as:
554 .. code:: c
556   typedef struct DescStateSplit {
557       /* Indicate whether this descriptor is inflight or not.
558        * Only available for head-descriptor. */
559       uint8_t inflight;
561       /* Padding */
562       uint8_t padding[5];
564       /* Maintain a list for the last batch of used descriptors.
565        * Only available when batching is used for submitting */
566       uint16_t next;
568       /* Used to preserve the order of fetching available descriptors.
569        * Only available for head-descriptor. */
570       uint64_t counter;
571   } DescStateSplit;
573   typedef struct QueueRegionSplit {
574       /* The feature flags of this region. Now it's initialized to 0. */
575       uint64_t features;
577       /* The version of this region. It's 1 currently.
578        * Zero value indicates an uninitialized buffer */
579       uint16_t version;
581       /* The size of DescStateSplit array. It's equal to the virtqueue
582        * size. Slave could get it from queue size field of VhostUserInflight. */
583       uint16_t desc_num;
585       /* The head of list that track the last batch of used descriptors. */
586       uint16_t last_batch_head;
588       /* Store the idx value of used ring */
589       uint16_t used_idx;
591       /* Used to track the state of each descriptor in descriptor table */
592       DescStateSplit desc[];
593   } QueueRegionSplit;
595 To track inflight I/O, the queue region should be processed as follows:
597 When receiving available buffers from the driver:
599 #. Get the next available head-descriptor index from available ring, ``i``
601 #. Set ``desc[i].counter`` to the value of global counter
603 #. Increase global counter by 1
605 #. Set ``desc[i].inflight`` to 1
607 When supplying used buffers to the driver:
609 1. Get corresponding used head-descriptor index, i
611 2. Set ``desc[i].next`` to ``last_batch_head``
613 3. Set ``last_batch_head`` to ``i``
615 #. Steps 1,2,3 may be performed repeatedly if batching is possible
617 #. Increase the ``idx`` value of used ring by the size of the batch
619 #. Set the ``inflight`` field of each ``DescStateSplit`` entry in the batch to 0
621 #. Set ``used_idx`` to the ``idx`` value of used ring
623 When reconnecting:
625 #. If the value of ``used_idx`` does not match the ``idx`` value of
626    used ring (means the inflight field of ``DescStateSplit`` entries in
627    last batch may be incorrect),
629    a. Subtract the value of ``used_idx`` from the ``idx`` value of
630       used ring to get last batch size of ``DescStateSplit`` entries
632    #. Set the ``inflight`` field of each ``DescStateSplit`` entry to 0 in last batch
633       list which starts from ``last_batch_head``
635    #. Set ``used_idx`` to the ``idx`` value of used ring
637 #. Resubmit inflight ``DescStateSplit`` entries in order of their
638    counter value
640 For packed virtqueue, queue region can be implemented as:
642 .. code:: c
644   typedef struct DescStatePacked {
645       /* Indicate whether this descriptor is inflight or not.
646        * Only available for head-descriptor. */
647       uint8_t inflight;
649       /* Padding */
650       uint8_t padding;
652       /* Link to the next free entry */
653       uint16_t next;
655       /* Link to the last entry of descriptor list.
656        * Only available for head-descriptor. */
657       uint16_t last;
659       /* The length of descriptor list.
660        * Only available for head-descriptor. */
661       uint16_t num;
663       /* Used to preserve the order of fetching available descriptors.
664        * Only available for head-descriptor. */
665       uint64_t counter;
667       /* The buffer id */
668       uint16_t id;
670       /* The descriptor flags */
671       uint16_t flags;
673       /* The buffer length */
674       uint32_t len;
676       /* The buffer address */
677       uint64_t addr;
678   } DescStatePacked;
680   typedef struct QueueRegionPacked {
681       /* The feature flags of this region. Now it's initialized to 0. */
682       uint64_t features;
684       /* The version of this region. It's 1 currently.
685        * Zero value indicates an uninitialized buffer */
686       uint16_t version;
688       /* The size of DescStatePacked array. It's equal to the virtqueue
689        * size. Slave could get it from queue size field of VhostUserInflight. */
690       uint16_t desc_num;
692       /* The head of free DescStatePacked entry list */
693       uint16_t free_head;
695       /* The old head of free DescStatePacked entry list */
696       uint16_t old_free_head;
698       /* The used index of descriptor ring */
699       uint16_t used_idx;
701       /* The old used index of descriptor ring */
702       uint16_t old_used_idx;
704       /* Device ring wrap counter */
705       uint8_t used_wrap_counter;
707       /* The old device ring wrap counter */
708       uint8_t old_used_wrap_counter;
710       /* Padding */
711       uint8_t padding[7];
713       /* Used to track the state of each descriptor fetched from descriptor ring */
714       DescStatePacked desc[];
715   } QueueRegionPacked;
717 To track inflight I/O, the queue region should be processed as follows:
719 When receiving available buffers from the driver:
721 #. Get the next available descriptor entry from descriptor ring, ``d``
723 #. If ``d`` is head descriptor,
725    a. Set ``desc[old_free_head].num`` to 0
727    #. Set ``desc[old_free_head].counter`` to the value of global counter
729    #. Increase global counter by 1
731    #. Set ``desc[old_free_head].inflight`` to 1
733 #. If ``d`` is last descriptor, set ``desc[old_free_head].last`` to
734    ``free_head``
736 #. Increase ``desc[old_free_head].num`` by 1
738 #. Set ``desc[free_head].addr``, ``desc[free_head].len``,
739    ``desc[free_head].flags``, ``desc[free_head].id`` to ``d.addr``,
740    ``d.len``, ``d.flags``, ``d.id``
742 #. Set ``free_head`` to ``desc[free_head].next``
744 #. If ``d`` is last descriptor, set ``old_free_head`` to ``free_head``
746 When supplying used buffers to the driver:
748 1. Get corresponding used head-descriptor entry from descriptor ring,
749    ``d``
751 2. Get corresponding ``DescStatePacked`` entry, ``e``
753 3. Set ``desc[e.last].next`` to ``free_head``
755 4. Set ``free_head`` to the index of ``e``
757 #. Steps 1,2,3,4 may be performed repeatedly if batching is possible
759 #. Increase ``used_idx`` by the size of the batch and update
760    ``used_wrap_counter`` if needed
762 #. Update ``d.flags``
764 #. Set the ``inflight`` field of each head ``DescStatePacked`` entry
765    in the batch to 0
767 #. Set ``old_free_head``,  ``old_used_idx``, ``old_used_wrap_counter``
768    to ``free_head``, ``used_idx``, ``used_wrap_counter``
770 When reconnecting:
772 #. If ``used_idx`` does not match ``old_used_idx`` (means the
773    ``inflight`` field of ``DescStatePacked`` entries in last batch may
774    be incorrect),
776    a. Get the next descriptor ring entry through ``old_used_idx``, ``d``
778    #. Use ``old_used_wrap_counter`` to calculate the available flags
780    #. If ``d.flags`` is not equal to the calculated flags value (means
781       slave has submitted the buffer to guest driver before crash, so
782       it has to commit the in-progres update), set ``old_free_head``,
783       ``old_used_idx``, ``old_used_wrap_counter`` to ``free_head``,
784       ``used_idx``, ``used_wrap_counter``
786 #. Set ``free_head``, ``used_idx``, ``used_wrap_counter`` to
787    ``old_free_head``, ``old_used_idx``, ``old_used_wrap_counter``
788    (roll back any in-progress update)
790 #. Set the ``inflight`` field of each ``DescStatePacked`` entry in
791    free list to 0
793 #. Resubmit inflight ``DescStatePacked`` entries in order of their
794    counter value
796 In-band notifications
797 ---------------------
799 In some limited situations (e.g. for simulation) it is desirable to
800 have the kick, call and error (if used) signals done via in-band
801 messages instead of asynchronous eventfd notifications. This can be
802 done by negotiating the ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS``
803 protocol feature.
805 Note that due to the fact that too many messages on the sockets can
806 cause the sending application(s) to block, it is not advised to use
807 this feature unless absolutely necessary. It is also considered an
808 error to negotiate this feature without also negotiating
809 ``VHOST_USER_PROTOCOL_F_SLAVE_REQ`` and ``VHOST_USER_PROTOCOL_F_REPLY_ACK``,
810 the former is necessary for getting a message channel from the slave
811 to the master, while the latter needs to be used with the in-band
812 notification messages to block until they are processed, both to avoid
813 blocking later and for proper processing (at least in the simulation
814 use case.) As it has no other way of signalling this error, the slave
815 should close the connection as a response to a
816 ``VHOST_USER_SET_PROTOCOL_FEATURES`` message that sets the in-band
817 notifications feature flag without the other two.
819 Protocol features
820 -----------------
822 .. code:: c
824   #define VHOST_USER_PROTOCOL_F_MQ                    0
825   #define VHOST_USER_PROTOCOL_F_LOG_SHMFD             1
826   #define VHOST_USER_PROTOCOL_F_RARP                  2
827   #define VHOST_USER_PROTOCOL_F_REPLY_ACK             3
828   #define VHOST_USER_PROTOCOL_F_MTU                   4
829   #define VHOST_USER_PROTOCOL_F_SLAVE_REQ             5
830   #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN          6
831   #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION        7
832   #define VHOST_USER_PROTOCOL_F_PAGEFAULT             8
833   #define VHOST_USER_PROTOCOL_F_CONFIG                9
834   #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD        10
835   #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER        11
836   #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD       12
837   #define VHOST_USER_PROTOCOL_F_RESET_DEVICE         13
838   #define VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS 14
839   #define VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS  15
840   #define VHOST_USER_PROTOCOL_F_STATUS               16
842 Master message types
843 --------------------
845 ``VHOST_USER_GET_FEATURES``
846   :id: 1
847   :equivalent ioctl: ``VHOST_GET_FEATURES``
848   :master payload: N/A
849   :slave payload: ``u64``
851   Get from the underlying vhost implementation the features bitmask.
852   Feature bit ``VHOST_USER_F_PROTOCOL_FEATURES`` signals slave support
853   for ``VHOST_USER_GET_PROTOCOL_FEATURES`` and
854   ``VHOST_USER_SET_PROTOCOL_FEATURES``.
856 ``VHOST_USER_SET_FEATURES``
857   :id: 2
858   :equivalent ioctl: ``VHOST_SET_FEATURES``
859   :master payload: ``u64``
861   Enable features in the underlying vhost implementation using a
862   bitmask.  Feature bit ``VHOST_USER_F_PROTOCOL_FEATURES`` signals
863   slave support for ``VHOST_USER_GET_PROTOCOL_FEATURES`` and
864   ``VHOST_USER_SET_PROTOCOL_FEATURES``.
866 ``VHOST_USER_GET_PROTOCOL_FEATURES``
867   :id: 15
868   :equivalent ioctl: ``VHOST_GET_FEATURES``
869   :master payload: N/A
870   :slave payload: ``u64``
872   Get the protocol feature bitmask from the underlying vhost
873   implementation.  Only legal if feature bit
874   ``VHOST_USER_F_PROTOCOL_FEATURES`` is present in
875   ``VHOST_USER_GET_FEATURES``.
877 .. Note::
878    Slave that reported ``VHOST_USER_F_PROTOCOL_FEATURES`` must
879    support this message even before ``VHOST_USER_SET_FEATURES`` was
880    called.
882 ``VHOST_USER_SET_PROTOCOL_FEATURES``
883   :id: 16
884   :equivalent ioctl: ``VHOST_SET_FEATURES``
885   :master payload: ``u64``
887   Enable protocol features in the underlying vhost implementation.
889   Only legal if feature bit ``VHOST_USER_F_PROTOCOL_FEATURES`` is present in
890   ``VHOST_USER_GET_FEATURES``.
892 .. Note::
893    Slave that reported ``VHOST_USER_F_PROTOCOL_FEATURES`` must support
894    this message even before ``VHOST_USER_SET_FEATURES`` was called.
896 ``VHOST_USER_SET_OWNER``
897   :id: 3
898   :equivalent ioctl: ``VHOST_SET_OWNER``
899   :master payload: N/A
901   Issued when a new connection is established. It sets the current
902   *master* as an owner of the session. This can be used on the *slave*
903   as a "session start" flag.
905 ``VHOST_USER_RESET_OWNER``
906   :id: 4
907   :master payload: N/A
909 .. admonition:: Deprecated
911    This is no longer used. Used to be sent to request disabling all
912    rings, but some clients interpreted it to also discard connection
913    state (this interpretation would lead to bugs).  It is recommended
914    that clients either ignore this message, or use it to disable all
915    rings.
917 ``VHOST_USER_SET_MEM_TABLE``
918   :id: 5
919   :equivalent ioctl: ``VHOST_SET_MEM_TABLE``
920   :master payload: memory regions description
921   :slave payload: (postcopy only) memory regions description
923   Sets the memory map regions on the slave so it can translate the
924   vring addresses. In the ancillary data there is an array of file
925   descriptors for each memory mapped region. The size and ordering of
926   the fds matches the number and ordering of memory regions.
928   When ``VHOST_USER_POSTCOPY_LISTEN`` has been received,
929   ``SET_MEM_TABLE`` replies with the bases of the memory mapped
930   regions to the master.  The slave must have mmap'd the regions but
931   not yet accessed them and should not yet generate a userfault
932   event.
934 .. Note::
935    ``NEED_REPLY_MASK`` is not set in this case.  QEMU will then
936    reply back to the list of mappings with an empty
937    ``VHOST_USER_SET_MEM_TABLE`` as an acknowledgement; only upon
938    reception of this message may the guest start accessing the memory
939    and generating faults.
941 ``VHOST_USER_SET_LOG_BASE``
942   :id: 6
943   :equivalent ioctl: ``VHOST_SET_LOG_BASE``
944   :master payload: u64
945   :slave payload: N/A
947   Sets logging shared memory space.
949   When slave has ``VHOST_USER_PROTOCOL_F_LOG_SHMFD`` protocol feature,
950   the log memory fd is provided in the ancillary data of
951   ``VHOST_USER_SET_LOG_BASE`` message, the size and offset of shared
952   memory area provided in the message.
954 ``VHOST_USER_SET_LOG_FD``
955   :id: 7
956   :equivalent ioctl: ``VHOST_SET_LOG_FD``
957   :master payload: N/A
959   Sets the logging file descriptor, which is passed as ancillary data.
961 ``VHOST_USER_SET_VRING_NUM``
962   :id: 8
963   :equivalent ioctl: ``VHOST_SET_VRING_NUM``
964   :master payload: vring state description
966   Set the size of the queue.
968 ``VHOST_USER_SET_VRING_ADDR``
969   :id: 9
970   :equivalent ioctl: ``VHOST_SET_VRING_ADDR``
971   :master payload: vring address description
972   :slave payload: N/A
974   Sets the addresses of the different aspects of the vring.
976 ``VHOST_USER_SET_VRING_BASE``
977   :id: 10
978   :equivalent ioctl: ``VHOST_SET_VRING_BASE``
979   :master payload: vring state description
981   Sets the base offset in the available vring.
983 ``VHOST_USER_GET_VRING_BASE``
984   :id: 11
985   :equivalent ioctl: ``VHOST_USER_GET_VRING_BASE``
986   :master payload: vring state description
987   :slave payload: vring state description
989   Get the available vring base offset.
991 ``VHOST_USER_SET_VRING_KICK``
992   :id: 12
993   :equivalent ioctl: ``VHOST_SET_VRING_KICK``
994   :master payload: ``u64``
996   Set the event file descriptor for adding buffers to the vring. It is
997   passed in the ancillary data.
999   Bits (0-7) of the payload contain the vring index. Bit 8 is the
1000   invalid FD flag. This flag is set when there is no file descriptor
1001   in the ancillary data. This signals that polling should be used
1002   instead of waiting for the kick. Note that if the protocol feature
1003   ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS`` has been negotiated
1004   this message isn't necessary as the ring is also started on the
1005   ``VHOST_USER_VRING_KICK`` message, it may however still be used to
1006   set an event file descriptor (which will be preferred over the
1007   message) or to enable polling.
1009 ``VHOST_USER_SET_VRING_CALL``
1010   :id: 13
1011   :equivalent ioctl: ``VHOST_SET_VRING_CALL``
1012   :master payload: ``u64``
1014   Set the event file descriptor to signal when buffers are used. It is
1015   passed in the ancillary data.
1017   Bits (0-7) of the payload contain the vring index. Bit 8 is the
1018   invalid FD flag. This flag is set when there is no file descriptor
1019   in the ancillary data. This signals that polling will be used
1020   instead of waiting for the call. Note that if the protocol features
1021   ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS`` and
1022   ``VHOST_USER_PROTOCOL_F_SLAVE_REQ`` have been negotiated this message
1023   isn't necessary as the ``VHOST_USER_SLAVE_VRING_CALL`` message can be
1024   used, it may however still be used to set an event file descriptor
1025   or to enable polling.
1027 ``VHOST_USER_SET_VRING_ERR``
1028   :id: 14
1029   :equivalent ioctl: ``VHOST_SET_VRING_ERR``
1030   :master payload: ``u64``
1032   Set the event file descriptor to signal when error occurs. It is
1033   passed in the ancillary data.
1035   Bits (0-7) of the payload contain the vring index. Bit 8 is the
1036   invalid FD flag. This flag is set when there is no file descriptor
1037   in the ancillary data. Note that if the protocol features
1038   ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS`` and
1039   ``VHOST_USER_PROTOCOL_F_SLAVE_REQ`` have been negotiated this message
1040   isn't necessary as the ``VHOST_USER_SLAVE_VRING_ERR`` message can be
1041   used, it may however still be used to set an event file descriptor
1042   (which will be preferred over the message).
1044 ``VHOST_USER_GET_QUEUE_NUM``
1045   :id: 17
1046   :equivalent ioctl: N/A
1047   :master payload: N/A
1048   :slave payload: u64
1050   Query how many queues the backend supports.
1052   This request should be sent only when ``VHOST_USER_PROTOCOL_F_MQ``
1053   is set in queried protocol features by
1054   ``VHOST_USER_GET_PROTOCOL_FEATURES``.
1056 ``VHOST_USER_SET_VRING_ENABLE``
1057   :id: 18
1058   :equivalent ioctl: N/A
1059   :master payload: vring state description
1061   Signal slave to enable or disable corresponding vring.
1063   This request should be sent only when
1064   ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated.
1066 ``VHOST_USER_SEND_RARP``
1067   :id: 19
1068   :equivalent ioctl: N/A
1069   :master payload: ``u64``
1071   Ask vhost user backend to broadcast a fake RARP to notify the migration
1072   is terminated for guest that does not support GUEST_ANNOUNCE.
1074   Only legal if feature bit ``VHOST_USER_F_PROTOCOL_FEATURES`` is
1075   present in ``VHOST_USER_GET_FEATURES`` and protocol feature bit
1076   ``VHOST_USER_PROTOCOL_F_RARP`` is present in
1077   ``VHOST_USER_GET_PROTOCOL_FEATURES``.  The first 6 bytes of the
1078   payload contain the mac address of the guest to allow the vhost user
1079   backend to construct and broadcast the fake RARP.
1081 ``VHOST_USER_NET_SET_MTU``
1082   :id: 20
1083   :equivalent ioctl: N/A
1084   :master payload: ``u64``
1086   Set host MTU value exposed to the guest.
1088   This request should be sent only when ``VIRTIO_NET_F_MTU`` feature
1089   has been successfully negotiated, ``VHOST_USER_F_PROTOCOL_FEATURES``
1090   is present in ``VHOST_USER_GET_FEATURES`` and protocol feature bit
1091   ``VHOST_USER_PROTOCOL_F_NET_MTU`` is present in
1092   ``VHOST_USER_GET_PROTOCOL_FEATURES``.
1094   If ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, slave must
1095   respond with zero in case the specified MTU is valid, or non-zero
1096   otherwise.
1098 ``VHOST_USER_SET_SLAVE_REQ_FD``
1099   :id: 21
1100   :equivalent ioctl: N/A
1101   :master payload: N/A
1103   Set the socket file descriptor for slave initiated requests. It is passed
1104   in the ancillary data.
1106   This request should be sent only when
1107   ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, and protocol
1108   feature bit ``VHOST_USER_PROTOCOL_F_SLAVE_REQ`` bit is present in
1109   ``VHOST_USER_GET_PROTOCOL_FEATURES``.  If
1110   ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, slave must
1111   respond with zero for success, non-zero otherwise.
1113 ``VHOST_USER_IOTLB_MSG``
1114   :id: 22
1115   :equivalent ioctl: N/A (equivalent to ``VHOST_IOTLB_MSG`` message type)
1116   :master payload: ``struct vhost_iotlb_msg``
1117   :slave payload: ``u64``
1119   Send IOTLB messages with ``struct vhost_iotlb_msg`` as payload.
1121   Master sends such requests to update and invalidate entries in the
1122   device IOTLB. The slave has to acknowledge the request with sending
1123   zero as ``u64`` payload for success, non-zero otherwise.
1125   This request should be send only when ``VIRTIO_F_IOMMU_PLATFORM``
1126   feature has been successfully negotiated.
1128 ``VHOST_USER_SET_VRING_ENDIAN``
1129   :id: 23
1130   :equivalent ioctl: ``VHOST_SET_VRING_ENDIAN``
1131   :master payload: vring state description
1133   Set the endianness of a VQ for legacy devices. Little-endian is
1134   indicated with state.num set to 0 and big-endian is indicated with
1135   state.num set to 1. Other values are invalid.
1137   This request should be sent only when
1138   ``VHOST_USER_PROTOCOL_F_CROSS_ENDIAN`` has been negotiated.
1139   Backends that negotiated this feature should handle both
1140   endiannesses and expect this message once (per VQ) during device
1141   configuration (ie. before the master starts the VQ).
1143 ``VHOST_USER_GET_CONFIG``
1144   :id: 24
1145   :equivalent ioctl: N/A
1146   :master payload: virtio device config space
1147   :slave payload: virtio device config space
1149   When ``VHOST_USER_PROTOCOL_F_CONFIG`` is negotiated, this message is
1150   submitted by the vhost-user master to fetch the contents of the
1151   virtio device configuration space, vhost-user slave's payload size
1152   MUST match master's request, vhost-user slave uses zero length of
1153   payload to indicate an error to vhost-user master. The vhost-user
1154   master may cache the contents to avoid repeated
1155   ``VHOST_USER_GET_CONFIG`` calls.
1157 ``VHOST_USER_SET_CONFIG``
1158   :id: 25
1159   :equivalent ioctl: N/A
1160   :master payload: virtio device config space
1161   :slave payload: N/A
1163   When ``VHOST_USER_PROTOCOL_F_CONFIG`` is negotiated, this message is
1164   submitted by the vhost-user master when the Guest changes the virtio
1165   device configuration space and also can be used for live migration
1166   on the destination host. The vhost-user slave must check the flags
1167   field, and slaves MUST NOT accept SET_CONFIG for read-only
1168   configuration space fields unless the live migration bit is set.
1170 ``VHOST_USER_CREATE_CRYPTO_SESSION``
1171   :id: 26
1172   :equivalent ioctl: N/A
1173   :master payload: crypto session description
1174   :slave payload: crypto session description
1176   Create a session for crypto operation. The server side must return
1177   the session id, 0 or positive for success, negative for failure.
1178   This request should be sent only when
1179   ``VHOST_USER_PROTOCOL_F_CRYPTO_SESSION`` feature has been
1180   successfully negotiated.  It's a required feature for crypto
1181   devices.
1183 ``VHOST_USER_CLOSE_CRYPTO_SESSION``
1184   :id: 27
1185   :equivalent ioctl: N/A
1186   :master payload: ``u64``
1188   Close a session for crypto operation which was previously
1189   created by ``VHOST_USER_CREATE_CRYPTO_SESSION``.
1191   This request should be sent only when
1192   ``VHOST_USER_PROTOCOL_F_CRYPTO_SESSION`` feature has been
1193   successfully negotiated.  It's a required feature for crypto
1194   devices.
1196 ``VHOST_USER_POSTCOPY_ADVISE``
1197   :id: 28
1198   :master payload: N/A
1199   :slave payload: userfault fd
1201   When ``VHOST_USER_PROTOCOL_F_PAGEFAULT`` is supported, the master
1202   advises slave that a migration with postcopy enabled is underway,
1203   the slave must open a userfaultfd for later use.  Note that at this
1204   stage the migration is still in precopy mode.
1206 ``VHOST_USER_POSTCOPY_LISTEN``
1207   :id: 29
1208   :master payload: N/A
1210   Master advises slave that a transition to postcopy mode has
1211   happened.  The slave must ensure that shared memory is registered
1212   with userfaultfd to cause faulting of non-present pages.
1214   This is always sent sometime after a ``VHOST_USER_POSTCOPY_ADVISE``,
1215   and thus only when ``VHOST_USER_PROTOCOL_F_PAGEFAULT`` is supported.
1217 ``VHOST_USER_POSTCOPY_END``
1218   :id: 30
1219   :slave payload: ``u64``
1221   Master advises that postcopy migration has now completed.  The slave
1222   must disable the userfaultfd. The response is an acknowledgement
1223   only.
1225   When ``VHOST_USER_PROTOCOL_F_PAGEFAULT`` is supported, this message
1226   is sent at the end of the migration, after
1227   ``VHOST_USER_POSTCOPY_LISTEN`` was previously sent.
1229   The value returned is an error indication; 0 is success.
1231 ``VHOST_USER_GET_INFLIGHT_FD``
1232   :id: 31
1233   :equivalent ioctl: N/A
1234   :master payload: inflight description
1236   When ``VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD`` protocol feature has
1237   been successfully negotiated, this message is submitted by master to
1238   get a shared buffer from slave. The shared buffer will be used to
1239   track inflight I/O by slave. QEMU should retrieve a new one when vm
1240   reset.
1242 ``VHOST_USER_SET_INFLIGHT_FD``
1243   :id: 32
1244   :equivalent ioctl: N/A
1245   :master payload: inflight description
1247   When ``VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD`` protocol feature has
1248   been successfully negotiated, this message is submitted by master to
1249   send the shared inflight buffer back to slave so that slave could
1250   get inflight I/O after a crash or restart.
1252 ``VHOST_USER_GPU_SET_SOCKET``
1253   :id: 33
1254   :equivalent ioctl: N/A
1255   :master payload: N/A
1257   Sets the GPU protocol socket file descriptor, which is passed as
1258   ancillary data. The GPU protocol is used to inform the master of
1259   rendering state and updates. See vhost-user-gpu.rst for details.
1261 ``VHOST_USER_RESET_DEVICE``
1262   :id: 34
1263   :equivalent ioctl: N/A
1264   :master payload: N/A
1265   :slave payload: N/A
1267   Ask the vhost user backend to disable all rings and reset all
1268   internal device state to the initial state, ready to be
1269   reinitialized. The backend retains ownership of the device
1270   throughout the reset operation.
1272   Only valid if the ``VHOST_USER_PROTOCOL_F_RESET_DEVICE`` protocol
1273   feature is set by the backend.
1275 ``VHOST_USER_VRING_KICK``
1276   :id: 35
1277   :equivalent ioctl: N/A
1278   :slave payload: vring state description
1279   :master payload: N/A
1281   When the ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS`` protocol
1282   feature has been successfully negotiated, this message may be
1283   submitted by the master to indicate that a buffer was added to
1284   the vring instead of signalling it using the vring's kick file
1285   descriptor or having the slave rely on polling.
1287   The state.num field is currently reserved and must be set to 0.
1289 ``VHOST_USER_GET_MAX_MEM_SLOTS``
1290   :id: 36
1291   :equivalent ioctl: N/A
1292   :slave payload: u64
1294   When the ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol
1295   feature has been successfully negotiated, this message is submitted
1296   by master to the slave. The slave should return the message with a
1297   u64 payload containing the maximum number of memory slots for
1298   QEMU to expose to the guest. The value returned by the backend
1299   will be capped at the maximum number of ram slots which can be
1300   supported by the target platform.
1302 ``VHOST_USER_ADD_MEM_REG``
1303   :id: 37
1304   :equivalent ioctl: N/A
1305   :slave payload: single memory region description
1307   When the ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol
1308   feature has been successfully negotiated, this message is submitted
1309   by the master to the slave. The message payload contains a memory
1310   region descriptor struct, describing a region of guest memory which
1311   the slave device must map in. When the
1312   ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol feature has
1313   been successfully negotiated, along with the
1314   ``VHOST_USER_REM_MEM_REG`` message, this message is used to set and
1315   update the memory tables of the slave device.
1317 ``VHOST_USER_REM_MEM_REG``
1318   :id: 38
1319   :equivalent ioctl: N/A
1320   :slave payload: single memory region description
1322   When the ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol
1323   feature has been successfully negotiated, this message is submitted
1324   by the master to the slave. The message payload contains a memory
1325   region descriptor struct, describing a region of guest memory which
1326   the slave device must unmap. When the
1327   ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol feature has
1328   been successfully negotiated, along with the
1329   ``VHOST_USER_ADD_MEM_REG`` message, this message is used to set and
1330   update the memory tables of the slave device.
1332 ``VHOST_USER_SET_STATUS``
1333   :id: 39
1334   :equivalent ioctl: VHOST_VDPA_SET_STATUS
1335   :slave payload: N/A
1336   :master payload: ``u64``
1338   When the ``VHOST_USER_PROTOCOL_F_STATUS`` protocol feature has been
1339   successfully negotiated, this message is submitted by the master to
1340   notify the backend with updated device status as defined in the Virtio
1341   specification.
1343 ``VHOST_USER_GET_STATUS``
1344   :id: 40
1345   :equivalent ioctl: VHOST_VDPA_GET_STATUS
1346   :slave payload: ``u64``
1347   :master payload: N/A
1349   When the ``VHOST_USER_PROTOCOL_F_STATUS`` protocol feature has been
1350   successfully negotiated, this message is submitted by the master to
1351   query the backend for its device status as defined in the Virtio
1352   specification.
1355 Slave message types
1356 -------------------
1358 ``VHOST_USER_SLAVE_IOTLB_MSG``
1359   :id: 1
1360   :equivalent ioctl: N/A (equivalent to ``VHOST_IOTLB_MSG`` message type)
1361   :slave payload: ``struct vhost_iotlb_msg``
1362   :master payload: N/A
1364   Send IOTLB messages with ``struct vhost_iotlb_msg`` as payload.
1365   Slave sends such requests to notify of an IOTLB miss, or an IOTLB
1366   access failure. If ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is
1367   negotiated, and slave set the ``VHOST_USER_NEED_REPLY`` flag, master
1368   must respond with zero when operation is successfully completed, or
1369   non-zero otherwise.  This request should be send only when
1370   ``VIRTIO_F_IOMMU_PLATFORM`` feature has been successfully
1371   negotiated.
1373 ``VHOST_USER_SLAVE_CONFIG_CHANGE_MSG``
1374   :id: 2
1375   :equivalent ioctl: N/A
1376   :slave payload: N/A
1377   :master payload: N/A
1379   When ``VHOST_USER_PROTOCOL_F_CONFIG`` is negotiated, vhost-user
1380   slave sends such messages to notify that the virtio device's
1381   configuration space has changed, for those host devices which can
1382   support such feature, host driver can send ``VHOST_USER_GET_CONFIG``
1383   message to slave to get the latest content. If
1384   ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, and slave set the
1385   ``VHOST_USER_NEED_REPLY`` flag, master must respond with zero when
1386   operation is successfully completed, or non-zero otherwise.
1388 ``VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG``
1389   :id: 3
1390   :equivalent ioctl: N/A
1391   :slave payload: vring area description
1392   :master payload: N/A
1394   Sets host notifier for a specified queue. The queue index is
1395   contained in the ``u64`` field of the vring area description. The
1396   host notifier is described by the file descriptor (typically it's a
1397   VFIO device fd) which is passed as ancillary data and the size
1398   (which is mmap size and should be the same as host page size) and
1399   offset (which is mmap offset) carried in the vring area
1400   description. QEMU can mmap the file descriptor based on the size and
1401   offset to get a memory range. Registering a host notifier means
1402   mapping this memory range to the VM as the specified queue's notify
1403   MMIO region. Slave sends this request to tell QEMU to de-register
1404   the existing notifier if any and register the new notifier if the
1405   request is sent with a file descriptor.
1407   This request should be sent only when
1408   ``VHOST_USER_PROTOCOL_F_HOST_NOTIFIER`` protocol feature has been
1409   successfully negotiated.
1411 ``VHOST_USER_SLAVE_VRING_CALL``
1412   :id: 4
1413   :equivalent ioctl: N/A
1414   :slave payload: vring state description
1415   :master payload: N/A
1417   When the ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS`` protocol
1418   feature has been successfully negotiated, this message may be
1419   submitted by the slave to indicate that a buffer was used from
1420   the vring instead of signalling this using the vring's call file
1421   descriptor or having the master relying on polling.
1423   The state.num field is currently reserved and must be set to 0.
1425 ``VHOST_USER_SLAVE_VRING_ERR``
1426   :id: 5
1427   :equivalent ioctl: N/A
1428   :slave payload: vring state description
1429   :master payload: N/A
1431   When the ``VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS`` protocol
1432   feature has been successfully negotiated, this message may be
1433   submitted by the slave to indicate that an error occurred on the
1434   specific vring, instead of signalling the error file descriptor
1435   set by the master via ``VHOST_USER_SET_VRING_ERR``.
1437   The state.num field is currently reserved and must be set to 0.
1439 .. _reply_ack:
1441 VHOST_USER_PROTOCOL_F_REPLY_ACK
1442 -------------------------------
1444 The original vhost-user specification only demands replies for certain
1445 commands. This differs from the vhost protocol implementation where
1446 commands are sent over an ``ioctl()`` call and block until the client
1447 has completed.
1449 With this protocol extension negotiated, the sender (QEMU) can set the
1450 ``need_reply`` [Bit 3] flag to any command. This indicates that the
1451 client MUST respond with a Payload ``VhostUserMsg`` indicating success
1452 or failure. The payload should be set to zero on success or non-zero
1453 on failure, unless the message already has an explicit reply body.
1455 The response payload gives QEMU a deterministic indication of the result
1456 of the command. Today, QEMU is expected to terminate the main vhost-user
1457 loop upon receiving such errors. In future, qemu could be taught to be more
1458 resilient for selective requests.
1460 For the message types that already solicit a reply from the client,
1461 the presence of ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` or need_reply bit
1462 being set brings no behavioural change. (See the Communication_
1463 section for details.)
1465 .. _backend_conventions:
1467 Backend program conventions
1468 ===========================
1470 vhost-user backends can provide various devices & services and may
1471 need to be configured manually depending on the use case. However, it
1472 is a good idea to follow the conventions listed here when
1473 possible. Users, QEMU or libvirt, can then rely on some common
1474 behaviour to avoid heterogeneous configuration and management of the
1475 backend programs and facilitate interoperability.
1477 Each backend installed on a host system should come with at least one
1478 JSON file that conforms to the vhost-user.json schema. Each file
1479 informs the management applications about the backend type, and binary
1480 location. In addition, it defines rules for management apps for
1481 picking the highest priority backend when multiple match the search
1482 criteria (see ``@VhostUserBackend`` documentation in the schema file).
1484 If the backend is not capable of enabling a requested feature on the
1485 host (such as 3D acceleration with virgl), or the initialization
1486 failed, the backend should fail to start early and exit with a status
1487 != 0. It may also print a message to stderr for further details.
1489 The backend program must not daemonize itself, but it may be
1490 daemonized by the management layer. It may also have a restricted
1491 access to the system.
1493 File descriptors 0, 1 and 2 will exist, and have regular
1494 stdin/stdout/stderr usage (they may have been redirected to /dev/null
1495 by the management layer, or to a log handler).
1497 The backend program must end (as quickly and cleanly as possible) when
1498 the SIGTERM signal is received. Eventually, it may receive SIGKILL by
1499 the management layer after a few seconds.
1501 The following command line options have an expected behaviour. They
1502 are mandatory, unless explicitly said differently:
1504 --socket-path=PATH
1506   This option specify the location of the vhost-user Unix domain socket.
1507   It is incompatible with --fd.
1509 --fd=FDNUM
1511   When this argument is given, the backend program is started with the
1512   vhost-user socket as file descriptor FDNUM. It is incompatible with
1513   --socket-path.
1515 --print-capabilities
1517   Output to stdout the backend capabilities in JSON format, and then
1518   exit successfully. Other options and arguments should be ignored, and
1519   the backend program should not perform its normal function.  The
1520   capabilities can be reported dynamically depending on the host
1521   capabilities.
1523 The JSON output is described in the ``vhost-user.json`` schema, by
1524 ```@VHostUserBackendCapabilities``.  Example:
1526 .. code:: json
1528   {
1529     "type": "foo",
1530     "features": [
1531       "feature-a",
1532       "feature-b"
1533     ]
1534   }
1536 vhost-user-input
1537 ----------------
1539 Command line options:
1541 --evdev-path=PATH
1543   Specify the linux input device.
1545   (optional)
1547 --no-grab
1549   Do no request exclusive access to the input device.
1551   (optional)
1553 vhost-user-gpu
1554 --------------
1556 Command line options:
1558 --render-node=PATH
1560   Specify the GPU DRM render node.
1562   (optional)
1564 --virgl
1566   Enable virgl rendering support.
1568   (optional)
1570 vhost-user-blk
1571 --------------
1573 Command line options:
1575 --blk-file=PATH
1577   Specify block device or file path.
1579   (optional)
1581 --read-only
1583   Enable read-only.
1585   (optional)