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