DPDK  22.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 typeof
28 #define typeof __typeof__
29 #endif
30 
31 #ifndef __cplusplus
32 #ifndef asm
33 #define asm __asm__
34 #endif
35 #endif
36 
38 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
39 #define RTE_STD_C11 __extension__
40 #else
41 #define RTE_STD_C11
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 #define __rte_aligned(a) __attribute__((__aligned__(a)))
69 
70 #ifdef RTE_ARCH_STRICT_ALIGN
71 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
72 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
73 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
74 #else
75 typedef uint64_t unaligned_uint64_t;
76 typedef uint32_t unaligned_uint32_t;
77 typedef uint16_t unaligned_uint16_t;
78 #endif
79 
83 #define __rte_packed __attribute__((__packed__))
84 
88 #define __rte_may_alias __attribute__((__may_alias__))
89 
90 /******* Macro to mark functions and fields scheduled for removal *****/
91 #define __rte_deprecated __attribute__((__deprecated__))
92 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
93 
97 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
98 #define RTE_PRAGMA(x) _Pragma(#x)
99 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
100 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
101 #else
102 #define RTE_DEPRECATED(x)
103 #endif
104 
108 #define __rte_weak __attribute__((__weak__))
109 
113 #define __rte_used __attribute__((used))
114 
115 /*********** Macros to eliminate unused variable warnings ********/
116 
120 #define __rte_unused __attribute__((__unused__))
121 
125 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
126 #define __rte_restrict __restrict
127 #else
128 #define __rte_restrict restrict
129 #endif
130 
135 #define RTE_SET_USED(x) (void)(x)
136 
144 #if RTE_CC_IS_GNU
145 #define __rte_format_printf(format_index, first_arg) \
146  __attribute__((format(gnu_printf, format_index, first_arg)))
147 #else
148 #define __rte_format_printf(format_index, first_arg) \
149  __attribute__((format(printf, format_index, first_arg)))
150 #endif
151 
157 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
158 #define __rte_alloc_size(...) \
159  __attribute__((alloc_size(__VA_ARGS__)))
160 #else
161 #define __rte_alloc_size(...)
162 #endif
163 
164 #define RTE_PRIORITY_LOG 101
165 #define RTE_PRIORITY_BUS 110
166 #define RTE_PRIORITY_CLASS 120
167 #define RTE_PRIORITY_LAST 65535
168 
169 #define RTE_PRIO(prio) \
170  RTE_PRIORITY_ ## prio
171 
181 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
182 #if defined(__x86_64__) || defined(__i386__)
183 #define RTE_INIT_PRIO(func, prio) \
184 static void \
185  __attribute__((constructor(RTE_PRIO(prio)), used)) \
186  __attribute__((target ("sse2"))) \
187  __attribute__((target ("no-sse3"))) \
188  __attribute__((target ("no-sse4"))) \
189  func(void)
190 #else
191 #define RTE_INIT_PRIO(func, prio) \
192 static void \
193  __attribute__((constructor(RTE_PRIO(prio)), used)) \
194  func(void)
195 #endif
196 #endif
197 
206 #define RTE_INIT(func) \
207  RTE_INIT_PRIO(func, LAST)
208 
218 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
219 #define RTE_FINI_PRIO(func, prio) \
220 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
221 #endif
222 
231 #define RTE_FINI(func) \
232  RTE_FINI_PRIO(func, LAST)
233 
237 #define __rte_noreturn __attribute__((noreturn))
238 
262 #define __rte_warn_unused_result __attribute__((warn_unused_result))
263 
267 #define __rte_always_inline inline __attribute__((always_inline))
268 
272 #define __rte_noinline __attribute__((noinline))
273 
277 #define __rte_hot __attribute__((hot))
278 
282 #define __rte_cold __attribute__((cold))
283 
287 #ifdef RTE_MALLOC_ASAN
288 #ifdef RTE_CC_CLANG
289 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
290 #else
291 #define __rte_no_asan __attribute__((no_sanitize_address))
292 #endif
293 #else /* ! RTE_MALLOC_ASAN */
294 #define __rte_no_asan
295 #endif
296 
297 /*********** Macros for pointer arithmetic ********/
298 
302 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
303 
307 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
308 
314 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
315 
319 #define RTE_CAST_FIELD(var, field, type) \
320  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
321 
322 /*********** Macros/static functions for doing alignment ********/
323 
324 
331 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
332  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
333 
340 #define RTE_ALIGN_FLOOR(val, align) \
341  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
342 
349 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
350  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
351 
358 #define RTE_ALIGN_CEIL(val, align) \
359  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
360 
368 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
369 
377 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
378 
384 #define RTE_ALIGN_MUL_CEIL(v, mul) \
385  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
386 
392 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
393  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
394 
400 #define RTE_ALIGN_MUL_NEAR(v, mul) \
401  ({ \
402  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
403  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
404  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
405  })
406 
418 static inline int
419 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
420 {
421  return ((uintptr_t)ptr & (align - 1)) == 0;
422 }
423 
424 /*********** Macros for compile type checks ********/
425 
429 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
430 
431 /*********** Cache line related macros ********/
432 
434 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
435 
437 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
438 
440 #if RTE_CACHE_LINE_SIZE == 64
441 #define RTE_CACHE_LINE_SIZE_LOG2 6
442 #elif RTE_CACHE_LINE_SIZE == 128
443 #define RTE_CACHE_LINE_SIZE_LOG2 7
444 #else
445 #error "Unsupported cache line size"
446 #endif
447 
449 #define RTE_CACHE_LINE_MIN_SIZE 64
450 
452 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
453 
455 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
456 
457 /*********** PA/IOVA type definitions ********/
458 
460 typedef uint64_t phys_addr_t;
461 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
462 
470 typedef uint64_t rte_iova_t;
471 #define RTE_BAD_IOVA ((rte_iova_t)-1)
472 
473 /*********** Structure alignment markers ********/
474 
476 __extension__ typedef void *RTE_MARKER[0];
478 __extension__ typedef uint8_t RTE_MARKER8[0];
480 __extension__ typedef uint16_t RTE_MARKER16[0];
482 __extension__ typedef uint32_t RTE_MARKER32[0];
484 __extension__ typedef uint64_t RTE_MARKER64[0];
485 
496 static inline uint32_t
497 rte_combine32ms1b(uint32_t x)
498 {
499  x |= x >> 1;
500  x |= x >> 2;
501  x |= x >> 4;
502  x |= x >> 8;
503  x |= x >> 16;
504 
505  return x;
506 }
507 
518 static inline uint64_t
519 rte_combine64ms1b(uint64_t v)
520 {
521  v |= v >> 1;
522  v |= v >> 2;
523  v |= v >> 4;
524  v |= v >> 8;
525  v |= v >> 16;
526  v |= v >> 32;
527 
528  return v;
529 }
530 
531 /*********** Macros to work with powers of 2 ********/
532 
536 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
537 
544 static inline int
545 rte_is_power_of_2(uint32_t n)
546 {
547  return n && !(n & (n - 1));
548 }
549 
559 static inline uint32_t
560 rte_align32pow2(uint32_t x)
561 {
562  x--;
563  x = rte_combine32ms1b(x);
564 
565  return x + 1;
566 }
567 
577 static inline uint32_t
579 {
580  x = rte_combine32ms1b(x);
581 
582  return x - (x >> 1);
583 }
584 
594 static inline uint64_t
595 rte_align64pow2(uint64_t v)
596 {
597  v--;
598  v = rte_combine64ms1b(v);
599 
600  return v + 1;
601 }
602 
612 static inline uint64_t
614 {
615  v = rte_combine64ms1b(v);
616 
617  return v - (v >> 1);
618 }
619 
620 /*********** Macros for calculating min and max **********/
621 
625 #define RTE_MIN(a, b) \
626  __extension__ ({ \
627  typeof (a) _a = (a); \
628  typeof (b) _b = (b); \
629  _a < _b ? _a : _b; \
630  })
631 
635 #define RTE_MAX(a, b) \
636  __extension__ ({ \
637  typeof (a) _a = (a); \
638  typeof (b) _b = (b); \
639  _a > _b ? _a : _b; \
640  })
641 
642 /*********** Other general functions / macros ********/
643 
655 static inline uint32_t
656 rte_bsf32(uint32_t v)
657 {
658  return (uint32_t)__builtin_ctz(v);
659 }
660 
675 static inline int
676 rte_bsf32_safe(uint32_t v, uint32_t *pos)
677 {
678  if (v == 0)
679  return 0;
680 
681  *pos = rte_bsf32(v);
682  return 1;
683 }
684 
696 static inline uint32_t
697 rte_log2_u32(uint32_t v)
698 {
699  if (v == 0)
700  return 0;
701  v = rte_align32pow2(v);
702  return rte_bsf32(v);
703 }
704 
705 
717 static inline uint32_t
718 rte_fls_u32(uint32_t x)
719 {
720  return (x == 0) ? 0 : 32 - __builtin_clz(x);
721 }
722 
734 static inline uint32_t
735 rte_bsf64(uint64_t v)
736 {
737  return (uint32_t)__builtin_ctzll(v);
738 }
739 
754 static inline int
755 rte_bsf64_safe(uint64_t v, uint32_t *pos)
756 {
757  if (v == 0)
758  return 0;
759 
760  *pos = rte_bsf64(v);
761  return 1;
762 }
763 
776 static inline uint32_t
777 rte_fls_u64(uint64_t x)
778 {
779  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
780 }
781 
793 static inline uint32_t
794 rte_log2_u64(uint64_t v)
795 {
796  if (v == 0)
797  return 0;
798  v = rte_align64pow2(v);
799  /* we checked for v being 0 already, so no undefined behavior */
800  return rte_bsf64(v);
801 }
802 
803 #ifndef offsetof
804 
805 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
806 #endif
807 
822 #ifndef container_of
823 #define container_of(ptr, type, member) __extension__ ({ \
824  const typeof(((type *)0)->member) *_ptr = (ptr); \
825  __rte_unused type *_target_ptr = \
826  (type *)(ptr); \
827  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
828  })
829 #endif
830 
832 #define RTE_SWAP(a, b) \
833  __extension__ ({ \
834  typeof (a) _a = a; \
835  a = b; \
836  b = _a; \
837  })
838 
849 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
850 
851 #define _RTE_STR(x) #x
852 
853 #define RTE_STR(x) _RTE_STR(x)
854 
860 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
861 #define RTE_FMT_HEAD(fmt, ...) fmt
862 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
863 
865 #define RTE_LEN2MASK(ln, tp) \
866  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
867 
869 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
870 
885 uint64_t
886 rte_str_to_size(const char *str);
887 
901 __rte_noreturn void
902 rte_exit(int exit_code, const char *format, ...)
903  __rte_format_printf(2, 3);
904 
905 #ifdef __cplusplus
906 }
907 #endif
908 
909 #endif
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:519
static uint32_t rte_fls_u64(uint64_t x)
Definition: rte_common.h:777
uint64_t rte_iova_t
Definition: rte_common.h:470
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:794
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:497
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:595
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:697
static uint32_t rte_fls_u32(uint32_t x)
Definition: rte_common.h:718
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:478
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:476
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:656
#define __rte_restrict
Definition: rte_common.h:126
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:613
static uint32_t rte_bsf64(uint64_t v)
Definition: rte_common.h:735
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:560
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:755
uint64_t phys_addr_t
Definition: rte_common.h:460
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:484
uint64_t rte_str_to_size(const char *str)
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:148
static int rte_bsf32_safe(uint32_t v, uint32_t *pos)
Definition: rte_common.h:676
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:545
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:480
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:482
#define __rte_noreturn
Definition: rte_common.h:237
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:578
__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:419
#define __rte_aligned(a)
Definition: rte_common.h:68