DPDK  23.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 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <stdint.h>
20 #include <limits.h>
21 
22 #include <rte_config.h>
23 
24 /* OS specific include */
25 #include <rte_os.h>
26 
27 #ifndef RTE_TOOLCHAIN_MSVC
28 #ifndef typeof
29 #define typeof __typeof__
30 #endif
31 #endif
32 
33 #ifndef __cplusplus
34 #ifndef asm
35 #define asm __asm__
36 #endif
37 #endif
38 
39 #ifdef RTE_TOOLCHAIN_MSVC
40 #define __extension__
41 #endif
42 
43 /*
44  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
45  * while a host application (like pmdinfogen) may have another compiler.
46  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
47  * no matter it is a target or host application.
48  */
49 #define RTE_CC_IS_GNU 0
50 #if defined __clang__
51 #define RTE_CC_CLANG
52 #elif defined __INTEL_COMPILER
53 #define RTE_CC_ICC
54 #elif defined __GNUC__
55 #define RTE_CC_GCC
56 #undef RTE_CC_IS_GNU
57 #define RTE_CC_IS_GNU 1
58 #endif
59 #if RTE_CC_IS_GNU
60 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
61  __GNUC_PATCHLEVEL__)
62 #endif
63 
67 #ifdef RTE_TOOLCHAIN_MSVC
68 #define __rte_aligned(a)
69 #else
70 #define __rte_aligned(a) __attribute__((__aligned__(a)))
71 #endif
72 
73 #ifdef RTE_ARCH_STRICT_ALIGN
74 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
75 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
76 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
77 #else
78 typedef uint64_t unaligned_uint64_t;
79 typedef uint32_t unaligned_uint32_t;
80 typedef uint16_t unaligned_uint16_t;
81 #endif
82 
86 #ifdef RTE_TOOLCHAIN_MSVC
87 #define __rte_packed
88 #else
89 #define __rte_packed __attribute__((__packed__))
90 #endif
91 
95 #ifdef RTE_TOOLCHAIN_MSVC
96 #define __rte_may_alias
97 #else
98 #define __rte_may_alias __attribute__((__may_alias__))
99 #endif
100 
101 /******* Macro to mark functions and fields scheduled for removal *****/
102 #ifdef RTE_TOOLCHAIN_MSVC
103 #define __rte_deprecated
104 #define __rte_deprecated_msg(msg)
105 #else
106 #define __rte_deprecated __attribute__((__deprecated__))
107 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
108 #endif
109 
113 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
114 #define RTE_PRAGMA(x) _Pragma(#x)
115 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
116 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
117 #else
118 #define RTE_DEPRECATED(x)
119 #endif
120 
124 #define __rte_weak __attribute__((__weak__))
125 
129 #ifdef RTE_TOOLCHAIN_MSVC
130 #define __rte_used
131 #else
132 #define __rte_used __attribute__((used))
133 #endif
134 
135 /*********** Macros to eliminate unused variable warnings ********/
136 
140 #ifdef RTE_TOOLCHAIN_MSVC
141 #define __rte_unused
142 #else
143 #define __rte_unused __attribute__((__unused__))
144 #endif
145 
149 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
150 #define __rte_restrict __restrict
151 #else
152 #define __rte_restrict restrict
153 #endif
154 
159 #define RTE_SET_USED(x) (void)(x)
160 
168 #ifdef RTE_TOOLCHAIN_MSVC
169 #define __rte_format_printf(format_index, first_arg)
170 #else
171 #if RTE_CC_IS_GNU
172 #define __rte_format_printf(format_index, first_arg) \
173  __attribute__((format(gnu_printf, format_index, first_arg)))
174 #else
175 #define __rte_format_printf(format_index, first_arg) \
176  __attribute__((format(printf, format_index, first_arg)))
177 #endif
178 #endif
179 
185 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
186 #define __rte_alloc_size(...) \
187  __attribute__((alloc_size(__VA_ARGS__)))
188 #else
189 #define __rte_alloc_size(...)
190 #endif
191 
192 #define RTE_PRIORITY_LOG 101
193 #define RTE_PRIORITY_BUS 110
194 #define RTE_PRIORITY_CLASS 120
195 #define RTE_PRIORITY_LAST 65535
196 
197 #define RTE_PRIO(prio) \
198  RTE_PRIORITY_ ## prio
199 
209 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
210 #ifndef RTE_TOOLCHAIN_MSVC
211 #if defined(__x86_64__) || defined(__i386__)
212 #define RTE_INIT_PRIO(func, prio) \
213 static void \
214  __attribute__((constructor(RTE_PRIO(prio)), used)) \
215  __attribute__((target ("sse2"))) \
216  __attribute__((target ("no-sse3"))) \
217  __attribute__((target ("no-sse4"))) \
218  func(void)
219 #else
220 #define RTE_INIT_PRIO(func, prio) \
221 static void \
222  __attribute__((constructor(RTE_PRIO(prio)), used)) \
223  func(void)
224 #endif
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 
510 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
511 
512 /*********** Cache line related macros ********/
513 
515 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
516 
518 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
519 
521 #if RTE_CACHE_LINE_SIZE == 64
522 #define RTE_CACHE_LINE_SIZE_LOG2 6
523 #elif RTE_CACHE_LINE_SIZE == 128
524 #define RTE_CACHE_LINE_SIZE_LOG2 7
525 #else
526 #error "Unsupported cache line size"
527 #endif
528 
530 #define RTE_CACHE_LINE_MIN_SIZE 64
531 
533 #ifdef RTE_TOOLCHAIN_MSVC
534 #define __rte_cache_aligned
535 #else
536 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
537 #endif
538 
540 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
541 
542 #define _RTE_CACHE_GUARD_HELPER2(unique) \
543  char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES] \
544  __rte_cache_aligned
545 #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
546 
553 #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
554 
555 /*********** PA/IOVA type definitions ********/
556 
558 typedef uint64_t phys_addr_t;
559 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
560 
568 typedef uint64_t rte_iova_t;
569 #define RTE_BAD_IOVA ((rte_iova_t)-1)
570 
571 /*********** Structure alignment markers ********/
572 
573 #ifndef RTE_TOOLCHAIN_MSVC
574 
576 __extension__ typedef void *RTE_MARKER[0];
578 __extension__ typedef uint8_t RTE_MARKER8[0];
580 __extension__ typedef uint16_t RTE_MARKER16[0];
582 __extension__ typedef uint32_t RTE_MARKER32[0];
584 __extension__ typedef uint64_t RTE_MARKER64[0];
585 
586 #endif
587 
588 /*********** Macros for calculating min and max **********/
589 
593 #define RTE_MIN(a, b) \
594  __extension__ ({ \
595  typeof (a) _a = (a); \
596  typeof (b) _b = (b); \
597  _a < _b ? _a : _b; \
598  })
599 
603 #define RTE_MAX(a, b) \
604  __extension__ ({ \
605  typeof (a) _a = (a); \
606  typeof (b) _b = (b); \
607  _a > _b ? _a : _b; \
608  })
609 
610 /*********** Other general functions / macros ********/
611 
612 #ifndef offsetof
613 
614 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
615 #endif
616 
631 #ifndef container_of
632 #ifdef RTE_TOOLCHAIN_MSVC
633 #define container_of(ptr, type, member) \
634  ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
635 #else
636 #define container_of(ptr, type, member) __extension__ ({ \
637  const typeof(((type *)0)->member) *_ptr = (ptr); \
638  __rte_unused type *_target_ptr = \
639  (type *)(ptr); \
640  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
641  })
642 #endif
643 #endif
644 
646 #define RTE_SWAP(a, b) \
647  __extension__ ({ \
648  typeof (a) _a = a; \
649  a = b; \
650  b = _a; \
651  })
652 
663 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
664 
665 #define _RTE_STR(x) #x
666 
667 #define RTE_STR(x) _RTE_STR(x)
668 
674 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
675 #define RTE_FMT_HEAD(fmt, ...) fmt
676 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
677 
679 #define RTE_LEN2MASK(ln, tp) \
680  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
681 
683 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
684 
699 uint64_t
700 rte_str_to_size(const char *str);
701 
715 __rte_noreturn void
716 rte_exit(int exit_code, const char *format, ...)
717  __rte_format_printf(2, 3);
718 
719 #ifdef __cplusplus
720 }
721 #endif
722 
723 #endif
uint64_t rte_iova_t
Definition: rte_common.h:568
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:578
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:576
#define __rte_restrict
Definition: rte_common.h:150
uint64_t phys_addr_t
Definition: rte_common.h:558
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:584
uint64_t rte_str_to_size(const char *str)
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:175
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:580
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:582
#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:70