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