DPDK  21.11.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 
88 /******* Macro to mark functions and fields scheduled for removal *****/
89 #define __rte_deprecated __attribute__((__deprecated__))
90 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
91 
95 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
96 #define RTE_PRAGMA(x) _Pragma(#x)
97 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
98 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
99 #else
100 #define RTE_DEPRECATED(x)
101 #endif
102 
106 #define __rte_weak __attribute__((__weak__))
107 
111 #define __rte_used __attribute__((used))
112 
113 /*********** Macros to eliminate unused variable warnings ********/
114 
118 #define __rte_unused __attribute__((__unused__))
119 
123 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
124 #define __rte_restrict __restrict
125 #else
126 #define __rte_restrict restrict
127 #endif
128 
133 #define RTE_SET_USED(x) (void)(x)
134 
142 #if RTE_CC_IS_GNU
143 #define __rte_format_printf(format_index, first_arg) \
144  __attribute__((format(gnu_printf, format_index, first_arg)))
145 #else
146 #define __rte_format_printf(format_index, first_arg) \
147  __attribute__((format(printf, format_index, first_arg)))
148 #endif
149 
155 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
156 #define __rte_alloc_size(...) \
157  __attribute__((alloc_size(__VA_ARGS__)))
158 #else
159 #define __rte_alloc_size(...)
160 #endif
161 
162 #define RTE_PRIORITY_LOG 101
163 #define RTE_PRIORITY_BUS 110
164 #define RTE_PRIORITY_CLASS 120
165 #define RTE_PRIORITY_LAST 65535
166 
167 #define RTE_PRIO(prio) \
168  RTE_PRIORITY_ ## prio
169 
179 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
180 #define RTE_INIT_PRIO(func, prio) \
181 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
182 #endif
183 
192 #define RTE_INIT(func) \
193  RTE_INIT_PRIO(func, LAST)
194 
204 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
205 #define RTE_FINI_PRIO(func, prio) \
206 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
207 #endif
208 
217 #define RTE_FINI(func) \
218  RTE_FINI_PRIO(func, LAST)
219 
223 #define __rte_noreturn __attribute__((noreturn))
224 
228 #define __rte_always_inline inline __attribute__((always_inline))
229 
233 #define __rte_noinline __attribute__((noinline))
234 
238 #define __rte_hot __attribute__((hot))
239 
243 #define __rte_cold __attribute__((cold))
244 
245 /*********** Macros for pointer arithmetic ********/
246 
250 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
251 
255 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
256 
262 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
263 
267 #define RTE_CAST_FIELD(var, field, type) \
268  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
269 
270 /*********** Macros/static functions for doing alignment ********/
271 
272 
279 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
280  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
281 
288 #define RTE_ALIGN_FLOOR(val, align) \
289  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
290 
297 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
298  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
299 
306 #define RTE_ALIGN_CEIL(val, align) \
307  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
308 
316 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
317 
325 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
326 
332 #define RTE_ALIGN_MUL_CEIL(v, mul) \
333  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
334 
340 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
341  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
342 
348 #define RTE_ALIGN_MUL_NEAR(v, mul) \
349  ({ \
350  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
351  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
352  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
353  })
354 
366 static inline int
367 rte_is_aligned(void *ptr, unsigned align)
368 {
369  return RTE_PTR_ALIGN(ptr, align) == ptr;
370 }
371 
372 /*********** Macros for compile type checks ********/
373 
377 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
378 
379 /*********** Cache line related macros ********/
380 
382 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
383 
385 #define RTE_CACHE_LINE_ROUNDUP(size) \
386  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
387  RTE_CACHE_LINE_SIZE))
388 
390 #if RTE_CACHE_LINE_SIZE == 64
391 #define RTE_CACHE_LINE_SIZE_LOG2 6
392 #elif RTE_CACHE_LINE_SIZE == 128
393 #define RTE_CACHE_LINE_SIZE_LOG2 7
394 #else
395 #error "Unsupported cache line size"
396 #endif
397 
399 #define RTE_CACHE_LINE_MIN_SIZE 64
400 
402 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
403 
405 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
406 
407 /*********** PA/IOVA type definitions ********/
408 
410 typedef uint64_t phys_addr_t;
411 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
412 
420 typedef uint64_t rte_iova_t;
421 #define RTE_BAD_IOVA ((rte_iova_t)-1)
422 
423 /*********** Structure alignment markers ********/
424 
426 __extension__ typedef void *RTE_MARKER[0];
428 __extension__ typedef uint8_t RTE_MARKER8[0];
430 __extension__ typedef uint16_t RTE_MARKER16[0];
432 __extension__ typedef uint32_t RTE_MARKER32[0];
434 __extension__ typedef uint64_t RTE_MARKER64[0];
435 
446 static inline uint32_t
447 rte_combine32ms1b(uint32_t x)
448 {
449  x |= x >> 1;
450  x |= x >> 2;
451  x |= x >> 4;
452  x |= x >> 8;
453  x |= x >> 16;
454 
455  return x;
456 }
457 
468 static inline uint64_t
469 rte_combine64ms1b(uint64_t v)
470 {
471  v |= v >> 1;
472  v |= v >> 2;
473  v |= v >> 4;
474  v |= v >> 8;
475  v |= v >> 16;
476  v |= v >> 32;
477 
478  return v;
479 }
480 
481 /*********** Macros to work with powers of 2 ********/
482 
486 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
487 
494 static inline int
495 rte_is_power_of_2(uint32_t n)
496 {
497  return n && !(n & (n - 1));
498 }
499 
509 static inline uint32_t
510 rte_align32pow2(uint32_t x)
511 {
512  x--;
513  x = rte_combine32ms1b(x);
514 
515  return x + 1;
516 }
517 
527 static inline uint32_t
529 {
530  x = rte_combine32ms1b(x);
531 
532  return x - (x >> 1);
533 }
534 
544 static inline uint64_t
545 rte_align64pow2(uint64_t v)
546 {
547  v--;
548  v = rte_combine64ms1b(v);
549 
550  return v + 1;
551 }
552 
562 static inline uint64_t
564 {
565  v = rte_combine64ms1b(v);
566 
567  return v - (v >> 1);
568 }
569 
570 /*********** Macros for calculating min and max **********/
571 
575 #define RTE_MIN(a, b) \
576  __extension__ ({ \
577  typeof (a) _a = (a); \
578  typeof (b) _b = (b); \
579  _a < _b ? _a : _b; \
580  })
581 
585 #define RTE_MAX(a, b) \
586  __extension__ ({ \
587  typeof (a) _a = (a); \
588  typeof (b) _b = (b); \
589  _a > _b ? _a : _b; \
590  })
591 
592 /*********** Other general functions / macros ********/
593 
605 static inline uint32_t
606 rte_bsf32(uint32_t v)
607 {
608  return (uint32_t)__builtin_ctz(v);
609 }
610 
625 static inline int
626 rte_bsf32_safe(uint32_t v, uint32_t *pos)
627 {
628  if (v == 0)
629  return 0;
630 
631  *pos = rte_bsf32(v);
632  return 1;
633 }
634 
646 static inline uint32_t
647 rte_log2_u32(uint32_t v)
648 {
649  if (v == 0)
650  return 0;
651  v = rte_align32pow2(v);
652  return rte_bsf32(v);
653 }
654 
655 
667 static inline int
668 rte_fls_u32(uint32_t x)
669 {
670  return (x == 0) ? 0 : 32 - __builtin_clz(x);
671 }
672 
684 static inline int
685 rte_bsf64(uint64_t v)
686 {
687  return (uint32_t)__builtin_ctzll(v);
688 }
689 
704 static inline int
705 rte_bsf64_safe(uint64_t v, uint32_t *pos)
706 {
707  if (v == 0)
708  return 0;
709 
710  *pos = rte_bsf64(v);
711  return 1;
712 }
713 
726 static inline int
727 rte_fls_u64(uint64_t x)
728 {
729  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
730 }
731 
743 static inline uint32_t
744 rte_log2_u64(uint64_t v)
745 {
746  if (v == 0)
747  return 0;
748  v = rte_align64pow2(v);
749  /* we checked for v being 0 already, so no undefined behavior */
750  return rte_bsf64(v);
751 }
752 
753 #ifndef offsetof
754 
755 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
756 #endif
757 
772 #ifndef container_of
773 #define container_of(ptr, type, member) __extension__ ({ \
774  const typeof(((type *)0)->member) *_ptr = (ptr); \
775  __rte_unused type *_target_ptr = \
776  (type *)(ptr); \
777  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
778  })
779 #endif
780 
782 #define RTE_SWAP(a, b) \
783  __extension__ ({ \
784  typeof (a) _a = a; \
785  a = b; \
786  b = _a; \
787  })
788 
799 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
800 
801 #define _RTE_STR(x) #x
802 
803 #define RTE_STR(x) _RTE_STR(x)
804 
810 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
811 #define RTE_FMT_HEAD(fmt, ...) fmt
812 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
813 
815 #define RTE_LEN2MASK(ln, tp) \
816  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
817 
819 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
820 
835 static inline uint64_t
836 rte_str_to_size(const char *str)
837 {
838  char *endptr;
839  unsigned long long size;
840 
841  while (isspace((int)*str))
842  str++;
843  if (*str == '-')
844  return 0;
845 
846  errno = 0;
847  size = strtoull(str, &endptr, 0);
848  if (errno)
849  return 0;
850 
851  if (*endptr == ' ')
852  endptr++; /* allow 1 space gap */
853 
854  switch (*endptr){
855  case 'G': case 'g': size *= 1024; /* fall-through */
856  case 'M': case 'm': size *= 1024; /* fall-through */
857  case 'K': case 'k': size *= 1024; /* fall-through */
858  default:
859  break;
860  }
861  return size;
862 }
863 
877 __rte_noreturn void
878 rte_exit(int exit_code, const char *format, ...)
879  __rte_format_printf(2, 3);
880 
881 #ifdef __cplusplus
882 }
883 #endif
884 
885 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:367
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:469
uint64_t rte_iova_t
Definition: rte_common.h:420
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:668
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:744
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:447
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:545
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:647
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:428
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:426
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:606
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:563
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:510
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:705
uint64_t phys_addr_t
Definition: rte_common.h:410
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:434
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:146
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:727
static int rte_bsf32_safe(uint32_t v, uint32_t *pos)
Definition: rte_common.h:626
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:316
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:495
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:430
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:685
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:432
#define __rte_noreturn
Definition: rte_common.h:223
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:528
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:836
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_aligned(a)
Definition: rte_common.h:71