xref: /dpdk/doc/guides/contributing/coding_style.rst (revision d76a592782c1291b19f74a8ec29bcbd40b5170cd)
16bdae907SThomas Monjalon.. _coding_style:
26bdae907SThomas Monjalon
36bdae907SThomas MonjalonDPDK Coding Style
46bdae907SThomas Monjalon=================
56bdae907SThomas Monjalon
66bdae907SThomas MonjalonDescription
76bdae907SThomas Monjalon-----------
86bdae907SThomas Monjalon
96bdae907SThomas MonjalonThis document specifies the preferred style for source files in the DPDK source tree.
106bdae907SThomas MonjalonIt is based on the Linux Kernel coding guidelines and the FreeBSD 7.2 Kernel Developer's Manual (see man style(9)), but was heavily modified for the needs of the DPDK.
116bdae907SThomas Monjalon
126bdae907SThomas MonjalonGeneral Guidelines
136bdae907SThomas Monjalon------------------
146bdae907SThomas Monjalon
156bdae907SThomas MonjalonThe rules and guidelines given in this document cannot cover every situation, so the following general guidelines should be used as a fallback:
166bdae907SThomas Monjalon
176bdae907SThomas Monjalon* The code style should be consistent within each individual file.
186bdae907SThomas Monjalon* In the case of creating new files, the style should be consistent within each file in a given directory or module.
196bdae907SThomas Monjalon* The primary reason for coding standards is to increase code readability and comprehensibility, therefore always use whatever option will make the code easiest to read.
206bdae907SThomas Monjalon
216bdae907SThomas MonjalonLine length is recommended to be not more than 80 characters, including comments.
226bdae907SThomas Monjalon[Tab stop size should be assumed to be 8-characters wide].
236bdae907SThomas Monjalon
246bdae907SThomas Monjalon.. note::
256bdae907SThomas Monjalon
266bdae907SThomas Monjalon	The above is recommendation, and not a hard limit.
276bdae907SThomas Monjalon	However, it is expected that the recommendations should be followed in all but the rarest situations.
286bdae907SThomas Monjalon
296bdae907SThomas MonjalonC Comment Style
306bdae907SThomas Monjalon---------------
316bdae907SThomas Monjalon
326bdae907SThomas MonjalonUsual Comments
336bdae907SThomas Monjalon~~~~~~~~~~~~~~
346bdae907SThomas Monjalon
356bdae907SThomas MonjalonThese comments should be used in normal cases.
366bdae907SThomas MonjalonTo document a public API, a doxygen-like format must be used: refer to :ref:`doxygen_guidelines`.
376bdae907SThomas Monjalon
386bdae907SThomas Monjalon.. code-block:: c
396bdae907SThomas Monjalon
406bdae907SThomas Monjalon /*
416bdae907SThomas Monjalon  * VERY important single-line comments look like this.
426bdae907SThomas Monjalon  */
436bdae907SThomas Monjalon
446bdae907SThomas Monjalon /* Most single-line comments look like this. */
456bdae907SThomas Monjalon
466bdae907SThomas Monjalon /*
476bdae907SThomas Monjalon  * Multi-line comments look like this.  Make them real sentences. Fill
486bdae907SThomas Monjalon  * them so they look like real paragraphs.
496bdae907SThomas Monjalon  */
506bdae907SThomas Monjalon
516bdae907SThomas MonjalonLicense Header
526bdae907SThomas Monjalon~~~~~~~~~~~~~~
536bdae907SThomas Monjalon
546bdae907SThomas MonjalonEach file should begin with a special comment containing the appropriate copyright and license for the file.
556bdae907SThomas MonjalonGenerally this is the BSD License, except for code for Linux Kernel modules.
566bdae907SThomas MonjalonAfter any copyright header, a blank line should be left before any other contents, e.g. include statements in a C file.
576bdae907SThomas Monjalon
586bdae907SThomas MonjalonC Preprocessor Directives
596bdae907SThomas Monjalon-------------------------
606bdae907SThomas Monjalon
616bdae907SThomas MonjalonHeader Includes
626bdae907SThomas Monjalon~~~~~~~~~~~~~~~
636bdae907SThomas Monjalon
646bdae907SThomas MonjalonIn DPDK sources, the include files should be ordered as following:
656bdae907SThomas Monjalon
666bdae907SThomas Monjalon#. libc includes (system includes first)
676bdae907SThomas Monjalon#. DPDK EAL includes
686bdae907SThomas Monjalon#. DPDK misc libraries includes
696bdae907SThomas Monjalon#. application-specific includes
706bdae907SThomas Monjalon
716bdae907SThomas MonjalonInclude files from the local application directory are included using quotes, while includes from other paths are included using angle brackets: "<>".
726bdae907SThomas Monjalon
736bdae907SThomas MonjalonExample:
746bdae907SThomas Monjalon
756bdae907SThomas Monjalon.. code-block:: c
766bdae907SThomas Monjalon
776bdae907SThomas Monjalon #include <stdio.h>
786bdae907SThomas Monjalon #include <stdlib.h>
796bdae907SThomas Monjalon
806bdae907SThomas Monjalon #include <rte_eal.h>
816bdae907SThomas Monjalon
826bdae907SThomas Monjalon #include <rte_ring.h>
836bdae907SThomas Monjalon #include <rte_mempool.h>
846bdae907SThomas Monjalon
856bdae907SThomas Monjalon #include "application.h"
866bdae907SThomas Monjalon
876bdae907SThomas MonjalonHeader File Guards
886bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~
896bdae907SThomas Monjalon
906bdae907SThomas MonjalonHeaders should be protected against multiple inclusion with the usual:
916bdae907SThomas Monjalon
926bdae907SThomas Monjalon.. code-block:: c
936bdae907SThomas Monjalon
946bdae907SThomas Monjalon   #ifndef _FILE_H_
956bdae907SThomas Monjalon   #define _FILE_H_
966bdae907SThomas Monjalon
976bdae907SThomas Monjalon   /* Code */
986bdae907SThomas Monjalon
996bdae907SThomas Monjalon   #endif /* _FILE_H_ */
1006bdae907SThomas Monjalon
1016bdae907SThomas Monjalon
1026bdae907SThomas MonjalonMacros
1036bdae907SThomas Monjalon~~~~~~
1046bdae907SThomas Monjalon
1056bdae907SThomas MonjalonDo not ``#define`` or declare names except with the standard DPDK prefix: ``RTE_``.
1066bdae907SThomas MonjalonThis is to ensure there are no collisions with definitions in the application itself.
1076bdae907SThomas Monjalon
1086bdae907SThomas MonjalonThe names of "unsafe" macros (ones that have side effects), and the names of macros for manifest constants, are all in uppercase.
1096bdae907SThomas Monjalon
1106bdae907SThomas MonjalonThe expansions of expression-like macros are either a single token or have outer parentheses.
1116bdae907SThomas MonjalonIf a macro is an inline expansion of a function, the function name is all in lowercase and the macro has the same name all in uppercase.
1126bdae907SThomas MonjalonIf the macro encapsulates a compound statement, enclose it in a do-while loop, so that it can be used safely in if statements.
1136bdae907SThomas MonjalonAny final statement-terminating semicolon should be supplied by the macro invocation rather than the macro, to make parsing easier for pretty-printers and editors.
1146bdae907SThomas Monjalon
1156bdae907SThomas MonjalonFor example:
1166bdae907SThomas Monjalon
1176bdae907SThomas Monjalon.. code-block:: c
1186bdae907SThomas Monjalon
1196bdae907SThomas Monjalon #define MACRO(x, y) do {                                        \
1206bdae907SThomas Monjalon         variable = (x) + (y);                                   \
1216bdae907SThomas Monjalon         (y) += 2;                                               \
1226bdae907SThomas Monjalon } while(0)
1236bdae907SThomas Monjalon
1246bdae907SThomas Monjalon.. note::
1256bdae907SThomas Monjalon
1266bdae907SThomas Monjalon Wherever possible, enums and inline functions should be preferred to macros, since they provide additional degrees of type-safety and can allow compilers to emit extra warnings about unsafe code.
1276bdae907SThomas Monjalon
1286bdae907SThomas MonjalonConditional Compilation
1296bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~~
1306bdae907SThomas Monjalon
1316bdae907SThomas Monjalon* When code is conditionally compiled using ``#ifdef`` or ``#if``, a comment may be added following the matching
1326bdae907SThomas Monjalon  ``#endif`` or ``#else`` to permit the reader to easily discern where conditionally compiled code regions end.
1336bdae907SThomas Monjalon* This comment should be used only for (subjectively) long regions, regions greater than 20 lines, or where a series of nested ``#ifdef``'s may be confusing to the reader.
1346bdae907SThomas Monjalon  Exceptions may be made for cases where code is conditionally not compiled for the purposes of lint(1), or other tools, even though the uncompiled region may be small.
1356bdae907SThomas Monjalon* The comment should be separated from the ``#endif`` or ``#else`` by a single space.
1366bdae907SThomas Monjalon* For short conditionally compiled regions, a closing comment should not be used.
1376bdae907SThomas Monjalon* The comment for ``#endif`` should match the expression used in the corresponding ``#if`` or ``#ifdef``.
1386bdae907SThomas Monjalon* The comment for ``#else`` and ``#elif`` should match the inverse of the expression(s) used in the preceding ``#if`` and/or ``#elif`` statements.
1396bdae907SThomas Monjalon* In the comments, the subexpression ``defined(FOO)`` is abbreviated as "FOO".
1406bdae907SThomas Monjalon  For the purposes of comments, ``#ifndef FOO`` is treated as ``#if !defined(FOO)``.
1416bdae907SThomas Monjalon
1426bdae907SThomas Monjalon.. code-block:: c
1436bdae907SThomas Monjalon
1446bdae907SThomas Monjalon #ifdef KTRACE
1456bdae907SThomas Monjalon #include <sys/ktrace.h>
1466bdae907SThomas Monjalon #endif
1476bdae907SThomas Monjalon
1486bdae907SThomas Monjalon #ifdef COMPAT_43
1496bdae907SThomas Monjalon /* A large region here, or other conditional code. */
1506bdae907SThomas Monjalon #else /* !COMPAT_43 */
1516bdae907SThomas Monjalon /* Or here. */
1526bdae907SThomas Monjalon #endif /* COMPAT_43 */
1536bdae907SThomas Monjalon
1546bdae907SThomas Monjalon #ifndef COMPAT_43
1556bdae907SThomas Monjalon /* Yet another large region here, or other conditional code. */
1566bdae907SThomas Monjalon #else /* COMPAT_43 */
1576bdae907SThomas Monjalon /* Or here. */
1586bdae907SThomas Monjalon #endif /* !COMPAT_43 */
1596bdae907SThomas Monjalon
1606bdae907SThomas Monjalon.. note::
1616bdae907SThomas Monjalon
1626bdae907SThomas Monjalon Conditional compilation should be used only when absolutely necessary, as it increases the number of target binaries that need to be built and tested.
1636bdae907SThomas Monjalon
1646bdae907SThomas MonjalonC Types
1656bdae907SThomas Monjalon-------
1666bdae907SThomas Monjalon
1676bdae907SThomas MonjalonIntegers
1686bdae907SThomas Monjalon~~~~~~~~
1696bdae907SThomas Monjalon
1706bdae907SThomas MonjalonFor fixed/minimum-size integer values, the project uses the form uintXX_t (from stdint.h) instead of older BSD-style integer identifiers of the form u_intXX_t.
1716bdae907SThomas Monjalon
1726bdae907SThomas MonjalonEnumerations
1736bdae907SThomas Monjalon~~~~~~~~~~~~
1746bdae907SThomas Monjalon
1756bdae907SThomas Monjalon* Enumeration values are all uppercase.
1766bdae907SThomas Monjalon
1776bdae907SThomas Monjalon.. code-block:: c
1786bdae907SThomas Monjalon
1796bdae907SThomas Monjalon enum enumtype { ONE, TWO } et;
1806bdae907SThomas Monjalon
1816bdae907SThomas Monjalon* Enum types should be used in preference to macros #defining a set of (sequential) values.
1826bdae907SThomas Monjalon* Enum types should be prefixed with ``rte_`` and the elements by a suitable prefix [generally starting ``RTE_<enum>_`` - where <enum> is a shortname for the enum type] to avoid namespace collisions.
1836bdae907SThomas Monjalon
1846bdae907SThomas MonjalonBitfields
1856bdae907SThomas Monjalon~~~~~~~~~
1866bdae907SThomas Monjalon
1876bdae907SThomas MonjalonThe developer should group bitfields that are included in the same integer, as follows:
1886bdae907SThomas Monjalon
1896bdae907SThomas Monjalon.. code-block:: c
1906bdae907SThomas Monjalon
1916bdae907SThomas Monjalon struct grehdr {
1926bdae907SThomas Monjalon   uint16_t rec:3,
1936bdae907SThomas Monjalon       srr:1,
1946bdae907SThomas Monjalon       seq:1,
1956bdae907SThomas Monjalon       key:1,
1966bdae907SThomas Monjalon       routing:1,
1976bdae907SThomas Monjalon       csum:1,
1986bdae907SThomas Monjalon       version:3,
1996bdae907SThomas Monjalon       reserved:4,
2006bdae907SThomas Monjalon       ack:1;
2016bdae907SThomas Monjalon /* ... */
2026bdae907SThomas Monjalon }
2036bdae907SThomas Monjalon
2046bdae907SThomas MonjalonVariable Declarations
2056bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~
2066bdae907SThomas Monjalon
2076bdae907SThomas MonjalonIn declarations, do not put any whitespace between asterisks and adjacent tokens, except for tokens that are identifiers related to types.
2086bdae907SThomas Monjalon(These identifiers are the names of basic types, type qualifiers, and typedef-names other than the one being declared.)
2096bdae907SThomas MonjalonSeparate these identifiers from asterisks using a single space.
2106bdae907SThomas Monjalon
2116bdae907SThomas MonjalonFor example:
2126bdae907SThomas Monjalon
2136bdae907SThomas Monjalon.. code-block:: c
2146bdae907SThomas Monjalon
2156bdae907SThomas Monjalon   int *x;         /* no space after asterisk */
2166bdae907SThomas Monjalon   int * const x;  /* space after asterisk when using a type qualifier */
2176bdae907SThomas Monjalon
2186bdae907SThomas Monjalon* All externally-visible variables should have an ``rte_`` prefix in the name to avoid namespace collisions.
2196bdae907SThomas Monjalon* Do not use uppercase letters - either in the form of ALL_UPPERCASE, or CamelCase - in variable names.
2206bdae907SThomas Monjalon  Lower-case letters and underscores only.
2216bdae907SThomas Monjalon
2226bdae907SThomas MonjalonStructure Declarations
2236bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~
2246bdae907SThomas Monjalon
2256bdae907SThomas Monjalon* In general, when declaring variables in new structures, declare them sorted by use, then by size (largest to smallest), and then in alphabetical order.
2266bdae907SThomas Monjalon  Sorting by use means that commonly used variables are used together and that the structure layout makes logical sense.
2276bdae907SThomas Monjalon  Ordering by size then ensures that as little padding is added to the structure as possible.
2286bdae907SThomas Monjalon* For existing structures, additions to structures should be added to the end so for backward compatibility reasons.
2296bdae907SThomas Monjalon* Each structure element gets its own line.
2306bdae907SThomas Monjalon* Try to make the structure readable by aligning the member names using spaces as shown below.
2316bdae907SThomas Monjalon* Names following extremely long types, which therefore cannot be easily aligned with the rest, should be separated by a single space.
2326bdae907SThomas Monjalon
2336bdae907SThomas Monjalon.. code-block:: c
2346bdae907SThomas Monjalon
2356bdae907SThomas Monjalon struct foo {
2366bdae907SThomas Monjalon         struct foo      *next;          /* List of active foo. */
2376bdae907SThomas Monjalon         struct mumble   amumble;        /* Comment for mumble. */
2386bdae907SThomas Monjalon         int             bar;            /* Try to align the comments. */
2396bdae907SThomas Monjalon         struct verylongtypename *baz;   /* Won't fit with other members */
2406bdae907SThomas Monjalon };
2416bdae907SThomas Monjalon
2426bdae907SThomas Monjalon
2436bdae907SThomas Monjalon* Major structures should be declared at the top of the file in which they are used, or in separate header files if they are used in multiple source files.
2446bdae907SThomas Monjalon* Use of the structures should be by separate variable declarations and those declarations must be extern if they are declared in a header file.
2456bdae907SThomas Monjalon* Externally visible structure definitions should have the structure name prefixed by ``rte_`` to avoid namespace collisions.
2466bdae907SThomas Monjalon
2476bdae907SThomas MonjalonQueues
2486bdae907SThomas Monjalon~~~~~~
2496bdae907SThomas Monjalon
2506bdae907SThomas MonjalonUse queue(3) macros rather than rolling your own lists, whenever possible.
2516bdae907SThomas MonjalonThus, the previous example would be better written:
2526bdae907SThomas Monjalon
2536bdae907SThomas Monjalon.. code-block:: c
2546bdae907SThomas Monjalon
2556bdae907SThomas Monjalon #include <sys/queue.h>
2566bdae907SThomas Monjalon
2576bdae907SThomas Monjalon struct foo {
2586bdae907SThomas Monjalon         LIST_ENTRY(foo) link;      /* Use queue macros for foo lists. */
2596bdae907SThomas Monjalon         struct mumble   amumble;   /* Comment for mumble. */
2606bdae907SThomas Monjalon         int             bar;       /* Try to align the comments. */
2616bdae907SThomas Monjalon         struct verylongtypename *baz;   /* Won't fit with other members */
2626bdae907SThomas Monjalon };
2636bdae907SThomas Monjalon LIST_HEAD(, foo) foohead;          /* Head of global foo list. */
2646bdae907SThomas Monjalon
2656bdae907SThomas Monjalon
2666bdae907SThomas MonjalonDPDK also provides an optimized way to store elements in lockless rings.
2676bdae907SThomas MonjalonThis should be used in all data-path code, when there are several consumer and/or producers to avoid locking for concurrent access.
2686bdae907SThomas Monjalon
2696bdae907SThomas MonjalonTypedefs
2706bdae907SThomas Monjalon~~~~~~~~
2716bdae907SThomas Monjalon
2726bdae907SThomas MonjalonAvoid using typedefs for structure types.
2736bdae907SThomas Monjalon
2746bdae907SThomas MonjalonFor example, use:
2756bdae907SThomas Monjalon
2766bdae907SThomas Monjalon.. code-block:: c
2776bdae907SThomas Monjalon
2786bdae907SThomas Monjalon struct my_struct_type {
2796bdae907SThomas Monjalon /* ... */
2806bdae907SThomas Monjalon };
2816bdae907SThomas Monjalon
2826bdae907SThomas Monjalon struct my_struct_type my_var;
2836bdae907SThomas Monjalon
2846bdae907SThomas Monjalon
2856bdae907SThomas Monjalonrather than:
2866bdae907SThomas Monjalon
2876bdae907SThomas Monjalon.. code-block:: c
2886bdae907SThomas Monjalon
2896bdae907SThomas Monjalon typedef struct my_struct_type {
2906bdae907SThomas Monjalon /* ... */
2916bdae907SThomas Monjalon } my_struct_type;
2926bdae907SThomas Monjalon
2936bdae907SThomas Monjalon my_struct_type my_var
2946bdae907SThomas Monjalon
2956bdae907SThomas Monjalon
2966bdae907SThomas MonjalonTypedefs are problematic because they do not properly hide their underlying type;
2976bdae907SThomas Monjalonfor example, you need to know if the typedef is the structure itself, as shown above, or a pointer to the structure.
2986bdae907SThomas MonjalonIn addition, they must be declared exactly once, whereas an incomplete structure type can be mentioned as many times as necessary.
2996bdae907SThomas MonjalonTypedefs are difficult to use in stand-alone header files.
3006bdae907SThomas MonjalonThe header that defines the typedef must be included before the header that uses it, or by the header that uses it (which causes namespace pollution),
3016bdae907SThomas Monjalonor there must be a back-door mechanism for obtaining the typedef.
3026bdae907SThomas Monjalon
3036bdae907SThomas MonjalonNote that #defines used instead of typedefs also are problematic (since they do not propagate the pointer type correctly due to direct text replacement).
3046bdae907SThomas MonjalonFor example, ``#define pint int *`` does not work as expected, while ``typedef int *pint`` does work.
3056bdae907SThomas MonjalonAs stated when discussing macros, typedefs should be preferred to macros in cases like this.
3066bdae907SThomas Monjalon
3076bdae907SThomas MonjalonWhen convention requires a typedef; make its name match the struct tag.
3086bdae907SThomas MonjalonAvoid typedefs ending in ``_t``, except as specified in Standard C or by POSIX.
3096bdae907SThomas Monjalon
3106bdae907SThomas Monjalon.. note::
3116bdae907SThomas Monjalon
3126bdae907SThomas Monjalon	It is recommended to use typedefs to define function pointer types, for reasons of code readability.
3136bdae907SThomas Monjalon	This is especially true when the function type is used as a parameter to another function.
3146bdae907SThomas Monjalon
3156bdae907SThomas MonjalonFor example:
3166bdae907SThomas Monjalon
3176bdae907SThomas Monjalon.. code-block:: c
3186bdae907SThomas Monjalon
3196bdae907SThomas Monjalon	/**
3206bdae907SThomas Monjalon	 * Definition of a remote launch function.
3216bdae907SThomas Monjalon	 */
3226bdae907SThomas Monjalon	typedef int (lcore_function_t)(void *);
3236bdae907SThomas Monjalon
3246bdae907SThomas Monjalon	/* launch a function of lcore_function_t type */
3256bdae907SThomas Monjalon	int rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned slave_id);
3266bdae907SThomas Monjalon
3276bdae907SThomas Monjalon
3286bdae907SThomas MonjalonC Indentation
3296bdae907SThomas Monjalon-------------
3306bdae907SThomas Monjalon
3316bdae907SThomas MonjalonGeneral
3326bdae907SThomas Monjalon~~~~~~~
3336bdae907SThomas Monjalon
3346bdae907SThomas Monjalon* Indentation is a hard tab, that is, a tab character, not a sequence of spaces,
3356bdae907SThomas Monjalon
3366bdae907SThomas Monjalon.. note::
3376bdae907SThomas Monjalon
3386bdae907SThomas Monjalon	Global whitespace rule in DPDK, use tabs for indentation, spaces for alignment.
3396bdae907SThomas Monjalon
3406bdae907SThomas Monjalon* Do not put any spaces before a tab for indentation.
3416bdae907SThomas Monjalon* If you have to wrap a long statement, put the operator at the end of the line, and indent again.
3426bdae907SThomas Monjalon* For control statements (if, while, etc.), continuation it is recommended that the next line be indented by two tabs, rather than one,
3436bdae907SThomas Monjalon  to prevent confusion as to whether the second line of the control statement forms part of the statement body or not.
3446bdae907SThomas Monjalon  Alternatively, the line continuation may use additional spaces to line up to an appropriately point on the preceding line, for example, to align to an opening brace.
3456bdae907SThomas Monjalon
3466bdae907SThomas Monjalon.. note::
3476bdae907SThomas Monjalon
3486bdae907SThomas Monjalon	As with all style guidelines, code should match style already in use in an existing file.
3496bdae907SThomas Monjalon
3506bdae907SThomas Monjalon.. code-block:: c
3516bdae907SThomas Monjalon
3526bdae907SThomas Monjalon while (really_long_variable_name_1 == really_long_variable_name_2 &&
3536bdae907SThomas Monjalon     var3 == var4){  /* confusing to read as */
3546bdae907SThomas Monjalon     x = y + z;      /* control stmt body lines up with second line of */
3556bdae907SThomas Monjalon     a = b + c;      /* control statement itself if single indent used */
3566bdae907SThomas Monjalon }
3576bdae907SThomas Monjalon
3586bdae907SThomas Monjalon if (really_long_variable_name_1 == really_long_variable_name_2 &&
3596bdae907SThomas Monjalon         var3 == var4){  /* two tabs used */
3606bdae907SThomas Monjalon     x = y + z;          /* statement body no longer lines up */
3616bdae907SThomas Monjalon     a = b + c;
3626bdae907SThomas Monjalon }
3636bdae907SThomas Monjalon
3646bdae907SThomas Monjalon z = a + really + long + statement + that + needs +
3656bdae907SThomas Monjalon         two + lines + gets + indented + on + the +
3666bdae907SThomas Monjalon         second + and + subsequent + lines;
3676bdae907SThomas Monjalon
3686bdae907SThomas Monjalon
3696bdae907SThomas Monjalon* Do not add whitespace at the end of a line.
3706bdae907SThomas Monjalon
3716bdae907SThomas Monjalon* Do not add whitespace or a blank line at the end of a file.
3726bdae907SThomas Monjalon
3736bdae907SThomas Monjalon
3746bdae907SThomas MonjalonControl Statements and Loops
3756bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3766bdae907SThomas Monjalon
3776bdae907SThomas Monjalon* Include a space after keywords (if, while, for, return, switch).
3786bdae907SThomas Monjalon* Do not use braces (``{`` and ``}``) for control statements with zero or just a single statement, unless that statement is more than a single line in which case the braces are permitted.
3796bdae907SThomas Monjalon
3806bdae907SThomas Monjalon.. code-block:: c
3816bdae907SThomas Monjalon
3826bdae907SThomas Monjalon for (p = buf; *p != '\0'; ++p)
3836bdae907SThomas Monjalon         ;       /* nothing */
3846bdae907SThomas Monjalon for (;;)
3856bdae907SThomas Monjalon         stmt;
3866bdae907SThomas Monjalon for (;;) {
3876bdae907SThomas Monjalon         z = a + really + long + statement + that + needs +
3886bdae907SThomas Monjalon                 two + lines + gets + indented + on + the +
3896bdae907SThomas Monjalon                 second + and + subsequent + lines;
3906bdae907SThomas Monjalon }
3916bdae907SThomas Monjalon for (;;) {
3926bdae907SThomas Monjalon         if (cond)
3936bdae907SThomas Monjalon                 stmt;
3946bdae907SThomas Monjalon }
3956bdae907SThomas Monjalon if (val != NULL)
3966bdae907SThomas Monjalon         val = realloc(val, newsize);
3976bdae907SThomas Monjalon
3986bdae907SThomas Monjalon
3996bdae907SThomas Monjalon* Parts of a for loop may be left empty.
4006bdae907SThomas Monjalon
4016bdae907SThomas Monjalon.. code-block:: c
4026bdae907SThomas Monjalon
4036bdae907SThomas Monjalon for (; cnt < 15; cnt++) {
4046bdae907SThomas Monjalon         stmt1;
4056bdae907SThomas Monjalon         stmt2;
4066bdae907SThomas Monjalon }
4076bdae907SThomas Monjalon
4086bdae907SThomas Monjalon* Closing and opening braces go on the same line as the else keyword.
4096bdae907SThomas Monjalon* Braces that are not necessary should be left out.
4106bdae907SThomas Monjalon
4116bdae907SThomas Monjalon.. code-block:: c
4126bdae907SThomas Monjalon
4136bdae907SThomas Monjalon if (test)
4146bdae907SThomas Monjalon         stmt;
4156bdae907SThomas Monjalon else if (bar) {
4166bdae907SThomas Monjalon         stmt;
4176bdae907SThomas Monjalon         stmt;
4186bdae907SThomas Monjalon } else
4196bdae907SThomas Monjalon         stmt;
4206bdae907SThomas Monjalon
4216bdae907SThomas Monjalon
4226bdae907SThomas MonjalonFunction Calls
4236bdae907SThomas Monjalon~~~~~~~~~~~~~~
4246bdae907SThomas Monjalon
4256bdae907SThomas Monjalon* Do not use spaces after function names.
4266bdae907SThomas Monjalon* Commas should have a space after them.
4276bdae907SThomas Monjalon* No spaces after ``(`` or ``[`` or preceding the ``]`` or ``)`` characters.
4286bdae907SThomas Monjalon
4296bdae907SThomas Monjalon.. code-block:: c
4306bdae907SThomas Monjalon
4316bdae907SThomas Monjalon	error = function(a1, a2);
4326bdae907SThomas Monjalon	if (error != 0)
4336bdae907SThomas Monjalon		exit(error);
4346bdae907SThomas Monjalon
4356bdae907SThomas Monjalon
4366bdae907SThomas MonjalonOperators
4376bdae907SThomas Monjalon~~~~~~~~~
4386bdae907SThomas Monjalon
4396bdae907SThomas Monjalon* Unary operators do not require spaces, binary operators do.
4406bdae907SThomas Monjalon* Do not use parentheses unless they are required for precedence or unless the statement is confusing without them.
4416bdae907SThomas Monjalon  However, remember that other people may be more easily confused than you.
4426bdae907SThomas Monjalon
4436bdae907SThomas MonjalonExit
4446bdae907SThomas Monjalon~~~~
4456bdae907SThomas Monjalon
4466bdae907SThomas MonjalonExits should be 0 on success, or 1 on failure.
4476bdae907SThomas Monjalon
4486bdae907SThomas Monjalon.. code-block:: c
4496bdae907SThomas Monjalon
4506bdae907SThomas Monjalon         exit(0);        /*
4516bdae907SThomas Monjalon                          * Avoid obvious comments such as
4526bdae907SThomas Monjalon                          * "Exit 0 on success."
4536bdae907SThomas Monjalon                          */
4546bdae907SThomas Monjalon }
4556bdae907SThomas Monjalon
4566bdae907SThomas MonjalonLocal Variables
4576bdae907SThomas Monjalon~~~~~~~~~~~~~~~
4586bdae907SThomas Monjalon
4596bdae907SThomas Monjalon* Variables should be declared at the start of a block of code rather than in the middle.
4606bdae907SThomas Monjalon  The exception to this is when the variable is ``const`` in which case the declaration must be at the point of first use/assignment.
4616bdae907SThomas Monjalon* When declaring variables in functions, multiple variables per line are OK.
4626bdae907SThomas Monjalon  However, if multiple declarations would cause the line to exceed a reasonable line length, begin a new set of declarations on the next line rather than using a line continuation.
4636bdae907SThomas Monjalon* Be careful to not obfuscate the code by initializing variables in the declarations, only the last variable on a line should be initialized.
4642fe68f32SJohn McNamara  If multiple variables are to be initialized when defined, put one per line.
4656bdae907SThomas Monjalon* Do not use function calls in initializers, except for ``const`` variables.
4666bdae907SThomas Monjalon
4676bdae907SThomas Monjalon.. code-block:: c
4686bdae907SThomas Monjalon
4696bdae907SThomas Monjalon int i = 0, j = 0, k = 0;  /* bad, too many initializer */
4706bdae907SThomas Monjalon
4716bdae907SThomas Monjalon char a = 0;        /* OK, one variable per line with initializer */
4726bdae907SThomas Monjalon char b = 0;
4736bdae907SThomas Monjalon
4746bdae907SThomas Monjalon float x, y = 0.0;  /* OK, only last variable has initializer */
4756bdae907SThomas Monjalon
4766bdae907SThomas Monjalon
4776bdae907SThomas MonjalonCasts and sizeof
4786bdae907SThomas Monjalon~~~~~~~~~~~~~~~~
4796bdae907SThomas Monjalon
4806bdae907SThomas Monjalon* Casts and sizeof statements are not followed by a space.
4816bdae907SThomas Monjalon* Always write sizeof statements with parenthesis.
4826bdae907SThomas Monjalon  The redundant parenthesis rules do not apply to sizeof(var) instances.
4836bdae907SThomas Monjalon
4846bdae907SThomas MonjalonC Function Definition, Declaration and Use
4856bdae907SThomas Monjalon-------------------------------------------
4866bdae907SThomas Monjalon
4876bdae907SThomas MonjalonPrototypes
4886bdae907SThomas Monjalon~~~~~~~~~~
4896bdae907SThomas Monjalon
4906bdae907SThomas Monjalon* It is recommended (and generally required by the compiler) that all non-static functions are prototyped somewhere.
4916bdae907SThomas Monjalon* Functions local to one source module should be declared static, and should not be prototyped unless absolutely necessary.
4926bdae907SThomas Monjalon* Functions used from other parts of code (external API) must be prototyped in the relevant include file.
4936bdae907SThomas Monjalon* Function prototypes should be listed in a logical order, preferably alphabetical unless there is a compelling reason to use a different ordering.
4946bdae907SThomas Monjalon* Functions that are used locally in more than one module go into a separate header file, for example, "extern.h".
4956bdae907SThomas Monjalon* Do not use the ``__P`` macro.
4966bdae907SThomas Monjalon* Functions that are part of an external API should be documented using Doxygen-like comments above declarations. See :ref:`doxygen_guidelines` for details.
4976bdae907SThomas Monjalon* Functions that are part of the external API must have an ``rte_`` prefix on the function name.
4986bdae907SThomas Monjalon* Do not use uppercase letters - either in the form of ALL_UPPERCASE, or CamelCase - in function names. Lower-case letters and underscores only.
4996bdae907SThomas Monjalon* When prototyping functions, associate names with parameter types, for example:
5006bdae907SThomas Monjalon
5016bdae907SThomas Monjalon.. code-block:: c
5026bdae907SThomas Monjalon
5036bdae907SThomas Monjalon void function1(int fd); /* good */
5046bdae907SThomas Monjalon void function2(int);    /* bad */
5056bdae907SThomas Monjalon
5066bdae907SThomas Monjalon* Short function prototypes should be contained on a single line.
5076bdae907SThomas Monjalon  Longer prototypes, e.g. those with many parameters, can be split across multiple lines.
5086bdae907SThomas Monjalon  The second and subsequent lines should be further indented as for line statement continuations as described in the previous section.
5096bdae907SThomas Monjalon
5106bdae907SThomas Monjalon.. code-block:: c
5116bdae907SThomas Monjalon
5126bdae907SThomas Monjalon static char *function1(int _arg, const char *_arg2,
5136bdae907SThomas Monjalon        struct foo *_arg3,
5146bdae907SThomas Monjalon        struct bar *_arg4,
5156bdae907SThomas Monjalon        struct baz *_arg5);
5166bdae907SThomas Monjalon static void usage(void);
5176bdae907SThomas Monjalon
5186bdae907SThomas Monjalon.. note::
5196bdae907SThomas Monjalon
5206bdae907SThomas Monjalon	Unlike function definitions, the function prototypes do not need to place the function return type on a separate line.
5216bdae907SThomas Monjalon
5226bdae907SThomas MonjalonDefinitions
5236bdae907SThomas Monjalon~~~~~~~~~~~
5246bdae907SThomas Monjalon
5256bdae907SThomas Monjalon* The function type should be on a line by itself preceding the function.
5266bdae907SThomas Monjalon* The opening brace of the function body should be on a line by itself.
5276bdae907SThomas Monjalon
5286bdae907SThomas Monjalon.. code-block:: c
5296bdae907SThomas Monjalon
5306bdae907SThomas Monjalon static char *
5316bdae907SThomas Monjalon function(int a1, int a2, float fl, int a4)
5326bdae907SThomas Monjalon {
5336bdae907SThomas Monjalon
5346bdae907SThomas Monjalon
5356bdae907SThomas Monjalon* Do not declare functions inside other functions.
5366bdae907SThomas Monjalon  ANSI C states that such declarations have file scope regardless of the nesting of the declaration.
5376bdae907SThomas Monjalon  Hiding file declarations in what appears to be a local scope is undesirable and will elicit complaints from a good compiler.
5386bdae907SThomas Monjalon* Old-style (K&R) function declaration should not be used, use ANSI function declarations instead as shown below.
5396bdae907SThomas Monjalon* Long argument lists should be wrapped as described above in the function prototypes section.
5406bdae907SThomas Monjalon
5416bdae907SThomas Monjalon.. code-block:: c
5426bdae907SThomas Monjalon
5436bdae907SThomas Monjalon /*
5446bdae907SThomas Monjalon  * All major routines should have a comment briefly describing what
5456bdae907SThomas Monjalon  * they do. The comment before the "main" routine should describe
5466bdae907SThomas Monjalon  * what the program does.
5476bdae907SThomas Monjalon  */
5486bdae907SThomas Monjalon int
5496bdae907SThomas Monjalon main(int argc, char *argv[])
5506bdae907SThomas Monjalon {
5516bdae907SThomas Monjalon         char *ep;
5526bdae907SThomas Monjalon         long num;
5536bdae907SThomas Monjalon         int ch;
5546bdae907SThomas Monjalon
5556bdae907SThomas MonjalonC Statement Style and Conventions
5566bdae907SThomas Monjalon---------------------------------
5576bdae907SThomas Monjalon
5586bdae907SThomas MonjalonNULL Pointers
5596bdae907SThomas Monjalon~~~~~~~~~~~~~
5606bdae907SThomas Monjalon
5616bdae907SThomas Monjalon* NULL is the preferred null pointer constant.
5626bdae907SThomas Monjalon  Use NULL instead of ``(type *)0`` or ``(type *)NULL``, except where the compiler does not know the destination type e.g. for variadic args to a function.
5636bdae907SThomas Monjalon* Test pointers against NULL, for example, use:
5646bdae907SThomas Monjalon
5656bdae907SThomas Monjalon.. code-block:: c
5666bdae907SThomas Monjalon
5676bdae907SThomas Monjalon if (p == NULL) /* Good, compare pointer to NULL */
5686bdae907SThomas Monjalon
5696bdae907SThomas Monjalon if (!p) /* Bad, using ! on pointer */
5706bdae907SThomas Monjalon
5716bdae907SThomas Monjalon
5726bdae907SThomas Monjalon* Do not use ! for tests unless it is a boolean, for example, use:
5736bdae907SThomas Monjalon
5746bdae907SThomas Monjalon.. code-block:: c
5756bdae907SThomas Monjalon
5766bdae907SThomas Monjalon	if (*p == '\0') /* check character against (char)0 */
5776bdae907SThomas Monjalon
5786bdae907SThomas MonjalonReturn Value
5796bdae907SThomas Monjalon~~~~~~~~~~~~
5806bdae907SThomas Monjalon
5816bdae907SThomas Monjalon* Functions which create objects, or allocate memory, should return pointer types, and NULL on error.
5826bdae907SThomas Monjalon  The error type should be indicated may setting the variable ``rte_errno`` appropriately.
5836bdae907SThomas Monjalon* Functions which work on bursts of packets, such as RX-like or TX-like functions, should return the number of packets handled.
5846bdae907SThomas Monjalon* Other functions returning int should generally behave like system calls:
5856bdae907SThomas Monjalon  returning 0 on success and -1 on error, setting ``rte_errno`` to indicate the specific type of error.
5866bdae907SThomas Monjalon* Where already standard in a given library, the alternative error approach may be used where the negative value is not -1 but is instead ``-errno`` if relevant, for example, ``-EINVAL``.
5876bdae907SThomas Monjalon  Note, however, to allow consistency across functions returning integer or pointer types, the previous approach is preferred for any new libraries.
5886bdae907SThomas Monjalon* For functions where no error is possible, the function type should be ``void`` not ``int``.
5896bdae907SThomas Monjalon* Routines returning ``void *`` should not have their return values cast to any pointer type.
5906bdae907SThomas Monjalon  (Typecasting can prevent the compiler from warning about missing prototypes as any implicit definition of a function returns int,
5916bdae907SThomas Monjalon  which, unlike ``void *``, needs a typecast to assign to a pointer variable.)
5926bdae907SThomas Monjalon
5936bdae907SThomas Monjalon.. note::
5946bdae907SThomas Monjalon
5956bdae907SThomas Monjalon	The above rule about not typecasting ``void *`` applies to malloc, as well as to DPDK functions.
5966bdae907SThomas Monjalon
5976bdae907SThomas Monjalon* Values in return statements should not be enclosed in parentheses.
5986bdae907SThomas Monjalon
5996bdae907SThomas MonjalonLogging and Errors
6006bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~
6016bdae907SThomas Monjalon
6026bdae907SThomas MonjalonIn the DPDK environment, use the logging interface provided:
6036bdae907SThomas Monjalon
6046bdae907SThomas Monjalon.. code-block:: c
6056bdae907SThomas Monjalon
6066bdae907SThomas Monjalon #define RTE_LOGTYPE_TESTAPP1 RTE_LOGTYPE_USER1
6076bdae907SThomas Monjalon #define RTE_LOGTYPE_TESTAPP2 RTE_LOGTYPE_USER2
6086bdae907SThomas Monjalon
6096bdae907SThomas Monjalon /* enable these logs type */
6106bdae907SThomas Monjalon rte_set_log_type(RTE_LOGTYPE_TESTAPP1, 1);
6116bdae907SThomas Monjalon rte_set_log_type(RTE_LOGTYPE_TESTAPP2, 1);
6126bdae907SThomas Monjalon
6136bdae907SThomas Monjalon /* log in debug level */
6146bdae907SThomas Monjalon rte_set_log_level(RTE_LOG_DEBUG);
6156bdae907SThomas Monjalon RTE_LOG(DEBUG, TESTAPP1, "this is is a debug level message\n");
6166bdae907SThomas Monjalon RTE_LOG(INFO, TESTAPP1, "this is is a info level message\n");
6176bdae907SThomas Monjalon RTE_LOG(WARNING, TESTAPP1, "this is is a warning level message\n");
6186bdae907SThomas Monjalon
6196bdae907SThomas Monjalon /* log in info level */
6206bdae907SThomas Monjalon rte_set_log_level(RTE_LOG_INFO);
6216bdae907SThomas Monjalon RTE_LOG(DEBUG, TESTAPP2, "debug level message (not displayed)\n");
6226bdae907SThomas Monjalon
6236bdae907SThomas MonjalonBranch Prediction
6246bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~
6256bdae907SThomas Monjalon
6266bdae907SThomas Monjalon* When a test is done in a critical zone (called often or in a data path) the code can use the ``likely()`` and ``unlikely()`` macros to indicate the expected, or preferred fast path.
6276bdae907SThomas Monjalon  They are expanded as a compiler builtin and allow the developer to indicate if the branch is likely to be taken or not. Example:
6286bdae907SThomas Monjalon
6296bdae907SThomas Monjalon.. code-block:: c
6306bdae907SThomas Monjalon
6316bdae907SThomas Monjalon #include <rte_branch_prediction.h>
6326bdae907SThomas Monjalon if (likely(x > 1))
6336bdae907SThomas Monjalon   do_stuff();
6346bdae907SThomas Monjalon
6356bdae907SThomas Monjalon.. note::
6366bdae907SThomas Monjalon
6376bdae907SThomas Monjalon	The use of ``likely()`` and ``unlikely()`` should only be done in performance critical paths,
6386bdae907SThomas Monjalon	and only when there is a clearly preferred path, or a measured performance increase gained from doing so.
6396bdae907SThomas Monjalon	These macros should be avoided in non-performance-critical code.
6406bdae907SThomas Monjalon
6416bdae907SThomas MonjalonStatic Variables and Functions
6426bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6436bdae907SThomas Monjalon
6446bdae907SThomas Monjalon* All functions and variables that are local to a file must be declared as ``static`` because it can often help the compiler to do some optimizations (such as, inlining the code).
6456bdae907SThomas Monjalon* Functions that should be inlined should to be declared as ``static inline`` and can be defined in a .c or a .h file.
6466bdae907SThomas Monjalon
6476bdae907SThomas Monjalon.. note::
6486bdae907SThomas Monjalon	Static functions defined in a header file must be declared as ``static inline`` in order to prevent compiler warnings about the function being unused.
6496bdae907SThomas Monjalon
6506bdae907SThomas MonjalonConst Attribute
6516bdae907SThomas Monjalon~~~~~~~~~~~~~~~
6526bdae907SThomas Monjalon
6536bdae907SThomas MonjalonThe ``const`` attribute should be used as often as possible when a variable is read-only.
6546bdae907SThomas Monjalon
6556bdae907SThomas MonjalonInline ASM in C code
6566bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~~~
6576bdae907SThomas Monjalon
6586bdae907SThomas MonjalonThe ``asm`` and ``volatile`` keywords do not have underscores. The AT&T syntax should be used.
6596bdae907SThomas MonjalonInput and output operands should be named to avoid confusion, as shown in the following example:
6606bdae907SThomas Monjalon
6616bdae907SThomas Monjalon.. code-block:: c
6626bdae907SThomas Monjalon
6636bdae907SThomas Monjalon	asm volatile("outb %[val], %[port]"
6646bdae907SThomas Monjalon		: :
6656bdae907SThomas Monjalon		[port] "dN" (port),
6666bdae907SThomas Monjalon		[val] "a" (val));
6676bdae907SThomas Monjalon
6686bdae907SThomas MonjalonControl Statements
6696bdae907SThomas Monjalon~~~~~~~~~~~~~~~~~~
6706bdae907SThomas Monjalon
6716bdae907SThomas Monjalon* Forever loops are done with for statements, not while statements.
6726bdae907SThomas Monjalon* Elements in a switch statement that cascade should have a FALLTHROUGH comment. For example:
6736bdae907SThomas Monjalon
6746bdae907SThomas Monjalon.. code-block:: c
6756bdae907SThomas Monjalon
6766bdae907SThomas Monjalon         switch (ch) {         /* Indent the switch. */
6776bdae907SThomas Monjalon         case 'a':             /* Don't indent the case. */
6786bdae907SThomas Monjalon                 aflag = 1;    /* Indent case body one tab. */
6796bdae907SThomas Monjalon                 /* FALLTHROUGH */
6806bdae907SThomas Monjalon         case 'b':
6816bdae907SThomas Monjalon                 bflag = 1;
6826bdae907SThomas Monjalon                 break;
6836bdae907SThomas Monjalon         case '?':
6846bdae907SThomas Monjalon         default:
6856bdae907SThomas Monjalon                 usage();
6866bdae907SThomas Monjalon                 /* NOTREACHED */
6876bdae907SThomas Monjalon         }
688d1a22085SJohn McNamara
689d1a22085SJohn McNamara
690d1a22085SJohn McNamaraPython Code
691d1a22085SJohn McNamara-----------
692d1a22085SJohn McNamara
693*d76a5927SJohn McNamaraAll Python code should work with Python 2.7+ and 3.2+ and be compliant with
694*d76a5927SJohn McNamara`PEP8 (Style Guide for Python Code) <https://www.python.org/dev/peps/pep-0008/>`_.
695d1a22085SJohn McNamara
696d1a22085SJohn McNamaraThe ``pep8`` tool can be used for testing compliance with the guidelines.
697