2
0

vhost-user.rst 73 KB

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