]> git.proxmox.com Git - qemu.git/blame - docs/qapi-code-gen.txt
QMP: Update qmp-spec.txt
[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
43=== Complex types ===
44
45A complex type is a dictionary containing a single key whose value is a
b84da831
MR
46dictionary. This corresponds to a struct in C or an Object in JSON. An
47example of a complex type is:
48
49 { 'type': 'MyType',
acf8394e 50 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
b84da831
MR
51
52The use of '*' as a prefix to the name means the member is optional. Optional
53members should always be added to the end of the dictionary to preserve
54backwards compatibility.
55
51631493
KW
56=== Enumeration types ===
57
58An enumeration type is a dictionary containing a single key whose value is a
b84da831
MR
59list of strings. An example enumeration is:
60
61 { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
62
51631493
KW
63=== Union types ===
64
65Union types are used to let the user choose between several different data
66types. A union type is defined using a dictionary as explained in the
67following paragraphs.
68
69
70A simple union type defines a mapping from discriminator values to data types
71like in this example:
72
73 { 'type': 'FileOptions', 'data': { 'filename': 'str' } }
74 { 'type': 'Qcow2Options',
75 'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
76
77 { 'union': 'BlockdevOptions',
78 'data': { 'file': 'FileOptions',
79 'qcow2': 'Qcow2Options' } }
80
81In the QMP wire format, a simple union is represented by a dictionary that
82contains the 'type' field as a discriminator, and a 'data' field that is of the
83specified data type corresponding to the discriminator value:
84
85 { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
86 "lazy-refcounts": true } }
87
88
89A union definition can specify a complex type as its base. In this case, the
90fields of the complex type are included as top-level fields of the union
91dictionary in the QMP wire format. An example definition is:
92
93 { 'type': 'BlockdevCommonOptions', 'data': { 'readonly': 'bool' } }
94 { 'union': 'BlockdevOptions',
95 'base': 'BlockdevCommonOptions',
96 'data': { 'raw': 'RawOptions',
97 'qcow2': 'Qcow2Options' } }
98
99And it looks like this on the wire:
100
101 { "type": "qcow2",
102 "readonly": false,
103 "data" : { "backing-file": "/some/place/my-image",
104 "lazy-refcounts": true } }
105
50f2bdc7
KW
106
107Flat union types avoid the nesting on the wire. They are used whenever a
108specific field of the base type is declared as the discriminator ('type' is
109then no longer generated). The discriminator must always be a string field.
110The above example can then be modified as follows:
111
112 { 'type': 'BlockdevCommonOptions',
113 'data': { 'driver': 'str', 'readonly': 'bool' } }
114 { 'union': 'BlockdevOptions',
115 'base': 'BlockdevCommonOptions',
116 'discriminator': 'driver',
117 'data': { 'raw': 'RawOptions',
118 'qcow2': 'Qcow2Options' } }
119
120Resulting in this JSON object:
121
122 { "driver": "qcow2",
123 "readonly": false,
124 "backing-file": "/some/place/my-image",
125 "lazy-refcounts": true }
126
127
69dd62df
KW
128A special type of unions are anonymous unions. They don't form a dictionary in
129the wire format but allow the direct use of different types in their place. As
130they aren't structured, they don't have any explicit discriminator but use
131the (QObject) data type of their value as an implicit discriminator. This means
132that they are restricted to using only one discriminator value per QObject
133type. For example, you cannot have two different complex types in an anonymous
134union, or two different integer types.
135
136Anonymous unions are declared using an empty dictionary as their discriminator.
137The discriminator values never appear on the wire, they are only used in the
138generated C code. Anonymous unions cannot have a base type.
139
140 { 'union': 'BlockRef',
141 'discriminator': {},
142 'data': { 'definition': 'BlockdevOptions',
143 'reference': 'str' } }
144
145This example allows using both of the following example objects:
146
147 { "file": "my_existing_block_device_id" }
148 { "file": { "driver": "file",
149 "readonly": false,
150 'filename': "/tmp/mydisk.qcow2" } }
151
152
51631493 153=== Commands ===
b84da831
MR
154
155Commands are defined by using a list containing three members. The first
156member is the command name, the second member is a dictionary containing
157arguments, and the third member is the return type.
158
159An example command is:
160
161 { 'command': 'my-command',
162 'data': { 'arg1': 'str', '*arg2': 'str' },
acf8394e 163 'returns': 'str' }
b84da831 164
b84da831
MR
165
166== Code generation ==
167
168Schemas are fed into 3 scripts to generate all the code/files that, paired
169with the core QAPI libraries, comprise everything required to take JSON
170commands read in by a QMP/guest agent server, unmarshal the arguments into
171the underlying C types, call into the corresponding C function, and map the
172response back to a QMP/guest agent response to be returned to the user.
173
174As an example, we'll use the following schema, which describes a single
175complex user-defined type (which will produce a C struct, along with a list
176node structure that can be used to chain together a list of such types in
177case we want to accept/return a list of this type with a command), and a
178command which takes that type as a parameter and returns the same type:
179
180 mdroth@illuin:~/w/qemu2.git$ cat example-schema.json
181 { 'type': 'UserDefOne',
182 'data': { 'integer': 'int', 'string': 'str' } }
183
184 { 'command': 'my-command',
185 'data': {'arg1': 'UserDefOne'},
186 'returns': 'UserDefOne' }
187 mdroth@illuin:~/w/qemu2.git$
188
189=== scripts/qapi-types.py ===
190
191Used to generate the C types defined by a schema. The following files are
192created:
193
194$(prefix)qapi-types.h - C types corresponding to types defined in
195 the schema you pass in
196$(prefix)qapi-types.c - Cleanup functions for the above C types
197
198The $(prefix) is an optional parameter used as a namespace to keep the
199generated code from one schema/code-generation separated from others so code
200can be generated/used from multiple schemas without clobbering previously
201created code.
202
203Example:
204
205 mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-types.py \
206 --output-dir="qapi-generated" --prefix="example-" < example-schema.json
207 mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.c
208 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
209
210 #include "qapi/qapi-dealloc-visitor.h"
211 #include "example-qapi-types.h"
212 #include "example-qapi-visit.h"
213
214 void qapi_free_UserDefOne(UserDefOne * obj)
215 {
216 QapiDeallocVisitor *md;
217 Visitor *v;
218
219 if (!obj) {
220 return;
221 }
222
223 md = qapi_dealloc_visitor_new();
224 v = qapi_dealloc_get_visitor(md);
225 visit_type_UserDefOne(v, &obj, NULL, NULL);
226 qapi_dealloc_visitor_cleanup(md);
227 }
228
229 mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.h
230 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
231 #ifndef QAPI_GENERATED_EXAMPLE_QAPI_TYPES
232 #define QAPI_GENERATED_EXAMPLE_QAPI_TYPES
233
234 #include "qapi/qapi-types-core.h"
235
236 typedef struct UserDefOne UserDefOne;
237
238 typedef struct UserDefOneList
239 {
240 UserDefOne *value;
241 struct UserDefOneList *next;
242 } UserDefOneList;
243
244 struct UserDefOne
245 {
246 int64_t integer;
247 char * string;
248 };
249
250 void qapi_free_UserDefOne(UserDefOne * obj);
251
252 #endif
253
254
255=== scripts/qapi-visit.py ===
256
257Used to generate the visitor functions used to walk through and convert
258a QObject (as provided by QMP) to a native C data structure and
259vice-versa, as well as the visitor function used to dealloc a complex
260schema-defined C type.
261
262The following files are generated:
263
264$(prefix)qapi-visit.c: visitor function for a particular C type, used
265 to automagically convert QObjects into the
266 corresponding C type and vice-versa, as well
267 as for deallocating memory for an existing C
268 type
269
270$(prefix)qapi-visit.h: declarations for previously mentioned visitor
271 functions
272
273Example:
274
275 mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-visit.py \
276 --output-dir="qapi-generated" --prefix="example-" < example-schema.json
277 mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.c
278 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
279
280 #include "example-qapi-visit.h"
281
282 void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp)
283 {
284 visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), errp);
285 visit_type_int(m, (obj && *obj) ? &(*obj)->integer : NULL, "integer", errp);
286 visit_type_str(m, (obj && *obj) ? &(*obj)->string : NULL, "string", errp);
287 visit_end_struct(m, errp);
288 }
289
290 void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp)
291 {
3a86a0fa 292 GenericList *i, **prev = (GenericList **)obj;
b84da831
MR
293
294 visit_start_list(m, name, errp);
295
3a86a0fa 296 for (; (i = visit_next_list(m, prev, errp)) != NULL; prev = &i) {
b84da831
MR
297 UserDefOneList *native_i = (UserDefOneList *)i;
298 visit_type_UserDefOne(m, &native_i->value, NULL, errp);
299 }
300
301 visit_end_list(m, errp);
302 }
303 mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.h
304 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
305
306 #ifndef QAPI_GENERATED_EXAMPLE_QAPI_VISIT
307 #define QAPI_GENERATED_EXAMPLE_QAPI_VISIT
308
309 #include "qapi/qapi-visit-core.h"
310 #include "example-qapi-types.h"
311
312 void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp);
313 void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp);
314
315 #endif
316 mdroth@illuin:~/w/qemu2.git$
317
d195325b
PB
318(The actual structure of the visit_type_* functions is a bit more complex
319in order to propagate errors correctly and avoid leaking memory).
b84da831
MR
320
321=== scripts/qapi-commands.py ===
322
323Used to generate the marshaling/dispatch functions for the commands defined
324in the schema. The following files are generated:
325
326$(prefix)qmp-marshal.c: command marshal/dispatch functions for each
327 QMP command defined in the schema. Functions
328 generated by qapi-visit.py are used to
2542bfd5 329 convert QObjects received from the wire into
b84da831
MR
330 function parameters, and uses the same
331 visitor functions to convert native C return
332 values to QObjects from transmission back
333 over the wire.
334
335$(prefix)qmp-commands.h: Function prototypes for the QMP commands
336 specified in the schema.
337
338Example:
339
340 mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-marshal.c
341 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
342
343 #include "qemu-objects.h"
344 #include "qapi/qmp-core.h"
345 #include "qapi/qapi-visit-core.h"
346 #include "qapi/qmp-output-visitor.h"
347 #include "qapi/qmp-input-visitor.h"
348 #include "qapi/qapi-dealloc-visitor.h"
349 #include "example-qapi-types.h"
350 #include "example-qapi-visit.h"
351
352 #include "example-qmp-commands.h"
353 static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
354 {
355 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
356 QmpOutputVisitor *mo = qmp_output_visitor_new();
357 Visitor *v;
358
359 v = qmp_output_get_visitor(mo);
360 visit_type_UserDefOne(v, &ret_in, "unused", errp);
361 v = qapi_dealloc_get_visitor(md);
362 visit_type_UserDefOne(v, &ret_in, "unused", errp);
363 qapi_dealloc_visitor_cleanup(md);
364
365
366 *ret_out = qmp_output_get_qobject(mo);
367 }
368
369 static void qmp_marshal_input_my_command(QmpState *qmp__sess, QDict *args, QObject **ret, Error **errp)
370 {
371 UserDefOne * retval = NULL;
372 QmpInputVisitor *mi;
373 QapiDeallocVisitor *md;
374 Visitor *v;
375 UserDefOne * arg1 = NULL;
376
377 mi = qmp_input_visitor_new(QOBJECT(args));
378 v = qmp_input_get_visitor(mi);
379 visit_type_UserDefOne(v, &arg1, "arg1", errp);
380
381 if (error_is_set(errp)) {
382 goto out;
383 }
384 retval = qmp_my_command(arg1, errp);
385 qmp_marshal_output_my_command(retval, ret, errp);
386
387 out:
388 md = qapi_dealloc_visitor_new();
389 v = qapi_dealloc_get_visitor(md);
390 visit_type_UserDefOne(v, &arg1, "arg1", errp);
391 qapi_dealloc_visitor_cleanup(md);
392 return;
393 }
394
395 static void qmp_init_marshal(void)
396 {
397 qmp_register_command("my-command", qmp_marshal_input_my_command);
398 }
399
400 qapi_init(qmp_init_marshal);
401 mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-commands.h
402 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
403
404 #ifndef QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
405 #define QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
406
407 #include "example-qapi-types.h"
408 #include "error.h"
409
410 UserDefOne * qmp_my_command(UserDefOne * arg1, Error **errp);
411
412 #endif
413 mdroth@illuin:~/w/qemu2.git$