]> git.proxmox.com Git - mirror_qemu.git/blame - docs/qapi-code-gen.txt
virtio: fix virtio-blk child refcount in transports
[mirror_qemu.git] / docs / qapi-code-gen.txt
CommitLineData
b84da831
MR
1= How to use the QAPI code generator =
2
3* Note: as of this writing, QMP does not use QAPI. Eventually QMP
4commands will be converted to use QAPI internally. The following
5information describes QMP/QAPI as it will exist after the
6conversion.
7
8QAPI is a native C API within QEMU which provides management-level
9functionality to internal/external users. For external
10users/processes, this interface is made available by a JSON-based
11QEMU Monitor protocol that is provided by the QMP server.
12
13To map QMP-defined interfaces to the native C QAPI implementations,
14a JSON-based schema is used to define types and function
15signatures, and a set of scripts is used to generate types/signatures,
16and marshaling/dispatch code. The QEMU Guest Agent also uses these
4238e264 17scripts, paired with a separate schema, to generate
b84da831
MR
18marshaling/dispatch code for the guest agent server running in the
19guest.
20
21This document will describe how the schemas, scripts, and resulting
22code is used.
23
24
25== QMP/Guest agent schema ==
26
27This file defines the types, commands, and events used by QMP. It should
28fully describe the interface used by QMP.
29
30This file is designed to be loosely based on JSON although it's technically
31executable Python. While dictionaries are used, they are parsed as
32OrderedDicts so that ordering is preserved.
33
34There are two basic syntaxes used, type definitions and command definitions.
35
36The first syntax defines a type and is represented by a dictionary. There are
51631493
KW
37three kinds of user-defined types that are supported: complex types,
38enumeration types and union types.
b84da831 39
51631493
KW
40Generally speaking, types definitions should always use CamelCase for the type
41names. Command names should be all lower case with words separated by a hyphen.
42
a719a27c
LV
43
44=== Includes ===
45
46The QAPI schema definitions can be modularized using the 'include' directive:
47
48 { 'include': 'path/to/file.json'}
49
50The directive is evaluated recursively, and include paths are relative to the
24fd8489 51file using the directive. Multiple includes of the same file are safe.
a719a27c
LV
52
53
51631493
KW
54=== Complex types ===
55
56A complex type is a dictionary containing a single key whose value is a
b84da831
MR
57dictionary. This corresponds to a struct in C or an Object in JSON. An
58example of a complex type is:
59
60 { 'type': 'MyType',
acf8394e 61 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
b84da831 62
cc162655
EB
63The use of '*' as a prefix to the name means the member is optional.
64
65The default initialization value of an optional argument should not be changed
66between versions of QEMU unless the new default maintains backward
67compatibility to the user-visible behavior of the old default.
68
69With proper documentation, this policy still allows some flexibility; for
70example, documenting that a default of 0 picks an optimal buffer size allows
71one release to declare the optimal size at 512 while another release declares
72the optimal size at 4096 - the user-visible behavior is not the bytes used by
73the buffer, but the fact that the buffer was optimal size.
74
75On input structures (only mentioned in the 'data' side of a command), changing
76from mandatory to optional is safe (older clients will supply the option, and
77newer clients can benefit from the default); changing from optional to
78mandatory is backwards incompatible (older clients may be omitting the option,
79and must continue to work).
80
81On output structures (only mentioned in the 'returns' side of a command),
82changing from mandatory to optional is in general unsafe (older clients may be
83expecting the field, and could crash if it is missing), although it can be done
84if the only way that the optional argument will be omitted is when it is
85triggered by the presence of a new input flag to the command that older clients
86don't know to send. Changing from optional to mandatory is safe.
87
88A structure that is used in both input and output of various commands
89must consider the backwards compatibility constraints of both directions
90of use.
622f557f
KW
91
92A complex type definition can specify another complex type as its base.
93In this case, the fields of the base type are included as top-level fields
94of the new complex type's dictionary in the QMP wire format. An example
95definition is:
96
97 { 'type': 'BlockdevOptionsGenericFormat', 'data': { 'file': 'str' } }
98 { 'type': 'BlockdevOptionsGenericCOWFormat',
99 'base': 'BlockdevOptionsGenericFormat',
100 'data': { '*backing': 'str' } }
101
102An example BlockdevOptionsGenericCOWFormat object on the wire could use
103both fields like this:
104
105 { "file": "/some/place/my-image",
106 "backing": "/some/place/my-backing-file" }
107
51631493
KW
108=== Enumeration types ===
109
110An enumeration type is a dictionary containing a single key whose value is a
b84da831
MR
111list of strings. An example enumeration is:
112
113 { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
114
51631493
KW
115=== Union types ===
116
117Union types are used to let the user choose between several different data
118types. A union type is defined using a dictionary as explained in the
119following paragraphs.
120
121
122A simple union type defines a mapping from discriminator values to data types
123like in this example:
124
125 { 'type': 'FileOptions', 'data': { 'filename': 'str' } }
126 { 'type': 'Qcow2Options',
127 'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
128
129 { 'union': 'BlockdevOptions',
130 'data': { 'file': 'FileOptions',
131 'qcow2': 'Qcow2Options' } }
132
133In the QMP wire format, a simple union is represented by a dictionary that
134contains the 'type' field as a discriminator, and a 'data' field that is of the
135specified data type corresponding to the discriminator value:
136
137 { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
138 "lazy-refcounts": true } }
139
140
141A union definition can specify a complex type as its base. In this case, the
142fields of the complex type are included as top-level fields of the union
143dictionary in the QMP wire format. An example definition is:
144
145 { 'type': 'BlockdevCommonOptions', 'data': { 'readonly': 'bool' } }
146 { 'union': 'BlockdevOptions',
147 'base': 'BlockdevCommonOptions',
148 'data': { 'raw': 'RawOptions',
149 'qcow2': 'Qcow2Options' } }
150
151And it looks like this on the wire:
152
153 { "type": "qcow2",
154 "readonly": false,
155 "data" : { "backing-file": "/some/place/my-image",
156 "lazy-refcounts": true } }
157
50f2bdc7
KW
158
159Flat union types avoid the nesting on the wire. They are used whenever a
160specific field of the base type is declared as the discriminator ('type' is
5223070c 161then no longer generated). The discriminator must be of enumeration type.
50f2bdc7
KW
162The above example can then be modified as follows:
163
bceae769 164 { 'enum': 'BlockdevDriver', 'data': [ 'raw', 'qcow2' ] }
50f2bdc7 165 { 'type': 'BlockdevCommonOptions',
bceae769 166 'data': { 'driver': 'BlockdevDriver', 'readonly': 'bool' } }
50f2bdc7
KW
167 { 'union': 'BlockdevOptions',
168 'base': 'BlockdevCommonOptions',
169 'discriminator': 'driver',
170 'data': { 'raw': 'RawOptions',
171 'qcow2': 'Qcow2Options' } }
172
173Resulting in this JSON object:
174
175 { "driver": "qcow2",
176 "readonly": false,
177 "backing-file": "/some/place/my-image",
178 "lazy-refcounts": true }
179
180
69dd62df
KW
181A special type of unions are anonymous unions. They don't form a dictionary in
182the wire format but allow the direct use of different types in their place. As
183they aren't structured, they don't have any explicit discriminator but use
184the (QObject) data type of their value as an implicit discriminator. This means
185that they are restricted to using only one discriminator value per QObject
186type. For example, you cannot have two different complex types in an anonymous
187union, or two different integer types.
188
189Anonymous unions are declared using an empty dictionary as their discriminator.
190The discriminator values never appear on the wire, they are only used in the
191generated C code. Anonymous unions cannot have a base type.
192
193 { 'union': 'BlockRef',
194 'discriminator': {},
195 'data': { 'definition': 'BlockdevOptions',
196 'reference': 'str' } }
197
198This example allows using both of the following example objects:
199
200 { "file": "my_existing_block_device_id" }
201 { "file": { "driver": "file",
202 "readonly": false,
63922c64 203 "filename": "/tmp/mydisk.qcow2" } }
69dd62df
KW
204
205
51631493 206=== Commands ===
b84da831
MR
207
208Commands are defined by using a list containing three members. The first
209member is the command name, the second member is a dictionary containing
210arguments, and the third member is the return type.
211
212An example command is:
213
214 { 'command': 'my-command',
215 'data': { 'arg1': 'str', '*arg2': 'str' },
acf8394e 216 'returns': 'str' }
b84da831 217
21cd70df
WX
218=== Events ===
219
220Events are defined with the keyword 'event'. When 'data' is also specified,
d6f9c82c
WX
221additional info will be included in the event. Finally there will be C API
222generated in qapi-event.h; when called by QEMU code, a message with timestamp
223will be emitted on the wire. If timestamp is -1, it means failure to retrieve
224host time.
21cd70df
WX
225
226An example event is:
227
228{ 'event': 'EVENT_C',
229 'data': { '*a': 'int', 'b': 'str' } }
230
231Resulting in this JSON object:
232
233{ "event": "EVENT_C",
234 "data": { "b": "test string" },
235 "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
b84da831
MR
236
237== Code generation ==
238
239Schemas are fed into 3 scripts to generate all the code/files that, paired
240with the core QAPI libraries, comprise everything required to take JSON
241commands read in by a QMP/guest agent server, unmarshal the arguments into
242the underlying C types, call into the corresponding C function, and map the
243response back to a QMP/guest agent response to be returned to the user.
244
245As an example, we'll use the following schema, which describes a single
246complex user-defined type (which will produce a C struct, along with a list
247node structure that can be used to chain together a list of such types in
248case we want to accept/return a list of this type with a command), and a
249command which takes that type as a parameter and returns the same type:
250
87a560c4 251 $ cat example-schema.json
b84da831
MR
252 { 'type': 'UserDefOne',
253 'data': { 'integer': 'int', 'string': 'str' } }
254
255 { 'command': 'my-command',
256 'data': {'arg1': 'UserDefOne'},
257 'returns': 'UserDefOne' }
b84da831
MR
258
259=== scripts/qapi-types.py ===
260
261Used to generate the C types defined by a schema. The following files are
262created:
263
264$(prefix)qapi-types.h - C types corresponding to types defined in
265 the schema you pass in
266$(prefix)qapi-types.c - Cleanup functions for the above C types
267
268The $(prefix) is an optional parameter used as a namespace to keep the
269generated code from one schema/code-generation separated from others so code
270can be generated/used from multiple schemas without clobbering previously
271created code.
272
273Example:
274
87a560c4
MA
275 $ python scripts/qapi-types.py --output-dir="qapi-generated" \
276 --prefix="example-" --input-file=example-schema.json
277 $ cat qapi-generated/example-qapi-types.c
6e2bb3ec
MA
278[Uninteresting stuff omitted...]
279
280 void qapi_free_UserDefOneList(UserDefOneList * obj)
281 {
282 QapiDeallocVisitor *md;
283 Visitor *v;
284
285 if (!obj) {
286 return;
287 }
288
289 md = qapi_dealloc_visitor_new();
290 v = qapi_dealloc_get_visitor(md);
291 visit_type_UserDefOneList(v, &obj, NULL, NULL);
292 qapi_dealloc_visitor_cleanup(md);
293 }
b84da831 294
b84da831
MR
295 void qapi_free_UserDefOne(UserDefOne * obj)
296 {
297 QapiDeallocVisitor *md;
298 Visitor *v;
299
300 if (!obj) {
301 return;
302 }
303
304 md = qapi_dealloc_visitor_new();
305 v = qapi_dealloc_get_visitor(md);
306 visit_type_UserDefOne(v, &obj, NULL, NULL);
307 qapi_dealloc_visitor_cleanup(md);
308 }
309
87a560c4 310 $ cat qapi-generated/example-qapi-types.h
6e2bb3ec
MA
311[Uninteresting stuff omitted...]
312
313 #ifndef EXAMPLE_QAPI_TYPES_H
314 #define EXAMPLE_QAPI_TYPES_H
b84da831 315
6e2bb3ec 316[Builtin types omitted...]
b84da831
MR
317
318 typedef struct UserDefOne UserDefOne;
319
320 typedef struct UserDefOneList
321 {
6e2bb3ec
MA
322 union {
323 UserDefOne *value;
324 uint64_t padding;
325 };
b84da831
MR
326 struct UserDefOneList *next;
327 } UserDefOneList;
328
6e2bb3ec
MA
329[Functions on builtin types omitted...]
330
b84da831
MR
331 struct UserDefOne
332 {
333 int64_t integer;
334 char * string;
335 };
336
6e2bb3ec 337 void qapi_free_UserDefOneList(UserDefOneList * obj);
b84da831
MR
338 void qapi_free_UserDefOne(UserDefOne * obj);
339
340 #endif
341
b84da831
MR
342=== scripts/qapi-visit.py ===
343
344Used to generate the visitor functions used to walk through and convert
345a QObject (as provided by QMP) to a native C data structure and
346vice-versa, as well as the visitor function used to dealloc a complex
347schema-defined C type.
348
349The following files are generated:
350
351$(prefix)qapi-visit.c: visitor function for a particular C type, used
352 to automagically convert QObjects into the
353 corresponding C type and vice-versa, as well
354 as for deallocating memory for an existing C
355 type
356
357$(prefix)qapi-visit.h: declarations for previously mentioned visitor
358 functions
359
360Example:
361
87a560c4
MA
362 $ python scripts/qapi-visit.py --output-dir="qapi-generated"
363 --prefix="example-" --input-file=example-schema.json
364 $ cat qapi-generated/example-qapi-visit.c
6e2bb3ec 365[Uninteresting stuff omitted...]
b84da831 366
6e2bb3ec
MA
367 static void visit_type_UserDefOne_fields(Visitor *m, UserDefOne ** obj, Error **errp)
368 {
369 Error *err = NULL;
370 visit_type_int(m, &(*obj)->integer, "integer", &err);
297a3646
MA
371 if (err) {
372 goto out;
373 }
6e2bb3ec 374 visit_type_str(m, &(*obj)->string, "string", &err);
297a3646
MA
375 if (err) {
376 goto out;
377 }
6e2bb3ec 378
297a3646 379 out:
6e2bb3ec
MA
380 error_propagate(errp, err);
381 }
b84da831
MR
382
383 void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp)
384 {
297a3646
MA
385 Error *err = NULL;
386
387 visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err);
388 if (!err) {
389 if (*obj) {
390 visit_type_UserDefOne_fields(m, obj, errp);
6e2bb3ec 391 }
297a3646 392 visit_end_struct(m, &err);
6e2bb3ec 393 }
297a3646 394 error_propagate(errp, err);
b84da831
MR
395 }
396
397 void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp)
398 {
6e2bb3ec 399 Error *err = NULL;
297a3646 400 GenericList *i, **prev;
6e2bb3ec 401
297a3646
MA
402 visit_start_list(m, name, &err);
403 if (err) {
404 goto out;
405 }
406
407 for (prev = (GenericList **)obj;
408 !err && (i = visit_next_list(m, prev, &err)) != NULL;
409 prev = &i) {
410 UserDefOneList *native_i = (UserDefOneList *)i;
411 visit_type_UserDefOne(m, &native_i->value, NULL, &err);
b84da831 412 }
297a3646
MA
413
414 error_propagate(errp, err);
415 err = NULL;
416 visit_end_list(m, &err);
417 out:
418 error_propagate(errp, err);
b84da831 419 }
87a560c4
MA
420 $ python scripts/qapi-commands.py --output-dir="qapi-generated" \
421 --prefix="example-" --input-file=example-schema.json
422 $ cat qapi-generated/example-qapi-visit.h
6e2bb3ec 423[Uninteresting stuff omitted...]
b84da831 424
6e2bb3ec
MA
425 #ifndef EXAMPLE_QAPI_VISIT_H
426 #define EXAMPLE_QAPI_VISIT_H
b84da831 427
6e2bb3ec 428[Visitors for builtin types omitted...]
b84da831
MR
429
430 void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp);
431 void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp);
432
433 #endif
b84da831 434
b84da831
MR
435=== scripts/qapi-commands.py ===
436
437Used to generate the marshaling/dispatch functions for the commands defined
438in the schema. The following files are generated:
439
440$(prefix)qmp-marshal.c: command marshal/dispatch functions for each
441 QMP command defined in the schema. Functions
442 generated by qapi-visit.py are used to
2542bfd5 443 convert QObjects received from the wire into
b84da831
MR
444 function parameters, and uses the same
445 visitor functions to convert native C return
446 values to QObjects from transmission back
447 over the wire.
448
449$(prefix)qmp-commands.h: Function prototypes for the QMP commands
450 specified in the schema.
451
452Example:
453
87a560c4 454 $ cat qapi-generated/example-qmp-marshal.c
6e2bb3ec 455[Uninteresting stuff omitted...]
b84da831 456
b84da831
MR
457 static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
458 {
297a3646 459 Error *local_err = NULL;
b84da831 460 QmpOutputVisitor *mo = qmp_output_visitor_new();
f9bee751 461 QapiDeallocVisitor *md;
b84da831
MR
462 Visitor *v;
463
464 v = qmp_output_get_visitor(mo);
297a3646
MA
465 visit_type_UserDefOne(v, &ret_in, "unused", &local_err);
466 if (local_err) {
467 goto out;
6e2bb3ec 468 }
297a3646
MA
469 *ret_out = qmp_output_get_qobject(mo);
470
471 out:
472 error_propagate(errp, local_err);
6e2bb3ec 473 qmp_output_visitor_cleanup(mo);
f9bee751 474 md = qapi_dealloc_visitor_new();
b84da831 475 v = qapi_dealloc_get_visitor(md);
6e2bb3ec 476 visit_type_UserDefOne(v, &ret_in, "unused", NULL);
b84da831 477 qapi_dealloc_visitor_cleanup(md);
b84da831
MR
478 }
479
6e2bb3ec 480 static void qmp_marshal_input_my_command(QDict *args, QObject **ret, Error **errp)
b84da831 481 {
297a3646 482 Error *local_err = NULL;
b84da831 483 UserDefOne * retval = NULL;
f9bee751 484 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
b84da831
MR
485 QapiDeallocVisitor *md;
486 Visitor *v;
487 UserDefOne * arg1 = NULL;
488
b84da831 489 v = qmp_input_get_visitor(mi);
297a3646
MA
490 visit_type_UserDefOne(v, &arg1, "arg1", &local_err);
491 if (local_err) {
b84da831
MR
492 goto out;
493 }
297a3646
MA
494
495 retval = qmp_my_command(arg1, &local_err);
496 if (local_err) {
497 goto out;
6e2bb3ec 498 }
b84da831 499
297a3646
MA
500 qmp_marshal_output_my_command(retval, ret, &local_err);
501
b84da831 502 out:
297a3646 503 error_propagate(errp, local_err);
f9bee751 504 qmp_input_visitor_cleanup(mi);
b84da831
MR
505 md = qapi_dealloc_visitor_new();
506 v = qapi_dealloc_get_visitor(md);
6e2bb3ec 507 visit_type_UserDefOne(v, &arg1, "arg1", NULL);
b84da831
MR
508 qapi_dealloc_visitor_cleanup(md);
509 return;
510 }
511
512 static void qmp_init_marshal(void)
513 {
6e2bb3ec 514 qmp_register_command("my-command", qmp_marshal_input_my_command, QCO_NO_OPTIONS);
b84da831
MR
515 }
516
517 qapi_init(qmp_init_marshal);
87a560c4 518 $ cat qapi-generated/example-qmp-commands.h
6e2bb3ec 519[Uninteresting stuff omitted...]
b84da831 520
6e2bb3ec
MA
521 #ifndef EXAMPLE_QMP_COMMANDS_H
522 #define EXAMPLE_QMP_COMMANDS_H
b84da831
MR
523
524 #include "example-qapi-types.h"
6e2bb3ec
MA
525 #include "qapi/qmp/qdict.h"
526 #include "qapi/error.h"
b84da831
MR
527
528 UserDefOne * qmp_my_command(UserDefOne * arg1, Error **errp);
529
530 #endif