qmp-spec.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. ..
  2. Copyright (C) 2009-2016 Red Hat, Inc.
  3. This work is licensed under the terms of the GNU GPL, version 2 or
  4. later. See the COPYING file in the top-level directory.
  5. ===================================
  6. QEMU Machine Protocol Specification
  7. ===================================
  8. The QEMU Machine Protocol (QMP) is a JSON-based
  9. protocol which is available for applications to operate QEMU at the
  10. machine-level. It is also in use by the QEMU Guest Agent (QGA), which
  11. is available for host applications to interact with the guest
  12. operating system. This page specifies the general format of
  13. the protocol; details of the commands and data structures can
  14. be found in the :doc:`qemu-qmp-ref` and the :doc:`qemu-ga-ref`.
  15. .. contents::
  16. Protocol Specification
  17. ======================
  18. This section details the protocol format. For the purpose of this
  19. document, "Server" is either QEMU or the QEMU Guest Agent, and
  20. "Client" is any application communicating with it via QMP.
  21. JSON data structures, when mentioned in this document, are always in the
  22. following format:
  23. json-DATA-STRUCTURE-NAME
  24. Where DATA-STRUCTURE-NAME is any valid JSON data structure, as defined
  25. by the `JSON standard <http://www.ietf.org/rfc/rfc8259.txt>`_.
  26. The server expects its input to be encoded in UTF-8, and sends its
  27. output encoded in ASCII.
  28. For convenience, json-object members mentioned in this document will
  29. be in a certain order. However, in real protocol usage they can be in
  30. ANY order, thus no particular order should be assumed. On the other
  31. hand, use of json-array elements presumes that preserving order is
  32. important unless specifically documented otherwise. Repeating a key
  33. within a json-object gives unpredictable results.
  34. Also for convenience, the server will accept an extension of
  35. ``'single-quoted'`` strings in place of the usual ``"double-quoted"``
  36. json-string, and both input forms of strings understand an additional
  37. escape sequence of ``\'`` for a single quote. The server will only use
  38. double quoting on output.
  39. General Definitions
  40. -------------------
  41. All interactions transmitted by the Server are json-objects, always
  42. terminating with CRLF.
  43. All json-objects members are mandatory when not specified otherwise.
  44. Server Greeting
  45. ---------------
  46. Right when connected the Server will issue a greeting message, which signals
  47. that the connection has been successfully established and that the Server is
  48. ready for capabilities negotiation (for more information refer to section
  49. `Capabilities Negotiation`_).
  50. The greeting message format is:
  51. ::
  52. { "QMP": { "version": json-object, "capabilities": json-array } }
  53. Where:
  54. - The ``version`` member contains the Server's version information (the format
  55. is the same as for the query-version command).
  56. - The ``capabilities`` member specifies the availability of features beyond the
  57. baseline specification; the order of elements in this array has no
  58. particular significance.
  59. Capabilities
  60. ------------
  61. Currently supported capabilities are:
  62. ``oob``
  63. the QMP server supports "out-of-band" (OOB) command
  64. execution, as described in section `Out-of-band execution`_.
  65. Issuing Commands
  66. ----------------
  67. The format for command execution is:
  68. ::
  69. { "execute": json-string, "arguments": json-object, "id": json-value }
  70. or
  71. ::
  72. { "exec-oob": json-string, "arguments": json-object, "id": json-value }
  73. Where:
  74. - The ``execute`` or ``exec-oob`` member identifies the command to be
  75. executed by the server. The latter requests out-of-band execution.
  76. - The ``arguments`` member is used to pass any arguments required for the
  77. execution of the command, it is optional when no arguments are
  78. required. Each command documents what contents will be considered
  79. valid when handling the json-argument.
  80. - The ``id`` member is a transaction identification associated with the
  81. command execution, it is optional and will be part of the response
  82. if provided. The ``id`` member can be any json-value. A json-number
  83. incremented for each successive command works fine.
  84. The actual commands are documented in the :doc:`qemu-qmp-ref`.
  85. Out-of-band execution
  86. ---------------------
  87. The server normally reads, executes and responds to one command after
  88. the other. The client therefore receives command responses in issue
  89. order.
  90. With out-of-band execution enabled via `capabilities negotiation`_,
  91. the server reads and queues commands as they arrive. It executes
  92. commands from the queue one after the other. Commands executed
  93. out-of-band jump the queue: the command get executed right away,
  94. possibly overtaking prior in-band commands. The client may therefore
  95. receive such a command's response before responses from prior in-band
  96. commands.
  97. To be able to match responses back to their commands, the client needs
  98. to pass ``id`` with out-of-band commands. Passing it with all commands
  99. is recommended for clients that accept capability ``oob``.
  100. If the client sends in-band commands faster than the server can
  101. execute them, the server will stop reading requests until the request
  102. queue length is reduced to an acceptable range.
  103. To ensure commands to be executed out-of-band get read and executed,
  104. the client should have at most eight in-band commands in flight.
  105. Only a few commands support out-of-band execution. The ones that do
  106. have ``"allow-oob": true`` in the output of ``query-qmp-schema``.
  107. Commands Responses
  108. ------------------
  109. There are two possible responses which the Server will issue as the result
  110. of a command execution: success or error.
  111. As long as the commands were issued with a proper ``id`` field, then the
  112. same ``id`` field will be attached in the corresponding response message
  113. so that requests and responses can match. Clients should drop all the
  114. responses that have an unknown ``id`` field.
  115. Success
  116. -------
  117. The format of a success response is:
  118. ::
  119. { "return": json-value, "id": json-value }
  120. Where:
  121. - The ``return`` member contains the data returned by the command, which
  122. is defined on a per-command basis (usually a json-object or
  123. json-array of json-objects, but sometimes a json-number, json-string,
  124. or json-array of json-strings); it is an empty json-object if the
  125. command does not return data.
  126. - The ``id`` member contains the transaction identification associated
  127. with the command execution if issued by the Client.
  128. Error
  129. -----
  130. The format of an error response is:
  131. ::
  132. { "error": { "class": json-string, "desc": json-string }, "id": json-value }
  133. Where:
  134. - The ``class`` member contains the error class name (eg. ``"GenericError"``).
  135. - The ``desc`` member is a human-readable error message. Clients should
  136. not attempt to parse this message.
  137. - The ``id`` member contains the transaction identification associated with
  138. the command execution if issued by the Client.
  139. NOTE: Some errors can occur before the Server is able to read the ``id`` member;
  140. in these cases the ``id`` member will not be part of the error response, even
  141. if provided by the client.
  142. Asynchronous events
  143. -------------------
  144. As a result of state changes, the Server may send messages unilaterally
  145. to the Client at any time, when not in the middle of any other
  146. response. They are called "asynchronous events".
  147. The format of asynchronous events is:
  148. ::
  149. { "event": json-string, "data": json-object,
  150. "timestamp": { "seconds": json-number, "microseconds": json-number } }
  151. Where:
  152. - The ``event`` member contains the event's name.
  153. - The ``data`` member contains event specific data, which is defined in a
  154. per-event basis. It is optional.
  155. - The ``timestamp`` member contains the exact time of when the event
  156. occurred in the Server. It is a fixed json-object with time in
  157. seconds and microseconds relative to the Unix Epoch (1 Jan 1970); if
  158. there is a failure to retrieve host time, both members of the
  159. timestamp will be set to -1.
  160. The actual asynchronous events are documented in the :doc:`qemu-qmp-ref`.
  161. Some events are rate-limited to at most one per second. If additional
  162. "similar" events arrive within one second, all but the last one are
  163. dropped, and the last one is delayed. "Similar" normally means same
  164. event type.
  165. Forcing the JSON parser into known-good state
  166. ---------------------------------------------
  167. Incomplete or invalid input can leave the server's JSON parser in a
  168. state where it can't parse additional commands. To get it back into
  169. known-good state, the client should provoke a lexical error.
  170. The cleanest way to do that is sending an ASCII control character
  171. other than ``\t`` (horizontal tab), ``\r`` (carriage return), or
  172. ``\n`` (new line).
  173. Sadly, older versions of QEMU can fail to flag this as an error. If a
  174. client needs to deal with them, it should send a 0xFF byte.
  175. QGA Synchronization
  176. -------------------
  177. When a client connects to QGA over a transport lacking proper
  178. connection semantics such as virtio-serial, QGA may have read partial
  179. input from a previous client. The client needs to force QGA's parser
  180. into known-good state using the previous section's technique.
  181. Moreover, the client may receive output a previous client didn't read.
  182. To help with skipping that output, QGA provides the
  183. ``guest-sync-delimited`` command. Refer to its documentation for
  184. details.
  185. QMP Examples
  186. ============
  187. This section provides some examples of real QMP usage, in all of them
  188. ``->`` marks text sent by the Client and ``<-`` marks replies by the Server.
  189. .. admonition:: Example
  190. Server greeting
  191. .. code-block:: QMP
  192. <- { "QMP": {"version": {"qemu": {"micro": 0, "minor": 0, "major": 3},
  193. "package": "v3.0.0"}, "capabilities": ["oob"] } }
  194. .. admonition:: Example
  195. Capabilities negotiation
  196. .. code-block:: QMP
  197. -> { "execute": "qmp_capabilities", "arguments": { "enable": ["oob"] } }
  198. <- { "return": {}}
  199. .. admonition:: Example
  200. Simple 'stop' execution
  201. .. code-block:: QMP
  202. -> { "execute": "stop" }
  203. <- { "return": {} }
  204. .. admonition:: Example
  205. KVM information
  206. .. code-block:: QMP
  207. -> { "execute": "query-kvm", "id": "example" }
  208. <- { "return": { "enabled": true, "present": true }, "id": "example"}
  209. .. admonition:: Example
  210. Parsing error
  211. .. code-block:: QMP
  212. -> { "execute": }
  213. <- { "error": { "class": "GenericError", "desc": "JSON parse error, expecting value" } }
  214. .. admonition:: Example
  215. Powerdown event
  216. .. code-block:: QMP
  217. <- { "timestamp": { "seconds": 1258551470, "microseconds": 802384 },
  218. "event": "POWERDOWN" }
  219. .. admonition:: Example
  220. Out-of-band execution
  221. .. code-block:: QMP
  222. -> { "exec-oob": "migrate-pause", "id": 42 }
  223. <- { "id": 42,
  224. "error": { "class": "GenericError",
  225. "desc": "migrate-pause is currently only supported during postcopy-active state" } }
  226. Capabilities Negotiation
  227. ========================
  228. When a Client successfully establishes a connection, the Server is in
  229. Capabilities Negotiation mode.
  230. In this mode only the ``qmp_capabilities`` command is allowed to run; all
  231. other commands will return the ``CommandNotFound`` error. Asynchronous
  232. messages are not delivered either.
  233. Clients should use the ``qmp_capabilities`` command to enable capabilities
  234. advertised in the `Server Greeting`_ which they support.
  235. When the ``qmp_capabilities`` command is issued, and if it does not return an
  236. error, the Server enters Command mode where capabilities changes take
  237. effect, all commands (except ``qmp_capabilities``) are allowed and asynchronous
  238. messages are delivered.
  239. Compatibility Considerations
  240. ============================
  241. All protocol changes or new features which modify the protocol format in an
  242. incompatible way are disabled by default and will be advertised by the
  243. capabilities array (in the `Server Greeting`_). Thus, Clients can check
  244. that array and enable the capabilities they support.
  245. The QMP Server performs a type check on the arguments to a command. It
  246. generates an error if a value does not have the expected type for its
  247. key, or if it does not understand a key that the Client included. The
  248. strictness of the Server catches wrong assumptions of Clients about
  249. the Server's schema. Clients can assume that, when such validation
  250. errors occur, they will be reported before the command generated any
  251. side effect.
  252. However, Clients must not assume any particular:
  253. - Length of json-arrays
  254. - Size of json-objects; in particular, future versions of QEMU may add
  255. new keys and Clients should be able to ignore them
  256. - Order of json-object members or json-array elements
  257. - Amount of errors generated by a command, that is, new errors can be added
  258. to any existing command in newer versions of the Server
  259. Any command or member name beginning with ``x-`` is deemed experimental,
  260. and may be withdrawn or changed in an incompatible manner in a future
  261. release.
  262. Of course, the Server does guarantee to send valid JSON. But apart from
  263. this, a Client should be "conservative in what they send, and liberal in
  264. what they accept".
  265. Downstream extension of QMP
  266. ===========================
  267. We recommend that downstream consumers of QEMU do *not* modify QMP.
  268. Management tools should be able to support both upstream and downstream
  269. versions of QMP without special logic, and downstream extensions are
  270. inherently at odds with that.
  271. However, we recognize that it is sometimes impossible for downstreams to
  272. avoid modifying QMP. Both upstream and downstream need to take care to
  273. preserve long-term compatibility and interoperability.
  274. To help with that, QMP reserves JSON object member names beginning with
  275. ``__`` (double underscore) for downstream use ("downstream names"). This
  276. means upstream will never use any downstream names for its commands,
  277. arguments, errors, asynchronous events, and so forth.
  278. Any new names downstream wishes to add must begin with ``__``. To
  279. ensure compatibility with other downstreams, it is strongly
  280. recommended that you prefix your downstream names with ``__RFQDN_`` where
  281. RFQDN is a valid, reverse fully qualified domain name which you
  282. control. For example, a qemu-kvm specific monitor command would be:
  283. ::
  284. (qemu) __org.linux-kvm_enable_irqchip
  285. Downstream must not change the `server greeting`_ other than
  286. to offer additional capabilities. But see below for why even that is
  287. discouraged.
  288. The section `Compatibility Considerations`_ applies to downstream as well
  289. as to upstream, obviously. It follows that downstream must behave
  290. exactly like upstream for any input not containing members with
  291. downstream names ("downstream members"), except it may add members
  292. with downstream names to its output.
  293. Thus, a client should not be able to distinguish downstream from
  294. upstream as long as it doesn't send input with downstream members, and
  295. properly ignores any downstream members in the output it receives.
  296. Advice on downstream modifications:
  297. 1. Introducing new commands is okay. If you want to extend an existing
  298. command, consider introducing a new one with the new behaviour
  299. instead.
  300. 2. Introducing new asynchronous messages is okay. If you want to extend
  301. an existing message, consider adding a new one instead.
  302. 3. Introducing new errors for use in new commands is okay. Adding new
  303. errors to existing commands counts as extension, so 1. applies.
  304. 4. New capabilities are strongly discouraged. Capabilities are for
  305. evolving the basic protocol, and multiple diverging basic protocol
  306. dialects are most undesirable.