xref: /openbmc/qemu/docs/devel/qapi-code-gen.rst (revision 2406e1e7)
1==================================
2How to use the QAPI code generator
3==================================
4
5..
6   Copyright IBM Corp. 2011
7   Copyright (C) 2012-2016 Red Hat, Inc.
8
9   This work is licensed under the terms of the GNU GPL, version 2 or
10   later.  See the COPYING file in the top-level directory.
11
12
13Introduction
14============
15
16QAPI is a native C API within QEMU which provides management-level
17functionality to internal and external users.  For external
18users/processes, this interface is made available by a JSON-based wire
19format for the QEMU Monitor Protocol (QMP) for controlling qemu, as
20well as the QEMU Guest Agent (QGA) for communicating with the guest.
21The remainder of this document uses "Client JSON Protocol" when
22referring to the wire contents of a QMP or QGA connection.
23
24To map between Client JSON Protocol interfaces and the native C API,
25we generate C code from a QAPI schema.  This document describes the
26QAPI schema language, and how it gets mapped to the Client JSON
27Protocol and to C.  It additionally provides guidance on maintaining
28Client JSON Protocol compatibility.
29
30
31The QAPI schema language
32========================
33
34The QAPI schema defines the Client JSON Protocol's commands and
35events, as well as types used by them.  Forward references are
36allowed.
37
38It is permissible for the schema to contain additional types not used
39by any commands or events, for the side effect of generated C code
40used internally.
41
42There are several kinds of types: simple types (a number of built-in
43types, such as ``int`` and ``str``; as well as enumerations), arrays,
44complex types (structs and unions), and alternate types (a choice
45between other types).
46
47
48Schema syntax
49-------------
50
51Syntax is loosely based on `JSON <http://www.ietf.org/rfc/rfc8259.txt>`_.
52Differences:
53
54* Comments: start with a hash character (``#``) that is not part of a
55  string, and extend to the end of the line.
56
57* Strings are enclosed in ``'single quotes'``, not ``"double quotes"``.
58
59* Strings are restricted to printable ASCII, and escape sequences to
60  just ``\\``.
61
62* Numbers and ``null`` are not supported.
63
64A second layer of syntax defines the sequences of JSON texts that are
65a correctly structured QAPI schema.  We provide a grammar for this
66syntax in an EBNF-like notation:
67
68* Production rules look like ``non-terminal = expression``
69* Concatenation: expression ``A B`` matches expression ``A``, then ``B``
70* Alternation: expression ``A | B`` matches expression ``A`` or ``B``
71* Repetition: expression ``A...`` matches zero or more occurrences of
72  expression ``A``
73* Repetition: expression ``A, ...`` matches zero or more occurrences of
74  expression ``A`` separated by ``,``
75* Grouping: expression ``( A )`` matches expression ``A``
76* JSON's structural characters are terminals: ``{ } [ ] : ,``
77* JSON's literal names are terminals: ``false true``
78* String literals enclosed in ``'single quotes'`` are terminal, and match
79  this JSON string, with a leading ``*`` stripped off
80* When JSON object member's name starts with ``*``, the member is
81  optional.
82* The symbol ``STRING`` is a terminal, and matches any JSON string
83* The symbol ``BOOL`` is a terminal, and matches JSON ``false`` or ``true``
84* ALL-CAPS words other than ``STRING`` are non-terminals
85
86The order of members within JSON objects does not matter unless
87explicitly noted.
88
89A QAPI schema consists of a series of top-level expressions::
90
91    SCHEMA = TOP-LEVEL-EXPR...
92
93The top-level expressions are all JSON objects.  Code and
94documentation is generated in schema definition order.  Code order
95should not matter.
96
97A top-level expressions is either a directive or a definition::
98
99    TOP-LEVEL-EXPR = DIRECTIVE | DEFINITION
100
101There are two kinds of directives and six kinds of definitions::
102
103    DIRECTIVE = INCLUDE | PRAGMA
104    DEFINITION = ENUM | STRUCT | UNION | ALTERNATE | COMMAND | EVENT
105
106These are discussed in detail below.
107
108
109Built-in Types
110--------------
111
112The following types are predefined, and map to C as follows:
113
114  ============= ============== ============================================
115  Schema        C              JSON
116  ============= ============== ============================================
117  ``str``       ``char *``     any JSON string, UTF-8
118  ``number``    ``double``     any JSON number
119  ``int``       ``int64_t``    a JSON number without fractional part
120                               that fits into the C integer type
121  ``int8``      ``int8_t``     likewise
122  ``int16``     ``int16_t``    likewise
123  ``int32``     ``int32_t``    likewise
124  ``int64``     ``int64_t``    likewise
125  ``uint8``     ``uint8_t``    likewise
126  ``uint16``    ``uint16_t``   likewise
127  ``uint32``    ``uint32_t``   likewise
128  ``uint64``    ``uint64_t``   likewise
129  ``size``      ``uint64_t``   like ``uint64_t``, except
130                               ``StringInputVisitor`` accepts size suffixes
131  ``bool``      ``bool``       JSON ``true`` or ``false``
132  ``null``      ``QNull *``    JSON ``null``
133  ``any``       ``QObject *``  any JSON value
134  ``QType``     ``QType``      JSON string matching enum ``QType`` values
135  ============= ============== ============================================
136
137
138Include directives
139------------------
140
141Syntax::
142
143    INCLUDE = { 'include': STRING }
144
145The QAPI schema definitions can be modularized using the 'include' directive::
146
147 { 'include': 'path/to/file.json' }
148
149The directive is evaluated recursively, and include paths are relative
150to the file using the directive.  Multiple includes of the same file
151are idempotent.
152
153As a matter of style, it is a good idea to have all files be
154self-contained, but at the moment, nothing prevents an included file
155from making a forward reference to a type that is only introduced by
156an outer file.  The parser may be made stricter in the future to
157prevent incomplete include files.
158
159.. _pragma:
160
161Pragma directives
162-----------------
163
164Syntax::
165
166    PRAGMA = { 'pragma': {
167                   '*doc-required': BOOL,
168                   '*command-name-exceptions': [ STRING, ... ],
169                   '*command-returns-exceptions': [ STRING, ... ],
170                   '*documentation-exceptions': [ STRING, ... ],
171                   '*member-name-exceptions': [ STRING, ... ] } }
172
173The pragma directive lets you control optional generator behavior.
174
175Pragma's scope is currently the complete schema.  Setting the same
176pragma to different values in parts of the schema doesn't work.
177
178Pragma 'doc-required' takes a boolean value.  If true, documentation
179is required.  Default is false.
180
181Pragma 'command-name-exceptions' takes a list of commands whose names
182may contain ``"_"`` instead of ``"-"``.  Default is none.
183
184Pragma 'command-returns-exceptions' takes a list of commands that may
185violate the rules on permitted return types.  Default is none.
186
187Pragma 'documentation-exceptions' takes a list of types, commands, and
188events whose members / arguments need not be documented.  Default is
189none.
190
191Pragma 'member-name-exceptions' takes a list of types whose member
192names may contain uppercase letters, and ``"_"`` instead of ``"-"``.
193Default is none.
194
195.. _ENUM-VALUE:
196
197Enumeration types
198-----------------
199
200Syntax::
201
202    ENUM = { 'enum': STRING,
203             'data': [ ENUM-VALUE, ... ],
204             '*prefix': STRING,
205             '*if': COND,
206             '*features': FEATURES }
207    ENUM-VALUE = STRING
208               | { 'name': STRING,
209                   '*if': COND,
210                   '*features': FEATURES }
211
212Member 'enum' names the enum type.
213
214Each member of the 'data' array defines a value of the enumeration
215type.  The form STRING is shorthand for :code:`{ 'name': STRING }`.  The
216'name' values must be be distinct.
217
218Example::
219
220 { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
221
222Nothing prevents an empty enumeration, although it is probably not
223useful.
224
225On the wire, an enumeration type's value is represented by its
226(string) name.  In C, it's represented by an enumeration constant.
227These are of the form PREFIX_NAME, where PREFIX is derived from the
228enumeration type's name, and NAME from the value's name.  For the
229example above, the generator maps 'MyEnum' to MY_ENUM and 'value1' to
230VALUE1, resulting in the enumeration constant MY_ENUM_VALUE1.  The
231optional 'prefix' member overrides PREFIX.
232
233The generated C enumeration constants have values 0, 1, ..., N-1 (in
234QAPI schema order), where N is the number of values.  There is an
235additional enumeration constant PREFIX__MAX with value N.
236
237Do not use string or an integer type when an enumeration type can do
238the job satisfactorily.
239
240The optional 'if' member specifies a conditional.  See `Configuring the
241schema`_ below for more on this.
242
243The optional 'features' member specifies features.  See Features_
244below for more on this.
245
246
247.. _TYPE-REF:
248
249Type references and array types
250-------------------------------
251
252Syntax::
253
254    TYPE-REF = STRING | ARRAY-TYPE
255    ARRAY-TYPE = [ STRING ]
256
257A string denotes the type named by the string.
258
259A one-element array containing a string denotes an array of the type
260named by the string.  Example: ``['int']`` denotes an array of ``int``.
261
262
263Struct types
264------------
265
266Syntax::
267
268    STRUCT = { 'struct': STRING,
269               'data': MEMBERS,
270               '*base': STRING,
271               '*if': COND,
272               '*features': FEATURES }
273    MEMBERS = { MEMBER, ... }
274    MEMBER = STRING : TYPE-REF
275           | STRING : { 'type': TYPE-REF,
276                        '*if': COND,
277                        '*features': FEATURES }
278
279Member 'struct' names the struct type.
280
281Each MEMBER of the 'data' object defines a member of the struct type.
282
283.. _MEMBERS:
284
285The MEMBER's STRING name consists of an optional ``*`` prefix and the
286struct member name.  If ``*`` is present, the member is optional.
287
288The MEMBER's value defines its properties, in particular its type.
289The form TYPE-REF_ is shorthand for :code:`{ 'type': TYPE-REF }`.
290
291Example::
292
293 { 'struct': 'MyType',
294   'data': { 'member1': 'str', 'member2': ['int'], '*member3': 'str' } }
295
296A struct type corresponds to a struct in C, and an object in JSON.
297The C struct's members are generated in QAPI schema order.
298
299The optional 'base' member names a struct type whose members are to be
300included in this type.  They go first in the C struct.
301
302Example::
303
304 { 'struct': 'BlockdevOptionsGenericFormat',
305   'data': { 'file': 'str' } }
306 { 'struct': 'BlockdevOptionsGenericCOWFormat',
307   'base': 'BlockdevOptionsGenericFormat',
308   'data': { '*backing': 'str' } }
309
310An example BlockdevOptionsGenericCOWFormat object on the wire could use
311both members like this::
312
313 { "file": "/some/place/my-image",
314   "backing": "/some/place/my-backing-file" }
315
316The optional 'if' member specifies a conditional.  See `Configuring
317the schema`_ below for more on this.
318
319The optional 'features' member specifies features.  See Features_
320below for more on this.
321
322
323Union types
324-----------
325
326Syntax::
327
328    UNION = { 'union': STRING,
329              'base': ( MEMBERS | STRING ),
330              'discriminator': STRING,
331              'data': BRANCHES,
332              '*if': COND,
333              '*features': FEATURES }
334    BRANCHES = { BRANCH, ... }
335    BRANCH = STRING : TYPE-REF
336           | STRING : { 'type': TYPE-REF, '*if': COND }
337
338Member 'union' names the union type.
339
340The 'base' member defines the common members.  If it is a MEMBERS_
341object, it defines common members just like a struct type's 'data'
342member defines struct type members.  If it is a STRING, it names a
343struct type whose members are the common members.
344
345Member 'discriminator' must name a non-optional enum-typed member of
346the base struct.  That member's value selects a branch by its name.
347If no such branch exists, an empty branch is assumed.
348
349Each BRANCH of the 'data' object defines a branch of the union.  A
350union must have at least one branch.
351
352The BRANCH's STRING name is the branch name.  It must be a value of
353the discriminator enum type.
354
355The BRANCH's value defines the branch's properties, in particular its
356type.  The type must a struct type.  The form TYPE-REF_ is shorthand
357for :code:`{ 'type': TYPE-REF }`.
358
359In the Client JSON Protocol, a union is represented by an object with
360the common members (from the base type) and the selected branch's
361members.  The two sets of member names must be disjoint.
362
363Example::
364
365 { 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] }
366 { 'union': 'BlockdevOptions',
367   'base': { 'driver': 'BlockdevDriver', '*read-only': 'bool' },
368   'discriminator': 'driver',
369   'data': { 'file': 'BlockdevOptionsFile',
370             'qcow2': 'BlockdevOptionsQcow2' } }
371
372Resulting in these JSON objects::
373
374 { "driver": "file", "read-only": true,
375   "filename": "/some/place/my-image" }
376 { "driver": "qcow2", "read-only": false,
377   "backing": "/some/place/my-image", "lazy-refcounts": true }
378
379The order of branches need not match the order of the enum values.
380The branches need not cover all possible enum values.  In the
381resulting generated C data types, a union is represented as a struct
382with the base members in QAPI schema order, and then a union of
383structures for each branch of the struct.
384
385The optional 'if' member specifies a conditional.  See `Configuring
386the schema`_ below for more on this.
387
388The optional 'features' member specifies features.  See Features_
389below for more on this.
390
391
392Alternate types
393---------------
394
395Syntax::
396
397    ALTERNATE = { 'alternate': STRING,
398                  'data': ALTERNATIVES,
399                  '*if': COND,
400                  '*features': FEATURES }
401    ALTERNATIVES = { ALTERNATIVE, ... }
402    ALTERNATIVE = STRING : STRING
403                | STRING : { 'type': STRING, '*if': COND }
404
405Member 'alternate' names the alternate type.
406
407Each ALTERNATIVE of the 'data' object defines a branch of the
408alternate.  An alternate must have at least one branch.
409
410The ALTERNATIVE's STRING name is the branch name.
411
412The ALTERNATIVE's value defines the branch's properties, in particular
413its type.  The form STRING is shorthand for :code:`{ 'type': STRING }`.
414
415Example::
416
417 { 'alternate': 'BlockdevRef',
418   'data': { 'definition': 'BlockdevOptions',
419             'reference': 'str' } }
420
421An alternate type is like a union type, except there is no
422discriminator on the wire.  Instead, the branch to use is inferred
423from the value.  An alternate can only express a choice between types
424represented differently on the wire.
425
426If a branch is typed as the 'bool' built-in, the alternate accepts
427true and false; if it is typed as any of the various numeric
428built-ins, it accepts a JSON number; if it is typed as a 'str'
429built-in or named enum type, it accepts a JSON string; if it is typed
430as the 'null' built-in, it accepts JSON null; and if it is typed as a
431complex type (struct or union), it accepts a JSON object.
432
433The example alternate declaration above allows using both of the
434following example objects::
435
436 { "file": "my_existing_block_device_id" }
437 { "file": { "driver": "file",
438             "read-only": false,
439             "filename": "/tmp/mydisk.qcow2" } }
440
441The optional 'if' member specifies a conditional.  See `Configuring
442the schema`_ below for more on this.
443
444The optional 'features' member specifies features.  See Features_
445below for more on this.
446
447
448Commands
449--------
450
451Syntax::
452
453    COMMAND = { 'command': STRING,
454                (
455                '*data': ( MEMBERS | STRING ),
456                |
457                'data': STRING,
458                'boxed': true,
459                )
460                '*returns': TYPE-REF,
461                '*success-response': false,
462                '*gen': false,
463                '*allow-oob': true,
464                '*allow-preconfig': true,
465                '*coroutine': true,
466                '*if': COND,
467                '*features': FEATURES }
468
469Member 'command' names the command.
470
471Member 'data' defines the arguments.  It defaults to an empty MEMBERS_
472object.
473
474If 'data' is a MEMBERS_ object, then MEMBERS defines arguments just
475like a struct type's 'data' defines struct type members.
476
477If 'data' is a STRING, then STRING names a complex type whose members
478are the arguments.  A union type requires ``'boxed': true``.
479
480Member 'returns' defines the command's return type.  It defaults to an
481empty struct type.  It must normally be a complex type or an array of
482a complex type.  To return anything else, the command must be listed
483in pragma 'commands-returns-exceptions'.  If you do this, extending
484the command to return additional information will be harder.  Use of
485the pragma for new commands is strongly discouraged.
486
487A command's error responses are not specified in the QAPI schema.
488Error conditions should be documented in comments.
489
490In the Client JSON Protocol, the value of the "execute" or "exec-oob"
491member is the command name.  The value of the "arguments" member then
492has to conform to the arguments, and the value of the success
493response's "return" member will conform to the return type.
494
495Some example commands::
496
497 { 'command': 'my-first-command',
498   'data': { 'arg1': 'str', '*arg2': 'str' } }
499 { 'struct': 'MyType', 'data': { '*value': 'str' } }
500 { 'command': 'my-second-command',
501   'returns': [ 'MyType' ] }
502
503which would validate this Client JSON Protocol transaction::
504
505 => { "execute": "my-first-command",
506      "arguments": { "arg1": "hello" } }
507 <= { "return": { } }
508 => { "execute": "my-second-command" }
509 <= { "return": [ { "value": "one" }, { } ] }
510
511The generator emits a prototype for the C function implementing the
512command.  The function itself needs to be written by hand.  See
513section `Code generated for commands`_ for examples.
514
515The function returns the return type.  When member 'boxed' is absent,
516it takes the command arguments as arguments one by one, in QAPI schema
517order.  Else it takes them wrapped in the C struct generated for the
518complex argument type.  It takes an additional ``Error **`` argument in
519either case.
520
521The generator also emits a marshalling function that extracts
522arguments for the user's function out of an input QDict, calls the
523user's function, and if it succeeded, builds an output QObject from
524its return value.  This is for use by the QMP monitor core.
525
526In rare cases, QAPI cannot express a type-safe representation of a
527corresponding Client JSON Protocol command.  You then have to suppress
528generation of a marshalling function by including a member 'gen' with
529boolean value false, and instead write your own function.  For
530example::
531
532 { 'command': 'netdev_add',
533   'data': {'type': 'str', 'id': 'str'},
534   'gen': false }
535
536Please try to avoid adding new commands that rely on this, and instead
537use type-safe unions.
538
539Normally, the QAPI schema is used to describe synchronous exchanges,
540where a response is expected.  But in some cases, the action of a
541command is expected to change state in a way that a successful
542response is not possible (although the command will still return an
543error object on failure).  When a successful reply is not possible,
544the command definition includes the optional member 'success-response'
545with boolean value false.  So far, only QGA makes use of this member.
546
547Member 'allow-oob' declares whether the command supports out-of-band
548(OOB) execution.  It defaults to false.  For example::
549
550 { 'command': 'migrate_recover',
551   'data': { 'uri': 'str' }, 'allow-oob': true }
552
553See the :doc:`/interop/qmp-spec` for out-of-band execution syntax
554and semantics.
555
556Commands supporting out-of-band execution can still be executed
557in-band.
558
559When a command is executed in-band, its handler runs in the main
560thread with the BQL held.
561
562When a command is executed out-of-band, its handler runs in a
563dedicated monitor I/O thread with the BQL *not* held.
564
565An OOB-capable command handler must satisfy the following conditions:
566
567- It terminates quickly.
568- It does not invoke system calls that may block.
569- It does not access guest RAM that may block when userfaultfd is
570  enabled for postcopy live migration.
571- It takes only "fast" locks, i.e. all critical sections protected by
572  any lock it takes also satisfy the conditions for OOB command
573  handler code.
574
575The restrictions on locking limit access to shared state.  Such access
576requires synchronization, but OOB commands can't take the BQL or any
577other "slow" lock.
578
579When in doubt, do not implement OOB execution support.
580
581Member 'allow-preconfig' declares whether the command is available
582before the machine is built.  It defaults to false.  For example::
583
584 { 'enum': 'QMPCapability',
585   'data': [ 'oob' ] }
586 { 'command': 'qmp_capabilities',
587   'data': { '*enable': [ 'QMPCapability' ] },
588   'allow-preconfig': true }
589
590QMP is available before the machine is built only when QEMU was
591started with --preconfig.
592
593Member 'coroutine' tells the QMP dispatcher whether the command handler
594is safe to be run in a coroutine.  It defaults to false.  If it is true,
595the command handler is called from coroutine context and may yield while
596waiting for an external event (such as I/O completion) in order to avoid
597blocking the guest and other background operations.
598
599Coroutine safety can be hard to prove, similar to thread safety.  Common
600pitfalls are:
601
602- The BQL isn't held across ``qemu_coroutine_yield()``, so
603  operations that used to assume that they execute atomically may have
604  to be more careful to protect against changes in the global state.
605
606- Nested event loops (``AIO_WAIT_WHILE()`` etc.) are problematic in
607  coroutine context and can easily lead to deadlocks.  They should be
608  replaced by yielding and reentering the coroutine when the condition
609  becomes false.
610
611Since the command handler may assume coroutine context, any callers
612other than the QMP dispatcher must also call it in coroutine context.
613In particular, HMP commands calling such a QMP command handler must be
614marked ``.coroutine = true`` in hmp-commands.hx.
615
616It is an error to specify both ``'coroutine': true`` and ``'allow-oob': true``
617for a command.  We don't currently have a use case for both together and
618without a use case, it's not entirely clear what the semantics should
619be.
620
621The optional 'if' member specifies a conditional.  See `Configuring
622the schema`_ below for more on this.
623
624The optional 'features' member specifies features.  See Features_
625below for more on this.
626
627
628Events
629------
630
631Syntax::
632
633    EVENT = { 'event': STRING,
634              (
635              '*data': ( MEMBERS | STRING ),
636              |
637              'data': STRING,
638              'boxed': true,
639              )
640              '*if': COND,
641              '*features': FEATURES }
642
643Member 'event' names the event.  This is the event name used in the
644Client JSON Protocol.
645
646Member 'data' defines the event-specific data.  It defaults to an
647empty MEMBERS object.
648
649If 'data' is a MEMBERS object, then MEMBERS defines event-specific
650data just like a struct type's 'data' defines struct type members.
651
652If 'data' is a STRING, then STRING names a complex type whose members
653are the event-specific data.  A union type requires ``'boxed': true``.
654
655An example event is::
656
657 { 'event': 'EVENT_C',
658   'data': { '*a': 'int', 'b': 'str' } }
659
660Resulting in this JSON object::
661
662 { "event": "EVENT_C",
663   "data": { "b": "test string" },
664   "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
665
666The generator emits a function to send the event.  When member 'boxed'
667is absent, it takes event-specific data one by one, in QAPI schema
668order.  Else it takes them wrapped in the C struct generated for the
669complex type.  See section `Code generated for events`_ for examples.
670
671The optional 'if' member specifies a conditional.  See `Configuring
672the schema`_ below for more on this.
673
674The optional 'features' member specifies features.  See Features_
675below for more on this.
676
677
678.. _FEATURE:
679
680Features
681--------
682
683Syntax::
684
685    FEATURES = [ FEATURE, ... ]
686    FEATURE = STRING
687            | { 'name': STRING, '*if': COND }
688
689Sometimes, the behaviour of QEMU changes compatibly, but without a
690change in the QMP syntax (usually by allowing values or operations
691that previously resulted in an error).  QMP clients may still need to
692know whether the extension is available.
693
694For this purpose, a list of features can be specified for definitions,
695enumeration values, and struct members.  Each feature list member can
696either be ``{ 'name': STRING, '*if': COND }``, or STRING, which is
697shorthand for ``{ 'name': STRING }``.
698
699The optional 'if' member specifies a conditional.  See `Configuring
700the schema`_ below for more on this.
701
702Example::
703
704 { 'struct': 'TestType',
705   'data': { 'number': 'int' },
706   'features': [ 'allow-negative-numbers' ] }
707
708The feature strings are exposed to clients in introspection, as
709explained in section `Client JSON Protocol introspection`_.
710
711Intended use is to have each feature string signal that this build of
712QEMU shows a certain behaviour.
713
714
715Special features
716~~~~~~~~~~~~~~~~
717
718Feature "deprecated" marks a command, event, enum value, or struct
719member as deprecated.  It is not supported elsewhere so far.
720Interfaces so marked may be withdrawn in future releases in accordance
721with QEMU's deprecation policy.
722
723Feature "unstable" marks a command, event, enum value, or struct
724member as unstable.  It is not supported elsewhere so far.  Interfaces
725so marked may be withdrawn or changed incompatibly in future releases.
726
727
728Naming rules and reserved names
729-------------------------------
730
731All names must begin with a letter, and contain only ASCII letters,
732digits, hyphen, and underscore.  There are two exceptions: enum values
733may start with a digit, and names that are downstream extensions (see
734section `Downstream extensions`_) start with underscore.
735
736Names beginning with ``q_`` are reserved for the generator, which uses
737them for munging QMP names that resemble C keywords or other
738problematic strings.  For example, a member named ``default`` in qapi
739becomes ``q_default`` in the generated C code.
740
741Types, commands, and events share a common namespace.  Therefore,
742generally speaking, type definitions should always use CamelCase for
743user-defined type names, while built-in types are lowercase.
744
745Type names ending with ``List`` are reserved for the generator, which
746uses them for array types.
747
748Command names, member names within a type, and feature names should be
749all lower case with words separated by a hyphen.  However, some
750existing older commands and complex types use underscore; when
751extending them, consistency is preferred over blindly avoiding
752underscore.
753
754Event names should be ALL_CAPS with words separated by underscore.
755
756Member name ``u`` and names starting with ``has-`` or ``has_`` are reserved
757for the generator, which uses them for unions and for tracking
758optional members.
759
760Names beginning with ``x-`` used to signify "experimental".  This
761convention has been replaced by special feature "unstable".
762
763Pragmas ``command-name-exceptions`` and ``member-name-exceptions`` let
764you violate naming rules.  Use for new code is strongly discouraged. See
765`Pragma directives`_ for details.
766
767
768Downstream extensions
769---------------------
770
771QAPI schema names that are externally visible, say in the Client JSON
772Protocol, need to be managed with care.  Names starting with a
773downstream prefix of the form __RFQDN_ are reserved for the downstream
774who controls the valid, reverse fully qualified domain name RFQDN.
775RFQDN may only contain ASCII letters, digits, hyphen and period.
776
777Example: Red Hat, Inc. controls redhat.com, and may therefore add a
778downstream command ``__com.redhat_drive-mirror``.
779
780
781Configuring the schema
782----------------------
783
784Syntax::
785
786    COND = STRING
787         | { 'all: [ COND, ... ] }
788         | { 'any: [ COND, ... ] }
789         | { 'not': COND }
790
791All definitions take an optional 'if' member.  Its value must be a
792string, or an object with a single member 'all', 'any' or 'not'.
793
794The C code generated for the definition will then be guarded by an #if
795preprocessing directive with an operand generated from that condition:
796
797 * STRING will generate defined(STRING)
798 * { 'all': [COND, ...] } will generate (COND && ...)
799 * { 'any': [COND, ...] } will generate (COND || ...)
800 * { 'not': COND } will generate !COND
801
802Example: a conditional struct ::
803
804 { 'struct': 'IfStruct', 'data': { 'foo': 'int' },
805   'if': { 'all': [ 'CONFIG_FOO', 'HAVE_BAR' ] } }
806
807gets its generated code guarded like this::
808
809 #if defined(CONFIG_FOO) && defined(HAVE_BAR)
810 ... generated code ...
811 #endif /* defined(HAVE_BAR) && defined(CONFIG_FOO) */
812
813Individual members of complex types can also be made conditional.
814This requires the longhand form of MEMBER.
815
816Example: a struct type with unconditional member 'foo' and conditional
817member 'bar' ::
818
819 { 'struct': 'IfStruct',
820   'data': { 'foo': 'int',
821             'bar': { 'type': 'int', 'if': 'IFCOND'} } }
822
823A union's discriminator may not be conditional.
824
825Likewise, individual enumeration values may be conditional.  This
826requires the longhand form of ENUM-VALUE_.
827
828Example: an enum type with unconditional value 'foo' and conditional
829value 'bar' ::
830
831 { 'enum': 'IfEnum',
832   'data': [ 'foo',
833             { 'name' : 'bar', 'if': 'IFCOND' } ] }
834
835Likewise, features can be conditional.  This requires the longhand
836form of FEATURE_.
837
838Example: a struct with conditional feature 'allow-negative-numbers' ::
839
840 { 'struct': 'TestType',
841   'data': { 'number': 'int' },
842   'features': [ { 'name': 'allow-negative-numbers',
843                   'if': 'IFCOND' } ] }
844
845Please note that you are responsible to ensure that the C code will
846compile with an arbitrary combination of conditions, since the
847generator is unable to check it at this point.
848
849The conditions apply to introspection as well, i.e. introspection
850shows a conditional entity only when the condition is satisfied in
851this particular build.
852
853
854Documentation comments
855----------------------
856
857A multi-line comment that starts and ends with a ``##`` line is a
858documentation comment.
859
860If the documentation comment starts like ::
861
862    ##
863    # @SYMBOL:
864
865it documents the definition of SYMBOL, else it's free-form
866documentation.
867
868See below for more on `Definition documentation`_.
869
870Free-form documentation may be used to provide additional text and
871structuring content.
872
873
874Headings and subheadings
875~~~~~~~~~~~~~~~~~~~~~~~~
876
877A free-form documentation comment containing a line which starts with
878some ``=`` symbols and then a space defines a section heading::
879
880    ##
881    # = This is a top level heading
882    #
883    # This is a free-form comment which will go under the
884    # top level heading.
885    ##
886
887    ##
888    # == This is a second level heading
889    ##
890
891A heading line must be the first line of the documentation
892comment block.
893
894Section headings must always be correctly nested, so you can only
895define a third-level heading inside a second-level heading, and so on.
896
897
898Documentation markup
899~~~~~~~~~~~~~~~~~~~~
900
901Documentation comments can use most rST markup.  In particular,
902a ``::`` literal block can be used for pre-formatted text::
903
904    # ::
905    #
906    #   Text of the example, may span
907    #   multiple lines
908
909``*`` starts an itemized list::
910
911    # * First item, may span
912    #   multiple lines
913    # * Second item
914
915You can also use ``-`` instead of ``*``.
916
917A decimal number followed by ``.`` starts a numbered list::
918
919    # 1. First item, may span
920    #    multiple lines
921    # 2. Second item
922
923The actual number doesn't matter.
924
925Lists of either kind must be preceded and followed by a blank line.
926If a list item's text spans multiple lines, then the second and
927subsequent lines must be correctly indented to line up with the
928first character of the first line.
929
930The usual ****strong****, *\*emphasized\** and ````literal```` markup
931should be used.  If you need a single literal ``*``, you will need to
932backslash-escape it.
933
934Use ``@foo`` to reference a name in the schema.  This is an rST
935extension.  It is rendered the same way as ````foo````, but carries
936additional meaning.
937
938Example::
939
940 ##
941 # Some text foo with **bold** and *emphasis*
942 #
943 # 1. with a list
944 # 2. like that
945 #
946 # And some code:
947 #
948 # ::
949 #
950 #   $ echo foo
951 #   -> do this
952 #   <- get that
953 ##
954
955For legibility, wrap text paragraphs so every line is at most 70
956characters long.
957
958Separate sentences with two spaces.
959
960
961Definition documentation
962~~~~~~~~~~~~~~~~~~~~~~~~
963
964Definition documentation, if present, must immediately precede the
965definition it documents.
966
967When documentation is required (see pragma_ 'doc-required'), every
968definition must have documentation.
969
970Definition documentation starts with a line naming the definition,
971followed by an optional overview, a description of each argument (for
972commands and events), member (for structs and unions), branch (for
973alternates), or value (for enums), a description of each feature (if
974any), and finally optional tagged sections.
975
976Descriptions start with '\@name:'.  The description text must be
977indented like this::
978
979 # @name: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
980 #     do eiusmod tempor incididunt ut labore et dolore magna aliqua.
981
982.. FIXME The parser accepts these things in almost any order.
983
984.. FIXME union branches should be described, too.
985
986Extensions added after the definition was first released carry a
987"(since x.y.z)" comment.
988
989The feature descriptions must be preceded by a blank line and then a
990line "Features:", like this::
991
992  #
993  # Features:
994  #
995  # @feature: Description text
996
997A tagged section begins with a paragraph that starts with one of the
998following words: "Since:", "Returns:", "Errors:", "TODO:".  It ends with
999the start of a new section.
1000
1001The second and subsequent lines of tagged sections must be indented
1002like this::
1003
1004 # TODO: Ut enim ad minim veniam, quis nostrud exercitation ullamco
1005 #     laboris nisi ut aliquip ex ea commodo consequat.
1006 #
1007 #     Duis aute irure dolor in reprehenderit in voluptate velit esse
1008 #     cillum dolore eu fugiat nulla pariatur.
1009
1010"Returns" and "Errors" sections are only valid for commands.  They
1011document the success and the error response, respectively.
1012
1013"Errors" sections should be formatted as an rST list, each entry
1014detailing a relevant error condition. For example::
1015
1016 # Errors:
1017 #     - If @device does not exist, DeviceNotFound
1018 #     - Any other error returns a GenericError.
1019
1020A "Since: x.y.z" tagged section lists the release that introduced the
1021definition.
1022
1023"TODO" sections are not rendered (they are for developers, not users of
1024QMP).  In other sections, the text is formatted, and rST markup can be
1025used.
1026
1027QMP Examples can be added by using the ``.. qmp-example::``
1028directive. In its simplest form, this can be used to contain a single
1029QMP code block which accepts standard JSON syntax with additional server
1030directionality indicators (``->`` and ``<-``), and elisions (``...``).
1031
1032Optionally, a plaintext title may be provided by using the ``:title:``
1033directive option. If the title is omitted, the example title will
1034default to "Example:".
1035
1036A simple QMP example::
1037
1038  # .. qmp-example::
1039  #    :title: Using query-block
1040  #
1041  #    -> { "execute": "query-block" }
1042  #    <- { ... }
1043
1044More complex or multi-step examples where exposition is needed before or
1045between QMP code blocks can be created by using the ``:annotated:``
1046directive option. When using this option, nested QMP code blocks must be
1047entered explicitly with rST's ``::`` syntax.
1048
1049Highlighting in non-QMP languages can be accomplished by using the
1050``.. code-block:: lang`` directive, and non-highlighted text can be
1051achieved by omitting the language argument.
1052
1053For example::
1054
1055  # .. qmp-example::
1056  #    :annotated:
1057  #    :title: A more complex demonstration
1058  #
1059  #    This is a more complex example that can use
1060  #    ``arbitrary rST syntax`` in its exposition::
1061  #
1062  #      -> { "execute": "query-block" }
1063  #      <- { ... }
1064  #
1065  #    Above, lengthy output has been omitted for brevity.
1066
1067
1068Examples of complete definition documentation::
1069
1070 ##
1071 # @BlockStats:
1072 #
1073 # Statistics of a virtual block device or a block backing device.
1074 #
1075 # @device: If the stats are for a virtual block device, the name
1076 #     corresponding to the virtual block device.
1077 #
1078 # @node-name: The node name of the device.  (Since 2.3)
1079 #
1080 # ... more members ...
1081 #
1082 # Since: 0.14
1083 ##
1084 { 'struct': 'BlockStats',
1085   'data': {'*device': 'str', '*node-name': 'str',
1086            ... more members ... } }
1087
1088 ##
1089 # @query-blockstats:
1090 #
1091 # Query the @BlockStats for all virtual block devices.
1092 #
1093 # @query-nodes: If true, the command will query all the block nodes
1094 #     ... explain, explain ...
1095 #     (Since 2.3)
1096 #
1097 # Returns: A list of @BlockStats for each virtual block devices.
1098 #
1099 # Since: 0.14
1100 #
1101 # .. qmp-example::
1102 #
1103 #     -> { "execute": "query-blockstats" }
1104 #     <- {
1105 #          ...
1106 #        }
1107 ##
1108 { 'command': 'query-blockstats',
1109   'data': { '*query-nodes': 'bool' },
1110   'returns': ['BlockStats'] }
1111
1112
1113Markup pitfalls
1114~~~~~~~~~~~~~~~
1115
1116A blank line is required between list items and paragraphs.  Without
1117it, the list may not be recognized, resulting in garbled output.  Good
1118example::
1119
1120 # An event's state is modified if:
1121 #
1122 # - its name matches the @name pattern, and
1123 # - if @vcpu is given, the event has the "vcpu" property.
1124
1125Without the blank line this would be a single paragraph.
1126
1127Indentation matters.  Bad example::
1128
1129 # @none: None (no memory side cache in this proximity domain,
1130 #              or cache associativity unknown)
1131 #     (since 5.0)
1132
1133The last line's de-indent is wrong.  The second and subsequent lines
1134need to line up with each other, like this::
1135
1136 # @none: None (no memory side cache in this proximity domain,
1137 #     or cache associativity unknown)
1138 #     (since 5.0)
1139
1140Section tags are case-sensitive and end with a colon.  They are only
1141recognized after a blank line.  Good example::
1142
1143 #
1144 # Since: 7.1
1145
1146Bad examples (all ordinary paragraphs)::
1147
1148 # since: 7.1
1149
1150 # Since 7.1
1151
1152 # Since : 7.1
1153
1154Likewise, member descriptions require a colon.  Good example::
1155
1156 # @interface-id: Interface ID
1157
1158Bad examples (all ordinary paragraphs)::
1159
1160 # @interface-id   Interface ID
1161
1162 # @interface-id : Interface ID
1163
1164Undocumented members are not flagged, yet.  Instead, the generated
1165documentation describes them as "Not documented".  Think twice before
1166adding more undocumented members.
1167
1168When you change documentation comments, please check the generated
1169documentation comes out as intended!
1170
1171
1172Client JSON Protocol introspection
1173==================================
1174
1175Clients of a Client JSON Protocol commonly need to figure out what
1176exactly the server (QEMU) supports.
1177
1178For this purpose, QMP provides introspection via command
1179query-qmp-schema.  QGA currently doesn't support introspection.
1180
1181While Client JSON Protocol wire compatibility should be maintained
1182between qemu versions, we cannot make the same guarantees for
1183introspection stability.  For example, one version of qemu may provide
1184a non-variant optional member of a struct, and a later version rework
1185the member to instead be non-optional and associated with a variant.
1186Likewise, one version of qemu may list a member with open-ended type
1187'str', and a later version could convert it to a finite set of strings
1188via an enum type; or a member may be converted from a specific type to
1189an alternate that represents a choice between the original type and
1190something else.
1191
1192query-qmp-schema returns a JSON array of SchemaInfo objects.  These
1193objects together describe the wire ABI, as defined in the QAPI schema.
1194There is no specified order to the SchemaInfo objects returned; a
1195client must search for a particular name throughout the entire array
1196to learn more about that name, but is at least guaranteed that there
1197will be no collisions between type, command, and event names.
1198
1199However, the SchemaInfo can't reflect all the rules and restrictions
1200that apply to QMP.  It's interface introspection (figuring out what's
1201there), not interface specification.  The specification is in the QAPI
1202schema.  To understand how QMP is to be used, you need to study the
1203QAPI schema.
1204
1205Like any other command, query-qmp-schema is itself defined in the QAPI
1206schema, along with the SchemaInfo type.  This text attempts to give an
1207overview how things work.  For details you need to consult the QAPI
1208schema.
1209
1210SchemaInfo objects have common members "name", "meta-type",
1211"features", and additional variant members depending on the value of
1212meta-type.
1213
1214Each SchemaInfo object describes a wire ABI entity of a certain
1215meta-type: a command, event or one of several kinds of type.
1216
1217SchemaInfo for commands and events have the same name as in the QAPI
1218schema.
1219
1220Command and event names are part of the wire ABI, but type names are
1221not.  Therefore, the SchemaInfo for types have auto-generated
1222meaningless names.  For readability, the examples in this section use
1223meaningful type names instead.
1224
1225Optional member "features" exposes the entity's feature strings as a
1226JSON array of strings.
1227
1228To examine a type, start with a command or event using it, then follow
1229references by name.
1230
1231QAPI schema definitions not reachable that way are omitted.
1232
1233The SchemaInfo for a command has meta-type "command", and variant
1234members "arg-type", "ret-type" and "allow-oob".  On the wire, the
1235"arguments" member of a client's "execute" command must conform to the
1236object type named by "arg-type".  The "return" member that the server
1237passes in a success response conforms to the type named by "ret-type".
1238When "allow-oob" is true, it means the command supports out-of-band
1239execution.  It defaults to false.
1240
1241If the command takes no arguments, "arg-type" names an object type
1242without members.  Likewise, if the command returns nothing, "ret-type"
1243names an object type without members.
1244
1245Example: the SchemaInfo for command query-qmp-schema ::
1246
1247 { "name": "query-qmp-schema", "meta-type": "command",
1248   "arg-type": "q_empty", "ret-type": "SchemaInfoList" }
1249
1250   Type "q_empty" is an automatic object type without members, and type
1251   "SchemaInfoList" is the array of SchemaInfo type.
1252
1253The SchemaInfo for an event has meta-type "event", and variant member
1254"arg-type".  On the wire, a "data" member that the server passes in an
1255event conforms to the object type named by "arg-type".
1256
1257If the event carries no additional information, "arg-type" names an
1258object type without members.  The event may not have a data member on
1259the wire then.
1260
1261Each command or event defined with 'data' as MEMBERS object in the
1262QAPI schema implicitly defines an object type.
1263
1264Example: the SchemaInfo for EVENT_C from section Events_ ::
1265
1266    { "name": "EVENT_C", "meta-type": "event",
1267      "arg-type": "q_obj-EVENT_C-arg" }
1268
1269    Type "q_obj-EVENT_C-arg" is an implicitly defined object type with
1270    the two members from the event's definition.
1271
1272The SchemaInfo for struct and union types has meta-type "object" and
1273variant member "members".
1274
1275The SchemaInfo for a union type additionally has variant members "tag"
1276and "variants".
1277
1278"members" is a JSON array describing the object's common members, if
1279any.  Each element is a JSON object with members "name" (the member's
1280name), "type" (the name of its type), "features" (a JSON array of
1281feature strings), and "default".  The latter two are optional.  The
1282member is optional if "default" is present.  Currently, "default" can
1283only have value null.  Other values are reserved for future
1284extensions.  The "members" array is in no particular order; clients
1285must search the entire object when learning whether a particular
1286member is supported.
1287
1288Example: the SchemaInfo for MyType from section `Struct types`_ ::
1289
1290    { "name": "MyType", "meta-type": "object",
1291      "members": [
1292          { "name": "member1", "type": "str" },
1293          { "name": "member2", "type": "int" },
1294          { "name": "member3", "type": "str", "default": null } ] }
1295
1296"features" exposes the command's feature strings as a JSON array of
1297strings.
1298
1299Example: the SchemaInfo for TestType from section Features_::
1300
1301    { "name": "TestType", "meta-type": "object",
1302      "members": [
1303          { "name": "number", "type": "int" } ],
1304      "features": ["allow-negative-numbers"] }
1305
1306"tag" is the name of the common member serving as type tag.
1307"variants" is a JSON array describing the object's variant members.
1308Each element is a JSON object with members "case" (the value of type
1309tag this element applies to) and "type" (the name of an object type
1310that provides the variant members for this type tag value).  The
1311"variants" array is in no particular order, and is not guaranteed to
1312list cases in the same order as the corresponding "tag" enum type.
1313
1314Example: the SchemaInfo for union BlockdevOptions from section
1315`Union types`_ ::
1316
1317    { "name": "BlockdevOptions", "meta-type": "object",
1318      "members": [
1319          { "name": "driver", "type": "BlockdevDriver" },
1320          { "name": "read-only", "type": "bool", "default": null } ],
1321      "tag": "driver",
1322      "variants": [
1323          { "case": "file", "type": "BlockdevOptionsFile" },
1324          { "case": "qcow2", "type": "BlockdevOptionsQcow2" } ] }
1325
1326Note that base types are "flattened": its members are included in the
1327"members" array.
1328
1329The SchemaInfo for an alternate type has meta-type "alternate", and
1330variant member "members".  "members" is a JSON array.  Each element is
1331a JSON object with member "type", which names a type.  Values of the
1332alternate type conform to exactly one of its member types.  There is
1333no guarantee on the order in which "members" will be listed.
1334
1335Example: the SchemaInfo for BlockdevRef from section `Alternate types`_ ::
1336
1337    { "name": "BlockdevRef", "meta-type": "alternate",
1338      "members": [
1339          { "type": "BlockdevOptions" },
1340          { "type": "str" } ] }
1341
1342The SchemaInfo for an array type has meta-type "array", and variant
1343member "element-type", which names the array's element type.  Array
1344types are implicitly defined.  For convenience, the array's name may
1345resemble the element type; however, clients should examine member
1346"element-type" instead of making assumptions based on parsing member
1347"name".
1348
1349Example: the SchemaInfo for ['str'] ::
1350
1351    { "name": "[str]", "meta-type": "array",
1352      "element-type": "str" }
1353
1354The SchemaInfo for an enumeration type has meta-type "enum" and
1355variant member "members".
1356
1357"members" is a JSON array describing the enumeration values.  Each
1358element is a JSON object with member "name" (the member's name), and
1359optionally "features" (a JSON array of feature strings).  The
1360"members" array is in no particular order; clients must search the
1361entire array when learning whether a particular value is supported.
1362
1363Example: the SchemaInfo for MyEnum from section `Enumeration types`_ ::
1364
1365    { "name": "MyEnum", "meta-type": "enum",
1366      "members": [
1367        { "name": "value1" },
1368        { "name": "value2" },
1369        { "name": "value3" }
1370      ] }
1371
1372The SchemaInfo for a built-in type has the same name as the type in
1373the QAPI schema (see section `Built-in Types`_), with one exception
1374detailed below.  It has variant member "json-type" that shows how
1375values of this type are encoded on the wire.
1376
1377Example: the SchemaInfo for str ::
1378
1379    { "name": "str", "meta-type": "builtin", "json-type": "string" }
1380
1381The QAPI schema supports a number of integer types that only differ in
1382how they map to C.  They are identical as far as SchemaInfo is
1383concerned.  Therefore, they get all mapped to a single type "int" in
1384SchemaInfo.
1385
1386As explained above, type names are not part of the wire ABI.  Not even
1387the names of built-in types.  Clients should examine member
1388"json-type" instead of hard-coding names of built-in types.
1389
1390
1391Compatibility considerations
1392============================
1393
1394Maintaining backward compatibility at the Client JSON Protocol level
1395while evolving the schema requires some care.  This section is about
1396syntactic compatibility, which is necessary, but not sufficient, for
1397actual compatibility.
1398
1399Clients send commands with argument data, and receive command
1400responses with return data and events with event data.
1401
1402Adding opt-in functionality to the send direction is backwards
1403compatible: adding commands, optional arguments, enumeration values,
1404union and alternate branches; turning an argument type into an
1405alternate of that type; making mandatory arguments optional.  Clients
1406oblivious of the new functionality continue to work.
1407
1408Incompatible changes include removing commands, command arguments,
1409enumeration values, union and alternate branches, adding mandatory
1410command arguments, and making optional arguments mandatory.
1411
1412The specified behavior of an absent optional argument should remain
1413the same.  With proper documentation, this policy still allows some
1414flexibility; for example, when an optional 'buffer-size' argument is
1415specified to default to a sensible buffer size, the actual default
1416value can still be changed.  The specified default behavior is not the
1417exact size of the buffer, only that the default size is sensible.
1418
1419Adding functionality to the receive direction is generally backwards
1420compatible: adding events, adding return and event data members.
1421Clients are expected to ignore the ones they don't know.
1422
1423Removing "unreachable" stuff like events that can't be triggered
1424anymore, optional return or event data members that can't be sent
1425anymore, and return or event data member (enumeration) values that
1426can't be sent anymore makes no difference to clients, except for
1427introspection.  The latter can conceivably confuse clients, so tread
1428carefully.
1429
1430Incompatible changes include removing return and event data members.
1431
1432Any change to a command definition's 'data' or one of the types used
1433there (recursively) needs to consider send direction compatibility.
1434
1435Any change to a command definition's 'return', an event definition's
1436'data', or one of the types used there (recursively) needs to consider
1437receive direction compatibility.
1438
1439Any change to types used in both contexts need to consider both.
1440
1441Enumeration type values and complex and alternate type members may be
1442reordered freely.  For enumerations and alternate types, this doesn't
1443affect the wire encoding.  For complex types, this might make the
1444implementation emit JSON object members in a different order, which
1445the Client JSON Protocol permits.
1446
1447Since type names are not visible in the Client JSON Protocol, types
1448may be freely renamed.  Even certain refactorings are invisible, such
1449as splitting members from one type into a common base type.
1450
1451
1452Code generation
1453===============
1454
1455The QAPI code generator qapi-gen.py generates code and documentation
1456from the schema.  Together with the core QAPI libraries, this code
1457provides everything required to take JSON commands read in by a Client
1458JSON Protocol server, unmarshal the arguments into the underlying C
1459types, call into the corresponding C function, map the response back
1460to a Client JSON Protocol response to be returned to the user, and
1461introspect the commands.
1462
1463As an example, we'll use the following schema, which describes a
1464single complex user-defined type, along with command which takes a
1465list of that type as a parameter, and returns a single element of that
1466type.  The user is responsible for writing the implementation of
1467qmp_my_command(); everything else is produced by the generator. ::
1468
1469    $ cat example-schema.json
1470    { 'struct': 'UserDefOne',
1471      'data': { 'integer': 'int', '*string': 'str', '*flag': 'bool' } }
1472
1473    { 'command': 'my-command',
1474      'data': { 'arg1': ['UserDefOne'] },
1475      'returns': 'UserDefOne' }
1476
1477    { 'event': 'MY_EVENT' }
1478
1479We run qapi-gen.py like this::
1480
1481    $ python scripts/qapi-gen.py --output-dir="qapi-generated" \
1482    --prefix="example-" example-schema.json
1483
1484For a more thorough look at generated code, the testsuite includes
1485tests/qapi-schema/qapi-schema-tests.json that covers more examples of
1486what the generator will accept, and compiles the resulting C code as
1487part of 'make check-unit'.
1488
1489
1490Code generated for QAPI types
1491-----------------------------
1492
1493The following files are created:
1494
1495 ``$(prefix)qapi-types.h``
1496     C types corresponding to types defined in the schema
1497
1498 ``$(prefix)qapi-types.c``
1499     Cleanup functions for the above C types
1500
1501The $(prefix) is an optional parameter used as a namespace to keep the
1502generated code from one schema/code-generation separated from others so code
1503can be generated/used from multiple schemas without clobbering previously
1504created code.
1505
1506Example::
1507
1508    $ cat qapi-generated/example-qapi-types.h
1509    [Uninteresting stuff omitted...]
1510
1511    #ifndef EXAMPLE_QAPI_TYPES_H
1512    #define EXAMPLE_QAPI_TYPES_H
1513
1514    #include "qapi/qapi-builtin-types.h"
1515
1516    typedef struct UserDefOne UserDefOne;
1517
1518    typedef struct UserDefOneList UserDefOneList;
1519
1520    typedef struct q_obj_my_command_arg q_obj_my_command_arg;
1521
1522    struct UserDefOne {
1523        int64_t integer;
1524        char *string;
1525        bool has_flag;
1526        bool flag;
1527    };
1528
1529    void qapi_free_UserDefOne(UserDefOne *obj);
1530    G_DEFINE_AUTOPTR_CLEANUP_FUNC(UserDefOne, qapi_free_UserDefOne)
1531
1532    struct UserDefOneList {
1533        UserDefOneList *next;
1534        UserDefOne *value;
1535    };
1536
1537    void qapi_free_UserDefOneList(UserDefOneList *obj);
1538    G_DEFINE_AUTOPTR_CLEANUP_FUNC(UserDefOneList, qapi_free_UserDefOneList)
1539
1540    struct q_obj_my_command_arg {
1541        UserDefOneList *arg1;
1542    };
1543
1544    #endif /* EXAMPLE_QAPI_TYPES_H */
1545    $ cat qapi-generated/example-qapi-types.c
1546    [Uninteresting stuff omitted...]
1547
1548    void qapi_free_UserDefOne(UserDefOne *obj)
1549    {
1550        Visitor *v;
1551
1552        if (!obj) {
1553            return;
1554        }
1555
1556        v = qapi_dealloc_visitor_new();
1557        visit_type_UserDefOne(v, NULL, &obj, NULL);
1558        visit_free(v);
1559    }
1560
1561    void qapi_free_UserDefOneList(UserDefOneList *obj)
1562    {
1563        Visitor *v;
1564
1565        if (!obj) {
1566            return;
1567        }
1568
1569        v = qapi_dealloc_visitor_new();
1570        visit_type_UserDefOneList(v, NULL, &obj, NULL);
1571        visit_free(v);
1572    }
1573
1574    [Uninteresting stuff omitted...]
1575
1576For a modular QAPI schema (see section `Include directives`_), code for
1577each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
1578
1579 SUBDIR/$(prefix)qapi-types-SUBMODULE.h
1580 SUBDIR/$(prefix)qapi-types-SUBMODULE.c
1581
1582If qapi-gen.py is run with option --builtins, additional files are
1583created:
1584
1585 ``qapi-builtin-types.h``
1586     C types corresponding to built-in types
1587
1588 ``qapi-builtin-types.c``
1589     Cleanup functions for the above C types
1590
1591
1592Code generated for visiting QAPI types
1593--------------------------------------
1594
1595These are the visitor functions used to walk through and convert
1596between a native QAPI C data structure and some other format (such as
1597QObject); the generated functions are named visit_type_FOO() and
1598visit_type_FOO_members().
1599
1600The following files are generated:
1601
1602 ``$(prefix)qapi-visit.c``
1603     Visitor function for a particular C type, used to automagically
1604     convert QObjects into the corresponding C type and vice-versa, as
1605     well as for deallocating memory for an existing C type
1606
1607 ``$(prefix)qapi-visit.h``
1608     Declarations for previously mentioned visitor functions
1609
1610Example::
1611
1612    $ cat qapi-generated/example-qapi-visit.h
1613    [Uninteresting stuff omitted...]
1614
1615    #ifndef EXAMPLE_QAPI_VISIT_H
1616    #define EXAMPLE_QAPI_VISIT_H
1617
1618    #include "qapi/qapi-builtin-visit.h"
1619    #include "example-qapi-types.h"
1620
1621
1622    bool visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error **errp);
1623
1624    bool visit_type_UserDefOne(Visitor *v, const char *name,
1625                     UserDefOne **obj, Error **errp);
1626
1627    bool visit_type_UserDefOneList(Visitor *v, const char *name,
1628                     UserDefOneList **obj, Error **errp);
1629
1630    bool visit_type_q_obj_my_command_arg_members(Visitor *v, q_obj_my_command_arg *obj, Error **errp);
1631
1632    #endif /* EXAMPLE_QAPI_VISIT_H */
1633    $ cat qapi-generated/example-qapi-visit.c
1634    [Uninteresting stuff omitted...]
1635
1636    bool visit_type_UserDefOne_members(Visitor *v, UserDefOne *obj, Error **errp)
1637    {
1638        bool has_string = !!obj->string;
1639
1640        if (!visit_type_int(v, "integer", &obj->integer, errp)) {
1641            return false;
1642        }
1643        if (visit_optional(v, "string", &has_string)) {
1644            if (!visit_type_str(v, "string", &obj->string, errp)) {
1645                return false;
1646            }
1647        }
1648        if (visit_optional(v, "flag", &obj->has_flag)) {
1649            if (!visit_type_bool(v, "flag", &obj->flag, errp)) {
1650                return false;
1651            }
1652        }
1653        return true;
1654    }
1655
1656    bool visit_type_UserDefOne(Visitor *v, const char *name,
1657                     UserDefOne **obj, Error **errp)
1658    {
1659        bool ok = false;
1660
1661        if (!visit_start_struct(v, name, (void **)obj, sizeof(UserDefOne), errp)) {
1662            return false;
1663        }
1664        if (!*obj) {
1665            /* incomplete */
1666            assert(visit_is_dealloc(v));
1667            ok = true;
1668            goto out_obj;
1669        }
1670        if (!visit_type_UserDefOne_members(v, *obj, errp)) {
1671            goto out_obj;
1672        }
1673        ok = visit_check_struct(v, errp);
1674    out_obj:
1675        visit_end_struct(v, (void **)obj);
1676        if (!ok && visit_is_input(v)) {
1677            qapi_free_UserDefOne(*obj);
1678            *obj = NULL;
1679        }
1680        return ok;
1681    }
1682
1683    bool visit_type_UserDefOneList(Visitor *v, const char *name,
1684                     UserDefOneList **obj, Error **errp)
1685    {
1686        bool ok = false;
1687        UserDefOneList *tail;
1688        size_t size = sizeof(**obj);
1689
1690        if (!visit_start_list(v, name, (GenericList **)obj, size, errp)) {
1691            return false;
1692        }
1693
1694        for (tail = *obj; tail;
1695             tail = (UserDefOneList *)visit_next_list(v, (GenericList *)tail, size)) {
1696            if (!visit_type_UserDefOne(v, NULL, &tail->value, errp)) {
1697                goto out_obj;
1698            }
1699        }
1700
1701        ok = visit_check_list(v, errp);
1702    out_obj:
1703        visit_end_list(v, (void **)obj);
1704        if (!ok && visit_is_input(v)) {
1705            qapi_free_UserDefOneList(*obj);
1706            *obj = NULL;
1707        }
1708        return ok;
1709    }
1710
1711    bool visit_type_q_obj_my_command_arg_members(Visitor *v, q_obj_my_command_arg *obj, Error **errp)
1712    {
1713        if (!visit_type_UserDefOneList(v, "arg1", &obj->arg1, errp)) {
1714            return false;
1715        }
1716        return true;
1717    }
1718
1719    [Uninteresting stuff omitted...]
1720
1721For a modular QAPI schema (see section `Include directives`_), code for
1722each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
1723
1724 SUBDIR/$(prefix)qapi-visit-SUBMODULE.h
1725 SUBDIR/$(prefix)qapi-visit-SUBMODULE.c
1726
1727If qapi-gen.py is run with option --builtins, additional files are
1728created:
1729
1730 ``qapi-builtin-visit.h``
1731     Visitor functions for built-in types
1732
1733 ``qapi-builtin-visit.c``
1734     Declarations for these visitor functions
1735
1736
1737Code generated for commands
1738---------------------------
1739
1740These are the marshaling/dispatch functions for the commands defined
1741in the schema.  The generated code provides qmp_marshal_COMMAND(), and
1742declares qmp_COMMAND() that the user must implement.
1743
1744The following files are generated:
1745
1746 ``$(prefix)qapi-commands.c``
1747     Command marshal/dispatch functions for each QMP command defined in
1748     the schema
1749
1750 ``$(prefix)qapi-commands.h``
1751     Function prototypes for the QMP commands specified in the schema
1752
1753 ``$(prefix)qapi-commands.trace-events``
1754     Trace event declarations, see :ref:`tracing`.
1755
1756 ``$(prefix)qapi-init-commands.h``
1757     Command initialization prototype
1758
1759 ``$(prefix)qapi-init-commands.c``
1760     Command initialization code
1761
1762Example::
1763
1764    $ cat qapi-generated/example-qapi-commands.h
1765    [Uninteresting stuff omitted...]
1766
1767    #ifndef EXAMPLE_QAPI_COMMANDS_H
1768    #define EXAMPLE_QAPI_COMMANDS_H
1769
1770    #include "example-qapi-types.h"
1771
1772    UserDefOne *qmp_my_command(UserDefOneList *arg1, Error **errp);
1773    void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp);
1774
1775    #endif /* EXAMPLE_QAPI_COMMANDS_H */
1776
1777    $ cat qapi-generated/example-qapi-commands.trace-events
1778    # AUTOMATICALLY GENERATED, DO NOT MODIFY
1779
1780    qmp_enter_my_command(const char *json) "%s"
1781    qmp_exit_my_command(const char *result, bool succeeded) "%s %d"
1782
1783    $ cat qapi-generated/example-qapi-commands.c
1784    [Uninteresting stuff omitted...]
1785
1786    static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in,
1787                                    QObject **ret_out, Error **errp)
1788    {
1789        Visitor *v;
1790
1791        v = qobject_output_visitor_new_qmp(ret_out);
1792        if (visit_type_UserDefOne(v, "unused", &ret_in, errp)) {
1793            visit_complete(v, ret_out);
1794        }
1795        visit_free(v);
1796        v = qapi_dealloc_visitor_new();
1797        visit_type_UserDefOne(v, "unused", &ret_in, NULL);
1798        visit_free(v);
1799    }
1800
1801    void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp)
1802    {
1803        Error *err = NULL;
1804        bool ok = false;
1805        Visitor *v;
1806        UserDefOne *retval;
1807        q_obj_my_command_arg arg = {0};
1808
1809        v = qobject_input_visitor_new_qmp(QOBJECT(args));
1810        if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
1811            goto out;
1812        }
1813        if (visit_type_q_obj_my_command_arg_members(v, &arg, errp)) {
1814            ok = visit_check_struct(v, errp);
1815        }
1816        visit_end_struct(v, NULL);
1817        if (!ok) {
1818            goto out;
1819        }
1820
1821        if (trace_event_get_state_backends(TRACE_QMP_ENTER_MY_COMMAND)) {
1822            g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
1823
1824            trace_qmp_enter_my_command(req_json->str);
1825        }
1826
1827        retval = qmp_my_command(arg.arg1, &err);
1828        if (err) {
1829            trace_qmp_exit_my_command(error_get_pretty(err), false);
1830            error_propagate(errp, err);
1831            goto out;
1832        }
1833
1834        qmp_marshal_output_UserDefOne(retval, ret, errp);
1835
1836        if (trace_event_get_state_backends(TRACE_QMP_EXIT_MY_COMMAND)) {
1837            g_autoptr(GString) ret_json = qobject_to_json(*ret);
1838
1839            trace_qmp_exit_my_command(ret_json->str, true);
1840        }
1841
1842    out:
1843        visit_free(v);
1844        v = qapi_dealloc_visitor_new();
1845        visit_start_struct(v, NULL, NULL, 0, NULL);
1846        visit_type_q_obj_my_command_arg_members(v, &arg, NULL);
1847        visit_end_struct(v, NULL);
1848        visit_free(v);
1849    }
1850
1851    [Uninteresting stuff omitted...]
1852    $ cat qapi-generated/example-qapi-init-commands.h
1853    [Uninteresting stuff omitted...]
1854    #ifndef EXAMPLE_QAPI_INIT_COMMANDS_H
1855    #define EXAMPLE_QAPI_INIT_COMMANDS_H
1856
1857    #include "qapi/qmp/dispatch.h"
1858
1859    void example_qmp_init_marshal(QmpCommandList *cmds);
1860
1861    #endif /* EXAMPLE_QAPI_INIT_COMMANDS_H */
1862    $ cat qapi-generated/example-qapi-init-commands.c
1863    [Uninteresting stuff omitted...]
1864    void example_qmp_init_marshal(QmpCommandList *cmds)
1865    {
1866        QTAILQ_INIT(cmds);
1867
1868        qmp_register_command(cmds, "my-command",
1869                             qmp_marshal_my_command, 0, 0);
1870    }
1871    [Uninteresting stuff omitted...]
1872
1873For a modular QAPI schema (see section `Include directives`_), code for
1874each sub-module SUBDIR/SUBMODULE.json is actually generated into::
1875
1876 SUBDIR/$(prefix)qapi-commands-SUBMODULE.h
1877 SUBDIR/$(prefix)qapi-commands-SUBMODULE.c
1878
1879
1880Code generated for events
1881-------------------------
1882
1883This is the code related to events defined in the schema, providing
1884qapi_event_send_EVENT().
1885
1886The following files are created:
1887
1888 ``$(prefix)qapi-events.h``
1889     Function prototypes for each event type
1890
1891 ``$(prefix)qapi-events.c``
1892     Implementation of functions to send an event
1893
1894 ``$(prefix)qapi-emit-events.h``
1895     Enumeration of all event names, and common event code declarations
1896
1897 ``$(prefix)qapi-emit-events.c``
1898     Common event code definitions
1899
1900Example::
1901
1902    $ cat qapi-generated/example-qapi-events.h
1903    [Uninteresting stuff omitted...]
1904
1905    #ifndef EXAMPLE_QAPI_EVENTS_H
1906    #define EXAMPLE_QAPI_EVENTS_H
1907
1908    #include "qapi/util.h"
1909    #include "example-qapi-types.h"
1910
1911    void qapi_event_send_my_event(void);
1912
1913    #endif /* EXAMPLE_QAPI_EVENTS_H */
1914    $ cat qapi-generated/example-qapi-events.c
1915    [Uninteresting stuff omitted...]
1916
1917    void qapi_event_send_my_event(void)
1918    {
1919        QDict *qmp;
1920
1921        qmp = qmp_event_build_dict("MY_EVENT");
1922
1923        example_qapi_event_emit(EXAMPLE_QAPI_EVENT_MY_EVENT, qmp);
1924
1925        qobject_unref(qmp);
1926    }
1927
1928    [Uninteresting stuff omitted...]
1929    $ cat qapi-generated/example-qapi-emit-events.h
1930    [Uninteresting stuff omitted...]
1931
1932    #ifndef EXAMPLE_QAPI_EMIT_EVENTS_H
1933    #define EXAMPLE_QAPI_EMIT_EVENTS_H
1934
1935    #include "qapi/util.h"
1936
1937    typedef enum example_QAPIEvent {
1938        EXAMPLE_QAPI_EVENT_MY_EVENT,
1939        EXAMPLE_QAPI_EVENT__MAX,
1940    } example_QAPIEvent;
1941
1942    #define example_QAPIEvent_str(val) \
1943        qapi_enum_lookup(&example_QAPIEvent_lookup, (val))
1944
1945    extern const QEnumLookup example_QAPIEvent_lookup;
1946
1947    void example_qapi_event_emit(example_QAPIEvent event, QDict *qdict);
1948
1949    #endif /* EXAMPLE_QAPI_EMIT_EVENTS_H */
1950    $ cat qapi-generated/example-qapi-emit-events.c
1951    [Uninteresting stuff omitted...]
1952
1953    const QEnumLookup example_QAPIEvent_lookup = {
1954        .array = (const char *const[]) {
1955            [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT",
1956        },
1957        .size = EXAMPLE_QAPI_EVENT__MAX
1958    };
1959
1960    [Uninteresting stuff omitted...]
1961
1962For a modular QAPI schema (see section `Include directives`_), code for
1963each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
1964
1965 SUBDIR/$(prefix)qapi-events-SUBMODULE.h
1966 SUBDIR/$(prefix)qapi-events-SUBMODULE.c
1967
1968
1969Code generated for introspection
1970--------------------------------
1971
1972The following files are created:
1973
1974 ``$(prefix)qapi-introspect.c``
1975     Defines a string holding a JSON description of the schema
1976
1977 ``$(prefix)qapi-introspect.h``
1978     Declares the above string
1979
1980Example::
1981
1982    $ cat qapi-generated/example-qapi-introspect.h
1983    [Uninteresting stuff omitted...]
1984
1985    #ifndef EXAMPLE_QAPI_INTROSPECT_H
1986    #define EXAMPLE_QAPI_INTROSPECT_H
1987
1988    #include "qapi/qmp/qlit.h"
1989
1990    extern const QLitObject example_qmp_schema_qlit;
1991
1992    #endif /* EXAMPLE_QAPI_INTROSPECT_H */
1993    $ cat qapi-generated/example-qapi-introspect.c
1994    [Uninteresting stuff omitted...]
1995
1996    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
1997        QLIT_QDICT(((QLitDictEntry[]) {
1998            { "arg-type", QLIT_QSTR("0"), },
1999            { "meta-type", QLIT_QSTR("command"), },
2000            { "name", QLIT_QSTR("my-command"), },
2001            { "ret-type", QLIT_QSTR("1"), },
2002            {}
2003        })),
2004        QLIT_QDICT(((QLitDictEntry[]) {
2005            { "arg-type", QLIT_QSTR("2"), },
2006            { "meta-type", QLIT_QSTR("event"), },
2007            { "name", QLIT_QSTR("MY_EVENT"), },
2008            {}
2009        })),
2010        /* "0" = q_obj_my-command-arg */
2011        QLIT_QDICT(((QLitDictEntry[]) {
2012            { "members", QLIT_QLIST(((QLitObject[]) {
2013                QLIT_QDICT(((QLitDictEntry[]) {
2014                    { "name", QLIT_QSTR("arg1"), },
2015                    { "type", QLIT_QSTR("[1]"), },
2016                    {}
2017                })),
2018                {}
2019            })), },
2020            { "meta-type", QLIT_QSTR("object"), },
2021            { "name", QLIT_QSTR("0"), },
2022            {}
2023        })),
2024        /* "1" = UserDefOne */
2025        QLIT_QDICT(((QLitDictEntry[]) {
2026            { "members", QLIT_QLIST(((QLitObject[]) {
2027                QLIT_QDICT(((QLitDictEntry[]) {
2028                    { "name", QLIT_QSTR("integer"), },
2029                    { "type", QLIT_QSTR("int"), },
2030                    {}
2031                })),
2032                QLIT_QDICT(((QLitDictEntry[]) {
2033                    { "default", QLIT_QNULL, },
2034                    { "name", QLIT_QSTR("string"), },
2035                    { "type", QLIT_QSTR("str"), },
2036                    {}
2037                })),
2038                QLIT_QDICT(((QLitDictEntry[]) {
2039                    { "default", QLIT_QNULL, },
2040                    { "name", QLIT_QSTR("flag"), },
2041                    { "type", QLIT_QSTR("bool"), },
2042                    {}
2043                })),
2044                {}
2045            })), },
2046            { "meta-type", QLIT_QSTR("object"), },
2047            { "name", QLIT_QSTR("1"), },
2048            {}
2049        })),
2050        /* "2" = q_empty */
2051        QLIT_QDICT(((QLitDictEntry[]) {
2052            { "members", QLIT_QLIST(((QLitObject[]) {
2053                {}
2054            })), },
2055            { "meta-type", QLIT_QSTR("object"), },
2056            { "name", QLIT_QSTR("2"), },
2057            {}
2058        })),
2059        QLIT_QDICT(((QLitDictEntry[]) {
2060            { "element-type", QLIT_QSTR("1"), },
2061            { "meta-type", QLIT_QSTR("array"), },
2062            { "name", QLIT_QSTR("[1]"), },
2063            {}
2064        })),
2065        QLIT_QDICT(((QLitDictEntry[]) {
2066            { "json-type", QLIT_QSTR("int"), },
2067            { "meta-type", QLIT_QSTR("builtin"), },
2068            { "name", QLIT_QSTR("int"), },
2069            {}
2070        })),
2071        QLIT_QDICT(((QLitDictEntry[]) {
2072            { "json-type", QLIT_QSTR("string"), },
2073            { "meta-type", QLIT_QSTR("builtin"), },
2074            { "name", QLIT_QSTR("str"), },
2075            {}
2076        })),
2077        QLIT_QDICT(((QLitDictEntry[]) {
2078            { "json-type", QLIT_QSTR("boolean"), },
2079            { "meta-type", QLIT_QSTR("builtin"), },
2080            { "name", QLIT_QSTR("bool"), },
2081            {}
2082        })),
2083        {}
2084    }));
2085
2086    [Uninteresting stuff omitted...]
2087