]> git.proxmox.com Git - mirror_qemu.git/blame - docs/qapi-code-gen.txt
qapi-commands: Rearrange code
[mirror_qemu.git] / docs / qapi-code-gen.txt
CommitLineData
b84da831
MR
1= How to use the QAPI code generator =
2
6fb55451
EB
3Copyright 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
b84da831 11QAPI is a native C API within QEMU which provides management-level
e790e666
EB
12functionality 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.
363b4262
EB
16The remainder of this document uses "Client JSON Protocol" when
17referring to the wire contents of a QMP or QGA connection.
b84da831 18
363b4262
EB
19To 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.
b84da831
MR
24
25
26== QMP/Guest agent schema ==
27
e790e666
EB
28A 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
363b4262
EB
39quotes' 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.
e790e666
EB
46
47Comments 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:
59
60 ##
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 ##
3b2a8b85 77 { 'struct': 'BlockStats',
e790e666
EB
78 'data': {'*device': 'str', 'stats': 'BlockDeviceStats',
79 '*parent': 'BlockStats',
80 '*backing': 'BlockStats'} }
81
82The 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
363b4262
EB
87types, 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.
e790e666 92
7b1b98c4 93There are seven top-level expressions recognized by the parser:
3b2a8b85 94'include', 'command', 'struct', 'enum', 'union', 'alternate', and
7b1b98c4
EB
95'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
e790e666
EB
99can 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
109creating implicit C enums for visiting union types. Command names,
110and field names within a type, should be all lower case with words
111separated by a hyphen. However, some existing older commands and
112complex types use underscore; when extending such expressions,
113consistency is preferred over blindly avoiding underscore. Event
114names should be ALL_CAPS with words separated by underscore. The
115special string '**' appears for some commands that manually perform
116their own type checking rather than relying on the type-safe code
117produced by the qapi code generators.
118
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.
128It is okay to reuse names that match C keywords; the generator will
129rename a field named "default" in the QAPI to "q_default" in the
130generated C code.
131
132In the rest of this document, usage lines are given for each
133expression type, with literal strings written in lower case and
134placeholders written in capitals. If a literal string includes a
135prefix of '*', that key/value pair can be omitted from the expression.
3b2a8b85 136For example, a usage statement that includes '*base':STRUCT-NAME
e790e666 137means that an expression has an optional key 'base', which if present
3b2a8b85 138must have a value that forms a struct name.
e790e666
EB
139
140
141=== Built-in Types ===
142
143The following types are built-in to the parser:
144 'str' - arbitrary UTF-8 string
145 'int' - 64-bit signed integer (although the C code may place further
146 restrictions on acceptable range)
147 'number' - floating point number
148 'bool' - JSON value of true or false
149 'int8', 'int16', 'int32', 'int64' - like 'int', but enforce maximum
150 bit size
151 'uint8', 'uint16', 'uint32', 'uint64' - unsigned counterparts
152 'size' - like 'uint64', but allows scaled suffix from command line
153 visitor
51631493 154
a719a27c
LV
155
156=== Includes ===
157
e790e666
EB
158Usage: { 'include': STRING }
159
a719a27c
LV
160The QAPI schema definitions can be modularized using the 'include' directive:
161
e790e666 162 { 'include': 'path/to/file.json' }
a719a27c
LV
163
164The directive is evaluated recursively, and include paths are relative to the
e790e666 165file using the directive. Multiple includes of the same file are
4247f839 166idempotent. No other keys should appear in the expression, and the include
e790e666
EB
167value should be a string.
168
169As a matter of style, it is a good idea to have all files be
170self-contained, but at the moment, nothing prevents an included file
171from making a forward reference to a type that is only introduced by
172an outer file. The parser may be made stricter in the future to
173prevent incomplete include files.
a719a27c
LV
174
175
3b2a8b85 176=== Struct types ===
51631493 177
3b2a8b85 178Usage: { 'struct': STRING, 'data': DICT, '*base': STRUCT-NAME }
e790e666 179
3b2a8b85 180A struct is a dictionary containing a single 'data' key whose
e790e666
EB
181value is a dictionary. This corresponds to a struct in C or an Object
182in JSON. Each value of the 'data' dictionary must be the name of a
183type, or a one-element array containing a type name. An example of a
3b2a8b85 184struct is:
b84da831 185
3b2a8b85 186 { 'struct': 'MyType',
acf8394e 187 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
b84da831 188
e790e666 189The use of '*' as a prefix to the name means the member is optional in
363b4262 190the corresponding JSON protocol usage.
cc162655
EB
191
192The default initialization value of an optional argument should not be changed
193between versions of QEMU unless the new default maintains backward
194compatibility to the user-visible behavior of the old default.
195
196With proper documentation, this policy still allows some flexibility; for
197example, documenting that a default of 0 picks an optimal buffer size allows
198one release to declare the optimal size at 512 while another release declares
199the optimal size at 4096 - the user-visible behavior is not the bytes used by
200the buffer, but the fact that the buffer was optimal size.
201
202On input structures (only mentioned in the 'data' side of a command), changing
203from mandatory to optional is safe (older clients will supply the option, and
204newer clients can benefit from the default); changing from optional to
205mandatory is backwards incompatible (older clients may be omitting the option,
206and must continue to work).
207
208On output structures (only mentioned in the 'returns' side of a command),
209changing from mandatory to optional is in general unsafe (older clients may be
210expecting the field, and could crash if it is missing), although it can be done
211if the only way that the optional argument will be omitted is when it is
212triggered by the presence of a new input flag to the command that older clients
213don't know to send. Changing from optional to mandatory is safe.
214
215A structure that is used in both input and output of various commands
216must consider the backwards compatibility constraints of both directions
217of use.
622f557f 218
3b2a8b85 219A struct definition can specify another struct as its base.
622f557f 220In this case, the fields of the base type are included as top-level fields
363b4262
EB
221of the new struct's dictionary in the Client JSON Protocol wire
222format. An example definition is:
622f557f 223
3b2a8b85
EB
224 { 'struct': 'BlockdevOptionsGenericFormat', 'data': { 'file': 'str' } }
225 { 'struct': 'BlockdevOptionsGenericCOWFormat',
622f557f
KW
226 'base': 'BlockdevOptionsGenericFormat',
227 'data': { '*backing': 'str' } }
228
229An example BlockdevOptionsGenericCOWFormat object on the wire could use
230both fields like this:
231
232 { "file": "/some/place/my-image",
233 "backing": "/some/place/my-backing-file" }
234
e790e666 235
51631493
KW
236=== Enumeration types ===
237
e790e666 238Usage: { 'enum': STRING, 'data': ARRAY-OF-STRING }
351d36e4 239 { 'enum': STRING, '*prefix': STRING, 'data': ARRAY-OF-STRING }
e790e666
EB
240
241An enumeration type is a dictionary containing a single 'data' key
242whose value is a list of strings. An example enumeration is:
b84da831
MR
243
244 { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
245
e790e666
EB
246Nothing prevents an empty enumeration, although it is probably not
247useful. The list of strings should be lower case; if an enum name
248represents multiple words, use '-' between words. The string 'max' is
249not allowed as an enum value, and values should not be repeated.
250
351d36e4
DB
251The enum constants will be named by using a heuristic to turn the
252type name into a set of underscore separated words. For the example
253above, 'MyEnum' will turn into 'MY_ENUM' giving a constant name
254of 'MY_ENUM_VALUE1' for the first value. If the default heuristic
255does not result in a desirable name, the optional 'prefix' field
256can be used when defining the enum.
257
363b4262
EB
258The enumeration values are passed as strings over the Client JSON
259Protocol, but are encoded as C enum integral values in generated code.
260While the C code starts numbering at 0, it is better to use explicit
e790e666
EB
261comparisons to enum values than implicit comparisons to 0; the C code
262will also include a generated enum member ending in _MAX for tracking
263the size of the enum, useful when using common functions for
264converting between strings and enum values. Since the wire format
265always passes by name, it is acceptable to reorder or add new
363b4262
EB
266enumeration members in any location without breaking clients of Client
267JSON Protocol; however, removing enum values would break
268compatibility. For any struct that has a field that will only contain
269a finite set of string values, using an enum type for that field is
270better than open-coding the field to be type 'str'.
e790e666
EB
271
272
51631493
KW
273=== Union types ===
274
e790e666 275Usage: { 'union': STRING, 'data': DICT }
3b2a8b85 276or: { 'union': STRING, 'data': DICT, 'base': STRUCT-NAME,
e790e666 277 'discriminator': ENUM-MEMBER-OF-BASE }
51631493 278
e790e666 279Union types are used to let the user choose between several different
7b1b98c4
EB
280variants for an object. There are two flavors: simple (no
281discriminator or base), flat (both discriminator and base). A union
282type is defined using a data dictionary as explained in the following
283paragraphs.
51631493 284
e790e666
EB
285A simple union type defines a mapping from automatic discriminator
286values to data types like in this example:
51631493 287
3b2a8b85
EB
288 { 'struct': 'FileOptions', 'data': { 'filename': 'str' } }
289 { 'struct': 'Qcow2Options',
51631493
KW
290 'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
291
292 { 'union': 'BlockdevOptions',
293 'data': { 'file': 'FileOptions',
294 'qcow2': 'Qcow2Options' } }
295
363b4262
EB
296In the Client JSON Protocol, a simple union is represented by a
297dictionary that contains the 'type' field as a discriminator, and a
298'data' field that is of the specified data type corresponding to the
299discriminator value, as in these examples:
51631493 300
e790e666 301 { "type": "file", "data" : { "filename": "/some/place/my-image" } }
51631493
KW
302 { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
303 "lazy-refcounts": true } }
304
e790e666
EB
305The generated C code uses a struct containing a union. Additionally,
306an implicit C enum 'NameKind' is created, corresponding to the union
307'Name', for accessing the various branches of the union. No branch of
308the union can be named 'max', as this would collide with the implicit
309enum. The value for each branch can be of any type.
51631493 310
3b2a8b85 311A flat union definition specifies a struct as its base, and
e790e666
EB
312avoids nesting on the wire. All branches of the union must be
313complex types, and the top-level fields of the union dictionary on
314the wire will be combination of fields from both the base type and the
315appropriate branch type (when merging two dictionaries, there must be
316no keys in common). The 'discriminator' field must be the name of an
3b2a8b85 317enum-typed member of the base struct.
51631493 318
e790e666
EB
319The following example enhances the above simple union example by
320adding a common field 'readonly', renaming the discriminator to
321something more applicable, and reducing the number of {} required on
322the wire:
50f2bdc7 323
94a3f0af 324 { 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] }
3b2a8b85 325 { 'struct': 'BlockdevCommonOptions',
bceae769 326 'data': { 'driver': 'BlockdevDriver', 'readonly': 'bool' } }
50f2bdc7
KW
327 { 'union': 'BlockdevOptions',
328 'base': 'BlockdevCommonOptions',
329 'discriminator': 'driver',
e790e666 330 'data': { 'file': 'FileOptions',
50f2bdc7
KW
331 'qcow2': 'Qcow2Options' } }
332
e790e666
EB
333Resulting in these JSON objects:
334
335 { "driver": "file", "readonly": true,
336 "filename": "/some/place/my-image" }
337 { "driver": "qcow2", "readonly": false,
338 "backing-file": "/some/place/my-image", "lazy-refcounts": true }
339
340Notice that in a flat union, the discriminator name is controlled by
341the user, but because it must map to a base member with enum type, the
342code generator can ensure that branches exist for all values of the
343enum (although the order of the keys need not match the declaration of
344the enum). In the resulting generated C data types, a flat union is
345represented as a struct with the base member fields included directly,
346and then a union of structures for each branch of the struct.
347
348A simple union can always be re-written as a flat union where the base
349class has a single member named 'type', and where each branch of the
3b2a8b85 350union has a struct with a single member named 'data'. That is,
50f2bdc7 351
e790e666 352 { 'union': 'Simple', 'data': { 'one': 'str', 'two': 'int' } }
50f2bdc7 353
e790e666 354is identical on the wire to:
50f2bdc7 355
e790e666 356 { 'enum': 'Enum', 'data': ['one', 'two'] }
3b2a8b85
EB
357 { 'struct': 'Base', 'data': { 'type': 'Enum' } }
358 { 'struct': 'Branch1', 'data': { 'data': 'str' } }
359 { 'struct': 'Branch2', 'data': { 'data': 'int' } }
94a3f0af 360 { 'union': 'Flat', 'base': 'Base', 'discriminator': 'type',
e790e666 361 'data': { 'one': 'Branch1', 'two': 'Branch2' } }
69dd62df 362
e790e666 363
7b1b98c4 364=== Alternate types ===
69dd62df 365
7b1b98c4
EB
366Usage: { 'alternate': STRING, 'data': DICT }
367
368An alternate type is one that allows a choice between two or more JSON
369data types (string, integer, number, or object, but currently not
370array) on the wire. The definition is similar to a simple union type,
371where each branch of the union names a QAPI type. For example:
372
373 { 'alternate': 'BlockRef',
69dd62df
KW
374 'data': { 'definition': 'BlockdevOptions',
375 'reference': 'str' } }
376
7b1b98c4
EB
377Just like for a simple union, an implicit C enum 'NameKind' is created
378to enumerate the branches for the alternate 'Name'.
379
380Unlike a union, the discriminator string is never passed on the wire
363b4262
EB
381for the Client JSON Protocol. Instead, the value's JSON type serves
382as an implicit discriminator, which in turn means that an alternate
383can only express a choice between types represented differently in
384JSON. If a branch is typed as the 'bool' built-in, the alternate
385accepts true and false; if it is typed as any of the various numeric
386built-ins, it accepts a JSON number; if it is typed as a 'str'
387built-in or named enum type, it accepts a JSON string; and if it is
388typed as a complex type (struct or union), it accepts a JSON object.
389Two different complex types, for instance, aren't permitted, because
390both are represented as a JSON object.
7b1b98c4
EB
391
392The example alternate declaration above allows using both of the
393following example objects:
69dd62df
KW
394
395 { "file": "my_existing_block_device_id" }
396 { "file": { "driver": "file",
397 "readonly": false,
63922c64 398 "filename": "/tmp/mydisk.qcow2" } }
69dd62df
KW
399
400
51631493 401=== Commands ===
b84da831 402
e790e666 403Usage: { 'command': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT,
9b090d42 404 '*returns': TYPE-NAME,
e790e666
EB
405 '*gen': false, '*success-response': false }
406
407Commands are defined by using a dictionary containing several members,
408where three members are most common. The 'command' member is a
363b4262
EB
409mandatory string, and determines the "execute" value passed in a
410Client JSON Protocol command exchange.
e790e666
EB
411
412The 'data' argument maps to the "arguments" dictionary passed in as
363b4262
EB
413part of a Client JSON Protocol command. The 'data' member is optional
414and defaults to {} (an empty dictionary). If present, it must be the
315932b5
MA
415string name of a complex type, or a dictionary that declares an
416anonymous type with the same semantics as a 'struct' expression, with
417one exception noted below when 'gen' is used.
e790e666
EB
418
419The 'returns' member describes what will appear in the "return" field
363b4262
EB
420of a Client JSON Protocol reply on successful completion of a command.
421The member is optional from the command declaration; if absent, the
422"return" field will be an empty dictionary. If 'returns' is present,
423it must be the string name of a complex or built-in type, a
424one-element array containing the name of a complex or built-in type,
9b090d42
MA
425with one exception noted below when 'gen' is used. Although it is
426permitted to have the 'returns' member name a built-in type or an
427array of built-in types, any command that does this cannot be extended
428to return additional information in the future; thus, new commands
429should strongly consider returning a dictionary-based type or an array
430of dictionaries, even if the dictionary only contains one field at the
431present.
363b4262
EB
432
433All commands in Client JSON Protocol use a dictionary to report
434failure, with no way to specify that in QAPI. Where the error return
435is different than the usual GenericError class in order to help the
436client react differently to certain error conditions, it is worth
437documenting this in the comments before the command declaration.
e790e666
EB
438
439Some example commands:
440
441 { 'command': 'my-first-command',
442 'data': { 'arg1': 'str', '*arg2': 'str' } }
3b2a8b85 443 { 'struct': 'MyType', 'data': { '*value': 'str' } }
e790e666
EB
444 { 'command': 'my-second-command',
445 'returns': [ 'MyType' ] }
446
363b4262 447which would validate this Client JSON Protocol transaction:
e790e666
EB
448
449 => { "execute": "my-first-command",
450 "arguments": { "arg1": "hello" } }
451 <= { "return": { } }
452 => { "execute": "my-second-command" }
453 <= { "return": [ { "value": "one" }, { } ] }
454
455In rare cases, QAPI cannot express a type-safe representation of a
363b4262
EB
456corresponding Client JSON Protocol command. In these cases, if the
457command expression includes the key 'gen' with boolean value false,
458then the 'data' or 'returns' member that intends to bypass generated
459type-safety and do its own manual validation should use an inline
460dictionary definition, with a value of '**' rather than a valid type
461name for the keys that the generated code will not validate. Please
462try to avoid adding new commands that rely on this, and instead use
463type-safe unions. For an example of bypass usage:
e790e666
EB
464
465 { 'command': 'netdev_add',
466 'data': {'type': 'str', 'id': 'str', '*props': '**'},
467 'gen': false }
468
469Normally, the QAPI schema is used to describe synchronous exchanges,
470where a response is expected. But in some cases, the action of a
471command is expected to change state in a way that a successful
472response is not possible (although the command will still return a
473normal dictionary error on failure). When a successful reply is not
474possible, the command expression should include the optional key
475'success-response' with boolean value false. So far, only QGA makes
476use of this field.
b84da831 477
b84da831 478
21cd70df
WX
479=== Events ===
480
e790e666
EB
481Usage: { 'event': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT }
482
483Events are defined with the keyword 'event'. It is not allowed to
484name an event 'MAX', since the generator also produces a C enumeration
485of all event names with a generated _MAX value at the end. When
486'data' is also specified, additional info will be included in the
3b2a8b85 487event, with similar semantics to a 'struct' expression. Finally there
e790e666
EB
488will be C API generated in qapi-event.h; when called by QEMU code, a
489message with timestamp will be emitted on the wire.
21cd70df
WX
490
491An example event is:
492
493{ 'event': 'EVENT_C',
494 'data': { '*a': 'int', 'b': 'str' } }
495
496Resulting in this JSON object:
497
498{ "event": "EVENT_C",
499 "data": { "b": "test string" },
500 "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
b84da831 501
59a2c4ce 502
b84da831
MR
503== Code generation ==
504
505Schemas are fed into 3 scripts to generate all the code/files that, paired
506with the core QAPI libraries, comprise everything required to take JSON
363b4262 507commands read in by a Client JSON Protocol server, unmarshal the arguments into
b84da831 508the underlying C types, call into the corresponding C function, and map the
363b4262 509response back to a Client JSON Protocol response to be returned to the user.
b84da831
MR
510
511As an example, we'll use the following schema, which describes a single
512complex user-defined type (which will produce a C struct, along with a list
513node structure that can be used to chain together a list of such types in
514case we want to accept/return a list of this type with a command), and a
515command which takes that type as a parameter and returns the same type:
516
87a560c4 517 $ cat example-schema.json
3b2a8b85 518 { 'struct': 'UserDefOne',
b84da831
MR
519 'data': { 'integer': 'int', 'string': 'str' } }
520
521 { 'command': 'my-command',
522 'data': {'arg1': 'UserDefOne'},
523 'returns': 'UserDefOne' }
b84da831 524
59a2c4ce
EB
525 { 'event': 'MY_EVENT' }
526
b84da831
MR
527=== scripts/qapi-types.py ===
528
529Used to generate the C types defined by a schema. The following files are
530created:
531
532$(prefix)qapi-types.h - C types corresponding to types defined in
533 the schema you pass in
534$(prefix)qapi-types.c - Cleanup functions for the above C types
535
536The $(prefix) is an optional parameter used as a namespace to keep the
537generated code from one schema/code-generation separated from others so code
538can be generated/used from multiple schemas without clobbering previously
539created code.
540
541Example:
542
87a560c4 543 $ python scripts/qapi-types.py --output-dir="qapi-generated" \
16d80f61 544 --prefix="example-" example-schema.json
87a560c4 545 $ cat qapi-generated/example-qapi-types.c
6e2bb3ec
MA
546[Uninteresting stuff omitted...]
547
2b162ccb 548 void qapi_free_UserDefOne(UserDefOne *obj)
6e2bb3ec
MA
549 {
550 QapiDeallocVisitor *md;
551 Visitor *v;
552
553 if (!obj) {
554 return;
555 }
556
557 md = qapi_dealloc_visitor_new();
558 v = qapi_dealloc_get_visitor(md);
2b162ccb 559 visit_type_UserDefOne(v, &obj, NULL, NULL);
6e2bb3ec
MA
560 qapi_dealloc_visitor_cleanup(md);
561 }
b84da831 562
2b162ccb 563 void qapi_free_UserDefOneList(UserDefOneList *obj)
b84da831
MR
564 {
565 QapiDeallocVisitor *md;
566 Visitor *v;
567
568 if (!obj) {
569 return;
570 }
571
572 md = qapi_dealloc_visitor_new();
573 v = qapi_dealloc_get_visitor(md);
2b162ccb 574 visit_type_UserDefOneList(v, &obj, NULL, NULL);
b84da831
MR
575 qapi_dealloc_visitor_cleanup(md);
576 }
87a560c4 577 $ cat qapi-generated/example-qapi-types.h
6e2bb3ec
MA
578[Uninteresting stuff omitted...]
579
580 #ifndef EXAMPLE_QAPI_TYPES_H
581 #define EXAMPLE_QAPI_TYPES_H
b84da831 582
e790e666 583[Built-in types omitted...]
b84da831
MR
584
585 typedef struct UserDefOne UserDefOne;
586
2b162ccb
MA
587 typedef struct UserDefOneList UserDefOneList;
588
589 struct UserDefOne {
590 int64_t integer;
591 char *string;
592 };
593
594 void qapi_free_UserDefOne(UserDefOne *obj);
595
596 struct UserDefOneList {
6e2bb3ec
MA
597 union {
598 UserDefOne *value;
599 uint64_t padding;
600 };
e98859a9 601 UserDefOneList *next;
b84da831
MR
602 };
603
59a2c4ce 604 void qapi_free_UserDefOneList(UserDefOneList *obj);
b84da831
MR
605
606 #endif
607
b84da831
MR
608=== scripts/qapi-visit.py ===
609
610Used to generate the visitor functions used to walk through and convert
611a QObject (as provided by QMP) to a native C data structure and
612vice-versa, as well as the visitor function used to dealloc a complex
613schema-defined C type.
614
615The following files are generated:
616
617$(prefix)qapi-visit.c: visitor function for a particular C type, used
618 to automagically convert QObjects into the
619 corresponding C type and vice-versa, as well
620 as for deallocating memory for an existing C
621 type
622
623$(prefix)qapi-visit.h: declarations for previously mentioned visitor
624 functions
625
626Example:
627
87a560c4 628 $ python scripts/qapi-visit.py --output-dir="qapi-generated"
16d80f61 629 --prefix="example-" example-schema.json
87a560c4 630 $ cat qapi-generated/example-qapi-visit.c
6e2bb3ec 631[Uninteresting stuff omitted...]
b84da831 632
59a2c4ce 633 static void visit_type_UserDefOne_fields(Visitor *m, UserDefOne **obj, Error **errp)
6e2bb3ec
MA
634 {
635 Error *err = NULL;
3a864e7c 636
6e2bb3ec 637 visit_type_int(m, &(*obj)->integer, "integer", &err);
297a3646
MA
638 if (err) {
639 goto out;
640 }
6e2bb3ec 641 visit_type_str(m, &(*obj)->string, "string", &err);
297a3646
MA
642 if (err) {
643 goto out;
644 }
6e2bb3ec 645
297a3646 646 out:
6e2bb3ec
MA
647 error_propagate(errp, err);
648 }
b84da831 649
59a2c4ce 650 void visit_type_UserDefOne(Visitor *m, UserDefOne **obj, const char *name, Error **errp)
b84da831 651 {
297a3646
MA
652 Error *err = NULL;
653
654 visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err);
655 if (!err) {
656 if (*obj) {
657 visit_type_UserDefOne_fields(m, obj, errp);
6e2bb3ec 658 }
297a3646 659 visit_end_struct(m, &err);
6e2bb3ec 660 }
297a3646 661 error_propagate(errp, err);
b84da831
MR
662 }
663
59a2c4ce 664 void visit_type_UserDefOneList(Visitor *m, UserDefOneList **obj, const char *name, Error **errp)
b84da831 665 {
6e2bb3ec 666 Error *err = NULL;
297a3646 667 GenericList *i, **prev;
6e2bb3ec 668
297a3646
MA
669 visit_start_list(m, name, &err);
670 if (err) {
671 goto out;
672 }
673
674 for (prev = (GenericList **)obj;
675 !err && (i = visit_next_list(m, prev, &err)) != NULL;
676 prev = &i) {
677 UserDefOneList *native_i = (UserDefOneList *)i;
678 visit_type_UserDefOne(m, &native_i->value, NULL, &err);
b84da831 679 }
297a3646
MA
680
681 error_propagate(errp, err);
682 err = NULL;
683 visit_end_list(m, &err);
684 out:
685 error_propagate(errp, err);
b84da831 686 }
87a560c4 687 $ cat qapi-generated/example-qapi-visit.h
6e2bb3ec 688[Uninteresting stuff omitted...]
b84da831 689
6e2bb3ec
MA
690 #ifndef EXAMPLE_QAPI_VISIT_H
691 #define EXAMPLE_QAPI_VISIT_H
b84da831 692
e790e666 693[Visitors for built-in types omitted...]
b84da831 694
59a2c4ce
EB
695 void visit_type_UserDefOne(Visitor *m, UserDefOne **obj, const char *name, Error **errp);
696 void visit_type_UserDefOneList(Visitor *m, UserDefOneList **obj, const char *name, Error **errp);
b84da831
MR
697
698 #endif
b84da831 699
b84da831
MR
700=== scripts/qapi-commands.py ===
701
702Used to generate the marshaling/dispatch functions for the commands defined
703in the schema. The following files are generated:
704
705$(prefix)qmp-marshal.c: command marshal/dispatch functions for each
706 QMP command defined in the schema. Functions
707 generated by qapi-visit.py are used to
2542bfd5 708 convert QObjects received from the wire into
b84da831
MR
709 function parameters, and uses the same
710 visitor functions to convert native C return
711 values to QObjects from transmission back
712 over the wire.
713
714$(prefix)qmp-commands.h: Function prototypes for the QMP commands
715 specified in the schema.
716
717Example:
718
59a2c4ce 719 $ python scripts/qapi-commands.py --output-dir="qapi-generated"
16d80f61 720 --prefix="example-" example-schema.json
87a560c4 721 $ cat qapi-generated/example-qmp-marshal.c
6e2bb3ec 722[Uninteresting stuff omitted...]
b84da831 723
59a2c4ce 724 static void qmp_marshal_output_my_command(UserDefOne *ret_in, QObject **ret_out, Error **errp)
b84da831 725 {
297a3646 726 Error *local_err = NULL;
b84da831 727 QmpOutputVisitor *mo = qmp_output_visitor_new();
f9bee751 728 QapiDeallocVisitor *md;
b84da831
MR
729 Visitor *v;
730
731 v = qmp_output_get_visitor(mo);
297a3646
MA
732 visit_type_UserDefOne(v, &ret_in, "unused", &local_err);
733 if (local_err) {
734 goto out;
6e2bb3ec 735 }
297a3646
MA
736 *ret_out = qmp_output_get_qobject(mo);
737
738 out:
739 error_propagate(errp, local_err);
6e2bb3ec 740 qmp_output_visitor_cleanup(mo);
f9bee751 741 md = qapi_dealloc_visitor_new();
b84da831 742 v = qapi_dealloc_get_visitor(md);
6e2bb3ec 743 visit_type_UserDefOne(v, &ret_in, "unused", NULL);
b84da831 744 qapi_dealloc_visitor_cleanup(md);
b84da831
MR
745 }
746
6e2bb3ec 747 static void qmp_marshal_input_my_command(QDict *args, QObject **ret, Error **errp)
b84da831 748 {
297a3646 749 Error *local_err = NULL;
3f99144c 750 UserDefOne *retval;
f9bee751 751 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
b84da831
MR
752 QapiDeallocVisitor *md;
753 Visitor *v;
59a2c4ce 754 UserDefOne *arg1 = NULL;
b84da831 755
b84da831 756 v = qmp_input_get_visitor(mi);
297a3646
MA
757 visit_type_UserDefOne(v, &arg1, "arg1", &local_err);
758 if (local_err) {
b84da831
MR
759 goto out;
760 }
297a3646
MA
761
762 retval = qmp_my_command(arg1, &local_err);
763 if (local_err) {
764 goto out;
6e2bb3ec 765 }
b84da831 766
297a3646
MA
767 qmp_marshal_output_my_command(retval, ret, &local_err);
768
b84da831 769 out:
297a3646 770 error_propagate(errp, local_err);
f9bee751 771 qmp_input_visitor_cleanup(mi);
b84da831
MR
772 md = qapi_dealloc_visitor_new();
773 v = qapi_dealloc_get_visitor(md);
6e2bb3ec 774 visit_type_UserDefOne(v, &arg1, "arg1", NULL);
b84da831 775 qapi_dealloc_visitor_cleanup(md);
b84da831
MR
776 }
777
778 static void qmp_init_marshal(void)
779 {
6e2bb3ec 780 qmp_register_command("my-command", qmp_marshal_input_my_command, QCO_NO_OPTIONS);
b84da831
MR
781 }
782
783 qapi_init(qmp_init_marshal);
87a560c4 784 $ cat qapi-generated/example-qmp-commands.h
6e2bb3ec 785[Uninteresting stuff omitted...]
b84da831 786
6e2bb3ec
MA
787 #ifndef EXAMPLE_QMP_COMMANDS_H
788 #define EXAMPLE_QMP_COMMANDS_H
b84da831
MR
789
790 #include "example-qapi-types.h"
6e2bb3ec
MA
791 #include "qapi/qmp/qdict.h"
792 #include "qapi/error.h"
b84da831 793
59a2c4ce
EB
794 UserDefOne *qmp_my_command(UserDefOne *arg1, Error **errp);
795
796 #endif
797
798=== scripts/qapi-event.py ===
799
800Used to generate the event-related C code defined by a schema. The
801following files are created:
802
803$(prefix)qapi-event.h - Function prototypes for each event type, plus an
804 enumeration of all event names
805$(prefix)qapi-event.c - Implementation of functions to send an event
806
807Example:
808
809 $ python scripts/qapi-event.py --output-dir="qapi-generated"
16d80f61 810 --prefix="example-" example-schema.json
59a2c4ce
EB
811 $ cat qapi-generated/example-qapi-event.c
812[Uninteresting stuff omitted...]
813
814 void qapi_event_send_my_event(Error **errp)
815 {
816 QDict *qmp;
817 Error *local_err = NULL;
818 QMPEventFuncEmit emit;
819 emit = qmp_event_get_func_emit();
820 if (!emit) {
821 return;
822 }
823
824 qmp = qmp_event_build_dict("MY_EVENT");
825
826 emit(EXAMPLE_QAPI_EVENT_MY_EVENT, qmp, &local_err);
827
828 error_propagate(errp, local_err);
829 QDECREF(qmp);
830 }
831
efd2eaa6
MA
832 const char *const example_QAPIEvent_lookup[] = {
833 [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT",
834 [EXAMPLE_QAPI_EVENT_MAX] = NULL,
59a2c4ce
EB
835 };
836 $ cat qapi-generated/example-qapi-event.h
837[Uninteresting stuff omitted...]
838
839 #ifndef EXAMPLE_QAPI_EVENT_H
840 #define EXAMPLE_QAPI_EVENT_H
841
842 #include "qapi/error.h"
843 #include "qapi/qmp/qdict.h"
844 #include "example-qapi-types.h"
845
846
847 void qapi_event_send_my_event(Error **errp);
848
3a864e7c 849 typedef enum example_QAPIEvent {
59a2c4ce
EB
850 EXAMPLE_QAPI_EVENT_MY_EVENT = 0,
851 EXAMPLE_QAPI_EVENT_MAX = 1,
016a335b 852 } example_QAPIEvent;
b84da831 853
efd2eaa6
MA
854 extern const char *const example_QAPIEvent_lookup[];
855
b84da831 856 #endif