DPDK  24.03.0-rc1
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 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <assert.h>
20 #include <stdint.h>
21 #include <limits.h>
22 
23 #include <rte_config.h>
24 
25 /* OS specific include */
26 #include <rte_os.h>
27 
28 #ifndef RTE_TOOLCHAIN_MSVC
29 #ifndef typeof
30 #define typeof __typeof__
31 #endif
32 #endif
33 
34 #ifndef __cplusplus
35 #ifndef asm
36 #define asm __asm__
37 #endif
38 #endif
39 
40 #ifdef RTE_TOOLCHAIN_MSVC
41 #define __extension__
42 #endif
43 
44 /*
45  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
46  * while a host application (like pmdinfogen) may have another compiler.
47  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
48  * no matter it is a target or host application.
49  */
50 #define RTE_CC_IS_GNU 0
51 #if defined __clang__
52 #define RTE_CC_CLANG
53 #elif defined __INTEL_COMPILER
54 #define RTE_CC_ICC
55 #elif defined __GNUC__
56 #define RTE_CC_GCC
57 #undef RTE_CC_IS_GNU
58 #define RTE_CC_IS_GNU 1
59 #endif
60 #if RTE_CC_IS_GNU
61 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
62  __GNUC_PATCHLEVEL__)
63 #endif
64 
68 #ifdef RTE_TOOLCHAIN_MSVC
69 #define __rte_aligned(a)
70 #else
71 #define __rte_aligned(a) __attribute__((__aligned__(a)))
72 #endif
73 
74 #ifdef RTE_ARCH_STRICT_ALIGN
75 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
76 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
77 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
78 #else
79 typedef uint64_t unaligned_uint64_t;
80 typedef uint32_t unaligned_uint32_t;
81 typedef uint16_t unaligned_uint16_t;
82 #endif
83 
87 #ifdef RTE_TOOLCHAIN_MSVC
88 #define __rte_packed
89 #else
90 #define __rte_packed __attribute__((__packed__))
91 #endif
92 
96 #ifdef RTE_TOOLCHAIN_MSVC
97 #define __rte_may_alias
98 #else
99 #define __rte_may_alias __attribute__((__may_alias__))
100 #endif
101 
102 /******* Macro to mark functions and fields scheduled for removal *****/
103 #ifdef RTE_TOOLCHAIN_MSVC
104 #define __rte_deprecated
105 #define __rte_deprecated_msg(msg)
106 #else
107 #define __rte_deprecated __attribute__((__deprecated__))
108 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
109 #endif
110 
114 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
115 #define RTE_PRAGMA(x) _Pragma(#x)
116 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
117 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
118 #else
119 #define RTE_DEPRECATED(x)
120 #endif
121 
125 #define __rte_weak __attribute__((__weak__))
126 
130 #ifdef RTE_TOOLCHAIN_MSVC
131 #define __rte_used
132 #else
133 #define __rte_used __attribute__((used))
134 #endif
135 
136 /*********** Macros to eliminate unused variable warnings ********/
137 
141 #ifdef RTE_TOOLCHAIN_MSVC
142 #define __rte_unused
143 #else
144 #define __rte_unused __attribute__((__unused__))
145 #endif
146 
150 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
151 #define __rte_restrict __restrict
152 #else
153 #define __rte_restrict restrict
154 #endif
155 
160 #define RTE_SET_USED(x) (void)(x)
161 
169 #ifdef RTE_TOOLCHAIN_MSVC
170 #define __rte_format_printf(format_index, first_arg)
171 #else
172 #if RTE_CC_IS_GNU
173 #define __rte_format_printf(format_index, first_arg) \
174  __attribute__((format(gnu_printf, format_index, first_arg)))
175 #else
176 #define __rte_format_printf(format_index, first_arg) \
177  __attribute__((format(printf, format_index, first_arg)))
178 #endif
179 #endif
180 
184 #ifdef RTE_TOOLCHAIN_MSVC
185 #define __rte_section(name) \
186  __pragma(data_seg(name)) __declspec(allocate(name))
187 #else
188 #define __rte_section(name) \
189  __attribute__((section(name)))
190 #endif
191 
197 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
198 #define __rte_alloc_size(...) \
199  __attribute__((alloc_size(__VA_ARGS__)))
200 #else
201 #define __rte_alloc_size(...)
202 #endif
203 
204 #define RTE_PRIORITY_LOG 101
205 #define RTE_PRIORITY_BUS 110
206 #define RTE_PRIORITY_CLASS 120
207 #define RTE_PRIORITY_LAST 65535
208 
209 #define RTE_PRIO(prio) \
210  RTE_PRIORITY_ ## prio
211 
221 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
222 #ifndef RTE_TOOLCHAIN_MSVC
223 #define RTE_INIT_PRIO(func, prio) \
224 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
225 #else
226 /* definition from the Microsoft CRT */
227 typedef int(__cdecl *_PIFV)(void);
228 
229 #define CTOR_SECTION_LOG ".CRT$XIB"
230 #define CTOR_SECTION_BUS ".CRT$XIC"
231 #define CTOR_SECTION_CLASS ".CRT$XID"
232 #define CTOR_SECTION_LAST ".CRT$XIY"
233 
234 #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
235 
236 #define RTE_INIT_PRIO(name, priority) \
237  static void name(void); \
238  static int __cdecl name ## _thunk(void) { name(); return 0; } \
239  __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
240  __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
241  _PIFV name ## _pointer = &name ## _thunk; \
242  __pragma(const_seg()) \
243  static void name(void)
244 #endif
245 #endif
246 
255 #define RTE_INIT(func) \
256  RTE_INIT_PRIO(func, LAST)
257 
267 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
268 #ifndef RTE_TOOLCHAIN_MSVC
269 #define RTE_FINI_PRIO(func, prio) \
270 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
271 #else
272 #define DTOR_SECTION_LOG "mydtor$B"
273 #define DTOR_SECTION_BUS "mydtor$C"
274 #define DTOR_SECTION_CLASS "mydtor$D"
275 #define DTOR_SECTION_LAST "mydtor$Y"
276 
277 #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
278 
279 #define RTE_FINI_PRIO(name, priority) \
280  static void name(void); \
281  __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
282  __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \
283  __pragma(const_seg()) \
284  static void name(void)
285 #endif
286 #endif
287 
296 #define RTE_FINI(func) \
297  RTE_FINI_PRIO(func, LAST)
298 
302 #ifdef RTE_TOOLCHAIN_MSVC
303 #define __rte_noreturn
304 #else
305 #define __rte_noreturn __attribute__((noreturn))
306 #endif
307 
331 #ifdef RTE_TOOLCHAIN_MSVC
332 #define __rte_warn_unused_result
333 #else
334 #define __rte_warn_unused_result __attribute__((warn_unused_result))
335 #endif
336 
340 #ifdef RTE_TOOLCHAIN_MSVC
341 #define __rte_always_inline
342 #else
343 #define __rte_always_inline inline __attribute__((always_inline))
344 #endif
345 
349 #define __rte_noinline __attribute__((noinline))
350 
354 #define __rte_hot __attribute__((hot))
355 
359 #ifdef RTE_TOOLCHAIN_MSVC
360 #define __rte_cold
361 #else
362 #define __rte_cold __attribute__((cold))
363 #endif
364 
368 #ifdef RTE_MALLOC_ASAN
369 #ifdef RTE_CC_CLANG
370 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
371 #else
372 #define __rte_no_asan __attribute__((no_sanitize_address))
373 #endif
374 #else /* ! RTE_MALLOC_ASAN */
375 #define __rte_no_asan
376 #endif
377 
378 /*********** Macros for pointer arithmetic ********/
379 
383 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
384 
388 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
389 
395 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
396 
400 #define RTE_CAST_FIELD(var, field, type) \
401  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
402 
403 /*********** Macros/static functions for doing alignment ********/
404 
405 
412 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
413  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
414 
421 #define RTE_ALIGN_FLOOR(val, align) \
422  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
423 
430 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
431  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
432 
439 #define RTE_ALIGN_CEIL(val, align) \
440  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
441 
449 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
450 
458 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
459 
465 #define RTE_ALIGN_MUL_CEIL(v, mul) \
466  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
467 
473 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
474  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
475 
481 #define RTE_ALIGN_MUL_NEAR(v, mul) \
482  __extension__ ({ \
483  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
484  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
485  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
486  })
487 
499 static inline int
500 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
501 {
502  return ((uintptr_t)ptr & (align - 1)) == 0;
503 }
504 
505 /*********** Macros for compile type checks ********/
506 
507 /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
508 #if !defined(static_assert) && !defined(__cplusplus)
509 #define static_assert _Static_assert
510 #endif
511 
518 #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
519 
520 /*********** Cache line related macros ********/
521 
523 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
524 
526 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
527 
529 #if RTE_CACHE_LINE_SIZE == 64
530 #define RTE_CACHE_LINE_SIZE_LOG2 6
531 #elif RTE_CACHE_LINE_SIZE == 128
532 #define RTE_CACHE_LINE_SIZE_LOG2 7
533 #else
534 #error "Unsupported cache line size"
535 #endif
536 
538 #define RTE_CACHE_LINE_MIN_SIZE 64
539 
541 #ifdef RTE_TOOLCHAIN_MSVC
542 #define __rte_cache_aligned
543 #else
544 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
545 #endif
546 
548 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
549 
550 #define _RTE_CACHE_GUARD_HELPER2(unique) \
551  char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES] \
552  __rte_cache_aligned
553 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
554 
561 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
562 
563 /*********** PA/IOVA type definitions ********/
564 
566 typedef uint64_t phys_addr_t;
567 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
568 
576 typedef uint64_t rte_iova_t;
577 #define RTE_BAD_IOVA ((rte_iova_t)-1)
578 
579 /*********** Structure alignment markers ********/
580 
581 #ifndef RTE_TOOLCHAIN_MSVC
582 
584 __extension__ typedef void *RTE_MARKER[0];
586 __extension__ typedef uint8_t RTE_MARKER8[0];
588 __extension__ typedef uint16_t RTE_MARKER16[0];
590 __extension__ typedef uint32_t RTE_MARKER32[0];
592 __extension__ typedef uint64_t RTE_MARKER64[0];
593 
594 #endif
595 
596 /*********** Macros for calculating min and max **********/
597 
601 #define RTE_MIN(a, b) \
602  __extension__ ({ \
603  typeof (a) _a = (a); \
604  typeof (b) _b = (b); \
605  _a < _b ? _a : _b; \
606  })
607 
615 #define RTE_MIN_T(a, b, t) \
616  ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
617 
621 #define RTE_MAX(a, b) \
622  __extension__ ({ \
623  typeof (a) _a = (a); \
624  typeof (b) _b = (b); \
625  _a > _b ? _a : _b; \
626  })
627 
635 #define RTE_MAX_T(a, b, t) \
636  ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
637 
638 /*********** Other general functions / macros ********/
639 
640 #ifndef offsetof
641 
642 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
643 #endif
644 
659 #ifndef container_of
660 #ifdef RTE_TOOLCHAIN_MSVC
661 #define container_of(ptr, type, member) \
662  ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
663 #else
664 #define container_of(ptr, type, member) __extension__ ({ \
665  const typeof(((type *)0)->member) *_ptr = (ptr); \
666  __rte_unused type *_target_ptr = \
667  (type *)(ptr); \
668  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
669  })
670 #endif
671 #endif
672 
674 #define RTE_SWAP(a, b) \
675  __extension__ ({ \
676  typeof (a) _a = a; \
677  a = b; \
678  b = _a; \
679  })
680 
691 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
692 
693 #define _RTE_STR(x) #x
694 
695 #define RTE_STR(x) _RTE_STR(x)
696 
702 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
703 #define RTE_FMT_HEAD(fmt, ...) fmt
704 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
705 
707 #define RTE_LEN2MASK(ln, tp) \
708  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
709 
711 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
712 
727 uint64_t
728 rte_str_to_size(const char *str);
729 
743 __rte_noreturn void
744 rte_exit(int exit_code, const char *format, ...)
745  __rte_format_printf(2, 3);
746 
747 #ifdef __cplusplus
748 }
749 #endif
750 
751 #endif
uint64_t rte_iova_t
Definition: rte_common.h:576
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:586
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:584
#define __rte_restrict
Definition: rte_common.h:151
uint64_t phys_addr_t
Definition: rte_common.h:566
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:592
uint64_t rte_str_to_size(const char *str)
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:176
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:588
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:590
#define __rte_noreturn
Definition: rte_common.h:305
__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:500
#define __rte_aligned(a)
Definition: rte_common.h:71