DPDK  20.02.1
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 
46 #ifdef RTE_TOOLCHAIN_GCC
47 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
48  __GNUC_PATCHLEVEL__)
49 #endif
50 
51 #ifdef RTE_ARCH_STRICT_ALIGN
52 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
53 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
54 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
55 #else
56 typedef uint64_t unaligned_uint64_t;
57 typedef uint32_t unaligned_uint32_t;
58 typedef uint16_t unaligned_uint16_t;
59 #endif
60 
64 #define __rte_aligned(a) __attribute__((__aligned__(a)))
65 
69 #define __rte_packed __attribute__((__packed__))
70 
71 /******* Macro to mark functions and fields scheduled for removal *****/
72 #define __rte_deprecated __attribute__((__deprecated__))
73 
77 #define __rte_weak __attribute__((__weak__))
78 
79 /*********** Macros to eliminate unused variable warnings ********/
80 
84 #define __rte_unused __attribute__((__unused__))
85 
90 #define RTE_SET_USED(x) (void)(x)
91 
92 #define RTE_PRIORITY_LOG 101
93 #define RTE_PRIORITY_BUS 110
94 #define RTE_PRIORITY_CLASS 120
95 #define RTE_PRIORITY_LAST 65535
96 
97 #define RTE_PRIO(prio) \
98  RTE_PRIORITY_ ## prio
99 
109 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
110 #define RTE_INIT_PRIO(func, prio) \
111 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
112 #endif
113 
122 #define RTE_INIT(func) \
123  RTE_INIT_PRIO(func, LAST)
124 
134 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
135 #define RTE_FINI_PRIO(func, prio) \
136 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
137 #endif
138 
147 #define RTE_FINI(func) \
148  RTE_FINI_PRIO(func, LAST)
149 
153 #define __rte_always_inline inline __attribute__((always_inline))
154 
158 #define __rte_noinline __attribute__((noinline))
159 
160 /*********** Macros for pointer arithmetic ********/
161 
165 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
166 
170 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
171 
177 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
178 
182 #define RTE_CAST_FIELD(var, field, type) \
183  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
184 
185 /*********** Macros/static functions for doing alignment ********/
186 
187 
194 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
195  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
196 
203 #define RTE_ALIGN_FLOOR(val, align) \
204  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
205 
212 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
213  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
214 
221 #define RTE_ALIGN_CEIL(val, align) \
222  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
223 
231 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
232 
240 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
241 
247 #define RTE_ALIGN_MUL_CEIL(v, mul) \
248  (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
249 
255 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
256  ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
257 
263 #define RTE_ALIGN_MUL_NEAR(v, mul) \
264  ({ \
265  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
266  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
267  (ceil - v) > (v - floor) ? floor : ceil; \
268  })
269 
281 static inline int
282 rte_is_aligned(void *ptr, unsigned align)
283 {
284  return RTE_PTR_ALIGN(ptr, align) == ptr;
285 }
286 
287 /*********** Macros for compile type checks ********/
288 
292 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
293 
294 /*********** Cache line related macros ********/
295 
297 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
298 
300 #define RTE_CACHE_LINE_ROUNDUP(size) \
301  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
302  RTE_CACHE_LINE_SIZE))
303 
305 #if RTE_CACHE_LINE_SIZE == 64
306 #define RTE_CACHE_LINE_SIZE_LOG2 6
307 #elif RTE_CACHE_LINE_SIZE == 128
308 #define RTE_CACHE_LINE_SIZE_LOG2 7
309 #else
310 #error "Unsupported cache line size"
311 #endif
312 
314 #define RTE_CACHE_LINE_MIN_SIZE 64
315 
317 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
318 
320 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
321 
322 /*********** PA/IOVA type definitions ********/
323 
325 typedef uint64_t phys_addr_t;
326 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
327 
335 typedef uint64_t rte_iova_t;
336 #define RTE_BAD_IOVA ((rte_iova_t)-1)
337 
338 /*********** Structure alignment markers ********/
339 
341 __extension__ typedef void *RTE_MARKER[0];
343 __extension__ typedef uint8_t RTE_MARKER8[0];
345 __extension__ typedef uint16_t RTE_MARKER16[0];
347 __extension__ typedef uint16_t RTE_MARKER32[0];
349 __extension__ typedef uint64_t RTE_MARKER64[0];
350 
361 static inline uint32_t
362 rte_combine32ms1b(register uint32_t x)
363 {
364  x |= x >> 1;
365  x |= x >> 2;
366  x |= x >> 4;
367  x |= x >> 8;
368  x |= x >> 16;
369 
370  return x;
371 }
372 
383 static inline uint64_t
384 rte_combine64ms1b(register uint64_t v)
385 {
386  v |= v >> 1;
387  v |= v >> 2;
388  v |= v >> 4;
389  v |= v >> 8;
390  v |= v >> 16;
391  v |= v >> 32;
392 
393  return v;
394 }
395 
396 /*********** Macros to work with powers of 2 ********/
397 
401 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
402 
409 static inline int
410 rte_is_power_of_2(uint32_t n)
411 {
412  return n && !(n & (n - 1));
413 }
414 
424 static inline uint32_t
425 rte_align32pow2(uint32_t x)
426 {
427  x--;
428  x = rte_combine32ms1b(x);
429 
430  return x + 1;
431 }
432 
442 static inline uint32_t
444 {
445  x = rte_combine32ms1b(x);
446 
447  return x - (x >> 1);
448 }
449 
459 static inline uint64_t
460 rte_align64pow2(uint64_t v)
461 {
462  v--;
463  v = rte_combine64ms1b(v);
464 
465  return v + 1;
466 }
467 
477 static inline uint64_t
479 {
480  v = rte_combine64ms1b(v);
481 
482  return v - (v >> 1);
483 }
484 
485 /*********** Macros for calculating min and max **********/
486 
490 #define RTE_MIN(a, b) \
491  __extension__ ({ \
492  typeof (a) _a = (a); \
493  typeof (b) _b = (b); \
494  _a < _b ? _a : _b; \
495  })
496 
500 #define RTE_MAX(a, b) \
501  __extension__ ({ \
502  typeof (a) _a = (a); \
503  typeof (b) _b = (b); \
504  _a > _b ? _a : _b; \
505  })
506 
507 /*********** Other general functions / macros ********/
508 
520 static inline uint32_t
521 rte_bsf32(uint32_t v)
522 {
523  return (uint32_t)__builtin_ctz(v);
524 }
525 
540 static inline int
541 rte_bsf32_safe(uint64_t v, uint32_t *pos)
542 {
543  if (v == 0)
544  return 0;
545 
546  *pos = rte_bsf32(v);
547  return 1;
548 }
549 
561 static inline uint32_t
562 rte_log2_u32(uint32_t v)
563 {
564  if (v == 0)
565  return 0;
566  v = rte_align32pow2(v);
567  return rte_bsf32(v);
568 }
569 
570 
582 static inline int
583 rte_fls_u32(uint32_t x)
584 {
585  return (x == 0) ? 0 : 32 - __builtin_clz(x);
586 }
587 
599 static inline int
600 rte_bsf64(uint64_t v)
601 {
602  return (uint32_t)__builtin_ctzll(v);
603 }
604 
619 static inline int
620 rte_bsf64_safe(uint64_t v, uint32_t *pos)
621 {
622  if (v == 0)
623  return 0;
624 
625  *pos = rte_bsf64(v);
626  return 1;
627 }
628 
641 static inline int
642 rte_fls_u64(uint64_t x)
643 {
644  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
645 }
646 
658 static inline uint32_t
659 rte_log2_u64(uint64_t v)
660 {
661  if (v == 0)
662  return 0;
663  v = rte_align64pow2(v);
664  /* we checked for v being 0 already, so no undefined behavior */
665  return rte_bsf64(v);
666 }
667 
668 #ifndef offsetof
669 
670 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
671 #endif
672 
687 #ifndef container_of
688 #define container_of(ptr, type, member) __extension__ ({ \
689  const typeof(((type *)0)->member) *_ptr = (ptr); \
690  __attribute__((unused)) type *_target_ptr = \
691  (type *)(ptr); \
692  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
693  })
694 #endif
695 
706 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
707 
708 #define _RTE_STR(x) #x
709 
710 #define RTE_STR(x) _RTE_STR(x)
711 
717 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
718 #define RTE_FMT_HEAD(fmt, ...) fmt
719 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
720 
722 #define RTE_LEN2MASK(ln, tp) \
723  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
724 
726 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
727 
742 static inline uint64_t
743 rte_str_to_size(const char *str)
744 {
745  char *endptr;
746  unsigned long long size;
747 
748  while (isspace((int)*str))
749  str++;
750  if (*str == '-')
751  return 0;
752 
753  errno = 0;
754  size = strtoull(str, &endptr, 0);
755  if (errno)
756  return 0;
757 
758  if (*endptr == ' ')
759  endptr++; /* allow 1 space gap */
760 
761  switch (*endptr){
762  case 'G': case 'g': size *= 1024; /* fall-through */
763  case 'M': case 'm': size *= 1024; /* fall-through */
764  case 'K': case 'k': size *= 1024; /* fall-through */
765  default:
766  break;
767  }
768  return size;
769 }
770 
784 void
785 rte_exit(int exit_code, const char *format, ...)
786  __attribute__((noreturn))
787  __attribute__((format(printf, 2, 3)));
788 
789 #ifdef __cplusplus
790 }
791 #endif
792 
793 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:282
static int rte_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:541
uint64_t rte_iova_t
Definition: rte_common.h:335
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:583
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:659
static uint64_t rte_combine64ms1b(register uint64_t v)
Definition: rte_common.h:384
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:460
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:562
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:343
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:341
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:521
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:478
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:425
__extension__ typedef uint16_t RTE_MARKER32[0]
Definition: rte_common.h:347
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:620
uint64_t phys_addr_t
Definition: rte_common.h:325
uint64_t unaligned_uint64_t
Definition: rte_common.h:56
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:349
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:642
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:231
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:410
void rte_exit(int exit_code, const char *format,...)
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:345
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:600
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:443
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:743
static uint32_t rte_combine32ms1b(register uint32_t x)
Definition: rte_common.h:362