xref: /qemu/docs/devel/qapi-domain.rst (revision 42b633dbd1b5d3f7ce5c9b8e4417267399f641e1)
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* ``:module: modname`` -- Borrowed from the Python domain, this option allows
470  you to override the module association of a given definition.
471* ``:since: x.y`` -- Allows the documenting of "Since" information, which is
472  displayed in the signature bar.
473* ``:ifcond: CONDITION`` -- Allows the documenting of conditional availability
474  information, which is displayed in an eyecatch just below the
475  signature bar.
476* ``:deprecated:`` -- Adds an eyecatch just below the signature bar that
477  advertises that this definition is deprecated and should be avoided.
478* ``:unstable:`` -- Adds an eyecatch just below the signature bar that
479  advertises that this definition is unstable and should not be used in
480  production code.
481
482
483qapi:module
484-----------
485
486The ``qapi:module`` directive marks the start of a QAPI module. It may have
487a content body, but it can be omitted. All subsequent QAPI directives
488are associated with the most recent module; this effects their "fully
489qualified" name, but has no other effect.
490
491Example::
492
493   .. qapi:module:: block-core
494
495      Welcome to the block-core module!
496
497Will be rendered as:
498
499.. qapi:module:: block-core
500   :noindex:
501
502   Welcome to the block-core module!
503
504
505qapi:command
506------------
507
508This directive documents a QMP command. It may use any of the standard
509Sphinx or QAPI options, and the documentation body may contain
510``:arg:``, ``:feat:``, ``:error:``, or ``:return:`` info field list
511entries.
512
513Example::
514
515  .. qapi:command:: x-fake-command
516     :since: 42.0
517     :unstable:
518
519     This command is fake, so it can't hurt you!
520
521     :arg int foo: Your favorite number.
522     :arg string? bar: Your favorite season.
523     :return [string]: A lovely computer-written poem for you.
524
525
526Will be rendered as:
527
528  .. qapi:command:: x-fake-command
529     :noindex:
530     :since: 42.0
531     :unstable:
532
533     This command is fake, so it can't hurt you!
534
535     :arg int foo: Your favorite number.
536     :arg string? bar: Your favorite season.
537     :return [string]: A lovely computer-written poem for you.
538
539
540qapi:event
541----------
542
543This directive documents a QMP event. It may use any of the standard
544Sphinx or QAPI options, and the documentation body may contain
545``:memb:`` or ``:feat:`` info field list entries.
546
547Example::
548
549  .. qapi:event:: COMPUTER_IS_RUINED
550     :since: 0.1
551     :deprecated:
552
553     This event is emitted when your computer is *extremely* ruined.
554
555     :memb string reason: Diagnostics as to what caused your computer to
556        be ruined.
557     :feat sadness: When present, the diagnostic message will also
558        explain how sad the computer is as a result of your wrongdoings.
559
560Will be rendered as:
561
562.. qapi:event:: COMPUTER_IS_RUINED
563   :noindex:
564   :since: 0.1
565   :deprecated:
566
567   This event is emitted when your computer is *extremely* ruined.
568
569   :memb string reason: Diagnostics as to what caused your computer to
570      be ruined.
571   :feat sadness: When present, the diagnostic message will also explain
572      how sad the computer is as a result of your wrongdoings.
573
574
575qapi:enum
576---------
577
578This directive documents a QAPI enum. It may use any of the standard
579Sphinx or QAPI options, and the documentation body may contain
580``:value:`` or ``:feat:`` info field list entries.
581
582Example::
583
584  .. qapi:enum:: Mood
585     :ifcond: LIB_PERSONALITY
586
587     This enum represents your virtual machine's current mood!
588
589     :value Happy: Your VM is content and well-fed.
590     :value Hungry: Your VM needs food.
591     :value Melancholic: Your VM is experiencing existential angst.
592     :value Petulant: Your VM is throwing a temper tantrum.
593
594Will be rendered as:
595
596.. qapi:enum:: Mood
597   :noindex:
598   :ifcond: LIB_PERSONALITY
599
600   This enum represents your virtual machine's current mood!
601
602   :value Happy: Your VM is content and well-fed.
603   :value Hungry: Your VM needs food.
604   :value Melancholic: Your VM is experiencing existential angst.
605   :value Petulant: Your VM is throwing a temper tantrum.
606
607
608qapi:object
609-----------
610
611This directive documents a QAPI structure or union and represents a QMP
612object. It may use any of the standard Sphinx or QAPI options, and the
613documentation body may contain ``:memb:`` or ``:feat:`` info field list
614entries.
615
616Example::
617
618  .. qapi:object:: BigBlobOfStuff
619
620     This object has a bunch of disparate and unrelated things in it.
621
622     :memb int Birthday: Your birthday, represented in seconds since the
623                         UNIX epoch.
624     :memb [string] Fav-Foods: A list of your favorite foods.
625     :memb boolean? Bizarre-Docs: True if the documentation reference
626        should be strange.
627
628Will be rendered as:
629
630.. qapi:object:: BigBlobOfStuff
631   :noindex:
632
633   This object has a bunch of disparate and unrelated things in it.
634
635   :memb int Birthday: Your birthday, represented in seconds since the
636                       UNIX epoch.
637   :memb [string] Fav-Foods: A list of your favorite foods.
638   :memb boolean? Bizarre-Docs: True if the documentation reference
639      should be strange.
640
641
642qapi:alternate
643--------------
644
645This directive documents a QAPI alternate. It may use any of the
646standard Sphinx or QAPI options, and the documentation body may contain
647``:alt:`` or ``:feat:`` info field list entries.
648
649Example::
650
651  .. qapi:alternate:: ErrorCode
652
653     This alternate represents an Error Code from the VM.
654
655     :alt int ec: An error code, like the type you're used to.
656     :alt string em: An expletive-laced error message, if your
657        computer is feeling particularly cranky and tired of your
658        antics.
659
660Will be rendered as:
661
662.. qapi:alternate:: ErrorCode
663   :noindex:
664
665   This alternate represents an Error Code from the VM.
666
667   :alt int ec: An error code, like the type you're used to.
668   :alt string em: An expletive-laced error message, if your
669      computer is feeling particularly cranky and tired of your
670      antics.
671