DPDK  25.11.0
rte_common.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2019 Intel Corporation
3  */
4 
5 #ifndef _RTE_COMMON_H_
6 #define _RTE_COMMON_H_
7 
15 #include <assert.h>
16 #include <limits.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <stdalign.h>
20 
21 #include <rte_compat.h>
22 #include <rte_config.h>
23 
24 /* OS specific include */
25 #include <rte_os.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #ifndef RTE_TOOLCHAIN_MSVC
32 #ifndef typeof
33 #define typeof __typeof__
34 #endif
35 #endif
36 
37 #ifndef __cplusplus
38 #ifndef asm
39 #define asm __asm__
40 #endif
41 #endif
42 
43 #ifdef RTE_TOOLCHAIN_MSVC
44 #ifdef __cplusplus
45 #define __extension__
46 #endif
47 #endif
48 
49 /*
50  * Macro __rte_constant checks if an expression's value can be determined at
51  * compile time. It takes a single argument, the expression to test, and
52  * returns 1 if the expression is a compile-time constant, and 0 otherwise.
53  * For most compilers it uses built-in function __builtin_constant_p, but for
54  * MSVC it uses a different method because MSVC does not have an equivalent
55  * to __builtin_constant_p.
56  *
57  * The trick used with MSVC relies on the way null pointer constants interact
58  * with the type of a ?: expression:
59  * An integer constant expression with the value 0, or such an expression cast
60  * to type void *, is called a null pointer constant.
61  * If both the second and third operands (of the ?: expression) are pointers or
62  * one is a null pointer constant and the other is a pointer, the result type
63  * is a pointer to a type qualified with all the type qualifiers of the types
64  * referenced by both operands. Furthermore, if both operands are pointers to
65  * compatible types or to differently qualified versions of compatible types,
66  * the result type is a pointer to an appropriately qualified version of the
67  * composite type; if one operand is a null pointer constant, the result has
68  * the type of the other operand; otherwise, one operand is a pointer to void
69  * or a qualified version of void, in which case the result type is a pointer
70  * to an appropriately qualified version of void.
71  *
72  * The _Generic keyword then checks the type of the expression
73  * (void *) ((e) * 0ll). It matches this type against the types listed in the
74  * _Generic construct:
75  * - If the type is int *, the result is 1.
76  * - If the type is void *, the result is 0.
77  *
78  * This explanation with some more details can be found at:
79  * https://stackoverflow.com/questions/49480442/detecting-integer-constant-expressions-in-macros
80  */
81 #ifdef RTE_TOOLCHAIN_MSVC
82 #define __rte_constant(e) _Generic((1 ? (void *) ((e) * 0ll) : (int *) 0), int * : 1, void * : 0)
83 #else
84 #define __rte_constant(e) __extension__(__builtin_constant_p(e))
85 #endif
86 
87 /*
88  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
89  * while a host application (like pmdinfogen) may have another compiler.
90  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
91  * no matter it is a target or host application.
92  */
93 #define RTE_CC_IS_GNU 0
94 #if defined __clang__
95 #define RTE_CC_CLANG
96 #elif defined __GNUC__
97 #define RTE_CC_GCC
98 #undef RTE_CC_IS_GNU
99 #define RTE_CC_IS_GNU 1
100 #endif
101 #if RTE_CC_IS_GNU
102 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
103  __GNUC_PATCHLEVEL__)
104 #endif
105 
118 #ifdef RTE_TOOLCHAIN_MSVC
119 #define __rte_aligned(a) __declspec(align(a))
120 #else
121 #define __rte_aligned(a) __attribute__((__aligned__(a)))
122 #endif
123 
124 #ifdef RTE_ARCH_STRICT_ALIGN
125 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
126 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
127 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
128 #else
129 typedef uint64_t unaligned_uint64_t;
130 typedef uint32_t unaligned_uint32_t;
131 typedef uint16_t unaligned_uint16_t;
132 #endif
133 
141 #ifdef RTE_TOOLCHAIN_MSVC
142 #define __rte_packed RTE_DEPRECATED(__rte_packed)
143 #else
144 #define __rte_packed (RTE_DEPRECATED(__rte_packed) __attribute__((__packed__)))
145 #endif
146 
154 #ifdef RTE_TOOLCHAIN_MSVC
155 #define __rte_packed_begin __pragma(pack(push, 1))
156 #define __rte_packed_end __pragma(pack(pop))
157 #else
158 #define __rte_packed_begin
159 #define __rte_packed_end __attribute__((__packed__))
160 #endif
161 
165 #ifdef RTE_TOOLCHAIN_MSVC
166 #define __rte_may_alias
167 #else
168 #define __rte_may_alias __attribute__((__may_alias__))
169 #endif
170 
171 /******* Macro to mark functions and fields scheduled for removal *****/
172 #ifdef RTE_TOOLCHAIN_MSVC
173 #define __rte_deprecated
174 #define __rte_deprecated_msg(msg)
175 #else
176 #define __rte_deprecated __attribute__((__deprecated__))
177 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
178 #endif
179 
183 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
184 #define RTE_PRAGMA(x) _Pragma(#x)
185 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
186 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
187 #else
188 #define RTE_DEPRECATED(x)
189 #endif
190 
195 #if !defined(RTE_TOOLCHAIN_MSVC)
196 #define __rte_diagnostic_push _Pragma("GCC diagnostic push")
197 #define __rte_diagnostic_pop _Pragma("GCC diagnostic pop")
198 #else
199 #define __rte_diagnostic_push
200 #define __rte_diagnostic_pop
201 #endif
202 
207 #if !defined(RTE_TOOLCHAIN_MSVC)
208 #define __rte_diagnostic_ignored_wcast_qual _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
209 #else
210 #define __rte_diagnostic_ignored_wcast_qual
211 #endif
212 
216 #ifdef RTE_TOOLCHAIN_MSVC
217 #define __rte_weak RTE_DEPRECATED(__rte_weak)
218 #else
219 #define __rte_weak RTE_DEPRECATED(__rte_weak) __attribute__((__weak__))
220 #endif
221 
225 #ifdef RTE_TOOLCHAIN_MSVC
226 #define __rte_pure
227 #else
228 #define __rte_pure __attribute__((pure))
229 #endif
230 
234 #ifdef RTE_TOOLCHAIN_MSVC
235 #define __rte_used
236 #else
237 #define __rte_used __attribute__((used))
238 #endif
239 
240 /*********** Macros to eliminate unused variable warnings ********/
241 
245 #ifdef RTE_TOOLCHAIN_MSVC
246 #define __rte_unused
247 #else
248 #define __rte_unused __attribute__((__unused__))
249 #endif
250 
254 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
255 #define __rte_restrict __restrict
256 #else
257 #define __rte_restrict restrict
258 #endif
259 
264 #define RTE_SET_USED(x) (void)(x)
265 
273 #ifdef RTE_TOOLCHAIN_MSVC
274 #define __rte_format_printf(format_index, first_arg)
275 #else
276 #if RTE_CC_IS_GNU
277 #define __rte_format_printf(format_index, first_arg) \
278  __attribute__((format(gnu_printf, format_index, first_arg)))
279 #else
280 #define __rte_format_printf(format_index, first_arg) \
281  __attribute__((format(printf, format_index, first_arg)))
282 #endif
283 #endif
284 
288 #ifdef RTE_TOOLCHAIN_MSVC
289 #define __rte_section(name) \
290  __pragma(data_seg(name)) __declspec(allocate(name))
291 #else
292 #define __rte_section(name) \
293  __attribute__((section(name)))
294 #endif
295 
301 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
302 #define __rte_alloc_size(...) \
303  __attribute__((alloc_size(__VA_ARGS__)))
304 #else
305 #define __rte_alloc_size(...)
306 #endif
307 
314 #if defined(RTE_CC_GCC)
315 #define __rte_alloc_align(argno) \
316  __attribute__((alloc_align(argno)))
317 #else
318 #define __rte_alloc_align(argno)
319 #endif
320 
325 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
326 #define __rte_malloc __attribute__((__malloc__))
327 #else
328 #define __rte_malloc
329 #endif
330 
335 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
336 #define __rte_dealloc(dealloc, argno) \
337  __attribute__((malloc(dealloc, argno)))
338 #else
339 #define __rte_dealloc(dealloc, argno)
340 #endif
341 
342 #define RTE_PRIORITY_LOG 101
343 #define RTE_PRIORITY_BUS 110
344 #define RTE_PRIORITY_CLASS 120
345 #define RTE_PRIORITY_LAST 65535
346 
347 #define RTE_PRIO(prio) \
348  RTE_PRIORITY_ ## prio
349 
359 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
360 #ifndef RTE_TOOLCHAIN_MSVC
361 #if defined(__x86_64__) || defined(__i386__)
362 #define RTE_INIT_PRIO(func, prio) \
363 static void \
364  __attribute__((constructor(RTE_PRIO(prio)), used)) \
365  __attribute__((target ("sse2"))) \
366  __attribute__((target ("no-sse3"))) \
367  __attribute__((target ("no-sse4"))) \
368  func(void)
369 #else
370 #define RTE_INIT_PRIO(func, prio) \
371 static void \
372  __attribute__((constructor(RTE_PRIO(prio)), used)) \
373  func(void)
374 #endif
375 #else
376 /* definition from the Microsoft CRT */
377 typedef int(__cdecl *_PIFV)(void);
378 
379 #define CTOR_SECTION_LOG ".CRT$XIB"
380 #define CTOR_SECTION_BUS ".CRT$XIC"
381 #define CTOR_SECTION_CLASS ".CRT$XID"
382 #define CTOR_SECTION_LAST ".CRT$XIY"
383 
384 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
385 
386 #define RTE_INIT_PRIO(name, priority) \
387  static void name(void); \
388  static int __cdecl name ## _thunk(void) { name(); return 0; } \
389  __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
390  __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
391  _PIFV name ## _pointer = &name ## _thunk; \
392  __pragma(const_seg()) \
393  static void name(void)
394 #endif
395 #endif
396 
405 #define RTE_INIT(func) \
406  RTE_INIT_PRIO(func, LAST)
407 
417 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
418 #ifndef RTE_TOOLCHAIN_MSVC
419 #define RTE_FINI_PRIO(func, prio) \
420 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
421 #else
422 #define DTOR_SECTION_LOG "mydtor$B"
423 #define DTOR_SECTION_BUS "mydtor$C"
424 #define DTOR_SECTION_CLASS "mydtor$D"
425 #define DTOR_SECTION_LAST "mydtor$Y"
426 
427 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
428 
429 #define RTE_FINI_PRIO(name, priority) \
430  static void name(void); \
431  __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
432  __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
433  __pragma(const_seg()) \
434  static void name(void)
435 #endif
436 #endif
437 
446 #define RTE_FINI(func) \
447  RTE_FINI_PRIO(func, LAST)
448 
452 #ifdef RTE_TOOLCHAIN_MSVC
453 #define __rte_noreturn
454 #else
455 #define __rte_noreturn __attribute__((noreturn))
456 #endif
457 
461 #if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
462 #define __rte_unreachable() __extension__(__builtin_unreachable())
463 #else
464 #define __rte_unreachable() __assume(0)
465 #endif
466 
490 #ifdef RTE_TOOLCHAIN_MSVC
491 #define __rte_warn_unused_result
492 #else
493 #define __rte_warn_unused_result __attribute__((warn_unused_result))
494 #endif
495 
499 #ifdef RTE_TOOLCHAIN_MSVC
500 #define __rte_always_inline __forceinline
501 #else
502 #define __rte_always_inline inline __attribute__((always_inline))
503 #endif
504 
508 #ifdef RTE_TOOLCHAIN_MSVC
509 #define __rte_noinline __declspec(noinline)
510 #else
511 #define __rte_noinline __attribute__((noinline))
512 #endif
513 
517 #ifdef RTE_TOOLCHAIN_MSVC
518 #define __rte_hot
519 #else
520 #define __rte_hot __attribute__((hot))
521 #endif
522 
526 #ifdef RTE_TOOLCHAIN_MSVC
527 #define __rte_cold
528 #else
529 #define __rte_cold __attribute__((cold))
530 #endif
531 
538 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
539 #define __rte_assume(condition) __attribute__((assume(condition)))
540 #elif defined(RTE_TOOLCHAIN_GCC)
541 #define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
542 #elif defined(RTE_TOOLCHAIN_CLANG)
543 #define __rte_assume(condition) __extension__(__builtin_assume(condition))
544 #else
545 #define __rte_assume(condition) __assume(condition)
546 #endif
547 
551 #ifdef RTE_MALLOC_ASAN
552 #ifdef RTE_CC_CLANG
553 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
554 #else
555 #define __rte_no_asan __attribute__((no_sanitize_address))
556 #endif
557 #else /* ! RTE_MALLOC_ASAN */
558 #define __rte_no_asan
559 #endif
560 
561 /*********** Macros for pointer arithmetic ********/
562 
566 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
567 
571 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
572 
578 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
579 
580 /*********** Macros for casting pointers ********/
581 
586 #define RTE_PTR_UNQUAL(X) ((void *)(uintptr_t)(X))
587 
605 #define RTE_CAST_PTR(type, ptr) ((type)(uintptr_t)(ptr))
606 
610 #define RTE_CAST_FIELD(var, field, type) \
611  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
612 
613 /*********** Macros/static functions for doing alignment ********/
614 
615 
622 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
623  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
624 
631 #define RTE_ALIGN_FLOOR(val, align) \
632  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
633 
640 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
641  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
642 
649 #define RTE_ALIGN_CEIL(val, align) \
650  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
651 
659 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
660 
668 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
669 
675 #define RTE_ALIGN_MUL_CEIL(v, mul) \
676  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
677 
683 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
684  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
685 
691 #define RTE_ALIGN_MUL_NEAR(v, mul) \
692  __extension__ ({ \
693  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
694  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
695  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
696  })
697 
709 static inline int
710 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
711 {
712  return ((uintptr_t)ptr & (align - 1)) == 0;
713 }
714 
715 /*********** Macros for compile type checks ********/
716 
717 /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
718 #if !defined(static_assert) && !defined(__cplusplus)
719 #define static_assert _Static_assert
720 #endif
721 
728 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
729 
730 /*********** Cache line related macros ********/
731 
733 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
734 
736 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
737 
739 #if RTE_CACHE_LINE_SIZE == 64
740 #define RTE_CACHE_LINE_SIZE_LOG2 6
741 #elif RTE_CACHE_LINE_SIZE == 128
742 #define RTE_CACHE_LINE_SIZE_LOG2 7
743 #else
744 #error "Unsupported cache line size"
745 #endif
746 
748 #define RTE_CACHE_LINE_MIN_SIZE 64
749 
751 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
752 
754 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
755 
756 #define _RTE_CACHE_GUARD_HELPER2(unique) \
757  alignas(RTE_CACHE_LINE_SIZE) \
758  char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
759 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
760 
767 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
768 
769 /*********** PA/IOVA type definitions ********/
770 
772 typedef uint64_t phys_addr_t;
773 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
774 
782 typedef uint64_t rte_iova_t;
783 #define RTE_BAD_IOVA ((rte_iova_t)-1)
784 
785 /*********** Structure alignment markers ********/
786 
787 #ifndef RTE_TOOLCHAIN_MSVC
788 
790 __extension__ typedef void *RTE_MARKER[0];
792 __extension__ typedef uint8_t RTE_MARKER8[0];
794 __extension__ typedef uint16_t RTE_MARKER16[0];
796 __extension__ typedef uint32_t RTE_MARKER32[0];
798 __extension__ typedef uint64_t RTE_MARKER64[0];
799 
800 #endif
801 
802 /*********** Macros for calculating min and max **********/
803 
807 #define RTE_MIN(a, b) \
808  __extension__ ({ \
809  typeof (a) _a = (a); \
810  typeof (b) _b = (b); \
811  _a < _b ? _a : _b; \
812  })
813 
821 #define RTE_MIN_T(a, b, t) \
822  ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
823 
827 #define RTE_MAX(a, b) \
828  __extension__ ({ \
829  typeof (a) _a = (a); \
830  typeof (b) _b = (b); \
831  _a > _b ? _a : _b; \
832  })
833 
841 #define RTE_MAX_T(a, b, t) \
842  ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
843 
844 /*********** Other general functions / macros ********/
845 
846 #ifndef offsetof
847 
848 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
849 #endif
850 
865 #ifndef container_of
866 #ifdef RTE_TOOLCHAIN_MSVC
867 #define container_of(ptr, type, member) \
868  ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
869 #else
870 #define container_of(ptr, type, member) __extension__ ({ \
871  const typeof(((type *)0)->member) *_ptr = (ptr); \
872  __rte_unused type *_target_ptr = \
873  (type *)(ptr); \
874  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
875  })
876 #endif
877 #endif
878 
880 #define RTE_SWAP(a, b) \
881  __extension__ ({ \
882  typeof (a) _a = a; \
883  a = b; \
884  b = _a; \
885  })
886 
897 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
898 
899 #define _RTE_STR(x) #x
900 
901 #define RTE_STR(x) _RTE_STR(x)
902 
908 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
909 #define RTE_FMT_HEAD(fmt, ...) fmt
910 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
911 
913 #define RTE_LEN2MASK(ln, tp) \
914  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
915 
917 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
918 
933 uint64_t
934 rte_str_to_size(const char *str);
935 
967 __rte_experimental
968 char *
969 rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit);
970 
984 __rte_noreturn void
985 rte_exit(int exit_code, const char *format, ...)
986  __rte_format_printf(2, 3);
987 
988 #ifdef __cplusplus
989 }
990 #endif
991 
992 #endif
__rte_experimental char * rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit)
uint64_t rte_iova_t
Definition: rte_common.h:782
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:792
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:790
#define __rte_restrict
Definition: rte_common.h:255
uint64_t phys_addr_t
Definition: rte_common.h:772
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:798
uint64_t rte_str_to_size(const char *str)
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:280
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:794
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:796
#define __rte_noreturn
Definition: rte_common.h:455
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
static int rte_is_aligned(const void *const __rte_restrict ptr, const unsigned int align)
Definition: rte_common.h:710
#define __rte_aligned(a)
Definition: rte_common.h:121