xref: /qemu/docs/devel/qapi-domain.rst (revision 100e8e2b383f1afce447274e1feb949166ad9c50)
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 analogous 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:`[QMP: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:`QMP: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.
404
405* A namespace can be explicitly provided;
406  e.g. ``:qapi:type:`QMP:BitmapSyncMode``
407* A module can be explicitly provided;
408  ``:qapi:type:`QMP:block-core.BitmapSyncMode``` will render to:
409  :qapi:type:`QMP:block-core.BitmapSyncMode`
410* If you don't want to display the "fully qualified" name, it can be
411  prefixed with a tilde; ``:qapi:type:`~QMP:block-core.BitmapSyncMode```
412  will render to: :qapi:type:`~QMP:block-core.BitmapSyncMode`
413
414
415Target resolution
416-----------------
417
418Any cross-reference to a QAPI type, whether using the ```any``` style of
419reference or the more explicit ```:qapi:any:`target``` syntax, allows
420for the presence or absence of either the namespace or module
421information.
422
423When absent, their value will be inferred from context by the presence
424of any ``qapi:namespace`` or ``qapi:module`` directives preceding the
425cross-reference.
426
427If no results are found when using the inferred values, other
428namespaces/modules will be searched as a last resort; but any explicitly
429provided values must always match in order to succeed.
430
431This allows for efficient cross-referencing with a minimum of syntax in
432the large majority of cases, but additional context or namespace markup
433may be required outside of the QAPI reference documents when linking to
434items that share a name across multiple documented QAPI schema.
435
436
437Custom link text
438----------------
439
440The name of a cross-reference link can be explicitly overridden like
441`most stock Sphinx references
442<https://www.sphinx-doc.org/en/master/usage/referencing.html#syntax>`_
443using the ``custom text <target>`` syntax.
444
445For example, ``:qapi:cmd:`Merge dirty bitmaps
446<block-dirty-bitmap-merge>``` will render as: :qapi:cmd:`Merge dirty
447bitmaps <QMP:block-dirty-bitmap-merge>`
448
449
450Directives
451==========
452
453The QAPI domain adds a number of custom directives for documenting
454various QAPI/QMP entities. The syntax is plain rST, and follows this
455general format::
456
457  .. qapi:directive:: argument
458     :option:
459     :another-option: with an argument
460
461     Content body, arbitrary rST is allowed here.
462
463
464Sphinx standard options
465-----------------------
466
467All QAPI directives inherit a number of `standard options
468<https://www.sphinx-doc.org/en/master/usage/domains/index.html#basic-markup>`_
469from Sphinx's ObjectDescription class.
470
471The dashed spellings of the below options were added in Sphinx 7.2, the
472undashed spellings are currently retained as aliases, but will be
473removed in a future version.
474
475* ``:no-index:`` and ``:noindex:`` -- Do not add this item into the
476  Index, and do not make it available for cross-referencing.
477* ``no-index-entry:`` and ``:noindexentry:`` -- Do not add this item
478  into the Index, but allow it to be cross-referenced.
479* ``no-contents-entry`` and ``:nocontentsentry:`` -- Exclude this item
480  from the Table of Contents.
481* ``no-typesetting`` -- Create TOC, Index and cross-referencing
482  entities, but don't actually display the content.
483
484
485QAPI standard options
486---------------------
487
488All QAPI directives -- *except* for namespace and module -- support
489these common options.
490
491* ``:namespace: name`` -- This option allows you to override the
492  namespace association of a given definition.
493* ``:module: modname`` -- Borrowed from the Python domain, this option allows
494  you to override the module association of a given definition.
495* ``:since: x.y`` -- Allows the documenting of "Since" information, which is
496  displayed in the signature bar.
497* ``:ifcond: CONDITION`` -- Allows the documenting of conditional availability
498  information, which is displayed in an eyecatch just below the
499  signature bar.
500* ``:deprecated:`` -- Adds an eyecatch just below the signature bar that
501  advertises that this definition is deprecated and should be avoided.
502* ``:unstable:`` -- Adds an eyecatch just below the signature bar that
503  advertises that this definition is unstable and should not be used in
504  production code.
505
506
507qapi:namespace
508--------------
509
510The ``qapi:namespace`` directive marks the start of a QAPI namespace. It
511does not take a content body, nor any options. All subsequent QAPI
512directives are associated with the most recent namespace. This affects
513the definition's "fully qualified name", allowing two different
514namespaces to create an otherwise identically named definition.
515
516This directive also influences how reference resolution works for any
517references that do not explicitly specify a namespace, so this directive
518can be used to nudge references into preferring targets from within that
519namespace.
520
521Example::
522
523   .. qapi:namespace:: QMP
524
525
526This directive has no visible effect.
527
528
529qapi:module
530-----------
531
532The ``qapi:module`` directive marks the start of a QAPI module. It may have
533a content body, but it can be omitted. All subsequent QAPI directives
534are associated with the most recent module; this effects their "fully
535qualified" name, but has no other effect.
536
537Example::
538
539   .. qapi:module:: block-core
540
541      Welcome to the block-core module!
542
543Will be rendered as:
544
545.. qapi:module:: block-core
546   :noindex:
547
548   Welcome to the block-core module!
549
550
551qapi:command
552------------
553
554This directive documents a QMP command. It may use any of the standard
555Sphinx or QAPI options, and the documentation body may contain
556``:arg:``, ``:feat:``, ``:error:``, or ``:return:`` info field list
557entries.
558
559Example::
560
561  .. qapi:command:: x-fake-command
562     :since: 42.0
563     :unstable:
564
565     This command is fake, so it can't hurt you!
566
567     :arg int foo: Your favorite number.
568     :arg string? bar: Your favorite season.
569     :return [string]: A lovely computer-written poem for you.
570
571
572Will be rendered as:
573
574  .. qapi:command:: x-fake-command
575     :noindex:
576     :since: 42.0
577     :unstable:
578
579     This command is fake, so it can't hurt you!
580
581     :arg int foo: Your favorite number.
582     :arg string? bar: Your favorite season.
583     :return [string]: A lovely computer-written poem for you.
584
585
586qapi:event
587----------
588
589This directive documents a QMP event. It may use any of the standard
590Sphinx or QAPI options, and the documentation body may contain
591``:memb:`` or ``:feat:`` info field list entries.
592
593Example::
594
595  .. qapi:event:: COMPUTER_IS_RUINED
596     :since: 0.1
597     :deprecated:
598
599     This event is emitted when your computer is *extremely* ruined.
600
601     :memb string reason: Diagnostics as to what caused your computer to
602        be ruined.
603     :feat sadness: When present, the diagnostic message will also
604        explain how sad the computer is as a result of your wrongdoings.
605
606Will be rendered as:
607
608.. qapi:event:: COMPUTER_IS_RUINED
609   :noindex:
610   :since: 0.1
611   :deprecated:
612
613   This event is emitted when your computer is *extremely* ruined.
614
615   :memb string reason: Diagnostics as to what caused your computer to
616      be ruined.
617   :feat sadness: When present, the diagnostic message will also explain
618      how sad the computer is as a result of your wrongdoings.
619
620
621qapi:enum
622---------
623
624This directive documents a QAPI enum. It may use any of the standard
625Sphinx or QAPI options, and the documentation body may contain
626``:value:`` or ``:feat:`` info field list entries.
627
628Example::
629
630  .. qapi:enum:: Mood
631     :ifcond: LIB_PERSONALITY
632
633     This enum represents your virtual machine's current mood!
634
635     :value Happy: Your VM is content and well-fed.
636     :value Hungry: Your VM needs food.
637     :value Melancholic: Your VM is experiencing existential angst.
638     :value Petulant: Your VM is throwing a temper tantrum.
639
640Will be rendered as:
641
642.. qapi:enum:: Mood
643   :noindex:
644   :ifcond: LIB_PERSONALITY
645
646   This enum represents your virtual machine's current mood!
647
648   :value Happy: Your VM is content and well-fed.
649   :value Hungry: Your VM needs food.
650   :value Melancholic: Your VM is experiencing existential angst.
651   :value Petulant: Your VM is throwing a temper tantrum.
652
653
654qapi:object
655-----------
656
657This directive documents a QAPI structure or union and represents a QMP
658object. It may use any of the standard Sphinx or QAPI options, and the
659documentation body may contain ``:memb:`` or ``:feat:`` info field list
660entries.
661
662Example::
663
664  .. qapi:object:: BigBlobOfStuff
665
666     This object has a bunch of disparate and unrelated things in it.
667
668     :memb int Birthday: Your birthday, represented in seconds since the
669                         UNIX epoch.
670     :memb [string] Fav-Foods: A list of your favorite foods.
671     :memb boolean? Bizarre-Docs: True if the documentation reference
672        should be strange.
673
674Will be rendered as:
675
676.. qapi:object:: BigBlobOfStuff
677   :noindex:
678
679   This object has a bunch of disparate and unrelated things in it.
680
681   :memb int Birthday: Your birthday, represented in seconds since the
682                       UNIX epoch.
683   :memb [string] Fav-Foods: A list of your favorite foods.
684   :memb boolean? Bizarre-Docs: True if the documentation reference
685      should be strange.
686
687
688qapi:alternate
689--------------
690
691This directive documents a QAPI alternate. It may use any of the
692standard Sphinx or QAPI options, and the documentation body may contain
693``:alt:`` or ``:feat:`` info field list entries.
694
695Example::
696
697  .. qapi:alternate:: ErrorCode
698
699     This alternate represents an Error Code from the VM.
700
701     :alt int ec: An error code, like the type you're used to.
702     :alt string em: An expletive-laced error message, if your
703        computer is feeling particularly cranky and tired of your
704        antics.
705
706Will be rendered as:
707
708.. qapi:alternate:: ErrorCode
709   :noindex:
710
711   This alternate represents an Error Code from the VM.
712
713   :alt int ec: An error code, like the type you're used to.
714   :alt string em: An expletive-laced error message, if your
715      computer is feeling particularly cranky and tired of your
716      antics.
717