DPDK  24.11.1
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 <stdint.h>
18 #include <stdalign.h>
19 
20 #include <rte_config.h>
21 
22 /* OS specific include */
23 #include <rte_os.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #ifndef RTE_TOOLCHAIN_MSVC
30 #ifndef typeof
31 #define typeof __typeof__
32 #endif
33 #endif
34 
35 #ifndef __cplusplus
36 #ifndef asm
37 #define asm __asm__
38 #endif
39 #endif
40 
41 #ifdef RTE_TOOLCHAIN_MSVC
42 #ifdef __cplusplus
43 #define __extension__
44 #endif
45 #endif
46 
47 #ifdef RTE_TOOLCHAIN_MSVC
48 #define __rte_constant(e) 0
49 #else
50 #define __rte_constant(e) __extension__(__builtin_constant_p(e))
51 #endif
52 
53 /*
54  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
55  * while a host application (like pmdinfogen) may have another compiler.
56  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
57  * no matter it is a target or host application.
58  */
59 #define RTE_CC_IS_GNU 0
60 #if defined __clang__
61 #define RTE_CC_CLANG
62 #elif defined __INTEL_COMPILER
63 #define RTE_CC_ICC
64 #elif defined __GNUC__
65 #define RTE_CC_GCC
66 #undef RTE_CC_IS_GNU
67 #define RTE_CC_IS_GNU 1
68 #endif
69 #if RTE_CC_IS_GNU
70 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
71  __GNUC_PATCHLEVEL__)
72 #endif
73 
86 #ifdef RTE_TOOLCHAIN_MSVC
87 #define __rte_aligned(a) __declspec(align(a))
88 #else
89 #define __rte_aligned(a) __attribute__((__aligned__(a)))
90 #endif
91 
92 #ifdef RTE_ARCH_STRICT_ALIGN
93 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
94 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
95 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
96 #else
97 typedef uint64_t unaligned_uint64_t;
98 typedef uint32_t unaligned_uint32_t;
99 typedef uint16_t unaligned_uint16_t;
100 #endif
101 
105 #ifdef RTE_TOOLCHAIN_MSVC
106 #define __rte_packed
107 #else
108 #define __rte_packed __attribute__((__packed__))
109 #endif
110 
114 #ifdef RTE_TOOLCHAIN_MSVC
115 #define __rte_may_alias
116 #else
117 #define __rte_may_alias __attribute__((__may_alias__))
118 #endif
119 
120 /******* Macro to mark functions and fields scheduled for removal *****/
121 #ifdef RTE_TOOLCHAIN_MSVC
122 #define __rte_deprecated
123 #define __rte_deprecated_msg(msg)
124 #else
125 #define __rte_deprecated __attribute__((__deprecated__))
126 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
127 #endif
128 
132 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
133 #define RTE_PRAGMA(x) _Pragma(#x)
134 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
135 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
136 #else
137 #define RTE_DEPRECATED(x)
138 #endif
139 
143 #define __rte_weak __attribute__((__weak__))
144 
148 #ifdef RTE_TOOLCHAIN_MSVC
149 #define __rte_pure
150 #else
151 #define __rte_pure __attribute__((pure))
152 #endif
153 
157 #ifdef RTE_TOOLCHAIN_MSVC
158 #define __rte_used
159 #else
160 #define __rte_used __attribute__((used))
161 #endif
162 
163 /*********** Macros to eliminate unused variable warnings ********/
164 
168 #ifdef RTE_TOOLCHAIN_MSVC
169 #define __rte_unused
170 #else
171 #define __rte_unused __attribute__((__unused__))
172 #endif
173 
177 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
178 #define __rte_restrict __restrict
179 #else
180 #define __rte_restrict restrict
181 #endif
182 
187 #define RTE_SET_USED(x) (void)(x)
188 
196 #ifdef RTE_TOOLCHAIN_MSVC
197 #define __rte_format_printf(format_index, first_arg)
198 #else
199 #if RTE_CC_IS_GNU
200 #define __rte_format_printf(format_index, first_arg) \
201  __attribute__((format(gnu_printf, format_index, first_arg)))
202 #else
203 #define __rte_format_printf(format_index, first_arg) \
204  __attribute__((format(printf, format_index, first_arg)))
205 #endif
206 #endif
207 
211 #ifdef RTE_TOOLCHAIN_MSVC
212 #define __rte_section(name) \
213  __pragma(data_seg(name)) __declspec(allocate(name))
214 #else
215 #define __rte_section(name) \
216  __attribute__((section(name)))
217 #endif
218 
224 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
225 #define __rte_alloc_size(...) \
226  __attribute__((alloc_size(__VA_ARGS__)))
227 #else
228 #define __rte_alloc_size(...)
229 #endif
230 
237 #if defined(RTE_CC_GCC)
238 #define __rte_alloc_align(argno) \
239  __attribute__((alloc_align(argno)))
240 #else
241 #define __rte_alloc_align(argno)
242 #endif
243 
248 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
249 #define __rte_malloc __attribute__((__malloc__))
250 #else
251 #define __rte_malloc
252 #endif
253 
258 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
259 #define __rte_dealloc(dealloc, argno) \
260  __attribute__((malloc(dealloc, argno)))
261 #else
262 #define __rte_dealloc(dealloc, argno)
263 #endif
264 
265 #define RTE_PRIORITY_LOG 101
266 #define RTE_PRIORITY_BUS 110
267 #define RTE_PRIORITY_CLASS 120
268 #define RTE_PRIORITY_LAST 65535
269 
270 #define RTE_PRIO(prio) \
271  RTE_PRIORITY_ ## prio
272 
282 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
283 #ifndef RTE_TOOLCHAIN_MSVC
284 #if defined(__x86_64__) || defined(__i386__)
285 #define RTE_INIT_PRIO(func, prio) \
286 static void \
287  __attribute__((constructor(RTE_PRIO(prio)), used)) \
288  __attribute__((target ("sse2"))) \
289  __attribute__((target ("no-sse3"))) \
290  __attribute__((target ("no-sse4"))) \
291  func(void)
292 #else
293 #define RTE_INIT_PRIO(func, prio) \
294 static void \
295  __attribute__((constructor(RTE_PRIO(prio)), used)) \
296  func(void)
297 #endif
298 #else
299 /* definition from the Microsoft CRT */
300 typedef int(__cdecl *_PIFV)(void);
301 
302 #define CTOR_SECTION_LOG ".CRT$XIB"
303 #define CTOR_SECTION_BUS ".CRT$XIC"
304 #define CTOR_SECTION_CLASS ".CRT$XID"
305 #define CTOR_SECTION_LAST ".CRT$XIY"
306 
307 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
308 
309 #define RTE_INIT_PRIO(name, priority) \
310  static void name(void); \
311  static int __cdecl name ## _thunk(void) { name(); return 0; } \
312  __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
313  __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
314  _PIFV name ## _pointer = &name ## _thunk; \
315  __pragma(const_seg()) \
316  static void name(void)
317 #endif
318 #endif
319 
328 #define RTE_INIT(func) \
329  RTE_INIT_PRIO(func, LAST)
330 
340 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
341 #ifndef RTE_TOOLCHAIN_MSVC
342 #define RTE_FINI_PRIO(func, prio) \
343 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
344 #else
345 #define DTOR_SECTION_LOG "mydtor$B"
346 #define DTOR_SECTION_BUS "mydtor$C"
347 #define DTOR_SECTION_CLASS "mydtor$D"
348 #define DTOR_SECTION_LAST "mydtor$Y"
349 
350 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
351 
352 #define RTE_FINI_PRIO(name, priority) \
353  static void name(void); \
354  __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
355  __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
356  __pragma(const_seg()) \
357  static void name(void)
358 #endif
359 #endif
360 
369 #define RTE_FINI(func) \
370  RTE_FINI_PRIO(func, LAST)
371 
375 #ifdef RTE_TOOLCHAIN_MSVC
376 #define __rte_noreturn
377 #else
378 #define __rte_noreturn __attribute__((noreturn))
379 #endif
380 
384 #if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
385 #define __rte_unreachable() __extension__(__builtin_unreachable())
386 #else
387 #define __rte_unreachable() __assume(0)
388 #endif
389 
413 #ifdef RTE_TOOLCHAIN_MSVC
414 #define __rte_warn_unused_result
415 #else
416 #define __rte_warn_unused_result __attribute__((warn_unused_result))
417 #endif
418 
422 #ifdef RTE_TOOLCHAIN_MSVC
423 #define __rte_always_inline
424 #else
425 #define __rte_always_inline inline __attribute__((always_inline))
426 #endif
427 
431 #define __rte_noinline __attribute__((noinline))
432 
436 #define __rte_hot __attribute__((hot))
437 
441 #ifdef RTE_TOOLCHAIN_MSVC
442 #define __rte_cold
443 #else
444 #define __rte_cold __attribute__((cold))
445 #endif
446 
453 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
454 #define __rte_assume(condition) __attribute__((assume(condition)))
455 #elif defined(RTE_TOOLCHAIN_GCC)
456 #define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
457 #elif defined(RTE_TOOLCHAIN_CLANG)
458 #define __rte_assume(condition) __extension__(__builtin_assume(condition))
459 #else
460 #define __rte_assume(condition) __assume(condition)
461 #endif
462 
466 #ifdef RTE_MALLOC_ASAN
467 #ifdef RTE_CC_CLANG
468 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
469 #else
470 #define __rte_no_asan __attribute__((no_sanitize_address))
471 #endif
472 #else /* ! RTE_MALLOC_ASAN */
473 #define __rte_no_asan
474 #endif
475 
476 /*********** Macros for pointer arithmetic ********/
477 
481 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
482 
486 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
487 
493 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
494 
498 #define RTE_CAST_FIELD(var, field, type) \
499  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
500 
501 /*********** Macros/static functions for doing alignment ********/
502 
503 
510 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
511  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
512 
519 #define RTE_ALIGN_FLOOR(val, align) \
520  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
521 
528 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
529  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
530 
537 #define RTE_ALIGN_CEIL(val, align) \
538  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
539 
547 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
548 
556 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
557 
563 #define RTE_ALIGN_MUL_CEIL(v, mul) \
564  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
565 
571 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
572  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
573 
579 #define RTE_ALIGN_MUL_NEAR(v, mul) \
580  __extension__ ({ \
581  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
582  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
583  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
584  })
585 
597 static inline int
598 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
599 {
600  return ((uintptr_t)ptr & (align - 1)) == 0;
601 }
602 
603 /*********** Macros for compile type checks ********/
604 
605 /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
606 #if !defined(static_assert) && !defined(__cplusplus)
607 #define static_assert _Static_assert
608 #endif
609 
616 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
617 
618 /*********** Cache line related macros ********/
619 
621 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
622 
624 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
625 
627 #if RTE_CACHE_LINE_SIZE == 64
628 #define RTE_CACHE_LINE_SIZE_LOG2 6
629 #elif RTE_CACHE_LINE_SIZE == 128
630 #define RTE_CACHE_LINE_SIZE_LOG2 7
631 #else
632 #error "Unsupported cache line size"
633 #endif
634 
636 #define RTE_CACHE_LINE_MIN_SIZE 64
637 
639 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
640 
642 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
643 
644 #define _RTE_CACHE_GUARD_HELPER2(unique) \
645  alignas(RTE_CACHE_LINE_SIZE) \
646  char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
647 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
648 
655 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
656 
657 /*********** PA/IOVA type definitions ********/
658 
660 typedef uint64_t phys_addr_t;
661 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
662 
670 typedef uint64_t rte_iova_t;
671 #define RTE_BAD_IOVA ((rte_iova_t)-1)
672 
673 /*********** Structure alignment markers ********/
674 
675 #ifndef RTE_TOOLCHAIN_MSVC
676 
678 __extension__ typedef void *RTE_MARKER[0];
680 __extension__ typedef uint8_t RTE_MARKER8[0];
682 __extension__ typedef uint16_t RTE_MARKER16[0];
684 __extension__ typedef uint32_t RTE_MARKER32[0];
686 __extension__ typedef uint64_t RTE_MARKER64[0];
687 
688 #endif
689 
690 /*********** Macros for calculating min and max **********/
691 
695 #define RTE_MIN(a, b) \
696  __extension__ ({ \
697  typeof (a) _a = (a); \
698  typeof (b) _b = (b); \
699  _a < _b ? _a : _b; \
700  })
701 
709 #define RTE_MIN_T(a, b, t) \
710  ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
711 
715 #define RTE_MAX(a, b) \
716  __extension__ ({ \
717  typeof (a) _a = (a); \
718  typeof (b) _b = (b); \
719  _a > _b ? _a : _b; \
720  })
721 
729 #define RTE_MAX_T(a, b, t) \
730  ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
731 
732 /*********** Other general functions / macros ********/
733 
734 #ifndef offsetof
735 
736 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
737 #endif
738 
753 #ifndef container_of
754 #ifdef RTE_TOOLCHAIN_MSVC
755 #define container_of(ptr, type, member) \
756  ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
757 #else
758 #define container_of(ptr, type, member) __extension__ ({ \
759  const typeof(((type *)0)->member) *_ptr = (ptr); \
760  __rte_unused type *_target_ptr = \
761  (type *)(ptr); \
762  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
763  })
764 #endif
765 #endif
766 
768 #define RTE_SWAP(a, b) \
769  __extension__ ({ \
770  typeof (a) _a = a; \
771  a = b; \
772  b = _a; \
773  })
774 
785 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
786 
787 #define _RTE_STR(x) #x
788 
789 #define RTE_STR(x) _RTE_STR(x)
790 
796 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
797 #define RTE_FMT_HEAD(fmt, ...) fmt
798 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
799 
801 #define RTE_LEN2MASK(ln, tp) \
802  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
803 
805 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
806 
821 uint64_t
822 rte_str_to_size(const char *str);
823 
837 __rte_noreturn void
838 rte_exit(int exit_code, const char *format, ...)
839  __rte_format_printf(2, 3);
840 
841 #ifdef __cplusplus
842 }
843 #endif
844 
845 #endif
uint64_t rte_iova_t
Definition: rte_common.h:670
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:680
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:678
#define __rte_restrict
Definition: rte_common.h:178
uint64_t phys_addr_t
Definition: rte_common.h:660
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:686
uint64_t rte_str_to_size(const char *str)
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:203
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:682
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:684
#define __rte_noreturn
Definition: rte_common.h:378
__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:598
#define __rte_aligned(a)
Definition: rte_common.h:89