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