DPDK  19.11.14
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 
74 #define __rte_may_alias __attribute__((__may_alias__))
75 
76 /******* Macro to mark functions and fields scheduled for removal *****/
77 #define __rte_deprecated __attribute__((__deprecated__))
78 
82 #define __rte_weak __attribute__((__weak__))
83 
84 /*********** Macros to eliminate unused variable warnings ********/
85 
89 #define __rte_unused __attribute__((__unused__))
90 
95 #define RTE_SET_USED(x) (void)(x)
96 
97 #define RTE_PRIORITY_LOG 101
98 #define RTE_PRIORITY_BUS 110
99 #define RTE_PRIORITY_CLASS 120
100 #define RTE_PRIORITY_LAST 65535
101 
102 #define RTE_PRIO(prio) \
103  RTE_PRIORITY_ ## prio
104 
114 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
115 #define RTE_INIT_PRIO(func, prio) \
116 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
117 #endif
118 
127 #define RTE_INIT(func) \
128  RTE_INIT_PRIO(func, LAST)
129 
139 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
140 #define RTE_FINI_PRIO(func, prio) \
141 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
142 #endif
143 
152 #define RTE_FINI(func) \
153  RTE_FINI_PRIO(func, LAST)
154 
158 #define __rte_always_inline inline __attribute__((always_inline))
159 
163 #define __rte_noinline __attribute__((noinline))
164 
165 /*********** Macros for pointer arithmetic ********/
166 
170 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
171 
175 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
176 
182 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
183 
187 #define RTE_CAST_FIELD(var, field, type) \
188  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
189 
190 /*********** Macros/static functions for doing alignment ********/
191 
192 
199 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
200  ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
201 
208 #define RTE_ALIGN_FLOOR(val, align) \
209  (typeof(val))((val) & (~((typeof(val))((align) - 1))))
210 
217 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
218  RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
219 
226 #define RTE_ALIGN_CEIL(val, align) \
227  RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
228 
236 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
237 
245 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
246 
252 #define RTE_ALIGN_MUL_CEIL(v, mul) \
253  ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
254 
260 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
261  (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
262 
268 #define RTE_ALIGN_MUL_NEAR(v, mul) \
269  ({ \
270  typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
271  typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
272  (ceil - (v)) > ((v) - floor) ? floor : ceil; \
273  })
274 
286 static inline int
287 rte_is_aligned(void *ptr, unsigned align)
288 {
289  return RTE_PTR_ALIGN(ptr, align) == ptr;
290 }
291 
292 /*********** Macros for compile type checks ********/
293 
297 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
298 
299 /*********** Cache line related macros ********/
300 
302 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
303 
305 #define RTE_CACHE_LINE_ROUNDUP(size) \
306  (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
307  RTE_CACHE_LINE_SIZE))
308 
310 #if RTE_CACHE_LINE_SIZE == 64
311 #define RTE_CACHE_LINE_SIZE_LOG2 6
312 #elif RTE_CACHE_LINE_SIZE == 128
313 #define RTE_CACHE_LINE_SIZE_LOG2 7
314 #else
315 #error "Unsupported cache line size"
316 #endif
317 
319 #define RTE_CACHE_LINE_MIN_SIZE 64
320 
322 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
323 
325 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
326 
327 /*********** PA/IOVA type definitions ********/
328 
330 typedef uint64_t phys_addr_t;
331 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
332 
340 typedef uint64_t rte_iova_t;
341 #define RTE_BAD_IOVA ((rte_iova_t)-1)
342 
343 
354 static inline uint32_t
355 rte_combine32ms1b(uint32_t x)
356 {
357  x |= x >> 1;
358  x |= x >> 2;
359  x |= x >> 4;
360  x |= x >> 8;
361  x |= x >> 16;
362 
363  return x;
364 }
365 
376 static inline uint64_t
377 rte_combine64ms1b(uint64_t v)
378 {
379  v |= v >> 1;
380  v |= v >> 2;
381  v |= v >> 4;
382  v |= v >> 8;
383  v |= v >> 16;
384  v |= v >> 32;
385 
386  return v;
387 }
388 
389 /*********** Macros to work with powers of 2 ********/
390 
394 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
395 
402 static inline int
403 rte_is_power_of_2(uint32_t n)
404 {
405  return n && !(n & (n - 1));
406 }
407 
417 static inline uint32_t
418 rte_align32pow2(uint32_t x)
419 {
420  x--;
421  x = rte_combine32ms1b(x);
422 
423  return x + 1;
424 }
425 
435 static inline uint32_t
437 {
438  x = rte_combine32ms1b(x);
439 
440  return x - (x >> 1);
441 }
442 
452 static inline uint64_t
453 rte_align64pow2(uint64_t v)
454 {
455  v--;
456  v = rte_combine64ms1b(v);
457 
458  return v + 1;
459 }
460 
470 static inline uint64_t
472 {
473  v = rte_combine64ms1b(v);
474 
475  return v - (v >> 1);
476 }
477 
478 /*********** Macros for calculating min and max **********/
479 
483 #define RTE_MIN(a, b) \
484  __extension__ ({ \
485  typeof (a) _a = (a); \
486  typeof (b) _b = (b); \
487  _a < _b ? _a : _b; \
488  })
489 
493 #define RTE_MAX(a, b) \
494  __extension__ ({ \
495  typeof (a) _a = (a); \
496  typeof (b) _b = (b); \
497  _a > _b ? _a : _b; \
498  })
499 
500 /*********** Other general functions / macros ********/
501 
513 static inline uint32_t
514 rte_bsf32(uint32_t v)
515 {
516  return (uint32_t)__builtin_ctz(v);
517 }
518 
533 static inline int
534 rte_bsf32_safe(uint64_t v, uint32_t *pos)
535 {
536  if (v == 0)
537  return 0;
538 
539  *pos = rte_bsf32(v);
540  return 1;
541 }
542 
554 static inline uint32_t
555 rte_log2_u32(uint32_t v)
556 {
557  if (v == 0)
558  return 0;
559  v = rte_align32pow2(v);
560  return rte_bsf32(v);
561 }
562 
563 
575 static inline int
576 rte_fls_u32(uint32_t x)
577 {
578  return (x == 0) ? 0 : 32 - __builtin_clz(x);
579 }
580 
592 static inline int
593 rte_bsf64(uint64_t v)
594 {
595  return (uint32_t)__builtin_ctzll(v);
596 }
597 
612 static inline int
613 rte_bsf64_safe(uint64_t v, uint32_t *pos)
614 {
615  if (v == 0)
616  return 0;
617 
618  *pos = rte_bsf64(v);
619  return 1;
620 }
621 
634 static inline int
635 rte_fls_u64(uint64_t x)
636 {
637  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
638 }
639 
651 static inline uint32_t
652 rte_log2_u64(uint64_t v)
653 {
654  if (v == 0)
655  return 0;
656  v = rte_align64pow2(v);
657  /* we checked for v being 0 already, so no undefined behavior */
658  return rte_bsf64(v);
659 }
660 
661 #ifndef offsetof
662 
663 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
664 #endif
665 
680 #ifndef container_of
681 #define container_of(ptr, type, member) __extension__ ({ \
682  const typeof(((type *)0)->member) *_ptr = (ptr); \
683  __attribute__((unused)) type *_target_ptr = \
684  (type *)(ptr); \
685  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
686  })
687 #endif
688 
699 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
700 
701 #define _RTE_STR(x) #x
702 
703 #define RTE_STR(x) _RTE_STR(x)
704 
710 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
711 #define RTE_FMT_HEAD(fmt, ...) fmt
712 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
713 
715 #define RTE_LEN2MASK(ln, tp) \
716  ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
717 
719 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
720 
735 static inline uint64_t
736 rte_str_to_size(const char *str)
737 {
738  char *endptr;
739  unsigned long long size;
740 
741  while (isspace((int)*str))
742  str++;
743  if (*str == '-')
744  return 0;
745 
746  errno = 0;
747  size = strtoull(str, &endptr, 0);
748  if (errno)
749  return 0;
750 
751  if (*endptr == ' ')
752  endptr++; /* allow 1 space gap */
753 
754  switch (*endptr){
755  case 'G': case 'g': size *= 1024; /* fall-through */
756  case 'M': case 'm': size *= 1024; /* fall-through */
757  case 'K': case 'k': size *= 1024; /* fall-through */
758  default:
759  break;
760  }
761  return size;
762 }
763 
777 void
778 rte_exit(int exit_code, const char *format, ...)
779  __attribute__((noreturn))
780  __attribute__((format(printf, 2, 3)));
781 
782 #ifdef __cplusplus
783 }
784 #endif
785 
786 #endif
static int rte_is_aligned(void *ptr, unsigned align)
Definition: rte_common.h:287
static uint64_t rte_combine64ms1b(uint64_t v)
Definition: rte_common.h:377
static int rte_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:534
uint64_t rte_iova_t
Definition: rte_common.h:340
static int rte_fls_u32(uint32_t x)
Definition: rte_common.h:576
static uint32_t rte_log2_u64(uint64_t v)
Definition: rte_common.h:652
static uint32_t rte_combine32ms1b(uint32_t x)
Definition: rte_common.h:355
static uint64_t rte_align64pow2(uint64_t v)
Definition: rte_common.h:453
static uint32_t rte_log2_u32(uint32_t v)
Definition: rte_common.h:555
static uint32_t rte_bsf32(uint32_t v)
Definition: rte_common.h:514
static uint64_t rte_align64prevpow2(uint64_t v)
Definition: rte_common.h:471
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:418
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:613
uint64_t phys_addr_t
Definition: rte_common.h:330
uint64_t unaligned_uint64_t
Definition: rte_common.h:56
static int rte_fls_u64(uint64_t x)
Definition: rte_common.h:635
#define RTE_PTR_ALIGN(ptr, align)
Definition: rte_common.h:236
static int rte_is_power_of_2(uint32_t n)
Definition: rte_common.h:403
void rte_exit(int exit_code, const char *format,...)
static int rte_bsf64(uint64_t v)
Definition: rte_common.h:593
static uint32_t rte_align32prevpow2(uint32_t x)
Definition: rte_common.h:436
static uint64_t rte_str_to_size(const char *str)
Definition: rte_common.h:736