DPDK  20.11.10
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 asm
35 #define asm __asm__
36 #endif
37 
39 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
40 #define RTE_STD_C11 __extension__
41 #else
42 #define RTE_STD_C11
43 #endif
44 
45 /*
46  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
47  * while a host application (like pmdinfogen) may have another compiler.
48  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
49  * no matter it is a target or host application.
50  */
51 #define RTE_CC_IS_GNU 0
52 #if defined __clang__
53 #define RTE_CC_CLANG
54 #elif defined __INTEL_COMPILER
55 #define RTE_CC_ICC
56 #elif defined __GNUC__
57 #define RTE_CC_GCC
58 #undef RTE_CC_IS_GNU
59 #define RTE_CC_IS_GNU 1
60 #endif
61 #if RTE_CC_IS_GNU
62 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
63  __GNUC_PATCHLEVEL__)
64 #endif
65 
69 #define __rte_aligned(a) __attribute__((__aligned__(a)))
70 
71 #ifdef RTE_ARCH_STRICT_ALIGN
72 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
73 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
74 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
75 #else
76 typedef uint64_t unaligned_uint64_t;
77 typedef uint32_t unaligned_uint32_t;
78 typedef uint16_t unaligned_uint16_t;
79 #endif
80 
84 #define __rte_packed __attribute__((__packed__))
85 
89 #define __rte_may_alias __attribute__((__may_alias__))
90 
91 /******* Macro to mark functions and fields scheduled for removal *****/
92 #define __rte_deprecated __attribute__((__deprecated__))
93 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
94 
98 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
99 #define RTE_PRAGMA(x) _Pragma(#x)
100 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
101 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
102 #else
103 #define RTE_DEPRECATED(x)
104 #endif
105 
109 #define __rte_weak __attribute__((__weak__))
110 
114 #define __rte_used __attribute__((used))
115 
116 /*********** Macros to eliminate unused variable warnings ********/
117 
121 #define __rte_unused __attribute__((__unused__))
122 
126 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
127 #define __rte_restrict __restrict
128 #else
129 #define __rte_restrict restrict
130 #endif
131 
136 #define RTE_SET_USED(x) (void)(x)
137 
145 #if RTE_CC_IS_GNU
146 #define __rte_format_printf(format_index, first_arg) \
147  __attribute__((format(gnu_printf, format_index, first_arg)))
148 #else
149 #define __rte_format_printf(format_index, first_arg) \
150  __attribute__((format(printf, format_index, first_arg)))
151 #endif
152 
158 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
159 #define __rte_alloc_size(...) \
160  __attribute__((alloc_size(__VA_ARGS__)))
161 #else
162 #define __rte_alloc_size(...)
163 #endif
164 
165 #define RTE_PRIORITY_LOG 101
166 #define RTE_PRIORITY_BUS 110
167 #define RTE_PRIORITY_CLASS 120
168 #define RTE_PRIORITY_LAST 65535
169 
170 #define RTE_PRIO(prio) \
171  RTE_PRIORITY_ ## prio
172 
182 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
183 #define RTE_INIT_PRIO(func, prio) \
184 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
185 #endif
186 
195 #define RTE_INIT(func) \
196  RTE_INIT_PRIO(func, LAST)
197 
207 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
208 #define RTE_FINI_PRIO(func, prio) \
209 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
210 #endif
211 
220 #define RTE_FINI(func) \
221  RTE_FINI_PRIO(func, LAST)
222 
226 #define __rte_noreturn __attribute__((noreturn))
227 
231 #define __rte_always_inline inline __attribute__((always_inline))
232 
236 #define __rte_noinline __attribute__((noinline))
237 
241 #define __rte_hot __attribute__((hot))
242 
246 #define __rte_cold __attribute__((cold))
247 
248 /*********** Macros for pointer arithmetic ********/
249 
253 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
254 
258 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
259 
265 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
266 
270 #define RTE_CAST_FIELD(var, field, type) \
271  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
272 
273 /*********** Macros/static functions for doing alignment ********/
274 
275 
282 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
283  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
284 
291 #define RTE_ALIGN_FLOOR(val, align) \
292  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
293 
300 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
301  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
302 
309 #define RTE_ALIGN_CEIL(val, align) \
310  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
311 
319 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
320 
328 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
329 
335 #define RTE_ALIGN_MUL_CEIL(v, mul) \
336  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
337 
343 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
344  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
345 
351 #define RTE_ALIGN_MUL_NEAR(v, mul) \
352  ({ \
353  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
354  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
355  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
356  })
357 
369 static inline int
370 rte_is_aligned(void *ptr, unsigned align)
371 {
372  return RTE_PTR_ALIGN(ptr, align) == ptr;
373 }
374 
375 /*********** Macros for compile type checks ********/
376 
380 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
381 
382 /*********** Cache line related macros ********/
383 
385 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
386 
388 #define RTE_CACHE_LINE_ROUNDUP(size) \
389  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
390  RTE_CACHE_LINE_SIZE))
391 
393 #if RTE_CACHE_LINE_SIZE == 64
394 #define RTE_CACHE_LINE_SIZE_LOG2 6
395 #elif RTE_CACHE_LINE_SIZE == 128
396 #define RTE_CACHE_LINE_SIZE_LOG2 7
397 #else
398 #error "Unsupported cache line size"
399 #endif
400 
402 #define RTE_CACHE_LINE_MIN_SIZE 64
403 
405 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
406 
408 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
409 
410 /*********** PA/IOVA type definitions ********/
411 
413 typedef uint64_t phys_addr_t;
414 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
415 
423 typedef uint64_t rte_iova_t;
424 #define RTE_BAD_IOVA ((rte_iova_t)-1)
425 
426 /*********** Structure alignment markers ********/
427 
429 __extension__ typedef void *RTE_MARKER[0];
431 __extension__ typedef uint8_t RTE_MARKER8[0];
433 __extension__ typedef uint16_t RTE_MARKER16[0];
435 __extension__ typedef uint32_t RTE_MARKER32[0];
437 __extension__ typedef uint64_t RTE_MARKER64[0];
438 
449 static inline uint32_t
450 rte_combine32ms1b(uint32_t x)
451 {
452  x |= x >> 1;
453  x |= x >> 2;
454  x |= x >> 4;
455  x |= x >> 8;
456  x |= x >> 16;
457 
458  return x;
459 }
460 
471 static inline uint64_t
472 rte_combine64ms1b(uint64_t v)
473 {
474  v |= v >> 1;
475  v |= v >> 2;
476  v |= v >> 4;
477  v |= v >> 8;
478  v |= v >> 16;
479  v |= v >> 32;
480 
481  return v;
482 }
483 
484 /*********** Macros to work with powers of 2 ********/
485 
489 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
490 
497 static inline int
498 rte_is_power_of_2(uint32_t n)
499 {
500  return n && !(n & (n - 1));
501 }
502 
512 static inline uint32_t
513 rte_align32pow2(uint32_t x)
514 {
515  x--;
516  x = rte_combine32ms1b(x);
517 
518  return x + 1;
519 }
520 
530 static inline uint32_t
532 {
533  x = rte_combine32ms1b(x);
534 
535  return x - (x >> 1);
536 }
537 
547 static inline uint64_t
548 rte_align64pow2(uint64_t v)
549 {
550  v--;
551  v = rte_combine64ms1b(v);
552 
553  return v + 1;
554 }
555 
565 static inline uint64_t
567 {
568  v = rte_combine64ms1b(v);
569 
570  return v - (v >> 1);
571 }
572 
573 /*********** Macros for calculating min and max **********/
574 
578 #define RTE_MIN(a, b) \
579  __extension__ ({ \
580  typeof (a) _a = (a); \
581  typeof (b) _b = (b); \
582  _a < _b ? _a : _b; \
583  })
584 
588 #define RTE_MAX(a, b) \
589  __extension__ ({ \
590  typeof (a) _a = (a); \
591  typeof (b) _b = (b); \
592  _a > _b ? _a : _b; \
593  })
594 
595 /*********** Other general functions / macros ********/
596 
608 static inline uint32_t
609 rte_bsf32(uint32_t v)
610 {
611  return (uint32_t)__builtin_ctz(v);
612 }
613 
628 static inline int
629 rte_bsf32_safe(uint64_t v, uint32_t *pos)
630 {
631  if (v == 0)
632  return 0;
633 
634  *pos = rte_bsf32(v);
635  return 1;
636 }
637 
649 static inline uint32_t
650 rte_log2_u32(uint32_t v)
651 {
652  if (v == 0)
653  return 0;
654  v = rte_align32pow2(v);
655  return rte_bsf32(v);
656 }
657 
658 
670 static inline int
671 rte_fls_u32(uint32_t x)
672 {
673  return (x == 0) ? 0 : 32 - __builtin_clz(x);
674 }
675 
687 static inline int
688 rte_bsf64(uint64_t v)
689 {
690  return (uint32_t)__builtin_ctzll(v);
691 }
692 
707 static inline int
708 rte_bsf64_safe(uint64_t v, uint32_t *pos)
709 {
710  if (v == 0)
711  return 0;
712 
713  *pos = rte_bsf64(v);
714  return 1;
715 }
716 
729 static inline int
730 rte_fls_u64(uint64_t x)
731 {
732  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
733 }
734 
746 static inline uint32_t
747 rte_log2_u64(uint64_t v)
748 {
749  if (v == 0)
750  return 0;
751  v = rte_align64pow2(v);
752  /* we checked for v being 0 already, so no undefined behavior */
753  return rte_bsf64(v);
754 }
755 
756 #ifndef offsetof
757 
758 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
759 #endif
760 
775 #ifndef container_of
776 #define container_of(ptr, type, member) __extension__ ({ \
777  const typeof(((type *)0)->member) *_ptr = (ptr); \
778  __rte_unused type *_target_ptr = \
779  (type *)(ptr); \
780  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
781  })
782 #endif
783 
794 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
795 
796 #define _RTE_STR(x) #x
797 
798 #define RTE_STR(x) _RTE_STR(x)
799 
805 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
806 #define RTE_FMT_HEAD(fmt, ...) fmt
807 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
808 
810 #define RTE_LEN2MASK(ln, tp) \
811  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
812 
814 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
815 
830 static inline uint64_t
831 rte_str_to_size(const char *str)
832 {
833  char *endptr;
834  unsigned long long size;
835 
836  while (isspace((int)*str))
837  str++;
838  if (*str == '-')
839  return 0;
840 
841  errno = 0;
842  size = strtoull(str, &endptr, 0);
843  if (errno)
844  return 0;
845 
846  if (*endptr == ' ')
847  endptr++; /* allow 1 space gap */
848 
849  switch (*endptr){
850  case 'G': case 'g': size *= 1024; /* fall-through */
851  case 'M': case 'm': size *= 1024; /* fall-through */
852  case 'K': case 'k': size *= 1024; /* fall-through */
853  default:
854  break;
855  }
856  return size;
857 }
858 
872 __rte_noreturn void
873 rte_exit(int exit_code, const char *format, ...)
874  __rte_format_printf(2, 3);
875 
876 #ifdef __cplusplus
877 }
878 #endif
879 
880 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:370
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:472
static int rte_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:629
uint64_t rte_iova_t
Definition: rte_common.h:423
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:671
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:747
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:450
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:548
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:650
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:431
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:429
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:609
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:566
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:513
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:708
uint64_t phys_addr_t
Definition: rte_common.h:413
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:437
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:149
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:730
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:319
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:498
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:433
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:688
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:435
#define __rte_noreturn
Definition: rte_common.h:226
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:531
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:831
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_aligned(a)
Definition: rte_common.h:69