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