1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright 2018 The DPDK contributors
3
4.. _doc_guidelines:
5
6DPDK Documentation Guidelines
7=============================
8
9This document outlines the guidelines for writing the DPDK Guides and API documentation in RST and Doxygen format.
10
11It also explains the structure of the DPDK documentation and shows how to build the Html and PDF versions of the documents.
12
13
14Structure of the Documentation
15------------------------------
16
17The DPDK source code repository contains input files to build the API documentation and User Guides.
18
19The main directories that contain files related to documentation are shown below::
20
21   lib
22   |-- librte_acl
23   |-- librte_cfgfile
24   |-- librte_cmdline
25   |-- librte_compat
26   |-- librte_eal
27   |   |-- ...
28   ...
29   doc
30   |-- api
31   +-- guides
32       |-- freebsd_gsg
33       |-- linux_gsg
34       |-- prog_guide
35       |-- sample_app_ug
36       |-- guidelines
37       |-- testpmd_app_ug
38       |-- rel_notes
39       |-- nics
40       |-- ...
41
42
43The API documentation is built from `Doxygen <http://www.doxygen.nl>`_ comments in the header files.
44These files are mainly in the ``lib/librte_*`` directories although some of the Poll Mode Drivers in ``drivers/net``
45are also documented with Doxygen.
46
47The configuration files that are used to control the Doxygen output are in the ``doc/api`` directory.
48
49The user guides such as *The Programmers Guide* and the *FreeBSD* and *Linux Getting Started* Guides are generated
50from RST markup text files using the `Sphinx <http://sphinx-doc.org>`_ Documentation Generator.
51
52These files are included in the ``doc/guides/`` directory.
53The output is controlled by the ``doc/guides/conf.py`` file.
54
55
56Role of the Documentation
57-------------------------
58
59The following items outline the roles of the different parts of the documentation and when they need to be updated or
60added to by the developer.
61
62* **Release Notes**
63
64  The Release Notes document which features have been added in the current and previous releases of DPDK and highlight
65  any known issues.
66  The Releases Notes also contain notifications of features that will change ABI compatibility in the next major release.
67
68  Developers should include updates to the Release Notes with patch sets that relate to any of the following sections:
69
70  * New Features
71  * Resolved Issues (see below)
72  * Known Issues
73  * API Changes
74  * ABI Changes
75  * Shared Library Versions
76
77  Resolved Issues should only include issues from previous releases that have been resolved in the current release.
78  Issues that are introduced and then fixed within a release cycle do not have to be included here.
79
80  Refer to the Release Notes from the previous DPDK release for the correct format of each section.
81
82
83* **API documentation**
84
85  The API documentation explains how to use the public DPDK functions.
86  The `API index page <http://doc.dpdk.org/api/>`_ shows the generated API documentation with related groups of functions.
87
88  The API documentation should be updated via Doxygen comments when new functions are added.
89
90* **Getting Started Guides**
91
92  The Getting Started Guides show how to install and configure DPDK and how to run DPDK based applications on different OSes.
93
94  A Getting Started Guide should be added when DPDK is ported to a new OS.
95
96* **The Programmers Guide**
97
98  The Programmers Guide explains how the API components of DPDK such as the EAL, Memzone, Rings and the Hash Library work.
99  It also explains how some higher level functionality such as Packet Distributor, Packet Framework and KNI work.
100  It also shows the build system and explains how to add applications.
101
102  The Programmers Guide should be expanded when new functionality is added to DPDK.
103
104* **App Guides**
105
106  The app guides document the DPDK applications in the ``app`` directory such as ``testpmd``.
107
108  The app guides should be updated if functionality is changed or added.
109
110* **Sample App Guides**
111
112  The sample app guides document the DPDK example applications in the examples directory.
113  Generally they demonstrate a major feature such as L2 or L3 Forwarding, Multi Process or Power Management.
114  They explain the purpose of the sample application, how to run it and step through some of the code to explain the
115  major functionality.
116
117  A new sample application should be accompanied by a new sample app guide.
118  The guide for the Skeleton Forwarding app is a good starting reference.
119
120* **Network Interface Controller Drivers**
121
122  The NIC Drivers document explains the features of the individual Poll Mode Drivers, such as software requirements,
123  configuration and initialization.
124
125  New documentation should be added for new Poll Mode Drivers.
126
127* **Guidelines**
128
129  The guideline documents record community process, expectations and design directions.
130
131  They can be extended, amended or discussed by submitting a patch and getting community approval.
132
133
134Building the Documentation
135--------------------------
136
137Dependencies
138~~~~~~~~~~~~
139
140
141The following dependencies must be installed to build the documentation:
142
143* Doxygen.
144
145* Sphinx (also called python-sphinx).
146
147* TexLive (at least TexLive-core and the extra Latex support).
148
149* Inkscape.
150
151`Doxygen`_ generates documentation from commented source code.
152It can be installed as follows:
153
154.. code-block:: console
155
156   # Ubuntu/Debian.
157   sudo apt-get -y install doxygen
158
159   # Red Hat/Fedora.
160   sudo dnf     -y install doxygen
161
162`Sphinx`_ is a Python documentation tool for converting RST files to Html or to PDF (via LaTeX).
163For full support with figure and table captioning the latest version of Sphinx can be installed as follows:
164
165.. code-block:: console
166
167   # Ubuntu/Debian.
168   sudo apt-get -y install python-pip
169   sudo pip install --upgrade sphinx
170   sudo pip install --upgrade sphinx_rtd_theme
171
172   # Red Hat/Fedora.
173   sudo dnf     -y install python-pip
174   sudo pip install --upgrade sphinx
175   sudo pip install --upgrade sphinx_rtd_theme
176
177For further information on getting started with Sphinx see the
178`Sphinx Getting Started <http://www.sphinx-doc.org/en/master/usage/quickstart.html>`_.
179
180.. Note::
181
182   To get full support for Figure and Table numbering it is best to install Sphinx 1.3.1 or later.
183
184
185`Inkscape`_ is a vector based graphics program which is used to create SVG images and also to convert SVG images to PDF images.
186It can be installed as follows:
187
188.. code-block:: console
189
190   # Ubuntu/Debian.
191   sudo apt-get -y install inkscape
192
193   # Red Hat/Fedora.
194   sudo dnf     -y install inkscape
195
196`TexLive <http://www.tug.org/texlive/>`_ is an installation package for Tex/LaTeX.
197It is used to generate the PDF versions of the documentation.
198The main required packages can be installed as follows:
199
200.. code-block:: console
201
202   # Ubuntu/Debian.
203   sudo apt-get -y install texlive-latex-extra texlive-lang-greek
204
205   # Red Hat/Fedora, selective install.
206   sudo dnf     -y install texlive-collection-latexextra texlive-greek-fontenc
207
208`Latexmk <http://personal.psu.edu/jcc8/software/latexmk-jcc/>`_ is a perl script
209for running LaTeX for resolving cross references,
210and it also runs auxiliary programs like bibtex, makeindex if necessary, and dvips.
211It has also a number of other useful capabilities (see man 1 latexmk).
212
213.. code-block:: console
214
215   # Ubuntu/Debian.
216   sudo apt-get -y install latexmk
217
218   # Red Hat/Fedora.
219   sudo dnf     -y install latexmk
220
221
222Build commands
223~~~~~~~~~~~~~~
224
225The documentation is built using the standard DPDK build system.
226Some examples are shown below:
227
228* Generate all the documentation targets::
229
230     make doc
231
232* Generate the Doxygen API documentation in Html::
233
234     make doc-api-html
235
236* Generate the guides documentation in Html::
237
238     make doc-guides-html
239
240* Generate the guides documentation in Pdf::
241
242     make doc-guides-pdf
243
244The output of these commands is generated in the ``build`` directory::
245
246   build/doc
247         |-- html
248         |   |-- api
249         |   +-- guides
250         |
251         +-- pdf
252             +-- guides
253
254
255.. Note::
256
257   Make sure to fix any Sphinx or Doxygen warnings when adding or updating documentation.
258
259The documentation output files can be removed as follows::
260
261   make doc-clean
262
263
264Document Guidelines
265-------------------
266
267Here are some guidelines in relation to the style of the documentation:
268
269* Document the obvious as well as the obscure since it won't always be obvious to the reader.
270  For example an instruction like "Set up 64 2MB Hugepages" is better when followed by a sample commandline or a link to
271  the appropriate section of the documentation.
272
273* Use American English spellings throughout.
274  This can be checked using the ``aspell`` utility::
275
276       aspell --lang=en_US --check doc/guides/sample_app_ug/mydoc.rst
277
278
279RST Guidelines
280--------------
281
282The RST (reStructuredText) format is a plain text markup format that can be converted to Html, PDF or other formats.
283It is most closely associated with Python but it can be used to document any language.
284It is used in DPDK to document everything apart from the API.
285
286The Sphinx documentation contains a very useful `RST Primer <http://sphinx-doc.org/rest.html#rst-primer>`_ which is a
287good place to learn the minimal set of syntax required to format a document.
288
289The official `reStructuredText <http://docutils.sourceforge.net/rst.html>`_ website contains the specification for the
290RST format and also examples of how to use it.
291However, for most developers the RST Primer is a better resource.
292
293The most common guidelines for writing RST text are detailed in the
294`Documenting Python <https://docs.python.org/devguide/documenting.html>`_ guidelines.
295The additional guidelines below reiterate or expand upon those guidelines.
296
297
298Line Length
299~~~~~~~~~~~
300
301* Lines in sentences should be less than 80 characters and wrapped at
302  words. Multiple sentences which are not separated by a blank line are joined
303  automatically into paragraphs.
304
305* Lines in literal blocks **must** be less than 80 characters since
306  they are not wrapped by the document formatters and can exceed the page width
307  in PDF documents.
308
309  Long literal command lines can be shown wrapped with backslashes. For
310  example::
311
312     testpmd -l 2-3 -n 4 \
313             --vdev=virtio_user0,path=/dev/vhost-net,queues=2,queue_size=1024 \
314             -- -i --tx-offloads=0x0000002c --enable-lro --txq=2 --rxq=2 \
315             --txd=1024 --rxd=1024
316
317
318Whitespace
319~~~~~~~~~~
320
321* Standard RST indentation is 3 spaces.
322  Code can be indented 4 spaces, especially if it is copied from source files.
323
324* No tabs.
325  Convert tabs in embedded code to 4 or 8 spaces.
326
327* No trailing whitespace.
328
329* Add 2 blank lines before each section header.
330
331* Add 1 blank line after each section header.
332
333* Add 1 blank line between each line of a list.
334
335
336Section Headers
337~~~~~~~~~~~~~~~
338
339* Section headers should use the following underline formats::
340
341   Level 1 Heading
342   ===============
343
344
345   Level 2 Heading
346   ---------------
347
348
349   Level 3 Heading
350   ~~~~~~~~~~~~~~~
351
352
353   Level 4 Heading
354   ^^^^^^^^^^^^^^^
355
356
357* Level 4 headings should be used sparingly.
358
359* The underlines should match the length of the text.
360
361* In general, the heading should be less than 80 characters, for conciseness.
362
363* As noted above:
364
365   * Add 2 blank lines before each section header.
366
367   * Add 1 blank line after each section header.
368
369
370Lists
371~~~~~
372
373* Bullet lists should be formatted with a leading ``*`` as follows::
374
375     * Item one.
376
377     * Item two is a long line that is wrapped and then indented to match
378       the start of the previous line.
379
380     * One space character between the bullet and the text is preferred.
381
382* Numbered lists can be formatted with a leading number but the preference is to use ``#.`` which will give automatic numbering.
383  This is more convenient when adding or removing items::
384
385     #. Item one.
386
387     #. Item two is a long line that is wrapped and then indented to match
388        the start of the previous line.
389
390     #. Item three.
391
392* Definition lists can be written with or without a bullet::
393
394     * Item one.
395
396       Some text about item one.
397
398     * Item two.
399
400       Some text about item two.
401
402* All lists, and sub-lists, must be separated from the preceding text by a blank line.
403  This is a syntax requirement.
404
405* All list items should be separated by a blank line for readability.
406
407
408Code and Literal block sections
409~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
410
411* Inline text that is required to be rendered with a fixed width font should be enclosed in backquotes like this:
412  \`\`text\`\`, so that it appears like this: ``text``.
413
414* Fixed width, literal blocks of texts should be indented at least 3 spaces and prefixed with ``::`` like this::
415
416     Here is some fixed width text::
417
418        0x0001 0x0001 0x00FF 0x00FF
419
420* It is also possible to specify an encoding for a literal block using the ``.. code-block::`` directive so that syntax
421  highlighting can be applied.
422  Examples of supported highlighting are::
423
424     .. code-block:: console
425     .. code-block:: c
426     .. code-block:: python
427     .. code-block:: diff
428     .. code-block:: none
429
430  That can be applied as follows::
431
432      .. code-block:: c
433
434         #include<stdio.h>
435
436         int main() {
437
438            printf("Hello World\n");
439
440            return 0;
441         }
442
443  Which would be rendered as:
444
445  .. code-block:: c
446
447      #include<stdio.h>
448
449      int main() {
450
451         printf("Hello World\n");
452
453         return 0;
454      }
455
456
457* The default encoding for a literal block using the simplified ``::``
458  directive is ``none``.
459
460* Lines in literal blocks must be less than 80 characters since they can exceed the page width when converted to PDF documentation.
461  For long literal lines that exceed that limit try to wrap the text at sensible locations.
462  For example a long command line could be documented like this and still work if copied directly from the docs::
463
464     build/app/testpmd -l 0-2 -n3 --vdev=net_pcap0,iface=eth0     \
465                               --vdev=net_pcap1,iface=eth1     \
466                               -- -i --nb-cores=2 --nb-ports=2 \
467                                  --total-num-mbufs=2048
468
469* Long lines that cannot be wrapped, such as application output, should be truncated to be less than 80 characters.
470
471
472Images
473~~~~~~
474
475* All images should be in SVG scalar graphics format.
476  They should be true SVG XML files and should not include binary formats embedded in a SVG wrapper.
477
478* The DPDK documentation contains some legacy images in PNG format.
479  These will be converted to SVG in time.
480
481* `Inkscape <http://inkscape.org>`_ is the recommended graphics editor for creating the images.
482  Use some of the older images in ``doc/guides/prog_guide/img/`` as a template, for example ``mbuf1.svg``
483  or ``ring-enqueue1.svg``.
484
485* The SVG images should include a copyright notice, as an XML comment.
486
487* Images in the documentation should be formatted as follows:
488
489   * The image should be preceded by a label in the format ``.. _figure_XXXX:`` with a leading underscore and
490     where ``XXXX`` is a unique descriptive name.
491
492   * Images should be included using the ``.. figure::`` directive and the file type should be set to ``*`` (not ``.svg``).
493     This allows the format of the image to be changed if required, without updating the documentation.
494
495   * Images must have a caption as part of the ``.. figure::`` directive.
496
497* Here is an example of the previous three guidelines::
498
499     .. _figure_mempool:
500
501     .. figure:: img/mempool.*
502
503        A mempool in memory with its associated ring.
504
505.. _mock_label:
506
507* Images can then be linked to using the ``:numref:`` directive::
508
509     The mempool layout is shown in :numref:`figure_mempool`.
510
511  This would be rendered as: *The mempool layout is shown in* :ref:`Fig 6.3 <mock_label>`.
512
513  **Note**: The ``:numref:`` directive requires Sphinx 1.3.1 or later.
514  With earlier versions it will still be rendered as a link but won't have an automatically generated number.
515
516* The caption of the image can be generated, with a link, using the ``:ref:`` directive::
517
518     :ref:`figure_mempool`
519
520  This would be rendered as: *A mempool in memory with its associated ring.*
521
522Tables
523~~~~~~
524
525* RST tables should be used sparingly.
526  They are hard to format and to edit, they are often rendered incorrectly in PDF format, and the same information
527  can usually be shown just as clearly with a definition or bullet list.
528
529* Tables in the documentation should be formatted as follows:
530
531   * The table should be preceded by a label in the format ``.. _table_XXXX:`` with a leading underscore and where
532     ``XXXX`` is a unique descriptive name.
533
534   * Tables should be included using the ``.. table::`` directive and must have a caption.
535
536* Here is an example of the previous two guidelines::
537
538     .. _table_qos_pipes:
539
540     .. table:: Sample configuration for QOS pipes.
541
542        +----------+----------+----------+
543        | Header 1 | Header 2 | Header 3 |
544        |          |          |          |
545        +==========+==========+==========+
546        | Text     | Text     | Text     |
547        +----------+----------+----------+
548        | ...      | ...      | ...      |
549        +----------+----------+----------+
550
551* Tables can be linked to using the ``:numref:`` and ``:ref:`` directives, as shown in the previous section for images.
552  For example::
553
554     The QOS configuration is shown in :numref:`table_qos_pipes`.
555
556* Tables should not include merged cells since they are not supported by the PDF renderer.
557
558
559.. _links:
560
561Hyperlinks
562~~~~~~~~~~
563
564* Links to external websites can be plain URLs.
565  The following is rendered as http://dpdk.org::
566
567     http://dpdk.org
568
569* They can contain alternative text.
570  The following is rendered as `Check out DPDK <http://dpdk.org>`_::
571
572     `Check out DPDK <http://dpdk.org>`_
573
574* An internal link can be generated by placing labels in the document with the format ``.. _label_name``.
575
576* The following links to the top of this section: :ref:`links`::
577
578     .. _links:
579
580     Hyperlinks
581     ~~~~~~~~~~
582
583     * The following links to the top of this section: :ref:`links`:
584
585.. Note::
586
587   The label must have a leading underscore but the reference to it must omit it.
588   This is a frequent cause of errors and warnings.
589
590* The use of a label is preferred since it works across files and will still work if the header text changes.
591
592
593.. _doxygen_guidelines:
594
595Doxygen Guidelines
596------------------
597
598The DPDK API is documented using Doxygen comment annotations in the header files.
599Doxygen is a very powerful tool, it is extremely configurable and with a little effort can be used to create expressive documents.
600See the `Doxygen website <http://www.doxygen.nl>`_ for full details on how to use it.
601
602The following are some guidelines for use of Doxygen in the DPDK API documentation:
603
604* New libraries that are documented with Doxygen should be added to the Doxygen configuration file: ``doc/api/doxy-api.conf``.
605  It is only required to add the directory that contains the files.
606  It isn't necessary to explicitly name each file since the configuration matches all ``rte_*.h`` files in the directory.
607
608* Use proper capitalization and punctuation in the Doxygen comments since they will become sentences in the documentation.
609  This in particular applies to single line comments, which is the case the is most often forgotten.
610
611* Use ``@`` style Doxygen commands instead of ``\`` style commands.
612
613* Add a general description of each library at the head of the main header files:
614
615  .. code-block:: c
616
617      /**
618       * @file
619       * RTE Mempool.
620       *
621       * A memory pool is an allocator of fixed-size object. It is
622       * identified by its name, and uses a ring to store free objects.
623       * ...
624       */
625
626* Document the purpose of a function, the parameters used and the return
627  value:
628
629  .. code-block:: c
630
631     /**
632      * Try to take the lock.
633      *
634      * @param sl
635      *   A pointer to the spinlock.
636      * @return
637      *   1 if the lock is successfully taken; 0 otherwise.
638      */
639     int rte_spinlock_trylock(rte_spinlock_t *sl);
640
641* Doxygen supports Markdown style syntax such as bold, italics, fixed width text and lists.
642  For example the second line in the ``devargs`` parameter in the previous example will be rendered as:
643
644     The strings should be a pci address like ``0000:01:00.0`` or **virtual** device name like ``net_pcap0``.
645
646* Use ``-`` instead of ``*`` for lists within the Doxygen comment since the latter can get confused with the comment delimiter.
647
648* Add an empty line between the function description, the ``@params`` and ``@return`` for readability.
649
650* Place the ``@params`` description on separate line and indent it by 2 spaces.
651  (It would be better to use no indentation since this is more common and also because checkpatch complains about leading
652  whitespace in comments.
653  However this is the convention used in the existing DPDK code.)
654
655* Documented functions can be linked to simply by adding ``()`` to the function name:
656
657  .. code-block:: c
658
659      /**
660       * The functions exported by the application Ethernet API to setup
661       * a device designated by its port identifier must be invoked in
662       * the following order:
663       *     - rte_eth_dev_configure()
664       *     - rte_eth_tx_queue_setup()
665       *     - rte_eth_rx_queue_setup()
666       *     - rte_eth_dev_start()
667       */
668
669  In the API documentation the functions will be rendered as links, see the
670  `online section of the rte_ethdev.h docs <http://doc.dpdk.org/api/rte__ethdev_8h.html>`_ that contains the above text.
671
672* The ``@see`` keyword can be used to create a *see also* link to another file or library.
673  This directive should be placed on one line at the bottom of the documentation section.
674
675  .. code-block:: c
676
677     /**
678      * ...
679      *
680      * Some text that references mempools.
681      *
682      * @see eal_memzone.c
683      */
684
685* Doxygen supports two types of comments for documenting variables, constants and members: prefix and postfix:
686
687  .. code-block:: c
688
689     /** This is a prefix comment. */
690     #define RTE_FOO_ERROR  0x023.
691
692     #define RTE_BAR_ERROR  0x024. /**< This is a postfix comment. */
693
694* Postfix comments are preferred for struct members and constants if they can be documented in the same way:
695
696  .. code-block:: c
697
698     struct rte_eth_stats {
699         uint64_t ipackets; /**< Total number of received packets. */
700         uint64_t opackets; /**< Total number of transmitted packets.*/
701         uint64_t ibytes;   /**< Total number of received bytes. */
702         uint64_t obytes;   /**< Total number of transmitted bytes. */
703         uint64_t imissed;  /**< Total of RX missed packets. */
704         uint64_t ibadcrc;  /**< Total of RX packets with CRC error. */
705         uint64_t ibadlen;  /**< Total of RX packets with bad length. */
706     }
707
708  Note: postfix comments should be aligned with spaces not tabs in accordance
709  with the :ref:`coding_style`.
710
711* If a single comment type can't be used, due to line length limitations then
712  prefix comments should be preferred.
713  For example this section of the code contains prefix comments, postfix comments on the same line and postfix
714  comments on a separate line:
715
716  .. code-block:: c
717
718     /** Number of elements in the elt_pa array. */
719     uint32_t    pg_num __rte_cache_aligned;
720     uint32_t    pg_shift;     /**< LOG2 of the physical pages. */
721     uintptr_t   pg_mask;      /**< Physical page mask value. */
722     uintptr_t   elt_va_start;
723     /**< Virtual address of the first mempool object. */
724     uintptr_t   elt_va_end;
725     /**< Virtual address of the <size + 1> mempool object. */
726     phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
727     /**< Array of physical page addresses for the mempool buffer. */
728
729  This doesn't have an effect on the rendered documentation but it is confusing for the developer reading the code.
730  It this case it would be clearer to use prefix comments throughout:
731
732  .. code-block:: c
733
734     /** Number of elements in the elt_pa array. */
735     uint32_t    pg_num __rte_cache_aligned;
736     /** LOG2 of the physical pages. */
737     uint32_t    pg_shift;
738     /** Physical page mask value. */
739     uintptr_t   pg_mask;
740     /** Virtual address of the first mempool object. */
741     uintptr_t   elt_va_start;
742     /** Virtual address of the <size + 1> mempool object. */
743     uintptr_t   elt_va_end;
744     /** Array of physical page addresses for the mempool buffer. */
745     phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
746
747* Check for Doxygen warnings in new code by checking the API documentation build::
748
749     make doc-api-html >/dev/null
750
751* Read the rendered section of the documentation that you have added for correctness, clarity and consistency
752  with the surrounding text.
753