DPDK  22.07.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 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 
25 #include <rte_config.h>
26 
27 /* OS specific include */
28 #include <rte_os.h>
29 
30 #ifndef typeof
31 #define typeof __typeof__
32 #endif
33 
34 #ifndef __cplusplus
35 #ifndef asm
36 #define asm __asm__
37 #endif
38 #endif
39 
41 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
42 #define RTE_STD_C11 __extension__
43 #else
44 #define RTE_STD_C11
45 #endif
46 
47 /*
48  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
49  * while a host application (like pmdinfogen) may have another compiler.
50  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
51  * no matter it is a target or host application.
52  */
53 #define RTE_CC_IS_GNU 0
54 #if defined __clang__
55 #define RTE_CC_CLANG
56 #elif defined __INTEL_COMPILER
57 #define RTE_CC_ICC
58 #elif defined __GNUC__
59 #define RTE_CC_GCC
60 #undef RTE_CC_IS_GNU
61 #define RTE_CC_IS_GNU 1
62 #endif
63 #if RTE_CC_IS_GNU
64 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
65  __GNUC_PATCHLEVEL__)
66 #endif
67 
71 #define __rte_aligned(a) __attribute__((__aligned__(a)))
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 #define __rte_packed __attribute__((__packed__))
87 
91 #define __rte_may_alias __attribute__((__may_alias__))
92 
93 /******* Macro to mark functions and fields scheduled for removal *****/
94 #define __rte_deprecated __attribute__((__deprecated__))
95 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
96 
100 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
101 #define RTE_PRAGMA(x) _Pragma(#x)
102 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
103 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
104 #else
105 #define RTE_DEPRECATED(x)
106 #endif
107 
111 #define __rte_weak __attribute__((__weak__))
112 
116 #define __rte_used __attribute__((used))
117 
118 /*********** Macros to eliminate unused variable warnings ********/
119 
123 #define __rte_unused __attribute__((__unused__))
124 
128 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
129 #define __rte_restrict __restrict
130 #else
131 #define __rte_restrict restrict
132 #endif
133 
138 #define RTE_SET_USED(x) (void)(x)
139 
147 #if RTE_CC_IS_GNU
148 #define __rte_format_printf(format_index, first_arg) \
149  __attribute__((format(gnu_printf, format_index, first_arg)))
150 #else
151 #define __rte_format_printf(format_index, first_arg) \
152  __attribute__((format(printf, format_index, first_arg)))
153 #endif
154 
160 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
161 #define __rte_alloc_size(...) \
162  __attribute__((alloc_size(__VA_ARGS__)))
163 #else
164 #define __rte_alloc_size(...)
165 #endif
166 
167 #define RTE_PRIORITY_LOG 101
168 #define RTE_PRIORITY_BUS 110
169 #define RTE_PRIORITY_CLASS 120
170 #define RTE_PRIORITY_LAST 65535
171 
172 #define RTE_PRIO(prio) \
173  RTE_PRIORITY_ ## prio
174 
184 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
185 #define RTE_INIT_PRIO(func, prio) \
186 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
187 #endif
188 
197 #define RTE_INIT(func) \
198  RTE_INIT_PRIO(func, LAST)
199 
209 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
210 #define RTE_FINI_PRIO(func, prio) \
211 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
212 #endif
213 
222 #define RTE_FINI(func) \
223  RTE_FINI_PRIO(func, LAST)
224 
228 #define __rte_noreturn __attribute__((noreturn))
229 
253 #define __rte_warn_unused_result __attribute__((warn_unused_result))
254 
258 #define __rte_always_inline inline __attribute__((always_inline))
259 
263 #define __rte_noinline __attribute__((noinline))
264 
268 #define __rte_hot __attribute__((hot))
269 
273 #define __rte_cold __attribute__((cold))
274 
278 #ifdef RTE_MALLOC_ASAN
279 #ifdef RTE_CC_CLANG
280 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
281 #else
282 #define __rte_no_asan __attribute__((no_sanitize_address))
283 #endif
284 #else /* ! RTE_MALLOC_ASAN */
285 #define __rte_no_asan
286 #endif
287 
288 /*********** Macros for pointer arithmetic ********/
289 
293 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
294 
298 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
299 
305 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
306 
310 #define RTE_CAST_FIELD(var, field, type) \
311  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
312 
313 /*********** Macros/static functions for doing alignment ********/
314 
315 
322 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
323  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
324 
331 #define RTE_ALIGN_FLOOR(val, align) \
332  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
333 
340 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
341  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
342 
349 #define RTE_ALIGN_CEIL(val, align) \
350  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
351 
359 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
360 
368 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
369 
375 #define RTE_ALIGN_MUL_CEIL(v, mul) \
376  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
377 
383 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
384  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
385 
391 #define RTE_ALIGN_MUL_NEAR(v, mul) \
392  ({ \
393  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
394  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
395  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
396  })
397 
409 static inline int
410 rte_is_aligned(void *ptr, unsigned align)
411 {
412  return RTE_PTR_ALIGN(ptr, align) == ptr;
413 }
414 
415 /*********** Macros for compile type checks ********/
416 
420 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
421 
422 /*********** Cache line related macros ********/
423 
425 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
426 
428 #define RTE_CACHE_LINE_ROUNDUP(size) \
429  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
430  RTE_CACHE_LINE_SIZE))
431 
433 #if RTE_CACHE_LINE_SIZE == 64
434 #define RTE_CACHE_LINE_SIZE_LOG2 6
435 #elif RTE_CACHE_LINE_SIZE == 128
436 #define RTE_CACHE_LINE_SIZE_LOG2 7
437 #else
438 #error "Unsupported cache line size"
439 #endif
440 
442 #define RTE_CACHE_LINE_MIN_SIZE 64
443 
445 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
446 
448 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
449 
450 /*********** PA/IOVA type definitions ********/
451 
453 typedef uint64_t phys_addr_t;
454 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
455 
463 typedef uint64_t rte_iova_t;
464 #define RTE_BAD_IOVA ((rte_iova_t)-1)
465 
466 /*********** Structure alignment markers ********/
467 
469 __extension__ typedef void *RTE_MARKER[0];
471 __extension__ typedef uint8_t RTE_MARKER8[0];
473 __extension__ typedef uint16_t RTE_MARKER16[0];
475 __extension__ typedef uint32_t RTE_MARKER32[0];
477 __extension__ typedef uint64_t RTE_MARKER64[0];
478 
489 static inline uint32_t
490 rte_combine32ms1b(uint32_t x)
491 {
492  x |= x >> 1;
493  x |= x >> 2;
494  x |= x >> 4;
495  x |= x >> 8;
496  x |= x >> 16;
497 
498  return x;
499 }
500 
511 static inline uint64_t
512 rte_combine64ms1b(uint64_t v)
513 {
514  v |= v >> 1;
515  v |= v >> 2;
516  v |= v >> 4;
517  v |= v >> 8;
518  v |= v >> 16;
519  v |= v >> 32;
520 
521  return v;
522 }
523 
524 /*********** Macros to work with powers of 2 ********/
525 
529 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
530 
537 static inline int
538 rte_is_power_of_2(uint32_t n)
539 {
540  return n && !(n & (n - 1));
541 }
542 
552 static inline uint32_t
553 rte_align32pow2(uint32_t x)
554 {
555  x--;
556  x = rte_combine32ms1b(x);
557 
558  return x + 1;
559 }
560 
570 static inline uint32_t
572 {
573  x = rte_combine32ms1b(x);
574 
575  return x - (x >> 1);
576 }
577 
587 static inline uint64_t
588 rte_align64pow2(uint64_t v)
589 {
590  v--;
591  v = rte_combine64ms1b(v);
592 
593  return v + 1;
594 }
595 
605 static inline uint64_t
607 {
608  v = rte_combine64ms1b(v);
609 
610  return v - (v >> 1);
611 }
612 
613 /*********** Macros for calculating min and max **********/
614 
618 #define RTE_MIN(a, b) \
619  __extension__ ({ \
620  typeof (a) _a = (a); \
621  typeof (b) _b = (b); \
622  _a < _b ? _a : _b; \
623  })
624 
628 #define RTE_MAX(a, b) \
629  __extension__ ({ \
630  typeof (a) _a = (a); \
631  typeof (b) _b = (b); \
632  _a > _b ? _a : _b; \
633  })
634 
635 /*********** Other general functions / macros ********/
636 
648 static inline uint32_t
649 rte_bsf32(uint32_t v)
650 {
651  return (uint32_t)__builtin_ctz(v);
652 }
653 
668 static inline int
669 rte_bsf32_safe(uint32_t v, uint32_t *pos)
670 {
671  if (v == 0)
672  return 0;
673 
674  *pos = rte_bsf32(v);
675  return 1;
676 }
677 
689 static inline uint32_t
690 rte_log2_u32(uint32_t v)
691 {
692  if (v == 0)
693  return 0;
694  v = rte_align32pow2(v);
695  return rte_bsf32(v);
696 }
697 
698 
710 static inline int
711 rte_fls_u32(uint32_t x)
712 {
713  return (x == 0) ? 0 : 32 - __builtin_clz(x);
714 }
715 
727 static inline int
728 rte_bsf64(uint64_t v)
729 {
730  return (uint32_t)__builtin_ctzll(v);
731 }
732 
747 static inline int
748 rte_bsf64_safe(uint64_t v, uint32_t *pos)
749 {
750  if (v == 0)
751  return 0;
752 
753  *pos = rte_bsf64(v);
754  return 1;
755 }
756 
769 static inline int
770 rte_fls_u64(uint64_t x)
771 {
772  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
773 }
774 
786 static inline uint32_t
787 rte_log2_u64(uint64_t v)
788 {
789  if (v == 0)
790  return 0;
791  v = rte_align64pow2(v);
792  /* we checked for v being 0 already, so no undefined behavior */
793  return rte_bsf64(v);
794 }
795 
796 #ifndef offsetof
797 
798 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
799 #endif
800 
815 #ifndef container_of
816 #define container_of(ptr, type, member) __extension__ ({ \
817  const typeof(((type *)0)->member) *_ptr = (ptr); \
818  __rte_unused type *_target_ptr = \
819  (type *)(ptr); \
820  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
821  })
822 #endif
823 
825 #define RTE_SWAP(a, b) \
826  __extension__ ({ \
827  typeof (a) _a = a; \
828  a = b; \
829  b = _a; \
830  })
831 
842 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
843 
844 #define _RTE_STR(x) #x
845 
846 #define RTE_STR(x) _RTE_STR(x)
847 
853 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
854 #define RTE_FMT_HEAD(fmt, ...) fmt
855 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
856 
858 #define RTE_LEN2MASK(ln, tp) \
859  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
860 
862 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
863 
878 static inline uint64_t
879 rte_str_to_size(const char *str)
880 {
881  char *endptr;
882  unsigned long long size;
883 
884  while (isspace((int)*str))
885  str++;
886  if (*str == '-')
887  return 0;
888 
889  errno = 0;
890  size = strtoull(str, &endptr, 0);
891  if (errno)
892  return 0;
893 
894  if (*endptr == ' ')
895  endptr++; /* allow 1 space gap */
896 
897  switch (*endptr){
898  case 'G': case 'g': size *= 1024; /* fall-through */
899  case 'M': case 'm': size *= 1024; /* fall-through */
900  case 'K': case 'k': size *= 1024; /* fall-through */
901  default:
902  break;
903  }
904  return size;
905 }
906 
920 __rte_noreturn void
921 rte_exit(int exit_code, const char *format, ...)
922  __rte_format_printf(2, 3);
923 
924 #ifdef __cplusplus
925 }
926 #endif
927 
928 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:410
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:512
uint64_t rte_iova_t
Definition: rte_common.h:463
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:711
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:787
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:490
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:588
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:690
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:471
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:469
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:649
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:606
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:553
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:748
uint64_t phys_addr_t
Definition: rte_common.h:453
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:477
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:151
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:770
static int rte_bsf32_safe(uint32_t v, uint32_t *pos)
Definition: rte_common.h:669
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:359
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:538
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:473
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:728
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:475
#define __rte_noreturn
Definition: rte_common.h:228
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:571
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:879
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_aligned(a)
Definition: rte_common.h:71