DPDK  20.08.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 
109 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
110 #define __rte_restrict __restrict
111 #else
112 #define __rte_restrict restrict
113 #endif
114 
119 #define RTE_SET_USED(x) (void)(x)
120 
128 #if RTE_CC_IS_GNU
129 #define __rte_format_printf(format_index, first_arg) \
130  __attribute__((format(gnu_printf, format_index, first_arg)))
131 #else
132 #define __rte_format_printf(format_index, first_arg) \
133  __attribute__((format(printf, format_index, first_arg)))
134 #endif
135 
136 #define RTE_PRIORITY_LOG 101
137 #define RTE_PRIORITY_BUS 110
138 #define RTE_PRIORITY_CLASS 120
139 #define RTE_PRIORITY_LAST 65535
140 
141 #define RTE_PRIO(prio) \
142  RTE_PRIORITY_ ## prio
143 
153 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
154 #define RTE_INIT_PRIO(func, prio) \
155 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
156 #endif
157 
166 #define RTE_INIT(func) \
167  RTE_INIT_PRIO(func, LAST)
168 
178 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
179 #define RTE_FINI_PRIO(func, prio) \
180 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
181 #endif
182 
191 #define RTE_FINI(func) \
192  RTE_FINI_PRIO(func, LAST)
193 
197 #define __rte_noreturn __attribute__((noreturn))
198 
202 #define __rte_always_inline inline __attribute__((always_inline))
203 
207 #define __rte_noinline __attribute__((noinline))
208 
212 #define __rte_hot __attribute__((hot))
213 
217 #define __rte_cold __attribute__((cold))
218 
219 /*********** Macros for pointer arithmetic ********/
220 
224 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
225 
229 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
230 
236 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
237 
241 #define RTE_CAST_FIELD(var, field, type) \
242  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
243 
244 /*********** Macros/static functions for doing alignment ********/
245 
246 
253 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
254  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
255 
262 #define RTE_ALIGN_FLOOR(val, align) \
263  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
264 
271 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
272  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
273 
280 #define RTE_ALIGN_CEIL(val, align) \
281  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
282 
290 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
291 
299 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
300 
306 #define RTE_ALIGN_MUL_CEIL(v, mul) \
307  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
308 
314 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
315  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
316 
322 #define RTE_ALIGN_MUL_NEAR(v, mul) \
323  ({ \
324  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
325  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
326  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
327  })
328 
340 static inline int
341 rte_is_aligned(void *ptr, unsigned align)
342 {
343  return RTE_PTR_ALIGN(ptr, align) == ptr;
344 }
345 
346 /*********** Macros for compile type checks ********/
347 
351 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
352 
353 /*********** Cache line related macros ********/
354 
356 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
357 
359 #define RTE_CACHE_LINE_ROUNDUP(size) \
360  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
361  RTE_CACHE_LINE_SIZE))
362 
364 #if RTE_CACHE_LINE_SIZE == 64
365 #define RTE_CACHE_LINE_SIZE_LOG2 6
366 #elif RTE_CACHE_LINE_SIZE == 128
367 #define RTE_CACHE_LINE_SIZE_LOG2 7
368 #else
369 #error "Unsupported cache line size"
370 #endif
371 
373 #define RTE_CACHE_LINE_MIN_SIZE 64
374 
376 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
377 
379 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
380 
381 /*********** PA/IOVA type definitions ********/
382 
384 typedef uint64_t phys_addr_t;
385 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
386 
394 typedef uint64_t rte_iova_t;
395 #define RTE_BAD_IOVA ((rte_iova_t)-1)
396 
397 /*********** Structure alignment markers ********/
398 
400 __extension__ typedef void *RTE_MARKER[0];
402 __extension__ typedef uint8_t RTE_MARKER8[0];
404 __extension__ typedef uint16_t RTE_MARKER16[0];
406 __extension__ typedef uint32_t RTE_MARKER32[0];
408 __extension__ typedef uint64_t RTE_MARKER64[0];
409 
420 static inline uint32_t
421 rte_combine32ms1b(uint32_t x)
422 {
423  x |= x >> 1;
424  x |= x >> 2;
425  x |= x >> 4;
426  x |= x >> 8;
427  x |= x >> 16;
428 
429  return x;
430 }
431 
442 static inline uint64_t
443 rte_combine64ms1b(uint64_t v)
444 {
445  v |= v >> 1;
446  v |= v >> 2;
447  v |= v >> 4;
448  v |= v >> 8;
449  v |= v >> 16;
450  v |= v >> 32;
451 
452  return v;
453 }
454 
455 /*********** Macros to work with powers of 2 ********/
456 
460 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
461 
468 static inline int
469 rte_is_power_of_2(uint32_t n)
470 {
471  return n && !(n & (n - 1));
472 }
473 
483 static inline uint32_t
484 rte_align32pow2(uint32_t x)
485 {
486  x--;
487  x = rte_combine32ms1b(x);
488 
489  return x + 1;
490 }
491 
501 static inline uint32_t
503 {
504  x = rte_combine32ms1b(x);
505 
506  return x - (x >> 1);
507 }
508 
518 static inline uint64_t
519 rte_align64pow2(uint64_t v)
520 {
521  v--;
522  v = rte_combine64ms1b(v);
523 
524  return v + 1;
525 }
526 
536 static inline uint64_t
538 {
539  v = rte_combine64ms1b(v);
540 
541  return v - (v >> 1);
542 }
543 
544 /*********** Macros for calculating min and max **********/
545 
549 #define RTE_MIN(a, b) \
550  __extension__ ({ \
551  typeof (a) _a = (a); \
552  typeof (b) _b = (b); \
553  _a < _b ? _a : _b; \
554  })
555 
559 #define RTE_MAX(a, b) \
560  __extension__ ({ \
561  typeof (a) _a = (a); \
562  typeof (b) _b = (b); \
563  _a > _b ? _a : _b; \
564  })
565 
566 /*********** Other general functions / macros ********/
567 
579 static inline uint32_t
580 rte_bsf32(uint32_t v)
581 {
582  return (uint32_t)__builtin_ctz(v);
583 }
584 
599 static inline int
600 rte_bsf32_safe(uint64_t v, uint32_t *pos)
601 {
602  if (v == 0)
603  return 0;
604 
605  *pos = rte_bsf32(v);
606  return 1;
607 }
608 
620 static inline uint32_t
621 rte_log2_u32(uint32_t v)
622 {
623  if (v == 0)
624  return 0;
625  v = rte_align32pow2(v);
626  return rte_bsf32(v);
627 }
628 
629 
641 static inline int
642 rte_fls_u32(uint32_t x)
643 {
644  return (x == 0) ? 0 : 32 - __builtin_clz(x);
645 }
646 
658 static inline int
659 rte_bsf64(uint64_t v)
660 {
661  return (uint32_t)__builtin_ctzll(v);
662 }
663 
678 static inline int
679 rte_bsf64_safe(uint64_t v, uint32_t *pos)
680 {
681  if (v == 0)
682  return 0;
683 
684  *pos = rte_bsf64(v);
685  return 1;
686 }
687 
700 static inline int
701 rte_fls_u64(uint64_t x)
702 {
703  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
704 }
705 
717 static inline uint32_t
718 rte_log2_u64(uint64_t v)
719 {
720  if (v == 0)
721  return 0;
722  v = rte_align64pow2(v);
723  /* we checked for v being 0 already, so no undefined behavior */
724  return rte_bsf64(v);
725 }
726 
727 #ifndef offsetof
728 
729 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
730 #endif
731 
746 #ifndef container_of
747 #define container_of(ptr, type, member) __extension__ ({ \
748  const typeof(((type *)0)->member) *_ptr = (ptr); \
749  __rte_unused type *_target_ptr = \
750  (type *)(ptr); \
751  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
752  })
753 #endif
754 
765 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
766 
767 #define _RTE_STR(x) #x
768 
769 #define RTE_STR(x) _RTE_STR(x)
770 
776 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
777 #define RTE_FMT_HEAD(fmt, ...) fmt
778 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
779 
781 #define RTE_LEN2MASK(ln, tp) \
782  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
783 
785 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
786 
801 static inline uint64_t
802 rte_str_to_size(const char *str)
803 {
804  char *endptr;
805  unsigned long long size;
806 
807  while (isspace((int)*str))
808  str++;
809  if (*str == '-')
810  return 0;
811 
812  errno = 0;
813  size = strtoull(str, &endptr, 0);
814  if (errno)
815  return 0;
816 
817  if (*endptr == ' ')
818  endptr++; /* allow 1 space gap */
819 
820  switch (*endptr){
821  case 'G': case 'g': size *= 1024; /* fall-through */
822  case 'M': case 'm': size *= 1024; /* fall-through */
823  case 'K': case 'k': size *= 1024; /* fall-through */
824  default:
825  break;
826  }
827  return size;
828 }
829 
843 __rte_noreturn void
844 rte_exit(int exit_code, const char *format, ...)
845  __rte_format_printf(2, 3);
846 
847 #ifdef __cplusplus
848 }
849 #endif
850 
851 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:341
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:443
static int rte_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:600
uint64_t rte_iova_t
Definition: rte_common.h:394
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:642
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:718
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:421
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:519
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:621
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:402
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:400
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:580
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:537
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:484
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:679
uint64_t phys_addr_t
Definition: rte_common.h:384
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:408
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:132
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:701
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:290
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:469
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:404
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:659
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:406
#define __rte_noreturn
Definition: rte_common.h:197
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:502
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:802
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_aligned(a)
Definition: rte_common.h:69