blob: f9fa6f3d96f0c7b3d2d6480ddfd98790fa6d92c9 [file] [log] [blame]
Michael Rothb84da832011-07-19 14:50:46 -05001= How to use the QAPI code generator =
2
Eric Blake6fb55452015-05-04 09:04:58 -06003Copyright IBM Corp. 2011
4Copyright (C) 2012-2015 Red Hat, Inc.
5
6This work is licensed under the terms of the GNU GPL, version 2 or
7later. See the COPYING file in the top-level directory.
8
9== Introduction ==
10
Michael Rothb84da832011-07-19 14:50:46 -050011QAPI is a native C API within QEMU which provides management-level
Eric Blakee790e662015-05-04 09:04:59 -060012functionality to internal and external users. For external
13users/processes, this interface is made available by a JSON-based wire
14format for the QEMU Monitor Protocol (QMP) for controlling qemu, as
15well as the QEMU Guest Agent (QGA) for communicating with the guest.
Eric Blake363b4262015-05-04 09:05:35 -060016The remainder of this document uses "Client JSON Protocol" when
17referring to the wire contents of a QMP or QGA connection.
Michael Rothb84da832011-07-19 14:50:46 -050018
Eric Blake363b4262015-05-04 09:05:35 -060019To map Client JSON Protocol interfaces to the native C QAPI
20implementations, a JSON-based schema is used to define types and
21function signatures, and a set of scripts is used to generate types,
22signatures, and marshaling/dispatch code. This document will describe
23how the schemas, scripts, and resulting code are used.
Michael Rothb84da832011-07-19 14:50:46 -050024
25
26== QMP/Guest agent schema ==
27
Eric Blakee790e662015-05-04 09:04:59 -060028A QAPI schema file is designed to be loosely based on JSON
29(http://www.ietf.org/rfc/rfc7159.txt) with changes for quoting style
30and the use of comments; a QAPI schema file is then parsed by a python
31code generation program. A valid QAPI schema consists of a series of
32top-level expressions, with no commas between them. Where
33dictionaries (JSON objects) are used, they are parsed as python
34OrderedDicts so that ordering is preserved (for predictable layout of
35generated C structs and parameter lists). Ordering doesn't matter
36between top-level expressions or the keys within an expression, but
37does matter within dictionary values for 'data' and 'returns' members
38of a single expression. QAPI schema input is written using 'single
Eric Blake363b4262015-05-04 09:05:35 -060039quotes' instead of JSON's "double quotes" (in contrast, Client JSON
40Protocol uses no comments, and while input accepts 'single quotes' as
41an extension, output is strict JSON using only "double quotes"). As
42in JSON, trailing commas are not permitted in arrays or dictionaries.
43Input must be ASCII (although QMP supports full Unicode strings, the
44QAPI parser does not). At present, there is no place where a QAPI
45schema requires the use of JSON numbers or null.
Michael Rothb84da832011-07-19 14:50:46 -050046
Eric Blakee790e662015-05-04 09:04:59 -060047Comments are allowed; anything between an unquoted # and the following
48newline is ignored. Although there is not yet a documentation
49generator, a form of stylized comments has developed for consistently
50documenting details about an expression and when it was added to the
51schema. The documentation is delimited between two lines of ##, then
52the first line names the expression, an optional overview is provided,
53then individual documentation about each member of 'data' is provided,
54and finally, a 'Since: x.y.z' tag lists the release that introduced
55the expression. Optional fields are tagged with the phrase
56'#optional', often with their default value; and extensions added
57after the expression was first released are also given a '(since
58x.y.z)' comment. For example:
Michael Rothb84da832011-07-19 14:50:46 -050059
Eric Blakee790e662015-05-04 09:04:59 -060060 ##
61 # @BlockStats:
62 #
63 # Statistics of a virtual block device or a block backing device.
64 #
65 # @device: #optional If the stats are for a virtual block device, the name
66 # corresponding to the virtual block device.
67 #
68 # @stats: A @BlockDeviceStats for the device.
69 #
70 # @parent: #optional This describes the file block device if it has one.
71 #
72 # @backing: #optional This describes the backing block device if it has one.
73 # (Since 2.0)
74 #
75 # Since: 0.14.0
76 ##
Eric Blake3b2a8b82015-05-04 09:05:26 -060077 { 'struct': 'BlockStats',
Eric Blakee790e662015-05-04 09:04:59 -060078 'data': {'*device': 'str', 'stats': 'BlockDeviceStats',
79 '*parent': 'BlockStats',
80 '*backing': 'BlockStats'} }
Michael Rothb84da832011-07-19 14:50:46 -050081
Eric Blakee790e662015-05-04 09:04:59 -060082The schema sets up a series of types, as well as commands and events
83that will use those types. Forward references are allowed: the parser
84scans in two passes, where the first pass learns all type names, and
85the second validates the schema and generates the code. This allows
86the definition of complex structs that can have mutually recursive
Eric Blake363b4262015-05-04 09:05:35 -060087types, and allows for indefinite nesting of Client JSON Protocol that
88satisfies the schema. A type name should not be defined more than
89once. It is permissible for the schema to contain additional types
90not used by any commands or events in the Client JSON Protocol, for
91the side effect of generated C code used internally.
Michael Rothb84da832011-07-19 14:50:46 -050092
Eric Blake7b1b98c2015-05-04 09:05:12 -060093There are seven top-level expressions recognized by the parser:
Eric Blake3b2a8b82015-05-04 09:05:26 -060094'include', 'command', 'struct', 'enum', 'union', 'alternate', and
Eric Blake7b1b98c2015-05-04 09:05:12 -060095'event'. There are several groups of types: simple types (a number of
96built-in types, such as 'int' and 'str'; as well as enumerations),
97complex types (structs and two flavors of unions), and alternate types
98(a choice between other types). The 'command' and 'event' expressions
Eric Blakee790e662015-05-04 09:04:59 -060099can refer to existing types by name, or list an anonymous type as a
100dictionary. Listing a type name inside an array refers to a
101single-dimension array of that type; multi-dimension arrays are not
102directly supported (although an array of a complex struct that
103contains an array member is possible).
104
105Types, commands, and events share a common namespace. Therefore,
106generally speaking, type definitions should always use CamelCase for
107user-defined type names, while built-in types are lowercase. Type
108definitions should not end in 'Kind', as this namespace is used for
Eric Blake255960d2015-10-26 16:34:43 -0600109creating implicit C enums for visiting union types, or in 'List', as
110this namespace is used for creating array types. Command names,
Eric Blakee790e662015-05-04 09:04:59 -0600111and field names within a type, should be all lower case with words
112separated by a hyphen. However, some existing older commands and
113complex types use underscore; when extending such expressions,
114consistency is preferred over blindly avoiding underscore. Event
Eric Blake9fb081e2015-10-26 16:34:44 -0600115names should be ALL_CAPS with words separated by underscore. Field
116names cannot start with 'has-' or 'has_', as this is reserved for
117tracking optional fields.
Eric Blakee790e662015-05-04 09:04:59 -0600118
119Any name (command, event, type, field, or enum value) beginning with
120"x-" is marked experimental, and may be withdrawn or changed
121incompatibly in a future release. Downstream vendors may add
122extensions; such extensions should begin with a prefix matching
123"__RFQDN_" (for the reverse-fully-qualified-domain-name of the
124vendor), even if the rest of the name uses dash (example:
125__com.redhat_drive-mirror). Other than downstream extensions (with
126leading underscore and the use of dots), all names should begin with a
127letter, and contain only ASCII letters, digits, dash, and underscore.
Eric Blake9fb081e2015-10-26 16:34:44 -0600128Names beginning with 'q_' are reserved for the generator: QMP names
129that resemble C keywords or other problematic strings will be munged
130in C to use this prefix. For example, a field named "default" in
131qapi becomes "q_default" in the generated C code.
Eric Blakee790e662015-05-04 09:04:59 -0600132
133In the rest of this document, usage lines are given for each
134expression type, with literal strings written in lower case and
135placeholders written in capitals. If a literal string includes a
136prefix of '*', that key/value pair can be omitted from the expression.
Eric Blake3b2a8b82015-05-04 09:05:26 -0600137For example, a usage statement that includes '*base':STRUCT-NAME
Eric Blakee790e662015-05-04 09:04:59 -0600138means that an expression has an optional key 'base', which if present
Eric Blake3b2a8b82015-05-04 09:05:26 -0600139must have a value that forms a struct name.
Eric Blakee790e662015-05-04 09:04:59 -0600140
141
142=== Built-in Types ===
143
Markus Armbrusterf133f2d2015-09-16 13:06:22 +0200144The following types are predefined, and map to C as follows:
145
146 Schema C JSON
147 str char * any JSON string, UTF-8
148 number double any JSON number
149 int int64_t a JSON number without fractional part
150 that fits into the C integer type
151 int8 int8_t likewise
152 int16 int16_t likewise
153 int32 int32_t likewise
154 int64 int64_t likewise
155 uint8 uint8_t likewise
156 uint16 uint16_t likewise
157 uint32 uint32_t likewise
158 uint64 uint64_t likewise
159 size uint64_t like uint64_t, except StringInputVisitor
160 accepts size suffixes
161 bool bool JSON true or false
Markus Armbruster28770e02015-09-16 13:06:24 +0200162 any QObject * any JSON value
Kevin Wolf51631492013-07-16 13:17:27 +0200163
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200164
165=== Includes ===
166
Eric Blakee790e662015-05-04 09:04:59 -0600167Usage: { 'include': STRING }
168
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200169The QAPI schema definitions can be modularized using the 'include' directive:
170
Eric Blakee790e662015-05-04 09:04:59 -0600171 { 'include': 'path/to/file.json' }
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200172
173The directive is evaluated recursively, and include paths are relative to the
Eric Blakee790e662015-05-04 09:04:59 -0600174file using the directive. Multiple includes of the same file are
Markus Armbruster4247f832015-06-09 15:24:36 +0200175idempotent. No other keys should appear in the expression, and the include
Eric Blakee790e662015-05-04 09:04:59 -0600176value should be a string.
177
178As a matter of style, it is a good idea to have all files be
179self-contained, but at the moment, nothing prevents an included file
180from making a forward reference to a type that is only introduced by
181an outer file. The parser may be made stricter in the future to
182prevent incomplete include files.
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200183
184
Eric Blake3b2a8b82015-05-04 09:05:26 -0600185=== Struct types ===
Kevin Wolf51631492013-07-16 13:17:27 +0200186
Eric Blake3b2a8b82015-05-04 09:05:26 -0600187Usage: { 'struct': STRING, 'data': DICT, '*base': STRUCT-NAME }
Eric Blakee790e662015-05-04 09:04:59 -0600188
Eric Blake3b2a8b82015-05-04 09:05:26 -0600189A struct is a dictionary containing a single 'data' key whose
Eric Blakee790e662015-05-04 09:04:59 -0600190value is a dictionary. This corresponds to a struct in C or an Object
191in JSON. Each value of the 'data' dictionary must be the name of a
192type, or a one-element array containing a type name. An example of a
Eric Blake3b2a8b82015-05-04 09:05:26 -0600193struct is:
Michael Rothb84da832011-07-19 14:50:46 -0500194
Eric Blake3b2a8b82015-05-04 09:05:26 -0600195 { 'struct': 'MyType',
Stefan Hajnocziacf83942011-10-28 15:58:26 +0100196 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
Michael Rothb84da832011-07-19 14:50:46 -0500197
Eric Blakee790e662015-05-04 09:04:59 -0600198The use of '*' as a prefix to the name means the member is optional in
Eric Blake363b4262015-05-04 09:05:35 -0600199the corresponding JSON protocol usage.
Michael Rothb84da832011-07-19 14:50:46 -0500200
Eric Blakecc162652014-05-07 09:57:41 +0800201The default initialization value of an optional argument should not be changed
202between versions of QEMU unless the new default maintains backward
203compatibility to the user-visible behavior of the old default.
204
205With proper documentation, this policy still allows some flexibility; for
206example, documenting that a default of 0 picks an optimal buffer size allows
207one release to declare the optimal size at 512 while another release declares
208the optimal size at 4096 - the user-visible behavior is not the bytes used by
209the buffer, but the fact that the buffer was optimal size.
210
211On input structures (only mentioned in the 'data' side of a command), changing
212from mandatory to optional is safe (older clients will supply the option, and
213newer clients can benefit from the default); changing from optional to
214mandatory is backwards incompatible (older clients may be omitting the option,
215and must continue to work).
216
217On output structures (only mentioned in the 'returns' side of a command),
218changing from mandatory to optional is in general unsafe (older clients may be
219expecting the field, and could crash if it is missing), although it can be done
220if the only way that the optional argument will be omitted is when it is
221triggered by the presence of a new input flag to the command that older clients
222don't know to send. Changing from optional to mandatory is safe.
223
224A structure that is used in both input and output of various commands
225must consider the backwards compatibility constraints of both directions
226of use.
Kevin Wolf622f5572013-09-19 11:56:36 +0200227
Eric Blake3b2a8b82015-05-04 09:05:26 -0600228A struct definition can specify another struct as its base.
Kevin Wolf622f5572013-09-19 11:56:36 +0200229In this case, the fields of the base type are included as top-level fields
Eric Blake363b4262015-05-04 09:05:35 -0600230of the new struct's dictionary in the Client JSON Protocol wire
231format. An example definition is:
Kevin Wolf622f5572013-09-19 11:56:36 +0200232
Eric Blake3b2a8b82015-05-04 09:05:26 -0600233 { 'struct': 'BlockdevOptionsGenericFormat', 'data': { 'file': 'str' } }
234 { 'struct': 'BlockdevOptionsGenericCOWFormat',
Kevin Wolf622f5572013-09-19 11:56:36 +0200235 'base': 'BlockdevOptionsGenericFormat',
236 'data': { '*backing': 'str' } }
237
238An example BlockdevOptionsGenericCOWFormat object on the wire could use
239both fields like this:
240
241 { "file": "/some/place/my-image",
242 "backing": "/some/place/my-backing-file" }
243
Eric Blakee790e662015-05-04 09:04:59 -0600244
Kevin Wolf51631492013-07-16 13:17:27 +0200245=== Enumeration types ===
246
Eric Blakee790e662015-05-04 09:04:59 -0600247Usage: { 'enum': STRING, 'data': ARRAY-OF-STRING }
Daniel P. Berrange351d36e2015-08-26 14:21:20 +0100248 { 'enum': STRING, '*prefix': STRING, 'data': ARRAY-OF-STRING }
Eric Blakee790e662015-05-04 09:04:59 -0600249
250An enumeration type is a dictionary containing a single 'data' key
251whose value is a list of strings. An example enumeration is:
Michael Rothb84da832011-07-19 14:50:46 -0500252
253 { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
254
Eric Blakee790e662015-05-04 09:04:59 -0600255Nothing prevents an empty enumeration, although it is probably not
256useful. The list of strings should be lower case; if an enum name
257represents multiple words, use '-' between words. The string 'max' is
258not allowed as an enum value, and values should not be repeated.
259
Daniel P. Berrange351d36e2015-08-26 14:21:20 +0100260The enum constants will be named by using a heuristic to turn the
261type name into a set of underscore separated words. For the example
262above, 'MyEnum' will turn into 'MY_ENUM' giving a constant name
263of 'MY_ENUM_VALUE1' for the first value. If the default heuristic
264does not result in a desirable name, the optional 'prefix' field
265can be used when defining the enum.
266
Eric Blake363b4262015-05-04 09:05:35 -0600267The enumeration values are passed as strings over the Client JSON
268Protocol, but are encoded as C enum integral values in generated code.
269While the C code starts numbering at 0, it is better to use explicit
Eric Blakee790e662015-05-04 09:04:59 -0600270comparisons to enum values than implicit comparisons to 0; the C code
271will also include a generated enum member ending in _MAX for tracking
272the size of the enum, useful when using common functions for
273converting between strings and enum values. Since the wire format
274always passes by name, it is acceptable to reorder or add new
Eric Blake363b4262015-05-04 09:05:35 -0600275enumeration members in any location without breaking clients of Client
276JSON Protocol; however, removing enum values would break
277compatibility. For any struct that has a field that will only contain
278a finite set of string values, using an enum type for that field is
279better than open-coding the field to be type 'str'.
Eric Blakee790e662015-05-04 09:04:59 -0600280
281
Kevin Wolf51631492013-07-16 13:17:27 +0200282=== Union types ===
283
Eric Blakee790e662015-05-04 09:04:59 -0600284Usage: { 'union': STRING, 'data': DICT }
Eric Blake3b2a8b82015-05-04 09:05:26 -0600285or: { 'union': STRING, 'data': DICT, 'base': STRUCT-NAME,
Eric Blakee790e662015-05-04 09:04:59 -0600286 'discriminator': ENUM-MEMBER-OF-BASE }
Eric Blakee790e662015-05-04 09:04:59 -0600287
288Union types are used to let the user choose between several different
Eric Blake7b1b98c2015-05-04 09:05:12 -0600289variants for an object. There are two flavors: simple (no
290discriminator or base), flat (both discriminator and base). A union
291type is defined using a data dictionary as explained in the following
292paragraphs.
Kevin Wolf51631492013-07-16 13:17:27 +0200293
Eric Blakee790e662015-05-04 09:04:59 -0600294A simple union type defines a mapping from automatic discriminator
295values to data types like in this example:
Kevin Wolf51631492013-07-16 13:17:27 +0200296
Eric Blake3b2a8b82015-05-04 09:05:26 -0600297 { 'struct': 'FileOptions', 'data': { 'filename': 'str' } }
298 { 'struct': 'Qcow2Options',
Kevin Wolf51631492013-07-16 13:17:27 +0200299 'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
300
301 { 'union': 'BlockdevOptions',
302 'data': { 'file': 'FileOptions',
303 'qcow2': 'Qcow2Options' } }
304
Eric Blake363b4262015-05-04 09:05:35 -0600305In the Client JSON Protocol, a simple union is represented by a
306dictionary that contains the 'type' field as a discriminator, and a
307'data' field that is of the specified data type corresponding to the
308discriminator value, as in these examples:
Kevin Wolf51631492013-07-16 13:17:27 +0200309
Eric Blakee790e662015-05-04 09:04:59 -0600310 { "type": "file", "data" : { "filename": "/some/place/my-image" } }
Kevin Wolf51631492013-07-16 13:17:27 +0200311 { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
312 "lazy-refcounts": true } }
313
Eric Blakee790e662015-05-04 09:04:59 -0600314The generated C code uses a struct containing a union. Additionally,
315an implicit C enum 'NameKind' is created, corresponding to the union
316'Name', for accessing the various branches of the union. No branch of
317the union can be named 'max', as this would collide with the implicit
318enum. The value for each branch can be of any type.
Kevin Wolf51631492013-07-16 13:17:27 +0200319
Eric Blake3b2a8b82015-05-04 09:05:26 -0600320A flat union definition specifies a struct as its base, and
Eric Blakee790e662015-05-04 09:04:59 -0600321avoids nesting on the wire. All branches of the union must be
322complex types, and the top-level fields of the union dictionary on
323the wire will be combination of fields from both the base type and the
324appropriate branch type (when merging two dictionaries, there must be
325no keys in common). The 'discriminator' field must be the name of an
Eric Blake3b2a8b82015-05-04 09:05:26 -0600326enum-typed member of the base struct.
Eric Blakee790e662015-05-04 09:04:59 -0600327
328The following example enhances the above simple union example by
329adding a common field 'readonly', renaming the discriminator to
330something more applicable, and reducing the number of {} required on
331the wire:
Kevin Wolf50f2bdc2013-07-03 15:58:57 +0200332
Markus Armbruster94a3f0a2015-09-03 10:18:06 +0200333 { 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] }
Eric Blake3b2a8b82015-05-04 09:05:26 -0600334 { 'struct': 'BlockdevCommonOptions',
Wenchao Xiabceae762014-03-06 17:08:56 -0800335 'data': { 'driver': 'BlockdevDriver', 'readonly': 'bool' } }
Kevin Wolf50f2bdc2013-07-03 15:58:57 +0200336 { 'union': 'BlockdevOptions',
337 'base': 'BlockdevCommonOptions',
338 'discriminator': 'driver',
Eric Blakee790e662015-05-04 09:04:59 -0600339 'data': { 'file': 'FileOptions',
Kevin Wolf50f2bdc2013-07-03 15:58:57 +0200340 'qcow2': 'Qcow2Options' } }
341
Eric Blakee790e662015-05-04 09:04:59 -0600342Resulting in these JSON objects:
Kevin Wolf50f2bdc2013-07-03 15:58:57 +0200343
Eric Blakee790e662015-05-04 09:04:59 -0600344 { "driver": "file", "readonly": true,
345 "filename": "/some/place/my-image" }
346 { "driver": "qcow2", "readonly": false,
347 "backing-file": "/some/place/my-image", "lazy-refcounts": true }
348
349Notice that in a flat union, the discriminator name is controlled by
350the user, but because it must map to a base member with enum type, the
351code generator can ensure that branches exist for all values of the
352enum (although the order of the keys need not match the declaration of
353the enum). In the resulting generated C data types, a flat union is
354represented as a struct with the base member fields included directly,
355and then a union of structures for each branch of the struct.
356
357A simple union can always be re-written as a flat union where the base
358class has a single member named 'type', and where each branch of the
Eric Blake3b2a8b82015-05-04 09:05:26 -0600359union has a struct with a single member named 'data'. That is,
Eric Blakee790e662015-05-04 09:04:59 -0600360
361 { 'union': 'Simple', 'data': { 'one': 'str', 'two': 'int' } }
362
363is identical on the wire to:
364
365 { 'enum': 'Enum', 'data': ['one', 'two'] }
Eric Blake3b2a8b82015-05-04 09:05:26 -0600366 { 'struct': 'Base', 'data': { 'type': 'Enum' } }
367 { 'struct': 'Branch1', 'data': { 'data': 'str' } }
368 { 'struct': 'Branch2', 'data': { 'data': 'int' } }
Markus Armbruster94a3f0a2015-09-03 10:18:06 +0200369 { 'union': 'Flat', 'base': 'Base', 'discriminator': 'type',
Eric Blakee790e662015-05-04 09:04:59 -0600370 'data': { 'one': 'Branch1', 'two': 'Branch2' } }
Kevin Wolf50f2bdc2013-07-03 15:58:57 +0200371
372
Eric Blake7b1b98c2015-05-04 09:05:12 -0600373=== Alternate types ===
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200374
Eric Blake7b1b98c2015-05-04 09:05:12 -0600375Usage: { 'alternate': STRING, 'data': DICT }
376
377An alternate type is one that allows a choice between two or more JSON
378data types (string, integer, number, or object, but currently not
379array) on the wire. The definition is similar to a simple union type,
380where each branch of the union names a QAPI type. For example:
381
382 { 'alternate': 'BlockRef',
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200383 'data': { 'definition': 'BlockdevOptions',
384 'reference': 'str' } }
385
Eric Blake7b1b98c2015-05-04 09:05:12 -0600386Just like for a simple union, an implicit C enum 'NameKind' is created
387to enumerate the branches for the alternate 'Name'.
388
389Unlike a union, the discriminator string is never passed on the wire
Eric Blake363b4262015-05-04 09:05:35 -0600390for the Client JSON Protocol. Instead, the value's JSON type serves
391as an implicit discriminator, which in turn means that an alternate
392can only express a choice between types represented differently in
393JSON. If a branch is typed as the 'bool' built-in, the alternate
394accepts true and false; if it is typed as any of the various numeric
395built-ins, it accepts a JSON number; if it is typed as a 'str'
396built-in or named enum type, it accepts a JSON string; and if it is
397typed as a complex type (struct or union), it accepts a JSON object.
398Two different complex types, for instance, aren't permitted, because
399both are represented as a JSON object.
Eric Blake7b1b98c2015-05-04 09:05:12 -0600400
401The example alternate declaration above allows using both of the
402following example objects:
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200403
404 { "file": "my_existing_block_device_id" }
405 { "file": { "driver": "file",
406 "readonly": false,
Eric Blake63922c62013-10-19 17:52:33 +0100407 "filename": "/tmp/mydisk.qcow2" } }
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200408
409
Kevin Wolf51631492013-07-16 13:17:27 +0200410=== Commands ===
Michael Rothb84da832011-07-19 14:50:46 -0500411
Eric Blakee790e662015-05-04 09:04:59 -0600412Usage: { 'command': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT,
Markus Armbruster9b090d42015-07-31 17:59:38 +0200413 '*returns': TYPE-NAME,
Eric Blakee790e662015-05-04 09:04:59 -0600414 '*gen': false, '*success-response': false }
Michael Rothb84da832011-07-19 14:50:46 -0500415
Eric Blakee790e662015-05-04 09:04:59 -0600416Commands are defined by using a dictionary containing several members,
417where three members are most common. The 'command' member is a
Eric Blake363b4262015-05-04 09:05:35 -0600418mandatory string, and determines the "execute" value passed in a
419Client JSON Protocol command exchange.
Michael Rothb84da832011-07-19 14:50:46 -0500420
Eric Blakee790e662015-05-04 09:04:59 -0600421The 'data' argument maps to the "arguments" dictionary passed in as
Eric Blake363b4262015-05-04 09:05:35 -0600422part of a Client JSON Protocol command. The 'data' member is optional
423and defaults to {} (an empty dictionary). If present, it must be the
Markus Armbruster315932b2015-07-01 10:12:24 +0200424string name of a complex type, or a dictionary that declares an
425anonymous type with the same semantics as a 'struct' expression, with
426one exception noted below when 'gen' is used.
Eric Blakee790e662015-05-04 09:04:59 -0600427
428The 'returns' member describes what will appear in the "return" field
Eric Blake363b4262015-05-04 09:05:35 -0600429of a Client JSON Protocol reply on successful completion of a command.
430The member is optional from the command declaration; if absent, the
431"return" field will be an empty dictionary. If 'returns' is present,
432it must be the string name of a complex or built-in type, a
433one-element array containing the name of a complex or built-in type,
Markus Armbruster9b090d42015-07-31 17:59:38 +0200434with one exception noted below when 'gen' is used. Although it is
435permitted to have the 'returns' member name a built-in type or an
436array of built-in types, any command that does this cannot be extended
437to return additional information in the future; thus, new commands
438should strongly consider returning a dictionary-based type or an array
439of dictionaries, even if the dictionary only contains one field at the
440present.
Eric Blakee790e662015-05-04 09:04:59 -0600441
Eric Blake363b4262015-05-04 09:05:35 -0600442All commands in Client JSON Protocol use a dictionary to report
443failure, with no way to specify that in QAPI. Where the error return
444is different than the usual GenericError class in order to help the
445client react differently to certain error conditions, it is worth
446documenting this in the comments before the command declaration.
Eric Blakee790e662015-05-04 09:04:59 -0600447
448Some example commands:
449
450 { 'command': 'my-first-command',
451 'data': { 'arg1': 'str', '*arg2': 'str' } }
Eric Blake3b2a8b82015-05-04 09:05:26 -0600452 { 'struct': 'MyType', 'data': { '*value': 'str' } }
Eric Blakee790e662015-05-04 09:04:59 -0600453 { 'command': 'my-second-command',
454 'returns': [ 'MyType' ] }
455
Eric Blake363b4262015-05-04 09:05:35 -0600456which would validate this Client JSON Protocol transaction:
Eric Blakee790e662015-05-04 09:04:59 -0600457
458 => { "execute": "my-first-command",
459 "arguments": { "arg1": "hello" } }
460 <= { "return": { } }
461 => { "execute": "my-second-command" }
462 <= { "return": [ { "value": "one" }, { } ] }
463
464In rare cases, QAPI cannot express a type-safe representation of a
Markus Armbruster2d212912015-09-16 13:06:27 +0200465corresponding Client JSON Protocol command. You then have to suppress
466generation of a marshalling function by including a key 'gen' with
467boolean value false, and instead write your own function. Please try
468to avoid adding new commands that rely on this, and instead use
469type-safe unions. For an example of this usage:
Eric Blakee790e662015-05-04 09:04:59 -0600470
471 { 'command': 'netdev_add',
Markus Armbrusterb8a98322015-09-16 13:06:26 +0200472 'data': {'type': 'str', 'id': 'str'},
Eric Blakee790e662015-05-04 09:04:59 -0600473 'gen': false }
474
475Normally, the QAPI schema is used to describe synchronous exchanges,
476where a response is expected. But in some cases, the action of a
477command is expected to change state in a way that a successful
478response is not possible (although the command will still return a
479normal dictionary error on failure). When a successful reply is not
480possible, the command expression should include the optional key
481'success-response' with boolean value false. So far, only QGA makes
482use of this field.
483
Michael Rothb84da832011-07-19 14:50:46 -0500484
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200485=== Events ===
486
Eric Blakee790e662015-05-04 09:04:59 -0600487Usage: { 'event': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT }
488
489Events are defined with the keyword 'event'. It is not allowed to
490name an event 'MAX', since the generator also produces a C enumeration
491of all event names with a generated _MAX value at the end. When
492'data' is also specified, additional info will be included in the
Eric Blake3b2a8b82015-05-04 09:05:26 -0600493event, with similar semantics to a 'struct' expression. Finally there
Eric Blakee790e662015-05-04 09:04:59 -0600494will be C API generated in qapi-event.h; when called by QEMU code, a
495message with timestamp will be emitted on the wire.
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200496
497An example event is:
498
499{ 'event': 'EVENT_C',
500 'data': { '*a': 'int', 'b': 'str' } }
501
502Resulting in this JSON object:
503
504{ "event": "EVENT_C",
505 "data": { "b": "test string" },
506 "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
Michael Rothb84da832011-07-19 14:50:46 -0500507
Eric Blake59a2c4c2014-09-26 09:20:33 -0600508
Markus Armbruster39a18152015-09-16 13:06:28 +0200509== Client JSON Protocol introspection ==
510
511Clients of a Client JSON Protocol commonly need to figure out what
512exactly the server (QEMU) supports.
513
514For this purpose, QMP provides introspection via command
515query-qmp-schema. QGA currently doesn't support introspection.
516
517query-qmp-schema returns a JSON array of SchemaInfo objects. These
518objects together describe the wire ABI, as defined in the QAPI schema.
Eric Blakef5455042015-11-05 23:35:36 -0700519There is no specified order to the SchemaInfo objects returned; a
520client must search for a particular name throughout the entire array
521to learn more about that name, but is at least guaranteed that there
522will be no collisions between type, command, and event names.
Markus Armbruster39a18152015-09-16 13:06:28 +0200523
524However, the SchemaInfo can't reflect all the rules and restrictions
525that apply to QMP. It's interface introspection (figuring out what's
526there), not interface specification. The specification is in the QAPI
527schema. To understand how QMP is to be used, you need to study the
528QAPI schema.
529
530Like any other command, query-qmp-schema is itself defined in the QAPI
531schema, along with the SchemaInfo type. This text attempts to give an
532overview how things work. For details you need to consult the QAPI
533schema.
534
535SchemaInfo objects have common members "name" and "meta-type", and
536additional variant members depending on the value of meta-type.
537
538Each SchemaInfo object describes a wire ABI entity of a certain
539meta-type: a command, event or one of several kinds of type.
540
Markus Armbruster1a9a5072015-09-16 13:06:29 +0200541SchemaInfo for commands and events have the same name as in the QAPI
542schema.
Markus Armbruster39a18152015-09-16 13:06:28 +0200543
544Command and event names are part of the wire ABI, but type names are
Markus Armbruster1a9a5072015-09-16 13:06:29 +0200545not. Therefore, the SchemaInfo for types have auto-generated
546meaningless names. For readability, the examples in this section use
547meaningful type names instead.
548
549To examine a type, start with a command or event using it, then follow
550references by name.
Markus Armbruster39a18152015-09-16 13:06:28 +0200551
552QAPI schema definitions not reachable that way are omitted.
553
554The SchemaInfo for a command has meta-type "command", and variant
555members "arg-type" and "ret-type". On the wire, the "arguments"
556member of a client's "execute" command must conform to the object type
557named by "arg-type". The "return" member that the server passes in a
558success response conforms to the type named by "ret-type".
559
560If the command takes no arguments, "arg-type" names an object type
561without members. Likewise, if the command returns nothing, "ret-type"
562names an object type without members.
563
564Example: the SchemaInfo for command query-qmp-schema
565
566 { "name": "query-qmp-schema", "meta-type": "command",
567 "arg-type": ":empty", "ret-type": "SchemaInfoList" }
568
569 Type ":empty" is an object type without members, and type
570 "SchemaInfoList" is the array of SchemaInfo type.
571
572The SchemaInfo for an event has meta-type "event", and variant member
573"arg-type". On the wire, a "data" member that the server passes in an
574event conforms to the object type named by "arg-type".
575
576If the event carries no additional information, "arg-type" names an
577object type without members. The event may not have a data member on
578the wire then.
579
580Each command or event defined with dictionary-valued 'data' in the
Markus Armbruster1a9a5072015-09-16 13:06:29 +0200581QAPI schema implicitly defines an object type.
Markus Armbruster39a18152015-09-16 13:06:28 +0200582
583Example: the SchemaInfo for EVENT_C from section Events
584
585 { "name": "EVENT_C", "meta-type": "event",
586 "arg-type": ":obj-EVENT_C-arg" }
587
588 Type ":obj-EVENT_C-arg" is an implicitly defined object type with
589 the two members from the event's definition.
590
591The SchemaInfo for struct and union types has meta-type "object".
592
593The SchemaInfo for a struct type has variant member "members".
594
595The SchemaInfo for a union type additionally has variant members "tag"
596and "variants".
597
598"members" is a JSON array describing the object's common members, if
599any. Each element is a JSON object with members "name" (the member's
600name), "type" (the name of its type), and optionally "default". The
601member is optional if "default" is present. Currently, "default" can
602only have value null. Other values are reserved for future
Eric Blakef5455042015-11-05 23:35:36 -0700603extensions. The "members" array is in no particular order; clients
604must search the entire object when learning whether a particular
605member is supported.
Markus Armbruster39a18152015-09-16 13:06:28 +0200606
607Example: the SchemaInfo for MyType from section Struct types
608
609 { "name": "MyType", "meta-type": "object",
610 "members": [
611 { "name": "member1", "type": "str" },
612 { "name": "member2", "type": "int" },
613 { "name": "member3", "type": "str", "default": null } ] }
614
615"tag" is the name of the common member serving as type tag.
616"variants" is a JSON array describing the object's variant members.
617Each element is a JSON object with members "case" (the value of type
618tag this element applies to) and "type" (the name of an object type
Eric Blakef5455042015-11-05 23:35:36 -0700619that provides the variant members for this type tag value). The
620"variants" array is in no particular order, and is not guaranteed to
621list cases in the same order as the corresponding "tag" enum type.
Markus Armbruster39a18152015-09-16 13:06:28 +0200622
623Example: the SchemaInfo for flat union BlockdevOptions from section
624Union types
625
626 { "name": "BlockdevOptions", "meta-type": "object",
627 "members": [
628 { "name": "driver", "type": "BlockdevDriver" },
629 { "name": "readonly", "type": "bool"} ],
630 "tag": "driver",
631 "variants": [
632 { "case": "file", "type": "FileOptions" },
633 { "case": "qcow2", "type": "Qcow2Options" } ] }
634
635Note that base types are "flattened": its members are included in the
636"members" array.
637
638A simple union implicitly defines an enumeration type for its implicit
639discriminator (called "type" on the wire, see section Union types).
Markus Armbruster39a18152015-09-16 13:06:28 +0200640
641A simple union implicitly defines an object type for each of its
Markus Armbruster1a9a5072015-09-16 13:06:29 +0200642variants.
Markus Armbruster39a18152015-09-16 13:06:28 +0200643
644Example: the SchemaInfo for simple union BlockdevOptions from section
645Union types
646
647 { "name": "BlockdevOptions", "meta-type": "object",
648 "members": [
649 { "name": "kind", "type": "BlockdevOptionsKind" } ],
650 "tag": "type",
651 "variants": [
652 { "case": "file", "type": ":obj-FileOptions-wrapper" },
653 { "case": "qcow2", "type": ":obj-Qcow2Options-wrapper" } ] }
654
655 Enumeration type "BlockdevOptionsKind" and the object types
656 ":obj-FileOptions-wrapper", ":obj-Qcow2Options-wrapper" are
657 implicitly defined.
658
659The SchemaInfo for an alternate type has meta-type "alternate", and
660variant member "members". "members" is a JSON array. Each element is
661a JSON object with member "type", which names a type. Values of the
Eric Blakef5455042015-11-05 23:35:36 -0700662alternate type conform to exactly one of its member types. There is
663no guarantee on the order in which "members" will be listed.
Markus Armbruster39a18152015-09-16 13:06:28 +0200664
665Example: the SchemaInfo for BlockRef from section Alternate types
666
667 { "name": "BlockRef", "meta-type": "alternate",
668 "members": [
669 { "type": "BlockdevOptions" },
670 { "type": "str" } ] }
671
672The SchemaInfo for an array type has meta-type "array", and variant
673member "element-type", which names the array's element type. Array
Eric Blakece5fcb42015-11-05 23:35:35 -0700674types are implicitly defined. For convenience, the array's name may
675resemble the element type; however, clients should examine member
676"element-type" instead of making assumptions based on parsing member
677"name".
Markus Armbruster39a18152015-09-16 13:06:28 +0200678
679Example: the SchemaInfo for ['str']
680
Eric Blakece5fcb42015-11-05 23:35:35 -0700681 { "name": "[str]", "meta-type": "array",
Markus Armbruster39a18152015-09-16 13:06:28 +0200682 "element-type": "str" }
683
684The SchemaInfo for an enumeration type has meta-type "enum" and
Eric Blakef5455042015-11-05 23:35:36 -0700685variant member "values". The values are listed in no particular
686order; clients must search the entire enum when learning whether a
687particular value is supported.
Markus Armbruster39a18152015-09-16 13:06:28 +0200688
689Example: the SchemaInfo for MyEnum from section Enumeration types
690
691 { "name": "MyEnum", "meta-type": "enum",
692 "values": [ "value1", "value2", "value3" ] }
693
694The SchemaInfo for a built-in type has the same name as the type in
695the QAPI schema (see section Built-in Types), with one exception
696detailed below. It has variant member "json-type" that shows how
697values of this type are encoded on the wire.
698
699Example: the SchemaInfo for str
700
701 { "name": "str", "meta-type": "builtin", "json-type": "string" }
702
703The QAPI schema supports a number of integer types that only differ in
704how they map to C. They are identical as far as SchemaInfo is
705concerned. Therefore, they get all mapped to a single type "int" in
706SchemaInfo.
707
708As explained above, type names are not part of the wire ABI. Not even
709the names of built-in types. Clients should examine member
710"json-type" instead of hard-coding names of built-in types.
711
712
Michael Rothb84da832011-07-19 14:50:46 -0500713== Code generation ==
714
Markus Armbruster39a18152015-09-16 13:06:28 +0200715Schemas are fed into four scripts to generate all the code/files that,
716paired with the core QAPI libraries, comprise everything required to
717take JSON commands read in by a Client JSON Protocol server, unmarshal
718the arguments into the underlying C types, call into the corresponding
719C function, and map the response back to a Client JSON Protocol
720response to be returned to the user.
Michael Rothb84da832011-07-19 14:50:46 -0500721
722As an example, we'll use the following schema, which describes a single
723complex user-defined type (which will produce a C struct, along with a list
724node structure that can be used to chain together a list of such types in
725case we want to accept/return a list of this type with a command), and a
726command which takes that type as a parameter and returns the same type:
727
Markus Armbruster87a560c2014-05-14 17:27:23 +0200728 $ cat example-schema.json
Eric Blake3b2a8b82015-05-04 09:05:26 -0600729 { 'struct': 'UserDefOne',
Michael Rothb84da832011-07-19 14:50:46 -0500730 'data': { 'integer': 'int', 'string': 'str' } }
731
732 { 'command': 'my-command',
733 'data': {'arg1': 'UserDefOne'},
734 'returns': 'UserDefOne' }
Michael Rothb84da832011-07-19 14:50:46 -0500735
Eric Blake59a2c4c2014-09-26 09:20:33 -0600736 { 'event': 'MY_EVENT' }
737
Michael Rothb84da832011-07-19 14:50:46 -0500738=== scripts/qapi-types.py ===
739
740Used to generate the C types defined by a schema. The following files are
741created:
742
743$(prefix)qapi-types.h - C types corresponding to types defined in
744 the schema you pass in
745$(prefix)qapi-types.c - Cleanup functions for the above C types
746
747The $(prefix) is an optional parameter used as a namespace to keep the
748generated code from one schema/code-generation separated from others so code
749can be generated/used from multiple schemas without clobbering previously
750created code.
751
752Example:
753
Markus Armbruster87a560c2014-05-14 17:27:23 +0200754 $ python scripts/qapi-types.py --output-dir="qapi-generated" \
Markus Armbruster16d80f62015-04-02 13:32:16 +0200755 --prefix="example-" example-schema.json
Markus Armbruster87a560c2014-05-14 17:27:23 +0200756 $ cat qapi-generated/example-qapi-types.c
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200757[Uninteresting stuff omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500758
Eric Blake59a2c4c2014-09-26 09:20:33 -0600759 void qapi_free_UserDefOne(UserDefOne *obj)
Michael Rothb84da832011-07-19 14:50:46 -0500760 {
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600761 QapiDeallocVisitor *qdv;
Michael Rothb84da832011-07-19 14:50:46 -0500762 Visitor *v;
763
764 if (!obj) {
765 return;
766 }
767
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600768 qdv = qapi_dealloc_visitor_new();
769 v = qapi_dealloc_get_visitor(qdv);
Michael Rothb84da832011-07-19 14:50:46 -0500770 visit_type_UserDefOne(v, &obj, NULL, NULL);
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600771 qapi_dealloc_visitor_cleanup(qdv);
Michael Rothb84da832011-07-19 14:50:46 -0500772 }
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200773
774 void qapi_free_UserDefOneList(UserDefOneList *obj)
775 {
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600776 QapiDeallocVisitor *qdv;
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200777 Visitor *v;
778
779 if (!obj) {
780 return;
781 }
782
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600783 qdv = qapi_dealloc_visitor_new();
784 v = qapi_dealloc_get_visitor(qdv);
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200785 visit_type_UserDefOneList(v, &obj, NULL, NULL);
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600786 qapi_dealloc_visitor_cleanup(qdv);
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200787 }
Markus Armbruster87a560c2014-05-14 17:27:23 +0200788 $ cat qapi-generated/example-qapi-types.h
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200789[Uninteresting stuff omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500790
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200791 #ifndef EXAMPLE_QAPI_TYPES_H
792 #define EXAMPLE_QAPI_TYPES_H
793
Eric Blakee790e662015-05-04 09:04:59 -0600794[Built-in types omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500795
796 typedef struct UserDefOne UserDefOne;
797
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200798 typedef struct UserDefOneList UserDefOneList;
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200799
Markus Armbruster3a864e72015-07-01 16:55:15 +0200800 struct UserDefOne {
Michael Rothb84da832011-07-19 14:50:46 -0500801 int64_t integer;
Eric Blake59a2c4c2014-09-26 09:20:33 -0600802 char *string;
Michael Rothb84da832011-07-19 14:50:46 -0500803 };
804
Eric Blake59a2c4c2014-09-26 09:20:33 -0600805 void qapi_free_UserDefOne(UserDefOne *obj);
Michael Rothb84da832011-07-19 14:50:46 -0500806
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200807 struct UserDefOneList {
808 union {
809 UserDefOne *value;
810 uint64_t padding;
811 };
Markus Armbrustere98859a2015-09-16 13:06:16 +0200812 UserDefOneList *next;
Markus Armbruster2b162cc2015-09-16 13:06:09 +0200813 };
814
815 void qapi_free_UserDefOneList(UserDefOneList *obj);
816
Michael Rothb84da832011-07-19 14:50:46 -0500817 #endif
818
Michael Rothb84da832011-07-19 14:50:46 -0500819=== scripts/qapi-visit.py ===
820
821Used to generate the visitor functions used to walk through and convert
822a QObject (as provided by QMP) to a native C data structure and
823vice-versa, as well as the visitor function used to dealloc a complex
824schema-defined C type.
825
826The following files are generated:
827
828$(prefix)qapi-visit.c: visitor function for a particular C type, used
829 to automagically convert QObjects into the
830 corresponding C type and vice-versa, as well
831 as for deallocating memory for an existing C
832 type
833
834$(prefix)qapi-visit.h: declarations for previously mentioned visitor
835 functions
836
837Example:
838
Markus Armbruster87a560c2014-05-14 17:27:23 +0200839 $ python scripts/qapi-visit.py --output-dir="qapi-generated"
Markus Armbruster16d80f62015-04-02 13:32:16 +0200840 --prefix="example-" example-schema.json
Markus Armbruster87a560c2014-05-14 17:27:23 +0200841 $ cat qapi-generated/example-qapi-visit.c
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200842[Uninteresting stuff omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500843
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600844 static void visit_type_UserDefOne_fields(Visitor *v, UserDefOne **obj, Error **errp)
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200845 {
846 Error *err = NULL;
Markus Armbruster3a864e72015-07-01 16:55:15 +0200847
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600848 visit_type_int(v, &(*obj)->integer, "integer", &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200849 if (err) {
850 goto out;
851 }
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600852 visit_type_str(v, &(*obj)->string, "string", &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200853 if (err) {
854 goto out;
855 }
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200856
Markus Armbruster297a3642014-05-07 09:53:54 +0200857 out:
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200858 error_propagate(errp, err);
859 }
Michael Rothb84da832011-07-19 14:50:46 -0500860
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600861 void visit_type_UserDefOne(Visitor *v, UserDefOne **obj, const char *name, Error **errp)
Michael Rothb84da832011-07-19 14:50:46 -0500862 {
Markus Armbruster297a3642014-05-07 09:53:54 +0200863 Error *err = NULL;
864
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600865 visit_start_struct(v, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200866 if (!err) {
867 if (*obj) {
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600868 visit_type_UserDefOne_fields(v, obj, errp);
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200869 }
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600870 visit_end_struct(v, &err);
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200871 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200872 error_propagate(errp, err);
Michael Rothb84da832011-07-19 14:50:46 -0500873 }
874
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600875 void visit_type_UserDefOneList(Visitor *v, UserDefOneList **obj, const char *name, Error **errp)
Michael Rothb84da832011-07-19 14:50:46 -0500876 {
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200877 Error *err = NULL;
Markus Armbruster297a3642014-05-07 09:53:54 +0200878 GenericList *i, **prev;
Michael Rothb84da832011-07-19 14:50:46 -0500879
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600880 visit_start_list(v, name, &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200881 if (err) {
882 goto out;
Michael Rothb84da832011-07-19 14:50:46 -0500883 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200884
885 for (prev = (GenericList **)obj;
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600886 !err && (i = visit_next_list(v, prev, &err)) != NULL;
Markus Armbruster297a3642014-05-07 09:53:54 +0200887 prev = &i) {
888 UserDefOneList *native_i = (UserDefOneList *)i;
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600889 visit_type_UserDefOne(v, &native_i->value, NULL, &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200890 }
891
892 error_propagate(errp, err);
893 err = NULL;
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600894 visit_end_list(v, &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200895 out:
896 error_propagate(errp, err);
Michael Rothb84da832011-07-19 14:50:46 -0500897 }
Markus Armbruster87a560c2014-05-14 17:27:23 +0200898 $ cat qapi-generated/example-qapi-visit.h
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200899[Uninteresting stuff omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500900
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200901 #ifndef EXAMPLE_QAPI_VISIT_H
902 #define EXAMPLE_QAPI_VISIT_H
Michael Rothb84da832011-07-19 14:50:46 -0500903
Eric Blakee790e662015-05-04 09:04:59 -0600904[Visitors for built-in types omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500905
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600906 void visit_type_UserDefOne(Visitor *v, UserDefOne **obj, const char *name, Error **errp);
907 void visit_type_UserDefOneList(Visitor *v, UserDefOneList **obj, const char *name, Error **errp);
Michael Rothb84da832011-07-19 14:50:46 -0500908
909 #endif
Michael Rothb84da832011-07-19 14:50:46 -0500910
Michael Rothb84da832011-07-19 14:50:46 -0500911=== scripts/qapi-commands.py ===
912
913Used to generate the marshaling/dispatch functions for the commands defined
914in the schema. The following files are generated:
915
916$(prefix)qmp-marshal.c: command marshal/dispatch functions for each
917 QMP command defined in the schema. Functions
918 generated by qapi-visit.py are used to
Stefan Weil2542bfd2011-08-28 21:45:40 +0200919 convert QObjects received from the wire into
Michael Rothb84da832011-07-19 14:50:46 -0500920 function parameters, and uses the same
921 visitor functions to convert native C return
922 values to QObjects from transmission back
923 over the wire.
924
925$(prefix)qmp-commands.h: Function prototypes for the QMP commands
926 specified in the schema.
927
928Example:
929
Eric Blake59a2c4c2014-09-26 09:20:33 -0600930 $ python scripts/qapi-commands.py --output-dir="qapi-generated"
Markus Armbruster16d80f62015-04-02 13:32:16 +0200931 --prefix="example-" example-schema.json
Markus Armbruster87a560c2014-05-14 17:27:23 +0200932 $ cat qapi-generated/example-qmp-marshal.c
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200933[Uninteresting stuff omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500934
Markus Armbruster56d92b02015-09-16 13:06:21 +0200935 static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in, QObject **ret_out, Error **errp)
Michael Rothb84da832011-07-19 14:50:46 -0500936 {
Eric Blake2a0f50e2015-09-29 16:21:08 -0600937 Error *err = NULL;
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600938 QmpOutputVisitor *qov = qmp_output_visitor_new();
939 QapiDeallocVisitor *qdv;
Michael Rothb84da832011-07-19 14:50:46 -0500940 Visitor *v;
941
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600942 v = qmp_output_get_visitor(qov);
Eric Blake2a0f50e2015-09-29 16:21:08 -0600943 visit_type_UserDefOne(v, &ret_in, "unused", &err);
944 if (err) {
Markus Armbruster297a3642014-05-07 09:53:54 +0200945 goto out;
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200946 }
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600947 *ret_out = qmp_output_get_qobject(qov);
Markus Armbruster297a3642014-05-07 09:53:54 +0200948
949 out:
Eric Blake2a0f50e2015-09-29 16:21:08 -0600950 error_propagate(errp, err);
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600951 qmp_output_visitor_cleanup(qov);
952 qdv = qapi_dealloc_visitor_new();
953 v = qapi_dealloc_get_visitor(qdv);
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200954 visit_type_UserDefOne(v, &ret_in, "unused", NULL);
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600955 qapi_dealloc_visitor_cleanup(qdv);
Michael Rothb84da832011-07-19 14:50:46 -0500956 }
957
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200958 static void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
Michael Rothb84da832011-07-19 14:50:46 -0500959 {
Eric Blake2a0f50e2015-09-29 16:21:08 -0600960 Error *err = NULL;
Markus Armbruster3f991442015-07-31 18:51:18 +0200961 UserDefOne *retval;
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600962 QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
963 QapiDeallocVisitor *qdv;
Michael Rothb84da832011-07-19 14:50:46 -0500964 Visitor *v;
Eric Blake59a2c4c2014-09-26 09:20:33 -0600965 UserDefOne *arg1 = NULL;
Michael Rothb84da832011-07-19 14:50:46 -0500966
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600967 v = qmp_input_get_visitor(qiv);
Eric Blake2a0f50e2015-09-29 16:21:08 -0600968 visit_type_UserDefOne(v, &arg1, "arg1", &err);
969 if (err) {
Michael Rothb84da832011-07-19 14:50:46 -0500970 goto out;
971 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200972
Eric Blake2a0f50e2015-09-29 16:21:08 -0600973 retval = qmp_my_command(arg1, &err);
974 if (err) {
Markus Armbruster297a3642014-05-07 09:53:54 +0200975 goto out;
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200976 }
Michael Rothb84da832011-07-19 14:50:46 -0500977
Eric Blake2a0f50e2015-09-29 16:21:08 -0600978 qmp_marshal_output_UserDefOne(retval, ret, &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200979
Michael Rothb84da832011-07-19 14:50:46 -0500980 out:
Eric Blake2a0f50e2015-09-29 16:21:08 -0600981 error_propagate(errp, err);
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600982 qmp_input_visitor_cleanup(qiv);
983 qdv = qapi_dealloc_visitor_new();
984 v = qapi_dealloc_get_visitor(qdv);
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200985 visit_type_UserDefOne(v, &arg1, "arg1", NULL);
Eric Blakef8b7f1a2015-09-29 16:21:09 -0600986 qapi_dealloc_visitor_cleanup(qdv);
Michael Rothb84da832011-07-19 14:50:46 -0500987 }
988
989 static void qmp_init_marshal(void)
990 {
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200991 qmp_register_command("my-command", qmp_marshal_my_command, QCO_NO_OPTIONS);
Michael Rothb84da832011-07-19 14:50:46 -0500992 }
993
994 qapi_init(qmp_init_marshal);
Markus Armbruster87a560c2014-05-14 17:27:23 +0200995 $ cat qapi-generated/example-qmp-commands.h
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200996[Uninteresting stuff omitted...]
Michael Rothb84da832011-07-19 14:50:46 -0500997
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +0200998 #ifndef EXAMPLE_QMP_COMMANDS_H
999 #define EXAMPLE_QMP_COMMANDS_H
Michael Rothb84da832011-07-19 14:50:46 -05001000
1001 #include "example-qapi-types.h"
Markus Armbruster6e2bb3e2014-05-07 09:53:43 +02001002 #include "qapi/qmp/qdict.h"
1003 #include "qapi/error.h"
Michael Rothb84da832011-07-19 14:50:46 -05001004
Eric Blake59a2c4c2014-09-26 09:20:33 -06001005 UserDefOne *qmp_my_command(UserDefOne *arg1, Error **errp);
1006
1007 #endif
1008
1009=== scripts/qapi-event.py ===
1010
1011Used to generate the event-related C code defined by a schema. The
1012following files are created:
1013
1014$(prefix)qapi-event.h - Function prototypes for each event type, plus an
1015 enumeration of all event names
1016$(prefix)qapi-event.c - Implementation of functions to send an event
1017
1018Example:
1019
1020 $ python scripts/qapi-event.py --output-dir="qapi-generated"
Markus Armbruster16d80f62015-04-02 13:32:16 +02001021 --prefix="example-" example-schema.json
Eric Blake59a2c4c2014-09-26 09:20:33 -06001022 $ cat qapi-generated/example-qapi-event.c
1023[Uninteresting stuff omitted...]
1024
1025 void qapi_event_send_my_event(Error **errp)
1026 {
1027 QDict *qmp;
Eric Blake2a0f50e2015-09-29 16:21:08 -06001028 Error *err = NULL;
Eric Blake59a2c4c2014-09-26 09:20:33 -06001029 QMPEventFuncEmit emit;
1030 emit = qmp_event_get_func_emit();
1031 if (!emit) {
1032 return;
1033 }
1034
1035 qmp = qmp_event_build_dict("MY_EVENT");
1036
Eric Blake2a0f50e2015-09-29 16:21:08 -06001037 emit(EXAMPLE_QAPI_EVENT_MY_EVENT, qmp, &err);
Eric Blake59a2c4c2014-09-26 09:20:33 -06001038
Eric Blake2a0f50e2015-09-29 16:21:08 -06001039 error_propagate(errp, err);
Eric Blake59a2c4c2014-09-26 09:20:33 -06001040 QDECREF(qmp);
1041 }
1042
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001043 const char *const example_QAPIEvent_lookup[] = {
1044 [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT",
1045 [EXAMPLE_QAPI_EVENT_MAX] = NULL,
Eric Blake59a2c4c2014-09-26 09:20:33 -06001046 };
1047 $ cat qapi-generated/example-qapi-event.h
1048[Uninteresting stuff omitted...]
1049
1050 #ifndef EXAMPLE_QAPI_EVENT_H
1051 #define EXAMPLE_QAPI_EVENT_H
1052
1053 #include "qapi/error.h"
1054 #include "qapi/qmp/qdict.h"
1055 #include "example-qapi-types.h"
1056
1057
1058 void qapi_event_send_my_event(Error **errp);
1059
Markus Armbruster3a864e72015-07-01 16:55:15 +02001060 typedef enum example_QAPIEvent {
Eric Blake59a2c4c2014-09-26 09:20:33 -06001061 EXAMPLE_QAPI_EVENT_MY_EVENT = 0,
1062 EXAMPLE_QAPI_EVENT_MAX = 1,
Markus Armbruster016a3352015-07-01 12:59:40 +02001063 } example_QAPIEvent;
Michael Rothb84da832011-07-19 14:50:46 -05001064
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001065 extern const char *const example_QAPIEvent_lookup[];
1066
Michael Rothb84da832011-07-19 14:50:46 -05001067 #endif
Markus Armbruster39a18152015-09-16 13:06:28 +02001068
1069=== scripts/qapi-introspect.py ===
1070
1071Used to generate the introspection C code for a schema. The following
1072files are created:
1073
1074$(prefix)qmp-introspect.c - Defines a string holding a JSON
1075 description of the schema.
1076$(prefix)qmp-introspect.h - Declares the above string.
1077
1078Example:
1079
1080 $ python scripts/qapi-introspect.py --output-dir="qapi-generated"
1081 --prefix="example-" example-schema.json
1082 $ cat qapi-generated/example-qmp-introspect.c
1083[Uninteresting stuff omitted...]
1084
1085 const char example_qmp_schema_json[] = "["
Markus Armbruster1a9a5072015-09-16 13:06:29 +02001086 "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
1087 "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
1088 "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
1089 "{\"members\": [{\"name\": \"arg1\", \"type\": \"2\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
1090 "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
Markus Armbruster39a18152015-09-16 13:06:28 +02001091 "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
Markus Armbruster1a9a5072015-09-16 13:06:29 +02001092 "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
Markus Armbruster39a18152015-09-16 13:06:28 +02001093 $ cat qapi-generated/example-qmp-introspect.h
1094[Uninteresting stuff omitted...]
1095
1096 #ifndef EXAMPLE_QMP_INTROSPECT_H
1097 #define EXAMPLE_QMP_INTROSPECT_H
1098
1099 extern const char example_qmp_schema_json[];
1100
1101 #endif