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