DPDK  20.05.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 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 
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 
119 #if RTE_CC_IS_GNU
120 #define __rte_format_printf(format_index, first_arg) \
121  __attribute__((format(gnu_printf, format_index, first_arg)))
122 #else
123 #define __rte_format_printf(format_index, first_arg) \
124  __attribute__((format(printf, format_index, first_arg)))
125 #endif
126 
127 #define RTE_PRIORITY_LOG 101
128 #define RTE_PRIORITY_BUS 110
129 #define RTE_PRIORITY_CLASS 120
130 #define RTE_PRIORITY_LAST 65535
131 
132 #define RTE_PRIO(prio) \
133  RTE_PRIORITY_ ## prio
134 
144 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
145 #define RTE_INIT_PRIO(func, prio) \
146 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
147 #endif
148 
157 #define RTE_INIT(func) \
158  RTE_INIT_PRIO(func, LAST)
159 
169 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
170 #define RTE_FINI_PRIO(func, prio) \
171 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
172 #endif
173 
182 #define RTE_FINI(func) \
183  RTE_FINI_PRIO(func, LAST)
184 
188 #define __rte_noreturn __attribute__((noreturn))
189 
193 #define __rte_always_inline inline __attribute__((always_inline))
194 
198 #define __rte_noinline __attribute__((noinline))
199 
203 #define __rte_hot __attribute__((hot))
204 
208 #define __rte_cold __attribute__((cold))
209 
210 /*********** Macros for pointer arithmetic ********/
211 
215 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
216 
220 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
221 
227 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
228 
232 #define RTE_CAST_FIELD(var, field, type) \
233  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
234 
235 /*********** Macros/static functions for doing alignment ********/
236 
237 
244 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
245  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
246 
253 #define RTE_ALIGN_FLOOR(val, align) \
254  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
255 
262 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
263  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
264 
271 #define RTE_ALIGN_CEIL(val, align) \
272  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
273 
281 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
282 
290 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
291 
297 #define RTE_ALIGN_MUL_CEIL(v, mul) \
298  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
299 
305 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
306  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
307 
313 #define RTE_ALIGN_MUL_NEAR(v, mul) \
314  ({ \
315  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
316  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
317  (ceil - v) > (v - floor) ? floor : ceil; \
318  })
319 
331 static inline int
332 rte_is_aligned(void *ptr, unsigned align)
333 {
334  return RTE_PTR_ALIGN(ptr, align) == ptr;
335 }
336 
337 /*********** Macros for compile type checks ********/
338 
342 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
343 
344 /*********** Cache line related macros ********/
345 
347 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
348 
350 #define RTE_CACHE_LINE_ROUNDUP(size) \
351  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
352  RTE_CACHE_LINE_SIZE))
353 
355 #if RTE_CACHE_LINE_SIZE == 64
356 #define RTE_CACHE_LINE_SIZE_LOG2 6
357 #elif RTE_CACHE_LINE_SIZE == 128
358 #define RTE_CACHE_LINE_SIZE_LOG2 7
359 #else
360 #error "Unsupported cache line size"
361 #endif
362 
364 #define RTE_CACHE_LINE_MIN_SIZE 64
365 
367 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
368 
370 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
371 
372 /*********** PA/IOVA type definitions ********/
373 
375 typedef uint64_t phys_addr_t;
376 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
377 
385 typedef uint64_t rte_iova_t;
386 #define RTE_BAD_IOVA ((rte_iova_t)-1)
387 
388 /*********** Structure alignment markers ********/
389 
391 __extension__ typedef void *RTE_MARKER[0];
393 __extension__ typedef uint8_t RTE_MARKER8[0];
395 __extension__ typedef uint16_t RTE_MARKER16[0];
397 __extension__ typedef uint32_t RTE_MARKER32[0];
399 __extension__ typedef uint64_t RTE_MARKER64[0];
400 
411 static inline uint32_t
412 rte_combine32ms1b(uint32_t x)
413 {
414  x |= x >> 1;
415  x |= x >> 2;
416  x |= x >> 4;
417  x |= x >> 8;
418  x |= x >> 16;
419 
420  return x;
421 }
422 
433 static inline uint64_t
434 rte_combine64ms1b(uint64_t v)
435 {
436  v |= v >> 1;
437  v |= v >> 2;
438  v |= v >> 4;
439  v |= v >> 8;
440  v |= v >> 16;
441  v |= v >> 32;
442 
443  return v;
444 }
445 
446 /*********** Macros to work with powers of 2 ********/
447 
451 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
452 
459 static inline int
460 rte_is_power_of_2(uint32_t n)
461 {
462  return n && !(n & (n - 1));
463 }
464 
474 static inline uint32_t
475 rte_align32pow2(uint32_t x)
476 {
477  x--;
478  x = rte_combine32ms1b(x);
479 
480  return x + 1;
481 }
482 
492 static inline uint32_t
494 {
495  x = rte_combine32ms1b(x);
496 
497  return x - (x >> 1);
498 }
499 
509 static inline uint64_t
510 rte_align64pow2(uint64_t v)
511 {
512  v--;
513  v = rte_combine64ms1b(v);
514 
515  return v + 1;
516 }
517 
527 static inline uint64_t
529 {
530  v = rte_combine64ms1b(v);
531 
532  return v - (v >> 1);
533 }
534 
535 /*********** Macros for calculating min and max **********/
536 
540 #define RTE_MIN(a, b) \
541  __extension__ ({ \
542  typeof (a) _a = (a); \
543  typeof (b) _b = (b); \
544  _a < _b ? _a : _b; \
545  })
546 
550 #define RTE_MAX(a, b) \
551  __extension__ ({ \
552  typeof (a) _a = (a); \
553  typeof (b) _b = (b); \
554  _a > _b ? _a : _b; \
555  })
556 
557 /*********** Other general functions / macros ********/
558 
570 static inline uint32_t
571 rte_bsf32(uint32_t v)
572 {
573  return (uint32_t)__builtin_ctz(v);
574 }
575 
590 static inline int
591 rte_bsf32_safe(uint64_t v, uint32_t *pos)
592 {
593  if (v == 0)
594  return 0;
595 
596  *pos = rte_bsf32(v);
597  return 1;
598 }
599 
611 static inline uint32_t
612 rte_log2_u32(uint32_t v)
613 {
614  if (v == 0)
615  return 0;
616  v = rte_align32pow2(v);
617  return rte_bsf32(v);
618 }
619 
620 
632 static inline int
633 rte_fls_u32(uint32_t x)
634 {
635  return (x == 0) ? 0 : 32 - __builtin_clz(x);
636 }
637 
649 static inline int
650 rte_bsf64(uint64_t v)
651 {
652  return (uint32_t)__builtin_ctzll(v);
653 }
654 
669 static inline int
670 rte_bsf64_safe(uint64_t v, uint32_t *pos)
671 {
672  if (v == 0)
673  return 0;
674 
675  *pos = rte_bsf64(v);
676  return 1;
677 }
678 
691 static inline int
692 rte_fls_u64(uint64_t x)
693 {
694  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
695 }
696 
708 static inline uint32_t
709 rte_log2_u64(uint64_t v)
710 {
711  if (v == 0)
712  return 0;
713  v = rte_align64pow2(v);
714  /* we checked for v being 0 already, so no undefined behavior */
715  return rte_bsf64(v);
716 }
717 
718 #ifndef offsetof
719 
720 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
721 #endif
722 
737 #ifndef container_of
738 #define container_of(ptr, type, member) __extension__ ({ \
739  const typeof(((type *)0)->member) *_ptr = (ptr); \
740  __rte_unused type *_target_ptr = \
741  (type *)(ptr); \
742  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
743  })
744 #endif
745 
756 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
757 
758 #define _RTE_STR(x) #x
759 
760 #define RTE_STR(x) _RTE_STR(x)
761 
767 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
768 #define RTE_FMT_HEAD(fmt, ...) fmt
769 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
770 
772 #define RTE_LEN2MASK(ln, tp) \
773  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
774 
776 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
777 
792 static inline uint64_t
793 rte_str_to_size(const char *str)
794 {
795  char *endptr;
796  unsigned long long size;
797 
798  while (isspace((int)*str))
799  str++;
800  if (*str == '-')
801  return 0;
802 
803  errno = 0;
804  size = strtoull(str, &endptr, 0);
805  if (errno)
806  return 0;
807 
808  if (*endptr == ' ')
809  endptr++; /* allow 1 space gap */
810 
811  switch (*endptr){
812  case 'G': case 'g': size *= 1024; /* fall-through */
813  case 'M': case 'm': size *= 1024; /* fall-through */
814  case 'K': case 'k': size *= 1024; /* fall-through */
815  default:
816  break;
817  }
818  return size;
819 }
820 
834 __rte_noreturn void
835 rte_exit(int exit_code, const char *format, ...)
836  __rte_format_printf(2, 3);
837 
838 #ifdef __cplusplus
839 }
840 #endif
841 
842 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:332
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:434
static int rte_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:591
uint64_t rte_iova_t
Definition: rte_common.h:385
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:633
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:709
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:412
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:510
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:612
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:393
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:391
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:571
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:528
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:475
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:670
uint64_t phys_addr_t
Definition: rte_common.h:375
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:399
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:123
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:692
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:281
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:460
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:395
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:650
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:397
#define __rte_noreturn
Definition: rte_common.h:188
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:493
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:793
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_aligned(a)
Definition: rte_common.h:69