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