1====================== 2The Sphinx QAPI Domain 3====================== 4 5An extension to the `rST syntax 6<https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`_ 7in Sphinx is provided by the QAPI Domain, located in 8``docs/sphinx/qapi_domain.py``. This extension is analogous to the 9`Python Domain 10<https://www.sphinx-doc.org/en/master/usage/domains/python.html>`_ 11included with Sphinx, but provides special directives and roles 12speciically for annotating and documenting QAPI definitions 13specifically. 14 15A `Domain 16<https://www.sphinx-doc.org/en/master/usage/domains/index.html>`_ 17provides a set of special rST directives and cross-referencing roles to 18Sphinx for understanding rST markup written to document a specific 19language. By itself, this QAPI extension is only sufficient to parse rST 20markup written by hand; the `autodoc 21<https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html>`_ 22functionality is provided elsewhere, in ``docs/sphinx/qapidoc.py``, by 23the "Transmogrifier". 24 25It is not expected that any developer nor documentation writer would 26never need to write *nor* read these special rST forms. However, in the 27event that something needs to be debugged, knowing the syntax of the 28domain is quite handy. This reference may also be useful as a guide for 29understanding the QAPI Domain extension code itself. Although most of 30these forms will not be needed for documentation writing purposes, 31understanding the cross-referencing syntax *will* be helpful when 32writing rST documentation elsewhere, or for enriching the body of 33QAPIDoc blocks themselves. 34 35 36Concepts 37======== 38 39The QAPI Domain itself provides no mechanisms for reading the QAPI 40Schema or generating documentation from code that exists. It is merely 41the rST syntax used to describe things. For instance, the Sphinx Python 42domain adds syntax like ``:py:func:`` for describing Python functions in 43documentation, but it's the autodoc module that is responsible for 44reading python code and generating such syntax. QAPI is analagous here: 45qapidoc.py is responsible for reading the QAPI Schema and generating rST 46syntax, and qapi_domain.py is responsible for translating that special 47syntax and providing APIs for Sphinx internals. 48 49In other words: 50 51qapi_domain.py adds syntax like ``.. qapi:command::`` to Sphinx, and 52qapidoc.py transforms the documentation in ``qapi/*.json`` into rST 53using directives defined by the domain. 54 55Or even shorter: 56 57``:py:`` is to ``:qapi:`` as *autodoc* is to *qapidoc*. 58 59 60Info Field Lists 61================ 62 63`Field lists 64<https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#field-lists>`_ 65are a standard syntax in reStructuredText. Sphinx `extends that syntax 66<https://www.sphinx-doc.org/en/master/usage/domains/python.html#info-field-lists>`_ 67to give certain field list entries special meaning and parsing to, for 68example, add cross-references. The QAPI Domain takes advantage of this 69field list extension to document things like Arguments, Members, Values, 70and so on. 71 72The special parsing and handling of info field lists in Sphinx is provided by 73three main classes; Field, GroupedField, and TypedField. The behavior 74and formatting for each configured field list entry in the domain 75changes depending on which class is used. 76 77Field: 78 * Creates an ungrouped field: i.e., each entry will create its own 79 section and they will not be combined. 80 * May *optionally* support an argument. 81 * May apply cross-reference roles to *either* the argument *or* the 82 content body, both, or neither. 83 84This is used primarily for entries which are not expected to be 85repeated, i.e., items that may only show up at most once. The QAPI 86domain uses this class for "Errors" section. 87 88GroupedField: 89 * Creates a grouped field: i.e. multiple adjacent entries will be 90 merged into one section, and the content will form a bulleted list. 91 * *Must* take an argument. 92 * May optionally apply a cross-reference role to the argument, but not 93 the body. 94 * Can be configured to remove the bulleted list if there is only a 95 single entry. 96 * All items will be generated with the form: "argument -- body" 97 98This is used for entries which are expected to be repeated, but aren't 99expected to have two arguments, i.e. types without names, or names 100without types. The QAPI domain uses this class for features, returns, 101and enum values. 102 103TypedField: 104 * Creates a grouped, typed field. Multiple adjacent entres will be 105 merged into one section, and the content will form a bulleted list. 106 * *Must* take at least one argument, but supports up to two - 107 nominally, a name and a type. 108 * May optionally apply a cross-reference role to the type or the name 109 argument, but not the body. 110 * Can be configured to remove the bulleted list if there is only a 111 single entry. 112 * All items will be generated with the form "name (type) -- body" 113 114This is used for entries that are expected to be repeated and will have 115a name, a type, and a description. The QAPI domain uses this class for 116arguments, alternatives, and members. Wherever type names are referenced 117below, They must be a valid, documented type that will be 118cross-referenced in the HTML output; or one of the built-in JSON types 119(string, number, int, boolean, null, value, q_empty). 120 121 122``:feat:`` 123---------- 124 125Document a feature attached to a QAPI definition. 126 127:availability: This field list is available in the body of Command, 128 Event, Enum, Object and Alternate directives. 129:syntax: ``:feat name: Lorem ipsum, dolor sit amet...`` 130:type: `sphinx.util.docfields.GroupedField 131 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.GroupedField.html?private=1>`_ 132 133Example:: 134 135 .. qapi:object:: BlockdevOptionsVirtioBlkVhostVdpa 136 :since: 7.2 137 :ifcond: CONFIG_BLKIO 138 139 Driver specific block device options for the virtio-blk-vhost-vdpa 140 backend. 141 142 :memb string path: path to the vhost-vdpa character device. 143 :feat fdset: Member ``path`` supports the special "/dev/fdset/N" path 144 (since 8.1) 145 146 147``:arg:`` 148--------- 149 150Document an argument to a QAPI command. 151 152:availability: This field list is only available in the body of the 153 Command directive. 154:syntax: ``:arg type name: description`` 155:type: `sphinx.util.docfields.TypedField 156 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.TypedField.html?private=1>`_ 157 158 159Example:: 160 161 .. qapi:command:: job-pause 162 :since: 3.0 163 164 Pause an active job. 165 166 This command returns immediately after marking the active job for 167 pausing. Pausing an already paused job is an error. 168 169 The job will pause as soon as possible, which means transitioning 170 into the PAUSED state if it was RUNNING, or into STANDBY if it was 171 READY. The corresponding JOB_STATUS_CHANGE event will be emitted. 172 173 Cancelling a paused job automatically resumes it. 174 175 :arg string id: The job identifier. 176 177 178``:error:`` 179----------- 180 181Document the error condition(s) of a QAPI command. 182 183:availability: This field list is only available in the body of the 184 Command directive. 185:syntax: ``:error: Lorem ipsum dolor sit amet ...`` 186:type: `sphinx.util.docfields.Field 187 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.Field.html?private=1>`_ 188 189The format of the :errors: field list description is free-form rST. The 190alternative spelling ":errors:" is also permitted, but strictly 191analogous. 192 193Example:: 194 195 .. qapi:command:: block-job-set-speed 196 :since: 1.1 197 198 Set maximum speed for a background block operation. 199 200 This command can only be issued when there is an active block job. 201 202 Throttling can be disabled by setting the speed to 0. 203 204 :arg string device: The job identifier. This used to be a device 205 name (hence the name of the parameter), but since QEMU 2.7 it 206 can have other values. 207 :arg int speed: the maximum speed, in bytes per second, or 0 for 208 unlimited. Defaults to 0. 209 :error: 210 - If no background operation is active on this device, 211 DeviceNotActive 212 213 214``:return:`` 215------------- 216 217Document the return type(s) and value(s) of a QAPI command. 218 219:availability: This field list is only available in the body of the 220 Command directive. 221:syntax: ``:return type: Lorem ipsum dolor sit amet ...`` 222:type: `sphinx.util.docfields.GroupedField 223 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.GroupedField.html?private=1>`_ 224 225 226Example:: 227 228 .. qapi:command:: query-replay 229 :since: 5.2 230 231 Retrieve the record/replay information. It includes current 232 instruction count which may be used for ``replay-break`` and 233 ``replay-seek`` commands. 234 235 :return ReplayInfo: record/replay information. 236 237 .. qmp-example:: 238 239 -> { "execute": "query-replay" } 240 <- { "return": { 241 "mode": "play", "filename": "log.rr", "icount": 220414 } 242 } 243 244 245``:value:`` 246----------- 247 248Document a possible value for a QAPI enum. 249 250:availability: This field list is only available in the body of the Enum 251 directive. 252:syntax: ``:value name: Lorem ipsum, dolor sit amet ...`` 253:type: `sphinx.util.docfields.GroupedField 254 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.GroupedField.html?private=1>`_ 255 256Example:: 257 258 .. qapi:enum:: QapiErrorClass 259 :since: 1.2 260 261 QEMU error classes 262 263 :value GenericError: this is used for errors that don't require a specific 264 error class. This should be the default case for most errors 265 :value CommandNotFound: the requested command has not been found 266 :value DeviceNotActive: a device has failed to be become active 267 :value DeviceNotFound: the requested device has not been found 268 :value KVMMissingCap: the requested operation can't be fulfilled because a 269 required KVM capability is missing 270 271 272``:alt:`` 273------------ 274 275Document a possible branch for a QAPI alternate. 276 277:availability: This field list is only available in the body of the 278 Alternate directive. 279:syntax: ``:alt type name: Lorem ipsum, dolor sit amet ...`` 280:type: `sphinx.util.docfields.TypedField 281 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.TypedField.html?private=1>`_ 282 283As a limitation of Sphinx, we must document the "name" of the branch in 284addition to the type, even though this information is not visible on the 285wire in the QMP protocol format. This limitation *may* be lifted at a 286future date. 287 288Example:: 289 290 .. qapi:alternate:: StrOrNull 291 :since: 2.10 292 293 This is a string value or the explicit lack of a string (null 294 pointer in C). Intended for cases when 'optional absent' already 295 has a different meaning. 296 297 :alt string s: the string value 298 :alt null n: no string value 299 300 301``:memb:`` 302---------- 303 304Document a member of an Event or Object. 305 306:availability: This field list is available in the body of Event or 307 Object directives. 308:syntax: ``:memb type name: Lorem ipsum, dolor sit amet ...`` 309:type: `sphinx.util.docfields.TypedField 310 <https://pydoc.dev/sphinx/latest/sphinx.util.docfields.TypedField.html?private=1>`_ 311 312This is fundamentally the same as ``:arg:`` and ``:alt:``, but uses the 313"Members" phrasing for Events and Objects (Structs and Unions). 314 315Example:: 316 317 .. qapi:event:: JOB_STATUS_CHANGE 318 :since: 3.0 319 320 Emitted when a job transitions to a different status. 321 322 :memb string id: The job identifier 323 :memb JobStatus status: The new job status 324 325 326Arbitrary field lists 327--------------------- 328 329Other field list names, while valid rST syntax, are prohibited inside of 330QAPI directives to help prevent accidental misspellings of info field 331list names. If you want to add a new arbitrary "non-value-added" field 332list to QAPI documentation, you must add the field name to the allow 333list in ``docs/conf.py`` 334 335For example:: 336 337 qapi_allowed_fields = { 338 "see also", 339 } 340 341Will allow you to add arbitrary field lists in QAPI directives:: 342 343 .. qapi:command:: x-fake-command 344 345 :see also: Lorem ipsum, dolor sit amet ... 346 347 348Cross-references 349================ 350 351Cross-reference `roles 352<https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html>`_ 353in the QAPI domain are modeled closely after the `Python 354cross-referencing syntax 355<https://www.sphinx-doc.org/en/master/usage/domains/python.html#cross-referencing-python-objects>`_. 356 357QAPI definitions can be referenced using the standard `any 358<https://www.sphinx-doc.org/en/master/usage/referencing.html#role-any>`_ 359role cross-reference syntax, such as with ```query-blockstats```. In 360the event that disambiguation is needed, cross-references can also be 361written using a number of explicit cross-reference roles: 362 363* ``:qapi:mod:`block-core``` -- Reference a QAPI module. The link will 364 take you to the beginning of that section in the documentation. 365* ``:qapi:cmd:`query-block``` -- Reference a QAPI command. 366* ``:qapi:event:`JOB_STATUS_CHANGE``` -- Reference a QAPI event. 367* ``:qapi:enum:`QapiErrorClass``` -- Reference a QAPI enum. 368* ``:qapi:obj:`BlockdevOptionsVirtioBlkVhostVdpa`` -- Reference a QAPI 369 object (struct or union) 370* ``:qapi:alt:`StrOrNull``` -- Reference a QAPI alternate. 371* ``:qapi:type:`BlockDirtyInfo``` -- Reference *any* QAPI type; this 372 excludes modules, commands, and events. 373* ``:qapi:any:`block-job-set-speed``` -- Reference absolutely any QAPI entity. 374 375Type arguments in info field lists are converted into references as if 376you had used the ``:qapi:type:`` role. All of the special syntax below 377applies to both info field lists and standalone explicit 378cross-references. 379 380 381Type decorations 382---------------- 383 384Type names in references can be surrounded by brackets, like 385``[typename]``, to indicate an array of that type. The cross-reference 386will apply only to the type name between the brackets. For example; 387``:qapi:type:`[Qcow2BitmapInfoFlags]``` renders to: 388:qapi:type:`[Qcow2BitmapInfoFlags]` 389 390To indicate an optional argument/member in a field list, the type name 391can be suffixed with ``?``. The cross-reference will be transformed to 392"type, Optional" with the link applying only to the type name. For 393example; ``:qapi:type:`BitmapSyncMode?``` renders to: 394:qapi:type:`BitmapSyncMode?` 395 396 397Namespaces 398---------- 399 400Mimicking the `Python domain target specification syntax 401<https://www.sphinx-doc.org/en/master/usage/domains/python.html#target-specification>`_, 402QAPI allows you to specify the fully qualified path for a data 403type. QAPI enforces globally unique names, so it's unlikely you'll need 404this specific feature, but it may be extended in the near future to 405allow referencing identically named commands and data types from 406different utilities; i.e. QEMU Storage Daemon vs QMP. 407 408* A module can be explicitly provided; 409 ``:qapi:type:`block-core.BitmapSyncMode``` will render to: 410 :qapi:type:`block-core.BitmapSyncMode` 411* If you don't want to display the "fully qualified" name, it can be 412 prefixed with a tilde; ``:qapi:type:`~block-core.BitmapSyncMode``` 413 will render to: :qapi:type:`~block-core.BitmapSyncMode` 414 415 416Custom link text 417---------------- 418 419The name of a cross-reference link can be explicitly overridden like 420`most stock Sphinx references 421<https://www.sphinx-doc.org/en/master/usage/referencing.html#syntax>`_ 422using the ``custom text <target>`` syntax. 423 424For example, ``:qapi:cmd:`Merge dirty bitmaps 425<block-dirty-bitmap-merge>``` will render as: :qapi:cmd:`Merge dirty 426bitmaps <block-dirty-bitmap-merge>` 427 428 429Directives 430========== 431 432The QAPI domain adds a number of custom directives for documenting 433various QAPI/QMP entities. The syntax is plain rST, and follows this 434general format:: 435 436 .. qapi:directive:: argument 437 :option: 438 :another-option: with an argument 439 440 Content body, arbitrary rST is allowed here. 441 442 443Sphinx standard options 444----------------------- 445 446All QAPI directives inherit a number of `standard options 447<https://www.sphinx-doc.org/en/master/usage/domains/index.html#basic-markup>`_ 448from Sphinx's ObjectDescription class. 449 450The dashed spellings of the below options were added in Sphinx 7.2, the 451undashed spellings are currently retained as aliases, but will be 452removed in a future version. 453 454* ``:no-index:`` and ``:noindex:`` -- Do not add this item into the 455 Index, and do not make it available for cross-referencing. 456* ``no-index-entry:`` and ``:noindexentry:`` -- Do not add this item 457 into the Index, but allow it to be cross-referenced. 458* ``no-contents-entry`` and ``:nocontentsentry:`` -- Exclude this item 459 from the Table of Contents. 460* ``no-typesetting`` -- Create TOC, Index and cross-referencing 461 entities, but don't actually display the content. 462 463 464QAPI standard options 465--------------------- 466 467All QAPI directives -- *except* for module -- support these common options. 468 469* ``:namespace: name`` -- This option allows you to override the 470 namespace association of a given definition. 471* ``:module: modname`` -- Borrowed from the Python domain, this option allows 472 you to override the module association of a given definition. 473* ``:since: x.y`` -- Allows the documenting of "Since" information, which is 474 displayed in the signature bar. 475* ``:ifcond: CONDITION`` -- Allows the documenting of conditional availability 476 information, which is displayed in an eyecatch just below the 477 signature bar. 478* ``:deprecated:`` -- Adds an eyecatch just below the signature bar that 479 advertises that this definition is deprecated and should be avoided. 480* ``:unstable:`` -- Adds an eyecatch just below the signature bar that 481 advertises that this definition is unstable and should not be used in 482 production code. 483 484 485qapi:module 486----------- 487 488The ``qapi:module`` directive marks the start of a QAPI module. It may have 489a content body, but it can be omitted. All subsequent QAPI directives 490are associated with the most recent module; this effects their "fully 491qualified" name, but has no other effect. 492 493Example:: 494 495 .. qapi:module:: block-core 496 497 Welcome to the block-core module! 498 499Will be rendered as: 500 501.. qapi:module:: block-core 502 :noindex: 503 504 Welcome to the block-core module! 505 506 507qapi:command 508------------ 509 510This directive documents a QMP command. It may use any of the standard 511Sphinx or QAPI options, and the documentation body may contain 512``:arg:``, ``:feat:``, ``:error:``, or ``:return:`` info field list 513entries. 514 515Example:: 516 517 .. qapi:command:: x-fake-command 518 :since: 42.0 519 :unstable: 520 521 This command is fake, so it can't hurt you! 522 523 :arg int foo: Your favorite number. 524 :arg string? bar: Your favorite season. 525 :return [string]: A lovely computer-written poem for you. 526 527 528Will be rendered as: 529 530 .. qapi:command:: x-fake-command 531 :noindex: 532 :since: 42.0 533 :unstable: 534 535 This command is fake, so it can't hurt you! 536 537 :arg int foo: Your favorite number. 538 :arg string? bar: Your favorite season. 539 :return [string]: A lovely computer-written poem for you. 540 541 542qapi:event 543---------- 544 545This directive documents a QMP event. It may use any of the standard 546Sphinx or QAPI options, and the documentation body may contain 547``:memb:`` or ``:feat:`` info field list entries. 548 549Example:: 550 551 .. qapi:event:: COMPUTER_IS_RUINED 552 :since: 0.1 553 :deprecated: 554 555 This event is emitted when your computer is *extremely* ruined. 556 557 :memb string reason: Diagnostics as to what caused your computer to 558 be ruined. 559 :feat sadness: When present, the diagnostic message will also 560 explain how sad the computer is as a result of your wrongdoings. 561 562Will be rendered as: 563 564.. qapi:event:: COMPUTER_IS_RUINED 565 :noindex: 566 :since: 0.1 567 :deprecated: 568 569 This event is emitted when your computer is *extremely* ruined. 570 571 :memb string reason: Diagnostics as to what caused your computer to 572 be ruined. 573 :feat sadness: When present, the diagnostic message will also explain 574 how sad the computer is as a result of your wrongdoings. 575 576 577qapi:enum 578--------- 579 580This directive documents a QAPI enum. It may use any of the standard 581Sphinx or QAPI options, and the documentation body may contain 582``:value:`` or ``:feat:`` info field list entries. 583 584Example:: 585 586 .. qapi:enum:: Mood 587 :ifcond: LIB_PERSONALITY 588 589 This enum represents your virtual machine's current mood! 590 591 :value Happy: Your VM is content and well-fed. 592 :value Hungry: Your VM needs food. 593 :value Melancholic: Your VM is experiencing existential angst. 594 :value Petulant: Your VM is throwing a temper tantrum. 595 596Will be rendered as: 597 598.. qapi:enum:: Mood 599 :noindex: 600 :ifcond: LIB_PERSONALITY 601 602 This enum represents your virtual machine's current mood! 603 604 :value Happy: Your VM is content and well-fed. 605 :value Hungry: Your VM needs food. 606 :value Melancholic: Your VM is experiencing existential angst. 607 :value Petulant: Your VM is throwing a temper tantrum. 608 609 610qapi:object 611----------- 612 613This directive documents a QAPI structure or union and represents a QMP 614object. It may use any of the standard Sphinx or QAPI options, and the 615documentation body may contain ``:memb:`` or ``:feat:`` info field list 616entries. 617 618Example:: 619 620 .. qapi:object:: BigBlobOfStuff 621 622 This object has a bunch of disparate and unrelated things in it. 623 624 :memb int Birthday: Your birthday, represented in seconds since the 625 UNIX epoch. 626 :memb [string] Fav-Foods: A list of your favorite foods. 627 :memb boolean? Bizarre-Docs: True if the documentation reference 628 should be strange. 629 630Will be rendered as: 631 632.. qapi:object:: BigBlobOfStuff 633 :noindex: 634 635 This object has a bunch of disparate and unrelated things in it. 636 637 :memb int Birthday: Your birthday, represented in seconds since the 638 UNIX epoch. 639 :memb [string] Fav-Foods: A list of your favorite foods. 640 :memb boolean? Bizarre-Docs: True if the documentation reference 641 should be strange. 642 643 644qapi:alternate 645-------------- 646 647This directive documents a QAPI alternate. It may use any of the 648standard Sphinx or QAPI options, and the documentation body may contain 649``:alt:`` or ``:feat:`` info field list entries. 650 651Example:: 652 653 .. qapi:alternate:: ErrorCode 654 655 This alternate represents an Error Code from the VM. 656 657 :alt int ec: An error code, like the type you're used to. 658 :alt string em: An expletive-laced error message, if your 659 computer is feeling particularly cranky and tired of your 660 antics. 661 662Will be rendered as: 663 664.. qapi:alternate:: ErrorCode 665 :noindex: 666 667 This alternate represents an Error Code from the VM. 668 669 :alt int ec: An error code, like the type you're used to. 670 :alt string em: An expletive-laced error message, if your 671 computer is feeling particularly cranky and tired of your 672 antics. 673