DPDK  20.05.0-rc0
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 
66 #ifdef RTE_ARCH_STRICT_ALIGN
67 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
68 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
69 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
70 #else
71 typedef uint64_t unaligned_uint64_t;
72 typedef uint32_t unaligned_uint32_t;
73 typedef uint16_t unaligned_uint16_t;
74 #endif
75 
79 #define __rte_aligned(a) __attribute__((__aligned__(a)))
80 
84 #define __rte_packed __attribute__((__packed__))
85 
86 /******* Macro to mark functions and fields scheduled for removal *****/
87 #define __rte_deprecated __attribute__((__deprecated__))
88 
92 #define __rte_weak __attribute__((__weak__))
93 
94 /*********** Macros to eliminate unused variable warnings ********/
95 
99 #define __rte_unused __attribute__((__unused__))
100 
105 #define RTE_SET_USED(x) (void)(x)
106 
114 #if RTE_CC_IS_GNU
115 #define __rte_format_printf(format_index, first_arg) \
116  __attribute__((format(gnu_printf, format_index, first_arg)))
117 #else
118 #define __rte_format_printf(format_index, first_arg) \
119  __attribute__((format(printf, format_index, first_arg)))
120 #endif
121 
122 #define RTE_PRIORITY_LOG 101
123 #define RTE_PRIORITY_BUS 110
124 #define RTE_PRIORITY_CLASS 120
125 #define RTE_PRIORITY_LAST 65535
126 
127 #define RTE_PRIO(prio) \
128  RTE_PRIORITY_ ## prio
129 
139 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
140 #define RTE_INIT_PRIO(func, prio) \
141 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
142 #endif
143 
152 #define RTE_INIT(func) \
153  RTE_INIT_PRIO(func, LAST)
154 
164 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
165 #define RTE_FINI_PRIO(func, prio) \
166 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
167 #endif
168 
177 #define RTE_FINI(func) \
178  RTE_FINI_PRIO(func, LAST)
179 
183 #define __rte_always_inline inline __attribute__((always_inline))
184 
188 #define __rte_noinline __attribute__((noinline))
189 
190 /*********** Macros for pointer arithmetic ********/
191 
195 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
196 
200 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
201 
207 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
208 
212 #define RTE_CAST_FIELD(var, field, type) \
213  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
214 
215 /*********** Macros/static functions for doing alignment ********/
216 
217 
224 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
225  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
226 
233 #define RTE_ALIGN_FLOOR(val, align) \
234  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
235 
242 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
243  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
244 
251 #define RTE_ALIGN_CEIL(val, align) \
252  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
253 
261 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
262 
270 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
271 
277 #define RTE_ALIGN_MUL_CEIL(v, mul) \
278  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
279 
285 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
286  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
287 
293 #define RTE_ALIGN_MUL_NEAR(v, mul) \
294  ({ \
295  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
296  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
297  (ceil - v) > (v - floor) ? floor : ceil; \
298  })
299 
311 static inline int
312 rte_is_aligned(void *ptr, unsigned align)
313 {
314  return RTE_PTR_ALIGN(ptr, align) == ptr;
315 }
316 
317 /*********** Macros for compile type checks ********/
318 
322 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
323 
324 /*********** Cache line related macros ********/
325 
327 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
328 
330 #define RTE_CACHE_LINE_ROUNDUP(size) \
331  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
332  RTE_CACHE_LINE_SIZE))
333 
335 #if RTE_CACHE_LINE_SIZE == 64
336 #define RTE_CACHE_LINE_SIZE_LOG2 6
337 #elif RTE_CACHE_LINE_SIZE == 128
338 #define RTE_CACHE_LINE_SIZE_LOG2 7
339 #else
340 #error "Unsupported cache line size"
341 #endif
342 
344 #define RTE_CACHE_LINE_MIN_SIZE 64
345 
347 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
348 
350 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
351 
352 /*********** PA/IOVA type definitions ********/
353 
355 typedef uint64_t phys_addr_t;
356 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
357 
365 typedef uint64_t rte_iova_t;
366 #define RTE_BAD_IOVA ((rte_iova_t)-1)
367 
368 /*********** Structure alignment markers ********/
369 
371 __extension__ typedef void *RTE_MARKER[0];
373 __extension__ typedef uint8_t RTE_MARKER8[0];
375 __extension__ typedef uint16_t RTE_MARKER16[0];
377 __extension__ typedef uint32_t RTE_MARKER32[0];
379 __extension__ typedef uint64_t RTE_MARKER64[0];
380 
391 static inline uint32_t
392 rte_combine32ms1b(register uint32_t x)
393 {
394  x |= x >> 1;
395  x |= x >> 2;
396  x |= x >> 4;
397  x |= x >> 8;
398  x |= x >> 16;
399 
400  return x;
401 }
402 
413 static inline uint64_t
414 rte_combine64ms1b(register uint64_t v)
415 {
416  v |= v >> 1;
417  v |= v >> 2;
418  v |= v >> 4;
419  v |= v >> 8;
420  v |= v >> 16;
421  v |= v >> 32;
422 
423  return v;
424 }
425 
426 /*********** Macros to work with powers of 2 ********/
427 
431 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
432 
439 static inline int
440 rte_is_power_of_2(uint32_t n)
441 {
442  return n && !(n & (n - 1));
443 }
444 
454 static inline uint32_t
455 rte_align32pow2(uint32_t x)
456 {
457  x--;
458  x = rte_combine32ms1b(x);
459 
460  return x + 1;
461 }
462 
472 static inline uint32_t
474 {
475  x = rte_combine32ms1b(x);
476 
477  return x - (x >> 1);
478 }
479 
489 static inline uint64_t
490 rte_align64pow2(uint64_t v)
491 {
492  v--;
493  v = rte_combine64ms1b(v);
494 
495  return v + 1;
496 }
497 
507 static inline uint64_t
509 {
510  v = rte_combine64ms1b(v);
511 
512  return v - (v >> 1);
513 }
514 
515 /*********** Macros for calculating min and max **********/
516 
520 #define RTE_MIN(a, b) \
521  __extension__ ({ \
522  typeof (a) _a = (a); \
523  typeof (b) _b = (b); \
524  _a < _b ? _a : _b; \
525  })
526 
530 #define RTE_MAX(a, b) \
531  __extension__ ({ \
532  typeof (a) _a = (a); \
533  typeof (b) _b = (b); \
534  _a > _b ? _a : _b; \
535  })
536 
537 /*********** Other general functions / macros ********/
538 
550 static inline uint32_t
551 rte_bsf32(uint32_t v)
552 {
553  return (uint32_t)__builtin_ctz(v);
554 }
555 
570 static inline int
571 rte_bsf32_safe(uint64_t v, uint32_t *pos)
572 {
573  if (v == 0)
574  return 0;
575 
576  *pos = rte_bsf32(v);
577  return 1;
578 }
579 
591 static inline uint32_t
592 rte_log2_u32(uint32_t v)
593 {
594  if (v == 0)
595  return 0;
596  v = rte_align32pow2(v);
597  return rte_bsf32(v);
598 }
599 
600 
612 static inline int
613 rte_fls_u32(uint32_t x)
614 {
615  return (x == 0) ? 0 : 32 - __builtin_clz(x);
616 }
617 
629 static inline int
630 rte_bsf64(uint64_t v)
631 {
632  return (uint32_t)__builtin_ctzll(v);
633 }
634 
649 static inline int
650 rte_bsf64_safe(uint64_t v, uint32_t *pos)
651 {
652  if (v == 0)
653  return 0;
654 
655  *pos = rte_bsf64(v);
656  return 1;
657 }
658 
671 static inline int
672 rte_fls_u64(uint64_t x)
673 {
674  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
675 }
676 
688 static inline uint32_t
689 rte_log2_u64(uint64_t v)
690 {
691  if (v == 0)
692  return 0;
693  v = rte_align64pow2(v);
694  /* we checked for v being 0 already, so no undefined behavior */
695  return rte_bsf64(v);
696 }
697 
698 #ifndef offsetof
699 
700 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
701 #endif
702 
717 #ifndef container_of
718 #define container_of(ptr, type, member) __extension__ ({ \
719  const typeof(((type *)0)->member) *_ptr = (ptr); \
720  __attribute__((unused)) type *_target_ptr = \
721  (type *)(ptr); \
722  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
723  })
724 #endif
725 
736 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
737 
738 #define _RTE_STR(x) #x
739 
740 #define RTE_STR(x) _RTE_STR(x)
741 
747 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
748 #define RTE_FMT_HEAD(fmt, ...) fmt
749 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
750 
752 #define RTE_LEN2MASK(ln, tp) \
753  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
754 
756 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
757 
772 static inline uint64_t
773 rte_str_to_size(const char *str)
774 {
775  char *endptr;
776  unsigned long long size;
777 
778  while (isspace((int)*str))
779  str++;
780  if (*str == '-')
781  return 0;
782 
783  errno = 0;
784  size = strtoull(str, &endptr, 0);
785  if (errno)
786  return 0;
787 
788  if (*endptr == ' ')
789  endptr++; /* allow 1 space gap */
790 
791  switch (*endptr){
792  case 'G': case 'g': size *= 1024; /* fall-through */
793  case 'M': case 'm': size *= 1024; /* fall-through */
794  case 'K': case 'k': size *= 1024; /* fall-through */
795  default:
796  break;
797  }
798  return size;
799 }
800 
814 void
815 rte_exit(int exit_code, const char *format, ...)
816  __attribute__((noreturn))
817  __rte_format_printf(2, 3);
818 
819 #ifdef __cplusplus
820 }
821 #endif
822 
823 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:312
static int rte_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:571
uint64_t rte_iova_t
Definition: rte_common.h:365
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:613
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:689
static uint64_t rte_combine64ms1b(register uint64_t v)
Definition: rte_common.h:414
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:490
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:592
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:373
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:371
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:551
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:508
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:455
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:650
uint64_t phys_addr_t
Definition: rte_common.h:355
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:379
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:118
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:672
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:261
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:440
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:375
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:630
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:377
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:473
void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:773
static uint32_t rte_combine32ms1b(register uint32_t x)
Definition: rte_common.h:392