DPDK  19.11.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 __rte_aligned(1);
68 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
69 typedef uint16_t unaligned_uint16_t __rte_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 
97 #define __rte_used __attribute__((used))
98 
99 /*********** Macros to eliminate unused variable warnings ********/
100 
104 #define __rte_unused __attribute__((__unused__))
105 
110 #define RTE_SET_USED(x) (void)(x)
111 
112 #define RTE_PRIORITY_LOG 101
113 #define RTE_PRIORITY_BUS 110
114 #define RTE_PRIORITY_CLASS 120
115 #define RTE_PRIORITY_LAST 65535
116 
117 #define RTE_PRIO(prio) \
118  RTE_PRIORITY_ ## prio
119 
129 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
130 #define RTE_INIT_PRIO(func, prio) \
131 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
132 #endif
133 
142 #define RTE_INIT(func) \
143  RTE_INIT_PRIO(func, LAST)
144 
154 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
155 #define RTE_FINI_PRIO(func, prio) \
156 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
157 #endif
158 
167 #define RTE_FINI(func) \
168  RTE_FINI_PRIO(func, LAST)
169 
173 #define __rte_always_inline inline __attribute__((always_inline))
174 
178 #define __rte_noinline __attribute__((noinline))
179 
183 #define __rte_hot __attribute__((hot))
184 
188 #define __rte_cold __attribute__((cold))
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 
379 static inline uint32_t
380 rte_combine32ms1b(register uint32_t x)
381 {
382  x |= x >> 1;
383  x |= x >> 2;
384  x |= x >> 4;
385  x |= x >> 8;
386  x |= x >> 16;
387 
388  return x;
389 }
390 
401 static inline uint64_t
402 rte_combine64ms1b(register uint64_t v)
403 {
404  v |= v >> 1;
405  v |= v >> 2;
406  v |= v >> 4;
407  v |= v >> 8;
408  v |= v >> 16;
409  v |= v >> 32;
410 
411  return v;
412 }
413 
414 /*********** Macros to work with powers of 2 ********/
415 
419 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
420 
427 static inline int
428 rte_is_power_of_2(uint32_t n)
429 {
430  return n && !(n & (n - 1));
431 }
432 
442 static inline uint32_t
443 rte_align32pow2(uint32_t x)
444 {
445  x--;
446  x = rte_combine32ms1b(x);
447 
448  return x + 1;
449 }
450 
460 static inline uint32_t
462 {
463  x = rte_combine32ms1b(x);
464 
465  return x - (x >> 1);
466 }
467 
477 static inline uint64_t
478 rte_align64pow2(uint64_t v)
479 {
480  v--;
481  v = rte_combine64ms1b(v);
482 
483  return v + 1;
484 }
485 
495 static inline uint64_t
497 {
498  v = rte_combine64ms1b(v);
499 
500  return v - (v >> 1);
501 }
502 
503 /*********** Macros for calculating min and max **********/
504 
508 #define RTE_MIN(a, b) \
509  __extension__ ({ \
510  typeof (a) _a = (a); \
511  typeof (b) _b = (b); \
512  _a < _b ? _a : _b; \
513  })
514 
518 #define RTE_MAX(a, b) \
519  __extension__ ({ \
520  typeof (a) _a = (a); \
521  typeof (b) _b = (b); \
522  _a > _b ? _a : _b; \
523  })
524 
525 /*********** Other general functions / macros ********/
526 
538 static inline uint32_t
539 rte_bsf32(uint32_t v)
540 {
541  return (uint32_t)__builtin_ctz(v);
542 }
543 
558 static inline int
559 rte_bsf32_safe(uint64_t v, uint32_t *pos)
560 {
561  if (v == 0)
562  return 0;
563 
564  *pos = rte_bsf32(v);
565  return 1;
566 }
567 
579 static inline uint32_t
580 rte_log2_u32(uint32_t v)
581 {
582  if (v == 0)
583  return 0;
584  v = rte_align32pow2(v);
585  return rte_bsf32(v);
586 }
587 
588 
600 static inline int
601 rte_fls_u32(uint32_t x)
602 {
603  return (x == 0) ? 0 : 32 - __builtin_clz(x);
604 }
605 
617 static inline int
618 rte_bsf64(uint64_t v)
619 {
620  return (uint32_t)__builtin_ctzll(v);
621 }
622 
637 static inline int
638 rte_bsf64_safe(uint64_t v, uint32_t *pos)
639 {
640  if (v == 0)
641  return 0;
642 
643  *pos = rte_bsf64(v);
644  return 1;
645 }
646 
659 static inline int
660 rte_fls_u64(uint64_t x)
661 {
662  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
663 }
664 
676 static inline uint32_t
677 rte_log2_u64(uint64_t v)
678 {
679  if (v == 0)
680  return 0;
681  v = rte_align64pow2(v);
682  /* we checked for v being 0 already, so no undefined behavior */
683  return rte_bsf64(v);
684 }
685 
686 #ifndef offsetof
687 
688 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
689 #endif
690 
705 #ifndef container_of
706 #define container_of(ptr, type, member) __extension__ ({ \
707  const typeof(((type *)0)->member) *_ptr = (ptr); \
708  __attribute__((unused)) type *_target_ptr = \
709  (type *)(ptr); \
710  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
711  })
712 #endif
713 
724 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
725 
726 #define _RTE_STR(x) #x
727 
728 #define RTE_STR(x) _RTE_STR(x)
729 
735 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
736 #define RTE_FMT_HEAD(fmt, ...) fmt
737 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
738 
740 #define RTE_LEN2MASK(ln, tp) \
741  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
742 
744 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
745 
760 static inline uint64_t
761 rte_str_to_size(const char *str)
762 {
763  char *endptr;
764  unsigned long long size;
765 
766  while (isspace((int)*str))
767  str++;
768  if (*str == '-')
769  return 0;
770 
771  errno = 0;
772  size = strtoull(str, &endptr, 0);
773  if (errno)
774  return 0;
775 
776  if (*endptr == ' ')
777  endptr++; /* allow 1 space gap */
778 
779  switch (*endptr){
780  case 'G': case 'g': size *= 1024; /* fall-through */
781  case 'M': case 'm': size *= 1024; /* fall-through */
782  case 'K': case 'k': size *= 1024; /* fall-through */
783  default:
784  break;
785  }
786  return size;
787 }
788 
802 void
803 rte_exit(int exit_code, const char *format, ...)
804  __attribute__((noreturn))
805  __attribute__((format(printf, 2, 3)));
806 
807 #ifdef __cplusplus
808 }
809 #endif
810 
811 #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:559
uint64_t rte_iova_t
Definition: rte_common.h:365
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:601
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:677
static uint64_t rte_combine64ms1b(register uint64_t v)
Definition: rte_common.h:402
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:478
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:580
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:539
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:496
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:443
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:638
uint64_t phys_addr_t
Definition: rte_common.h:355
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:660
#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:428
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:618
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:461
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:761
static uint32_t rte_combine32ms1b(register uint32_t x)
Definition: rte_common.h:380
#define __rte_aligned(a)
Definition: rte_common.h:79