qapi-code-gen.txt 58 KB

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