2
0

qapi-code-gen.rst 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. ==================================
  2. How to use the QAPI code generator
  3. ==================================
  4. ..
  5. Copyright IBM Corp. 2011
  6. Copyright (C) 2012-2016 Red Hat, Inc.
  7. This work is licensed under the terms of the GNU GPL, version 2 or
  8. later. See the COPYING file in the top-level directory.
  9. .. _qapi:
  10. Introduction
  11. ============
  12. QAPI is a native C API within QEMU which provides management-level
  13. functionality to internal and external users. For external
  14. users/processes, this interface is made available by a JSON-based wire
  15. format for the QEMU Monitor Protocol (QMP) for controlling qemu, as
  16. well as the QEMU Guest Agent (QGA) for communicating with the guest.
  17. The remainder of this document uses "Client JSON Protocol" when
  18. referring to the wire contents of a QMP or QGA connection.
  19. To map between Client JSON Protocol interfaces and the native C API,
  20. we generate C code from a QAPI schema. This document describes the
  21. QAPI schema language, and how it gets mapped to the Client JSON
  22. Protocol and to C. It additionally provides guidance on maintaining
  23. Client JSON Protocol compatibility.
  24. The QAPI schema language
  25. ========================
  26. The QAPI schema defines the Client JSON Protocol's commands and
  27. events, as well as types used by them. Forward references are
  28. allowed.
  29. It is permissible for the schema to contain additional types not used
  30. by any commands or events, for the side effect of generated C code
  31. used internally.
  32. There are several kinds of types: simple types (a number of built-in
  33. types, such as ``int`` and ``str``; as well as enumerations), arrays,
  34. complex types (structs and unions), and alternate types (a choice
  35. between other types).
  36. Schema syntax
  37. -------------
  38. Syntax is loosely based on `JSON <http://www.ietf.org/rfc/rfc8259.txt>`_.
  39. Differences:
  40. * Comments: start with a hash character (``#``) that is not part of a
  41. string, and extend to the end of the line.
  42. * Strings are enclosed in ``'single quotes'``, not ``"double quotes"``.
  43. * Strings are restricted to printable ASCII, and escape sequences to
  44. just ``\\``.
  45. * Numbers and ``null`` are not supported.
  46. A second layer of syntax defines the sequences of JSON texts that are
  47. a correctly structured QAPI schema. We provide a grammar for this
  48. syntax in an EBNF-like notation:
  49. * Production rules look like ``non-terminal = expression``
  50. * Concatenation: expression ``A B`` matches expression ``A``, then ``B``
  51. * Alternation: expression ``A | B`` matches expression ``A`` or ``B``
  52. * Repetition: expression ``A...`` matches zero or more occurrences of
  53. expression ``A``
  54. * Repetition: expression ``A, ...`` matches zero or more occurrences of
  55. expression ``A`` separated by ``,``
  56. * Grouping: expression ``( A )`` matches expression ``A``
  57. * JSON's structural characters are terminals: ``{ } [ ] : ,``
  58. * JSON's literal names are terminals: ``false true``
  59. * String literals enclosed in ``'single quotes'`` are terminal, and match
  60. this JSON string, with a leading ``*`` stripped off
  61. * When JSON object member's name starts with ``*``, the member is
  62. optional.
  63. * The symbol ``STRING`` is a terminal, and matches any JSON string
  64. * The symbol ``BOOL`` is a terminal, and matches JSON ``false`` or ``true``
  65. * ALL-CAPS words other than ``STRING`` are non-terminals
  66. The order of members within JSON objects does not matter unless
  67. explicitly noted.
  68. A QAPI schema consists of a series of top-level expressions::
  69. SCHEMA = TOP-LEVEL-EXPR...
  70. The top-level expressions are all JSON objects. Code and
  71. documentation is generated in schema definition order. Code order
  72. should not matter.
  73. A top-level expressions is either a directive or a definition::
  74. TOP-LEVEL-EXPR = DIRECTIVE | DEFINITION
  75. There are two kinds of directives and six kinds of definitions::
  76. DIRECTIVE = INCLUDE | PRAGMA
  77. DEFINITION = ENUM | STRUCT | UNION | ALTERNATE | COMMAND | EVENT
  78. These are discussed in detail below.
  79. Built-in Types
  80. --------------
  81. The following types are predefined, and map to C as follows:
  82. ============= ============== ============================================
  83. Schema C JSON
  84. ============= ============== ============================================
  85. ``str`` ``char *`` any JSON string, UTF-8
  86. ``number`` ``double`` any JSON number
  87. ``int`` ``int64_t`` a JSON number without fractional part
  88. that fits into the C integer type
  89. ``int8`` ``int8_t`` likewise
  90. ``int16`` ``int16_t`` likewise
  91. ``int32`` ``int32_t`` likewise
  92. ``int64`` ``int64_t`` likewise
  93. ``uint8`` ``uint8_t`` likewise
  94. ``uint16`` ``uint16_t`` likewise
  95. ``uint32`` ``uint32_t`` likewise
  96. ``uint64`` ``uint64_t`` likewise
  97. ``size`` ``uint64_t`` like ``uint64_t``, except
  98. ``StringInputVisitor`` accepts size suffixes
  99. ``bool`` ``bool`` JSON ``true`` or ``false``
  100. ``null`` ``QNull *`` JSON ``null``
  101. ``any`` ``QObject *`` any JSON value
  102. ``QType`` ``QType`` JSON string matching enum ``QType`` values
  103. ============= ============== ============================================
  104. Include directives
  105. ------------------
  106. Syntax::
  107. INCLUDE = { 'include': STRING }
  108. The QAPI schema definitions can be modularized using the 'include' directive::
  109. { 'include': 'path/to/file.json' }
  110. The directive is evaluated recursively, and include paths are relative
  111. to the file using the directive. Multiple includes of the same file
  112. are idempotent.
  113. As a matter of style, it is a good idea to have all files be
  114. self-contained, but at the moment, nothing prevents an included file
  115. from making a forward reference to a type that is only introduced by
  116. an outer file. The parser may be made stricter in the future to
  117. prevent incomplete include files.
  118. .. _pragma:
  119. Pragma directives
  120. -----------------
  121. Syntax::
  122. PRAGMA = { 'pragma': {
  123. '*doc-required': BOOL,
  124. '*command-name-exceptions': [ STRING, ... ],
  125. '*command-returns-exceptions': [ STRING, ... ],
  126. '*documentation-exceptions': [ STRING, ... ],
  127. '*member-name-exceptions': [ STRING, ... ] } }
  128. The pragma directive lets you control optional generator behavior.
  129. Pragma's scope is currently the complete schema. Setting the same
  130. pragma to different values in parts of the schema doesn't work.
  131. Pragma 'doc-required' takes a boolean value. If true, documentation
  132. is required. Default is false.
  133. Pragma 'command-name-exceptions' takes a list of commands whose names
  134. may contain ``"_"`` instead of ``"-"``. Default is none.
  135. Pragma 'command-returns-exceptions' takes a list of commands that may
  136. violate the rules on permitted return types. Default is none.
  137. Pragma 'documentation-exceptions' takes a list of types, commands, and
  138. events whose members / arguments need not be documented. Default is
  139. none.
  140. Pragma 'member-name-exceptions' takes a list of types whose member
  141. names may contain uppercase letters, and ``"_"`` instead of ``"-"``.
  142. Default is none.
  143. .. _ENUM-VALUE:
  144. Enumeration types
  145. -----------------
  146. Syntax::
  147. ENUM = { 'enum': STRING,
  148. 'data': [ ENUM-VALUE, ... ],
  149. '*prefix': STRING,
  150. '*if': COND,
  151. '*features': FEATURES }
  152. ENUM-VALUE = STRING
  153. | { 'name': STRING,
  154. '*if': COND,
  155. '*features': FEATURES }
  156. Member 'enum' names the enum type.
  157. Each member of the 'data' array defines a value of the enumeration
  158. type. The form STRING is shorthand for :code:`{ 'name': STRING }`. The
  159. 'name' values must be be distinct.
  160. Example::
  161. { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
  162. Nothing prevents an empty enumeration, although it is probably not
  163. useful.
  164. On the wire, an enumeration type's value is represented by its
  165. (string) name. In C, it's represented by an enumeration constant.
  166. These are of the form PREFIX_NAME, where PREFIX is derived from the
  167. enumeration type's name, and NAME from the value's name. For the
  168. example above, the generator maps 'MyEnum' to MY_ENUM and 'value1' to
  169. VALUE1, resulting in the enumeration constant MY_ENUM_VALUE1. The
  170. optional 'prefix' member overrides PREFIX. This is rarely necessary,
  171. and should be used with restraint.
  172. The generated C enumeration constants have values 0, 1, ..., N-1 (in
  173. QAPI schema order), where N is the number of values. There is an
  174. additional enumeration constant PREFIX__MAX with value N.
  175. Do not use string or an integer type when an enumeration type can do
  176. the job satisfactorily.
  177. The optional 'if' member specifies a conditional. See `Configuring the
  178. schema`_ below for more on this.
  179. The optional 'features' member specifies features. See Features_
  180. below for more on this.
  181. .. _TYPE-REF:
  182. Type references and array types
  183. -------------------------------
  184. Syntax::
  185. TYPE-REF = STRING | ARRAY-TYPE
  186. ARRAY-TYPE = [ STRING ]
  187. A string denotes the type named by the string.
  188. A one-element array containing a string denotes an array of the type
  189. named by the string. Example: ``['int']`` denotes an array of ``int``.
  190. Struct types
  191. ------------
  192. Syntax::
  193. STRUCT = { 'struct': STRING,
  194. 'data': MEMBERS,
  195. '*base': STRING,
  196. '*if': COND,
  197. '*features': FEATURES }
  198. MEMBERS = { MEMBER, ... }
  199. MEMBER = STRING : TYPE-REF
  200. | STRING : { 'type': TYPE-REF,
  201. '*if': COND,
  202. '*features': FEATURES }
  203. Member 'struct' names the struct type.
  204. Each MEMBER of the 'data' object defines a member of the struct type.
  205. .. _MEMBERS:
  206. The MEMBER's STRING name consists of an optional ``*`` prefix and the
  207. struct member name. If ``*`` is present, the member is optional.
  208. The MEMBER's value defines its properties, in particular its type.
  209. The form TYPE-REF_ is shorthand for :code:`{ 'type': TYPE-REF }`.
  210. Example::
  211. { 'struct': 'MyType',
  212. 'data': { 'member1': 'str', 'member2': ['int'], '*member3': 'str' } }
  213. A struct type corresponds to a struct in C, and an object in JSON.
  214. The C struct's members are generated in QAPI schema order.
  215. The optional 'base' member names a struct type whose members are to be
  216. included in this type. They go first in the C struct.
  217. Example::
  218. { 'struct': 'BlockdevOptionsGenericFormat',
  219. 'data': { 'file': 'str' } }
  220. { 'struct': 'BlockdevOptionsGenericCOWFormat',
  221. 'base': 'BlockdevOptionsGenericFormat',
  222. 'data': { '*backing': 'str' } }
  223. An example BlockdevOptionsGenericCOWFormat object on the wire could use
  224. both members like this::
  225. { "file": "/some/place/my-image",
  226. "backing": "/some/place/my-backing-file" }
  227. The optional 'if' member specifies a conditional. See `Configuring
  228. the schema`_ below for more on this.
  229. The optional 'features' member specifies features. See Features_
  230. below for more on this.
  231. Union types
  232. -----------
  233. Syntax::
  234. UNION = { 'union': STRING,
  235. 'base': ( MEMBERS | STRING ),
  236. 'discriminator': STRING,
  237. 'data': BRANCHES,
  238. '*if': COND,
  239. '*features': FEATURES }
  240. BRANCHES = { BRANCH, ... }
  241. BRANCH = STRING : TYPE-REF
  242. | STRING : { 'type': TYPE-REF, '*if': COND }
  243. Member 'union' names the union type.
  244. The 'base' member defines the common members. If it is a MEMBERS_
  245. object, it defines common members just like a struct type's 'data'
  246. member defines struct type members. If it is a STRING, it names a
  247. struct type whose members are the common members.
  248. Member 'discriminator' must name a non-optional enum-typed member of
  249. the base struct. That member's value selects a branch by its name.
  250. If no such branch exists, an empty branch is assumed.
  251. Each BRANCH of the 'data' object defines a branch of the union. A
  252. union must have at least one branch.
  253. The BRANCH's STRING name is the branch name. It must be a value of
  254. the discriminator enum type.
  255. The BRANCH's value defines the branch's properties, in particular its
  256. type. The type must a struct type. The form TYPE-REF_ is shorthand
  257. for :code:`{ 'type': TYPE-REF }`.
  258. In the Client JSON Protocol, a union is represented by an object with
  259. the common members (from the base type) and the selected branch's
  260. members. The two sets of member names must be disjoint.
  261. Example::
  262. { 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] }
  263. { 'union': 'BlockdevOptions',
  264. 'base': { 'driver': 'BlockdevDriver', '*read-only': 'bool' },
  265. 'discriminator': 'driver',
  266. 'data': { 'file': 'BlockdevOptionsFile',
  267. 'qcow2': 'BlockdevOptionsQcow2' } }
  268. Resulting in these JSON objects::
  269. { "driver": "file", "read-only": true,
  270. "filename": "/some/place/my-image" }
  271. { "driver": "qcow2", "read-only": false,
  272. "backing": "/some/place/my-image", "lazy-refcounts": true }
  273. The order of branches need not match the order of the enum values.
  274. The branches need not cover all possible enum values. In the
  275. resulting generated C data types, a union is represented as a struct
  276. with the base members in QAPI schema order, and then a union of
  277. structures for each branch of the struct.
  278. The optional 'if' member specifies a conditional. See `Configuring
  279. the schema`_ below for more on this.
  280. The optional 'features' member specifies features. See Features_
  281. below for more on this.
  282. Alternate types
  283. ---------------
  284. Syntax::
  285. ALTERNATE = { 'alternate': STRING,
  286. 'data': ALTERNATIVES,
  287. '*if': COND,
  288. '*features': FEATURES }
  289. ALTERNATIVES = { ALTERNATIVE, ... }
  290. ALTERNATIVE = STRING : STRING
  291. | STRING : { 'type': STRING, '*if': COND }
  292. Member 'alternate' names the alternate type.
  293. Each ALTERNATIVE of the 'data' object defines a branch of the
  294. alternate. An alternate must have at least one branch.
  295. The ALTERNATIVE's STRING name is the branch name.
  296. The ALTERNATIVE's value defines the branch's properties, in particular
  297. its type. The form STRING is shorthand for :code:`{ 'type': STRING }`.
  298. Example::
  299. { 'alternate': 'BlockdevRef',
  300. 'data': { 'definition': 'BlockdevOptions',
  301. 'reference': 'str' } }
  302. An alternate type is like a union type, except there is no
  303. discriminator on the wire. Instead, the branch to use is inferred
  304. from the value. An alternate can only express a choice between types
  305. represented differently on the wire.
  306. If a branch is typed as the 'bool' built-in, the alternate accepts
  307. true and false; if it is typed as any of the various numeric
  308. built-ins, it accepts a JSON number; if it is typed as a 'str'
  309. built-in or named enum type, it accepts a JSON string; if it is typed
  310. as the 'null' built-in, it accepts JSON null; and if it is typed as a
  311. complex type (struct or union), it accepts a JSON object.
  312. The example alternate declaration above allows using both of the
  313. following example objects::
  314. { "file": "my_existing_block_device_id" }
  315. { "file": { "driver": "file",
  316. "read-only": false,
  317. "filename": "/tmp/mydisk.qcow2" } }
  318. The optional 'if' member specifies a conditional. See `Configuring
  319. the schema`_ below for more on this.
  320. The optional 'features' member specifies features. See Features_
  321. below for more on this.
  322. Commands
  323. --------
  324. Syntax::
  325. COMMAND = { 'command': STRING,
  326. (
  327. '*data': ( MEMBERS | STRING ),
  328. |
  329. 'data': STRING,
  330. 'boxed': true,
  331. )
  332. '*returns': TYPE-REF,
  333. '*success-response': false,
  334. '*gen': false,
  335. '*allow-oob': true,
  336. '*allow-preconfig': true,
  337. '*coroutine': true,
  338. '*if': COND,
  339. '*features': FEATURES }
  340. Member 'command' names the command.
  341. Member 'data' defines the arguments. It defaults to an empty MEMBERS_
  342. object.
  343. If 'data' is a MEMBERS_ object, then MEMBERS defines arguments just
  344. like a struct type's 'data' defines struct type members.
  345. If 'data' is a STRING, then STRING names a complex type whose members
  346. are the arguments. A union type requires ``'boxed': true``.
  347. Member 'returns' defines the command's return type. It defaults to an
  348. empty struct type. It must normally be a complex type or an array of
  349. a complex type. To return anything else, the command must be listed
  350. in pragma 'commands-returns-exceptions'. If you do this, extending
  351. the command to return additional information will be harder. Use of
  352. the pragma for new commands is strongly discouraged.
  353. A command's error responses are not specified in the QAPI schema.
  354. Error conditions should be documented in comments.
  355. In the Client JSON Protocol, the value of the "execute" or "exec-oob"
  356. member is the command name. The value of the "arguments" member then
  357. has to conform to the arguments, and the value of the success
  358. response's "return" member will conform to the return type.
  359. Some example commands::
  360. { 'command': 'my-first-command',
  361. 'data': { 'arg1': 'str', '*arg2': 'str' } }
  362. { 'struct': 'MyType', 'data': { '*value': 'str' } }
  363. { 'command': 'my-second-command',
  364. 'returns': [ 'MyType' ] }
  365. which would validate this Client JSON Protocol transaction::
  366. => { "execute": "my-first-command",
  367. "arguments": { "arg1": "hello" } }
  368. <= { "return": { } }
  369. => { "execute": "my-second-command" }
  370. <= { "return": [ { "value": "one" }, { } ] }
  371. The generator emits a prototype for the C function implementing the
  372. command. The function itself needs to be written by hand. See
  373. section `Code generated for commands`_ for examples.
  374. The function returns the return type. When member 'boxed' is absent,
  375. it takes the command arguments as arguments one by one, in QAPI schema
  376. order. Else it takes them wrapped in the C struct generated for the
  377. complex argument type. It takes an additional ``Error **`` argument in
  378. either case.
  379. The generator also emits a marshalling function that extracts
  380. arguments for the user's function out of an input QDict, calls the
  381. user's function, and if it succeeded, builds an output QObject from
  382. its return value. This is for use by the QMP monitor core.
  383. In rare cases, QAPI cannot express a type-safe representation of a
  384. corresponding Client JSON Protocol command. You then have to suppress
  385. generation of a marshalling function by including a member 'gen' with
  386. boolean value false, and instead write your own function. For
  387. example::
  388. { 'command': 'netdev_add',
  389. 'data': {'type': 'str', 'id': 'str'},
  390. 'gen': false }
  391. Please try to avoid adding new commands that rely on this, and instead
  392. use type-safe unions.
  393. Normally, the QAPI schema is used to describe synchronous exchanges,
  394. where a response is expected. But in some cases, the action of a
  395. command is expected to change state in a way that a successful
  396. response is not possible (although the command will still return an
  397. error object on failure). When a successful reply is not possible,
  398. the command definition includes the optional member 'success-response'
  399. with boolean value false. So far, only QGA makes use of this member.
  400. Member 'allow-oob' declares whether the command supports out-of-band
  401. (OOB) execution. It defaults to false. For example::
  402. { 'command': 'migrate_recover',
  403. 'data': { 'uri': 'str' }, 'allow-oob': true }
  404. See the :doc:`/interop/qmp-spec` for out-of-band execution syntax
  405. and semantics.
  406. Commands supporting out-of-band execution can still be executed
  407. in-band.
  408. When a command is executed in-band, its handler runs in the main
  409. thread with the BQL held.
  410. When a command is executed out-of-band, its handler runs in a
  411. dedicated monitor I/O thread with the BQL *not* held.
  412. An OOB-capable command handler must satisfy the following conditions:
  413. - It terminates quickly.
  414. - It does not invoke system calls that may block.
  415. - It does not access guest RAM that may block when userfaultfd is
  416. enabled for postcopy live migration.
  417. - It takes only "fast" locks, i.e. all critical sections protected by
  418. any lock it takes also satisfy the conditions for OOB command
  419. handler code.
  420. The restrictions on locking limit access to shared state. Such access
  421. requires synchronization, but OOB commands can't take the BQL or any
  422. other "slow" lock.
  423. When in doubt, do not implement OOB execution support.
  424. Member 'allow-preconfig' declares whether the command is available
  425. before the machine is built. It defaults to false. For example::
  426. { 'enum': 'QMPCapability',
  427. 'data': [ 'oob' ] }
  428. { 'command': 'qmp_capabilities',
  429. 'data': { '*enable': [ 'QMPCapability' ] },
  430. 'allow-preconfig': true }
  431. QMP is available before the machine is built only when QEMU was
  432. started with --preconfig.
  433. Member 'coroutine' tells the QMP dispatcher whether the command handler
  434. is safe to be run in a coroutine. It defaults to false. If it is true,
  435. the command handler is called from coroutine context and may yield while
  436. waiting for an external event (such as I/O completion) in order to avoid
  437. blocking the guest and other background operations.
  438. Coroutine safety can be hard to prove, similar to thread safety. Common
  439. pitfalls are:
  440. - The BQL isn't held across ``qemu_coroutine_yield()``, so
  441. operations that used to assume that they execute atomically may have
  442. to be more careful to protect against changes in the global state.
  443. - Nested event loops (``AIO_WAIT_WHILE()`` etc.) are problematic in
  444. coroutine context and can easily lead to deadlocks. They should be
  445. replaced by yielding and reentering the coroutine when the condition
  446. becomes false.
  447. Since the command handler may assume coroutine context, any callers
  448. other than the QMP dispatcher must also call it in coroutine context.
  449. In particular, HMP commands calling such a QMP command handler must be
  450. marked ``.coroutine = true`` in hmp-commands.hx.
  451. It is an error to specify both ``'coroutine': true`` and ``'allow-oob': true``
  452. for a command. We don't currently have a use case for both together and
  453. without a use case, it's not entirely clear what the semantics should
  454. be.
  455. The optional 'if' member specifies a conditional. See `Configuring
  456. the schema`_ below for more on this.
  457. The optional 'features' member specifies features. See Features_
  458. below for more on this.
  459. Events
  460. ------
  461. Syntax::
  462. EVENT = { 'event': STRING,
  463. (
  464. '*data': ( MEMBERS | STRING ),
  465. |
  466. 'data': STRING,
  467. 'boxed': true,
  468. )
  469. '*if': COND,
  470. '*features': FEATURES }
  471. Member 'event' names the event. This is the event name used in the
  472. Client JSON Protocol.
  473. Member 'data' defines the event-specific data. It defaults to an
  474. empty MEMBERS object.
  475. If 'data' is a MEMBERS object, then MEMBERS defines event-specific
  476. data just like a struct type's 'data' defines struct type members.
  477. If 'data' is a STRING, then STRING names a complex type whose members
  478. are the event-specific data. A union type requires ``'boxed': true``.
  479. An example event is::
  480. { 'event': 'EVENT_C',
  481. 'data': { '*a': 'int', 'b': 'str' } }
  482. Resulting in this JSON object::
  483. { "event": "EVENT_C",
  484. "data": { "b": "test string" },
  485. "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
  486. The generator emits a function to send the event. When member 'boxed'
  487. is absent, it takes event-specific data one by one, in QAPI schema
  488. order. Else it takes them wrapped in the C struct generated for the
  489. complex type. See section `Code generated for events`_ for examples.
  490. The optional 'if' member specifies a conditional. See `Configuring
  491. the schema`_ below for more on this.
  492. The optional 'features' member specifies features. See Features_
  493. below for more on this.
  494. .. _FEATURE:
  495. Features
  496. --------
  497. Syntax::
  498. FEATURES = [ FEATURE, ... ]
  499. FEATURE = STRING
  500. | { 'name': STRING, '*if': COND }
  501. Sometimes, the behaviour of QEMU changes compatibly, but without a
  502. change in the QMP syntax (usually by allowing values or operations
  503. that previously resulted in an error). QMP clients may still need to
  504. know whether the extension is available.
  505. For this purpose, a list of features can be specified for definitions,
  506. enumeration values, and struct members. Each feature list member can
  507. either be ``{ 'name': STRING, '*if': COND }``, or STRING, which is
  508. shorthand for ``{ 'name': STRING }``.
  509. The optional 'if' member specifies a conditional. See `Configuring
  510. the schema`_ below for more on this.
  511. Example::
  512. { 'struct': 'TestType',
  513. 'data': { 'number': 'int' },
  514. 'features': [ 'allow-negative-numbers' ] }
  515. The feature strings are exposed to clients in introspection, as
  516. explained in section `Client JSON Protocol introspection`_.
  517. Intended use is to have each feature string signal that this build of
  518. QEMU shows a certain behaviour.
  519. Special features
  520. ~~~~~~~~~~~~~~~~
  521. Feature "deprecated" marks a command, event, enum value, or struct
  522. member as deprecated. It is not supported elsewhere so far.
  523. Interfaces so marked may be withdrawn in future releases in accordance
  524. with QEMU's deprecation policy.
  525. Feature "unstable" marks a command, event, enum value, or struct
  526. member as unstable. It is not supported elsewhere so far. Interfaces
  527. so marked may be withdrawn or changed incompatibly in future releases.
  528. Naming rules and reserved names
  529. -------------------------------
  530. All names must begin with a letter, and contain only ASCII letters,
  531. digits, hyphen, and underscore. There are two exceptions: enum values
  532. may start with a digit, and names that are downstream extensions (see
  533. section `Downstream extensions`_) start with underscore.
  534. Names beginning with ``q_`` are reserved for the generator, which uses
  535. them for munging QMP names that resemble C keywords or other
  536. problematic strings. For example, a member named ``default`` in qapi
  537. becomes ``q_default`` in the generated C code.
  538. Types, commands, and events share a common namespace. Therefore,
  539. generally speaking, type definitions should always use CamelCase for
  540. user-defined type names, while built-in types are lowercase.
  541. Type names ending with ``List`` are reserved for the generator, which
  542. uses them for array types.
  543. Command names, member names within a type, and feature names should be
  544. all lower case with words separated by a hyphen. However, some
  545. existing older commands and complex types use underscore; when
  546. extending them, consistency is preferred over blindly avoiding
  547. underscore.
  548. Event names should be ALL_CAPS with words separated by underscore.
  549. Member name ``u`` and names starting with ``has-`` or ``has_`` are reserved
  550. for the generator, which uses them for unions and for tracking
  551. optional members.
  552. Names beginning with ``x-`` used to signify "experimental". This
  553. convention has been replaced by special feature "unstable".
  554. Pragmas ``command-name-exceptions`` and ``member-name-exceptions`` let
  555. you violate naming rules. Use for new code is strongly discouraged.
  556. See `Pragma directives`_ for details.
  557. Downstream extensions
  558. ---------------------
  559. QAPI schema names that are externally visible, say in the Client JSON
  560. Protocol, need to be managed with care. Names starting with a
  561. downstream prefix of the form __RFQDN_ are reserved for the downstream
  562. who controls the valid, reverse fully qualified domain name RFQDN.
  563. RFQDN may only contain ASCII letters, digits, hyphen and period.
  564. Example: Red Hat, Inc. controls redhat.com, and may therefore add a
  565. downstream command ``__com.redhat_drive-mirror``.
  566. Configuring the schema
  567. ----------------------
  568. Syntax::
  569. COND = STRING
  570. | { 'all: [ COND, ... ] }
  571. | { 'any: [ COND, ... ] }
  572. | { 'not': COND }
  573. All definitions take an optional 'if' member. Its value must be a
  574. string, or an object with a single member 'all', 'any' or 'not'.
  575. The C code generated for the definition will then be guarded by an #if
  576. preprocessing directive with an operand generated from that condition:
  577. * STRING will generate defined(STRING)
  578. * { 'all': [COND, ...] } will generate (COND && ...)
  579. * { 'any': [COND, ...] } will generate (COND || ...)
  580. * { 'not': COND } will generate !COND
  581. Example: a conditional struct ::
  582. { 'struct': 'IfStruct', 'data': { 'foo': 'int' },
  583. 'if': { 'all': [ 'CONFIG_FOO', 'HAVE_BAR' ] } }
  584. gets its generated code guarded like this::
  585. #if defined(CONFIG_FOO) && defined(HAVE_BAR)
  586. ... generated code ...
  587. #endif /* defined(HAVE_BAR) && defined(CONFIG_FOO) */
  588. Individual members of complex types can also be made conditional.
  589. This requires the longhand form of MEMBER.
  590. Example: a struct type with unconditional member 'foo' and conditional
  591. member 'bar' ::
  592. { 'struct': 'IfStruct',
  593. 'data': { 'foo': 'int',
  594. 'bar': { 'type': 'int', 'if': 'IFCOND'} } }
  595. A union's discriminator may not be conditional.
  596. Likewise, individual enumeration values may be conditional. This
  597. requires the longhand form of ENUM-VALUE_.
  598. Example: an enum type with unconditional value 'foo' and conditional
  599. value 'bar' ::
  600. { 'enum': 'IfEnum',
  601. 'data': [ 'foo',
  602. { 'name' : 'bar', 'if': 'IFCOND' } ] }
  603. Likewise, features can be conditional. This requires the longhand
  604. form of FEATURE_.
  605. Example: a struct with conditional feature 'allow-negative-numbers' ::
  606. { 'struct': 'TestType',
  607. 'data': { 'number': 'int' },
  608. 'features': [ { 'name': 'allow-negative-numbers',
  609. 'if': 'IFCOND' } ] }
  610. Please note that you are responsible to ensure that the C code will
  611. compile with an arbitrary combination of conditions, since the
  612. generator is unable to check it at this point.
  613. The conditions apply to introspection as well, i.e. introspection
  614. shows a conditional entity only when the condition is satisfied in
  615. this particular build.
  616. Documentation comments
  617. ----------------------
  618. A multi-line comment that starts and ends with a ``##`` line is a
  619. documentation comment.
  620. If the documentation comment starts like ::
  621. ##
  622. # @SYMBOL:
  623. it documents the definition of SYMBOL, else it's free-form
  624. documentation.
  625. See below for more on `Definition documentation`_.
  626. Free-form documentation may be used to provide additional text and
  627. structuring content.
  628. Headings and subheadings
  629. ~~~~~~~~~~~~~~~~~~~~~~~~
  630. A free-form documentation comment containing a line which starts with
  631. some ``=`` symbols and then a space defines a section heading::
  632. ##
  633. # = This is a top level heading
  634. #
  635. # This is a free-form comment which will go under the
  636. # top level heading.
  637. ##
  638. ##
  639. # == This is a second level heading
  640. ##
  641. A heading line must be the first line of the documentation
  642. comment block.
  643. Section headings must always be correctly nested, so you can only
  644. define a third-level heading inside a second-level heading, and so on.
  645. Documentation markup
  646. ~~~~~~~~~~~~~~~~~~~~
  647. Documentation comments can use most rST markup. In particular,
  648. a ``::`` literal block can be used for pre-formatted text::
  649. # ::
  650. #
  651. # Text of the example, may span
  652. # multiple lines
  653. ``*`` starts an itemized list::
  654. # * First item, may span
  655. # multiple lines
  656. # * Second item
  657. You can also use ``-`` instead of ``*``.
  658. A decimal number followed by ``.`` starts a numbered list::
  659. # 1. First item, may span
  660. # multiple lines
  661. # 2. Second item
  662. The actual number doesn't matter.
  663. Lists of either kind must be preceded and followed by a blank line.
  664. If a list item's text spans multiple lines, then the second and
  665. subsequent lines must be correctly indented to line up with the
  666. first character of the first line.
  667. The usual ****strong****, *\*emphasized\** and ````literal```` markup
  668. should be used. If you need a single literal ``*``, you will need to
  669. backslash-escape it.
  670. Use ``@foo`` to reference a name in the schema. This is an rST
  671. extension. It is rendered the same way as ````foo````, but carries
  672. additional meaning.
  673. Example::
  674. ##
  675. # Some text foo with **bold** and *emphasis*
  676. #
  677. # 1. with a list
  678. # 2. like that
  679. #
  680. # And some code:
  681. #
  682. # ::
  683. #
  684. # $ echo foo
  685. # -> do this
  686. # <- get that
  687. ##
  688. For legibility, wrap text paragraphs so every line is at most 70
  689. characters long.
  690. Separate sentences with two spaces.
  691. Definition documentation
  692. ~~~~~~~~~~~~~~~~~~~~~~~~
  693. Definition documentation, if present, must immediately precede the
  694. definition it documents.
  695. When documentation is required (see pragma_ 'doc-required'), every
  696. definition must have documentation.
  697. Definition documentation starts with a line naming the definition,
  698. followed by an optional overview, a description of each argument (for
  699. commands and events), member (for structs and unions), branch (for
  700. alternates), or value (for enums), a description of each feature (if
  701. any), and finally optional tagged sections.
  702. Descriptions start with '\@name:'. The description text must be
  703. indented like this::
  704. # @name: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
  705. # do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  706. .. FIXME The parser accepts these things in almost any order.
  707. .. FIXME union branches should be described, too.
  708. Extensions added after the definition was first released carry a
  709. "(since x.y.z)" comment.
  710. The feature descriptions must be preceded by a blank line and then a
  711. line "Features:", like this::
  712. #
  713. # Features:
  714. #
  715. # @feature: Description text
  716. A tagged section begins with a paragraph that starts with one of the
  717. following words: "Since:", "Returns:", "Errors:", "TODO:". It ends with
  718. the start of a new section.
  719. The second and subsequent lines of tagged sections must be indented
  720. like this::
  721. # TODO: Ut enim ad minim veniam, quis nostrud exercitation ullamco
  722. # laboris nisi ut aliquip ex ea commodo consequat.
  723. #
  724. # Duis aute irure dolor in reprehenderit in voluptate velit esse
  725. # cillum dolore eu fugiat nulla pariatur.
  726. "Returns" and "Errors" sections are only valid for commands. They
  727. document the success and the error response, respectively.
  728. "Errors" sections should be formatted as an rST list, each entry
  729. detailing a relevant error condition. For example::
  730. # Errors:
  731. # - If @device does not exist, DeviceNotFound
  732. # - Any other error returns a GenericError.
  733. A "Since: x.y.z" tagged section lists the release that introduced the
  734. definition.
  735. "TODO" sections are not rendered (they are for developers, not users of
  736. QMP). In other sections, the text is formatted, and rST markup can be
  737. used.
  738. QMP Examples can be added by using the ``.. qmp-example::`` directive.
  739. In its simplest form, this can be used to contain a single QMP code
  740. block which accepts standard JSON syntax with additional server
  741. directionality indicators (``->`` and ``<-``), and elisions. An
  742. elision is commonly ``...``, but it can also be or a pair of ``...``
  743. with text in between.
  744. Optionally, a plaintext title may be provided by using the ``:title:``
  745. directive option. If the title is omitted, the example title will
  746. default to "Example:".
  747. A simple QMP example::
  748. # .. qmp-example::
  749. #
  750. # -> { "execute": "query-name" }
  751. # <- { "return": { "name": "Fred" } }
  752. More complex or multi-step examples where exposition is needed before
  753. or between QMP code blocks can be created by using the ``:annotated:``
  754. directive option. When using this option, nested QMP code blocks must
  755. be entered explicitly with rST's ``::`` syntax.
  756. For example::
  757. # .. qmp-example::
  758. # :annotated:
  759. # :title: A more complex demonstration
  760. #
  761. # This is a more complex example that can use
  762. # ``arbitrary rST syntax`` in its exposition::
  763. #
  764. # -> { "execute": "query-block" }
  765. # <- { "return": [
  766. # {
  767. # "device": "ide0-hd0",
  768. # ...
  769. # }
  770. # ... more ...
  771. # ] }
  772. #
  773. # Above, lengthy output has been omitted for brevity.
  774. Highlighting in non-QMP languages can be accomplished by using the
  775. ``.. code-block:: lang`` directive, and non-highlighted text can be
  776. achieved by omitting the language argument.
  777. Examples of complete definition documentation::
  778. ##
  779. # @BlockStats:
  780. #
  781. # Statistics of a virtual block device or a block backing device.
  782. #
  783. # @device: If the stats are for a virtual block device, the name
  784. # corresponding to the virtual block device.
  785. #
  786. # @node-name: The node name of the device. (Since 2.3)
  787. #
  788. # ... more members ...
  789. #
  790. # Since: 0.14
  791. ##
  792. { 'struct': 'BlockStats',
  793. 'data': {'*device': 'str', '*node-name': 'str',
  794. ... more members ... } }
  795. ##
  796. # @query-blockstats:
  797. #
  798. # Query the @BlockStats for all virtual block devices.
  799. #
  800. # @query-nodes: If true, the command will query all the block nodes
  801. # ... explain, explain ...
  802. # (Since 2.3)
  803. #
  804. # Returns: A list of @BlockStats for each virtual block devices.
  805. #
  806. # Since: 0.14
  807. #
  808. # .. qmp-example::
  809. #
  810. # -> { "execute": "query-blockstats" }
  811. # <- {
  812. # ...
  813. # }
  814. ##
  815. { 'command': 'query-blockstats',
  816. 'data': { '*query-nodes': 'bool' },
  817. 'returns': ['BlockStats'] }
  818. Markup pitfalls
  819. ~~~~~~~~~~~~~~~
  820. A blank line is required between list items and paragraphs. Without
  821. it, the list may not be recognized, resulting in garbled output. Good
  822. example::
  823. # An event's state is modified if:
  824. #
  825. # - its name matches the @name pattern, and
  826. # - if @vcpu is given, the event has the "vcpu" property.
  827. Without the blank line this would be a single paragraph.
  828. Indentation matters. Bad example::
  829. # @none: None (no memory side cache in this proximity domain,
  830. # or cache associativity unknown)
  831. # (since 5.0)
  832. The last line's de-indent is wrong. The second and subsequent lines
  833. need to line up with each other, like this::
  834. # @none: None (no memory side cache in this proximity domain,
  835. # or cache associativity unknown)
  836. # (since 5.0)
  837. Section tags are case-sensitive and end with a colon. They are only
  838. recognized after a blank line. Good example::
  839. #
  840. # Since: 7.1
  841. Bad examples (all ordinary paragraphs)::
  842. # since: 7.1
  843. # Since 7.1
  844. # Since : 7.1
  845. Likewise, member descriptions require a colon. Good example::
  846. # @interface-id: Interface ID
  847. Bad examples (all ordinary paragraphs)::
  848. # @interface-id Interface ID
  849. # @interface-id : Interface ID
  850. Undocumented members are not flagged, yet. Instead, the generated
  851. documentation describes them as "Not documented". Think twice before
  852. adding more undocumented members.
  853. When you change documentation comments, please check the generated
  854. documentation comes out as intended!
  855. Client JSON Protocol introspection
  856. ==================================
  857. Clients of a Client JSON Protocol commonly need to figure out what
  858. exactly the server (QEMU) supports.
  859. For this purpose, QMP provides introspection via command
  860. query-qmp-schema. QGA currently doesn't support introspection.
  861. While Client JSON Protocol wire compatibility should be maintained
  862. between qemu versions, we cannot make the same guarantees for
  863. introspection stability. For example, one version of qemu may provide
  864. a non-variant optional member of a struct, and a later version rework
  865. the member to instead be non-optional and associated with a variant.
  866. Likewise, one version of qemu may list a member with open-ended type
  867. 'str', and a later version could convert it to a finite set of strings
  868. via an enum type; or a member may be converted from a specific type to
  869. an alternate that represents a choice between the original type and
  870. something else.
  871. query-qmp-schema returns a JSON array of SchemaInfo objects. These
  872. objects together describe the wire ABI, as defined in the QAPI schema.
  873. There is no specified order to the SchemaInfo objects returned; a
  874. client must search for a particular name throughout the entire array
  875. to learn more about that name, but is at least guaranteed that there
  876. will be no collisions between type, command, and event names.
  877. However, the SchemaInfo can't reflect all the rules and restrictions
  878. that apply to QMP. It's interface introspection (figuring out what's
  879. there), not interface specification. The specification is in the QAPI
  880. schema. To understand how QMP is to be used, you need to study the
  881. QAPI schema.
  882. Like any other command, query-qmp-schema is itself defined in the QAPI
  883. schema, along with the SchemaInfo type. This text attempts to give an
  884. overview how things work. For details you need to consult the QAPI
  885. schema.
  886. SchemaInfo objects have common members "name", "meta-type",
  887. "features", and additional variant members depending on the value of
  888. meta-type.
  889. Each SchemaInfo object describes a wire ABI entity of a certain
  890. meta-type: a command, event or one of several kinds of type.
  891. SchemaInfo for commands and events have the same name as in the QAPI
  892. schema.
  893. Command and event names are part of the wire ABI, but type names are
  894. not. Therefore, the SchemaInfo for types have auto-generated
  895. meaningless names. For readability, the examples in this section use
  896. meaningful type names instead.
  897. Optional member "features" exposes the entity's feature strings as a
  898. JSON array of strings.
  899. To examine a type, start with a command or event using it, then follow
  900. references by name.
  901. QAPI schema definitions not reachable that way are omitted.
  902. The SchemaInfo for a command has meta-type "command", and variant
  903. members "arg-type", "ret-type" and "allow-oob". On the wire, the
  904. "arguments" member of a client's "execute" command must conform to the
  905. object type named by "arg-type". The "return" member that the server
  906. passes in a success response conforms to the type named by "ret-type".
  907. When "allow-oob" is true, it means the command supports out-of-band
  908. execution. It defaults to false.
  909. If the command takes no arguments, "arg-type" names an object type
  910. without members. Likewise, if the command returns nothing, "ret-type"
  911. names an object type without members.
  912. Example: the SchemaInfo for command query-qmp-schema ::
  913. { "name": "query-qmp-schema", "meta-type": "command",
  914. "arg-type": "q_empty", "ret-type": "SchemaInfoList" }
  915. Type "q_empty" is an automatic object type without members, and type
  916. "SchemaInfoList" is the array of SchemaInfo type.
  917. The SchemaInfo for an event has meta-type "event", and variant member
  918. "arg-type". On the wire, a "data" member that the server passes in an
  919. event conforms to the object type named by "arg-type".
  920. If the event carries no additional information, "arg-type" names an
  921. object type without members. The event may not have a data member on
  922. the wire then.
  923. Each command or event defined with 'data' as MEMBERS object in the
  924. QAPI schema implicitly defines an object type.
  925. Example: the SchemaInfo for EVENT_C from section Events_ ::
  926. { "name": "EVENT_C", "meta-type": "event",
  927. "arg-type": "q_obj-EVENT_C-arg" }
  928. Type "q_obj-EVENT_C-arg" is an implicitly defined object type with
  929. the two members from the event's definition.
  930. The SchemaInfo for struct and union types has meta-type "object" and
  931. variant member "members".
  932. The SchemaInfo for a union type additionally has variant members "tag"
  933. and "variants".
  934. "members" is a JSON array describing the object's common members, if
  935. any. Each element is a JSON object with members "name" (the member's
  936. name), "type" (the name of its type), "features" (a JSON array of
  937. feature strings), and "default". The latter two are optional. The
  938. member is optional if "default" is present. Currently, "default" can
  939. only have value null. Other values are reserved for future
  940. extensions. The "members" array is in no particular order; clients
  941. must search the entire object when learning whether a particular
  942. member is supported.
  943. Example: the SchemaInfo for MyType from section `Struct types`_ ::
  944. { "name": "MyType", "meta-type": "object",
  945. "members": [
  946. { "name": "member1", "type": "str" },
  947. { "name": "member2", "type": "int" },
  948. { "name": "member3", "type": "str", "default": null } ] }
  949. "features" exposes the command's feature strings as a JSON array of
  950. strings.
  951. Example: the SchemaInfo for TestType from section Features_::
  952. { "name": "TestType", "meta-type": "object",
  953. "members": [
  954. { "name": "number", "type": "int" } ],
  955. "features": ["allow-negative-numbers"] }
  956. "tag" is the name of the common member serving as type tag.
  957. "variants" is a JSON array describing the object's variant members.
  958. Each element is a JSON object with members "case" (the value of type
  959. tag this element applies to) and "type" (the name of an object type
  960. that provides the variant members for this type tag value). The
  961. "variants" array is in no particular order, and is not guaranteed to
  962. list cases in the same order as the corresponding "tag" enum type.
  963. Example: the SchemaInfo for union BlockdevOptions from section
  964. `Union types`_ ::
  965. { "name": "BlockdevOptions", "meta-type": "object",
  966. "members": [
  967. { "name": "driver", "type": "BlockdevDriver" },
  968. { "name": "read-only", "type": "bool", "default": null } ],
  969. "tag": "driver",
  970. "variants": [
  971. { "case": "file", "type": "BlockdevOptionsFile" },
  972. { "case": "qcow2", "type": "BlockdevOptionsQcow2" } ] }
  973. Note that base types are "flattened": its members are included in the
  974. "members" array.
  975. The SchemaInfo for an alternate type has meta-type "alternate", and
  976. variant member "members". "members" is a JSON array. Each element is
  977. a JSON object with member "type", which names a type. Values of the
  978. alternate type conform to exactly one of its member types. There is
  979. no guarantee on the order in which "members" will be listed.
  980. Example: the SchemaInfo for BlockdevRef from section `Alternate types`_ ::
  981. { "name": "BlockdevRef", "meta-type": "alternate",
  982. "members": [
  983. { "type": "BlockdevOptions" },
  984. { "type": "str" } ] }
  985. The SchemaInfo for an array type has meta-type "array", and variant
  986. member "element-type", which names the array's element type. Array
  987. types are implicitly defined. For convenience, the array's name may
  988. resemble the element type; however, clients should examine member
  989. "element-type" instead of making assumptions based on parsing member
  990. "name".
  991. Example: the SchemaInfo for ['str'] ::
  992. { "name": "[str]", "meta-type": "array",
  993. "element-type": "str" }
  994. The SchemaInfo for an enumeration type has meta-type "enum" and
  995. variant member "members".
  996. "members" is a JSON array describing the enumeration values. Each
  997. element is a JSON object with member "name" (the member's name), and
  998. optionally "features" (a JSON array of feature strings). The
  999. "members" array is in no particular order; clients must search the
  1000. entire array when learning whether a particular value is supported.
  1001. Example: the SchemaInfo for MyEnum from section `Enumeration types`_ ::
  1002. { "name": "MyEnum", "meta-type": "enum",
  1003. "members": [
  1004. { "name": "value1" },
  1005. { "name": "value2" },
  1006. { "name": "value3" }
  1007. ] }
  1008. The SchemaInfo for a built-in type has the same name as the type in
  1009. the QAPI schema (see section `Built-in Types`_), with one exception
  1010. detailed below. It has variant member "json-type" that shows how
  1011. values of this type are encoded on the wire.
  1012. Example: the SchemaInfo for str ::
  1013. { "name": "str", "meta-type": "builtin", "json-type": "string" }
  1014. The QAPI schema supports a number of integer types that only differ in
  1015. how they map to C. They are identical as far as SchemaInfo is
  1016. concerned. Therefore, they get all mapped to a single type "int" in
  1017. SchemaInfo.
  1018. As explained above, type names are not part of the wire ABI. Not even
  1019. the names of built-in types. Clients should examine member
  1020. "json-type" instead of hard-coding names of built-in types.
  1021. Compatibility considerations
  1022. ============================
  1023. Maintaining backward compatibility at the Client JSON Protocol level
  1024. while evolving the schema requires some care. This section is about
  1025. syntactic compatibility, which is necessary, but not sufficient, for
  1026. actual compatibility.
  1027. Clients send commands with argument data, and receive command
  1028. responses with return data and events with event data.
  1029. Adding opt-in functionality to the send direction is backwards
  1030. compatible: adding commands, optional arguments, enumeration values,
  1031. union and alternate branches; turning an argument type into an
  1032. alternate of that type; making mandatory arguments optional. Clients
  1033. oblivious of the new functionality continue to work.
  1034. Incompatible changes include removing commands, command arguments,
  1035. enumeration values, union and alternate branches, adding mandatory
  1036. command arguments, and making optional arguments mandatory.
  1037. The specified behavior of an absent optional argument should remain
  1038. the same. With proper documentation, this policy still allows some
  1039. flexibility; for example, when an optional 'buffer-size' argument is
  1040. specified to default to a sensible buffer size, the actual default
  1041. value can still be changed. The specified default behavior is not the
  1042. exact size of the buffer, only that the default size is sensible.
  1043. Adding functionality to the receive direction is generally backwards
  1044. compatible: adding events, adding return and event data members.
  1045. Clients are expected to ignore the ones they don't know.
  1046. Removing "unreachable" stuff like events that can't be triggered
  1047. anymore, optional return or event data members that can't be sent
  1048. anymore, and return or event data member (enumeration) values that
  1049. can't be sent anymore makes no difference to clients, except for
  1050. introspection. The latter can conceivably confuse clients, so tread
  1051. carefully.
  1052. Incompatible changes include removing return and event data members.
  1053. Any change to a command definition's 'data' or one of the types used
  1054. there (recursively) needs to consider send direction compatibility.
  1055. Any change to a command definition's 'return', an event definition's
  1056. 'data', or one of the types used there (recursively) needs to consider
  1057. receive direction compatibility.
  1058. Any change to types used in both contexts need to consider both.
  1059. Enumeration type values and complex and alternate type members may be
  1060. reordered freely. For enumerations and alternate types, this doesn't
  1061. affect the wire encoding. For complex types, this might make the
  1062. implementation emit JSON object members in a different order, which
  1063. the Client JSON Protocol permits.
  1064. Since type names are not visible in the Client JSON Protocol, types
  1065. may be freely renamed. Even certain refactorings are invisible, such
  1066. as splitting members from one type into a common base type.
  1067. Code generation
  1068. ===============
  1069. The QAPI code generator qapi-gen.py generates code and documentation
  1070. from the schema. Together with the core QAPI libraries, this code
  1071. provides everything required to take JSON commands read in by a Client
  1072. JSON Protocol server, unmarshal the arguments into the underlying C
  1073. types, call into the corresponding C function, map the response back
  1074. to a Client JSON Protocol response to be returned to the user, and
  1075. introspect the commands.
  1076. As an example, we'll use the following schema, which describes a
  1077. single complex user-defined type, along with command which takes a
  1078. list of that type as a parameter, and returns a single element of that
  1079. type. The user is responsible for writing the implementation of
  1080. qmp_my_command(); everything else is produced by the generator.
  1081. ::
  1082. $ cat example-schema.json
  1083. { 'struct': 'UserDefOne',
  1084. 'data': { 'integer': 'int', '*string': 'str', '*flag': 'bool' } }
  1085. { 'command': 'my-command',
  1086. 'data': { 'arg1': ['UserDefOne'] },
  1087. 'returns': 'UserDefOne' }
  1088. { 'event': 'MY_EVENT' }
  1089. We run qapi-gen.py like this::
  1090. $ python scripts/qapi-gen.py --output-dir="qapi-generated" \
  1091. --prefix="example-" example-schema.json
  1092. For a more thorough look at generated code, the testsuite includes
  1093. tests/qapi-schema/qapi-schema-tests.json that covers more examples of
  1094. what the generator will accept, and compiles the resulting C code as
  1095. part of 'make check-unit'.
  1096. Code generated for QAPI types
  1097. -----------------------------
  1098. The following files are created:
  1099. ``$(prefix)qapi-types.h``
  1100. C types corresponding to types defined in the schema
  1101. ``$(prefix)qapi-types.c``
  1102. Cleanup functions for the above C types
  1103. The $(prefix) is an optional parameter used as a namespace to keep the
  1104. generated code from one schema/code-generation separated from others so code
  1105. can be generated/used from multiple schemas without clobbering previously
  1106. created code.
  1107. Example::
  1108. $ cat qapi-generated/example-qapi-types.h
  1109. [Uninteresting stuff omitted...]
  1110. #ifndef EXAMPLE_QAPI_TYPES_H
  1111. #define EXAMPLE_QAPI_TYPES_H
  1112. #include "qapi/qapi-builtin-types.h"
  1113. typedef struct UserDefOne UserDefOne;
  1114. typedef struct UserDefOneList UserDefOneList;
  1115. typedef struct q_obj_my_command_arg q_obj_my_command_arg;
  1116. struct UserDefOne {
  1117. int64_t integer;
  1118. char *string;
  1119. bool has_flag;
  1120. bool flag;
  1121. };
  1122. void qapi_free_UserDefOne(UserDefOne *obj);
  1123. G_DEFINE_AUTOPTR_CLEANUP_FUNC(UserDefOne, qapi_free_UserDefOne)
  1124. struct UserDefOneList {
  1125. UserDefOneList *next;
  1126. UserDefOne *value;
  1127. };
  1128. void qapi_free_UserDefOneList(UserDefOneList *obj);
  1129. G_DEFINE_AUTOPTR_CLEANUP_FUNC(UserDefOneList, qapi_free_UserDefOneList)
  1130. struct q_obj_my_command_arg {
  1131. UserDefOneList *arg1;
  1132. };
  1133. #endif /* EXAMPLE_QAPI_TYPES_H */
  1134. $ cat qapi-generated/example-qapi-types.c
  1135. [Uninteresting stuff omitted...]
  1136. void qapi_free_UserDefOne(UserDefOne *obj)
  1137. {
  1138. Visitor *v;
  1139. if (!obj) {
  1140. return;
  1141. }
  1142. v = qapi_dealloc_visitor_new();
  1143. visit_type_UserDefOne(v, NULL, &obj, NULL);
  1144. visit_free(v);
  1145. }
  1146. void qapi_free_UserDefOneList(UserDefOneList *obj)
  1147. {
  1148. Visitor *v;
  1149. if (!obj) {
  1150. return;
  1151. }
  1152. v = qapi_dealloc_visitor_new();
  1153. visit_type_UserDefOneList(v, NULL, &obj, NULL);
  1154. visit_free(v);
  1155. }
  1156. [Uninteresting stuff omitted...]
  1157. For a modular QAPI schema (see section `Include directives`_), code for
  1158. each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
  1159. SUBDIR/$(prefix)qapi-types-SUBMODULE.h
  1160. SUBDIR/$(prefix)qapi-types-SUBMODULE.c
  1161. If qapi-gen.py is run with option --builtins, additional files are
  1162. created:
  1163. ``qapi-builtin-types.h``
  1164. C types corresponding to built-in types
  1165. ``qapi-builtin-types.c``
  1166. Cleanup functions for the above C types
  1167. Code generated for visiting QAPI types
  1168. --------------------------------------
  1169. These are the visitor functions used to walk through and convert
  1170. between a native QAPI C data structure and some other format (such as
  1171. QObject); the generated functions are named visit_type_FOO() and
  1172. visit_type_FOO_members().
  1173. The following files are generated:
  1174. ``$(prefix)qapi-visit.c``
  1175. Visitor function for a particular C type, used to automagically
  1176. convert QObjects into the corresponding C type and vice-versa, as
  1177. well as for deallocating memory for an existing C type
  1178. ``$(prefix)qapi-visit.h``
  1179. Declarations for previously mentioned visitor functions
  1180. Example::
  1181. $ cat qapi-generated/example-qapi-visit.h
  1182. [Uninteresting stuff omitted...]
  1183. #ifndef EXAMPLE_QAPI_VISIT_H
  1184. #define EXAMPLE_QAPI_VISIT_H
  1185. #include "qapi/qapi-builtin-visit.h"
  1186. #include "example-qapi-types.h"
  1187. bool visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error **errp);
  1188. bool visit_type_UserDefOne(Visitor *v, const char *name,
  1189. UserDefOne **obj, Error **errp);
  1190. bool visit_type_UserDefOneList(Visitor *v, const char *name,
  1191. UserDefOneList **obj, Error **errp);
  1192. bool visit_type_q_obj_my_command_arg_members(Visitor *v, q_obj_my_command_arg *obj, Error **errp);
  1193. #endif /* EXAMPLE_QAPI_VISIT_H */
  1194. $ cat qapi-generated/example-qapi-visit.c
  1195. [Uninteresting stuff omitted...]
  1196. bool visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error **errp)
  1197. {
  1198. bool has_string = !!obj->string;
  1199. if (!visit_type_int(v, "integer", &obj->integer, errp)) {
  1200. return false;
  1201. }
  1202. if (visit_optional(v, "string", &has_string)) {
  1203. if (!visit_type_str(v, "string", &obj->string, errp)) {
  1204. return false;
  1205. }
  1206. }
  1207. if (visit_optional(v, "flag", &obj->has_flag)) {
  1208. if (!visit_type_bool(v, "flag", &obj->flag, errp)) {
  1209. return false;
  1210. }
  1211. }
  1212. return true;
  1213. }
  1214. bool visit_type_UserDefOne(Visitor *v, const char *name,
  1215. UserDefOne **obj, Error **errp)
  1216. {
  1217. bool ok = false;
  1218. if (!visit_start_struct(v, name, (void **)obj, sizeof(UserDefOne), errp)) {
  1219. return false;
  1220. }
  1221. if (!*obj) {
  1222. /* incomplete */
  1223. assert(visit_is_dealloc(v));
  1224. ok = true;
  1225. goto out_obj;
  1226. }
  1227. if (!visit_type_UserDefOne_members(v, *obj, errp)) {
  1228. goto out_obj;
  1229. }
  1230. ok = visit_check_struct(v, errp);
  1231. out_obj:
  1232. visit_end_struct(v, (void **)obj);
  1233. if (!ok && visit_is_input(v)) {
  1234. qapi_free_UserDefOne(*obj);
  1235. *obj = NULL;
  1236. }
  1237. return ok;
  1238. }
  1239. bool visit_type_UserDefOneList(Visitor *v, const char *name,
  1240. UserDefOneList **obj, Error **errp)
  1241. {
  1242. bool ok = false;
  1243. UserDefOneList *tail;
  1244. size_t size = sizeof(**obj);
  1245. if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
  1246. return false;
  1247. }
  1248. for (tail = *obj; tail;
  1249. tail = (UserDefOneList *)visit_next_list(v, (GenericList *)tail, size)) {
  1250. if (!visit_type_UserDefOne(v, NULL, &tail->value, errp)) {
  1251. goto out_obj;
  1252. }
  1253. }
  1254. ok = visit_check_list(v, errp);
  1255. out_obj:
  1256. visit_end_list(v, (void **)obj);
  1257. if (!ok && visit_is_input(v)) {
  1258. qapi_free_UserDefOneList(*obj);
  1259. *obj = NULL;
  1260. }
  1261. return ok;
  1262. }
  1263. bool visit_type_q_obj_my_command_arg_members(Visitor *v, q_obj_my_command_arg *obj, Error **errp)
  1264. {
  1265. if (!visit_type_UserDefOneList(v, "arg1", &obj->arg1, errp)) {
  1266. return false;
  1267. }
  1268. return true;
  1269. }
  1270. [Uninteresting stuff omitted...]
  1271. For a modular QAPI schema (see section `Include directives`_), code for
  1272. each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
  1273. SUBDIR/$(prefix)qapi-visit-SUBMODULE.h
  1274. SUBDIR/$(prefix)qapi-visit-SUBMODULE.c
  1275. If qapi-gen.py is run with option --builtins, additional files are
  1276. created:
  1277. ``qapi-builtin-visit.h``
  1278. Visitor functions for built-in types
  1279. ``qapi-builtin-visit.c``
  1280. Declarations for these visitor functions
  1281. Code generated for commands
  1282. ---------------------------
  1283. These are the marshaling/dispatch functions for the commands defined
  1284. in the schema. The generated code provides qmp_marshal_COMMAND(), and
  1285. declares qmp_COMMAND() that the user must implement.
  1286. The following files are generated:
  1287. ``$(prefix)qapi-commands.c``
  1288. Command marshal/dispatch functions for each QMP command defined in
  1289. the schema
  1290. ``$(prefix)qapi-commands.h``
  1291. Function prototypes for the QMP commands specified in the schema
  1292. ``$(prefix)qapi-commands.trace-events``
  1293. Trace event declarations, see :ref:`tracing`.
  1294. ``$(prefix)qapi-init-commands.h``
  1295. Command initialization prototype
  1296. ``$(prefix)qapi-init-commands.c``
  1297. Command initialization code
  1298. Example::
  1299. $ cat qapi-generated/example-qapi-commands.h
  1300. [Uninteresting stuff omitted...]
  1301. #ifndef EXAMPLE_QAPI_COMMANDS_H
  1302. #define EXAMPLE_QAPI_COMMANDS_H
  1303. #include "example-qapi-types.h"
  1304. UserDefOne *qmp_my_command(UserDefOneList *arg1, Error **errp);
  1305. void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp);
  1306. #endif /* EXAMPLE_QAPI_COMMANDS_H */
  1307. $ cat qapi-generated/example-qapi-commands.trace-events
  1308. # AUTOMATICALLY GENERATED, DO NOT MODIFY
  1309. qmp_enter_my_command(const char *json) "%s"
  1310. qmp_exit_my_command(const char *result, bool succeeded) "%s %d"
  1311. $ cat qapi-generated/example-qapi-commands.c
  1312. [Uninteresting stuff omitted...]
  1313. static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in,
  1314. QObject **ret_out, Error **errp)
  1315. {
  1316. Visitor *v;
  1317. v = qobject_output_visitor_new_qmp(ret_out);
  1318. if (visit_type_UserDefOne(v, "unused", &ret_in, errp)) {
  1319. visit_complete(v, ret_out);
  1320. }
  1321. visit_free(v);
  1322. v = qapi_dealloc_visitor_new();
  1323. visit_type_UserDefOne(v, "unused", &ret_in, NULL);
  1324. visit_free(v);
  1325. }
  1326. void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
  1327. {
  1328. Error *err = NULL;
  1329. bool ok = false;
  1330. Visitor *v;
  1331. UserDefOne *retval;
  1332. q_obj_my_command_arg arg = {0};
  1333. v = qobject_input_visitor_new_qmp(QOBJECT(args));
  1334. if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
  1335. goto out;
  1336. }
  1337. if (visit_type_q_obj_my_command_arg_members(v, &arg, errp)) {
  1338. ok = visit_check_struct(v, errp);
  1339. }
  1340. visit_end_struct(v, NULL);
  1341. if (!ok) {
  1342. goto out;
  1343. }
  1344. if (trace_event_get_state_backends(TRACE_QMP_ENTER_MY_COMMAND)) {
  1345. g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
  1346. trace_qmp_enter_my_command(req_json->str);
  1347. }
  1348. retval = qmp_my_command(arg.arg1, &err);
  1349. if (err) {
  1350. trace_qmp_exit_my_command(error_get_pretty(err), false);
  1351. error_propagate(errp, err);
  1352. goto out;
  1353. }
  1354. qmp_marshal_output_UserDefOne(retval, ret, errp);
  1355. if (trace_event_get_state_backends(TRACE_QMP_EXIT_MY_COMMAND)) {
  1356. g_autoptr(GString) ret_json = qobject_to_json(*ret);
  1357. trace_qmp_exit_my_command(ret_json->str, true);
  1358. }
  1359. out:
  1360. visit_free(v);
  1361. v = qapi_dealloc_visitor_new();
  1362. visit_start_struct(v, NULL, NULL, 0, NULL);
  1363. visit_type_q_obj_my_command_arg_members(v, &arg, NULL);
  1364. visit_end_struct(v, NULL);
  1365. visit_free(v);
  1366. }
  1367. [Uninteresting stuff omitted...]
  1368. $ cat qapi-generated/example-qapi-init-commands.h
  1369. [Uninteresting stuff omitted...]
  1370. #ifndef EXAMPLE_QAPI_INIT_COMMANDS_H
  1371. #define EXAMPLE_QAPI_INIT_COMMANDS_H
  1372. #include "qapi/qmp-registry.h"
  1373. void example_qmp_init_marshal(QmpCommandList *cmds);
  1374. #endif /* EXAMPLE_QAPI_INIT_COMMANDS_H */
  1375. $ cat qapi-generated/example-qapi-init-commands.c
  1376. [Uninteresting stuff omitted...]
  1377. void example_qmp_init_marshal(QmpCommandList *cmds)
  1378. {
  1379. QTAILQ_INIT(cmds);
  1380. qmp_register_command(cmds, "my-command",
  1381. qmp_marshal_my_command, 0, 0);
  1382. }
  1383. [Uninteresting stuff omitted...]
  1384. For a modular QAPI schema (see section `Include directives`_), code for
  1385. each sub-module SUBDIR/SUBMODULE.json is actually generated into::
  1386. SUBDIR/$(prefix)qapi-commands-SUBMODULE.h
  1387. SUBDIR/$(prefix)qapi-commands-SUBMODULE.c
  1388. Code generated for events
  1389. -------------------------
  1390. This is the code related to events defined in the schema, providing
  1391. qapi_event_send_EVENT().
  1392. The following files are created:
  1393. ``$(prefix)qapi-events.h``
  1394. Function prototypes for each event type
  1395. ``$(prefix)qapi-events.c``
  1396. Implementation of functions to send an event
  1397. ``$(prefix)qapi-emit-events.h``
  1398. Enumeration of all event names, and common event code declarations
  1399. ``$(prefix)qapi-emit-events.c``
  1400. Common event code definitions
  1401. Example::
  1402. $ cat qapi-generated/example-qapi-events.h
  1403. [Uninteresting stuff omitted...]
  1404. #ifndef EXAMPLE_QAPI_EVENTS_H
  1405. #define EXAMPLE_QAPI_EVENTS_H
  1406. #include "qapi/util.h"
  1407. #include "example-qapi-types.h"
  1408. void qapi_event_send_my_event(void);
  1409. #endif /* EXAMPLE_QAPI_EVENTS_H */
  1410. $ cat qapi-generated/example-qapi-events.c
  1411. [Uninteresting stuff omitted...]
  1412. void qapi_event_send_my_event(void)
  1413. {
  1414. QDict *qmp;
  1415. qmp = qmp_event_build_dict("MY_EVENT");
  1416. example_qapi_event_emit(EXAMPLE_QAPI_EVENT_MY_EVENT, qmp);
  1417. qobject_unref(qmp);
  1418. }
  1419. [Uninteresting stuff omitted...]
  1420. $ cat qapi-generated/example-qapi-emit-events.h
  1421. [Uninteresting stuff omitted...]
  1422. #ifndef EXAMPLE_QAPI_EMIT_EVENTS_H
  1423. #define EXAMPLE_QAPI_EMIT_EVENTS_H
  1424. #include "qapi/util.h"
  1425. typedef enum example_QAPIEvent {
  1426. EXAMPLE_QAPI_EVENT_MY_EVENT,
  1427. EXAMPLE_QAPI_EVENT__MAX,
  1428. } example_QAPIEvent;
  1429. #define example_QAPIEvent_str(val) \
  1430. qapi_enum_lookup(&example_QAPIEvent_lookup, (val))
  1431. extern const QEnumLookup example_QAPIEvent_lookup;
  1432. void example_qapi_event_emit(example_QAPIEvent event, QDict *qdict);
  1433. #endif /* EXAMPLE_QAPI_EMIT_EVENTS_H */
  1434. $ cat qapi-generated/example-qapi-emit-events.c
  1435. [Uninteresting stuff omitted...]
  1436. const QEnumLookup example_QAPIEvent_lookup = {
  1437. .array = (const char *const[]) {
  1438. [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT",
  1439. },
  1440. .size = EXAMPLE_QAPI_EVENT__MAX
  1441. };
  1442. [Uninteresting stuff omitted...]
  1443. For a modular QAPI schema (see section `Include directives`_), code for
  1444. each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
  1445. SUBDIR/$(prefix)qapi-events-SUBMODULE.h
  1446. SUBDIR/$(prefix)qapi-events-SUBMODULE.c
  1447. Code generated for introspection
  1448. --------------------------------
  1449. The following files are created:
  1450. ``$(prefix)qapi-introspect.c``
  1451. Defines a string holding a JSON description of the schema
  1452. ``$(prefix)qapi-introspect.h``
  1453. Declares the above string
  1454. Example::
  1455. $ cat qapi-generated/example-qapi-introspect.h
  1456. [Uninteresting stuff omitted...]
  1457. #ifndef EXAMPLE_QAPI_INTROSPECT_H
  1458. #define EXAMPLE_QAPI_INTROSPECT_H
  1459. #include "qobject/qlit.h"
  1460. extern const QLitObject example_qmp_schema_qlit;
  1461. #endif /* EXAMPLE_QAPI_INTROSPECT_H */
  1462. $ cat qapi-generated/example-qapi-introspect.c
  1463. [Uninteresting stuff omitted...]
  1464. const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
  1465. QLIT_QDICT(((QLitDictEntry[]) {
  1466. { "arg-type", QLIT_QSTR("0"), },
  1467. { "meta-type", QLIT_QSTR("command"), },
  1468. { "name", QLIT_QSTR("my-command"), },
  1469. { "ret-type", QLIT_QSTR("1"), },
  1470. {}
  1471. })),
  1472. QLIT_QDICT(((QLitDictEntry[]) {
  1473. { "arg-type", QLIT_QSTR("2"), },
  1474. { "meta-type", QLIT_QSTR("event"), },
  1475. { "name", QLIT_QSTR("MY_EVENT"), },
  1476. {}
  1477. })),
  1478. /* "0" = q_obj_my-command-arg */
  1479. QLIT_QDICT(((QLitDictEntry[]) {
  1480. { "members", QLIT_QLIST(((QLitObject[]) {
  1481. QLIT_QDICT(((QLitDictEntry[]) {
  1482. { "name", QLIT_QSTR("arg1"), },
  1483. { "type", QLIT_QSTR("[1]"), },
  1484. {}
  1485. })),
  1486. {}
  1487. })), },
  1488. { "meta-type", QLIT_QSTR("object"), },
  1489. { "name", QLIT_QSTR("0"), },
  1490. {}
  1491. })),
  1492. /* "1" = UserDefOne */
  1493. QLIT_QDICT(((QLitDictEntry[]) {
  1494. { "members", QLIT_QLIST(((QLitObject[]) {
  1495. QLIT_QDICT(((QLitDictEntry[]) {
  1496. { "name", QLIT_QSTR("integer"), },
  1497. { "type", QLIT_QSTR("int"), },
  1498. {}
  1499. })),
  1500. QLIT_QDICT(((QLitDictEntry[]) {
  1501. { "default", QLIT_QNULL, },
  1502. { "name", QLIT_QSTR("string"), },
  1503. { "type", QLIT_QSTR("str"), },
  1504. {}
  1505. })),
  1506. QLIT_QDICT(((QLitDictEntry[]) {
  1507. { "default", QLIT_QNULL, },
  1508. { "name", QLIT_QSTR("flag"), },
  1509. { "type", QLIT_QSTR("bool"), },
  1510. {}
  1511. })),
  1512. {}
  1513. })), },
  1514. { "meta-type", QLIT_QSTR("object"), },
  1515. { "name", QLIT_QSTR("1"), },
  1516. {}
  1517. })),
  1518. /* "2" = q_empty */
  1519. QLIT_QDICT(((QLitDictEntry[]) {
  1520. { "members", QLIT_QLIST(((QLitObject[]) {
  1521. {}
  1522. })), },
  1523. { "meta-type", QLIT_QSTR("object"), },
  1524. { "name", QLIT_QSTR("2"), },
  1525. {}
  1526. })),
  1527. QLIT_QDICT(((QLitDictEntry[]) {
  1528. { "element-type", QLIT_QSTR("1"), },
  1529. { "meta-type", QLIT_QSTR("array"), },
  1530. { "name", QLIT_QSTR("[1]"), },
  1531. {}
  1532. })),
  1533. QLIT_QDICT(((QLitDictEntry[]) {
  1534. { "json-type", QLIT_QSTR("int"), },
  1535. { "meta-type", QLIT_QSTR("builtin"), },
  1536. { "name", QLIT_QSTR("int"), },
  1537. {}
  1538. })),
  1539. QLIT_QDICT(((QLitDictEntry[]) {
  1540. { "json-type", QLIT_QSTR("string"), },
  1541. { "meta-type", QLIT_QSTR("builtin"), },
  1542. { "name", QLIT_QSTR("str"), },
  1543. {}
  1544. })),
  1545. QLIT_QDICT(((QLitDictEntry[]) {
  1546. { "json-type", QLIT_QSTR("boolean"), },
  1547. { "meta-type", QLIT_QSTR("builtin"), },
  1548. { "name", QLIT_QSTR("bool"), },
  1549. {}
  1550. })),
  1551. {}
  1552. }));
  1553. [Uninteresting stuff omitted...]