1d30ea906Sjfb8856606.. SPDX-License-Identifier: BSD-3-Clause 2d30ea906Sjfb8856606 Copyright 2018 The DPDK contributors 3d30ea906Sjfb8856606 4a9643ea8Slogwang.. _coding_style: 5a9643ea8Slogwang 6a9643ea8SlogwangDPDK Coding Style 7a9643ea8Slogwang================= 8a9643ea8Slogwang 9a9643ea8SlogwangDescription 10a9643ea8Slogwang----------- 11a9643ea8Slogwang 12a9643ea8SlogwangThis document specifies the preferred style for source files in the DPDK source tree. 13a9643ea8SlogwangIt 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. 14a9643ea8Slogwang 15a9643ea8SlogwangGeneral Guidelines 16a9643ea8Slogwang------------------ 17a9643ea8Slogwang 18a9643ea8SlogwangThe rules and guidelines given in this document cannot cover every situation, so the following general guidelines should be used as a fallback: 19a9643ea8Slogwang 20a9643ea8Slogwang* The code style should be consistent within each individual file. 21a9643ea8Slogwang* In the case of creating new files, the style should be consistent within each file in a given directory or module. 22a9643ea8Slogwang* 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. 23a9643ea8Slogwang 24a9643ea8SlogwangLine length is recommended to be not more than 80 characters, including comments. 25a9643ea8Slogwang[Tab stop size should be assumed to be 8-characters wide]. 26a9643ea8Slogwang 27a9643ea8Slogwang.. note:: 28a9643ea8Slogwang 29a9643ea8Slogwang The above is recommendation, and not a hard limit. 30a9643ea8Slogwang However, it is expected that the recommendations should be followed in all but the rarest situations. 31a9643ea8Slogwang 32a9643ea8SlogwangC Comment Style 33a9643ea8Slogwang--------------- 34a9643ea8Slogwang 35a9643ea8SlogwangUsual Comments 36a9643ea8Slogwang~~~~~~~~~~~~~~ 37a9643ea8Slogwang 38a9643ea8SlogwangThese comments should be used in normal cases. 39a9643ea8SlogwangTo document a public API, a doxygen-like format must be used: refer to :ref:`doxygen_guidelines`. 40a9643ea8Slogwang 41a9643ea8Slogwang.. code-block:: c 42a9643ea8Slogwang 43a9643ea8Slogwang /* 44a9643ea8Slogwang * VERY important single-line comments look like this. 45a9643ea8Slogwang */ 46a9643ea8Slogwang 47a9643ea8Slogwang /* Most single-line comments look like this. */ 48a9643ea8Slogwang 49a9643ea8Slogwang /* 50a9643ea8Slogwang * Multi-line comments look like this. Make them real sentences. Fill 51a9643ea8Slogwang * them so they look like real paragraphs. 52a9643ea8Slogwang */ 53a9643ea8Slogwang 54a9643ea8SlogwangLicense Header 55a9643ea8Slogwang~~~~~~~~~~~~~~ 56a9643ea8Slogwang 57*2d9fd380Sjfb8856606Each file must begin with a special comment containing the 58*2d9fd380Sjfb8856606`Software Package Data Exchange (SPDX) License Identfier <https://spdx.org/using-spdx-license-identifier>`_. 59*2d9fd380Sjfb8856606 60*2d9fd380Sjfb8856606Generally this is the BSD License, except for code granted special exceptions. 61*2d9fd380Sjfb8856606The SPDX licences identifier is sufficient, a file should not contain 62*2d9fd380Sjfb8856606an additional text version of the license (boilerplate). 63*2d9fd380Sjfb8856606 64a9643ea8SlogwangAfter any copyright header, a blank line should be left before any other contents, e.g. include statements in a C file. 65a9643ea8Slogwang 66a9643ea8SlogwangC Preprocessor Directives 67a9643ea8Slogwang------------------------- 68a9643ea8Slogwang 69a9643ea8SlogwangHeader Includes 70a9643ea8Slogwang~~~~~~~~~~~~~~~ 71a9643ea8Slogwang 72a9643ea8SlogwangIn DPDK sources, the include files should be ordered as following: 73a9643ea8Slogwang 74a9643ea8Slogwang#. libc includes (system includes first) 75a9643ea8Slogwang#. DPDK EAL includes 76a9643ea8Slogwang#. DPDK misc libraries includes 77a9643ea8Slogwang#. application-specific includes 78a9643ea8Slogwang 79a9643ea8SlogwangInclude files from the local application directory are included using quotes, while includes from other paths are included using angle brackets: "<>". 80a9643ea8Slogwang 81a9643ea8SlogwangExample: 82a9643ea8Slogwang 83a9643ea8Slogwang.. code-block:: c 84a9643ea8Slogwang 85a9643ea8Slogwang #include <stdio.h> 86a9643ea8Slogwang #include <stdlib.h> 87a9643ea8Slogwang 88a9643ea8Slogwang #include <rte_eal.h> 89a9643ea8Slogwang 90a9643ea8Slogwang #include <rte_ring.h> 91a9643ea8Slogwang #include <rte_mempool.h> 92a9643ea8Slogwang 93a9643ea8Slogwang #include "application.h" 94a9643ea8Slogwang 95a9643ea8SlogwangHeader File Guards 96a9643ea8Slogwang~~~~~~~~~~~~~~~~~~ 97a9643ea8Slogwang 98a9643ea8SlogwangHeaders should be protected against multiple inclusion with the usual: 99a9643ea8Slogwang 100a9643ea8Slogwang.. code-block:: c 101a9643ea8Slogwang 102a9643ea8Slogwang #ifndef _FILE_H_ 103a9643ea8Slogwang #define _FILE_H_ 104a9643ea8Slogwang 105a9643ea8Slogwang /* Code */ 106a9643ea8Slogwang 107a9643ea8Slogwang #endif /* _FILE_H_ */ 108a9643ea8Slogwang 109a9643ea8Slogwang 110a9643ea8SlogwangMacros 111a9643ea8Slogwang~~~~~~ 112a9643ea8Slogwang 113a9643ea8SlogwangDo not ``#define`` or declare names except with the standard DPDK prefix: ``RTE_``. 114a9643ea8SlogwangThis is to ensure there are no collisions with definitions in the application itself. 115a9643ea8Slogwang 116a9643ea8SlogwangThe names of "unsafe" macros (ones that have side effects), and the names of macros for manifest constants, are all in uppercase. 117a9643ea8Slogwang 118a9643ea8SlogwangThe expansions of expression-like macros are either a single token or have outer parentheses. 119a9643ea8SlogwangIf 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. 120a9643ea8SlogwangIf the macro encapsulates a compound statement, enclose it in a do-while loop, so that it can be used safely in if statements. 121a9643ea8SlogwangAny final statement-terminating semicolon should be supplied by the macro invocation rather than the macro, to make parsing easier for pretty-printers and editors. 122a9643ea8Slogwang 123a9643ea8SlogwangFor example: 124a9643ea8Slogwang 125a9643ea8Slogwang.. code-block:: c 126a9643ea8Slogwang 127a9643ea8Slogwang #define MACRO(x, y) do { \ 128a9643ea8Slogwang variable = (x) + (y); \ 129a9643ea8Slogwang (y) += 2; \ 130a9643ea8Slogwang } while(0) 131a9643ea8Slogwang 132a9643ea8Slogwang.. note:: 133a9643ea8Slogwang 134a9643ea8Slogwang 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. 135a9643ea8Slogwang 136a9643ea8SlogwangConditional Compilation 137a9643ea8Slogwang~~~~~~~~~~~~~~~~~~~~~~~ 138a9643ea8Slogwang 139a9643ea8Slogwang* When code is conditionally compiled using ``#ifdef`` or ``#if``, a comment may be added following the matching 140a9643ea8Slogwang ``#endif`` or ``#else`` to permit the reader to easily discern where conditionally compiled code regions end. 141a9643ea8Slogwang* 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. 142a9643ea8Slogwang 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. 143a9643ea8Slogwang* The comment should be separated from the ``#endif`` or ``#else`` by a single space. 144a9643ea8Slogwang* For short conditionally compiled regions, a closing comment should not be used. 145a9643ea8Slogwang* The comment for ``#endif`` should match the expression used in the corresponding ``#if`` or ``#ifdef``. 146a9643ea8Slogwang* The comment for ``#else`` and ``#elif`` should match the inverse of the expression(s) used in the preceding ``#if`` and/or ``#elif`` statements. 147a9643ea8Slogwang* In the comments, the subexpression ``defined(FOO)`` is abbreviated as "FOO". 148a9643ea8Slogwang For the purposes of comments, ``#ifndef FOO`` is treated as ``#if !defined(FOO)``. 149a9643ea8Slogwang 150a9643ea8Slogwang.. code-block:: c 151a9643ea8Slogwang 152a9643ea8Slogwang #ifdef KTRACE 153a9643ea8Slogwang #include <sys/ktrace.h> 154a9643ea8Slogwang #endif 155a9643ea8Slogwang 156a9643ea8Slogwang #ifdef COMPAT_43 157a9643ea8Slogwang /* A large region here, or other conditional code. */ 158a9643ea8Slogwang #else /* !COMPAT_43 */ 159a9643ea8Slogwang /* Or here. */ 160a9643ea8Slogwang #endif /* COMPAT_43 */ 161a9643ea8Slogwang 162a9643ea8Slogwang #ifndef COMPAT_43 163a9643ea8Slogwang /* Yet another large region here, or other conditional code. */ 164a9643ea8Slogwang #else /* COMPAT_43 */ 165a9643ea8Slogwang /* Or here. */ 166a9643ea8Slogwang #endif /* !COMPAT_43 */ 167a9643ea8Slogwang 168a9643ea8Slogwang.. note:: 169a9643ea8Slogwang 170a9643ea8Slogwang Conditional compilation should be used only when absolutely necessary, as it increases the number of target binaries that need to be built and tested. 171a9643ea8Slogwang 172a9643ea8SlogwangC Types 173a9643ea8Slogwang------- 174a9643ea8Slogwang 175a9643ea8SlogwangIntegers 176a9643ea8Slogwang~~~~~~~~ 177a9643ea8Slogwang 178a9643ea8SlogwangFor 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. 179a9643ea8Slogwang 180a9643ea8SlogwangEnumerations 181a9643ea8Slogwang~~~~~~~~~~~~ 182a9643ea8Slogwang 183a9643ea8Slogwang* Enumeration values are all uppercase. 184a9643ea8Slogwang 185a9643ea8Slogwang.. code-block:: c 186a9643ea8Slogwang 187a9643ea8Slogwang enum enumtype { ONE, TWO } et; 188a9643ea8Slogwang 189a9643ea8Slogwang* Enum types should be used in preference to macros #defining a set of (sequential) values. 190a9643ea8Slogwang* 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. 191a9643ea8Slogwang 192a9643ea8SlogwangBitfields 193a9643ea8Slogwang~~~~~~~~~ 194a9643ea8Slogwang 195a9643ea8SlogwangThe developer should group bitfields that are included in the same integer, as follows: 196a9643ea8Slogwang 197a9643ea8Slogwang.. code-block:: c 198a9643ea8Slogwang 199a9643ea8Slogwang struct grehdr { 200a9643ea8Slogwang uint16_t rec:3, 201a9643ea8Slogwang srr:1, 202a9643ea8Slogwang seq:1, 203a9643ea8Slogwang key:1, 204a9643ea8Slogwang routing:1, 205a9643ea8Slogwang csum:1, 206a9643ea8Slogwang version:3, 207a9643ea8Slogwang reserved:4, 208a9643ea8Slogwang ack:1; 209a9643ea8Slogwang /* ... */ 210a9643ea8Slogwang } 211a9643ea8Slogwang 212a9643ea8SlogwangVariable Declarations 213a9643ea8Slogwang~~~~~~~~~~~~~~~~~~~~~ 214a9643ea8Slogwang 215a9643ea8SlogwangIn declarations, do not put any whitespace between asterisks and adjacent tokens, except for tokens that are identifiers related to types. 216a9643ea8Slogwang(These identifiers are the names of basic types, type qualifiers, and typedef-names other than the one being declared.) 217a9643ea8SlogwangSeparate these identifiers from asterisks using a single space. 218a9643ea8Slogwang 219a9643ea8SlogwangFor example: 220a9643ea8Slogwang 221a9643ea8Slogwang.. code-block:: c 222a9643ea8Slogwang 223a9643ea8Slogwang int *x; /* no space after asterisk */ 224a9643ea8Slogwang int * const x; /* space after asterisk when using a type qualifier */ 225a9643ea8Slogwang 226a9643ea8Slogwang* All externally-visible variables should have an ``rte_`` prefix in the name to avoid namespace collisions. 227a9643ea8Slogwang* Do not use uppercase letters - either in the form of ALL_UPPERCASE, or CamelCase - in variable names. 228a9643ea8Slogwang Lower-case letters and underscores only. 229a9643ea8Slogwang 230a9643ea8SlogwangStructure Declarations 231a9643ea8Slogwang~~~~~~~~~~~~~~~~~~~~~~ 232a9643ea8Slogwang 233a9643ea8Slogwang* In general, when declaring variables in new structures, declare them sorted by use, then by size (largest to smallest), and then in alphabetical order. 234a9643ea8Slogwang Sorting by use means that commonly used variables are used together and that the structure layout makes logical sense. 235a9643ea8Slogwang Ordering by size then ensures that as little padding is added to the structure as possible. 236a9643ea8Slogwang* For existing structures, additions to structures should be added to the end so for backward compatibility reasons. 237a9643ea8Slogwang* Each structure element gets its own line. 238a9643ea8Slogwang* Try to make the structure readable by aligning the member names using spaces as shown below. 239a9643ea8Slogwang* Names following extremely long types, which therefore cannot be easily aligned with the rest, should be separated by a single space. 240a9643ea8Slogwang 241a9643ea8Slogwang.. code-block:: c 242a9643ea8Slogwang 243a9643ea8Slogwang struct foo { 244a9643ea8Slogwang struct foo *next; /* List of active foo. */ 245a9643ea8Slogwang struct mumble amumble; /* Comment for mumble. */ 246a9643ea8Slogwang int bar; /* Try to align the comments. */ 247a9643ea8Slogwang struct verylongtypename *baz; /* Won't fit with other members */ 248a9643ea8Slogwang }; 249a9643ea8Slogwang 250a9643ea8Slogwang 251a9643ea8Slogwang* 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. 252a9643ea8Slogwang* Use of the structures should be by separate variable declarations and those declarations must be extern if they are declared in a header file. 253a9643ea8Slogwang* Externally visible structure definitions should have the structure name prefixed by ``rte_`` to avoid namespace collisions. 254a9643ea8Slogwang 255d30ea906Sjfb8856606.. note:: 256d30ea906Sjfb8856606 257d30ea906Sjfb8856606 Uses of ``bool`` in structures are not preferred as is wastes space and 258d30ea906Sjfb8856606 it's also not clear as to what type size the bool is. A preferred use of 259d30ea906Sjfb8856606 ``bool`` is mainly as a return type from functions that return true/false, 260d30ea906Sjfb8856606 and maybe local variable functions. 261d30ea906Sjfb8856606 262d30ea906Sjfb8856606 Ref: `LKML <https://lkml.org/lkml/2017/11/21/384>`_ 263d30ea906Sjfb8856606 264a9643ea8SlogwangQueues 265a9643ea8Slogwang~~~~~~ 266a9643ea8Slogwang 267a9643ea8SlogwangUse queue(3) macros rather than rolling your own lists, whenever possible. 268a9643ea8SlogwangThus, the previous example would be better written: 269a9643ea8Slogwang 270a9643ea8Slogwang.. code-block:: c 271a9643ea8Slogwang 272a9643ea8Slogwang #include <sys/queue.h> 273a9643ea8Slogwang 274a9643ea8Slogwang struct foo { 275a9643ea8Slogwang LIST_ENTRY(foo) link; /* Use queue macros for foo lists. */ 276a9643ea8Slogwang struct mumble amumble; /* Comment for mumble. */ 277a9643ea8Slogwang int bar; /* Try to align the comments. */ 278a9643ea8Slogwang struct verylongtypename *baz; /* Won't fit with other members */ 279a9643ea8Slogwang }; 280a9643ea8Slogwang LIST_HEAD(, foo) foohead; /* Head of global foo list. */ 281a9643ea8Slogwang 282a9643ea8Slogwang 283a9643ea8SlogwangDPDK also provides an optimized way to store elements in lockless rings. 284a9643ea8SlogwangThis should be used in all data-path code, when there are several consumer and/or producers to avoid locking for concurrent access. 285a9643ea8Slogwang 286*2d9fd380Sjfb8856606Naming 287*2d9fd380Sjfb8856606------ 288*2d9fd380Sjfb8856606 289*2d9fd380Sjfb8856606For symbol names and documentation, new usage of 290*2d9fd380Sjfb8856606'master / slave' (or 'slave' independent of 'master') and 'blacklist / 291*2d9fd380Sjfb8856606whitelist' is not allowed. 292*2d9fd380Sjfb8856606 293*2d9fd380Sjfb8856606Recommended replacements for 'master / slave' are: 294*2d9fd380Sjfb8856606 '{primary,main} / {secondary,replica,subordinate}' 295*2d9fd380Sjfb8856606 '{initiator,requester} / {target,responder}' 296*2d9fd380Sjfb8856606 '{controller,host} / {device,worker,proxy}' 297*2d9fd380Sjfb8856606 'leader / follower' 298*2d9fd380Sjfb8856606 'director / performer' 299*2d9fd380Sjfb8856606 300*2d9fd380Sjfb8856606Recommended replacements for 'blacklist/whitelist' are: 301*2d9fd380Sjfb8856606 'denylist / allowlist' 302*2d9fd380Sjfb8856606 'blocklist / passlist' 303*2d9fd380Sjfb8856606 304*2d9fd380Sjfb8856606Exceptions for introducing new usage is to maintain compatibility 305*2d9fd380Sjfb8856606with an existing (as of 2020) hardware or protocol 306*2d9fd380Sjfb8856606specification that mandates those terms. 307*2d9fd380Sjfb8856606 308*2d9fd380Sjfb8856606 309a9643ea8SlogwangTypedefs 310a9643ea8Slogwang~~~~~~~~ 311a9643ea8Slogwang 312a9643ea8SlogwangAvoid using typedefs for structure types. 313a9643ea8Slogwang 314a9643ea8SlogwangFor example, use: 315a9643ea8Slogwang 316a9643ea8Slogwang.. code-block:: c 317a9643ea8Slogwang 318a9643ea8Slogwang struct my_struct_type { 319a9643ea8Slogwang /* ... */ 320a9643ea8Slogwang }; 321a9643ea8Slogwang 322a9643ea8Slogwang struct my_struct_type my_var; 323a9643ea8Slogwang 324a9643ea8Slogwang 325a9643ea8Slogwangrather than: 326a9643ea8Slogwang 327a9643ea8Slogwang.. code-block:: c 328a9643ea8Slogwang 329a9643ea8Slogwang typedef struct my_struct_type { 330a9643ea8Slogwang /* ... */ 331a9643ea8Slogwang } my_struct_type; 332a9643ea8Slogwang 333a9643ea8Slogwang my_struct_type my_var 334a9643ea8Slogwang 335a9643ea8Slogwang 336a9643ea8SlogwangTypedefs are problematic because they do not properly hide their underlying type; 337a9643ea8Slogwangfor example, you need to know if the typedef is the structure itself, as shown above, or a pointer to the structure. 338a9643ea8SlogwangIn addition, they must be declared exactly once, whereas an incomplete structure type can be mentioned as many times as necessary. 339a9643ea8SlogwangTypedefs are difficult to use in stand-alone header files. 340a9643ea8SlogwangThe 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), 341a9643ea8Slogwangor there must be a back-door mechanism for obtaining the typedef. 342a9643ea8Slogwang 343a9643ea8SlogwangNote that #defines used instead of typedefs also are problematic (since they do not propagate the pointer type correctly due to direct text replacement). 344a9643ea8SlogwangFor example, ``#define pint int *`` does not work as expected, while ``typedef int *pint`` does work. 345a9643ea8SlogwangAs stated when discussing macros, typedefs should be preferred to macros in cases like this. 346a9643ea8Slogwang 347a9643ea8SlogwangWhen convention requires a typedef; make its name match the struct tag. 348a9643ea8SlogwangAvoid typedefs ending in ``_t``, except as specified in Standard C or by POSIX. 349a9643ea8Slogwang 350a9643ea8Slogwang.. note:: 351a9643ea8Slogwang 352a9643ea8Slogwang It is recommended to use typedefs to define function pointer types, for reasons of code readability. 353a9643ea8Slogwang This is especially true when the function type is used as a parameter to another function. 354a9643ea8Slogwang 355a9643ea8SlogwangFor example: 356a9643ea8Slogwang 357a9643ea8Slogwang.. code-block:: c 358a9643ea8Slogwang 359a9643ea8Slogwang /** 360a9643ea8Slogwang * Definition of a remote launch function. 361a9643ea8Slogwang */ 362a9643ea8Slogwang typedef int (lcore_function_t)(void *); 363a9643ea8Slogwang 364a9643ea8Slogwang /* launch a function of lcore_function_t type */ 365*2d9fd380Sjfb8856606 int rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned worker_id); 366a9643ea8Slogwang 367a9643ea8Slogwang 368a9643ea8SlogwangC Indentation 369a9643ea8Slogwang------------- 370a9643ea8Slogwang 371a9643ea8SlogwangGeneral 372a9643ea8Slogwang~~~~~~~ 373a9643ea8Slogwang 374a9643ea8Slogwang* Indentation is a hard tab, that is, a tab character, not a sequence of spaces, 375a9643ea8Slogwang 376a9643ea8Slogwang.. note:: 377a9643ea8Slogwang 378a9643ea8Slogwang Global whitespace rule in DPDK, use tabs for indentation, spaces for alignment. 379a9643ea8Slogwang 380a9643ea8Slogwang* Do not put any spaces before a tab for indentation. 381a9643ea8Slogwang* If you have to wrap a long statement, put the operator at the end of the line, and indent again. 382a9643ea8Slogwang* For control statements (if, while, etc.), continuation it is recommended that the next line be indented by two tabs, rather than one, 383a9643ea8Slogwang to prevent confusion as to whether the second line of the control statement forms part of the statement body or not. 384a9643ea8Slogwang 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. 385a9643ea8Slogwang 386a9643ea8Slogwang.. note:: 387a9643ea8Slogwang 388a9643ea8Slogwang As with all style guidelines, code should match style already in use in an existing file. 389a9643ea8Slogwang 390a9643ea8Slogwang.. code-block:: c 391a9643ea8Slogwang 392a9643ea8Slogwang while (really_long_variable_name_1 == really_long_variable_name_2 && 393a9643ea8Slogwang var3 == var4){ /* confusing to read as */ 394a9643ea8Slogwang x = y + z; /* control stmt body lines up with second line of */ 395a9643ea8Slogwang a = b + c; /* control statement itself if single indent used */ 396a9643ea8Slogwang } 397a9643ea8Slogwang 398a9643ea8Slogwang if (really_long_variable_name_1 == really_long_variable_name_2 && 399a9643ea8Slogwang var3 == var4){ /* two tabs used */ 400a9643ea8Slogwang x = y + z; /* statement body no longer lines up */ 401a9643ea8Slogwang a = b + c; 402a9643ea8Slogwang } 403a9643ea8Slogwang 404a9643ea8Slogwang z = a + really + long + statement + that + needs + 405a9643ea8Slogwang two + lines + gets + indented + on + the + 406a9643ea8Slogwang second + and + subsequent + lines; 407a9643ea8Slogwang 408a9643ea8Slogwang 409a9643ea8Slogwang* Do not add whitespace at the end of a line. 410a9643ea8Slogwang 411a9643ea8Slogwang* Do not add whitespace or a blank line at the end of a file. 412a9643ea8Slogwang 413a9643ea8Slogwang 414a9643ea8SlogwangControl Statements and Loops 415a9643ea8Slogwang~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 416a9643ea8Slogwang 417a9643ea8Slogwang* Include a space after keywords (if, while, for, return, switch). 418a9643ea8Slogwang* 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. 419a9643ea8Slogwang 420a9643ea8Slogwang.. code-block:: c 421a9643ea8Slogwang 422a9643ea8Slogwang for (p = buf; *p != '\0'; ++p) 423a9643ea8Slogwang ; /* nothing */ 424a9643ea8Slogwang for (;;) 425a9643ea8Slogwang stmt; 426a9643ea8Slogwang for (;;) { 427a9643ea8Slogwang z = a + really + long + statement + that + needs + 428a9643ea8Slogwang two + lines + gets + indented + on + the + 429a9643ea8Slogwang second + and + subsequent + lines; 430a9643ea8Slogwang } 431a9643ea8Slogwang for (;;) { 432a9643ea8Slogwang if (cond) 433a9643ea8Slogwang stmt; 434a9643ea8Slogwang } 435a9643ea8Slogwang if (val != NULL) 436a9643ea8Slogwang val = realloc(val, newsize); 437a9643ea8Slogwang 438a9643ea8Slogwang 439a9643ea8Slogwang* Parts of a for loop may be left empty. 440a9643ea8Slogwang 441a9643ea8Slogwang.. code-block:: c 442a9643ea8Slogwang 443a9643ea8Slogwang for (; cnt < 15; cnt++) { 444a9643ea8Slogwang stmt1; 445a9643ea8Slogwang stmt2; 446a9643ea8Slogwang } 447a9643ea8Slogwang 448a9643ea8Slogwang* Closing and opening braces go on the same line as the else keyword. 449a9643ea8Slogwang* Braces that are not necessary should be left out. 450a9643ea8Slogwang 451a9643ea8Slogwang.. code-block:: c 452a9643ea8Slogwang 453a9643ea8Slogwang if (test) 454a9643ea8Slogwang stmt; 455a9643ea8Slogwang else if (bar) { 456a9643ea8Slogwang stmt; 457a9643ea8Slogwang stmt; 458a9643ea8Slogwang } else 459a9643ea8Slogwang stmt; 460a9643ea8Slogwang 461a9643ea8Slogwang 462a9643ea8SlogwangFunction Calls 463a9643ea8Slogwang~~~~~~~~~~~~~~ 464a9643ea8Slogwang 465a9643ea8Slogwang* Do not use spaces after function names. 466a9643ea8Slogwang* Commas should have a space after them. 467a9643ea8Slogwang* No spaces after ``(`` or ``[`` or preceding the ``]`` or ``)`` characters. 468a9643ea8Slogwang 469a9643ea8Slogwang.. code-block:: c 470a9643ea8Slogwang 471a9643ea8Slogwang error = function(a1, a2); 472a9643ea8Slogwang if (error != 0) 473a9643ea8Slogwang exit(error); 474a9643ea8Slogwang 475a9643ea8Slogwang 476a9643ea8SlogwangOperators 477a9643ea8Slogwang~~~~~~~~~ 478a9643ea8Slogwang 479a9643ea8Slogwang* Unary operators do not require spaces, binary operators do. 480a9643ea8Slogwang* Do not use parentheses unless they are required for precedence or unless the statement is confusing without them. 481a9643ea8Slogwang However, remember that other people may be more easily confused than you. 482a9643ea8Slogwang 483a9643ea8SlogwangExit 484a9643ea8Slogwang~~~~ 485a9643ea8Slogwang 486a9643ea8SlogwangExits should be 0 on success, or 1 on failure. 487a9643ea8Slogwang 488a9643ea8Slogwang.. code-block:: c 489a9643ea8Slogwang 490a9643ea8Slogwang exit(0); /* 491a9643ea8Slogwang * Avoid obvious comments such as 492a9643ea8Slogwang * "Exit 0 on success." 493a9643ea8Slogwang */ 494a9643ea8Slogwang } 495a9643ea8Slogwang 496a9643ea8SlogwangLocal Variables 497a9643ea8Slogwang~~~~~~~~~~~~~~~ 498a9643ea8Slogwang 499a9643ea8Slogwang* Variables should be declared at the start of a block of code rather than in the middle. 500a9643ea8Slogwang The exception to this is when the variable is ``const`` in which case the declaration must be at the point of first use/assignment. 501a9643ea8Slogwang* When declaring variables in functions, multiple variables per line are OK. 502a9643ea8Slogwang 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. 503a9643ea8Slogwang* Be careful to not obfuscate the code by initializing variables in the declarations, only the last variable on a line should be initialized. 504a9643ea8Slogwang If multiple variables are to be initialized when defined, put one per line. 505a9643ea8Slogwang* Do not use function calls in initializers, except for ``const`` variables. 506a9643ea8Slogwang 507a9643ea8Slogwang.. code-block:: c 508a9643ea8Slogwang 509a9643ea8Slogwang int i = 0, j = 0, k = 0; /* bad, too many initializer */ 510a9643ea8Slogwang 511a9643ea8Slogwang char a = 0; /* OK, one variable per line with initializer */ 512a9643ea8Slogwang char b = 0; 513a9643ea8Slogwang 514a9643ea8Slogwang float x, y = 0.0; /* OK, only last variable has initializer */ 515a9643ea8Slogwang 516a9643ea8Slogwang 517a9643ea8SlogwangCasts and sizeof 518a9643ea8Slogwang~~~~~~~~~~~~~~~~ 519a9643ea8Slogwang 520a9643ea8Slogwang* Casts and sizeof statements are not followed by a space. 521a9643ea8Slogwang* Always write sizeof statements with parenthesis. 522a9643ea8Slogwang The redundant parenthesis rules do not apply to sizeof(var) instances. 523a9643ea8Slogwang 524a9643ea8SlogwangC Function Definition, Declaration and Use 525a9643ea8Slogwang------------------------------------------- 526a9643ea8Slogwang 527a9643ea8SlogwangPrototypes 528a9643ea8Slogwang~~~~~~~~~~ 529a9643ea8Slogwang 530a9643ea8Slogwang* It is recommended (and generally required by the compiler) that all non-static functions are prototyped somewhere. 531a9643ea8Slogwang* Functions local to one source module should be declared static, and should not be prototyped unless absolutely necessary. 532a9643ea8Slogwang* Functions used from other parts of code (external API) must be prototyped in the relevant include file. 533a9643ea8Slogwang* Function prototypes should be listed in a logical order, preferably alphabetical unless there is a compelling reason to use a different ordering. 534a9643ea8Slogwang* Functions that are used locally in more than one module go into a separate header file, for example, "extern.h". 535a9643ea8Slogwang* Do not use the ``__P`` macro. 536a9643ea8Slogwang* Functions that are part of an external API should be documented using Doxygen-like comments above declarations. See :ref:`doxygen_guidelines` for details. 537a9643ea8Slogwang* Functions that are part of the external API must have an ``rte_`` prefix on the function name. 538a9643ea8Slogwang* Do not use uppercase letters - either in the form of ALL_UPPERCASE, or CamelCase - in function names. Lower-case letters and underscores only. 539a9643ea8Slogwang* When prototyping functions, associate names with parameter types, for example: 540a9643ea8Slogwang 541a9643ea8Slogwang.. code-block:: c 542a9643ea8Slogwang 543a9643ea8Slogwang void function1(int fd); /* good */ 544a9643ea8Slogwang void function2(int); /* bad */ 545a9643ea8Slogwang 546a9643ea8Slogwang* Short function prototypes should be contained on a single line. 547a9643ea8Slogwang Longer prototypes, e.g. those with many parameters, can be split across multiple lines. 548a9643ea8Slogwang The second and subsequent lines should be further indented as for line statement continuations as described in the previous section. 549a9643ea8Slogwang 550a9643ea8Slogwang.. code-block:: c 551a9643ea8Slogwang 552a9643ea8Slogwang static char *function1(int _arg, const char *_arg2, 553a9643ea8Slogwang struct foo *_arg3, 554a9643ea8Slogwang struct bar *_arg4, 555a9643ea8Slogwang struct baz *_arg5); 556a9643ea8Slogwang static void usage(void); 557a9643ea8Slogwang 558a9643ea8Slogwang.. note:: 559a9643ea8Slogwang 560a9643ea8Slogwang Unlike function definitions, the function prototypes do not need to place the function return type on a separate line. 561a9643ea8Slogwang 562a9643ea8SlogwangDefinitions 563a9643ea8Slogwang~~~~~~~~~~~ 564a9643ea8Slogwang 565a9643ea8Slogwang* The function type should be on a line by itself preceding the function. 566a9643ea8Slogwang* The opening brace of the function body should be on a line by itself. 567a9643ea8Slogwang 568a9643ea8Slogwang.. code-block:: c 569a9643ea8Slogwang 570a9643ea8Slogwang static char * 571a9643ea8Slogwang function(int a1, int a2, float fl, int a4) 572a9643ea8Slogwang { 573a9643ea8Slogwang 574a9643ea8Slogwang 575a9643ea8Slogwang* Do not declare functions inside other functions. 576a9643ea8Slogwang ANSI C states that such declarations have file scope regardless of the nesting of the declaration. 577a9643ea8Slogwang Hiding file declarations in what appears to be a local scope is undesirable and will elicit complaints from a good compiler. 578a9643ea8Slogwang* Old-style (K&R) function declaration should not be used, use ANSI function declarations instead as shown below. 579a9643ea8Slogwang* Long argument lists should be wrapped as described above in the function prototypes section. 580a9643ea8Slogwang 581a9643ea8Slogwang.. code-block:: c 582a9643ea8Slogwang 583a9643ea8Slogwang /* 584a9643ea8Slogwang * All major routines should have a comment briefly describing what 585a9643ea8Slogwang * they do. The comment before the "main" routine should describe 586a9643ea8Slogwang * what the program does. 587a9643ea8Slogwang */ 588a9643ea8Slogwang int 589a9643ea8Slogwang main(int argc, char *argv[]) 590a9643ea8Slogwang { 591a9643ea8Slogwang char *ep; 592a9643ea8Slogwang long num; 593a9643ea8Slogwang int ch; 594a9643ea8Slogwang 595a9643ea8SlogwangC Statement Style and Conventions 596a9643ea8Slogwang--------------------------------- 597a9643ea8Slogwang 598a9643ea8SlogwangNULL Pointers 599a9643ea8Slogwang~~~~~~~~~~~~~ 600a9643ea8Slogwang 601a9643ea8Slogwang* NULL is the preferred null pointer constant. 602a9643ea8Slogwang 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. 603a9643ea8Slogwang* Test pointers against NULL, for example, use: 604a9643ea8Slogwang 605a9643ea8Slogwang.. code-block:: c 606a9643ea8Slogwang 607a9643ea8Slogwang if (p == NULL) /* Good, compare pointer to NULL */ 608a9643ea8Slogwang 609a9643ea8Slogwang if (!p) /* Bad, using ! on pointer */ 610a9643ea8Slogwang 611a9643ea8Slogwang 612a9643ea8Slogwang* Do not use ! for tests unless it is a boolean, for example, use: 613a9643ea8Slogwang 614a9643ea8Slogwang.. code-block:: c 615a9643ea8Slogwang 616a9643ea8Slogwang if (*p == '\0') /* check character against (char)0 */ 617a9643ea8Slogwang 618a9643ea8SlogwangReturn Value 619a9643ea8Slogwang~~~~~~~~~~~~ 620a9643ea8Slogwang 621a9643ea8Slogwang* Functions which create objects, or allocate memory, should return pointer types, and NULL on error. 622a9643ea8Slogwang The error type should be indicated may setting the variable ``rte_errno`` appropriately. 623a9643ea8Slogwang* Functions which work on bursts of packets, such as RX-like or TX-like functions, should return the number of packets handled. 624a9643ea8Slogwang* Other functions returning int should generally behave like system calls: 625a9643ea8Slogwang returning 0 on success and -1 on error, setting ``rte_errno`` to indicate the specific type of error. 626a9643ea8Slogwang* 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``. 627a9643ea8Slogwang Note, however, to allow consistency across functions returning integer or pointer types, the previous approach is preferred for any new libraries. 628a9643ea8Slogwang* For functions where no error is possible, the function type should be ``void`` not ``int``. 629a9643ea8Slogwang* Routines returning ``void *`` should not have their return values cast to any pointer type. 630a9643ea8Slogwang (Typecasting can prevent the compiler from warning about missing prototypes as any implicit definition of a function returns int, 631a9643ea8Slogwang which, unlike ``void *``, needs a typecast to assign to a pointer variable.) 632a9643ea8Slogwang 633a9643ea8Slogwang.. note:: 634a9643ea8Slogwang 635a9643ea8Slogwang The above rule about not typecasting ``void *`` applies to malloc, as well as to DPDK functions. 636a9643ea8Slogwang 637a9643ea8Slogwang* Values in return statements should not be enclosed in parentheses. 638a9643ea8Slogwang 639a9643ea8SlogwangLogging and Errors 640a9643ea8Slogwang~~~~~~~~~~~~~~~~~~ 641a9643ea8Slogwang 642a9643ea8SlogwangIn the DPDK environment, use the logging interface provided: 643a9643ea8Slogwang 644a9643ea8Slogwang.. code-block:: c 645a9643ea8Slogwang 6462bfe3f2eSlogwang /* register log types for this application */ 6472bfe3f2eSlogwang int my_logtype1 = rte_log_register("myapp.log1"); 6482bfe3f2eSlogwang int my_logtype2 = rte_log_register("myapp.log2"); 649a9643ea8Slogwang 6502bfe3f2eSlogwang /* set global log level to INFO */ 6512bfe3f2eSlogwang rte_log_set_global_level(RTE_LOG_INFO); 6522bfe3f2eSlogwang 6532bfe3f2eSlogwang /* only display messages higher than NOTICE for log2 (default 6542bfe3f2eSlogwang * is DEBUG) */ 6552bfe3f2eSlogwang rte_log_set_level(my_logtype2, RTE_LOG_NOTICE); 6562bfe3f2eSlogwang 657d30ea906Sjfb8856606 /* enable all PMD logs (whose identifier string starts with "pmd.") */ 658d30ea906Sjfb8856606 rte_log_set_level_pattern("pmd.*", RTE_LOG_DEBUG); 659a9643ea8Slogwang 660a9643ea8Slogwang /* log in debug level */ 6612bfe3f2eSlogwang rte_log_set_global_level(RTE_LOG_DEBUG); 6624418919fSjohnjiang RTE_LOG(DEBUG, my_logtype1, "this is a debug level message\n"); 6634418919fSjohnjiang RTE_LOG(INFO, my_logtype1, "this is a info level message\n"); 6644418919fSjohnjiang RTE_LOG(WARNING, my_logtype1, "this is a warning level message\n"); 6654418919fSjohnjiang RTE_LOG(WARNING, my_logtype2, "this is a debug level message (not displayed)\n"); 666a9643ea8Slogwang 667a9643ea8Slogwang /* log in info level */ 6682bfe3f2eSlogwang rte_log_set_global_level(RTE_LOG_INFO); 6692bfe3f2eSlogwang RTE_LOG(DEBUG, my_logtype1, "debug level message (not displayed)\n"); 670a9643ea8Slogwang 671a9643ea8SlogwangBranch Prediction 672a9643ea8Slogwang~~~~~~~~~~~~~~~~~ 673a9643ea8Slogwang 674a9643ea8Slogwang* 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. 675a9643ea8Slogwang They are expanded as a compiler builtin and allow the developer to indicate if the branch is likely to be taken or not. Example: 676a9643ea8Slogwang 677a9643ea8Slogwang.. code-block:: c 678a9643ea8Slogwang 679a9643ea8Slogwang #include <rte_branch_prediction.h> 680a9643ea8Slogwang if (likely(x > 1)) 681a9643ea8Slogwang do_stuff(); 682a9643ea8Slogwang 683a9643ea8Slogwang.. note:: 684a9643ea8Slogwang 685a9643ea8Slogwang The use of ``likely()`` and ``unlikely()`` should only be done in performance critical paths, 686a9643ea8Slogwang and only when there is a clearly preferred path, or a measured performance increase gained from doing so. 687a9643ea8Slogwang These macros should be avoided in non-performance-critical code. 688a9643ea8Slogwang 689a9643ea8SlogwangStatic Variables and Functions 690a9643ea8Slogwang~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 691a9643ea8Slogwang 692a9643ea8Slogwang* 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). 693a9643ea8Slogwang* Functions that should be inlined should to be declared as ``static inline`` and can be defined in a .c or a .h file. 694a9643ea8Slogwang 695a9643ea8Slogwang.. note:: 696a9643ea8Slogwang Static functions defined in a header file must be declared as ``static inline`` in order to prevent compiler warnings about the function being unused. 697a9643ea8Slogwang 698a9643ea8SlogwangConst Attribute 699a9643ea8Slogwang~~~~~~~~~~~~~~~ 700a9643ea8Slogwang 701a9643ea8SlogwangThe ``const`` attribute should be used as often as possible when a variable is read-only. 702a9643ea8Slogwang 703a9643ea8SlogwangInline ASM in C code 704a9643ea8Slogwang~~~~~~~~~~~~~~~~~~~~ 705a9643ea8Slogwang 706a9643ea8SlogwangThe ``asm`` and ``volatile`` keywords do not have underscores. The AT&T syntax should be used. 707a9643ea8SlogwangInput and output operands should be named to avoid confusion, as shown in the following example: 708a9643ea8Slogwang 709a9643ea8Slogwang.. code-block:: c 710a9643ea8Slogwang 711a9643ea8Slogwang asm volatile("outb %[val], %[port]" 712a9643ea8Slogwang : : 713a9643ea8Slogwang [port] "dN" (port), 714a9643ea8Slogwang [val] "a" (val)); 715a9643ea8Slogwang 716a9643ea8SlogwangControl Statements 717a9643ea8Slogwang~~~~~~~~~~~~~~~~~~ 718a9643ea8Slogwang 719a9643ea8Slogwang* Forever loops are done with for statements, not while statements. 720a9643ea8Slogwang* Elements in a switch statement that cascade should have a FALLTHROUGH comment. For example: 721a9643ea8Slogwang 722a9643ea8Slogwang.. code-block:: c 723a9643ea8Slogwang 724a9643ea8Slogwang switch (ch) { /* Indent the switch. */ 725a9643ea8Slogwang case 'a': /* Don't indent the case. */ 726a9643ea8Slogwang aflag = 1; /* Indent case body one tab. */ 727a9643ea8Slogwang /* FALLTHROUGH */ 728a9643ea8Slogwang case 'b': 729a9643ea8Slogwang bflag = 1; 730a9643ea8Slogwang break; 731a9643ea8Slogwang case '?': 732a9643ea8Slogwang default: 733a9643ea8Slogwang usage(); 734a9643ea8Slogwang /* NOTREACHED */ 735a9643ea8Slogwang } 736a9643ea8Slogwang 737d30ea906Sjfb8856606Dynamic Logging 738d30ea906Sjfb8856606--------------- 739d30ea906Sjfb8856606 740d30ea906Sjfb8856606DPDK provides infrastructure to perform logging during runtime. This is very 741d30ea906Sjfb8856606useful for enabling debug output without recompilation. To enable or disable 742d30ea906Sjfb8856606logging of a particular topic, the ``--log-level`` parameter can be provided 743d30ea906Sjfb8856606to EAL, which will change the log level. DPDK code can register topics, 744d30ea906Sjfb8856606which allows the user to adjust the log verbosity for that specific topic. 745d30ea906Sjfb8856606 746d30ea906Sjfb8856606In general, the naming scheme is as follows: ``type.section.name`` 747d30ea906Sjfb8856606 748d30ea906Sjfb8856606 * Type is the type of component, where ``lib``, ``pmd``, ``bus`` and ``user`` 749d30ea906Sjfb8856606 are the common options. 750d30ea906Sjfb8856606 * Section refers to a specific area, for example a poll-mode-driver for an 751d30ea906Sjfb8856606 ethernet device would use ``pmd.net``, while an eventdev PMD uses 752d30ea906Sjfb8856606 ``pmd.event``. 753d30ea906Sjfb8856606 * The name identifies the individual item that the log applies to. 754d30ea906Sjfb8856606 The name section must align with 755d30ea906Sjfb8856606 the directory that the PMD code resides. See examples below for clarity. 756d30ea906Sjfb8856606 757d30ea906Sjfb8856606Examples: 758d30ea906Sjfb8856606 759d30ea906Sjfb8856606 * The virtio network PMD in ``drivers/net/virtio`` uses ``pmd.net.virtio`` 760d30ea906Sjfb8856606 * The eventdev software poll mode driver in ``drivers/event/sw`` uses ``pmd.event.sw`` 761d30ea906Sjfb8856606 * The octeontx mempool driver in ``drivers/mempool/octeontx`` uses ``pmd.mempool.octeontx`` 762d30ea906Sjfb8856606 * The DPDK hash library in ``lib/librte_hash`` uses ``lib.hash`` 763d30ea906Sjfb8856606 764d30ea906Sjfb8856606Specializations 765d30ea906Sjfb8856606~~~~~~~~~~~~~~~ 766d30ea906Sjfb8856606 767d30ea906Sjfb8856606In addition to the above logging topic, any PMD or library can further split 768d30ea906Sjfb8856606logging output by using "specializations". A specialization could be the 769d30ea906Sjfb8856606difference between initialization code, and logs of events that occur at runtime. 770d30ea906Sjfb8856606 771d30ea906Sjfb8856606An example could be the initialization log messages getting one 772d30ea906Sjfb8856606specialization, while another specialization handles mailbox command logging. 773d30ea906Sjfb8856606Each PMD, library or component can create as many specializations as required. 774d30ea906Sjfb8856606 775d30ea906Sjfb8856606A specialization looks like this: 776d30ea906Sjfb8856606 777d30ea906Sjfb8856606 * Initialization output: ``type.section.name.init`` 778d30ea906Sjfb8856606 * PF/VF mailbox output: ``type.section.name.mbox`` 779d30ea906Sjfb8856606 780d30ea906Sjfb8856606A real world example is the i40e poll mode driver which exposes two 781d30ea906Sjfb8856606specializations, one for initialization ``pmd.net.i40e.init`` and the other for 782d30ea906Sjfb8856606the remaining driver logs ``pmd.net.i40e.driver``. 783d30ea906Sjfb8856606 784d30ea906Sjfb8856606Note that specializations have no formatting rules, but please follow 785d30ea906Sjfb8856606a precedent if one exists. In order to see all current log topics and 786d30ea906Sjfb8856606specializations, run the ``app/test`` binary, and use the ``dump_log_types`` 787a9643ea8Slogwang 788a9643ea8SlogwangPython Code 789a9643ea8Slogwang----------- 790a9643ea8Slogwang 791*2d9fd380Sjfb8856606All Python code should be compliant with 7922bfe3f2eSlogwang`PEP8 (Style Guide for Python Code) <https://www.python.org/dev/peps/pep-0008/>`_. 793a9643ea8Slogwang 794a9643ea8SlogwangThe ``pep8`` tool can be used for testing compliance with the guidelines. 795d30ea906Sjfb8856606 796d30ea906Sjfb8856606Integrating with the Build System 797d30ea906Sjfb8856606--------------------------------- 798d30ea906Sjfb8856606 799*2d9fd380Sjfb8856606DPDK is built using the tools ``meson`` and ``ninja``. 800d30ea906Sjfb8856606 801*2d9fd380Sjfb8856606Therefore all new component additions should include a ``meson.build`` file, 802*2d9fd380Sjfb8856606and should be added to the component lists in the ``meson.build`` files in the 803*2d9fd380Sjfb8856606relevant top-level directory: 804d30ea906Sjfb8856606either ``lib`` directory or a ``driver`` subdirectory. 805d30ea906Sjfb8856606 806d30ea906Sjfb8856606Meson Build File Contents - Libraries 807d30ea906Sjfb8856606~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 808d30ea906Sjfb8856606 809d30ea906Sjfb8856606The ``meson.build`` file for a new DPDK library should be of the following basic 810d30ea906Sjfb8856606format. 811d30ea906Sjfb8856606 812d30ea906Sjfb8856606.. code-block:: python 813d30ea906Sjfb8856606 814d30ea906Sjfb8856606 sources = files('file1.c', ...) 8151646932aSjfb8856606 headers = files('file1.h', ...) 816d30ea906Sjfb8856606 817d30ea906Sjfb8856606 8181646932aSjfb8856606This will build based on a number of conventions and assumptions within the DPDK 819d30ea906Sjfb8856606itself, for example, that the library name is the same as the directory name in 820d30ea906Sjfb8856606which the files are stored. 821d30ea906Sjfb8856606 822d30ea906Sjfb8856606For a library ``meson.build`` file, there are number of variables which can be 823d30ea906Sjfb8856606set, some mandatory, others optional. The mandatory fields are: 824d30ea906Sjfb8856606 825d30ea906Sjfb8856606sources 826d30ea906Sjfb8856606 **Default Value = []**. 827d30ea906Sjfb8856606 This variable should list out the files to be compiled up to create the 828d30ea906Sjfb8856606 library. Files must be specified using the meson ``files()`` function. 829d30ea906Sjfb8856606 830d30ea906Sjfb8856606 831d30ea906Sjfb8856606The optional fields are: 832d30ea906Sjfb8856606 833d30ea906Sjfb8856606build 834d30ea906Sjfb8856606 **Default Value = true** 835d30ea906Sjfb8856606 Used to optionally compile a library, based on its dependencies or 8364418919fSjohnjiang environment. When set to "false" the ``reason`` value, explained below, should 8374418919fSjohnjiang also be set to explain to the user why the component is not being built. 8384418919fSjohnjiang A simple example of use would be: 839d30ea906Sjfb8856606 840d30ea906Sjfb8856606.. code-block:: python 841d30ea906Sjfb8856606 8424418919fSjohnjiang if not is_linux 843d30ea906Sjfb8856606 build = false 8444418919fSjohnjiang reason = 'only supported on Linux' 845d30ea906Sjfb8856606 endif 846d30ea906Sjfb8856606 847d30ea906Sjfb8856606 848d30ea906Sjfb8856606cflags 849d30ea906Sjfb8856606 **Default Value = [<-march/-mcpu flags>]**. 850d30ea906Sjfb8856606 Used to specify any additional cflags that need to be passed to compile 851d30ea906Sjfb8856606 the sources in the library. 852d30ea906Sjfb8856606 853d30ea906Sjfb8856606deps 854d30ea906Sjfb8856606 **Default Value = ['eal']**. 855d30ea906Sjfb8856606 Used to list the internal library dependencies of the library. It should 856d30ea906Sjfb8856606 be assigned to using ``+=`` rather than overwriting using ``=``. The 857d30ea906Sjfb8856606 dependencies should be specified as strings, each one giving the name of 858d30ea906Sjfb8856606 a DPDK library, without the ``librte_`` prefix. Dependencies are handled 859d30ea906Sjfb8856606 recursively, so specifying e.g. ``mempool``, will automatically also 860d30ea906Sjfb8856606 make the library depend upon the mempool library's dependencies too - 861d30ea906Sjfb8856606 ``ring`` and ``eal``. For libraries that only depend upon EAL, this 862d30ea906Sjfb8856606 variable may be omitted from the ``meson.build`` file. For example: 863d30ea906Sjfb8856606 864d30ea906Sjfb8856606.. code-block:: python 865d30ea906Sjfb8856606 866d30ea906Sjfb8856606 deps += ['ethdev'] 867d30ea906Sjfb8856606 868d30ea906Sjfb8856606 869d30ea906Sjfb8856606ext_deps 870d30ea906Sjfb8856606 **Default Value = []**. 871d30ea906Sjfb8856606 Used to specify external dependencies of this library. They should be 872d30ea906Sjfb8856606 returned as dependency objects, as returned from the meson 873d30ea906Sjfb8856606 ``dependency()`` or ``find_library()`` functions. Before returning 874d30ea906Sjfb8856606 these, they should be checked to ensure the dependencies have been 875d30ea906Sjfb8856606 found, and, if not, the ``build`` variable should be set to ``false``. 876d30ea906Sjfb8856606 For example: 877d30ea906Sjfb8856606 878d30ea906Sjfb8856606.. code-block:: python 879d30ea906Sjfb8856606 880d30ea906Sjfb8856606 my_dep = dependency('libX', required: 'false') 881d30ea906Sjfb8856606 if my_dep.found() 882d30ea906Sjfb8856606 ext_deps += my_dep 883d30ea906Sjfb8856606 else 884d30ea906Sjfb8856606 build = false 885d30ea906Sjfb8856606 endif 886d30ea906Sjfb8856606 887d30ea906Sjfb8856606 888d30ea906Sjfb8856606headers 889d30ea906Sjfb8856606 **Default Value = []**. 890d30ea906Sjfb8856606 Used to return the list of header files for the library that should be 891d30ea906Sjfb8856606 installed to $PREFIX/include when ``ninja install`` is run. As with 892d30ea906Sjfb8856606 source files, these should be specified using the meson ``files()`` 893d30ea906Sjfb8856606 function. 894d30ea906Sjfb8856606 895d30ea906Sjfb8856606includes: 896d30ea906Sjfb8856606 **Default Value = []**. 897d30ea906Sjfb8856606 Used to indicate any additional header file paths which should be 898d30ea906Sjfb8856606 added to the header search path for other libs depending on this 899d30ea906Sjfb8856606 library. EAL uses this so that other libraries building against it 900d30ea906Sjfb8856606 can find the headers in subdirectories of the main EAL directory. The 901d30ea906Sjfb8856606 base directory of each library is always given in the include path, 902d30ea906Sjfb8856606 it does not need to be specified here. 903d30ea906Sjfb8856606 904d30ea906Sjfb8856606name 905d30ea906Sjfb8856606 **Default Value = library name derived from the directory name**. 906d30ea906Sjfb8856606 If a library's .so or .a file differs from that given in the directory 907d30ea906Sjfb8856606 name, the name should be specified using this variable. In practice, 908d30ea906Sjfb8856606 since the convention is that for a library called ``librte_xyz.so``, the 909d30ea906Sjfb8856606 sources are stored in a directory ``lib/librte_xyz``, this value should 910d30ea906Sjfb8856606 never be needed for new libraries. 911d30ea906Sjfb8856606 912d30ea906Sjfb8856606.. note:: 913d30ea906Sjfb8856606 914d30ea906Sjfb8856606 The name value also provides the name used to find the function version 915d30ea906Sjfb8856606 map file, as part of the build process, so if the directory name and 916d30ea906Sjfb8856606 library names differ, the ``version.map`` file should be named 917d30ea906Sjfb8856606 consistently with the library, not the directory 918d30ea906Sjfb8856606 919d30ea906Sjfb8856606objs 920d30ea906Sjfb8856606 **Default Value = []**. 921d30ea906Sjfb8856606 This variable can be used to pass to the library build some pre-built 922d30ea906Sjfb8856606 objects that were compiled up as part of another target given in the 923d30ea906Sjfb8856606 included library ``meson.build`` file. 924d30ea906Sjfb8856606 9254418919fSjohnjiangreason 9264418919fSjohnjiang **Default Value = '<unknown reason>'**. 9274418919fSjohnjiang This variable should be used when a library is not to be built i.e. when 9284418919fSjohnjiang ``build`` is set to "false", to specify the reason why a library will not be 9294418919fSjohnjiang built. For missing dependencies this should be of the form 9304418919fSjohnjiang ``'missing dependency, "libname"'``. 9314418919fSjohnjiang 9324418919fSjohnjianguse_function_versioning 9334418919fSjohnjiang **Default Value = false**. 9344418919fSjohnjiang Specifies if the library in question has ABI versioned functions. If it 9354418919fSjohnjiang has, this value should be set to ensure that the C files are compiled 9364418919fSjohnjiang twice with suitable parameters for each of shared or static library 9374418919fSjohnjiang builds. 938d30ea906Sjfb8856606 939d30ea906Sjfb8856606Meson Build File Contents - Drivers 940d30ea906Sjfb8856606~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 941d30ea906Sjfb8856606 942d30ea906Sjfb8856606For drivers, the values are largely the same as for libraries. The variables 943d30ea906Sjfb8856606supported are: 944d30ea906Sjfb8856606 945d30ea906Sjfb8856606build 946d30ea906Sjfb8856606 As above. 947d30ea906Sjfb8856606 948d30ea906Sjfb8856606cflags 949d30ea906Sjfb8856606 As above. 950d30ea906Sjfb8856606 951d30ea906Sjfb8856606deps 952d30ea906Sjfb8856606 As above. 953d30ea906Sjfb8856606 954d30ea906Sjfb8856606ext_deps 955d30ea906Sjfb8856606 As above. 956d30ea906Sjfb8856606 957d30ea906Sjfb8856606includes 958d30ea906Sjfb8856606 **Default Value = <driver directory>** Some drivers include a base 959d30ea906Sjfb8856606 directory for additional source files and headers, so we have this 960d30ea906Sjfb8856606 variable to allow the headers from that base directory to be found when 961d30ea906Sjfb8856606 compiling driver sources. Should be appended to using ``+=`` rather than 962d30ea906Sjfb8856606 overwritten using ``=``. The values appended should be meson include 963d30ea906Sjfb8856606 objects got using the ``include_directories()`` function. For example: 964d30ea906Sjfb8856606 965d30ea906Sjfb8856606.. code-block:: python 966d30ea906Sjfb8856606 967d30ea906Sjfb8856606 includes += include_directories('base') 968d30ea906Sjfb8856606 969d30ea906Sjfb8856606name 970d30ea906Sjfb8856606 As above, though note that each driver class can define it's own naming 971d30ea906Sjfb8856606 scheme for the resulting ``.so`` files. 972d30ea906Sjfb8856606 973d30ea906Sjfb8856606objs 974d30ea906Sjfb8856606 As above, generally used for the contents of the ``base`` directory. 975d30ea906Sjfb8856606 976d30ea906Sjfb8856606pkgconfig_extra_libs 977d30ea906Sjfb8856606 **Default Value = []** 978d30ea906Sjfb8856606 This variable is used to pass additional library link flags through to 979d30ea906Sjfb8856606 the DPDK pkgconfig file generated, for example, to track any additional 980d30ea906Sjfb8856606 libraries that may need to be linked into the build - especially when 981d30ea906Sjfb8856606 using static libraries. Anything added here will be appended to the end 982d30ea906Sjfb8856606 of the ``pkgconfig --libs`` output. 983d30ea906Sjfb8856606 9844418919fSjohnjiangreason 9854418919fSjohnjiang As above. 9864418919fSjohnjiang 987d30ea906Sjfb8856606sources [mandatory] 988d30ea906Sjfb8856606 As above 989d30ea906Sjfb8856606 990*2d9fd380Sjfb8856606headers 991*2d9fd380Sjfb8856606 As above 992*2d9fd380Sjfb8856606 993d30ea906Sjfb8856606version 994d30ea906Sjfb8856606 As above 995